diff --git a/.github/workflows/genesis-openembedded.yml b/.github/workflows/genesis-openembedded.yml index 1d09ef4..ee8f3e5 100644 --- a/.github/workflows/genesis-openembedded.yml +++ b/.github/workflows/genesis-openembedded.yml @@ -24,7 +24,7 @@ jobs: apt-utils bats createrepo-c dpkg-dev gnupg \ libfile-slurper-perl \ libparallel-forkmanager-perl libperl-critic-perl rpm rpm2cpio cpio \ - grub-common ca-certificates + grub-common ca-certificates build-essential libssl-dev patch - name: Install the arm64-efi grub modules # t/aarch64_target.t runs the spec's own grub2-mkimage command, and it skips without @@ -46,6 +46,8 @@ jobs: perl -c ipxe-xcat/sbuild.pl perl -c ipxe-xcat/verify-payload.pl rpmspec -P ipxe-xcat/ipxe-xcat.spec >/dev/null + perl -c uftp/mockbuild.pl + rpmspec -P uftp/uftp.spec >/dev/null rpmspec -P \ -D 'genesis_arch x86_64' \ -D 'version 2.19.0' \ @@ -61,12 +63,14 @@ jobs: lib/XCAT/BuildUtils.pm \ lib/XCAT/GenesisRelease.pm \ mockbuild-all.pl \ + native/uftp-fixes.t \ t/build_utils.t \ t/common-repo-gate.t \ t/genesis_openembedded_release.t \ t/genesis_openembedded_consumer.t \ t/ipxe_xcat_payload.t \ - t/lib/XCAT/GenesisReleaseTest.pm + t/lib/XCAT/GenesisReleaseTest.pm \ + uftp/mockbuild.pl - name: Run package tests run: | @@ -104,7 +108,7 @@ jobs: sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 prove -v native/buildinfo_provenance.t native/goconserver-openeuler.t \ native/openeuler-power-inputs.t native/openeuler-srpm.t \ - native/xnba-release-suffix.t 2>&1 | tee native.tap + native/xnba-release-suffix.t native/uftp-fixes.t 2>&1 | tee native.tap # prove returns success when a test file skips all its checks. if grep -E '^1\.\.0 # SKIP|^native/[^ ]+\.t \.+ skipped' native.tap; then echo 'a native test file was skipped' >&2 diff --git a/native/README.md b/native/README.md index e999353..15aeeee 100644 --- a/native/README.md +++ b/native/README.md @@ -8,6 +8,10 @@ native tools: prove -v native/*.t ``` +`uftp-fixes.t` builds the UFTP programs from the archive and patches in `uftp/` +with AddressSanitizer. It needs a C compiler, `make`, `patch` and the OpenSSL +headers. + Use an ordinary user for RPM builds. `openeuler-power-inputs.t` exercises the whole build owner only on POWER with native Mock; its RPM admission checks also run on x86_64. When running that test as root, set `XCAT_TEST_BUILD_USER` to an diff --git a/native/fixtures/uftp/announce_blocksize.c b/native/fixtures/uftp/announce_blocksize.c new file mode 100644 index 0000000..813fefd --- /dev/null +++ b/native/fixtures/uftp/announce_blocksize.c @@ -0,0 +1,64 @@ +/* + * Gives read_announce() of the uftp receiver an unencrypted ANNOUNCE with each block size in turn, + * while descriptor 0 holds the file of another transfer. + * Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "uftp_common.h" + +int read_announce(struct group_list_t *group, unsigned char *packet, union sockaddr_u *src, + struct timeval rxtime, int packetlen); + +static void announce(unsigned int blocksize) +{ + unsigned char packet[sizeof(struct uftp_h) + sizeof(struct announce_h) + 8]; + struct uftp_h *header = (struct uftp_h *)packet; + struct announce_h *ann = (struct announce_h *)(packet + sizeof(struct uftp_h)); + struct group_list_t group; + union sockaddr_u src; + struct timeval now; + + memset(packet, 0, sizeof(packet)); + memset(&group, 0, sizeof(group)); + memset(&src, 0, sizeof(src)); + src.sin.sin_family = AF_INET; + header->version = UFTP_VER_NUM; + header->func = ANNOUNCE; + header->group_id = htonl(1); + ann->func = ANNOUNCE; + ann->hlen = (sizeof(struct announce_h) + 8) / 4; + ann->robust = 2; + ann->blocksize = htons(blocksize); + gettimeofday(&now, NULL); + printf("block size %u: %s\n", blocksize, + read_announce(&group, packet, &src, now, sizeof(packet)) ? "accepted" : "rejected"); +} + +int main(void) +{ + static const unsigned int sizes[] = { 0, 511, 512, 1300, MAXMTU - 200, MAXMTU - 199, 65535 }; + char path[] = "/tmp/uftp-fd0-XXXXXX"; + unsigned int i; + + applog = stderr; + listener = INVALID_SOCKET; + close(0); + if (mkstemp(path) != 0) { + fprintf(stderr, "descriptor 0 is not free\n"); + return 2; + } + for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) { + announce(sizes[i]); + } + printf("fd 0: %s\n", fcntl(0, F_GETFD) == -1 ? "closed" : "open"); + unlink(path); + return 0; +} diff --git a/native/fixtures/uftp/backup_link.c b/native/fixtures/uftp/backup_link.c new file mode 100644 index 0000000..dcaaf66 --- /dev/null +++ b/native/fixtures/uftp/backup_link.c @@ -0,0 +1,161 @@ +/* + * Receives into a destination directory with a backup directory (-D and -A, without -T). One + * group creates a symbolic link to a directory outside both, and the next replaces it with a + * directory, which moves the link into the backup directory. Two more groups create a file below + * that directory and overwrite it, which backs the file up below the moved link. A last pair of + * groups overwrites a file without a link in the way, and three groups receive a file whose backup + * path is longer than MAXPATHNAME. Built with the receiver objects and AddressSanitizer by + * native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_common.h" +#include "client_config.h" +#include "client_fileinfo.h" +#include "uftp_common.h" + +static void start_group(struct group_list_t *group, uint32_t group_id) +{ + memset(group, 0, sizeof(*group)); + group->version = UFTP_VER_NUM; + group->group_id = group_id; + group->blocksize = 1300; + group->phase = PHASE_MIDGROUP; + group->fileinfo.fd = -1; + snprintf(group->start_date, sizeof(group->start_date), "20261018"); + snprintf(group->start_time, sizeof(group->start_time), "120000"); +} + +static void send_fileinfo(struct group_list_t *group, int ftype, const char *name, const char *link, + uint32_t size) +{ + unsigned char message[sizeof(struct fileinfo_h) + 2 * MAXPATHNAME + 8]; + struct fileinfo_h *fileinfo = (struct fileinfo_h *)message; + int namelen = (int)(strlen(name) + 4) / 4; + int linklen = link ? (int)(strlen(link) + 4) / 4 : 0; + struct timeval now; + + memset(message, 0, sizeof(message)); + fileinfo->func = FILEINFO; + fileinfo->hlen = (sizeof(struct fileinfo_h) / 4) + namelen + linklen; + fileinfo->file_id = htons(1); + fileinfo->ftype = ftype; + fileinfo->namelen = namelen; + fileinfo->linklen = linklen; + fileinfo->lofsize = htonl(size); + memcpy(message + sizeof(struct fileinfo_h), name, strlen(name)); + if (link) { + memcpy(message + sizeof(struct fileinfo_h) + (namelen * 4), link, strlen(link)); + } + memcpy(message + (fileinfo->hlen * 4), &uid, sizeof(uid)); + gettimeofday(&now, NULL); + handle_fileinfo(group, message, (fileinfo->hlen * 4) + sizeof(uid), now); +} + +/* Receives a file that holds content, as one group. */ +static void receive(uint32_t group_id, const char *name, const char *content) +{ + struct group_list_t group; + + start_group(&group, group_id); + send_fileinfo(&group, FTYPE_REG, name, NULL, 2600); + if ((group.fileinfo.fd >= 0) && + (write(group.fileinfo.fd, content, strlen(content)) != (ssize_t)strlen(content))) { + perror("write"); + } + file_cleanup(&group, 0); +} + +static void report(const char *label, const char *path) +{ + char buf[64]; + ssize_t len = -1; + int fd; + + if ((fd = open(path, O_RDONLY)) != -1) { + len = read(fd, buf, sizeof(buf) - 1); + close(fd); + } + if (len < 0) { + printf("%s: absent\n", label); + } else { + buf[len] = '\x0'; + printf("%s: %s\n", label, buf); + } +} + +int main(void) +{ + struct group_list_t group; + char base[] = "/tmp/uftp-backup-XXXXXX", path[MAXBACKUPPATHNAME], outside[MAXPATHNAME]; + char name[MAXPATHNAME], dir[60]; + int fd, len; + + if (!mkdtemp(base)) { + perror("mkdtemp"); + return 2; + } + applog = stderr; + listener = INVALID_SOCKET; + cache_len = DEF_CACHE; + uid = htonl(0x0A000001); + snprintf(destdir[0], sizeof(destdir[0]), "%s/dest", base); + destdircnt = 1; + mkdir(destdir[0], 0755); + snprintf(backupdir[0], sizeof(backupdir[0]), "%s/backup", base); + backupcnt = 1; + mkdir(backupdir[0], 0755); + snprintf(outside, sizeof(outside), "%s/outside", base); + mkdir(outside, 0755); + snprintf(path, sizeof(path), "%s/outside/sub", base); + mkdir(path, 0755); + snprintf(path, sizeof(path), "%s/outside/sub/victim", base); + if ((fd = open(path, O_CREAT | O_WRONLY, 0644)) != -1) { + if (write(fd, "outside", 7) != 7) { + perror("write"); + } + close(fd); + } + + start_group(&group, 1); + send_fileinfo(&group, FTYPE_LINK, "up", outside, 0); + start_group(&group, 2); + send_fileinfo(&group, FTYPE_DIR, "up", NULL, 0); + receive(3, "up/sub/victim", "first"); + receive(4, "up/sub/victim", "second"); + report("outside victim", path); + snprintf(path, sizeof(path), "%s/up/sub/victim", destdir[0]); + report("received file", path); + + receive(5, "plain", "first"); + receive(6, "plain", "second"); + snprintf(path, sizeof(path), "%s/20261018/120000/%s/plain", backupdir[0], destdir[0]); + report("backup of plain", path); + snprintf(path, sizeof(path), "%s/plain", destdir[0]); + report("plain", path); + + /* Four directories and a file name that bring the file path 25 bytes below MAXPATHNAME, + which leaves room for the temp file suffix. */ + memset(dir, 'd', sizeof(dir) - 1); + dir[sizeof(dir) - 1] = '\x0'; + len = snprintf(name, sizeof(name), "%s/%s/%s/%s/", dir, dir, dir, dir); + memset(name + len, 'f', MAXPATHNAME - 25 - (strlen(destdir[0]) + 1) - len); + name[MAXPATHNAME - 25 - (strlen(destdir[0]) + 1)] = '\x0'; + receive(7, name, "first"); + receive(8, name, "second"); + receive(9, name, "third"); + snprintf(path, sizeof(path), "%s/20261018/120000/%s/%s", backupdir[0], destdir[0], name); + printf("long backup path: %zu bytes\n", strlen(path)); + report("backup of long", path); + snprintf(path, sizeof(path), "%s/%s", destdir[0], name); + report("long", path); + return 0; +} diff --git a/native/fixtures/uftp/cache_boundary.c b/native/fixtures/uftp/cache_boundary.c new file mode 100644 index 0000000..496f4ce --- /dev/null +++ b/native/fixtures/uftp/cache_boundary.c @@ -0,0 +1,143 @@ +/* + * Receives a file through handle_fileseg() of the uftp receiver and checks the file on disk. + * "short-tail" ends the file with a short block at the end of the default disk cache. + * "long-repeat" then sends that last block again with a full block of data. + * "short-last" sends a last block shorter than the end of the file, then the right one. + * "lost-blocks" loses blocks before a full disk cache of the smallest size with the largest blocks. + * Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_common.h" +#include "client_config.h" +#include "client_transfer.h" +#include "uftp_common.h" + +void handle_fileinfo_regular(struct group_list_t *group); + +static unsigned char byte_at(f_offset_t offset) +{ + return (unsigned char)(offset * 7 + offset / 251); +} + +/* Sends a block with len bytes of file data, or with the data of the file when len is 0. */ +static void send_block(struct group_list_t *group, uint32_t block, int len) +{ + f_offset_t start = (f_offset_t)block * group->blocksize; + int i; + unsigned char *message; + struct fileseg_h *fileseg; + + if (!len) { + len = group->blocksize; + if (group->fileinfo.size - start < len) { + len = (int)(group->fileinfo.size - start); + } + } + message = calloc(sizeof(struct fileseg_h) + len, 1); + fileseg = (struct fileseg_h *)message; + fileseg->func = FILESEG; + fileseg->hlen = sizeof(struct fileseg_h) / 4; + fileseg->file_id = htons(group->file_id); + fileseg->section = htons(0); + fileseg->sec_block = htons(block); + for (i = 0; i < len; i++) { + message[sizeof(struct fileseg_h) + i] = byte_at(start + i); + } + handle_fileseg(group, message, sizeof(struct fileseg_h) + len, 0); + free(message); +} + +int main(int argc, char **argv) +{ + static const uint32_t lost_order[] = { 0, 5, 6, 7, 1, 2, 3, 4 }; + struct group_list_t group; + char path[] = "/tmp/uftp-cache-XXXXXX"; + unsigned char *data; + uint32_t block, missing = 0; + f_offset_t i, differ = 0; + int lost, fd; + FILE *fp; + + if (argc != 2) { + fprintf(stderr, "usage: %s short-tail|long-repeat|short-last|lost-blocks\n", argv[0]); + return 2; + } + lost = !strcmp(argv[1], "lost-blocks"); + memset(&group, 0, sizeof(group)); + group.file_id = 1; + group.max_txseq = 1; + group.fileinfo.ftype = FTYPE_REG; + if (lost) { + cache_len = 10240; + group.blocksize = 8800; + group.fileinfo.size = 7 * 8800 + 100; + } else { + /* 1300 bytes is the default block size of the sender. */ + cache_len = DEF_CACHE; + group.blocksize = 1300; + group.fileinfo.size = (f_offset_t)(DEF_CACHE / 1300) * 1300 + DEF_CACHE % 1300; + if (!strcmp(argv[1], "short-last")) { + /* The right last block does not fit after block 805, a short one does. */ + group.fileinfo.size += 1000 - DEF_CACHE % 1300; + } + } + group.fileinfo.blocks = + (uint32_t)((group.fileinfo.size + group.blocksize - 1) / group.blocksize); + group.fileinfo.sections = 1; + group.fileinfo.big_sections = 1; + group.fileinfo.secsize_big = group.fileinfo.blocks; + group.fileinfo.secsize_small = group.fileinfo.blocks; + if ((fd = mkstemp(path)) == -1) { + perror("mkstemp"); + return 2; + } + close(fd); + strncpy(group.fileinfo.filepath, path, sizeof(group.fileinfo.filepath) - 1); + applog = stderr; + listener = INVALID_SOCKET; + + handle_fileinfo_regular(&group); + if (lost) { + for (block = 0; block < sizeof(lost_order) / sizeof(lost_order[0]); block++) { + send_block(&group, lost_order[block], 0); + } + } else { + for (block = 0; block < group.fileinfo.blocks - 1; block++) { + send_block(&group, block, 0); + } + if (!strcmp(argv[1], "short-last")) { + send_block(&group, block, 500); + } + send_block(&group, block, 0); + if (!strcmp(argv[1], "long-repeat")) { + send_block(&group, block, group.blocksize); + } + } + flush_disk_cache(&group); + close(group.fileinfo.fd); + + for (block = 0; block < group.fileinfo.blocks; block++) { + missing += group.fileinfo.naklist[block]; + } + data = calloc(group.fileinfo.size + 1, 1); + fp = fopen(path, "rb"); + if (!fp || fread(data, 1, group.fileinfo.size + 1, fp) != (size_t)group.fileinfo.size) { + differ = 1; + } + for (i = 0; !differ && i < group.fileinfo.size; i++) { + differ = data[i] != byte_at(i); + } + if (fp) { + fclose(fp); + } + unlink(path); + printf("%s: %u blocks, %u missing, file %s\n", argv[1], group.fileinfo.blocks, missing, + differ ? "differs" : "matches"); + return 0; +} diff --git a/native/fixtures/uftp/cleanup_link.c b/native/fixtures/uftp/cleanup_link.c new file mode 100644 index 0000000..884f1ff --- /dev/null +++ b/native/fixtures/uftp/cleanup_link.c @@ -0,0 +1,104 @@ +/* + * Starts a file in one group of the uftp receiver, replaces a directory of its path with a symbolic + * link from a second group, then aborts the first file. A third group aborts a file normally. + * Receives straight into the destination directory (-D without -T). + * Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_common.h" +#include "client_config.h" +#include "client_fileinfo.h" +#include "uftp_common.h" + +static void start_group(struct group_list_t *group, uint32_t group_id) +{ + memset(group, 0, sizeof(*group)); + group->version = UFTP_VER_NUM; + group->group_id = group_id; + group->blocksize = 1300; + group->phase = PHASE_MIDGROUP; + group->fileinfo.fd = -1; +} + +static void send_fileinfo(struct group_list_t *group, int ftype, const char *name, const char *link, + uint32_t size) +{ + unsigned char message[sizeof(struct fileinfo_h) + 2 * MAXPATHNAME + 8]; + struct fileinfo_h *fileinfo = (struct fileinfo_h *)message; + int namelen = (int)(strlen(name) + 4) / 4; + int linklen = link ? (int)(strlen(link) + 4) / 4 : 0; + struct timeval now; + + memset(message, 0, sizeof(message)); + fileinfo->func = FILEINFO; + fileinfo->hlen = (sizeof(struct fileinfo_h) / 4) + namelen + linklen; + fileinfo->file_id = htons(1); + fileinfo->ftype = ftype; + fileinfo->namelen = namelen; + fileinfo->linklen = linklen; + fileinfo->lofsize = htonl(size); + memcpy(message + sizeof(struct fileinfo_h), name, strlen(name)); + if (link) { + memcpy(message + sizeof(struct fileinfo_h) + (namelen * 4), link, strlen(link)); + } + memcpy(message + (fileinfo->hlen * 4), &uid, sizeof(uid)); + gettimeofday(&now, NULL); + handle_fileinfo(group, message, (fileinfo->hlen * 4) + sizeof(uid), now); +} + +static void report(const char *label, const char *path) +{ + struct stat statbuf; + + printf("%s: %s\n", label, lstat(path, &statbuf) == 0 ? "present" : "absent"); +} + +int main(void) +{ + struct group_list_t first, second, third; + char base[] = "/tmp/uftp-cleanup-XXXXXX", path[MAXPATHNAME]; + int fd; + + if (!mkdtemp(base)) { + perror("mkdtemp"); + return 2; + } + applog = stderr; + listener = INVALID_SOCKET; + cache_len = DEF_CACHE; + uid = htonl(0x0A000001); + snprintf(destdir[0], sizeof(destdir[0]), "%s/dest", base); + destdircnt = 1; + mkdir(destdir[0], 0755); + snprintf(path, sizeof(path), "%s/outside", base); + mkdir(path, 0755); + snprintf(path, sizeof(path), "%s/outside/sub", base); + mkdir(path, 0755); + snprintf(path, sizeof(path), "%s/outside/sub/victim", base); + if ((fd = open(path, O_CREAT | O_WRONLY, 0644)) != -1) { + close(fd); + } + + start_group(&first, 1); + send_fileinfo(&first, FTYPE_REG, "a/sub/victim", NULL, 2600); + start_group(&second, 2); + send_fileinfo(&second, FTYPE_LINK, "a", "../outside", 0); + file_cleanup(&first, 1); + report("outside victim", path); + + start_group(&third, 3); + send_fileinfo(&third, FTYPE_REG, "c/partial", NULL, 2600); + file_cleanup(&third, 1); + snprintf(path, sizeof(path), "%s/c/partial", destdir[0]); + report("aborted partial", path); + return 0; +} diff --git a/native/fixtures/uftp/cleanup_replaced.c b/native/fixtures/uftp/cleanup_replaced.c new file mode 100644 index 0000000..a54f312 --- /dev/null +++ b/native/fixtures/uftp/cleanup_replaced.c @@ -0,0 +1,122 @@ +/* + * Starts a file in one group of the uftp receiver, then receives a file of the same name in a + * second group, which replaces the first file, and completes it. The first group then ends: it + * aborts, or with "complete" it completes. A third group completes a file and a fourth aborts + * one without another group in the way. Receives straight into the destination directory (-D + * without -T). Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_common.h" +#include "client_config.h" +#include "client_fileinfo.h" +#include "uftp_common.h" + +#define TSTAMP 1000000000 + +static void start_group(struct group_list_t *group, uint32_t group_id) +{ + memset(group, 0, sizeof(*group)); + group->version = UFTP_VER_NUM; + group->group_id = group_id; + group->blocksize = 1300; + group->phase = PHASE_MIDGROUP; + group->fileinfo.fd = -1; +} + +static void send_fileinfo(struct group_list_t *group, const char *name) +{ + unsigned char message[sizeof(struct fileinfo_h) + MAXPATHNAME + 8]; + struct fileinfo_h *fileinfo = (struct fileinfo_h *)message; + int namelen = (int)(strlen(name) + 4) / 4; + struct timeval now; + + memset(message, 0, sizeof(message)); + fileinfo->func = FILEINFO; + fileinfo->hlen = (sizeof(struct fileinfo_h) / 4) + namelen; + fileinfo->file_id = htons(1); + fileinfo->ftype = FTYPE_REG; + fileinfo->namelen = namelen; + fileinfo->lofsize = htonl(2600); + memcpy(message + sizeof(struct fileinfo_h), name, strlen(name)); + memcpy(message + (fileinfo->hlen * 4), &uid, sizeof(uid)); + gettimeofday(&now, NULL); + handle_fileinfo(group, message, (fileinfo->hlen * 4) + sizeof(uid), now); + group->fileinfo.tstamp = TSTAMP; +} + +static void report(const char *label, const char *path, const char *content) +{ + struct stat statbuf; + char buf[64]; + ssize_t len = -1; + int fd; + + if ((fd = open(path, O_RDONLY)) != -1) { + len = read(fd, buf, sizeof(buf) - 1); + close(fd); + } + if (len < 0 || lstat(path, &statbuf) == -1) { + printf("%s: absent\n", label); + return; + } + buf[len] = '\x0'; + printf("%s: %s, time %s\n", label, strcmp(buf, content) ? "other content" : content, + statbuf.st_mtime == TSTAMP ? "set" : "kept"); +} + +int main(int argc, char **argv) +{ + struct group_list_t first, second, third, fourth; + char base[] = "/tmp/uftp-replaced-XXXXXX", path[MAXPATHNAME]; + int complete = argc > 1 && !strcmp(argv[1], "complete"); + + if (!mkdtemp(base)) { + perror("mkdtemp"); + return 2; + } + applog = stderr; + listener = INVALID_SOCKET; + cache_len = DEF_CACHE; + uid = htonl(0x0A000001); + snprintf(destdir[0], sizeof(destdir[0]), "%s/dest", base); + destdircnt = 1; + mkdir(destdir[0], 0755); + + start_group(&first, 1); + send_fileinfo(&first, "image"); + start_group(&second, 2); + send_fileinfo(&second, "image"); + if (write(second.fileinfo.fd, "second", 6) != 6) { + perror("write"); + } + second.fileinfo.tstamp = 0; + file_cleanup(&second, 0); + file_cleanup(&first, !complete); + snprintf(path, sizeof(path), "%s/image", destdir[0]); + report("replaced file", path, "second"); + + start_group(&third, 3); + send_fileinfo(&third, "done"); + if (write(third.fileinfo.fd, "third", 5) != 5) { + perror("write"); + } + file_cleanup(&third, 0); + snprintf(path, sizeof(path), "%s/done", destdir[0]); + report("completed file", path, "third"); + + start_group(&fourth, 4); + send_fileinfo(&fourth, "partial"); + file_cleanup(&fourth, 1); + snprintf(path, sizeof(path), "%s/partial", destdir[0]); + report("aborted file", path, ""); + return 0; +} diff --git a/native/fixtures/uftp/cleanup_restart.c b/native/fixtures/uftp/cleanup_restart.c new file mode 100644 index 0000000..0d88794 --- /dev/null +++ b/native/fixtures/uftp/cleanup_restart.c @@ -0,0 +1,111 @@ +/* + * Interrupts a file in a group of the uftp receiver, restarts the group, replaces a directory of + * the path of that file with a symbolic link from the restarted group, and aborts it without the + * file. A second group restarts the same way without the link. Receives into a temp directory + * (-T). Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_common.h" +#include "client_config.h" +#include "client_fileinfo.h" +#include "uftp_common.h" + +static void start_group(struct group_list_t *group, uint32_t group_id, uint8_t group_inst) +{ + memset(group, 0, sizeof(*group)); + group->version = UFTP_VER_NUM; + group->group_id = group_id; + group->group_inst = group_inst; + group->blocksize = 1300; + group->phase = PHASE_MIDGROUP; + group->fileinfo.fd = -1; +} + +static void send_fileinfo(struct group_list_t *group, int ftype, const char *name, const char *link, + uint32_t size) +{ + unsigned char message[sizeof(struct fileinfo_h) + 2 * MAXPATHNAME + 8]; + struct fileinfo_h *fileinfo = (struct fileinfo_h *)message; + int namelen = (int)(strlen(name) + 4) / 4; + int linklen = link ? (int)(strlen(link) + 4) / 4 : 0; + struct timeval now; + + memset(message, 0, sizeof(message)); + fileinfo->func = FILEINFO; + fileinfo->hlen = (sizeof(struct fileinfo_h) / 4) + namelen + linklen; + fileinfo->file_id = htons(1); + fileinfo->ftype = ftype; + fileinfo->namelen = namelen; + fileinfo->linklen = linklen; + fileinfo->lofsize = htonl(size); + memcpy(message + sizeof(struct fileinfo_h), name, strlen(name)); + if (link) { + memcpy(message + sizeof(struct fileinfo_h) + (namelen * 4), link, strlen(link)); + } + memcpy(message + (fileinfo->hlen * 4), &uid, sizeof(uid)); + gettimeofday(&now, NULL); + handle_fileinfo(group, message, (fileinfo->hlen * 4) + sizeof(uid), now); +} + +static void report(const char *label, const char *path) +{ + struct stat statbuf; + + printf("%s: %s\n", label, lstat(path, &statbuf) == 0 ? "present" : "absent"); +} + +int main(void) +{ + struct group_list_t first, restarted; + char base[] = "/tmp/uftp-restart-XXXXXX", path[MAXPATHNAME]; + int fd; + + if (!mkdtemp(base)) { + perror("mkdtemp"); + return 2; + } + applog = stderr; + listener = INVALID_SOCKET; + cache_len = DEF_CACHE; + uid = htonl(0x0A000001); + snprintf(tempdir, sizeof(tempdir), "%s/temp", base); + mkdir(tempdir, 0755); + snprintf(destdir[0], sizeof(destdir[0]), "%s/dest", base); + destdircnt = 1; + mkdir(destdir[0], 0755); + snprintf(path, sizeof(path), "%s/outside", base); + mkdir(path, 0755); + snprintf(path, sizeof(path), "%s/outside/victim", base); + if ((fd = open(path, O_CREAT | O_WRONLY, 0644)) != -1) { + close(fd); + } + + start_group(&first, 1, 0); + send_fileinfo(&first, FTYPE_REG, "a/victim", NULL, 2600); + file_cleanup(&first, 1); + start_group(&restarted, 1, 1); + read_restart_file(&restarted); + send_fileinfo(&restarted, FTYPE_LINK, "a", "../../outside", 0); + file_cleanup(&restarted, 1); + report("outside victim", path); + + start_group(&first, 2, 0); + send_fileinfo(&first, FTYPE_REG, "c/partial", NULL, 2600); + file_cleanup(&first, 1); + snprintf(path, sizeof(path), "%s/_group_00000002/c/partial", tempdir); + report("interrupted partial", path); + start_group(&restarted, 2, 1); + read_restart_file(&restarted); + file_cleanup(&restarted, 1); + report("unused partial", path); + return 0; +} diff --git a/native/fixtures/uftp/dest_root.c b/native/fixtures/uftp/dest_root.c new file mode 100644 index 0000000..ac6d4cf --- /dev/null +++ b/native/fixtures/uftp/dest_root.c @@ -0,0 +1,105 @@ +/* + * Gives handle_fileinfo() of the uftp receiver delete requests and regular files whose names + * resolve to the destination directory, and checks after each that a file in that directory + * survives. A delete request for a file and a free space request for "." show the requests that + * still work. Receives straight into the destination directory (-D without -T). Built with the + * receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_common.h" +#include "client_config.h" +#include "client_fileinfo.h" +#include "uftp_common.h" + +static char keep[MAXPATHNAME], other[MAXPATHNAME]; + +static void create(const char *path) +{ + int fd; + + if ((fd = open(path, O_CREAT | O_WRONLY, 0644)) != -1) { + close(fd); + } +} + +static const char *exists(const char *path) +{ + struct stat statbuf; + + return lstat(path, &statbuf) == 0 ? "present" : "absent"; +} + +static void request(const char *label, int ftype, const char *name) +{ + unsigned char message[sizeof(struct fileinfo_h) + MAXPATHNAME + 8]; + struct fileinfo_h *fileinfo = (struct fileinfo_h *)message; + struct group_list_t group; + int namelen = (int)(strlen(name) + 4) / 4; + struct timeval now; + + create(keep); + create(other); + memset(&group, 0, sizeof(group)); + group.version = UFTP_VER_NUM; + group.group_id = 1; + group.blocksize = 1300; + group.phase = PHASE_MIDGROUP; + group.fileinfo.fd = -1; + memset(message, 0, sizeof(message)); + fileinfo->func = FILEINFO; + fileinfo->hlen = (sizeof(struct fileinfo_h) / 4) + namelen; + fileinfo->file_id = htons(1); + fileinfo->ftype = ftype; + fileinfo->namelen = namelen; + fileinfo->lofsize = htonl(ftype == FTYPE_REG ? 2600 : 0); + memcpy(message + sizeof(struct fileinfo_h), name, strlen(name)); + memcpy(message + (fileinfo->hlen * 4), &uid, sizeof(uid)); + gettimeofday(&now, NULL); + handle_fileinfo(&group, message, (fileinfo->hlen * 4) + sizeof(uid), now); + printf("%s: %s, keep %s, other %s\n", label, + group.fileinfo.comp_status == COMP_STAT_REJECTED ? "rejected" : "accepted", + exists(keep), exists(other)); + file_cleanup(&group, 1); +} + +int main(void) +{ + char base[] = "/tmp/uftp-root-XXXXXX", name[MAXPATHNAME]; + + if (!mkdtemp(base)) { + perror("mkdtemp"); + return 2; + } + applog = stderr; + listener = INVALID_SOCKET; + cache_len = DEF_CACHE; + uid = htonl(0x0A000001); + snprintf(destdir[0], sizeof(destdir[0]), "%s/dest", base); + destdircnt = 1; + mkdir(destdir[0], 0755); + snprintf(keep, sizeof(keep), "%s/keep", destdir[0]); + snprintf(other, sizeof(other), "%s/other", destdir[0]); + + request("delete .", FTYPE_DELETE, "."); + request("delete ./", FTYPE_DELETE, "./"); + request("delete ./.", FTYPE_DELETE, "./."); + snprintf(name, sizeof(name), "%s/", destdir[0]); + request("delete dest/", FTYPE_DELETE, name); + snprintf(name, sizeof(name), "%s/.", destdir[0]); + request("delete dest/.", FTYPE_DELETE, name); + request("file .", FTYPE_REG, "."); + snprintf(name, sizeof(name), "%s/./", destdir[0]); + request("file dest/./", FTYPE_REG, name); + request("delete other", FTYPE_DELETE, "other"); + request("free space .", FTYPE_FREESPACE, "."); + return 0; +} diff --git a/native/fixtures/uftp/done_section.c b/native/fixtures/uftp/done_section.c new file mode 100644 index 0000000..84e7292 --- /dev/null +++ b/native/fixtures/uftp/done_section.c @@ -0,0 +1,88 @@ +/* + * Gives handle_done() of the uftp receiver a DONE for a section of the file and DONE messages for + * a section past its end, then gets the NAKs of each section that the DONE scheduled, as the + * receive loop does when the NAK timer expires. "empty" receives a file without sections. + * Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_transfer.h" +#include "uftp_common.h" + +void handle_fileinfo_regular(struct group_list_t *group); + +static void send_done(struct group_list_t *group, const char *label, uint16_t section, int listed) +{ + unsigned char message[sizeof(struct done_h) + 4]; + struct done_h *done = (struct done_h *)message; + uint32_t client = listed ? uid : uid + 1, s; + unsigned char *naks; + unsigned int nak_count = 0; + + group->fileinfo.nak_time.tv_sec = 0; + group->fileinfo.nak_time.tv_usec = 0; + group->fileinfo.nak_section_first = 0; + group->fileinfo.nak_section_last = 0; + group->fileinfo.last_section = 0; + memset(message, 0, sizeof(message)); + done->func = DONE; + done->hlen = sizeof(struct done_h) / 4; + done->file_id = htons(group->file_id); + done->section = htons(section); + memcpy(message + sizeof(struct done_h), &client, sizeof(client)); + handle_done(group, message, sizeof(message)); + for (s = group->fileinfo.nak_section_first; s < group->fileinfo.nak_section_last; s++) { + naks = NULL; + nak_count += get_naks(group, s, &naks); + free(naks); + } + printf("%s: sections %u-%u, %u NAKs\n", label, group->fileinfo.nak_section_first, + group->fileinfo.nak_section_last, nak_count); +} + +int main(int argc, char **argv) +{ + struct group_list_t group; + char path[] = "/tmp/uftp-done-XXXXXX"; + int fd, empty = argc > 1 && !strcmp(argv[1], "empty"); + + memset(&group, 0, sizeof(group)); + applog = stderr; + listener = INVALID_SOCKET; + cache_len = 1048576; + uid = htonl(0x0A000001); + group.file_id = 1; + group.blocksize = 1300; + group.fileinfo.ftype = FTYPE_REG; + if (!empty) { + /* One section of two blocks. */ + group.fileinfo.size = 2 * 1300; + group.fileinfo.blocks = 2; + group.fileinfo.sections = 1; + group.fileinfo.big_sections = 1; + group.fileinfo.secsize_big = 2; + group.fileinfo.secsize_small = 2; + } + if ((fd = mkstemp(path)) == -1) { + perror("mkstemp"); + return 2; + } + close(fd); + strncpy(group.fileinfo.filepath, path, sizeof(group.fileinfo.filepath) - 1); + handle_fileinfo_regular(&group); + + if (empty) { + send_done(&group, "empty unlisted", 0, 0); + } else { + send_done(&group, "section 0 listed", 0, 1); + send_done(&group, "section 1 listed", 1, 1); + send_done(&group, "section 1 unlisted", 1, 0); + } + unlink(path); + return 0; +} diff --git a/native/fixtures/uftp/encrypted_message.c b/native/fixtures/uftp/encrypted_message.c new file mode 100644 index 0000000..3be5932 --- /dev/null +++ b/native/fixtures/uftp/encrypted_message.c @@ -0,0 +1,38 @@ +/* + * Gives the transfer and completion phases of the uftp sender an encrypted message with the ID of a + * listed client that has no encryption state, as a client that never registered. Built with the + * sender objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include + +#include "server.h" + +void handle_transfer_phase(unsigned char *packet, unsigned char *decrypted, int packetlen, + const union sockaddr_u *receiver, struct finfo_t *finfo, int *got_naks); +void handle_completion_phase(unsigned char *packet, unsigned char *decrypted, int packetlen, + const union sockaddr_u *receiver, struct finfo_t *finfo); + +int main(void) +{ + static unsigned char packet[256], decrypted[MAXMTU]; + struct uftp_h *header = (struct uftp_h *)packet; + union sockaddr_u receiver; + struct finfo_t finfo; + int got_naks = 0; + + memset(&receiver, 0, sizeof(receiver)); + memset(&finfo, 0, sizeof(finfo)); + keytype = KEY_AES256_GCM; + destcount = 1; + destlist[0].id = htonl(0xC0000234); + strcpy(destlist[0].name, "receiver"); + header->func = ENCRYPTED; + header->src_id = destlist[0].id; + handle_transfer_phase(packet, decrypted, sizeof(packet), &receiver, &finfo, &got_naks); + printf("transfer phase: survived\n"); + handle_completion_phase(packet, decrypted, sizeof(packet), &receiver, &finfo); + printf("completion phase: survived\n"); + return 0; +} diff --git a/native/fixtures/uftp/file_names.c b/native/fixtures/uftp/file_names.c new file mode 100644 index 0000000..168e942 --- /dev/null +++ b/native/fixtures/uftp/file_names.c @@ -0,0 +1,117 @@ +/* + * Gives handle_fileinfo() of the uftp receiver FILEINFO messages whose names try to leave the + * destination directory, then reports which of the files exist. "temp" receives into a temp + * directory (-T) and skips the absolute names, which a temp directory rejects. + * Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_config.h" +#include "client_fileinfo.h" +#include "uftp_common.h" + +static void send_fileinfo(struct group_list_t *group, int ftype, const char *name, const char *link) +{ + static uint16_t file_id; + unsigned char message[sizeof(struct fileinfo_h) + 2 * MAXPATHNAME + 8]; + struct fileinfo_h *fileinfo = (struct fileinfo_h *)message; + int namelen = (int)(strlen(name) + 4) / 4; + int linklen = link ? (int)(strlen(link) + 4) / 4 : 0; + struct timeval now; + + memset(message, 0, sizeof(message)); + fileinfo->func = FILEINFO; + fileinfo->hlen = (sizeof(struct fileinfo_h) / 4) + namelen + linklen; + fileinfo->file_id = htons(++file_id); + fileinfo->ftype = ftype; + fileinfo->namelen = namelen; + fileinfo->linklen = linklen; + memcpy(message + sizeof(struct fileinfo_h), name, strlen(name)); + if (link) { + memcpy(message + sizeof(struct fileinfo_h) + (namelen * 4), link, strlen(link)); + } + memcpy(message + (fileinfo->hlen * 4), &uid, sizeof(uid)); + group->phase = PHASE_MIDGROUP; + gettimeofday(&now, NULL); + handle_fileinfo(group, message, (fileinfo->hlen * 4) + sizeof(uid), now); + if (group->fileinfo.fd != -1) { + close(group->fileinfo.fd); + group->fileinfo.fd = -1; + } +} + +static void report(const char *label, const char *dir, const char *name) +{ + char path[MAXPATHNAME]; + struct stat statbuf; + + snprintf(path, sizeof(path), "%s/%s", dir, name); + printf("%s: %s\n", label, lstat(path, &statbuf) == 0 ? "present" : "absent"); +} + +int main(int argc, char **argv) +{ + struct group_list_t group; + char base[] = "/tmp/uftp-names-XXXXXX", dest[MAXPATHNAME], root[MAXPATHNAME]; + char path[MAXPATHNAME], name[MAXPATHNAME]; + int temp = argc > 1 && !strcmp(argv[1], "temp"); + + if (!mkdtemp(base)) { + perror("mkdtemp"); + return 2; + } + snprintf(dest, sizeof(dest), "%s/dest", base); + mkdir(dest, 0755); + snprintf(path, sizeof(path), "%s/outside", base); + mkdir(path, 0755); + snprintf(path, sizeof(path), "%s/outside/sub", base); + mkdir(path, 0755); + snprintf(path, sizeof(path), "%s/dest-other", base); + mkdir(path, 0755); + + memset(&group, 0, sizeof(group)); + applog = stderr; + listener = INVALID_SOCKET; + cache_len = DEF_CACHE; + uid = htonl(0x0A000001); + strncpy(destdir[0], dest, sizeof(destdir[0]) - 1); + destdircnt = 1; + group.version = UFTP_VER_NUM; + group.group_id = 1; + group.blocksize = 1300; + group.fileinfo.fd = -1; + if (temp) { + snprintf(tempdir, sizeof(tempdir), "%s/tmp", base); + mkdir(tempdir, 0755); + snprintf(root, sizeof(root), "%s/_group_%08X", tempdir, group.group_id); + } else { + snprintf(root, sizeof(root), "%s", dest); + } + + /* "up" leads from the root to /outside. */ + send_fileinfo(&group, FTYPE_DIR, "a..b", NULL); + send_fileinfo(&group, FTYPE_REG, "a..b/../../escape-dotdot", NULL); + send_fileinfo(&group, FTYPE_LINK, "up", temp ? "../../outside" : "../outside"); + send_fileinfo(&group, FTYPE_REG, "up/sub/escape-link", NULL); + send_fileinfo(&group, FTYPE_REG, "ok/inside", NULL); + report("a..b", root, "a..b"); + report("ok/inside", root, "ok/inside"); + report("escape-dotdot", temp ? tempdir : base, "escape-dotdot"); + report("escape-link", base, "outside/sub/escape-link"); + if (!temp) { + snprintf(name, sizeof(name), "%s-other/escape-prefix", dest); + send_fileinfo(&group, FTYPE_REG, name, NULL); + snprintf(name, sizeof(name), "%s/abs-inside", dest); + send_fileinfo(&group, FTYPE_REG, name, NULL); + report("abs-inside", dest, "abs-inside"); + report("escape-prefix", base, "dest-other/escape-prefix"); + } + return 0; +} diff --git a/native/fixtures/uftp/file_size_client.c b/native/fixtures/uftp/file_size_client.c new file mode 100644 index 0000000..41ce4c2 --- /dev/null +++ b/native/fixtures/uftp/file_size_client.c @@ -0,0 +1,72 @@ +/* + * Gives read_fileinfo() of the uftp receiver FILEINFO messages for a file of the largest size that + * 65535 sections of 512 byte blocks hold, and for a file one byte larger, then aborts the group. + * Descriptor 0 holds the file of another transfer. + * Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_common.h" +#include "uftp_common.h" + +int read_fileinfo(struct group_list_t *group, const unsigned char *message, int meslen, + struct timeval rxtime); + +static void fileinfo(f_offset_t size) +{ + unsigned char message[sizeof(struct fileinfo_h) + 8]; + struct fileinfo_h *info = (struct fileinfo_h *)message; + struct group_list_t group; + struct timeval now; + int accepted; + + memset(message, 0, sizeof(message)); + memset(&group, 0, sizeof(group)); + group.version = UFTP_VER_NUM; + group.blocksize = 512; + group.phase = PHASE_MIDGROUP; + info->func = FILEINFO; + info->hlen = (sizeof(struct fileinfo_h) / 4) + 1; + info->file_id = htons(1); + info->ftype = FTYPE_REG; + info->namelen = 1; + info->hifsize = htons((uint16_t)(size >> 32)); + info->lofsize = htonl((uint32_t)(size & 0xFFFFFFFF)); + memcpy(message + sizeof(struct fileinfo_h), "big", 3); + memcpy(message + (info->hlen * 4), &uid, sizeof(uid)); + gettimeofday(&now, NULL); + accepted = read_fileinfo(&group, message, sizeof(message), now); + printf("size %lld: %s", (long long)size, accepted ? "accepted" : "rejected"); + if (accepted) { + printf(", %u sections", group.fileinfo.sections); + } + printf("\n"); + file_cleanup(&group, 1); +} + +int main(void) +{ + f_offset_t limit = (f_offset_t)512 * 4096 * 65535; + char path[] = "/tmp/uftp-fd0-XXXXXX"; + + applog = stderr; + listener = INVALID_SOCKET; + uid = htonl(0x0A000001); + close(0); + if (mkstemp(path) != 0) { + fprintf(stderr, "descriptor 0 is not free\n"); + return 2; + } + fileinfo(limit); + fileinfo(limit + 1); + printf("fd 0: %s\n", fcntl(0, F_GETFD) == -1 ? "closed" : "open"); + unlink(path); + return 0; +} diff --git a/native/fixtures/uftp/file_size_server.c b/native/fixtures/uftp/file_size_server.c new file mode 100644 index 0000000..c03043d --- /dev/null +++ b/native/fixtures/uftp/file_size_server.c @@ -0,0 +1,40 @@ +/* + * Gives send_file() of the uftp sender a sparse file one byte larger than 65535 sections of 512 + * byte blocks hold. Built with the sender objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include + +#include "server.h" +#include "uftp_common.h" + +int send_file(const char *f_basedir, const char *filename, const char *n_destfname, + uint32_t group_id, uint8_t group_inst, int delete, int freespace); + +int main(void) +{ + char dir[] = "/tmp/uftp-size-XXXXXX", path[MAXPATHNAME]; + int fd; + + applog = stderr; + blocksize = 512; + if (!mkdtemp(dir)) { + perror("mkdtemp"); + return 2; + } + snprintf(path, sizeof(path), "%s/big", dir); + if ((fd = open(path, O_CREAT | O_WRONLY, 0644)) == -1 || + ftruncate(fd, (off_t)512 * 4096 * 65535 + 1) == -1) { + perror(path); + return 2; + } + close(fd); + printf("send_file returned %s\n", + send_file(dir, "big", "big", 1, 0, 0, 0) == ERR_PARAM ? "ERR_PARAM" : "another code"); + unlink(path); + rmdir(dir); + return 0; +} diff --git a/native/fixtures/uftp/fileseg_block.c b/native/fixtures/uftp/fileseg_block.c new file mode 100644 index 0000000..2cf2f2d --- /dev/null +++ b/native/fixtures/uftp/fileseg_block.c @@ -0,0 +1,50 @@ +/* + * Gives handle_fileseg() of the uftp receiver a block of the file, then blocks that the file does + * not have. Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include + +#include "client.h" +#include "client_transfer.h" + +static void send_block(struct group_list_t *group, uint16_t section, uint16_t block) +{ + unsigned char message[sizeof(struct fileseg_h) + 4]; + struct fileseg_h *fileseg = (struct fileseg_h *)message; + + memset(message, 0, sizeof(message)); + fileseg->func = FILESEG; + fileseg->hlen = sizeof(struct fileseg_h) / 4; + fileseg->file_id = htons(group->file_id); + fileseg->section = htons(section); + fileseg->sec_block = htons(block); + group->fileinfo.got_data = 0; + handle_fileseg(group, message, sizeof(message), 0); + printf("section %u block %u: %s\n", section, block, group->fileinfo.got_data ? "accepted" : "rejected"); +} + +int main(void) +{ + struct group_list_t group; + + memset(&group, 0, sizeof(group)); + group.file_id = 1; + group.blocksize = 4; + group.max_txseq = 1; + group.fileinfo.ftype = FTYPE_REG; + group.fileinfo.blocks = 2; + group.fileinfo.size = 2 * group.blocksize; + group.fileinfo.sections = 1; + group.fileinfo.secsize_small = 2; + group.fileinfo.secsize_big = 2; + /* Every block is marked as received, so an accepted block is a duplicate that writes nothing. */ + group.fileinfo.naklist = calloc(group.fileinfo.blocks, 1); + send_block(&group, 0, 1); + send_block(&group, 0, 2); + send_block(&group, 1, 0); + free(group.fileinfo.naklist); + return 0; +} diff --git a/native/fixtures/uftp/move_list.c b/native/fixtures/uftp/move_list.c new file mode 100644 index 0000000..18450b1 --- /dev/null +++ b/native/fixtures/uftp/move_list.c @@ -0,0 +1,56 @@ +/* + * Ends a group of the uftp receiver with more top level entries in its temp directory than the + * 10000 that move_files() had room for, then counts the entries in the destination directory. + * Built with the receiver objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "uftp_common.h" + +void move_files(struct group_list_t *group); + +int main(void) +{ + struct group_list_t group; + char base[] = "/tmp/uftp-move-XXXXXX", path[MAXPATHNAME]; + int i, fd, moved = 0, entries = 10050; + DIR *dir; + struct dirent *de; + + if (!mkdtemp(base)) { + perror("mkdtemp"); + return 2; + } + applog = stderr; + snprintf(tempdir, sizeof(tempdir), "%s/tmp", base); + snprintf(destdir[0], sizeof(destdir[0]), "%s/dest", base); + destdircnt = 1; + mkdir(tempdir, 0755); + mkdir(destdir[0], 0755); + memset(&group, 0, sizeof(group)); + group.group_id = 1; + snprintf(path, sizeof(path), "%s/_group_%08X", tempdir, group.group_id); + mkdir(path, 0755); + for (i = 0; i < entries; i++) { + snprintf(path, sizeof(path), "%s/_group_%08X/f%05d", tempdir, group.group_id, i); + if ((fd = open(path, O_CREAT | O_WRONLY, 0644)) != -1) { + close(fd); + } + } + move_files(&group); + if ((dir = opendir(destdir[0])) != NULL) { + while ((de = readdir(dir)) != NULL) { + moved += de->d_name[0] != '.'; + } + closedir(dir); + } + printf("moved %d of %d entries\n", moved, entries); + return 0; +} diff --git a/native/fixtures/uftp/nak_list.c b/native/fixtures/uftp/nak_list.c new file mode 100644 index 0000000..cab8960 --- /dev/null +++ b/native/fixtures/uftp/nak_list.c @@ -0,0 +1,118 @@ +/* + * Builds the NAK list of a full section and of a short section with get_naks() of the uftp + * receiver, sends each with send_status() as the receive loop does when the NAK timer expires, + * and reads the STATUS back from a loopback socket. Built with the receiver objects and + * AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "client.h" +#include "client_transfer.h" +#include "uftp_common.h" + +void handle_fileinfo_regular(struct group_list_t *group); + +static int bound_socket(union sockaddr_u *addr) +{ + socklen_t len = sizeof(addr->sin); + struct timeval timeout = { 2, 0 }; + int s; + + memset(addr, 0, sizeof(*addr)); + addr->sin.sin_family = AF_INET; + addr->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) || + (bind(s, (struct sockaddr *)&addr->sin, len) == -1) || + (getsockname(s, (struct sockaddr *)&addr->sin, &len) == -1) || + (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1)) { + perror("socket"); + exit(2); + } + return s; +} + +/* Receives every block but the last one of a file of blocks 512-byte blocks in one section. */ +static void send_naks(const char *label, uint32_t blocks, int receiver, const union sockaddr_u *to) +{ + struct group_list_t group; + unsigned char packet[MAXMTU], *naks; + const struct status_h *status = (const struct status_h *)(packet + sizeof(struct uftp_h)); + char path[] = "/tmp/uftp-naks-XXXXXX"; + unsigned int nak_count, i; + int fd, len, naklen; + + memset(&group, 0, sizeof(group)); + group.group_id = 1; + group.file_id = 1; + group.blocksize = 512; + group.fileinfo.ftype = FTYPE_REG; + group.fileinfo.size = (f_offset_t)blocks * group.blocksize; + group.fileinfo.blocks = blocks; + /* One section, as read_fileinfo() splits a file of up to blocksize * 8 blocks. */ + group.fileinfo.sections = 1; + group.fileinfo.secsize_small = blocks; + group.fileinfo.secsize_big = blocks + 1; + group.fileinfo.big_sections = 0; + if ((fd = mkstemp(path)) == -1) { + perror("mkstemp"); + exit(2); + } + close(fd); + strncpy(group.fileinfo.filepath, path, sizeof(group.fileinfo.filepath) - 1); + group.replyaddr = *to; + handle_fileinfo_regular(&group); + memset(group.fileinfo.naklist, 0, blocks); + group.fileinfo.naklist[blocks - 1] = 1; + + naks = NULL; + nak_count = get_naks(&group, 0, &naks); + send_status(&group, 0, naks, nak_count); + free(naks); + + do { + len = recv(receiver, packet, sizeof(packet), 0); + } while ((len > (int)(sizeof(struct uftp_h) + sizeof(struct status_h))) && + (packet[1] != STATUS)); + if ((len <= (int)(sizeof(struct uftp_h) + sizeof(struct status_h))) || + (packet[1] != STATUS)) { + printf("%s: no STATUS\n", label); + } else { + naks = packet + sizeof(struct uftp_h) + (status->hlen * 4); + naklen = len - (int)(naks - packet); + printf("%s: %d bytes, NAKs", label, naklen); + for (i = 0; i < (unsigned int)naklen * 8; i++) { + if (naks[i >> 3] & (1 << (i & 7))) { + printf(" %u", i); + } + } + printf("\n"); + } + close(group.fileinfo.fd); + free(group.fileinfo.naklist); + free(group.fileinfo.section_done); + free(group.fileinfo.cache); + free(group.fileinfo.cache_status); + unlink(path); +} + +int main(void) +{ + union sockaddr_u listener_addr, receiver_addr; + int receiver; + + applog = stderr; + cache_len = 1048576; + uid = htonl(0x0A000001); + listener = bound_socket(&listener_addr); + receiver = bound_socket(&receiver_addr); + send_naks("full section", 512 * 8, receiver, &receiver_addr); + send_naks("short section", 10, receiver, &receiver_addr); + return 0; +} diff --git a/native/fixtures/uftp/proxy_announce.c b/native/fixtures/uftp/proxy_announce.c new file mode 100644 index 0000000..789f030 --- /dev/null +++ b/native/fixtures/uftp/proxy_announce.c @@ -0,0 +1,50 @@ +/* + * Gives read_announce() of the uftp proxy an unencrypted ANNOUNCE with each block size in turn. + * Built with the proxy objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include + +#include "proxy.h" +#include "uftp_common.h" + +int read_announce(struct pr_group_list_t *group, unsigned char *packet, const union sockaddr_u *src, + int packetlen); + +static void announce(unsigned int blocksize) +{ + unsigned char packet[sizeof(struct uftp_h) + sizeof(struct announce_h) + 8]; + struct uftp_h *header = (struct uftp_h *)packet; + struct announce_h *ann = (struct announce_h *)(packet + sizeof(struct uftp_h)); + struct pr_group_list_t group; + union sockaddr_u src; + + memset(packet, 0, sizeof(packet)); + memset(&group, 0, sizeof(group)); + memset(&src, 0, sizeof(src)); + src.sin.sin_family = AF_INET; + header->version = UFTP_VER_NUM; + header->func = ANNOUNCE; + header->group_id = htonl(1); + ann->func = ANNOUNCE; + ann->hlen = (sizeof(struct announce_h) + 8) / 4; + ann->robust = 2; + ann->blocksize = htons(blocksize); + printf("block size %u: %s\n", blocksize, + read_announce(&group, packet, &src, sizeof(packet)) ? "accepted" : "rejected"); +} + +int main(void) +{ + static const unsigned int sizes[] = { 0, 511, 512, 1300, MAXMTU - 200, MAXMTU - 199, 65535 }; + unsigned int i; + + applog = stderr; + listener = INVALID_SOCKET; + for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) { + announce(sizes[i]); + } + return 0; +} diff --git a/native/fixtures/uftp/proxy_lists.c b/native/fixtures/uftp/proxy_lists.c new file mode 100644 index 0000000..d7dddfc --- /dev/null +++ b/native/fixtures/uftp/proxy_lists.c @@ -0,0 +1,34 @@ +/* + * Prints whether each client list that the uftp proxy builds fits in its packet buffer, for the + * largest block size and an RSA-2048 key. Built with the proxy objects by native/uftp-fixes.t. + */ +#include +#include + +#include "proxy.h" +#include "proxy_common.h" + +static void check(const char *name, struct pr_group_list_t *group, int func, int hlen, int size, int encrypted) +{ + int count = max_msg_dest(group, func, hlen); + int len = (int)sizeof(struct uftp_h) + hlen + count * size; + + if (encrypted) { + len += (int)sizeof(struct encrypted_h) + KEYBLSIZE; + } + printf("%s: %d entries, %s\n", name, count, (count > 0 && len <= MAXMTU) ? "fits" : "overflows"); +} + +int main(void) +{ + struct pr_group_list_t group; + + memset(&group, 0, sizeof(group)); + group.blocksize = 8800; + group.keytype = KEY_AES256_GCM; + check("KEYINFO", &group, KEYINFO, sizeof(struct keyinfo_h) + 256, sizeof(struct destkey), 0); + check("REGISTER", &group, REGISTER, sizeof(struct register_h) + 256, sizeof(uint32_t), 1); + check("FILEINFO_ACK", &group, FILEINFO_ACK, sizeof(struct fileinfoack_h), sizeof(uint32_t), 1); + check("COMPLETE", &group, COMPLETE, sizeof(struct complete_h), sizeof(uint32_t), 1); + return 0; +} diff --git a/native/fixtures/uftp/proxy_status.c b/native/fixtures/uftp/proxy_status.c new file mode 100644 index 0000000..8ac228e --- /dev/null +++ b/native/fixtures/uftp/proxy_status.c @@ -0,0 +1,119 @@ +/* + * Gives handle_status() of the uftp proxy the STATUS that a client sends for a section of 512-byte + * blocks, sends the pending NAKs upstream with send_status(), and reads that STATUS back from a + * loopback socket. The client STATUS covers a full section, a full section with the TFMCC + * extension, and a short section followed by old data in the packet buffer. Built with the proxy + * objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "proxy.h" +#include "proxy_downstream.h" +#include "proxy_upstream.h" +#include "uftp_common.h" + +#define BLOCKSIZE 512 + +static struct pr_group_list_t group; +static int server; + +static int bound_socket(union sockaddr_u *addr) +{ + socklen_t len = sizeof(addr->sin); + struct timeval timeout = { 2, 0 }; + int s; + + memset(addr, 0, sizeof(*addr)); + addr->sin.sin_family = AF_INET; + addr->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) || + (bind(s, (struct sockaddr *)&addr->sin, len) == -1) || + (getsockname(s, (struct sockaddr *)&addr->sin, &len) == -1) || + (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1)) { + perror("socket"); + exit(2); + } + return s; +} + +/* + * Sends the proxy a client STATUS for a section of blocks blocks with a NAK for its last block, as + * a client sends it: blocks / 8 + 1 bytes of NAKs after the header and its extensions. The packet + * buffer holds 0xFF after the message. + */ +static void forward(const char *label, unsigned int blocks, int tfmcc) +{ + unsigned char message[MAXMTU], packet[MAXMTU], *naks; + struct status_h *status = (struct status_h *)message; + struct tfmcc_ack_info_he *ext = (struct tfmcc_ack_info_he *)(message + sizeof(struct status_h)); + unsigned int i, hlen; + int len, naklen; + + memset(message, 0xFF, sizeof(message)); + hlen = sizeof(struct status_h) + (tfmcc ? sizeof(struct tfmcc_ack_info_he) : 0); + memset(message, 0, hlen + (blocks / 8) + 1); + status->func = STATUS; + status->hlen = hlen / 4; + status->file_id = htons(1); + status->section = htons(0); + if (tfmcc) { + ext->exttype = EXT_TFMCC_ACK_INFO; + ext->extlen = sizeof(struct tfmcc_ack_info_he) / 4; + ext->cc_seq = htons(0xFFFF); + ext->cc_rate = htons(0xFFFF); + ext->client_id = 0xFFFFFFFF; + ext->tstamp_hi = 0xFFFFFFFF; + ext->tstamp_lo = 0xFFFFFFFF; + } + message[hlen + (blocks - 1) / 8] |= 1 << ((blocks - 1) % 8); + handle_status(&group, 0, message, hlen + (blocks / 8) + 1); + send_status(&group, 0); + + do { + len = recv(server, packet, sizeof(packet), 0); + } while ((len > (int)(sizeof(struct uftp_h) + sizeof(struct status_h))) && + (packet[1] != STATUS)); + if ((len <= (int)(sizeof(struct uftp_h) + sizeof(struct status_h))) || + (packet[1] != STATUS)) { + printf("%s: no STATUS\n", label); + return; + } + status = (struct status_h *)(packet + sizeof(struct uftp_h)); + naks = packet + sizeof(struct uftp_h) + (status->hlen * 4); + naklen = len - (int)(naks - packet); + printf("%s: %d bytes, NAKs", label, naklen); + for (i = 0; i < (unsigned int)naklen * 8; i++) { + if (naks[i >> 3] & (1 << (i & 7))) { + printf(" %u", i); + } + } + printf("\n"); +} + +int main(void) +{ + union sockaddr_u listener_addr, server_addr; + + applog = stderr; + uid = htonl(0x0A000002); + listener = bound_socket(&listener_addr); + server = bound_socket(&server_addr); + group.version = UFTP_VER_NUM; + group.group_id = 1; + group.blocksize = BLOCKSIZE; + group.up_addr = server_addr; + group.destcount = 1; + group.destinfo[0].pending = -1; + + forward("full section", BLOCKSIZE * 8, 0); + forward("tfmcc full section", BLOCKSIZE * 8, 1); + forward("short section", 10, 0); + return 0; +} diff --git a/native/fixtures/uftp/seek_block.c b/native/fixtures/uftp/seek_block.c new file mode 100644 index 0000000..a591092 --- /dev/null +++ b/native/fixtures/uftp/seek_block.c @@ -0,0 +1,47 @@ +/* + * Seeks with seek_block() of the uftp sender to the last block below 2^31, to block 2^31, and to + * the last block that the section count allows, in a file of 512-byte blocks. Built with the + * sender objects and AddressSanitizer by native/uftp-fixes.t. + */ +#include +#include +#include +#include + +#include "server.h" +#include "uftp_common.h" + +int seek_block(const struct finfo_t *finfo, int file, uint32_t block, f_offset_t *offset); + +static void seek(const struct finfo_t *finfo, int fd, uint32_t block) +{ + f_offset_t offset = 0; + + lseek(fd, 0, SEEK_SET); + if (seek_block(finfo, fd, block, &offset)) { + printf("block %u: offset %lld\n", block, (long long)offset); + } else { + printf("block %u: failed\n", block); + } +} + +int main(void) +{ + struct finfo_t finfo; + char path[] = "/tmp/uftp-seek-XXXXXX"; + int fd; + + applog = stderr; + blocksize = 512; + memset(&finfo, 0, sizeof(finfo)); + if ((fd = mkstemp(path)) == -1) { + perror("mkstemp"); + return 2; + } + seek(&finfo, fd, 0x7FFFFFFF); + seek(&finfo, fd, 0x80000000); + seek(&finfo, fd, MAXSECTION * 0xFFFFU - 1); + close(fd); + unlink(path); + return 0; +} diff --git a/native/fixtures/uftp/status_section.c b/native/fixtures/uftp/status_section.c new file mode 100644 index 0000000..7eb9c35 --- /dev/null +++ b/native/fixtures/uftp/status_section.c @@ -0,0 +1,53 @@ +/* + * Drives handle_status() of the uftp sender with a STATUS that a receiver in the session can send. + * Built with the sender objects and AddressSanitizer by t/uftp_status_section.t. + */ +#include +#include +#include +#include + +#include "server.h" +#include "server_config.h" +#include "server_transfer.h" + +extern uint32_t current_position; + +static int send_status(struct finfo_t *finfo, uint16_t section) +{ + unsigned char message[sizeof(struct status_h) + 1]; + struct status_h *status = (struct status_h *)message; + int got_naks = 0; + + memset(message, 0, sizeof(message)); + status->func = STATUS; + status->hlen = sizeof(struct status_h) / 4; + status->file_id = htons(finfo->file_id); + status->section = htons(section); + message[sizeof(struct status_h)] = 1; + handle_status(message, sizeof(message), finfo, 0, &got_naks); + return got_naks; +} + +int main(void) +{ + struct finfo_t finfo; + + memset(&finfo, 0, sizeof(finfo)); + finfo.file_id = 1; + finfo.blocks = 2; + finfo.sections = 1; + finfo.secsize_small = 2; + finfo.secsize_big = 2; + finfo.naklist = calloc(finfo.blocks, 1); + strcpy(destlist[0].name, "receiver"); + destcount = 1; + max_nak_pct = 100; + max_nak_cnt = 1; + current_position = finfo.blocks; + + printf("section 0: %d\n", send_status(&finfo, 0)); + printf("section 1: %d\n", send_status(&finfo, 1)); + free(finfo.naklist); + return 0; +} diff --git a/native/uftp-fixes.t b/native/uftp-fixes.t new file mode 100644 index 0000000..aeeb864 --- /dev/null +++ b/native/uftp-fixes.t @@ -0,0 +1,239 @@ +#!/usr/bin/env perl +# The uftp sender must reject a STATUS for a section that the file does not have, and an encrypted +# message from a client without encryption state. The receiver must reject a FILESEG for a block +# that the file does not have or a last block of the wrong length, a DONE for a section past the +# end of the file, an ANNOUNCE with a block size that the sender does not accept, a file name that +# leaves its directory, and a name of the destination directory itself. It must keep each block +# that it receives inside its disk cache, send the NAK list of a full section without reading past +# it, move any number of files at the end of a group, and leave a file alone at cleanup when +# another group replaced it or turned a directory of its path into a symbolic link. A restarted +# group must not delete its unused file, and a backup must not delete or replace a file, through a +# symbolic link. Both must refuse a file with more sections than the protocol holds, and the +# sender must seek to every block that the section count allows. The proxy must reject an ANNOUNCE +# with a block size that the sender does not accept, keep each client list that it builds inside +# its packet buffer, and forward the NAKs of a client STATUS, after its header extensions, with +# the whole NAK list of a full section. The test builds the programs from the committed archive +# and patches with AddressSanitizer, and calls the functions that handle those cases directly. The +# built programs must carry the xCAT build mark. +use strict; +use warnings; +use FindBin qw($RealBin); +use File::Basename qw(dirname); +use File::Temp qw(tempdir); +use Test::More; + +use lib "$RealBin/../lib"; +use XCAT::BuildUtils qw(command_exists read_binary read_lines); + +my $root = "$RealBin/.."; +my @spec = read_lines("$root/uftp/uftp.spec"); +my ($version) = map { /^Version:\s*(\S+)/ ? $1 : () } @spec; +my @patches = map { /^Patch\d*:\s*(\S+)/ ? $1 : () } @spec; +my @missing = grep { !command_exists($_) } qw(cc make patch); +push @missing, 'the OpenSSL headers' unless -e '/usr/include/openssl/evp.h'; +plan skip_all => "needs @missing" if @missing; + +sub run_or_die { + my ($cmd, $log) = @_; + return if system("$cmd >>'$log' 2>&1") == 0; + die "$cmd failed:\n" . read_binary($log); +} + +my $flags = '-g -O1 -fsanitize=address -fno-omit-frame-pointer'; +my %tree; + +# Builds $program once from the archive and the patches of the spec, then links the harness $name +# in the place of its main. +sub harness { + my ($program, $name) = @_; + my ($src, $log) = @{ $tree{$program} //= build($program) }; + my %objects = ( + uftp => [qw(server_announce server_transfer server_send server_phase server_common server_config server_init)], + uftpd => [qw(client_loop client_announce client_fileinfo client_transfer client_common client_config client_init heartbeat_send)], + uftpproxyd => [qw(proxy_loop proxy_upstream proxy_downstream proxy_common proxy_config proxy_init heartbeat_send)], + ); + run_or_die("cc $flags -I'$src' -c '$root/native/fixtures/uftp/$name.c' -o '$src/$name.o'", $log); + my @objects = map { "'$src/$_.o'" } qw(uftp_common encrypt_openssl), @{ $objects{$program} }, 'main_lib', $name; + run_or_die("cc -fsanitize=address -o '$src/$name' @objects -lcrypto -lm -lpthread", $log); + return "$src/$name"; +} + +sub build { + my ($program) = @_; + my $dir = tempdir(CLEANUP => 1); + my $src = "$dir/uftp-$version"; + my $log = "$dir/build.log"; + run_or_die("tar -xzf '$root/uftp/uftp-$version.tar.gz' -C '$dir'", $log); + run_or_die("patch -s -p1 -d '$src' < '$root/uftp/$_'", $log) for @patches; + run_or_die("make -C '$src' CFLAGS='$flags' LDFLAGS='-fsanitize=address' $program", $log); + my %main = (uftp => 'server_main', uftpd => 'client_main', uftpproxyd => 'proxy_main'); + run_or_die("cc $flags -I'$src' -Dmain=uftp_program_main -c '$src/$main{$program}.c' -o '$src/main_lib.o'", $log); + return [$src, $log]; +} + +ok(scalar(@patches), 'uftp.spec applies patches'); +is_deeply([ grep { /\S/ } map { s/\s+\z//r } read_lines("$root/uftp/debian/patches/series") ], \@patches, + 'the deb applies the patches of the rpm in the same order'); + +my $bin = harness('uftp', 'status_section'); +my %built = (uftp => dirname($bin) . '/uftp'); +my $out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the sender survives a STATUS for a section past the end of the file') or diag($out); +like($out, qr/^section 0: 1$/m, 'a STATUS for a section of the file records its NAK'); +like($out, qr/^section 1: 0$/m, 'a STATUS for a section past the end of the file records nothing'); +unlike($out, qr/AddressSanitizer/, 'the sender stays inside its NAK list'); + +$bin = harness('uftp', 'encrypted_message'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the sender survives an encrypted message from a client without encryption state') or diag($out); +like($out, qr/^$_ phase: survived$/m, "the $_ phase rejects that message") for qw(transfer completion); + +$bin = harness('uftpd', 'fileseg_block'); +$built{uftpd} = dirname($bin) . '/uftpd'; +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver survives a FILESEG for a block that the file does not have') or diag($out); +like($out, qr/^section 0 block 1: accepted$/m, 'a FILESEG for a block of the file is accepted'); +like($out, qr/^section 0 block 2: rejected$/m, 'a FILESEG for a block past its section is rejected'); +like($out, qr/^section 1 block 0: rejected$/m, 'a FILESEG for a section past the end of the file is rejected'); + +$bin = harness('uftpd', 'cache_boundary'); +my %cache_case = ( + 'short-tail' => 'a short last block at the end of the default disk cache', + 'long-repeat' => 'a full-size copy of a short last block that is in the disk cache', + 'short-last' => 'a last block shorter than the end of the file', + 'lost-blocks' => 'blocks after lost blocks in a small disk cache with large blocks', +); +for my $case (sort keys %cache_case) { + $out = `ASAN_OPTIONS=detect_leaks=0 '$bin' $case 2>&1`; + is($?, 0, "the receiver survives $cache_case{$case}") or diag($out); + like($out, qr/^\Q$case\E: \d+ blocks, 0 missing, file matches$/m, "the file is complete after $cache_case{$case}"); +} + +$bin = harness('uftpd', 'done_section'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver survives a DONE for a section past the end of the file') or diag($out); +like($out, qr/^section 0 listed: sections 0-1, 2 NAKs$/m, 'a DONE for a section of the file schedules its NAKs'); +like($out, qr/^section 1 $_: sections 0-0, 0 NAKs$/m, "a DONE $_ for this client for a section past the end of the file schedules nothing") + for qw(listed unlisted); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' empty 2>&1`; +is($?, 0, 'the receiver survives a DONE for a file without sections') or diag($out); +like($out, qr/^empty unlisted: sections 0-1, 0 NAKs$/m, 'a file without sections has no NAKs'); + +$bin = harness('uftpd', 'nak_list'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver survives a STATUS for a full section') or diag($out); +like($out, qr/^full section: 513 bytes, NAKs 4095$/m, 'the STATUS for a section of 4096 blocks of 512 bytes sends 513 bytes of NAKs'); +like($out, qr/^short section: 2 bytes, NAKs 9$/m, 'the STATUS for a section of 10 blocks sends 2 bytes of NAKs'); +unlike($out, qr/AddressSanitizer/, 'the receiver stays inside its NAK list'); + +$bin = harness('uftpd', 'announce_blocksize'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver reads an ANNOUNCE with each block size') or diag($out); +like($out, qr/^block size $_: accepted$/m, "an ANNOUNCE with a block size of $_ is accepted") for 512, 1300, 8800; +like($out, qr/^block size $_: rejected$/m, "an ANNOUNCE with a block size of $_ is rejected") for 0, 511, 8801, 65535; +like($out, qr/^fd 0: open$/m, 'a rejected ANNOUNCE leaves the file of another transfer open'); + +$bin = harness('uftpd', 'file_names'); +for my $mode ('', 'temp') { + my $where = $mode ? 'a temp directory' : 'the destination directory'; + $out = `ASAN_OPTIONS=detect_leaks=0 '$bin' $mode 2>&1`; + is($?, 0, "the receiver handles the file names into $where") or diag($out); + like($out, qr/^$_: present$/m, "the receiver creates $_ in $where") for 'a..b', 'ok/inside'; + like($out, qr/^escape-dotdot: absent$/m, "a name with .. after a component that contains .. stays out of $where"); + like($out, qr/^escape-link: absent$/m, "a name through a symbolic link below $where is rejected"); +} +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +like($out, qr/^abs-inside: present$/m, 'an absolute name below the destination directory is accepted'); +like($out, qr/^escape-prefix: absent$/m, 'an absolute name that only starts with the destination directory is rejected'); + +my $limit = 512 * 4096 * 65535; +$bin = harness('uftpd', 'file_size_client'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver survives a FILEINFO with more sections than the protocol holds') or diag($out); +like($out, qr/^size $limit: accepted, 65535 sections$/m, 'a file of 65535 sections is accepted'); +like($out, qr/^size @{[ $limit + 1 ]}: rejected$/m, 'a file of one more section is rejected'); +like($out, qr/^fd 0: open$/m, 'an abort after a rejected FILEINFO leaves the file of another transfer open'); + +$bin = harness('uftp', 'file_size_server'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the sender survives a file with more sections than the protocol holds') or diag($out); +like($out, qr/^send_file returned ERR_PARAM$/m, 'the sender fails on that file'); + +$bin = harness('uftp', 'seek_block'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the sender seeks to blocks past 2^31') or diag($out); +like($out, qr/^block 2147483647: offset 1099511627264$/m, 'the sender seeks to the last block below 2^31'); +like($out, qr/^block 2147483648: offset 1099511627776$/m, 'the sender seeks to block 2^31'); +like($out, qr/^block 4294901759: offset 2198989700608$/m, 'the sender seeks to the last block that the section count allows'); + +$bin = harness('uftpd', 'move_list'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver survives the end of a group with more than 10000 top level entries') or diag($out); +like($out, qr/^moved 10050 of 10050 entries$/m, 'the receiver moves every entry of that group'); + +$bin = harness('uftpd', 'cleanup_link'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver survives an abort after another group replaced a directory') or diag($out); +like($out, qr/^outside victim: present$/m, 'an aborted file is not deleted through a symbolic link that another group made'); +like($out, qr/^aborted partial: absent$/m, 'an aborted file is still deleted when its path is unchanged'); + +$bin = harness('uftpd', 'cleanup_replaced'); +for my $mode ('', 'complete') { + my $end = $mode ? 'completes' : 'aborts'; + $out = `ASAN_OPTIONS=detect_leaks=0 '$bin' $mode 2>&1`; + is($?, 0, "the receiver survives a group that $end after another group replaced its file") or diag($out); + like($out, qr/^replaced file: second, time kept$/m, "a group that $end leaves alone the file that another group completed in its place"); +} +like($out, qr/^completed file: third, time set$/m, 'a completed file still gets its time when no other group replaced it'); +like($out, qr/^aborted file: absent$/m, 'an aborted file is still deleted when no other group replaced it'); + +$bin = harness('uftpd', 'cleanup_restart'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver survives an abort of a restarted group that replaced a directory') or diag($out); +like($out, qr/^outside victim: present$/m, 'a restarted group does not delete its unused file through a symbolic link that it made'); +like($out, qr/^interrupted partial: present$/m, 'an interrupted file stays in the temp directory for a restart'); +like($out, qr/^unused partial: absent$/m, 'a restarted group still deletes its unused file when its path is unchanged'); + +$bin = harness('uftpd', 'backup_link'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver survives a backup below a symbolic link in its backup directory') or diag($out); +like($out, qr/^outside victim: outside$/m, 'a backup does not delete or replace a file through a symbolic link in the backup directory'); +like($out, qr/^received file: second$/m, 'the file whose backup went through that link is still received'); +like($out, qr/^backup of plain: first$/m, 'an overwritten file is still backed up when no link is in the way'); +like($out, qr/^plain: second$/m, 'the new file replaces the file that was backed up'); +like($out, qr/^long backup path: 3\d\d bytes$/m, 'a backup path is longer than 300 bytes'); +like($out, qr/^backup of long: second$/m, 'a file is backed up into existing directories along a backup path longer than 300 bytes'); +like($out, qr/^long: third$/m, 'the new file with that long a backup path replaces the backed up one'); + +$bin = harness('uftpd', 'dest_root'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the receiver survives names of its destination directory') or diag($out); +like($out, qr/^\Q$_\E: rejected, keep present, other present$/m, "a request for the name $_ is rejected and the destination keeps its files") + for 'delete .', 'delete ./', 'delete ./.', 'delete dest/', 'delete dest/.', 'file .', 'file dest/./'; +like($out, qr/^delete other: accepted, keep present, other absent$/m, 'a delete request for a file of the destination deletes only that file'); +like($out, qr/^free space \.: accepted, keep present, other present$/m, 'a free space request for . is still accepted'); + +for my $program (sort keys %built) { + like(read_binary($built{$program}), qr/UFTP version \Q$version\E \(xCAT build \d+\)/, + "the built $program carries the xCAT build mark"); +} + +$bin = harness('uftpproxyd', 'proxy_announce'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the proxy reads an ANNOUNCE with each block size') or diag($out); +like($out, qr/^block size $_: accepted$/m, "the proxy accepts an ANNOUNCE with a block size of $_") for 512, 1300, 8800; +like($out, qr/^block size $_: rejected$/m, "the proxy rejects an ANNOUNCE with a block size of $_") for 0, 511, 8801, 65535; + +$bin = harness('uftpproxyd', 'proxy_lists'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the proxy computes its list sizes') or diag($out); +like($out, qr/^$_: \d+ entries, fits$/m, "a $_ list of the proxy fits in its packet buffer") for qw(KEYINFO REGISTER FILEINFO_ACK COMPLETE); + +$bin = harness('uftpproxyd', 'proxy_status'); +$out = `ASAN_OPTIONS=detect_leaks=0 '$bin' 2>&1`; +is($?, 0, 'the proxy forwards the STATUS of a client') or diag($out); +like($out, qr/^full section: 513 bytes, NAKs 4095$/m, 'the proxy sends the 513 bytes of NAKs that the server requires for a section of 4096 blocks of 512 bytes'); +like($out, qr/^tfmcc full section: 513 bytes, NAKs 4095$/m, 'the proxy reads the NAKs of a client STATUS after its TFMCC extension'); +like($out, qr/^short section: 513 bytes, NAKs 9$/m, 'the proxy reads only the NAKs that a client STATUS holds'); + +done_testing(); diff --git a/t/mockbuild-all.t b/t/mockbuild-all.t index c5ea55c..4609ac4 100644 --- a/t/mockbuild-all.t +++ b/t/mockbuild-all.t @@ -536,6 +536,10 @@ is(rpm_release(tempdir(CLEANUP => 1), 'nonexistent-pkg'), undef, 'rpm_release is or diag("missing ipxe-xcat in: @no_ipxe_xcat"); is_deeply(\@missing, [], 'conserver-xcat is present in every legacy manifest target section') or diag("missing conserver-xcat in: @missing"); + # uftp is optional for xCAT like conserver-xcat, and BUILD.md documents it for every EL target. + my @no_uftp = grep { !exists $m{$_}{uftp} } @targets, 'rocky-10-riscv64-xcat'; + is_deeply(\@no_uftp, [], 'uftp is present in every EL manifest target section') + or diag("missing uftp in: @no_uftp"); my @native_missing = grep { /^openeuler-/ && !exists $m{$_}{goconserver} } sort keys %m; is_deeply(\@native_missing, [], 'native manifest targets retain goconserver');