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 #ifdef HAVE_ACL_LIBACL_H
62 #include <acl/libacl.h>
64 #ifdef HAVE_SYS_TYPES_H
65 #include <sys/types.h>
73 #ifdef HAVE_SYS_EXTATTR_H
74 #include <sys/extattr.h>
77 #include <sys/xattr.h>
78 #elif HAVE_ATTR_XATTR_H
79 #include <attr/xattr.h>
81 #ifdef HAVE_SYS_RICHACL_H
82 #include <sys/richacl.h>
85 #include <membership.h>
90 * Windows support routines
92 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
93 * in the test harness is dangerous because they cover up
94 * configuration errors. The classic example of this is omitting a
95 * configure check. If libarchive and libarchive_test both look for
96 * the same feature macro, such errors are hard to detect. Platform
97 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
98 * easily lead to very messy code. It's best to limit yourself
99 * to only the most generic programming techniques in the test harness
100 * and thus avoid conditionals altogether. Where that's not possible,
101 * try to minimize conditionals by grouping platform-specific tests in
102 * one place (e.g., test_acl_freebsd) or by adding new assert()
103 * functions (e.g., assertMakeHardlink()) to cover up platform
104 * differences. Platform-specific coding in libarchive_test is often
105 * a symptom that some capability is missing from libarchive itself.
107 #if defined(_WIN32) && !defined(__CYGWIN__)
115 #define S_ISDIR(m) ((m) & _S_IFDIR)
118 #define S_ISREG(m) ((m) & _S_IFREG)
120 #if !defined(__BORLANDC__)
121 #define access _access
126 #define fileno _fileno
128 /*#define fstat _fstat64*/
129 #if !defined(__BORLANDC__)
130 #define getcwd _getcwd
133 /*#define lstat _stat64*/
134 /*#define stat _stat64*/
136 #if !defined(__BORLANDC__)
137 #define strdup _strdup
140 #define int64_t __int64
143 #if defined(HAVE__CrtSetReportMode)
147 mode_t umasked(mode_t expected_mode)
149 mode_t mode = umask(0);
151 return expected_mode & ~mode;
154 /* Path to working directory for current test */
155 const char *testworkdir;
157 /* Pathname of exe to be tested. */
158 const char *testprogfile;
159 /* Name of exe to use in printf-formatted command strings. */
160 /* On Windows, this includes leading/trailing quotes. */
161 const char *testprog;
164 #if defined(_WIN32) && !defined(__CYGWIN__)
165 static void *GetFunctionKernel32(const char *);
166 static int my_CreateSymbolicLinkA(const char *, const char *, int);
167 static int my_CreateHardLinkA(const char *, const char *);
168 static int my_GetFileInformationByName(const char *,
169 BY_HANDLE_FILE_INFORMATION *);
172 GetFunctionKernel32(const char *name)
174 static HINSTANCE lib;
178 lib = LoadLibrary("kernel32.dll");
181 fprintf(stderr, "Can't load kernel32.dll?!\n");
184 return (void *)GetProcAddress(lib, name);
188 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
190 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
194 f = GetFunctionKernel32("CreateSymbolicLinkA");
196 return f == NULL ? 0 : (*f)(linkname, target, flags);
200 my_CreateHardLinkA(const char *linkname, const char *target)
202 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
206 f = GetFunctionKernel32("CreateHardLinkA");
208 return f == NULL ? 0 : (*f)(linkname, target, NULL);
212 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
217 memset(bhfi, 0, sizeof(*bhfi));
218 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
219 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
220 if (h == INVALID_HANDLE_VALUE)
222 r = GetFileInformationByHandle(h, bhfi);
228 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
230 invalid_parameter_handler(const wchar_t * expression,
231 const wchar_t * function, const wchar_t * file,
232 unsigned int line, uintptr_t pReserved)
235 // Silence unused-parameter compiler warnings.
250 /* Enable core dump on failure. */
251 static int dump_on_failure = 0;
252 /* Default is to remove temp dirs and log data for successful tests. */
253 static int keep_temp_files = 0;
254 /* Default is to run the specified tests once and report errors. */
255 static int until_failure = 0;
256 /* Default is to just report pass/fail for each test. */
257 static int verbosity = 0;
258 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
259 #define VERBOSITY_PASSFAIL 0 /* Default */
260 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
261 #define VERBOSITY_FULL 2 /* -vv */
262 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
263 * mostly for debugging the test harness itself. */
264 /* Cumulative count of assertion failures. */
265 static int failures = 0;
266 /* Cumulative count of reported skips. */
267 static int skips = 0;
268 /* Cumulative count of assertions checked. */
269 static int assertions = 0;
271 /* Directory where uuencoded reference files can be found. */
272 static const char *refdir;
275 * Report log information selectively to console and/or disk log.
277 static int log_console = 0;
278 static FILE *logfile;
280 vlogprintf(const char *fmt, va_list ap)
287 vfprintf(stdout, fmt, ap);
290 vfprintf(logfile, fmt, lfap);
293 vfprintf(logfile, fmt, ap);
298 logprintf(const char *fmt, ...)
306 /* Set up a message to display only if next assertion fails. */
307 static char msgbuff[4096];
308 static const char *msg, *nextmsg;
310 failure(const char *fmt, ...)
317 vsprintf(msgbuff, fmt, ap);
324 * Copy arguments into file-local variables.
325 * This was added to permit vararg assert() functions without needing
326 * variadic wrapper macros. Turns out that the vararg capability is almost
327 * never used, so almost all of the vararg assertions can be simplified
328 * by removing the vararg capability and reworking the wrapper macro to
329 * pass __FILE__, __LINE__ directly into the function instead of using
330 * this hook. I suspect this machinery is used so rarely that we
331 * would be better off just removing it entirely. That would simplify
332 * the code here noticeably.
334 static const char *skipping_filename;
335 static int skipping_line;
336 void skipping_setup(const char *filename, int line)
338 skipping_filename = filename;
339 skipping_line = line;
342 /* Called at the beginning of each assert() function. */
344 assertion_count(const char *file, int line)
346 (void)file; /* UNUSED */
347 (void)line; /* UNUSED */
349 /* Proper handling of "failure()" message. */
352 /* Uncomment to print file:line after every assertion.
353 * Verbose, but occasionally useful in tracking down crashes. */
354 /* printf("Checked %s:%d\n", file, line); */
358 * For each test source file, we remember how many times each
359 * assertion was reported. Cleared before each new test,
360 * used by test_summarize().
365 } failed_lines[10000];
366 const char *failed_filename;
368 /* Count this failure, setup up log destination and handle initial report. */
370 failure_start(const char *filename, int line, const char *fmt, ...)
374 /* Record another failure for this line. */
376 failed_filename = filename;
377 failed_lines[line].count++;
379 /* Determine whether to log header to console. */
381 case VERBOSITY_LIGHT_REPORT:
382 log_console = (failed_lines[line].count < 2);
385 log_console = (verbosity >= VERBOSITY_FULL);
388 /* Log file:line header for this failure */
391 logprintf("%s(%d): ", filename, line);
393 logprintf("%s:%d: ", filename, line);
399 if (msg != NULL && msg[0] != '\0') {
400 logprintf(" Description: %s\n", msg);
404 /* Determine whether to log details to console. */
405 if (verbosity == VERBOSITY_LIGHT_REPORT)
409 /* Complete reporting of failed tests. */
411 * The 'extra' hook here is used by libarchive to include libarchive
412 * error messages with assertion failures. It could also be used
413 * to add strerror() output, for example. Just define the EXTRA_DUMP()
414 * macro appropriately.
417 failure_finish(void *extra)
419 (void)extra; /* UNUSED (maybe) */
422 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
423 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
427 if (dump_on_failure) {
429 " *** forcing core dump so failure can be debugged ***\n");
434 /* Inform user that we're skipping some checks. */
436 test_skipping(const char *fmt, ...)
442 vsprintf(buff, fmt, ap);
444 /* Use failure() message if set. */
447 /* failure_start() isn't quite right, but is awfully convenient. */
448 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
449 --failures; /* Undo failures++ in failure_start() */
450 /* Don't failure_finish() here. */
451 /* Mark as skip, so doesn't count as failed test. */
452 failed_lines[skipping_line].skip = 1;
462 /* Generic assert() just displays the failed condition. */
464 assertion_assert(const char *file, int line, int value,
465 const char *condition, void *extra)
467 assertion_count(file, line);
469 failure_start(file, line, "Assertion failed: %s", condition);
470 failure_finish(extra);
475 /* chdir() and report any errors */
477 assertion_chdir(const char *file, int line, const char *pathname)
479 assertion_count(file, line);
480 if (chdir(pathname) == 0)
482 failure_start(file, line, "chdir(\"%s\")", pathname);
483 failure_finish(NULL);
488 /* Verify two integers are equal. */
490 assertion_equal_int(const char *file, int line,
491 long long v1, const char *e1, long long v2, const char *e2, void *extra)
493 assertion_count(file, line);
496 failure_start(file, line, "%s != %s", e1, e2);
497 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
498 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
499 failure_finish(extra);
504 * Utility to convert a single UTF-8 sequence.
507 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
509 static const char utf8_count[256] = {
510 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
511 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
512 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
513 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
514 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
515 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
516 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
517 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
518 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
519 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
520 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
521 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
522 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
523 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
524 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
525 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
537 * Decode 1-4 bytes depending on the value of the first byte.
539 ch = (unsigned char)*s;
541 return (0); /* Standard: return 0 for end-of-string. */
542 cnt = utf8_count[ch];
544 /* Invalid sequence or there are not plenty bytes. */
548 /* Make a Unicode code point from a single UTF-8 sequence. */
550 case 1: /* 1 byte sequence. */
553 case 2: /* 2 bytes sequence. */
554 if ((s[1] & 0xc0) != 0x80) return (-1);
555 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
557 case 3: /* 3 bytes sequence. */
558 if ((s[1] & 0xc0) != 0x80) return (-1);
559 if ((s[2] & 0xc0) != 0x80) return (-1);
560 wc = ((ch & 0x0f) << 12)
561 | ((s[1] & 0x3f) << 6)
564 return (-1);/* Overlong sequence. */
566 case 4: /* 4 bytes sequence. */
569 if ((s[1] & 0xc0) != 0x80) return (-1);
570 if ((s[2] & 0xc0) != 0x80) return (-1);
571 if ((s[3] & 0xc0) != 0x80) return (-1);
572 wc = ((ch & 0x07) << 18)
573 | ((s[1] & 0x3f) << 12)
574 | ((s[2] & 0x3f) << 6)
577 return (-1);/* Overlong sequence. */
583 /* The code point larger than 0x10FFFF is not legal
587 /* Correctly gets a Unicode, returns used bytes. */
592 static void strdump(const char *e, const char *p, int ewidth, int utf8)
596 logprintf(" %*s = ", ewidth, e);
603 unsigned int c = 0xff & *p++;
605 case '\a': logprintf("\\a"); break;
606 case '\b': logprintf("\\b"); break;
607 case '\n': logprintf("\\n"); break;
608 case '\r': logprintf("\\r"); break;
610 if (c >= 32 && c < 127)
613 logprintf("\\x%02X", c);
617 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
620 * If the current string is UTF-8, dump its code points.
631 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
634 logprintf("%04X", uc);
640 logprintf(" (count %d", cnt);
642 logprintf(",unknown %d bytes", len);
650 /* Verify two strings are equal, dump them if not. */
652 assertion_equal_string(const char *file, int line,
653 const char *v1, const char *e1,
654 const char *v2, const char *e2,
655 void *extra, int utf8)
659 assertion_count(file, line);
660 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
662 failure_start(file, line, "%s != %s", e1, e2);
663 l1 = (int)strlen(e1);
664 l2 = (int)strlen(e2);
667 strdump(e1, v1, l1, utf8);
668 strdump(e2, v2, l1, utf8);
669 failure_finish(extra);
674 wcsdump(const char *e, const wchar_t *w)
676 logprintf(" %s = ", e);
682 while (*w != L'\0') {
683 unsigned int c = *w++;
684 if (c >= 32 && c < 127)
687 logprintf("\\x%02X", c);
688 else if (c < 0x10000)
689 logprintf("\\u%04X", c);
691 logprintf("\\U%08X", c);
698 wcscmp(const wchar_t *s1, const wchar_t *s2)
701 while (*s1 == *s2++) {
712 /* Verify that two wide strings are equal, dump them if not. */
714 assertion_equal_wstring(const char *file, int line,
715 const wchar_t *v1, const char *e1,
716 const wchar_t *v2, const char *e2,
719 assertion_count(file, line);
722 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
724 failure_start(file, line, "%s != %s", e1, e2);
727 failure_finish(extra);
732 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
733 * any bytes in p that differ from ref will be highlighted with '_'
734 * before and after the hex value.
737 hexdump(const char *p, const char *ref, size_t l, size_t offset)
743 logprintf("(null)\n");
746 for(i=0; i < l; i+=16) {
747 logprintf("%04x", (unsigned)(i + offset));
749 for (j = 0; j < 16 && i + j < l; j++) {
750 if (ref != NULL && p[i + j] != ref[i + j])
752 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
753 if (ref != NULL && p[i + j] == ref[i + j])
756 for (; j < 16; j++) {
757 logprintf("%c ", sep);
760 logprintf("%c", sep);
761 for (j=0; j < 16 && i + j < l; j++) {
763 if (c >= ' ' && c <= 126)
772 /* Verify that two blocks of memory are the same, display the first
773 * block of differences if they're not. */
775 assertion_equal_mem(const char *file, int line,
776 const void *_v1, const char *e1,
777 const void *_v2, const char *e2,
778 size_t l, const char *ld, void *extra)
780 const char *v1 = (const char *)_v1;
781 const char *v2 = (const char *)_v2;
784 assertion_count(file, line);
785 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
787 if (v1 == NULL || v2 == NULL)
790 failure_start(file, line, "%s != %s", e1, e2);
791 logprintf(" size %s = %d\n", ld, (int)l);
792 /* Dump 48 bytes (3 lines) so that the first difference is
793 * in the second line. */
795 while (l > 64 && memcmp(v1, v2, 32) == 0) {
796 /* Two lines agree, so step forward one line. */
802 logprintf(" Dump of %s\n", e1);
803 hexdump(v1, v2, l < 128 ? l : 128, offset);
804 logprintf(" Dump of %s\n", e2);
805 hexdump(v2, v1, l < 128 ? l : 128, offset);
807 failure_finish(extra);
811 /* Verify that a block of memory is filled with the specified byte. */
813 assertion_memory_filled_with(const char *file, int line,
814 const void *_v1, const char *vd,
815 size_t l, const char *ld,
816 char b, const char *bd, void *extra)
818 const char *v1 = (const char *)_v1;
821 (void)ld; /* UNUSED */
823 assertion_count(file, line);
825 for (i = 0; i < l; ++i) {
833 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
834 logprintf(" Only %d bytes were correct\n", (int)c);
835 failure_finish(extra);
839 /* Verify that the named file exists and is empty. */
841 assertion_empty_file(const char *filename, int line, const char *f1)
848 assertion_count(filename, line);
850 if (stat(f1, &st) != 0) {
851 failure_start(filename, line, "Stat failed: %s", f1);
852 failure_finish(NULL);
858 failure_start(filename, line, "File should be empty: %s", f1);
859 logprintf(" File size: %d\n", (int)st.st_size);
860 logprintf(" Contents:\n");
863 logprintf(" Unable to open %s\n", f1);
865 s = ((off_t)sizeof(buff) < st.st_size) ?
866 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
867 s = fread(buff, 1, s, f);
868 hexdump(buff, NULL, s, 0);
871 failure_finish(NULL);
875 /* Verify that the named file exists and is not empty. */
877 assertion_non_empty_file(const char *filename, int line, const char *f1)
881 assertion_count(filename, line);
883 if (stat(f1, &st) != 0) {
884 failure_start(filename, line, "Stat failed: %s", f1);
885 failure_finish(NULL);
888 if (st.st_size == 0) {
889 failure_start(filename, line, "File empty: %s", f1);
890 failure_finish(NULL);
896 /* Verify that two files have the same contents. */
897 /* TODO: hexdump the first bytes that actually differ. */
899 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
906 assertion_count(filename, line);
908 f1 = fopen(fn1, "rb");
909 f2 = fopen(fn2, "rb");
910 if (f1 == NULL || f2 == NULL) {
916 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
917 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
920 if (n1 == 0 && n2 == 0) {
925 if (memcmp(buff1, buff2, n1) != 0)
930 failure_start(filename, line, "Files not identical");
931 logprintf(" file1=\"%s\"\n", fn1);
932 logprintf(" file2=\"%s\"\n", fn2);
933 failure_finish(NULL);
937 /* Verify that the named file does exist. */
939 assertion_file_exists(const char *filename, int line, const char *f)
941 assertion_count(filename, line);
943 #if defined(_WIN32) && !defined(__CYGWIN__)
947 if (!access(f, F_OK))
950 failure_start(filename, line, "File should exist: %s", f);
951 failure_finish(NULL);
955 /* Verify that the named file doesn't exist. */
957 assertion_file_not_exists(const char *filename, int line, const char *f)
959 assertion_count(filename, line);
961 #if defined(_WIN32) && !defined(__CYGWIN__)
968 failure_start(filename, line, "File should not exist: %s", f);
969 failure_finish(NULL);
973 /* Compare the contents of a file to a block of memory. */
975 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
981 assertion_count(filename, line);
985 failure_start(filename, line,
986 "File should exist: %s", fn);
987 failure_finish(NULL);
990 contents = malloc(s * 2);
991 n = (int)fread(contents, 1, s * 2, f);
993 if (n == s && memcmp(buff, contents, s) == 0) {
997 failure_start(filename, line, "File contents don't match");
998 logprintf(" file=\"%s\"\n", fn);
1000 hexdump(contents, buff, n > 512 ? 512 : n, 0);
1002 logprintf(" File empty, contents should be:\n");
1003 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
1005 failure_finish(NULL);
1010 /* Check the contents of a text file, being tolerant of line endings. */
1012 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
1015 const char *btxt, *ftxt;
1019 assertion_count(filename, line);
1022 failure_start(filename, line,
1023 "File doesn't exist: %s", fn);
1024 failure_finish(NULL);
1027 s = (int)strlen(buff);
1028 contents = malloc(s * 2 + 128);
1029 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1033 /* Compare texts. */
1035 ftxt = (const char *)contents;
1036 while (*btxt != '\0' && *ftxt != '\0') {
1037 if (*btxt == *ftxt) {
1042 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1043 /* Pass over different new line characters. */
1050 if (*btxt == '\0' && *ftxt == '\0') {
1054 failure_start(filename, line, "Contents don't match");
1055 logprintf(" file=\"%s\"\n", fn);
1057 hexdump(contents, buff, n, 0);
1058 logprintf(" expected\n", fn);
1059 hexdump(buff, contents, s, 0);
1061 logprintf(" File empty, contents should be:\n");
1062 hexdump(buff, NULL, s, 0);
1064 failure_finish(NULL);
1069 /* Verify that a text file contains the specified lines, regardless of order */
1070 /* This could be more efficient if we sorted both sets of lines, etc, but
1071 * since this is used only for testing and only ever deals with a dozen or so
1072 * lines at a time, this relatively crude approach is just fine. */
1074 assertion_file_contains_lines_any_order(const char *file, int line,
1075 const char *pathname, const char *lines[])
1079 size_t expected_count, actual_count, i, j;
1080 char **expected = NULL;
1081 char *p, **actual = NULL;
1083 int expected_failure = 0, actual_failure = 0;
1085 assertion_count(file, line);
1087 buff = slurpfile(&buff_size, "%s", pathname);
1089 failure_start(pathname, line, "Can't read file: %s", pathname);
1090 failure_finish(NULL);
1094 /* Make a copy of the provided lines and count up the expected
1096 for (i = 0; lines[i] != NULL; ++i) {
1099 if (expected_count) {
1100 expected = malloc(sizeof(char *) * expected_count);
1101 if (expected == NULL) {
1102 failure_start(pathname, line, "Can't allocate memory");
1103 failure_finish(NULL);
1108 for (i = 0; lines[i] != NULL; ++i) {
1109 expected[i] = strdup(lines[i]);
1113 /* Break the file into lines */
1115 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1116 if (*p == '\x0d' || *p == '\x0a')
1118 if (c == '\0' && *p != '\0')
1123 actual = calloc(sizeof(char *), actual_count);
1124 if (actual == NULL) {
1125 failure_start(pathname, line, "Can't allocate memory");
1126 failure_finish(NULL);
1131 for (j = 0, p = buff; p < buff + buff_size;
1132 p += 1 + strlen(p)) {
1140 /* Erase matching lines from both lists */
1141 for (i = 0; i < expected_count; ++i) {
1142 if (expected[i] == NULL)
1144 for (j = 0; j < actual_count; ++j) {
1145 if (actual[j] == NULL)
1147 if (strcmp(expected[i], actual[j]) == 0) {
1156 /* If there's anything left, it's a failure */
1157 for (i = 0; i < expected_count; ++i) {
1158 if (expected[i] != NULL)
1161 for (j = 0; j < actual_count; ++j) {
1162 if (actual[j] != NULL)
1165 if (expected_failure == 0 && actual_failure == 0) {
1171 failure_start(file, line, "File doesn't match: %s", pathname);
1172 for (i = 0; i < expected_count; ++i) {
1173 if (expected[i] != NULL) {
1174 logprintf(" Expected but not present: %s\n", expected[i]);
1178 for (j = 0; j < actual_count; ++j) {
1179 if (actual[j] != NULL)
1180 logprintf(" Present but not expected: %s\n", actual[j]);
1182 failure_finish(NULL);
1189 /* Verify that a text file does not contains the specified strings */
1191 assertion_file_contains_no_invalid_strings(const char *file, int line,
1192 const char *pathname, const char *strings[])
1197 buff = slurpfile(NULL, "%s", pathname);
1199 failure_start(file, line, "Can't read file: %s", pathname);
1200 failure_finish(NULL);
1204 for (i = 0; strings[i] != NULL; ++i) {
1205 if (strstr(buff, strings[i]) != NULL) {
1206 failure_start(file, line, "Invalid string in %s: %s", pathname,
1208 failure_finish(NULL);
1218 /* Test that two paths point to the same file. */
1219 /* As a side-effect, asserts that both files exist. */
1221 is_hardlink(const char *file, int line,
1222 const char *path1, const char *path2)
1224 #if defined(_WIN32) && !defined(__CYGWIN__)
1225 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1228 assertion_count(file, line);
1229 r = my_GetFileInformationByName(path1, &bhfi1);
1231 failure_start(file, line, "File %s can't be inspected?", path1);
1232 failure_finish(NULL);
1235 r = my_GetFileInformationByName(path2, &bhfi2);
1237 failure_start(file, line, "File %s can't be inspected?", path2);
1238 failure_finish(NULL);
1241 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1242 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1243 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1245 struct stat st1, st2;
1248 assertion_count(file, line);
1249 r = lstat(path1, &st1);
1251 failure_start(file, line, "File should exist: %s", path1);
1252 failure_finish(NULL);
1255 r = lstat(path2, &st2);
1257 failure_start(file, line, "File should exist: %s", path2);
1258 failure_finish(NULL);
1261 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1266 assertion_is_hardlink(const char *file, int line,
1267 const char *path1, const char *path2)
1269 if (is_hardlink(file, line, path1, path2))
1271 failure_start(file, line,
1272 "Files %s and %s are not hardlinked", path1, path2);
1273 failure_finish(NULL);
1278 assertion_is_not_hardlink(const char *file, int line,
1279 const char *path1, const char *path2)
1281 if (!is_hardlink(file, line, path1, path2))
1283 failure_start(file, line,
1284 "Files %s and %s should not be hardlinked", path1, path2);
1285 failure_finish(NULL);
1289 /* Verify a/b/mtime of 'pathname'. */
1290 /* If 'recent', verify that it's within last 10 seconds. */
1292 assertion_file_time(const char *file, int line,
1293 const char *pathname, long t, long nsec, char type, int recent)
1295 long long filet, filet_nsec;
1298 #if defined(_WIN32) && !defined(__CYGWIN__)
1299 #define EPOC_TIME (116444736000000000ULL)
1300 FILETIME fxtime, fbirthtime, fatime, fmtime;
1301 ULARGE_INTEGER wintm;
1303 fxtime.dwLowDateTime = 0;
1304 fxtime.dwHighDateTime = 0;
1306 assertion_count(file, line);
1307 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1308 * a directory file. If not, CreateFile() will fail when
1309 * the pathname is a directory. */
1310 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1311 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1312 if (h == INVALID_HANDLE_VALUE) {
1313 failure_start(file, line, "Can't access %s\n", pathname);
1314 failure_finish(NULL);
1317 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1319 case 'a': fxtime = fatime; break;
1320 case 'b': fxtime = fbirthtime; break;
1321 case 'm': fxtime = fmtime; break;
1325 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1326 failure_finish(NULL);
1329 wintm.LowPart = fxtime.dwLowDateTime;
1330 wintm.HighPart = fxtime.dwHighDateTime;
1331 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1332 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1333 nsec = (nsec / 100) * 100; /* Round the request */
1337 assertion_count(file, line);
1338 r = lstat(pathname, &st);
1340 failure_start(file, line, "Can't stat %s\n", pathname);
1341 failure_finish(NULL);
1345 case 'a': filet = st.st_atime; break;
1346 case 'm': filet = st.st_mtime; break;
1347 case 'b': filet = 0; break;
1348 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1351 #if defined(__FreeBSD__)
1353 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1354 case 'b': filet = st.st_birthtime;
1355 /* FreeBSD filesystems that don't support birthtime
1356 * (e.g., UFS1) always return -1 here. */
1360 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1361 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1362 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1365 /* FreeBSD generally only stores to microsecond res, so round. */
1366 filet_nsec = (filet_nsec / 1000) * 1000;
1367 nsec = (nsec / 1000) * 1000;
1369 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1370 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1371 #if defined(__HAIKU__)
1372 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1377 /* Check that requested time is up-to-date. */
1378 time_t now = time(NULL);
1379 if (filet < now - 10 || filet > now + 1) {
1380 failure_start(file, line,
1381 "File %s has %ctime %lld, %lld seconds ago\n",
1382 pathname, type, filet, now - filet);
1383 failure_finish(NULL);
1386 } else if (filet != t || filet_nsec != nsec) {
1387 failure_start(file, line,
1388 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1389 pathname, type, filet, filet_nsec, t, nsec);
1390 failure_finish(NULL);
1396 /* Verify atime of 'pathname'. */
1398 assertion_file_atime(const char *file, int line,
1399 const char *pathname, long t, long nsec)
1401 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1404 /* Verify atime of 'pathname' is up-to-date. */
1406 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1408 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1411 /* Verify birthtime of 'pathname'. */
1413 assertion_file_birthtime(const char *file, int line,
1414 const char *pathname, long t, long nsec)
1416 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1419 /* Verify birthtime of 'pathname' is up-to-date. */
1421 assertion_file_birthtime_recent(const char *file, int line,
1422 const char *pathname)
1424 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1427 /* Verify mode of 'pathname'. */
1429 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1434 assertion_count(file, line);
1435 #if defined(_WIN32) && !defined(__CYGWIN__)
1436 failure_start(file, line, "assertFileMode not yet implemented for Windows");
1437 (void)mode; /* UNUSED */
1438 (void)r; /* UNUSED */
1439 (void)pathname; /* UNUSED */
1440 (void)expected_mode; /* UNUSED */
1444 r = lstat(pathname, &st);
1445 mode = (int)(st.st_mode & 0777);
1447 if (r == 0 && mode == expected_mode)
1449 failure_start(file, line, "File %s has mode %o, expected %o",
1450 pathname, mode, expected_mode);
1452 failure_finish(NULL);
1456 /* Verify mtime of 'pathname'. */
1458 assertion_file_mtime(const char *file, int line,
1459 const char *pathname, long t, long nsec)
1461 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1464 /* Verify mtime of 'pathname' is up-to-date. */
1466 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1468 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1471 /* Verify number of links to 'pathname'. */
1473 assertion_file_nlinks(const char *file, int line,
1474 const char *pathname, int nlinks)
1476 #if defined(_WIN32) && !defined(__CYGWIN__)
1477 BY_HANDLE_FILE_INFORMATION bhfi;
1480 assertion_count(file, line);
1481 r = my_GetFileInformationByName(pathname, &bhfi);
1482 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1484 failure_start(file, line, "File %s has %d links, expected %d",
1485 pathname, bhfi.nNumberOfLinks, nlinks);
1486 failure_finish(NULL);
1492 assertion_count(file, line);
1493 r = lstat(pathname, &st);
1494 if (r == 0 && (int)st.st_nlink == nlinks)
1496 failure_start(file, line, "File %s has %d links, expected %d",
1497 pathname, st.st_nlink, nlinks);
1498 failure_finish(NULL);
1503 /* Verify size of 'pathname'. */
1505 assertion_file_size(const char *file, int line, const char *pathname, long size)
1510 assertion_count(file, line);
1511 #if defined(_WIN32) && !defined(__CYGWIN__)
1513 BY_HANDLE_FILE_INFORMATION bhfi;
1514 r = !my_GetFileInformationByName(pathname, &bhfi);
1515 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1520 r = lstat(pathname, &st);
1521 filesize = st.st_size;
1524 if (r == 0 && filesize == size)
1526 failure_start(file, line, "File %s has size %ld, expected %ld",
1527 pathname, (long)filesize, (long)size);
1528 failure_finish(NULL);
1532 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1534 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1539 #if defined(_WIN32) && !defined(__CYGWIN__)
1540 (void)mode; /* UNUSED */
1542 assertion_count(file, line);
1543 r = lstat(pathname, &st);
1545 failure_start(file, line, "Dir should exist: %s", pathname);
1546 failure_finish(NULL);
1549 if (!S_ISDIR(st.st_mode)) {
1550 failure_start(file, line, "%s is not a dir", pathname);
1551 failure_finish(NULL);
1554 #if !defined(_WIN32) || defined(__CYGWIN__)
1555 /* Windows doesn't handle permissions the same way as POSIX,
1556 * so just ignore the mode tests. */
1557 /* TODO: Can we do better here? */
1558 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1559 failure_start(file, line, "Dir %s has wrong mode", pathname);
1560 logprintf(" Expected: 0%3o\n", mode);
1561 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1562 failure_finish(NULL);
1569 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1570 * verify that too. */
1572 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1577 #if defined(_WIN32) && !defined(__CYGWIN__)
1578 (void)mode; /* UNUSED */
1580 assertion_count(file, line);
1581 r = lstat(pathname, &st);
1582 if (r != 0 || !S_ISREG(st.st_mode)) {
1583 failure_start(file, line, "File should exist: %s", pathname);
1584 failure_finish(NULL);
1587 #if !defined(_WIN32) || defined(__CYGWIN__)
1588 /* Windows doesn't handle permissions the same way as POSIX,
1589 * so just ignore the mode tests. */
1590 /* TODO: Can we do better here? */
1591 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1592 failure_start(file, line, "File %s has wrong mode", pathname);
1593 logprintf(" Expected: 0%3o\n", mode);
1594 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1595 failure_finish(NULL);
1602 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1603 * non-NULL, verify that the symlink has those contents. */
1605 is_symlink(const char *file, int line,
1606 const char *pathname, const char *contents)
1608 #if defined(_WIN32) && !defined(__CYGWIN__)
1609 (void)pathname; /* UNUSED */
1610 (void)contents; /* UNUSED */
1611 assertion_count(file, line);
1612 /* Windows sort-of has real symlinks, but they're only usable
1613 * by privileged users and are crippled even then, so there's
1614 * really not much point in bothering with this. */
1622 assertion_count(file, line);
1623 r = lstat(pathname, &st);
1625 failure_start(file, line,
1626 "Symlink should exist: %s", pathname);
1627 failure_finish(NULL);
1630 if (!S_ISLNK(st.st_mode))
1632 if (contents == NULL)
1634 linklen = readlink(pathname, buff, sizeof(buff));
1636 failure_start(file, line, "Can't read symlink %s", pathname);
1637 failure_finish(NULL);
1640 buff[linklen] = '\0';
1641 if (strcmp(buff, contents) != 0)
1647 /* Assert that path is a symlink that (optionally) contains contents. */
1649 assertion_is_symlink(const char *file, int line,
1650 const char *path, const char *contents)
1652 if (is_symlink(file, line, path, contents))
1655 failure_start(file, line, "File %s is not a symlink to %s",
1658 failure_start(file, line, "File %s is not a symlink", path);
1659 failure_finish(NULL);
1664 /* Create a directory and report any errors. */
1666 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1668 assertion_count(file, line);
1669 #if defined(_WIN32) && !defined(__CYGWIN__)
1670 (void)mode; /* UNUSED */
1671 if (0 == _mkdir(dirname))
1674 if (0 == mkdir(dirname, mode)) {
1675 if (0 == chmod(dirname, mode)) {
1676 assertion_file_mode(file, line, dirname, mode);
1681 failure_start(file, line, "Could not create directory %s", dirname);
1682 failure_finish(NULL);
1686 /* Create a file with the specified contents and report any failures. */
1688 assertion_make_file(const char *file, int line,
1689 const char *path, int mode, int csize, const void *contents)
1691 #if defined(_WIN32) && !defined(__CYGWIN__)
1692 /* TODO: Rework this to set file mode as well. */
1694 (void)mode; /* UNUSED */
1695 assertion_count(file, line);
1696 f = fopen(path, "wb");
1698 failure_start(file, line, "Could not create file %s", path);
1699 failure_finish(NULL);
1702 if (contents != NULL) {
1706 wsize = strlen(contents);
1708 wsize = (size_t)csize;
1709 if (wsize != fwrite(contents, 1, wsize, f)) {
1711 failure_start(file, line,
1712 "Could not write file %s", path);
1713 failure_finish(NULL);
1721 assertion_count(file, line);
1722 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1724 failure_start(file, line, "Could not create %s", path);
1725 failure_finish(NULL);
1728 if (0 != chmod(path, mode)) {
1729 failure_start(file, line, "Could not chmod %s", path);
1730 failure_finish(NULL);
1734 if (contents != NULL) {
1738 wsize = (ssize_t)strlen(contents);
1740 wsize = (ssize_t)csize;
1741 if (wsize != write(fd, contents, wsize)) {
1743 failure_start(file, line,
1744 "Could not write to %s", path);
1745 failure_finish(NULL);
1751 assertion_file_mode(file, line, path, mode);
1756 /* Create a hardlink and report any failures. */
1758 assertion_make_hardlink(const char *file, int line,
1759 const char *newpath, const char *linkto)
1763 assertion_count(file, line);
1764 #if defined(_WIN32) && !defined(__CYGWIN__)
1765 succeeded = my_CreateHardLinkA(newpath, linkto);
1767 succeeded = !link(linkto, newpath);
1773 failure_start(file, line, "Could not create hardlink");
1774 logprintf(" New link: %s\n", newpath);
1775 logprintf(" Old name: %s\n", linkto);
1776 failure_finish(NULL);
1780 /* Create a symlink and report any failures. */
1782 assertion_make_symlink(const char *file, int line,
1783 const char *newpath, const char *linkto)
1785 #if defined(_WIN32) && !defined(__CYGWIN__)
1786 int targetIsDir = 0; /* TODO: Fix this */
1787 assertion_count(file, line);
1788 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1791 assertion_count(file, line);
1792 if (0 == symlink(linkto, newpath))
1795 failure_start(file, line, "Could not create symlink");
1796 logprintf(" New link: %s\n", newpath);
1797 logprintf(" Old name: %s\n", linkto);
1798 failure_finish(NULL);
1802 /* Set umask, report failures. */
1804 assertion_umask(const char *file, int line, int mask)
1806 assertion_count(file, line);
1807 (void)file; /* UNUSED */
1808 (void)line; /* UNUSED */
1813 /* Set times, report failures. */
1815 assertion_utimes(const char *file, int line,
1816 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1820 #if defined(_WIN32) && !defined(__CYGWIN__)
1821 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1822 + (((nsec)/1000)*10))
1824 ULARGE_INTEGER wintm;
1825 FILETIME fatime, fmtime;
1826 FILETIME *pat, *pmt;
1828 assertion_count(file, line);
1829 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1830 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1831 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1832 if (h == INVALID_HANDLE_VALUE) {
1833 failure_start(file, line, "Can't access %s\n", pathname);
1834 failure_finish(NULL);
1838 if (at > 0 || at_nsec > 0) {
1839 wintm.QuadPart = WINTIME(at, at_nsec);
1840 fatime.dwLowDateTime = wintm.LowPart;
1841 fatime.dwHighDateTime = wintm.HighPart;
1845 if (mt > 0 || mt_nsec > 0) {
1846 wintm.QuadPart = WINTIME(mt, mt_nsec);
1847 fmtime.dwLowDateTime = wintm.LowPart;
1848 fmtime.dwHighDateTime = wintm.HighPart;
1852 if (pat != NULL || pmt != NULL)
1853 r = SetFileTime(h, NULL, pat, pmt);
1858 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1859 failure_finish(NULL);
1863 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1865 struct timeval times[2];
1867 #if !defined(__FreeBSD__)
1868 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1870 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1873 r = lstat(pathname, &st);
1875 failure_start(file, line, "Can't stat %s\n", pathname);
1876 failure_finish(NULL);
1880 if (mt == 0 && mt_nsec == 0) {
1882 #if defined(__FreeBSD__)
1883 mt_nsec = st.st_mtimespec.tv_nsec;
1884 /* FreeBSD generally only stores to microsecond res, so round. */
1885 mt_nsec = (mt_nsec / 1000) * 1000;
1888 if (at == 0 && at_nsec == 0) {
1890 #if defined(__FreeBSD__)
1891 at_nsec = st.st_atimespec.tv_nsec;
1892 /* FreeBSD generally only stores to microsecond res, so round. */
1893 at_nsec = (at_nsec / 1000) * 1000;
1897 times[1].tv_sec = mt;
1898 times[1].tv_usec = mt_nsec / 1000;
1900 times[0].tv_sec = at;
1901 times[0].tv_usec = at_nsec / 1000;
1904 r = lutimes(pathname, times);
1906 r = utimes(pathname, times);
1909 failure_start(file, line, "Can't utimes %s\n", pathname);
1910 failure_finish(NULL);
1914 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1917 /* Compare file flags */
1919 assertion_compare_fflags(const char *file, int line, const char *patha,
1920 const char *pathb, int nomatch)
1922 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1925 assertion_count(file, line);
1927 if (stat(patha, &sa) < 0)
1929 if (stat(pathb, &sb) < 0)
1931 if (!nomatch && sa.st_flags != sb.st_flags) {
1932 failure_start(file, line, "File flags should be identical: "
1933 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
1935 failure_finish(NULL);
1938 if (nomatch && sa.st_flags == sb.st_flags) {
1939 failure_start(file, line, "File flags should be different: "
1940 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
1942 failure_finish(NULL);
1945 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
1946 defined(FS_NODUMP_FL)) || \
1947 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
1948 && defined(EXT2_NODUMP_FL))
1949 int fd, r, flagsa, flagsb;
1951 assertion_count(file, line);
1952 fd = open(patha, O_RDONLY | O_NONBLOCK);
1954 failure_start(file, line, "Can't open %s\n", patha);
1955 failure_finish(NULL);
1959 #ifdef FS_IOC_GETFLAGS
1967 failure_start(file, line, "Can't get flags %s\n", patha);
1968 failure_finish(NULL);
1971 fd = open(pathb, O_RDONLY | O_NONBLOCK);
1973 failure_start(file, line, "Can't open %s\n", pathb);
1974 failure_finish(NULL);
1978 #ifdef FS_IOC_GETFLAGS
1986 failure_start(file, line, "Can't get flags %s\n", pathb);
1987 failure_finish(NULL);
1990 if (!nomatch && flagsa != flagsb) {
1991 failure_start(file, line, "File flags should be identical: "
1992 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
1993 failure_finish(NULL);
1996 if (nomatch && flagsa == flagsb) {
1997 failure_start(file, line, "File flags should be different: "
1998 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
1999 failure_finish(NULL);
2003 (void)patha; /* UNUSED */
2004 (void)pathb; /* UNUSED */
2005 (void)nomatch; /* UNUSED */
2006 assertion_count(file, line);
2011 /* Set nodump, report failures. */
2013 assertion_set_nodump(const char *file, int line, const char *pathname)
2015 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2018 assertion_count(file, line);
2019 r = chflags(pathname, UF_NODUMP);
2021 failure_start(file, line, "Can't set nodump %s\n", pathname);
2022 failure_finish(NULL);
2025 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
2026 defined(FS_NODUMP_FL)) || \
2027 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2028 && defined(EXT2_NODUMP_FL))
2031 assertion_count(file, line);
2032 fd = open(pathname, O_RDONLY | O_NONBLOCK);
2034 failure_start(file, line, "Can't open %s\n", pathname);
2035 failure_finish(NULL);
2039 #ifdef FS_IOC_GETFLAGS
2046 failure_start(file, line, "Can't get flags %s\n", pathname);
2047 failure_finish(NULL);
2051 flags |= FS_NODUMP_FL;
2053 flags |= EXT2_NODUMP_FL;
2057 #ifdef FS_IOC_SETFLAGS
2064 failure_start(file, line, "Can't set nodump %s\n", pathname);
2065 failure_finish(NULL);
2070 (void)pathname; /* UNUSED */
2071 assertion_count(file, line);
2077 static void assert_version_id(char **qq, size_t *ss)
2082 /* Version number is a series of digits and periods. */
2083 while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
2088 if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') {
2093 /* Skip a single trailing a,b,c, or d. */
2094 if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
2097 /* Version number terminated by space. */
2098 failure("No space after version: ``%s''", q);
2100 failure("No space after version: ``%s''", q);
2111 * Check program version
2113 void assertVersion(const char *prog, const char *base)
2118 size_t prog_len = strlen(base);
2120 r = systemf("%s --version >version.stdout 2>version.stderr", prog);
2122 r = systemf("%s -W version >version.stdout 2>version.stderr",
2125 failure("Unable to run either %s --version or %s -W version",
2127 if (!assert(r == 0))
2130 /* --version should generate nothing to stdout. */
2131 assertEmptyFile("version.stderr");
2133 /* Verify format of version message. */
2134 q = p = slurpfile(&s, "version.stdout");
2136 /* Version message should start with name of program, then space. */
2137 assert(s > prog_len + 1);
2139 failure("Version must start with '%s': ``%s''", base, p);
2140 if (!assertEqualMem(q, base, prog_len)) {
2145 q += prog_len; s -= prog_len;
2150 assert_version_id(&q, &s);
2153 failure("No `-' between program name and versions: ``%s''", p);
2154 assertEqualMem(q, "- ", 2);
2157 failure("Not long enough for libarchive version: ``%s''", p);
2160 failure("Libarchive version must start with `libarchive': ``%s''", p);
2161 assertEqualMem(q, "libarchive ", 11);
2165 assert_version_id(&q, &s);
2167 /* Skip arbitrary third-party version numbers. */
2168 while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' ||
2169 isalnum((unsigned char)*q))) {
2174 /* All terminated by end-of-line. */
2177 /* Skip an optional CR character (e.g., Windows) */
2178 failure("Version output must end with \\n or \\r\\n");
2180 if (*q == '\r') { ++q; --s; }
2181 assertEqualMem(q, "\n", 1);
2185 #endif /* PROGRAM */
2189 * UTILITIES for use by tests.
2194 * Check whether platform supports symlinks. This is intended
2195 * for tests to use in deciding whether to bother testing symlink
2196 * support; if the platform doesn't support symlinks, there's no point
2197 * in checking whether the program being tested can create them.
2199 * Note that the first time this test is called, we actually go out to
2200 * disk to create and verify a symlink. This is necessary because
2201 * symlink support is actually a property of a particular filesystem
2202 * and can thus vary between directories on a single system. After
2203 * the first call, this returns the cached result from memory, so it's
2204 * safe to call it as often as you wish.
2209 /* Remember the test result */
2210 static int value = 0, tested = 0;
2215 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
2216 /* Note: Cygwin has its own symlink() emulation that does not
2217 * use the Win32 CreateSymbolicLink() function. */
2218 #if defined(_WIN32) && !defined(__CYGWIN__)
2219 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
2220 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
2222 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
2223 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
2228 /* Platform-dependent options for hiding the output of a subcommand. */
2229 #if defined(_WIN32) && !defined(__CYGWIN__)
2230 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
2232 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
2235 * Can this platform run the bzip2 program?
2240 static int tested = 0, value = 0;
2243 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
2250 * Can this platform run the grzip program?
2255 static int tested = 0, value = 0;
2258 if (systemf("grzip -V %s", redirectArgs) == 0)
2265 * Can this platform run the gzip program?
2270 static int tested = 0, value = 0;
2273 if (systemf("gzip -V %s", redirectArgs) == 0)
2280 * Can this platform run the lrzip program?
2283 canRunCommand(const char *cmd)
2285 static int tested = 0, value = 0;
2288 if (systemf("%s %s", cmd, redirectArgs) == 0)
2297 static int tested = 0, value = 0;
2300 if (systemf("lrzip -V %s", redirectArgs) == 0)
2307 * Can this platform run the lz4 program?
2312 static int tested = 0, value = 0;
2315 if (systemf("lz4 -V %s", redirectArgs) == 0)
2322 * Can this platform run the zstd program?
2327 static int tested = 0, value = 0;
2330 if (systemf("zstd -V %s", redirectArgs) == 0)
2337 * Can this platform run the lzip program?
2342 static int tested = 0, value = 0;
2345 if (systemf("lzip -V %s", redirectArgs) == 0)
2352 * Can this platform run the lzma program?
2357 static int tested = 0, value = 0;
2360 if (systemf("lzma -V %s", redirectArgs) == 0)
2367 * Can this platform run the lzop program?
2372 static int tested = 0, value = 0;
2375 if (systemf("lzop -V %s", redirectArgs) == 0)
2382 * Can this platform run the xz program?
2387 static int tested = 0, value = 0;
2390 if (systemf("xz -V %s", redirectArgs) == 0)
2397 * Can this filesystem handle nodump flags.
2402 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2403 const char *path = "cannodumptest";
2406 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2407 if (chflags(path, UF_NODUMP) < 0)
2409 if (stat(path, &sb) < 0)
2411 if (sb.st_flags & UF_NODUMP)
2413 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \
2414 && defined(FS_NODUMP_FL)) || \
2415 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2416 && defined(EXT2_NODUMP_FL))
2417 const char *path = "cannodumptest";
2420 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2421 fd = open(path, O_RDONLY | O_NONBLOCK);
2425 #ifdef FS_IOC_GETFLAGS
2434 flags |= FS_NODUMP_FL;
2436 flags |= EXT2_NODUMP_FL;
2439 #ifdef FS_IOC_SETFLAGS
2448 fd = open(path, O_RDONLY | O_NONBLOCK);
2452 #ifdef FS_IOC_GETFLAGS
2462 if (flags & FS_NODUMP_FL)
2464 if (flags & EXT2_NODUMP_FL)
2471 /* Get extended attribute from a path */
2473 getXattr(const char *path, const char *name, size_t *sizep)
2476 #if ARCHIVE_XATTR_SUPPORT
2478 #if ARCHIVE_XATTR_LINUX
2479 size = lgetxattr(path, name, NULL, 0);
2480 #elif ARCHIVE_XATTR_DARWIN
2481 size = getxattr(path, name, NULL, 0, 0, XATTR_NOFOLLOW);
2482 #elif ARCHIVE_XATTR_AIX
2483 size = lgetea(path, name, NULL, 0);
2484 #elif ARCHIVE_XATTR_FREEBSD
2485 size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
2490 value = malloc(size);
2491 #if ARCHIVE_XATTR_LINUX
2492 size = lgetxattr(path, name, value, size);
2493 #elif ARCHIVE_XATTR_DARWIN
2494 size = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
2495 #elif ARCHIVE_XATTR_AIX
2496 size = lgetea(path, name, value, size);
2497 #elif ARCHIVE_XATTR_FREEBSD
2498 size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
2509 *sizep = (size_t)size;
2510 #else /* !ARCHIVE_XATTR_SUPPORT */
2511 (void)path; /* UNUSED */
2512 (void)name; /* UNUSED */
2514 #endif /* !ARCHIVE_XATTR_SUPPORT */
2519 * Set extended attribute on a path
2520 * Returns 0 on error, 1 on success
2523 setXattr(const char *path, const char *name, const void *value, size_t size)
2525 #if ARCHIVE_XATTR_SUPPORT
2526 #if ARCHIVE_XATTR_LINUX
2527 if (lsetxattr(path, name, value, size, 0) == 0)
2528 #elif ARCHIVE_XATTR_DARWIN
2529 if (setxattr(path, name, value, size, 0, XATTR_NOFOLLOW) == 0)
2530 #elif ARCHIVE_XATTR_AIX
2531 if (lsetea(path, name, value, size, 0) == 0)
2532 #elif ARCHIVE_XATTR_FREEBSD
2533 if (extattr_set_link(path, EXTATTR_NAMESPACE_USER, name + 5, value,
2539 #else /* !ARCHIVE_XATTR_SUPPORT */
2540 (void)path; /* UNUSED */
2541 (void)name; /* UNUSED */
2542 (void)value; /* UNUSED */
2543 (void)size; /* UNUSED */
2544 #endif /* !ARCHIVE_XATTR_SUPPORT */
2548 #if ARCHIVE_ACL_SUNOS
2549 /* Fetch ACLs on Solaris using acl() or facl() */
2551 sunacl_get(int cmd, int *aclcnt, int fd, const char *path)
2557 if (cmd == GETACL) {
2559 size = sizeof(aclent_t);
2561 #if ARCHIVE_ACL_SUNOS_NFS4
2562 else if (cmd == ACE_GETACL) {
2563 cntcmd = ACE_GETACLCNT;
2564 size = sizeof(ace_t);
2575 while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) {
2577 cnt = acl(path, cntcmd, 0, NULL);
2579 cnt = facl(fd, cntcmd, 0, NULL);
2583 aclp = malloc(cnt * size);
2585 aclp = realloc(NULL, cnt * size);
2588 cnt = acl(path, cmd, cnt, aclp);
2590 cnt = facl(fd, cmd, cnt, aclp);
2604 #endif /* ARCHIVE_ACL_SUNOS */
2607 * Set test ACLs on a path
2609 * 0: error setting ACLs
2610 * ARCHIVE_TEST_ACL_TYPE_POSIX1E: POSIX.1E ACLs have been set
2611 * ARCHIVE_TEST_ACL_TYPE_NFS4: NFSv4 or extended ACLs have been set
2614 setTestAcl(const char *path)
2616 #if ARCHIVE_ACL_SUPPORT
2618 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_DARWIN
2621 #if ARCHIVE_ACL_LIBRICHACL
2622 struct richacl *richacl;
2624 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD
2625 const char *acltext_posix1e = "user:1:rw-,"
2631 #elif ARCHIVE_ACL_SUNOS /* Solaris POSIX.1e */
2632 aclent_t aclp_posix1e[] = {
2633 { USER_OBJ, -1, 4 | 2 | 1 },
2635 { GROUP_OBJ, -1, 4 | 2 | 1 },
2636 { GROUP, 15, 4 | 1 },
2637 { CLASS_OBJ, -1, 4 | 2 | 1 },
2638 { OTHER_OBJ, -1, 4 | 2 | 1 }
2641 #if ARCHIVE_ACL_FREEBSD /* FreeBSD NFS4 */
2642 const char *acltext_nfs4 = "user:1:rwpaRcs::allow:1,"
2643 "group:15:rxaRcs::allow:15,"
2644 "owner@:rwpxaARWcCos::allow,"
2645 "group@:rwpxaRcs::allow,"
2646 "everyone@:rxaRcs::allow";
2647 #elif ARCHIVE_ACL_LIBRICHACL
2648 const char *acltext_nfs4 = "owner:rwpxaARWcCoS::mask,"
2649 "group:rwpxaRcS::mask,"
2650 "other:rxaRcS::mask,"
2651 "user:1:rwpaRcS::allow,"
2652 "group:15:rxaRcS::allow,"
2653 "owner@:rwpxaARWcCoS::allow,"
2654 "group@:rwpxaRcS::allow,"
2655 "everyone@:rxaRcS::allow";
2656 #elif ARCHIVE_ACL_SUNOS_NFS4 /* Solaris NFS4 */
2657 ace_t aclp_nfs4[] = {
2658 { 1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2659 ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | ACE_READ_ACL |
2660 ACE_SYNCHRONIZE, 0, ACE_ACCESS_ALLOWED_ACE_TYPE },
2661 { 15, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2662 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2663 ACE_IDENTIFIER_GROUP, ACE_ACCESS_ALLOWED_ACE_TYPE },
2664 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2665 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_WRITE_ATTRIBUTES |
2666 ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS |
2667 ACE_READ_ACL | ACE_WRITE_ACL | ACE_WRITE_OWNER | ACE_SYNCHRONIZE,
2668 ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE },
2669 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2670 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
2671 ACE_READ_ACL | ACE_SYNCHRONIZE, ACE_GROUP | ACE_IDENTIFIER_GROUP,
2672 ACE_ACCESS_ALLOWED_ACE_TYPE },
2673 { -1, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2674 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2675 ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE }
2677 #elif ARCHIVE_ACL_DARWIN /* Mac OS X */
2679 acl_permset_t permset;
2680 const uid_t uid = 1;
2683 const acl_perm_t acl_perms[] = {
2688 ACL_READ_ATTRIBUTES,
2689 ACL_READ_EXTATTRIBUTES,
2691 #if HAVE_DECL_ACL_SYNCHRONIZE
2695 #endif /* ARCHIVE_ACL_DARWIN */
2697 #if ARCHIVE_ACL_FREEBSD
2698 acl = acl_from_text(acltext_nfs4);
2699 failure("acl_from_text() error: %s", strerror(errno));
2700 if (assert(acl != NULL) == 0)
2702 #elif ARCHIVE_ACL_LIBRICHACL
2703 richacl = richacl_from_text(acltext_nfs4, NULL, NULL);
2704 failure("richacl_from_text() error: %s", strerror(errno));
2705 if (assert(richacl != NULL) == 0)
2707 #elif ARCHIVE_ACL_DARWIN
2709 failure("acl_init() error: %s", strerror(errno));
2710 if (assert(acl != NULL) == 0)
2712 r = acl_create_entry(&acl, &aclent);
2713 failure("acl_create_entry() error: %s", strerror(errno));
2714 if (assertEqualInt(r, 0) == 0)
2716 r = acl_set_tag_type(aclent, ACL_EXTENDED_ALLOW);
2717 failure("acl_set_tag_type() error: %s", strerror(errno));
2718 if (assertEqualInt(r, 0) == 0)
2720 r = acl_get_permset(aclent, &permset);
2721 failure("acl_get_permset() error: %s", strerror(errno));
2722 if (assertEqualInt(r, 0) == 0)
2724 for (i = 0; i < (int)(sizeof(acl_perms) / sizeof(acl_perms[0])); i++) {
2725 r = acl_add_perm(permset, acl_perms[i]);
2726 failure("acl_add_perm() error: %s", strerror(errno));
2727 if (assertEqualInt(r, 0) == 0)
2730 r = acl_set_permset(aclent, permset);
2731 failure("acl_set_permset() error: %s", strerror(errno));
2732 if (assertEqualInt(r, 0) == 0)
2734 r = mbr_uid_to_uuid(uid, uuid);
2735 failure("mbr_uid_to_uuid() error: %s", strerror(errno));
2736 if (assertEqualInt(r, 0) == 0)
2738 r = acl_set_qualifier(aclent, uuid);
2739 failure("acl_set_qualifier() error: %s", strerror(errno));
2740 if (assertEqualInt(r, 0) == 0)
2742 #endif /* ARCHIVE_ACL_DARWIN */
2744 #if ARCHIVE_ACL_NFS4
2745 #if ARCHIVE_ACL_FREEBSD
2746 r = acl_set_file(path, ACL_TYPE_NFS4, acl);
2748 #elif ARCHIVE_ACL_LIBRICHACL
2749 r = richacl_set_file(path, richacl);
2750 richacl_free(richacl);
2751 #elif ARCHIVE_ACL_SUNOS_NFS4
2752 r = acl(path, ACE_SETACL,
2753 (int)(sizeof(aclp_nfs4)/sizeof(aclp_nfs4[0])), aclp_nfs4);
2754 #elif ARCHIVE_ACL_DARWIN
2755 r = acl_set_file(path, ACL_TYPE_EXTENDED, acl);
2759 return (ARCHIVE_TEST_ACL_TYPE_NFS4);
2760 #endif /* ARCHIVE_ACL_NFS4 */
2762 #if ARCHIVE_ACL_POSIX1E
2763 #if ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_LIBACL
2764 acl = acl_from_text(acltext_posix1e);
2765 failure("acl_from_text() error: %s", strerror(errno));
2766 if (assert(acl != NULL) == 0)
2769 r = acl_set_file(path, ACL_TYPE_ACCESS, acl);
2771 #elif ARCHIVE_ACL_SUNOS
2772 r = acl(path, SETACL,
2773 (int)(sizeof(aclp_posix1e)/sizeof(aclp_posix1e[0])), aclp_posix1e);
2776 return (ARCHIVE_TEST_ACL_TYPE_POSIX1E);
2779 #endif /* ARCHIVE_ACL_POSIX1E */
2780 #if ARCHIVE_ACL_DARWIN
2784 #endif /* ARCHIVE_ACL_SUPPORT */
2785 (void)path; /* UNUSED */
2790 * Sleep as needed; useful for verifying disk timestamp changes by
2791 * ensuring that the wall-clock time has actually changed before we
2792 * go back to re-read something from disk.
2795 sleepUntilAfter(time_t t)
2797 while (t >= time(NULL))
2798 #if defined(_WIN32) && !defined(__CYGWIN__)
2806 * Call standard system() call, but build up the command line using
2807 * sprintf() conventions.
2810 systemf(const char *fmt, ...)
2817 vsprintf(buff, fmt, ap);
2818 if (verbosity > VERBOSITY_FULL)
2819 logprintf("Cmd: %s\n", buff);
2826 * Slurp a file into memory for ease of comparison and testing.
2827 * Returns size of file in 'sizep' if non-NULL, null-terminates
2828 * data in memory for ease of use.
2831 slurpfile(size_t * sizep, const char *fmt, ...)
2833 char filename[8192];
2842 vsprintf(filename, fmt, ap);
2845 f = fopen(filename, "rb");
2847 /* Note: No error; non-existent file is okay here. */
2850 r = fstat(fileno(f), &st);
2852 logprintf("Can't stat file %s\n", filename);
2856 p = malloc((size_t)st.st_size + 1);
2858 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2859 (long int)st.st_size, filename);
2863 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2864 if (bytes_read < st.st_size) {
2865 logprintf("Can't read file %s\n", filename);
2870 p[st.st_size] = '\0';
2872 *sizep = (size_t)st.st_size;
2878 * Slurp a file into memory for ease of comparison and testing.
2879 * Returns size of file in 'sizep' if non-NULL, null-terminates
2880 * data in memory for ease of use.
2883 dumpfile(const char *filename, void *data, size_t len)
2885 ssize_t bytes_written;
2888 f = fopen(filename, "wb");
2890 logprintf("Can't open file %s for writing\n", filename);
2893 bytes_written = fwrite(data, 1, len, f);
2894 if (bytes_written < (ssize_t)len)
2895 logprintf("Can't write file %s\n", filename);
2899 /* Read a uuencoded file from the reference directory, decode, and
2900 * write the result into the current directory. */
2901 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2902 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2904 extract_reference_file(const char *name)
2909 sprintf(buff, "%s/%s.uu", refdir, name);
2910 in = fopen(buff, "r");
2911 failure("Couldn't open reference file %s", buff);
2915 /* Read up to and including the 'begin' line. */
2917 if (fgets(buff, sizeof(buff), in) == NULL) {
2918 /* TODO: This is a failure. */
2921 if (memcmp(buff, "begin ", 6) == 0)
2924 /* Now, decode the rest and write it. */
2925 out = fopen(name, "wb");
2926 while (fgets(buff, sizeof(buff), in) != NULL) {
2930 if (memcmp(buff, "end", 3) == 0)
2933 bytes = UUDECODE(*p++);
2936 /* Write out 1-3 bytes from that. */
2938 assert(VALID_UUDECODE(p[0]));
2939 assert(VALID_UUDECODE(p[1]));
2940 n = UUDECODE(*p++) << 18;
2941 n |= UUDECODE(*p++) << 12;
2942 fputc(n >> 16, out);
2946 assert(VALID_UUDECODE(p[0]));
2947 n |= UUDECODE(*p++) << 6;
2948 fputc((n >> 8) & 0xFF, out);
2952 assert(VALID_UUDECODE(p[0]));
2953 n |= UUDECODE(*p++);
2954 fputc(n & 0xFF, out);
2964 copy_reference_file(const char *name)
2970 sprintf(buff, "%s/%s", refdir, name);
2971 in = fopen(buff, "rb");
2972 failure("Couldn't open reference file %s", buff);
2976 /* Now, decode the rest and write it. */
2977 /* Not a lot of error checking here; the input better be right. */
2978 out = fopen(name, "wb");
2979 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2980 if (fwrite(buff, 1, rbytes, out) != rbytes) {
2981 logprintf("Error: fwrite\n");
2990 is_LargeInode(const char *file)
2992 #if defined(_WIN32) && !defined(__CYGWIN__)
2993 BY_HANDLE_FILE_INFORMATION bhfi;
2996 r = my_GetFileInformationByName(file, &bhfi);
2999 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
3004 if (stat(file, &st) < 0)
3006 ino = (int64_t)st.st_ino;
3007 return (ino > 0xffffffff);
3012 extract_reference_files(const char **names)
3014 while (names && *names)
3015 extract_reference_file(*names++);
3021 assertion_entry_set_acls(const char *file, int line, struct archive_entry *ae,
3022 struct archive_test_acl_t *acls, int n)
3026 assertion_count(file, line);
3029 archive_entry_acl_clear(ae);
3030 for (i = 0; i < n; i++) {
3031 r = archive_entry_acl_add_entry(ae,
3032 acls[i].type, acls[i].permset, acls[i].tag,
3033 acls[i].qual, acls[i].name);
3036 failure_start(file, line, "type=%#010x, ",
3037 "permset=%#010x, tag=%d, qual=%d name=%s",
3038 acls[i].type, acls[i].permset, acls[i].tag,
3039 acls[i].qual, acls[i].name);
3040 failure_finish(NULL);
3048 archive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset,
3049 int tag, int qual, const char *name)
3051 if (type != acl->type)
3053 if (permset != acl->permset)
3055 if (tag != acl->tag)
3057 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
3059 if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
3061 if (tag == ARCHIVE_ENTRY_ACL_EVERYONE)
3063 if (tag == ARCHIVE_ENTRY_ACL_OTHER)
3065 if (qual != acl->qual)
3068 if (acl->name == NULL || acl->name[0] == '\0')
3072 if (acl->name == NULL) {
3073 if (name[0] == '\0')
3077 return (0 == strcmp(name, acl->name));
3082 assertion_entry_compare_acls(const char *file, int line,
3083 struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt,
3084 int want_type, int mode)
3088 int type, permset, tag, qual;
3092 assertion_count(file, line);
3096 marker = malloc(sizeof(marker[0]) * cnt);
3098 for (i = 0; i < cnt; i++) {
3099 if ((acls[i].type & want_type) != 0) {
3106 failure_start(file, line, "No ACL's to compare, type mask: %d",
3111 while (0 == (r = archive_entry_acl_next(ae, want_type,
3112 &type, &permset, &tag, &qual, &name))) {
3113 for (i = 0, matched = 0; i < n && !matched; i++) {
3114 if (archive_test_acl_match(&acls[marker[i]], type,
3115 permset, tag, qual, name)) {
3116 /* We found a match; remove it. */
3117 marker[i] = marker[n - 1];
3122 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3123 && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
3125 failure_start(file, line, "No match for "
3127 failure_finish(NULL);
3130 if ((permset << 6) != (mode & 0700)) {
3131 failure_start(file, line, "USER_OBJ permset "
3132 "(%02o) != user mode (%02o)", permset,
3134 failure_finish(NULL);
3137 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3138 && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
3140 failure_start(file, line, "No match for "
3142 failure_finish(NULL);
3145 if ((permset << 3) != (mode & 0070)) {
3146 failure_start(file, line, "GROUP_OBJ permset "
3147 "(%02o) != group mode (%02o)", permset,
3149 failure_finish(NULL);
3152 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3153 && tag == ARCHIVE_ENTRY_ACL_OTHER) {
3155 failure_start(file, line, "No match for "
3157 failure_finish(NULL);
3160 if ((permset << 0) != (mode & 0007)) {
3161 failure_start(file, line, "OTHER permset "
3162 "(%02o) != other mode (%02o)", permset,
3164 failure_finish(NULL);
3167 } else if (matched != 1) {
3168 failure_start(file, line, "Could not find match for "
3169 "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d,"
3170 "name=``%s'')", type, permset, tag, qual, name);
3171 failure_finish(NULL);
3175 if (r != ARCHIVE_EOF) {
3176 failure_start(file, line, "Should not exit before EOF");
3177 failure_finish(NULL);
3180 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 &&
3181 (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) {
3182 failure_start(file, line, "Mode (%02o) and entry mode (%02o) "
3183 "mismatch", mode, archive_entry_mode(ae));
3184 failure_finish(NULL);
3188 failure_start(file, line, "Could not find match for ACL "
3189 "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')",
3190 acls[marker[0]].type, acls[marker[0]].permset,
3191 acls[marker[0]].tag, acls[marker[0]].qual,
3192 acls[marker[0]].name);
3193 failure_finish(NULL);
3195 /* Number of ACLs not matched should == 0 */
3200 #endif /* !defined(PROGRAM) */
3209 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
3211 * DEFINE_TEST(test_function)
3215 /* Use "list.h" to declare all of the test functions. */
3217 #define DEFINE_TEST(name) void name(void);
3220 /* Use "list.h" to create a list of all tests (functions and names). */
3222 #define DEFINE_TEST(n) { n, #n, 0 },
3223 struct test_list_t tests[] = {
3228 * Summarize repeated failures in the just-completed test.
3231 test_summarize(int failed, int skips_num)
3235 switch (verbosity) {
3236 case VERBOSITY_SUMMARY_ONLY:
3237 printf(failed ? "E" : ".");
3240 case VERBOSITY_PASSFAIL:
3241 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
3245 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
3247 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
3248 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
3249 logprintf("%s:%d: Summary: Failed %d times\n",
3250 failed_filename, i, failed_lines[i].count);
3252 /* Clear the failure history for the next file. */
3253 failed_filename = NULL;
3254 memset(failed_lines, 0, sizeof(failed_lines));
3258 * Actually run a single test, with appropriate setup and cleanup.
3261 test_run(int i, const char *tmpdir)
3264 char workdir[PATH_MAX];
3268 char logfilename[64];
3269 int failures_before = failures;
3270 int skips_before = skips;
3273 switch (verbosity) {
3274 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
3276 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
3277 printf("%3d: %-64s", i, tests[i].name);
3280 default: /* Title of test, details will follow */
3281 printf("%3d: %s\n", i, tests[i].name);
3284 /* Chdir to the top-level work directory. */
3285 if (!assertChdir(tmpdir)) {
3287 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
3290 /* Create a log file for this test. */
3291 sprintf(logfilename, "%s.log", tests[i].name);
3292 logfile = fopen(logfilename, "w");
3293 fprintf(logfile, "%s\n\n", tests[i].name);
3294 /* Chdir() to a work dir for this specific test. */
3295 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
3296 testworkdir = workdir;
3297 if (!assertMakeDir(testworkdir, 0755)
3298 || !assertChdir(testworkdir)) {
3300 "ERROR: Can't chdir to work dir %s\n", testworkdir);
3303 /* Explicitly reset the locale before each test. */
3304 setlocale(LC_ALL, "C");
3305 /* Record the umask before we run the test. */
3306 umask(oldumask = umask(0));
3308 * Run the actual test.
3312 * Clean up and report afterwards.
3318 setlocale(LC_ALL, "C");
3319 /* Reset directory. */
3320 if (!assertChdir(tmpdir)) {
3321 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
3325 /* Report per-test summaries. */
3326 tests[i].failures = failures - failures_before;
3327 test_summarize(tests[i].failures, skips - skips_before);
3328 /* Close the per-test log file. */
3331 /* If there were no failures, we can remove the work dir and logfile. */
3332 if (tests[i].failures == 0) {
3333 if (!keep_temp_files && assertChdir(tmpdir)) {
3334 #if defined(_WIN32) && !defined(__CYGWIN__)
3335 /* Make sure not to leave empty directories.
3336 * Sometimes a processing of closing files used by tests
3337 * is not done, then rmdir will be failed and it will
3338 * leave a empty test directory. So we should wait a few
3339 * seconds and retry rmdir. */
3341 for (t = 0; t < 10; t++) {
3344 r = systemf("rmdir /S /Q %s", tests[i].name);
3348 systemf("del %s", logfilename);
3350 systemf("rm -rf %s", tests[i].name);
3351 systemf("rm %s", logfilename);
3355 /* Return appropriate status. */
3356 return (tests[i].failures);
3362 * MAIN and support routines.
3368 usage(const char *program)
3370 static const int limit = sizeof(tests) / sizeof(tests[0]);
3373 printf("Usage: %s [options] <test> <test> ...\n", program);
3374 printf("Default is to run all tests.\n");
3375 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
3376 printf("Options:\n");
3377 printf(" -d Dump core after any failure, for debugging.\n");
3378 printf(" -k Keep all temp files.\n");
3379 printf(" Default: temp files for successful tests deleted.\n");
3381 printf(" -p <path> Path to executable to be tested.\n");
3382 printf(" Default: path taken from " ENVBASE " environment variable.\n");
3384 printf(" -q Quiet.\n");
3385 printf(" -r <dir> Path to dir containing reference files.\n");
3386 printf(" Default: Current directory.\n");
3387 printf(" -u Keep running specifies tests until one fails.\n");
3388 printf(" -v Verbose.\n");
3389 printf("Available tests:\n");
3390 for (i = 0; i < limit; i++)
3391 printf(" %d: %s\n", i, tests[i].name);
3396 get_refdir(const char *d)
3398 size_t tried_size, buff_size;
3399 char *buff, *tried, *pwd = NULL, *p = NULL;
3402 buff_size = PATH_MAX;
3406 buff = calloc(buff_size, 1);
3408 fprintf(stderr, "Unable to allocate memory\n");
3412 /* Allocate a buffer to hold the various directories we checked. */
3413 tried_size = buff_size * 2;
3414 tried = calloc(tried_size, 1);
3415 if (tried == NULL) {
3416 fprintf(stderr, "Unable to allocate memory\n");
3420 /* If a dir was specified, try that */
3423 snprintf(buff, buff_size, "%s", d);
3424 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3425 if (p != NULL) goto success;
3426 strncat(tried, buff, tried_size - strlen(tried) - 1);
3427 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3431 /* Get the current dir. */
3433 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3435 pwd = getcwd(NULL, 0);
3437 while (pwd[strlen(pwd) - 1] == '\n')
3438 pwd[strlen(pwd) - 1] = '\0';
3440 /* Look for a known file. */
3441 snprintf(buff, buff_size, "%s", pwd);
3442 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3443 if (p != NULL) goto success;
3444 strncat(tried, buff, tried_size - strlen(tried) - 1);
3445 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3447 snprintf(buff, buff_size, "%s/test", pwd);
3448 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3449 if (p != NULL) goto success;
3450 strncat(tried, buff, tried_size - strlen(tried) - 1);
3451 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3453 #if defined(LIBRARY)
3454 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
3456 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
3458 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3459 if (p != NULL) goto success;
3460 strncat(tried, buff, tried_size - strlen(tried) - 1);
3461 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3463 #if defined(PROGRAM_ALIAS)
3464 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
3465 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3466 if (p != NULL) goto success;
3467 strncat(tried, buff, tried_size - strlen(tried) - 1);
3468 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3471 if (memcmp(pwd, "/usr/obj", 8) == 0) {
3472 snprintf(buff, buff_size, "%s", pwd + 8);
3473 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3474 if (p != NULL) goto success;
3475 strncat(tried, buff, tried_size - strlen(tried) - 1);
3476 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3478 snprintf(buff, buff_size, "%s/test", pwd + 8);
3479 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3480 if (p != NULL) goto success;
3481 strncat(tried, buff, tried_size - strlen(tried) - 1);
3482 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3486 printf("Unable to locate known reference file %s\n", KNOWNREF);
3487 printf(" Checked following directories:\n%s\n", tried);
3488 printf("Use -r option to specify full path to reference directory\n");
3489 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
3499 /* Copy result into a fresh buffer to reduce memory usage. */
3506 main(int argc, char **argv)
3508 static const int limit = sizeof(tests) / sizeof(tests[0]);
3509 int test_set[sizeof(tests) / sizeof(tests[0])];
3510 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
3512 char *refdir_alloc = NULL;
3513 const char *progname;
3515 const char *tmp, *option_arg, *p;
3517 char tmpdir[PATH_MAX];
3521 char *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
3522 char tmpdir_timestamp[32];
3524 (void)argc; /* UNUSED */
3526 /* Get the current dir. */
3528 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3530 pwd = getcwd(NULL, 0);
3532 while (pwd[strlen(pwd) - 1] == '\n')
3533 pwd[strlen(pwd) - 1] = '\0';
3535 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
3536 /* To stop to run the default invalid parameter handler. */
3537 _set_invalid_parameter_handler(invalid_parameter_handler);
3538 /* Disable annoying assertion message box. */
3539 _CrtSetReportMode(_CRT_ASSERT, 0);
3543 * Name of this program, used to build root of our temp directory
3546 progname = p = argv[0];
3547 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
3549 fprintf(stderr, "ERROR: Out of memory.");
3552 strcpy(testprogdir, progname);
3553 while (*p != '\0') {
3554 /* Support \ or / dir separators for Windows compat. */
3555 if (*p == '/' || *p == '\\')
3563 testprogdir[i] = '\0';
3564 #if defined(_WIN32) && !defined(__CYGWIN__)
3565 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
3566 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
3567 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
3568 testprogdir[1] == ':' &&
3569 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
3571 if (testprogdir[0] != '/')
3574 /* Fixup path for relative directories. */
3575 if ((testprogdir = (char *)realloc(testprogdir,
3576 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
3578 fprintf(stderr, "ERROR: Out of memory.");
3581 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
3582 strlen(testprogdir) + 1);
3583 memcpy(testprogdir, pwd, strlen(pwd));
3584 testprogdir[strlen(pwd)] = '/';
3588 /* Get the target program from environment, if available. */
3589 testprogfile = getenv(ENVBASE);
3592 if (getenv("TMPDIR") != NULL)
3593 tmp = getenv("TMPDIR");
3594 else if (getenv("TMP") != NULL)
3595 tmp = getenv("TMP");
3596 else if (getenv("TEMP") != NULL)
3597 tmp = getenv("TEMP");
3598 else if (getenv("TEMPDIR") != NULL)
3599 tmp = getenv("TEMPDIR");
3603 /* Allow -d to be controlled through the environment. */
3604 if (getenv(ENVBASE "_DEBUG") != NULL)
3605 dump_on_failure = 1;
3607 /* Allow -v to be controlled through the environment. */
3608 if (getenv("_VERBOSITY_LEVEL") != NULL)
3610 vlevel = getenv("_VERBOSITY_LEVEL");
3611 verbosity = atoi(vlevel);
3612 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
3614 /* Unsupported verbosity levels are silently ignored */
3616 verbosity = VERBOSITY_PASSFAIL;
3620 /* Get the directory holding test files from environment. */
3621 refdir = getenv(ENVBASE "_TEST_FILES");
3624 * Parse options, without using getopt(), which isn't available
3627 ++argv; /* Skip program name */
3628 while (*argv != NULL) {
3633 while (*p != '\0') {
3636 /* If 'opt' takes an argument, parse that. */
3637 if (option == 'p' || option == 'r') {
3640 else if (*argv == NULL) {
3642 "Option -%c requires argument.\n",
3646 option_arg = *argv++;
3647 p = ""; /* End of this option word. */
3650 /* Now, handle the option. */
3653 dump_on_failure = 1;
3656 keep_temp_files = 1;
3660 testprogfile = option_arg;
3662 fprintf(stderr, "-p option not permitted\n");
3671 refdir = option_arg;
3681 fprintf(stderr, "Unrecognized option '%c'\n",
3689 * Sanity-check that our options make sense.
3692 if (testprogfile == NULL)
3694 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
3695 strlen(PROGRAM) + 1)) == NULL)
3697 fprintf(stderr, "ERROR: Out of memory.");
3700 strcpy(tmp2, testprogdir);
3702 strcat(tmp2, PROGRAM);
3703 testprogfile = tmp2;
3708 #if defined(_WIN32) && !defined(__CYGWIN__)
3709 /* Command.com sometimes rejects '/' separators. */
3710 testprg = strdup(testprogfile);
3711 for (i = 0; testprg[i] != '\0'; i++) {
3712 if (testprg[i] == '/')
3715 testprogfile = testprg;
3717 /* Quote the name that gets put into shell command lines. */
3718 testprg = malloc(strlen(testprogfile) + 3);
3719 strcpy(testprg, "\"");
3720 strcat(testprg, testprogfile);
3721 strcat(testprg, "\"");
3726 #if !defined(_WIN32) && defined(SIGPIPE)
3727 { /* Ignore SIGPIPE signals */
3728 struct sigaction sa;
3729 sa.sa_handler = SIG_IGN;
3730 sigemptyset(&sa.sa_mask);
3732 sigaction(SIGPIPE, &sa, NULL);
3737 * Create a temp directory for the following tests.
3738 * Include the time the tests started as part of the name,
3739 * to make it easier to track the results of multiple tests.
3742 for (i = 0; ; i++) {
3743 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
3744 "%Y-%m-%dT%H.%M.%S",
3746 if ((strlen(tmp) + 1 + strlen(progname) + 1 +
3747 strlen(tmpdir_timestamp) + 1 + 3) >
3748 (sizeof(tmpdir) / sizeof(char))) {
3750 "ERROR: Temp directory pathname too long\n");
3753 snprintf(tmpdir, sizeof(tmpdir), "%s/%s.%s-%03d", tmp,
3754 progname, tmpdir_timestamp, i);
3755 if (assertMakeDir(tmpdir,0755))
3759 "ERROR: Unable to create temp directory %s\n",
3766 * If the user didn't specify a directory for locating
3767 * reference files, try to find the reference files in
3768 * the "usual places."
3770 refdir = refdir_alloc = get_refdir(refdir);
3773 * Banner with basic information.
3776 printf("If tests fail or crash, details will be in:\n");
3777 printf(" %s\n", tmpdir);
3779 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3780 printf("Reference files will be read from: %s\n", refdir);
3782 printf("Running tests on: %s\n", testprog);
3784 printf("Exercising: ");
3786 printf("%s\n", EXTRA_VERSION);
3793 * Run some or all of the individual tests.
3801 test_num = get_test_set(test_set, limit, *argv, tests);
3803 printf("*** INVALID Test %s\n", *argv);
3809 for (i = 0; i < test_num; i++) {
3811 if (test_run(test_set[i], tmpdir)) {
3819 } while (*argv != NULL);
3820 } while (until_failure);
3823 /* Must be freed after all tests run */
3829 * Report summary statistics.
3831 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3833 printf("Totals:\n");
3834 printf(" Tests run: %8d\n", tests_run);
3835 printf(" Tests failed: %8d\n", tests_failed);
3836 printf(" Assertions checked:%8d\n", assertions);
3837 printf(" Assertions failed: %8d\n", failures);
3838 printf(" Skips reported: %8d\n", skips);
3842 printf("Failing tests:\n");
3843 for (i = 0; i < limit; ++i) {
3844 if (tests[i].failures)
3845 printf(" %d: %s (%d failures)\n", i,
3846 tests[i].name, tests[i].failures);
3849 printf("Details for failing tests: %s\n", tmpdir);
3852 if (verbosity == VERBOSITY_SUMMARY_ONLY)
3854 printf("%d tests passed, no failures\n", tests_run);
3859 /* If the final tmpdir is empty, we can remove it. */
3860 /* This should be the usual case when all tests succeed. */
3864 return (tests_failed ? 1 : 0);