test_main.c revision 358088
1/*
2 * Copyright (c) 2003-2009 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "test.h"
27#include "test_utils.h"
28#ifdef HAVE_SYS_IOCTL_H
29#include <sys/ioctl.h>
30#endif
31#ifdef HAVE_SYS_TIME_H
32#include <sys/time.h>
33#endif
34#include <errno.h>
35#ifdef HAVE_ICONV_H
36#include <iconv.h>
37#endif
38/*
39 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
40 * As the include guards don't agree, the order of include is important.
41 */
42#ifdef HAVE_LINUX_EXT2_FS_H
43#include <linux/ext2_fs.h>      /* for Linux file flags */
44#endif
45#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
46#include <ext2fs/ext2_fs.h>     /* Linux file flags, broken on Cygwin */
47#endif
48#ifdef HAVE_LINUX_FS_H
49#include <linux/fs.h>
50#endif
51#include <limits.h>
52#include <locale.h>
53#ifdef HAVE_SIGNAL_H
54#include <signal.h>
55#endif
56#include <stdarg.h>
57#include <time.h>
58
59#ifdef HAVE_SIGNAL_H
60#endif
61#ifdef HAVE_ACL_LIBACL_H
62#include <acl/libacl.h>
63#endif
64#ifdef HAVE_SYS_TYPES_H
65#include <sys/types.h>
66#endif
67#ifdef HAVE_SYS_ACL_H
68#include <sys/acl.h>
69#endif
70#ifdef HAVE_SYS_EA_H
71#include <sys/ea.h>
72#endif
73#ifdef HAVE_SYS_EXTATTR_H
74#include <sys/extattr.h>
75#endif
76#if HAVE_SYS_XATTR_H
77#include <sys/xattr.h>
78#elif HAVE_ATTR_XATTR_H
79#include <attr/xattr.h>
80#endif
81#ifdef HAVE_SYS_RICHACL_H
82#include <sys/richacl.h>
83#endif
84#if HAVE_MEMBERSHIP_H
85#include <membership.h>
86#endif
87
88/*
89 *
90 * Windows support routines
91 *
92 * Note: Configuration is a tricky issue.  Using HAVE_* feature macros
93 * in the test harness is dangerous because they cover up
94 * configuration errors.  The classic example of this is omitting a
95 * configure check.  If libarchive and libarchive_test both look for
96 * the same feature macro, such errors are hard to detect.  Platform
97 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
98 * easily lead to very messy code.  It's best to limit yourself
99 * to only the most generic programming techniques in the test harness
100 * and thus avoid conditionals altogether.  Where that's not possible,
101 * try to minimize conditionals by grouping platform-specific tests in
102 * one place (e.g., test_acl_freebsd) or by adding new assert()
103 * functions (e.g., assertMakeHardlink()) to cover up platform
104 * differences.  Platform-specific coding in libarchive_test is often
105 * a symptom that some capability is missing from libarchive itself.
106 */
107#if defined(_WIN32) && !defined(__CYGWIN__)
108#include <io.h>
109#include <direct.h>
110#include <windows.h>
111#ifndef F_OK
112#define F_OK (0)
113#endif
114#ifndef S_ISDIR
115#define S_ISDIR(m)  ((m) & _S_IFDIR)
116#endif
117#ifndef S_ISREG
118#define S_ISREG(m)  ((m) & _S_IFREG)
119#endif
120#if !defined(__BORLANDC__)
121#define access _access
122#undef chdir
123#define chdir _chdir
124#endif
125#ifndef fileno
126#define fileno _fileno
127#endif
128/*#define fstat _fstat64*/
129#if !defined(__BORLANDC__)
130#define getcwd _getcwd
131#endif
132#define lstat stat
133/*#define lstat _stat64*/
134/*#define stat _stat64*/
135#define rmdir _rmdir
136#if !defined(__BORLANDC__)
137#define strdup _strdup
138#define umask _umask
139#endif
140#define int64_t __int64
141#endif
142
143#if defined(HAVE__CrtSetReportMode)
144# include <crtdbg.h>
145#endif
146
147mode_t umasked(mode_t expected_mode)
148{
149	mode_t mode = umask(0);
150	umask(mode);
151	return expected_mode & ~mode;
152}
153
154/* Path to working directory for current test */
155const char *testworkdir;
156#ifdef PROGRAM
157/* Pathname of exe to be tested. */
158const char *testprogfile;
159/* Name of exe to use in printf-formatted command strings. */
160/* On Windows, this includes leading/trailing quotes. */
161const char *testprog;
162#endif
163
164#if defined(_WIN32) && !defined(__CYGWIN__)
165static void	*GetFunctionKernel32(const char *);
166static int	 my_CreateSymbolicLinkA(const char *, const char *, int);
167static int	 my_CreateHardLinkA(const char *, const char *);
168static int	 my_GetFileInformationByName(const char *,
169		     BY_HANDLE_FILE_INFORMATION *);
170
171typedef struct _REPARSE_DATA_BUFFER {
172	ULONG	ReparseTag;
173	USHORT ReparseDataLength;
174	USHORT	Reserved;
175	union {
176		struct {
177			USHORT	SubstituteNameOffset;
178			USHORT	SubstituteNameLength;
179			USHORT	PrintNameOffset;
180			USHORT	PrintNameLength;
181			ULONG	Flags;
182			WCHAR	PathBuffer[1];
183		} SymbolicLinkReparseBuffer;
184		struct {
185			USHORT	SubstituteNameOffset;
186			USHORT	SubstituteNameLength;
187			USHORT	PrintNameOffset;
188			USHORT	PrintNameLength;
189			WCHAR	PathBuffer[1];
190		} MountPointReparseBuffer;
191		struct {
192			UCHAR	DataBuffer[1];
193		} GenericReparseBuffer;
194	} DUMMYUNIONNAME;
195} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
196
197static void *
198GetFunctionKernel32(const char *name)
199{
200	static HINSTANCE lib;
201	static int set;
202	if (!set) {
203		set = 1;
204		lib = LoadLibrary("kernel32.dll");
205	}
206	if (lib == NULL) {
207		fprintf(stderr, "Can't load kernel32.dll?!\n");
208		exit(1);
209	}
210	return (void *)GetProcAddress(lib, name);
211}
212
213static int
214my_CreateSymbolicLinkA(const char *linkname, const char *target,
215    int targetIsDir)
216{
217	static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
218	DWORD attrs;
219	static int set;
220	int ret, tmpflags, llen, tlen;
221	int flags = 0;
222	char *src, *tgt, *p;
223	if (!set) {
224		set = 1;
225		f = GetFunctionKernel32("CreateSymbolicLinkA");
226	}
227	if (f == NULL)
228		return (0);
229
230	tlen = strlen(target);
231	llen = strlen(linkname);
232
233	if (tlen == 0 || llen == 0)
234		return (0);
235
236	tgt = malloc((tlen + 1) * sizeof(char));
237	if (tgt == NULL)
238		return (0);
239	src = malloc((llen + 1) * sizeof(char));
240	if (src == NULL) {
241		free(tgt);
242		return (0);
243	}
244
245	/*
246	 * Translate slashes to backslashes
247	 */
248	p = src;
249	while(*linkname != '\0') {
250		if (*linkname == '/')
251			*p = '\\';
252		else
253			*p = *linkname;
254		linkname++;
255		p++;
256	}
257	*p = '\0';
258
259	p = tgt;
260	while(*target != '\0') {
261		if (*target == '/')
262			*p = '\\';
263		else
264			*p = *target;
265		target++;
266		p++;
267	}
268	*p = '\0';
269
270	/*
271	 * Each test has to specify if a file or a directory symlink
272	 * should be created.
273	 */
274	if (targetIsDir) {
275#if defined(SYMBOLIC_LINK_FLAG_DIRECTORY)
276		flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
277#else
278		flags |= 0x1;
279#endif
280	}
281
282#if defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
283	tmpflags = flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
284#else
285	tmpflags = flags | 0x2;
286#endif
287	/*
288	 * Windows won't overwrite existing links
289	 */
290	attrs = GetFileAttributesA(linkname);
291	if (attrs != INVALID_FILE_ATTRIBUTES) {
292		if (attrs & FILE_ATTRIBUTE_DIRECTORY)
293			RemoveDirectoryA(linkname);
294		else
295			DeleteFileA(linkname);
296	}
297
298	ret = (*f)(src, tgt, tmpflags);
299	/*
300	 * Prior to Windows 10 the SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
301	 * is not understood
302	 */
303	if (!ret)
304		ret = (*f)(src, tgt, flags);
305
306	free(src);
307	free(tgt);
308	return (ret);
309}
310
311static int
312my_CreateHardLinkA(const char *linkname, const char *target)
313{
314	static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
315	static int set;
316	if (!set) {
317		set = 1;
318		f = GetFunctionKernel32("CreateHardLinkA");
319	}
320	return f == NULL ? 0 : (*f)(linkname, target, NULL);
321}
322
323static int
324my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
325{
326	HANDLE h;
327	int r;
328
329	memset(bhfi, 0, sizeof(*bhfi));
330	h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL,
331		OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
332	if (h == INVALID_HANDLE_VALUE)
333		return (0);
334	r = GetFileInformationByHandle(h, bhfi);
335	CloseHandle(h);
336	return (r);
337}
338#endif
339
340#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
341static void
342invalid_parameter_handler(const wchar_t * expression,
343    const wchar_t * function, const wchar_t * file,
344    unsigned int line, uintptr_t pReserved)
345{
346	/* nop */
347	// Silence unused-parameter compiler warnings.
348	(void)expression;
349	(void)function;
350	(void)file;
351	(void)line;
352	(void)pReserved;
353}
354#endif
355
356/*
357 *
358 * OPTIONS FLAGS
359 *
360 */
361
362/* Enable core dump on failure. */
363static int dump_on_failure = 0;
364/* Default is to remove temp dirs and log data for successful tests. */
365static int keep_temp_files = 0;
366/* Default is to run the specified tests once and report errors. */
367static int until_failure = 0;
368/* Default is to just report pass/fail for each test. */
369static int verbosity = 0;
370#define	VERBOSITY_SUMMARY_ONLY -1 /* -q */
371#define VERBOSITY_PASSFAIL 0   /* Default */
372#define VERBOSITY_LIGHT_REPORT 1 /* -v */
373#define VERBOSITY_FULL 2 /* -vv */
374/* A few places generate even more output for verbosity > VERBOSITY_FULL,
375 * mostly for debugging the test harness itself. */
376/* Cumulative count of assertion failures. */
377static int failures = 0;
378/* Cumulative count of reported skips. */
379static int skips = 0;
380/* Cumulative count of assertions checked. */
381static int assertions = 0;
382
383/* Directory where uuencoded reference files can be found. */
384static const char *refdir;
385
386/*
387 * Report log information selectively to console and/or disk log.
388 */
389static int log_console = 0;
390static FILE *logfile;
391static void __LA_PRINTFLIKE(1, 0)
392vlogprintf(const char *fmt, va_list ap)
393{
394#ifdef va_copy
395	va_list lfap;
396	va_copy(lfap, ap);
397#endif
398	if (log_console)
399		vfprintf(stdout, fmt, ap);
400	if (logfile != NULL)
401#ifdef va_copy
402		vfprintf(logfile, fmt, lfap);
403	va_end(lfap);
404#else
405		vfprintf(logfile, fmt, ap);
406#endif
407}
408
409static void __LA_PRINTFLIKE(1, 2)
410logprintf(const char *fmt, ...)
411{
412	va_list ap;
413	va_start(ap, fmt);
414	vlogprintf(fmt, ap);
415	va_end(ap);
416}
417
418/* Set up a message to display only if next assertion fails. */
419static char msgbuff[4096];
420static const char *msg, *nextmsg;
421void
422failure(const char *fmt, ...)
423{
424	va_list ap;
425	if (fmt == NULL) {
426		nextmsg = NULL;
427	} else {
428		va_start(ap, fmt);
429		vsprintf(msgbuff, fmt, ap);
430		va_end(ap);
431		nextmsg = msgbuff;
432	}
433}
434
435/*
436 * Copy arguments into file-local variables.
437 * This was added to permit vararg assert() functions without needing
438 * variadic wrapper macros.  Turns out that the vararg capability is almost
439 * never used, so almost all of the vararg assertions can be simplified
440 * by removing the vararg capability and reworking the wrapper macro to
441 * pass __FILE__, __LINE__ directly into the function instead of using
442 * this hook.  I suspect this machinery is used so rarely that we
443 * would be better off just removing it entirely.  That would simplify
444 * the code here noticeably.
445 */
446static const char *skipping_filename;
447static int skipping_line;
448void skipping_setup(const char *filename, int line)
449{
450	skipping_filename = filename;
451	skipping_line = line;
452}
453
454/* Called at the beginning of each assert() function. */
455static void
456assertion_count(const char *file, int line)
457{
458	(void)file; /* UNUSED */
459	(void)line; /* UNUSED */
460	++assertions;
461	/* Proper handling of "failure()" message. */
462	msg = nextmsg;
463	nextmsg = NULL;
464	/* Uncomment to print file:line after every assertion.
465	 * Verbose, but occasionally useful in tracking down crashes. */
466	/* printf("Checked %s:%d\n", file, line); */
467}
468
469/*
470 * For each test source file, we remember how many times each
471 * assertion was reported.  Cleared before each new test,
472 * used by test_summarize().
473 */
474static struct line {
475	int count;
476	int skip;
477}  failed_lines[10000];
478const char *failed_filename;
479
480/* Count this failure, setup up log destination and handle initial report. */
481static void __LA_PRINTFLIKE(3, 4)
482failure_start(const char *filename, int line, const char *fmt, ...)
483{
484	va_list ap;
485
486	/* Record another failure for this line. */
487	++failures;
488	failed_filename = filename;
489	failed_lines[line].count++;
490
491	/* Determine whether to log header to console. */
492	switch (verbosity) {
493	case VERBOSITY_LIGHT_REPORT:
494		log_console = (failed_lines[line].count < 2);
495		break;
496	default:
497		log_console = (verbosity >= VERBOSITY_FULL);
498	}
499
500	/* Log file:line header for this failure */
501	va_start(ap, fmt);
502#if _MSC_VER
503	logprintf("%s(%d): ", filename, line);
504#else
505	logprintf("%s:%d: ", filename, line);
506#endif
507	vlogprintf(fmt, ap);
508	va_end(ap);
509	logprintf("\n");
510
511	if (msg != NULL && msg[0] != '\0') {
512		logprintf("   Description: %s\n", msg);
513		msg = NULL;
514	}
515
516	/* Determine whether to log details to console. */
517	if (verbosity == VERBOSITY_LIGHT_REPORT)
518		log_console = 0;
519}
520
521/* Complete reporting of failed tests. */
522/*
523 * The 'extra' hook here is used by libarchive to include libarchive
524 * error messages with assertion failures.  It could also be used
525 * to add strerror() output, for example.  Just define the EXTRA_DUMP()
526 * macro appropriately.
527 */
528static void
529failure_finish(void *extra)
530{
531	(void)extra; /* UNUSED (maybe) */
532#ifdef EXTRA_DUMP
533	if (extra != NULL) {
534		logprintf("    errno: %d\n", EXTRA_ERRNO(extra));
535		logprintf("   detail: %s\n", EXTRA_DUMP(extra));
536	}
537#endif
538
539	if (dump_on_failure) {
540		fprintf(stderr,
541		    " *** forcing core dump so failure can be debugged ***\n");
542		abort();
543	}
544}
545
546/* Inform user that we're skipping some checks. */
547void
548test_skipping(const char *fmt, ...)
549{
550	char buff[1024];
551	va_list ap;
552
553	va_start(ap, fmt);
554	vsprintf(buff, fmt, ap);
555	va_end(ap);
556	/* Use failure() message if set. */
557	msg = nextmsg;
558	nextmsg = NULL;
559	/* failure_start() isn't quite right, but is awfully convenient. */
560	failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
561	--failures; /* Undo failures++ in failure_start() */
562	/* Don't failure_finish() here. */
563	/* Mark as skip, so doesn't count as failed test. */
564	failed_lines[skipping_line].skip = 1;
565	++skips;
566}
567
568/*
569 *
570 * ASSERTIONS
571 *
572 */
573
574/* Generic assert() just displays the failed condition. */
575int
576assertion_assert(const char *file, int line, int value,
577    const char *condition, void *extra)
578{
579	assertion_count(file, line);
580	if (!value) {
581		failure_start(file, line, "Assertion failed: %s", condition);
582		failure_finish(extra);
583	}
584	return (value);
585}
586
587/* chdir() and report any errors */
588int
589assertion_chdir(const char *file, int line, const char *pathname)
590{
591	assertion_count(file, line);
592	if (chdir(pathname) == 0)
593		return (1);
594	failure_start(file, line, "chdir(\"%s\")", pathname);
595	failure_finish(NULL);
596	return (0);
597
598}
599
600/* Verify two integers are equal. */
601int
602assertion_equal_int(const char *file, int line,
603    long long v1, const char *e1, long long v2, const char *e2, void *extra)
604{
605	assertion_count(file, line);
606	if (v1 == v2)
607		return (1);
608	failure_start(file, line, "%s != %s", e1, e2);
609	logprintf("      %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
610	logprintf("      %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
611	failure_finish(extra);
612	return (0);
613}
614
615/*
616 * Utility to convert a single UTF-8 sequence.
617 */
618static int
619_utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
620{
621	static const char utf8_count[256] = {
622		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
623		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
624		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
625		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
626		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
627		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
628		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
629		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
630		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
631		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
632		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
633		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
634		 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
635		 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
636		 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
637		 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
638	};
639	int ch;
640	int cnt;
641	uint32_t wc;
642
643	*pwc = 0;
644
645	/* Sanity check. */
646	if (n == 0)
647		return (0);
648	/*
649	 * Decode 1-4 bytes depending on the value of the first byte.
650	 */
651	ch = (unsigned char)*s;
652	if (ch == 0)
653		return (0); /* Standard:  return 0 for end-of-string. */
654	cnt = utf8_count[ch];
655
656	/* Invalid sequence or there are not plenty bytes. */
657	if (n < (size_t)cnt)
658		return (-1);
659
660	/* Make a Unicode code point from a single UTF-8 sequence. */
661	switch (cnt) {
662	case 1:	/* 1 byte sequence. */
663		*pwc = ch & 0x7f;
664		return (cnt);
665	case 2:	/* 2 bytes sequence. */
666		if ((s[1] & 0xc0) != 0x80) return (-1);
667		*pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
668		return (cnt);
669	case 3:	/* 3 bytes sequence. */
670		if ((s[1] & 0xc0) != 0x80) return (-1);
671		if ((s[2] & 0xc0) != 0x80) return (-1);
672		wc = ((ch & 0x0f) << 12)
673		    | ((s[1] & 0x3f) << 6)
674		    | (s[2] & 0x3f);
675		if (wc < 0x800)
676			return (-1);/* Overlong sequence. */
677		break;
678	case 4:	/* 4 bytes sequence. */
679		if (n < 4)
680			return (-1);
681		if ((s[1] & 0xc0) != 0x80) return (-1);
682		if ((s[2] & 0xc0) != 0x80) return (-1);
683		if ((s[3] & 0xc0) != 0x80) return (-1);
684		wc = ((ch & 0x07) << 18)
685		    | ((s[1] & 0x3f) << 12)
686		    | ((s[2] & 0x3f) << 6)
687		    | (s[3] & 0x3f);
688		if (wc < 0x10000)
689			return (-1);/* Overlong sequence. */
690		break;
691	default:
692		return (-1);
693	}
694
695	/* The code point larger than 0x10FFFF is not legal
696	 * Unicode values. */
697	if (wc > 0x10FFFF)
698		return (-1);
699	/* Correctly gets a Unicode, returns used bytes. */
700	*pwc = wc;
701	return (cnt);
702}
703
704static void strdump(const char *e, const char *p, int ewidth, int utf8)
705{
706	const char *q = p;
707
708	logprintf("      %*s = ", ewidth, e);
709	if (p == NULL) {
710		logprintf("NULL\n");
711		return;
712	}
713	logprintf("\"");
714	while (*p != '\0') {
715		unsigned int c = 0xff & *p++;
716		switch (c) {
717		case '\a': logprintf("\\a"); break;
718		case '\b': logprintf("\\b"); break;
719		case '\n': logprintf("\\n"); break;
720		case '\r': logprintf("\\r"); break;
721		default:
722			if (c >= 32 && c < 127)
723				logprintf("%c", c);
724			else
725				logprintf("\\x%02X", c);
726		}
727	}
728	logprintf("\"");
729	logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
730
731	/*
732	 * If the current string is UTF-8, dump its code points.
733	 */
734	if (utf8) {
735		size_t len;
736		uint32_t uc;
737		int n;
738		int cnt = 0;
739
740		p = q;
741		len = strlen(p);
742		logprintf(" [");
743		while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
744			if (p != q)
745				logprintf(" ");
746			logprintf("%04X", uc);
747			p += n;
748			len -= n;
749			cnt++;
750		}
751		logprintf("]");
752		logprintf(" (count %d", cnt);
753		if (n < 0) {
754			logprintf(",unknown %zu bytes", len);
755		}
756		logprintf(")");
757
758	}
759	logprintf("\n");
760}
761
762/* Verify two strings are equal, dump them if not. */
763int
764assertion_equal_string(const char *file, int line,
765    const char *v1, const char *e1,
766    const char *v2, const char *e2,
767    void *extra, int utf8)
768{
769	int l1, l2;
770
771	assertion_count(file, line);
772	if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
773		return (1);
774	failure_start(file, line, "%s != %s", e1, e2);
775	l1 = (int)strlen(e1);
776	l2 = (int)strlen(e2);
777	if (l1 < l2)
778		l1 = l2;
779	strdump(e1, v1, l1, utf8);
780	strdump(e2, v2, l1, utf8);
781	failure_finish(extra);
782	return (0);
783}
784
785static void
786wcsdump(const char *e, const wchar_t *w)
787{
788	logprintf("      %s = ", e);
789	if (w == NULL) {
790		logprintf("(null)");
791		return;
792	}
793	logprintf("\"");
794	while (*w != L'\0') {
795		unsigned int c = *w++;
796		if (c >= 32 && c < 127)
797			logprintf("%c", c);
798		else if (c < 256)
799			logprintf("\\x%02X", c);
800		else if (c < 0x10000)
801			logprintf("\\u%04X", c);
802		else
803			logprintf("\\U%08X", c);
804	}
805	logprintf("\"\n");
806}
807
808#ifndef HAVE_WCSCMP
809static int
810wcscmp(const wchar_t *s1, const wchar_t *s2)
811{
812
813	while (*s1 == *s2++) {
814		if (*s1++ == L'\0')
815			return 0;
816	}
817	if (*s1 > *--s2)
818		return 1;
819	else
820		return -1;
821}
822#endif
823
824/* Verify that two wide strings are equal, dump them if not. */
825int
826assertion_equal_wstring(const char *file, int line,
827    const wchar_t *v1, const char *e1,
828    const wchar_t *v2, const char *e2,
829    void *extra)
830{
831	assertion_count(file, line);
832	if (v1 == v2)
833		return (1);
834	if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
835		return (1);
836	failure_start(file, line, "%s != %s", e1, e2);
837	wcsdump(e1, v1);
838	wcsdump(e2, v2);
839	failure_finish(extra);
840	return (0);
841}
842
843/*
844 * Pretty standard hexdump routine.  As a bonus, if ref != NULL, then
845 * any bytes in p that differ from ref will be highlighted with '_'
846 * before and after the hex value.
847 */
848static void
849hexdump(const char *p, const char *ref, size_t l, size_t offset)
850{
851	size_t i, j;
852	char sep;
853
854	if (p == NULL) {
855		logprintf("(null)\n");
856		return;
857	}
858	for(i=0; i < l; i+=16) {
859		logprintf("%04x", (unsigned)(i + offset));
860		sep = ' ';
861		for (j = 0; j < 16 && i + j < l; j++) {
862			if (ref != NULL && p[i + j] != ref[i + j])
863				sep = '_';
864			logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
865			if (ref != NULL && p[i + j] == ref[i + j])
866				sep = ' ';
867		}
868		for (; j < 16; j++) {
869			logprintf("%c  ", sep);
870			sep = ' ';
871		}
872		logprintf("%c", sep);
873		for (j=0; j < 16 && i + j < l; j++) {
874			int c = p[i + j];
875			if (c >= ' ' && c <= 126)
876				logprintf("%c", c);
877			else
878				logprintf(".");
879		}
880		logprintf("\n");
881	}
882}
883
884/* Verify that two blocks of memory are the same, display the first
885 * block of differences if they're not. */
886int
887assertion_equal_mem(const char *file, int line,
888    const void *_v1, const char *e1,
889    const void *_v2, const char *e2,
890    size_t l, const char *ld, void *extra)
891{
892	const char *v1 = (const char *)_v1;
893	const char *v2 = (const char *)_v2;
894	size_t offset;
895
896	assertion_count(file, line);
897	if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
898		return (1);
899	if (v1 == NULL || v2 == NULL)
900		return (0);
901
902	failure_start(file, line, "%s != %s", e1, e2);
903	logprintf("      size %s = %d\n", ld, (int)l);
904	/* Dump 48 bytes (3 lines) so that the first difference is
905	 * in the second line. */
906	offset = 0;
907	while (l > 64 && memcmp(v1, v2, 32) == 0) {
908		/* Two lines agree, so step forward one line. */
909		v1 += 16;
910		v2 += 16;
911		l -= 16;
912		offset += 16;
913	}
914	logprintf("      Dump of %s\n", e1);
915	hexdump(v1, v2, l < 128 ? l : 128, offset);
916	logprintf("      Dump of %s\n", e2);
917	hexdump(v2, v1, l < 128 ? l : 128, offset);
918	logprintf("\n");
919	failure_finish(extra);
920	return (0);
921}
922
923/* Verify that a block of memory is filled with the specified byte. */
924int
925assertion_memory_filled_with(const char *file, int line,
926    const void *_v1, const char *vd,
927    size_t l, const char *ld,
928    char b, const char *bd, void *extra)
929{
930	const char *v1 = (const char *)_v1;
931	size_t c = 0;
932	size_t i;
933	(void)ld; /* UNUSED */
934
935	assertion_count(file, line);
936
937	for (i = 0; i < l; ++i) {
938		if (v1[i] == b) {
939			++c;
940		}
941	}
942	if (c == l)
943		return (1);
944
945	failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
946	logprintf("   Only %d bytes were correct\n", (int)c);
947	failure_finish(extra);
948	return (0);
949}
950
951/* Verify that the named file exists and is empty. */
952int
953assertion_empty_file(const char *filename, int line, const char *f1)
954{
955	char buff[1024];
956	struct stat st;
957	ssize_t s;
958	FILE *f;
959
960	assertion_count(filename, line);
961
962	if (stat(f1, &st) != 0) {
963		failure_start(filename, line, "Stat failed: %s", f1);
964		failure_finish(NULL);
965		return (0);
966	}
967	if (st.st_size == 0)
968		return (1);
969
970	failure_start(filename, line, "File should be empty: %s", f1);
971	logprintf("    File size: %d\n", (int)st.st_size);
972	logprintf("    Contents:\n");
973	f = fopen(f1, "rb");
974	if (f == NULL) {
975		logprintf("    Unable to open %s\n", f1);
976	} else {
977		s = ((off_t)sizeof(buff) < st.st_size) ?
978		    (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
979		s = fread(buff, 1, s, f);
980		hexdump(buff, NULL, s, 0);
981		fclose(f);
982	}
983	failure_finish(NULL);
984	return (0);
985}
986
987/* Verify that the named file exists and is not empty. */
988int
989assertion_non_empty_file(const char *filename, int line, const char *f1)
990{
991	struct stat st;
992
993	assertion_count(filename, line);
994
995	if (stat(f1, &st) != 0) {
996		failure_start(filename, line, "Stat failed: %s", f1);
997		failure_finish(NULL);
998		return (0);
999	}
1000	if (st.st_size == 0) {
1001		failure_start(filename, line, "File empty: %s", f1);
1002		failure_finish(NULL);
1003		return (0);
1004	}
1005	return (1);
1006}
1007
1008/* Verify that two files have the same contents. */
1009/* TODO: hexdump the first bytes that actually differ. */
1010int
1011assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
1012{
1013	char buff1[1024];
1014	char buff2[1024];
1015	FILE *f1, *f2;
1016	int n1, n2;
1017
1018	assertion_count(filename, line);
1019
1020	f1 = fopen(fn1, "rb");
1021	f2 = fopen(fn2, "rb");
1022	if (f1 == NULL || f2 == NULL) {
1023		if (f1) fclose(f1);
1024		if (f2) fclose(f2);
1025		return (0);
1026	}
1027	for (;;) {
1028		n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
1029		n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
1030		if (n1 != n2)
1031			break;
1032		if (n1 == 0 && n2 == 0) {
1033			fclose(f1);
1034			fclose(f2);
1035			return (1);
1036		}
1037		if (memcmp(buff1, buff2, n1) != 0)
1038			break;
1039	}
1040	fclose(f1);
1041	fclose(f2);
1042	failure_start(filename, line, "Files not identical");
1043	logprintf("  file1=\"%s\"\n", fn1);
1044	logprintf("  file2=\"%s\"\n", fn2);
1045	failure_finish(NULL);
1046	return (0);
1047}
1048
1049/* Verify that the named file does exist. */
1050int
1051assertion_file_exists(const char *filename, int line, const char *f)
1052{
1053	assertion_count(filename, line);
1054
1055#if defined(_WIN32) && !defined(__CYGWIN__)
1056	if (!_access(f, 0))
1057		return (1);
1058#else
1059	if (!access(f, F_OK))
1060		return (1);
1061#endif
1062	failure_start(filename, line, "File should exist: %s", f);
1063	failure_finish(NULL);
1064	return (0);
1065}
1066
1067/* Verify that the named file doesn't exist. */
1068int
1069assertion_file_not_exists(const char *filename, int line, const char *f)
1070{
1071	assertion_count(filename, line);
1072
1073#if defined(_WIN32) && !defined(__CYGWIN__)
1074	if (_access(f, 0))
1075		return (1);
1076#else
1077	if (access(f, F_OK))
1078		return (1);
1079#endif
1080	failure_start(filename, line, "File should not exist: %s", f);
1081	failure_finish(NULL);
1082	return (0);
1083}
1084
1085/* Compare the contents of a file to a block of memory. */
1086int
1087assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
1088{
1089	char *contents;
1090	FILE *f;
1091	int n;
1092
1093	assertion_count(filename, line);
1094
1095	f = fopen(fn, "rb");
1096	if (f == NULL) {
1097		failure_start(filename, line,
1098		    "File should exist: %s", fn);
1099		failure_finish(NULL);
1100		return (0);
1101	}
1102	contents = malloc(s * 2);
1103	n = (int)fread(contents, 1, s * 2, f);
1104	fclose(f);
1105	if (n == s && memcmp(buff, contents, s) == 0) {
1106		free(contents);
1107		return (1);
1108	}
1109	failure_start(filename, line, "File contents don't match");
1110	logprintf("  file=\"%s\"\n", fn);
1111	if (n > 0)
1112		hexdump(contents, buff, n > 512 ? 512 : n, 0);
1113	else {
1114		logprintf("  File empty, contents should be:\n");
1115		hexdump(buff, NULL, s > 512 ? 512 : s, 0);
1116	}
1117	failure_finish(NULL);
1118	free(contents);
1119	return (0);
1120}
1121
1122/* Check the contents of a text file, being tolerant of line endings. */
1123int
1124assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
1125{
1126	char *contents;
1127	const char *btxt, *ftxt;
1128	FILE *f;
1129	int n, s;
1130
1131	assertion_count(filename, line);
1132	f = fopen(fn, "r");
1133	if (f == NULL) {
1134		failure_start(filename, line,
1135		    "File doesn't exist: %s", fn);
1136		failure_finish(NULL);
1137		return (0);
1138	}
1139	s = (int)strlen(buff);
1140	contents = malloc(s * 2 + 128);
1141	n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
1142	if (n >= 0)
1143		contents[n] = '\0';
1144	fclose(f);
1145	/* Compare texts. */
1146	btxt = buff;
1147	ftxt = (const char *)contents;
1148	while (*btxt != '\0' && *ftxt != '\0') {
1149		if (*btxt == *ftxt) {
1150			++btxt;
1151			++ftxt;
1152			continue;
1153		}
1154		if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
1155			/* Pass over different new line characters. */
1156			++btxt;
1157			ftxt += 2;
1158			continue;
1159		}
1160		break;
1161	}
1162	if (*btxt == '\0' && *ftxt == '\0') {
1163		free(contents);
1164		return (1);
1165	}
1166	failure_start(filename, line, "Contents don't match");
1167	logprintf("  file=\"%s\"\n", fn);
1168	if (n > 0) {
1169		hexdump(contents, buff, n, 0);
1170		logprintf("  expected\n");
1171		hexdump(buff, contents, s, 0);
1172	} else {
1173		logprintf("  File empty, contents should be:\n");
1174		hexdump(buff, NULL, s, 0);
1175	}
1176	failure_finish(NULL);
1177	free(contents);
1178	return (0);
1179}
1180
1181/* Verify that a text file contains the specified lines, regardless of order */
1182/* This could be more efficient if we sorted both sets of lines, etc, but
1183 * since this is used only for testing and only ever deals with a dozen or so
1184 * lines at a time, this relatively crude approach is just fine. */
1185int
1186assertion_file_contains_lines_any_order(const char *file, int line,
1187    const char *pathname, const char *lines[])
1188{
1189	char *buff;
1190	size_t buff_size;
1191	size_t expected_count, actual_count, i, j;
1192	char **expected = NULL;
1193	char *p, **actual = NULL;
1194	char c;
1195	int expected_failure = 0, actual_failure = 0;
1196
1197	assertion_count(file, line);
1198
1199	buff = slurpfile(&buff_size, "%s", pathname);
1200	if (buff == NULL) {
1201		failure_start(pathname, line, "Can't read file: %s", pathname);
1202		failure_finish(NULL);
1203		return (0);
1204	}
1205
1206	/* Make a copy of the provided lines and count up the expected
1207	 * file size. */
1208	for (i = 0; lines[i] != NULL; ++i) {
1209	}
1210	expected_count = i;
1211	if (expected_count) {
1212		expected = malloc(sizeof(char *) * expected_count);
1213		if (expected == NULL) {
1214			failure_start(pathname, line, "Can't allocate memory");
1215			failure_finish(NULL);
1216			free(expected);
1217			free(buff);
1218			return (0);
1219		}
1220		for (i = 0; lines[i] != NULL; ++i) {
1221			expected[i] = strdup(lines[i]);
1222		}
1223	}
1224
1225	/* Break the file into lines */
1226	actual_count = 0;
1227	for (c = '\0', p = buff; p < buff + buff_size; ++p) {
1228		if (*p == '\x0d' || *p == '\x0a')
1229			*p = '\0';
1230		if (c == '\0' && *p != '\0')
1231			++actual_count;
1232		c = *p;
1233	}
1234	if (actual_count) {
1235		actual = calloc(sizeof(char *), actual_count);
1236		if (actual == NULL) {
1237			failure_start(pathname, line, "Can't allocate memory");
1238			failure_finish(NULL);
1239			free(expected);
1240			free(buff);
1241			return (0);
1242		}
1243		for (j = 0, p = buff; p < buff + buff_size;
1244		    p += 1 + strlen(p)) {
1245			if (*p != '\0') {
1246				actual[j] = p;
1247				++j;
1248			}
1249		}
1250	}
1251
1252	/* Erase matching lines from both lists */
1253	for (i = 0; i < expected_count; ++i) {
1254		if (expected[i] == NULL)
1255			continue;
1256		for (j = 0; j < actual_count; ++j) {
1257			if (actual[j] == NULL)
1258				continue;
1259			if (strcmp(expected[i], actual[j]) == 0) {
1260				free(expected[i]);
1261				expected[i] = NULL;
1262				actual[j] = NULL;
1263				break;
1264			}
1265		}
1266	}
1267
1268	/* If there's anything left, it's a failure */
1269	for (i = 0; i < expected_count; ++i) {
1270		if (expected[i] != NULL)
1271			++expected_failure;
1272	}
1273	for (j = 0; j < actual_count; ++j) {
1274		if (actual[j] != NULL)
1275			++actual_failure;
1276	}
1277	if (expected_failure == 0 && actual_failure == 0) {
1278		free(buff);
1279		free(expected);
1280		free(actual);
1281		return (1);
1282	}
1283	failure_start(file, line, "File doesn't match: %s", pathname);
1284	for (i = 0; i < expected_count; ++i) {
1285		if (expected[i] != NULL) {
1286			logprintf("  Expected but not present: %s\n", expected[i]);
1287			free(expected[i]);
1288		}
1289	}
1290	for (j = 0; j < actual_count; ++j) {
1291		if (actual[j] != NULL)
1292			logprintf("  Present but not expected: %s\n", actual[j]);
1293	}
1294	failure_finish(NULL);
1295	free(buff);
1296	free(expected);
1297	free(actual);
1298	return (0);
1299}
1300
1301/* Verify that a text file does not contains the specified strings */
1302int
1303assertion_file_contains_no_invalid_strings(const char *file, int line,
1304    const char *pathname, const char *strings[])
1305{
1306	char *buff;
1307	int i;
1308
1309	buff = slurpfile(NULL, "%s", pathname);
1310	if (buff == NULL) {
1311		failure_start(file, line, "Can't read file: %s", pathname);
1312		failure_finish(NULL);
1313		return (0);
1314	}
1315
1316	for (i = 0; strings[i] != NULL; ++i) {
1317		if (strstr(buff, strings[i]) != NULL) {
1318			failure_start(file, line, "Invalid string in %s: %s", pathname,
1319			    strings[i]);
1320			failure_finish(NULL);
1321			free(buff);
1322			return(0);
1323		}
1324	}
1325
1326	free(buff);
1327	return (0);
1328}
1329
1330/* Test that two paths point to the same file. */
1331/* As a side-effect, asserts that both files exist. */
1332static int
1333is_hardlink(const char *file, int line,
1334    const char *path1, const char *path2)
1335{
1336#if defined(_WIN32) && !defined(__CYGWIN__)
1337	BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
1338	int r;
1339
1340	assertion_count(file, line);
1341	r = my_GetFileInformationByName(path1, &bhfi1);
1342	if (r == 0) {
1343		failure_start(file, line, "File %s can't be inspected?", path1);
1344		failure_finish(NULL);
1345		return (0);
1346	}
1347	r = my_GetFileInformationByName(path2, &bhfi2);
1348	if (r == 0) {
1349		failure_start(file, line, "File %s can't be inspected?", path2);
1350		failure_finish(NULL);
1351		return (0);
1352	}
1353	return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
1354		&& bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
1355		&& bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
1356#else
1357	struct stat st1, st2;
1358	int r;
1359
1360	assertion_count(file, line);
1361	r = lstat(path1, &st1);
1362	if (r != 0) {
1363		failure_start(file, line, "File should exist: %s", path1);
1364		failure_finish(NULL);
1365		return (0);
1366	}
1367	r = lstat(path2, &st2);
1368	if (r != 0) {
1369		failure_start(file, line, "File should exist: %s", path2);
1370		failure_finish(NULL);
1371		return (0);
1372	}
1373	return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
1374#endif
1375}
1376
1377int
1378assertion_is_hardlink(const char *file, int line,
1379    const char *path1, const char *path2)
1380{
1381	if (is_hardlink(file, line, path1, path2))
1382		return (1);
1383	failure_start(file, line,
1384	    "Files %s and %s are not hardlinked", path1, path2);
1385	failure_finish(NULL);
1386	return (0);
1387}
1388
1389int
1390assertion_is_not_hardlink(const char *file, int line,
1391    const char *path1, const char *path2)
1392{
1393	if (!is_hardlink(file, line, path1, path2))
1394		return (1);
1395	failure_start(file, line,
1396	    "Files %s and %s should not be hardlinked", path1, path2);
1397	failure_finish(NULL);
1398	return (0);
1399}
1400
1401/* Verify a/b/mtime of 'pathname'. */
1402/* If 'recent', verify that it's within last 10 seconds. */
1403static int
1404assertion_file_time(const char *file, int line,
1405    const char *pathname, long t, long nsec, char type, int recent)
1406{
1407	long long filet, filet_nsec;
1408	int r;
1409
1410#if defined(_WIN32) && !defined(__CYGWIN__)
1411#define EPOC_TIME	(116444736000000000ULL)
1412	FILETIME fxtime, fbirthtime, fatime, fmtime;
1413	ULARGE_INTEGER wintm;
1414	HANDLE h;
1415	fxtime.dwLowDateTime = 0;
1416	fxtime.dwHighDateTime = 0;
1417
1418	assertion_count(file, line);
1419	/* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
1420	 * a directory file. If not, CreateFile() will fail when
1421	 * the pathname is a directory. */
1422	h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
1423	    OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1424	if (h == INVALID_HANDLE_VALUE) {
1425		failure_start(file, line, "Can't access %s\n", pathname);
1426		failure_finish(NULL);
1427		return (0);
1428	}
1429	r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
1430	switch (type) {
1431	case 'a': fxtime = fatime; break;
1432	case 'b': fxtime = fbirthtime; break;
1433	case 'm': fxtime = fmtime; break;
1434	}
1435	CloseHandle(h);
1436	if (r == 0) {
1437		failure_start(file, line, "Can't GetFileTime %s\n", pathname);
1438		failure_finish(NULL);
1439		return (0);
1440	}
1441	wintm.LowPart = fxtime.dwLowDateTime;
1442	wintm.HighPart = fxtime.dwHighDateTime;
1443	filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
1444	filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
1445	nsec = (nsec / 100) * 100; /* Round the request */
1446#else
1447	struct stat st;
1448
1449	assertion_count(file, line);
1450	r = lstat(pathname, &st);
1451	if (r != 0) {
1452		failure_start(file, line, "Can't stat %s\n", pathname);
1453		failure_finish(NULL);
1454		return (0);
1455	}
1456	switch (type) {
1457	case 'a': filet = st.st_atime; break;
1458	case 'm': filet = st.st_mtime; break;
1459	case 'b': filet = 0; break;
1460	default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1461		exit(1);
1462	}
1463#if defined(__FreeBSD__)
1464	switch (type) {
1465	case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
1466	case 'b': filet = st.st_birthtime;
1467		/* FreeBSD filesystems that don't support birthtime
1468		 * (e.g., UFS1) always return -1 here. */
1469		if (filet == -1) {
1470			return (1);
1471		}
1472		filet_nsec = st.st_birthtimespec.tv_nsec; break;
1473	case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
1474	default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
1475		exit(1);
1476	}
1477	/* FreeBSD generally only stores to microsecond res, so round. */
1478	filet_nsec = (filet_nsec / 1000) * 1000;
1479	nsec = (nsec / 1000) * 1000;
1480#else
1481	filet_nsec = nsec = 0;	/* Generic POSIX only has whole seconds. */
1482	if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
1483#if defined(__HAIKU__)
1484	if (type == 'a') return (1); /* Haiku doesn't have atime. */
1485#endif
1486#endif
1487#endif
1488	if (recent) {
1489		/* Check that requested time is up-to-date. */
1490		time_t now = time(NULL);
1491		if (filet < now - 10 || filet > now + 1) {
1492			failure_start(file, line,
1493			    "File %s has %ctime %lld, %lld seconds ago\n",
1494			    pathname, type, filet, now - filet);
1495			failure_finish(NULL);
1496			return (0);
1497		}
1498	} else if (filet != t || filet_nsec != nsec) {
1499		failure_start(file, line,
1500		    "File %s has %ctime %lld.%09lld, expected %ld.%09ld",
1501		    pathname, type, filet, filet_nsec, t, nsec);
1502		failure_finish(NULL);
1503		return (0);
1504	}
1505	return (1);
1506}
1507
1508/* Verify atime of 'pathname'. */
1509int
1510assertion_file_atime(const char *file, int line,
1511    const char *pathname, long t, long nsec)
1512{
1513	return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
1514}
1515
1516/* Verify atime of 'pathname' is up-to-date. */
1517int
1518assertion_file_atime_recent(const char *file, int line, const char *pathname)
1519{
1520	return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
1521}
1522
1523/* Verify birthtime of 'pathname'. */
1524int
1525assertion_file_birthtime(const char *file, int line,
1526    const char *pathname, long t, long nsec)
1527{
1528	return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
1529}
1530
1531/* Verify birthtime of 'pathname' is up-to-date. */
1532int
1533assertion_file_birthtime_recent(const char *file, int line,
1534    const char *pathname)
1535{
1536	return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
1537}
1538
1539/* Verify mode of 'pathname'. */
1540int
1541assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
1542{
1543	int mode;
1544	int r;
1545
1546	assertion_count(file, line);
1547#if defined(_WIN32) && !defined(__CYGWIN__)
1548	failure_start(file, line, "assertFileMode not yet implemented for Windows");
1549	(void)mode; /* UNUSED */
1550	(void)r; /* UNUSED */
1551	(void)pathname; /* UNUSED */
1552	(void)expected_mode; /* UNUSED */
1553#else
1554	{
1555		struct stat st;
1556		r = lstat(pathname, &st);
1557		mode = (int)(st.st_mode & 0777);
1558	}
1559	if (r == 0 && mode == expected_mode)
1560			return (1);
1561	failure_start(file, line, "File %s has mode %o, expected %o",
1562	    pathname, mode, expected_mode);
1563#endif
1564	failure_finish(NULL);
1565	return (0);
1566}
1567
1568/* Verify mtime of 'pathname'. */
1569int
1570assertion_file_mtime(const char *file, int line,
1571    const char *pathname, long t, long nsec)
1572{
1573	return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
1574}
1575
1576/* Verify mtime of 'pathname' is up-to-date. */
1577int
1578assertion_file_mtime_recent(const char *file, int line, const char *pathname)
1579{
1580	return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
1581}
1582
1583/* Verify number of links to 'pathname'. */
1584int
1585assertion_file_nlinks(const char *file, int line,
1586    const char *pathname, int nlinks)
1587{
1588#if defined(_WIN32) && !defined(__CYGWIN__)
1589	BY_HANDLE_FILE_INFORMATION bhfi;
1590	int r;
1591
1592	assertion_count(file, line);
1593	r = my_GetFileInformationByName(pathname, &bhfi);
1594	if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
1595		return (1);
1596	failure_start(file, line, "File %s has %jd links, expected %d",
1597	    pathname, (intmax_t)bhfi.nNumberOfLinks, nlinks);
1598	failure_finish(NULL);
1599	return (0);
1600#else
1601	struct stat st;
1602	int r;
1603
1604	assertion_count(file, line);
1605	r = lstat(pathname, &st);
1606	if (r == 0 && (int)st.st_nlink == nlinks)
1607		return (1);
1608	failure_start(file, line, "File %s has %jd links, expected %d",
1609	    pathname, (intmax_t)st.st_nlink, nlinks);
1610	failure_finish(NULL);
1611	return (0);
1612#endif
1613}
1614
1615/* Verify size of 'pathname'. */
1616int
1617assertion_file_size(const char *file, int line, const char *pathname, long size)
1618{
1619	int64_t filesize;
1620	int r;
1621
1622	assertion_count(file, line);
1623#if defined(_WIN32) && !defined(__CYGWIN__)
1624	{
1625		BY_HANDLE_FILE_INFORMATION bhfi;
1626		r = !my_GetFileInformationByName(pathname, &bhfi);
1627		filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
1628	}
1629#else
1630	{
1631		struct stat st;
1632		r = lstat(pathname, &st);
1633		filesize = st.st_size;
1634	}
1635#endif
1636	if (r == 0 && filesize == size)
1637			return (1);
1638	failure_start(file, line, "File %s has size %ld, expected %ld",
1639	    pathname, (long)filesize, (long)size);
1640	failure_finish(NULL);
1641	return (0);
1642}
1643
1644/* Assert that 'pathname' is a dir.  If mode >= 0, verify that too. */
1645int
1646assertion_is_dir(const char *file, int line, const char *pathname, int mode)
1647{
1648	struct stat st;
1649	int r;
1650
1651#if defined(_WIN32) && !defined(__CYGWIN__)
1652	(void)mode; /* UNUSED */
1653#endif
1654	assertion_count(file, line);
1655	r = lstat(pathname, &st);
1656	if (r != 0) {
1657		failure_start(file, line, "Dir should exist: %s", pathname);
1658		failure_finish(NULL);
1659		return (0);
1660	}
1661	if (!S_ISDIR(st.st_mode)) {
1662		failure_start(file, line, "%s is not a dir", pathname);
1663		failure_finish(NULL);
1664		return (0);
1665	}
1666#if !defined(_WIN32) || defined(__CYGWIN__)
1667	/* Windows doesn't handle permissions the same way as POSIX,
1668	 * so just ignore the mode tests. */
1669	/* TODO: Can we do better here? */
1670	if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1671		failure_start(file, line, "Dir %s has wrong mode", pathname);
1672		logprintf("  Expected: 0%3o\n", mode);
1673		logprintf("  Found: 0%3o\n", st.st_mode & 07777);
1674		failure_finish(NULL);
1675		return (0);
1676	}
1677#endif
1678	return (1);
1679}
1680
1681/* Verify that 'pathname' is a regular file.  If 'mode' is >= 0,
1682 * verify that too. */
1683int
1684assertion_is_reg(const char *file, int line, const char *pathname, int mode)
1685{
1686	struct stat st;
1687	int r;
1688
1689#if defined(_WIN32) && !defined(__CYGWIN__)
1690	(void)mode; /* UNUSED */
1691#endif
1692	assertion_count(file, line);
1693	r = lstat(pathname, &st);
1694	if (r != 0 || !S_ISREG(st.st_mode)) {
1695		failure_start(file, line, "File should exist: %s", pathname);
1696		failure_finish(NULL);
1697		return (0);
1698	}
1699#if !defined(_WIN32) || defined(__CYGWIN__)
1700	/* Windows doesn't handle permissions the same way as POSIX,
1701	 * so just ignore the mode tests. */
1702	/* TODO: Can we do better here? */
1703	if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
1704		failure_start(file, line, "File %s has wrong mode", pathname);
1705		logprintf("  Expected: 0%3o\n", mode);
1706		logprintf("  Found: 0%3o\n", st.st_mode & 07777);
1707		failure_finish(NULL);
1708		return (0);
1709	}
1710#endif
1711	return (1);
1712}
1713
1714/*
1715 * Check whether 'pathname' is a symbolic link.  If 'contents' is
1716 * non-NULL, verify that the symlink has those contents.
1717 *
1718 * On platforms with directory symlinks, set isdir to 0 to test for a file
1719 * symlink and to 1 to test for a directory symlink. On other platforms
1720 * the variable is ignored.
1721 */
1722static int
1723is_symlink(const char *file, int line,
1724    const char *pathname, const char *contents, int isdir)
1725{
1726#if defined(_WIN32) && !defined(__CYGWIN__)
1727	HANDLE h;
1728	DWORD inbytes;
1729	REPARSE_DATA_BUFFER *buf;
1730	BY_HANDLE_FILE_INFORMATION st;
1731	size_t len, len2;
1732	wchar_t *linknamew, *contentsw;
1733	const char *p;
1734	char *s, *pn;
1735	int ret = 0;
1736	BYTE *indata;
1737	const DWORD flag = FILE_FLAG_BACKUP_SEMANTICS |
1738	    FILE_FLAG_OPEN_REPARSE_POINT;
1739
1740	/* Replace slashes with backslashes in pathname */
1741	pn = malloc((strlen(pathname) + 1) * sizeof(char));
1742	p = pathname;
1743	s = pn;
1744	while(*p != '\0') {
1745		if(*p == '/')
1746			*s = '\\';
1747		else
1748			*s = *p;
1749		p++;
1750		s++;
1751	}
1752	*s = '\0';
1753
1754	h = CreateFileA(pn, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1755	    flag, NULL);
1756	free(pn);
1757	if (h == INVALID_HANDLE_VALUE) {
1758		failure_start(file, line, "Can't access %s\n", pathname);
1759		failure_finish(NULL);
1760		return (0);
1761	}
1762	ret = GetFileInformationByHandle(h, &st);
1763	if (ret == 0) {
1764		failure_start(file, line,
1765		    "Can't stat: %s", pathname);
1766		failure_finish(NULL);
1767	} else if ((st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
1768		failure_start(file, line,
1769		    "Not a symlink: %s", pathname);
1770		failure_finish(NULL);
1771		ret = 0;
1772	}
1773	if (isdir && ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
1774		failure_start(file, line,
1775		    "Not a directory symlink: %s", pathname);
1776		failure_finish(NULL);
1777		ret = 0;
1778	}
1779	if (!isdir &&
1780	    ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) {
1781		failure_start(file, line,
1782		    "Not a file symlink: %s", pathname);
1783		failure_finish(NULL);
1784		ret = 0;
1785	}
1786	if (ret == 0) {
1787		CloseHandle(h);
1788		return (0);
1789	}
1790
1791	indata = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
1792	ret = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, indata,
1793	    1024, &inbytes, NULL);
1794	CloseHandle(h);
1795	if (ret == 0) {
1796		free(indata);
1797		failure_start(file, line,
1798		    "Could not retrieve symlink target: %s", pathname);
1799		failure_finish(NULL);
1800		return (0);
1801	}
1802
1803	buf = (REPARSE_DATA_BUFFER *) indata;
1804	if (buf->ReparseTag != IO_REPARSE_TAG_SYMLINK) {
1805		free(indata);
1806		/* File is not a symbolic link */
1807		failure_start(file, line,
1808		    "Not a symlink: %s", pathname);
1809		failure_finish(NULL);
1810		return (0);
1811	}
1812
1813	if (contents == NULL) {
1814		free(indata);
1815		return (1);
1816	}
1817
1818	len = buf->SymbolicLinkReparseBuffer.SubstituteNameLength;
1819
1820	linknamew = malloc(len + sizeof(wchar_t));
1821	if (linknamew == NULL) {
1822		free(indata);
1823		return (0);
1824	}
1825
1826	memcpy(linknamew, &((BYTE *)buf->SymbolicLinkReparseBuffer.PathBuffer)
1827	    [buf->SymbolicLinkReparseBuffer.SubstituteNameOffset], len);
1828	free(indata);
1829
1830	linknamew[len / sizeof(wchar_t)] = L'\0';
1831
1832	contentsw = malloc(len + sizeof(wchar_t));
1833	if (contentsw == NULL) {
1834		free(linknamew);
1835		return (0);
1836	}
1837
1838	len2 = mbsrtowcs(contentsw, &contents, (len + sizeof(wchar_t)
1839	    / sizeof(wchar_t)), NULL);
1840
1841	if (len2 > 0 && wcscmp(linknamew, contentsw) != 0)
1842		ret = 1;
1843
1844	free(linknamew);
1845	free(contentsw);
1846	return (ret);
1847#else
1848	char buff[300];
1849	struct stat st;
1850	ssize_t linklen;
1851	int r;
1852
1853	(void)isdir; /* UNUSED */
1854	assertion_count(file, line);
1855	r = lstat(pathname, &st);
1856	if (r != 0) {
1857		failure_start(file, line,
1858		    "Symlink should exist: %s", pathname);
1859		failure_finish(NULL);
1860		return (0);
1861	}
1862	if (!S_ISLNK(st.st_mode))
1863		return (0);
1864	if (contents == NULL)
1865		return (1);
1866	linklen = readlink(pathname, buff, sizeof(buff) - 1);
1867	if (linklen < 0) {
1868		failure_start(file, line, "Can't read symlink %s", pathname);
1869		failure_finish(NULL);
1870		return (0);
1871	}
1872	buff[linklen] = '\0';
1873	if (strcmp(buff, contents) != 0)
1874		return (0);
1875	return (1);
1876#endif
1877}
1878
1879/* Assert that path is a symlink that (optionally) contains contents. */
1880int
1881assertion_is_symlink(const char *file, int line,
1882    const char *path, const char *contents, int isdir)
1883{
1884	if (is_symlink(file, line, path, contents, isdir))
1885		return (1);
1886	if (contents)
1887		failure_start(file, line, "File %s is not a symlink to %s",
1888		    path, contents);
1889	else
1890		failure_start(file, line, "File %s is not a symlink", path);
1891	failure_finish(NULL);
1892	return (0);
1893}
1894
1895
1896/* Create a directory and report any errors. */
1897int
1898assertion_make_dir(const char *file, int line, const char *dirname, int mode)
1899{
1900	assertion_count(file, line);
1901#if defined(_WIN32) && !defined(__CYGWIN__)
1902	(void)mode; /* UNUSED */
1903	if (0 == _mkdir(dirname))
1904		return (1);
1905#else
1906	if (0 == mkdir(dirname, mode)) {
1907		if (0 == chmod(dirname, mode)) {
1908			assertion_file_mode(file, line, dirname, mode);
1909			return (1);
1910		}
1911	}
1912#endif
1913	failure_start(file, line, "Could not create directory %s", dirname);
1914	failure_finish(NULL);
1915	return(0);
1916}
1917
1918/* Create a file with the specified contents and report any failures. */
1919int
1920assertion_make_file(const char *file, int line,
1921    const char *path, int mode, int csize, const void *contents)
1922{
1923#if defined(_WIN32) && !defined(__CYGWIN__)
1924	/* TODO: Rework this to set file mode as well. */
1925	FILE *f;
1926	(void)mode; /* UNUSED */
1927	assertion_count(file, line);
1928	f = fopen(path, "wb");
1929	if (f == NULL) {
1930		failure_start(file, line, "Could not create file %s", path);
1931		failure_finish(NULL);
1932		return (0);
1933	}
1934	if (contents != NULL) {
1935		size_t wsize;
1936
1937		if (csize < 0)
1938			wsize = strlen(contents);
1939		else
1940			wsize = (size_t)csize;
1941		if (wsize != fwrite(contents, 1, wsize, f)) {
1942			fclose(f);
1943			failure_start(file, line,
1944			    "Could not write file %s", path);
1945			failure_finish(NULL);
1946			return (0);
1947		}
1948	}
1949	fclose(f);
1950	return (1);
1951#else
1952	int fd;
1953	assertion_count(file, line);
1954	fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
1955	if (fd < 0) {
1956		failure_start(file, line, "Could not create %s", path);
1957		failure_finish(NULL);
1958		return (0);
1959	}
1960	if (0 != chmod(path, mode)) {
1961		failure_start(file, line, "Could not chmod %s", path);
1962		failure_finish(NULL);
1963		close(fd);
1964		return (0);
1965	}
1966	if (contents != NULL) {
1967		ssize_t wsize;
1968
1969		if (csize < 0)
1970			wsize = (ssize_t)strlen(contents);
1971		else
1972			wsize = (ssize_t)csize;
1973		if (wsize != write(fd, contents, wsize)) {
1974			close(fd);
1975			failure_start(file, line,
1976			    "Could not write to %s", path);
1977			failure_finish(NULL);
1978			close(fd);
1979			return (0);
1980		}
1981	}
1982	close(fd);
1983	assertion_file_mode(file, line, path, mode);
1984	return (1);
1985#endif
1986}
1987
1988/* Create a hardlink and report any failures. */
1989int
1990assertion_make_hardlink(const char *file, int line,
1991    const char *newpath, const char *linkto)
1992{
1993	int succeeded;
1994
1995	assertion_count(file, line);
1996#if defined(_WIN32) && !defined(__CYGWIN__)
1997	succeeded = my_CreateHardLinkA(newpath, linkto);
1998#elif HAVE_LINK
1999	succeeded = !link(linkto, newpath);
2000#else
2001	succeeded = 0;
2002#endif
2003	if (succeeded)
2004		return (1);
2005	failure_start(file, line, "Could not create hardlink");
2006	logprintf("   New link: %s\n", newpath);
2007	logprintf("   Old name: %s\n", linkto);
2008	failure_finish(NULL);
2009	return(0);
2010}
2011
2012/*
2013 * Create a symlink and report any failures.
2014 *
2015 * Windows symlinks need to know if the target is a directory.
2016 */
2017int
2018assertion_make_symlink(const char *file, int line,
2019    const char *newpath, const char *linkto, int targetIsDir)
2020{
2021#if defined(_WIN32) && !defined(__CYGWIN__)
2022	assertion_count(file, line);
2023	if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
2024		return (1);
2025#elif HAVE_SYMLINK
2026	(void)targetIsDir; /* UNUSED */
2027	assertion_count(file, line);
2028	if (0 == symlink(linkto, newpath))
2029		return (1);
2030#else
2031	(void)targetIsDir; /* UNUSED */
2032#endif
2033	failure_start(file, line, "Could not create symlink");
2034	logprintf("   New link: %s\n", newpath);
2035	logprintf("   Old name: %s\n", linkto);
2036	failure_finish(NULL);
2037	return(0);
2038}
2039
2040/* Set umask, report failures. */
2041int
2042assertion_umask(const char *file, int line, int mask)
2043{
2044	assertion_count(file, line);
2045	(void)file; /* UNUSED */
2046	(void)line; /* UNUSED */
2047	umask(mask);
2048	return (1);
2049}
2050
2051/* Set times, report failures. */
2052int
2053assertion_utimes(const char *file, int line,
2054    const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
2055{
2056	int r;
2057
2058#if defined(_WIN32) && !defined(__CYGWIN__)
2059#define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
2060	 + (((nsec)/1000)*10))
2061	HANDLE h;
2062	ULARGE_INTEGER wintm;
2063	FILETIME fatime, fmtime;
2064	FILETIME *pat, *pmt;
2065
2066	assertion_count(file, line);
2067	h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
2068		    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
2069		    FILE_FLAG_BACKUP_SEMANTICS, NULL);
2070	if (h == INVALID_HANDLE_VALUE) {
2071		failure_start(file, line, "Can't access %s\n", pathname);
2072		failure_finish(NULL);
2073		return (0);
2074	}
2075
2076	if (at > 0 || at_nsec > 0) {
2077		wintm.QuadPart = WINTIME(at, at_nsec);
2078		fatime.dwLowDateTime = wintm.LowPart;
2079		fatime.dwHighDateTime = wintm.HighPart;
2080		pat = &fatime;
2081	} else
2082		pat = NULL;
2083	if (mt > 0 || mt_nsec > 0) {
2084		wintm.QuadPart = WINTIME(mt, mt_nsec);
2085		fmtime.dwLowDateTime = wintm.LowPart;
2086		fmtime.dwHighDateTime = wintm.HighPart;
2087		pmt = &fmtime;
2088	} else
2089		pmt = NULL;
2090	if (pat != NULL || pmt != NULL)
2091		r = SetFileTime(h, NULL, pat, pmt);
2092	else
2093		r = 1;
2094	CloseHandle(h);
2095	if (r == 0) {
2096		failure_start(file, line, "Can't SetFileTime %s\n", pathname);
2097		failure_finish(NULL);
2098		return (0);
2099	}
2100	return (1);
2101#else /* defined(_WIN32) && !defined(__CYGWIN__) */
2102	struct stat st;
2103	struct timeval times[2];
2104
2105#if !defined(__FreeBSD__)
2106	mt_nsec = at_nsec = 0;	/* Generic POSIX only has whole seconds. */
2107#endif
2108	if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
2109		return (1);
2110
2111	r = lstat(pathname, &st);
2112	if (r < 0) {
2113		failure_start(file, line, "Can't stat %s\n", pathname);
2114		failure_finish(NULL);
2115		return (0);
2116	}
2117
2118	if (mt == 0 && mt_nsec == 0) {
2119		mt = st.st_mtime;
2120#if defined(__FreeBSD__)
2121		mt_nsec = st.st_mtimespec.tv_nsec;
2122		/* FreeBSD generally only stores to microsecond res, so round. */
2123		mt_nsec = (mt_nsec / 1000) * 1000;
2124#endif
2125	}
2126	if (at == 0 && at_nsec == 0) {
2127		at = st.st_atime;
2128#if defined(__FreeBSD__)
2129		at_nsec = st.st_atimespec.tv_nsec;
2130		/* FreeBSD generally only stores to microsecond res, so round. */
2131		at_nsec = (at_nsec / 1000) * 1000;
2132#endif
2133	}
2134
2135	times[1].tv_sec = mt;
2136	times[1].tv_usec = mt_nsec / 1000;
2137
2138	times[0].tv_sec = at;
2139	times[0].tv_usec = at_nsec / 1000;
2140
2141#ifdef HAVE_LUTIMES
2142	r = lutimes(pathname, times);
2143#else
2144	r = utimes(pathname, times);
2145#endif
2146	if (r < 0) {
2147		failure_start(file, line, "Can't utimes %s\n", pathname);
2148		failure_finish(NULL);
2149		return (0);
2150	}
2151	return (1);
2152#endif /* defined(_WIN32) && !defined(__CYGWIN__) */
2153}
2154
2155/* Compare file flags */
2156int
2157assertion_compare_fflags(const char *file, int line, const char *patha,
2158    const char *pathb, int nomatch)
2159{
2160#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2161	struct stat sa, sb;
2162
2163	assertion_count(file, line);
2164
2165	if (stat(patha, &sa) < 0)
2166		return (0);
2167	if (stat(pathb, &sb) < 0)
2168		return (0);
2169	if (!nomatch && sa.st_flags != sb.st_flags) {
2170		failure_start(file, line, "File flags should be identical: "
2171		    "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
2172		    sb.st_flags);
2173		failure_finish(NULL);
2174		return (0);
2175	}
2176	if (nomatch && sa.st_flags == sb.st_flags) {
2177		failure_start(file, line, "File flags should be different: "
2178		    "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
2179		    sb.st_flags);
2180		failure_finish(NULL);
2181		return (0);
2182	}
2183#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
2184       defined(FS_NODUMP_FL)) || \
2185      (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2186         && defined(EXT2_NODUMP_FL))
2187	int fd, r, flagsa, flagsb;
2188
2189	assertion_count(file, line);
2190	fd = open(patha, O_RDONLY | O_NONBLOCK);
2191	if (fd < 0) {
2192		failure_start(file, line, "Can't open %s\n", patha);
2193		failure_finish(NULL);
2194		return (0);
2195	}
2196	r = ioctl(fd,
2197#ifdef FS_IOC_GETFLAGS
2198	    FS_IOC_GETFLAGS,
2199#else
2200	    EXT2_IOC_GETFLAGS,
2201#endif
2202	    &flagsa);
2203	close(fd);
2204	if (r < 0) {
2205		failure_start(file, line, "Can't get flags %s\n", patha);
2206		failure_finish(NULL);
2207		return (0);
2208	}
2209	fd = open(pathb, O_RDONLY | O_NONBLOCK);
2210	if (fd < 0) {
2211		failure_start(file, line, "Can't open %s\n", pathb);
2212		failure_finish(NULL);
2213		return (0);
2214	}
2215	r = ioctl(fd,
2216#ifdef FS_IOC_GETFLAGS
2217	    FS_IOC_GETFLAGS,
2218#else
2219	    EXT2_IOC_GETFLAGS,
2220#endif
2221	    &flagsb);
2222	close(fd);
2223	if (r < 0) {
2224		failure_start(file, line, "Can't get flags %s\n", pathb);
2225		failure_finish(NULL);
2226		return (0);
2227	}
2228	if (!nomatch && flagsa != flagsb) {
2229		failure_start(file, line, "File flags should be identical: "
2230		    "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
2231		failure_finish(NULL);
2232		return (0);
2233	}
2234	if (nomatch && flagsa == flagsb) {
2235		failure_start(file, line, "File flags should be different: "
2236		    "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
2237		failure_finish(NULL);
2238		return (0);
2239	}
2240#else
2241	(void)patha; /* UNUSED */
2242	(void)pathb; /* UNUSED */
2243	(void)nomatch; /* UNUSED */
2244	assertion_count(file, line);
2245#endif
2246	return (1);
2247}
2248
2249/* Set nodump, report failures. */
2250int
2251assertion_set_nodump(const char *file, int line, const char *pathname)
2252{
2253#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2254	int r;
2255
2256	assertion_count(file, line);
2257	r = chflags(pathname, UF_NODUMP);
2258	if (r < 0) {
2259		failure_start(file, line, "Can't set nodump %s\n", pathname);
2260		failure_finish(NULL);
2261		return (0);
2262	}
2263#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
2264       defined(FS_NODUMP_FL)) || \
2265      (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2266	 && defined(EXT2_NODUMP_FL))
2267	int fd, r, flags;
2268
2269	assertion_count(file, line);
2270	fd = open(pathname, O_RDONLY | O_NONBLOCK);
2271	if (fd < 0) {
2272		failure_start(file, line, "Can't open %s\n", pathname);
2273		failure_finish(NULL);
2274		return (0);
2275	}
2276	r = ioctl(fd,
2277#ifdef FS_IOC_GETFLAGS
2278	    FS_IOC_GETFLAGS,
2279#else
2280	    EXT2_IOC_GETFLAGS,
2281#endif
2282	    &flags);
2283	if (r < 0) {
2284		failure_start(file, line, "Can't get flags %s\n", pathname);
2285		failure_finish(NULL);
2286		return (0);
2287	}
2288#ifdef FS_NODUMP_FL
2289	flags |= FS_NODUMP_FL;
2290#else
2291	flags |= EXT2_NODUMP_FL;
2292#endif
2293
2294	 r = ioctl(fd,
2295#ifdef FS_IOC_SETFLAGS
2296	    FS_IOC_SETFLAGS,
2297#else
2298	    EXT2_IOC_SETFLAGS,
2299#endif
2300	    &flags);
2301	if (r < 0) {
2302		failure_start(file, line, "Can't set nodump %s\n", pathname);
2303		failure_finish(NULL);
2304		return (0);
2305	}
2306	close(fd);
2307#else
2308	(void)pathname; /* UNUSED */
2309	assertion_count(file, line);
2310#endif
2311	return (1);
2312}
2313
2314#ifdef PROGRAM
2315static void assert_version_id(char **qq, size_t *ss)
2316{
2317	char *q = *qq;
2318	size_t s = *ss;
2319
2320	/* Version number is a series of digits and periods. */
2321	while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
2322		++q;
2323		--s;
2324	}
2325
2326	if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') {
2327		q += 3;
2328		s -= 3;
2329	}
2330
2331	/* Skip a single trailing a,b,c, or d. */
2332	if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
2333		++q;
2334
2335	/* Version number terminated by space. */
2336	failure("No space after version: ``%s''", q);
2337	assert(s > 1);
2338	failure("No space after version: ``%s''", q);
2339	assert(*q == ' ');
2340
2341	++q; --s;
2342
2343	*qq = q;
2344	*ss = s;
2345}
2346
2347
2348/*
2349 * Check program version
2350 */
2351void assertVersion(const char *prog, const char *base)
2352{
2353	int r;
2354	char *p, *q;
2355	size_t s;
2356	size_t prog_len = strlen(base);
2357
2358	r = systemf("%s --version >version.stdout 2>version.stderr", prog);
2359	if (r != 0)
2360		r = systemf("%s -W version >version.stdout 2>version.stderr",
2361		    prog);
2362
2363	failure("Unable to run either %s --version or %s -W version",
2364		prog, prog);
2365	if (!assert(r == 0))
2366		return;
2367
2368	/* --version should generate nothing to stdout. */
2369	assertEmptyFile("version.stderr");
2370
2371	/* Verify format of version message. */
2372	q = p = slurpfile(&s, "version.stdout");
2373
2374	/* Version message should start with name of program, then space. */
2375	assert(s > prog_len + 1);
2376
2377	failure("Version must start with '%s': ``%s''", base, p);
2378	if (!assertEqualMem(q, base, prog_len)) {
2379		free(p);
2380		return;
2381	}
2382
2383	q += prog_len; s -= prog_len;
2384
2385	assert(*q == ' ');
2386	q++; s--;
2387
2388	assert_version_id(&q, &s);
2389
2390	/* Separator. */
2391	failure("No `-' between program name and versions: ``%s''", p);
2392	assertEqualMem(q, "- ", 2);
2393	q += 2; s -= 2;
2394
2395	failure("Not long enough for libarchive version: ``%s''", p);
2396	assert(s > 11);
2397
2398	failure("Libarchive version must start with `libarchive': ``%s''", p);
2399	assertEqualMem(q, "libarchive ", 11);
2400
2401	q += 11; s -= 11;
2402
2403	assert_version_id(&q, &s);
2404
2405	/* Skip arbitrary third-party version numbers. */
2406	while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' ||
2407	    isalnum((unsigned char)*q))) {
2408		++q;
2409		--s;
2410	}
2411
2412	/* All terminated by end-of-line. */
2413	assert(s >= 1);
2414
2415	/* Skip an optional CR character (e.g., Windows) */
2416	failure("Version output must end with \\n or \\r\\n");
2417
2418	if (*q == '\r') { ++q; --s; }
2419	assertEqualMem(q, "\n", 1);
2420
2421	free(p);
2422}
2423#endif	/* PROGRAM */
2424
2425/*
2426 *
2427 *  UTILITIES for use by tests.
2428 *
2429 */
2430
2431/*
2432 * Check whether platform supports symlinks.  This is intended
2433 * for tests to use in deciding whether to bother testing symlink
2434 * support; if the platform doesn't support symlinks, there's no point
2435 * in checking whether the program being tested can create them.
2436 *
2437 * Note that the first time this test is called, we actually go out to
2438 * disk to create and verify a symlink.  This is necessary because
2439 * symlink support is actually a property of a particular filesystem
2440 * and can thus vary between directories on a single system.  After
2441 * the first call, this returns the cached result from memory, so it's
2442 * safe to call it as often as you wish.
2443 */
2444int
2445canSymlink(void)
2446{
2447	/* Remember the test result */
2448	static int value = 0, tested = 0;
2449	if (tested)
2450		return (value);
2451
2452	++tested;
2453	assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
2454	/* Note: Cygwin has its own symlink() emulation that does not
2455	 * use the Win32 CreateSymbolicLink() function. */
2456#if defined(_WIN32) && !defined(__CYGWIN__)
2457	value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
2458	    && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0",
2459	    0);
2460#elif HAVE_SYMLINK
2461	value = (0 == symlink("canSymlink.0", "canSymlink.1"))
2462	    && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0",
2463	    0);
2464#endif
2465	return (value);
2466}
2467
2468/* Platform-dependent options for hiding the output of a subcommand. */
2469#if defined(_WIN32) && !defined(__CYGWIN__)
2470static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
2471#else
2472static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
2473#endif
2474/*
2475 * Can this platform run the bzip2 program?
2476 */
2477int
2478canBzip2(void)
2479{
2480	static int tested = 0, value = 0;
2481	if (!tested) {
2482		tested = 1;
2483		if (systemf("bzip2 --help %s", redirectArgs) == 0)
2484			value = 1;
2485	}
2486	return (value);
2487}
2488
2489/*
2490 * Can this platform run the grzip program?
2491 */
2492int
2493canGrzip(void)
2494{
2495	static int tested = 0, value = 0;
2496	if (!tested) {
2497		tested = 1;
2498		if (systemf("grzip -V %s", redirectArgs) == 0)
2499			value = 1;
2500	}
2501	return (value);
2502}
2503
2504/*
2505 * Can this platform run the gzip program?
2506 */
2507int
2508canGzip(void)
2509{
2510	static int tested = 0, value = 0;
2511	if (!tested) {
2512		tested = 1;
2513		if (systemf("gzip --help %s", redirectArgs) == 0)
2514			value = 1;
2515	}
2516	return (value);
2517}
2518
2519/*
2520 * Can this platform run the lrzip program?
2521 */
2522int
2523canRunCommand(const char *cmd)
2524{
2525  static int tested = 0, value = 0;
2526  if (!tested) {
2527    tested = 1;
2528    if (systemf("%s %s", cmd, redirectArgs) == 0)
2529      value = 1;
2530  }
2531  return (value);
2532}
2533
2534int
2535canLrzip(void)
2536{
2537	static int tested = 0, value = 0;
2538	if (!tested) {
2539		tested = 1;
2540		if (systemf("lrzip -V %s", redirectArgs) == 0)
2541			value = 1;
2542	}
2543	return (value);
2544}
2545
2546/*
2547 * Can this platform run the lz4 program?
2548 */
2549int
2550canLz4(void)
2551{
2552	static int tested = 0, value = 0;
2553	if (!tested) {
2554		tested = 1;
2555		if (systemf("lz4 --help %s", redirectArgs) == 0)
2556			value = 1;
2557	}
2558	return (value);
2559}
2560
2561/*
2562 * Can this platform run the zstd program?
2563 */
2564int
2565canZstd(void)
2566{
2567	static int tested = 0, value = 0;
2568	if (!tested) {
2569		tested = 1;
2570		if (systemf("zstd --help %s", redirectArgs) == 0)
2571			value = 1;
2572	}
2573	return (value);
2574}
2575
2576/*
2577 * Can this platform run the lzip program?
2578 */
2579int
2580canLzip(void)
2581{
2582	static int tested = 0, value = 0;
2583	if (!tested) {
2584		tested = 1;
2585		if (systemf("lzip --help %s", redirectArgs) == 0)
2586			value = 1;
2587	}
2588	return (value);
2589}
2590
2591/*
2592 * Can this platform run the lzma program?
2593 */
2594int
2595canLzma(void)
2596{
2597	static int tested = 0, value = 0;
2598	if (!tested) {
2599		tested = 1;
2600		if (systemf("lzma %s", redirectArgs) == 0)
2601			value = 1;
2602	}
2603	return (value);
2604}
2605
2606/*
2607 * Can this platform run the lzop program?
2608 */
2609int
2610canLzop(void)
2611{
2612	static int tested = 0, value = 0;
2613	if (!tested) {
2614		tested = 1;
2615		if (systemf("lzop --help %s", redirectArgs) == 0)
2616			value = 1;
2617	}
2618	return (value);
2619}
2620
2621/*
2622 * Can this platform run the xz program?
2623 */
2624int
2625canXz(void)
2626{
2627	static int tested = 0, value = 0;
2628	if (!tested) {
2629		tested = 1;
2630		if (systemf("xz --help %s", redirectArgs) == 0)
2631			value = 1;
2632	}
2633	return (value);
2634}
2635
2636/*
2637 * Can this filesystem handle nodump flags.
2638 */
2639int
2640canNodump(void)
2641{
2642#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
2643	const char *path = "cannodumptest";
2644	struct stat sb;
2645
2646	assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2647	if (chflags(path, UF_NODUMP) < 0)
2648		return (0);
2649	if (stat(path, &sb) < 0)
2650		return (0);
2651	if (sb.st_flags & UF_NODUMP)
2652		return (1);
2653#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \
2654	 && defined(FS_NODUMP_FL)) || \
2655      (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
2656	 && defined(EXT2_NODUMP_FL))
2657	const char *path = "cannodumptest";
2658	int fd, r, flags;
2659
2660	assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
2661	fd = open(path, O_RDONLY | O_NONBLOCK);
2662	if (fd < 0)
2663		return (0);
2664	r = ioctl(fd,
2665#ifdef FS_IOC_GETFLAGS
2666	    FS_IOC_GETFLAGS,
2667#else
2668	    EXT2_IOC_GETFLAGS,
2669#endif
2670	    &flags);
2671	if (r < 0)
2672		return (0);
2673#ifdef FS_NODUMP_FL
2674	flags |= FS_NODUMP_FL;
2675#else
2676	flags |= EXT2_NODUMP_FL;
2677#endif
2678	r = ioctl(fd,
2679#ifdef FS_IOC_SETFLAGS
2680	    FS_IOC_SETFLAGS,
2681#else
2682	    EXT2_IOC_SETFLAGS,
2683#endif
2684	   &flags);
2685	if (r < 0)
2686		return (0);
2687	close(fd);
2688	fd = open(path, O_RDONLY | O_NONBLOCK);
2689	if (fd < 0)
2690		return (0);
2691	r = ioctl(fd,
2692#ifdef FS_IOC_GETFLAGS
2693	    FS_IOC_GETFLAGS,
2694#else
2695	    EXT2_IOC_GETFLAGS,
2696#endif
2697	    &flags);
2698	if (r < 0)
2699		return (0);
2700	close(fd);
2701#ifdef FS_NODUMP_FL
2702	if (flags & FS_NODUMP_FL)
2703#else
2704	if (flags & EXT2_NODUMP_FL)
2705#endif
2706		return (1);
2707#endif
2708	return (0);
2709}
2710
2711/* Get extended attribute value from a path */
2712void *
2713getXattr(const char *path, const char *name, size_t *sizep)
2714{
2715	void *value = NULL;
2716#if ARCHIVE_XATTR_SUPPORT
2717	ssize_t size;
2718#if ARCHIVE_XATTR_LINUX
2719	size = lgetxattr(path, name, NULL, 0);
2720#elif ARCHIVE_XATTR_DARWIN
2721	size = getxattr(path, name, NULL, 0, 0, XATTR_NOFOLLOW);
2722#elif ARCHIVE_XATTR_AIX
2723	size = lgetea(path, name, NULL, 0);
2724#elif ARCHIVE_XATTR_FREEBSD
2725	size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
2726	    NULL, 0);
2727#endif
2728
2729	if (size >= 0) {
2730		value = malloc(size);
2731#if ARCHIVE_XATTR_LINUX
2732		size = lgetxattr(path, name, value, size);
2733#elif ARCHIVE_XATTR_DARWIN
2734		size = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
2735#elif ARCHIVE_XATTR_AIX
2736		size = lgetea(path, name, value, size);
2737#elif ARCHIVE_XATTR_FREEBSD
2738		size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
2739		    value, size);
2740#endif
2741		if (size < 0) {
2742			free(value);
2743			value = NULL;
2744		}
2745	}
2746	if (size < 0)
2747		*sizep = 0;
2748	else
2749		*sizep = (size_t)size;
2750#else	/* !ARCHIVE_XATTR_SUPPORT */
2751	(void)path;	/* UNUSED */
2752	(void)name;	/* UNUSED */
2753	*sizep = 0;
2754#endif 	/* !ARCHIVE_XATTR_SUPPORT */
2755	return (value);
2756}
2757
2758/*
2759 * Set extended attribute on a path
2760 * Returns 0 on error, 1 on success
2761 */
2762int
2763setXattr(const char *path, const char *name, const void *value, size_t size)
2764{
2765#if ARCHIVE_XATTR_SUPPORT
2766#if ARCHIVE_XATTR_LINUX
2767	if (lsetxattr(path, name, value, size, 0) == 0)
2768#elif ARCHIVE_XATTR_DARWIN
2769	if (setxattr(path, name, value, size, 0, XATTR_NOFOLLOW) == 0)
2770#elif ARCHIVE_XATTR_AIX
2771	if (lsetea(path, name, value, size, 0) == 0)
2772#elif ARCHIVE_XATTR_FREEBSD
2773	if (extattr_set_link(path, EXTATTR_NAMESPACE_USER, name + 5, value,
2774	    size) > -1)
2775#else
2776	if (0)
2777#endif
2778		return (1);
2779#else	/* !ARCHIVE_XATTR_SUPPORT */
2780	(void)path;     /* UNUSED */
2781	(void)name;	/* UNUSED */
2782	(void)value;	/* UNUSED */
2783	(void)size;	/* UNUSED */
2784#endif	/* !ARCHIVE_XATTR_SUPPORT */
2785	return (0);
2786}
2787
2788#if ARCHIVE_ACL_SUNOS
2789/* Fetch ACLs on Solaris using acl() or facl() */
2790void *
2791sunacl_get(int cmd, int *aclcnt, int fd, const char *path)
2792{
2793	int cnt, cntcmd;
2794	size_t size;
2795	void *aclp;
2796
2797	if (cmd == GETACL) {
2798		cntcmd = GETACLCNT;
2799		size = sizeof(aclent_t);
2800	}
2801#if ARCHIVE_ACL_SUNOS_NFS4
2802	else if (cmd == ACE_GETACL) {
2803		cntcmd = ACE_GETACLCNT;
2804		size = sizeof(ace_t);
2805	}
2806#endif
2807	else {
2808		errno = EINVAL;
2809		*aclcnt = -1;
2810		return (NULL);
2811	}
2812
2813	aclp = NULL;
2814	cnt = -2;
2815	while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) {
2816		if (path != NULL)
2817			cnt = acl(path, cntcmd, 0, NULL);
2818		else
2819			cnt = facl(fd, cntcmd, 0, NULL);
2820
2821		if (cnt > 0) {
2822			if (aclp == NULL)
2823				aclp = malloc(cnt * size);
2824			else
2825				aclp = realloc(NULL, cnt * size);
2826			if (aclp != NULL) {
2827				if (path != NULL)
2828					cnt = acl(path, cmd, cnt, aclp);
2829				else
2830					cnt = facl(fd, cmd, cnt, aclp);
2831			}
2832		} else {
2833			free(aclp);
2834			aclp = NULL;
2835			break;
2836		}
2837	}
2838
2839	*aclcnt = cnt;
2840	return (aclp);
2841}
2842#endif /* ARCHIVE_ACL_SUNOS */
2843
2844/*
2845 * Set test ACLs on a path
2846 * Return values:
2847 * 0: error setting ACLs
2848 * ARCHIVE_TEST_ACL_TYPE_POSIX1E: POSIX.1E ACLs have been set
2849 * ARCHIVE_TEST_ACL_TYPE_NFS4: NFSv4 or extended ACLs have been set
2850 */
2851int
2852setTestAcl(const char *path)
2853{
2854#if ARCHIVE_ACL_SUPPORT
2855	int r = 1;
2856#if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_DARWIN
2857	acl_t acl;
2858#endif
2859#if ARCHIVE_ACL_LIBRICHACL
2860	struct richacl *richacl;
2861#endif
2862#if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD
2863	const char *acltext_posix1e = "user:1:rw-,"
2864	    "group:15:r-x,"
2865	    "user::rwx,"
2866	    "group::rwx,"
2867	    "other::r-x,"
2868	    "mask::rwx";
2869#elif ARCHIVE_ACL_SUNOS /* Solaris POSIX.1e */
2870	aclent_t aclp_posix1e[] = {
2871	    { USER_OBJ, -1, 4 | 2 | 1 },
2872	    { USER, 1, 4 | 2 },
2873	    { GROUP_OBJ, -1, 4 | 2 | 1 },
2874	    { GROUP, 15, 4 | 1 },
2875	    { CLASS_OBJ, -1, 4 | 2 | 1 },
2876	    { OTHER_OBJ, -1, 4 | 2 | 1 }
2877	};
2878#endif
2879#if ARCHIVE_ACL_FREEBSD /* FreeBSD NFS4 */
2880	const char *acltext_nfs4 = "user:1:rwpaRcs::allow:1,"
2881	    "group:15:rxaRcs::allow:15,"
2882	    "owner@:rwpxaARWcCos::allow,"
2883	    "group@:rwpxaRcs::allow,"
2884	    "everyone@:rxaRcs::allow";
2885#elif ARCHIVE_ACL_LIBRICHACL
2886	const char *acltext_nfs4 = "owner:rwpxaARWcCoS::mask,"
2887	    "group:rwpxaRcS::mask,"
2888	    "other:rxaRcS::mask,"
2889	    "user:1:rwpaRcS::allow,"
2890	    "group:15:rxaRcS::allow,"
2891	    "owner@:rwpxaARWcCoS::allow,"
2892	    "group@:rwpxaRcS::allow,"
2893	    "everyone@:rxaRcS::allow";
2894#elif ARCHIVE_ACL_SUNOS_NFS4 /* Solaris NFS4 */
2895	ace_t aclp_nfs4[] = {
2896	    { 1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2897	      ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | ACE_READ_ACL |
2898	      ACE_SYNCHRONIZE, 0, ACE_ACCESS_ALLOWED_ACE_TYPE },
2899	    { 15, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2900	      ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2901	      ACE_IDENTIFIER_GROUP, ACE_ACCESS_ALLOWED_ACE_TYPE },
2902	    { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2903	      ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_WRITE_ATTRIBUTES |
2904	      ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS |
2905	      ACE_READ_ACL | ACE_WRITE_ACL | ACE_WRITE_OWNER | ACE_SYNCHRONIZE,
2906	      ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE },
2907	    { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
2908	      ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
2909	      ACE_READ_ACL | ACE_SYNCHRONIZE, ACE_GROUP | ACE_IDENTIFIER_GROUP,
2910	      ACE_ACCESS_ALLOWED_ACE_TYPE },
2911	    { -1, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
2912	      ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
2913	      ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE }
2914	};
2915#elif ARCHIVE_ACL_DARWIN /* Mac OS X */
2916	acl_entry_t aclent;
2917	acl_permset_t permset;
2918	const uid_t uid = 1;
2919	uuid_t uuid;
2920	int i;
2921	const acl_perm_t acl_perms[] = {
2922		ACL_READ_DATA,
2923		ACL_WRITE_DATA,
2924		ACL_APPEND_DATA,
2925		ACL_EXECUTE,
2926		ACL_READ_ATTRIBUTES,
2927		ACL_READ_EXTATTRIBUTES,
2928		ACL_READ_SECURITY,
2929#if HAVE_DECL_ACL_SYNCHRONIZE
2930		ACL_SYNCHRONIZE
2931#endif
2932	};
2933#endif /* ARCHIVE_ACL_DARWIN */
2934
2935#if ARCHIVE_ACL_FREEBSD
2936	acl = acl_from_text(acltext_nfs4);
2937	failure("acl_from_text() error: %s", strerror(errno));
2938	if (assert(acl != NULL) == 0)
2939		return (0);
2940#elif ARCHIVE_ACL_LIBRICHACL
2941	richacl = richacl_from_text(acltext_nfs4, NULL, NULL);
2942	failure("richacl_from_text() error: %s", strerror(errno));
2943	if (assert(richacl != NULL) == 0)
2944		return (0);
2945#elif ARCHIVE_ACL_DARWIN
2946	acl = acl_init(1);
2947	failure("acl_init() error: %s", strerror(errno));
2948	if (assert(acl != NULL) == 0)
2949		return (0);
2950	r = acl_create_entry(&acl, &aclent);
2951	failure("acl_create_entry() error: %s", strerror(errno));
2952	if (assertEqualInt(r, 0) == 0)
2953		goto testacl_free;
2954	r = acl_set_tag_type(aclent, ACL_EXTENDED_ALLOW);
2955	failure("acl_set_tag_type() error: %s", strerror(errno));
2956	if (assertEqualInt(r, 0) == 0)
2957		goto testacl_free;
2958	r = acl_get_permset(aclent, &permset);
2959	failure("acl_get_permset() error: %s", strerror(errno));
2960	if (assertEqualInt(r, 0) == 0)
2961		goto testacl_free;
2962	for (i = 0; i < (int)(sizeof(acl_perms) / sizeof(acl_perms[0])); i++) {
2963		r = acl_add_perm(permset, acl_perms[i]);
2964		failure("acl_add_perm() error: %s", strerror(errno));
2965		if (assertEqualInt(r, 0) == 0)
2966			goto testacl_free;
2967	}
2968	r = acl_set_permset(aclent, permset);
2969	failure("acl_set_permset() error: %s", strerror(errno));
2970	if (assertEqualInt(r, 0) == 0)
2971		goto testacl_free;
2972	r = mbr_uid_to_uuid(uid, uuid);
2973	failure("mbr_uid_to_uuid() error: %s", strerror(errno));
2974	if (assertEqualInt(r, 0) == 0)
2975		goto testacl_free;
2976	r = acl_set_qualifier(aclent, uuid);
2977	failure("acl_set_qualifier() error: %s", strerror(errno));
2978	if (assertEqualInt(r, 0) == 0)
2979		goto testacl_free;
2980#endif /* ARCHIVE_ACL_DARWIN */
2981
2982#if ARCHIVE_ACL_NFS4
2983#if ARCHIVE_ACL_FREEBSD
2984	r = acl_set_file(path, ACL_TYPE_NFS4, acl);
2985	acl_free(acl);
2986#elif ARCHIVE_ACL_LIBRICHACL
2987	r = richacl_set_file(path, richacl);
2988	richacl_free(richacl);
2989#elif ARCHIVE_ACL_SUNOS_NFS4
2990	r = acl(path, ACE_SETACL,
2991	    (int)(sizeof(aclp_nfs4)/sizeof(aclp_nfs4[0])), aclp_nfs4);
2992#elif ARCHIVE_ACL_DARWIN
2993	r = acl_set_file(path, ACL_TYPE_EXTENDED, acl);
2994	acl_free(acl);
2995#endif
2996	if (r == 0)
2997		return (ARCHIVE_TEST_ACL_TYPE_NFS4);
2998#endif	/* ARCHIVE_ACL_NFS4 */
2999
3000#if ARCHIVE_ACL_POSIX1E
3001#if ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_LIBACL
3002	acl = acl_from_text(acltext_posix1e);
3003	failure("acl_from_text() error: %s", strerror(errno));
3004	if (assert(acl != NULL) == 0)
3005		return (0);
3006
3007	r = acl_set_file(path, ACL_TYPE_ACCESS, acl);
3008	acl_free(acl);
3009#elif ARCHIVE_ACL_SUNOS
3010	r = acl(path, SETACL,
3011	    (int)(sizeof(aclp_posix1e)/sizeof(aclp_posix1e[0])), aclp_posix1e);
3012#endif
3013	if (r == 0)
3014		return (ARCHIVE_TEST_ACL_TYPE_POSIX1E);
3015	else
3016		return (0);
3017#endif /* ARCHIVE_ACL_POSIX1E */
3018#if ARCHIVE_ACL_DARWIN
3019testacl_free:
3020	acl_free(acl);
3021#endif
3022#endif /* ARCHIVE_ACL_SUPPORT */
3023	(void)path;	/* UNUSED */
3024	return (0);
3025}
3026
3027/*
3028 * Sleep as needed; useful for verifying disk timestamp changes by
3029 * ensuring that the wall-clock time has actually changed before we
3030 * go back to re-read something from disk.
3031 */
3032void
3033sleepUntilAfter(time_t t)
3034{
3035	while (t >= time(NULL))
3036#if defined(_WIN32) && !defined(__CYGWIN__)
3037		Sleep(500);
3038#else
3039		sleep(1);
3040#endif
3041}
3042
3043/*
3044 * Call standard system() call, but build up the command line using
3045 * sprintf() conventions.
3046 */
3047int
3048systemf(const char *fmt, ...)
3049{
3050	char buff[8192];
3051	va_list ap;
3052	int r;
3053
3054	va_start(ap, fmt);
3055	vsprintf(buff, fmt, ap);
3056	if (verbosity > VERBOSITY_FULL)
3057		logprintf("Cmd: %s\n", buff);
3058	r = system(buff);
3059	va_end(ap);
3060	return (r);
3061}
3062
3063/*
3064 * Slurp a file into memory for ease of comparison and testing.
3065 * Returns size of file in 'sizep' if non-NULL, null-terminates
3066 * data in memory for ease of use.
3067 */
3068char *
3069slurpfile(size_t * sizep, const char *fmt, ...)
3070{
3071	char filename[8192];
3072	struct stat st;
3073	va_list ap;
3074	char *p;
3075	ssize_t bytes_read;
3076	FILE *f;
3077	int r;
3078
3079	va_start(ap, fmt);
3080	vsprintf(filename, fmt, ap);
3081	va_end(ap);
3082
3083	f = fopen(filename, "rb");
3084	if (f == NULL) {
3085		/* Note: No error; non-existent file is okay here. */
3086		return (NULL);
3087	}
3088	r = fstat(fileno(f), &st);
3089	if (r != 0) {
3090		logprintf("Can't stat file %s\n", filename);
3091		fclose(f);
3092		return (NULL);
3093	}
3094	p = malloc((size_t)st.st_size + 1);
3095	if (p == NULL) {
3096		logprintf("Can't allocate %ld bytes of memory to read file %s\n",
3097		    (long int)st.st_size, filename);
3098		fclose(f);
3099		return (NULL);
3100	}
3101	bytes_read = fread(p, 1, (size_t)st.st_size, f);
3102	if (bytes_read < st.st_size) {
3103		logprintf("Can't read file %s\n", filename);
3104		fclose(f);
3105		free(p);
3106		return (NULL);
3107	}
3108	p[st.st_size] = '\0';
3109	if (sizep != NULL)
3110		*sizep = (size_t)st.st_size;
3111	fclose(f);
3112	return (p);
3113}
3114
3115/*
3116 * Slurp a file into memory for ease of comparison and testing.
3117 * Returns size of file in 'sizep' if non-NULL, null-terminates
3118 * data in memory for ease of use.
3119 */
3120void
3121dumpfile(const char *filename, void *data, size_t len)
3122{
3123	ssize_t bytes_written;
3124	FILE *f;
3125
3126	f = fopen(filename, "wb");
3127	if (f == NULL) {
3128		logprintf("Can't open file %s for writing\n", filename);
3129		return;
3130	}
3131	bytes_written = fwrite(data, 1, len, f);
3132	if (bytes_written < (ssize_t)len)
3133		logprintf("Can't write file %s\n", filename);
3134	fclose(f);
3135}
3136
3137/* Read a uuencoded file from the reference directory, decode, and
3138 * write the result into the current directory. */
3139#define VALID_UUDECODE(c) (c >= 32 && c <= 96)
3140#define	UUDECODE(c) (((c) - 0x20) & 0x3f)
3141void
3142extract_reference_file(const char *name)
3143{
3144	char buff[1024];
3145	FILE *in, *out;
3146
3147	sprintf(buff, "%s/%s.uu", refdir, name);
3148	in = fopen(buff, "r");
3149	failure("Couldn't open reference file %s", buff);
3150	assert(in != NULL);
3151	if (in == NULL)
3152		return;
3153	/* Read up to and including the 'begin' line. */
3154	for (;;) {
3155		if (fgets(buff, sizeof(buff), in) == NULL) {
3156			/* TODO: This is a failure. */
3157			return;
3158		}
3159		if (memcmp(buff, "begin ", 6) == 0)
3160			break;
3161	}
3162	/* Now, decode the rest and write it. */
3163	out = fopen(name, "wb");
3164	while (fgets(buff, sizeof(buff), in) != NULL) {
3165		char *p = buff;
3166		int bytes;
3167
3168		if (memcmp(buff, "end", 3) == 0)
3169			break;
3170
3171		bytes = UUDECODE(*p++);
3172		while (bytes > 0) {
3173			int n = 0;
3174			/* Write out 1-3 bytes from that. */
3175			if (bytes > 0) {
3176				assert(VALID_UUDECODE(p[0]));
3177				assert(VALID_UUDECODE(p[1]));
3178				n = UUDECODE(*p++) << 18;
3179				n |= UUDECODE(*p++) << 12;
3180				fputc(n >> 16, out);
3181				--bytes;
3182			}
3183			if (bytes > 0) {
3184				assert(VALID_UUDECODE(p[0]));
3185				n |= UUDECODE(*p++) << 6;
3186				fputc((n >> 8) & 0xFF, out);
3187				--bytes;
3188			}
3189			if (bytes > 0) {
3190				assert(VALID_UUDECODE(p[0]));
3191				n |= UUDECODE(*p++);
3192				fputc(n & 0xFF, out);
3193				--bytes;
3194			}
3195		}
3196	}
3197	fclose(out);
3198	fclose(in);
3199}
3200
3201void
3202copy_reference_file(const char *name)
3203{
3204	char buff[1024];
3205	FILE *in, *out;
3206	size_t rbytes;
3207
3208	sprintf(buff, "%s/%s", refdir, name);
3209	in = fopen(buff, "rb");
3210	failure("Couldn't open reference file %s", buff);
3211	assert(in != NULL);
3212	if (in == NULL)
3213		return;
3214	/* Now, decode the rest and write it. */
3215	/* Not a lot of error checking here; the input better be right. */
3216	out = fopen(name, "wb");
3217	while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
3218		if (fwrite(buff, 1, rbytes, out) != rbytes) {
3219			logprintf("Error: fwrite\n");
3220			break;
3221		}
3222	}
3223	fclose(out);
3224	fclose(in);
3225}
3226
3227int
3228is_LargeInode(const char *file)
3229{
3230#if defined(_WIN32) && !defined(__CYGWIN__)
3231	BY_HANDLE_FILE_INFORMATION bhfi;
3232	int r;
3233
3234	r = my_GetFileInformationByName(file, &bhfi);
3235	if (r != 0)
3236		return (0);
3237	return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
3238#else
3239	struct stat st;
3240	int64_t ino;
3241
3242	if (stat(file, &st) < 0)
3243		return (0);
3244	ino = (int64_t)st.st_ino;
3245	return (ino > 0xffffffff);
3246#endif
3247}
3248
3249void
3250extract_reference_files(const char **names)
3251{
3252	while (names && *names)
3253		extract_reference_file(*names++);
3254}
3255
3256#ifndef PROGRAM
3257/* Set ACLs */
3258int
3259assertion_entry_set_acls(const char *file, int line, struct archive_entry *ae,
3260    struct archive_test_acl_t *acls, int n)
3261{
3262	int i, r, ret;
3263
3264	assertion_count(file, line);
3265
3266	ret = 0;
3267	archive_entry_acl_clear(ae);
3268	for (i = 0; i < n; i++) {
3269		r = archive_entry_acl_add_entry(ae,
3270		    acls[i].type, acls[i].permset, acls[i].tag,
3271		    acls[i].qual, acls[i].name);
3272		if (r != 0) {
3273			ret = 1;
3274			failure_start(file, line, "type=%#010x, "
3275			    "permset=%#010x, tag=%d, qual=%d name=%s",
3276			    acls[i].type, acls[i].permset, acls[i].tag,
3277			    acls[i].qual, acls[i].name);
3278			failure_finish(NULL);
3279		}
3280	}
3281
3282	return (ret);
3283}
3284
3285static int
3286archive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset,
3287    int tag, int qual, const char *name)
3288{
3289	if (type != acl->type)
3290		return (0);
3291	if (permset != acl->permset)
3292		return (0);
3293	if (tag != acl->tag)
3294		return (0);
3295	if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
3296		return (1);
3297	if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
3298		return (1);
3299	if (tag == ARCHIVE_ENTRY_ACL_EVERYONE)
3300		return (1);
3301	if (tag == ARCHIVE_ENTRY_ACL_OTHER)
3302		return (1);
3303	if (qual != acl->qual)
3304		return (0);
3305	if (name == NULL) {
3306		if (acl->name == NULL || acl->name[0] == '\0')
3307			return (1);
3308		return (0);
3309	}
3310	if (acl->name == NULL) {
3311		if (name[0] == '\0')
3312			return (1);
3313		return (0);
3314	}
3315	return (0 == strcmp(name, acl->name));
3316}
3317
3318/* Compare ACLs */
3319int
3320assertion_entry_compare_acls(const char *file, int line,
3321    struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt,
3322    int want_type, int mode)
3323{
3324	int *marker;
3325	int i, r, n, ret;
3326	int type, permset, tag, qual;
3327	int matched;
3328	const char *name;
3329
3330	assertion_count(file, line);
3331
3332	ret = 0;
3333	n = 0;
3334	marker = malloc(sizeof(marker[0]) * cnt);
3335
3336	for (i = 0; i < cnt; i++) {
3337		if ((acls[i].type & want_type) != 0) {
3338			marker[n] = i;
3339			n++;
3340		}
3341	}
3342
3343	if (n == 0) {
3344		failure_start(file, line, "No ACL's to compare, type mask: %d",
3345		    want_type);
3346		return (1);
3347	}
3348
3349	while (0 == (r = archive_entry_acl_next(ae, want_type,
3350			 &type, &permset, &tag, &qual, &name))) {
3351		for (i = 0, matched = 0; i < n && !matched; i++) {
3352			if (archive_test_acl_match(&acls[marker[i]], type,
3353			    permset, tag, qual, name)) {
3354				/* We found a match; remove it. */
3355				marker[i] = marker[n - 1];
3356				n--;
3357				matched = 1;
3358			}
3359		}
3360		if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3361		    && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
3362			if (!matched) {
3363				failure_start(file, line, "No match for "
3364				    "user_obj perm");
3365				failure_finish(NULL);
3366				ret = 1;
3367			}
3368			if ((permset << 6) != (mode & 0700)) {
3369				failure_start(file, line, "USER_OBJ permset "
3370				    "(%02o) != user mode (%02o)", permset,
3371				    07 & (mode >> 6));
3372				failure_finish(NULL);
3373				ret = 1;
3374			}
3375		} else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3376		    && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
3377			if (!matched) {
3378				failure_start(file, line, "No match for "
3379				    "group_obj perm");
3380				failure_finish(NULL);
3381				ret = 1;
3382			}
3383			if ((permset << 3) != (mode & 0070)) {
3384				failure_start(file, line, "GROUP_OBJ permset "
3385				    "(%02o) != group mode (%02o)", permset,
3386				    07 & (mode >> 3));
3387				failure_finish(NULL);
3388				ret = 1;
3389			}
3390		} else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
3391		    && tag == ARCHIVE_ENTRY_ACL_OTHER) {
3392			if (!matched) {
3393				failure_start(file, line, "No match for "
3394				    "other perm");
3395				failure_finish(NULL);
3396				ret = 1;
3397			}
3398			if ((permset << 0) != (mode & 0007)) {
3399				failure_start(file, line, "OTHER permset "
3400				    "(%02o) != other mode (%02o)", permset,
3401				    mode & 07);
3402				failure_finish(NULL);
3403				ret = 1;
3404			}
3405		} else if (matched != 1) {
3406			failure_start(file, line, "Could not find match for "
3407			    "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d,"
3408			    "name=``%s'')", type, permset, tag, qual, name);
3409			failure_finish(NULL);
3410			ret = 1;
3411		}
3412	}
3413	if (r != ARCHIVE_EOF) {
3414		failure_start(file, line, "Should not exit before EOF");
3415		failure_finish(NULL);
3416		ret = 1;
3417	}
3418	if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 &&
3419	    (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) {
3420		failure_start(file, line, "Mode (%02o) and entry mode (%02o) "
3421		    "mismatch", mode, archive_entry_mode(ae));
3422		failure_finish(NULL);
3423		ret = 1;
3424	}
3425	if (n != 0) {
3426		failure_start(file, line, "Could not find match for ACL "
3427		    "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')",
3428		    acls[marker[0]].type, acls[marker[0]].permset,
3429		    acls[marker[0]].tag, acls[marker[0]].qual,
3430		    acls[marker[0]].name);
3431		failure_finish(NULL);
3432		ret = 1;
3433		/* Number of ACLs not matched should == 0 */
3434	}
3435	free(marker);
3436	return (ret);
3437}
3438#endif	/* !defined(PROGRAM) */
3439
3440/*
3441 *
3442 * TEST management
3443 *
3444 */
3445
3446/*
3447 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
3448 * a line like
3449 *      DEFINE_TEST(test_function)
3450 * for each test.
3451 */
3452
3453/* Use "list.h" to declare all of the test functions. */
3454#undef DEFINE_TEST
3455#define	DEFINE_TEST(name) void name(void);
3456#include "list.h"
3457
3458/* Use "list.h" to create a list of all tests (functions and names). */
3459#undef DEFINE_TEST
3460#define	DEFINE_TEST(n) { n, #n, 0 },
3461struct test_list_t tests[] = {
3462	#include "list.h"
3463};
3464
3465/*
3466 * Summarize repeated failures in the just-completed test.
3467 */
3468static void
3469test_summarize(int failed, int skips_num)
3470{
3471	unsigned int i;
3472
3473	switch (verbosity) {
3474	case VERBOSITY_SUMMARY_ONLY:
3475		printf(failed ? "E" : ".");
3476		fflush(stdout);
3477		break;
3478	case VERBOSITY_PASSFAIL:
3479		printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
3480		break;
3481	}
3482
3483	log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
3484
3485	for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
3486		if (failed_lines[i].count > 1 && !failed_lines[i].skip)
3487			logprintf("%s:%d: Summary: Failed %d times\n",
3488			    failed_filename, i, failed_lines[i].count);
3489	}
3490	/* Clear the failure history for the next file. */
3491	failed_filename = NULL;
3492	memset(failed_lines, 0, sizeof(failed_lines));
3493}
3494
3495/*
3496 * Actually run a single test, with appropriate setup and cleanup.
3497 */
3498static int
3499test_run(int i, const char *tmpdir)
3500{
3501#ifdef PATH_MAX
3502	char workdir[PATH_MAX * 2];
3503#else
3504	char workdir[1024 * 2];
3505#endif
3506	char logfilename[64];
3507	int failures_before = failures;
3508	int skips_before = skips;
3509	int oldumask;
3510
3511	switch (verbosity) {
3512	case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
3513		break;
3514	case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
3515		printf("%3d: %-64s", i, tests[i].name);
3516		fflush(stdout);
3517		break;
3518	default: /* Title of test, details will follow */
3519		printf("%3d: %s\n", i, tests[i].name);
3520	}
3521
3522	/* Chdir to the top-level work directory. */
3523	if (!assertChdir(tmpdir)) {
3524		fprintf(stderr,
3525		    "ERROR: Can't chdir to top work dir %s\n", tmpdir);
3526		exit(1);
3527	}
3528	/* Create a log file for this test. */
3529	sprintf(logfilename, "%s.log", tests[i].name);
3530	logfile = fopen(logfilename, "w");
3531	fprintf(logfile, "%s\n\n", tests[i].name);
3532	/* Chdir() to a work dir for this specific test. */
3533	snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
3534	testworkdir = workdir;
3535	if (!assertMakeDir(testworkdir, 0755)
3536	    || !assertChdir(testworkdir)) {
3537		fprintf(stderr,
3538		    "ERROR: Can't chdir to work dir %s\n", testworkdir);
3539		exit(1);
3540	}
3541	/* Explicitly reset the locale before each test. */
3542	setlocale(LC_ALL, "C");
3543	/* Record the umask before we run the test. */
3544	umask(oldumask = umask(0));
3545	/*
3546	 * Run the actual test.
3547	 */
3548	(*tests[i].func)();
3549	/*
3550	 * Clean up and report afterwards.
3551	 */
3552	testworkdir = NULL;
3553	/* Restore umask */
3554	umask(oldumask);
3555	/* Reset locale. */
3556	setlocale(LC_ALL, "C");
3557	/* Reset directory. */
3558	if (!assertChdir(tmpdir)) {
3559		fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
3560		    tmpdir);
3561		exit(1);
3562	}
3563	/* Report per-test summaries. */
3564	tests[i].failures = failures - failures_before;
3565	test_summarize(tests[i].failures, skips - skips_before);
3566	/* Close the per-test log file. */
3567	fclose(logfile);
3568	logfile = NULL;
3569	/* If there were no failures, we can remove the work dir and logfile. */
3570	if (tests[i].failures == 0) {
3571		if (!keep_temp_files && assertChdir(tmpdir)) {
3572#if defined(_WIN32) && !defined(__CYGWIN__)
3573			/* Make sure not to leave empty directories.
3574			 * Sometimes a processing of closing files used by tests
3575			 * is not done, then rmdir will be failed and it will
3576			 * leave a empty test directory. So we should wait a few
3577			 * seconds and retry rmdir. */
3578			int r, t;
3579			for (t = 0; t < 10; t++) {
3580				if (t > 0)
3581					Sleep(1000);
3582				r = systemf("rmdir /S /Q %s", tests[i].name);
3583				if (r == 0)
3584					break;
3585			}
3586			systemf("del %s", logfilename);
3587#else
3588			systemf("rm -rf %s", tests[i].name);
3589			systemf("rm %s", logfilename);
3590#endif
3591		}
3592	}
3593	/* Return appropriate status. */
3594	return (tests[i].failures);
3595}
3596
3597/*
3598 *
3599 *
3600 * MAIN and support routines.
3601 *
3602 *
3603 */
3604
3605static void
3606usage(const char *program)
3607{
3608	static const int limit = sizeof(tests) / sizeof(tests[0]);
3609	int i;
3610
3611	printf("Usage: %s [options] <test> <test> ...\n", program);
3612	printf("Default is to run all tests.\n");
3613	printf("Otherwise, specify the numbers of the tests you wish to run.\n");
3614	printf("Options:\n");
3615	printf("  -d  Dump core after any failure, for debugging.\n");
3616	printf("  -k  Keep all temp files.\n");
3617	printf("      Default: temp files for successful tests deleted.\n");
3618#ifdef PROGRAM
3619	printf("  -p <path>  Path to executable to be tested.\n");
3620	printf("      Default: path taken from " ENVBASE " environment variable.\n");
3621#endif
3622	printf("  -q  Quiet.\n");
3623	printf("  -r <dir>   Path to dir containing reference files.\n");
3624	printf("      Default: Current directory.\n");
3625	printf("  -u  Keep running specifies tests until one fails.\n");
3626	printf("  -v  Verbose.\n");
3627	printf("Available tests:\n");
3628	for (i = 0; i < limit; i++)
3629		printf("  %d: %s\n", i, tests[i].name);
3630	exit(1);
3631}
3632
3633static char *
3634get_refdir(const char *d)
3635{
3636	size_t tried_size, buff_size;
3637	char *buff, *tried, *pwd = NULL, *p = NULL;
3638
3639#ifdef PATH_MAX
3640	buff_size = PATH_MAX;
3641#else
3642	buff_size = 8192;
3643#endif
3644	buff = calloc(buff_size, 1);
3645	if (buff == NULL) {
3646		fprintf(stderr, "Unable to allocate memory\n");
3647		exit(1);
3648	}
3649
3650	/* Allocate a buffer to hold the various directories we checked. */
3651	tried_size = buff_size * 2;
3652	tried = calloc(tried_size, 1);
3653	if (tried == NULL) {
3654		fprintf(stderr, "Unable to allocate memory\n");
3655		exit(1);
3656	}
3657
3658	/* If a dir was specified, try that */
3659	if (d != NULL) {
3660		pwd = NULL;
3661		snprintf(buff, buff_size, "%s", d);
3662		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3663		if (p != NULL) goto success;
3664		strncat(tried, buff, tried_size - strlen(tried) - 1);
3665		strncat(tried, "\n", tried_size - strlen(tried) - 1);
3666		goto failure;
3667	}
3668
3669	/* Get the current dir. */
3670#ifdef PATH_MAX
3671	pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3672#else
3673	pwd = getcwd(NULL, 0);
3674#endif
3675	while (pwd[strlen(pwd) - 1] == '\n')
3676		pwd[strlen(pwd) - 1] = '\0';
3677
3678	/* Look for a known file. */
3679	snprintf(buff, buff_size, "%s", pwd);
3680	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3681	if (p != NULL) goto success;
3682	strncat(tried, buff, tried_size - strlen(tried) - 1);
3683	strncat(tried, "\n", tried_size - strlen(tried) - 1);
3684
3685	snprintf(buff, buff_size, "%s/test", pwd);
3686	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3687	if (p != NULL) goto success;
3688	strncat(tried, buff, tried_size - strlen(tried) - 1);
3689	strncat(tried, "\n", tried_size - strlen(tried) - 1);
3690
3691#if defined(LIBRARY)
3692	snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
3693#else
3694	snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
3695#endif
3696	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3697	if (p != NULL) goto success;
3698	strncat(tried, buff, tried_size - strlen(tried) - 1);
3699	strncat(tried, "\n", tried_size - strlen(tried) - 1);
3700
3701#if defined(PROGRAM_ALIAS)
3702	snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
3703	p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3704	if (p != NULL) goto success;
3705	strncat(tried, buff, tried_size - strlen(tried) - 1);
3706	strncat(tried, "\n", tried_size - strlen(tried) - 1);
3707#endif
3708
3709	if (memcmp(pwd, "/usr/obj", 8) == 0) {
3710		snprintf(buff, buff_size, "%s", pwd + 8);
3711		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3712		if (p != NULL) goto success;
3713		strncat(tried, buff, tried_size - strlen(tried) - 1);
3714		strncat(tried, "\n", tried_size - strlen(tried) - 1);
3715
3716		snprintf(buff, buff_size, "%s/test", pwd + 8);
3717		p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
3718		if (p != NULL) goto success;
3719		strncat(tried, buff, tried_size - strlen(tried) - 1);
3720		strncat(tried, "\n", tried_size - strlen(tried) - 1);
3721	}
3722
3723failure:
3724	printf("Unable to locate known reference file %s\n", KNOWNREF);
3725	printf("  Checked following directories:\n%s\n", tried);
3726	printf("Use -r option to specify full path to reference directory\n");
3727#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
3728	DebugBreak();
3729#endif
3730	exit(1);
3731
3732success:
3733	free(p);
3734	free(pwd);
3735	free(tried);
3736
3737	/* Copy result into a fresh buffer to reduce memory usage. */
3738	p = strdup(buff);
3739	free(buff);
3740	return p;
3741}
3742
3743int
3744main(int argc, char **argv)
3745{
3746	static const int limit = sizeof(tests) / sizeof(tests[0]);
3747	int test_set[sizeof(tests) / sizeof(tests[0])];
3748	int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
3749	time_t now;
3750	char *refdir_alloc = NULL;
3751	const char *progname;
3752	char **saved_argv;
3753	const char *tmp, *option_arg, *p;
3754#ifdef PATH_MAX
3755	char tmpdir[PATH_MAX];
3756#else
3757	char tmpdir[256];
3758#endif
3759	char *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
3760	char tmpdir_timestamp[32];
3761
3762	(void)argc; /* UNUSED */
3763
3764	/* Get the current dir. */
3765#ifdef PATH_MAX
3766	pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
3767#else
3768	pwd = getcwd(NULL, 0);
3769#endif
3770	while (pwd[strlen(pwd) - 1] == '\n')
3771		pwd[strlen(pwd) - 1] = '\0';
3772
3773#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
3774	/* To stop to run the default invalid parameter handler. */
3775	_set_invalid_parameter_handler(invalid_parameter_handler);
3776	/* Disable annoying assertion message box. */
3777	_CrtSetReportMode(_CRT_ASSERT, 0);
3778#endif
3779
3780	/*
3781	 * Name of this program, used to build root of our temp directory
3782	 * tree.
3783	 */
3784	progname = p = argv[0];
3785	if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL)
3786	{
3787		fprintf(stderr, "ERROR: Out of memory.");
3788		exit(1);
3789	}
3790	strcpy(testprogdir, progname);
3791	while (*p != '\0') {
3792		/* Support \ or / dir separators for Windows compat. */
3793		if (*p == '/' || *p == '\\')
3794		{
3795			progname = p + 1;
3796			i = j;
3797		}
3798		++p;
3799		j++;
3800	}
3801	testprogdir[i] = '\0';
3802#if defined(_WIN32) && !defined(__CYGWIN__)
3803	if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
3804	    !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
3805	       (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
3806		testprogdir[1] == ':' &&
3807		(testprogdir[2] == '/' || testprogdir[2] == '\\')))
3808#else
3809	if (testprogdir[0] != '/')
3810#endif
3811	{
3812		/* Fixup path for relative directories. */
3813		if ((testprogdir = (char *)realloc(testprogdir,
3814			strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
3815		{
3816			fprintf(stderr, "ERROR: Out of memory.");
3817			exit(1);
3818		}
3819		memmove(testprogdir + strlen(pwd) + 1, testprogdir,
3820		    strlen(testprogdir) + 1);
3821		memcpy(testprogdir, pwd, strlen(pwd));
3822		testprogdir[strlen(pwd)] = '/';
3823	}
3824
3825#ifdef PROGRAM
3826	/* Get the target program from environment, if available. */
3827	testprogfile = getenv(ENVBASE);
3828#endif
3829
3830	if (getenv("TMPDIR") != NULL)
3831		tmp = getenv("TMPDIR");
3832	else if (getenv("TMP") != NULL)
3833		tmp = getenv("TMP");
3834	else if (getenv("TEMP") != NULL)
3835		tmp = getenv("TEMP");
3836	else if (getenv("TEMPDIR") != NULL)
3837		tmp = getenv("TEMPDIR");
3838	else
3839		tmp = "/tmp";
3840
3841	/* Allow -d to be controlled through the environment. */
3842	if (getenv(ENVBASE "_DEBUG") != NULL)
3843		dump_on_failure = 1;
3844
3845	/* Allow -v to be controlled through the environment. */
3846	if (getenv("_VERBOSITY_LEVEL") != NULL)
3847	{
3848		vlevel = getenv("_VERBOSITY_LEVEL");
3849		verbosity = atoi(vlevel);
3850		if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
3851		{
3852			/* Unsupported verbosity levels are silently ignored */
3853			vlevel = NULL;
3854			verbosity = VERBOSITY_PASSFAIL;
3855		}
3856	}
3857
3858	/* Get the directory holding test files from environment. */
3859	refdir = getenv(ENVBASE "_TEST_FILES");
3860
3861	/*
3862	 * Parse options, without using getopt(), which isn't available
3863	 * on all platforms.
3864	 */
3865	++argv; /* Skip program name */
3866	while (*argv != NULL) {
3867		if (**argv != '-')
3868			break;
3869		p = *argv++;
3870		++p; /* Skip '-' */
3871		while (*p != '\0') {
3872			option = *p++;
3873			option_arg = NULL;
3874			/* If 'opt' takes an argument, parse that. */
3875			if (option == 'p' || option == 'r') {
3876				if (*p != '\0')
3877					option_arg = p;
3878				else if (*argv == NULL) {
3879					fprintf(stderr,
3880					    "Option -%c requires argument.\n",
3881					    option);
3882					usage(progname);
3883				} else
3884					option_arg = *argv++;
3885				p = ""; /* End of this option word. */
3886			}
3887
3888			/* Now, handle the option. */
3889			switch (option) {
3890			case 'd':
3891				dump_on_failure = 1;
3892				break;
3893			case 'k':
3894				keep_temp_files = 1;
3895				break;
3896			case 'p':
3897#ifdef PROGRAM
3898				testprogfile = option_arg;
3899#else
3900				fprintf(stderr, "-p option not permitted\n");
3901				usage(progname);
3902#endif
3903				break;
3904			case 'q':
3905				if (!vlevel)
3906					verbosity--;
3907				break;
3908			case 'r':
3909				refdir = option_arg;
3910				break;
3911			case 'u':
3912				until_failure++;
3913				break;
3914			case 'v':
3915				if (!vlevel)
3916					verbosity++;
3917				break;
3918			default:
3919				fprintf(stderr, "Unrecognized option '%c'\n",
3920				    option);
3921				usage(progname);
3922			}
3923		}
3924	}
3925
3926	/*
3927	 * Sanity-check that our options make sense.
3928	 */
3929#ifdef PROGRAM
3930	if (testprogfile == NULL)
3931	{
3932		if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 +
3933			strlen(PROGRAM) + 1)) == NULL)
3934		{
3935			fprintf(stderr, "ERROR: Out of memory.");
3936			exit(1);
3937		}
3938		strcpy(tmp2, testprogdir);
3939		strcat(tmp2, "/");
3940		strcat(tmp2, PROGRAM);
3941		testprogfile = tmp2;
3942	}
3943
3944	{
3945		char *testprg;
3946#if defined(_WIN32) && !defined(__CYGWIN__)
3947		/* Command.com sometimes rejects '/' separators. */
3948		testprg = strdup(testprogfile);
3949		for (i = 0; testprg[i] != '\0'; i++) {
3950			if (testprg[i] == '/')
3951				testprg[i] = '\\';
3952		}
3953		testprogfile = testprg;
3954#endif
3955		/* Quote the name that gets put into shell command lines. */
3956		testprg = malloc(strlen(testprogfile) + 3);
3957		strcpy(testprg, "\"");
3958		strcat(testprg, testprogfile);
3959		strcat(testprg, "\"");
3960		testprog = testprg;
3961	}
3962#endif
3963
3964#if !defined(_WIN32) && defined(SIGPIPE)
3965	{   /* Ignore SIGPIPE signals */
3966		struct sigaction sa;
3967		sa.sa_handler = SIG_IGN;
3968		sigemptyset(&sa.sa_mask);
3969		sa.sa_flags = 0;
3970		sigaction(SIGPIPE, &sa, NULL);
3971	}
3972#endif
3973
3974	/*
3975	 * Create a temp directory for the following tests.
3976	 * Include the time the tests started as part of the name,
3977	 * to make it easier to track the results of multiple tests.
3978	 */
3979	now = time(NULL);
3980	for (i = 0; ; i++) {
3981		strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
3982		    "%Y-%m-%dT%H.%M.%S",
3983		    localtime(&now));
3984		if ((strlen(tmp) + 1 + strlen(progname) + 1 +
3985		    strlen(tmpdir_timestamp) + 1 + 3) >
3986		    (sizeof(tmpdir) / sizeof(char))) {
3987			fprintf(stderr,
3988			    "ERROR: Temp directory pathname too long\n");
3989			exit(1);
3990		}
3991		snprintf(tmpdir, sizeof(tmpdir), "%s/%s.%s-%03d", tmp,
3992		    progname, tmpdir_timestamp, i);
3993		if (assertMakeDir(tmpdir,0755))
3994			break;
3995		if (i >= 999) {
3996			fprintf(stderr,
3997			    "ERROR: Unable to create temp directory %s\n",
3998			    tmpdir);
3999			exit(1);
4000		}
4001	}
4002
4003	/*
4004	 * If the user didn't specify a directory for locating
4005	 * reference files, try to find the reference files in
4006	 * the "usual places."
4007	 */
4008	refdir = refdir_alloc = get_refdir(refdir);
4009
4010	/*
4011	 * Banner with basic information.
4012	 */
4013	printf("\n");
4014	printf("If tests fail or crash, details will be in:\n");
4015	printf("   %s\n", tmpdir);
4016	printf("\n");
4017	if (verbosity > VERBOSITY_SUMMARY_ONLY) {
4018		printf("Reference files will be read from: %s\n", refdir);
4019#ifdef PROGRAM
4020		printf("Running tests on: %s\n", testprog);
4021#endif
4022		printf("Exercising: ");
4023		fflush(stdout);
4024		printf("%s\n", EXTRA_VERSION);
4025	} else {
4026		printf("Running ");
4027		fflush(stdout);
4028	}
4029
4030	/*
4031	 * Run some or all of the individual tests.
4032	 */
4033	saved_argv = argv;
4034	do {
4035		argv = saved_argv;
4036		do {
4037			int test_num;
4038
4039			test_num = get_test_set(test_set, limit, *argv, tests);
4040			if (test_num < 0) {
4041				printf("*** INVALID Test %s\n", *argv);
4042				free(refdir_alloc);
4043				free(testprogdir);
4044				usage(progname);
4045				return (1);
4046			}
4047			for (i = 0; i < test_num; i++) {
4048				tests_run++;
4049				if (test_run(test_set[i], tmpdir)) {
4050					tests_failed++;
4051					if (until_failure)
4052						goto finish;
4053				}
4054			}
4055			if (*argv != NULL)
4056				argv++;
4057		} while (*argv != NULL);
4058	} while (until_failure);
4059
4060finish:
4061	/* Must be freed after all tests run */
4062	free(tmp2);
4063	free(testprogdir);
4064	free(pwd);
4065
4066	/*
4067	 * Report summary statistics.
4068	 */
4069	if (verbosity > VERBOSITY_SUMMARY_ONLY) {
4070		printf("\n");
4071		printf("Totals:\n");
4072		printf("  Tests run:         %8d\n", tests_run);
4073		printf("  Tests failed:      %8d\n", tests_failed);
4074		printf("  Assertions checked:%8d\n", assertions);
4075		printf("  Assertions failed: %8d\n", failures);
4076		printf("  Skips reported:    %8d\n", skips);
4077	}
4078	if (failures) {
4079		printf("\n");
4080		printf("Failing tests:\n");
4081		for (i = 0; i < limit; ++i) {
4082			if (tests[i].failures)
4083				printf("  %d: %s (%d failures)\n", i,
4084				    tests[i].name, tests[i].failures);
4085		}
4086		printf("\n");
4087		printf("Details for failing tests: %s\n", tmpdir);
4088		printf("\n");
4089	} else {
4090		if (verbosity == VERBOSITY_SUMMARY_ONLY)
4091			printf("\n");
4092		printf("%d tests passed, no failures\n", tests_run);
4093	}
4094
4095	free(refdir_alloc);
4096
4097	/* If the final tmpdir is empty, we can remove it. */
4098	/* This should be the usual case when all tests succeed. */
4099	assertChdir("..");
4100	rmdir(tmpdir);
4101
4102	return (tests_failed ? 1 : 0);
4103}
4104