main.c revision 238856
1228753Smm/*
2228753Smm * Copyright (c) 2003-2009 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm
26228753Smm#include "test.h"
27238856Smm#ifdef HAVE_SYS_IOCTL_H
28238856Smm#include <sys/ioctl.h>
29238856Smm#endif
30232153Smm#ifdef HAVE_SYS_TIME_H
31232153Smm#include <sys/time.h>
32232153Smm#endif
33228753Smm#include <errno.h>
34232153Smm#ifdef HAVE_ICONV_H
35232153Smm#include <iconv.h>
36232153Smm#endif
37238856Smm/*
38238856Smm * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
39238856Smm * As the include guards don't agree, the order of include is important.
40238856Smm */
41238856Smm#ifdef HAVE_LINUX_EXT2_FS_H
42238856Smm#include <linux/ext2_fs.h>      /* for Linux file flags */
43238856Smm#endif
44238856Smm#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
45238856Smm#include <ext2fs/ext2_fs.h>     /* Linux file flags, broken on Cygwin */
46238856Smm#endif
47232153Smm#include <limits.h>
48228753Smm#include <locale.h>
49232153Smm#ifdef HAVE_SIGNAL_H
50232153Smm#include <signal.h>
51232153Smm#endif
52228753Smm#include <stdarg.h>
53228753Smm#include <time.h>
54228753Smm
55228753Smm/*
56228753Smm * This same file is used pretty much verbatim for all test harnesses.
57228753Smm *
58228753Smm * The next few lines are the only differences.
59228753Smm * TODO: Move this into a separate configuration header, have all test
60228753Smm * suites share one copy of this file.
61228753Smm */
62228763Smm__FBSDID("$FreeBSD: head/contrib/libarchive/tar/test/main.c 238856 2012-07-28 06:38:44Z mm $");
63228753Smm#define KNOWNREF	"test_patterns_2.tar.uu"
64228753Smm#define ENVBASE "BSDTAR"  /* Prefix for environment variables. */
65228753Smm#define	PROGRAM "bsdtar"  /* Name of program being tested. */
66232153Smm#define PROGRAM_ALIAS "tar" /* Generic alias for program */
67232153Smm#undef	LIBRARY		  /* Not testing a library. */
68232153Smm#undef	EXTRA_DUMP	  /* How to dump extra data */
69232153Smm#undef	EXTRA_ERRNO	  /* How to dump errno */
70228753Smm/* How to generate extra version info. */
71228753Smm#define	EXTRA_VERSION    (systemf("%s --version", testprog) ? "" : "")
72228753Smm
73228753Smm/*
74228753Smm *
75228753Smm * Windows support routines
76228753Smm *
77228753Smm * Note: Configuration is a tricky issue.  Using HAVE_* feature macros
78228753Smm * in the test harness is dangerous because they cover up
79228753Smm * configuration errors.  The classic example of this is omitting a
80228753Smm * configure check.  If libarchive and libarchive_test both look for
81228753Smm * the same feature macro, such errors are hard to detect.  Platform
82228753Smm * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
83228753Smm * easily lead to very messy code.  It's best to limit yourself
84228753Smm * to only the most generic programming techniques in the test harness
85228753Smm * and thus avoid conditionals altogether.  Where that's not possible,
86228753Smm * try to minimize conditionals by grouping platform-specific tests in
87228753Smm * one place (e.g., test_acl_freebsd) or by adding new assert()
88228753Smm * functions (e.g., assertMakeHardlink()) to cover up platform
89228753Smm * differences.  Platform-specific coding in libarchive_test is often
90228753Smm * a symptom that some capability is missing from libarchive itself.
91228753Smm */
92228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
93228753Smm#include <io.h>
94228753Smm#include <windows.h>
95228753Smm#ifndef F_OK
96228753Smm#define F_OK (0)
97228753Smm#endif
98228753Smm#ifndef S_ISDIR
99228753Smm#define S_ISDIR(m)  ((m) & _S_IFDIR)
100228753Smm#endif
101228753Smm#ifndef S_ISREG
102228753Smm#define S_ISREG(m)  ((m) & _S_IFREG)
103228753Smm#endif
104228753Smm#if !defined(__BORLANDC__)
105228753Smm#define access _access
106228753Smm#undef chdir
107228753Smm#define chdir _chdir
108228753Smm#endif
109228753Smm#ifndef fileno
110228753Smm#define fileno _fileno
111228753Smm#endif
112228753Smm/*#define fstat _fstat64*/
113228753Smm#if !defined(__BORLANDC__)
114228753Smm#define getcwd _getcwd
115228753Smm#endif
116228753Smm#define lstat stat
117228753Smm/*#define lstat _stat64*/
118228753Smm/*#define stat _stat64*/
119228753Smm#define rmdir _rmdir
120228753Smm#if !defined(__BORLANDC__)
121228753Smm#define strdup _strdup
122228753Smm#define umask _umask
123228753Smm#endif
124228753Smm#define int64_t __int64
125228753Smm#endif
126228753Smm
127228753Smm#if defined(HAVE__CrtSetReportMode)
128228753Smm# include <crtdbg.h>
129228753Smm#endif
130228753Smm
131228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
132238856Smmstatic void	*GetFunctionKernel32(const char *);
133238856Smmstatic int	 my_CreateSymbolicLinkA(const char *, const char *, int);
134238856Smmstatic int	 my_CreateHardLinkA(const char *, const char *);
135238856Smmstatic int	 my_GetFileInformationByName(const char *,
136238856Smm		     BY_HANDLE_FILE_INFORMATION *);
137238856Smm
138238856Smmstatic void *
139238856SmmGetFunctionKernel32(const char *name)
140228753Smm{
141228753Smm	static HINSTANCE lib;
142228753Smm	static int set;
143228753Smm	if (!set) {
144228753Smm		set = 1;
145228753Smm		lib = LoadLibrary("kernel32.dll");
146228753Smm	}
147228753Smm	if (lib == NULL) {
148228753Smm		fprintf(stderr, "Can't load kernel32.dll?!\n");
149228753Smm		exit(1);
150228753Smm	}
151228753Smm	return (void *)GetProcAddress(lib, name);
152228753Smm}
153228753Smm
154228753Smmstatic int
155228753Smmmy_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
156228753Smm{
157228753Smm	static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
158228753Smm	static int set;
159228753Smm	if (!set) {
160228753Smm		set = 1;
161228753Smm		f = GetFunctionKernel32("CreateSymbolicLinkA");
162228753Smm	}
163228753Smm	return f == NULL ? 0 : (*f)(linkname, target, flags);
164228753Smm}
165228753Smm
166228753Smmstatic int
167228753Smmmy_CreateHardLinkA(const char *linkname, const char *target)
168228753Smm{
169228753Smm	static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
170228753Smm	static int set;
171228753Smm	if (!set) {
172228753Smm		set = 1;
173228753Smm		f = GetFunctionKernel32("CreateHardLinkA");
174228753Smm	}
175228753Smm	return f == NULL ? 0 : (*f)(linkname, target, NULL);
176228753Smm}
177228753Smm
178238856Smmstatic int
179228753Smmmy_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
180228753Smm{
181228753Smm	HANDLE h;
182228753Smm	int r;
183228753Smm
184228753Smm	memset(bhfi, 0, sizeof(*bhfi));
185228753Smm	h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
186232153Smm		OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
187228753Smm	if (h == INVALID_HANDLE_VALUE)
188228753Smm		return (0);
189228753Smm	r = GetFileInformationByHandle(h, bhfi);
190228753Smm	CloseHandle(h);
191228753Smm	return (r);
192228753Smm}
193228753Smm#endif
194228753Smm
195228753Smm#if defined(HAVE__CrtSetReportMode)
196228753Smmstatic void
197228753Smminvalid_parameter_handler(const wchar_t * expression,
198228753Smm    const wchar_t * function, const wchar_t * file,
199228753Smm    unsigned int line, uintptr_t pReserved)
200228753Smm{
201228753Smm	/* nop */
202228753Smm}
203228753Smm#endif
204228753Smm
205228753Smm/*
206228753Smm *
207228753Smm * OPTIONS FLAGS
208228753Smm *
209228753Smm */
210228753Smm
211228753Smm/* Enable core dump on failure. */
212228753Smmstatic int dump_on_failure = 0;
213228753Smm/* Default is to remove temp dirs and log data for successful tests. */
214228753Smmstatic int keep_temp_files = 0;
215232153Smm/* Default is to run the specified tests once and report errors. */
216232153Smmstatic int until_failure = 0;
217228753Smm/* Default is to just report pass/fail for each test. */
218228753Smmstatic int verbosity = 0;
219228753Smm#define	VERBOSITY_SUMMARY_ONLY -1 /* -q */
220228753Smm#define VERBOSITY_PASSFAIL 0   /* Default */
221228753Smm#define VERBOSITY_LIGHT_REPORT 1 /* -v */
222228753Smm#define VERBOSITY_FULL 2 /* -vv */
223228753Smm/* A few places generate even more output for verbosity > VERBOSITY_FULL,
224228753Smm * mostly for debugging the test harness itself. */
225228753Smm/* Cumulative count of assertion failures. */
226228753Smmstatic int failures = 0;
227228753Smm/* Cumulative count of reported skips. */
228228753Smmstatic int skips = 0;
229228753Smm/* Cumulative count of assertions checked. */
230228753Smmstatic int assertions = 0;
231228753Smm
232228753Smm/* Directory where uuencoded reference files can be found. */
233228753Smmstatic const char *refdir;
234228753Smm
235228753Smm/*
236228753Smm * Report log information selectively to console and/or disk log.
237228753Smm */
238228753Smmstatic int log_console = 0;
239228753Smmstatic FILE *logfile;
240228753Smmstatic void
241228753Smmvlogprintf(const char *fmt, va_list ap)
242228753Smm{
243228753Smm#ifdef va_copy
244228753Smm	va_list lfap;
245228753Smm	va_copy(lfap, ap);
246228753Smm#endif
247228753Smm	if (log_console)
248228753Smm		vfprintf(stdout, fmt, ap);
249228753Smm	if (logfile != NULL)
250228753Smm#ifdef va_copy
251228753Smm		vfprintf(logfile, fmt, lfap);
252228753Smm	va_end(lfap);
253228753Smm#else
254228753Smm		vfprintf(logfile, fmt, ap);
255228753Smm#endif
256228753Smm}
257228753Smm
258228753Smmstatic void
259228753Smmlogprintf(const char *fmt, ...)
260228753Smm{
261228753Smm	va_list ap;
262228753Smm	va_start(ap, fmt);
263228753Smm	vlogprintf(fmt, ap);
264228753Smm	va_end(ap);
265228753Smm}
266228753Smm
267228753Smm/* Set up a message to display only if next assertion fails. */
268228753Smmstatic char msgbuff[4096];
269228753Smmstatic const char *msg, *nextmsg;
270228753Smmvoid
271228753Smmfailure(const char *fmt, ...)
272228753Smm{
273228753Smm	va_list ap;
274232153Smm	if (fmt == NULL) {
275232153Smm		nextmsg = NULL;
276232153Smm	} else {
277232153Smm		va_start(ap, fmt);
278232153Smm		vsprintf(msgbuff, fmt, ap);
279232153Smm		va_end(ap);
280232153Smm		nextmsg = msgbuff;
281232153Smm	}
282228753Smm}
283228753Smm
284228753Smm/*
285228753Smm * Copy arguments into file-local variables.
286228753Smm * This was added to permit vararg assert() functions without needing
287228753Smm * variadic wrapper macros.  Turns out that the vararg capability is almost
288228753Smm * never used, so almost all of the vararg assertions can be simplified
289228753Smm * by removing the vararg capability and reworking the wrapper macro to
290228753Smm * pass __FILE__, __LINE__ directly into the function instead of using
291228753Smm * this hook.  I suspect this machinery is used so rarely that we
292228753Smm * would be better off just removing it entirely.  That would simplify
293232153Smm * the code here noticeably.
294228753Smm */
295232153Smmstatic const char *skipping_filename;
296232153Smmstatic int skipping_line;
297232153Smmvoid skipping_setup(const char *filename, int line)
298228753Smm{
299232153Smm	skipping_filename = filename;
300232153Smm	skipping_line = line;
301228753Smm}
302228753Smm
303228753Smm/* Called at the beginning of each assert() function. */
304228753Smmstatic void
305228753Smmassertion_count(const char *file, int line)
306228753Smm{
307228753Smm	(void)file; /* UNUSED */
308228753Smm	(void)line; /* UNUSED */
309228753Smm	++assertions;
310228753Smm	/* Proper handling of "failure()" message. */
311228753Smm	msg = nextmsg;
312228753Smm	nextmsg = NULL;
313228753Smm	/* Uncomment to print file:line after every assertion.
314228753Smm	 * Verbose, but occasionally useful in tracking down crashes. */
315228753Smm	/* printf("Checked %s:%d\n", file, line); */
316228753Smm}
317228753Smm
318228753Smm/*
319228753Smm * For each test source file, we remember how many times each
320228753Smm * assertion was reported.  Cleared before each new test,
321228753Smm * used by test_summarize().
322228753Smm */
323228753Smmstatic struct line {
324228753Smm	int count;
325228753Smm	int skip;
326228753Smm}  failed_lines[10000];
327232153Smmconst char *failed_filename;
328228753Smm
329228753Smm/* Count this failure, setup up log destination and handle initial report. */
330228753Smmstatic void
331228753Smmfailure_start(const char *filename, int line, const char *fmt, ...)
332228753Smm{
333228753Smm	va_list ap;
334228753Smm
335228753Smm	/* Record another failure for this line. */
336228753Smm	++failures;
337232153Smm	failed_filename = filename;
338228753Smm	failed_lines[line].count++;
339228753Smm
340228753Smm	/* Determine whether to log header to console. */
341228753Smm	switch (verbosity) {
342228753Smm	case VERBOSITY_LIGHT_REPORT:
343228753Smm		log_console = (failed_lines[line].count < 2);
344228753Smm		break;
345228753Smm	default:
346232153Smm		log_console = (verbosity >= VERBOSITY_FULL);
347228753Smm	}
348228753Smm
349228753Smm	/* Log file:line header for this failure */
350228753Smm	va_start(ap, fmt);
351228753Smm#if _MSC_VER
352228753Smm	logprintf("%s(%d): ", filename, line);
353228753Smm#else
354228753Smm	logprintf("%s:%d: ", filename, line);
355228753Smm#endif
356228753Smm	vlogprintf(fmt, ap);
357228753Smm	va_end(ap);
358228753Smm	logprintf("\n");
359228753Smm
360228753Smm	if (msg != NULL && msg[0] != '\0') {
361228753Smm		logprintf("   Description: %s\n", msg);
362228753Smm		msg = NULL;
363228753Smm	}
364228753Smm
365228753Smm	/* Determine whether to log details to console. */
366228753Smm	if (verbosity == VERBOSITY_LIGHT_REPORT)
367228753Smm		log_console = 0;
368228753Smm}
369228753Smm
370228753Smm/* Complete reporting of failed tests. */
371228753Smm/*
372228753Smm * The 'extra' hook here is used by libarchive to include libarchive
373228753Smm * error messages with assertion failures.  It could also be used
374228753Smm * to add strerror() output, for example.  Just define the EXTRA_DUMP()
375228753Smm * macro appropriately.
376228753Smm */
377228753Smmstatic void
378228753Smmfailure_finish(void *extra)
379228753Smm{
380228753Smm	(void)extra; /* UNUSED (maybe) */
381228753Smm#ifdef EXTRA_DUMP
382232153Smm	if (extra != NULL) {
383232153Smm		logprintf("    errno: %d\n", EXTRA_ERRNO(extra));
384228753Smm		logprintf("   detail: %s\n", EXTRA_DUMP(extra));
385232153Smm	}
386228753Smm#endif
387228753Smm
388228753Smm	if (dump_on_failure) {
389228753Smm		fprintf(stderr,
390228753Smm		    " *** forcing core dump so failure can be debugged ***\n");
391232153Smm		abort();
392228753Smm		exit(1);
393228753Smm	}
394228753Smm}
395228753Smm
396228753Smm/* Inform user that we're skipping some checks. */
397228753Smmvoid
398228753Smmtest_skipping(const char *fmt, ...)
399228753Smm{
400228753Smm	char buff[1024];
401228753Smm	va_list ap;
402228753Smm
403228753Smm	va_start(ap, fmt);
404228753Smm	vsprintf(buff, fmt, ap);
405228753Smm	va_end(ap);
406232153Smm	/* Use failure() message if set. */
407232153Smm	msg = nextmsg;
408232153Smm	nextmsg = NULL;
409228753Smm	/* failure_start() isn't quite right, but is awfully convenient. */
410232153Smm	failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
411228753Smm	--failures; /* Undo failures++ in failure_start() */
412228753Smm	/* Don't failure_finish() here. */
413228753Smm	/* Mark as skip, so doesn't count as failed test. */
414232153Smm	failed_lines[skipping_line].skip = 1;
415228753Smm	++skips;
416228753Smm}
417228753Smm
418228753Smm/*
419228753Smm *
420228753Smm * ASSERTIONS
421228753Smm *
422228753Smm */
423228753Smm
424228753Smm/* Generic assert() just displays the failed condition. */
425228753Smmint
426228753Smmassertion_assert(const char *file, int line, int value,
427228753Smm    const char *condition, void *extra)
428228753Smm{
429228753Smm	assertion_count(file, line);
430228753Smm	if (!value) {
431228753Smm		failure_start(file, line, "Assertion failed: %s", condition);
432228753Smm		failure_finish(extra);
433228753Smm	}
434228753Smm	return (value);
435228753Smm}
436228753Smm
437228753Smm/* chdir() and report any errors */
438228753Smmint
439228753Smmassertion_chdir(const char *file, int line, const char *pathname)
440228753Smm{
441228753Smm	assertion_count(file, line);
442228753Smm	if (chdir(pathname) == 0)
443228753Smm		return (1);
444228753Smm	failure_start(file, line, "chdir(\"%s\")", pathname);
445228753Smm	failure_finish(NULL);
446228753Smm	return (0);
447228753Smm
448228753Smm}
449228753Smm
450228753Smm/* Verify two integers are equal. */
451228753Smmint
452228753Smmassertion_equal_int(const char *file, int line,
453228753Smm    long long v1, const char *e1, long long v2, const char *e2, void *extra)
454228753Smm{
455228753Smm	assertion_count(file, line);
456228753Smm	if (v1 == v2)
457228753Smm		return (1);
458228753Smm	failure_start(file, line, "%s != %s", e1, e2);
459228753Smm	logprintf("      %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
460228753Smm	logprintf("      %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
461228753Smm	failure_finish(extra);
462228753Smm	return (0);
463228753Smm}
464228753Smm
465232153Smm/*
466232153Smm * Utility to convert a single UTF-8 sequence.
467232153Smm */
468232153Smmstatic int
469232153Smm_utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
470228753Smm{
471232153Smm	static const char utf8_count[256] = {
472232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
473232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
474232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
475232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
476232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
477232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
478232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
479232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
480232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
481232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
482232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
483232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
484232153Smm		 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
485232153Smm		 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
486232153Smm		 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
487232153Smm		 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
488232153Smm	};
489232153Smm	int ch;
490232153Smm	int cnt;
491232153Smm	uint32_t wc;
492232153Smm
493232153Smm	*pwc = 0;
494232153Smm
495232153Smm	/* Sanity check. */
496232153Smm	if (n == 0)
497232153Smm		return (0);
498232153Smm	/*
499232153Smm	 * Decode 1-4 bytes depending on the value of the first byte.
500232153Smm	 */
501232153Smm	ch = (unsigned char)*s;
502232153Smm	if (ch == 0)
503232153Smm		return (0); /* Standard:  return 0 for end-of-string. */
504232153Smm	cnt = utf8_count[ch];
505232153Smm
506232153Smm	/* Invalide sequence or there are not plenty bytes. */
507232153Smm	if (n < (size_t)cnt)
508232153Smm		return (-1);
509232153Smm
510232153Smm	/* Make a Unicode code point from a single UTF-8 sequence. */
511232153Smm	switch (cnt) {
512232153Smm	case 1:	/* 1 byte sequence. */
513232153Smm		*pwc = ch & 0x7f;
514232153Smm		return (cnt);
515232153Smm	case 2:	/* 2 bytes sequence. */
516232153Smm		if ((s[1] & 0xc0) != 0x80) return (-1);
517232153Smm		*pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
518232153Smm		return (cnt);
519232153Smm	case 3:	/* 3 bytes sequence. */
520232153Smm		if ((s[1] & 0xc0) != 0x80) return (-1);
521232153Smm		if ((s[2] & 0xc0) != 0x80) return (-1);
522232153Smm		wc = ((ch & 0x0f) << 12)
523232153Smm		    | ((s[1] & 0x3f) << 6)
524232153Smm		    | (s[2] & 0x3f);
525232153Smm		if (wc < 0x800)
526232153Smm			return (-1);/* Overlong sequence. */
527232153Smm		break;
528232153Smm	case 4:	/* 4 bytes sequence. */
529232153Smm		if (n < 4)
530232153Smm			return (-1);
531232153Smm		if ((s[1] & 0xc0) != 0x80) return (-1);
532232153Smm		if ((s[2] & 0xc0) != 0x80) return (-1);
533232153Smm		if ((s[3] & 0xc0) != 0x80) return (-1);
534232153Smm		wc = ((ch & 0x07) << 18)
535232153Smm		    | ((s[1] & 0x3f) << 12)
536232153Smm		    | ((s[2] & 0x3f) << 6)
537232153Smm		    | (s[3] & 0x3f);
538232153Smm		if (wc < 0x10000)
539232153Smm			return (-1);/* Overlong sequence. */
540232153Smm		break;
541232153Smm	default:
542232153Smm		return (-1);
543232153Smm	}
544232153Smm
545232153Smm	/* The code point larger than 0x10FFFF is not leagal
546232153Smm	 * Unicode values. */
547232153Smm	if (wc > 0x10FFFF)
548232153Smm		return (-1);
549232153Smm	/* Correctly gets a Unicode, returns used bytes. */
550232153Smm	*pwc = wc;
551232153Smm	return (cnt);
552232153Smm}
553232153Smm
554232153Smmstatic void strdump(const char *e, const char *p, int ewidth, int utf8)
555232153Smm{
556228753Smm	const char *q = p;
557228753Smm
558232153Smm	logprintf("      %*s = ", ewidth, e);
559228753Smm	if (p == NULL) {
560232153Smm		logprintf("NULL\n");
561228753Smm		return;
562228753Smm	}
563228753Smm	logprintf("\"");
564228753Smm	while (*p != '\0') {
565228753Smm		unsigned int c = 0xff & *p++;
566228753Smm		switch (c) {
567228753Smm		case '\a': printf("\a"); break;
568228753Smm		case '\b': printf("\b"); break;
569228753Smm		case '\n': printf("\n"); break;
570228753Smm		case '\r': printf("\r"); break;
571228753Smm		default:
572228753Smm			if (c >= 32 && c < 127)
573228753Smm				logprintf("%c", c);
574228753Smm			else
575228753Smm				logprintf("\\x%02X", c);
576228753Smm		}
577228753Smm	}
578228753Smm	logprintf("\"");
579232153Smm	logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
580232153Smm
581232153Smm	/*
582232153Smm	 * If the current string is UTF-8, dump its code points.
583232153Smm	 */
584232153Smm	if (utf8) {
585232153Smm		size_t len;
586232153Smm		uint32_t uc;
587232153Smm		int n;
588232153Smm		int cnt = 0;
589232153Smm
590232153Smm		p = q;
591232153Smm		len = strlen(p);
592232153Smm		logprintf(" [");
593232153Smm		while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
594232153Smm			if (p != q)
595232153Smm				logprintf(" ");
596232153Smm			logprintf("%04X", uc);
597232153Smm			p += n;
598232153Smm			len -= n;
599232153Smm			cnt++;
600232153Smm		}
601232153Smm		logprintf("]");
602232153Smm		logprintf(" (count %d", cnt);
603232153Smm		if (n < 0) {
604232153Smm			logprintf(",unknown %d bytes", len);
605232153Smm		}
606232153Smm		logprintf(")");
607232153Smm
608232153Smm	}
609232153Smm	logprintf("\n");
610228753Smm}
611228753Smm
612228753Smm/* Verify two strings are equal, dump them if not. */
613228753Smmint
614228753Smmassertion_equal_string(const char *file, int line,
615228753Smm    const char *v1, const char *e1,
616228753Smm    const char *v2, const char *e2,
617232153Smm    void *extra, int utf8)
618228753Smm{
619232153Smm	int l1, l2;
620232153Smm
621228753Smm	assertion_count(file, line);
622228753Smm	if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
623228753Smm		return (1);
624228753Smm	failure_start(file, line, "%s != %s", e1, e2);
625232153Smm	l1 = strlen(e1);
626232153Smm	l2 = strlen(e2);
627232153Smm	if (l1 < l2)
628232153Smm		l1 = l2;
629232153Smm	strdump(e1, v1, l1, utf8);
630232153Smm	strdump(e2, v2, l1, utf8);
631228753Smm	failure_finish(extra);
632228753Smm	return (0);
633228753Smm}
634228753Smm
635228753Smmstatic void
636228753Smmwcsdump(const char *e, const wchar_t *w)
637228753Smm{
638228753Smm	logprintf("      %s = ", e);
639228753Smm	if (w == NULL) {
640228753Smm		logprintf("(null)");
641228753Smm		return;
642228753Smm	}
643228753Smm	logprintf("\"");
644228753Smm	while (*w != L'\0') {
645228753Smm		unsigned int c = *w++;
646228753Smm		if (c >= 32 && c < 127)
647228753Smm			logprintf("%c", c);
648228753Smm		else if (c < 256)
649228753Smm			logprintf("\\x%02X", c);
650228753Smm		else if (c < 0x10000)
651228753Smm			logprintf("\\u%04X", c);
652228753Smm		else
653228753Smm			logprintf("\\U%08X", c);
654228753Smm	}
655228753Smm	logprintf("\"\n");
656228753Smm}
657228753Smm
658228753Smm#ifndef HAVE_WCSCMP
659228753Smmstatic int
660228753Smmwcscmp(const wchar_t *s1, const wchar_t *s2)
661228753Smm{
662228753Smm
663228753Smm	while (*s1 == *s2++) {
664228753Smm		if (*s1++ == L'\0')
665228753Smm			return 0;
666228753Smm	}
667228753Smm	if (*s1 > *--s2)
668228753Smm		return 1;
669228753Smm	else
670228753Smm		return -1;
671228753Smm}
672228753Smm#endif
673228753Smm
674228753Smm/* Verify that two wide strings are equal, dump them if not. */
675228753Smmint
676228753Smmassertion_equal_wstring(const char *file, int line,
677228753Smm    const wchar_t *v1, const char *e1,
678228753Smm    const wchar_t *v2, const char *e2,
679228753Smm    void *extra)
680228753Smm{
681228753Smm	assertion_count(file, line);
682232153Smm	if (v1 == v2)
683228753Smm		return (1);
684232153Smm	if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
685232153Smm		return (1);
686228753Smm	failure_start(file, line, "%s != %s", e1, e2);
687228753Smm	wcsdump(e1, v1);
688228753Smm	wcsdump(e2, v2);
689228753Smm	failure_finish(extra);
690228753Smm	return (0);
691228753Smm}
692228753Smm
693228753Smm/*
694228753Smm * Pretty standard hexdump routine.  As a bonus, if ref != NULL, then
695228753Smm * any bytes in p that differ from ref will be highlighted with '_'
696228753Smm * before and after the hex value.
697228753Smm */
698228753Smmstatic void
699228753Smmhexdump(const char *p, const char *ref, size_t l, size_t offset)
700228753Smm{
701228753Smm	size_t i, j;
702228753Smm	char sep;
703228753Smm
704228753Smm	if (p == NULL) {
705228753Smm		logprintf("(null)\n");
706228753Smm		return;
707228753Smm	}
708228753Smm	for(i=0; i < l; i+=16) {
709228753Smm		logprintf("%04x", (unsigned)(i + offset));
710228753Smm		sep = ' ';
711228753Smm		for (j = 0; j < 16 && i + j < l; j++) {
712228753Smm			if (ref != NULL && p[i + j] != ref[i + j])
713228753Smm				sep = '_';
714228753Smm			logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
715228753Smm			if (ref != NULL && p[i + j] == ref[i + j])
716228753Smm				sep = ' ';
717228753Smm		}
718228753Smm		for (; j < 16; j++) {
719228753Smm			logprintf("%c  ", sep);
720228753Smm			sep = ' ';
721228753Smm		}
722228753Smm		logprintf("%c", sep);
723228753Smm		for (j=0; j < 16 && i + j < l; j++) {
724228753Smm			int c = p[i + j];
725228753Smm			if (c >= ' ' && c <= 126)
726228753Smm				logprintf("%c", c);
727228753Smm			else
728228753Smm				logprintf(".");
729228753Smm		}
730228753Smm		logprintf("\n");
731228753Smm	}
732228753Smm}
733228753Smm
734228753Smm/* Verify that two blocks of memory are the same, display the first
735228753Smm * block of differences if they're not. */
736228753Smmint
737228753Smmassertion_equal_mem(const char *file, int line,
738228753Smm    const void *_v1, const char *e1,
739228753Smm    const void *_v2, const char *e2,
740228753Smm    size_t l, const char *ld, void *extra)
741228753Smm{
742228753Smm	const char *v1 = (const char *)_v1;
743228753Smm	const char *v2 = (const char *)_v2;
744228753Smm	size_t offset;
745228753Smm
746228753Smm	assertion_count(file, line);
747228753Smm	if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
748228753Smm		return (1);
749228753Smm
750228753Smm	failure_start(file, line, "%s != %s", e1, e2);
751228753Smm	logprintf("      size %s = %d\n", ld, (int)l);
752228753Smm	/* Dump 48 bytes (3 lines) so that the first difference is
753228753Smm	 * in the second line. */
754228753Smm	offset = 0;
755228753Smm	while (l > 64 && memcmp(v1, v2, 32) == 0) {
756228753Smm		/* Two lines agree, so step forward one line. */
757228753Smm		v1 += 16;
758228753Smm		v2 += 16;
759228753Smm		l -= 16;
760228753Smm		offset += 16;
761228753Smm	}
762228753Smm	logprintf("      Dump of %s\n", e1);
763232153Smm	hexdump(v1, v2, l < 128 ? l : 128, offset);
764228753Smm	logprintf("      Dump of %s\n", e2);
765232153Smm	hexdump(v2, v1, l < 128 ? l : 128, offset);
766228753Smm	logprintf("\n");
767228753Smm	failure_finish(extra);
768228753Smm	return (0);
769228753Smm}
770228753Smm
771228753Smm/* Verify that the named file exists and is empty. */
772228753Smmint
773232153Smmassertion_empty_file(const char *filename, int line, const char *f1)
774228753Smm{
775228753Smm	char buff[1024];
776228753Smm	struct stat st;
777228753Smm	ssize_t s;
778228753Smm	FILE *f;
779228753Smm
780232153Smm	assertion_count(filename, line);
781228753Smm
782228753Smm	if (stat(f1, &st) != 0) {
783232153Smm		failure_start(filename, line, "Stat failed: %s", f1);
784228753Smm		failure_finish(NULL);
785228753Smm		return (0);
786228753Smm	}
787228753Smm	if (st.st_size == 0)
788228753Smm		return (1);
789228753Smm
790232153Smm	failure_start(filename, line, "File should be empty: %s", f1);
791228753Smm	logprintf("    File size: %d\n", (int)st.st_size);
792228753Smm	logprintf("    Contents:\n");
793228753Smm	f = fopen(f1, "rb");
794228753Smm	if (f == NULL) {
795228753Smm		logprintf("    Unable to open %s\n", f1);
796228753Smm	} else {
797228753Smm		s = ((off_t)sizeof(buff) < st.st_size) ?
798228753Smm		    (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
799228753Smm		s = fread(buff, 1, s, f);
800228753Smm		hexdump(buff, NULL, s, 0);
801228753Smm		fclose(f);
802228753Smm	}
803228753Smm	failure_finish(NULL);
804228753Smm	return (0);
805228753Smm}
806228753Smm
807228753Smm/* Verify that the named file exists and is not empty. */
808228753Smmint
809232153Smmassertion_non_empty_file(const char *filename, int line, const char *f1)
810228753Smm{
811228753Smm	struct stat st;
812228753Smm
813232153Smm	assertion_count(filename, line);
814228753Smm
815228753Smm	if (stat(f1, &st) != 0) {
816232153Smm		failure_start(filename, line, "Stat failed: %s", f1);
817228753Smm		failure_finish(NULL);
818228753Smm		return (0);
819228753Smm	}
820228753Smm	if (st.st_size == 0) {
821232153Smm		failure_start(filename, line, "File empty: %s", f1);
822228753Smm		failure_finish(NULL);
823228753Smm		return (0);
824228753Smm	}
825228753Smm	return (1);
826228753Smm}
827228753Smm
828228753Smm/* Verify that two files have the same contents. */
829228753Smm/* TODO: hexdump the first bytes that actually differ. */
830228753Smmint
831232153Smmassertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
832228753Smm{
833228753Smm	char buff1[1024];
834228753Smm	char buff2[1024];
835228753Smm	FILE *f1, *f2;
836228753Smm	int n1, n2;
837228753Smm
838232153Smm	assertion_count(filename, line);
839228753Smm
840228753Smm	f1 = fopen(fn1, "rb");
841228753Smm	f2 = fopen(fn2, "rb");
842228753Smm	for (;;) {
843228753Smm		n1 = fread(buff1, 1, sizeof(buff1), f1);
844228753Smm		n2 = fread(buff2, 1, sizeof(buff2), f2);
845228753Smm		if (n1 != n2)
846228753Smm			break;
847228753Smm		if (n1 == 0 && n2 == 0) {
848228753Smm			fclose(f1);
849228753Smm			fclose(f2);
850228753Smm			return (1);
851228753Smm		}
852228753Smm		if (memcmp(buff1, buff2, n1) != 0)
853228753Smm			break;
854228753Smm	}
855228753Smm	fclose(f1);
856228753Smm	fclose(f2);
857232153Smm	failure_start(filename, line, "Files not identical");
858228753Smm	logprintf("  file1=\"%s\"\n", fn1);
859228753Smm	logprintf("  file2=\"%s\"\n", fn2);
860232153Smm	failure_finish(NULL);
861228753Smm	return (0);
862228753Smm}
863228753Smm
864228753Smm/* Verify that the named file does exist. */
865228753Smmint
866232153Smmassertion_file_exists(const char *filename, int line, const char *f)
867228753Smm{
868232153Smm	assertion_count(filename, line);
869228753Smm
870228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
871228753Smm	if (!_access(f, 0))
872228753Smm		return (1);
873228753Smm#else
874228753Smm	if (!access(f, F_OK))
875228753Smm		return (1);
876228753Smm#endif
877232153Smm	failure_start(filename, line, "File should exist: %s", f);
878232153Smm	failure_finish(NULL);
879228753Smm	return (0);
880228753Smm}
881228753Smm
882228753Smm/* Verify that the named file doesn't exist. */
883228753Smmint
884232153Smmassertion_file_not_exists(const char *filename, int line, const char *f)
885228753Smm{
886232153Smm	assertion_count(filename, line);
887228753Smm
888228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
889228753Smm	if (_access(f, 0))
890228753Smm		return (1);
891228753Smm#else
892228753Smm	if (access(f, F_OK))
893228753Smm		return (1);
894228753Smm#endif
895232153Smm	failure_start(filename, line, "File should not exist: %s", f);
896232153Smm	failure_finish(NULL);
897228753Smm	return (0);
898228753Smm}
899228753Smm
900228753Smm/* Compare the contents of a file to a block of memory. */
901228753Smmint
902232153Smmassertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
903228753Smm{
904228753Smm	char *contents;
905228753Smm	FILE *f;
906228753Smm	int n;
907228753Smm
908232153Smm	assertion_count(filename, line);
909228753Smm
910228753Smm	f = fopen(fn, "rb");
911228753Smm	if (f == NULL) {
912232153Smm		failure_start(filename, line,
913228753Smm		    "File should exist: %s", fn);
914232153Smm		failure_finish(NULL);
915228753Smm		return (0);
916228753Smm	}
917228753Smm	contents = malloc(s * 2);
918228753Smm	n = fread(contents, 1, s * 2, f);
919228753Smm	fclose(f);
920228753Smm	if (n == s && memcmp(buff, contents, s) == 0) {
921228753Smm		free(contents);
922228753Smm		return (1);
923228753Smm	}
924232153Smm	failure_start(filename, line, "File contents don't match");
925228753Smm	logprintf("  file=\"%s\"\n", fn);
926228753Smm	if (n > 0)
927228753Smm		hexdump(contents, buff, n > 512 ? 512 : n, 0);
928228753Smm	else {
929228753Smm		logprintf("  File empty, contents should be:\n");
930228753Smm		hexdump(buff, NULL, s > 512 ? 512 : s, 0);
931228753Smm	}
932232153Smm	failure_finish(NULL);
933228753Smm	free(contents);
934228753Smm	return (0);
935228753Smm}
936228753Smm
937228753Smm/* Check the contents of a text file, being tolerant of line endings. */
938228753Smmint
939232153Smmassertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
940228753Smm{
941228753Smm	char *contents;
942228753Smm	const char *btxt, *ftxt;
943228753Smm	FILE *f;
944228753Smm	int n, s;
945228753Smm
946232153Smm	assertion_count(filename, line);
947228753Smm	f = fopen(fn, "r");
948232153Smm	if (f == NULL) {
949232153Smm		failure_start(filename, line,
950232153Smm		    "File doesn't exist: %s", fn);
951232153Smm		failure_finish(NULL);
952232153Smm		return (0);
953232153Smm	}
954228753Smm	s = strlen(buff);
955228753Smm	contents = malloc(s * 2 + 128);
956228753Smm	n = fread(contents, 1, s * 2 + 128 - 1, f);
957228753Smm	if (n >= 0)
958228753Smm		contents[n] = '\0';
959228753Smm	fclose(f);
960228753Smm	/* Compare texts. */
961228753Smm	btxt = buff;
962228753Smm	ftxt = (const char *)contents;
963228753Smm	while (*btxt != '\0' && *ftxt != '\0') {
964228753Smm		if (*btxt == *ftxt) {
965228753Smm			++btxt;
966228753Smm			++ftxt;
967228753Smm			continue;
968228753Smm		}
969228753Smm		if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
970228753Smm			/* Pass over different new line characters. */
971228753Smm			++btxt;
972228753Smm			ftxt += 2;
973228753Smm			continue;
974228753Smm		}
975228753Smm		break;
976228753Smm	}
977228753Smm	if (*btxt == '\0' && *ftxt == '\0') {
978228753Smm		free(contents);
979228753Smm		return (1);
980228753Smm	}
981232153Smm	failure_start(filename, line, "Contents don't match");
982228753Smm	logprintf("  file=\"%s\"\n", fn);
983232153Smm	if (n > 0) {
984228753Smm		hexdump(contents, buff, n, 0);
985232153Smm		logprintf("  expected\n", fn);
986232153Smm		hexdump(buff, contents, s, 0);
987232153Smm	} else {
988228753Smm		logprintf("  File empty, contents should be:\n");
989228753Smm		hexdump(buff, NULL, s, 0);
990228753Smm	}
991232153Smm	failure_finish(NULL);
992228753Smm	free(contents);
993228753Smm	return (0);
994228753Smm}
995228753Smm
996228753Smm/* Verify that a text file contains the specified lines, regardless of order */
997228753Smm/* This could be more efficient if we sorted both sets of lines, etc, but
998228753Smm * since this is used only for testing and only ever deals with a dozen or so
999228753Smm * lines at a time, this relatively crude approach is just fine. */
1000228753Smmint
1001228753Smmassertion_file_contains_lines_any_order(const char *file, int line,
1002228753Smm    const char *pathname, const char *lines[])
1003228753Smm{
1004228753Smm	char *buff;
1005228753Smm	size_t buff_size;
1006228753Smm	size_t expected_count, actual_count, i, j;
1007228753Smm	char **expected;
1008228753Smm	char *p, **actual;
1009228753Smm	char c;
1010228753Smm	int expected_failure = 0, actual_failure = 0;
1011228753Smm
1012228753Smm	assertion_count(file, line);
1013228753Smm
1014228753Smm	buff = slurpfile(&buff_size, "%s", pathname);
1015228753Smm	if (buff == NULL) {
1016228753Smm		failure_start(pathname, line, "Can't read file: %s", pathname);
1017228753Smm		failure_finish(NULL);
1018228753Smm		return (0);
1019228753Smm	}
1020228753Smm
1021232153Smm	/* Make a copy of the provided lines and count up the expected file size. */
1022228753Smm	expected_count = 0;
1023228753Smm	for (i = 0; lines[i] != NULL; ++i) {
1024228753Smm	}
1025228753Smm	expected_count = i;
1026228753Smm	expected = malloc(sizeof(char *) * expected_count);
1027228753Smm	for (i = 0; lines[i] != NULL; ++i) {
1028228753Smm		expected[i] = strdup(lines[i]);
1029228753Smm	}
1030228753Smm
1031232153Smm	/* Break the file into lines */
1032228753Smm	actual_count = 0;
1033228753Smm	for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1034228753Smm		if (*p == '\x0d' || *p == '\x0a')
1035228753Smm			*p = '\0';
1036228753Smm		if (c == '\0' && *p != '\0')
1037228753Smm			++actual_count;
1038228753Smm		c = *p;
1039228753Smm	}
1040228753Smm	actual = malloc(sizeof(char *) * actual_count);
1041228753Smm	for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) {
1042228753Smm		if (*p != '\0') {
1043228753Smm			actual[j] = p;
1044228753Smm			++j;
1045228753Smm		}
1046228753Smm	}
1047228753Smm
1048232153Smm	/* Erase matching lines from both lists */
1049228753Smm	for (i = 0; i < expected_count; ++i) {
1050228753Smm		if (expected[i] == NULL)
1051228753Smm			continue;
1052228753Smm		for (j = 0; j < actual_count; ++j) {
1053228753Smm			if (actual[j] == NULL)
1054228753Smm				continue;
1055228753Smm			if (strcmp(expected[i], actual[j]) == 0) {
1056228753Smm				free(expected[i]);
1057228753Smm				expected[i] = NULL;
1058228753Smm				actual[j] = NULL;
1059228753Smm				break;
1060228753Smm			}
1061228753Smm		}
1062228753Smm	}
1063228753Smm
1064232153Smm	/* If there's anything left, it's a failure */
1065228753Smm	for (i = 0; i < expected_count; ++i) {
1066228753Smm		if (expected[i] != NULL)
1067228753Smm			++expected_failure;
1068228753Smm	}
1069228753Smm	for (j = 0; j < actual_count; ++j) {
1070228753Smm		if (actual[j] != NULL)
1071228753Smm			++actual_failure;
1072228753Smm	}
1073228753Smm	if (expected_failure == 0 && actual_failure == 0) {
1074228753Smm		free(buff);
1075228753Smm		free(expected);
1076228753Smm		free(actual);
1077228753Smm		return (1);
1078228753Smm	}
1079228753Smm	failure_start(file, line, "File doesn't match: %s", pathname);
1080228753Smm	for (i = 0; i < expected_count; ++i) {
1081228753Smm		if (expected[i] != NULL) {
1082228753Smm			logprintf("  Expected but not present: %s\n", expected[i]);
1083228753Smm			free(expected[i]);
1084228753Smm		}
1085228753Smm	}
1086228753Smm	for (j = 0; j < actual_count; ++j) {
1087228753Smm		if (actual[j] != NULL)
1088228753Smm			logprintf("  Present but not expected: %s\n", actual[j]);
1089228753Smm	}
1090228753Smm	failure_finish(NULL);
1091228753Smm	free(buff);
1092228753Smm	free(expected);
1093228753Smm	free(actual);
1094228753Smm	return (0);
1095228753Smm}
1096228753Smm
1097228753Smm/* Test that two paths point to the same file. */
1098228753Smm/* As a side-effect, asserts that both files exist. */
1099228753Smmstatic int
1100228753Smmis_hardlink(const char *file, int line,
1101228753Smm    const char *path1, const char *path2)
1102228753Smm{
1103228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1104228753Smm	BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1105228753Smm	int r;
1106228753Smm
1107228753Smm	assertion_count(file, line);
1108228753Smm	r = my_GetFileInformationByName(path1, &bhfi1);
1109228753Smm	if (r == 0) {
1110228753Smm		failure_start(file, line, "File %s can't be inspected?", path1);
1111228753Smm		failure_finish(NULL);
1112228753Smm		return (0);
1113228753Smm	}
1114228753Smm	r = my_GetFileInformationByName(path2, &bhfi2);
1115228753Smm	if (r == 0) {
1116228753Smm		failure_start(file, line, "File %s can't be inspected?", path2);
1117228753Smm		failure_finish(NULL);
1118228753Smm		return (0);
1119228753Smm	}
1120228753Smm	return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1121228753Smm		&& bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1122228753Smm		&& bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1123228753Smm#else
1124228753Smm	struct stat st1, st2;
1125228753Smm	int r;
1126228753Smm
1127228753Smm	assertion_count(file, line);
1128228753Smm	r = lstat(path1, &st1);
1129228753Smm	if (r != 0) {
1130228753Smm		failure_start(file, line, "File should exist: %s", path1);
1131228753Smm		failure_finish(NULL);
1132228753Smm		return (0);
1133228753Smm	}
1134228753Smm	r = lstat(path2, &st2);
1135228753Smm	if (r != 0) {
1136228753Smm		failure_start(file, line, "File should exist: %s", path2);
1137228753Smm		failure_finish(NULL);
1138228753Smm		return (0);
1139228753Smm	}
1140228753Smm	return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1141228753Smm#endif
1142228753Smm}
1143228753Smm
1144228753Smmint
1145228753Smmassertion_is_hardlink(const char *file, int line,
1146228753Smm    const char *path1, const char *path2)
1147228753Smm{
1148228753Smm	if (is_hardlink(file, line, path1, path2))
1149228753Smm		return (1);
1150228753Smm	failure_start(file, line,
1151228753Smm	    "Files %s and %s are not hardlinked", path1, path2);
1152228753Smm	failure_finish(NULL);
1153228753Smm	return (0);
1154228753Smm}
1155228753Smm
1156228753Smmint
1157228753Smmassertion_is_not_hardlink(const char *file, int line,
1158228753Smm    const char *path1, const char *path2)
1159228753Smm{
1160228753Smm	if (!is_hardlink(file, line, path1, path2))
1161228753Smm		return (1);
1162228753Smm	failure_start(file, line,
1163228753Smm	    "Files %s and %s should not be hardlinked", path1, path2);
1164228753Smm	failure_finish(NULL);
1165228753Smm	return (0);
1166228753Smm}
1167228753Smm
1168228753Smm/* Verify a/b/mtime of 'pathname'. */
1169228753Smm/* If 'recent', verify that it's within last 10 seconds. */
1170228753Smmstatic int
1171228753Smmassertion_file_time(const char *file, int line,
1172228753Smm    const char *pathname, long t, long nsec, char type, int recent)
1173228753Smm{
1174228753Smm	long long filet, filet_nsec;
1175228753Smm	int r;
1176228753Smm
1177228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1178228753Smm#define EPOC_TIME	(116444736000000000ULL)
1179228753Smm	FILETIME ftime, fbirthtime, fatime, fmtime;
1180228753Smm	ULARGE_INTEGER wintm;
1181228753Smm	HANDLE h;
1182228753Smm	ftime.dwLowDateTime = 0;
1183228753Smm	ftime.dwHighDateTime = 0;
1184228753Smm
1185228753Smm	assertion_count(file, line);
1186232153Smm	/* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1187232153Smm	 * a directory file. If not, CreateFile() will fail when
1188232153Smm	 * the pathname is a directory. */
1189228753Smm	h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1190232153Smm	    OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1191228753Smm	if (h == INVALID_HANDLE_VALUE) {
1192228753Smm		failure_start(file, line, "Can't access %s\n", pathname);
1193228753Smm		failure_finish(NULL);
1194228753Smm		return (0);
1195228753Smm	}
1196228753Smm	r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1197228753Smm	switch (type) {
1198228753Smm	case 'a': ftime = fatime; break;
1199228753Smm	case 'b': ftime = fbirthtime; break;
1200228753Smm	case 'm': ftime = fmtime; break;
1201228753Smm	}
1202228753Smm	CloseHandle(h);
1203228753Smm	if (r == 0) {
1204228753Smm		failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1205228753Smm		failure_finish(NULL);
1206228753Smm		return (0);
1207228753Smm	}
1208228753Smm	wintm.LowPart = ftime.dwLowDateTime;
1209228753Smm	wintm.HighPart = ftime.dwHighDateTime;
1210228753Smm	filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1211228753Smm	filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1212228753Smm	nsec = (nsec / 100) * 100; /* Round the request */
1213228753Smm#else
1214228753Smm	struct stat st;
1215228753Smm
1216228753Smm	assertion_count(file, line);
1217228753Smm	r = lstat(pathname, &st);
1218228753Smm	if (r != 0) {
1219228753Smm		failure_start(file, line, "Can't stat %s\n", pathname);
1220228753Smm		failure_finish(NULL);
1221228753Smm		return (0);
1222228753Smm	}
1223228753Smm	switch (type) {
1224228753Smm	case 'a': filet = st.st_atime; break;
1225228753Smm	case 'm': filet = st.st_mtime; break;
1226228753Smm	case 'b': filet = 0; break;
1227228753Smm	default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1228228753Smm		exit(1);
1229228753Smm	}
1230228753Smm#if defined(__FreeBSD__)
1231228753Smm	switch (type) {
1232228753Smm	case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1233228753Smm	case 'b': filet = st.st_birthtime;
1234228753Smm		filet_nsec = st.st_birthtimespec.tv_nsec; break;
1235228753Smm	case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1236228753Smm	default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1237228753Smm		exit(1);
1238228753Smm	}
1239228753Smm	/* FreeBSD generally only stores to microsecond res, so round. */
1240228753Smm	filet_nsec = (filet_nsec / 1000) * 1000;
1241228753Smm	nsec = (nsec / 1000) * 1000;
1242228753Smm#else
1243228753Smm	filet_nsec = nsec = 0;	/* Generic POSIX only has whole seconds. */
1244228753Smm	if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1245228753Smm#if defined(__HAIKU__)
1246228753Smm	if (type == 'a') return (1); /* Haiku doesn't have atime. */
1247228753Smm#endif
1248228753Smm#endif
1249228753Smm#endif
1250228753Smm	if (recent) {
1251228753Smm		/* Check that requested time is up-to-date. */
1252228753Smm		time_t now = time(NULL);
1253228753Smm		if (filet < now - 10 || filet > now + 1) {
1254228753Smm			failure_start(file, line,
1255232153Smm			    "File %s has %ctime %lld, %lld seconds ago\n",
1256228753Smm			    pathname, type, filet, now - filet);
1257228753Smm			failure_finish(NULL);
1258228753Smm			return (0);
1259228753Smm		}
1260228753Smm	} else if (filet != t || filet_nsec != nsec) {
1261228753Smm		failure_start(file, line,
1262232153Smm		    "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1263228753Smm		    pathname, type, filet, filet_nsec, t, nsec);
1264228753Smm		failure_finish(NULL);
1265228753Smm		return (0);
1266228753Smm	}
1267228753Smm	return (1);
1268228753Smm}
1269228753Smm
1270228753Smm/* Verify atime of 'pathname'. */
1271228753Smmint
1272228753Smmassertion_file_atime(const char *file, int line,
1273228753Smm    const char *pathname, long t, long nsec)
1274228753Smm{
1275228753Smm	return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1276228753Smm}
1277228753Smm
1278228753Smm/* Verify atime of 'pathname' is up-to-date. */
1279228753Smmint
1280228753Smmassertion_file_atime_recent(const char *file, int line, const char *pathname)
1281228753Smm{
1282228753Smm	return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1283228753Smm}
1284228753Smm
1285228753Smm/* Verify birthtime of 'pathname'. */
1286228753Smmint
1287228753Smmassertion_file_birthtime(const char *file, int line,
1288228753Smm    const char *pathname, long t, long nsec)
1289228753Smm{
1290228753Smm	return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1291228753Smm}
1292228753Smm
1293228753Smm/* Verify birthtime of 'pathname' is up-to-date. */
1294228753Smmint
1295228753Smmassertion_file_birthtime_recent(const char *file, int line,
1296228753Smm    const char *pathname)
1297228753Smm{
1298228753Smm	return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1299228753Smm}
1300228753Smm
1301228753Smm/* Verify mtime of 'pathname'. */
1302228753Smmint
1303228753Smmassertion_file_mtime(const char *file, int line,
1304228753Smm    const char *pathname, long t, long nsec)
1305228753Smm{
1306228753Smm	return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1307228753Smm}
1308228753Smm
1309228753Smm/* Verify mtime of 'pathname' is up-to-date. */
1310228753Smmint
1311228753Smmassertion_file_mtime_recent(const char *file, int line, const char *pathname)
1312228753Smm{
1313228753Smm	return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1314228753Smm}
1315228753Smm
1316228753Smm/* Verify number of links to 'pathname'. */
1317228753Smmint
1318228753Smmassertion_file_nlinks(const char *file, int line,
1319228753Smm    const char *pathname, int nlinks)
1320228753Smm{
1321228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1322228753Smm	BY_HANDLE_FILE_INFORMATION bhfi;
1323228753Smm	int r;
1324228753Smm
1325228753Smm	assertion_count(file, line);
1326228753Smm	r = my_GetFileInformationByName(pathname, &bhfi);
1327228753Smm	if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1328228753Smm		return (1);
1329228753Smm	failure_start(file, line, "File %s has %d links, expected %d",
1330228753Smm	    pathname, bhfi.nNumberOfLinks, nlinks);
1331228753Smm	failure_finish(NULL);
1332228753Smm	return (0);
1333228753Smm#else
1334228753Smm	struct stat st;
1335228753Smm	int r;
1336228753Smm
1337228753Smm	assertion_count(file, line);
1338228753Smm	r = lstat(pathname, &st);
1339232153Smm	if (r == 0 && (int)st.st_nlink == nlinks)
1340228753Smm			return (1);
1341228753Smm	failure_start(file, line, "File %s has %d links, expected %d",
1342228753Smm	    pathname, st.st_nlink, nlinks);
1343228753Smm	failure_finish(NULL);
1344228753Smm	return (0);
1345228753Smm#endif
1346228753Smm}
1347228753Smm
1348228753Smm/* Verify size of 'pathname'. */
1349228753Smmint
1350228753Smmassertion_file_size(const char *file, int line, const char *pathname, long size)
1351228753Smm{
1352228753Smm	int64_t filesize;
1353228753Smm	int r;
1354228753Smm
1355228753Smm	assertion_count(file, line);
1356228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1357228753Smm	{
1358228753Smm		BY_HANDLE_FILE_INFORMATION bhfi;
1359228753Smm		r = !my_GetFileInformationByName(pathname, &bhfi);
1360228753Smm		filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1361228753Smm	}
1362228753Smm#else
1363228753Smm	{
1364228753Smm		struct stat st;
1365228753Smm		r = lstat(pathname, &st);
1366228753Smm		filesize = st.st_size;
1367228753Smm	}
1368228753Smm#endif
1369228753Smm	if (r == 0 && filesize == size)
1370228753Smm			return (1);
1371228753Smm	failure_start(file, line, "File %s has size %ld, expected %ld",
1372228753Smm	    pathname, (long)filesize, (long)size);
1373228753Smm	failure_finish(NULL);
1374228753Smm	return (0);
1375228753Smm}
1376228753Smm
1377228753Smm/* Assert that 'pathname' is a dir.  If mode >= 0, verify that too. */
1378228753Smmint
1379228753Smmassertion_is_dir(const char *file, int line, const char *pathname, int mode)
1380228753Smm{
1381228753Smm	struct stat st;
1382228753Smm	int r;
1383228753Smm
1384228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1385228753Smm	(void)mode; /* UNUSED */
1386228753Smm#endif
1387228753Smm	assertion_count(file, line);
1388228753Smm	r = lstat(pathname, &st);
1389228753Smm	if (r != 0) {
1390228753Smm		failure_start(file, line, "Dir should exist: %s", pathname);
1391228753Smm		failure_finish(NULL);
1392228753Smm		return (0);
1393228753Smm	}
1394228753Smm	if (!S_ISDIR(st.st_mode)) {
1395228753Smm		failure_start(file, line, "%s is not a dir", pathname);
1396228753Smm		failure_finish(NULL);
1397228753Smm		return (0);
1398228753Smm	}
1399228753Smm#if !defined(_WIN32) || defined(__CYGWIN__)
1400228753Smm	/* Windows doesn't handle permissions the same way as POSIX,
1401228753Smm	 * so just ignore the mode tests. */
1402228753Smm	/* TODO: Can we do better here? */
1403232153Smm	if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1404228753Smm		failure_start(file, line, "Dir %s has wrong mode", pathname);
1405228753Smm		logprintf("  Expected: 0%3o\n", mode);
1406228753Smm		logprintf("  Found: 0%3o\n", st.st_mode & 07777);
1407228753Smm		failure_finish(NULL);
1408228753Smm		return (0);
1409228753Smm	}
1410228753Smm#endif
1411228753Smm	return (1);
1412228753Smm}
1413228753Smm
1414228753Smm/* Verify that 'pathname' is a regular file.  If 'mode' is >= 0,
1415228753Smm * verify that too. */
1416228753Smmint
1417228753Smmassertion_is_reg(const char *file, int line, const char *pathname, int mode)
1418228753Smm{
1419228753Smm	struct stat st;
1420228753Smm	int r;
1421228753Smm
1422228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1423228753Smm	(void)mode; /* UNUSED */
1424228753Smm#endif
1425228753Smm	assertion_count(file, line);
1426228753Smm	r = lstat(pathname, &st);
1427228753Smm	if (r != 0 || !S_ISREG(st.st_mode)) {
1428228753Smm		failure_start(file, line, "File should exist: %s", pathname);
1429228753Smm		failure_finish(NULL);
1430228753Smm		return (0);
1431228753Smm	}
1432228753Smm#if !defined(_WIN32) || defined(__CYGWIN__)
1433228753Smm	/* Windows doesn't handle permissions the same way as POSIX,
1434228753Smm	 * so just ignore the mode tests. */
1435228753Smm	/* TODO: Can we do better here? */
1436232153Smm	if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1437228753Smm		failure_start(file, line, "File %s has wrong mode", pathname);
1438228753Smm		logprintf("  Expected: 0%3o\n", mode);
1439228753Smm		logprintf("  Found: 0%3o\n", st.st_mode & 07777);
1440228753Smm		failure_finish(NULL);
1441228753Smm		return (0);
1442228753Smm	}
1443228753Smm#endif
1444228753Smm	return (1);
1445228753Smm}
1446228753Smm
1447228753Smm/* Check whether 'pathname' is a symbolic link.  If 'contents' is
1448228753Smm * non-NULL, verify that the symlink has those contents. */
1449228753Smmstatic int
1450228753Smmis_symlink(const char *file, int line,
1451228753Smm    const char *pathname, const char *contents)
1452228753Smm{
1453228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1454228753Smm	(void)pathname; /* UNUSED */
1455228753Smm	(void)contents; /* UNUSED */
1456228753Smm	assertion_count(file, line);
1457228753Smm	/* Windows sort-of has real symlinks, but they're only usable
1458228753Smm	 * by privileged users and are crippled even then, so there's
1459228753Smm	 * really not much point in bothering with this. */
1460228753Smm	return (0);
1461228753Smm#else
1462228753Smm	char buff[300];
1463228753Smm	struct stat st;
1464228753Smm	ssize_t linklen;
1465228753Smm	int r;
1466228753Smm
1467228753Smm	assertion_count(file, line);
1468228753Smm	r = lstat(pathname, &st);
1469228753Smm	if (r != 0) {
1470228753Smm		failure_start(file, line,
1471228753Smm		    "Symlink should exist: %s", pathname);
1472228753Smm		failure_finish(NULL);
1473228753Smm		return (0);
1474228753Smm	}
1475228753Smm	if (!S_ISLNK(st.st_mode))
1476228753Smm		return (0);
1477228753Smm	if (contents == NULL)
1478228753Smm		return (1);
1479228753Smm	linklen = readlink(pathname, buff, sizeof(buff));
1480228753Smm	if (linklen < 0) {
1481228753Smm		failure_start(file, line, "Can't read symlink %s", pathname);
1482228753Smm		failure_finish(NULL);
1483228753Smm		return (0);
1484228753Smm	}
1485228753Smm	buff[linklen] = '\0';
1486228753Smm	if (strcmp(buff, contents) != 0)
1487228753Smm		return (0);
1488228753Smm	return (1);
1489228753Smm#endif
1490228753Smm}
1491228753Smm
1492228753Smm/* Assert that path is a symlink that (optionally) contains contents. */
1493228753Smmint
1494228753Smmassertion_is_symlink(const char *file, int line,
1495228753Smm    const char *path, const char *contents)
1496228753Smm{
1497228753Smm	if (is_symlink(file, line, path, contents))
1498228753Smm		return (1);
1499228753Smm	if (contents)
1500228753Smm		failure_start(file, line, "File %s is not a symlink to %s",
1501228753Smm		    path, contents);
1502228753Smm	else
1503228753Smm		failure_start(file, line, "File %s is not a symlink", path);
1504228753Smm	failure_finish(NULL);
1505228753Smm	return (0);
1506228753Smm}
1507228753Smm
1508228753Smm
1509228753Smm/* Create a directory and report any errors. */
1510228753Smmint
1511228753Smmassertion_make_dir(const char *file, int line, const char *dirname, int mode)
1512228753Smm{
1513228753Smm	assertion_count(file, line);
1514228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1515228753Smm	(void)mode; /* UNUSED */
1516228753Smm	if (0 == _mkdir(dirname))
1517228753Smm		return (1);
1518228753Smm#else
1519228753Smm	if (0 == mkdir(dirname, mode))
1520228753Smm		return (1);
1521228753Smm#endif
1522228753Smm	failure_start(file, line, "Could not create directory %s", dirname);
1523228753Smm	failure_finish(NULL);
1524228753Smm	return(0);
1525228753Smm}
1526228753Smm
1527228753Smm/* Create a file with the specified contents and report any failures. */
1528228753Smmint
1529228753Smmassertion_make_file(const char *file, int line,
1530238856Smm    const char *path, int mode, int csize, const void *contents)
1531228753Smm{
1532228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1533228753Smm	/* TODO: Rework this to set file mode as well. */
1534228753Smm	FILE *f;
1535228753Smm	(void)mode; /* UNUSED */
1536228753Smm	assertion_count(file, line);
1537228753Smm	f = fopen(path, "wb");
1538228753Smm	if (f == NULL) {
1539228753Smm		failure_start(file, line, "Could not create file %s", path);
1540228753Smm		failure_finish(NULL);
1541228753Smm		return (0);
1542228753Smm	}
1543228753Smm	if (contents != NULL) {
1544238856Smm		size_t wsize;
1545238856Smm
1546238856Smm		if (csize < 0)
1547238856Smm			wsize = strlen(contents);
1548238856Smm		else
1549238856Smm			wsize = (size_t)csize;
1550238856Smm		if (wsize != fwrite(contents, 1, wsize, f)) {
1551228753Smm			fclose(f);
1552228753Smm			failure_start(file, line,
1553228753Smm			    "Could not write file %s", path);
1554228753Smm			failure_finish(NULL);
1555228753Smm			return (0);
1556228753Smm		}
1557228753Smm	}
1558228753Smm	fclose(f);
1559228753Smm	return (1);
1560228753Smm#else
1561228753Smm	int fd;
1562228753Smm	assertion_count(file, line);
1563228753Smm	fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1564228753Smm	if (fd < 0) {
1565228753Smm		failure_start(file, line, "Could not create %s", path);
1566228753Smm		failure_finish(NULL);
1567228753Smm		return (0);
1568228753Smm	}
1569228753Smm	if (contents != NULL) {
1570238856Smm		ssize_t wsize;
1571238856Smm
1572238856Smm		if (csize < 0)
1573238856Smm			wsize = (ssize_t)strlen(contents);
1574238856Smm		else
1575238856Smm			wsize = (ssize_t)csize;
1576238856Smm		if (wsize != write(fd, contents, wsize)) {
1577228753Smm			close(fd);
1578238856Smm			failure_start(file, line,
1579238856Smm			    "Could not write to %s", path);
1580228753Smm			failure_finish(NULL);
1581228753Smm			return (0);
1582228753Smm		}
1583228753Smm	}
1584228753Smm	close(fd);
1585228753Smm	return (1);
1586228753Smm#endif
1587228753Smm}
1588228753Smm
1589228753Smm/* Create a hardlink and report any failures. */
1590228753Smmint
1591228753Smmassertion_make_hardlink(const char *file, int line,
1592228753Smm    const char *newpath, const char *linkto)
1593228753Smm{
1594228753Smm	int succeeded;
1595228753Smm
1596228753Smm	assertion_count(file, line);
1597228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1598228753Smm	succeeded = my_CreateHardLinkA(newpath, linkto);
1599228753Smm#elif HAVE_LINK
1600228753Smm	succeeded = !link(linkto, newpath);
1601228753Smm#else
1602228753Smm	succeeded = 0;
1603228753Smm#endif
1604228753Smm	if (succeeded)
1605228753Smm		return (1);
1606228753Smm	failure_start(file, line, "Could not create hardlink");
1607228753Smm	logprintf("   New link: %s\n", newpath);
1608228753Smm	logprintf("   Old name: %s\n", linkto);
1609228753Smm	failure_finish(NULL);
1610228753Smm	return(0);
1611228753Smm}
1612228753Smm
1613228753Smm/* Create a symlink and report any failures. */
1614228753Smmint
1615228753Smmassertion_make_symlink(const char *file, int line,
1616228753Smm    const char *newpath, const char *linkto)
1617228753Smm{
1618228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1619228753Smm	int targetIsDir = 0;  /* TODO: Fix this */
1620228753Smm	assertion_count(file, line);
1621228753Smm	if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1622228753Smm		return (1);
1623228753Smm#elif HAVE_SYMLINK
1624228753Smm	assertion_count(file, line);
1625228753Smm	if (0 == symlink(linkto, newpath))
1626228753Smm		return (1);
1627228753Smm#endif
1628228753Smm	failure_start(file, line, "Could not create symlink");
1629228753Smm	logprintf("   New link: %s\n", newpath);
1630228753Smm	logprintf("   Old name: %s\n", linkto);
1631228753Smm	failure_finish(NULL);
1632228753Smm	return(0);
1633228753Smm}
1634228753Smm
1635228753Smm/* Set umask, report failures. */
1636228753Smmint
1637228753Smmassertion_umask(const char *file, int line, int mask)
1638228753Smm{
1639228753Smm	assertion_count(file, line);
1640228753Smm	(void)file; /* UNUSED */
1641228753Smm	(void)line; /* UNUSED */
1642228753Smm	umask(mask);
1643228753Smm	return (1);
1644228753Smm}
1645228753Smm
1646232153Smm/* Set times, report failures. */
1647232153Smmint
1648232153Smmassertion_utimes(const char *file, int line,
1649232153Smm    const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1650232153Smm{
1651232153Smm	int r;
1652232153Smm
1653232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1654232153Smm#define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1655232153Smm	 + (((nsec)/1000)*10))
1656232153Smm	HANDLE h;
1657232153Smm	ULARGE_INTEGER wintm;
1658232153Smm	FILETIME fatime, fmtime;
1659232153Smm	FILETIME *pat, *pmt;
1660232153Smm
1661232153Smm	assertion_count(file, line);
1662232153Smm	h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1663232153Smm		    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1664232153Smm		    FILE_FLAG_BACKUP_SEMANTICS, NULL);
1665232153Smm	if (h == INVALID_HANDLE_VALUE) {
1666232153Smm		failure_start(file, line, "Can't access %s\n", pathname);
1667232153Smm		failure_finish(NULL);
1668232153Smm		return (0);
1669232153Smm	}
1670232153Smm
1671232153Smm	if (at > 0 || at_nsec > 0) {
1672232153Smm		wintm.QuadPart = WINTIME(at, at_nsec);
1673232153Smm		fatime.dwLowDateTime = wintm.LowPart;
1674232153Smm		fatime.dwHighDateTime = wintm.HighPart;
1675232153Smm		pat = &fatime;
1676232153Smm	} else
1677232153Smm		pat = NULL;
1678232153Smm	if (mt > 0 || mt_nsec > 0) {
1679232153Smm		wintm.QuadPart = WINTIME(mt, mt_nsec);
1680232153Smm		fmtime.dwLowDateTime = wintm.LowPart;
1681232153Smm		fmtime.dwHighDateTime = wintm.HighPart;
1682232153Smm		pmt = &fmtime;
1683232153Smm	} else
1684232153Smm		pmt = NULL;
1685232153Smm	if (pat != NULL || pmt != NULL)
1686232153Smm		r = SetFileTime(h, NULL, pat, pmt);
1687232153Smm	else
1688232153Smm		r = 1;
1689232153Smm	CloseHandle(h);
1690232153Smm	if (r == 0) {
1691232153Smm		failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1692232153Smm		failure_finish(NULL);
1693232153Smm		return (0);
1694232153Smm	}
1695232153Smm	return (1);
1696232153Smm#else /* defined(_WIN32) && !defined(__CYGWIN__) */
1697232153Smm	struct stat st;
1698232153Smm	struct timeval times[2];
1699232153Smm
1700232153Smm#if !defined(__FreeBSD__)
1701232153Smm	mt_nsec = at_nsec = 0;	/* Generic POSIX only has whole seconds. */
1702232153Smm#endif
1703232153Smm	if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1704232153Smm		return (1);
1705232153Smm
1706232153Smm	r = lstat(pathname, &st);
1707232153Smm	if (r < 0) {
1708232153Smm		failure_start(file, line, "Can't stat %s\n", pathname);
1709232153Smm		failure_finish(NULL);
1710232153Smm		return (0);
1711232153Smm	}
1712232153Smm
1713232153Smm	if (mt == 0 && mt_nsec == 0) {
1714232153Smm		mt = st.st_mtime;
1715232153Smm#if defined(__FreeBSD__)
1716232153Smm		mt_nsec = st.st_mtimespec.tv_nsec;
1717232153Smm		/* FreeBSD generally only stores to microsecond res, so round. */
1718232153Smm		mt_nsec = (mt_nsec / 1000) * 1000;
1719232153Smm#endif
1720232153Smm	}
1721232153Smm	if (at == 0 && at_nsec == 0) {
1722232153Smm		at = st.st_atime;
1723232153Smm#if defined(__FreeBSD__)
1724232153Smm		at_nsec = st.st_atimespec.tv_nsec;
1725232153Smm		/* FreeBSD generally only stores to microsecond res, so round. */
1726232153Smm		at_nsec = (at_nsec / 1000) * 1000;
1727232153Smm#endif
1728232153Smm	}
1729232153Smm
1730232153Smm	times[1].tv_sec = mt;
1731232153Smm	times[1].tv_usec = mt_nsec / 1000;
1732232153Smm
1733232153Smm	times[0].tv_sec = at;
1734232153Smm	times[0].tv_usec = at_nsec / 1000;
1735232153Smm
1736232153Smm#ifdef HAVE_LUTIMES
1737232153Smm	r = lutimes(pathname, times);
1738232153Smm#else
1739232153Smm	r = utimes(pathname, times);
1740232153Smm#endif
1741232153Smm	if (r < 0) {
1742232153Smm		failure_start(file, line, "Can't utimes %s\n", pathname);
1743232153Smm		failure_finish(NULL);
1744232153Smm		return (0);
1745232153Smm	}
1746232153Smm	return (1);
1747232153Smm#endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1748232153Smm}
1749232153Smm
1750238856Smm/* Set nodump, report failures. */
1751238856Smmint
1752238856Smmassertion_nodump(const char *file, int line, const char *pathname)
1753238856Smm{
1754238856Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1755238856Smm	int r;
1756238856Smm
1757238856Smm	assertion_count(file, line);
1758238856Smm	r = chflags(pathname, UF_NODUMP);
1759238856Smm	if (r < 0) {
1760238856Smm		failure_start(file, line, "Can't set nodump %s\n", pathname);
1761238856Smm		failure_finish(NULL);
1762238856Smm		return (0);
1763238856Smm	}
1764238856Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1765238856Smm	 && defined(EXT2_NODUMP_FL)
1766238856Smm	int fd, r, flags;
1767238856Smm
1768238856Smm	assertion_count(file, line);
1769238856Smm	fd = open(pathname, O_RDONLY | O_NONBLOCK);
1770238856Smm	if (fd < 0) {
1771238856Smm		failure_start(file, line, "Can't open %s\n", pathname);
1772238856Smm		failure_finish(NULL);
1773238856Smm		return (0);
1774238856Smm	}
1775238856Smm	r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1776238856Smm	if (r < 0) {
1777238856Smm		failure_start(file, line, "Can't get flags %s\n", pathname);
1778238856Smm		failure_finish(NULL);
1779238856Smm		return (0);
1780238856Smm	}
1781238856Smm	flags |= EXT2_NODUMP_FL;
1782238856Smm	r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1783238856Smm	if (r < 0) {
1784238856Smm		failure_start(file, line, "Can't set nodump %s\n", pathname);
1785238856Smm		failure_finish(NULL);
1786238856Smm		return (0);
1787238856Smm	}
1788238856Smm	close(fd);
1789238856Smm#else
1790238856Smm	(void)pathname; /* UNUSED */
1791238856Smm	assertion_count(file, line);
1792238856Smm#endif
1793238856Smm	return (1);
1794238856Smm}
1795238856Smm
1796228753Smm/*
1797228753Smm *
1798228753Smm *  UTILITIES for use by tests.
1799228753Smm *
1800228753Smm */
1801228753Smm
1802228753Smm/*
1803228753Smm * Check whether platform supports symlinks.  This is intended
1804228753Smm * for tests to use in deciding whether to bother testing symlink
1805228753Smm * support; if the platform doesn't support symlinks, there's no point
1806228753Smm * in checking whether the program being tested can create them.
1807228753Smm *
1808228753Smm * Note that the first time this test is called, we actually go out to
1809228753Smm * disk to create and verify a symlink.  This is necessary because
1810228753Smm * symlink support is actually a property of a particular filesystem
1811228753Smm * and can thus vary between directories on a single system.  After
1812228753Smm * the first call, this returns the cached result from memory, so it's
1813228753Smm * safe to call it as often as you wish.
1814228753Smm */
1815228753Smmint
1816228753SmmcanSymlink(void)
1817228753Smm{
1818228753Smm	/* Remember the test result */
1819228753Smm	static int value = 0, tested = 0;
1820228753Smm	if (tested)
1821228753Smm		return (value);
1822228753Smm
1823228753Smm	++tested;
1824238856Smm	assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1825228753Smm	/* Note: Cygwin has its own symlink() emulation that does not
1826228753Smm	 * use the Win32 CreateSymbolicLink() function. */
1827228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1828228753Smm	value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1829228753Smm	    && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1830228753Smm#elif HAVE_SYMLINK
1831228753Smm	value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1832228753Smm	    && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1833228753Smm#endif
1834228753Smm	return (value);
1835228753Smm}
1836228753Smm
1837228753Smm/*
1838228753Smm * Can this platform run the gzip program?
1839228753Smm */
1840228753Smm/* Platform-dependent options for hiding the output of a subcommand. */
1841228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1842228753Smmstatic const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1843228753Smm#else
1844228753Smmstatic const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1845228753Smm#endif
1846228753Smmint
1847228753SmmcanGzip(void)
1848228753Smm{
1849228753Smm	static int tested = 0, value = 0;
1850228753Smm	if (!tested) {
1851228753Smm		tested = 1;
1852228753Smm		if (systemf("gzip -V %s", redirectArgs) == 0)
1853228753Smm			value = 1;
1854228753Smm	}
1855228753Smm	return (value);
1856228753Smm}
1857228753Smm
1858228753Smm/*
1859228753Smm * Can this platform run the gunzip program?
1860228753Smm */
1861228753Smmint
1862228753SmmcanGunzip(void)
1863228753Smm{
1864228753Smm	static int tested = 0, value = 0;
1865228753Smm	if (!tested) {
1866228753Smm		tested = 1;
1867228753Smm		if (systemf("gunzip -V %s", redirectArgs) == 0)
1868228753Smm			value = 1;
1869228753Smm	}
1870228753Smm	return (value);
1871228753Smm}
1872228753Smm
1873228753Smm/*
1874238856Smm * Can this filesystem handle nodump flags.
1875238856Smm */
1876238856Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1877238856Smm
1878238856Smmint
1879238856SmmcanNodump(void)
1880238856Smm{
1881238856Smm	const char *path = "cannodumptest";
1882238856Smm	struct stat sb;
1883238856Smm
1884238856Smm	assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
1885238856Smm	if (chflags(path, UF_NODUMP) < 0)
1886238856Smm		return (0);
1887238856Smm	if (stat(path, &sb) < 0)
1888238856Smm		return (0);
1889238856Smm	if (sb.st_flags & UF_NODUMP)
1890238856Smm		return (1);
1891238856Smm	return (0);
1892238856Smm}
1893238856Smm
1894238856Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1895238856Smm	 && defined(EXT2_NODUMP_FL)
1896238856Smm
1897238856Smmint
1898238856SmmcanNodump(void)
1899238856Smm{
1900238856Smm	const char *path = "cannodumptest";
1901238856Smm	int fd, r, flags;
1902238856Smm
1903238856Smm	assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
1904238856Smm	fd = open(path, O_RDONLY | O_NONBLOCK);
1905238856Smm	if (fd < 0)
1906238856Smm		return (0);
1907238856Smm	r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1908238856Smm	if (r < 0)
1909238856Smm		return (0);
1910238856Smm	flags |= EXT2_NODUMP_FL;
1911238856Smm	r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1912238856Smm	if (r < 0)
1913238856Smm		return (0);
1914238856Smm	close(fd);
1915238856Smm	fd = open(path, O_RDONLY | O_NONBLOCK);
1916238856Smm	if (fd < 0)
1917238856Smm		return (0);
1918238856Smm	r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1919238856Smm	if (r < 0)
1920238856Smm		return (0);
1921238856Smm	close(fd);
1922238856Smm	if (flags & EXT2_NODUMP_FL)
1923238856Smm		return (1);
1924238856Smm	return (0);
1925238856Smm}
1926238856Smm
1927238856Smm#else
1928238856Smm
1929238856Smmint
1930238856SmmcanNodump()
1931238856Smm{
1932238856Smm	return (0);
1933238856Smm}
1934238856Smm
1935238856Smm#endif
1936238856Smm
1937238856Smm/*
1938228753Smm * Sleep as needed; useful for verifying disk timestamp changes by
1939228753Smm * ensuring that the wall-clock time has actually changed before we
1940228753Smm * go back to re-read something from disk.
1941228753Smm */
1942228753Smmvoid
1943228753SmmsleepUntilAfter(time_t t)
1944228753Smm{
1945228753Smm	while (t >= time(NULL))
1946228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1947228753Smm		Sleep(500);
1948228753Smm#else
1949228753Smm		sleep(1);
1950228753Smm#endif
1951228753Smm}
1952228753Smm
1953228753Smm/*
1954228753Smm * Call standard system() call, but build up the command line using
1955228753Smm * sprintf() conventions.
1956228753Smm */
1957228753Smmint
1958228753Smmsystemf(const char *fmt, ...)
1959228753Smm{
1960228753Smm	char buff[8192];
1961228753Smm	va_list ap;
1962228753Smm	int r;
1963228753Smm
1964228753Smm	va_start(ap, fmt);
1965228753Smm	vsprintf(buff, fmt, ap);
1966228753Smm	if (verbosity > VERBOSITY_FULL)
1967228753Smm		logprintf("Cmd: %s\n", buff);
1968228753Smm	r = system(buff);
1969228753Smm	va_end(ap);
1970228753Smm	return (r);
1971228753Smm}
1972228753Smm
1973228753Smm/*
1974228753Smm * Slurp a file into memory for ease of comparison and testing.
1975228753Smm * Returns size of file in 'sizep' if non-NULL, null-terminates
1976228753Smm * data in memory for ease of use.
1977228753Smm */
1978228753Smmchar *
1979228753Smmslurpfile(size_t * sizep, const char *fmt, ...)
1980228753Smm{
1981228753Smm	char filename[8192];
1982228753Smm	struct stat st;
1983228753Smm	va_list ap;
1984228753Smm	char *p;
1985228753Smm	ssize_t bytes_read;
1986228753Smm	FILE *f;
1987228753Smm	int r;
1988228753Smm
1989228753Smm	va_start(ap, fmt);
1990228753Smm	vsprintf(filename, fmt, ap);
1991228753Smm	va_end(ap);
1992228753Smm
1993228753Smm	f = fopen(filename, "rb");
1994228753Smm	if (f == NULL) {
1995228753Smm		/* Note: No error; non-existent file is okay here. */
1996228753Smm		return (NULL);
1997228753Smm	}
1998228753Smm	r = fstat(fileno(f), &st);
1999228753Smm	if (r != 0) {
2000228753Smm		logprintf("Can't stat file %s\n", filename);
2001228753Smm		fclose(f);
2002228753Smm		return (NULL);
2003228753Smm	}
2004228753Smm	p = malloc((size_t)st.st_size + 1);
2005228753Smm	if (p == NULL) {
2006228753Smm		logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2007228753Smm		    (long int)st.st_size, filename);
2008228753Smm		fclose(f);
2009228753Smm		return (NULL);
2010228753Smm	}
2011228753Smm	bytes_read = fread(p, 1, (size_t)st.st_size, f);
2012228753Smm	if (bytes_read < st.st_size) {
2013228753Smm		logprintf("Can't read file %s\n", filename);
2014228753Smm		fclose(f);
2015228753Smm		free(p);
2016228753Smm		return (NULL);
2017228753Smm	}
2018228753Smm	p[st.st_size] = '\0';
2019228753Smm	if (sizep != NULL)
2020228753Smm		*sizep = (size_t)st.st_size;
2021228753Smm	fclose(f);
2022228753Smm	return (p);
2023228753Smm}
2024228753Smm
2025228753Smm/* Read a uuencoded file from the reference directory, decode, and
2026228753Smm * write the result into the current directory. */
2027228753Smm#define	UUDECODE(c) (((c) - 0x20) & 0x3f)
2028228753Smmvoid
2029228753Smmextract_reference_file(const char *name)
2030228753Smm{
2031228753Smm	char buff[1024];
2032228753Smm	FILE *in, *out;
2033228753Smm
2034228753Smm	sprintf(buff, "%s/%s.uu", refdir, name);
2035228753Smm	in = fopen(buff, "r");
2036228753Smm	failure("Couldn't open reference file %s", buff);
2037228753Smm	assert(in != NULL);
2038228753Smm	if (in == NULL)
2039228753Smm		return;
2040228753Smm	/* Read up to and including the 'begin' line. */
2041228753Smm	for (;;) {
2042228753Smm		if (fgets(buff, sizeof(buff), in) == NULL) {
2043228753Smm			/* TODO: This is a failure. */
2044228753Smm			return;
2045228753Smm		}
2046228753Smm		if (memcmp(buff, "begin ", 6) == 0)
2047228753Smm			break;
2048228753Smm	}
2049228753Smm	/* Now, decode the rest and write it. */
2050228753Smm	/* Not a lot of error checking here; the input better be right. */
2051228753Smm	out = fopen(name, "wb");
2052228753Smm	while (fgets(buff, sizeof(buff), in) != NULL) {
2053228753Smm		char *p = buff;
2054228753Smm		int bytes;
2055228753Smm
2056228753Smm		if (memcmp(buff, "end", 3) == 0)
2057228753Smm			break;
2058228753Smm
2059228753Smm		bytes = UUDECODE(*p++);
2060228753Smm		while (bytes > 0) {
2061228753Smm			int n = 0;
2062228753Smm			/* Write out 1-3 bytes from that. */
2063228753Smm			if (bytes > 0) {
2064228753Smm				n = UUDECODE(*p++) << 18;
2065228753Smm				n |= UUDECODE(*p++) << 12;
2066228753Smm				fputc(n >> 16, out);
2067228753Smm				--bytes;
2068228753Smm			}
2069228753Smm			if (bytes > 0) {
2070228753Smm				n |= UUDECODE(*p++) << 6;
2071228753Smm				fputc((n >> 8) & 0xFF, out);
2072228753Smm				--bytes;
2073228753Smm			}
2074228753Smm			if (bytes > 0) {
2075228753Smm				n |= UUDECODE(*p++);
2076228753Smm				fputc(n & 0xFF, out);
2077228753Smm				--bytes;
2078228753Smm			}
2079228753Smm		}
2080228753Smm	}
2081228753Smm	fclose(out);
2082228753Smm	fclose(in);
2083228753Smm}
2084228753Smm
2085232153Smmint
2086232153Smmis_LargeInode(const char *file)
2087232153Smm{
2088232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2089232153Smm	BY_HANDLE_FILE_INFORMATION bhfi;
2090232153Smm	int r;
2091232153Smm
2092232153Smm	r = my_GetFileInformationByName(file, &bhfi);
2093232153Smm	if (r != 0)
2094232153Smm		return (0);
2095232153Smm	return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2096232153Smm#else
2097232153Smm	struct stat st;
2098232153Smm	int64_t ino;
2099232153Smm
2100232153Smm	if (stat(file, &st) < 0)
2101232153Smm		return (0);
2102232153Smm	ino = (int64_t)st.st_ino;
2103232153Smm	return (ino > 0xffffffff);
2104232153Smm#endif
2105232153Smm}
2106228753Smm/*
2107228753Smm *
2108228753Smm * TEST management
2109228753Smm *
2110228753Smm */
2111228753Smm
2112228753Smm/*
2113228753Smm * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2114228753Smm * a line like
2115228753Smm *      DEFINE_TEST(test_function)
2116228753Smm * for each test.
2117228753Smm */
2118228753Smm
2119228753Smm/* Use "list.h" to declare all of the test functions. */
2120228753Smm#undef DEFINE_TEST
2121228753Smm#define	DEFINE_TEST(name) void name(void);
2122228753Smm#include "list.h"
2123228753Smm
2124228753Smm/* Use "list.h" to create a list of all tests (functions and names). */
2125228753Smm#undef DEFINE_TEST
2126228753Smm#define	DEFINE_TEST(n) { n, #n, 0 },
2127228753Smmstruct { void (*func)(void); const char *name; int failures; } tests[] = {
2128228753Smm	#include "list.h"
2129228753Smm};
2130228753Smm
2131228753Smm/*
2132228753Smm * Summarize repeated failures in the just-completed test.
2133228753Smm */
2134228753Smmstatic void
2135232153Smmtest_summarize(int failed)
2136228753Smm{
2137228753Smm	unsigned int i;
2138228753Smm
2139228753Smm	switch (verbosity) {
2140228753Smm	case VERBOSITY_SUMMARY_ONLY:
2141228753Smm		printf(failed ? "E" : ".");
2142228753Smm		fflush(stdout);
2143228753Smm		break;
2144228753Smm	case VERBOSITY_PASSFAIL:
2145228753Smm		printf(failed ? "FAIL\n" : "ok\n");
2146228753Smm		break;
2147228753Smm	}
2148228753Smm
2149228753Smm	log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2150228753Smm
2151228753Smm	for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2152228753Smm		if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2153228753Smm			logprintf("%s:%d: Summary: Failed %d times\n",
2154232153Smm			    failed_filename, i, failed_lines[i].count);
2155228753Smm	}
2156228753Smm	/* Clear the failure history for the next file. */
2157232153Smm	failed_filename = NULL;
2158228753Smm	memset(failed_lines, 0, sizeof(failed_lines));
2159228753Smm}
2160228753Smm
2161228753Smm/*
2162228753Smm * Actually run a single test, with appropriate setup and cleanup.
2163228753Smm */
2164228753Smmstatic int
2165228753Smmtest_run(int i, const char *tmpdir)
2166228753Smm{
2167232153Smm	char workdir[1024];
2168228753Smm	char logfilename[64];
2169228753Smm	int failures_before = failures;
2170228753Smm	int oldumask;
2171228753Smm
2172228753Smm	switch (verbosity) {
2173228753Smm	case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2174228753Smm		break;
2175228753Smm	case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2176228753Smm		printf("%3d: %-50s", i, tests[i].name);
2177228753Smm		fflush(stdout);
2178228753Smm		break;
2179228753Smm	default: /* Title of test, details will follow */
2180228753Smm		printf("%3d: %s\n", i, tests[i].name);
2181228753Smm	}
2182228753Smm
2183228753Smm	/* Chdir to the top-level work directory. */
2184228753Smm	if (!assertChdir(tmpdir)) {
2185228753Smm		fprintf(stderr,
2186228753Smm		    "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2187228753Smm		exit(1);
2188228753Smm	}
2189228753Smm	/* Create a log file for this test. */
2190228753Smm	sprintf(logfilename, "%s.log", tests[i].name);
2191228753Smm	logfile = fopen(logfilename, "w");
2192228753Smm	fprintf(logfile, "%s\n\n", tests[i].name);
2193228753Smm	/* Chdir() to a work dir for this specific test. */
2194232153Smm	snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2195232153Smm	testworkdir = workdir;
2196232153Smm	if (!assertMakeDir(testworkdir, 0755)
2197232153Smm	    || !assertChdir(testworkdir)) {
2198228753Smm		fprintf(stderr,
2199232153Smm		    "ERROR: Can't chdir to work dir %s\n", testworkdir);
2200228753Smm		exit(1);
2201228753Smm	}
2202228753Smm	/* Explicitly reset the locale before each test. */
2203228753Smm	setlocale(LC_ALL, "C");
2204228753Smm	/* Record the umask before we run the test. */
2205228753Smm	umask(oldumask = umask(0));
2206228753Smm	/*
2207228753Smm	 * Run the actual test.
2208228753Smm	 */
2209228753Smm	(*tests[i].func)();
2210228753Smm	/*
2211228753Smm	 * Clean up and report afterwards.
2212228753Smm	 */
2213232153Smm	testworkdir = NULL;
2214228753Smm	/* Restore umask */
2215228753Smm	umask(oldumask);
2216228753Smm	/* Reset locale. */
2217228753Smm	setlocale(LC_ALL, "C");
2218228753Smm	/* Reset directory. */
2219228753Smm	if (!assertChdir(tmpdir)) {
2220228753Smm		fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2221228753Smm		    tmpdir);
2222228753Smm		exit(1);
2223228753Smm	}
2224228753Smm	/* Report per-test summaries. */
2225228753Smm	tests[i].failures = failures - failures_before;
2226232153Smm	test_summarize(tests[i].failures);
2227228753Smm	/* Close the per-test log file. */
2228228753Smm	fclose(logfile);
2229228753Smm	logfile = NULL;
2230228753Smm	/* If there were no failures, we can remove the work dir and logfile. */
2231228753Smm	if (tests[i].failures == 0) {
2232228753Smm		if (!keep_temp_files && assertChdir(tmpdir)) {
2233228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2234228753Smm			/* Make sure not to leave empty directories.
2235228753Smm			 * Sometimes a processing of closing files used by tests
2236228753Smm			 * is not done, then rmdir will be failed and it will
2237228753Smm			 * leave a empty test directory. So we should wait a few
2238228753Smm			 * seconds and retry rmdir. */
2239228753Smm			int r, t;
2240228753Smm			for (t = 0; t < 10; t++) {
2241228753Smm				if (t > 0)
2242228753Smm					Sleep(1000);
2243228753Smm				r = systemf("rmdir /S /Q %s", tests[i].name);
2244228753Smm				if (r == 0)
2245228753Smm					break;
2246228753Smm			}
2247228753Smm			systemf("del %s", logfilename);
2248228753Smm#else
2249228753Smm			systemf("rm -rf %s", tests[i].name);
2250228753Smm			systemf("rm %s", logfilename);
2251228753Smm#endif
2252228753Smm		}
2253228753Smm	}
2254228753Smm	/* Return appropriate status. */
2255228753Smm	return (tests[i].failures);
2256228753Smm}
2257228753Smm
2258228753Smm/*
2259228753Smm *
2260228753Smm *
2261228753Smm * MAIN and support routines.
2262228753Smm *
2263228753Smm *
2264228753Smm */
2265228753Smm
2266228753Smmstatic void
2267228753Smmusage(const char *program)
2268228753Smm{
2269228753Smm	static const int limit = sizeof(tests) / sizeof(tests[0]);
2270228753Smm	int i;
2271228753Smm
2272228753Smm	printf("Usage: %s [options] <test> <test> ...\n", program);
2273228753Smm	printf("Default is to run all tests.\n");
2274228753Smm	printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2275228753Smm	printf("Options:\n");
2276228753Smm	printf("  -d  Dump core after any failure, for debugging.\n");
2277228753Smm	printf("  -k  Keep all temp files.\n");
2278228753Smm	printf("      Default: temp files for successful tests deleted.\n");
2279228753Smm#ifdef PROGRAM
2280228753Smm	printf("  -p <path>  Path to executable to be tested.\n");
2281228753Smm	printf("      Default: path taken from " ENVBASE " environment variable.\n");
2282228753Smm#endif
2283228753Smm	printf("  -q  Quiet.\n");
2284228753Smm	printf("  -r <dir>   Path to dir containing reference files.\n");
2285228753Smm	printf("      Default: Current directory.\n");
2286232153Smm	printf("  -u  Keep running specifies tests until one fails.\n");
2287228753Smm	printf("  -v  Verbose.\n");
2288228753Smm	printf("Available tests:\n");
2289228753Smm	for (i = 0; i < limit; i++)
2290228753Smm		printf("  %d: %s\n", i, tests[i].name);
2291228753Smm	exit(1);
2292228753Smm}
2293228753Smm
2294228753Smmstatic char *
2295228753Smmget_refdir(const char *d)
2296228753Smm{
2297228753Smm	char tried[512] = { '\0' };
2298228753Smm	char buff[128];
2299228753Smm	char *pwd, *p;
2300228753Smm
2301228753Smm	/* If a dir was specified, try that */
2302228753Smm	if (d != NULL) {
2303228753Smm		pwd = NULL;
2304228753Smm		snprintf(buff, sizeof(buff), "%s", d);
2305228753Smm		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2306228753Smm		if (p != NULL) goto success;
2307228753Smm		strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2308228753Smm		strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2309228753Smm		goto failure;
2310228753Smm	}
2311228753Smm
2312228753Smm	/* Get the current dir. */
2313232153Smm#ifdef PATH_MAX
2314232153Smm	pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2315232153Smm#else
2316228753Smm	pwd = getcwd(NULL, 0);
2317232153Smm#endif
2318228753Smm	while (pwd[strlen(pwd) - 1] == '\n')
2319228753Smm		pwd[strlen(pwd) - 1] = '\0';
2320228753Smm
2321228753Smm	/* Look for a known file. */
2322228753Smm	snprintf(buff, sizeof(buff), "%s", pwd);
2323228753Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2324228753Smm	if (p != NULL) goto success;
2325228753Smm	strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2326228753Smm	strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2327228753Smm
2328228753Smm	snprintf(buff, sizeof(buff), "%s/test", pwd);
2329228753Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2330228753Smm	if (p != NULL) goto success;
2331228753Smm	strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2332228753Smm	strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2333228753Smm
2334228753Smm#if defined(LIBRARY)
2335228753Smm	snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
2336228753Smm#else
2337228753Smm	snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
2338228753Smm#endif
2339228753Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2340228753Smm	if (p != NULL) goto success;
2341228753Smm	strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2342228753Smm	strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2343228753Smm
2344232153Smm#if defined(PROGRAM_ALIAS)
2345232153Smm	snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
2346232153Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2347232153Smm	if (p != NULL) goto success;
2348232153Smm	strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2349232153Smm	strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2350232153Smm#endif
2351232153Smm
2352228753Smm	if (memcmp(pwd, "/usr/obj", 8) == 0) {
2353228753Smm		snprintf(buff, sizeof(buff), "%s", pwd + 8);
2354228753Smm		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2355228753Smm		if (p != NULL) goto success;
2356228753Smm		strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2357228753Smm		strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2358228753Smm
2359228753Smm		snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
2360228753Smm		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2361228753Smm		if (p != NULL) goto success;
2362228753Smm		strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2363228753Smm		strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2364228753Smm	}
2365228753Smm
2366228753Smmfailure:
2367228753Smm	printf("Unable to locate known reference file %s\n", KNOWNREF);
2368228753Smm	printf("  Checked following directories:\n%s\n", tried);
2369228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2370228753Smm	DebugBreak();
2371228753Smm#endif
2372228753Smm	exit(1);
2373228753Smm
2374228753Smmsuccess:
2375228753Smm	free(p);
2376228753Smm	free(pwd);
2377228753Smm	return strdup(buff);
2378228753Smm}
2379228753Smm
2380238856Smmstatic int
2381238856Smmget_test_set(int *test_set, int limit, const char *test)
2382238856Smm{
2383238856Smm	int start, end;
2384238856Smm	int idx = 0;
2385238856Smm
2386238856Smm	if (test == NULL) {
2387238856Smm		/* Default: Run all tests. */
2388238856Smm		for (;idx < limit; idx++)
2389238856Smm			test_set[idx] = idx;
2390238856Smm		return (limit);
2391238856Smm	}
2392238856Smm	if (*test >= '0' && *test <= '9') {
2393238856Smm		const char *vp = test;
2394238856Smm		start = 0;
2395238856Smm		while (*vp >= '0' && *vp <= '9') {
2396238856Smm			start *= 10;
2397238856Smm			start += *vp - '0';
2398238856Smm			++vp;
2399238856Smm		}
2400238856Smm		if (*vp == '\0') {
2401238856Smm			end = start;
2402238856Smm		} else if (*vp == '-') {
2403238856Smm			++vp;
2404238856Smm			if (*vp == '\0') {
2405238856Smm				end = limit - 1;
2406238856Smm			} else {
2407238856Smm				end = 0;
2408238856Smm				while (*vp >= '0' && *vp <= '9') {
2409238856Smm					end *= 10;
2410238856Smm					end += *vp - '0';
2411238856Smm					++vp;
2412238856Smm				}
2413238856Smm			}
2414238856Smm		} else
2415238856Smm			return (-1);
2416238856Smm		if (start < 0 || end >= limit || start > end)
2417238856Smm			return (-1);
2418238856Smm		while (start <= end)
2419238856Smm			test_set[idx++] = start++;
2420238856Smm	} else {
2421238856Smm		size_t len = strlen(test);
2422238856Smm		for (start = 0; start < limit; ++start) {
2423238856Smm			const char *name = tests[start].name;
2424238856Smm			const char *p;
2425238856Smm
2426238856Smm			while ((p = strchr(name, test[0])) != NULL) {
2427238856Smm				if (strncmp(p, test, len) == 0) {
2428238856Smm					test_set[idx++] = start;
2429238856Smm					break;
2430238856Smm				} else
2431238856Smm					name = p + 1;
2432238856Smm			}
2433238856Smm
2434238856Smm		}
2435238856Smm	}
2436238856Smm	return ((idx == 0)?-1:idx);
2437238856Smm}
2438238856Smm
2439228753Smmint
2440228753Smmmain(int argc, char **argv)
2441228753Smm{
2442228753Smm	static const int limit = sizeof(tests) / sizeof(tests[0]);
2443238856Smm	int test_set[sizeof(tests) / sizeof(tests[0])];
2444238856Smm	int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2445228753Smm	time_t now;
2446228753Smm	char *refdir_alloc = NULL;
2447228753Smm	const char *progname;
2448232153Smm	char **saved_argv;
2449228753Smm	const char *tmp, *option_arg, *p;
2450238856Smm	char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2451228753Smm	char tmpdir_timestamp[256];
2452228753Smm
2453228753Smm	(void)argc; /* UNUSED */
2454228753Smm
2455232153Smm	/* Get the current dir. */
2456232153Smm#ifdef PATH_MAX
2457232153Smm	pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2458232153Smm#else
2459232153Smm	pwd = getcwd(NULL, 0);
2460232153Smm#endif
2461232153Smm	while (pwd[strlen(pwd) - 1] == '\n')
2462232153Smm		pwd[strlen(pwd) - 1] = '\0';
2463232153Smm
2464228753Smm#if defined(HAVE__CrtSetReportMode)
2465228753Smm	/* To stop to run the default invalid parameter handler. */
2466228753Smm	_set_invalid_parameter_handler(invalid_parameter_handler);
2467228753Smm	/* Disable annoying assertion message box. */
2468228753Smm	_CrtSetReportMode(_CRT_ASSERT, 0);
2469228753Smm#endif
2470228753Smm
2471228753Smm	/*
2472228753Smm	 * Name of this program, used to build root of our temp directory
2473228753Smm	 * tree.
2474228753Smm	 */
2475228753Smm	progname = p = argv[0];
2476232153Smm	if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2477232153Smm	{
2478232153Smm		fprintf(stderr, "ERROR: Out of memory.");
2479232153Smm		exit(1);
2480232153Smm	}
2481232153Smm	strcpy(testprogdir, progname);
2482228753Smm	while (*p != '\0') {
2483228753Smm		/* Support \ or / dir separators for Windows compat. */
2484228753Smm		if (*p == '/' || *p == '\\')
2485232153Smm		{
2486228753Smm			progname = p + 1;
2487232153Smm			i = j;
2488232153Smm		}
2489228753Smm		++p;
2490232153Smm		j++;
2491228753Smm	}
2492232153Smm	testprogdir[i] = '\0';
2493232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2494232153Smm	if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2495232153Smm	    !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2496232153Smm	       (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2497232153Smm		testprogdir[1] == ':' &&
2498232153Smm		(testprogdir[2] == '/' || testprogdir[2] == '\\')))
2499232153Smm#else
2500232153Smm	if (testprogdir[0] != '/')
2501232153Smm#endif
2502232153Smm	{
2503232153Smm		/* Fixup path for relative directories. */
2504232153Smm		if ((testprogdir = (char *)realloc(testprogdir,
2505232153Smm			strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2506232153Smm		{
2507232153Smm			fprintf(stderr, "ERROR: Out of memory.");
2508232153Smm			exit(1);
2509232153Smm		}
2510232153Smm		memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2511232153Smm		    strlen(testprogdir));
2512232153Smm		memcpy(testprogdir, pwd, strlen(pwd));
2513232153Smm		testprogdir[strlen(pwd)] = '/';
2514232153Smm	}
2515228753Smm
2516228753Smm#ifdef PROGRAM
2517228753Smm	/* Get the target program from environment, if available. */
2518228753Smm	testprogfile = getenv(ENVBASE);
2519228753Smm#endif
2520228753Smm
2521228753Smm	if (getenv("TMPDIR") != NULL)
2522228753Smm		tmp = getenv("TMPDIR");
2523228753Smm	else if (getenv("TMP") != NULL)
2524228753Smm		tmp = getenv("TMP");
2525228753Smm	else if (getenv("TEMP") != NULL)
2526228753Smm		tmp = getenv("TEMP");
2527228753Smm	else if (getenv("TEMPDIR") != NULL)
2528228753Smm		tmp = getenv("TEMPDIR");
2529228753Smm	else
2530228753Smm		tmp = "/tmp";
2531228753Smm
2532228753Smm	/* Allow -d to be controlled through the environment. */
2533228753Smm	if (getenv(ENVBASE "_DEBUG") != NULL)
2534228753Smm		dump_on_failure = 1;
2535228753Smm
2536238856Smm	/* Allow -v to be controlled through the environment. */
2537238856Smm	if (getenv("_VERBOSITY_LEVEL") != NULL)
2538238856Smm	{
2539238856Smm		vlevel = getenv("_VERBOSITY_LEVEL");
2540238856Smm		verbosity = atoi(vlevel);
2541238856Smm		if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2542238856Smm		{
2543238856Smm			/* Unsupported verbosity levels are silently ignored */
2544238856Smm			vlevel = NULL;
2545238856Smm			verbosity = VERBOSITY_PASSFAIL;
2546238856Smm		}
2547238856Smm	}
2548238856Smm
2549228753Smm	/* Get the directory holding test files from environment. */
2550228753Smm	refdir = getenv(ENVBASE "_TEST_FILES");
2551228753Smm
2552228753Smm	/*
2553228753Smm	 * Parse options, without using getopt(), which isn't available
2554228753Smm	 * on all platforms.
2555228753Smm	 */
2556228753Smm	++argv; /* Skip program name */
2557228753Smm	while (*argv != NULL) {
2558228753Smm		if (**argv != '-')
2559228753Smm			break;
2560228753Smm		p = *argv++;
2561228753Smm		++p; /* Skip '-' */
2562228753Smm		while (*p != '\0') {
2563228753Smm			option = *p++;
2564228753Smm			option_arg = NULL;
2565228753Smm			/* If 'opt' takes an argument, parse that. */
2566228753Smm			if (option == 'p' || option == 'r') {
2567228753Smm				if (*p != '\0')
2568228753Smm					option_arg = p;
2569228753Smm				else if (*argv == NULL) {
2570228753Smm					fprintf(stderr,
2571228753Smm					    "Option -%c requires argument.\n",
2572228753Smm					    option);
2573228753Smm					usage(progname);
2574228753Smm				} else
2575228753Smm					option_arg = *argv++;
2576228753Smm				p = ""; /* End of this option word. */
2577228753Smm			}
2578228753Smm
2579228753Smm			/* Now, handle the option. */
2580228753Smm			switch (option) {
2581228753Smm			case 'd':
2582228753Smm				dump_on_failure = 1;
2583228753Smm				break;
2584228753Smm			case 'k':
2585228753Smm				keep_temp_files = 1;
2586228753Smm				break;
2587228753Smm			case 'p':
2588228753Smm#ifdef PROGRAM
2589228753Smm				testprogfile = option_arg;
2590228753Smm#else
2591228753Smm				fprintf(stderr, "-p option not permitted\n");
2592228753Smm				usage(progname);
2593228753Smm#endif
2594228753Smm				break;
2595228753Smm			case 'q':
2596238856Smm				if (!vlevel)
2597238856Smm					verbosity--;
2598228753Smm				break;
2599228753Smm			case 'r':
2600228753Smm				refdir = option_arg;
2601228753Smm				break;
2602232153Smm			case 'u':
2603232153Smm				until_failure++;
2604232153Smm				break;
2605228753Smm			case 'v':
2606238856Smm				if (!vlevel)
2607238856Smm					verbosity++;
2608228753Smm				break;
2609228753Smm			default:
2610228753Smm				fprintf(stderr, "Unrecognized option '%c'\n",
2611228753Smm				    option);
2612228753Smm				usage(progname);
2613228753Smm			}
2614228753Smm		}
2615228753Smm	}
2616228753Smm
2617228753Smm	/*
2618228753Smm	 * Sanity-check that our options make sense.
2619228753Smm	 */
2620228753Smm#ifdef PROGRAM
2621232153Smm	if (testprogfile == NULL)
2622232153Smm	{
2623232153Smm		if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2624232153Smm			strlen(PROGRAM) + 1)) == NULL)
2625232153Smm		{
2626232153Smm			fprintf(stderr, "ERROR: Out of memory.");
2627232153Smm			exit(1);
2628232153Smm		}
2629232153Smm		strcpy(tmp2, testprogdir);
2630232153Smm		strcat(tmp2, "/");
2631232153Smm		strcat(tmp2, PROGRAM);
2632232153Smm		testprogfile = tmp2;
2633228753Smm	}
2634228753Smm
2635228753Smm	{
2636228753Smm		char *testprg;
2637228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2638228753Smm		/* Command.com sometimes rejects '/' separators. */
2639228753Smm		testprg = strdup(testprogfile);
2640228753Smm		for (i = 0; testprg[i] != '\0'; i++) {
2641228753Smm			if (testprg[i] == '/')
2642228753Smm				testprg[i] = '\\';
2643228753Smm		}
2644228753Smm		testprogfile = testprg;
2645228753Smm#endif
2646228753Smm		/* Quote the name that gets put into shell command lines. */
2647228753Smm		testprg = malloc(strlen(testprogfile) + 3);
2648228753Smm		strcpy(testprg, "\"");
2649228753Smm		strcat(testprg, testprogfile);
2650228753Smm		strcat(testprg, "\"");
2651228753Smm		testprog = testprg;
2652228753Smm	}
2653228753Smm#endif
2654228753Smm
2655232153Smm#if !defined(_WIN32) && defined(SIGPIPE)
2656232153Smm	{   /* Ignore SIGPIPE signals */
2657232153Smm		struct sigaction sa;
2658232153Smm		sa.sa_handler = SIG_IGN;
2659232153Smm		sigemptyset(&sa.sa_mask);
2660232153Smm		sa.sa_flags = 0;
2661232153Smm		sigaction(SIGPIPE, &sa, NULL);
2662232153Smm	}
2663232153Smm#endif
2664232153Smm
2665228753Smm	/*
2666228753Smm	 * Create a temp directory for the following tests.
2667228753Smm	 * Include the time the tests started as part of the name,
2668228753Smm	 * to make it easier to track the results of multiple tests.
2669228753Smm	 */
2670228753Smm	now = time(NULL);
2671228753Smm	for (i = 0; ; i++) {
2672228753Smm		strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2673228753Smm		    "%Y-%m-%dT%H.%M.%S",
2674228753Smm		    localtime(&now));
2675228753Smm		sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2676228753Smm		    tmpdir_timestamp, i);
2677228753Smm		if (assertMakeDir(tmpdir,0755))
2678228753Smm			break;
2679228753Smm		if (i >= 999) {
2680228753Smm			fprintf(stderr,
2681228753Smm			    "ERROR: Unable to create temp directory %s\n",
2682228753Smm			    tmpdir);
2683228753Smm			exit(1);
2684228753Smm		}
2685228753Smm	}
2686228753Smm
2687228753Smm	/*
2688228753Smm	 * If the user didn't specify a directory for locating
2689228753Smm	 * reference files, try to find the reference files in
2690228753Smm	 * the "usual places."
2691228753Smm	 */
2692228753Smm	refdir = refdir_alloc = get_refdir(refdir);
2693228753Smm
2694228753Smm	/*
2695228753Smm	 * Banner with basic information.
2696228753Smm	 */
2697228753Smm	printf("\n");
2698228753Smm	printf("If tests fail or crash, details will be in:\n");
2699228753Smm	printf("   %s\n", tmpdir);
2700228753Smm	printf("\n");
2701228753Smm	if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2702228753Smm		printf("Reference files will be read from: %s\n", refdir);
2703228753Smm#ifdef PROGRAM
2704228753Smm		printf("Running tests on: %s\n", testprog);
2705228753Smm#endif
2706228753Smm		printf("Exercising: ");
2707228753Smm		fflush(stdout);
2708228753Smm		printf("%s\n", EXTRA_VERSION);
2709228753Smm	} else {
2710228753Smm		printf("Running ");
2711228753Smm		fflush(stdout);
2712228753Smm	}
2713228753Smm
2714228753Smm	/*
2715228753Smm	 * Run some or all of the individual tests.
2716228753Smm	 */
2717232153Smm	saved_argv = argv;
2718232153Smm	do {
2719232153Smm		argv = saved_argv;
2720238856Smm		do {
2721238856Smm			int test_num;
2722238856Smm
2723238856Smm			test_num = get_test_set(test_set, limit, *argv);
2724238856Smm			if (test_num < 0) {
2725238856Smm				printf("*** INVALID Test %s\n", *argv);
2726238856Smm				free(refdir_alloc);
2727238856Smm				usage(progname);
2728238856Smm				return (1);
2729238856Smm			}
2730238856Smm			for (i = 0; i < test_num; i++) {
2731232153Smm				tests_run++;
2732238856Smm				if (test_run(test_set[i], tmpdir)) {
2733232153Smm					tests_failed++;
2734232153Smm					if (until_failure)
2735232153Smm						goto finish;
2736228753Smm				}
2737232153Smm			}
2738238856Smm			if (*argv != NULL)
2739232153Smm				argv++;
2740238856Smm		} while (*argv != NULL);
2741232153Smm	} while (until_failure);
2742228753Smm
2743232153Smmfinish:
2744232153Smm	/* Must be freed after all tests run */
2745232153Smm	free(tmp2);
2746232153Smm	free(testprogdir);
2747232153Smm	free(pwd);
2748232153Smm
2749228753Smm	/*
2750228753Smm	 * Report summary statistics.
2751228753Smm	 */
2752228753Smm	if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2753228753Smm		printf("\n");
2754228753Smm		printf("Totals:\n");
2755228753Smm		printf("  Tests run:         %8d\n", tests_run);
2756228753Smm		printf("  Tests failed:      %8d\n", tests_failed);
2757228753Smm		printf("  Assertions checked:%8d\n", assertions);
2758228753Smm		printf("  Assertions failed: %8d\n", failures);
2759228753Smm		printf("  Skips reported:    %8d\n", skips);
2760228753Smm	}
2761228753Smm	if (failures) {
2762228753Smm		printf("\n");
2763228753Smm		printf("Failing tests:\n");
2764228753Smm		for (i = 0; i < limit; ++i) {
2765228753Smm			if (tests[i].failures)
2766228753Smm				printf("  %d: %s (%d failures)\n", i,
2767228753Smm				    tests[i].name, tests[i].failures);
2768228753Smm		}
2769228753Smm		printf("\n");
2770228753Smm		printf("Details for failing tests: %s\n", tmpdir);
2771228753Smm		printf("\n");
2772228753Smm	} else {
2773228753Smm		if (verbosity == VERBOSITY_SUMMARY_ONLY)
2774228753Smm			printf("\n");
2775228753Smm		printf("%d tests passed, no failures\n", tests_run);
2776228753Smm	}
2777228753Smm
2778228753Smm	free(refdir_alloc);
2779228753Smm
2780228753Smm	/* If the final tmpdir is empty, we can remove it. */
2781228753Smm	/* This should be the usual case when all tests succeed. */
2782228753Smm	assertChdir("..");
2783228753Smm	rmdir(tmpdir);
2784228753Smm
2785228753Smm	return (tests_failed ? 1 : 0);
2786228753Smm}
2787