2 * Copyright (c) 2003-2009 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * This same file is used pretty much verbatim for all test harnesses.
35 * The next few lines are the only differences.
36 * TODO: Move this into a separate configuration header, have all test
37 * suites share one copy of this file.
39 __FBSDID("$FreeBSD$");
40 #define KNOWNREF "test_compat_gtar_1.tar.uu"
41 #define ENVBASE "LIBARCHIVE" /* Prefix for environment variables. */
42 #undef PROGRAM /* Testing a library, not a program. */
43 #define LIBRARY "libarchive"
44 #define EXTRA_DUMP(x) archive_error_string((struct archive *)(x))
45 #define EXTRA_VERSION archive_version()
49 * Windows support routines
51 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
52 * in the test harness is dangerous because they cover up
53 * configuration errors. The classic example of this is omitting a
54 * configure check. If libarchive and libarchive_test both look for
55 * the same feature macro, such errors are hard to detect. Platform
56 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
57 * easily lead to very messy code. It's best to limit yourself
58 * to only the most generic programming techniques in the test harness
59 * and thus avoid conditionals altogether. Where that's not possible,
60 * try to minimize conditionals by grouping platform-specific tests in
61 * one place (e.g., test_acl_freebsd) or by adding new assert()
62 * functions (e.g., assertMakeHardlink()) to cover up platform
63 * differences. Platform-specific coding in libarchive_test is often
64 * a symptom that some capability is missing from libarchive itself.
66 #if defined(_WIN32) && !defined(__CYGWIN__)
73 #define S_ISDIR(m) ((m) & _S_IFDIR)
76 #define S_ISREG(m) ((m) & _S_IFREG)
78 #if !defined(__BORLANDC__)
79 #define access _access
83 #define fileno _fileno
85 /*#define fstat _fstat64*/
86 #if !defined(__BORLANDC__)
87 #define getcwd _getcwd
90 /*#define lstat _stat64*/
91 /*#define stat _stat64*/
93 #if !defined(__BORLANDC__)
94 #define strdup _strdup
97 #define int64_t __int64
100 #if defined(HAVE__CrtSetReportMode)
104 #if defined(_WIN32) && !defined(__CYGWIN__)
105 void *GetFunctionKernel32(const char *name)
107 static HINSTANCE lib;
111 lib = LoadLibrary("kernel32.dll");
114 fprintf(stderr, "Can't load kernel32.dll?!\n");
117 return (void *)GetProcAddress(lib, name);
121 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
123 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
127 f = GetFunctionKernel32("CreateSymbolicLinkA");
129 return f == NULL ? 0 : (*f)(linkname, target, flags);
133 my_CreateHardLinkA(const char *linkname, const char *target)
135 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
139 f = GetFunctionKernel32("CreateHardLinkA");
141 return f == NULL ? 0 : (*f)(linkname, target, NULL);
145 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
150 memset(bhfi, 0, sizeof(*bhfi));
151 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
152 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
153 if (h == INVALID_HANDLE_VALUE)
155 r = GetFileInformationByHandle(h, bhfi);
161 #if defined(HAVE__CrtSetReportMode)
163 invalid_parameter_handler(const wchar_t * expression,
164 const wchar_t * function, const wchar_t * file,
165 unsigned int line, uintptr_t pReserved)
177 /* Enable core dump on failure. */
178 static int dump_on_failure = 0;
179 /* Default is to remove temp dirs and log data for successful tests. */
180 static int keep_temp_files = 0;
181 /* Default is to just report pass/fail for each test. */
182 static int verbosity = 0;
183 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
184 #define VERBOSITY_PASSFAIL 0 /* Default */
185 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
186 #define VERBOSITY_FULL 2 /* -vv */
187 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
188 * mostly for debugging the test harness itself. */
189 /* Cumulative count of assertion failures. */
190 static int failures = 0;
191 /* Cumulative count of reported skips. */
192 static int skips = 0;
193 /* Cumulative count of assertions checked. */
194 static int assertions = 0;
196 /* Directory where uuencoded reference files can be found. */
197 static const char *refdir;
200 * Report log information selectively to console and/or disk log.
202 static int log_console = 0;
203 static FILE *logfile;
205 vlogprintf(const char *fmt, va_list ap)
212 vfprintf(stdout, fmt, ap);
215 vfprintf(logfile, fmt, lfap);
218 vfprintf(logfile, fmt, ap);
223 logprintf(const char *fmt, ...)
231 /* Set up a message to display only if next assertion fails. */
232 static char msgbuff[4096];
233 static const char *msg, *nextmsg;
235 failure(const char *fmt, ...)
239 vsprintf(msgbuff, fmt, ap);
245 * Copy arguments into file-local variables.
246 * This was added to permit vararg assert() functions without needing
247 * variadic wrapper macros. Turns out that the vararg capability is almost
248 * never used, so almost all of the vararg assertions can be simplified
249 * by removing the vararg capability and reworking the wrapper macro to
250 * pass __FILE__, __LINE__ directly into the function instead of using
251 * this hook. I suspect this machinery is used so rarely that we
252 * would be better off just removing it entirely. That would simplify
253 * the code here noticably.
255 static const char *test_filename;
256 static int test_line;
257 static void *test_extra;
258 void assertion_setup(const char *filename, int line)
260 test_filename = filename;
264 /* Called at the beginning of each assert() function. */
266 assertion_count(const char *file, int line)
268 (void)file; /* UNUSED */
269 (void)line; /* UNUSED */
271 /* Proper handling of "failure()" message. */
274 /* Uncomment to print file:line after every assertion.
275 * Verbose, but occasionally useful in tracking down crashes. */
276 /* printf("Checked %s:%d\n", file, line); */
280 * For each test source file, we remember how many times each
281 * assertion was reported. Cleared before each new test,
282 * used by test_summarize().
287 } failed_lines[10000];
289 /* Count this failure, setup up log destination and handle initial report. */
291 failure_start(const char *filename, int line, const char *fmt, ...)
295 /* Record another failure for this line. */
297 /* test_filename = filename; */
298 failed_lines[line].count++;
300 /* Determine whether to log header to console. */
302 case VERBOSITY_LIGHT_REPORT:
303 log_console = (failed_lines[line].count < 2);
306 log_console = (verbosity >= VERBOSITY_FULL);
309 /* Log file:line header for this failure */
312 logprintf("%s(%d): ", filename, line);
314 logprintf("%s:%d: ", filename, line);
320 if (msg != NULL && msg[0] != '\0') {
321 logprintf(" Description: %s\n", msg);
325 /* Determine whether to log details to console. */
326 if (verbosity == VERBOSITY_LIGHT_REPORT)
330 /* Complete reporting of failed tests. */
332 * The 'extra' hook here is used by libarchive to include libarchive
333 * error messages with assertion failures. It could also be used
334 * to add strerror() output, for example. Just define the EXTRA_DUMP()
335 * macro appropriately.
338 failure_finish(void *extra)
340 (void)extra; /* UNUSED (maybe) */
343 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
346 if (dump_on_failure) {
348 " *** forcing core dump so failure can be debugged ***\n");
354 /* Inform user that we're skipping some checks. */
356 test_skipping(const char *fmt, ...)
362 vsprintf(buff, fmt, ap);
364 /* failure_start() isn't quite right, but is awfully convenient. */
365 failure_start(test_filename, test_line, "SKIPPING: %s", buff);
366 --failures; /* Undo failures++ in failure_start() */
367 /* Don't failure_finish() here. */
368 /* Mark as skip, so doesn't count as failed test. */
369 failed_lines[test_line].skip = 1;
379 /* Generic assert() just displays the failed condition. */
381 assertion_assert(const char *file, int line, int value,
382 const char *condition, void *extra)
384 assertion_count(file, line);
386 failure_start(file, line, "Assertion failed: %s", condition);
387 failure_finish(extra);
392 /* chdir() and report any errors */
394 assertion_chdir(const char *file, int line, const char *pathname)
396 assertion_count(file, line);
397 if (chdir(pathname) == 0)
399 failure_start(file, line, "chdir(\"%s\")", pathname);
400 failure_finish(NULL);
405 /* Verify two integers are equal. */
407 assertion_equal_int(const char *file, int line,
408 long long v1, const char *e1, long long v2, const char *e2, void *extra)
410 assertion_count(file, line);
413 failure_start(file, line, "%s != %s", e1, e2);
414 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
415 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
416 failure_finish(extra);
420 static void strdump(const char *e, const char *p)
424 logprintf(" %s = ", e);
431 unsigned int c = 0xff & *p++;
433 case '\a': printf("\a"); break;
434 case '\b': printf("\b"); break;
435 case '\n': printf("\n"); break;
436 case '\r': printf("\r"); break;
438 if (c >= 32 && c < 127)
441 logprintf("\\x%02X", c);
445 logprintf(" (length %d)\n", q == NULL ? -1 : (int)strlen(q));
448 /* Verify two strings are equal, dump them if not. */
450 assertion_equal_string(const char *file, int line,
451 const char *v1, const char *e1,
452 const char *v2, const char *e2,
455 assertion_count(file, line);
456 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
458 failure_start(file, line, "%s != %s", e1, e2);
461 failure_finish(extra);
466 wcsdump(const char *e, const wchar_t *w)
468 logprintf(" %s = ", e);
474 while (*w != L'\0') {
475 unsigned int c = *w++;
476 if (c >= 32 && c < 127)
479 logprintf("\\x%02X", c);
480 else if (c < 0x10000)
481 logprintf("\\u%04X", c);
483 logprintf("\\U%08X", c);
490 wcscmp(const wchar_t *s1, const wchar_t *s2)
493 while (*s1 == *s2++) {
504 /* Verify that two wide strings are equal, dump them if not. */
506 assertion_equal_wstring(const char *file, int line,
507 const wchar_t *v1, const char *e1,
508 const wchar_t *v2, const char *e2,
511 assertion_count(file, line);
512 if (v1 == v2 || wcscmp(v1, v2) == 0)
514 failure_start(file, line, "%s != %s", e1, e2);
517 failure_finish(extra);
522 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
523 * any bytes in p that differ from ref will be highlighted with '_'
524 * before and after the hex value.
527 hexdump(const char *p, const char *ref, size_t l, size_t offset)
533 logprintf("(null)\n");
536 for(i=0; i < l; i+=16) {
537 logprintf("%04x", (unsigned)(i + offset));
539 for (j = 0; j < 16 && i + j < l; j++) {
540 if (ref != NULL && p[i + j] != ref[i + j])
542 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
543 if (ref != NULL && p[i + j] == ref[i + j])
546 for (; j < 16; j++) {
547 logprintf("%c ", sep);
550 logprintf("%c", sep);
551 for (j=0; j < 16 && i + j < l; j++) {
553 if (c >= ' ' && c <= 126)
562 /* Verify that two blocks of memory are the same, display the first
563 * block of differences if they're not. */
565 assertion_equal_mem(const char *file, int line,
566 const void *_v1, const char *e1,
567 const void *_v2, const char *e2,
568 size_t l, const char *ld, void *extra)
570 const char *v1 = (const char *)_v1;
571 const char *v2 = (const char *)_v2;
574 assertion_count(file, line);
575 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
578 failure_start(file, line, "%s != %s", e1, e2);
579 logprintf(" size %s = %d\n", ld, (int)l);
580 /* Dump 48 bytes (3 lines) so that the first difference is
581 * in the second line. */
583 while (l > 64 && memcmp(v1, v2, 32) == 0) {
584 /* Two lines agree, so step forward one line. */
590 logprintf(" Dump of %s\n", e1);
591 hexdump(v1, v2, l < 64 ? l : 64, offset);
592 logprintf(" Dump of %s\n", e2);
593 hexdump(v2, v1, l < 64 ? l : 64, offset);
595 failure_finish(extra);
599 /* Verify that the named file exists and is empty. */
601 assertion_empty_file(const char *f1fmt, ...)
610 assertion_count(test_filename, test_line);
612 vsprintf(f1, f1fmt, ap);
615 if (stat(f1, &st) != 0) {
616 failure_start(test_filename, test_line, "Stat failed: %s", f1);
617 failure_finish(NULL);
623 failure_start(test_filename, test_line, "File should be empty: %s", f1);
624 logprintf(" File size: %d\n", (int)st.st_size);
625 logprintf(" Contents:\n");
628 logprintf(" Unable to open %s\n", f1);
630 s = ((off_t)sizeof(buff) < st.st_size) ?
631 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
632 s = fread(buff, 1, s, f);
633 hexdump(buff, NULL, s, 0);
636 failure_finish(NULL);
640 /* Verify that the named file exists and is not empty. */
642 assertion_non_empty_file(const char *f1fmt, ...)
648 assertion_count(test_filename, test_line);
650 vsprintf(f1, f1fmt, ap);
653 if (stat(f1, &st) != 0) {
654 failure_start(test_filename, test_line, "Stat failed: %s", f1);
655 failure_finish(NULL);
658 if (st.st_size == 0) {
659 failure_start(test_filename, test_line, "File empty: %s", f1);
660 failure_finish(NULL);
666 /* Verify that two files have the same contents. */
667 /* TODO: hexdump the first bytes that actually differ. */
669 assertion_equal_file(const char *fn1, const char *f2pattern, ...)
678 assertion_count(test_filename, test_line);
679 va_start(ap, f2pattern);
680 vsprintf(fn2, f2pattern, ap);
683 f1 = fopen(fn1, "rb");
684 f2 = fopen(fn2, "rb");
686 n1 = fread(buff1, 1, sizeof(buff1), f1);
687 n2 = fread(buff2, 1, sizeof(buff2), f2);
690 if (n1 == 0 && n2 == 0) {
695 if (memcmp(buff1, buff2, n1) != 0)
700 failure_start(test_filename, test_line, "Files not identical");
701 logprintf(" file1=\"%s\"\n", fn1);
702 logprintf(" file2=\"%s\"\n", fn2);
703 failure_finish(test_extra);
707 /* Verify that the named file does exist. */
709 assertion_file_exists(const char *fpattern, ...)
714 assertion_count(test_filename, test_line);
715 va_start(ap, fpattern);
716 vsprintf(f, fpattern, ap);
719 #if defined(_WIN32) && !defined(__CYGWIN__)
723 if (!access(f, F_OK))
726 failure_start(test_filename, test_line, "File should exist: %s", f);
727 failure_finish(test_extra);
731 /* Verify that the named file doesn't exist. */
733 assertion_file_not_exists(const char *fpattern, ...)
738 assertion_count(test_filename, test_line);
739 va_start(ap, fpattern);
740 vsprintf(f, fpattern, ap);
743 #if defined(_WIN32) && !defined(__CYGWIN__)
750 failure_start(test_filename, test_line, "File should not exist: %s", f);
751 failure_finish(test_extra);
755 /* Compare the contents of a file to a block of memory. */
757 assertion_file_contents(const void *buff, int s, const char *fpattern, ...)
765 assertion_count(test_filename, test_line);
766 va_start(ap, fpattern);
767 vsprintf(fn, fpattern, ap);
772 failure_start(test_filename, test_line,
773 "File should exist: %s", fn);
774 failure_finish(test_extra);
777 contents = malloc(s * 2);
778 n = fread(contents, 1, s * 2, f);
780 if (n == s && memcmp(buff, contents, s) == 0) {
784 failure_start(test_filename, test_line, "File contents don't match");
785 logprintf(" file=\"%s\"\n", fn);
787 hexdump(contents, buff, n > 512 ? 512 : n, 0);
789 logprintf(" File empty, contents should be:\n");
790 hexdump(buff, NULL, s > 512 ? 512 : n, 0);
792 failure_finish(test_extra);
797 /* Check the contents of a text file, being tolerant of line endings. */
799 assertion_text_file_contents(const char *buff, const char *fn)
802 const char *btxt, *ftxt;
806 assertion_count(test_filename, test_line);
809 contents = malloc(s * 2 + 128);
810 n = fread(contents, 1, s * 2 + 128 - 1, f);
816 ftxt = (const char *)contents;
817 while (*btxt != '\0' && *ftxt != '\0') {
818 if (*btxt == *ftxt) {
823 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
824 /* Pass over different new line characters. */
831 if (*btxt == '\0' && *ftxt == '\0') {
835 failure_start(test_filename, test_line, "Contents don't match");
836 logprintf(" file=\"%s\"\n", fn);
838 hexdump(contents, buff, n, 0);
840 logprintf(" File empty, contents should be:\n");
841 hexdump(buff, NULL, s, 0);
843 failure_finish(test_extra);
848 /* Test that two paths point to the same file. */
849 /* As a side-effect, asserts that both files exist. */
851 is_hardlink(const char *file, int line,
852 const char *path1, const char *path2)
854 #if defined(_WIN32) && !defined(__CYGWIN__)
855 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
858 assertion_count(file, line);
859 r = my_GetFileInformationByName(path1, &bhfi1);
861 failure_start(file, line, "File %s can't be inspected?", path1);
862 failure_finish(NULL);
865 r = my_GetFileInformationByName(path2, &bhfi2);
867 failure_start(file, line, "File %s can't be inspected?", path2);
868 failure_finish(NULL);
871 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
872 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
873 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
875 struct stat st1, st2;
878 assertion_count(file, line);
879 r = lstat(path1, &st1);
881 failure_start(file, line, "File should exist: %s", path1);
882 failure_finish(NULL);
885 r = lstat(path2, &st2);
887 failure_start(file, line, "File should exist: %s", path2);
888 failure_finish(NULL);
891 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
896 assertion_is_hardlink(const char *file, int line,
897 const char *path1, const char *path2)
899 if (is_hardlink(file, line, path1, path2))
901 failure_start(file, line,
902 "Files %s and %s are not hardlinked", path1, path2);
903 failure_finish(NULL);
908 assertion_is_not_hardlink(const char *file, int line,
909 const char *path1, const char *path2)
911 if (!is_hardlink(file, line, path1, path2))
913 failure_start(file, line,
914 "Files %s and %s should not be hardlinked", path1, path2);
915 failure_finish(NULL);
919 /* Verify a/b/mtime of 'pathname'. */
920 /* If 'recent', verify that it's within last 10 seconds. */
922 assertion_file_time(const char *file, int line,
923 const char *pathname, long t, long nsec, char type, int recent)
925 long long filet, filet_nsec;
928 #if defined(_WIN32) && !defined(__CYGWIN__)
929 #define EPOC_TIME (116444736000000000ULL)
930 FILETIME ftime, fbirthtime, fatime, fmtime;
931 ULARGE_INTEGER wintm;
933 ftime.dwLowDateTime = 0;
934 ftime.dwHighDateTime = 0;
936 assertion_count(file, line);
937 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
938 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
939 if (h == INVALID_HANDLE_VALUE) {
940 failure_start(file, line, "Can't access %s\n", pathname);
941 failure_finish(NULL);
944 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
946 case 'a': ftime = fatime; break;
947 case 'b': ftime = fbirthtime; break;
948 case 'm': ftime = fmtime; break;
952 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
953 failure_finish(NULL);
956 wintm.LowPart = ftime.dwLowDateTime;
957 wintm.HighPart = ftime.dwHighDateTime;
958 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
959 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
960 nsec = (nsec / 100) * 100; /* Round the request */
964 assertion_count(file, line);
965 r = lstat(pathname, &st);
967 failure_start(file, line, "Can't stat %s\n", pathname);
968 failure_finish(NULL);
972 case 'a': filet = st.st_atime; break;
973 case 'm': filet = st.st_mtime; break;
974 case 'b': filet = 0; break;
975 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
978 #if defined(__FreeBSD__)
980 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
981 case 'b': filet = st.st_birthtime;
982 filet_nsec = st.st_birthtimespec.tv_nsec; break;
983 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
984 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
987 /* FreeBSD generally only stores to microsecond res, so round. */
988 filet_nsec = (filet_nsec / 1000) * 1000;
989 nsec = (nsec / 1000) * 1000;
991 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
992 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
993 #if defined(__HAIKU__)
994 if (type == 'a') return (1); /* Haiku doesn't have atime. */
999 /* Check that requested time is up-to-date. */
1000 time_t now = time(NULL);
1001 if (filet < now - 10 || filet > now + 1) {
1002 failure_start(file, line,
1003 "File %s has %ctime %ld, %ld seconds ago\n",
1004 pathname, type, filet, now - filet);
1005 failure_finish(NULL);
1008 } else if (filet != t || filet_nsec != nsec) {
1009 failure_start(file, line,
1010 "File %s has %ctime %ld.%09ld, expected %ld.%09ld",
1011 pathname, type, filet, filet_nsec, t, nsec);
1012 failure_finish(NULL);
1018 /* Verify atime of 'pathname'. */
1020 assertion_file_atime(const char *file, int line,
1021 const char *pathname, long t, long nsec)
1023 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1026 /* Verify atime of 'pathname' is up-to-date. */
1028 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1030 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1033 /* Verify birthtime of 'pathname'. */
1035 assertion_file_birthtime(const char *file, int line,
1036 const char *pathname, long t, long nsec)
1038 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1041 /* Verify birthtime of 'pathname' is up-to-date. */
1043 assertion_file_birthtime_recent(const char *file, int line,
1044 const char *pathname)
1046 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1049 /* Verify mtime of 'pathname'. */
1051 assertion_file_mtime(const char *file, int line,
1052 const char *pathname, long t, long nsec)
1054 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1057 /* Verify mtime of 'pathname' is up-to-date. */
1059 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1061 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1064 /* Verify number of links to 'pathname'. */
1066 assertion_file_nlinks(const char *file, int line,
1067 const char *pathname, int nlinks)
1069 #if defined(_WIN32) && !defined(__CYGWIN__)
1070 BY_HANDLE_FILE_INFORMATION bhfi;
1073 assertion_count(file, line);
1074 r = my_GetFileInformationByName(pathname, &bhfi);
1075 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1077 failure_start(file, line, "File %s has %d links, expected %d",
1078 pathname, bhfi.nNumberOfLinks, nlinks);
1079 failure_finish(NULL);
1085 assertion_count(file, line);
1086 r = lstat(pathname, &st);
1087 if (r == 0 && st.st_nlink == nlinks)
1089 failure_start(file, line, "File %s has %d links, expected %d",
1090 pathname, st.st_nlink, nlinks);
1091 failure_finish(NULL);
1096 /* Verify size of 'pathname'. */
1098 assertion_file_size(const char *file, int line, const char *pathname, long size)
1103 assertion_count(file, line);
1104 #if defined(_WIN32) && !defined(__CYGWIN__)
1106 BY_HANDLE_FILE_INFORMATION bhfi;
1107 r = !my_GetFileInformationByName(pathname, &bhfi);
1108 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1113 r = lstat(pathname, &st);
1114 filesize = st.st_size;
1117 if (r == 0 && filesize == size)
1119 failure_start(file, line, "File %s has size %ld, expected %ld",
1120 pathname, (long)filesize, (long)size);
1121 failure_finish(NULL);
1125 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1127 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1132 #if defined(_WIN32) && !defined(__CYGWIN__)
1133 (void)mode; /* UNUSED */
1135 assertion_count(file, line);
1136 r = lstat(pathname, &st);
1138 failure_start(file, line, "Dir should exist: %s", pathname);
1139 failure_finish(NULL);
1142 if (!S_ISDIR(st.st_mode)) {
1143 failure_start(file, line, "%s is not a dir", pathname);
1144 failure_finish(NULL);
1147 #if !defined(_WIN32) || defined(__CYGWIN__)
1148 /* Windows doesn't handle permissions the same way as POSIX,
1149 * so just ignore the mode tests. */
1150 /* TODO: Can we do better here? */
1151 if (mode >= 0 && mode != (st.st_mode & 07777)) {
1152 failure_start(file, line, "Dir %s has wrong mode", pathname);
1153 logprintf(" Expected: 0%3o\n", mode);
1154 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1155 failure_finish(NULL);
1162 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1163 * verify that too. */
1165 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1170 #if defined(_WIN32) && !defined(__CYGWIN__)
1171 (void)mode; /* UNUSED */
1173 assertion_count(file, line);
1174 r = lstat(pathname, &st);
1175 if (r != 0 || !S_ISREG(st.st_mode)) {
1176 failure_start(file, line, "File should exist: %s", pathname);
1177 failure_finish(NULL);
1180 #if !defined(_WIN32) || defined(__CYGWIN__)
1181 /* Windows doesn't handle permissions the same way as POSIX,
1182 * so just ignore the mode tests. */
1183 /* TODO: Can we do better here? */
1184 if (mode >= 0 && mode != (st.st_mode & 07777)) {
1185 failure_start(file, line, "File %s has wrong mode", pathname);
1186 logprintf(" Expected: 0%3o\n", mode);
1187 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1188 failure_finish(NULL);
1195 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1196 * non-NULL, verify that the symlink has those contents. */
1198 is_symlink(const char *file, int line,
1199 const char *pathname, const char *contents)
1201 #if defined(_WIN32) && !defined(__CYGWIN__)
1202 (void)pathname; /* UNUSED */
1203 (void)contents; /* UNUSED */
1204 assertion_count(file, line);
1205 /* Windows sort-of has real symlinks, but they're only usable
1206 * by privileged users and are crippled even then, so there's
1207 * really not much point in bothering with this. */
1215 assertion_count(file, line);
1216 r = lstat(pathname, &st);
1218 failure_start(file, line,
1219 "Symlink should exist: %s", pathname);
1220 failure_finish(NULL);
1223 if (!S_ISLNK(st.st_mode))
1225 if (contents == NULL)
1227 linklen = readlink(pathname, buff, sizeof(buff));
1229 failure_start(file, line, "Can't read symlink %s", pathname);
1230 failure_finish(NULL);
1233 buff[linklen] = '\0';
1234 if (strcmp(buff, contents) != 0)
1240 /* Assert that path is a symlink that (optionally) contains contents. */
1242 assertion_is_symlink(const char *file, int line,
1243 const char *path, const char *contents)
1245 if (is_symlink(file, line, path, contents))
1248 failure_start(file, line, "File %s is not a symlink to %s",
1251 failure_start(file, line, "File %s is not a symlink", path);
1252 failure_finish(NULL);
1257 /* Create a directory and report any errors. */
1259 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1261 assertion_count(file, line);
1262 #if defined(_WIN32) && !defined(__CYGWIN__)
1263 (void)mode; /* UNUSED */
1264 if (0 == _mkdir(dirname))
1267 if (0 == mkdir(dirname, mode))
1270 failure_start(file, line, "Could not create directory %s", dirname);
1271 failure_finish(NULL);
1275 /* Create a file with the specified contents and report any failures. */
1277 assertion_make_file(const char *file, int line,
1278 const char *path, int mode, const char *contents)
1280 #if defined(_WIN32) && !defined(__CYGWIN__)
1281 /* TODO: Rework this to set file mode as well. */
1283 (void)mode; /* UNUSED */
1284 assertion_count(file, line);
1285 f = fopen(path, "wb");
1287 failure_start(file, line, "Could not create file %s", path);
1288 failure_finish(NULL);
1291 if (contents != NULL) {
1292 if (strlen(contents)
1293 != fwrite(contents, 1, strlen(contents), f)) {
1295 failure_start(file, line,
1296 "Could not write file %s", path);
1297 failure_finish(NULL);
1305 assertion_count(file, line);
1306 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1308 failure_start(file, line, "Could not create %s", path);
1309 failure_finish(NULL);
1312 if (contents != NULL) {
1313 if ((ssize_t)strlen(contents)
1314 != write(fd, contents, strlen(contents))) {
1316 failure_start(file, line, "Could not write to %s", path);
1317 failure_finish(NULL);
1326 /* Create a hardlink and report any failures. */
1328 assertion_make_hardlink(const char *file, int line,
1329 const char *newpath, const char *linkto)
1333 assertion_count(file, line);
1334 #if defined(_WIN32) && !defined(__CYGWIN__)
1335 succeeded = my_CreateHardLinkA(newpath, linkto);
1337 succeeded = !link(linkto, newpath);
1343 failure_start(file, line, "Could not create hardlink");
1344 logprintf(" New link: %s\n", newpath);
1345 logprintf(" Old name: %s\n", linkto);
1346 failure_finish(NULL);
1350 /* Create a symlink and report any failures. */
1352 assertion_make_symlink(const char *file, int line,
1353 const char *newpath, const char *linkto)
1355 #if defined(_WIN32) && !defined(__CYGWIN__)
1356 int targetIsDir = 0; /* TODO: Fix this */
1357 assertion_count(file, line);
1358 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1361 assertion_count(file, line);
1362 if (0 == symlink(linkto, newpath))
1365 failure_start(file, line, "Could not create symlink");
1366 logprintf(" New link: %s\n", newpath);
1367 logprintf(" Old name: %s\n", linkto);
1368 failure_finish(NULL);
1372 /* Set umask, report failures. */
1374 assertion_umask(const char *file, int line, int mask)
1376 assertion_count(file, line);
1377 (void)file; /* UNUSED */
1378 (void)line; /* UNUSED */
1385 * UTILITIES for use by tests.
1390 * Check whether platform supports symlinks. This is intended
1391 * for tests to use in deciding whether to bother testing symlink
1392 * support; if the platform doesn't support symlinks, there's no point
1393 * in checking whether the program being tested can create them.
1395 * Note that the first time this test is called, we actually go out to
1396 * disk to create and verify a symlink. This is necessary because
1397 * symlink support is actually a property of a particular filesystem
1398 * and can thus vary between directories on a single system. After
1399 * the first call, this returns the cached result from memory, so it's
1400 * safe to call it as often as you wish.
1405 /* Remember the test result */
1406 static int value = 0, tested = 0;
1411 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, "a");
1412 /* Note: Cygwin has its own symlink() emulation that does not
1413 * use the Win32 CreateSymbolicLink() function. */
1414 #if defined(_WIN32) && !defined(__CYGWIN__)
1415 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1416 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1418 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1419 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1425 * Can this platform run the gzip program?
1427 /* Platform-dependent options for hiding the output of a subcommand. */
1428 #if defined(_WIN32) && !defined(__CYGWIN__)
1429 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1431 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1436 static int tested = 0, value = 0;
1439 if (systemf("gzip -V %s", redirectArgs) == 0)
1446 * Can this platform run the gunzip program?
1451 static int tested = 0, value = 0;
1454 if (systemf("gunzip -V %s", redirectArgs) == 0)
1461 * Sleep as needed; useful for verifying disk timestamp changes by
1462 * ensuring that the wall-clock time has actually changed before we
1463 * go back to re-read something from disk.
1466 sleepUntilAfter(time_t t)
1468 while (t >= time(NULL))
1469 #if defined(_WIN32) && !defined(__CYGWIN__)
1477 * Call standard system() call, but build up the command line using
1478 * sprintf() conventions.
1481 systemf(const char *fmt, ...)
1488 vsprintf(buff, fmt, ap);
1489 if (verbosity > VERBOSITY_FULL)
1490 logprintf("Cmd: %s\n", buff);
1497 * Slurp a file into memory for ease of comparison and testing.
1498 * Returns size of file in 'sizep' if non-NULL, null-terminates
1499 * data in memory for ease of use.
1502 slurpfile(size_t * sizep, const char *fmt, ...)
1504 char filename[8192];
1513 vsprintf(filename, fmt, ap);
1516 f = fopen(filename, "rb");
1518 /* Note: No error; non-existent file is okay here. */
1521 r = fstat(fileno(f), &st);
1523 logprintf("Can't stat file %s\n", filename);
1527 p = malloc((size_t)st.st_size + 1);
1529 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
1530 (long int)st.st_size, filename);
1534 bytes_read = fread(p, 1, (size_t)st.st_size, f);
1535 if (bytes_read < st.st_size) {
1536 logprintf("Can't read file %s\n", filename);
1541 p[st.st_size] = '\0';
1543 *sizep = (size_t)st.st_size;
1548 /* Read a uuencoded file from the reference directory, decode, and
1549 * write the result into the current directory. */
1550 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
1552 extract_reference_file(const char *name)
1557 sprintf(buff, "%s/%s.uu", refdir, name);
1558 in = fopen(buff, "r");
1559 failure("Couldn't open reference file %s", buff);
1563 /* Read up to and including the 'begin' line. */
1565 if (fgets(buff, sizeof(buff), in) == NULL) {
1566 /* TODO: This is a failure. */
1569 if (memcmp(buff, "begin ", 6) == 0)
1572 /* Now, decode the rest and write it. */
1573 /* Not a lot of error checking here; the input better be right. */
1574 out = fopen(name, "wb");
1575 while (fgets(buff, sizeof(buff), in) != NULL) {
1579 if (memcmp(buff, "end", 3) == 0)
1582 bytes = UUDECODE(*p++);
1585 /* Write out 1-3 bytes from that. */
1587 n = UUDECODE(*p++) << 18;
1588 n |= UUDECODE(*p++) << 12;
1589 fputc(n >> 16, out);
1593 n |= UUDECODE(*p++) << 6;
1594 fputc((n >> 8) & 0xFF, out);
1598 n |= UUDECODE(*p++);
1599 fputc(n & 0xFF, out);
1615 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
1617 * DEFINE_TEST(test_function)
1621 /* Use "list.h" to declare all of the test functions. */
1623 #define DEFINE_TEST(name) void name(void);
1626 /* Use "list.h" to create a list of all tests (functions and names). */
1628 #define DEFINE_TEST(n) { n, #n, 0 },
1629 struct { void (*func)(void); const char *name; int failures; } tests[] = {
1634 * Summarize repeated failures in the just-completed test.
1637 test_summarize(const char *filename, int failed)
1641 switch (verbosity) {
1642 case VERBOSITY_SUMMARY_ONLY:
1643 printf(failed ? "E" : ".");
1646 case VERBOSITY_PASSFAIL:
1647 printf(failed ? "FAIL\n" : "ok\n");
1651 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
1653 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
1654 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
1655 logprintf("%s:%d: Summary: Failed %d times\n",
1656 filename, i, failed_lines[i].count);
1658 /* Clear the failure history for the next file. */
1659 memset(failed_lines, 0, sizeof(failed_lines));
1663 * Actually run a single test, with appropriate setup and cleanup.
1666 test_run(int i, const char *tmpdir)
1668 char logfilename[64];
1669 int failures_before = failures;
1672 switch (verbosity) {
1673 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
1675 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
1676 printf("%3d: %-50s", i, tests[i].name);
1679 default: /* Title of test, details will follow */
1680 printf("%3d: %s\n", i, tests[i].name);
1683 /* Chdir to the top-level work directory. */
1684 if (!assertChdir(tmpdir)) {
1686 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
1689 /* Create a log file for this test. */
1690 sprintf(logfilename, "%s.log", tests[i].name);
1691 logfile = fopen(logfilename, "w");
1692 fprintf(logfile, "%s\n\n", tests[i].name);
1693 /* Chdir() to a work dir for this specific test. */
1694 if (!assertMakeDir(tests[i].name, 0755)
1695 || !assertChdir(tests[i].name)) {
1697 "ERROR: Can't chdir to work dir %s/%s\n",
1698 tmpdir, tests[i].name);
1701 /* Explicitly reset the locale before each test. */
1702 setlocale(LC_ALL, "C");
1703 /* Record the umask before we run the test. */
1704 umask(oldumask = umask(0));
1706 * Run the actual test.
1710 * Clean up and report afterwards.
1715 setlocale(LC_ALL, "C");
1716 /* Reset directory. */
1717 if (!assertChdir(tmpdir)) {
1718 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
1722 /* Report per-test summaries. */
1723 tests[i].failures = failures - failures_before;
1724 test_summarize(test_filename, tests[i].failures);
1725 /* Close the per-test log file. */
1728 /* If there were no failures, we can remove the work dir and logfile. */
1729 if (tests[i].failures == 0) {
1730 if (!keep_temp_files && assertChdir(tmpdir)) {
1731 #if defined(_WIN32) && !defined(__CYGWIN__)
1732 /* Make sure not to leave empty directories.
1733 * Sometimes a processing of closing files used by tests
1734 * is not done, then rmdir will be failed and it will
1735 * leave a empty test directory. So we should wait a few
1736 * seconds and retry rmdir. */
1738 for (t = 0; t < 10; t++) {
1741 r = systemf("rmdir /S /Q %s", tests[i].name);
1745 systemf("del %s", logfilename);
1747 systemf("rm -rf %s", tests[i].name);
1748 systemf("rm %s", logfilename);
1752 /* Return appropriate status. */
1753 return (tests[i].failures);
1759 * MAIN and support routines.
1765 usage(const char *program)
1767 static const int limit = sizeof(tests) / sizeof(tests[0]);
1770 printf("Usage: %s [options] <test> <test> ...\n", program);
1771 printf("Default is to run all tests.\n");
1772 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
1773 printf("Options:\n");
1774 printf(" -d Dump core after any failure, for debugging.\n");
1775 printf(" -k Keep all temp files.\n");
1776 printf(" Default: temp files for successful tests deleted.\n");
1778 printf(" -p <path> Path to executable to be tested.\n");
1779 printf(" Default: path taken from " ENVBASE " environment variable.\n");
1781 printf(" -q Quiet.\n");
1782 printf(" -r <dir> Path to dir containing reference files.\n");
1783 printf(" Default: Current directory.\n");
1784 printf(" -v Verbose.\n");
1785 printf("Available tests:\n");
1786 for (i = 0; i < limit; i++)
1787 printf(" %d: %s\n", i, tests[i].name);
1792 get_refdir(const char *d)
1794 char tried[512] = { '\0' };
1798 /* If a dir was specified, try that */
1801 snprintf(buff, sizeof(buff), "%s", d);
1802 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1803 if (p != NULL) goto success;
1804 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1805 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1809 /* Get the current dir. */
1810 pwd = getcwd(NULL, 0);
1811 while (pwd[strlen(pwd) - 1] == '\n')
1812 pwd[strlen(pwd) - 1] = '\0';
1814 /* Look for a known file. */
1815 snprintf(buff, sizeof(buff), "%s", pwd);
1816 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1817 if (p != NULL) goto success;
1818 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1819 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1821 snprintf(buff, sizeof(buff), "%s/test", pwd);
1822 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1823 if (p != NULL) goto success;
1824 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1825 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1827 #if defined(LIBRARY)
1828 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
1830 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
1832 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1833 if (p != NULL) goto success;
1834 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1835 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1837 if (memcmp(pwd, "/usr/obj", 8) == 0) {
1838 snprintf(buff, sizeof(buff), "%s", pwd + 8);
1839 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1840 if (p != NULL) goto success;
1841 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1842 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1844 snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
1845 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1846 if (p != NULL) goto success;
1847 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1848 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1852 printf("Unable to locate known reference file %s\n", KNOWNREF);
1853 printf(" Checked following directories:\n%s\n", tried);
1854 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
1862 return strdup(buff);
1866 main(int argc, char **argv)
1868 static const int limit = sizeof(tests) / sizeof(tests[0]);
1869 int i, tests_run = 0, tests_failed = 0, option;
1871 char *refdir_alloc = NULL;
1872 const char *progname;
1873 const char *tmp, *option_arg, *p;
1875 char tmpdir_timestamp[256];
1877 (void)argc; /* UNUSED */
1879 #if defined(HAVE__CrtSetReportMode)
1880 /* To stop to run the default invalid parameter handler. */
1881 _set_invalid_parameter_handler(invalid_parameter_handler);
1882 /* Disable annoying assertion message box. */
1883 _CrtSetReportMode(_CRT_ASSERT, 0);
1887 * Name of this program, used to build root of our temp directory
1890 progname = p = argv[0];
1891 while (*p != '\0') {
1892 /* Support \ or / dir separators for Windows compat. */
1893 if (*p == '/' || *p == '\\')
1899 /* Get the target program from environment, if available. */
1900 testprogfile = getenv(ENVBASE);
1903 if (getenv("TMPDIR") != NULL)
1904 tmp = getenv("TMPDIR");
1905 else if (getenv("TMP") != NULL)
1906 tmp = getenv("TMP");
1907 else if (getenv("TEMP") != NULL)
1908 tmp = getenv("TEMP");
1909 else if (getenv("TEMPDIR") != NULL)
1910 tmp = getenv("TEMPDIR");
1914 /* Allow -d to be controlled through the environment. */
1915 if (getenv(ENVBASE "_DEBUG") != NULL)
1916 dump_on_failure = 1;
1918 /* Get the directory holding test files from environment. */
1919 refdir = getenv(ENVBASE "_TEST_FILES");
1922 * Parse options, without using getopt(), which isn't available
1925 ++argv; /* Skip program name */
1926 while (*argv != NULL) {
1931 while (*p != '\0') {
1934 /* If 'opt' takes an argument, parse that. */
1935 if (option == 'p' || option == 'r') {
1938 else if (*argv == NULL) {
1940 "Option -%c requires argument.\n",
1944 option_arg = *argv++;
1945 p = ""; /* End of this option word. */
1948 /* Now, handle the option. */
1951 dump_on_failure = 1;
1954 keep_temp_files = 1;
1958 testprogfile = option_arg;
1967 refdir = option_arg;
1979 * Sanity-check that our options make sense.
1982 if (testprogfile == NULL)
1986 #if defined(_WIN32) && !defined(__CYGWIN__)
1987 /* Command.com sometimes rejects '/' separators. */
1988 testprg = strdup(testprogfile);
1989 for (i = 0; testprg[i] != '\0'; i++) {
1990 if (testprg[i] == '/')
1993 testprogfile = testprg;
1995 /* Quote the name that gets put into shell command lines. */
1996 testprg = malloc(strlen(testprogfile) + 3);
1997 strcpy(testprg, "\"");
1998 strcat(testprg, testprogfile);
1999 strcat(testprg, "\"");
2005 * Create a temp directory for the following tests.
2006 * Include the time the tests started as part of the name,
2007 * to make it easier to track the results of multiple tests.
2010 for (i = 0; ; i++) {
2011 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2012 "%Y-%m-%dT%H.%M.%S",
2014 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2015 tmpdir_timestamp, i);
2016 if (assertMakeDir(tmpdir,0755))
2020 "ERROR: Unable to create temp directory %s\n",
2027 * If the user didn't specify a directory for locating
2028 * reference files, try to find the reference files in
2029 * the "usual places."
2031 refdir = refdir_alloc = get_refdir(refdir);
2034 * Banner with basic information.
2037 printf("If tests fail or crash, details will be in:\n");
2038 printf(" %s\n", tmpdir);
2040 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2041 printf("Reference files will be read from: %s\n", refdir);
2043 printf("Running tests on: %s\n", testprog);
2045 printf("Exercising: ");
2047 printf("%s\n", EXTRA_VERSION);
2054 * Run some or all of the individual tests.
2056 if (*argv == NULL) {
2057 /* Default: Run all tests. */
2058 for (i = 0; i < limit; i++) {
2059 if (test_run(i, tmpdir))
2064 while (*(argv) != NULL) {
2065 if (**argv >= '0' && **argv <= '9') {
2067 if (i < 0 || i >= limit) {
2068 printf("*** INVALID Test %s\n", *argv);
2071 /* usage() never returns */
2074 for (i = 0; i < limit; ++i) {
2075 if (strcmp(*argv, tests[i].name) == 0)
2079 printf("*** INVALID Test ``%s''\n",
2083 /* usage() never returns */
2086 if (test_run(i, tmpdir))
2094 * Report summary statistics.
2096 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2098 printf("Totals:\n");
2099 printf(" Tests run: %8d\n", tests_run);
2100 printf(" Tests failed: %8d\n", tests_failed);
2101 printf(" Assertions checked:%8d\n", assertions);
2102 printf(" Assertions failed: %8d\n", failures);
2103 printf(" Skips reported: %8d\n", skips);
2107 printf("Failing tests:\n");
2108 for (i = 0; i < limit; ++i) {
2109 if (tests[i].failures)
2110 printf(" %d: %s (%d failures)\n", i,
2111 tests[i].name, tests[i].failures);
2114 printf("Details for failing tests: %s\n", tmpdir);
2117 if (verbosity == VERBOSITY_SUMMARY_ONLY)
2119 printf("%d tests passed, no failures\n", tests_run);
2124 /* If the final tmpdir is empty, we can remove it. */
2125 /* This should be the usual case when all tests succeed. */
2129 return (tests_failed ? 1 : 0);