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);
1107 for (i = 0; lines[i] != NULL; ++i) {
1108 expected[i] = strdup(lines[i]);
1112 /* Break the file into lines */
1114 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1115 if (*p == '\x0d' || *p == '\x0a')
1117 if (c == '\0' && *p != '\0')
1122 actual = calloc(sizeof(char *), actual_count);
1123 if (actual == NULL) {
1124 failure_start(pathname, line, "Can't allocate memory");
1125 failure_finish(NULL);
1129 for (j = 0, p = buff; p < buff + buff_size;
1130 p += 1 + strlen(p)) {
1138 /* Erase matching lines from both lists */
1139 for (i = 0; i < expected_count; ++i) {
1140 if (expected[i] == NULL)
1142 for (j = 0; j < actual_count; ++j) {
1143 if (actual[j] == NULL)
1145 if (strcmp(expected[i], actual[j]) == 0) {
1154 /* If there's anything left, it's a failure */
1155 for (i = 0; i < expected_count; ++i) {
1156 if (expected[i] != NULL)
1159 for (j = 0; j < actual_count; ++j) {
1160 if (actual[j] != NULL)
1163 if (expected_failure == 0 && actual_failure == 0) {
1169 failure_start(file, line, "File doesn't match: %s", pathname);
1170 for (i = 0; i < expected_count; ++i) {
1171 if (expected[i] != NULL) {
1172 logprintf(" Expected but not present: %s\n", expected[i]);
1176 for (j = 0; j < actual_count; ++j) {
1177 if (actual[j] != NULL)
1178 logprintf(" Present but not expected: %s\n", actual[j]);
1180 failure_finish(NULL);
1187 /* Verify that a text file does not contains the specified strings */
1189 assertion_file_contains_no_invalid_strings(const char *file, int line,
1190 const char *pathname, const char *strings[])
1195 buff = slurpfile(NULL, "%s", pathname);
1197 failure_start(file, line, "Can't read file: %s", pathname);
1198 failure_finish(NULL);
1202 for (i = 0; strings[i] != NULL; ++i) {
1203 if (strstr(buff, strings[i]) != NULL) {
1204 failure_start(file, line, "Invalid string in %s: %s", pathname,
1206 failure_finish(NULL);
1216 /* Test that two paths point to the same file. */
1217 /* As a side-effect, asserts that both files exist. */
1219 is_hardlink(const char *file, int line,
1220 const char *path1, const char *path2)
1222 #if defined(_WIN32) && !defined(__CYGWIN__)
1223 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1226 assertion_count(file, line);
1227 r = my_GetFileInformationByName(path1, &bhfi1);
1229 failure_start(file, line, "File %s can't be inspected?", path1);
1230 failure_finish(NULL);
1233 r = my_GetFileInformationByName(path2, &bhfi2);
1235 failure_start(file, line, "File %s can't be inspected?", path2);
1236 failure_finish(NULL);
1239 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1240 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1241 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1243 struct stat st1, st2;
1246 assertion_count(file, line);
1247 r = lstat(path1, &st1);
1249 failure_start(file, line, "File should exist: %s", path1);
1250 failure_finish(NULL);
1253 r = lstat(path2, &st2);
1255 failure_start(file, line, "File should exist: %s", path2);
1256 failure_finish(NULL);
1259 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1264 assertion_is_hardlink(const char *file, int line,
1265 const char *path1, const char *path2)
1267 if (is_hardlink(file, line, path1, path2))
1269 failure_start(file, line,
1270 "Files %s and %s are not hardlinked", path1, path2);
1271 failure_finish(NULL);
1276 assertion_is_not_hardlink(const char *file, int line,
1277 const char *path1, const char *path2)
1279 if (!is_hardlink(file, line, path1, path2))
1281 failure_start(file, line,
1282 "Files %s and %s should not be hardlinked", path1, path2);
1283 failure_finish(NULL);
1287 /* Verify a/b/mtime of 'pathname'. */
1288 /* If 'recent', verify that it's within last 10 seconds. */
1290 assertion_file_time(const char *file, int line,
1291 const char *pathname, long t, long nsec, char type, int recent)
1293 long long filet, filet_nsec;
1296 #if defined(_WIN32) && !defined(__CYGWIN__)
1297 #define EPOC_TIME (116444736000000000ULL)
1298 FILETIME fxtime, fbirthtime, fatime, fmtime;
1299 ULARGE_INTEGER wintm;
1301 fxtime.dwLowDateTime = 0;
1302 fxtime.dwHighDateTime = 0;
1304 assertion_count(file, line);
1305 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1306 * a directory file. If not, CreateFile() will fail when
1307 * the pathname is a directory. */
1308 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1309 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1310 if (h == INVALID_HANDLE_VALUE) {
1311 failure_start(file, line, "Can't access %s\n", pathname);
1312 failure_finish(NULL);
1315 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1317 case 'a': fxtime = fatime; break;
1318 case 'b': fxtime = fbirthtime; break;
1319 case 'm': fxtime = fmtime; break;
1323 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1324 failure_finish(NULL);
1327 wintm.LowPart = fxtime.dwLowDateTime;
1328 wintm.HighPart = fxtime.dwHighDateTime;
1329 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1330 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1331 nsec = (nsec / 100) * 100; /* Round the request */
1335 assertion_count(file, line);
1336 r = lstat(pathname, &st);
1338 failure_start(file, line, "Can't stat %s\n", pathname);
1339 failure_finish(NULL);
1343 case 'a': filet = st.st_atime; break;
1344 case 'm': filet = st.st_mtime; break;
1345 case 'b': filet = 0; break;
1346 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1349 #if defined(__FreeBSD__)
1351 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1352 case 'b': filet = st.st_birthtime;
1353 /* FreeBSD filesystems that don't support birthtime
1354 * (e.g., UFS1) always return -1 here. */
1358 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1359 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1360 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1363 /* FreeBSD generally only stores to microsecond res, so round. */
1364 filet_nsec = (filet_nsec / 1000) * 1000;
1365 nsec = (nsec / 1000) * 1000;
1367 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1368 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1369 #if defined(__HAIKU__)
1370 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1375 /* Check that requested time is up-to-date. */
1376 time_t now = time(NULL);
1377 if (filet < now - 10 || filet > now + 1) {
1378 failure_start(file, line,
1379 "File %s has %ctime %lld, %lld seconds ago\n",
1380 pathname, type, filet, now - filet);
1381 failure_finish(NULL);
1384 } else if (filet != t || filet_nsec != nsec) {
1385 failure_start(file, line,
1386 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1387 pathname, type, filet, filet_nsec, t, nsec);
1388 failure_finish(NULL);
1394 /* Verify atime of 'pathname'. */
1396 assertion_file_atime(const char *file, int line,
1397 const char *pathname, long t, long nsec)
1399 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1402 /* Verify atime of 'pathname' is up-to-date. */
1404 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1406 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1409 /* Verify birthtime of 'pathname'. */
1411 assertion_file_birthtime(const char *file, int line,
1412 const char *pathname, long t, long nsec)
1414 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1417 /* Verify birthtime of 'pathname' is up-to-date. */
1419 assertion_file_birthtime_recent(const char *file, int line,
1420 const char *pathname)
1422 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1425 /* Verify mode of 'pathname'. */
1427 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1432 assertion_count(file, line);
1433 #if defined(_WIN32) && !defined(__CYGWIN__)
1434 failure_start(file, line, "assertFileMode not yet implemented for Windows");
1435 (void)mode; /* UNUSED */
1436 (void)r; /* UNUSED */
1437 (void)pathname; /* UNUSED */
1438 (void)expected_mode; /* UNUSED */
1442 r = lstat(pathname, &st);
1443 mode = (int)(st.st_mode & 0777);
1445 if (r == 0 && mode == expected_mode)
1447 failure_start(file, line, "File %s has mode %o, expected %o",
1448 pathname, mode, expected_mode);
1450 failure_finish(NULL);
1454 /* Verify mtime of 'pathname'. */
1456 assertion_file_mtime(const char *file, int line,
1457 const char *pathname, long t, long nsec)
1459 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1462 /* Verify mtime of 'pathname' is up-to-date. */
1464 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1466 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1469 /* Verify number of links to 'pathname'. */
1471 assertion_file_nlinks(const char *file, int line,
1472 const char *pathname, int nlinks)
1474 #if defined(_WIN32) && !defined(__CYGWIN__)
1475 BY_HANDLE_FILE_INFORMATION bhfi;
1478 assertion_count(file, line);
1479 r = my_GetFileInformationByName(pathname, &bhfi);
1480 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1482 failure_start(file, line, "File %s has %d links, expected %d",
1483 pathname, bhfi.nNumberOfLinks, nlinks);
1484 failure_finish(NULL);
1490 assertion_count(file, line);
1491 r = lstat(pathname, &st);
1492 if (r == 0 && (int)st.st_nlink == nlinks)
1494 failure_start(file, line, "File %s has %d links, expected %d",
1495 pathname, st.st_nlink, nlinks);
1496 failure_finish(NULL);
1501 /* Verify size of 'pathname'. */
1503 assertion_file_size(const char *file, int line, const char *pathname, long size)
1508 assertion_count(file, line);
1509 #if defined(_WIN32) && !defined(__CYGWIN__)
1511 BY_HANDLE_FILE_INFORMATION bhfi;
1512 r = !my_GetFileInformationByName(pathname, &bhfi);
1513 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1518 r = lstat(pathname, &st);
1519 filesize = st.st_size;
1522 if (r == 0 && filesize == size)
1524 failure_start(file, line, "File %s has size %ld, expected %ld",
1525 pathname, (long)filesize, (long)size);
1526 failure_finish(NULL);
1530 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1532 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1537 #if defined(_WIN32) && !defined(__CYGWIN__)
1538 (void)mode; /* UNUSED */
1540 assertion_count(file, line);
1541 r = lstat(pathname, &st);
1543 failure_start(file, line, "Dir should exist: %s", pathname);
1544 failure_finish(NULL);
1547 if (!S_ISDIR(st.st_mode)) {
1548 failure_start(file, line, "%s is not a dir", pathname);
1549 failure_finish(NULL);
1552 #if !defined(_WIN32) || defined(__CYGWIN__)
1553 /* Windows doesn't handle permissions the same way as POSIX,
1554 * so just ignore the mode tests. */
1555 /* TODO: Can we do better here? */
1556 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1557 failure_start(file, line, "Dir %s has wrong mode", pathname);
1558 logprintf(" Expected: 0%3o\n", mode);
1559 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1560 failure_finish(NULL);
1567 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1568 * verify that too. */
1570 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1575 #if defined(_WIN32) && !defined(__CYGWIN__)
1576 (void)mode; /* UNUSED */
1578 assertion_count(file, line);
1579 r = lstat(pathname, &st);
1580 if (r != 0 || !S_ISREG(st.st_mode)) {
1581 failure_start(file, line, "File should exist: %s", pathname);
1582 failure_finish(NULL);
1585 #if !defined(_WIN32) || defined(__CYGWIN__)
1586 /* Windows doesn't handle permissions the same way as POSIX,
1587 * so just ignore the mode tests. */
1588 /* TODO: Can we do better here? */
1589 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1590 failure_start(file, line, "File %s has wrong mode", pathname);
1591 logprintf(" Expected: 0%3o\n", mode);
1592 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1593 failure_finish(NULL);
1600 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1601 * non-NULL, verify that the symlink has those contents. */
1603 is_symlink(const char *file, int line,
1604 const char *pathname, const char *contents)
1606 #if defined(_WIN32) && !defined(__CYGWIN__)
1607 (void)pathname; /* UNUSED */
1608 (void)contents; /* UNUSED */
1609 assertion_count(file, line);
1610 /* Windows sort-of has real symlinks, but they're only usable
1611 * by privileged users and are crippled even then, so there's
1612 * really not much point in bothering with this. */
1620 assertion_count(file, line);
1621 r = lstat(pathname, &st);
1623 failure_start(file, line,
1624 "Symlink should exist: %s", pathname);
1625 failure_finish(NULL);
1628 if (!S_ISLNK(st.st_mode))
1630 if (contents == NULL)
1632 linklen = readlink(pathname, buff, sizeof(buff));
1634 failure_start(file, line, "Can't read symlink %s", pathname);
1635 failure_finish(NULL);
1638 buff[linklen] = '\0';
1639 if (strcmp(buff, contents) != 0)
1645 /* Assert that path is a symlink that (optionally) contains contents. */
1647 assertion_is_symlink(const char *file, int line,
1648 const char *path, const char *contents)
1650 if (is_symlink(file, line, path, contents))
1653 failure_start(file, line, "File %s is not a symlink to %s",
1656 failure_start(file, line, "File %s is not a symlink", path);
1657 failure_finish(NULL);
1662 /* Create a directory and report any errors. */
1664 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1666 assertion_count(file, line);
1667 #if defined(_WIN32) && !defined(__CYGWIN__)
1668 (void)mode; /* UNUSED */
1669 if (0 == _mkdir(dirname))
1672 if (0 == mkdir(dirname, mode)) {
1673 if (0 == chmod(dirname, mode)) {
1674 assertion_file_mode(file, line, dirname, mode);
1679 failure_start(file, line, "Could not create directory %s", dirname);
1680 failure_finish(NULL);
1684 /* Create a file with the specified contents and report any failures. */
1686 assertion_make_file(const char *file, int line,
1687 const char *path, int mode, int csize, const void *contents)
1689 #if defined(_WIN32) && !defined(__CYGWIN__)
1690 /* TODO: Rework this to set file mode as well. */
1692 (void)mode; /* UNUSED */
1693 assertion_count(file, line);
1694 f = fopen(path, "wb");
1696 failure_start(file, line, "Could not create file %s", path);
1697 failure_finish(NULL);
1700 if (contents != NULL) {
1704 wsize = strlen(contents);
1706 wsize = (size_t)csize;
1707 if (wsize != fwrite(contents, 1, wsize, f)) {
1709 failure_start(file, line,
1710 "Could not write file %s", path);
1711 failure_finish(NULL);
1719 assertion_count(file, line);
1720 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1722 failure_start(file, line, "Could not create %s", path);
1723 failure_finish(NULL);
1726 if (0 != chmod(path, mode)) {
1727 failure_start(file, line, "Could not chmod %s", path);
1728 failure_finish(NULL);
1732 if (contents != NULL) {
1736 wsize = (ssize_t)strlen(contents);
1738 wsize = (ssize_t)csize;
1739 if (wsize != write(fd, contents, wsize)) {
1741 failure_start(file, line,
1742 "Could not write to %s", path);
1743 failure_finish(NULL);
1749 assertion_file_mode(file, line, path, mode);
1754 /* Create a hardlink and report any failures. */
1756 assertion_make_hardlink(const char *file, int line,
1757 const char *newpath, const char *linkto)
1761 assertion_count(file, line);
1762 #if defined(_WIN32) && !defined(__CYGWIN__)
1763 succeeded = my_CreateHardLinkA(newpath, linkto);
1765 succeeded = !link(linkto, newpath);
1771 failure_start(file, line, "Could not create hardlink");
1772 logprintf(" New link: %s\n", newpath);
1773 logprintf(" Old name: %s\n", linkto);
1774 failure_finish(NULL);
1778 /* Create a symlink and report any failures. */
1780 assertion_make_symlink(const char *file, int line,
1781 const char *newpath, const char *linkto)
1783 #if defined(_WIN32) && !defined(__CYGWIN__)
1784 int targetIsDir = 0; /* TODO: Fix this */
1785 assertion_count(file, line);
1786 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1789 assertion_count(file, line);
1790 if (0 == symlink(linkto, newpath))
1793 failure_start(file, line, "Could not create symlink");
1794 logprintf(" New link: %s\n", newpath);
1795 logprintf(" Old name: %s\n", linkto);
1796 failure_finish(NULL);
1800 /* Set umask, report failures. */
1802 assertion_umask(const char *file, int line, int mask)
1804 assertion_count(file, line);
1805 (void)file; /* UNUSED */
1806 (void)line; /* UNUSED */
1811 /* Set times, report failures. */
1813 assertion_utimes(const char *file, int line,
1814 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1818 #if defined(_WIN32) && !defined(__CYGWIN__)
1819 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1820 + (((nsec)/1000)*10))
1822 ULARGE_INTEGER wintm;
1823 FILETIME fatime, fmtime;
1824 FILETIME *pat, *pmt;
1826 assertion_count(file, line);
1827 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1828 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1829 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1830 if (h == INVALID_HANDLE_VALUE) {
1831 failure_start(file, line, "Can't access %s\n", pathname);
1832 failure_finish(NULL);
1836 if (at > 0 || at_nsec > 0) {
1837 wintm.QuadPart = WINTIME(at, at_nsec);
1838 fatime.dwLowDateTime = wintm.LowPart;
1839 fatime.dwHighDateTime = wintm.HighPart;
1843 if (mt > 0 || mt_nsec > 0) {
1844 wintm.QuadPart = WINTIME(mt, mt_nsec);
1845 fmtime.dwLowDateTime = wintm.LowPart;
1846 fmtime.dwHighDateTime = wintm.HighPart;
1850 if (pat != NULL || pmt != NULL)
1851 r = SetFileTime(h, NULL, pat, pmt);
1856 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1857 failure_finish(NULL);
1861 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1863 struct timeval times[2];
1865 #if !defined(__FreeBSD__)
1866 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1868 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1871 r = lstat(pathname, &st);
1873 failure_start(file, line, "Can't stat %s\n", pathname);
1874 failure_finish(NULL);
1878 if (mt == 0 && mt_nsec == 0) {
1880 #if defined(__FreeBSD__)
1881 mt_nsec = st.st_mtimespec.tv_nsec;
1882 /* FreeBSD generally only stores to microsecond res, so round. */
1883 mt_nsec = (mt_nsec / 1000) * 1000;
1886 if (at == 0 && at_nsec == 0) {
1888 #if defined(__FreeBSD__)
1889 at_nsec = st.st_atimespec.tv_nsec;
1890 /* FreeBSD generally only stores to microsecond res, so round. */
1891 at_nsec = (at_nsec / 1000) * 1000;
1895 times[1].tv_sec = mt;
1896 times[1].tv_usec = mt_nsec / 1000;
1898 times[0].tv_sec = at;
1899 times[0].tv_usec = at_nsec / 1000;
1902 r = lutimes(pathname, times);
1904 r = utimes(pathname, times);
1907 failure_start(file, line, "Can't utimes %s\n", pathname);
1908 failure_finish(NULL);
1912 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1915 /* Compare file flags */
1917 assertion_compare_fflags(const char *file, int line, const char *patha,
1918 const char *pathb, int nomatch)
1920 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1923 assertion_count(file, line);
1925 if (stat(patha, &sa) < 0)
1927 if (stat(pathb, &sb) < 0)
1929 if (!nomatch && sa.st_flags != sb.st_flags) {
1930 failure_start(file, line, "File flags should be identical: "
1931 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
1933 failure_finish(NULL);
1936 if (nomatch && sa.st_flags == sb.st_flags) {
1937 failure_start(file, line, "File flags should be different: "
1938 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
1940 failure_finish(NULL);
1943 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
1944 defined(FS_NODUMP_FL)) || \
1945 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
1946 && defined(EXT2_NODUMP_FL))
1947 int fd, r, flagsa, flagsb;
1949 assertion_count(file, line);
1950 fd = open(patha, O_RDONLY | O_NONBLOCK);
1952 failure_start(file, line, "Can't open %s\n", patha);
1953 failure_finish(NULL);
1957 #ifdef FS_IOC_GETFLAGS
1965 failure_start(file, line, "Can't get flags %s\n", patha);
1966 failure_finish(NULL);
1969 fd = open(pathb, O_RDONLY | O_NONBLOCK);
1971 failure_start(file, line, "Can't open %s\n", pathb);
1972 failure_finish(NULL);
1976 #ifdef FS_IOC_GETFLAGS
1984 failure_start(file, line, "Can't get flags %s\n", pathb);
1985 failure_finish(NULL);
1988 if (!nomatch && flagsa != flagsb) {
1989 failure_start(file, line, "File flags should be identical: "
1990 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
1991 failure_finish(NULL);
1994 if (nomatch && flagsa == flagsb) {
1995 failure_start(file, line, "File flags should be different: "
1996 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
1997 failure_finish(NULL);
2001 (void)patha; /* UNUSED */
2002 (void)pathb; /* UNUSED */
2003 (void)nomatch; /* UNUSED */
2004 assertion_count(file, line);
2009 /* Set nodump, report failures. */
2011 assertion_set_nodump(const char *file, int line, const char *pathname)
2013 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2016 assertion_count(file, line);
2017 r = chflags(pathname, UF_NODUMP);
2019 failure_start(file, line, "Can't set nodump %s\n", pathname);
2020 failure_finish(NULL);
2023 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
2024 defined(FS_NODUMP_FL)) || \
2025 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2026 && defined(EXT2_NODUMP_FL))
2029 assertion_count(file, line);
2030 fd = open(pathname, O_RDONLY | O_NONBLOCK);
2032 failure_start(file, line, "Can't open %s\n", pathname);
2033 failure_finish(NULL);
2037 #ifdef FS_IOC_GETFLAGS
2044 failure_start(file, line, "Can't get flags %s\n", pathname);
2045 failure_finish(NULL);
2049 flags |= FS_NODUMP_FL;
2051 flags |= EXT2_NODUMP_FL;
2055 #ifdef FS_IOC_SETFLAGS
2062 failure_start(file, line, "Can't set nodump %s\n", pathname);
2063 failure_finish(NULL);
2068 (void)pathname; /* UNUSED */
2069 assertion_count(file, line);
2075 static void assert_version_id(char **qq, size_t *ss)
2080 /* Version number is a series of digits and periods. */
2081 while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
2086 if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') {
2091 /* Skip a single trailing a,b,c, or d. */
2092 if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
2095 /* Version number terminated by space. */
2096 failure("No space after version: ``%s''", q);
2098 failure("No space after version: ``%s''", q);
2109 * Check program version
2111 void assertVersion(const char *prog, const char *base)
2116 unsigned int prog_len = strlen(base);
2118 r = systemf("%s --version >version.stdout 2>version.stderr", prog);
2120 r = systemf("%s -W version >version.stdout 2>version.stderr",
2123 failure("Unable to run either %s --version or %s -W version",
2125 if (!assert(r == 0))
2128 /* --version should generate nothing to stdout. */
2129 assertEmptyFile("version.stderr");
2131 /* Verify format of version message. */
2132 q = p = slurpfile(&s, "version.stdout");
2134 /* Version message should start with name of program, then space. */
2135 assert(s > prog_len + 1);
2137 failure("Version must start with '%s': ``%s''", base, p);
2138 if (!assertEqualMem(q, base, prog_len)) {
2143 q += prog_len; s -= prog_len;
2148 assert_version_id(&q, &s);
2151 failure("No `-' between program name and versions: ``%s''", p);
2152 assertEqualMem(q, "- ", 2);
2155 failure("Not long enough for libarchive version: ``%s''", p);
2158 failure("Libarchive version must start with `libarchive': ``%s''", p);
2159 assertEqualMem(q, "libarchive ", 11);
2163 assert_version_id(&q, &s);
2165 /* Skip arbitrary third-party version numbers. */
2166 while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' ||
2172 /* All terminated by end-of-line. */
2175 /* Skip an optional CR character (e.g., Windows) */
2176 failure("Version output must end with \\n or \\r\\n");
2178 if (*q == '\r') { ++q; --s; }
2179 assertEqualMem(q, "\n", 1);
2183 #endif /* PROGRAM */
2187 * UTILITIES for use by tests.
2192 * Check whether platform supports symlinks. This is intended
2193 * for tests to use in deciding whether to bother testing symlink
2194 * support; if the platform doesn't support symlinks, there's no point
2195 * in checking whether the program being tested can create them.
2197 * Note that the first time this test is called, we actually go out to
2198 * disk to create and verify a symlink. This is necessary because
2199 * symlink support is actually a property of a particular filesystem
2200 * and can thus vary between directories on a single system. After
2201 * the first call, this returns the cached result from memory, so it's
2202 * safe to call it as often as you wish.
2207 /* Remember the test result */
2208 static int value = 0, tested = 0;
2213 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
2214 /* Note: Cygwin has its own symlink() emulation that does not
2215 * use the Win32 CreateSymbolicLink() function. */
2216 #if defined(_WIN32) && !defined(__CYGWIN__)
2217 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
2218 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
2220 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
2221 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
2226 /* Platform-dependent options for hiding the output of a subcommand. */
2227 #if defined(_WIN32) && !defined(__CYGWIN__)
2228 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
2230 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
2233 * Can this platform run the bzip2 program?
2238 static int tested = 0, value = 0;
2241 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
2248 * Can this platform run the grzip program?
2253 static int tested = 0, value = 0;
2256 if (systemf("grzip -V %s", redirectArgs) == 0)
2263 * Can this platform run the gzip program?
2268 static int tested = 0, value = 0;
2271 if (systemf("gzip -V %s", redirectArgs) == 0)
2278 * Can this platform run the lrzip program?
2281 canRunCommand(const char *cmd)
2283 static int tested = 0, value = 0;
2286 if (systemf("%s %s", cmd, redirectArgs) == 0)
2295 static int tested = 0, value = 0;
2298 if (systemf("lrzip -V %s", redirectArgs) == 0)
2305 * Can this platform run the lz4 program?
2310 static int tested = 0, value = 0;
2313 if (systemf("lz4 -V %s", redirectArgs) == 0)
2320 * Can this platform run the lzip program?
2325 static int tested = 0, value = 0;
2328 if (systemf("lzip -V %s", redirectArgs) == 0)
2335 * Can this platform run the lzma program?
2340 static int tested = 0, value = 0;
2343 if (systemf("lzma -V %s", redirectArgs) == 0)
2350 * Can this platform run the lzop program?
2355 static int tested = 0, value = 0;
2358 if (systemf("lzop -V %s", redirectArgs) == 0)
2365 * Can this platform run the xz program?
2370 static int tested = 0, value = 0;
2373 if (systemf("xz -V %s", redirectArgs) == 0)
2380 * Can this filesystem handle nodump flags.
2385 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2386 const char *path = "cannodumptest";
2389 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2390 if (chflags(path, UF_NODUMP) < 0)
2392 if (stat(path, &sb) < 0)
2394 if (sb.st_flags & UF_NODUMP)
2396 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \
2397 && defined(FS_NODUMP_FL)) || \
2398 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2399 && defined(EXT2_NODUMP_FL))
2400 const char *path = "cannodumptest";
2403 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2404 fd = open(path, O_RDONLY | O_NONBLOCK);
2408 #ifdef FS_IOC_GETFLAGS
2417 flags |= FS_NODUMP_FL;
2419 flags |= EXT2_NODUMP_FL;
2422 #ifdef FS_IOC_SETFLAGS
2431 fd = open(path, O_RDONLY | O_NONBLOCK);
2435 #ifdef FS_IOC_GETFLAGS
2445 if (flags & FS_NODUMP_FL)
2447 if (flags & EXT2_NODUMP_FL)
2454 /* Get extended attribute from a path */
2456 getXattr(const char *path, const char *name, size_t *sizep)
2459 #if ARCHIVE_XATTR_SUPPORT
2461 #if ARCHIVE_XATTR_LINUX
2462 size = lgetxattr(path, name, NULL, 0);
2463 #elif ARCHIVE_XATTR_DARWIN
2464 size = getxattr(path, name, NULL, 0, 0, XATTR_NOFOLLOW);
2465 #elif ARCHIVE_XATTR_AIX
2466 size = lgetea(path, name, NULL, 0);
2467 #elif ARCHIVE_XATTR_FREEBSD
2468 size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
2473 value = malloc(size);
2474 #if ARCHIVE_XATTR_LINUX
2475 size = lgetxattr(path, name, value, size);
2476 #elif ARCHIVE_XATTR_DARWIN
2477 size = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
2478 #elif ARCHIVE_XATTR_AIX
2479 size = lgetea(path, name, value, size);
2480 #elif ARCHIVE_XATTR_FREEBSD
2481 size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
2492 *sizep = (size_t)size;
2493 #else /* !ARCHIVE_XATTR_SUPPORT */
2494 (void)path; /* UNUSED */
2495 (void)name; /* UNUSED */
2497 #endif /* !ARCHIVE_XATTR_SUPPORT */
2502 * Set extended attribute on a path
2503 * Returns 0 on error, 1 on success
2506 setXattr(const char *path, const char *name, const void *value, size_t size)
2508 #if ARCHIVE_XATTR_SUPPORT
2509 #if ARCHIVE_XATTR_LINUX
2510 if (lsetxattr(path, name, value, size, 0) == 0)
2511 #elif ARCHIVE_XATTR_DARWIN
2512 if (setxattr(path, name, value, size, 0, XATTR_NOFOLLOW) == 0)
2513 #elif ARCHIVE_XATTR_AIX
2514 if (lsetea(path, name, value, size, 0) == 0)
2515 #elif ARCHIVE_XATTR_FREEBSD
2516 if (extattr_set_link(path, EXTATTR_NAMESPACE_USER, name + 5, value,
2522 #else /* !ARCHIVE_XATTR_SUPPORT */
2523 (void)path; /* UNUSED */
2524 (void)name; /* UNUSED */
2525 (void)value; /* UNUSED */
2526 (void)size; /* UNUSED */
2527 #endif /* !ARCHIVE_XATTR_SUPPORT */
2531 #if ARCHIVE_ACL_SUNOS
2532 /* Fetch ACLs on Solaris using acl() or facl() */
2534 sunacl_get(int cmd, int *aclcnt, int fd, const char *path)
2540 if (cmd == GETACL) {
2542 size = sizeof(aclent_t);
2544 #if ARCHIVE_ACL_SUNOS_NFS4
2545 else if (cmd == ACE_GETACL) {
2546 cntcmd = ACE_GETACLCNT;
2547 size = sizeof(ace_t);
2558 while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) {
2560 cnt = acl(path, cntcmd, 0, NULL);
2562 cnt = facl(fd, cntcmd, 0, NULL);
2566 aclp = malloc(cnt * size);
2568 aclp = realloc(NULL, cnt * size);
2571 cnt = acl(path, cmd, cnt, aclp);
2573 cnt = facl(fd, cmd, cnt, aclp);
2587 #endif /* ARCHIVE_ACL_SUNOS */
2590 * Set test ACLs on a path
2592 * 0: error setting ACLs
2593 * ARCHIVE_TEST_ACL_TYPE_POSIX1E: POSIX.1E ACLs have been set
2594 * ARCHIVE_TEST_ACL_TYPE_NFS4: NFSv4 or extended ACLs have been set
2597 setTestAcl(const char *path)
2599 #if ARCHIVE_ACL_SUPPORT
2601 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_DARWIN
2604 #if ARCHIVE_ACL_LIBRICHACL
2605 struct richacl *richacl;
2607 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD
2608 const char *acltext_posix1e = "user:1:rw-,"
2614 #elif ARCHIVE_ACL_SUNOS /* Solaris POSIX.1e */
2615 aclent_t aclp_posix1e[] = {
2616 { USER_OBJ, -1, 4 | 2 | 1 },
2618 { GROUP_OBJ, -1, 4 | 2 | 1 },
2619 { GROUP, 15, 4 | 1 },
2620 { CLASS_OBJ, -1, 4 | 2 | 1 },
2621 { OTHER_OBJ, -1, 4 | 2 | 1 }
2624 #if ARCHIVE_ACL_FREEBSD /* FreeBSD NFS4 */
2625 const char *acltext_nfs4 = "user:1:rwpaRcs::allow:1,"
2626 "group:15:rxaRcs::allow:15,"
2627 "owner@:rwpxaARWcCos::allow,"
2628 "group@:rwpxaRcs::allow,"
2629 "everyone@:rxaRcs::allow";
2630 #elif ARCHIVE_ACL_LIBRICHACL
2631 const char *acltext_nfs4 = "owner:rwpxaARWcCoS::mask,"
2632 "group:rwpxaRcS::mask,"
2633 "other:rxaRcS::mask,"
2634 "user:1:rwpaRcS::allow,"
2635 "group:15:rxaRcS::allow,"
2636 "owner@:rwpxaARWcCoS::allow,"
2637 "group@:rwpxaRcS::allow,"
2638 "everyone@:rxaRcS::allow";
2639 #elif ARCHIVE_ACL_SUNOS_NFS4 /* Solaris NFS4 */
2640 ace_t aclp_nfs4[] = {
2641 { 1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2642 ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | ACE_READ_ACL |
2643 ACE_SYNCHRONIZE, 0, ACE_ACCESS_ALLOWED_ACE_TYPE },
2644 { 15, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2645 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2646 ACE_IDENTIFIER_GROUP, ACE_ACCESS_ALLOWED_ACE_TYPE },
2647 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2648 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_WRITE_ATTRIBUTES |
2649 ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS |
2650 ACE_READ_ACL | ACE_WRITE_ACL | ACE_WRITE_OWNER | ACE_SYNCHRONIZE,
2651 ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE },
2652 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2653 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
2654 ACE_READ_ACL | ACE_SYNCHRONIZE, ACE_GROUP | ACE_IDENTIFIER_GROUP,
2655 ACE_ACCESS_ALLOWED_ACE_TYPE },
2656 { -1, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2657 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2658 ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE }
2660 #elif ARCHIVE_ACL_DARWIN /* Mac OS X */
2662 acl_permset_t permset;
2663 const uid_t uid = 1;
2666 const acl_perm_t acl_perms[] = {
2671 ACL_READ_ATTRIBUTES,
2672 ACL_READ_EXTATTRIBUTES,
2674 #if HAVE_DECL_ACL_SYNCHRONIZE
2678 #endif /* ARCHIVE_ACL_DARWIN */
2680 #if ARCHIVE_ACL_FREEBSD
2681 acl = acl_from_text(acltext_nfs4);
2682 failure("acl_from_text() error: %s", strerror(errno));
2683 if (assert(acl != NULL) == 0)
2685 #elif ARCHIVE_ACL_LIBRICHACL
2686 richacl = richacl_from_text(acltext_nfs4, NULL, NULL);
2687 failure("richacl_from_text() error: %s", strerror(errno));
2688 if (assert(richacl != NULL) == 0)
2690 #elif ARCHIVE_ACL_DARWIN
2692 failure("acl_init() error: %s", strerror(errno));
2693 if (assert(acl != NULL) == 0)
2695 r = acl_create_entry(&acl, &aclent);
2696 failure("acl_create_entry() error: %s", strerror(errno));
2697 if (assertEqualInt(r, 0) == 0)
2699 r = acl_set_tag_type(aclent, ACL_EXTENDED_ALLOW);
2700 failure("acl_set_tag_type() error: %s", strerror(errno));
2701 if (assertEqualInt(r, 0) == 0)
2703 r = acl_get_permset(aclent, &permset);
2704 failure("acl_get_permset() error: %s", strerror(errno));
2705 if (assertEqualInt(r, 0) == 0)
2707 for (i = 0; i < (int)(sizeof(acl_perms) / sizeof(acl_perms[0])); i++) {
2708 r = acl_add_perm(permset, acl_perms[i]);
2709 failure("acl_add_perm() error: %s", strerror(errno));
2710 if (assertEqualInt(r, 0) == 0)
2713 r = acl_set_permset(aclent, permset);
2714 failure("acl_set_permset() error: %s", strerror(errno));
2715 if (assertEqualInt(r, 0) == 0)
2717 r = mbr_uid_to_uuid(uid, uuid);
2718 failure("mbr_uid_to_uuid() error: %s", strerror(errno));
2719 if (assertEqualInt(r, 0) == 0)
2721 r = acl_set_qualifier(aclent, uuid);
2722 failure("acl_set_qualifier() error: %s", strerror(errno));
2723 if (assertEqualInt(r, 0) == 0)
2725 #endif /* ARCHIVE_ACL_DARWIN */
2727 #if ARCHIVE_ACL_NFS4
2728 #if ARCHIVE_ACL_FREEBSD
2729 r = acl_set_file(path, ACL_TYPE_NFS4, acl);
2731 #elif ARCHIVE_ACL_LIBRICHACL
2732 r = richacl_set_file(path, richacl);
2733 richacl_free(richacl);
2734 #elif ARCHIVE_ACL_SUNOS_NFS4
2735 r = acl(path, ACE_SETACL,
2736 (int)(sizeof(aclp_nfs4)/sizeof(aclp_nfs4[0])), aclp_nfs4);
2737 #elif ARCHIVE_ACL_DARWIN
2738 r = acl_set_file(path, ACL_TYPE_EXTENDED, acl);
2742 return (ARCHIVE_TEST_ACL_TYPE_NFS4);
2743 #endif /* ARCHIVE_ACL_NFS4 */
2745 #if ARCHIVE_ACL_POSIX1E
2746 #if ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_LIBACL
2747 acl = acl_from_text(acltext_posix1e);
2748 failure("acl_from_text() error: %s", strerror(errno));
2749 if (assert(acl != NULL) == 0)
2752 r = acl_set_file(path, ACL_TYPE_ACCESS, acl);
2754 #elif ARCHIVE_ACL_SUNOS
2755 r = acl(path, SETACL,
2756 (int)(sizeof(aclp_posix1e)/sizeof(aclp_posix1e[0])), aclp_posix1e);
2759 return (ARCHIVE_TEST_ACL_TYPE_POSIX1E);
2762 #endif /* ARCHIVE_ACL_POSIX1E */
2763 #if ARCHIVE_ACL_DARWIN
2767 #endif /* ARCHIVE_ACL_SUPPORT */
2768 (void)path; /* UNUSED */
2773 * Sleep as needed; useful for verifying disk timestamp changes by
2774 * ensuring that the wall-clock time has actually changed before we
2775 * go back to re-read something from disk.
2778 sleepUntilAfter(time_t t)
2780 while (t >= time(NULL))
2781 #if defined(_WIN32) && !defined(__CYGWIN__)
2789 * Call standard system() call, but build up the command line using
2790 * sprintf() conventions.
2793 systemf(const char *fmt, ...)
2800 vsprintf(buff, fmt, ap);
2801 if (verbosity > VERBOSITY_FULL)
2802 logprintf("Cmd: %s\n", buff);
2809 * Slurp a file into memory for ease of comparison and testing.
2810 * Returns size of file in 'sizep' if non-NULL, null-terminates
2811 * data in memory for ease of use.
2814 slurpfile(size_t * sizep, const char *fmt, ...)
2816 char filename[8192];
2825 vsprintf(filename, fmt, ap);
2828 f = fopen(filename, "rb");
2830 /* Note: No error; non-existent file is okay here. */
2833 r = fstat(fileno(f), &st);
2835 logprintf("Can't stat file %s\n", filename);
2839 p = malloc((size_t)st.st_size + 1);
2841 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2842 (long int)st.st_size, filename);
2846 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2847 if (bytes_read < st.st_size) {
2848 logprintf("Can't read file %s\n", filename);
2853 p[st.st_size] = '\0';
2855 *sizep = (size_t)st.st_size;
2861 * Slurp a file into memory for ease of comparison and testing.
2862 * Returns size of file in 'sizep' if non-NULL, null-terminates
2863 * data in memory for ease of use.
2866 dumpfile(const char *filename, void *data, size_t len)
2868 ssize_t bytes_written;
2871 f = fopen(filename, "wb");
2873 logprintf("Can't open file %s for writing\n", filename);
2876 bytes_written = fwrite(data, 1, len, f);
2877 if (bytes_written < (ssize_t)len)
2878 logprintf("Can't write file %s\n", filename);
2882 /* Read a uuencoded file from the reference directory, decode, and
2883 * write the result into the current directory. */
2884 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2885 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2887 extract_reference_file(const char *name)
2892 sprintf(buff, "%s/%s.uu", refdir, name);
2893 in = fopen(buff, "r");
2894 failure("Couldn't open reference file %s", buff);
2898 /* Read up to and including the 'begin' line. */
2900 if (fgets(buff, sizeof(buff), in) == NULL) {
2901 /* TODO: This is a failure. */
2904 if (memcmp(buff, "begin ", 6) == 0)
2907 /* Now, decode the rest and write it. */
2908 out = fopen(name, "wb");
2909 while (fgets(buff, sizeof(buff), in) != NULL) {
2913 if (memcmp(buff, "end", 3) == 0)
2916 bytes = UUDECODE(*p++);
2919 /* Write out 1-3 bytes from that. */
2921 assert(VALID_UUDECODE(p[0]));
2922 assert(VALID_UUDECODE(p[1]));
2923 n = UUDECODE(*p++) << 18;
2924 n |= UUDECODE(*p++) << 12;
2925 fputc(n >> 16, out);
2929 assert(VALID_UUDECODE(p[0]));
2930 n |= UUDECODE(*p++) << 6;
2931 fputc((n >> 8) & 0xFF, out);
2935 assert(VALID_UUDECODE(p[0]));
2936 n |= UUDECODE(*p++);
2937 fputc(n & 0xFF, out);
2947 copy_reference_file(const char *name)
2953 sprintf(buff, "%s/%s", refdir, name);
2954 in = fopen(buff, "rb");
2955 failure("Couldn't open reference file %s", buff);
2959 /* Now, decode the rest and write it. */
2960 /* Not a lot of error checking here; the input better be right. */
2961 out = fopen(name, "wb");
2962 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2963 if (fwrite(buff, 1, rbytes, out) != rbytes) {
2964 logprintf("Error: fwrite\n");
2973 is_LargeInode(const char *file)
2975 #if defined(_WIN32) && !defined(__CYGWIN__)
2976 BY_HANDLE_FILE_INFORMATION bhfi;
2979 r = my_GetFileInformationByName(file, &bhfi);
2982 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2987 if (stat(file, &st) < 0)
2989 ino = (int64_t)st.st_ino;
2990 return (ino > 0xffffffff);
2995 extract_reference_files(const char **names)
2997 while (names && *names)
2998 extract_reference_file(*names++);
3004 assertion_entry_set_acls(const char *file, int line, struct archive_entry *ae,
3005 struct archive_test_acl_t *acls, int n)
3009 assertion_count(file, line);
3012 archive_entry_acl_clear(ae);
3013 for (i = 0; i < n; i++) {
3014 r = archive_entry_acl_add_entry(ae,
3015 acls[i].type, acls[i].permset, acls[i].tag,
3016 acls[i].qual, acls[i].name);
3019 failure_start(file, line, "type=%#010x, ",
3020 "permset=%#010x, tag=%d, qual=%d name=%s",
3021 acls[i].type, acls[i].permset, acls[i].tag,
3022 acls[i].qual, acls[i].name);
3023 failure_finish(NULL);
3031 archive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset,
3032 int tag, int qual, const char *name)
3034 if (type != acl->type)
3036 if (permset != acl->permset)
3038 if (tag != acl->tag)
3040 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
3042 if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
3044 if (tag == ARCHIVE_ENTRY_ACL_EVERYONE)
3046 if (tag == ARCHIVE_ENTRY_ACL_OTHER)
3048 if (qual != acl->qual)
3051 if (acl->name == NULL || acl->name[0] == '\0')
3055 if (acl->name == NULL) {
3056 if (name[0] == '\0')
3060 return (0 == strcmp(name, acl->name));
3065 assertion_entry_compare_acls(const char *file, int line,
3066 struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt,
3067 int want_type, int mode)
3071 int type, permset, tag, qual;
3075 assertion_count(file, line);
3079 marker = malloc(sizeof(marker[0]) * cnt);
3081 for (i = 0; i < cnt; i++) {
3082 if ((acls[i].type & want_type) != 0) {
3089 failure_start(file, line, "No ACL's to compare, type mask: %d",
3094 while (0 == (r = archive_entry_acl_next(ae, want_type,
3095 &type, &permset, &tag, &qual, &name))) {
3096 for (i = 0, matched = 0; i < n && !matched; i++) {
3097 if (archive_test_acl_match(&acls[marker[i]], type,
3098 permset, tag, qual, name)) {
3099 /* We found a match; remove it. */
3100 marker[i] = marker[n - 1];
3105 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3106 && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
3108 failure_start(file, line, "No match for "
3110 failure_finish(NULL);
3113 if ((permset << 6) != (mode & 0700)) {
3114 failure_start(file, line, "USER_OBJ permset "
3115 "(%02o) != user mode (%02o)", permset,
3117 failure_finish(NULL);
3120 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3121 && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
3123 failure_start(file, line, "No match for "
3125 failure_finish(NULL);
3128 if ((permset << 3) != (mode & 0070)) {
3129 failure_start(file, line, "GROUP_OBJ permset "
3130 "(%02o) != group mode (%02o)", permset,
3132 failure_finish(NULL);
3135 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3136 && tag == ARCHIVE_ENTRY_ACL_OTHER) {
3138 failure_start(file, line, "No match for "
3140 failure_finish(NULL);
3143 if ((permset << 0) != (mode & 0007)) {
3144 failure_start(file, line, "OTHER permset "
3145 "(%02o) != other mode (%02o)", permset,
3147 failure_finish(NULL);
3150 } else if (matched != 1) {
3151 failure_start(file, line, "Could not find match for "
3152 "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d,"
3153 "name=``%s'')", type, permset, tag, qual, name);
3154 failure_finish(NULL);
3158 if (r != ARCHIVE_EOF) {
3159 failure_start(file, line, "Should not exit before EOF");
3160 failure_finish(NULL);
3163 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 &&
3164 (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) {
3165 failure_start(file, line, "Mode (%02o) and entry mode (%02o) "
3166 "mismatch", mode, archive_entry_mode(ae));
3167 failure_finish(NULL);
3171 failure_start(file, line, "Could not find match for ACL "
3172 "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')",
3173 acls[marker[0]].type, acls[marker[0]].permset,
3174 acls[marker[0]].tag, acls[marker[0]].qual,
3175 acls[marker[0]].name);
3176 failure_finish(NULL);
3178 /* Number of ACLs not matched should == 0 */
3183 #endif /* !defined(PROGRAM) */
3192 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
3194 * DEFINE_TEST(test_function)
3198 /* Use "list.h" to declare all of the test functions. */
3200 #define DEFINE_TEST(name) void name(void);
3203 /* Use "list.h" to create a list of all tests (functions and names). */
3205 #define DEFINE_TEST(n) { n, #n, 0 },
3206 struct test_list_t tests[] = {
3211 * Summarize repeated failures in the just-completed test.
3214 test_summarize(int failed, int skips_num)
3218 switch (verbosity) {
3219 case VERBOSITY_SUMMARY_ONLY:
3220 printf(failed ? "E" : ".");
3223 case VERBOSITY_PASSFAIL:
3224 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
3228 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
3230 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
3231 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
3232 logprintf("%s:%d: Summary: Failed %d times\n",
3233 failed_filename, i, failed_lines[i].count);
3235 /* Clear the failure history for the next file. */
3236 failed_filename = NULL;
3237 memset(failed_lines, 0, sizeof(failed_lines));
3241 * Actually run a single test, with appropriate setup and cleanup.
3244 test_run(int i, const char *tmpdir)
3247 char logfilename[64];
3248 int failures_before = failures;
3249 int skips_before = skips;
3252 switch (verbosity) {
3253 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
3255 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
3256 printf("%3d: %-64s", i, tests[i].name);
3259 default: /* Title of test, details will follow */
3260 printf("%3d: %s\n", i, tests[i].name);
3263 /* Chdir to the top-level work directory. */
3264 if (!assertChdir(tmpdir)) {
3266 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
3269 /* Create a log file for this test. */
3270 sprintf(logfilename, "%s.log", tests[i].name);
3271 logfile = fopen(logfilename, "w");
3272 fprintf(logfile, "%s\n\n", tests[i].name);
3273 /* Chdir() to a work dir for this specific test. */
3274 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
3275 testworkdir = workdir;
3276 if (!assertMakeDir(testworkdir, 0755)
3277 || !assertChdir(testworkdir)) {
3279 "ERROR: Can't chdir to work dir %s\n", testworkdir);
3282 /* Explicitly reset the locale before each test. */
3283 setlocale(LC_ALL, "C");
3284 /* Record the umask before we run the test. */
3285 umask(oldumask = umask(0));
3287 * Run the actual test.
3291 * Clean up and report afterwards.
3297 setlocale(LC_ALL, "C");
3298 /* Reset directory. */
3299 if (!assertChdir(tmpdir)) {
3300 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
3304 /* Report per-test summaries. */
3305 tests[i].failures = failures - failures_before;
3306 test_summarize(tests[i].failures, skips - skips_before);
3307 /* Close the per-test log file. */
3310 /* If there were no failures, we can remove the work dir and logfile. */
3311 if (tests[i].failures == 0) {
3312 if (!keep_temp_files && assertChdir(tmpdir)) {
3313 #if defined(_WIN32) && !defined(__CYGWIN__)
3314 /* Make sure not to leave empty directories.
3315 * Sometimes a processing of closing files used by tests
3316 * is not done, then rmdir will be failed and it will
3317 * leave a empty test directory. So we should wait a few
3318 * seconds and retry rmdir. */
3320 for (t = 0; t < 10; t++) {
3323 r = systemf("rmdir /S /Q %s", tests[i].name);
3327 systemf("del %s", logfilename);
3329 systemf("rm -rf %s", tests[i].name);
3330 systemf("rm %s", logfilename);
3334 /* Return appropriate status. */
3335 return (tests[i].failures);
3341 * MAIN and support routines.
3347 usage(const char *program)
3349 static const int limit = sizeof(tests) / sizeof(tests[0]);
3352 printf("Usage: %s [options] <test> <test> ...\n", program);
3353 printf("Default is to run all tests.\n");
3354 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
3355 printf("Options:\n");
3356 printf(" -d Dump core after any failure, for debugging.\n");
3357 printf(" -k Keep all temp files.\n");
3358 printf(" Default: temp files for successful tests deleted.\n");
3360 printf(" -p <path> Path to executable to be tested.\n");
3361 printf(" Default: path taken from " ENVBASE " environment variable.\n");
3363 printf(" -q Quiet.\n");
3364 printf(" -r <dir> Path to dir containing reference files.\n");
3365 printf(" Default: Current directory.\n");
3366 printf(" -u Keep running specifies tests until one fails.\n");
3367 printf(" -v Verbose.\n");
3368 printf("Available tests:\n");
3369 for (i = 0; i < limit; i++)
3370 printf(" %d: %s\n", i, tests[i].name);
3375 get_refdir(const char *d)
3377 size_t tried_size, buff_size;
3378 char *buff, *tried, *pwd = NULL, *p = NULL;
3381 buff_size = PATH_MAX;
3385 buff = calloc(buff_size, 1);
3387 fprintf(stderr, "Unable to allocate memory\n");
3391 /* Allocate a buffer to hold the various directories we checked. */
3392 tried_size = buff_size * 2;
3393 tried = calloc(tried_size, 1);
3394 if (tried == NULL) {
3395 fprintf(stderr, "Unable to allocate memory\n");
3399 /* If a dir was specified, try that */
3402 snprintf(buff, buff_size, "%s", d);
3403 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3404 if (p != NULL) goto success;
3405 strncat(tried, buff, tried_size - strlen(tried) - 1);
3406 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3410 /* Get the current dir. */
3412 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3414 pwd = getcwd(NULL, 0);
3416 while (pwd[strlen(pwd) - 1] == '\n')
3417 pwd[strlen(pwd) - 1] = '\0';
3419 /* Look for a known file. */
3420 snprintf(buff, buff_size, "%s", pwd);
3421 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3422 if (p != NULL) goto success;
3423 strncat(tried, buff, tried_size - strlen(tried) - 1);
3424 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3426 snprintf(buff, buff_size, "%s/test", pwd);
3427 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3428 if (p != NULL) goto success;
3429 strncat(tried, buff, tried_size - strlen(tried) - 1);
3430 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3432 #if defined(LIBRARY)
3433 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
3435 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
3437 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3438 if (p != NULL) goto success;
3439 strncat(tried, buff, tried_size - strlen(tried) - 1);
3440 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3442 #if defined(PROGRAM_ALIAS)
3443 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
3444 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3445 if (p != NULL) goto success;
3446 strncat(tried, buff, tried_size - strlen(tried) - 1);
3447 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3450 if (memcmp(pwd, "/usr/obj", 8) == 0) {
3451 snprintf(buff, buff_size, "%s", pwd + 8);
3452 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3453 if (p != NULL) goto success;
3454 strncat(tried, buff, tried_size - strlen(tried) - 1);
3455 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3457 snprintf(buff, buff_size, "%s/test", pwd + 8);
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);
3465 printf("Unable to locate known reference file %s\n", KNOWNREF);
3466 printf(" Checked following directories:\n%s\n", tried);
3467 printf("Use -r option to specify full path to reference directory\n");
3468 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
3478 /* Copy result into a fresh buffer to reduce memory usage. */
3485 main(int argc, char **argv)
3487 static const int limit = sizeof(tests) / sizeof(tests[0]);
3488 int test_set[sizeof(tests) / sizeof(tests[0])];
3489 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
3491 char *refdir_alloc = NULL;
3492 const char *progname;
3494 const char *tmp, *option_arg, *p;
3495 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
3496 char tmpdir_timestamp[256];
3498 (void)argc; /* UNUSED */
3500 /* Get the current dir. */
3502 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3504 pwd = getcwd(NULL, 0);
3506 while (pwd[strlen(pwd) - 1] == '\n')
3507 pwd[strlen(pwd) - 1] = '\0';
3509 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
3510 /* To stop to run the default invalid parameter handler. */
3511 _set_invalid_parameter_handler(invalid_parameter_handler);
3512 /* Disable annoying assertion message box. */
3513 _CrtSetReportMode(_CRT_ASSERT, 0);
3517 * Name of this program, used to build root of our temp directory
3520 progname = p = argv[0];
3521 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
3523 fprintf(stderr, "ERROR: Out of memory.");
3526 strcpy(testprogdir, progname);
3527 while (*p != '\0') {
3528 /* Support \ or / dir separators for Windows compat. */
3529 if (*p == '/' || *p == '\\')
3537 testprogdir[i] = '\0';
3538 #if defined(_WIN32) && !defined(__CYGWIN__)
3539 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
3540 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
3541 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
3542 testprogdir[1] == ':' &&
3543 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
3545 if (testprogdir[0] != '/')
3548 /* Fixup path for relative directories. */
3549 if ((testprogdir = (char *)realloc(testprogdir,
3550 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
3552 fprintf(stderr, "ERROR: Out of memory.");
3555 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
3556 strlen(testprogdir) + 1);
3557 memcpy(testprogdir, pwd, strlen(pwd));
3558 testprogdir[strlen(pwd)] = '/';
3562 /* Get the target program from environment, if available. */
3563 testprogfile = getenv(ENVBASE);
3566 if (getenv("TMPDIR") != NULL)
3567 tmp = getenv("TMPDIR");
3568 else if (getenv("TMP") != NULL)
3569 tmp = getenv("TMP");
3570 else if (getenv("TEMP") != NULL)
3571 tmp = getenv("TEMP");
3572 else if (getenv("TEMPDIR") != NULL)
3573 tmp = getenv("TEMPDIR");
3577 /* Allow -d to be controlled through the environment. */
3578 if (getenv(ENVBASE "_DEBUG") != NULL)
3579 dump_on_failure = 1;
3581 /* Allow -v to be controlled through the environment. */
3582 if (getenv("_VERBOSITY_LEVEL") != NULL)
3584 vlevel = getenv("_VERBOSITY_LEVEL");
3585 verbosity = atoi(vlevel);
3586 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
3588 /* Unsupported verbosity levels are silently ignored */
3590 verbosity = VERBOSITY_PASSFAIL;
3594 /* Get the directory holding test files from environment. */
3595 refdir = getenv(ENVBASE "_TEST_FILES");
3598 * Parse options, without using getopt(), which isn't available
3601 ++argv; /* Skip program name */
3602 while (*argv != NULL) {
3607 while (*p != '\0') {
3610 /* If 'opt' takes an argument, parse that. */
3611 if (option == 'p' || option == 'r') {
3614 else if (*argv == NULL) {
3616 "Option -%c requires argument.\n",
3620 option_arg = *argv++;
3621 p = ""; /* End of this option word. */
3624 /* Now, handle the option. */
3627 dump_on_failure = 1;
3630 keep_temp_files = 1;
3634 testprogfile = option_arg;
3636 fprintf(stderr, "-p option not permitted\n");
3645 refdir = option_arg;
3655 fprintf(stderr, "Unrecognized option '%c'\n",
3663 * Sanity-check that our options make sense.
3666 if (testprogfile == NULL)
3668 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
3669 strlen(PROGRAM) + 1)) == NULL)
3671 fprintf(stderr, "ERROR: Out of memory.");
3674 strcpy(tmp2, testprogdir);
3676 strcat(tmp2, PROGRAM);
3677 testprogfile = tmp2;
3682 #if defined(_WIN32) && !defined(__CYGWIN__)
3683 /* Command.com sometimes rejects '/' separators. */
3684 testprg = strdup(testprogfile);
3685 for (i = 0; testprg[i] != '\0'; i++) {
3686 if (testprg[i] == '/')
3689 testprogfile = testprg;
3691 /* Quote the name that gets put into shell command lines. */
3692 testprg = malloc(strlen(testprogfile) + 3);
3693 strcpy(testprg, "\"");
3694 strcat(testprg, testprogfile);
3695 strcat(testprg, "\"");
3700 #if !defined(_WIN32) && defined(SIGPIPE)
3701 { /* Ignore SIGPIPE signals */
3702 struct sigaction sa;
3703 sa.sa_handler = SIG_IGN;
3704 sigemptyset(&sa.sa_mask);
3706 sigaction(SIGPIPE, &sa, NULL);
3711 * Create a temp directory for the following tests.
3712 * Include the time the tests started as part of the name,
3713 * to make it easier to track the results of multiple tests.
3716 for (i = 0; ; i++) {
3717 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
3718 "%Y-%m-%dT%H.%M.%S",
3720 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
3721 tmpdir_timestamp, i);
3722 if (assertMakeDir(tmpdir,0755))
3726 "ERROR: Unable to create temp directory %s\n",
3733 * If the user didn't specify a directory for locating
3734 * reference files, try to find the reference files in
3735 * the "usual places."
3737 refdir = refdir_alloc = get_refdir(refdir);
3740 * Banner with basic information.
3743 printf("If tests fail or crash, details will be in:\n");
3744 printf(" %s\n", tmpdir);
3746 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3747 printf("Reference files will be read from: %s\n", refdir);
3749 printf("Running tests on: %s\n", testprog);
3751 printf("Exercising: ");
3753 printf("%s\n", EXTRA_VERSION);
3760 * Run some or all of the individual tests.
3768 test_num = get_test_set(test_set, limit, *argv, tests);
3770 printf("*** INVALID Test %s\n", *argv);
3776 for (i = 0; i < test_num; i++) {
3778 if (test_run(test_set[i], tmpdir)) {
3786 } while (*argv != NULL);
3787 } while (until_failure);
3790 /* Must be freed after all tests run */
3796 * Report summary statistics.
3798 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3800 printf("Totals:\n");
3801 printf(" Tests run: %8d\n", tests_run);
3802 printf(" Tests failed: %8d\n", tests_failed);
3803 printf(" Assertions checked:%8d\n", assertions);
3804 printf(" Assertions failed: %8d\n", failures);
3805 printf(" Skips reported: %8d\n", skips);
3809 printf("Failing tests:\n");
3810 for (i = 0; i < limit; ++i) {
3811 if (tests[i].failures)
3812 printf(" %d: %s (%d failures)\n", i,
3813 tests[i].name, tests[i].failures);
3816 printf("Details for failing tests: %s\n", tmpdir);
3819 if (verbosity == VERBOSITY_SUMMARY_ONLY)
3821 printf("%d tests passed, no failures\n", tests_run);
3826 /* If the final tmpdir is empty, we can remove it. */
3827 /* This should be the usual case when all tests succeed. */
3831 return (tests_failed ? 1 : 0);