test_main.c revision 324418
1228753Smm/* 2228753Smm * Copyright (c) 2003-2009 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm 26228753Smm#include "test.h" 27248590Smm#include "test_utils.h" 28238825Smm#ifdef HAVE_SYS_IOCTL_H 29238825Smm#include <sys/ioctl.h> 30238825Smm#endif 31231200Smm#ifdef HAVE_SYS_TIME_H 32231200Smm#include <sys/time.h> 33231200Smm#endif 34228753Smm#include <errno.h> 35231200Smm#ifdef HAVE_ICONV_H 36231200Smm#include <iconv.h> 37231200Smm#endif 38238825Smm/* 39238825Smm * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h. 40238825Smm * As the include guards don't agree, the order of include is important. 41238825Smm */ 42238825Smm#ifdef HAVE_LINUX_EXT2_FS_H 43238825Smm#include <linux/ext2_fs.h> /* for Linux file flags */ 44238825Smm#endif 45238825Smm#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__) 46238825Smm#include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */ 47238825Smm#endif 48314565Smm#ifdef HAVE_LINUX_FS_H 49314565Smm#include <linux/fs.h> 50314565Smm#endif 51231200Smm#include <limits.h> 52228753Smm#include <locale.h> 53231200Smm#ifdef HAVE_SIGNAL_H 54231200Smm#include <signal.h> 55231200Smm#endif 56228753Smm#include <stdarg.h> 57228753Smm#include <time.h> 58228753Smm 59316338Smm#ifdef HAVE_SIGNAL_H 60316338Smm#endif 61314571Smm#ifdef HAVE_ACL_LIBACL_H 62314571Smm#include <acl/libacl.h> 63314571Smm#endif 64314571Smm#ifdef HAVE_SYS_TYPES_H 65314571Smm#include <sys/types.h> 66314571Smm#endif 67314571Smm#ifdef HAVE_SYS_ACL_H 68314571Smm#include <sys/acl.h> 69314571Smm#endif 70316338Smm#ifdef HAVE_SYS_EA_H 71316338Smm#include <sys/ea.h> 72316338Smm#endif 73316338Smm#ifdef HAVE_SYS_EXTATTR_H 74316338Smm#include <sys/extattr.h> 75316338Smm#endif 76316338Smm#if HAVE_SYS_XATTR_H 77316338Smm#include <sys/xattr.h> 78316338Smm#elif HAVE_ATTR_XATTR_H 79316338Smm#include <attr/xattr.h> 80316338Smm#endif 81316338Smm#ifdef HAVE_SYS_RICHACL_H 82316338Smm#include <sys/richacl.h> 83316338Smm#endif 84316338Smm#if HAVE_MEMBERSHIP_H 85314571Smm#include <membership.h> 86314571Smm#endif 87314571Smm 88228753Smm/* 89228753Smm * 90228753Smm * Windows support routines 91228753Smm * 92228753Smm * Note: Configuration is a tricky issue. Using HAVE_* feature macros 93228753Smm * in the test harness is dangerous because they cover up 94228753Smm * configuration errors. The classic example of this is omitting a 95228753Smm * configure check. If libarchive and libarchive_test both look for 96228753Smm * the same feature macro, such errors are hard to detect. Platform 97228753Smm * macros (e.g., _WIN32 or __GNUC__) are a little better, but can 98228753Smm * easily lead to very messy code. It's best to limit yourself 99228753Smm * to only the most generic programming techniques in the test harness 100228753Smm * and thus avoid conditionals altogether. Where that's not possible, 101228753Smm * try to minimize conditionals by grouping platform-specific tests in 102228753Smm * one place (e.g., test_acl_freebsd) or by adding new assert() 103228753Smm * functions (e.g., assertMakeHardlink()) to cover up platform 104228753Smm * differences. Platform-specific coding in libarchive_test is often 105228753Smm * a symptom that some capability is missing from libarchive itself. 106228753Smm */ 107228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 108228753Smm#include <io.h> 109248590Smm#include <direct.h> 110228753Smm#include <windows.h> 111228753Smm#ifndef F_OK 112228753Smm#define F_OK (0) 113228753Smm#endif 114228753Smm#ifndef S_ISDIR 115228753Smm#define S_ISDIR(m) ((m) & _S_IFDIR) 116228753Smm#endif 117228753Smm#ifndef S_ISREG 118228753Smm#define S_ISREG(m) ((m) & _S_IFREG) 119228753Smm#endif 120228753Smm#if !defined(__BORLANDC__) 121228753Smm#define access _access 122231200Smm#undef chdir 123228753Smm#define chdir _chdir 124228753Smm#endif 125228753Smm#ifndef fileno 126228753Smm#define fileno _fileno 127228753Smm#endif 128228753Smm/*#define fstat _fstat64*/ 129228753Smm#if !defined(__BORLANDC__) 130228753Smm#define getcwd _getcwd 131228753Smm#endif 132228753Smm#define lstat stat 133228753Smm/*#define lstat _stat64*/ 134228753Smm/*#define stat _stat64*/ 135228753Smm#define rmdir _rmdir 136228753Smm#if !defined(__BORLANDC__) 137228753Smm#define strdup _strdup 138228753Smm#define umask _umask 139228753Smm#endif 140228753Smm#define int64_t __int64 141228753Smm#endif 142228753Smm 143228753Smm#if defined(HAVE__CrtSetReportMode) 144228753Smm# include <crtdbg.h> 145228753Smm#endif 146228753Smm 147307214Smmmode_t umasked(mode_t expected_mode) 148307214Smm{ 149307214Smm mode_t mode = umask(0); 150307214Smm umask(mode); 151307214Smm return expected_mode & ~mode; 152307214Smm} 153307214Smm 154299425Smm/* Path to working directory for current test */ 155299425Smmconst char *testworkdir; 156299425Smm#ifdef PROGRAM 157299425Smm/* Pathname of exe to be tested. */ 158299425Smmconst char *testprogfile; 159299425Smm/* Name of exe to use in printf-formatted command strings. */ 160299425Smm/* On Windows, this includes leading/trailing quotes. */ 161299425Smmconst char *testprog; 162299425Smm#endif 163299425Smm 164228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 165238825Smmstatic void *GetFunctionKernel32(const char *); 166238825Smmstatic int my_CreateSymbolicLinkA(const char *, const char *, int); 167238825Smmstatic int my_CreateHardLinkA(const char *, const char *); 168238825Smmstatic int my_GetFileInformationByName(const char *, 169238825Smm BY_HANDLE_FILE_INFORMATION *); 170238825Smm 171238825Smmstatic void * 172238825SmmGetFunctionKernel32(const char *name) 173228753Smm{ 174228753Smm static HINSTANCE lib; 175228753Smm static int set; 176228753Smm if (!set) { 177228753Smm set = 1; 178228753Smm lib = LoadLibrary("kernel32.dll"); 179228753Smm } 180228753Smm if (lib == NULL) { 181228753Smm fprintf(stderr, "Can't load kernel32.dll?!\n"); 182228753Smm exit(1); 183228753Smm } 184228753Smm return (void *)GetProcAddress(lib, name); 185228753Smm} 186228753Smm 187228753Smmstatic int 188228753Smmmy_CreateSymbolicLinkA(const char *linkname, const char *target, int flags) 189228753Smm{ 190228753Smm static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD); 191228753Smm static int set; 192228753Smm if (!set) { 193228753Smm set = 1; 194228753Smm f = GetFunctionKernel32("CreateSymbolicLinkA"); 195228753Smm } 196228753Smm return f == NULL ? 0 : (*f)(linkname, target, flags); 197228753Smm} 198228753Smm 199228753Smmstatic int 200228753Smmmy_CreateHardLinkA(const char *linkname, const char *target) 201228753Smm{ 202228753Smm static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES); 203228753Smm static int set; 204228753Smm if (!set) { 205228753Smm set = 1; 206228753Smm f = GetFunctionKernel32("CreateHardLinkA"); 207228753Smm } 208228753Smm return f == NULL ? 0 : (*f)(linkname, target, NULL); 209228753Smm} 210228753Smm 211238825Smmstatic int 212228753Smmmy_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi) 213228753Smm{ 214228753Smm HANDLE h; 215228753Smm int r; 216228753Smm 217228753Smm memset(bhfi, 0, sizeof(*bhfi)); 218228753Smm h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, 219231200Smm OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 220228753Smm if (h == INVALID_HANDLE_VALUE) 221228753Smm return (0); 222228753Smm r = GetFileInformationByHandle(h, bhfi); 223228753Smm CloseHandle(h); 224228753Smm return (r); 225228753Smm} 226228753Smm#endif 227228753Smm 228299425Smm#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 229228753Smmstatic void 230228753Smminvalid_parameter_handler(const wchar_t * expression, 231228753Smm const wchar_t * function, const wchar_t * file, 232228753Smm unsigned int line, uintptr_t pReserved) 233228753Smm{ 234228753Smm /* nop */ 235313071Smm // Silence unused-parameter compiler warnings. 236313071Smm (void)expression; 237313071Smm (void)function; 238313071Smm (void)file; 239313071Smm (void)line; 240313071Smm (void)pReserved; 241228753Smm} 242228753Smm#endif 243228753Smm 244228753Smm/* 245228753Smm * 246228753Smm * OPTIONS FLAGS 247228753Smm * 248228753Smm */ 249228753Smm 250228753Smm/* Enable core dump on failure. */ 251228753Smmstatic int dump_on_failure = 0; 252228753Smm/* Default is to remove temp dirs and log data for successful tests. */ 253228753Smmstatic int keep_temp_files = 0; 254231200Smm/* Default is to run the specified tests once and report errors. */ 255231200Smmstatic int until_failure = 0; 256228753Smm/* Default is to just report pass/fail for each test. */ 257228753Smmstatic int verbosity = 0; 258228753Smm#define VERBOSITY_SUMMARY_ONLY -1 /* -q */ 259228753Smm#define VERBOSITY_PASSFAIL 0 /* Default */ 260228753Smm#define VERBOSITY_LIGHT_REPORT 1 /* -v */ 261228753Smm#define VERBOSITY_FULL 2 /* -vv */ 262228753Smm/* A few places generate even more output for verbosity > VERBOSITY_FULL, 263228753Smm * mostly for debugging the test harness itself. */ 264228753Smm/* Cumulative count of assertion failures. */ 265228753Smmstatic int failures = 0; 266228753Smm/* Cumulative count of reported skips. */ 267228753Smmstatic int skips = 0; 268228753Smm/* Cumulative count of assertions checked. */ 269228753Smmstatic int assertions = 0; 270228753Smm 271228753Smm/* Directory where uuencoded reference files can be found. */ 272228753Smmstatic const char *refdir; 273228753Smm 274228753Smm/* 275228753Smm * Report log information selectively to console and/or disk log. 276228753Smm */ 277228753Smmstatic int log_console = 0; 278228753Smmstatic FILE *logfile; 279228753Smmstatic void 280228753Smmvlogprintf(const char *fmt, va_list ap) 281228753Smm{ 282228753Smm#ifdef va_copy 283228753Smm va_list lfap; 284228753Smm va_copy(lfap, ap); 285228753Smm#endif 286228753Smm if (log_console) 287228753Smm vfprintf(stdout, fmt, ap); 288228753Smm if (logfile != NULL) 289228753Smm#ifdef va_copy 290228753Smm vfprintf(logfile, fmt, lfap); 291228753Smm va_end(lfap); 292228753Smm#else 293228753Smm vfprintf(logfile, fmt, ap); 294228753Smm#endif 295228753Smm} 296228753Smm 297228753Smmstatic void 298228753Smmlogprintf(const char *fmt, ...) 299228753Smm{ 300228753Smm va_list ap; 301228753Smm va_start(ap, fmt); 302228753Smm vlogprintf(fmt, ap); 303228753Smm va_end(ap); 304228753Smm} 305228753Smm 306228753Smm/* Set up a message to display only if next assertion fails. */ 307228753Smmstatic char msgbuff[4096]; 308228753Smmstatic const char *msg, *nextmsg; 309228753Smmvoid 310228753Smmfailure(const char *fmt, ...) 311228753Smm{ 312228753Smm va_list ap; 313231200Smm if (fmt == NULL) { 314231200Smm nextmsg = NULL; 315231200Smm } else { 316231200Smm va_start(ap, fmt); 317231200Smm vsprintf(msgbuff, fmt, ap); 318231200Smm va_end(ap); 319231200Smm nextmsg = msgbuff; 320231200Smm } 321228753Smm} 322228753Smm 323228753Smm/* 324228753Smm * Copy arguments into file-local variables. 325228753Smm * This was added to permit vararg assert() functions without needing 326228753Smm * variadic wrapper macros. Turns out that the vararg capability is almost 327228753Smm * never used, so almost all of the vararg assertions can be simplified 328228753Smm * by removing the vararg capability and reworking the wrapper macro to 329228753Smm * pass __FILE__, __LINE__ directly into the function instead of using 330228753Smm * this hook. I suspect this machinery is used so rarely that we 331228753Smm * would be better off just removing it entirely. That would simplify 332231200Smm * the code here noticeably. 333228753Smm */ 334231200Smmstatic const char *skipping_filename; 335231200Smmstatic int skipping_line; 336231200Smmvoid skipping_setup(const char *filename, int line) 337228753Smm{ 338231200Smm skipping_filename = filename; 339231200Smm skipping_line = line; 340228753Smm} 341228753Smm 342228753Smm/* Called at the beginning of each assert() function. */ 343228753Smmstatic void 344228753Smmassertion_count(const char *file, int line) 345228753Smm{ 346228753Smm (void)file; /* UNUSED */ 347228753Smm (void)line; /* UNUSED */ 348228753Smm ++assertions; 349228753Smm /* Proper handling of "failure()" message. */ 350228753Smm msg = nextmsg; 351228753Smm nextmsg = NULL; 352228753Smm /* Uncomment to print file:line after every assertion. 353228753Smm * Verbose, but occasionally useful in tracking down crashes. */ 354228753Smm /* printf("Checked %s:%d\n", file, line); */ 355228753Smm} 356228753Smm 357228753Smm/* 358228753Smm * For each test source file, we remember how many times each 359228753Smm * assertion was reported. Cleared before each new test, 360228753Smm * used by test_summarize(). 361228753Smm */ 362228753Smmstatic struct line { 363228753Smm int count; 364228753Smm int skip; 365228753Smm} failed_lines[10000]; 366231200Smmconst char *failed_filename; 367228753Smm 368228753Smm/* Count this failure, setup up log destination and handle initial report. */ 369228753Smmstatic void 370228753Smmfailure_start(const char *filename, int line, const char *fmt, ...) 371228753Smm{ 372228753Smm va_list ap; 373228753Smm 374228753Smm /* Record another failure for this line. */ 375228753Smm ++failures; 376231200Smm failed_filename = filename; 377228753Smm failed_lines[line].count++; 378228753Smm 379228753Smm /* Determine whether to log header to console. */ 380228753Smm switch (verbosity) { 381228753Smm case VERBOSITY_LIGHT_REPORT: 382228753Smm log_console = (failed_lines[line].count < 2); 383228753Smm break; 384228753Smm default: 385228753Smm log_console = (verbosity >= VERBOSITY_FULL); 386228753Smm } 387228753Smm 388228753Smm /* Log file:line header for this failure */ 389228753Smm va_start(ap, fmt); 390228753Smm#if _MSC_VER 391228753Smm logprintf("%s(%d): ", filename, line); 392228753Smm#else 393228753Smm logprintf("%s:%d: ", filename, line); 394228753Smm#endif 395228753Smm vlogprintf(fmt, ap); 396228753Smm va_end(ap); 397228753Smm logprintf("\n"); 398228753Smm 399228753Smm if (msg != NULL && msg[0] != '\0') { 400228753Smm logprintf(" Description: %s\n", msg); 401228753Smm msg = NULL; 402228753Smm } 403228753Smm 404228753Smm /* Determine whether to log details to console. */ 405228753Smm if (verbosity == VERBOSITY_LIGHT_REPORT) 406228753Smm log_console = 0; 407228753Smm} 408228753Smm 409228753Smm/* Complete reporting of failed tests. */ 410228753Smm/* 411228753Smm * The 'extra' hook here is used by libarchive to include libarchive 412228753Smm * error messages with assertion failures. It could also be used 413228753Smm * to add strerror() output, for example. Just define the EXTRA_DUMP() 414228753Smm * macro appropriately. 415228753Smm */ 416228753Smmstatic void 417228753Smmfailure_finish(void *extra) 418228753Smm{ 419228753Smm (void)extra; /* UNUSED (maybe) */ 420228753Smm#ifdef EXTRA_DUMP 421231200Smm if (extra != NULL) { 422231200Smm logprintf(" errno: %d\n", EXTRA_ERRNO(extra)); 423228753Smm logprintf(" detail: %s\n", EXTRA_DUMP(extra)); 424231200Smm } 425228753Smm#endif 426228753Smm 427228753Smm if (dump_on_failure) { 428228753Smm fprintf(stderr, 429228753Smm " *** forcing core dump so failure can be debugged ***\n"); 430228753Smm abort(); 431228753Smm } 432228753Smm} 433228753Smm 434228753Smm/* Inform user that we're skipping some checks. */ 435228753Smmvoid 436228753Smmtest_skipping(const char *fmt, ...) 437228753Smm{ 438228753Smm char buff[1024]; 439228753Smm va_list ap; 440228753Smm 441228753Smm va_start(ap, fmt); 442228753Smm vsprintf(buff, fmt, ap); 443228753Smm va_end(ap); 444231200Smm /* Use failure() message if set. */ 445231200Smm msg = nextmsg; 446231200Smm nextmsg = NULL; 447228753Smm /* failure_start() isn't quite right, but is awfully convenient. */ 448231200Smm failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff); 449228753Smm --failures; /* Undo failures++ in failure_start() */ 450228753Smm /* Don't failure_finish() here. */ 451228753Smm /* Mark as skip, so doesn't count as failed test. */ 452231200Smm failed_lines[skipping_line].skip = 1; 453228753Smm ++skips; 454228753Smm} 455228753Smm 456228753Smm/* 457228753Smm * 458228753Smm * ASSERTIONS 459228753Smm * 460228753Smm */ 461228753Smm 462228753Smm/* Generic assert() just displays the failed condition. */ 463228753Smmint 464228753Smmassertion_assert(const char *file, int line, int value, 465228753Smm const char *condition, void *extra) 466228753Smm{ 467228753Smm assertion_count(file, line); 468228753Smm if (!value) { 469228753Smm failure_start(file, line, "Assertion failed: %s", condition); 470228753Smm failure_finish(extra); 471228753Smm } 472228753Smm return (value); 473228753Smm} 474228753Smm 475228753Smm/* chdir() and report any errors */ 476228753Smmint 477228753Smmassertion_chdir(const char *file, int line, const char *pathname) 478228753Smm{ 479228753Smm assertion_count(file, line); 480228753Smm if (chdir(pathname) == 0) 481228753Smm return (1); 482228753Smm failure_start(file, line, "chdir(\"%s\")", pathname); 483228753Smm failure_finish(NULL); 484228753Smm return (0); 485228753Smm 486228753Smm} 487228753Smm 488228753Smm/* Verify two integers are equal. */ 489228753Smmint 490228753Smmassertion_equal_int(const char *file, int line, 491228753Smm long long v1, const char *e1, long long v2, const char *e2, void *extra) 492228753Smm{ 493228753Smm assertion_count(file, line); 494228753Smm if (v1 == v2) 495228753Smm return (1); 496228753Smm failure_start(file, line, "%s != %s", e1, e2); 497228753Smm logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1); 498228753Smm logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2); 499228753Smm failure_finish(extra); 500228753Smm return (0); 501228753Smm} 502228753Smm 503231200Smm/* 504231200Smm * Utility to convert a single UTF-8 sequence. 505231200Smm */ 506231200Smmstatic int 507231200Smm_utf8_to_unicode(uint32_t *pwc, const char *s, size_t n) 508228753Smm{ 509231200Smm static const char utf8_count[256] = { 510231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */ 511231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */ 512231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */ 513231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */ 514231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */ 515231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */ 516231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */ 517231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */ 518231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */ 519231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */ 520231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */ 521231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */ 522231200Smm 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */ 523231200Smm 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */ 524231200Smm 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */ 525231200Smm 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */ 526231200Smm }; 527231200Smm int ch; 528231200Smm int cnt; 529231200Smm uint32_t wc; 530231200Smm 531231200Smm *pwc = 0; 532231200Smm 533231200Smm /* Sanity check. */ 534231200Smm if (n == 0) 535231200Smm return (0); 536231200Smm /* 537231200Smm * Decode 1-4 bytes depending on the value of the first byte. 538231200Smm */ 539231200Smm ch = (unsigned char)*s; 540231200Smm if (ch == 0) 541231200Smm return (0); /* Standard: return 0 for end-of-string. */ 542231200Smm cnt = utf8_count[ch]; 543231200Smm 544309587Smm /* Invalid sequence or there are not plenty bytes. */ 545231200Smm if (n < (size_t)cnt) 546231200Smm return (-1); 547231200Smm 548231200Smm /* Make a Unicode code point from a single UTF-8 sequence. */ 549231200Smm switch (cnt) { 550231200Smm case 1: /* 1 byte sequence. */ 551231200Smm *pwc = ch & 0x7f; 552231200Smm return (cnt); 553231200Smm case 2: /* 2 bytes sequence. */ 554231200Smm if ((s[1] & 0xc0) != 0x80) return (-1); 555231200Smm *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f); 556231200Smm return (cnt); 557231200Smm case 3: /* 3 bytes sequence. */ 558231200Smm if ((s[1] & 0xc0) != 0x80) return (-1); 559231200Smm if ((s[2] & 0xc0) != 0x80) return (-1); 560231200Smm wc = ((ch & 0x0f) << 12) 561231200Smm | ((s[1] & 0x3f) << 6) 562231200Smm | (s[2] & 0x3f); 563231200Smm if (wc < 0x800) 564231200Smm return (-1);/* Overlong sequence. */ 565231200Smm break; 566231200Smm case 4: /* 4 bytes sequence. */ 567231200Smm if (n < 4) 568231200Smm return (-1); 569231200Smm if ((s[1] & 0xc0) != 0x80) return (-1); 570231200Smm if ((s[2] & 0xc0) != 0x80) return (-1); 571231200Smm if ((s[3] & 0xc0) != 0x80) return (-1); 572231200Smm wc = ((ch & 0x07) << 18) 573231200Smm | ((s[1] & 0x3f) << 12) 574231200Smm | ((s[2] & 0x3f) << 6) 575231200Smm | (s[3] & 0x3f); 576231200Smm if (wc < 0x10000) 577231200Smm return (-1);/* Overlong sequence. */ 578231200Smm break; 579231200Smm default: 580231200Smm return (-1); 581231200Smm } 582231200Smm 583309587Smm /* The code point larger than 0x10FFFF is not legal 584231200Smm * Unicode values. */ 585231200Smm if (wc > 0x10FFFF) 586231200Smm return (-1); 587231200Smm /* Correctly gets a Unicode, returns used bytes. */ 588231200Smm *pwc = wc; 589231200Smm return (cnt); 590231200Smm} 591231200Smm 592231200Smmstatic void strdump(const char *e, const char *p, int ewidth, int utf8) 593231200Smm{ 594228753Smm const char *q = p; 595228753Smm 596231200Smm logprintf(" %*s = ", ewidth, e); 597228753Smm if (p == NULL) { 598231200Smm logprintf("NULL\n"); 599228753Smm return; 600228753Smm } 601228753Smm logprintf("\""); 602228753Smm while (*p != '\0') { 603228753Smm unsigned int c = 0xff & *p++; 604228753Smm switch (c) { 605299425Smm case '\a': logprintf("\\a"); break; 606299425Smm case '\b': logprintf("\\b"); break; 607299425Smm case '\n': logprintf("\\n"); break; 608299425Smm case '\r': logprintf("\\r"); break; 609228753Smm default: 610228753Smm if (c >= 32 && c < 127) 611228753Smm logprintf("%c", c); 612228753Smm else 613228753Smm logprintf("\\x%02X", c); 614228753Smm } 615228753Smm } 616228753Smm logprintf("\""); 617231200Smm logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q)); 618231200Smm 619231200Smm /* 620231200Smm * If the current string is UTF-8, dump its code points. 621231200Smm */ 622231200Smm if (utf8) { 623231200Smm size_t len; 624231200Smm uint32_t uc; 625231200Smm int n; 626231200Smm int cnt = 0; 627231200Smm 628231200Smm p = q; 629231200Smm len = strlen(p); 630231200Smm logprintf(" ["); 631231200Smm while ((n = _utf8_to_unicode(&uc, p, len)) > 0) { 632231200Smm if (p != q) 633231200Smm logprintf(" "); 634231200Smm logprintf("%04X", uc); 635231200Smm p += n; 636231200Smm len -= n; 637231200Smm cnt++; 638231200Smm } 639231200Smm logprintf("]"); 640231200Smm logprintf(" (count %d", cnt); 641231200Smm if (n < 0) { 642231200Smm logprintf(",unknown %d bytes", len); 643231200Smm } 644231200Smm logprintf(")"); 645231200Smm 646231200Smm } 647231200Smm logprintf("\n"); 648228753Smm} 649228753Smm 650228753Smm/* Verify two strings are equal, dump them if not. */ 651228753Smmint 652228753Smmassertion_equal_string(const char *file, int line, 653228753Smm const char *v1, const char *e1, 654228753Smm const char *v2, const char *e2, 655231200Smm void *extra, int utf8) 656228753Smm{ 657231200Smm int l1, l2; 658231200Smm 659228753Smm assertion_count(file, line); 660228753Smm if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0)) 661228753Smm return (1); 662228753Smm failure_start(file, line, "%s != %s", e1, e2); 663248590Smm l1 = (int)strlen(e1); 664248590Smm l2 = (int)strlen(e2); 665231200Smm if (l1 < l2) 666231200Smm l1 = l2; 667231200Smm strdump(e1, v1, l1, utf8); 668231200Smm strdump(e2, v2, l1, utf8); 669228753Smm failure_finish(extra); 670228753Smm return (0); 671228753Smm} 672228753Smm 673228753Smmstatic void 674228753Smmwcsdump(const char *e, const wchar_t *w) 675228753Smm{ 676228753Smm logprintf(" %s = ", e); 677228753Smm if (w == NULL) { 678228753Smm logprintf("(null)"); 679228753Smm return; 680228753Smm } 681228753Smm logprintf("\""); 682228753Smm while (*w != L'\0') { 683228753Smm unsigned int c = *w++; 684228753Smm if (c >= 32 && c < 127) 685228753Smm logprintf("%c", c); 686228753Smm else if (c < 256) 687228753Smm logprintf("\\x%02X", c); 688228753Smm else if (c < 0x10000) 689228753Smm logprintf("\\u%04X", c); 690228753Smm else 691228753Smm logprintf("\\U%08X", c); 692228753Smm } 693228753Smm logprintf("\"\n"); 694228753Smm} 695228753Smm 696228753Smm#ifndef HAVE_WCSCMP 697228753Smmstatic int 698228753Smmwcscmp(const wchar_t *s1, const wchar_t *s2) 699228753Smm{ 700228753Smm 701228753Smm while (*s1 == *s2++) { 702228753Smm if (*s1++ == L'\0') 703228753Smm return 0; 704228753Smm } 705228753Smm if (*s1 > *--s2) 706228753Smm return 1; 707228753Smm else 708228753Smm return -1; 709228753Smm} 710228753Smm#endif 711228753Smm 712228753Smm/* Verify that two wide strings are equal, dump them if not. */ 713228753Smmint 714228753Smmassertion_equal_wstring(const char *file, int line, 715228753Smm const wchar_t *v1, const char *e1, 716228753Smm const wchar_t *v2, const char *e2, 717228753Smm void *extra) 718228753Smm{ 719228753Smm assertion_count(file, line); 720231200Smm if (v1 == v2) 721228753Smm return (1); 722231200Smm if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0) 723231200Smm return (1); 724228753Smm failure_start(file, line, "%s != %s", e1, e2); 725228753Smm wcsdump(e1, v1); 726228753Smm wcsdump(e2, v2); 727228753Smm failure_finish(extra); 728228753Smm return (0); 729228753Smm} 730228753Smm 731228753Smm/* 732228753Smm * Pretty standard hexdump routine. As a bonus, if ref != NULL, then 733228753Smm * any bytes in p that differ from ref will be highlighted with '_' 734228753Smm * before and after the hex value. 735228753Smm */ 736228753Smmstatic void 737228753Smmhexdump(const char *p, const char *ref, size_t l, size_t offset) 738228753Smm{ 739228753Smm size_t i, j; 740228753Smm char sep; 741228753Smm 742228753Smm if (p == NULL) { 743228753Smm logprintf("(null)\n"); 744228753Smm return; 745228753Smm } 746228753Smm for(i=0; i < l; i+=16) { 747228753Smm logprintf("%04x", (unsigned)(i + offset)); 748228753Smm sep = ' '; 749228753Smm for (j = 0; j < 16 && i + j < l; j++) { 750228753Smm if (ref != NULL && p[i + j] != ref[i + j]) 751228753Smm sep = '_'; 752228753Smm logprintf("%c%02x", sep, 0xff & (int)p[i+j]); 753228753Smm if (ref != NULL && p[i + j] == ref[i + j]) 754228753Smm sep = ' '; 755228753Smm } 756228753Smm for (; j < 16; j++) { 757228753Smm logprintf("%c ", sep); 758228753Smm sep = ' '; 759228753Smm } 760228753Smm logprintf("%c", sep); 761228753Smm for (j=0; j < 16 && i + j < l; j++) { 762228753Smm int c = p[i + j]; 763228753Smm if (c >= ' ' && c <= 126) 764228753Smm logprintf("%c", c); 765228753Smm else 766228753Smm logprintf("."); 767228753Smm } 768228753Smm logprintf("\n"); 769228753Smm } 770228753Smm} 771228753Smm 772228753Smm/* Verify that two blocks of memory are the same, display the first 773228753Smm * block of differences if they're not. */ 774228753Smmint 775228753Smmassertion_equal_mem(const char *file, int line, 776228753Smm const void *_v1, const char *e1, 777228753Smm const void *_v2, const char *e2, 778228753Smm size_t l, const char *ld, void *extra) 779228753Smm{ 780228753Smm const char *v1 = (const char *)_v1; 781228753Smm const char *v2 = (const char *)_v2; 782228753Smm size_t offset; 783228753Smm 784228753Smm assertion_count(file, line); 785228753Smm if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0)) 786228753Smm return (1); 787248590Smm if (v1 == NULL || v2 == NULL) 788248590Smm return (0); 789228753Smm 790228753Smm failure_start(file, line, "%s != %s", e1, e2); 791228753Smm logprintf(" size %s = %d\n", ld, (int)l); 792228753Smm /* Dump 48 bytes (3 lines) so that the first difference is 793228753Smm * in the second line. */ 794228753Smm offset = 0; 795228753Smm while (l > 64 && memcmp(v1, v2, 32) == 0) { 796228753Smm /* Two lines agree, so step forward one line. */ 797228753Smm v1 += 16; 798228753Smm v2 += 16; 799228753Smm l -= 16; 800228753Smm offset += 16; 801228753Smm } 802228753Smm logprintf(" Dump of %s\n", e1); 803231200Smm hexdump(v1, v2, l < 128 ? l : 128, offset); 804228753Smm logprintf(" Dump of %s\n", e2); 805231200Smm hexdump(v2, v1, l < 128 ? l : 128, offset); 806228753Smm logprintf("\n"); 807228753Smm failure_finish(extra); 808228753Smm return (0); 809228753Smm} 810228753Smm 811299425Smm/* Verify that a block of memory is filled with the specified byte. */ 812299425Smmint 813299425Smmassertion_memory_filled_with(const char *file, int line, 814299425Smm const void *_v1, const char *vd, 815299425Smm size_t l, const char *ld, 816299425Smm char b, const char *bd, void *extra) 817299425Smm{ 818299425Smm const char *v1 = (const char *)_v1; 819299425Smm size_t c = 0; 820299425Smm size_t i; 821299425Smm (void)ld; /* UNUSED */ 822299425Smm 823299425Smm assertion_count(file, line); 824299425Smm 825299425Smm for (i = 0; i < l; ++i) { 826299425Smm if (v1[i] == b) { 827299425Smm ++c; 828299425Smm } 829299425Smm } 830299425Smm if (c == l) 831299425Smm return (1); 832299425Smm 833299425Smm failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd); 834299425Smm logprintf(" Only %d bytes were correct\n", (int)c); 835299425Smm failure_finish(extra); 836299425Smm return (0); 837299425Smm} 838299425Smm 839228753Smm/* Verify that the named file exists and is empty. */ 840228753Smmint 841231200Smmassertion_empty_file(const char *filename, int line, const char *f1) 842228753Smm{ 843228753Smm char buff[1024]; 844228753Smm struct stat st; 845228753Smm ssize_t s; 846228753Smm FILE *f; 847228753Smm 848231200Smm assertion_count(filename, line); 849228753Smm 850228753Smm if (stat(f1, &st) != 0) { 851231200Smm failure_start(filename, line, "Stat failed: %s", f1); 852228753Smm failure_finish(NULL); 853228753Smm return (0); 854228753Smm } 855228753Smm if (st.st_size == 0) 856228753Smm return (1); 857228753Smm 858231200Smm failure_start(filename, line, "File should be empty: %s", f1); 859228753Smm logprintf(" File size: %d\n", (int)st.st_size); 860228753Smm logprintf(" Contents:\n"); 861228753Smm f = fopen(f1, "rb"); 862228753Smm if (f == NULL) { 863228753Smm logprintf(" Unable to open %s\n", f1); 864228753Smm } else { 865228753Smm s = ((off_t)sizeof(buff) < st.st_size) ? 866228753Smm (ssize_t)sizeof(buff) : (ssize_t)st.st_size; 867228753Smm s = fread(buff, 1, s, f); 868228753Smm hexdump(buff, NULL, s, 0); 869228753Smm fclose(f); 870228753Smm } 871228753Smm failure_finish(NULL); 872228753Smm return (0); 873228753Smm} 874228753Smm 875228753Smm/* Verify that the named file exists and is not empty. */ 876228753Smmint 877231200Smmassertion_non_empty_file(const char *filename, int line, const char *f1) 878228753Smm{ 879228753Smm struct stat st; 880228753Smm 881231200Smm assertion_count(filename, line); 882228753Smm 883228753Smm if (stat(f1, &st) != 0) { 884231200Smm failure_start(filename, line, "Stat failed: %s", f1); 885228753Smm failure_finish(NULL); 886228753Smm return (0); 887228753Smm } 888228753Smm if (st.st_size == 0) { 889231200Smm failure_start(filename, line, "File empty: %s", f1); 890228753Smm failure_finish(NULL); 891228753Smm return (0); 892228753Smm } 893228753Smm return (1); 894228753Smm} 895228753Smm 896228753Smm/* Verify that two files have the same contents. */ 897228753Smm/* TODO: hexdump the first bytes that actually differ. */ 898228753Smmint 899231200Smmassertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2) 900228753Smm{ 901228753Smm char buff1[1024]; 902228753Smm char buff2[1024]; 903228753Smm FILE *f1, *f2; 904228753Smm int n1, n2; 905228753Smm 906231200Smm assertion_count(filename, line); 907228753Smm 908228753Smm f1 = fopen(fn1, "rb"); 909228753Smm f2 = fopen(fn2, "rb"); 910248590Smm if (f1 == NULL || f2 == NULL) { 911248590Smm if (f1) fclose(f1); 912248590Smm if (f2) fclose(f2); 913248590Smm return (0); 914248590Smm } 915228753Smm for (;;) { 916248590Smm n1 = (int)fread(buff1, 1, sizeof(buff1), f1); 917248590Smm n2 = (int)fread(buff2, 1, sizeof(buff2), f2); 918228753Smm if (n1 != n2) 919228753Smm break; 920228753Smm if (n1 == 0 && n2 == 0) { 921228753Smm fclose(f1); 922228753Smm fclose(f2); 923228753Smm return (1); 924228753Smm } 925228753Smm if (memcmp(buff1, buff2, n1) != 0) 926228753Smm break; 927228753Smm } 928228753Smm fclose(f1); 929228753Smm fclose(f2); 930231200Smm failure_start(filename, line, "Files not identical"); 931228753Smm logprintf(" file1=\"%s\"\n", fn1); 932228753Smm logprintf(" file2=\"%s\"\n", fn2); 933231200Smm failure_finish(NULL); 934228753Smm return (0); 935228753Smm} 936228753Smm 937228753Smm/* Verify that the named file does exist. */ 938228753Smmint 939231200Smmassertion_file_exists(const char *filename, int line, const char *f) 940228753Smm{ 941231200Smm assertion_count(filename, line); 942228753Smm 943228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 944228753Smm if (!_access(f, 0)) 945228753Smm return (1); 946228753Smm#else 947228753Smm if (!access(f, F_OK)) 948228753Smm return (1); 949228753Smm#endif 950231200Smm failure_start(filename, line, "File should exist: %s", f); 951231200Smm failure_finish(NULL); 952228753Smm return (0); 953228753Smm} 954228753Smm 955228753Smm/* Verify that the named file doesn't exist. */ 956228753Smmint 957231200Smmassertion_file_not_exists(const char *filename, int line, const char *f) 958228753Smm{ 959231200Smm assertion_count(filename, line); 960228753Smm 961228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 962228753Smm if (_access(f, 0)) 963228753Smm return (1); 964228753Smm#else 965228753Smm if (access(f, F_OK)) 966228753Smm return (1); 967228753Smm#endif 968231200Smm failure_start(filename, line, "File should not exist: %s", f); 969231200Smm failure_finish(NULL); 970228753Smm return (0); 971228753Smm} 972228753Smm 973228753Smm/* Compare the contents of a file to a block of memory. */ 974228753Smmint 975231200Smmassertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn) 976228753Smm{ 977228753Smm char *contents; 978228753Smm FILE *f; 979228753Smm int n; 980228753Smm 981231200Smm assertion_count(filename, line); 982228753Smm 983228753Smm f = fopen(fn, "rb"); 984228753Smm if (f == NULL) { 985231200Smm failure_start(filename, line, 986228753Smm "File should exist: %s", fn); 987231200Smm failure_finish(NULL); 988228753Smm return (0); 989228753Smm } 990228753Smm contents = malloc(s * 2); 991248590Smm n = (int)fread(contents, 1, s * 2, f); 992228753Smm fclose(f); 993228753Smm if (n == s && memcmp(buff, contents, s) == 0) { 994228753Smm free(contents); 995228753Smm return (1); 996228753Smm } 997231200Smm failure_start(filename, line, "File contents don't match"); 998228753Smm logprintf(" file=\"%s\"\n", fn); 999228753Smm if (n > 0) 1000228753Smm hexdump(contents, buff, n > 512 ? 512 : n, 0); 1001228753Smm else { 1002228753Smm logprintf(" File empty, contents should be:\n"); 1003231200Smm hexdump(buff, NULL, s > 512 ? 512 : s, 0); 1004228753Smm } 1005231200Smm failure_finish(NULL); 1006228753Smm free(contents); 1007228753Smm return (0); 1008228753Smm} 1009228753Smm 1010228753Smm/* Check the contents of a text file, being tolerant of line endings. */ 1011228753Smmint 1012231200Smmassertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn) 1013228753Smm{ 1014228753Smm char *contents; 1015228753Smm const char *btxt, *ftxt; 1016228753Smm FILE *f; 1017228753Smm int n, s; 1018228753Smm 1019231200Smm assertion_count(filename, line); 1020228753Smm f = fopen(fn, "r"); 1021231200Smm if (f == NULL) { 1022231200Smm failure_start(filename, line, 1023231200Smm "File doesn't exist: %s", fn); 1024231200Smm failure_finish(NULL); 1025231200Smm return (0); 1026231200Smm } 1027248590Smm s = (int)strlen(buff); 1028228753Smm contents = malloc(s * 2 + 128); 1029248590Smm n = (int)fread(contents, 1, s * 2 + 128 - 1, f); 1030228753Smm if (n >= 0) 1031228753Smm contents[n] = '\0'; 1032228753Smm fclose(f); 1033228753Smm /* Compare texts. */ 1034228753Smm btxt = buff; 1035228753Smm ftxt = (const char *)contents; 1036228753Smm while (*btxt != '\0' && *ftxt != '\0') { 1037228753Smm if (*btxt == *ftxt) { 1038228753Smm ++btxt; 1039228753Smm ++ftxt; 1040228753Smm continue; 1041228753Smm } 1042228753Smm if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') { 1043228753Smm /* Pass over different new line characters. */ 1044228753Smm ++btxt; 1045228753Smm ftxt += 2; 1046228753Smm continue; 1047228753Smm } 1048228753Smm break; 1049228753Smm } 1050228753Smm if (*btxt == '\0' && *ftxt == '\0') { 1051228753Smm free(contents); 1052228753Smm return (1); 1053228753Smm } 1054231200Smm failure_start(filename, line, "Contents don't match"); 1055228753Smm logprintf(" file=\"%s\"\n", fn); 1056231200Smm if (n > 0) { 1057228753Smm hexdump(contents, buff, n, 0); 1058231200Smm logprintf(" expected\n", fn); 1059231200Smm hexdump(buff, contents, s, 0); 1060231200Smm } else { 1061228753Smm logprintf(" File empty, contents should be:\n"); 1062228753Smm hexdump(buff, NULL, s, 0); 1063228753Smm } 1064231200Smm failure_finish(NULL); 1065228753Smm free(contents); 1066228753Smm return (0); 1067228753Smm} 1068228753Smm 1069231200Smm/* Verify that a text file contains the specified lines, regardless of order */ 1070231200Smm/* This could be more efficient if we sorted both sets of lines, etc, but 1071231200Smm * since this is used only for testing and only ever deals with a dozen or so 1072231200Smm * lines at a time, this relatively crude approach is just fine. */ 1073231200Smmint 1074231200Smmassertion_file_contains_lines_any_order(const char *file, int line, 1075231200Smm const char *pathname, const char *lines[]) 1076231200Smm{ 1077231200Smm char *buff; 1078231200Smm size_t buff_size; 1079231200Smm size_t expected_count, actual_count, i, j; 1080248590Smm char **expected = NULL; 1081248590Smm char *p, **actual = NULL; 1082231200Smm char c; 1083231200Smm int expected_failure = 0, actual_failure = 0; 1084231200Smm 1085231200Smm assertion_count(file, line); 1086231200Smm 1087231200Smm buff = slurpfile(&buff_size, "%s", pathname); 1088231200Smm if (buff == NULL) { 1089231200Smm failure_start(pathname, line, "Can't read file: %s", pathname); 1090231200Smm failure_finish(NULL); 1091231200Smm return (0); 1092231200Smm } 1093231200Smm 1094248590Smm /* Make a copy of the provided lines and count up the expected 1095248590Smm * file size. */ 1096231200Smm for (i = 0; lines[i] != NULL; ++i) { 1097231200Smm } 1098231200Smm expected_count = i; 1099248590Smm if (expected_count) { 1100248590Smm expected = malloc(sizeof(char *) * expected_count); 1101248590Smm if (expected == NULL) { 1102248590Smm failure_start(pathname, line, "Can't allocate memory"); 1103248590Smm failure_finish(NULL); 1104299425Smm free(expected); 1105318483Smm free(buff); 1106248590Smm return (0); 1107248590Smm } 1108248590Smm for (i = 0; lines[i] != NULL; ++i) { 1109248590Smm expected[i] = strdup(lines[i]); 1110248590Smm } 1111231200Smm } 1112231200Smm 1113231200Smm /* Break the file into lines */ 1114231200Smm actual_count = 0; 1115231200Smm for (c = '\0', p = buff; p < buff + buff_size; ++p) { 1116231200Smm if (*p == '\x0d' || *p == '\x0a') 1117231200Smm *p = '\0'; 1118231200Smm if (c == '\0' && *p != '\0') 1119231200Smm ++actual_count; 1120231200Smm c = *p; 1121231200Smm } 1122248590Smm if (actual_count) { 1123248590Smm actual = calloc(sizeof(char *), actual_count); 1124248590Smm if (actual == NULL) { 1125248590Smm failure_start(pathname, line, "Can't allocate memory"); 1126248590Smm failure_finish(NULL); 1127248590Smm free(expected); 1128318483Smm free(buff); 1129248590Smm return (0); 1130231200Smm } 1131248590Smm for (j = 0, p = buff; p < buff + buff_size; 1132248590Smm p += 1 + strlen(p)) { 1133248590Smm if (*p != '\0') { 1134248590Smm actual[j] = p; 1135248590Smm ++j; 1136248590Smm } 1137248590Smm } 1138231200Smm } 1139231200Smm 1140231200Smm /* Erase matching lines from both lists */ 1141231200Smm for (i = 0; i < expected_count; ++i) { 1142231200Smm if (expected[i] == NULL) 1143231200Smm continue; 1144231200Smm for (j = 0; j < actual_count; ++j) { 1145231200Smm if (actual[j] == NULL) 1146231200Smm continue; 1147231200Smm if (strcmp(expected[i], actual[j]) == 0) { 1148231200Smm free(expected[i]); 1149231200Smm expected[i] = NULL; 1150231200Smm actual[j] = NULL; 1151231200Smm break; 1152231200Smm } 1153231200Smm } 1154231200Smm } 1155231200Smm 1156231200Smm /* If there's anything left, it's a failure */ 1157231200Smm for (i = 0; i < expected_count; ++i) { 1158231200Smm if (expected[i] != NULL) 1159231200Smm ++expected_failure; 1160231200Smm } 1161231200Smm for (j = 0; j < actual_count; ++j) { 1162231200Smm if (actual[j] != NULL) 1163231200Smm ++actual_failure; 1164231200Smm } 1165231200Smm if (expected_failure == 0 && actual_failure == 0) { 1166231200Smm free(buff); 1167231200Smm free(expected); 1168231200Smm free(actual); 1169231200Smm return (1); 1170231200Smm } 1171231200Smm failure_start(file, line, "File doesn't match: %s", pathname); 1172231200Smm for (i = 0; i < expected_count; ++i) { 1173231200Smm if (expected[i] != NULL) { 1174231200Smm logprintf(" Expected but not present: %s\n", expected[i]); 1175231200Smm free(expected[i]); 1176231200Smm } 1177231200Smm } 1178231200Smm for (j = 0; j < actual_count; ++j) { 1179231200Smm if (actual[j] != NULL) 1180231200Smm logprintf(" Present but not expected: %s\n", actual[j]); 1181231200Smm } 1182231200Smm failure_finish(NULL); 1183231200Smm free(buff); 1184231200Smm free(expected); 1185231200Smm free(actual); 1186231200Smm return (0); 1187231200Smm} 1188231200Smm 1189307859Smm/* Verify that a text file does not contains the specified strings */ 1190307859Smmint 1191307859Smmassertion_file_contains_no_invalid_strings(const char *file, int line, 1192307859Smm const char *pathname, const char *strings[]) 1193307859Smm{ 1194307859Smm char *buff; 1195307859Smm int i; 1196307859Smm 1197307859Smm buff = slurpfile(NULL, "%s", pathname); 1198307859Smm if (buff == NULL) { 1199307859Smm failure_start(file, line, "Can't read file: %s", pathname); 1200307859Smm failure_finish(NULL); 1201307859Smm return (0); 1202307859Smm } 1203307859Smm 1204307859Smm for (i = 0; strings[i] != NULL; ++i) { 1205307859Smm if (strstr(buff, strings[i]) != NULL) { 1206307859Smm failure_start(file, line, "Invalid string in %s: %s", pathname, 1207307859Smm strings[i]); 1208307859Smm failure_finish(NULL); 1209307859Smm free(buff); 1210307859Smm return(0); 1211307859Smm } 1212307859Smm } 1213307859Smm 1214307859Smm free(buff); 1215307859Smm return (0); 1216307859Smm} 1217307859Smm 1218228753Smm/* Test that two paths point to the same file. */ 1219228753Smm/* As a side-effect, asserts that both files exist. */ 1220228753Smmstatic int 1221228753Smmis_hardlink(const char *file, int line, 1222228753Smm const char *path1, const char *path2) 1223228753Smm{ 1224228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1225228753Smm BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2; 1226228753Smm int r; 1227228753Smm 1228228753Smm assertion_count(file, line); 1229228753Smm r = my_GetFileInformationByName(path1, &bhfi1); 1230228753Smm if (r == 0) { 1231228753Smm failure_start(file, line, "File %s can't be inspected?", path1); 1232228753Smm failure_finish(NULL); 1233228753Smm return (0); 1234228753Smm } 1235228753Smm r = my_GetFileInformationByName(path2, &bhfi2); 1236228753Smm if (r == 0) { 1237228753Smm failure_start(file, line, "File %s can't be inspected?", path2); 1238228753Smm failure_finish(NULL); 1239228753Smm return (0); 1240228753Smm } 1241228753Smm return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber 1242228753Smm && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh 1243228753Smm && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow); 1244228753Smm#else 1245228753Smm struct stat st1, st2; 1246228753Smm int r; 1247228753Smm 1248228753Smm assertion_count(file, line); 1249228753Smm r = lstat(path1, &st1); 1250228753Smm if (r != 0) { 1251228753Smm failure_start(file, line, "File should exist: %s", path1); 1252228753Smm failure_finish(NULL); 1253228753Smm return (0); 1254228753Smm } 1255228753Smm r = lstat(path2, &st2); 1256228753Smm if (r != 0) { 1257228753Smm failure_start(file, line, "File should exist: %s", path2); 1258228753Smm failure_finish(NULL); 1259228753Smm return (0); 1260228753Smm } 1261228753Smm return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev); 1262228753Smm#endif 1263228753Smm} 1264228753Smm 1265228753Smmint 1266228753Smmassertion_is_hardlink(const char *file, int line, 1267228753Smm const char *path1, const char *path2) 1268228753Smm{ 1269228753Smm if (is_hardlink(file, line, path1, path2)) 1270228753Smm return (1); 1271228753Smm failure_start(file, line, 1272228753Smm "Files %s and %s are not hardlinked", path1, path2); 1273228753Smm failure_finish(NULL); 1274228753Smm return (0); 1275228753Smm} 1276228753Smm 1277228753Smmint 1278228753Smmassertion_is_not_hardlink(const char *file, int line, 1279228753Smm const char *path1, const char *path2) 1280228753Smm{ 1281228753Smm if (!is_hardlink(file, line, path1, path2)) 1282228753Smm return (1); 1283228753Smm failure_start(file, line, 1284228753Smm "Files %s and %s should not be hardlinked", path1, path2); 1285228753Smm failure_finish(NULL); 1286228753Smm return (0); 1287228753Smm} 1288228753Smm 1289228753Smm/* Verify a/b/mtime of 'pathname'. */ 1290228753Smm/* If 'recent', verify that it's within last 10 seconds. */ 1291228753Smmstatic int 1292228753Smmassertion_file_time(const char *file, int line, 1293228753Smm const char *pathname, long t, long nsec, char type, int recent) 1294228753Smm{ 1295228753Smm long long filet, filet_nsec; 1296228753Smm int r; 1297228753Smm 1298228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1299228753Smm#define EPOC_TIME (116444736000000000ULL) 1300248590Smm FILETIME fxtime, fbirthtime, fatime, fmtime; 1301228753Smm ULARGE_INTEGER wintm; 1302228753Smm HANDLE h; 1303248590Smm fxtime.dwLowDateTime = 0; 1304248590Smm fxtime.dwHighDateTime = 0; 1305228753Smm 1306228753Smm assertion_count(file, line); 1307231200Smm /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open 1308231200Smm * a directory file. If not, CreateFile() will fail when 1309231200Smm * the pathname is a directory. */ 1310228753Smm h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL, 1311231200Smm OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 1312228753Smm if (h == INVALID_HANDLE_VALUE) { 1313228753Smm failure_start(file, line, "Can't access %s\n", pathname); 1314228753Smm failure_finish(NULL); 1315228753Smm return (0); 1316228753Smm } 1317228753Smm r = GetFileTime(h, &fbirthtime, &fatime, &fmtime); 1318228753Smm switch (type) { 1319248590Smm case 'a': fxtime = fatime; break; 1320248590Smm case 'b': fxtime = fbirthtime; break; 1321248590Smm case 'm': fxtime = fmtime; break; 1322228753Smm } 1323228753Smm CloseHandle(h); 1324228753Smm if (r == 0) { 1325228753Smm failure_start(file, line, "Can't GetFileTime %s\n", pathname); 1326228753Smm failure_finish(NULL); 1327228753Smm return (0); 1328228753Smm } 1329248590Smm wintm.LowPart = fxtime.dwLowDateTime; 1330248590Smm wintm.HighPart = fxtime.dwHighDateTime; 1331228753Smm filet = (wintm.QuadPart - EPOC_TIME) / 10000000; 1332228753Smm filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100; 1333228753Smm nsec = (nsec / 100) * 100; /* Round the request */ 1334228753Smm#else 1335228753Smm struct stat st; 1336228753Smm 1337228753Smm assertion_count(file, line); 1338228753Smm r = lstat(pathname, &st); 1339228753Smm if (r != 0) { 1340228753Smm failure_start(file, line, "Can't stat %s\n", pathname); 1341228753Smm failure_finish(NULL); 1342228753Smm return (0); 1343228753Smm } 1344228753Smm switch (type) { 1345228753Smm case 'a': filet = st.st_atime; break; 1346228753Smm case 'm': filet = st.st_mtime; break; 1347228753Smm case 'b': filet = 0; break; 1348228753Smm default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1349228753Smm exit(1); 1350228753Smm } 1351228753Smm#if defined(__FreeBSD__) 1352228753Smm switch (type) { 1353228753Smm case 'a': filet_nsec = st.st_atimespec.tv_nsec; break; 1354228753Smm case 'b': filet = st.st_birthtime; 1355302264Smm /* FreeBSD filesystems that don't support birthtime 1356302264Smm * (e.g., UFS1) always return -1 here. */ 1357302264Smm if (filet == -1) { 1358302264Smm return (1); 1359302264Smm } 1360228753Smm filet_nsec = st.st_birthtimespec.tv_nsec; break; 1361228753Smm case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break; 1362228753Smm default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1363228753Smm exit(1); 1364228753Smm } 1365228753Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1366228753Smm filet_nsec = (filet_nsec / 1000) * 1000; 1367228753Smm nsec = (nsec / 1000) * 1000; 1368228753Smm#else 1369228753Smm filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */ 1370228753Smm if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */ 1371228753Smm#if defined(__HAIKU__) 1372228753Smm if (type == 'a') return (1); /* Haiku doesn't have atime. */ 1373228753Smm#endif 1374228753Smm#endif 1375228753Smm#endif 1376228753Smm if (recent) { 1377228753Smm /* Check that requested time is up-to-date. */ 1378228753Smm time_t now = time(NULL); 1379228753Smm if (filet < now - 10 || filet > now + 1) { 1380228753Smm failure_start(file, line, 1381231200Smm "File %s has %ctime %lld, %lld seconds ago\n", 1382228753Smm pathname, type, filet, now - filet); 1383228753Smm failure_finish(NULL); 1384228753Smm return (0); 1385228753Smm } 1386228753Smm } else if (filet != t || filet_nsec != nsec) { 1387228753Smm failure_start(file, line, 1388231200Smm "File %s has %ctime %lld.%09lld, expected %lld.%09lld", 1389228753Smm pathname, type, filet, filet_nsec, t, nsec); 1390228753Smm failure_finish(NULL); 1391228753Smm return (0); 1392228753Smm } 1393228753Smm return (1); 1394228753Smm} 1395228753Smm 1396228753Smm/* Verify atime of 'pathname'. */ 1397228753Smmint 1398228753Smmassertion_file_atime(const char *file, int line, 1399228753Smm const char *pathname, long t, long nsec) 1400228753Smm{ 1401228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'a', 0); 1402228753Smm} 1403228753Smm 1404228753Smm/* Verify atime of 'pathname' is up-to-date. */ 1405228753Smmint 1406228753Smmassertion_file_atime_recent(const char *file, int line, const char *pathname) 1407228753Smm{ 1408228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'a', 1); 1409228753Smm} 1410228753Smm 1411228753Smm/* Verify birthtime of 'pathname'. */ 1412228753Smmint 1413228753Smmassertion_file_birthtime(const char *file, int line, 1414228753Smm const char *pathname, long t, long nsec) 1415228753Smm{ 1416228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'b', 0); 1417228753Smm} 1418228753Smm 1419228753Smm/* Verify birthtime of 'pathname' is up-to-date. */ 1420228753Smmint 1421228753Smmassertion_file_birthtime_recent(const char *file, int line, 1422228753Smm const char *pathname) 1423228753Smm{ 1424228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'b', 1); 1425228753Smm} 1426228753Smm 1427307214Smm/* Verify mode of 'pathname'. */ 1428307214Smmint 1429307214Smmassertion_file_mode(const char *file, int line, const char *pathname, int expected_mode) 1430307214Smm{ 1431307214Smm int mode; 1432307214Smm int r; 1433307214Smm 1434307214Smm assertion_count(file, line); 1435307214Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1436307214Smm failure_start(file, line, "assertFileMode not yet implemented for Windows"); 1437307859Smm (void)mode; /* UNUSED */ 1438307859Smm (void)r; /* UNUSED */ 1439313071Smm (void)pathname; /* UNUSED */ 1440313071Smm (void)expected_mode; /* UNUSED */ 1441307214Smm#else 1442307214Smm { 1443307214Smm struct stat st; 1444307214Smm r = lstat(pathname, &st); 1445307214Smm mode = (int)(st.st_mode & 0777); 1446307214Smm } 1447307214Smm if (r == 0 && mode == expected_mode) 1448307214Smm return (1); 1449307214Smm failure_start(file, line, "File %s has mode %o, expected %o", 1450307214Smm pathname, mode, expected_mode); 1451307214Smm#endif 1452307214Smm failure_finish(NULL); 1453307214Smm return (0); 1454307214Smm} 1455307214Smm 1456228753Smm/* Verify mtime of 'pathname'. */ 1457228753Smmint 1458228753Smmassertion_file_mtime(const char *file, int line, 1459228753Smm const char *pathname, long t, long nsec) 1460228753Smm{ 1461228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'm', 0); 1462228753Smm} 1463228753Smm 1464228753Smm/* Verify mtime of 'pathname' is up-to-date. */ 1465228753Smmint 1466228753Smmassertion_file_mtime_recent(const char *file, int line, const char *pathname) 1467228753Smm{ 1468228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'm', 1); 1469228753Smm} 1470228753Smm 1471228753Smm/* Verify number of links to 'pathname'. */ 1472228753Smmint 1473228753Smmassertion_file_nlinks(const char *file, int line, 1474228753Smm const char *pathname, int nlinks) 1475228753Smm{ 1476228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1477228753Smm BY_HANDLE_FILE_INFORMATION bhfi; 1478228753Smm int r; 1479228753Smm 1480228753Smm assertion_count(file, line); 1481228753Smm r = my_GetFileInformationByName(pathname, &bhfi); 1482228753Smm if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks) 1483228753Smm return (1); 1484228753Smm failure_start(file, line, "File %s has %d links, expected %d", 1485228753Smm pathname, bhfi.nNumberOfLinks, nlinks); 1486228753Smm failure_finish(NULL); 1487228753Smm return (0); 1488228753Smm#else 1489228753Smm struct stat st; 1490228753Smm int r; 1491228753Smm 1492228753Smm assertion_count(file, line); 1493228753Smm r = lstat(pathname, &st); 1494231297Smm if (r == 0 && (int)st.st_nlink == nlinks) 1495307214Smm return (1); 1496228753Smm failure_start(file, line, "File %s has %d links, expected %d", 1497228753Smm pathname, st.st_nlink, nlinks); 1498228753Smm failure_finish(NULL); 1499228753Smm return (0); 1500228753Smm#endif 1501228753Smm} 1502228753Smm 1503228753Smm/* Verify size of 'pathname'. */ 1504228753Smmint 1505228753Smmassertion_file_size(const char *file, int line, const char *pathname, long size) 1506228753Smm{ 1507228753Smm int64_t filesize; 1508228753Smm int r; 1509228753Smm 1510228753Smm assertion_count(file, line); 1511228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1512228753Smm { 1513228753Smm BY_HANDLE_FILE_INFORMATION bhfi; 1514228753Smm r = !my_GetFileInformationByName(pathname, &bhfi); 1515228753Smm filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow; 1516228753Smm } 1517228753Smm#else 1518228753Smm { 1519228753Smm struct stat st; 1520228753Smm r = lstat(pathname, &st); 1521228753Smm filesize = st.st_size; 1522228753Smm } 1523228753Smm#endif 1524228753Smm if (r == 0 && filesize == size) 1525228753Smm return (1); 1526228753Smm failure_start(file, line, "File %s has size %ld, expected %ld", 1527228753Smm pathname, (long)filesize, (long)size); 1528228753Smm failure_finish(NULL); 1529228753Smm return (0); 1530228753Smm} 1531228753Smm 1532228753Smm/* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */ 1533228753Smmint 1534228753Smmassertion_is_dir(const char *file, int line, const char *pathname, int mode) 1535228753Smm{ 1536228753Smm struct stat st; 1537228753Smm int r; 1538228753Smm 1539228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1540228753Smm (void)mode; /* UNUSED */ 1541228753Smm#endif 1542228753Smm assertion_count(file, line); 1543228753Smm r = lstat(pathname, &st); 1544228753Smm if (r != 0) { 1545228753Smm failure_start(file, line, "Dir should exist: %s", pathname); 1546228753Smm failure_finish(NULL); 1547228753Smm return (0); 1548228753Smm } 1549228753Smm if (!S_ISDIR(st.st_mode)) { 1550228753Smm failure_start(file, line, "%s is not a dir", pathname); 1551228753Smm failure_finish(NULL); 1552228753Smm return (0); 1553228753Smm } 1554228753Smm#if !defined(_WIN32) || defined(__CYGWIN__) 1555228753Smm /* Windows doesn't handle permissions the same way as POSIX, 1556228753Smm * so just ignore the mode tests. */ 1557228753Smm /* TODO: Can we do better here? */ 1558231297Smm if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1559228753Smm failure_start(file, line, "Dir %s has wrong mode", pathname); 1560228753Smm logprintf(" Expected: 0%3o\n", mode); 1561228753Smm logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1562228753Smm failure_finish(NULL); 1563228753Smm return (0); 1564228753Smm } 1565228753Smm#endif 1566228753Smm return (1); 1567228753Smm} 1568228753Smm 1569228753Smm/* Verify that 'pathname' is a regular file. If 'mode' is >= 0, 1570228753Smm * verify that too. */ 1571228753Smmint 1572228753Smmassertion_is_reg(const char *file, int line, const char *pathname, int mode) 1573228753Smm{ 1574228753Smm struct stat st; 1575228753Smm int r; 1576228753Smm 1577228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1578228753Smm (void)mode; /* UNUSED */ 1579228753Smm#endif 1580228753Smm assertion_count(file, line); 1581228753Smm r = lstat(pathname, &st); 1582228753Smm if (r != 0 || !S_ISREG(st.st_mode)) { 1583228753Smm failure_start(file, line, "File should exist: %s", pathname); 1584228753Smm failure_finish(NULL); 1585228753Smm return (0); 1586228753Smm } 1587228753Smm#if !defined(_WIN32) || defined(__CYGWIN__) 1588228753Smm /* Windows doesn't handle permissions the same way as POSIX, 1589228753Smm * so just ignore the mode tests. */ 1590228753Smm /* TODO: Can we do better here? */ 1591231297Smm if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1592228753Smm failure_start(file, line, "File %s has wrong mode", pathname); 1593228753Smm logprintf(" Expected: 0%3o\n", mode); 1594228753Smm logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1595228753Smm failure_finish(NULL); 1596228753Smm return (0); 1597228753Smm } 1598228753Smm#endif 1599228753Smm return (1); 1600228753Smm} 1601228753Smm 1602228753Smm/* Check whether 'pathname' is a symbolic link. If 'contents' is 1603228753Smm * non-NULL, verify that the symlink has those contents. */ 1604228753Smmstatic int 1605228753Smmis_symlink(const char *file, int line, 1606228753Smm const char *pathname, const char *contents) 1607228753Smm{ 1608228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1609228753Smm (void)pathname; /* UNUSED */ 1610228753Smm (void)contents; /* UNUSED */ 1611228753Smm assertion_count(file, line); 1612228753Smm /* Windows sort-of has real symlinks, but they're only usable 1613228753Smm * by privileged users and are crippled even then, so there's 1614228753Smm * really not much point in bothering with this. */ 1615228753Smm return (0); 1616228753Smm#else 1617228753Smm char buff[300]; 1618228753Smm struct stat st; 1619228753Smm ssize_t linklen; 1620228753Smm int r; 1621228753Smm 1622228753Smm assertion_count(file, line); 1623228753Smm r = lstat(pathname, &st); 1624228753Smm if (r != 0) { 1625228753Smm failure_start(file, line, 1626228753Smm "Symlink should exist: %s", pathname); 1627228753Smm failure_finish(NULL); 1628228753Smm return (0); 1629228753Smm } 1630228753Smm if (!S_ISLNK(st.st_mode)) 1631228753Smm return (0); 1632228753Smm if (contents == NULL) 1633228753Smm return (1); 1634228753Smm linklen = readlink(pathname, buff, sizeof(buff)); 1635228753Smm if (linklen < 0) { 1636228753Smm failure_start(file, line, "Can't read symlink %s", pathname); 1637228753Smm failure_finish(NULL); 1638228753Smm return (0); 1639228753Smm } 1640228753Smm buff[linklen] = '\0'; 1641228753Smm if (strcmp(buff, contents) != 0) 1642228753Smm return (0); 1643228753Smm return (1); 1644228753Smm#endif 1645228753Smm} 1646228753Smm 1647228753Smm/* Assert that path is a symlink that (optionally) contains contents. */ 1648228753Smmint 1649228753Smmassertion_is_symlink(const char *file, int line, 1650228753Smm const char *path, const char *contents) 1651228753Smm{ 1652228753Smm if (is_symlink(file, line, path, contents)) 1653228753Smm return (1); 1654228753Smm if (contents) 1655228753Smm failure_start(file, line, "File %s is not a symlink to %s", 1656228753Smm path, contents); 1657228753Smm else 1658228753Smm failure_start(file, line, "File %s is not a symlink", path); 1659228753Smm failure_finish(NULL); 1660228753Smm return (0); 1661228753Smm} 1662228753Smm 1663228753Smm 1664228753Smm/* Create a directory and report any errors. */ 1665228753Smmint 1666228753Smmassertion_make_dir(const char *file, int line, const char *dirname, int mode) 1667228753Smm{ 1668228753Smm assertion_count(file, line); 1669228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1670228753Smm (void)mode; /* UNUSED */ 1671228753Smm if (0 == _mkdir(dirname)) 1672228753Smm return (1); 1673228753Smm#else 1674306669Smm if (0 == mkdir(dirname, mode)) { 1675306669Smm if (0 == chmod(dirname, mode)) { 1676306669Smm assertion_file_mode(file, line, dirname, mode); 1677306669Smm return (1); 1678306669Smm } 1679306669Smm } 1680228753Smm#endif 1681228753Smm failure_start(file, line, "Could not create directory %s", dirname); 1682228753Smm failure_finish(NULL); 1683228753Smm return(0); 1684228753Smm} 1685228753Smm 1686228753Smm/* Create a file with the specified contents and report any failures. */ 1687228753Smmint 1688228753Smmassertion_make_file(const char *file, int line, 1689238825Smm const char *path, int mode, int csize, const void *contents) 1690228753Smm{ 1691228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1692228753Smm /* TODO: Rework this to set file mode as well. */ 1693228753Smm FILE *f; 1694228753Smm (void)mode; /* UNUSED */ 1695228753Smm assertion_count(file, line); 1696228753Smm f = fopen(path, "wb"); 1697228753Smm if (f == NULL) { 1698228753Smm failure_start(file, line, "Could not create file %s", path); 1699228753Smm failure_finish(NULL); 1700228753Smm return (0); 1701228753Smm } 1702228753Smm if (contents != NULL) { 1703238825Smm size_t wsize; 1704238825Smm 1705238825Smm if (csize < 0) 1706238825Smm wsize = strlen(contents); 1707238825Smm else 1708238825Smm wsize = (size_t)csize; 1709238825Smm if (wsize != fwrite(contents, 1, wsize, f)) { 1710228753Smm fclose(f); 1711228753Smm failure_start(file, line, 1712228753Smm "Could not write file %s", path); 1713228753Smm failure_finish(NULL); 1714228753Smm return (0); 1715228753Smm } 1716228753Smm } 1717228753Smm fclose(f); 1718228753Smm return (1); 1719228753Smm#else 1720228753Smm int fd; 1721228753Smm assertion_count(file, line); 1722228753Smm fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644); 1723228753Smm if (fd < 0) { 1724228753Smm failure_start(file, line, "Could not create %s", path); 1725228753Smm failure_finish(NULL); 1726228753Smm return (0); 1727228753Smm } 1728306669Smm if (0 != chmod(path, mode)) { 1729306669Smm failure_start(file, line, "Could not chmod %s", path); 1730306669Smm failure_finish(NULL); 1731307214Smm close(fd); 1732306669Smm return (0); 1733306669Smm } 1734228753Smm if (contents != NULL) { 1735238825Smm ssize_t wsize; 1736238825Smm 1737238825Smm if (csize < 0) 1738238825Smm wsize = (ssize_t)strlen(contents); 1739238825Smm else 1740238825Smm wsize = (ssize_t)csize; 1741238825Smm if (wsize != write(fd, contents, wsize)) { 1742228753Smm close(fd); 1743238825Smm failure_start(file, line, 1744238825Smm "Could not write to %s", path); 1745228753Smm failure_finish(NULL); 1746307214Smm close(fd); 1747228753Smm return (0); 1748228753Smm } 1749228753Smm } 1750228753Smm close(fd); 1751306669Smm assertion_file_mode(file, line, path, mode); 1752228753Smm return (1); 1753228753Smm#endif 1754228753Smm} 1755228753Smm 1756228753Smm/* Create a hardlink and report any failures. */ 1757228753Smmint 1758228753Smmassertion_make_hardlink(const char *file, int line, 1759228753Smm const char *newpath, const char *linkto) 1760228753Smm{ 1761228753Smm int succeeded; 1762228753Smm 1763228753Smm assertion_count(file, line); 1764228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1765228753Smm succeeded = my_CreateHardLinkA(newpath, linkto); 1766228753Smm#elif HAVE_LINK 1767228753Smm succeeded = !link(linkto, newpath); 1768228753Smm#else 1769228753Smm succeeded = 0; 1770228753Smm#endif 1771228753Smm if (succeeded) 1772228753Smm return (1); 1773228753Smm failure_start(file, line, "Could not create hardlink"); 1774228753Smm logprintf(" New link: %s\n", newpath); 1775228753Smm logprintf(" Old name: %s\n", linkto); 1776228753Smm failure_finish(NULL); 1777228753Smm return(0); 1778228753Smm} 1779228753Smm 1780228753Smm/* Create a symlink and report any failures. */ 1781228753Smmint 1782228753Smmassertion_make_symlink(const char *file, int line, 1783228753Smm const char *newpath, const char *linkto) 1784228753Smm{ 1785228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1786228753Smm int targetIsDir = 0; /* TODO: Fix this */ 1787228753Smm assertion_count(file, line); 1788228753Smm if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir)) 1789228753Smm return (1); 1790228753Smm#elif HAVE_SYMLINK 1791228753Smm assertion_count(file, line); 1792228753Smm if (0 == symlink(linkto, newpath)) 1793228753Smm return (1); 1794228753Smm#endif 1795228753Smm failure_start(file, line, "Could not create symlink"); 1796228753Smm logprintf(" New link: %s\n", newpath); 1797228753Smm logprintf(" Old name: %s\n", linkto); 1798228753Smm failure_finish(NULL); 1799228753Smm return(0); 1800228753Smm} 1801228753Smm 1802228753Smm/* Set umask, report failures. */ 1803228753Smmint 1804228753Smmassertion_umask(const char *file, int line, int mask) 1805228753Smm{ 1806228753Smm assertion_count(file, line); 1807228753Smm (void)file; /* UNUSED */ 1808228753Smm (void)line; /* UNUSED */ 1809228753Smm umask(mask); 1810228753Smm return (1); 1811228753Smm} 1812228753Smm 1813231200Smm/* Set times, report failures. */ 1814231200Smmint 1815231200Smmassertion_utimes(const char *file, int line, 1816231200Smm const char *pathname, long at, long at_nsec, long mt, long mt_nsec) 1817231200Smm{ 1818231200Smm int r; 1819231200Smm 1820231200Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1821231200Smm#define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\ 1822231200Smm + (((nsec)/1000)*10)) 1823231200Smm HANDLE h; 1824231200Smm ULARGE_INTEGER wintm; 1825231200Smm FILETIME fatime, fmtime; 1826231200Smm FILETIME *pat, *pmt; 1827231200Smm 1828231200Smm assertion_count(file, line); 1829231200Smm h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE, 1830231200Smm FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 1831231200Smm FILE_FLAG_BACKUP_SEMANTICS, NULL); 1832231200Smm if (h == INVALID_HANDLE_VALUE) { 1833231200Smm failure_start(file, line, "Can't access %s\n", pathname); 1834231200Smm failure_finish(NULL); 1835231200Smm return (0); 1836231200Smm } 1837231200Smm 1838231200Smm if (at > 0 || at_nsec > 0) { 1839231200Smm wintm.QuadPart = WINTIME(at, at_nsec); 1840231200Smm fatime.dwLowDateTime = wintm.LowPart; 1841231200Smm fatime.dwHighDateTime = wintm.HighPart; 1842231200Smm pat = &fatime; 1843231200Smm } else 1844231200Smm pat = NULL; 1845231200Smm if (mt > 0 || mt_nsec > 0) { 1846231200Smm wintm.QuadPart = WINTIME(mt, mt_nsec); 1847231200Smm fmtime.dwLowDateTime = wintm.LowPart; 1848231200Smm fmtime.dwHighDateTime = wintm.HighPart; 1849231200Smm pmt = &fmtime; 1850231200Smm } else 1851231200Smm pmt = NULL; 1852231200Smm if (pat != NULL || pmt != NULL) 1853231200Smm r = SetFileTime(h, NULL, pat, pmt); 1854231200Smm else 1855231200Smm r = 1; 1856231200Smm CloseHandle(h); 1857231200Smm if (r == 0) { 1858231200Smm failure_start(file, line, "Can't SetFileTime %s\n", pathname); 1859231200Smm failure_finish(NULL); 1860231200Smm return (0); 1861231200Smm } 1862231200Smm return (1); 1863231200Smm#else /* defined(_WIN32) && !defined(__CYGWIN__) */ 1864231200Smm struct stat st; 1865231200Smm struct timeval times[2]; 1866231200Smm 1867231200Smm#if !defined(__FreeBSD__) 1868231200Smm mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */ 1869231200Smm#endif 1870231200Smm if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0) 1871231200Smm return (1); 1872231200Smm 1873231200Smm r = lstat(pathname, &st); 1874231200Smm if (r < 0) { 1875231200Smm failure_start(file, line, "Can't stat %s\n", pathname); 1876231200Smm failure_finish(NULL); 1877231200Smm return (0); 1878231200Smm } 1879231200Smm 1880231200Smm if (mt == 0 && mt_nsec == 0) { 1881231200Smm mt = st.st_mtime; 1882231200Smm#if defined(__FreeBSD__) 1883231200Smm mt_nsec = st.st_mtimespec.tv_nsec; 1884231200Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1885231200Smm mt_nsec = (mt_nsec / 1000) * 1000; 1886231200Smm#endif 1887231200Smm } 1888231200Smm if (at == 0 && at_nsec == 0) { 1889231200Smm at = st.st_atime; 1890231200Smm#if defined(__FreeBSD__) 1891231200Smm at_nsec = st.st_atimespec.tv_nsec; 1892231200Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1893231200Smm at_nsec = (at_nsec / 1000) * 1000; 1894231200Smm#endif 1895231200Smm } 1896231200Smm 1897231200Smm times[1].tv_sec = mt; 1898231200Smm times[1].tv_usec = mt_nsec / 1000; 1899231200Smm 1900231200Smm times[0].tv_sec = at; 1901231200Smm times[0].tv_usec = at_nsec / 1000; 1902231200Smm 1903231200Smm#ifdef HAVE_LUTIMES 1904231200Smm r = lutimes(pathname, times); 1905231200Smm#else 1906231200Smm r = utimes(pathname, times); 1907231200Smm#endif 1908231200Smm if (r < 0) { 1909231200Smm failure_start(file, line, "Can't utimes %s\n", pathname); 1910231200Smm failure_finish(NULL); 1911231200Smm return (0); 1912231200Smm } 1913231200Smm return (1); 1914231200Smm#endif /* defined(_WIN32) && !defined(__CYGWIN__) */ 1915231200Smm} 1916231200Smm 1917314571Smm/* Compare file flags */ 1918314571Smmint 1919314571Smmassertion_compare_fflags(const char *file, int line, const char *patha, 1920314571Smm const char *pathb, int nomatch) 1921314571Smm{ 1922314571Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 1923314571Smm struct stat sa, sb; 1924314571Smm 1925314571Smm assertion_count(file, line); 1926314571Smm 1927314571Smm if (stat(patha, &sa) < 0) 1928314571Smm return (0); 1929314571Smm if (stat(pathb, &sb) < 0) 1930314571Smm return (0); 1931314571Smm if (!nomatch && sa.st_flags != sb.st_flags) { 1932314571Smm failure_start(file, line, "File flags should be identical: " 1933314571Smm "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb, 1934314571Smm sb.st_flags); 1935314571Smm failure_finish(NULL); 1936314571Smm return (0); 1937314571Smm } 1938314571Smm if (nomatch && sa.st_flags == sb.st_flags) { 1939314571Smm failure_start(file, line, "File flags should be different: " 1940314571Smm "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb, 1941314571Smm sb.st_flags); 1942314571Smm failure_finish(NULL); 1943314571Smm return (0); 1944314571Smm } 1945314571Smm#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \ 1946314571Smm defined(FS_NODUMP_FL)) || \ 1947314571Smm (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \ 1948314571Smm && defined(EXT2_NODUMP_FL)) 1949314571Smm int fd, r, flagsa, flagsb; 1950314571Smm 1951314571Smm assertion_count(file, line); 1952314571Smm fd = open(patha, O_RDONLY | O_NONBLOCK); 1953314571Smm if (fd < 0) { 1954314571Smm failure_start(file, line, "Can't open %s\n", patha); 1955314571Smm failure_finish(NULL); 1956314571Smm return (0); 1957314571Smm } 1958314571Smm r = ioctl(fd, 1959314571Smm#ifdef FS_IOC_GETFLAGS 1960314571Smm FS_IOC_GETFLAGS, 1961314571Smm#else 1962314571Smm EXT2_IOC_GETFLAGS, 1963314571Smm#endif 1964314571Smm &flagsa); 1965314571Smm close(fd); 1966314571Smm if (r < 0) { 1967314571Smm failure_start(file, line, "Can't get flags %s\n", patha); 1968314571Smm failure_finish(NULL); 1969314571Smm return (0); 1970314571Smm } 1971314571Smm fd = open(pathb, O_RDONLY | O_NONBLOCK); 1972314571Smm if (fd < 0) { 1973314571Smm failure_start(file, line, "Can't open %s\n", pathb); 1974314571Smm failure_finish(NULL); 1975314571Smm return (0); 1976314571Smm } 1977314571Smm r = ioctl(fd, 1978314571Smm#ifdef FS_IOC_GETFLAGS 1979314571Smm FS_IOC_GETFLAGS, 1980314571Smm#else 1981314571Smm EXT2_IOC_GETFLAGS, 1982314571Smm#endif 1983314571Smm &flagsb); 1984314571Smm close(fd); 1985314571Smm if (r < 0) { 1986314571Smm failure_start(file, line, "Can't get flags %s\n", pathb); 1987314571Smm failure_finish(NULL); 1988314571Smm return (0); 1989314571Smm } 1990314571Smm if (!nomatch && flagsa != flagsb) { 1991314571Smm failure_start(file, line, "File flags should be identical: " 1992314571Smm "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb); 1993314571Smm failure_finish(NULL); 1994314571Smm return (0); 1995314571Smm } 1996314571Smm if (nomatch && flagsa == flagsb) { 1997314571Smm failure_start(file, line, "File flags should be different: " 1998314571Smm "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb); 1999314571Smm failure_finish(NULL); 2000314571Smm return (0); 2001314571Smm } 2002314571Smm#else 2003314571Smm (void)patha; /* UNUSED */ 2004314571Smm (void)pathb; /* UNUSED */ 2005314571Smm (void)nomatch; /* UNUSED */ 2006314571Smm assertion_count(file, line); 2007314571Smm#endif 2008314571Smm return (1); 2009314571Smm} 2010314571Smm 2011238825Smm/* Set nodump, report failures. */ 2012238825Smmint 2013314571Smmassertion_set_nodump(const char *file, int line, const char *pathname) 2014238825Smm{ 2015238825Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2016238825Smm int r; 2017238825Smm 2018238825Smm assertion_count(file, line); 2019238825Smm r = chflags(pathname, UF_NODUMP); 2020238825Smm if (r < 0) { 2021238825Smm failure_start(file, line, "Can't set nodump %s\n", pathname); 2022238825Smm failure_finish(NULL); 2023238825Smm return (0); 2024238825Smm } 2025314565Smm#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \ 2026314565Smm defined(FS_NODUMP_FL)) || \ 2027314565Smm (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \ 2028314565Smm && defined(EXT2_NODUMP_FL)) 2029238825Smm int fd, r, flags; 2030238825Smm 2031238825Smm assertion_count(file, line); 2032238825Smm fd = open(pathname, O_RDONLY | O_NONBLOCK); 2033238825Smm if (fd < 0) { 2034238825Smm failure_start(file, line, "Can't open %s\n", pathname); 2035238825Smm failure_finish(NULL); 2036238825Smm return (0); 2037238825Smm } 2038314565Smm r = ioctl(fd, 2039314565Smm#ifdef FS_IOC_GETFLAGS 2040314565Smm FS_IOC_GETFLAGS, 2041314565Smm#else 2042314565Smm EXT2_IOC_GETFLAGS, 2043314565Smm#endif 2044314565Smm &flags); 2045238825Smm if (r < 0) { 2046238825Smm failure_start(file, line, "Can't get flags %s\n", pathname); 2047238825Smm failure_finish(NULL); 2048238825Smm return (0); 2049238825Smm } 2050314565Smm#ifdef FS_NODUMP_FL 2051314565Smm flags |= FS_NODUMP_FL; 2052314565Smm#else 2053238825Smm flags |= EXT2_NODUMP_FL; 2054314565Smm#endif 2055314565Smm 2056314565Smm r = ioctl(fd, 2057314565Smm#ifdef FS_IOC_SETFLAGS 2058314565Smm FS_IOC_SETFLAGS, 2059314565Smm#else 2060314565Smm EXT2_IOC_SETFLAGS, 2061314565Smm#endif 2062314565Smm &flags); 2063238825Smm if (r < 0) { 2064238825Smm failure_start(file, line, "Can't set nodump %s\n", pathname); 2065238825Smm failure_finish(NULL); 2066238825Smm return (0); 2067238825Smm } 2068238825Smm close(fd); 2069238825Smm#else 2070238825Smm (void)pathname; /* UNUSED */ 2071238825Smm assertion_count(file, line); 2072238825Smm#endif 2073238825Smm return (1); 2074238825Smm} 2075238825Smm 2076314565Smm#ifdef PROGRAM 2077314565Smmstatic void assert_version_id(char **qq, size_t *ss) 2078314565Smm{ 2079314565Smm char *q = *qq; 2080314565Smm size_t s = *ss; 2081314565Smm 2082314565Smm /* Version number is a series of digits and periods. */ 2083314565Smm while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) { 2084314565Smm ++q; 2085314565Smm --s; 2086314565Smm } 2087314565Smm 2088314565Smm if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') { 2089314565Smm q += 3; 2090314565Smm s -= 3; 2091314565Smm } 2092314565Smm 2093314565Smm /* Skip a single trailing a,b,c, or d. */ 2094314565Smm if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd') 2095314565Smm ++q; 2096314565Smm 2097314565Smm /* Version number terminated by space. */ 2098314565Smm failure("No space after version: ``%s''", q); 2099314565Smm assert(s > 1); 2100314565Smm failure("No space after version: ``%s''", q); 2101314565Smm assert(*q == ' '); 2102314565Smm 2103314565Smm ++q; --s; 2104314565Smm 2105314565Smm *qq = q; 2106314565Smm *ss = s; 2107314565Smm} 2108314565Smm 2109314565Smm 2110228753Smm/* 2111314565Smm * Check program version 2112314565Smm */ 2113314565Smmvoid assertVersion(const char *prog, const char *base) 2114314565Smm{ 2115314565Smm int r; 2116314565Smm char *p, *q; 2117314565Smm size_t s; 2118314565Smm unsigned int prog_len = strlen(base); 2119314565Smm 2120314565Smm r = systemf("%s --version >version.stdout 2>version.stderr", prog); 2121314565Smm if (r != 0) 2122314565Smm r = systemf("%s -W version >version.stdout 2>version.stderr", 2123314565Smm prog); 2124314565Smm 2125314565Smm failure("Unable to run either %s --version or %s -W version", 2126314565Smm prog, prog); 2127314565Smm if (!assert(r == 0)) 2128314565Smm return; 2129314565Smm 2130314565Smm /* --version should generate nothing to stdout. */ 2131314565Smm assertEmptyFile("version.stderr"); 2132314565Smm 2133314565Smm /* Verify format of version message. */ 2134314565Smm q = p = slurpfile(&s, "version.stdout"); 2135314565Smm 2136314565Smm /* Version message should start with name of program, then space. */ 2137314565Smm assert(s > prog_len + 1); 2138314565Smm 2139314565Smm failure("Version must start with '%s': ``%s''", base, p); 2140314565Smm if (!assertEqualMem(q, base, prog_len)) { 2141314565Smm free(p); 2142314565Smm return; 2143314565Smm } 2144314565Smm 2145314565Smm q += prog_len; s -= prog_len; 2146314565Smm 2147314565Smm assert(*q == ' '); 2148314565Smm q++; s--; 2149314565Smm 2150314565Smm assert_version_id(&q, &s); 2151314565Smm 2152314565Smm /* Separator. */ 2153314565Smm failure("No `-' between program name and versions: ``%s''", p); 2154314565Smm assertEqualMem(q, "- ", 2); 2155314565Smm q += 2; s -= 2; 2156314565Smm 2157314565Smm failure("Not long enough for libarchive version: ``%s''", p); 2158314565Smm assert(s > 11); 2159314565Smm 2160314565Smm failure("Libarchive version must start with `libarchive': ``%s''", p); 2161314565Smm assertEqualMem(q, "libarchive ", 11); 2162314565Smm 2163314565Smm q += 11; s -= 11; 2164314565Smm 2165314565Smm assert_version_id(&q, &s); 2166314565Smm 2167314565Smm /* Skip arbitrary third-party version numbers. */ 2168314565Smm while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' || 2169314565Smm isalnum(*q))) { 2170314565Smm ++q; 2171314565Smm --s; 2172314565Smm } 2173314565Smm 2174314565Smm /* All terminated by end-of-line. */ 2175314565Smm assert(s >= 1); 2176314565Smm 2177314565Smm /* Skip an optional CR character (e.g., Windows) */ 2178314565Smm failure("Version output must end with \\n or \\r\\n"); 2179314565Smm 2180314565Smm if (*q == '\r') { ++q; --s; } 2181314565Smm assertEqualMem(q, "\n", 1); 2182314565Smm 2183314565Smm free(p); 2184314565Smm} 2185314565Smm#endif /* PROGRAM */ 2186314565Smm 2187314565Smm/* 2188228753Smm * 2189228753Smm * UTILITIES for use by tests. 2190228753Smm * 2191228753Smm */ 2192228753Smm 2193228753Smm/* 2194228753Smm * Check whether platform supports symlinks. This is intended 2195228753Smm * for tests to use in deciding whether to bother testing symlink 2196228753Smm * support; if the platform doesn't support symlinks, there's no point 2197228753Smm * in checking whether the program being tested can create them. 2198228753Smm * 2199228753Smm * Note that the first time this test is called, we actually go out to 2200228753Smm * disk to create and verify a symlink. This is necessary because 2201228753Smm * symlink support is actually a property of a particular filesystem 2202228753Smm * and can thus vary between directories on a single system. After 2203228753Smm * the first call, this returns the cached result from memory, so it's 2204228753Smm * safe to call it as often as you wish. 2205228753Smm */ 2206228753Smmint 2207228753SmmcanSymlink(void) 2208228753Smm{ 2209228753Smm /* Remember the test result */ 2210228753Smm static int value = 0, tested = 0; 2211228753Smm if (tested) 2212228753Smm return (value); 2213228753Smm 2214228753Smm ++tested; 2215238825Smm assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a"); 2216228753Smm /* Note: Cygwin has its own symlink() emulation that does not 2217228753Smm * use the Win32 CreateSymbolicLink() function. */ 2218228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2219228753Smm value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0) 2220228753Smm && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0"); 2221228753Smm#elif HAVE_SYMLINK 2222228753Smm value = (0 == symlink("canSymlink.0", "canSymlink.1")) 2223228753Smm && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0"); 2224228753Smm#endif 2225228753Smm return (value); 2226228753Smm} 2227228753Smm 2228228753Smm/* Platform-dependent options for hiding the output of a subcommand. */ 2229228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2230228753Smmstatic const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */ 2231228753Smm#else 2232228753Smmstatic const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */ 2233228753Smm#endif 2234248590Smm/* 2235248590Smm * Can this platform run the bzip2 program? 2236248590Smm */ 2237228753Smmint 2238248590SmmcanBzip2(void) 2239248590Smm{ 2240248590Smm static int tested = 0, value = 0; 2241248590Smm if (!tested) { 2242248590Smm tested = 1; 2243248590Smm if (systemf("bzip2 -d -V %s", redirectArgs) == 0) 2244248590Smm value = 1; 2245248590Smm } 2246248590Smm return (value); 2247248590Smm} 2248248590Smm 2249248590Smm/* 2250248590Smm * Can this platform run the grzip program? 2251248590Smm */ 2252248590Smmint 2253248590SmmcanGrzip(void) 2254248590Smm{ 2255248590Smm static int tested = 0, value = 0; 2256248590Smm if (!tested) { 2257248590Smm tested = 1; 2258248590Smm if (systemf("grzip -V %s", redirectArgs) == 0) 2259248590Smm value = 1; 2260248590Smm } 2261248590Smm return (value); 2262248590Smm} 2263248590Smm 2264248590Smm/* 2265248590Smm * Can this platform run the gzip program? 2266248590Smm */ 2267248590Smmint 2268228753SmmcanGzip(void) 2269228753Smm{ 2270228753Smm static int tested = 0, value = 0; 2271228753Smm if (!tested) { 2272228753Smm tested = 1; 2273228753Smm if (systemf("gzip -V %s", redirectArgs) == 0) 2274228753Smm value = 1; 2275228753Smm } 2276228753Smm return (value); 2277228753Smm} 2278228753Smm 2279228753Smm/* 2280248590Smm * Can this platform run the lrzip program? 2281228753Smm */ 2282228753Smmint 2283248590SmmcanRunCommand(const char *cmd) 2284228753Smm{ 2285248590Smm static int tested = 0, value = 0; 2286248590Smm if (!tested) { 2287248590Smm tested = 1; 2288248590Smm if (systemf("%s %s", cmd, redirectArgs) == 0) 2289248590Smm value = 1; 2290248590Smm } 2291248590Smm return (value); 2292248590Smm} 2293248590Smm 2294248590Smmint 2295248590SmmcanLrzip(void) 2296248590Smm{ 2297228753Smm static int tested = 0, value = 0; 2298228753Smm if (!tested) { 2299228753Smm tested = 1; 2300248590Smm if (systemf("lrzip -V %s", redirectArgs) == 0) 2301228753Smm value = 1; 2302228753Smm } 2303228753Smm return (value); 2304228753Smm} 2305228753Smm 2306228753Smm/* 2307299425Smm * Can this platform run the lz4 program? 2308299425Smm */ 2309299425Smmint 2310299425SmmcanLz4(void) 2311299425Smm{ 2312299425Smm static int tested = 0, value = 0; 2313299425Smm if (!tested) { 2314299425Smm tested = 1; 2315299425Smm if (systemf("lz4 -V %s", redirectArgs) == 0) 2316299425Smm value = 1; 2317299425Smm } 2318299425Smm return (value); 2319299425Smm} 2320299425Smm 2321299425Smm/* 2322324418Smm * Can this platform run the zstd program? 2323324418Smm */ 2324324418Smmint 2325324418SmmcanZstd(void) 2326324418Smm{ 2327324418Smm static int tested = 0, value = 0; 2328324418Smm if (!tested) { 2329324418Smm tested = 1; 2330324418Smm if (systemf("zstd -V %s", redirectArgs) == 0) 2331324418Smm value = 1; 2332324418Smm } 2333324418Smm return (value); 2334324418Smm} 2335324418Smm 2336324418Smm/* 2337248590Smm * Can this platform run the lzip program? 2338248590Smm */ 2339248590Smmint 2340248590SmmcanLzip(void) 2341248590Smm{ 2342248590Smm static int tested = 0, value = 0; 2343248590Smm if (!tested) { 2344248590Smm tested = 1; 2345248590Smm if (systemf("lzip -V %s", redirectArgs) == 0) 2346248590Smm value = 1; 2347248590Smm } 2348248590Smm return (value); 2349248590Smm} 2350248590Smm 2351248590Smm/* 2352248590Smm * Can this platform run the lzma program? 2353248590Smm */ 2354248590Smmint 2355248590SmmcanLzma(void) 2356248590Smm{ 2357248590Smm static int tested = 0, value = 0; 2358248590Smm if (!tested) { 2359248590Smm tested = 1; 2360248590Smm if (systemf("lzma -V %s", redirectArgs) == 0) 2361248590Smm value = 1; 2362248590Smm } 2363248590Smm return (value); 2364248590Smm} 2365248590Smm 2366248590Smm/* 2367248590Smm * Can this platform run the lzop program? 2368248590Smm */ 2369248590Smmint 2370248590SmmcanLzop(void) 2371248590Smm{ 2372248590Smm static int tested = 0, value = 0; 2373248590Smm if (!tested) { 2374248590Smm tested = 1; 2375248590Smm if (systemf("lzop -V %s", redirectArgs) == 0) 2376248590Smm value = 1; 2377248590Smm } 2378248590Smm return (value); 2379248590Smm} 2380248590Smm 2381248590Smm/* 2382248590Smm * Can this platform run the xz program? 2383248590Smm */ 2384248590Smmint 2385248590SmmcanXz(void) 2386248590Smm{ 2387248590Smm static int tested = 0, value = 0; 2388248590Smm if (!tested) { 2389248590Smm tested = 1; 2390248590Smm if (systemf("xz -V %s", redirectArgs) == 0) 2391248590Smm value = 1; 2392248590Smm } 2393248590Smm return (value); 2394248590Smm} 2395248590Smm 2396248590Smm/* 2397238825Smm * Can this filesystem handle nodump flags. 2398238825Smm */ 2399238825Smmint 2400238825SmmcanNodump(void) 2401238825Smm{ 2402314571Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2403238825Smm const char *path = "cannodumptest"; 2404238825Smm struct stat sb; 2405238825Smm 2406238825Smm assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2407238825Smm if (chflags(path, UF_NODUMP) < 0) 2408238825Smm return (0); 2409238825Smm if (stat(path, &sb) < 0) 2410238825Smm return (0); 2411238825Smm if (sb.st_flags & UF_NODUMP) 2412238825Smm return (1); 2413314565Smm#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \ 2414314565Smm && defined(FS_NODUMP_FL)) || \ 2415314565Smm (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \ 2416314565Smm && defined(EXT2_NODUMP_FL)) 2417238825Smm const char *path = "cannodumptest"; 2418238825Smm int fd, r, flags; 2419238825Smm 2420238825Smm assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2421238825Smm fd = open(path, O_RDONLY | O_NONBLOCK); 2422238825Smm if (fd < 0) 2423238825Smm return (0); 2424314565Smm r = ioctl(fd, 2425314565Smm#ifdef FS_IOC_GETFLAGS 2426314565Smm FS_IOC_GETFLAGS, 2427314565Smm#else 2428314565Smm EXT2_IOC_GETFLAGS, 2429314565Smm#endif 2430314565Smm &flags); 2431238825Smm if (r < 0) 2432238825Smm return (0); 2433314565Smm#ifdef FS_NODUMP_FL 2434314565Smm flags |= FS_NODUMP_FL; 2435314565Smm#else 2436238825Smm flags |= EXT2_NODUMP_FL; 2437314565Smm#endif 2438314565Smm r = ioctl(fd, 2439314565Smm#ifdef FS_IOC_SETFLAGS 2440314565Smm FS_IOC_SETFLAGS, 2441314565Smm#else 2442314565Smm EXT2_IOC_SETFLAGS, 2443314565Smm#endif 2444314565Smm &flags); 2445238825Smm if (r < 0) 2446238825Smm return (0); 2447238825Smm close(fd); 2448238825Smm fd = open(path, O_RDONLY | O_NONBLOCK); 2449238825Smm if (fd < 0) 2450238825Smm return (0); 2451314565Smm r = ioctl(fd, 2452314565Smm#ifdef FS_IOC_GETFLAGS 2453314565Smm FS_IOC_GETFLAGS, 2454314565Smm#else 2455314565Smm EXT2_IOC_GETFLAGS, 2456314565Smm#endif 2457314565Smm &flags); 2458238825Smm if (r < 0) 2459238825Smm return (0); 2460238825Smm close(fd); 2461314565Smm#ifdef FS_NODUMP_FL 2462314565Smm if (flags & FS_NODUMP_FL) 2463314565Smm#else 2464238825Smm if (flags & EXT2_NODUMP_FL) 2465314565Smm#endif 2466238825Smm return (1); 2467314571Smm#endif 2468238825Smm return (0); 2469238825Smm} 2470238825Smm 2471316338Smm/* Get extended attribute from a path */ 2472316338Smmconst void * 2473316338SmmgetXattr(const char *path, const char *name, size_t *sizep) 2474316338Smm{ 2475316338Smm void *value = NULL; 2476316338Smm#if ARCHIVE_XATTR_SUPPORT 2477316338Smm ssize_t size; 2478316338Smm#if ARCHIVE_XATTR_LINUX 2479316338Smm size = lgetxattr(path, name, NULL, 0); 2480316338Smm#elif ARCHIVE_XATTR_DARWIN 2481316338Smm size = getxattr(path, name, NULL, 0, 0, XATTR_NOFOLLOW); 2482316338Smm#elif ARCHIVE_XATTR_AIX 2483316338Smm size = lgetea(path, name, NULL, 0); 2484316338Smm#elif ARCHIVE_XATTR_FREEBSD 2485316338Smm size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5, 2486316338Smm NULL, 0); 2487316338Smm#endif 2488316338Smm 2489316338Smm if (size >= 0) { 2490316338Smm value = malloc(size); 2491316338Smm#if ARCHIVE_XATTR_LINUX 2492316338Smm size = lgetxattr(path, name, value, size); 2493316338Smm#elif ARCHIVE_XATTR_DARWIN 2494316338Smm size = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW); 2495316338Smm#elif ARCHIVE_XATTR_AIX 2496316338Smm size = lgetea(path, name, value, size); 2497316338Smm#elif ARCHIVE_XATTR_FREEBSD 2498316338Smm size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5, 2499316338Smm value, size); 2500316338Smm#endif 2501316338Smm if (size < 0) { 2502316338Smm free(value); 2503316338Smm value = NULL; 2504316338Smm } 2505316338Smm } 2506316338Smm if (size < 0) 2507316338Smm *sizep = 0; 2508316338Smm else 2509316338Smm *sizep = (size_t)size; 2510316338Smm#else /* !ARCHIVE_XATTR_SUPPORT */ 2511316338Smm (void)path; /* UNUSED */ 2512316338Smm (void)name; /* UNUSED */ 2513316338Smm *sizep = 0; 2514316338Smm#endif /* !ARCHIVE_XATTR_SUPPORT */ 2515316338Smm return (value); 2516316338Smm} 2517316338Smm 2518316338Smm/* 2519316338Smm * Set extended attribute on a path 2520316338Smm * Returns 0 on error, 1 on success 2521316338Smm */ 2522316338Smmint 2523316338SmmsetXattr(const char *path, const char *name, const void *value, size_t size) 2524316338Smm{ 2525316338Smm#if ARCHIVE_XATTR_SUPPORT 2526316338Smm#if ARCHIVE_XATTR_LINUX 2527316338Smm if (lsetxattr(path, name, value, size, 0) == 0) 2528316338Smm#elif ARCHIVE_XATTR_DARWIN 2529316338Smm if (setxattr(path, name, value, size, 0, XATTR_NOFOLLOW) == 0) 2530316338Smm#elif ARCHIVE_XATTR_AIX 2531316338Smm if (lsetea(path, name, value, size, 0) == 0) 2532316338Smm#elif ARCHIVE_XATTR_FREEBSD 2533316338Smm if (extattr_set_link(path, EXTATTR_NAMESPACE_USER, name + 5, value, 2534316338Smm size) > -1) 2535316338Smm#else 2536316338Smm if (0) 2537316338Smm#endif 2538316338Smm return (1); 2539316338Smm#else /* !ARCHIVE_XATTR_SUPPORT */ 2540316338Smm (void)path; /* UNUSED */ 2541316338Smm (void)name; /* UNUSED */ 2542316338Smm (void)value; /* UNUSED */ 2543316338Smm (void)size; /* UNUSED */ 2544316338Smm#endif /* !ARCHIVE_XATTR_SUPPORT */ 2545316338Smm return (0); 2546316338Smm} 2547316338Smm 2548316338Smm#if ARCHIVE_ACL_SUNOS 2549314571Smm/* Fetch ACLs on Solaris using acl() or facl() */ 2550314571Smmvoid * 2551314571Smmsunacl_get(int cmd, int *aclcnt, int fd, const char *path) 2552314571Smm{ 2553314571Smm int cnt, cntcmd; 2554314571Smm size_t size; 2555314571Smm void *aclp; 2556238825Smm 2557314571Smm if (cmd == GETACL) { 2558314571Smm cntcmd = GETACLCNT; 2559314571Smm size = sizeof(aclent_t); 2560314571Smm } 2561316338Smm#if ARCHIVE_ACL_SUNOS_NFS4 2562314571Smm else if (cmd == ACE_GETACL) { 2563314571Smm cntcmd = ACE_GETACLCNT; 2564314571Smm size = sizeof(ace_t); 2565314571Smm } 2566314571Smm#endif 2567314571Smm else { 2568314571Smm errno = EINVAL; 2569314571Smm *aclcnt = -1; 2570314571Smm return (NULL); 2571314571Smm } 2572314571Smm 2573314571Smm aclp = NULL; 2574314571Smm cnt = -2; 2575314571Smm while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) { 2576314571Smm if (path != NULL) 2577314571Smm cnt = acl(path, cntcmd, 0, NULL); 2578314571Smm else 2579314571Smm cnt = facl(fd, cntcmd, 0, NULL); 2580314571Smm 2581314571Smm if (cnt > 0) { 2582314571Smm if (aclp == NULL) 2583314571Smm aclp = malloc(cnt * size); 2584314571Smm else 2585314571Smm aclp = realloc(NULL, cnt * size); 2586314571Smm if (aclp != NULL) { 2587314571Smm if (path != NULL) 2588314571Smm cnt = acl(path, cmd, cnt, aclp); 2589314571Smm else 2590314571Smm cnt = facl(fd, cmd, cnt, aclp); 2591314571Smm } 2592314571Smm } else { 2593314571Smm if (aclp != NULL) { 2594314571Smm free(aclp); 2595314571Smm aclp = NULL; 2596314571Smm } 2597314571Smm break; 2598314571Smm } 2599314571Smm } 2600314571Smm 2601314571Smm *aclcnt = cnt; 2602314571Smm return (aclp); 2603314571Smm} 2604316338Smm#endif /* ARCHIVE_ACL_SUNOS */ 2605314571Smm 2606314571Smm/* 2607314571Smm * Set test ACLs on a path 2608314571Smm * Return values: 2609314571Smm * 0: error setting ACLs 2610314571Smm * ARCHIVE_TEST_ACL_TYPE_POSIX1E: POSIX.1E ACLs have been set 2611314571Smm * ARCHIVE_TEST_ACL_TYPE_NFS4: NFSv4 or extended ACLs have been set 2612314571Smm */ 2613238825Smmint 2614314571SmmsetTestAcl(const char *path) 2615238825Smm{ 2616316338Smm#if ARCHIVE_ACL_SUPPORT 2617314571Smm int r = 1; 2618316338Smm#if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_DARWIN 2619314571Smm acl_t acl; 2620314571Smm#endif 2621316338Smm#if ARCHIVE_ACL_LIBRICHACL 2622316338Smm struct richacl *richacl; 2623316338Smm#endif 2624316338Smm#if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD 2625314571Smm const char *acltext_posix1e = "user:1:rw-," 2626314571Smm "group:15:r-x," 2627314571Smm "user::rwx," 2628314571Smm "group::rwx," 2629314571Smm "other::r-x," 2630314571Smm "mask::rwx"; 2631316338Smm#elif ARCHIVE_ACL_SUNOS /* Solaris POSIX.1e */ 2632314571Smm aclent_t aclp_posix1e[] = { 2633314571Smm { USER_OBJ, -1, 4 | 2 | 1 }, 2634314571Smm { USER, 1, 4 | 2 }, 2635314571Smm { GROUP_OBJ, -1, 4 | 2 | 1 }, 2636314571Smm { GROUP, 15, 4 | 1 }, 2637314571Smm { CLASS_OBJ, -1, 4 | 2 | 1 }, 2638314571Smm { OTHER_OBJ, -1, 4 | 2 | 1 } 2639314571Smm }; 2640314571Smm#endif 2641316338Smm#if ARCHIVE_ACL_FREEBSD /* FreeBSD NFS4 */ 2642314571Smm const char *acltext_nfs4 = "user:1:rwpaRcs::allow:1," 2643314571Smm "group:15:rxaRcs::allow:15," 2644314571Smm "owner@:rwpxaARWcCos::allow," 2645314571Smm "group@:rwpxaRcs::allow," 2646314571Smm "everyone@:rxaRcs::allow"; 2647316338Smm#elif ARCHIVE_ACL_LIBRICHACL 2648316338Smm const char *acltext_nfs4 = "owner:rwpxaARWcCoS::mask," 2649316338Smm "group:rwpxaRcS::mask," 2650316338Smm "other:rxaRcS::mask," 2651316338Smm "user:1:rwpaRcS::allow," 2652316338Smm "group:15:rxaRcS::allow," 2653316338Smm "owner@:rwpxaARWcCoS::allow," 2654316338Smm "group@:rwpxaRcS::allow," 2655316338Smm "everyone@:rxaRcS::allow"; 2656316338Smm#elif ARCHIVE_ACL_SUNOS_NFS4 /* Solaris NFS4 */ 2657314571Smm ace_t aclp_nfs4[] = { 2658314571Smm { 1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA | 2659314571Smm ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | ACE_READ_ACL | 2660314571Smm ACE_SYNCHRONIZE, 0, ACE_ACCESS_ALLOWED_ACE_TYPE }, 2661314571Smm { 15, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES | 2662314571Smm ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE, 2663314571Smm ACE_IDENTIFIER_GROUP, ACE_ACCESS_ALLOWED_ACE_TYPE }, 2664314571Smm { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA | 2665314571Smm ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_WRITE_ATTRIBUTES | 2666314571Smm ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS | 2667314571Smm ACE_READ_ACL | ACE_WRITE_ACL | ACE_WRITE_OWNER | ACE_SYNCHRONIZE, 2668314571Smm ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE }, 2669314571Smm { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA | 2670314571Smm ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | 2671314571Smm ACE_READ_ACL | ACE_SYNCHRONIZE, ACE_GROUP | ACE_IDENTIFIER_GROUP, 2672314571Smm ACE_ACCESS_ALLOWED_ACE_TYPE }, 2673314571Smm { -1, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES | 2674314571Smm ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE, 2675314571Smm ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE } 2676314571Smm }; 2677316338Smm#elif ARCHIVE_ACL_DARWIN /* Mac OS X */ 2678314571Smm acl_entry_t aclent; 2679314571Smm acl_permset_t permset; 2680314571Smm const uid_t uid = 1; 2681314571Smm uuid_t uuid; 2682314571Smm int i; 2683314571Smm const acl_perm_t acl_perms[] = { 2684314571Smm ACL_READ_DATA, 2685314571Smm ACL_WRITE_DATA, 2686314571Smm ACL_APPEND_DATA, 2687314571Smm ACL_EXECUTE, 2688314571Smm ACL_READ_ATTRIBUTES, 2689314571Smm ACL_READ_EXTATTRIBUTES, 2690314571Smm ACL_READ_SECURITY, 2691314571Smm#if HAVE_DECL_ACL_SYNCHRONIZE 2692314571Smm ACL_SYNCHRONIZE 2693314571Smm#endif 2694314571Smm }; 2695316338Smm#endif /* ARCHIVE_ACL_DARWIN */ 2696314571Smm 2697316338Smm#if ARCHIVE_ACL_FREEBSD 2698314571Smm acl = acl_from_text(acltext_nfs4); 2699314571Smm failure("acl_from_text() error: %s", strerror(errno)); 2700314571Smm if (assert(acl != NULL) == 0) 2701314571Smm return (0); 2702316338Smm#elif ARCHIVE_ACL_LIBRICHACL 2703316338Smm richacl = richacl_from_text(acltext_nfs4, NULL, NULL); 2704316338Smm failure("richacl_from_text() error: %s", strerror(errno)); 2705316338Smm if (assert(richacl != NULL) == 0) 2706316338Smm return (0); 2707316338Smm#elif ARCHIVE_ACL_DARWIN 2708314571Smm acl = acl_init(1); 2709314571Smm failure("acl_init() error: %s", strerror(errno)); 2710314571Smm if (assert(acl != NULL) == 0) 2711314571Smm return (0); 2712314571Smm r = acl_create_entry(&acl, &aclent); 2713314571Smm failure("acl_create_entry() error: %s", strerror(errno)); 2714314571Smm if (assertEqualInt(r, 0) == 0) 2715314571Smm goto testacl_free; 2716314571Smm r = acl_set_tag_type(aclent, ACL_EXTENDED_ALLOW); 2717314571Smm failure("acl_set_tag_type() error: %s", strerror(errno)); 2718314571Smm if (assertEqualInt(r, 0) == 0) 2719314571Smm goto testacl_free; 2720314571Smm r = acl_get_permset(aclent, &permset); 2721314571Smm failure("acl_get_permset() error: %s", strerror(errno)); 2722314571Smm if (assertEqualInt(r, 0) == 0) 2723314571Smm goto testacl_free; 2724314571Smm for (i = 0; i < (int)(sizeof(acl_perms) / sizeof(acl_perms[0])); i++) { 2725314571Smm r = acl_add_perm(permset, acl_perms[i]); 2726314571Smm failure("acl_add_perm() error: %s", strerror(errno)); 2727314571Smm if (assertEqualInt(r, 0) == 0) 2728314571Smm goto testacl_free; 2729314571Smm } 2730314571Smm r = acl_set_permset(aclent, permset); 2731314571Smm failure("acl_set_permset() error: %s", strerror(errno)); 2732314571Smm if (assertEqualInt(r, 0) == 0) 2733314571Smm goto testacl_free; 2734316338Smm r = mbr_uid_to_uuid(uid, uuid); 2735316338Smm failure("mbr_uid_to_uuid() error: %s", strerror(errno)); 2736314571Smm if (assertEqualInt(r, 0) == 0) 2737314571Smm goto testacl_free; 2738314571Smm r = acl_set_qualifier(aclent, uuid); 2739314571Smm failure("acl_set_qualifier() error: %s", strerror(errno)); 2740314571Smm if (assertEqualInt(r, 0) == 0) 2741314571Smm goto testacl_free; 2742316338Smm#endif /* ARCHIVE_ACL_DARWIN */ 2743314571Smm 2744316338Smm#if ARCHIVE_ACL_NFS4 2745316338Smm#if ARCHIVE_ACL_FREEBSD 2746314571Smm r = acl_set_file(path, ACL_TYPE_NFS4, acl); 2747314571Smm acl_free(acl); 2748316338Smm#elif ARCHIVE_ACL_LIBRICHACL 2749316338Smm r = richacl_set_file(path, richacl); 2750316338Smm richacl_free(richacl); 2751316338Smm#elif ARCHIVE_ACL_SUNOS_NFS4 2752314571Smm r = acl(path, ACE_SETACL, 2753314571Smm (int)(sizeof(aclp_nfs4)/sizeof(aclp_nfs4[0])), aclp_nfs4); 2754316338Smm#elif ARCHIVE_ACL_DARWIN 2755314571Smm r = acl_set_file(path, ACL_TYPE_EXTENDED, acl); 2756314571Smm acl_free(acl); 2757314571Smm#endif 2758314571Smm if (r == 0) 2759314571Smm return (ARCHIVE_TEST_ACL_TYPE_NFS4); 2760316338Smm#endif /* ARCHIVE_ACL_NFS4 */ 2761314571Smm 2762316338Smm#if ARCHIVE_ACL_POSIX1E 2763316338Smm#if ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_LIBACL 2764314571Smm acl = acl_from_text(acltext_posix1e); 2765314571Smm failure("acl_from_text() error: %s", strerror(errno)); 2766314571Smm if (assert(acl != NULL) == 0) 2767314571Smm return (0); 2768314571Smm 2769314571Smm r = acl_set_file(path, ACL_TYPE_ACCESS, acl); 2770314571Smm acl_free(acl); 2771316338Smm#elif ARCHIVE_ACL_SUNOS 2772314571Smm r = acl(path, SETACL, 2773314571Smm (int)(sizeof(aclp_posix1e)/sizeof(aclp_posix1e[0])), aclp_posix1e); 2774314571Smm#endif 2775314571Smm if (r == 0) 2776314571Smm return (ARCHIVE_TEST_ACL_TYPE_POSIX1E); 2777314571Smm else 2778314571Smm return (0); 2779316338Smm#endif /* ARCHIVE_ACL_POSIX1E */ 2780316338Smm#if ARCHIVE_ACL_DARWIN 2781314571Smmtestacl_free: 2782314571Smm acl_free(acl); 2783314571Smm#endif 2784316338Smm#endif /* ARCHIVE_ACL_SUPPORT */ 2785314571Smm (void)path; /* UNUSED */ 2786238825Smm return (0); 2787238825Smm} 2788238825Smm 2789238825Smm/* 2790228753Smm * Sleep as needed; useful for verifying disk timestamp changes by 2791228753Smm * ensuring that the wall-clock time has actually changed before we 2792228753Smm * go back to re-read something from disk. 2793228753Smm */ 2794228753Smmvoid 2795228753SmmsleepUntilAfter(time_t t) 2796228753Smm{ 2797228753Smm while (t >= time(NULL)) 2798228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2799228753Smm Sleep(500); 2800228753Smm#else 2801228753Smm sleep(1); 2802228753Smm#endif 2803228753Smm} 2804228753Smm 2805228753Smm/* 2806228753Smm * Call standard system() call, but build up the command line using 2807228753Smm * sprintf() conventions. 2808228753Smm */ 2809228753Smmint 2810228753Smmsystemf(const char *fmt, ...) 2811228753Smm{ 2812228753Smm char buff[8192]; 2813228753Smm va_list ap; 2814228753Smm int r; 2815228753Smm 2816228753Smm va_start(ap, fmt); 2817228753Smm vsprintf(buff, fmt, ap); 2818228753Smm if (verbosity > VERBOSITY_FULL) 2819228753Smm logprintf("Cmd: %s\n", buff); 2820228753Smm r = system(buff); 2821228753Smm va_end(ap); 2822228753Smm return (r); 2823228753Smm} 2824228753Smm 2825228753Smm/* 2826228753Smm * Slurp a file into memory for ease of comparison and testing. 2827228753Smm * Returns size of file in 'sizep' if non-NULL, null-terminates 2828228753Smm * data in memory for ease of use. 2829228753Smm */ 2830228753Smmchar * 2831228753Smmslurpfile(size_t * sizep, const char *fmt, ...) 2832228753Smm{ 2833228753Smm char filename[8192]; 2834228753Smm struct stat st; 2835228753Smm va_list ap; 2836228753Smm char *p; 2837228753Smm ssize_t bytes_read; 2838228753Smm FILE *f; 2839228753Smm int r; 2840228753Smm 2841228753Smm va_start(ap, fmt); 2842228753Smm vsprintf(filename, fmt, ap); 2843228753Smm va_end(ap); 2844228753Smm 2845228753Smm f = fopen(filename, "rb"); 2846228753Smm if (f == NULL) { 2847228753Smm /* Note: No error; non-existent file is okay here. */ 2848228753Smm return (NULL); 2849228753Smm } 2850228753Smm r = fstat(fileno(f), &st); 2851228753Smm if (r != 0) { 2852228753Smm logprintf("Can't stat file %s\n", filename); 2853228753Smm fclose(f); 2854228753Smm return (NULL); 2855228753Smm } 2856228753Smm p = malloc((size_t)st.st_size + 1); 2857228753Smm if (p == NULL) { 2858228753Smm logprintf("Can't allocate %ld bytes of memory to read file %s\n", 2859228753Smm (long int)st.st_size, filename); 2860228753Smm fclose(f); 2861228753Smm return (NULL); 2862228753Smm } 2863228753Smm bytes_read = fread(p, 1, (size_t)st.st_size, f); 2864228753Smm if (bytes_read < st.st_size) { 2865228753Smm logprintf("Can't read file %s\n", filename); 2866228753Smm fclose(f); 2867228753Smm free(p); 2868228753Smm return (NULL); 2869228753Smm } 2870228753Smm p[st.st_size] = '\0'; 2871228753Smm if (sizep != NULL) 2872228753Smm *sizep = (size_t)st.st_size; 2873228753Smm fclose(f); 2874228753Smm return (p); 2875228753Smm} 2876228753Smm 2877299425Smm/* 2878299425Smm * Slurp a file into memory for ease of comparison and testing. 2879299425Smm * Returns size of file in 'sizep' if non-NULL, null-terminates 2880299425Smm * data in memory for ease of use. 2881299425Smm */ 2882299425Smmvoid 2883299425Smmdumpfile(const char *filename, void *data, size_t len) 2884299425Smm{ 2885299425Smm ssize_t bytes_written; 2886299425Smm FILE *f; 2887299425Smm 2888299425Smm f = fopen(filename, "wb"); 2889299425Smm if (f == NULL) { 2890299425Smm logprintf("Can't open file %s for writing\n", filename); 2891299425Smm return; 2892299425Smm } 2893299425Smm bytes_written = fwrite(data, 1, len, f); 2894299425Smm if (bytes_written < (ssize_t)len) 2895299425Smm logprintf("Can't write file %s\n", filename); 2896299425Smm fclose(f); 2897299425Smm} 2898299425Smm 2899228753Smm/* Read a uuencoded file from the reference directory, decode, and 2900228753Smm * write the result into the current directory. */ 2901299425Smm#define VALID_UUDECODE(c) (c >= 32 && c <= 96) 2902228753Smm#define UUDECODE(c) (((c) - 0x20) & 0x3f) 2903228753Smmvoid 2904228753Smmextract_reference_file(const char *name) 2905228753Smm{ 2906228753Smm char buff[1024]; 2907228753Smm FILE *in, *out; 2908228753Smm 2909228753Smm sprintf(buff, "%s/%s.uu", refdir, name); 2910228753Smm in = fopen(buff, "r"); 2911228753Smm failure("Couldn't open reference file %s", buff); 2912228753Smm assert(in != NULL); 2913228753Smm if (in == NULL) 2914228753Smm return; 2915228753Smm /* Read up to and including the 'begin' line. */ 2916228753Smm for (;;) { 2917228753Smm if (fgets(buff, sizeof(buff), in) == NULL) { 2918228753Smm /* TODO: This is a failure. */ 2919228753Smm return; 2920228753Smm } 2921228753Smm if (memcmp(buff, "begin ", 6) == 0) 2922228753Smm break; 2923228753Smm } 2924228753Smm /* Now, decode the rest and write it. */ 2925228753Smm out = fopen(name, "wb"); 2926228753Smm while (fgets(buff, sizeof(buff), in) != NULL) { 2927228753Smm char *p = buff; 2928228753Smm int bytes; 2929228753Smm 2930228753Smm if (memcmp(buff, "end", 3) == 0) 2931228753Smm break; 2932228753Smm 2933228753Smm bytes = UUDECODE(*p++); 2934228753Smm while (bytes > 0) { 2935228753Smm int n = 0; 2936228753Smm /* Write out 1-3 bytes from that. */ 2937228753Smm if (bytes > 0) { 2938299425Smm assert(VALID_UUDECODE(p[0])); 2939299425Smm assert(VALID_UUDECODE(p[1])); 2940228753Smm n = UUDECODE(*p++) << 18; 2941228753Smm n |= UUDECODE(*p++) << 12; 2942228753Smm fputc(n >> 16, out); 2943228753Smm --bytes; 2944228753Smm } 2945228753Smm if (bytes > 0) { 2946299425Smm assert(VALID_UUDECODE(p[0])); 2947228753Smm n |= UUDECODE(*p++) << 6; 2948228753Smm fputc((n >> 8) & 0xFF, out); 2949228753Smm --bytes; 2950228753Smm } 2951228753Smm if (bytes > 0) { 2952299425Smm assert(VALID_UUDECODE(p[0])); 2953228753Smm n |= UUDECODE(*p++); 2954228753Smm fputc(n & 0xFF, out); 2955228753Smm --bytes; 2956228753Smm } 2957228753Smm } 2958228753Smm } 2959228753Smm fclose(out); 2960228753Smm fclose(in); 2961228753Smm} 2962228753Smm 2963299425Smmvoid 2964299425Smmcopy_reference_file(const char *name) 2965299425Smm{ 2966299425Smm char buff[1024]; 2967299425Smm FILE *in, *out; 2968299425Smm size_t rbytes; 2969299425Smm 2970299425Smm sprintf(buff, "%s/%s", refdir, name); 2971299425Smm in = fopen(buff, "rb"); 2972299425Smm failure("Couldn't open reference file %s", buff); 2973299425Smm assert(in != NULL); 2974299425Smm if (in == NULL) 2975299425Smm return; 2976299425Smm /* Now, decode the rest and write it. */ 2977299425Smm /* Not a lot of error checking here; the input better be right. */ 2978299425Smm out = fopen(name, "wb"); 2979299425Smm while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) { 2980299425Smm if (fwrite(buff, 1, rbytes, out) != rbytes) { 2981299425Smm logprintf("Error: fwrite\n"); 2982299425Smm break; 2983299425Smm } 2984299425Smm } 2985299425Smm fclose(out); 2986299425Smm fclose(in); 2987299425Smm} 2988299425Smm 2989231200Smmint 2990231200Smmis_LargeInode(const char *file) 2991231200Smm{ 2992231200Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2993231200Smm BY_HANDLE_FILE_INFORMATION bhfi; 2994231200Smm int r; 2995231200Smm 2996231200Smm r = my_GetFileInformationByName(file, &bhfi); 2997231200Smm if (r != 0) 2998231200Smm return (0); 2999231200Smm return (bhfi.nFileIndexHigh & 0x0000FFFFUL); 3000231200Smm#else 3001231200Smm struct stat st; 3002231200Smm int64_t ino; 3003231200Smm 3004231200Smm if (stat(file, &st) < 0) 3005231200Smm return (0); 3006231200Smm ino = (int64_t)st.st_ino; 3007231200Smm return (ino > 0xffffffff); 3008231200Smm#endif 3009231200Smm} 3010248590Smm 3011248590Smmvoid 3012248590Smmextract_reference_files(const char **names) 3013248590Smm{ 3014248590Smm while (names && *names) 3015248590Smm extract_reference_file(*names++); 3016248590Smm} 3017248590Smm 3018314565Smm#ifndef PROGRAM 3019310796Smm/* Set ACLs */ 3020314565Smmint 3021314565Smmassertion_entry_set_acls(const char *file, int line, struct archive_entry *ae, 3022310796Smm struct archive_test_acl_t *acls, int n) 3023310796Smm{ 3024314565Smm int i, r, ret; 3025310796Smm 3026314565Smm assertion_count(file, line); 3027314565Smm 3028314565Smm ret = 0; 3029310796Smm archive_entry_acl_clear(ae); 3030310796Smm for (i = 0; i < n; i++) { 3031314565Smm r = archive_entry_acl_add_entry(ae, 3032310796Smm acls[i].type, acls[i].permset, acls[i].tag, 3033310796Smm acls[i].qual, acls[i].name); 3034314565Smm if (r != 0) { 3035314565Smm ret = 1; 3036314565Smm failure_start(file, line, "type=%#010x, ", 3037314565Smm "permset=%#010x, tag=%d, qual=%d name=%s", 3038314565Smm acls[i].type, acls[i].permset, acls[i].tag, 3039314565Smm acls[i].qual, acls[i].name); 3040314565Smm failure_finish(NULL); 3041314565Smm } 3042310796Smm } 3043314565Smm 3044314565Smm return (ret); 3045310796Smm} 3046310796Smm 3047310796Smmstatic int 3048310796Smmarchive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset, 3049310796Smm int tag, int qual, const char *name) 3050310796Smm{ 3051310796Smm if (type != acl->type) 3052310796Smm return (0); 3053310796Smm if (permset != acl->permset) 3054310796Smm return (0); 3055310796Smm if (tag != acl->tag) 3056310796Smm return (0); 3057310796Smm if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ) 3058310796Smm return (1); 3059310796Smm if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) 3060310796Smm return (1); 3061310796Smm if (tag == ARCHIVE_ENTRY_ACL_EVERYONE) 3062310796Smm return (1); 3063310796Smm if (tag == ARCHIVE_ENTRY_ACL_OTHER) 3064310796Smm return (1); 3065310796Smm if (qual != acl->qual) 3066310796Smm return (0); 3067310796Smm if (name == NULL) { 3068310796Smm if (acl->name == NULL || acl->name[0] == '\0') 3069310796Smm return (1); 3070310796Smm return (0); 3071310796Smm } 3072310796Smm if (acl->name == NULL) { 3073310796Smm if (name[0] == '\0') 3074310796Smm return (1); 3075310796Smm return (0); 3076310796Smm } 3077310796Smm return (0 == strcmp(name, acl->name)); 3078310796Smm} 3079310796Smm 3080310796Smm/* Compare ACLs */ 3081314565Smmint 3082314565Smmassertion_entry_compare_acls(const char *file, int line, 3083314565Smm struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt, 3084314565Smm int want_type, int mode) 3085310796Smm{ 3086310796Smm int *marker; 3087314565Smm int i, r, n, ret; 3088310796Smm int type, permset, tag, qual; 3089310796Smm int matched; 3090310796Smm const char *name; 3091310796Smm 3092314565Smm assertion_count(file, line); 3093314565Smm 3094314565Smm ret = 0; 3095310796Smm n = 0; 3096310796Smm marker = malloc(sizeof(marker[0]) * cnt); 3097310796Smm 3098310796Smm for (i = 0; i < cnt; i++) { 3099310796Smm if ((acls[i].type & want_type) != 0) { 3100310796Smm marker[n] = i; 3101310796Smm n++; 3102310796Smm } 3103310796Smm } 3104310796Smm 3105314565Smm if (n == 0) { 3106314565Smm failure_start(file, line, "No ACL's to compare, type mask: %d", 3107314565Smm want_type); 3108314565Smm return (1); 3109314565Smm } 3110310796Smm 3111310796Smm while (0 == (r = archive_entry_acl_next(ae, want_type, 3112310796Smm &type, &permset, &tag, &qual, &name))) { 3113310796Smm for (i = 0, matched = 0; i < n && !matched; i++) { 3114310796Smm if (archive_test_acl_match(&acls[marker[i]], type, 3115310796Smm permset, tag, qual, name)) { 3116310796Smm /* We found a match; remove it. */ 3117310796Smm marker[i] = marker[n - 1]; 3118310796Smm n--; 3119310796Smm matched = 1; 3120310796Smm } 3121310796Smm } 3122310796Smm if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS 3123310796Smm && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) { 3124314565Smm if (!matched) { 3125314565Smm failure_start(file, line, "No match for " 3126314565Smm "user_obj perm"); 3127314565Smm failure_finish(NULL); 3128314565Smm ret = 1; 3129314565Smm } 3130314565Smm if ((permset << 6) != (mode & 0700)) { 3131314565Smm failure_start(file, line, "USER_OBJ permset " 3132314565Smm "(%02o) != user mode (%02o)", permset, 3133314565Smm 07 & (mode >> 6)); 3134314565Smm failure_finish(NULL); 3135314565Smm ret = 1; 3136314565Smm } 3137310796Smm } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS 3138310796Smm && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) { 3139314565Smm if (!matched) { 3140314565Smm failure_start(file, line, "No match for " 3141314565Smm "group_obj perm"); 3142314565Smm failure_finish(NULL); 3143314565Smm ret = 1; 3144314565Smm } 3145314565Smm if ((permset << 3) != (mode & 0070)) { 3146314565Smm failure_start(file, line, "GROUP_OBJ permset " 3147314565Smm "(%02o) != group mode (%02o)", permset, 3148314565Smm 07 & (mode >> 3)); 3149314565Smm failure_finish(NULL); 3150314565Smm ret = 1; 3151314565Smm } 3152310796Smm } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS 3153310796Smm && tag == ARCHIVE_ENTRY_ACL_OTHER) { 3154314565Smm if (!matched) { 3155314565Smm failure_start(file, line, "No match for " 3156314565Smm "other perm"); 3157314565Smm failure_finish(NULL); 3158314565Smm ret = 1; 3159314565Smm } 3160314565Smm if ((permset << 0) != (mode & 0007)) { 3161314565Smm failure_start(file, line, "OTHER permset " 3162314565Smm "(%02o) != other mode (%02o)", permset, 3163314565Smm mode & 07); 3164314565Smm failure_finish(NULL); 3165314565Smm ret = 1; 3166314565Smm } 3167314565Smm } else if (matched != 1) { 3168314565Smm failure_start(file, line, "Could not find match for " 3169314565Smm "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d," 3170310796Smm "name=``%s'')", type, permset, tag, qual, name); 3171314565Smm failure_finish(NULL); 3172314565Smm ret = 1; 3173310796Smm } 3174310796Smm } 3175314565Smm if (r != ARCHIVE_EOF) { 3176314565Smm failure_start(file, line, "Should not exit before EOF"); 3177314565Smm failure_finish(NULL); 3178314565Smm ret = 1; 3179314565Smm } 3180314565Smm if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 && 3181314565Smm (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) { 3182314565Smm failure_start(file, line, "Mode (%02o) and entry mode (%02o) " 3183314565Smm "mismatch", mode, archive_entry_mode(ae)); 3184314565Smm failure_finish(NULL); 3185314565Smm ret = 1; 3186314565Smm } 3187314565Smm if (n != 0) { 3188314565Smm failure_start(file, line, "Could not find match for ACL " 3189314565Smm "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')", 3190314565Smm acls[marker[0]].type, acls[marker[0]].permset, 3191314565Smm acls[marker[0]].tag, acls[marker[0]].qual, 3192314565Smm acls[marker[0]].name); 3193314565Smm failure_finish(NULL); 3194314565Smm ret = 1; 3195314565Smm /* Number of ACLs not matched should == 0 */ 3196314565Smm } 3197310796Smm free(marker); 3198314565Smm return (ret); 3199310796Smm} 3200314565Smm#endif /* !defined(PROGRAM) */ 3201310796Smm 3202228753Smm/* 3203228753Smm * 3204228753Smm * TEST management 3205228753Smm * 3206228753Smm */ 3207228753Smm 3208228753Smm/* 3209228753Smm * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has 3210228753Smm * a line like 3211228753Smm * DEFINE_TEST(test_function) 3212228753Smm * for each test. 3213228753Smm */ 3214228753Smm 3215228753Smm/* Use "list.h" to declare all of the test functions. */ 3216228753Smm#undef DEFINE_TEST 3217228753Smm#define DEFINE_TEST(name) void name(void); 3218228753Smm#include "list.h" 3219228753Smm 3220228753Smm/* Use "list.h" to create a list of all tests (functions and names). */ 3221228753Smm#undef DEFINE_TEST 3222228753Smm#define DEFINE_TEST(n) { n, #n, 0 }, 3223248590Smmstruct test_list_t tests[] = { 3224228753Smm #include "list.h" 3225228753Smm}; 3226228753Smm 3227228753Smm/* 3228228753Smm * Summarize repeated failures in the just-completed test. 3229228753Smm */ 3230228753Smmstatic void 3231299425Smmtest_summarize(int failed, int skips_num) 3232228753Smm{ 3233228753Smm unsigned int i; 3234228753Smm 3235228753Smm switch (verbosity) { 3236228753Smm case VERBOSITY_SUMMARY_ONLY: 3237228753Smm printf(failed ? "E" : "."); 3238228753Smm fflush(stdout); 3239228753Smm break; 3240228753Smm case VERBOSITY_PASSFAIL: 3241299425Smm printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n"); 3242228753Smm break; 3243228753Smm } 3244228753Smm 3245228753Smm log_console = (verbosity == VERBOSITY_LIGHT_REPORT); 3246228753Smm 3247228753Smm for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { 3248228753Smm if (failed_lines[i].count > 1 && !failed_lines[i].skip) 3249228753Smm logprintf("%s:%d: Summary: Failed %d times\n", 3250231200Smm failed_filename, i, failed_lines[i].count); 3251228753Smm } 3252228753Smm /* Clear the failure history for the next file. */ 3253231200Smm failed_filename = NULL; 3254228753Smm memset(failed_lines, 0, sizeof(failed_lines)); 3255228753Smm} 3256228753Smm 3257228753Smm/* 3258228753Smm * Actually run a single test, with appropriate setup and cleanup. 3259228753Smm */ 3260228753Smmstatic int 3261228753Smmtest_run(int i, const char *tmpdir) 3262228753Smm{ 3263231200Smm char workdir[1024]; 3264228753Smm char logfilename[64]; 3265228753Smm int failures_before = failures; 3266299425Smm int skips_before = skips; 3267228753Smm int oldumask; 3268228753Smm 3269228753Smm switch (verbosity) { 3270228753Smm case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */ 3271228753Smm break; 3272228753Smm case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */ 3273299425Smm printf("%3d: %-64s", i, tests[i].name); 3274228753Smm fflush(stdout); 3275228753Smm break; 3276228753Smm default: /* Title of test, details will follow */ 3277228753Smm printf("%3d: %s\n", i, tests[i].name); 3278228753Smm } 3279228753Smm 3280228753Smm /* Chdir to the top-level work directory. */ 3281228753Smm if (!assertChdir(tmpdir)) { 3282228753Smm fprintf(stderr, 3283228753Smm "ERROR: Can't chdir to top work dir %s\n", tmpdir); 3284228753Smm exit(1); 3285228753Smm } 3286228753Smm /* Create a log file for this test. */ 3287228753Smm sprintf(logfilename, "%s.log", tests[i].name); 3288228753Smm logfile = fopen(logfilename, "w"); 3289228753Smm fprintf(logfile, "%s\n\n", tests[i].name); 3290228753Smm /* Chdir() to a work dir for this specific test. */ 3291231200Smm snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name); 3292231200Smm testworkdir = workdir; 3293231200Smm if (!assertMakeDir(testworkdir, 0755) 3294231200Smm || !assertChdir(testworkdir)) { 3295228753Smm fprintf(stderr, 3296231200Smm "ERROR: Can't chdir to work dir %s\n", testworkdir); 3297228753Smm exit(1); 3298228753Smm } 3299228753Smm /* Explicitly reset the locale before each test. */ 3300228753Smm setlocale(LC_ALL, "C"); 3301228753Smm /* Record the umask before we run the test. */ 3302228753Smm umask(oldumask = umask(0)); 3303228753Smm /* 3304228753Smm * Run the actual test. 3305228753Smm */ 3306228753Smm (*tests[i].func)(); 3307228753Smm /* 3308228753Smm * Clean up and report afterwards. 3309228753Smm */ 3310231200Smm testworkdir = NULL; 3311228753Smm /* Restore umask */ 3312228753Smm umask(oldumask); 3313228753Smm /* Reset locale. */ 3314228753Smm setlocale(LC_ALL, "C"); 3315228753Smm /* Reset directory. */ 3316228753Smm if (!assertChdir(tmpdir)) { 3317228753Smm fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n", 3318228753Smm tmpdir); 3319228753Smm exit(1); 3320228753Smm } 3321228753Smm /* Report per-test summaries. */ 3322228753Smm tests[i].failures = failures - failures_before; 3323299425Smm test_summarize(tests[i].failures, skips - skips_before); 3324228753Smm /* Close the per-test log file. */ 3325228753Smm fclose(logfile); 3326228753Smm logfile = NULL; 3327228753Smm /* If there were no failures, we can remove the work dir and logfile. */ 3328228753Smm if (tests[i].failures == 0) { 3329228753Smm if (!keep_temp_files && assertChdir(tmpdir)) { 3330228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 3331228753Smm /* Make sure not to leave empty directories. 3332228753Smm * Sometimes a processing of closing files used by tests 3333228753Smm * is not done, then rmdir will be failed and it will 3334228753Smm * leave a empty test directory. So we should wait a few 3335228753Smm * seconds and retry rmdir. */ 3336228753Smm int r, t; 3337228753Smm for (t = 0; t < 10; t++) { 3338228753Smm if (t > 0) 3339228753Smm Sleep(1000); 3340228753Smm r = systemf("rmdir /S /Q %s", tests[i].name); 3341228753Smm if (r == 0) 3342228753Smm break; 3343228753Smm } 3344228753Smm systemf("del %s", logfilename); 3345228753Smm#else 3346228753Smm systemf("rm -rf %s", tests[i].name); 3347228753Smm systemf("rm %s", logfilename); 3348228753Smm#endif 3349228753Smm } 3350228753Smm } 3351228753Smm /* Return appropriate status. */ 3352228753Smm return (tests[i].failures); 3353228753Smm} 3354228753Smm 3355228753Smm/* 3356228753Smm * 3357228753Smm * 3358228753Smm * MAIN and support routines. 3359228753Smm * 3360228753Smm * 3361228753Smm */ 3362228753Smm 3363228753Smmstatic void 3364228753Smmusage(const char *program) 3365228753Smm{ 3366228753Smm static const int limit = sizeof(tests) / sizeof(tests[0]); 3367228753Smm int i; 3368228753Smm 3369228753Smm printf("Usage: %s [options] <test> <test> ...\n", program); 3370228753Smm printf("Default is to run all tests.\n"); 3371228753Smm printf("Otherwise, specify the numbers of the tests you wish to run.\n"); 3372228753Smm printf("Options:\n"); 3373228753Smm printf(" -d Dump core after any failure, for debugging.\n"); 3374228753Smm printf(" -k Keep all temp files.\n"); 3375228753Smm printf(" Default: temp files for successful tests deleted.\n"); 3376228753Smm#ifdef PROGRAM 3377228753Smm printf(" -p <path> Path to executable to be tested.\n"); 3378228753Smm printf(" Default: path taken from " ENVBASE " environment variable.\n"); 3379228753Smm#endif 3380228753Smm printf(" -q Quiet.\n"); 3381228753Smm printf(" -r <dir> Path to dir containing reference files.\n"); 3382228753Smm printf(" Default: Current directory.\n"); 3383231200Smm printf(" -u Keep running specifies tests until one fails.\n"); 3384228753Smm printf(" -v Verbose.\n"); 3385228753Smm printf("Available tests:\n"); 3386228753Smm for (i = 0; i < limit; i++) 3387228753Smm printf(" %d: %s\n", i, tests[i].name); 3388228753Smm exit(1); 3389228753Smm} 3390228753Smm 3391228753Smmstatic char * 3392228753Smmget_refdir(const char *d) 3393228753Smm{ 3394302003Smm size_t tried_size, buff_size; 3395302003Smm char *buff, *tried, *pwd = NULL, *p = NULL; 3396228753Smm 3397302003Smm#ifdef PATH_MAX 3398302003Smm buff_size = PATH_MAX; 3399302003Smm#else 3400302003Smm buff_size = 8192; 3401302003Smm#endif 3402302003Smm buff = calloc(buff_size, 1); 3403302003Smm if (buff == NULL) { 3404302003Smm fprintf(stderr, "Unable to allocate memory\n"); 3405302003Smm exit(1); 3406302003Smm } 3407302003Smm 3408302003Smm /* Allocate a buffer to hold the various directories we checked. */ 3409302003Smm tried_size = buff_size * 2; 3410302003Smm tried = calloc(tried_size, 1); 3411302003Smm if (tried == NULL) { 3412302003Smm fprintf(stderr, "Unable to allocate memory\n"); 3413302003Smm exit(1); 3414302003Smm } 3415302003Smm 3416228753Smm /* If a dir was specified, try that */ 3417228753Smm if (d != NULL) { 3418228753Smm pwd = NULL; 3419302003Smm snprintf(buff, buff_size, "%s", d); 3420228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3421228753Smm if (p != NULL) goto success; 3422302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3423302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3424228753Smm goto failure; 3425228753Smm } 3426228753Smm 3427228753Smm /* Get the current dir. */ 3428231200Smm#ifdef PATH_MAX 3429231200Smm pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 3430231200Smm#else 3431228753Smm pwd = getcwd(NULL, 0); 3432231200Smm#endif 3433228753Smm while (pwd[strlen(pwd) - 1] == '\n') 3434228753Smm pwd[strlen(pwd) - 1] = '\0'; 3435228753Smm 3436228753Smm /* Look for a known file. */ 3437302003Smm snprintf(buff, buff_size, "%s", pwd); 3438228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3439228753Smm if (p != NULL) goto success; 3440302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3441302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3442228753Smm 3443302003Smm snprintf(buff, buff_size, "%s/test", pwd); 3444228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3445228753Smm if (p != NULL) goto success; 3446302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3447302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3448228753Smm 3449228753Smm#if defined(LIBRARY) 3450302003Smm snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY); 3451228753Smm#else 3452302003Smm snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM); 3453228753Smm#endif 3454228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3455228753Smm if (p != NULL) goto success; 3456302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3457302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3458228753Smm 3459231200Smm#if defined(PROGRAM_ALIAS) 3460302003Smm snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS); 3461231200Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3462231200Smm if (p != NULL) goto success; 3463302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3464302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3465231200Smm#endif 3466231200Smm 3467228753Smm if (memcmp(pwd, "/usr/obj", 8) == 0) { 3468302003Smm snprintf(buff, buff_size, "%s", pwd + 8); 3469228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3470228753Smm if (p != NULL) goto success; 3471302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3472302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3473228753Smm 3474302003Smm snprintf(buff, buff_size, "%s/test", pwd + 8); 3475228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3476228753Smm if (p != NULL) goto success; 3477302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3478302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3479228753Smm } 3480228753Smm 3481228753Smmfailure: 3482228753Smm printf("Unable to locate known reference file %s\n", KNOWNREF); 3483228753Smm printf(" Checked following directories:\n%s\n", tried); 3484299425Smm printf("Use -r option to specify full path to reference directory\n"); 3485228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) 3486228753Smm DebugBreak(); 3487228753Smm#endif 3488228753Smm exit(1); 3489228753Smm 3490228753Smmsuccess: 3491228753Smm free(p); 3492228753Smm free(pwd); 3493302003Smm free(tried); 3494302003Smm 3495302003Smm /* Copy result into a fresh buffer to reduce memory usage. */ 3496302003Smm p = strdup(buff); 3497302003Smm free(buff); 3498302003Smm return p; 3499228753Smm} 3500228753Smm 3501228753Smmint 3502228753Smmmain(int argc, char **argv) 3503228753Smm{ 3504228753Smm static const int limit = sizeof(tests) / sizeof(tests[0]); 3505238825Smm int test_set[sizeof(tests) / sizeof(tests[0])]; 3506238825Smm int i = 0, j = 0, tests_run = 0, tests_failed = 0, option; 3507228753Smm time_t now; 3508228753Smm char *refdir_alloc = NULL; 3509228753Smm const char *progname; 3510231200Smm char **saved_argv; 3511228753Smm const char *tmp, *option_arg, *p; 3512238825Smm char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL; 3513228753Smm char tmpdir_timestamp[256]; 3514228753Smm 3515228753Smm (void)argc; /* UNUSED */ 3516228753Smm 3517231200Smm /* Get the current dir. */ 3518231200Smm#ifdef PATH_MAX 3519231200Smm pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 3520231200Smm#else 3521231200Smm pwd = getcwd(NULL, 0); 3522231200Smm#endif 3523231200Smm while (pwd[strlen(pwd) - 1] == '\n') 3524231200Smm pwd[strlen(pwd) - 1] = '\0'; 3525231200Smm 3526299425Smm#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 3527228753Smm /* To stop to run the default invalid parameter handler. */ 3528228753Smm _set_invalid_parameter_handler(invalid_parameter_handler); 3529228753Smm /* Disable annoying assertion message box. */ 3530228753Smm _CrtSetReportMode(_CRT_ASSERT, 0); 3531228753Smm#endif 3532228753Smm 3533228753Smm /* 3534228753Smm * Name of this program, used to build root of our temp directory 3535228753Smm * tree. 3536228753Smm */ 3537228753Smm progname = p = argv[0]; 3538231200Smm if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL) 3539231200Smm { 3540231200Smm fprintf(stderr, "ERROR: Out of memory."); 3541231200Smm exit(1); 3542231200Smm } 3543231200Smm strcpy(testprogdir, progname); 3544228753Smm while (*p != '\0') { 3545228753Smm /* Support \ or / dir separators for Windows compat. */ 3546228753Smm if (*p == '/' || *p == '\\') 3547231200Smm { 3548228753Smm progname = p + 1; 3549231200Smm i = j; 3550231200Smm } 3551228753Smm ++p; 3552231200Smm j++; 3553228753Smm } 3554231200Smm testprogdir[i] = '\0'; 3555232133Smm#if defined(_WIN32) && !defined(__CYGWIN__) 3556232133Smm if (testprogdir[0] != '/' && testprogdir[0] != '\\' && 3557232133Smm !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') || 3558232133Smm (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) && 3559232133Smm testprogdir[1] == ':' && 3560232133Smm (testprogdir[2] == '/' || testprogdir[2] == '\\'))) 3561232133Smm#else 3562231200Smm if (testprogdir[0] != '/') 3563232133Smm#endif 3564231200Smm { 3565231200Smm /* Fixup path for relative directories. */ 3566231200Smm if ((testprogdir = (char *)realloc(testprogdir, 3567231200Smm strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL) 3568231200Smm { 3569231200Smm fprintf(stderr, "ERROR: Out of memory."); 3570231200Smm exit(1); 3571231200Smm } 3572232133Smm memmove(testprogdir + strlen(pwd) + 1, testprogdir, 3573299425Smm strlen(testprogdir) + 1); 3574232133Smm memcpy(testprogdir, pwd, strlen(pwd)); 3575231200Smm testprogdir[strlen(pwd)] = '/'; 3576231200Smm } 3577228753Smm 3578228753Smm#ifdef PROGRAM 3579228753Smm /* Get the target program from environment, if available. */ 3580228753Smm testprogfile = getenv(ENVBASE); 3581228753Smm#endif 3582228753Smm 3583228753Smm if (getenv("TMPDIR") != NULL) 3584228753Smm tmp = getenv("TMPDIR"); 3585228753Smm else if (getenv("TMP") != NULL) 3586228753Smm tmp = getenv("TMP"); 3587228753Smm else if (getenv("TEMP") != NULL) 3588228753Smm tmp = getenv("TEMP"); 3589228753Smm else if (getenv("TEMPDIR") != NULL) 3590228753Smm tmp = getenv("TEMPDIR"); 3591228753Smm else 3592228753Smm tmp = "/tmp"; 3593228753Smm 3594228753Smm /* Allow -d to be controlled through the environment. */ 3595228753Smm if (getenv(ENVBASE "_DEBUG") != NULL) 3596228753Smm dump_on_failure = 1; 3597228753Smm 3598238825Smm /* Allow -v to be controlled through the environment. */ 3599238825Smm if (getenv("_VERBOSITY_LEVEL") != NULL) 3600238825Smm { 3601238825Smm vlevel = getenv("_VERBOSITY_LEVEL"); 3602238825Smm verbosity = atoi(vlevel); 3603238825Smm if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL) 3604238825Smm { 3605238825Smm /* Unsupported verbosity levels are silently ignored */ 3606238825Smm vlevel = NULL; 3607238825Smm verbosity = VERBOSITY_PASSFAIL; 3608238825Smm } 3609238825Smm } 3610238825Smm 3611228753Smm /* Get the directory holding test files from environment. */ 3612228753Smm refdir = getenv(ENVBASE "_TEST_FILES"); 3613228753Smm 3614228753Smm /* 3615228753Smm * Parse options, without using getopt(), which isn't available 3616228753Smm * on all platforms. 3617228753Smm */ 3618228753Smm ++argv; /* Skip program name */ 3619228753Smm while (*argv != NULL) { 3620228753Smm if (**argv != '-') 3621228753Smm break; 3622228753Smm p = *argv++; 3623228753Smm ++p; /* Skip '-' */ 3624228753Smm while (*p != '\0') { 3625228753Smm option = *p++; 3626228753Smm option_arg = NULL; 3627228753Smm /* If 'opt' takes an argument, parse that. */ 3628228753Smm if (option == 'p' || option == 'r') { 3629228753Smm if (*p != '\0') 3630228753Smm option_arg = p; 3631228753Smm else if (*argv == NULL) { 3632228753Smm fprintf(stderr, 3633228753Smm "Option -%c requires argument.\n", 3634228753Smm option); 3635228753Smm usage(progname); 3636228753Smm } else 3637228753Smm option_arg = *argv++; 3638228753Smm p = ""; /* End of this option word. */ 3639228753Smm } 3640228753Smm 3641228753Smm /* Now, handle the option. */ 3642228753Smm switch (option) { 3643228753Smm case 'd': 3644228753Smm dump_on_failure = 1; 3645228753Smm break; 3646228753Smm case 'k': 3647228753Smm keep_temp_files = 1; 3648228753Smm break; 3649228753Smm case 'p': 3650228753Smm#ifdef PROGRAM 3651228753Smm testprogfile = option_arg; 3652228753Smm#else 3653231200Smm fprintf(stderr, "-p option not permitted\n"); 3654228753Smm usage(progname); 3655228753Smm#endif 3656228753Smm break; 3657228753Smm case 'q': 3658238825Smm if (!vlevel) 3659238825Smm verbosity--; 3660228753Smm break; 3661228753Smm case 'r': 3662228753Smm refdir = option_arg; 3663228753Smm break; 3664231200Smm case 'u': 3665231200Smm until_failure++; 3666231200Smm break; 3667228753Smm case 'v': 3668238825Smm if (!vlevel) 3669238825Smm verbosity++; 3670228753Smm break; 3671228753Smm default: 3672231200Smm fprintf(stderr, "Unrecognized option '%c'\n", 3673231200Smm option); 3674228753Smm usage(progname); 3675228753Smm } 3676228753Smm } 3677228753Smm } 3678228753Smm 3679228753Smm /* 3680228753Smm * Sanity-check that our options make sense. 3681228753Smm */ 3682228753Smm#ifdef PROGRAM 3683228753Smm if (testprogfile == NULL) 3684228753Smm { 3685231200Smm if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 + 3686231200Smm strlen(PROGRAM) + 1)) == NULL) 3687231200Smm { 3688231200Smm fprintf(stderr, "ERROR: Out of memory."); 3689231200Smm exit(1); 3690231200Smm } 3691231200Smm strcpy(tmp2, testprogdir); 3692231200Smm strcat(tmp2, "/"); 3693231200Smm strcat(tmp2, PROGRAM); 3694231200Smm testprogfile = tmp2; 3695231200Smm } 3696231200Smm 3697231200Smm { 3698228753Smm char *testprg; 3699228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 3700228753Smm /* Command.com sometimes rejects '/' separators. */ 3701228753Smm testprg = strdup(testprogfile); 3702228753Smm for (i = 0; testprg[i] != '\0'; i++) { 3703228753Smm if (testprg[i] == '/') 3704228753Smm testprg[i] = '\\'; 3705228753Smm } 3706228753Smm testprogfile = testprg; 3707228753Smm#endif 3708228753Smm /* Quote the name that gets put into shell command lines. */ 3709228753Smm testprg = malloc(strlen(testprogfile) + 3); 3710228753Smm strcpy(testprg, "\""); 3711228753Smm strcat(testprg, testprogfile); 3712228753Smm strcat(testprg, "\""); 3713228753Smm testprog = testprg; 3714228753Smm } 3715228753Smm#endif 3716228753Smm 3717231200Smm#if !defined(_WIN32) && defined(SIGPIPE) 3718231200Smm { /* Ignore SIGPIPE signals */ 3719231200Smm struct sigaction sa; 3720231200Smm sa.sa_handler = SIG_IGN; 3721231200Smm sigemptyset(&sa.sa_mask); 3722231200Smm sa.sa_flags = 0; 3723231200Smm sigaction(SIGPIPE, &sa, NULL); 3724231200Smm } 3725231200Smm#endif 3726231200Smm 3727228753Smm /* 3728228753Smm * Create a temp directory for the following tests. 3729228753Smm * Include the time the tests started as part of the name, 3730228753Smm * to make it easier to track the results of multiple tests. 3731228753Smm */ 3732228753Smm now = time(NULL); 3733228753Smm for (i = 0; ; i++) { 3734228753Smm strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), 3735228753Smm "%Y-%m-%dT%H.%M.%S", 3736228753Smm localtime(&now)); 3737228753Smm sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname, 3738228753Smm tmpdir_timestamp, i); 3739228753Smm if (assertMakeDir(tmpdir,0755)) 3740228753Smm break; 3741228753Smm if (i >= 999) { 3742228753Smm fprintf(stderr, 3743228753Smm "ERROR: Unable to create temp directory %s\n", 3744228753Smm tmpdir); 3745228753Smm exit(1); 3746228753Smm } 3747228753Smm } 3748228753Smm 3749228753Smm /* 3750228753Smm * If the user didn't specify a directory for locating 3751228753Smm * reference files, try to find the reference files in 3752228753Smm * the "usual places." 3753228753Smm */ 3754228753Smm refdir = refdir_alloc = get_refdir(refdir); 3755228753Smm 3756228753Smm /* 3757228753Smm * Banner with basic information. 3758228753Smm */ 3759228753Smm printf("\n"); 3760228753Smm printf("If tests fail or crash, details will be in:\n"); 3761228753Smm printf(" %s\n", tmpdir); 3762228753Smm printf("\n"); 3763228753Smm if (verbosity > VERBOSITY_SUMMARY_ONLY) { 3764228753Smm printf("Reference files will be read from: %s\n", refdir); 3765228753Smm#ifdef PROGRAM 3766228753Smm printf("Running tests on: %s\n", testprog); 3767228753Smm#endif 3768228753Smm printf("Exercising: "); 3769228753Smm fflush(stdout); 3770228753Smm printf("%s\n", EXTRA_VERSION); 3771228753Smm } else { 3772228753Smm printf("Running "); 3773228753Smm fflush(stdout); 3774228753Smm } 3775228753Smm 3776228753Smm /* 3777228753Smm * Run some or all of the individual tests. 3778228753Smm */ 3779231200Smm saved_argv = argv; 3780231200Smm do { 3781231200Smm argv = saved_argv; 3782238825Smm do { 3783238825Smm int test_num; 3784238825Smm 3785248590Smm test_num = get_test_set(test_set, limit, *argv, tests); 3786238825Smm if (test_num < 0) { 3787238825Smm printf("*** INVALID Test %s\n", *argv); 3788238825Smm free(refdir_alloc); 3789248590Smm free(testprogdir); 3790238825Smm usage(progname); 3791238825Smm return (1); 3792238825Smm } 3793238825Smm for (i = 0; i < test_num; i++) { 3794231200Smm tests_run++; 3795238825Smm if (test_run(test_set[i], tmpdir)) { 3796231200Smm tests_failed++; 3797231200Smm if (until_failure) 3798231200Smm goto finish; 3799228753Smm } 3800231200Smm } 3801238825Smm if (*argv != NULL) 3802231200Smm argv++; 3803238825Smm } while (*argv != NULL); 3804231200Smm } while (until_failure); 3805228753Smm 3806231200Smmfinish: 3807231200Smm /* Must be freed after all tests run */ 3808231200Smm free(tmp2); 3809231200Smm free(testprogdir); 3810231200Smm free(pwd); 3811231200Smm 3812228753Smm /* 3813228753Smm * Report summary statistics. 3814228753Smm */ 3815228753Smm if (verbosity > VERBOSITY_SUMMARY_ONLY) { 3816228753Smm printf("\n"); 3817228753Smm printf("Totals:\n"); 3818228753Smm printf(" Tests run: %8d\n", tests_run); 3819228753Smm printf(" Tests failed: %8d\n", tests_failed); 3820228753Smm printf(" Assertions checked:%8d\n", assertions); 3821228753Smm printf(" Assertions failed: %8d\n", failures); 3822228753Smm printf(" Skips reported: %8d\n", skips); 3823228753Smm } 3824228753Smm if (failures) { 3825228753Smm printf("\n"); 3826228753Smm printf("Failing tests:\n"); 3827228753Smm for (i = 0; i < limit; ++i) { 3828228753Smm if (tests[i].failures) 3829228753Smm printf(" %d: %s (%d failures)\n", i, 3830228753Smm tests[i].name, tests[i].failures); 3831228753Smm } 3832228753Smm printf("\n"); 3833228753Smm printf("Details for failing tests: %s\n", tmpdir); 3834228753Smm printf("\n"); 3835228753Smm } else { 3836228753Smm if (verbosity == VERBOSITY_SUMMARY_ONLY) 3837228753Smm printf("\n"); 3838228753Smm printf("%d tests passed, no failures\n", tests_run); 3839228753Smm } 3840228753Smm 3841228753Smm free(refdir_alloc); 3842228753Smm 3843228753Smm /* If the final tmpdir is empty, we can remove it. */ 3844228753Smm /* This should be the usual case when all tests succeed. */ 3845228753Smm assertChdir(".."); 3846228753Smm rmdir(tmpdir); 3847228753Smm 3848228753Smm return (tests_failed ? 1 : 0); 3849228753Smm} 3850