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 */
48 #ifdef HAVE_LINUX_FS_H
61 * Windows support routines
63 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
64 * in the test harness is dangerous because they cover up
65 * configuration errors. The classic example of this is omitting a
66 * configure check. If libarchive and libarchive_test both look for
67 * the same feature macro, such errors are hard to detect. Platform
68 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
69 * easily lead to very messy code. It's best to limit yourself
70 * to only the most generic programming techniques in the test harness
71 * and thus avoid conditionals altogether. Where that's not possible,
72 * try to minimize conditionals by grouping platform-specific tests in
73 * one place (e.g., test_acl_freebsd) or by adding new assert()
74 * functions (e.g., assertMakeHardlink()) to cover up platform
75 * differences. Platform-specific coding in libarchive_test is often
76 * a symptom that some capability is missing from libarchive itself.
78 #if defined(_WIN32) && !defined(__CYGWIN__)
86 #define S_ISDIR(m) ((m) & _S_IFDIR)
89 #define S_ISREG(m) ((m) & _S_IFREG)
91 #if !defined(__BORLANDC__)
92 #define access _access
97 #define fileno _fileno
99 /*#define fstat _fstat64*/
100 #if !defined(__BORLANDC__)
101 #define getcwd _getcwd
104 /*#define lstat _stat64*/
105 /*#define stat _stat64*/
107 #if !defined(__BORLANDC__)
108 #define strdup _strdup
111 #define int64_t __int64
114 #if defined(HAVE__CrtSetReportMode)
118 mode_t umasked(mode_t expected_mode)
120 mode_t mode = umask(0);
122 return expected_mode & ~mode;
125 /* Path to working directory for current test */
126 const char *testworkdir;
128 /* Pathname of exe to be tested. */
129 const char *testprogfile;
130 /* Name of exe to use in printf-formatted command strings. */
131 /* On Windows, this includes leading/trailing quotes. */
132 const char *testprog;
135 #if defined(_WIN32) && !defined(__CYGWIN__)
136 static void *GetFunctionKernel32(const char *);
137 static int my_CreateSymbolicLinkA(const char *, const char *, int);
138 static int my_CreateHardLinkA(const char *, const char *);
139 static int my_GetFileInformationByName(const char *,
140 BY_HANDLE_FILE_INFORMATION *);
143 GetFunctionKernel32(const char *name)
145 static HINSTANCE lib;
149 lib = LoadLibrary("kernel32.dll");
152 fprintf(stderr, "Can't load kernel32.dll?!\n");
155 return (void *)GetProcAddress(lib, name);
159 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
161 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
165 f = GetFunctionKernel32("CreateSymbolicLinkA");
167 return f == NULL ? 0 : (*f)(linkname, target, flags);
171 my_CreateHardLinkA(const char *linkname, const char *target)
173 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
177 f = GetFunctionKernel32("CreateHardLinkA");
179 return f == NULL ? 0 : (*f)(linkname, target, NULL);
183 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
188 memset(bhfi, 0, sizeof(*bhfi));
189 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
190 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
191 if (h == INVALID_HANDLE_VALUE)
193 r = GetFileInformationByHandle(h, bhfi);
199 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
201 invalid_parameter_handler(const wchar_t * expression,
202 const wchar_t * function, const wchar_t * file,
203 unsigned int line, uintptr_t pReserved)
206 // Silence unused-parameter compiler warnings.
221 /* Enable core dump on failure. */
222 static int dump_on_failure = 0;
223 /* Default is to remove temp dirs and log data for successful tests. */
224 static int keep_temp_files = 0;
225 /* Default is to run the specified tests once and report errors. */
226 static int until_failure = 0;
227 /* Default is to just report pass/fail for each test. */
228 static int verbosity = 0;
229 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
230 #define VERBOSITY_PASSFAIL 0 /* Default */
231 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
232 #define VERBOSITY_FULL 2 /* -vv */
233 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
234 * mostly for debugging the test harness itself. */
235 /* Cumulative count of assertion failures. */
236 static int failures = 0;
237 /* Cumulative count of reported skips. */
238 static int skips = 0;
239 /* Cumulative count of assertions checked. */
240 static int assertions = 0;
242 /* Directory where uuencoded reference files can be found. */
243 static const char *refdir;
246 * Report log information selectively to console and/or disk log.
248 static int log_console = 0;
249 static FILE *logfile;
251 vlogprintf(const char *fmt, va_list ap)
258 vfprintf(stdout, fmt, ap);
261 vfprintf(logfile, fmt, lfap);
264 vfprintf(logfile, fmt, ap);
269 logprintf(const char *fmt, ...)
277 /* Set up a message to display only if next assertion fails. */
278 static char msgbuff[4096];
279 static const char *msg, *nextmsg;
281 failure(const char *fmt, ...)
288 vsprintf(msgbuff, fmt, ap);
295 * Copy arguments into file-local variables.
296 * This was added to permit vararg assert() functions without needing
297 * variadic wrapper macros. Turns out that the vararg capability is almost
298 * never used, so almost all of the vararg assertions can be simplified
299 * by removing the vararg capability and reworking the wrapper macro to
300 * pass __FILE__, __LINE__ directly into the function instead of using
301 * this hook. I suspect this machinery is used so rarely that we
302 * would be better off just removing it entirely. That would simplify
303 * the code here noticeably.
305 static const char *skipping_filename;
306 static int skipping_line;
307 void skipping_setup(const char *filename, int line)
309 skipping_filename = filename;
310 skipping_line = line;
313 /* Called at the beginning of each assert() function. */
315 assertion_count(const char *file, int line)
317 (void)file; /* UNUSED */
318 (void)line; /* UNUSED */
320 /* Proper handling of "failure()" message. */
323 /* Uncomment to print file:line after every assertion.
324 * Verbose, but occasionally useful in tracking down crashes. */
325 /* printf("Checked %s:%d\n", file, line); */
329 * For each test source file, we remember how many times each
330 * assertion was reported. Cleared before each new test,
331 * used by test_summarize().
336 } failed_lines[10000];
337 const char *failed_filename;
339 /* Count this failure, setup up log destination and handle initial report. */
341 failure_start(const char *filename, int line, const char *fmt, ...)
345 /* Record another failure for this line. */
347 failed_filename = filename;
348 failed_lines[line].count++;
350 /* Determine whether to log header to console. */
352 case VERBOSITY_LIGHT_REPORT:
353 log_console = (failed_lines[line].count < 2);
356 log_console = (verbosity >= VERBOSITY_FULL);
359 /* Log file:line header for this failure */
362 logprintf("%s(%d): ", filename, line);
364 logprintf("%s:%d: ", filename, line);
370 if (msg != NULL && msg[0] != '\0') {
371 logprintf(" Description: %s\n", msg);
375 /* Determine whether to log details to console. */
376 if (verbosity == VERBOSITY_LIGHT_REPORT)
380 /* Complete reporting of failed tests. */
382 * The 'extra' hook here is used by libarchive to include libarchive
383 * error messages with assertion failures. It could also be used
384 * to add strerror() output, for example. Just define the EXTRA_DUMP()
385 * macro appropriately.
388 failure_finish(void *extra)
390 (void)extra; /* UNUSED (maybe) */
393 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
394 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
398 if (dump_on_failure) {
400 " *** forcing core dump so failure can be debugged ***\n");
405 /* Inform user that we're skipping some checks. */
407 test_skipping(const char *fmt, ...)
413 vsprintf(buff, fmt, ap);
415 /* Use failure() message if set. */
418 /* failure_start() isn't quite right, but is awfully convenient. */
419 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
420 --failures; /* Undo failures++ in failure_start() */
421 /* Don't failure_finish() here. */
422 /* Mark as skip, so doesn't count as failed test. */
423 failed_lines[skipping_line].skip = 1;
433 /* Generic assert() just displays the failed condition. */
435 assertion_assert(const char *file, int line, int value,
436 const char *condition, void *extra)
438 assertion_count(file, line);
440 failure_start(file, line, "Assertion failed: %s", condition);
441 failure_finish(extra);
446 /* chdir() and report any errors */
448 assertion_chdir(const char *file, int line, const char *pathname)
450 assertion_count(file, line);
451 if (chdir(pathname) == 0)
453 failure_start(file, line, "chdir(\"%s\")", pathname);
454 failure_finish(NULL);
459 /* Verify two integers are equal. */
461 assertion_equal_int(const char *file, int line,
462 long long v1, const char *e1, long long v2, const char *e2, void *extra)
464 assertion_count(file, line);
467 failure_start(file, line, "%s != %s", e1, e2);
468 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
469 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
470 failure_finish(extra);
475 * Utility to convert a single UTF-8 sequence.
478 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
480 static const char utf8_count[256] = {
481 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
482 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
483 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
484 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
485 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
486 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
487 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
488 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
489 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
490 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
491 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
493 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
494 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
495 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
496 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
508 * Decode 1-4 bytes depending on the value of the first byte.
510 ch = (unsigned char)*s;
512 return (0); /* Standard: return 0 for end-of-string. */
513 cnt = utf8_count[ch];
515 /* Invalid sequence or there are not plenty bytes. */
519 /* Make a Unicode code point from a single UTF-8 sequence. */
521 case 1: /* 1 byte sequence. */
524 case 2: /* 2 bytes sequence. */
525 if ((s[1] & 0xc0) != 0x80) return (-1);
526 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
528 case 3: /* 3 bytes sequence. */
529 if ((s[1] & 0xc0) != 0x80) return (-1);
530 if ((s[2] & 0xc0) != 0x80) return (-1);
531 wc = ((ch & 0x0f) << 12)
532 | ((s[1] & 0x3f) << 6)
535 return (-1);/* Overlong sequence. */
537 case 4: /* 4 bytes sequence. */
540 if ((s[1] & 0xc0) != 0x80) return (-1);
541 if ((s[2] & 0xc0) != 0x80) return (-1);
542 if ((s[3] & 0xc0) != 0x80) return (-1);
543 wc = ((ch & 0x07) << 18)
544 | ((s[1] & 0x3f) << 12)
545 | ((s[2] & 0x3f) << 6)
548 return (-1);/* Overlong sequence. */
554 /* The code point larger than 0x10FFFF is not legal
558 /* Correctly gets a Unicode, returns used bytes. */
563 static void strdump(const char *e, const char *p, int ewidth, int utf8)
567 logprintf(" %*s = ", ewidth, e);
574 unsigned int c = 0xff & *p++;
576 case '\a': logprintf("\\a"); break;
577 case '\b': logprintf("\\b"); break;
578 case '\n': logprintf("\\n"); break;
579 case '\r': logprintf("\\r"); break;
581 if (c >= 32 && c < 127)
584 logprintf("\\x%02X", c);
588 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
591 * If the current string is UTF-8, dump its code points.
602 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
605 logprintf("%04X", uc);
611 logprintf(" (count %d", cnt);
613 logprintf(",unknown %d bytes", len);
621 /* Verify two strings are equal, dump them if not. */
623 assertion_equal_string(const char *file, int line,
624 const char *v1, const char *e1,
625 const char *v2, const char *e2,
626 void *extra, int utf8)
630 assertion_count(file, line);
631 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
633 failure_start(file, line, "%s != %s", e1, e2);
634 l1 = (int)strlen(e1);
635 l2 = (int)strlen(e2);
638 strdump(e1, v1, l1, utf8);
639 strdump(e2, v2, l1, utf8);
640 failure_finish(extra);
645 wcsdump(const char *e, const wchar_t *w)
647 logprintf(" %s = ", e);
653 while (*w != L'\0') {
654 unsigned int c = *w++;
655 if (c >= 32 && c < 127)
658 logprintf("\\x%02X", c);
659 else if (c < 0x10000)
660 logprintf("\\u%04X", c);
662 logprintf("\\U%08X", c);
669 wcscmp(const wchar_t *s1, const wchar_t *s2)
672 while (*s1 == *s2++) {
683 /* Verify that two wide strings are equal, dump them if not. */
685 assertion_equal_wstring(const char *file, int line,
686 const wchar_t *v1, const char *e1,
687 const wchar_t *v2, const char *e2,
690 assertion_count(file, line);
693 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
695 failure_start(file, line, "%s != %s", e1, e2);
698 failure_finish(extra);
703 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
704 * any bytes in p that differ from ref will be highlighted with '_'
705 * before and after the hex value.
708 hexdump(const char *p, const char *ref, size_t l, size_t offset)
714 logprintf("(null)\n");
717 for(i=0; i < l; i+=16) {
718 logprintf("%04x", (unsigned)(i + offset));
720 for (j = 0; j < 16 && i + j < l; j++) {
721 if (ref != NULL && p[i + j] != ref[i + j])
723 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
724 if (ref != NULL && p[i + j] == ref[i + j])
727 for (; j < 16; j++) {
728 logprintf("%c ", sep);
731 logprintf("%c", sep);
732 for (j=0; j < 16 && i + j < l; j++) {
734 if (c >= ' ' && c <= 126)
743 /* Verify that two blocks of memory are the same, display the first
744 * block of differences if they're not. */
746 assertion_equal_mem(const char *file, int line,
747 const void *_v1, const char *e1,
748 const void *_v2, const char *e2,
749 size_t l, const char *ld, void *extra)
751 const char *v1 = (const char *)_v1;
752 const char *v2 = (const char *)_v2;
755 assertion_count(file, line);
756 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
758 if (v1 == NULL || v2 == NULL)
761 failure_start(file, line, "%s != %s", e1, e2);
762 logprintf(" size %s = %d\n", ld, (int)l);
763 /* Dump 48 bytes (3 lines) so that the first difference is
764 * in the second line. */
766 while (l > 64 && memcmp(v1, v2, 32) == 0) {
767 /* Two lines agree, so step forward one line. */
773 logprintf(" Dump of %s\n", e1);
774 hexdump(v1, v2, l < 128 ? l : 128, offset);
775 logprintf(" Dump of %s\n", e2);
776 hexdump(v2, v1, l < 128 ? l : 128, offset);
778 failure_finish(extra);
782 /* Verify that a block of memory is filled with the specified byte. */
784 assertion_memory_filled_with(const char *file, int line,
785 const void *_v1, const char *vd,
786 size_t l, const char *ld,
787 char b, const char *bd, void *extra)
789 const char *v1 = (const char *)_v1;
792 (void)ld; /* UNUSED */
794 assertion_count(file, line);
796 for (i = 0; i < l; ++i) {
804 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
805 logprintf(" Only %d bytes were correct\n", (int)c);
806 failure_finish(extra);
810 /* Verify that the named file exists and is empty. */
812 assertion_empty_file(const char *filename, int line, const char *f1)
819 assertion_count(filename, line);
821 if (stat(f1, &st) != 0) {
822 failure_start(filename, line, "Stat failed: %s", f1);
823 failure_finish(NULL);
829 failure_start(filename, line, "File should be empty: %s", f1);
830 logprintf(" File size: %d\n", (int)st.st_size);
831 logprintf(" Contents:\n");
834 logprintf(" Unable to open %s\n", f1);
836 s = ((off_t)sizeof(buff) < st.st_size) ?
837 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
838 s = fread(buff, 1, s, f);
839 hexdump(buff, NULL, s, 0);
842 failure_finish(NULL);
846 /* Verify that the named file exists and is not empty. */
848 assertion_non_empty_file(const char *filename, int line, const char *f1)
852 assertion_count(filename, line);
854 if (stat(f1, &st) != 0) {
855 failure_start(filename, line, "Stat failed: %s", f1);
856 failure_finish(NULL);
859 if (st.st_size == 0) {
860 failure_start(filename, line, "File empty: %s", f1);
861 failure_finish(NULL);
867 /* Verify that two files have the same contents. */
868 /* TODO: hexdump the first bytes that actually differ. */
870 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
877 assertion_count(filename, line);
879 f1 = fopen(fn1, "rb");
880 f2 = fopen(fn2, "rb");
881 if (f1 == NULL || f2 == NULL) {
887 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
888 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
891 if (n1 == 0 && n2 == 0) {
896 if (memcmp(buff1, buff2, n1) != 0)
901 failure_start(filename, line, "Files not identical");
902 logprintf(" file1=\"%s\"\n", fn1);
903 logprintf(" file2=\"%s\"\n", fn2);
904 failure_finish(NULL);
908 /* Verify that the named file does exist. */
910 assertion_file_exists(const char *filename, int line, const char *f)
912 assertion_count(filename, line);
914 #if defined(_WIN32) && !defined(__CYGWIN__)
918 if (!access(f, F_OK))
921 failure_start(filename, line, "File should exist: %s", f);
922 failure_finish(NULL);
926 /* Verify that the named file doesn't exist. */
928 assertion_file_not_exists(const char *filename, int line, const char *f)
930 assertion_count(filename, line);
932 #if defined(_WIN32) && !defined(__CYGWIN__)
939 failure_start(filename, line, "File should not exist: %s", f);
940 failure_finish(NULL);
944 /* Compare the contents of a file to a block of memory. */
946 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
952 assertion_count(filename, line);
956 failure_start(filename, line,
957 "File should exist: %s", fn);
958 failure_finish(NULL);
961 contents = malloc(s * 2);
962 n = (int)fread(contents, 1, s * 2, f);
964 if (n == s && memcmp(buff, contents, s) == 0) {
968 failure_start(filename, line, "File contents don't match");
969 logprintf(" file=\"%s\"\n", fn);
971 hexdump(contents, buff, n > 512 ? 512 : n, 0);
973 logprintf(" File empty, contents should be:\n");
974 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
976 failure_finish(NULL);
981 /* Check the contents of a text file, being tolerant of line endings. */
983 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
986 const char *btxt, *ftxt;
990 assertion_count(filename, line);
993 failure_start(filename, line,
994 "File doesn't exist: %s", fn);
995 failure_finish(NULL);
998 s = (int)strlen(buff);
999 contents = malloc(s * 2 + 128);
1000 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1004 /* Compare texts. */
1006 ftxt = (const char *)contents;
1007 while (*btxt != '\0' && *ftxt != '\0') {
1008 if (*btxt == *ftxt) {
1013 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1014 /* Pass over different new line characters. */
1021 if (*btxt == '\0' && *ftxt == '\0') {
1025 failure_start(filename, line, "Contents don't match");
1026 logprintf(" file=\"%s\"\n", fn);
1028 hexdump(contents, buff, n, 0);
1029 logprintf(" expected\n", fn);
1030 hexdump(buff, contents, s, 0);
1032 logprintf(" File empty, contents should be:\n");
1033 hexdump(buff, NULL, s, 0);
1035 failure_finish(NULL);
1040 /* Verify that a text file contains the specified lines, regardless of order */
1041 /* This could be more efficient if we sorted both sets of lines, etc, but
1042 * since this is used only for testing and only ever deals with a dozen or so
1043 * lines at a time, this relatively crude approach is just fine. */
1045 assertion_file_contains_lines_any_order(const char *file, int line,
1046 const char *pathname, const char *lines[])
1050 size_t expected_count, actual_count, i, j;
1051 char **expected = NULL;
1052 char *p, **actual = NULL;
1054 int expected_failure = 0, actual_failure = 0;
1056 assertion_count(file, line);
1058 buff = slurpfile(&buff_size, "%s", pathname);
1060 failure_start(pathname, line, "Can't read file: %s", pathname);
1061 failure_finish(NULL);
1065 /* Make a copy of the provided lines and count up the expected
1067 for (i = 0; lines[i] != NULL; ++i) {
1070 if (expected_count) {
1071 expected = malloc(sizeof(char *) * expected_count);
1072 if (expected == NULL) {
1073 failure_start(pathname, line, "Can't allocate memory");
1074 failure_finish(NULL);
1078 for (i = 0; lines[i] != NULL; ++i) {
1079 expected[i] = strdup(lines[i]);
1083 /* Break the file into lines */
1085 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1086 if (*p == '\x0d' || *p == '\x0a')
1088 if (c == '\0' && *p != '\0')
1093 actual = calloc(sizeof(char *), actual_count);
1094 if (actual == NULL) {
1095 failure_start(pathname, line, "Can't allocate memory");
1096 failure_finish(NULL);
1100 for (j = 0, p = buff; p < buff + buff_size;
1101 p += 1 + strlen(p)) {
1109 /* Erase matching lines from both lists */
1110 for (i = 0; i < expected_count; ++i) {
1111 if (expected[i] == NULL)
1113 for (j = 0; j < actual_count; ++j) {
1114 if (actual[j] == NULL)
1116 if (strcmp(expected[i], actual[j]) == 0) {
1125 /* If there's anything left, it's a failure */
1126 for (i = 0; i < expected_count; ++i) {
1127 if (expected[i] != NULL)
1130 for (j = 0; j < actual_count; ++j) {
1131 if (actual[j] != NULL)
1134 if (expected_failure == 0 && actual_failure == 0) {
1140 failure_start(file, line, "File doesn't match: %s", pathname);
1141 for (i = 0; i < expected_count; ++i) {
1142 if (expected[i] != NULL) {
1143 logprintf(" Expected but not present: %s\n", expected[i]);
1147 for (j = 0; j < actual_count; ++j) {
1148 if (actual[j] != NULL)
1149 logprintf(" Present but not expected: %s\n", actual[j]);
1151 failure_finish(NULL);
1158 /* Verify that a text file does not contains the specified strings */
1160 assertion_file_contains_no_invalid_strings(const char *file, int line,
1161 const char *pathname, const char *strings[])
1166 buff = slurpfile(NULL, "%s", pathname);
1168 failure_start(file, line, "Can't read file: %s", pathname);
1169 failure_finish(NULL);
1173 for (i = 0; strings[i] != NULL; ++i) {
1174 if (strstr(buff, strings[i]) != NULL) {
1175 failure_start(file, line, "Invalid string in %s: %s", pathname,
1177 failure_finish(NULL);
1187 /* Test that two paths point to the same file. */
1188 /* As a side-effect, asserts that both files exist. */
1190 is_hardlink(const char *file, int line,
1191 const char *path1, const char *path2)
1193 #if defined(_WIN32) && !defined(__CYGWIN__)
1194 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1197 assertion_count(file, line);
1198 r = my_GetFileInformationByName(path1, &bhfi1);
1200 failure_start(file, line, "File %s can't be inspected?", path1);
1201 failure_finish(NULL);
1204 r = my_GetFileInformationByName(path2, &bhfi2);
1206 failure_start(file, line, "File %s can't be inspected?", path2);
1207 failure_finish(NULL);
1210 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1211 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1212 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1214 struct stat st1, st2;
1217 assertion_count(file, line);
1218 r = lstat(path1, &st1);
1220 failure_start(file, line, "File should exist: %s", path1);
1221 failure_finish(NULL);
1224 r = lstat(path2, &st2);
1226 failure_start(file, line, "File should exist: %s", path2);
1227 failure_finish(NULL);
1230 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1235 assertion_is_hardlink(const char *file, int line,
1236 const char *path1, const char *path2)
1238 if (is_hardlink(file, line, path1, path2))
1240 failure_start(file, line,
1241 "Files %s and %s are not hardlinked", path1, path2);
1242 failure_finish(NULL);
1247 assertion_is_not_hardlink(const char *file, int line,
1248 const char *path1, const char *path2)
1250 if (!is_hardlink(file, line, path1, path2))
1252 failure_start(file, line,
1253 "Files %s and %s should not be hardlinked", path1, path2);
1254 failure_finish(NULL);
1258 /* Verify a/b/mtime of 'pathname'. */
1259 /* If 'recent', verify that it's within last 10 seconds. */
1261 assertion_file_time(const char *file, int line,
1262 const char *pathname, long t, long nsec, char type, int recent)
1264 long long filet, filet_nsec;
1267 #if defined(_WIN32) && !defined(__CYGWIN__)
1268 #define EPOC_TIME (116444736000000000ULL)
1269 FILETIME fxtime, fbirthtime, fatime, fmtime;
1270 ULARGE_INTEGER wintm;
1272 fxtime.dwLowDateTime = 0;
1273 fxtime.dwHighDateTime = 0;
1275 assertion_count(file, line);
1276 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1277 * a directory file. If not, CreateFile() will fail when
1278 * the pathname is a directory. */
1279 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1280 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1281 if (h == INVALID_HANDLE_VALUE) {
1282 failure_start(file, line, "Can't access %s\n", pathname);
1283 failure_finish(NULL);
1286 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1288 case 'a': fxtime = fatime; break;
1289 case 'b': fxtime = fbirthtime; break;
1290 case 'm': fxtime = fmtime; break;
1294 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1295 failure_finish(NULL);
1298 wintm.LowPart = fxtime.dwLowDateTime;
1299 wintm.HighPart = fxtime.dwHighDateTime;
1300 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1301 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1302 nsec = (nsec / 100) * 100; /* Round the request */
1306 assertion_count(file, line);
1307 r = lstat(pathname, &st);
1309 failure_start(file, line, "Can't stat %s\n", pathname);
1310 failure_finish(NULL);
1314 case 'a': filet = st.st_atime; break;
1315 case 'm': filet = st.st_mtime; break;
1316 case 'b': filet = 0; break;
1317 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1320 #if defined(__FreeBSD__)
1322 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1323 case 'b': filet = st.st_birthtime;
1324 /* FreeBSD filesystems that don't support birthtime
1325 * (e.g., UFS1) always return -1 here. */
1329 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1330 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1331 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1334 /* FreeBSD generally only stores to microsecond res, so round. */
1335 filet_nsec = (filet_nsec / 1000) * 1000;
1336 nsec = (nsec / 1000) * 1000;
1338 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1339 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1340 #if defined(__HAIKU__)
1341 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1346 /* Check that requested time is up-to-date. */
1347 time_t now = time(NULL);
1348 if (filet < now - 10 || filet > now + 1) {
1349 failure_start(file, line,
1350 "File %s has %ctime %lld, %lld seconds ago\n",
1351 pathname, type, filet, now - filet);
1352 failure_finish(NULL);
1355 } else if (filet != t || filet_nsec != nsec) {
1356 failure_start(file, line,
1357 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1358 pathname, type, filet, filet_nsec, t, nsec);
1359 failure_finish(NULL);
1365 /* Verify atime of 'pathname'. */
1367 assertion_file_atime(const char *file, int line,
1368 const char *pathname, long t, long nsec)
1370 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1373 /* Verify atime of 'pathname' is up-to-date. */
1375 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1377 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1380 /* Verify birthtime of 'pathname'. */
1382 assertion_file_birthtime(const char *file, int line,
1383 const char *pathname, long t, long nsec)
1385 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1388 /* Verify birthtime of 'pathname' is up-to-date. */
1390 assertion_file_birthtime_recent(const char *file, int line,
1391 const char *pathname)
1393 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1396 /* Verify mode of 'pathname'. */
1398 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1403 assertion_count(file, line);
1404 #if defined(_WIN32) && !defined(__CYGWIN__)
1405 failure_start(file, line, "assertFileMode not yet implemented for Windows");
1406 (void)mode; /* UNUSED */
1407 (void)r; /* UNUSED */
1408 (void)pathname; /* UNUSED */
1409 (void)expected_mode; /* UNUSED */
1413 r = lstat(pathname, &st);
1414 mode = (int)(st.st_mode & 0777);
1416 if (r == 0 && mode == expected_mode)
1418 failure_start(file, line, "File %s has mode %o, expected %o",
1419 pathname, mode, expected_mode);
1421 failure_finish(NULL);
1425 /* Verify mtime of 'pathname'. */
1427 assertion_file_mtime(const char *file, int line,
1428 const char *pathname, long t, long nsec)
1430 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1433 /* Verify mtime of 'pathname' is up-to-date. */
1435 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1437 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1440 /* Verify number of links to 'pathname'. */
1442 assertion_file_nlinks(const char *file, int line,
1443 const char *pathname, int nlinks)
1445 #if defined(_WIN32) && !defined(__CYGWIN__)
1446 BY_HANDLE_FILE_INFORMATION bhfi;
1449 assertion_count(file, line);
1450 r = my_GetFileInformationByName(pathname, &bhfi);
1451 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1453 failure_start(file, line, "File %s has %d links, expected %d",
1454 pathname, bhfi.nNumberOfLinks, nlinks);
1455 failure_finish(NULL);
1461 assertion_count(file, line);
1462 r = lstat(pathname, &st);
1463 if (r == 0 && (int)st.st_nlink == nlinks)
1465 failure_start(file, line, "File %s has %d links, expected %d",
1466 pathname, st.st_nlink, nlinks);
1467 failure_finish(NULL);
1472 /* Verify size of 'pathname'. */
1474 assertion_file_size(const char *file, int line, const char *pathname, long size)
1479 assertion_count(file, line);
1480 #if defined(_WIN32) && !defined(__CYGWIN__)
1482 BY_HANDLE_FILE_INFORMATION bhfi;
1483 r = !my_GetFileInformationByName(pathname, &bhfi);
1484 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1489 r = lstat(pathname, &st);
1490 filesize = st.st_size;
1493 if (r == 0 && filesize == size)
1495 failure_start(file, line, "File %s has size %ld, expected %ld",
1496 pathname, (long)filesize, (long)size);
1497 failure_finish(NULL);
1501 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1503 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1508 #if defined(_WIN32) && !defined(__CYGWIN__)
1509 (void)mode; /* UNUSED */
1511 assertion_count(file, line);
1512 r = lstat(pathname, &st);
1514 failure_start(file, line, "Dir should exist: %s", pathname);
1515 failure_finish(NULL);
1518 if (!S_ISDIR(st.st_mode)) {
1519 failure_start(file, line, "%s is not a dir", pathname);
1520 failure_finish(NULL);
1523 #if !defined(_WIN32) || defined(__CYGWIN__)
1524 /* Windows doesn't handle permissions the same way as POSIX,
1525 * so just ignore the mode tests. */
1526 /* TODO: Can we do better here? */
1527 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1528 failure_start(file, line, "Dir %s has wrong mode", pathname);
1529 logprintf(" Expected: 0%3o\n", mode);
1530 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1531 failure_finish(NULL);
1538 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1539 * verify that too. */
1541 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1546 #if defined(_WIN32) && !defined(__CYGWIN__)
1547 (void)mode; /* UNUSED */
1549 assertion_count(file, line);
1550 r = lstat(pathname, &st);
1551 if (r != 0 || !S_ISREG(st.st_mode)) {
1552 failure_start(file, line, "File should exist: %s", pathname);
1553 failure_finish(NULL);
1556 #if !defined(_WIN32) || defined(__CYGWIN__)
1557 /* Windows doesn't handle permissions the same way as POSIX,
1558 * so just ignore the mode tests. */
1559 /* TODO: Can we do better here? */
1560 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1561 failure_start(file, line, "File %s has wrong mode", pathname);
1562 logprintf(" Expected: 0%3o\n", mode);
1563 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1564 failure_finish(NULL);
1571 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1572 * non-NULL, verify that the symlink has those contents. */
1574 is_symlink(const char *file, int line,
1575 const char *pathname, const char *contents)
1577 #if defined(_WIN32) && !defined(__CYGWIN__)
1578 (void)pathname; /* UNUSED */
1579 (void)contents; /* UNUSED */
1580 assertion_count(file, line);
1581 /* Windows sort-of has real symlinks, but they're only usable
1582 * by privileged users and are crippled even then, so there's
1583 * really not much point in bothering with this. */
1591 assertion_count(file, line);
1592 r = lstat(pathname, &st);
1594 failure_start(file, line,
1595 "Symlink should exist: %s", pathname);
1596 failure_finish(NULL);
1599 if (!S_ISLNK(st.st_mode))
1601 if (contents == NULL)
1603 linklen = readlink(pathname, buff, sizeof(buff));
1605 failure_start(file, line, "Can't read symlink %s", pathname);
1606 failure_finish(NULL);
1609 buff[linklen] = '\0';
1610 if (strcmp(buff, contents) != 0)
1616 /* Assert that path is a symlink that (optionally) contains contents. */
1618 assertion_is_symlink(const char *file, int line,
1619 const char *path, const char *contents)
1621 if (is_symlink(file, line, path, contents))
1624 failure_start(file, line, "File %s is not a symlink to %s",
1627 failure_start(file, line, "File %s is not a symlink", path);
1628 failure_finish(NULL);
1633 /* Create a directory and report any errors. */
1635 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1637 assertion_count(file, line);
1638 #if defined(_WIN32) && !defined(__CYGWIN__)
1639 (void)mode; /* UNUSED */
1640 if (0 == _mkdir(dirname))
1643 if (0 == mkdir(dirname, mode)) {
1644 if (0 == chmod(dirname, mode)) {
1645 assertion_file_mode(file, line, dirname, mode);
1650 failure_start(file, line, "Could not create directory %s", dirname);
1651 failure_finish(NULL);
1655 /* Create a file with the specified contents and report any failures. */
1657 assertion_make_file(const char *file, int line,
1658 const char *path, int mode, int csize, const void *contents)
1660 #if defined(_WIN32) && !defined(__CYGWIN__)
1661 /* TODO: Rework this to set file mode as well. */
1663 (void)mode; /* UNUSED */
1664 assertion_count(file, line);
1665 f = fopen(path, "wb");
1667 failure_start(file, line, "Could not create file %s", path);
1668 failure_finish(NULL);
1671 if (contents != NULL) {
1675 wsize = strlen(contents);
1677 wsize = (size_t)csize;
1678 if (wsize != fwrite(contents, 1, wsize, f)) {
1680 failure_start(file, line,
1681 "Could not write file %s", path);
1682 failure_finish(NULL);
1690 assertion_count(file, line);
1691 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1693 failure_start(file, line, "Could not create %s", path);
1694 failure_finish(NULL);
1697 if (0 != chmod(path, mode)) {
1698 failure_start(file, line, "Could not chmod %s", path);
1699 failure_finish(NULL);
1703 if (contents != NULL) {
1707 wsize = (ssize_t)strlen(contents);
1709 wsize = (ssize_t)csize;
1710 if (wsize != write(fd, contents, wsize)) {
1712 failure_start(file, line,
1713 "Could not write to %s", path);
1714 failure_finish(NULL);
1720 assertion_file_mode(file, line, path, mode);
1725 /* Create a hardlink and report any failures. */
1727 assertion_make_hardlink(const char *file, int line,
1728 const char *newpath, const char *linkto)
1732 assertion_count(file, line);
1733 #if defined(_WIN32) && !defined(__CYGWIN__)
1734 succeeded = my_CreateHardLinkA(newpath, linkto);
1736 succeeded = !link(linkto, newpath);
1742 failure_start(file, line, "Could not create hardlink");
1743 logprintf(" New link: %s\n", newpath);
1744 logprintf(" Old name: %s\n", linkto);
1745 failure_finish(NULL);
1749 /* Create a symlink and report any failures. */
1751 assertion_make_symlink(const char *file, int line,
1752 const char *newpath, const char *linkto)
1754 #if defined(_WIN32) && !defined(__CYGWIN__)
1755 int targetIsDir = 0; /* TODO: Fix this */
1756 assertion_count(file, line);
1757 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1760 assertion_count(file, line);
1761 if (0 == symlink(linkto, newpath))
1764 failure_start(file, line, "Could not create symlink");
1765 logprintf(" New link: %s\n", newpath);
1766 logprintf(" Old name: %s\n", linkto);
1767 failure_finish(NULL);
1771 /* Set umask, report failures. */
1773 assertion_umask(const char *file, int line, int mask)
1775 assertion_count(file, line);
1776 (void)file; /* UNUSED */
1777 (void)line; /* UNUSED */
1782 /* Set times, report failures. */
1784 assertion_utimes(const char *file, int line,
1785 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1789 #if defined(_WIN32) && !defined(__CYGWIN__)
1790 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1791 + (((nsec)/1000)*10))
1793 ULARGE_INTEGER wintm;
1794 FILETIME fatime, fmtime;
1795 FILETIME *pat, *pmt;
1797 assertion_count(file, line);
1798 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1799 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1800 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1801 if (h == INVALID_HANDLE_VALUE) {
1802 failure_start(file, line, "Can't access %s\n", pathname);
1803 failure_finish(NULL);
1807 if (at > 0 || at_nsec > 0) {
1808 wintm.QuadPart = WINTIME(at, at_nsec);
1809 fatime.dwLowDateTime = wintm.LowPart;
1810 fatime.dwHighDateTime = wintm.HighPart;
1814 if (mt > 0 || mt_nsec > 0) {
1815 wintm.QuadPart = WINTIME(mt, mt_nsec);
1816 fmtime.dwLowDateTime = wintm.LowPart;
1817 fmtime.dwHighDateTime = wintm.HighPart;
1821 if (pat != NULL || pmt != NULL)
1822 r = SetFileTime(h, NULL, pat, pmt);
1827 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1828 failure_finish(NULL);
1832 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1834 struct timeval times[2];
1836 #if !defined(__FreeBSD__)
1837 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1839 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1842 r = lstat(pathname, &st);
1844 failure_start(file, line, "Can't stat %s\n", pathname);
1845 failure_finish(NULL);
1849 if (mt == 0 && mt_nsec == 0) {
1851 #if defined(__FreeBSD__)
1852 mt_nsec = st.st_mtimespec.tv_nsec;
1853 /* FreeBSD generally only stores to microsecond res, so round. */
1854 mt_nsec = (mt_nsec / 1000) * 1000;
1857 if (at == 0 && at_nsec == 0) {
1859 #if defined(__FreeBSD__)
1860 at_nsec = st.st_atimespec.tv_nsec;
1861 /* FreeBSD generally only stores to microsecond res, so round. */
1862 at_nsec = (at_nsec / 1000) * 1000;
1866 times[1].tv_sec = mt;
1867 times[1].tv_usec = mt_nsec / 1000;
1869 times[0].tv_sec = at;
1870 times[0].tv_usec = at_nsec / 1000;
1873 r = lutimes(pathname, times);
1875 r = utimes(pathname, times);
1878 failure_start(file, line, "Can't utimes %s\n", pathname);
1879 failure_finish(NULL);
1883 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1886 /* Set nodump, report failures. */
1888 assertion_nodump(const char *file, int line, const char *pathname)
1890 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1893 assertion_count(file, line);
1894 r = chflags(pathname, UF_NODUMP);
1896 failure_start(file, line, "Can't set nodump %s\n", pathname);
1897 failure_finish(NULL);
1900 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
1901 defined(FS_NODUMP_FL)) || \
1902 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
1903 && defined(EXT2_NODUMP_FL))
1906 assertion_count(file, line);
1907 fd = open(pathname, O_RDONLY | O_NONBLOCK);
1909 failure_start(file, line, "Can't open %s\n", pathname);
1910 failure_finish(NULL);
1914 #ifdef FS_IOC_GETFLAGS
1921 failure_start(file, line, "Can't get flags %s\n", pathname);
1922 failure_finish(NULL);
1926 flags |= FS_NODUMP_FL;
1928 flags |= EXT2_NODUMP_FL;
1932 #ifdef FS_IOC_SETFLAGS
1939 failure_start(file, line, "Can't set nodump %s\n", pathname);
1940 failure_finish(NULL);
1945 (void)pathname; /* UNUSED */
1946 assertion_count(file, line);
1952 static void assert_version_id(char **qq, size_t *ss)
1957 /* Version number is a series of digits and periods. */
1958 while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
1963 if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') {
1968 /* Skip a single trailing a,b,c, or d. */
1969 if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
1972 /* Version number terminated by space. */
1973 failure("No space after version: ``%s''", q);
1975 failure("No space after version: ``%s''", q);
1986 * Check program version
1988 void assertVersion(const char *prog, const char *base)
1993 unsigned int prog_len = strlen(base);
1995 r = systemf("%s --version >version.stdout 2>version.stderr", prog);
1997 r = systemf("%s -W version >version.stdout 2>version.stderr",
2000 failure("Unable to run either %s --version or %s -W version",
2002 if (!assert(r == 0))
2005 /* --version should generate nothing to stdout. */
2006 assertEmptyFile("version.stderr");
2008 /* Verify format of version message. */
2009 q = p = slurpfile(&s, "version.stdout");
2011 /* Version message should start with name of program, then space. */
2012 assert(s > prog_len + 1);
2014 failure("Version must start with '%s': ``%s''", base, p);
2015 if (!assertEqualMem(q, base, prog_len)) {
2020 q += prog_len; s -= prog_len;
2025 assert_version_id(&q, &s);
2028 failure("No `-' between program name and versions: ``%s''", p);
2029 assertEqualMem(q, "- ", 2);
2032 failure("Not long enough for libarchive version: ``%s''", p);
2035 failure("Libarchive version must start with `libarchive': ``%s''", p);
2036 assertEqualMem(q, "libarchive ", 11);
2040 assert_version_id(&q, &s);
2042 /* Skip arbitrary third-party version numbers. */
2043 while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' ||
2049 /* All terminated by end-of-line. */
2052 /* Skip an optional CR character (e.g., Windows) */
2053 failure("Version output must end with \\n or \\r\\n");
2055 if (*q == '\r') { ++q; --s; }
2056 assertEqualMem(q, "\n", 1);
2060 #endif /* PROGRAM */
2064 * UTILITIES for use by tests.
2069 * Check whether platform supports symlinks. This is intended
2070 * for tests to use in deciding whether to bother testing symlink
2071 * support; if the platform doesn't support symlinks, there's no point
2072 * in checking whether the program being tested can create them.
2074 * Note that the first time this test is called, we actually go out to
2075 * disk to create and verify a symlink. This is necessary because
2076 * symlink support is actually a property of a particular filesystem
2077 * and can thus vary between directories on a single system. After
2078 * the first call, this returns the cached result from memory, so it's
2079 * safe to call it as often as you wish.
2084 /* Remember the test result */
2085 static int value = 0, tested = 0;
2090 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
2091 /* Note: Cygwin has its own symlink() emulation that does not
2092 * use the Win32 CreateSymbolicLink() function. */
2093 #if defined(_WIN32) && !defined(__CYGWIN__)
2094 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
2095 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
2097 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
2098 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
2103 /* Platform-dependent options for hiding the output of a subcommand. */
2104 #if defined(_WIN32) && !defined(__CYGWIN__)
2105 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
2107 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
2110 * Can this platform run the bzip2 program?
2115 static int tested = 0, value = 0;
2118 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
2125 * Can this platform run the grzip program?
2130 static int tested = 0, value = 0;
2133 if (systemf("grzip -V %s", redirectArgs) == 0)
2140 * Can this platform run the gzip program?
2145 static int tested = 0, value = 0;
2148 if (systemf("gzip -V %s", redirectArgs) == 0)
2155 * Can this platform run the lrzip program?
2158 canRunCommand(const char *cmd)
2160 static int tested = 0, value = 0;
2163 if (systemf("%s %s", cmd, redirectArgs) == 0)
2172 static int tested = 0, value = 0;
2175 if (systemf("lrzip -V %s", redirectArgs) == 0)
2182 * Can this platform run the lz4 program?
2187 static int tested = 0, value = 0;
2190 if (systemf("lz4 -V %s", redirectArgs) == 0)
2197 * Can this platform run the lzip program?
2202 static int tested = 0, value = 0;
2205 if (systemf("lzip -V %s", redirectArgs) == 0)
2212 * Can this platform run the lzma program?
2217 static int tested = 0, value = 0;
2220 if (systemf("lzma -V %s", redirectArgs) == 0)
2227 * Can this platform run the lzop program?
2232 static int tested = 0, value = 0;
2235 if (systemf("lzop -V %s", redirectArgs) == 0)
2242 * Can this platform run the xz program?
2247 static int tested = 0, value = 0;
2250 if (systemf("xz -V %s", redirectArgs) == 0)
2257 * Can this filesystem handle nodump flags.
2259 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2264 const char *path = "cannodumptest";
2267 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2268 if (chflags(path, UF_NODUMP) < 0)
2270 if (stat(path, &sb) < 0)
2272 if (sb.st_flags & UF_NODUMP)
2277 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \
2278 && defined(FS_NODUMP_FL)) || \
2279 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2280 && defined(EXT2_NODUMP_FL))
2284 const char *path = "cannodumptest";
2287 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2288 fd = open(path, O_RDONLY | O_NONBLOCK);
2292 #ifdef FS_IOC_GETFLAGS
2301 flags |= FS_NODUMP_FL;
2303 flags |= EXT2_NODUMP_FL;
2306 #ifdef FS_IOC_SETFLAGS
2315 fd = open(path, O_RDONLY | O_NONBLOCK);
2319 #ifdef FS_IOC_GETFLAGS
2329 if (flags & FS_NODUMP_FL)
2331 if (flags & EXT2_NODUMP_FL)
2348 * Sleep as needed; useful for verifying disk timestamp changes by
2349 * ensuring that the wall-clock time has actually changed before we
2350 * go back to re-read something from disk.
2353 sleepUntilAfter(time_t t)
2355 while (t >= time(NULL))
2356 #if defined(_WIN32) && !defined(__CYGWIN__)
2364 * Call standard system() call, but build up the command line using
2365 * sprintf() conventions.
2368 systemf(const char *fmt, ...)
2375 vsprintf(buff, fmt, ap);
2376 if (verbosity > VERBOSITY_FULL)
2377 logprintf("Cmd: %s\n", buff);
2384 * Slurp a file into memory for ease of comparison and testing.
2385 * Returns size of file in 'sizep' if non-NULL, null-terminates
2386 * data in memory for ease of use.
2389 slurpfile(size_t * sizep, const char *fmt, ...)
2391 char filename[8192];
2400 vsprintf(filename, fmt, ap);
2403 f = fopen(filename, "rb");
2405 /* Note: No error; non-existent file is okay here. */
2408 r = fstat(fileno(f), &st);
2410 logprintf("Can't stat file %s\n", filename);
2414 p = malloc((size_t)st.st_size + 1);
2416 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2417 (long int)st.st_size, filename);
2421 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2422 if (bytes_read < st.st_size) {
2423 logprintf("Can't read file %s\n", filename);
2428 p[st.st_size] = '\0';
2430 *sizep = (size_t)st.st_size;
2436 * Slurp a file into memory for ease of comparison and testing.
2437 * Returns size of file in 'sizep' if non-NULL, null-terminates
2438 * data in memory for ease of use.
2441 dumpfile(const char *filename, void *data, size_t len)
2443 ssize_t bytes_written;
2446 f = fopen(filename, "wb");
2448 logprintf("Can't open file %s for writing\n", filename);
2451 bytes_written = fwrite(data, 1, len, f);
2452 if (bytes_written < (ssize_t)len)
2453 logprintf("Can't write file %s\n", filename);
2457 /* Read a uuencoded file from the reference directory, decode, and
2458 * write the result into the current directory. */
2459 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2460 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2462 extract_reference_file(const char *name)
2467 sprintf(buff, "%s/%s.uu", refdir, name);
2468 in = fopen(buff, "r");
2469 failure("Couldn't open reference file %s", buff);
2473 /* Read up to and including the 'begin' line. */
2475 if (fgets(buff, sizeof(buff), in) == NULL) {
2476 /* TODO: This is a failure. */
2479 if (memcmp(buff, "begin ", 6) == 0)
2482 /* Now, decode the rest and write it. */
2483 out = fopen(name, "wb");
2484 while (fgets(buff, sizeof(buff), in) != NULL) {
2488 if (memcmp(buff, "end", 3) == 0)
2491 bytes = UUDECODE(*p++);
2494 /* Write out 1-3 bytes from that. */
2496 assert(VALID_UUDECODE(p[0]));
2497 assert(VALID_UUDECODE(p[1]));
2498 n = UUDECODE(*p++) << 18;
2499 n |= UUDECODE(*p++) << 12;
2500 fputc(n >> 16, out);
2504 assert(VALID_UUDECODE(p[0]));
2505 n |= UUDECODE(*p++) << 6;
2506 fputc((n >> 8) & 0xFF, out);
2510 assert(VALID_UUDECODE(p[0]));
2511 n |= UUDECODE(*p++);
2512 fputc(n & 0xFF, out);
2522 copy_reference_file(const char *name)
2528 sprintf(buff, "%s/%s", refdir, name);
2529 in = fopen(buff, "rb");
2530 failure("Couldn't open reference file %s", buff);
2534 /* Now, decode the rest and write it. */
2535 /* Not a lot of error checking here; the input better be right. */
2536 out = fopen(name, "wb");
2537 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2538 if (fwrite(buff, 1, rbytes, out) != rbytes) {
2539 logprintf("Error: fwrite\n");
2548 is_LargeInode(const char *file)
2550 #if defined(_WIN32) && !defined(__CYGWIN__)
2551 BY_HANDLE_FILE_INFORMATION bhfi;
2554 r = my_GetFileInformationByName(file, &bhfi);
2557 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2562 if (stat(file, &st) < 0)
2564 ino = (int64_t)st.st_ino;
2565 return (ino > 0xffffffff);
2570 extract_reference_files(const char **names)
2572 while (names && *names)
2573 extract_reference_file(*names++);
2579 assertion_entry_set_acls(const char *file, int line, struct archive_entry *ae,
2580 struct archive_test_acl_t *acls, int n)
2584 assertion_count(file, line);
2587 archive_entry_acl_clear(ae);
2588 for (i = 0; i < n; i++) {
2589 r = archive_entry_acl_add_entry(ae,
2590 acls[i].type, acls[i].permset, acls[i].tag,
2591 acls[i].qual, acls[i].name);
2594 failure_start(file, line, "type=%#010x, ",
2595 "permset=%#010x, tag=%d, qual=%d name=%s",
2596 acls[i].type, acls[i].permset, acls[i].tag,
2597 acls[i].qual, acls[i].name);
2598 failure_finish(NULL);
2606 archive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset,
2607 int tag, int qual, const char *name)
2609 if (type != acl->type)
2611 if (permset != acl->permset)
2613 if (tag != acl->tag)
2615 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
2617 if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
2619 if (tag == ARCHIVE_ENTRY_ACL_EVERYONE)
2621 if (tag == ARCHIVE_ENTRY_ACL_OTHER)
2623 if (qual != acl->qual)
2626 if (acl->name == NULL || acl->name[0] == '\0')
2630 if (acl->name == NULL) {
2631 if (name[0] == '\0')
2635 return (0 == strcmp(name, acl->name));
2640 assertion_entry_compare_acls(const char *file, int line,
2641 struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt,
2642 int want_type, int mode)
2646 int type, permset, tag, qual;
2650 assertion_count(file, line);
2654 marker = malloc(sizeof(marker[0]) * cnt);
2656 for (i = 0; i < cnt; i++) {
2657 if ((acls[i].type & want_type) != 0) {
2664 failure_start(file, line, "No ACL's to compare, type mask: %d",
2669 while (0 == (r = archive_entry_acl_next(ae, want_type,
2670 &type, &permset, &tag, &qual, &name))) {
2671 for (i = 0, matched = 0; i < n && !matched; i++) {
2672 if (archive_test_acl_match(&acls[marker[i]], type,
2673 permset, tag, qual, name)) {
2674 /* We found a match; remove it. */
2675 marker[i] = marker[n - 1];
2680 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
2681 && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
2683 failure_start(file, line, "No match for "
2685 failure_finish(NULL);
2688 if ((permset << 6) != (mode & 0700)) {
2689 failure_start(file, line, "USER_OBJ permset "
2690 "(%02o) != user mode (%02o)", permset,
2692 failure_finish(NULL);
2695 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
2696 && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
2698 failure_start(file, line, "No match for "
2700 failure_finish(NULL);
2703 if ((permset << 3) != (mode & 0070)) {
2704 failure_start(file, line, "GROUP_OBJ permset "
2705 "(%02o) != group mode (%02o)", permset,
2707 failure_finish(NULL);
2710 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
2711 && tag == ARCHIVE_ENTRY_ACL_OTHER) {
2713 failure_start(file, line, "No match for "
2715 failure_finish(NULL);
2718 if ((permset << 0) != (mode & 0007)) {
2719 failure_start(file, line, "OTHER permset "
2720 "(%02o) != other mode (%02o)", permset,
2722 failure_finish(NULL);
2725 } else if (matched != 1) {
2726 failure_start(file, line, "Could not find match for "
2727 "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d,"
2728 "name=``%s'')", type, permset, tag, qual, name);
2729 failure_finish(NULL);
2733 if (r != ARCHIVE_EOF) {
2734 failure_start(file, line, "Should not exit before EOF");
2735 failure_finish(NULL);
2738 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 &&
2739 (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) {
2740 failure_start(file, line, "Mode (%02o) and entry mode (%02o) "
2741 "mismatch", mode, archive_entry_mode(ae));
2742 failure_finish(NULL);
2746 failure_start(file, line, "Could not find match for ACL "
2747 "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')",
2748 acls[marker[0]].type, acls[marker[0]].permset,
2749 acls[marker[0]].tag, acls[marker[0]].qual,
2750 acls[marker[0]].name);
2751 failure_finish(NULL);
2753 /* Number of ACLs not matched should == 0 */
2758 #endif /* !defined(PROGRAM) */
2767 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2769 * DEFINE_TEST(test_function)
2773 /* Use "list.h" to declare all of the test functions. */
2775 #define DEFINE_TEST(name) void name(void);
2778 /* Use "list.h" to create a list of all tests (functions and names). */
2780 #define DEFINE_TEST(n) { n, #n, 0 },
2781 struct test_list_t tests[] = {
2786 * Summarize repeated failures in the just-completed test.
2789 test_summarize(int failed, int skips_num)
2793 switch (verbosity) {
2794 case VERBOSITY_SUMMARY_ONLY:
2795 printf(failed ? "E" : ".");
2798 case VERBOSITY_PASSFAIL:
2799 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
2803 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2805 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2806 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2807 logprintf("%s:%d: Summary: Failed %d times\n",
2808 failed_filename, i, failed_lines[i].count);
2810 /* Clear the failure history for the next file. */
2811 failed_filename = NULL;
2812 memset(failed_lines, 0, sizeof(failed_lines));
2816 * Actually run a single test, with appropriate setup and cleanup.
2819 test_run(int i, const char *tmpdir)
2822 char logfilename[64];
2823 int failures_before = failures;
2824 int skips_before = skips;
2827 switch (verbosity) {
2828 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2830 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2831 printf("%3d: %-64s", i, tests[i].name);
2834 default: /* Title of test, details will follow */
2835 printf("%3d: %s\n", i, tests[i].name);
2838 /* Chdir to the top-level work directory. */
2839 if (!assertChdir(tmpdir)) {
2841 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2844 /* Create a log file for this test. */
2845 sprintf(logfilename, "%s.log", tests[i].name);
2846 logfile = fopen(logfilename, "w");
2847 fprintf(logfile, "%s\n\n", tests[i].name);
2848 /* Chdir() to a work dir for this specific test. */
2849 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2850 testworkdir = workdir;
2851 if (!assertMakeDir(testworkdir, 0755)
2852 || !assertChdir(testworkdir)) {
2854 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2857 /* Explicitly reset the locale before each test. */
2858 setlocale(LC_ALL, "C");
2859 /* Record the umask before we run the test. */
2860 umask(oldumask = umask(0));
2862 * Run the actual test.
2866 * Clean up and report afterwards.
2872 setlocale(LC_ALL, "C");
2873 /* Reset directory. */
2874 if (!assertChdir(tmpdir)) {
2875 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2879 /* Report per-test summaries. */
2880 tests[i].failures = failures - failures_before;
2881 test_summarize(tests[i].failures, skips - skips_before);
2882 /* Close the per-test log file. */
2885 /* If there were no failures, we can remove the work dir and logfile. */
2886 if (tests[i].failures == 0) {
2887 if (!keep_temp_files && assertChdir(tmpdir)) {
2888 #if defined(_WIN32) && !defined(__CYGWIN__)
2889 /* Make sure not to leave empty directories.
2890 * Sometimes a processing of closing files used by tests
2891 * is not done, then rmdir will be failed and it will
2892 * leave a empty test directory. So we should wait a few
2893 * seconds and retry rmdir. */
2895 for (t = 0; t < 10; t++) {
2898 r = systemf("rmdir /S /Q %s", tests[i].name);
2902 systemf("del %s", logfilename);
2904 systemf("rm -rf %s", tests[i].name);
2905 systemf("rm %s", logfilename);
2909 /* Return appropriate status. */
2910 return (tests[i].failures);
2916 * MAIN and support routines.
2922 usage(const char *program)
2924 static const int limit = sizeof(tests) / sizeof(tests[0]);
2927 printf("Usage: %s [options] <test> <test> ...\n", program);
2928 printf("Default is to run all tests.\n");
2929 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2930 printf("Options:\n");
2931 printf(" -d Dump core after any failure, for debugging.\n");
2932 printf(" -k Keep all temp files.\n");
2933 printf(" Default: temp files for successful tests deleted.\n");
2935 printf(" -p <path> Path to executable to be tested.\n");
2936 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2938 printf(" -q Quiet.\n");
2939 printf(" -r <dir> Path to dir containing reference files.\n");
2940 printf(" Default: Current directory.\n");
2941 printf(" -u Keep running specifies tests until one fails.\n");
2942 printf(" -v Verbose.\n");
2943 printf("Available tests:\n");
2944 for (i = 0; i < limit; i++)
2945 printf(" %d: %s\n", i, tests[i].name);
2950 get_refdir(const char *d)
2952 size_t tried_size, buff_size;
2953 char *buff, *tried, *pwd = NULL, *p = NULL;
2956 buff_size = PATH_MAX;
2960 buff = calloc(buff_size, 1);
2962 fprintf(stderr, "Unable to allocate memory\n");
2966 /* Allocate a buffer to hold the various directories we checked. */
2967 tried_size = buff_size * 2;
2968 tried = calloc(tried_size, 1);
2969 if (tried == NULL) {
2970 fprintf(stderr, "Unable to allocate memory\n");
2974 /* If a dir was specified, try that */
2977 snprintf(buff, buff_size, "%s", d);
2978 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2979 if (p != NULL) goto success;
2980 strncat(tried, buff, tried_size - strlen(tried) - 1);
2981 strncat(tried, "\n", tried_size - strlen(tried) - 1);
2985 /* Get the current dir. */
2987 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2989 pwd = getcwd(NULL, 0);
2991 while (pwd[strlen(pwd) - 1] == '\n')
2992 pwd[strlen(pwd) - 1] = '\0';
2994 /* Look for a known file. */
2995 snprintf(buff, buff_size, "%s", pwd);
2996 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2997 if (p != NULL) goto success;
2998 strncat(tried, buff, tried_size - strlen(tried) - 1);
2999 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3001 snprintf(buff, buff_size, "%s/test", pwd);
3002 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3003 if (p != NULL) goto success;
3004 strncat(tried, buff, tried_size - strlen(tried) - 1);
3005 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3007 #if defined(LIBRARY)
3008 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
3010 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
3012 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3013 if (p != NULL) goto success;
3014 strncat(tried, buff, tried_size - strlen(tried) - 1);
3015 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3017 #if defined(PROGRAM_ALIAS)
3018 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
3019 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3020 if (p != NULL) goto success;
3021 strncat(tried, buff, tried_size - strlen(tried) - 1);
3022 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3025 if (memcmp(pwd, "/usr/obj", 8) == 0) {
3026 snprintf(buff, buff_size, "%s", pwd + 8);
3027 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3028 if (p != NULL) goto success;
3029 strncat(tried, buff, tried_size - strlen(tried) - 1);
3030 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3032 snprintf(buff, buff_size, "%s/test", pwd + 8);
3033 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3034 if (p != NULL) goto success;
3035 strncat(tried, buff, tried_size - strlen(tried) - 1);
3036 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3040 printf("Unable to locate known reference file %s\n", KNOWNREF);
3041 printf(" Checked following directories:\n%s\n", tried);
3042 printf("Use -r option to specify full path to reference directory\n");
3043 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
3053 /* Copy result into a fresh buffer to reduce memory usage. */
3060 main(int argc, char **argv)
3062 static const int limit = sizeof(tests) / sizeof(tests[0]);
3063 int test_set[sizeof(tests) / sizeof(tests[0])];
3064 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
3066 char *refdir_alloc = NULL;
3067 const char *progname;
3069 const char *tmp, *option_arg, *p;
3070 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
3071 char tmpdir_timestamp[256];
3073 (void)argc; /* UNUSED */
3075 /* Get the current dir. */
3077 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3079 pwd = getcwd(NULL, 0);
3081 while (pwd[strlen(pwd) - 1] == '\n')
3082 pwd[strlen(pwd) - 1] = '\0';
3084 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
3085 /* To stop to run the default invalid parameter handler. */
3086 _set_invalid_parameter_handler(invalid_parameter_handler);
3087 /* Disable annoying assertion message box. */
3088 _CrtSetReportMode(_CRT_ASSERT, 0);
3092 * Name of this program, used to build root of our temp directory
3095 progname = p = argv[0];
3096 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
3098 fprintf(stderr, "ERROR: Out of memory.");
3101 strcpy(testprogdir, progname);
3102 while (*p != '\0') {
3103 /* Support \ or / dir separators for Windows compat. */
3104 if (*p == '/' || *p == '\\')
3112 testprogdir[i] = '\0';
3113 #if defined(_WIN32) && !defined(__CYGWIN__)
3114 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
3115 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
3116 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
3117 testprogdir[1] == ':' &&
3118 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
3120 if (testprogdir[0] != '/')
3123 /* Fixup path for relative directories. */
3124 if ((testprogdir = (char *)realloc(testprogdir,
3125 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
3127 fprintf(stderr, "ERROR: Out of memory.");
3130 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
3131 strlen(testprogdir) + 1);
3132 memcpy(testprogdir, pwd, strlen(pwd));
3133 testprogdir[strlen(pwd)] = '/';
3137 /* Get the target program from environment, if available. */
3138 testprogfile = getenv(ENVBASE);
3141 if (getenv("TMPDIR") != NULL)
3142 tmp = getenv("TMPDIR");
3143 else if (getenv("TMP") != NULL)
3144 tmp = getenv("TMP");
3145 else if (getenv("TEMP") != NULL)
3146 tmp = getenv("TEMP");
3147 else if (getenv("TEMPDIR") != NULL)
3148 tmp = getenv("TEMPDIR");
3152 /* Allow -d to be controlled through the environment. */
3153 if (getenv(ENVBASE "_DEBUG") != NULL)
3154 dump_on_failure = 1;
3156 /* Allow -v to be controlled through the environment. */
3157 if (getenv("_VERBOSITY_LEVEL") != NULL)
3159 vlevel = getenv("_VERBOSITY_LEVEL");
3160 verbosity = atoi(vlevel);
3161 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
3163 /* Unsupported verbosity levels are silently ignored */
3165 verbosity = VERBOSITY_PASSFAIL;
3169 /* Get the directory holding test files from environment. */
3170 refdir = getenv(ENVBASE "_TEST_FILES");
3173 * Parse options, without using getopt(), which isn't available
3176 ++argv; /* Skip program name */
3177 while (*argv != NULL) {
3182 while (*p != '\0') {
3185 /* If 'opt' takes an argument, parse that. */
3186 if (option == 'p' || option == 'r') {
3189 else if (*argv == NULL) {
3191 "Option -%c requires argument.\n",
3195 option_arg = *argv++;
3196 p = ""; /* End of this option word. */
3199 /* Now, handle the option. */
3202 dump_on_failure = 1;
3205 keep_temp_files = 1;
3209 testprogfile = option_arg;
3211 fprintf(stderr, "-p option not permitted\n");
3220 refdir = option_arg;
3230 fprintf(stderr, "Unrecognized option '%c'\n",
3238 * Sanity-check that our options make sense.
3241 if (testprogfile == NULL)
3243 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
3244 strlen(PROGRAM) + 1)) == NULL)
3246 fprintf(stderr, "ERROR: Out of memory.");
3249 strcpy(tmp2, testprogdir);
3251 strcat(tmp2, PROGRAM);
3252 testprogfile = tmp2;
3257 #if defined(_WIN32) && !defined(__CYGWIN__)
3258 /* Command.com sometimes rejects '/' separators. */
3259 testprg = strdup(testprogfile);
3260 for (i = 0; testprg[i] != '\0'; i++) {
3261 if (testprg[i] == '/')
3264 testprogfile = testprg;
3266 /* Quote the name that gets put into shell command lines. */
3267 testprg = malloc(strlen(testprogfile) + 3);
3268 strcpy(testprg, "\"");
3269 strcat(testprg, testprogfile);
3270 strcat(testprg, "\"");
3275 #if !defined(_WIN32) && defined(SIGPIPE)
3276 { /* Ignore SIGPIPE signals */
3277 struct sigaction sa;
3278 sa.sa_handler = SIG_IGN;
3279 sigemptyset(&sa.sa_mask);
3281 sigaction(SIGPIPE, &sa, NULL);
3286 * Create a temp directory for the following tests.
3287 * Include the time the tests started as part of the name,
3288 * to make it easier to track the results of multiple tests.
3291 for (i = 0; ; i++) {
3292 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
3293 "%Y-%m-%dT%H.%M.%S",
3295 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
3296 tmpdir_timestamp, i);
3297 if (assertMakeDir(tmpdir,0755))
3301 "ERROR: Unable to create temp directory %s\n",
3308 * If the user didn't specify a directory for locating
3309 * reference files, try to find the reference files in
3310 * the "usual places."
3312 refdir = refdir_alloc = get_refdir(refdir);
3315 * Banner with basic information.
3318 printf("If tests fail or crash, details will be in:\n");
3319 printf(" %s\n", tmpdir);
3321 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3322 printf("Reference files will be read from: %s\n", refdir);
3324 printf("Running tests on: %s\n", testprog);
3326 printf("Exercising: ");
3328 printf("%s\n", EXTRA_VERSION);
3335 * Run some or all of the individual tests.
3343 test_num = get_test_set(test_set, limit, *argv, tests);
3345 printf("*** INVALID Test %s\n", *argv);
3351 for (i = 0; i < test_num; i++) {
3353 if (test_run(test_set[i], tmpdir)) {
3361 } while (*argv != NULL);
3362 } while (until_failure);
3365 /* Must be freed after all tests run */
3371 * Report summary statistics.
3373 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3375 printf("Totals:\n");
3376 printf(" Tests run: %8d\n", tests_run);
3377 printf(" Tests failed: %8d\n", tests_failed);
3378 printf(" Assertions checked:%8d\n", assertions);
3379 printf(" Assertions failed: %8d\n", failures);
3380 printf(" Skips reported: %8d\n", skips);
3384 printf("Failing tests:\n");
3385 for (i = 0; i < limit; ++i) {
3386 if (tests[i].failures)
3387 printf(" %d: %s (%d failures)\n", i,
3388 tests[i].name, tests[i].failures);
3391 printf("Details for failing tests: %s\n", tmpdir);
3394 if (verbosity == VERBOSITY_SUMMARY_ONLY)
3396 printf("%d tests passed, no failures\n", tests_run);
3401 /* If the final tmpdir is empty, we can remove it. */
3402 /* This should be the usual case when all tests succeed. */
3406 return (tests_failed ? 1 : 0);