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 mode_t umasked(mode_t expected_mode)
135 mode_t mode = umask(0);
137 return expected_mode & ~mode;
140 /* Path to working directory for current test */
141 const char *testworkdir;
143 /* Pathname of exe to be tested. */
144 const char *testprogfile;
145 /* Name of exe to use in printf-formatted command strings. */
146 /* On Windows, this includes leading/trailing quotes. */
147 const char *testprog;
150 #if defined(_WIN32) && !defined(__CYGWIN__)
151 static void *GetFunctionKernel32(const char *);
152 static int my_CreateSymbolicLinkA(const char *, const char *, int);
153 static int my_CreateHardLinkA(const char *, const char *);
154 static int my_GetFileInformationByName(const char *,
155 BY_HANDLE_FILE_INFORMATION *);
158 GetFunctionKernel32(const char *name)
160 static HINSTANCE lib;
164 lib = LoadLibrary("kernel32.dll");
167 fprintf(stderr, "Can't load kernel32.dll?!\n");
170 return (void *)GetProcAddress(lib, name);
174 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
176 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
180 f = GetFunctionKernel32("CreateSymbolicLinkA");
182 return f == NULL ? 0 : (*f)(linkname, target, flags);
186 my_CreateHardLinkA(const char *linkname, const char *target)
188 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
192 f = GetFunctionKernel32("CreateHardLinkA");
194 return f == NULL ? 0 : (*f)(linkname, target, NULL);
198 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
203 memset(bhfi, 0, sizeof(*bhfi));
204 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
205 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
206 if (h == INVALID_HANDLE_VALUE)
208 r = GetFileInformationByHandle(h, bhfi);
214 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
216 invalid_parameter_handler(const wchar_t * expression,
217 const wchar_t * function, const wchar_t * file,
218 unsigned int line, uintptr_t pReserved)
230 /* Enable core dump on failure. */
231 static int dump_on_failure = 0;
232 /* Default is to remove temp dirs and log data for successful tests. */
233 static int keep_temp_files = 0;
234 /* Default is to run the specified tests once and report errors. */
235 static int until_failure = 0;
236 /* Default is to just report pass/fail for each test. */
237 static int verbosity = 0;
238 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
239 #define VERBOSITY_PASSFAIL 0 /* Default */
240 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
241 #define VERBOSITY_FULL 2 /* -vv */
242 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
243 * mostly for debugging the test harness itself. */
244 /* Cumulative count of assertion failures. */
245 static int failures = 0;
246 /* Cumulative count of reported skips. */
247 static int skips = 0;
248 /* Cumulative count of assertions checked. */
249 static int assertions = 0;
251 /* Directory where uuencoded reference files can be found. */
252 static const char *refdir;
255 * Report log information selectively to console and/or disk log.
257 static int log_console = 0;
258 static FILE *logfile;
260 vlogprintf(const char *fmt, va_list ap)
267 vfprintf(stdout, fmt, ap);
270 vfprintf(logfile, fmt, lfap);
273 vfprintf(logfile, fmt, ap);
278 logprintf(const char *fmt, ...)
286 /* Set up a message to display only if next assertion fails. */
287 static char msgbuff[4096];
288 static const char *msg, *nextmsg;
290 failure(const char *fmt, ...)
297 vsprintf(msgbuff, fmt, ap);
304 * Copy arguments into file-local variables.
305 * This was added to permit vararg assert() functions without needing
306 * variadic wrapper macros. Turns out that the vararg capability is almost
307 * never used, so almost all of the vararg assertions can be simplified
308 * by removing the vararg capability and reworking the wrapper macro to
309 * pass __FILE__, __LINE__ directly into the function instead of using
310 * this hook. I suspect this machinery is used so rarely that we
311 * would be better off just removing it entirely. That would simplify
312 * the code here noticeably.
314 static const char *skipping_filename;
315 static int skipping_line;
316 void skipping_setup(const char *filename, int line)
318 skipping_filename = filename;
319 skipping_line = line;
322 /* Called at the beginning of each assert() function. */
324 assertion_count(const char *file, int line)
326 (void)file; /* UNUSED */
327 (void)line; /* UNUSED */
329 /* Proper handling of "failure()" message. */
332 /* Uncomment to print file:line after every assertion.
333 * Verbose, but occasionally useful in tracking down crashes. */
334 /* printf("Checked %s:%d\n", file, line); */
338 * For each test source file, we remember how many times each
339 * assertion was reported. Cleared before each new test,
340 * used by test_summarize().
345 } failed_lines[10000];
346 const char *failed_filename;
348 /* Count this failure, setup up log destination and handle initial report. */
350 failure_start(const char *filename, int line, const char *fmt, ...)
354 /* Record another failure for this line. */
356 failed_filename = filename;
357 failed_lines[line].count++;
359 /* Determine whether to log header to console. */
361 case VERBOSITY_LIGHT_REPORT:
362 log_console = (failed_lines[line].count < 2);
365 log_console = (verbosity >= VERBOSITY_FULL);
368 /* Log file:line header for this failure */
371 logprintf("%s(%d): ", filename, line);
373 logprintf("%s:%d: ", filename, line);
379 if (msg != NULL && msg[0] != '\0') {
380 logprintf(" Description: %s\n", msg);
384 /* Determine whether to log details to console. */
385 if (verbosity == VERBOSITY_LIGHT_REPORT)
389 /* Complete reporting of failed tests. */
391 * The 'extra' hook here is used by libarchive to include libarchive
392 * error messages with assertion failures. It could also be used
393 * to add strerror() output, for example. Just define the EXTRA_DUMP()
394 * macro appropriately.
397 failure_finish(void *extra)
399 (void)extra; /* UNUSED (maybe) */
402 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
403 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
407 if (dump_on_failure) {
409 " *** forcing core dump so failure can be debugged ***\n");
414 /* Inform user that we're skipping some checks. */
416 test_skipping(const char *fmt, ...)
422 vsprintf(buff, fmt, ap);
424 /* Use failure() message if set. */
427 /* failure_start() isn't quite right, but is awfully convenient. */
428 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
429 --failures; /* Undo failures++ in failure_start() */
430 /* Don't failure_finish() here. */
431 /* Mark as skip, so doesn't count as failed test. */
432 failed_lines[skipping_line].skip = 1;
442 /* Generic assert() just displays the failed condition. */
444 assertion_assert(const char *file, int line, int value,
445 const char *condition, void *extra)
447 assertion_count(file, line);
449 failure_start(file, line, "Assertion failed: %s", condition);
450 failure_finish(extra);
455 /* chdir() and report any errors */
457 assertion_chdir(const char *file, int line, const char *pathname)
459 assertion_count(file, line);
460 if (chdir(pathname) == 0)
462 failure_start(file, line, "chdir(\"%s\")", pathname);
463 failure_finish(NULL);
468 /* Verify two integers are equal. */
470 assertion_equal_int(const char *file, int line,
471 long long v1, const char *e1, long long v2, const char *e2, void *extra)
473 assertion_count(file, line);
476 failure_start(file, line, "%s != %s", e1, e2);
477 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
478 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
479 failure_finish(extra);
484 * Utility to convert a single UTF-8 sequence.
487 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
489 static const char utf8_count[256] = {
490 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
491 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
492 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
493 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
494 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
495 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
496 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
497 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
498 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
499 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
500 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
501 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
502 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
503 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
504 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
505 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
517 * Decode 1-4 bytes depending on the value of the first byte.
519 ch = (unsigned char)*s;
521 return (0); /* Standard: return 0 for end-of-string. */
522 cnt = utf8_count[ch];
524 /* Invalid sequence or there are not plenty bytes. */
528 /* Make a Unicode code point from a single UTF-8 sequence. */
530 case 1: /* 1 byte sequence. */
533 case 2: /* 2 bytes sequence. */
534 if ((s[1] & 0xc0) != 0x80) return (-1);
535 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
537 case 3: /* 3 bytes sequence. */
538 if ((s[1] & 0xc0) != 0x80) return (-1);
539 if ((s[2] & 0xc0) != 0x80) return (-1);
540 wc = ((ch & 0x0f) << 12)
541 | ((s[1] & 0x3f) << 6)
544 return (-1);/* Overlong sequence. */
546 case 4: /* 4 bytes sequence. */
549 if ((s[1] & 0xc0) != 0x80) return (-1);
550 if ((s[2] & 0xc0) != 0x80) return (-1);
551 if ((s[3] & 0xc0) != 0x80) return (-1);
552 wc = ((ch & 0x07) << 18)
553 | ((s[1] & 0x3f) << 12)
554 | ((s[2] & 0x3f) << 6)
557 return (-1);/* Overlong sequence. */
563 /* The code point larger than 0x10FFFF is not legal
567 /* Correctly gets a Unicode, returns used bytes. */
572 static void strdump(const char *e, const char *p, int ewidth, int utf8)
576 logprintf(" %*s = ", ewidth, e);
583 unsigned int c = 0xff & *p++;
585 case '\a': logprintf("\\a"); break;
586 case '\b': logprintf("\\b"); break;
587 case '\n': logprintf("\\n"); break;
588 case '\r': logprintf("\\r"); break;
590 if (c >= 32 && c < 127)
593 logprintf("\\x%02X", c);
597 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
600 * If the current string is UTF-8, dump its code points.
611 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
614 logprintf("%04X", uc);
620 logprintf(" (count %d", cnt);
622 logprintf(",unknown %d bytes", len);
630 /* Verify two strings are equal, dump them if not. */
632 assertion_equal_string(const char *file, int line,
633 const char *v1, const char *e1,
634 const char *v2, const char *e2,
635 void *extra, int utf8)
639 assertion_count(file, line);
640 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
642 failure_start(file, line, "%s != %s", e1, e2);
643 l1 = (int)strlen(e1);
644 l2 = (int)strlen(e2);
647 strdump(e1, v1, l1, utf8);
648 strdump(e2, v2, l1, utf8);
649 failure_finish(extra);
654 wcsdump(const char *e, const wchar_t *w)
656 logprintf(" %s = ", e);
662 while (*w != L'\0') {
663 unsigned int c = *w++;
664 if (c >= 32 && c < 127)
667 logprintf("\\x%02X", c);
668 else if (c < 0x10000)
669 logprintf("\\u%04X", c);
671 logprintf("\\U%08X", c);
678 wcscmp(const wchar_t *s1, const wchar_t *s2)
681 while (*s1 == *s2++) {
692 /* Verify that two wide strings are equal, dump them if not. */
694 assertion_equal_wstring(const char *file, int line,
695 const wchar_t *v1, const char *e1,
696 const wchar_t *v2, const char *e2,
699 assertion_count(file, line);
702 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
704 failure_start(file, line, "%s != %s", e1, e2);
707 failure_finish(extra);
712 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
713 * any bytes in p that differ from ref will be highlighted with '_'
714 * before and after the hex value.
717 hexdump(const char *p, const char *ref, size_t l, size_t offset)
723 logprintf("(null)\n");
726 for(i=0; i < l; i+=16) {
727 logprintf("%04x", (unsigned)(i + offset));
729 for (j = 0; j < 16 && i + j < l; j++) {
730 if (ref != NULL && p[i + j] != ref[i + j])
732 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
733 if (ref != NULL && p[i + j] == ref[i + j])
736 for (; j < 16; j++) {
737 logprintf("%c ", sep);
740 logprintf("%c", sep);
741 for (j=0; j < 16 && i + j < l; j++) {
743 if (c >= ' ' && c <= 126)
752 /* Verify that two blocks of memory are the same, display the first
753 * block of differences if they're not. */
755 assertion_equal_mem(const char *file, int line,
756 const void *_v1, const char *e1,
757 const void *_v2, const char *e2,
758 size_t l, const char *ld, void *extra)
760 const char *v1 = (const char *)_v1;
761 const char *v2 = (const char *)_v2;
764 assertion_count(file, line);
765 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
767 if (v1 == NULL || v2 == NULL)
770 failure_start(file, line, "%s != %s", e1, e2);
771 logprintf(" size %s = %d\n", ld, (int)l);
772 /* Dump 48 bytes (3 lines) so that the first difference is
773 * in the second line. */
775 while (l > 64 && memcmp(v1, v2, 32) == 0) {
776 /* Two lines agree, so step forward one line. */
782 logprintf(" Dump of %s\n", e1);
783 hexdump(v1, v2, l < 128 ? l : 128, offset);
784 logprintf(" Dump of %s\n", e2);
785 hexdump(v2, v1, l < 128 ? l : 128, offset);
787 failure_finish(extra);
791 /* Verify that a block of memory is filled with the specified byte. */
793 assertion_memory_filled_with(const char *file, int line,
794 const void *_v1, const char *vd,
795 size_t l, const char *ld,
796 char b, const char *bd, void *extra)
798 const char *v1 = (const char *)_v1;
801 (void)ld; /* UNUSED */
803 assertion_count(file, line);
805 for (i = 0; i < l; ++i) {
813 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
814 logprintf(" Only %d bytes were correct\n", (int)c);
815 failure_finish(extra);
819 /* Verify that the named file exists and is empty. */
821 assertion_empty_file(const char *filename, int line, const char *f1)
828 assertion_count(filename, line);
830 if (stat(f1, &st) != 0) {
831 failure_start(filename, line, "Stat failed: %s", f1);
832 failure_finish(NULL);
838 failure_start(filename, line, "File should be empty: %s", f1);
839 logprintf(" File size: %d\n", (int)st.st_size);
840 logprintf(" Contents:\n");
843 logprintf(" Unable to open %s\n", f1);
845 s = ((off_t)sizeof(buff) < st.st_size) ?
846 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
847 s = fread(buff, 1, s, f);
848 hexdump(buff, NULL, s, 0);
851 failure_finish(NULL);
855 /* Verify that the named file exists and is not empty. */
857 assertion_non_empty_file(const char *filename, int line, const char *f1)
861 assertion_count(filename, line);
863 if (stat(f1, &st) != 0) {
864 failure_start(filename, line, "Stat failed: %s", f1);
865 failure_finish(NULL);
868 if (st.st_size == 0) {
869 failure_start(filename, line, "File empty: %s", f1);
870 failure_finish(NULL);
876 /* Verify that two files have the same contents. */
877 /* TODO: hexdump the first bytes that actually differ. */
879 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
886 assertion_count(filename, line);
888 f1 = fopen(fn1, "rb");
889 f2 = fopen(fn2, "rb");
890 if (f1 == NULL || f2 == NULL) {
896 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
897 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
900 if (n1 == 0 && n2 == 0) {
905 if (memcmp(buff1, buff2, n1) != 0)
910 failure_start(filename, line, "Files not identical");
911 logprintf(" file1=\"%s\"\n", fn1);
912 logprintf(" file2=\"%s\"\n", fn2);
913 failure_finish(NULL);
917 /* Verify that the named file does exist. */
919 assertion_file_exists(const char *filename, int line, const char *f)
921 assertion_count(filename, line);
923 #if defined(_WIN32) && !defined(__CYGWIN__)
927 if (!access(f, F_OK))
930 failure_start(filename, line, "File should exist: %s", f);
931 failure_finish(NULL);
935 /* Verify that the named file doesn't exist. */
937 assertion_file_not_exists(const char *filename, int line, const char *f)
939 assertion_count(filename, line);
941 #if defined(_WIN32) && !defined(__CYGWIN__)
948 failure_start(filename, line, "File should not exist: %s", f);
949 failure_finish(NULL);
953 /* Compare the contents of a file to a block of memory. */
955 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
961 assertion_count(filename, line);
965 failure_start(filename, line,
966 "File should exist: %s", fn);
967 failure_finish(NULL);
970 contents = malloc(s * 2);
971 n = (int)fread(contents, 1, s * 2, f);
973 if (n == s && memcmp(buff, contents, s) == 0) {
977 failure_start(filename, line, "File contents don't match");
978 logprintf(" file=\"%s\"\n", fn);
980 hexdump(contents, buff, n > 512 ? 512 : n, 0);
982 logprintf(" File empty, contents should be:\n");
983 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
985 failure_finish(NULL);
990 /* Check the contents of a text file, being tolerant of line endings. */
992 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
995 const char *btxt, *ftxt;
999 assertion_count(filename, line);
1002 failure_start(filename, line,
1003 "File doesn't exist: %s", fn);
1004 failure_finish(NULL);
1007 s = (int)strlen(buff);
1008 contents = malloc(s * 2 + 128);
1009 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1013 /* Compare texts. */
1015 ftxt = (const char *)contents;
1016 while (*btxt != '\0' && *ftxt != '\0') {
1017 if (*btxt == *ftxt) {
1022 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1023 /* Pass over different new line characters. */
1030 if (*btxt == '\0' && *ftxt == '\0') {
1034 failure_start(filename, line, "Contents don't match");
1035 logprintf(" file=\"%s\"\n", fn);
1037 hexdump(contents, buff, n, 0);
1038 logprintf(" expected\n", fn);
1039 hexdump(buff, contents, s, 0);
1041 logprintf(" File empty, contents should be:\n");
1042 hexdump(buff, NULL, s, 0);
1044 failure_finish(NULL);
1049 /* Verify that a text file contains the specified lines, regardless of order */
1050 /* This could be more efficient if we sorted both sets of lines, etc, but
1051 * since this is used only for testing and only ever deals with a dozen or so
1052 * lines at a time, this relatively crude approach is just fine. */
1054 assertion_file_contains_lines_any_order(const char *file, int line,
1055 const char *pathname, const char *lines[])
1059 size_t expected_count, actual_count, i, j;
1060 char **expected = NULL;
1061 char *p, **actual = NULL;
1063 int expected_failure = 0, actual_failure = 0;
1065 assertion_count(file, line);
1067 buff = slurpfile(&buff_size, "%s", pathname);
1069 failure_start(pathname, line, "Can't read file: %s", pathname);
1070 failure_finish(NULL);
1074 /* Make a copy of the provided lines and count up the expected
1076 for (i = 0; lines[i] != NULL; ++i) {
1079 if (expected_count) {
1080 expected = malloc(sizeof(char *) * expected_count);
1081 if (expected == NULL) {
1082 failure_start(pathname, line, "Can't allocate memory");
1083 failure_finish(NULL);
1087 for (i = 0; lines[i] != NULL; ++i) {
1088 expected[i] = strdup(lines[i]);
1092 /* Break the file into lines */
1094 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1095 if (*p == '\x0d' || *p == '\x0a')
1097 if (c == '\0' && *p != '\0')
1102 actual = calloc(sizeof(char *), actual_count);
1103 if (actual == NULL) {
1104 failure_start(pathname, line, "Can't allocate memory");
1105 failure_finish(NULL);
1109 for (j = 0, p = buff; p < buff + buff_size;
1110 p += 1 + strlen(p)) {
1118 /* Erase matching lines from both lists */
1119 for (i = 0; i < expected_count; ++i) {
1120 if (expected[i] == NULL)
1122 for (j = 0; j < actual_count; ++j) {
1123 if (actual[j] == NULL)
1125 if (strcmp(expected[i], actual[j]) == 0) {
1134 /* If there's anything left, it's a failure */
1135 for (i = 0; i < expected_count; ++i) {
1136 if (expected[i] != NULL)
1139 for (j = 0; j < actual_count; ++j) {
1140 if (actual[j] != NULL)
1143 if (expected_failure == 0 && actual_failure == 0) {
1149 failure_start(file, line, "File doesn't match: %s", pathname);
1150 for (i = 0; i < expected_count; ++i) {
1151 if (expected[i] != NULL) {
1152 logprintf(" Expected but not present: %s\n", expected[i]);
1156 for (j = 0; j < actual_count; ++j) {
1157 if (actual[j] != NULL)
1158 logprintf(" Present but not expected: %s\n", actual[j]);
1160 failure_finish(NULL);
1167 /* Verify that a text file does not contains the specified strings */
1169 assertion_file_contains_no_invalid_strings(const char *file, int line,
1170 const char *pathname, const char *strings[])
1175 buff = slurpfile(NULL, "%s", pathname);
1177 failure_start(file, line, "Can't read file: %s", pathname);
1178 failure_finish(NULL);
1182 for (i = 0; strings[i] != NULL; ++i) {
1183 if (strstr(buff, strings[i]) != NULL) {
1184 failure_start(file, line, "Invalid string in %s: %s", pathname,
1186 failure_finish(NULL);
1196 /* Test that two paths point to the same file. */
1197 /* As a side-effect, asserts that both files exist. */
1199 is_hardlink(const char *file, int line,
1200 const char *path1, const char *path2)
1202 #if defined(_WIN32) && !defined(__CYGWIN__)
1203 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1206 assertion_count(file, line);
1207 r = my_GetFileInformationByName(path1, &bhfi1);
1209 failure_start(file, line, "File %s can't be inspected?", path1);
1210 failure_finish(NULL);
1213 r = my_GetFileInformationByName(path2, &bhfi2);
1215 failure_start(file, line, "File %s can't be inspected?", path2);
1216 failure_finish(NULL);
1219 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1220 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1221 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1223 struct stat st1, st2;
1226 assertion_count(file, line);
1227 r = lstat(path1, &st1);
1229 failure_start(file, line, "File should exist: %s", path1);
1230 failure_finish(NULL);
1233 r = lstat(path2, &st2);
1235 failure_start(file, line, "File should exist: %s", path2);
1236 failure_finish(NULL);
1239 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1244 assertion_is_hardlink(const char *file, int line,
1245 const char *path1, const char *path2)
1247 if (is_hardlink(file, line, path1, path2))
1249 failure_start(file, line,
1250 "Files %s and %s are not hardlinked", path1, path2);
1251 failure_finish(NULL);
1256 assertion_is_not_hardlink(const char *file, int line,
1257 const char *path1, const char *path2)
1259 if (!is_hardlink(file, line, path1, path2))
1261 failure_start(file, line,
1262 "Files %s and %s should not be hardlinked", path1, path2);
1263 failure_finish(NULL);
1267 /* Verify a/b/mtime of 'pathname'. */
1268 /* If 'recent', verify that it's within last 10 seconds. */
1270 assertion_file_time(const char *file, int line,
1271 const char *pathname, long t, long nsec, char type, int recent)
1273 long long filet, filet_nsec;
1276 #if defined(_WIN32) && !defined(__CYGWIN__)
1277 #define EPOC_TIME (116444736000000000ULL)
1278 FILETIME fxtime, fbirthtime, fatime, fmtime;
1279 ULARGE_INTEGER wintm;
1281 fxtime.dwLowDateTime = 0;
1282 fxtime.dwHighDateTime = 0;
1284 assertion_count(file, line);
1285 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1286 * a directory file. If not, CreateFile() will fail when
1287 * the pathname is a directory. */
1288 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1289 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1290 if (h == INVALID_HANDLE_VALUE) {
1291 failure_start(file, line, "Can't access %s\n", pathname);
1292 failure_finish(NULL);
1295 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1297 case 'a': fxtime = fatime; break;
1298 case 'b': fxtime = fbirthtime; break;
1299 case 'm': fxtime = fmtime; break;
1303 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1304 failure_finish(NULL);
1307 wintm.LowPart = fxtime.dwLowDateTime;
1308 wintm.HighPart = fxtime.dwHighDateTime;
1309 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1310 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1311 nsec = (nsec / 100) * 100; /* Round the request */
1315 assertion_count(file, line);
1316 r = lstat(pathname, &st);
1318 failure_start(file, line, "Can't stat %s\n", pathname);
1319 failure_finish(NULL);
1323 case 'a': filet = st.st_atime; break;
1324 case 'm': filet = st.st_mtime; break;
1325 case 'b': filet = 0; break;
1326 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1329 #if defined(__FreeBSD__)
1331 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1332 case 'b': filet = st.st_birthtime;
1333 /* FreeBSD filesystems that don't support birthtime
1334 * (e.g., UFS1) always return -1 here. */
1338 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1339 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1340 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1343 /* FreeBSD generally only stores to microsecond res, so round. */
1344 filet_nsec = (filet_nsec / 1000) * 1000;
1345 nsec = (nsec / 1000) * 1000;
1347 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1348 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1349 #if defined(__HAIKU__)
1350 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1355 /* Check that requested time is up-to-date. */
1356 time_t now = time(NULL);
1357 if (filet < now - 10 || filet > now + 1) {
1358 failure_start(file, line,
1359 "File %s has %ctime %lld, %lld seconds ago\n",
1360 pathname, type, filet, now - filet);
1361 failure_finish(NULL);
1364 } else if (filet != t || filet_nsec != nsec) {
1365 failure_start(file, line,
1366 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1367 pathname, type, filet, filet_nsec, t, nsec);
1368 failure_finish(NULL);
1374 /* Verify atime of 'pathname'. */
1376 assertion_file_atime(const char *file, int line,
1377 const char *pathname, long t, long nsec)
1379 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1382 /* Verify atime of 'pathname' is up-to-date. */
1384 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1386 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1389 /* Verify birthtime of 'pathname'. */
1391 assertion_file_birthtime(const char *file, int line,
1392 const char *pathname, long t, long nsec)
1394 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1397 /* Verify birthtime of 'pathname' is up-to-date. */
1399 assertion_file_birthtime_recent(const char *file, int line,
1400 const char *pathname)
1402 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1405 /* Verify mode of 'pathname'. */
1407 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1412 assertion_count(file, line);
1413 #if defined(_WIN32) && !defined(__CYGWIN__)
1414 failure_start(file, line, "assertFileMode not yet implemented for Windows");
1415 (void)mode; /* UNUSED */
1416 (void)r; /* UNUSED */
1420 r = lstat(pathname, &st);
1421 mode = (int)(st.st_mode & 0777);
1423 if (r == 0 && mode == expected_mode)
1425 failure_start(file, line, "File %s has mode %o, expected %o",
1426 pathname, mode, expected_mode);
1428 failure_finish(NULL);
1432 /* Verify mtime of 'pathname'. */
1434 assertion_file_mtime(const char *file, int line,
1435 const char *pathname, long t, long nsec)
1437 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1440 /* Verify mtime of 'pathname' is up-to-date. */
1442 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1444 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1447 /* Verify number of links to 'pathname'. */
1449 assertion_file_nlinks(const char *file, int line,
1450 const char *pathname, int nlinks)
1452 #if defined(_WIN32) && !defined(__CYGWIN__)
1453 BY_HANDLE_FILE_INFORMATION bhfi;
1456 assertion_count(file, line);
1457 r = my_GetFileInformationByName(pathname, &bhfi);
1458 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1460 failure_start(file, line, "File %s has %d links, expected %d",
1461 pathname, bhfi.nNumberOfLinks, nlinks);
1462 failure_finish(NULL);
1468 assertion_count(file, line);
1469 r = lstat(pathname, &st);
1470 if (r == 0 && (int)st.st_nlink == nlinks)
1472 failure_start(file, line, "File %s has %d links, expected %d",
1473 pathname, st.st_nlink, nlinks);
1474 failure_finish(NULL);
1479 /* Verify size of 'pathname'. */
1481 assertion_file_size(const char *file, int line, const char *pathname, long size)
1486 assertion_count(file, line);
1487 #if defined(_WIN32) && !defined(__CYGWIN__)
1489 BY_HANDLE_FILE_INFORMATION bhfi;
1490 r = !my_GetFileInformationByName(pathname, &bhfi);
1491 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1496 r = lstat(pathname, &st);
1497 filesize = st.st_size;
1500 if (r == 0 && filesize == size)
1502 failure_start(file, line, "File %s has size %ld, expected %ld",
1503 pathname, (long)filesize, (long)size);
1504 failure_finish(NULL);
1508 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1510 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1515 #if defined(_WIN32) && !defined(__CYGWIN__)
1516 (void)mode; /* UNUSED */
1518 assertion_count(file, line);
1519 r = lstat(pathname, &st);
1521 failure_start(file, line, "Dir should exist: %s", pathname);
1522 failure_finish(NULL);
1525 if (!S_ISDIR(st.st_mode)) {
1526 failure_start(file, line, "%s is not a dir", pathname);
1527 failure_finish(NULL);
1530 #if !defined(_WIN32) || defined(__CYGWIN__)
1531 /* Windows doesn't handle permissions the same way as POSIX,
1532 * so just ignore the mode tests. */
1533 /* TODO: Can we do better here? */
1534 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1535 failure_start(file, line, "Dir %s has wrong mode", pathname);
1536 logprintf(" Expected: 0%3o\n", mode);
1537 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1538 failure_finish(NULL);
1545 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1546 * verify that too. */
1548 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1553 #if defined(_WIN32) && !defined(__CYGWIN__)
1554 (void)mode; /* UNUSED */
1556 assertion_count(file, line);
1557 r = lstat(pathname, &st);
1558 if (r != 0 || !S_ISREG(st.st_mode)) {
1559 failure_start(file, line, "File should exist: %s", pathname);
1560 failure_finish(NULL);
1563 #if !defined(_WIN32) || defined(__CYGWIN__)
1564 /* Windows doesn't handle permissions the same way as POSIX,
1565 * so just ignore the mode tests. */
1566 /* TODO: Can we do better here? */
1567 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1568 failure_start(file, line, "File %s has wrong mode", pathname);
1569 logprintf(" Expected: 0%3o\n", mode);
1570 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1571 failure_finish(NULL);
1578 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1579 * non-NULL, verify that the symlink has those contents. */
1581 is_symlink(const char *file, int line,
1582 const char *pathname, const char *contents)
1584 #if defined(_WIN32) && !defined(__CYGWIN__)
1585 (void)pathname; /* UNUSED */
1586 (void)contents; /* UNUSED */
1587 assertion_count(file, line);
1588 /* Windows sort-of has real symlinks, but they're only usable
1589 * by privileged users and are crippled even then, so there's
1590 * really not much point in bothering with this. */
1598 assertion_count(file, line);
1599 r = lstat(pathname, &st);
1601 failure_start(file, line,
1602 "Symlink should exist: %s", pathname);
1603 failure_finish(NULL);
1606 if (!S_ISLNK(st.st_mode))
1608 if (contents == NULL)
1610 linklen = readlink(pathname, buff, sizeof(buff));
1612 failure_start(file, line, "Can't read symlink %s", pathname);
1613 failure_finish(NULL);
1616 buff[linklen] = '\0';
1617 if (strcmp(buff, contents) != 0)
1623 /* Assert that path is a symlink that (optionally) contains contents. */
1625 assertion_is_symlink(const char *file, int line,
1626 const char *path, const char *contents)
1628 if (is_symlink(file, line, path, contents))
1631 failure_start(file, line, "File %s is not a symlink to %s",
1634 failure_start(file, line, "File %s is not a symlink", path);
1635 failure_finish(NULL);
1640 /* Create a directory and report any errors. */
1642 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1644 assertion_count(file, line);
1645 #if defined(_WIN32) && !defined(__CYGWIN__)
1646 (void)mode; /* UNUSED */
1647 if (0 == _mkdir(dirname))
1650 if (0 == mkdir(dirname, mode)) {
1651 if (0 == chmod(dirname, mode)) {
1652 assertion_file_mode(file, line, dirname, mode);
1657 failure_start(file, line, "Could not create directory %s", dirname);
1658 failure_finish(NULL);
1662 /* Create a file with the specified contents and report any failures. */
1664 assertion_make_file(const char *file, int line,
1665 const char *path, int mode, int csize, const void *contents)
1667 #if defined(_WIN32) && !defined(__CYGWIN__)
1668 /* TODO: Rework this to set file mode as well. */
1670 (void)mode; /* UNUSED */
1671 assertion_count(file, line);
1672 f = fopen(path, "wb");
1674 failure_start(file, line, "Could not create file %s", path);
1675 failure_finish(NULL);
1678 if (contents != NULL) {
1682 wsize = strlen(contents);
1684 wsize = (size_t)csize;
1685 if (wsize != fwrite(contents, 1, wsize, f)) {
1687 failure_start(file, line,
1688 "Could not write file %s", path);
1689 failure_finish(NULL);
1697 assertion_count(file, line);
1698 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1700 failure_start(file, line, "Could not create %s", path);
1701 failure_finish(NULL);
1704 if (0 != chmod(path, mode)) {
1705 failure_start(file, line, "Could not chmod %s", path);
1706 failure_finish(NULL);
1710 if (contents != NULL) {
1714 wsize = (ssize_t)strlen(contents);
1716 wsize = (ssize_t)csize;
1717 if (wsize != write(fd, contents, wsize)) {
1719 failure_start(file, line,
1720 "Could not write to %s", path);
1721 failure_finish(NULL);
1727 assertion_file_mode(file, line, path, mode);
1732 /* Create a hardlink and report any failures. */
1734 assertion_make_hardlink(const char *file, int line,
1735 const char *newpath, const char *linkto)
1739 assertion_count(file, line);
1740 #if defined(_WIN32) && !defined(__CYGWIN__)
1741 succeeded = my_CreateHardLinkA(newpath, linkto);
1743 succeeded = !link(linkto, newpath);
1749 failure_start(file, line, "Could not create hardlink");
1750 logprintf(" New link: %s\n", newpath);
1751 logprintf(" Old name: %s\n", linkto);
1752 failure_finish(NULL);
1756 /* Create a symlink and report any failures. */
1758 assertion_make_symlink(const char *file, int line,
1759 const char *newpath, const char *linkto)
1761 #if defined(_WIN32) && !defined(__CYGWIN__)
1762 int targetIsDir = 0; /* TODO: Fix this */
1763 assertion_count(file, line);
1764 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1767 assertion_count(file, line);
1768 if (0 == symlink(linkto, newpath))
1771 failure_start(file, line, "Could not create symlink");
1772 logprintf(" New link: %s\n", newpath);
1773 logprintf(" Old name: %s\n", linkto);
1774 failure_finish(NULL);
1778 /* Set umask, report failures. */
1780 assertion_umask(const char *file, int line, int mask)
1782 assertion_count(file, line);
1783 (void)file; /* UNUSED */
1784 (void)line; /* UNUSED */
1789 /* Set times, report failures. */
1791 assertion_utimes(const char *file, int line,
1792 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1796 #if defined(_WIN32) && !defined(__CYGWIN__)
1797 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1798 + (((nsec)/1000)*10))
1800 ULARGE_INTEGER wintm;
1801 FILETIME fatime, fmtime;
1802 FILETIME *pat, *pmt;
1804 assertion_count(file, line);
1805 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1806 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1807 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1808 if (h == INVALID_HANDLE_VALUE) {
1809 failure_start(file, line, "Can't access %s\n", pathname);
1810 failure_finish(NULL);
1814 if (at > 0 || at_nsec > 0) {
1815 wintm.QuadPart = WINTIME(at, at_nsec);
1816 fatime.dwLowDateTime = wintm.LowPart;
1817 fatime.dwHighDateTime = wintm.HighPart;
1821 if (mt > 0 || mt_nsec > 0) {
1822 wintm.QuadPart = WINTIME(mt, mt_nsec);
1823 fmtime.dwLowDateTime = wintm.LowPart;
1824 fmtime.dwHighDateTime = wintm.HighPart;
1828 if (pat != NULL || pmt != NULL)
1829 r = SetFileTime(h, NULL, pat, pmt);
1834 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1835 failure_finish(NULL);
1839 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1841 struct timeval times[2];
1843 #if !defined(__FreeBSD__)
1844 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1846 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1849 r = lstat(pathname, &st);
1851 failure_start(file, line, "Can't stat %s\n", pathname);
1852 failure_finish(NULL);
1856 if (mt == 0 && mt_nsec == 0) {
1858 #if defined(__FreeBSD__)
1859 mt_nsec = st.st_mtimespec.tv_nsec;
1860 /* FreeBSD generally only stores to microsecond res, so round. */
1861 mt_nsec = (mt_nsec / 1000) * 1000;
1864 if (at == 0 && at_nsec == 0) {
1866 #if defined(__FreeBSD__)
1867 at_nsec = st.st_atimespec.tv_nsec;
1868 /* FreeBSD generally only stores to microsecond res, so round. */
1869 at_nsec = (at_nsec / 1000) * 1000;
1873 times[1].tv_sec = mt;
1874 times[1].tv_usec = mt_nsec / 1000;
1876 times[0].tv_sec = at;
1877 times[0].tv_usec = at_nsec / 1000;
1880 r = lutimes(pathname, times);
1882 r = utimes(pathname, times);
1885 failure_start(file, line, "Can't utimes %s\n", pathname);
1886 failure_finish(NULL);
1890 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1893 /* Set nodump, report failures. */
1895 assertion_nodump(const char *file, int line, const char *pathname)
1897 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1900 assertion_count(file, line);
1901 r = chflags(pathname, UF_NODUMP);
1903 failure_start(file, line, "Can't set nodump %s\n", pathname);
1904 failure_finish(NULL);
1907 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1908 && defined(EXT2_NODUMP_FL)
1911 assertion_count(file, line);
1912 fd = open(pathname, O_RDONLY | O_NONBLOCK);
1914 failure_start(file, line, "Can't open %s\n", pathname);
1915 failure_finish(NULL);
1918 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1920 failure_start(file, line, "Can't get flags %s\n", pathname);
1921 failure_finish(NULL);
1924 flags |= EXT2_NODUMP_FL;
1925 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1927 failure_start(file, line, "Can't set nodump %s\n", pathname);
1928 failure_finish(NULL);
1933 (void)pathname; /* UNUSED */
1934 assertion_count(file, line);
1941 * UTILITIES for use by tests.
1946 * Check whether platform supports symlinks. This is intended
1947 * for tests to use in deciding whether to bother testing symlink
1948 * support; if the platform doesn't support symlinks, there's no point
1949 * in checking whether the program being tested can create them.
1951 * Note that the first time this test is called, we actually go out to
1952 * disk to create and verify a symlink. This is necessary because
1953 * symlink support is actually a property of a particular filesystem
1954 * and can thus vary between directories on a single system. After
1955 * the first call, this returns the cached result from memory, so it's
1956 * safe to call it as often as you wish.
1961 /* Remember the test result */
1962 static int value = 0, tested = 0;
1967 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1968 /* Note: Cygwin has its own symlink() emulation that does not
1969 * use the Win32 CreateSymbolicLink() function. */
1970 #if defined(_WIN32) && !defined(__CYGWIN__)
1971 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1972 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1974 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1975 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1980 /* Platform-dependent options for hiding the output of a subcommand. */
1981 #if defined(_WIN32) && !defined(__CYGWIN__)
1982 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1984 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1987 * Can this platform run the bzip2 program?
1992 static int tested = 0, value = 0;
1995 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
2002 * Can this platform run the grzip program?
2007 static int tested = 0, value = 0;
2010 if (systemf("grzip -V %s", redirectArgs) == 0)
2017 * Can this platform run the gzip program?
2022 static int tested = 0, value = 0;
2025 if (systemf("gzip -V %s", redirectArgs) == 0)
2032 * Can this platform run the lrzip program?
2035 canRunCommand(const char *cmd)
2037 static int tested = 0, value = 0;
2040 if (systemf("%s %s", cmd, redirectArgs) == 0)
2049 static int tested = 0, value = 0;
2052 if (systemf("lrzip -V %s", redirectArgs) == 0)
2059 * Can this platform run the lz4 program?
2064 static int tested = 0, value = 0;
2067 if (systemf("lz4 -V %s", redirectArgs) == 0)
2074 * Can this platform run the lzip program?
2079 static int tested = 0, value = 0;
2082 if (systemf("lzip -V %s", redirectArgs) == 0)
2089 * Can this platform run the lzma program?
2094 static int tested = 0, value = 0;
2097 if (systemf("lzma -V %s", redirectArgs) == 0)
2104 * Can this platform run the lzop program?
2109 static int tested = 0, value = 0;
2112 if (systemf("lzop -V %s", redirectArgs) == 0)
2119 * Can this platform run the xz program?
2124 static int tested = 0, value = 0;
2127 if (systemf("xz -V %s", redirectArgs) == 0)
2134 * Can this filesystem handle nodump flags.
2136 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2141 const char *path = "cannodumptest";
2144 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2145 if (chflags(path, UF_NODUMP) < 0)
2147 if (stat(path, &sb) < 0)
2149 if (sb.st_flags & UF_NODUMP)
2154 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2155 && defined(EXT2_NODUMP_FL)
2160 const char *path = "cannodumptest";
2163 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2164 fd = open(path, O_RDONLY | O_NONBLOCK);
2167 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2170 flags |= EXT2_NODUMP_FL;
2171 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2175 fd = open(path, O_RDONLY | O_NONBLOCK);
2178 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2182 if (flags & EXT2_NODUMP_FL)
2198 * Sleep as needed; useful for verifying disk timestamp changes by
2199 * ensuring that the wall-clock time has actually changed before we
2200 * go back to re-read something from disk.
2203 sleepUntilAfter(time_t t)
2205 while (t >= time(NULL))
2206 #if defined(_WIN32) && !defined(__CYGWIN__)
2214 * Call standard system() call, but build up the command line using
2215 * sprintf() conventions.
2218 systemf(const char *fmt, ...)
2225 vsprintf(buff, fmt, ap);
2226 if (verbosity > VERBOSITY_FULL)
2227 logprintf("Cmd: %s\n", buff);
2234 * Slurp a file into memory for ease of comparison and testing.
2235 * Returns size of file in 'sizep' if non-NULL, null-terminates
2236 * data in memory for ease of use.
2239 slurpfile(size_t * sizep, const char *fmt, ...)
2241 char filename[8192];
2250 vsprintf(filename, fmt, ap);
2253 f = fopen(filename, "rb");
2255 /* Note: No error; non-existent file is okay here. */
2258 r = fstat(fileno(f), &st);
2260 logprintf("Can't stat file %s\n", filename);
2264 p = malloc((size_t)st.st_size + 1);
2266 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2267 (long int)st.st_size, filename);
2271 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2272 if (bytes_read < st.st_size) {
2273 logprintf("Can't read file %s\n", filename);
2278 p[st.st_size] = '\0';
2280 *sizep = (size_t)st.st_size;
2286 * Slurp a file into memory for ease of comparison and testing.
2287 * Returns size of file in 'sizep' if non-NULL, null-terminates
2288 * data in memory for ease of use.
2291 dumpfile(const char *filename, void *data, size_t len)
2293 ssize_t bytes_written;
2296 f = fopen(filename, "wb");
2298 logprintf("Can't open file %s for writing\n", filename);
2301 bytes_written = fwrite(data, 1, len, f);
2302 if (bytes_written < (ssize_t)len)
2303 logprintf("Can't write file %s\n", filename);
2307 /* Read a uuencoded file from the reference directory, decode, and
2308 * write the result into the current directory. */
2309 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2310 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2312 extract_reference_file(const char *name)
2317 sprintf(buff, "%s/%s.uu", refdir, name);
2318 in = fopen(buff, "r");
2319 failure("Couldn't open reference file %s", buff);
2323 /* Read up to and including the 'begin' line. */
2325 if (fgets(buff, sizeof(buff), in) == NULL) {
2326 /* TODO: This is a failure. */
2329 if (memcmp(buff, "begin ", 6) == 0)
2332 /* Now, decode the rest and write it. */
2333 out = fopen(name, "wb");
2334 while (fgets(buff, sizeof(buff), in) != NULL) {
2338 if (memcmp(buff, "end", 3) == 0)
2341 bytes = UUDECODE(*p++);
2344 /* Write out 1-3 bytes from that. */
2346 assert(VALID_UUDECODE(p[0]));
2347 assert(VALID_UUDECODE(p[1]));
2348 n = UUDECODE(*p++) << 18;
2349 n |= UUDECODE(*p++) << 12;
2350 fputc(n >> 16, out);
2354 assert(VALID_UUDECODE(p[0]));
2355 n |= UUDECODE(*p++) << 6;
2356 fputc((n >> 8) & 0xFF, out);
2360 assert(VALID_UUDECODE(p[0]));
2361 n |= UUDECODE(*p++);
2362 fputc(n & 0xFF, out);
2372 copy_reference_file(const char *name)
2378 sprintf(buff, "%s/%s", refdir, name);
2379 in = fopen(buff, "rb");
2380 failure("Couldn't open reference file %s", buff);
2384 /* Now, decode the rest and write it. */
2385 /* Not a lot of error checking here; the input better be right. */
2386 out = fopen(name, "wb");
2387 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2388 if (fwrite(buff, 1, rbytes, out) != rbytes) {
2389 logprintf("Error: fwrite\n");
2398 is_LargeInode(const char *file)
2400 #if defined(_WIN32) && !defined(__CYGWIN__)
2401 BY_HANDLE_FILE_INFORMATION bhfi;
2404 r = my_GetFileInformationByName(file, &bhfi);
2407 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2412 if (stat(file, &st) < 0)
2414 ino = (int64_t)st.st_ino;
2415 return (ino > 0xffffffff);
2420 extract_reference_files(const char **names)
2422 while (names && *names)
2423 extract_reference_file(*names++);
2433 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2435 * DEFINE_TEST(test_function)
2439 /* Use "list.h" to declare all of the test functions. */
2441 #define DEFINE_TEST(name) void name(void);
2444 /* Use "list.h" to create a list of all tests (functions and names). */
2446 #define DEFINE_TEST(n) { n, #n, 0 },
2447 struct test_list_t tests[] = {
2452 * Summarize repeated failures in the just-completed test.
2455 test_summarize(int failed, int skips_num)
2459 switch (verbosity) {
2460 case VERBOSITY_SUMMARY_ONLY:
2461 printf(failed ? "E" : ".");
2464 case VERBOSITY_PASSFAIL:
2465 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
2469 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2471 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2472 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2473 logprintf("%s:%d: Summary: Failed %d times\n",
2474 failed_filename, i, failed_lines[i].count);
2476 /* Clear the failure history for the next file. */
2477 failed_filename = NULL;
2478 memset(failed_lines, 0, sizeof(failed_lines));
2482 * Actually run a single test, with appropriate setup and cleanup.
2485 test_run(int i, const char *tmpdir)
2488 char logfilename[64];
2489 int failures_before = failures;
2490 int skips_before = skips;
2493 switch (verbosity) {
2494 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2496 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2497 printf("%3d: %-64s", i, tests[i].name);
2500 default: /* Title of test, details will follow */
2501 printf("%3d: %s\n", i, tests[i].name);
2504 /* Chdir to the top-level work directory. */
2505 if (!assertChdir(tmpdir)) {
2507 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2510 /* Create a log file for this test. */
2511 sprintf(logfilename, "%s.log", tests[i].name);
2512 logfile = fopen(logfilename, "w");
2513 fprintf(logfile, "%s\n\n", tests[i].name);
2514 /* Chdir() to a work dir for this specific test. */
2515 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2516 testworkdir = workdir;
2517 if (!assertMakeDir(testworkdir, 0755)
2518 || !assertChdir(testworkdir)) {
2520 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2523 /* Explicitly reset the locale before each test. */
2524 setlocale(LC_ALL, "C");
2525 /* Record the umask before we run the test. */
2526 umask(oldumask = umask(0));
2528 * Run the actual test.
2532 * Clean up and report afterwards.
2538 setlocale(LC_ALL, "C");
2539 /* Reset directory. */
2540 if (!assertChdir(tmpdir)) {
2541 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2545 /* Report per-test summaries. */
2546 tests[i].failures = failures - failures_before;
2547 test_summarize(tests[i].failures, skips - skips_before);
2548 /* Close the per-test log file. */
2551 /* If there were no failures, we can remove the work dir and logfile. */
2552 if (tests[i].failures == 0) {
2553 if (!keep_temp_files && assertChdir(tmpdir)) {
2554 #if defined(_WIN32) && !defined(__CYGWIN__)
2555 /* Make sure not to leave empty directories.
2556 * Sometimes a processing of closing files used by tests
2557 * is not done, then rmdir will be failed and it will
2558 * leave a empty test directory. So we should wait a few
2559 * seconds and retry rmdir. */
2561 for (t = 0; t < 10; t++) {
2564 r = systemf("rmdir /S /Q %s", tests[i].name);
2568 systemf("del %s", logfilename);
2570 systemf("rm -rf %s", tests[i].name);
2571 systemf("rm %s", logfilename);
2575 /* Return appropriate status. */
2576 return (tests[i].failures);
2582 * MAIN and support routines.
2588 usage(const char *program)
2590 static const int limit = sizeof(tests) / sizeof(tests[0]);
2593 printf("Usage: %s [options] <test> <test> ...\n", program);
2594 printf("Default is to run all tests.\n");
2595 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2596 printf("Options:\n");
2597 printf(" -d Dump core after any failure, for debugging.\n");
2598 printf(" -k Keep all temp files.\n");
2599 printf(" Default: temp files for successful tests deleted.\n");
2601 printf(" -p <path> Path to executable to be tested.\n");
2602 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2604 printf(" -q Quiet.\n");
2605 printf(" -r <dir> Path to dir containing reference files.\n");
2606 printf(" Default: Current directory.\n");
2607 printf(" -u Keep running specifies tests until one fails.\n");
2608 printf(" -v Verbose.\n");
2609 printf("Available tests:\n");
2610 for (i = 0; i < limit; i++)
2611 printf(" %d: %s\n", i, tests[i].name);
2616 get_refdir(const char *d)
2618 size_t tried_size, buff_size;
2619 char *buff, *tried, *pwd = NULL, *p = NULL;
2622 buff_size = PATH_MAX;
2626 buff = calloc(buff_size, 1);
2628 fprintf(stderr, "Unable to allocate memory\n");
2632 /* Allocate a buffer to hold the various directories we checked. */
2633 tried_size = buff_size * 2;
2634 tried = calloc(tried_size, 1);
2635 if (tried == NULL) {
2636 fprintf(stderr, "Unable to allocate memory\n");
2640 /* If a dir was specified, try that */
2643 snprintf(buff, buff_size, "%s", d);
2644 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2645 if (p != NULL) goto success;
2646 strncat(tried, buff, tried_size - strlen(tried) - 1);
2647 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2651 /* Get the current dir. */
2653 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2655 pwd = getcwd(NULL, 0);
2657 while (pwd[strlen(pwd) - 1] == '\n')
2658 pwd[strlen(pwd) - 1] = '\0';
2660 /* Look for a known file. */
2661 snprintf(buff, buff_size, "%s", pwd);
2662 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2663 if (p != NULL) goto success;
2664 strncat(tried, buff, tried_size - strlen(tried) - 1);
2665 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2667 snprintf(buff, buff_size, "%s/test", pwd);
2668 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2669 if (p != NULL) goto success;
2670 strncat(tried, buff, tried_size - strlen(tried) - 1);
2671 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2673 #if defined(LIBRARY)
2674 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
2676 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
2678 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2679 if (p != NULL) goto success;
2680 strncat(tried, buff, tried_size - strlen(tried) - 1);
2681 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2683 #if defined(PROGRAM_ALIAS)
2684 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
2685 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2686 if (p != NULL) goto success;
2687 strncat(tried, buff, tried_size - strlen(tried) - 1);
2688 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2691 if (memcmp(pwd, "/usr/obj", 8) == 0) {
2692 snprintf(buff, buff_size, "%s", pwd + 8);
2693 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2694 if (p != NULL) goto success;
2695 strncat(tried, buff, tried_size - strlen(tried) - 1);
2696 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2698 snprintf(buff, buff_size, "%s/test", pwd + 8);
2699 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2700 if (p != NULL) goto success;
2701 strncat(tried, buff, tried_size - strlen(tried) - 1);
2702 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2706 printf("Unable to locate known reference file %s\n", KNOWNREF);
2707 printf(" Checked following directories:\n%s\n", tried);
2708 printf("Use -r option to specify full path to reference directory\n");
2709 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2719 /* Copy result into a fresh buffer to reduce memory usage. */
2726 main(int argc, char **argv)
2728 static const int limit = sizeof(tests) / sizeof(tests[0]);
2729 int test_set[sizeof(tests) / sizeof(tests[0])];
2730 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2732 char *refdir_alloc = NULL;
2733 const char *progname;
2735 const char *tmp, *option_arg, *p;
2736 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2737 char tmpdir_timestamp[256];
2739 (void)argc; /* UNUSED */
2741 /* Get the current dir. */
2743 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2745 pwd = getcwd(NULL, 0);
2747 while (pwd[strlen(pwd) - 1] == '\n')
2748 pwd[strlen(pwd) - 1] = '\0';
2750 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
2751 /* To stop to run the default invalid parameter handler. */
2752 _set_invalid_parameter_handler(invalid_parameter_handler);
2753 /* Disable annoying assertion message box. */
2754 _CrtSetReportMode(_CRT_ASSERT, 0);
2758 * Name of this program, used to build root of our temp directory
2761 progname = p = argv[0];
2762 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2764 fprintf(stderr, "ERROR: Out of memory.");
2767 strcpy(testprogdir, progname);
2768 while (*p != '\0') {
2769 /* Support \ or / dir separators for Windows compat. */
2770 if (*p == '/' || *p == '\\')
2778 testprogdir[i] = '\0';
2779 #if defined(_WIN32) && !defined(__CYGWIN__)
2780 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2781 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2782 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2783 testprogdir[1] == ':' &&
2784 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
2786 if (testprogdir[0] != '/')
2789 /* Fixup path for relative directories. */
2790 if ((testprogdir = (char *)realloc(testprogdir,
2791 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2793 fprintf(stderr, "ERROR: Out of memory.");
2796 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2797 strlen(testprogdir) + 1);
2798 memcpy(testprogdir, pwd, strlen(pwd));
2799 testprogdir[strlen(pwd)] = '/';
2803 /* Get the target program from environment, if available. */
2804 testprogfile = getenv(ENVBASE);
2807 if (getenv("TMPDIR") != NULL)
2808 tmp = getenv("TMPDIR");
2809 else if (getenv("TMP") != NULL)
2810 tmp = getenv("TMP");
2811 else if (getenv("TEMP") != NULL)
2812 tmp = getenv("TEMP");
2813 else if (getenv("TEMPDIR") != NULL)
2814 tmp = getenv("TEMPDIR");
2818 /* Allow -d to be controlled through the environment. */
2819 if (getenv(ENVBASE "_DEBUG") != NULL)
2820 dump_on_failure = 1;
2822 /* Allow -v to be controlled through the environment. */
2823 if (getenv("_VERBOSITY_LEVEL") != NULL)
2825 vlevel = getenv("_VERBOSITY_LEVEL");
2826 verbosity = atoi(vlevel);
2827 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2829 /* Unsupported verbosity levels are silently ignored */
2831 verbosity = VERBOSITY_PASSFAIL;
2835 /* Get the directory holding test files from environment. */
2836 refdir = getenv(ENVBASE "_TEST_FILES");
2839 * Parse options, without using getopt(), which isn't available
2842 ++argv; /* Skip program name */
2843 while (*argv != NULL) {
2848 while (*p != '\0') {
2851 /* If 'opt' takes an argument, parse that. */
2852 if (option == 'p' || option == 'r') {
2855 else if (*argv == NULL) {
2857 "Option -%c requires argument.\n",
2861 option_arg = *argv++;
2862 p = ""; /* End of this option word. */
2865 /* Now, handle the option. */
2868 dump_on_failure = 1;
2871 keep_temp_files = 1;
2875 testprogfile = option_arg;
2877 fprintf(stderr, "-p option not permitted\n");
2886 refdir = option_arg;
2896 fprintf(stderr, "Unrecognized option '%c'\n",
2904 * Sanity-check that our options make sense.
2907 if (testprogfile == NULL)
2909 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2910 strlen(PROGRAM) + 1)) == NULL)
2912 fprintf(stderr, "ERROR: Out of memory.");
2915 strcpy(tmp2, testprogdir);
2917 strcat(tmp2, PROGRAM);
2918 testprogfile = tmp2;
2923 #if defined(_WIN32) && !defined(__CYGWIN__)
2924 /* Command.com sometimes rejects '/' separators. */
2925 testprg = strdup(testprogfile);
2926 for (i = 0; testprg[i] != '\0'; i++) {
2927 if (testprg[i] == '/')
2930 testprogfile = testprg;
2932 /* Quote the name that gets put into shell command lines. */
2933 testprg = malloc(strlen(testprogfile) + 3);
2934 strcpy(testprg, "\"");
2935 strcat(testprg, testprogfile);
2936 strcat(testprg, "\"");
2941 #if !defined(_WIN32) && defined(SIGPIPE)
2942 { /* Ignore SIGPIPE signals */
2943 struct sigaction sa;
2944 sa.sa_handler = SIG_IGN;
2945 sigemptyset(&sa.sa_mask);
2947 sigaction(SIGPIPE, &sa, NULL);
2952 * Create a temp directory for the following tests.
2953 * Include the time the tests started as part of the name,
2954 * to make it easier to track the results of multiple tests.
2957 for (i = 0; ; i++) {
2958 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2959 "%Y-%m-%dT%H.%M.%S",
2961 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2962 tmpdir_timestamp, i);
2963 if (assertMakeDir(tmpdir,0755))
2967 "ERROR: Unable to create temp directory %s\n",
2974 * If the user didn't specify a directory for locating
2975 * reference files, try to find the reference files in
2976 * the "usual places."
2978 refdir = refdir_alloc = get_refdir(refdir);
2981 * Banner with basic information.
2984 printf("If tests fail or crash, details will be in:\n");
2985 printf(" %s\n", tmpdir);
2987 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2988 printf("Reference files will be read from: %s\n", refdir);
2990 printf("Running tests on: %s\n", testprog);
2992 printf("Exercising: ");
2994 printf("%s\n", EXTRA_VERSION);
3001 * Run some or all of the individual tests.
3009 test_num = get_test_set(test_set, limit, *argv, tests);
3011 printf("*** INVALID Test %s\n", *argv);
3017 for (i = 0; i < test_num; i++) {
3019 if (test_run(test_set[i], tmpdir)) {
3027 } while (*argv != NULL);
3028 } while (until_failure);
3031 /* Must be freed after all tests run */
3037 * Report summary statistics.
3039 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3041 printf("Totals:\n");
3042 printf(" Tests run: %8d\n", tests_run);
3043 printf(" Tests failed: %8d\n", tests_failed);
3044 printf(" Assertions checked:%8d\n", assertions);
3045 printf(" Assertions failed: %8d\n", failures);
3046 printf(" Skips reported: %8d\n", skips);
3050 printf("Failing tests:\n");
3051 for (i = 0; i < limit; ++i) {
3052 if (tests[i].failures)
3053 printf(" %d: %s (%d failures)\n", i,
3054 tests[i].name, tests[i].failures);
3057 printf("Details for failing tests: %s\n", tmpdir);
3060 if (verbosity == VERBOSITY_SUMMARY_ONLY)
3062 printf("%d tests passed, no failures\n", tests_run);
3067 /* If the final tmpdir is empty, we can remove it. */
3068 /* This should be the usual case when all tests succeed. */
3072 return (tests_failed ? 1 : 0);