2 * Copyright (c) 2003-2009 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "test_utils.h"
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
31 #ifdef HAVE_SYS_TIME_H
39 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
40 * As the include guards don't agree, the order of include is important.
42 #ifdef HAVE_LINUX_EXT2_FS_H
43 #include <linux/ext2_fs.h> /* for Linux file flags */
45 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
46 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
57 * This same file is used pretty much verbatim for all test harnesses.
59 * The next few lines are the only differences.
60 * TODO: Move this into a separate configuration header, have all test
61 * suites share one copy of this file.
63 #define KNOWNREF "test_expand.Z.uu"
64 #define ENVBASE "BSDCAT" /* Prefix for environment variables. */
65 #define PROGRAM "bsdcat" /* Name of program being tested. */
66 #define PROGRAM_ALIAS "cat" /* Generic alias for program */
67 #undef LIBRARY /* Not testing a library. */
68 #undef EXTRA_DUMP /* How to dump extra data */
69 #undef EXTRA_ERRNO /* How to dump errno */
70 /* How to generate extra version info. */
71 #define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "")
75 * Windows support routines
77 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
78 * in the test harness is dangerous because they cover up
79 * configuration errors. The classic example of this is omitting a
80 * configure check. If libarchive and libarchive_test both look for
81 * the same feature macro, such errors are hard to detect. Platform
82 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
83 * easily lead to very messy code. It's best to limit yourself
84 * to only the most generic programming techniques in the test harness
85 * and thus avoid conditionals altogether. Where that's not possible,
86 * try to minimize conditionals by grouping platform-specific tests in
87 * one place (e.g., test_acl_freebsd) or by adding new assert()
88 * functions (e.g., assertMakeHardlink()) to cover up platform
89 * differences. Platform-specific coding in libarchive_test is often
90 * a symptom that some capability is missing from libarchive itself.
92 #if defined(_WIN32) && !defined(__CYGWIN__)
100 #define S_ISDIR(m) ((m) & _S_IFDIR)
103 #define S_ISREG(m) ((m) & _S_IFREG)
105 #if !defined(__BORLANDC__)
106 #define access _access
111 #define fileno _fileno
113 /*#define fstat _fstat64*/
114 #if !defined(__BORLANDC__)
115 #define getcwd _getcwd
118 /*#define lstat _stat64*/
119 /*#define stat _stat64*/
121 #if !defined(__BORLANDC__)
122 #define strdup _strdup
125 #define int64_t __int64
128 #if defined(HAVE__CrtSetReportMode)
132 mode_t umasked(mode_t expected_mode)
134 mode_t mode = umask(0);
136 return expected_mode & ~mode;
139 /* Path to working directory for current test */
140 const char *testworkdir;
142 /* Pathname of exe to be tested. */
143 const char *testprogfile;
144 /* Name of exe to use in printf-formatted command strings. */
145 /* On Windows, this includes leading/trailing quotes. */
146 const char *testprog;
149 #if defined(_WIN32) && !defined(__CYGWIN__)
150 static void *GetFunctionKernel32(const char *);
151 static int my_CreateSymbolicLinkA(const char *, const char *, int);
152 static int my_CreateHardLinkA(const char *, const char *);
153 static int my_GetFileInformationByName(const char *,
154 BY_HANDLE_FILE_INFORMATION *);
157 GetFunctionKernel32(const char *name)
159 static HINSTANCE lib;
163 lib = LoadLibrary("kernel32.dll");
166 fprintf(stderr, "Can't load kernel32.dll?!\n");
169 return (void *)GetProcAddress(lib, name);
173 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
175 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
179 f = GetFunctionKernel32("CreateSymbolicLinkA");
181 return f == NULL ? 0 : (*f)(linkname, target, flags);
185 my_CreateHardLinkA(const char *linkname, const char *target)
187 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
191 f = GetFunctionKernel32("CreateHardLinkA");
193 return f == NULL ? 0 : (*f)(linkname, target, NULL);
197 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
202 memset(bhfi, 0, sizeof(*bhfi));
203 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
204 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
205 if (h == INVALID_HANDLE_VALUE)
207 r = GetFileInformationByHandle(h, bhfi);
213 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
215 invalid_parameter_handler(const wchar_t * expression,
216 const wchar_t * function, const wchar_t * file,
217 unsigned int line, uintptr_t pReserved)
229 /* Enable core dump on failure. */
230 static int dump_on_failure = 0;
231 /* Default is to remove temp dirs and log data for successful tests. */
232 static int keep_temp_files = 0;
233 /* Default is to run the specified tests once and report errors. */
234 static int until_failure = 0;
235 /* Default is to just report pass/fail for each test. */
236 static int verbosity = 0;
237 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
238 #define VERBOSITY_PASSFAIL 0 /* Default */
239 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
240 #define VERBOSITY_FULL 2 /* -vv */
241 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
242 * mostly for debugging the test harness itself. */
243 /* Cumulative count of assertion failures. */
244 static int failures = 0;
245 /* Cumulative count of reported skips. */
246 static int skips = 0;
247 /* Cumulative count of assertions checked. */
248 static int assertions = 0;
250 /* Directory where uuencoded reference files can be found. */
251 static const char *refdir;
254 * Report log information selectively to console and/or disk log.
256 static int log_console = 0;
257 static FILE *logfile;
259 vlogprintf(const char *fmt, va_list ap)
266 vfprintf(stdout, fmt, ap);
269 vfprintf(logfile, fmt, lfap);
272 vfprintf(logfile, fmt, ap);
277 logprintf(const char *fmt, ...)
285 /* Set up a message to display only if next assertion fails. */
286 static char msgbuff[4096];
287 static const char *msg, *nextmsg;
289 failure(const char *fmt, ...)
296 vsprintf(msgbuff, fmt, ap);
303 * Copy arguments into file-local variables.
304 * This was added to permit vararg assert() functions without needing
305 * variadic wrapper macros. Turns out that the vararg capability is almost
306 * never used, so almost all of the vararg assertions can be simplified
307 * by removing the vararg capability and reworking the wrapper macro to
308 * pass __FILE__, __LINE__ directly into the function instead of using
309 * this hook. I suspect this machinery is used so rarely that we
310 * would be better off just removing it entirely. That would simplify
311 * the code here noticeably.
313 static const char *skipping_filename;
314 static int skipping_line;
315 void skipping_setup(const char *filename, int line)
317 skipping_filename = filename;
318 skipping_line = line;
321 /* Called at the beginning of each assert() function. */
323 assertion_count(const char *file, int line)
325 (void)file; /* UNUSED */
326 (void)line; /* UNUSED */
328 /* Proper handling of "failure()" message. */
331 /* Uncomment to print file:line after every assertion.
332 * Verbose, but occasionally useful in tracking down crashes. */
333 /* printf("Checked %s:%d\n", file, line); */
337 * For each test source file, we remember how many times each
338 * assertion was reported. Cleared before each new test,
339 * used by test_summarize().
344 } failed_lines[10000];
345 const char *failed_filename;
347 /* Count this failure, setup up log destination and handle initial report. */
349 failure_start(const char *filename, int line, const char *fmt, ...)
353 /* Record another failure for this line. */
355 failed_filename = filename;
356 failed_lines[line].count++;
358 /* Determine whether to log header to console. */
360 case VERBOSITY_LIGHT_REPORT:
361 log_console = (failed_lines[line].count < 2);
364 log_console = (verbosity >= VERBOSITY_FULL);
367 /* Log file:line header for this failure */
370 logprintf("%s(%d): ", filename, line);
372 logprintf("%s:%d: ", filename, line);
378 if (msg != NULL && msg[0] != '\0') {
379 logprintf(" Description: %s\n", msg);
383 /* Determine whether to log details to console. */
384 if (verbosity == VERBOSITY_LIGHT_REPORT)
388 /* Complete reporting of failed tests. */
390 * The 'extra' hook here is used by libarchive to include libarchive
391 * error messages with assertion failures. It could also be used
392 * to add strerror() output, for example. Just define the EXTRA_DUMP()
393 * macro appropriately.
396 failure_finish(void *extra)
398 (void)extra; /* UNUSED (maybe) */
401 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
402 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
406 if (dump_on_failure) {
408 " *** forcing core dump so failure can be debugged ***\n");
413 /* Inform user that we're skipping some checks. */
415 test_skipping(const char *fmt, ...)
421 vsprintf(buff, fmt, ap);
423 /* Use failure() message if set. */
426 /* failure_start() isn't quite right, but is awfully convenient. */
427 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
428 --failures; /* Undo failures++ in failure_start() */
429 /* Don't failure_finish() here. */
430 /* Mark as skip, so doesn't count as failed test. */
431 failed_lines[skipping_line].skip = 1;
441 /* Generic assert() just displays the failed condition. */
443 assertion_assert(const char *file, int line, int value,
444 const char *condition, void *extra)
446 assertion_count(file, line);
448 failure_start(file, line, "Assertion failed: %s", condition);
449 failure_finish(extra);
454 /* chdir() and report any errors */
456 assertion_chdir(const char *file, int line, const char *pathname)
458 assertion_count(file, line);
459 if (chdir(pathname) == 0)
461 failure_start(file, line, "chdir(\"%s\")", pathname);
462 failure_finish(NULL);
467 /* Verify two integers are equal. */
469 assertion_equal_int(const char *file, int line,
470 long long v1, const char *e1, long long v2, const char *e2, void *extra)
472 assertion_count(file, line);
475 failure_start(file, line, "%s != %s", e1, e2);
476 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
477 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
478 failure_finish(extra);
483 * Utility to convert a single UTF-8 sequence.
486 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
488 static const char utf8_count[256] = {
489 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
490 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
491 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
492 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
493 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
494 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
495 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
496 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
497 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
498 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
499 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
500 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
501 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
502 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
503 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
504 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
516 * Decode 1-4 bytes depending on the value of the first byte.
518 ch = (unsigned char)*s;
520 return (0); /* Standard: return 0 for end-of-string. */
521 cnt = utf8_count[ch];
523 /* Invalid sequence or there are not plenty bytes. */
527 /* Make a Unicode code point from a single UTF-8 sequence. */
529 case 1: /* 1 byte sequence. */
532 case 2: /* 2 bytes sequence. */
533 if ((s[1] & 0xc0) != 0x80) return (-1);
534 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
536 case 3: /* 3 bytes sequence. */
537 if ((s[1] & 0xc0) != 0x80) return (-1);
538 if ((s[2] & 0xc0) != 0x80) return (-1);
539 wc = ((ch & 0x0f) << 12)
540 | ((s[1] & 0x3f) << 6)
543 return (-1);/* Overlong sequence. */
545 case 4: /* 4 bytes sequence. */
548 if ((s[1] & 0xc0) != 0x80) return (-1);
549 if ((s[2] & 0xc0) != 0x80) return (-1);
550 if ((s[3] & 0xc0) != 0x80) return (-1);
551 wc = ((ch & 0x07) << 18)
552 | ((s[1] & 0x3f) << 12)
553 | ((s[2] & 0x3f) << 6)
556 return (-1);/* Overlong sequence. */
562 /* The code point larger than 0x10FFFF is not legal
566 /* Correctly gets a Unicode, returns used bytes. */
571 static void strdump(const char *e, const char *p, int ewidth, int utf8)
575 logprintf(" %*s = ", ewidth, e);
582 unsigned int c = 0xff & *p++;
584 case '\a': logprintf("\\a"); break;
585 case '\b': logprintf("\\b"); break;
586 case '\n': logprintf("\\n"); break;
587 case '\r': logprintf("\\r"); break;
589 if (c >= 32 && c < 127)
592 logprintf("\\x%02X", c);
596 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
599 * If the current string is UTF-8, dump its code points.
610 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
613 logprintf("%04X", uc);
619 logprintf(" (count %d", cnt);
621 logprintf(",unknown %d bytes", len);
629 /* Verify two strings are equal, dump them if not. */
631 assertion_equal_string(const char *file, int line,
632 const char *v1, const char *e1,
633 const char *v2, const char *e2,
634 void *extra, int utf8)
638 assertion_count(file, line);
639 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
641 failure_start(file, line, "%s != %s", e1, e2);
642 l1 = (int)strlen(e1);
643 l2 = (int)strlen(e2);
646 strdump(e1, v1, l1, utf8);
647 strdump(e2, v2, l1, utf8);
648 failure_finish(extra);
653 wcsdump(const char *e, const wchar_t *w)
655 logprintf(" %s = ", e);
661 while (*w != L'\0') {
662 unsigned int c = *w++;
663 if (c >= 32 && c < 127)
666 logprintf("\\x%02X", c);
667 else if (c < 0x10000)
668 logprintf("\\u%04X", c);
670 logprintf("\\U%08X", c);
677 wcscmp(const wchar_t *s1, const wchar_t *s2)
680 while (*s1 == *s2++) {
691 /* Verify that two wide strings are equal, dump them if not. */
693 assertion_equal_wstring(const char *file, int line,
694 const wchar_t *v1, const char *e1,
695 const wchar_t *v2, const char *e2,
698 assertion_count(file, line);
701 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
703 failure_start(file, line, "%s != %s", e1, e2);
706 failure_finish(extra);
711 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
712 * any bytes in p that differ from ref will be highlighted with '_'
713 * before and after the hex value.
716 hexdump(const char *p, const char *ref, size_t l, size_t offset)
722 logprintf("(null)\n");
725 for(i=0; i < l; i+=16) {
726 logprintf("%04x", (unsigned)(i + offset));
728 for (j = 0; j < 16 && i + j < l; j++) {
729 if (ref != NULL && p[i + j] != ref[i + j])
731 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
732 if (ref != NULL && p[i + j] == ref[i + j])
735 for (; j < 16; j++) {
736 logprintf("%c ", sep);
739 logprintf("%c", sep);
740 for (j=0; j < 16 && i + j < l; j++) {
742 if (c >= ' ' && c <= 126)
751 /* Verify that two blocks of memory are the same, display the first
752 * block of differences if they're not. */
754 assertion_equal_mem(const char *file, int line,
755 const void *_v1, const char *e1,
756 const void *_v2, const char *e2,
757 size_t l, const char *ld, void *extra)
759 const char *v1 = (const char *)_v1;
760 const char *v2 = (const char *)_v2;
763 assertion_count(file, line);
764 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
766 if (v1 == NULL || v2 == NULL)
769 failure_start(file, line, "%s != %s", e1, e2);
770 logprintf(" size %s = %d\n", ld, (int)l);
771 /* Dump 48 bytes (3 lines) so that the first difference is
772 * in the second line. */
774 while (l > 64 && memcmp(v1, v2, 32) == 0) {
775 /* Two lines agree, so step forward one line. */
781 logprintf(" Dump of %s\n", e1);
782 hexdump(v1, v2, l < 128 ? l : 128, offset);
783 logprintf(" Dump of %s\n", e2);
784 hexdump(v2, v1, l < 128 ? l : 128, offset);
786 failure_finish(extra);
790 /* Verify that a block of memory is filled with the specified byte. */
792 assertion_memory_filled_with(const char *file, int line,
793 const void *_v1, const char *vd,
794 size_t l, const char *ld,
795 char b, const char *bd, void *extra)
797 const char *v1 = (const char *)_v1;
800 (void)ld; /* UNUSED */
802 assertion_count(file, line);
804 for (i = 0; i < l; ++i) {
812 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
813 logprintf(" Only %d bytes were correct\n", (int)c);
814 failure_finish(extra);
818 /* Verify that the named file exists and is empty. */
820 assertion_empty_file(const char *filename, int line, const char *f1)
827 assertion_count(filename, line);
829 if (stat(f1, &st) != 0) {
830 failure_start(filename, line, "Stat failed: %s", f1);
831 failure_finish(NULL);
837 failure_start(filename, line, "File should be empty: %s", f1);
838 logprintf(" File size: %d\n", (int)st.st_size);
839 logprintf(" Contents:\n");
842 logprintf(" Unable to open %s\n", f1);
844 s = ((off_t)sizeof(buff) < st.st_size) ?
845 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
846 s = fread(buff, 1, s, f);
847 hexdump(buff, NULL, s, 0);
850 failure_finish(NULL);
854 /* Verify that the named file exists and is not empty. */
856 assertion_non_empty_file(const char *filename, int line, const char *f1)
860 assertion_count(filename, line);
862 if (stat(f1, &st) != 0) {
863 failure_start(filename, line, "Stat failed: %s", f1);
864 failure_finish(NULL);
867 if (st.st_size == 0) {
868 failure_start(filename, line, "File empty: %s", f1);
869 failure_finish(NULL);
875 /* Verify that two files have the same contents. */
876 /* TODO: hexdump the first bytes that actually differ. */
878 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
885 assertion_count(filename, line);
887 f1 = fopen(fn1, "rb");
888 f2 = fopen(fn2, "rb");
889 if (f1 == NULL || f2 == NULL) {
895 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
896 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
899 if (n1 == 0 && n2 == 0) {
904 if (memcmp(buff1, buff2, n1) != 0)
909 failure_start(filename, line, "Files not identical");
910 logprintf(" file1=\"%s\"\n", fn1);
911 logprintf(" file2=\"%s\"\n", fn2);
912 failure_finish(NULL);
916 /* Verify that the named file does exist. */
918 assertion_file_exists(const char *filename, int line, const char *f)
920 assertion_count(filename, line);
922 #if defined(_WIN32) && !defined(__CYGWIN__)
926 if (!access(f, F_OK))
929 failure_start(filename, line, "File should exist: %s", f);
930 failure_finish(NULL);
934 /* Verify that the named file doesn't exist. */
936 assertion_file_not_exists(const char *filename, int line, const char *f)
938 assertion_count(filename, line);
940 #if defined(_WIN32) && !defined(__CYGWIN__)
947 failure_start(filename, line, "File should not exist: %s", f);
948 failure_finish(NULL);
952 /* Compare the contents of a file to a block of memory. */
954 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
960 assertion_count(filename, line);
964 failure_start(filename, line,
965 "File should exist: %s", fn);
966 failure_finish(NULL);
969 contents = malloc(s * 2);
970 n = (int)fread(contents, 1, s * 2, f);
972 if (n == s && memcmp(buff, contents, s) == 0) {
976 failure_start(filename, line, "File contents don't match");
977 logprintf(" file=\"%s\"\n", fn);
979 hexdump(contents, buff, n > 512 ? 512 : n, 0);
981 logprintf(" File empty, contents should be:\n");
982 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
984 failure_finish(NULL);
989 /* Check the contents of a text file, being tolerant of line endings. */
991 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
994 const char *btxt, *ftxt;
998 assertion_count(filename, line);
1001 failure_start(filename, line,
1002 "File doesn't exist: %s", fn);
1003 failure_finish(NULL);
1006 s = (int)strlen(buff);
1007 contents = malloc(s * 2 + 128);
1008 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1012 /* Compare texts. */
1014 ftxt = (const char *)contents;
1015 while (*btxt != '\0' && *ftxt != '\0') {
1016 if (*btxt == *ftxt) {
1021 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1022 /* Pass over different new line characters. */
1029 if (*btxt == '\0' && *ftxt == '\0') {
1033 failure_start(filename, line, "Contents don't match");
1034 logprintf(" file=\"%s\"\n", fn);
1036 hexdump(contents, buff, n, 0);
1037 logprintf(" expected\n", fn);
1038 hexdump(buff, contents, s, 0);
1040 logprintf(" File empty, contents should be:\n");
1041 hexdump(buff, NULL, s, 0);
1043 failure_finish(NULL);
1048 /* Verify that a text file contains the specified lines, regardless of order */
1049 /* This could be more efficient if we sorted both sets of lines, etc, but
1050 * since this is used only for testing and only ever deals with a dozen or so
1051 * lines at a time, this relatively crude approach is just fine. */
1053 assertion_file_contains_lines_any_order(const char *file, int line,
1054 const char *pathname, const char *lines[])
1058 size_t expected_count, actual_count, i, j;
1059 char **expected = NULL;
1060 char *p, **actual = NULL;
1062 int expected_failure = 0, actual_failure = 0;
1064 assertion_count(file, line);
1066 buff = slurpfile(&buff_size, "%s", pathname);
1068 failure_start(pathname, line, "Can't read file: %s", pathname);
1069 failure_finish(NULL);
1073 /* Make a copy of the provided lines and count up the expected
1075 for (i = 0; lines[i] != NULL; ++i) {
1078 if (expected_count) {
1079 expected = malloc(sizeof(char *) * expected_count);
1080 if (expected == NULL) {
1081 failure_start(pathname, line, "Can't allocate memory");
1082 failure_finish(NULL);
1086 for (i = 0; lines[i] != NULL; ++i) {
1087 expected[i] = strdup(lines[i]);
1091 /* Break the file into lines */
1093 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1094 if (*p == '\x0d' || *p == '\x0a')
1096 if (c == '\0' && *p != '\0')
1101 actual = calloc(sizeof(char *), actual_count);
1102 if (actual == NULL) {
1103 failure_start(pathname, line, "Can't allocate memory");
1104 failure_finish(NULL);
1108 for (j = 0, p = buff; p < buff + buff_size;
1109 p += 1 + strlen(p)) {
1117 /* Erase matching lines from both lists */
1118 for (i = 0; i < expected_count; ++i) {
1119 if (expected[i] == NULL)
1121 for (j = 0; j < actual_count; ++j) {
1122 if (actual[j] == NULL)
1124 if (strcmp(expected[i], actual[j]) == 0) {
1133 /* If there's anything left, it's a failure */
1134 for (i = 0; i < expected_count; ++i) {
1135 if (expected[i] != NULL)
1138 for (j = 0; j < actual_count; ++j) {
1139 if (actual[j] != NULL)
1142 if (expected_failure == 0 && actual_failure == 0) {
1148 failure_start(file, line, "File doesn't match: %s", pathname);
1149 for (i = 0; i < expected_count; ++i) {
1150 if (expected[i] != NULL) {
1151 logprintf(" Expected but not present: %s\n", expected[i]);
1155 for (j = 0; j < actual_count; ++j) {
1156 if (actual[j] != NULL)
1157 logprintf(" Present but not expected: %s\n", actual[j]);
1159 failure_finish(NULL);
1166 /* Verify that a text file does not contains the specified strings */
1168 assertion_file_contains_no_invalid_strings(const char *file, int line,
1169 const char *pathname, const char *strings[])
1174 buff = slurpfile(NULL, "%s", pathname);
1176 failure_start(file, line, "Can't read file: %s", pathname);
1177 failure_finish(NULL);
1181 for (i = 0; strings[i] != NULL; ++i) {
1182 if (strstr(buff, strings[i]) != NULL) {
1183 failure_start(file, line, "Invalid string in %s: %s", pathname,
1185 failure_finish(NULL);
1195 /* Test that two paths point to the same file. */
1196 /* As a side-effect, asserts that both files exist. */
1198 is_hardlink(const char *file, int line,
1199 const char *path1, const char *path2)
1201 #if defined(_WIN32) && !defined(__CYGWIN__)
1202 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1205 assertion_count(file, line);
1206 r = my_GetFileInformationByName(path1, &bhfi1);
1208 failure_start(file, line, "File %s can't be inspected?", path1);
1209 failure_finish(NULL);
1212 r = my_GetFileInformationByName(path2, &bhfi2);
1214 failure_start(file, line, "File %s can't be inspected?", path2);
1215 failure_finish(NULL);
1218 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1219 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1220 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1222 struct stat st1, st2;
1225 assertion_count(file, line);
1226 r = lstat(path1, &st1);
1228 failure_start(file, line, "File should exist: %s", path1);
1229 failure_finish(NULL);
1232 r = lstat(path2, &st2);
1234 failure_start(file, line, "File should exist: %s", path2);
1235 failure_finish(NULL);
1238 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1243 assertion_is_hardlink(const char *file, int line,
1244 const char *path1, const char *path2)
1246 if (is_hardlink(file, line, path1, path2))
1248 failure_start(file, line,
1249 "Files %s and %s are not hardlinked", path1, path2);
1250 failure_finish(NULL);
1255 assertion_is_not_hardlink(const char *file, int line,
1256 const char *path1, const char *path2)
1258 if (!is_hardlink(file, line, path1, path2))
1260 failure_start(file, line,
1261 "Files %s and %s should not be hardlinked", path1, path2);
1262 failure_finish(NULL);
1266 /* Verify a/b/mtime of 'pathname'. */
1267 /* If 'recent', verify that it's within last 10 seconds. */
1269 assertion_file_time(const char *file, int line,
1270 const char *pathname, long t, long nsec, char type, int recent)
1272 long long filet, filet_nsec;
1275 #if defined(_WIN32) && !defined(__CYGWIN__)
1276 #define EPOC_TIME (116444736000000000ULL)
1277 FILETIME fxtime, fbirthtime, fatime, fmtime;
1278 ULARGE_INTEGER wintm;
1280 fxtime.dwLowDateTime = 0;
1281 fxtime.dwHighDateTime = 0;
1283 assertion_count(file, line);
1284 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1285 * a directory file. If not, CreateFile() will fail when
1286 * the pathname is a directory. */
1287 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1288 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1289 if (h == INVALID_HANDLE_VALUE) {
1290 failure_start(file, line, "Can't access %s\n", pathname);
1291 failure_finish(NULL);
1294 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1296 case 'a': fxtime = fatime; break;
1297 case 'b': fxtime = fbirthtime; break;
1298 case 'm': fxtime = fmtime; break;
1302 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1303 failure_finish(NULL);
1306 wintm.LowPart = fxtime.dwLowDateTime;
1307 wintm.HighPart = fxtime.dwHighDateTime;
1308 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1309 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1310 nsec = (nsec / 100) * 100; /* Round the request */
1314 assertion_count(file, line);
1315 r = lstat(pathname, &st);
1317 failure_start(file, line, "Can't stat %s\n", pathname);
1318 failure_finish(NULL);
1322 case 'a': filet = st.st_atime; break;
1323 case 'm': filet = st.st_mtime; break;
1324 case 'b': filet = 0; break;
1325 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1328 #if defined(__FreeBSD__)
1330 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1331 case 'b': filet = st.st_birthtime;
1332 /* FreeBSD filesystems that don't support birthtime
1333 * (e.g., UFS1) always return -1 here. */
1337 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1338 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1339 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1342 /* FreeBSD generally only stores to microsecond res, so round. */
1343 filet_nsec = (filet_nsec / 1000) * 1000;
1344 nsec = (nsec / 1000) * 1000;
1346 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1347 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1348 #if defined(__HAIKU__)
1349 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1354 /* Check that requested time is up-to-date. */
1355 time_t now = time(NULL);
1356 if (filet < now - 10 || filet > now + 1) {
1357 failure_start(file, line,
1358 "File %s has %ctime %lld, %lld seconds ago\n",
1359 pathname, type, filet, now - filet);
1360 failure_finish(NULL);
1363 } else if (filet != t || filet_nsec != nsec) {
1364 failure_start(file, line,
1365 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1366 pathname, type, filet, filet_nsec, t, nsec);
1367 failure_finish(NULL);
1373 /* Verify atime of 'pathname'. */
1375 assertion_file_atime(const char *file, int line,
1376 const char *pathname, long t, long nsec)
1378 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1381 /* Verify atime of 'pathname' is up-to-date. */
1383 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1385 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1388 /* Verify birthtime of 'pathname'. */
1390 assertion_file_birthtime(const char *file, int line,
1391 const char *pathname, long t, long nsec)
1393 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1396 /* Verify birthtime of 'pathname' is up-to-date. */
1398 assertion_file_birthtime_recent(const char *file, int line,
1399 const char *pathname)
1401 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1404 /* Verify mode of 'pathname'. */
1406 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1411 assertion_count(file, line);
1412 #if defined(_WIN32) && !defined(__CYGWIN__)
1413 failure_start(file, line, "assertFileMode not yet implemented for Windows");
1414 (void)mode; /* UNUSED */
1415 (void)r; /* UNUSED */
1419 r = lstat(pathname, &st);
1420 mode = (int)(st.st_mode & 0777);
1422 if (r == 0 && mode == expected_mode)
1424 failure_start(file, line, "File %s has mode %o, expected %o",
1425 pathname, mode, expected_mode);
1427 failure_finish(NULL);
1431 /* Verify mtime of 'pathname'. */
1433 assertion_file_mtime(const char *file, int line,
1434 const char *pathname, long t, long nsec)
1436 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1439 /* Verify mtime of 'pathname' is up-to-date. */
1441 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1443 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1446 /* Verify number of links to 'pathname'. */
1448 assertion_file_nlinks(const char *file, int line,
1449 const char *pathname, int nlinks)
1451 #if defined(_WIN32) && !defined(__CYGWIN__)
1452 BY_HANDLE_FILE_INFORMATION bhfi;
1455 assertion_count(file, line);
1456 r = my_GetFileInformationByName(pathname, &bhfi);
1457 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1459 failure_start(file, line, "File %s has %d links, expected %d",
1460 pathname, bhfi.nNumberOfLinks, nlinks);
1461 failure_finish(NULL);
1467 assertion_count(file, line);
1468 r = lstat(pathname, &st);
1469 if (r == 0 && (int)st.st_nlink == nlinks)
1471 failure_start(file, line, "File %s has %d links, expected %d",
1472 pathname, st.st_nlink, nlinks);
1473 failure_finish(NULL);
1478 /* Verify size of 'pathname'. */
1480 assertion_file_size(const char *file, int line, const char *pathname, long size)
1485 assertion_count(file, line);
1486 #if defined(_WIN32) && !defined(__CYGWIN__)
1488 BY_HANDLE_FILE_INFORMATION bhfi;
1489 r = !my_GetFileInformationByName(pathname, &bhfi);
1490 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1495 r = lstat(pathname, &st);
1496 filesize = st.st_size;
1499 if (r == 0 && filesize == size)
1501 failure_start(file, line, "File %s has size %ld, expected %ld",
1502 pathname, (long)filesize, (long)size);
1503 failure_finish(NULL);
1507 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1509 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1514 #if defined(_WIN32) && !defined(__CYGWIN__)
1515 (void)mode; /* UNUSED */
1517 assertion_count(file, line);
1518 r = lstat(pathname, &st);
1520 failure_start(file, line, "Dir should exist: %s", pathname);
1521 failure_finish(NULL);
1524 if (!S_ISDIR(st.st_mode)) {
1525 failure_start(file, line, "%s is not a dir", pathname);
1526 failure_finish(NULL);
1529 #if !defined(_WIN32) || defined(__CYGWIN__)
1530 /* Windows doesn't handle permissions the same way as POSIX,
1531 * so just ignore the mode tests. */
1532 /* TODO: Can we do better here? */
1533 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1534 failure_start(file, line, "Dir %s has wrong mode", pathname);
1535 logprintf(" Expected: 0%3o\n", mode);
1536 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1537 failure_finish(NULL);
1544 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1545 * verify that too. */
1547 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1552 #if defined(_WIN32) && !defined(__CYGWIN__)
1553 (void)mode; /* UNUSED */
1555 assertion_count(file, line);
1556 r = lstat(pathname, &st);
1557 if (r != 0 || !S_ISREG(st.st_mode)) {
1558 failure_start(file, line, "File should exist: %s", pathname);
1559 failure_finish(NULL);
1562 #if !defined(_WIN32) || defined(__CYGWIN__)
1563 /* Windows doesn't handle permissions the same way as POSIX,
1564 * so just ignore the mode tests. */
1565 /* TODO: Can we do better here? */
1566 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1567 failure_start(file, line, "File %s has wrong mode", pathname);
1568 logprintf(" Expected: 0%3o\n", mode);
1569 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1570 failure_finish(NULL);
1577 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1578 * non-NULL, verify that the symlink has those contents. */
1580 is_symlink(const char *file, int line,
1581 const char *pathname, const char *contents)
1583 #if defined(_WIN32) && !defined(__CYGWIN__)
1584 (void)pathname; /* UNUSED */
1585 (void)contents; /* UNUSED */
1586 assertion_count(file, line);
1587 /* Windows sort-of has real symlinks, but they're only usable
1588 * by privileged users and are crippled even then, so there's
1589 * really not much point in bothering with this. */
1597 assertion_count(file, line);
1598 r = lstat(pathname, &st);
1600 failure_start(file, line,
1601 "Symlink should exist: %s", pathname);
1602 failure_finish(NULL);
1605 if (!S_ISLNK(st.st_mode))
1607 if (contents == NULL)
1609 linklen = readlink(pathname, buff, sizeof(buff));
1611 failure_start(file, line, "Can't read symlink %s", pathname);
1612 failure_finish(NULL);
1615 buff[linklen] = '\0';
1616 if (strcmp(buff, contents) != 0)
1622 /* Assert that path is a symlink that (optionally) contains contents. */
1624 assertion_is_symlink(const char *file, int line,
1625 const char *path, const char *contents)
1627 if (is_symlink(file, line, path, contents))
1630 failure_start(file, line, "File %s is not a symlink to %s",
1633 failure_start(file, line, "File %s is not a symlink", path);
1634 failure_finish(NULL);
1639 /* Create a directory and report any errors. */
1641 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1643 assertion_count(file, line);
1644 #if defined(_WIN32) && !defined(__CYGWIN__)
1645 (void)mode; /* UNUSED */
1646 if (0 == _mkdir(dirname))
1649 if (0 == mkdir(dirname, mode)) {
1650 if (0 == chmod(dirname, mode)) {
1651 assertion_file_mode(file, line, dirname, mode);
1656 failure_start(file, line, "Could not create directory %s", dirname);
1657 failure_finish(NULL);
1661 /* Create a file with the specified contents and report any failures. */
1663 assertion_make_file(const char *file, int line,
1664 const char *path, int mode, int csize, const void *contents)
1666 #if defined(_WIN32) && !defined(__CYGWIN__)
1667 /* TODO: Rework this to set file mode as well. */
1669 (void)mode; /* UNUSED */
1670 assertion_count(file, line);
1671 f = fopen(path, "wb");
1673 failure_start(file, line, "Could not create file %s", path);
1674 failure_finish(NULL);
1677 if (contents != NULL) {
1681 wsize = strlen(contents);
1683 wsize = (size_t)csize;
1684 if (wsize != fwrite(contents, 1, wsize, f)) {
1686 failure_start(file, line,
1687 "Could not write file %s", path);
1688 failure_finish(NULL);
1696 assertion_count(file, line);
1697 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1699 failure_start(file, line, "Could not create %s", path);
1700 failure_finish(NULL);
1703 if (0 != chmod(path, mode)) {
1704 failure_start(file, line, "Could not chmod %s", path);
1705 failure_finish(NULL);
1709 if (contents != NULL) {
1713 wsize = (ssize_t)strlen(contents);
1715 wsize = (ssize_t)csize;
1716 if (wsize != write(fd, contents, wsize)) {
1718 failure_start(file, line,
1719 "Could not write to %s", path);
1720 failure_finish(NULL);
1726 assertion_file_mode(file, line, path, mode);
1731 /* Create a hardlink and report any failures. */
1733 assertion_make_hardlink(const char *file, int line,
1734 const char *newpath, const char *linkto)
1738 assertion_count(file, line);
1739 #if defined(_WIN32) && !defined(__CYGWIN__)
1740 succeeded = my_CreateHardLinkA(newpath, linkto);
1742 succeeded = !link(linkto, newpath);
1748 failure_start(file, line, "Could not create hardlink");
1749 logprintf(" New link: %s\n", newpath);
1750 logprintf(" Old name: %s\n", linkto);
1751 failure_finish(NULL);
1755 /* Create a symlink and report any failures. */
1757 assertion_make_symlink(const char *file, int line,
1758 const char *newpath, const char *linkto)
1760 #if defined(_WIN32) && !defined(__CYGWIN__)
1761 int targetIsDir = 0; /* TODO: Fix this */
1762 assertion_count(file, line);
1763 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1766 assertion_count(file, line);
1767 if (0 == symlink(linkto, newpath))
1770 failure_start(file, line, "Could not create symlink");
1771 logprintf(" New link: %s\n", newpath);
1772 logprintf(" Old name: %s\n", linkto);
1773 failure_finish(NULL);
1777 /* Set umask, report failures. */
1779 assertion_umask(const char *file, int line, int mask)
1781 assertion_count(file, line);
1782 (void)file; /* UNUSED */
1783 (void)line; /* UNUSED */
1788 /* Set times, report failures. */
1790 assertion_utimes(const char *file, int line,
1791 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1795 #if defined(_WIN32) && !defined(__CYGWIN__)
1796 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1797 + (((nsec)/1000)*10))
1799 ULARGE_INTEGER wintm;
1800 FILETIME fatime, fmtime;
1801 FILETIME *pat, *pmt;
1803 assertion_count(file, line);
1804 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1805 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1806 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1807 if (h == INVALID_HANDLE_VALUE) {
1808 failure_start(file, line, "Can't access %s\n", pathname);
1809 failure_finish(NULL);
1813 if (at > 0 || at_nsec > 0) {
1814 wintm.QuadPart = WINTIME(at, at_nsec);
1815 fatime.dwLowDateTime = wintm.LowPart;
1816 fatime.dwHighDateTime = wintm.HighPart;
1820 if (mt > 0 || mt_nsec > 0) {
1821 wintm.QuadPart = WINTIME(mt, mt_nsec);
1822 fmtime.dwLowDateTime = wintm.LowPart;
1823 fmtime.dwHighDateTime = wintm.HighPart;
1827 if (pat != NULL || pmt != NULL)
1828 r = SetFileTime(h, NULL, pat, pmt);
1833 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1834 failure_finish(NULL);
1838 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1840 struct timeval times[2];
1842 #if !defined(__FreeBSD__)
1843 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1845 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1848 r = lstat(pathname, &st);
1850 failure_start(file, line, "Can't stat %s\n", pathname);
1851 failure_finish(NULL);
1855 if (mt == 0 && mt_nsec == 0) {
1857 #if defined(__FreeBSD__)
1858 mt_nsec = st.st_mtimespec.tv_nsec;
1859 /* FreeBSD generally only stores to microsecond res, so round. */
1860 mt_nsec = (mt_nsec / 1000) * 1000;
1863 if (at == 0 && at_nsec == 0) {
1865 #if defined(__FreeBSD__)
1866 at_nsec = st.st_atimespec.tv_nsec;
1867 /* FreeBSD generally only stores to microsecond res, so round. */
1868 at_nsec = (at_nsec / 1000) * 1000;
1872 times[1].tv_sec = mt;
1873 times[1].tv_usec = mt_nsec / 1000;
1875 times[0].tv_sec = at;
1876 times[0].tv_usec = at_nsec / 1000;
1879 r = lutimes(pathname, times);
1881 r = utimes(pathname, times);
1884 failure_start(file, line, "Can't utimes %s\n", pathname);
1885 failure_finish(NULL);
1889 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1892 /* Set nodump, report failures. */
1894 assertion_nodump(const char *file, int line, const char *pathname)
1896 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1899 assertion_count(file, line);
1900 r = chflags(pathname, UF_NODUMP);
1902 failure_start(file, line, "Can't set nodump %s\n", pathname);
1903 failure_finish(NULL);
1906 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1907 && defined(EXT2_NODUMP_FL)
1910 assertion_count(file, line);
1911 fd = open(pathname, O_RDONLY | O_NONBLOCK);
1913 failure_start(file, line, "Can't open %s\n", pathname);
1914 failure_finish(NULL);
1917 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1919 failure_start(file, line, "Can't get flags %s\n", pathname);
1920 failure_finish(NULL);
1923 flags |= EXT2_NODUMP_FL;
1924 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1926 failure_start(file, line, "Can't set nodump %s\n", pathname);
1927 failure_finish(NULL);
1932 (void)pathname; /* UNUSED */
1933 assertion_count(file, line);
1940 * UTILITIES for use by tests.
1945 * Check whether platform supports symlinks. This is intended
1946 * for tests to use in deciding whether to bother testing symlink
1947 * support; if the platform doesn't support symlinks, there's no point
1948 * in checking whether the program being tested can create them.
1950 * Note that the first time this test is called, we actually go out to
1951 * disk to create and verify a symlink. This is necessary because
1952 * symlink support is actually a property of a particular filesystem
1953 * and can thus vary between directories on a single system. After
1954 * the first call, this returns the cached result from memory, so it's
1955 * safe to call it as often as you wish.
1960 /* Remember the test result */
1961 static int value = 0, tested = 0;
1966 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1967 /* Note: Cygwin has its own symlink() emulation that does not
1968 * use the Win32 CreateSymbolicLink() function. */
1969 #if defined(_WIN32) && !defined(__CYGWIN__)
1970 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1971 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1973 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1974 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1979 /* Platform-dependent options for hiding the output of a subcommand. */
1980 #if defined(_WIN32) && !defined(__CYGWIN__)
1981 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1983 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1986 * Can this platform run the bzip2 program?
1991 static int tested = 0, value = 0;
1994 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
2001 * Can this platform run the grzip program?
2006 static int tested = 0, value = 0;
2009 if (systemf("grzip -V %s", redirectArgs) == 0)
2016 * Can this platform run the gzip program?
2021 static int tested = 0, value = 0;
2024 if (systemf("gzip -V %s", redirectArgs) == 0)
2031 * Can this platform run the lrzip program?
2034 canRunCommand(const char *cmd)
2036 static int tested = 0, value = 0;
2039 if (systemf("%s %s", cmd, redirectArgs) == 0)
2048 static int tested = 0, value = 0;
2051 if (systemf("lrzip -V %s", redirectArgs) == 0)
2058 * Can this platform run the lz4 program?
2063 static int tested = 0, value = 0;
2066 if (systemf("lz4 -V %s", redirectArgs) == 0)
2073 * Can this platform run the lzip program?
2078 static int tested = 0, value = 0;
2081 if (systemf("lzip -V %s", redirectArgs) == 0)
2088 * Can this platform run the lzma program?
2093 static int tested = 0, value = 0;
2096 if (systemf("lzma -V %s", redirectArgs) == 0)
2103 * Can this platform run the lzop program?
2108 static int tested = 0, value = 0;
2111 if (systemf("lzop -V %s", redirectArgs) == 0)
2118 * Can this platform run the xz program?
2123 static int tested = 0, value = 0;
2126 if (systemf("xz -V %s", redirectArgs) == 0)
2133 * Can this filesystem handle nodump flags.
2135 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2140 const char *path = "cannodumptest";
2143 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2144 if (chflags(path, UF_NODUMP) < 0)
2146 if (stat(path, &sb) < 0)
2148 if (sb.st_flags & UF_NODUMP)
2153 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2154 && defined(EXT2_NODUMP_FL)
2159 const char *path = "cannodumptest";
2162 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2163 fd = open(path, O_RDONLY | O_NONBLOCK);
2166 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2169 flags |= EXT2_NODUMP_FL;
2170 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2174 fd = open(path, O_RDONLY | O_NONBLOCK);
2177 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2181 if (flags & EXT2_NODUMP_FL)
2197 * Sleep as needed; useful for verifying disk timestamp changes by
2198 * ensuring that the wall-clock time has actually changed before we
2199 * go back to re-read something from disk.
2202 sleepUntilAfter(time_t t)
2204 while (t >= time(NULL))
2205 #if defined(_WIN32) && !defined(__CYGWIN__)
2213 * Call standard system() call, but build up the command line using
2214 * sprintf() conventions.
2217 systemf(const char *fmt, ...)
2224 vsprintf(buff, fmt, ap);
2225 if (verbosity > VERBOSITY_FULL)
2226 logprintf("Cmd: %s\n", buff);
2233 * Slurp a file into memory for ease of comparison and testing.
2234 * Returns size of file in 'sizep' if non-NULL, null-terminates
2235 * data in memory for ease of use.
2238 slurpfile(size_t * sizep, const char *fmt, ...)
2240 char filename[8192];
2249 vsprintf(filename, fmt, ap);
2252 f = fopen(filename, "rb");
2254 /* Note: No error; non-existent file is okay here. */
2257 r = fstat(fileno(f), &st);
2259 logprintf("Can't stat file %s\n", filename);
2263 p = malloc((size_t)st.st_size + 1);
2265 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2266 (long int)st.st_size, filename);
2270 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2271 if (bytes_read < st.st_size) {
2272 logprintf("Can't read file %s\n", filename);
2277 p[st.st_size] = '\0';
2279 *sizep = (size_t)st.st_size;
2285 * Slurp a file into memory for ease of comparison and testing.
2286 * Returns size of file in 'sizep' if non-NULL, null-terminates
2287 * data in memory for ease of use.
2290 dumpfile(const char *filename, void *data, size_t len)
2292 ssize_t bytes_written;
2295 f = fopen(filename, "wb");
2297 logprintf("Can't open file %s for writing\n", filename);
2300 bytes_written = fwrite(data, 1, len, f);
2301 if (bytes_written < (ssize_t)len)
2302 logprintf("Can't write file %s\n", filename);
2306 /* Read a uuencoded file from the reference directory, decode, and
2307 * write the result into the current directory. */
2308 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2309 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2311 extract_reference_file(const char *name)
2316 sprintf(buff, "%s/%s.uu", refdir, name);
2317 in = fopen(buff, "r");
2318 failure("Couldn't open reference file %s", buff);
2322 /* Read up to and including the 'begin' line. */
2324 if (fgets(buff, sizeof(buff), in) == NULL) {
2325 /* TODO: This is a failure. */
2328 if (memcmp(buff, "begin ", 6) == 0)
2331 /* Now, decode the rest and write it. */
2332 out = fopen(name, "wb");
2333 while (fgets(buff, sizeof(buff), in) != NULL) {
2337 if (memcmp(buff, "end", 3) == 0)
2340 bytes = UUDECODE(*p++);
2343 /* Write out 1-3 bytes from that. */
2345 assert(VALID_UUDECODE(p[0]));
2346 assert(VALID_UUDECODE(p[1]));
2347 n = UUDECODE(*p++) << 18;
2348 n |= UUDECODE(*p++) << 12;
2349 fputc(n >> 16, out);
2353 assert(VALID_UUDECODE(p[0]));
2354 n |= UUDECODE(*p++) << 6;
2355 fputc((n >> 8) & 0xFF, out);
2359 assert(VALID_UUDECODE(p[0]));
2360 n |= UUDECODE(*p++);
2361 fputc(n & 0xFF, out);
2371 copy_reference_file(const char *name)
2377 sprintf(buff, "%s/%s", refdir, name);
2378 in = fopen(buff, "rb");
2379 failure("Couldn't open reference file %s", buff);
2383 /* Now, decode the rest and write it. */
2384 /* Not a lot of error checking here; the input better be right. */
2385 out = fopen(name, "wb");
2386 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2387 if (fwrite(buff, 1, rbytes, out) != rbytes) {
2388 logprintf("Error: fwrite\n");
2397 is_LargeInode(const char *file)
2399 #if defined(_WIN32) && !defined(__CYGWIN__)
2400 BY_HANDLE_FILE_INFORMATION bhfi;
2403 r = my_GetFileInformationByName(file, &bhfi);
2406 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2411 if (stat(file, &st) < 0)
2413 ino = (int64_t)st.st_ino;
2414 return (ino > 0xffffffff);
2419 extract_reference_files(const char **names)
2421 while (names && *names)
2422 extract_reference_file(*names++);
2432 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2434 * DEFINE_TEST(test_function)
2438 /* Use "list.h" to declare all of the test functions. */
2440 #define DEFINE_TEST(name) void name(void);
2443 /* Use "list.h" to create a list of all tests (functions and names). */
2445 #define DEFINE_TEST(n) { n, #n, 0 },
2446 struct test_list_t tests[] = {
2451 * Summarize repeated failures in the just-completed test.
2454 test_summarize(int failed, int skips_num)
2458 switch (verbosity) {
2459 case VERBOSITY_SUMMARY_ONLY:
2460 printf(failed ? "E" : ".");
2463 case VERBOSITY_PASSFAIL:
2464 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
2468 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2470 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2471 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2472 logprintf("%s:%d: Summary: Failed %d times\n",
2473 failed_filename, i, failed_lines[i].count);
2475 /* Clear the failure history for the next file. */
2476 failed_filename = NULL;
2477 memset(failed_lines, 0, sizeof(failed_lines));
2481 * Actually run a single test, with appropriate setup and cleanup.
2484 test_run(int i, const char *tmpdir)
2487 char logfilename[64];
2488 int failures_before = failures;
2489 int skips_before = skips;
2492 switch (verbosity) {
2493 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2495 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2496 printf("%3d: %-64s", i, tests[i].name);
2499 default: /* Title of test, details will follow */
2500 printf("%3d: %s\n", i, tests[i].name);
2503 /* Chdir to the top-level work directory. */
2504 if (!assertChdir(tmpdir)) {
2506 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2509 /* Create a log file for this test. */
2510 sprintf(logfilename, "%s.log", tests[i].name);
2511 logfile = fopen(logfilename, "w");
2512 fprintf(logfile, "%s\n\n", tests[i].name);
2513 /* Chdir() to a work dir for this specific test. */
2514 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2515 testworkdir = workdir;
2516 if (!assertMakeDir(testworkdir, 0755)
2517 || !assertChdir(testworkdir)) {
2519 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2522 /* Explicitly reset the locale before each test. */
2523 setlocale(LC_ALL, "C");
2524 /* Record the umask before we run the test. */
2525 umask(oldumask = umask(0));
2527 * Run the actual test.
2531 * Clean up and report afterwards.
2537 setlocale(LC_ALL, "C");
2538 /* Reset directory. */
2539 if (!assertChdir(tmpdir)) {
2540 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2544 /* Report per-test summaries. */
2545 tests[i].failures = failures - failures_before;
2546 test_summarize(tests[i].failures, skips - skips_before);
2547 /* Close the per-test log file. */
2550 /* If there were no failures, we can remove the work dir and logfile. */
2551 if (tests[i].failures == 0) {
2552 if (!keep_temp_files && assertChdir(tmpdir)) {
2553 #if defined(_WIN32) && !defined(__CYGWIN__)
2554 /* Make sure not to leave empty directories.
2555 * Sometimes a processing of closing files used by tests
2556 * is not done, then rmdir will be failed and it will
2557 * leave a empty test directory. So we should wait a few
2558 * seconds and retry rmdir. */
2560 for (t = 0; t < 10; t++) {
2563 r = systemf("rmdir /S /Q %s", tests[i].name);
2567 systemf("del %s", logfilename);
2569 systemf("rm -rf %s", tests[i].name);
2570 systemf("rm %s", logfilename);
2574 /* Return appropriate status. */
2575 return (tests[i].failures);
2581 * MAIN and support routines.
2587 usage(const char *program)
2589 static const int limit = sizeof(tests) / sizeof(tests[0]);
2592 printf("Usage: %s [options] <test> <test> ...\n", program);
2593 printf("Default is to run all tests.\n");
2594 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2595 printf("Options:\n");
2596 printf(" -d Dump core after any failure, for debugging.\n");
2597 printf(" -k Keep all temp files.\n");
2598 printf(" Default: temp files for successful tests deleted.\n");
2600 printf(" -p <path> Path to executable to be tested.\n");
2601 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2603 printf(" -q Quiet.\n");
2604 printf(" -r <dir> Path to dir containing reference files.\n");
2605 printf(" Default: Current directory.\n");
2606 printf(" -u Keep running specifies tests until one fails.\n");
2607 printf(" -v Verbose.\n");
2608 printf("Available tests:\n");
2609 for (i = 0; i < limit; i++)
2610 printf(" %d: %s\n", i, tests[i].name);
2615 get_refdir(const char *d)
2617 size_t tried_size, buff_size;
2618 char *buff, *tried, *pwd = NULL, *p = NULL;
2621 buff_size = PATH_MAX;
2625 buff = calloc(buff_size, 1);
2627 fprintf(stderr, "Unable to allocate memory\n");
2631 /* Allocate a buffer to hold the various directories we checked. */
2632 tried_size = buff_size * 2;
2633 tried = calloc(tried_size, 1);
2634 if (tried == NULL) {
2635 fprintf(stderr, "Unable to allocate memory\n");
2639 /* If a dir was specified, try that */
2642 snprintf(buff, buff_size, "%s", d);
2643 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2644 if (p != NULL) goto success;
2645 strncat(tried, buff, tried_size - strlen(tried) - 1);
2646 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2650 /* Get the current dir. */
2652 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2654 pwd = getcwd(NULL, 0);
2656 while (pwd[strlen(pwd) - 1] == '\n')
2657 pwd[strlen(pwd) - 1] = '\0';
2659 /* Look for a known file. */
2660 snprintf(buff, buff_size, "%s", pwd);
2661 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2662 if (p != NULL) goto success;
2663 strncat(tried, buff, tried_size - strlen(tried) - 1);
2664 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2666 snprintf(buff, buff_size, "%s/test", pwd);
2667 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2668 if (p != NULL) goto success;
2669 strncat(tried, buff, tried_size - strlen(tried) - 1);
2670 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2672 #if defined(LIBRARY)
2673 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
2675 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
2677 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2678 if (p != NULL) goto success;
2679 strncat(tried, buff, tried_size - strlen(tried) - 1);
2680 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2682 #if defined(PROGRAM_ALIAS)
2683 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
2684 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2685 if (p != NULL) goto success;
2686 strncat(tried, buff, tried_size - strlen(tried) - 1);
2687 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2690 if (memcmp(pwd, "/usr/obj", 8) == 0) {
2691 snprintf(buff, buff_size, "%s", pwd + 8);
2692 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2693 if (p != NULL) goto success;
2694 strncat(tried, buff, tried_size - strlen(tried) - 1);
2695 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2697 snprintf(buff, buff_size, "%s/test", pwd + 8);
2698 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2699 if (p != NULL) goto success;
2700 strncat(tried, buff, tried_size - strlen(tried) - 1);
2701 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2705 printf("Unable to locate known reference file %s\n", KNOWNREF);
2706 printf(" Checked following directories:\n%s\n", tried);
2707 printf("Use -r option to specify full path to reference directory\n");
2708 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2718 /* Copy result into a fresh buffer to reduce memory usage. */
2725 main(int argc, char **argv)
2727 static const int limit = sizeof(tests) / sizeof(tests[0]);
2728 int test_set[sizeof(tests) / sizeof(tests[0])];
2729 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2731 char *refdir_alloc = NULL;
2732 const char *progname;
2734 const char *tmp, *option_arg, *p;
2735 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2736 char tmpdir_timestamp[256];
2738 (void)argc; /* UNUSED */
2740 /* Get the current dir. */
2742 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2744 pwd = getcwd(NULL, 0);
2746 while (pwd[strlen(pwd) - 1] == '\n')
2747 pwd[strlen(pwd) - 1] = '\0';
2749 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
2750 /* To stop to run the default invalid parameter handler. */
2751 _set_invalid_parameter_handler(invalid_parameter_handler);
2752 /* Disable annoying assertion message box. */
2753 _CrtSetReportMode(_CRT_ASSERT, 0);
2757 * Name of this program, used to build root of our temp directory
2760 progname = p = argv[0];
2761 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2763 fprintf(stderr, "ERROR: Out of memory.");
2766 strcpy(testprogdir, progname);
2767 while (*p != '\0') {
2768 /* Support \ or / dir separators for Windows compat. */
2769 if (*p == '/' || *p == '\\')
2777 testprogdir[i] = '\0';
2778 #if defined(_WIN32) && !defined(__CYGWIN__)
2779 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2780 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2781 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2782 testprogdir[1] == ':' &&
2783 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
2785 if (testprogdir[0] != '/')
2788 /* Fixup path for relative directories. */
2789 if ((testprogdir = (char *)realloc(testprogdir,
2790 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2792 fprintf(stderr, "ERROR: Out of memory.");
2795 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2796 strlen(testprogdir) + 1);
2797 memcpy(testprogdir, pwd, strlen(pwd));
2798 testprogdir[strlen(pwd)] = '/';
2802 /* Get the target program from environment, if available. */
2803 testprogfile = getenv(ENVBASE);
2806 if (getenv("TMPDIR") != NULL)
2807 tmp = getenv("TMPDIR");
2808 else if (getenv("TMP") != NULL)
2809 tmp = getenv("TMP");
2810 else if (getenv("TEMP") != NULL)
2811 tmp = getenv("TEMP");
2812 else if (getenv("TEMPDIR") != NULL)
2813 tmp = getenv("TEMPDIR");
2817 /* Allow -d to be controlled through the environment. */
2818 if (getenv(ENVBASE "_DEBUG") != NULL)
2819 dump_on_failure = 1;
2821 /* Allow -v to be controlled through the environment. */
2822 if (getenv("_VERBOSITY_LEVEL") != NULL)
2824 vlevel = getenv("_VERBOSITY_LEVEL");
2825 verbosity = atoi(vlevel);
2826 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2828 /* Unsupported verbosity levels are silently ignored */
2830 verbosity = VERBOSITY_PASSFAIL;
2834 /* Get the directory holding test files from environment. */
2835 refdir = getenv(ENVBASE "_TEST_FILES");
2838 * Parse options, without using getopt(), which isn't available
2841 ++argv; /* Skip program name */
2842 while (*argv != NULL) {
2847 while (*p != '\0') {
2850 /* If 'opt' takes an argument, parse that. */
2851 if (option == 'p' || option == 'r') {
2854 else if (*argv == NULL) {
2856 "Option -%c requires argument.\n",
2860 option_arg = *argv++;
2861 p = ""; /* End of this option word. */
2864 /* Now, handle the option. */
2867 dump_on_failure = 1;
2870 keep_temp_files = 1;
2874 testprogfile = option_arg;
2876 fprintf(stderr, "-p option not permitted\n");
2885 refdir = option_arg;
2895 fprintf(stderr, "Unrecognized option '%c'\n",
2903 * Sanity-check that our options make sense.
2906 if (testprogfile == NULL)
2908 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2909 strlen(PROGRAM) + 1)) == NULL)
2911 fprintf(stderr, "ERROR: Out of memory.");
2914 strcpy(tmp2, testprogdir);
2916 strcat(tmp2, PROGRAM);
2917 testprogfile = tmp2;
2922 #if defined(_WIN32) && !defined(__CYGWIN__)
2923 /* Command.com sometimes rejects '/' separators. */
2924 testprg = strdup(testprogfile);
2925 for (i = 0; testprg[i] != '\0'; i++) {
2926 if (testprg[i] == '/')
2929 testprogfile = testprg;
2931 /* Quote the name that gets put into shell command lines. */
2932 testprg = malloc(strlen(testprogfile) + 3);
2933 strcpy(testprg, "\"");
2934 strcat(testprg, testprogfile);
2935 strcat(testprg, "\"");
2940 #if !defined(_WIN32) && defined(SIGPIPE)
2941 { /* Ignore SIGPIPE signals */
2942 struct sigaction sa;
2943 sa.sa_handler = SIG_IGN;
2944 sigemptyset(&sa.sa_mask);
2946 sigaction(SIGPIPE, &sa, NULL);
2951 * Create a temp directory for the following tests.
2952 * Include the time the tests started as part of the name,
2953 * to make it easier to track the results of multiple tests.
2956 for (i = 0; ; i++) {
2957 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2958 "%Y-%m-%dT%H.%M.%S",
2960 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2961 tmpdir_timestamp, i);
2962 if (assertMakeDir(tmpdir,0755))
2966 "ERROR: Unable to create temp directory %s\n",
2973 * If the user didn't specify a directory for locating
2974 * reference files, try to find the reference files in
2975 * the "usual places."
2977 refdir = refdir_alloc = get_refdir(refdir);
2980 * Banner with basic information.
2983 printf("If tests fail or crash, details will be in:\n");
2984 printf(" %s\n", tmpdir);
2986 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2987 printf("Reference files will be read from: %s\n", refdir);
2989 printf("Running tests on: %s\n", testprog);
2991 printf("Exercising: ");
2993 printf("%s\n", EXTRA_VERSION);
3000 * Run some or all of the individual tests.
3008 test_num = get_test_set(test_set, limit, *argv, tests);
3010 printf("*** INVALID Test %s\n", *argv);
3016 for (i = 0; i < test_num; i++) {
3018 if (test_run(test_set[i], tmpdir)) {
3026 } while (*argv != NULL);
3027 } while (until_failure);
3030 /* Must be freed after all tests run */
3036 * Report summary statistics.
3038 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3040 printf("Totals:\n");
3041 printf(" Tests run: %8d\n", tests_run);
3042 printf(" Tests failed: %8d\n", tests_failed);
3043 printf(" Assertions checked:%8d\n", assertions);
3044 printf(" Assertions failed: %8d\n", failures);
3045 printf(" Skips reported: %8d\n", skips);
3049 printf("Failing tests:\n");
3050 for (i = 0; i < limit; ++i) {
3051 if (tests[i].failures)
3052 printf(" %d: %s (%d failures)\n", i,
3053 tests[i].name, tests[i].failures);
3056 printf("Details for failing tests: %s\n", tmpdir);
3059 if (verbosity == VERBOSITY_SUMMARY_ONLY)
3061 printf("%d tests passed, no failures\n", tests_run);
3066 /* If the final tmpdir is empty, we can remove it. */
3067 /* This should be the usual case when all tests succeed. */
3071 return (tests_failed ? 1 : 0);