diff options
author | kientzle <kientzle@FreeBSD.org> | 2009-04-17 01:06:31 +0000 |
---|---|---|
committer | kientzle <kientzle@FreeBSD.org> | 2009-04-17 01:06:31 +0000 |
commit | 621fee72b361fb219dc90b56c58ec551f03a1072 (patch) | |
tree | 53d4a3656571c490fa7ee297568309894736ba26 /lib/libarchive/test/main.c | |
parent | 68cb951cd0d330003aa4564ad6312d927b58c5ae (diff) | |
download | FreeBSD-src-621fee72b361fb219dc90b56c58ec551f03a1072.zip FreeBSD-src-621fee72b361fb219dc90b56c58ec551f03a1072.tar.gz |
Merge lots of test suite updates from libarchive.googlecode.com.
Diffstat (limited to 'lib/libarchive/test/main.c')
-rw-r--r-- | lib/libarchive/test/main.c | 148 |
1 files changed, 93 insertions, 55 deletions
diff --git a/lib/libarchive/test/main.c b/lib/libarchive/test/main.c index 01a0817..4022370 100644 --- a/lib/libarchive/test/main.c +++ b/lib/libarchive/test/main.c @@ -62,11 +62,7 @@ __FBSDID("$FreeBSD$"); */ #undef DEFINE_TEST #define DEFINE_TEST(name) void name(void); -#ifdef LIST_H -#include LIST_H -#else #include "list.h" -#endif /* Interix doesn't define these in a standard header. */ #if __INTERIX__ @@ -128,13 +124,28 @@ static const char *failed_filename = NULL; static struct line { int line; int count; + int critical; } failed_lines[1000]; /* + * Called at the beginning of each assert() function. + */ +static void +count_assertion(const char *file, int line) +{ + (void)file; /* UNUSED */ + (void)line; /* UNUSED */ + ++assertions; + /* Uncomment to print file:line after every assertion. + * Verbose, but occasionally useful in tracking down crashes. */ + /* printf("Checked %s:%d\n", file, line); */ +} + +/* * Count this failure; return the number of previous failures. */ static int -previous_failures(const char *filename, int line) +previous_failures(const char *filename, int line, int critical) { unsigned int i; int count; @@ -152,6 +163,7 @@ previous_failures(const char *filename, int line) if (failed_lines[i].line == 0) { failed_lines[i].line = line; failed_lines[i].count = 1; + failed_lines[i].critical = critical; return (0); } } @@ -178,7 +190,7 @@ test_skipping(const char *fmt, ...) { va_list ap; - if (previous_failures(test_filename, test_line)) + if (previous_failures(test_filename, test_line, 0)) return; va_start(ap, fmt); @@ -241,7 +253,7 @@ summarize(void) for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { if (failed_lines[i].line == 0) break; - if (failed_lines[i].count > 1) + if (failed_lines[i].count > 1 && failed_lines[i].critical) fprintf(stderr, "%s:%d: Failed %d times\n", failed_filename, failed_lines[i].line, failed_lines[i].count); @@ -264,13 +276,13 @@ failure(const char *fmt, ...) int test_assert(const char *file, int line, int value, const char *condition, void *extra) { - ++assertions; + count_assertion(file, line); if (value) { msg[0] = '\0'; return (value); } failures ++; - if (!verbose && previous_failures(file, line)) + if (!verbose && previous_failures(file, line, 1)) return (value); fprintf(stderr, "%s:%d: Assertion failed\n", file, line); fprintf(stderr, " Condition: %s\n", condition); @@ -283,13 +295,13 @@ int test_assert_equal_int(const char *file, int line, int v1, const char *e1, int v2, const char *e2, void *extra) { - ++assertions; + count_assertion(file, line); if (v1 == v2) { msg[0] = '\0'; return (1); } failures ++; - if (!verbose && previous_failures(file, line)) + if (!verbose && previous_failures(file, line, 1)) return (0); fprintf(stderr, "%s:%d: Assertion failed: Ints not equal\n", file, line); @@ -330,7 +342,7 @@ test_assert_equal_string(const char *file, int line, const char *v2, const char *e2, void *extra) { - ++assertions; + count_assertion(file, line); if (v1 == NULL || v2 == NULL) { if (v1 == v2) { msg[0] = '\0'; @@ -341,7 +353,7 @@ test_assert_equal_string(const char *file, int line, return (1); } failures ++; - if (!verbose && previous_failures(file, line)) + if (!verbose && previous_failures(file, line, 1)) return (0); fprintf(stderr, "%s:%d: Assertion failed: Strings not equal\n", file, line); @@ -383,7 +395,7 @@ test_assert_equal_wstring(const char *file, int line, const wchar_t *v2, const char *e2, void *extra) { - ++assertions; + count_assertion(file, line); if (v1 == NULL) { if (v2 == NULL) { msg[0] = '\0'; @@ -399,7 +411,7 @@ test_assert_equal_wstring(const char *file, int line, return (1); } failures ++; - if (!verbose && previous_failures(file, line)) + if (!verbose && previous_failures(file, line, 1)) return (0); fprintf(stderr, "%s:%d: Assertion failed: Unicode strings not equal\n", file, line); @@ -458,7 +470,7 @@ test_assert_equal_mem(const char *file, int line, const char *v2, const char *e2, size_t l, const char *ld, void *extra) { - ++assertions; + count_assertion(file, line); if (v1 == NULL || v2 == NULL) { if (v1 == v2) { msg[0] = '\0'; @@ -469,7 +481,7 @@ test_assert_equal_mem(const char *file, int line, return (1); } failures ++; - if (!verbose && previous_failures(file, line)) + if (!verbose && previous_failures(file, line, 1)) return (0); fprintf(stderr, "%s:%d: Assertion failed: memory not equal\n", file, line); @@ -507,7 +519,7 @@ test_assert_empty_file(const char *f1fmt, ...) return (1); failures ++; - if (!verbose && previous_failures(test_filename, test_line)) + if (!verbose && previous_failures(test_filename, test_line, 1)) return (0); fprintf(stderr, "%s:%d: File not empty: %s\n", test_filename, test_line, f1); @@ -554,7 +566,7 @@ test_assert_equal_file(const char *f1, const char *f2pattern, ...) break; } failures ++; - if (!verbose && previous_failures(test_filename, test_line)) + if (!verbose && previous_failures(test_filename, test_line, 1)) return (0); fprintf(stderr, "%s:%d: Files are not identical\n", test_filename, test_line); @@ -576,7 +588,7 @@ test_assert_file_exists(const char *fpattern, ...) if (!access(f, F_OK)) return (1); - if (!previous_failures(test_filename, test_line)) { + if (!previous_failures(test_filename, test_line, 1)) { fprintf(stderr, "%s:%d: File doesn't exist\n", test_filename, test_line); fprintf(stderr, " file=\"%s\"\n", f); @@ -597,7 +609,7 @@ test_assert_file_not_exists(const char *fpattern, ...) if (access(f, F_OK)) return (1); - if (!previous_failures(test_filename, test_line)) { + if (!previous_failures(test_filename, test_line, 1)) { fprintf(stderr, "%s:%d: File exists and shouldn't\n", test_filename, test_line); fprintf(stderr, " file=\"%s\"\n", f); @@ -628,7 +640,7 @@ test_assert_file_contents(const void *buff, int s, const char *fpattern, ...) return (1); } failures ++; - if (!previous_failures(test_filename, test_line)) { + if (!previous_failures(test_filename, test_line, 1)) { fprintf(stderr, "%s:%d: File contents don't match\n", test_filename, test_line); fprintf(stderr, " file=\"%s\"\n", f); @@ -722,13 +734,19 @@ slurpfile(size_t * sizep, const char *fmt, ...) #undef DEFINE_TEST #define DEFINE_TEST(n) { n, #n }, struct { void (*func)(void); const char *name; } tests[] = { -#ifdef LIST_H - #include LIST_H -#else #include "list.h" -#endif }; +/* + * This is well-intentioned, but sometimes the standard libraries + * leave open file descriptors and expect to be able to come back to + * them (e.g., for username lookups or logging). Closing these + * descriptors out from under those libraries creates havoc. + * + * Maybe there's some reasonably portable way to tell if a descriptor + * is open without using close()? + */ +#if 0 static void close_descriptors(int warn) { @@ -745,6 +763,7 @@ close_descriptors(int warn) report_failure(NULL); } } +#endif /* * Each test is run in a private work dir. Those work dirs @@ -788,11 +807,12 @@ static int test_run(int i, const char *tmpdir) /* Explicitly reset the locale before each test. */ setlocale(LC_ALL, "C"); /* Make sure there are no stray descriptors going into the test. */ - close_descriptors(0); + /* TODO: Find a better way to identify file descriptor leaks. */ + //close_descriptors(0); /* Run the actual test. */ (*tests[i].func)(); /* Close stray descriptors, record as errors against this test. */ - close_descriptors(1); + //close_descriptors(1); /* Summarize the results of this test. */ summarize(); /* If there were no failures, we can remove the work dir. */ @@ -894,30 +914,34 @@ extract_reference_file(const char *name) fclose(in); } -#if defined(_WIN32) && !defined(__CYGWIN__) -#define DEV_NULL "NUL" -#else -#define DEV_NULL "/dev/null" -#endif +/* Since gzip is by far the most popular external compression program + * available, we try to use it in the read_program and write_program + * tests. But if it's not available, then we can't use it. This + * function just tries to run gzip/gunzip to see if they're available. + * If not, some of the external compression program tests will be + * skipped. */ const char * external_gzip_program(int un) { - const char *extprog; - - if (un) { - extprog = "gunzip"; - if (systemf("%s -V >" DEV_NULL " 2>" DEV_NULL, extprog) == 0) - return (extprog); - extprog = "gzip -d"; - if (systemf("%s -V >" DEV_NULL " 2>" DEV_NULL, extprog) == 0) - return (extprog); - } else { - extprog = "gzip"; - if (systemf("%s -V >" DEV_NULL " 2>" DEV_NULL, extprog) == 0) - return (extprog); + static int tested = 0; + static const char *compress_prog = NULL; + static const char *decompress_prog = NULL; + /* Args vary depending on the command interpreter we're using. */ +#if defined(_WIN32) && !defined(__CYGWIN__) + static const char *args = "-V >NUL 2>NUL"; /* Win32 cmd.exe */ +#else + static const char *args = "-V >/dev/null 2>/dev/null"; /* POSIX 'sh' */ +#endif + + if (!tested) { + if (systemf("gunzip %s", args) == 0) + decompress_prog = "gunzip"; + if (systemf("gzip %s", args) == 0) + compress_prog = "gzip"; + tested = 1; } - return (NULL); + return (un ? decompress_prog : compress_prog); } static char * @@ -1144,16 +1168,30 @@ int main(int argc, char **argv) } } else { while (*(argv) != NULL) { - i = atoi(*argv); - if (**argv < '0' || **argv > '9' || i < 0 || i >= limit) { - printf("*** INVALID Test %s\n", *argv); - free(refdir_alloc); - usage(progname); + if (**argv >= '0' && **argv <= '9') { + i = atoi(*argv); + if (i < 0 || i >= limit) { + printf("*** INVALID Test %s\n", *argv); + free(refdir_alloc); + usage(progname); + /* usage() never returns */ + } } else { - if (test_run(i, tmpdir)) - tests_failed++; - tests_run++; + for (i = 0; i < limit; ++i) { + if (strcmp(*argv, tests[i].name) == 0) + break; + } + if (i >= limit) { + printf("*** INVALID Test ``%s''\n", + *argv); + free(refdir_alloc); + usage(progname); + /* usage() never returns */ + } } + if (test_run(i, tmpdir)) + tests_failed++; + tests_run++; argv++; } } |