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);
488 /* Verify that two wide strings are equal, dump them if not. */
490 assertion_equal_wstring(const char *file, int line,
491 const wchar_t *v1, const char *e1,
492 const wchar_t *v2, const char *e2,
495 assertion_count(file, line);
496 if (v1 == v2 || wcscmp(v1, v2) == 0)
498 failure_start(file, line, "%s != %s", e1, e2);
501 failure_finish(extra);
506 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
507 * any bytes in p that differ from ref will be highlighted with '_'
508 * before and after the hex value.
511 hexdump(const char *p, const char *ref, size_t l, size_t offset)
517 logprintf("(null)\n");
520 for(i=0; i < l; i+=16) {
521 logprintf("%04x", (unsigned)(i + offset));
523 for (j = 0; j < 16 && i + j < l; j++) {
524 if (ref != NULL && p[i + j] != ref[i + j])
526 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
527 if (ref != NULL && p[i + j] == ref[i + j])
530 for (; j < 16; j++) {
531 logprintf("%c ", sep);
534 logprintf("%c", sep);
535 for (j=0; j < 16 && i + j < l; j++) {
537 if (c >= ' ' && c <= 126)
546 /* Verify that two blocks of memory are the same, display the first
547 * block of differences if they're not. */
549 assertion_equal_mem(const char *file, int line,
550 const void *_v1, const char *e1,
551 const void *_v2, const char *e2,
552 size_t l, const char *ld, void *extra)
554 const char *v1 = (const char *)_v1;
555 const char *v2 = (const char *)_v2;
558 assertion_count(file, line);
559 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
562 failure_start(file, line, "%s != %s", e1, e2);
563 logprintf(" size %s = %d\n", ld, (int)l);
564 /* Dump 48 bytes (3 lines) so that the first difference is
565 * in the second line. */
567 while (l > 64 && memcmp(v1, v2, 32) == 0) {
568 /* Two lines agree, so step forward one line. */
574 logprintf(" Dump of %s\n", e1);
575 hexdump(v1, v2, l < 64 ? l : 64, offset);
576 logprintf(" Dump of %s\n", e2);
577 hexdump(v2, v1, l < 64 ? l : 64, offset);
579 failure_finish(extra);
583 /* Verify that the named file exists and is empty. */
585 assertion_empty_file(const char *f1fmt, ...)
594 assertion_count(test_filename, test_line);
596 vsprintf(f1, f1fmt, ap);
599 if (stat(f1, &st) != 0) {
600 failure_start(test_filename, test_line, "Stat failed: %s", f1);
601 failure_finish(NULL);
607 failure_start(test_filename, test_line, "File should be empty: %s", f1);
608 logprintf(" File size: %d\n", (int)st.st_size);
609 logprintf(" Contents:\n");
612 logprintf(" Unable to open %s\n", f1);
614 s = ((off_t)sizeof(buff) < st.st_size) ?
615 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
616 s = fread(buff, 1, s, f);
617 hexdump(buff, NULL, s, 0);
620 failure_finish(NULL);
624 /* Verify that the named file exists and is not empty. */
626 assertion_non_empty_file(const char *f1fmt, ...)
632 assertion_count(test_filename, test_line);
634 vsprintf(f1, f1fmt, ap);
637 if (stat(f1, &st) != 0) {
638 failure_start(test_filename, test_line, "Stat failed: %s", f1);
639 failure_finish(NULL);
642 if (st.st_size == 0) {
643 failure_start(test_filename, test_line, "File empty: %s", f1);
644 failure_finish(NULL);
650 /* Verify that two files have the same contents. */
651 /* TODO: hexdump the first bytes that actually differ. */
653 assertion_equal_file(const char *fn1, const char *f2pattern, ...)
662 assertion_count(test_filename, test_line);
663 va_start(ap, f2pattern);
664 vsprintf(fn2, f2pattern, ap);
667 f1 = fopen(fn1, "rb");
668 f2 = fopen(fn2, "rb");
670 n1 = fread(buff1, 1, sizeof(buff1), f1);
671 n2 = fread(buff2, 1, sizeof(buff2), f2);
674 if (n1 == 0 && n2 == 0) {
679 if (memcmp(buff1, buff2, n1) != 0)
684 failure_start(test_filename, test_line, "Files not identical");
685 logprintf(" file1=\"%s\"\n", fn1);
686 logprintf(" file2=\"%s\"\n", fn2);
687 failure_finish(test_extra);
691 /* Verify that the named file does exist. */
693 assertion_file_exists(const char *fpattern, ...)
698 assertion_count(test_filename, test_line);
699 va_start(ap, fpattern);
700 vsprintf(f, fpattern, ap);
703 #if defined(_WIN32) && !defined(__CYGWIN__)
707 if (!access(f, F_OK))
710 failure_start(test_filename, test_line, "File should exist: %s", f);
711 failure_finish(test_extra);
715 /* Verify that the named file doesn't exist. */
717 assertion_file_not_exists(const char *fpattern, ...)
722 assertion_count(test_filename, test_line);
723 va_start(ap, fpattern);
724 vsprintf(f, fpattern, ap);
727 #if defined(_WIN32) && !defined(__CYGWIN__)
734 failure_start(test_filename, test_line, "File should not exist: %s", f);
735 failure_finish(test_extra);
739 /* Compare the contents of a file to a block of memory. */
741 assertion_file_contents(const void *buff, int s, const char *fpattern, ...)
749 assertion_count(test_filename, test_line);
750 va_start(ap, fpattern);
751 vsprintf(fn, fpattern, ap);
756 failure_start(test_filename, test_line,
757 "File should exist: %s", fn);
758 failure_finish(test_extra);
761 contents = malloc(s * 2);
762 n = fread(contents, 1, s * 2, f);
764 if (n == s && memcmp(buff, contents, s) == 0) {
768 failure_start(test_filename, test_line, "File contents don't match");
769 logprintf(" file=\"%s\"\n", fn);
771 hexdump(contents, buff, n > 512 ? 512 : n, 0);
773 logprintf(" File empty, contents should be:\n");
774 hexdump(buff, NULL, s > 512 ? 512 : n, 0);
776 failure_finish(test_extra);
781 /* Check the contents of a text file, being tolerant of line endings. */
783 assertion_text_file_contents(const char *buff, const char *fn)
786 const char *btxt, *ftxt;
790 assertion_count(test_filename, test_line);
793 contents = malloc(s * 2 + 128);
794 n = fread(contents, 1, s * 2 + 128 - 1, f);
800 ftxt = (const char *)contents;
801 while (*btxt != '\0' && *ftxt != '\0') {
802 if (*btxt == *ftxt) {
807 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
808 /* Pass over different new line characters. */
815 if (*btxt == '\0' && *ftxt == '\0') {
819 failure_start(test_filename, test_line, "Contents don't match");
820 logprintf(" file=\"%s\"\n", fn);
822 hexdump(contents, buff, n, 0);
824 logprintf(" File empty, contents should be:\n");
825 hexdump(buff, NULL, s, 0);
827 failure_finish(test_extra);
832 /* Test that two paths point to the same file. */
833 /* As a side-effect, asserts that both files exist. */
835 is_hardlink(const char *file, int line,
836 const char *path1, const char *path2)
838 #if defined(_WIN32) && !defined(__CYGWIN__)
839 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
842 assertion_count(file, line);
843 r = my_GetFileInformationByName(path1, &bhfi1);
845 failure_start(file, line, "File %s can't be inspected?", path1);
846 failure_finish(NULL);
849 r = my_GetFileInformationByName(path2, &bhfi2);
851 failure_start(file, line, "File %s can't be inspected?", path2);
852 failure_finish(NULL);
855 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
856 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
857 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
859 struct stat st1, st2;
862 assertion_count(file, line);
863 r = lstat(path1, &st1);
865 failure_start(file, line, "File should exist: %s", path1);
866 failure_finish(NULL);
869 r = lstat(path2, &st2);
871 failure_start(file, line, "File should exist: %s", path2);
872 failure_finish(NULL);
875 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
880 assertion_is_hardlink(const char *file, int line,
881 const char *path1, const char *path2)
883 if (is_hardlink(file, line, path1, path2))
885 failure_start(file, line,
886 "Files %s and %s are not hardlinked", path1, path2);
887 failure_finish(NULL);
892 assertion_is_not_hardlink(const char *file, int line,
893 const char *path1, const char *path2)
895 if (!is_hardlink(file, line, path1, path2))
897 failure_start(file, line,
898 "Files %s and %s should not be hardlinked", path1, path2);
899 failure_finish(NULL);
903 /* Verify a/b/mtime of 'pathname'. */
904 /* If 'recent', verify that it's within last 10 seconds. */
906 assertion_file_time(const char *file, int line,
907 const char *pathname, long t, long nsec, char type, int recent)
909 long long filet, filet_nsec;
912 #if defined(_WIN32) && !defined(__CYGWIN__)
913 #define EPOC_TIME (116444736000000000ULL)
914 FILETIME ftime, fbirthtime, fatime, fmtime;
915 ULARGE_INTEGER wintm;
917 ftime.dwLowDateTime = 0;
918 ftime.dwHighDateTime = 0;
920 assertion_count(file, line);
921 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
922 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
923 if (h == INVALID_HANDLE_VALUE) {
924 failure_start(file, line, "Can't access %s\n", pathname);
925 failure_finish(NULL);
928 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
930 case 'a': ftime = fatime; break;
931 case 'b': ftime = fbirthtime; break;
932 case 'm': ftime = fmtime; break;
936 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
937 failure_finish(NULL);
940 wintm.LowPart = ftime.dwLowDateTime;
941 wintm.HighPart = ftime.dwHighDateTime;
942 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
943 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
944 nsec = (nsec / 100) * 100; /* Round the request */
948 assertion_count(file, line);
949 r = lstat(pathname, &st);
951 failure_start(file, line, "Can't stat %s\n", pathname);
952 failure_finish(NULL);
956 case 'a': filet = st.st_atime; break;
957 case 'm': filet = st.st_mtime; break;
958 case 'b': filet = 0; break;
959 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
962 #if defined(__FreeBSD__)
964 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
965 case 'b': filet = st.st_birthtime;
966 filet_nsec = st.st_birthtimespec.tv_nsec; break;
967 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
968 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
971 /* FreeBSD generally only stores to microsecond res, so round. */
972 filet_nsec = (filet_nsec / 1000) * 1000;
973 nsec = (nsec / 1000) * 1000;
975 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
976 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
977 #if defined(__HAIKU__)
978 if (type == 'a') return (1); /* Haiku doesn't have atime. */
983 /* Check that requested time is up-to-date. */
984 time_t now = time(NULL);
985 if (filet < now - 10 || filet > now + 1) {
986 failure_start(file, line,
987 "File %s has %ctime %ld, %ld seconds ago\n",
988 pathname, type, filet, now - filet);
989 failure_finish(NULL);
992 } else if (filet != t || filet_nsec != nsec) {
993 failure_start(file, line,
994 "File %s has %ctime %ld.%09ld, expected %ld.%09ld",
995 pathname, type, filet, filet_nsec, t, nsec);
996 failure_finish(NULL);
1002 /* Verify atime of 'pathname'. */
1004 assertion_file_atime(const char *file, int line,
1005 const char *pathname, long t, long nsec)
1007 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1010 /* Verify atime of 'pathname' is up-to-date. */
1012 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1014 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1017 /* Verify birthtime of 'pathname'. */
1019 assertion_file_birthtime(const char *file, int line,
1020 const char *pathname, long t, long nsec)
1022 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1025 /* Verify birthtime of 'pathname' is up-to-date. */
1027 assertion_file_birthtime_recent(const char *file, int line,
1028 const char *pathname)
1030 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1033 /* Verify mtime of 'pathname'. */
1035 assertion_file_mtime(const char *file, int line,
1036 const char *pathname, long t, long nsec)
1038 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1041 /* Verify mtime of 'pathname' is up-to-date. */
1043 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1045 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1048 /* Verify number of links to 'pathname'. */
1050 assertion_file_nlinks(const char *file, int line,
1051 const char *pathname, int nlinks)
1053 #if defined(_WIN32) && !defined(__CYGWIN__)
1054 BY_HANDLE_FILE_INFORMATION bhfi;
1057 assertion_count(file, line);
1058 r = my_GetFileInformationByName(pathname, &bhfi);
1059 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1061 failure_start(file, line, "File %s has %d links, expected %d",
1062 pathname, bhfi.nNumberOfLinks, nlinks);
1063 failure_finish(NULL);
1069 assertion_count(file, line);
1070 r = lstat(pathname, &st);
1071 if (r == 0 && st.st_nlink == nlinks)
1073 failure_start(file, line, "File %s has %d links, expected %d",
1074 pathname, st.st_nlink, nlinks);
1075 failure_finish(NULL);
1080 /* Verify size of 'pathname'. */
1082 assertion_file_size(const char *file, int line, const char *pathname, long size)
1087 assertion_count(file, line);
1088 #if defined(_WIN32) && !defined(__CYGWIN__)
1090 BY_HANDLE_FILE_INFORMATION bhfi;
1091 r = !my_GetFileInformationByName(pathname, &bhfi);
1092 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1097 r = lstat(pathname, &st);
1098 filesize = st.st_size;
1101 if (r == 0 && filesize == size)
1103 failure_start(file, line, "File %s has size %ld, expected %ld",
1104 pathname, (long)filesize, (long)size);
1105 failure_finish(NULL);
1109 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1111 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1116 #if defined(_WIN32) && !defined(__CYGWIN__)
1117 (void)mode; /* UNUSED */
1119 assertion_count(file, line);
1120 r = lstat(pathname, &st);
1122 failure_start(file, line, "Dir should exist: %s", pathname);
1123 failure_finish(NULL);
1126 if (!S_ISDIR(st.st_mode)) {
1127 failure_start(file, line, "%s is not a dir", pathname);
1128 failure_finish(NULL);
1131 #if !defined(_WIN32) || defined(__CYGWIN__)
1132 /* Windows doesn't handle permissions the same way as POSIX,
1133 * so just ignore the mode tests. */
1134 /* TODO: Can we do better here? */
1135 if (mode >= 0 && mode != (st.st_mode & 07777)) {
1136 failure_start(file, line, "Dir %s has wrong mode", pathname);
1137 logprintf(" Expected: 0%3o\n", mode);
1138 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1139 failure_finish(NULL);
1146 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1147 * verify that too. */
1149 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1154 #if defined(_WIN32) && !defined(__CYGWIN__)
1155 (void)mode; /* UNUSED */
1157 assertion_count(file, line);
1158 r = lstat(pathname, &st);
1159 if (r != 0 || !S_ISREG(st.st_mode)) {
1160 failure_start(file, line, "File should exist: %s", pathname);
1161 failure_finish(NULL);
1164 #if !defined(_WIN32) || defined(__CYGWIN__)
1165 /* Windows doesn't handle permissions the same way as POSIX,
1166 * so just ignore the mode tests. */
1167 /* TODO: Can we do better here? */
1168 if (mode >= 0 && mode != (st.st_mode & 07777)) {
1169 failure_start(file, line, "File %s has wrong mode", pathname);
1170 logprintf(" Expected: 0%3o\n", mode);
1171 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1172 failure_finish(NULL);
1179 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1180 * non-NULL, verify that the symlink has those contents. */
1182 is_symlink(const char *file, int line,
1183 const char *pathname, const char *contents)
1185 #if defined(_WIN32) && !defined(__CYGWIN__)
1186 (void)pathname; /* UNUSED */
1187 (void)contents; /* UNUSED */
1188 assertion_count(file, line);
1189 /* Windows sort-of has real symlinks, but they're only usable
1190 * by privileged users and are crippled even then, so there's
1191 * really not much point in bothering with this. */
1199 assertion_count(file, line);
1200 r = lstat(pathname, &st);
1202 failure_start(file, line,
1203 "Symlink should exist: %s", pathname);
1204 failure_finish(NULL);
1207 if (!S_ISLNK(st.st_mode))
1209 if (contents == NULL)
1211 linklen = readlink(pathname, buff, sizeof(buff));
1213 failure_start(file, line, "Can't read symlink %s", pathname);
1214 failure_finish(NULL);
1217 buff[linklen] = '\0';
1218 if (strcmp(buff, contents) != 0)
1224 /* Assert that path is a symlink that (optionally) contains contents. */
1226 assertion_is_symlink(const char *file, int line,
1227 const char *path, const char *contents)
1229 if (is_symlink(file, line, path, contents))
1232 failure_start(file, line, "File %s is not a symlink to %s",
1235 failure_start(file, line, "File %s is not a symlink", path);
1236 failure_finish(NULL);
1241 /* Create a directory and report any errors. */
1243 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1245 assertion_count(file, line);
1246 #if defined(_WIN32) && !defined(__CYGWIN__)
1247 (void)mode; /* UNUSED */
1248 if (0 == _mkdir(dirname))
1251 if (0 == mkdir(dirname, mode))
1254 failure_start(file, line, "Could not create directory %s", dirname);
1255 failure_finish(NULL);
1259 /* Create a file with the specified contents and report any failures. */
1261 assertion_make_file(const char *file, int line,
1262 const char *path, int mode, const char *contents)
1264 #if defined(_WIN32) && !defined(__CYGWIN__)
1265 /* TODO: Rework this to set file mode as well. */
1267 (void)mode; /* UNUSED */
1268 assertion_count(file, line);
1269 f = fopen(path, "wb");
1271 failure_start(file, line, "Could not create file %s", path);
1272 failure_finish(NULL);
1275 if (contents != NULL) {
1276 if (strlen(contents)
1277 != fwrite(contents, 1, strlen(contents), f)) {
1279 failure_start(file, line,
1280 "Could not write file %s", path);
1281 failure_finish(NULL);
1289 assertion_count(file, line);
1290 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1292 failure_start(file, line, "Could not create %s", path);
1293 failure_finish(NULL);
1296 if (contents != NULL) {
1297 if ((ssize_t)strlen(contents)
1298 != write(fd, contents, strlen(contents))) {
1300 failure_start(file, line, "Could not write to %s", path);
1301 failure_finish(NULL);
1310 /* Create a hardlink and report any failures. */
1312 assertion_make_hardlink(const char *file, int line,
1313 const char *newpath, const char *linkto)
1317 assertion_count(file, line);
1318 #if defined(_WIN32) && !defined(__CYGWIN__)
1319 succeeded = my_CreateHardLinkA(newpath, linkto);
1321 succeeded = !link(linkto, newpath);
1327 failure_start(file, line, "Could not create hardlink");
1328 logprintf(" New link: %s\n", newpath);
1329 logprintf(" Old name: %s\n", linkto);
1330 failure_finish(NULL);
1334 /* Create a symlink and report any failures. */
1336 assertion_make_symlink(const char *file, int line,
1337 const char *newpath, const char *linkto)
1339 #if defined(_WIN32) && !defined(__CYGWIN__)
1340 int targetIsDir = 0; /* TODO: Fix this */
1341 assertion_count(file, line);
1342 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1345 assertion_count(file, line);
1346 if (0 == symlink(linkto, newpath))
1349 failure_start(file, line, "Could not create symlink");
1350 logprintf(" New link: %s\n", newpath);
1351 logprintf(" Old name: %s\n", linkto);
1352 failure_finish(NULL);
1356 /* Set umask, report failures. */
1358 assertion_umask(const char *file, int line, int mask)
1360 assertion_count(file, line);
1361 (void)file; /* UNUSED */
1362 (void)line; /* UNUSED */
1369 * UTILITIES for use by tests.
1374 * Check whether platform supports symlinks. This is intended
1375 * for tests to use in deciding whether to bother testing symlink
1376 * support; if the platform doesn't support symlinks, there's no point
1377 * in checking whether the program being tested can create them.
1379 * Note that the first time this test is called, we actually go out to
1380 * disk to create and verify a symlink. This is necessary because
1381 * symlink support is actually a property of a particular filesystem
1382 * and can thus vary between directories on a single system. After
1383 * the first call, this returns the cached result from memory, so it's
1384 * safe to call it as often as you wish.
1389 /* Remember the test result */
1390 static int value = 0, tested = 0;
1395 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, "a");
1396 /* Note: Cygwin has its own symlink() emulation that does not
1397 * use the Win32 CreateSymbolicLink() function. */
1398 #if defined(_WIN32) && !defined(__CYGWIN__)
1399 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1400 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1402 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1403 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1409 * Can this platform run the gzip program?
1411 /* Platform-dependent options for hiding the output of a subcommand. */
1412 #if defined(_WIN32) && !defined(__CYGWIN__)
1413 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1415 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1420 static int tested = 0, value = 0;
1423 if (systemf("gzip -V %s", redirectArgs) == 0)
1430 * Can this platform run the gunzip program?
1435 static int tested = 0, value = 0;
1438 if (systemf("gunzip -V %s", redirectArgs) == 0)
1445 * Sleep as needed; useful for verifying disk timestamp changes by
1446 * ensuring that the wall-clock time has actually changed before we
1447 * go back to re-read something from disk.
1450 sleepUntilAfter(time_t t)
1452 while (t >= time(NULL))
1453 #if defined(_WIN32) && !defined(__CYGWIN__)
1461 * Call standard system() call, but build up the command line using
1462 * sprintf() conventions.
1465 systemf(const char *fmt, ...)
1472 vsprintf(buff, fmt, ap);
1473 if (verbosity > VERBOSITY_FULL)
1474 logprintf("Cmd: %s\n", buff);
1481 * Slurp a file into memory for ease of comparison and testing.
1482 * Returns size of file in 'sizep' if non-NULL, null-terminates
1483 * data in memory for ease of use.
1486 slurpfile(size_t * sizep, const char *fmt, ...)
1488 char filename[8192];
1497 vsprintf(filename, fmt, ap);
1500 f = fopen(filename, "rb");
1502 /* Note: No error; non-existent file is okay here. */
1505 r = fstat(fileno(f), &st);
1507 logprintf("Can't stat file %s\n", filename);
1511 p = malloc((size_t)st.st_size + 1);
1513 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
1514 (long int)st.st_size, filename);
1518 bytes_read = fread(p, 1, (size_t)st.st_size, f);
1519 if (bytes_read < st.st_size) {
1520 logprintf("Can't read file %s\n", filename);
1525 p[st.st_size] = '\0';
1527 *sizep = (size_t)st.st_size;
1532 /* Read a uuencoded file from the reference directory, decode, and
1533 * write the result into the current directory. */
1534 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
1536 extract_reference_file(const char *name)
1541 sprintf(buff, "%s/%s.uu", refdir, name);
1542 in = fopen(buff, "r");
1543 failure("Couldn't open reference file %s", buff);
1547 /* Read up to and including the 'begin' line. */
1549 if (fgets(buff, sizeof(buff), in) == NULL) {
1550 /* TODO: This is a failure. */
1553 if (memcmp(buff, "begin ", 6) == 0)
1556 /* Now, decode the rest and write it. */
1557 /* Not a lot of error checking here; the input better be right. */
1558 out = fopen(name, "wb");
1559 while (fgets(buff, sizeof(buff), in) != NULL) {
1563 if (memcmp(buff, "end", 3) == 0)
1566 bytes = UUDECODE(*p++);
1569 /* Write out 1-3 bytes from that. */
1571 n = UUDECODE(*p++) << 18;
1572 n |= UUDECODE(*p++) << 12;
1573 fputc(n >> 16, out);
1577 n |= UUDECODE(*p++) << 6;
1578 fputc((n >> 8) & 0xFF, out);
1582 n |= UUDECODE(*p++);
1583 fputc(n & 0xFF, out);
1599 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
1601 * DEFINE_TEST(test_function)
1605 /* Use "list.h" to declare all of the test functions. */
1607 #define DEFINE_TEST(name) void name(void);
1610 /* Use "list.h" to create a list of all tests (functions and names). */
1612 #define DEFINE_TEST(n) { n, #n, 0 },
1613 struct { void (*func)(void); const char *name; int failures; } tests[] = {
1618 * Summarize repeated failures in the just-completed test.
1621 test_summarize(const char *filename, int failed)
1625 switch (verbosity) {
1626 case VERBOSITY_SUMMARY_ONLY:
1627 printf(failed ? "E" : ".");
1630 case VERBOSITY_PASSFAIL:
1631 printf(failed ? "FAIL\n" : "ok\n");
1635 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
1637 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
1638 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
1639 logprintf("%s:%d: Summary: Failed %d times\n",
1640 filename, i, failed_lines[i].count);
1642 /* Clear the failure history for the next file. */
1643 memset(failed_lines, 0, sizeof(failed_lines));
1647 * Actually run a single test, with appropriate setup and cleanup.
1650 test_run(int i, const char *tmpdir)
1652 char logfilename[64];
1653 int failures_before = failures;
1656 switch (verbosity) {
1657 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
1659 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
1660 printf("%3d: %-50s", i, tests[i].name);
1663 default: /* Title of test, details will follow */
1664 printf("%3d: %s\n", i, tests[i].name);
1667 /* Chdir to the top-level work directory. */
1668 if (!assertChdir(tmpdir)) {
1670 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
1673 /* Create a log file for this test. */
1674 sprintf(logfilename, "%s.log", tests[i].name);
1675 logfile = fopen(logfilename, "w");
1676 fprintf(logfile, "%s\n\n", tests[i].name);
1677 /* Chdir() to a work dir for this specific test. */
1678 if (!assertMakeDir(tests[i].name, 0755)
1679 || !assertChdir(tests[i].name)) {
1681 "ERROR: Can't chdir to work dir %s/%s\n",
1682 tmpdir, tests[i].name);
1685 /* Explicitly reset the locale before each test. */
1686 setlocale(LC_ALL, "C");
1687 /* Record the umask before we run the test. */
1688 umask(oldumask = umask(0));
1690 * Run the actual test.
1694 * Clean up and report afterwards.
1699 setlocale(LC_ALL, "C");
1700 /* Reset directory. */
1701 if (!assertChdir(tmpdir)) {
1702 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
1706 /* Report per-test summaries. */
1707 tests[i].failures = failures - failures_before;
1708 test_summarize(test_filename, tests[i].failures);
1709 /* Close the per-test log file. */
1712 /* If there were no failures, we can remove the work dir and logfile. */
1713 if (tests[i].failures == 0) {
1714 if (!keep_temp_files && assertChdir(tmpdir)) {
1715 #if defined(_WIN32) && !defined(__CYGWIN__)
1716 /* Make sure not to leave empty directories.
1717 * Sometimes a processing of closing files used by tests
1718 * is not done, then rmdir will be failed and it will
1719 * leave a empty test directory. So we should wait a few
1720 * seconds and retry rmdir. */
1722 for (t = 0; t < 10; t++) {
1725 r = systemf("rmdir /S /Q %s", tests[i].name);
1729 systemf("del %s", logfilename);
1731 systemf("rm -rf %s", tests[i].name);
1732 systemf("rm %s", logfilename);
1736 /* Return appropriate status. */
1737 return (tests[i].failures);
1743 * MAIN and support routines.
1749 usage(const char *program)
1751 static const int limit = sizeof(tests) / sizeof(tests[0]);
1754 printf("Usage: %s [options] <test> <test> ...\n", program);
1755 printf("Default is to run all tests.\n");
1756 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
1757 printf("Options:\n");
1758 printf(" -d Dump core after any failure, for debugging.\n");
1759 printf(" -k Keep all temp files.\n");
1760 printf(" Default: temp files for successful tests deleted.\n");
1762 printf(" -p <path> Path to executable to be tested.\n");
1763 printf(" Default: path taken from " ENVBASE " environment variable.\n");
1765 printf(" -q Quiet.\n");
1766 printf(" -r <dir> Path to dir containing reference files.\n");
1767 printf(" Default: Current directory.\n");
1768 printf(" -v Verbose.\n");
1769 printf("Available tests:\n");
1770 for (i = 0; i < limit; i++)
1771 printf(" %d: %s\n", i, tests[i].name);
1776 get_refdir(const char *d)
1778 char tried[512] = { '\0' };
1782 /* If a dir was specified, try that */
1785 snprintf(buff, sizeof(buff), "%s", d);
1786 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1787 if (p != NULL) goto success;
1788 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1789 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1793 /* Get the current dir. */
1794 pwd = getcwd(NULL, 0);
1795 while (pwd[strlen(pwd) - 1] == '\n')
1796 pwd[strlen(pwd) - 1] = '\0';
1798 /* Look for a known file. */
1799 snprintf(buff, sizeof(buff), "%s", pwd);
1800 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1801 if (p != NULL) goto success;
1802 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1803 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1805 snprintf(buff, sizeof(buff), "%s/test", pwd);
1806 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1807 if (p != NULL) goto success;
1808 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1809 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1811 #if defined(LIBRARY)
1812 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
1814 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
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 if (memcmp(pwd, "/usr/obj", 8) == 0) {
1822 snprintf(buff, sizeof(buff), "%s", pwd + 8);
1823 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1824 if (p != NULL) goto success;
1825 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1826 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1828 snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
1829 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1830 if (p != NULL) goto success;
1831 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1832 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1836 printf("Unable to locate known reference file %s\n", KNOWNREF);
1837 printf(" Checked following directories:\n%s\n", tried);
1838 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
1846 return strdup(buff);
1850 main(int argc, char **argv)
1852 static const int limit = sizeof(tests) / sizeof(tests[0]);
1853 int i, tests_run = 0, tests_failed = 0, option;
1855 char *refdir_alloc = NULL;
1856 const char *progname;
1857 const char *tmp, *option_arg, *p;
1859 char tmpdir_timestamp[256];
1861 (void)argc; /* UNUSED */
1863 #if defined(HAVE__CrtSetReportMode)
1864 /* To stop to run the default invalid parameter handler. */
1865 _set_invalid_parameter_handler(invalid_parameter_handler);
1866 /* Disable annoying assertion message box. */
1867 _CrtSetReportMode(_CRT_ASSERT, 0);
1871 * Name of this program, used to build root of our temp directory
1874 progname = p = argv[0];
1875 while (*p != '\0') {
1876 /* Support \ or / dir separators for Windows compat. */
1877 if (*p == '/' || *p == '\\')
1883 /* Get the target program from environment, if available. */
1884 testprogfile = getenv(ENVBASE);
1887 if (getenv("TMPDIR") != NULL)
1888 tmp = getenv("TMPDIR");
1889 else if (getenv("TMP") != NULL)
1890 tmp = getenv("TMP");
1891 else if (getenv("TEMP") != NULL)
1892 tmp = getenv("TEMP");
1893 else if (getenv("TEMPDIR") != NULL)
1894 tmp = getenv("TEMPDIR");
1898 /* Allow -d to be controlled through the environment. */
1899 if (getenv(ENVBASE "_DEBUG") != NULL)
1900 dump_on_failure = 1;
1902 /* Get the directory holding test files from environment. */
1903 refdir = getenv(ENVBASE "_TEST_FILES");
1906 * Parse options, without using getopt(), which isn't available
1909 ++argv; /* Skip program name */
1910 while (*argv != NULL) {
1915 while (*p != '\0') {
1918 /* If 'opt' takes an argument, parse that. */
1919 if (option == 'p' || option == 'r') {
1922 else if (*argv == NULL) {
1924 "Option -%c requires argument.\n",
1928 option_arg = *argv++;
1929 p = ""; /* End of this option word. */
1932 /* Now, handle the option. */
1935 dump_on_failure = 1;
1938 keep_temp_files = 1;
1942 testprogfile = option_arg;
1951 refdir = option_arg;
1963 * Sanity-check that our options make sense.
1966 if (testprogfile == NULL)
1970 #if defined(_WIN32) && !defined(__CYGWIN__)
1971 /* Command.com sometimes rejects '/' separators. */
1972 testprg = strdup(testprogfile);
1973 for (i = 0; testprg[i] != '\0'; i++) {
1974 if (testprg[i] == '/')
1977 testprogfile = testprg;
1979 /* Quote the name that gets put into shell command lines. */
1980 testprg = malloc(strlen(testprogfile) + 3);
1981 strcpy(testprg, "\"");
1982 strcat(testprg, testprogfile);
1983 strcat(testprg, "\"");
1989 * Create a temp directory for the following tests.
1990 * Include the time the tests started as part of the name,
1991 * to make it easier to track the results of multiple tests.
1994 for (i = 0; ; i++) {
1995 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
1996 "%Y-%m-%dT%H.%M.%S",
1998 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
1999 tmpdir_timestamp, i);
2000 if (assertMakeDir(tmpdir,0755))
2004 "ERROR: Unable to create temp directory %s\n",
2011 * If the user didn't specify a directory for locating
2012 * reference files, try to find the reference files in
2013 * the "usual places."
2015 refdir = refdir_alloc = get_refdir(refdir);
2018 * Banner with basic information.
2021 printf("If tests fail or crash, details will be in:\n");
2022 printf(" %s\n", tmpdir);
2024 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2025 printf("Reference files will be read from: %s\n", refdir);
2027 printf("Running tests on: %s\n", testprog);
2029 printf("Exercising: ");
2031 printf("%s\n", EXTRA_VERSION);
2038 * Run some or all of the individual tests.
2040 if (*argv == NULL) {
2041 /* Default: Run all tests. */
2042 for (i = 0; i < limit; i++) {
2043 if (test_run(i, tmpdir))
2048 while (*(argv) != NULL) {
2049 if (**argv >= '0' && **argv <= '9') {
2051 if (i < 0 || i >= limit) {
2052 printf("*** INVALID Test %s\n", *argv);
2055 /* usage() never returns */
2058 for (i = 0; i < limit; ++i) {
2059 if (strcmp(*argv, tests[i].name) == 0)
2063 printf("*** INVALID Test ``%s''\n",
2067 /* usage() never returns */
2070 if (test_run(i, tmpdir))
2078 * Report summary statistics.
2080 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2082 printf("Totals:\n");
2083 printf(" Tests run: %8d\n", tests_run);
2084 printf(" Tests failed: %8d\n", tests_failed);
2085 printf(" Assertions checked:%8d\n", assertions);
2086 printf(" Assertions failed: %8d\n", failures);
2087 printf(" Skips reported: %8d\n", skips);
2091 printf("Failing tests:\n");
2092 for (i = 0; i < limit; ++i) {
2093 if (tests[i].failures)
2094 printf(" %d: %s (%d failures)\n", i,
2095 tests[i].name, tests[i].failures);
2098 printf("Details for failing tests: %s\n", tmpdir);
2101 if (verbosity == VERBOSITY_SUMMARY_ONLY)
2103 printf("%d tests passed, no failures\n", tests_run);
2108 /* If the final tmpdir is empty, we can remove it. */
2109 /* This should be the usual case when all tests succeed. */
2113 return (tests_failed ? 1 : 0);