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 __FBSDID("$FreeBSD: src/usr.bin/tar/test/main.c,v 1.6 2008/11/05 06:40:53 kientzle Exp $");
64 #define KNOWNREF "test_patterns_2.tar.uu"
65 #define ENVBASE "BSDTAR" /* Prefix for environment variables. */
66 #define PROGRAM "bsdtar" /* Name of program being tested. */
67 #define PROGRAM_ALIAS "tar" /* Generic alias for program */
68 #undef LIBRARY /* Not testing a library. */
69 #undef EXTRA_DUMP /* How to dump extra data */
70 #undef EXTRA_ERRNO /* How to dump errno */
71 /* How to generate extra version info. */
72 #define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "")
76 * Windows support routines
78 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
79 * in the test harness is dangerous because they cover up
80 * configuration errors. The classic example of this is omitting a
81 * configure check. If libarchive and libarchive_test both look for
82 * the same feature macro, such errors are hard to detect. Platform
83 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
84 * easily lead to very messy code. It's best to limit yourself
85 * to only the most generic programming techniques in the test harness
86 * and thus avoid conditionals altogether. Where that's not possible,
87 * try to minimize conditionals by grouping platform-specific tests in
88 * one place (e.g., test_acl_freebsd) or by adding new assert()
89 * functions (e.g., assertMakeHardlink()) to cover up platform
90 * differences. Platform-specific coding in libarchive_test is often
91 * a symptom that some capability is missing from libarchive itself.
93 #if defined(_WIN32) && !defined(__CYGWIN__)
101 #define S_ISDIR(m) ((m) & _S_IFDIR)
104 #define S_ISREG(m) ((m) & _S_IFREG)
106 #if !defined(__BORLANDC__)
107 #define access _access
112 #define fileno _fileno
114 /*#define fstat _fstat64*/
115 #if !defined(__BORLANDC__)
116 #define getcwd _getcwd
119 /*#define lstat _stat64*/
120 /*#define stat _stat64*/
122 #if !defined(__BORLANDC__)
123 #define strdup _strdup
126 #define int64_t __int64
129 #if defined(HAVE__CrtSetReportMode)
133 /* Path to working directory for current test */
134 const char *testworkdir;
136 /* Pathname of exe to be tested. */
137 const char *testprogfile;
138 /* Name of exe to use in printf-formatted command strings. */
139 /* On Windows, this includes leading/trailing quotes. */
140 const char *testprog;
143 #if defined(_WIN32) && !defined(__CYGWIN__)
144 static void *GetFunctionKernel32(const char *);
145 static int my_CreateSymbolicLinkA(const char *, const char *, int);
146 static int my_CreateHardLinkA(const char *, const char *);
147 static int my_GetFileInformationByName(const char *,
148 BY_HANDLE_FILE_INFORMATION *);
151 GetFunctionKernel32(const char *name)
153 static HINSTANCE lib;
157 lib = LoadLibrary("kernel32.dll");
160 fprintf(stderr, "Can't load kernel32.dll?!\n");
163 return (void *)GetProcAddress(lib, name);
167 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
169 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
173 f = GetFunctionKernel32("CreateSymbolicLinkA");
175 return f == NULL ? 0 : (*f)(linkname, target, flags);
179 my_CreateHardLinkA(const char *linkname, const char *target)
181 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
185 f = GetFunctionKernel32("CreateHardLinkA");
187 return f == NULL ? 0 : (*f)(linkname, target, NULL);
191 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
196 memset(bhfi, 0, sizeof(*bhfi));
197 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
198 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
199 if (h == INVALID_HANDLE_VALUE)
201 r = GetFileInformationByHandle(h, bhfi);
207 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
209 invalid_parameter_handler(const wchar_t * expression,
210 const wchar_t * function, const wchar_t * file,
211 unsigned int line, uintptr_t pReserved)
223 /* Enable core dump on failure. */
224 static int dump_on_failure = 0;
225 /* Default is to remove temp dirs and log data for successful tests. */
226 static int keep_temp_files = 0;
227 /* Default is to run the specified tests once and report errors. */
228 static int until_failure = 0;
229 /* Default is to just report pass/fail for each test. */
230 static int verbosity = 0;
231 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
232 #define VERBOSITY_PASSFAIL 0 /* Default */
233 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
234 #define VERBOSITY_FULL 2 /* -vv */
235 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
236 * mostly for debugging the test harness itself. */
237 /* Cumulative count of assertion failures. */
238 static int failures = 0;
239 /* Cumulative count of reported skips. */
240 static int skips = 0;
241 /* Cumulative count of assertions checked. */
242 static int assertions = 0;
244 /* Directory where uuencoded reference files can be found. */
245 static const char *refdir;
248 * Report log information selectively to console and/or disk log.
250 static int log_console = 0;
251 static FILE *logfile;
253 vlogprintf(const char *fmt, va_list ap)
260 vfprintf(stdout, fmt, ap);
263 vfprintf(logfile, fmt, lfap);
266 vfprintf(logfile, fmt, ap);
271 logprintf(const char *fmt, ...)
279 /* Set up a message to display only if next assertion fails. */
280 static char msgbuff[4096];
281 static const char *msg, *nextmsg;
283 failure(const char *fmt, ...)
290 vsprintf(msgbuff, fmt, ap);
297 * Copy arguments into file-local variables.
298 * This was added to permit vararg assert() functions without needing
299 * variadic wrapper macros. Turns out that the vararg capability is almost
300 * never used, so almost all of the vararg assertions can be simplified
301 * by removing the vararg capability and reworking the wrapper macro to
302 * pass __FILE__, __LINE__ directly into the function instead of using
303 * this hook. I suspect this machinery is used so rarely that we
304 * would be better off just removing it entirely. That would simplify
305 * the code here noticeably.
307 static const char *skipping_filename;
308 static int skipping_line;
309 void skipping_setup(const char *filename, int line)
311 skipping_filename = filename;
312 skipping_line = line;
315 /* Called at the beginning of each assert() function. */
317 assertion_count(const char *file, int line)
319 (void)file; /* UNUSED */
320 (void)line; /* UNUSED */
322 /* Proper handling of "failure()" message. */
325 /* Uncomment to print file:line after every assertion.
326 * Verbose, but occasionally useful in tracking down crashes. */
327 /* printf("Checked %s:%d\n", file, line); */
331 * For each test source file, we remember how many times each
332 * assertion was reported. Cleared before each new test,
333 * used by test_summarize().
338 } failed_lines[10000];
339 const char *failed_filename;
341 /* Count this failure, setup up log destination and handle initial report. */
343 failure_start(const char *filename, int line, const char *fmt, ...)
347 /* Record another failure for this line. */
349 failed_filename = filename;
350 failed_lines[line].count++;
352 /* Determine whether to log header to console. */
354 case VERBOSITY_LIGHT_REPORT:
355 log_console = (failed_lines[line].count < 2);
358 log_console = (verbosity >= VERBOSITY_FULL);
361 /* Log file:line header for this failure */
364 logprintf("%s(%d): ", filename, line);
366 logprintf("%s:%d: ", filename, line);
372 if (msg != NULL && msg[0] != '\0') {
373 logprintf(" Description: %s\n", msg);
377 /* Determine whether to log details to console. */
378 if (verbosity == VERBOSITY_LIGHT_REPORT)
382 /* Complete reporting of failed tests. */
384 * The 'extra' hook here is used by libarchive to include libarchive
385 * error messages with assertion failures. It could also be used
386 * to add strerror() output, for example. Just define the EXTRA_DUMP()
387 * macro appropriately.
390 failure_finish(void *extra)
392 (void)extra; /* UNUSED (maybe) */
395 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
396 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
400 if (dump_on_failure) {
402 " *** forcing core dump so failure can be debugged ***\n");
407 /* Inform user that we're skipping some checks. */
409 test_skipping(const char *fmt, ...)
415 vsprintf(buff, fmt, ap);
417 /* Use failure() message if set. */
420 /* failure_start() isn't quite right, but is awfully convenient. */
421 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
422 --failures; /* Undo failures++ in failure_start() */
423 /* Don't failure_finish() here. */
424 /* Mark as skip, so doesn't count as failed test. */
425 failed_lines[skipping_line].skip = 1;
435 /* Generic assert() just displays the failed condition. */
437 assertion_assert(const char *file, int line, int value,
438 const char *condition, void *extra)
440 assertion_count(file, line);
442 failure_start(file, line, "Assertion failed: %s", condition);
443 failure_finish(extra);
448 /* chdir() and report any errors */
450 assertion_chdir(const char *file, int line, const char *pathname)
452 assertion_count(file, line);
453 if (chdir(pathname) == 0)
455 failure_start(file, line, "chdir(\"%s\")", pathname);
456 failure_finish(NULL);
461 /* Verify two integers are equal. */
463 assertion_equal_int(const char *file, int line,
464 long long v1, const char *e1, long long v2, const char *e2, void *extra)
466 assertion_count(file, line);
469 failure_start(file, line, "%s != %s", e1, e2);
470 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
471 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
472 failure_finish(extra);
477 * Utility to convert a single UTF-8 sequence.
480 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
482 static const char utf8_count[256] = {
483 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
484 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
485 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
486 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
487 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
488 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
489 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
490 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
491 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
493 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
494 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
495 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
496 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
497 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
498 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
510 * Decode 1-4 bytes depending on the value of the first byte.
512 ch = (unsigned char)*s;
514 return (0); /* Standard: return 0 for end-of-string. */
515 cnt = utf8_count[ch];
517 /* Invalide sequence or there are not plenty bytes. */
521 /* Make a Unicode code point from a single UTF-8 sequence. */
523 case 1: /* 1 byte sequence. */
526 case 2: /* 2 bytes sequence. */
527 if ((s[1] & 0xc0) != 0x80) return (-1);
528 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
530 case 3: /* 3 bytes sequence. */
531 if ((s[1] & 0xc0) != 0x80) return (-1);
532 if ((s[2] & 0xc0) != 0x80) return (-1);
533 wc = ((ch & 0x0f) << 12)
534 | ((s[1] & 0x3f) << 6)
537 return (-1);/* Overlong sequence. */
539 case 4: /* 4 bytes sequence. */
542 if ((s[1] & 0xc0) != 0x80) return (-1);
543 if ((s[2] & 0xc0) != 0x80) return (-1);
544 if ((s[3] & 0xc0) != 0x80) return (-1);
545 wc = ((ch & 0x07) << 18)
546 | ((s[1] & 0x3f) << 12)
547 | ((s[2] & 0x3f) << 6)
550 return (-1);/* Overlong sequence. */
556 /* The code point larger than 0x10FFFF is not leagal
560 /* Correctly gets a Unicode, returns used bytes. */
565 static void strdump(const char *e, const char *p, int ewidth, int utf8)
569 logprintf(" %*s = ", ewidth, e);
576 unsigned int c = 0xff & *p++;
578 case '\a': logprintf("\\a"); break;
579 case '\b': logprintf("\\b"); break;
580 case '\n': logprintf("\\n"); break;
581 case '\r': logprintf("\\r"); break;
583 if (c >= 32 && c < 127)
586 logprintf("\\x%02X", c);
590 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
593 * If the current string is UTF-8, dump its code points.
604 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
607 logprintf("%04X", uc);
613 logprintf(" (count %d", cnt);
615 logprintf(",unknown %d bytes", len);
623 /* Verify two strings are equal, dump them if not. */
625 assertion_equal_string(const char *file, int line,
626 const char *v1, const char *e1,
627 const char *v2, const char *e2,
628 void *extra, int utf8)
632 assertion_count(file, line);
633 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
635 failure_start(file, line, "%s != %s", e1, e2);
636 l1 = (int)strlen(e1);
637 l2 = (int)strlen(e2);
640 strdump(e1, v1, l1, utf8);
641 strdump(e2, v2, l1, utf8);
642 failure_finish(extra);
647 wcsdump(const char *e, const wchar_t *w)
649 logprintf(" %s = ", e);
655 while (*w != L'\0') {
656 unsigned int c = *w++;
657 if (c >= 32 && c < 127)
660 logprintf("\\x%02X", c);
661 else if (c < 0x10000)
662 logprintf("\\u%04X", c);
664 logprintf("\\U%08X", c);
671 wcscmp(const wchar_t *s1, const wchar_t *s2)
674 while (*s1 == *s2++) {
685 /* Verify that two wide strings are equal, dump them if not. */
687 assertion_equal_wstring(const char *file, int line,
688 const wchar_t *v1, const char *e1,
689 const wchar_t *v2, const char *e2,
692 assertion_count(file, line);
695 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
697 failure_start(file, line, "%s != %s", e1, e2);
700 failure_finish(extra);
705 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
706 * any bytes in p that differ from ref will be highlighted with '_'
707 * before and after the hex value.
710 hexdump(const char *p, const char *ref, size_t l, size_t offset)
716 logprintf("(null)\n");
719 for(i=0; i < l; i+=16) {
720 logprintf("%04x", (unsigned)(i + offset));
722 for (j = 0; j < 16 && i + j < l; j++) {
723 if (ref != NULL && p[i + j] != ref[i + j])
725 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
726 if (ref != NULL && p[i + j] == ref[i + j])
729 for (; j < 16; j++) {
730 logprintf("%c ", sep);
733 logprintf("%c", sep);
734 for (j=0; j < 16 && i + j < l; j++) {
736 if (c >= ' ' && c <= 126)
745 /* Verify that two blocks of memory are the same, display the first
746 * block of differences if they're not. */
748 assertion_equal_mem(const char *file, int line,
749 const void *_v1, const char *e1,
750 const void *_v2, const char *e2,
751 size_t l, const char *ld, void *extra)
753 const char *v1 = (const char *)_v1;
754 const char *v2 = (const char *)_v2;
757 assertion_count(file, line);
758 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
760 if (v1 == NULL || v2 == NULL)
763 failure_start(file, line, "%s != %s", e1, e2);
764 logprintf(" size %s = %d\n", ld, (int)l);
765 /* Dump 48 bytes (3 lines) so that the first difference is
766 * in the second line. */
768 while (l > 64 && memcmp(v1, v2, 32) == 0) {
769 /* Two lines agree, so step forward one line. */
775 logprintf(" Dump of %s\n", e1);
776 hexdump(v1, v2, l < 128 ? l : 128, offset);
777 logprintf(" Dump of %s\n", e2);
778 hexdump(v2, v1, l < 128 ? l : 128, offset);
780 failure_finish(extra);
784 /* Verify that a block of memory is filled with the specified byte. */
786 assertion_memory_filled_with(const char *file, int line,
787 const void *_v1, const char *vd,
788 size_t l, const char *ld,
789 char b, const char *bd, void *extra)
791 const char *v1 = (const char *)_v1;
794 (void)ld; /* UNUSED */
796 assertion_count(file, line);
798 for (i = 0; i < l; ++i) {
806 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
807 logprintf(" Only %d bytes were correct\n", (int)c);
808 failure_finish(extra);
812 /* Verify that the named file exists and is empty. */
814 assertion_empty_file(const char *filename, int line, const char *f1)
821 assertion_count(filename, line);
823 if (stat(f1, &st) != 0) {
824 failure_start(filename, line, "Stat failed: %s", f1);
825 failure_finish(NULL);
831 failure_start(filename, line, "File should be empty: %s", f1);
832 logprintf(" File size: %d\n", (int)st.st_size);
833 logprintf(" Contents:\n");
836 logprintf(" Unable to open %s\n", f1);
838 s = ((off_t)sizeof(buff) < st.st_size) ?
839 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
840 s = fread(buff, 1, s, f);
841 hexdump(buff, NULL, s, 0);
844 failure_finish(NULL);
848 /* Verify that the named file exists and is not empty. */
850 assertion_non_empty_file(const char *filename, int line, const char *f1)
854 assertion_count(filename, line);
856 if (stat(f1, &st) != 0) {
857 failure_start(filename, line, "Stat failed: %s", f1);
858 failure_finish(NULL);
861 if (st.st_size == 0) {
862 failure_start(filename, line, "File empty: %s", f1);
863 failure_finish(NULL);
869 /* Verify that two files have the same contents. */
870 /* TODO: hexdump the first bytes that actually differ. */
872 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
879 assertion_count(filename, line);
881 f1 = fopen(fn1, "rb");
882 f2 = fopen(fn2, "rb");
883 if (f1 == NULL || f2 == NULL) {
889 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
890 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
893 if (n1 == 0 && n2 == 0) {
898 if (memcmp(buff1, buff2, n1) != 0)
903 failure_start(filename, line, "Files not identical");
904 logprintf(" file1=\"%s\"\n", fn1);
905 logprintf(" file2=\"%s\"\n", fn2);
906 failure_finish(NULL);
910 /* Verify that the named file does exist. */
912 assertion_file_exists(const char *filename, int line, const char *f)
914 assertion_count(filename, line);
916 #if defined(_WIN32) && !defined(__CYGWIN__)
920 if (!access(f, F_OK))
923 failure_start(filename, line, "File should exist: %s", f);
924 failure_finish(NULL);
928 /* Verify that the named file doesn't exist. */
930 assertion_file_not_exists(const char *filename, int line, const char *f)
932 assertion_count(filename, line);
934 #if defined(_WIN32) && !defined(__CYGWIN__)
941 failure_start(filename, line, "File should not exist: %s", f);
942 failure_finish(NULL);
946 /* Compare the contents of a file to a block of memory. */
948 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
954 assertion_count(filename, line);
958 failure_start(filename, line,
959 "File should exist: %s", fn);
960 failure_finish(NULL);
963 contents = malloc(s * 2);
964 n = (int)fread(contents, 1, s * 2, f);
966 if (n == s && memcmp(buff, contents, s) == 0) {
970 failure_start(filename, line, "File contents don't match");
971 logprintf(" file=\"%s\"\n", fn);
973 hexdump(contents, buff, n > 512 ? 512 : n, 0);
975 logprintf(" File empty, contents should be:\n");
976 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
978 failure_finish(NULL);
983 /* Check the contents of a text file, being tolerant of line endings. */
985 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
988 const char *btxt, *ftxt;
992 assertion_count(filename, line);
995 failure_start(filename, line,
996 "File doesn't exist: %s", fn);
997 failure_finish(NULL);
1000 s = (int)strlen(buff);
1001 contents = malloc(s * 2 + 128);
1002 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1006 /* Compare texts. */
1008 ftxt = (const char *)contents;
1009 while (*btxt != '\0' && *ftxt != '\0') {
1010 if (*btxt == *ftxt) {
1015 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1016 /* Pass over different new line characters. */
1023 if (*btxt == '\0' && *ftxt == '\0') {
1027 failure_start(filename, line, "Contents don't match");
1028 logprintf(" file=\"%s\"\n", fn);
1030 hexdump(contents, buff, n, 0);
1031 logprintf(" expected\n", fn);
1032 hexdump(buff, contents, s, 0);
1034 logprintf(" File empty, contents should be:\n");
1035 hexdump(buff, NULL, s, 0);
1037 failure_finish(NULL);
1042 /* Verify that a text file contains the specified lines, regardless of order */
1043 /* This could be more efficient if we sorted both sets of lines, etc, but
1044 * since this is used only for testing and only ever deals with a dozen or so
1045 * lines at a time, this relatively crude approach is just fine. */
1047 assertion_file_contains_lines_any_order(const char *file, int line,
1048 const char *pathname, const char *lines[])
1052 size_t expected_count, actual_count, i, j;
1053 char **expected = NULL;
1054 char *p, **actual = NULL;
1056 int expected_failure = 0, actual_failure = 0;
1058 assertion_count(file, line);
1060 buff = slurpfile(&buff_size, "%s", pathname);
1062 failure_start(pathname, line, "Can't read file: %s", pathname);
1063 failure_finish(NULL);
1067 /* Make a copy of the provided lines and count up the expected
1069 for (i = 0; lines[i] != NULL; ++i) {
1072 if (expected_count) {
1073 expected = malloc(sizeof(char *) * expected_count);
1074 if (expected == NULL) {
1075 failure_start(pathname, line, "Can't allocate memory");
1076 failure_finish(NULL);
1080 for (i = 0; lines[i] != NULL; ++i) {
1081 expected[i] = strdup(lines[i]);
1085 /* Break the file into lines */
1087 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1088 if (*p == '\x0d' || *p == '\x0a')
1090 if (c == '\0' && *p != '\0')
1095 actual = calloc(sizeof(char *), actual_count);
1096 if (actual == NULL) {
1097 failure_start(pathname, line, "Can't allocate memory");
1098 failure_finish(NULL);
1102 for (j = 0, p = buff; p < buff + buff_size;
1103 p += 1 + strlen(p)) {
1111 /* Erase matching lines from both lists */
1112 for (i = 0; i < expected_count; ++i) {
1113 if (expected[i] == NULL)
1115 for (j = 0; j < actual_count; ++j) {
1116 if (actual[j] == NULL)
1118 if (strcmp(expected[i], actual[j]) == 0) {
1127 /* If there's anything left, it's a failure */
1128 for (i = 0; i < expected_count; ++i) {
1129 if (expected[i] != NULL)
1132 for (j = 0; j < actual_count; ++j) {
1133 if (actual[j] != NULL)
1136 if (expected_failure == 0 && actual_failure == 0) {
1142 failure_start(file, line, "File doesn't match: %s", pathname);
1143 for (i = 0; i < expected_count; ++i) {
1144 if (expected[i] != NULL) {
1145 logprintf(" Expected but not present: %s\n", expected[i]);
1149 for (j = 0; j < actual_count; ++j) {
1150 if (actual[j] != NULL)
1151 logprintf(" Present but not expected: %s\n", actual[j]);
1153 failure_finish(NULL);
1160 /* Test that two paths point to the same file. */
1161 /* As a side-effect, asserts that both files exist. */
1163 is_hardlink(const char *file, int line,
1164 const char *path1, const char *path2)
1166 #if defined(_WIN32) && !defined(__CYGWIN__)
1167 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1170 assertion_count(file, line);
1171 r = my_GetFileInformationByName(path1, &bhfi1);
1173 failure_start(file, line, "File %s can't be inspected?", path1);
1174 failure_finish(NULL);
1177 r = my_GetFileInformationByName(path2, &bhfi2);
1179 failure_start(file, line, "File %s can't be inspected?", path2);
1180 failure_finish(NULL);
1183 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1184 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1185 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1187 struct stat st1, st2;
1190 assertion_count(file, line);
1191 r = lstat(path1, &st1);
1193 failure_start(file, line, "File should exist: %s", path1);
1194 failure_finish(NULL);
1197 r = lstat(path2, &st2);
1199 failure_start(file, line, "File should exist: %s", path2);
1200 failure_finish(NULL);
1203 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1208 assertion_is_hardlink(const char *file, int line,
1209 const char *path1, const char *path2)
1211 if (is_hardlink(file, line, path1, path2))
1213 failure_start(file, line,
1214 "Files %s and %s are not hardlinked", path1, path2);
1215 failure_finish(NULL);
1220 assertion_is_not_hardlink(const char *file, int line,
1221 const char *path1, const char *path2)
1223 if (!is_hardlink(file, line, path1, path2))
1225 failure_start(file, line,
1226 "Files %s and %s should not be hardlinked", path1, path2);
1227 failure_finish(NULL);
1231 /* Verify a/b/mtime of 'pathname'. */
1232 /* If 'recent', verify that it's within last 10 seconds. */
1234 assertion_file_time(const char *file, int line,
1235 const char *pathname, long t, long nsec, char type, int recent)
1237 long long filet, filet_nsec;
1240 #if defined(_WIN32) && !defined(__CYGWIN__)
1241 #define EPOC_TIME (116444736000000000ULL)
1242 FILETIME fxtime, fbirthtime, fatime, fmtime;
1243 ULARGE_INTEGER wintm;
1245 fxtime.dwLowDateTime = 0;
1246 fxtime.dwHighDateTime = 0;
1248 assertion_count(file, line);
1249 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1250 * a directory file. If not, CreateFile() will fail when
1251 * the pathname is a directory. */
1252 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1253 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1254 if (h == INVALID_HANDLE_VALUE) {
1255 failure_start(file, line, "Can't access %s\n", pathname);
1256 failure_finish(NULL);
1259 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1261 case 'a': fxtime = fatime; break;
1262 case 'b': fxtime = fbirthtime; break;
1263 case 'm': fxtime = fmtime; break;
1267 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1268 failure_finish(NULL);
1271 wintm.LowPart = fxtime.dwLowDateTime;
1272 wintm.HighPart = fxtime.dwHighDateTime;
1273 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1274 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1275 nsec = (nsec / 100) * 100; /* Round the request */
1279 assertion_count(file, line);
1280 r = lstat(pathname, &st);
1282 failure_start(file, line, "Can't stat %s\n", pathname);
1283 failure_finish(NULL);
1287 case 'a': filet = st.st_atime; break;
1288 case 'm': filet = st.st_mtime; break;
1289 case 'b': filet = 0; break;
1290 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1293 #if defined(__FreeBSD__)
1295 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1296 case 'b': filet = st.st_birthtime;
1297 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1298 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1299 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1302 /* FreeBSD generally only stores to microsecond res, so round. */
1303 filet_nsec = (filet_nsec / 1000) * 1000;
1304 nsec = (nsec / 1000) * 1000;
1306 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1307 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1308 #if defined(__HAIKU__)
1309 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1314 /* Check that requested time is up-to-date. */
1315 time_t now = time(NULL);
1316 if (filet < now - 10 || filet > now + 1) {
1317 failure_start(file, line,
1318 "File %s has %ctime %lld, %lld seconds ago\n",
1319 pathname, type, filet, now - filet);
1320 failure_finish(NULL);
1323 } else if (filet != t || filet_nsec != nsec) {
1324 failure_start(file, line,
1325 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1326 pathname, type, filet, filet_nsec, t, nsec);
1327 failure_finish(NULL);
1333 /* Verify atime of 'pathname'. */
1335 assertion_file_atime(const char *file, int line,
1336 const char *pathname, long t, long nsec)
1338 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1341 /* Verify atime of 'pathname' is up-to-date. */
1343 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1345 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1348 /* Verify birthtime of 'pathname'. */
1350 assertion_file_birthtime(const char *file, int line,
1351 const char *pathname, long t, long nsec)
1353 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1356 /* Verify birthtime of 'pathname' is up-to-date. */
1358 assertion_file_birthtime_recent(const char *file, int line,
1359 const char *pathname)
1361 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1364 /* Verify mtime of 'pathname'. */
1366 assertion_file_mtime(const char *file, int line,
1367 const char *pathname, long t, long nsec)
1369 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1372 /* Verify mtime of 'pathname' is up-to-date. */
1374 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1376 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1379 /* Verify number of links to 'pathname'. */
1381 assertion_file_nlinks(const char *file, int line,
1382 const char *pathname, int nlinks)
1384 #if defined(_WIN32) && !defined(__CYGWIN__)
1385 BY_HANDLE_FILE_INFORMATION bhfi;
1388 assertion_count(file, line);
1389 r = my_GetFileInformationByName(pathname, &bhfi);
1390 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1392 failure_start(file, line, "File %s has %d links, expected %d",
1393 pathname, bhfi.nNumberOfLinks, nlinks);
1394 failure_finish(NULL);
1400 assertion_count(file, line);
1401 r = lstat(pathname, &st);
1402 if (r == 0 && (int)st.st_nlink == nlinks)
1404 failure_start(file, line, "File %s has %d links, expected %d",
1405 pathname, st.st_nlink, nlinks);
1406 failure_finish(NULL);
1411 /* Verify size of 'pathname'. */
1413 assertion_file_size(const char *file, int line, const char *pathname, long size)
1418 assertion_count(file, line);
1419 #if defined(_WIN32) && !defined(__CYGWIN__)
1421 BY_HANDLE_FILE_INFORMATION bhfi;
1422 r = !my_GetFileInformationByName(pathname, &bhfi);
1423 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1428 r = lstat(pathname, &st);
1429 filesize = st.st_size;
1432 if (r == 0 && filesize == size)
1434 failure_start(file, line, "File %s has size %ld, expected %ld",
1435 pathname, (long)filesize, (long)size);
1436 failure_finish(NULL);
1440 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1442 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1447 #if defined(_WIN32) && !defined(__CYGWIN__)
1448 (void)mode; /* UNUSED */
1450 assertion_count(file, line);
1451 r = lstat(pathname, &st);
1453 failure_start(file, line, "Dir should exist: %s", pathname);
1454 failure_finish(NULL);
1457 if (!S_ISDIR(st.st_mode)) {
1458 failure_start(file, line, "%s is not a dir", pathname);
1459 failure_finish(NULL);
1462 #if !defined(_WIN32) || defined(__CYGWIN__)
1463 /* Windows doesn't handle permissions the same way as POSIX,
1464 * so just ignore the mode tests. */
1465 /* TODO: Can we do better here? */
1466 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1467 failure_start(file, line, "Dir %s has wrong mode", pathname);
1468 logprintf(" Expected: 0%3o\n", mode);
1469 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1470 failure_finish(NULL);
1477 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1478 * verify that too. */
1480 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1485 #if defined(_WIN32) && !defined(__CYGWIN__)
1486 (void)mode; /* UNUSED */
1488 assertion_count(file, line);
1489 r = lstat(pathname, &st);
1490 if (r != 0 || !S_ISREG(st.st_mode)) {
1491 failure_start(file, line, "File should exist: %s", pathname);
1492 failure_finish(NULL);
1495 #if !defined(_WIN32) || defined(__CYGWIN__)
1496 /* Windows doesn't handle permissions the same way as POSIX,
1497 * so just ignore the mode tests. */
1498 /* TODO: Can we do better here? */
1499 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1500 failure_start(file, line, "File %s has wrong mode", pathname);
1501 logprintf(" Expected: 0%3o\n", mode);
1502 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1503 failure_finish(NULL);
1510 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1511 * non-NULL, verify that the symlink has those contents. */
1513 is_symlink(const char *file, int line,
1514 const char *pathname, const char *contents)
1516 #if defined(_WIN32) && !defined(__CYGWIN__)
1517 (void)pathname; /* UNUSED */
1518 (void)contents; /* UNUSED */
1519 assertion_count(file, line);
1520 /* Windows sort-of has real symlinks, but they're only usable
1521 * by privileged users and are crippled even then, so there's
1522 * really not much point in bothering with this. */
1530 assertion_count(file, line);
1531 r = lstat(pathname, &st);
1533 failure_start(file, line,
1534 "Symlink should exist: %s", pathname);
1535 failure_finish(NULL);
1538 if (!S_ISLNK(st.st_mode))
1540 if (contents == NULL)
1542 linklen = readlink(pathname, buff, sizeof(buff));
1544 failure_start(file, line, "Can't read symlink %s", pathname);
1545 failure_finish(NULL);
1548 buff[linklen] = '\0';
1549 if (strcmp(buff, contents) != 0)
1555 /* Assert that path is a symlink that (optionally) contains contents. */
1557 assertion_is_symlink(const char *file, int line,
1558 const char *path, const char *contents)
1560 if (is_symlink(file, line, path, contents))
1563 failure_start(file, line, "File %s is not a symlink to %s",
1566 failure_start(file, line, "File %s is not a symlink", path);
1567 failure_finish(NULL);
1572 /* Create a directory and report any errors. */
1574 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1576 assertion_count(file, line);
1577 #if defined(_WIN32) && !defined(__CYGWIN__)
1578 (void)mode; /* UNUSED */
1579 if (0 == _mkdir(dirname))
1582 if (0 == mkdir(dirname, mode))
1585 failure_start(file, line, "Could not create directory %s", dirname);
1586 failure_finish(NULL);
1590 /* Create a file with the specified contents and report any failures. */
1592 assertion_make_file(const char *file, int line,
1593 const char *path, int mode, int csize, const void *contents)
1595 #if defined(_WIN32) && !defined(__CYGWIN__)
1596 /* TODO: Rework this to set file mode as well. */
1598 (void)mode; /* UNUSED */
1599 assertion_count(file, line);
1600 f = fopen(path, "wb");
1602 failure_start(file, line, "Could not create file %s", path);
1603 failure_finish(NULL);
1606 if (contents != NULL) {
1610 wsize = strlen(contents);
1612 wsize = (size_t)csize;
1613 if (wsize != fwrite(contents, 1, wsize, f)) {
1615 failure_start(file, line,
1616 "Could not write file %s", path);
1617 failure_finish(NULL);
1625 assertion_count(file, line);
1626 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1628 failure_start(file, line, "Could not create %s", path);
1629 failure_finish(NULL);
1632 if (contents != NULL) {
1636 wsize = (ssize_t)strlen(contents);
1638 wsize = (ssize_t)csize;
1639 if (wsize != write(fd, contents, wsize)) {
1641 failure_start(file, line,
1642 "Could not write to %s", path);
1643 failure_finish(NULL);
1652 /* Create a hardlink and report any failures. */
1654 assertion_make_hardlink(const char *file, int line,
1655 const char *newpath, const char *linkto)
1659 assertion_count(file, line);
1660 #if defined(_WIN32) && !defined(__CYGWIN__)
1661 succeeded = my_CreateHardLinkA(newpath, linkto);
1663 succeeded = !link(linkto, newpath);
1669 failure_start(file, line, "Could not create hardlink");
1670 logprintf(" New link: %s\n", newpath);
1671 logprintf(" Old name: %s\n", linkto);
1672 failure_finish(NULL);
1676 /* Create a symlink and report any failures. */
1678 assertion_make_symlink(const char *file, int line,
1679 const char *newpath, const char *linkto)
1681 #if defined(_WIN32) && !defined(__CYGWIN__)
1682 int targetIsDir = 0; /* TODO: Fix this */
1683 assertion_count(file, line);
1684 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1687 assertion_count(file, line);
1688 if (0 == symlink(linkto, newpath))
1691 failure_start(file, line, "Could not create symlink");
1692 logprintf(" New link: %s\n", newpath);
1693 logprintf(" Old name: %s\n", linkto);
1694 failure_finish(NULL);
1698 /* Set umask, report failures. */
1700 assertion_umask(const char *file, int line, int mask)
1702 assertion_count(file, line);
1703 (void)file; /* UNUSED */
1704 (void)line; /* UNUSED */
1709 /* Set times, report failures. */
1711 assertion_utimes(const char *file, int line,
1712 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1716 #if defined(_WIN32) && !defined(__CYGWIN__)
1717 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1718 + (((nsec)/1000)*10))
1720 ULARGE_INTEGER wintm;
1721 FILETIME fatime, fmtime;
1722 FILETIME *pat, *pmt;
1724 assertion_count(file, line);
1725 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1726 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1727 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1728 if (h == INVALID_HANDLE_VALUE) {
1729 failure_start(file, line, "Can't access %s\n", pathname);
1730 failure_finish(NULL);
1734 if (at > 0 || at_nsec > 0) {
1735 wintm.QuadPart = WINTIME(at, at_nsec);
1736 fatime.dwLowDateTime = wintm.LowPart;
1737 fatime.dwHighDateTime = wintm.HighPart;
1741 if (mt > 0 || mt_nsec > 0) {
1742 wintm.QuadPart = WINTIME(mt, mt_nsec);
1743 fmtime.dwLowDateTime = wintm.LowPart;
1744 fmtime.dwHighDateTime = wintm.HighPart;
1748 if (pat != NULL || pmt != NULL)
1749 r = SetFileTime(h, NULL, pat, pmt);
1754 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1755 failure_finish(NULL);
1759 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1761 struct timeval times[2];
1763 #if !defined(__FreeBSD__)
1764 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1766 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1769 r = lstat(pathname, &st);
1771 failure_start(file, line, "Can't stat %s\n", pathname);
1772 failure_finish(NULL);
1776 if (mt == 0 && mt_nsec == 0) {
1778 #if defined(__FreeBSD__)
1779 mt_nsec = st.st_mtimespec.tv_nsec;
1780 /* FreeBSD generally only stores to microsecond res, so round. */
1781 mt_nsec = (mt_nsec / 1000) * 1000;
1784 if (at == 0 && at_nsec == 0) {
1786 #if defined(__FreeBSD__)
1787 at_nsec = st.st_atimespec.tv_nsec;
1788 /* FreeBSD generally only stores to microsecond res, so round. */
1789 at_nsec = (at_nsec / 1000) * 1000;
1793 times[1].tv_sec = mt;
1794 times[1].tv_usec = mt_nsec / 1000;
1796 times[0].tv_sec = at;
1797 times[0].tv_usec = at_nsec / 1000;
1800 r = lutimes(pathname, times);
1802 r = utimes(pathname, times);
1805 failure_start(file, line, "Can't utimes %s\n", pathname);
1806 failure_finish(NULL);
1810 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1813 /* Set nodump, report failures. */
1815 assertion_nodump(const char *file, int line, const char *pathname)
1817 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1820 assertion_count(file, line);
1821 r = chflags(pathname, UF_NODUMP);
1823 failure_start(file, line, "Can't set nodump %s\n", pathname);
1824 failure_finish(NULL);
1827 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1828 && defined(EXT2_NODUMP_FL)
1831 assertion_count(file, line);
1832 fd = open(pathname, O_RDONLY | O_NONBLOCK);
1834 failure_start(file, line, "Can't open %s\n", pathname);
1835 failure_finish(NULL);
1838 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1840 failure_start(file, line, "Can't get flags %s\n", pathname);
1841 failure_finish(NULL);
1844 flags |= EXT2_NODUMP_FL;
1845 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1847 failure_start(file, line, "Can't set nodump %s\n", pathname);
1848 failure_finish(NULL);
1853 (void)pathname; /* UNUSED */
1854 assertion_count(file, line);
1861 * UTILITIES for use by tests.
1866 * Check whether platform supports symlinks. This is intended
1867 * for tests to use in deciding whether to bother testing symlink
1868 * support; if the platform doesn't support symlinks, there's no point
1869 * in checking whether the program being tested can create them.
1871 * Note that the first time this test is called, we actually go out to
1872 * disk to create and verify a symlink. This is necessary because
1873 * symlink support is actually a property of a particular filesystem
1874 * and can thus vary between directories on a single system. After
1875 * the first call, this returns the cached result from memory, so it's
1876 * safe to call it as often as you wish.
1881 /* Remember the test result */
1882 static int value = 0, tested = 0;
1887 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1888 /* Note: Cygwin has its own symlink() emulation that does not
1889 * use the Win32 CreateSymbolicLink() function. */
1890 #if defined(_WIN32) && !defined(__CYGWIN__)
1891 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1892 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1894 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1895 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1900 /* Platform-dependent options for hiding the output of a subcommand. */
1901 #if defined(_WIN32) && !defined(__CYGWIN__)
1902 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1904 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1907 * Can this platform run the bzip2 program?
1912 static int tested = 0, value = 0;
1915 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
1922 * Can this platform run the grzip program?
1927 static int tested = 0, value = 0;
1930 if (systemf("grzip -V %s", redirectArgs) == 0)
1937 * Can this platform run the gzip program?
1942 static int tested = 0, value = 0;
1945 if (systemf("gzip -V %s", redirectArgs) == 0)
1952 * Can this platform run the lrzip program?
1955 canRunCommand(const char *cmd)
1957 static int tested = 0, value = 0;
1960 if (systemf("%s %s", cmd, redirectArgs) == 0)
1969 static int tested = 0, value = 0;
1972 if (systemf("lrzip -V %s", redirectArgs) == 0)
1979 * Can this platform run the lz4 program?
1984 static int tested = 0, value = 0;
1987 if (systemf("lz4 -V %s", redirectArgs) == 0)
1994 * Can this platform run the lzip program?
1999 static int tested = 0, value = 0;
2002 if (systemf("lzip -V %s", redirectArgs) == 0)
2009 * Can this platform run the lzma program?
2014 static int tested = 0, value = 0;
2017 if (systemf("lzma -V %s", redirectArgs) == 0)
2024 * Can this platform run the lzop program?
2029 static int tested = 0, value = 0;
2032 if (systemf("lzop -V %s", redirectArgs) == 0)
2039 * Can this platform run the xz program?
2044 static int tested = 0, value = 0;
2047 if (systemf("xz -V %s", redirectArgs) == 0)
2054 * Can this filesystem handle nodump flags.
2056 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2061 const char *path = "cannodumptest";
2064 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2065 if (chflags(path, UF_NODUMP) < 0)
2067 if (stat(path, &sb) < 0)
2069 if (sb.st_flags & UF_NODUMP)
2074 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2075 && defined(EXT2_NODUMP_FL)
2080 const char *path = "cannodumptest";
2083 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2084 fd = open(path, O_RDONLY | O_NONBLOCK);
2087 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2090 flags |= EXT2_NODUMP_FL;
2091 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2095 fd = open(path, O_RDONLY | O_NONBLOCK);
2098 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2102 if (flags & EXT2_NODUMP_FL)
2118 * Sleep as needed; useful for verifying disk timestamp changes by
2119 * ensuring that the wall-clock time has actually changed before we
2120 * go back to re-read something from disk.
2123 sleepUntilAfter(time_t t)
2125 while (t >= time(NULL))
2126 #if defined(_WIN32) && !defined(__CYGWIN__)
2134 * Call standard system() call, but build up the command line using
2135 * sprintf() conventions.
2138 systemf(const char *fmt, ...)
2145 vsprintf(buff, fmt, ap);
2146 if (verbosity > VERBOSITY_FULL)
2147 logprintf("Cmd: %s\n", buff);
2154 * Slurp a file into memory for ease of comparison and testing.
2155 * Returns size of file in 'sizep' if non-NULL, null-terminates
2156 * data in memory for ease of use.
2159 slurpfile(size_t * sizep, const char *fmt, ...)
2161 char filename[8192];
2170 vsprintf(filename, fmt, ap);
2173 f = fopen(filename, "rb");
2175 /* Note: No error; non-existent file is okay here. */
2178 r = fstat(fileno(f), &st);
2180 logprintf("Can't stat file %s\n", filename);
2184 p = malloc((size_t)st.st_size + 1);
2186 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2187 (long int)st.st_size, filename);
2191 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2192 if (bytes_read < st.st_size) {
2193 logprintf("Can't read file %s\n", filename);
2198 p[st.st_size] = '\0';
2200 *sizep = (size_t)st.st_size;
2206 * Slurp a file into memory for ease of comparison and testing.
2207 * Returns size of file in 'sizep' if non-NULL, null-terminates
2208 * data in memory for ease of use.
2211 dumpfile(const char *filename, void *data, size_t len)
2213 ssize_t bytes_written;
2216 f = fopen(filename, "wb");
2218 logprintf("Can't open file %s for writing\n", filename);
2221 bytes_written = fwrite(data, 1, len, f);
2222 if (bytes_written < (ssize_t)len)
2223 logprintf("Can't write file %s\n", filename);
2227 /* Read a uuencoded file from the reference directory, decode, and
2228 * write the result into the current directory. */
2229 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2230 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2232 extract_reference_file(const char *name)
2237 sprintf(buff, "%s/%s.uu", refdir, name);
2238 in = fopen(buff, "r");
2239 failure("Couldn't open reference file %s", buff);
2243 /* Read up to and including the 'begin' line. */
2245 if (fgets(buff, sizeof(buff), in) == NULL) {
2246 /* TODO: This is a failure. */
2249 if (memcmp(buff, "begin ", 6) == 0)
2252 /* Now, decode the rest and write it. */
2253 out = fopen(name, "wb");
2254 while (fgets(buff, sizeof(buff), in) != NULL) {
2258 if (memcmp(buff, "end", 3) == 0)
2261 bytes = UUDECODE(*p++);
2264 /* Write out 1-3 bytes from that. */
2266 assert(VALID_UUDECODE(p[0]));
2267 assert(VALID_UUDECODE(p[1]));
2268 n = UUDECODE(*p++) << 18;
2269 n |= UUDECODE(*p++) << 12;
2270 fputc(n >> 16, out);
2274 assert(VALID_UUDECODE(p[0]));
2275 n |= UUDECODE(*p++) << 6;
2276 fputc((n >> 8) & 0xFF, out);
2280 assert(VALID_UUDECODE(p[0]));
2281 n |= UUDECODE(*p++);
2282 fputc(n & 0xFF, out);
2292 copy_reference_file(const char *name)
2298 sprintf(buff, "%s/%s", refdir, name);
2299 in = fopen(buff, "rb");
2300 failure("Couldn't open reference file %s", buff);
2304 /* Now, decode the rest and write it. */
2305 /* Not a lot of error checking here; the input better be right. */
2306 out = fopen(name, "wb");
2307 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2308 if (fwrite(buff, 1, rbytes, out) != rbytes) {
2309 logprintf("Error: fwrite\n");
2318 is_LargeInode(const char *file)
2320 #if defined(_WIN32) && !defined(__CYGWIN__)
2321 BY_HANDLE_FILE_INFORMATION bhfi;
2324 r = my_GetFileInformationByName(file, &bhfi);
2327 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2332 if (stat(file, &st) < 0)
2334 ino = (int64_t)st.st_ino;
2335 return (ino > 0xffffffff);
2340 extract_reference_files(const char **names)
2342 while (names && *names)
2343 extract_reference_file(*names++);
2353 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2355 * DEFINE_TEST(test_function)
2359 /* Use "list.h" to declare all of the test functions. */
2361 #define DEFINE_TEST(name) void name(void);
2364 /* Use "list.h" to create a list of all tests (functions and names). */
2366 #define DEFINE_TEST(n) { n, #n, 0 },
2367 struct test_list_t tests[] = {
2372 * Summarize repeated failures in the just-completed test.
2375 test_summarize(int failed, int skips_num)
2379 switch (verbosity) {
2380 case VERBOSITY_SUMMARY_ONLY:
2381 printf(failed ? "E" : ".");
2384 case VERBOSITY_PASSFAIL:
2385 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
2389 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2391 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2392 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2393 logprintf("%s:%d: Summary: Failed %d times\n",
2394 failed_filename, i, failed_lines[i].count);
2396 /* Clear the failure history for the next file. */
2397 failed_filename = NULL;
2398 memset(failed_lines, 0, sizeof(failed_lines));
2402 * Actually run a single test, with appropriate setup and cleanup.
2405 test_run(int i, const char *tmpdir)
2408 char logfilename[64];
2409 int failures_before = failures;
2410 int skips_before = skips;
2413 switch (verbosity) {
2414 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2416 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2417 printf("%3d: %-64s", i, tests[i].name);
2420 default: /* Title of test, details will follow */
2421 printf("%3d: %s\n", i, tests[i].name);
2424 /* Chdir to the top-level work directory. */
2425 if (!assertChdir(tmpdir)) {
2427 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2430 /* Create a log file for this test. */
2431 sprintf(logfilename, "%s.log", tests[i].name);
2432 logfile = fopen(logfilename, "w");
2433 fprintf(logfile, "%s\n\n", tests[i].name);
2434 /* Chdir() to a work dir for this specific test. */
2435 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2436 testworkdir = workdir;
2437 if (!assertMakeDir(testworkdir, 0755)
2438 || !assertChdir(testworkdir)) {
2440 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2443 /* Explicitly reset the locale before each test. */
2444 setlocale(LC_ALL, "C");
2445 /* Record the umask before we run the test. */
2446 umask(oldumask = umask(0));
2448 * Run the actual test.
2452 * Clean up and report afterwards.
2458 setlocale(LC_ALL, "C");
2459 /* Reset directory. */
2460 if (!assertChdir(tmpdir)) {
2461 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2465 /* Report per-test summaries. */
2466 tests[i].failures = failures - failures_before;
2467 test_summarize(tests[i].failures, skips - skips_before);
2468 /* Close the per-test log file. */
2471 /* If there were no failures, we can remove the work dir and logfile. */
2472 if (tests[i].failures == 0) {
2473 if (!keep_temp_files && assertChdir(tmpdir)) {
2474 #if defined(_WIN32) && !defined(__CYGWIN__)
2475 /* Make sure not to leave empty directories.
2476 * Sometimes a processing of closing files used by tests
2477 * is not done, then rmdir will be failed and it will
2478 * leave a empty test directory. So we should wait a few
2479 * seconds and retry rmdir. */
2481 for (t = 0; t < 10; t++) {
2484 r = systemf("rmdir /S /Q %s", tests[i].name);
2488 systemf("del %s", logfilename);
2490 systemf("rm -rf %s", tests[i].name);
2491 systemf("rm %s", logfilename);
2495 /* Return appropriate status. */
2496 return (tests[i].failures);
2502 * MAIN and support routines.
2508 usage(const char *program)
2510 static const int limit = sizeof(tests) / sizeof(tests[0]);
2513 printf("Usage: %s [options] <test> <test> ...\n", program);
2514 printf("Default is to run all tests.\n");
2515 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2516 printf("Options:\n");
2517 printf(" -d Dump core after any failure, for debugging.\n");
2518 printf(" -k Keep all temp files.\n");
2519 printf(" Default: temp files for successful tests deleted.\n");
2521 printf(" -p <path> Path to executable to be tested.\n");
2522 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2524 printf(" -q Quiet.\n");
2525 printf(" -r <dir> Path to dir containing reference files.\n");
2526 printf(" Default: Current directory.\n");
2527 printf(" -u Keep running specifies tests until one fails.\n");
2528 printf(" -v Verbose.\n");
2529 printf("Available tests:\n");
2530 for (i = 0; i < limit; i++)
2531 printf(" %d: %s\n", i, tests[i].name);
2536 get_refdir(const char *d)
2538 char tried[512] = { '\0' };
2542 /* If a dir was specified, try that */
2545 snprintf(buff, sizeof(buff), "%s", d);
2546 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2547 if (p != NULL) goto success;
2548 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2549 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2553 /* Get the current dir. */
2555 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2557 pwd = getcwd(NULL, 0);
2559 while (pwd[strlen(pwd) - 1] == '\n')
2560 pwd[strlen(pwd) - 1] = '\0';
2562 /* Look for a known file. */
2563 snprintf(buff, sizeof(buff), "%s", pwd);
2564 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2565 if (p != NULL) goto success;
2566 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2567 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2569 snprintf(buff, sizeof(buff), "%s/test", pwd);
2570 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2571 if (p != NULL) goto success;
2572 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2573 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2575 #if defined(LIBRARY)
2576 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
2578 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
2580 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2581 if (p != NULL) goto success;
2582 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2583 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2585 #if defined(PROGRAM_ALIAS)
2586 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
2587 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2588 if (p != NULL) goto success;
2589 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2590 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2593 if (memcmp(pwd, "/usr/obj", 8) == 0) {
2594 snprintf(buff, sizeof(buff), "%s", pwd + 8);
2595 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2596 if (p != NULL) goto success;
2597 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2598 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2600 snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
2601 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2602 if (p != NULL) goto success;
2603 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2604 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2608 printf("Unable to locate known reference file %s\n", KNOWNREF);
2609 printf(" Checked following directories:\n%s\n", tried);
2610 printf("Use -r option to specify full path to reference directory\n");
2611 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2619 return strdup(buff);
2623 main(int argc, char **argv)
2625 static const int limit = sizeof(tests) / sizeof(tests[0]);
2626 int test_set[sizeof(tests) / sizeof(tests[0])];
2627 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2629 char *refdir_alloc = NULL;
2630 const char *progname;
2632 const char *tmp, *option_arg, *p;
2633 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2634 char tmpdir_timestamp[256];
2636 (void)argc; /* UNUSED */
2638 /* Get the current dir. */
2640 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2642 pwd = getcwd(NULL, 0);
2644 while (pwd[strlen(pwd) - 1] == '\n')
2645 pwd[strlen(pwd) - 1] = '\0';
2647 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
2648 /* To stop to run the default invalid parameter handler. */
2649 _set_invalid_parameter_handler(invalid_parameter_handler);
2650 /* Disable annoying assertion message box. */
2651 _CrtSetReportMode(_CRT_ASSERT, 0);
2655 * Name of this program, used to build root of our temp directory
2658 progname = p = argv[0];
2659 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2661 fprintf(stderr, "ERROR: Out of memory.");
2664 strcpy(testprogdir, progname);
2665 while (*p != '\0') {
2666 /* Support \ or / dir separators for Windows compat. */
2667 if (*p == '/' || *p == '\\')
2675 testprogdir[i] = '\0';
2676 #if defined(_WIN32) && !defined(__CYGWIN__)
2677 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2678 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2679 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2680 testprogdir[1] == ':' &&
2681 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
2683 if (testprogdir[0] != '/')
2686 /* Fixup path for relative directories. */
2687 if ((testprogdir = (char *)realloc(testprogdir,
2688 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2690 fprintf(stderr, "ERROR: Out of memory.");
2693 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2694 strlen(testprogdir) + 1);
2695 memcpy(testprogdir, pwd, strlen(pwd));
2696 testprogdir[strlen(pwd)] = '/';
2700 /* Get the target program from environment, if available. */
2701 testprogfile = getenv(ENVBASE);
2704 if (getenv("TMPDIR") != NULL)
2705 tmp = getenv("TMPDIR");
2706 else if (getenv("TMP") != NULL)
2707 tmp = getenv("TMP");
2708 else if (getenv("TEMP") != NULL)
2709 tmp = getenv("TEMP");
2710 else if (getenv("TEMPDIR") != NULL)
2711 tmp = getenv("TEMPDIR");
2715 /* Allow -d to be controlled through the environment. */
2716 if (getenv(ENVBASE "_DEBUG") != NULL)
2717 dump_on_failure = 1;
2719 /* Allow -v to be controlled through the environment. */
2720 if (getenv("_VERBOSITY_LEVEL") != NULL)
2722 vlevel = getenv("_VERBOSITY_LEVEL");
2723 verbosity = atoi(vlevel);
2724 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2726 /* Unsupported verbosity levels are silently ignored */
2728 verbosity = VERBOSITY_PASSFAIL;
2732 /* Get the directory holding test files from environment. */
2733 refdir = getenv(ENVBASE "_TEST_FILES");
2736 * Parse options, without using getopt(), which isn't available
2739 ++argv; /* Skip program name */
2740 while (*argv != NULL) {
2745 while (*p != '\0') {
2748 /* If 'opt' takes an argument, parse that. */
2749 if (option == 'p' || option == 'r') {
2752 else if (*argv == NULL) {
2754 "Option -%c requires argument.\n",
2758 option_arg = *argv++;
2759 p = ""; /* End of this option word. */
2762 /* Now, handle the option. */
2765 dump_on_failure = 1;
2768 keep_temp_files = 1;
2772 testprogfile = option_arg;
2774 fprintf(stderr, "-p option not permitted\n");
2783 refdir = option_arg;
2793 fprintf(stderr, "Unrecognized option '%c'\n",
2801 * Sanity-check that our options make sense.
2804 if (testprogfile == NULL)
2806 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2807 strlen(PROGRAM) + 1)) == NULL)
2809 fprintf(stderr, "ERROR: Out of memory.");
2812 strcpy(tmp2, testprogdir);
2814 strcat(tmp2, PROGRAM);
2815 testprogfile = tmp2;
2820 #if defined(_WIN32) && !defined(__CYGWIN__)
2821 /* Command.com sometimes rejects '/' separators. */
2822 testprg = strdup(testprogfile);
2823 for (i = 0; testprg[i] != '\0'; i++) {
2824 if (testprg[i] == '/')
2827 testprogfile = testprg;
2829 /* Quote the name that gets put into shell command lines. */
2830 testprg = malloc(strlen(testprogfile) + 3);
2831 strcpy(testprg, "\"");
2832 strcat(testprg, testprogfile);
2833 strcat(testprg, "\"");
2838 #if !defined(_WIN32) && defined(SIGPIPE)
2839 { /* Ignore SIGPIPE signals */
2840 struct sigaction sa;
2841 sa.sa_handler = SIG_IGN;
2842 sigemptyset(&sa.sa_mask);
2844 sigaction(SIGPIPE, &sa, NULL);
2849 * Create a temp directory for the following tests.
2850 * Include the time the tests started as part of the name,
2851 * to make it easier to track the results of multiple tests.
2854 for (i = 0; ; i++) {
2855 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2856 "%Y-%m-%dT%H.%M.%S",
2858 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2859 tmpdir_timestamp, i);
2860 if (assertMakeDir(tmpdir,0755))
2864 "ERROR: Unable to create temp directory %s\n",
2871 * If the user didn't specify a directory for locating
2872 * reference files, try to find the reference files in
2873 * the "usual places."
2875 refdir = refdir_alloc = get_refdir(refdir);
2878 * Banner with basic information.
2881 printf("If tests fail or crash, details will be in:\n");
2882 printf(" %s\n", tmpdir);
2884 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2885 printf("Reference files will be read from: %s\n", refdir);
2887 printf("Running tests on: %s\n", testprog);
2889 printf("Exercising: ");
2891 printf("%s\n", EXTRA_VERSION);
2898 * Run some or all of the individual tests.
2906 test_num = get_test_set(test_set, limit, *argv, tests);
2908 printf("*** INVALID Test %s\n", *argv);
2914 for (i = 0; i < test_num; i++) {
2916 if (test_run(test_set[i], tmpdir)) {
2924 } while (*argv != NULL);
2925 } while (until_failure);
2928 /* Must be freed after all tests run */
2934 * Report summary statistics.
2936 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2938 printf("Totals:\n");
2939 printf(" Tests run: %8d\n", tests_run);
2940 printf(" Tests failed: %8d\n", tests_failed);
2941 printf(" Assertions checked:%8d\n", assertions);
2942 printf(" Assertions failed: %8d\n", failures);
2943 printf(" Skips reported: %8d\n", skips);
2947 printf("Failing tests:\n");
2948 for (i = 0; i < limit; ++i) {
2949 if (tests[i].failures)
2950 printf(" %d: %s (%d failures)\n", i,
2951 tests[i].name, tests[i].failures);
2954 printf("Details for failing tests: %s\n", tmpdir);
2957 if (verbosity == VERBOSITY_SUMMARY_ONLY)
2959 printf("%d tests passed, no failures\n", tests_run);
2964 /* If the final tmpdir is empty, we can remove it. */
2965 /* This should be the usual case when all tests succeed. */
2969 return (tests_failed ? 1 : 0);