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/cpio/test/main.c,v 1.3 2008/08/24 04:58:22 kientzle Exp $");
40 #define KNOWNREF "test_option_f.cpio.uu"
41 #define ENVBASE "BSDCPIO" /* Prefix for environment variables. */
42 #define PROGRAM "bsdcpio" /* 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
84 #define fileno _fileno
86 /*#define fstat _fstat64*/
87 #if !defined(__BORLANDC__)
88 #define getcwd _getcwd
91 /*#define lstat _stat64*/
92 /*#define stat _stat64*/
94 #if !defined(__BORLANDC__)
95 #define strdup _strdup
98 #define int64_t __int64
101 #if defined(HAVE__CrtSetReportMode)
105 #if defined(_WIN32) && !defined(__CYGWIN__)
106 void *GetFunctionKernel32(const char *name
)
108 static HINSTANCE lib
;
112 lib
= LoadLibrary("kernel32.dll");
115 fprintf(stderr
, "Can't load kernel32.dll?!\n");
118 return (void *)GetProcAddress(lib
, name
);
122 my_CreateSymbolicLinkA(const char *linkname
, const char *target
, int flags
)
124 static BOOLEAN (WINAPI
*f
)(LPCSTR
, LPCSTR
, DWORD
);
128 f
= GetFunctionKernel32("CreateSymbolicLinkA");
130 return f
== NULL
? 0 : (*f
)(linkname
, target
, flags
);
134 my_CreateHardLinkA(const char *linkname
, const char *target
)
136 static BOOLEAN (WINAPI
*f
)(LPCSTR
, LPCSTR
, LPSECURITY_ATTRIBUTES
);
140 f
= GetFunctionKernel32("CreateHardLinkA");
142 return f
== NULL
? 0 : (*f
)(linkname
, target
, NULL
);
146 my_GetFileInformationByName(const char *path
, BY_HANDLE_FILE_INFORMATION
*bhfi
)
151 memset(bhfi
, 0, sizeof(*bhfi
));
152 h
= CreateFile(path
, FILE_READ_ATTRIBUTES
, 0, NULL
,
153 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
154 if (h
== INVALID_HANDLE_VALUE
)
156 r
= GetFileInformationByHandle(h
, bhfi
);
162 #if defined(HAVE__CrtSetReportMode)
164 invalid_parameter_handler(const wchar_t * expression
,
165 const wchar_t * function
, const wchar_t * file
,
166 unsigned int line
, uintptr_t pReserved
)
178 /* Enable core dump on failure. */
179 static int dump_on_failure
= 0;
180 /* Default is to remove temp dirs and log data for successful tests. */
181 static int keep_temp_files
= 0;
182 /* Default is to just report pass/fail for each test. */
183 static int verbosity
= 0;
184 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
185 #define VERBOSITY_PASSFAIL 0 /* Default */
186 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
187 #define VERBOSITY_FULL 2 /* -vv */
188 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
189 * mostly for debugging the test harness itself. */
190 /* Cumulative count of assertion failures. */
191 static int failures
= 0;
192 /* Cumulative count of reported skips. */
193 static int skips
= 0;
194 /* Cumulative count of assertions checked. */
195 static int assertions
= 0;
197 /* Directory where uuencoded reference files can be found. */
198 static const char *refdir
;
201 * Report log information selectively to console and/or disk log.
203 static int log_console
= 0;
204 static FILE *logfile
;
206 vlogprintf(const char *fmt
, va_list ap
)
213 vfprintf(stdout
, fmt
, ap
);
216 vfprintf(logfile
, fmt
, lfap
);
219 vfprintf(logfile
, fmt
, ap
);
224 logprintf(const char *fmt
, ...)
232 /* Set up a message to display only if next assertion fails. */
233 static char msgbuff
[4096];
234 static const char *msg
, *nextmsg
;
236 failure(const char *fmt
, ...)
240 vsprintf(msgbuff
, fmt
, ap
);
246 * Copy arguments into file-local variables.
247 * This was added to permit vararg assert() functions without needing
248 * variadic wrapper macros. Turns out that the vararg capability is almost
249 * never used, so almost all of the vararg assertions can be simplified
250 * by removing the vararg capability and reworking the wrapper macro to
251 * pass __FILE__, __LINE__ directly into the function instead of using
252 * this hook. I suspect this machinery is used so rarely that we
253 * would be better off just removing it entirely. That would simplify
254 * the code here noticably.
256 static const char *test_filename
;
257 static int test_line
;
258 static void *test_extra
;
259 void assertion_setup(const char *filename
, int line
)
261 test_filename
= filename
;
265 /* Called at the beginning of each assert() function. */
267 assertion_count(const char *file
, int line
)
269 (void)file
; /* UNUSED */
270 (void)line
; /* UNUSED */
272 /* Proper handling of "failure()" message. */
275 /* Uncomment to print file:line after every assertion.
276 * Verbose, but occasionally useful in tracking down crashes. */
277 /* printf("Checked %s:%d\n", file, line); */
281 * For each test source file, we remember how many times each
282 * assertion was reported. Cleared before each new test,
283 * used by test_summarize().
288 } failed_lines
[10000];
290 /* Count this failure, setup up log destination and handle initial report. */
292 failure_start(const char *filename
, int line
, const char *fmt
, ...)
296 /* Record another failure for this line. */
298 /* test_filename = filename; */
299 failed_lines
[line
].count
++;
301 /* Determine whether to log header to console. */
306 case VERBOSITY_LIGHT_REPORT
:
307 log_console
= (failed_lines
[line
].count
< 2);
313 /* Log file:line header for this failure */
316 logprintf("%s(%d): ", filename
, line
);
318 logprintf("%s:%d: ", filename
, line
);
324 if (msg
!= NULL
&& msg
[0] != '\0') {
325 logprintf(" Description: %s\n", msg
);
329 /* Determine whether to log details to console. */
330 if (verbosity
== VERBOSITY_LIGHT_REPORT
)
334 /* Complete reporting of failed tests. */
336 * The 'extra' hook here is used by libarchive to include libarchive
337 * error messages with assertion failures. It could also be used
338 * to add strerror() output, for example. Just define the EXTRA_DUMP()
339 * macro appropriately.
342 failure_finish(void *extra
)
344 (void)extra
; /* UNUSED (maybe) */
347 logprintf(" detail: %s\n", EXTRA_DUMP(extra
));
350 if (dump_on_failure
) {
352 " *** forcing core dump so failure can be debugged ***\n");
358 /* Inform user that we're skipping some checks. */
360 test_skipping(const char *fmt
, ...)
366 vsprintf(buff
, fmt
, ap
);
368 /* failure_start() isn't quite right, but is awfully convenient. */
369 failure_start(test_filename
, test_line
, "SKIPPING: %s", buff
);
370 --failures
; /* Undo failures++ in failure_start() */
371 /* Don't failure_finish() here. */
372 /* Mark as skip, so doesn't count as failed test. */
373 failed_lines
[test_line
].skip
= 1;
383 /* Generic assert() just displays the failed condition. */
385 assertion_assert(const char *file
, int line
, int value
,
386 const char *condition
, void *extra
)
388 assertion_count(file
, line
);
390 failure_start(file
, line
, "Assertion failed: %s", condition
);
391 failure_finish(extra
);
396 /* chdir() and report any errors */
398 assertion_chdir(const char *file
, int line
, const char *pathname
)
400 assertion_count(file
, line
);
401 if (chdir(pathname
) == 0)
403 failure_start(file
, line
, "chdir(\"%s\")", pathname
);
404 failure_finish(NULL
);
409 /* Verify two integers are equal. */
411 assertion_equal_int(const char *file
, int line
,
412 long long v1
, const char *e1
, long long v2
, const char *e2
, void *extra
)
414 assertion_count(file
, line
);
417 failure_start(file
, line
, "%s != %s", e1
, e2
);
418 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1
, v1
, v1
, v1
);
419 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2
, v2
, v2
, v2
);
420 failure_finish(extra
);
424 static void strdump(const char *e
, const char *p
)
428 logprintf(" %s = ", e
);
435 unsigned int c
= 0xff & *p
++;
437 case '\a': printf("\a"); break;
438 case '\b': printf("\b"); break;
439 case '\n': printf("\n"); break;
440 case '\r': printf("\r"); break;
442 if (c
>= 32 && c
< 127)
445 logprintf("\\x%02X", c
);
449 logprintf(" (length %d)\n", q
== NULL
? -1 : (int)strlen(q
));
452 /* Verify two strings are equal, dump them if not. */
454 assertion_equal_string(const char *file
, int line
,
455 const char *v1
, const char *e1
,
456 const char *v2
, const char *e2
,
459 assertion_count(file
, line
);
460 if (v1
== v2
|| (v1
!= NULL
&& v2
!= NULL
&& strcmp(v1
, v2
) == 0))
462 failure_start(file
, line
, "%s != %s", e1
, e2
);
465 failure_finish(extra
);
470 wcsdump(const char *e
, const wchar_t *w
)
472 logprintf(" %s = ", e
);
478 while (*w
!= L
'\0') {
479 unsigned int c
= *w
++;
480 if (c
>= 32 && c
< 127)
483 logprintf("\\x%02X", c
);
484 else if (c
< 0x10000)
485 logprintf("\\u%04X", c
);
487 logprintf("\\U%08X", c
);
494 wcscmp(const wchar_t *s1
, const wchar_t *s2
)
497 while (*s1
== *s2
++) {
508 /* Verify that two wide strings are equal, dump them if not. */
510 assertion_equal_wstring(const char *file
, int line
,
511 const wchar_t *v1
, const char *e1
,
512 const wchar_t *v2
, const char *e2
,
515 assertion_count(file
, line
);
516 if (v1
== v2
|| wcscmp(v1
, v2
) == 0)
518 failure_start(file
, line
, "%s != %s", e1
, e2
);
521 failure_finish(extra
);
526 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
527 * any bytes in p that differ from ref will be highlighted with '_'
528 * before and after the hex value.
531 hexdump(const char *p
, const char *ref
, size_t l
, size_t offset
)
537 logprintf("(null)\n");
540 for(i
=0; i
< l
; i
+=16) {
541 logprintf("%04x", (unsigned)(i
+ offset
));
543 for (j
= 0; j
< 16 && i
+ j
< l
; j
++) {
544 if (ref
!= NULL
&& p
[i
+ j
] != ref
[i
+ j
])
546 logprintf("%c%02x", sep
, 0xff & (int)p
[i
+j
]);
547 if (ref
!= NULL
&& p
[i
+ j
] == ref
[i
+ j
])
550 for (; j
< 16; j
++) {
551 logprintf("%c ", sep
);
554 logprintf("%c", sep
);
555 for (j
=0; j
< 16 && i
+ j
< l
; j
++) {
557 if (c
>= ' ' && c
<= 126)
566 /* Verify that two blocks of memory are the same, display the first
567 * block of differences if they're not. */
569 assertion_equal_mem(const char *file
, int line
,
570 const void *_v1
, const char *e1
,
571 const void *_v2
, const char *e2
,
572 size_t l
, const char *ld
, void *extra
)
574 const char *v1
= (const char *)_v1
;
575 const char *v2
= (const char *)_v2
;
578 assertion_count(file
, line
);
579 if (v1
== v2
|| (v1
!= NULL
&& v2
!= NULL
&& memcmp(v1
, v2
, l
) == 0))
582 failure_start(file
, line
, "%s != %s", e1
, e2
);
583 logprintf(" size %s = %d\n", ld
, (int)l
);
584 /* Dump 48 bytes (3 lines) so that the first difference is
585 * in the second line. */
587 while (l
> 64 && memcmp(v1
, v2
, 32) == 0) {
588 /* Two lines agree, so step forward one line. */
594 logprintf(" Dump of %s\n", e1
);
595 hexdump(v1
, v2
, l
< 64 ? l
: 64, offset
);
596 logprintf(" Dump of %s\n", e2
);
597 hexdump(v2
, v1
, l
< 64 ? l
: 64, offset
);
599 failure_finish(extra
);
603 /* Verify that the named file exists and is empty. */
605 assertion_empty_file(const char *f1fmt
, ...)
614 assertion_count(test_filename
, test_line
);
616 vsprintf(f1
, f1fmt
, ap
);
619 if (stat(f1
, &st
) != 0) {
620 failure_start(test_filename
, test_line
, "Stat failed: %s", f1
);
621 failure_finish(NULL
);
627 failure_start(test_filename
, test_line
, "File should be empty: %s", f1
);
628 logprintf(" File size: %d\n", (int)st
.st_size
);
629 logprintf(" Contents:\n");
632 logprintf(" Unable to open %s\n", f1
);
634 s
= ((off_t
)sizeof(buff
) < st
.st_size
) ?
635 (ssize_t
)sizeof(buff
) : (ssize_t
)st
.st_size
;
636 s
= fread(buff
, 1, s
, f
);
637 hexdump(buff
, NULL
, s
, 0);
640 failure_finish(NULL
);
644 /* Verify that the named file exists and is not empty. */
646 assertion_non_empty_file(const char *f1fmt
, ...)
652 assertion_count(test_filename
, test_line
);
654 vsprintf(f1
, f1fmt
, ap
);
657 if (stat(f1
, &st
) != 0) {
658 failure_start(test_filename
, test_line
, "Stat failed: %s", f1
);
659 failure_finish(NULL
);
662 if (st
.st_size
== 0) {
663 failure_start(test_filename
, test_line
, "File empty: %s", f1
);
664 failure_finish(NULL
);
670 /* Verify that two files have the same contents. */
671 /* TODO: hexdump the first bytes that actually differ. */
673 assertion_equal_file(const char *fn1
, const char *f2pattern
, ...)
682 assertion_count(test_filename
, test_line
);
683 va_start(ap
, f2pattern
);
684 vsprintf(fn2
, f2pattern
, ap
);
687 f1
= fopen(fn1
, "rb");
688 f2
= fopen(fn2
, "rb");
690 n1
= fread(buff1
, 1, sizeof(buff1
), f1
);
691 n2
= fread(buff2
, 1, sizeof(buff2
), f2
);
694 if (n1
== 0 && n2
== 0) {
699 if (memcmp(buff1
, buff2
, n1
) != 0)
704 failure_start(test_filename
, test_line
, "Files not identical");
705 logprintf(" file1=\"%s\"\n", fn1
);
706 logprintf(" file2=\"%s\"\n", fn2
);
707 failure_finish(test_extra
);
711 /* Verify that the named file does exist. */
713 assertion_file_exists(const char *fpattern
, ...)
718 assertion_count(test_filename
, test_line
);
719 va_start(ap
, fpattern
);
720 vsprintf(f
, fpattern
, ap
);
723 #if defined(_WIN32) && !defined(__CYGWIN__)
727 if (!access(f
, F_OK
))
730 failure_start(test_filename
, test_line
, "File should exist: %s", f
);
731 failure_finish(test_extra
);
735 /* Verify that the named file doesn't exist. */
737 assertion_file_not_exists(const char *fpattern
, ...)
742 assertion_count(test_filename
, test_line
);
743 va_start(ap
, fpattern
);
744 vsprintf(f
, fpattern
, ap
);
747 #if defined(_WIN32) && !defined(__CYGWIN__)
754 failure_start(test_filename
, test_line
, "File should not exist: %s", f
);
755 failure_finish(test_extra
);
759 /* Compare the contents of a file to a block of memory. */
761 assertion_file_contents(const void *buff
, int s
, const char *fpattern
, ...)
769 assertion_count(test_filename
, test_line
);
770 va_start(ap
, fpattern
);
771 vsprintf(fn
, fpattern
, ap
);
776 failure_start(test_filename
, test_line
,
777 "File should exist: %s", fn
);
778 failure_finish(test_extra
);
781 contents
= malloc(s
* 2);
782 n
= fread(contents
, 1, s
* 2, f
);
784 if (n
== s
&& memcmp(buff
, contents
, s
) == 0) {
788 failure_start(test_filename
, test_line
, "File contents don't match");
789 logprintf(" file=\"%s\"\n", fn
);
791 hexdump(contents
, buff
, n
> 512 ? 512 : n
, 0);
793 logprintf(" File empty, contents should be:\n");
794 hexdump(buff
, NULL
, s
> 512 ? 512 : n
, 0);
796 failure_finish(test_extra
);
801 /* Check the contents of a text file, being tolerant of line endings. */
803 assertion_text_file_contents(const char *buff
, const char *fn
)
806 const char *btxt
, *ftxt
;
810 assertion_count(test_filename
, test_line
);
813 failure_start(test_filename
, test_line
,
814 "File doesn't exist: %s", fn
);
815 failure_finish(test_extra
);
819 contents
= malloc(s
* 2 + 128);
820 n
= fread(contents
, 1, s
* 2 + 128 - 1, f
);
826 ftxt
= (const char *)contents
;
827 while (*btxt
!= '\0' && *ftxt
!= '\0') {
828 if (*btxt
== *ftxt
) {
833 if (btxt
[0] == '\n' && ftxt
[0] == '\r' && ftxt
[1] == '\n') {
834 /* Pass over different new line characters. */
841 if (*btxt
== '\0' && *ftxt
== '\0') {
845 failure_start(test_filename
, test_line
, "Contents don't match");
846 logprintf(" file=\"%s\"\n", fn
);
848 hexdump(contents
, buff
, n
, 0);
850 logprintf(" File empty, contents should be:\n");
851 hexdump(buff
, NULL
, s
, 0);
853 failure_finish(test_extra
);
858 /* Test that two paths point to the same file. */
859 /* As a side-effect, asserts that both files exist. */
861 is_hardlink(const char *file
, int line
,
862 const char *path1
, const char *path2
)
864 #if defined(_WIN32) && !defined(__CYGWIN__)
865 BY_HANDLE_FILE_INFORMATION bhfi1
, bhfi2
;
868 assertion_count(file
, line
);
869 r
= my_GetFileInformationByName(path1
, &bhfi1
);
871 failure_start(file
, line
, "File %s can't be inspected?", path1
);
872 failure_finish(NULL
);
875 r
= my_GetFileInformationByName(path2
, &bhfi2
);
877 failure_start(file
, line
, "File %s can't be inspected?", path2
);
878 failure_finish(NULL
);
881 return (bhfi1
.dwVolumeSerialNumber
== bhfi2
.dwVolumeSerialNumber
882 && bhfi1
.nFileIndexHigh
== bhfi2
.nFileIndexHigh
883 && bhfi1
.nFileIndexLow
== bhfi2
.nFileIndexLow
);
885 struct stat st1
, st2
;
888 assertion_count(file
, line
);
889 r
= lstat(path1
, &st1
);
891 failure_start(file
, line
, "File should exist: %s", path1
);
892 failure_finish(NULL
);
895 r
= lstat(path2
, &st2
);
897 failure_start(file
, line
, "File should exist: %s", path2
);
898 failure_finish(NULL
);
901 return (st1
.st_ino
== st2
.st_ino
&& st1
.st_dev
== st2
.st_dev
);
906 assertion_is_hardlink(const char *file
, int line
,
907 const char *path1
, const char *path2
)
909 if (is_hardlink(file
, line
, path1
, path2
))
911 failure_start(file
, line
,
912 "Files %s and %s are not hardlinked", path1
, path2
);
913 failure_finish(NULL
);
918 assertion_is_not_hardlink(const char *file
, int line
,
919 const char *path1
, const char *path2
)
921 if (!is_hardlink(file
, line
, path1
, path2
))
923 failure_start(file
, line
,
924 "Files %s and %s should not be hardlinked", path1
, path2
);
925 failure_finish(NULL
);
929 /* Verify a/b/mtime of 'pathname'. */
930 /* If 'recent', verify that it's within last 10 seconds. */
932 assertion_file_time(const char *file
, int line
,
933 const char *pathname
, long t
, long nsec
, char type
, int recent
)
935 long long filet
, filet_nsec
;
938 #if defined(_WIN32) && !defined(__CYGWIN__)
939 #define EPOC_TIME (116444736000000000ULL)
940 FILETIME ftime
, fbirthtime
, fatime
, fmtime
;
941 ULARGE_INTEGER wintm
;
943 ftime
.dwLowDateTime
= 0;
944 ftime
.dwHighDateTime
= 0;
946 assertion_count(file
, line
);
947 h
= CreateFile(pathname
, FILE_READ_ATTRIBUTES
, 0, NULL
,
948 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
949 if (h
== INVALID_HANDLE_VALUE
) {
950 failure_start(file
, line
, "Can't access %s\n", pathname
);
951 failure_finish(NULL
);
954 r
= GetFileTime(h
, &fbirthtime
, &fatime
, &fmtime
);
956 case 'a': ftime
= fatime
; break;
957 case 'b': ftime
= fbirthtime
; break;
958 case 'm': ftime
= fmtime
; break;
962 failure_start(file
, line
, "Can't GetFileTime %s\n", pathname
);
963 failure_finish(NULL
);
966 wintm
.LowPart
= ftime
.dwLowDateTime
;
967 wintm
.HighPart
= ftime
.dwHighDateTime
;
968 filet
= (wintm
.QuadPart
- EPOC_TIME
) / 10000000;
969 filet_nsec
= ((wintm
.QuadPart
- EPOC_TIME
) % 10000000) * 100;
970 nsec
= (nsec
/ 100) * 100; /* Round the request */
974 assertion_count(file
, line
);
975 r
= lstat(pathname
, &st
);
977 failure_start(file
, line
, "Can't stat %s\n", pathname
);
978 failure_finish(NULL
);
982 case 'a': filet
= st
.st_atime
; break;
983 case 'm': filet
= st
.st_mtime
; break;
984 case 'b': filet
= 0; break;
985 default: fprintf(stderr
, "INTERNAL: Bad type %c for file time", type
);
988 #if defined(__FreeBSD__)
990 case 'a': filet_nsec
= st
.st_atimespec
.tv_nsec
; break;
991 case 'b': filet
= st
.st_birthtime
;
992 filet_nsec
= st
.st_birthtimespec
.tv_nsec
; break;
993 case 'm': filet_nsec
= st
.st_mtimespec
.tv_nsec
; break;
994 default: fprintf(stderr
, "INTERNAL: Bad type %c for file time", type
);
997 /* FreeBSD generally only stores to microsecond res, so round. */
998 filet_nsec
= (filet_nsec
/ 1000) * 1000;
999 nsec
= (nsec
/ 1000) * 1000;
1001 filet_nsec
= nsec
= 0; /* Generic POSIX only has whole seconds. */
1002 if (type
== 'b') return (1); /* Generic POSIX doesn't have birthtime */
1003 #if defined(__HAIKU__)
1004 if (type
== 'a') return (1); /* Haiku doesn't have atime. */
1009 /* Check that requested time is up-to-date. */
1010 time_t now
= time(NULL
);
1011 if (filet
< now
- 10 || filet
> now
+ 1) {
1012 failure_start(file
, line
,
1013 "File %s has %ctime %ld, %ld seconds ago\n",
1014 pathname
, type
, filet
, now
- filet
);
1015 failure_finish(NULL
);
1018 } else if (filet
!= t
|| filet_nsec
!= nsec
) {
1019 failure_start(file
, line
,
1020 "File %s has %ctime %ld.%09ld, expected %ld.%09ld",
1021 pathname
, type
, filet
, filet_nsec
, t
, nsec
);
1022 failure_finish(NULL
);
1028 /* Verify atime of 'pathname'. */
1030 assertion_file_atime(const char *file
, int line
,
1031 const char *pathname
, long t
, long nsec
)
1033 return assertion_file_time(file
, line
, pathname
, t
, nsec
, 'a', 0);
1036 /* Verify atime of 'pathname' is up-to-date. */
1038 assertion_file_atime_recent(const char *file
, int line
, const char *pathname
)
1040 return assertion_file_time(file
, line
, pathname
, 0, 0, 'a', 1);
1043 /* Verify birthtime of 'pathname'. */
1045 assertion_file_birthtime(const char *file
, int line
,
1046 const char *pathname
, long t
, long nsec
)
1048 return assertion_file_time(file
, line
, pathname
, t
, nsec
, 'b', 0);
1051 /* Verify birthtime of 'pathname' is up-to-date. */
1053 assertion_file_birthtime_recent(const char *file
, int line
,
1054 const char *pathname
)
1056 return assertion_file_time(file
, line
, pathname
, 0, 0, 'b', 1);
1059 /* Verify mtime of 'pathname'. */
1061 assertion_file_mtime(const char *file
, int line
,
1062 const char *pathname
, long t
, long nsec
)
1064 return assertion_file_time(file
, line
, pathname
, t
, nsec
, 'm', 0);
1067 /* Verify mtime of 'pathname' is up-to-date. */
1069 assertion_file_mtime_recent(const char *file
, int line
, const char *pathname
)
1071 return assertion_file_time(file
, line
, pathname
, 0, 0, 'm', 1);
1074 /* Verify number of links to 'pathname'. */
1076 assertion_file_nlinks(const char *file
, int line
,
1077 const char *pathname
, int nlinks
)
1079 #if defined(_WIN32) && !defined(__CYGWIN__)
1080 BY_HANDLE_FILE_INFORMATION bhfi
;
1083 assertion_count(file
, line
);
1084 r
= my_GetFileInformationByName(pathname
, &bhfi
);
1085 if (r
!= 0 && bhfi
.nNumberOfLinks
== (DWORD
)nlinks
)
1087 failure_start(file
, line
, "File %s has %d links, expected %d",
1088 pathname
, bhfi
.nNumberOfLinks
, nlinks
);
1089 failure_finish(NULL
);
1095 assertion_count(file
, line
);
1096 r
= lstat(pathname
, &st
);
1097 if (r
== 0 && st
.st_nlink
== nlinks
)
1099 failure_start(file
, line
, "File %s has %d links, expected %d",
1100 pathname
, st
.st_nlink
, nlinks
);
1101 failure_finish(NULL
);
1106 /* Verify size of 'pathname'. */
1108 assertion_file_size(const char *file
, int line
, const char *pathname
, long size
)
1113 assertion_count(file
, line
);
1114 #if defined(_WIN32) && !defined(__CYGWIN__)
1116 BY_HANDLE_FILE_INFORMATION bhfi
;
1117 r
= !my_GetFileInformationByName(pathname
, &bhfi
);
1118 filesize
= ((int64_t)bhfi
.nFileSizeHigh
<< 32) + bhfi
.nFileSizeLow
;
1123 r
= lstat(pathname
, &st
);
1124 filesize
= st
.st_size
;
1127 if (r
== 0 && filesize
== size
)
1129 failure_start(file
, line
, "File %s has size %ld, expected %ld",
1130 pathname
, (long)filesize
, (long)size
);
1131 failure_finish(NULL
);
1135 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1137 assertion_is_dir(const char *file
, int line
, const char *pathname
, int mode
)
1142 #if defined(_WIN32) && !defined(__CYGWIN__)
1143 (void)mode
; /* UNUSED */
1145 assertion_count(file
, line
);
1146 r
= lstat(pathname
, &st
);
1148 failure_start(file
, line
, "Dir should exist: %s", pathname
);
1149 failure_finish(NULL
);
1152 if (!S_ISDIR(st
.st_mode
)) {
1153 failure_start(file
, line
, "%s is not a dir", pathname
);
1154 failure_finish(NULL
);
1157 #if !defined(_WIN32) || defined(__CYGWIN__)
1158 /* Windows doesn't handle permissions the same way as POSIX,
1159 * so just ignore the mode tests. */
1160 /* TODO: Can we do better here? */
1161 if (mode
>= 0 && mode
!= (st
.st_mode
& 07777)) {
1162 failure_start(file
, line
, "Dir %s has wrong mode", pathname
);
1163 logprintf(" Expected: 0%3o\n", mode
);
1164 logprintf(" Found: 0%3o\n", st
.st_mode
& 07777);
1165 failure_finish(NULL
);
1172 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1173 * verify that too. */
1175 assertion_is_reg(const char *file
, int line
, const char *pathname
, int mode
)
1180 #if defined(_WIN32) && !defined(__CYGWIN__)
1181 (void)mode
; /* UNUSED */
1183 assertion_count(file
, line
);
1184 r
= lstat(pathname
, &st
);
1185 if (r
!= 0 || !S_ISREG(st
.st_mode
)) {
1186 failure_start(file
, line
, "File should exist: %s", pathname
);
1187 failure_finish(NULL
);
1190 #if !defined(_WIN32) || defined(__CYGWIN__)
1191 /* Windows doesn't handle permissions the same way as POSIX,
1192 * so just ignore the mode tests. */
1193 /* TODO: Can we do better here? */
1194 if (mode
>= 0 && mode
!= (st
.st_mode
& 07777)) {
1195 failure_start(file
, line
, "File %s has wrong mode", pathname
);
1196 logprintf(" Expected: 0%3o\n", mode
);
1197 logprintf(" Found: 0%3o\n", st
.st_mode
& 07777);
1198 failure_finish(NULL
);
1205 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1206 * non-NULL, verify that the symlink has those contents. */
1208 is_symlink(const char *file
, int line
,
1209 const char *pathname
, const char *contents
)
1211 #if defined(_WIN32) && !defined(__CYGWIN__)
1212 (void)pathname
; /* UNUSED */
1213 (void)contents
; /* UNUSED */
1214 assertion_count(file
, line
);
1215 /* Windows sort-of has real symlinks, but they're only usable
1216 * by privileged users and are crippled even then, so there's
1217 * really not much point in bothering with this. */
1225 assertion_count(file
, line
);
1226 r
= lstat(pathname
, &st
);
1228 failure_start(file
, line
,
1229 "Symlink should exist: %s", pathname
);
1230 failure_finish(NULL
);
1233 if (!S_ISLNK(st
.st_mode
))
1235 if (contents
== NULL
)
1237 linklen
= readlink(pathname
, buff
, sizeof(buff
));
1239 failure_start(file
, line
, "Can't read symlink %s", pathname
);
1240 failure_finish(NULL
);
1243 buff
[linklen
] = '\0';
1244 if (strcmp(buff
, contents
) != 0)
1250 /* Assert that path is a symlink that (optionally) contains contents. */
1252 assertion_is_symlink(const char *file
, int line
,
1253 const char *path
, const char *contents
)
1255 if (is_symlink(file
, line
, path
, contents
))
1258 failure_start(file
, line
, "File %s is not a symlink to %s",
1261 failure_start(file
, line
, "File %s is not a symlink", path
);
1262 failure_finish(NULL
);
1267 /* Create a directory and report any errors. */
1269 assertion_make_dir(const char *file
, int line
, const char *dirname
, int mode
)
1271 assertion_count(file
, line
);
1272 #if defined(_WIN32) && !defined(__CYGWIN__)
1273 (void)mode
; /* UNUSED */
1274 if (0 == _mkdir(dirname
))
1277 if (0 == mkdir(dirname
, mode
))
1280 failure_start(file
, line
, "Could not create directory %s", dirname
);
1281 failure_finish(NULL
);
1285 /* Create a file with the specified contents and report any failures. */
1287 assertion_make_file(const char *file
, int line
,
1288 const char *path
, int mode
, const char *contents
)
1290 #if defined(_WIN32) && !defined(__CYGWIN__)
1291 /* TODO: Rework this to set file mode as well. */
1293 (void)mode
; /* UNUSED */
1294 assertion_count(file
, line
);
1295 f
= fopen(path
, "wb");
1297 failure_start(file
, line
, "Could not create file %s", path
);
1298 failure_finish(NULL
);
1301 if (contents
!= NULL
) {
1302 if (strlen(contents
)
1303 != fwrite(contents
, 1, strlen(contents
), f
)) {
1305 failure_start(file
, line
,
1306 "Could not write file %s", path
);
1307 failure_finish(NULL
);
1315 assertion_count(file
, line
);
1316 fd
= open(path
, O_CREAT
| O_WRONLY
, mode
>= 0 ? mode
: 0644);
1318 failure_start(file
, line
, "Could not create %s", path
);
1319 failure_finish(NULL
);
1322 if (contents
!= NULL
) {
1323 if ((ssize_t
)strlen(contents
)
1324 != write(fd
, contents
, strlen(contents
))) {
1326 failure_start(file
, line
, "Could not write to %s", path
);
1327 failure_finish(NULL
);
1336 /* Create a hardlink and report any failures. */
1338 assertion_make_hardlink(const char *file
, int line
,
1339 const char *newpath
, const char *linkto
)
1343 assertion_count(file
, line
);
1344 #if defined(_WIN32) && !defined(__CYGWIN__)
1345 succeeded
= my_CreateHardLinkA(newpath
, linkto
);
1347 succeeded
= !link(linkto
, newpath
);
1353 failure_start(file
, line
, "Could not create hardlink");
1354 logprintf(" New link: %s\n", newpath
);
1355 logprintf(" Old name: %s\n", linkto
);
1356 failure_finish(NULL
);
1360 /* Create a symlink and report any failures. */
1362 assertion_make_symlink(const char *file
, int line
,
1363 const char *newpath
, const char *linkto
)
1365 #if defined(_WIN32) && !defined(__CYGWIN__)
1366 int targetIsDir
= 0; /* TODO: Fix this */
1367 assertion_count(file
, line
);
1368 if (my_CreateSymbolicLinkA(newpath
, linkto
, targetIsDir
))
1371 assertion_count(file
, line
);
1372 if (0 == symlink(linkto
, newpath
))
1375 failure_start(file
, line
, "Could not create symlink");
1376 logprintf(" New link: %s\n", newpath
);
1377 logprintf(" Old name: %s\n", linkto
);
1378 failure_finish(NULL
);
1382 /* Set umask, report failures. */
1384 assertion_umask(const char *file
, int line
, int mask
)
1386 assertion_count(file
, line
);
1387 (void)file
; /* UNUSED */
1388 (void)line
; /* UNUSED */
1395 * UTILITIES for use by tests.
1400 * Check whether platform supports symlinks. This is intended
1401 * for tests to use in deciding whether to bother testing symlink
1402 * support; if the platform doesn't support symlinks, there's no point
1403 * in checking whether the program being tested can create them.
1405 * Note that the first time this test is called, we actually go out to
1406 * disk to create and verify a symlink. This is necessary because
1407 * symlink support is actually a property of a particular filesystem
1408 * and can thus vary between directories on a single system. After
1409 * the first call, this returns the cached result from memory, so it's
1410 * safe to call it as often as you wish.
1415 /* Remember the test result */
1416 static int value
= 0, tested
= 0;
1421 assertion_make_file(__FILE__
, __LINE__
, "canSymlink.0", 0644, "a");
1422 /* Note: Cygwin has its own symlink() emulation that does not
1423 * use the Win32 CreateSymbolicLink() function. */
1424 #if defined(_WIN32) && !defined(__CYGWIN__)
1425 value
= my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1426 && is_symlink(__FILE__
, __LINE__
, "canSymlink.1", "canSymlink.0");
1428 value
= (0 == symlink("canSymlink.0", "canSymlink.1"))
1429 && is_symlink(__FILE__
, __LINE__
, "canSymlink.1","canSymlink.0");
1435 * Can this platform run the gzip program?
1437 /* Platform-dependent options for hiding the output of a subcommand. */
1438 #if defined(_WIN32) && !defined(__CYGWIN__)
1439 static const char *redirectArgs
= ">NUL 2>NUL"; /* Win32 cmd.exe */
1441 static const char *redirectArgs
= ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1446 static int tested
= 0, value
= 0;
1449 if (systemf("gzip -V %s", redirectArgs
) == 0)
1456 * Can this platform run the gunzip program?
1461 static int tested
= 0, value
= 0;
1464 if (systemf("gunzip -V %s", redirectArgs
) == 0)
1471 * Sleep as needed; useful for verifying disk timestamp changes by
1472 * ensuring that the wall-clock time has actually changed before we
1473 * go back to re-read something from disk.
1476 sleepUntilAfter(time_t t
)
1478 while (t
>= time(NULL
))
1479 #if defined(_WIN32) && !defined(__CYGWIN__)
1487 * Call standard system() call, but build up the command line using
1488 * sprintf() conventions.
1491 systemf(const char *fmt
, ...)
1498 vsprintf(buff
, fmt
, ap
);
1499 if (verbosity
> VERBOSITY_FULL
)
1500 logprintf("Cmd: %s\n", buff
);
1507 * Slurp a file into memory for ease of comparison and testing.
1508 * Returns size of file in 'sizep' if non-NULL, null-terminates
1509 * data in memory for ease of use.
1512 slurpfile(size_t * sizep
, const char *fmt
, ...)
1514 char filename
[8192];
1523 vsprintf(filename
, fmt
, ap
);
1526 f
= fopen(filename
, "rb");
1528 /* Note: No error; non-existent file is okay here. */
1531 r
= fstat(fileno(f
), &st
);
1533 logprintf("Can't stat file %s\n", filename
);
1537 p
= malloc((size_t)st
.st_size
+ 1);
1539 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
1540 (long int)st
.st_size
, filename
);
1544 bytes_read
= fread(p
, 1, (size_t)st
.st_size
, f
);
1545 if (bytes_read
< st
.st_size
) {
1546 logprintf("Can't read file %s\n", filename
);
1551 p
[st
.st_size
] = '\0';
1553 *sizep
= (size_t)st
.st_size
;
1558 /* Read a uuencoded file from the reference directory, decode, and
1559 * write the result into the current directory. */
1560 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
1562 extract_reference_file(const char *name
)
1567 sprintf(buff
, "%s/%s.uu", refdir
, name
);
1568 in
= fopen(buff
, "r");
1569 failure("Couldn't open reference file %s", buff
);
1573 /* Read up to and including the 'begin' line. */
1575 if (fgets(buff
, sizeof(buff
), in
) == NULL
) {
1576 /* TODO: This is a failure. */
1579 if (memcmp(buff
, "begin ", 6) == 0)
1582 /* Now, decode the rest and write it. */
1583 /* Not a lot of error checking here; the input better be right. */
1584 out
= fopen(name
, "wb");
1585 while (fgets(buff
, sizeof(buff
), in
) != NULL
) {
1589 if (memcmp(buff
, "end", 3) == 0)
1592 bytes
= UUDECODE(*p
++);
1595 /* Write out 1-3 bytes from that. */
1597 n
= UUDECODE(*p
++) << 18;
1598 n
|= UUDECODE(*p
++) << 12;
1599 fputc(n
>> 16, out
);
1603 n
|= UUDECODE(*p
++) << 6;
1604 fputc((n
>> 8) & 0xFF, out
);
1608 n
|= UUDECODE(*p
++);
1609 fputc(n
& 0xFF, out
);
1625 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
1627 * DEFINE_TEST(test_function)
1631 /* Use "list.h" to declare all of the test functions. */
1633 #define DEFINE_TEST(name) void name(void);
1636 /* Use "list.h" to create a list of all tests (functions and names). */
1638 #define DEFINE_TEST(n) { n, #n, 0 },
1639 struct { void (*func
)(void); const char *name
; int failures
; } tests
[] = {
1644 * Summarize repeated failures in the just-completed test.
1647 test_summarize(const char *filename
, int failed
)
1651 switch (verbosity
) {
1652 case VERBOSITY_SUMMARY_ONLY
:
1653 printf(failed
? "E" : ".");
1656 case VERBOSITY_PASSFAIL
:
1657 printf(failed
? "FAIL\n" : "ok\n");
1661 log_console
= (verbosity
== VERBOSITY_LIGHT_REPORT
);
1663 for (i
= 0; i
< sizeof(failed_lines
)/sizeof(failed_lines
[0]); i
++) {
1664 if (failed_lines
[i
].count
> 1 && !failed_lines
[i
].skip
)
1665 logprintf("%s:%d: Summary: Failed %d times\n",
1666 filename
, i
, failed_lines
[i
].count
);
1668 /* Clear the failure history for the next file. */
1669 memset(failed_lines
, 0, sizeof(failed_lines
));
1673 * Actually run a single test, with appropriate setup and cleanup.
1676 test_run(int i
, const char *tmpdir
)
1678 char logfilename
[64];
1679 int failures_before
= failures
;
1682 switch (verbosity
) {
1683 case VERBOSITY_SUMMARY_ONLY
: /* No per-test reports at all */
1685 case VERBOSITY_PASSFAIL
: /* rest of line will include ok/FAIL marker */
1686 printf("%3d: %-50s", i
, tests
[i
].name
);
1689 default: /* Title of test, details will follow */
1690 printf("%3d: %s\n", i
, tests
[i
].name
);
1693 /* Chdir to the top-level work directory. */
1694 if (!assertChdir(tmpdir
)) {
1696 "ERROR: Can't chdir to top work dir %s\n", tmpdir
);
1699 /* Create a log file for this test. */
1700 sprintf(logfilename
, "%s.log", tests
[i
].name
);
1701 logfile
= fopen(logfilename
, "w");
1702 fprintf(logfile
, "%s\n\n", tests
[i
].name
);
1703 /* Chdir() to a work dir for this specific test. */
1704 if (!assertMakeDir(tests
[i
].name
, 0755)
1705 || !assertChdir(tests
[i
].name
)) {
1707 "ERROR: Can't chdir to work dir %s/%s\n",
1708 tmpdir
, tests
[i
].name
);
1711 /* Explicitly reset the locale before each test. */
1712 setlocale(LC_ALL
, "C");
1713 /* Record the umask before we run the test. */
1714 umask(oldumask
= umask(0));
1716 * Run the actual test.
1720 * Clean up and report afterwards.
1725 setlocale(LC_ALL
, "C");
1726 /* Reset directory. */
1727 if (!assertChdir(tmpdir
)) {
1728 fprintf(stderr
, "ERROR: Couldn't chdir to temp dir %s\n",
1732 /* Report per-test summaries. */
1733 tests
[i
].failures
= failures
- failures_before
;
1734 test_summarize(test_filename
, tests
[i
].failures
);
1735 /* Close the per-test log file. */
1738 /* If there were no failures, we can remove the work dir and logfile. */
1739 if (tests
[i
].failures
== 0) {
1740 if (!keep_temp_files
&& assertChdir(tmpdir
)) {
1741 #if defined(_WIN32) && !defined(__CYGWIN__)
1742 /* Make sure not to leave empty directories.
1743 * Sometimes a processing of closing files used by tests
1744 * is not done, then rmdir will be failed and it will
1745 * leave a empty test directory. So we should wait a few
1746 * seconds and retry rmdir. */
1748 for (t
= 0; t
< 10; t
++) {
1751 r
= systemf("rmdir /S /Q %s", tests
[i
].name
);
1755 systemf("del %s", logfilename
);
1757 systemf("rm -rf %s", tests
[i
].name
);
1758 systemf("rm %s", logfilename
);
1762 /* Return appropriate status. */
1763 return (tests
[i
].failures
);
1769 * MAIN and support routines.
1775 usage(const char *program
)
1777 static const int limit
= sizeof(tests
) / sizeof(tests
[0]);
1780 printf("Usage: %s [options] <test> <test> ...\n", program
);
1781 printf("Default is to run all tests.\n");
1782 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
1783 printf("Options:\n");
1784 printf(" -d Dump core after any failure, for debugging.\n");
1785 printf(" -k Keep all temp files.\n");
1786 printf(" Default: temp files for successful tests deleted.\n");
1788 printf(" -p <path> Path to executable to be tested.\n");
1789 printf(" Default: path taken from " ENVBASE
" environment variable.\n");
1791 printf(" -q Quiet.\n");
1792 printf(" -r <dir> Path to dir containing reference files.\n");
1793 printf(" Default: Current directory.\n");
1794 printf(" -v Verbose.\n");
1795 printf("Available tests:\n");
1796 for (i
= 0; i
< limit
; i
++)
1797 printf(" %d: %s\n", i
, tests
[i
].name
);
1802 get_refdir(const char *d
)
1804 char tried
[512] = { '\0' };
1808 /* If a dir was specified, try that */
1811 snprintf(buff
, sizeof(buff
), "%s", d
);
1812 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1813 if (p
!= NULL
) goto success
;
1814 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1815 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1819 /* Get the current dir. */
1820 pwd
= getcwd(NULL
, 0);
1821 while (pwd
[strlen(pwd
) - 1] == '\n')
1822 pwd
[strlen(pwd
) - 1] = '\0';
1824 /* Look for a known file. */
1825 snprintf(buff
, sizeof(buff
), "%s", pwd
);
1826 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1827 if (p
!= NULL
) goto success
;
1828 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1829 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1831 snprintf(buff
, sizeof(buff
), "%s/test", pwd
);
1832 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1833 if (p
!= NULL
) goto success
;
1834 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1835 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1837 #if defined(LIBRARY)
1838 snprintf(buff
, sizeof(buff
), "%s/%s/test", pwd
, LIBRARY
);
1840 snprintf(buff
, sizeof(buff
), "%s/%s/test", pwd
, PROGRAM
);
1842 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1843 if (p
!= NULL
) goto success
;
1844 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1845 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1847 if (memcmp(pwd
, "/usr/obj", 8) == 0) {
1848 snprintf(buff
, sizeof(buff
), "%s", pwd
+ 8);
1849 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1850 if (p
!= NULL
) goto success
;
1851 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1852 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1854 snprintf(buff
, sizeof(buff
), "%s/test", pwd
+ 8);
1855 p
= slurpfile(NULL
, "%s/%s", buff
, KNOWNREF
);
1856 if (p
!= NULL
) goto success
;
1857 strncat(tried
, buff
, sizeof(tried
) - strlen(tried
) - 1);
1858 strncat(tried
, "\n", sizeof(tried
) - strlen(tried
) - 1);
1862 printf("Unable to locate known reference file %s\n", KNOWNREF
);
1863 printf(" Checked following directories:\n%s\n", tried
);
1864 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
1872 return strdup(buff
);
1876 main(int argc
, char **argv
)
1878 static const int limit
= sizeof(tests
) / sizeof(tests
[0]);
1879 int i
, tests_run
= 0, tests_failed
= 0, option
;
1881 char *refdir_alloc
= NULL
;
1882 const char *progname
;
1883 const char *tmp
, *option_arg
, *p
;
1885 char tmpdir_timestamp
[256];
1887 (void)argc
; /* UNUSED */
1889 #if defined(HAVE__CrtSetReportMode)
1890 /* To stop to run the default invalid parameter handler. */
1891 _set_invalid_parameter_handler(invalid_parameter_handler
);
1892 /* Disable annoying assertion message box. */
1893 _CrtSetReportMode(_CRT_ASSERT
, 0);
1897 * Name of this program, used to build root of our temp directory
1900 progname
= p
= argv
[0];
1901 while (*p
!= '\0') {
1902 /* Support \ or / dir separators for Windows compat. */
1903 if (*p
== '/' || *p
== '\\')
1909 /* Get the target program from environment, if available. */
1910 testprogfile
= getenv(ENVBASE
);
1913 if (getenv("TMPDIR") != NULL
)
1914 tmp
= getenv("TMPDIR");
1915 else if (getenv("TMP") != NULL
)
1916 tmp
= getenv("TMP");
1917 else if (getenv("TEMP") != NULL
)
1918 tmp
= getenv("TEMP");
1919 else if (getenv("TEMPDIR") != NULL
)
1920 tmp
= getenv("TEMPDIR");
1924 /* Allow -d to be controlled through the environment. */
1925 if (getenv(ENVBASE
"_DEBUG") != NULL
)
1926 dump_on_failure
= 1;
1928 /* Get the directory holding test files from environment. */
1929 refdir
= getenv(ENVBASE
"_TEST_FILES");
1932 * Parse options, without using getopt(), which isn't available
1935 ++argv
; /* Skip program name */
1936 while (*argv
!= NULL
) {
1941 while (*p
!= '\0') {
1944 /* If 'opt' takes an argument, parse that. */
1945 if (option
== 'p' || option
== 'r') {
1948 else if (*argv
== NULL
) {
1950 "Option -%c requires argument.\n",
1954 option_arg
= *argv
++;
1955 p
= ""; /* End of this option word. */
1958 /* Now, handle the option. */
1961 dump_on_failure
= 1;
1964 keep_temp_files
= 1;
1968 testprogfile
= option_arg
;
1977 refdir
= option_arg
;
1989 * Sanity-check that our options make sense.
1992 if (testprogfile
== NULL
)
1996 #if defined(_WIN32) && !defined(__CYGWIN__)
1997 /* Command.com sometimes rejects '/' separators. */
1998 testprg
= strdup(testprogfile
);
1999 for (i
= 0; testprg
[i
] != '\0'; i
++) {
2000 if (testprg
[i
] == '/')
2003 testprogfile
= testprg
;
2005 /* Quote the name that gets put into shell command lines. */
2006 testprg
= malloc(strlen(testprogfile
) + 3);
2007 strcpy(testprg
, "\"");
2008 strcat(testprg
, testprogfile
);
2009 strcat(testprg
, "\"");
2015 * Create a temp directory for the following tests.
2016 * Include the time the tests started as part of the name,
2017 * to make it easier to track the results of multiple tests.
2020 for (i
= 0; ; i
++) {
2021 strftime(tmpdir_timestamp
, sizeof(tmpdir_timestamp
),
2022 "%Y-%m-%dT%H.%M.%S",
2024 sprintf(tmpdir
, "%s/%s.%s-%03d", tmp
, progname
,
2025 tmpdir_timestamp
, i
);
2026 if (assertMakeDir(tmpdir
,0755))
2030 "ERROR: Unable to create temp directory %s\n",
2037 * If the user didn't specify a directory for locating
2038 * reference files, try to find the reference files in
2039 * the "usual places."
2041 refdir
= refdir_alloc
= get_refdir(refdir
);
2044 * Banner with basic information.
2047 printf("If tests fail or crash, details will be in:\n");
2048 printf(" %s\n", tmpdir
);
2050 if (verbosity
> VERBOSITY_SUMMARY_ONLY
) {
2051 printf("Reference files will be read from: %s\n", refdir
);
2053 printf("Running tests on: %s\n", testprog
);
2055 printf("Exercising: ");
2057 printf("%s\n", EXTRA_VERSION
);
2064 * Run some or all of the individual tests.
2066 if (*argv
== NULL
) {
2067 /* Default: Run all tests. */
2068 for (i
= 0; i
< limit
; i
++) {
2069 if (test_run(i
, tmpdir
))
2074 while (*(argv
) != NULL
) {
2075 if (**argv
>= '0' && **argv
<= '9') {
2077 if (i
< 0 || i
>= limit
) {
2078 printf("*** INVALID Test %s\n", *argv
);
2081 /* usage() never returns */
2084 for (i
= 0; i
< limit
; ++i
) {
2085 if (strcmp(*argv
, tests
[i
].name
) == 0)
2089 printf("*** INVALID Test ``%s''\n",
2093 /* usage() never returns */
2096 if (test_run(i
, tmpdir
))
2104 * Report summary statistics.
2106 if (verbosity
> VERBOSITY_SUMMARY_ONLY
) {
2108 printf("Totals:\n");
2109 printf(" Tests run: %8d\n", tests_run
);
2110 printf(" Tests failed: %8d\n", tests_failed
);
2111 printf(" Assertions checked:%8d\n", assertions
);
2112 printf(" Assertions failed: %8d\n", failures
);
2113 printf(" Skips reported: %8d\n", skips
);
2117 printf("Failing tests:\n");
2118 for (i
= 0; i
< limit
; ++i
) {
2119 if (tests
[i
].failures
)
2120 printf(" %d: %s (%d failures)\n", i
,
2121 tests
[i
].name
, tests
[i
].failures
);
2124 printf("Details for failing tests: %s\n", tmpdir
);
2127 if (verbosity
== VERBOSITY_SUMMARY_ONLY
)
2129 printf("%d tests passed, no failures\n", tests_run
);
2134 /* If the final tmpdir is empty, we can remove it. */
2135 /* This should be the usual case when all tests succeed. */
2139 return (tests_failed
? 1 : 0);