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.
33
* This same file is used pretty much verbatim for all test harnesses.
35
* The next few lines are the only differences.
36
* TODO: Move this into a separate configuration header, have all test
37
* suites share one copy of this file.
39
__FBSDID("$FreeBSD: src/usr.bin/tar/test/main.c,v 1.6 2008/11/05 06:40:53 kientzle Exp $");
40
#define KNOWNREF "test_patterns_2.tar.uu"
41
#define ENVBASE "BSDTAR" /* Prefix for environment variables. */
42
#define PROGRAM "bsdtar" /* Name of program being tested. */
43
#undef LIBRARY /* Not testing a library. */
44
#undef EXTRA_DUMP /* How to dump extra data */
45
/* How to generate extra version info. */
46
#define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "")
50
* Windows support routines
52
* Note: Configuration is a tricky issue. Using HAVE_* feature macros
53
* in the test harness is dangerous because they cover up
54
* configuration errors. The classic example of this is omitting a
55
* configure check. If libarchive and libarchive_test both look for
56
* the same feature macro, such errors are hard to detect. Platform
57
* macros (e.g., _WIN32 or __GNUC__) are a little better, but can
58
* easily lead to very messy code. It's best to limit yourself
59
* to only the most generic programming techniques in the test harness
60
* and thus avoid conditionals altogether. Where that's not possible,
61
* try to minimize conditionals by grouping platform-specific tests in
62
* one place (e.g., test_acl_freebsd) or by adding new assert()
63
* functions (e.g., assertMakeHardlink()) to cover up platform
64
* differences. Platform-specific coding in libarchive_test is often
65
* a symptom that some capability is missing from libarchive itself.
67
#if defined(_WIN32) && !defined(__CYGWIN__)
74
#define S_ISDIR(m) ((m) & _S_IFDIR)
77
#define S_ISREG(m) ((m) & _S_IFREG)
79
#if !defined(__BORLANDC__)
80
#define access _access
85
#define fileno _fileno
87
/*#define fstat _fstat64*/
88
#if !defined(__BORLANDC__)
89
#define getcwd _getcwd
92
/*#define lstat _stat64*/
93
/*#define stat _stat64*/
95
#if !defined(__BORLANDC__)
96
#define strdup _strdup
99
#define int64_t __int64
102
#if defined(HAVE__CrtSetReportMode)
106
#if defined(_WIN32) && !defined(__CYGWIN__)
107
void *GetFunctionKernel32(const char *name)
109
static HINSTANCE lib;
113
lib = LoadLibrary("kernel32.dll");
116
fprintf(stderr, "Can't load kernel32.dll?!\n");
119
return (void *)GetProcAddress(lib, name);
123
my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
125
static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
129
f = GetFunctionKernel32("CreateSymbolicLinkA");
131
return f == NULL ? 0 : (*f)(linkname, target, flags);
135
my_CreateHardLinkA(const char *linkname, const char *target)
137
static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
141
f = GetFunctionKernel32("CreateHardLinkA");
143
return f == NULL ? 0 : (*f)(linkname, target, NULL);
147
my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
152
memset(bhfi, 0, sizeof(*bhfi));
153
h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
154
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
155
if (h == INVALID_HANDLE_VALUE)
157
r = GetFileInformationByHandle(h, bhfi);
163
#if defined(HAVE__CrtSetReportMode)
165
invalid_parameter_handler(const wchar_t * expression,
166
const wchar_t * function, const wchar_t * file,
167
unsigned int line, uintptr_t pReserved)
179
/* Enable core dump on failure. */
180
static int dump_on_failure = 0;
181
/* Default is to remove temp dirs and log data for successful tests. */
182
static int keep_temp_files = 0;
183
/* Default is to just report pass/fail for each test. */
184
static int verbosity = 0;
185
#define VERBOSITY_SUMMARY_ONLY -1 /* -q */
186
#define VERBOSITY_PASSFAIL 0 /* Default */
187
#define VERBOSITY_LIGHT_REPORT 1 /* -v */
188
#define VERBOSITY_FULL 2 /* -vv */
189
/* A few places generate even more output for verbosity > VERBOSITY_FULL,
190
* mostly for debugging the test harness itself. */
191
/* Cumulative count of assertion failures. */
192
static int failures = 0;
193
/* Cumulative count of reported skips. */
194
static int skips = 0;
195
/* Cumulative count of assertions checked. */
196
static int assertions = 0;
198
/* Directory where uuencoded reference files can be found. */
199
static const char *refdir;
202
* Report log information selectively to console and/or disk log.
204
static int log_console = 0;
205
static FILE *logfile;
207
vlogprintf(const char *fmt, va_list ap)
214
vfprintf(stdout, fmt, ap);
217
vfprintf(logfile, fmt, lfap);
220
vfprintf(logfile, fmt, ap);
225
logprintf(const char *fmt, ...)
233
/* Set up a message to display only if next assertion fails. */
234
static char msgbuff[4096];
235
static const char *msg, *nextmsg;
237
failure(const char *fmt, ...)
241
vsprintf(msgbuff, fmt, ap);
247
* Copy arguments into file-local variables.
248
* This was added to permit vararg assert() functions without needing
249
* variadic wrapper macros. Turns out that the vararg capability is almost
250
* never used, so almost all of the vararg assertions can be simplified
251
* by removing the vararg capability and reworking the wrapper macro to
252
* pass __FILE__, __LINE__ directly into the function instead of using
253
* this hook. I suspect this machinery is used so rarely that we
254
* would be better off just removing it entirely. That would simplify
255
* the code here noticably.
257
static const char *test_filename;
258
static int test_line;
259
static void *test_extra;
260
void assertion_setup(const char *filename, int line)
262
test_filename = filename;
266
/* Called at the beginning of each assert() function. */
268
assertion_count(const char *file, int line)
270
(void)file; /* UNUSED */
271
(void)line; /* UNUSED */
273
/* Proper handling of "failure()" message. */
276
/* Uncomment to print file:line after every assertion.
277
* Verbose, but occasionally useful in tracking down crashes. */
278
/* printf("Checked %s:%d\n", file, line); */
282
* For each test source file, we remember how many times each
283
* assertion was reported. Cleared before each new test,
284
* used by test_summarize().
289
} failed_lines[10000];
291
/* Count this failure, setup up log destination and handle initial report. */
293
failure_start(const char *filename, int line, const char *fmt, ...)
297
/* Record another failure for this line. */
299
/* test_filename = filename; */
300
failed_lines[line].count++;
302
/* Determine whether to log header to console. */
307
case VERBOSITY_LIGHT_REPORT:
308
log_console = (failed_lines[line].count < 2);
314
/* Log file:line header for this failure */
317
logprintf("%s(%d): ", filename, line);
319
logprintf("%s:%d: ", filename, line);
325
if (msg != NULL && msg[0] != '\0') {
326
logprintf(" Description: %s\n", msg);
330
/* Determine whether to log details to console. */
331
if (verbosity == VERBOSITY_LIGHT_REPORT)
335
/* Complete reporting of failed tests. */
337
* The 'extra' hook here is used by libarchive to include libarchive
338
* error messages with assertion failures. It could also be used
339
* to add strerror() output, for example. Just define the EXTRA_DUMP()
340
* macro appropriately.
343
failure_finish(void *extra)
345
(void)extra; /* UNUSED (maybe) */
348
logprintf(" detail: %s\n", EXTRA_DUMP(extra));
351
if (dump_on_failure) {
353
" *** forcing core dump so failure can be debugged ***\n");
359
/* Inform user that we're skipping some checks. */
361
test_skipping(const char *fmt, ...)
367
vsprintf(buff, fmt, ap);
369
/* failure_start() isn't quite right, but is awfully convenient. */
370
failure_start(test_filename, test_line, "SKIPPING: %s", buff);
371
--failures; /* Undo failures++ in failure_start() */
372
/* Don't failure_finish() here. */
373
/* Mark as skip, so doesn't count as failed test. */
374
failed_lines[test_line].skip = 1;
384
/* Generic assert() just displays the failed condition. */
386
assertion_assert(const char *file, int line, int value,
387
const char *condition, void *extra)
389
assertion_count(file, line);
391
failure_start(file, line, "Assertion failed: %s", condition);
392
failure_finish(extra);
397
/* chdir() and report any errors */
399
assertion_chdir(const char *file, int line, const char *pathname)
401
assertion_count(file, line);
402
if (chdir(pathname) == 0)
404
failure_start(file, line, "chdir(\"%s\")", pathname);
405
failure_finish(NULL);
410
/* Verify two integers are equal. */
412
assertion_equal_int(const char *file, int line,
413
long long v1, const char *e1, long long v2, const char *e2, void *extra)
415
assertion_count(file, line);
418
failure_start(file, line, "%s != %s", e1, e2);
419
logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
420
logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
421
failure_finish(extra);
425
static void strdump(const char *e, const char *p)
429
logprintf(" %s = ", e);
436
unsigned int c = 0xff & *p++;
438
case '\a': printf("\a"); break;
439
case '\b': printf("\b"); break;
440
case '\n': printf("\n"); break;
441
case '\r': printf("\r"); break;
443
if (c >= 32 && c < 127)
446
logprintf("\\x%02X", c);
450
logprintf(" (length %d)\n", q == NULL ? -1 : (int)strlen(q));
453
/* Verify two strings are equal, dump them if not. */
455
assertion_equal_string(const char *file, int line,
456
const char *v1, const char *e1,
457
const char *v2, const char *e2,
460
assertion_count(file, line);
461
if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
463
failure_start(file, line, "%s != %s", e1, e2);
466
failure_finish(extra);
471
wcsdump(const char *e, const wchar_t *w)
473
logprintf(" %s = ", e);
479
while (*w != L'\0') {
480
unsigned int c = *w++;
481
if (c >= 32 && c < 127)
484
logprintf("\\x%02X", c);
485
else if (c < 0x10000)
486
logprintf("\\u%04X", c);
488
logprintf("\\U%08X", c);
495
wcscmp(const wchar_t *s1, const wchar_t *s2)
498
while (*s1 == *s2++) {
509
/* Verify that two wide strings are equal, dump them if not. */
511
assertion_equal_wstring(const char *file, int line,
512
const wchar_t *v1, const char *e1,
513
const wchar_t *v2, const char *e2,
516
assertion_count(file, line);
517
if (v1 == v2 || wcscmp(v1, v2) == 0)
519
failure_start(file, line, "%s != %s", e1, e2);
522
failure_finish(extra);
527
* Pretty standard hexdump routine. As a bonus, if ref != NULL, then
528
* any bytes in p that differ from ref will be highlighted with '_'
529
* before and after the hex value.
532
hexdump(const char *p, const char *ref, size_t l, size_t offset)
538
logprintf("(null)\n");
541
for(i=0; i < l; i+=16) {
542
logprintf("%04x", (unsigned)(i + offset));
544
for (j = 0; j < 16 && i + j < l; j++) {
545
if (ref != NULL && p[i + j] != ref[i + j])
547
logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
548
if (ref != NULL && p[i + j] == ref[i + j])
551
for (; j < 16; j++) {
552
logprintf("%c ", sep);
555
logprintf("%c", sep);
556
for (j=0; j < 16 && i + j < l; j++) {
558
if (c >= ' ' && c <= 126)
567
/* Verify that two blocks of memory are the same, display the first
568
* block of differences if they're not. */
570
assertion_equal_mem(const char *file, int line,
571
const void *_v1, const char *e1,
572
const void *_v2, const char *e2,
573
size_t l, const char *ld, void *extra)
575
const char *v1 = (const char *)_v1;
576
const char *v2 = (const char *)_v2;
579
assertion_count(file, line);
580
if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
583
failure_start(file, line, "%s != %s", e1, e2);
584
logprintf(" size %s = %d\n", ld, (int)l);
585
/* Dump 48 bytes (3 lines) so that the first difference is
586
* in the second line. */
588
while (l > 64 && memcmp(v1, v2, 32) == 0) {
589
/* Two lines agree, so step forward one line. */
595
logprintf(" Dump of %s\n", e1);
596
hexdump(v1, v2, l < 64 ? l : 64, offset);
597
logprintf(" Dump of %s\n", e2);
598
hexdump(v2, v1, l < 64 ? l : 64, offset);
600
failure_finish(extra);
604
/* Verify that the named file exists and is empty. */
606
assertion_empty_file(const char *f1fmt, ...)
615
assertion_count(test_filename, test_line);
617
vsprintf(f1, f1fmt, ap);
620
if (stat(f1, &st) != 0) {
621
failure_start(test_filename, test_line, "Stat failed: %s", f1);
622
failure_finish(NULL);
628
failure_start(test_filename, test_line, "File should be empty: %s", f1);
629
logprintf(" File size: %d\n", (int)st.st_size);
630
logprintf(" Contents:\n");
633
logprintf(" Unable to open %s\n", f1);
635
s = ((off_t)sizeof(buff) < st.st_size) ?
636
(ssize_t)sizeof(buff) : (ssize_t)st.st_size;
637
s = fread(buff, 1, s, f);
638
hexdump(buff, NULL, s, 0);
641
failure_finish(NULL);
645
/* Verify that the named file exists and is not empty. */
647
assertion_non_empty_file(const char *f1fmt, ...)
653
assertion_count(test_filename, test_line);
655
vsprintf(f1, f1fmt, ap);
658
if (stat(f1, &st) != 0) {
659
failure_start(test_filename, test_line, "Stat failed: %s", f1);
660
failure_finish(NULL);
663
if (st.st_size == 0) {
664
failure_start(test_filename, test_line, "File empty: %s", f1);
665
failure_finish(NULL);
671
/* Verify that two files have the same contents. */
672
/* TODO: hexdump the first bytes that actually differ. */
674
assertion_equal_file(const char *fn1, const char *f2pattern, ...)
683
assertion_count(test_filename, test_line);
684
va_start(ap, f2pattern);
685
vsprintf(fn2, f2pattern, ap);
688
f1 = fopen(fn1, "rb");
689
f2 = fopen(fn2, "rb");
691
n1 = fread(buff1, 1, sizeof(buff1), f1);
692
n2 = fread(buff2, 1, sizeof(buff2), f2);
695
if (n1 == 0 && n2 == 0) {
700
if (memcmp(buff1, buff2, n1) != 0)
705
failure_start(test_filename, test_line, "Files not identical");
706
logprintf(" file1=\"%s\"\n", fn1);
707
logprintf(" file2=\"%s\"\n", fn2);
708
failure_finish(test_extra);
712
/* Verify that the named file does exist. */
714
assertion_file_exists(const char *fpattern, ...)
719
assertion_count(test_filename, test_line);
720
va_start(ap, fpattern);
721
vsprintf(f, fpattern, ap);
724
#if defined(_WIN32) && !defined(__CYGWIN__)
728
if (!access(f, F_OK))
731
failure_start(test_filename, test_line, "File should exist: %s", f);
732
failure_finish(test_extra);
736
/* Verify that the named file doesn't exist. */
738
assertion_file_not_exists(const char *fpattern, ...)
743
assertion_count(test_filename, test_line);
744
va_start(ap, fpattern);
745
vsprintf(f, fpattern, ap);
748
#if defined(_WIN32) && !defined(__CYGWIN__)
755
failure_start(test_filename, test_line, "File should not exist: %s", f);
756
failure_finish(test_extra);
760
/* Compare the contents of a file to a block of memory. */
762
assertion_file_contents(const void *buff, int s, const char *fpattern, ...)
770
assertion_count(test_filename, test_line);
771
va_start(ap, fpattern);
772
vsprintf(fn, fpattern, ap);
777
failure_start(test_filename, test_line,
778
"File should exist: %s", fn);
779
failure_finish(test_extra);
782
contents = malloc(s * 2);
783
n = fread(contents, 1, s * 2, f);
785
if (n == s && memcmp(buff, contents, s) == 0) {
789
failure_start(test_filename, test_line, "File contents don't match");
790
logprintf(" file=\"%s\"\n", fn);
792
hexdump(contents, buff, n > 512 ? 512 : n, 0);
794
logprintf(" File empty, contents should be:\n");
795
hexdump(buff, NULL, s > 512 ? 512 : s, 0);
797
failure_finish(test_extra);
802
/* Check the contents of a text file, being tolerant of line endings. */
804
assertion_text_file_contents(const char *buff, const char *fn)
807
const char *btxt, *ftxt;
811
assertion_count(test_filename, test_line);
814
contents = malloc(s * 2 + 128);
815
n = fread(contents, 1, s * 2 + 128 - 1, f);
821
ftxt = (const char *)contents;
822
while (*btxt != '\0' && *ftxt != '\0') {
823
if (*btxt == *ftxt) {
828
if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
829
/* Pass over different new line characters. */
836
if (*btxt == '\0' && *ftxt == '\0') {
840
failure_start(test_filename, test_line, "Contents don't match");
841
logprintf(" file=\"%s\"\n", fn);
843
hexdump(contents, buff, n, 0);
845
logprintf(" File empty, contents should be:\n");
846
hexdump(buff, NULL, s, 0);
848
failure_finish(test_extra);
853
/* Verify that a text file contains the specified lines, regardless of order */
854
/* This could be more efficient if we sorted both sets of lines, etc, but
855
* since this is used only for testing and only ever deals with a dozen or so
856
* lines at a time, this relatively crude approach is just fine. */
858
assertion_file_contains_lines_any_order(const char *file, int line,
859
const char *pathname, const char *lines[])
863
size_t expected_count, actual_count, i, j;
867
int expected_failure = 0, actual_failure = 0;
869
assertion_count(file, line);
871
buff = slurpfile(&buff_size, "%s", pathname);
873
failure_start(pathname, line, "Can't read file: %s", pathname);
874
failure_finish(NULL);
878
// Make a copy of the provided lines and count up the expected file size.
880
for (i = 0; lines[i] != NULL; ++i) {
883
expected = malloc(sizeof(char *) * expected_count);
884
for (i = 0; lines[i] != NULL; ++i) {
885
expected[i] = strdup(lines[i]);
888
// Break the file into lines
890
for (c = '\0', p = buff; p < buff + buff_size; ++p) {
891
if (*p == '\x0d' || *p == '\x0a')
893
if (c == '\0' && *p != '\0')
897
actual = malloc(sizeof(char *) * actual_count);
898
for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) {
905
// Erase matching lines from both lists
906
for (i = 0; i < expected_count; ++i) {
907
if (expected[i] == NULL)
909
for (j = 0; j < actual_count; ++j) {
910
if (actual[j] == NULL)
912
if (strcmp(expected[i], actual[j]) == 0) {
921
// If there's anything left, it's a failure
922
for (i = 0; i < expected_count; ++i) {
923
if (expected[i] != NULL)
926
for (j = 0; j < actual_count; ++j) {
927
if (actual[j] != NULL)
930
if (expected_failure == 0 && actual_failure == 0) {
936
failure_start(file, line, "File doesn't match: %s", pathname);
937
for (i = 0; i < expected_count; ++i) {
938
if (expected[i] != NULL) {
939
logprintf(" Expected but not present: %s\n", expected[i]);
943
for (j = 0; j < actual_count; ++j) {
944
if (actual[j] != NULL)
945
logprintf(" Present but not expected: %s\n", actual[j]);
947
failure_finish(NULL);
954
/* Test that two paths point to the same file. */
955
/* As a side-effect, asserts that both files exist. */
957
is_hardlink(const char *file, int line,
958
const char *path1, const char *path2)
960
#if defined(_WIN32) && !defined(__CYGWIN__)
961
BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
964
assertion_count(file, line);
965
r = my_GetFileInformationByName(path1, &bhfi1);
967
failure_start(file, line, "File %s can't be inspected?", path1);
968
failure_finish(NULL);
971
r = my_GetFileInformationByName(path2, &bhfi2);
973
failure_start(file, line, "File %s can't be inspected?", path2);
974
failure_finish(NULL);
977
return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
978
&& bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
979
&& bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
981
struct stat st1, st2;
984
assertion_count(file, line);
985
r = lstat(path1, &st1);
987
failure_start(file, line, "File should exist: %s", path1);
988
failure_finish(NULL);
991
r = lstat(path2, &st2);
993
failure_start(file, line, "File should exist: %s", path2);
994
failure_finish(NULL);
997
return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1002
assertion_is_hardlink(const char *file, int line,
1003
const char *path1, const char *path2)
1005
if (is_hardlink(file, line, path1, path2))
1007
failure_start(file, line,
1008
"Files %s and %s are not hardlinked", path1, path2);
1009
failure_finish(NULL);
1014
assertion_is_not_hardlink(const char *file, int line,
1015
const char *path1, const char *path2)
1017
if (!is_hardlink(file, line, path1, path2))
1019
failure_start(file, line,
1020
"Files %s and %s should not be hardlinked", path1, path2);
1021
failure_finish(NULL);
1025
/* Verify a/b/mtime of 'pathname'. */
1026
/* If 'recent', verify that it's within last 10 seconds. */
1028
assertion_file_time(const char *file, int line,
1029
const char *pathname, long t, long nsec, char type, int recent)
1031
long long filet, filet_nsec;
1034
#if defined(_WIN32) && !defined(__CYGWIN__)
1035
#define EPOC_TIME (116444736000000000ULL)
1036
FILETIME ftime, fbirthtime, fatime, fmtime;
1037
ULARGE_INTEGER wintm;
1039
ftime.dwLowDateTime = 0;
1040
ftime.dwHighDateTime = 0;
1042
assertion_count(file, line);
1043
h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1044
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1045
if (h == INVALID_HANDLE_VALUE) {
1046
failure_start(file, line, "Can't access %s\n", pathname);
1047
failure_finish(NULL);
1050
r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1052
case 'a': ftime = fatime; break;
1053
case 'b': ftime = fbirthtime; break;
1054
case 'm': ftime = fmtime; break;
1058
failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1059
failure_finish(NULL);
1062
wintm.LowPart = ftime.dwLowDateTime;
1063
wintm.HighPart = ftime.dwHighDateTime;
1064
filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1065
filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1066
nsec = (nsec / 100) * 100; /* Round the request */
1070
assertion_count(file, line);
1071
r = lstat(pathname, &st);
1073
failure_start(file, line, "Can't stat %s\n", pathname);
1074
failure_finish(NULL);
1078
case 'a': filet = st.st_atime; break;
1079
case 'm': filet = st.st_mtime; break;
1080
case 'b': filet = 0; break;
1081
default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1084
#if defined(__FreeBSD__)
1086
case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1087
case 'b': filet = st.st_birthtime;
1088
filet_nsec = st.st_birthtimespec.tv_nsec; break;
1089
case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1090
default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1093
/* FreeBSD generally only stores to microsecond res, so round. */
1094
filet_nsec = (filet_nsec / 1000) * 1000;
1095
nsec = (nsec / 1000) * 1000;
1097
filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
1098
if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1099
#if defined(__HAIKU__)
1100
if (type == 'a') return (1); /* Haiku doesn't have atime. */
1105
/* Check that requested time is up-to-date. */
1106
time_t now = time(NULL);
1107
if (filet < now - 10 || filet > now + 1) {
1108
failure_start(file, line,
1109
"File %s has %ctime %ld, %ld seconds ago\n",
1110
pathname, type, filet, now - filet);
1111
failure_finish(NULL);
1114
} else if (filet != t || filet_nsec != nsec) {
1115
failure_start(file, line,
1116
"File %s has %ctime %ld.%09ld, expected %ld.%09ld",
1117
pathname, type, filet, filet_nsec, t, nsec);
1118
failure_finish(NULL);
1124
/* Verify atime of 'pathname'. */
1126
assertion_file_atime(const char *file, int line,
1127
const char *pathname, long t, long nsec)
1129
return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1132
/* Verify atime of 'pathname' is up-to-date. */
1134
assertion_file_atime_recent(const char *file, int line, const char *pathname)
1136
return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1139
/* Verify birthtime of 'pathname'. */
1141
assertion_file_birthtime(const char *file, int line,
1142
const char *pathname, long t, long nsec)
1144
return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1147
/* Verify birthtime of 'pathname' is up-to-date. */
1149
assertion_file_birthtime_recent(const char *file, int line,
1150
const char *pathname)
1152
return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1155
/* Verify mtime of 'pathname'. */
1157
assertion_file_mtime(const char *file, int line,
1158
const char *pathname, long t, long nsec)
1160
return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1163
/* Verify mtime of 'pathname' is up-to-date. */
1165
assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1167
return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1170
/* Verify number of links to 'pathname'. */
1172
assertion_file_nlinks(const char *file, int line,
1173
const char *pathname, int nlinks)
1175
#if defined(_WIN32) && !defined(__CYGWIN__)
1176
BY_HANDLE_FILE_INFORMATION bhfi;
1179
assertion_count(file, line);
1180
r = my_GetFileInformationByName(pathname, &bhfi);
1181
if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1183
failure_start(file, line, "File %s has %d links, expected %d",
1184
pathname, bhfi.nNumberOfLinks, nlinks);
1185
failure_finish(NULL);
1191
assertion_count(file, line);
1192
r = lstat(pathname, &st);
1193
if (r == 0 && st.st_nlink == nlinks)
1195
failure_start(file, line, "File %s has %d links, expected %d",
1196
pathname, st.st_nlink, nlinks);
1197
failure_finish(NULL);
1202
/* Verify size of 'pathname'. */
1204
assertion_file_size(const char *file, int line, const char *pathname, long size)
1209
assertion_count(file, line);
1210
#if defined(_WIN32) && !defined(__CYGWIN__)
1212
BY_HANDLE_FILE_INFORMATION bhfi;
1213
r = !my_GetFileInformationByName(pathname, &bhfi);
1214
filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1219
r = lstat(pathname, &st);
1220
filesize = st.st_size;
1223
if (r == 0 && filesize == size)
1225
failure_start(file, line, "File %s has size %ld, expected %ld",
1226
pathname, (long)filesize, (long)size);
1227
failure_finish(NULL);
1231
/* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
1233
assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1238
#if defined(_WIN32) && !defined(__CYGWIN__)
1239
(void)mode; /* UNUSED */
1241
assertion_count(file, line);
1242
r = lstat(pathname, &st);
1244
failure_start(file, line, "Dir should exist: %s", pathname);
1245
failure_finish(NULL);
1248
if (!S_ISDIR(st.st_mode)) {
1249
failure_start(file, line, "%s is not a dir", pathname);
1250
failure_finish(NULL);
1253
#if !defined(_WIN32) || defined(__CYGWIN__)
1254
/* Windows doesn't handle permissions the same way as POSIX,
1255
* so just ignore the mode tests. */
1256
/* TODO: Can we do better here? */
1257
if (mode >= 0 && mode != (st.st_mode & 07777)) {
1258
failure_start(file, line, "Dir %s has wrong mode", pathname);
1259
logprintf(" Expected: 0%3o\n", mode);
1260
logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1261
failure_finish(NULL);
1268
/* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
1269
* verify that too. */
1271
assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1276
#if defined(_WIN32) && !defined(__CYGWIN__)
1277
(void)mode; /* UNUSED */
1279
assertion_count(file, line);
1280
r = lstat(pathname, &st);
1281
if (r != 0 || !S_ISREG(st.st_mode)) {
1282
failure_start(file, line, "File should exist: %s", pathname);
1283
failure_finish(NULL);
1286
#if !defined(_WIN32) || defined(__CYGWIN__)
1287
/* Windows doesn't handle permissions the same way as POSIX,
1288
* so just ignore the mode tests. */
1289
/* TODO: Can we do better here? */
1290
if (mode >= 0 && mode != (st.st_mode & 07777)) {
1291
failure_start(file, line, "File %s has wrong mode", pathname);
1292
logprintf(" Expected: 0%3o\n", mode);
1293
logprintf(" Found: 0%3o\n", st.st_mode & 07777);
1294
failure_finish(NULL);
1301
/* Check whether 'pathname' is a symbolic link. If 'contents' is
1302
* non-NULL, verify that the symlink has those contents. */
1304
is_symlink(const char *file, int line,
1305
const char *pathname, const char *contents)
1307
#if defined(_WIN32) && !defined(__CYGWIN__)
1308
(void)pathname; /* UNUSED */
1309
(void)contents; /* UNUSED */
1310
assertion_count(file, line);
1311
/* Windows sort-of has real symlinks, but they're only usable
1312
* by privileged users and are crippled even then, so there's
1313
* really not much point in bothering with this. */
1321
assertion_count(file, line);
1322
r = lstat(pathname, &st);
1324
failure_start(file, line,
1325
"Symlink should exist: %s", pathname);
1326
failure_finish(NULL);
1329
if (!S_ISLNK(st.st_mode))
1331
if (contents == NULL)
1333
linklen = readlink(pathname, buff, sizeof(buff));
1335
failure_start(file, line, "Can't read symlink %s", pathname);
1336
failure_finish(NULL);
1339
buff[linklen] = '\0';
1340
if (strcmp(buff, contents) != 0)
1346
/* Assert that path is a symlink that (optionally) contains contents. */
1348
assertion_is_symlink(const char *file, int line,
1349
const char *path, const char *contents)
1351
if (is_symlink(file, line, path, contents))
1354
failure_start(file, line, "File %s is not a symlink to %s",
1357
failure_start(file, line, "File %s is not a symlink", path);
1358
failure_finish(NULL);
1363
/* Create a directory and report any errors. */
1365
assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1367
assertion_count(file, line);
1368
#if defined(_WIN32) && !defined(__CYGWIN__)
1369
(void)mode; /* UNUSED */
1370
if (0 == _mkdir(dirname))
1373
if (0 == mkdir(dirname, mode))
1376
failure_start(file, line, "Could not create directory %s", dirname);
1377
failure_finish(NULL);
1381
/* Create a file with the specified contents and report any failures. */
1383
assertion_make_file(const char *file, int line,
1384
const char *path, int mode, const char *contents)
1386
#if defined(_WIN32) && !defined(__CYGWIN__)
1387
/* TODO: Rework this to set file mode as well. */
1389
(void)mode; /* UNUSED */
1390
assertion_count(file, line);
1391
f = fopen(path, "wb");
1393
failure_start(file, line, "Could not create file %s", path);
1394
failure_finish(NULL);
1397
if (contents != NULL) {
1398
if (strlen(contents)
1399
!= fwrite(contents, 1, strlen(contents), f)) {
1401
failure_start(file, line,
1402
"Could not write file %s", path);
1403
failure_finish(NULL);
1411
assertion_count(file, line);
1412
fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1414
failure_start(file, line, "Could not create %s", path);
1415
failure_finish(NULL);
1418
if (contents != NULL) {
1419
if ((ssize_t)strlen(contents)
1420
!= write(fd, contents, strlen(contents))) {
1422
failure_start(file, line, "Could not write to %s", path);
1423
failure_finish(NULL);
1432
/* Create a hardlink and report any failures. */
1434
assertion_make_hardlink(const char *file, int line,
1435
const char *newpath, const char *linkto)
1439
assertion_count(file, line);
1440
#if defined(_WIN32) && !defined(__CYGWIN__)
1441
succeeded = my_CreateHardLinkA(newpath, linkto);
1443
succeeded = !link(linkto, newpath);
1449
failure_start(file, line, "Could not create hardlink");
1450
logprintf(" New link: %s\n", newpath);
1451
logprintf(" Old name: %s\n", linkto);
1452
failure_finish(NULL);
1456
/* Create a symlink and report any failures. */
1458
assertion_make_symlink(const char *file, int line,
1459
const char *newpath, const char *linkto)
1461
#if defined(_WIN32) && !defined(__CYGWIN__)
1462
int targetIsDir = 0; /* TODO: Fix this */
1463
assertion_count(file, line);
1464
if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1467
assertion_count(file, line);
1468
if (0 == symlink(linkto, newpath))
1471
failure_start(file, line, "Could not create symlink");
1472
logprintf(" New link: %s\n", newpath);
1473
logprintf(" Old name: %s\n", linkto);
1474
failure_finish(NULL);
1478
/* Set umask, report failures. */
1480
assertion_umask(const char *file, int line, int mask)
1482
assertion_count(file, line);
1483
(void)file; /* UNUSED */
1484
(void)line; /* UNUSED */
1491
* UTILITIES for use by tests.
1496
* Check whether platform supports symlinks. This is intended
1497
* for tests to use in deciding whether to bother testing symlink
1498
* support; if the platform doesn't support symlinks, there's no point
1499
* in checking whether the program being tested can create them.
1501
* Note that the first time this test is called, we actually go out to
1502
* disk to create and verify a symlink. This is necessary because
1503
* symlink support is actually a property of a particular filesystem
1504
* and can thus vary between directories on a single system. After
1505
* the first call, this returns the cached result from memory, so it's
1506
* safe to call it as often as you wish.
1511
/* Remember the test result */
1512
static int value = 0, tested = 0;
1517
assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, "a");
1518
/* Note: Cygwin has its own symlink() emulation that does not
1519
* use the Win32 CreateSymbolicLink() function. */
1520
#if defined(_WIN32) && !defined(__CYGWIN__)
1521
value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1522
&& is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1524
value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1525
&& is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1531
* Can this platform run the gzip program?
1533
/* Platform-dependent options for hiding the output of a subcommand. */
1534
#if defined(_WIN32) && !defined(__CYGWIN__)
1535
static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1537
static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1542
static int tested = 0, value = 0;
1545
if (systemf("gzip -V %s", redirectArgs) == 0)
1552
* Can this platform run the gunzip program?
1557
static int tested = 0, value = 0;
1560
if (systemf("gunzip -V %s", redirectArgs) == 0)
1567
* Sleep as needed; useful for verifying disk timestamp changes by
1568
* ensuring that the wall-clock time has actually changed before we
1569
* go back to re-read something from disk.
1572
sleepUntilAfter(time_t t)
1574
while (t >= time(NULL))
1575
#if defined(_WIN32) && !defined(__CYGWIN__)
1583
* Call standard system() call, but build up the command line using
1584
* sprintf() conventions.
1587
systemf(const char *fmt, ...)
1594
vsprintf(buff, fmt, ap);
1595
if (verbosity > VERBOSITY_FULL)
1596
logprintf("Cmd: %s\n", buff);
1603
* Slurp a file into memory for ease of comparison and testing.
1604
* Returns size of file in 'sizep' if non-NULL, null-terminates
1605
* data in memory for ease of use.
1608
slurpfile(size_t * sizep, const char *fmt, ...)
1610
char filename[8192];
1619
vsprintf(filename, fmt, ap);
1622
f = fopen(filename, "rb");
1624
/* Note: No error; non-existent file is okay here. */
1627
r = fstat(fileno(f), &st);
1629
logprintf("Can't stat file %s\n", filename);
1633
p = malloc((size_t)st.st_size + 1);
1635
logprintf("Can't allocate %ld bytes of memory to read file %s\n",
1636
(long int)st.st_size, filename);
1640
bytes_read = fread(p, 1, (size_t)st.st_size, f);
1641
if (bytes_read < st.st_size) {
1642
logprintf("Can't read file %s\n", filename);
1647
p[st.st_size] = '\0';
1649
*sizep = (size_t)st.st_size;
1654
/* Read a uuencoded file from the reference directory, decode, and
1655
* write the result into the current directory. */
1656
#define UUDECODE(c) (((c) - 0x20) & 0x3f)
1658
extract_reference_file(const char *name)
1663
sprintf(buff, "%s/%s.uu", refdir, name);
1664
in = fopen(buff, "r");
1665
failure("Couldn't open reference file %s", buff);
1669
/* Read up to and including the 'begin' line. */
1671
if (fgets(buff, sizeof(buff), in) == NULL) {
1672
/* TODO: This is a failure. */
1675
if (memcmp(buff, "begin ", 6) == 0)
1678
/* Now, decode the rest and write it. */
1679
/* Not a lot of error checking here; the input better be right. */
1680
out = fopen(name, "wb");
1681
while (fgets(buff, sizeof(buff), in) != NULL) {
1685
if (memcmp(buff, "end", 3) == 0)
1688
bytes = UUDECODE(*p++);
1691
/* Write out 1-3 bytes from that. */
1693
n = UUDECODE(*p++) << 18;
1694
n |= UUDECODE(*p++) << 12;
1695
fputc(n >> 16, out);
1699
n |= UUDECODE(*p++) << 6;
1700
fputc((n >> 8) & 0xFF, out);
1704
n |= UUDECODE(*p++);
1705
fputc(n & 0xFF, out);
1721
* "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
1723
* DEFINE_TEST(test_function)
1727
/* Use "list.h" to declare all of the test functions. */
1729
#define DEFINE_TEST(name) void name(void);
1732
/* Use "list.h" to create a list of all tests (functions and names). */
1734
#define DEFINE_TEST(n) { n, #n, 0 },
1735
struct { void (*func)(void); const char *name; int failures; } tests[] = {
1740
* Summarize repeated failures in the just-completed test.
1743
test_summarize(const char *filename, int failed)
1747
switch (verbosity) {
1748
case VERBOSITY_SUMMARY_ONLY:
1749
printf(failed ? "E" : ".");
1752
case VERBOSITY_PASSFAIL:
1753
printf(failed ? "FAIL\n" : "ok\n");
1757
log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
1759
for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
1760
if (failed_lines[i].count > 1 && !failed_lines[i].skip)
1761
logprintf("%s:%d: Summary: Failed %d times\n",
1762
filename, i, failed_lines[i].count);
1764
/* Clear the failure history for the next file. */
1765
memset(failed_lines, 0, sizeof(failed_lines));
1769
* Actually run a single test, with appropriate setup and cleanup.
1772
test_run(int i, const char *tmpdir)
1774
char logfilename[64];
1775
int failures_before = failures;
1778
switch (verbosity) {
1779
case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
1781
case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
1782
printf("%3d: %-50s", i, tests[i].name);
1785
default: /* Title of test, details will follow */
1786
printf("%3d: %s\n", i, tests[i].name);
1789
/* Chdir to the top-level work directory. */
1790
if (!assertChdir(tmpdir)) {
1792
"ERROR: Can't chdir to top work dir %s\n", tmpdir);
1795
/* Create a log file for this test. */
1796
sprintf(logfilename, "%s.log", tests[i].name);
1797
logfile = fopen(logfilename, "w");
1798
fprintf(logfile, "%s\n\n", tests[i].name);
1799
/* Chdir() to a work dir for this specific test. */
1800
if (!assertMakeDir(tests[i].name, 0755)
1801
|| !assertChdir(tests[i].name)) {
1803
"ERROR: Can't chdir to work dir %s/%s\n",
1804
tmpdir, tests[i].name);
1807
/* Explicitly reset the locale before each test. */
1808
setlocale(LC_ALL, "C");
1809
/* Record the umask before we run the test. */
1810
umask(oldumask = umask(0));
1812
* Run the actual test.
1816
* Clean up and report afterwards.
1821
setlocale(LC_ALL, "C");
1822
/* Reset directory. */
1823
if (!assertChdir(tmpdir)) {
1824
fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
1828
/* Report per-test summaries. */
1829
tests[i].failures = failures - failures_before;
1830
test_summarize(test_filename, tests[i].failures);
1831
/* Close the per-test log file. */
1834
/* If there were no failures, we can remove the work dir and logfile. */
1835
if (tests[i].failures == 0) {
1836
if (!keep_temp_files && assertChdir(tmpdir)) {
1837
#if defined(_WIN32) && !defined(__CYGWIN__)
1838
/* Make sure not to leave empty directories.
1839
* Sometimes a processing of closing files used by tests
1840
* is not done, then rmdir will be failed and it will
1841
* leave a empty test directory. So we should wait a few
1842
* seconds and retry rmdir. */
1844
for (t = 0; t < 10; t++) {
1847
r = systemf("rmdir /S /Q %s", tests[i].name);
1851
systemf("del %s", logfilename);
1853
systemf("rm -rf %s", tests[i].name);
1854
systemf("rm %s", logfilename);
1858
/* Return appropriate status. */
1859
return (tests[i].failures);
1865
* MAIN and support routines.
1871
usage(const char *program)
1873
static const int limit = sizeof(tests) / sizeof(tests[0]);
1876
printf("Usage: %s [options] <test> <test> ...\n", program);
1877
printf("Default is to run all tests.\n");
1878
printf("Otherwise, specify the numbers of the tests you wish to run.\n");
1879
printf("Options:\n");
1880
printf(" -d Dump core after any failure, for debugging.\n");
1881
printf(" -k Keep all temp files.\n");
1882
printf(" Default: temp files for successful tests deleted.\n");
1884
printf(" -p <path> Path to executable to be tested.\n");
1885
printf(" Default: path taken from " ENVBASE " environment variable.\n");
1887
printf(" -q Quiet.\n");
1888
printf(" -r <dir> Path to dir containing reference files.\n");
1889
printf(" Default: Current directory.\n");
1890
printf(" -v Verbose.\n");
1891
printf("Available tests:\n");
1892
for (i = 0; i < limit; i++)
1893
printf(" %d: %s\n", i, tests[i].name);
1898
get_refdir(const char *d)
1900
char tried[512] = { '\0' };
1904
/* If a dir was specified, try that */
1907
snprintf(buff, sizeof(buff), "%s", d);
1908
p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1909
if (p != NULL) goto success;
1910
strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1911
strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1915
/* Get the current dir. */
1916
pwd = getcwd(NULL, 0);
1917
while (pwd[strlen(pwd) - 1] == '\n')
1918
pwd[strlen(pwd) - 1] = '\0';
1920
/* Look for a known file. */
1921
snprintf(buff, sizeof(buff), "%s", pwd);
1922
p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1923
if (p != NULL) goto success;
1924
strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1925
strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1927
snprintf(buff, sizeof(buff), "%s/test", pwd);
1928
p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1929
if (p != NULL) goto success;
1930
strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1931
strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1933
#if defined(LIBRARY)
1934
snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
1936
snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
1938
p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1939
if (p != NULL) goto success;
1940
strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1941
strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1943
if (memcmp(pwd, "/usr/obj", 8) == 0) {
1944
snprintf(buff, sizeof(buff), "%s", pwd + 8);
1945
p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1946
if (p != NULL) goto success;
1947
strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1948
strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1950
snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
1951
p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
1952
if (p != NULL) goto success;
1953
strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
1954
strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
1958
printf("Unable to locate known reference file %s\n", KNOWNREF);
1959
printf(" Checked following directories:\n%s\n", tried);
1960
#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
1968
return strdup(buff);
1972
main(int argc, char **argv)
1974
static const int limit = sizeof(tests) / sizeof(tests[0]);
1975
int i, tests_run = 0, tests_failed = 0, option;
1977
char *refdir_alloc = NULL;
1978
const char *progname;
1979
const char *tmp, *option_arg, *p;
1981
char tmpdir_timestamp[256];
1983
(void)argc; /* UNUSED */
1985
#if defined(HAVE__CrtSetReportMode)
1986
/* To stop to run the default invalid parameter handler. */
1987
_set_invalid_parameter_handler(invalid_parameter_handler);
1988
/* Disable annoying assertion message box. */
1989
_CrtSetReportMode(_CRT_ASSERT, 0);
1993
* Name of this program, used to build root of our temp directory
1996
progname = p = argv[0];
1997
while (*p != '\0') {
1998
/* Support \ or / dir separators for Windows compat. */
1999
if (*p == '/' || *p == '\\')
2005
/* Get the target program from environment, if available. */
2006
testprogfile = getenv(ENVBASE);
2009
if (getenv("TMPDIR") != NULL)
2010
tmp = getenv("TMPDIR");
2011
else if (getenv("TMP") != NULL)
2012
tmp = getenv("TMP");
2013
else if (getenv("TEMP") != NULL)
2014
tmp = getenv("TEMP");
2015
else if (getenv("TEMPDIR") != NULL)
2016
tmp = getenv("TEMPDIR");
2020
/* Allow -d to be controlled through the environment. */
2021
if (getenv(ENVBASE "_DEBUG") != NULL)
2022
dump_on_failure = 1;
2024
/* Get the directory holding test files from environment. */
2025
refdir = getenv(ENVBASE "_TEST_FILES");
2028
* Parse options, without using getopt(), which isn't available
2031
++argv; /* Skip program name */
2032
while (*argv != NULL) {
2037
while (*p != '\0') {
2040
/* If 'opt' takes an argument, parse that. */
2041
if (option == 'p' || option == 'r') {
2044
else if (*argv == NULL) {
2046
"Option -%c requires argument.\n",
2050
option_arg = *argv++;
2051
p = ""; /* End of this option word. */
2054
/* Now, handle the option. */
2057
dump_on_failure = 1;
2060
keep_temp_files = 1;
2064
testprogfile = option_arg;
2066
fprintf(stderr, "-p option not permitted\n");
2074
refdir = option_arg;
2080
fprintf(stderr, "Unrecognized option '%c'\n",
2088
* Sanity-check that our options make sense.
2091
if (testprogfile == NULL) {
2092
fprintf(stderr, "Program executable required\n");
2098
#if defined(_WIN32) && !defined(__CYGWIN__)
2099
/* Command.com sometimes rejects '/' separators. */
2100
testprg = strdup(testprogfile);
2101
for (i = 0; testprg[i] != '\0'; i++) {
2102
if (testprg[i] == '/')
2105
testprogfile = testprg;
2107
/* Quote the name that gets put into shell command lines. */
2108
testprg = malloc(strlen(testprogfile) + 3);
2109
strcpy(testprg, "\"");
2110
strcat(testprg, testprogfile);
2111
strcat(testprg, "\"");
2117
* Create a temp directory for the following tests.
2118
* Include the time the tests started as part of the name,
2119
* to make it easier to track the results of multiple tests.
2122
for (i = 0; ; i++) {
2123
strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2124
"%Y-%m-%dT%H.%M.%S",
2126
sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2127
tmpdir_timestamp, i);
2128
if (assertMakeDir(tmpdir,0755))
2132
"ERROR: Unable to create temp directory %s\n",
2139
* If the user didn't specify a directory for locating
2140
* reference files, try to find the reference files in
2141
* the "usual places."
2143
refdir = refdir_alloc = get_refdir(refdir);
2146
* Banner with basic information.
2149
printf("If tests fail or crash, details will be in:\n");
2150
printf(" %s\n", tmpdir);
2152
if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2153
printf("Reference files will be read from: %s\n", refdir);
2155
printf("Running tests on: %s\n", testprog);
2157
printf("Exercising: ");
2159
printf("%s\n", EXTRA_VERSION);
2166
* Run some or all of the individual tests.
2168
if (*argv == NULL) {
2169
/* Default: Run all tests. */
2170
for (i = 0; i < limit; i++) {
2171
if (test_run(i, tmpdir))
2176
while (*(argv) != NULL) {
2177
if (**argv >= '0' && **argv <= '9') {
2179
if (i < 0 || i >= limit) {
2180
printf("*** INVALID Test %s\n", *argv);
2183
/* usage() never returns */
2186
for (i = 0; i < limit; ++i) {
2187
if (strcmp(*argv, tests[i].name) == 0)
2191
printf("*** INVALID Test ``%s''\n",
2195
/* usage() never returns */
2198
if (test_run(i, tmpdir))
2206
* Report summary statistics.
2208
if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2210
printf("Totals:\n");
2211
printf(" Tests run: %8d\n", tests_run);
2212
printf(" Tests failed: %8d\n", tests_failed);
2213
printf(" Assertions checked:%8d\n", assertions);
2214
printf(" Assertions failed: %8d\n", failures);
2215
printf(" Skips reported: %8d\n", skips);
2219
printf("Failing tests:\n");
2220
for (i = 0; i < limit; ++i) {
2221
if (tests[i].failures)
2222
printf(" %d: %s (%d failures)\n", i,
2223
tests[i].name, tests[i].failures);
2226
printf("Details for failing tests: %s\n", tmpdir);
2229
if (verbosity == VERBOSITY_SUMMARY_ONLY)
2231
printf("%d tests passed, no failures\n", tests_run);
2236
/* If the final tmpdir is empty, we can remove it. */
2237
/* This should be the usual case when all tests succeed. */
2241
return (tests_failed ? 1 : 0);