test_main.c revision 358090
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 171348608Smmtypedef struct _REPARSE_DATA_BUFFER { 172348608Smm ULONG ReparseTag; 173348608Smm USHORT ReparseDataLength; 174348608Smm USHORT Reserved; 175348608Smm union { 176348608Smm struct { 177348608Smm USHORT SubstituteNameOffset; 178348608Smm USHORT SubstituteNameLength; 179348608Smm USHORT PrintNameOffset; 180348608Smm USHORT PrintNameLength; 181348608Smm ULONG Flags; 182348608Smm WCHAR PathBuffer[1]; 183348608Smm } SymbolicLinkReparseBuffer; 184348608Smm struct { 185348608Smm USHORT SubstituteNameOffset; 186348608Smm USHORT SubstituteNameLength; 187348608Smm USHORT PrintNameOffset; 188348608Smm USHORT PrintNameLength; 189348608Smm WCHAR PathBuffer[1]; 190348608Smm } MountPointReparseBuffer; 191348608Smm struct { 192348608Smm UCHAR DataBuffer[1]; 193348608Smm } GenericReparseBuffer; 194348608Smm } DUMMYUNIONNAME; 195348608Smm} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; 196348608Smm 197238825Smmstatic void * 198238825SmmGetFunctionKernel32(const char *name) 199228753Smm{ 200228753Smm static HINSTANCE lib; 201228753Smm static int set; 202228753Smm if (!set) { 203228753Smm set = 1; 204228753Smm lib = LoadLibrary("kernel32.dll"); 205228753Smm } 206228753Smm if (lib == NULL) { 207228753Smm fprintf(stderr, "Can't load kernel32.dll?!\n"); 208228753Smm exit(1); 209228753Smm } 210228753Smm return (void *)GetProcAddress(lib, name); 211228753Smm} 212228753Smm 213228753Smmstatic int 214348608Smmmy_CreateSymbolicLinkA(const char *linkname, const char *target, 215348608Smm int targetIsDir) 216228753Smm{ 217228753Smm static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD); 218348608Smm DWORD attrs; 219228753Smm static int set; 220348608Smm int ret, tmpflags, llen, tlen; 221348608Smm int flags = 0; 222348608Smm char *src, *tgt, *p; 223228753Smm if (!set) { 224228753Smm set = 1; 225228753Smm f = GetFunctionKernel32("CreateSymbolicLinkA"); 226228753Smm } 227348608Smm if (f == NULL) 228348608Smm return (0); 229348608Smm 230348608Smm tlen = strlen(target); 231348608Smm llen = strlen(linkname); 232348608Smm 233348608Smm if (tlen == 0 || llen == 0) 234348608Smm return (0); 235348608Smm 236348608Smm tgt = malloc((tlen + 1) * sizeof(char)); 237348608Smm if (tgt == NULL) 238348608Smm return (0); 239348608Smm src = malloc((llen + 1) * sizeof(char)); 240348608Smm if (src == NULL) { 241348608Smm free(tgt); 242348608Smm return (0); 243348608Smm } 244348608Smm 245348608Smm /* 246348608Smm * Translate slashes to backslashes 247348608Smm */ 248348608Smm p = src; 249348608Smm while(*linkname != '\0') { 250348608Smm if (*linkname == '/') 251348608Smm *p = '\\'; 252348608Smm else 253348608Smm *p = *linkname; 254348608Smm linkname++; 255348608Smm p++; 256348608Smm } 257348608Smm *p = '\0'; 258348608Smm 259348608Smm p = tgt; 260348608Smm while(*target != '\0') { 261348608Smm if (*target == '/') 262348608Smm *p = '\\'; 263348608Smm else 264348608Smm *p = *target; 265348608Smm target++; 266348608Smm p++; 267348608Smm } 268348608Smm *p = '\0'; 269348608Smm 270348608Smm /* 271348608Smm * Each test has to specify if a file or a directory symlink 272348608Smm * should be created. 273348608Smm */ 274348608Smm if (targetIsDir) { 275348608Smm#if defined(SYMBOLIC_LINK_FLAG_DIRECTORY) 276348608Smm flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; 277348608Smm#else 278348608Smm flags |= 0x1; 279348608Smm#endif 280348608Smm } 281348608Smm 282348608Smm#if defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) 283348608Smm tmpflags = flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; 284348608Smm#else 285348608Smm tmpflags = flags | 0x2; 286348608Smm#endif 287348608Smm /* 288348608Smm * Windows won't overwrite existing links 289348608Smm */ 290348608Smm attrs = GetFileAttributesA(linkname); 291348608Smm if (attrs != INVALID_FILE_ATTRIBUTES) { 292348608Smm if (attrs & FILE_ATTRIBUTE_DIRECTORY) 293348608Smm RemoveDirectoryA(linkname); 294348608Smm else 295348608Smm DeleteFileA(linkname); 296348608Smm } 297348608Smm 298348608Smm ret = (*f)(src, tgt, tmpflags); 299348608Smm /* 300348608Smm * Prior to Windows 10 the SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE 301353377Smm * is not understood 302348608Smm */ 303348608Smm if (!ret) 304348608Smm ret = (*f)(src, tgt, flags); 305348608Smm 306348608Smm free(src); 307348608Smm free(tgt); 308348608Smm return (ret); 309228753Smm} 310228753Smm 311228753Smmstatic int 312228753Smmmy_CreateHardLinkA(const char *linkname, const char *target) 313228753Smm{ 314228753Smm static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES); 315228753Smm static int set; 316228753Smm if (!set) { 317228753Smm set = 1; 318228753Smm f = GetFunctionKernel32("CreateHardLinkA"); 319228753Smm } 320228753Smm return f == NULL ? 0 : (*f)(linkname, target, NULL); 321228753Smm} 322228753Smm 323238825Smmstatic int 324228753Smmmy_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi) 325228753Smm{ 326228753Smm HANDLE h; 327228753Smm int r; 328228753Smm 329228753Smm memset(bhfi, 0, sizeof(*bhfi)); 330228753Smm h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, 331231200Smm OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 332228753Smm if (h == INVALID_HANDLE_VALUE) 333228753Smm return (0); 334228753Smm r = GetFileInformationByHandle(h, bhfi); 335228753Smm CloseHandle(h); 336228753Smm return (r); 337228753Smm} 338228753Smm#endif 339228753Smm 340299425Smm#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 341228753Smmstatic void 342228753Smminvalid_parameter_handler(const wchar_t * expression, 343228753Smm const wchar_t * function, const wchar_t * file, 344228753Smm unsigned int line, uintptr_t pReserved) 345228753Smm{ 346228753Smm /* nop */ 347313071Smm // Silence unused-parameter compiler warnings. 348313071Smm (void)expression; 349313071Smm (void)function; 350313071Smm (void)file; 351313071Smm (void)line; 352313071Smm (void)pReserved; 353228753Smm} 354228753Smm#endif 355228753Smm 356228753Smm/* 357228753Smm * 358228753Smm * OPTIONS FLAGS 359228753Smm * 360228753Smm */ 361228753Smm 362228753Smm/* Enable core dump on failure. */ 363228753Smmstatic int dump_on_failure = 0; 364228753Smm/* Default is to remove temp dirs and log data for successful tests. */ 365228753Smmstatic int keep_temp_files = 0; 366231200Smm/* Default is to run the specified tests once and report errors. */ 367231200Smmstatic int until_failure = 0; 368228753Smm/* Default is to just report pass/fail for each test. */ 369228753Smmstatic int verbosity = 0; 370228753Smm#define VERBOSITY_SUMMARY_ONLY -1 /* -q */ 371228753Smm#define VERBOSITY_PASSFAIL 0 /* Default */ 372228753Smm#define VERBOSITY_LIGHT_REPORT 1 /* -v */ 373228753Smm#define VERBOSITY_FULL 2 /* -vv */ 374228753Smm/* A few places generate even more output for verbosity > VERBOSITY_FULL, 375228753Smm * mostly for debugging the test harness itself. */ 376228753Smm/* Cumulative count of assertion failures. */ 377228753Smmstatic int failures = 0; 378228753Smm/* Cumulative count of reported skips. */ 379228753Smmstatic int skips = 0; 380228753Smm/* Cumulative count of assertions checked. */ 381228753Smmstatic int assertions = 0; 382228753Smm 383228753Smm/* Directory where uuencoded reference files can be found. */ 384228753Smmstatic const char *refdir; 385228753Smm 386228753Smm/* 387228753Smm * Report log information selectively to console and/or disk log. 388228753Smm */ 389228753Smmstatic int log_console = 0; 390228753Smmstatic FILE *logfile; 391358090Smmstatic void __LA_PRINTFLIKE(1, 0) 392228753Smmvlogprintf(const char *fmt, va_list ap) 393228753Smm{ 394228753Smm#ifdef va_copy 395228753Smm va_list lfap; 396228753Smm va_copy(lfap, ap); 397228753Smm#endif 398228753Smm if (log_console) 399228753Smm vfprintf(stdout, fmt, ap); 400228753Smm if (logfile != NULL) 401228753Smm#ifdef va_copy 402228753Smm vfprintf(logfile, fmt, lfap); 403228753Smm va_end(lfap); 404228753Smm#else 405228753Smm vfprintf(logfile, fmt, ap); 406228753Smm#endif 407228753Smm} 408228753Smm 409358090Smmstatic void __LA_PRINTFLIKE(1, 2) 410228753Smmlogprintf(const char *fmt, ...) 411228753Smm{ 412228753Smm va_list ap; 413228753Smm va_start(ap, fmt); 414228753Smm vlogprintf(fmt, ap); 415228753Smm va_end(ap); 416228753Smm} 417228753Smm 418228753Smm/* Set up a message to display only if next assertion fails. */ 419228753Smmstatic char msgbuff[4096]; 420228753Smmstatic const char *msg, *nextmsg; 421228753Smmvoid 422228753Smmfailure(const char *fmt, ...) 423228753Smm{ 424228753Smm va_list ap; 425231200Smm if (fmt == NULL) { 426231200Smm nextmsg = NULL; 427231200Smm } else { 428231200Smm va_start(ap, fmt); 429231200Smm vsprintf(msgbuff, fmt, ap); 430231200Smm va_end(ap); 431231200Smm nextmsg = msgbuff; 432231200Smm } 433228753Smm} 434228753Smm 435228753Smm/* 436228753Smm * Copy arguments into file-local variables. 437228753Smm * This was added to permit vararg assert() functions without needing 438228753Smm * variadic wrapper macros. Turns out that the vararg capability is almost 439228753Smm * never used, so almost all of the vararg assertions can be simplified 440228753Smm * by removing the vararg capability and reworking the wrapper macro to 441228753Smm * pass __FILE__, __LINE__ directly into the function instead of using 442228753Smm * this hook. I suspect this machinery is used so rarely that we 443228753Smm * would be better off just removing it entirely. That would simplify 444231200Smm * the code here noticeably. 445228753Smm */ 446231200Smmstatic const char *skipping_filename; 447231200Smmstatic int skipping_line; 448231200Smmvoid skipping_setup(const char *filename, int line) 449228753Smm{ 450231200Smm skipping_filename = filename; 451231200Smm skipping_line = line; 452228753Smm} 453228753Smm 454228753Smm/* Called at the beginning of each assert() function. */ 455228753Smmstatic void 456228753Smmassertion_count(const char *file, int line) 457228753Smm{ 458228753Smm (void)file; /* UNUSED */ 459228753Smm (void)line; /* UNUSED */ 460228753Smm ++assertions; 461228753Smm /* Proper handling of "failure()" message. */ 462228753Smm msg = nextmsg; 463228753Smm nextmsg = NULL; 464228753Smm /* Uncomment to print file:line after every assertion. 465228753Smm * Verbose, but occasionally useful in tracking down crashes. */ 466228753Smm /* printf("Checked %s:%d\n", file, line); */ 467228753Smm} 468228753Smm 469228753Smm/* 470228753Smm * For each test source file, we remember how many times each 471228753Smm * assertion was reported. Cleared before each new test, 472228753Smm * used by test_summarize(). 473228753Smm */ 474228753Smmstatic struct line { 475228753Smm int count; 476228753Smm int skip; 477228753Smm} failed_lines[10000]; 478231200Smmconst char *failed_filename; 479228753Smm 480228753Smm/* Count this failure, setup up log destination and handle initial report. */ 481358090Smmstatic void __LA_PRINTFLIKE(3, 4) 482228753Smmfailure_start(const char *filename, int line, const char *fmt, ...) 483228753Smm{ 484228753Smm va_list ap; 485228753Smm 486228753Smm /* Record another failure for this line. */ 487228753Smm ++failures; 488231200Smm failed_filename = filename; 489228753Smm failed_lines[line].count++; 490228753Smm 491228753Smm /* Determine whether to log header to console. */ 492228753Smm switch (verbosity) { 493228753Smm case VERBOSITY_LIGHT_REPORT: 494228753Smm log_console = (failed_lines[line].count < 2); 495228753Smm break; 496228753Smm default: 497228753Smm log_console = (verbosity >= VERBOSITY_FULL); 498228753Smm } 499228753Smm 500228753Smm /* Log file:line header for this failure */ 501228753Smm va_start(ap, fmt); 502228753Smm#if _MSC_VER 503228753Smm logprintf("%s(%d): ", filename, line); 504228753Smm#else 505228753Smm logprintf("%s:%d: ", filename, line); 506228753Smm#endif 507228753Smm vlogprintf(fmt, ap); 508228753Smm va_end(ap); 509228753Smm logprintf("\n"); 510228753Smm 511228753Smm if (msg != NULL && msg[0] != '\0') { 512228753Smm logprintf(" Description: %s\n", msg); 513228753Smm msg = NULL; 514228753Smm } 515228753Smm 516228753Smm /* Determine whether to log details to console. */ 517228753Smm if (verbosity == VERBOSITY_LIGHT_REPORT) 518228753Smm log_console = 0; 519228753Smm} 520228753Smm 521228753Smm/* Complete reporting of failed tests. */ 522228753Smm/* 523228753Smm * The 'extra' hook here is used by libarchive to include libarchive 524228753Smm * error messages with assertion failures. It could also be used 525228753Smm * to add strerror() output, for example. Just define the EXTRA_DUMP() 526228753Smm * macro appropriately. 527228753Smm */ 528228753Smmstatic void 529228753Smmfailure_finish(void *extra) 530228753Smm{ 531228753Smm (void)extra; /* UNUSED (maybe) */ 532228753Smm#ifdef EXTRA_DUMP 533231200Smm if (extra != NULL) { 534231200Smm logprintf(" errno: %d\n", EXTRA_ERRNO(extra)); 535228753Smm logprintf(" detail: %s\n", EXTRA_DUMP(extra)); 536231200Smm } 537228753Smm#endif 538228753Smm 539228753Smm if (dump_on_failure) { 540228753Smm fprintf(stderr, 541228753Smm " *** forcing core dump so failure can be debugged ***\n"); 542228753Smm abort(); 543228753Smm } 544228753Smm} 545228753Smm 546228753Smm/* Inform user that we're skipping some checks. */ 547228753Smmvoid 548228753Smmtest_skipping(const char *fmt, ...) 549228753Smm{ 550228753Smm char buff[1024]; 551228753Smm va_list ap; 552228753Smm 553228753Smm va_start(ap, fmt); 554228753Smm vsprintf(buff, fmt, ap); 555228753Smm va_end(ap); 556231200Smm /* Use failure() message if set. */ 557231200Smm msg = nextmsg; 558231200Smm nextmsg = NULL; 559228753Smm /* failure_start() isn't quite right, but is awfully convenient. */ 560231200Smm failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff); 561228753Smm --failures; /* Undo failures++ in failure_start() */ 562228753Smm /* Don't failure_finish() here. */ 563228753Smm /* Mark as skip, so doesn't count as failed test. */ 564231200Smm failed_lines[skipping_line].skip = 1; 565228753Smm ++skips; 566228753Smm} 567228753Smm 568228753Smm/* 569228753Smm * 570228753Smm * ASSERTIONS 571228753Smm * 572228753Smm */ 573228753Smm 574228753Smm/* Generic assert() just displays the failed condition. */ 575228753Smmint 576228753Smmassertion_assert(const char *file, int line, int value, 577228753Smm const char *condition, void *extra) 578228753Smm{ 579228753Smm assertion_count(file, line); 580228753Smm if (!value) { 581228753Smm failure_start(file, line, "Assertion failed: %s", condition); 582228753Smm failure_finish(extra); 583228753Smm } 584228753Smm return (value); 585228753Smm} 586228753Smm 587228753Smm/* chdir() and report any errors */ 588228753Smmint 589228753Smmassertion_chdir(const char *file, int line, const char *pathname) 590228753Smm{ 591228753Smm assertion_count(file, line); 592228753Smm if (chdir(pathname) == 0) 593228753Smm return (1); 594228753Smm failure_start(file, line, "chdir(\"%s\")", pathname); 595228753Smm failure_finish(NULL); 596228753Smm return (0); 597228753Smm 598228753Smm} 599228753Smm 600228753Smm/* Verify two integers are equal. */ 601228753Smmint 602228753Smmassertion_equal_int(const char *file, int line, 603228753Smm long long v1, const char *e1, long long v2, const char *e2, void *extra) 604228753Smm{ 605228753Smm assertion_count(file, line); 606228753Smm if (v1 == v2) 607228753Smm return (1); 608228753Smm failure_start(file, line, "%s != %s", e1, e2); 609228753Smm logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1); 610228753Smm logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2); 611228753Smm failure_finish(extra); 612228753Smm return (0); 613228753Smm} 614228753Smm 615231200Smm/* 616231200Smm * Utility to convert a single UTF-8 sequence. 617231200Smm */ 618231200Smmstatic int 619231200Smm_utf8_to_unicode(uint32_t *pwc, const char *s, size_t n) 620228753Smm{ 621231200Smm static const char utf8_count[256] = { 622231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */ 623231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */ 624231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */ 625231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */ 626231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */ 627231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */ 628231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */ 629231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */ 630231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */ 631231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */ 632231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */ 633231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */ 634231200Smm 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */ 635231200Smm 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */ 636231200Smm 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */ 637231200Smm 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */ 638231200Smm }; 639231200Smm int ch; 640231200Smm int cnt; 641231200Smm uint32_t wc; 642231200Smm 643231200Smm *pwc = 0; 644231200Smm 645231200Smm /* Sanity check. */ 646231200Smm if (n == 0) 647231200Smm return (0); 648231200Smm /* 649231200Smm * Decode 1-4 bytes depending on the value of the first byte. 650231200Smm */ 651231200Smm ch = (unsigned char)*s; 652231200Smm if (ch == 0) 653231200Smm return (0); /* Standard: return 0 for end-of-string. */ 654231200Smm cnt = utf8_count[ch]; 655231200Smm 656309587Smm /* Invalid sequence or there are not plenty bytes. */ 657231200Smm if (n < (size_t)cnt) 658231200Smm return (-1); 659231200Smm 660231200Smm /* Make a Unicode code point from a single UTF-8 sequence. */ 661231200Smm switch (cnt) { 662231200Smm case 1: /* 1 byte sequence. */ 663231200Smm *pwc = ch & 0x7f; 664231200Smm return (cnt); 665231200Smm case 2: /* 2 bytes sequence. */ 666231200Smm if ((s[1] & 0xc0) != 0x80) return (-1); 667231200Smm *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f); 668231200Smm return (cnt); 669231200Smm case 3: /* 3 bytes sequence. */ 670231200Smm if ((s[1] & 0xc0) != 0x80) return (-1); 671231200Smm if ((s[2] & 0xc0) != 0x80) return (-1); 672231200Smm wc = ((ch & 0x0f) << 12) 673231200Smm | ((s[1] & 0x3f) << 6) 674231200Smm | (s[2] & 0x3f); 675231200Smm if (wc < 0x800) 676231200Smm return (-1);/* Overlong sequence. */ 677231200Smm break; 678231200Smm case 4: /* 4 bytes sequence. */ 679231200Smm if (n < 4) 680231200Smm return (-1); 681231200Smm if ((s[1] & 0xc0) != 0x80) return (-1); 682231200Smm if ((s[2] & 0xc0) != 0x80) return (-1); 683231200Smm if ((s[3] & 0xc0) != 0x80) return (-1); 684231200Smm wc = ((ch & 0x07) << 18) 685231200Smm | ((s[1] & 0x3f) << 12) 686231200Smm | ((s[2] & 0x3f) << 6) 687231200Smm | (s[3] & 0x3f); 688231200Smm if (wc < 0x10000) 689231200Smm return (-1);/* Overlong sequence. */ 690231200Smm break; 691231200Smm default: 692231200Smm return (-1); 693231200Smm } 694231200Smm 695309587Smm /* The code point larger than 0x10FFFF is not legal 696231200Smm * Unicode values. */ 697231200Smm if (wc > 0x10FFFF) 698231200Smm return (-1); 699231200Smm /* Correctly gets a Unicode, returns used bytes. */ 700231200Smm *pwc = wc; 701231200Smm return (cnt); 702231200Smm} 703231200Smm 704231200Smmstatic void strdump(const char *e, const char *p, int ewidth, int utf8) 705231200Smm{ 706228753Smm const char *q = p; 707228753Smm 708231200Smm logprintf(" %*s = ", ewidth, e); 709228753Smm if (p == NULL) { 710231200Smm logprintf("NULL\n"); 711228753Smm return; 712228753Smm } 713228753Smm logprintf("\""); 714228753Smm while (*p != '\0') { 715228753Smm unsigned int c = 0xff & *p++; 716228753Smm switch (c) { 717299425Smm case '\a': logprintf("\\a"); break; 718299425Smm case '\b': logprintf("\\b"); break; 719299425Smm case '\n': logprintf("\\n"); break; 720299425Smm case '\r': logprintf("\\r"); break; 721228753Smm default: 722228753Smm if (c >= 32 && c < 127) 723228753Smm logprintf("%c", c); 724228753Smm else 725228753Smm logprintf("\\x%02X", c); 726228753Smm } 727228753Smm } 728228753Smm logprintf("\""); 729231200Smm logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q)); 730231200Smm 731231200Smm /* 732231200Smm * If the current string is UTF-8, dump its code points. 733231200Smm */ 734231200Smm if (utf8) { 735231200Smm size_t len; 736231200Smm uint32_t uc; 737231200Smm int n; 738231200Smm int cnt = 0; 739231200Smm 740231200Smm p = q; 741231200Smm len = strlen(p); 742231200Smm logprintf(" ["); 743231200Smm while ((n = _utf8_to_unicode(&uc, p, len)) > 0) { 744231200Smm if (p != q) 745231200Smm logprintf(" "); 746231200Smm logprintf("%04X", uc); 747231200Smm p += n; 748231200Smm len -= n; 749231200Smm cnt++; 750231200Smm } 751231200Smm logprintf("]"); 752231200Smm logprintf(" (count %d", cnt); 753231200Smm if (n < 0) { 754358090Smm logprintf(",unknown %zu bytes", len); 755231200Smm } 756231200Smm logprintf(")"); 757231200Smm 758231200Smm } 759231200Smm logprintf("\n"); 760228753Smm} 761228753Smm 762228753Smm/* Verify two strings are equal, dump them if not. */ 763228753Smmint 764228753Smmassertion_equal_string(const char *file, int line, 765228753Smm const char *v1, const char *e1, 766228753Smm const char *v2, const char *e2, 767231200Smm void *extra, int utf8) 768228753Smm{ 769231200Smm int l1, l2; 770231200Smm 771228753Smm assertion_count(file, line); 772228753Smm if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0)) 773228753Smm return (1); 774228753Smm failure_start(file, line, "%s != %s", e1, e2); 775248590Smm l1 = (int)strlen(e1); 776248590Smm l2 = (int)strlen(e2); 777231200Smm if (l1 < l2) 778231200Smm l1 = l2; 779231200Smm strdump(e1, v1, l1, utf8); 780231200Smm strdump(e2, v2, l1, utf8); 781228753Smm failure_finish(extra); 782228753Smm return (0); 783228753Smm} 784228753Smm 785228753Smmstatic void 786228753Smmwcsdump(const char *e, const wchar_t *w) 787228753Smm{ 788228753Smm logprintf(" %s = ", e); 789228753Smm if (w == NULL) { 790228753Smm logprintf("(null)"); 791228753Smm return; 792228753Smm } 793228753Smm logprintf("\""); 794228753Smm while (*w != L'\0') { 795228753Smm unsigned int c = *w++; 796228753Smm if (c >= 32 && c < 127) 797228753Smm logprintf("%c", c); 798228753Smm else if (c < 256) 799228753Smm logprintf("\\x%02X", c); 800228753Smm else if (c < 0x10000) 801228753Smm logprintf("\\u%04X", c); 802228753Smm else 803228753Smm logprintf("\\U%08X", c); 804228753Smm } 805228753Smm logprintf("\"\n"); 806228753Smm} 807228753Smm 808228753Smm#ifndef HAVE_WCSCMP 809228753Smmstatic int 810228753Smmwcscmp(const wchar_t *s1, const wchar_t *s2) 811228753Smm{ 812228753Smm 813228753Smm while (*s1 == *s2++) { 814228753Smm if (*s1++ == L'\0') 815228753Smm return 0; 816228753Smm } 817228753Smm if (*s1 > *--s2) 818228753Smm return 1; 819228753Smm else 820228753Smm return -1; 821228753Smm} 822228753Smm#endif 823228753Smm 824228753Smm/* Verify that two wide strings are equal, dump them if not. */ 825228753Smmint 826228753Smmassertion_equal_wstring(const char *file, int line, 827228753Smm const wchar_t *v1, const char *e1, 828228753Smm const wchar_t *v2, const char *e2, 829228753Smm void *extra) 830228753Smm{ 831228753Smm assertion_count(file, line); 832231200Smm if (v1 == v2) 833228753Smm return (1); 834231200Smm if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0) 835231200Smm return (1); 836228753Smm failure_start(file, line, "%s != %s", e1, e2); 837228753Smm wcsdump(e1, v1); 838228753Smm wcsdump(e2, v2); 839228753Smm failure_finish(extra); 840228753Smm return (0); 841228753Smm} 842228753Smm 843228753Smm/* 844228753Smm * Pretty standard hexdump routine. As a bonus, if ref != NULL, then 845228753Smm * any bytes in p that differ from ref will be highlighted with '_' 846228753Smm * before and after the hex value. 847228753Smm */ 848228753Smmstatic void 849228753Smmhexdump(const char *p, const char *ref, size_t l, size_t offset) 850228753Smm{ 851228753Smm size_t i, j; 852228753Smm char sep; 853228753Smm 854228753Smm if (p == NULL) { 855228753Smm logprintf("(null)\n"); 856228753Smm return; 857228753Smm } 858228753Smm for(i=0; i < l; i+=16) { 859228753Smm logprintf("%04x", (unsigned)(i + offset)); 860228753Smm sep = ' '; 861228753Smm for (j = 0; j < 16 && i + j < l; j++) { 862228753Smm if (ref != NULL && p[i + j] != ref[i + j]) 863228753Smm sep = '_'; 864228753Smm logprintf("%c%02x", sep, 0xff & (int)p[i+j]); 865228753Smm if (ref != NULL && p[i + j] == ref[i + j]) 866228753Smm sep = ' '; 867228753Smm } 868228753Smm for (; j < 16; j++) { 869228753Smm logprintf("%c ", sep); 870228753Smm sep = ' '; 871228753Smm } 872228753Smm logprintf("%c", sep); 873228753Smm for (j=0; j < 16 && i + j < l; j++) { 874228753Smm int c = p[i + j]; 875228753Smm if (c >= ' ' && c <= 126) 876228753Smm logprintf("%c", c); 877228753Smm else 878228753Smm logprintf("."); 879228753Smm } 880228753Smm logprintf("\n"); 881228753Smm } 882228753Smm} 883228753Smm 884228753Smm/* Verify that two blocks of memory are the same, display the first 885228753Smm * block of differences if they're not. */ 886228753Smmint 887228753Smmassertion_equal_mem(const char *file, int line, 888228753Smm const void *_v1, const char *e1, 889228753Smm const void *_v2, const char *e2, 890228753Smm size_t l, const char *ld, void *extra) 891228753Smm{ 892228753Smm const char *v1 = (const char *)_v1; 893228753Smm const char *v2 = (const char *)_v2; 894228753Smm size_t offset; 895228753Smm 896228753Smm assertion_count(file, line); 897228753Smm if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0)) 898228753Smm return (1); 899248590Smm if (v1 == NULL || v2 == NULL) 900248590Smm return (0); 901228753Smm 902228753Smm failure_start(file, line, "%s != %s", e1, e2); 903228753Smm logprintf(" size %s = %d\n", ld, (int)l); 904228753Smm /* Dump 48 bytes (3 lines) so that the first difference is 905228753Smm * in the second line. */ 906228753Smm offset = 0; 907228753Smm while (l > 64 && memcmp(v1, v2, 32) == 0) { 908228753Smm /* Two lines agree, so step forward one line. */ 909228753Smm v1 += 16; 910228753Smm v2 += 16; 911228753Smm l -= 16; 912228753Smm offset += 16; 913228753Smm } 914228753Smm logprintf(" Dump of %s\n", e1); 915231200Smm hexdump(v1, v2, l < 128 ? l : 128, offset); 916228753Smm logprintf(" Dump of %s\n", e2); 917231200Smm hexdump(v2, v1, l < 128 ? l : 128, offset); 918228753Smm logprintf("\n"); 919228753Smm failure_finish(extra); 920228753Smm return (0); 921228753Smm} 922228753Smm 923299425Smm/* Verify that a block of memory is filled with the specified byte. */ 924299425Smmint 925299425Smmassertion_memory_filled_with(const char *file, int line, 926299425Smm const void *_v1, const char *vd, 927299425Smm size_t l, const char *ld, 928299425Smm char b, const char *bd, void *extra) 929299425Smm{ 930299425Smm const char *v1 = (const char *)_v1; 931299425Smm size_t c = 0; 932299425Smm size_t i; 933299425Smm (void)ld; /* UNUSED */ 934299425Smm 935299425Smm assertion_count(file, line); 936299425Smm 937299425Smm for (i = 0; i < l; ++i) { 938299425Smm if (v1[i] == b) { 939299425Smm ++c; 940299425Smm } 941299425Smm } 942299425Smm if (c == l) 943299425Smm return (1); 944299425Smm 945299425Smm failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd); 946299425Smm logprintf(" Only %d bytes were correct\n", (int)c); 947299425Smm failure_finish(extra); 948299425Smm return (0); 949299425Smm} 950299425Smm 951228753Smm/* Verify that the named file exists and is empty. */ 952228753Smmint 953231200Smmassertion_empty_file(const char *filename, int line, const char *f1) 954228753Smm{ 955228753Smm char buff[1024]; 956228753Smm struct stat st; 957228753Smm ssize_t s; 958228753Smm FILE *f; 959228753Smm 960231200Smm assertion_count(filename, line); 961228753Smm 962228753Smm if (stat(f1, &st) != 0) { 963231200Smm failure_start(filename, line, "Stat failed: %s", f1); 964228753Smm failure_finish(NULL); 965228753Smm return (0); 966228753Smm } 967228753Smm if (st.st_size == 0) 968228753Smm return (1); 969228753Smm 970231200Smm failure_start(filename, line, "File should be empty: %s", f1); 971228753Smm logprintf(" File size: %d\n", (int)st.st_size); 972228753Smm logprintf(" Contents:\n"); 973228753Smm f = fopen(f1, "rb"); 974228753Smm if (f == NULL) { 975228753Smm logprintf(" Unable to open %s\n", f1); 976228753Smm } else { 977228753Smm s = ((off_t)sizeof(buff) < st.st_size) ? 978228753Smm (ssize_t)sizeof(buff) : (ssize_t)st.st_size; 979228753Smm s = fread(buff, 1, s, f); 980228753Smm hexdump(buff, NULL, s, 0); 981228753Smm fclose(f); 982228753Smm } 983228753Smm failure_finish(NULL); 984228753Smm return (0); 985228753Smm} 986228753Smm 987228753Smm/* Verify that the named file exists and is not empty. */ 988228753Smmint 989231200Smmassertion_non_empty_file(const char *filename, int line, const char *f1) 990228753Smm{ 991228753Smm struct stat st; 992228753Smm 993231200Smm assertion_count(filename, line); 994228753Smm 995228753Smm if (stat(f1, &st) != 0) { 996231200Smm failure_start(filename, line, "Stat failed: %s", f1); 997228753Smm failure_finish(NULL); 998228753Smm return (0); 999228753Smm } 1000228753Smm if (st.st_size == 0) { 1001231200Smm failure_start(filename, line, "File empty: %s", f1); 1002228753Smm failure_finish(NULL); 1003228753Smm return (0); 1004228753Smm } 1005228753Smm return (1); 1006228753Smm} 1007228753Smm 1008228753Smm/* Verify that two files have the same contents. */ 1009228753Smm/* TODO: hexdump the first bytes that actually differ. */ 1010228753Smmint 1011231200Smmassertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2) 1012228753Smm{ 1013228753Smm char buff1[1024]; 1014228753Smm char buff2[1024]; 1015228753Smm FILE *f1, *f2; 1016228753Smm int n1, n2; 1017228753Smm 1018231200Smm assertion_count(filename, line); 1019228753Smm 1020228753Smm f1 = fopen(fn1, "rb"); 1021228753Smm f2 = fopen(fn2, "rb"); 1022248590Smm if (f1 == NULL || f2 == NULL) { 1023248590Smm if (f1) fclose(f1); 1024248590Smm if (f2) fclose(f2); 1025248590Smm return (0); 1026248590Smm } 1027228753Smm for (;;) { 1028248590Smm n1 = (int)fread(buff1, 1, sizeof(buff1), f1); 1029248590Smm n2 = (int)fread(buff2, 1, sizeof(buff2), f2); 1030228753Smm if (n1 != n2) 1031228753Smm break; 1032228753Smm if (n1 == 0 && n2 == 0) { 1033228753Smm fclose(f1); 1034228753Smm fclose(f2); 1035228753Smm return (1); 1036228753Smm } 1037228753Smm if (memcmp(buff1, buff2, n1) != 0) 1038228753Smm break; 1039228753Smm } 1040228753Smm fclose(f1); 1041228753Smm fclose(f2); 1042231200Smm failure_start(filename, line, "Files not identical"); 1043228753Smm logprintf(" file1=\"%s\"\n", fn1); 1044228753Smm logprintf(" file2=\"%s\"\n", fn2); 1045231200Smm failure_finish(NULL); 1046228753Smm return (0); 1047228753Smm} 1048228753Smm 1049228753Smm/* Verify that the named file does exist. */ 1050228753Smmint 1051231200Smmassertion_file_exists(const char *filename, int line, const char *f) 1052228753Smm{ 1053231200Smm assertion_count(filename, line); 1054228753Smm 1055228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1056228753Smm if (!_access(f, 0)) 1057228753Smm return (1); 1058228753Smm#else 1059228753Smm if (!access(f, F_OK)) 1060228753Smm return (1); 1061228753Smm#endif 1062231200Smm failure_start(filename, line, "File should exist: %s", f); 1063231200Smm failure_finish(NULL); 1064228753Smm return (0); 1065228753Smm} 1066228753Smm 1067228753Smm/* Verify that the named file doesn't exist. */ 1068228753Smmint 1069231200Smmassertion_file_not_exists(const char *filename, int line, const char *f) 1070228753Smm{ 1071231200Smm assertion_count(filename, line); 1072228753Smm 1073228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1074228753Smm if (_access(f, 0)) 1075228753Smm return (1); 1076228753Smm#else 1077228753Smm if (access(f, F_OK)) 1078228753Smm return (1); 1079228753Smm#endif 1080231200Smm failure_start(filename, line, "File should not exist: %s", f); 1081231200Smm failure_finish(NULL); 1082228753Smm return (0); 1083228753Smm} 1084228753Smm 1085228753Smm/* Compare the contents of a file to a block of memory. */ 1086228753Smmint 1087231200Smmassertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn) 1088228753Smm{ 1089228753Smm char *contents; 1090228753Smm FILE *f; 1091228753Smm int n; 1092228753Smm 1093231200Smm assertion_count(filename, line); 1094228753Smm 1095228753Smm f = fopen(fn, "rb"); 1096228753Smm if (f == NULL) { 1097231200Smm failure_start(filename, line, 1098228753Smm "File should exist: %s", fn); 1099231200Smm failure_finish(NULL); 1100228753Smm return (0); 1101228753Smm } 1102228753Smm contents = malloc(s * 2); 1103248590Smm n = (int)fread(contents, 1, s * 2, f); 1104228753Smm fclose(f); 1105228753Smm if (n == s && memcmp(buff, contents, s) == 0) { 1106228753Smm free(contents); 1107228753Smm return (1); 1108228753Smm } 1109231200Smm failure_start(filename, line, "File contents don't match"); 1110228753Smm logprintf(" file=\"%s\"\n", fn); 1111228753Smm if (n > 0) 1112228753Smm hexdump(contents, buff, n > 512 ? 512 : n, 0); 1113228753Smm else { 1114228753Smm logprintf(" File empty, contents should be:\n"); 1115231200Smm hexdump(buff, NULL, s > 512 ? 512 : s, 0); 1116228753Smm } 1117231200Smm failure_finish(NULL); 1118228753Smm free(contents); 1119228753Smm return (0); 1120228753Smm} 1121228753Smm 1122228753Smm/* Check the contents of a text file, being tolerant of line endings. */ 1123228753Smmint 1124231200Smmassertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn) 1125228753Smm{ 1126228753Smm char *contents; 1127228753Smm const char *btxt, *ftxt; 1128228753Smm FILE *f; 1129228753Smm int n, s; 1130228753Smm 1131231200Smm assertion_count(filename, line); 1132228753Smm f = fopen(fn, "r"); 1133231200Smm if (f == NULL) { 1134231200Smm failure_start(filename, line, 1135231200Smm "File doesn't exist: %s", fn); 1136231200Smm failure_finish(NULL); 1137231200Smm return (0); 1138231200Smm } 1139248590Smm s = (int)strlen(buff); 1140228753Smm contents = malloc(s * 2 + 128); 1141248590Smm n = (int)fread(contents, 1, s * 2 + 128 - 1, f); 1142228753Smm if (n >= 0) 1143228753Smm contents[n] = '\0'; 1144228753Smm fclose(f); 1145228753Smm /* Compare texts. */ 1146228753Smm btxt = buff; 1147228753Smm ftxt = (const char *)contents; 1148228753Smm while (*btxt != '\0' && *ftxt != '\0') { 1149228753Smm if (*btxt == *ftxt) { 1150228753Smm ++btxt; 1151228753Smm ++ftxt; 1152228753Smm continue; 1153228753Smm } 1154228753Smm if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') { 1155228753Smm /* Pass over different new line characters. */ 1156228753Smm ++btxt; 1157228753Smm ftxt += 2; 1158228753Smm continue; 1159228753Smm } 1160228753Smm break; 1161228753Smm } 1162228753Smm if (*btxt == '\0' && *ftxt == '\0') { 1163228753Smm free(contents); 1164228753Smm return (1); 1165228753Smm } 1166231200Smm failure_start(filename, line, "Contents don't match"); 1167228753Smm logprintf(" file=\"%s\"\n", fn); 1168231200Smm if (n > 0) { 1169228753Smm hexdump(contents, buff, n, 0); 1170358090Smm logprintf(" expected\n"); 1171231200Smm hexdump(buff, contents, s, 0); 1172231200Smm } else { 1173228753Smm logprintf(" File empty, contents should be:\n"); 1174228753Smm hexdump(buff, NULL, s, 0); 1175228753Smm } 1176231200Smm failure_finish(NULL); 1177228753Smm free(contents); 1178228753Smm return (0); 1179228753Smm} 1180228753Smm 1181231200Smm/* Verify that a text file contains the specified lines, regardless of order */ 1182231200Smm/* This could be more efficient if we sorted both sets of lines, etc, but 1183231200Smm * since this is used only for testing and only ever deals with a dozen or so 1184231200Smm * lines at a time, this relatively crude approach is just fine. */ 1185231200Smmint 1186231200Smmassertion_file_contains_lines_any_order(const char *file, int line, 1187231200Smm const char *pathname, const char *lines[]) 1188231200Smm{ 1189231200Smm char *buff; 1190231200Smm size_t buff_size; 1191231200Smm size_t expected_count, actual_count, i, j; 1192248590Smm char **expected = NULL; 1193248590Smm char *p, **actual = NULL; 1194231200Smm char c; 1195231200Smm int expected_failure = 0, actual_failure = 0; 1196231200Smm 1197231200Smm assertion_count(file, line); 1198231200Smm 1199231200Smm buff = slurpfile(&buff_size, "%s", pathname); 1200231200Smm if (buff == NULL) { 1201231200Smm failure_start(pathname, line, "Can't read file: %s", pathname); 1202231200Smm failure_finish(NULL); 1203231200Smm return (0); 1204231200Smm } 1205231200Smm 1206248590Smm /* Make a copy of the provided lines and count up the expected 1207248590Smm * file size. */ 1208231200Smm for (i = 0; lines[i] != NULL; ++i) { 1209231200Smm } 1210231200Smm expected_count = i; 1211248590Smm if (expected_count) { 1212248590Smm expected = malloc(sizeof(char *) * expected_count); 1213248590Smm if (expected == NULL) { 1214248590Smm failure_start(pathname, line, "Can't allocate memory"); 1215248590Smm failure_finish(NULL); 1216299425Smm free(expected); 1217318483Smm free(buff); 1218248590Smm return (0); 1219248590Smm } 1220248590Smm for (i = 0; lines[i] != NULL; ++i) { 1221248590Smm expected[i] = strdup(lines[i]); 1222248590Smm } 1223231200Smm } 1224231200Smm 1225231200Smm /* Break the file into lines */ 1226231200Smm actual_count = 0; 1227231200Smm for (c = '\0', p = buff; p < buff + buff_size; ++p) { 1228231200Smm if (*p == '\x0d' || *p == '\x0a') 1229231200Smm *p = '\0'; 1230231200Smm if (c == '\0' && *p != '\0') 1231231200Smm ++actual_count; 1232231200Smm c = *p; 1233231200Smm } 1234248590Smm if (actual_count) { 1235248590Smm actual = calloc(sizeof(char *), actual_count); 1236248590Smm if (actual == NULL) { 1237248590Smm failure_start(pathname, line, "Can't allocate memory"); 1238248590Smm failure_finish(NULL); 1239248590Smm free(expected); 1240318483Smm free(buff); 1241248590Smm return (0); 1242231200Smm } 1243248590Smm for (j = 0, p = buff; p < buff + buff_size; 1244248590Smm p += 1 + strlen(p)) { 1245248590Smm if (*p != '\0') { 1246248590Smm actual[j] = p; 1247248590Smm ++j; 1248248590Smm } 1249248590Smm } 1250231200Smm } 1251231200Smm 1252231200Smm /* Erase matching lines from both lists */ 1253231200Smm for (i = 0; i < expected_count; ++i) { 1254231200Smm if (expected[i] == NULL) 1255231200Smm continue; 1256231200Smm for (j = 0; j < actual_count; ++j) { 1257231200Smm if (actual[j] == NULL) 1258231200Smm continue; 1259231200Smm if (strcmp(expected[i], actual[j]) == 0) { 1260231200Smm free(expected[i]); 1261231200Smm expected[i] = NULL; 1262231200Smm actual[j] = NULL; 1263231200Smm break; 1264231200Smm } 1265231200Smm } 1266231200Smm } 1267231200Smm 1268231200Smm /* If there's anything left, it's a failure */ 1269231200Smm for (i = 0; i < expected_count; ++i) { 1270231200Smm if (expected[i] != NULL) 1271231200Smm ++expected_failure; 1272231200Smm } 1273231200Smm for (j = 0; j < actual_count; ++j) { 1274231200Smm if (actual[j] != NULL) 1275231200Smm ++actual_failure; 1276231200Smm } 1277231200Smm if (expected_failure == 0 && actual_failure == 0) { 1278231200Smm free(buff); 1279231200Smm free(expected); 1280231200Smm free(actual); 1281231200Smm return (1); 1282231200Smm } 1283231200Smm failure_start(file, line, "File doesn't match: %s", pathname); 1284231200Smm for (i = 0; i < expected_count; ++i) { 1285231200Smm if (expected[i] != NULL) { 1286231200Smm logprintf(" Expected but not present: %s\n", expected[i]); 1287231200Smm free(expected[i]); 1288231200Smm } 1289231200Smm } 1290231200Smm for (j = 0; j < actual_count; ++j) { 1291231200Smm if (actual[j] != NULL) 1292231200Smm logprintf(" Present but not expected: %s\n", actual[j]); 1293231200Smm } 1294231200Smm failure_finish(NULL); 1295231200Smm free(buff); 1296231200Smm free(expected); 1297231200Smm free(actual); 1298231200Smm return (0); 1299231200Smm} 1300231200Smm 1301307859Smm/* Verify that a text file does not contains the specified strings */ 1302307859Smmint 1303307859Smmassertion_file_contains_no_invalid_strings(const char *file, int line, 1304307859Smm const char *pathname, const char *strings[]) 1305307859Smm{ 1306307859Smm char *buff; 1307307859Smm int i; 1308307859Smm 1309307859Smm buff = slurpfile(NULL, "%s", pathname); 1310307859Smm if (buff == NULL) { 1311307859Smm failure_start(file, line, "Can't read file: %s", pathname); 1312307859Smm failure_finish(NULL); 1313307859Smm return (0); 1314307859Smm } 1315307859Smm 1316307859Smm for (i = 0; strings[i] != NULL; ++i) { 1317307859Smm if (strstr(buff, strings[i]) != NULL) { 1318307859Smm failure_start(file, line, "Invalid string in %s: %s", pathname, 1319307859Smm strings[i]); 1320307859Smm failure_finish(NULL); 1321307859Smm free(buff); 1322307859Smm return(0); 1323307859Smm } 1324307859Smm } 1325307859Smm 1326307859Smm free(buff); 1327307859Smm return (0); 1328307859Smm} 1329307859Smm 1330228753Smm/* Test that two paths point to the same file. */ 1331228753Smm/* As a side-effect, asserts that both files exist. */ 1332228753Smmstatic int 1333228753Smmis_hardlink(const char *file, int line, 1334228753Smm const char *path1, const char *path2) 1335228753Smm{ 1336228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1337228753Smm BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2; 1338228753Smm int r; 1339228753Smm 1340228753Smm assertion_count(file, line); 1341228753Smm r = my_GetFileInformationByName(path1, &bhfi1); 1342228753Smm if (r == 0) { 1343228753Smm failure_start(file, line, "File %s can't be inspected?", path1); 1344228753Smm failure_finish(NULL); 1345228753Smm return (0); 1346228753Smm } 1347228753Smm r = my_GetFileInformationByName(path2, &bhfi2); 1348228753Smm if (r == 0) { 1349228753Smm failure_start(file, line, "File %s can't be inspected?", path2); 1350228753Smm failure_finish(NULL); 1351228753Smm return (0); 1352228753Smm } 1353228753Smm return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber 1354228753Smm && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh 1355228753Smm && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow); 1356228753Smm#else 1357228753Smm struct stat st1, st2; 1358228753Smm int r; 1359228753Smm 1360228753Smm assertion_count(file, line); 1361228753Smm r = lstat(path1, &st1); 1362228753Smm if (r != 0) { 1363228753Smm failure_start(file, line, "File should exist: %s", path1); 1364228753Smm failure_finish(NULL); 1365228753Smm return (0); 1366228753Smm } 1367228753Smm r = lstat(path2, &st2); 1368228753Smm if (r != 0) { 1369228753Smm failure_start(file, line, "File should exist: %s", path2); 1370228753Smm failure_finish(NULL); 1371228753Smm return (0); 1372228753Smm } 1373228753Smm return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev); 1374228753Smm#endif 1375228753Smm} 1376228753Smm 1377228753Smmint 1378228753Smmassertion_is_hardlink(const char *file, int line, 1379228753Smm const char *path1, const char *path2) 1380228753Smm{ 1381228753Smm if (is_hardlink(file, line, path1, path2)) 1382228753Smm return (1); 1383228753Smm failure_start(file, line, 1384228753Smm "Files %s and %s are not hardlinked", path1, path2); 1385228753Smm failure_finish(NULL); 1386228753Smm return (0); 1387228753Smm} 1388228753Smm 1389228753Smmint 1390228753Smmassertion_is_not_hardlink(const char *file, int line, 1391228753Smm const char *path1, const char *path2) 1392228753Smm{ 1393228753Smm if (!is_hardlink(file, line, path1, path2)) 1394228753Smm return (1); 1395228753Smm failure_start(file, line, 1396228753Smm "Files %s and %s should not be hardlinked", path1, path2); 1397228753Smm failure_finish(NULL); 1398228753Smm return (0); 1399228753Smm} 1400228753Smm 1401228753Smm/* Verify a/b/mtime of 'pathname'. */ 1402228753Smm/* If 'recent', verify that it's within last 10 seconds. */ 1403228753Smmstatic int 1404228753Smmassertion_file_time(const char *file, int line, 1405228753Smm const char *pathname, long t, long nsec, char type, int recent) 1406228753Smm{ 1407228753Smm long long filet, filet_nsec; 1408228753Smm int r; 1409228753Smm 1410228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1411228753Smm#define EPOC_TIME (116444736000000000ULL) 1412248590Smm FILETIME fxtime, fbirthtime, fatime, fmtime; 1413228753Smm ULARGE_INTEGER wintm; 1414228753Smm HANDLE h; 1415248590Smm fxtime.dwLowDateTime = 0; 1416248590Smm fxtime.dwHighDateTime = 0; 1417228753Smm 1418228753Smm assertion_count(file, line); 1419231200Smm /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open 1420231200Smm * a directory file. If not, CreateFile() will fail when 1421231200Smm * the pathname is a directory. */ 1422228753Smm h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL, 1423231200Smm OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 1424228753Smm if (h == INVALID_HANDLE_VALUE) { 1425228753Smm failure_start(file, line, "Can't access %s\n", pathname); 1426228753Smm failure_finish(NULL); 1427228753Smm return (0); 1428228753Smm } 1429228753Smm r = GetFileTime(h, &fbirthtime, &fatime, &fmtime); 1430228753Smm switch (type) { 1431248590Smm case 'a': fxtime = fatime; break; 1432248590Smm case 'b': fxtime = fbirthtime; break; 1433248590Smm case 'm': fxtime = fmtime; break; 1434228753Smm } 1435228753Smm CloseHandle(h); 1436228753Smm if (r == 0) { 1437228753Smm failure_start(file, line, "Can't GetFileTime %s\n", pathname); 1438228753Smm failure_finish(NULL); 1439228753Smm return (0); 1440228753Smm } 1441248590Smm wintm.LowPart = fxtime.dwLowDateTime; 1442248590Smm wintm.HighPart = fxtime.dwHighDateTime; 1443228753Smm filet = (wintm.QuadPart - EPOC_TIME) / 10000000; 1444228753Smm filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100; 1445228753Smm nsec = (nsec / 100) * 100; /* Round the request */ 1446228753Smm#else 1447228753Smm struct stat st; 1448228753Smm 1449228753Smm assertion_count(file, line); 1450228753Smm r = lstat(pathname, &st); 1451228753Smm if (r != 0) { 1452228753Smm failure_start(file, line, "Can't stat %s\n", pathname); 1453228753Smm failure_finish(NULL); 1454228753Smm return (0); 1455228753Smm } 1456228753Smm switch (type) { 1457228753Smm case 'a': filet = st.st_atime; break; 1458228753Smm case 'm': filet = st.st_mtime; break; 1459228753Smm case 'b': filet = 0; break; 1460228753Smm default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1461228753Smm exit(1); 1462228753Smm } 1463228753Smm#if defined(__FreeBSD__) 1464228753Smm switch (type) { 1465228753Smm case 'a': filet_nsec = st.st_atimespec.tv_nsec; break; 1466228753Smm case 'b': filet = st.st_birthtime; 1467302264Smm /* FreeBSD filesystems that don't support birthtime 1468302264Smm * (e.g., UFS1) always return -1 here. */ 1469302264Smm if (filet == -1) { 1470302264Smm return (1); 1471302264Smm } 1472228753Smm filet_nsec = st.st_birthtimespec.tv_nsec; break; 1473228753Smm case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break; 1474228753Smm default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1475228753Smm exit(1); 1476228753Smm } 1477228753Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1478228753Smm filet_nsec = (filet_nsec / 1000) * 1000; 1479228753Smm nsec = (nsec / 1000) * 1000; 1480228753Smm#else 1481228753Smm filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */ 1482228753Smm if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */ 1483228753Smm#if defined(__HAIKU__) 1484228753Smm if (type == 'a') return (1); /* Haiku doesn't have atime. */ 1485228753Smm#endif 1486228753Smm#endif 1487228753Smm#endif 1488228753Smm if (recent) { 1489228753Smm /* Check that requested time is up-to-date. */ 1490228753Smm time_t now = time(NULL); 1491228753Smm if (filet < now - 10 || filet > now + 1) { 1492228753Smm failure_start(file, line, 1493231200Smm "File %s has %ctime %lld, %lld seconds ago\n", 1494228753Smm pathname, type, filet, now - filet); 1495228753Smm failure_finish(NULL); 1496228753Smm return (0); 1497228753Smm } 1498228753Smm } else if (filet != t || filet_nsec != nsec) { 1499228753Smm failure_start(file, line, 1500358090Smm "File %s has %ctime %lld.%09lld, expected %ld.%09ld", 1501228753Smm pathname, type, filet, filet_nsec, t, nsec); 1502228753Smm failure_finish(NULL); 1503228753Smm return (0); 1504228753Smm } 1505228753Smm return (1); 1506228753Smm} 1507228753Smm 1508228753Smm/* Verify atime of 'pathname'. */ 1509228753Smmint 1510228753Smmassertion_file_atime(const char *file, int line, 1511228753Smm const char *pathname, long t, long nsec) 1512228753Smm{ 1513228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'a', 0); 1514228753Smm} 1515228753Smm 1516228753Smm/* Verify atime of 'pathname' is up-to-date. */ 1517228753Smmint 1518228753Smmassertion_file_atime_recent(const char *file, int line, const char *pathname) 1519228753Smm{ 1520228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'a', 1); 1521228753Smm} 1522228753Smm 1523228753Smm/* Verify birthtime of 'pathname'. */ 1524228753Smmint 1525228753Smmassertion_file_birthtime(const char *file, int line, 1526228753Smm const char *pathname, long t, long nsec) 1527228753Smm{ 1528228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'b', 0); 1529228753Smm} 1530228753Smm 1531228753Smm/* Verify birthtime of 'pathname' is up-to-date. */ 1532228753Smmint 1533228753Smmassertion_file_birthtime_recent(const char *file, int line, 1534228753Smm const char *pathname) 1535228753Smm{ 1536228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'b', 1); 1537228753Smm} 1538228753Smm 1539307214Smm/* Verify mode of 'pathname'. */ 1540307214Smmint 1541307214Smmassertion_file_mode(const char *file, int line, const char *pathname, int expected_mode) 1542307214Smm{ 1543307214Smm int mode; 1544307214Smm int r; 1545307214Smm 1546307214Smm assertion_count(file, line); 1547307214Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1548307214Smm failure_start(file, line, "assertFileMode not yet implemented for Windows"); 1549307859Smm (void)mode; /* UNUSED */ 1550307859Smm (void)r; /* UNUSED */ 1551313071Smm (void)pathname; /* UNUSED */ 1552313071Smm (void)expected_mode; /* UNUSED */ 1553307214Smm#else 1554307214Smm { 1555307214Smm struct stat st; 1556307214Smm r = lstat(pathname, &st); 1557307214Smm mode = (int)(st.st_mode & 0777); 1558307214Smm } 1559307214Smm if (r == 0 && mode == expected_mode) 1560307214Smm return (1); 1561307214Smm failure_start(file, line, "File %s has mode %o, expected %o", 1562307214Smm pathname, mode, expected_mode); 1563307214Smm#endif 1564307214Smm failure_finish(NULL); 1565307214Smm return (0); 1566307214Smm} 1567307214Smm 1568228753Smm/* Verify mtime of 'pathname'. */ 1569228753Smmint 1570228753Smmassertion_file_mtime(const char *file, int line, 1571228753Smm const char *pathname, long t, long nsec) 1572228753Smm{ 1573228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'm', 0); 1574228753Smm} 1575228753Smm 1576228753Smm/* Verify mtime of 'pathname' is up-to-date. */ 1577228753Smmint 1578228753Smmassertion_file_mtime_recent(const char *file, int line, const char *pathname) 1579228753Smm{ 1580228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'm', 1); 1581228753Smm} 1582228753Smm 1583228753Smm/* Verify number of links to 'pathname'. */ 1584228753Smmint 1585228753Smmassertion_file_nlinks(const char *file, int line, 1586228753Smm const char *pathname, int nlinks) 1587228753Smm{ 1588228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1589228753Smm BY_HANDLE_FILE_INFORMATION bhfi; 1590228753Smm int r; 1591228753Smm 1592228753Smm assertion_count(file, line); 1593228753Smm r = my_GetFileInformationByName(pathname, &bhfi); 1594228753Smm if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks) 1595228753Smm return (1); 1596358090Smm failure_start(file, line, "File %s has %jd links, expected %d", 1597358090Smm pathname, (intmax_t)bhfi.nNumberOfLinks, nlinks); 1598228753Smm failure_finish(NULL); 1599228753Smm return (0); 1600228753Smm#else 1601228753Smm struct stat st; 1602228753Smm int r; 1603228753Smm 1604228753Smm assertion_count(file, line); 1605228753Smm r = lstat(pathname, &st); 1606231297Smm if (r == 0 && (int)st.st_nlink == nlinks) 1607307214Smm return (1); 1608358090Smm failure_start(file, line, "File %s has %jd links, expected %d", 1609358090Smm pathname, (intmax_t)st.st_nlink, nlinks); 1610228753Smm failure_finish(NULL); 1611228753Smm return (0); 1612228753Smm#endif 1613228753Smm} 1614228753Smm 1615228753Smm/* Verify size of 'pathname'. */ 1616228753Smmint 1617228753Smmassertion_file_size(const char *file, int line, const char *pathname, long size) 1618228753Smm{ 1619228753Smm int64_t filesize; 1620228753Smm int r; 1621228753Smm 1622228753Smm assertion_count(file, line); 1623228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1624228753Smm { 1625228753Smm BY_HANDLE_FILE_INFORMATION bhfi; 1626228753Smm r = !my_GetFileInformationByName(pathname, &bhfi); 1627228753Smm filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow; 1628228753Smm } 1629228753Smm#else 1630228753Smm { 1631228753Smm struct stat st; 1632228753Smm r = lstat(pathname, &st); 1633228753Smm filesize = st.st_size; 1634228753Smm } 1635228753Smm#endif 1636228753Smm if (r == 0 && filesize == size) 1637228753Smm return (1); 1638228753Smm failure_start(file, line, "File %s has size %ld, expected %ld", 1639228753Smm pathname, (long)filesize, (long)size); 1640228753Smm failure_finish(NULL); 1641228753Smm return (0); 1642228753Smm} 1643228753Smm 1644228753Smm/* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */ 1645228753Smmint 1646228753Smmassertion_is_dir(const char *file, int line, const char *pathname, int mode) 1647228753Smm{ 1648228753Smm struct stat st; 1649228753Smm int r; 1650228753Smm 1651228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1652228753Smm (void)mode; /* UNUSED */ 1653228753Smm#endif 1654228753Smm assertion_count(file, line); 1655228753Smm r = lstat(pathname, &st); 1656228753Smm if (r != 0) { 1657228753Smm failure_start(file, line, "Dir should exist: %s", pathname); 1658228753Smm failure_finish(NULL); 1659228753Smm return (0); 1660228753Smm } 1661228753Smm if (!S_ISDIR(st.st_mode)) { 1662228753Smm failure_start(file, line, "%s is not a dir", pathname); 1663228753Smm failure_finish(NULL); 1664228753Smm return (0); 1665228753Smm } 1666228753Smm#if !defined(_WIN32) || defined(__CYGWIN__) 1667228753Smm /* Windows doesn't handle permissions the same way as POSIX, 1668228753Smm * so just ignore the mode tests. */ 1669228753Smm /* TODO: Can we do better here? */ 1670231297Smm if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1671228753Smm failure_start(file, line, "Dir %s has wrong mode", pathname); 1672228753Smm logprintf(" Expected: 0%3o\n", mode); 1673228753Smm logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1674228753Smm failure_finish(NULL); 1675228753Smm return (0); 1676228753Smm } 1677228753Smm#endif 1678228753Smm return (1); 1679228753Smm} 1680228753Smm 1681228753Smm/* Verify that 'pathname' is a regular file. If 'mode' is >= 0, 1682228753Smm * verify that too. */ 1683228753Smmint 1684228753Smmassertion_is_reg(const char *file, int line, const char *pathname, int mode) 1685228753Smm{ 1686228753Smm struct stat st; 1687228753Smm int r; 1688228753Smm 1689228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1690228753Smm (void)mode; /* UNUSED */ 1691228753Smm#endif 1692228753Smm assertion_count(file, line); 1693228753Smm r = lstat(pathname, &st); 1694228753Smm if (r != 0 || !S_ISREG(st.st_mode)) { 1695228753Smm failure_start(file, line, "File should exist: %s", pathname); 1696228753Smm failure_finish(NULL); 1697228753Smm return (0); 1698228753Smm } 1699228753Smm#if !defined(_WIN32) || defined(__CYGWIN__) 1700228753Smm /* Windows doesn't handle permissions the same way as POSIX, 1701228753Smm * so just ignore the mode tests. */ 1702228753Smm /* TODO: Can we do better here? */ 1703231297Smm if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1704228753Smm failure_start(file, line, "File %s has wrong mode", pathname); 1705228753Smm logprintf(" Expected: 0%3o\n", mode); 1706228753Smm logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1707228753Smm failure_finish(NULL); 1708228753Smm return (0); 1709228753Smm } 1710228753Smm#endif 1711228753Smm return (1); 1712228753Smm} 1713228753Smm 1714348608Smm/* 1715348608Smm * Check whether 'pathname' is a symbolic link. If 'contents' is 1716348608Smm * non-NULL, verify that the symlink has those contents. 1717348608Smm * 1718348608Smm * On platforms with directory symlinks, set isdir to 0 to test for a file 1719348608Smm * symlink and to 1 to test for a directory symlink. On other platforms 1720348608Smm * the variable is ignored. 1721348608Smm */ 1722228753Smmstatic int 1723228753Smmis_symlink(const char *file, int line, 1724348608Smm const char *pathname, const char *contents, int isdir) 1725228753Smm{ 1726228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1727348608Smm HANDLE h; 1728348608Smm DWORD inbytes; 1729348608Smm REPARSE_DATA_BUFFER *buf; 1730348608Smm BY_HANDLE_FILE_INFORMATION st; 1731348608Smm size_t len, len2; 1732348608Smm wchar_t *linknamew, *contentsw; 1733348608Smm const char *p; 1734348608Smm char *s, *pn; 1735348608Smm int ret = 0; 1736348608Smm BYTE *indata; 1737348608Smm const DWORD flag = FILE_FLAG_BACKUP_SEMANTICS | 1738348608Smm FILE_FLAG_OPEN_REPARSE_POINT; 1739348608Smm 1740348608Smm /* Replace slashes with backslashes in pathname */ 1741348608Smm pn = malloc((strlen(pathname) + 1) * sizeof(char)); 1742348608Smm p = pathname; 1743348608Smm s = pn; 1744348608Smm while(*p != '\0') { 1745348608Smm if(*p == '/') 1746348608Smm *s = '\\'; 1747348608Smm else 1748348608Smm *s = *p; 1749348608Smm p++; 1750348608Smm s++; 1751348608Smm } 1752348608Smm *s = '\0'; 1753348608Smm 1754348608Smm h = CreateFileA(pn, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 1755348608Smm flag, NULL); 1756348608Smm free(pn); 1757348608Smm if (h == INVALID_HANDLE_VALUE) { 1758348608Smm failure_start(file, line, "Can't access %s\n", pathname); 1759348608Smm failure_finish(NULL); 1760348608Smm return (0); 1761348608Smm } 1762348608Smm ret = GetFileInformationByHandle(h, &st); 1763348608Smm if (ret == 0) { 1764348608Smm failure_start(file, line, 1765348608Smm "Can't stat: %s", pathname); 1766348608Smm failure_finish(NULL); 1767348608Smm } else if ((st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) { 1768348608Smm failure_start(file, line, 1769348608Smm "Not a symlink: %s", pathname); 1770348608Smm failure_finish(NULL); 1771348608Smm ret = 0; 1772348608Smm } 1773348608Smm if (isdir && ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)) { 1774348608Smm failure_start(file, line, 1775348608Smm "Not a directory symlink: %s", pathname); 1776348608Smm failure_finish(NULL); 1777348608Smm ret = 0; 1778348608Smm } 1779348608Smm if (!isdir && 1780348608Smm ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) { 1781348608Smm failure_start(file, line, 1782348608Smm "Not a file symlink: %s", pathname); 1783348608Smm failure_finish(NULL); 1784348608Smm ret = 0; 1785348608Smm } 1786348608Smm if (ret == 0) { 1787348608Smm CloseHandle(h); 1788348608Smm return (0); 1789348608Smm } 1790348608Smm 1791348608Smm indata = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); 1792348608Smm ret = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, indata, 1793348608Smm 1024, &inbytes, NULL); 1794348608Smm CloseHandle(h); 1795348608Smm if (ret == 0) { 1796348608Smm free(indata); 1797348608Smm failure_start(file, line, 1798348608Smm "Could not retrieve symlink target: %s", pathname); 1799348608Smm failure_finish(NULL); 1800348608Smm return (0); 1801348608Smm } 1802348608Smm 1803348608Smm buf = (REPARSE_DATA_BUFFER *) indata; 1804348608Smm if (buf->ReparseTag != IO_REPARSE_TAG_SYMLINK) { 1805348608Smm free(indata); 1806348608Smm /* File is not a symbolic link */ 1807348608Smm failure_start(file, line, 1808348608Smm "Not a symlink: %s", pathname); 1809348608Smm failure_finish(NULL); 1810348608Smm return (0); 1811348608Smm } 1812348608Smm 1813348608Smm if (contents == NULL) { 1814348608Smm free(indata); 1815348608Smm return (1); 1816348608Smm } 1817348608Smm 1818348608Smm len = buf->SymbolicLinkReparseBuffer.SubstituteNameLength; 1819348608Smm 1820348608Smm linknamew = malloc(len + sizeof(wchar_t)); 1821348608Smm if (linknamew == NULL) { 1822348608Smm free(indata); 1823348608Smm return (0); 1824348608Smm } 1825348608Smm 1826348608Smm memcpy(linknamew, &((BYTE *)buf->SymbolicLinkReparseBuffer.PathBuffer) 1827348608Smm [buf->SymbolicLinkReparseBuffer.SubstituteNameOffset], len); 1828348608Smm free(indata); 1829348608Smm 1830348608Smm linknamew[len / sizeof(wchar_t)] = L'\0'; 1831348608Smm 1832348608Smm contentsw = malloc(len + sizeof(wchar_t)); 1833348608Smm if (contentsw == NULL) { 1834348608Smm free(linknamew); 1835348608Smm return (0); 1836348608Smm } 1837348608Smm 1838348608Smm len2 = mbsrtowcs(contentsw, &contents, (len + sizeof(wchar_t) 1839348608Smm / sizeof(wchar_t)), NULL); 1840348608Smm 1841348608Smm if (len2 > 0 && wcscmp(linknamew, contentsw) != 0) 1842348608Smm ret = 1; 1843348608Smm 1844348608Smm free(linknamew); 1845348608Smm free(contentsw); 1846348608Smm return (ret); 1847228753Smm#else 1848228753Smm char buff[300]; 1849228753Smm struct stat st; 1850228753Smm ssize_t linklen; 1851228753Smm int r; 1852228753Smm 1853348608Smm (void)isdir; /* UNUSED */ 1854228753Smm assertion_count(file, line); 1855228753Smm r = lstat(pathname, &st); 1856228753Smm if (r != 0) { 1857228753Smm failure_start(file, line, 1858228753Smm "Symlink should exist: %s", pathname); 1859228753Smm failure_finish(NULL); 1860228753Smm return (0); 1861228753Smm } 1862228753Smm if (!S_ISLNK(st.st_mode)) 1863228753Smm return (0); 1864228753Smm if (contents == NULL) 1865228753Smm return (1); 1866358090Smm linklen = readlink(pathname, buff, sizeof(buff) - 1); 1867228753Smm if (linklen < 0) { 1868228753Smm failure_start(file, line, "Can't read symlink %s", pathname); 1869228753Smm failure_finish(NULL); 1870228753Smm return (0); 1871228753Smm } 1872228753Smm buff[linklen] = '\0'; 1873228753Smm if (strcmp(buff, contents) != 0) 1874228753Smm return (0); 1875228753Smm return (1); 1876228753Smm#endif 1877228753Smm} 1878228753Smm 1879228753Smm/* Assert that path is a symlink that (optionally) contains contents. */ 1880228753Smmint 1881228753Smmassertion_is_symlink(const char *file, int line, 1882348608Smm const char *path, const char *contents, int isdir) 1883228753Smm{ 1884348608Smm if (is_symlink(file, line, path, contents, isdir)) 1885228753Smm return (1); 1886228753Smm if (contents) 1887228753Smm failure_start(file, line, "File %s is not a symlink to %s", 1888228753Smm path, contents); 1889228753Smm else 1890228753Smm failure_start(file, line, "File %s is not a symlink", path); 1891228753Smm failure_finish(NULL); 1892228753Smm return (0); 1893228753Smm} 1894228753Smm 1895228753Smm 1896228753Smm/* Create a directory and report any errors. */ 1897228753Smmint 1898228753Smmassertion_make_dir(const char *file, int line, const char *dirname, int mode) 1899228753Smm{ 1900228753Smm assertion_count(file, line); 1901228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1902228753Smm (void)mode; /* UNUSED */ 1903228753Smm if (0 == _mkdir(dirname)) 1904228753Smm return (1); 1905228753Smm#else 1906306669Smm if (0 == mkdir(dirname, mode)) { 1907306669Smm if (0 == chmod(dirname, mode)) { 1908306669Smm assertion_file_mode(file, line, dirname, mode); 1909306669Smm return (1); 1910306669Smm } 1911306669Smm } 1912228753Smm#endif 1913228753Smm failure_start(file, line, "Could not create directory %s", dirname); 1914228753Smm failure_finish(NULL); 1915228753Smm return(0); 1916228753Smm} 1917228753Smm 1918228753Smm/* Create a file with the specified contents and report any failures. */ 1919228753Smmint 1920228753Smmassertion_make_file(const char *file, int line, 1921238825Smm const char *path, int mode, int csize, const void *contents) 1922228753Smm{ 1923228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1924228753Smm /* TODO: Rework this to set file mode as well. */ 1925228753Smm FILE *f; 1926228753Smm (void)mode; /* UNUSED */ 1927228753Smm assertion_count(file, line); 1928228753Smm f = fopen(path, "wb"); 1929228753Smm if (f == NULL) { 1930228753Smm failure_start(file, line, "Could not create file %s", path); 1931228753Smm failure_finish(NULL); 1932228753Smm return (0); 1933228753Smm } 1934228753Smm if (contents != NULL) { 1935238825Smm size_t wsize; 1936238825Smm 1937238825Smm if (csize < 0) 1938238825Smm wsize = strlen(contents); 1939238825Smm else 1940238825Smm wsize = (size_t)csize; 1941238825Smm if (wsize != fwrite(contents, 1, wsize, f)) { 1942228753Smm fclose(f); 1943228753Smm failure_start(file, line, 1944228753Smm "Could not write file %s", path); 1945228753Smm failure_finish(NULL); 1946228753Smm return (0); 1947228753Smm } 1948228753Smm } 1949228753Smm fclose(f); 1950228753Smm return (1); 1951228753Smm#else 1952228753Smm int fd; 1953228753Smm assertion_count(file, line); 1954228753Smm fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644); 1955228753Smm if (fd < 0) { 1956228753Smm failure_start(file, line, "Could not create %s", path); 1957228753Smm failure_finish(NULL); 1958228753Smm return (0); 1959228753Smm } 1960306669Smm if (0 != chmod(path, mode)) { 1961306669Smm failure_start(file, line, "Could not chmod %s", path); 1962306669Smm failure_finish(NULL); 1963307214Smm close(fd); 1964306669Smm return (0); 1965306669Smm } 1966228753Smm if (contents != NULL) { 1967238825Smm ssize_t wsize; 1968238825Smm 1969238825Smm if (csize < 0) 1970238825Smm wsize = (ssize_t)strlen(contents); 1971238825Smm else 1972238825Smm wsize = (ssize_t)csize; 1973238825Smm if (wsize != write(fd, contents, wsize)) { 1974228753Smm close(fd); 1975238825Smm failure_start(file, line, 1976238825Smm "Could not write to %s", path); 1977228753Smm failure_finish(NULL); 1978307214Smm close(fd); 1979228753Smm return (0); 1980228753Smm } 1981228753Smm } 1982228753Smm close(fd); 1983306669Smm assertion_file_mode(file, line, path, mode); 1984228753Smm return (1); 1985228753Smm#endif 1986228753Smm} 1987228753Smm 1988228753Smm/* Create a hardlink and report any failures. */ 1989228753Smmint 1990228753Smmassertion_make_hardlink(const char *file, int line, 1991228753Smm const char *newpath, const char *linkto) 1992228753Smm{ 1993228753Smm int succeeded; 1994228753Smm 1995228753Smm assertion_count(file, line); 1996228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1997228753Smm succeeded = my_CreateHardLinkA(newpath, linkto); 1998228753Smm#elif HAVE_LINK 1999228753Smm succeeded = !link(linkto, newpath); 2000228753Smm#else 2001228753Smm succeeded = 0; 2002228753Smm#endif 2003228753Smm if (succeeded) 2004228753Smm return (1); 2005228753Smm failure_start(file, line, "Could not create hardlink"); 2006228753Smm logprintf(" New link: %s\n", newpath); 2007228753Smm logprintf(" Old name: %s\n", linkto); 2008228753Smm failure_finish(NULL); 2009228753Smm return(0); 2010228753Smm} 2011228753Smm 2012348608Smm/* 2013348608Smm * Create a symlink and report any failures. 2014348608Smm * 2015348608Smm * Windows symlinks need to know if the target is a directory. 2016348608Smm */ 2017228753Smmint 2018228753Smmassertion_make_symlink(const char *file, int line, 2019348608Smm const char *newpath, const char *linkto, int targetIsDir) 2020228753Smm{ 2021228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2022228753Smm assertion_count(file, line); 2023228753Smm if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir)) 2024228753Smm return (1); 2025228753Smm#elif HAVE_SYMLINK 2026348608Smm (void)targetIsDir; /* UNUSED */ 2027228753Smm assertion_count(file, line); 2028228753Smm if (0 == symlink(linkto, newpath)) 2029228753Smm return (1); 2030348608Smm#else 2031348608Smm (void)targetIsDir; /* UNUSED */ 2032228753Smm#endif 2033228753Smm failure_start(file, line, "Could not create symlink"); 2034228753Smm logprintf(" New link: %s\n", newpath); 2035228753Smm logprintf(" Old name: %s\n", linkto); 2036228753Smm failure_finish(NULL); 2037228753Smm return(0); 2038228753Smm} 2039228753Smm 2040228753Smm/* Set umask, report failures. */ 2041228753Smmint 2042228753Smmassertion_umask(const char *file, int line, int mask) 2043228753Smm{ 2044228753Smm assertion_count(file, line); 2045228753Smm (void)file; /* UNUSED */ 2046228753Smm (void)line; /* UNUSED */ 2047228753Smm umask(mask); 2048228753Smm return (1); 2049228753Smm} 2050228753Smm 2051231200Smm/* Set times, report failures. */ 2052231200Smmint 2053231200Smmassertion_utimes(const char *file, int line, 2054231200Smm const char *pathname, long at, long at_nsec, long mt, long mt_nsec) 2055231200Smm{ 2056231200Smm int r; 2057231200Smm 2058231200Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2059231200Smm#define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\ 2060231200Smm + (((nsec)/1000)*10)) 2061231200Smm HANDLE h; 2062231200Smm ULARGE_INTEGER wintm; 2063231200Smm FILETIME fatime, fmtime; 2064231200Smm FILETIME *pat, *pmt; 2065231200Smm 2066231200Smm assertion_count(file, line); 2067231200Smm h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE, 2068231200Smm FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 2069231200Smm FILE_FLAG_BACKUP_SEMANTICS, NULL); 2070231200Smm if (h == INVALID_HANDLE_VALUE) { 2071231200Smm failure_start(file, line, "Can't access %s\n", pathname); 2072231200Smm failure_finish(NULL); 2073231200Smm return (0); 2074231200Smm } 2075231200Smm 2076231200Smm if (at > 0 || at_nsec > 0) { 2077231200Smm wintm.QuadPart = WINTIME(at, at_nsec); 2078231200Smm fatime.dwLowDateTime = wintm.LowPart; 2079231200Smm fatime.dwHighDateTime = wintm.HighPart; 2080231200Smm pat = &fatime; 2081231200Smm } else 2082231200Smm pat = NULL; 2083231200Smm if (mt > 0 || mt_nsec > 0) { 2084231200Smm wintm.QuadPart = WINTIME(mt, mt_nsec); 2085231200Smm fmtime.dwLowDateTime = wintm.LowPart; 2086231200Smm fmtime.dwHighDateTime = wintm.HighPart; 2087231200Smm pmt = &fmtime; 2088231200Smm } else 2089231200Smm pmt = NULL; 2090231200Smm if (pat != NULL || pmt != NULL) 2091231200Smm r = SetFileTime(h, NULL, pat, pmt); 2092231200Smm else 2093231200Smm r = 1; 2094231200Smm CloseHandle(h); 2095231200Smm if (r == 0) { 2096231200Smm failure_start(file, line, "Can't SetFileTime %s\n", pathname); 2097231200Smm failure_finish(NULL); 2098231200Smm return (0); 2099231200Smm } 2100231200Smm return (1); 2101231200Smm#else /* defined(_WIN32) && !defined(__CYGWIN__) */ 2102231200Smm struct stat st; 2103231200Smm struct timeval times[2]; 2104231200Smm 2105231200Smm#if !defined(__FreeBSD__) 2106231200Smm mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */ 2107231200Smm#endif 2108231200Smm if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0) 2109231200Smm return (1); 2110231200Smm 2111231200Smm r = lstat(pathname, &st); 2112231200Smm if (r < 0) { 2113231200Smm failure_start(file, line, "Can't stat %s\n", pathname); 2114231200Smm failure_finish(NULL); 2115231200Smm return (0); 2116231200Smm } 2117231200Smm 2118231200Smm if (mt == 0 && mt_nsec == 0) { 2119231200Smm mt = st.st_mtime; 2120231200Smm#if defined(__FreeBSD__) 2121231200Smm mt_nsec = st.st_mtimespec.tv_nsec; 2122231200Smm /* FreeBSD generally only stores to microsecond res, so round. */ 2123231200Smm mt_nsec = (mt_nsec / 1000) * 1000; 2124231200Smm#endif 2125231200Smm } 2126231200Smm if (at == 0 && at_nsec == 0) { 2127231200Smm at = st.st_atime; 2128231200Smm#if defined(__FreeBSD__) 2129231200Smm at_nsec = st.st_atimespec.tv_nsec; 2130231200Smm /* FreeBSD generally only stores to microsecond res, so round. */ 2131231200Smm at_nsec = (at_nsec / 1000) * 1000; 2132231200Smm#endif 2133231200Smm } 2134231200Smm 2135231200Smm times[1].tv_sec = mt; 2136231200Smm times[1].tv_usec = mt_nsec / 1000; 2137231200Smm 2138231200Smm times[0].tv_sec = at; 2139231200Smm times[0].tv_usec = at_nsec / 1000; 2140231200Smm 2141231200Smm#ifdef HAVE_LUTIMES 2142231200Smm r = lutimes(pathname, times); 2143231200Smm#else 2144231200Smm r = utimes(pathname, times); 2145231200Smm#endif 2146231200Smm if (r < 0) { 2147231200Smm failure_start(file, line, "Can't utimes %s\n", pathname); 2148231200Smm failure_finish(NULL); 2149231200Smm return (0); 2150231200Smm } 2151231200Smm return (1); 2152231200Smm#endif /* defined(_WIN32) && !defined(__CYGWIN__) */ 2153231200Smm} 2154231200Smm 2155314571Smm/* Compare file flags */ 2156314571Smmint 2157314571Smmassertion_compare_fflags(const char *file, int line, const char *patha, 2158314571Smm const char *pathb, int nomatch) 2159314571Smm{ 2160314571Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2161314571Smm struct stat sa, sb; 2162314571Smm 2163314571Smm assertion_count(file, line); 2164314571Smm 2165314571Smm if (stat(patha, &sa) < 0) 2166314571Smm return (0); 2167314571Smm if (stat(pathb, &sb) < 0) 2168314571Smm return (0); 2169314571Smm if (!nomatch && sa.st_flags != sb.st_flags) { 2170314571Smm failure_start(file, line, "File flags should be identical: " 2171314571Smm "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb, 2172314571Smm sb.st_flags); 2173314571Smm failure_finish(NULL); 2174314571Smm return (0); 2175314571Smm } 2176314571Smm if (nomatch && sa.st_flags == sb.st_flags) { 2177314571Smm failure_start(file, line, "File flags should be different: " 2178314571Smm "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb, 2179314571Smm sb.st_flags); 2180314571Smm failure_finish(NULL); 2181314571Smm return (0); 2182314571Smm } 2183314571Smm#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \ 2184314571Smm defined(FS_NODUMP_FL)) || \ 2185314571Smm (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \ 2186314571Smm && defined(EXT2_NODUMP_FL)) 2187314571Smm int fd, r, flagsa, flagsb; 2188314571Smm 2189314571Smm assertion_count(file, line); 2190314571Smm fd = open(patha, O_RDONLY | O_NONBLOCK); 2191314571Smm if (fd < 0) { 2192314571Smm failure_start(file, line, "Can't open %s\n", patha); 2193314571Smm failure_finish(NULL); 2194314571Smm return (0); 2195314571Smm } 2196314571Smm r = ioctl(fd, 2197314571Smm#ifdef FS_IOC_GETFLAGS 2198314571Smm FS_IOC_GETFLAGS, 2199314571Smm#else 2200314571Smm EXT2_IOC_GETFLAGS, 2201314571Smm#endif 2202314571Smm &flagsa); 2203314571Smm close(fd); 2204314571Smm if (r < 0) { 2205314571Smm failure_start(file, line, "Can't get flags %s\n", patha); 2206314571Smm failure_finish(NULL); 2207314571Smm return (0); 2208314571Smm } 2209314571Smm fd = open(pathb, O_RDONLY | O_NONBLOCK); 2210314571Smm if (fd < 0) { 2211314571Smm failure_start(file, line, "Can't open %s\n", pathb); 2212314571Smm failure_finish(NULL); 2213314571Smm return (0); 2214314571Smm } 2215314571Smm r = ioctl(fd, 2216314571Smm#ifdef FS_IOC_GETFLAGS 2217314571Smm FS_IOC_GETFLAGS, 2218314571Smm#else 2219314571Smm EXT2_IOC_GETFLAGS, 2220314571Smm#endif 2221314571Smm &flagsb); 2222314571Smm close(fd); 2223314571Smm if (r < 0) { 2224314571Smm failure_start(file, line, "Can't get flags %s\n", pathb); 2225314571Smm failure_finish(NULL); 2226314571Smm return (0); 2227314571Smm } 2228314571Smm if (!nomatch && flagsa != flagsb) { 2229314571Smm failure_start(file, line, "File flags should be identical: " 2230314571Smm "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb); 2231314571Smm failure_finish(NULL); 2232314571Smm return (0); 2233314571Smm } 2234314571Smm if (nomatch && flagsa == flagsb) { 2235314571Smm failure_start(file, line, "File flags should be different: " 2236314571Smm "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb); 2237314571Smm failure_finish(NULL); 2238314571Smm return (0); 2239314571Smm } 2240314571Smm#else 2241314571Smm (void)patha; /* UNUSED */ 2242314571Smm (void)pathb; /* UNUSED */ 2243314571Smm (void)nomatch; /* UNUSED */ 2244314571Smm assertion_count(file, line); 2245314571Smm#endif 2246314571Smm return (1); 2247314571Smm} 2248314571Smm 2249238825Smm/* Set nodump, report failures. */ 2250238825Smmint 2251314571Smmassertion_set_nodump(const char *file, int line, const char *pathname) 2252238825Smm{ 2253238825Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2254238825Smm int r; 2255238825Smm 2256238825Smm assertion_count(file, line); 2257238825Smm r = chflags(pathname, UF_NODUMP); 2258238825Smm if (r < 0) { 2259238825Smm failure_start(file, line, "Can't set nodump %s\n", pathname); 2260238825Smm failure_finish(NULL); 2261238825Smm return (0); 2262238825Smm } 2263314565Smm#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \ 2264314565Smm defined(FS_NODUMP_FL)) || \ 2265314565Smm (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \ 2266314565Smm && defined(EXT2_NODUMP_FL)) 2267238825Smm int fd, r, flags; 2268238825Smm 2269238825Smm assertion_count(file, line); 2270238825Smm fd = open(pathname, O_RDONLY | O_NONBLOCK); 2271238825Smm if (fd < 0) { 2272238825Smm failure_start(file, line, "Can't open %s\n", pathname); 2273238825Smm failure_finish(NULL); 2274238825Smm return (0); 2275238825Smm } 2276314565Smm r = ioctl(fd, 2277314565Smm#ifdef FS_IOC_GETFLAGS 2278314565Smm FS_IOC_GETFLAGS, 2279314565Smm#else 2280314565Smm EXT2_IOC_GETFLAGS, 2281314565Smm#endif 2282314565Smm &flags); 2283238825Smm if (r < 0) { 2284238825Smm failure_start(file, line, "Can't get flags %s\n", pathname); 2285238825Smm failure_finish(NULL); 2286238825Smm return (0); 2287238825Smm } 2288314565Smm#ifdef FS_NODUMP_FL 2289314565Smm flags |= FS_NODUMP_FL; 2290314565Smm#else 2291238825Smm flags |= EXT2_NODUMP_FL; 2292314565Smm#endif 2293314565Smm 2294314565Smm r = ioctl(fd, 2295314565Smm#ifdef FS_IOC_SETFLAGS 2296314565Smm FS_IOC_SETFLAGS, 2297314565Smm#else 2298314565Smm EXT2_IOC_SETFLAGS, 2299314565Smm#endif 2300314565Smm &flags); 2301238825Smm if (r < 0) { 2302238825Smm failure_start(file, line, "Can't set nodump %s\n", pathname); 2303238825Smm failure_finish(NULL); 2304238825Smm return (0); 2305238825Smm } 2306238825Smm close(fd); 2307238825Smm#else 2308238825Smm (void)pathname; /* UNUSED */ 2309238825Smm assertion_count(file, line); 2310238825Smm#endif 2311238825Smm return (1); 2312238825Smm} 2313238825Smm 2314314565Smm#ifdef PROGRAM 2315314565Smmstatic void assert_version_id(char **qq, size_t *ss) 2316314565Smm{ 2317314565Smm char *q = *qq; 2318314565Smm size_t s = *ss; 2319314565Smm 2320314565Smm /* Version number is a series of digits and periods. */ 2321314565Smm while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) { 2322314565Smm ++q; 2323314565Smm --s; 2324314565Smm } 2325314565Smm 2326314565Smm if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') { 2327314565Smm q += 3; 2328314565Smm s -= 3; 2329314565Smm } 2330314565Smm 2331314565Smm /* Skip a single trailing a,b,c, or d. */ 2332314565Smm if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd') 2333314565Smm ++q; 2334314565Smm 2335314565Smm /* Version number terminated by space. */ 2336314565Smm failure("No space after version: ``%s''", q); 2337314565Smm assert(s > 1); 2338314565Smm failure("No space after version: ``%s''", q); 2339314565Smm assert(*q == ' '); 2340314565Smm 2341314565Smm ++q; --s; 2342314565Smm 2343314565Smm *qq = q; 2344314565Smm *ss = s; 2345314565Smm} 2346314565Smm 2347314565Smm 2348228753Smm/* 2349314565Smm * Check program version 2350314565Smm */ 2351314565Smmvoid assertVersion(const char *prog, const char *base) 2352314565Smm{ 2353314565Smm int r; 2354314565Smm char *p, *q; 2355314565Smm size_t s; 2356337352Smm size_t prog_len = strlen(base); 2357314565Smm 2358314565Smm r = systemf("%s --version >version.stdout 2>version.stderr", prog); 2359314565Smm if (r != 0) 2360314565Smm r = systemf("%s -W version >version.stdout 2>version.stderr", 2361314565Smm prog); 2362314565Smm 2363314565Smm failure("Unable to run either %s --version or %s -W version", 2364314565Smm prog, prog); 2365314565Smm if (!assert(r == 0)) 2366314565Smm return; 2367314565Smm 2368314565Smm /* --version should generate nothing to stdout. */ 2369314565Smm assertEmptyFile("version.stderr"); 2370314565Smm 2371314565Smm /* Verify format of version message. */ 2372314565Smm q = p = slurpfile(&s, "version.stdout"); 2373314565Smm 2374314565Smm /* Version message should start with name of program, then space. */ 2375314565Smm assert(s > prog_len + 1); 2376314565Smm 2377314565Smm failure("Version must start with '%s': ``%s''", base, p); 2378314565Smm if (!assertEqualMem(q, base, prog_len)) { 2379314565Smm free(p); 2380314565Smm return; 2381314565Smm } 2382314565Smm 2383314565Smm q += prog_len; s -= prog_len; 2384314565Smm 2385314565Smm assert(*q == ' '); 2386314565Smm q++; s--; 2387314565Smm 2388314565Smm assert_version_id(&q, &s); 2389314565Smm 2390314565Smm /* Separator. */ 2391314565Smm failure("No `-' between program name and versions: ``%s''", p); 2392314565Smm assertEqualMem(q, "- ", 2); 2393314565Smm q += 2; s -= 2; 2394314565Smm 2395314565Smm failure("Not long enough for libarchive version: ``%s''", p); 2396314565Smm assert(s > 11); 2397314565Smm 2398314565Smm failure("Libarchive version must start with `libarchive': ``%s''", p); 2399314565Smm assertEqualMem(q, "libarchive ", 11); 2400314565Smm 2401314565Smm q += 11; s -= 11; 2402314565Smm 2403314565Smm assert_version_id(&q, &s); 2404314565Smm 2405314565Smm /* Skip arbitrary third-party version numbers. */ 2406314565Smm while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' || 2407339006Smm isalnum((unsigned char)*q))) { 2408314565Smm ++q; 2409314565Smm --s; 2410314565Smm } 2411314565Smm 2412314565Smm /* All terminated by end-of-line. */ 2413314565Smm assert(s >= 1); 2414314565Smm 2415314565Smm /* Skip an optional CR character (e.g., Windows) */ 2416314565Smm failure("Version output must end with \\n or \\r\\n"); 2417314565Smm 2418314565Smm if (*q == '\r') { ++q; --s; } 2419314565Smm assertEqualMem(q, "\n", 1); 2420314565Smm 2421314565Smm free(p); 2422314565Smm} 2423314565Smm#endif /* PROGRAM */ 2424314565Smm 2425314565Smm/* 2426228753Smm * 2427228753Smm * UTILITIES for use by tests. 2428228753Smm * 2429228753Smm */ 2430228753Smm 2431228753Smm/* 2432228753Smm * Check whether platform supports symlinks. This is intended 2433228753Smm * for tests to use in deciding whether to bother testing symlink 2434228753Smm * support; if the platform doesn't support symlinks, there's no point 2435228753Smm * in checking whether the program being tested can create them. 2436228753Smm * 2437228753Smm * Note that the first time this test is called, we actually go out to 2438228753Smm * disk to create and verify a symlink. This is necessary because 2439228753Smm * symlink support is actually a property of a particular filesystem 2440228753Smm * and can thus vary between directories on a single system. After 2441228753Smm * the first call, this returns the cached result from memory, so it's 2442228753Smm * safe to call it as often as you wish. 2443228753Smm */ 2444228753Smmint 2445228753SmmcanSymlink(void) 2446228753Smm{ 2447228753Smm /* Remember the test result */ 2448228753Smm static int value = 0, tested = 0; 2449228753Smm if (tested) 2450228753Smm return (value); 2451228753Smm 2452228753Smm ++tested; 2453238825Smm assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a"); 2454228753Smm /* Note: Cygwin has its own symlink() emulation that does not 2455228753Smm * use the Win32 CreateSymbolicLink() function. */ 2456228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2457228753Smm value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0) 2458348608Smm && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0", 2459348608Smm 0); 2460228753Smm#elif HAVE_SYMLINK 2461228753Smm value = (0 == symlink("canSymlink.0", "canSymlink.1")) 2462348608Smm && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0", 2463348608Smm 0); 2464228753Smm#endif 2465228753Smm return (value); 2466228753Smm} 2467228753Smm 2468228753Smm/* Platform-dependent options for hiding the output of a subcommand. */ 2469228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2470228753Smmstatic const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */ 2471228753Smm#else 2472228753Smmstatic const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */ 2473228753Smm#endif 2474248590Smm/* 2475248590Smm * Can this platform run the bzip2 program? 2476248590Smm */ 2477228753Smmint 2478248590SmmcanBzip2(void) 2479248590Smm{ 2480248590Smm static int tested = 0, value = 0; 2481248590Smm if (!tested) { 2482248590Smm tested = 1; 2483358090Smm if (systemf("bzip2 --help %s", redirectArgs) == 0) 2484248590Smm value = 1; 2485248590Smm } 2486248590Smm return (value); 2487248590Smm} 2488248590Smm 2489248590Smm/* 2490248590Smm * Can this platform run the grzip program? 2491248590Smm */ 2492248590Smmint 2493248590SmmcanGrzip(void) 2494248590Smm{ 2495248590Smm static int tested = 0, value = 0; 2496248590Smm if (!tested) { 2497248590Smm tested = 1; 2498248590Smm if (systemf("grzip -V %s", redirectArgs) == 0) 2499248590Smm value = 1; 2500248590Smm } 2501248590Smm return (value); 2502248590Smm} 2503248590Smm 2504248590Smm/* 2505248590Smm * Can this platform run the gzip program? 2506248590Smm */ 2507248590Smmint 2508228753SmmcanGzip(void) 2509228753Smm{ 2510228753Smm static int tested = 0, value = 0; 2511228753Smm if (!tested) { 2512228753Smm tested = 1; 2513358090Smm if (systemf("gzip --help %s", redirectArgs) == 0) 2514228753Smm value = 1; 2515228753Smm } 2516228753Smm return (value); 2517228753Smm} 2518228753Smm 2519228753Smm/* 2520248590Smm * Can this platform run the lrzip program? 2521228753Smm */ 2522228753Smmint 2523248590SmmcanRunCommand(const char *cmd) 2524228753Smm{ 2525248590Smm static int tested = 0, value = 0; 2526248590Smm if (!tested) { 2527248590Smm tested = 1; 2528248590Smm if (systemf("%s %s", cmd, redirectArgs) == 0) 2529248590Smm value = 1; 2530248590Smm } 2531248590Smm return (value); 2532248590Smm} 2533248590Smm 2534248590Smmint 2535248590SmmcanLrzip(void) 2536248590Smm{ 2537228753Smm static int tested = 0, value = 0; 2538228753Smm if (!tested) { 2539228753Smm tested = 1; 2540248590Smm if (systemf("lrzip -V %s", redirectArgs) == 0) 2541228753Smm value = 1; 2542228753Smm } 2543228753Smm return (value); 2544228753Smm} 2545228753Smm 2546228753Smm/* 2547299425Smm * Can this platform run the lz4 program? 2548299425Smm */ 2549299425Smmint 2550299425SmmcanLz4(void) 2551299425Smm{ 2552299425Smm static int tested = 0, value = 0; 2553299425Smm if (!tested) { 2554299425Smm tested = 1; 2555358090Smm if (systemf("lz4 --help %s", redirectArgs) == 0) 2556299425Smm value = 1; 2557299425Smm } 2558299425Smm return (value); 2559299425Smm} 2560299425Smm 2561299425Smm/* 2562324418Smm * Can this platform run the zstd program? 2563324418Smm */ 2564324418Smmint 2565324418SmmcanZstd(void) 2566324418Smm{ 2567324418Smm static int tested = 0, value = 0; 2568324418Smm if (!tested) { 2569324418Smm tested = 1; 2570358090Smm if (systemf("zstd --help %s", redirectArgs) == 0) 2571324418Smm value = 1; 2572324418Smm } 2573324418Smm return (value); 2574324418Smm} 2575324418Smm 2576324418Smm/* 2577248590Smm * Can this platform run the lzip program? 2578248590Smm */ 2579248590Smmint 2580248590SmmcanLzip(void) 2581248590Smm{ 2582248590Smm static int tested = 0, value = 0; 2583248590Smm if (!tested) { 2584248590Smm tested = 1; 2585358090Smm if (systemf("lzip --help %s", redirectArgs) == 0) 2586248590Smm value = 1; 2587248590Smm } 2588248590Smm return (value); 2589248590Smm} 2590248590Smm 2591248590Smm/* 2592248590Smm * Can this platform run the lzma program? 2593248590Smm */ 2594248590Smmint 2595248590SmmcanLzma(void) 2596248590Smm{ 2597248590Smm static int tested = 0, value = 0; 2598248590Smm if (!tested) { 2599248590Smm tested = 1; 2600358090Smm if (systemf("lzma %s", redirectArgs) == 0) 2601248590Smm value = 1; 2602248590Smm } 2603248590Smm return (value); 2604248590Smm} 2605248590Smm 2606248590Smm/* 2607248590Smm * Can this platform run the lzop program? 2608248590Smm */ 2609248590Smmint 2610248590SmmcanLzop(void) 2611248590Smm{ 2612248590Smm static int tested = 0, value = 0; 2613248590Smm if (!tested) { 2614248590Smm tested = 1; 2615358090Smm if (systemf("lzop --help %s", redirectArgs) == 0) 2616248590Smm value = 1; 2617248590Smm } 2618248590Smm return (value); 2619248590Smm} 2620248590Smm 2621248590Smm/* 2622248590Smm * Can this platform run the xz program? 2623248590Smm */ 2624248590Smmint 2625248590SmmcanXz(void) 2626248590Smm{ 2627248590Smm static int tested = 0, value = 0; 2628248590Smm if (!tested) { 2629248590Smm tested = 1; 2630358090Smm if (systemf("xz --help %s", redirectArgs) == 0) 2631248590Smm value = 1; 2632248590Smm } 2633248590Smm return (value); 2634248590Smm} 2635248590Smm 2636248590Smm/* 2637238825Smm * Can this filesystem handle nodump flags. 2638238825Smm */ 2639238825Smmint 2640238825SmmcanNodump(void) 2641238825Smm{ 2642314571Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2643238825Smm const char *path = "cannodumptest"; 2644238825Smm struct stat sb; 2645238825Smm 2646238825Smm assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2647238825Smm if (chflags(path, UF_NODUMP) < 0) 2648238825Smm return (0); 2649238825Smm if (stat(path, &sb) < 0) 2650238825Smm return (0); 2651238825Smm if (sb.st_flags & UF_NODUMP) 2652238825Smm return (1); 2653314565Smm#elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \ 2654314565Smm && defined(FS_NODUMP_FL)) || \ 2655314565Smm (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \ 2656314565Smm && defined(EXT2_NODUMP_FL)) 2657238825Smm const char *path = "cannodumptest"; 2658238825Smm int fd, r, flags; 2659238825Smm 2660238825Smm assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2661238825Smm fd = open(path, O_RDONLY | O_NONBLOCK); 2662238825Smm if (fd < 0) 2663238825Smm return (0); 2664314565Smm r = ioctl(fd, 2665314565Smm#ifdef FS_IOC_GETFLAGS 2666314565Smm FS_IOC_GETFLAGS, 2667314565Smm#else 2668314565Smm EXT2_IOC_GETFLAGS, 2669314565Smm#endif 2670314565Smm &flags); 2671238825Smm if (r < 0) 2672238825Smm return (0); 2673314565Smm#ifdef FS_NODUMP_FL 2674314565Smm flags |= FS_NODUMP_FL; 2675314565Smm#else 2676238825Smm flags |= EXT2_NODUMP_FL; 2677314565Smm#endif 2678314565Smm r = ioctl(fd, 2679314565Smm#ifdef FS_IOC_SETFLAGS 2680314565Smm FS_IOC_SETFLAGS, 2681314565Smm#else 2682314565Smm EXT2_IOC_SETFLAGS, 2683314565Smm#endif 2684314565Smm &flags); 2685238825Smm if (r < 0) 2686238825Smm return (0); 2687238825Smm close(fd); 2688238825Smm fd = open(path, O_RDONLY | O_NONBLOCK); 2689238825Smm if (fd < 0) 2690238825Smm return (0); 2691314565Smm r = ioctl(fd, 2692314565Smm#ifdef FS_IOC_GETFLAGS 2693314565Smm FS_IOC_GETFLAGS, 2694314565Smm#else 2695314565Smm EXT2_IOC_GETFLAGS, 2696314565Smm#endif 2697314565Smm &flags); 2698238825Smm if (r < 0) 2699238825Smm return (0); 2700238825Smm close(fd); 2701314565Smm#ifdef FS_NODUMP_FL 2702314565Smm if (flags & FS_NODUMP_FL) 2703314565Smm#else 2704238825Smm if (flags & EXT2_NODUMP_FL) 2705314565Smm#endif 2706238825Smm return (1); 2707314571Smm#endif 2708238825Smm return (0); 2709238825Smm} 2710238825Smm 2711353377Smm/* Get extended attribute value from a path */ 2712353377Smmvoid * 2713316338SmmgetXattr(const char *path, const char *name, size_t *sizep) 2714316338Smm{ 2715316338Smm void *value = NULL; 2716316338Smm#if ARCHIVE_XATTR_SUPPORT 2717316338Smm ssize_t size; 2718316338Smm#if ARCHIVE_XATTR_LINUX 2719316338Smm size = lgetxattr(path, name, NULL, 0); 2720316338Smm#elif ARCHIVE_XATTR_DARWIN 2721316338Smm size = getxattr(path, name, NULL, 0, 0, XATTR_NOFOLLOW); 2722316338Smm#elif ARCHIVE_XATTR_AIX 2723316338Smm size = lgetea(path, name, NULL, 0); 2724316338Smm#elif ARCHIVE_XATTR_FREEBSD 2725316338Smm size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5, 2726316338Smm NULL, 0); 2727316338Smm#endif 2728316338Smm 2729316338Smm if (size >= 0) { 2730316338Smm value = malloc(size); 2731316338Smm#if ARCHIVE_XATTR_LINUX 2732316338Smm size = lgetxattr(path, name, value, size); 2733316338Smm#elif ARCHIVE_XATTR_DARWIN 2734316338Smm size = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW); 2735316338Smm#elif ARCHIVE_XATTR_AIX 2736316338Smm size = lgetea(path, name, value, size); 2737316338Smm#elif ARCHIVE_XATTR_FREEBSD 2738316338Smm size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5, 2739316338Smm value, size); 2740316338Smm#endif 2741316338Smm if (size < 0) { 2742316338Smm free(value); 2743316338Smm value = NULL; 2744316338Smm } 2745316338Smm } 2746316338Smm if (size < 0) 2747316338Smm *sizep = 0; 2748316338Smm else 2749316338Smm *sizep = (size_t)size; 2750316338Smm#else /* !ARCHIVE_XATTR_SUPPORT */ 2751316338Smm (void)path; /* UNUSED */ 2752316338Smm (void)name; /* UNUSED */ 2753316338Smm *sizep = 0; 2754316338Smm#endif /* !ARCHIVE_XATTR_SUPPORT */ 2755316338Smm return (value); 2756316338Smm} 2757316338Smm 2758316338Smm/* 2759316338Smm * Set extended attribute on a path 2760316338Smm * Returns 0 on error, 1 on success 2761316338Smm */ 2762316338Smmint 2763316338SmmsetXattr(const char *path, const char *name, const void *value, size_t size) 2764316338Smm{ 2765316338Smm#if ARCHIVE_XATTR_SUPPORT 2766316338Smm#if ARCHIVE_XATTR_LINUX 2767316338Smm if (lsetxattr(path, name, value, size, 0) == 0) 2768316338Smm#elif ARCHIVE_XATTR_DARWIN 2769316338Smm if (setxattr(path, name, value, size, 0, XATTR_NOFOLLOW) == 0) 2770316338Smm#elif ARCHIVE_XATTR_AIX 2771316338Smm if (lsetea(path, name, value, size, 0) == 0) 2772316338Smm#elif ARCHIVE_XATTR_FREEBSD 2773316338Smm if (extattr_set_link(path, EXTATTR_NAMESPACE_USER, name + 5, value, 2774316338Smm size) > -1) 2775316338Smm#else 2776316338Smm if (0) 2777316338Smm#endif 2778316338Smm return (1); 2779316338Smm#else /* !ARCHIVE_XATTR_SUPPORT */ 2780316338Smm (void)path; /* UNUSED */ 2781316338Smm (void)name; /* UNUSED */ 2782316338Smm (void)value; /* UNUSED */ 2783316338Smm (void)size; /* UNUSED */ 2784316338Smm#endif /* !ARCHIVE_XATTR_SUPPORT */ 2785316338Smm return (0); 2786316338Smm} 2787316338Smm 2788316338Smm#if ARCHIVE_ACL_SUNOS 2789314571Smm/* Fetch ACLs on Solaris using acl() or facl() */ 2790314571Smmvoid * 2791314571Smmsunacl_get(int cmd, int *aclcnt, int fd, const char *path) 2792314571Smm{ 2793314571Smm int cnt, cntcmd; 2794314571Smm size_t size; 2795314571Smm void *aclp; 2796238825Smm 2797314571Smm if (cmd == GETACL) { 2798314571Smm cntcmd = GETACLCNT; 2799314571Smm size = sizeof(aclent_t); 2800314571Smm } 2801316338Smm#if ARCHIVE_ACL_SUNOS_NFS4 2802314571Smm else if (cmd == ACE_GETACL) { 2803314571Smm cntcmd = ACE_GETACLCNT; 2804314571Smm size = sizeof(ace_t); 2805314571Smm } 2806314571Smm#endif 2807314571Smm else { 2808314571Smm errno = EINVAL; 2809314571Smm *aclcnt = -1; 2810314571Smm return (NULL); 2811314571Smm } 2812314571Smm 2813314571Smm aclp = NULL; 2814314571Smm cnt = -2; 2815314571Smm while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) { 2816314571Smm if (path != NULL) 2817314571Smm cnt = acl(path, cntcmd, 0, NULL); 2818314571Smm else 2819314571Smm cnt = facl(fd, cntcmd, 0, NULL); 2820314571Smm 2821314571Smm if (cnt > 0) { 2822314571Smm if (aclp == NULL) 2823314571Smm aclp = malloc(cnt * size); 2824314571Smm else 2825314571Smm aclp = realloc(NULL, cnt * size); 2826314571Smm if (aclp != NULL) { 2827314571Smm if (path != NULL) 2828314571Smm cnt = acl(path, cmd, cnt, aclp); 2829314571Smm else 2830314571Smm cnt = facl(fd, cmd, cnt, aclp); 2831314571Smm } 2832314571Smm } else { 2833344674Smm free(aclp); 2834344674Smm aclp = NULL; 2835314571Smm break; 2836314571Smm } 2837314571Smm } 2838314571Smm 2839314571Smm *aclcnt = cnt; 2840314571Smm return (aclp); 2841314571Smm} 2842316338Smm#endif /* ARCHIVE_ACL_SUNOS */ 2843314571Smm 2844314571Smm/* 2845314571Smm * Set test ACLs on a path 2846314571Smm * Return values: 2847314571Smm * 0: error setting ACLs 2848314571Smm * ARCHIVE_TEST_ACL_TYPE_POSIX1E: POSIX.1E ACLs have been set 2849314571Smm * ARCHIVE_TEST_ACL_TYPE_NFS4: NFSv4 or extended ACLs have been set 2850314571Smm */ 2851238825Smmint 2852314571SmmsetTestAcl(const char *path) 2853238825Smm{ 2854316338Smm#if ARCHIVE_ACL_SUPPORT 2855314571Smm int r = 1; 2856316338Smm#if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_DARWIN 2857314571Smm acl_t acl; 2858314571Smm#endif 2859316338Smm#if ARCHIVE_ACL_LIBRICHACL 2860316338Smm struct richacl *richacl; 2861316338Smm#endif 2862316338Smm#if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD 2863314571Smm const char *acltext_posix1e = "user:1:rw-," 2864314571Smm "group:15:r-x," 2865314571Smm "user::rwx," 2866314571Smm "group::rwx," 2867314571Smm "other::r-x," 2868314571Smm "mask::rwx"; 2869316338Smm#elif ARCHIVE_ACL_SUNOS /* Solaris POSIX.1e */ 2870314571Smm aclent_t aclp_posix1e[] = { 2871314571Smm { USER_OBJ, -1, 4 | 2 | 1 }, 2872314571Smm { USER, 1, 4 | 2 }, 2873314571Smm { GROUP_OBJ, -1, 4 | 2 | 1 }, 2874314571Smm { GROUP, 15, 4 | 1 }, 2875314571Smm { CLASS_OBJ, -1, 4 | 2 | 1 }, 2876314571Smm { OTHER_OBJ, -1, 4 | 2 | 1 } 2877314571Smm }; 2878314571Smm#endif 2879316338Smm#if ARCHIVE_ACL_FREEBSD /* FreeBSD NFS4 */ 2880314571Smm const char *acltext_nfs4 = "user:1:rwpaRcs::allow:1," 2881314571Smm "group:15:rxaRcs::allow:15," 2882314571Smm "owner@:rwpxaARWcCos::allow," 2883314571Smm "group@:rwpxaRcs::allow," 2884314571Smm "everyone@:rxaRcs::allow"; 2885316338Smm#elif ARCHIVE_ACL_LIBRICHACL 2886316338Smm const char *acltext_nfs4 = "owner:rwpxaARWcCoS::mask," 2887316338Smm "group:rwpxaRcS::mask," 2888316338Smm "other:rxaRcS::mask," 2889316338Smm "user:1:rwpaRcS::allow," 2890316338Smm "group:15:rxaRcS::allow," 2891316338Smm "owner@:rwpxaARWcCoS::allow," 2892316338Smm "group@:rwpxaRcS::allow," 2893316338Smm "everyone@:rxaRcS::allow"; 2894316338Smm#elif ARCHIVE_ACL_SUNOS_NFS4 /* Solaris NFS4 */ 2895314571Smm ace_t aclp_nfs4[] = { 2896314571Smm { 1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA | 2897314571Smm ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | ACE_READ_ACL | 2898314571Smm ACE_SYNCHRONIZE, 0, ACE_ACCESS_ALLOWED_ACE_TYPE }, 2899314571Smm { 15, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES | 2900314571Smm ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE, 2901314571Smm ACE_IDENTIFIER_GROUP, ACE_ACCESS_ALLOWED_ACE_TYPE }, 2902314571Smm { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA | 2903314571Smm ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_WRITE_ATTRIBUTES | 2904314571Smm ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS | 2905314571Smm ACE_READ_ACL | ACE_WRITE_ACL | ACE_WRITE_OWNER | ACE_SYNCHRONIZE, 2906314571Smm ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE }, 2907314571Smm { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA | 2908314571Smm ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | 2909314571Smm ACE_READ_ACL | ACE_SYNCHRONIZE, ACE_GROUP | ACE_IDENTIFIER_GROUP, 2910314571Smm ACE_ACCESS_ALLOWED_ACE_TYPE }, 2911314571Smm { -1, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES | 2912314571Smm ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE, 2913314571Smm ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE } 2914314571Smm }; 2915316338Smm#elif ARCHIVE_ACL_DARWIN /* Mac OS X */ 2916314571Smm acl_entry_t aclent; 2917314571Smm acl_permset_t permset; 2918314571Smm const uid_t uid = 1; 2919314571Smm uuid_t uuid; 2920314571Smm int i; 2921314571Smm const acl_perm_t acl_perms[] = { 2922314571Smm ACL_READ_DATA, 2923314571Smm ACL_WRITE_DATA, 2924314571Smm ACL_APPEND_DATA, 2925314571Smm ACL_EXECUTE, 2926314571Smm ACL_READ_ATTRIBUTES, 2927314571Smm ACL_READ_EXTATTRIBUTES, 2928314571Smm ACL_READ_SECURITY, 2929314571Smm#if HAVE_DECL_ACL_SYNCHRONIZE 2930314571Smm ACL_SYNCHRONIZE 2931314571Smm#endif 2932314571Smm }; 2933316338Smm#endif /* ARCHIVE_ACL_DARWIN */ 2934314571Smm 2935316338Smm#if ARCHIVE_ACL_FREEBSD 2936314571Smm acl = acl_from_text(acltext_nfs4); 2937314571Smm failure("acl_from_text() error: %s", strerror(errno)); 2938314571Smm if (assert(acl != NULL) == 0) 2939314571Smm return (0); 2940316338Smm#elif ARCHIVE_ACL_LIBRICHACL 2941316338Smm richacl = richacl_from_text(acltext_nfs4, NULL, NULL); 2942316338Smm failure("richacl_from_text() error: %s", strerror(errno)); 2943316338Smm if (assert(richacl != NULL) == 0) 2944316338Smm return (0); 2945316338Smm#elif ARCHIVE_ACL_DARWIN 2946314571Smm acl = acl_init(1); 2947314571Smm failure("acl_init() error: %s", strerror(errno)); 2948314571Smm if (assert(acl != NULL) == 0) 2949314571Smm return (0); 2950314571Smm r = acl_create_entry(&acl, &aclent); 2951314571Smm failure("acl_create_entry() error: %s", strerror(errno)); 2952314571Smm if (assertEqualInt(r, 0) == 0) 2953314571Smm goto testacl_free; 2954314571Smm r = acl_set_tag_type(aclent, ACL_EXTENDED_ALLOW); 2955314571Smm failure("acl_set_tag_type() error: %s", strerror(errno)); 2956314571Smm if (assertEqualInt(r, 0) == 0) 2957314571Smm goto testacl_free; 2958314571Smm r = acl_get_permset(aclent, &permset); 2959314571Smm failure("acl_get_permset() error: %s", strerror(errno)); 2960314571Smm if (assertEqualInt(r, 0) == 0) 2961314571Smm goto testacl_free; 2962314571Smm for (i = 0; i < (int)(sizeof(acl_perms) / sizeof(acl_perms[0])); i++) { 2963314571Smm r = acl_add_perm(permset, acl_perms[i]); 2964314571Smm failure("acl_add_perm() error: %s", strerror(errno)); 2965314571Smm if (assertEqualInt(r, 0) == 0) 2966314571Smm goto testacl_free; 2967314571Smm } 2968314571Smm r = acl_set_permset(aclent, permset); 2969314571Smm failure("acl_set_permset() error: %s", strerror(errno)); 2970314571Smm if (assertEqualInt(r, 0) == 0) 2971314571Smm goto testacl_free; 2972316338Smm r = mbr_uid_to_uuid(uid, uuid); 2973316338Smm failure("mbr_uid_to_uuid() error: %s", strerror(errno)); 2974314571Smm if (assertEqualInt(r, 0) == 0) 2975314571Smm goto testacl_free; 2976314571Smm r = acl_set_qualifier(aclent, uuid); 2977314571Smm failure("acl_set_qualifier() error: %s", strerror(errno)); 2978314571Smm if (assertEqualInt(r, 0) == 0) 2979314571Smm goto testacl_free; 2980316338Smm#endif /* ARCHIVE_ACL_DARWIN */ 2981314571Smm 2982316338Smm#if ARCHIVE_ACL_NFS4 2983316338Smm#if ARCHIVE_ACL_FREEBSD 2984314571Smm r = acl_set_file(path, ACL_TYPE_NFS4, acl); 2985314571Smm acl_free(acl); 2986316338Smm#elif ARCHIVE_ACL_LIBRICHACL 2987316338Smm r = richacl_set_file(path, richacl); 2988316338Smm richacl_free(richacl); 2989316338Smm#elif ARCHIVE_ACL_SUNOS_NFS4 2990314571Smm r = acl(path, ACE_SETACL, 2991314571Smm (int)(sizeof(aclp_nfs4)/sizeof(aclp_nfs4[0])), aclp_nfs4); 2992316338Smm#elif ARCHIVE_ACL_DARWIN 2993314571Smm r = acl_set_file(path, ACL_TYPE_EXTENDED, acl); 2994314571Smm acl_free(acl); 2995314571Smm#endif 2996314571Smm if (r == 0) 2997314571Smm return (ARCHIVE_TEST_ACL_TYPE_NFS4); 2998316338Smm#endif /* ARCHIVE_ACL_NFS4 */ 2999314571Smm 3000316338Smm#if ARCHIVE_ACL_POSIX1E 3001316338Smm#if ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_LIBACL 3002314571Smm acl = acl_from_text(acltext_posix1e); 3003314571Smm failure("acl_from_text() error: %s", strerror(errno)); 3004314571Smm if (assert(acl != NULL) == 0) 3005314571Smm return (0); 3006314571Smm 3007314571Smm r = acl_set_file(path, ACL_TYPE_ACCESS, acl); 3008314571Smm acl_free(acl); 3009316338Smm#elif ARCHIVE_ACL_SUNOS 3010314571Smm r = acl(path, SETACL, 3011314571Smm (int)(sizeof(aclp_posix1e)/sizeof(aclp_posix1e[0])), aclp_posix1e); 3012314571Smm#endif 3013314571Smm if (r == 0) 3014314571Smm return (ARCHIVE_TEST_ACL_TYPE_POSIX1E); 3015314571Smm else 3016314571Smm return (0); 3017316338Smm#endif /* ARCHIVE_ACL_POSIX1E */ 3018316338Smm#if ARCHIVE_ACL_DARWIN 3019314571Smmtestacl_free: 3020314571Smm acl_free(acl); 3021314571Smm#endif 3022316338Smm#endif /* ARCHIVE_ACL_SUPPORT */ 3023314571Smm (void)path; /* UNUSED */ 3024238825Smm return (0); 3025238825Smm} 3026238825Smm 3027238825Smm/* 3028228753Smm * Sleep as needed; useful for verifying disk timestamp changes by 3029228753Smm * ensuring that the wall-clock time has actually changed before we 3030228753Smm * go back to re-read something from disk. 3031228753Smm */ 3032228753Smmvoid 3033228753SmmsleepUntilAfter(time_t t) 3034228753Smm{ 3035228753Smm while (t >= time(NULL)) 3036228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 3037228753Smm Sleep(500); 3038228753Smm#else 3039228753Smm sleep(1); 3040228753Smm#endif 3041228753Smm} 3042228753Smm 3043228753Smm/* 3044228753Smm * Call standard system() call, but build up the command line using 3045228753Smm * sprintf() conventions. 3046228753Smm */ 3047228753Smmint 3048228753Smmsystemf(const char *fmt, ...) 3049228753Smm{ 3050228753Smm char buff[8192]; 3051228753Smm va_list ap; 3052228753Smm int r; 3053228753Smm 3054228753Smm va_start(ap, fmt); 3055228753Smm vsprintf(buff, fmt, ap); 3056228753Smm if (verbosity > VERBOSITY_FULL) 3057228753Smm logprintf("Cmd: %s\n", buff); 3058228753Smm r = system(buff); 3059228753Smm va_end(ap); 3060228753Smm return (r); 3061228753Smm} 3062228753Smm 3063228753Smm/* 3064228753Smm * Slurp a file into memory for ease of comparison and testing. 3065228753Smm * Returns size of file in 'sizep' if non-NULL, null-terminates 3066228753Smm * data in memory for ease of use. 3067228753Smm */ 3068228753Smmchar * 3069228753Smmslurpfile(size_t * sizep, const char *fmt, ...) 3070228753Smm{ 3071228753Smm char filename[8192]; 3072228753Smm struct stat st; 3073228753Smm va_list ap; 3074228753Smm char *p; 3075228753Smm ssize_t bytes_read; 3076228753Smm FILE *f; 3077228753Smm int r; 3078228753Smm 3079228753Smm va_start(ap, fmt); 3080228753Smm vsprintf(filename, fmt, ap); 3081228753Smm va_end(ap); 3082228753Smm 3083228753Smm f = fopen(filename, "rb"); 3084228753Smm if (f == NULL) { 3085228753Smm /* Note: No error; non-existent file is okay here. */ 3086228753Smm return (NULL); 3087228753Smm } 3088228753Smm r = fstat(fileno(f), &st); 3089228753Smm if (r != 0) { 3090228753Smm logprintf("Can't stat file %s\n", filename); 3091228753Smm fclose(f); 3092228753Smm return (NULL); 3093228753Smm } 3094228753Smm p = malloc((size_t)st.st_size + 1); 3095228753Smm if (p == NULL) { 3096228753Smm logprintf("Can't allocate %ld bytes of memory to read file %s\n", 3097228753Smm (long int)st.st_size, filename); 3098228753Smm fclose(f); 3099228753Smm return (NULL); 3100228753Smm } 3101228753Smm bytes_read = fread(p, 1, (size_t)st.st_size, f); 3102228753Smm if (bytes_read < st.st_size) { 3103228753Smm logprintf("Can't read file %s\n", filename); 3104228753Smm fclose(f); 3105228753Smm free(p); 3106228753Smm return (NULL); 3107228753Smm } 3108228753Smm p[st.st_size] = '\0'; 3109228753Smm if (sizep != NULL) 3110228753Smm *sizep = (size_t)st.st_size; 3111228753Smm fclose(f); 3112228753Smm return (p); 3113228753Smm} 3114228753Smm 3115299425Smm/* 3116299425Smm * Slurp a file into memory for ease of comparison and testing. 3117299425Smm * Returns size of file in 'sizep' if non-NULL, null-terminates 3118299425Smm * data in memory for ease of use. 3119299425Smm */ 3120299425Smmvoid 3121299425Smmdumpfile(const char *filename, void *data, size_t len) 3122299425Smm{ 3123299425Smm ssize_t bytes_written; 3124299425Smm FILE *f; 3125299425Smm 3126299425Smm f = fopen(filename, "wb"); 3127299425Smm if (f == NULL) { 3128299425Smm logprintf("Can't open file %s for writing\n", filename); 3129299425Smm return; 3130299425Smm } 3131299425Smm bytes_written = fwrite(data, 1, len, f); 3132299425Smm if (bytes_written < (ssize_t)len) 3133299425Smm logprintf("Can't write file %s\n", filename); 3134299425Smm fclose(f); 3135299425Smm} 3136299425Smm 3137228753Smm/* Read a uuencoded file from the reference directory, decode, and 3138228753Smm * write the result into the current directory. */ 3139299425Smm#define VALID_UUDECODE(c) (c >= 32 && c <= 96) 3140228753Smm#define UUDECODE(c) (((c) - 0x20) & 0x3f) 3141228753Smmvoid 3142228753Smmextract_reference_file(const char *name) 3143228753Smm{ 3144228753Smm char buff[1024]; 3145228753Smm FILE *in, *out; 3146228753Smm 3147228753Smm sprintf(buff, "%s/%s.uu", refdir, name); 3148228753Smm in = fopen(buff, "r"); 3149228753Smm failure("Couldn't open reference file %s", buff); 3150228753Smm assert(in != NULL); 3151228753Smm if (in == NULL) 3152228753Smm return; 3153228753Smm /* Read up to and including the 'begin' line. */ 3154228753Smm for (;;) { 3155228753Smm if (fgets(buff, sizeof(buff), in) == NULL) { 3156228753Smm /* TODO: This is a failure. */ 3157228753Smm return; 3158228753Smm } 3159228753Smm if (memcmp(buff, "begin ", 6) == 0) 3160228753Smm break; 3161228753Smm } 3162228753Smm /* Now, decode the rest and write it. */ 3163228753Smm out = fopen(name, "wb"); 3164228753Smm while (fgets(buff, sizeof(buff), in) != NULL) { 3165228753Smm char *p = buff; 3166228753Smm int bytes; 3167228753Smm 3168228753Smm if (memcmp(buff, "end", 3) == 0) 3169228753Smm break; 3170228753Smm 3171228753Smm bytes = UUDECODE(*p++); 3172228753Smm while (bytes > 0) { 3173228753Smm int n = 0; 3174228753Smm /* Write out 1-3 bytes from that. */ 3175228753Smm if (bytes > 0) { 3176299425Smm assert(VALID_UUDECODE(p[0])); 3177299425Smm assert(VALID_UUDECODE(p[1])); 3178228753Smm n = UUDECODE(*p++) << 18; 3179228753Smm n |= UUDECODE(*p++) << 12; 3180228753Smm fputc(n >> 16, out); 3181228753Smm --bytes; 3182228753Smm } 3183228753Smm if (bytes > 0) { 3184299425Smm assert(VALID_UUDECODE(p[0])); 3185228753Smm n |= UUDECODE(*p++) << 6; 3186228753Smm fputc((n >> 8) & 0xFF, out); 3187228753Smm --bytes; 3188228753Smm } 3189228753Smm if (bytes > 0) { 3190299425Smm assert(VALID_UUDECODE(p[0])); 3191228753Smm n |= UUDECODE(*p++); 3192228753Smm fputc(n & 0xFF, out); 3193228753Smm --bytes; 3194228753Smm } 3195228753Smm } 3196228753Smm } 3197228753Smm fclose(out); 3198228753Smm fclose(in); 3199228753Smm} 3200228753Smm 3201299425Smmvoid 3202299425Smmcopy_reference_file(const char *name) 3203299425Smm{ 3204299425Smm char buff[1024]; 3205299425Smm FILE *in, *out; 3206299425Smm size_t rbytes; 3207299425Smm 3208299425Smm sprintf(buff, "%s/%s", refdir, name); 3209299425Smm in = fopen(buff, "rb"); 3210299425Smm failure("Couldn't open reference file %s", buff); 3211299425Smm assert(in != NULL); 3212299425Smm if (in == NULL) 3213299425Smm return; 3214299425Smm /* Now, decode the rest and write it. */ 3215299425Smm /* Not a lot of error checking here; the input better be right. */ 3216299425Smm out = fopen(name, "wb"); 3217299425Smm while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) { 3218299425Smm if (fwrite(buff, 1, rbytes, out) != rbytes) { 3219299425Smm logprintf("Error: fwrite\n"); 3220299425Smm break; 3221299425Smm } 3222299425Smm } 3223299425Smm fclose(out); 3224299425Smm fclose(in); 3225299425Smm} 3226299425Smm 3227231200Smmint 3228231200Smmis_LargeInode(const char *file) 3229231200Smm{ 3230231200Smm#if defined(_WIN32) && !defined(__CYGWIN__) 3231231200Smm BY_HANDLE_FILE_INFORMATION bhfi; 3232231200Smm int r; 3233231200Smm 3234231200Smm r = my_GetFileInformationByName(file, &bhfi); 3235231200Smm if (r != 0) 3236231200Smm return (0); 3237231200Smm return (bhfi.nFileIndexHigh & 0x0000FFFFUL); 3238231200Smm#else 3239231200Smm struct stat st; 3240231200Smm int64_t ino; 3241231200Smm 3242231200Smm if (stat(file, &st) < 0) 3243231200Smm return (0); 3244231200Smm ino = (int64_t)st.st_ino; 3245231200Smm return (ino > 0xffffffff); 3246231200Smm#endif 3247231200Smm} 3248248590Smm 3249248590Smmvoid 3250248590Smmextract_reference_files(const char **names) 3251248590Smm{ 3252248590Smm while (names && *names) 3253248590Smm extract_reference_file(*names++); 3254248590Smm} 3255248590Smm 3256314565Smm#ifndef PROGRAM 3257310796Smm/* Set ACLs */ 3258314565Smmint 3259314565Smmassertion_entry_set_acls(const char *file, int line, struct archive_entry *ae, 3260310796Smm struct archive_test_acl_t *acls, int n) 3261310796Smm{ 3262314565Smm int i, r, ret; 3263310796Smm 3264314565Smm assertion_count(file, line); 3265314565Smm 3266314565Smm ret = 0; 3267310796Smm archive_entry_acl_clear(ae); 3268310796Smm for (i = 0; i < n; i++) { 3269314565Smm r = archive_entry_acl_add_entry(ae, 3270310796Smm acls[i].type, acls[i].permset, acls[i].tag, 3271310796Smm acls[i].qual, acls[i].name); 3272314565Smm if (r != 0) { 3273314565Smm ret = 1; 3274358090Smm failure_start(file, line, "type=%#010x, " 3275314565Smm "permset=%#010x, tag=%d, qual=%d name=%s", 3276314565Smm acls[i].type, acls[i].permset, acls[i].tag, 3277314565Smm acls[i].qual, acls[i].name); 3278314565Smm failure_finish(NULL); 3279314565Smm } 3280310796Smm } 3281314565Smm 3282314565Smm return (ret); 3283310796Smm} 3284310796Smm 3285310796Smmstatic int 3286310796Smmarchive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset, 3287310796Smm int tag, int qual, const char *name) 3288310796Smm{ 3289310796Smm if (type != acl->type) 3290310796Smm return (0); 3291310796Smm if (permset != acl->permset) 3292310796Smm return (0); 3293310796Smm if (tag != acl->tag) 3294310796Smm return (0); 3295310796Smm if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ) 3296310796Smm return (1); 3297310796Smm if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) 3298310796Smm return (1); 3299310796Smm if (tag == ARCHIVE_ENTRY_ACL_EVERYONE) 3300310796Smm return (1); 3301310796Smm if (tag == ARCHIVE_ENTRY_ACL_OTHER) 3302310796Smm return (1); 3303310796Smm if (qual != acl->qual) 3304310796Smm return (0); 3305310796Smm if (name == NULL) { 3306310796Smm if (acl->name == NULL || acl->name[0] == '\0') 3307310796Smm return (1); 3308310796Smm return (0); 3309310796Smm } 3310310796Smm if (acl->name == NULL) { 3311310796Smm if (name[0] == '\0') 3312310796Smm return (1); 3313310796Smm return (0); 3314310796Smm } 3315310796Smm return (0 == strcmp(name, acl->name)); 3316310796Smm} 3317310796Smm 3318310796Smm/* Compare ACLs */ 3319314565Smmint 3320314565Smmassertion_entry_compare_acls(const char *file, int line, 3321314565Smm struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt, 3322314565Smm int want_type, int mode) 3323310796Smm{ 3324310796Smm int *marker; 3325314565Smm int i, r, n, ret; 3326310796Smm int type, permset, tag, qual; 3327310796Smm int matched; 3328310796Smm const char *name; 3329310796Smm 3330314565Smm assertion_count(file, line); 3331314565Smm 3332314565Smm ret = 0; 3333310796Smm n = 0; 3334310796Smm marker = malloc(sizeof(marker[0]) * cnt); 3335310796Smm 3336310796Smm for (i = 0; i < cnt; i++) { 3337310796Smm if ((acls[i].type & want_type) != 0) { 3338310796Smm marker[n] = i; 3339310796Smm n++; 3340310796Smm } 3341310796Smm } 3342310796Smm 3343314565Smm if (n == 0) { 3344314565Smm failure_start(file, line, "No ACL's to compare, type mask: %d", 3345314565Smm want_type); 3346314565Smm return (1); 3347314565Smm } 3348310796Smm 3349310796Smm while (0 == (r = archive_entry_acl_next(ae, want_type, 3350310796Smm &type, &permset, &tag, &qual, &name))) { 3351310796Smm for (i = 0, matched = 0; i < n && !matched; i++) { 3352310796Smm if (archive_test_acl_match(&acls[marker[i]], type, 3353310796Smm permset, tag, qual, name)) { 3354310796Smm /* We found a match; remove it. */ 3355310796Smm marker[i] = marker[n - 1]; 3356310796Smm n--; 3357310796Smm matched = 1; 3358310796Smm } 3359310796Smm } 3360310796Smm if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS 3361310796Smm && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) { 3362314565Smm if (!matched) { 3363314565Smm failure_start(file, line, "No match for " 3364314565Smm "user_obj perm"); 3365314565Smm failure_finish(NULL); 3366314565Smm ret = 1; 3367314565Smm } 3368314565Smm if ((permset << 6) != (mode & 0700)) { 3369314565Smm failure_start(file, line, "USER_OBJ permset " 3370314565Smm "(%02o) != user mode (%02o)", permset, 3371314565Smm 07 & (mode >> 6)); 3372314565Smm failure_finish(NULL); 3373314565Smm ret = 1; 3374314565Smm } 3375310796Smm } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS 3376310796Smm && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) { 3377314565Smm if (!matched) { 3378314565Smm failure_start(file, line, "No match for " 3379314565Smm "group_obj perm"); 3380314565Smm failure_finish(NULL); 3381314565Smm ret = 1; 3382314565Smm } 3383314565Smm if ((permset << 3) != (mode & 0070)) { 3384314565Smm failure_start(file, line, "GROUP_OBJ permset " 3385314565Smm "(%02o) != group mode (%02o)", permset, 3386314565Smm 07 & (mode >> 3)); 3387314565Smm failure_finish(NULL); 3388314565Smm ret = 1; 3389314565Smm } 3390310796Smm } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS 3391310796Smm && tag == ARCHIVE_ENTRY_ACL_OTHER) { 3392314565Smm if (!matched) { 3393314565Smm failure_start(file, line, "No match for " 3394314565Smm "other perm"); 3395314565Smm failure_finish(NULL); 3396314565Smm ret = 1; 3397314565Smm } 3398314565Smm if ((permset << 0) != (mode & 0007)) { 3399314565Smm failure_start(file, line, "OTHER permset " 3400314565Smm "(%02o) != other mode (%02o)", permset, 3401314565Smm mode & 07); 3402314565Smm failure_finish(NULL); 3403314565Smm ret = 1; 3404314565Smm } 3405314565Smm } else if (matched != 1) { 3406314565Smm failure_start(file, line, "Could not find match for " 3407314565Smm "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d," 3408310796Smm "name=``%s'')", type, permset, tag, qual, name); 3409314565Smm failure_finish(NULL); 3410314565Smm ret = 1; 3411310796Smm } 3412310796Smm } 3413314565Smm if (r != ARCHIVE_EOF) { 3414314565Smm failure_start(file, line, "Should not exit before EOF"); 3415314565Smm failure_finish(NULL); 3416314565Smm ret = 1; 3417314565Smm } 3418314565Smm if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 && 3419314565Smm (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) { 3420314565Smm failure_start(file, line, "Mode (%02o) and entry mode (%02o) " 3421314565Smm "mismatch", mode, archive_entry_mode(ae)); 3422314565Smm failure_finish(NULL); 3423314565Smm ret = 1; 3424314565Smm } 3425314565Smm if (n != 0) { 3426314565Smm failure_start(file, line, "Could not find match for ACL " 3427314565Smm "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')", 3428314565Smm acls[marker[0]].type, acls[marker[0]].permset, 3429314565Smm acls[marker[0]].tag, acls[marker[0]].qual, 3430314565Smm acls[marker[0]].name); 3431314565Smm failure_finish(NULL); 3432314565Smm ret = 1; 3433314565Smm /* Number of ACLs not matched should == 0 */ 3434314565Smm } 3435310796Smm free(marker); 3436314565Smm return (ret); 3437310796Smm} 3438314565Smm#endif /* !defined(PROGRAM) */ 3439310796Smm 3440228753Smm/* 3441228753Smm * 3442228753Smm * TEST management 3443228753Smm * 3444228753Smm */ 3445228753Smm 3446228753Smm/* 3447228753Smm * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has 3448228753Smm * a line like 3449228753Smm * DEFINE_TEST(test_function) 3450228753Smm * for each test. 3451228753Smm */ 3452228753Smm 3453228753Smm/* Use "list.h" to declare all of the test functions. */ 3454228753Smm#undef DEFINE_TEST 3455228753Smm#define DEFINE_TEST(name) void name(void); 3456228753Smm#include "list.h" 3457228753Smm 3458228753Smm/* Use "list.h" to create a list of all tests (functions and names). */ 3459228753Smm#undef DEFINE_TEST 3460228753Smm#define DEFINE_TEST(n) { n, #n, 0 }, 3461248590Smmstruct test_list_t tests[] = { 3462228753Smm #include "list.h" 3463228753Smm}; 3464228753Smm 3465228753Smm/* 3466228753Smm * Summarize repeated failures in the just-completed test. 3467228753Smm */ 3468228753Smmstatic void 3469299425Smmtest_summarize(int failed, int skips_num) 3470228753Smm{ 3471228753Smm unsigned int i; 3472228753Smm 3473228753Smm switch (verbosity) { 3474228753Smm case VERBOSITY_SUMMARY_ONLY: 3475228753Smm printf(failed ? "E" : "."); 3476228753Smm fflush(stdout); 3477228753Smm break; 3478228753Smm case VERBOSITY_PASSFAIL: 3479299425Smm printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n"); 3480228753Smm break; 3481228753Smm } 3482228753Smm 3483228753Smm log_console = (verbosity == VERBOSITY_LIGHT_REPORT); 3484228753Smm 3485228753Smm for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { 3486228753Smm if (failed_lines[i].count > 1 && !failed_lines[i].skip) 3487228753Smm logprintf("%s:%d: Summary: Failed %d times\n", 3488231200Smm failed_filename, i, failed_lines[i].count); 3489228753Smm } 3490228753Smm /* Clear the failure history for the next file. */ 3491231200Smm failed_filename = NULL; 3492228753Smm memset(failed_lines, 0, sizeof(failed_lines)); 3493228753Smm} 3494228753Smm 3495228753Smm/* 3496228753Smm * Actually run a single test, with appropriate setup and cleanup. 3497228753Smm */ 3498228753Smmstatic int 3499228753Smmtest_run(int i, const char *tmpdir) 3500228753Smm{ 3501342361Smm#ifdef PATH_MAX 3502358090Smm char workdir[PATH_MAX * 2]; 3503342361Smm#else 3504358090Smm char workdir[1024 * 2]; 3505342361Smm#endif 3506228753Smm char logfilename[64]; 3507228753Smm int failures_before = failures; 3508299425Smm int skips_before = skips; 3509228753Smm int oldumask; 3510228753Smm 3511228753Smm switch (verbosity) { 3512228753Smm case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */ 3513228753Smm break; 3514228753Smm case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */ 3515299425Smm printf("%3d: %-64s", i, tests[i].name); 3516228753Smm fflush(stdout); 3517228753Smm break; 3518228753Smm default: /* Title of test, details will follow */ 3519228753Smm printf("%3d: %s\n", i, tests[i].name); 3520228753Smm } 3521228753Smm 3522228753Smm /* Chdir to the top-level work directory. */ 3523228753Smm if (!assertChdir(tmpdir)) { 3524228753Smm fprintf(stderr, 3525228753Smm "ERROR: Can't chdir to top work dir %s\n", tmpdir); 3526228753Smm exit(1); 3527228753Smm } 3528228753Smm /* Create a log file for this test. */ 3529228753Smm sprintf(logfilename, "%s.log", tests[i].name); 3530228753Smm logfile = fopen(logfilename, "w"); 3531228753Smm fprintf(logfile, "%s\n\n", tests[i].name); 3532228753Smm /* Chdir() to a work dir for this specific test. */ 3533231200Smm snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name); 3534231200Smm testworkdir = workdir; 3535231200Smm if (!assertMakeDir(testworkdir, 0755) 3536231200Smm || !assertChdir(testworkdir)) { 3537228753Smm fprintf(stderr, 3538231200Smm "ERROR: Can't chdir to work dir %s\n", testworkdir); 3539228753Smm exit(1); 3540228753Smm } 3541228753Smm /* Explicitly reset the locale before each test. */ 3542228753Smm setlocale(LC_ALL, "C"); 3543228753Smm /* Record the umask before we run the test. */ 3544228753Smm umask(oldumask = umask(0)); 3545228753Smm /* 3546228753Smm * Run the actual test. 3547228753Smm */ 3548228753Smm (*tests[i].func)(); 3549228753Smm /* 3550228753Smm * Clean up and report afterwards. 3551228753Smm */ 3552231200Smm testworkdir = NULL; 3553228753Smm /* Restore umask */ 3554228753Smm umask(oldumask); 3555228753Smm /* Reset locale. */ 3556228753Smm setlocale(LC_ALL, "C"); 3557228753Smm /* Reset directory. */ 3558228753Smm if (!assertChdir(tmpdir)) { 3559228753Smm fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n", 3560228753Smm tmpdir); 3561228753Smm exit(1); 3562228753Smm } 3563228753Smm /* Report per-test summaries. */ 3564228753Smm tests[i].failures = failures - failures_before; 3565299425Smm test_summarize(tests[i].failures, skips - skips_before); 3566228753Smm /* Close the per-test log file. */ 3567228753Smm fclose(logfile); 3568228753Smm logfile = NULL; 3569228753Smm /* If there were no failures, we can remove the work dir and logfile. */ 3570228753Smm if (tests[i].failures == 0) { 3571228753Smm if (!keep_temp_files && assertChdir(tmpdir)) { 3572228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 3573228753Smm /* Make sure not to leave empty directories. 3574228753Smm * Sometimes a processing of closing files used by tests 3575228753Smm * is not done, then rmdir will be failed and it will 3576228753Smm * leave a empty test directory. So we should wait a few 3577228753Smm * seconds and retry rmdir. */ 3578228753Smm int r, t; 3579228753Smm for (t = 0; t < 10; t++) { 3580228753Smm if (t > 0) 3581228753Smm Sleep(1000); 3582228753Smm r = systemf("rmdir /S /Q %s", tests[i].name); 3583228753Smm if (r == 0) 3584228753Smm break; 3585228753Smm } 3586228753Smm systemf("del %s", logfilename); 3587228753Smm#else 3588228753Smm systemf("rm -rf %s", tests[i].name); 3589228753Smm systemf("rm %s", logfilename); 3590228753Smm#endif 3591228753Smm } 3592228753Smm } 3593228753Smm /* Return appropriate status. */ 3594228753Smm return (tests[i].failures); 3595228753Smm} 3596228753Smm 3597228753Smm/* 3598228753Smm * 3599228753Smm * 3600228753Smm * MAIN and support routines. 3601228753Smm * 3602228753Smm * 3603228753Smm */ 3604228753Smm 3605228753Smmstatic void 3606228753Smmusage(const char *program) 3607228753Smm{ 3608228753Smm static const int limit = sizeof(tests) / sizeof(tests[0]); 3609228753Smm int i; 3610228753Smm 3611228753Smm printf("Usage: %s [options] <test> <test> ...\n", program); 3612228753Smm printf("Default is to run all tests.\n"); 3613228753Smm printf("Otherwise, specify the numbers of the tests you wish to run.\n"); 3614228753Smm printf("Options:\n"); 3615228753Smm printf(" -d Dump core after any failure, for debugging.\n"); 3616228753Smm printf(" -k Keep all temp files.\n"); 3617228753Smm printf(" Default: temp files for successful tests deleted.\n"); 3618228753Smm#ifdef PROGRAM 3619228753Smm printf(" -p <path> Path to executable to be tested.\n"); 3620228753Smm printf(" Default: path taken from " ENVBASE " environment variable.\n"); 3621228753Smm#endif 3622228753Smm printf(" -q Quiet.\n"); 3623228753Smm printf(" -r <dir> Path to dir containing reference files.\n"); 3624228753Smm printf(" Default: Current directory.\n"); 3625231200Smm printf(" -u Keep running specifies tests until one fails.\n"); 3626228753Smm printf(" -v Verbose.\n"); 3627228753Smm printf("Available tests:\n"); 3628228753Smm for (i = 0; i < limit; i++) 3629228753Smm printf(" %d: %s\n", i, tests[i].name); 3630228753Smm exit(1); 3631228753Smm} 3632228753Smm 3633228753Smmstatic char * 3634228753Smmget_refdir(const char *d) 3635228753Smm{ 3636302003Smm size_t tried_size, buff_size; 3637302003Smm char *buff, *tried, *pwd = NULL, *p = NULL; 3638228753Smm 3639302003Smm#ifdef PATH_MAX 3640302003Smm buff_size = PATH_MAX; 3641302003Smm#else 3642302003Smm buff_size = 8192; 3643302003Smm#endif 3644302003Smm buff = calloc(buff_size, 1); 3645302003Smm if (buff == NULL) { 3646302003Smm fprintf(stderr, "Unable to allocate memory\n"); 3647302003Smm exit(1); 3648302003Smm } 3649302003Smm 3650302003Smm /* Allocate a buffer to hold the various directories we checked. */ 3651302003Smm tried_size = buff_size * 2; 3652302003Smm tried = calloc(tried_size, 1); 3653302003Smm if (tried == NULL) { 3654302003Smm fprintf(stderr, "Unable to allocate memory\n"); 3655302003Smm exit(1); 3656302003Smm } 3657302003Smm 3658228753Smm /* If a dir was specified, try that */ 3659228753Smm if (d != NULL) { 3660228753Smm pwd = NULL; 3661302003Smm snprintf(buff, buff_size, "%s", d); 3662228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3663228753Smm if (p != NULL) goto success; 3664302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3665302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3666228753Smm goto failure; 3667228753Smm } 3668228753Smm 3669228753Smm /* Get the current dir. */ 3670231200Smm#ifdef PATH_MAX 3671231200Smm pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 3672231200Smm#else 3673228753Smm pwd = getcwd(NULL, 0); 3674231200Smm#endif 3675228753Smm while (pwd[strlen(pwd) - 1] == '\n') 3676228753Smm pwd[strlen(pwd) - 1] = '\0'; 3677228753Smm 3678228753Smm /* Look for a known file. */ 3679302003Smm snprintf(buff, buff_size, "%s", pwd); 3680228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3681228753Smm if (p != NULL) goto success; 3682302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3683302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3684228753Smm 3685302003Smm snprintf(buff, buff_size, "%s/test", pwd); 3686228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3687228753Smm if (p != NULL) goto success; 3688302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3689302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3690228753Smm 3691228753Smm#if defined(LIBRARY) 3692302003Smm snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY); 3693228753Smm#else 3694302003Smm snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM); 3695228753Smm#endif 3696228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3697228753Smm if (p != NULL) goto success; 3698302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3699302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3700228753Smm 3701231200Smm#if defined(PROGRAM_ALIAS) 3702302003Smm snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS); 3703231200Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3704231200Smm if (p != NULL) goto success; 3705302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3706302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3707231200Smm#endif 3708231200Smm 3709228753Smm if (memcmp(pwd, "/usr/obj", 8) == 0) { 3710302003Smm snprintf(buff, buff_size, "%s", pwd + 8); 3711228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3712228753Smm if (p != NULL) goto success; 3713302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3714302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3715228753Smm 3716302003Smm snprintf(buff, buff_size, "%s/test", pwd + 8); 3717228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3718228753Smm if (p != NULL) goto success; 3719302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 3720302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 3721228753Smm } 3722228753Smm 3723228753Smmfailure: 3724228753Smm printf("Unable to locate known reference file %s\n", KNOWNREF); 3725228753Smm printf(" Checked following directories:\n%s\n", tried); 3726299425Smm printf("Use -r option to specify full path to reference directory\n"); 3727228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) 3728228753Smm DebugBreak(); 3729228753Smm#endif 3730228753Smm exit(1); 3731228753Smm 3732228753Smmsuccess: 3733228753Smm free(p); 3734228753Smm free(pwd); 3735302003Smm free(tried); 3736302003Smm 3737302003Smm /* Copy result into a fresh buffer to reduce memory usage. */ 3738302003Smm p = strdup(buff); 3739302003Smm free(buff); 3740302003Smm return p; 3741228753Smm} 3742228753Smm 3743228753Smmint 3744228753Smmmain(int argc, char **argv) 3745228753Smm{ 3746228753Smm static const int limit = sizeof(tests) / sizeof(tests[0]); 3747238825Smm int test_set[sizeof(tests) / sizeof(tests[0])]; 3748238825Smm int i = 0, j = 0, tests_run = 0, tests_failed = 0, option; 3749228753Smm time_t now; 3750228753Smm char *refdir_alloc = NULL; 3751228753Smm const char *progname; 3752231200Smm char **saved_argv; 3753228753Smm const char *tmp, *option_arg, *p; 3754342361Smm#ifdef PATH_MAX 3755342361Smm char tmpdir[PATH_MAX]; 3756342361Smm#else 3757342361Smm char tmpdir[256]; 3758342361Smm#endif 3759342361Smm char *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL; 3760342361Smm char tmpdir_timestamp[32]; 3761228753Smm 3762228753Smm (void)argc; /* UNUSED */ 3763228753Smm 3764231200Smm /* Get the current dir. */ 3765231200Smm#ifdef PATH_MAX 3766231200Smm pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 3767231200Smm#else 3768231200Smm pwd = getcwd(NULL, 0); 3769231200Smm#endif 3770231200Smm while (pwd[strlen(pwd) - 1] == '\n') 3771231200Smm pwd[strlen(pwd) - 1] = '\0'; 3772231200Smm 3773299425Smm#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 3774228753Smm /* To stop to run the default invalid parameter handler. */ 3775228753Smm _set_invalid_parameter_handler(invalid_parameter_handler); 3776228753Smm /* Disable annoying assertion message box. */ 3777228753Smm _CrtSetReportMode(_CRT_ASSERT, 0); 3778228753Smm#endif 3779228753Smm 3780228753Smm /* 3781228753Smm * Name of this program, used to build root of our temp directory 3782228753Smm * tree. 3783228753Smm */ 3784228753Smm progname = p = argv[0]; 3785231200Smm if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL) 3786231200Smm { 3787231200Smm fprintf(stderr, "ERROR: Out of memory."); 3788231200Smm exit(1); 3789231200Smm } 3790231200Smm strcpy(testprogdir, progname); 3791228753Smm while (*p != '\0') { 3792228753Smm /* Support \ or / dir separators for Windows compat. */ 3793228753Smm if (*p == '/' || *p == '\\') 3794231200Smm { 3795228753Smm progname = p + 1; 3796231200Smm i = j; 3797231200Smm } 3798228753Smm ++p; 3799231200Smm j++; 3800228753Smm } 3801231200Smm testprogdir[i] = '\0'; 3802232133Smm#if defined(_WIN32) && !defined(__CYGWIN__) 3803232133Smm if (testprogdir[0] != '/' && testprogdir[0] != '\\' && 3804232133Smm !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') || 3805232133Smm (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) && 3806232133Smm testprogdir[1] == ':' && 3807232133Smm (testprogdir[2] == '/' || testprogdir[2] == '\\'))) 3808232133Smm#else 3809231200Smm if (testprogdir[0] != '/') 3810232133Smm#endif 3811231200Smm { 3812231200Smm /* Fixup path for relative directories. */ 3813231200Smm if ((testprogdir = (char *)realloc(testprogdir, 3814231200Smm strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL) 3815231200Smm { 3816231200Smm fprintf(stderr, "ERROR: Out of memory."); 3817231200Smm exit(1); 3818231200Smm } 3819232133Smm memmove(testprogdir + strlen(pwd) + 1, testprogdir, 3820299425Smm strlen(testprogdir) + 1); 3821232133Smm memcpy(testprogdir, pwd, strlen(pwd)); 3822231200Smm testprogdir[strlen(pwd)] = '/'; 3823231200Smm } 3824228753Smm 3825228753Smm#ifdef PROGRAM 3826228753Smm /* Get the target program from environment, if available. */ 3827228753Smm testprogfile = getenv(ENVBASE); 3828228753Smm#endif 3829228753Smm 3830228753Smm if (getenv("TMPDIR") != NULL) 3831228753Smm tmp = getenv("TMPDIR"); 3832228753Smm else if (getenv("TMP") != NULL) 3833228753Smm tmp = getenv("TMP"); 3834228753Smm else if (getenv("TEMP") != NULL) 3835228753Smm tmp = getenv("TEMP"); 3836228753Smm else if (getenv("TEMPDIR") != NULL) 3837228753Smm tmp = getenv("TEMPDIR"); 3838228753Smm else 3839228753Smm tmp = "/tmp"; 3840228753Smm 3841228753Smm /* Allow -d to be controlled through the environment. */ 3842228753Smm if (getenv(ENVBASE "_DEBUG") != NULL) 3843228753Smm dump_on_failure = 1; 3844228753Smm 3845238825Smm /* Allow -v to be controlled through the environment. */ 3846238825Smm if (getenv("_VERBOSITY_LEVEL") != NULL) 3847238825Smm { 3848238825Smm vlevel = getenv("_VERBOSITY_LEVEL"); 3849238825Smm verbosity = atoi(vlevel); 3850238825Smm if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL) 3851238825Smm { 3852238825Smm /* Unsupported verbosity levels are silently ignored */ 3853238825Smm vlevel = NULL; 3854238825Smm verbosity = VERBOSITY_PASSFAIL; 3855238825Smm } 3856238825Smm } 3857238825Smm 3858228753Smm /* Get the directory holding test files from environment. */ 3859228753Smm refdir = getenv(ENVBASE "_TEST_FILES"); 3860228753Smm 3861228753Smm /* 3862228753Smm * Parse options, without using getopt(), which isn't available 3863228753Smm * on all platforms. 3864228753Smm */ 3865228753Smm ++argv; /* Skip program name */ 3866228753Smm while (*argv != NULL) { 3867228753Smm if (**argv != '-') 3868228753Smm break; 3869228753Smm p = *argv++; 3870228753Smm ++p; /* Skip '-' */ 3871228753Smm while (*p != '\0') { 3872228753Smm option = *p++; 3873228753Smm option_arg = NULL; 3874228753Smm /* If 'opt' takes an argument, parse that. */ 3875228753Smm if (option == 'p' || option == 'r') { 3876228753Smm if (*p != '\0') 3877228753Smm option_arg = p; 3878228753Smm else if (*argv == NULL) { 3879228753Smm fprintf(stderr, 3880228753Smm "Option -%c requires argument.\n", 3881228753Smm option); 3882228753Smm usage(progname); 3883228753Smm } else 3884228753Smm option_arg = *argv++; 3885228753Smm p = ""; /* End of this option word. */ 3886228753Smm } 3887228753Smm 3888228753Smm /* Now, handle the option. */ 3889228753Smm switch (option) { 3890228753Smm case 'd': 3891228753Smm dump_on_failure = 1; 3892228753Smm break; 3893228753Smm case 'k': 3894228753Smm keep_temp_files = 1; 3895228753Smm break; 3896228753Smm case 'p': 3897228753Smm#ifdef PROGRAM 3898228753Smm testprogfile = option_arg; 3899228753Smm#else 3900231200Smm fprintf(stderr, "-p option not permitted\n"); 3901228753Smm usage(progname); 3902228753Smm#endif 3903228753Smm break; 3904228753Smm case 'q': 3905238825Smm if (!vlevel) 3906238825Smm verbosity--; 3907228753Smm break; 3908228753Smm case 'r': 3909228753Smm refdir = option_arg; 3910228753Smm break; 3911231200Smm case 'u': 3912231200Smm until_failure++; 3913231200Smm break; 3914228753Smm case 'v': 3915238825Smm if (!vlevel) 3916238825Smm verbosity++; 3917228753Smm break; 3918228753Smm default: 3919231200Smm fprintf(stderr, "Unrecognized option '%c'\n", 3920231200Smm option); 3921228753Smm usage(progname); 3922228753Smm } 3923228753Smm } 3924228753Smm } 3925228753Smm 3926228753Smm /* 3927228753Smm * Sanity-check that our options make sense. 3928228753Smm */ 3929228753Smm#ifdef PROGRAM 3930228753Smm if (testprogfile == NULL) 3931228753Smm { 3932231200Smm if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 + 3933231200Smm strlen(PROGRAM) + 1)) == NULL) 3934231200Smm { 3935231200Smm fprintf(stderr, "ERROR: Out of memory."); 3936231200Smm exit(1); 3937231200Smm } 3938231200Smm strcpy(tmp2, testprogdir); 3939231200Smm strcat(tmp2, "/"); 3940231200Smm strcat(tmp2, PROGRAM); 3941231200Smm testprogfile = tmp2; 3942231200Smm } 3943231200Smm 3944231200Smm { 3945228753Smm char *testprg; 3946228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 3947228753Smm /* Command.com sometimes rejects '/' separators. */ 3948228753Smm testprg = strdup(testprogfile); 3949228753Smm for (i = 0; testprg[i] != '\0'; i++) { 3950228753Smm if (testprg[i] == '/') 3951228753Smm testprg[i] = '\\'; 3952228753Smm } 3953228753Smm testprogfile = testprg; 3954228753Smm#endif 3955228753Smm /* Quote the name that gets put into shell command lines. */ 3956228753Smm testprg = malloc(strlen(testprogfile) + 3); 3957228753Smm strcpy(testprg, "\""); 3958228753Smm strcat(testprg, testprogfile); 3959228753Smm strcat(testprg, "\""); 3960228753Smm testprog = testprg; 3961228753Smm } 3962228753Smm#endif 3963228753Smm 3964231200Smm#if !defined(_WIN32) && defined(SIGPIPE) 3965231200Smm { /* Ignore SIGPIPE signals */ 3966231200Smm struct sigaction sa; 3967231200Smm sa.sa_handler = SIG_IGN; 3968231200Smm sigemptyset(&sa.sa_mask); 3969231200Smm sa.sa_flags = 0; 3970231200Smm sigaction(SIGPIPE, &sa, NULL); 3971231200Smm } 3972231200Smm#endif 3973231200Smm 3974228753Smm /* 3975228753Smm * Create a temp directory for the following tests. 3976228753Smm * Include the time the tests started as part of the name, 3977228753Smm * to make it easier to track the results of multiple tests. 3978228753Smm */ 3979228753Smm now = time(NULL); 3980228753Smm for (i = 0; ; i++) { 3981228753Smm strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), 3982228753Smm "%Y-%m-%dT%H.%M.%S", 3983228753Smm localtime(&now)); 3984342361Smm if ((strlen(tmp) + 1 + strlen(progname) + 1 + 3985342361Smm strlen(tmpdir_timestamp) + 1 + 3) > 3986342361Smm (sizeof(tmpdir) / sizeof(char))) { 3987342361Smm fprintf(stderr, 3988342361Smm "ERROR: Temp directory pathname too long\n"); 3989342361Smm exit(1); 3990342361Smm } 3991342361Smm snprintf(tmpdir, sizeof(tmpdir), "%s/%s.%s-%03d", tmp, 3992342361Smm progname, tmpdir_timestamp, i); 3993228753Smm if (assertMakeDir(tmpdir,0755)) 3994228753Smm break; 3995228753Smm if (i >= 999) { 3996228753Smm fprintf(stderr, 3997228753Smm "ERROR: Unable to create temp directory %s\n", 3998228753Smm tmpdir); 3999228753Smm exit(1); 4000228753Smm } 4001228753Smm } 4002228753Smm 4003228753Smm /* 4004228753Smm * If the user didn't specify a directory for locating 4005228753Smm * reference files, try to find the reference files in 4006228753Smm * the "usual places." 4007228753Smm */ 4008228753Smm refdir = refdir_alloc = get_refdir(refdir); 4009228753Smm 4010228753Smm /* 4011228753Smm * Banner with basic information. 4012228753Smm */ 4013228753Smm printf("\n"); 4014228753Smm printf("If tests fail or crash, details will be in:\n"); 4015228753Smm printf(" %s\n", tmpdir); 4016228753Smm printf("\n"); 4017228753Smm if (verbosity > VERBOSITY_SUMMARY_ONLY) { 4018228753Smm printf("Reference files will be read from: %s\n", refdir); 4019228753Smm#ifdef PROGRAM 4020228753Smm printf("Running tests on: %s\n", testprog); 4021228753Smm#endif 4022228753Smm printf("Exercising: "); 4023228753Smm fflush(stdout); 4024228753Smm printf("%s\n", EXTRA_VERSION); 4025228753Smm } else { 4026228753Smm printf("Running "); 4027228753Smm fflush(stdout); 4028228753Smm } 4029228753Smm 4030228753Smm /* 4031228753Smm * Run some or all of the individual tests. 4032228753Smm */ 4033231200Smm saved_argv = argv; 4034231200Smm do { 4035231200Smm argv = saved_argv; 4036238825Smm do { 4037238825Smm int test_num; 4038238825Smm 4039248590Smm test_num = get_test_set(test_set, limit, *argv, tests); 4040238825Smm if (test_num < 0) { 4041238825Smm printf("*** INVALID Test %s\n", *argv); 4042238825Smm free(refdir_alloc); 4043248590Smm free(testprogdir); 4044238825Smm usage(progname); 4045238825Smm return (1); 4046238825Smm } 4047238825Smm for (i = 0; i < test_num; i++) { 4048231200Smm tests_run++; 4049238825Smm if (test_run(test_set[i], tmpdir)) { 4050231200Smm tests_failed++; 4051231200Smm if (until_failure) 4052231200Smm goto finish; 4053228753Smm } 4054231200Smm } 4055238825Smm if (*argv != NULL) 4056231200Smm argv++; 4057238825Smm } while (*argv != NULL); 4058231200Smm } while (until_failure); 4059228753Smm 4060231200Smmfinish: 4061231200Smm /* Must be freed after all tests run */ 4062231200Smm free(tmp2); 4063231200Smm free(testprogdir); 4064231200Smm free(pwd); 4065231200Smm 4066228753Smm /* 4067228753Smm * Report summary statistics. 4068228753Smm */ 4069228753Smm if (verbosity > VERBOSITY_SUMMARY_ONLY) { 4070228753Smm printf("\n"); 4071228753Smm printf("Totals:\n"); 4072228753Smm printf(" Tests run: %8d\n", tests_run); 4073228753Smm printf(" Tests failed: %8d\n", tests_failed); 4074228753Smm printf(" Assertions checked:%8d\n", assertions); 4075228753Smm printf(" Assertions failed: %8d\n", failures); 4076228753Smm printf(" Skips reported: %8d\n", skips); 4077228753Smm } 4078228753Smm if (failures) { 4079228753Smm printf("\n"); 4080228753Smm printf("Failing tests:\n"); 4081228753Smm for (i = 0; i < limit; ++i) { 4082228753Smm if (tests[i].failures) 4083228753Smm printf(" %d: %s (%d failures)\n", i, 4084228753Smm tests[i].name, tests[i].failures); 4085228753Smm } 4086228753Smm printf("\n"); 4087228753Smm printf("Details for failing tests: %s\n", tmpdir); 4088228753Smm printf("\n"); 4089228753Smm } else { 4090228753Smm if (verbosity == VERBOSITY_SUMMARY_ONLY) 4091228753Smm printf("\n"); 4092228753Smm printf("%d tests passed, no failures\n", tests_run); 4093228753Smm } 4094228753Smm 4095228753Smm free(refdir_alloc); 4096228753Smm 4097228753Smm /* If the final tmpdir is empty, we can remove it. */ 4098228753Smm /* This should be the usual case when all tests succeed. */ 4099228753Smm assertChdir(".."); 4100228753Smm rmdir(tmpdir); 4101228753Smm 4102228753Smm return (tests_failed ? 1 : 0); 4103228753Smm} 4104