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_patterns_2.tar.uu"
65 #define ENVBASE "BSDTAR" /* Prefix for environment variables. */
66 #define PROGRAM "bsdtar" /* Name of program being tested. */
67 #define PROGRAM_ALIAS "tar" /* Generic alias for program */
68 #undef LIBRARY /* Not testing a library. */
69 #undef EXTRA_DUMP /* How to dump extra data */
70 #undef EXTRA_ERRNO /* How to dump errno */
71 /* How to generate extra version info. */
72 #define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "")
76 * Windows support routines
78 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
79 * in the test harness is dangerous because they cover up
80 * configuration errors. The classic example of this is omitting a
81 * configure check. If libarchive and libarchive_test both look for
82 * the same feature macro, such errors are hard to detect. Platform
83 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
84 * easily lead to very messy code. It's best to limit yourself
85 * to only the most generic programming techniques in the test harness
86 * and thus avoid conditionals altogether. Where that's not possible,
87 * try to minimize conditionals by grouping platform-specific tests in
88 * one place (e.g., test_acl_freebsd) or by adding new assert()
89 * functions (e.g., assertMakeHardlink()) to cover up platform
90 * differences. Platform-specific coding in libarchive_test is often
91 * a symptom that some capability is missing from libarchive itself.
93 #if defined(_WIN32) && !defined(__CYGWIN__)
101 #define S_ISDIR(m) ((m) & _S_IFDIR)
104 #define S_ISREG(m) ((m) & _S_IFREG)
106 #if !defined(__BORLANDC__)
107 #define access _access
112 #define fileno _fileno
114 /*#define fstat _fstat64*/
115 #if !defined(__BORLANDC__)
116 #define getcwd _getcwd
119 /*#define lstat _stat64*/
120 /*#define stat _stat64*/
122 #if !defined(__BORLANDC__)
123 #define strdup _strdup
126 #define int64_t __int64
129 #if defined(HAVE__CrtSetReportMode)
133 #if defined(_WIN32) && !defined(__CYGWIN__)
134 static void *GetFunctionKernel32(const char *);
135 static int my_CreateSymbolicLinkA(const char *, const char *, int);
136 static int my_CreateHardLinkA(const char *, const char *);
137 static int my_GetFileInformationByName(const char *,
138 BY_HANDLE_FILE_INFORMATION *);
141 GetFunctionKernel32(const char *name)
143 static HINSTANCE lib;
147 lib = LoadLibrary("kernel32.dll");
150 fprintf(stderr, "Can't load kernel32.dll?!\n");
153 return (void *)GetProcAddress(lib, name);
157 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
159 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
163 f = GetFunctionKernel32("CreateSymbolicLinkA");
165 return f == NULL ? 0 : (*f)(linkname, target, flags);
169 my_CreateHardLinkA(const char *linkname, const char *target)
171 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
175 f = GetFunctionKernel32("CreateHardLinkA");
177 return f == NULL ? 0 : (*f)(linkname, target, NULL);
181 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
186 memset(bhfi, 0, sizeof(*bhfi));
187 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
188 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
189 if (h == INVALID_HANDLE_VALUE)
191 r = GetFileInformationByHandle(h, bhfi);
197 #if defined(HAVE__CrtSetReportMode)
199 invalid_parameter_handler(const wchar_t * expression,
200 const wchar_t * function, const wchar_t * file,
201 unsigned int line, uintptr_t pReserved)
213 /* Enable core dump on failure. */
214 static int dump_on_failure = 0;
215 /* Default is to remove temp dirs and log data for successful tests. */
216 static int keep_temp_files = 0;
217 /* Default is to run the specified tests once and report errors. */
218 static int until_failure = 0;
219 /* Default is to just report pass/fail for each test. */
220 static int verbosity = 0;
221 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
222 #define VERBOSITY_PASSFAIL 0 /* Default */
223 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
224 #define VERBOSITY_FULL 2 /* -vv */
225 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
226 * mostly for debugging the test harness itself. */
227 /* Cumulative count of assertion failures. */
228 static int failures = 0;
229 /* Cumulative count of reported skips. */
230 static int skips = 0;
231 /* Cumulative count of assertions checked. */
232 static int assertions = 0;
234 /* Directory where uuencoded reference files can be found. */
235 static const char *refdir;
238 * Report log information selectively to console and/or disk log.
240 static int log_console = 0;
241 static FILE *logfile;
243 vlogprintf(const char *fmt, va_list ap)
250 vfprintf(stdout, fmt, ap);
253 vfprintf(logfile, fmt, lfap);
256 vfprintf(logfile, fmt, ap);
261 logprintf(const char *fmt, ...)
269 /* Set up a message to display only if next assertion fails. */
270 static char msgbuff[4096];
271 static const char *msg, *nextmsg;
273 failure(const char *fmt, ...)
280 vsprintf(msgbuff, fmt, ap);
287 * Copy arguments into file-local variables.
288 * This was added to permit vararg assert() functions without needing
289 * variadic wrapper macros. Turns out that the vararg capability is almost
290 * never used, so almost all of the vararg assertions can be simplified
291 * by removing the vararg capability and reworking the wrapper macro to
292 * pass __FILE__, __LINE__ directly into the function instead of using
293 * this hook. I suspect this machinery is used so rarely that we
294 * would be better off just removing it entirely. That would simplify
295 * the code here noticeably.
297 static const char *skipping_filename;
298 static int skipping_line;
299 void skipping_setup(const char *filename, int line)
301 skipping_filename = filename;
302 skipping_line = line;
305 /* Called at the beginning of each assert() function. */
307 assertion_count(const char *file, int line)
309 (void)file; /* UNUSED */
310 (void)line; /* UNUSED */
312 /* Proper handling of "failure()" message. */
315 /* Uncomment to print file:line after every assertion.
316 * Verbose, but occasionally useful in tracking down crashes. */
317 /* printf("Checked %s:%d\n", file, line); */
321 * For each test source file, we remember how many times each
322 * assertion was reported. Cleared before each new test,
323 * used by test_summarize().
328 } failed_lines[10000];
329 const char *failed_filename;
331 /* Count this failure, setup up log destination and handle initial report. */
333 failure_start(const char *filename, int line, const char *fmt, ...)
337 /* Record another failure for this line. */
339 failed_filename = filename;
340 failed_lines[line].count++;
342 /* Determine whether to log header to console. */
344 case VERBOSITY_LIGHT_REPORT:
345 log_console = (failed_lines[line].count < 2);
348 log_console = (verbosity >= VERBOSITY_FULL);
351 /* Log file:line header for this failure */
354 logprintf("%s(%d): ", filename, line);
356 logprintf("%s:%d: ", filename, line);
362 if (msg != NULL && msg[0] != '\0') {
363 logprintf(" Description: %s\n", msg);
367 /* Determine whether to log details to console. */
368 if (verbosity == VERBOSITY_LIGHT_REPORT)
372 /* Complete reporting of failed tests. */
374 * The 'extra' hook here is used by libarchive to include libarchive
375 * error messages with assertion failures. It could also be used
376 * to add strerror() output, for example. Just define the EXTRA_DUMP()
377 * macro appropriately.
380 failure_finish(void *extra)
382 (void)extra; /* UNUSED (maybe) */
385 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
386 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
390 if (dump_on_failure) {
392 " *** forcing core dump so failure can be debugged ***\n");
397 /* Inform user that we're skipping some checks. */
399 test_skipping(const char *fmt, ...)
405 vsprintf(buff, fmt, ap);
407 /* Use failure() message if set. */
410 /* failure_start() isn't quite right, but is awfully convenient. */
411 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
412 --failures; /* Undo failures++ in failure_start() */
413 /* Don't failure_finish() here. */
414 /* Mark as skip, so doesn't count as failed test. */
415 failed_lines[skipping_line].skip = 1;
425 /* Generic assert() just displays the failed condition. */
427 assertion_assert(const char *file, int line, int value,
428 const char *condition, void *extra)
430 assertion_count(file, line);
432 failure_start(file, line, "Assertion failed: %s", condition);
433 failure_finish(extra);
438 /* chdir() and report any errors */
440 assertion_chdir(const char *file, int line, const char *pathname)
442 assertion_count(file, line);
443 if (chdir(pathname) == 0)
445 failure_start(file, line, "chdir(\"%s\")", pathname);
446 failure_finish(NULL);
451 /* Verify two integers are equal. */
453 assertion_equal_int(const char *file, int line,
454 long long v1, const char *e1, long long v2, const char *e2, void *extra)
456 assertion_count(file, line);
459 failure_start(file, line, "%s != %s", e1, e2);
460 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
461 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
462 failure_finish(extra);
467 * Utility to convert a single UTF-8 sequence.
470 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
472 static const char utf8_count[256] = {
473 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
474 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
475 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
476 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
477 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
478 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
479 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
480 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
481 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
482 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
483 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
484 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
485 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
486 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
487 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
488 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
500 * Decode 1-4 bytes depending on the value of the first byte.
502 ch = (unsigned char)*s;
504 return (0); /* Standard: return 0 for end-of-string. */
505 cnt = utf8_count[ch];
507 /* Invalide sequence or there are not plenty bytes. */
511 /* Make a Unicode code point from a single UTF-8 sequence. */
513 case 1: /* 1 byte sequence. */
516 case 2: /* 2 bytes sequence. */
517 if ((s[1] & 0xc0) != 0x80) return (-1);
518 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
520 case 3: /* 3 bytes sequence. */
521 if ((s[1] & 0xc0) != 0x80) return (-1);
522 if ((s[2] & 0xc0) != 0x80) return (-1);
523 wc = ((ch & 0x0f) << 12)
524 | ((s[1] & 0x3f) << 6)
527 return (-1);/* Overlong sequence. */
529 case 4: /* 4 bytes sequence. */
532 if ((s[1] & 0xc0) != 0x80) return (-1);
533 if ((s[2] & 0xc0) != 0x80) return (-1);
534 if ((s[3] & 0xc0) != 0x80) return (-1);
535 wc = ((ch & 0x07) << 18)
536 | ((s[1] & 0x3f) << 12)
537 | ((s[2] & 0x3f) << 6)
540 return (-1);/* Overlong sequence. */
546 /* The code point larger than 0x10FFFF is not leagal
550 /* Correctly gets a Unicode, returns used bytes. */
555 static void strdump(const char *e, const char *p, int ewidth, int utf8)
559 logprintf(" %*s = ", ewidth, e);
566 unsigned int c = 0xff & *p++;
568 case '\a': printf("\a"); break;
569 case '\b': printf("\b"); break;
570 case '\n': printf("\n"); break;
571 case '\r': printf("\r"); break;
573 if (c >= 32 && c < 127)
576 logprintf("\\x%02X", c);
580 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
583 * If the current string is UTF-8, dump its code points.
594 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
597 logprintf("%04X", uc);
603 logprintf(" (count %d", cnt);
605 logprintf(",unknown %d bytes", len);
613 /* Verify two strings are equal, dump them if not. */
615 assertion_equal_string(const char *file, int line,
616 const char *v1, const char *e1,
617 const char *v2, const char *e2,
618 void *extra, int utf8)
622 assertion_count(file, line);
623 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
625 failure_start(file, line, "%s != %s", e1, e2);
626 l1 = (int)strlen(e1);
627 l2 = (int)strlen(e2);
630 strdump(e1, v1, l1, utf8);
631 strdump(e2, v2, l1, utf8);
632 failure_finish(extra);
637 wcsdump(const char *e, const wchar_t *w)
639 logprintf(" %s = ", e);
645 while (*w != L'\0') {
646 unsigned int c = *w++;
647 if (c >= 32 && c < 127)
650 logprintf("\\x%02X", c);
651 else if (c < 0x10000)
652 logprintf("\\u%04X", c);
654 logprintf("\\U%08X", c);
661 wcscmp(const wchar_t *s1, const wchar_t *s2)
664 while (*s1 == *s2++) {
675 /* Verify that two wide strings are equal, dump them if not. */
677 assertion_equal_wstring(const char *file, int line,
678 const wchar_t *v1, const char *e1,
679 const wchar_t *v2, const char *e2,
682 assertion_count(file, line);
685 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
687 failure_start(file, line, "%s != %s", e1, e2);
690 failure_finish(extra);
695 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
696 * any bytes in p that differ from ref will be highlighted with '_'
697 * before and after the hex value.
700 hexdump(const char *p, const char *ref, size_t l, size_t offset)
706 logprintf("(null)\n");
709 for(i=0; i < l; i+=16) {
710 logprintf("%04x", (unsigned)(i + offset));
712 for (j = 0; j < 16 && i + j < l; j++) {
713 if (ref != NULL && p[i + j] != ref[i + j])
715 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
716 if (ref != NULL && p[i + j] == ref[i + j])
719 for (; j < 16; j++) {
720 logprintf("%c ", sep);
723 logprintf("%c", sep);
724 for (j=0; j < 16 && i + j < l; j++) {
726 if (c >= ' ' && c <= 126)
735 /* Verify that two blocks of memory are the same, display the first
736 * block of differences if they're not. */
738 assertion_equal_mem(const char *file, int line,
739 const void *_v1, const char *e1,
740 const void *_v2, const char *e2,
741 size_t l, const char *ld, void *extra)
743 const char *v1 = (const char *)_v1;
744 const char *v2 = (const char *)_v2;
747 assertion_count(file, line);
748 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
750 if (v1 == NULL || v2 == NULL)
753 failure_start(file, line, "%s != %s", e1, e2);
754 logprintf(" size %s = %d\n", ld, (int)l);
755 /* Dump 48 bytes (3 lines) so that the first difference is
756 * in the second line. */
758 while (l > 64 && memcmp(v1, v2, 32) == 0) {
759 /* Two lines agree, so step forward one line. */
765 logprintf(" Dump of %s\n", e1);
766 hexdump(v1, v2, l < 128 ? l : 128, offset);
767 logprintf(" Dump of %s\n", e2);
768 hexdump(v2, v1, l < 128 ? l : 128, offset);
770 failure_finish(extra);
774 /* Verify that the named file exists and is empty. */
776 assertion_empty_file(const char *filename, int line, const char *f1)
783 assertion_count(filename, line);
785 if (stat(f1, &st) != 0) {
786 failure_start(filename, line, "Stat failed: %s", f1);
787 failure_finish(NULL);
793 failure_start(filename, line, "File should be empty: %s", f1);
794 logprintf(" File size: %d\n", (int)st.st_size);
795 logprintf(" Contents:\n");
798 logprintf(" Unable to open %s\n", f1);
800 s = ((off_t)sizeof(buff) < st.st_size) ?
801 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
802 s = fread(buff, 1, s, f);
803 hexdump(buff, NULL, s, 0);
806 failure_finish(NULL);
810 /* Verify that the named file exists and is not empty. */
812 assertion_non_empty_file(const char *filename, int line, const char *f1)
816 assertion_count(filename, line);
818 if (stat(f1, &st) != 0) {
819 failure_start(filename, line, "Stat failed: %s", f1);
820 failure_finish(NULL);
823 if (st.st_size == 0) {
824 failure_start(filename, line, "File empty: %s", f1);
825 failure_finish(NULL);
831 /* Verify that two files have the same contents. */
832 /* TODO: hexdump the first bytes that actually differ. */
834 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
841 assertion_count(filename, line);
843 f1 = fopen(fn1, "rb");
844 f2 = fopen(fn2, "rb");
845 if (f1 == NULL || f2 == NULL) {
851 n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
852 n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
855 if (n1 == 0 && n2 == 0) {
860 if (memcmp(buff1, buff2, n1) != 0)
865 failure_start(filename, line, "Files not identical");
866 logprintf(" file1=\"%s\"\n", fn1);
867 logprintf(" file2=\"%s\"\n", fn2);
868 failure_finish(NULL);
872 /* Verify that the named file does exist. */
874 assertion_file_exists(const char *filename, int line, const char *f)
876 assertion_count(filename, line);
878 #if defined(_WIN32) && !defined(__CYGWIN__)
882 if (!access(f, F_OK))
885 failure_start(filename, line, "File should exist: %s", f);
886 failure_finish(NULL);
890 /* Verify that the named file doesn't exist. */
892 assertion_file_not_exists(const char *filename, int line, const char *f)
894 assertion_count(filename, line);
896 #if defined(_WIN32) && !defined(__CYGWIN__)
903 failure_start(filename, line, "File should not exist: %s", f);
904 failure_finish(NULL);
908 /* Compare the contents of a file to a block of memory. */
910 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
916 assertion_count(filename, line);
920 failure_start(filename, line,
921 "File should exist: %s", fn);
922 failure_finish(NULL);
925 contents = malloc(s * 2);
926 n = (int)fread(contents, 1, s * 2, f);
928 if (n == s && memcmp(buff, contents, s) == 0) {
932 failure_start(filename, line, "File contents don't match");
933 logprintf(" file=\"%s\"\n", fn);
935 hexdump(contents, buff, n > 512 ? 512 : n, 0);
937 logprintf(" File empty, contents should be:\n");
938 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
940 failure_finish(NULL);
945 /* Check the contents of a text file, being tolerant of line endings. */
947 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
950 const char *btxt, *ftxt;
954 assertion_count(filename, line);
957 failure_start(filename, line,
958 "File doesn't exist: %s", fn);
959 failure_finish(NULL);
962 s = (int)strlen(buff);
963 contents = malloc(s * 2 + 128);
964 n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
970 ftxt = (const char *)contents;
971 while (*btxt != '\0' && *ftxt != '\0') {
972 if (*btxt == *ftxt) {
977 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
978 /* Pass over different new line characters. */
985 if (*btxt == '\0' && *ftxt == '\0') {
989 failure_start(filename, line, "Contents don't match");
990 logprintf(" file=\"%s\"\n", fn);
992 hexdump(contents, buff, n, 0);
993 logprintf(" expected\n", fn);
994 hexdump(buff, contents, s, 0);
996 logprintf(" File empty, contents should be:\n");
997 hexdump(buff, NULL, s, 0);
999 failure_finish(NULL);
1004 /* Verify that a text file contains the specified lines, regardless of order */
1005 /* This could be more efficient if we sorted both sets of lines, etc, but
1006 * since this is used only for testing and only ever deals with a dozen or so
1007 * lines at a time, this relatively crude approach is just fine. */
1009 assertion_file_contains_lines_any_order(const char *file, int line,
1010 const char *pathname, const char *lines[])
1014 size_t expected_count, actual_count, i, j;
1015 char **expected = NULL;
1016 char *p, **actual = NULL;
1018 int expected_failure = 0, actual_failure = 0;
1020 assertion_count(file, line);
1022 buff = slurpfile(&buff_size, "%s", pathname);
1024 failure_start(pathname, line, "Can't read file: %s", pathname);
1025 failure_finish(NULL);
1029 /* Make a copy of the provided lines and count up the expected
1031 for (i = 0; lines[i] != NULL; ++i) {
1034 if (expected_count) {
1035 expected = malloc(sizeof(char *) * expected_count);
1036 if (expected == NULL) {
1037 failure_start(pathname, line, "Can't allocate memory");
1038 failure_finish(NULL);
1042 for (i = 0; lines[i] != NULL; ++i) {
1043 expected[i] = strdup(lines[i]);
1047 /* Break the file into lines */
1049 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1050 if (*p == '\x0d' || *p == '\x0a')
1052 if (c == '\0' && *p != '\0')
1057 actual = calloc(sizeof(char *), actual_count);
1058 if (actual == NULL) {
1059 failure_start(pathname, line, "Can't allocate memory");
1060 failure_finish(NULL);
1064 for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) {
1072 /* Erase matching lines from both lists */
1073 for (i = 0; i < expected_count; ++i) {
1074 if (expected[i] == NULL)
1076 for (j = 0; j < actual_count; ++j) {
1077 if (actual[j] == NULL)
1079 if (strcmp(expected[i], actual[j]) == 0) {
1088 /* If there's anything left, it's a failure */
1089 for (i = 0; i < expected_count; ++i) {
1090 if (expected[i] != NULL)
1093 for (j = 0; j < actual_count; ++j) {
1094 if (actual[j] != NULL)
1097 if (expected_failure == 0 && actual_failure == 0) {
1103 failure_start(file, line, "File doesn't match: %s", pathname);
1104 for (i = 0; i < expected_count; ++i) {
1105 if (expected[i] != NULL) {
1106 logprintf(" Expected but not present: %s\n", expected[i]);
1110 for (j = 0; j < actual_count; ++j) {
1111 if (actual[j] != NULL)
1112 logprintf(" Present but not expected: %s\n", actual[j]);
1114 failure_finish(NULL);
1121 /* Test that two paths point to the same file. */
1122 /* As a side-effect, asserts that both files exist. */
1124 is_hardlink(const char *file, int line,
1125 const char *path1, const char *path2)
1127 #if defined(_WIN32) && !defined(__CYGWIN__)
1128 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1131 assertion_count(file, line);
1132 r = my_GetFileInformationByName(path1, &bhfi1);
1134 failure_start(file, line, "File %s can't be inspected?", path1);
1135 failure_finish(NULL);
1138 r = my_GetFileInformationByName(path2, &bhfi2);
1140 failure_start(file, line, "File %s can't be inspected?", path2);
1141 failure_finish(NULL);
1144 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1145 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1146 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1148 struct stat st1, st2;
1151 assertion_count(file, line);
1152 r = lstat(path1, &st1);
1154 failure_start(file, line, "File should exist: %s", path1);
1155 failure_finish(NULL);
1158 r = lstat(path2, &st2);
1160 failure_start(file, line, "File should exist: %s", path2);
1161 failure_finish(NULL);
1164 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1169 assertion_is_hardlink(const char *file, int line,
1170 const char *path1, const char *path2)
1172 if (is_hardlink(file, line, path1, path2))
1174 failure_start(file, line,
1175 "Files %s and %s are not hardlinked", path1, path2);
1176 failure_finish(NULL);
1181 assertion_is_not_hardlink(const char *file, int line,
1182 const char *path1, const char *path2)
1184 if (!is_hardlink(file, line, path1, path2))
1186 failure_start(file, line,
1187 "Files %s and %s should not be hardlinked", path1, path2);
1188 failure_finish(NULL);
1192 /* Verify a/b/mtime of 'pathname'. */
1193 /* If 'recent', verify that it's within last 10 seconds. */
1195 assertion_file_time(const char *file, int line,
1196 const char *pathname, long t, long nsec, char type, int recent)
1198 long long filet, filet_nsec;
1201 #if defined(_WIN32) && !defined(__CYGWIN__)
1202 #define EPOC_TIME (116444736000000000ULL)
1203 FILETIME fxtime, fbirthtime, fatime, fmtime;
1204 ULARGE_INTEGER wintm;
1206 fxtime.dwLowDateTime = 0;
1207 fxtime.dwHighDateTime = 0;
1209 assertion_count(file, line);
1210 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1211 * a directory file. If not, CreateFile() will fail when
1212 * the pathname is a directory. */
1213 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1214 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1215 if (h == INVALID_HANDLE_VALUE) {
1216 failure_start(file, line, "Can't access %s\n", pathname);
1217 failure_finish(NULL);
1220 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1222 case 'a': fxtime = fatime; break;
1223 case 'b': fxtime = fbirthtime; break;
1224 case 'm': fxtime = fmtime; break;
1228 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1229 failure_finish(NULL);
1232 wintm.LowPart = fxtime.dwLowDateTime;
1233 wintm.HighPart = fxtime.dwHighDateTime;
1234 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1235 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1236 nsec = (nsec / 100) * 100; /* Round the request */
1240 assertion_count(file, line);
1241 r = lstat(pathname, &st);
1243 failure_start(file, line, "Can't stat %s\n", pathname);
1244 failure_finish(NULL);
1248 case 'a': filet = st.st_atime; break;
1249 case 'm': filet = st.st_mtime; break;
1250 case 'b': filet = 0; break;
1251 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1254 #if defined(__FreeBSD__)
1256 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1257 case 'b': filet = st.st_birthtime;
1258 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1259 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1260 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1263 /* FreeBSD generally only stores to microsecond res, so round. */
1264 filet_nsec = (filet_nsec / 1000) * 1000;
1265 nsec = (nsec / 1000) * 1000;
1267 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1268 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1269 #if defined(__HAIKU__)
1270 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1275 /* Check that requested time is up-to-date. */
1276 time_t now = time(NULL);
1277 if (filet < now - 10 || filet > now + 1) {
1278 failure_start(file, line,
1279 "File %s has %ctime %lld, %lld seconds ago\n",
1280 pathname, type, filet, now - filet);
1281 failure_finish(NULL);
1284 } else if (filet != t || filet_nsec != nsec) {
1285 failure_start(file, line,
1286 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1287 pathname, type, filet, filet_nsec, t, nsec);
1288 failure_finish(NULL);
1294 /* Verify atime of 'pathname'. */
1296 assertion_file_atime(const char *file, int line,
1297 const char *pathname, long t, long nsec)
1299 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1302 /* Verify atime of 'pathname' is up-to-date. */
1304 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1306 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1309 /* Verify birthtime of 'pathname'. */
1311 assertion_file_birthtime(const char *file, int line,
1312 const char *pathname, long t, long nsec)
1314 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1317 /* Verify birthtime of 'pathname' is up-to-date. */
1319 assertion_file_birthtime_recent(const char *file, int line,
1320 const char *pathname)
1322 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1325 /* Verify mtime of 'pathname'. */
1327 assertion_file_mtime(const char *file, int line,
1328 const char *pathname, long t, long nsec)
1330 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1333 /* Verify mtime of 'pathname' is up-to-date. */
1335 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1337 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1340 /* Verify number of links to 'pathname'. */
1342 assertion_file_nlinks(const char *file, int line,
1343 const char *pathname, int nlinks)
1345 #if defined(_WIN32) && !defined(__CYGWIN__)
1346 BY_HANDLE_FILE_INFORMATION bhfi;
1349 assertion_count(file, line);
1350 r = my_GetFileInformationByName(pathname, &bhfi);
1351 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1353 failure_start(file, line, "File %s has %d links, expected %d",
1354 pathname, bhfi.nNumberOfLinks, nlinks);
1355 failure_finish(NULL);
1361 assertion_count(file, line);
1362 r = lstat(pathname, &st);
1363 if (r == 0 && (int)st.st_nlink == nlinks)
1365 failure_start(file, line, "File %s has %d links, expected %d",
1366 pathname, st.st_nlink, nlinks);
1367 failure_finish(NULL);
1372 /* Verify size of 'pathname'. */
1374 assertion_file_size(const char *file, int line, const char *pathname, long size)
1379 assertion_count(file, line);
1380 #if defined(_WIN32) && !defined(__CYGWIN__)
1382 BY_HANDLE_FILE_INFORMATION bhfi;
1383 r = !my_GetFileInformationByName(pathname, &bhfi);
1384 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1389 r = lstat(pathname, &st);
1390 filesize = st.st_size;
1393 if (r == 0 && filesize == size)
1395 failure_start(file, line, "File %s has size %ld, expected %ld",
1396 pathname, (long)filesize, (long)size);
1397 failure_finish(NULL);
1401 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1403 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1408 #if defined(_WIN32) && !defined(__CYGWIN__)
1409 (void)mode; /* UNUSED */
1411 assertion_count(file, line);
1412 r = lstat(pathname, &st);
1414 failure_start(file, line, "Dir should exist: %s", pathname);
1415 failure_finish(NULL);
1418 if (!S_ISDIR(st.st_mode)) {
1419 failure_start(file, line, "%s is not a dir", pathname);
1420 failure_finish(NULL);
1423 #if !defined(_WIN32) || defined(__CYGWIN__)
1424 /* Windows doesn't handle permissions the same way as POSIX,
1425 * so just ignore the mode tests. */
1426 /* TODO: Can we do better here? */
1427 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1428 failure_start(file, line, "Dir %s has wrong mode", pathname);
1429 logprintf(" Expected: 0%3o\n", mode);
1430 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1431 failure_finish(NULL);
1438 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1439 * verify that too. */
1441 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1446 #if defined(_WIN32) && !defined(__CYGWIN__)
1447 (void)mode; /* UNUSED */
1449 assertion_count(file, line);
1450 r = lstat(pathname, &st);
1451 if (r != 0 || !S_ISREG(st.st_mode)) {
1452 failure_start(file, line, "File should exist: %s", pathname);
1453 failure_finish(NULL);
1456 #if !defined(_WIN32) || defined(__CYGWIN__)
1457 /* Windows doesn't handle permissions the same way as POSIX,
1458 * so just ignore the mode tests. */
1459 /* TODO: Can we do better here? */
1460 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1461 failure_start(file, line, "File %s has wrong mode", pathname);
1462 logprintf(" Expected: 0%3o\n", mode);
1463 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1464 failure_finish(NULL);
1471 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1472 * non-NULL, verify that the symlink has those contents. */
1474 is_symlink(const char *file, int line,
1475 const char *pathname, const char *contents)
1477 #if defined(_WIN32) && !defined(__CYGWIN__)
1478 (void)pathname; /* UNUSED */
1479 (void)contents; /* UNUSED */
1480 assertion_count(file, line);
1481 /* Windows sort-of has real symlinks, but they're only usable
1482 * by privileged users and are crippled even then, so there's
1483 * really not much point in bothering with this. */
1491 assertion_count(file, line);
1492 r = lstat(pathname, &st);
1494 failure_start(file, line,
1495 "Symlink should exist: %s", pathname);
1496 failure_finish(NULL);
1499 if (!S_ISLNK(st.st_mode))
1501 if (contents == NULL)
1503 linklen = readlink(pathname, buff, sizeof(buff));
1505 failure_start(file, line, "Can't read symlink %s", pathname);
1506 failure_finish(NULL);
1509 buff[linklen] = '\0';
1510 if (strcmp(buff, contents) != 0)
1516 /* Assert that path is a symlink that (optionally) contains contents. */
1518 assertion_is_symlink(const char *file, int line,
1519 const char *path, const char *contents)
1521 if (is_symlink(file, line, path, contents))
1524 failure_start(file, line, "File %s is not a symlink to %s",
1527 failure_start(file, line, "File %s is not a symlink", path);
1528 failure_finish(NULL);
1533 /* Create a directory and report any errors. */
1535 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1537 assertion_count(file, line);
1538 #if defined(_WIN32) && !defined(__CYGWIN__)
1539 (void)mode; /* UNUSED */
1540 if (0 == _mkdir(dirname))
1543 if (0 == mkdir(dirname, mode))
1546 failure_start(file, line, "Could not create directory %s", dirname);
1547 failure_finish(NULL);
1551 /* Create a file with the specified contents and report any failures. */
1553 assertion_make_file(const char *file, int line,
1554 const char *path, int mode, int csize, const void *contents)
1556 #if defined(_WIN32) && !defined(__CYGWIN__)
1557 /* TODO: Rework this to set file mode as well. */
1559 (void)mode; /* UNUSED */
1560 assertion_count(file, line);
1561 f = fopen(path, "wb");
1563 failure_start(file, line, "Could not create file %s", path);
1564 failure_finish(NULL);
1567 if (contents != NULL) {
1571 wsize = strlen(contents);
1573 wsize = (size_t)csize;
1574 if (wsize != fwrite(contents, 1, wsize, f)) {
1576 failure_start(file, line,
1577 "Could not write file %s", path);
1578 failure_finish(NULL);
1586 assertion_count(file, line);
1587 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1589 failure_start(file, line, "Could not create %s", path);
1590 failure_finish(NULL);
1593 if (contents != NULL) {
1597 wsize = (ssize_t)strlen(contents);
1599 wsize = (ssize_t)csize;
1600 if (wsize != write(fd, contents, wsize)) {
1602 failure_start(file, line,
1603 "Could not write to %s", path);
1604 failure_finish(NULL);
1613 /* Create a hardlink and report any failures. */
1615 assertion_make_hardlink(const char *file, int line,
1616 const char *newpath, const char *linkto)
1620 assertion_count(file, line);
1621 #if defined(_WIN32) && !defined(__CYGWIN__)
1622 succeeded = my_CreateHardLinkA(newpath, linkto);
1624 succeeded = !link(linkto, newpath);
1630 failure_start(file, line, "Could not create hardlink");
1631 logprintf(" New link: %s\n", newpath);
1632 logprintf(" Old name: %s\n", linkto);
1633 failure_finish(NULL);
1637 /* Create a symlink and report any failures. */
1639 assertion_make_symlink(const char *file, int line,
1640 const char *newpath, const char *linkto)
1642 #if defined(_WIN32) && !defined(__CYGWIN__)
1643 int targetIsDir = 0; /* TODO: Fix this */
1644 assertion_count(file, line);
1645 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1648 assertion_count(file, line);
1649 if (0 == symlink(linkto, newpath))
1652 failure_start(file, line, "Could not create symlink");
1653 logprintf(" New link: %s\n", newpath);
1654 logprintf(" Old name: %s\n", linkto);
1655 failure_finish(NULL);
1659 /* Set umask, report failures. */
1661 assertion_umask(const char *file, int line, int mask)
1663 assertion_count(file, line);
1664 (void)file; /* UNUSED */
1665 (void)line; /* UNUSED */
1670 /* Set times, report failures. */
1672 assertion_utimes(const char *file, int line,
1673 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1677 #if defined(_WIN32) && !defined(__CYGWIN__)
1678 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1679 + (((nsec)/1000)*10))
1681 ULARGE_INTEGER wintm;
1682 FILETIME fatime, fmtime;
1683 FILETIME *pat, *pmt;
1685 assertion_count(file, line);
1686 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1687 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1688 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1689 if (h == INVALID_HANDLE_VALUE) {
1690 failure_start(file, line, "Can't access %s\n", pathname);
1691 failure_finish(NULL);
1695 if (at > 0 || at_nsec > 0) {
1696 wintm.QuadPart = WINTIME(at, at_nsec);
1697 fatime.dwLowDateTime = wintm.LowPart;
1698 fatime.dwHighDateTime = wintm.HighPart;
1702 if (mt > 0 || mt_nsec > 0) {
1703 wintm.QuadPart = WINTIME(mt, mt_nsec);
1704 fmtime.dwLowDateTime = wintm.LowPart;
1705 fmtime.dwHighDateTime = wintm.HighPart;
1709 if (pat != NULL || pmt != NULL)
1710 r = SetFileTime(h, NULL, pat, pmt);
1715 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1716 failure_finish(NULL);
1720 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1722 struct timeval times[2];
1724 #if !defined(__FreeBSD__)
1725 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1727 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1730 r = lstat(pathname, &st);
1732 failure_start(file, line, "Can't stat %s\n", pathname);
1733 failure_finish(NULL);
1737 if (mt == 0 && mt_nsec == 0) {
1739 #if defined(__FreeBSD__)
1740 mt_nsec = st.st_mtimespec.tv_nsec;
1741 /* FreeBSD generally only stores to microsecond res, so round. */
1742 mt_nsec = (mt_nsec / 1000) * 1000;
1745 if (at == 0 && at_nsec == 0) {
1747 #if defined(__FreeBSD__)
1748 at_nsec = st.st_atimespec.tv_nsec;
1749 /* FreeBSD generally only stores to microsecond res, so round. */
1750 at_nsec = (at_nsec / 1000) * 1000;
1754 times[1].tv_sec = mt;
1755 times[1].tv_usec = mt_nsec / 1000;
1757 times[0].tv_sec = at;
1758 times[0].tv_usec = at_nsec / 1000;
1761 r = lutimes(pathname, times);
1763 r = utimes(pathname, times);
1766 failure_start(file, line, "Can't utimes %s\n", pathname);
1767 failure_finish(NULL);
1771 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1774 /* Set nodump, report failures. */
1776 assertion_nodump(const char *file, int line, const char *pathname)
1778 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1781 assertion_count(file, line);
1782 r = chflags(pathname, UF_NODUMP);
1784 failure_start(file, line, "Can't set nodump %s\n", pathname);
1785 failure_finish(NULL);
1788 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1789 && defined(EXT2_NODUMP_FL)
1792 assertion_count(file, line);
1793 fd = open(pathname, O_RDONLY | O_NONBLOCK);
1795 failure_start(file, line, "Can't open %s\n", pathname);
1796 failure_finish(NULL);
1799 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1801 failure_start(file, line, "Can't get flags %s\n", pathname);
1802 failure_finish(NULL);
1805 flags |= EXT2_NODUMP_FL;
1806 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1808 failure_start(file, line, "Can't set nodump %s\n", pathname);
1809 failure_finish(NULL);
1814 (void)pathname; /* UNUSED */
1815 assertion_count(file, line);
1822 * UTILITIES for use by tests.
1827 * Check whether platform supports symlinks. This is intended
1828 * for tests to use in deciding whether to bother testing symlink
1829 * support; if the platform doesn't support symlinks, there's no point
1830 * in checking whether the program being tested can create them.
1832 * Note that the first time this test is called, we actually go out to
1833 * disk to create and verify a symlink. This is necessary because
1834 * symlink support is actually a property of a particular filesystem
1835 * and can thus vary between directories on a single system. After
1836 * the first call, this returns the cached result from memory, so it's
1837 * safe to call it as often as you wish.
1842 /* Remember the test result */
1843 static int value = 0, tested = 0;
1848 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1849 /* Note: Cygwin has its own symlink() emulation that does not
1850 * use the Win32 CreateSymbolicLink() function. */
1851 #if defined(_WIN32) && !defined(__CYGWIN__)
1852 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1853 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1855 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1856 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1861 /* Platform-dependent options for hiding the output of a subcommand. */
1862 #if defined(_WIN32) && !defined(__CYGWIN__)
1863 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1865 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1868 * Can this platform run the bzip2 program?
1873 static int tested = 0, value = 0;
1876 if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
1883 * Can this platform run the grzip program?
1888 static int tested = 0, value = 0;
1891 if (systemf("grzip -V %s", redirectArgs) == 0)
1898 * Can this platform run the gzip program?
1903 static int tested = 0, value = 0;
1906 if (systemf("gzip -V %s", redirectArgs) == 0)
1913 * Can this platform run the lrzip program?
1918 static int tested = 0, value = 0;
1921 if (systemf("lrzip -V %s", redirectArgs) == 0)
1928 * Can this platform run the lzip program?
1933 static int tested = 0, value = 0;
1936 if (systemf("lzip -V %s", redirectArgs) == 0)
1943 * Can this platform run the lzma program?
1948 static int tested = 0, value = 0;
1951 if (systemf("lzma -V %s", redirectArgs) == 0)
1958 * Can this platform run the lzop program?
1963 static int tested = 0, value = 0;
1966 if (systemf("lzop -V %s", redirectArgs) == 0)
1973 * Can this platform run the xz program?
1978 static int tested = 0, value = 0;
1981 if (systemf("xz -V %s", redirectArgs) == 0)
1988 * Can this filesystem handle nodump flags.
1990 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1995 const char *path = "cannodumptest";
1998 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
1999 if (chflags(path, UF_NODUMP) < 0)
2001 if (stat(path, &sb) < 0)
2003 if (sb.st_flags & UF_NODUMP)
2008 #elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2009 && defined(EXT2_NODUMP_FL)
2014 const char *path = "cannodumptest";
2017 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2018 fd = open(path, O_RDONLY | O_NONBLOCK);
2021 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2024 flags |= EXT2_NODUMP_FL;
2025 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2029 fd = open(path, O_RDONLY | O_NONBLOCK);
2032 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2036 if (flags & EXT2_NODUMP_FL)
2052 * Sleep as needed; useful for verifying disk timestamp changes by
2053 * ensuring that the wall-clock time has actually changed before we
2054 * go back to re-read something from disk.
2057 sleepUntilAfter(time_t t)
2059 while (t >= time(NULL))
2060 #if defined(_WIN32) && !defined(__CYGWIN__)
2068 * Call standard system() call, but build up the command line using
2069 * sprintf() conventions.
2072 systemf(const char *fmt, ...)
2079 vsprintf(buff, fmt, ap);
2080 if (verbosity > VERBOSITY_FULL)
2081 logprintf("Cmd: %s\n", buff);
2088 * Slurp a file into memory for ease of comparison and testing.
2089 * Returns size of file in 'sizep' if non-NULL, null-terminates
2090 * data in memory for ease of use.
2093 slurpfile(size_t * sizep, const char *fmt, ...)
2095 char filename[8192];
2104 vsprintf(filename, fmt, ap);
2107 f = fopen(filename, "rb");
2109 /* Note: No error; non-existent file is okay here. */
2112 r = fstat(fileno(f), &st);
2114 logprintf("Can't stat file %s\n", filename);
2118 p = malloc((size_t)st.st_size + 1);
2120 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2121 (long int)st.st_size, filename);
2125 bytes_read = fread(p, 1, (size_t)st.st_size, f);
2126 if (bytes_read < st.st_size) {
2127 logprintf("Can't read file %s\n", filename);
2132 p[st.st_size] = '\0';
2134 *sizep = (size_t)st.st_size;
2139 /* Read a uuencoded file from the reference directory, decode, and
2140 * write the result into the current directory. */
2141 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
2143 extract_reference_file(const char *name)
2148 sprintf(buff, "%s/%s.uu", refdir, name);
2149 in = fopen(buff, "r");
2150 failure("Couldn't open reference file %s", buff);
2154 /* Read up to and including the 'begin' line. */
2156 if (fgets(buff, sizeof(buff), in) == NULL) {
2157 /* TODO: This is a failure. */
2160 if (memcmp(buff, "begin ", 6) == 0)
2163 /* Now, decode the rest and write it. */
2164 /* Not a lot of error checking here; the input better be right. */
2165 out = fopen(name, "wb");
2166 while (fgets(buff, sizeof(buff), in) != NULL) {
2170 if (memcmp(buff, "end", 3) == 0)
2173 bytes = UUDECODE(*p++);
2176 /* Write out 1-3 bytes from that. */
2178 n = UUDECODE(*p++) << 18;
2179 n |= UUDECODE(*p++) << 12;
2180 fputc(n >> 16, out);
2184 n |= UUDECODE(*p++) << 6;
2185 fputc((n >> 8) & 0xFF, out);
2189 n |= UUDECODE(*p++);
2190 fputc(n & 0xFF, out);
2200 is_LargeInode(const char *file)
2202 #if defined(_WIN32) && !defined(__CYGWIN__)
2203 BY_HANDLE_FILE_INFORMATION bhfi;
2206 r = my_GetFileInformationByName(file, &bhfi);
2209 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2214 if (stat(file, &st) < 0)
2216 ino = (int64_t)st.st_ino;
2217 return (ino > 0xffffffff);
2227 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2229 * DEFINE_TEST(test_function)
2233 /* Use "list.h" to declare all of the test functions. */
2235 #define DEFINE_TEST(name) void name(void);
2238 /* Use "list.h" to create a list of all tests (functions and names). */
2240 #define DEFINE_TEST(n) { n, #n, 0 },
2241 struct test_list_t tests[] = {
2246 * Summarize repeated failures in the just-completed test.
2249 test_summarize(int failed)
2253 switch (verbosity) {
2254 case VERBOSITY_SUMMARY_ONLY:
2255 printf(failed ? "E" : ".");
2258 case VERBOSITY_PASSFAIL:
2259 printf(failed ? "FAIL\n" : "ok\n");
2263 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2265 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2266 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2267 logprintf("%s:%d: Summary: Failed %d times\n",
2268 failed_filename, i, failed_lines[i].count);
2270 /* Clear the failure history for the next file. */
2271 failed_filename = NULL;
2272 memset(failed_lines, 0, sizeof(failed_lines));
2276 * Actually run a single test, with appropriate setup and cleanup.
2279 test_run(int i, const char *tmpdir)
2282 char logfilename[64];
2283 int failures_before = failures;
2286 switch (verbosity) {
2287 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2289 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2290 printf("%3d: %-50s", i, tests[i].name);
2293 default: /* Title of test, details will follow */
2294 printf("%3d: %s\n", i, tests[i].name);
2297 /* Chdir to the top-level work directory. */
2298 if (!assertChdir(tmpdir)) {
2300 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2303 /* Create a log file for this test. */
2304 sprintf(logfilename, "%s.log", tests[i].name);
2305 logfile = fopen(logfilename, "w");
2306 fprintf(logfile, "%s\n\n", tests[i].name);
2307 /* Chdir() to a work dir for this specific test. */
2308 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2309 testworkdir = workdir;
2310 if (!assertMakeDir(testworkdir, 0755)
2311 || !assertChdir(testworkdir)) {
2313 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2316 /* Explicitly reset the locale before each test. */
2317 setlocale(LC_ALL, "C");
2318 /* Record the umask before we run the test. */
2319 umask(oldumask = umask(0));
2321 * Run the actual test.
2325 * Clean up and report afterwards.
2331 setlocale(LC_ALL, "C");
2332 /* Reset directory. */
2333 if (!assertChdir(tmpdir)) {
2334 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2338 /* Report per-test summaries. */
2339 tests[i].failures = failures - failures_before;
2340 test_summarize(tests[i].failures);
2341 /* Close the per-test log file. */
2344 /* If there were no failures, we can remove the work dir and logfile. */
2345 if (tests[i].failures == 0) {
2346 if (!keep_temp_files && assertChdir(tmpdir)) {
2347 #if defined(_WIN32) && !defined(__CYGWIN__)
2348 /* Make sure not to leave empty directories.
2349 * Sometimes a processing of closing files used by tests
2350 * is not done, then rmdir will be failed and it will
2351 * leave a empty test directory. So we should wait a few
2352 * seconds and retry rmdir. */
2354 for (t = 0; t < 10; t++) {
2357 r = systemf("rmdir /S /Q %s", tests[i].name);
2361 systemf("del %s", logfilename);
2363 systemf("rm -rf %s", tests[i].name);
2364 systemf("rm %s", logfilename);
2368 /* Return appropriate status. */
2369 return (tests[i].failures);
2375 * MAIN and support routines.
2381 usage(const char *program)
2383 static const int limit = sizeof(tests) / sizeof(tests[0]);
2386 printf("Usage: %s [options] <test> <test> ...\n", program);
2387 printf("Default is to run all tests.\n");
2388 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2389 printf("Options:\n");
2390 printf(" -d Dump core after any failure, for debugging.\n");
2391 printf(" -k Keep all temp files.\n");
2392 printf(" Default: temp files for successful tests deleted.\n");
2394 printf(" -p <path> Path to executable to be tested.\n");
2395 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2397 printf(" -q Quiet.\n");
2398 printf(" -r <dir> Path to dir containing reference files.\n");
2399 printf(" Default: Current directory.\n");
2400 printf(" -u Keep running specifies tests until one fails.\n");
2401 printf(" -v Verbose.\n");
2402 printf("Available tests:\n");
2403 for (i = 0; i < limit; i++)
2404 printf(" %d: %s\n", i, tests[i].name);
2409 get_refdir(const char *d)
2411 char tried[512] = { '\0' };
2415 /* If a dir was specified, try that */
2418 snprintf(buff, sizeof(buff), "%s", d);
2419 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2420 if (p != NULL) goto success;
2421 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2422 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2426 /* Get the current dir. */
2428 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2430 pwd = getcwd(NULL, 0);
2432 while (pwd[strlen(pwd) - 1] == '\n')
2433 pwd[strlen(pwd) - 1] = '\0';
2435 /* Look for a known file. */
2436 snprintf(buff, sizeof(buff), "%s", pwd);
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);
2442 snprintf(buff, sizeof(buff), "%s/test", pwd);
2443 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2444 if (p != NULL) goto success;
2445 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2446 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2448 #if defined(LIBRARY)
2449 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
2451 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
2453 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2454 if (p != NULL) goto success;
2455 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2456 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2458 #if defined(PROGRAM_ALIAS)
2459 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
2460 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2461 if (p != NULL) goto success;
2462 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2463 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2466 if (memcmp(pwd, "/usr/obj", 8) == 0) {
2467 snprintf(buff, sizeof(buff), "%s", pwd + 8);
2468 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2469 if (p != NULL) goto success;
2470 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2471 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2473 snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
2474 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2475 if (p != NULL) goto success;
2476 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2477 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2481 printf("Unable to locate known reference file %s\n", KNOWNREF);
2482 printf(" Checked following directories:\n%s\n", tried);
2483 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2491 return strdup(buff);
2495 main(int argc, char **argv)
2497 static const int limit = sizeof(tests) / sizeof(tests[0]);
2498 int test_set[sizeof(tests) / sizeof(tests[0])];
2499 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2501 char *refdir_alloc = NULL;
2502 const char *progname;
2504 const char *tmp, *option_arg, *p;
2505 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2506 char tmpdir_timestamp[256];
2508 (void)argc; /* UNUSED */
2510 /* Get the current dir. */
2512 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2514 pwd = getcwd(NULL, 0);
2516 while (pwd[strlen(pwd) - 1] == '\n')
2517 pwd[strlen(pwd) - 1] = '\0';
2519 #if defined(HAVE__CrtSetReportMode)
2520 /* To stop to run the default invalid parameter handler. */
2521 _set_invalid_parameter_handler(invalid_parameter_handler);
2522 /* Disable annoying assertion message box. */
2523 _CrtSetReportMode(_CRT_ASSERT, 0);
2527 * Name of this program, used to build root of our temp directory
2530 progname = p = argv[0];
2531 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2533 fprintf(stderr, "ERROR: Out of memory.");
2536 strcpy(testprogdir, progname);
2537 while (*p != '\0') {
2538 /* Support \ or / dir separators for Windows compat. */
2539 if (*p == '/' || *p == '\\')
2547 testprogdir[i] = '\0';
2548 #if defined(_WIN32) && !defined(__CYGWIN__)
2549 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2550 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2551 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2552 testprogdir[1] == ':' &&
2553 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
2555 if (testprogdir[0] != '/')
2558 /* Fixup path for relative directories. */
2559 if ((testprogdir = (char *)realloc(testprogdir,
2560 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2562 fprintf(stderr, "ERROR: Out of memory.");
2565 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2566 strlen(testprogdir));
2567 memcpy(testprogdir, pwd, strlen(pwd));
2568 testprogdir[strlen(pwd)] = '/';
2572 /* Get the target program from environment, if available. */
2573 testprogfile = getenv(ENVBASE);
2576 if (getenv("TMPDIR") != NULL)
2577 tmp = getenv("TMPDIR");
2578 else if (getenv("TMP") != NULL)
2579 tmp = getenv("TMP");
2580 else if (getenv("TEMP") != NULL)
2581 tmp = getenv("TEMP");
2582 else if (getenv("TEMPDIR") != NULL)
2583 tmp = getenv("TEMPDIR");
2587 /* Allow -d to be controlled through the environment. */
2588 if (getenv(ENVBASE "_DEBUG") != NULL)
2589 dump_on_failure = 1;
2591 /* Allow -v to be controlled through the environment. */
2592 if (getenv("_VERBOSITY_LEVEL") != NULL)
2594 vlevel = getenv("_VERBOSITY_LEVEL");
2595 verbosity = atoi(vlevel);
2596 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2598 /* Unsupported verbosity levels are silently ignored */
2600 verbosity = VERBOSITY_PASSFAIL;
2604 /* Get the directory holding test files from environment. */
2605 refdir = getenv(ENVBASE "_TEST_FILES");
2608 * Parse options, without using getopt(), which isn't available
2611 ++argv; /* Skip program name */
2612 while (*argv != NULL) {
2617 while (*p != '\0') {
2620 /* If 'opt' takes an argument, parse that. */
2621 if (option == 'p' || option == 'r') {
2624 else if (*argv == NULL) {
2626 "Option -%c requires argument.\n",
2630 option_arg = *argv++;
2631 p = ""; /* End of this option word. */
2634 /* Now, handle the option. */
2637 dump_on_failure = 1;
2640 keep_temp_files = 1;
2644 testprogfile = option_arg;
2646 fprintf(stderr, "-p option not permitted\n");
2655 refdir = option_arg;
2665 fprintf(stderr, "Unrecognized option '%c'\n",
2673 * Sanity-check that our options make sense.
2676 if (testprogfile == NULL)
2678 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2679 strlen(PROGRAM) + 1)) == NULL)
2681 fprintf(stderr, "ERROR: Out of memory.");
2684 strcpy(tmp2, testprogdir);
2686 strcat(tmp2, PROGRAM);
2687 testprogfile = tmp2;
2692 #if defined(_WIN32) && !defined(__CYGWIN__)
2693 /* Command.com sometimes rejects '/' separators. */
2694 testprg = strdup(testprogfile);
2695 for (i = 0; testprg[i] != '\0'; i++) {
2696 if (testprg[i] == '/')
2699 testprogfile = testprg;
2701 /* Quote the name that gets put into shell command lines. */
2702 testprg = malloc(strlen(testprogfile) + 3);
2703 strcpy(testprg, "\"");
2704 strcat(testprg, testprogfile);
2705 strcat(testprg, "\"");
2710 #if !defined(_WIN32) && defined(SIGPIPE)
2711 { /* Ignore SIGPIPE signals */
2712 struct sigaction sa;
2713 sa.sa_handler = SIG_IGN;
2714 sigemptyset(&sa.sa_mask);
2716 sigaction(SIGPIPE, &sa, NULL);
2721 * Create a temp directory for the following tests.
2722 * Include the time the tests started as part of the name,
2723 * to make it easier to track the results of multiple tests.
2726 for (i = 0; ; i++) {
2727 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2728 "%Y-%m-%dT%H.%M.%S",
2730 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2731 tmpdir_timestamp, i);
2732 if (assertMakeDir(tmpdir,0755))
2736 "ERROR: Unable to create temp directory %s\n",
2743 * If the user didn't specify a directory for locating
2744 * reference files, try to find the reference files in
2745 * the "usual places."
2747 refdir = refdir_alloc = get_refdir(refdir);
2750 * Banner with basic information.
2753 printf("If tests fail or crash, details will be in:\n");
2754 printf(" %s\n", tmpdir);
2756 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2757 printf("Reference files will be read from: %s\n", refdir);
2759 printf("Running tests on: %s\n", testprog);
2761 printf("Exercising: ");
2763 printf("%s\n", EXTRA_VERSION);
2770 * Run some or all of the individual tests.
2778 test_num = get_test_set(test_set, limit, *argv, tests);
2780 printf("*** INVALID Test %s\n", *argv);
2786 for (i = 0; i < test_num; i++) {
2788 if (test_run(test_set[i], tmpdir)) {
2796 } while (*argv != NULL);
2797 } while (until_failure);
2800 /* Must be freed after all tests run */
2806 * Report summary statistics.
2808 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2810 printf("Totals:\n");
2811 printf(" Tests run: %8d\n", tests_run);
2812 printf(" Tests failed: %8d\n", tests_failed);
2813 printf(" Assertions checked:%8d\n", assertions);
2814 printf(" Assertions failed: %8d\n", failures);
2815 printf(" Skips reported: %8d\n", skips);
2819 printf("Failing tests:\n");
2820 for (i = 0; i < limit; ++i) {
2821 if (tests[i].failures)
2822 printf(" %d: %s (%d failures)\n", i,
2823 tests[i].name, tests[i].failures);
2826 printf("Details for failing tests: %s\n", tmpdir);
2829 if (verbosity == VERBOSITY_SUMMARY_ONLY)
2831 printf("%d tests passed, no failures\n", tests_run);
2836 /* If the final tmpdir is empty, we can remove it. */
2837 /* This should be the usual case when all tests succeed. */
2841 return (tests_failed ? 1 : 0);