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$");
64 #define KNOWNREF "test_option_f.cpio.uu"
65 #define ENVBASE "BSDCPIO" /* Prefix for environment variables. */
66 #define PROGRAM "bsdcpio" /* Name of program being tested. */
67 #define PROGRAM_ALIAS "cpio" /* 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 mode of 'pathname'. */
1366 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1371 assertion_count(file, line);
1372 #if defined(_WIN32) && !defined(__CYGWIN__)
1373 failure_start(file, line, "assertFileMode not yet implemented for Windows");
1377 r = lstat(pathname, &st);
1378 mode = (int)(st.st_mode & 0777);
1380 if (r == 0 && mode == expected_mode)
1382 failure_start(file, line, "File %s has mode %o, expected %o",
1383 pathname, mode, expected_mode);
1385 failure_finish(NULL);
1389 /* Verify mtime of 'pathname'. */
1391 assertion_file_mtime(const char *file, int line,
1392 const char *pathname, long t, long nsec)
1394 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1397 /* Verify mtime of 'pathname' is up-to-date. */
1399 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1401 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1404 /* Verify number of links to 'pathname'. */
1406 assertion_file_nlinks(const char *file, int line,
1407 const char *pathname, int nlinks)
1409 #if defined(_WIN32) && !defined(__CYGWIN__)
1410 BY_HANDLE_FILE_INFORMATION bhfi;
1413 assertion_count(file, line);
1414 r = my_GetFileInformationByName(pathname, &bhfi);
1415 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1417 failure_start(file, line, "File %s has %d links, expected %d",
1418 pathname, bhfi.nNumberOfLinks, nlinks);
1419 failure_finish(NULL);
1425 assertion_count(file, line);
1426 r = lstat(pathname, &st);
1427 if (r == 0 && (int)st.st_nlink == nlinks)
1429 failure_start(file, line, "File %s has %d links, expected %d",
1430 pathname, st.st_nlink, nlinks);
1431 failure_finish(NULL);
1436 /* Verify size of 'pathname'. */
1438 assertion_file_size(const char *file, int line, const char *pathname, long size)
1443 assertion_count(file, line);
1444 #if defined(_WIN32) && !defined(__CYGWIN__)
1446 BY_HANDLE_FILE_INFORMATION bhfi;
1447 r = !my_GetFileInformationByName(pathname, &bhfi);
1448 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1453 r = lstat(pathname, &st);
1454 filesize = st.st_size;
1457 if (r == 0 && filesize == size)
1459 failure_start(file, line, "File %s has size %ld, expected %ld",
1460 pathname, (long)filesize, (long)size);
1461 failure_finish(NULL);
1465 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1467 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1472 #if defined(_WIN32) && !defined(__CYGWIN__)
1473 (void)mode; /* UNUSED */
1475 assertion_count(file, line);
1476 r = lstat(pathname, &st);
1478 failure_start(file, line, "Dir should exist: %s", pathname);
1479 failure_finish(NULL);
1482 if (!S_ISDIR(st.st_mode)) {
1483 failure_start(file, line, "%s is not a dir", pathname);
1484 failure_finish(NULL);
1487 #if !defined(_WIN32) || defined(__CYGWIN__)
1488 /* Windows doesn't handle permissions the same way as POSIX,
1489 * so just ignore the mode tests. */
1490 /* TODO: Can we do better here? */
1491 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1492 failure_start(file, line, "Dir %s has wrong mode", pathname);
1493 logprintf(" Expected: 0%3o\n", mode);
1494 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1495 failure_finish(NULL);
1502 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1503 * verify that too. */
1505 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1510 #if defined(_WIN32) && !defined(__CYGWIN__)
1511 (void)mode; /* UNUSED */
1513 assertion_count(file, line);
1514 r = lstat(pathname, &st);
1515 if (r != 0 || !S_ISREG(st.st_mode)) {
1516 failure_start(file, line, "File should exist: %s", pathname);
1517 failure_finish(NULL);
1520 #if !defined(_WIN32) || defined(__CYGWIN__)
1521 /* Windows doesn't handle permissions the same way as POSIX,
1522 * so just ignore the mode tests. */
1523 /* TODO: Can we do better here? */
1524 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1525 failure_start(file, line, "File %s has wrong mode", pathname);
1526 logprintf(" Expected: 0%3o\n", mode);
1527 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1528 failure_finish(NULL);
1535 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1536 * non-NULL, verify that the symlink has those contents. */
1538 is_symlink(const char *file, int line,
1539 const char *pathname, const char *contents)
1541 #if defined(_WIN32) && !defined(__CYGWIN__)
1542 (void)pathname; /* UNUSED */
1543 (void)contents; /* UNUSED */
1544 assertion_count(file, line);
1545 /* Windows sort-of has real symlinks, but they're only usable
1546 * by privileged users and are crippled even then, so there's
1547 * really not much point in bothering with this. */
1555 assertion_count(file, line);
1556 r = lstat(pathname, &st);
1558 failure_start(file, line,
1559 "Symlink should exist: %s", pathname);
1560 failure_finish(NULL);
1563 if (!S_ISLNK(st.st_mode))
1565 if (contents == NULL)
1567 linklen = readlink(pathname, buff, sizeof(buff));
1569 failure_start(file, line, "Can't read symlink %s", pathname);
1570 failure_finish(NULL);
1573 buff[linklen] = '\0';
1574 if (strcmp(buff, contents) != 0)
1580 /* Assert that path is a symlink that (optionally) contains contents. */
1582 assertion_is_symlink(const char *file, int line,
1583 const char *path, const char *contents)
1585 if (is_symlink(file, line, path, contents))
1588 failure_start(file, line, "File %s is not a symlink to %s",
1591 failure_start(file, line, "File %s is not a symlink", path);
1592 failure_finish(NULL);
1597 /* Create a directory and report any errors. */
1599 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1601 assertion_count(file, line);
1602 #if defined(_WIN32) && !defined(__CYGWIN__)
1603 (void)mode; /* UNUSED */
1604 if (0 == _mkdir(dirname))
1607 if (0 == mkdir(dirname, mode)) {
1608 if (0 == chmod(dirname, mode)) {
1609 assertion_file_mode(file, line, dirname, mode);
1614 failure_start(file, line, "Could not create directory %s", dirname);
1615 failure_finish(NULL);
1619 /* Create a file with the specified contents and report any failures. */
1621 assertion_make_file(const char *file, int line,
1622 const char *path, int mode, int csize, const void *contents)
1624 #if defined(_WIN32) && !defined(__CYGWIN__)
1625 /* TODO: Rework this to set file mode as well. */
1627 (void)mode; /* UNUSED */
1628 assertion_count(file, line);
1629 f = fopen(path, "wb");
1631 failure_start(file, line, "Could not create file %s", path);
1632 failure_finish(NULL);
1635 if (contents != NULL) {
1639 wsize = strlen(contents);
1641 wsize = (size_t)csize;
1642 if (wsize != fwrite(contents, 1, wsize, f)) {
1644 failure_start(file, line,
1645 "Could not write file %s", path);
1646 failure_finish(NULL);
1654 assertion_count(file, line);
1655 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1657 failure_start(file, line, "Could not create %s", path);
1658 failure_finish(NULL);
1661 if (0 != chmod(path, mode)) {
1662 failure_start(file, line, "Could not chmod %s", path);
1663 failure_finish(NULL);
1666 if (contents != NULL) {
1670 wsize = (ssize_t)strlen(contents);
1672 wsize = (ssize_t)csize;
1673 if (wsize != write(fd, contents, wsize)) {
1675 failure_start(file, line,
1676 "Could not write to %s", path);
1677 failure_finish(NULL);
1682 assertion_file_mode(file, line, path, mode);
1687 /* Create a hardlink and report any failures. */
1689 assertion_make_hardlink(const char *file, int line,
1690 const char *newpath, const char *linkto)
1694 assertion_count(file, line);
1695 #if defined(_WIN32) && !defined(__CYGWIN__)
1696 succeeded = my_CreateHardLinkA(newpath, linkto);
1698 succeeded = !link(linkto, newpath);
1704 failure_start(file, line, "Could not create hardlink");
1705 logprintf(" New link: %s\n", newpath);
1706 logprintf(" Old name: %s\n", linkto);
1707 failure_finish(NULL);
1711 /* Create a symlink and report any failures. */
1713 assertion_make_symlink(const char *file, int line,
1714 const char *newpath, const char *linkto)
1716 #if defined(_WIN32) && !defined(__CYGWIN__)
1717 int targetIsDir = 0; /* TODO: Fix this */
1718 assertion_count(file, line);
1719 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1722 assertion_count(file, line);
1723 if (0 == symlink(linkto, newpath))
1726 failure_start(file, line, "Could not create symlink");
1727 logprintf(" New link: %s\n", newpath);
1728 logprintf(" Old name: %s\n", linkto);
1729 failure_finish(NULL);
1733 /* Set umask, report failures. */
1735 assertion_umask(const char *file, int line, int mask)
1737 assertion_count(file, line);
1738 (void)file; /* UNUSED */
1739 (void)line; /* UNUSED */
1744 /* Set times, report failures. */
1746 assertion_utimes(const char *file, int line,
1747 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1751 #if defined(_WIN32) && !defined(__CYGWIN__)
1752 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1753 + (((nsec)/1000)*10))
1755 ULARGE_INTEGER wintm;
1756 FILETIME fatime, fmtime;
1757 FILETIME *pat, *pmt;
1759 assertion_count(file, line);
1760 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1761 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1762 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1763 if (h == INVALID_HANDLE_VALUE) {
1764 failure_start(file, line, "Can't access %s\n", pathname);
1765 failure_finish(NULL);
1769 if (at > 0 || at_nsec > 0) {
1770 wintm.QuadPart = WINTIME(at, at_nsec);
1771 fatime.dwLowDateTime = wintm.LowPart;
1772 fatime.dwHighDateTime = wintm.HighPart;
1776 if (mt > 0 || mt_nsec > 0) {
1777 wintm.QuadPart = WINTIME(mt, mt_nsec);
1778 fmtime.dwLowDateTime = wintm.LowPart;
1779 fmtime.dwHighDateTime = wintm.HighPart;
1783 if (pat != NULL || pmt != NULL)
1784 r = SetFileTime(h, NULL, pat, pmt);
1789 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1790 failure_finish(NULL);
1794 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1796 struct timeval times[2];
1798 #if !defined(__FreeBSD__)
1799 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1801 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1804 r = lstat(pathname, &st);
1806 failure_start(file, line, "Can't stat %s\n", pathname);
1807 failure_finish(NULL);
1811 if (mt == 0 && mt_nsec == 0) {
1813 #if defined(__FreeBSD__)
1814 mt_nsec = st.st_mtimespec.tv_nsec;
1815 /* FreeBSD generally only stores to microsecond res, so round. */
1816 mt_nsec = (mt_nsec / 1000) * 1000;
1819 if (at == 0 && at_nsec == 0) {
1821 #if defined(__FreeBSD__)
1822 at_nsec = st.st_atimespec.tv_nsec;
1823 /* FreeBSD generally only stores to microsecond res, so round. */
1824 at_nsec = (at_nsec / 1000) * 1000;
1828 times[1].tv_sec = mt;
1829 times[1].tv_usec = mt_nsec / 1000;
1831 times[0].tv_sec = at;
1832 times[0].tv_usec = at_nsec / 1000;
1835 r = lutimes(pathname, times);
1837 r = utimes(pathname, times);
1840 failure_start(file, line, "Can't utimes %s\n", pathname);
1841 failure_finish(NULL);
1845 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1848 /* Set nodump, report failures. */
1850 assertion_nodump(const char *file, int line, const char *pathname)
1852 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1855 assertion_count(file, line);
1856 r = chflags(pathname, UF_NODUMP);
1858 failure_start(file, line, "Can't set nodump %s\n", pathname);
1859 failure_finish(NULL);
1862 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1863 && defined(EXT2_NODUMP_FL)
1866 assertion_count(file, line);
1867 fd = open(pathname, O_RDONLY | O_NONBLOCK);
1869 failure_start(file, line, "Can't open %s\n", pathname);
1870 failure_finish(NULL);
1873 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1875 failure_start(file, line, "Can't get flags %s\n", pathname);
1876 failure_finish(NULL);
1879 flags |= EXT2_NODUMP_FL;
1880 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1882 failure_start(file, line, "Can't set nodump %s\n", pathname);
1883 failure_finish(NULL);
1888 (void)pathname; /* UNUSED */
1889 assertion_count(file, line);
1896 * UTILITIES for use by tests.
1901 * Check whether platform supports symlinks. This is intended
1902 * for tests to use in deciding whether to bother testing symlink
1903 * support; if the platform doesn't support symlinks, there's no point
1904 * in checking whether the program being tested can create them.
1906 * Note that the first time this test is called, we actually go out to
1907 * disk to create and verify a symlink. This is necessary because
1908 * symlink support is actually a property of a particular filesystem
1909 * and can thus vary between directories on a single system. After
1910 * the first call, this returns the cached result from memory, so it's
1911 * safe to call it as often as you wish.
1916 /* Remember the test result */
1917 static int value = 0, tested = 0;
1922 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1923 /* Note: Cygwin has its own symlink() emulation that does not
1924 * use the Win32 CreateSymbolicLink() function. */
1925 #if defined(_WIN32) && !defined(__CYGWIN__)
1926 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1927 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1929 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1930 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1935 /* Platform-dependent options for hiding the output of a subcommand. */
1936 #if defined(_WIN32) && !defined(__CYGWIN__)
1937 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1939 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1942 * Can this platform run the bzip2 program?
1947 static int tested = 0, value = 0;
1950 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
1957 * Can this platform run the grzip program?
1962 static int tested = 0, value = 0;
1965 if (systemf("grzip -V %s", redirectArgs) == 0)
1972 * Can this platform run the gzip program?
1977 static int tested = 0, value = 0;
1980 if (systemf("gzip -V %s", redirectArgs) == 0)
1987 * Can this platform run the lrzip program?
1990 canRunCommand(const char *cmd)
1992 static int tested = 0, value = 0;
1995 if (systemf("%s %s", cmd, redirectArgs) == 0)
2004 static int tested = 0, value = 0;
2007 if (systemf("lrzip -V %s", redirectArgs) == 0)
2014 * Can this platform run the lz4 program?
2019 static int tested = 0, value = 0;
2022 if (systemf("lz4 -V %s", redirectArgs) == 0)
2029 * Can this platform run the lzip program?
2034 static int tested = 0, value = 0;
2037 if (systemf("lzip -V %s", redirectArgs) == 0)
2044 * Can this platform run the lzma program?
2049 static int tested = 0, value = 0;
2052 if (systemf("lzma -V %s", redirectArgs) == 0)
2059 * Can this platform run the lzop program?
2064 static int tested = 0, value = 0;
2067 if (systemf("lzop -V %s", redirectArgs) == 0)
2074 * Can this platform run the xz program?
2079 static int tested = 0, value = 0;
2082 if (systemf("xz -V %s", redirectArgs) == 0)
2089 * Can this filesystem handle nodump flags.
2091 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2096 const char *path = "cannodumptest";
2099 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2100 if (chflags(path, UF_NODUMP) < 0)
2102 if (stat(path, &sb) < 0)
2104 if (sb.st_flags & UF_NODUMP)
2109 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2110 && defined(EXT2_NODUMP_FL)
2115 const char *path = "cannodumptest";
2118 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2119 fd = open(path, O_RDONLY | O_NONBLOCK);
2122 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2125 flags |= EXT2_NODUMP_FL;
2126 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2130 fd = open(path, O_RDONLY | O_NONBLOCK);
2133 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2137 if (flags & EXT2_NODUMP_FL)
2153 * Sleep as needed; useful for verifying disk timestamp changes by
2154 * ensuring that the wall-clock time has actually changed before we
2155 * go back to re-read something from disk.
2158 sleepUntilAfter(time_t t)
2160 while (t >= time(NULL))
2161 #if defined(_WIN32) && !defined(__CYGWIN__)
2169 * Call standard system() call, but build up the command line using
2170 * sprintf() conventions.
2173 systemf(const char *fmt, ...)
2180 vsprintf(buff, fmt, ap);
2181 if (verbosity > VERBOSITY_FULL)
2182 logprintf("Cmd: %s\n", buff);
2189 * Slurp a file into memory for ease of comparison and testing.
2190 * Returns size of file in 'sizep' if non-NULL, null-terminates
2191 * data in memory for ease of use.
2194 slurpfile(size_t * sizep, const char *fmt, ...)
2196 char filename[8192];
2205 vsprintf(filename, fmt, ap);
2208 f = fopen(filename, "rb");
2210 /* Note: No error; non-existent file is okay here. */
2213 r = fstat(fileno(f), &st);
2215 logprintf("Can't stat file %s\n", filename);
2219 p = malloc((size_t)st.st_size + 1);
2221 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2222 (long int)st.st_size, filename);
2226 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2227 if (bytes_read < st.st_size) {
2228 logprintf("Can't read file %s\n", filename);
2233 p[st.st_size] = '\0';
2235 *sizep = (size_t)st.st_size;
2241 * Slurp a file into memory for ease of comparison and testing.
2242 * Returns size of file in 'sizep' if non-NULL, null-terminates
2243 * data in memory for ease of use.
2246 dumpfile(const char *filename, void *data, size_t len)
2248 ssize_t bytes_written;
2251 f = fopen(filename, "wb");
2253 logprintf("Can't open file %s for writing\n", filename);
2256 bytes_written = fwrite(data, 1, len, f);
2257 if (bytes_written < (ssize_t)len)
2258 logprintf("Can't write file %s\n", filename);
2262 /* Read a uuencoded file from the reference directory, decode, and
2263 * write the result into the current directory. */
2264 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2265 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2267 extract_reference_file(const char *name)
2272 sprintf(buff, "%s/%s.uu", refdir, name);
2273 in = fopen(buff, "r");
2274 failure("Couldn't open reference file %s", buff);
2278 /* Read up to and including the 'begin' line. */
2280 if (fgets(buff, sizeof(buff), in) == NULL) {
2281 /* TODO: This is a failure. */
2284 if (memcmp(buff, "begin ", 6) == 0)
2287 /* Now, decode the rest and write it. */
2288 out = fopen(name, "wb");
2289 while (fgets(buff, sizeof(buff), in) != NULL) {
2293 if (memcmp(buff, "end", 3) == 0)
2296 bytes = UUDECODE(*p++);
2299 /* Write out 1-3 bytes from that. */
2301 assert(VALID_UUDECODE(p[0]));
2302 assert(VALID_UUDECODE(p[1]));
2303 n = UUDECODE(*p++) << 18;
2304 n |= UUDECODE(*p++) << 12;
2305 fputc(n >> 16, out);
2309 assert(VALID_UUDECODE(p[0]));
2310 n |= UUDECODE(*p++) << 6;
2311 fputc((n >> 8) & 0xFF, out);
2315 assert(VALID_UUDECODE(p[0]));
2316 n |= UUDECODE(*p++);
2317 fputc(n & 0xFF, out);
2327 copy_reference_file(const char *name)
2333 sprintf(buff, "%s/%s", refdir, name);
2334 in = fopen(buff, "rb");
2335 failure("Couldn't open reference file %s", buff);
2339 /* Now, decode the rest and write it. */
2340 /* Not a lot of error checking here; the input better be right. */
2341 out = fopen(name, "wb");
2342 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2343 if (fwrite(buff, 1, rbytes, out) != rbytes) {
2344 logprintf("Error: fwrite\n");
2353 is_LargeInode(const char *file)
2355 #if defined(_WIN32) && !defined(__CYGWIN__)
2356 BY_HANDLE_FILE_INFORMATION bhfi;
2359 r = my_GetFileInformationByName(file, &bhfi);
2362 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2367 if (stat(file, &st) < 0)
2369 ino = (int64_t)st.st_ino;
2370 return (ino > 0xffffffff);
2375 extract_reference_files(const char **names)
2377 while (names && *names)
2378 extract_reference_file(*names++);
2388 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2390 * DEFINE_TEST(test_function)
2394 /* Use "list.h" to declare all of the test functions. */
2396 #define DEFINE_TEST(name) void name(void);
2399 /* Use "list.h" to create a list of all tests (functions and names). */
2401 #define DEFINE_TEST(n) { n, #n, 0 },
2402 struct test_list_t tests[] = {
2407 * Summarize repeated failures in the just-completed test.
2410 test_summarize(int failed, int skips_num)
2414 switch (verbosity) {
2415 case VERBOSITY_SUMMARY_ONLY:
2416 printf(failed ? "E" : ".");
2419 case VERBOSITY_PASSFAIL:
2420 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
2424 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2426 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2427 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2428 logprintf("%s:%d: Summary: Failed %d times\n",
2429 failed_filename, i, failed_lines[i].count);
2431 /* Clear the failure history for the next file. */
2432 failed_filename = NULL;
2433 memset(failed_lines, 0, sizeof(failed_lines));
2437 * Actually run a single test, with appropriate setup and cleanup.
2440 test_run(int i, const char *tmpdir)
2443 char logfilename[64];
2444 int failures_before = failures;
2445 int skips_before = skips;
2448 switch (verbosity) {
2449 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2451 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2452 printf("%3d: %-64s", i, tests[i].name);
2455 default: /* Title of test, details will follow */
2456 printf("%3d: %s\n", i, tests[i].name);
2459 /* Chdir to the top-level work directory. */
2460 if (!assertChdir(tmpdir)) {
2462 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2465 /* Create a log file for this test. */
2466 sprintf(logfilename, "%s.log", tests[i].name);
2467 logfile = fopen(logfilename, "w");
2468 fprintf(logfile, "%s\n\n", tests[i].name);
2469 /* Chdir() to a work dir for this specific test. */
2470 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2471 testworkdir = workdir;
2472 if (!assertMakeDir(testworkdir, 0755)
2473 || !assertChdir(testworkdir)) {
2475 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2478 /* Explicitly reset the locale before each test. */
2479 setlocale(LC_ALL, "C");
2480 /* Record the umask before we run the test. */
2481 umask(oldumask = umask(0));
2483 * Run the actual test.
2487 * Clean up and report afterwards.
2493 setlocale(LC_ALL, "C");
2494 /* Reset directory. */
2495 if (!assertChdir(tmpdir)) {
2496 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2500 /* Report per-test summaries. */
2501 tests[i].failures = failures - failures_before;
2502 test_summarize(tests[i].failures, skips - skips_before);
2503 /* Close the per-test log file. */
2506 /* If there were no failures, we can remove the work dir and logfile. */
2507 if (tests[i].failures == 0) {
2508 if (!keep_temp_files && assertChdir(tmpdir)) {
2509 #if defined(_WIN32) && !defined(__CYGWIN__)
2510 /* Make sure not to leave empty directories.
2511 * Sometimes a processing of closing files used by tests
2512 * is not done, then rmdir will be failed and it will
2513 * leave a empty test directory. So we should wait a few
2514 * seconds and retry rmdir. */
2516 for (t = 0; t < 10; t++) {
2519 r = systemf("rmdir /S /Q %s", tests[i].name);
2523 systemf("del %s", logfilename);
2525 systemf("rm -rf %s", tests[i].name);
2526 systemf("rm %s", logfilename);
2530 /* Return appropriate status. */
2531 return (tests[i].failures);
2537 * MAIN and support routines.
2543 usage(const char *program)
2545 static const int limit = sizeof(tests) / sizeof(tests[0]);
2548 printf("Usage: %s [options] <test> <test> ...\n", program);
2549 printf("Default is to run all tests.\n");
2550 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2551 printf("Options:\n");
2552 printf(" -d Dump core after any failure, for debugging.\n");
2553 printf(" -k Keep all temp files.\n");
2554 printf(" Default: temp files for successful tests deleted.\n");
2556 printf(" -p <path> Path to executable to be tested.\n");
2557 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2559 printf(" -q Quiet.\n");
2560 printf(" -r <dir> Path to dir containing reference files.\n");
2561 printf(" Default: Current directory.\n");
2562 printf(" -u Keep running specifies tests until one fails.\n");
2563 printf(" -v Verbose.\n");
2564 printf("Available tests:\n");
2565 for (i = 0; i < limit; i++)
2566 printf(" %d: %s\n", i, tests[i].name);
2571 get_refdir(const char *d)
2573 size_t tried_size, buff_size;
2574 char *buff, *tried, *pwd = NULL, *p = NULL;
2577 buff_size = PATH_MAX;
2581 buff = calloc(buff_size, 1);
2583 fprintf(stderr, "Unable to allocate memory\n");
2587 /* Allocate a buffer to hold the various directories we checked. */
2588 tried_size = buff_size * 2;
2589 tried = calloc(tried_size, 1);
2590 if (tried == NULL) {
2591 fprintf(stderr, "Unable to allocate memory\n");
2595 /* If a dir was specified, try that */
2598 snprintf(buff, buff_size, "%s", d);
2599 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2600 if (p != NULL) goto success;
2601 strncat(tried, buff, tried_size - strlen(tried) - 1);
2602 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2606 /* Get the current dir. */
2608 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2610 pwd = getcwd(NULL, 0);
2612 while (pwd[strlen(pwd) - 1] == '\n')
2613 pwd[strlen(pwd) - 1] = '\0';
2615 /* Look for a known file. */
2616 snprintf(buff, buff_size, "%s", pwd);
2617 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2618 if (p != NULL) goto success;
2619 strncat(tried, buff, tried_size - strlen(tried) - 1);
2620 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2622 snprintf(buff, buff_size, "%s/test", pwd);
2623 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2624 if (p != NULL) goto success;
2625 strncat(tried, buff, tried_size - strlen(tried) - 1);
2626 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2628 #if defined(LIBRARY)
2629 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
2631 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
2633 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2634 if (p != NULL) goto success;
2635 strncat(tried, buff, tried_size - strlen(tried) - 1);
2636 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2638 #if defined(PROGRAM_ALIAS)
2639 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
2640 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2641 if (p != NULL) goto success;
2642 strncat(tried, buff, tried_size - strlen(tried) - 1);
2643 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2646 if (memcmp(pwd, "/usr/obj", 8) == 0) {
2647 snprintf(buff, buff_size, "%s", pwd + 8);
2648 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2649 if (p != NULL) goto success;
2650 strncat(tried, buff, tried_size - strlen(tried) - 1);
2651 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2653 snprintf(buff, buff_size, "%s/test", pwd + 8);
2654 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2655 if (p != NULL) goto success;
2656 strncat(tried, buff, tried_size - strlen(tried) - 1);
2657 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2661 printf("Unable to locate known reference file %s\n", KNOWNREF);
2662 printf(" Checked following directories:\n%s\n", tried);
2663 printf("Use -r option to specify full path to reference directory\n");
2664 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2674 /* Copy result into a fresh buffer to reduce memory usage. */
2681 main(int argc, char **argv)
2683 static const int limit = sizeof(tests) / sizeof(tests[0]);
2684 int test_set[sizeof(tests) / sizeof(tests[0])];
2685 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2687 char *refdir_alloc = NULL;
2688 const char *progname;
2690 const char *tmp, *option_arg, *p;
2691 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2692 char tmpdir_timestamp[256];
2694 (void)argc; /* UNUSED */
2696 /* Get the current dir. */
2698 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2700 pwd = getcwd(NULL, 0);
2702 while (pwd[strlen(pwd) - 1] == '\n')
2703 pwd[strlen(pwd) - 1] = '\0';
2705 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
2706 /* To stop to run the default invalid parameter handler. */
2707 _set_invalid_parameter_handler(invalid_parameter_handler);
2708 /* Disable annoying assertion message box. */
2709 _CrtSetReportMode(_CRT_ASSERT, 0);
2713 * Name of this program, used to build root of our temp directory
2716 progname = p = argv[0];
2717 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2719 fprintf(stderr, "ERROR: Out of memory.");
2722 strcpy(testprogdir, progname);
2723 while (*p != '\0') {
2724 /* Support \ or / dir separators for Windows compat. */
2725 if (*p == '/' || *p == '\\')
2733 testprogdir[i] = '\0';
2734 #if defined(_WIN32) && !defined(__CYGWIN__)
2735 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2736 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2737 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2738 testprogdir[1] == ':' &&
2739 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
2741 if (testprogdir[0] != '/')
2744 /* Fixup path for relative directories. */
2745 if ((testprogdir = (char *)realloc(testprogdir,
2746 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2748 fprintf(stderr, "ERROR: Out of memory.");
2751 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2752 strlen(testprogdir) + 1);
2753 memcpy(testprogdir, pwd, strlen(pwd));
2754 testprogdir[strlen(pwd)] = '/';
2758 /* Get the target program from environment, if available. */
2759 testprogfile = getenv(ENVBASE);
2762 if (getenv("TMPDIR") != NULL)
2763 tmp = getenv("TMPDIR");
2764 else if (getenv("TMP") != NULL)
2765 tmp = getenv("TMP");
2766 else if (getenv("TEMP") != NULL)
2767 tmp = getenv("TEMP");
2768 else if (getenv("TEMPDIR") != NULL)
2769 tmp = getenv("TEMPDIR");
2773 /* Allow -d to be controlled through the environment. */
2774 if (getenv(ENVBASE "_DEBUG") != NULL)
2775 dump_on_failure = 1;
2777 /* Allow -v to be controlled through the environment. */
2778 if (getenv("_VERBOSITY_LEVEL") != NULL)
2780 vlevel = getenv("_VERBOSITY_LEVEL");
2781 verbosity = atoi(vlevel);
2782 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2784 /* Unsupported verbosity levels are silently ignored */
2786 verbosity = VERBOSITY_PASSFAIL;
2790 /* Get the directory holding test files from environment. */
2791 refdir = getenv(ENVBASE "_TEST_FILES");
2794 * Parse options, without using getopt(), which isn't available
2797 ++argv; /* Skip program name */
2798 while (*argv != NULL) {
2803 while (*p != '\0') {
2806 /* If 'opt' takes an argument, parse that. */
2807 if (option == 'p' || option == 'r') {
2810 else if (*argv == NULL) {
2812 "Option -%c requires argument.\n",
2816 option_arg = *argv++;
2817 p = ""; /* End of this option word. */
2820 /* Now, handle the option. */
2823 dump_on_failure = 1;
2826 keep_temp_files = 1;
2830 testprogfile = option_arg;
2832 fprintf(stderr, "-p option not permitted\n");
2841 refdir = option_arg;
2851 fprintf(stderr, "Unrecognized option '%c'\n",
2859 * Sanity-check that our options make sense.
2862 if (testprogfile == NULL)
2864 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2865 strlen(PROGRAM) + 1)) == NULL)
2867 fprintf(stderr, "ERROR: Out of memory.");
2870 strcpy(tmp2, testprogdir);
2872 strcat(tmp2, PROGRAM);
2873 testprogfile = tmp2;
2878 #if defined(_WIN32) && !defined(__CYGWIN__)
2879 /* Command.com sometimes rejects '/' separators. */
2880 testprg = strdup(testprogfile);
2881 for (i = 0; testprg[i] != '\0'; i++) {
2882 if (testprg[i] == '/')
2885 testprogfile = testprg;
2887 /* Quote the name that gets put into shell command lines. */
2888 testprg = malloc(strlen(testprogfile) + 3);
2889 strcpy(testprg, "\"");
2890 strcat(testprg, testprogfile);
2891 strcat(testprg, "\"");
2896 #if !defined(_WIN32) && defined(SIGPIPE)
2897 { /* Ignore SIGPIPE signals */
2898 struct sigaction sa;
2899 sa.sa_handler = SIG_IGN;
2900 sigemptyset(&sa.sa_mask);
2902 sigaction(SIGPIPE, &sa, NULL);
2907 * Create a temp directory for the following tests.
2908 * Include the time the tests started as part of the name,
2909 * to make it easier to track the results of multiple tests.
2912 for (i = 0; ; i++) {
2913 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2914 "%Y-%m-%dT%H.%M.%S",
2916 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2917 tmpdir_timestamp, i);
2918 if (assertMakeDir(tmpdir,0755))
2922 "ERROR: Unable to create temp directory %s\n",
2929 * If the user didn't specify a directory for locating
2930 * reference files, try to find the reference files in
2931 * the "usual places."
2933 refdir = refdir_alloc = get_refdir(refdir);
2936 * Banner with basic information.
2939 printf("If tests fail or crash, details will be in:\n");
2940 printf(" %s\n", tmpdir);
2942 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2943 printf("Reference files will be read from: %s\n", refdir);
2945 printf("Running tests on: %s\n", testprog);
2947 printf("Exercising: ");
2949 printf("%s\n", EXTRA_VERSION);
2956 * Run some or all of the individual tests.
2964 test_num = get_test_set(test_set, limit, *argv, tests);
2966 printf("*** INVALID Test %s\n", *argv);
2972 for (i = 0; i < test_num; i++) {
2974 if (test_run(test_set[i], tmpdir)) {
2982 } while (*argv != NULL);
2983 } while (until_failure);
2986 /* Must be freed after all tests run */
2992 * Report summary statistics.
2994 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2996 printf("Totals:\n");
2997 printf(" Tests run: %8d\n", tests_run);
2998 printf(" Tests failed: %8d\n", tests_failed);
2999 printf(" Assertions checked:%8d\n", assertions);
3000 printf(" Assertions failed: %8d\n", failures);
3001 printf(" Skips reported: %8d\n", skips);
3005 printf("Failing tests:\n");
3006 for (i = 0; i < limit; ++i) {
3007 if (tests[i].failures)
3008 printf(" %d: %s (%d failures)\n", i,
3009 tests[i].name, tests[i].failures);
3012 printf("Details for failing tests: %s\n", tmpdir);
3015 if (verbosity == VERBOSITY_SUMMARY_ONLY)
3017 printf("%d tests passed, no failures\n", tests_run);
3022 /* If the final tmpdir is empty, we can remove it. */
3023 /* This should be the usual case when all tests succeed. */
3027 return (tests_failed ? 1 : 0);