1228753Smm/* 2228753Smm * Copyright (c) 2003-2009 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm 26228753Smm#include "test.h" 27248616Smm#include "test_utils.h" 28238856Smm#ifdef HAVE_SYS_IOCTL_H 29238856Smm#include <sys/ioctl.h> 30238856Smm#endif 31232153Smm#ifdef HAVE_SYS_TIME_H 32232153Smm#include <sys/time.h> 33232153Smm#endif 34228753Smm#include <errno.h> 35232153Smm#ifdef HAVE_ICONV_H 36232153Smm#include <iconv.h> 37232153Smm#endif 38238856Smm/* 39238856Smm * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h. 40238856Smm * As the include guards don't agree, the order of include is important. 41238856Smm */ 42238856Smm#ifdef HAVE_LINUX_EXT2_FS_H 43238856Smm#include <linux/ext2_fs.h> /* for Linux file flags */ 44238856Smm#endif 45238856Smm#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__) 46238856Smm#include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */ 47238856Smm#endif 48232153Smm#include <limits.h> 49228753Smm#include <locale.h> 50232153Smm#ifdef HAVE_SIGNAL_H 51232153Smm#include <signal.h> 52232153Smm#endif 53228753Smm#include <stdarg.h> 54228753Smm#include <time.h> 55228753Smm 56228753Smm/* 57228753Smm * This same file is used pretty much verbatim for all test harnesses. 58228753Smm * 59228753Smm * The next few lines are the only differences. 60228753Smm * TODO: Move this into a separate configuration header, have all test 61228753Smm * suites share one copy of this file. 62228753Smm */ 63228763Smm__FBSDID("$FreeBSD$"); 64228753Smm#define KNOWNREF "test_patterns_2.tar.uu" 65228753Smm#define ENVBASE "BSDTAR" /* Prefix for environment variables. */ 66228753Smm#define PROGRAM "bsdtar" /* Name of program being tested. */ 67232153Smm#define PROGRAM_ALIAS "tar" /* Generic alias for program */ 68232153Smm#undef LIBRARY /* Not testing a library. */ 69232153Smm#undef EXTRA_DUMP /* How to dump extra data */ 70232153Smm#undef EXTRA_ERRNO /* How to dump errno */ 71228753Smm/* How to generate extra version info. */ 72228753Smm#define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "") 73228753Smm 74228753Smm/* 75228753Smm * 76228753Smm * Windows support routines 77228753Smm * 78228753Smm * Note: Configuration is a tricky issue. Using HAVE_* feature macros 79228753Smm * in the test harness is dangerous because they cover up 80228753Smm * configuration errors. The classic example of this is omitting a 81228753Smm * configure check. If libarchive and libarchive_test both look for 82228753Smm * the same feature macro, such errors are hard to detect. Platform 83228753Smm * macros (e.g., _WIN32 or __GNUC__) are a little better, but can 84228753Smm * easily lead to very messy code. It's best to limit yourself 85228753Smm * to only the most generic programming techniques in the test harness 86228753Smm * and thus avoid conditionals altogether. Where that's not possible, 87228753Smm * try to minimize conditionals by grouping platform-specific tests in 88228753Smm * one place (e.g., test_acl_freebsd) or by adding new assert() 89228753Smm * functions (e.g., assertMakeHardlink()) to cover up platform 90228753Smm * differences. Platform-specific coding in libarchive_test is often 91228753Smm * a symptom that some capability is missing from libarchive itself. 92228753Smm */ 93228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 94228753Smm#include <io.h> 95248616Smm#include <direct.h> 96228753Smm#include <windows.h> 97228753Smm#ifndef F_OK 98228753Smm#define F_OK (0) 99228753Smm#endif 100228753Smm#ifndef S_ISDIR 101228753Smm#define S_ISDIR(m) ((m) & _S_IFDIR) 102228753Smm#endif 103228753Smm#ifndef S_ISREG 104228753Smm#define S_ISREG(m) ((m) & _S_IFREG) 105228753Smm#endif 106228753Smm#if !defined(__BORLANDC__) 107228753Smm#define access _access 108228753Smm#undef chdir 109228753Smm#define chdir _chdir 110228753Smm#endif 111228753Smm#ifndef fileno 112228753Smm#define fileno _fileno 113228753Smm#endif 114228753Smm/*#define fstat _fstat64*/ 115228753Smm#if !defined(__BORLANDC__) 116228753Smm#define getcwd _getcwd 117228753Smm#endif 118228753Smm#define lstat stat 119228753Smm/*#define lstat _stat64*/ 120228753Smm/*#define stat _stat64*/ 121228753Smm#define rmdir _rmdir 122228753Smm#if !defined(__BORLANDC__) 123228753Smm#define strdup _strdup 124228753Smm#define umask _umask 125228753Smm#endif 126228753Smm#define int64_t __int64 127228753Smm#endif 128228753Smm 129228753Smm#if defined(HAVE__CrtSetReportMode) 130228753Smm# include <crtdbg.h> 131228753Smm#endif 132228753Smm 133228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 134238856Smmstatic void *GetFunctionKernel32(const char *); 135238856Smmstatic int my_CreateSymbolicLinkA(const char *, const char *, int); 136238856Smmstatic int my_CreateHardLinkA(const char *, const char *); 137238856Smmstatic int my_GetFileInformationByName(const char *, 138238856Smm BY_HANDLE_FILE_INFORMATION *); 139238856Smm 140238856Smmstatic void * 141238856SmmGetFunctionKernel32(const char *name) 142228753Smm{ 143228753Smm static HINSTANCE lib; 144228753Smm static int set; 145228753Smm if (!set) { 146228753Smm set = 1; 147228753Smm lib = LoadLibrary("kernel32.dll"); 148228753Smm } 149228753Smm if (lib == NULL) { 150228753Smm fprintf(stderr, "Can't load kernel32.dll?!\n"); 151228753Smm exit(1); 152228753Smm } 153228753Smm return (void *)GetProcAddress(lib, name); 154228753Smm} 155228753Smm 156228753Smmstatic int 157228753Smmmy_CreateSymbolicLinkA(const char *linkname, const char *target, int flags) 158228753Smm{ 159228753Smm static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD); 160228753Smm static int set; 161228753Smm if (!set) { 162228753Smm set = 1; 163228753Smm f = GetFunctionKernel32("CreateSymbolicLinkA"); 164228753Smm } 165228753Smm return f == NULL ? 0 : (*f)(linkname, target, flags); 166228753Smm} 167228753Smm 168228753Smmstatic int 169228753Smmmy_CreateHardLinkA(const char *linkname, const char *target) 170228753Smm{ 171228753Smm static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES); 172228753Smm static int set; 173228753Smm if (!set) { 174228753Smm set = 1; 175228753Smm f = GetFunctionKernel32("CreateHardLinkA"); 176228753Smm } 177228753Smm return f == NULL ? 0 : (*f)(linkname, target, NULL); 178228753Smm} 179228753Smm 180238856Smmstatic int 181228753Smmmy_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi) 182228753Smm{ 183228753Smm HANDLE h; 184228753Smm int r; 185228753Smm 186228753Smm memset(bhfi, 0, sizeof(*bhfi)); 187228753Smm h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, 188232153Smm OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 189228753Smm if (h == INVALID_HANDLE_VALUE) 190228753Smm return (0); 191228753Smm r = GetFileInformationByHandle(h, bhfi); 192228753Smm CloseHandle(h); 193228753Smm return (r); 194228753Smm} 195228753Smm#endif 196228753Smm 197228753Smm#if defined(HAVE__CrtSetReportMode) 198228753Smmstatic void 199228753Smminvalid_parameter_handler(const wchar_t * expression, 200228753Smm const wchar_t * function, const wchar_t * file, 201228753Smm unsigned int line, uintptr_t pReserved) 202228753Smm{ 203228753Smm /* nop */ 204228753Smm} 205228753Smm#endif 206228753Smm 207228753Smm/* 208228753Smm * 209228753Smm * OPTIONS FLAGS 210228753Smm * 211228753Smm */ 212228753Smm 213228753Smm/* Enable core dump on failure. */ 214228753Smmstatic int dump_on_failure = 0; 215228753Smm/* Default is to remove temp dirs and log data for successful tests. */ 216228753Smmstatic int keep_temp_files = 0; 217232153Smm/* Default is to run the specified tests once and report errors. */ 218232153Smmstatic int until_failure = 0; 219228753Smm/* Default is to just report pass/fail for each test. */ 220228753Smmstatic int verbosity = 0; 221228753Smm#define VERBOSITY_SUMMARY_ONLY -1 /* -q */ 222228753Smm#define VERBOSITY_PASSFAIL 0 /* Default */ 223228753Smm#define VERBOSITY_LIGHT_REPORT 1 /* -v */ 224228753Smm#define VERBOSITY_FULL 2 /* -vv */ 225228753Smm/* A few places generate even more output for verbosity > VERBOSITY_FULL, 226228753Smm * mostly for debugging the test harness itself. */ 227228753Smm/* Cumulative count of assertion failures. */ 228228753Smmstatic int failures = 0; 229228753Smm/* Cumulative count of reported skips. */ 230228753Smmstatic int skips = 0; 231228753Smm/* Cumulative count of assertions checked. */ 232228753Smmstatic int assertions = 0; 233228753Smm 234228753Smm/* Directory where uuencoded reference files can be found. */ 235228753Smmstatic const char *refdir; 236228753Smm 237228753Smm/* 238228753Smm * Report log information selectively to console and/or disk log. 239228753Smm */ 240228753Smmstatic int log_console = 0; 241228753Smmstatic FILE *logfile; 242228753Smmstatic void 243228753Smmvlogprintf(const char *fmt, va_list ap) 244228753Smm{ 245228753Smm#ifdef va_copy 246228753Smm va_list lfap; 247228753Smm va_copy(lfap, ap); 248228753Smm#endif 249228753Smm if (log_console) 250228753Smm vfprintf(stdout, fmt, ap); 251228753Smm if (logfile != NULL) 252228753Smm#ifdef va_copy 253228753Smm vfprintf(logfile, fmt, lfap); 254228753Smm va_end(lfap); 255228753Smm#else 256228753Smm vfprintf(logfile, fmt, ap); 257228753Smm#endif 258228753Smm} 259228753Smm 260228753Smmstatic void 261228753Smmlogprintf(const char *fmt, ...) 262228753Smm{ 263228753Smm va_list ap; 264228753Smm va_start(ap, fmt); 265228753Smm vlogprintf(fmt, ap); 266228753Smm va_end(ap); 267228753Smm} 268228753Smm 269228753Smm/* Set up a message to display only if next assertion fails. */ 270228753Smmstatic char msgbuff[4096]; 271228753Smmstatic const char *msg, *nextmsg; 272228753Smmvoid 273228753Smmfailure(const char *fmt, ...) 274228753Smm{ 275228753Smm va_list ap; 276232153Smm if (fmt == NULL) { 277232153Smm nextmsg = NULL; 278232153Smm } else { 279232153Smm va_start(ap, fmt); 280232153Smm vsprintf(msgbuff, fmt, ap); 281232153Smm va_end(ap); 282232153Smm nextmsg = msgbuff; 283232153Smm } 284228753Smm} 285228753Smm 286228753Smm/* 287228753Smm * Copy arguments into file-local variables. 288228753Smm * This was added to permit vararg assert() functions without needing 289228753Smm * variadic wrapper macros. Turns out that the vararg capability is almost 290228753Smm * never used, so almost all of the vararg assertions can be simplified 291228753Smm * by removing the vararg capability and reworking the wrapper macro to 292228753Smm * pass __FILE__, __LINE__ directly into the function instead of using 293228753Smm * this hook. I suspect this machinery is used so rarely that we 294228753Smm * would be better off just removing it entirely. That would simplify 295232153Smm * the code here noticeably. 296228753Smm */ 297232153Smmstatic const char *skipping_filename; 298232153Smmstatic int skipping_line; 299232153Smmvoid skipping_setup(const char *filename, int line) 300228753Smm{ 301232153Smm skipping_filename = filename; 302232153Smm skipping_line = line; 303228753Smm} 304228753Smm 305228753Smm/* Called at the beginning of each assert() function. */ 306228753Smmstatic void 307228753Smmassertion_count(const char *file, int line) 308228753Smm{ 309228753Smm (void)file; /* UNUSED */ 310228753Smm (void)line; /* UNUSED */ 311228753Smm ++assertions; 312228753Smm /* Proper handling of "failure()" message. */ 313228753Smm msg = nextmsg; 314228753Smm nextmsg = NULL; 315228753Smm /* Uncomment to print file:line after every assertion. 316228753Smm * Verbose, but occasionally useful in tracking down crashes. */ 317228753Smm /* printf("Checked %s:%d\n", file, line); */ 318228753Smm} 319228753Smm 320228753Smm/* 321228753Smm * For each test source file, we remember how many times each 322228753Smm * assertion was reported. Cleared before each new test, 323228753Smm * used by test_summarize(). 324228753Smm */ 325228753Smmstatic struct line { 326228753Smm int count; 327228753Smm int skip; 328228753Smm} failed_lines[10000]; 329232153Smmconst char *failed_filename; 330228753Smm 331228753Smm/* Count this failure, setup up log destination and handle initial report. */ 332228753Smmstatic void 333228753Smmfailure_start(const char *filename, int line, const char *fmt, ...) 334228753Smm{ 335228753Smm va_list ap; 336228753Smm 337228753Smm /* Record another failure for this line. */ 338228753Smm ++failures; 339232153Smm failed_filename = filename; 340228753Smm failed_lines[line].count++; 341228753Smm 342228753Smm /* Determine whether to log header to console. */ 343228753Smm switch (verbosity) { 344228753Smm case VERBOSITY_LIGHT_REPORT: 345228753Smm log_console = (failed_lines[line].count < 2); 346228753Smm break; 347228753Smm default: 348232153Smm log_console = (verbosity >= VERBOSITY_FULL); 349228753Smm } 350228753Smm 351228753Smm /* Log file:line header for this failure */ 352228753Smm va_start(ap, fmt); 353228753Smm#if _MSC_VER 354228753Smm logprintf("%s(%d): ", filename, line); 355228753Smm#else 356228753Smm logprintf("%s:%d: ", filename, line); 357228753Smm#endif 358228753Smm vlogprintf(fmt, ap); 359228753Smm va_end(ap); 360228753Smm logprintf("\n"); 361228753Smm 362228753Smm if (msg != NULL && msg[0] != '\0') { 363228753Smm logprintf(" Description: %s\n", msg); 364228753Smm msg = NULL; 365228753Smm } 366228753Smm 367228753Smm /* Determine whether to log details to console. */ 368228753Smm if (verbosity == VERBOSITY_LIGHT_REPORT) 369228753Smm log_console = 0; 370228753Smm} 371228753Smm 372228753Smm/* Complete reporting of failed tests. */ 373228753Smm/* 374228753Smm * The 'extra' hook here is used by libarchive to include libarchive 375228753Smm * error messages with assertion failures. It could also be used 376228753Smm * to add strerror() output, for example. Just define the EXTRA_DUMP() 377228753Smm * macro appropriately. 378228753Smm */ 379228753Smmstatic void 380228753Smmfailure_finish(void *extra) 381228753Smm{ 382228753Smm (void)extra; /* UNUSED (maybe) */ 383228753Smm#ifdef EXTRA_DUMP 384232153Smm if (extra != NULL) { 385232153Smm logprintf(" errno: %d\n", EXTRA_ERRNO(extra)); 386228753Smm logprintf(" detail: %s\n", EXTRA_DUMP(extra)); 387232153Smm } 388228753Smm#endif 389228753Smm 390228753Smm if (dump_on_failure) { 391228753Smm fprintf(stderr, 392228753Smm " *** forcing core dump so failure can be debugged ***\n"); 393232153Smm abort(); 394228753Smm } 395228753Smm} 396228753Smm 397228753Smm/* Inform user that we're skipping some checks. */ 398228753Smmvoid 399228753Smmtest_skipping(const char *fmt, ...) 400228753Smm{ 401228753Smm char buff[1024]; 402228753Smm va_list ap; 403228753Smm 404228753Smm va_start(ap, fmt); 405228753Smm vsprintf(buff, fmt, ap); 406228753Smm va_end(ap); 407232153Smm /* Use failure() message if set. */ 408232153Smm msg = nextmsg; 409232153Smm nextmsg = NULL; 410228753Smm /* failure_start() isn't quite right, but is awfully convenient. */ 411232153Smm failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff); 412228753Smm --failures; /* Undo failures++ in failure_start() */ 413228753Smm /* Don't failure_finish() here. */ 414228753Smm /* Mark as skip, so doesn't count as failed test. */ 415232153Smm failed_lines[skipping_line].skip = 1; 416228753Smm ++skips; 417228753Smm} 418228753Smm 419228753Smm/* 420228753Smm * 421228753Smm * ASSERTIONS 422228753Smm * 423228753Smm */ 424228753Smm 425228753Smm/* Generic assert() just displays the failed condition. */ 426228753Smmint 427228753Smmassertion_assert(const char *file, int line, int value, 428228753Smm const char *condition, void *extra) 429228753Smm{ 430228753Smm assertion_count(file, line); 431228753Smm if (!value) { 432228753Smm failure_start(file, line, "Assertion failed: %s", condition); 433228753Smm failure_finish(extra); 434228753Smm } 435228753Smm return (value); 436228753Smm} 437228753Smm 438228753Smm/* chdir() and report any errors */ 439228753Smmint 440228753Smmassertion_chdir(const char *file, int line, const char *pathname) 441228753Smm{ 442228753Smm assertion_count(file, line); 443228753Smm if (chdir(pathname) == 0) 444228753Smm return (1); 445228753Smm failure_start(file, line, "chdir(\"%s\")", pathname); 446228753Smm failure_finish(NULL); 447228753Smm return (0); 448228753Smm 449228753Smm} 450228753Smm 451228753Smm/* Verify two integers are equal. */ 452228753Smmint 453228753Smmassertion_equal_int(const char *file, int line, 454228753Smm long long v1, const char *e1, long long v2, const char *e2, void *extra) 455228753Smm{ 456228753Smm assertion_count(file, line); 457228753Smm if (v1 == v2) 458228753Smm return (1); 459228753Smm failure_start(file, line, "%s != %s", e1, e2); 460228753Smm logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1); 461228753Smm logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2); 462228753Smm failure_finish(extra); 463228753Smm return (0); 464228753Smm} 465228753Smm 466232153Smm/* 467232153Smm * Utility to convert a single UTF-8 sequence. 468232153Smm */ 469232153Smmstatic int 470232153Smm_utf8_to_unicode(uint32_t *pwc, const char *s, size_t n) 471228753Smm{ 472232153Smm static const char utf8_count[256] = { 473232153Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */ 474232153Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */ 475232153Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */ 476232153Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */ 477232153Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */ 478232153Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */ 479232153Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */ 480232153Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */ 481232153Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */ 482232153Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */ 483232153Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */ 484232153Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */ 485232153Smm 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */ 486232153Smm 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */ 487232153Smm 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */ 488232153Smm 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */ 489232153Smm }; 490232153Smm int ch; 491232153Smm int cnt; 492232153Smm uint32_t wc; 493232153Smm 494232153Smm *pwc = 0; 495232153Smm 496232153Smm /* Sanity check. */ 497232153Smm if (n == 0) 498232153Smm return (0); 499232153Smm /* 500232153Smm * Decode 1-4 bytes depending on the value of the first byte. 501232153Smm */ 502232153Smm ch = (unsigned char)*s; 503232153Smm if (ch == 0) 504232153Smm return (0); /* Standard: return 0 for end-of-string. */ 505232153Smm cnt = utf8_count[ch]; 506232153Smm 507232153Smm /* Invalide sequence or there are not plenty bytes. */ 508232153Smm if (n < (size_t)cnt) 509232153Smm return (-1); 510232153Smm 511232153Smm /* Make a Unicode code point from a single UTF-8 sequence. */ 512232153Smm switch (cnt) { 513232153Smm case 1: /* 1 byte sequence. */ 514232153Smm *pwc = ch & 0x7f; 515232153Smm return (cnt); 516232153Smm case 2: /* 2 bytes sequence. */ 517232153Smm if ((s[1] & 0xc0) != 0x80) return (-1); 518232153Smm *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f); 519232153Smm return (cnt); 520232153Smm case 3: /* 3 bytes sequence. */ 521232153Smm if ((s[1] & 0xc0) != 0x80) return (-1); 522232153Smm if ((s[2] & 0xc0) != 0x80) return (-1); 523232153Smm wc = ((ch & 0x0f) << 12) 524232153Smm | ((s[1] & 0x3f) << 6) 525232153Smm | (s[2] & 0x3f); 526232153Smm if (wc < 0x800) 527232153Smm return (-1);/* Overlong sequence. */ 528232153Smm break; 529232153Smm case 4: /* 4 bytes sequence. */ 530232153Smm if (n < 4) 531232153Smm return (-1); 532232153Smm if ((s[1] & 0xc0) != 0x80) return (-1); 533232153Smm if ((s[2] & 0xc0) != 0x80) return (-1); 534232153Smm if ((s[3] & 0xc0) != 0x80) return (-1); 535232153Smm wc = ((ch & 0x07) << 18) 536232153Smm | ((s[1] & 0x3f) << 12) 537232153Smm | ((s[2] & 0x3f) << 6) 538232153Smm | (s[3] & 0x3f); 539232153Smm if (wc < 0x10000) 540232153Smm return (-1);/* Overlong sequence. */ 541232153Smm break; 542232153Smm default: 543232153Smm return (-1); 544232153Smm } 545232153Smm 546232153Smm /* The code point larger than 0x10FFFF is not leagal 547232153Smm * Unicode values. */ 548232153Smm if (wc > 0x10FFFF) 549232153Smm return (-1); 550232153Smm /* Correctly gets a Unicode, returns used bytes. */ 551232153Smm *pwc = wc; 552232153Smm return (cnt); 553232153Smm} 554232153Smm 555232153Smmstatic void strdump(const char *e, const char *p, int ewidth, int utf8) 556232153Smm{ 557228753Smm const char *q = p; 558228753Smm 559232153Smm logprintf(" %*s = ", ewidth, e); 560228753Smm if (p == NULL) { 561232153Smm logprintf("NULL\n"); 562228753Smm return; 563228753Smm } 564228753Smm logprintf("\""); 565228753Smm while (*p != '\0') { 566228753Smm unsigned int c = 0xff & *p++; 567228753Smm switch (c) { 568228753Smm case '\a': printf("\a"); break; 569228753Smm case '\b': printf("\b"); break; 570228753Smm case '\n': printf("\n"); break; 571228753Smm case '\r': printf("\r"); break; 572228753Smm default: 573228753Smm if (c >= 32 && c < 127) 574228753Smm logprintf("%c", c); 575228753Smm else 576228753Smm logprintf("\\x%02X", c); 577228753Smm } 578228753Smm } 579228753Smm logprintf("\""); 580232153Smm logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q)); 581232153Smm 582232153Smm /* 583232153Smm * If the current string is UTF-8, dump its code points. 584232153Smm */ 585232153Smm if (utf8) { 586232153Smm size_t len; 587232153Smm uint32_t uc; 588232153Smm int n; 589232153Smm int cnt = 0; 590232153Smm 591232153Smm p = q; 592232153Smm len = strlen(p); 593232153Smm logprintf(" ["); 594232153Smm while ((n = _utf8_to_unicode(&uc, p, len)) > 0) { 595232153Smm if (p != q) 596232153Smm logprintf(" "); 597232153Smm logprintf("%04X", uc); 598232153Smm p += n; 599232153Smm len -= n; 600232153Smm cnt++; 601232153Smm } 602232153Smm logprintf("]"); 603232153Smm logprintf(" (count %d", cnt); 604232153Smm if (n < 0) { 605232153Smm logprintf(",unknown %d bytes", len); 606232153Smm } 607232153Smm logprintf(")"); 608232153Smm 609232153Smm } 610232153Smm logprintf("\n"); 611228753Smm} 612228753Smm 613228753Smm/* Verify two strings are equal, dump them if not. */ 614228753Smmint 615228753Smmassertion_equal_string(const char *file, int line, 616228753Smm const char *v1, const char *e1, 617228753Smm const char *v2, const char *e2, 618232153Smm void *extra, int utf8) 619228753Smm{ 620232153Smm int l1, l2; 621232153Smm 622228753Smm assertion_count(file, line); 623228753Smm if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0)) 624228753Smm return (1); 625228753Smm failure_start(file, line, "%s != %s", e1, e2); 626248616Smm l1 = (int)strlen(e1); 627248616Smm l2 = (int)strlen(e2); 628232153Smm if (l1 < l2) 629232153Smm l1 = l2; 630232153Smm strdump(e1, v1, l1, utf8); 631232153Smm strdump(e2, v2, l1, utf8); 632228753Smm failure_finish(extra); 633228753Smm return (0); 634228753Smm} 635228753Smm 636228753Smmstatic void 637228753Smmwcsdump(const char *e, const wchar_t *w) 638228753Smm{ 639228753Smm logprintf(" %s = ", e); 640228753Smm if (w == NULL) { 641228753Smm logprintf("(null)"); 642228753Smm return; 643228753Smm } 644228753Smm logprintf("\""); 645228753Smm while (*w != L'\0') { 646228753Smm unsigned int c = *w++; 647228753Smm if (c >= 32 && c < 127) 648228753Smm logprintf("%c", c); 649228753Smm else if (c < 256) 650228753Smm logprintf("\\x%02X", c); 651228753Smm else if (c < 0x10000) 652228753Smm logprintf("\\u%04X", c); 653228753Smm else 654228753Smm logprintf("\\U%08X", c); 655228753Smm } 656228753Smm logprintf("\"\n"); 657228753Smm} 658228753Smm 659228753Smm#ifndef HAVE_WCSCMP 660228753Smmstatic int 661228753Smmwcscmp(const wchar_t *s1, const wchar_t *s2) 662228753Smm{ 663228753Smm 664228753Smm while (*s1 == *s2++) { 665228753Smm if (*s1++ == L'\0') 666228753Smm return 0; 667228753Smm } 668228753Smm if (*s1 > *--s2) 669228753Smm return 1; 670228753Smm else 671228753Smm return -1; 672228753Smm} 673228753Smm#endif 674228753Smm 675228753Smm/* Verify that two wide strings are equal, dump them if not. */ 676228753Smmint 677228753Smmassertion_equal_wstring(const char *file, int line, 678228753Smm const wchar_t *v1, const char *e1, 679228753Smm const wchar_t *v2, const char *e2, 680228753Smm void *extra) 681228753Smm{ 682228753Smm assertion_count(file, line); 683232153Smm if (v1 == v2) 684228753Smm return (1); 685232153Smm if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0) 686232153Smm return (1); 687228753Smm failure_start(file, line, "%s != %s", e1, e2); 688228753Smm wcsdump(e1, v1); 689228753Smm wcsdump(e2, v2); 690228753Smm failure_finish(extra); 691228753Smm return (0); 692228753Smm} 693228753Smm 694228753Smm/* 695228753Smm * Pretty standard hexdump routine. As a bonus, if ref != NULL, then 696228753Smm * any bytes in p that differ from ref will be highlighted with '_' 697228753Smm * before and after the hex value. 698228753Smm */ 699228753Smmstatic void 700228753Smmhexdump(const char *p, const char *ref, size_t l, size_t offset) 701228753Smm{ 702228753Smm size_t i, j; 703228753Smm char sep; 704228753Smm 705228753Smm if (p == NULL) { 706228753Smm logprintf("(null)\n"); 707228753Smm return; 708228753Smm } 709228753Smm for(i=0; i < l; i+=16) { 710228753Smm logprintf("%04x", (unsigned)(i + offset)); 711228753Smm sep = ' '; 712228753Smm for (j = 0; j < 16 && i + j < l; j++) { 713228753Smm if (ref != NULL && p[i + j] != ref[i + j]) 714228753Smm sep = '_'; 715228753Smm logprintf("%c%02x", sep, 0xff & (int)p[i+j]); 716228753Smm if (ref != NULL && p[i + j] == ref[i + j]) 717228753Smm sep = ' '; 718228753Smm } 719228753Smm for (; j < 16; j++) { 720228753Smm logprintf("%c ", sep); 721228753Smm sep = ' '; 722228753Smm } 723228753Smm logprintf("%c", sep); 724228753Smm for (j=0; j < 16 && i + j < l; j++) { 725228753Smm int c = p[i + j]; 726228753Smm if (c >= ' ' && c <= 126) 727228753Smm logprintf("%c", c); 728228753Smm else 729228753Smm logprintf("."); 730228753Smm } 731228753Smm logprintf("\n"); 732228753Smm } 733228753Smm} 734228753Smm 735228753Smm/* Verify that two blocks of memory are the same, display the first 736228753Smm * block of differences if they're not. */ 737228753Smmint 738228753Smmassertion_equal_mem(const char *file, int line, 739228753Smm const void *_v1, const char *e1, 740228753Smm const void *_v2, const char *e2, 741228753Smm size_t l, const char *ld, void *extra) 742228753Smm{ 743228753Smm const char *v1 = (const char *)_v1; 744228753Smm const char *v2 = (const char *)_v2; 745228753Smm size_t offset; 746228753Smm 747228753Smm assertion_count(file, line); 748228753Smm if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0)) 749228753Smm return (1); 750248616Smm if (v1 == NULL || v2 == NULL) 751248616Smm return (0); 752228753Smm 753228753Smm failure_start(file, line, "%s != %s", e1, e2); 754228753Smm logprintf(" size %s = %d\n", ld, (int)l); 755228753Smm /* Dump 48 bytes (3 lines) so that the first difference is 756228753Smm * in the second line. */ 757228753Smm offset = 0; 758228753Smm while (l > 64 && memcmp(v1, v2, 32) == 0) { 759228753Smm /* Two lines agree, so step forward one line. */ 760228753Smm v1 += 16; 761228753Smm v2 += 16; 762228753Smm l -= 16; 763228753Smm offset += 16; 764228753Smm } 765228753Smm logprintf(" Dump of %s\n", e1); 766232153Smm hexdump(v1, v2, l < 128 ? l : 128, offset); 767228753Smm logprintf(" Dump of %s\n", e2); 768232153Smm hexdump(v2, v1, l < 128 ? l : 128, offset); 769228753Smm logprintf("\n"); 770228753Smm failure_finish(extra); 771228753Smm return (0); 772228753Smm} 773228753Smm 774228753Smm/* Verify that the named file exists and is empty. */ 775228753Smmint 776232153Smmassertion_empty_file(const char *filename, int line, const char *f1) 777228753Smm{ 778228753Smm char buff[1024]; 779228753Smm struct stat st; 780228753Smm ssize_t s; 781228753Smm FILE *f; 782228753Smm 783232153Smm assertion_count(filename, line); 784228753Smm 785228753Smm if (stat(f1, &st) != 0) { 786232153Smm failure_start(filename, line, "Stat failed: %s", f1); 787228753Smm failure_finish(NULL); 788228753Smm return (0); 789228753Smm } 790228753Smm if (st.st_size == 0) 791228753Smm return (1); 792228753Smm 793232153Smm failure_start(filename, line, "File should be empty: %s", f1); 794228753Smm logprintf(" File size: %d\n", (int)st.st_size); 795228753Smm logprintf(" Contents:\n"); 796228753Smm f = fopen(f1, "rb"); 797228753Smm if (f == NULL) { 798228753Smm logprintf(" Unable to open %s\n", f1); 799228753Smm } else { 800228753Smm s = ((off_t)sizeof(buff) < st.st_size) ? 801228753Smm (ssize_t)sizeof(buff) : (ssize_t)st.st_size; 802228753Smm s = fread(buff, 1, s, f); 803228753Smm hexdump(buff, NULL, s, 0); 804228753Smm fclose(f); 805228753Smm } 806228753Smm failure_finish(NULL); 807228753Smm return (0); 808228753Smm} 809228753Smm 810228753Smm/* Verify that the named file exists and is not empty. */ 811228753Smmint 812232153Smmassertion_non_empty_file(const char *filename, int line, const char *f1) 813228753Smm{ 814228753Smm struct stat st; 815228753Smm 816232153Smm assertion_count(filename, line); 817228753Smm 818228753Smm if (stat(f1, &st) != 0) { 819232153Smm failure_start(filename, line, "Stat failed: %s", f1); 820228753Smm failure_finish(NULL); 821228753Smm return (0); 822228753Smm } 823228753Smm if (st.st_size == 0) { 824232153Smm failure_start(filename, line, "File empty: %s", f1); 825228753Smm failure_finish(NULL); 826228753Smm return (0); 827228753Smm } 828228753Smm return (1); 829228753Smm} 830228753Smm 831228753Smm/* Verify that two files have the same contents. */ 832228753Smm/* TODO: hexdump the first bytes that actually differ. */ 833228753Smmint 834232153Smmassertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2) 835228753Smm{ 836228753Smm char buff1[1024]; 837228753Smm char buff2[1024]; 838228753Smm FILE *f1, *f2; 839228753Smm int n1, n2; 840228753Smm 841232153Smm assertion_count(filename, line); 842228753Smm 843228753Smm f1 = fopen(fn1, "rb"); 844228753Smm f2 = fopen(fn2, "rb"); 845248616Smm if (f1 == NULL || f2 == NULL) { 846248616Smm if (f1) fclose(f1); 847248616Smm if (f2) fclose(f2); 848248616Smm return (0); 849248616Smm } 850228753Smm for (;;) { 851248616Smm n1 = (int)fread(buff1, 1, sizeof(buff1), f1); 852248616Smm n2 = (int)fread(buff2, 1, sizeof(buff2), f2); 853228753Smm if (n1 != n2) 854228753Smm break; 855228753Smm if (n1 == 0 && n2 == 0) { 856228753Smm fclose(f1); 857228753Smm fclose(f2); 858228753Smm return (1); 859228753Smm } 860228753Smm if (memcmp(buff1, buff2, n1) != 0) 861228753Smm break; 862228753Smm } 863228753Smm fclose(f1); 864228753Smm fclose(f2); 865232153Smm failure_start(filename, line, "Files not identical"); 866228753Smm logprintf(" file1=\"%s\"\n", fn1); 867228753Smm logprintf(" file2=\"%s\"\n", fn2); 868232153Smm failure_finish(NULL); 869228753Smm return (0); 870228753Smm} 871228753Smm 872228753Smm/* Verify that the named file does exist. */ 873228753Smmint 874232153Smmassertion_file_exists(const char *filename, int line, const char *f) 875228753Smm{ 876232153Smm assertion_count(filename, line); 877228753Smm 878228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 879228753Smm if (!_access(f, 0)) 880228753Smm return (1); 881228753Smm#else 882228753Smm if (!access(f, F_OK)) 883228753Smm return (1); 884228753Smm#endif 885232153Smm failure_start(filename, line, "File should exist: %s", f); 886232153Smm failure_finish(NULL); 887228753Smm return (0); 888228753Smm} 889228753Smm 890228753Smm/* Verify that the named file doesn't exist. */ 891228753Smmint 892232153Smmassertion_file_not_exists(const char *filename, int line, const char *f) 893228753Smm{ 894232153Smm assertion_count(filename, line); 895228753Smm 896228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 897228753Smm if (_access(f, 0)) 898228753Smm return (1); 899228753Smm#else 900228753Smm if (access(f, F_OK)) 901228753Smm return (1); 902228753Smm#endif 903232153Smm failure_start(filename, line, "File should not exist: %s", f); 904232153Smm failure_finish(NULL); 905228753Smm return (0); 906228753Smm} 907228753Smm 908228753Smm/* Compare the contents of a file to a block of memory. */ 909228753Smmint 910232153Smmassertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn) 911228753Smm{ 912228753Smm char *contents; 913228753Smm FILE *f; 914228753Smm int n; 915228753Smm 916232153Smm assertion_count(filename, line); 917228753Smm 918228753Smm f = fopen(fn, "rb"); 919228753Smm if (f == NULL) { 920232153Smm failure_start(filename, line, 921228753Smm "File should exist: %s", fn); 922232153Smm failure_finish(NULL); 923228753Smm return (0); 924228753Smm } 925228753Smm contents = malloc(s * 2); 926248616Smm n = (int)fread(contents, 1, s * 2, f); 927228753Smm fclose(f); 928228753Smm if (n == s && memcmp(buff, contents, s) == 0) { 929228753Smm free(contents); 930228753Smm return (1); 931228753Smm } 932232153Smm failure_start(filename, line, "File contents don't match"); 933228753Smm logprintf(" file=\"%s\"\n", fn); 934228753Smm if (n > 0) 935228753Smm hexdump(contents, buff, n > 512 ? 512 : n, 0); 936228753Smm else { 937228753Smm logprintf(" File empty, contents should be:\n"); 938228753Smm hexdump(buff, NULL, s > 512 ? 512 : s, 0); 939228753Smm } 940232153Smm failure_finish(NULL); 941228753Smm free(contents); 942228753Smm return (0); 943228753Smm} 944228753Smm 945228753Smm/* Check the contents of a text file, being tolerant of line endings. */ 946228753Smmint 947232153Smmassertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn) 948228753Smm{ 949228753Smm char *contents; 950228753Smm const char *btxt, *ftxt; 951228753Smm FILE *f; 952228753Smm int n, s; 953228753Smm 954232153Smm assertion_count(filename, line); 955228753Smm f = fopen(fn, "r"); 956232153Smm if (f == NULL) { 957232153Smm failure_start(filename, line, 958232153Smm "File doesn't exist: %s", fn); 959232153Smm failure_finish(NULL); 960232153Smm return (0); 961232153Smm } 962248616Smm s = (int)strlen(buff); 963228753Smm contents = malloc(s * 2 + 128); 964248616Smm n = (int)fread(contents, 1, s * 2 + 128 - 1, f); 965228753Smm if (n >= 0) 966228753Smm contents[n] = '\0'; 967228753Smm fclose(f); 968228753Smm /* Compare texts. */ 969228753Smm btxt = buff; 970228753Smm ftxt = (const char *)contents; 971228753Smm while (*btxt != '\0' && *ftxt != '\0') { 972228753Smm if (*btxt == *ftxt) { 973228753Smm ++btxt; 974228753Smm ++ftxt; 975228753Smm continue; 976228753Smm } 977228753Smm if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') { 978228753Smm /* Pass over different new line characters. */ 979228753Smm ++btxt; 980228753Smm ftxt += 2; 981228753Smm continue; 982228753Smm } 983228753Smm break; 984228753Smm } 985228753Smm if (*btxt == '\0' && *ftxt == '\0') { 986228753Smm free(contents); 987228753Smm return (1); 988228753Smm } 989232153Smm failure_start(filename, line, "Contents don't match"); 990228753Smm logprintf(" file=\"%s\"\n", fn); 991232153Smm if (n > 0) { 992228753Smm hexdump(contents, buff, n, 0); 993232153Smm logprintf(" expected\n", fn); 994232153Smm hexdump(buff, contents, s, 0); 995232153Smm } else { 996228753Smm logprintf(" File empty, contents should be:\n"); 997228753Smm hexdump(buff, NULL, s, 0); 998228753Smm } 999232153Smm failure_finish(NULL); 1000228753Smm free(contents); 1001228753Smm return (0); 1002228753Smm} 1003228753Smm 1004228753Smm/* Verify that a text file contains the specified lines, regardless of order */ 1005228753Smm/* This could be more efficient if we sorted both sets of lines, etc, but 1006228753Smm * since this is used only for testing and only ever deals with a dozen or so 1007228753Smm * lines at a time, this relatively crude approach is just fine. */ 1008228753Smmint 1009228753Smmassertion_file_contains_lines_any_order(const char *file, int line, 1010228753Smm const char *pathname, const char *lines[]) 1011228753Smm{ 1012228753Smm char *buff; 1013228753Smm size_t buff_size; 1014228753Smm size_t expected_count, actual_count, i, j; 1015248616Smm char **expected = NULL; 1016248616Smm char *p, **actual = NULL; 1017228753Smm char c; 1018228753Smm int expected_failure = 0, actual_failure = 0; 1019228753Smm 1020228753Smm assertion_count(file, line); 1021228753Smm 1022228753Smm buff = slurpfile(&buff_size, "%s", pathname); 1023228753Smm if (buff == NULL) { 1024228753Smm failure_start(pathname, line, "Can't read file: %s", pathname); 1025228753Smm failure_finish(NULL); 1026228753Smm return (0); 1027228753Smm } 1028228753Smm 1029248616Smm /* Make a copy of the provided lines and count up the expected 1030248616Smm * file size. */ 1031228753Smm for (i = 0; lines[i] != NULL; ++i) { 1032228753Smm } 1033228753Smm expected_count = i; 1034248616Smm if (expected_count) { 1035248616Smm expected = malloc(sizeof(char *) * expected_count); 1036248616Smm if (expected == NULL) { 1037248616Smm failure_start(pathname, line, "Can't allocate memory"); 1038248616Smm failure_finish(NULL); 1039248616Smm free(expected); 1040248616Smm return (0); 1041248616Smm } 1042248616Smm for (i = 0; lines[i] != NULL; ++i) { 1043248616Smm expected[i] = strdup(lines[i]); 1044248616Smm } 1045228753Smm } 1046228753Smm 1047232153Smm /* Break the file into lines */ 1048228753Smm actual_count = 0; 1049228753Smm for (c = '\0', p = buff; p < buff + buff_size; ++p) { 1050228753Smm if (*p == '\x0d' || *p == '\x0a') 1051228753Smm *p = '\0'; 1052228753Smm if (c == '\0' && *p != '\0') 1053228753Smm ++actual_count; 1054228753Smm c = *p; 1055228753Smm } 1056248616Smm if (actual_count) { 1057248616Smm actual = calloc(sizeof(char *), actual_count); 1058248616Smm if (actual == NULL) { 1059248616Smm failure_start(pathname, line, "Can't allocate memory"); 1060248616Smm failure_finish(NULL); 1061248616Smm free(expected); 1062248616Smm return (0); 1063228753Smm } 1064248616Smm for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) { 1065248616Smm if (*p != '\0') { 1066248616Smm actual[j] = p; 1067248616Smm ++j; 1068248616Smm } 1069248616Smm } 1070228753Smm } 1071228753Smm 1072232153Smm /* Erase matching lines from both lists */ 1073228753Smm for (i = 0; i < expected_count; ++i) { 1074228753Smm if (expected[i] == NULL) 1075228753Smm continue; 1076228753Smm for (j = 0; j < actual_count; ++j) { 1077228753Smm if (actual[j] == NULL) 1078228753Smm continue; 1079228753Smm if (strcmp(expected[i], actual[j]) == 0) { 1080228753Smm free(expected[i]); 1081228753Smm expected[i] = NULL; 1082228753Smm actual[j] = NULL; 1083228753Smm break; 1084228753Smm } 1085228753Smm } 1086228753Smm } 1087228753Smm 1088232153Smm /* If there's anything left, it's a failure */ 1089228753Smm for (i = 0; i < expected_count; ++i) { 1090228753Smm if (expected[i] != NULL) 1091228753Smm ++expected_failure; 1092228753Smm } 1093228753Smm for (j = 0; j < actual_count; ++j) { 1094228753Smm if (actual[j] != NULL) 1095228753Smm ++actual_failure; 1096228753Smm } 1097228753Smm if (expected_failure == 0 && actual_failure == 0) { 1098228753Smm free(buff); 1099228753Smm free(expected); 1100228753Smm free(actual); 1101228753Smm return (1); 1102228753Smm } 1103228753Smm failure_start(file, line, "File doesn't match: %s", pathname); 1104228753Smm for (i = 0; i < expected_count; ++i) { 1105228753Smm if (expected[i] != NULL) { 1106228753Smm logprintf(" Expected but not present: %s\n", expected[i]); 1107228753Smm free(expected[i]); 1108228753Smm } 1109228753Smm } 1110228753Smm for (j = 0; j < actual_count; ++j) { 1111228753Smm if (actual[j] != NULL) 1112228753Smm logprintf(" Present but not expected: %s\n", actual[j]); 1113228753Smm } 1114228753Smm failure_finish(NULL); 1115228753Smm free(buff); 1116228753Smm free(expected); 1117228753Smm free(actual); 1118228753Smm return (0); 1119228753Smm} 1120228753Smm 1121228753Smm/* Test that two paths point to the same file. */ 1122228753Smm/* As a side-effect, asserts that both files exist. */ 1123228753Smmstatic int 1124228753Smmis_hardlink(const char *file, int line, 1125228753Smm const char *path1, const char *path2) 1126228753Smm{ 1127228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1128228753Smm BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2; 1129228753Smm int r; 1130228753Smm 1131228753Smm assertion_count(file, line); 1132228753Smm r = my_GetFileInformationByName(path1, &bhfi1); 1133228753Smm if (r == 0) { 1134228753Smm failure_start(file, line, "File %s can't be inspected?", path1); 1135228753Smm failure_finish(NULL); 1136228753Smm return (0); 1137228753Smm } 1138228753Smm r = my_GetFileInformationByName(path2, &bhfi2); 1139228753Smm if (r == 0) { 1140228753Smm failure_start(file, line, "File %s can't be inspected?", path2); 1141228753Smm failure_finish(NULL); 1142228753Smm return (0); 1143228753Smm } 1144228753Smm return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber 1145228753Smm && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh 1146228753Smm && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow); 1147228753Smm#else 1148228753Smm struct stat st1, st2; 1149228753Smm int r; 1150228753Smm 1151228753Smm assertion_count(file, line); 1152228753Smm r = lstat(path1, &st1); 1153228753Smm if (r != 0) { 1154228753Smm failure_start(file, line, "File should exist: %s", path1); 1155228753Smm failure_finish(NULL); 1156228753Smm return (0); 1157228753Smm } 1158228753Smm r = lstat(path2, &st2); 1159228753Smm if (r != 0) { 1160228753Smm failure_start(file, line, "File should exist: %s", path2); 1161228753Smm failure_finish(NULL); 1162228753Smm return (0); 1163228753Smm } 1164228753Smm return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev); 1165228753Smm#endif 1166228753Smm} 1167228753Smm 1168228753Smmint 1169228753Smmassertion_is_hardlink(const char *file, int line, 1170228753Smm const char *path1, const char *path2) 1171228753Smm{ 1172228753Smm if (is_hardlink(file, line, path1, path2)) 1173228753Smm return (1); 1174228753Smm failure_start(file, line, 1175228753Smm "Files %s and %s are not hardlinked", path1, path2); 1176228753Smm failure_finish(NULL); 1177228753Smm return (0); 1178228753Smm} 1179228753Smm 1180228753Smmint 1181228753Smmassertion_is_not_hardlink(const char *file, int line, 1182228753Smm const char *path1, const char *path2) 1183228753Smm{ 1184228753Smm if (!is_hardlink(file, line, path1, path2)) 1185228753Smm return (1); 1186228753Smm failure_start(file, line, 1187228753Smm "Files %s and %s should not be hardlinked", path1, path2); 1188228753Smm failure_finish(NULL); 1189228753Smm return (0); 1190228753Smm} 1191228753Smm 1192228753Smm/* Verify a/b/mtime of 'pathname'. */ 1193228753Smm/* If 'recent', verify that it's within last 10 seconds. */ 1194228753Smmstatic int 1195228753Smmassertion_file_time(const char *file, int line, 1196228753Smm const char *pathname, long t, long nsec, char type, int recent) 1197228753Smm{ 1198228753Smm long long filet, filet_nsec; 1199228753Smm int r; 1200228753Smm 1201228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1202228753Smm#define EPOC_TIME (116444736000000000ULL) 1203248616Smm FILETIME fxtime, fbirthtime, fatime, fmtime; 1204228753Smm ULARGE_INTEGER wintm; 1205228753Smm HANDLE h; 1206248616Smm fxtime.dwLowDateTime = 0; 1207248616Smm fxtime.dwHighDateTime = 0; 1208228753Smm 1209228753Smm assertion_count(file, line); 1210232153Smm /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open 1211232153Smm * a directory file. If not, CreateFile() will fail when 1212232153Smm * the pathname is a directory. */ 1213228753Smm h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL, 1214232153Smm OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 1215228753Smm if (h == INVALID_HANDLE_VALUE) { 1216228753Smm failure_start(file, line, "Can't access %s\n", pathname); 1217228753Smm failure_finish(NULL); 1218228753Smm return (0); 1219228753Smm } 1220228753Smm r = GetFileTime(h, &fbirthtime, &fatime, &fmtime); 1221228753Smm switch (type) { 1222248616Smm case 'a': fxtime = fatime; break; 1223248616Smm case 'b': fxtime = fbirthtime; break; 1224248616Smm case 'm': fxtime = fmtime; break; 1225228753Smm } 1226228753Smm CloseHandle(h); 1227228753Smm if (r == 0) { 1228228753Smm failure_start(file, line, "Can't GetFileTime %s\n", pathname); 1229228753Smm failure_finish(NULL); 1230228753Smm return (0); 1231228753Smm } 1232248616Smm wintm.LowPart = fxtime.dwLowDateTime; 1233248616Smm wintm.HighPart = fxtime.dwHighDateTime; 1234228753Smm filet = (wintm.QuadPart - EPOC_TIME) / 10000000; 1235228753Smm filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100; 1236228753Smm nsec = (nsec / 100) * 100; /* Round the request */ 1237228753Smm#else 1238228753Smm struct stat st; 1239228753Smm 1240228753Smm assertion_count(file, line); 1241228753Smm r = lstat(pathname, &st); 1242228753Smm if (r != 0) { 1243228753Smm failure_start(file, line, "Can't stat %s\n", pathname); 1244228753Smm failure_finish(NULL); 1245228753Smm return (0); 1246228753Smm } 1247228753Smm switch (type) { 1248228753Smm case 'a': filet = st.st_atime; break; 1249228753Smm case 'm': filet = st.st_mtime; break; 1250228753Smm case 'b': filet = 0; break; 1251228753Smm default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1252228753Smm exit(1); 1253228753Smm } 1254228753Smm#if defined(__FreeBSD__) 1255228753Smm switch (type) { 1256228753Smm case 'a': filet_nsec = st.st_atimespec.tv_nsec; break; 1257228753Smm case 'b': filet = st.st_birthtime; 1258228753Smm filet_nsec = st.st_birthtimespec.tv_nsec; break; 1259228753Smm case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break; 1260228753Smm default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1261228753Smm exit(1); 1262228753Smm } 1263228753Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1264228753Smm filet_nsec = (filet_nsec / 1000) * 1000; 1265228753Smm nsec = (nsec / 1000) * 1000; 1266228753Smm#else 1267228753Smm filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */ 1268228753Smm if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */ 1269228753Smm#if defined(__HAIKU__) 1270228753Smm if (type == 'a') return (1); /* Haiku doesn't have atime. */ 1271228753Smm#endif 1272228753Smm#endif 1273228753Smm#endif 1274228753Smm if (recent) { 1275228753Smm /* Check that requested time is up-to-date. */ 1276228753Smm time_t now = time(NULL); 1277228753Smm if (filet < now - 10 || filet > now + 1) { 1278228753Smm failure_start(file, line, 1279232153Smm "File %s has %ctime %lld, %lld seconds ago\n", 1280228753Smm pathname, type, filet, now - filet); 1281228753Smm failure_finish(NULL); 1282228753Smm return (0); 1283228753Smm } 1284228753Smm } else if (filet != t || filet_nsec != nsec) { 1285228753Smm failure_start(file, line, 1286232153Smm "File %s has %ctime %lld.%09lld, expected %lld.%09lld", 1287228753Smm pathname, type, filet, filet_nsec, t, nsec); 1288228753Smm failure_finish(NULL); 1289228753Smm return (0); 1290228753Smm } 1291228753Smm return (1); 1292228753Smm} 1293228753Smm 1294228753Smm/* Verify atime of 'pathname'. */ 1295228753Smmint 1296228753Smmassertion_file_atime(const char *file, int line, 1297228753Smm const char *pathname, long t, long nsec) 1298228753Smm{ 1299228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'a', 0); 1300228753Smm} 1301228753Smm 1302228753Smm/* Verify atime of 'pathname' is up-to-date. */ 1303228753Smmint 1304228753Smmassertion_file_atime_recent(const char *file, int line, const char *pathname) 1305228753Smm{ 1306228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'a', 1); 1307228753Smm} 1308228753Smm 1309228753Smm/* Verify birthtime of 'pathname'. */ 1310228753Smmint 1311228753Smmassertion_file_birthtime(const char *file, int line, 1312228753Smm const char *pathname, long t, long nsec) 1313228753Smm{ 1314228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'b', 0); 1315228753Smm} 1316228753Smm 1317228753Smm/* Verify birthtime of 'pathname' is up-to-date. */ 1318228753Smmint 1319228753Smmassertion_file_birthtime_recent(const char *file, int line, 1320228753Smm const char *pathname) 1321228753Smm{ 1322228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'b', 1); 1323228753Smm} 1324228753Smm 1325228753Smm/* Verify mtime of 'pathname'. */ 1326228753Smmint 1327228753Smmassertion_file_mtime(const char *file, int line, 1328228753Smm const char *pathname, long t, long nsec) 1329228753Smm{ 1330228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'm', 0); 1331228753Smm} 1332228753Smm 1333228753Smm/* Verify mtime of 'pathname' is up-to-date. */ 1334228753Smmint 1335228753Smmassertion_file_mtime_recent(const char *file, int line, const char *pathname) 1336228753Smm{ 1337228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'm', 1); 1338228753Smm} 1339228753Smm 1340228753Smm/* Verify number of links to 'pathname'. */ 1341228753Smmint 1342228753Smmassertion_file_nlinks(const char *file, int line, 1343228753Smm const char *pathname, int nlinks) 1344228753Smm{ 1345228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1346228753Smm BY_HANDLE_FILE_INFORMATION bhfi; 1347228753Smm int r; 1348228753Smm 1349228753Smm assertion_count(file, line); 1350228753Smm r = my_GetFileInformationByName(pathname, &bhfi); 1351228753Smm if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks) 1352228753Smm return (1); 1353228753Smm failure_start(file, line, "File %s has %d links, expected %d", 1354228753Smm pathname, bhfi.nNumberOfLinks, nlinks); 1355228753Smm failure_finish(NULL); 1356228753Smm return (0); 1357228753Smm#else 1358228753Smm struct stat st; 1359228753Smm int r; 1360228753Smm 1361228753Smm assertion_count(file, line); 1362228753Smm r = lstat(pathname, &st); 1363232153Smm if (r == 0 && (int)st.st_nlink == nlinks) 1364228753Smm return (1); 1365228753Smm failure_start(file, line, "File %s has %d links, expected %d", 1366228753Smm pathname, st.st_nlink, nlinks); 1367228753Smm failure_finish(NULL); 1368228753Smm return (0); 1369228753Smm#endif 1370228753Smm} 1371228753Smm 1372228753Smm/* Verify size of 'pathname'. */ 1373228753Smmint 1374228753Smmassertion_file_size(const char *file, int line, const char *pathname, long size) 1375228753Smm{ 1376228753Smm int64_t filesize; 1377228753Smm int r; 1378228753Smm 1379228753Smm assertion_count(file, line); 1380228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1381228753Smm { 1382228753Smm BY_HANDLE_FILE_INFORMATION bhfi; 1383228753Smm r = !my_GetFileInformationByName(pathname, &bhfi); 1384228753Smm filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow; 1385228753Smm } 1386228753Smm#else 1387228753Smm { 1388228753Smm struct stat st; 1389228753Smm r = lstat(pathname, &st); 1390228753Smm filesize = st.st_size; 1391228753Smm } 1392228753Smm#endif 1393228753Smm if (r == 0 && filesize == size) 1394228753Smm return (1); 1395228753Smm failure_start(file, line, "File %s has size %ld, expected %ld", 1396228753Smm pathname, (long)filesize, (long)size); 1397228753Smm failure_finish(NULL); 1398228753Smm return (0); 1399228753Smm} 1400228753Smm 1401228753Smm/* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */ 1402228753Smmint 1403228753Smmassertion_is_dir(const char *file, int line, const char *pathname, int mode) 1404228753Smm{ 1405228753Smm struct stat st; 1406228753Smm int r; 1407228753Smm 1408228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1409228753Smm (void)mode; /* UNUSED */ 1410228753Smm#endif 1411228753Smm assertion_count(file, line); 1412228753Smm r = lstat(pathname, &st); 1413228753Smm if (r != 0) { 1414228753Smm failure_start(file, line, "Dir should exist: %s", pathname); 1415228753Smm failure_finish(NULL); 1416228753Smm return (0); 1417228753Smm } 1418228753Smm if (!S_ISDIR(st.st_mode)) { 1419228753Smm failure_start(file, line, "%s is not a dir", pathname); 1420228753Smm failure_finish(NULL); 1421228753Smm return (0); 1422228753Smm } 1423228753Smm#if !defined(_WIN32) || defined(__CYGWIN__) 1424228753Smm /* Windows doesn't handle permissions the same way as POSIX, 1425228753Smm * so just ignore the mode tests. */ 1426228753Smm /* TODO: Can we do better here? */ 1427232153Smm if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1428228753Smm failure_start(file, line, "Dir %s has wrong mode", pathname); 1429228753Smm logprintf(" Expected: 0%3o\n", mode); 1430228753Smm logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1431228753Smm failure_finish(NULL); 1432228753Smm return (0); 1433228753Smm } 1434228753Smm#endif 1435228753Smm return (1); 1436228753Smm} 1437228753Smm 1438228753Smm/* Verify that 'pathname' is a regular file. If 'mode' is >= 0, 1439228753Smm * verify that too. */ 1440228753Smmint 1441228753Smmassertion_is_reg(const char *file, int line, const char *pathname, int mode) 1442228753Smm{ 1443228753Smm struct stat st; 1444228753Smm int r; 1445228753Smm 1446228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1447228753Smm (void)mode; /* UNUSED */ 1448228753Smm#endif 1449228753Smm assertion_count(file, line); 1450228753Smm r = lstat(pathname, &st); 1451228753Smm if (r != 0 || !S_ISREG(st.st_mode)) { 1452228753Smm failure_start(file, line, "File should exist: %s", pathname); 1453228753Smm failure_finish(NULL); 1454228753Smm return (0); 1455228753Smm } 1456228753Smm#if !defined(_WIN32) || defined(__CYGWIN__) 1457228753Smm /* Windows doesn't handle permissions the same way as POSIX, 1458228753Smm * so just ignore the mode tests. */ 1459228753Smm /* TODO: Can we do better here? */ 1460232153Smm if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1461228753Smm failure_start(file, line, "File %s has wrong mode", pathname); 1462228753Smm logprintf(" Expected: 0%3o\n", mode); 1463228753Smm logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1464228753Smm failure_finish(NULL); 1465228753Smm return (0); 1466228753Smm } 1467228753Smm#endif 1468228753Smm return (1); 1469228753Smm} 1470228753Smm 1471228753Smm/* Check whether 'pathname' is a symbolic link. If 'contents' is 1472228753Smm * non-NULL, verify that the symlink has those contents. */ 1473228753Smmstatic int 1474228753Smmis_symlink(const char *file, int line, 1475228753Smm const char *pathname, const char *contents) 1476228753Smm{ 1477228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1478228753Smm (void)pathname; /* UNUSED */ 1479228753Smm (void)contents; /* UNUSED */ 1480228753Smm assertion_count(file, line); 1481228753Smm /* Windows sort-of has real symlinks, but they're only usable 1482228753Smm * by privileged users and are crippled even then, so there's 1483228753Smm * really not much point in bothering with this. */ 1484228753Smm return (0); 1485228753Smm#else 1486228753Smm char buff[300]; 1487228753Smm struct stat st; 1488228753Smm ssize_t linklen; 1489228753Smm int r; 1490228753Smm 1491228753Smm assertion_count(file, line); 1492228753Smm r = lstat(pathname, &st); 1493228753Smm if (r != 0) { 1494228753Smm failure_start(file, line, 1495228753Smm "Symlink should exist: %s", pathname); 1496228753Smm failure_finish(NULL); 1497228753Smm return (0); 1498228753Smm } 1499228753Smm if (!S_ISLNK(st.st_mode)) 1500228753Smm return (0); 1501228753Smm if (contents == NULL) 1502228753Smm return (1); 1503228753Smm linklen = readlink(pathname, buff, sizeof(buff)); 1504228753Smm if (linklen < 0) { 1505228753Smm failure_start(file, line, "Can't read symlink %s", pathname); 1506228753Smm failure_finish(NULL); 1507228753Smm return (0); 1508228753Smm } 1509228753Smm buff[linklen] = '\0'; 1510228753Smm if (strcmp(buff, contents) != 0) 1511228753Smm return (0); 1512228753Smm return (1); 1513228753Smm#endif 1514228753Smm} 1515228753Smm 1516228753Smm/* Assert that path is a symlink that (optionally) contains contents. */ 1517228753Smmint 1518228753Smmassertion_is_symlink(const char *file, int line, 1519228753Smm const char *path, const char *contents) 1520228753Smm{ 1521228753Smm if (is_symlink(file, line, path, contents)) 1522228753Smm return (1); 1523228753Smm if (contents) 1524228753Smm failure_start(file, line, "File %s is not a symlink to %s", 1525228753Smm path, contents); 1526228753Smm else 1527228753Smm failure_start(file, line, "File %s is not a symlink", path); 1528228753Smm failure_finish(NULL); 1529228753Smm return (0); 1530228753Smm} 1531228753Smm 1532228753Smm 1533228753Smm/* Create a directory and report any errors. */ 1534228753Smmint 1535228753Smmassertion_make_dir(const char *file, int line, const char *dirname, int mode) 1536228753Smm{ 1537228753Smm assertion_count(file, line); 1538228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1539228753Smm (void)mode; /* UNUSED */ 1540228753Smm if (0 == _mkdir(dirname)) 1541228753Smm return (1); 1542228753Smm#else 1543228753Smm if (0 == mkdir(dirname, mode)) 1544228753Smm return (1); 1545228753Smm#endif 1546228753Smm failure_start(file, line, "Could not create directory %s", dirname); 1547228753Smm failure_finish(NULL); 1548228753Smm return(0); 1549228753Smm} 1550228753Smm 1551228753Smm/* Create a file with the specified contents and report any failures. */ 1552228753Smmint 1553228753Smmassertion_make_file(const char *file, int line, 1554238856Smm const char *path, int mode, int csize, const void *contents) 1555228753Smm{ 1556228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1557228753Smm /* TODO: Rework this to set file mode as well. */ 1558228753Smm FILE *f; 1559228753Smm (void)mode; /* UNUSED */ 1560228753Smm assertion_count(file, line); 1561228753Smm f = fopen(path, "wb"); 1562228753Smm if (f == NULL) { 1563228753Smm failure_start(file, line, "Could not create file %s", path); 1564228753Smm failure_finish(NULL); 1565228753Smm return (0); 1566228753Smm } 1567228753Smm if (contents != NULL) { 1568238856Smm size_t wsize; 1569238856Smm 1570238856Smm if (csize < 0) 1571238856Smm wsize = strlen(contents); 1572238856Smm else 1573238856Smm wsize = (size_t)csize; 1574238856Smm if (wsize != fwrite(contents, 1, wsize, f)) { 1575228753Smm fclose(f); 1576228753Smm failure_start(file, line, 1577228753Smm "Could not write file %s", path); 1578228753Smm failure_finish(NULL); 1579228753Smm return (0); 1580228753Smm } 1581228753Smm } 1582228753Smm fclose(f); 1583228753Smm return (1); 1584228753Smm#else 1585228753Smm int fd; 1586228753Smm assertion_count(file, line); 1587228753Smm fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644); 1588228753Smm if (fd < 0) { 1589228753Smm failure_start(file, line, "Could not create %s", path); 1590228753Smm failure_finish(NULL); 1591228753Smm return (0); 1592228753Smm } 1593228753Smm if (contents != NULL) { 1594238856Smm ssize_t wsize; 1595238856Smm 1596238856Smm if (csize < 0) 1597238856Smm wsize = (ssize_t)strlen(contents); 1598238856Smm else 1599238856Smm wsize = (ssize_t)csize; 1600238856Smm if (wsize != write(fd, contents, wsize)) { 1601228753Smm close(fd); 1602238856Smm failure_start(file, line, 1603238856Smm "Could not write to %s", path); 1604228753Smm failure_finish(NULL); 1605228753Smm return (0); 1606228753Smm } 1607228753Smm } 1608228753Smm close(fd); 1609228753Smm return (1); 1610228753Smm#endif 1611228753Smm} 1612228753Smm 1613228753Smm/* Create a hardlink and report any failures. */ 1614228753Smmint 1615228753Smmassertion_make_hardlink(const char *file, int line, 1616228753Smm const char *newpath, const char *linkto) 1617228753Smm{ 1618228753Smm int succeeded; 1619228753Smm 1620228753Smm assertion_count(file, line); 1621228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1622228753Smm succeeded = my_CreateHardLinkA(newpath, linkto); 1623228753Smm#elif HAVE_LINK 1624228753Smm succeeded = !link(linkto, newpath); 1625228753Smm#else 1626228753Smm succeeded = 0; 1627228753Smm#endif 1628228753Smm if (succeeded) 1629228753Smm return (1); 1630228753Smm failure_start(file, line, "Could not create hardlink"); 1631228753Smm logprintf(" New link: %s\n", newpath); 1632228753Smm logprintf(" Old name: %s\n", linkto); 1633228753Smm failure_finish(NULL); 1634228753Smm return(0); 1635228753Smm} 1636228753Smm 1637228753Smm/* Create a symlink and report any failures. */ 1638228753Smmint 1639228753Smmassertion_make_symlink(const char *file, int line, 1640228753Smm const char *newpath, const char *linkto) 1641228753Smm{ 1642228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1643228753Smm int targetIsDir = 0; /* TODO: Fix this */ 1644228753Smm assertion_count(file, line); 1645228753Smm if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir)) 1646228753Smm return (1); 1647228753Smm#elif HAVE_SYMLINK 1648228753Smm assertion_count(file, line); 1649228753Smm if (0 == symlink(linkto, newpath)) 1650228753Smm return (1); 1651228753Smm#endif 1652228753Smm failure_start(file, line, "Could not create symlink"); 1653228753Smm logprintf(" New link: %s\n", newpath); 1654228753Smm logprintf(" Old name: %s\n", linkto); 1655228753Smm failure_finish(NULL); 1656228753Smm return(0); 1657228753Smm} 1658228753Smm 1659228753Smm/* Set umask, report failures. */ 1660228753Smmint 1661228753Smmassertion_umask(const char *file, int line, int mask) 1662228753Smm{ 1663228753Smm assertion_count(file, line); 1664228753Smm (void)file; /* UNUSED */ 1665228753Smm (void)line; /* UNUSED */ 1666228753Smm umask(mask); 1667228753Smm return (1); 1668228753Smm} 1669228753Smm 1670232153Smm/* Set times, report failures. */ 1671232153Smmint 1672232153Smmassertion_utimes(const char *file, int line, 1673232153Smm const char *pathname, long at, long at_nsec, long mt, long mt_nsec) 1674232153Smm{ 1675232153Smm int r; 1676232153Smm 1677232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1678232153Smm#define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\ 1679232153Smm + (((nsec)/1000)*10)) 1680232153Smm HANDLE h; 1681232153Smm ULARGE_INTEGER wintm; 1682232153Smm FILETIME fatime, fmtime; 1683232153Smm FILETIME *pat, *pmt; 1684232153Smm 1685232153Smm assertion_count(file, line); 1686232153Smm h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE, 1687232153Smm FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 1688232153Smm FILE_FLAG_BACKUP_SEMANTICS, NULL); 1689232153Smm if (h == INVALID_HANDLE_VALUE) { 1690232153Smm failure_start(file, line, "Can't access %s\n", pathname); 1691232153Smm failure_finish(NULL); 1692232153Smm return (0); 1693232153Smm } 1694232153Smm 1695232153Smm if (at > 0 || at_nsec > 0) { 1696232153Smm wintm.QuadPart = WINTIME(at, at_nsec); 1697232153Smm fatime.dwLowDateTime = wintm.LowPart; 1698232153Smm fatime.dwHighDateTime = wintm.HighPart; 1699232153Smm pat = &fatime; 1700232153Smm } else 1701232153Smm pat = NULL; 1702232153Smm if (mt > 0 || mt_nsec > 0) { 1703232153Smm wintm.QuadPart = WINTIME(mt, mt_nsec); 1704232153Smm fmtime.dwLowDateTime = wintm.LowPart; 1705232153Smm fmtime.dwHighDateTime = wintm.HighPart; 1706232153Smm pmt = &fmtime; 1707232153Smm } else 1708232153Smm pmt = NULL; 1709232153Smm if (pat != NULL || pmt != NULL) 1710232153Smm r = SetFileTime(h, NULL, pat, pmt); 1711232153Smm else 1712232153Smm r = 1; 1713232153Smm CloseHandle(h); 1714232153Smm if (r == 0) { 1715232153Smm failure_start(file, line, "Can't SetFileTime %s\n", pathname); 1716232153Smm failure_finish(NULL); 1717232153Smm return (0); 1718232153Smm } 1719232153Smm return (1); 1720232153Smm#else /* defined(_WIN32) && !defined(__CYGWIN__) */ 1721232153Smm struct stat st; 1722232153Smm struct timeval times[2]; 1723232153Smm 1724232153Smm#if !defined(__FreeBSD__) 1725232153Smm mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */ 1726232153Smm#endif 1727232153Smm if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0) 1728232153Smm return (1); 1729232153Smm 1730232153Smm r = lstat(pathname, &st); 1731232153Smm if (r < 0) { 1732232153Smm failure_start(file, line, "Can't stat %s\n", pathname); 1733232153Smm failure_finish(NULL); 1734232153Smm return (0); 1735232153Smm } 1736232153Smm 1737232153Smm if (mt == 0 && mt_nsec == 0) { 1738232153Smm mt = st.st_mtime; 1739232153Smm#if defined(__FreeBSD__) 1740232153Smm mt_nsec = st.st_mtimespec.tv_nsec; 1741232153Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1742232153Smm mt_nsec = (mt_nsec / 1000) * 1000; 1743232153Smm#endif 1744232153Smm } 1745232153Smm if (at == 0 && at_nsec == 0) { 1746232153Smm at = st.st_atime; 1747232153Smm#if defined(__FreeBSD__) 1748232153Smm at_nsec = st.st_atimespec.tv_nsec; 1749232153Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1750232153Smm at_nsec = (at_nsec / 1000) * 1000; 1751232153Smm#endif 1752232153Smm } 1753232153Smm 1754232153Smm times[1].tv_sec = mt; 1755232153Smm times[1].tv_usec = mt_nsec / 1000; 1756232153Smm 1757232153Smm times[0].tv_sec = at; 1758232153Smm times[0].tv_usec = at_nsec / 1000; 1759232153Smm 1760232153Smm#ifdef HAVE_LUTIMES 1761232153Smm r = lutimes(pathname, times); 1762232153Smm#else 1763232153Smm r = utimes(pathname, times); 1764232153Smm#endif 1765232153Smm if (r < 0) { 1766232153Smm failure_start(file, line, "Can't utimes %s\n", pathname); 1767232153Smm failure_finish(NULL); 1768232153Smm return (0); 1769232153Smm } 1770232153Smm return (1); 1771232153Smm#endif /* defined(_WIN32) && !defined(__CYGWIN__) */ 1772232153Smm} 1773232153Smm 1774238856Smm/* Set nodump, report failures. */ 1775238856Smmint 1776238856Smmassertion_nodump(const char *file, int line, const char *pathname) 1777238856Smm{ 1778238856Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 1779238856Smm int r; 1780238856Smm 1781238856Smm assertion_count(file, line); 1782238856Smm r = chflags(pathname, UF_NODUMP); 1783238856Smm if (r < 0) { 1784238856Smm failure_start(file, line, "Can't set nodump %s\n", pathname); 1785238856Smm failure_finish(NULL); 1786238856Smm return (0); 1787238856Smm } 1788238856Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\ 1789238856Smm && defined(EXT2_NODUMP_FL) 1790238856Smm int fd, r, flags; 1791238856Smm 1792238856Smm assertion_count(file, line); 1793238856Smm fd = open(pathname, O_RDONLY | O_NONBLOCK); 1794238856Smm if (fd < 0) { 1795238856Smm failure_start(file, line, "Can't open %s\n", pathname); 1796238856Smm failure_finish(NULL); 1797238856Smm return (0); 1798238856Smm } 1799238856Smm r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags); 1800238856Smm if (r < 0) { 1801238856Smm failure_start(file, line, "Can't get flags %s\n", pathname); 1802238856Smm failure_finish(NULL); 1803238856Smm return (0); 1804238856Smm } 1805238856Smm flags |= EXT2_NODUMP_FL; 1806238856Smm r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags); 1807238856Smm if (r < 0) { 1808238856Smm failure_start(file, line, "Can't set nodump %s\n", pathname); 1809238856Smm failure_finish(NULL); 1810238856Smm return (0); 1811238856Smm } 1812238856Smm close(fd); 1813238856Smm#else 1814238856Smm (void)pathname; /* UNUSED */ 1815238856Smm assertion_count(file, line); 1816238856Smm#endif 1817238856Smm return (1); 1818238856Smm} 1819238856Smm 1820228753Smm/* 1821228753Smm * 1822228753Smm * UTILITIES for use by tests. 1823228753Smm * 1824228753Smm */ 1825228753Smm 1826228753Smm/* 1827228753Smm * Check whether platform supports symlinks. This is intended 1828228753Smm * for tests to use in deciding whether to bother testing symlink 1829228753Smm * support; if the platform doesn't support symlinks, there's no point 1830228753Smm * in checking whether the program being tested can create them. 1831228753Smm * 1832228753Smm * Note that the first time this test is called, we actually go out to 1833228753Smm * disk to create and verify a symlink. This is necessary because 1834228753Smm * symlink support is actually a property of a particular filesystem 1835228753Smm * and can thus vary between directories on a single system. After 1836228753Smm * the first call, this returns the cached result from memory, so it's 1837228753Smm * safe to call it as often as you wish. 1838228753Smm */ 1839228753Smmint 1840228753SmmcanSymlink(void) 1841228753Smm{ 1842228753Smm /* Remember the test result */ 1843228753Smm static int value = 0, tested = 0; 1844228753Smm if (tested) 1845228753Smm return (value); 1846228753Smm 1847228753Smm ++tested; 1848238856Smm assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a"); 1849228753Smm /* Note: Cygwin has its own symlink() emulation that does not 1850228753Smm * use the Win32 CreateSymbolicLink() function. */ 1851228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1852228753Smm value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0) 1853228753Smm && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0"); 1854228753Smm#elif HAVE_SYMLINK 1855228753Smm value = (0 == symlink("canSymlink.0", "canSymlink.1")) 1856228753Smm && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0"); 1857228753Smm#endif 1858228753Smm return (value); 1859228753Smm} 1860228753Smm 1861228753Smm/* Platform-dependent options for hiding the output of a subcommand. */ 1862228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1863228753Smmstatic const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */ 1864228753Smm#else 1865228753Smmstatic const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */ 1866228753Smm#endif 1867248616Smm/* 1868248616Smm * Can this platform run the bzip2 program? 1869248616Smm */ 1870228753Smmint 1871248616SmmcanBzip2(void) 1872248616Smm{ 1873248616Smm static int tested = 0, value = 0; 1874248616Smm if (!tested) { 1875248616Smm tested = 1; 1876248616Smm if (systemf("bzip2 -d -V %s", redirectArgs) == 0) 1877248616Smm value = 1; 1878248616Smm } 1879248616Smm return (value); 1880248616Smm} 1881248616Smm 1882248616Smm/* 1883248616Smm * Can this platform run the grzip program? 1884248616Smm */ 1885248616Smmint 1886248616SmmcanGrzip(void) 1887248616Smm{ 1888248616Smm static int tested = 0, value = 0; 1889248616Smm if (!tested) { 1890248616Smm tested = 1; 1891248616Smm if (systemf("grzip -V %s", redirectArgs) == 0) 1892248616Smm value = 1; 1893248616Smm } 1894248616Smm return (value); 1895248616Smm} 1896248616Smm 1897248616Smm/* 1898248616Smm * Can this platform run the gzip program? 1899248616Smm */ 1900248616Smmint 1901228753SmmcanGzip(void) 1902228753Smm{ 1903228753Smm static int tested = 0, value = 0; 1904228753Smm if (!tested) { 1905228753Smm tested = 1; 1906228753Smm if (systemf("gzip -V %s", redirectArgs) == 0) 1907228753Smm value = 1; 1908228753Smm } 1909228753Smm return (value); 1910228753Smm} 1911228753Smm 1912228753Smm/* 1913248616Smm * Can this platform run the lrzip program? 1914228753Smm */ 1915228753Smmint 1916248616SmmcanLrzip(void) 1917228753Smm{ 1918228753Smm static int tested = 0, value = 0; 1919228753Smm if (!tested) { 1920228753Smm tested = 1; 1921248616Smm if (systemf("lrzip -V %s", redirectArgs) == 0) 1922228753Smm value = 1; 1923228753Smm } 1924228753Smm return (value); 1925228753Smm} 1926228753Smm 1927228753Smm/* 1928248616Smm * Can this platform run the lzip program? 1929248616Smm */ 1930248616Smmint 1931248616SmmcanLzip(void) 1932248616Smm{ 1933248616Smm static int tested = 0, value = 0; 1934248616Smm if (!tested) { 1935248616Smm tested = 1; 1936248616Smm if (systemf("lzip -V %s", redirectArgs) == 0) 1937248616Smm value = 1; 1938248616Smm } 1939248616Smm return (value); 1940248616Smm} 1941248616Smm 1942248616Smm/* 1943248616Smm * Can this platform run the lzma program? 1944248616Smm */ 1945248616Smmint 1946248616SmmcanLzma(void) 1947248616Smm{ 1948248616Smm static int tested = 0, value = 0; 1949248616Smm if (!tested) { 1950248616Smm tested = 1; 1951248616Smm if (systemf("lzma -V %s", redirectArgs) == 0) 1952248616Smm value = 1; 1953248616Smm } 1954248616Smm return (value); 1955248616Smm} 1956248616Smm 1957248616Smm/* 1958248616Smm * Can this platform run the lzop program? 1959248616Smm */ 1960248616Smmint 1961248616SmmcanLzop(void) 1962248616Smm{ 1963248616Smm static int tested = 0, value = 0; 1964248616Smm if (!tested) { 1965248616Smm tested = 1; 1966248616Smm if (systemf("lzop -V %s", redirectArgs) == 0) 1967248616Smm value = 1; 1968248616Smm } 1969248616Smm return (value); 1970248616Smm} 1971248616Smm 1972248616Smm/* 1973248616Smm * Can this platform run the xz program? 1974248616Smm */ 1975248616Smmint 1976248616SmmcanXz(void) 1977248616Smm{ 1978248616Smm static int tested = 0, value = 0; 1979248616Smm if (!tested) { 1980248616Smm tested = 1; 1981248616Smm if (systemf("xz -V %s", redirectArgs) == 0) 1982248616Smm value = 1; 1983248616Smm } 1984248616Smm return (value); 1985248616Smm} 1986248616Smm 1987248616Smm/* 1988238856Smm * Can this filesystem handle nodump flags. 1989238856Smm */ 1990238856Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 1991238856Smm 1992238856Smmint 1993238856SmmcanNodump(void) 1994238856Smm{ 1995238856Smm const char *path = "cannodumptest"; 1996238856Smm struct stat sb; 1997238856Smm 1998238856Smm assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 1999238856Smm if (chflags(path, UF_NODUMP) < 0) 2000238856Smm return (0); 2001238856Smm if (stat(path, &sb) < 0) 2002238856Smm return (0); 2003238856Smm if (sb.st_flags & UF_NODUMP) 2004238856Smm return (1); 2005238856Smm return (0); 2006238856Smm} 2007238856Smm 2008238856Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\ 2009238856Smm && defined(EXT2_NODUMP_FL) 2010238856Smm 2011238856Smmint 2012238856SmmcanNodump(void) 2013238856Smm{ 2014238856Smm const char *path = "cannodumptest"; 2015238856Smm int fd, r, flags; 2016238856Smm 2017238856Smm assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2018238856Smm fd = open(path, O_RDONLY | O_NONBLOCK); 2019238856Smm if (fd < 0) 2020238856Smm return (0); 2021238856Smm r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags); 2022238856Smm if (r < 0) 2023238856Smm return (0); 2024238856Smm flags |= EXT2_NODUMP_FL; 2025238856Smm r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags); 2026238856Smm if (r < 0) 2027238856Smm return (0); 2028238856Smm close(fd); 2029238856Smm fd = open(path, O_RDONLY | O_NONBLOCK); 2030238856Smm if (fd < 0) 2031238856Smm return (0); 2032238856Smm r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags); 2033238856Smm if (r < 0) 2034238856Smm return (0); 2035238856Smm close(fd); 2036238856Smm if (flags & EXT2_NODUMP_FL) 2037238856Smm return (1); 2038238856Smm return (0); 2039238856Smm} 2040238856Smm 2041238856Smm#else 2042238856Smm 2043238856Smmint 2044238856SmmcanNodump() 2045238856Smm{ 2046238856Smm return (0); 2047238856Smm} 2048238856Smm 2049238856Smm#endif 2050238856Smm 2051238856Smm/* 2052228753Smm * Sleep as needed; useful for verifying disk timestamp changes by 2053228753Smm * ensuring that the wall-clock time has actually changed before we 2054228753Smm * go back to re-read something from disk. 2055228753Smm */ 2056228753Smmvoid 2057228753SmmsleepUntilAfter(time_t t) 2058228753Smm{ 2059228753Smm while (t >= time(NULL)) 2060228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2061228753Smm Sleep(500); 2062228753Smm#else 2063228753Smm sleep(1); 2064228753Smm#endif 2065228753Smm} 2066228753Smm 2067228753Smm/* 2068228753Smm * Call standard system() call, but build up the command line using 2069228753Smm * sprintf() conventions. 2070228753Smm */ 2071228753Smmint 2072228753Smmsystemf(const char *fmt, ...) 2073228753Smm{ 2074228753Smm char buff[8192]; 2075228753Smm va_list ap; 2076228753Smm int r; 2077228753Smm 2078228753Smm va_start(ap, fmt); 2079228753Smm vsprintf(buff, fmt, ap); 2080228753Smm if (verbosity > VERBOSITY_FULL) 2081228753Smm logprintf("Cmd: %s\n", buff); 2082228753Smm r = system(buff); 2083228753Smm va_end(ap); 2084228753Smm return (r); 2085228753Smm} 2086228753Smm 2087228753Smm/* 2088228753Smm * Slurp a file into memory for ease of comparison and testing. 2089228753Smm * Returns size of file in 'sizep' if non-NULL, null-terminates 2090228753Smm * data in memory for ease of use. 2091228753Smm */ 2092228753Smmchar * 2093228753Smmslurpfile(size_t * sizep, const char *fmt, ...) 2094228753Smm{ 2095228753Smm char filename[8192]; 2096228753Smm struct stat st; 2097228753Smm va_list ap; 2098228753Smm char *p; 2099228753Smm ssize_t bytes_read; 2100228753Smm FILE *f; 2101228753Smm int r; 2102228753Smm 2103228753Smm va_start(ap, fmt); 2104228753Smm vsprintf(filename, fmt, ap); 2105228753Smm va_end(ap); 2106228753Smm 2107228753Smm f = fopen(filename, "rb"); 2108228753Smm if (f == NULL) { 2109228753Smm /* Note: No error; non-existent file is okay here. */ 2110228753Smm return (NULL); 2111228753Smm } 2112228753Smm r = fstat(fileno(f), &st); 2113228753Smm if (r != 0) { 2114228753Smm logprintf("Can't stat file %s\n", filename); 2115228753Smm fclose(f); 2116228753Smm return (NULL); 2117228753Smm } 2118228753Smm p = malloc((size_t)st.st_size + 1); 2119228753Smm if (p == NULL) { 2120228753Smm logprintf("Can't allocate %ld bytes of memory to read file %s\n", 2121228753Smm (long int)st.st_size, filename); 2122228753Smm fclose(f); 2123228753Smm return (NULL); 2124228753Smm } 2125228753Smm bytes_read = fread(p, 1, (size_t)st.st_size, f); 2126228753Smm if (bytes_read < st.st_size) { 2127228753Smm logprintf("Can't read file %s\n", filename); 2128228753Smm fclose(f); 2129228753Smm free(p); 2130228753Smm return (NULL); 2131228753Smm } 2132228753Smm p[st.st_size] = '\0'; 2133228753Smm if (sizep != NULL) 2134228753Smm *sizep = (size_t)st.st_size; 2135228753Smm fclose(f); 2136228753Smm return (p); 2137228753Smm} 2138228753Smm 2139228753Smm/* Read a uuencoded file from the reference directory, decode, and 2140228753Smm * write the result into the current directory. */ 2141228753Smm#define UUDECODE(c) (((c) - 0x20) & 0x3f) 2142228753Smmvoid 2143228753Smmextract_reference_file(const char *name) 2144228753Smm{ 2145228753Smm char buff[1024]; 2146228753Smm FILE *in, *out; 2147228753Smm 2148228753Smm sprintf(buff, "%s/%s.uu", refdir, name); 2149228753Smm in = fopen(buff, "r"); 2150228753Smm failure("Couldn't open reference file %s", buff); 2151228753Smm assert(in != NULL); 2152228753Smm if (in == NULL) 2153228753Smm return; 2154228753Smm /* Read up to and including the 'begin' line. */ 2155228753Smm for (;;) { 2156228753Smm if (fgets(buff, sizeof(buff), in) == NULL) { 2157228753Smm /* TODO: This is a failure. */ 2158228753Smm return; 2159228753Smm } 2160228753Smm if (memcmp(buff, "begin ", 6) == 0) 2161228753Smm break; 2162228753Smm } 2163228753Smm /* Now, decode the rest and write it. */ 2164228753Smm /* Not a lot of error checking here; the input better be right. */ 2165228753Smm out = fopen(name, "wb"); 2166228753Smm while (fgets(buff, sizeof(buff), in) != NULL) { 2167228753Smm char *p = buff; 2168228753Smm int bytes; 2169228753Smm 2170228753Smm if (memcmp(buff, "end", 3) == 0) 2171228753Smm break; 2172228753Smm 2173228753Smm bytes = UUDECODE(*p++); 2174228753Smm while (bytes > 0) { 2175228753Smm int n = 0; 2176228753Smm /* Write out 1-3 bytes from that. */ 2177228753Smm if (bytes > 0) { 2178228753Smm n = UUDECODE(*p++) << 18; 2179228753Smm n |= UUDECODE(*p++) << 12; 2180228753Smm fputc(n >> 16, out); 2181228753Smm --bytes; 2182228753Smm } 2183228753Smm if (bytes > 0) { 2184228753Smm n |= UUDECODE(*p++) << 6; 2185228753Smm fputc((n >> 8) & 0xFF, out); 2186228753Smm --bytes; 2187228753Smm } 2188228753Smm if (bytes > 0) { 2189228753Smm n |= UUDECODE(*p++); 2190228753Smm fputc(n & 0xFF, out); 2191228753Smm --bytes; 2192228753Smm } 2193228753Smm } 2194228753Smm } 2195228753Smm fclose(out); 2196228753Smm fclose(in); 2197228753Smm} 2198228753Smm 2199232153Smmint 2200232153Smmis_LargeInode(const char *file) 2201232153Smm{ 2202232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2203232153Smm BY_HANDLE_FILE_INFORMATION bhfi; 2204232153Smm int r; 2205232153Smm 2206232153Smm r = my_GetFileInformationByName(file, &bhfi); 2207232153Smm if (r != 0) 2208232153Smm return (0); 2209232153Smm return (bhfi.nFileIndexHigh & 0x0000FFFFUL); 2210232153Smm#else 2211232153Smm struct stat st; 2212232153Smm int64_t ino; 2213232153Smm 2214232153Smm if (stat(file, &st) < 0) 2215232153Smm return (0); 2216232153Smm ino = (int64_t)st.st_ino; 2217232153Smm return (ino > 0xffffffff); 2218232153Smm#endif 2219232153Smm} 2220228753Smm/* 2221228753Smm * 2222228753Smm * TEST management 2223228753Smm * 2224228753Smm */ 2225228753Smm 2226228753Smm/* 2227228753Smm * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has 2228228753Smm * a line like 2229228753Smm * DEFINE_TEST(test_function) 2230228753Smm * for each test. 2231228753Smm */ 2232228753Smm 2233228753Smm/* Use "list.h" to declare all of the test functions. */ 2234228753Smm#undef DEFINE_TEST 2235228753Smm#define DEFINE_TEST(name) void name(void); 2236228753Smm#include "list.h" 2237228753Smm 2238228753Smm/* Use "list.h" to create a list of all tests (functions and names). */ 2239228753Smm#undef DEFINE_TEST 2240228753Smm#define DEFINE_TEST(n) { n, #n, 0 }, 2241248616Smmstruct test_list_t tests[] = { 2242228753Smm #include "list.h" 2243228753Smm}; 2244228753Smm 2245228753Smm/* 2246228753Smm * Summarize repeated failures in the just-completed test. 2247228753Smm */ 2248228753Smmstatic void 2249232153Smmtest_summarize(int failed) 2250228753Smm{ 2251228753Smm unsigned int i; 2252228753Smm 2253228753Smm switch (verbosity) { 2254228753Smm case VERBOSITY_SUMMARY_ONLY: 2255228753Smm printf(failed ? "E" : "."); 2256228753Smm fflush(stdout); 2257228753Smm break; 2258228753Smm case VERBOSITY_PASSFAIL: 2259228753Smm printf(failed ? "FAIL\n" : "ok\n"); 2260228753Smm break; 2261228753Smm } 2262228753Smm 2263228753Smm log_console = (verbosity == VERBOSITY_LIGHT_REPORT); 2264228753Smm 2265228753Smm for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { 2266228753Smm if (failed_lines[i].count > 1 && !failed_lines[i].skip) 2267228753Smm logprintf("%s:%d: Summary: Failed %d times\n", 2268232153Smm failed_filename, i, failed_lines[i].count); 2269228753Smm } 2270228753Smm /* Clear the failure history for the next file. */ 2271232153Smm failed_filename = NULL; 2272228753Smm memset(failed_lines, 0, sizeof(failed_lines)); 2273228753Smm} 2274228753Smm 2275228753Smm/* 2276228753Smm * Actually run a single test, with appropriate setup and cleanup. 2277228753Smm */ 2278228753Smmstatic int 2279228753Smmtest_run(int i, const char *tmpdir) 2280228753Smm{ 2281232153Smm char workdir[1024]; 2282228753Smm char logfilename[64]; 2283228753Smm int failures_before = failures; 2284228753Smm int oldumask; 2285228753Smm 2286228753Smm switch (verbosity) { 2287228753Smm case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */ 2288228753Smm break; 2289228753Smm case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */ 2290228753Smm printf("%3d: %-50s", i, tests[i].name); 2291228753Smm fflush(stdout); 2292228753Smm break; 2293228753Smm default: /* Title of test, details will follow */ 2294228753Smm printf("%3d: %s\n", i, tests[i].name); 2295228753Smm } 2296228753Smm 2297228753Smm /* Chdir to the top-level work directory. */ 2298228753Smm if (!assertChdir(tmpdir)) { 2299228753Smm fprintf(stderr, 2300228753Smm "ERROR: Can't chdir to top work dir %s\n", tmpdir); 2301228753Smm exit(1); 2302228753Smm } 2303228753Smm /* Create a log file for this test. */ 2304228753Smm sprintf(logfilename, "%s.log", tests[i].name); 2305228753Smm logfile = fopen(logfilename, "w"); 2306228753Smm fprintf(logfile, "%s\n\n", tests[i].name); 2307228753Smm /* Chdir() to a work dir for this specific test. */ 2308232153Smm snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name); 2309232153Smm testworkdir = workdir; 2310232153Smm if (!assertMakeDir(testworkdir, 0755) 2311232153Smm || !assertChdir(testworkdir)) { 2312228753Smm fprintf(stderr, 2313232153Smm "ERROR: Can't chdir to work dir %s\n", testworkdir); 2314228753Smm exit(1); 2315228753Smm } 2316228753Smm /* Explicitly reset the locale before each test. */ 2317228753Smm setlocale(LC_ALL, "C"); 2318228753Smm /* Record the umask before we run the test. */ 2319228753Smm umask(oldumask = umask(0)); 2320228753Smm /* 2321228753Smm * Run the actual test. 2322228753Smm */ 2323228753Smm (*tests[i].func)(); 2324228753Smm /* 2325228753Smm * Clean up and report afterwards. 2326228753Smm */ 2327232153Smm testworkdir = NULL; 2328228753Smm /* Restore umask */ 2329228753Smm umask(oldumask); 2330228753Smm /* Reset locale. */ 2331228753Smm setlocale(LC_ALL, "C"); 2332228753Smm /* Reset directory. */ 2333228753Smm if (!assertChdir(tmpdir)) { 2334228753Smm fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n", 2335228753Smm tmpdir); 2336228753Smm exit(1); 2337228753Smm } 2338228753Smm /* Report per-test summaries. */ 2339228753Smm tests[i].failures = failures - failures_before; 2340232153Smm test_summarize(tests[i].failures); 2341228753Smm /* Close the per-test log file. */ 2342228753Smm fclose(logfile); 2343228753Smm logfile = NULL; 2344228753Smm /* If there were no failures, we can remove the work dir and logfile. */ 2345228753Smm if (tests[i].failures == 0) { 2346228753Smm if (!keep_temp_files && assertChdir(tmpdir)) { 2347228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2348228753Smm /* Make sure not to leave empty directories. 2349228753Smm * Sometimes a processing of closing files used by tests 2350228753Smm * is not done, then rmdir will be failed and it will 2351228753Smm * leave a empty test directory. So we should wait a few 2352228753Smm * seconds and retry rmdir. */ 2353228753Smm int r, t; 2354228753Smm for (t = 0; t < 10; t++) { 2355228753Smm if (t > 0) 2356228753Smm Sleep(1000); 2357228753Smm r = systemf("rmdir /S /Q %s", tests[i].name); 2358228753Smm if (r == 0) 2359228753Smm break; 2360228753Smm } 2361228753Smm systemf("del %s", logfilename); 2362228753Smm#else 2363228753Smm systemf("rm -rf %s", tests[i].name); 2364228753Smm systemf("rm %s", logfilename); 2365228753Smm#endif 2366228753Smm } 2367228753Smm } 2368228753Smm /* Return appropriate status. */ 2369228753Smm return (tests[i].failures); 2370228753Smm} 2371228753Smm 2372228753Smm/* 2373228753Smm * 2374228753Smm * 2375228753Smm * MAIN and support routines. 2376228753Smm * 2377228753Smm * 2378228753Smm */ 2379228753Smm 2380228753Smmstatic void 2381228753Smmusage(const char *program) 2382228753Smm{ 2383228753Smm static const int limit = sizeof(tests) / sizeof(tests[0]); 2384228753Smm int i; 2385228753Smm 2386228753Smm printf("Usage: %s [options] <test> <test> ...\n", program); 2387228753Smm printf("Default is to run all tests.\n"); 2388228753Smm printf("Otherwise, specify the numbers of the tests you wish to run.\n"); 2389228753Smm printf("Options:\n"); 2390228753Smm printf(" -d Dump core after any failure, for debugging.\n"); 2391228753Smm printf(" -k Keep all temp files.\n"); 2392228753Smm printf(" Default: temp files for successful tests deleted.\n"); 2393228753Smm#ifdef PROGRAM 2394228753Smm printf(" -p <path> Path to executable to be tested.\n"); 2395228753Smm printf(" Default: path taken from " ENVBASE " environment variable.\n"); 2396228753Smm#endif 2397228753Smm printf(" -q Quiet.\n"); 2398228753Smm printf(" -r <dir> Path to dir containing reference files.\n"); 2399228753Smm printf(" Default: Current directory.\n"); 2400232153Smm printf(" -u Keep running specifies tests until one fails.\n"); 2401228753Smm printf(" -v Verbose.\n"); 2402228753Smm printf("Available tests:\n"); 2403228753Smm for (i = 0; i < limit; i++) 2404228753Smm printf(" %d: %s\n", i, tests[i].name); 2405228753Smm exit(1); 2406228753Smm} 2407228753Smm 2408228753Smmstatic char * 2409228753Smmget_refdir(const char *d) 2410228753Smm{ 2411228753Smm char tried[512] = { '\0' }; 2412228753Smm char buff[128]; 2413228753Smm char *pwd, *p; 2414228753Smm 2415228753Smm /* If a dir was specified, try that */ 2416228753Smm if (d != NULL) { 2417228753Smm pwd = NULL; 2418228753Smm snprintf(buff, sizeof(buff), "%s", d); 2419228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2420228753Smm if (p != NULL) goto success; 2421228753Smm strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2422228753Smm strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 2423228753Smm goto failure; 2424228753Smm } 2425228753Smm 2426228753Smm /* Get the current dir. */ 2427232153Smm#ifdef PATH_MAX 2428232153Smm pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 2429232153Smm#else 2430228753Smm pwd = getcwd(NULL, 0); 2431232153Smm#endif 2432228753Smm while (pwd[strlen(pwd) - 1] == '\n') 2433228753Smm pwd[strlen(pwd) - 1] = '\0'; 2434228753Smm 2435228753Smm /* Look for a known file. */ 2436228753Smm snprintf(buff, sizeof(buff), "%s", pwd); 2437228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2438228753Smm if (p != NULL) goto success; 2439228753Smm strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2440228753Smm strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 2441228753Smm 2442228753Smm snprintf(buff, sizeof(buff), "%s/test", pwd); 2443228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2444228753Smm if (p != NULL) goto success; 2445228753Smm strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2446228753Smm strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 2447228753Smm 2448228753Smm#if defined(LIBRARY) 2449228753Smm snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY); 2450228753Smm#else 2451228753Smm snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM); 2452228753Smm#endif 2453228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2454228753Smm if (p != NULL) goto success; 2455228753Smm strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2456228753Smm strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 2457228753Smm 2458232153Smm#if defined(PROGRAM_ALIAS) 2459232153Smm snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS); 2460232153Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2461232153Smm if (p != NULL) goto success; 2462232153Smm strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2463232153Smm strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 2464232153Smm#endif 2465232153Smm 2466228753Smm if (memcmp(pwd, "/usr/obj", 8) == 0) { 2467228753Smm snprintf(buff, sizeof(buff), "%s", pwd + 8); 2468228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2469228753Smm if (p != NULL) goto success; 2470228753Smm strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2471228753Smm strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 2472228753Smm 2473228753Smm snprintf(buff, sizeof(buff), "%s/test", pwd + 8); 2474228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2475228753Smm if (p != NULL) goto success; 2476228753Smm strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 2477228753Smm strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 2478228753Smm } 2479228753Smm 2480228753Smmfailure: 2481228753Smm printf("Unable to locate known reference file %s\n", KNOWNREF); 2482228753Smm printf(" Checked following directories:\n%s\n", tried); 2483228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) 2484228753Smm DebugBreak(); 2485228753Smm#endif 2486228753Smm exit(1); 2487228753Smm 2488228753Smmsuccess: 2489228753Smm free(p); 2490228753Smm free(pwd); 2491228753Smm return strdup(buff); 2492228753Smm} 2493228753Smm 2494228753Smmint 2495228753Smmmain(int argc, char **argv) 2496228753Smm{ 2497228753Smm static const int limit = sizeof(tests) / sizeof(tests[0]); 2498238856Smm int test_set[sizeof(tests) / sizeof(tests[0])]; 2499238856Smm int i = 0, j = 0, tests_run = 0, tests_failed = 0, option; 2500228753Smm time_t now; 2501228753Smm char *refdir_alloc = NULL; 2502228753Smm const char *progname; 2503232153Smm char **saved_argv; 2504228753Smm const char *tmp, *option_arg, *p; 2505238856Smm char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL; 2506228753Smm char tmpdir_timestamp[256]; 2507228753Smm 2508228753Smm (void)argc; /* UNUSED */ 2509228753Smm 2510232153Smm /* Get the current dir. */ 2511232153Smm#ifdef PATH_MAX 2512232153Smm pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 2513232153Smm#else 2514232153Smm pwd = getcwd(NULL, 0); 2515232153Smm#endif 2516232153Smm while (pwd[strlen(pwd) - 1] == '\n') 2517232153Smm pwd[strlen(pwd) - 1] = '\0'; 2518232153Smm 2519228753Smm#if defined(HAVE__CrtSetReportMode) 2520228753Smm /* To stop to run the default invalid parameter handler. */ 2521228753Smm _set_invalid_parameter_handler(invalid_parameter_handler); 2522228753Smm /* Disable annoying assertion message box. */ 2523228753Smm _CrtSetReportMode(_CRT_ASSERT, 0); 2524228753Smm#endif 2525228753Smm 2526228753Smm /* 2527228753Smm * Name of this program, used to build root of our temp directory 2528228753Smm * tree. 2529228753Smm */ 2530228753Smm progname = p = argv[0]; 2531232153Smm if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL) 2532232153Smm { 2533232153Smm fprintf(stderr, "ERROR: Out of memory."); 2534232153Smm exit(1); 2535232153Smm } 2536232153Smm strcpy(testprogdir, progname); 2537228753Smm while (*p != '\0') { 2538228753Smm /* Support \ or / dir separators for Windows compat. */ 2539228753Smm if (*p == '/' || *p == '\\') 2540232153Smm { 2541228753Smm progname = p + 1; 2542232153Smm i = j; 2543232153Smm } 2544228753Smm ++p; 2545232153Smm j++; 2546228753Smm } 2547232153Smm testprogdir[i] = '\0'; 2548232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2549232153Smm if (testprogdir[0] != '/' && testprogdir[0] != '\\' && 2550232153Smm !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') || 2551232153Smm (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) && 2552232153Smm testprogdir[1] == ':' && 2553232153Smm (testprogdir[2] == '/' || testprogdir[2] == '\\'))) 2554232153Smm#else 2555232153Smm if (testprogdir[0] != '/') 2556232153Smm#endif 2557232153Smm { 2558232153Smm /* Fixup path for relative directories. */ 2559232153Smm if ((testprogdir = (char *)realloc(testprogdir, 2560232153Smm strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL) 2561232153Smm { 2562232153Smm fprintf(stderr, "ERROR: Out of memory."); 2563232153Smm exit(1); 2564232153Smm } 2565232153Smm memmove(testprogdir + strlen(pwd) + 1, testprogdir, 2566232153Smm strlen(testprogdir)); 2567232153Smm memcpy(testprogdir, pwd, strlen(pwd)); 2568232153Smm testprogdir[strlen(pwd)] = '/'; 2569232153Smm } 2570228753Smm 2571228753Smm#ifdef PROGRAM 2572228753Smm /* Get the target program from environment, if available. */ 2573228753Smm testprogfile = getenv(ENVBASE); 2574228753Smm#endif 2575228753Smm 2576228753Smm if (getenv("TMPDIR") != NULL) 2577228753Smm tmp = getenv("TMPDIR"); 2578228753Smm else if (getenv("TMP") != NULL) 2579228753Smm tmp = getenv("TMP"); 2580228753Smm else if (getenv("TEMP") != NULL) 2581228753Smm tmp = getenv("TEMP"); 2582228753Smm else if (getenv("TEMPDIR") != NULL) 2583228753Smm tmp = getenv("TEMPDIR"); 2584228753Smm else 2585228753Smm tmp = "/tmp"; 2586228753Smm 2587228753Smm /* Allow -d to be controlled through the environment. */ 2588228753Smm if (getenv(ENVBASE "_DEBUG") != NULL) 2589228753Smm dump_on_failure = 1; 2590228753Smm 2591238856Smm /* Allow -v to be controlled through the environment. */ 2592238856Smm if (getenv("_VERBOSITY_LEVEL") != NULL) 2593238856Smm { 2594238856Smm vlevel = getenv("_VERBOSITY_LEVEL"); 2595238856Smm verbosity = atoi(vlevel); 2596238856Smm if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL) 2597238856Smm { 2598238856Smm /* Unsupported verbosity levels are silently ignored */ 2599238856Smm vlevel = NULL; 2600238856Smm verbosity = VERBOSITY_PASSFAIL; 2601238856Smm } 2602238856Smm } 2603238856Smm 2604228753Smm /* Get the directory holding test files from environment. */ 2605228753Smm refdir = getenv(ENVBASE "_TEST_FILES"); 2606228753Smm 2607228753Smm /* 2608228753Smm * Parse options, without using getopt(), which isn't available 2609228753Smm * on all platforms. 2610228753Smm */ 2611228753Smm ++argv; /* Skip program name */ 2612228753Smm while (*argv != NULL) { 2613228753Smm if (**argv != '-') 2614228753Smm break; 2615228753Smm p = *argv++; 2616228753Smm ++p; /* Skip '-' */ 2617228753Smm while (*p != '\0') { 2618228753Smm option = *p++; 2619228753Smm option_arg = NULL; 2620228753Smm /* If 'opt' takes an argument, parse that. */ 2621228753Smm if (option == 'p' || option == 'r') { 2622228753Smm if (*p != '\0') 2623228753Smm option_arg = p; 2624228753Smm else if (*argv == NULL) { 2625228753Smm fprintf(stderr, 2626228753Smm "Option -%c requires argument.\n", 2627228753Smm option); 2628228753Smm usage(progname); 2629228753Smm } else 2630228753Smm option_arg = *argv++; 2631228753Smm p = ""; /* End of this option word. */ 2632228753Smm } 2633228753Smm 2634228753Smm /* Now, handle the option. */ 2635228753Smm switch (option) { 2636228753Smm case 'd': 2637228753Smm dump_on_failure = 1; 2638228753Smm break; 2639228753Smm case 'k': 2640228753Smm keep_temp_files = 1; 2641228753Smm break; 2642228753Smm case 'p': 2643228753Smm#ifdef PROGRAM 2644228753Smm testprogfile = option_arg; 2645228753Smm#else 2646228753Smm fprintf(stderr, "-p option not permitted\n"); 2647228753Smm usage(progname); 2648228753Smm#endif 2649228753Smm break; 2650228753Smm case 'q': 2651238856Smm if (!vlevel) 2652238856Smm verbosity--; 2653228753Smm break; 2654228753Smm case 'r': 2655228753Smm refdir = option_arg; 2656228753Smm break; 2657232153Smm case 'u': 2658232153Smm until_failure++; 2659232153Smm break; 2660228753Smm case 'v': 2661238856Smm if (!vlevel) 2662238856Smm verbosity++; 2663228753Smm break; 2664228753Smm default: 2665228753Smm fprintf(stderr, "Unrecognized option '%c'\n", 2666228753Smm option); 2667228753Smm usage(progname); 2668228753Smm } 2669228753Smm } 2670228753Smm } 2671228753Smm 2672228753Smm /* 2673228753Smm * Sanity-check that our options make sense. 2674228753Smm */ 2675228753Smm#ifdef PROGRAM 2676232153Smm if (testprogfile == NULL) 2677232153Smm { 2678232153Smm if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 + 2679232153Smm strlen(PROGRAM) + 1)) == NULL) 2680232153Smm { 2681232153Smm fprintf(stderr, "ERROR: Out of memory."); 2682232153Smm exit(1); 2683232153Smm } 2684232153Smm strcpy(tmp2, testprogdir); 2685232153Smm strcat(tmp2, "/"); 2686232153Smm strcat(tmp2, PROGRAM); 2687232153Smm testprogfile = tmp2; 2688228753Smm } 2689228753Smm 2690228753Smm { 2691228753Smm char *testprg; 2692228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2693228753Smm /* Command.com sometimes rejects '/' separators. */ 2694228753Smm testprg = strdup(testprogfile); 2695228753Smm for (i = 0; testprg[i] != '\0'; i++) { 2696228753Smm if (testprg[i] == '/') 2697228753Smm testprg[i] = '\\'; 2698228753Smm } 2699228753Smm testprogfile = testprg; 2700228753Smm#endif 2701228753Smm /* Quote the name that gets put into shell command lines. */ 2702228753Smm testprg = malloc(strlen(testprogfile) + 3); 2703228753Smm strcpy(testprg, "\""); 2704228753Smm strcat(testprg, testprogfile); 2705228753Smm strcat(testprg, "\""); 2706228753Smm testprog = testprg; 2707228753Smm } 2708228753Smm#endif 2709228753Smm 2710232153Smm#if !defined(_WIN32) && defined(SIGPIPE) 2711232153Smm { /* Ignore SIGPIPE signals */ 2712232153Smm struct sigaction sa; 2713232153Smm sa.sa_handler = SIG_IGN; 2714232153Smm sigemptyset(&sa.sa_mask); 2715232153Smm sa.sa_flags = 0; 2716232153Smm sigaction(SIGPIPE, &sa, NULL); 2717232153Smm } 2718232153Smm#endif 2719232153Smm 2720228753Smm /* 2721228753Smm * Create a temp directory for the following tests. 2722228753Smm * Include the time the tests started as part of the name, 2723228753Smm * to make it easier to track the results of multiple tests. 2724228753Smm */ 2725228753Smm now = time(NULL); 2726228753Smm for (i = 0; ; i++) { 2727228753Smm strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), 2728228753Smm "%Y-%m-%dT%H.%M.%S", 2729228753Smm localtime(&now)); 2730228753Smm sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname, 2731228753Smm tmpdir_timestamp, i); 2732228753Smm if (assertMakeDir(tmpdir,0755)) 2733228753Smm break; 2734228753Smm if (i >= 999) { 2735228753Smm fprintf(stderr, 2736228753Smm "ERROR: Unable to create temp directory %s\n", 2737228753Smm tmpdir); 2738228753Smm exit(1); 2739228753Smm } 2740228753Smm } 2741228753Smm 2742228753Smm /* 2743228753Smm * If the user didn't specify a directory for locating 2744228753Smm * reference files, try to find the reference files in 2745228753Smm * the "usual places." 2746228753Smm */ 2747228753Smm refdir = refdir_alloc = get_refdir(refdir); 2748228753Smm 2749228753Smm /* 2750228753Smm * Banner with basic information. 2751228753Smm */ 2752228753Smm printf("\n"); 2753228753Smm printf("If tests fail or crash, details will be in:\n"); 2754228753Smm printf(" %s\n", tmpdir); 2755228753Smm printf("\n"); 2756228753Smm if (verbosity > VERBOSITY_SUMMARY_ONLY) { 2757228753Smm printf("Reference files will be read from: %s\n", refdir); 2758228753Smm#ifdef PROGRAM 2759228753Smm printf("Running tests on: %s\n", testprog); 2760228753Smm#endif 2761228753Smm printf("Exercising: "); 2762228753Smm fflush(stdout); 2763228753Smm printf("%s\n", EXTRA_VERSION); 2764228753Smm } else { 2765228753Smm printf("Running "); 2766228753Smm fflush(stdout); 2767228753Smm } 2768228753Smm 2769228753Smm /* 2770228753Smm * Run some or all of the individual tests. 2771228753Smm */ 2772232153Smm saved_argv = argv; 2773232153Smm do { 2774232153Smm argv = saved_argv; 2775238856Smm do { 2776238856Smm int test_num; 2777238856Smm 2778248616Smm test_num = get_test_set(test_set, limit, *argv, tests); 2779238856Smm if (test_num < 0) { 2780238856Smm printf("*** INVALID Test %s\n", *argv); 2781238856Smm free(refdir_alloc); 2782248616Smm free(testprogdir); 2783238856Smm usage(progname); 2784238856Smm return (1); 2785238856Smm } 2786238856Smm for (i = 0; i < test_num; i++) { 2787232153Smm tests_run++; 2788238856Smm if (test_run(test_set[i], tmpdir)) { 2789232153Smm tests_failed++; 2790232153Smm if (until_failure) 2791232153Smm goto finish; 2792228753Smm } 2793232153Smm } 2794238856Smm if (*argv != NULL) 2795232153Smm argv++; 2796238856Smm } while (*argv != NULL); 2797232153Smm } while (until_failure); 2798228753Smm 2799232153Smmfinish: 2800232153Smm /* Must be freed after all tests run */ 2801232153Smm free(tmp2); 2802232153Smm free(testprogdir); 2803232153Smm free(pwd); 2804232153Smm 2805228753Smm /* 2806228753Smm * Report summary statistics. 2807228753Smm */ 2808228753Smm if (verbosity > VERBOSITY_SUMMARY_ONLY) { 2809228753Smm printf("\n"); 2810228753Smm printf("Totals:\n"); 2811228753Smm printf(" Tests run: %8d\n", tests_run); 2812228753Smm printf(" Tests failed: %8d\n", tests_failed); 2813228753Smm printf(" Assertions checked:%8d\n", assertions); 2814228753Smm printf(" Assertions failed: %8d\n", failures); 2815228753Smm printf(" Skips reported: %8d\n", skips); 2816228753Smm } 2817228753Smm if (failures) { 2818228753Smm printf("\n"); 2819228753Smm printf("Failing tests:\n"); 2820228753Smm for (i = 0; i < limit; ++i) { 2821228753Smm if (tests[i].failures) 2822228753Smm printf(" %d: %s (%d failures)\n", i, 2823228753Smm tests[i].name, tests[i].failures); 2824228753Smm } 2825228753Smm printf("\n"); 2826228753Smm printf("Details for failing tests: %s\n", tmpdir); 2827228753Smm printf("\n"); 2828228753Smm } else { 2829228753Smm if (verbosity == VERBOSITY_SUMMARY_ONLY) 2830228753Smm printf("\n"); 2831228753Smm printf("%d tests passed, no failures\n", tests_run); 2832228753Smm } 2833228753Smm 2834228753Smm free(refdir_alloc); 2835228753Smm 2836228753Smm /* If the final tmpdir is empty, we can remove it. */ 2837228753Smm /* This should be the usual case when all tests succeed. */ 2838228753Smm assertChdir(".."); 2839228753Smm rmdir(tmpdir); 2840228753Smm 2841228753Smm return (tests_failed ? 1 : 0); 2842228753Smm} 2843