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 */
57 * This same file is used pretty much verbatim for all test harnesses.
59 * The next few lines are the only differences.
60 * TODO: Move this into a separate configuration header, have all test
61 * suites share one copy of this file.
63 __FBSDID("$FreeBSD$");
64 #define KNOWNREF "test_compat_gtar_1.tar.uu"
65 #define ENVBASE "LIBARCHIVE" /* Prefix for environment variables. */
66 #undef PROGRAM /* Testing a library, not a program. */
67 #define LIBRARY "libarchive"
68 #define EXTRA_DUMP(x) archive_error_string((struct archive *)(x))
69 #define EXTRA_ERRNO(x) archive_errno((struct archive *)(x))
70 #define EXTRA_VERSION archive_version_string()
74 * Windows support routines
76 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
77 * in the test harness is dangerous because they cover up
78 * configuration errors. The classic example of this is omitting a
79 * configure check. If libarchive and libarchive_test both look for
80 * the same feature macro, such errors are hard to detect. Platform
81 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
82 * easily lead to very messy code. It's best to limit yourself
83 * to only the most generic programming techniques in the test harness
84 * and thus avoid conditionals altogether. Where that's not possible,
85 * try to minimize conditionals by grouping platform-specific tests in
86 * one place (e.g., test_acl_freebsd) or by adding new assert()
87 * functions (e.g., assertMakeHardlink()) to cover up platform
88 * differences. Platform-specific coding in libarchive_test is often
89 * a symptom that some capability is missing from libarchive itself.
91 #if defined(_WIN32) && !defined(__CYGWIN__)
99 #define S_ISDIR(m) ((m) & _S_IFDIR)
102 #define S_ISREG(m) ((m) & _S_IFREG)
104 #if !defined(__BORLANDC__)
105 #define access _access
110 #define fileno _fileno
112 /*#define fstat _fstat64*/
113 #if !defined(__BORLANDC__)
114 #define getcwd _getcwd
117 /*#define lstat _stat64*/
118 /*#define stat _stat64*/
120 #if !defined(__BORLANDC__)
121 #define strdup _strdup
124 #define int64_t __int64
127 #if defined(HAVE__CrtSetReportMode)
131 #if defined(_WIN32) && !defined(__CYGWIN__)
132 static void *GetFunctionKernel32(const char *);
133 static int my_CreateSymbolicLinkA(const char *, const char *, int);
134 static int my_CreateHardLinkA(const char *, const char *);
135 static int my_GetFileInformationByName(const char *,
136 BY_HANDLE_FILE_INFORMATION *);
139 GetFunctionKernel32(const char *name)
141 static HINSTANCE lib;
145 lib = LoadLibrary("kernel32.dll");
148 fprintf(stderr, "Can't load kernel32.dll?!\n");
151 return (void *)GetProcAddress(lib, name);
155 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
157 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
161 f = GetFunctionKernel32("CreateSymbolicLinkA");
163 return f == NULL ? 0 : (*f)(linkname, target, flags);
167 my_CreateHardLinkA(const char *linkname, const char *target)
169 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
173 f = GetFunctionKernel32("CreateHardLinkA");
175 return f == NULL ? 0 : (*f)(linkname, target, NULL);
179 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
184 memset(bhfi, 0, sizeof(*bhfi));
185 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
186 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
187 if (h == INVALID_HANDLE_VALUE)
189 r = GetFileInformationByHandle(h, bhfi);
195 #if defined(HAVE__CrtSetReportMode)
197 invalid_parameter_handler(const wchar_t * expression,
198 const wchar_t * function, const wchar_t * file,
199 unsigned int line, uintptr_t pReserved)
211 /* Enable core dump on failure. */
212 static int dump_on_failure = 0;
213 /* Default is to remove temp dirs and log data for successful tests. */
214 static int keep_temp_files = 0;
215 /* Default is to run the specified tests once and report errors. */
216 static int until_failure = 0;
217 /* Default is to just report pass/fail for each test. */
218 static int verbosity = 0;
219 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
220 #define VERBOSITY_PASSFAIL 0 /* Default */
221 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
222 #define VERBOSITY_FULL 2 /* -vv */
223 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
224 * mostly for debugging the test harness itself. */
225 /* Cumulative count of assertion failures. */
226 static int failures = 0;
227 /* Cumulative count of reported skips. */
228 static int skips = 0;
229 /* Cumulative count of assertions checked. */
230 static int assertions = 0;
232 /* Directory where uuencoded reference files can be found. */
233 static const char *refdir;
236 * Report log information selectively to console and/or disk log.
238 static int log_console = 0;
239 static FILE *logfile;
241 vlogprintf(const char *fmt, va_list ap)
248 vfprintf(stdout, fmt, ap);
251 vfprintf(logfile, fmt, lfap);
254 vfprintf(logfile, fmt, ap);
259 logprintf(const char *fmt, ...)
267 /* Set up a message to display only if next assertion fails. */
268 static char msgbuff[4096];
269 static const char *msg, *nextmsg;
271 failure(const char *fmt, ...)
278 vsprintf(msgbuff, fmt, ap);
285 * Copy arguments into file-local variables.
286 * This was added to permit vararg assert() functions without needing
287 * variadic wrapper macros. Turns out that the vararg capability is almost
288 * never used, so almost all of the vararg assertions can be simplified
289 * by removing the vararg capability and reworking the wrapper macro to
290 * pass __FILE__, __LINE__ directly into the function instead of using
291 * this hook. I suspect this machinery is used so rarely that we
292 * would be better off just removing it entirely. That would simplify
293 * the code here noticeably.
295 static const char *skipping_filename;
296 static int skipping_line;
297 void skipping_setup(const char *filename, int line)
299 skipping_filename = filename;
300 skipping_line = line;
303 /* Called at the beginning of each assert() function. */
305 assertion_count(const char *file, int line)
307 (void)file; /* UNUSED */
308 (void)line; /* UNUSED */
310 /* Proper handling of "failure()" message. */
313 /* Uncomment to print file:line after every assertion.
314 * Verbose, but occasionally useful in tracking down crashes. */
315 /* printf("Checked %s:%d\n", file, line); */
319 * For each test source file, we remember how many times each
320 * assertion was reported. Cleared before each new test,
321 * used by test_summarize().
326 } failed_lines[10000];
327 const char *failed_filename;
329 /* Count this failure, setup up log destination and handle initial report. */
331 failure_start(const char *filename, int line, const char *fmt, ...)
335 /* Record another failure for this line. */
337 failed_filename = filename;
338 failed_lines[line].count++;
340 /* Determine whether to log header to console. */
342 case VERBOSITY_LIGHT_REPORT:
343 log_console = (failed_lines[line].count < 2);
346 log_console = (verbosity >= VERBOSITY_FULL);
349 /* Log file:line header for this failure */
352 logprintf("%s(%d): ", filename, line);
354 logprintf("%s:%d: ", filename, line);
360 if (msg != NULL && msg[0] != '\0') {
361 logprintf(" Description: %s\n", msg);
365 /* Determine whether to log details to console. */
366 if (verbosity == VERBOSITY_LIGHT_REPORT)
370 /* Complete reporting of failed tests. */
372 * The 'extra' hook here is used by libarchive to include libarchive
373 * error messages with assertion failures. It could also be used
374 * to add strerror() output, for example. Just define the EXTRA_DUMP()
375 * macro appropriately.
378 failure_finish(void *extra)
380 (void)extra; /* UNUSED (maybe) */
383 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
384 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
388 if (dump_on_failure) {
390 " *** forcing core dump so failure can be debugged ***\n");
395 /* Inform user that we're skipping some checks. */
397 test_skipping(const char *fmt, ...)
403 vsprintf(buff, fmt, ap);
405 /* Use failure() message if set. */
408 /* failure_start() isn't quite right, but is awfully convenient. */
409 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
410 --failures; /* Undo failures++ in failure_start() */
411 /* Don't failure_finish() here. */
412 /* Mark as skip, so doesn't count as failed test. */
413 failed_lines[skipping_line].skip = 1;
423 /* Generic assert() just displays the failed condition. */
425 assertion_assert(const char *file, int line, int value,
426 const char *condition, void *extra)
428 assertion_count(file, line);
430 failure_start(file, line, "Assertion failed: %s", condition);
431 failure_finish(extra);
436 /* chdir() and report any errors */
438 assertion_chdir(const char *file, int line, const char *pathname)
440 assertion_count(file, line);
441 if (chdir(pathname) == 0)
443 failure_start(file, line, "chdir(\"%s\")", pathname);
444 failure_finish(NULL);
449 /* Verify two integers are equal. */
451 assertion_equal_int(const char *file, int line,
452 long long v1, const char *e1, long long v2, const char *e2, void *extra)
454 assertion_count(file, line);
457 failure_start(file, line, "%s != %s", e1, e2);
458 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
459 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
460 failure_finish(extra);
465 * Utility to convert a single UTF-8 sequence.
468 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
470 static const char utf8_count[256] = {
471 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
472 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
473 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
474 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
475 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
476 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
477 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
478 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
479 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
480 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
481 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
482 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
483 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
484 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
485 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
486 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
498 * Decode 1-4 bytes depending on the value of the first byte.
500 ch = (unsigned char)*s;
502 return (0); /* Standard: return 0 for end-of-string. */
503 cnt = utf8_count[ch];
505 /* Invalide sequence or there are not plenty bytes. */
509 /* Make a Unicode code point from a single UTF-8 sequence. */
511 case 1: /* 1 byte sequence. */
514 case 2: /* 2 bytes sequence. */
515 if ((s[1] & 0xc0) != 0x80) return (-1);
516 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
518 case 3: /* 3 bytes sequence. */
519 if ((s[1] & 0xc0) != 0x80) return (-1);
520 if ((s[2] & 0xc0) != 0x80) return (-1);
521 wc = ((ch & 0x0f) << 12)
522 | ((s[1] & 0x3f) << 6)
525 return (-1);/* Overlong sequence. */
527 case 4: /* 4 bytes sequence. */
530 if ((s[1] & 0xc0) != 0x80) return (-1);
531 if ((s[2] & 0xc0) != 0x80) return (-1);
532 if ((s[3] & 0xc0) != 0x80) return (-1);
533 wc = ((ch & 0x07) << 18)
534 | ((s[1] & 0x3f) << 12)
535 | ((s[2] & 0x3f) << 6)
538 return (-1);/* Overlong sequence. */
544 /* The code point larger than 0x10FFFF is not leagal
548 /* Correctly gets a Unicode, returns used bytes. */
553 static void strdump(const char *e, const char *p, int ewidth, int utf8)
557 logprintf(" %*s = ", ewidth, e);
564 unsigned int c = 0xff & *p++;
566 case '\a': printf("\a"); break;
567 case '\b': printf("\b"); break;
568 case '\n': printf("\n"); break;
569 case '\r': printf("\r"); break;
571 if (c >= 32 && c < 127)
574 logprintf("\\x%02X", c);
578 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
581 * If the current string is UTF-8, dump its code points.
592 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
595 logprintf("%04X", uc);
601 logprintf(" (count %d", cnt);
603 logprintf(",unknown %d bytes", len);
611 /* Verify two strings are equal, dump them if not. */
613 assertion_equal_string(const char *file, int line,
614 const char *v1, const char *e1,
615 const char *v2, const char *e2,
616 void *extra, int utf8)
620 assertion_count(file, line);
621 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
623 failure_start(file, line, "%s != %s", e1, e2);
624 l1 = (int)strlen(e1);
625 l2 = (int)strlen(e2);
628 strdump(e1, v1, l1, utf8);
629 strdump(e2, v2, l1, utf8);
630 failure_finish(extra);
635 wcsdump(const char *e, const wchar_t *w)
637 logprintf(" %s = ", e);
643 while (*w != L'\0') {
644 unsigned int c = *w++;
645 if (c >= 32 && c < 127)
648 logprintf("\\x%02X", c);
649 else if (c < 0x10000)
650 logprintf("\\u%04X", c);
652 logprintf("\\U%08X", c);
659 wcscmp(const wchar_t *s1, const wchar_t *s2)
662 while (*s1 == *s2++) {
673 /* Verify that two wide strings are equal, dump them if not. */
675 assertion_equal_wstring(const char *file, int line,
676 const wchar_t *v1, const char *e1,
677 const wchar_t *v2, const char *e2,
680 assertion_count(file, line);
683 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
685 failure_start(file, line, "%s != %s", e1, e2);
688 failure_finish(extra);
693 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
694 * any bytes in p that differ from ref will be highlighted with '_'
695 * before and after the hex value.
698 hexdump(const char *p, const char *ref, size_t l, size_t offset)
704 logprintf("(null)\n");
707 for(i=0; i < l; i+=16) {
708 logprintf("%04x", (unsigned)(i + offset));
710 for (j = 0; j < 16 && i + j < l; j++) {
711 if (ref != NULL && p[i + j] != ref[i + j])
713 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
714 if (ref != NULL && p[i + j] == ref[i + j])
717 for (; j < 16; j++) {
718 logprintf("%c ", sep);
721 logprintf("%c", sep);
722 for (j=0; j < 16 && i + j < l; j++) {
724 if (c >= ' ' && c <= 126)
733 /* Verify that two blocks of memory are the same, display the first
734 * block of differences if they're not. */
736 assertion_equal_mem(const char *file, int line,
737 const void *_v1, const char *e1,
738 const void *_v2, const char *e2,
739 size_t l, const char *ld, void *extra)
741 const char *v1 = (const char *)_v1;
742 const char *v2 = (const char *)_v2;
745 assertion_count(file, line);
746 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
748 if (v1 == NULL || v2 == NULL)
751 failure_start(file, line, "%s != %s", e1, e2);
752 logprintf(" size %s = %d\n", ld, (int)l);
753 /* Dump 48 bytes (3 lines) so that the first difference is
754 * in the second line. */
756 while (l > 64 && memcmp(v1, v2, 32) == 0) {
757 /* Two lines agree, so step forward one line. */
763 logprintf(" Dump of %s\n", e1);
764 hexdump(v1, v2, l < 128 ? l : 128, offset);
765 logprintf(" Dump of %s\n", e2);
766 hexdump(v2, v1, l < 128 ? l : 128, offset);
768 failure_finish(extra);
772 /* Verify that the named file exists and is empty. */
774 assertion_empty_file(const char *filename, int line, const char *f1)
781 assertion_count(filename, line);
783 if (stat(f1, &st) != 0) {
784 failure_start(filename, line, "Stat failed: %s", f1);
785 failure_finish(NULL);
791 failure_start(filename, line, "File should be empty: %s", f1);
792 logprintf(" File size: %d\n", (int)st.st_size);
793 logprintf(" Contents:\n");
796 logprintf(" Unable to open %s\n", f1);
798 s = ((off_t)sizeof(buff) < st.st_size) ?
799 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
800 s = fread(buff, 1, s, f);
801 hexdump(buff, NULL, s, 0);
804 failure_finish(NULL);
808 /* Verify that the named file exists and is not empty. */
810 assertion_non_empty_file(const char *filename, int line, const char *f1)
814 assertion_count(filename, line);
816 if (stat(f1, &st) != 0) {
817 failure_start(filename, line, "Stat failed: %s", f1);
818 failure_finish(NULL);
821 if (st.st_size == 0) {
822 failure_start(filename, line, "File empty: %s", f1);
823 failure_finish(NULL);
829 /* Verify that two files have the same contents. */
830 /* TODO: hexdump the first bytes that actually differ. */
832 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
839 assertion_count(filename, line);
841 f1 = fopen(fn1, "rb");
842 f2 = fopen(fn2, "rb");
843 if (f1 == NULL || f2 == NULL) {
849 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
850 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
853 if (n1 == 0 && n2 == 0) {
858 if (memcmp(buff1, buff2, n1) != 0)
863 failure_start(filename, line, "Files not identical");
864 logprintf(" file1=\"%s\"\n", fn1);
865 logprintf(" file2=\"%s\"\n", fn2);
866 failure_finish(NULL);
870 /* Verify that the named file does exist. */
872 assertion_file_exists(const char *filename, int line, const char *f)
874 assertion_count(filename, line);
876 #if defined(_WIN32) && !defined(__CYGWIN__)
880 if (!access(f, F_OK))
883 failure_start(filename, line, "File should exist: %s", f);
884 failure_finish(NULL);
888 /* Verify that the named file doesn't exist. */
890 assertion_file_not_exists(const char *filename, int line, const char *f)
892 assertion_count(filename, line);
894 #if defined(_WIN32) && !defined(__CYGWIN__)
901 failure_start(filename, line, "File should not exist: %s", f);
902 failure_finish(NULL);
906 /* Compare the contents of a file to a block of memory. */
908 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
914 assertion_count(filename, line);
918 failure_start(filename, line,
919 "File should exist: %s", fn);
920 failure_finish(NULL);
923 contents = malloc(s * 2);
924 n = (int)fread(contents, 1, s * 2, f);
926 if (n == s && memcmp(buff, contents, s) == 0) {
930 failure_start(filename, line, "File contents don't match");
931 logprintf(" file=\"%s\"\n", fn);
933 hexdump(contents, buff, n > 512 ? 512 : n, 0);
935 logprintf(" File empty, contents should be:\n");
936 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
938 failure_finish(NULL);
943 /* Check the contents of a text file, being tolerant of line endings. */
945 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
948 const char *btxt, *ftxt;
952 assertion_count(filename, line);
955 failure_start(filename, line,
956 "File doesn't exist: %s", fn);
957 failure_finish(NULL);
960 s = (int)strlen(buff);
961 contents = malloc(s * 2 + 128);
962 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
968 ftxt = (const char *)contents;
969 while (*btxt != '\0' && *ftxt != '\0') {
970 if (*btxt == *ftxt) {
975 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
976 /* Pass over different new line characters. */
983 if (*btxt == '\0' && *ftxt == '\0') {
987 failure_start(filename, line, "Contents don't match");
988 logprintf(" file=\"%s\"\n", fn);
990 hexdump(contents, buff, n, 0);
991 logprintf(" expected\n", fn);
992 hexdump(buff, contents, s, 0);
994 logprintf(" File empty, contents should be:\n");
995 hexdump(buff, NULL, s, 0);
997 failure_finish(NULL);
1002 /* Verify that a text file contains the specified lines, regardless of order */
1003 /* This could be more efficient if we sorted both sets of lines, etc, but
1004 * since this is used only for testing and only ever deals with a dozen or so
1005 * lines at a time, this relatively crude approach is just fine. */
1007 assertion_file_contains_lines_any_order(const char *file, int line,
1008 const char *pathname, const char *lines[])
1012 size_t expected_count, actual_count, i, j;
1013 char **expected = NULL;
1014 char *p, **actual = NULL;
1016 int expected_failure = 0, actual_failure = 0;
1018 assertion_count(file, line);
1020 buff = slurpfile(&buff_size, "%s", pathname);
1022 failure_start(pathname, line, "Can't read file: %s", pathname);
1023 failure_finish(NULL);
1027 /* Make a copy of the provided lines and count up the expected
1029 for (i = 0; lines[i] != NULL; ++i) {
1032 if (expected_count) {
1033 expected = malloc(sizeof(char *) * expected_count);
1034 if (expected == NULL) {
1035 failure_start(pathname, line, "Can't allocate memory");
1036 failure_finish(NULL);
1039 for (i = 0; lines[i] != NULL; ++i) {
1040 expected[i] = strdup(lines[i]);
1044 /* Break the file into lines */
1046 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1047 if (*p == '\x0d' || *p == '\x0a')
1049 if (c == '\0' && *p != '\0')
1054 actual = calloc(sizeof(char *), actual_count);
1055 if (actual == NULL) {
1056 failure_start(pathname, line, "Can't allocate memory");
1057 failure_finish(NULL);
1061 for (j = 0, p = buff; p < buff + buff_size;
1062 p += 1 + strlen(p)) {
1070 /* Erase matching lines from both lists */
1071 for (i = 0; i < expected_count; ++i) {
1072 if (expected[i] == NULL)
1074 for (j = 0; j < actual_count; ++j) {
1075 if (actual[j] == NULL)
1077 if (strcmp(expected[i], actual[j]) == 0) {
1086 /* If there's anything left, it's a failure */
1087 for (i = 0; i < expected_count; ++i) {
1088 if (expected[i] != NULL)
1091 for (j = 0; j < actual_count; ++j) {
1092 if (actual[j] != NULL)
1095 if (expected_failure == 0 && actual_failure == 0) {
1101 failure_start(file, line, "File doesn't match: %s", pathname);
1102 for (i = 0; i < expected_count; ++i) {
1103 if (expected[i] != NULL) {
1104 logprintf(" Expected but not present: %s\n", expected[i]);
1108 for (j = 0; j < actual_count; ++j) {
1109 if (actual[j] != NULL)
1110 logprintf(" Present but not expected: %s\n", actual[j]);
1112 failure_finish(NULL);
1119 /* Test that two paths point to the same file. */
1120 /* As a side-effect, asserts that both files exist. */
1122 is_hardlink(const char *file, int line,
1123 const char *path1, const char *path2)
1125 #if defined(_WIN32) && !defined(__CYGWIN__)
1126 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1129 assertion_count(file, line);
1130 r = my_GetFileInformationByName(path1, &bhfi1);
1132 failure_start(file, line, "File %s can't be inspected?", path1);
1133 failure_finish(NULL);
1136 r = my_GetFileInformationByName(path2, &bhfi2);
1138 failure_start(file, line, "File %s can't be inspected?", path2);
1139 failure_finish(NULL);
1142 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1143 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1144 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1146 struct stat st1, st2;
1149 assertion_count(file, line);
1150 r = lstat(path1, &st1);
1152 failure_start(file, line, "File should exist: %s", path1);
1153 failure_finish(NULL);
1156 r = lstat(path2, &st2);
1158 failure_start(file, line, "File should exist: %s", path2);
1159 failure_finish(NULL);
1162 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1167 assertion_is_hardlink(const char *file, int line,
1168 const char *path1, const char *path2)
1170 if (is_hardlink(file, line, path1, path2))
1172 failure_start(file, line,
1173 "Files %s and %s are not hardlinked", path1, path2);
1174 failure_finish(NULL);
1179 assertion_is_not_hardlink(const char *file, int line,
1180 const char *path1, const char *path2)
1182 if (!is_hardlink(file, line, path1, path2))
1184 failure_start(file, line,
1185 "Files %s and %s should not be hardlinked", path1, path2);
1186 failure_finish(NULL);
1190 /* Verify a/b/mtime of 'pathname'. */
1191 /* If 'recent', verify that it's within last 10 seconds. */
1193 assertion_file_time(const char *file, int line,
1194 const char *pathname, long t, long nsec, char type, int recent)
1196 long long filet, filet_nsec;
1199 #if defined(_WIN32) && !defined(__CYGWIN__)
1200 #define EPOC_TIME (116444736000000000ULL)
1201 FILETIME fxtime, fbirthtime, fatime, fmtime;
1202 ULARGE_INTEGER wintm;
1204 fxtime.dwLowDateTime = 0;
1205 fxtime.dwHighDateTime = 0;
1207 assertion_count(file, line);
1208 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1209 * a directory file. If not, CreateFile() will fail when
1210 * the pathname is a directory. */
1211 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1212 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1213 if (h == INVALID_HANDLE_VALUE) {
1214 failure_start(file, line, "Can't access %s\n", pathname);
1215 failure_finish(NULL);
1218 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1220 case 'a': fxtime = fatime; break;
1221 case 'b': fxtime = fbirthtime; break;
1222 case 'm': fxtime = fmtime; break;
1226 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1227 failure_finish(NULL);
1230 wintm.LowPart = fxtime.dwLowDateTime;
1231 wintm.HighPart = fxtime.dwHighDateTime;
1232 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1233 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1234 nsec = (nsec / 100) * 100; /* Round the request */
1238 assertion_count(file, line);
1239 r = lstat(pathname, &st);
1241 failure_start(file, line, "Can't stat %s\n", pathname);
1242 failure_finish(NULL);
1246 case 'a': filet = st.st_atime; break;
1247 case 'm': filet = st.st_mtime; break;
1248 case 'b': filet = 0; break;
1249 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1252 #if defined(__FreeBSD__)
1254 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1255 case 'b': filet = st.st_birthtime;
1256 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1257 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1258 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1261 /* FreeBSD generally only stores to microsecond res, so round. */
1262 filet_nsec = (filet_nsec / 1000) * 1000;
1263 nsec = (nsec / 1000) * 1000;
1265 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1266 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1267 #if defined(__HAIKU__)
1268 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1273 /* Check that requested time is up-to-date. */
1274 time_t now = time(NULL);
1275 if (filet < now - 10 || filet > now + 1) {
1276 failure_start(file, line,
1277 "File %s has %ctime %lld, %lld seconds ago\n",
1278 pathname, type, filet, now - filet);
1279 failure_finish(NULL);
1282 } else if (filet != t || filet_nsec != nsec) {
1283 failure_start(file, line,
1284 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1285 pathname, type, filet, filet_nsec, t, nsec);
1286 failure_finish(NULL);
1292 /* Verify atime of 'pathname'. */
1294 assertion_file_atime(const char *file, int line,
1295 const char *pathname, long t, long nsec)
1297 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1300 /* Verify atime of 'pathname' is up-to-date. */
1302 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1304 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1307 /* Verify birthtime of 'pathname'. */
1309 assertion_file_birthtime(const char *file, int line,
1310 const char *pathname, long t, long nsec)
1312 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1315 /* Verify birthtime of 'pathname' is up-to-date. */
1317 assertion_file_birthtime_recent(const char *file, int line,
1318 const char *pathname)
1320 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1323 /* Verify mtime of 'pathname'. */
1325 assertion_file_mtime(const char *file, int line,
1326 const char *pathname, long t, long nsec)
1328 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1331 /* Verify mtime of 'pathname' is up-to-date. */
1333 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1335 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1338 /* Verify number of links to 'pathname'. */
1340 assertion_file_nlinks(const char *file, int line,
1341 const char *pathname, int nlinks)
1343 #if defined(_WIN32) && !defined(__CYGWIN__)
1344 BY_HANDLE_FILE_INFORMATION bhfi;
1347 assertion_count(file, line);
1348 r = my_GetFileInformationByName(pathname, &bhfi);
1349 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1351 failure_start(file, line, "File %s has %d links, expected %d",
1352 pathname, bhfi.nNumberOfLinks, nlinks);
1353 failure_finish(NULL);
1359 assertion_count(file, line);
1360 r = lstat(pathname, &st);
1361 if (r == 0 && (int)st.st_nlink == nlinks)
1363 failure_start(file, line, "File %s has %d links, expected %d",
1364 pathname, st.st_nlink, nlinks);
1365 failure_finish(NULL);
1370 /* Verify size of 'pathname'. */
1372 assertion_file_size(const char *file, int line, const char *pathname, long size)
1377 assertion_count(file, line);
1378 #if defined(_WIN32) && !defined(__CYGWIN__)
1380 BY_HANDLE_FILE_INFORMATION bhfi;
1381 r = !my_GetFileInformationByName(pathname, &bhfi);
1382 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1387 r = lstat(pathname, &st);
1388 filesize = st.st_size;
1391 if (r == 0 && filesize == size)
1393 failure_start(file, line, "File %s has size %ld, expected %ld",
1394 pathname, (long)filesize, (long)size);
1395 failure_finish(NULL);
1399 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1401 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1406 #if defined(_WIN32) && !defined(__CYGWIN__)
1407 (void)mode; /* UNUSED */
1409 assertion_count(file, line);
1410 r = lstat(pathname, &st);
1412 failure_start(file, line, "Dir should exist: %s", pathname);
1413 failure_finish(NULL);
1416 if (!S_ISDIR(st.st_mode)) {
1417 failure_start(file, line, "%s is not a dir", pathname);
1418 failure_finish(NULL);
1421 #if !defined(_WIN32) || defined(__CYGWIN__)
1422 /* Windows doesn't handle permissions the same way as POSIX,
1423 * so just ignore the mode tests. */
1424 /* TODO: Can we do better here? */
1425 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1426 failure_start(file, line, "Dir %s has wrong mode", pathname);
1427 logprintf(" Expected: 0%3o\n", mode);
1428 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1429 failure_finish(NULL);
1436 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1437 * verify that too. */
1439 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1444 #if defined(_WIN32) && !defined(__CYGWIN__)
1445 (void)mode; /* UNUSED */
1447 assertion_count(file, line);
1448 r = lstat(pathname, &st);
1449 if (r != 0 || !S_ISREG(st.st_mode)) {
1450 failure_start(file, line, "File should exist: %s", pathname);
1451 failure_finish(NULL);
1454 #if !defined(_WIN32) || defined(__CYGWIN__)
1455 /* Windows doesn't handle permissions the same way as POSIX,
1456 * so just ignore the mode tests. */
1457 /* TODO: Can we do better here? */
1458 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1459 failure_start(file, line, "File %s has wrong mode", pathname);
1460 logprintf(" Expected: 0%3o\n", mode);
1461 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1462 failure_finish(NULL);
1469 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1470 * non-NULL, verify that the symlink has those contents. */
1472 is_symlink(const char *file, int line,
1473 const char *pathname, const char *contents)
1475 #if defined(_WIN32) && !defined(__CYGWIN__)
1476 (void)pathname; /* UNUSED */
1477 (void)contents; /* UNUSED */
1478 assertion_count(file, line);
1479 /* Windows sort-of has real symlinks, but they're only usable
1480 * by privileged users and are crippled even then, so there's
1481 * really not much point in bothering with this. */
1489 assertion_count(file, line);
1490 r = lstat(pathname, &st);
1492 failure_start(file, line,
1493 "Symlink should exist: %s", pathname);
1494 failure_finish(NULL);
1497 if (!S_ISLNK(st.st_mode))
1499 if (contents == NULL)
1501 linklen = readlink(pathname, buff, sizeof(buff));
1503 failure_start(file, line, "Can't read symlink %s", pathname);
1504 failure_finish(NULL);
1507 buff[linklen] = '\0';
1508 if (strcmp(buff, contents) != 0)
1514 /* Assert that path is a symlink that (optionally) contains contents. */
1516 assertion_is_symlink(const char *file, int line,
1517 const char *path, const char *contents)
1519 if (is_symlink(file, line, path, contents))
1522 failure_start(file, line, "File %s is not a symlink to %s",
1525 failure_start(file, line, "File %s is not a symlink", path);
1526 failure_finish(NULL);
1531 /* Create a directory and report any errors. */
1533 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1535 assertion_count(file, line);
1536 #if defined(_WIN32) && !defined(__CYGWIN__)
1537 (void)mode; /* UNUSED */
1538 if (0 == _mkdir(dirname))
1541 if (0 == mkdir(dirname, mode))
1544 failure_start(file, line, "Could not create directory %s", dirname);
1545 failure_finish(NULL);
1549 /* Create a file with the specified contents and report any failures. */
1551 assertion_make_file(const char *file, int line,
1552 const char *path, int mode, int csize, const void *contents)
1554 #if defined(_WIN32) && !defined(__CYGWIN__)
1555 /* TODO: Rework this to set file mode as well. */
1557 (void)mode; /* UNUSED */
1558 assertion_count(file, line);
1559 f = fopen(path, "wb");
1561 failure_start(file, line, "Could not create file %s", path);
1562 failure_finish(NULL);
1565 if (contents != NULL) {
1569 wsize = strlen(contents);
1571 wsize = (size_t)csize;
1572 if (wsize != fwrite(contents, 1, wsize, f)) {
1574 failure_start(file, line,
1575 "Could not write file %s", path);
1576 failure_finish(NULL);
1584 assertion_count(file, line);
1585 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1587 failure_start(file, line, "Could not create %s", path);
1588 failure_finish(NULL);
1591 if (contents != NULL) {
1595 wsize = (ssize_t)strlen(contents);
1597 wsize = (ssize_t)csize;
1598 if (wsize != write(fd, contents, wsize)) {
1600 failure_start(file, line,
1601 "Could not write to %s", path);
1602 failure_finish(NULL);
1611 /* Create a hardlink and report any failures. */
1613 assertion_make_hardlink(const char *file, int line,
1614 const char *newpath, const char *linkto)
1618 assertion_count(file, line);
1619 #if defined(_WIN32) && !defined(__CYGWIN__)
1620 succeeded = my_CreateHardLinkA(newpath, linkto);
1622 succeeded = !link(linkto, newpath);
1628 failure_start(file, line, "Could not create hardlink");
1629 logprintf(" New link: %s\n", newpath);
1630 logprintf(" Old name: %s\n", linkto);
1631 failure_finish(NULL);
1635 /* Create a symlink and report any failures. */
1637 assertion_make_symlink(const char *file, int line,
1638 const char *newpath, const char *linkto)
1640 #if defined(_WIN32) && !defined(__CYGWIN__)
1641 int targetIsDir = 0; /* TODO: Fix this */
1642 assertion_count(file, line);
1643 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1646 assertion_count(file, line);
1647 if (0 == symlink(linkto, newpath))
1650 failure_start(file, line, "Could not create symlink");
1651 logprintf(" New link: %s\n", newpath);
1652 logprintf(" Old name: %s\n", linkto);
1653 failure_finish(NULL);
1657 /* Set umask, report failures. */
1659 assertion_umask(const char *file, int line, int mask)
1661 assertion_count(file, line);
1662 (void)file; /* UNUSED */
1663 (void)line; /* UNUSED */
1668 /* Set times, report failures. */
1670 assertion_utimes(const char *file, int line,
1671 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1675 #if defined(_WIN32) && !defined(__CYGWIN__)
1676 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1677 + (((nsec)/1000)*10))
1679 ULARGE_INTEGER wintm;
1680 FILETIME fatime, fmtime;
1681 FILETIME *pat, *pmt;
1683 assertion_count(file, line);
1684 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1685 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1686 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1687 if (h == INVALID_HANDLE_VALUE) {
1688 failure_start(file, line, "Can't access %s\n", pathname);
1689 failure_finish(NULL);
1693 if (at > 0 || at_nsec > 0) {
1694 wintm.QuadPart = WINTIME(at, at_nsec);
1695 fatime.dwLowDateTime = wintm.LowPart;
1696 fatime.dwHighDateTime = wintm.HighPart;
1700 if (mt > 0 || mt_nsec > 0) {
1701 wintm.QuadPart = WINTIME(mt, mt_nsec);
1702 fmtime.dwLowDateTime = wintm.LowPart;
1703 fmtime.dwHighDateTime = wintm.HighPart;
1707 if (pat != NULL || pmt != NULL)
1708 r = SetFileTime(h, NULL, pat, pmt);
1713 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1714 failure_finish(NULL);
1718 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1720 struct timeval times[2];
1722 #if !defined(__FreeBSD__)
1723 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1725 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1728 r = lstat(pathname, &st);
1730 failure_start(file, line, "Can't stat %s\n", pathname);
1731 failure_finish(NULL);
1735 if (mt == 0 && mt_nsec == 0) {
1737 #if defined(__FreeBSD__)
1738 mt_nsec = st.st_mtimespec.tv_nsec;
1739 /* FreeBSD generally only stores to microsecond res, so round. */
1740 mt_nsec = (mt_nsec / 1000) * 1000;
1743 if (at == 0 && at_nsec == 0) {
1745 #if defined(__FreeBSD__)
1746 at_nsec = st.st_atimespec.tv_nsec;
1747 /* FreeBSD generally only stores to microsecond res, so round. */
1748 at_nsec = (at_nsec / 1000) * 1000;
1752 times[1].tv_sec = mt;
1753 times[1].tv_usec = mt_nsec / 1000;
1755 times[0].tv_sec = at;
1756 times[0].tv_usec = at_nsec / 1000;
1759 r = lutimes(pathname, times);
1761 r = utimes(pathname, times);
1764 failure_start(file, line, "Can't utimes %s\n", pathname);
1765 failure_finish(NULL);
1769 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1772 /* Set nodump, report failures. */
1774 assertion_nodump(const char *file, int line, const char *pathname)
1776 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1779 assertion_count(file, line);
1780 r = chflags(pathname, UF_NODUMP);
1782 failure_start(file, line, "Can't set nodump %s\n", pathname);
1783 failure_finish(NULL);
1786 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1787 && defined(EXT2_NODUMP_FL)
1790 assertion_count(file, line);
1791 fd = open(pathname, O_RDONLY | O_NONBLOCK);
1793 failure_start(file, line, "Can't open %s\n", pathname);
1794 failure_finish(NULL);
1797 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1799 failure_start(file, line, "Can't get flags %s\n", pathname);
1800 failure_finish(NULL);
1803 flags |= EXT2_NODUMP_FL;
1804 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1806 failure_start(file, line, "Can't set nodump %s\n", pathname);
1807 failure_finish(NULL);
1812 (void)pathname; /* UNUSED */
1813 assertion_count(file, line);
1820 * UTILITIES for use by tests.
1825 * Check whether platform supports symlinks. This is intended
1826 * for tests to use in deciding whether to bother testing symlink
1827 * support; if the platform doesn't support symlinks, there's no point
1828 * in checking whether the program being tested can create them.
1830 * Note that the first time this test is called, we actually go out to
1831 * disk to create and verify a symlink. This is necessary because
1832 * symlink support is actually a property of a particular filesystem
1833 * and can thus vary between directories on a single system. After
1834 * the first call, this returns the cached result from memory, so it's
1835 * safe to call it as often as you wish.
1840 /* Remember the test result */
1841 static int value = 0, tested = 0;
1846 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1847 /* Note: Cygwin has its own symlink() emulation that does not
1848 * use the Win32 CreateSymbolicLink() function. */
1849 #if defined(_WIN32) && !defined(__CYGWIN__)
1850 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1851 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1853 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1854 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1859 /* Platform-dependent options for hiding the output of a subcommand. */
1860 #if defined(_WIN32) && !defined(__CYGWIN__)
1861 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1863 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1866 * Can this platform run the bzip2 program?
1871 static int tested = 0, value = 0;
1874 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
1881 * Can this platform run the grzip program?
1886 static int tested = 0, value = 0;
1889 if (systemf("grzip -V %s", redirectArgs) == 0)
1896 * Can this platform run the gzip program?
1901 static int tested = 0, value = 0;
1904 if (systemf("gzip -V %s", redirectArgs) == 0)
1911 * Can this platform run the lrzip program?
1914 canRunCommand(const char *cmd)
1916 static int tested = 0, value = 0;
1919 if (systemf("%s %s", cmd, redirectArgs) == 0)
1928 static int tested = 0, value = 0;
1931 if (systemf("lrzip -V %s", redirectArgs) == 0)
1938 * Can this platform run the lzip program?
1943 static int tested = 0, value = 0;
1946 if (systemf("lzip -V %s", redirectArgs) == 0)
1953 * Can this platform run the lzma program?
1958 static int tested = 0, value = 0;
1961 if (systemf("lzma -V %s", redirectArgs) == 0)
1968 * Can this platform run the lzop program?
1973 static int tested = 0, value = 0;
1976 if (systemf("lzop -V %s", redirectArgs) == 0)
1983 * Can this platform run the xz program?
1988 static int tested = 0, value = 0;
1991 if (systemf("xz -V %s", redirectArgs) == 0)
1998 * Can this filesystem handle nodump flags.
2000 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2005 const char *path = "cannodumptest";
2008 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2009 if (chflags(path, UF_NODUMP) < 0)
2011 if (stat(path, &sb) < 0)
2013 if (sb.st_flags & UF_NODUMP)
2018 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2019 && defined(EXT2_NODUMP_FL)
2024 const char *path = "cannodumptest";
2027 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2028 fd = open(path, O_RDONLY | O_NONBLOCK);
2031 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2034 flags |= EXT2_NODUMP_FL;
2035 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2039 fd = open(path, O_RDONLY | O_NONBLOCK);
2042 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2046 if (flags & EXT2_NODUMP_FL)
2062 * Sleep as needed; useful for verifying disk timestamp changes by
2063 * ensuring that the wall-clock time has actually changed before we
2064 * go back to re-read something from disk.
2067 sleepUntilAfter(time_t t)
2069 while (t >= time(NULL))
2070 #if defined(_WIN32) && !defined(__CYGWIN__)
2078 * Call standard system() call, but build up the command line using
2079 * sprintf() conventions.
2082 systemf(const char *fmt, ...)
2089 vsprintf(buff, fmt, ap);
2090 if (verbosity > VERBOSITY_FULL)
2091 logprintf("Cmd: %s\n", buff);
2098 * Slurp a file into memory for ease of comparison and testing.
2099 * Returns size of file in 'sizep' if non-NULL, null-terminates
2100 * data in memory for ease of use.
2103 slurpfile(size_t * sizep, const char *fmt, ...)
2105 char filename[8192];
2114 vsprintf(filename, fmt, ap);
2117 f = fopen(filename, "rb");
2119 /* Note: No error; non-existent file is okay here. */
2122 r = fstat(fileno(f), &st);
2124 logprintf("Can't stat file %s\n", filename);
2128 p = malloc((size_t)st.st_size + 1);
2130 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2131 (long int)st.st_size, filename);
2135 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2136 if (bytes_read < st.st_size) {
2137 logprintf("Can't read file %s\n", filename);
2142 p[st.st_size] = '\0';
2144 *sizep = (size_t)st.st_size;
2149 /* Read a uuencoded file from the reference directory, decode, and
2150 * write the result into the current directory. */
2151 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2153 extract_reference_file(const char *name)
2158 sprintf(buff, "%s/%s.uu", refdir, name);
2159 in = fopen(buff, "r");
2160 failure("Couldn't open reference file %s", buff);
2164 /* Read up to and including the 'begin' line. */
2166 if (fgets(buff, sizeof(buff), in) == NULL) {
2167 /* TODO: This is a failure. */
2170 if (memcmp(buff, "begin ", 6) == 0)
2173 /* Now, decode the rest and write it. */
2174 /* Not a lot of error checking here; the input better be right. */
2175 out = fopen(name, "wb");
2176 while (fgets(buff, sizeof(buff), in) != NULL) {
2180 if (memcmp(buff, "end", 3) == 0)
2183 bytes = UUDECODE(*p++);
2186 /* Write out 1-3 bytes from that. */
2188 n = UUDECODE(*p++) << 18;
2189 n |= UUDECODE(*p++) << 12;
2190 fputc(n >> 16, out);
2194 n |= UUDECODE(*p++) << 6;
2195 fputc((n >> 8) & 0xFF, out);
2199 n |= UUDECODE(*p++);
2200 fputc(n & 0xFF, out);
2210 is_LargeInode(const char *file)
2212 #if defined(_WIN32) && !defined(__CYGWIN__)
2213 BY_HANDLE_FILE_INFORMATION bhfi;
2216 r = my_GetFileInformationByName(file, &bhfi);
2219 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2224 if (stat(file, &st) < 0)
2226 ino = (int64_t)st.st_ino;
2227 return (ino > 0xffffffff);
2232 extract_reference_files(const char **names)
2234 while (names && *names)
2235 extract_reference_file(*names++);
2245 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2247 * DEFINE_TEST(test_function)
2251 /* Use "list.h" to declare all of the test functions. */
2253 #define DEFINE_TEST(name) void name(void);
2256 /* Use "list.h" to create a list of all tests (functions and names). */
2258 #define DEFINE_TEST(n) { n, #n, 0 },
2259 struct test_list_t tests[] = {
2264 * Summarize repeated failures in the just-completed test.
2267 test_summarize(int failed)
2271 switch (verbosity) {
2272 case VERBOSITY_SUMMARY_ONLY:
2273 printf(failed ? "E" : ".");
2276 case VERBOSITY_PASSFAIL:
2277 printf(failed ? "FAIL\n" : "ok\n");
2281 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2283 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2284 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2285 logprintf("%s:%d: Summary: Failed %d times\n",
2286 failed_filename, i, failed_lines[i].count);
2288 /* Clear the failure history for the next file. */
2289 failed_filename = NULL;
2290 memset(failed_lines, 0, sizeof(failed_lines));
2294 * Actually run a single test, with appropriate setup and cleanup.
2297 test_run(int i, const char *tmpdir)
2300 char logfilename[64];
2301 int failures_before = failures;
2304 switch (verbosity) {
2305 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2307 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2308 printf("%3d: %-50s", i, tests[i].name);
2311 default: /* Title of test, details will follow */
2312 printf("%3d: %s\n", i, tests[i].name);
2315 /* Chdir to the top-level work directory. */
2316 if (!assertChdir(tmpdir)) {
2318 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2321 /* Create a log file for this test. */
2322 sprintf(logfilename, "%s.log", tests[i].name);
2323 logfile = fopen(logfilename, "w");
2324 fprintf(logfile, "%s\n\n", tests[i].name);
2325 /* Chdir() to a work dir for this specific test. */
2326 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2327 testworkdir = workdir;
2328 if (!assertMakeDir(testworkdir, 0755)
2329 || !assertChdir(testworkdir)) {
2331 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2334 /* Explicitly reset the locale before each test. */
2335 setlocale(LC_ALL, "C");
2336 /* Record the umask before we run the test. */
2337 umask(oldumask = umask(0));
2339 * Run the actual test.
2343 * Clean up and report afterwards.
2349 setlocale(LC_ALL, "C");
2350 /* Reset directory. */
2351 if (!assertChdir(tmpdir)) {
2352 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2356 /* Report per-test summaries. */
2357 tests[i].failures = failures - failures_before;
2358 test_summarize(tests[i].failures);
2359 /* Close the per-test log file. */
2362 /* If there were no failures, we can remove the work dir and logfile. */
2363 if (tests[i].failures == 0) {
2364 if (!keep_temp_files && assertChdir(tmpdir)) {
2365 #if defined(_WIN32) && !defined(__CYGWIN__)
2366 /* Make sure not to leave empty directories.
2367 * Sometimes a processing of closing files used by tests
2368 * is not done, then rmdir will be failed and it will
2369 * leave a empty test directory. So we should wait a few
2370 * seconds and retry rmdir. */
2372 for (t = 0; t < 10; t++) {
2375 r = systemf("rmdir /S /Q %s", tests[i].name);
2379 systemf("del %s", logfilename);
2381 systemf("rm -rf %s", tests[i].name);
2382 systemf("rm %s", logfilename);
2386 /* Return appropriate status. */
2387 return (tests[i].failures);
2393 * MAIN and support routines.
2399 usage(const char *program)
2401 static const int limit = sizeof(tests) / sizeof(tests[0]);
2404 printf("Usage: %s [options] <test> <test> ...\n", program);
2405 printf("Default is to run all tests.\n");
2406 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2407 printf("Options:\n");
2408 printf(" -d Dump core after any failure, for debugging.\n");
2409 printf(" -k Keep all temp files.\n");
2410 printf(" Default: temp files for successful tests deleted.\n");
2412 printf(" -p <path> Path to executable to be tested.\n");
2413 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2415 printf(" -q Quiet.\n");
2416 printf(" -r <dir> Path to dir containing reference files.\n");
2417 printf(" Default: Current directory.\n");
2418 printf(" -u Keep running specifies tests until one fails.\n");
2419 printf(" -v Verbose.\n");
2420 printf("Available tests:\n");
2421 for (i = 0; i < limit; i++)
2422 printf(" %d: %s\n", i, tests[i].name);
2427 get_refdir(const char *d)
2429 char tried[512] = { '\0' };
2433 /* If a dir was specified, try that */
2436 snprintf(buff, sizeof(buff), "%s", d);
2437 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2438 if (p != NULL) goto success;
2439 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2440 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2444 /* Get the current dir. */
2446 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2448 pwd = getcwd(NULL, 0);
2450 while (pwd[strlen(pwd) - 1] == '\n')
2451 pwd[strlen(pwd) - 1] = '\0';
2453 /* Look for a known file. */
2454 snprintf(buff, sizeof(buff), "%s", pwd);
2455 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2456 if (p != NULL) goto success;
2457 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2458 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2460 snprintf(buff, sizeof(buff), "%s/test", pwd);
2461 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2462 if (p != NULL) goto success;
2463 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2464 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2466 #if defined(LIBRARY)
2467 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
2469 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
2471 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2472 if (p != NULL) goto success;
2473 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2474 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2476 #if defined(PROGRAM_ALIAS)
2477 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
2478 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2479 if (p != NULL) goto success;
2480 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2481 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2484 if (memcmp(pwd, "/usr/obj", 8) == 0) {
2485 snprintf(buff, sizeof(buff), "%s", pwd + 8);
2486 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2487 if (p != NULL) goto success;
2488 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2489 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2491 snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
2492 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2493 if (p != NULL) goto success;
2494 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2495 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2499 printf("Unable to locate known reference file %s\n", KNOWNREF);
2500 printf(" Checked following directories:\n%s\n", tried);
2501 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2509 return strdup(buff);
2513 main(int argc, char **argv)
2515 static const int limit = sizeof(tests) / sizeof(tests[0]);
2516 int test_set[sizeof(tests) / sizeof(tests[0])];
2517 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2519 char *refdir_alloc = NULL;
2520 const char *progname;
2522 const char *tmp, *option_arg, *p;
2523 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2524 char tmpdir_timestamp[256];
2526 (void)argc; /* UNUSED */
2528 /* Get the current dir. */
2530 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2532 pwd = getcwd(NULL, 0);
2534 while (pwd[strlen(pwd) - 1] == '\n')
2535 pwd[strlen(pwd) - 1] = '\0';
2537 #if defined(HAVE__CrtSetReportMode)
2538 /* To stop to run the default invalid parameter handler. */
2539 _set_invalid_parameter_handler(invalid_parameter_handler);
2540 /* Disable annoying assertion message box. */
2541 _CrtSetReportMode(_CRT_ASSERT, 0);
2545 * Name of this program, used to build root of our temp directory
2548 progname = p = argv[0];
2549 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2551 fprintf(stderr, "ERROR: Out of memory.");
2554 strcpy(testprogdir, progname);
2555 while (*p != '\0') {
2556 /* Support \ or / dir separators for Windows compat. */
2557 if (*p == '/' || *p == '\\')
2565 testprogdir[i] = '\0';
2566 #if defined(_WIN32) && !defined(__CYGWIN__)
2567 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2568 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2569 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2570 testprogdir[1] == ':' &&
2571 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
2573 if (testprogdir[0] != '/')
2576 /* Fixup path for relative directories. */
2577 if ((testprogdir = (char *)realloc(testprogdir,
2578 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2580 fprintf(stderr, "ERROR: Out of memory.");
2583 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2584 strlen(testprogdir));
2585 memcpy(testprogdir, pwd, strlen(pwd));
2586 testprogdir[strlen(pwd)] = '/';
2590 /* Get the target program from environment, if available. */
2591 testprogfile = getenv(ENVBASE);
2594 if (getenv("TMPDIR") != NULL)
2595 tmp = getenv("TMPDIR");
2596 else if (getenv("TMP") != NULL)
2597 tmp = getenv("TMP");
2598 else if (getenv("TEMP") != NULL)
2599 tmp = getenv("TEMP");
2600 else if (getenv("TEMPDIR") != NULL)
2601 tmp = getenv("TEMPDIR");
2605 /* Allow -d to be controlled through the environment. */
2606 if (getenv(ENVBASE "_DEBUG") != NULL)
2607 dump_on_failure = 1;
2609 /* Allow -v to be controlled through the environment. */
2610 if (getenv("_VERBOSITY_LEVEL") != NULL)
2612 vlevel = getenv("_VERBOSITY_LEVEL");
2613 verbosity = atoi(vlevel);
2614 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2616 /* Unsupported verbosity levels are silently ignored */
2618 verbosity = VERBOSITY_PASSFAIL;
2622 /* Get the directory holding test files from environment. */
2623 refdir = getenv(ENVBASE "_TEST_FILES");
2626 * Parse options, without using getopt(), which isn't available
2629 ++argv; /* Skip program name */
2630 while (*argv != NULL) {
2635 while (*p != '\0') {
2638 /* If 'opt' takes an argument, parse that. */
2639 if (option == 'p' || option == 'r') {
2642 else if (*argv == NULL) {
2644 "Option -%c requires argument.\n",
2648 option_arg = *argv++;
2649 p = ""; /* End of this option word. */
2652 /* Now, handle the option. */
2655 dump_on_failure = 1;
2658 keep_temp_files = 1;
2662 testprogfile = option_arg;
2664 fprintf(stderr, "-p option not permitted\n");
2673 refdir = option_arg;
2683 fprintf(stderr, "Unrecognized option '%c'\n",
2691 * Sanity-check that our options make sense.
2694 if (testprogfile == NULL)
2696 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2697 strlen(PROGRAM) + 1)) == NULL)
2699 fprintf(stderr, "ERROR: Out of memory.");
2702 strcpy(tmp2, testprogdir);
2704 strcat(tmp2, PROGRAM);
2705 testprogfile = tmp2;
2710 #if defined(_WIN32) && !defined(__CYGWIN__)
2711 /* Command.com sometimes rejects '/' separators. */
2712 testprg = strdup(testprogfile);
2713 for (i = 0; testprg[i] != '\0'; i++) {
2714 if (testprg[i] == '/')
2717 testprogfile = testprg;
2719 /* Quote the name that gets put into shell command lines. */
2720 testprg = malloc(strlen(testprogfile) + 3);
2721 strcpy(testprg, "\"");
2722 strcat(testprg, testprogfile);
2723 strcat(testprg, "\"");
2728 #if !defined(_WIN32) && defined(SIGPIPE)
2729 { /* Ignore SIGPIPE signals */
2730 struct sigaction sa;
2731 sa.sa_handler = SIG_IGN;
2732 sigemptyset(&sa.sa_mask);
2734 sigaction(SIGPIPE, &sa, NULL);
2739 * Create a temp directory for the following tests.
2740 * Include the time the tests started as part of the name,
2741 * to make it easier to track the results of multiple tests.
2744 for (i = 0; ; i++) {
2745 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2746 "%Y-%m-%dT%H.%M.%S",
2748 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2749 tmpdir_timestamp, i);
2750 if (assertMakeDir(tmpdir,0755))
2754 "ERROR: Unable to create temp directory %s\n",
2761 * If the user didn't specify a directory for locating
2762 * reference files, try to find the reference files in
2763 * the "usual places."
2765 refdir = refdir_alloc = get_refdir(refdir);
2768 * Banner with basic information.
2771 printf("If tests fail or crash, details will be in:\n");
2772 printf(" %s\n", tmpdir);
2774 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2775 printf("Reference files will be read from: %s\n", refdir);
2777 printf("Running tests on: %s\n", testprog);
2779 printf("Exercising: ");
2781 printf("%s\n", EXTRA_VERSION);
2788 * Run some or all of the individual tests.
2796 test_num = get_test_set(test_set, limit, *argv, tests);
2798 printf("*** INVALID Test %s\n", *argv);
2804 for (i = 0; i < test_num; i++) {
2806 if (test_run(test_set[i], tmpdir)) {
2814 } while (*argv != NULL);
2815 } while (until_failure);
2818 /* Must be freed after all tests run */
2824 * Report summary statistics.
2826 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2828 printf("Totals:\n");
2829 printf(" Tests run: %8d\n", tests_run);
2830 printf(" Tests failed: %8d\n", tests_failed);
2831 printf(" Assertions checked:%8d\n", assertions);
2832 printf(" Assertions failed: %8d\n", failures);
2833 printf(" Skips reported: %8d\n", skips);
2837 printf("Failing tests:\n");
2838 for (i = 0; i < limit; ++i) {
2839 if (tests[i].failures)
2840 printf(" %d: %s (%d failures)\n", i,
2841 tests[i].name, tests[i].failures);
2844 printf("Details for failing tests: %s\n", tmpdir);
2847 if (verbosity == VERBOSITY_SUMMARY_ONLY)
2849 printf("%d tests passed, no failures\n", tests_run);
2854 /* If the final tmpdir is empty, we can remove it. */
2855 /* This should be the usual case when all tests succeed. */
2859 return (tests_failed ? 1 : 0);