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 *);
171 typedef struct _REPARSE_DATA_BUFFER {
173 USHORT ReparseDataLength;
177 USHORT SubstituteNameOffset;
178 USHORT SubstituteNameLength;
179 USHORT PrintNameOffset;
180 USHORT PrintNameLength;
183 } SymbolicLinkReparseBuffer;
185 USHORT SubstituteNameOffset;
186 USHORT SubstituteNameLength;
187 USHORT PrintNameOffset;
188 USHORT PrintNameLength;
190 } MountPointReparseBuffer;
193 } GenericReparseBuffer;
195 } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
198 GetFunctionKernel32(const char *name)
200 static HINSTANCE lib;
204 lib = LoadLibrary("kernel32.dll");
207 fprintf(stderr, "Can't load kernel32.dll?!\n");
210 return (void *)GetProcAddress(lib, name);
214 my_CreateSymbolicLinkA(const char *linkname, const char *target,
217 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
220 int ret, tmpflags, llen, tlen;
225 f = GetFunctionKernel32("CreateSymbolicLinkA");
230 tlen = strlen(target);
231 llen = strlen(linkname);
233 if (tlen == 0 || llen == 0)
236 tgt = malloc((tlen + 1) * sizeof(char));
239 src = malloc((llen + 1) * sizeof(char));
246 * Translate slashes to backslashes
249 while(*linkname != '\0') {
250 if (*linkname == '/')
260 while(*target != '\0') {
271 * Each test has to specify if a file or a directory symlink
275 #if defined(SYMBOLIC_LINK_FLAG_DIRECTORY)
276 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
282 #if defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
283 tmpflags = flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
285 tmpflags = flags | 0x2;
288 * Windows won't overwrite existing links
290 attrs = GetFileAttributesA(linkname);
291 if (attrs != INVALID_FILE_ATTRIBUTES) {
292 if (attrs & FILE_ATTRIBUTE_DIRECTORY)
293 RemoveDirectoryA(linkname);
295 DeleteFileA(linkname);
298 ret = (*f)(src, tgt, tmpflags);
300 * Prior to Windows 10 the SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
304 ret = (*f)(src, tgt, flags);
312 my_CreateHardLinkA(const char *linkname, const char *target)
314 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
318 f = GetFunctionKernel32("CreateHardLinkA");
320 return f == NULL ? 0 : (*f)(linkname, target, NULL);
324 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
329 memset(bhfi, 0, sizeof(*bhfi));
330 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
331 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
332 if (h == INVALID_HANDLE_VALUE)
334 r = GetFileInformationByHandle(h, bhfi);
340 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
342 invalid_parameter_handler(const wchar_t * expression,
343 const wchar_t * function, const wchar_t * file,
344 unsigned int line, uintptr_t pReserved)
347 // Silence unused-parameter compiler warnings.
362 /* Enable core dump on failure. */
363 static int dump_on_failure = 0;
364 /* Default is to remove temp dirs and log data for successful tests. */
365 static int keep_temp_files = 0;
366 /* Default is to run the specified tests once and report errors. */
367 static int until_failure = 0;
368 /* Default is to just report pass/fail for each test. */
369 static int verbosity = 0;
370 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
371 #define VERBOSITY_PASSFAIL 0 /* Default */
372 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
373 #define VERBOSITY_FULL 2 /* -vv */
374 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
375 * mostly for debugging the test harness itself. */
376 /* Cumulative count of assertion failures. */
377 static int failures = 0;
378 /* Cumulative count of reported skips. */
379 static int skips = 0;
380 /* Cumulative count of assertions checked. */
381 static int assertions = 0;
383 /* Directory where uuencoded reference files can be found. */
384 static const char *refdir;
387 * Report log information selectively to console and/or disk log.
389 static int log_console = 0;
390 static FILE *logfile;
392 vlogprintf(const char *fmt, va_list ap)
399 vfprintf(stdout, fmt, ap);
402 vfprintf(logfile, fmt, lfap);
405 vfprintf(logfile, fmt, ap);
410 logprintf(const char *fmt, ...)
418 /* Set up a message to display only if next assertion fails. */
419 static char msgbuff[4096];
420 static const char *msg, *nextmsg;
422 failure(const char *fmt, ...)
429 vsprintf(msgbuff, fmt, ap);
436 * Copy arguments into file-local variables.
437 * This was added to permit vararg assert() functions without needing
438 * variadic wrapper macros. Turns out that the vararg capability is almost
439 * never used, so almost all of the vararg assertions can be simplified
440 * by removing the vararg capability and reworking the wrapper macro to
441 * pass __FILE__, __LINE__ directly into the function instead of using
442 * this hook. I suspect this machinery is used so rarely that we
443 * would be better off just removing it entirely. That would simplify
444 * the code here noticeably.
446 static const char *skipping_filename;
447 static int skipping_line;
448 void skipping_setup(const char *filename, int line)
450 skipping_filename = filename;
451 skipping_line = line;
454 /* Called at the beginning of each assert() function. */
456 assertion_count(const char *file, int line)
458 (void)file; /* UNUSED */
459 (void)line; /* UNUSED */
461 /* Proper handling of "failure()" message. */
464 /* Uncomment to print file:line after every assertion.
465 * Verbose, but occasionally useful in tracking down crashes. */
466 /* printf("Checked %s:%d\n", file, line); */
470 * For each test source file, we remember how many times each
471 * assertion was reported. Cleared before each new test,
472 * used by test_summarize().
477 } failed_lines[10000];
478 const char *failed_filename;
480 /* Count this failure, setup up log destination and handle initial report. */
482 failure_start(const char *filename, int line, const char *fmt, ...)
486 /* Record another failure for this line. */
488 failed_filename = filename;
489 failed_lines[line].count++;
491 /* Determine whether to log header to console. */
493 case VERBOSITY_LIGHT_REPORT:
494 log_console = (failed_lines[line].count < 2);
497 log_console = (verbosity >= VERBOSITY_FULL);
500 /* Log file:line header for this failure */
503 logprintf("%s(%d): ", filename, line);
505 logprintf("%s:%d: ", filename, line);
511 if (msg != NULL && msg[0] != '\0') {
512 logprintf(" Description: %s\n", msg);
516 /* Determine whether to log details to console. */
517 if (verbosity == VERBOSITY_LIGHT_REPORT)
521 /* Complete reporting of failed tests. */
523 * The 'extra' hook here is used by libarchive to include libarchive
524 * error messages with assertion failures. It could also be used
525 * to add strerror() output, for example. Just define the EXTRA_DUMP()
526 * macro appropriately.
529 failure_finish(void *extra)
531 (void)extra; /* UNUSED (maybe) */
534 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
535 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
539 if (dump_on_failure) {
541 " *** forcing core dump so failure can be debugged ***\n");
546 /* Inform user that we're skipping some checks. */
548 test_skipping(const char *fmt, ...)
554 vsprintf(buff, fmt, ap);
556 /* Use failure() message if set. */
559 /* failure_start() isn't quite right, but is awfully convenient. */
560 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
561 --failures; /* Undo failures++ in failure_start() */
562 /* Don't failure_finish() here. */
563 /* Mark as skip, so doesn't count as failed test. */
564 failed_lines[skipping_line].skip = 1;
574 /* Generic assert() just displays the failed condition. */
576 assertion_assert(const char *file, int line, int value,
577 const char *condition, void *extra)
579 assertion_count(file, line);
581 failure_start(file, line, "Assertion failed: %s", condition);
582 failure_finish(extra);
587 /* chdir() and report any errors */
589 assertion_chdir(const char *file, int line, const char *pathname)
591 assertion_count(file, line);
592 if (chdir(pathname) == 0)
594 failure_start(file, line, "chdir(\"%s\")", pathname);
595 failure_finish(NULL);
600 /* Verify two integers are equal. */
602 assertion_equal_int(const char *file, int line,
603 long long v1, const char *e1, long long v2, const char *e2, void *extra)
605 assertion_count(file, line);
608 failure_start(file, line, "%s != %s", e1, e2);
609 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
610 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
611 failure_finish(extra);
616 * Utility to convert a single UTF-8 sequence.
619 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
621 static const char utf8_count[256] = {
622 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
623 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
624 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
625 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
626 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
627 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
628 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
629 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
630 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
631 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
632 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
633 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
634 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
635 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
636 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
637 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
649 * Decode 1-4 bytes depending on the value of the first byte.
651 ch = (unsigned char)*s;
653 return (0); /* Standard: return 0 for end-of-string. */
654 cnt = utf8_count[ch];
656 /* Invalid sequence or there are not plenty bytes. */
660 /* Make a Unicode code point from a single UTF-8 sequence. */
662 case 1: /* 1 byte sequence. */
665 case 2: /* 2 bytes sequence. */
666 if ((s[1] & 0xc0) != 0x80) return (-1);
667 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
669 case 3: /* 3 bytes sequence. */
670 if ((s[1] & 0xc0) != 0x80) return (-1);
671 if ((s[2] & 0xc0) != 0x80) return (-1);
672 wc = ((ch & 0x0f) << 12)
673 | ((s[1] & 0x3f) << 6)
676 return (-1);/* Overlong sequence. */
678 case 4: /* 4 bytes sequence. */
681 if ((s[1] & 0xc0) != 0x80) return (-1);
682 if ((s[2] & 0xc0) != 0x80) return (-1);
683 if ((s[3] & 0xc0) != 0x80) return (-1);
684 wc = ((ch & 0x07) << 18)
685 | ((s[1] & 0x3f) << 12)
686 | ((s[2] & 0x3f) << 6)
689 return (-1);/* Overlong sequence. */
695 /* The code point larger than 0x10FFFF is not legal
699 /* Correctly gets a Unicode, returns used bytes. */
704 static void strdump(const char *e, const char *p, int ewidth, int utf8)
708 logprintf(" %*s = ", ewidth, e);
715 unsigned int c = 0xff & *p++;
717 case '\a': logprintf("\\a"); break;
718 case '\b': logprintf("\\b"); break;
719 case '\n': logprintf("\\n"); break;
720 case '\r': logprintf("\\r"); break;
722 if (c >= 32 && c < 127)
725 logprintf("\\x%02X", c);
729 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
732 * If the current string is UTF-8, dump its code points.
743 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
746 logprintf("%04X", uc);
752 logprintf(" (count %d", cnt);
754 logprintf(",unknown %d bytes", len);
762 /* Verify two strings are equal, dump them if not. */
764 assertion_equal_string(const char *file, int line,
765 const char *v1, const char *e1,
766 const char *v2, const char *e2,
767 void *extra, int utf8)
771 assertion_count(file, line);
772 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
774 failure_start(file, line, "%s != %s", e1, e2);
775 l1 = (int)strlen(e1);
776 l2 = (int)strlen(e2);
779 strdump(e1, v1, l1, utf8);
780 strdump(e2, v2, l1, utf8);
781 failure_finish(extra);
786 wcsdump(const char *e, const wchar_t *w)
788 logprintf(" %s = ", e);
794 while (*w != L'\0') {
795 unsigned int c = *w++;
796 if (c >= 32 && c < 127)
799 logprintf("\\x%02X", c);
800 else if (c < 0x10000)
801 logprintf("\\u%04X", c);
803 logprintf("\\U%08X", c);
810 wcscmp(const wchar_t *s1, const wchar_t *s2)
813 while (*s1 == *s2++) {
824 /* Verify that two wide strings are equal, dump them if not. */
826 assertion_equal_wstring(const char *file, int line,
827 const wchar_t *v1, const char *e1,
828 const wchar_t *v2, const char *e2,
831 assertion_count(file, line);
834 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
836 failure_start(file, line, "%s != %s", e1, e2);
839 failure_finish(extra);
844 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
845 * any bytes in p that differ from ref will be highlighted with '_'
846 * before and after the hex value.
849 hexdump(const char *p, const char *ref, size_t l, size_t offset)
855 logprintf("(null)\n");
858 for(i=0; i < l; i+=16) {
859 logprintf("%04x", (unsigned)(i + offset));
861 for (j = 0; j < 16 && i + j < l; j++) {
862 if (ref != NULL && p[i + j] != ref[i + j])
864 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
865 if (ref != NULL && p[i + j] == ref[i + j])
868 for (; j < 16; j++) {
869 logprintf("%c ", sep);
872 logprintf("%c", sep);
873 for (j=0; j < 16 && i + j < l; j++) {
875 if (c >= ' ' && c <= 126)
884 /* Verify that two blocks of memory are the same, display the first
885 * block of differences if they're not. */
887 assertion_equal_mem(const char *file, int line,
888 const void *_v1, const char *e1,
889 const void *_v2, const char *e2,
890 size_t l, const char *ld, void *extra)
892 const char *v1 = (const char *)_v1;
893 const char *v2 = (const char *)_v2;
896 assertion_count(file, line);
897 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
899 if (v1 == NULL || v2 == NULL)
902 failure_start(file, line, "%s != %s", e1, e2);
903 logprintf(" size %s = %d\n", ld, (int)l);
904 /* Dump 48 bytes (3 lines) so that the first difference is
905 * in the second line. */
907 while (l > 64 && memcmp(v1, v2, 32) == 0) {
908 /* Two lines agree, so step forward one line. */
914 logprintf(" Dump of %s\n", e1);
915 hexdump(v1, v2, l < 128 ? l : 128, offset);
916 logprintf(" Dump of %s\n", e2);
917 hexdump(v2, v1, l < 128 ? l : 128, offset);
919 failure_finish(extra);
923 /* Verify that a block of memory is filled with the specified byte. */
925 assertion_memory_filled_with(const char *file, int line,
926 const void *_v1, const char *vd,
927 size_t l, const char *ld,
928 char b, const char *bd, void *extra)
930 const char *v1 = (const char *)_v1;
933 (void)ld; /* UNUSED */
935 assertion_count(file, line);
937 for (i = 0; i < l; ++i) {
945 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
946 logprintf(" Only %d bytes were correct\n", (int)c);
947 failure_finish(extra);
951 /* Verify that the named file exists and is empty. */
953 assertion_empty_file(const char *filename, int line, const char *f1)
960 assertion_count(filename, line);
962 if (stat(f1, &st) != 0) {
963 failure_start(filename, line, "Stat failed: %s", f1);
964 failure_finish(NULL);
970 failure_start(filename, line, "File should be empty: %s", f1);
971 logprintf(" File size: %d\n", (int)st.st_size);
972 logprintf(" Contents:\n");
975 logprintf(" Unable to open %s\n", f1);
977 s = ((off_t)sizeof(buff) < st.st_size) ?
978 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
979 s = fread(buff, 1, s, f);
980 hexdump(buff, NULL, s, 0);
983 failure_finish(NULL);
987 /* Verify that the named file exists and is not empty. */
989 assertion_non_empty_file(const char *filename, int line, const char *f1)
993 assertion_count(filename, line);
995 if (stat(f1, &st) != 0) {
996 failure_start(filename, line, "Stat failed: %s", f1);
997 failure_finish(NULL);
1000 if (st.st_size == 0) {
1001 failure_start(filename, line, "File empty: %s", f1);
1002 failure_finish(NULL);
1008 /* Verify that two files have the same contents. */
1009 /* TODO: hexdump the first bytes that actually differ. */
1011 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
1018 assertion_count(filename, line);
1020 f1 = fopen(fn1, "rb");
1021 f2 = fopen(fn2, "rb");
1022 if (f1 == NULL || f2 == NULL) {
1028 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
1029 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
1032 if (n1 == 0 && n2 == 0) {
1037 if (memcmp(buff1, buff2, n1) != 0)
1042 failure_start(filename, line, "Files not identical");
1043 logprintf(" file1=\"%s\"\n", fn1);
1044 logprintf(" file2=\"%s\"\n", fn2);
1045 failure_finish(NULL);
1049 /* Verify that the named file does exist. */
1051 assertion_file_exists(const char *filename, int line, const char *f)
1053 assertion_count(filename, line);
1055 #if defined(_WIN32) && !defined(__CYGWIN__)
1059 if (!access(f, F_OK))
1062 failure_start(filename, line, "File should exist: %s", f);
1063 failure_finish(NULL);
1067 /* Verify that the named file doesn't exist. */
1069 assertion_file_not_exists(const char *filename, int line, const char *f)
1071 assertion_count(filename, line);
1073 #if defined(_WIN32) && !defined(__CYGWIN__)
1077 if (access(f, F_OK))
1080 failure_start(filename, line, "File should not exist: %s", f);
1081 failure_finish(NULL);
1085 /* Compare the contents of a file to a block of memory. */
1087 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
1093 assertion_count(filename, line);
1095 f = fopen(fn, "rb");
1097 failure_start(filename, line,
1098 "File should exist: %s", fn);
1099 failure_finish(NULL);
1102 contents = malloc(s * 2);
1103 n = (int)fread(contents, 1, s * 2, f);
1105 if (n == s && memcmp(buff, contents, s) == 0) {
1109 failure_start(filename, line, "File contents don't match");
1110 logprintf(" file=\"%s\"\n", fn);
1112 hexdump(contents, buff, n > 512 ? 512 : n, 0);
1114 logprintf(" File empty, contents should be:\n");
1115 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
1117 failure_finish(NULL);
1122 /* Check the contents of a text file, being tolerant of line endings. */
1124 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
1127 const char *btxt, *ftxt;
1131 assertion_count(filename, line);
1134 failure_start(filename, line,
1135 "File doesn't exist: %s", fn);
1136 failure_finish(NULL);
1139 s = (int)strlen(buff);
1140 contents = malloc(s * 2 + 128);
1141 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1145 /* Compare texts. */
1147 ftxt = (const char *)contents;
1148 while (*btxt != '\0' && *ftxt != '\0') {
1149 if (*btxt == *ftxt) {
1154 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1155 /* Pass over different new line characters. */
1162 if (*btxt == '\0' && *ftxt == '\0') {
1166 failure_start(filename, line, "Contents don't match");
1167 logprintf(" file=\"%s\"\n", fn);
1169 hexdump(contents, buff, n, 0);
1170 logprintf(" expected\n", fn);
1171 hexdump(buff, contents, s, 0);
1173 logprintf(" File empty, contents should be:\n");
1174 hexdump(buff, NULL, s, 0);
1176 failure_finish(NULL);
1181 /* Verify that a text file contains the specified lines, regardless of order */
1182 /* This could be more efficient if we sorted both sets of lines, etc, but
1183 * since this is used only for testing and only ever deals with a dozen or so
1184 * lines at a time, this relatively crude approach is just fine. */
1186 assertion_file_contains_lines_any_order(const char *file, int line,
1187 const char *pathname, const char *lines[])
1191 size_t expected_count, actual_count, i, j;
1192 char **expected = NULL;
1193 char *p, **actual = NULL;
1195 int expected_failure = 0, actual_failure = 0;
1197 assertion_count(file, line);
1199 buff = slurpfile(&buff_size, "%s", pathname);
1201 failure_start(pathname, line, "Can't read file: %s", pathname);
1202 failure_finish(NULL);
1206 /* Make a copy of the provided lines and count up the expected
1208 for (i = 0; lines[i] != NULL; ++i) {
1211 if (expected_count) {
1212 expected = malloc(sizeof(char *) * expected_count);
1213 if (expected == NULL) {
1214 failure_start(pathname, line, "Can't allocate memory");
1215 failure_finish(NULL);
1220 for (i = 0; lines[i] != NULL; ++i) {
1221 expected[i] = strdup(lines[i]);
1225 /* Break the file into lines */
1227 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1228 if (*p == '\x0d' || *p == '\x0a')
1230 if (c == '\0' && *p != '\0')
1235 actual = calloc(sizeof(char *), actual_count);
1236 if (actual == NULL) {
1237 failure_start(pathname, line, "Can't allocate memory");
1238 failure_finish(NULL);
1243 for (j = 0, p = buff; p < buff + buff_size;
1244 p += 1 + strlen(p)) {
1252 /* Erase matching lines from both lists */
1253 for (i = 0; i < expected_count; ++i) {
1254 if (expected[i] == NULL)
1256 for (j = 0; j < actual_count; ++j) {
1257 if (actual[j] == NULL)
1259 if (strcmp(expected[i], actual[j]) == 0) {
1268 /* If there's anything left, it's a failure */
1269 for (i = 0; i < expected_count; ++i) {
1270 if (expected[i] != NULL)
1273 for (j = 0; j < actual_count; ++j) {
1274 if (actual[j] != NULL)
1277 if (expected_failure == 0 && actual_failure == 0) {
1283 failure_start(file, line, "File doesn't match: %s", pathname);
1284 for (i = 0; i < expected_count; ++i) {
1285 if (expected[i] != NULL) {
1286 logprintf(" Expected but not present: %s\n", expected[i]);
1290 for (j = 0; j < actual_count; ++j) {
1291 if (actual[j] != NULL)
1292 logprintf(" Present but not expected: %s\n", actual[j]);
1294 failure_finish(NULL);
1301 /* Verify that a text file does not contains the specified strings */
1303 assertion_file_contains_no_invalid_strings(const char *file, int line,
1304 const char *pathname, const char *strings[])
1309 buff = slurpfile(NULL, "%s", pathname);
1311 failure_start(file, line, "Can't read file: %s", pathname);
1312 failure_finish(NULL);
1316 for (i = 0; strings[i] != NULL; ++i) {
1317 if (strstr(buff, strings[i]) != NULL) {
1318 failure_start(file, line, "Invalid string in %s: %s", pathname,
1320 failure_finish(NULL);
1330 /* Test that two paths point to the same file. */
1331 /* As a side-effect, asserts that both files exist. */
1333 is_hardlink(const char *file, int line,
1334 const char *path1, const char *path2)
1336 #if defined(_WIN32) && !defined(__CYGWIN__)
1337 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1340 assertion_count(file, line);
1341 r = my_GetFileInformationByName(path1, &bhfi1);
1343 failure_start(file, line, "File %s can't be inspected?", path1);
1344 failure_finish(NULL);
1347 r = my_GetFileInformationByName(path2, &bhfi2);
1349 failure_start(file, line, "File %s can't be inspected?", path2);
1350 failure_finish(NULL);
1353 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1354 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1355 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1357 struct stat st1, st2;
1360 assertion_count(file, line);
1361 r = lstat(path1, &st1);
1363 failure_start(file, line, "File should exist: %s", path1);
1364 failure_finish(NULL);
1367 r = lstat(path2, &st2);
1369 failure_start(file, line, "File should exist: %s", path2);
1370 failure_finish(NULL);
1373 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1378 assertion_is_hardlink(const char *file, int line,
1379 const char *path1, const char *path2)
1381 if (is_hardlink(file, line, path1, path2))
1383 failure_start(file, line,
1384 "Files %s and %s are not hardlinked", path1, path2);
1385 failure_finish(NULL);
1390 assertion_is_not_hardlink(const char *file, int line,
1391 const char *path1, const char *path2)
1393 if (!is_hardlink(file, line, path1, path2))
1395 failure_start(file, line,
1396 "Files %s and %s should not be hardlinked", path1, path2);
1397 failure_finish(NULL);
1401 /* Verify a/b/mtime of 'pathname'. */
1402 /* If 'recent', verify that it's within last 10 seconds. */
1404 assertion_file_time(const char *file, int line,
1405 const char *pathname, long t, long nsec, char type, int recent)
1407 long long filet, filet_nsec;
1410 #if defined(_WIN32) && !defined(__CYGWIN__)
1411 #define EPOC_TIME (116444736000000000ULL)
1412 FILETIME fxtime, fbirthtime, fatime, fmtime;
1413 ULARGE_INTEGER wintm;
1415 fxtime.dwLowDateTime = 0;
1416 fxtime.dwHighDateTime = 0;
1418 assertion_count(file, line);
1419 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1420 * a directory file. If not, CreateFile() will fail when
1421 * the pathname is a directory. */
1422 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1423 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1424 if (h == INVALID_HANDLE_VALUE) {
1425 failure_start(file, line, "Can't access %s\n", pathname);
1426 failure_finish(NULL);
1429 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1431 case 'a': fxtime = fatime; break;
1432 case 'b': fxtime = fbirthtime; break;
1433 case 'm': fxtime = fmtime; break;
1437 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1438 failure_finish(NULL);
1441 wintm.LowPart = fxtime.dwLowDateTime;
1442 wintm.HighPart = fxtime.dwHighDateTime;
1443 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1444 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1445 nsec = (nsec / 100) * 100; /* Round the request */
1449 assertion_count(file, line);
1450 r = lstat(pathname, &st);
1452 failure_start(file, line, "Can't stat %s\n", pathname);
1453 failure_finish(NULL);
1457 case 'a': filet = st.st_atime; break;
1458 case 'm': filet = st.st_mtime; break;
1459 case 'b': filet = 0; break;
1460 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1463 #if defined(__FreeBSD__)
1465 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1466 case 'b': filet = st.st_birthtime;
1467 /* FreeBSD filesystems that don't support birthtime
1468 * (e.g., UFS1) always return -1 here. */
1472 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1473 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1474 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1477 /* FreeBSD generally only stores to microsecond res, so round. */
1478 filet_nsec = (filet_nsec / 1000) * 1000;
1479 nsec = (nsec / 1000) * 1000;
1481 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1482 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1483 #if defined(__HAIKU__)
1484 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1489 /* Check that requested time is up-to-date. */
1490 time_t now = time(NULL);
1491 if (filet < now - 10 || filet > now + 1) {
1492 failure_start(file, line,
1493 "File %s has %ctime %lld, %lld seconds ago\n",
1494 pathname, type, filet, now - filet);
1495 failure_finish(NULL);
1498 } else if (filet != t || filet_nsec != nsec) {
1499 failure_start(file, line,
1500 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1501 pathname, type, filet, filet_nsec, t, nsec);
1502 failure_finish(NULL);
1508 /* Verify atime of 'pathname'. */
1510 assertion_file_atime(const char *file, int line,
1511 const char *pathname, long t, long nsec)
1513 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1516 /* Verify atime of 'pathname' is up-to-date. */
1518 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1520 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1523 /* Verify birthtime of 'pathname'. */
1525 assertion_file_birthtime(const char *file, int line,
1526 const char *pathname, long t, long nsec)
1528 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1531 /* Verify birthtime of 'pathname' is up-to-date. */
1533 assertion_file_birthtime_recent(const char *file, int line,
1534 const char *pathname)
1536 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1539 /* Verify mode of 'pathname'. */
1541 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1546 assertion_count(file, line);
1547 #if defined(_WIN32) && !defined(__CYGWIN__)
1548 failure_start(file, line, "assertFileMode not yet implemented for Windows");
1549 (void)mode; /* UNUSED */
1550 (void)r; /* UNUSED */
1551 (void)pathname; /* UNUSED */
1552 (void)expected_mode; /* UNUSED */
1556 r = lstat(pathname, &st);
1557 mode = (int)(st.st_mode & 0777);
1559 if (r == 0 && mode == expected_mode)
1561 failure_start(file, line, "File %s has mode %o, expected %o",
1562 pathname, mode, expected_mode);
1564 failure_finish(NULL);
1568 /* Verify mtime of 'pathname'. */
1570 assertion_file_mtime(const char *file, int line,
1571 const char *pathname, long t, long nsec)
1573 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1576 /* Verify mtime of 'pathname' is up-to-date. */
1578 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1580 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1583 /* Verify number of links to 'pathname'. */
1585 assertion_file_nlinks(const char *file, int line,
1586 const char *pathname, int nlinks)
1588 #if defined(_WIN32) && !defined(__CYGWIN__)
1589 BY_HANDLE_FILE_INFORMATION bhfi;
1592 assertion_count(file, line);
1593 r = my_GetFileInformationByName(pathname, &bhfi);
1594 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1596 failure_start(file, line, "File %s has %d links, expected %d",
1597 pathname, bhfi.nNumberOfLinks, nlinks);
1598 failure_finish(NULL);
1604 assertion_count(file, line);
1605 r = lstat(pathname, &st);
1606 if (r == 0 && (int)st.st_nlink == nlinks)
1608 failure_start(file, line, "File %s has %d links, expected %d",
1609 pathname, st.st_nlink, nlinks);
1610 failure_finish(NULL);
1615 /* Verify size of 'pathname'. */
1617 assertion_file_size(const char *file, int line, const char *pathname, long size)
1622 assertion_count(file, line);
1623 #if defined(_WIN32) && !defined(__CYGWIN__)
1625 BY_HANDLE_FILE_INFORMATION bhfi;
1626 r = !my_GetFileInformationByName(pathname, &bhfi);
1627 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1632 r = lstat(pathname, &st);
1633 filesize = st.st_size;
1636 if (r == 0 && filesize == size)
1638 failure_start(file, line, "File %s has size %ld, expected %ld",
1639 pathname, (long)filesize, (long)size);
1640 failure_finish(NULL);
1644 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1646 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1651 #if defined(_WIN32) && !defined(__CYGWIN__)
1652 (void)mode; /* UNUSED */
1654 assertion_count(file, line);
1655 r = lstat(pathname, &st);
1657 failure_start(file, line, "Dir should exist: %s", pathname);
1658 failure_finish(NULL);
1661 if (!S_ISDIR(st.st_mode)) {
1662 failure_start(file, line, "%s is not a dir", pathname);
1663 failure_finish(NULL);
1666 #if !defined(_WIN32) || defined(__CYGWIN__)
1667 /* Windows doesn't handle permissions the same way as POSIX,
1668 * so just ignore the mode tests. */
1669 /* TODO: Can we do better here? */
1670 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1671 failure_start(file, line, "Dir %s has wrong mode", pathname);
1672 logprintf(" Expected: 0%3o\n", mode);
1673 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1674 failure_finish(NULL);
1681 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1682 * verify that too. */
1684 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1689 #if defined(_WIN32) && !defined(__CYGWIN__)
1690 (void)mode; /* UNUSED */
1692 assertion_count(file, line);
1693 r = lstat(pathname, &st);
1694 if (r != 0 || !S_ISREG(st.st_mode)) {
1695 failure_start(file, line, "File should exist: %s", pathname);
1696 failure_finish(NULL);
1699 #if !defined(_WIN32) || defined(__CYGWIN__)
1700 /* Windows doesn't handle permissions the same way as POSIX,
1701 * so just ignore the mode tests. */
1702 /* TODO: Can we do better here? */
1703 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1704 failure_start(file, line, "File %s has wrong mode", pathname);
1705 logprintf(" Expected: 0%3o\n", mode);
1706 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1707 failure_finish(NULL);
1715 * Check whether 'pathname' is a symbolic link. If 'contents' is
1716 * non-NULL, verify that the symlink has those contents.
1718 * On platforms with directory symlinks, set isdir to 0 to test for a file
1719 * symlink and to 1 to test for a directory symlink. On other platforms
1720 * the variable is ignored.
1723 is_symlink(const char *file, int line,
1724 const char *pathname, const char *contents, int isdir)
1726 #if defined(_WIN32) && !defined(__CYGWIN__)
1729 REPARSE_DATA_BUFFER *buf;
1730 BY_HANDLE_FILE_INFORMATION st;
1732 wchar_t *linknamew, *contentsw;
1737 const DWORD flag = FILE_FLAG_BACKUP_SEMANTICS |
1738 FILE_FLAG_OPEN_REPARSE_POINT;
1740 /* Replace slashes with backslashes in pathname */
1741 pn = malloc((strlen(pathname) + 1) * sizeof(char));
1754 h = CreateFileA(pn, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1757 if (h == INVALID_HANDLE_VALUE) {
1758 failure_start(file, line, "Can't access %s\n", pathname);
1759 failure_finish(NULL);
1762 ret = GetFileInformationByHandle(h, &st);
1764 failure_start(file, line,
1765 "Can't stat: %s", pathname);
1766 failure_finish(NULL);
1767 } else if ((st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
1768 failure_start(file, line,
1769 "Not a symlink: %s", pathname);
1770 failure_finish(NULL);
1773 if (isdir && ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
1774 failure_start(file, line,
1775 "Not a directory symlink: %s", pathname);
1776 failure_finish(NULL);
1780 ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) {
1781 failure_start(file, line,
1782 "Not a file symlink: %s", pathname);
1783 failure_finish(NULL);
1791 indata = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
1792 ret = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, indata,
1793 1024, &inbytes, NULL);
1797 failure_start(file, line,
1798 "Could not retrieve symlink target: %s", pathname);
1799 failure_finish(NULL);
1803 buf = (REPARSE_DATA_BUFFER *) indata;
1804 if (buf->ReparseTag != IO_REPARSE_TAG_SYMLINK) {
1806 /* File is not a symbolic link */
1807 failure_start(file, line,
1808 "Not a symlink: %s", pathname);
1809 failure_finish(NULL);
1813 if (contents == NULL) {
1818 len = buf->SymbolicLinkReparseBuffer.SubstituteNameLength;
1820 linknamew = malloc(len + sizeof(wchar_t));
1821 if (linknamew == NULL) {
1826 memcpy(linknamew, &((BYTE *)buf->SymbolicLinkReparseBuffer.PathBuffer)
1827 [buf->SymbolicLinkReparseBuffer.SubstituteNameOffset], len);
1830 linknamew[len / sizeof(wchar_t)] = L'\0';
1832 contentsw = malloc(len + sizeof(wchar_t));
1833 if (contentsw == NULL) {
1838 len2 = mbsrtowcs(contentsw, &contents, (len + sizeof(wchar_t)
1839 / sizeof(wchar_t)), NULL);
1841 if (len2 > 0 && wcscmp(linknamew, contentsw) != 0)
1853 (void)isdir; /* UNUSED */
1854 assertion_count(file, line);
1855 r = lstat(pathname, &st);
1857 failure_start(file, line,
1858 "Symlink should exist: %s", pathname);
1859 failure_finish(NULL);
1862 if (!S_ISLNK(st.st_mode))
1864 if (contents == NULL)
1866 linklen = readlink(pathname, buff, sizeof(buff));
1868 failure_start(file, line, "Can't read symlink %s", pathname);
1869 failure_finish(NULL);
1872 buff[linklen] = '\0';
1873 if (strcmp(buff, contents) != 0)
1879 /* Assert that path is a symlink that (optionally) contains contents. */
1881 assertion_is_symlink(const char *file, int line,
1882 const char *path, const char *contents, int isdir)
1884 if (is_symlink(file, line, path, contents, isdir))
1887 failure_start(file, line, "File %s is not a symlink to %s",
1890 failure_start(file, line, "File %s is not a symlink", path);
1891 failure_finish(NULL);
1896 /* Create a directory and report any errors. */
1898 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1900 assertion_count(file, line);
1901 #if defined(_WIN32) && !defined(__CYGWIN__)
1902 (void)mode; /* UNUSED */
1903 if (0 == _mkdir(dirname))
1906 if (0 == mkdir(dirname, mode)) {
1907 if (0 == chmod(dirname, mode)) {
1908 assertion_file_mode(file, line, dirname, mode);
1913 failure_start(file, line, "Could not create directory %s", dirname);
1914 failure_finish(NULL);
1918 /* Create a file with the specified contents and report any failures. */
1920 assertion_make_file(const char *file, int line,
1921 const char *path, int mode, int csize, const void *contents)
1923 #if defined(_WIN32) && !defined(__CYGWIN__)
1924 /* TODO: Rework this to set file mode as well. */
1926 (void)mode; /* UNUSED */
1927 assertion_count(file, line);
1928 f = fopen(path, "wb");
1930 failure_start(file, line, "Could not create file %s", path);
1931 failure_finish(NULL);
1934 if (contents != NULL) {
1938 wsize = strlen(contents);
1940 wsize = (size_t)csize;
1941 if (wsize != fwrite(contents, 1, wsize, f)) {
1943 failure_start(file, line,
1944 "Could not write file %s", path);
1945 failure_finish(NULL);
1953 assertion_count(file, line);
1954 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1956 failure_start(file, line, "Could not create %s", path);
1957 failure_finish(NULL);
1960 if (0 != chmod(path, mode)) {
1961 failure_start(file, line, "Could not chmod %s", path);
1962 failure_finish(NULL);
1966 if (contents != NULL) {
1970 wsize = (ssize_t)strlen(contents);
1972 wsize = (ssize_t)csize;
1973 if (wsize != write(fd, contents, wsize)) {
1975 failure_start(file, line,
1976 "Could not write to %s", path);
1977 failure_finish(NULL);
1983 assertion_file_mode(file, line, path, mode);
1988 /* Create a hardlink and report any failures. */
1990 assertion_make_hardlink(const char *file, int line,
1991 const char *newpath, const char *linkto)
1995 assertion_count(file, line);
1996 #if defined(_WIN32) && !defined(__CYGWIN__)
1997 succeeded = my_CreateHardLinkA(newpath, linkto);
1999 succeeded = !link(linkto, newpath);
2005 failure_start(file, line, "Could not create hardlink");
2006 logprintf(" New link: %s\n", newpath);
2007 logprintf(" Old name: %s\n", linkto);
2008 failure_finish(NULL);
2013 * Create a symlink and report any failures.
2015 * Windows symlinks need to know if the target is a directory.
2018 assertion_make_symlink(const char *file, int line,
2019 const char *newpath, const char *linkto, int targetIsDir)
2021 #if defined(_WIN32) && !defined(__CYGWIN__)
2022 assertion_count(file, line);
2023 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
2026 (void)targetIsDir; /* UNUSED */
2027 assertion_count(file, line);
2028 if (0 == symlink(linkto, newpath))
2031 (void)targetIsDir; /* UNUSED */
2033 failure_start(file, line, "Could not create symlink");
2034 logprintf(" New link: %s\n", newpath);
2035 logprintf(" Old name: %s\n", linkto);
2036 failure_finish(NULL);
2040 /* Set umask, report failures. */
2042 assertion_umask(const char *file, int line, int mask)
2044 assertion_count(file, line);
2045 (void)file; /* UNUSED */
2046 (void)line; /* UNUSED */
2051 /* Set times, report failures. */
2053 assertion_utimes(const char *file, int line,
2054 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
2058 #if defined(_WIN32) && !defined(__CYGWIN__)
2059 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
2060 + (((nsec)/1000)*10))
2062 ULARGE_INTEGER wintm;
2063 FILETIME fatime, fmtime;
2064 FILETIME *pat, *pmt;
2066 assertion_count(file, line);
2067 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
2068 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
2069 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2070 if (h == INVALID_HANDLE_VALUE) {
2071 failure_start(file, line, "Can't access %s\n", pathname);
2072 failure_finish(NULL);
2076 if (at > 0 || at_nsec > 0) {
2077 wintm.QuadPart = WINTIME(at, at_nsec);
2078 fatime.dwLowDateTime = wintm.LowPart;
2079 fatime.dwHighDateTime = wintm.HighPart;
2083 if (mt > 0 || mt_nsec > 0) {
2084 wintm.QuadPart = WINTIME(mt, mt_nsec);
2085 fmtime.dwLowDateTime = wintm.LowPart;
2086 fmtime.dwHighDateTime = wintm.HighPart;
2090 if (pat != NULL || pmt != NULL)
2091 r = SetFileTime(h, NULL, pat, pmt);
2096 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
2097 failure_finish(NULL);
2101 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
2103 struct timeval times[2];
2105 #if !defined(__FreeBSD__)
2106 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
2108 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
2111 r = lstat(pathname, &st);
2113 failure_start(file, line, "Can't stat %s\n", pathname);
2114 failure_finish(NULL);
2118 if (mt == 0 && mt_nsec == 0) {
2120 #if defined(__FreeBSD__)
2121 mt_nsec = st.st_mtimespec.tv_nsec;
2122 /* FreeBSD generally only stores to microsecond res, so round. */
2123 mt_nsec = (mt_nsec / 1000) * 1000;
2126 if (at == 0 && at_nsec == 0) {
2128 #if defined(__FreeBSD__)
2129 at_nsec = st.st_atimespec.tv_nsec;
2130 /* FreeBSD generally only stores to microsecond res, so round. */
2131 at_nsec = (at_nsec / 1000) * 1000;
2135 times[1].tv_sec = mt;
2136 times[1].tv_usec = mt_nsec / 1000;
2138 times[0].tv_sec = at;
2139 times[0].tv_usec = at_nsec / 1000;
2142 r = lutimes(pathname, times);
2144 r = utimes(pathname, times);
2147 failure_start(file, line, "Can't utimes %s\n", pathname);
2148 failure_finish(NULL);
2152 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
2155 /* Compare file flags */
2157 assertion_compare_fflags(const char *file, int line, const char *patha,
2158 const char *pathb, int nomatch)
2160 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2163 assertion_count(file, line);
2165 if (stat(patha, &sa) < 0)
2167 if (stat(pathb, &sb) < 0)
2169 if (!nomatch && sa.st_flags != sb.st_flags) {
2170 failure_start(file, line, "File flags should be identical: "
2171 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
2173 failure_finish(NULL);
2176 if (nomatch && sa.st_flags == sb.st_flags) {
2177 failure_start(file, line, "File flags should be different: "
2178 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
2180 failure_finish(NULL);
2183 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
2184 defined(FS_NODUMP_FL)) || \
2185 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2186 && defined(EXT2_NODUMP_FL))
2187 int fd, r, flagsa, flagsb;
2189 assertion_count(file, line);
2190 fd = open(patha, O_RDONLY | O_NONBLOCK);
2192 failure_start(file, line, "Can't open %s\n", patha);
2193 failure_finish(NULL);
2197 #ifdef FS_IOC_GETFLAGS
2205 failure_start(file, line, "Can't get flags %s\n", patha);
2206 failure_finish(NULL);
2209 fd = open(pathb, O_RDONLY | O_NONBLOCK);
2211 failure_start(file, line, "Can't open %s\n", pathb);
2212 failure_finish(NULL);
2216 #ifdef FS_IOC_GETFLAGS
2224 failure_start(file, line, "Can't get flags %s\n", pathb);
2225 failure_finish(NULL);
2228 if (!nomatch && flagsa != flagsb) {
2229 failure_start(file, line, "File flags should be identical: "
2230 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
2231 failure_finish(NULL);
2234 if (nomatch && flagsa == flagsb) {
2235 failure_start(file, line, "File flags should be different: "
2236 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
2237 failure_finish(NULL);
2241 (void)patha; /* UNUSED */
2242 (void)pathb; /* UNUSED */
2243 (void)nomatch; /* UNUSED */
2244 assertion_count(file, line);
2249 /* Set nodump, report failures. */
2251 assertion_set_nodump(const char *file, int line, const char *pathname)
2253 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2256 assertion_count(file, line);
2257 r = chflags(pathname, UF_NODUMP);
2259 failure_start(file, line, "Can't set nodump %s\n", pathname);
2260 failure_finish(NULL);
2263 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
2264 defined(FS_NODUMP_FL)) || \
2265 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2266 && defined(EXT2_NODUMP_FL))
2269 assertion_count(file, line);
2270 fd = open(pathname, O_RDONLY | O_NONBLOCK);
2272 failure_start(file, line, "Can't open %s\n", pathname);
2273 failure_finish(NULL);
2277 #ifdef FS_IOC_GETFLAGS
2284 failure_start(file, line, "Can't get flags %s\n", pathname);
2285 failure_finish(NULL);
2289 flags |= FS_NODUMP_FL;
2291 flags |= EXT2_NODUMP_FL;
2295 #ifdef FS_IOC_SETFLAGS
2302 failure_start(file, line, "Can't set nodump %s\n", pathname);
2303 failure_finish(NULL);
2308 (void)pathname; /* UNUSED */
2309 assertion_count(file, line);
2315 static void assert_version_id(char **qq, size_t *ss)
2320 /* Version number is a series of digits and periods. */
2321 while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
2326 if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') {
2331 /* Skip a single trailing a,b,c, or d. */
2332 if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
2335 /* Version number terminated by space. */
2336 failure("No space after version: ``%s''", q);
2338 failure("No space after version: ``%s''", q);
2349 * Check program version
2351 void assertVersion(const char *prog, const char *base)
2356 size_t prog_len = strlen(base);
2358 r = systemf("%s --version >version.stdout 2>version.stderr", prog);
2360 r = systemf("%s -W version >version.stdout 2>version.stderr",
2363 failure("Unable to run either %s --version or %s -W version",
2365 if (!assert(r == 0))
2368 /* --version should generate nothing to stdout. */
2369 assertEmptyFile("version.stderr");
2371 /* Verify format of version message. */
2372 q = p = slurpfile(&s, "version.stdout");
2374 /* Version message should start with name of program, then space. */
2375 assert(s > prog_len + 1);
2377 failure("Version must start with '%s': ``%s''", base, p);
2378 if (!assertEqualMem(q, base, prog_len)) {
2383 q += prog_len; s -= prog_len;
2388 assert_version_id(&q, &s);
2391 failure("No `-' between program name and versions: ``%s''", p);
2392 assertEqualMem(q, "- ", 2);
2395 failure("Not long enough for libarchive version: ``%s''", p);
2398 failure("Libarchive version must start with `libarchive': ``%s''", p);
2399 assertEqualMem(q, "libarchive ", 11);
2403 assert_version_id(&q, &s);
2405 /* Skip arbitrary third-party version numbers. */
2406 while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' ||
2407 isalnum((unsigned char)*q))) {
2412 /* All terminated by end-of-line. */
2415 /* Skip an optional CR character (e.g., Windows) */
2416 failure("Version output must end with \\n or \\r\\n");
2418 if (*q == '\r') { ++q; --s; }
2419 assertEqualMem(q, "\n", 1);
2423 #endif /* PROGRAM */
2427 * UTILITIES for use by tests.
2432 * Check whether platform supports symlinks. This is intended
2433 * for tests to use in deciding whether to bother testing symlink
2434 * support; if the platform doesn't support symlinks, there's no point
2435 * in checking whether the program being tested can create them.
2437 * Note that the first time this test is called, we actually go out to
2438 * disk to create and verify a symlink. This is necessary because
2439 * symlink support is actually a property of a particular filesystem
2440 * and can thus vary between directories on a single system. After
2441 * the first call, this returns the cached result from memory, so it's
2442 * safe to call it as often as you wish.
2447 /* Remember the test result */
2448 static int value = 0, tested = 0;
2453 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
2454 /* Note: Cygwin has its own symlink() emulation that does not
2455 * use the Win32 CreateSymbolicLink() function. */
2456 #if defined(_WIN32) && !defined(__CYGWIN__)
2457 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
2458 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0",
2461 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
2462 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0",
2468 /* Platform-dependent options for hiding the output of a subcommand. */
2469 #if defined(_WIN32) && !defined(__CYGWIN__)
2470 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
2472 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
2475 * Can this platform run the bzip2 program?
2480 static int tested = 0, value = 0;
2483 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
2490 * Can this platform run the grzip program?
2495 static int tested = 0, value = 0;
2498 if (systemf("grzip -V %s", redirectArgs) == 0)
2505 * Can this platform run the gzip program?
2510 static int tested = 0, value = 0;
2513 if (systemf("gzip -V %s", redirectArgs) == 0)
2520 * Can this platform run the lrzip program?
2523 canRunCommand(const char *cmd)
2525 static int tested = 0, value = 0;
2528 if (systemf("%s %s", cmd, redirectArgs) == 0)
2537 static int tested = 0, value = 0;
2540 if (systemf("lrzip -V %s", redirectArgs) == 0)
2547 * Can this platform run the lz4 program?
2552 static int tested = 0, value = 0;
2555 if (systemf("lz4 -V %s", redirectArgs) == 0)
2562 * Can this platform run the zstd program?
2567 static int tested = 0, value = 0;
2570 if (systemf("zstd -V %s", redirectArgs) == 0)
2577 * Can this platform run the lzip program?
2582 static int tested = 0, value = 0;
2585 if (systemf("lzip -V %s", redirectArgs) == 0)
2592 * Can this platform run the lzma program?
2597 static int tested = 0, value = 0;
2600 if (systemf("lzma -V %s", redirectArgs) == 0)
2607 * Can this platform run the lzop program?
2612 static int tested = 0, value = 0;
2615 if (systemf("lzop -V %s", redirectArgs) == 0)
2622 * Can this platform run the xz program?
2627 static int tested = 0, value = 0;
2630 if (systemf("xz -V %s", redirectArgs) == 0)
2637 * Can this filesystem handle nodump flags.
2642 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2643 const char *path = "cannodumptest";
2646 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2647 if (chflags(path, UF_NODUMP) < 0)
2649 if (stat(path, &sb) < 0)
2651 if (sb.st_flags & UF_NODUMP)
2653 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \
2654 && defined(FS_NODUMP_FL)) || \
2655 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2656 && defined(EXT2_NODUMP_FL))
2657 const char *path = "cannodumptest";
2660 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2661 fd = open(path, O_RDONLY | O_NONBLOCK);
2665 #ifdef FS_IOC_GETFLAGS
2674 flags |= FS_NODUMP_FL;
2676 flags |= EXT2_NODUMP_FL;
2679 #ifdef FS_IOC_SETFLAGS
2688 fd = open(path, O_RDONLY | O_NONBLOCK);
2692 #ifdef FS_IOC_GETFLAGS
2702 if (flags & FS_NODUMP_FL)
2704 if (flags & EXT2_NODUMP_FL)
2711 /* Get extended attribute value from a path */
2713 getXattr(const char *path, const char *name, size_t *sizep)
2716 #if ARCHIVE_XATTR_SUPPORT
2718 #if ARCHIVE_XATTR_LINUX
2719 size = lgetxattr(path, name, NULL, 0);
2720 #elif ARCHIVE_XATTR_DARWIN
2721 size = getxattr(path, name, NULL, 0, 0, XATTR_NOFOLLOW);
2722 #elif ARCHIVE_XATTR_AIX
2723 size = lgetea(path, name, NULL, 0);
2724 #elif ARCHIVE_XATTR_FREEBSD
2725 size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
2730 value = malloc(size);
2731 #if ARCHIVE_XATTR_LINUX
2732 size = lgetxattr(path, name, value, size);
2733 #elif ARCHIVE_XATTR_DARWIN
2734 size = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
2735 #elif ARCHIVE_XATTR_AIX
2736 size = lgetea(path, name, value, size);
2737 #elif ARCHIVE_XATTR_FREEBSD
2738 size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
2749 *sizep = (size_t)size;
2750 #else /* !ARCHIVE_XATTR_SUPPORT */
2751 (void)path; /* UNUSED */
2752 (void)name; /* UNUSED */
2754 #endif /* !ARCHIVE_XATTR_SUPPORT */
2759 * Set extended attribute on a path
2760 * Returns 0 on error, 1 on success
2763 setXattr(const char *path, const char *name, const void *value, size_t size)
2765 #if ARCHIVE_XATTR_SUPPORT
2766 #if ARCHIVE_XATTR_LINUX
2767 if (lsetxattr(path, name, value, size, 0) == 0)
2768 #elif ARCHIVE_XATTR_DARWIN
2769 if (setxattr(path, name, value, size, 0, XATTR_NOFOLLOW) == 0)
2770 #elif ARCHIVE_XATTR_AIX
2771 if (lsetea(path, name, value, size, 0) == 0)
2772 #elif ARCHIVE_XATTR_FREEBSD
2773 if (extattr_set_link(path, EXTATTR_NAMESPACE_USER, name + 5, value,
2779 #else /* !ARCHIVE_XATTR_SUPPORT */
2780 (void)path; /* UNUSED */
2781 (void)name; /* UNUSED */
2782 (void)value; /* UNUSED */
2783 (void)size; /* UNUSED */
2784 #endif /* !ARCHIVE_XATTR_SUPPORT */
2788 #if ARCHIVE_ACL_SUNOS
2789 /* Fetch ACLs on Solaris using acl() or facl() */
2791 sunacl_get(int cmd, int *aclcnt, int fd, const char *path)
2797 if (cmd == GETACL) {
2799 size = sizeof(aclent_t);
2801 #if ARCHIVE_ACL_SUNOS_NFS4
2802 else if (cmd == ACE_GETACL) {
2803 cntcmd = ACE_GETACLCNT;
2804 size = sizeof(ace_t);
2815 while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) {
2817 cnt = acl(path, cntcmd, 0, NULL);
2819 cnt = facl(fd, cntcmd, 0, NULL);
2823 aclp = malloc(cnt * size);
2825 aclp = realloc(NULL, cnt * size);
2828 cnt = acl(path, cmd, cnt, aclp);
2830 cnt = facl(fd, cmd, cnt, aclp);
2842 #endif /* ARCHIVE_ACL_SUNOS */
2845 * Set test ACLs on a path
2847 * 0: error setting ACLs
2848 * ARCHIVE_TEST_ACL_TYPE_POSIX1E: POSIX.1E ACLs have been set
2849 * ARCHIVE_TEST_ACL_TYPE_NFS4: NFSv4 or extended ACLs have been set
2852 setTestAcl(const char *path)
2854 #if ARCHIVE_ACL_SUPPORT
2856 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_DARWIN
2859 #if ARCHIVE_ACL_LIBRICHACL
2860 struct richacl *richacl;
2862 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD
2863 const char *acltext_posix1e = "user:1:rw-,"
2869 #elif ARCHIVE_ACL_SUNOS /* Solaris POSIX.1e */
2870 aclent_t aclp_posix1e[] = {
2871 { USER_OBJ, -1, 4 | 2 | 1 },
2873 { GROUP_OBJ, -1, 4 | 2 | 1 },
2874 { GROUP, 15, 4 | 1 },
2875 { CLASS_OBJ, -1, 4 | 2 | 1 },
2876 { OTHER_OBJ, -1, 4 | 2 | 1 }
2879 #if ARCHIVE_ACL_FREEBSD /* FreeBSD NFS4 */
2880 const char *acltext_nfs4 = "user:1:rwpaRcs::allow:1,"
2881 "group:15:rxaRcs::allow:15,"
2882 "owner@:rwpxaARWcCos::allow,"
2883 "group@:rwpxaRcs::allow,"
2884 "everyone@:rxaRcs::allow";
2885 #elif ARCHIVE_ACL_LIBRICHACL
2886 const char *acltext_nfs4 = "owner:rwpxaARWcCoS::mask,"
2887 "group:rwpxaRcS::mask,"
2888 "other:rxaRcS::mask,"
2889 "user:1:rwpaRcS::allow,"
2890 "group:15:rxaRcS::allow,"
2891 "owner@:rwpxaARWcCoS::allow,"
2892 "group@:rwpxaRcS::allow,"
2893 "everyone@:rxaRcS::allow";
2894 #elif ARCHIVE_ACL_SUNOS_NFS4 /* Solaris NFS4 */
2895 ace_t aclp_nfs4[] = {
2896 { 1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2897 ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | ACE_READ_ACL |
2898 ACE_SYNCHRONIZE, 0, ACE_ACCESS_ALLOWED_ACE_TYPE },
2899 { 15, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2900 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2901 ACE_IDENTIFIER_GROUP, ACE_ACCESS_ALLOWED_ACE_TYPE },
2902 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2903 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_WRITE_ATTRIBUTES |
2904 ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS |
2905 ACE_READ_ACL | ACE_WRITE_ACL | ACE_WRITE_OWNER | ACE_SYNCHRONIZE,
2906 ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE },
2907 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2908 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
2909 ACE_READ_ACL | ACE_SYNCHRONIZE, ACE_GROUP | ACE_IDENTIFIER_GROUP,
2910 ACE_ACCESS_ALLOWED_ACE_TYPE },
2911 { -1, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2912 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2913 ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE }
2915 #elif ARCHIVE_ACL_DARWIN /* Mac OS X */
2917 acl_permset_t permset;
2918 const uid_t uid = 1;
2921 const acl_perm_t acl_perms[] = {
2926 ACL_READ_ATTRIBUTES,
2927 ACL_READ_EXTATTRIBUTES,
2929 #if HAVE_DECL_ACL_SYNCHRONIZE
2933 #endif /* ARCHIVE_ACL_DARWIN */
2935 #if ARCHIVE_ACL_FREEBSD
2936 acl = acl_from_text(acltext_nfs4);
2937 failure("acl_from_text() error: %s", strerror(errno));
2938 if (assert(acl != NULL) == 0)
2940 #elif ARCHIVE_ACL_LIBRICHACL
2941 richacl = richacl_from_text(acltext_nfs4, NULL, NULL);
2942 failure("richacl_from_text() error: %s", strerror(errno));
2943 if (assert(richacl != NULL) == 0)
2945 #elif ARCHIVE_ACL_DARWIN
2947 failure("acl_init() error: %s", strerror(errno));
2948 if (assert(acl != NULL) == 0)
2950 r = acl_create_entry(&acl, &aclent);
2951 failure("acl_create_entry() error: %s", strerror(errno));
2952 if (assertEqualInt(r, 0) == 0)
2954 r = acl_set_tag_type(aclent, ACL_EXTENDED_ALLOW);
2955 failure("acl_set_tag_type() error: %s", strerror(errno));
2956 if (assertEqualInt(r, 0) == 0)
2958 r = acl_get_permset(aclent, &permset);
2959 failure("acl_get_permset() error: %s", strerror(errno));
2960 if (assertEqualInt(r, 0) == 0)
2962 for (i = 0; i < (int)(sizeof(acl_perms) / sizeof(acl_perms[0])); i++) {
2963 r = acl_add_perm(permset, acl_perms[i]);
2964 failure("acl_add_perm() error: %s", strerror(errno));
2965 if (assertEqualInt(r, 0) == 0)
2968 r = acl_set_permset(aclent, permset);
2969 failure("acl_set_permset() error: %s", strerror(errno));
2970 if (assertEqualInt(r, 0) == 0)
2972 r = mbr_uid_to_uuid(uid, uuid);
2973 failure("mbr_uid_to_uuid() error: %s", strerror(errno));
2974 if (assertEqualInt(r, 0) == 0)
2976 r = acl_set_qualifier(aclent, uuid);
2977 failure("acl_set_qualifier() error: %s", strerror(errno));
2978 if (assertEqualInt(r, 0) == 0)
2980 #endif /* ARCHIVE_ACL_DARWIN */
2982 #if ARCHIVE_ACL_NFS4
2983 #if ARCHIVE_ACL_FREEBSD
2984 r = acl_set_file(path, ACL_TYPE_NFS4, acl);
2986 #elif ARCHIVE_ACL_LIBRICHACL
2987 r = richacl_set_file(path, richacl);
2988 richacl_free(richacl);
2989 #elif ARCHIVE_ACL_SUNOS_NFS4
2990 r = acl(path, ACE_SETACL,
2991 (int)(sizeof(aclp_nfs4)/sizeof(aclp_nfs4[0])), aclp_nfs4);
2992 #elif ARCHIVE_ACL_DARWIN
2993 r = acl_set_file(path, ACL_TYPE_EXTENDED, acl);
2997 return (ARCHIVE_TEST_ACL_TYPE_NFS4);
2998 #endif /* ARCHIVE_ACL_NFS4 */
3000 #if ARCHIVE_ACL_POSIX1E
3001 #if ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_LIBACL
3002 acl = acl_from_text(acltext_posix1e);
3003 failure("acl_from_text() error: %s", strerror(errno));
3004 if (assert(acl != NULL) == 0)
3007 r = acl_set_file(path, ACL_TYPE_ACCESS, acl);
3009 #elif ARCHIVE_ACL_SUNOS
3010 r = acl(path, SETACL,
3011 (int)(sizeof(aclp_posix1e)/sizeof(aclp_posix1e[0])), aclp_posix1e);
3014 return (ARCHIVE_TEST_ACL_TYPE_POSIX1E);
3017 #endif /* ARCHIVE_ACL_POSIX1E */
3018 #if ARCHIVE_ACL_DARWIN
3022 #endif /* ARCHIVE_ACL_SUPPORT */
3023 (void)path; /* UNUSED */
3028 * Sleep as needed; useful for verifying disk timestamp changes by
3029 * ensuring that the wall-clock time has actually changed before we
3030 * go back to re-read something from disk.
3033 sleepUntilAfter(time_t t)
3035 while (t >= time(NULL))
3036 #if defined(_WIN32) && !defined(__CYGWIN__)
3044 * Call standard system() call, but build up the command line using
3045 * sprintf() conventions.
3048 systemf(const char *fmt, ...)
3055 vsprintf(buff, fmt, ap);
3056 if (verbosity > VERBOSITY_FULL)
3057 logprintf("Cmd: %s\n", buff);
3064 * Slurp a file into memory for ease of comparison and testing.
3065 * Returns size of file in 'sizep' if non-NULL, null-terminates
3066 * data in memory for ease of use.
3069 slurpfile(size_t * sizep, const char *fmt, ...)
3071 char filename[8192];
3080 vsprintf(filename, fmt, ap);
3083 f = fopen(filename, "rb");
3085 /* Note: No error; non-existent file is okay here. */
3088 r = fstat(fileno(f), &st);
3090 logprintf("Can't stat file %s\n", filename);
3094 p = malloc((size_t)st.st_size + 1);
3096 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
3097 (long int)st.st_size, filename);
3101 bytes_read = fread(p, 1, (size_t)st.st_size, f);
3102 if (bytes_read < st.st_size) {
3103 logprintf("Can't read file %s\n", filename);
3108 p[st.st_size] = '\0';
3110 *sizep = (size_t)st.st_size;
3116 * Slurp a file into memory for ease of comparison and testing.
3117 * Returns size of file in 'sizep' if non-NULL, null-terminates
3118 * data in memory for ease of use.
3121 dumpfile(const char *filename, void *data, size_t len)
3123 ssize_t bytes_written;
3126 f = fopen(filename, "wb");
3128 logprintf("Can't open file %s for writing\n", filename);
3131 bytes_written = fwrite(data, 1, len, f);
3132 if (bytes_written < (ssize_t)len)
3133 logprintf("Can't write file %s\n", filename);
3137 /* Read a uuencoded file from the reference directory, decode, and
3138 * write the result into the current directory. */
3139 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
3140 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
3142 extract_reference_file(const char *name)
3147 sprintf(buff, "%s/%s.uu", refdir, name);
3148 in = fopen(buff, "r");
3149 failure("Couldn't open reference file %s", buff);
3153 /* Read up to and including the 'begin' line. */
3155 if (fgets(buff, sizeof(buff), in) == NULL) {
3156 /* TODO: This is a failure. */
3159 if (memcmp(buff, "begin ", 6) == 0)
3162 /* Now, decode the rest and write it. */
3163 out = fopen(name, "wb");
3164 while (fgets(buff, sizeof(buff), in) != NULL) {
3168 if (memcmp(buff, "end", 3) == 0)
3171 bytes = UUDECODE(*p++);
3174 /* Write out 1-3 bytes from that. */
3176 assert(VALID_UUDECODE(p[0]));
3177 assert(VALID_UUDECODE(p[1]));
3178 n = UUDECODE(*p++) << 18;
3179 n |= UUDECODE(*p++) << 12;
3180 fputc(n >> 16, out);
3184 assert(VALID_UUDECODE(p[0]));
3185 n |= UUDECODE(*p++) << 6;
3186 fputc((n >> 8) & 0xFF, out);
3190 assert(VALID_UUDECODE(p[0]));
3191 n |= UUDECODE(*p++);
3192 fputc(n & 0xFF, out);
3202 copy_reference_file(const char *name)
3208 sprintf(buff, "%s/%s", refdir, name);
3209 in = fopen(buff, "rb");
3210 failure("Couldn't open reference file %s", buff);
3214 /* Now, decode the rest and write it. */
3215 /* Not a lot of error checking here; the input better be right. */
3216 out = fopen(name, "wb");
3217 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
3218 if (fwrite(buff, 1, rbytes, out) != rbytes) {
3219 logprintf("Error: fwrite\n");
3228 is_LargeInode(const char *file)
3230 #if defined(_WIN32) && !defined(__CYGWIN__)
3231 BY_HANDLE_FILE_INFORMATION bhfi;
3234 r = my_GetFileInformationByName(file, &bhfi);
3237 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
3242 if (stat(file, &st) < 0)
3244 ino = (int64_t)st.st_ino;
3245 return (ino > 0xffffffff);
3250 extract_reference_files(const char **names)
3252 while (names && *names)
3253 extract_reference_file(*names++);
3259 assertion_entry_set_acls(const char *file, int line, struct archive_entry *ae,
3260 struct archive_test_acl_t *acls, int n)
3264 assertion_count(file, line);
3267 archive_entry_acl_clear(ae);
3268 for (i = 0; i < n; i++) {
3269 r = archive_entry_acl_add_entry(ae,
3270 acls[i].type, acls[i].permset, acls[i].tag,
3271 acls[i].qual, acls[i].name);
3274 failure_start(file, line, "type=%#010x, ",
3275 "permset=%#010x, tag=%d, qual=%d name=%s",
3276 acls[i].type, acls[i].permset, acls[i].tag,
3277 acls[i].qual, acls[i].name);
3278 failure_finish(NULL);
3286 archive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset,
3287 int tag, int qual, const char *name)
3289 if (type != acl->type)
3291 if (permset != acl->permset)
3293 if (tag != acl->tag)
3295 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
3297 if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
3299 if (tag == ARCHIVE_ENTRY_ACL_EVERYONE)
3301 if (tag == ARCHIVE_ENTRY_ACL_OTHER)
3303 if (qual != acl->qual)
3306 if (acl->name == NULL || acl->name[0] == '\0')
3310 if (acl->name == NULL) {
3311 if (name[0] == '\0')
3315 return (0 == strcmp(name, acl->name));
3320 assertion_entry_compare_acls(const char *file, int line,
3321 struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt,
3322 int want_type, int mode)
3326 int type, permset, tag, qual;
3330 assertion_count(file, line);
3334 marker = malloc(sizeof(marker[0]) * cnt);
3336 for (i = 0; i < cnt; i++) {
3337 if ((acls[i].type & want_type) != 0) {
3344 failure_start(file, line, "No ACL's to compare, type mask: %d",
3349 while (0 == (r = archive_entry_acl_next(ae, want_type,
3350 &type, &permset, &tag, &qual, &name))) {
3351 for (i = 0, matched = 0; i < n && !matched; i++) {
3352 if (archive_test_acl_match(&acls[marker[i]], type,
3353 permset, tag, qual, name)) {
3354 /* We found a match; remove it. */
3355 marker[i] = marker[n - 1];
3360 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3361 && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
3363 failure_start(file, line, "No match for "
3365 failure_finish(NULL);
3368 if ((permset << 6) != (mode & 0700)) {
3369 failure_start(file, line, "USER_OBJ permset "
3370 "(%02o) != user mode (%02o)", permset,
3372 failure_finish(NULL);
3375 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3376 && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
3378 failure_start(file, line, "No match for "
3380 failure_finish(NULL);
3383 if ((permset << 3) != (mode & 0070)) {
3384 failure_start(file, line, "GROUP_OBJ permset "
3385 "(%02o) != group mode (%02o)", permset,
3387 failure_finish(NULL);
3390 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3391 && tag == ARCHIVE_ENTRY_ACL_OTHER) {
3393 failure_start(file, line, "No match for "
3395 failure_finish(NULL);
3398 if ((permset << 0) != (mode & 0007)) {
3399 failure_start(file, line, "OTHER permset "
3400 "(%02o) != other mode (%02o)", permset,
3402 failure_finish(NULL);
3405 } else if (matched != 1) {
3406 failure_start(file, line, "Could not find match for "
3407 "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d,"
3408 "name=``%s'')", type, permset, tag, qual, name);
3409 failure_finish(NULL);
3413 if (r != ARCHIVE_EOF) {
3414 failure_start(file, line, "Should not exit before EOF");
3415 failure_finish(NULL);
3418 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 &&
3419 (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) {
3420 failure_start(file, line, "Mode (%02o) and entry mode (%02o) "
3421 "mismatch", mode, archive_entry_mode(ae));
3422 failure_finish(NULL);
3426 failure_start(file, line, "Could not find match for ACL "
3427 "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')",
3428 acls[marker[0]].type, acls[marker[0]].permset,
3429 acls[marker[0]].tag, acls[marker[0]].qual,
3430 acls[marker[0]].name);
3431 failure_finish(NULL);
3433 /* Number of ACLs not matched should == 0 */
3438 #endif /* !defined(PROGRAM) */
3447 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
3449 * DEFINE_TEST(test_function)
3453 /* Use "list.h" to declare all of the test functions. */
3455 #define DEFINE_TEST(name) void name(void);
3458 /* Use "list.h" to create a list of all tests (functions and names). */
3460 #define DEFINE_TEST(n) { n, #n, 0 },
3461 struct test_list_t tests[] = {
3466 * Summarize repeated failures in the just-completed test.
3469 test_summarize(int failed, int skips_num)
3473 switch (verbosity) {
3474 case VERBOSITY_SUMMARY_ONLY:
3475 printf(failed ? "E" : ".");
3478 case VERBOSITY_PASSFAIL:
3479 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
3483 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
3485 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
3486 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
3487 logprintf("%s:%d: Summary: Failed %d times\n",
3488 failed_filename, i, failed_lines[i].count);
3490 /* Clear the failure history for the next file. */
3491 failed_filename = NULL;
3492 memset(failed_lines, 0, sizeof(failed_lines));
3496 * Actually run a single test, with appropriate setup and cleanup.
3499 test_run(int i, const char *tmpdir)
3502 char workdir[PATH_MAX];
3506 char logfilename[64];
3507 int failures_before = failures;
3508 int skips_before = skips;
3511 switch (verbosity) {
3512 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
3514 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
3515 printf("%3d: %-64s", i, tests[i].name);
3518 default: /* Title of test, details will follow */
3519 printf("%3d: %s\n", i, tests[i].name);
3522 /* Chdir to the top-level work directory. */
3523 if (!assertChdir(tmpdir)) {
3525 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
3528 /* Create a log file for this test. */
3529 sprintf(logfilename, "%s.log", tests[i].name);
3530 logfile = fopen(logfilename, "w");
3531 fprintf(logfile, "%s\n\n", tests[i].name);
3532 /* Chdir() to a work dir for this specific test. */
3533 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
3534 testworkdir = workdir;
3535 if (!assertMakeDir(testworkdir, 0755)
3536 || !assertChdir(testworkdir)) {
3538 "ERROR: Can't chdir to work dir %s\n", testworkdir);
3541 /* Explicitly reset the locale before each test. */
3542 setlocale(LC_ALL, "C");
3543 /* Record the umask before we run the test. */
3544 umask(oldumask = umask(0));
3546 * Run the actual test.
3550 * Clean up and report afterwards.
3556 setlocale(LC_ALL, "C");
3557 /* Reset directory. */
3558 if (!assertChdir(tmpdir)) {
3559 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
3563 /* Report per-test summaries. */
3564 tests[i].failures = failures - failures_before;
3565 test_summarize(tests[i].failures, skips - skips_before);
3566 /* Close the per-test log file. */
3569 /* If there were no failures, we can remove the work dir and logfile. */
3570 if (tests[i].failures == 0) {
3571 if (!keep_temp_files && assertChdir(tmpdir)) {
3572 #if defined(_WIN32) && !defined(__CYGWIN__)
3573 /* Make sure not to leave empty directories.
3574 * Sometimes a processing of closing files used by tests
3575 * is not done, then rmdir will be failed and it will
3576 * leave a empty test directory. So we should wait a few
3577 * seconds and retry rmdir. */
3579 for (t = 0; t < 10; t++) {
3582 r = systemf("rmdir /S /Q %s", tests[i].name);
3586 systemf("del %s", logfilename);
3588 systemf("rm -rf %s", tests[i].name);
3589 systemf("rm %s", logfilename);
3593 /* Return appropriate status. */
3594 return (tests[i].failures);
3600 * MAIN and support routines.
3606 usage(const char *program)
3608 static const int limit = sizeof(tests) / sizeof(tests[0]);
3611 printf("Usage: %s [options] <test> <test> ...\n", program);
3612 printf("Default is to run all tests.\n");
3613 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
3614 printf("Options:\n");
3615 printf(" -d Dump core after any failure, for debugging.\n");
3616 printf(" -k Keep all temp files.\n");
3617 printf(" Default: temp files for successful tests deleted.\n");
3619 printf(" -p <path> Path to executable to be tested.\n");
3620 printf(" Default: path taken from " ENVBASE " environment variable.\n");
3622 printf(" -q Quiet.\n");
3623 printf(" -r <dir> Path to dir containing reference files.\n");
3624 printf(" Default: Current directory.\n");
3625 printf(" -u Keep running specifies tests until one fails.\n");
3626 printf(" -v Verbose.\n");
3627 printf("Available tests:\n");
3628 for (i = 0; i < limit; i++)
3629 printf(" %d: %s\n", i, tests[i].name);
3634 get_refdir(const char *d)
3636 size_t tried_size, buff_size;
3637 char *buff, *tried, *pwd = NULL, *p = NULL;
3640 buff_size = PATH_MAX;
3644 buff = calloc(buff_size, 1);
3646 fprintf(stderr, "Unable to allocate memory\n");
3650 /* Allocate a buffer to hold the various directories we checked. */
3651 tried_size = buff_size * 2;
3652 tried = calloc(tried_size, 1);
3653 if (tried == NULL) {
3654 fprintf(stderr, "Unable to allocate memory\n");
3658 /* If a dir was specified, try that */
3661 snprintf(buff, buff_size, "%s", d);
3662 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3663 if (p != NULL) goto success;
3664 strncat(tried, buff, tried_size - strlen(tried) - 1);
3665 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3669 /* Get the current dir. */
3671 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3673 pwd = getcwd(NULL, 0);
3675 while (pwd[strlen(pwd) - 1] == '\n')
3676 pwd[strlen(pwd) - 1] = '\0';
3678 /* Look for a known file. */
3679 snprintf(buff, buff_size, "%s", pwd);
3680 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3681 if (p != NULL) goto success;
3682 strncat(tried, buff, tried_size - strlen(tried) - 1);
3683 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3685 snprintf(buff, buff_size, "%s/test", pwd);
3686 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3687 if (p != NULL) goto success;
3688 strncat(tried, buff, tried_size - strlen(tried) - 1);
3689 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3691 #if defined(LIBRARY)
3692 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
3694 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
3696 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3697 if (p != NULL) goto success;
3698 strncat(tried, buff, tried_size - strlen(tried) - 1);
3699 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3701 #if defined(PROGRAM_ALIAS)
3702 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
3703 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3704 if (p != NULL) goto success;
3705 strncat(tried, buff, tried_size - strlen(tried) - 1);
3706 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3709 if (memcmp(pwd, "/usr/obj", 8) == 0) {
3710 snprintf(buff, buff_size, "%s", pwd + 8);
3711 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3712 if (p != NULL) goto success;
3713 strncat(tried, buff, tried_size - strlen(tried) - 1);
3714 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3716 snprintf(buff, buff_size, "%s/test", pwd + 8);
3717 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3718 if (p != NULL) goto success;
3719 strncat(tried, buff, tried_size - strlen(tried) - 1);
3720 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3724 printf("Unable to locate known reference file %s\n", KNOWNREF);
3725 printf(" Checked following directories:\n%s\n", tried);
3726 printf("Use -r option to specify full path to reference directory\n");
3727 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
3737 /* Copy result into a fresh buffer to reduce memory usage. */
3744 main(int argc, char **argv)
3746 static const int limit = sizeof(tests) / sizeof(tests[0]);
3747 int test_set[sizeof(tests) / sizeof(tests[0])];
3748 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
3750 char *refdir_alloc = NULL;
3751 const char *progname;
3753 const char *tmp, *option_arg, *p;
3755 char tmpdir[PATH_MAX];
3759 char *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
3760 char tmpdir_timestamp[32];
3762 (void)argc; /* UNUSED */
3764 /* Get the current dir. */
3766 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3768 pwd = getcwd(NULL, 0);
3770 while (pwd[strlen(pwd) - 1] == '\n')
3771 pwd[strlen(pwd) - 1] = '\0';
3773 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
3774 /* To stop to run the default invalid parameter handler. */
3775 _set_invalid_parameter_handler(invalid_parameter_handler);
3776 /* Disable annoying assertion message box. */
3777 _CrtSetReportMode(_CRT_ASSERT, 0);
3781 * Name of this program, used to build root of our temp directory
3784 progname = p = argv[0];
3785 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
3787 fprintf(stderr, "ERROR: Out of memory.");
3790 strcpy(testprogdir, progname);
3791 while (*p != '\0') {
3792 /* Support \ or / dir separators for Windows compat. */
3793 if (*p == '/' || *p == '\\')
3801 testprogdir[i] = '\0';
3802 #if defined(_WIN32) && !defined(__CYGWIN__)
3803 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
3804 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
3805 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
3806 testprogdir[1] == ':' &&
3807 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
3809 if (testprogdir[0] != '/')
3812 /* Fixup path for relative directories. */
3813 if ((testprogdir = (char *)realloc(testprogdir,
3814 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
3816 fprintf(stderr, "ERROR: Out of memory.");
3819 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
3820 strlen(testprogdir) + 1);
3821 memcpy(testprogdir, pwd, strlen(pwd));
3822 testprogdir[strlen(pwd)] = '/';
3826 /* Get the target program from environment, if available. */
3827 testprogfile = getenv(ENVBASE);
3830 if (getenv("TMPDIR") != NULL)
3831 tmp = getenv("TMPDIR");
3832 else if (getenv("TMP") != NULL)
3833 tmp = getenv("TMP");
3834 else if (getenv("TEMP") != NULL)
3835 tmp = getenv("TEMP");
3836 else if (getenv("TEMPDIR") != NULL)
3837 tmp = getenv("TEMPDIR");
3841 /* Allow -d to be controlled through the environment. */
3842 if (getenv(ENVBASE "_DEBUG") != NULL)
3843 dump_on_failure = 1;
3845 /* Allow -v to be controlled through the environment. */
3846 if (getenv("_VERBOSITY_LEVEL") != NULL)
3848 vlevel = getenv("_VERBOSITY_LEVEL");
3849 verbosity = atoi(vlevel);
3850 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
3852 /* Unsupported verbosity levels are silently ignored */
3854 verbosity = VERBOSITY_PASSFAIL;
3858 /* Get the directory holding test files from environment. */
3859 refdir = getenv(ENVBASE "_TEST_FILES");
3862 * Parse options, without using getopt(), which isn't available
3865 ++argv; /* Skip program name */
3866 while (*argv != NULL) {
3871 while (*p != '\0') {
3874 /* If 'opt' takes an argument, parse that. */
3875 if (option == 'p' || option == 'r') {
3878 else if (*argv == NULL) {
3880 "Option -%c requires argument.\n",
3884 option_arg = *argv++;
3885 p = ""; /* End of this option word. */
3888 /* Now, handle the option. */
3891 dump_on_failure = 1;
3894 keep_temp_files = 1;
3898 testprogfile = option_arg;
3900 fprintf(stderr, "-p option not permitted\n");
3909 refdir = option_arg;
3919 fprintf(stderr, "Unrecognized option '%c'\n",
3927 * Sanity-check that our options make sense.
3930 if (testprogfile == NULL)
3932 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
3933 strlen(PROGRAM) + 1)) == NULL)
3935 fprintf(stderr, "ERROR: Out of memory.");
3938 strcpy(tmp2, testprogdir);
3940 strcat(tmp2, PROGRAM);
3941 testprogfile = tmp2;
3946 #if defined(_WIN32) && !defined(__CYGWIN__)
3947 /* Command.com sometimes rejects '/' separators. */
3948 testprg = strdup(testprogfile);
3949 for (i = 0; testprg[i] != '\0'; i++) {
3950 if (testprg[i] == '/')
3953 testprogfile = testprg;
3955 /* Quote the name that gets put into shell command lines. */
3956 testprg = malloc(strlen(testprogfile) + 3);
3957 strcpy(testprg, "\"");
3958 strcat(testprg, testprogfile);
3959 strcat(testprg, "\"");
3964 #if !defined(_WIN32) && defined(SIGPIPE)
3965 { /* Ignore SIGPIPE signals */
3966 struct sigaction sa;
3967 sa.sa_handler = SIG_IGN;
3968 sigemptyset(&sa.sa_mask);
3970 sigaction(SIGPIPE, &sa, NULL);
3975 * Create a temp directory for the following tests.
3976 * Include the time the tests started as part of the name,
3977 * to make it easier to track the results of multiple tests.
3980 for (i = 0; ; i++) {
3981 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
3982 "%Y-%m-%dT%H.%M.%S",
3984 if ((strlen(tmp) + 1 + strlen(progname) + 1 +
3985 strlen(tmpdir_timestamp) + 1 + 3) >
3986 (sizeof(tmpdir) / sizeof(char))) {
3988 "ERROR: Temp directory pathname too long\n");
3991 snprintf(tmpdir, sizeof(tmpdir), "%s/%s.%s-%03d", tmp,
3992 progname, tmpdir_timestamp, i);
3993 if (assertMakeDir(tmpdir,0755))
3997 "ERROR: Unable to create temp directory %s\n",
4004 * If the user didn't specify a directory for locating
4005 * reference files, try to find the reference files in
4006 * the "usual places."
4008 refdir = refdir_alloc = get_refdir(refdir);
4011 * Banner with basic information.
4014 printf("If tests fail or crash, details will be in:\n");
4015 printf(" %s\n", tmpdir);
4017 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
4018 printf("Reference files will be read from: %s\n", refdir);
4020 printf("Running tests on: %s\n", testprog);
4022 printf("Exercising: ");
4024 printf("%s\n", EXTRA_VERSION);
4031 * Run some or all of the individual tests.
4039 test_num = get_test_set(test_set, limit, *argv, tests);
4041 printf("*** INVALID Test %s\n", *argv);
4047 for (i = 0; i < test_num; i++) {
4049 if (test_run(test_set[i], tmpdir)) {
4057 } while (*argv != NULL);
4058 } while (until_failure);
4061 /* Must be freed after all tests run */
4067 * Report summary statistics.
4069 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
4071 printf("Totals:\n");
4072 printf(" Tests run: %8d\n", tests_run);
4073 printf(" Tests failed: %8d\n", tests_failed);
4074 printf(" Assertions checked:%8d\n", assertions);
4075 printf(" Assertions failed: %8d\n", failures);
4076 printf(" Skips reported: %8d\n", skips);
4080 printf("Failing tests:\n");
4081 for (i = 0; i < limit; ++i) {
4082 if (tests[i].failures)
4083 printf(" %d: %s (%d failures)\n", i,
4084 tests[i].name, tests[i].failures);
4087 printf("Details for failing tests: %s\n", tmpdir);
4090 if (verbosity == VERBOSITY_SUMMARY_ONLY)
4092 printf("%d tests passed, no failures\n", tests_run);
4097 /* If the final tmpdir is empty, we can remove it. */
4098 /* This should be the usual case when all tests succeed. */
4102 return (tests_failed ? 1 : 0);