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.
27 #include "test_utils.h"
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
31 #ifdef HAVE_SYS_TIME_H
39 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
40 * As the include guards don't agree, the order of include is important.
42 #ifdef HAVE_LINUX_EXT2_FS_H
43 #include <linux/ext2_fs.h> /* for Linux file flags */
45 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
46 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
57 * This same file is used pretty much verbatim for all test harnesses.
59 * The next few lines are the only differences.
60 * TODO: Move this into a separate configuration header, have all test
61 * suites share one copy of this file.
63 #define KNOWNREF "test_expand.Z.uu"
64 #define ENVBASE "BSDCAT" /* Prefix for environment variables. */
65 #define PROGRAM "bsdcat" /* Name of program being tested. */
66 #define PROGRAM_ALIAS "cat" /* Generic alias for program */
67 #undef LIBRARY /* Not testing a library. */
68 #undef EXTRA_DUMP /* How to dump extra data */
69 #undef EXTRA_ERRNO /* How to dump errno */
70 /* How to generate extra version info. */
71 #define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "")
75 * Windows support routines
77 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
78 * in the test harness is dangerous because they cover up
79 * configuration errors. The classic example of this is omitting a
80 * configure check. If libarchive and libarchive_test both look for
81 * the same feature macro, such errors are hard to detect. Platform
82 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
83 * easily lead to very messy code. It's best to limit yourself
84 * to only the most generic programming techniques in the test harness
85 * and thus avoid conditionals altogether. Where that's not possible,
86 * try to minimize conditionals by grouping platform-specific tests in
87 * one place (e.g., test_acl_freebsd) or by adding new assert()
88 * functions (e.g., assertMakeHardlink()) to cover up platform
89 * differences. Platform-specific coding in libarchive_test is often
90 * a symptom that some capability is missing from libarchive itself.
92 #if defined(_WIN32) && !defined(__CYGWIN__)
100 #define S_ISDIR(m) ((m) & _S_IFDIR)
103 #define S_ISREG(m) ((m) & _S_IFREG)
105 #if !defined(__BORLANDC__)
106 #define access _access
111 #define fileno _fileno
113 /*#define fstat _fstat64*/
114 #if !defined(__BORLANDC__)
115 #define getcwd _getcwd
118 /*#define lstat _stat64*/
119 /*#define stat _stat64*/
121 #if !defined(__BORLANDC__)
122 #define strdup _strdup
125 #define int64_t __int64
128 #if defined(HAVE__CrtSetReportMode)
132 /* Path to working directory for current test */
133 const char *testworkdir;
135 /* Pathname of exe to be tested. */
136 const char *testprogfile;
137 /* Name of exe to use in printf-formatted command strings. */
138 /* On Windows, this includes leading/trailing quotes. */
139 const char *testprog;
142 #if defined(_WIN32) && !defined(__CYGWIN__)
143 static void *GetFunctionKernel32(const char *);
144 static int my_CreateSymbolicLinkA(const char *, const char *, int);
145 static int my_CreateHardLinkA(const char *, const char *);
146 static int my_GetFileInformationByName(const char *,
147 BY_HANDLE_FILE_INFORMATION *);
150 GetFunctionKernel32(const char *name)
152 static HINSTANCE lib;
156 lib = LoadLibrary("kernel32.dll");
159 fprintf(stderr, "Can't load kernel32.dll?!\n");
162 return (void *)GetProcAddress(lib, name);
166 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
168 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
172 f = GetFunctionKernel32("CreateSymbolicLinkA");
174 return f == NULL ? 0 : (*f)(linkname, target, flags);
178 my_CreateHardLinkA(const char *linkname, const char *target)
180 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
184 f = GetFunctionKernel32("CreateHardLinkA");
186 return f == NULL ? 0 : (*f)(linkname, target, NULL);
190 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
195 memset(bhfi, 0, sizeof(*bhfi));
196 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
197 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
198 if (h == INVALID_HANDLE_VALUE)
200 r = GetFileInformationByHandle(h, bhfi);
206 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
208 invalid_parameter_handler(const wchar_t * expression,
209 const wchar_t * function, const wchar_t * file,
210 unsigned int line, uintptr_t pReserved)
222 /* Enable core dump on failure. */
223 static int dump_on_failure = 0;
224 /* Default is to remove temp dirs and log data for successful tests. */
225 static int keep_temp_files = 0;
226 /* Default is to run the specified tests once and report errors. */
227 static int until_failure = 0;
228 /* Default is to just report pass/fail for each test. */
229 static int verbosity = 0;
230 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
231 #define VERBOSITY_PASSFAIL 0 /* Default */
232 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
233 #define VERBOSITY_FULL 2 /* -vv */
234 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
235 * mostly for debugging the test harness itself. */
236 /* Cumulative count of assertion failures. */
237 static int failures = 0;
238 /* Cumulative count of reported skips. */
239 static int skips = 0;
240 /* Cumulative count of assertions checked. */
241 static int assertions = 0;
243 /* Directory where uuencoded reference files can be found. */
244 static const char *refdir;
247 * Report log information selectively to console and/or disk log.
249 static int log_console = 0;
250 static FILE *logfile;
252 vlogprintf(const char *fmt, va_list ap)
259 vfprintf(stdout, fmt, ap);
262 vfprintf(logfile, fmt, lfap);
265 vfprintf(logfile, fmt, ap);
270 logprintf(const char *fmt, ...)
278 /* Set up a message to display only if next assertion fails. */
279 static char msgbuff[4096];
280 static const char *msg, *nextmsg;
282 failure(const char *fmt, ...)
289 vsprintf(msgbuff, fmt, ap);
296 * Copy arguments into file-local variables.
297 * This was added to permit vararg assert() functions without needing
298 * variadic wrapper macros. Turns out that the vararg capability is almost
299 * never used, so almost all of the vararg assertions can be simplified
300 * by removing the vararg capability and reworking the wrapper macro to
301 * pass __FILE__, __LINE__ directly into the function instead of using
302 * this hook. I suspect this machinery is used so rarely that we
303 * would be better off just removing it entirely. That would simplify
304 * the code here noticeably.
306 static const char *skipping_filename;
307 static int skipping_line;
308 void skipping_setup(const char *filename, int line)
310 skipping_filename = filename;
311 skipping_line = line;
314 /* Called at the beginning of each assert() function. */
316 assertion_count(const char *file, int line)
318 (void)file; /* UNUSED */
319 (void)line; /* UNUSED */
321 /* Proper handling of "failure()" message. */
324 /* Uncomment to print file:line after every assertion.
325 * Verbose, but occasionally useful in tracking down crashes. */
326 /* printf("Checked %s:%d\n", file, line); */
330 * For each test source file, we remember how many times each
331 * assertion was reported. Cleared before each new test,
332 * used by test_summarize().
337 } failed_lines[10000];
338 const char *failed_filename;
340 /* Count this failure, setup up log destination and handle initial report. */
342 failure_start(const char *filename, int line, const char *fmt, ...)
346 /* Record another failure for this line. */
348 failed_filename = filename;
349 failed_lines[line].count++;
351 /* Determine whether to log header to console. */
353 case VERBOSITY_LIGHT_REPORT:
354 log_console = (failed_lines[line].count < 2);
357 log_console = (verbosity >= VERBOSITY_FULL);
360 /* Log file:line header for this failure */
363 logprintf("%s(%d): ", filename, line);
365 logprintf("%s:%d: ", filename, line);
371 if (msg != NULL && msg[0] != '\0') {
372 logprintf(" Description: %s\n", msg);
376 /* Determine whether to log details to console. */
377 if (verbosity == VERBOSITY_LIGHT_REPORT)
381 /* Complete reporting of failed tests. */
383 * The 'extra' hook here is used by libarchive to include libarchive
384 * error messages with assertion failures. It could also be used
385 * to add strerror() output, for example. Just define the EXTRA_DUMP()
386 * macro appropriately.
389 failure_finish(void *extra)
391 (void)extra; /* UNUSED (maybe) */
394 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
395 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
399 if (dump_on_failure) {
401 " *** forcing core dump so failure can be debugged ***\n");
406 /* Inform user that we're skipping some checks. */
408 test_skipping(const char *fmt, ...)
414 vsprintf(buff, fmt, ap);
416 /* Use failure() message if set. */
419 /* failure_start() isn't quite right, but is awfully convenient. */
420 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
421 --failures; /* Undo failures++ in failure_start() */
422 /* Don't failure_finish() here. */
423 /* Mark as skip, so doesn't count as failed test. */
424 failed_lines[skipping_line].skip = 1;
434 /* Generic assert() just displays the failed condition. */
436 assertion_assert(const char *file, int line, int value,
437 const char *condition, void *extra)
439 assertion_count(file, line);
441 failure_start(file, line, "Assertion failed: %s", condition);
442 failure_finish(extra);
447 /* chdir() and report any errors */
449 assertion_chdir(const char *file, int line, const char *pathname)
451 assertion_count(file, line);
452 if (chdir(pathname) == 0)
454 failure_start(file, line, "chdir(\"%s\")", pathname);
455 failure_finish(NULL);
460 /* Verify two integers are equal. */
462 assertion_equal_int(const char *file, int line,
463 long long v1, const char *e1, long long v2, const char *e2, void *extra)
465 assertion_count(file, line);
468 failure_start(file, line, "%s != %s", e1, e2);
469 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
470 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
471 failure_finish(extra);
476 * Utility to convert a single UTF-8 sequence.
479 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
481 static const char utf8_count[256] = {
482 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
483 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
484 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
485 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
486 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
487 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
488 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
489 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
490 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
491 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
493 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
494 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
495 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
496 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
497 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
509 * Decode 1-4 bytes depending on the value of the first byte.
511 ch = (unsigned char)*s;
513 return (0); /* Standard: return 0 for end-of-string. */
514 cnt = utf8_count[ch];
516 /* Invalide sequence or there are not plenty bytes. */
520 /* Make a Unicode code point from a single UTF-8 sequence. */
522 case 1: /* 1 byte sequence. */
525 case 2: /* 2 bytes sequence. */
526 if ((s[1] & 0xc0) != 0x80) return (-1);
527 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
529 case 3: /* 3 bytes sequence. */
530 if ((s[1] & 0xc0) != 0x80) return (-1);
531 if ((s[2] & 0xc0) != 0x80) return (-1);
532 wc = ((ch & 0x0f) << 12)
533 | ((s[1] & 0x3f) << 6)
536 return (-1);/* Overlong sequence. */
538 case 4: /* 4 bytes sequence. */
541 if ((s[1] & 0xc0) != 0x80) return (-1);
542 if ((s[2] & 0xc0) != 0x80) return (-1);
543 if ((s[3] & 0xc0) != 0x80) return (-1);
544 wc = ((ch & 0x07) << 18)
545 | ((s[1] & 0x3f) << 12)
546 | ((s[2] & 0x3f) << 6)
549 return (-1);/* Overlong sequence. */
555 /* The code point larger than 0x10FFFF is not leagal
559 /* Correctly gets a Unicode, returns used bytes. */
564 static void strdump(const char *e, const char *p, int ewidth, int utf8)
568 logprintf(" %*s = ", ewidth, e);
575 unsigned int c = 0xff & *p++;
577 case '\a': logprintf("\\a"); break;
578 case '\b': logprintf("\\b"); break;
579 case '\n': logprintf("\\n"); break;
580 case '\r': logprintf("\\r"); break;
582 if (c >= 32 && c < 127)
585 logprintf("\\x%02X", c);
589 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
592 * If the current string is UTF-8, dump its code points.
603 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
606 logprintf("%04X", uc);
612 logprintf(" (count %d", cnt);
614 logprintf(",unknown %d bytes", len);
622 /* Verify two strings are equal, dump them if not. */
624 assertion_equal_string(const char *file, int line,
625 const char *v1, const char *e1,
626 const char *v2, const char *e2,
627 void *extra, int utf8)
631 assertion_count(file, line);
632 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
634 failure_start(file, line, "%s != %s", e1, e2);
635 l1 = (int)strlen(e1);
636 l2 = (int)strlen(e2);
639 strdump(e1, v1, l1, utf8);
640 strdump(e2, v2, l1, utf8);
641 failure_finish(extra);
646 wcsdump(const char *e, const wchar_t *w)
648 logprintf(" %s = ", e);
654 while (*w != L'\0') {
655 unsigned int c = *w++;
656 if (c >= 32 && c < 127)
659 logprintf("\\x%02X", c);
660 else if (c < 0x10000)
661 logprintf("\\u%04X", c);
663 logprintf("\\U%08X", c);
670 wcscmp(const wchar_t *s1, const wchar_t *s2)
673 while (*s1 == *s2++) {
684 /* Verify that two wide strings are equal, dump them if not. */
686 assertion_equal_wstring(const char *file, int line,
687 const wchar_t *v1, const char *e1,
688 const wchar_t *v2, const char *e2,
691 assertion_count(file, line);
694 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
696 failure_start(file, line, "%s != %s", e1, e2);
699 failure_finish(extra);
704 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
705 * any bytes in p that differ from ref will be highlighted with '_'
706 * before and after the hex value.
709 hexdump(const char *p, const char *ref, size_t l, size_t offset)
715 logprintf("(null)\n");
718 for(i=0; i < l; i+=16) {
719 logprintf("%04x", (unsigned)(i + offset));
721 for (j = 0; j < 16 && i + j < l; j++) {
722 if (ref != NULL && p[i + j] != ref[i + j])
724 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
725 if (ref != NULL && p[i + j] == ref[i + j])
728 for (; j < 16; j++) {
729 logprintf("%c ", sep);
732 logprintf("%c", sep);
733 for (j=0; j < 16 && i + j < l; j++) {
735 if (c >= ' ' && c <= 126)
744 /* Verify that two blocks of memory are the same, display the first
745 * block of differences if they're not. */
747 assertion_equal_mem(const char *file, int line,
748 const void *_v1, const char *e1,
749 const void *_v2, const char *e2,
750 size_t l, const char *ld, void *extra)
752 const char *v1 = (const char *)_v1;
753 const char *v2 = (const char *)_v2;
756 assertion_count(file, line);
757 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
759 if (v1 == NULL || v2 == NULL)
762 failure_start(file, line, "%s != %s", e1, e2);
763 logprintf(" size %s = %d\n", ld, (int)l);
764 /* Dump 48 bytes (3 lines) so that the first difference is
765 * in the second line. */
767 while (l > 64 && memcmp(v1, v2, 32) == 0) {
768 /* Two lines agree, so step forward one line. */
774 logprintf(" Dump of %s\n", e1);
775 hexdump(v1, v2, l < 128 ? l : 128, offset);
776 logprintf(" Dump of %s\n", e2);
777 hexdump(v2, v1, l < 128 ? l : 128, offset);
779 failure_finish(extra);
783 /* Verify that a block of memory is filled with the specified byte. */
785 assertion_memory_filled_with(const char *file, int line,
786 const void *_v1, const char *vd,
787 size_t l, const char *ld,
788 char b, const char *bd, void *extra)
790 const char *v1 = (const char *)_v1;
793 (void)ld; /* UNUSED */
795 assertion_count(file, line);
797 for (i = 0; i < l; ++i) {
805 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
806 logprintf(" Only %d bytes were correct\n", (int)c);
807 failure_finish(extra);
811 /* Verify that the named file exists and is empty. */
813 assertion_empty_file(const char *filename, int line, const char *f1)
820 assertion_count(filename, line);
822 if (stat(f1, &st) != 0) {
823 failure_start(filename, line, "Stat failed: %s", f1);
824 failure_finish(NULL);
830 failure_start(filename, line, "File should be empty: %s", f1);
831 logprintf(" File size: %d\n", (int)st.st_size);
832 logprintf(" Contents:\n");
835 logprintf(" Unable to open %s\n", f1);
837 s = ((off_t)sizeof(buff) < st.st_size) ?
838 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
839 s = fread(buff, 1, s, f);
840 hexdump(buff, NULL, s, 0);
843 failure_finish(NULL);
847 /* Verify that the named file exists and is not empty. */
849 assertion_non_empty_file(const char *filename, int line, const char *f1)
853 assertion_count(filename, line);
855 if (stat(f1, &st) != 0) {
856 failure_start(filename, line, "Stat failed: %s", f1);
857 failure_finish(NULL);
860 if (st.st_size == 0) {
861 failure_start(filename, line, "File empty: %s", f1);
862 failure_finish(NULL);
868 /* Verify that two files have the same contents. */
869 /* TODO: hexdump the first bytes that actually differ. */
871 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
878 assertion_count(filename, line);
880 f1 = fopen(fn1, "rb");
881 f2 = fopen(fn2, "rb");
882 if (f1 == NULL || f2 == NULL) {
888 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
889 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
892 if (n1 == 0 && n2 == 0) {
897 if (memcmp(buff1, buff2, n1) != 0)
902 failure_start(filename, line, "Files not identical");
903 logprintf(" file1=\"%s\"\n", fn1);
904 logprintf(" file2=\"%s\"\n", fn2);
905 failure_finish(NULL);
909 /* Verify that the named file does exist. */
911 assertion_file_exists(const char *filename, int line, const char *f)
913 assertion_count(filename, line);
915 #if defined(_WIN32) && !defined(__CYGWIN__)
919 if (!access(f, F_OK))
922 failure_start(filename, line, "File should exist: %s", f);
923 failure_finish(NULL);
927 /* Verify that the named file doesn't exist. */
929 assertion_file_not_exists(const char *filename, int line, const char *f)
931 assertion_count(filename, line);
933 #if defined(_WIN32) && !defined(__CYGWIN__)
940 failure_start(filename, line, "File should not exist: %s", f);
941 failure_finish(NULL);
945 /* Compare the contents of a file to a block of memory. */
947 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
953 assertion_count(filename, line);
957 failure_start(filename, line,
958 "File should exist: %s", fn);
959 failure_finish(NULL);
962 contents = malloc(s * 2);
963 n = (int)fread(contents, 1, s * 2, f);
965 if (n == s && memcmp(buff, contents, s) == 0) {
969 failure_start(filename, line, "File contents don't match");
970 logprintf(" file=\"%s\"\n", fn);
972 hexdump(contents, buff, n > 512 ? 512 : n, 0);
974 logprintf(" File empty, contents should be:\n");
975 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
977 failure_finish(NULL);
982 /* Check the contents of a text file, being tolerant of line endings. */
984 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
987 const char *btxt, *ftxt;
991 assertion_count(filename, line);
994 failure_start(filename, line,
995 "File doesn't exist: %s", fn);
996 failure_finish(NULL);
999 s = (int)strlen(buff);
1000 contents = malloc(s * 2 + 128);
1001 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1005 /* Compare texts. */
1007 ftxt = (const char *)contents;
1008 while (*btxt != '\0' && *ftxt != '\0') {
1009 if (*btxt == *ftxt) {
1014 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1015 /* Pass over different new line characters. */
1022 if (*btxt == '\0' && *ftxt == '\0') {
1026 failure_start(filename, line, "Contents don't match");
1027 logprintf(" file=\"%s\"\n", fn);
1029 hexdump(contents, buff, n, 0);
1030 logprintf(" expected\n", fn);
1031 hexdump(buff, contents, s, 0);
1033 logprintf(" File empty, contents should be:\n");
1034 hexdump(buff, NULL, s, 0);
1036 failure_finish(NULL);
1041 /* Verify that a text file contains the specified lines, regardless of order */
1042 /* This could be more efficient if we sorted both sets of lines, etc, but
1043 * since this is used only for testing and only ever deals with a dozen or so
1044 * lines at a time, this relatively crude approach is just fine. */
1046 assertion_file_contains_lines_any_order(const char *file, int line,
1047 const char *pathname, const char *lines[])
1051 size_t expected_count, actual_count, i, j;
1052 char **expected = NULL;
1053 char *p, **actual = NULL;
1055 int expected_failure = 0, actual_failure = 0;
1057 assertion_count(file, line);
1059 buff = slurpfile(&buff_size, "%s", pathname);
1061 failure_start(pathname, line, "Can't read file: %s", pathname);
1062 failure_finish(NULL);
1066 /* Make a copy of the provided lines and count up the expected
1068 for (i = 0; lines[i] != NULL; ++i) {
1071 if (expected_count) {
1072 expected = malloc(sizeof(char *) * expected_count);
1073 if (expected == NULL) {
1074 failure_start(pathname, line, "Can't allocate memory");
1075 failure_finish(NULL);
1079 for (i = 0; lines[i] != NULL; ++i) {
1080 expected[i] = strdup(lines[i]);
1084 /* Break the file into lines */
1086 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1087 if (*p == '\x0d' || *p == '\x0a')
1089 if (c == '\0' && *p != '\0')
1094 actual = calloc(sizeof(char *), actual_count);
1095 if (actual == NULL) {
1096 failure_start(pathname, line, "Can't allocate memory");
1097 failure_finish(NULL);
1101 for (j = 0, p = buff; p < buff + buff_size;
1102 p += 1 + strlen(p)) {
1110 /* Erase matching lines from both lists */
1111 for (i = 0; i < expected_count; ++i) {
1112 if (expected[i] == NULL)
1114 for (j = 0; j < actual_count; ++j) {
1115 if (actual[j] == NULL)
1117 if (strcmp(expected[i], actual[j]) == 0) {
1126 /* If there's anything left, it's a failure */
1127 for (i = 0; i < expected_count; ++i) {
1128 if (expected[i] != NULL)
1131 for (j = 0; j < actual_count; ++j) {
1132 if (actual[j] != NULL)
1135 if (expected_failure == 0 && actual_failure == 0) {
1141 failure_start(file, line, "File doesn't match: %s", pathname);
1142 for (i = 0; i < expected_count; ++i) {
1143 if (expected[i] != NULL) {
1144 logprintf(" Expected but not present: %s\n", expected[i]);
1148 for (j = 0; j < actual_count; ++j) {
1149 if (actual[j] != NULL)
1150 logprintf(" Present but not expected: %s\n", actual[j]);
1152 failure_finish(NULL);
1159 /* Test that two paths point to the same file. */
1160 /* As a side-effect, asserts that both files exist. */
1162 is_hardlink(const char *file, int line,
1163 const char *path1, const char *path2)
1165 #if defined(_WIN32) && !defined(__CYGWIN__)
1166 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1169 assertion_count(file, line);
1170 r = my_GetFileInformationByName(path1, &bhfi1);
1172 failure_start(file, line, "File %s can't be inspected?", path1);
1173 failure_finish(NULL);
1176 r = my_GetFileInformationByName(path2, &bhfi2);
1178 failure_start(file, line, "File %s can't be inspected?", path2);
1179 failure_finish(NULL);
1182 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1183 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1184 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1186 struct stat st1, st2;
1189 assertion_count(file, line);
1190 r = lstat(path1, &st1);
1192 failure_start(file, line, "File should exist: %s", path1);
1193 failure_finish(NULL);
1196 r = lstat(path2, &st2);
1198 failure_start(file, line, "File should exist: %s", path2);
1199 failure_finish(NULL);
1202 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1207 assertion_is_hardlink(const char *file, int line,
1208 const char *path1, const char *path2)
1210 if (is_hardlink(file, line, path1, path2))
1212 failure_start(file, line,
1213 "Files %s and %s are not hardlinked", path1, path2);
1214 failure_finish(NULL);
1219 assertion_is_not_hardlink(const char *file, int line,
1220 const char *path1, const char *path2)
1222 if (!is_hardlink(file, line, path1, path2))
1224 failure_start(file, line,
1225 "Files %s and %s should not be hardlinked", path1, path2);
1226 failure_finish(NULL);
1230 /* Verify a/b/mtime of 'pathname'. */
1231 /* If 'recent', verify that it's within last 10 seconds. */
1233 assertion_file_time(const char *file, int line,
1234 const char *pathname, long t, long nsec, char type, int recent)
1236 long long filet, filet_nsec;
1239 #if defined(_WIN32) && !defined(__CYGWIN__)
1240 #define EPOC_TIME (116444736000000000ULL)
1241 FILETIME fxtime, fbirthtime, fatime, fmtime;
1242 ULARGE_INTEGER wintm;
1244 fxtime.dwLowDateTime = 0;
1245 fxtime.dwHighDateTime = 0;
1247 assertion_count(file, line);
1248 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1249 * a directory file. If not, CreateFile() will fail when
1250 * the pathname is a directory. */
1251 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1252 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1253 if (h == INVALID_HANDLE_VALUE) {
1254 failure_start(file, line, "Can't access %s\n", pathname);
1255 failure_finish(NULL);
1258 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1260 case 'a': fxtime = fatime; break;
1261 case 'b': fxtime = fbirthtime; break;
1262 case 'm': fxtime = fmtime; break;
1266 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1267 failure_finish(NULL);
1270 wintm.LowPart = fxtime.dwLowDateTime;
1271 wintm.HighPart = fxtime.dwHighDateTime;
1272 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1273 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1274 nsec = (nsec / 100) * 100; /* Round the request */
1278 assertion_count(file, line);
1279 r = lstat(pathname, &st);
1281 failure_start(file, line, "Can't stat %s\n", pathname);
1282 failure_finish(NULL);
1286 case 'a': filet = st.st_atime; break;
1287 case 'm': filet = st.st_mtime; break;
1288 case 'b': filet = 0; break;
1289 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1292 #if defined(__FreeBSD__)
1294 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1295 case 'b': filet = st.st_birthtime;
1296 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1297 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1298 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1301 /* FreeBSD generally only stores to microsecond res, so round. */
1302 filet_nsec = (filet_nsec / 1000) * 1000;
1303 nsec = (nsec / 1000) * 1000;
1305 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1306 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1307 #if defined(__HAIKU__)
1308 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1313 /* Check that requested time is up-to-date. */
1314 time_t now = time(NULL);
1315 if (filet < now - 10 || filet > now + 1) {
1316 failure_start(file, line,
1317 "File %s has %ctime %lld, %lld seconds ago\n",
1318 pathname, type, filet, now - filet);
1319 failure_finish(NULL);
1322 } else if (filet != t || filet_nsec != nsec) {
1323 failure_start(file, line,
1324 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1325 pathname, type, filet, filet_nsec, t, nsec);
1326 failure_finish(NULL);
1332 /* Verify atime of 'pathname'. */
1334 assertion_file_atime(const char *file, int line,
1335 const char *pathname, long t, long nsec)
1337 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1340 /* Verify atime of 'pathname' is up-to-date. */
1342 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1344 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1347 /* Verify birthtime of 'pathname'. */
1349 assertion_file_birthtime(const char *file, int line,
1350 const char *pathname, long t, long nsec)
1352 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1355 /* Verify birthtime of 'pathname' is up-to-date. */
1357 assertion_file_birthtime_recent(const char *file, int line,
1358 const char *pathname)
1360 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1363 /* Verify mtime of 'pathname'. */
1365 assertion_file_mtime(const char *file, int line,
1366 const char *pathname, long t, long nsec)
1368 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1371 /* Verify mtime of 'pathname' is up-to-date. */
1373 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1375 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1378 /* Verify number of links to 'pathname'. */
1380 assertion_file_nlinks(const char *file, int line,
1381 const char *pathname, int nlinks)
1383 #if defined(_WIN32) && !defined(__CYGWIN__)
1384 BY_HANDLE_FILE_INFORMATION bhfi;
1387 assertion_count(file, line);
1388 r = my_GetFileInformationByName(pathname, &bhfi);
1389 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1391 failure_start(file, line, "File %s has %d links, expected %d",
1392 pathname, bhfi.nNumberOfLinks, nlinks);
1393 failure_finish(NULL);
1399 assertion_count(file, line);
1400 r = lstat(pathname, &st);
1401 if (r == 0 && (int)st.st_nlink == nlinks)
1403 failure_start(file, line, "File %s has %d links, expected %d",
1404 pathname, st.st_nlink, nlinks);
1405 failure_finish(NULL);
1410 /* Verify size of 'pathname'. */
1412 assertion_file_size(const char *file, int line, const char *pathname, long size)
1417 assertion_count(file, line);
1418 #if defined(_WIN32) && !defined(__CYGWIN__)
1420 BY_HANDLE_FILE_INFORMATION bhfi;
1421 r = !my_GetFileInformationByName(pathname, &bhfi);
1422 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1427 r = lstat(pathname, &st);
1428 filesize = st.st_size;
1431 if (r == 0 && filesize == size)
1433 failure_start(file, line, "File %s has size %ld, expected %ld",
1434 pathname, (long)filesize, (long)size);
1435 failure_finish(NULL);
1439 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1441 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1446 #if defined(_WIN32) && !defined(__CYGWIN__)
1447 (void)mode; /* UNUSED */
1449 assertion_count(file, line);
1450 r = lstat(pathname, &st);
1452 failure_start(file, line, "Dir should exist: %s", pathname);
1453 failure_finish(NULL);
1456 if (!S_ISDIR(st.st_mode)) {
1457 failure_start(file, line, "%s is not a dir", pathname);
1458 failure_finish(NULL);
1461 #if !defined(_WIN32) || defined(__CYGWIN__)
1462 /* Windows doesn't handle permissions the same way as POSIX,
1463 * so just ignore the mode tests. */
1464 /* TODO: Can we do better here? */
1465 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1466 failure_start(file, line, "Dir %s has wrong mode", pathname);
1467 logprintf(" Expected: 0%3o\n", mode);
1468 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1469 failure_finish(NULL);
1476 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1477 * verify that too. */
1479 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1484 #if defined(_WIN32) && !defined(__CYGWIN__)
1485 (void)mode; /* UNUSED */
1487 assertion_count(file, line);
1488 r = lstat(pathname, &st);
1489 if (r != 0 || !S_ISREG(st.st_mode)) {
1490 failure_start(file, line, "File should exist: %s", pathname);
1491 failure_finish(NULL);
1494 #if !defined(_WIN32) || defined(__CYGWIN__)
1495 /* Windows doesn't handle permissions the same way as POSIX,
1496 * so just ignore the mode tests. */
1497 /* TODO: Can we do better here? */
1498 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1499 failure_start(file, line, "File %s has wrong mode", pathname);
1500 logprintf(" Expected: 0%3o\n", mode);
1501 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1502 failure_finish(NULL);
1509 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1510 * non-NULL, verify that the symlink has those contents. */
1512 is_symlink(const char *file, int line,
1513 const char *pathname, const char *contents)
1515 #if defined(_WIN32) && !defined(__CYGWIN__)
1516 (void)pathname; /* UNUSED */
1517 (void)contents; /* UNUSED */
1518 assertion_count(file, line);
1519 /* Windows sort-of has real symlinks, but they're only usable
1520 * by privileged users and are crippled even then, so there's
1521 * really not much point in bothering with this. */
1529 assertion_count(file, line);
1530 r = lstat(pathname, &st);
1532 failure_start(file, line,
1533 "Symlink should exist: %s", pathname);
1534 failure_finish(NULL);
1537 if (!S_ISLNK(st.st_mode))
1539 if (contents == NULL)
1541 linklen = readlink(pathname, buff, sizeof(buff));
1543 failure_start(file, line, "Can't read symlink %s", pathname);
1544 failure_finish(NULL);
1547 buff[linklen] = '\0';
1548 if (strcmp(buff, contents) != 0)
1554 /* Assert that path is a symlink that (optionally) contains contents. */
1556 assertion_is_symlink(const char *file, int line,
1557 const char *path, const char *contents)
1559 if (is_symlink(file, line, path, contents))
1562 failure_start(file, line, "File %s is not a symlink to %s",
1565 failure_start(file, line, "File %s is not a symlink", path);
1566 failure_finish(NULL);
1571 /* Create a directory and report any errors. */
1573 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1575 assertion_count(file, line);
1576 #if defined(_WIN32) && !defined(__CYGWIN__)
1577 (void)mode; /* UNUSED */
1578 if (0 == _mkdir(dirname))
1581 if (0 == mkdir(dirname, mode))
1584 failure_start(file, line, "Could not create directory %s", dirname);
1585 failure_finish(NULL);
1589 /* Create a file with the specified contents and report any failures. */
1591 assertion_make_file(const char *file, int line,
1592 const char *path, int mode, int csize, const void *contents)
1594 #if defined(_WIN32) && !defined(__CYGWIN__)
1595 /* TODO: Rework this to set file mode as well. */
1597 (void)mode; /* UNUSED */
1598 assertion_count(file, line);
1599 f = fopen(path, "wb");
1601 failure_start(file, line, "Could not create file %s", path);
1602 failure_finish(NULL);
1605 if (contents != NULL) {
1609 wsize = strlen(contents);
1611 wsize = (size_t)csize;
1612 if (wsize != fwrite(contents, 1, wsize, f)) {
1614 failure_start(file, line,
1615 "Could not write file %s", path);
1616 failure_finish(NULL);
1624 assertion_count(file, line);
1625 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1627 failure_start(file, line, "Could not create %s", path);
1628 failure_finish(NULL);
1631 if (contents != NULL) {
1635 wsize = (ssize_t)strlen(contents);
1637 wsize = (ssize_t)csize;
1638 if (wsize != write(fd, contents, wsize)) {
1640 failure_start(file, line,
1641 "Could not write to %s", path);
1642 failure_finish(NULL);
1651 /* Create a hardlink and report any failures. */
1653 assertion_make_hardlink(const char *file, int line,
1654 const char *newpath, const char *linkto)
1658 assertion_count(file, line);
1659 #if defined(_WIN32) && !defined(__CYGWIN__)
1660 succeeded = my_CreateHardLinkA(newpath, linkto);
1662 succeeded = !link(linkto, newpath);
1668 failure_start(file, line, "Could not create hardlink");
1669 logprintf(" New link: %s\n", newpath);
1670 logprintf(" Old name: %s\n", linkto);
1671 failure_finish(NULL);
1675 /* Create a symlink and report any failures. */
1677 assertion_make_symlink(const char *file, int line,
1678 const char *newpath, const char *linkto)
1680 #if defined(_WIN32) && !defined(__CYGWIN__)
1681 int targetIsDir = 0; /* TODO: Fix this */
1682 assertion_count(file, line);
1683 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1686 assertion_count(file, line);
1687 if (0 == symlink(linkto, newpath))
1690 failure_start(file, line, "Could not create symlink");
1691 logprintf(" New link: %s\n", newpath);
1692 logprintf(" Old name: %s\n", linkto);
1693 failure_finish(NULL);
1697 /* Set umask, report failures. */
1699 assertion_umask(const char *file, int line, int mask)
1701 assertion_count(file, line);
1702 (void)file; /* UNUSED */
1703 (void)line; /* UNUSED */
1708 /* Set times, report failures. */
1710 assertion_utimes(const char *file, int line,
1711 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1715 #if defined(_WIN32) && !defined(__CYGWIN__)
1716 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1717 + (((nsec)/1000)*10))
1719 ULARGE_INTEGER wintm;
1720 FILETIME fatime, fmtime;
1721 FILETIME *pat, *pmt;
1723 assertion_count(file, line);
1724 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1725 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1726 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1727 if (h == INVALID_HANDLE_VALUE) {
1728 failure_start(file, line, "Can't access %s\n", pathname);
1729 failure_finish(NULL);
1733 if (at > 0 || at_nsec > 0) {
1734 wintm.QuadPart = WINTIME(at, at_nsec);
1735 fatime.dwLowDateTime = wintm.LowPart;
1736 fatime.dwHighDateTime = wintm.HighPart;
1740 if (mt > 0 || mt_nsec > 0) {
1741 wintm.QuadPart = WINTIME(mt, mt_nsec);
1742 fmtime.dwLowDateTime = wintm.LowPart;
1743 fmtime.dwHighDateTime = wintm.HighPart;
1747 if (pat != NULL || pmt != NULL)
1748 r = SetFileTime(h, NULL, pat, pmt);
1753 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1754 failure_finish(NULL);
1758 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1760 struct timeval times[2];
1762 #if !defined(__FreeBSD__)
1763 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1765 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1768 r = lstat(pathname, &st);
1770 failure_start(file, line, "Can't stat %s\n", pathname);
1771 failure_finish(NULL);
1775 if (mt == 0 && mt_nsec == 0) {
1777 #if defined(__FreeBSD__)
1778 mt_nsec = st.st_mtimespec.tv_nsec;
1779 /* FreeBSD generally only stores to microsecond res, so round. */
1780 mt_nsec = (mt_nsec / 1000) * 1000;
1783 if (at == 0 && at_nsec == 0) {
1785 #if defined(__FreeBSD__)
1786 at_nsec = st.st_atimespec.tv_nsec;
1787 /* FreeBSD generally only stores to microsecond res, so round. */
1788 at_nsec = (at_nsec / 1000) * 1000;
1792 times[1].tv_sec = mt;
1793 times[1].tv_usec = mt_nsec / 1000;
1795 times[0].tv_sec = at;
1796 times[0].tv_usec = at_nsec / 1000;
1799 r = lutimes(pathname, times);
1801 r = utimes(pathname, times);
1804 failure_start(file, line, "Can't utimes %s\n", pathname);
1805 failure_finish(NULL);
1809 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1812 /* Set nodump, report failures. */
1814 assertion_nodump(const char *file, int line, const char *pathname)
1816 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1819 assertion_count(file, line);
1820 r = chflags(pathname, UF_NODUMP);
1822 failure_start(file, line, "Can't set nodump %s\n", pathname);
1823 failure_finish(NULL);
1826 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1827 && defined(EXT2_NODUMP_FL)
1830 assertion_count(file, line);
1831 fd = open(pathname, O_RDONLY | O_NONBLOCK);
1833 failure_start(file, line, "Can't open %s\n", pathname);
1834 failure_finish(NULL);
1837 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1839 failure_start(file, line, "Can't get flags %s\n", pathname);
1840 failure_finish(NULL);
1843 flags |= EXT2_NODUMP_FL;
1844 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1846 failure_start(file, line, "Can't set nodump %s\n", pathname);
1847 failure_finish(NULL);
1852 (void)pathname; /* UNUSED */
1853 assertion_count(file, line);
1860 * UTILITIES for use by tests.
1865 * Check whether platform supports symlinks. This is intended
1866 * for tests to use in deciding whether to bother testing symlink
1867 * support; if the platform doesn't support symlinks, there's no point
1868 * in checking whether the program being tested can create them.
1870 * Note that the first time this test is called, we actually go out to
1871 * disk to create and verify a symlink. This is necessary because
1872 * symlink support is actually a property of a particular filesystem
1873 * and can thus vary between directories on a single system. After
1874 * the first call, this returns the cached result from memory, so it's
1875 * safe to call it as often as you wish.
1880 /* Remember the test result */
1881 static int value = 0, tested = 0;
1886 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1887 /* Note: Cygwin has its own symlink() emulation that does not
1888 * use the Win32 CreateSymbolicLink() function. */
1889 #if defined(_WIN32) && !defined(__CYGWIN__)
1890 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1891 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1893 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1894 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1899 /* Platform-dependent options for hiding the output of a subcommand. */
1900 #if defined(_WIN32) && !defined(__CYGWIN__)
1901 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1903 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1906 * Can this platform run the bzip2 program?
1911 static int tested = 0, value = 0;
1914 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
1921 * Can this platform run the grzip program?
1926 static int tested = 0, value = 0;
1929 if (systemf("grzip -V %s", redirectArgs) == 0)
1936 * Can this platform run the gzip program?
1941 static int tested = 0, value = 0;
1944 if (systemf("gzip -V %s", redirectArgs) == 0)
1951 * Can this platform run the lrzip program?
1954 canRunCommand(const char *cmd)
1956 static int tested = 0, value = 0;
1959 if (systemf("%s %s", cmd, redirectArgs) == 0)
1968 static int tested = 0, value = 0;
1971 if (systemf("lrzip -V %s", redirectArgs) == 0)
1978 * Can this platform run the lz4 program?
1983 static int tested = 0, value = 0;
1986 if (systemf("lz4 -V %s", redirectArgs) == 0)
1993 * Can this platform run the lzip program?
1998 static int tested = 0, value = 0;
2001 if (systemf("lzip -V %s", redirectArgs) == 0)
2008 * Can this platform run the lzma program?
2013 static int tested = 0, value = 0;
2016 if (systemf("lzma -V %s", redirectArgs) == 0)
2023 * Can this platform run the lzop program?
2028 static int tested = 0, value = 0;
2031 if (systemf("lzop -V %s", redirectArgs) == 0)
2038 * Can this platform run the xz program?
2043 static int tested = 0, value = 0;
2046 if (systemf("xz -V %s", redirectArgs) == 0)
2053 * Can this filesystem handle nodump flags.
2055 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2060 const char *path = "cannodumptest";
2063 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2064 if (chflags(path, UF_NODUMP) < 0)
2066 if (stat(path, &sb) < 0)
2068 if (sb.st_flags & UF_NODUMP)
2073 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2074 && defined(EXT2_NODUMP_FL)
2079 const char *path = "cannodumptest";
2082 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2083 fd = open(path, O_RDONLY | O_NONBLOCK);
2086 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2089 flags |= EXT2_NODUMP_FL;
2090 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2094 fd = open(path, O_RDONLY | O_NONBLOCK);
2097 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2101 if (flags & EXT2_NODUMP_FL)
2117 * Sleep as needed; useful for verifying disk timestamp changes by
2118 * ensuring that the wall-clock time has actually changed before we
2119 * go back to re-read something from disk.
2122 sleepUntilAfter(time_t t)
2124 while (t >= time(NULL))
2125 #if defined(_WIN32) && !defined(__CYGWIN__)
2133 * Call standard system() call, but build up the command line using
2134 * sprintf() conventions.
2137 systemf(const char *fmt, ...)
2144 vsprintf(buff, fmt, ap);
2145 if (verbosity > VERBOSITY_FULL)
2146 logprintf("Cmd: %s\n", buff);
2153 * Slurp a file into memory for ease of comparison and testing.
2154 * Returns size of file in 'sizep' if non-NULL, null-terminates
2155 * data in memory for ease of use.
2158 slurpfile(size_t * sizep, const char *fmt, ...)
2160 char filename[8192];
2169 vsprintf(filename, fmt, ap);
2172 f = fopen(filename, "rb");
2174 /* Note: No error; non-existent file is okay here. */
2177 r = fstat(fileno(f), &st);
2179 logprintf("Can't stat file %s\n", filename);
2183 p = malloc((size_t)st.st_size + 1);
2185 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2186 (long int)st.st_size, filename);
2190 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2191 if (bytes_read < st.st_size) {
2192 logprintf("Can't read file %s\n", filename);
2197 p[st.st_size] = '\0';
2199 *sizep = (size_t)st.st_size;
2205 * Slurp a file into memory for ease of comparison and testing.
2206 * Returns size of file in 'sizep' if non-NULL, null-terminates
2207 * data in memory for ease of use.
2210 dumpfile(const char *filename, void *data, size_t len)
2212 ssize_t bytes_written;
2215 f = fopen(filename, "wb");
2217 logprintf("Can't open file %s for writing\n", filename);
2220 bytes_written = fwrite(data, 1, len, f);
2221 if (bytes_written < (ssize_t)len)
2222 logprintf("Can't write file %s\n", filename);
2226 /* Read a uuencoded file from the reference directory, decode, and
2227 * write the result into the current directory. */
2228 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2229 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2231 extract_reference_file(const char *name)
2236 sprintf(buff, "%s/%s.uu", refdir, name);
2237 in = fopen(buff, "r");
2238 failure("Couldn't open reference file %s", buff);
2242 /* Read up to and including the 'begin' line. */
2244 if (fgets(buff, sizeof(buff), in) == NULL) {
2245 /* TODO: This is a failure. */
2248 if (memcmp(buff, "begin ", 6) == 0)
2251 /* Now, decode the rest and write it. */
2252 out = fopen(name, "wb");
2253 while (fgets(buff, sizeof(buff), in) != NULL) {
2257 if (memcmp(buff, "end", 3) == 0)
2260 bytes = UUDECODE(*p++);
2263 /* Write out 1-3 bytes from that. */
2265 assert(VALID_UUDECODE(p[0]));
2266 assert(VALID_UUDECODE(p[1]));
2267 n = UUDECODE(*p++) << 18;
2268 n |= UUDECODE(*p++) << 12;
2269 fputc(n >> 16, out);
2273 assert(VALID_UUDECODE(p[0]));
2274 n |= UUDECODE(*p++) << 6;
2275 fputc((n >> 8) & 0xFF, out);
2279 assert(VALID_UUDECODE(p[0]));
2280 n |= UUDECODE(*p++);
2281 fputc(n & 0xFF, out);
2291 copy_reference_file(const char *name)
2297 sprintf(buff, "%s/%s", refdir, name);
2298 in = fopen(buff, "rb");
2299 failure("Couldn't open reference file %s", buff);
2303 /* Now, decode the rest and write it. */
2304 /* Not a lot of error checking here; the input better be right. */
2305 out = fopen(name, "wb");
2306 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2307 if (fwrite(buff, 1, rbytes, out) != rbytes) {
2308 logprintf("Error: fwrite\n");
2317 is_LargeInode(const char *file)
2319 #if defined(_WIN32) && !defined(__CYGWIN__)
2320 BY_HANDLE_FILE_INFORMATION bhfi;
2323 r = my_GetFileInformationByName(file, &bhfi);
2326 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2331 if (stat(file, &st) < 0)
2333 ino = (int64_t)st.st_ino;
2334 return (ino > 0xffffffff);
2339 extract_reference_files(const char **names)
2341 while (names && *names)
2342 extract_reference_file(*names++);
2352 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2354 * DEFINE_TEST(test_function)
2358 /* Use "list.h" to declare all of the test functions. */
2360 #define DEFINE_TEST(name) void name(void);
2363 /* Use "list.h" to create a list of all tests (functions and names). */
2365 #define DEFINE_TEST(n) { n, #n, 0 },
2366 struct test_list_t tests[] = {
2371 * Summarize repeated failures in the just-completed test.
2374 test_summarize(int failed, int skips_num)
2378 switch (verbosity) {
2379 case VERBOSITY_SUMMARY_ONLY:
2380 printf(failed ? "E" : ".");
2383 case VERBOSITY_PASSFAIL:
2384 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
2388 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2390 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2391 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2392 logprintf("%s:%d: Summary: Failed %d times\n",
2393 failed_filename, i, failed_lines[i].count);
2395 /* Clear the failure history for the next file. */
2396 failed_filename = NULL;
2397 memset(failed_lines, 0, sizeof(failed_lines));
2401 * Actually run a single test, with appropriate setup and cleanup.
2404 test_run(int i, const char *tmpdir)
2407 char logfilename[64];
2408 int failures_before = failures;
2409 int skips_before = skips;
2412 switch (verbosity) {
2413 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2415 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2416 printf("%3d: %-64s", i, tests[i].name);
2419 default: /* Title of test, details will follow */
2420 printf("%3d: %s\n", i, tests[i].name);
2423 /* Chdir to the top-level work directory. */
2424 if (!assertChdir(tmpdir)) {
2426 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2429 /* Create a log file for this test. */
2430 sprintf(logfilename, "%s.log", tests[i].name);
2431 logfile = fopen(logfilename, "w");
2432 fprintf(logfile, "%s\n\n", tests[i].name);
2433 /* Chdir() to a work dir for this specific test. */
2434 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2435 testworkdir = workdir;
2436 if (!assertMakeDir(testworkdir, 0755)
2437 || !assertChdir(testworkdir)) {
2439 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2442 /* Explicitly reset the locale before each test. */
2443 setlocale(LC_ALL, "C");
2444 /* Record the umask before we run the test. */
2445 umask(oldumask = umask(0));
2447 * Run the actual test.
2451 * Clean up and report afterwards.
2457 setlocale(LC_ALL, "C");
2458 /* Reset directory. */
2459 if (!assertChdir(tmpdir)) {
2460 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2464 /* Report per-test summaries. */
2465 tests[i].failures = failures - failures_before;
2466 test_summarize(tests[i].failures, skips - skips_before);
2467 /* Close the per-test log file. */
2470 /* If there were no failures, we can remove the work dir and logfile. */
2471 if (tests[i].failures == 0) {
2472 if (!keep_temp_files && assertChdir(tmpdir)) {
2473 #if defined(_WIN32) && !defined(__CYGWIN__)
2474 /* Make sure not to leave empty directories.
2475 * Sometimes a processing of closing files used by tests
2476 * is not done, then rmdir will be failed and it will
2477 * leave a empty test directory. So we should wait a few
2478 * seconds and retry rmdir. */
2480 for (t = 0; t < 10; t++) {
2483 r = systemf("rmdir /S /Q %s", tests[i].name);
2487 systemf("del %s", logfilename);
2489 systemf("rm -rf %s", tests[i].name);
2490 systemf("rm %s", logfilename);
2494 /* Return appropriate status. */
2495 return (tests[i].failures);
2501 * MAIN and support routines.
2507 usage(const char *program)
2509 static const int limit = sizeof(tests) / sizeof(tests[0]);
2512 printf("Usage: %s [options] <test> <test> ...\n", program);
2513 printf("Default is to run all tests.\n");
2514 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2515 printf("Options:\n");
2516 printf(" -d Dump core after any failure, for debugging.\n");
2517 printf(" -k Keep all temp files.\n");
2518 printf(" Default: temp files for successful tests deleted.\n");
2520 printf(" -p <path> Path to executable to be tested.\n");
2521 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2523 printf(" -q Quiet.\n");
2524 printf(" -r <dir> Path to dir containing reference files.\n");
2525 printf(" Default: Current directory.\n");
2526 printf(" -u Keep running specifies tests until one fails.\n");
2527 printf(" -v Verbose.\n");
2528 printf("Available tests:\n");
2529 for (i = 0; i < limit; i++)
2530 printf(" %d: %s\n", i, tests[i].name);
2535 get_refdir(const char *d)
2537 size_t tried_size, buff_size;
2538 char *buff, *tried, *pwd = NULL, *p = NULL;
2541 buff_size = PATH_MAX;
2545 buff = calloc(buff_size, 1);
2547 fprintf(stderr, "Unable to allocate memory\n");
2551 /* Allocate a buffer to hold the various directories we checked. */
2552 tried_size = buff_size * 2;
2553 tried = calloc(tried_size, 1);
2554 if (tried == NULL) {
2555 fprintf(stderr, "Unable to allocate memory\n");
2559 /* If a dir was specified, try that */
2562 snprintf(buff, buff_size, "%s", d);
2563 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2564 if (p != NULL) goto success;
2565 strncat(tried, buff, tried_size - strlen(tried) - 1);
2566 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2570 /* Get the current dir. */
2572 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2574 pwd = getcwd(NULL, 0);
2576 while (pwd[strlen(pwd) - 1] == '\n')
2577 pwd[strlen(pwd) - 1] = '\0';
2579 /* Look for a known file. */
2580 snprintf(buff, buff_size, "%s", pwd);
2581 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2582 if (p != NULL) goto success;
2583 strncat(tried, buff, tried_size - strlen(tried) - 1);
2584 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2586 snprintf(buff, buff_size, "%s/test", pwd);
2587 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2588 if (p != NULL) goto success;
2589 strncat(tried, buff, tried_size - strlen(tried) - 1);
2590 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2592 #if defined(LIBRARY)
2593 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
2595 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
2597 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2598 if (p != NULL) goto success;
2599 strncat(tried, buff, tried_size - strlen(tried) - 1);
2600 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2602 #if defined(PROGRAM_ALIAS)
2603 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
2604 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2605 if (p != NULL) goto success;
2606 strncat(tried, buff, tried_size - strlen(tried) - 1);
2607 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2610 if (memcmp(pwd, "/usr/obj", 8) == 0) {
2611 snprintf(buff, buff_size, "%s", pwd + 8);
2612 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2613 if (p != NULL) goto success;
2614 strncat(tried, buff, tried_size - strlen(tried) - 1);
2615 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2617 snprintf(buff, buff_size, "%s/test", pwd + 8);
2618 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2619 if (p != NULL) goto success;
2620 strncat(tried, buff, tried_size - strlen(tried) - 1);
2621 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2625 printf("Unable to locate known reference file %s\n", KNOWNREF);
2626 printf(" Checked following directories:\n%s\n", tried);
2627 printf("Use -r option to specify full path to reference directory\n");
2628 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2638 /* Copy result into a fresh buffer to reduce memory usage. */
2645 main(int argc, char **argv)
2647 static const int limit = sizeof(tests) / sizeof(tests[0]);
2648 int test_set[sizeof(tests) / sizeof(tests[0])];
2649 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2651 char *refdir_alloc = NULL;
2652 const char *progname;
2654 const char *tmp, *option_arg, *p;
2655 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2656 char tmpdir_timestamp[256];
2658 (void)argc; /* UNUSED */
2660 /* Get the current dir. */
2662 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2664 pwd = getcwd(NULL, 0);
2666 while (pwd[strlen(pwd) - 1] == '\n')
2667 pwd[strlen(pwd) - 1] = '\0';
2669 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
2670 /* To stop to run the default invalid parameter handler. */
2671 _set_invalid_parameter_handler(invalid_parameter_handler);
2672 /* Disable annoying assertion message box. */
2673 _CrtSetReportMode(_CRT_ASSERT, 0);
2677 * Name of this program, used to build root of our temp directory
2680 progname = p = argv[0];
2681 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2683 fprintf(stderr, "ERROR: Out of memory.");
2686 strcpy(testprogdir, progname);
2687 while (*p != '\0') {
2688 /* Support \ or / dir separators for Windows compat. */
2689 if (*p == '/' || *p == '\\')
2697 testprogdir[i] = '\0';
2698 #if defined(_WIN32) && !defined(__CYGWIN__)
2699 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2700 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2701 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2702 testprogdir[1] == ':' &&
2703 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
2705 if (testprogdir[0] != '/')
2708 /* Fixup path for relative directories. */
2709 if ((testprogdir = (char *)realloc(testprogdir,
2710 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2712 fprintf(stderr, "ERROR: Out of memory.");
2715 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2716 strlen(testprogdir) + 1);
2717 memcpy(testprogdir, pwd, strlen(pwd));
2718 testprogdir[strlen(pwd)] = '/';
2722 /* Get the target program from environment, if available. */
2723 testprogfile = getenv(ENVBASE);
2726 if (getenv("TMPDIR") != NULL)
2727 tmp = getenv("TMPDIR");
2728 else if (getenv("TMP") != NULL)
2729 tmp = getenv("TMP");
2730 else if (getenv("TEMP") != NULL)
2731 tmp = getenv("TEMP");
2732 else if (getenv("TEMPDIR") != NULL)
2733 tmp = getenv("TEMPDIR");
2737 /* Allow -d to be controlled through the environment. */
2738 if (getenv(ENVBASE "_DEBUG") != NULL)
2739 dump_on_failure = 1;
2741 /* Allow -v to be controlled through the environment. */
2742 if (getenv("_VERBOSITY_LEVEL") != NULL)
2744 vlevel = getenv("_VERBOSITY_LEVEL");
2745 verbosity = atoi(vlevel);
2746 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2748 /* Unsupported verbosity levels are silently ignored */
2750 verbosity = VERBOSITY_PASSFAIL;
2754 /* Get the directory holding test files from environment. */
2755 refdir = getenv(ENVBASE "_TEST_FILES");
2758 * Parse options, without using getopt(), which isn't available
2761 ++argv; /* Skip program name */
2762 while (*argv != NULL) {
2767 while (*p != '\0') {
2770 /* If 'opt' takes an argument, parse that. */
2771 if (option == 'p' || option == 'r') {
2774 else if (*argv == NULL) {
2776 "Option -%c requires argument.\n",
2780 option_arg = *argv++;
2781 p = ""; /* End of this option word. */
2784 /* Now, handle the option. */
2787 dump_on_failure = 1;
2790 keep_temp_files = 1;
2794 testprogfile = option_arg;
2796 fprintf(stderr, "-p option not permitted\n");
2805 refdir = option_arg;
2815 fprintf(stderr, "Unrecognized option '%c'\n",
2823 * Sanity-check that our options make sense.
2826 if (testprogfile == NULL)
2828 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2829 strlen(PROGRAM) + 1)) == NULL)
2831 fprintf(stderr, "ERROR: Out of memory.");
2834 strcpy(tmp2, testprogdir);
2836 strcat(tmp2, PROGRAM);
2837 testprogfile = tmp2;
2842 #if defined(_WIN32) && !defined(__CYGWIN__)
2843 /* Command.com sometimes rejects '/' separators. */
2844 testprg = strdup(testprogfile);
2845 for (i = 0; testprg[i] != '\0'; i++) {
2846 if (testprg[i] == '/')
2849 testprogfile = testprg;
2851 /* Quote the name that gets put into shell command lines. */
2852 testprg = malloc(strlen(testprogfile) + 3);
2853 strcpy(testprg, "\"");
2854 strcat(testprg, testprogfile);
2855 strcat(testprg, "\"");
2860 #if !defined(_WIN32) && defined(SIGPIPE)
2861 { /* Ignore SIGPIPE signals */
2862 struct sigaction sa;
2863 sa.sa_handler = SIG_IGN;
2864 sigemptyset(&sa.sa_mask);
2866 sigaction(SIGPIPE, &sa, NULL);
2871 * Create a temp directory for the following tests.
2872 * Include the time the tests started as part of the name,
2873 * to make it easier to track the results of multiple tests.
2876 for (i = 0; ; i++) {
2877 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2878 "%Y-%m-%dT%H.%M.%S",
2880 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2881 tmpdir_timestamp, i);
2882 if (assertMakeDir(tmpdir,0755))
2886 "ERROR: Unable to create temp directory %s\n",
2893 * If the user didn't specify a directory for locating
2894 * reference files, try to find the reference files in
2895 * the "usual places."
2897 refdir = refdir_alloc = get_refdir(refdir);
2900 * Banner with basic information.
2903 printf("If tests fail or crash, details will be in:\n");
2904 printf(" %s\n", tmpdir);
2906 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2907 printf("Reference files will be read from: %s\n", refdir);
2909 printf("Running tests on: %s\n", testprog);
2911 printf("Exercising: ");
2913 printf("%s\n", EXTRA_VERSION);
2920 * Run some or all of the individual tests.
2928 test_num = get_test_set(test_set, limit, *argv, tests);
2930 printf("*** INVALID Test %s\n", *argv);
2936 for (i = 0; i < test_num; i++) {
2938 if (test_run(test_set[i], tmpdir)) {
2946 } while (*argv != NULL);
2947 } while (until_failure);
2950 /* Must be freed after all tests run */
2956 * Report summary statistics.
2958 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2960 printf("Totals:\n");
2961 printf(" Tests run: %8d\n", tests_run);
2962 printf(" Tests failed: %8d\n", tests_failed);
2963 printf(" Assertions checked:%8d\n", assertions);
2964 printf(" Assertions failed: %8d\n", failures);
2965 printf(" Skips reported: %8d\n", skips);
2969 printf("Failing tests:\n");
2970 for (i = 0; i < limit; ++i) {
2971 if (tests[i].failures)
2972 printf(" %d: %s (%d failures)\n", i,
2973 tests[i].name, tests[i].failures);
2976 printf("Details for failing tests: %s\n", tmpdir);
2979 if (verbosity == VERBOSITY_SUMMARY_ONLY)
2981 printf("%d tests passed, no failures\n", tests_run);
2986 /* If the final tmpdir is empty, we can remove it. */
2987 /* This should be the usual case when all tests succeed. */
2991 return (tests_failed ? 1 : 0);