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>
70 #ifdef HAVE_SYS_RICHACL_H
71 #include <sys/richacl.h>
74 #include <membership.h>
79 * Windows support routines
81 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
82 * in the test harness is dangerous because they cover up
83 * configuration errors. The classic example of this is omitting a
84 * configure check. If libarchive and libarchive_test both look for
85 * the same feature macro, such errors are hard to detect. Platform
86 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
87 * easily lead to very messy code. It's best to limit yourself
88 * to only the most generic programming techniques in the test harness
89 * and thus avoid conditionals altogether. Where that's not possible,
90 * try to minimize conditionals by grouping platform-specific tests in
91 * one place (e.g., test_acl_freebsd) or by adding new assert()
92 * functions (e.g., assertMakeHardlink()) to cover up platform
93 * differences. Platform-specific coding in libarchive_test is often
94 * a symptom that some capability is missing from libarchive itself.
96 #if defined(_WIN32) && !defined(__CYGWIN__)
104 #define S_ISDIR(m) ((m) & _S_IFDIR)
107 #define S_ISREG(m) ((m) & _S_IFREG)
109 #if !defined(__BORLANDC__)
110 #define access _access
115 #define fileno _fileno
117 /*#define fstat _fstat64*/
118 #if !defined(__BORLANDC__)
119 #define getcwd _getcwd
122 /*#define lstat _stat64*/
123 /*#define stat _stat64*/
125 #if !defined(__BORLANDC__)
126 #define strdup _strdup
129 #define int64_t __int64
132 #if defined(HAVE__CrtSetReportMode)
136 mode_t umasked(mode_t expected_mode)
138 mode_t mode = umask(0);
140 return expected_mode & ~mode;
143 /* Path to working directory for current test */
144 const char *testworkdir;
146 /* Pathname of exe to be tested. */
147 const char *testprogfile;
148 /* Name of exe to use in printf-formatted command strings. */
149 /* On Windows, this includes leading/trailing quotes. */
150 const char *testprog;
153 #if defined(_WIN32) && !defined(__CYGWIN__)
154 static void *GetFunctionKernel32(const char *);
155 static int my_CreateSymbolicLinkA(const char *, const char *, int);
156 static int my_CreateHardLinkA(const char *, const char *);
157 static int my_GetFileInformationByName(const char *,
158 BY_HANDLE_FILE_INFORMATION *);
161 GetFunctionKernel32(const char *name)
163 static HINSTANCE lib;
167 lib = LoadLibrary("kernel32.dll");
170 fprintf(stderr, "Can't load kernel32.dll?!\n");
173 return (void *)GetProcAddress(lib, name);
177 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
179 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
183 f = GetFunctionKernel32("CreateSymbolicLinkA");
185 return f == NULL ? 0 : (*f)(linkname, target, flags);
189 my_CreateHardLinkA(const char *linkname, const char *target)
191 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
195 f = GetFunctionKernel32("CreateHardLinkA");
197 return f == NULL ? 0 : (*f)(linkname, target, NULL);
201 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
206 memset(bhfi, 0, sizeof(*bhfi));
207 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
208 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
209 if (h == INVALID_HANDLE_VALUE)
211 r = GetFileInformationByHandle(h, bhfi);
217 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
219 invalid_parameter_handler(const wchar_t * expression,
220 const wchar_t * function, const wchar_t * file,
221 unsigned int line, uintptr_t pReserved)
224 // Silence unused-parameter compiler warnings.
239 /* Enable core dump on failure. */
240 static int dump_on_failure = 0;
241 /* Default is to remove temp dirs and log data for successful tests. */
242 static int keep_temp_files = 0;
243 /* Default is to run the specified tests once and report errors. */
244 static int until_failure = 0;
245 /* Default is to just report pass/fail for each test. */
246 static int verbosity = 0;
247 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
248 #define VERBOSITY_PASSFAIL 0 /* Default */
249 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
250 #define VERBOSITY_FULL 2 /* -vv */
251 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
252 * mostly for debugging the test harness itself. */
253 /* Cumulative count of assertion failures. */
254 static int failures = 0;
255 /* Cumulative count of reported skips. */
256 static int skips = 0;
257 /* Cumulative count of assertions checked. */
258 static int assertions = 0;
260 /* Directory where uuencoded reference files can be found. */
261 static const char *refdir;
264 * Report log information selectively to console and/or disk log.
266 static int log_console = 0;
267 static FILE *logfile;
269 vlogprintf(const char *fmt, va_list ap)
276 vfprintf(stdout, fmt, ap);
279 vfprintf(logfile, fmt, lfap);
282 vfprintf(logfile, fmt, ap);
287 logprintf(const char *fmt, ...)
295 /* Set up a message to display only if next assertion fails. */
296 static char msgbuff[4096];
297 static const char *msg, *nextmsg;
299 failure(const char *fmt, ...)
306 vsprintf(msgbuff, fmt, ap);
313 * Copy arguments into file-local variables.
314 * This was added to permit vararg assert() functions without needing
315 * variadic wrapper macros. Turns out that the vararg capability is almost
316 * never used, so almost all of the vararg assertions can be simplified
317 * by removing the vararg capability and reworking the wrapper macro to
318 * pass __FILE__, __LINE__ directly into the function instead of using
319 * this hook. I suspect this machinery is used so rarely that we
320 * would be better off just removing it entirely. That would simplify
321 * the code here noticeably.
323 static const char *skipping_filename;
324 static int skipping_line;
325 void skipping_setup(const char *filename, int line)
327 skipping_filename = filename;
328 skipping_line = line;
331 /* Called at the beginning of each assert() function. */
333 assertion_count(const char *file, int line)
335 (void)file; /* UNUSED */
336 (void)line; /* UNUSED */
338 /* Proper handling of "failure()" message. */
341 /* Uncomment to print file:line after every assertion.
342 * Verbose, but occasionally useful in tracking down crashes. */
343 /* printf("Checked %s:%d\n", file, line); */
347 * For each test source file, we remember how many times each
348 * assertion was reported. Cleared before each new test,
349 * used by test_summarize().
354 } failed_lines[10000];
355 const char *failed_filename;
357 /* Count this failure, setup up log destination and handle initial report. */
359 failure_start(const char *filename, int line, const char *fmt, ...)
363 /* Record another failure for this line. */
365 failed_filename = filename;
366 failed_lines[line].count++;
368 /* Determine whether to log header to console. */
370 case VERBOSITY_LIGHT_REPORT:
371 log_console = (failed_lines[line].count < 2);
374 log_console = (verbosity >= VERBOSITY_FULL);
377 /* Log file:line header for this failure */
380 logprintf("%s(%d): ", filename, line);
382 logprintf("%s:%d: ", filename, line);
388 if (msg != NULL && msg[0] != '\0') {
389 logprintf(" Description: %s\n", msg);
393 /* Determine whether to log details to console. */
394 if (verbosity == VERBOSITY_LIGHT_REPORT)
398 /* Complete reporting of failed tests. */
400 * The 'extra' hook here is used by libarchive to include libarchive
401 * error messages with assertion failures. It could also be used
402 * to add strerror() output, for example. Just define the EXTRA_DUMP()
403 * macro appropriately.
406 failure_finish(void *extra)
408 (void)extra; /* UNUSED (maybe) */
411 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
412 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
416 if (dump_on_failure) {
418 " *** forcing core dump so failure can be debugged ***\n");
423 /* Inform user that we're skipping some checks. */
425 test_skipping(const char *fmt, ...)
431 vsprintf(buff, fmt, ap);
433 /* Use failure() message if set. */
436 /* failure_start() isn't quite right, but is awfully convenient. */
437 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
438 --failures; /* Undo failures++ in failure_start() */
439 /* Don't failure_finish() here. */
440 /* Mark as skip, so doesn't count as failed test. */
441 failed_lines[skipping_line].skip = 1;
451 /* Generic assert() just displays the failed condition. */
453 assertion_assert(const char *file, int line, int value,
454 const char *condition, void *extra)
456 assertion_count(file, line);
458 failure_start(file, line, "Assertion failed: %s", condition);
459 failure_finish(extra);
464 /* chdir() and report any errors */
466 assertion_chdir(const char *file, int line, const char *pathname)
468 assertion_count(file, line);
469 if (chdir(pathname) == 0)
471 failure_start(file, line, "chdir(\"%s\")", pathname);
472 failure_finish(NULL);
477 /* Verify two integers are equal. */
479 assertion_equal_int(const char *file, int line,
480 long long v1, const char *e1, long long v2, const char *e2, void *extra)
482 assertion_count(file, line);
485 failure_start(file, line, "%s != %s", e1, e2);
486 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
487 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
488 failure_finish(extra);
493 * Utility to convert a single UTF-8 sequence.
496 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
498 static const char utf8_count[256] = {
499 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
500 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
501 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
502 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
503 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
504 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
505 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
506 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
507 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
508 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
509 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
510 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
511 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
512 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
513 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
514 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
526 * Decode 1-4 bytes depending on the value of the first byte.
528 ch = (unsigned char)*s;
530 return (0); /* Standard: return 0 for end-of-string. */
531 cnt = utf8_count[ch];
533 /* Invalid sequence or there are not plenty bytes. */
537 /* Make a Unicode code point from a single UTF-8 sequence. */
539 case 1: /* 1 byte sequence. */
542 case 2: /* 2 bytes sequence. */
543 if ((s[1] & 0xc0) != 0x80) return (-1);
544 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
546 case 3: /* 3 bytes sequence. */
547 if ((s[1] & 0xc0) != 0x80) return (-1);
548 if ((s[2] & 0xc0) != 0x80) return (-1);
549 wc = ((ch & 0x0f) << 12)
550 | ((s[1] & 0x3f) << 6)
553 return (-1);/* Overlong sequence. */
555 case 4: /* 4 bytes sequence. */
558 if ((s[1] & 0xc0) != 0x80) return (-1);
559 if ((s[2] & 0xc0) != 0x80) return (-1);
560 if ((s[3] & 0xc0) != 0x80) return (-1);
561 wc = ((ch & 0x07) << 18)
562 | ((s[1] & 0x3f) << 12)
563 | ((s[2] & 0x3f) << 6)
566 return (-1);/* Overlong sequence. */
572 /* The code point larger than 0x10FFFF is not legal
576 /* Correctly gets a Unicode, returns used bytes. */
581 static void strdump(const char *e, const char *p, int ewidth, int utf8)
585 logprintf(" %*s = ", ewidth, e);
592 unsigned int c = 0xff & *p++;
594 case '\a': logprintf("\\a"); break;
595 case '\b': logprintf("\\b"); break;
596 case '\n': logprintf("\\n"); break;
597 case '\r': logprintf("\\r"); break;
599 if (c >= 32 && c < 127)
602 logprintf("\\x%02X", c);
606 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
609 * If the current string is UTF-8, dump its code points.
620 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
623 logprintf("%04X", uc);
629 logprintf(" (count %d", cnt);
631 logprintf(",unknown %d bytes", len);
639 /* Verify two strings are equal, dump them if not. */
641 assertion_equal_string(const char *file, int line,
642 const char *v1, const char *e1,
643 const char *v2, const char *e2,
644 void *extra, int utf8)
648 assertion_count(file, line);
649 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
651 failure_start(file, line, "%s != %s", e1, e2);
652 l1 = (int)strlen(e1);
653 l2 = (int)strlen(e2);
656 strdump(e1, v1, l1, utf8);
657 strdump(e2, v2, l1, utf8);
658 failure_finish(extra);
663 wcsdump(const char *e, const wchar_t *w)
665 logprintf(" %s = ", e);
671 while (*w != L'\0') {
672 unsigned int c = *w++;
673 if (c >= 32 && c < 127)
676 logprintf("\\x%02X", c);
677 else if (c < 0x10000)
678 logprintf("\\u%04X", c);
680 logprintf("\\U%08X", c);
687 wcscmp(const wchar_t *s1, const wchar_t *s2)
690 while (*s1 == *s2++) {
701 /* Verify that two wide strings are equal, dump them if not. */
703 assertion_equal_wstring(const char *file, int line,
704 const wchar_t *v1, const char *e1,
705 const wchar_t *v2, const char *e2,
708 assertion_count(file, line);
711 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
713 failure_start(file, line, "%s != %s", e1, e2);
716 failure_finish(extra);
721 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
722 * any bytes in p that differ from ref will be highlighted with '_'
723 * before and after the hex value.
726 hexdump(const char *p, const char *ref, size_t l, size_t offset)
732 logprintf("(null)\n");
735 for(i=0; i < l; i+=16) {
736 logprintf("%04x", (unsigned)(i + offset));
738 for (j = 0; j < 16 && i + j < l; j++) {
739 if (ref != NULL && p[i + j] != ref[i + j])
741 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
742 if (ref != NULL && p[i + j] == ref[i + j])
745 for (; j < 16; j++) {
746 logprintf("%c ", sep);
749 logprintf("%c", sep);
750 for (j=0; j < 16 && i + j < l; j++) {
752 if (c >= ' ' && c <= 126)
761 /* Verify that two blocks of memory are the same, display the first
762 * block of differences if they're not. */
764 assertion_equal_mem(const char *file, int line,
765 const void *_v1, const char *e1,
766 const void *_v2, const char *e2,
767 size_t l, const char *ld, void *extra)
769 const char *v1 = (const char *)_v1;
770 const char *v2 = (const char *)_v2;
773 assertion_count(file, line);
774 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
776 if (v1 == NULL || v2 == NULL)
779 failure_start(file, line, "%s != %s", e1, e2);
780 logprintf(" size %s = %d\n", ld, (int)l);
781 /* Dump 48 bytes (3 lines) so that the first difference is
782 * in the second line. */
784 while (l > 64 && memcmp(v1, v2, 32) == 0) {
785 /* Two lines agree, so step forward one line. */
791 logprintf(" Dump of %s\n", e1);
792 hexdump(v1, v2, l < 128 ? l : 128, offset);
793 logprintf(" Dump of %s\n", e2);
794 hexdump(v2, v1, l < 128 ? l : 128, offset);
796 failure_finish(extra);
800 /* Verify that a block of memory is filled with the specified byte. */
802 assertion_memory_filled_with(const char *file, int line,
803 const void *_v1, const char *vd,
804 size_t l, const char *ld,
805 char b, const char *bd, void *extra)
807 const char *v1 = (const char *)_v1;
810 (void)ld; /* UNUSED */
812 assertion_count(file, line);
814 for (i = 0; i < l; ++i) {
822 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
823 logprintf(" Only %d bytes were correct\n", (int)c);
824 failure_finish(extra);
828 /* Verify that the named file exists and is empty. */
830 assertion_empty_file(const char *filename, int line, const char *f1)
837 assertion_count(filename, line);
839 if (stat(f1, &st) != 0) {
840 failure_start(filename, line, "Stat failed: %s", f1);
841 failure_finish(NULL);
847 failure_start(filename, line, "File should be empty: %s", f1);
848 logprintf(" File size: %d\n", (int)st.st_size);
849 logprintf(" Contents:\n");
852 logprintf(" Unable to open %s\n", f1);
854 s = ((off_t)sizeof(buff) < st.st_size) ?
855 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
856 s = fread(buff, 1, s, f);
857 hexdump(buff, NULL, s, 0);
860 failure_finish(NULL);
864 /* Verify that the named file exists and is not empty. */
866 assertion_non_empty_file(const char *filename, int line, const char *f1)
870 assertion_count(filename, line);
872 if (stat(f1, &st) != 0) {
873 failure_start(filename, line, "Stat failed: %s", f1);
874 failure_finish(NULL);
877 if (st.st_size == 0) {
878 failure_start(filename, line, "File empty: %s", f1);
879 failure_finish(NULL);
885 /* Verify that two files have the same contents. */
886 /* TODO: hexdump the first bytes that actually differ. */
888 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
895 assertion_count(filename, line);
897 f1 = fopen(fn1, "rb");
898 f2 = fopen(fn2, "rb");
899 if (f1 == NULL || f2 == NULL) {
905 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
906 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
909 if (n1 == 0 && n2 == 0) {
914 if (memcmp(buff1, buff2, n1) != 0)
919 failure_start(filename, line, "Files not identical");
920 logprintf(" file1=\"%s\"\n", fn1);
921 logprintf(" file2=\"%s\"\n", fn2);
922 failure_finish(NULL);
926 /* Verify that the named file does exist. */
928 assertion_file_exists(const char *filename, int line, const char *f)
930 assertion_count(filename, line);
932 #if defined(_WIN32) && !defined(__CYGWIN__)
936 if (!access(f, F_OK))
939 failure_start(filename, line, "File should exist: %s", f);
940 failure_finish(NULL);
944 /* Verify that the named file doesn't exist. */
946 assertion_file_not_exists(const char *filename, int line, const char *f)
948 assertion_count(filename, line);
950 #if defined(_WIN32) && !defined(__CYGWIN__)
957 failure_start(filename, line, "File should not exist: %s", f);
958 failure_finish(NULL);
962 /* Compare the contents of a file to a block of memory. */
964 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
970 assertion_count(filename, line);
974 failure_start(filename, line,
975 "File should exist: %s", fn);
976 failure_finish(NULL);
979 contents = malloc(s * 2);
980 n = (int)fread(contents, 1, s * 2, f);
982 if (n == s && memcmp(buff, contents, s) == 0) {
986 failure_start(filename, line, "File contents don't match");
987 logprintf(" file=\"%s\"\n", fn);
989 hexdump(contents, buff, n > 512 ? 512 : n, 0);
991 logprintf(" File empty, contents should be:\n");
992 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
994 failure_finish(NULL);
999 /* Check the contents of a text file, being tolerant of line endings. */
1001 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
1004 const char *btxt, *ftxt;
1008 assertion_count(filename, line);
1011 failure_start(filename, line,
1012 "File doesn't exist: %s", fn);
1013 failure_finish(NULL);
1016 s = (int)strlen(buff);
1017 contents = malloc(s * 2 + 128);
1018 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1022 /* Compare texts. */
1024 ftxt = (const char *)contents;
1025 while (*btxt != '\0' && *ftxt != '\0') {
1026 if (*btxt == *ftxt) {
1031 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1032 /* Pass over different new line characters. */
1039 if (*btxt == '\0' && *ftxt == '\0') {
1043 failure_start(filename, line, "Contents don't match");
1044 logprintf(" file=\"%s\"\n", fn);
1046 hexdump(contents, buff, n, 0);
1047 logprintf(" expected\n", fn);
1048 hexdump(buff, contents, s, 0);
1050 logprintf(" File empty, contents should be:\n");
1051 hexdump(buff, NULL, s, 0);
1053 failure_finish(NULL);
1058 /* Verify that a text file contains the specified lines, regardless of order */
1059 /* This could be more efficient if we sorted both sets of lines, etc, but
1060 * since this is used only for testing and only ever deals with a dozen or so
1061 * lines at a time, this relatively crude approach is just fine. */
1063 assertion_file_contains_lines_any_order(const char *file, int line,
1064 const char *pathname, const char *lines[])
1068 size_t expected_count, actual_count, i, j;
1069 char **expected = NULL;
1070 char *p, **actual = NULL;
1072 int expected_failure = 0, actual_failure = 0;
1074 assertion_count(file, line);
1076 buff = slurpfile(&buff_size, "%s", pathname);
1078 failure_start(pathname, line, "Can't read file: %s", pathname);
1079 failure_finish(NULL);
1083 /* Make a copy of the provided lines and count up the expected
1085 for (i = 0; lines[i] != NULL; ++i) {
1088 if (expected_count) {
1089 expected = malloc(sizeof(char *) * expected_count);
1090 if (expected == NULL) {
1091 failure_start(pathname, line, "Can't allocate memory");
1092 failure_finish(NULL);
1096 for (i = 0; lines[i] != NULL; ++i) {
1097 expected[i] = strdup(lines[i]);
1101 /* Break the file into lines */
1103 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1104 if (*p == '\x0d' || *p == '\x0a')
1106 if (c == '\0' && *p != '\0')
1111 actual = calloc(sizeof(char *), actual_count);
1112 if (actual == NULL) {
1113 failure_start(pathname, line, "Can't allocate memory");
1114 failure_finish(NULL);
1118 for (j = 0, p = buff; p < buff + buff_size;
1119 p += 1 + strlen(p)) {
1127 /* Erase matching lines from both lists */
1128 for (i = 0; i < expected_count; ++i) {
1129 if (expected[i] == NULL)
1131 for (j = 0; j < actual_count; ++j) {
1132 if (actual[j] == NULL)
1134 if (strcmp(expected[i], actual[j]) == 0) {
1143 /* If there's anything left, it's a failure */
1144 for (i = 0; i < expected_count; ++i) {
1145 if (expected[i] != NULL)
1148 for (j = 0; j < actual_count; ++j) {
1149 if (actual[j] != NULL)
1152 if (expected_failure == 0 && actual_failure == 0) {
1158 failure_start(file, line, "File doesn't match: %s", pathname);
1159 for (i = 0; i < expected_count; ++i) {
1160 if (expected[i] != NULL) {
1161 logprintf(" Expected but not present: %s\n", expected[i]);
1165 for (j = 0; j < actual_count; ++j) {
1166 if (actual[j] != NULL)
1167 logprintf(" Present but not expected: %s\n", actual[j]);
1169 failure_finish(NULL);
1176 /* Verify that a text file does not contains the specified strings */
1178 assertion_file_contains_no_invalid_strings(const char *file, int line,
1179 const char *pathname, const char *strings[])
1184 buff = slurpfile(NULL, "%s", pathname);
1186 failure_start(file, line, "Can't read file: %s", pathname);
1187 failure_finish(NULL);
1191 for (i = 0; strings[i] != NULL; ++i) {
1192 if (strstr(buff, strings[i]) != NULL) {
1193 failure_start(file, line, "Invalid string in %s: %s", pathname,
1195 failure_finish(NULL);
1205 /* Test that two paths point to the same file. */
1206 /* As a side-effect, asserts that both files exist. */
1208 is_hardlink(const char *file, int line,
1209 const char *path1, const char *path2)
1211 #if defined(_WIN32) && !defined(__CYGWIN__)
1212 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1215 assertion_count(file, line);
1216 r = my_GetFileInformationByName(path1, &bhfi1);
1218 failure_start(file, line, "File %s can't be inspected?", path1);
1219 failure_finish(NULL);
1222 r = my_GetFileInformationByName(path2, &bhfi2);
1224 failure_start(file, line, "File %s can't be inspected?", path2);
1225 failure_finish(NULL);
1228 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1229 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1230 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1232 struct stat st1, st2;
1235 assertion_count(file, line);
1236 r = lstat(path1, &st1);
1238 failure_start(file, line, "File should exist: %s", path1);
1239 failure_finish(NULL);
1242 r = lstat(path2, &st2);
1244 failure_start(file, line, "File should exist: %s", path2);
1245 failure_finish(NULL);
1248 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1253 assertion_is_hardlink(const char *file, int line,
1254 const char *path1, const char *path2)
1256 if (is_hardlink(file, line, path1, path2))
1258 failure_start(file, line,
1259 "Files %s and %s are not hardlinked", path1, path2);
1260 failure_finish(NULL);
1265 assertion_is_not_hardlink(const char *file, int line,
1266 const char *path1, const char *path2)
1268 if (!is_hardlink(file, line, path1, path2))
1270 failure_start(file, line,
1271 "Files %s and %s should not be hardlinked", path1, path2);
1272 failure_finish(NULL);
1276 /* Verify a/b/mtime of 'pathname'. */
1277 /* If 'recent', verify that it's within last 10 seconds. */
1279 assertion_file_time(const char *file, int line,
1280 const char *pathname, long t, long nsec, char type, int recent)
1282 long long filet, filet_nsec;
1285 #if defined(_WIN32) && !defined(__CYGWIN__)
1286 #define EPOC_TIME (116444736000000000ULL)
1287 FILETIME fxtime, fbirthtime, fatime, fmtime;
1288 ULARGE_INTEGER wintm;
1290 fxtime.dwLowDateTime = 0;
1291 fxtime.dwHighDateTime = 0;
1293 assertion_count(file, line);
1294 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1295 * a directory file. If not, CreateFile() will fail when
1296 * the pathname is a directory. */
1297 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1298 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1299 if (h == INVALID_HANDLE_VALUE) {
1300 failure_start(file, line, "Can't access %s\n", pathname);
1301 failure_finish(NULL);
1304 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1306 case 'a': fxtime = fatime; break;
1307 case 'b': fxtime = fbirthtime; break;
1308 case 'm': fxtime = fmtime; break;
1312 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1313 failure_finish(NULL);
1316 wintm.LowPart = fxtime.dwLowDateTime;
1317 wintm.HighPart = fxtime.dwHighDateTime;
1318 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1319 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1320 nsec = (nsec / 100) * 100; /* Round the request */
1324 assertion_count(file, line);
1325 r = lstat(pathname, &st);
1327 failure_start(file, line, "Can't stat %s\n", pathname);
1328 failure_finish(NULL);
1332 case 'a': filet = st.st_atime; break;
1333 case 'm': filet = st.st_mtime; break;
1334 case 'b': filet = 0; break;
1335 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1338 #if defined(__FreeBSD__)
1340 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1341 case 'b': filet = st.st_birthtime;
1342 /* FreeBSD filesystems that don't support birthtime
1343 * (e.g., UFS1) always return -1 here. */
1347 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1348 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1349 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1352 /* FreeBSD generally only stores to microsecond res, so round. */
1353 filet_nsec = (filet_nsec / 1000) * 1000;
1354 nsec = (nsec / 1000) * 1000;
1356 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1357 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1358 #if defined(__HAIKU__)
1359 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1364 /* Check that requested time is up-to-date. */
1365 time_t now = time(NULL);
1366 if (filet < now - 10 || filet > now + 1) {
1367 failure_start(file, line,
1368 "File %s has %ctime %lld, %lld seconds ago\n",
1369 pathname, type, filet, now - filet);
1370 failure_finish(NULL);
1373 } else if (filet != t || filet_nsec != nsec) {
1374 failure_start(file, line,
1375 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1376 pathname, type, filet, filet_nsec, t, nsec);
1377 failure_finish(NULL);
1383 /* Verify atime of 'pathname'. */
1385 assertion_file_atime(const char *file, int line,
1386 const char *pathname, long t, long nsec)
1388 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1391 /* Verify atime of 'pathname' is up-to-date. */
1393 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1395 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1398 /* Verify birthtime of 'pathname'. */
1400 assertion_file_birthtime(const char *file, int line,
1401 const char *pathname, long t, long nsec)
1403 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1406 /* Verify birthtime of 'pathname' is up-to-date. */
1408 assertion_file_birthtime_recent(const char *file, int line,
1409 const char *pathname)
1411 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1414 /* Verify mode of 'pathname'. */
1416 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1421 assertion_count(file, line);
1422 #if defined(_WIN32) && !defined(__CYGWIN__)
1423 failure_start(file, line, "assertFileMode not yet implemented for Windows");
1424 (void)mode; /* UNUSED */
1425 (void)r; /* UNUSED */
1426 (void)pathname; /* UNUSED */
1427 (void)expected_mode; /* UNUSED */
1431 r = lstat(pathname, &st);
1432 mode = (int)(st.st_mode & 0777);
1434 if (r == 0 && mode == expected_mode)
1436 failure_start(file, line, "File %s has mode %o, expected %o",
1437 pathname, mode, expected_mode);
1439 failure_finish(NULL);
1443 /* Verify mtime of 'pathname'. */
1445 assertion_file_mtime(const char *file, int line,
1446 const char *pathname, long t, long nsec)
1448 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1451 /* Verify mtime of 'pathname' is up-to-date. */
1453 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1455 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1458 /* Verify number of links to 'pathname'. */
1460 assertion_file_nlinks(const char *file, int line,
1461 const char *pathname, int nlinks)
1463 #if defined(_WIN32) && !defined(__CYGWIN__)
1464 BY_HANDLE_FILE_INFORMATION bhfi;
1467 assertion_count(file, line);
1468 r = my_GetFileInformationByName(pathname, &bhfi);
1469 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1471 failure_start(file, line, "File %s has %d links, expected %d",
1472 pathname, bhfi.nNumberOfLinks, nlinks);
1473 failure_finish(NULL);
1479 assertion_count(file, line);
1480 r = lstat(pathname, &st);
1481 if (r == 0 && (int)st.st_nlink == nlinks)
1483 failure_start(file, line, "File %s has %d links, expected %d",
1484 pathname, st.st_nlink, nlinks);
1485 failure_finish(NULL);
1490 /* Verify size of 'pathname'. */
1492 assertion_file_size(const char *file, int line, const char *pathname, long size)
1497 assertion_count(file, line);
1498 #if defined(_WIN32) && !defined(__CYGWIN__)
1500 BY_HANDLE_FILE_INFORMATION bhfi;
1501 r = !my_GetFileInformationByName(pathname, &bhfi);
1502 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1507 r = lstat(pathname, &st);
1508 filesize = st.st_size;
1511 if (r == 0 && filesize == size)
1513 failure_start(file, line, "File %s has size %ld, expected %ld",
1514 pathname, (long)filesize, (long)size);
1515 failure_finish(NULL);
1519 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1521 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1526 #if defined(_WIN32) && !defined(__CYGWIN__)
1527 (void)mode; /* UNUSED */
1529 assertion_count(file, line);
1530 r = lstat(pathname, &st);
1532 failure_start(file, line, "Dir should exist: %s", pathname);
1533 failure_finish(NULL);
1536 if (!S_ISDIR(st.st_mode)) {
1537 failure_start(file, line, "%s is not a dir", pathname);
1538 failure_finish(NULL);
1541 #if !defined(_WIN32) || defined(__CYGWIN__)
1542 /* Windows doesn't handle permissions the same way as POSIX,
1543 * so just ignore the mode tests. */
1544 /* TODO: Can we do better here? */
1545 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1546 failure_start(file, line, "Dir %s has wrong mode", pathname);
1547 logprintf(" Expected: 0%3o\n", mode);
1548 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1549 failure_finish(NULL);
1556 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1557 * verify that too. */
1559 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1564 #if defined(_WIN32) && !defined(__CYGWIN__)
1565 (void)mode; /* UNUSED */
1567 assertion_count(file, line);
1568 r = lstat(pathname, &st);
1569 if (r != 0 || !S_ISREG(st.st_mode)) {
1570 failure_start(file, line, "File should exist: %s", pathname);
1571 failure_finish(NULL);
1574 #if !defined(_WIN32) || defined(__CYGWIN__)
1575 /* Windows doesn't handle permissions the same way as POSIX,
1576 * so just ignore the mode tests. */
1577 /* TODO: Can we do better here? */
1578 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1579 failure_start(file, line, "File %s has wrong mode", pathname);
1580 logprintf(" Expected: 0%3o\n", mode);
1581 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1582 failure_finish(NULL);
1589 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1590 * non-NULL, verify that the symlink has those contents. */
1592 is_symlink(const char *file, int line,
1593 const char *pathname, const char *contents)
1595 #if defined(_WIN32) && !defined(__CYGWIN__)
1596 (void)pathname; /* UNUSED */
1597 (void)contents; /* UNUSED */
1598 assertion_count(file, line);
1599 /* Windows sort-of has real symlinks, but they're only usable
1600 * by privileged users and are crippled even then, so there's
1601 * really not much point in bothering with this. */
1609 assertion_count(file, line);
1610 r = lstat(pathname, &st);
1612 failure_start(file, line,
1613 "Symlink should exist: %s", pathname);
1614 failure_finish(NULL);
1617 if (!S_ISLNK(st.st_mode))
1619 if (contents == NULL)
1621 linklen = readlink(pathname, buff, sizeof(buff));
1623 failure_start(file, line, "Can't read symlink %s", pathname);
1624 failure_finish(NULL);
1627 buff[linklen] = '\0';
1628 if (strcmp(buff, contents) != 0)
1634 /* Assert that path is a symlink that (optionally) contains contents. */
1636 assertion_is_symlink(const char *file, int line,
1637 const char *path, const char *contents)
1639 if (is_symlink(file, line, path, contents))
1642 failure_start(file, line, "File %s is not a symlink to %s",
1645 failure_start(file, line, "File %s is not a symlink", path);
1646 failure_finish(NULL);
1651 /* Create a directory and report any errors. */
1653 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1655 assertion_count(file, line);
1656 #if defined(_WIN32) && !defined(__CYGWIN__)
1657 (void)mode; /* UNUSED */
1658 if (0 == _mkdir(dirname))
1661 if (0 == mkdir(dirname, mode)) {
1662 if (0 == chmod(dirname, mode)) {
1663 assertion_file_mode(file, line, dirname, mode);
1668 failure_start(file, line, "Could not create directory %s", dirname);
1669 failure_finish(NULL);
1673 /* Create a file with the specified contents and report any failures. */
1675 assertion_make_file(const char *file, int line,
1676 const char *path, int mode, int csize, const void *contents)
1678 #if defined(_WIN32) && !defined(__CYGWIN__)
1679 /* TODO: Rework this to set file mode as well. */
1681 (void)mode; /* UNUSED */
1682 assertion_count(file, line);
1683 f = fopen(path, "wb");
1685 failure_start(file, line, "Could not create file %s", path);
1686 failure_finish(NULL);
1689 if (contents != NULL) {
1693 wsize = strlen(contents);
1695 wsize = (size_t)csize;
1696 if (wsize != fwrite(contents, 1, wsize, f)) {
1698 failure_start(file, line,
1699 "Could not write file %s", path);
1700 failure_finish(NULL);
1708 assertion_count(file, line);
1709 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1711 failure_start(file, line, "Could not create %s", path);
1712 failure_finish(NULL);
1715 if (0 != chmod(path, mode)) {
1716 failure_start(file, line, "Could not chmod %s", path);
1717 failure_finish(NULL);
1721 if (contents != NULL) {
1725 wsize = (ssize_t)strlen(contents);
1727 wsize = (ssize_t)csize;
1728 if (wsize != write(fd, contents, wsize)) {
1730 failure_start(file, line,
1731 "Could not write to %s", path);
1732 failure_finish(NULL);
1738 assertion_file_mode(file, line, path, mode);
1743 /* Create a hardlink and report any failures. */
1745 assertion_make_hardlink(const char *file, int line,
1746 const char *newpath, const char *linkto)
1750 assertion_count(file, line);
1751 #if defined(_WIN32) && !defined(__CYGWIN__)
1752 succeeded = my_CreateHardLinkA(newpath, linkto);
1754 succeeded = !link(linkto, newpath);
1760 failure_start(file, line, "Could not create hardlink");
1761 logprintf(" New link: %s\n", newpath);
1762 logprintf(" Old name: %s\n", linkto);
1763 failure_finish(NULL);
1767 /* Create a symlink and report any failures. */
1769 assertion_make_symlink(const char *file, int line,
1770 const char *newpath, const char *linkto)
1772 #if defined(_WIN32) && !defined(__CYGWIN__)
1773 int targetIsDir = 0; /* TODO: Fix this */
1774 assertion_count(file, line);
1775 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1778 assertion_count(file, line);
1779 if (0 == symlink(linkto, newpath))
1782 failure_start(file, line, "Could not create symlink");
1783 logprintf(" New link: %s\n", newpath);
1784 logprintf(" Old name: %s\n", linkto);
1785 failure_finish(NULL);
1789 /* Set umask, report failures. */
1791 assertion_umask(const char *file, int line, int mask)
1793 assertion_count(file, line);
1794 (void)file; /* UNUSED */
1795 (void)line; /* UNUSED */
1800 /* Set times, report failures. */
1802 assertion_utimes(const char *file, int line,
1803 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1807 #if defined(_WIN32) && !defined(__CYGWIN__)
1808 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1809 + (((nsec)/1000)*10))
1811 ULARGE_INTEGER wintm;
1812 FILETIME fatime, fmtime;
1813 FILETIME *pat, *pmt;
1815 assertion_count(file, line);
1816 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1817 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1818 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1819 if (h == INVALID_HANDLE_VALUE) {
1820 failure_start(file, line, "Can't access %s\n", pathname);
1821 failure_finish(NULL);
1825 if (at > 0 || at_nsec > 0) {
1826 wintm.QuadPart = WINTIME(at, at_nsec);
1827 fatime.dwLowDateTime = wintm.LowPart;
1828 fatime.dwHighDateTime = wintm.HighPart;
1832 if (mt > 0 || mt_nsec > 0) {
1833 wintm.QuadPart = WINTIME(mt, mt_nsec);
1834 fmtime.dwLowDateTime = wintm.LowPart;
1835 fmtime.dwHighDateTime = wintm.HighPart;
1839 if (pat != NULL || pmt != NULL)
1840 r = SetFileTime(h, NULL, pat, pmt);
1845 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1846 failure_finish(NULL);
1850 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1852 struct timeval times[2];
1854 #if !defined(__FreeBSD__)
1855 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1857 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1860 r = lstat(pathname, &st);
1862 failure_start(file, line, "Can't stat %s\n", pathname);
1863 failure_finish(NULL);
1867 if (mt == 0 && mt_nsec == 0) {
1869 #if defined(__FreeBSD__)
1870 mt_nsec = st.st_mtimespec.tv_nsec;
1871 /* FreeBSD generally only stores to microsecond res, so round. */
1872 mt_nsec = (mt_nsec / 1000) * 1000;
1875 if (at == 0 && at_nsec == 0) {
1877 #if defined(__FreeBSD__)
1878 at_nsec = st.st_atimespec.tv_nsec;
1879 /* FreeBSD generally only stores to microsecond res, so round. */
1880 at_nsec = (at_nsec / 1000) * 1000;
1884 times[1].tv_sec = mt;
1885 times[1].tv_usec = mt_nsec / 1000;
1887 times[0].tv_sec = at;
1888 times[0].tv_usec = at_nsec / 1000;
1891 r = lutimes(pathname, times);
1893 r = utimes(pathname, times);
1896 failure_start(file, line, "Can't utimes %s\n", pathname);
1897 failure_finish(NULL);
1901 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1904 /* Compare file flags */
1906 assertion_compare_fflags(const char *file, int line, const char *patha,
1907 const char *pathb, int nomatch)
1909 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1912 assertion_count(file, line);
1914 if (stat(patha, &sa) < 0)
1916 if (stat(pathb, &sb) < 0)
1918 if (!nomatch && sa.st_flags != sb.st_flags) {
1919 failure_start(file, line, "File flags should be identical: "
1920 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
1922 failure_finish(NULL);
1925 if (nomatch && sa.st_flags == sb.st_flags) {
1926 failure_start(file, line, "File flags should be different: "
1927 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
1929 failure_finish(NULL);
1932 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
1933 defined(FS_NODUMP_FL)) || \
1934 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
1935 && defined(EXT2_NODUMP_FL))
1936 int fd, r, flagsa, flagsb;
1938 assertion_count(file, line);
1939 fd = open(patha, O_RDONLY | O_NONBLOCK);
1941 failure_start(file, line, "Can't open %s\n", patha);
1942 failure_finish(NULL);
1946 #ifdef FS_IOC_GETFLAGS
1954 failure_start(file, line, "Can't get flags %s\n", patha);
1955 failure_finish(NULL);
1958 fd = open(pathb, O_RDONLY | O_NONBLOCK);
1960 failure_start(file, line, "Can't open %s\n", pathb);
1961 failure_finish(NULL);
1965 #ifdef FS_IOC_GETFLAGS
1973 failure_start(file, line, "Can't get flags %s\n", pathb);
1974 failure_finish(NULL);
1977 if (!nomatch && flagsa != flagsb) {
1978 failure_start(file, line, "File flags should be identical: "
1979 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
1980 failure_finish(NULL);
1983 if (nomatch && flagsa == flagsb) {
1984 failure_start(file, line, "File flags should be different: "
1985 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
1986 failure_finish(NULL);
1990 (void)patha; /* UNUSED */
1991 (void)pathb; /* UNUSED */
1992 (void)nomatch; /* UNUSED */
1993 assertion_count(file, line);
1998 /* Set nodump, report failures. */
2000 assertion_set_nodump(const char *file, int line, const char *pathname)
2002 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2005 assertion_count(file, line);
2006 r = chflags(pathname, UF_NODUMP);
2008 failure_start(file, line, "Can't set nodump %s\n", pathname);
2009 failure_finish(NULL);
2012 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
2013 defined(FS_NODUMP_FL)) || \
2014 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2015 && defined(EXT2_NODUMP_FL))
2018 assertion_count(file, line);
2019 fd = open(pathname, O_RDONLY | O_NONBLOCK);
2021 failure_start(file, line, "Can't open %s\n", pathname);
2022 failure_finish(NULL);
2026 #ifdef FS_IOC_GETFLAGS
2033 failure_start(file, line, "Can't get flags %s\n", pathname);
2034 failure_finish(NULL);
2038 flags |= FS_NODUMP_FL;
2040 flags |= EXT2_NODUMP_FL;
2044 #ifdef FS_IOC_SETFLAGS
2051 failure_start(file, line, "Can't set nodump %s\n", pathname);
2052 failure_finish(NULL);
2057 (void)pathname; /* UNUSED */
2058 assertion_count(file, line);
2064 static void assert_version_id(char **qq, size_t *ss)
2069 /* Version number is a series of digits and periods. */
2070 while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
2075 if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') {
2080 /* Skip a single trailing a,b,c, or d. */
2081 if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
2084 /* Version number terminated by space. */
2085 failure("No space after version: ``%s''", q);
2087 failure("No space after version: ``%s''", q);
2098 * Check program version
2100 void assertVersion(const char *prog, const char *base)
2105 unsigned int prog_len = strlen(base);
2107 r = systemf("%s --version >version.stdout 2>version.stderr", prog);
2109 r = systemf("%s -W version >version.stdout 2>version.stderr",
2112 failure("Unable to run either %s --version or %s -W version",
2114 if (!assert(r == 0))
2117 /* --version should generate nothing to stdout. */
2118 assertEmptyFile("version.stderr");
2120 /* Verify format of version message. */
2121 q = p = slurpfile(&s, "version.stdout");
2123 /* Version message should start with name of program, then space. */
2124 assert(s > prog_len + 1);
2126 failure("Version must start with '%s': ``%s''", base, p);
2127 if (!assertEqualMem(q, base, prog_len)) {
2132 q += prog_len; s -= prog_len;
2137 assert_version_id(&q, &s);
2140 failure("No `-' between program name and versions: ``%s''", p);
2141 assertEqualMem(q, "- ", 2);
2144 failure("Not long enough for libarchive version: ``%s''", p);
2147 failure("Libarchive version must start with `libarchive': ``%s''", p);
2148 assertEqualMem(q, "libarchive ", 11);
2152 assert_version_id(&q, &s);
2154 /* Skip arbitrary third-party version numbers. */
2155 while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' ||
2161 /* All terminated by end-of-line. */
2164 /* Skip an optional CR character (e.g., Windows) */
2165 failure("Version output must end with \\n or \\r\\n");
2167 if (*q == '\r') { ++q; --s; }
2168 assertEqualMem(q, "\n", 1);
2172 #endif /* PROGRAM */
2176 * UTILITIES for use by tests.
2181 * Check whether platform supports symlinks. This is intended
2182 * for tests to use in deciding whether to bother testing symlink
2183 * support; if the platform doesn't support symlinks, there's no point
2184 * in checking whether the program being tested can create them.
2186 * Note that the first time this test is called, we actually go out to
2187 * disk to create and verify a symlink. This is necessary because
2188 * symlink support is actually a property of a particular filesystem
2189 * and can thus vary between directories on a single system. After
2190 * the first call, this returns the cached result from memory, so it's
2191 * safe to call it as often as you wish.
2196 /* Remember the test result */
2197 static int value = 0, tested = 0;
2202 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
2203 /* Note: Cygwin has its own symlink() emulation that does not
2204 * use the Win32 CreateSymbolicLink() function. */
2205 #if defined(_WIN32) && !defined(__CYGWIN__)
2206 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
2207 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
2209 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
2210 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
2215 /* Platform-dependent options for hiding the output of a subcommand. */
2216 #if defined(_WIN32) && !defined(__CYGWIN__)
2217 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
2219 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
2222 * Can this platform run the bzip2 program?
2227 static int tested = 0, value = 0;
2230 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
2237 * Can this platform run the grzip program?
2242 static int tested = 0, value = 0;
2245 if (systemf("grzip -V %s", redirectArgs) == 0)
2252 * Can this platform run the gzip program?
2257 static int tested = 0, value = 0;
2260 if (systemf("gzip -V %s", redirectArgs) == 0)
2267 * Can this platform run the lrzip program?
2270 canRunCommand(const char *cmd)
2272 static int tested = 0, value = 0;
2275 if (systemf("%s %s", cmd, redirectArgs) == 0)
2284 static int tested = 0, value = 0;
2287 if (systemf("lrzip -V %s", redirectArgs) == 0)
2294 * Can this platform run the lz4 program?
2299 static int tested = 0, value = 0;
2302 if (systemf("lz4 -V %s", redirectArgs) == 0)
2309 * Can this platform run the lzip program?
2314 static int tested = 0, value = 0;
2317 if (systemf("lzip -V %s", redirectArgs) == 0)
2324 * Can this platform run the lzma program?
2329 static int tested = 0, value = 0;
2332 if (systemf("lzma -V %s", redirectArgs) == 0)
2339 * Can this platform run the lzop program?
2344 static int tested = 0, value = 0;
2347 if (systemf("lzop -V %s", redirectArgs) == 0)
2354 * Can this platform run the xz program?
2359 static int tested = 0, value = 0;
2362 if (systemf("xz -V %s", redirectArgs) == 0)
2369 * Can this filesystem handle nodump flags.
2374 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2375 const char *path = "cannodumptest";
2378 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2379 if (chflags(path, UF_NODUMP) < 0)
2381 if (stat(path, &sb) < 0)
2383 if (sb.st_flags & UF_NODUMP)
2385 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \
2386 && defined(FS_NODUMP_FL)) || \
2387 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2388 && defined(EXT2_NODUMP_FL))
2389 const char *path = "cannodumptest";
2392 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2393 fd = open(path, O_RDONLY | O_NONBLOCK);
2397 #ifdef FS_IOC_GETFLAGS
2406 flags |= FS_NODUMP_FL;
2408 flags |= EXT2_NODUMP_FL;
2411 #ifdef FS_IOC_SETFLAGS
2420 fd = open(path, O_RDONLY | O_NONBLOCK);
2424 #ifdef FS_IOC_GETFLAGS
2434 if (flags & FS_NODUMP_FL)
2436 if (flags & EXT2_NODUMP_FL)
2443 #if ARCHIVE_ACL_SUNOS
2444 /* Fetch ACLs on Solaris using acl() or facl() */
2446 sunacl_get(int cmd, int *aclcnt, int fd, const char *path)
2452 if (cmd == GETACL) {
2454 size = sizeof(aclent_t);
2456 #if ARCHIVE_ACL_SUNOS_NFS4
2457 else if (cmd == ACE_GETACL) {
2458 cntcmd = ACE_GETACLCNT;
2459 size = sizeof(ace_t);
2470 while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) {
2472 cnt = acl(path, cntcmd, 0, NULL);
2474 cnt = facl(fd, cntcmd, 0, NULL);
2478 aclp = malloc(cnt * size);
2480 aclp = realloc(NULL, cnt * size);
2483 cnt = acl(path, cmd, cnt, aclp);
2485 cnt = facl(fd, cmd, cnt, aclp);
2499 #endif /* ARCHIVE_ACL_SUNOS */
2502 * Set test ACLs on a path
2504 * 0: error setting ACLs
2505 * ARCHIVE_TEST_ACL_TYPE_POSIX1E: POSIX.1E ACLs have been set
2506 * ARCHIVE_TEST_ACL_TYPE_NFS4: NFSv4 or extended ACLs have been set
2509 setTestAcl(const char *path)
2511 #if ARCHIVE_ACL_SUPPORT
2513 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_DARWIN
2516 #if ARCHIVE_ACL_LIBRICHACL
2517 struct richacl *richacl;
2519 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD
2520 const char *acltext_posix1e = "user:1:rw-,"
2526 #elif ARCHIVE_ACL_SUNOS /* Solaris POSIX.1e */
2527 aclent_t aclp_posix1e[] = {
2528 { USER_OBJ, -1, 4 | 2 | 1 },
2530 { GROUP_OBJ, -1, 4 | 2 | 1 },
2531 { GROUP, 15, 4 | 1 },
2532 { CLASS_OBJ, -1, 4 | 2 | 1 },
2533 { OTHER_OBJ, -1, 4 | 2 | 1 }
2536 #if ARCHIVE_ACL_FREEBSD /* FreeBSD NFS4 */
2537 const char *acltext_nfs4 = "user:1:rwpaRcs::allow:1,"
2538 "group:15:rxaRcs::allow:15,"
2539 "owner@:rwpxaARWcCos::allow,"
2540 "group@:rwpxaRcs::allow,"
2541 "everyone@:rxaRcs::allow";
2542 #elif ARCHIVE_ACL_LIBRICHACL
2543 const char *acltext_nfs4 = "owner:rwpxaARWcCoS::mask,"
2544 "group:rwpxaRcS::mask,"
2545 "other:rxaRcS::mask,"
2546 "user:1:rwpaRcS::allow,"
2547 "group:15:rxaRcS::allow,"
2548 "owner@:rwpxaARWcCoS::allow,"
2549 "group@:rwpxaRcS::allow,"
2550 "everyone@:rxaRcS::allow";
2551 #elif ARCHIVE_ACL_SUNOS_NFS4 /* Solaris NFS4 */
2552 ace_t aclp_nfs4[] = {
2553 { 1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2554 ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | ACE_READ_ACL |
2555 ACE_SYNCHRONIZE, 0, ACE_ACCESS_ALLOWED_ACE_TYPE },
2556 { 15, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2557 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2558 ACE_IDENTIFIER_GROUP, ACE_ACCESS_ALLOWED_ACE_TYPE },
2559 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2560 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_WRITE_ATTRIBUTES |
2561 ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS |
2562 ACE_READ_ACL | ACE_WRITE_ACL | ACE_WRITE_OWNER | ACE_SYNCHRONIZE,
2563 ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE },
2564 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2565 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
2566 ACE_READ_ACL | ACE_SYNCHRONIZE, ACE_GROUP | ACE_IDENTIFIER_GROUP,
2567 ACE_ACCESS_ALLOWED_ACE_TYPE },
2568 { -1, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2569 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2570 ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE }
2572 #elif ARCHIVE_ACL_DARWIN /* Mac OS X */
2574 acl_permset_t permset;
2575 const uid_t uid = 1;
2578 const acl_perm_t acl_perms[] = {
2583 ACL_READ_ATTRIBUTES,
2584 ACL_READ_EXTATTRIBUTES,
2586 #if HAVE_DECL_ACL_SYNCHRONIZE
2590 #endif /* ARCHIVE_ACL_DARWIN */
2592 #if ARCHIVE_ACL_FREEBSD
2593 acl = acl_from_text(acltext_nfs4);
2594 failure("acl_from_text() error: %s", strerror(errno));
2595 if (assert(acl != NULL) == 0)
2597 #elif ARCHIVE_ACL_LIBRICHACL
2598 richacl = richacl_from_text(acltext_nfs4, NULL, NULL);
2599 failure("richacl_from_text() error: %s", strerror(errno));
2600 if (assert(richacl != NULL) == 0)
2602 #elif ARCHIVE_ACL_DARWIN
2604 failure("acl_init() error: %s", strerror(errno));
2605 if (assert(acl != NULL) == 0)
2607 r = acl_create_entry(&acl, &aclent);
2608 failure("acl_create_entry() error: %s", strerror(errno));
2609 if (assertEqualInt(r, 0) == 0)
2611 r = acl_set_tag_type(aclent, ACL_EXTENDED_ALLOW);
2612 failure("acl_set_tag_type() error: %s", strerror(errno));
2613 if (assertEqualInt(r, 0) == 0)
2615 r = acl_get_permset(aclent, &permset);
2616 failure("acl_get_permset() error: %s", strerror(errno));
2617 if (assertEqualInt(r, 0) == 0)
2619 for (i = 0; i < (int)(sizeof(acl_perms) / sizeof(acl_perms[0])); i++) {
2620 r = acl_add_perm(permset, acl_perms[i]);
2621 failure("acl_add_perm() error: %s", strerror(errno));
2622 if (assertEqualInt(r, 0) == 0)
2625 r = acl_set_permset(aclent, permset);
2626 failure("acl_set_permset() error: %s", strerror(errno));
2627 if (assertEqualInt(r, 0) == 0)
2629 r = mbr_uid_to_uuid(uid, uuid);
2630 failure("mbr_uid_to_uuid() error: %s", strerror(errno));
2631 if (assertEqualInt(r, 0) == 0)
2633 r = acl_set_qualifier(aclent, uuid);
2634 failure("acl_set_qualifier() error: %s", strerror(errno));
2635 if (assertEqualInt(r, 0) == 0)
2637 #endif /* ARCHIVE_ACL_DARWIN */
2639 #if ARCHIVE_ACL_NFS4
2640 #if ARCHIVE_ACL_FREEBSD
2641 r = acl_set_file(path, ACL_TYPE_NFS4, acl);
2643 #elif ARCHIVE_ACL_LIBRICHACL
2644 r = richacl_set_file(path, richacl);
2645 richacl_free(richacl);
2646 #elif ARCHIVE_ACL_SUNOS_NFS4
2647 r = acl(path, ACE_SETACL,
2648 (int)(sizeof(aclp_nfs4)/sizeof(aclp_nfs4[0])), aclp_nfs4);
2649 #elif ARCHIVE_ACL_DARWIN
2650 r = acl_set_file(path, ACL_TYPE_EXTENDED, acl);
2654 return (ARCHIVE_TEST_ACL_TYPE_NFS4);
2655 #endif /* ARCHIVE_ACL_NFS4 */
2657 #if ARCHIVE_ACL_POSIX1E
2658 #if ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_LIBACL
2659 acl = acl_from_text(acltext_posix1e);
2660 failure("acl_from_text() error: %s", strerror(errno));
2661 if (assert(acl != NULL) == 0)
2664 r = acl_set_file(path, ACL_TYPE_ACCESS, acl);
2666 #elif ARCHIVE_ACL_SUNOS
2667 r = acl(path, SETACL,
2668 (int)(sizeof(aclp_posix1e)/sizeof(aclp_posix1e[0])), aclp_posix1e);
2671 return (ARCHIVE_TEST_ACL_TYPE_POSIX1E);
2674 #endif /* ARCHIVE_ACL_POSIX1E */
2675 #if ARCHIVE_ACL_DARWIN
2679 #endif /* ARCHIVE_ACL_SUPPORT */
2680 (void)path; /* UNUSED */
2685 * Sleep as needed; useful for verifying disk timestamp changes by
2686 * ensuring that the wall-clock time has actually changed before we
2687 * go back to re-read something from disk.
2690 sleepUntilAfter(time_t t)
2692 while (t >= time(NULL))
2693 #if defined(_WIN32) && !defined(__CYGWIN__)
2701 * Call standard system() call, but build up the command line using
2702 * sprintf() conventions.
2705 systemf(const char *fmt, ...)
2712 vsprintf(buff, fmt, ap);
2713 if (verbosity > VERBOSITY_FULL)
2714 logprintf("Cmd: %s\n", buff);
2721 * Slurp a file into memory for ease of comparison and testing.
2722 * Returns size of file in 'sizep' if non-NULL, null-terminates
2723 * data in memory for ease of use.
2726 slurpfile(size_t * sizep, const char *fmt, ...)
2728 char filename[8192];
2737 vsprintf(filename, fmt, ap);
2740 f = fopen(filename, "rb");
2742 /* Note: No error; non-existent file is okay here. */
2745 r = fstat(fileno(f), &st);
2747 logprintf("Can't stat file %s\n", filename);
2751 p = malloc((size_t)st.st_size + 1);
2753 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2754 (long int)st.st_size, filename);
2758 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2759 if (bytes_read < st.st_size) {
2760 logprintf("Can't read file %s\n", filename);
2765 p[st.st_size] = '\0';
2767 *sizep = (size_t)st.st_size;
2773 * Slurp a file into memory for ease of comparison and testing.
2774 * Returns size of file in 'sizep' if non-NULL, null-terminates
2775 * data in memory for ease of use.
2778 dumpfile(const char *filename, void *data, size_t len)
2780 ssize_t bytes_written;
2783 f = fopen(filename, "wb");
2785 logprintf("Can't open file %s for writing\n", filename);
2788 bytes_written = fwrite(data, 1, len, f);
2789 if (bytes_written < (ssize_t)len)
2790 logprintf("Can't write file %s\n", filename);
2794 /* Read a uuencoded file from the reference directory, decode, and
2795 * write the result into the current directory. */
2796 #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2797 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2799 extract_reference_file(const char *name)
2804 sprintf(buff, "%s/%s.uu", refdir, name);
2805 in = fopen(buff, "r");
2806 failure("Couldn't open reference file %s", buff);
2810 /* Read up to and including the 'begin' line. */
2812 if (fgets(buff, sizeof(buff), in) == NULL) {
2813 /* TODO: This is a failure. */
2816 if (memcmp(buff, "begin ", 6) == 0)
2819 /* Now, decode the rest and write it. */
2820 out = fopen(name, "wb");
2821 while (fgets(buff, sizeof(buff), in) != NULL) {
2825 if (memcmp(buff, "end", 3) == 0)
2828 bytes = UUDECODE(*p++);
2831 /* Write out 1-3 bytes from that. */
2833 assert(VALID_UUDECODE(p[0]));
2834 assert(VALID_UUDECODE(p[1]));
2835 n = UUDECODE(*p++) << 18;
2836 n |= UUDECODE(*p++) << 12;
2837 fputc(n >> 16, out);
2841 assert(VALID_UUDECODE(p[0]));
2842 n |= UUDECODE(*p++) << 6;
2843 fputc((n >> 8) & 0xFF, out);
2847 assert(VALID_UUDECODE(p[0]));
2848 n |= UUDECODE(*p++);
2849 fputc(n & 0xFF, out);
2859 copy_reference_file(const char *name)
2865 sprintf(buff, "%s/%s", refdir, name);
2866 in = fopen(buff, "rb");
2867 failure("Couldn't open reference file %s", buff);
2871 /* Now, decode the rest and write it. */
2872 /* Not a lot of error checking here; the input better be right. */
2873 out = fopen(name, "wb");
2874 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2875 if (fwrite(buff, 1, rbytes, out) != rbytes) {
2876 logprintf("Error: fwrite\n");
2885 is_LargeInode(const char *file)
2887 #if defined(_WIN32) && !defined(__CYGWIN__)
2888 BY_HANDLE_FILE_INFORMATION bhfi;
2891 r = my_GetFileInformationByName(file, &bhfi);
2894 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2899 if (stat(file, &st) < 0)
2901 ino = (int64_t)st.st_ino;
2902 return (ino > 0xffffffff);
2907 extract_reference_files(const char **names)
2909 while (names && *names)
2910 extract_reference_file(*names++);
2916 assertion_entry_set_acls(const char *file, int line, struct archive_entry *ae,
2917 struct archive_test_acl_t *acls, int n)
2921 assertion_count(file, line);
2924 archive_entry_acl_clear(ae);
2925 for (i = 0; i < n; i++) {
2926 r = archive_entry_acl_add_entry(ae,
2927 acls[i].type, acls[i].permset, acls[i].tag,
2928 acls[i].qual, acls[i].name);
2931 failure_start(file, line, "type=%#010x, ",
2932 "permset=%#010x, tag=%d, qual=%d name=%s",
2933 acls[i].type, acls[i].permset, acls[i].tag,
2934 acls[i].qual, acls[i].name);
2935 failure_finish(NULL);
2943 archive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset,
2944 int tag, int qual, const char *name)
2946 if (type != acl->type)
2948 if (permset != acl->permset)
2950 if (tag != acl->tag)
2952 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
2954 if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
2956 if (tag == ARCHIVE_ENTRY_ACL_EVERYONE)
2958 if (tag == ARCHIVE_ENTRY_ACL_OTHER)
2960 if (qual != acl->qual)
2963 if (acl->name == NULL || acl->name[0] == '\0')
2967 if (acl->name == NULL) {
2968 if (name[0] == '\0')
2972 return (0 == strcmp(name, acl->name));
2977 assertion_entry_compare_acls(const char *file, int line,
2978 struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt,
2979 int want_type, int mode)
2983 int type, permset, tag, qual;
2987 assertion_count(file, line);
2991 marker = malloc(sizeof(marker[0]) * cnt);
2993 for (i = 0; i < cnt; i++) {
2994 if ((acls[i].type & want_type) != 0) {
3001 failure_start(file, line, "No ACL's to compare, type mask: %d",
3006 while (0 == (r = archive_entry_acl_next(ae, want_type,
3007 &type, &permset, &tag, &qual, &name))) {
3008 for (i = 0, matched = 0; i < n && !matched; i++) {
3009 if (archive_test_acl_match(&acls[marker[i]], type,
3010 permset, tag, qual, name)) {
3011 /* We found a match; remove it. */
3012 marker[i] = marker[n - 1];
3017 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3018 && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
3020 failure_start(file, line, "No match for "
3022 failure_finish(NULL);
3025 if ((permset << 6) != (mode & 0700)) {
3026 failure_start(file, line, "USER_OBJ permset "
3027 "(%02o) != user mode (%02o)", permset,
3029 failure_finish(NULL);
3032 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3033 && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
3035 failure_start(file, line, "No match for "
3037 failure_finish(NULL);
3040 if ((permset << 3) != (mode & 0070)) {
3041 failure_start(file, line, "GROUP_OBJ permset "
3042 "(%02o) != group mode (%02o)", permset,
3044 failure_finish(NULL);
3047 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3048 && tag == ARCHIVE_ENTRY_ACL_OTHER) {
3050 failure_start(file, line, "No match for "
3052 failure_finish(NULL);
3055 if ((permset << 0) != (mode & 0007)) {
3056 failure_start(file, line, "OTHER permset "
3057 "(%02o) != other mode (%02o)", permset,
3059 failure_finish(NULL);
3062 } else if (matched != 1) {
3063 failure_start(file, line, "Could not find match for "
3064 "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d,"
3065 "name=``%s'')", type, permset, tag, qual, name);
3066 failure_finish(NULL);
3070 if (r != ARCHIVE_EOF) {
3071 failure_start(file, line, "Should not exit before EOF");
3072 failure_finish(NULL);
3075 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 &&
3076 (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) {
3077 failure_start(file, line, "Mode (%02o) and entry mode (%02o) "
3078 "mismatch", mode, archive_entry_mode(ae));
3079 failure_finish(NULL);
3083 failure_start(file, line, "Could not find match for ACL "
3084 "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')",
3085 acls[marker[0]].type, acls[marker[0]].permset,
3086 acls[marker[0]].tag, acls[marker[0]].qual,
3087 acls[marker[0]].name);
3088 failure_finish(NULL);
3090 /* Number of ACLs not matched should == 0 */
3095 #endif /* !defined(PROGRAM) */
3104 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
3106 * DEFINE_TEST(test_function)
3110 /* Use "list.h" to declare all of the test functions. */
3112 #define DEFINE_TEST(name) void name(void);
3115 /* Use "list.h" to create a list of all tests (functions and names). */
3117 #define DEFINE_TEST(n) { n, #n, 0 },
3118 struct test_list_t tests[] = {
3123 * Summarize repeated failures in the just-completed test.
3126 test_summarize(int failed, int skips_num)
3130 switch (verbosity) {
3131 case VERBOSITY_SUMMARY_ONLY:
3132 printf(failed ? "E" : ".");
3135 case VERBOSITY_PASSFAIL:
3136 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
3140 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
3142 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
3143 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
3144 logprintf("%s:%d: Summary: Failed %d times\n",
3145 failed_filename, i, failed_lines[i].count);
3147 /* Clear the failure history for the next file. */
3148 failed_filename = NULL;
3149 memset(failed_lines, 0, sizeof(failed_lines));
3153 * Actually run a single test, with appropriate setup and cleanup.
3156 test_run(int i, const char *tmpdir)
3159 char logfilename[64];
3160 int failures_before = failures;
3161 int skips_before = skips;
3164 switch (verbosity) {
3165 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
3167 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
3168 printf("%3d: %-64s", i, tests[i].name);
3171 default: /* Title of test, details will follow */
3172 printf("%3d: %s\n", i, tests[i].name);
3175 /* Chdir to the top-level work directory. */
3176 if (!assertChdir(tmpdir)) {
3178 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
3181 /* Create a log file for this test. */
3182 sprintf(logfilename, "%s.log", tests[i].name);
3183 logfile = fopen(logfilename, "w");
3184 fprintf(logfile, "%s\n\n", tests[i].name);
3185 /* Chdir() to a work dir for this specific test. */
3186 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
3187 testworkdir = workdir;
3188 if (!assertMakeDir(testworkdir, 0755)
3189 || !assertChdir(testworkdir)) {
3191 "ERROR: Can't chdir to work dir %s\n", testworkdir);
3194 /* Explicitly reset the locale before each test. */
3195 setlocale(LC_ALL, "C");
3196 /* Record the umask before we run the test. */
3197 umask(oldumask = umask(0));
3199 * Run the actual test.
3203 * Clean up and report afterwards.
3209 setlocale(LC_ALL, "C");
3210 /* Reset directory. */
3211 if (!assertChdir(tmpdir)) {
3212 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
3216 /* Report per-test summaries. */
3217 tests[i].failures = failures - failures_before;
3218 test_summarize(tests[i].failures, skips - skips_before);
3219 /* Close the per-test log file. */
3222 /* If there were no failures, we can remove the work dir and logfile. */
3223 if (tests[i].failures == 0) {
3224 if (!keep_temp_files && assertChdir(tmpdir)) {
3225 #if defined(_WIN32) && !defined(__CYGWIN__)
3226 /* Make sure not to leave empty directories.
3227 * Sometimes a processing of closing files used by tests
3228 * is not done, then rmdir will be failed and it will
3229 * leave a empty test directory. So we should wait a few
3230 * seconds and retry rmdir. */
3232 for (t = 0; t < 10; t++) {
3235 r = systemf("rmdir /S /Q %s", tests[i].name);
3239 systemf("del %s", logfilename);
3241 systemf("rm -rf %s", tests[i].name);
3242 systemf("rm %s", logfilename);
3246 /* Return appropriate status. */
3247 return (tests[i].failures);
3253 * MAIN and support routines.
3259 usage(const char *program)
3261 static const int limit = sizeof(tests) / sizeof(tests[0]);
3264 printf("Usage: %s [options] <test> <test> ...\n", program);
3265 printf("Default is to run all tests.\n");
3266 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
3267 printf("Options:\n");
3268 printf(" -d Dump core after any failure, for debugging.\n");
3269 printf(" -k Keep all temp files.\n");
3270 printf(" Default: temp files for successful tests deleted.\n");
3272 printf(" -p <path> Path to executable to be tested.\n");
3273 printf(" Default: path taken from " ENVBASE " environment variable.\n");
3275 printf(" -q Quiet.\n");
3276 printf(" -r <dir> Path to dir containing reference files.\n");
3277 printf(" Default: Current directory.\n");
3278 printf(" -u Keep running specifies tests until one fails.\n");
3279 printf(" -v Verbose.\n");
3280 printf("Available tests:\n");
3281 for (i = 0; i < limit; i++)
3282 printf(" %d: %s\n", i, tests[i].name);
3287 get_refdir(const char *d)
3289 size_t tried_size, buff_size;
3290 char *buff, *tried, *pwd = NULL, *p = NULL;
3293 buff_size = PATH_MAX;
3297 buff = calloc(buff_size, 1);
3299 fprintf(stderr, "Unable to allocate memory\n");
3303 /* Allocate a buffer to hold the various directories we checked. */
3304 tried_size = buff_size * 2;
3305 tried = calloc(tried_size, 1);
3306 if (tried == NULL) {
3307 fprintf(stderr, "Unable to allocate memory\n");
3311 /* If a dir was specified, try that */
3314 snprintf(buff, buff_size, "%s", d);
3315 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3316 if (p != NULL) goto success;
3317 strncat(tried, buff, tried_size - strlen(tried) - 1);
3318 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3322 /* Get the current dir. */
3324 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3326 pwd = getcwd(NULL, 0);
3328 while (pwd[strlen(pwd) - 1] == '\n')
3329 pwd[strlen(pwd) - 1] = '\0';
3331 /* Look for a known file. */
3332 snprintf(buff, buff_size, "%s", pwd);
3333 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3334 if (p != NULL) goto success;
3335 strncat(tried, buff, tried_size - strlen(tried) - 1);
3336 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3338 snprintf(buff, buff_size, "%s/test", pwd);
3339 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3340 if (p != NULL) goto success;
3341 strncat(tried, buff, tried_size - strlen(tried) - 1);
3342 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3344 #if defined(LIBRARY)
3345 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
3347 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
3349 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3350 if (p != NULL) goto success;
3351 strncat(tried, buff, tried_size - strlen(tried) - 1);
3352 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3354 #if defined(PROGRAM_ALIAS)
3355 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
3356 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3357 if (p != NULL) goto success;
3358 strncat(tried, buff, tried_size - strlen(tried) - 1);
3359 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3362 if (memcmp(pwd, "/usr/obj", 8) == 0) {
3363 snprintf(buff, buff_size, "%s", pwd + 8);
3364 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3365 if (p != NULL) goto success;
3366 strncat(tried, buff, tried_size - strlen(tried) - 1);
3367 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3369 snprintf(buff, buff_size, "%s/test", pwd + 8);
3370 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3371 if (p != NULL) goto success;
3372 strncat(tried, buff, tried_size - strlen(tried) - 1);
3373 strncat(tried, "\n", tried_size - strlen(tried) - 1);
3377 printf("Unable to locate known reference file %s\n", KNOWNREF);
3378 printf(" Checked following directories:\n%s\n", tried);
3379 printf("Use -r option to specify full path to reference directory\n");
3380 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
3390 /* Copy result into a fresh buffer to reduce memory usage. */
3397 main(int argc, char **argv)
3399 static const int limit = sizeof(tests) / sizeof(tests[0]);
3400 int test_set[sizeof(tests) / sizeof(tests[0])];
3401 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
3403 char *refdir_alloc = NULL;
3404 const char *progname;
3406 const char *tmp, *option_arg, *p;
3407 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
3408 char tmpdir_timestamp[256];
3410 (void)argc; /* UNUSED */
3412 /* Get the current dir. */
3414 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3416 pwd = getcwd(NULL, 0);
3418 while (pwd[strlen(pwd) - 1] == '\n')
3419 pwd[strlen(pwd) - 1] = '\0';
3421 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
3422 /* To stop to run the default invalid parameter handler. */
3423 _set_invalid_parameter_handler(invalid_parameter_handler);
3424 /* Disable annoying assertion message box. */
3425 _CrtSetReportMode(_CRT_ASSERT, 0);
3429 * Name of this program, used to build root of our temp directory
3432 progname = p = argv[0];
3433 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
3435 fprintf(stderr, "ERROR: Out of memory.");
3438 strcpy(testprogdir, progname);
3439 while (*p != '\0') {
3440 /* Support \ or / dir separators for Windows compat. */
3441 if (*p == '/' || *p == '\\')
3449 testprogdir[i] = '\0';
3450 #if defined(_WIN32) && !defined(__CYGWIN__)
3451 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
3452 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
3453 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
3454 testprogdir[1] == ':' &&
3455 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
3457 if (testprogdir[0] != '/')
3460 /* Fixup path for relative directories. */
3461 if ((testprogdir = (char *)realloc(testprogdir,
3462 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
3464 fprintf(stderr, "ERROR: Out of memory.");
3467 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
3468 strlen(testprogdir) + 1);
3469 memcpy(testprogdir, pwd, strlen(pwd));
3470 testprogdir[strlen(pwd)] = '/';
3474 /* Get the target program from environment, if available. */
3475 testprogfile = getenv(ENVBASE);
3478 if (getenv("TMPDIR") != NULL)
3479 tmp = getenv("TMPDIR");
3480 else if (getenv("TMP") != NULL)
3481 tmp = getenv("TMP");
3482 else if (getenv("TEMP") != NULL)
3483 tmp = getenv("TEMP");
3484 else if (getenv("TEMPDIR") != NULL)
3485 tmp = getenv("TEMPDIR");
3489 /* Allow -d to be controlled through the environment. */
3490 if (getenv(ENVBASE "_DEBUG") != NULL)
3491 dump_on_failure = 1;
3493 /* Allow -v to be controlled through the environment. */
3494 if (getenv("_VERBOSITY_LEVEL") != NULL)
3496 vlevel = getenv("_VERBOSITY_LEVEL");
3497 verbosity = atoi(vlevel);
3498 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
3500 /* Unsupported verbosity levels are silently ignored */
3502 verbosity = VERBOSITY_PASSFAIL;
3506 /* Get the directory holding test files from environment. */
3507 refdir = getenv(ENVBASE "_TEST_FILES");
3510 * Parse options, without using getopt(), which isn't available
3513 ++argv; /* Skip program name */
3514 while (*argv != NULL) {
3519 while (*p != '\0') {
3522 /* If 'opt' takes an argument, parse that. */
3523 if (option == 'p' || option == 'r') {
3526 else if (*argv == NULL) {
3528 "Option -%c requires argument.\n",
3532 option_arg = *argv++;
3533 p = ""; /* End of this option word. */
3536 /* Now, handle the option. */
3539 dump_on_failure = 1;
3542 keep_temp_files = 1;
3546 testprogfile = option_arg;
3548 fprintf(stderr, "-p option not permitted\n");
3557 refdir = option_arg;
3567 fprintf(stderr, "Unrecognized option '%c'\n",
3575 * Sanity-check that our options make sense.
3578 if (testprogfile == NULL)
3580 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
3581 strlen(PROGRAM) + 1)) == NULL)
3583 fprintf(stderr, "ERROR: Out of memory.");
3586 strcpy(tmp2, testprogdir);
3588 strcat(tmp2, PROGRAM);
3589 testprogfile = tmp2;
3594 #if defined(_WIN32) && !defined(__CYGWIN__)
3595 /* Command.com sometimes rejects '/' separators. */
3596 testprg = strdup(testprogfile);
3597 for (i = 0; testprg[i] != '\0'; i++) {
3598 if (testprg[i] == '/')
3601 testprogfile = testprg;
3603 /* Quote the name that gets put into shell command lines. */
3604 testprg = malloc(strlen(testprogfile) + 3);
3605 strcpy(testprg, "\"");
3606 strcat(testprg, testprogfile);
3607 strcat(testprg, "\"");
3612 #if !defined(_WIN32) && defined(SIGPIPE)
3613 { /* Ignore SIGPIPE signals */
3614 struct sigaction sa;
3615 sa.sa_handler = SIG_IGN;
3616 sigemptyset(&sa.sa_mask);
3618 sigaction(SIGPIPE, &sa, NULL);
3623 * Create a temp directory for the following tests.
3624 * Include the time the tests started as part of the name,
3625 * to make it easier to track the results of multiple tests.
3628 for (i = 0; ; i++) {
3629 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
3630 "%Y-%m-%dT%H.%M.%S",
3632 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
3633 tmpdir_timestamp, i);
3634 if (assertMakeDir(tmpdir,0755))
3638 "ERROR: Unable to create temp directory %s\n",
3645 * If the user didn't specify a directory for locating
3646 * reference files, try to find the reference files in
3647 * the "usual places."
3649 refdir = refdir_alloc = get_refdir(refdir);
3652 * Banner with basic information.
3655 printf("If tests fail or crash, details will be in:\n");
3656 printf(" %s\n", tmpdir);
3658 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3659 printf("Reference files will be read from: %s\n", refdir);
3661 printf("Running tests on: %s\n", testprog);
3663 printf("Exercising: ");
3665 printf("%s\n", EXTRA_VERSION);
3672 * Run some or all of the individual tests.
3680 test_num = get_test_set(test_set, limit, *argv, tests);
3682 printf("*** INVALID Test %s\n", *argv);
3688 for (i = 0; i < test_num; i++) {
3690 if (test_run(test_set[i], tmpdir)) {
3698 } while (*argv != NULL);
3699 } while (until_failure);
3702 /* Must be freed after all tests run */
3708 * Report summary statistics.
3710 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3712 printf("Totals:\n");
3713 printf(" Tests run: %8d\n", tests_run);
3714 printf(" Tests failed: %8d\n", tests_failed);
3715 printf(" Assertions checked:%8d\n", assertions);
3716 printf(" Assertions failed: %8d\n", failures);
3717 printf(" Skips reported: %8d\n", skips);
3721 printf("Failing tests:\n");
3722 for (i = 0; i < limit; ++i) {
3723 if (tests[i].failures)
3724 printf(" %d: %s (%d failures)\n", i,
3725 tests[i].name, tests[i].failures);
3728 printf("Details for failing tests: %s\n", tmpdir);
3731 if (verbosity == VERBOSITY_SUMMARY_ONLY)
3733 printf("%d tests passed, no failures\n", tests_run);
3738 /* If the final tmpdir is empty, we can remove it. */
3739 /* This should be the usual case when all tests succeed. */
3743 return (tests_failed ? 1 : 0);