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