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: src/usr.bin/tar/test/main.c,v 1.6 2008/11/05 06:40:53 kientzle Exp $");
40 #define KNOWNREF "test_patterns_2.tar.uu"
41 #define ENVBASE "BSDTAR" /* Prefix for environment variables. */
42 #define PROGRAM "bsdtar" /* Name of program being tested. */
43 #undef LIBRARY /* Not testing a library. */
44 #undef EXTRA_DUMP /* How to dump extra data */
45 /* How to generate extra version info. */
46 #define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "")
50 * Windows support routines
52 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
53 * in the test harness is dangerous because they cover up
54 * configuration errors. The classic example of this is omitting a
55 * configure check. If libarchive and libarchive_test both look for
56 * the same feature macro, such errors are hard to detect. Platform
57 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
58 * easily lead to very messy code. It's best to limit yourself
59 * to only the most generic programming techniques in the test harness
60 * and thus avoid conditionals altogether. Where that's not possible,
61 * try to minimize conditionals by grouping platform-specific tests in
62 * one place (e.g., test_acl_freebsd) or by adding new assert()
63 * functions (e.g., assertMakeHardlink()) to cover up platform
64 * differences. Platform-specific coding in libarchive_test is often
65 * a symptom that some capability is missing from libarchive itself.
67 #if defined(_WIN32) && !defined(__CYGWIN__)
74 #define S_ISDIR(m) ((m) & _S_IFDIR)
77 #define S_ISREG(m) ((m) & _S_IFREG)
79 #if !defined(__BORLANDC__)
80 #define access _access
85 #define fileno _fileno
87 /*#define fstat _fstat64*/
88 #if !defined(__BORLANDC__)
89 #define getcwd _getcwd
92 /*#define lstat _stat64*/
93 /*#define stat _stat64*/
95 #if !defined(__BORLANDC__)
96 #define strdup _strdup
99 #define int64_t __int64
102 #if defined(HAVE__CrtSetReportMode)
106 #if defined(_WIN32) && !defined(__CYGWIN__)
107 void *GetFunctionKernel32(const char *name
)
109 static HINSTANCE lib
;
113 lib
= LoadLibrary("kernel32.dll");
116 fprintf(stderr
, "Can't load kernel32.dll?!\n");
119 return (void *)GetProcAddress(lib
, name
);
123 my_CreateSymbolicLinkA(const char *linkname
, const char *target
, int flags
)
125 static BOOLEAN (WINAPI
*f
)(LPCSTR
, LPCSTR
, DWORD
);
129 f
= GetFunctionKernel32("CreateSymbolicLinkA");
131 return f
== NULL
? 0 : (*f
)(linkname
, target
, flags
);
135 my_CreateHardLinkA(const char *linkname
, const char *target
)
137 static BOOLEAN (WINAPI
*f
)(LPCSTR
, LPCSTR
, LPSECURITY_ATTRIBUTES
);
141 f
= GetFunctionKernel32("CreateHardLinkA");
143 return f
== NULL
? 0 : (*f
)(linkname
, target
, NULL
);
147 my_GetFileInformationByName(const char *path
, BY_HANDLE_FILE_INFORMATION
*bhfi
)
152 memset(bhfi
, 0, sizeof(*bhfi
));
153 h
= CreateFile(path
, FILE_READ_ATTRIBUTES
, 0, NULL
,
154 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
155 if (h
== INVALID_HANDLE_VALUE
)
157 r
= GetFileInformationByHandle(h
, bhfi
);
163 #if defined(HAVE__CrtSetReportMode)
165 invalid_parameter_handler(const wchar_t * expression
,
166 const wchar_t * function
, const wchar_t * file
,
167 unsigned int line
, uintptr_t pReserved
)
179 /* Enable core dump on failure. */
180 static int dump_on_failure
= 0;
181 /* Default is to remove temp dirs and log data for successful tests. */
182 static int keep_temp_files
= 0;
183 /* Default is to just report pass/fail for each test. */
184 static int verbosity
= 0;
185 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
186 #define VERBOSITY_PASSFAIL 0 /* Default */
187 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
188 #define VERBOSITY_FULL 2 /* -vv */
189 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
190 * mostly for debugging the test harness itself. */
191 /* Cumulative count of assertion failures. */
192 static int failures
= 0;
193 /* Cumulative count of reported skips. */
194 static int skips
= 0;
195 /* Cumulative count of assertions checked. */
196 static int assertions
= 0;
198 /* Directory where uuencoded reference files can be found. */
199 static const char *refdir
;
202 * Report log information selectively to console and/or disk log.
204 static int log_console
= 0;
205 static FILE *logfile
;
207 vlogprintf(const char *fmt
, va_list ap
)
214 vfprintf(stdout
, fmt
, ap
);
217 vfprintf(logfile
, fmt
, lfap
);
220 vfprintf(logfile
, fmt
, ap
);
225 logprintf(const char *fmt
, ...)
233 /* Set up a message to display only if next assertion fails. */
234 static char msgbuff
[4096];
235 static const char *msg
, *nextmsg
;
237 failure(const char *fmt
, ...)
241 vsprintf(msgbuff
, fmt
, ap
);
247 * Copy arguments into file-local variables.
248 * This was added to permit vararg assert() functions without needing
249 * variadic wrapper macros. Turns out that the vararg capability is almost
250 * never used, so almost all of the vararg assertions can be simplified
251 * by removing the vararg capability and reworking the wrapper macro to
252 * pass __FILE__, __LINE__ directly into the function instead of using
253 * this hook. I suspect this machinery is used so rarely that we
254 * would be better off just removing it entirely. That would simplify
255 * the code here noticably.
257 static const char *test_filename
;
258 static int test_line
;
259 static void *test_extra
;
260 void assertion_setup(const char *filename
, int line
)
262 test_filename
= filename
;
266 /* Called at the beginning of each assert() function. */
268 assertion_count(const char *file
, int line
)
270 (void)file
; /* UNUSED */
271 (void)line
; /* UNUSED */
273 /* Proper handling of "failure()" message. */
276 /* Uncomment to print file:line after every assertion.
277 * Verbose, but occasionally useful in tracking down crashes. */
278 /* printf("Checked %s:%d\n", file, line); */
282 * For each test source file, we remember how many times each
283 * assertion was reported. Cleared before each new test,
284 * used by test_summarize().
289 } failed_lines
[10000];
291 /* Count this failure, setup up log destination and handle initial report. */
293 failure_start(const char *filename
, int line
, const char *fmt
, ...)
297 /* Record another failure for this line. */
299 /* test_filename = filename; */
300 failed_lines
[line
].count
++;
302 /* Determine whether to log header to console. */
307 case VERBOSITY_LIGHT_REPORT
:
308 log_console
= (failed_lines
[line
].count
< 2);
314 /* Log file:line header for this failure */
317 logprintf("%s(%d): ", filename
, line
);
319 logprintf("%s:%d: ", filename
, line
);
325 if (msg
!= NULL
&& msg
[0] != '\0') {
326 logprintf(" Description: %s\n", msg
);
330 /* Determine whether to log details to console. */
331 if (verbosity
== VERBOSITY_LIGHT_REPORT
)
335 /* Complete reporting of failed tests. */
337 * The 'extra' hook here is used by libarchive to include libarchive
338 * error messages with assertion failures. It could also be used
339 * to add strerror() output, for example. Just define the EXTRA_DUMP()
340 * macro appropriately.
343 failure_finish(void *extra
)
345 (void)extra
; /* UNUSED (maybe) */
348 logprintf(" detail: %s\n", EXTRA_DUMP(extra
));
351 if (dump_on_failure
) {
353 " *** forcing core dump so failure can be debugged ***\n");
359 /* Inform user that we're skipping some checks. */
361 test_skipping(const char *fmt
, ...)
367 vsprintf(buff
, fmt
, ap
);
369 /* failure_start() isn't quite right, but is awfully convenient. */
370 failure_start(test_filename
, test_line
, "SKIPPING: %s", buff
);
371 --failures
; /* Undo failures++ in failure_start() */
372 /* Don't failure_finish() here. */
373 /* Mark as skip, so doesn't count as failed test. */
374 failed_lines
[test_line
].skip
= 1;
384 /* Generic assert() just displays the failed condition. */
386 assertion_assert(const char *file
, int line
, int value
,
387 const char *condition
, void *extra
)
389 assertion_count(file
, line
);
391 failure_start(file
, line
, "Assertion failed: %s", condition
);
392 failure_finish(extra
);
397 /* chdir() and report any errors */
399 assertion_chdir(const char *file
, int line
, const char *pathname
)
401 assertion_count(file
, line
);
402 if (chdir(pathname
) == 0)
404 failure_start(file
, line
, "chdir(\"%s\")", pathname
);
405 failure_finish(NULL
);
410 /* Verify two integers are equal. */
412 assertion_equal_int(const char *file
, int line
,
413 long long v1
, const char *e1
, long long v2
, const char *e2
, void *extra
)
415 assertion_count(file
, line
);
418 failure_start(file
, line
, "%s != %s", e1
, e2
);
419 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1
, v1
, v1
, v1
);
420 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2
, v2
, v2
, v2
);
421 failure_finish(extra
);
425 static void strdump(const char *e
, const char *p
)
429 logprintf(" %s = ", e
);
436 unsigned int c
= 0xff & *p
++;
438 case '\a': printf("\a"); break;
439 case '\b': printf("\b"); break;
440 case '\n': printf("\n"); break;
441 case '\r': printf("\r"); break;
443 if (c
>= 32 && c
< 127)
446 logprintf("\\x%02X", c
);
450 logprintf(" (length %d)\n", q
== NULL
? -1 : (int)strlen(q
));
453 /* Verify two strings are equal, dump them if not. */
455 assertion_equal_string(const char *file
, int line
,
456 const char *v1
, const char *e1
,
457 const char *v2
, const char *e2
,
460 assertion_count(file
, line
);
461 if (v1
== v2
|| (v1
!= NULL
&& v2
!= NULL
&& strcmp(v1
, v2
) == 0))
463 failure_start(file
, line
, "%s != %s", e1
, e2
);
466 failure_finish(extra
);
471 wcsdump(const char *e
, const wchar_t *w
)
473 logprintf(" %s = ", e
);
479 while (*w
!= L
'\0') {
480 unsigned int c
= *w
++;
481 if (c
>= 32 && c
< 127)
484 logprintf("\\x%02X", c
);
485 else if (c
< 0x10000)
486 logprintf("\\u%04X", c
);
488 logprintf("\\U%08X", c
);
493 /* Verify that two wide strings are equal, dump them if not. */
495 assertion_equal_wstring(const char *file
, int line
,
496 const wchar_t *v1
, const char *e1
,
497 const wchar_t *v2
, const char *e2
,
500 assertion_count(file
, line
);
501 if (v1
== v2
|| wcscmp(v1
, v2
) == 0)
503 failure_start(file
, line
, "%s != %s", e1
, e2
);
506 failure_finish(extra
);
511 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
512 * any bytes in p that differ from ref will be highlighted with '_'
513 * before and after the hex value.
516 hexdump(const char *p
, const char *ref
, size_t l
, size_t offset
)
522 logprintf("(null)\n");
525 for(i
=0; i
< l
; i
+=16) {
526 logprintf("%04x", (unsigned)(i
+ offset
));
528 for (j
= 0; j
< 16 && i
+ j
< l
; j
++) {
529 if (ref
!= NULL
&& p
[i
+ j
] != ref
[i
+ j
])
531 logprintf("%c%02x", sep
, 0xff & (int)p
[i
+j
]);
532 if (ref
!= NULL
&& p
[i
+ j
] == ref
[i
+ j
])
535 for (; j
< 16; j
++) {
536 logprintf("%c ", sep
);
539 logprintf("%c", sep
);
540 for (j
=0; j
< 16 && i
+ j
< l
; j
++) {
542 if (c
>= ' ' && c
<= 126)
551 /* Verify that two blocks of memory are the same, display the first
552 * block of differences if they're not. */
554 assertion_equal_mem(const char *file
, int line
,
555 const void *_v1
, const char *e1
,
556 const void *_v2
, const char *e2
,
557 size_t l
, const char *ld
, void *extra
)
559 const char *v1
= (const char *)_v1
;
560 const char *v2
= (const char *)_v2
;
563 assertion_count(file
, line
);
564 if (v1
== v2
|| (v1
!= NULL
&& v2
!= NULL
&& memcmp(v1
, v2
, l
) == 0))
567 failure_start(file
, line
, "%s != %s", e1
, e2
);
568 logprintf(" size %s = %d\n", ld
, (int)l
);
569 /* Dump 48 bytes (3 lines) so that the first difference is
570 * in the second line. */
572 while (l
> 64 && memcmp(v1
, v2
, 32) == 0) {
573 /* Two lines agree, so step forward one line. */
579 logprintf(" Dump of %s\n", e1
);
580 hexdump(v1
, v2
, l
< 64 ? l
: 64, offset
);
581 logprintf(" Dump of %s\n", e2
);
582 hexdump(v2
, v1
, l
< 64 ? l
: 64, offset
);
584 failure_finish(extra
);
588 /* Verify that the named file exists and is empty. */
590 assertion_empty_file(const char *f1fmt
, ...)
599 assertion_count(test_filename
, test_line
);
601 vsprintf(f1
, f1fmt
, ap
);
604 if (stat(f1
, &st
) != 0) {
605 failure_start(test_filename
, test_line
, "Stat failed: %s", f1
);
606 failure_finish(NULL
);
612 failure_start(test_filename
, test_line
, "File should be empty: %s", f1
);
613 logprintf(" File size: %d\n", (int)st
.st_size
);
614 logprintf(" Contents:\n");
617 logprintf(" Unable to open %s\n", f1
);
619 s
= ((off_t
)sizeof(buff
) < st
.st_size
) ?
620 (ssize_t
)sizeof(buff
) : (ssize_t
)st
.st_size
;
621 s
= fread(buff
, 1, s
, f
);
622 hexdump(buff
, NULL
, s
, 0);
625 failure_finish(NULL
);
629 /* Verify that the named file exists and is not empty. */
631 assertion_non_empty_file(const char *f1fmt
, ...)
637 assertion_count(test_filename
, test_line
);
639 vsprintf(f1
, f1fmt
, ap
);
642 if (stat(f1
, &st
) != 0) {
643 failure_start(test_filename
, test_line
, "Stat failed: %s", f1
);
644 failure_finish(NULL
);
647 if (st
.st_size
== 0) {
648 failure_start(test_filename
, test_line
, "File empty: %s", f1
);
649 failure_finish(NULL
);
655 /* Verify that two files have the same contents. */
656 /* TODO: hexdump the first bytes that actually differ. */
658 assertion_equal_file(const char *fn1
, const char *f2pattern
, ...)
667 assertion_count(test_filename
, test_line
);
668 va_start(ap
, f2pattern
);
669 vsprintf(fn2
, f2pattern
, ap
);
672 f1
= fopen(fn1
, "rb");
673 f2
= fopen(fn2
, "rb");
675 n1
= fread(buff1
, 1, sizeof(buff1
), f1
);
676 n2
= fread(buff2
, 1, sizeof(buff2
), f2
);
679 if (n1
== 0 && n2
== 0) {
684 if (memcmp(buff1
, buff2
, n1
) != 0)
689 failure_start(test_filename
, test_line
, "Files not identical");
690 logprintf(" file1=\"%s\"\n", fn1
);
691 logprintf(" file2=\"%s\"\n", fn2
);
692 failure_finish(test_extra
);
696 /* Verify that the named file does exist. */
698 assertion_file_exists(const char *fpattern
, ...)
703 assertion_count(test_filename
, test_line
);
704 va_start(ap
, fpattern
);
705 vsprintf(f
, fpattern
, ap
);
708 #if defined(_WIN32) && !defined(__CYGWIN__)
712 if (!access(f
, F_OK
))
715 failure_start(test_filename
, test_line
, "File should exist: %s", f
);
716 failure_finish(test_extra
);
720 /* Verify that the named file doesn't exist. */
722 assertion_file_not_exists(const char *fpattern
, ...)
727 assertion_count(test_filename
, test_line
);
728 va_start(ap
, fpattern
);
729 vsprintf(f
, fpattern
, ap
);
732 #if defined(_WIN32) && !defined(__CYGWIN__)
739 failure_start(test_filename
, test_line
, "File should not exist: %s", f
);
740 failure_finish(test_extra
);
744 /* Compare the contents of a file to a block of memory. */
746 assertion_file_contents(const void *buff
, int s
, const char *fpattern
, ...)
754 assertion_count(test_filename
, test_line
);
755 va_start(ap
, fpattern
);
756 vsprintf(fn
, fpattern
, ap
);
761 failure_start(test_filename
, test_line
,
762 "File should exist: %s", fn
);
763 failure_finish(test_extra
);
766 contents
= malloc(s
* 2);
767 n
= fread(contents
, 1, s
* 2, f
);
769 if (n
== s
&& memcmp(buff
, contents
, s
) == 0) {
773 failure_start(test_filename
, test_line
, "File contents don't match");
774 logprintf(" file=\"%s\"\n", fn
);
776 hexdump(contents
, buff
, n
> 512 ? 512 : n
, 0);
778 logprintf(" File empty, contents should be:\n");
779 hexdump(buff
, NULL
, s
> 512 ? 512 : s
, 0);
781 failure_finish(test_extra
);
786 /* Check the contents of a text file, being tolerant of line endings. */
788 assertion_text_file_contents(const char *buff
, const char *fn
)
791 const char *btxt
, *ftxt
;
795 assertion_count(test_filename
, test_line
);
798 contents
= malloc(s
* 2 + 128);
799 n
= fread(contents
, 1, s
* 2 + 128 - 1, f
);
805 ftxt
= (const char *)contents
;
806 while (*btxt
!= '\0' && *ftxt
!= '\0') {
807 if (*btxt
== *ftxt
) {
812 if (btxt
[0] == '\n' && ftxt
[0] == '\r' && ftxt
[1] == '\n') {
813 /* Pass over different new line characters. */
820 if (*btxt
== '\0' && *ftxt
== '\0') {
824 failure_start(test_filename
, test_line
, "Contents don't match");
825 logprintf(" file=\"%s\"\n", fn
);
827 hexdump(contents
, buff
, n
, 0);
829 logprintf(" File empty, contents should be:\n");
830 hexdump(buff
, NULL
, s
, 0);
832 failure_finish(test_extra
);
837 /* Verify that a text file contains the specified lines, regardless of order */
838 /* This could be more efficient if we sorted both sets of lines, etc, but
839 * since this is used only for testing and only ever deals with a dozen or so
840 * lines at a time, this relatively crude approach is just fine. */
842 assertion_file_contains_lines_any_order(const char *file
, int line
,
843 const char *pathname
, const char *lines
[])
847 size_t expected_count
, actual_count
, i
, j
;
851 int expected_failure
= 0, actual_failure
= 0;
853 assertion_count(file
, line
);
855 buff
= slurpfile(&buff_size
, "%s", pathname
);
857 failure_start(pathname
, line
, "Can't read file: %s", pathname
);
858 failure_finish(NULL
);
862 // Make a copy of the provided lines and count up the expected file size.
864 for (i
= 0; lines
[i
] != NULL
; ++i
) {
867 expected
= malloc(sizeof(char *) * expected_count
);
868 for (i
= 0; lines
[i
] != NULL
; ++i
) {
869 expected
[i
] = strdup(lines
[i
]);
872 // Break the file into lines
874 for (c
= '\0', p
= buff
; p
< buff
+ buff_size
; ++p
) {
875 if (*p
== '\x0d' || *p
== '\x0a')
877 if (c
== '\0' && *p
!= '\0')
881 actual
= malloc(sizeof(char *) * actual_count
);
882 for (j
= 0, p
= buff
; p
< buff
+ buff_size
; p
+= 1 + strlen(p
)) {
889 // Erase matching lines from both lists
890 for (i
= 0; i
< expected_count
; ++i
) {
891 if (expected
[i
] == NULL
)
893 for (j
= 0; j
< actual_count
; ++j
) {
894 if (actual
[j
] == NULL
)
896 if (strcmp(expected
[i
], actual
[j
]) == 0) {
905 // If there's anything left, it's a failure
906 for (i
= 0; i
< expected_count
; ++i
) {
907 if (expected
[i
] != NULL
)
910 for (j
= 0; j
< actual_count
; ++j
) {
911 if (actual
[j
] != NULL
)
914 if (expected_failure
== 0 && actual_failure
== 0) {
920 failure_start(file
, line
, "File doesn't match: %s", pathname
);
921 for (i
= 0; i
< expected_count
; ++i
) {
922 if (expected
[i
] != NULL
) {
923 logprintf(" Expected but not present: %s\n", expected
[i
]);
927 for (j
= 0; j
< actual_count
; ++j
) {
928 if (actual
[j
] != NULL
)
929 logprintf(" Present but not expected: %s\n", actual
[j
]);
931 failure_finish(NULL
);
938 /* Test that two paths point to the same file. */
939 /* As a side-effect, asserts that both files exist. */
941 is_hardlink(const char *file
, int line
,
942 const char *path1
, const char *path2
)
944 #if defined(_WIN32) && !defined(__CYGWIN__)
945 BY_HANDLE_FILE_INFORMATION bhfi1
, bhfi2
;
948 assertion_count(file
, line
);
949 r
= my_GetFileInformationByName(path1
, &bhfi1
);
951 failure_start(file
, line
, "File %s can't be inspected?", path1
);
952 failure_finish(NULL
);
955 r
= my_GetFileInformationByName(path2
, &bhfi2
);
957 failure_start(file
, line
, "File %s can't be inspected?", path2
);
958 failure_finish(NULL
);
961 return (bhfi1
.dwVolumeSerialNumber
== bhfi2
.dwVolumeSerialNumber
962 && bhfi1
.nFileIndexHigh
== bhfi2
.nFileIndexHigh
963 && bhfi1
.nFileIndexLow
== bhfi2
.nFileIndexLow
);
965 struct stat st1
, st2
;
968 assertion_count(file
, line
);
969 r
= lstat(path1
, &st1
);
971 failure_start(file
, line
, "File should exist: %s", path1
);
972 failure_finish(NULL
);
975 r
= lstat(path2
, &st2
);
977 failure_start(file
, line
, "File should exist: %s", path2
);
978 failure_finish(NULL
);
981 return (st1
.st_ino
== st2
.st_ino
&& st1
.st_dev
== st2
.st_dev
);
986 assertion_is_hardlink(const char *file
, int line
,
987 const char *path1
, const char *path2
)
989 if (is_hardlink(file
, line
, path1
, path2
))
991 failure_start(file
, line
,
992 "Files %s and %s are not hardlinked", path1
, path2
);
993 failure_finish(NULL
);
998 assertion_is_not_hardlink(const char *file
, int line
,
999 const char *path1
, const char *path2
)
1001 if (!is_hardlink(file
, line
, path1
, path2
))
1003 failure_start(file
, line
,
1004 "Files %s and %s should not be hardlinked", path1
, path2
);
1005 failure_finish(NULL
);
1009 /* Verify a/b/mtime of 'pathname'. */
1010 /* If 'recent', verify that it's within last 10 seconds. */
1012 assertion_file_time(const char *file
, int line
,
1013 const char *pathname
, long t
, long nsec
, char type
, int recent
)
1015 long long filet
, filet_nsec
;
1018 #if defined(_WIN32) && !defined(__CYGWIN__)
1019 #define EPOC_TIME (116444736000000000ULL)
1020 FILETIME ftime
, fbirthtime
, fatime
, fmtime
;
1021 ULARGE_INTEGER wintm
;
1023 ftime
.dwLowDateTime
= 0;
1024 ftime
.dwHighDateTime
= 0;
1026 assertion_count(file
, line
);
1027 h
= CreateFile(pathname
, FILE_READ_ATTRIBUTES
, 0, NULL
,
1028 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1029 if (h
== INVALID_HANDLE_VALUE
) {
1030 failure_start(file
, line
, "Can't access %s\n", pathname
);
1031 failure_finish(NULL
);
1034 r
= GetFileTime(h
, &fbirthtime
, &fatime
, &fmtime
);
1036 case 'a': ftime
= fatime
; break;
1037 case 'b': ftime
= fbirthtime
; break;
1038 case 'm': ftime
= fmtime
; break;
1042 failure_start(file
, line
, "Can't GetFileTime %s\n", pathname
);
1043 failure_finish(NULL
);
1046 wintm
.LowPart
= ftime
.dwLowDateTime
;
1047 wintm
.HighPart
= ftime
.dwHighDateTime
;
1048 filet
= (wintm
.QuadPart
- EPOC_TIME
) / 10000000;
1049 filet_nsec
= ((wintm
.QuadPart
- EPOC_TIME
) % 10000000) * 100;
1050 nsec
= (nsec
/ 100) * 100; /* Round the request */
1054 assertion_count(file
, line
);
1055 r
= lstat(pathname
, &st
);
1057 failure_start(file
, line
, "Can't stat %s\n", pathname
);
1058 failure_finish(NULL
);
1062 case 'a': filet
= st
.st_atime
; break;
1063 case 'm': filet
= st
.st_mtime
; break;
1064 case 'b': filet
= 0; break;
1065 default: fprintf(stderr
, "INTERNAL: Bad type %c for file time", type
);
1068 #if defined(__FreeBSD__)
1070 case 'a': filet_nsec
= st
.st_atimespec
.tv_nsec
; break;
1071 case 'b': filet
= st
.st_birthtime
;
1072 filet_nsec
= st
.st_birthtimespec
.tv_nsec
; break;
1073 case 'm': filet_nsec
= st
.st_mtimespec
.tv_nsec
; break;
1074 default: fprintf(stderr
, "INTERNAL: Bad type %c for file time", type
);
1077 /* FreeBSD generally only stores to microsecond res, so round. */
1078 filet_nsec
= (filet_nsec
/ 1000) * 1000;
1079 nsec
= (nsec
/ 1000) * 1000;
1081 filet_nsec
= nsec
= 0; /* Generic POSIX only has whole seconds. */
1082 if (type
== 'b') return (1); /* Generic POSIX doesn't have birthtime */
1083 #if defined(__HAIKU__)
1084 if (type
== 'a') return (1); /* Haiku doesn't have atime. */
1089 /* Check that requested time is up-to-date. */
1090 time_t now
= time(NULL
);
1091 if (filet
< now
- 10 || filet
> now
+ 1) {
1092 failure_start(file
, line
,
1093 "File %s has %ctime %ld, %ld seconds ago\n",
1094 pathname
, type
, filet
, now
- filet
);
1095 failure_finish(NULL
);
1098 } else if (filet
!= t
|| filet_nsec
!= nsec
) {
1099 failure_start(file
, line
,
1100 "File %s has %ctime %ld.%09ld, expected %ld.%09ld",
1101 pathname
, type
, filet
, filet_nsec
, t
, nsec
);
1102 failure_finish(NULL
);
1108 /* Verify atime of 'pathname'. */
1110 assertion_file_atime(const char *file
, int line
,
1111 const char *pathname
, long t
, long nsec
)
1113 return assertion_file_time(file
, line
, pathname
, t
, nsec
, 'a', 0);
1116 /* Verify atime of 'pathname' is up-to-date. */
1118 assertion_file_atime_recent(const char *file
, int line
, const char *pathname
)
1120 return assertion_file_time(file
, line
, pathname
, 0, 0, 'a', 1);
1123 /* Verify birthtime of 'pathname'. */
1125 assertion_file_birthtime(const char *file
, int line
,
1126 const char *pathname
, long t
, long nsec
)
1128 return assertion_file_time(file
, line
, pathname
, t
, nsec
, 'b', 0);
1131 /* Verify birthtime of 'pathname' is up-to-date. */
1133 assertion_file_birthtime_recent(const char *file
, int line
,
1134 const char *pathname
)
1136 return assertion_file_time(file
, line
, pathname
, 0, 0, 'b', 1);
1139 /* Verify mtime of 'pathname'. */
1141 assertion_file_mtime(const char *file
, int line
,
1142 const char *pathname
, long t
, long nsec
)
1144 return assertion_file_time(file
, line
, pathname
, t
, nsec
, 'm', 0);
1147 /* Verify mtime of 'pathname' is up-to-date. */
1149 assertion_file_mtime_recent(const char *file
, int line
, const char *pathname
)
1151 return assertion_file_time(file
, line
, pathname
, 0, 0, 'm', 1);
1154 /* Verify number of links to 'pathname'. */
1156 assertion_file_nlinks(const char *file
, int line
,
1157 const char *pathname
, int nlinks
)
1159 #if defined(_WIN32) && !defined(__CYGWIN__)
1160 BY_HANDLE_FILE_INFORMATION bhfi
;
1163 assertion_count(file
, line
);
1164 r
= my_GetFileInformationByName(pathname
, &bhfi
);
1165 if (r
!= 0 && bhfi
.nNumberOfLinks
== (DWORD
)nlinks
)
1167 failure_start(file
, line
, "File %s has %d links, expected %d",
1168 pathname
, bhfi
.nNumberOfLinks
, nlinks
);
1169 failure_finish(NULL
);
1175 assertion_count(file
, line
);
1176 r
= lstat(pathname
, &st
);
1177 if (r
== 0 && st
.st_nlink
== nlinks
)
1179 failure_start(file
, line
, "File %s has %d links, expected %d",
1180 pathname
, st
.st_nlink
, nlinks
);
1181 failure_finish(NULL
);
1186 /* Verify size of 'pathname'. */
1188 assertion_file_size(const char *file
, int line
, const char *pathname
, long size
)
1193 assertion_count(file
, line
);
1194 #if defined(_WIN32) && !defined(__CYGWIN__)
1196 BY_HANDLE_FILE_INFORMATION bhfi
;
1197 r
= !my_GetFileInformationByName(pathname
, &bhfi
);
1198 filesize
= ((int64_t)bhfi
.nFileSizeHigh
<< 32) + bhfi
.nFileSizeLow
;
1203 r
= lstat(pathname
, &st
);
1204 filesize
= st
.st_size
;
1207 if (r
== 0 && filesize
== size
)
1209 failure_start(file
, line
, "File %s has size %ld, expected %ld",
1210 pathname
, (long)filesize
, (long)size
);
1211 failure_finish(NULL
);
1215 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1217 assertion_is_dir(const char *file
, int line
, const char *pathname
, int mode
)
1222 #if defined(_WIN32) && !defined(__CYGWIN__)
1223 (void)mode
; /* UNUSED */
1225 assertion_count(file
, line
);
1226 r
= lstat(pathname
, &st
);
1228 failure_start(file
, line
, "Dir should exist: %s", pathname
);
1229 failure_finish(NULL
);
1232 if (!S_ISDIR(st
.st_mode
)) {
1233 failure_start(file
, line
, "%s is not a dir", pathname
);
1234 failure_finish(NULL
);
1237 #if !defined(_WIN32) || defined(__CYGWIN__)
1238 /* Windows doesn't handle permissions the same way as POSIX,
1239 * so just ignore the mode tests. */
1240 /* TODO: Can we do better here? */
1241 if (mode
>= 0 && mode
!= (st
.st_mode
& 07777)) {
1242 failure_start(file
, line
, "Dir %s has wrong mode", pathname
);
1243 logprintf(" Expected: 0%3o\n", mode
);
1244 logprintf(" Found: 0%3o\n", st
.st_mode
& 07777);
1245 failure_finish(NULL
);
1252 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1253 * verify that too. */
1255 assertion_is_reg(const char *file
, int line
, const char *pathname
, int mode
)
1260 #if defined(_WIN32) && !defined(__CYGWIN__)
1261 (void)mode
; /* UNUSED */
1263 assertion_count(file
, line
);
1264 r
= lstat(pathname
, &st
);
1265 if (r
!= 0 || !S_ISREG(st
.st_mode
)) {
1266 failure_start(file
, line
, "File should exist: %s", pathname
);
1267 failure_finish(NULL
);
1270 #if !defined(_WIN32) || defined(__CYGWIN__)
1271 /* Windows doesn't handle permissions the same way as POSIX,
1272 * so just ignore the mode tests. */
1273 /* TODO: Can we do better here? */
1274 if (mode
>= 0 && mode
!= (st
.st_mode
& 07777)) {
1275 failure_start(file
, line
, "File %s has wrong mode", pathname
);
1276 logprintf(" Expected: 0%3o\n", mode
);
1277 logprintf(" Found: 0%3o\n", st
.st_mode
& 07777);
1278 failure_finish(NULL
);
1285 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1286 * non-NULL, verify that the symlink has those contents. */
1288 is_symlink(const char *file
, int line
,
1289 const char *pathname
, const char *contents
)
1291 #if defined(_WIN32) && !defined(__CYGWIN__)
1292 (void)pathname
; /* UNUSED */
1293 (void)contents
; /* UNUSED */
1294 assertion_count(file
, line
);
1295 /* Windows sort-of has real symlinks, but they're only usable
1296 * by privileged users and are crippled even then, so there's
1297 * really not much point in bothering with this. */
1305 assertion_count(file
, line
);
1306 r
= lstat(pathname
, &st
);
1308 failure_start(file
, line
,
1309 "Symlink should exist: %s", pathname
);
1310 failure_finish(NULL
);
1313 if (!S_ISLNK(st
.st_mode
))
1315 if (contents
== NULL
)
1317 linklen
= readlink(pathname
, buff
, sizeof(buff
));
1319 failure_start(file
, line
, "Can't read symlink %s", pathname
);
1320 failure_finish(NULL
);
1323 buff
[linklen
] = '\0';
1324 if (strcmp(buff
, contents
) != 0)
1330 /* Assert that path is a symlink that (optionally) contains contents. */
1332 assertion_is_symlink(const char *file
, int line
,
1333 const char *path
, const char *contents
)
1335 if (is_symlink(file
, line
, path
, contents
))
1338 failure_start(file
, line
, "File %s is not a symlink to %s",
1341 failure_start(file
, line
, "File %s is not a symlink", path
);
1342 failure_finish(NULL
);
1347 /* Create a directory and report any errors. */
1349 assertion_make_dir(const char *file
, int line
, const char *dirname
, int mode
)
1351 assertion_count(file
, line
);
1352 #if defined(_WIN32) && !defined(__CYGWIN__)
1353 (void)mode
; /* UNUSED */
1354 if (0 == _mkdir(dirname
))
1357 if (0 == mkdir(dirname
, mode
))
1360 failure_start(file
, line
, "Could not create directory %s", dirname
);
1361 failure_finish(NULL
);
1365 /* Create a file with the specified contents and report any failures. */
1367 assertion_make_file(const char *file
, int line
,
1368 const char *path
, int mode
, const char *contents
)
1370 #if defined(_WIN32) && !defined(__CYGWIN__)
1371 /* TODO: Rework this to set file mode as well. */
1373 (void)mode
; /* UNUSED */
1374 assertion_count(file
, line
);
1375 f
= fopen(path
, "wb");
1377 failure_start(file
, line
, "Could not create file %s", path
);
1378 failure_finish(NULL
);
1381 if (contents
!= NULL
) {
1382 if (strlen(contents
)
1383 != fwrite(contents
, 1, strlen(contents
), f
)) {
1385 failure_start(file
, line
,
1386 "Could not write file %s", path
);
1387 failure_finish(NULL
);
1395 assertion_count(file
, line
);
1396 fd
= open(path
, O_CREAT
| O_WRONLY
, mode
>= 0 ? mode
: 0644);
1398 failure_start(file
, line
, "Could not create %s", path
);
1399 failure_finish(NULL
);
1402 if (contents
!= NULL
) {
1403 if ((ssize_t
)strlen(contents
)
1404 != write(fd
, contents
, strlen(contents
))) {
1406 failure_start(file
, line
, "Could not write to %s", path
);
1407 failure_finish(NULL
);
1416 /* Create a hardlink and report any failures. */
1418 assertion_make_hardlink(const char *file
, int line
,
1419 const char *newpath
, const char *linkto
)
1423 assertion_count(file
, line
);
1424 #if defined(_WIN32) && !defined(__CYGWIN__)
1425 succeeded
= my_CreateHardLinkA(newpath
, linkto
);
1427 succeeded
= !link(linkto
, newpath
);
1433 failure_start(file
, line
, "Could not create hardlink");
1434 logprintf(" New link: %s\n", newpath
);
1435 logprintf(" Old name: %s\n", linkto
);
1436 failure_finish(NULL
);
1440 /* Create a symlink and report any failures. */
1442 assertion_make_symlink(const char *file
, int line
,
1443 const char *newpath
, const char *linkto
)
1445 #if defined(_WIN32) && !defined(__CYGWIN__)
1446 int targetIsDir
= 0; /* TODO: Fix this */
1447 assertion_count(file
, line
);
1448 if (my_CreateSymbolicLinkA(newpath
, linkto
, targetIsDir
))
1451 assertion_count(file
, line
);
1452 if (0 == symlink(linkto
, newpath
))
1455 failure_start(file
, line
, "Could not create symlink");
1456 logprintf(" New link: %s\n", newpath
);
1457 logprintf(" Old name: %s\n", linkto
);
1458 failure_finish(NULL
);
1462 /* Set umask, report failures. */
1464 assertion_umask(const char *file
, int line
, int mask
)
1466 assertion_count(file
, line
);
1467 (void)file
; /* UNUSED */
1468 (void)line
; /* UNUSED */
1475 * UTILITIES for use by tests.
1480 * Check whether platform supports symlinks. This is intended
1481 * for tests to use in deciding whether to bother testing symlink
1482 * support; if the platform doesn't support symlinks, there's no point
1483 * in checking whether the program being tested can create them.
1485 * Note that the first time this test is called, we actually go out to
1486 * disk to create and verify a symlink. This is necessary because
1487 * symlink support is actually a property of a particular filesystem
1488 * and can thus vary between directories on a single system. After
1489 * the first call, this returns the cached result from memory, so it's
1490 * safe to call it as often as you wish.
1495 /* Remember the test result */
1496 static int value
= 0, tested
= 0;
1501 assertion_make_file(__FILE__
, __LINE__
, "canSymlink.0", 0644, "a");
1502 /* Note: Cygwin has its own symlink() emulation that does not
1503 * use the Win32 CreateSymbolicLink() function. */
1504 #if defined(_WIN32) && !defined(__CYGWIN__)
1505 value
= my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1506 && is_symlink(__FILE__
, __LINE__
, "canSymlink.1", "canSymlink.0");
1508 value
= (0 == symlink("canSymlink.0", "canSymlink.1"))
1509 && is_symlink(__FILE__
, __LINE__
, "canSymlink.1","canSymlink.0");
1515 * Can this platform run the gzip program?
1517 /* Platform-dependent options for hiding the output of a subcommand. */
1518 #if defined(_WIN32) && !defined(__CYGWIN__)
1519 static const char *redirectArgs
= ">NUL 2>NUL"; /* Win32 cmd.exe */
1521 static const char *redirectArgs
= ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1526 static int tested
= 0, value
= 0;
1529 if (systemf("gzip -V %s", redirectArgs
) == 0)
1536 * Can this platform run the gunzip program?
1541 static int tested
= 0, value
= 0;
1544 if (systemf("gunzip -V %s", redirectArgs
) == 0)
1551 * Sleep as needed; useful for verifying disk timestamp changes by
1552 * ensuring that the wall-clock time has actually changed before we
1553 * go back to re-read something from disk.
1556 sleepUntilAfter(time_t t
)
1558 while (t
>= time(NULL
))
1559 #if defined(_WIN32) && !defined(__CYGWIN__)
1567 * Call standard system() call, but build up the command line using
1568 * sprintf() conventions.
1571 systemf(const char *fmt
, ...)
1578 vsprintf(buff
, fmt
, ap
);
1579 if (verbosity
> VERBOSITY_FULL
)
1580 logprintf("Cmd: %s\n", buff
);
1587 * Slurp a file into memory for ease of comparison and testing.
1588 * Returns size of file in 'sizep' if non-NULL, null-terminates
1589 * data in memory for ease of use.
1592 slurpfile(size_t * sizep
, const char *fmt
, ...)
1594 char filename
[8192];
1603 vsprintf(filename
, fmt
, ap
);
1606 f
= fopen(filename
, "rb");
1608 /* Note: No error; non-existent file is okay here. */
1611 r
= fstat(fileno(f
), &st
);
1613 logprintf("Can't stat file %s\n", filename
);
1617 p
= malloc((size_t)st
.st_size
+ 1);
1619 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
1620 (long int)st
.st_size
, filename
);
1624 bytes_read
= fread(p
, 1, (size_t)st
.st_size
, f
);
1625 if (bytes_read
< st
.st_size
) {
1626 logprintf("Can't read file %s\n", filename
);
1631 p
[st
.st_size
] = '\0';
1633 *sizep
= (size_t)st
.st_size
;
1638 /* Read a uuencoded file from the reference directory, decode, and
1639 * write the result into the current directory. */
1640 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
1642 extract_reference_file(const char *name
)
1647 sprintf(buff
, "%s/%s.uu", refdir
, name
);
1648 in
= fopen(buff
, "r");
1649 failure("Couldn't open reference file %s", buff
);
1653 /* Read up to and including the 'begin' line. */
1655 if (fgets(buff
, sizeof(buff
), in
) == NULL
) {
1656 /* TODO: This is a failure. */
1659 if (memcmp(buff
, "begin ", 6) == 0)
1662 /* Now, decode the rest and write it. */
1663 /* Not a lot of error checking here; the input better be right. */
1664 out
= fopen(name
, "wb");
1665 while (fgets(buff
, sizeof(buff
), in
) != NULL
) {
1669 if (memcmp(buff
, "end", 3) == 0)
1672 bytes
= UUDECODE(*p
++);
1675 /* Write out 1-3 bytes from that. */
1677 n
= UUDECODE(*p
++) << 18;
1678 n
|= UUDECODE(*p
++) << 12;
1679 fputc(n
>> 16, out
);
1683 n
|= UUDECODE(*p
++) << 6;
1684 fputc((n
>> 8) & 0xFF, out
);
1688 n
|= UUDECODE(*p
++);
1689 fputc(n
& 0xFF, out
);
1705 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
1707 * DEFINE_TEST(test_function)
1711 /* Use "list.h" to declare all of the test functions. */
1713 #define DEFINE_TEST(name) void name(void);
1716 /* Use "list.h" to create a list of all tests (functions and names). */
1718 #define DEFINE_TEST(n) { n, #n, 0 },
1719 struct { void (*func
)(void); const char *name
; int failures
; } tests
[] = {
1724 * Summarize repeated failures in the just-completed test.
1727 test_summarize(const char *filename
, int failed
)
1731 switch (verbosity
) {
1732 case VERBOSITY_SUMMARY_ONLY
:
1733 printf(failed
? "E" : ".");
1736 case VERBOSITY_PASSFAIL
:
1737 printf(failed
? "FAIL\n" : "ok\n");
1741 log_console
= (verbosity
== VERBOSITY_LIGHT_REPORT
);
1743 for (i
= 0; i
< sizeof(failed_lines
)/sizeof(failed_lines
[0]); i
++) {
1744 if (failed_lines
[i
].count
> 1 && !failed_lines
[i
].skip
)
1745 logprintf("%s:%d: Summary: Failed %d times\n",
1746 filename
, i
, failed_lines
[i
].count
);
1748 /* Clear the failure history for the next file. */
1749 memset(failed_lines
, 0, sizeof(failed_lines
));
1753 * Actually run a single test, with appropriate setup and cleanup.
1756 test_run(int i
, const char *tmpdir
)
1758 char logfilename
[64];
1759 int failures_before
= failures
;
1762 switch (verbosity
) {
1763 case VERBOSITY_SUMMARY_ONLY
: /* No per-test reports at all */
1765 case VERBOSITY_PASSFAIL
: /* rest of line will include ok/FAIL marker */
1766 printf("%3d: %-50s", i
, tests
[i
].name
);
1769 default: /* Title of test, details will follow */
1770 printf("%3d: %s\n", i
, tests
[i
].name
);
1773 /* Chdir to the top-level work directory. */
1774 if (!assertChdir(tmpdir
)) {
1776 "ERROR: Can't chdir to top work dir %s\n", tmpdir
);
1779 /* Create a log file for this test. */
1780 sprintf(logfilename
, "%s.log", tests
[i
].name
);
1781 logfile
= fopen(logfilename
, "w");
1782 fprintf(logfile
, "%s\n\n", tests
[i
].name
);
1783 /* Chdir() to a work dir for this specific test. */
1784 if (!assertMakeDir(tests
[i
].name
, 0755)
1785 || !assertChdir(tests
[i
].name
)) {
1787 "ERROR: Can't chdir to work dir %s/%s\n",
1788 tmpdir
, tests
[i
].name
);
1791 /* Explicitly reset the locale before each test. */
1792 setlocale(LC_ALL
, "C");
1793 /* Record the umask before we run the test. */
1794 umask(oldumask
= umask(0));
1796 * Run the actual test.
1800 * Clean up and report afterwards.
1805 setlocale(LC_ALL
, "C");
1806 /* Reset directory. */
1807 if (!assertChdir(tmpdir
)) {
1808 fprintf(stderr
, "ERROR: Couldn't chdir to temp dir %s\n",
1812 /* Report per-test summaries. */
1813 tests
[i
].failures
= failures
- failures_before
;
1814 test_summarize(test_filename
, tests
[i
].failures
);
1815 /* Close the per-test log file. */
1818 /* If there were no failures, we can remove the work dir and logfile. */
1819 if (tests
[i
].failures
== 0) {
1820 if (!keep_temp_files
&& assertChdir(tmpdir
)) {
1821 #if defined(_WIN32) && !defined(__CYGWIN__)
1822 /* Make sure not to leave empty directories.
1823 * Sometimes a processing of closing files used by tests
1824 * is not done, then rmdir will be failed and it will
1825 * leave a empty test directory. So we should wait a few
1826 * seconds and retry rmdir. */
1828 for (t
= 0; t
< 10; t
++) {
1831 r
= systemf("rmdir /S /Q %s", tests
[i
].name
);
1835 systemf("del %s", logfilename
);
1837 systemf("rm -rf %s", tests
[i
].name
);
1838 systemf("rm %s", logfilename
);
1842 /* Return appropriate status. */
1843 return (tests
[i
].failures
);
1849 * MAIN and support routines.
1855 usage(const char *program
)
1857 static const int limit
= sizeof(tests
) / sizeof(tests
[0]);
1860 printf("Usage: %s [options] <test> <test> ...\n", program
);
1861 printf("Default is to run all tests.\n");
1862 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
1863 printf("Options:\n");
1864 printf(" -d Dump core after any failure, for debugging.\n");
1865 printf(" -k Keep all temp files.\n");
1866 printf(" Default: temp files for successful tests deleted.\n");
1868 printf(" -p <path> Path to executable to be tested.\n");
1869 printf(" Default: path taken from " ENVBASE
" environment variable.\n");
1871 printf(" -q Quiet.\n");
1872 printf(" -r <dir> Path to dir containing reference files.\n");
1873 printf(" Default: Current directory.\n");
1874 printf(" -v Verbose.\n");
1875 printf("Available tests:\n");
1876 for (i
= 0; i
< limit
; i
++)
1877 printf(" %d: %s\n", i
, tests
[i
].name
);
1882 get_refdir(const char *d
)
1884 char tried
[512] = { '\0' };
1888 /* If a dir was specified, try that */
1891 snprintf(buff
, sizeof(buff
), "%s", d
);
1892 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1893 if (p
!= NULL
) goto success
;
1894 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1895 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1899 /* Get the current dir. */
1900 pwd
= getcwd(NULL
, 0);
1901 while (pwd
[strlen(pwd
) - 1] == '\n')
1902 pwd
[strlen(pwd
) - 1] = '\0';
1904 /* Look for a known file. */
1905 snprintf(buff
, sizeof(buff
), "%s", pwd
);
1906 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1907 if (p
!= NULL
) goto success
;
1908 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1909 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1911 snprintf(buff
, sizeof(buff
), "%s/test", pwd
);
1912 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1913 if (p
!= NULL
) goto success
;
1914 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1915 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1917 #if defined(LIBRARY)
1918 snprintf(buff
, sizeof(buff
), "%s/%s/test", pwd
, LIBRARY
);
1920 snprintf(buff
, sizeof(buff
), "%s/%s/test", pwd
, PROGRAM
);
1922 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1923 if (p
!= NULL
) goto success
;
1924 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1925 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1927 if (memcmp(pwd
, "/usr/obj", 8) == 0) {
1928 snprintf(buff
, sizeof(buff
), "%s", pwd
+ 8);
1929 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1930 if (p
!= NULL
) goto success
;
1931 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1932 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1934 snprintf(buff
, sizeof(buff
), "%s/test", pwd
+ 8);
1935 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1936 if (p
!= NULL
) goto success
;
1937 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1938 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1942 printf("Unable to locate known reference file %s\n", KNOWNREF
);
1943 printf(" Checked following directories:\n%s\n", tried
);
1944 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
1952 return strdup(buff
);
1956 main(int argc
, char **argv
)
1958 static const int limit
= sizeof(tests
) / sizeof(tests
[0]);
1959 int i
, tests_run
= 0, tests_failed
= 0, option
;
1961 char *refdir_alloc
= NULL
;
1962 const char *progname
;
1963 const char *tmp
, *option_arg
, *p
;
1965 char tmpdir_timestamp
[256];
1967 (void)argc
; /* UNUSED */
1969 #if defined(HAVE__CrtSetReportMode)
1970 /* To stop to run the default invalid parameter handler. */
1971 _set_invalid_parameter_handler(invalid_parameter_handler
);
1972 /* Disable annoying assertion message box. */
1973 _CrtSetReportMode(_CRT_ASSERT
, 0);
1977 * Name of this program, used to build root of our temp directory
1980 progname
= p
= argv
[0];
1981 while (*p
!= '\0') {
1982 /* Support \ or / dir separators for Windows compat. */
1983 if (*p
== '/' || *p
== '\\')
1989 /* Get the target program from environment, if available. */
1990 testprogfile
= getenv(ENVBASE
);
1993 if (getenv("TMPDIR") != NULL
)
1994 tmp
= getenv("TMPDIR");
1995 else if (getenv("TMP") != NULL
)
1996 tmp
= getenv("TMP");
1997 else if (getenv("TEMP") != NULL
)
1998 tmp
= getenv("TEMP");
1999 else if (getenv("TEMPDIR") != NULL
)
2000 tmp
= getenv("TEMPDIR");
2004 /* Allow -d to be controlled through the environment. */
2005 if (getenv(ENVBASE
"_DEBUG") != NULL
)
2006 dump_on_failure
= 1;
2008 /* Get the directory holding test files from environment. */
2009 refdir
= getenv(ENVBASE
"_TEST_FILES");
2012 * Parse options, without using getopt(), which isn't available
2015 ++argv
; /* Skip program name */
2016 while (*argv
!= NULL
) {
2021 while (*p
!= '\0') {
2024 /* If 'opt' takes an argument, parse that. */
2025 if (option
== 'p' || option
== 'r') {
2028 else if (*argv
== NULL
) {
2030 "Option -%c requires argument.\n",
2034 option_arg
= *argv
++;
2035 p
= ""; /* End of this option word. */
2038 /* Now, handle the option. */
2041 dump_on_failure
= 1;
2044 keep_temp_files
= 1;
2048 testprogfile
= option_arg
;
2050 fprintf(stderr
, "-p option not permitted\n");
2058 refdir
= option_arg
;
2064 fprintf(stderr
, "Unrecognized option '%c'\n",
2072 * Sanity-check that our options make sense.
2075 if (testprogfile
== NULL
) {
2076 fprintf(stderr
, "Program executable required\n");
2082 #if defined(_WIN32) && !defined(__CYGWIN__)
2083 /* Command.com sometimes rejects '/' separators. */
2084 testprg
= strdup(testprogfile
);
2085 for (i
= 0; testprg
[i
] != '\0'; i
++) {
2086 if (testprg
[i
] == '/')
2089 testprogfile
= testprg
;
2091 /* Quote the name that gets put into shell command lines. */
2092 testprg
= malloc(strlen(testprogfile
) + 3);
2093 strcpy(testprg
, "\"");
2094 strcat(testprg
, testprogfile
);
2095 strcat(testprg
, "\"");
2101 * Create a temp directory for the following tests.
2102 * Include the time the tests started as part of the name,
2103 * to make it easier to track the results of multiple tests.
2106 for (i
= 0; ; i
++) {
2107 strftime(tmpdir_timestamp
, sizeof(tmpdir_timestamp
),
2108 "%Y-%m-%dT%H.%M.%S",
2110 sprintf(tmpdir
, "%s/%s.%s-%03d", tmp
, progname
,
2111 tmpdir_timestamp
, i
);
2112 if (assertMakeDir(tmpdir
,0755))
2116 "ERROR: Unable to create temp directory %s\n",
2123 * If the user didn't specify a directory for locating
2124 * reference files, try to find the reference files in
2125 * the "usual places."
2127 refdir
= refdir_alloc
= get_refdir(refdir
);
2130 * Banner with basic information.
2133 printf("If tests fail or crash, details will be in:\n");
2134 printf(" %s\n", tmpdir
);
2136 if (verbosity
> VERBOSITY_SUMMARY_ONLY
) {
2137 printf("Reference files will be read from: %s\n", refdir
);
2139 printf("Running tests on: %s\n", testprog
);
2141 printf("Exercising: ");
2143 printf("%s\n", EXTRA_VERSION
);
2150 * Run some or all of the individual tests.
2152 if (*argv
== NULL
) {
2153 /* Default: Run all tests. */
2154 for (i
= 0; i
< limit
; i
++) {
2155 if (test_run(i
, tmpdir
))
2160 while (*(argv
) != NULL
) {
2161 if (**argv
>= '0' && **argv
<= '9') {
2163 if (i
< 0 || i
>= limit
) {
2164 printf("*** INVALID Test %s\n", *argv
);
2167 /* usage() never returns */
2170 for (i
= 0; i
< limit
; ++i
) {
2171 if (strcmp(*argv
, tests
[i
].name
) == 0)
2175 printf("*** INVALID Test ``%s''\n",
2179 /* usage() never returns */
2182 if (test_run(i
, tmpdir
))
2190 * Report summary statistics.
2192 if (verbosity
> VERBOSITY_SUMMARY_ONLY
) {
2194 printf("Totals:\n");
2195 printf(" Tests run: %8d\n", tests_run
);
2196 printf(" Tests failed: %8d\n", tests_failed
);
2197 printf(" Assertions checked:%8d\n", assertions
);
2198 printf(" Assertions failed: %8d\n", failures
);
2199 printf(" Skips reported: %8d\n", skips
);
2203 printf("Failing tests:\n");
2204 for (i
= 0; i
< limit
; ++i
) {
2205 if (tests
[i
].failures
)
2206 printf(" %d: %s (%d failures)\n", i
,
2207 tests
[i
].name
, tests
[i
].failures
);
2210 printf("Details for failing tests: %s\n", tmpdir
);
2213 if (verbosity
== VERBOSITY_SUMMARY_ONLY
)
2215 printf("%d tests passed, no failures\n", tests_run
);
2220 /* If the final tmpdir is empty, we can remove it. */
2221 /* This should be the usual case when all tests succeed. */
2225 return (tests_failed
? 1 : 0);