From 6cf3aa0ad277135ba4eaae2d67de3d2f5375786f Mon Sep 17 00:00:00 2001 From: kientzle Date: Sat, 7 Mar 2009 03:16:16 +0000 Subject: [PATCH] Merge r280,281,496,595,675,712 from libarchive.googlecode.com: Various test improvements, including some work on Windows compatibility and an extra check to verify that no test leaves open file descriptors around. --- lib/libarchive/test/main.c | 64 +++++++++++++++++-- lib/libarchive/test/test.h | 3 + .../test/test_read_compress_program.c | 12 +++- lib/libarchive/test/test_read_extract.c | 1 + .../test/test_write_compress_program.c | 38 ++++++++++- 5 files changed, 108 insertions(+), 10 deletions(-) diff --git a/lib/libarchive/test/main.c b/lib/libarchive/test/main.c index ecffec0c49fb..64d92ff04d1c 100644 --- a/lib/libarchive/test/main.c +++ b/lib/libarchive/test/main.c @@ -62,7 +62,11 @@ __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__ @@ -343,10 +347,10 @@ test_assert_equal_string(const char *file, int line, file, line); fprintf(stderr, " %s = ", e1); strdump(v1); - fprintf(stderr, " (length %d)\n", v1 == NULL ? 0 : strlen(v1)); + fprintf(stderr, " (length %d)\n", v1 == NULL ? 0 : (int)strlen(v1)); fprintf(stderr, " %s = ", e2); strdump(v2); - fprintf(stderr, " (length %d)\n", v2 == NULL ? 0 : strlen(v2)); + fprintf(stderr, " (length %d)\n", v2 == NULL ? 0 : (int)strlen(v2)); report_failure(extra); return (0); } @@ -421,7 +425,7 @@ hexdump(const char *p, const char *ref, size_t l, size_t offset) char sep; for(i=0; i < l; i+=16) { - fprintf(stderr, "%04x", i + offset); + fprintf(stderr, "%04x", (unsigned)(i + offset)); sep = ' '; for (j = 0; j < 16 && i + j < l; j++) { if (ref != NULL && p[i + j] != ref[i + j]) @@ -718,9 +722,30 @@ 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 }; +static void +close_descriptors(int warn) +{ + int i; + int left_open = 0; + + for (i = 3; i < 100; ++i) { + if (close(i) == 0) + ++left_open; + } + if (warn && left_open > 0) { + fprintf(stderr, " ** %d descriptors unclosed\n", left_open); + failures += left_open; + report_failure(NULL); + } +} + /* * Each test is run in a private work dir. Those work dirs * do have consistent and predictable names, in case a group @@ -762,8 +787,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); /* Run the actual test. */ (*tests[i].func)(); + /* Close stray descriptors, record as errors against this test. */ + close_descriptors(1); /* Summarize the results of this test. */ summarize(); /* If there were no failures, we can remove the work dir. */ @@ -865,6 +894,32 @@ extract_reference_file(const char *name) fclose(in); } +#ifdef _WIN32 +#define DEV_NULL "NUL" +#else +#define DEV_NULL "/dev/null" +#endif + +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); + } + return (NULL); +} + static char * get_refdir(void) { @@ -873,7 +928,6 @@ get_refdir(void) char *pwd, *p; /* Get the current dir. */ - /* XXX Visual C++ uses _getcwd() XXX */ pwd = getcwd(NULL, 0); while (pwd[strlen(pwd) - 1] == '\n') pwd[strlen(pwd) - 1] = '\0'; @@ -974,7 +1028,7 @@ int main(int argc, char **argv) * Parse options, without using getopt(), which isn't available * on all platforms. */ - ++argv; --argc;/* Skip program name */ + ++argv; /* Skip program name */ while (*argv != NULL) { if (**argv != '-') break; diff --git a/lib/libarchive/test/test.h b/lib/libarchive/test/test.h index 678910a21fa7..19b2a67ecf0b 100644 --- a/lib/libarchive/test/test.h +++ b/lib/libarchive/test/test.h @@ -157,6 +157,9 @@ char *slurpfile(size_t *, const char *fmt, ...); /* Extracts named reference file to the current directory. */ void extract_reference_file(const char *); +/* Get external gzip program name */ +const char *external_gzip_program(int un); + /* * Special interfaces for libarchive test harness. */ diff --git a/lib/libarchive/test/test_read_compress_program.c b/lib/libarchive/test/test_read_compress_program.c index a6626dcbeff3..020c8f854cf7 100644 --- a/lib/libarchive/test/test_read_compress_program.c +++ b/lib/libarchive/test/test_read_compress_program.c @@ -41,12 +41,20 @@ DEFINE_TEST(test_read_compress_program) #else struct archive_entry *ae; struct archive *a; + const char *extprog; + + if ((extprog = external_gzip_program(1)) == NULL) { + skipping("There is no gzip uncompression " + "program in this platform"); + return; + } assert((a = archive_read_new()) != NULL); assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_none(a)); - r = archive_read_support_compression_program(a, "gunzip"); + r = archive_read_support_compression_program(a, extprog); if (r == ARCHIVE_FATAL) { - skipping("archive_read_support_compression_program() unsupported on this platform"); + skipping("archive_read_support_compression_program() " + "unsupported on this platform"); return; } assertEqualIntA(a, ARCHIVE_OK, r); diff --git a/lib/libarchive/test/test_read_extract.c b/lib/libarchive/test/test_read_extract.c index e55f23159da6..b58b364a8418 100644 --- a/lib/libarchive/test/test_read_extract.c +++ b/lib/libarchive/test/test_read_extract.c @@ -158,6 +158,7 @@ DEFINE_TEST(test_read_extract) failure("The file on disk could not be opened."); assert(fd != 0); bytes_read = read(fd, buff, FILE_BUFF_SIZE); + close(fd); failure("The file contents read from disk are the wrong size"); assert(bytes_read == FILE_BUFF_SIZE); failure("The file contents on disk do not match the file contents that were put into the archive."); diff --git a/lib/libarchive/test/test_write_compress_program.c b/lib/libarchive/test/test_write_compress_program.c index 9b525e6d13a2..c4cf9333e248 100644 --- a/lib/libarchive/test/test_write_compress_program.c +++ b/lib/libarchive/test/test_write_compress_program.c @@ -38,14 +38,21 @@ DEFINE_TEST(test_write_compress_program) size_t used; int blocksize = 1024; int r; + const char *extprog; + if ((extprog = external_gzip_program(0)) == NULL) { + skipping("There is no gzip compression " + "program in this platform"); + return; + } /* Create a new archive in memory. */ /* Write it through an external "gzip" program. */ assert((a = archive_write_new()) != NULL); assertA(0 == archive_write_set_format_ustar(a)); - r = archive_write_set_compression_program(a, "gzip"); + r = archive_write_set_compression_program(a, extprog); if (r == ARCHIVE_FATAL) { - skipping("Write compression via external program unsupported on this platform"); + skipping("Write compression via external " + "program unsupported on this platform"); archive_write_finish(a); return; } @@ -84,7 +91,32 @@ DEFINE_TEST(test_write_compress_program) assertA(0 == archive_read_support_compression_all(a)); assertA(0 == archive_read_open_memory(a, buff, used)); - assertA(0 == archive_read_next_header(a, &ae)); + r = archive_read_next_header(a, &ae); + if (r != ARCHIVE_OK) { + if (strcmp(archive_error_string(a), + "Unrecognized archive format") == 0) { + skipping("This version of libarchive was compiled " + "without gzip support"); + assert(0 == archive_read_finish(a)); + /* + * Try using an external "gunzip","gzip -d" program + */ + if ((extprog = external_gzip_program(1)) == NULL) { + skipping("There is no gzip uncompression " + "program in this platform"); + return; + } + assert((a = archive_read_new()) != NULL); + assertEqualIntA(a, ARCHIVE_OK, + archive_read_support_compression_none(a)); + assertEqualIntA(a, ARCHIVE_OK, + archive_read_support_compression_program(a, extprog)); + assertA(0 == archive_read_support_format_all(a)); + assertA(0 == archive_read_open_memory(a, buff, used)); + r = archive_read_next_header(a, &ae); + } + } + assertA(0 == r); assert(1 == archive_entry_mtime(ae)); assert(0 == archive_entry_atime(ae));