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 /* Verify mode of 'pathname'. */
1401 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1406 assertion_count(file, line);
1407 #if defined(_WIN32) && !defined(__CYGWIN__)
1408 failure_start(file, line, "assertFileMode not yet implemented for Windows");
1412 r = lstat(pathname, &st);
1413 mode = (int)(st.st_mode & 0777);
1415 if (r == 0 && mode == expected_mode)
1417 failure_start(file, line, "File %s has mode %o, expected %o",
1418 pathname, mode, expected_mode);
1420 failure_finish(NULL);
1424 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1426 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1431 #if defined(_WIN32) && !defined(__CYGWIN__)
1432 (void)mode; /* UNUSED */
1434 assertion_count(file, line);
1435 r = lstat(pathname, &st);
1437 failure_start(file, line, "Dir should exist: %s", pathname);
1438 failure_finish(NULL);
1441 if (!S_ISDIR(st.st_mode)) {
1442 failure_start(file, line, "%s is not a dir", pathname);
1443 failure_finish(NULL);
1446 #if !defined(_WIN32) || defined(__CYGWIN__)
1447 /* Windows doesn't handle permissions the same way as POSIX,
1448 * so just ignore the mode tests. */
1449 /* TODO: Can we do better here? */
1450 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1451 failure_start(file, line, "Dir %s has wrong mode", pathname);
1452 logprintf(" Expected: 0%3o\n", mode);
1453 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1454 failure_finish(NULL);
1461 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1462 * verify that too. */
1464 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1469 #if defined(_WIN32) && !defined(__CYGWIN__)
1470 (void)mode; /* UNUSED */
1472 assertion_count(file, line);
1473 r = lstat(pathname, &st);
1474 if (r != 0 || !S_ISREG(st.st_mode)) {
1475 failure_start(file, line, "File should exist: %s", pathname);
1476 failure_finish(NULL);
1479 #if !defined(_WIN32) || defined(__CYGWIN__)
1480 /* Windows doesn't handle permissions the same way as POSIX,
1481 * so just ignore the mode tests. */
1482 /* TODO: Can we do better here? */
1483 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1484 failure_start(file, line, "File %s has wrong mode", pathname);
1485 logprintf(" Expected: 0%3o\n", mode);
1486 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1487 failure_finish(NULL);
1494 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1495 * non-NULL, verify that the symlink has those contents. */
1497 is_symlink(const char *file, int line,
1498 const char *pathname, const char *contents)
1500 #if defined(_WIN32) && !defined(__CYGWIN__)
1501 (void)pathname; /* UNUSED */
1502 (void)contents; /* UNUSED */
1503 assertion_count(file, line);
1504 /* Windows sort-of has real symlinks, but they're only usable
1505 * by privileged users and are crippled even then, so there's
1506 * really not much point in bothering with this. */
1514 assertion_count(file, line);
1515 r = lstat(pathname, &st);
1517 failure_start(file, line,
1518 "Symlink should exist: %s", pathname);
1519 failure_finish(NULL);
1522 if (!S_ISLNK(st.st_mode))
1524 if (contents == NULL)
1526 linklen = readlink(pathname, buff, sizeof(buff));
1528 failure_start(file, line, "Can't read symlink %s", pathname);
1529 failure_finish(NULL);
1532 buff[linklen] = '\0';
1533 if (strcmp(buff, contents) != 0)
1539 /* Assert that path is a symlink that (optionally) contains contents. */
1541 assertion_is_symlink(const char *file, int line,
1542 const char *path, const char *contents)
1544 if (is_symlink(file, line, path, contents))
1547 failure_start(file, line, "File %s is not a symlink to %s",
1550 failure_start(file, line, "File %s is not a symlink", path);
1551 failure_finish(NULL);
1556 /* Create a directory and report any errors. */
1558 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1560 assertion_count(file, line);
1561 #if defined(_WIN32) && !defined(__CYGWIN__)
1562 (void)mode; /* UNUSED */
1563 if (0 == _mkdir(dirname))
1566 if (0 == mkdir(dirname, mode))
1569 failure_start(file, line, "Could not create directory %s", dirname);
1570 failure_finish(NULL);
1574 /* Create a file with the specified contents and report any failures. */
1576 assertion_make_file(const char *file, int line,
1577 const char *path, int mode, int csize, const void *contents)
1579 #if defined(_WIN32) && !defined(__CYGWIN__)
1580 /* TODO: Rework this to set file mode as well. */
1582 (void)mode; /* UNUSED */
1583 assertion_count(file, line);
1584 f = fopen(path, "wb");
1586 failure_start(file, line, "Could not create file %s", path);
1587 failure_finish(NULL);
1590 if (contents != NULL) {
1594 wsize = strlen(contents);
1596 wsize = (size_t)csize;
1597 if (wsize != fwrite(contents, 1, wsize, f)) {
1599 failure_start(file, line,
1600 "Could not write file %s", path);
1601 failure_finish(NULL);
1609 assertion_count(file, line);
1610 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1612 failure_start(file, line, "Could not create %s", path);
1613 failure_finish(NULL);
1616 if (contents != NULL) {
1620 wsize = (ssize_t)strlen(contents);
1622 wsize = (ssize_t)csize;
1623 if (wsize != write(fd, contents, wsize)) {
1625 failure_start(file, line,
1626 "Could not write to %s", path);
1627 failure_finish(NULL);
1636 /* Create a hardlink and report any failures. */
1638 assertion_make_hardlink(const char *file, int line,
1639 const char *newpath, const char *linkto)
1643 assertion_count(file, line);
1644 #if defined(_WIN32) && !defined(__CYGWIN__)
1645 succeeded = my_CreateHardLinkA(newpath, linkto);
1647 succeeded = !link(linkto, newpath);
1653 failure_start(file, line, "Could not create hardlink");
1654 logprintf(" New link: %s\n", newpath);
1655 logprintf(" Old name: %s\n", linkto);
1656 failure_finish(NULL);
1660 /* Create a symlink and report any failures. */
1662 assertion_make_symlink(const char *file, int line,
1663 const char *newpath, const char *linkto)
1665 #if defined(_WIN32) && !defined(__CYGWIN__)
1666 int targetIsDir = 0; /* TODO: Fix this */
1667 assertion_count(file, line);
1668 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1671 assertion_count(file, line);
1672 if (0 == symlink(linkto, newpath))
1675 failure_start(file, line, "Could not create symlink");
1676 logprintf(" New link: %s\n", newpath);
1677 logprintf(" Old name: %s\n", linkto);
1678 failure_finish(NULL);
1682 /* Set umask, report failures. */
1684 assertion_umask(const char *file, int line, int mask)
1686 assertion_count(file, line);
1687 (void)file; /* UNUSED */
1688 (void)line; /* UNUSED */
1693 /* Set times, report failures. */
1695 assertion_utimes(const char *file, int line,
1696 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1700 #if defined(_WIN32) && !defined(__CYGWIN__)
1701 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1702 + (((nsec)/1000)*10))
1704 ULARGE_INTEGER wintm;
1705 FILETIME fatime, fmtime;
1706 FILETIME *pat, *pmt;
1708 assertion_count(file, line);
1709 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1710 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1711 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1712 if (h == INVALID_HANDLE_VALUE) {
1713 failure_start(file, line, "Can't access %s\n", pathname);
1714 failure_finish(NULL);
1718 if (at > 0 || at_nsec > 0) {
1719 wintm.QuadPart = WINTIME(at, at_nsec);
1720 fatime.dwLowDateTime = wintm.LowPart;
1721 fatime.dwHighDateTime = wintm.HighPart;
1725 if (mt > 0 || mt_nsec > 0) {
1726 wintm.QuadPart = WINTIME(mt, mt_nsec);
1727 fmtime.dwLowDateTime = wintm.LowPart;
1728 fmtime.dwHighDateTime = wintm.HighPart;
1732 if (pat != NULL || pmt != NULL)
1733 r = SetFileTime(h, NULL, pat, pmt);
1738 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1739 failure_finish(NULL);
1743 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1745 struct timeval times[2];
1747 #if !defined(__FreeBSD__)
1748 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1750 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1753 r = lstat(pathname, &st);
1755 failure_start(file, line, "Can't stat %s\n", pathname);
1756 failure_finish(NULL);
1760 if (mt == 0 && mt_nsec == 0) {
1762 #if defined(__FreeBSD__)
1763 mt_nsec = st.st_mtimespec.tv_nsec;
1764 /* FreeBSD generally only stores to microsecond res, so round. */
1765 mt_nsec = (mt_nsec / 1000) * 1000;
1768 if (at == 0 && at_nsec == 0) {
1770 #if defined(__FreeBSD__)
1771 at_nsec = st.st_atimespec.tv_nsec;
1772 /* FreeBSD generally only stores to microsecond res, so round. */
1773 at_nsec = (at_nsec / 1000) * 1000;
1777 times[1].tv_sec = mt;
1778 times[1].tv_usec = mt_nsec / 1000;
1780 times[0].tv_sec = at;
1781 times[0].tv_usec = at_nsec / 1000;
1784 r = lutimes(pathname, times);
1786 r = utimes(pathname, times);
1789 failure_start(file, line, "Can't utimes %s\n", pathname);
1790 failure_finish(NULL);
1794 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1797 /* Set nodump, report failures. */
1799 assertion_nodump(const char *file, int line, const char *pathname)
1801 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1804 assertion_count(file, line);
1805 r = chflags(pathname, UF_NODUMP);
1807 failure_start(file, line, "Can't set nodump %s\n", pathname);
1808 failure_finish(NULL);
1811 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1812 && defined(EXT2_NODUMP_FL)
1815 assertion_count(file, line);
1816 fd = open(pathname, O_RDONLY | O_NONBLOCK);
1818 failure_start(file, line, "Can't open %s\n", pathname);
1819 failure_finish(NULL);
1822 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1824 failure_start(file, line, "Can't get flags %s\n", pathname);
1825 failure_finish(NULL);
1828 flags |= EXT2_NODUMP_FL;
1829 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1831 failure_start(file, line, "Can't set nodump %s\n", pathname);
1832 failure_finish(NULL);
1837 (void)pathname; /* UNUSED */
1838 assertion_count(file, line);
1845 * UTILITIES for use by tests.
1850 * Check whether platform supports symlinks. This is intended
1851 * for tests to use in deciding whether to bother testing symlink
1852 * support; if the platform doesn't support symlinks, there's no point
1853 * in checking whether the program being tested can create them.
1855 * Note that the first time this test is called, we actually go out to
1856 * disk to create and verify a symlink. This is necessary because
1857 * symlink support is actually a property of a particular filesystem
1858 * and can thus vary between directories on a single system. After
1859 * the first call, this returns the cached result from memory, so it's
1860 * safe to call it as often as you wish.
1865 /* Remember the test result */
1866 static int value = 0, tested = 0;
1871 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1872 /* Note: Cygwin has its own symlink() emulation that does not
1873 * use the Win32 CreateSymbolicLink() function. */
1874 #if defined(_WIN32) && !defined(__CYGWIN__)
1875 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1876 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1878 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1879 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1884 /* Platform-dependent options for hiding the output of a subcommand. */
1885 #if defined(_WIN32) && !defined(__CYGWIN__)
1886 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1888 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1891 * Can this platform run the bzip2 program?
1896 static int tested = 0, value = 0;
1899 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
1906 * Can this platform run the grzip program?
1911 static int tested = 0, value = 0;
1914 if (systemf("grzip -V %s", redirectArgs) == 0)
1921 * Can this platform run the gzip program?
1926 static int tested = 0, value = 0;
1929 if (systemf("gzip -V %s", redirectArgs) == 0)
1936 * Can this platform run the lrzip program?
1939 canRunCommand(const char *cmd)
1941 static int tested = 0, value = 0;
1944 if (systemf("%s %s", cmd, redirectArgs) == 0)
1953 static int tested = 0, value = 0;
1956 if (systemf("lrzip -V %s", redirectArgs) == 0)
1963 * Can this platform run the lzip program?
1968 static int tested = 0, value = 0;
1971 if (systemf("lzip -V %s", redirectArgs) == 0)
1978 * Can this platform run the lzma program?
1983 static int tested = 0, value = 0;
1986 if (systemf("lzma -V %s", redirectArgs) == 0)
1993 * Can this platform run the lzop program?
1998 static int tested = 0, value = 0;
2001 if (systemf("lzop -V %s", redirectArgs) == 0)
2008 * Can this platform run the xz program?
2013 static int tested = 0, value = 0;
2016 if (systemf("xz -V %s", redirectArgs) == 0)
2023 * Can this filesystem handle nodump flags.
2025 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2030 const char *path = "cannodumptest";
2033 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2034 if (chflags(path, UF_NODUMP) < 0)
2036 if (stat(path, &sb) < 0)
2038 if (sb.st_flags & UF_NODUMP)
2043 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2044 && defined(EXT2_NODUMP_FL)
2049 const char *path = "cannodumptest";
2052 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2053 fd = open(path, O_RDONLY | O_NONBLOCK);
2056 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2059 flags |= EXT2_NODUMP_FL;
2060 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2064 fd = open(path, O_RDONLY | O_NONBLOCK);
2067 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2071 if (flags & EXT2_NODUMP_FL)
2087 * Sleep as needed; useful for verifying disk timestamp changes by
2088 * ensuring that the wall-clock time has actually changed before we
2089 * go back to re-read something from disk.
2092 sleepUntilAfter(time_t t)
2094 while (t >= time(NULL))
2095 #if defined(_WIN32) && !defined(__CYGWIN__)
2103 * Call standard system() call, but build up the command line using
2104 * sprintf() conventions.
2107 systemf(const char *fmt, ...)
2114 vsprintf(buff, fmt, ap);
2115 if (verbosity > VERBOSITY_FULL)
2116 logprintf("Cmd: %s\n", buff);
2123 * Slurp a file into memory for ease of comparison and testing.
2124 * Returns size of file in 'sizep' if non-NULL, null-terminates
2125 * data in memory for ease of use.
2128 slurpfile(size_t * sizep, const char *fmt, ...)
2130 char filename[8192];
2139 vsprintf(filename, fmt, ap);
2142 f = fopen(filename, "rb");
2144 /* Note: No error; non-existent file is okay here. */
2147 r = fstat(fileno(f), &st);
2149 logprintf("Can't stat file %s\n", filename);
2153 p = malloc((size_t)st.st_size + 1);
2155 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2156 (long int)st.st_size, filename);
2160 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2161 if (bytes_read < st.st_size) {
2162 logprintf("Can't read file %s\n", filename);
2167 p[st.st_size] = '\0';
2169 *sizep = (size_t)st.st_size;
2174 /* Read a uuencoded file from the reference directory, decode, and
2175 * write the result into the current directory. */
2176 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2178 extract_reference_file(const char *name)
2183 sprintf(buff, "%s/%s.uu", refdir, name);
2184 in = fopen(buff, "r");
2185 failure("Couldn't open reference file %s", buff);
2189 /* Read up to and including the 'begin' line. */
2191 if (fgets(buff, sizeof(buff), in) == NULL) {
2192 /* TODO: This is a failure. */
2195 if (memcmp(buff, "begin ", 6) == 0)
2198 /* Now, decode the rest and write it. */
2199 /* Not a lot of error checking here; the input better be right. */
2200 out = fopen(name, "wb");
2201 while (fgets(buff, sizeof(buff), in) != NULL) {
2205 if (memcmp(buff, "end", 3) == 0)
2208 bytes = UUDECODE(*p++);
2211 /* Write out 1-3 bytes from that. */
2213 n = UUDECODE(*p++) << 18;
2214 n |= UUDECODE(*p++) << 12;
2215 fputc(n >> 16, out);
2219 n |= UUDECODE(*p++) << 6;
2220 fputc((n >> 8) & 0xFF, out);
2224 n |= UUDECODE(*p++);
2225 fputc(n & 0xFF, out);
2235 is_LargeInode(const char *file)
2237 #if defined(_WIN32) && !defined(__CYGWIN__)
2238 BY_HANDLE_FILE_INFORMATION bhfi;
2241 r = my_GetFileInformationByName(file, &bhfi);
2244 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2249 if (stat(file, &st) < 0)
2251 ino = (int64_t)st.st_ino;
2252 return (ino > 0xffffffff);
2257 extract_reference_files(const char **names)
2259 while (names && *names)
2260 extract_reference_file(*names++);
2270 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2272 * DEFINE_TEST(test_function)
2276 /* Use "list.h" to declare all of the test functions. */
2278 #define DEFINE_TEST(name) void name(void);
2281 /* Use "list.h" to create a list of all tests (functions and names). */
2283 #define DEFINE_TEST(n) { n, #n, 0 },
2284 struct test_list_t tests[] = {
2289 * Summarize repeated failures in the just-completed test.
2292 test_summarize(int failed)
2296 switch (verbosity) {
2297 case VERBOSITY_SUMMARY_ONLY:
2298 printf(failed ? "E" : ".");
2301 case VERBOSITY_PASSFAIL:
2302 printf(failed ? "FAIL\n" : "ok\n");
2306 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2308 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2309 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2310 logprintf("%s:%d: Summary: Failed %d times\n",
2311 failed_filename, i, failed_lines[i].count);
2313 /* Clear the failure history for the next file. */
2314 failed_filename = NULL;
2315 memset(failed_lines, 0, sizeof(failed_lines));
2319 * Actually run a single test, with appropriate setup and cleanup.
2322 test_run(int i, const char *tmpdir)
2325 char logfilename[64];
2326 int failures_before = failures;
2329 switch (verbosity) {
2330 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2332 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2333 printf("%3d: %-50s", i, tests[i].name);
2336 default: /* Title of test, details will follow */
2337 printf("%3d: %s\n", i, tests[i].name);
2340 /* Chdir to the top-level work directory. */
2341 if (!assertChdir(tmpdir)) {
2343 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2346 /* Create a log file for this test. */
2347 sprintf(logfilename, "%s.log", tests[i].name);
2348 logfile = fopen(logfilename, "w");
2349 fprintf(logfile, "%s\n\n", tests[i].name);
2350 /* Chdir() to a work dir for this specific test. */
2351 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2352 testworkdir = workdir;
2353 if (!assertMakeDir(testworkdir, 0755)
2354 || !assertChdir(testworkdir)) {
2356 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2359 /* Explicitly reset the locale before each test. */
2360 setlocale(LC_ALL, "C");
2361 /* Record the umask before we run the test. */
2362 umask(oldumask = umask(0));
2364 * Run the actual test.
2368 * Clean up and report afterwards.
2374 setlocale(LC_ALL, "C");
2375 /* Reset directory. */
2376 if (!assertChdir(tmpdir)) {
2377 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2381 /* Report per-test summaries. */
2382 tests[i].failures = failures - failures_before;
2383 test_summarize(tests[i].failures);
2384 /* Close the per-test log file. */
2387 /* If there were no failures, we can remove the work dir and logfile. */
2388 if (tests[i].failures == 0) {
2389 if (!keep_temp_files && assertChdir(tmpdir)) {
2390 #if defined(_WIN32) && !defined(__CYGWIN__)
2391 /* Make sure not to leave empty directories.
2392 * Sometimes a processing of closing files used by tests
2393 * is not done, then rmdir will be failed and it will
2394 * leave a empty test directory. So we should wait a few
2395 * seconds and retry rmdir. */
2397 for (t = 0; t < 10; t++) {
2400 r = systemf("rmdir /S /Q %s", tests[i].name);
2404 systemf("del %s", logfilename);
2406 systemf("rm -rf %s", tests[i].name);
2407 systemf("rm %s", logfilename);
2411 /* Return appropriate status. */
2412 return (tests[i].failures);
2418 * MAIN and support routines.
2424 usage(const char *program)
2426 static const int limit = sizeof(tests) / sizeof(tests[0]);
2429 printf("Usage: %s [options] <test> <test> ...\n", program);
2430 printf("Default is to run all tests.\n");
2431 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2432 printf("Options:\n");
2433 printf(" -d Dump core after any failure, for debugging.\n");
2434 printf(" -k Keep all temp files.\n");
2435 printf(" Default: temp files for successful tests deleted.\n");
2437 printf(" -p <path> Path to executable to be tested.\n");
2438 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2440 printf(" -q Quiet.\n");
2441 printf(" -r <dir> Path to dir containing reference files.\n");
2442 printf(" Default: Current directory.\n");
2443 printf(" -u Keep running specifies tests until one fails.\n");
2444 printf(" -v Verbose.\n");
2445 printf("Available tests:\n");
2446 for (i = 0; i < limit; i++)
2447 printf(" %d: %s\n", i, tests[i].name);
2452 get_refdir(const char *d)
2454 char tried[512] = { '\0' };
2458 /* If a dir was specified, try that */
2461 snprintf(buff, sizeof(buff), "%s", d);
2462 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2463 if (p != NULL) goto success;
2464 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2465 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2469 /* Get the current dir. */
2471 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2473 pwd = getcwd(NULL, 0);
2475 while (pwd[strlen(pwd) - 1] == '\n')
2476 pwd[strlen(pwd) - 1] = '\0';
2478 /* Look for a known file. */
2479 snprintf(buff, sizeof(buff), "%s", pwd);
2480 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2481 if (p != NULL) goto success;
2482 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2483 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2485 snprintf(buff, sizeof(buff), "%s/test", pwd);
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 #if defined(LIBRARY)
2492 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
2494 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
2496 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2497 if (p != NULL) goto success;
2498 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2499 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2501 #if defined(PROGRAM_ALIAS)
2502 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
2503 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2504 if (p != NULL) goto success;
2505 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2506 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2509 if (memcmp(pwd, "/usr/obj", 8) == 0) {
2510 snprintf(buff, sizeof(buff), "%s", pwd + 8);
2511 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2512 if (p != NULL) goto success;
2513 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2514 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2516 snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
2517 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2518 if (p != NULL) goto success;
2519 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2520 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2524 printf("Unable to locate known reference file %s\n", KNOWNREF);
2525 printf(" Checked following directories:\n%s\n", tried);
2526 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2534 return strdup(buff);
2538 main(int argc, char **argv)
2540 static const int limit = sizeof(tests) / sizeof(tests[0]);
2541 int test_set[sizeof(tests) / sizeof(tests[0])];
2542 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2544 char *refdir_alloc = NULL;
2545 const char *progname;
2547 const char *tmp, *option_arg, *p;
2548 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2549 char tmpdir_timestamp[256];
2551 (void)argc; /* UNUSED */
2553 /* Get the current dir. */
2555 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2557 pwd = getcwd(NULL, 0);
2559 while (pwd[strlen(pwd) - 1] == '\n')
2560 pwd[strlen(pwd) - 1] = '\0';
2562 #if defined(HAVE__CrtSetReportMode)
2563 /* To stop to run the default invalid parameter handler. */
2564 _set_invalid_parameter_handler(invalid_parameter_handler);
2565 /* Disable annoying assertion message box. */
2566 _CrtSetReportMode(_CRT_ASSERT, 0);
2570 * Name of this program, used to build root of our temp directory
2573 progname = p = argv[0];
2574 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2576 fprintf(stderr, "ERROR: Out of memory.");
2579 strcpy(testprogdir, progname);
2580 while (*p != '\0') {
2581 /* Support \ or / dir separators for Windows compat. */
2582 if (*p == '/' || *p == '\\')
2590 testprogdir[i] = '\0';
2591 #if defined(_WIN32) && !defined(__CYGWIN__)
2592 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2593 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2594 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2595 testprogdir[1] == ':' &&
2596 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
2598 if (testprogdir[0] != '/')
2601 /* Fixup path for relative directories. */
2602 if ((testprogdir = (char *)realloc(testprogdir,
2603 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2605 fprintf(stderr, "ERROR: Out of memory.");
2608 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2609 strlen(testprogdir));
2610 memcpy(testprogdir, pwd, strlen(pwd));
2611 testprogdir[strlen(pwd)] = '/';
2615 /* Get the target program from environment, if available. */
2616 testprogfile = getenv(ENVBASE);
2619 if (getenv("TMPDIR") != NULL)
2620 tmp = getenv("TMPDIR");
2621 else if (getenv("TMP") != NULL)
2622 tmp = getenv("TMP");
2623 else if (getenv("TEMP") != NULL)
2624 tmp = getenv("TEMP");
2625 else if (getenv("TEMPDIR") != NULL)
2626 tmp = getenv("TEMPDIR");
2630 /* Allow -d to be controlled through the environment. */
2631 if (getenv(ENVBASE "_DEBUG") != NULL)
2632 dump_on_failure = 1;
2634 /* Allow -v to be controlled through the environment. */
2635 if (getenv("_VERBOSITY_LEVEL") != NULL)
2637 vlevel = getenv("_VERBOSITY_LEVEL");
2638 verbosity = atoi(vlevel);
2639 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2641 /* Unsupported verbosity levels are silently ignored */
2643 verbosity = VERBOSITY_PASSFAIL;
2647 /* Get the directory holding test files from environment. */
2648 refdir = getenv(ENVBASE "_TEST_FILES");
2651 * Parse options, without using getopt(), which isn't available
2654 ++argv; /* Skip program name */
2655 while (*argv != NULL) {
2660 while (*p != '\0') {
2663 /* If 'opt' takes an argument, parse that. */
2664 if (option == 'p' || option == 'r') {
2667 else if (*argv == NULL) {
2669 "Option -%c requires argument.\n",
2673 option_arg = *argv++;
2674 p = ""; /* End of this option word. */
2677 /* Now, handle the option. */
2680 dump_on_failure = 1;
2683 keep_temp_files = 1;
2687 testprogfile = option_arg;
2689 fprintf(stderr, "-p option not permitted\n");
2698 refdir = option_arg;
2708 fprintf(stderr, "Unrecognized option '%c'\n",
2716 * Sanity-check that our options make sense.
2719 if (testprogfile == NULL)
2721 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2722 strlen(PROGRAM) + 1)) == NULL)
2724 fprintf(stderr, "ERROR: Out of memory.");
2727 strcpy(tmp2, testprogdir);
2729 strcat(tmp2, PROGRAM);
2730 testprogfile = tmp2;
2735 #if defined(_WIN32) && !defined(__CYGWIN__)
2736 /* Command.com sometimes rejects '/' separators. */
2737 testprg = strdup(testprogfile);
2738 for (i = 0; testprg[i] != '\0'; i++) {
2739 if (testprg[i] == '/')
2742 testprogfile = testprg;
2744 /* Quote the name that gets put into shell command lines. */
2745 testprg = malloc(strlen(testprogfile) + 3);
2746 strcpy(testprg, "\"");
2747 strcat(testprg, testprogfile);
2748 strcat(testprg, "\"");
2753 #if !defined(_WIN32) && defined(SIGPIPE)
2754 { /* Ignore SIGPIPE signals */
2755 struct sigaction sa;
2756 sa.sa_handler = SIG_IGN;
2757 sigemptyset(&sa.sa_mask);
2759 sigaction(SIGPIPE, &sa, NULL);
2764 * Create a temp directory for the following tests.
2765 * Include the time the tests started as part of the name,
2766 * to make it easier to track the results of multiple tests.
2769 for (i = 0; ; i++) {
2770 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2771 "%Y-%m-%dT%H.%M.%S",
2773 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2774 tmpdir_timestamp, i);
2775 if (assertMakeDir(tmpdir,0755))
2779 "ERROR: Unable to create temp directory %s\n",
2786 * If the user didn't specify a directory for locating
2787 * reference files, try to find the reference files in
2788 * the "usual places."
2790 refdir = refdir_alloc = get_refdir(refdir);
2793 * Banner with basic information.
2796 printf("If tests fail or crash, details will be in:\n");
2797 printf(" %s\n", tmpdir);
2799 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2800 printf("Reference files will be read from: %s\n", refdir);
2802 printf("Running tests on: %s\n", testprog);
2804 printf("Exercising: ");
2806 printf("%s\n", EXTRA_VERSION);
2813 * Run some or all of the individual tests.
2821 test_num = get_test_set(test_set, limit, *argv, tests);
2823 printf("*** INVALID Test %s\n", *argv);
2829 for (i = 0; i < test_num; i++) {
2831 if (test_run(test_set[i], tmpdir)) {
2839 } while (*argv != NULL);
2840 } while (until_failure);
2843 /* Must be freed after all tests run */
2849 * Report summary statistics.
2851 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2853 printf("Totals:\n");
2854 printf(" Tests run: %8d\n", tests_run);
2855 printf(" Tests failed: %8d\n", tests_failed);
2856 printf(" Assertions checked:%8d\n", assertions);
2857 printf(" Assertions failed: %8d\n", failures);
2858 printf(" Skips reported: %8d\n", skips);
2862 printf("Failing tests:\n");
2863 for (i = 0; i < limit; ++i) {
2864 if (tests[i].failures)
2865 printf(" %d: %s (%d failures)\n", i,
2866 tests[i].name, tests[i].failures);
2869 printf("Details for failing tests: %s\n", tmpdir);
2872 if (verbosity == VERBOSITY_SUMMARY_ONLY)
2874 printf("%d tests passed, no failures\n", tests_run);
2879 /* If the final tmpdir is empty, we can remove it. */
2880 /* This should be the usual case when all tests succeed. */
2884 return (tests_failed ? 1 : 0);