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: stable/10/contrib/libarchive/libarchive/test/main.c 313571 2017-02-11 00:56:18Z mm $");
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))
70302001Smm#define	EXTRA_VERSION	archive_version_details()
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
131307798Smmmode_t umasked(mode_t expected_mode)
132307798Smm{
133307798Smm	mode_t mode = umask(0);
134307798Smm	umask(mode);
135307798Smm	return expected_mode & ~mode;
136307798Smm}
137307798Smm
138302001Smm/* Path to working directory for current test */
139302001Smmconst char *testworkdir;
140302001Smm#ifdef PROGRAM
141302001Smm/* Pathname of exe to be tested. */
142302001Smmconst char *testprogfile;
143302001Smm/* Name of exe to use in printf-formatted command strings. */
144302001Smm/* On Windows, this includes leading/trailing quotes. */
145302001Smmconst char *testprog;
146302001Smm#endif
147302001Smm
148228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
149238856Smmstatic void	*GetFunctionKernel32(const char *);
150238856Smmstatic int	 my_CreateSymbolicLinkA(const char *, const char *, int);
151238856Smmstatic int	 my_CreateHardLinkA(const char *, const char *);
152238856Smmstatic int	 my_GetFileInformationByName(const char *,
153238856Smm		     BY_HANDLE_FILE_INFORMATION *);
154238856Smm
155238856Smmstatic void *
156238856SmmGetFunctionKernel32(const char *name)
157228753Smm{
158228753Smm	static HINSTANCE lib;
159228753Smm	static int set;
160228753Smm	if (!set) {
161228753Smm		set = 1;
162228753Smm		lib = LoadLibrary("kernel32.dll");
163228753Smm	}
164228753Smm	if (lib == NULL) {
165228753Smm		fprintf(stderr, "Can't load kernel32.dll?!\n");
166228753Smm		exit(1);
167228753Smm	}
168228753Smm	return (void *)GetProcAddress(lib, name);
169228753Smm}
170228753Smm
171228753Smmstatic int
172228753Smmmy_CreateSymbolicLinkA(const char *linkname, const char *target, int flags)
173228753Smm{
174228753Smm	static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
175228753Smm	static int set;
176228753Smm	if (!set) {
177228753Smm		set = 1;
178228753Smm		f = GetFunctionKernel32("CreateSymbolicLinkA");
179228753Smm	}
180228753Smm	return f == NULL ? 0 : (*f)(linkname, target, flags);
181228753Smm}
182228753Smm
183228753Smmstatic int
184228753Smmmy_CreateHardLinkA(const char *linkname, const char *target)
185228753Smm{
186228753Smm	static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
187228753Smm	static int set;
188228753Smm	if (!set) {
189228753Smm		set = 1;
190228753Smm		f = GetFunctionKernel32("CreateHardLinkA");
191228753Smm	}
192228753Smm	return f == NULL ? 0 : (*f)(linkname, target, NULL);
193228753Smm}
194228753Smm
195238856Smmstatic int
196228753Smmmy_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
197228753Smm{
198228753Smm	HANDLE h;
199228753Smm	int r;
200228753Smm
201228753Smm	memset(bhfi, 0, sizeof(*bhfi));
202228753Smm	h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
203232153Smm		OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
204228753Smm	if (h == INVALID_HANDLE_VALUE)
205228753Smm		return (0);
206228753Smm	r = GetFileInformationByHandle(h, bhfi);
207228753Smm	CloseHandle(h);
208228753Smm	return (r);
209228753Smm}
210228753Smm#endif
211228753Smm
212302001Smm#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
213228753Smmstatic void
214228753Smminvalid_parameter_handler(const wchar_t * expression,
215228753Smm    const wchar_t * function, const wchar_t * file,
216228753Smm    unsigned int line, uintptr_t pReserved)
217228753Smm{
218228753Smm	/* nop */
219313571Smm	// Silence unused-parameter compiler warnings.
220313571Smm	(void)expression;
221313571Smm	(void)function;
222313571Smm	(void)file;
223313571Smm	(void)line;
224313571Smm	(void)pReserved;
225228753Smm}
226228753Smm#endif
227228753Smm
228228753Smm/*
229228753Smm *
230228753Smm * OPTIONS FLAGS
231228753Smm *
232228753Smm */
233228753Smm
234228753Smm/* Enable core dump on failure. */
235228753Smmstatic int dump_on_failure = 0;
236228753Smm/* Default is to remove temp dirs and log data for successful tests. */
237228753Smmstatic int keep_temp_files = 0;
238232153Smm/* Default is to run the specified tests once and report errors. */
239232153Smmstatic int until_failure = 0;
240228753Smm/* Default is to just report pass/fail for each test. */
241228753Smmstatic int verbosity = 0;
242228753Smm#define	VERBOSITY_SUMMARY_ONLY -1 /* -q */
243228753Smm#define VERBOSITY_PASSFAIL 0   /* Default */
244228753Smm#define VERBOSITY_LIGHT_REPORT 1 /* -v */
245228753Smm#define VERBOSITY_FULL 2 /* -vv */
246228753Smm/* A few places generate even more output for verbosity > VERBOSITY_FULL,
247228753Smm * mostly for debugging the test harness itself. */
248228753Smm/* Cumulative count of assertion failures. */
249228753Smmstatic int failures = 0;
250228753Smm/* Cumulative count of reported skips. */
251228753Smmstatic int skips = 0;
252228753Smm/* Cumulative count of assertions checked. */
253228753Smmstatic int assertions = 0;
254228753Smm
255228753Smm/* Directory where uuencoded reference files can be found. */
256228753Smmstatic const char *refdir;
257228753Smm
258228753Smm/*
259228753Smm * Report log information selectively to console and/or disk log.
260228753Smm */
261228753Smmstatic int log_console = 0;
262228753Smmstatic FILE *logfile;
263228753Smmstatic void
264228753Smmvlogprintf(const char *fmt, va_list ap)
265228753Smm{
266228753Smm#ifdef va_copy
267228753Smm	va_list lfap;
268228753Smm	va_copy(lfap, ap);
269228753Smm#endif
270228753Smm	if (log_console)
271228753Smm		vfprintf(stdout, fmt, ap);
272228753Smm	if (logfile != NULL)
273228753Smm#ifdef va_copy
274228753Smm		vfprintf(logfile, fmt, lfap);
275228753Smm	va_end(lfap);
276228753Smm#else
277228753Smm		vfprintf(logfile, fmt, ap);
278228753Smm#endif
279228753Smm}
280228753Smm
281228753Smmstatic void
282228753Smmlogprintf(const char *fmt, ...)
283228753Smm{
284228753Smm	va_list ap;
285228753Smm	va_start(ap, fmt);
286228753Smm	vlogprintf(fmt, ap);
287228753Smm	va_end(ap);
288228753Smm}
289228753Smm
290228753Smm/* Set up a message to display only if next assertion fails. */
291228753Smmstatic char msgbuff[4096];
292228753Smmstatic const char *msg, *nextmsg;
293228753Smmvoid
294228753Smmfailure(const char *fmt, ...)
295228753Smm{
296228753Smm	va_list ap;
297232153Smm	if (fmt == NULL) {
298232153Smm		nextmsg = NULL;
299232153Smm	} else {
300232153Smm		va_start(ap, fmt);
301232153Smm		vsprintf(msgbuff, fmt, ap);
302232153Smm		va_end(ap);
303232153Smm		nextmsg = msgbuff;
304232153Smm	}
305228753Smm}
306228753Smm
307228753Smm/*
308228753Smm * Copy arguments into file-local variables.
309228753Smm * This was added to permit vararg assert() functions without needing
310228753Smm * variadic wrapper macros.  Turns out that the vararg capability is almost
311228753Smm * never used, so almost all of the vararg assertions can be simplified
312228753Smm * by removing the vararg capability and reworking the wrapper macro to
313228753Smm * pass __FILE__, __LINE__ directly into the function instead of using
314228753Smm * this hook.  I suspect this machinery is used so rarely that we
315228753Smm * would be better off just removing it entirely.  That would simplify
316232153Smm * the code here noticeably.
317228753Smm */
318232153Smmstatic const char *skipping_filename;
319232153Smmstatic int skipping_line;
320232153Smmvoid skipping_setup(const char *filename, int line)
321228753Smm{
322232153Smm	skipping_filename = filename;
323232153Smm	skipping_line = line;
324228753Smm}
325228753Smm
326228753Smm/* Called at the beginning of each assert() function. */
327228753Smmstatic void
328228753Smmassertion_count(const char *file, int line)
329228753Smm{
330228753Smm	(void)file; /* UNUSED */
331228753Smm	(void)line; /* UNUSED */
332228753Smm	++assertions;
333228753Smm	/* Proper handling of "failure()" message. */
334228753Smm	msg = nextmsg;
335228753Smm	nextmsg = NULL;
336228753Smm	/* Uncomment to print file:line after every assertion.
337228753Smm	 * Verbose, but occasionally useful in tracking down crashes. */
338228753Smm	/* printf("Checked %s:%d\n", file, line); */
339228753Smm}
340228753Smm
341228753Smm/*
342228753Smm * For each test source file, we remember how many times each
343228753Smm * assertion was reported.  Cleared before each new test,
344228753Smm * used by test_summarize().
345228753Smm */
346228753Smmstatic struct line {
347228753Smm	int count;
348228753Smm	int skip;
349228753Smm}  failed_lines[10000];
350232153Smmconst char *failed_filename;
351228753Smm
352228753Smm/* Count this failure, setup up log destination and handle initial report. */
353228753Smmstatic void
354228753Smmfailure_start(const char *filename, int line, const char *fmt, ...)
355228753Smm{
356228753Smm	va_list ap;
357228753Smm
358228753Smm	/* Record another failure for this line. */
359228753Smm	++failures;
360232153Smm	failed_filename = filename;
361228753Smm	failed_lines[line].count++;
362228753Smm
363228753Smm	/* Determine whether to log header to console. */
364228753Smm	switch (verbosity) {
365228753Smm	case VERBOSITY_LIGHT_REPORT:
366228753Smm		log_console = (failed_lines[line].count < 2);
367228753Smm		break;
368228753Smm	default:
369228753Smm		log_console = (verbosity >= VERBOSITY_FULL);
370228753Smm	}
371228753Smm
372228753Smm	/* Log file:line header for this failure */
373228753Smm	va_start(ap, fmt);
374228753Smm#if _MSC_VER
375228753Smm	logprintf("%s(%d): ", filename, line);
376228753Smm#else
377228753Smm	logprintf("%s:%d: ", filename, line);
378228753Smm#endif
379228753Smm	vlogprintf(fmt, ap);
380228753Smm	va_end(ap);
381228753Smm	logprintf("\n");
382228753Smm
383228753Smm	if (msg != NULL && msg[0] != '\0') {
384228753Smm		logprintf("   Description: %s\n", msg);
385228753Smm		msg = NULL;
386228753Smm	}
387228753Smm
388228753Smm	/* Determine whether to log details to console. */
389228753Smm	if (verbosity == VERBOSITY_LIGHT_REPORT)
390228753Smm		log_console = 0;
391228753Smm}
392228753Smm
393228753Smm/* Complete reporting of failed tests. */
394228753Smm/*
395228753Smm * The 'extra' hook here is used by libarchive to include libarchive
396228753Smm * error messages with assertion failures.  It could also be used
397228753Smm * to add strerror() output, for example.  Just define the EXTRA_DUMP()
398228753Smm * macro appropriately.
399228753Smm */
400228753Smmstatic void
401228753Smmfailure_finish(void *extra)
402228753Smm{
403228753Smm	(void)extra; /* UNUSED (maybe) */
404228753Smm#ifdef EXTRA_DUMP
405232153Smm	if (extra != NULL) {
406232153Smm		logprintf("    errno: %d\n", EXTRA_ERRNO(extra));
407228753Smm		logprintf("   detail: %s\n", EXTRA_DUMP(extra));
408232153Smm	}
409228753Smm#endif
410228753Smm
411228753Smm	if (dump_on_failure) {
412228753Smm		fprintf(stderr,
413228753Smm		    " *** forcing core dump so failure can be debugged ***\n");
414228753Smm		abort();
415228753Smm	}
416228753Smm}
417228753Smm
418228753Smm/* Inform user that we're skipping some checks. */
419228753Smmvoid
420228753Smmtest_skipping(const char *fmt, ...)
421228753Smm{
422228753Smm	char buff[1024];
423228753Smm	va_list ap;
424228753Smm
425228753Smm	va_start(ap, fmt);
426228753Smm	vsprintf(buff, fmt, ap);
427228753Smm	va_end(ap);
428232153Smm	/* Use failure() message if set. */
429232153Smm	msg = nextmsg;
430232153Smm	nextmsg = NULL;
431228753Smm	/* failure_start() isn't quite right, but is awfully convenient. */
432232153Smm	failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
433228753Smm	--failures; /* Undo failures++ in failure_start() */
434228753Smm	/* Don't failure_finish() here. */
435228753Smm	/* Mark as skip, so doesn't count as failed test. */
436232153Smm	failed_lines[skipping_line].skip = 1;
437228753Smm	++skips;
438228753Smm}
439228753Smm
440228753Smm/*
441228753Smm *
442228753Smm * ASSERTIONS
443228753Smm *
444228753Smm */
445228753Smm
446228753Smm/* Generic assert() just displays the failed condition. */
447228753Smmint
448228753Smmassertion_assert(const char *file, int line, int value,
449228753Smm    const char *condition, void *extra)
450228753Smm{
451228753Smm	assertion_count(file, line);
452228753Smm	if (!value) {
453228753Smm		failure_start(file, line, "Assertion failed: %s", condition);
454228753Smm		failure_finish(extra);
455228753Smm	}
456228753Smm	return (value);
457228753Smm}
458228753Smm
459228753Smm/* chdir() and report any errors */
460228753Smmint
461228753Smmassertion_chdir(const char *file, int line, const char *pathname)
462228753Smm{
463228753Smm	assertion_count(file, line);
464228753Smm	if (chdir(pathname) == 0)
465228753Smm		return (1);
466228753Smm	failure_start(file, line, "chdir(\"%s\")", pathname);
467228753Smm	failure_finish(NULL);
468228753Smm	return (0);
469228753Smm
470228753Smm}
471228753Smm
472228753Smm/* Verify two integers are equal. */
473228753Smmint
474228753Smmassertion_equal_int(const char *file, int line,
475228753Smm    long long v1, const char *e1, long long v2, const char *e2, void *extra)
476228753Smm{
477228753Smm	assertion_count(file, line);
478228753Smm	if (v1 == v2)
479228753Smm		return (1);
480228753Smm	failure_start(file, line, "%s != %s", e1, e2);
481228753Smm	logprintf("      %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
482228753Smm	logprintf("      %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
483228753Smm	failure_finish(extra);
484228753Smm	return (0);
485228753Smm}
486228753Smm
487232153Smm/*
488232153Smm * Utility to convert a single UTF-8 sequence.
489232153Smm */
490232153Smmstatic int
491232153Smm_utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
492228753Smm{
493232153Smm	static const char utf8_count[256] = {
494232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
495232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
496232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
497232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
498232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
499232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
500232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
501232153Smm		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
502232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
503232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
504232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
505232153Smm		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
506232153Smm		 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
507232153Smm		 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
508232153Smm		 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
509232153Smm		 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
510232153Smm	};
511232153Smm	int ch;
512232153Smm	int cnt;
513232153Smm	uint32_t wc;
514232153Smm
515232153Smm	*pwc = 0;
516232153Smm
517232153Smm	/* Sanity check. */
518232153Smm	if (n == 0)
519232153Smm		return (0);
520232153Smm	/*
521232153Smm	 * Decode 1-4 bytes depending on the value of the first byte.
522232153Smm	 */
523232153Smm	ch = (unsigned char)*s;
524232153Smm	if (ch == 0)
525232153Smm		return (0); /* Standard:  return 0 for end-of-string. */
526232153Smm	cnt = utf8_count[ch];
527232153Smm
528311042Smm	/* Invalid sequence or there are not plenty bytes. */
529232153Smm	if (n < (size_t)cnt)
530232153Smm		return (-1);
531232153Smm
532232153Smm	/* Make a Unicode code point from a single UTF-8 sequence. */
533232153Smm	switch (cnt) {
534232153Smm	case 1:	/* 1 byte sequence. */
535232153Smm		*pwc = ch & 0x7f;
536232153Smm		return (cnt);
537232153Smm	case 2:	/* 2 bytes sequence. */
538232153Smm		if ((s[1] & 0xc0) != 0x80) return (-1);
539232153Smm		*pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
540232153Smm		return (cnt);
541232153Smm	case 3:	/* 3 bytes sequence. */
542232153Smm		if ((s[1] & 0xc0) != 0x80) return (-1);
543232153Smm		if ((s[2] & 0xc0) != 0x80) return (-1);
544232153Smm		wc = ((ch & 0x0f) << 12)
545232153Smm		    | ((s[1] & 0x3f) << 6)
546232153Smm		    | (s[2] & 0x3f);
547232153Smm		if (wc < 0x800)
548232153Smm			return (-1);/* Overlong sequence. */
549232153Smm		break;
550232153Smm	case 4:	/* 4 bytes sequence. */
551232153Smm		if (n < 4)
552232153Smm			return (-1);
553232153Smm		if ((s[1] & 0xc0) != 0x80) return (-1);
554232153Smm		if ((s[2] & 0xc0) != 0x80) return (-1);
555232153Smm		if ((s[3] & 0xc0) != 0x80) return (-1);
556232153Smm		wc = ((ch & 0x07) << 18)
557232153Smm		    | ((s[1] & 0x3f) << 12)
558232153Smm		    | ((s[2] & 0x3f) << 6)
559232153Smm		    | (s[3] & 0x3f);
560232153Smm		if (wc < 0x10000)
561232153Smm			return (-1);/* Overlong sequence. */
562232153Smm		break;
563232153Smm	default:
564232153Smm		return (-1);
565232153Smm	}
566232153Smm
567311042Smm	/* The code point larger than 0x10FFFF is not legal
568232153Smm	 * Unicode values. */
569232153Smm	if (wc > 0x10FFFF)
570232153Smm		return (-1);
571232153Smm	/* Correctly gets a Unicode, returns used bytes. */
572232153Smm	*pwc = wc;
573232153Smm	return (cnt);
574232153Smm}
575232153Smm
576232153Smmstatic void strdump(const char *e, const char *p, int ewidth, int utf8)
577232153Smm{
578228753Smm	const char *q = p;
579228753Smm
580232153Smm	logprintf("      %*s = ", ewidth, e);
581228753Smm	if (p == NULL) {
582232153Smm		logprintf("NULL\n");
583228753Smm		return;
584228753Smm	}
585228753Smm	logprintf("\"");
586228753Smm	while (*p != '\0') {
587228753Smm		unsigned int c = 0xff & *p++;
588228753Smm		switch (c) {
589302001Smm		case '\a': logprintf("\\a"); break;
590302001Smm		case '\b': logprintf("\\b"); break;
591302001Smm		case '\n': logprintf("\\n"); break;
592302001Smm		case '\r': logprintf("\\r"); break;
593228753Smm		default:
594228753Smm			if (c >= 32 && c < 127)
595228753Smm				logprintf("%c", c);
596228753Smm			else
597228753Smm				logprintf("\\x%02X", c);
598228753Smm		}
599228753Smm	}
600228753Smm	logprintf("\"");
601232153Smm	logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
602232153Smm
603232153Smm	/*
604232153Smm	 * If the current string is UTF-8, dump its code points.
605232153Smm	 */
606232153Smm	if (utf8) {
607232153Smm		size_t len;
608232153Smm		uint32_t uc;
609232153Smm		int n;
610232153Smm		int cnt = 0;
611232153Smm
612232153Smm		p = q;
613232153Smm		len = strlen(p);
614232153Smm		logprintf(" [");
615232153Smm		while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
616232153Smm			if (p != q)
617232153Smm				logprintf(" ");
618232153Smm			logprintf("%04X", uc);
619232153Smm			p += n;
620232153Smm			len -= n;
621232153Smm			cnt++;
622232153Smm		}
623232153Smm		logprintf("]");
624232153Smm		logprintf(" (count %d", cnt);
625232153Smm		if (n < 0) {
626232153Smm			logprintf(",unknown %d bytes", len);
627232153Smm		}
628232153Smm		logprintf(")");
629232153Smm
630232153Smm	}
631232153Smm	logprintf("\n");
632228753Smm}
633228753Smm
634228753Smm/* Verify two strings are equal, dump them if not. */
635228753Smmint
636228753Smmassertion_equal_string(const char *file, int line,
637228753Smm    const char *v1, const char *e1,
638228753Smm    const char *v2, const char *e2,
639232153Smm    void *extra, int utf8)
640228753Smm{
641232153Smm	int l1, l2;
642232153Smm
643228753Smm	assertion_count(file, line);
644228753Smm	if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
645228753Smm		return (1);
646228753Smm	failure_start(file, line, "%s != %s", e1, e2);
647248616Smm	l1 = (int)strlen(e1);
648248616Smm	l2 = (int)strlen(e2);
649232153Smm	if (l1 < l2)
650232153Smm		l1 = l2;
651232153Smm	strdump(e1, v1, l1, utf8);
652232153Smm	strdump(e2, v2, l1, utf8);
653228753Smm	failure_finish(extra);
654228753Smm	return (0);
655228753Smm}
656228753Smm
657228753Smmstatic void
658228753Smmwcsdump(const char *e, const wchar_t *w)
659228753Smm{
660228753Smm	logprintf("      %s = ", e);
661228753Smm	if (w == NULL) {
662228753Smm		logprintf("(null)");
663228753Smm		return;
664228753Smm	}
665228753Smm	logprintf("\"");
666228753Smm	while (*w != L'\0') {
667228753Smm		unsigned int c = *w++;
668228753Smm		if (c >= 32 && c < 127)
669228753Smm			logprintf("%c", c);
670228753Smm		else if (c < 256)
671228753Smm			logprintf("\\x%02X", c);
672228753Smm		else if (c < 0x10000)
673228753Smm			logprintf("\\u%04X", c);
674228753Smm		else
675228753Smm			logprintf("\\U%08X", c);
676228753Smm	}
677228753Smm	logprintf("\"\n");
678228753Smm}
679228753Smm
680228753Smm#ifndef HAVE_WCSCMP
681228753Smmstatic int
682228753Smmwcscmp(const wchar_t *s1, const wchar_t *s2)
683228753Smm{
684228753Smm
685228753Smm	while (*s1 == *s2++) {
686228753Smm		if (*s1++ == L'\0')
687228753Smm			return 0;
688228753Smm	}
689228753Smm	if (*s1 > *--s2)
690228753Smm		return 1;
691228753Smm	else
692228753Smm		return -1;
693228753Smm}
694228753Smm#endif
695228753Smm
696228753Smm/* Verify that two wide strings are equal, dump them if not. */
697228753Smmint
698228753Smmassertion_equal_wstring(const char *file, int line,
699228753Smm    const wchar_t *v1, const char *e1,
700228753Smm    const wchar_t *v2, const char *e2,
701228753Smm    void *extra)
702228753Smm{
703228753Smm	assertion_count(file, line);
704232153Smm	if (v1 == v2)
705228753Smm		return (1);
706232153Smm	if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
707232153Smm		return (1);
708228753Smm	failure_start(file, line, "%s != %s", e1, e2);
709228753Smm	wcsdump(e1, v1);
710228753Smm	wcsdump(e2, v2);
711228753Smm	failure_finish(extra);
712228753Smm	return (0);
713228753Smm}
714228753Smm
715228753Smm/*
716228753Smm * Pretty standard hexdump routine.  As a bonus, if ref != NULL, then
717228753Smm * any bytes in p that differ from ref will be highlighted with '_'
718228753Smm * before and after the hex value.
719228753Smm */
720228753Smmstatic void
721228753Smmhexdump(const char *p, const char *ref, size_t l, size_t offset)
722228753Smm{
723228753Smm	size_t i, j;
724228753Smm	char sep;
725228753Smm
726228753Smm	if (p == NULL) {
727228753Smm		logprintf("(null)\n");
728228753Smm		return;
729228753Smm	}
730228753Smm	for(i=0; i < l; i+=16) {
731228753Smm		logprintf("%04x", (unsigned)(i + offset));
732228753Smm		sep = ' ';
733228753Smm		for (j = 0; j < 16 && i + j < l; j++) {
734228753Smm			if (ref != NULL && p[i + j] != ref[i + j])
735228753Smm				sep = '_';
736228753Smm			logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
737228753Smm			if (ref != NULL && p[i + j] == ref[i + j])
738228753Smm				sep = ' ';
739228753Smm		}
740228753Smm		for (; j < 16; j++) {
741228753Smm			logprintf("%c  ", sep);
742228753Smm			sep = ' ';
743228753Smm		}
744228753Smm		logprintf("%c", sep);
745228753Smm		for (j=0; j < 16 && i + j < l; j++) {
746228753Smm			int c = p[i + j];
747228753Smm			if (c >= ' ' && c <= 126)
748228753Smm				logprintf("%c", c);
749228753Smm			else
750228753Smm				logprintf(".");
751228753Smm		}
752228753Smm		logprintf("\n");
753228753Smm	}
754228753Smm}
755228753Smm
756228753Smm/* Verify that two blocks of memory are the same, display the first
757228753Smm * block of differences if they're not. */
758228753Smmint
759228753Smmassertion_equal_mem(const char *file, int line,
760228753Smm    const void *_v1, const char *e1,
761228753Smm    const void *_v2, const char *e2,
762228753Smm    size_t l, const char *ld, void *extra)
763228753Smm{
764228753Smm	const char *v1 = (const char *)_v1;
765228753Smm	const char *v2 = (const char *)_v2;
766228753Smm	size_t offset;
767228753Smm
768228753Smm	assertion_count(file, line);
769228753Smm	if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
770228753Smm		return (1);
771248616Smm	if (v1 == NULL || v2 == NULL)
772248616Smm		return (0);
773228753Smm
774228753Smm	failure_start(file, line, "%s != %s", e1, e2);
775228753Smm	logprintf("      size %s = %d\n", ld, (int)l);
776228753Smm	/* Dump 48 bytes (3 lines) so that the first difference is
777228753Smm	 * in the second line. */
778228753Smm	offset = 0;
779228753Smm	while (l > 64 && memcmp(v1, v2, 32) == 0) {
780228753Smm		/* Two lines agree, so step forward one line. */
781228753Smm		v1 += 16;
782228753Smm		v2 += 16;
783228753Smm		l -= 16;
784228753Smm		offset += 16;
785228753Smm	}
786228753Smm	logprintf("      Dump of %s\n", e1);
787232153Smm	hexdump(v1, v2, l < 128 ? l : 128, offset);
788228753Smm	logprintf("      Dump of %s\n", e2);
789232153Smm	hexdump(v2, v1, l < 128 ? l : 128, offset);
790228753Smm	logprintf("\n");
791228753Smm	failure_finish(extra);
792228753Smm	return (0);
793228753Smm}
794228753Smm
795302001Smm/* Verify that a block of memory is filled with the specified byte. */
796302001Smmint
797302001Smmassertion_memory_filled_with(const char *file, int line,
798302001Smm    const void *_v1, const char *vd,
799302001Smm    size_t l, const char *ld,
800302001Smm    char b, const char *bd, void *extra)
801302001Smm{
802302001Smm	const char *v1 = (const char *)_v1;
803302001Smm	size_t c = 0;
804302001Smm	size_t i;
805302001Smm	(void)ld; /* UNUSED */
806302001Smm
807302001Smm	assertion_count(file, line);
808302001Smm
809302001Smm	for (i = 0; i < l; ++i) {
810302001Smm		if (v1[i] == b) {
811302001Smm			++c;
812302001Smm		}
813302001Smm	}
814302001Smm	if (c == l)
815302001Smm		return (1);
816302001Smm
817302001Smm	failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
818302001Smm	logprintf("   Only %d bytes were correct\n", (int)c);
819302001Smm	failure_finish(extra);
820302001Smm	return (0);
821302001Smm}
822302001Smm
823228753Smm/* Verify that the named file exists and is empty. */
824228753Smmint
825232153Smmassertion_empty_file(const char *filename, int line, const char *f1)
826228753Smm{
827228753Smm	char buff[1024];
828228753Smm	struct stat st;
829228753Smm	ssize_t s;
830228753Smm	FILE *f;
831228753Smm
832232153Smm	assertion_count(filename, line);
833228753Smm
834228753Smm	if (stat(f1, &st) != 0) {
835232153Smm		failure_start(filename, line, "Stat failed: %s", f1);
836228753Smm		failure_finish(NULL);
837228753Smm		return (0);
838228753Smm	}
839228753Smm	if (st.st_size == 0)
840228753Smm		return (1);
841228753Smm
842232153Smm	failure_start(filename, line, "File should be empty: %s", f1);
843228753Smm	logprintf("    File size: %d\n", (int)st.st_size);
844228753Smm	logprintf("    Contents:\n");
845228753Smm	f = fopen(f1, "rb");
846228753Smm	if (f == NULL) {
847228753Smm		logprintf("    Unable to open %s\n", f1);
848228753Smm	} else {
849228753Smm		s = ((off_t)sizeof(buff) < st.st_size) ?
850228753Smm		    (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
851228753Smm		s = fread(buff, 1, s, f);
852228753Smm		hexdump(buff, NULL, s, 0);
853228753Smm		fclose(f);
854228753Smm	}
855228753Smm	failure_finish(NULL);
856228753Smm	return (0);
857228753Smm}
858228753Smm
859228753Smm/* Verify that the named file exists and is not empty. */
860228753Smmint
861232153Smmassertion_non_empty_file(const char *filename, int line, const char *f1)
862228753Smm{
863228753Smm	struct stat st;
864228753Smm
865232153Smm	assertion_count(filename, line);
866228753Smm
867228753Smm	if (stat(f1, &st) != 0) {
868232153Smm		failure_start(filename, line, "Stat failed: %s", f1);
869228753Smm		failure_finish(NULL);
870228753Smm		return (0);
871228753Smm	}
872228753Smm	if (st.st_size == 0) {
873232153Smm		failure_start(filename, line, "File empty: %s", f1);
874228753Smm		failure_finish(NULL);
875228753Smm		return (0);
876228753Smm	}
877228753Smm	return (1);
878228753Smm}
879228753Smm
880228753Smm/* Verify that two files have the same contents. */
881228753Smm/* TODO: hexdump the first bytes that actually differ. */
882228753Smmint
883232153Smmassertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
884228753Smm{
885228753Smm	char buff1[1024];
886228753Smm	char buff2[1024];
887228753Smm	FILE *f1, *f2;
888228753Smm	int n1, n2;
889228753Smm
890232153Smm	assertion_count(filename, line);
891228753Smm
892228753Smm	f1 = fopen(fn1, "rb");
893228753Smm	f2 = fopen(fn2, "rb");
894248616Smm	if (f1 == NULL || f2 == NULL) {
895248616Smm		if (f1) fclose(f1);
896248616Smm		if (f2) fclose(f2);
897248616Smm		return (0);
898248616Smm	}
899228753Smm	for (;;) {
900248616Smm		n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
901248616Smm		n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
902228753Smm		if (n1 != n2)
903228753Smm			break;
904228753Smm		if (n1 == 0 && n2 == 0) {
905228753Smm			fclose(f1);
906228753Smm			fclose(f2);
907228753Smm			return (1);
908228753Smm		}
909228753Smm		if (memcmp(buff1, buff2, n1) != 0)
910228753Smm			break;
911228753Smm	}
912228753Smm	fclose(f1);
913228753Smm	fclose(f2);
914232153Smm	failure_start(filename, line, "Files not identical");
915228753Smm	logprintf("  file1=\"%s\"\n", fn1);
916228753Smm	logprintf("  file2=\"%s\"\n", fn2);
917232153Smm	failure_finish(NULL);
918228753Smm	return (0);
919228753Smm}
920228753Smm
921228753Smm/* Verify that the named file does exist. */
922228753Smmint
923232153Smmassertion_file_exists(const char *filename, int line, const char *f)
924228753Smm{
925232153Smm	assertion_count(filename, line);
926228753Smm
927228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
928228753Smm	if (!_access(f, 0))
929228753Smm		return (1);
930228753Smm#else
931228753Smm	if (!access(f, F_OK))
932228753Smm		return (1);
933228753Smm#endif
934232153Smm	failure_start(filename, line, "File should exist: %s", f);
935232153Smm	failure_finish(NULL);
936228753Smm	return (0);
937228753Smm}
938228753Smm
939228753Smm/* Verify that the named file doesn't exist. */
940228753Smmint
941232153Smmassertion_file_not_exists(const char *filename, int line, const char *f)
942228753Smm{
943232153Smm	assertion_count(filename, line);
944228753Smm
945228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
946228753Smm	if (_access(f, 0))
947228753Smm		return (1);
948228753Smm#else
949228753Smm	if (access(f, F_OK))
950228753Smm		return (1);
951228753Smm#endif
952232153Smm	failure_start(filename, line, "File should not exist: %s", f);
953232153Smm	failure_finish(NULL);
954228753Smm	return (0);
955228753Smm}
956228753Smm
957228753Smm/* Compare the contents of a file to a block of memory. */
958228753Smmint
959232153Smmassertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
960228753Smm{
961228753Smm	char *contents;
962228753Smm	FILE *f;
963228753Smm	int n;
964228753Smm
965232153Smm	assertion_count(filename, line);
966228753Smm
967228753Smm	f = fopen(fn, "rb");
968228753Smm	if (f == NULL) {
969232153Smm		failure_start(filename, line,
970228753Smm		    "File should exist: %s", fn);
971232153Smm		failure_finish(NULL);
972228753Smm		return (0);
973228753Smm	}
974228753Smm	contents = malloc(s * 2);
975248616Smm	n = (int)fread(contents, 1, s * 2, f);
976228753Smm	fclose(f);
977228753Smm	if (n == s && memcmp(buff, contents, s) == 0) {
978228753Smm		free(contents);
979228753Smm		return (1);
980228753Smm	}
981232153Smm	failure_start(filename, line, "File contents don't match");
982228753Smm	logprintf("  file=\"%s\"\n", fn);
983228753Smm	if (n > 0)
984228753Smm		hexdump(contents, buff, n > 512 ? 512 : n, 0);
985228753Smm	else {
986228753Smm		logprintf("  File empty, contents should be:\n");
987232153Smm		hexdump(buff, NULL, s > 512 ? 512 : s, 0);
988228753Smm	}
989232153Smm	failure_finish(NULL);
990228753Smm	free(contents);
991228753Smm	return (0);
992228753Smm}
993228753Smm
994228753Smm/* Check the contents of a text file, being tolerant of line endings. */
995228753Smmint
996232153Smmassertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
997228753Smm{
998228753Smm	char *contents;
999228753Smm	const char *btxt, *ftxt;
1000228753Smm	FILE *f;
1001228753Smm	int n, s;
1002228753Smm
1003232153Smm	assertion_count(filename, line);
1004228753Smm	f = fopen(fn, "r");
1005232153Smm	if (f == NULL) {
1006232153Smm		failure_start(filename, line,
1007232153Smm		    "File doesn't exist: %s", fn);
1008232153Smm		failure_finish(NULL);
1009232153Smm		return (0);
1010232153Smm	}
1011248616Smm	s = (int)strlen(buff);
1012228753Smm	contents = malloc(s * 2 + 128);
1013248616Smm	n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1014228753Smm	if (n >= 0)
1015228753Smm		contents[n] = '\0';
1016228753Smm	fclose(f);
1017228753Smm	/* Compare texts. */
1018228753Smm	btxt = buff;
1019228753Smm	ftxt = (const char *)contents;
1020228753Smm	while (*btxt != '\0' && *ftxt != '\0') {
1021228753Smm		if (*btxt == *ftxt) {
1022228753Smm			++btxt;
1023228753Smm			++ftxt;
1024228753Smm			continue;
1025228753Smm		}
1026228753Smm		if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1027228753Smm			/* Pass over different new line characters. */
1028228753Smm			++btxt;
1029228753Smm			ftxt += 2;
1030228753Smm			continue;
1031228753Smm		}
1032228753Smm		break;
1033228753Smm	}
1034228753Smm	if (*btxt == '\0' && *ftxt == '\0') {
1035228753Smm		free(contents);
1036228753Smm		return (1);
1037228753Smm	}
1038232153Smm	failure_start(filename, line, "Contents don't match");
1039228753Smm	logprintf("  file=\"%s\"\n", fn);
1040232153Smm	if (n > 0) {
1041228753Smm		hexdump(contents, buff, n, 0);
1042232153Smm		logprintf("  expected\n", fn);
1043232153Smm		hexdump(buff, contents, s, 0);
1044232153Smm	} else {
1045228753Smm		logprintf("  File empty, contents should be:\n");
1046228753Smm		hexdump(buff, NULL, s, 0);
1047228753Smm	}
1048232153Smm	failure_finish(NULL);
1049228753Smm	free(contents);
1050228753Smm	return (0);
1051228753Smm}
1052228753Smm
1053232153Smm/* Verify that a text file contains the specified lines, regardless of order */
1054232153Smm/* This could be more efficient if we sorted both sets of lines, etc, but
1055232153Smm * since this is used only for testing and only ever deals with a dozen or so
1056232153Smm * lines at a time, this relatively crude approach is just fine. */
1057232153Smmint
1058232153Smmassertion_file_contains_lines_any_order(const char *file, int line,
1059232153Smm    const char *pathname, const char *lines[])
1060232153Smm{
1061232153Smm	char *buff;
1062232153Smm	size_t buff_size;
1063232153Smm	size_t expected_count, actual_count, i, j;
1064248616Smm	char **expected = NULL;
1065248616Smm	char *p, **actual = NULL;
1066232153Smm	char c;
1067232153Smm	int expected_failure = 0, actual_failure = 0;
1068232153Smm
1069232153Smm	assertion_count(file, line);
1070232153Smm
1071232153Smm	buff = slurpfile(&buff_size, "%s", pathname);
1072232153Smm	if (buff == NULL) {
1073232153Smm		failure_start(pathname, line, "Can't read file: %s", pathname);
1074232153Smm		failure_finish(NULL);
1075232153Smm		return (0);
1076232153Smm	}
1077232153Smm
1078248616Smm	/* Make a copy of the provided lines and count up the expected
1079248616Smm	 * file size. */
1080232153Smm	for (i = 0; lines[i] != NULL; ++i) {
1081232153Smm	}
1082232153Smm	expected_count = i;
1083248616Smm	if (expected_count) {
1084248616Smm		expected = malloc(sizeof(char *) * expected_count);
1085248616Smm		if (expected == NULL) {
1086248616Smm			failure_start(pathname, line, "Can't allocate memory");
1087248616Smm			failure_finish(NULL);
1088302001Smm			free(expected);
1089248616Smm			return (0);
1090248616Smm		}
1091248616Smm		for (i = 0; lines[i] != NULL; ++i) {
1092248616Smm			expected[i] = strdup(lines[i]);
1093248616Smm		}
1094232153Smm	}
1095232153Smm
1096232153Smm	/* Break the file into lines */
1097232153Smm	actual_count = 0;
1098232153Smm	for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1099232153Smm		if (*p == '\x0d' || *p == '\x0a')
1100232153Smm			*p = '\0';
1101232153Smm		if (c == '\0' && *p != '\0')
1102232153Smm			++actual_count;
1103232153Smm		c = *p;
1104232153Smm	}
1105248616Smm	if (actual_count) {
1106248616Smm		actual = calloc(sizeof(char *), actual_count);
1107248616Smm		if (actual == NULL) {
1108248616Smm			failure_start(pathname, line, "Can't allocate memory");
1109248616Smm			failure_finish(NULL);
1110248616Smm			free(expected);
1111248616Smm			return (0);
1112232153Smm		}
1113248616Smm		for (j = 0, p = buff; p < buff + buff_size;
1114248616Smm		    p += 1 + strlen(p)) {
1115248616Smm			if (*p != '\0') {
1116248616Smm				actual[j] = p;
1117248616Smm				++j;
1118248616Smm			}
1119248616Smm		}
1120232153Smm	}
1121232153Smm
1122232153Smm	/* Erase matching lines from both lists */
1123232153Smm	for (i = 0; i < expected_count; ++i) {
1124232153Smm		if (expected[i] == NULL)
1125232153Smm			continue;
1126232153Smm		for (j = 0; j < actual_count; ++j) {
1127232153Smm			if (actual[j] == NULL)
1128232153Smm				continue;
1129232153Smm			if (strcmp(expected[i], actual[j]) == 0) {
1130232153Smm				free(expected[i]);
1131232153Smm				expected[i] = NULL;
1132232153Smm				actual[j] = NULL;
1133232153Smm				break;
1134232153Smm			}
1135232153Smm		}
1136232153Smm	}
1137232153Smm
1138232153Smm	/* If there's anything left, it's a failure */
1139232153Smm	for (i = 0; i < expected_count; ++i) {
1140232153Smm		if (expected[i] != NULL)
1141232153Smm			++expected_failure;
1142232153Smm	}
1143232153Smm	for (j = 0; j < actual_count; ++j) {
1144232153Smm		if (actual[j] != NULL)
1145232153Smm			++actual_failure;
1146232153Smm	}
1147232153Smm	if (expected_failure == 0 && actual_failure == 0) {
1148232153Smm		free(buff);
1149232153Smm		free(expected);
1150232153Smm		free(actual);
1151232153Smm		return (1);
1152232153Smm	}
1153232153Smm	failure_start(file, line, "File doesn't match: %s", pathname);
1154232153Smm	for (i = 0; i < expected_count; ++i) {
1155232153Smm		if (expected[i] != NULL) {
1156232153Smm			logprintf("  Expected but not present: %s\n", expected[i]);
1157232153Smm			free(expected[i]);
1158232153Smm		}
1159232153Smm	}
1160232153Smm	for (j = 0; j < actual_count; ++j) {
1161232153Smm		if (actual[j] != NULL)
1162232153Smm			logprintf("  Present but not expected: %s\n", actual[j]);
1163232153Smm	}
1164232153Smm	failure_finish(NULL);
1165232153Smm	free(buff);
1166232153Smm	free(expected);
1167232153Smm	free(actual);
1168232153Smm	return (0);
1169232153Smm}
1170232153Smm
1171308152Smm/* Verify that a text file does not contains the specified strings */
1172308152Smmint
1173308152Smmassertion_file_contains_no_invalid_strings(const char *file, int line,
1174308152Smm    const char *pathname, const char *strings[])
1175308152Smm{
1176308152Smm	char *buff;
1177308152Smm	int i;
1178308152Smm
1179308152Smm	buff = slurpfile(NULL, "%s", pathname);
1180308152Smm	if (buff == NULL) {
1181308152Smm		failure_start(file, line, "Can't read file: %s", pathname);
1182308152Smm		failure_finish(NULL);
1183308152Smm		return (0);
1184308152Smm	}
1185308152Smm
1186308152Smm	for (i = 0; strings[i] != NULL; ++i) {
1187308152Smm		if (strstr(buff, strings[i]) != NULL) {
1188308152Smm			failure_start(file, line, "Invalid string in %s: %s", pathname,
1189308152Smm			    strings[i]);
1190308152Smm			failure_finish(NULL);
1191308152Smm			free(buff);
1192308152Smm			return(0);
1193308152Smm		}
1194308152Smm	}
1195308152Smm
1196308152Smm	free(buff);
1197308152Smm	return (0);
1198308152Smm}
1199308152Smm
1200228753Smm/* Test that two paths point to the same file. */
1201228753Smm/* As a side-effect, asserts that both files exist. */
1202228753Smmstatic int
1203228753Smmis_hardlink(const char *file, int line,
1204228753Smm    const char *path1, const char *path2)
1205228753Smm{
1206228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1207228753Smm	BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1208228753Smm	int r;
1209228753Smm
1210228753Smm	assertion_count(file, line);
1211228753Smm	r = my_GetFileInformationByName(path1, &bhfi1);
1212228753Smm	if (r == 0) {
1213228753Smm		failure_start(file, line, "File %s can't be inspected?", path1);
1214228753Smm		failure_finish(NULL);
1215228753Smm		return (0);
1216228753Smm	}
1217228753Smm	r = my_GetFileInformationByName(path2, &bhfi2);
1218228753Smm	if (r == 0) {
1219228753Smm		failure_start(file, line, "File %s can't be inspected?", path2);
1220228753Smm		failure_finish(NULL);
1221228753Smm		return (0);
1222228753Smm	}
1223228753Smm	return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1224228753Smm		&& bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1225228753Smm		&& bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1226228753Smm#else
1227228753Smm	struct stat st1, st2;
1228228753Smm	int r;
1229228753Smm
1230228753Smm	assertion_count(file, line);
1231228753Smm	r = lstat(path1, &st1);
1232228753Smm	if (r != 0) {
1233228753Smm		failure_start(file, line, "File should exist: %s", path1);
1234228753Smm		failure_finish(NULL);
1235228753Smm		return (0);
1236228753Smm	}
1237228753Smm	r = lstat(path2, &st2);
1238228753Smm	if (r != 0) {
1239228753Smm		failure_start(file, line, "File should exist: %s", path2);
1240228753Smm		failure_finish(NULL);
1241228753Smm		return (0);
1242228753Smm	}
1243228753Smm	return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1244228753Smm#endif
1245228753Smm}
1246228753Smm
1247228753Smmint
1248228753Smmassertion_is_hardlink(const char *file, int line,
1249228753Smm    const char *path1, const char *path2)
1250228753Smm{
1251228753Smm	if (is_hardlink(file, line, path1, path2))
1252228753Smm		return (1);
1253228753Smm	failure_start(file, line,
1254228753Smm	    "Files %s and %s are not hardlinked", path1, path2);
1255228753Smm	failure_finish(NULL);
1256228753Smm	return (0);
1257228753Smm}
1258228753Smm
1259228753Smmint
1260228753Smmassertion_is_not_hardlink(const char *file, int line,
1261228753Smm    const char *path1, const char *path2)
1262228753Smm{
1263228753Smm	if (!is_hardlink(file, line, path1, path2))
1264228753Smm		return (1);
1265228753Smm	failure_start(file, line,
1266228753Smm	    "Files %s and %s should not be hardlinked", path1, path2);
1267228753Smm	failure_finish(NULL);
1268228753Smm	return (0);
1269228753Smm}
1270228753Smm
1271228753Smm/* Verify a/b/mtime of 'pathname'. */
1272228753Smm/* If 'recent', verify that it's within last 10 seconds. */
1273228753Smmstatic int
1274228753Smmassertion_file_time(const char *file, int line,
1275228753Smm    const char *pathname, long t, long nsec, char type, int recent)
1276228753Smm{
1277228753Smm	long long filet, filet_nsec;
1278228753Smm	int r;
1279228753Smm
1280228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1281228753Smm#define EPOC_TIME	(116444736000000000ULL)
1282248616Smm	FILETIME fxtime, fbirthtime, fatime, fmtime;
1283228753Smm	ULARGE_INTEGER wintm;
1284228753Smm	HANDLE h;
1285248616Smm	fxtime.dwLowDateTime = 0;
1286248616Smm	fxtime.dwHighDateTime = 0;
1287228753Smm
1288228753Smm	assertion_count(file, line);
1289232153Smm	/* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1290232153Smm	 * a directory file. If not, CreateFile() will fail when
1291232153Smm	 * the pathname is a directory. */
1292228753Smm	h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1293232153Smm	    OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1294228753Smm	if (h == INVALID_HANDLE_VALUE) {
1295228753Smm		failure_start(file, line, "Can't access %s\n", pathname);
1296228753Smm		failure_finish(NULL);
1297228753Smm		return (0);
1298228753Smm	}
1299228753Smm	r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1300228753Smm	switch (type) {
1301248616Smm	case 'a': fxtime = fatime; break;
1302248616Smm	case 'b': fxtime = fbirthtime; break;
1303248616Smm	case 'm': fxtime = fmtime; break;
1304228753Smm	}
1305228753Smm	CloseHandle(h);
1306228753Smm	if (r == 0) {
1307228753Smm		failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1308228753Smm		failure_finish(NULL);
1309228753Smm		return (0);
1310228753Smm	}
1311248616Smm	wintm.LowPart = fxtime.dwLowDateTime;
1312248616Smm	wintm.HighPart = fxtime.dwHighDateTime;
1313228753Smm	filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1314228753Smm	filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1315228753Smm	nsec = (nsec / 100) * 100; /* Round the request */
1316228753Smm#else
1317228753Smm	struct stat st;
1318228753Smm
1319228753Smm	assertion_count(file, line);
1320228753Smm	r = lstat(pathname, &st);
1321228753Smm	if (r != 0) {
1322228753Smm		failure_start(file, line, "Can't stat %s\n", pathname);
1323228753Smm		failure_finish(NULL);
1324228753Smm		return (0);
1325228753Smm	}
1326228753Smm	switch (type) {
1327228753Smm	case 'a': filet = st.st_atime; break;
1328228753Smm	case 'm': filet = st.st_mtime; break;
1329228753Smm	case 'b': filet = 0; break;
1330228753Smm	default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1331228753Smm		exit(1);
1332228753Smm	}
1333228753Smm#if defined(__FreeBSD__)
1334228753Smm	switch (type) {
1335228753Smm	case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1336228753Smm	case 'b': filet = st.st_birthtime;
1337302425Smm		/* FreeBSD filesystems that don't support birthtime
1338302425Smm		 * (e.g., UFS1) always return -1 here. */
1339302425Smm		if (filet == -1) {
1340302425Smm			return (1);
1341302425Smm		}
1342228753Smm		filet_nsec = st.st_birthtimespec.tv_nsec; break;
1343228753Smm	case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1344228753Smm	default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1345228753Smm		exit(1);
1346228753Smm	}
1347228753Smm	/* FreeBSD generally only stores to microsecond res, so round. */
1348228753Smm	filet_nsec = (filet_nsec / 1000) * 1000;
1349228753Smm	nsec = (nsec / 1000) * 1000;
1350228753Smm#else
1351228753Smm	filet_nsec = nsec = 0;	/* Generic POSIX only has whole seconds. */
1352228753Smm	if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1353228753Smm#if defined(__HAIKU__)
1354228753Smm	if (type == 'a') return (1); /* Haiku doesn't have atime. */
1355228753Smm#endif
1356228753Smm#endif
1357228753Smm#endif
1358228753Smm	if (recent) {
1359228753Smm		/* Check that requested time is up-to-date. */
1360228753Smm		time_t now = time(NULL);
1361228753Smm		if (filet < now - 10 || filet > now + 1) {
1362228753Smm			failure_start(file, line,
1363232153Smm			    "File %s has %ctime %lld, %lld seconds ago\n",
1364228753Smm			    pathname, type, filet, now - filet);
1365228753Smm			failure_finish(NULL);
1366228753Smm			return (0);
1367228753Smm		}
1368228753Smm	} else if (filet != t || filet_nsec != nsec) {
1369228753Smm		failure_start(file, line,
1370232153Smm		    "File %s has %ctime %lld.%09lld, expected %lld.%09lld",
1371228753Smm		    pathname, type, filet, filet_nsec, t, nsec);
1372228753Smm		failure_finish(NULL);
1373228753Smm		return (0);
1374228753Smm	}
1375228753Smm	return (1);
1376228753Smm}
1377228753Smm
1378228753Smm/* Verify atime of 'pathname'. */
1379228753Smmint
1380228753Smmassertion_file_atime(const char *file, int line,
1381228753Smm    const char *pathname, long t, long nsec)
1382228753Smm{
1383228753Smm	return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1384228753Smm}
1385228753Smm
1386228753Smm/* Verify atime of 'pathname' is up-to-date. */
1387228753Smmint
1388228753Smmassertion_file_atime_recent(const char *file, int line, const char *pathname)
1389228753Smm{
1390228753Smm	return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1391228753Smm}
1392228753Smm
1393228753Smm/* Verify birthtime of 'pathname'. */
1394228753Smmint
1395228753Smmassertion_file_birthtime(const char *file, int line,
1396228753Smm    const char *pathname, long t, long nsec)
1397228753Smm{
1398228753Smm	return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1399228753Smm}
1400228753Smm
1401228753Smm/* Verify birthtime of 'pathname' is up-to-date. */
1402228753Smmint
1403228753Smmassertion_file_birthtime_recent(const char *file, int line,
1404228753Smm    const char *pathname)
1405228753Smm{
1406228753Smm	return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1407228753Smm}
1408228753Smm
1409307798Smm/* Verify mode of 'pathname'. */
1410307798Smmint
1411307798Smmassertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1412307798Smm{
1413307798Smm	int mode;
1414307798Smm	int r;
1415307798Smm
1416307798Smm	assertion_count(file, line);
1417307798Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1418307798Smm	failure_start(file, line, "assertFileMode not yet implemented for Windows");
1419308152Smm	(void)mode; /* UNUSED */
1420308152Smm	(void)r; /* UNUSED */
1421313571Smm	(void)pathname; /* UNUSED */
1422313571Smm	(void)expected_mode; /* UNUSED */
1423307798Smm#else
1424307798Smm	{
1425307798Smm		struct stat st;
1426307798Smm		r = lstat(pathname, &st);
1427307798Smm		mode = (int)(st.st_mode & 0777);
1428307798Smm	}
1429307798Smm	if (r == 0 && mode == expected_mode)
1430307798Smm			return (1);
1431307798Smm	failure_start(file, line, "File %s has mode %o, expected %o",
1432307798Smm	    pathname, mode, expected_mode);
1433307798Smm#endif
1434307798Smm	failure_finish(NULL);
1435307798Smm	return (0);
1436307798Smm}
1437307798Smm
1438228753Smm/* Verify mtime of 'pathname'. */
1439228753Smmint
1440228753Smmassertion_file_mtime(const char *file, int line,
1441228753Smm    const char *pathname, long t, long nsec)
1442228753Smm{
1443228753Smm	return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1444228753Smm}
1445228753Smm
1446228753Smm/* Verify mtime of 'pathname' is up-to-date. */
1447228753Smmint
1448228753Smmassertion_file_mtime_recent(const char *file, int line, const char *pathname)
1449228753Smm{
1450228753Smm	return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1451228753Smm}
1452228753Smm
1453228753Smm/* Verify number of links to 'pathname'. */
1454228753Smmint
1455228753Smmassertion_file_nlinks(const char *file, int line,
1456228753Smm    const char *pathname, int nlinks)
1457228753Smm{
1458228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1459228753Smm	BY_HANDLE_FILE_INFORMATION bhfi;
1460228753Smm	int r;
1461228753Smm
1462228753Smm	assertion_count(file, line);
1463228753Smm	r = my_GetFileInformationByName(pathname, &bhfi);
1464228753Smm	if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1465228753Smm		return (1);
1466228753Smm	failure_start(file, line, "File %s has %d links, expected %d",
1467228753Smm	    pathname, bhfi.nNumberOfLinks, nlinks);
1468228753Smm	failure_finish(NULL);
1469228753Smm	return (0);
1470228753Smm#else
1471228753Smm	struct stat st;
1472228753Smm	int r;
1473228753Smm
1474228753Smm	assertion_count(file, line);
1475228753Smm	r = lstat(pathname, &st);
1476232153Smm	if (r == 0 && (int)st.st_nlink == nlinks)
1477307798Smm		return (1);
1478228753Smm	failure_start(file, line, "File %s has %d links, expected %d",
1479228753Smm	    pathname, st.st_nlink, nlinks);
1480228753Smm	failure_finish(NULL);
1481228753Smm	return (0);
1482228753Smm#endif
1483228753Smm}
1484228753Smm
1485228753Smm/* Verify size of 'pathname'. */
1486228753Smmint
1487228753Smmassertion_file_size(const char *file, int line, const char *pathname, long size)
1488228753Smm{
1489228753Smm	int64_t filesize;
1490228753Smm	int r;
1491228753Smm
1492228753Smm	assertion_count(file, line);
1493228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1494228753Smm	{
1495228753Smm		BY_HANDLE_FILE_INFORMATION bhfi;
1496228753Smm		r = !my_GetFileInformationByName(pathname, &bhfi);
1497228753Smm		filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1498228753Smm	}
1499228753Smm#else
1500228753Smm	{
1501228753Smm		struct stat st;
1502228753Smm		r = lstat(pathname, &st);
1503228753Smm		filesize = st.st_size;
1504228753Smm	}
1505228753Smm#endif
1506228753Smm	if (r == 0 && filesize == size)
1507228753Smm			return (1);
1508228753Smm	failure_start(file, line, "File %s has size %ld, expected %ld",
1509228753Smm	    pathname, (long)filesize, (long)size);
1510228753Smm	failure_finish(NULL);
1511228753Smm	return (0);
1512228753Smm}
1513228753Smm
1514228753Smm/* Assert that 'pathname' is a dir.  If mode >= 0, verify that too. */
1515228753Smmint
1516228753Smmassertion_is_dir(const char *file, int line, const char *pathname, int mode)
1517228753Smm{
1518228753Smm	struct stat st;
1519228753Smm	int r;
1520228753Smm
1521228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1522228753Smm	(void)mode; /* UNUSED */
1523228753Smm#endif
1524228753Smm	assertion_count(file, line);
1525228753Smm	r = lstat(pathname, &st);
1526228753Smm	if (r != 0) {
1527228753Smm		failure_start(file, line, "Dir should exist: %s", pathname);
1528228753Smm		failure_finish(NULL);
1529228753Smm		return (0);
1530228753Smm	}
1531228753Smm	if (!S_ISDIR(st.st_mode)) {
1532228753Smm		failure_start(file, line, "%s is not a dir", pathname);
1533228753Smm		failure_finish(NULL);
1534228753Smm		return (0);
1535228753Smm	}
1536228753Smm#if !defined(_WIN32) || defined(__CYGWIN__)
1537228753Smm	/* Windows doesn't handle permissions the same way as POSIX,
1538228753Smm	 * so just ignore the mode tests. */
1539228753Smm	/* TODO: Can we do better here? */
1540232153Smm	if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1541228753Smm		failure_start(file, line, "Dir %s has wrong mode", pathname);
1542228753Smm		logprintf("  Expected: 0%3o\n", mode);
1543228753Smm		logprintf("  Found: 0%3o\n", st.st_mode & 07777);
1544228753Smm		failure_finish(NULL);
1545228753Smm		return (0);
1546228753Smm	}
1547228753Smm#endif
1548228753Smm	return (1);
1549228753Smm}
1550228753Smm
1551228753Smm/* Verify that 'pathname' is a regular file.  If 'mode' is >= 0,
1552228753Smm * verify that too. */
1553228753Smmint
1554228753Smmassertion_is_reg(const char *file, int line, const char *pathname, int mode)
1555228753Smm{
1556228753Smm	struct stat st;
1557228753Smm	int r;
1558228753Smm
1559228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1560228753Smm	(void)mode; /* UNUSED */
1561228753Smm#endif
1562228753Smm	assertion_count(file, line);
1563228753Smm	r = lstat(pathname, &st);
1564228753Smm	if (r != 0 || !S_ISREG(st.st_mode)) {
1565228753Smm		failure_start(file, line, "File should exist: %s", pathname);
1566228753Smm		failure_finish(NULL);
1567228753Smm		return (0);
1568228753Smm	}
1569228753Smm#if !defined(_WIN32) || defined(__CYGWIN__)
1570228753Smm	/* Windows doesn't handle permissions the same way as POSIX,
1571228753Smm	 * so just ignore the mode tests. */
1572228753Smm	/* TODO: Can we do better here? */
1573232153Smm	if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1574228753Smm		failure_start(file, line, "File %s has wrong mode", pathname);
1575228753Smm		logprintf("  Expected: 0%3o\n", mode);
1576228753Smm		logprintf("  Found: 0%3o\n", st.st_mode & 07777);
1577228753Smm		failure_finish(NULL);
1578228753Smm		return (0);
1579228753Smm	}
1580228753Smm#endif
1581228753Smm	return (1);
1582228753Smm}
1583228753Smm
1584228753Smm/* Check whether 'pathname' is a symbolic link.  If 'contents' is
1585228753Smm * non-NULL, verify that the symlink has those contents. */
1586228753Smmstatic int
1587228753Smmis_symlink(const char *file, int line,
1588228753Smm    const char *pathname, const char *contents)
1589228753Smm{
1590228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1591228753Smm	(void)pathname; /* UNUSED */
1592228753Smm	(void)contents; /* UNUSED */
1593228753Smm	assertion_count(file, line);
1594228753Smm	/* Windows sort-of has real symlinks, but they're only usable
1595228753Smm	 * by privileged users and are crippled even then, so there's
1596228753Smm	 * really not much point in bothering with this. */
1597228753Smm	return (0);
1598228753Smm#else
1599228753Smm	char buff[300];
1600228753Smm	struct stat st;
1601228753Smm	ssize_t linklen;
1602228753Smm	int r;
1603228753Smm
1604228753Smm	assertion_count(file, line);
1605228753Smm	r = lstat(pathname, &st);
1606228753Smm	if (r != 0) {
1607228753Smm		failure_start(file, line,
1608228753Smm		    "Symlink should exist: %s", pathname);
1609228753Smm		failure_finish(NULL);
1610228753Smm		return (0);
1611228753Smm	}
1612228753Smm	if (!S_ISLNK(st.st_mode))
1613228753Smm		return (0);
1614228753Smm	if (contents == NULL)
1615228753Smm		return (1);
1616228753Smm	linklen = readlink(pathname, buff, sizeof(buff));
1617228753Smm	if (linklen < 0) {
1618228753Smm		failure_start(file, line, "Can't read symlink %s", pathname);
1619228753Smm		failure_finish(NULL);
1620228753Smm		return (0);
1621228753Smm	}
1622228753Smm	buff[linklen] = '\0';
1623228753Smm	if (strcmp(buff, contents) != 0)
1624228753Smm		return (0);
1625228753Smm	return (1);
1626228753Smm#endif
1627228753Smm}
1628228753Smm
1629228753Smm/* Assert that path is a symlink that (optionally) contains contents. */
1630228753Smmint
1631228753Smmassertion_is_symlink(const char *file, int line,
1632228753Smm    const char *path, const char *contents)
1633228753Smm{
1634228753Smm	if (is_symlink(file, line, path, contents))
1635228753Smm		return (1);
1636228753Smm	if (contents)
1637228753Smm		failure_start(file, line, "File %s is not a symlink to %s",
1638228753Smm		    path, contents);
1639228753Smm	else
1640228753Smm		failure_start(file, line, "File %s is not a symlink", path);
1641228753Smm	failure_finish(NULL);
1642228753Smm	return (0);
1643228753Smm}
1644228753Smm
1645228753Smm
1646228753Smm/* Create a directory and report any errors. */
1647228753Smmint
1648228753Smmassertion_make_dir(const char *file, int line, const char *dirname, int mode)
1649228753Smm{
1650228753Smm	assertion_count(file, line);
1651228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1652228753Smm	(void)mode; /* UNUSED */
1653228753Smm	if (0 == _mkdir(dirname))
1654228753Smm		return (1);
1655228753Smm#else
1656307139Smm	if (0 == mkdir(dirname, mode)) {
1657307139Smm		if (0 == chmod(dirname, mode)) {
1658307139Smm			assertion_file_mode(file, line, dirname, mode);
1659307139Smm			return (1);
1660307139Smm		}
1661307139Smm	}
1662228753Smm#endif
1663228753Smm	failure_start(file, line, "Could not create directory %s", dirname);
1664228753Smm	failure_finish(NULL);
1665228753Smm	return(0);
1666228753Smm}
1667228753Smm
1668228753Smm/* Create a file with the specified contents and report any failures. */
1669228753Smmint
1670228753Smmassertion_make_file(const char *file, int line,
1671238856Smm    const char *path, int mode, int csize, const void *contents)
1672228753Smm{
1673228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1674228753Smm	/* TODO: Rework this to set file mode as well. */
1675228753Smm	FILE *f;
1676228753Smm	(void)mode; /* UNUSED */
1677228753Smm	assertion_count(file, line);
1678228753Smm	f = fopen(path, "wb");
1679228753Smm	if (f == NULL) {
1680228753Smm		failure_start(file, line, "Could not create file %s", path);
1681228753Smm		failure_finish(NULL);
1682228753Smm		return (0);
1683228753Smm	}
1684228753Smm	if (contents != NULL) {
1685238856Smm		size_t wsize;
1686238856Smm
1687238856Smm		if (csize < 0)
1688238856Smm			wsize = strlen(contents);
1689238856Smm		else
1690238856Smm			wsize = (size_t)csize;
1691238856Smm		if (wsize != fwrite(contents, 1, wsize, f)) {
1692228753Smm			fclose(f);
1693228753Smm			failure_start(file, line,
1694228753Smm			    "Could not write file %s", path);
1695228753Smm			failure_finish(NULL);
1696228753Smm			return (0);
1697228753Smm		}
1698228753Smm	}
1699228753Smm	fclose(f);
1700228753Smm	return (1);
1701228753Smm#else
1702228753Smm	int fd;
1703228753Smm	assertion_count(file, line);
1704228753Smm	fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1705228753Smm	if (fd < 0) {
1706228753Smm		failure_start(file, line, "Could not create %s", path);
1707228753Smm		failure_finish(NULL);
1708228753Smm		return (0);
1709228753Smm	}
1710307139Smm	if (0 != chmod(path, mode)) {
1711307139Smm		failure_start(file, line, "Could not chmod %s", path);
1712307139Smm		failure_finish(NULL);
1713307798Smm		close(fd);
1714307139Smm		return (0);
1715307139Smm	}
1716228753Smm	if (contents != NULL) {
1717238856Smm		ssize_t wsize;
1718238856Smm
1719238856Smm		if (csize < 0)
1720238856Smm			wsize = (ssize_t)strlen(contents);
1721238856Smm		else
1722238856Smm			wsize = (ssize_t)csize;
1723238856Smm		if (wsize != write(fd, contents, wsize)) {
1724228753Smm			close(fd);
1725238856Smm			failure_start(file, line,
1726238856Smm			    "Could not write to %s", path);
1727228753Smm			failure_finish(NULL);
1728307798Smm			close(fd);
1729228753Smm			return (0);
1730228753Smm		}
1731228753Smm	}
1732228753Smm	close(fd);
1733307139Smm	assertion_file_mode(file, line, path, mode);
1734228753Smm	return (1);
1735228753Smm#endif
1736228753Smm}
1737228753Smm
1738228753Smm/* Create a hardlink and report any failures. */
1739228753Smmint
1740228753Smmassertion_make_hardlink(const char *file, int line,
1741228753Smm    const char *newpath, const char *linkto)
1742228753Smm{
1743228753Smm	int succeeded;
1744228753Smm
1745228753Smm	assertion_count(file, line);
1746228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1747228753Smm	succeeded = my_CreateHardLinkA(newpath, linkto);
1748228753Smm#elif HAVE_LINK
1749228753Smm	succeeded = !link(linkto, newpath);
1750228753Smm#else
1751228753Smm	succeeded = 0;
1752228753Smm#endif
1753228753Smm	if (succeeded)
1754228753Smm		return (1);
1755228753Smm	failure_start(file, line, "Could not create hardlink");
1756228753Smm	logprintf("   New link: %s\n", newpath);
1757228753Smm	logprintf("   Old name: %s\n", linkto);
1758228753Smm	failure_finish(NULL);
1759228753Smm	return(0);
1760228753Smm}
1761228753Smm
1762228753Smm/* Create a symlink and report any failures. */
1763228753Smmint
1764228753Smmassertion_make_symlink(const char *file, int line,
1765228753Smm    const char *newpath, const char *linkto)
1766228753Smm{
1767228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1768228753Smm	int targetIsDir = 0;  /* TODO: Fix this */
1769228753Smm	assertion_count(file, line);
1770228753Smm	if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
1771228753Smm		return (1);
1772228753Smm#elif HAVE_SYMLINK
1773228753Smm	assertion_count(file, line);
1774228753Smm	if (0 == symlink(linkto, newpath))
1775228753Smm		return (1);
1776228753Smm#endif
1777228753Smm	failure_start(file, line, "Could not create symlink");
1778228753Smm	logprintf("   New link: %s\n", newpath);
1779228753Smm	logprintf("   Old name: %s\n", linkto);
1780228753Smm	failure_finish(NULL);
1781228753Smm	return(0);
1782228753Smm}
1783228753Smm
1784228753Smm/* Set umask, report failures. */
1785228753Smmint
1786228753Smmassertion_umask(const char *file, int line, int mask)
1787228753Smm{
1788228753Smm	assertion_count(file, line);
1789228753Smm	(void)file; /* UNUSED */
1790228753Smm	(void)line; /* UNUSED */
1791228753Smm	umask(mask);
1792228753Smm	return (1);
1793228753Smm}
1794228753Smm
1795232153Smm/* Set times, report failures. */
1796232153Smmint
1797232153Smmassertion_utimes(const char *file, int line,
1798232153Smm    const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
1799232153Smm{
1800232153Smm	int r;
1801232153Smm
1802232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1803232153Smm#define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
1804232153Smm	 + (((nsec)/1000)*10))
1805232153Smm	HANDLE h;
1806232153Smm	ULARGE_INTEGER wintm;
1807232153Smm	FILETIME fatime, fmtime;
1808232153Smm	FILETIME *pat, *pmt;
1809232153Smm
1810232153Smm	assertion_count(file, line);
1811232153Smm	h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
1812232153Smm		    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1813232153Smm		    FILE_FLAG_BACKUP_SEMANTICS, NULL);
1814232153Smm	if (h == INVALID_HANDLE_VALUE) {
1815232153Smm		failure_start(file, line, "Can't access %s\n", pathname);
1816232153Smm		failure_finish(NULL);
1817232153Smm		return (0);
1818232153Smm	}
1819232153Smm
1820232153Smm	if (at > 0 || at_nsec > 0) {
1821232153Smm		wintm.QuadPart = WINTIME(at, at_nsec);
1822232153Smm		fatime.dwLowDateTime = wintm.LowPart;
1823232153Smm		fatime.dwHighDateTime = wintm.HighPart;
1824232153Smm		pat = &fatime;
1825232153Smm	} else
1826232153Smm		pat = NULL;
1827232153Smm	if (mt > 0 || mt_nsec > 0) {
1828232153Smm		wintm.QuadPart = WINTIME(mt, mt_nsec);
1829232153Smm		fmtime.dwLowDateTime = wintm.LowPart;
1830232153Smm		fmtime.dwHighDateTime = wintm.HighPart;
1831232153Smm		pmt = &fmtime;
1832232153Smm	} else
1833232153Smm		pmt = NULL;
1834232153Smm	if (pat != NULL || pmt != NULL)
1835232153Smm		r = SetFileTime(h, NULL, pat, pmt);
1836232153Smm	else
1837232153Smm		r = 1;
1838232153Smm	CloseHandle(h);
1839232153Smm	if (r == 0) {
1840232153Smm		failure_start(file, line, "Can't SetFileTime %s\n", pathname);
1841232153Smm		failure_finish(NULL);
1842232153Smm		return (0);
1843232153Smm	}
1844232153Smm	return (1);
1845232153Smm#else /* defined(_WIN32) && !defined(__CYGWIN__) */
1846232153Smm	struct stat st;
1847232153Smm	struct timeval times[2];
1848232153Smm
1849232153Smm#if !defined(__FreeBSD__)
1850232153Smm	mt_nsec = at_nsec = 0;	/* Generic POSIX only has whole seconds. */
1851232153Smm#endif
1852232153Smm	if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
1853232153Smm		return (1);
1854232153Smm
1855232153Smm	r = lstat(pathname, &st);
1856232153Smm	if (r < 0) {
1857232153Smm		failure_start(file, line, "Can't stat %s\n", pathname);
1858232153Smm		failure_finish(NULL);
1859232153Smm		return (0);
1860232153Smm	}
1861232153Smm
1862232153Smm	if (mt == 0 && mt_nsec == 0) {
1863232153Smm		mt = st.st_mtime;
1864232153Smm#if defined(__FreeBSD__)
1865232153Smm		mt_nsec = st.st_mtimespec.tv_nsec;
1866232153Smm		/* FreeBSD generally only stores to microsecond res, so round. */
1867232153Smm		mt_nsec = (mt_nsec / 1000) * 1000;
1868232153Smm#endif
1869232153Smm	}
1870232153Smm	if (at == 0 && at_nsec == 0) {
1871232153Smm		at = st.st_atime;
1872232153Smm#if defined(__FreeBSD__)
1873232153Smm		at_nsec = st.st_atimespec.tv_nsec;
1874232153Smm		/* FreeBSD generally only stores to microsecond res, so round. */
1875232153Smm		at_nsec = (at_nsec / 1000) * 1000;
1876232153Smm#endif
1877232153Smm	}
1878232153Smm
1879232153Smm	times[1].tv_sec = mt;
1880232153Smm	times[1].tv_usec = mt_nsec / 1000;
1881232153Smm
1882232153Smm	times[0].tv_sec = at;
1883232153Smm	times[0].tv_usec = at_nsec / 1000;
1884232153Smm
1885232153Smm#ifdef HAVE_LUTIMES
1886232153Smm	r = lutimes(pathname, times);
1887232153Smm#else
1888232153Smm	r = utimes(pathname, times);
1889232153Smm#endif
1890232153Smm	if (r < 0) {
1891232153Smm		failure_start(file, line, "Can't utimes %s\n", pathname);
1892232153Smm		failure_finish(NULL);
1893232153Smm		return (0);
1894232153Smm	}
1895232153Smm	return (1);
1896232153Smm#endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1897232153Smm}
1898232153Smm
1899238856Smm/* Set nodump, report failures. */
1900238856Smmint
1901238856Smmassertion_nodump(const char *file, int line, const char *pathname)
1902238856Smm{
1903238856Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
1904238856Smm	int r;
1905238856Smm
1906238856Smm	assertion_count(file, line);
1907238856Smm	r = chflags(pathname, UF_NODUMP);
1908238856Smm	if (r < 0) {
1909238856Smm		failure_start(file, line, "Can't set nodump %s\n", pathname);
1910238856Smm		failure_finish(NULL);
1911238856Smm		return (0);
1912238856Smm	}
1913238856Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
1914238856Smm	 && defined(EXT2_NODUMP_FL)
1915238856Smm	int fd, r, flags;
1916238856Smm
1917238856Smm	assertion_count(file, line);
1918238856Smm	fd = open(pathname, O_RDONLY | O_NONBLOCK);
1919238856Smm	if (fd < 0) {
1920238856Smm		failure_start(file, line, "Can't open %s\n", pathname);
1921238856Smm		failure_finish(NULL);
1922238856Smm		return (0);
1923238856Smm	}
1924238856Smm	r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
1925238856Smm	if (r < 0) {
1926238856Smm		failure_start(file, line, "Can't get flags %s\n", pathname);
1927238856Smm		failure_finish(NULL);
1928238856Smm		return (0);
1929238856Smm	}
1930238856Smm	flags |= EXT2_NODUMP_FL;
1931238856Smm	r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
1932238856Smm	if (r < 0) {
1933238856Smm		failure_start(file, line, "Can't set nodump %s\n", pathname);
1934238856Smm		failure_finish(NULL);
1935238856Smm		return (0);
1936238856Smm	}
1937238856Smm	close(fd);
1938238856Smm#else
1939238856Smm	(void)pathname; /* UNUSED */
1940238856Smm	assertion_count(file, line);
1941238856Smm#endif
1942238856Smm	return (1);
1943238856Smm}
1944238856Smm
1945228753Smm/*
1946228753Smm *
1947228753Smm *  UTILITIES for use by tests.
1948228753Smm *
1949228753Smm */
1950228753Smm
1951228753Smm/*
1952228753Smm * Check whether platform supports symlinks.  This is intended
1953228753Smm * for tests to use in deciding whether to bother testing symlink
1954228753Smm * support; if the platform doesn't support symlinks, there's no point
1955228753Smm * in checking whether the program being tested can create them.
1956228753Smm *
1957228753Smm * Note that the first time this test is called, we actually go out to
1958228753Smm * disk to create and verify a symlink.  This is necessary because
1959228753Smm * symlink support is actually a property of a particular filesystem
1960228753Smm * and can thus vary between directories on a single system.  After
1961228753Smm * the first call, this returns the cached result from memory, so it's
1962228753Smm * safe to call it as often as you wish.
1963228753Smm */
1964228753Smmint
1965228753SmmcanSymlink(void)
1966228753Smm{
1967228753Smm	/* Remember the test result */
1968228753Smm	static int value = 0, tested = 0;
1969228753Smm	if (tested)
1970228753Smm		return (value);
1971228753Smm
1972228753Smm	++tested;
1973238856Smm	assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
1974228753Smm	/* Note: Cygwin has its own symlink() emulation that does not
1975228753Smm	 * use the Win32 CreateSymbolicLink() function. */
1976228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1977228753Smm	value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
1978228753Smm	    && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0");
1979228753Smm#elif HAVE_SYMLINK
1980228753Smm	value = (0 == symlink("canSymlink.0", "canSymlink.1"))
1981228753Smm	    && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0");
1982228753Smm#endif
1983228753Smm	return (value);
1984228753Smm}
1985228753Smm
1986228753Smm/* Platform-dependent options for hiding the output of a subcommand. */
1987228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
1988228753Smmstatic const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
1989228753Smm#else
1990228753Smmstatic const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
1991228753Smm#endif
1992248616Smm/*
1993248616Smm * Can this platform run the bzip2 program?
1994248616Smm */
1995228753Smmint
1996248616SmmcanBzip2(void)
1997248616Smm{
1998248616Smm	static int tested = 0, value = 0;
1999248616Smm	if (!tested) {
2000248616Smm		tested = 1;
2001248616Smm		if (systemf("bzip2 -d -V %s", redirectArgs) == 0)
2002248616Smm			value = 1;
2003248616Smm	}
2004248616Smm	return (value);
2005248616Smm}
2006248616Smm
2007248616Smm/*
2008248616Smm * Can this platform run the grzip program?
2009248616Smm */
2010248616Smmint
2011248616SmmcanGrzip(void)
2012248616Smm{
2013248616Smm	static int tested = 0, value = 0;
2014248616Smm	if (!tested) {
2015248616Smm		tested = 1;
2016248616Smm		if (systemf("grzip -V %s", redirectArgs) == 0)
2017248616Smm			value = 1;
2018248616Smm	}
2019248616Smm	return (value);
2020248616Smm}
2021248616Smm
2022248616Smm/*
2023248616Smm * Can this platform run the gzip program?
2024248616Smm */
2025248616Smmint
2026228753SmmcanGzip(void)
2027228753Smm{
2028228753Smm	static int tested = 0, value = 0;
2029228753Smm	if (!tested) {
2030228753Smm		tested = 1;
2031228753Smm		if (systemf("gzip -V %s", redirectArgs) == 0)
2032228753Smm			value = 1;
2033228753Smm	}
2034228753Smm	return (value);
2035228753Smm}
2036228753Smm
2037228753Smm/*
2038248616Smm * Can this platform run the lrzip program?
2039228753Smm */
2040228753Smmint
2041248616SmmcanRunCommand(const char *cmd)
2042228753Smm{
2043248616Smm  static int tested = 0, value = 0;
2044248616Smm  if (!tested) {
2045248616Smm    tested = 1;
2046248616Smm    if (systemf("%s %s", cmd, redirectArgs) == 0)
2047248616Smm      value = 1;
2048248616Smm  }
2049248616Smm  return (value);
2050248616Smm}
2051248616Smm
2052248616Smmint
2053248616SmmcanLrzip(void)
2054248616Smm{
2055228753Smm	static int tested = 0, value = 0;
2056228753Smm	if (!tested) {
2057228753Smm		tested = 1;
2058248616Smm		if (systemf("lrzip -V %s", redirectArgs) == 0)
2059228753Smm			value = 1;
2060228753Smm	}
2061228753Smm	return (value);
2062228753Smm}
2063228753Smm
2064228753Smm/*
2065302001Smm * Can this platform run the lz4 program?
2066302001Smm */
2067302001Smmint
2068302001SmmcanLz4(void)
2069302001Smm{
2070302001Smm	static int tested = 0, value = 0;
2071302001Smm	if (!tested) {
2072302001Smm		tested = 1;
2073302001Smm		if (systemf("lz4 -V %s", redirectArgs) == 0)
2074302001Smm			value = 1;
2075302001Smm	}
2076302001Smm	return (value);
2077302001Smm}
2078302001Smm
2079302001Smm/*
2080248616Smm * Can this platform run the lzip program?
2081248616Smm */
2082248616Smmint
2083248616SmmcanLzip(void)
2084248616Smm{
2085248616Smm	static int tested = 0, value = 0;
2086248616Smm	if (!tested) {
2087248616Smm		tested = 1;
2088248616Smm		if (systemf("lzip -V %s", redirectArgs) == 0)
2089248616Smm			value = 1;
2090248616Smm	}
2091248616Smm	return (value);
2092248616Smm}
2093248616Smm
2094248616Smm/*
2095248616Smm * Can this platform run the lzma program?
2096248616Smm */
2097248616Smmint
2098248616SmmcanLzma(void)
2099248616Smm{
2100248616Smm	static int tested = 0, value = 0;
2101248616Smm	if (!tested) {
2102248616Smm		tested = 1;
2103248616Smm		if (systemf("lzma -V %s", redirectArgs) == 0)
2104248616Smm			value = 1;
2105248616Smm	}
2106248616Smm	return (value);
2107248616Smm}
2108248616Smm
2109248616Smm/*
2110248616Smm * Can this platform run the lzop program?
2111248616Smm */
2112248616Smmint
2113248616SmmcanLzop(void)
2114248616Smm{
2115248616Smm	static int tested = 0, value = 0;
2116248616Smm	if (!tested) {
2117248616Smm		tested = 1;
2118248616Smm		if (systemf("lzop -V %s", redirectArgs) == 0)
2119248616Smm			value = 1;
2120248616Smm	}
2121248616Smm	return (value);
2122248616Smm}
2123248616Smm
2124248616Smm/*
2125248616Smm * Can this platform run the xz program?
2126248616Smm */
2127248616Smmint
2128248616SmmcanXz(void)
2129248616Smm{
2130248616Smm	static int tested = 0, value = 0;
2131248616Smm	if (!tested) {
2132248616Smm		tested = 1;
2133248616Smm		if (systemf("xz -V %s", redirectArgs) == 0)
2134248616Smm			value = 1;
2135248616Smm	}
2136248616Smm	return (value);
2137248616Smm}
2138248616Smm
2139248616Smm/*
2140238856Smm * Can this filesystem handle nodump flags.
2141238856Smm */
2142238856Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2143238856Smm
2144238856Smmint
2145238856SmmcanNodump(void)
2146238856Smm{
2147238856Smm	const char *path = "cannodumptest";
2148238856Smm	struct stat sb;
2149238856Smm
2150238856Smm	assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2151238856Smm	if (chflags(path, UF_NODUMP) < 0)
2152238856Smm		return (0);
2153238856Smm	if (stat(path, &sb) < 0)
2154238856Smm		return (0);
2155238856Smm	if (sb.st_flags & UF_NODUMP)
2156238856Smm		return (1);
2157238856Smm	return (0);
2158238856Smm}
2159238856Smm
2160238856Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\
2161238856Smm	 && defined(EXT2_NODUMP_FL)
2162238856Smm
2163238856Smmint
2164238856SmmcanNodump(void)
2165238856Smm{
2166238856Smm	const char *path = "cannodumptest";
2167238856Smm	int fd, r, flags;
2168238856Smm
2169238856Smm	assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2170238856Smm	fd = open(path, O_RDONLY | O_NONBLOCK);
2171238856Smm	if (fd < 0)
2172238856Smm		return (0);
2173238856Smm	r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2174238856Smm	if (r < 0)
2175238856Smm		return (0);
2176238856Smm	flags |= EXT2_NODUMP_FL;
2177238856Smm	r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
2178238856Smm	if (r < 0)
2179238856Smm		return (0);
2180238856Smm	close(fd);
2181238856Smm	fd = open(path, O_RDONLY | O_NONBLOCK);
2182238856Smm	if (fd < 0)
2183238856Smm		return (0);
2184238856Smm	r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
2185238856Smm	if (r < 0)
2186238856Smm		return (0);
2187238856Smm	close(fd);
2188238856Smm	if (flags & EXT2_NODUMP_FL)
2189238856Smm		return (1);
2190238856Smm	return (0);
2191238856Smm}
2192238856Smm
2193238856Smm#else
2194238856Smm
2195238856Smmint
2196238856SmmcanNodump()
2197238856Smm{
2198238856Smm	return (0);
2199238856Smm}
2200238856Smm
2201238856Smm#endif
2202238856Smm
2203238856Smm/*
2204228753Smm * Sleep as needed; useful for verifying disk timestamp changes by
2205228753Smm * ensuring that the wall-clock time has actually changed before we
2206228753Smm * go back to re-read something from disk.
2207228753Smm */
2208228753Smmvoid
2209228753SmmsleepUntilAfter(time_t t)
2210228753Smm{
2211228753Smm	while (t >= time(NULL))
2212228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2213228753Smm		Sleep(500);
2214228753Smm#else
2215228753Smm		sleep(1);
2216228753Smm#endif
2217228753Smm}
2218228753Smm
2219228753Smm/*
2220228753Smm * Call standard system() call, but build up the command line using
2221228753Smm * sprintf() conventions.
2222228753Smm */
2223228753Smmint
2224228753Smmsystemf(const char *fmt, ...)
2225228753Smm{
2226228753Smm	char buff[8192];
2227228753Smm	va_list ap;
2228228753Smm	int r;
2229228753Smm
2230228753Smm	va_start(ap, fmt);
2231228753Smm	vsprintf(buff, fmt, ap);
2232228753Smm	if (verbosity > VERBOSITY_FULL)
2233228753Smm		logprintf("Cmd: %s\n", buff);
2234228753Smm	r = system(buff);
2235228753Smm	va_end(ap);
2236228753Smm	return (r);
2237228753Smm}
2238228753Smm
2239228753Smm/*
2240228753Smm * Slurp a file into memory for ease of comparison and testing.
2241228753Smm * Returns size of file in 'sizep' if non-NULL, null-terminates
2242228753Smm * data in memory for ease of use.
2243228753Smm */
2244228753Smmchar *
2245228753Smmslurpfile(size_t * sizep, const char *fmt, ...)
2246228753Smm{
2247228753Smm	char filename[8192];
2248228753Smm	struct stat st;
2249228753Smm	va_list ap;
2250228753Smm	char *p;
2251228753Smm	ssize_t bytes_read;
2252228753Smm	FILE *f;
2253228753Smm	int r;
2254228753Smm
2255228753Smm	va_start(ap, fmt);
2256228753Smm	vsprintf(filename, fmt, ap);
2257228753Smm	va_end(ap);
2258228753Smm
2259228753Smm	f = fopen(filename, "rb");
2260228753Smm	if (f == NULL) {
2261228753Smm		/* Note: No error; non-existent file is okay here. */
2262228753Smm		return (NULL);
2263228753Smm	}
2264228753Smm	r = fstat(fileno(f), &st);
2265228753Smm	if (r != 0) {
2266228753Smm		logprintf("Can't stat file %s\n", filename);
2267228753Smm		fclose(f);
2268228753Smm		return (NULL);
2269228753Smm	}
2270228753Smm	p = malloc((size_t)st.st_size + 1);
2271228753Smm	if (p == NULL) {
2272228753Smm		logprintf("Can't allocate %ld bytes of memory to read file %s\n",
2273228753Smm		    (long int)st.st_size, filename);
2274228753Smm		fclose(f);
2275228753Smm		return (NULL);
2276228753Smm	}
2277228753Smm	bytes_read = fread(p, 1, (size_t)st.st_size, f);
2278228753Smm	if (bytes_read < st.st_size) {
2279228753Smm		logprintf("Can't read file %s\n", filename);
2280228753Smm		fclose(f);
2281228753Smm		free(p);
2282228753Smm		return (NULL);
2283228753Smm	}
2284228753Smm	p[st.st_size] = '\0';
2285228753Smm	if (sizep != NULL)
2286228753Smm		*sizep = (size_t)st.st_size;
2287228753Smm	fclose(f);
2288228753Smm	return (p);
2289228753Smm}
2290228753Smm
2291302001Smm/*
2292302001Smm * Slurp a file into memory for ease of comparison and testing.
2293302001Smm * Returns size of file in 'sizep' if non-NULL, null-terminates
2294302001Smm * data in memory for ease of use.
2295302001Smm */
2296302001Smmvoid
2297302001Smmdumpfile(const char *filename, void *data, size_t len)
2298302001Smm{
2299302001Smm	ssize_t bytes_written;
2300302001Smm	FILE *f;
2301302001Smm
2302302001Smm	f = fopen(filename, "wb");
2303302001Smm	if (f == NULL) {
2304302001Smm		logprintf("Can't open file %s for writing\n", filename);
2305302001Smm		return;
2306302001Smm	}
2307302001Smm	bytes_written = fwrite(data, 1, len, f);
2308302001Smm	if (bytes_written < (ssize_t)len)
2309302001Smm		logprintf("Can't write file %s\n", filename);
2310302001Smm	fclose(f);
2311302001Smm}
2312302001Smm
2313228753Smm/* Read a uuencoded file from the reference directory, decode, and
2314228753Smm * write the result into the current directory. */
2315302001Smm#define VALID_UUDECODE(c) (c >= 32 && c <= 96)
2316228753Smm#define	UUDECODE(c) (((c) - 0x20) & 0x3f)
2317228753Smmvoid
2318228753Smmextract_reference_file(const char *name)
2319228753Smm{
2320228753Smm	char buff[1024];
2321228753Smm	FILE *in, *out;
2322228753Smm
2323228753Smm	sprintf(buff, "%s/%s.uu", refdir, name);
2324228753Smm	in = fopen(buff, "r");
2325228753Smm	failure("Couldn't open reference file %s", buff);
2326228753Smm	assert(in != NULL);
2327228753Smm	if (in == NULL)
2328228753Smm		return;
2329228753Smm	/* Read up to and including the 'begin' line. */
2330228753Smm	for (;;) {
2331228753Smm		if (fgets(buff, sizeof(buff), in) == NULL) {
2332228753Smm			/* TODO: This is a failure. */
2333228753Smm			return;
2334228753Smm		}
2335228753Smm		if (memcmp(buff, "begin ", 6) == 0)
2336228753Smm			break;
2337228753Smm	}
2338228753Smm	/* Now, decode the rest and write it. */
2339228753Smm	out = fopen(name, "wb");
2340228753Smm	while (fgets(buff, sizeof(buff), in) != NULL) {
2341228753Smm		char *p = buff;
2342228753Smm		int bytes;
2343228753Smm
2344228753Smm		if (memcmp(buff, "end", 3) == 0)
2345228753Smm			break;
2346228753Smm
2347228753Smm		bytes = UUDECODE(*p++);
2348228753Smm		while (bytes > 0) {
2349228753Smm			int n = 0;
2350228753Smm			/* Write out 1-3 bytes from that. */
2351228753Smm			if (bytes > 0) {
2352302001Smm				assert(VALID_UUDECODE(p[0]));
2353302001Smm				assert(VALID_UUDECODE(p[1]));
2354228753Smm				n = UUDECODE(*p++) << 18;
2355228753Smm				n |= UUDECODE(*p++) << 12;
2356228753Smm				fputc(n >> 16, out);
2357228753Smm				--bytes;
2358228753Smm			}
2359228753Smm			if (bytes > 0) {
2360302001Smm				assert(VALID_UUDECODE(p[0]));
2361228753Smm				n |= UUDECODE(*p++) << 6;
2362228753Smm				fputc((n >> 8) & 0xFF, out);
2363228753Smm				--bytes;
2364228753Smm			}
2365228753Smm			if (bytes > 0) {
2366302001Smm				assert(VALID_UUDECODE(p[0]));
2367228753Smm				n |= UUDECODE(*p++);
2368228753Smm				fputc(n & 0xFF, out);
2369228753Smm				--bytes;
2370228753Smm			}
2371228753Smm		}
2372228753Smm	}
2373228753Smm	fclose(out);
2374228753Smm	fclose(in);
2375228753Smm}
2376228753Smm
2377302001Smmvoid
2378302001Smmcopy_reference_file(const char *name)
2379302001Smm{
2380302001Smm	char buff[1024];
2381302001Smm	FILE *in, *out;
2382302001Smm	size_t rbytes;
2383302001Smm
2384302001Smm	sprintf(buff, "%s/%s", refdir, name);
2385302001Smm	in = fopen(buff, "rb");
2386302001Smm	failure("Couldn't open reference file %s", buff);
2387302001Smm	assert(in != NULL);
2388302001Smm	if (in == NULL)
2389302001Smm		return;
2390302001Smm	/* Now, decode the rest and write it. */
2391302001Smm	/* Not a lot of error checking here; the input better be right. */
2392302001Smm	out = fopen(name, "wb");
2393302001Smm	while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
2394302001Smm		if (fwrite(buff, 1, rbytes, out) != rbytes) {
2395302001Smm			logprintf("Error: fwrite\n");
2396302001Smm			break;
2397302001Smm		}
2398302001Smm	}
2399302001Smm	fclose(out);
2400302001Smm	fclose(in);
2401302001Smm}
2402302001Smm
2403232153Smmint
2404232153Smmis_LargeInode(const char *file)
2405232153Smm{
2406232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2407232153Smm	BY_HANDLE_FILE_INFORMATION bhfi;
2408232153Smm	int r;
2409232153Smm
2410232153Smm	r = my_GetFileInformationByName(file, &bhfi);
2411232153Smm	if (r != 0)
2412232153Smm		return (0);
2413232153Smm	return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
2414232153Smm#else
2415232153Smm	struct stat st;
2416232153Smm	int64_t ino;
2417232153Smm
2418232153Smm	if (stat(file, &st) < 0)
2419232153Smm		return (0);
2420232153Smm	ino = (int64_t)st.st_ino;
2421232153Smm	return (ino > 0xffffffff);
2422232153Smm#endif
2423232153Smm}
2424248616Smm
2425248616Smmvoid
2426248616Smmextract_reference_files(const char **names)
2427248616Smm{
2428248616Smm	while (names && *names)
2429248616Smm		extract_reference_file(*names++);
2430248616Smm}
2431248616Smm
2432313571Smm/* Set ACLs */
2433313571Smmvoid
2434313571Smmarchive_test_set_acls(struct archive_entry *ae,
2435313571Smm    struct archive_test_acl_t *acls, int n)
2436313571Smm{
2437313571Smm	int i;
2438313571Smm
2439313571Smm	archive_entry_acl_clear(ae);
2440313571Smm	for (i = 0; i < n; i++) {
2441313571Smm		failure("type=%#010x, permset=%#010x, tag=%d, qual=%d name=%s",
2442313571Smm		    acls[i].type, acls[i].permset, acls[i].tag,
2443313571Smm		    acls[i].qual, acls[i].name);
2444313571Smm		assertEqualInt(ARCHIVE_OK,
2445313571Smm		    archive_entry_acl_add_entry(ae,
2446313571Smm			acls[i].type, acls[i].permset, acls[i].tag,
2447313571Smm			acls[i].qual, acls[i].name));
2448313571Smm	}
2449313571Smm}
2450313571Smm
2451313571Smmstatic int
2452313571Smmarchive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset,
2453313571Smm    int tag, int qual, const char *name)
2454313571Smm{
2455313571Smm	if (type != acl->type)
2456313571Smm		return (0);
2457313571Smm	if (permset != acl->permset)
2458313571Smm		return (0);
2459313571Smm	if (tag != acl->tag)
2460313571Smm		return (0);
2461313571Smm	if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
2462313571Smm		return (1);
2463313571Smm	if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
2464313571Smm		return (1);
2465313571Smm	if (tag == ARCHIVE_ENTRY_ACL_EVERYONE)
2466313571Smm		return (1);
2467313571Smm	if (tag == ARCHIVE_ENTRY_ACL_OTHER)
2468313571Smm		return (1);
2469313571Smm	if (qual != acl->qual)
2470313571Smm		return (0);
2471313571Smm	if (name == NULL) {
2472313571Smm		if (acl->name == NULL || acl->name[0] == '\0')
2473313571Smm			return (1);
2474313571Smm		return (0);
2475313571Smm	}
2476313571Smm	if (acl->name == NULL) {
2477313571Smm		if (name[0] == '\0')
2478313571Smm			return (1);
2479313571Smm		return (0);
2480313571Smm	}
2481313571Smm	return (0 == strcmp(name, acl->name));
2482313571Smm}
2483313571Smm
2484313571Smm/* Compare ACLs */
2485313571Smmvoid
2486313571Smmarchive_test_compare_acls(struct archive_entry *ae,
2487313571Smm    struct archive_test_acl_t *acls, int cnt, int want_type, int mode)
2488313571Smm{
2489313571Smm	int *marker;
2490313571Smm	int i, r, n;
2491313571Smm	int type, permset, tag, qual;
2492313571Smm	int matched;
2493313571Smm	const char *name;
2494313571Smm
2495313571Smm	n = 0;
2496313571Smm	marker = malloc(sizeof(marker[0]) * cnt);
2497313571Smm
2498313571Smm	for (i = 0; i < cnt; i++) {
2499313571Smm		if ((acls[i].type & want_type) != 0) {
2500313571Smm			marker[n] = i;
2501313571Smm			n++;
2502313571Smm		}
2503313571Smm	}
2504313571Smm
2505313571Smm	failure("No ACL's to compare, type mask: %d", want_type);
2506313571Smm	assert(n > 0);
2507313571Smm	if (n == 0)
2508313571Smm		return;
2509313571Smm
2510313571Smm	while (0 == (r = archive_entry_acl_next(ae, want_type,
2511313571Smm			 &type, &permset, &tag, &qual, &name))) {
2512313571Smm		for (i = 0, matched = 0; i < n && !matched; i++) {
2513313571Smm			if (archive_test_acl_match(&acls[marker[i]], type,
2514313571Smm			    permset, tag, qual, name)) {
2515313571Smm				/* We found a match; remove it. */
2516313571Smm				marker[i] = marker[n - 1];
2517313571Smm				n--;
2518313571Smm				matched = 1;
2519313571Smm			}
2520313571Smm		}
2521313571Smm		if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
2522313571Smm		    && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
2523313571Smm			if (!matched) printf("No match for user_obj perm\n");
2524313571Smm			failure("USER_OBJ permset (%02o) != user mode (%02o)",
2525313571Smm			    permset, 07 & (mode >> 6));
2526313571Smm			assert((permset << 6) == (mode & 0700));
2527313571Smm		} else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
2528313571Smm		    && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
2529313571Smm			if (!matched) printf("No match for group_obj perm\n");
2530313571Smm			failure("GROUP_OBJ permset %02o != group mode %02o",
2531313571Smm			    permset, 07 & (mode >> 3));
2532313571Smm			assert((permset << 3) == (mode & 0070));
2533313571Smm		} else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
2534313571Smm		    && tag == ARCHIVE_ENTRY_ACL_OTHER) {
2535313571Smm			if (!matched) printf("No match for other perm\n");
2536313571Smm			failure("OTHER permset (%02o) != other mode (%02o)",
2537313571Smm			    permset, mode & 07);
2538313571Smm			assert((permset << 0) == (mode & 0007));
2539313571Smm		} else {
2540313571Smm			failure("Could not find match for ACL "
2541313571Smm			    "(type=%#010x,permset=%#010x,tag=%d,qual=%d,"
2542313571Smm			    "name=``%s'')", type, permset, tag, qual, name);
2543313571Smm			assert(matched == 1);
2544313571Smm		}
2545313571Smm	}
2546313571Smm	assertEqualInt(ARCHIVE_EOF, r);
2547313571Smm	if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
2548313571Smm		assert((mode_t)(mode & 0777) == (archive_entry_mode(ae)
2549313571Smm		    & 0777));
2550313571Smm	failure("Could not find match for ACL "
2551313571Smm	    "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')",
2552313571Smm	    acls[marker[0]].type, acls[marker[0]].permset,
2553313571Smm	    acls[marker[0]].tag, acls[marker[0]].qual, acls[marker[0]].name);
2554313571Smm	assert(n == 0); /* Number of ACLs not matched should == 0 */
2555313571Smm	free(marker);
2556313571Smm}
2557313571Smm
2558228753Smm/*
2559228753Smm *
2560228753Smm * TEST management
2561228753Smm *
2562228753Smm */
2563228753Smm
2564228753Smm/*
2565228753Smm * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
2566228753Smm * a line like
2567228753Smm *      DEFINE_TEST(test_function)
2568228753Smm * for each test.
2569228753Smm */
2570228753Smm
2571228753Smm/* Use "list.h" to declare all of the test functions. */
2572228753Smm#undef DEFINE_TEST
2573228753Smm#define	DEFINE_TEST(name) void name(void);
2574228753Smm#include "list.h"
2575228753Smm
2576228753Smm/* Use "list.h" to create a list of all tests (functions and names). */
2577228753Smm#undef DEFINE_TEST
2578228753Smm#define	DEFINE_TEST(n) { n, #n, 0 },
2579248616Smmstruct test_list_t tests[] = {
2580228753Smm	#include "list.h"
2581228753Smm};
2582228753Smm
2583228753Smm/*
2584228753Smm * Summarize repeated failures in the just-completed test.
2585228753Smm */
2586228753Smmstatic void
2587302001Smmtest_summarize(int failed, int skips_num)
2588228753Smm{
2589228753Smm	unsigned int i;
2590228753Smm
2591228753Smm	switch (verbosity) {
2592228753Smm	case VERBOSITY_SUMMARY_ONLY:
2593228753Smm		printf(failed ? "E" : ".");
2594228753Smm		fflush(stdout);
2595228753Smm		break;
2596228753Smm	case VERBOSITY_PASSFAIL:
2597302001Smm		printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
2598228753Smm		break;
2599228753Smm	}
2600228753Smm
2601228753Smm	log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
2602228753Smm
2603228753Smm	for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
2604228753Smm		if (failed_lines[i].count > 1 && !failed_lines[i].skip)
2605228753Smm			logprintf("%s:%d: Summary: Failed %d times\n",
2606232153Smm			    failed_filename, i, failed_lines[i].count);
2607228753Smm	}
2608228753Smm	/* Clear the failure history for the next file. */
2609232153Smm	failed_filename = NULL;
2610228753Smm	memset(failed_lines, 0, sizeof(failed_lines));
2611228753Smm}
2612228753Smm
2613228753Smm/*
2614228753Smm * Actually run a single test, with appropriate setup and cleanup.
2615228753Smm */
2616228753Smmstatic int
2617228753Smmtest_run(int i, const char *tmpdir)
2618228753Smm{
2619232153Smm	char workdir[1024];
2620228753Smm	char logfilename[64];
2621228753Smm	int failures_before = failures;
2622302001Smm	int skips_before = skips;
2623228753Smm	int oldumask;
2624228753Smm
2625228753Smm	switch (verbosity) {
2626228753Smm	case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
2627228753Smm		break;
2628228753Smm	case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
2629302001Smm		printf("%3d: %-64s", i, tests[i].name);
2630228753Smm		fflush(stdout);
2631228753Smm		break;
2632228753Smm	default: /* Title of test, details will follow */
2633228753Smm		printf("%3d: %s\n", i, tests[i].name);
2634228753Smm	}
2635228753Smm
2636228753Smm	/* Chdir to the top-level work directory. */
2637228753Smm	if (!assertChdir(tmpdir)) {
2638228753Smm		fprintf(stderr,
2639228753Smm		    "ERROR: Can't chdir to top work dir %s\n", tmpdir);
2640228753Smm		exit(1);
2641228753Smm	}
2642228753Smm	/* Create a log file for this test. */
2643228753Smm	sprintf(logfilename, "%s.log", tests[i].name);
2644228753Smm	logfile = fopen(logfilename, "w");
2645228753Smm	fprintf(logfile, "%s\n\n", tests[i].name);
2646228753Smm	/* Chdir() to a work dir for this specific test. */
2647232153Smm	snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
2648232153Smm	testworkdir = workdir;
2649232153Smm	if (!assertMakeDir(testworkdir, 0755)
2650232153Smm	    || !assertChdir(testworkdir)) {
2651228753Smm		fprintf(stderr,
2652232153Smm		    "ERROR: Can't chdir to work dir %s\n", testworkdir);
2653228753Smm		exit(1);
2654228753Smm	}
2655228753Smm	/* Explicitly reset the locale before each test. */
2656228753Smm	setlocale(LC_ALL, "C");
2657228753Smm	/* Record the umask before we run the test. */
2658228753Smm	umask(oldumask = umask(0));
2659228753Smm	/*
2660228753Smm	 * Run the actual test.
2661228753Smm	 */
2662228753Smm	(*tests[i].func)();
2663228753Smm	/*
2664228753Smm	 * Clean up and report afterwards.
2665228753Smm	 */
2666232153Smm	testworkdir = NULL;
2667228753Smm	/* Restore umask */
2668228753Smm	umask(oldumask);
2669228753Smm	/* Reset locale. */
2670228753Smm	setlocale(LC_ALL, "C");
2671228753Smm	/* Reset directory. */
2672228753Smm	if (!assertChdir(tmpdir)) {
2673228753Smm		fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
2674228753Smm		    tmpdir);
2675228753Smm		exit(1);
2676228753Smm	}
2677228753Smm	/* Report per-test summaries. */
2678228753Smm	tests[i].failures = failures - failures_before;
2679302001Smm	test_summarize(tests[i].failures, skips - skips_before);
2680228753Smm	/* Close the per-test log file. */
2681228753Smm	fclose(logfile);
2682228753Smm	logfile = NULL;
2683228753Smm	/* If there were no failures, we can remove the work dir and logfile. */
2684228753Smm	if (tests[i].failures == 0) {
2685228753Smm		if (!keep_temp_files && assertChdir(tmpdir)) {
2686228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2687228753Smm			/* Make sure not to leave empty directories.
2688228753Smm			 * Sometimes a processing of closing files used by tests
2689228753Smm			 * is not done, then rmdir will be failed and it will
2690228753Smm			 * leave a empty test directory. So we should wait a few
2691228753Smm			 * seconds and retry rmdir. */
2692228753Smm			int r, t;
2693228753Smm			for (t = 0; t < 10; t++) {
2694228753Smm				if (t > 0)
2695228753Smm					Sleep(1000);
2696228753Smm				r = systemf("rmdir /S /Q %s", tests[i].name);
2697228753Smm				if (r == 0)
2698228753Smm					break;
2699228753Smm			}
2700228753Smm			systemf("del %s", logfilename);
2701228753Smm#else
2702228753Smm			systemf("rm -rf %s", tests[i].name);
2703228753Smm			systemf("rm %s", logfilename);
2704228753Smm#endif
2705228753Smm		}
2706228753Smm	}
2707228753Smm	/* Return appropriate status. */
2708228753Smm	return (tests[i].failures);
2709228753Smm}
2710228753Smm
2711228753Smm/*
2712228753Smm *
2713228753Smm *
2714228753Smm * MAIN and support routines.
2715228753Smm *
2716228753Smm *
2717228753Smm */
2718228753Smm
2719228753Smmstatic void
2720228753Smmusage(const char *program)
2721228753Smm{
2722228753Smm	static const int limit = sizeof(tests) / sizeof(tests[0]);
2723228753Smm	int i;
2724228753Smm
2725228753Smm	printf("Usage: %s [options] <test> <test> ...\n", program);
2726228753Smm	printf("Default is to run all tests.\n");
2727228753Smm	printf("Otherwise, specify the numbers of the tests you wish to run.\n");
2728228753Smm	printf("Options:\n");
2729228753Smm	printf("  -d  Dump core after any failure, for debugging.\n");
2730228753Smm	printf("  -k  Keep all temp files.\n");
2731228753Smm	printf("      Default: temp files for successful tests deleted.\n");
2732228753Smm#ifdef PROGRAM
2733228753Smm	printf("  -p <path>  Path to executable to be tested.\n");
2734228753Smm	printf("      Default: path taken from " ENVBASE " environment variable.\n");
2735228753Smm#endif
2736228753Smm	printf("  -q  Quiet.\n");
2737228753Smm	printf("  -r <dir>   Path to dir containing reference files.\n");
2738228753Smm	printf("      Default: Current directory.\n");
2739232153Smm	printf("  -u  Keep running specifies tests until one fails.\n");
2740228753Smm	printf("  -v  Verbose.\n");
2741228753Smm	printf("Available tests:\n");
2742228753Smm	for (i = 0; i < limit; i++)
2743228753Smm		printf("  %d: %s\n", i, tests[i].name);
2744228753Smm	exit(1);
2745228753Smm}
2746228753Smm
2747228753Smmstatic char *
2748228753Smmget_refdir(const char *d)
2749228753Smm{
2750302295Smm	size_t tried_size, buff_size;
2751302295Smm	char *buff, *tried, *pwd = NULL, *p = NULL;
2752228753Smm
2753302295Smm#ifdef PATH_MAX
2754302295Smm	buff_size = PATH_MAX;
2755302295Smm#else
2756302295Smm	buff_size = 8192;
2757302295Smm#endif
2758302295Smm	buff = calloc(buff_size, 1);
2759302295Smm	if (buff == NULL) {
2760302295Smm		fprintf(stderr, "Unable to allocate memory\n");
2761302295Smm		exit(1);
2762302295Smm	}
2763302295Smm
2764302295Smm	/* Allocate a buffer to hold the various directories we checked. */
2765302295Smm	tried_size = buff_size * 2;
2766302295Smm	tried = calloc(tried_size, 1);
2767302295Smm	if (tried == NULL) {
2768302295Smm		fprintf(stderr, "Unable to allocate memory\n");
2769302295Smm		exit(1);
2770302295Smm	}
2771302295Smm
2772228753Smm	/* If a dir was specified, try that */
2773228753Smm	if (d != NULL) {
2774228753Smm		pwd = NULL;
2775302295Smm		snprintf(buff, buff_size, "%s", d);
2776228753Smm		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2777228753Smm		if (p != NULL) goto success;
2778302295Smm		strncat(tried, buff, tried_size - strlen(tried) - 1);
2779302295Smm		strncat(tried, "\n", tried_size - strlen(tried) - 1);
2780228753Smm		goto failure;
2781228753Smm	}
2782228753Smm
2783228753Smm	/* Get the current dir. */
2784232153Smm#ifdef PATH_MAX
2785232153Smm	pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2786232153Smm#else
2787228753Smm	pwd = getcwd(NULL, 0);
2788232153Smm#endif
2789228753Smm	while (pwd[strlen(pwd) - 1] == '\n')
2790228753Smm		pwd[strlen(pwd) - 1] = '\0';
2791228753Smm
2792228753Smm	/* Look for a known file. */
2793302295Smm	snprintf(buff, buff_size, "%s", pwd);
2794228753Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2795228753Smm	if (p != NULL) goto success;
2796302295Smm	strncat(tried, buff, tried_size - strlen(tried) - 1);
2797302295Smm	strncat(tried, "\n", tried_size - strlen(tried) - 1);
2798228753Smm
2799302295Smm	snprintf(buff, buff_size, "%s/test", pwd);
2800228753Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2801228753Smm	if (p != NULL) goto success;
2802302295Smm	strncat(tried, buff, tried_size - strlen(tried) - 1);
2803302295Smm	strncat(tried, "\n", tried_size - strlen(tried) - 1);
2804228753Smm
2805228753Smm#if defined(LIBRARY)
2806302295Smm	snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
2807228753Smm#else
2808302295Smm	snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
2809228753Smm#endif
2810228753Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2811228753Smm	if (p != NULL) goto success;
2812302295Smm	strncat(tried, buff, tried_size - strlen(tried) - 1);
2813302295Smm	strncat(tried, "\n", tried_size - strlen(tried) - 1);
2814228753Smm
2815232153Smm#if defined(PROGRAM_ALIAS)
2816302295Smm	snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
2817232153Smm	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2818232153Smm	if (p != NULL) goto success;
2819302295Smm	strncat(tried, buff, tried_size - strlen(tried) - 1);
2820302295Smm	strncat(tried, "\n", tried_size - strlen(tried) - 1);
2821232153Smm#endif
2822232153Smm
2823228753Smm	if (memcmp(pwd, "/usr/obj", 8) == 0) {
2824302295Smm		snprintf(buff, buff_size, "%s", pwd + 8);
2825228753Smm		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2826228753Smm		if (p != NULL) goto success;
2827302295Smm		strncat(tried, buff, tried_size - strlen(tried) - 1);
2828302295Smm		strncat(tried, "\n", tried_size - strlen(tried) - 1);
2829228753Smm
2830302295Smm		snprintf(buff, buff_size, "%s/test", pwd + 8);
2831228753Smm		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
2832228753Smm		if (p != NULL) goto success;
2833302295Smm		strncat(tried, buff, tried_size - strlen(tried) - 1);
2834302295Smm		strncat(tried, "\n", tried_size - strlen(tried) - 1);
2835228753Smm	}
2836228753Smm
2837228753Smmfailure:
2838228753Smm	printf("Unable to locate known reference file %s\n", KNOWNREF);
2839228753Smm	printf("  Checked following directories:\n%s\n", tried);
2840302001Smm	printf("Use -r option to specify full path to reference directory\n");
2841228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2842228753Smm	DebugBreak();
2843228753Smm#endif
2844228753Smm	exit(1);
2845228753Smm
2846228753Smmsuccess:
2847228753Smm	free(p);
2848228753Smm	free(pwd);
2849302295Smm	free(tried);
2850302295Smm
2851302295Smm	/* Copy result into a fresh buffer to reduce memory usage. */
2852302295Smm	p = strdup(buff);
2853302295Smm	free(buff);
2854302295Smm	return p;
2855228753Smm}
2856228753Smm
2857228753Smmint
2858228753Smmmain(int argc, char **argv)
2859228753Smm{
2860228753Smm	static const int limit = sizeof(tests) / sizeof(tests[0]);
2861238856Smm	int test_set[sizeof(tests) / sizeof(tests[0])];
2862238856Smm	int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
2863228753Smm	time_t now;
2864228753Smm	char *refdir_alloc = NULL;
2865228753Smm	const char *progname;
2866232153Smm	char **saved_argv;
2867228753Smm	const char *tmp, *option_arg, *p;
2868238856Smm	char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
2869228753Smm	char tmpdir_timestamp[256];
2870228753Smm
2871228753Smm	(void)argc; /* UNUSED */
2872228753Smm
2873232153Smm	/* Get the current dir. */
2874232153Smm#ifdef PATH_MAX
2875232153Smm	pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
2876232153Smm#else
2877232153Smm	pwd = getcwd(NULL, 0);
2878232153Smm#endif
2879232153Smm	while (pwd[strlen(pwd) - 1] == '\n')
2880232153Smm		pwd[strlen(pwd) - 1] = '\0';
2881232153Smm
2882302001Smm#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
2883228753Smm	/* To stop to run the default invalid parameter handler. */
2884228753Smm	_set_invalid_parameter_handler(invalid_parameter_handler);
2885228753Smm	/* Disable annoying assertion message box. */
2886228753Smm	_CrtSetReportMode(_CRT_ASSERT, 0);
2887228753Smm#endif
2888228753Smm
2889228753Smm	/*
2890228753Smm	 * Name of this program, used to build root of our temp directory
2891228753Smm	 * tree.
2892228753Smm	 */
2893228753Smm	progname = p = argv[0];
2894232153Smm	if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
2895232153Smm	{
2896232153Smm		fprintf(stderr, "ERROR: Out of memory.");
2897232153Smm		exit(1);
2898232153Smm	}
2899232153Smm	strcpy(testprogdir, progname);
2900228753Smm	while (*p != '\0') {
2901228753Smm		/* Support \ or / dir separators for Windows compat. */
2902228753Smm		if (*p == '/' || *p == '\\')
2903232153Smm		{
2904228753Smm			progname = p + 1;
2905232153Smm			i = j;
2906232153Smm		}
2907228753Smm		++p;
2908232153Smm		j++;
2909228753Smm	}
2910232153Smm	testprogdir[i] = '\0';
2911232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
2912232153Smm	if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
2913232153Smm	    !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
2914232153Smm	       (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
2915232153Smm		testprogdir[1] == ':' &&
2916232153Smm		(testprogdir[2] == '/' || testprogdir[2] == '\\')))
2917232153Smm#else
2918232153Smm	if (testprogdir[0] != '/')
2919232153Smm#endif
2920232153Smm	{
2921232153Smm		/* Fixup path for relative directories. */
2922232153Smm		if ((testprogdir = (char *)realloc(testprogdir,
2923232153Smm			strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
2924232153Smm		{
2925232153Smm			fprintf(stderr, "ERROR: Out of memory.");
2926232153Smm			exit(1);
2927232153Smm		}
2928232153Smm		memmove(testprogdir + strlen(pwd) + 1, testprogdir,
2929302001Smm		    strlen(testprogdir) + 1);
2930232153Smm		memcpy(testprogdir, pwd, strlen(pwd));
2931232153Smm		testprogdir[strlen(pwd)] = '/';
2932232153Smm	}
2933228753Smm
2934228753Smm#ifdef PROGRAM
2935228753Smm	/* Get the target program from environment, if available. */
2936228753Smm	testprogfile = getenv(ENVBASE);
2937228753Smm#endif
2938228753Smm
2939228753Smm	if (getenv("TMPDIR") != NULL)
2940228753Smm		tmp = getenv("TMPDIR");
2941228753Smm	else if (getenv("TMP") != NULL)
2942228753Smm		tmp = getenv("TMP");
2943228753Smm	else if (getenv("TEMP") != NULL)
2944228753Smm		tmp = getenv("TEMP");
2945228753Smm	else if (getenv("TEMPDIR") != NULL)
2946228753Smm		tmp = getenv("TEMPDIR");
2947228753Smm	else
2948228753Smm		tmp = "/tmp";
2949228753Smm
2950228753Smm	/* Allow -d to be controlled through the environment. */
2951228753Smm	if (getenv(ENVBASE "_DEBUG") != NULL)
2952228753Smm		dump_on_failure = 1;
2953228753Smm
2954238856Smm	/* Allow -v to be controlled through the environment. */
2955238856Smm	if (getenv("_VERBOSITY_LEVEL") != NULL)
2956238856Smm	{
2957238856Smm		vlevel = getenv("_VERBOSITY_LEVEL");
2958238856Smm		verbosity = atoi(vlevel);
2959238856Smm		if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
2960238856Smm		{
2961238856Smm			/* Unsupported verbosity levels are silently ignored */
2962238856Smm			vlevel = NULL;
2963238856Smm			verbosity = VERBOSITY_PASSFAIL;
2964238856Smm		}
2965238856Smm	}
2966238856Smm
2967228753Smm	/* Get the directory holding test files from environment. */
2968228753Smm	refdir = getenv(ENVBASE "_TEST_FILES");
2969228753Smm
2970228753Smm	/*
2971228753Smm	 * Parse options, without using getopt(), which isn't available
2972228753Smm	 * on all platforms.
2973228753Smm	 */
2974228753Smm	++argv; /* Skip program name */
2975228753Smm	while (*argv != NULL) {
2976228753Smm		if (**argv != '-')
2977228753Smm			break;
2978228753Smm		p = *argv++;
2979228753Smm		++p; /* Skip '-' */
2980228753Smm		while (*p != '\0') {
2981228753Smm			option = *p++;
2982228753Smm			option_arg = NULL;
2983228753Smm			/* If 'opt' takes an argument, parse that. */
2984228753Smm			if (option == 'p' || option == 'r') {
2985228753Smm				if (*p != '\0')
2986228753Smm					option_arg = p;
2987228753Smm				else if (*argv == NULL) {
2988228753Smm					fprintf(stderr,
2989228753Smm					    "Option -%c requires argument.\n",
2990228753Smm					    option);
2991228753Smm					usage(progname);
2992228753Smm				} else
2993228753Smm					option_arg = *argv++;
2994228753Smm				p = ""; /* End of this option word. */
2995228753Smm			}
2996228753Smm
2997228753Smm			/* Now, handle the option. */
2998228753Smm			switch (option) {
2999228753Smm			case 'd':
3000228753Smm				dump_on_failure = 1;
3001228753Smm				break;
3002228753Smm			case 'k':
3003228753Smm				keep_temp_files = 1;
3004228753Smm				break;
3005228753Smm			case 'p':
3006228753Smm#ifdef PROGRAM
3007228753Smm				testprogfile = option_arg;
3008228753Smm#else
3009232153Smm				fprintf(stderr, "-p option not permitted\n");
3010228753Smm				usage(progname);
3011228753Smm#endif
3012228753Smm				break;
3013228753Smm			case 'q':
3014238856Smm				if (!vlevel)
3015238856Smm					verbosity--;
3016228753Smm				break;
3017228753Smm			case 'r':
3018228753Smm				refdir = option_arg;
3019228753Smm				break;
3020232153Smm			case 'u':
3021232153Smm				until_failure++;
3022232153Smm				break;
3023228753Smm			case 'v':
3024238856Smm				if (!vlevel)
3025238856Smm					verbosity++;
3026228753Smm				break;
3027228753Smm			default:
3028232153Smm				fprintf(stderr, "Unrecognized option '%c'\n",
3029232153Smm				    option);
3030228753Smm				usage(progname);
3031228753Smm			}
3032228753Smm		}
3033228753Smm	}
3034228753Smm
3035228753Smm	/*
3036228753Smm	 * Sanity-check that our options make sense.
3037228753Smm	 */
3038228753Smm#ifdef PROGRAM
3039228753Smm	if (testprogfile == NULL)
3040228753Smm	{
3041232153Smm		if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
3042232153Smm			strlen(PROGRAM) + 1)) == NULL)
3043232153Smm		{
3044232153Smm			fprintf(stderr, "ERROR: Out of memory.");
3045232153Smm			exit(1);
3046232153Smm		}
3047232153Smm		strcpy(tmp2, testprogdir);
3048232153Smm		strcat(tmp2, "/");
3049232153Smm		strcat(tmp2, PROGRAM);
3050232153Smm		testprogfile = tmp2;
3051232153Smm	}
3052232153Smm
3053232153Smm	{
3054228753Smm		char *testprg;
3055228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
3056228753Smm		/* Command.com sometimes rejects '/' separators. */
3057228753Smm		testprg = strdup(testprogfile);
3058228753Smm		for (i = 0; testprg[i] != '\0'; i++) {
3059228753Smm			if (testprg[i] == '/')
3060228753Smm				testprg[i] = '\\';
3061228753Smm		}
3062228753Smm		testprogfile = testprg;
3063228753Smm#endif
3064228753Smm		/* Quote the name that gets put into shell command lines. */
3065228753Smm		testprg = malloc(strlen(testprogfile) + 3);
3066228753Smm		strcpy(testprg, "\"");
3067228753Smm		strcat(testprg, testprogfile);
3068228753Smm		strcat(testprg, "\"");
3069228753Smm		testprog = testprg;
3070228753Smm	}
3071228753Smm#endif
3072228753Smm
3073232153Smm#if !defined(_WIN32) && defined(SIGPIPE)
3074232153Smm	{   /* Ignore SIGPIPE signals */
3075232153Smm		struct sigaction sa;
3076232153Smm		sa.sa_handler = SIG_IGN;
3077232153Smm		sigemptyset(&sa.sa_mask);
3078232153Smm		sa.sa_flags = 0;
3079232153Smm		sigaction(SIGPIPE, &sa, NULL);
3080232153Smm	}
3081232153Smm#endif
3082232153Smm
3083228753Smm	/*
3084228753Smm	 * Create a temp directory for the following tests.
3085228753Smm	 * Include the time the tests started as part of the name,
3086228753Smm	 * to make it easier to track the results of multiple tests.
3087228753Smm	 */
3088228753Smm	now = time(NULL);
3089228753Smm	for (i = 0; ; i++) {
3090228753Smm		strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
3091228753Smm		    "%Y-%m-%dT%H.%M.%S",
3092228753Smm		    localtime(&now));
3093228753Smm		sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname,
3094228753Smm		    tmpdir_timestamp, i);
3095228753Smm		if (assertMakeDir(tmpdir,0755))
3096228753Smm			break;
3097228753Smm		if (i >= 999) {
3098228753Smm			fprintf(stderr,
3099228753Smm			    "ERROR: Unable to create temp directory %s\n",
3100228753Smm			    tmpdir);
3101228753Smm			exit(1);
3102228753Smm		}
3103228753Smm	}
3104228753Smm
3105228753Smm	/*
3106228753Smm	 * If the user didn't specify a directory for locating
3107228753Smm	 * reference files, try to find the reference files in
3108228753Smm	 * the "usual places."
3109228753Smm	 */
3110228753Smm	refdir = refdir_alloc = get_refdir(refdir);
3111228753Smm
3112228753Smm	/*
3113228753Smm	 * Banner with basic information.
3114228753Smm	 */
3115228753Smm	printf("\n");
3116228753Smm	printf("If tests fail or crash, details will be in:\n");
3117228753Smm	printf("   %s\n", tmpdir);
3118228753Smm	printf("\n");
3119228753Smm	if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3120228753Smm		printf("Reference files will be read from: %s\n", refdir);
3121228753Smm#ifdef PROGRAM
3122228753Smm		printf("Running tests on: %s\n", testprog);
3123228753Smm#endif
3124228753Smm		printf("Exercising: ");
3125228753Smm		fflush(stdout);
3126228753Smm		printf("%s\n", EXTRA_VERSION);
3127228753Smm	} else {
3128228753Smm		printf("Running ");
3129228753Smm		fflush(stdout);
3130228753Smm	}
3131228753Smm
3132228753Smm	/*
3133228753Smm	 * Run some or all of the individual tests.
3134228753Smm	 */
3135232153Smm	saved_argv = argv;
3136232153Smm	do {
3137232153Smm		argv = saved_argv;
3138238856Smm		do {
3139238856Smm			int test_num;
3140238856Smm
3141248616Smm			test_num = get_test_set(test_set, limit, *argv, tests);
3142238856Smm			if (test_num < 0) {
3143238856Smm				printf("*** INVALID Test %s\n", *argv);
3144238856Smm				free(refdir_alloc);
3145248616Smm				free(testprogdir);
3146238856Smm				usage(progname);
3147238856Smm				return (1);
3148238856Smm			}
3149238856Smm			for (i = 0; i < test_num; i++) {
3150232153Smm				tests_run++;
3151238856Smm				if (test_run(test_set[i], tmpdir)) {
3152232153Smm					tests_failed++;
3153232153Smm					if (until_failure)
3154232153Smm						goto finish;
3155228753Smm				}
3156232153Smm			}
3157238856Smm			if (*argv != NULL)
3158232153Smm				argv++;
3159238856Smm		} while (*argv != NULL);
3160232153Smm	} while (until_failure);
3161228753Smm
3162232153Smmfinish:
3163232153Smm	/* Must be freed after all tests run */
3164232153Smm	free(tmp2);
3165232153Smm	free(testprogdir);
3166232153Smm	free(pwd);
3167232153Smm
3168228753Smm	/*
3169228753Smm	 * Report summary statistics.
3170228753Smm	 */
3171228753Smm	if (verbosity > VERBOSITY_SUMMARY_ONLY) {
3172228753Smm		printf("\n");
3173228753Smm		printf("Totals:\n");
3174228753Smm		printf("  Tests run:         %8d\n", tests_run);
3175228753Smm		printf("  Tests failed:      %8d\n", tests_failed);
3176228753Smm		printf("  Assertions checked:%8d\n", assertions);
3177228753Smm		printf("  Assertions failed: %8d\n", failures);
3178228753Smm		printf("  Skips reported:    %8d\n", skips);
3179228753Smm	}
3180228753Smm	if (failures) {
3181228753Smm		printf("\n");
3182228753Smm		printf("Failing tests:\n");
3183228753Smm		for (i = 0; i < limit; ++i) {
3184228753Smm			if (tests[i].failures)
3185228753Smm				printf("  %d: %s (%d failures)\n", i,
3186228753Smm				    tests[i].name, tests[i].failures);
3187228753Smm		}
3188228753Smm		printf("\n");
3189228753Smm		printf("Details for failing tests: %s\n", tmpdir);
3190228753Smm		printf("\n");
3191228753Smm	} else {
3192228753Smm		if (verbosity == VERBOSITY_SUMMARY_ONLY)
3193228753Smm			printf("\n");
3194228753Smm		printf("%d tests passed, no failures\n", tests_run);
3195228753Smm	}
3196228753Smm
3197228753Smm	free(refdir_alloc);
3198228753Smm
3199228753Smm	/* If the final tmpdir is empty, we can remove it. */
3200228753Smm	/* This should be the usual case when all tests succeed. */
3201228753Smm	assertChdir("..");
3202228753Smm	rmdir(tmpdir);
3203228753Smm
3204228753Smm	return (tests_failed ? 1 : 0);
3205228753Smm}
3206