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"
27248616Smm#include "test_utils.h"
28238856Smm#ifdef HAVE_SYS_IOCTL_H
29238856Smm#include <sys/ioctl.h>
30238856Smm#endif
31232153Smm#ifdef HAVE_SYS_TIME_H
32232153Smm#include <sys/time.h>
33232153Smm#endif
34228753Smm#include <errno.h>
35232153Smm#ifdef HAVE_ICONV_H
36232153Smm#include <iconv.h>
37232153Smm#endif
38238856Smm/*
39238856Smm * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
40238856Smm * As the include guards don't agree, the order of include is important.
41238856Smm */
42238856Smm#ifdef HAVE_LINUX_EXT2_FS_H
43238856Smm#include <linux/ext2_fs.h>      /* for Linux file flags */
44238856Smm#endif
45238856Smm#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
46238856Smm#include <ext2fs/ext2_fs.h>     /* Linux file flags, broken on Cygwin */
47238856Smm#endif
48232153Smm#include <limits.h>
49228753Smm#include <locale.h>
50232153Smm#ifdef HAVE_SIGNAL_H
51232153Smm#include <signal.h>
52232153Smm#endif
53228753Smm#include <stdarg.h>
54228753Smm#include <time.h>
55228753Smm
56228753Smm/*
57228753Smm * This same file is used pretty much verbatim for all test harnesses.
58228753Smm *
59228753Smm * The next few lines are the only differences.
60228753Smm * TODO: Move this into a separate configuration header, have all test
61228753Smm * suites share one copy of this file.
62228753Smm */
63228763Smm__FBSDID("$FreeBSD: releng/10.3/contrib/libarchive/libarchive/test/main.c 306941 2016-10-10 07:18:54Z delphij $");
64228753Smm#define KNOWNREF	"test_compat_gtar_1.tar.uu"
65228753Smm#define	ENVBASE "LIBARCHIVE" /* Prefix for environment variables. */
66228753Smm#undef	PROGRAM              /* Testing a library, not a program. */
67228753Smm#define	LIBRARY	"libarchive"
68228753Smm#define	EXTRA_DUMP(x)	archive_error_string((struct archive *)(x))
69232153Smm#define	EXTRA_ERRNO(x)	archive_errno((struct archive *)(x))
70232153Smm#define	EXTRA_VERSION	archive_version_string()
71228753Smm
72228753Smm/*
73228753Smm *
74228753Smm * Windows support routines
75228753Smm *
76228753Smm * Note: Configuration is a tricky issue.  Using HAVE_* feature macros
77228753Smm * in the test harness is dangerous because they cover up
78228753Smm * configuration errors.  The classic example of this is omitting a
79228753Smm * configure check.  If libarchive and libarchive_test both look for
80228753Smm * the same feature macro, such errors are hard to detect.  Platform
81228753Smm * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
82228753Smm * easily lead to very messy code.  It's best to limit yourself
83228753Smm * to only the most generic programming techniques in the test harness
84228753Smm * and thus avoid conditionals altogether.  Where that's not possible,
85228753Smm * try to minimize conditionals by grouping platform-specific tests in
86228753Smm * one place (e.g., test_acl_freebsd) or by adding new assert()
87228753Smm * functions (e.g., assertMakeHardlink()) to cover up platform
88228753Smm * differences.  Platform-specific coding in libarchive_test is often
89228753Smm * a symptom that some capability is missing from libarchive itself.
90228753Smm */
91228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
92228753Smm#include <io.h>
93248616Smm#include <direct.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
106232153Smm#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:
346228753Smm		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");
391228753Smm		abort();
392228753Smm	}
393228753Smm}
394228753Smm
395228753Smm/* Inform user that we're skipping some checks. */
396228753Smmvoid
397228753Smmtest_skipping(const char *fmt, ...)
398228753Smm{
399228753Smm	char buff[1024];
400228753Smm	va_list ap;
401228753Smm
402228753Smm	va_start(ap, fmt);
403228753Smm	vsprintf(buff, fmt, ap);
404228753Smm	va_end(ap);
405232153Smm	/* Use failure() message if set. */
406232153Smm	msg = nextmsg;
407232153Smm	nextmsg = NULL;
408228753Smm	/* failure_start() isn't quite right, but is awfully convenient. */
409232153Smm	failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
410228753Smm	--failures; /* Undo failures++ in failure_start() */
411228753Smm	/* Don't failure_finish() here. */
412228753Smm	/* Mark as skip, so doesn't count as failed test. */
413232153Smm	failed_lines[skipping_line].skip = 1;
414228753Smm	++skips;
415228753Smm}
416228753Smm
417228753Smm/*
418228753Smm *
419228753Smm * ASSERTIONS
420228753Smm *
421228753Smm */
422228753Smm
423228753Smm/* Generic assert() just displays the failed condition. */
424228753Smmint
425228753Smmassertion_assert(const char *file, int line, int value,
426228753Smm    const char *condition, void *extra)
427228753Smm{
428228753Smm	assertion_count(file, line);
429228753Smm	if (!value) {
430228753Smm		failure_start(file, line, "Assertion failed: %s", condition);
431228753Smm		failure_finish(extra);
432228753Smm	}
433228753Smm	return (value);
434228753Smm}
435228753Smm
436228753Smm/* chdir() and report any errors */
437228753Smmint
438228753Smmassertion_chdir(const char *file, int line, const char *pathname)
439228753Smm{
440228753Smm	assertion_count(file, line);
441228753Smm	if (chdir(pathname) == 0)
442228753Smm		return (1);
443228753Smm	failure_start(file, line, "chdir(\"%s\")", pathname);
444228753Smm	failure_finish(NULL);
445228753Smm	return (0);
446228753Smm
447228753Smm}
448228753Smm
449228753Smm/* Verify two integers are equal. */
450228753Smmint
451228753Smmassertion_equal_int(const char *file, int line,
452228753Smm    long long v1, const char *e1, long long v2, const char *e2, void *extra)
453228753Smm{
454228753Smm	assertion_count(file, line);
455228753Smm	if (v1 == v2)
456228753Smm		return (1);
457228753Smm	failure_start(file, line, "%s != %s", e1, e2);
458228753Smm	logprintf("      %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
459228753Smm	logprintf("      %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
460228753Smm	failure_finish(extra);
461228753Smm	return (0);
462228753Smm}
463228753Smm
464232153Smm/*
465232153Smm * Utility to convert a single UTF-8 sequence.
466232153Smm */
467232153Smmstatic int
468232153Smm_utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
469228753Smm{
470232153Smm	static const char utf8_count[256] = {
471232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
472232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
473232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
474232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
475232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
476232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
477232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
478232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
479232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
480232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
481232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
482232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
483232153Smm		 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
484232153Smm		 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
485232153Smm		 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
486232153Smm		 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
487232153Smm	};
488232153Smm	int ch;
489232153Smm	int cnt;
490232153Smm	uint32_t wc;
491232153Smm
492232153Smm	*pwc = 0;
493232153Smm
494232153Smm	/* Sanity check. */
495232153Smm	if (n == 0)
496232153Smm		return (0);
497232153Smm	/*
498232153Smm	 * Decode 1-4 bytes depending on the value of the first byte.
499232153Smm	 */
500232153Smm	ch = (unsigned char)*s;
501232153Smm	if (ch == 0)
502232153Smm		return (0); /* Standard:  return 0 for end-of-string. */
503232153Smm	cnt = utf8_count[ch];
504232153Smm
505232153Smm	/* Invalide sequence or there are not plenty bytes. */
506232153Smm	if (n < (size_t)cnt)
507232153Smm		return (-1);
508232153Smm
509232153Smm	/* Make a Unicode code point from a single UTF-8 sequence. */
510232153Smm	switch (cnt) {
511232153Smm	case 1:	/* 1 byte sequence. */
512232153Smm		*pwc = ch & 0x7f;
513232153Smm		return (cnt);
514232153Smm	case 2:	/* 2 bytes sequence. */
515232153Smm		if ((s[1] & 0xc0) != 0x80) return (-1);
516232153Smm		*pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
517232153Smm		return (cnt);
518232153Smm	case 3:	/* 3 bytes sequence. */
519232153Smm		if ((s[1] & 0xc0) != 0x80) return (-1);
520232153Smm		if ((s[2] & 0xc0) != 0x80) return (-1);
521232153Smm		wc = ((ch & 0x0f) << 12)
522232153Smm		    | ((s[1] & 0x3f) << 6)
523232153Smm		    | (s[2] & 0x3f);
524232153Smm		if (wc < 0x800)
525232153Smm			return (-1);/* Overlong sequence. */
526232153Smm		break;
527232153Smm	case 4:	/* 4 bytes sequence. */
528232153Smm		if (n < 4)
529232153Smm			return (-1);
530232153Smm		if ((s[1] & 0xc0) != 0x80) return (-1);
531232153Smm		if ((s[2] & 0xc0) != 0x80) return (-1);
532232153Smm		if ((s[3] & 0xc0) != 0x80) return (-1);
533232153Smm		wc = ((ch & 0x07) << 18)
534232153Smm		    | ((s[1] & 0x3f) << 12)
535232153Smm		    | ((s[2] & 0x3f) << 6)
536232153Smm		    | (s[3] & 0x3f);
537232153Smm		if (wc < 0x10000)
538232153Smm			return (-1);/* Overlong sequence. */
539232153Smm		break;
540232153Smm	default:
541232153Smm		return (-1);
542232153Smm	}
543232153Smm
544232153Smm	/* The code point larger than 0x10FFFF is not leagal
545232153Smm	 * Unicode values. */
546232153Smm	if (wc > 0x10FFFF)
547232153Smm		return (-1);
548232153Smm	/* Correctly gets a Unicode, returns used bytes. */
549232153Smm	*pwc = wc;
550232153Smm	return (cnt);
551232153Smm}
552232153Smm
553232153Smmstatic void strdump(const char *e, const char *p, int ewidth, int utf8)
554232153Smm{
555228753Smm	const char *q = p;
556228753Smm
557232153Smm	logprintf("      %*s = ", ewidth, e);
558228753Smm	if (p == NULL) {
559232153Smm		logprintf("NULL\n");
560228753Smm		return;
561228753Smm	}
562228753Smm	logprintf("\"");
563228753Smm	while (*p != '\0') {
564228753Smm		unsigned int c = 0xff & *p++;
565228753Smm		switch (c) {
566228753Smm		case '\a': printf("\a"); break;
567228753Smm		case '\b': printf("\b"); break;
568228753Smm		case '\n': printf("\n"); break;
569228753Smm		case '\r': printf("\r"); break;
570228753Smm		default:
571228753Smm			if (c >= 32 && c < 127)
572228753Smm				logprintf("%c", c);
573228753Smm			else
574228753Smm				logprintf("\\x%02X", c);
575228753Smm		}
576228753Smm	}
577228753Smm	logprintf("\"");
578232153Smm	logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
579232153Smm
580232153Smm	/*
581232153Smm	 * If the current string is UTF-8, dump its code points.
582232153Smm	 */
583232153Smm	if (utf8) {
584232153Smm		size_t len;
585232153Smm		uint32_t uc;
586232153Smm		int n;
587232153Smm		int cnt = 0;
588232153Smm
589232153Smm		p = q;
590232153Smm		len = strlen(p);
591232153Smm		logprintf(" [");
592232153Smm		while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
593232153Smm			if (p != q)
594232153Smm				logprintf(" ");
595232153Smm			logprintf("%04X", uc);
596232153Smm			p += n;
597232153Smm			len -= n;
598232153Smm			cnt++;
599232153Smm		}
600232153Smm		logprintf("]");
601232153Smm		logprintf(" (count %d", cnt);
602232153Smm		if (n < 0) {
603232153Smm			logprintf(",unknown %d bytes", len);
604232153Smm		}
605232153Smm		logprintf(")");
606232153Smm
607232153Smm	}
608232153Smm	logprintf("\n");
609228753Smm}
610228753Smm
611228753Smm/* Verify two strings are equal, dump them if not. */
612228753Smmint
613228753Smmassertion_equal_string(const char *file, int line,
614228753Smm    const char *v1, const char *e1,
615228753Smm    const char *v2, const char *e2,
616232153Smm    void *extra, int utf8)
617228753Smm{
618232153Smm	int l1, l2;
619232153Smm
620228753Smm	assertion_count(file, line);
621228753Smm	if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
622228753Smm		return (1);
623228753Smm	failure_start(file, line, "%s != %s", e1, e2);
624248616Smm	l1 = (int)strlen(e1);
625248616Smm	l2 = (int)strlen(e2);
626232153Smm	if (l1 < l2)
627232153Smm		l1 = l2;
628232153Smm	strdump(e1, v1, l1, utf8);
629232153Smm	strdump(e2, v2, l1, utf8);
630228753Smm	failure_finish(extra);
631228753Smm	return (0);
632228753Smm}
633228753Smm
634228753Smmstatic void
635228753Smmwcsdump(const char *e, const wchar_t *w)
636228753Smm{
637228753Smm	logprintf("      %s = ", e);
638228753Smm	if (w == NULL) {
639228753Smm		logprintf("(null)");
640228753Smm		return;
641228753Smm	}
642228753Smm	logprintf("\"");
643228753Smm	while (*w != L'\0') {
644228753Smm		unsigned int c = *w++;
645228753Smm		if (c >= 32 && c < 127)
646228753Smm			logprintf("%c", c);
647228753Smm		else if (c < 256)
648228753Smm			logprintf("\\x%02X", c);
649228753Smm		else if (c < 0x10000)
650228753Smm			logprintf("\\u%04X", c);
651228753Smm		else
652228753Smm			logprintf("\\U%08X", c);
653228753Smm	}
654228753Smm	logprintf("\"\n");
655228753Smm}
656228753Smm
657228753Smm#ifndef HAVE_WCSCMP
658228753Smmstatic int
659228753Smmwcscmp(const wchar_t *s1, const wchar_t *s2)
660228753Smm{
661228753Smm
662228753Smm	while (*s1 == *s2++) {
663228753Smm		if (*s1++ == L'\0')
664228753Smm			return 0;
665228753Smm	}
666228753Smm	if (*s1 > *--s2)
667228753Smm		return 1;
668228753Smm	else
669228753Smm		return -1;
670228753Smm}
671228753Smm#endif
672228753Smm
673228753Smm/* Verify that two wide strings are equal, dump them if not. */
674228753Smmint
675228753Smmassertion_equal_wstring(const char *file, int line,
676228753Smm    const wchar_t *v1, const char *e1,
677228753Smm    const wchar_t *v2, const char *e2,
678228753Smm    void *extra)
679228753Smm{
680228753Smm	assertion_count(file, line);
681232153Smm	if (v1 == v2)
682228753Smm		return (1);
683232153Smm	if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
684232153Smm		return (1);
685228753Smm	failure_start(file, line, "%s != %s", e1, e2);
686228753Smm	wcsdump(e1, v1);
687228753Smm	wcsdump(e2, v2);
688228753Smm	failure_finish(extra);
689228753Smm	return (0);
690228753Smm}
691228753Smm
692228753Smm/*
693228753Smm * Pretty standard hexdump routine.  As a bonus, if ref != NULL, then
694228753Smm * any bytes in p that differ from ref will be highlighted with '_'
695228753Smm * before and after the hex value.
696228753Smm */
697228753Smmstatic void
698228753Smmhexdump(const char *p, const char *ref, size_t l, size_t offset)
699228753Smm{
700228753Smm	size_t i, j;
701228753Smm	char sep;
702228753Smm
703228753Smm	if (p == NULL) {
704228753Smm		logprintf("(null)\n");
705228753Smm		return;
706228753Smm	}
707228753Smm	for(i=0; i < l; i+=16) {
708228753Smm		logprintf("%04x", (unsigned)(i + offset));
709228753Smm		sep = ' ';
710228753Smm		for (j = 0; j < 16 && i + j < l; j++) {
711228753Smm			if (ref != NULL && p[i + j] != ref[i + j])
712228753Smm				sep = '_';
713228753Smm			logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
714228753Smm			if (ref != NULL && p[i + j] == ref[i + j])
715228753Smm				sep = ' ';
716228753Smm		}
717228753Smm		for (; j < 16; j++) {
718228753Smm			logprintf("%c  ", sep);
719228753Smm			sep = ' ';
720228753Smm		}
721228753Smm		logprintf("%c", sep);
722228753Smm		for (j=0; j < 16 && i + j < l; j++) {
723228753Smm			int c = p[i + j];
724228753Smm			if (c >= ' ' && c <= 126)
725228753Smm				logprintf("%c", c);
726228753Smm			else
727228753Smm				logprintf(".");
728228753Smm		}
729228753Smm		logprintf("\n");
730228753Smm	}
731228753Smm}
732228753Smm
733228753Smm/* Verify that two blocks of memory are the same, display the first
734228753Smm * block of differences if they're not. */
735228753Smmint
736228753Smmassertion_equal_mem(const char *file, int line,
737228753Smm    const void *_v1, const char *e1,
738228753Smm    const void *_v2, const char *e2,
739228753Smm    size_t l, const char *ld, void *extra)
740228753Smm{
741228753Smm	const char *v1 = (const char *)_v1;
742228753Smm	const char *v2 = (const char *)_v2;
743228753Smm	size_t offset;
744228753Smm
745228753Smm	assertion_count(file, line);
746228753Smm	if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
747228753Smm		return (1);
748248616Smm	if (v1 == NULL || v2 == NULL)
749248616Smm		return (0);
750228753Smm
751228753Smm	failure_start(file, line, "%s != %s", e1, e2);
752228753Smm	logprintf("      size %s = %d\n", ld, (int)l);
753228753Smm	/* Dump 48 bytes (3 lines) so that the first difference is
754228753Smm	 * in the second line. */
755228753Smm	offset = 0;
756228753Smm	while (l > 64 && memcmp(v1, v2, 32) == 0) {
757228753Smm		/* Two lines agree, so step forward one line. */
758228753Smm		v1 += 16;
759228753Smm		v2 += 16;
760228753Smm		l -= 16;
761228753Smm		offset += 16;
762228753Smm	}
763228753Smm	logprintf("      Dump of %s\n", e1);
764232153Smm	hexdump(v1, v2, l < 128 ? l : 128, offset);
765228753Smm	logprintf("      Dump of %s\n", e2);
766232153Smm	hexdump(v2, v1, l < 128 ? l : 128, offset);
767228753Smm	logprintf("\n");
768228753Smm	failure_finish(extra);
769228753Smm	return (0);
770228753Smm}
771228753Smm
772228753Smm/* Verify that the named file exists and is empty. */
773228753Smmint
774232153Smmassertion_empty_file(const char *filename, int line, const char *f1)
775228753Smm{
776228753Smm	char buff[1024];
777228753Smm	struct stat st;
778228753Smm	ssize_t s;
779228753Smm	FILE *f;
780228753Smm
781232153Smm	assertion_count(filename, line);
782228753Smm
783228753Smm	if (stat(f1, &st) != 0) {
784232153Smm		failure_start(filename, line, "Stat failed: %s", f1);
785228753Smm		failure_finish(NULL);
786228753Smm		return (0);
787228753Smm	}
788228753Smm	if (st.st_size == 0)
789228753Smm		return (1);
790228753Smm
791232153Smm	failure_start(filename, line, "File should be empty: %s", f1);
792228753Smm	logprintf("    File size: %d\n", (int)st.st_size);
793228753Smm	logprintf("    Contents:\n");
794228753Smm	f = fopen(f1, "rb");
795228753Smm	if (f == NULL) {
796228753Smm		logprintf("    Unable to open %s\n", f1);
797228753Smm	} else {
798228753Smm		s = ((off_t)sizeof(buff) < st.st_size) ?
799228753Smm		    (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
800228753Smm		s = fread(buff, 1, s, f);
801228753Smm		hexdump(buff, NULL, s, 0);
802228753Smm		fclose(f);
803228753Smm	}
804228753Smm	failure_finish(NULL);
805228753Smm	return (0);
806228753Smm}
807228753Smm
808228753Smm/* Verify that the named file exists and is not empty. */
809228753Smmint
810232153Smmassertion_non_empty_file(const char *filename, int line, const char *f1)
811228753Smm{
812228753Smm	struct stat st;
813228753Smm
814232153Smm	assertion_count(filename, line);
815228753Smm
816228753Smm	if (stat(f1, &st) != 0) {
817232153Smm		failure_start(filename, line, "Stat failed: %s", f1);
818228753Smm		failure_finish(NULL);
819228753Smm		return (0);
820228753Smm	}
821228753Smm	if (st.st_size == 0) {
822232153Smm		failure_start(filename, line, "File empty: %s", f1);
823228753Smm		failure_finish(NULL);
824228753Smm		return (0);
825228753Smm	}
826228753Smm	return (1);
827228753Smm}
828228753Smm
829228753Smm/* Verify that two files have the same contents. */
830228753Smm/* TODO: hexdump the first bytes that actually differ. */
831228753Smmint
832232153Smmassertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
833228753Smm{
834228753Smm	char buff1[1024];
835228753Smm	char buff2[1024];
836228753Smm	FILE *f1, *f2;
837228753Smm	int n1, n2;
838228753Smm
839232153Smm	assertion_count(filename, line);
840228753Smm
841228753Smm	f1 = fopen(fn1, "rb");
842228753Smm	f2 = fopen(fn2, "rb");
843248616Smm	if (f1 == NULL || f2 == NULL) {
844248616Smm		if (f1) fclose(f1);
845248616Smm		if (f2) fclose(f2);
846248616Smm		return (0);
847248616Smm	}
848228753Smm	for (;;) {
849248616Smm		n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
850248616Smm		n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
851228753Smm		if (n1 != n2)
852228753Smm			break;
853228753Smm		if (n1 == 0 && n2 == 0) {
854228753Smm			fclose(f1);
855228753Smm			fclose(f2);
856228753Smm			return (1);
857228753Smm		}
858228753Smm		if (memcmp(buff1, buff2, n1) != 0)
859228753Smm			break;
860228753Smm	}
861228753Smm	fclose(f1);
862228753Smm	fclose(f2);
863232153Smm	failure_start(filename, line, "Files not identical");
864228753Smm	logprintf("  file1=\"%s\"\n", fn1);
865228753Smm	logprintf("  file2=\"%s\"\n", fn2);
866232153Smm	failure_finish(NULL);
867228753Smm	return (0);
868228753Smm}
869228753Smm
870228753Smm/* Verify that the named file does exist. */
871228753Smmint
872232153Smmassertion_file_exists(const char *filename, int line, const char *f)
873228753Smm{
874232153Smm	assertion_count(filename, line);
875228753Smm
876228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
877228753Smm	if (!_access(f, 0))
878228753Smm		return (1);
879228753Smm#else
880228753Smm	if (!access(f, F_OK))
881228753Smm		return (1);
882228753Smm#endif
883232153Smm	failure_start(filename, line, "File should exist: %s", f);
884232153Smm	failure_finish(NULL);
885228753Smm	return (0);
886228753Smm}
887228753Smm
888228753Smm/* Verify that the named file doesn't exist. */
889228753Smmint
890232153Smmassertion_file_not_exists(const char *filename, int line, const char *f)
891228753Smm{
892232153Smm	assertion_count(filename, line);
893228753Smm
894228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
895228753Smm	if (_access(f, 0))
896228753Smm		return (1);
897228753Smm#else
898228753Smm	if (access(f, F_OK))
899228753Smm		return (1);
900228753Smm#endif
901232153Smm	failure_start(filename, line, "File should not exist: %s", f);
902232153Smm	failure_finish(NULL);
903228753Smm	return (0);
904228753Smm}
905228753Smm
906228753Smm/* Compare the contents of a file to a block of memory. */
907228753Smmint
908232153Smmassertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
909228753Smm{
910228753Smm	char *contents;
911228753Smm	FILE *f;
912228753Smm	int n;
913228753Smm
914232153Smm	assertion_count(filename, line);
915228753Smm
916228753Smm	f = fopen(fn, "rb");
917228753Smm	if (f == NULL) {
918232153Smm		failure_start(filename, line,
919228753Smm		    "File should exist: %s", fn);
920232153Smm		failure_finish(NULL);
921228753Smm		return (0);
922228753Smm	}
923228753Smm	contents = malloc(s * 2);
924248616Smm	n = (int)fread(contents, 1, s * 2, f);
925228753Smm	fclose(f);
926228753Smm	if (n == s && memcmp(buff, contents, s) == 0) {
927228753Smm		free(contents);
928228753Smm		return (1);
929228753Smm	}
930232153Smm	failure_start(filename, line, "File contents don't match");
931228753Smm	logprintf("  file=\"%s\"\n", fn);
932228753Smm	if (n > 0)
933228753Smm		hexdump(contents, buff, n > 512 ? 512 : n, 0);
934228753Smm	else {
935228753Smm		logprintf("  File empty, contents should be:\n");
936232153Smm		hexdump(buff, NULL, s > 512 ? 512 : s, 0);
937228753Smm	}
938232153Smm	failure_finish(NULL);
939228753Smm	free(contents);
940228753Smm	return (0);
941228753Smm}
942228753Smm
943228753Smm/* Check the contents of a text file, being tolerant of line endings. */
944228753Smmint
945232153Smmassertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
946228753Smm{
947228753Smm	char *contents;
948228753Smm	const char *btxt, *ftxt;
949228753Smm	FILE *f;
950228753Smm	int n, s;
951228753Smm
952232153Smm	assertion_count(filename, line);
953228753Smm	f = fopen(fn, "r");
954232153Smm	if (f == NULL) {
955232153Smm		failure_start(filename, line,
956232153Smm		    "File doesn't exist: %s", fn);
957232153Smm		failure_finish(NULL);
958232153Smm		return (0);
959232153Smm	}
960248616Smm	s = (int)strlen(buff);
961228753Smm	contents = malloc(s * 2 + 128);
962248616Smm	n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
963228753Smm	if (n >= 0)
964228753Smm		contents[n] = '\0';
965228753Smm	fclose(f);
966228753Smm	/* Compare texts. */
967228753Smm	btxt = buff;
968228753Smm	ftxt = (const char *)contents;
969228753Smm	while (*btxt != '\0' && *ftxt != '\0') {
970228753Smm		if (*btxt == *ftxt) {
971228753Smm			++btxt;
972228753Smm			++ftxt;
973228753Smm			continue;
974228753Smm		}
975228753Smm		if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
976228753Smm			/* Pass over different new line characters. */
977228753Smm			++btxt;
978228753Smm			ftxt += 2;
979228753Smm			continue;
980228753Smm		}
981228753Smm		break;
982228753Smm	}
983228753Smm	if (*btxt == '\0' && *ftxt == '\0') {
984228753Smm		free(contents);
985228753Smm		return (1);
986228753Smm	}
987232153Smm	failure_start(filename, line, "Contents don't match");
988228753Smm	logprintf("  file=\"%s\"\n", fn);
989232153Smm	if (n > 0) {
990228753Smm		hexdump(contents, buff, n, 0);
991232153Smm		logprintf("  expected\n", fn);
992232153Smm		hexdump(buff, contents, s, 0);
993232153Smm	} else {
994228753Smm		logprintf("  File empty, contents should be:\n");
995228753Smm		hexdump(buff, NULL, s, 0);
996228753Smm	}
997232153Smm	failure_finish(NULL);
998228753Smm	free(contents);
999228753Smm	return (0);
1000228753Smm}
1001228753Smm
1002232153Smm/* Verify that a text file contains the specified lines, regardless of order */
1003232153Smm/* This could be more efficient if we sorted both sets of lines, etc, but
1004232153Smm * since this is used only for testing and only ever deals with a dozen or so
1005232153Smm * lines at a time, this relatively crude approach is just fine. */
1006232153Smmint
1007232153Smmassertion_file_contains_lines_any_order(const char *file, int line,
1008232153Smm    const char *pathname, const char *lines[])
1009232153Smm{
1010232153Smm	char *buff;
1011232153Smm	size_t buff_size;
1012232153Smm	size_t expected_count, actual_count, i, j;
1013248616Smm	char **expected = NULL;
1014248616Smm	char *p, **actual = NULL;
1015232153Smm	char c;
1016232153Smm	int expected_failure = 0, actual_failure = 0;
1017232153Smm
1018232153Smm	assertion_count(file, line);
1019232153Smm
1020232153Smm	buff = slurpfile(&buff_size, "%s", pathname);
1021232153Smm	if (buff == NULL) {
1022232153Smm		failure_start(pathname, line, "Can't read file: %s", pathname);
1023232153Smm		failure_finish(NULL);
1024232153Smm		return (0);
1025232153Smm	}
1026232153Smm
1027248616Smm	/* Make a copy of the provided lines and count up the expected
1028248616Smm	 * file size. */
1029232153Smm	for (i = 0; lines[i] != NULL; ++i) {
1030232153Smm	}
1031232153Smm	expected_count = i;
1032248616Smm	if (expected_count) {
1033248616Smm		expected = malloc(sizeof(char *) * expected_count);
1034248616Smm		if (expected == NULL) {
1035248616Smm			failure_start(pathname, line, "Can't allocate memory");
1036248616Smm			failure_finish(NULL);
1037248616Smm			return (0);
1038248616Smm		}
1039248616Smm		for (i = 0; lines[i] != NULL; ++i) {
1040248616Smm			expected[i] = strdup(lines[i]);
1041248616Smm		}
1042232153Smm	}
1043232153Smm
1044232153Smm	/* Break the file into lines */
1045232153Smm	actual_count = 0;
1046232153Smm	for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1047232153Smm		if (*p == '\x0d' || *p == '\x0a')
1048232153Smm			*p = '\0';
1049232153Smm		if (c == '\0' && *p != '\0')
1050232153Smm			++actual_count;
1051232153Smm		c = *p;
1052232153Smm	}
1053248616Smm	if (actual_count) {
1054248616Smm		actual = calloc(sizeof(char *), actual_count);
1055248616Smm		if (actual == NULL) {
1056248616Smm			failure_start(pathname, line, "Can't allocate memory");
1057248616Smm			failure_finish(NULL);
1058248616Smm			free(expected);
1059248616Smm			return (0);
1060232153Smm		}
1061248616Smm		for (j = 0, p = buff; p < buff + buff_size;
1062248616Smm		    p += 1 + strlen(p)) {
1063248616Smm			if (*p != '\0') {
1064248616Smm				actual[j] = p;
1065248616Smm				++j;
1066248616Smm			}
1067248616Smm		}
1068232153Smm	}
1069232153Smm
1070232153Smm	/* Erase matching lines from both lists */
1071232153Smm	for (i = 0; i < expected_count; ++i) {
1072232153Smm		if (expected[i] == NULL)
1073232153Smm			continue;
1074232153Smm		for (j = 0; j < actual_count; ++j) {
1075232153Smm			if (actual[j] == NULL)
1076232153Smm				continue;
1077232153Smm			if (strcmp(expected[i], actual[j]) == 0) {
1078232153Smm				free(expected[i]);
1079232153Smm				expected[i] = NULL;
1080232153Smm				actual[j] = NULL;
1081232153Smm				break;
1082232153Smm			}
1083232153Smm		}
1084232153Smm	}
1085232153Smm
1086232153Smm	/* If there's anything left, it's a failure */
1087232153Smm	for (i = 0; i < expected_count; ++i) {
1088232153Smm		if (expected[i] != NULL)
1089232153Smm			++expected_failure;
1090232153Smm	}
1091232153Smm	for (j = 0; j < actual_count; ++j) {
1092232153Smm		if (actual[j] != NULL)
1093232153Smm			++actual_failure;
1094232153Smm	}
1095232153Smm	if (expected_failure == 0 && actual_failure == 0) {
1096232153Smm		free(buff);
1097232153Smm		free(expected);
1098232153Smm		free(actual);
1099232153Smm		return (1);
1100232153Smm	}
1101232153Smm	failure_start(file, line, "File doesn't match: %s", pathname);
1102232153Smm	for (i = 0; i < expected_count; ++i) {
1103232153Smm		if (expected[i] != NULL) {
1104232153Smm			logprintf("  Expected but not present: %s\n", expected[i]);
1105232153Smm			free(expected[i]);
1106232153Smm		}
1107232153Smm	}
1108232153Smm	for (j = 0; j < actual_count; ++j) {
1109232153Smm		if (actual[j] != NULL)
1110232153Smm			logprintf("  Present but not expected: %s\n", actual[j]);
1111232153Smm	}
1112232153Smm	failure_finish(NULL);
1113232153Smm	free(buff);
1114232153Smm	free(expected);
1115232153Smm	free(actual);
1116232153Smm	return (0);
1117232153Smm}
1118232153Smm
1119228753Smm/* Test that two paths point to the same file. */
1120228753Smm/* As a side-effect, asserts that both files exist. */
1121228753Smmstatic int
1122228753Smmis_hardlink(const char *file, int line,
1123228753Smm    const char *path1, const char *path2)
1124228753Smm{
1125228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1126228753Smm	BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1127228753Smm	int r;
1128228753Smm
1129228753Smm	assertion_count(file, line);
1130228753Smm	r = my_GetFileInformationByName(path1, &bhfi1);
1131228753Smm	if (r == 0) {
1132228753Smm		failure_start(file, line, "File %s can't be inspected?", path1);
1133228753Smm		failure_finish(NULL);
1134228753Smm		return (0);
1135228753Smm	}
1136228753Smm	r = my_GetFileInformationByName(path2, &bhfi2);
1137228753Smm	if (r == 0) {
1138228753Smm		failure_start(file, line, "File %s can't be inspected?", path2);
1139228753Smm		failure_finish(NULL);
1140228753Smm		return (0);
1141228753Smm	}
1142228753Smm	return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1143228753Smm		&& bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1144228753Smm		&& bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1145228753Smm#else
1146228753Smm	struct stat st1, st2;
1147228753Smm	int r;
1148228753Smm
1149228753Smm	assertion_count(file, line);
1150228753Smm	r = lstat(path1, &st1);
1151228753Smm	if (r != 0) {
1152228753Smm		failure_start(file, line, "File should exist: %s", path1);
1153228753Smm		failure_finish(NULL);
1154228753Smm		return (0);
1155228753Smm	}
1156228753Smm	r = lstat(path2, &st2);
1157228753Smm	if (r != 0) {
1158228753Smm		failure_start(file, line, "File should exist: %s", path2);
1159228753Smm		failure_finish(NULL);
1160228753Smm		return (0);
1161228753Smm	}
1162228753Smm	return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1163228753Smm#endif
1164228753Smm}
1165228753Smm
1166228753Smmint
1167228753Smmassertion_is_hardlink(const char *file, int line,
1168228753Smm    const char *path1, const char *path2)
1169228753Smm{
1170228753Smm	if (is_hardlink(file, line, path1, path2))
1171228753Smm		return (1);
1172228753Smm	failure_start(file, line,
1173228753Smm	    "Files %s and %s are not hardlinked", path1, path2);
1174228753Smm	failure_finish(NULL);
1175228753Smm	return (0);
1176228753Smm}
1177228753Smm
1178228753Smmint
1179228753Smmassertion_is_not_hardlink(const char *file, int line,
1180228753Smm    const char *path1, const char *path2)
1181228753Smm{
1182228753Smm	if (!is_hardlink(file, line, path1, path2))
1183228753Smm		return (1);
1184228753Smm	failure_start(file, line,
1185228753Smm	    "Files %s and %s should not be hardlinked", path1, path2);
1186228753Smm	failure_finish(NULL);
1187228753Smm	return (0);
1188228753Smm}
1189228753Smm
1190228753Smm/* Verify a/b/mtime of 'pathname'. */
1191228753Smm/* If 'recent', verify that it's within last 10 seconds. */
1192228753Smmstatic int
1193228753Smmassertion_file_time(const char *file, int line,
1194228753Smm    const char *pathname, long t, long nsec, char type, int recent)
1195228753Smm{
1196228753Smm	long long filet, filet_nsec;
1197228753Smm	int r;
1198228753Smm
1199228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1200228753Smm#define EPOC_TIME	(116444736000000000ULL)
1201248616Smm	FILETIME fxtime, fbirthtime, fatime, fmtime;
1202228753Smm	ULARGE_INTEGER wintm;
1203228753Smm	HANDLE h;
1204248616Smm	fxtime.dwLowDateTime = 0;
1205248616Smm	fxtime.dwHighDateTime = 0;
1206228753Smm
1207228753Smm	assertion_count(file, line);
1208232153Smm	/* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1209232153Smm	 * a directory file. If not, CreateFile() will fail when
1210232153Smm	 * the pathname is a directory. */
1211228753Smm	h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1212232153Smm	    OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1213228753Smm	if (h == INVALID_HANDLE_VALUE) {
1214228753Smm		failure_start(file, line, "Can't access %s\n", pathname);
1215228753Smm		failure_finish(NULL);
1216228753Smm		return (0);
1217228753Smm	}
1218228753Smm	r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1219228753Smm	switch (type) {
1220248616Smm	case 'a': fxtime = fatime; break;
1221248616Smm	case 'b': fxtime = fbirthtime; break;
1222248616Smm	case 'm': fxtime = fmtime; break;
1223228753Smm	}
1224228753Smm	CloseHandle(h);
1225228753Smm	if (r == 0) {
1226228753Smm		failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1227228753Smm		failure_finish(NULL);
1228228753Smm		return (0);
1229228753Smm	}
1230248616Smm	wintm.LowPart = fxtime.dwLowDateTime;
1231248616Smm	wintm.HighPart = fxtime.dwHighDateTime;
1232228753Smm	filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1233228753Smm	filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1234228753Smm	nsec = (nsec / 100) * 100; /* Round the request */
1235228753Smm#else
1236228753Smm	struct stat st;
1237228753Smm
1238228753Smm	assertion_count(file, line);
1239228753Smm	r = lstat(pathname, &st);
1240228753Smm	if (r != 0) {
1241228753Smm		failure_start(file, line, "Can't stat %s\n", pathname);
1242228753Smm		failure_finish(NULL);
1243228753Smm		return (0);
1244228753Smm	}
1245228753Smm	switch (type) {
1246228753Smm	case 'a': filet = st.st_atime; break;
1247228753Smm	case 'm': filet = st.st_mtime; break;
1248228753Smm	case 'b': filet = 0; break;
1249228753Smm	default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1250228753Smm		exit(1);
1251228753Smm	}
1252228753Smm#if defined(__FreeBSD__)
1253228753Smm	switch (type) {
1254228753Smm	case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1255228753Smm	case 'b': filet = st.st_birthtime;
1256228753Smm		filet_nsec = st.st_birthtimespec.tv_nsec; break;
1257228753Smm	case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1258228753Smm	default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1259228753Smm		exit(1);
1260228753Smm	}
1261228753Smm	/* FreeBSD generally only stores to microsecond res, so round. */
1262228753Smm	filet_nsec = (filet_nsec / 1000) * 1000;
1263228753Smm	nsec = (nsec / 1000) * 1000;
1264228753Smm#else
1265228753Smm	filet_nsec = nsec = 0;	/* Generic POSIX only has whole seconds. */
1266228753Smm	if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1267228753Smm#if defined(__HAIKU__)
1268228753Smm	if (type == 'a') return (1); /* Haiku doesn't have atime. */
1269228753Smm#endif
1270228753Smm#endif
1271228753Smm#endif
1272228753Smm	if (recent) {
1273228753Smm		/* Check that requested time is up-to-date. */
1274228753Smm		time_t now = time(NULL);
1275228753Smm		if (filet < now - 10 || filet > now + 1) {
1276228753Smm			failure_start(file, line,
1277232153Smm			    "File %s has %ctime %lld, %lld seconds ago\n",
1278228753Smm			    pathname, type, filet, now - filet);
1279228753Smm			failure_finish(NULL);
1280228753Smm			return (0);
1281228753Smm		}
1282228753Smm	} else if (filet != t || filet_nsec != nsec) {
1283228753Smm		failure_start(file, line,
1284232153Smm		    "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1285228753Smm		    pathname, type, filet, filet_nsec, t, nsec);
1286228753Smm		failure_finish(NULL);
1287228753Smm		return (0);
1288228753Smm	}
1289228753Smm	return (1);
1290228753Smm}
1291228753Smm
1292228753Smm/* Verify atime of 'pathname'. */
1293228753Smmint
1294228753Smmassertion_file_atime(const char *file, int line,
1295228753Smm    const char *pathname, long t, long nsec)
1296228753Smm{
1297228753Smm	return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1298228753Smm}
1299228753Smm
1300228753Smm/* Verify atime of 'pathname' is up-to-date. */
1301228753Smmint
1302228753Smmassertion_file_atime_recent(const char *file, int line, const char *pathname)
1303228753Smm{
1304228753Smm	return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1305228753Smm}
1306228753Smm
1307228753Smm/* Verify birthtime of 'pathname'. */
1308228753Smmint
1309228753Smmassertion_file_birthtime(const char *file, int line,
1310228753Smm    const char *pathname, long t, long nsec)
1311228753Smm{
1312228753Smm	return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1313228753Smm}
1314228753Smm
1315228753Smm/* Verify birthtime of 'pathname' is up-to-date. */
1316228753Smmint
1317228753Smmassertion_file_birthtime_recent(const char *file, int line,
1318228753Smm    const char *pathname)
1319228753Smm{
1320228753Smm	return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1321228753Smm}
1322228753Smm
1323228753Smm/* Verify mtime of 'pathname'. */
1324228753Smmint
1325228753Smmassertion_file_mtime(const char *file, int line,
1326228753Smm    const char *pathname, long t, long nsec)
1327228753Smm{
1328228753Smm	return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1329228753Smm}
1330228753Smm
1331228753Smm/* Verify mtime of 'pathname' is up-to-date. */
1332228753Smmint
1333228753Smmassertion_file_mtime_recent(const char *file, int line, const char *pathname)
1334228753Smm{
1335228753Smm	return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1336228753Smm}
1337228753Smm
1338228753Smm/* Verify number of links to 'pathname'. */
1339228753Smmint
1340228753Smmassertion_file_nlinks(const char *file, int line,
1341228753Smm    const char *pathname, int nlinks)
1342228753Smm{
1343228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1344228753Smm	BY_HANDLE_FILE_INFORMATION bhfi;
1345228753Smm	int r;
1346228753Smm
1347228753Smm	assertion_count(file, line);
1348228753Smm	r = my_GetFileInformationByName(pathname, &bhfi);
1349228753Smm	if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1350228753Smm		return (1);
1351228753Smm	failure_start(file, line, "File %s has %d links, expected %d",
1352228753Smm	    pathname, bhfi.nNumberOfLinks, nlinks);
1353228753Smm	failure_finish(NULL);
1354228753Smm	return (0);
1355228753Smm#else
1356228753Smm	struct stat st;
1357228753Smm	int r;
1358228753Smm
1359228753Smm	assertion_count(file, line);
1360228753Smm	r = lstat(pathname, &st);
1361232153Smm	if (r == 0 && (int)st.st_nlink == nlinks)
1362228753Smm			return (1);
1363228753Smm	failure_start(file, line, "File %s has %d links, expected %d",
1364228753Smm	    pathname, st.st_nlink, nlinks);
1365228753Smm	failure_finish(NULL);
1366228753Smm	return (0);
1367228753Smm#endif
1368228753Smm}
1369228753Smm
1370228753Smm/* Verify size of 'pathname'. */
1371228753Smmint
1372228753Smmassertion_file_size(const char *file, int line, const char *pathname, long size)
1373228753Smm{
1374228753Smm	int64_t filesize;
1375228753Smm	int r;
1376228753Smm
1377228753Smm	assertion_count(file, line);
1378228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1379228753Smm	{
1380228753Smm		BY_HANDLE_FILE_INFORMATION bhfi;
1381228753Smm		r = !my_GetFileInformationByName(pathname, &bhfi);
1382228753Smm		filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1383228753Smm	}
1384228753Smm#else
1385228753Smm	{
1386228753Smm		struct stat st;
1387228753Smm		r = lstat(pathname, &st);
1388228753Smm		filesize = st.st_size;
1389228753Smm	}
1390228753Smm#endif
1391228753Smm	if (r == 0 && filesize == size)
1392228753Smm			return (1);
1393228753Smm	failure_start(file, line, "File %s has size %ld, expected %ld",
1394228753Smm	    pathname, (long)filesize, (long)size);
1395228753Smm	failure_finish(NULL);
1396228753Smm	return (0);
1397228753Smm}
1398228753Smm
1399306941Sdelphij/* Verify mode of 'pathname'. */
1400306941Sdelphijint
1401306941Sdelphijassertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1402306941Sdelphij{
1403306941Sdelphij	int mode;
1404306941Sdelphij	int r;
1405306941Sdelphij
1406306941Sdelphij	assertion_count(file, line);
1407306941Sdelphij#if defined(_WIN32) && !defined(__CYGWIN__)
1408306941Sdelphij	failure_start(file, line, "assertFileMode not yet implemented for Windows");
1409306941Sdelphij#else
1410306941Sdelphij	{
1411306941Sdelphij		struct stat st;
1412306941Sdelphij		r = lstat(pathname, &st);
1413306941Sdelphij		mode = (int)(st.st_mode & 0777);
1414306941Sdelphij	}
1415306941Sdelphij	if (r == 0 && mode == expected_mode)
1416306941Sdelphij		return (1);
1417306941Sdelphij	failure_start(file, line, "File %s has mode %o, expected %o",
1418306941Sdelphij	    pathname, mode, expected_mode);
1419306941Sdelphij#endif
1420306941Sdelphij	failure_finish(NULL);
1421306941Sdelphij	return (0);
1422306941Sdelphij}
1423306941Sdelphij
1424228753Smm/* Assert that 'pathname' is a dir.  If mode >= 0, verify that too. */
1425228753Smmint
1426228753Smmassertion_is_dir(const char *file, int line, const char *pathname, int mode)
1427228753Smm{
1428228753Smm	struct stat st;
1429228753Smm	int r;
1430228753Smm
1431228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1432228753Smm	(void)mode; /* UNUSED */
1433228753Smm#endif
1434228753Smm	assertion_count(file, line);
1435228753Smm	r = lstat(pathname, &st);
1436228753Smm	if (r != 0) {
1437228753Smm		failure_start(file, line, "Dir should exist: %s", pathname);
1438228753Smm		failure_finish(NULL);
1439228753Smm		return (0);
1440228753Smm	}
1441228753Smm	if (!S_ISDIR(st.st_mode)) {
1442228753Smm		failure_start(file, line, "%s is not a dir", pathname);
1443228753Smm		failure_finish(NULL);
1444228753Smm		return (0);
1445228753Smm	}
1446228753Smm#if !defined(_WIN32) || defined(__CYGWIN__)
1447228753Smm	/* Windows doesn't handle permissions the same way as POSIX,
1448228753Smm	 * so just ignore the mode tests. */
1449228753Smm	/* TODO: Can we do better here? */
1450232153Smm	if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1451228753Smm		failure_start(file, line, "Dir %s has wrong mode", pathname);
1452228753Smm		logprintf("  Expected: 0%3o\n", mode);
1453228753Smm		logprintf("  Found: 0%3o\n", st.st_mode & 07777);
1454228753Smm		failure_finish(NULL);
1455228753Smm		return (0);
1456228753Smm	}
1457228753Smm#endif
1458228753Smm	return (1);
1459228753Smm}
1460228753Smm
1461228753Smm/* Verify that 'pathname' is a regular file.  If 'mode' is >= 0,
1462228753Smm * verify that too. */
1463228753Smmint
1464228753Smmassertion_is_reg(const char *file, int line, const char *pathname, int mode)
1465228753Smm{
1466228753Smm	struct stat st;
1467228753Smm	int r;
1468228753Smm
1469228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1470228753Smm	(void)mode; /* UNUSED */
1471228753Smm#endif
1472228753Smm	assertion_count(file, line);
1473228753Smm	r = lstat(pathname, &st);
1474228753Smm	if (r != 0 || !S_ISREG(st.st_mode)) {
1475228753Smm		failure_start(file, line, "File should exist: %s", pathname);
1476228753Smm		failure_finish(NULL);
1477228753Smm		return (0);
1478228753Smm	}
1479228753Smm#if !defined(_WIN32) || defined(__CYGWIN__)
1480228753Smm	/* Windows doesn't handle permissions the same way as POSIX,
1481228753Smm	 * so just ignore the mode tests. */
1482228753Smm	/* TODO: Can we do better here? */
1483232153Smm	if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1484228753Smm		failure_start(file, line, "File %s has wrong mode", pathname);
1485228753Smm		logprintf("  Expected: 0%3o\n", mode);
1486228753Smm		logprintf("  Found: 0%3o\n", st.st_mode & 07777);
1487228753Smm		failure_finish(NULL);
1488228753Smm		return (0);
1489228753Smm	}
1490228753Smm#endif
1491228753Smm	return (1);
1492228753Smm}
1493228753Smm
1494228753Smm/* Check whether 'pathname' is a symbolic link.  If 'contents' is
1495228753Smm * non-NULL, verify that the symlink has those contents. */
1496228753Smmstatic int
1497228753Smmis_symlink(const char *file, int line,
1498228753Smm    const char *pathname, const char *contents)
1499228753Smm{
1500228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1501228753Smm	(void)pathname; /* UNUSED */
1502228753Smm	(void)contents; /* UNUSED */
1503228753Smm	assertion_count(file, line);
1504228753Smm	/* Windows sort-of has real symlinks, but they're only usable
1505228753Smm	 * by privileged users and are crippled even then, so there's
1506228753Smm	 * really not much point in bothering with this. */
1507228753Smm	return (0);
1508228753Smm#else
1509228753Smm	char buff[300];
1510228753Smm	struct stat st;
1511228753Smm	ssize_t linklen;
1512228753Smm	int r;
1513228753Smm
1514228753Smm	assertion_count(file, line);
1515228753Smm	r = lstat(pathname, &st);
1516228753Smm	if (r != 0) {
1517228753Smm		failure_start(file, line,
1518228753Smm		    "Symlink should exist: %s", pathname);
1519228753Smm		failure_finish(NULL);
1520228753Smm		return (0);
1521228753Smm	}
1522228753Smm	if (!S_ISLNK(st.st_mode))
1523228753Smm		return (0);
1524228753Smm	if (contents == NULL)
1525228753Smm		return (1);
1526228753Smm	linklen = readlink(pathname, buff, sizeof(buff));
1527228753Smm	if (linklen < 0) {
1528228753Smm		failure_start(file, line, "Can't read symlink %s", pathname);
1529228753Smm		failure_finish(NULL);
1530228753Smm		return (0);
1531228753Smm	}
1532228753Smm	buff[linklen] = '\0';
1533228753Smm	if (strcmp(buff, contents) != 0)
1534228753Smm		return (0);
1535228753Smm	return (1);
1536228753Smm#endif
1537228753Smm}
1538228753Smm
1539228753Smm/* Assert that path is a symlink that (optionally) contains contents. */
1540228753Smmint
1541228753Smmassertion_is_symlink(const char *file, int line,
1542228753Smm    const char *path, const char *contents)
1543228753Smm{
1544228753Smm	if (is_symlink(file, line, path, contents))
1545228753Smm		return (1);
1546228753Smm	if (contents)
1547228753Smm		failure_start(file, line, "File %s is not a symlink to %s",
1548228753Smm		    path, contents);
1549228753Smm	else
1550228753Smm		failure_start(file, line, "File %s is not a symlink", path);
1551228753Smm	failure_finish(NULL);
1552228753Smm	return (0);
1553228753Smm}
1554228753Smm
1555228753Smm
1556228753Smm/* Create a directory and report any errors. */
1557228753Smmint
1558228753Smmassertion_make_dir(const char *file, int line, const char *dirname, int mode)
1559228753Smm{
1560228753Smm	assertion_count(file, line);
1561228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1562228753Smm	(void)mode; /* UNUSED */
1563228753Smm	if (0 == _mkdir(dirname))
1564228753Smm		return (1);
1565228753Smm#else
1566228753Smm	if (0 == mkdir(dirname, mode))
1567228753Smm		return (1);
1568228753Smm#endif
1569228753Smm	failure_start(file, line, "Could not create directory %s", dirname);
1570228753Smm	failure_finish(NULL);
1571228753Smm	return(0);
1572228753Smm}
1573228753Smm
1574228753Smm/* Create a file with the specified contents and report any failures. */
1575228753Smmint
1576228753Smmassertion_make_file(const char *file, int line,
1577238856Smm    const char *path, int mode, int csize, const void *contents)
1578228753Smm{
1579228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1580228753Smm	/* TODO: Rework this to set file mode as well. */
1581228753Smm	FILE *f;
1582228753Smm	(void)mode; /* UNUSED */
1583228753Smm	assertion_count(file, line);
1584228753Smm	f = fopen(path, "wb");
1585228753Smm	if (f == NULL) {
1586228753Smm		failure_start(file, line, "Could not create file %s", path);
1587228753Smm		failure_finish(NULL);
1588228753Smm		return (0);
1589228753Smm	}
1590228753Smm	if (contents != NULL) {
1591238856Smm		size_t wsize;
1592238856Smm
1593238856Smm		if (csize < 0)
1594238856Smm			wsize = strlen(contents);
1595238856Smm		else
1596238856Smm			wsize = (size_t)csize;
1597238856Smm		if (wsize != fwrite(contents, 1, wsize, f)) {
1598228753Smm			fclose(f);
1599228753Smm			failure_start(file, line,
1600228753Smm			    "Could not write file %s", path);
1601228753Smm			failure_finish(NULL);
1602228753Smm			return (0);
1603228753Smm		}
1604228753Smm	}
1605228753Smm	fclose(f);
1606228753Smm	return (1);
1607228753Smm#else
1608228753Smm	int fd;
1609228753Smm	assertion_count(file, line);
1610228753Smm	fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1611228753Smm	if (fd < 0) {
1612228753Smm		failure_start(file, line, "Could not create %s", path);
1613228753Smm		failure_finish(NULL);
1614228753Smm		return (0);
1615228753Smm	}
1616228753Smm	if (contents != NULL) {
1617238856Smm		ssize_t wsize;
1618238856Smm
1619238856Smm		if (csize < 0)
1620238856Smm			wsize = (ssize_t)strlen(contents);
1621238856Smm		else
1622238856Smm			wsize = (ssize_t)csize;
1623238856Smm		if (wsize != write(fd, contents, wsize)) {
1624228753Smm			close(fd);
1625238856Smm			failure_start(file, line,
1626238856Smm			    "Could not write to %s", path);
1627228753Smm			failure_finish(NULL);
1628228753Smm			return (0);
1629228753Smm		}
1630228753Smm	}
1631228753Smm	close(fd);
1632228753Smm	return (1);
1633228753Smm#endif
1634228753Smm}
1635228753Smm
1636228753Smm/* Create a hardlink and report any failures. */
1637228753Smmint
1638228753Smmassertion_make_hardlink(const char *file, int line,
1639228753Smm    const char *newpath, const char *linkto)
1640228753Smm{
1641228753Smm	int succeeded;
1642228753Smm
1643228753Smm	assertion_count(file, line);
1644228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1645228753Smm	succeeded = my_CreateHardLinkA(newpath, linkto);
1646228753Smm#elif HAVE_LINK
1647228753Smm	succeeded = !link(linkto, newpath);
1648228753Smm#else
1649228753Smm	succeeded = 0;
1650228753Smm#endif
1651228753Smm	if (succeeded)
1652228753Smm		return (1);
1653228753Smm	failure_start(file, line, "Could not create hardlink");
1654228753Smm	logprintf("   New link: %s\n", newpath);
1655228753Smm	logprintf("   Old name: %s\n", linkto);
1656228753Smm	failure_finish(NULL);
1657228753Smm	return(0);
1658228753Smm}
1659228753Smm
1660228753Smm/* Create a symlink and report any failures. */
1661228753Smmint
1662228753Smmassertion_make_symlink(const char *file, int line,
1663228753Smm    const char *newpath, const char *linkto)
1664228753Smm{
1665228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1666228753Smm	int targetIsDir = 0;  /* TODO: Fix this */
1667228753Smm	assertion_count(file, line);
1668228753Smm	if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1669228753Smm		return (1);
1670228753Smm#elif HAVE_SYMLINK
1671228753Smm	assertion_count(file, line);
1672228753Smm	if (0 == symlink(linkto, newpath))
1673228753Smm		return (1);
1674228753Smm#endif
1675228753Smm	failure_start(file, line, "Could not create symlink");
1676228753Smm	logprintf("   New link: %s\n", newpath);
1677228753Smm	logprintf("   Old name: %s\n", linkto);
1678228753Smm	failure_finish(NULL);
1679228753Smm	return(0);
1680228753Smm}
1681228753Smm
1682228753Smm/* Set umask, report failures. */
1683228753Smmint
1684228753Smmassertion_umask(const char *file, int line, int mask)
1685228753Smm{
1686228753Smm	assertion_count(file, line);
1687228753Smm	(void)file; /* UNUSED */
1688228753Smm	(void)line; /* UNUSED */
1689228753Smm	umask(mask);
1690228753Smm	return (1);
1691228753Smm}
1692228753Smm
1693232153Smm/* Set times, report failures. */
1694232153Smmint
1695232153Smmassertion_utimes(const char *file, int line,
1696232153Smm    const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1697232153Smm{
1698232153Smm	int r;
1699232153Smm
1700232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1701232153Smm#define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1702232153Smm	 + (((nsec)/1000)*10))
1703232153Smm	HANDLE h;
1704232153Smm	ULARGE_INTEGER wintm;
1705232153Smm	FILETIME fatime, fmtime;
1706232153Smm	FILETIME *pat, *pmt;
1707232153Smm
1708232153Smm	assertion_count(file, line);
1709232153Smm	h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1710232153Smm		    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1711232153Smm		    FILE_FLAG_BACKUP_SEMANTICS, NULL);
1712232153Smm	if (h == INVALID_HANDLE_VALUE) {
1713232153Smm		failure_start(file, line, "Can't access %s\n", pathname);
1714232153Smm		failure_finish(NULL);
1715232153Smm		return (0);
1716232153Smm	}
1717232153Smm
1718232153Smm	if (at > 0 || at_nsec > 0) {
1719232153Smm		wintm.QuadPart = WINTIME(at, at_nsec);
1720232153Smm		fatime.dwLowDateTime = wintm.LowPart;
1721232153Smm		fatime.dwHighDateTime = wintm.HighPart;
1722232153Smm		pat = &fatime;
1723232153Smm	} else
1724232153Smm		pat = NULL;
1725232153Smm	if (mt > 0 || mt_nsec > 0) {
1726232153Smm		wintm.QuadPart = WINTIME(mt, mt_nsec);
1727232153Smm		fmtime.dwLowDateTime = wintm.LowPart;
1728232153Smm		fmtime.dwHighDateTime = wintm.HighPart;
1729232153Smm		pmt = &fmtime;
1730232153Smm	} else
1731232153Smm		pmt = NULL;
1732232153Smm	if (pat != NULL || pmt != NULL)
1733232153Smm		r = SetFileTime(h, NULL, pat, pmt);
1734232153Smm	else
1735232153Smm		r = 1;
1736232153Smm	CloseHandle(h);
1737232153Smm	if (r == 0) {
1738232153Smm		failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1739232153Smm		failure_finish(NULL);
1740232153Smm		return (0);
1741232153Smm	}
1742232153Smm	return (1);
1743232153Smm#else /* defined(_WIN32) && !defined(__CYGWIN__) */
1744232153Smm	struct stat st;
1745232153Smm	struct timeval times[2];
1746232153Smm
1747232153Smm#if !defined(__FreeBSD__)
1748232153Smm	mt_nsec = at_nsec = 0;	/* Generic POSIX only has whole seconds. */
1749232153Smm#endif
1750232153Smm	if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1751232153Smm		return (1);
1752232153Smm
1753232153Smm	r = lstat(pathname, &st);
1754232153Smm	if (r < 0) {
1755232153Smm		failure_start(file, line, "Can't stat %s\n", pathname);
1756232153Smm		failure_finish(NULL);
1757232153Smm		return (0);
1758232153Smm	}
1759232153Smm
1760232153Smm	if (mt == 0 && mt_nsec == 0) {
1761232153Smm		mt = st.st_mtime;
1762232153Smm#if defined(__FreeBSD__)
1763232153Smm		mt_nsec = st.st_mtimespec.tv_nsec;
1764232153Smm		/* FreeBSD generally only stores to microsecond res, so round. */
1765232153Smm		mt_nsec = (mt_nsec / 1000) * 1000;
1766232153Smm#endif
1767232153Smm	}
1768232153Smm	if (at == 0 && at_nsec == 0) {
1769232153Smm		at = st.st_atime;
1770232153Smm#if defined(__FreeBSD__)
1771232153Smm		at_nsec = st.st_atimespec.tv_nsec;
1772232153Smm		/* FreeBSD generally only stores to microsecond res, so round. */
1773232153Smm		at_nsec = (at_nsec / 1000) * 1000;
1774232153Smm#endif
1775232153Smm	}
1776232153Smm
1777232153Smm	times[1].tv_sec = mt;
1778232153Smm	times[1].tv_usec = mt_nsec / 1000;
1779232153Smm
1780232153Smm	times[0].tv_sec = at;
1781232153Smm	times[0].tv_usec = at_nsec / 1000;
1782232153Smm
1783232153Smm#ifdef HAVE_LUTIMES
1784232153Smm	r = lutimes(pathname, times);
1785232153Smm#else
1786232153Smm	r = utimes(pathname, times);
1787232153Smm#endif
1788232153Smm	if (r < 0) {
1789232153Smm		failure_start(file, line, "Can't utimes %s\n", pathname);
1790232153Smm		failure_finish(NULL);
1791232153Smm		return (0);
1792232153Smm	}
1793232153Smm	return (1);
1794232153Smm#endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1795232153Smm}
1796232153Smm
1797238856Smm/* Set nodump, report failures. */
1798238856Smmint
1799238856Smmassertion_nodump(const char *file, int line, const char *pathname)
1800238856Smm{
1801238856Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1802238856Smm	int r;
1803238856Smm
1804238856Smm	assertion_count(file, line);
1805238856Smm	r = chflags(pathname, UF_NODUMP);
1806238856Smm	if (r < 0) {
1807238856Smm		failure_start(file, line, "Can't set nodump %s\n", pathname);
1808238856Smm		failure_finish(NULL);
1809238856Smm		return (0);
1810238856Smm	}
1811238856Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1812238856Smm	 && defined(EXT2_NODUMP_FL)
1813238856Smm	int fd, r, flags;
1814238856Smm
1815238856Smm	assertion_count(file, line);
1816238856Smm	fd = open(pathname, O_RDONLY | O_NONBLOCK);
1817238856Smm	if (fd < 0) {
1818238856Smm		failure_start(file, line, "Can't open %s\n", pathname);
1819238856Smm		failure_finish(NULL);
1820238856Smm		return (0);
1821238856Smm	}
1822238856Smm	r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1823238856Smm	if (r < 0) {
1824238856Smm		failure_start(file, line, "Can't get flags %s\n", pathname);
1825238856Smm		failure_finish(NULL);
1826238856Smm		return (0);
1827238856Smm	}
1828238856Smm	flags |= EXT2_NODUMP_FL;
1829238856Smm	r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1830238856Smm	if (r < 0) {
1831238856Smm		failure_start(file, line, "Can't set nodump %s\n", pathname);
1832238856Smm		failure_finish(NULL);
1833238856Smm		return (0);
1834238856Smm	}
1835238856Smm	close(fd);
1836238856Smm#else
1837238856Smm	(void)pathname; /* UNUSED */
1838238856Smm	assertion_count(file, line);
1839238856Smm#endif
1840238856Smm	return (1);
1841238856Smm}
1842238856Smm
1843228753Smm/*
1844228753Smm *
1845228753Smm *  UTILITIES for use by tests.
1846228753Smm *
1847228753Smm */
1848228753Smm
1849228753Smm/*
1850228753Smm * Check whether platform supports symlinks.  This is intended
1851228753Smm * for tests to use in deciding whether to bother testing symlink
1852228753Smm * support; if the platform doesn't support symlinks, there's no point
1853228753Smm * in checking whether the program being tested can create them.
1854228753Smm *
1855228753Smm * Note that the first time this test is called, we actually go out to
1856228753Smm * disk to create and verify a symlink.  This is necessary because
1857228753Smm * symlink support is actually a property of a particular filesystem
1858228753Smm * and can thus vary between directories on a single system.  After
1859228753Smm * the first call, this returns the cached result from memory, so it's
1860228753Smm * safe to call it as often as you wish.
1861228753Smm */
1862228753Smmint
1863228753SmmcanSymlink(void)
1864228753Smm{
1865228753Smm	/* Remember the test result */
1866228753Smm	static int value = 0, tested = 0;
1867228753Smm	if (tested)
1868228753Smm		return (value);
1869228753Smm
1870228753Smm	++tested;
1871238856Smm	assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1872228753Smm	/* Note: Cygwin has its own symlink() emulation that does not
1873228753Smm	 * use the Win32 CreateSymbolicLink() function. */
1874228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1875228753Smm	value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1876228753Smm	    && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1877228753Smm#elif HAVE_SYMLINK
1878228753Smm	value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1879228753Smm	    && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1880228753Smm#endif
1881228753Smm	return (value);
1882228753Smm}
1883228753Smm
1884228753Smm/* Platform-dependent options for hiding the output of a subcommand. */
1885228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1886228753Smmstatic const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1887228753Smm#else
1888228753Smmstatic const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1889228753Smm#endif
1890248616Smm/*
1891248616Smm * Can this platform run the bzip2 program?
1892248616Smm */
1893228753Smmint
1894248616SmmcanBzip2(void)
1895248616Smm{
1896248616Smm	static int tested = 0, value = 0;
1897248616Smm	if (!tested) {
1898248616Smm		tested = 1;
1899248616Smm		if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
1900248616Smm			value = 1;
1901248616Smm	}
1902248616Smm	return (value);
1903248616Smm}
1904248616Smm
1905248616Smm/*
1906248616Smm * Can this platform run the grzip program?
1907248616Smm */
1908248616Smmint
1909248616SmmcanGrzip(void)
1910248616Smm{
1911248616Smm	static int tested = 0, value = 0;
1912248616Smm	if (!tested) {
1913248616Smm		tested = 1;
1914248616Smm		if (systemf("grzip -V %s", redirectArgs) == 0)
1915248616Smm			value = 1;
1916248616Smm	}
1917248616Smm	return (value);
1918248616Smm}
1919248616Smm
1920248616Smm/*
1921248616Smm * Can this platform run the gzip program?
1922248616Smm */
1923248616Smmint
1924228753SmmcanGzip(void)
1925228753Smm{
1926228753Smm	static int tested = 0, value = 0;
1927228753Smm	if (!tested) {
1928228753Smm		tested = 1;
1929228753Smm		if (systemf("gzip -V %s", redirectArgs) == 0)
1930228753Smm			value = 1;
1931228753Smm	}
1932228753Smm	return (value);
1933228753Smm}
1934228753Smm
1935228753Smm/*
1936248616Smm * Can this platform run the lrzip program?
1937228753Smm */
1938228753Smmint
1939248616SmmcanRunCommand(const char *cmd)
1940228753Smm{
1941248616Smm  static int tested = 0, value = 0;
1942248616Smm  if (!tested) {
1943248616Smm    tested = 1;
1944248616Smm    if (systemf("%s %s", cmd, redirectArgs) == 0)
1945248616Smm      value = 1;
1946248616Smm  }
1947248616Smm  return (value);
1948248616Smm}
1949248616Smm
1950248616Smmint
1951248616SmmcanLrzip(void)
1952248616Smm{
1953228753Smm	static int tested = 0, value = 0;
1954228753Smm	if (!tested) {
1955228753Smm		tested = 1;
1956248616Smm		if (systemf("lrzip -V %s", redirectArgs) == 0)
1957228753Smm			value = 1;
1958228753Smm	}
1959228753Smm	return (value);
1960228753Smm}
1961228753Smm
1962228753Smm/*
1963248616Smm * Can this platform run the lzip program?
1964248616Smm */
1965248616Smmint
1966248616SmmcanLzip(void)
1967248616Smm{
1968248616Smm	static int tested = 0, value = 0;
1969248616Smm	if (!tested) {
1970248616Smm		tested = 1;
1971248616Smm		if (systemf("lzip -V %s", redirectArgs) == 0)
1972248616Smm			value = 1;
1973248616Smm	}
1974248616Smm	return (value);
1975248616Smm}
1976248616Smm
1977248616Smm/*
1978248616Smm * Can this platform run the lzma program?
1979248616Smm */
1980248616Smmint
1981248616SmmcanLzma(void)
1982248616Smm{
1983248616Smm	static int tested = 0, value = 0;
1984248616Smm	if (!tested) {
1985248616Smm		tested = 1;
1986248616Smm		if (systemf("lzma -V %s", redirectArgs) == 0)
1987248616Smm			value = 1;
1988248616Smm	}
1989248616Smm	return (value);
1990248616Smm}
1991248616Smm
1992248616Smm/*
1993248616Smm * Can this platform run the lzop program?
1994248616Smm */
1995248616Smmint
1996248616SmmcanLzop(void)
1997248616Smm{
1998248616Smm	static int tested = 0, value = 0;
1999248616Smm	if (!tested) {
2000248616Smm		tested = 1;
2001248616Smm		if (systemf("lzop -V %s", redirectArgs) == 0)
2002248616Smm			value = 1;
2003248616Smm	}
2004248616Smm	return (value);
2005248616Smm}
2006248616Smm
2007248616Smm/*
2008248616Smm * Can this platform run the xz program?
2009248616Smm */
2010248616Smmint
2011248616SmmcanXz(void)
2012248616Smm{
2013248616Smm	static int tested = 0, value = 0;
2014248616Smm	if (!tested) {
2015248616Smm		tested = 1;
2016248616Smm		if (systemf("xz -V %s", redirectArgs) == 0)
2017248616Smm			value = 1;
2018248616Smm	}
2019248616Smm	return (value);
2020248616Smm}
2021248616Smm
2022248616Smm/*
2023238856Smm * Can this filesystem handle nodump flags.
2024238856Smm */
2025238856Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2026238856Smm
2027238856Smmint
2028238856SmmcanNodump(void)
2029238856Smm{
2030238856Smm	const char *path = "cannodumptest";
2031238856Smm	struct stat sb;
2032238856Smm
2033238856Smm	assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2034238856Smm	if (chflags(path, UF_NODUMP) < 0)
2035238856Smm		return (0);
2036238856Smm	if (stat(path, &sb) < 0)
2037238856Smm		return (0);
2038238856Smm	if (sb.st_flags & UF_NODUMP)
2039238856Smm		return (1);
2040238856Smm	return (0);
2041238856Smm}
2042238856Smm
2043238856Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2044238856Smm	 && defined(EXT2_NODUMP_FL)
2045238856Smm
2046238856Smmint
2047238856SmmcanNodump(void)
2048238856Smm{
2049238856Smm	const char *path = "cannodumptest";
2050238856Smm	int fd, r, flags;
2051238856Smm
2052238856Smm	assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2053238856Smm	fd = open(path, O_RDONLY | O_NONBLOCK);
2054238856Smm	if (fd < 0)
2055238856Smm		return (0);
2056238856Smm	r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2057238856Smm	if (r < 0)
2058238856Smm		return (0);
2059238856Smm	flags |= EXT2_NODUMP_FL;
2060238856Smm	r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2061238856Smm	if (r < 0)
2062238856Smm		return (0);
2063238856Smm	close(fd);
2064238856Smm	fd = open(path, O_RDONLY | O_NONBLOCK);
2065238856Smm	if (fd < 0)
2066238856Smm		return (0);
2067238856Smm	r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2068238856Smm	if (r < 0)
2069238856Smm		return (0);
2070238856Smm	close(fd);
2071238856Smm	if (flags & EXT2_NODUMP_FL)
2072238856Smm		return (1);
2073238856Smm	return (0);
2074238856Smm}
2075238856Smm
2076238856Smm#else
2077238856Smm
2078238856Smmint
2079238856SmmcanNodump()
2080238856Smm{
2081238856Smm	return (0);
2082238856Smm}
2083238856Smm
2084238856Smm#endif
2085238856Smm
2086238856Smm/*
2087228753Smm * Sleep as needed; useful for verifying disk timestamp changes by
2088228753Smm * ensuring that the wall-clock time has actually changed before we
2089228753Smm * go back to re-read something from disk.
2090228753Smm */
2091228753Smmvoid
2092228753SmmsleepUntilAfter(time_t t)
2093228753Smm{
2094228753Smm	while (t >= time(NULL))
2095228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2096228753Smm		Sleep(500);
2097228753Smm#else
2098228753Smm		sleep(1);
2099228753Smm#endif
2100228753Smm}
2101228753Smm
2102228753Smm/*
2103228753Smm * Call standard system() call, but build up the command line using
2104228753Smm * sprintf() conventions.
2105228753Smm */
2106228753Smmint
2107228753Smmsystemf(const char *fmt, ...)
2108228753Smm{
2109228753Smm	char buff[8192];
2110228753Smm	va_list ap;
2111228753Smm	int r;
2112228753Smm
2113228753Smm	va_start(ap, fmt);
2114228753Smm	vsprintf(buff, fmt, ap);
2115228753Smm	if (verbosity > VERBOSITY_FULL)
2116228753Smm		logprintf("Cmd: %s\n", buff);
2117228753Smm	r = system(buff);
2118228753Smm	va_end(ap);
2119228753Smm	return (r);
2120228753Smm}
2121228753Smm
2122228753Smm/*
2123228753Smm * Slurp a file into memory for ease of comparison and testing.
2124228753Smm * Returns size of file in 'sizep' if non-NULL, null-terminates
2125228753Smm * data in memory for ease of use.
2126228753Smm */
2127228753Smmchar *
2128228753Smmslurpfile(size_t * sizep, const char *fmt, ...)
2129228753Smm{
2130228753Smm	char filename[8192];
2131228753Smm	struct stat st;
2132228753Smm	va_list ap;
2133228753Smm	char *p;
2134228753Smm	ssize_t bytes_read;
2135228753Smm	FILE *f;
2136228753Smm	int r;
2137228753Smm
2138228753Smm	va_start(ap, fmt);
2139228753Smm	vsprintf(filename, fmt, ap);
2140228753Smm	va_end(ap);
2141228753Smm
2142228753Smm	f = fopen(filename, "rb");
2143228753Smm	if (f == NULL) {
2144228753Smm		/* Note: No error; non-existent file is okay here. */
2145228753Smm		return (NULL);
2146228753Smm	}
2147228753Smm	r = fstat(fileno(f), &st);
2148228753Smm	if (r != 0) {
2149228753Smm		logprintf("Can't stat file %s\n", filename);
2150228753Smm		fclose(f);
2151228753Smm		return (NULL);
2152228753Smm	}
2153228753Smm	p = malloc((size_t)st.st_size + 1);
2154228753Smm	if (p == NULL) {
2155228753Smm		logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2156228753Smm		    (long int)st.st_size, filename);
2157228753Smm		fclose(f);
2158228753Smm		return (NULL);
2159228753Smm	}
2160228753Smm	bytes_read = fread(p, 1, (size_t)st.st_size, f);
2161228753Smm	if (bytes_read < st.st_size) {
2162228753Smm		logprintf("Can't read file %s\n", filename);
2163228753Smm		fclose(f);
2164228753Smm		free(p);
2165228753Smm		return (NULL);
2166228753Smm	}
2167228753Smm	p[st.st_size] = '\0';
2168228753Smm	if (sizep != NULL)
2169228753Smm		*sizep = (size_t)st.st_size;
2170228753Smm	fclose(f);
2171228753Smm	return (p);
2172228753Smm}
2173228753Smm
2174228753Smm/* Read a uuencoded file from the reference directory, decode, and
2175228753Smm * write the result into the current directory. */
2176228753Smm#define	UUDECODE(c) (((c) - 0x20) & 0x3f)
2177228753Smmvoid
2178228753Smmextract_reference_file(const char *name)
2179228753Smm{
2180228753Smm	char buff[1024];
2181228753Smm	FILE *in, *out;
2182228753Smm
2183228753Smm	sprintf(buff, "%s/%s.uu", refdir, name);
2184228753Smm	in = fopen(buff, "r");
2185228753Smm	failure("Couldn't open reference file %s", buff);
2186228753Smm	assert(in != NULL);
2187228753Smm	if (in == NULL)
2188228753Smm		return;
2189228753Smm	/* Read up to and including the 'begin' line. */
2190228753Smm	for (;;) {
2191228753Smm		if (fgets(buff, sizeof(buff), in) == NULL) {
2192228753Smm			/* TODO: This is a failure. */
2193228753Smm			return;
2194228753Smm		}
2195228753Smm		if (memcmp(buff, "begin ", 6) == 0)
2196228753Smm			break;
2197228753Smm	}
2198228753Smm	/* Now, decode the rest and write it. */
2199228753Smm	/* Not a lot of error checking here; the input better be right. */
2200228753Smm	out = fopen(name, "wb");
2201228753Smm	while (fgets(buff, sizeof(buff), in) != NULL) {
2202228753Smm		char *p = buff;
2203228753Smm		int bytes;
2204228753Smm
2205228753Smm		if (memcmp(buff, "end", 3) == 0)
2206228753Smm			break;
2207228753Smm
2208228753Smm		bytes = UUDECODE(*p++);
2209228753Smm		while (bytes > 0) {
2210228753Smm			int n = 0;
2211228753Smm			/* Write out 1-3 bytes from that. */
2212228753Smm			if (bytes > 0) {
2213228753Smm				n = UUDECODE(*p++) << 18;
2214228753Smm				n |= UUDECODE(*p++) << 12;
2215228753Smm				fputc(n >> 16, out);
2216228753Smm				--bytes;
2217228753Smm			}
2218228753Smm			if (bytes > 0) {
2219228753Smm				n |= UUDECODE(*p++) << 6;
2220228753Smm				fputc((n >> 8) & 0xFF, out);
2221228753Smm				--bytes;
2222228753Smm			}
2223228753Smm			if (bytes > 0) {
2224228753Smm				n |= UUDECODE(*p++);
2225228753Smm				fputc(n & 0xFF, out);
2226228753Smm				--bytes;
2227228753Smm			}
2228228753Smm		}
2229228753Smm	}
2230228753Smm	fclose(out);
2231228753Smm	fclose(in);
2232228753Smm}
2233228753Smm
2234232153Smmint
2235232153Smmis_LargeInode(const char *file)
2236232153Smm{
2237232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2238232153Smm	BY_HANDLE_FILE_INFORMATION bhfi;
2239232153Smm	int r;
2240232153Smm
2241232153Smm	r = my_GetFileInformationByName(file, &bhfi);
2242232153Smm	if (r != 0)
2243232153Smm		return (0);
2244232153Smm	return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2245232153Smm#else
2246232153Smm	struct stat st;
2247232153Smm	int64_t ino;
2248232153Smm
2249232153Smm	if (stat(file, &st) < 0)
2250232153Smm		return (0);
2251232153Smm	ino = (int64_t)st.st_ino;
2252232153Smm	return (ino > 0xffffffff);
2253232153Smm#endif
2254232153Smm}
2255248616Smm
2256248616Smmvoid
2257248616Smmextract_reference_files(const char **names)
2258248616Smm{
2259248616Smm	while (names && *names)
2260248616Smm		extract_reference_file(*names++);
2261248616Smm}
2262248616Smm
2263228753Smm/*
2264228753Smm *
2265228753Smm * TEST management
2266228753Smm *
2267228753Smm */
2268228753Smm
2269228753Smm/*
2270228753Smm * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2271228753Smm * a line like
2272228753Smm *      DEFINE_TEST(test_function)
2273228753Smm * for each test.
2274228753Smm */
2275228753Smm
2276228753Smm/* Use "list.h" to declare all of the test functions. */
2277228753Smm#undef DEFINE_TEST
2278228753Smm#define	DEFINE_TEST(name) void name(void);
2279228753Smm#include "list.h"
2280228753Smm
2281228753Smm/* Use "list.h" to create a list of all tests (functions and names). */
2282228753Smm#undef DEFINE_TEST
2283228753Smm#define	DEFINE_TEST(n) { n, #n, 0 },
2284248616Smmstruct test_list_t tests[] = {
2285228753Smm	#include "list.h"
2286228753Smm};
2287228753Smm
2288228753Smm/*
2289228753Smm * Summarize repeated failures in the just-completed test.
2290228753Smm */
2291228753Smmstatic void
2292232153Smmtest_summarize(int failed)
2293228753Smm{
2294228753Smm	unsigned int i;
2295228753Smm
2296228753Smm	switch (verbosity) {
2297228753Smm	case VERBOSITY_SUMMARY_ONLY:
2298228753Smm		printf(failed ? "E" : ".");
2299228753Smm		fflush(stdout);
2300228753Smm		break;
2301228753Smm	case VERBOSITY_PASSFAIL:
2302228753Smm		printf(failed ? "FAIL\n" : "ok\n");
2303228753Smm		break;
2304228753Smm	}
2305228753Smm
2306228753Smm	log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2307228753Smm
2308228753Smm	for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2309228753Smm		if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2310228753Smm			logprintf("%s:%d: Summary: Failed %d times\n",
2311232153Smm			    failed_filename, i, failed_lines[i].count);
2312228753Smm	}
2313228753Smm	/* Clear the failure history for the next file. */
2314232153Smm	failed_filename = NULL;
2315228753Smm	memset(failed_lines, 0, sizeof(failed_lines));
2316228753Smm}
2317228753Smm
2318228753Smm/*
2319228753Smm * Actually run a single test, with appropriate setup and cleanup.
2320228753Smm */
2321228753Smmstatic int
2322228753Smmtest_run(int i, const char *tmpdir)
2323228753Smm{
2324232153Smm	char workdir[1024];
2325228753Smm	char logfilename[64];
2326228753Smm	int failures_before = failures;
2327228753Smm	int oldumask;
2328228753Smm
2329228753Smm	switch (verbosity) {
2330228753Smm	case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2331228753Smm		break;
2332228753Smm	case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2333228753Smm		printf("%3d: %-50s", i, tests[i].name);
2334228753Smm		fflush(stdout);
2335228753Smm		break;
2336228753Smm	default: /* Title of test, details will follow */
2337228753Smm		printf("%3d: %s\n", i, tests[i].name);
2338228753Smm	}
2339228753Smm
2340228753Smm	/* Chdir to the top-level work directory. */
2341228753Smm	if (!assertChdir(tmpdir)) {
2342228753Smm		fprintf(stderr,
2343228753Smm		    "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2344228753Smm		exit(1);
2345228753Smm	}
2346228753Smm	/* Create a log file for this test. */
2347228753Smm	sprintf(logfilename, "%s.log", tests[i].name);
2348228753Smm	logfile = fopen(logfilename, "w");
2349228753Smm	fprintf(logfile, "%s\n\n", tests[i].name);
2350228753Smm	/* Chdir() to a work dir for this specific test. */
2351232153Smm	snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2352232153Smm	testworkdir = workdir;
2353232153Smm	if (!assertMakeDir(testworkdir, 0755)
2354232153Smm	    || !assertChdir(testworkdir)) {
2355228753Smm		fprintf(stderr,
2356232153Smm		    "ERROR: Can't chdir to work dir %s\n", testworkdir);
2357228753Smm		exit(1);
2358228753Smm	}
2359228753Smm	/* Explicitly reset the locale before each test. */
2360228753Smm	setlocale(LC_ALL, "C");
2361228753Smm	/* Record the umask before we run the test. */
2362228753Smm	umask(oldumask = umask(0));
2363228753Smm	/*
2364228753Smm	 * Run the actual test.
2365228753Smm	 */
2366228753Smm	(*tests[i].func)();
2367228753Smm	/*
2368228753Smm	 * Clean up and report afterwards.
2369228753Smm	 */
2370232153Smm	testworkdir = NULL;
2371228753Smm	/* Restore umask */
2372228753Smm	umask(oldumask);
2373228753Smm	/* Reset locale. */
2374228753Smm	setlocale(LC_ALL, "C");
2375228753Smm	/* Reset directory. */
2376228753Smm	if (!assertChdir(tmpdir)) {
2377228753Smm		fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2378228753Smm		    tmpdir);
2379228753Smm		exit(1);
2380228753Smm	}
2381228753Smm	/* Report per-test summaries. */
2382228753Smm	tests[i].failures = failures - failures_before;
2383232153Smm	test_summarize(tests[i].failures);
2384228753Smm	/* Close the per-test log file. */
2385228753Smm	fclose(logfile);
2386228753Smm	logfile = NULL;
2387228753Smm	/* If there were no failures, we can remove the work dir and logfile. */
2388228753Smm	if (tests[i].failures == 0) {
2389228753Smm		if (!keep_temp_files && assertChdir(tmpdir)) {
2390228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2391228753Smm			/* Make sure not to leave empty directories.
2392228753Smm			 * Sometimes a processing of closing files used by tests
2393228753Smm			 * is not done, then rmdir will be failed and it will
2394228753Smm			 * leave a empty test directory. So we should wait a few
2395228753Smm			 * seconds and retry rmdir. */
2396228753Smm			int r, t;
2397228753Smm			for (t = 0; t < 10; t++) {
2398228753Smm				if (t > 0)
2399228753Smm					Sleep(1000);
2400228753Smm				r = systemf("rmdir /S /Q %s", tests[i].name);
2401228753Smm				if (r == 0)
2402228753Smm					break;
2403228753Smm			}
2404228753Smm			systemf("del %s", logfilename);
2405228753Smm#else
2406228753Smm			systemf("rm -rf %s", tests[i].name);
2407228753Smm			systemf("rm %s", logfilename);
2408228753Smm#endif
2409228753Smm		}
2410228753Smm	}
2411228753Smm	/* Return appropriate status. */
2412228753Smm	return (tests[i].failures);
2413228753Smm}
2414228753Smm
2415228753Smm/*
2416228753Smm *
2417228753Smm *
2418228753Smm * MAIN and support routines.
2419228753Smm *
2420228753Smm *
2421228753Smm */
2422228753Smm
2423228753Smmstatic void
2424228753Smmusage(const char *program)
2425228753Smm{
2426228753Smm	static const int limit = sizeof(tests) / sizeof(tests[0]);
2427228753Smm	int i;
2428228753Smm
2429228753Smm	printf("Usage: %s [options] <test> <test> ...\n", program);
2430228753Smm	printf("Default is to run all tests.\n");
2431228753Smm	printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2432228753Smm	printf("Options:\n");
2433228753Smm	printf("  -d  Dump core after any failure, for debugging.\n");
2434228753Smm	printf("  -k  Keep all temp files.\n");
2435228753Smm	printf("      Default: temp files for successful tests deleted.\n");
2436228753Smm#ifdef PROGRAM
2437228753Smm	printf("  -p <path>  Path to executable to be tested.\n");
2438228753Smm	printf("      Default: path taken from " ENVBASE " environment variable.\n");
2439228753Smm#endif
2440228753Smm	printf("  -q  Quiet.\n");
2441228753Smm	printf("  -r <dir>   Path to dir containing reference files.\n");
2442228753Smm	printf("      Default: Current directory.\n");
2443232153Smm	printf("  -u  Keep running specifies tests until one fails.\n");
2444228753Smm	printf("  -v  Verbose.\n");
2445228753Smm	printf("Available tests:\n");
2446228753Smm	for (i = 0; i < limit; i++)
2447228753Smm		printf("  %d: %s\n", i, tests[i].name);
2448228753Smm	exit(1);
2449228753Smm}
2450228753Smm
2451228753Smmstatic char *
2452228753Smmget_refdir(const char *d)
2453228753Smm{
2454228753Smm	char tried[512] = { '\0' };
2455228753Smm	char buff[128];
2456228753Smm	char *pwd, *p;
2457228753Smm
2458228753Smm	/* If a dir was specified, try that */
2459228753Smm	if (d != NULL) {
2460228753Smm		pwd = NULL;
2461228753Smm		snprintf(buff, sizeof(buff), "%s", d);
2462228753Smm		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2463228753Smm		if (p != NULL) goto success;
2464228753Smm		strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2465228753Smm		strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2466228753Smm		goto failure;
2467228753Smm	}
2468228753Smm
2469228753Smm	/* Get the current dir. */
2470232153Smm#ifdef PATH_MAX
2471232153Smm	pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2472232153Smm#else
2473228753Smm	pwd = getcwd(NULL, 0);
2474232153Smm#endif
2475228753Smm	while (pwd[strlen(pwd) - 1] == '\n')
2476228753Smm		pwd[strlen(pwd) - 1] = '\0';
2477228753Smm
2478228753Smm	/* Look for a known file. */
2479228753Smm	snprintf(buff, sizeof(buff), "%s", pwd);
2480228753Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2481228753Smm	if (p != NULL) goto success;
2482228753Smm	strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2483228753Smm	strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2484228753Smm
2485228753Smm	snprintf(buff, sizeof(buff), "%s/test", pwd);
2486228753Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2487228753Smm	if (p != NULL) goto success;
2488228753Smm	strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2489228753Smm	strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2490228753Smm
2491228753Smm#if defined(LIBRARY)
2492228753Smm	snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
2493228753Smm#else
2494228753Smm	snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
2495228753Smm#endif
2496228753Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2497228753Smm	if (p != NULL) goto success;
2498228753Smm	strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2499228753Smm	strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2500228753Smm
2501232153Smm#if defined(PROGRAM_ALIAS)
2502232153Smm	snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
2503232153Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2504232153Smm	if (p != NULL) goto success;
2505232153Smm	strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2506232153Smm	strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2507232153Smm#endif
2508232153Smm
2509228753Smm	if (memcmp(pwd, "/usr/obj", 8) == 0) {
2510228753Smm		snprintf(buff, sizeof(buff), "%s", pwd + 8);
2511228753Smm		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2512228753Smm		if (p != NULL) goto success;
2513228753Smm		strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2514228753Smm		strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2515228753Smm
2516228753Smm		snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
2517228753Smm		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2518228753Smm		if (p != NULL) goto success;
2519228753Smm		strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
2520228753Smm		strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
2521228753Smm	}
2522228753Smm
2523228753Smmfailure:
2524228753Smm	printf("Unable to locate known reference file %s\n", KNOWNREF);
2525228753Smm	printf("  Checked following directories:\n%s\n", tried);
2526228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2527228753Smm	DebugBreak();
2528228753Smm#endif
2529228753Smm	exit(1);
2530228753Smm
2531228753Smmsuccess:
2532228753Smm	free(p);
2533228753Smm	free(pwd);
2534228753Smm	return strdup(buff);
2535228753Smm}
2536228753Smm
2537228753Smmint
2538228753Smmmain(int argc, char **argv)
2539228753Smm{
2540228753Smm	static const int limit = sizeof(tests) / sizeof(tests[0]);
2541238856Smm	int test_set[sizeof(tests) / sizeof(tests[0])];
2542238856Smm	int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2543228753Smm	time_t now;
2544228753Smm	char *refdir_alloc = NULL;
2545228753Smm	const char *progname;
2546232153Smm	char **saved_argv;
2547228753Smm	const char *tmp, *option_arg, *p;
2548238856Smm	char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2549228753Smm	char tmpdir_timestamp[256];
2550228753Smm
2551228753Smm	(void)argc; /* UNUSED */
2552228753Smm
2553232153Smm	/* Get the current dir. */
2554232153Smm#ifdef PATH_MAX
2555232153Smm	pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2556232153Smm#else
2557232153Smm	pwd = getcwd(NULL, 0);
2558232153Smm#endif
2559232153Smm	while (pwd[strlen(pwd) - 1] == '\n')
2560232153Smm		pwd[strlen(pwd) - 1] = '\0';
2561232153Smm
2562228753Smm#if defined(HAVE__CrtSetReportMode)
2563228753Smm	/* To stop to run the default invalid parameter handler. */
2564228753Smm	_set_invalid_parameter_handler(invalid_parameter_handler);
2565228753Smm	/* Disable annoying assertion message box. */
2566228753Smm	_CrtSetReportMode(_CRT_ASSERT, 0);
2567228753Smm#endif
2568228753Smm
2569228753Smm	/*
2570228753Smm	 * Name of this program, used to build root of our temp directory
2571228753Smm	 * tree.
2572228753Smm	 */
2573228753Smm	progname = p = argv[0];
2574232153Smm	if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2575232153Smm	{
2576232153Smm		fprintf(stderr, "ERROR: Out of memory.");
2577232153Smm		exit(1);
2578232153Smm	}
2579232153Smm	strcpy(testprogdir, progname);
2580228753Smm	while (*p != '\0') {
2581228753Smm		/* Support \ or / dir separators for Windows compat. */
2582228753Smm		if (*p == '/' || *p == '\\')
2583232153Smm		{
2584228753Smm			progname = p + 1;
2585232153Smm			i = j;
2586232153Smm		}
2587228753Smm		++p;
2588232153Smm		j++;
2589228753Smm	}
2590232153Smm	testprogdir[i] = '\0';
2591232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2592232153Smm	if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2593232153Smm	    !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2594232153Smm	       (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2595232153Smm		testprogdir[1] == ':' &&
2596232153Smm		(testprogdir[2] == '/' || testprogdir[2] == '\\')))
2597232153Smm#else
2598232153Smm	if (testprogdir[0] != '/')
2599232153Smm#endif
2600232153Smm	{
2601232153Smm		/* Fixup path for relative directories. */
2602232153Smm		if ((testprogdir = (char *)realloc(testprogdir,
2603232153Smm			strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2604232153Smm		{
2605232153Smm			fprintf(stderr, "ERROR: Out of memory.");
2606232153Smm			exit(1);
2607232153Smm		}
2608232153Smm		memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2609232153Smm		    strlen(testprogdir));
2610232153Smm		memcpy(testprogdir, pwd, strlen(pwd));
2611232153Smm		testprogdir[strlen(pwd)] = '/';
2612232153Smm	}
2613228753Smm
2614228753Smm#ifdef PROGRAM
2615228753Smm	/* Get the target program from environment, if available. */
2616228753Smm	testprogfile = getenv(ENVBASE);
2617228753Smm#endif
2618228753Smm
2619228753Smm	if (getenv("TMPDIR") != NULL)
2620228753Smm		tmp = getenv("TMPDIR");
2621228753Smm	else if (getenv("TMP") != NULL)
2622228753Smm		tmp = getenv("TMP");
2623228753Smm	else if (getenv("TEMP") != NULL)
2624228753Smm		tmp = getenv("TEMP");
2625228753Smm	else if (getenv("TEMPDIR") != NULL)
2626228753Smm		tmp = getenv("TEMPDIR");
2627228753Smm	else
2628228753Smm		tmp = "/tmp";
2629228753Smm
2630228753Smm	/* Allow -d to be controlled through the environment. */
2631228753Smm	if (getenv(ENVBASE "_DEBUG") != NULL)
2632228753Smm		dump_on_failure = 1;
2633228753Smm
2634238856Smm	/* Allow -v to be controlled through the environment. */
2635238856Smm	if (getenv("_VERBOSITY_LEVEL") != NULL)
2636238856Smm	{
2637238856Smm		vlevel = getenv("_VERBOSITY_LEVEL");
2638238856Smm		verbosity = atoi(vlevel);
2639238856Smm		if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2640238856Smm		{
2641238856Smm			/* Unsupported verbosity levels are silently ignored */
2642238856Smm			vlevel = NULL;
2643238856Smm			verbosity = VERBOSITY_PASSFAIL;
2644238856Smm		}
2645238856Smm	}
2646238856Smm
2647228753Smm	/* Get the directory holding test files from environment. */
2648228753Smm	refdir = getenv(ENVBASE "_TEST_FILES");
2649228753Smm
2650228753Smm	/*
2651228753Smm	 * Parse options, without using getopt(), which isn't available
2652228753Smm	 * on all platforms.
2653228753Smm	 */
2654228753Smm	++argv; /* Skip program name */
2655228753Smm	while (*argv != NULL) {
2656228753Smm		if (**argv != '-')
2657228753Smm			break;
2658228753Smm		p = *argv++;
2659228753Smm		++p; /* Skip '-' */
2660228753Smm		while (*p != '\0') {
2661228753Smm			option = *p++;
2662228753Smm			option_arg = NULL;
2663228753Smm			/* If 'opt' takes an argument, parse that. */
2664228753Smm			if (option == 'p' || option == 'r') {
2665228753Smm				if (*p != '\0')
2666228753Smm					option_arg = p;
2667228753Smm				else if (*argv == NULL) {
2668228753Smm					fprintf(stderr,
2669228753Smm					    "Option -%c requires argument.\n",
2670228753Smm					    option);
2671228753Smm					usage(progname);
2672228753Smm				} else
2673228753Smm					option_arg = *argv++;
2674228753Smm				p = ""; /* End of this option word. */
2675228753Smm			}
2676228753Smm
2677228753Smm			/* Now, handle the option. */
2678228753Smm			switch (option) {
2679228753Smm			case 'd':
2680228753Smm				dump_on_failure = 1;
2681228753Smm				break;
2682228753Smm			case 'k':
2683228753Smm				keep_temp_files = 1;
2684228753Smm				break;
2685228753Smm			case 'p':
2686228753Smm#ifdef PROGRAM
2687228753Smm				testprogfile = option_arg;
2688228753Smm#else
2689232153Smm				fprintf(stderr, "-p option not permitted\n");
2690228753Smm				usage(progname);
2691228753Smm#endif
2692228753Smm				break;
2693228753Smm			case 'q':
2694238856Smm				if (!vlevel)
2695238856Smm					verbosity--;
2696228753Smm				break;
2697228753Smm			case 'r':
2698228753Smm				refdir = option_arg;
2699228753Smm				break;
2700232153Smm			case 'u':
2701232153Smm				until_failure++;
2702232153Smm				break;
2703228753Smm			case 'v':
2704238856Smm				if (!vlevel)
2705238856Smm					verbosity++;
2706228753Smm				break;
2707228753Smm			default:
2708232153Smm				fprintf(stderr, "Unrecognized option '%c'\n",
2709232153Smm				    option);
2710228753Smm				usage(progname);
2711228753Smm			}
2712228753Smm		}
2713228753Smm	}
2714228753Smm
2715228753Smm	/*
2716228753Smm	 * Sanity-check that our options make sense.
2717228753Smm	 */
2718228753Smm#ifdef PROGRAM
2719228753Smm	if (testprogfile == NULL)
2720228753Smm	{
2721232153Smm		if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
2722232153Smm			strlen(PROGRAM) + 1)) == NULL)
2723232153Smm		{
2724232153Smm			fprintf(stderr, "ERROR: Out of memory.");
2725232153Smm			exit(1);
2726232153Smm		}
2727232153Smm		strcpy(tmp2, testprogdir);
2728232153Smm		strcat(tmp2, "/");
2729232153Smm		strcat(tmp2, PROGRAM);
2730232153Smm		testprogfile = tmp2;
2731232153Smm	}
2732232153Smm
2733232153Smm	{
2734228753Smm		char *testprg;
2735228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2736228753Smm		/* Command.com sometimes rejects '/' separators. */
2737228753Smm		testprg = strdup(testprogfile);
2738228753Smm		for (i = 0; testprg[i] != '\0'; i++) {
2739228753Smm			if (testprg[i] == '/')
2740228753Smm				testprg[i] = '\\';
2741228753Smm		}
2742228753Smm		testprogfile = testprg;
2743228753Smm#endif
2744228753Smm		/* Quote the name that gets put into shell command lines. */
2745228753Smm		testprg = malloc(strlen(testprogfile) + 3);
2746228753Smm		strcpy(testprg, "\"");
2747228753Smm		strcat(testprg, testprogfile);
2748228753Smm		strcat(testprg, "\"");
2749228753Smm		testprog = testprg;
2750228753Smm	}
2751228753Smm#endif
2752228753Smm
2753232153Smm#if !defined(_WIN32) && defined(SIGPIPE)
2754232153Smm	{   /* Ignore SIGPIPE signals */
2755232153Smm		struct sigaction sa;
2756232153Smm		sa.sa_handler = SIG_IGN;
2757232153Smm		sigemptyset(&sa.sa_mask);
2758232153Smm		sa.sa_flags = 0;
2759232153Smm		sigaction(SIGPIPE, &sa, NULL);
2760232153Smm	}
2761232153Smm#endif
2762232153Smm
2763228753Smm	/*
2764228753Smm	 * Create a temp directory for the following tests.
2765228753Smm	 * Include the time the tests started as part of the name,
2766228753Smm	 * to make it easier to track the results of multiple tests.
2767228753Smm	 */
2768228753Smm	now = time(NULL);
2769228753Smm	for (i = 0; ; i++) {
2770228753Smm		strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
2771228753Smm		    "%Y-%m-%dT%H.%M.%S",
2772228753Smm		    localtime(&now));
2773228753Smm		sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
2774228753Smm		    tmpdir_timestamp, i);
2775228753Smm		if (assertMakeDir(tmpdir,0755))
2776228753Smm			break;
2777228753Smm		if (i >= 999) {
2778228753Smm			fprintf(stderr,
2779228753Smm			    "ERROR: Unable to create temp directory %s\n",
2780228753Smm			    tmpdir);
2781228753Smm			exit(1);
2782228753Smm		}
2783228753Smm	}
2784228753Smm
2785228753Smm	/*
2786228753Smm	 * If the user didn't specify a directory for locating
2787228753Smm	 * reference files, try to find the reference files in
2788228753Smm	 * the "usual places."
2789228753Smm	 */
2790228753Smm	refdir = refdir_alloc = get_refdir(refdir);
2791228753Smm
2792228753Smm	/*
2793228753Smm	 * Banner with basic information.
2794228753Smm	 */
2795228753Smm	printf("\n");
2796228753Smm	printf("If tests fail or crash, details will be in:\n");
2797228753Smm	printf("   %s\n", tmpdir);
2798228753Smm	printf("\n");
2799228753Smm	if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2800228753Smm		printf("Reference files will be read from: %s\n", refdir);
2801228753Smm#ifdef PROGRAM
2802228753Smm		printf("Running tests on: %s\n", testprog);
2803228753Smm#endif
2804228753Smm		printf("Exercising: ");
2805228753Smm		fflush(stdout);
2806228753Smm		printf("%s\n", EXTRA_VERSION);
2807228753Smm	} else {
2808228753Smm		printf("Running ");
2809228753Smm		fflush(stdout);
2810228753Smm	}
2811228753Smm
2812228753Smm	/*
2813228753Smm	 * Run some or all of the individual tests.
2814228753Smm	 */
2815232153Smm	saved_argv = argv;
2816232153Smm	do {
2817232153Smm		argv = saved_argv;
2818238856Smm		do {
2819238856Smm			int test_num;
2820238856Smm
2821248616Smm			test_num = get_test_set(test_set, limit, *argv, tests);
2822238856Smm			if (test_num < 0) {
2823238856Smm				printf("*** INVALID Test %s\n", *argv);
2824238856Smm				free(refdir_alloc);
2825248616Smm				free(testprogdir);
2826238856Smm				usage(progname);
2827238856Smm				return (1);
2828238856Smm			}
2829238856Smm			for (i = 0; i < test_num; i++) {
2830232153Smm				tests_run++;
2831238856Smm				if (test_run(test_set[i], tmpdir)) {
2832232153Smm					tests_failed++;
2833232153Smm					if (until_failure)
2834232153Smm						goto finish;
2835228753Smm				}
2836232153Smm			}
2837238856Smm			if (*argv != NULL)
2838232153Smm				argv++;
2839238856Smm		} while (*argv != NULL);
2840232153Smm	} while (until_failure);
2841228753Smm
2842232153Smmfinish:
2843232153Smm	/* Must be freed after all tests run */
2844232153Smm	free(tmp2);
2845232153Smm	free(testprogdir);
2846232153Smm	free(pwd);
2847232153Smm
2848228753Smm	/*
2849228753Smm	 * Report summary statistics.
2850228753Smm	 */
2851228753Smm	if (verbosity > VERBOSITY_SUMMARY_ONLY) {
2852228753Smm		printf("\n");
2853228753Smm		printf("Totals:\n");
2854228753Smm		printf("  Tests run:         %8d\n", tests_run);
2855228753Smm		printf("  Tests failed:      %8d\n", tests_failed);
2856228753Smm		printf("  Assertions checked:%8d\n", assertions);
2857228753Smm		printf("  Assertions failed: %8d\n", failures);
2858228753Smm		printf("  Skips reported:    %8d\n", skips);
2859228753Smm	}
2860228753Smm	if (failures) {
2861228753Smm		printf("\n");
2862228753Smm		printf("Failing tests:\n");
2863228753Smm		for (i = 0; i < limit; ++i) {
2864228753Smm			if (tests[i].failures)
2865228753Smm				printf("  %d: %s (%d failures)\n", i,
2866228753Smm				    tests[i].name, tests[i].failures);
2867228753Smm		}
2868228753Smm		printf("\n");
2869228753Smm		printf("Details for failing tests: %s\n", tmpdir);
2870228753Smm		printf("\n");
2871228753Smm	} else {
2872228753Smm		if (verbosity == VERBOSITY_SUMMARY_ONLY)
2873228753Smm			printf("\n");
2874228753Smm		printf("%d tests passed, no failures\n", tests_run);
2875228753Smm	}
2876228753Smm
2877228753Smm	free(refdir_alloc);
2878228753Smm
2879228753Smm	/* If the final tmpdir is empty, we can remove it. */
2880228753Smm	/* This should be the usual case when all tests succeed. */
2881228753Smm	assertChdir("..");
2882228753Smm	rmdir(tmpdir);
2883228753Smm
2884228753Smm	return (tests_failed ? 1 : 0);
2885228753Smm}
2886