diff options
Diffstat (limited to 'archivers/libarchive/files/cpio/test')
10 files changed, 183 insertions, 74 deletions
diff --git a/archivers/libarchive/files/cpio/test/main.c b/archivers/libarchive/files/cpio/test/main.c index 373238fa2fb..1f8bac53a0d 100644 --- a/archivers/libarchive/files/cpio/test/main.c +++ b/archivers/libarchive/files/cpio/test/main.c @@ -24,25 +24,26 @@ */ /* - * This same file is used pretty much verbatim for all test harnesses. - * - * The next line is used to define various environment variables, etc. - * - * The tar and cpio test harnesses are identical except for this line; - * the libarchive test harness omits some code that is needed only for - * testing standalone executables. - */ -#define PROGRAM "BSDCPIO" - -/* * Various utility routines useful for test programs. * Each test program is linked against this file. */ #include <errno.h> +#include <locale.h> #include <stdarg.h> #include <time.h> #include "test.h" + +/* + * This same file is used pretty much verbatim for all test harnesses. + * + * The next few lines are the only differences. + */ +#define PROGRAM "bsdcpio" /* Program being tested. */ +#define ENVBASE "BSDCPIO" /* Prefix for environment variables. */ +#undef EXTRA_DUMP /* How to dump extra data */ +/* How to generate extra version info. */ +#define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "") __FBSDID("$FreeBSD$"); /* @@ -74,6 +75,9 @@ static int skips = 0; /* Cumulative count of assertions. */ static int assertions = 0; +/* Directory where uuencoded reference files can be found. */ +static char *refdir; + /* * My own implementation of the standard assert() macro emits the * message in the same format as GCC (file:line: message). @@ -167,7 +171,12 @@ report_failure(void *extra) msg[0] = '\0'; } +#ifdef EXTRA_DUMP + if (extra != NULL) + fprintf(stderr, " detail: %s\n", EXTRA_DUMP(extra)); +#else (void)extra; /* UNUSED */ +#endif if (dump_on_failure) { fprintf(stderr, @@ -231,15 +240,15 @@ test_assert(const char *file, int line, int value, const char *condition, void * ++assertions; if (value) { msg[0] = '\0'; - return (1); + return (value); } failures ++; if (previous_failures(file, line)) - return (0); + return (value); fprintf(stderr, "%s:%d: Assertion failed\n", file, line); fprintf(stderr, " Condition: %s\n", condition); report_failure(extra); - return (0); + return (value); } /* assertEqualInt() displays the values of the two integers. */ @@ -502,7 +511,6 @@ test_assert_file_contents(const void *buff, int s, const char *fpattern, ...) return (0); } - /* * Call standard system() call, but build up the command line using * sprintf() conventions. @@ -620,6 +628,8 @@ static int test_run(int i, const char *tmpdir) tests[i].name); exit(1); } + /* Explicitly reset the locale before each test. */ + setlocale(LC_ALL, "C"); /* Run the actual test. */ (*tests[i].func)(); /* Summarize the results of this test. */ @@ -639,8 +649,10 @@ static void usage(const char *program) printf("Options:\n"); printf(" -k Keep running after failures.\n"); printf(" Default: Core dump after any failure.\n"); +#ifdef PROGRAM printf(" -p <path> Path to executable to be tested.\n"); - printf(" Default: path taken from " PROGRAM " environment variable.\n"); + printf(" Default: path taken from " ENVBASE " environment variable.\n"); +#endif printf(" -q Quiet.\n"); printf(" -r <dir> Path to dir containing reference files.\n"); printf(" Default: Current directory.\n"); @@ -650,6 +662,66 @@ static void usage(const char *program) exit(1); } +#define uudecode(c) (((c) - 0x20) & 0x3f) + +void +extract_reference_file(const char *name) +{ + char buff[1024]; + FILE *in, *out; + + sprintf(buff, "%s/%s.uu", refdir, name); + in = fopen(buff, "r"); + failure("Couldn't open reference file %s", buff); + assert(in != NULL); + if (in == NULL) + return; + /* Read up to and including the 'begin' line. */ + for (;;) { + if (fgets(buff, sizeof(buff), in) == NULL) { + /* TODO: This is a failure. */ + return; + } + if (memcmp(buff, "begin ", 6) == 0) + break; + } + /* Now, decode the rest and write it. */ + /* Not a lot of error checking here; the input better be right. */ + out = fopen(name, "w"); + while (fgets(buff, sizeof(buff), in) != NULL) { + char *p = buff; + int bytes; + + if (memcmp(buff, "end", 3) == 0) + break; + + bytes = uudecode(*p++); + while (bytes > 0) { + int n = 0; + /* Write out 1-3 bytes from that. */ + if (bytes > 0) { + n = uudecode(*p++) << 18; + n |= uudecode(*p++) << 12; + fputc(n >> 16, out); + --bytes; + } + if (bytes > 0) { + n |= uudecode(*p++) << 6; + fputc((n >> 8) & 0xFF, out); + --bytes; + } + if (bytes > 0) { + n |= uudecode(*p++); + fputc(n & 0xFF, out); + --bytes; + } + } + } + fclose(out); + fclose(in); +} + + int main(int argc, char **argv) { static const int limit = sizeof(tests) / sizeof(tests[0]); @@ -671,15 +743,17 @@ int main(int argc, char **argv) ++p; } +#ifdef PROGRAM /* Get the target program from environment, if available. */ - testprog = getenv(PROGRAM); + testprog = getenv(ENVBASE); +#endif /* Allow -k to be controlled through the environment. */ - if (getenv(PROGRAM "_KEEP_GOING") != NULL) + if (getenv(ENVBASE "_KEEP_GOING") != NULL) dump_on_failure = 0; /* Get the directory holding test files from environment. */ - refdir = getenv(PROGRAM "_TEST_FILES"); + refdir = getenv(ENVBASE "_TEST_FILES"); /* * Parse options. @@ -690,7 +764,11 @@ int main(int argc, char **argv) dump_on_failure = 0; break; case 'p': +#ifdef PROGRAM testprog = optarg; +#else + usage(progname); +#endif break; case 'q': quiet_flag++; @@ -709,9 +787,10 @@ int main(int argc, char **argv) /* * Sanity-check that our options make sense. */ +#ifdef PROGRAM if (testprog == NULL) usage(progname); - +#endif /* * Create a temp directory for the following tests. @@ -753,7 +832,12 @@ int main(int argc, char **argv) if (!quiet_flag) { printf("Running tests in: %s\n", tmpdir); printf("Reference files will be read from: %s\n", refdir); +#ifdef PROGRAM printf("Running tests on: %s\n", testprog); +#endif + printf("Exercising: "); + fflush(stdout); + printf("%s\n", EXTRA_VERSION); } /* diff --git a/archivers/libarchive/files/cpio/test/test.h b/archivers/libarchive/files/cpio/test/test.h index fcd3304d4ef..06e6d903fc8 100644 --- a/archivers/libarchive/files/cpio/test/test.h +++ b/archivers/libarchive/files/cpio/test/test.h @@ -22,7 +22,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $FreeBSD: src/lib/libarchive/test/test.h,v 1.6 2007/07/14 17:52:01 kientzle Exp $ + * $FreeBSD$ */ /* Every test program should #include "test.h" as the first thing. */ @@ -31,16 +31,20 @@ * The goal of this file (and the matching test.c) is to * simplify the very repetitive test-*.c test programs. */ - +#ifndef _FILE_OFFSET_BITS #define _FILE_OFFSET_BITS 64 +#endif +#include <dirent.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> +#ifndef _WIN32 #include <unistd.h> +#endif #include <wchar.h> #ifdef USE_DMALLOC @@ -99,7 +103,7 @@ #define assertEmptyFile \ test_setup(__FILE__, __LINE__);test_assert_empty_file /* Assert that file contents match a string; supports printf-style arguments. */ -#define assertFileContents \ +#define assertFileContents \ test_setup(__FILE__, __LINE__);test_assert_file_contents /* @@ -131,15 +135,11 @@ int systemf(const char * fmt, ...); /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */ char *slurpfile(size_t *, const char *fmt, ...); -/* - * Global vars - */ - -/* Directory holding reference files. */ -char *refdir; +/* Extracts named reference file to the current directory. */ +void extract_reference_file(const char *); /* - * Special interfaces for bsdcpio test harness. + * Special interfaces for program test harness. */ /* Pathname of exe to be tested. */ diff --git a/archivers/libarchive/files/cpio/test/test_basic.c b/archivers/libarchive/files/cpio/test/test_basic.c index 3a9982b8e1c..449d90e67b9 100644 --- a/archivers/libarchive/files/cpio/test/test_basic.c +++ b/archivers/libarchive/files/cpio/test/test_basic.c @@ -53,6 +53,7 @@ basic_cpio(const char *target, const char *pack_options, const char *unpack_opti assertEqualInt(r, 0); /* Verify stderr. */ + failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target); assertEmptyFile("unpack.err"); /* diff --git a/archivers/libarchive/files/cpio/test/test_format_newc.c b/archivers/libarchive/files/cpio/test/test_format_newc.c index 8d24ad5f020..68cc193cf2a 100644 --- a/archivers/libarchive/files/cpio/test/test_format_newc.c +++ b/archivers/libarchive/files/cpio/test/test_format_newc.c @@ -90,6 +90,9 @@ DEFINE_TEST(test_format_newc) assertEqualInt(0, link("file1", "hardlink")); assertEqualInt(9, write(list, "hardlink\n", 9)); + /* Another hardlink, but this one won't be archived. */ + assertEqualInt(0, link("file1", "hardlink2")); + /* "symlink" */ assertEqualInt(0, symlink("file1", "symlink")); assertEqualInt(8, write(list, "symlink\n", 8)); @@ -105,7 +108,8 @@ DEFINE_TEST(test_format_newc) close(list); r = systemf("%s -o --format=newc --quiet <list >newc.out 2>newc.err", testprog); - assertEqualInt(r, 0); + if (!assertEqualInt(r, 0)) + return; /* Verify that nothing went to stderr. */ assertEmptyFile("newc.err"); @@ -127,21 +131,16 @@ DEFINE_TEST(test_format_newc) assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */ assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */ gid = from_hex(e + 30, 8); /* gid */ - assertEqualMem(e + 38, "00000002", 8); /* nlink */ + assertEqualMem(e + 38, "00000003", 8); /* nlink */ t = from_hex(e + 46, 8); /* mtime */ failure("t=0x%08x now=0x%08x=%d", t, now, now); assert(t <= now); /* File wasn't created in future. */ failure("t=0x%08x now - 2=0x%08x = %d", t, now - 2, now - 2); assert(t >= now - 2); /* File was created w/in last 2 secs. */ -#if 0 - /* TODO: FIX THIS!!! */ failure("newc format stores body only with last appearance of a link\n" " first appearance should be empty, so this file size\n" " field should be zero"); assertEqualInt(0, from_hex(e + 54, 8)); /* File size */ -#else - skipping("Known bug in writing hardlinks to newc files.\n This bug will be fixed before bsdcpio 1.0 is released.\n Note that this is not a bug in libarchive's implementation of newc format,\n it is a bug in bsdcpio not properly marking subsequent links to a file."); -#endif devmajor = from_hex(e + 62, 8); /* devmajor */ devminor = from_hex(e + 70, 8); /* devminor */ assert(is_hex(e + 78, 8)); /* rdevmajor */ @@ -152,27 +151,6 @@ DEFINE_TEST(test_format_newc) /* Since there's another link, no file contents here. */ /* But add in file size so that an error here doesn't cascade. */ e += 116 + from_hex(e + 54, 8) + (3 & -from_hex(e + 54, 8)); - - /* Hardlink identical to "file1" */ - assert(is_hex(e, 110)); - assertEqualMem(e + 0, "070701", 6); /* Magic */ - assertEqualInt(ino, from_hex(e + 6, 8)); /* ino */ - assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */ - assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */ - assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */ - assertEqualMem(e + 38, "00000002", 8); /* nlink */ - assertEqualInt(t, from_hex(e + 46, 8)); /* mtime */ - assertEqualInt(10, from_hex(e + 54, 8)); /* File size */ - assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */ - assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */ - assert(is_hex(e + 78, 8)); /* rdevmajor */ - assert(is_hex(e + 86, 8)); /* rdevminor */ - assertEqualMem(e + 94, "00000009", 8); /* Name size */ - assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ - assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */ - assertEqualMem(e + 120, "123456789\0\0\0", 12); /* File contents */ - e += 120 + from_hex(e + 54, 8) + (3 & -from_hex(e + 54, 8)); - /* "symlink" pointing to "file1" */ assert(is_hex(e, 110)); assertEqualMem(e + 0, "070701", 6); /* Magic */ @@ -214,6 +192,29 @@ DEFINE_TEST(test_format_newc) /* TODO: Verify other types of entries. */ + /* Hardlink identical to "file1" */ + /* Since we only wrote two of the three links to this + * file, this link should get deferred by the hardlink logic. */ + assert(is_hex(e, 110)); + assertEqualMem(e + 0, "070701", 6); /* Magic */ + failure("If these aren't the same, then the hardlink detection failed to match them."); + assertEqualInt(ino, from_hex(e + 6, 8)); /* ino */ + assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */ + assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */ + assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */ + assertEqualMem(e + 38, "00000003", 8); /* nlink */ + assertEqualInt(t, from_hex(e + 46, 8)); /* mtime */ + assertEqualInt(10, from_hex(e + 54, 8)); /* File size */ + assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */ + assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */ + assert(is_hex(e + 78, 8)); /* rdevmajor */ + assert(is_hex(e + 86, 8)); /* rdevminor */ + assertEqualMem(e + 94, "00000009", 8); /* Name size */ + assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ + assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */ + assertEqualMem(e + 120, "123456789\0\0\0", 12); /* File contents */ + e += 120 + from_hex(e + 54, 8) + (3 & -from_hex(e + 54, 8)); + /* Last entry is end-of-archive marker. */ assert(is_hex(e, 110)); assertEqualMem(e + 0, "070701", 6); /* Magic */ diff --git a/archivers/libarchive/files/cpio/test/test_gcpio_compat.c b/archivers/libarchive/files/cpio/test/test_gcpio_compat.c index 32f4b6b3103..61c9878b11e 100644 --- a/archivers/libarchive/files/cpio/test/test_gcpio_compat.c +++ b/archivers/libarchive/files/cpio/test/test_gcpio_compat.c @@ -40,10 +40,11 @@ unpack_test(const char *from, const char *options) /* * Use cpio to unpack the sample archive */ - r = systemf("%s -i --quiet %s < %s/%s >unpack.out 2>unpack.err", - testprog, options, refdir, from); - failure("Error invoking %s -i --quiet %s < %s/%s", - testprog, options, refdir, from); + extract_reference_file(from); + r = systemf("%s -i --quiet %s < %s >unpack.out 2>unpack.err", + testprog, options, from); + failure("Error invoking %s -i --quiet %s < %s", + testprog, options, from); assertEqualInt(r, 0); /* Verify that nothing went to stderr. */ diff --git a/archivers/libarchive/files/cpio/test/test_option_c.c b/archivers/libarchive/files/cpio/test/test_option_c.c index 7596ba9de88..9c422c97058 100644 --- a/archivers/libarchive/files/cpio/test/test_option_c.c +++ b/archivers/libarchive/files/cpio/test/test_option_c.c @@ -89,11 +89,14 @@ DEFINE_TEST(test_option_c) /* Use the cpio program to create an archive. */ close(filelist); r = systemf("%s -oc --quiet <filelist >basic.out 2>basic.err", testprog); - assertEqualInt(r, 0); - /* Verify that nothing went to stderr. */ assertEmptyFile("basic.err"); + /* Assert that the program finished. */ + failure("%s -oc crashed", testprog); + if (!assertEqualInt(r, 0)) + return; + /* Verify that stdout is a well-formed cpio file in "odc" format. */ p = slurpfile(&s, "basic.out"); assertEqualInt(s, 512); diff --git a/archivers/libarchive/files/cpio/test/test_option_f.c b/archivers/libarchive/files/cpio/test/test_option_f.c index c1fefd9c804..37aba7fa41d 100644 --- a/archivers/libarchive/files/cpio/test/test_option_f.c +++ b/archivers/libarchive/files/cpio/test/test_option_f.c @@ -35,7 +35,8 @@ unpack(const char *dirname, const char *option) assertEqualInt(0, mkdir(dirname, 0755)); assertEqualInt(0, chdir(dirname)); - r = systemf("%s -i --quiet %s < %s/test_option_f.cpio > copy-no-a.out 2>copy-no-a.err", testprog, option, refdir); + extract_reference_file("test_option_f.cpio"); + r = systemf("%s -i --quiet %s < test_option_f.cpio > copy-no-a.out 2>copy-no-a.err", testprog, option); assertEqualInt(0, r); assertEqualInt(0, chdir("..")); } diff --git a/archivers/libarchive/files/cpio/test/test_option_m.c b/archivers/libarchive/files/cpio/test/test_option_m.c index e43d259277a..d5bbad2e902 100644 --- a/archivers/libarchive/files/cpio/test/test_option_m.c +++ b/archivers/libarchive/files/cpio/test/test_option_m.c @@ -40,7 +40,8 @@ DEFINE_TEST(test_option_m) /* Restored without -m, the result should have a current mtime. */ assertEqualInt(0, mkdir("without-m", 0755)); assertEqualInt(0, chdir("without-m")); - r = systemf("%s -i < %s/test_option_m.cpio >out 2>err", testprog, refdir); + extract_reference_file("test_option_m.cpio"); + r = systemf("%s -i < test_option_m.cpio >out 2>err", testprog); now = time(NULL); assertEqualInt(r, 0); assertEmptyFile("out"); @@ -54,7 +55,8 @@ DEFINE_TEST(test_option_m) assertEqualInt(0, chdir("..")); assertEqualInt(0, mkdir("with-m", 0755)); assertEqualInt(0, chdir("with-m")); - r = systemf("%s -im < %s/test_option_m.cpio >out 2>err", testprog, refdir); + extract_reference_file("test_option_m.cpio"); + r = systemf("%s -im < test_option_m.cpio >out 2>err", testprog); now = time(NULL); assertEqualInt(r, 0); assertEmptyFile("out"); diff --git a/archivers/libarchive/files/cpio/test/test_option_t.c b/archivers/libarchive/files/cpio/test/test_option_t.c index 1bb993461f7..c9cdd0bc8a2 100644 --- a/archivers/libarchive/files/cpio/test/test_option_t.c +++ b/archivers/libarchive/files/cpio/test/test_option_t.c @@ -31,14 +31,17 @@ DEFINE_TEST(test_option_t) int r; /* List reference archive, make sure the TOC is correct. */ - r = systemf("%s -it < %s/test_option_t.cpio >t.out 2>t.err", testprog, refdir); + extract_reference_file("test_option_t.cpio"); + r = systemf("%s -it < test_option_t.cpio >t.out 2>t.err", testprog); assertEqualInt(r, 0); assertFileContents("1 block\n", 8, "t.err"); - assertEqualFile("t.out", "%s/test_option_t.stdout", refdir); + extract_reference_file("test_option_t.stdout"); + assertEqualFile("t.out", "test_option_t.stdout"); /* List reference archive verbosely, make sure the TOC is correct. */ - r = systemf("%s -itv < %s/test_option_t.cpio >tv.out 2>tv.err", testprog, refdir); + r = systemf("%s -itv < test_option_t.cpio >tv.out 2>tv.err", testprog); assertEqualInt(r, 0); assertFileContents("1 block\n", 8, "tv.err"); - assertEqualFile("tv.out", "%s/test_option_tv.stdout", refdir); + extract_reference_file("test_option_tv.stdout"); + assertEqualFile("tv.out", "test_option_tv.stdout"); } diff --git a/archivers/libarchive/files/cpio/test/test_option_version.c b/archivers/libarchive/files/cpio/test/test_option_version.c index 06df47a171c..ff90dfd824e 100644 --- a/archivers/libarchive/files/cpio/test/test_option_version.c +++ b/archivers/libarchive/files/cpio/test/test_option_version.c @@ -30,13 +30,15 @@ __FBSDID("$FreeBSD$"); */ static void -verify(const char *q, size_t s) +verify(const char *p, size_t s) { + const char *q = p; + /* Version message should start with name of program, then space. */ - failure("version message too short"); + failure("version message too short:", p); if (!assert(s > 6)) return; - failure("Version message should begin with 'bsdcpio'"); + failure("Version message should begin with 'bsdcpio': %s", p); if (!assertEqualMem(q, "bsdcpio ", 8)) /* If we're not testing bsdcpio, don't keep going. */ return; @@ -47,14 +49,21 @@ verify(const char *q, size_t s) --s; } /* Version number terminated by space. */ + failure("Version: %s", p); assert(s > 1); + /* Skip a single trailing a,b,c, or d. */ + if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd') + ++q; + failure("Version: %s", p); assert(*q == ' '); ++q; --s; /* Separator. */ + failure("Version: %s", p); assertEqualMem(q, "-- ", 3); q += 3; s -= 3; /* libarchive name and version number */ assert(s > 11); + failure("Version: %s", p); assertEqualMem(q, "libarchive ", 11); q += 11; s -= 11; /* Version number is a series of digits and periods. */ @@ -62,8 +71,12 @@ verify(const char *q, size_t s) ++q; --s; } + /* Skip a single trailing a,b,c, or d. */ + if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd') + ++q; /* All terminated by a newline. */ assert(s >= 1); + failure("Version: %s", p); assertEqualMem(q, "\n", 1); } |