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 #ifdef HAVE_SYS_TIME_H
43 * This same file is used pretty much verbatim for all test harnesses.
45 * The next few lines are the only differences.
46 * TODO: Move this into a separate configuration header, have all test
47 * suites share one copy of this file.
49 __FBSDID("$FreeBSD$");
50 #define KNOWNREF "test_compat_gtar_1.tar.uu"
51 #define ENVBASE "LIBARCHIVE" /* Prefix for environment variables. */
52 #undef PROGRAM /* Testing a library, not a program. */
53 #define LIBRARY "libarchive"
54 #define EXTRA_DUMP(x) archive_error_string((struct archive *)(x))
55 #define EXTRA_ERRNO(x) archive_errno((struct archive *)(x))
56 #define EXTRA_VERSION archive_version_string()
60 * Windows support routines
62 * Note: Configuration is a tricky issue. Using HAVE_* feature macros
63 * in the test harness is dangerous because they cover up
64 * configuration errors. The classic example of this is omitting a
65 * configure check. If libarchive and libarchive_test both look for
66 * the same feature macro, such errors are hard to detect. Platform
67 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
68 * easily lead to very messy code. It's best to limit yourself
69 * to only the most generic programming techniques in the test harness
70 * and thus avoid conditionals altogether. Where that's not possible,
71 * try to minimize conditionals by grouping platform-specific tests in
72 * one place (e.g., test_acl_freebsd) or by adding new assert()
73 * functions (e.g., assertMakeHardlink()) to cover up platform
74 * differences. Platform-specific coding in libarchive_test is often
75 * a symptom that some capability is missing from libarchive itself.
77 #if defined(_WIN32) && !defined(__CYGWIN__)
84 #define S_ISDIR(m) ((m) & _S_IFDIR)
87 #define S_ISREG(m) ((m) & _S_IFREG)
89 #if !defined(__BORLANDC__)
90 #define access _access
95 #define fileno _fileno
97 /*#define fstat _fstat64*/
98 #if !defined(__BORLANDC__)
99 #define getcwd _getcwd
102 /*#define lstat _stat64*/
103 /*#define stat _stat64*/
105 #if !defined(__BORLANDC__)
106 #define strdup _strdup
109 #define int64_t __int64
112 #if defined(HAVE__CrtSetReportMode)
116 #if defined(_WIN32) && !defined(__CYGWIN__)
117 void *GetFunctionKernel32(const char *name)
119 static HINSTANCE lib;
123 lib = LoadLibrary("kernel32.dll");
126 fprintf(stderr, "Can't load kernel32.dll?!\n");
129 return (void *)GetProcAddress(lib, name);
133 my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
135 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
139 f = GetFunctionKernel32("CreateSymbolicLinkA");
141 return f == NULL ? 0 : (*f)(linkname, target, flags);
145 my_CreateHardLinkA(const char *linkname, const char *target)
147 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
151 f = GetFunctionKernel32("CreateHardLinkA");
153 return f == NULL ? 0 : (*f)(linkname, target, NULL);
157 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
162 memset(bhfi, 0, sizeof(*bhfi));
163 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
164 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
165 if (h == INVALID_HANDLE_VALUE)
167 r = GetFileInformationByHandle(h, bhfi);
173 #if defined(HAVE__CrtSetReportMode)
175 invalid_parameter_handler(const wchar_t * expression,
176 const wchar_t * function, const wchar_t * file,
177 unsigned int line, uintptr_t pReserved)
189 /* Enable core dump on failure. */
190 static int dump_on_failure = 0;
191 /* Default is to remove temp dirs and log data for successful tests. */
192 static int keep_temp_files = 0;
193 /* Default is to run the specified tests once and report errors. */
194 static int until_failure = 0;
195 /* Default is to just report pass/fail for each test. */
196 static int verbosity = 0;
197 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
198 #define VERBOSITY_PASSFAIL 0 /* Default */
199 #define VERBOSITY_LIGHT_REPORT 1 /* -v */
200 #define VERBOSITY_FULL 2 /* -vv */
201 /* A few places generate even more output for verbosity > VERBOSITY_FULL,
202 * mostly for debugging the test harness itself. */
203 /* Cumulative count of assertion failures. */
204 static int failures = 0;
205 /* Cumulative count of reported skips. */
206 static int skips = 0;
207 /* Cumulative count of assertions checked. */
208 static int assertions = 0;
210 /* Directory where uuencoded reference files can be found. */
211 static const char *refdir;
214 * Report log information selectively to console and/or disk log.
216 static int log_console = 0;
217 static FILE *logfile;
219 vlogprintf(const char *fmt, va_list ap)
226 vfprintf(stdout, fmt, ap);
229 vfprintf(logfile, fmt, lfap);
232 vfprintf(logfile, fmt, ap);
237 logprintf(const char *fmt, ...)
245 /* Set up a message to display only if next assertion fails. */
246 static char msgbuff[4096];
247 static const char *msg, *nextmsg;
249 failure(const char *fmt, ...)
256 vsprintf(msgbuff, fmt, ap);
263 * Copy arguments into file-local variables.
264 * This was added to permit vararg assert() functions without needing
265 * variadic wrapper macros. Turns out that the vararg capability is almost
266 * never used, so almost all of the vararg assertions can be simplified
267 * by removing the vararg capability and reworking the wrapper macro to
268 * pass __FILE__, __LINE__ directly into the function instead of using
269 * this hook. I suspect this machinery is used so rarely that we
270 * would be better off just removing it entirely. That would simplify
271 * the code here noticeably.
273 static const char *skipping_filename;
274 static int skipping_line;
275 void skipping_setup(const char *filename, int line)
277 skipping_filename = filename;
278 skipping_line = line;
281 /* Called at the beginning of each assert() function. */
283 assertion_count(const char *file, int line)
285 (void)file; /* UNUSED */
286 (void)line; /* UNUSED */
288 /* Proper handling of "failure()" message. */
291 /* Uncomment to print file:line after every assertion.
292 * Verbose, but occasionally useful in tracking down crashes. */
293 /* printf("Checked %s:%d\n", file, line); */
297 * For each test source file, we remember how many times each
298 * assertion was reported. Cleared before each new test,
299 * used by test_summarize().
304 } failed_lines[10000];
305 const char *failed_filename;
307 /* Count this failure, setup up log destination and handle initial report. */
309 failure_start(const char *filename, int line, const char *fmt, ...)
313 /* Record another failure for this line. */
315 failed_filename = filename;
316 failed_lines[line].count++;
318 /* Determine whether to log header to console. */
320 case VERBOSITY_LIGHT_REPORT:
321 log_console = (failed_lines[line].count < 2);
324 log_console = (verbosity >= VERBOSITY_FULL);
327 /* Log file:line header for this failure */
330 logprintf("%s(%d): ", filename, line);
332 logprintf("%s:%d: ", filename, line);
338 if (msg != NULL && msg[0] != '\0') {
339 logprintf(" Description: %s\n", msg);
343 /* Determine whether to log details to console. */
344 if (verbosity == VERBOSITY_LIGHT_REPORT)
348 /* Complete reporting of failed tests. */
350 * The 'extra' hook here is used by libarchive to include libarchive
351 * error messages with assertion failures. It could also be used
352 * to add strerror() output, for example. Just define the EXTRA_DUMP()
353 * macro appropriately.
356 failure_finish(void *extra)
358 (void)extra; /* UNUSED (maybe) */
361 logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
362 logprintf(" detail: %s\n", EXTRA_DUMP(extra));
366 if (dump_on_failure) {
368 " *** forcing core dump so failure can be debugged ***\n");
374 /* Inform user that we're skipping some checks. */
376 test_skipping(const char *fmt, ...)
382 vsprintf(buff, fmt, ap);
384 /* Use failure() message if set. */
387 /* failure_start() isn't quite right, but is awfully convenient. */
388 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
389 --failures; /* Undo failures++ in failure_start() */
390 /* Don't failure_finish() here. */
391 /* Mark as skip, so doesn't count as failed test. */
392 failed_lines[skipping_line].skip = 1;
402 /* Generic assert() just displays the failed condition. */
404 assertion_assert(const char *file, int line, int value,
405 const char *condition, void *extra)
407 assertion_count(file, line);
409 failure_start(file, line, "Assertion failed: %s", condition);
410 failure_finish(extra);
415 /* chdir() and report any errors */
417 assertion_chdir(const char *file, int line, const char *pathname)
419 assertion_count(file, line);
420 if (chdir(pathname) == 0)
422 failure_start(file, line, "chdir(\"%s\")", pathname);
423 failure_finish(NULL);
428 /* Verify two integers are equal. */
430 assertion_equal_int(const char *file, int line,
431 long long v1, const char *e1, long long v2, const char *e2, void *extra)
433 assertion_count(file, line);
436 failure_start(file, line, "%s != %s", e1, e2);
437 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
438 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
439 failure_finish(extra);
444 * Utility to convert a single UTF-8 sequence.
447 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
449 static const char utf8_count[256] = {
450 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
451 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
452 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
453 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
454 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
455 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
456 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
457 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
458 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
459 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
460 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
461 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
462 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
463 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
464 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
465 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
477 * Decode 1-4 bytes depending on the value of the first byte.
479 ch = (unsigned char)*s;
481 return (0); /* Standard: return 0 for end-of-string. */
482 cnt = utf8_count[ch];
484 /* Invalide sequence or there are not plenty bytes. */
488 /* Make a Unicode code point from a single UTF-8 sequence. */
490 case 1: /* 1 byte sequence. */
493 case 2: /* 2 bytes sequence. */
494 if ((s[1] & 0xc0) != 0x80) return (-1);
495 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
497 case 3: /* 3 bytes sequence. */
498 if ((s[1] & 0xc0) != 0x80) return (-1);
499 if ((s[2] & 0xc0) != 0x80) return (-1);
500 wc = ((ch & 0x0f) << 12)
501 | ((s[1] & 0x3f) << 6)
504 return (-1);/* Overlong sequence. */
506 case 4: /* 4 bytes sequence. */
509 if ((s[1] & 0xc0) != 0x80) return (-1);
510 if ((s[2] & 0xc0) != 0x80) return (-1);
511 if ((s[3] & 0xc0) != 0x80) return (-1);
512 wc = ((ch & 0x07) << 18)
513 | ((s[1] & 0x3f) << 12)
514 | ((s[2] & 0x3f) << 6)
517 return (-1);/* Overlong sequence. */
523 /* The code point larger than 0x10FFFF is not leagal
527 /* Correctly gets a Unicode, returns used bytes. */
532 static void strdump(const char *e, const char *p, int ewidth, int utf8)
536 logprintf(" %*s = ", ewidth, e);
543 unsigned int c = 0xff & *p++;
545 case '\a': printf("\a"); break;
546 case '\b': printf("\b"); break;
547 case '\n': printf("\n"); break;
548 case '\r': printf("\r"); break;
550 if (c >= 32 && c < 127)
553 logprintf("\\x%02X", c);
557 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
560 * If the current string is UTF-8, dump its code points.
571 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
574 logprintf("%04X", uc);
580 logprintf(" (count %d", cnt);
582 logprintf(",unknown %d bytes", len);
590 /* Verify two strings are equal, dump them if not. */
592 assertion_equal_string(const char *file, int line,
593 const char *v1, const char *e1,
594 const char *v2, const char *e2,
595 void *extra, int utf8)
599 assertion_count(file, line);
600 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
602 failure_start(file, line, "%s != %s", e1, e2);
607 strdump(e1, v1, l1, utf8);
608 strdump(e2, v2, l1, utf8);
609 failure_finish(extra);
614 wcsdump(const char *e, const wchar_t *w)
616 logprintf(" %s = ", e);
622 while (*w != L'\0') {
623 unsigned int c = *w++;
624 if (c >= 32 && c < 127)
627 logprintf("\\x%02X", c);
628 else if (c < 0x10000)
629 logprintf("\\u%04X", c);
631 logprintf("\\U%08X", c);
638 wcscmp(const wchar_t *s1, const wchar_t *s2)
641 while (*s1 == *s2++) {
652 /* Verify that two wide strings are equal, dump them if not. */
654 assertion_equal_wstring(const char *file, int line,
655 const wchar_t *v1, const char *e1,
656 const wchar_t *v2, const char *e2,
659 assertion_count(file, line);
662 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
664 failure_start(file, line, "%s != %s", e1, e2);
667 failure_finish(extra);
672 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
673 * any bytes in p that differ from ref will be highlighted with '_'
674 * before and after the hex value.
677 hexdump(const char *p, const char *ref, size_t l, size_t offset)
683 logprintf("(null)\n");
686 for(i=0; i < l; i+=16) {
687 logprintf("%04x", (unsigned)(i + offset));
689 for (j = 0; j < 16 && i + j < l; j++) {
690 if (ref != NULL && p[i + j] != ref[i + j])
692 logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
693 if (ref != NULL && p[i + j] == ref[i + j])
696 for (; j < 16; j++) {
697 logprintf("%c ", sep);
700 logprintf("%c", sep);
701 for (j=0; j < 16 && i + j < l; j++) {
703 if (c >= ' ' && c <= 126)
712 /* Verify that two blocks of memory are the same, display the first
713 * block of differences if they're not. */
715 assertion_equal_mem(const char *file, int line,
716 const void *_v1, const char *e1,
717 const void *_v2, const char *e2,
718 size_t l, const char *ld, void *extra)
720 const char *v1 = (const char *)_v1;
721 const char *v2 = (const char *)_v2;
724 assertion_count(file, line);
725 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
728 failure_start(file, line, "%s != %s", e1, e2);
729 logprintf(" size %s = %d\n", ld, (int)l);
730 /* Dump 48 bytes (3 lines) so that the first difference is
731 * in the second line. */
733 while (l > 64 && memcmp(v1, v2, 32) == 0) {
734 /* Two lines agree, so step forward one line. */
740 logprintf(" Dump of %s\n", e1);
741 hexdump(v1, v2, l < 128 ? l : 128, offset);
742 logprintf(" Dump of %s\n", e2);
743 hexdump(v2, v1, l < 128 ? l : 128, offset);
745 failure_finish(extra);
749 /* Verify that the named file exists and is empty. */
751 assertion_empty_file(const char *filename, int line, const char *f1)
758 assertion_count(filename, line);
760 if (stat(f1, &st) != 0) {
761 failure_start(filename, line, "Stat failed: %s", f1);
762 failure_finish(NULL);
768 failure_start(filename, line, "File should be empty: %s", f1);
769 logprintf(" File size: %d\n", (int)st.st_size);
770 logprintf(" Contents:\n");
773 logprintf(" Unable to open %s\n", f1);
775 s = ((off_t)sizeof(buff) < st.st_size) ?
776 (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
777 s = fread(buff, 1, s, f);
778 hexdump(buff, NULL, s, 0);
781 failure_finish(NULL);
785 /* Verify that the named file exists and is not empty. */
787 assertion_non_empty_file(const char *filename, int line, const char *f1)
791 assertion_count(filename, line);
793 if (stat(f1, &st) != 0) {
794 failure_start(filename, line, "Stat failed: %s", f1);
795 failure_finish(NULL);
798 if (st.st_size == 0) {
799 failure_start(filename, line, "File empty: %s", f1);
800 failure_finish(NULL);
806 /* Verify that two files have the same contents. */
807 /* TODO: hexdump the first bytes that actually differ. */
809 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
816 assertion_count(filename, line);
818 f1 = fopen(fn1, "rb");
819 f2 = fopen(fn2, "rb");
821 n1 = fread(buff1, 1, sizeof(buff1), f1);
822 n2 = fread(buff2, 1, sizeof(buff2), f2);
825 if (n1 == 0 && n2 == 0) {
830 if (memcmp(buff1, buff2, n1) != 0)
835 failure_start(filename, line, "Files not identical");
836 logprintf(" file1=\"%s\"\n", fn1);
837 logprintf(" file2=\"%s\"\n", fn2);
838 failure_finish(NULL);
842 /* Verify that the named file does exist. */
844 assertion_file_exists(const char *filename, int line, const char *f)
846 assertion_count(filename, line);
848 #if defined(_WIN32) && !defined(__CYGWIN__)
852 if (!access(f, F_OK))
855 failure_start(filename, line, "File should exist: %s", f);
856 failure_finish(NULL);
860 /* Verify that the named file doesn't exist. */
862 assertion_file_not_exists(const char *filename, int line, const char *f)
864 assertion_count(filename, line);
866 #if defined(_WIN32) && !defined(__CYGWIN__)
873 failure_start(filename, line, "File should not exist: %s", f);
874 failure_finish(NULL);
878 /* Compare the contents of a file to a block of memory. */
880 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
886 assertion_count(filename, line);
890 failure_start(filename, line,
891 "File should exist: %s", fn);
892 failure_finish(NULL);
895 contents = malloc(s * 2);
896 n = fread(contents, 1, s * 2, f);
898 if (n == s && memcmp(buff, contents, s) == 0) {
902 failure_start(filename, line, "File contents don't match");
903 logprintf(" file=\"%s\"\n", fn);
905 hexdump(contents, buff, n > 512 ? 512 : n, 0);
907 logprintf(" File empty, contents should be:\n");
908 hexdump(buff, NULL, s > 512 ? 512 : s, 0);
910 failure_finish(NULL);
915 /* Check the contents of a text file, being tolerant of line endings. */
917 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
920 const char *btxt, *ftxt;
924 assertion_count(filename, line);
927 failure_start(filename, line,
928 "File doesn't exist: %s", fn);
929 failure_finish(NULL);
933 contents = malloc(s * 2 + 128);
934 n = fread(contents, 1, s * 2 + 128 - 1, f);
940 ftxt = (const char *)contents;
941 while (*btxt != '\0' && *ftxt != '\0') {
942 if (*btxt == *ftxt) {
947 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
948 /* Pass over different new line characters. */
955 if (*btxt == '\0' && *ftxt == '\0') {
959 failure_start(filename, line, "Contents don't match");
960 logprintf(" file=\"%s\"\n", fn);
962 hexdump(contents, buff, n, 0);
963 logprintf(" expected\n", fn);
964 hexdump(buff, contents, s, 0);
966 logprintf(" File empty, contents should be:\n");
967 hexdump(buff, NULL, s, 0);
969 failure_finish(NULL);
974 /* Verify that a text file contains the specified lines, regardless of order */
975 /* This could be more efficient if we sorted both sets of lines, etc, but
976 * since this is used only for testing and only ever deals with a dozen or so
977 * lines at a time, this relatively crude approach is just fine. */
979 assertion_file_contains_lines_any_order(const char *file, int line,
980 const char *pathname, const char *lines[])
984 size_t expected_count, actual_count, i, j;
988 int expected_failure = 0, actual_failure = 0;
990 assertion_count(file, line);
992 buff = slurpfile(&buff_size, "%s", pathname);
994 failure_start(pathname, line, "Can't read file: %s", pathname);
995 failure_finish(NULL);
999 /* Make a copy of the provided lines and count up the expected file size. */
1001 for (i = 0; lines[i] != NULL; ++i) {
1004 expected = malloc(sizeof(char *) * expected_count);
1005 for (i = 0; lines[i] != NULL; ++i) {
1006 expected[i] = strdup(lines[i]);
1009 /* Break the file into lines */
1011 for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1012 if (*p == '\x0d' || *p == '\x0a')
1014 if (c == '\0' && *p != '\0')
1018 actual = malloc(sizeof(char *) * actual_count);
1019 for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) {
1026 /* Erase matching lines from both lists */
1027 for (i = 0; i < expected_count; ++i) {
1028 if (expected[i] == NULL)
1030 for (j = 0; j < actual_count; ++j) {
1031 if (actual[j] == NULL)
1033 if (strcmp(expected[i], actual[j]) == 0) {
1042 /* If there's anything left, it's a failure */
1043 for (i = 0; i < expected_count; ++i) {
1044 if (expected[i] != NULL)
1047 for (j = 0; j < actual_count; ++j) {
1048 if (actual[j] != NULL)
1051 if (expected_failure == 0 && actual_failure == 0) {
1057 failure_start(file, line, "File doesn't match: %s", pathname);
1058 for (i = 0; i < expected_count; ++i) {
1059 if (expected[i] != NULL) {
1060 logprintf(" Expected but not present: %s\n", expected[i]);
1064 for (j = 0; j < actual_count; ++j) {
1065 if (actual[j] != NULL)
1066 logprintf(" Present but not expected: %s\n", actual[j]);
1068 failure_finish(NULL);
1075 /* Test that two paths point to the same file. */
1076 /* As a side-effect, asserts that both files exist. */
1078 is_hardlink(const char *file, int line,
1079 const char *path1, const char *path2)
1081 #if defined(_WIN32) && !defined(__CYGWIN__)
1082 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1085 assertion_count(file, line);
1086 r = my_GetFileInformationByName(path1, &bhfi1);
1088 failure_start(file, line, "File %s can't be inspected?", path1);
1089 failure_finish(NULL);
1092 r = my_GetFileInformationByName(path2, &bhfi2);
1094 failure_start(file, line, "File %s can't be inspected?", path2);
1095 failure_finish(NULL);
1098 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1099 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1100 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1102 struct stat st1, st2;
1105 assertion_count(file, line);
1106 r = lstat(path1, &st1);
1108 failure_start(file, line, "File should exist: %s", path1);
1109 failure_finish(NULL);
1112 r = lstat(path2, &st2);
1114 failure_start(file, line, "File should exist: %s", path2);
1115 failure_finish(NULL);
1118 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1123 assertion_is_hardlink(const char *file, int line,
1124 const char *path1, const char *path2)
1126 if (is_hardlink(file, line, path1, path2))
1128 failure_start(file, line,
1129 "Files %s and %s are not hardlinked", path1, path2);
1130 failure_finish(NULL);
1135 assertion_is_not_hardlink(const char *file, int line,
1136 const char *path1, const char *path2)
1138 if (!is_hardlink(file, line, path1, path2))
1140 failure_start(file, line,
1141 "Files %s and %s should not be hardlinked", path1, path2);
1142 failure_finish(NULL);
1146 /* Verify a/b/mtime of 'pathname'. */
1147 /* If 'recent', verify that it's within last 10 seconds. */
1149 assertion_file_time(const char *file, int line,
1150 const char *pathname, long t, long nsec, char type, int recent)
1152 long long filet, filet_nsec;
1155 #if defined(_WIN32) && !defined(__CYGWIN__)
1156 #define EPOC_TIME (116444736000000000ULL)
1157 FILETIME ftime, fbirthtime, fatime, fmtime;
1158 ULARGE_INTEGER wintm;
1160 ftime.dwLowDateTime = 0;
1161 ftime.dwHighDateTime = 0;
1163 assertion_count(file, line);
1164 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1165 * a directory file. If not, CreateFile() will fail when
1166 * the pathname is a directory. */
1167 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1168 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1169 if (h == INVALID_HANDLE_VALUE) {
1170 failure_start(file, line, "Can't access %s\n", pathname);
1171 failure_finish(NULL);
1174 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1176 case 'a': ftime = fatime; break;
1177 case 'b': ftime = fbirthtime; break;
1178 case 'm': ftime = fmtime; break;
1182 failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1183 failure_finish(NULL);
1186 wintm.LowPart = ftime.dwLowDateTime;
1187 wintm.HighPart = ftime.dwHighDateTime;
1188 filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1189 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1190 nsec = (nsec / 100) * 100; /* Round the request */
1194 assertion_count(file, line);
1195 r = lstat(pathname, &st);
1197 failure_start(file, line, "Can't stat %s\n", pathname);
1198 failure_finish(NULL);
1202 case 'a': filet = st.st_atime; break;
1203 case 'm': filet = st.st_mtime; break;
1204 case 'b': filet = 0; break;
1205 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1208 #if defined(__FreeBSD__)
1210 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1211 case 'b': filet = st.st_birthtime;
1212 filet_nsec = st.st_birthtimespec.tv_nsec; break;
1213 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1214 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1217 /* FreeBSD generally only stores to microsecond res, so round. */
1218 filet_nsec = (filet_nsec / 1000) * 1000;
1219 nsec = (nsec / 1000) * 1000;
1221 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1222 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1223 #if defined(__HAIKU__)
1224 if (type == 'a') return (1); /* Haiku doesn't have atime. */
1229 /* Check that requested time is up-to-date. */
1230 time_t now = time(NULL);
1231 if (filet < now - 10 || filet > now + 1) {
1232 failure_start(file, line,
1233 "File %s has %ctime %lld, %lld seconds ago\n",
1234 pathname, type, filet, now - filet);
1235 failure_finish(NULL);
1238 } else if (filet != t || filet_nsec != nsec) {
1239 failure_start(file, line,
1240 "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1241 pathname, type, filet, filet_nsec, t, nsec);
1242 failure_finish(NULL);
1248 /* Verify atime of 'pathname'. */
1250 assertion_file_atime(const char *file, int line,
1251 const char *pathname, long t, long nsec)
1253 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1256 /* Verify atime of 'pathname' is up-to-date. */
1258 assertion_file_atime_recent(const char *file, int line, const char *pathname)
1260 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1263 /* Verify birthtime of 'pathname'. */
1265 assertion_file_birthtime(const char *file, int line,
1266 const char *pathname, long t, long nsec)
1268 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1271 /* Verify birthtime of 'pathname' is up-to-date. */
1273 assertion_file_birthtime_recent(const char *file, int line,
1274 const char *pathname)
1276 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1279 /* Verify mtime of 'pathname'. */
1281 assertion_file_mtime(const char *file, int line,
1282 const char *pathname, long t, long nsec)
1284 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1287 /* Verify mtime of 'pathname' is up-to-date. */
1289 assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1291 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1294 /* Verify number of links to 'pathname'. */
1296 assertion_file_nlinks(const char *file, int line,
1297 const char *pathname, int nlinks)
1299 #if defined(_WIN32) && !defined(__CYGWIN__)
1300 BY_HANDLE_FILE_INFORMATION bhfi;
1303 assertion_count(file, line);
1304 r = my_GetFileInformationByName(pathname, &bhfi);
1305 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1307 failure_start(file, line, "File %s has %d links, expected %d",
1308 pathname, bhfi.nNumberOfLinks, nlinks);
1309 failure_finish(NULL);
1315 assertion_count(file, line);
1316 r = lstat(pathname, &st);
1317 if (r == 0 && (int)st.st_nlink == nlinks)
1319 failure_start(file, line, "File %s has %d links, expected %d",
1320 pathname, st.st_nlink, nlinks);
1321 failure_finish(NULL);
1326 /* Verify size of 'pathname'. */
1328 assertion_file_size(const char *file, int line, const char *pathname, long size)
1333 assertion_count(file, line);
1334 #if defined(_WIN32) && !defined(__CYGWIN__)
1336 BY_HANDLE_FILE_INFORMATION bhfi;
1337 r = !my_GetFileInformationByName(pathname, &bhfi);
1338 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1343 r = lstat(pathname, &st);
1344 filesize = st.st_size;
1347 if (r == 0 && filesize == size)
1349 failure_start(file, line, "File %s has size %ld, expected %ld",
1350 pathname, (long)filesize, (long)size);
1351 failure_finish(NULL);
1355 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1357 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1362 #if defined(_WIN32) && !defined(__CYGWIN__)
1363 (void)mode; /* UNUSED */
1365 assertion_count(file, line);
1366 r = lstat(pathname, &st);
1368 failure_start(file, line, "Dir should exist: %s", pathname);
1369 failure_finish(NULL);
1372 if (!S_ISDIR(st.st_mode)) {
1373 failure_start(file, line, "%s is not a dir", pathname);
1374 failure_finish(NULL);
1377 #if !defined(_WIN32) || defined(__CYGWIN__)
1378 /* Windows doesn't handle permissions the same way as POSIX,
1379 * so just ignore the mode tests. */
1380 /* TODO: Can we do better here? */
1381 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1382 failure_start(file, line, "Dir %s has wrong mode", pathname);
1383 logprintf(" Expected: 0%3o\n", mode);
1384 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1385 failure_finish(NULL);
1392 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1393 * verify that too. */
1395 assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1400 #if defined(_WIN32) && !defined(__CYGWIN__)
1401 (void)mode; /* UNUSED */
1403 assertion_count(file, line);
1404 r = lstat(pathname, &st);
1405 if (r != 0 || !S_ISREG(st.st_mode)) {
1406 failure_start(file, line, "File should exist: %s", pathname);
1407 failure_finish(NULL);
1410 #if !defined(_WIN32) || defined(__CYGWIN__)
1411 /* Windows doesn't handle permissions the same way as POSIX,
1412 * so just ignore the mode tests. */
1413 /* TODO: Can we do better here? */
1414 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1415 failure_start(file, line, "File %s has wrong mode", pathname);
1416 logprintf(" Expected: 0%3o\n", mode);
1417 logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1418 failure_finish(NULL);
1425 /* Check whether 'pathname' is a symbolic link. If 'contents' is
1426 * non-NULL, verify that the symlink has those contents. */
1428 is_symlink(const char *file, int line,
1429 const char *pathname, const char *contents)
1431 #if defined(_WIN32) && !defined(__CYGWIN__)
1432 (void)pathname; /* UNUSED */
1433 (void)contents; /* UNUSED */
1434 assertion_count(file, line);
1435 /* Windows sort-of has real symlinks, but they're only usable
1436 * by privileged users and are crippled even then, so there's
1437 * really not much point in bothering with this. */
1445 assertion_count(file, line);
1446 r = lstat(pathname, &st);
1448 failure_start(file, line,
1449 "Symlink should exist: %s", pathname);
1450 failure_finish(NULL);
1453 if (!S_ISLNK(st.st_mode))
1455 if (contents == NULL)
1457 linklen = readlink(pathname, buff, sizeof(buff));
1459 failure_start(file, line, "Can't read symlink %s", pathname);
1460 failure_finish(NULL);
1463 buff[linklen] = '\0';
1464 if (strcmp(buff, contents) != 0)
1470 /* Assert that path is a symlink that (optionally) contains contents. */
1472 assertion_is_symlink(const char *file, int line,
1473 const char *path, const char *contents)
1475 if (is_symlink(file, line, path, contents))
1478 failure_start(file, line, "File %s is not a symlink to %s",
1481 failure_start(file, line, "File %s is not a symlink", path);
1482 failure_finish(NULL);
1487 /* Create a directory and report any errors. */
1489 assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1491 assertion_count(file, line);
1492 #if defined(_WIN32) && !defined(__CYGWIN__)
1493 (void)mode; /* UNUSED */
1494 if (0 == _mkdir(dirname))
1497 if (0 == mkdir(dirname, mode))
1500 failure_start(file, line, "Could not create directory %s", dirname);
1501 failure_finish(NULL);
1505 /* Create a file with the specified contents and report any failures. */
1507 assertion_make_file(const char *file, int line,
1508 const char *path, int mode, const char *contents)
1510 #if defined(_WIN32) && !defined(__CYGWIN__)
1511 /* TODO: Rework this to set file mode as well. */
1513 (void)mode; /* UNUSED */
1514 assertion_count(file, line);
1515 f = fopen(path, "wb");
1517 failure_start(file, line, "Could not create file %s", path);
1518 failure_finish(NULL);
1521 if (contents != NULL) {
1522 if (strlen(contents)
1523 != fwrite(contents, 1, strlen(contents), f)) {
1525 failure_start(file, line,
1526 "Could not write file %s", path);
1527 failure_finish(NULL);
1535 assertion_count(file, line);
1536 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1538 failure_start(file, line, "Could not create %s", path);
1539 failure_finish(NULL);
1542 if (contents != NULL) {
1543 if ((ssize_t)strlen(contents)
1544 != write(fd, contents, strlen(contents))) {
1546 failure_start(file, line, "Could not write to %s", path);
1547 failure_finish(NULL);
1556 /* Create a hardlink and report any failures. */
1558 assertion_make_hardlink(const char *file, int line,
1559 const char *newpath, const char *linkto)
1563 assertion_count(file, line);
1564 #if defined(_WIN32) && !defined(__CYGWIN__)
1565 succeeded = my_CreateHardLinkA(newpath, linkto);
1567 succeeded = !link(linkto, newpath);
1573 failure_start(file, line, "Could not create hardlink");
1574 logprintf(" New link: %s\n", newpath);
1575 logprintf(" Old name: %s\n", linkto);
1576 failure_finish(NULL);
1580 /* Create a symlink and report any failures. */
1582 assertion_make_symlink(const char *file, int line,
1583 const char *newpath, const char *linkto)
1585 #if defined(_WIN32) && !defined(__CYGWIN__)
1586 int targetIsDir = 0; /* TODO: Fix this */
1587 assertion_count(file, line);
1588 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1591 assertion_count(file, line);
1592 if (0 == symlink(linkto, newpath))
1595 failure_start(file, line, "Could not create symlink");
1596 logprintf(" New link: %s\n", newpath);
1597 logprintf(" Old name: %s\n", linkto);
1598 failure_finish(NULL);
1602 /* Set umask, report failures. */
1604 assertion_umask(const char *file, int line, int mask)
1606 assertion_count(file, line);
1607 (void)file; /* UNUSED */
1608 (void)line; /* UNUSED */
1613 /* Set times, report failures. */
1615 assertion_utimes(const char *file, int line,
1616 const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1620 #if defined(_WIN32) && !defined(__CYGWIN__)
1621 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1622 + (((nsec)/1000)*10))
1624 ULARGE_INTEGER wintm;
1625 FILETIME fatime, fmtime;
1626 FILETIME *pat, *pmt;
1628 assertion_count(file, line);
1629 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1630 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1631 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1632 if (h == INVALID_HANDLE_VALUE) {
1633 failure_start(file, line, "Can't access %s\n", pathname);
1634 failure_finish(NULL);
1638 if (at > 0 || at_nsec > 0) {
1639 wintm.QuadPart = WINTIME(at, at_nsec);
1640 fatime.dwLowDateTime = wintm.LowPart;
1641 fatime.dwHighDateTime = wintm.HighPart;
1645 if (mt > 0 || mt_nsec > 0) {
1646 wintm.QuadPart = WINTIME(mt, mt_nsec);
1647 fmtime.dwLowDateTime = wintm.LowPart;
1648 fmtime.dwHighDateTime = wintm.HighPart;
1652 if (pat != NULL || pmt != NULL)
1653 r = SetFileTime(h, NULL, pat, pmt);
1658 failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1659 failure_finish(NULL);
1663 #else /* defined(_WIN32) && !defined(__CYGWIN__) */
1665 struct timeval times[2];
1667 #if !defined(__FreeBSD__)
1668 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
1670 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1673 r = lstat(pathname, &st);
1675 failure_start(file, line, "Can't stat %s\n", pathname);
1676 failure_finish(NULL);
1680 if (mt == 0 && mt_nsec == 0) {
1682 #if defined(__FreeBSD__)
1683 mt_nsec = st.st_mtimespec.tv_nsec;
1684 /* FreeBSD generally only stores to microsecond res, so round. */
1685 mt_nsec = (mt_nsec / 1000) * 1000;
1688 if (at == 0 && at_nsec == 0) {
1690 #if defined(__FreeBSD__)
1691 at_nsec = st.st_atimespec.tv_nsec;
1692 /* FreeBSD generally only stores to microsecond res, so round. */
1693 at_nsec = (at_nsec / 1000) * 1000;
1697 times[1].tv_sec = mt;
1698 times[1].tv_usec = mt_nsec / 1000;
1700 times[0].tv_sec = at;
1701 times[0].tv_usec = at_nsec / 1000;
1704 r = lutimes(pathname, times);
1706 r = utimes(pathname, times);
1709 failure_start(file, line, "Can't utimes %s\n", pathname);
1710 failure_finish(NULL);
1714 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1719 * UTILITIES for use by tests.
1724 * Check whether platform supports symlinks. This is intended
1725 * for tests to use in deciding whether to bother testing symlink
1726 * support; if the platform doesn't support symlinks, there's no point
1727 * in checking whether the program being tested can create them.
1729 * Note that the first time this test is called, we actually go out to
1730 * disk to create and verify a symlink. This is necessary because
1731 * symlink support is actually a property of a particular filesystem
1732 * and can thus vary between directories on a single system. After
1733 * the first call, this returns the cached result from memory, so it's
1734 * safe to call it as often as you wish.
1739 /* Remember the test result */
1740 static int value = 0, tested = 0;
1745 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, "a");
1746 /* Note: Cygwin has its own symlink() emulation that does not
1747 * use the Win32 CreateSymbolicLink() function. */
1748 #if defined(_WIN32) && !defined(__CYGWIN__)
1749 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1750 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1752 value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1753 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1759 * Can this platform run the gzip program?
1761 /* Platform-dependent options for hiding the output of a subcommand. */
1762 #if defined(_WIN32) && !defined(__CYGWIN__)
1763 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1765 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1770 static int tested = 0, value = 0;
1773 if (systemf("gzip -V %s", redirectArgs) == 0)
1780 * Can this platform run the gunzip program?
1785 static int tested = 0, value = 0;
1788 if (systemf("gunzip -V %s", redirectArgs) == 0)
1795 * Sleep as needed; useful for verifying disk timestamp changes by
1796 * ensuring that the wall-clock time has actually changed before we
1797 * go back to re-read something from disk.
1800 sleepUntilAfter(time_t t)
1802 while (t >= time(NULL))
1803 #if defined(_WIN32) && !defined(__CYGWIN__)
1811 * Call standard system() call, but build up the command line using
1812 * sprintf() conventions.
1815 systemf(const char *fmt, ...)
1822 vsprintf(buff, fmt, ap);
1823 if (verbosity > VERBOSITY_FULL)
1824 logprintf("Cmd: %s\n", buff);
1831 * Slurp a file into memory for ease of comparison and testing.
1832 * Returns size of file in 'sizep' if non-NULL, null-terminates
1833 * data in memory for ease of use.
1836 slurpfile(size_t * sizep, const char *fmt, ...)
1838 char filename[8192];
1847 vsprintf(filename, fmt, ap);
1850 f = fopen(filename, "rb");
1852 /* Note: No error; non-existent file is okay here. */
1855 r = fstat(fileno(f), &st);
1857 logprintf("Can't stat file %s\n", filename);
1861 p = malloc((size_t)st.st_size + 1);
1863 logprintf("Can't allocate %ld bytes of memory to read file %s\n",
1864 (long int)st.st_size, filename);
1868 bytes_read = fread(p, 1, (size_t)st.st_size, f);
1869 if (bytes_read < st.st_size) {
1870 logprintf("Can't read file %s\n", filename);
1875 p[st.st_size] = '\0';
1877 *sizep = (size_t)st.st_size;
1882 /* Read a uuencoded file from the reference directory, decode, and
1883 * write the result into the current directory. */
1884 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
1886 extract_reference_file(const char *name)
1891 sprintf(buff, "%s/%s.uu", refdir, name);
1892 in = fopen(buff, "r");
1893 failure("Couldn't open reference file %s", buff);
1897 /* Read up to and including the 'begin' line. */
1899 if (fgets(buff, sizeof(buff), in) == NULL) {
1900 /* TODO: This is a failure. */
1903 if (memcmp(buff, "begin ", 6) == 0)
1906 /* Now, decode the rest and write it. */
1907 /* Not a lot of error checking here; the input better be right. */
1908 out = fopen(name, "wb");
1909 while (fgets(buff, sizeof(buff), in) != NULL) {
1913 if (memcmp(buff, "end", 3) == 0)
1916 bytes = UUDECODE(*p++);
1919 /* Write out 1-3 bytes from that. */
1921 n = UUDECODE(*p++) << 18;
1922 n |= UUDECODE(*p++) << 12;
1923 fputc(n >> 16, out);
1927 n |= UUDECODE(*p++) << 6;
1928 fputc((n >> 8) & 0xFF, out);
1932 n |= UUDECODE(*p++);
1933 fputc(n & 0xFF, out);
1943 is_LargeInode(const char *file)
1945 #if defined(_WIN32) && !defined(__CYGWIN__)
1946 BY_HANDLE_FILE_INFORMATION bhfi;
1949 r = my_GetFileInformationByName(file, &bhfi);
1952 return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
1957 if (stat(file, &st) < 0)
1959 ino = (int64_t)st.st_ino;
1960 return (ino > 0xffffffff);
1970 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
1972 * DEFINE_TEST(test_function)
1976 /* Use "list.h" to declare all of the test functions. */
1978 #define DEFINE_TEST(name) void name(void);
1981 /* Use "list.h" to create a list of all tests (functions and names). */
1983 #define DEFINE_TEST(n) { n, #n, 0 },
1984 struct { void (*func)(void); const char *name; int failures; } tests[] = {
1989 * Summarize repeated failures in the just-completed test.
1992 test_summarize(int failed)
1996 switch (verbosity) {
1997 case VERBOSITY_SUMMARY_ONLY:
1998 printf(failed ? "E" : ".");
2001 case VERBOSITY_PASSFAIL:
2002 printf(failed ? "FAIL\n" : "ok\n");
2006 log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2008 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2009 if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2010 logprintf("%s:%d: Summary: Failed %d times\n",
2011 failed_filename, i, failed_lines[i].count);
2013 /* Clear the failure history for the next file. */
2014 failed_filename = NULL;
2015 memset(failed_lines, 0, sizeof(failed_lines));
2019 * Actually run a single test, with appropriate setup and cleanup.
2022 test_run(int i, const char *tmpdir)
2025 char logfilename[64];
2026 int failures_before = failures;
2029 switch (verbosity) {
2030 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2032 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2033 printf("%3d: %-50s", i, tests[i].name);
2036 default: /* Title of test, details will follow */
2037 printf("%3d: %s\n", i, tests[i].name);
2040 /* Chdir to the top-level work directory. */
2041 if (!assertChdir(tmpdir)) {
2043 "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2046 /* Create a log file for this test. */
2047 sprintf(logfilename, "%s.log", tests[i].name);
2048 logfile = fopen(logfilename, "w");
2049 fprintf(logfile, "%s\n\n", tests[i].name);
2050 /* Chdir() to a work dir for this specific test. */
2051 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2052 testworkdir = workdir;
2053 if (!assertMakeDir(testworkdir, 0755)
2054 || !assertChdir(testworkdir)) {
2056 "ERROR: Can't chdir to work dir %s\n", testworkdir);
2059 /* Explicitly reset the locale before each test. */
2060 setlocale(LC_ALL, "C");
2061 /* Record the umask before we run the test. */
2062 umask(oldumask = umask(0));
2064 * Run the actual test.
2068 * Clean up and report afterwards.
2074 setlocale(LC_ALL, "C");
2075 /* Reset directory. */
2076 if (!assertChdir(tmpdir)) {
2077 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2081 /* Report per-test summaries. */
2082 tests[i].failures = failures - failures_before;
2083 test_summarize(tests[i].failures);
2084 /* Close the per-test log file. */
2087 /* If there were no failures, we can remove the work dir and logfile. */
2088 if (tests[i].failures == 0) {
2089 if (!keep_temp_files && assertChdir(tmpdir)) {
2090 #if defined(_WIN32) && !defined(__CYGWIN__)
2091 /* Make sure not to leave empty directories.
2092 * Sometimes a processing of closing files used by tests
2093 * is not done, then rmdir will be failed and it will
2094 * leave a empty test directory. So we should wait a few
2095 * seconds and retry rmdir. */
2097 for (t = 0; t < 10; t++) {
2100 r = systemf("rmdir /S /Q %s", tests[i].name);
2104 systemf("del %s", logfilename);
2106 systemf("rm -rf %s", tests[i].name);
2107 systemf("rm %s", logfilename);
2111 /* Return appropriate status. */
2112 return (tests[i].failures);
2118 * MAIN and support routines.
2124 usage(const char *program)
2126 static const int limit = sizeof(tests) / sizeof(tests[0]);
2129 printf("Usage: %s [options] <test> <test> ...\n", program);
2130 printf("Default is to run all tests.\n");
2131 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2132 printf("Options:\n");
2133 printf(" -d Dump core after any failure, for debugging.\n");
2134 printf(" -k Keep all temp files.\n");
2135 printf(" Default: temp files for successful tests deleted.\n");
2137 printf(" -p <path> Path to executable to be tested.\n");
2138 printf(" Default: path taken from " ENVBASE " environment variable.\n");
2140 printf(" -q Quiet.\n");
2141 printf(" -r <dir> Path to dir containing reference files.\n");
2142 printf(" Default: Current directory.\n");
2143 printf(" -u Keep running specifies tests until one fails.\n");
2144 printf(" -v Verbose.\n");
2145 printf("Available tests:\n");
2146 for (i = 0; i < limit; i++)
2147 printf(" %d: %s\n", i, tests[i].name);
2152 get_refdir(const char *d)
2154 char tried[512] = { '\0' };
2158 /* If a dir was specified, try that */
2161 snprintf(buff, sizeof(buff), "%s", d);
2162 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2163 if (p != NULL) goto success;
2164 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2165 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2169 /* Get the current dir. */
2171 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2173 pwd = getcwd(NULL, 0);
2175 while (pwd[strlen(pwd) - 1] == '\n')
2176 pwd[strlen(pwd) - 1] = '\0';
2178 /* Look for a known file. */
2179 snprintf(buff, sizeof(buff), "%s", pwd);
2180 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2181 if (p != NULL) goto success;
2182 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2183 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2185 snprintf(buff, sizeof(buff), "%s/test", pwd);
2186 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2187 if (p != NULL) goto success;
2188 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2189 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2191 #if defined(LIBRARY)
2192 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
2194 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
2196 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2197 if (p != NULL) goto success;
2198 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2199 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2201 #if defined(PROGRAM_ALIAS)
2202 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
2203 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2204 if (p != NULL) goto success;
2205 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2206 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2209 if (memcmp(pwd, "/usr/obj", 8) == 0) {
2210 snprintf(buff, sizeof(buff), "%s", pwd + 8);
2211 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2212 if (p != NULL) goto success;
2213 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2214 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2216 snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
2217 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2218 if (p != NULL) goto success;
2219 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2220 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2224 printf("Unable to locate known reference file %s\n", KNOWNREF);
2225 printf(" Checked following directories:\n%s\n", tried);
2226 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2234 return strdup(buff);
2238 main(int argc, char **argv)
2240 static const int limit = sizeof(tests) / sizeof(tests[0]);
2241 int i = 0, j = 0, start, end, tests_run = 0, tests_failed = 0, option;
2243 char *refdir_alloc = NULL;
2244 const char *progname;
2246 const char *tmp, *option_arg, *p;
2247 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL;
2248 char tmpdir_timestamp[256];
2250 (void)argc; /* UNUSED */
2252 /* Get the current dir. */
2254 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2256 pwd = getcwd(NULL, 0);
2258 while (pwd[strlen(pwd) - 1] == '\n')
2259 pwd[strlen(pwd) - 1] = '\0';
2261 #if defined(HAVE__CrtSetReportMode)
2262 /* To stop to run the default invalid parameter handler. */
2263 _set_invalid_parameter_handler(invalid_parameter_handler);
2264 /* Disable annoying assertion message box. */
2265 _CrtSetReportMode(_CRT_ASSERT, 0);
2269 * Name of this program, used to build root of our temp directory
2272 progname = p = argv[0];
2273 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2275 fprintf(stderr, "ERROR: Out of memory.");
2278 strcpy(testprogdir, progname);
2279 while (*p != '\0') {
2280 /* Support \ or / dir separators for Windows compat. */
2281 if (*p == '/' || *p == '\\')
2289 testprogdir[i] = '\0';
2290 #if defined(_WIN32) && !defined(__CYGWIN__)
2291 if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2292 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2293 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2294 testprogdir[1] == ':' &&
2295 (testprogdir[2] == '/' || testprogdir[2] == '\\')))
2297 if (testprogdir[0] != '/')
2300 /* Fixup path for relative directories. */
2301 if ((testprogdir = (char *)realloc(testprogdir,
2302 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2304 fprintf(stderr, "ERROR: Out of memory.");
2307 memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2308 strlen(testprogdir));
2309 memcpy(testprogdir, pwd, strlen(pwd));
2310 testprogdir[strlen(pwd)] = '/';
2314 /* Get the target program from environment, if available. */
2315 testprogfile = getenv(ENVBASE);
2318 if (getenv("TMPDIR") != NULL)
2319 tmp = getenv("TMPDIR");
2320 else if (getenv("TMP") != NULL)
2321 tmp = getenv("TMP");
2322 else if (getenv("TEMP") != NULL)
2323 tmp = getenv("TEMP");
2324 else if (getenv("TEMPDIR") != NULL)
2325 tmp = getenv("TEMPDIR");
2329 /* Allow -d to be controlled through the environment. */
2330 if (getenv(ENVBASE "_DEBUG") != NULL)
2331 dump_on_failure = 1;
2333 /* Get the directory holding test files from environment. */
2334 refdir = getenv(ENVBASE "_TEST_FILES");
2337 * Parse options, without using getopt(), which isn't available
2340 ++argv; /* Skip program name */
2341 while (*argv != NULL) {
2346 while (*p != '\0') {
2349 /* If 'opt' takes an argument, parse that. */
2350 if (option == 'p' || option == 'r') {
2353 else if (*argv == NULL) {
2355 "Option -%c requires argument.\n",
2359 option_arg = *argv++;
2360 p = ""; /* End of this option word. */
2363 /* Now, handle the option. */
2366 dump_on_failure = 1;
2369 keep_temp_files = 1;
2373 testprogfile = option_arg;
2375 fprintf(stderr, "-p option not permitted\n");
2383 refdir = option_arg;
2392 fprintf(stderr, "Unrecognized option '%c'\n",
2400 * Sanity-check that our options make sense.
2403 if (testprogfile == NULL)
2405 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2406 strlen(PROGRAM) + 1)) == NULL)
2408 fprintf(stderr, "ERROR: Out of memory.");
2411 strcpy(tmp2, testprogdir);
2413 strcat(tmp2, PROGRAM);
2414 testprogfile = tmp2;
2419 #if defined(_WIN32) && !defined(__CYGWIN__)
2420 /* Command.com sometimes rejects '/' separators. */
2421 testprg = strdup(testprogfile);
2422 for (i = 0; testprg[i] != '\0'; i++) {
2423 if (testprg[i] == '/')
2426 testprogfile = testprg;
2428 /* Quote the name that gets put into shell command lines. */
2429 testprg = malloc(strlen(testprogfile) + 3);
2430 strcpy(testprg, "\"");
2431 strcat(testprg, testprogfile);
2432 strcat(testprg, "\"");
2437 #if !defined(_WIN32) && defined(SIGPIPE)
2438 { /* Ignore SIGPIPE signals */
2439 struct sigaction sa;
2440 sa.sa_handler = SIG_IGN;
2441 sigemptyset(&sa.sa_mask);
2443 sigaction(SIGPIPE, &sa, NULL);
2448 * Create a temp directory for the following tests.
2449 * Include the time the tests started as part of the name,
2450 * to make it easier to track the results of multiple tests.
2453 for (i = 0; ; i++) {
2454 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2455 "%Y-%m-%dT%H.%M.%S",
2457 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2458 tmpdir_timestamp, i);
2459 if (assertMakeDir(tmpdir,0755))
2463 "ERROR: Unable to create temp directory %s\n",
2470 * If the user didn't specify a directory for locating
2471 * reference files, try to find the reference files in
2472 * the "usual places."
2474 refdir = refdir_alloc = get_refdir(refdir);
2477 * Banner with basic information.
2480 printf("If tests fail or crash, details will be in:\n");
2481 printf(" %s\n", tmpdir);
2483 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2484 printf("Reference files will be read from: %s\n", refdir);
2486 printf("Running tests on: %s\n", testprog);
2488 printf("Exercising: ");
2490 printf("%s\n", EXTRA_VERSION);
2497 * Run some or all of the individual tests.
2502 if (*argv == NULL) {
2503 /* Default: Run all tests. */
2504 for (i = 0; i < limit; i++) {
2506 if (test_run(i, tmpdir)) {
2513 while (*(argv) != NULL) {
2514 if (**argv >= '0' && **argv <= '9') {
2517 while (*vp >= '0' && *vp <= '9') {
2524 } else if (*vp == '-') {
2530 while (*vp >= '0' && *vp <= '9') {
2537 printf("*** INVALID Test %s\n", *argv);
2542 if (start < 0 || end >= limit || start > end) {
2543 printf("*** INVALID Test %s\n", *argv);
2549 for (start = 0; start < limit; ++start) {
2550 if (strcmp(*argv, tests[start].name) == 0)
2554 if (start >= limit) {
2555 printf("*** INVALID Test ``%s''\n",
2559 /* usage() never returns */
2562 while (start <= end) {
2564 if (test_run(start, tmpdir)) {
2574 } while (until_failure);
2577 /* Must be freed after all tests run */
2583 * Report summary statistics.
2585 if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2587 printf("Totals:\n");
2588 printf(" Tests run: %8d\n", tests_run);
2589 printf(" Tests failed: %8d\n", tests_failed);
2590 printf(" Assertions checked:%8d\n", assertions);
2591 printf(" Assertions failed: %8d\n", failures);
2592 printf(" Skips reported: %8d\n", skips);
2596 printf("Failing tests:\n");
2597 for (i = 0; i < limit; ++i) {
2598 if (tests[i].failures)
2599 printf(" %d: %s (%d failures)\n", i,
2600 tests[i].name, tests[i].failures);
2603 printf("Details for failing tests: %s\n", tmpdir);
2606 if (verbosity == VERBOSITY_SUMMARY_ONLY)
2608 printf("%d tests passed, no failures\n", tests_run);
2613 /* If the final tmpdir is empty, we can remove it. */
2614 /* This should be the usual case when all tests succeed. */
2618 return (tests_failed ? 1 : 0);