]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Sync up the test harnesses somewhat.
authorTim Kientzle <kientzle@gmail.com>
Thu, 18 Jun 2009 05:44:35 +0000 (01:44 -0400)
committerTim Kientzle <kientzle@gmail.com>
Thu, 18 Jun 2009 05:44:35 +0000 (01:44 -0400)
SVN-Revision: 1171

libarchive/test/main.c
libarchive/test/test.h
tar/test/main.c
tar/test/test.h

index db15ca290900e35a598e30a088844c1f9d8903da..d68d16f6be8e93f96dfdc19f1f6b3b64bf1681e1 100644 (file)
@@ -518,7 +518,7 @@ test_assert_empty_file(const char *f1fmt, ...)
        struct stat st;
        va_list ap;
        ssize_t s;
-       int fd;
+       FILE *f;
 
 
        va_start(ap, f1fmt);
@@ -540,53 +540,92 @@ test_assert_empty_file(const char *f1fmt, ...)
        fprintf(stderr, "%s:%d: File not empty: %s\n", test_filename, test_line, f1);
        fprintf(stderr, "    File size: %d\n", (int)st.st_size);
        fprintf(stderr, "    Contents:\n");
-       fd = open(f1, O_RDONLY | O_BINARY);
-       if (fd < 0) {
+       f = fopen(f1, "rb");
+       if (f != NULL) {
                fprintf(stderr, "    Unable to open %s\n", f1);
        } else {
-               s = sizeof(buff) < st.st_size ? sizeof(buff) : st.st_size;
-               s = read(fd, buff, s);
+               s = ((off_t)sizeof(buff) < st.st_size) ?
+                   (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
+               s = fread(buff, 1, s, f);
                hexdump(buff, NULL, s, 0);
+               fclose(f);
        }
        report_failure(NULL);
        return (0);
 }
 
+int
+test_assert_non_empty_file(const char *f1fmt, ...)
+{
+       char f1[1024];
+       struct stat st;
+       va_list ap;
+
+
+       va_start(ap, f1fmt);
+       vsprintf(f1, f1fmt, ap);
+       va_end(ap);
+
+       if (stat(f1, &st) != 0) {
+               fprintf(stderr, "%s:%d: Could not stat: %s\n",
+                   test_filename, test_line, f1);
+               report_failure(NULL);
+               failures++;
+               return (0);
+       }
+       if (st.st_size != 0)
+               return (1);
+
+       failures ++;
+       if (!verbose && previous_failures(test_filename, test_line, 1))
+               return (0);
+
+       fprintf(stderr, "%s:%d: File empty: %s\n",
+           test_filename, test_line, f1);
+       report_failure(NULL);
+       return (0);
+}
+
 /* assertEqualFile() asserts that two files have the same contents. */
 /* TODO: hexdump the first bytes that actually differ. */
 int
-test_assert_equal_file(const char *f1, const char *f2pattern, ...)
+test_assert_equal_file(const char *fn1, const char *f2pattern, ...)
 {
-       char f2[1024];
+       char fn2[1024];
        va_list ap;
        char buff1[1024];
        char buff2[1024];
-       int fd1, fd2;
+       FILE *f1, *f2;
        int n1, n2;
 
        va_start(ap, f2pattern);
-       vsprintf(f2, f2pattern, ap);
+       vsprintf(fn2, f2pattern, ap);
        va_end(ap);
 
-       fd1 = open(f1, O_RDONLY | O_BINARY);
-       fd2 = open(f2, O_RDONLY | O_BINARY);
+       f1 = fopen(fn1, "rb");
+       f2 = fopen(fn2, "rb");
        for (;;) {
-               n1 = read(fd1, buff1, sizeof(buff1));
-               n2 = read(fd2, buff2, sizeof(buff2));
+               n1 = fread(buff1, 1, sizeof(buff1), f1);
+               n2 = fread(buff2, 1, sizeof(buff2), f2);
                if (n1 != n2)
                        break;
-               if (n1 == 0 && n2 == 0)
+               if (n1 == 0 && n2 == 0) {
+                       fclose(f1);
+                       fclose(f2);
                        return (1);
+               }
                if (memcmp(buff1, buff2, n1) != 0)
                        break;
        }
+       fclose(f1);
+       fclose(f2);
        failures ++;
        if (!verbose && previous_failures(test_filename, test_line, 1))
                return (0);
        fprintf(stderr, "%s:%d: Files are not identical\n",
            test_filename, test_line);
-       fprintf(stderr, "  file1=\"%s\"\n", f1);
-       fprintf(stderr, "  file2=\"%s\"\n", f2);
+       fprintf(stderr, "  file1=\"%s\"\n", fn1);
+       fprintf(stderr, "  file2=\"%s\"\n", fn2);
        report_failure(test_extra);
        return (0);
 }
@@ -637,19 +676,29 @@ test_assert_file_not_exists(const char *fpattern, ...)
 int
 test_assert_file_contents(const void *buff, int s, const char *fpattern, ...)
 {
-       char f[1024];
+       char fn[1024];
        va_list ap;
        char *contents;
-       int fd;
+       FILE *f;
        int n;
 
        va_start(ap, fpattern);
-       vsprintf(f, fpattern, ap);
+       vsprintf(fn, fpattern, ap);
        va_end(ap);
 
-       fd = open(f, O_RDONLY | O_BINARY);
+       f = fopen(fn, "rb");
+       if (f == NULL) {
+               failures ++;
+               if (!previous_failures(test_filename, test_line, 1)) {
+                       fprintf(stderr, "%s:%d: File doesn't exist: %s\n",
+                           test_filename, test_line, fn);
+                       report_failure(test_extra);
+               }
+               return (0);
+       }
        contents = malloc(s * 2);
-       n = read(fd, contents, s * 2);
+       n = fread(contents, 1, s * 2, f);
+       fclose(f);
        if (n == s && memcmp(buff, contents, s) == 0) {
                free(contents);
                return (1);
@@ -658,7 +707,7 @@ test_assert_file_contents(const void *buff, int s, const char *fpattern, ...)
        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);
+               fprintf(stderr, "  file=\"%s\"\n", fn);
                if (n > 0)
                        hexdump(contents, buff, n, 0);
                else {
@@ -702,41 +751,41 @@ slurpfile(size_t * sizep, const char *fmt, ...)
        va_list ap;
        char *p;
        ssize_t bytes_read;
-       int fd;
+       FILE *f;
        int r;
 
        va_start(ap, fmt);
        vsprintf(filename, fmt, ap);
        va_end(ap);
 
-       fd = open(filename, O_RDONLY | O_BINARY);
-       if (fd < 0) {
+       f = fopen(filename, "rb");
+       if (f == NULL) {
                /* Note: No error; non-existent file is okay here. */
                return (NULL);
        }
-       r = fstat(fd, &st);
+       r = fstat(fileno(f), &st);
        if (r != 0) {
                fprintf(stderr, "Can't stat file %s\n", filename);
-               close(fd);
+               fclose(f);
                return (NULL);
        }
        p = malloc(st.st_size + 1);
        if (p == NULL) {
                fprintf(stderr, "Can't allocate %ld bytes of memory to read file %s\n", (long int)st.st_size, filename);
-               close(fd);
+               fclose(f);
                return (NULL);
        }
-       bytes_read = read(fd, p, st.st_size);
+       bytes_read = fread(p, 1, st.st_size, f);
        if (bytes_read < st.st_size) {
                fprintf(stderr, "Can't read file %s\n", filename);
-               close(fd);
+               fclose(f);
                free(p);
                return (NULL);
        }
        p[st.st_size] = '\0';
        if (sizep != NULL)
                *sizep = (size_t)st.st_size;
-       close(fd);
+       fclose(f);
        return (p);
 }
 
@@ -752,34 +801,6 @@ struct { void (*func)(void); const char *name; } tests[] = {
        #include "list.h"
 };
 
-/*
- * 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)
-{
-       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);
-       }
-}
-#endif
-
 /*
  * Each test is run in a private work dir.  Those work dirs
  * do have consistent and predictable names, in case a group
@@ -821,22 +842,17 @@ 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. */
-       /* 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);
        /* Summarize the results of this test. */
        summarize();
        /* If there were no failures, we can remove the work dir. */
        if (failures == failures_before) {
                if (!keep_temp_files && chdir(tmpdir) == 0) {
-#if !defined(_WIN32) || defined(__CYGWIN__)
-                       systemf("rm -rf %s", tests[i].name);
-#else
+#if defined(_WIN32) && !defined(__CYGWIN__)
                        systemf("rmdir /S /Q %s", tests[i].name);
+#else
+                       systemf("rm -rf %s", tests[i].name);
 #endif
                }
        }
@@ -895,7 +911,7 @@ extract_reference_file(const char *name)
        }
        /* Now, decode the rest and write it. */
        /* Not a lot of error checking here; the input better be right. */
-       out = fopen(name, "wb");
+       out = fopen(name, "w");
        while (fgets(buff, sizeof(buff), in) != NULL) {
                char *p = buff;
                int bytes;
@@ -1035,7 +1051,10 @@ int main(int argc, char **argv)
        int i, tests_run = 0, tests_failed = 0, option;
        time_t now;
        char *refdir_alloc = NULL;
-       const char *progname = LIBRARY "_test";
+#if defined(_WIN32) && !defined(__CYGWIN__)
+       char *testprg;
+#endif
+       const char *progname;
        const char *tmp, *option_arg, *p;
        char tmpdir[256];
        char tmpdir_timestamp[256];
@@ -1054,6 +1073,18 @@ int main(int argc, char **argv)
        _CrtSetReportMode(_CRT_ASSERT, 0);
 #endif
 
+       /*
+        * Name of this program, used to build root of our temp directory
+        * tree.
+        */
+       progname = p = argv[0];
+       while (*p != '\0') {
+               /* Support \ or / dir separators for Windows compat. */
+               if (*p == '/' || *p == '\\')
+                       progname = p + 1;
+               ++p;
+       }
+
 #ifdef PROGRAM
        /* Get the target program from environment, if available. */
        testprog = getenv(ENVBASE);
@@ -1141,6 +1172,18 @@ int main(int argc, char **argv)
        if (testprog == NULL)
                usage(progname);
 #endif
+#if defined(_WIN32) && !defined(__CYGWIN__)
+       /*
+        * command.com cannot accept the command used '/' with drive
+        * name such as c:/xxx/command.exe when use '|' pipe handling.
+        */
+       testprg = strdup(testprog);
+       for (i = 0; testprg[i] != '\0'; i++) {
+               if (testprg[i] == '/')
+                       testprg[i] = '\\';
+       }
+       testprog = testprg;
+#endif
 
        /*
         * Create a temp directory for the following tests.
index 0fcec4965cc67a05f36b0311de2992bec64e5288..b7cbd22957c834c41a28529868d4ac3bd0823711 100644 (file)
 #include <dmalloc.h>
 #endif
 
-/* No non-FreeBSD platform will have __FBSDID, so just define it here. */
 #ifdef __FreeBSD__
 #include <sys/cdefs.h>  /* For __FBSDID */
 #else
-/* Some non-FreeBSD platforms such as newlib-derived ones like 
+/* Some non-FreeBSD platforms such as newlib-derived ones like
  * cygwin, have __FBSDID, so this definition must be guarded.
  */
 #ifndef __FBSDID
 /* Assert that a file is empty; supports printf-style arguments. */
 #define assertEmptyFile                \
   test_setup(__FILE__, __LINE__);test_assert_empty_file
+/* Assert that a file is not empty; supports printf-style arguments. */
+#define assertNonEmptyFile             \
+  test_setup(__FILE__, __LINE__);test_assert_non_empty_file
 /* Assert that a file exists; supports printf-style arguments. */
 #define assertFileExists               \
   test_setup(__FILE__, __LINE__);test_assert_file_exists
@@ -143,6 +145,7 @@ void test_setup(const char *, int);
 void test_skipping(const char *fmt, ...);
 int test_assert(const char *, int, int, const char *, void *);
 int test_assert_empty_file(const char *, ...);
+int test_assert_non_empty_file(const char *, ...);
 int test_assert_equal_file(const char *, const char *, ...);
 int test_assert_equal_int(const char *, int, int, const char *, int, const char *, void *);
 int test_assert_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
index 5ca1c45bfb71c884ab4c44f14cf30fd74e51586b..87c220fa0ea0f987de5a849a73b7cfef957ef4f6 100644 (file)
 #include <locale.h>
 #include <stdarg.h>
 #include <time.h>
+#if defined(_WIN32) && !defined(__CYGWIN__)
+#if !defined(__GNUC__)
+#include <crtdbg.h>
+#endif
+#include <windows.h>
+#include <winbase.h>
+#endif
 
 /*
  * This same file is used pretty much verbatim for all test harnesses.
  * The next few lines are the only differences.
  */
 #define        PROGRAM "bsdtar" /* Name of program being tested. */
+#undef LIBRARY          /* Not testing a library. */
 #define ENVBASE "BSDTAR" /* 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) ? "" : "")
+#define KNOWNREF       "test_patterns_2.tar.uu"
 __FBSDID("$FreeBSD: src/usr.bin/tar/test/main.c,v 1.6 2008/11/05 06:40:53 kientzle Exp $");
 
 /*
@@ -82,6 +91,19 @@ static int assertions = 0;
 /* Directory where uuencoded reference files can be found. */
 static const char *refdir;
 
+
+#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__GNUC__)
+
+static void
+invalid_parameter_handler(const wchar_t * expression,
+    const wchar_t * function, const wchar_t * file,
+    unsigned int line, uintptr_t pReserved)
+{
+       /* nop */
+}
+
+#endif
+
 /*
  * My own implementation of the standard assert() macro emits the
  * message in the same format as GCC (file:line: message).
@@ -105,13 +127,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;
@@ -129,6 +166,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);
                }
        }
@@ -155,7 +193,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);
@@ -218,7 +256,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);
@@ -241,13 +279,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);
@@ -260,13 +298,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);
@@ -307,7 +345,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';
@@ -318,7 +356,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);
@@ -360,7 +398,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';
@@ -376,7 +414,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);
@@ -402,7 +440,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", (int)(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])
@@ -428,14 +466,17 @@ hexdump(const char *p, const char *ref, size_t l, size_t offset)
 }
 
 /* assertEqualMem() displays the values of the two memory blocks. */
-/* TODO: For long blocks, hexdump the first bytes that actually differ. */
 int
 test_assert_equal_mem(const char *file, int line,
-    const char *v1, const char *e1,
-    const char *v2, const char *e2,
+    const void *_v1, const char *e1,
+    const void *_v2, const char *e2,
     size_t l, const char *ld, void *extra)
 {
-       ++assertions;
+       const char *v1 = (const char *)_v1;
+       const char *v2 = (const char *)_v2;
+       size_t offset;
+
+       count_assertion(file, line);
        if (v1 == NULL || v2 == NULL) {
                if (v1 == v2) {
                        msg[0] = '\0';
@@ -446,15 +487,25 @@ 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);
        fprintf(stderr, "      size %s = %d\n", ld, (int)l);
+       /* Dump 48 bytes (3 lines) so that the first difference is
+        * in the second line. */
+       offset = 0;
+       while (l > 64 && memcmp(v1, v2, 32) == 0) {
+               /* The first two lines agree, so step forward one line. */
+               v1 += 16;
+               v2 += 16;
+               l -= 16;
+               offset += 16;
+       }
        fprintf(stderr, "      Dump of %s\n", e1);
-       hexdump(v1, v2, l < 32 ? l : 32, 0);
+       hexdump(v1, v2, l < 64 ? l : 64, offset);
        fprintf(stderr, "      Dump of %s\n", e2);
-       hexdump(v2, v1, l < 32 ? l : 32, 0);
+       hexdump(v2, v1, l < 64 ? l : 64, offset);
        fprintf(stderr, "\n");
        report_failure(extra);
        return (0);
@@ -476,9 +527,7 @@ test_assert_empty_file(const char *f1fmt, ...)
        va_end(ap);
 
        if (stat(f1, &st) != 0) {
-               fprintf(stderr, "%s:%d: Could not stat: %s\n",
-                   test_filename, test_line, f1);
-               failures ++;
+               fprintf(stderr, "%s:%d: Could not stat: %s\n", test_filename, test_line, f1);
                report_failure(NULL);
                return (0);
        }
@@ -486,7 +535,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);
@@ -529,7 +578,7 @@ test_assert_non_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 empty: %s\n",
@@ -572,7 +621,7 @@ test_assert_equal_file(const char *fn1, const char *f2pattern, ...)
        fclose(f1);
        fclose(f2);
        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);
@@ -594,7 +643,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);
@@ -615,7 +664,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);
@@ -641,7 +690,7 @@ test_assert_file_contents(const void *buff, int s, const char *fpattern, ...)
        f = fopen(fn, "rb");
        if (f == NULL) {
                failures ++;
-               if (!previous_failures(test_filename, test_line)) {
+               if (!previous_failures(test_filename, test_line, 1)) {
                        fprintf(stderr, "%s:%d: File doesn't exist: %s\n",
                            test_filename, test_line, fn);
                        report_failure(test_extra);
@@ -656,7 +705,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", fn);
@@ -897,23 +946,108 @@ extract_reference_file(const char *name)
        fclose(in);
 }
 
+static char *
+get_refdir(const char *d)
+{
+       char tried[512] = { '\0' };
+       char buff[128];
+       char *pwd, *p;
+
+       /* If a dir was specified, try that */
+       if (d != NULL) {
+               pwd = NULL;
+               strcpy(buff, d);
+               p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
+               if (p != NULL) goto success;
+               strcat(tried, d);
+               goto failure;
+       }
+
+       /* Get the current dir. */
+       pwd = getcwd(NULL, 0);
+       while (pwd[strlen(pwd) - 1] == '\n')
+               pwd[strlen(pwd) - 1] = '\0';
+       printf("PWD: %s\n", pwd);
+
+       /* Look for a known file. */
+       snprintf(buff, sizeof(buff), "%s", pwd);
+       p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
+       if (p != NULL) goto success;
+       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+
+       snprintf(buff, sizeof(buff), "%s/test", pwd);
+       p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
+       if (p != NULL) goto success;
+       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+
+#if defined(LIBRARY)
+       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
+#else
+       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
+#endif
+       p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
+       if (p != NULL) goto success;
+       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+
+       if (memcmp(pwd, "/usr/obj", 8) == 0) {
+               snprintf(buff, sizeof(buff), "%s", pwd + 8);
+               p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
+               if (p != NULL) goto success;
+               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+
+               snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
+               p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
+               if (p != NULL) goto success;
+               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       }
+
+failure:
+#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
+       DebugBreak();
+#endif
+       printf("Unable to locate known reference file %s\n", KNOWNREF);
+       printf("  Checked following directories:\n%s\n", tried);
+       exit(1);
+
+success:
+       free(p);
+       free(pwd);
+       return strdup(buff);
+}
 
 int main(int argc, char **argv)
 {
        static const int limit = sizeof(tests) / sizeof(tests[0]);
-       int i, tests_run = 0, tests_failed = 0, opt;
+       int i, tests_run = 0, tests_failed = 0, option;
        time_t now;
        char *refdir_alloc = NULL;
 #if defined(_WIN32) && !defined(__CYGWIN__)
        char *testprg;
 #endif
-       const char *opt_arg, *progname, *p;
-       char *tmp;
+       const char *progname;
+       const char *tmp, *option_arg, *p;
        char tmpdir[256];
        char tmpdir_timestamp[256];
 
        (void)argc; /* UNUSED */
 
+#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__GNUC__)
+       /* To stop to run the default invalid parameter handler. */
+       _set_invalid_parameter_handler(invalid_parameter_handler);
+       /* for open() to a binary mode. */
+       /* This shouldn't be needed, because all tests should
+        * explicitly use O_BINARY flag to open() and "b" to
+        * fopen(): */
+       /* _set_fmode(_O_BINARY); */
+       /* Disable annoying assertion message box. */
+       _CrtSetReportMode(_CRT_ASSERT, 0);
+#endif
+
        /*
         * Name of this program, used to build root of our temp directory
         * tree.
@@ -960,24 +1094,24 @@ int main(int argc, char **argv)
                p = *argv++;
                ++p; /* Skip '-' */
                while (*p != '\0') {
-                       opt = *p++;
-                       opt_arg = NULL;
+                       option = *p++;
+                       option_arg = NULL;
                        /* If 'opt' takes an argument, parse that. */
-                       if (opt == 'p' || opt == 'r') {
+                       if (option == 'p' || option == 'r') {
                                if (*p != '\0')
-                                       opt_arg = p;
+                                       option_arg = p;
                                else if (*argv == NULL) {
                                        fprintf(stderr,
                                            "Option -%c requires argument.\n",
-                                           opt);
+                                           option);
                                        usage(progname);
                                } else
-                                       opt_arg = *argv++;
+                                       option_arg = *argv++;
                                p = ""; /* End of this option word. */
                        }
 
                        /* Now, handle the option. */
-                       switch (opt) {
+                       switch (option) {
                        case 'd':
                                dump_on_failure = 1;
                                break;
@@ -986,7 +1120,7 @@ int main(int argc, char **argv)
                                break;
                        case 'p':
 #ifdef PROGRAM
-                               testprog = opt_arg;
+                               testprog = option_arg;
 #else
                                usage(progname);
 #endif
@@ -995,12 +1129,11 @@ int main(int argc, char **argv)
                                quiet_flag++;
                                break;
                        case 'r':
-                               refdir = opt_arg;
+                               refdir = option_arg;
                                break;
                        case 'v':
                                verbose = 1;
                                break;
-                       case '?':
                        default:
                                usage(progname);
                        }
@@ -1050,20 +1183,10 @@ int main(int argc, char **argv)
 
        /*
         * If the user didn't specify a directory for locating
-        * reference files, use the current directory for that.
+        * reference files, try to find the reference files in
+        * the "usual places."
         */
-       if (refdir == NULL) {
-               char *q;
-               systemf("/bin/pwd > %s/refdir", tmpdir);
-               q = slurpfile(NULL, "%s/refdir", tmpdir);
-               refdir = refdir_alloc = q;
-               q += strlen(refdir);
-               while (q[-1] == '\n') {
-                       --q;
-                       *q = '\0';
-               }
-               systemf("rm %s/refdir", tmpdir);
-       }
+       refdir = refdir_alloc = get_refdir(refdir);
 
        /*
         * Banner with basic information.
@@ -1091,15 +1214,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);
-                               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++;
                }
        }
@@ -1113,7 +1251,7 @@ int main(int argc, char **argv)
                    tests_failed, tests_run);
                printf(" Total of %d assertions checked.\n", assertions);
                printf(" Total of %d assertions failed.\n", failures);
-               printf(" Total of %d assertions skipped.\n", skips);
+               printf(" Total of %d reported skips.\n", skips);
        }
 
        free(refdir_alloc);
index c2b40c09cfd68bbfc945d2433fbe86e7bbf85f63..4dab51c324b131f990f448bb07bf9779a19803c9 100644 (file)
 #endif
 #endif
 
+#if defined(_WIN32) && !defined(__CYGWIN__)
+#define snprintf       sprintf_s
+#define LOCALE_DE      "deu"
+#else
+#define LOCALE_DE      "de_DE.UTF-8"
+#endif
+
+#ifndef O_BINARY
+#define        O_BINARY 0
+#endif
+
 /*
  * Redefine DEFINE_TEST for use in defining the test functions.
  */
@@ -141,7 +152,7 @@ int test_assert_equal_file(const char *, const char *, ...);
 int test_assert_equal_int(const char *, int, int, const char *, int, const char *, void *);
 int test_assert_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
 int test_assert_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
-int test_assert_equal_mem(const char *, int, const char *, const char *, const char *, const char *, size_t, const char *, void *);
+int test_assert_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
 int test_assert_file_contents(const void *, int, const char *, ...);
 int test_assert_file_exists(const char *, ...);
 int test_assert_file_not_exists(const char *, ...);