test_main.c revision 306669
1228753Smm/* 2228753Smm * Copyright (c) 2003-2009 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm 26228753Smm#include "test.h" 27248590Smm#include "test_utils.h" 28238825Smm#ifdef HAVE_SYS_IOCTL_H 29238825Smm#include <sys/ioctl.h> 30238825Smm#endif 31231200Smm#ifdef HAVE_SYS_TIME_H 32231200Smm#include <sys/time.h> 33231200Smm#endif 34228753Smm#include <errno.h> 35231200Smm#ifdef HAVE_ICONV_H 36231200Smm#include <iconv.h> 37231200Smm#endif 38238825Smm/* 39238825Smm * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h. 40238825Smm * As the include guards don't agree, the order of include is important. 41238825Smm */ 42238825Smm#ifdef HAVE_LINUX_EXT2_FS_H 43238825Smm#include <linux/ext2_fs.h> /* for Linux file flags */ 44238825Smm#endif 45238825Smm#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__) 46238825Smm#include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */ 47238825Smm#endif 48231200Smm#include <limits.h> 49228753Smm#include <locale.h> 50231200Smm#ifdef HAVE_SIGNAL_H 51231200Smm#include <signal.h> 52231200Smm#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 */ 63228753Smm__FBSDID("$FreeBSD: head/lib/libarchive/test/main.c 201247 2009-12-30 05:59:21Z kientzle $"); 64228753Smm#define KNOWNREF "test_compat_gtar_1.tar.uu" 65228753Smm#define ENVBASE "LIBARCHIVE" /* Prefix for environment variables. */ 66228753Smm#undef PROGRAM /* Testing a library, not a program. */ 67228753Smm#define LIBRARY "libarchive" 68228753Smm#define EXTRA_DUMP(x) archive_error_string((struct archive *)(x)) 69231200Smm#define EXTRA_ERRNO(x) archive_errno((struct archive *)(x)) 70299425Smm#define EXTRA_VERSION archive_version_details() 71228753Smm 72228753Smm/* 73228753Smm * 74228753Smm * Windows support routines 75228753Smm * 76228753Smm * Note: Configuration is a tricky issue. Using HAVE_* feature macros 77228753Smm * in the test harness is dangerous because they cover up 78228753Smm * configuration errors. The classic example of this is omitting a 79228753Smm * configure check. If libarchive and libarchive_test both look for 80228753Smm * the same feature macro, such errors are hard to detect. Platform 81228753Smm * macros (e.g., _WIN32 or __GNUC__) are a little better, but can 82228753Smm * easily lead to very messy code. It's best to limit yourself 83228753Smm * to only the most generic programming techniques in the test harness 84228753Smm * and thus avoid conditionals altogether. Where that's not possible, 85228753Smm * try to minimize conditionals by grouping platform-specific tests in 86228753Smm * one place (e.g., test_acl_freebsd) or by adding new assert() 87228753Smm * functions (e.g., assertMakeHardlink()) to cover up platform 88228753Smm * differences. Platform-specific coding in libarchive_test is often 89228753Smm * a symptom that some capability is missing from libarchive itself. 90228753Smm */ 91228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 92228753Smm#include <io.h> 93248590Smm#include <direct.h> 94228753Smm#include <windows.h> 95228753Smm#ifndef F_OK 96228753Smm#define F_OK (0) 97228753Smm#endif 98228753Smm#ifndef S_ISDIR 99228753Smm#define S_ISDIR(m) ((m) & _S_IFDIR) 100228753Smm#endif 101228753Smm#ifndef S_ISREG 102228753Smm#define S_ISREG(m) ((m) & _S_IFREG) 103228753Smm#endif 104228753Smm#if !defined(__BORLANDC__) 105228753Smm#define access _access 106231200Smm#undef chdir 107228753Smm#define chdir _chdir 108228753Smm#endif 109228753Smm#ifndef fileno 110228753Smm#define fileno _fileno 111228753Smm#endif 112228753Smm/*#define fstat _fstat64*/ 113228753Smm#if !defined(__BORLANDC__) 114228753Smm#define getcwd _getcwd 115228753Smm#endif 116228753Smm#define lstat stat 117228753Smm/*#define lstat _stat64*/ 118228753Smm/*#define stat _stat64*/ 119228753Smm#define rmdir _rmdir 120228753Smm#if !defined(__BORLANDC__) 121228753Smm#define strdup _strdup 122228753Smm#define umask _umask 123228753Smm#endif 124228753Smm#define int64_t __int64 125228753Smm#endif 126228753Smm 127228753Smm#if defined(HAVE__CrtSetReportMode) 128228753Smm# include <crtdbg.h> 129228753Smm#endif 130228753Smm 131299425Smm/* Path to working directory for current test */ 132299425Smmconst char *testworkdir; 133299425Smm#ifdef PROGRAM 134299425Smm/* Pathname of exe to be tested. */ 135299425Smmconst char *testprogfile; 136299425Smm/* Name of exe to use in printf-formatted command strings. */ 137299425Smm/* On Windows, this includes leading/trailing quotes. */ 138299425Smmconst char *testprog; 139299425Smm#endif 140299425Smm 141228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 142238825Smmstatic void *GetFunctionKernel32(const char *); 143238825Smmstatic int my_CreateSymbolicLinkA(const char *, const char *, int); 144238825Smmstatic int my_CreateHardLinkA(const char *, const char *); 145238825Smmstatic int my_GetFileInformationByName(const char *, 146238825Smm BY_HANDLE_FILE_INFORMATION *); 147238825Smm 148238825Smmstatic void * 149238825SmmGetFunctionKernel32(const char *name) 150228753Smm{ 151228753Smm static HINSTANCE lib; 152228753Smm static int set; 153228753Smm if (!set) { 154228753Smm set = 1; 155228753Smm lib = LoadLibrary("kernel32.dll"); 156228753Smm } 157228753Smm if (lib == NULL) { 158228753Smm fprintf(stderr, "Can't load kernel32.dll?!\n"); 159228753Smm exit(1); 160228753Smm } 161228753Smm return (void *)GetProcAddress(lib, name); 162228753Smm} 163228753Smm 164228753Smmstatic int 165228753Smmmy_CreateSymbolicLinkA(const char *linkname, const char *target, int flags) 166228753Smm{ 167228753Smm static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD); 168228753Smm static int set; 169228753Smm if (!set) { 170228753Smm set = 1; 171228753Smm f = GetFunctionKernel32("CreateSymbolicLinkA"); 172228753Smm } 173228753Smm return f == NULL ? 0 : (*f)(linkname, target, flags); 174228753Smm} 175228753Smm 176228753Smmstatic int 177228753Smmmy_CreateHardLinkA(const char *linkname, const char *target) 178228753Smm{ 179228753Smm static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES); 180228753Smm static int set; 181228753Smm if (!set) { 182228753Smm set = 1; 183228753Smm f = GetFunctionKernel32("CreateHardLinkA"); 184228753Smm } 185228753Smm return f == NULL ? 0 : (*f)(linkname, target, NULL); 186228753Smm} 187228753Smm 188238825Smmstatic int 189228753Smmmy_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi) 190228753Smm{ 191228753Smm HANDLE h; 192228753Smm int r; 193228753Smm 194228753Smm memset(bhfi, 0, sizeof(*bhfi)); 195228753Smm h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, 196231200Smm OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 197228753Smm if (h == INVALID_HANDLE_VALUE) 198228753Smm return (0); 199228753Smm r = GetFileInformationByHandle(h, bhfi); 200228753Smm CloseHandle(h); 201228753Smm return (r); 202228753Smm} 203228753Smm#endif 204228753Smm 205299425Smm#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 206228753Smmstatic void 207228753Smminvalid_parameter_handler(const wchar_t * expression, 208228753Smm const wchar_t * function, const wchar_t * file, 209228753Smm unsigned int line, uintptr_t pReserved) 210228753Smm{ 211228753Smm /* nop */ 212228753Smm} 213228753Smm#endif 214228753Smm 215228753Smm/* 216228753Smm * 217228753Smm * OPTIONS FLAGS 218228753Smm * 219228753Smm */ 220228753Smm 221228753Smm/* Enable core dump on failure. */ 222228753Smmstatic int dump_on_failure = 0; 223228753Smm/* Default is to remove temp dirs and log data for successful tests. */ 224228753Smmstatic int keep_temp_files = 0; 225231200Smm/* Default is to run the specified tests once and report errors. */ 226231200Smmstatic int until_failure = 0; 227228753Smm/* Default is to just report pass/fail for each test. */ 228228753Smmstatic int verbosity = 0; 229228753Smm#define VERBOSITY_SUMMARY_ONLY -1 /* -q */ 230228753Smm#define VERBOSITY_PASSFAIL 0 /* Default */ 231228753Smm#define VERBOSITY_LIGHT_REPORT 1 /* -v */ 232228753Smm#define VERBOSITY_FULL 2 /* -vv */ 233228753Smm/* A few places generate even more output for verbosity > VERBOSITY_FULL, 234228753Smm * mostly for debugging the test harness itself. */ 235228753Smm/* Cumulative count of assertion failures. */ 236228753Smmstatic int failures = 0; 237228753Smm/* Cumulative count of reported skips. */ 238228753Smmstatic int skips = 0; 239228753Smm/* Cumulative count of assertions checked. */ 240228753Smmstatic int assertions = 0; 241228753Smm 242228753Smm/* Directory where uuencoded reference files can be found. */ 243228753Smmstatic const char *refdir; 244228753Smm 245228753Smm/* 246228753Smm * Report log information selectively to console and/or disk log. 247228753Smm */ 248228753Smmstatic int log_console = 0; 249228753Smmstatic FILE *logfile; 250228753Smmstatic void 251228753Smmvlogprintf(const char *fmt, va_list ap) 252228753Smm{ 253228753Smm#ifdef va_copy 254228753Smm va_list lfap; 255228753Smm va_copy(lfap, ap); 256228753Smm#endif 257228753Smm if (log_console) 258228753Smm vfprintf(stdout, fmt, ap); 259228753Smm if (logfile != NULL) 260228753Smm#ifdef va_copy 261228753Smm vfprintf(logfile, fmt, lfap); 262228753Smm va_end(lfap); 263228753Smm#else 264228753Smm vfprintf(logfile, fmt, ap); 265228753Smm#endif 266228753Smm} 267228753Smm 268228753Smmstatic void 269228753Smmlogprintf(const char *fmt, ...) 270228753Smm{ 271228753Smm va_list ap; 272228753Smm va_start(ap, fmt); 273228753Smm vlogprintf(fmt, ap); 274228753Smm va_end(ap); 275228753Smm} 276228753Smm 277228753Smm/* Set up a message to display only if next assertion fails. */ 278228753Smmstatic char msgbuff[4096]; 279228753Smmstatic const char *msg, *nextmsg; 280228753Smmvoid 281228753Smmfailure(const char *fmt, ...) 282228753Smm{ 283228753Smm va_list ap; 284231200Smm if (fmt == NULL) { 285231200Smm nextmsg = NULL; 286231200Smm } else { 287231200Smm va_start(ap, fmt); 288231200Smm vsprintf(msgbuff, fmt, ap); 289231200Smm va_end(ap); 290231200Smm nextmsg = msgbuff; 291231200Smm } 292228753Smm} 293228753Smm 294228753Smm/* 295228753Smm * Copy arguments into file-local variables. 296228753Smm * This was added to permit vararg assert() functions without needing 297228753Smm * variadic wrapper macros. Turns out that the vararg capability is almost 298228753Smm * never used, so almost all of the vararg assertions can be simplified 299228753Smm * by removing the vararg capability and reworking the wrapper macro to 300228753Smm * pass __FILE__, __LINE__ directly into the function instead of using 301228753Smm * this hook. I suspect this machinery is used so rarely that we 302228753Smm * would be better off just removing it entirely. That would simplify 303231200Smm * the code here noticeably. 304228753Smm */ 305231200Smmstatic const char *skipping_filename; 306231200Smmstatic int skipping_line; 307231200Smmvoid skipping_setup(const char *filename, int line) 308228753Smm{ 309231200Smm skipping_filename = filename; 310231200Smm skipping_line = line; 311228753Smm} 312228753Smm 313228753Smm/* Called at the beginning of each assert() function. */ 314228753Smmstatic void 315228753Smmassertion_count(const char *file, int line) 316228753Smm{ 317228753Smm (void)file; /* UNUSED */ 318228753Smm (void)line; /* UNUSED */ 319228753Smm ++assertions; 320228753Smm /* Proper handling of "failure()" message. */ 321228753Smm msg = nextmsg; 322228753Smm nextmsg = NULL; 323228753Smm /* Uncomment to print file:line after every assertion. 324228753Smm * Verbose, but occasionally useful in tracking down crashes. */ 325228753Smm /* printf("Checked %s:%d\n", file, line); */ 326228753Smm} 327228753Smm 328228753Smm/* 329228753Smm * For each test source file, we remember how many times each 330228753Smm * assertion was reported. Cleared before each new test, 331228753Smm * used by test_summarize(). 332228753Smm */ 333228753Smmstatic struct line { 334228753Smm int count; 335228753Smm int skip; 336228753Smm} failed_lines[10000]; 337231200Smmconst char *failed_filename; 338228753Smm 339228753Smm/* Count this failure, setup up log destination and handle initial report. */ 340228753Smmstatic void 341228753Smmfailure_start(const char *filename, int line, const char *fmt, ...) 342228753Smm{ 343228753Smm va_list ap; 344228753Smm 345228753Smm /* Record another failure for this line. */ 346228753Smm ++failures; 347231200Smm failed_filename = filename; 348228753Smm failed_lines[line].count++; 349228753Smm 350228753Smm /* Determine whether to log header to console. */ 351228753Smm switch (verbosity) { 352228753Smm case VERBOSITY_LIGHT_REPORT: 353228753Smm log_console = (failed_lines[line].count < 2); 354228753Smm break; 355228753Smm default: 356228753Smm log_console = (verbosity >= VERBOSITY_FULL); 357228753Smm } 358228753Smm 359228753Smm /* Log file:line header for this failure */ 360228753Smm va_start(ap, fmt); 361228753Smm#if _MSC_VER 362228753Smm logprintf("%s(%d): ", filename, line); 363228753Smm#else 364228753Smm logprintf("%s:%d: ", filename, line); 365228753Smm#endif 366228753Smm vlogprintf(fmt, ap); 367228753Smm va_end(ap); 368228753Smm logprintf("\n"); 369228753Smm 370228753Smm if (msg != NULL && msg[0] != '\0') { 371228753Smm logprintf(" Description: %s\n", msg); 372228753Smm msg = NULL; 373228753Smm } 374228753Smm 375228753Smm /* Determine whether to log details to console. */ 376228753Smm if (verbosity == VERBOSITY_LIGHT_REPORT) 377228753Smm log_console = 0; 378228753Smm} 379228753Smm 380228753Smm/* Complete reporting of failed tests. */ 381228753Smm/* 382228753Smm * The 'extra' hook here is used by libarchive to include libarchive 383228753Smm * error messages with assertion failures. It could also be used 384228753Smm * to add strerror() output, for example. Just define the EXTRA_DUMP() 385228753Smm * macro appropriately. 386228753Smm */ 387228753Smmstatic void 388228753Smmfailure_finish(void *extra) 389228753Smm{ 390228753Smm (void)extra; /* UNUSED (maybe) */ 391228753Smm#ifdef EXTRA_DUMP 392231200Smm if (extra != NULL) { 393231200Smm logprintf(" errno: %d\n", EXTRA_ERRNO(extra)); 394228753Smm logprintf(" detail: %s\n", EXTRA_DUMP(extra)); 395231200Smm } 396228753Smm#endif 397228753Smm 398228753Smm if (dump_on_failure) { 399228753Smm fprintf(stderr, 400228753Smm " *** forcing core dump so failure can be debugged ***\n"); 401228753Smm abort(); 402228753Smm } 403228753Smm} 404228753Smm 405228753Smm/* Inform user that we're skipping some checks. */ 406228753Smmvoid 407228753Smmtest_skipping(const char *fmt, ...) 408228753Smm{ 409228753Smm char buff[1024]; 410228753Smm va_list ap; 411228753Smm 412228753Smm va_start(ap, fmt); 413228753Smm vsprintf(buff, fmt, ap); 414228753Smm va_end(ap); 415231200Smm /* Use failure() message if set. */ 416231200Smm msg = nextmsg; 417231200Smm nextmsg = NULL; 418228753Smm /* failure_start() isn't quite right, but is awfully convenient. */ 419231200Smm failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff); 420228753Smm --failures; /* Undo failures++ in failure_start() */ 421228753Smm /* Don't failure_finish() here. */ 422228753Smm /* Mark as skip, so doesn't count as failed test. */ 423231200Smm failed_lines[skipping_line].skip = 1; 424228753Smm ++skips; 425228753Smm} 426228753Smm 427228753Smm/* 428228753Smm * 429228753Smm * ASSERTIONS 430228753Smm * 431228753Smm */ 432228753Smm 433228753Smm/* Generic assert() just displays the failed condition. */ 434228753Smmint 435228753Smmassertion_assert(const char *file, int line, int value, 436228753Smm const char *condition, void *extra) 437228753Smm{ 438228753Smm assertion_count(file, line); 439228753Smm if (!value) { 440228753Smm failure_start(file, line, "Assertion failed: %s", condition); 441228753Smm failure_finish(extra); 442228753Smm } 443228753Smm return (value); 444228753Smm} 445228753Smm 446228753Smm/* chdir() and report any errors */ 447228753Smmint 448228753Smmassertion_chdir(const char *file, int line, const char *pathname) 449228753Smm{ 450228753Smm assertion_count(file, line); 451228753Smm if (chdir(pathname) == 0) 452228753Smm return (1); 453228753Smm failure_start(file, line, "chdir(\"%s\")", pathname); 454228753Smm failure_finish(NULL); 455228753Smm return (0); 456228753Smm 457228753Smm} 458228753Smm 459228753Smm/* Verify two integers are equal. */ 460228753Smmint 461228753Smmassertion_equal_int(const char *file, int line, 462228753Smm long long v1, const char *e1, long long v2, const char *e2, void *extra) 463228753Smm{ 464228753Smm assertion_count(file, line); 465228753Smm if (v1 == v2) 466228753Smm return (1); 467228753Smm failure_start(file, line, "%s != %s", e1, e2); 468228753Smm logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1); 469228753Smm logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2); 470228753Smm failure_finish(extra); 471228753Smm return (0); 472228753Smm} 473228753Smm 474231200Smm/* 475231200Smm * Utility to convert a single UTF-8 sequence. 476231200Smm */ 477231200Smmstatic int 478231200Smm_utf8_to_unicode(uint32_t *pwc, const char *s, size_t n) 479228753Smm{ 480231200Smm static const char utf8_count[256] = { 481231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */ 482231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */ 483231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */ 484231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */ 485231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */ 486231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */ 487231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */ 488231200Smm 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */ 489231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */ 490231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */ 491231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */ 492231200Smm 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */ 493231200Smm 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */ 494231200Smm 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */ 495231200Smm 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */ 496231200Smm 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */ 497231200Smm }; 498231200Smm int ch; 499231200Smm int cnt; 500231200Smm uint32_t wc; 501231200Smm 502231200Smm *pwc = 0; 503231200Smm 504231200Smm /* Sanity check. */ 505231200Smm if (n == 0) 506231200Smm return (0); 507231200Smm /* 508231200Smm * Decode 1-4 bytes depending on the value of the first byte. 509231200Smm */ 510231200Smm ch = (unsigned char)*s; 511231200Smm if (ch == 0) 512231200Smm return (0); /* Standard: return 0 for end-of-string. */ 513231200Smm cnt = utf8_count[ch]; 514231200Smm 515231200Smm /* Invalide sequence or there are not plenty bytes. */ 516231200Smm if (n < (size_t)cnt) 517231200Smm return (-1); 518231200Smm 519231200Smm /* Make a Unicode code point from a single UTF-8 sequence. */ 520231200Smm switch (cnt) { 521231200Smm case 1: /* 1 byte sequence. */ 522231200Smm *pwc = ch & 0x7f; 523231200Smm return (cnt); 524231200Smm case 2: /* 2 bytes sequence. */ 525231200Smm if ((s[1] & 0xc0) != 0x80) return (-1); 526231200Smm *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f); 527231200Smm return (cnt); 528231200Smm case 3: /* 3 bytes sequence. */ 529231200Smm if ((s[1] & 0xc0) != 0x80) return (-1); 530231200Smm if ((s[2] & 0xc0) != 0x80) return (-1); 531231200Smm wc = ((ch & 0x0f) << 12) 532231200Smm | ((s[1] & 0x3f) << 6) 533231200Smm | (s[2] & 0x3f); 534231200Smm if (wc < 0x800) 535231200Smm return (-1);/* Overlong sequence. */ 536231200Smm break; 537231200Smm case 4: /* 4 bytes sequence. */ 538231200Smm if (n < 4) 539231200Smm return (-1); 540231200Smm if ((s[1] & 0xc0) != 0x80) return (-1); 541231200Smm if ((s[2] & 0xc0) != 0x80) return (-1); 542231200Smm if ((s[3] & 0xc0) != 0x80) return (-1); 543231200Smm wc = ((ch & 0x07) << 18) 544231200Smm | ((s[1] & 0x3f) << 12) 545231200Smm | ((s[2] & 0x3f) << 6) 546231200Smm | (s[3] & 0x3f); 547231200Smm if (wc < 0x10000) 548231200Smm return (-1);/* Overlong sequence. */ 549231200Smm break; 550231200Smm default: 551231200Smm return (-1); 552231200Smm } 553231200Smm 554231200Smm /* The code point larger than 0x10FFFF is not leagal 555231200Smm * Unicode values. */ 556231200Smm if (wc > 0x10FFFF) 557231200Smm return (-1); 558231200Smm /* Correctly gets a Unicode, returns used bytes. */ 559231200Smm *pwc = wc; 560231200Smm return (cnt); 561231200Smm} 562231200Smm 563231200Smmstatic void strdump(const char *e, const char *p, int ewidth, int utf8) 564231200Smm{ 565228753Smm const char *q = p; 566228753Smm 567231200Smm logprintf(" %*s = ", ewidth, e); 568228753Smm if (p == NULL) { 569231200Smm logprintf("NULL\n"); 570228753Smm return; 571228753Smm } 572228753Smm logprintf("\""); 573228753Smm while (*p != '\0') { 574228753Smm unsigned int c = 0xff & *p++; 575228753Smm switch (c) { 576299425Smm case '\a': logprintf("\\a"); break; 577299425Smm case '\b': logprintf("\\b"); break; 578299425Smm case '\n': logprintf("\\n"); break; 579299425Smm case '\r': logprintf("\\r"); break; 580228753Smm default: 581228753Smm if (c >= 32 && c < 127) 582228753Smm logprintf("%c", c); 583228753Smm else 584228753Smm logprintf("\\x%02X", c); 585228753Smm } 586228753Smm } 587228753Smm logprintf("\""); 588231200Smm logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q)); 589231200Smm 590231200Smm /* 591231200Smm * If the current string is UTF-8, dump its code points. 592231200Smm */ 593231200Smm if (utf8) { 594231200Smm size_t len; 595231200Smm uint32_t uc; 596231200Smm int n; 597231200Smm int cnt = 0; 598231200Smm 599231200Smm p = q; 600231200Smm len = strlen(p); 601231200Smm logprintf(" ["); 602231200Smm while ((n = _utf8_to_unicode(&uc, p, len)) > 0) { 603231200Smm if (p != q) 604231200Smm logprintf(" "); 605231200Smm logprintf("%04X", uc); 606231200Smm p += n; 607231200Smm len -= n; 608231200Smm cnt++; 609231200Smm } 610231200Smm logprintf("]"); 611231200Smm logprintf(" (count %d", cnt); 612231200Smm if (n < 0) { 613231200Smm logprintf(",unknown %d bytes", len); 614231200Smm } 615231200Smm logprintf(")"); 616231200Smm 617231200Smm } 618231200Smm logprintf("\n"); 619228753Smm} 620228753Smm 621228753Smm/* Verify two strings are equal, dump them if not. */ 622228753Smmint 623228753Smmassertion_equal_string(const char *file, int line, 624228753Smm const char *v1, const char *e1, 625228753Smm const char *v2, const char *e2, 626231200Smm void *extra, int utf8) 627228753Smm{ 628231200Smm int l1, l2; 629231200Smm 630228753Smm assertion_count(file, line); 631228753Smm if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0)) 632228753Smm return (1); 633228753Smm failure_start(file, line, "%s != %s", e1, e2); 634248590Smm l1 = (int)strlen(e1); 635248590Smm l2 = (int)strlen(e2); 636231200Smm if (l1 < l2) 637231200Smm l1 = l2; 638231200Smm strdump(e1, v1, l1, utf8); 639231200Smm strdump(e2, v2, l1, utf8); 640228753Smm failure_finish(extra); 641228753Smm return (0); 642228753Smm} 643228753Smm 644228753Smmstatic void 645228753Smmwcsdump(const char *e, const wchar_t *w) 646228753Smm{ 647228753Smm logprintf(" %s = ", e); 648228753Smm if (w == NULL) { 649228753Smm logprintf("(null)"); 650228753Smm return; 651228753Smm } 652228753Smm logprintf("\""); 653228753Smm while (*w != L'\0') { 654228753Smm unsigned int c = *w++; 655228753Smm if (c >= 32 && c < 127) 656228753Smm logprintf("%c", c); 657228753Smm else if (c < 256) 658228753Smm logprintf("\\x%02X", c); 659228753Smm else if (c < 0x10000) 660228753Smm logprintf("\\u%04X", c); 661228753Smm else 662228753Smm logprintf("\\U%08X", c); 663228753Smm } 664228753Smm logprintf("\"\n"); 665228753Smm} 666228753Smm 667228753Smm#ifndef HAVE_WCSCMP 668228753Smmstatic int 669228753Smmwcscmp(const wchar_t *s1, const wchar_t *s2) 670228753Smm{ 671228753Smm 672228753Smm while (*s1 == *s2++) { 673228753Smm if (*s1++ == L'\0') 674228753Smm return 0; 675228753Smm } 676228753Smm if (*s1 > *--s2) 677228753Smm return 1; 678228753Smm else 679228753Smm return -1; 680228753Smm} 681228753Smm#endif 682228753Smm 683228753Smm/* Verify that two wide strings are equal, dump them if not. */ 684228753Smmint 685228753Smmassertion_equal_wstring(const char *file, int line, 686228753Smm const wchar_t *v1, const char *e1, 687228753Smm const wchar_t *v2, const char *e2, 688228753Smm void *extra) 689228753Smm{ 690228753Smm assertion_count(file, line); 691231200Smm if (v1 == v2) 692228753Smm return (1); 693231200Smm if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0) 694231200Smm return (1); 695228753Smm failure_start(file, line, "%s != %s", e1, e2); 696228753Smm wcsdump(e1, v1); 697228753Smm wcsdump(e2, v2); 698228753Smm failure_finish(extra); 699228753Smm return (0); 700228753Smm} 701228753Smm 702228753Smm/* 703228753Smm * Pretty standard hexdump routine. As a bonus, if ref != NULL, then 704228753Smm * any bytes in p that differ from ref will be highlighted with '_' 705228753Smm * before and after the hex value. 706228753Smm */ 707228753Smmstatic void 708228753Smmhexdump(const char *p, const char *ref, size_t l, size_t offset) 709228753Smm{ 710228753Smm size_t i, j; 711228753Smm char sep; 712228753Smm 713228753Smm if (p == NULL) { 714228753Smm logprintf("(null)\n"); 715228753Smm return; 716228753Smm } 717228753Smm for(i=0; i < l; i+=16) { 718228753Smm logprintf("%04x", (unsigned)(i + offset)); 719228753Smm sep = ' '; 720228753Smm for (j = 0; j < 16 && i + j < l; j++) { 721228753Smm if (ref != NULL && p[i + j] != ref[i + j]) 722228753Smm sep = '_'; 723228753Smm logprintf("%c%02x", sep, 0xff & (int)p[i+j]); 724228753Smm if (ref != NULL && p[i + j] == ref[i + j]) 725228753Smm sep = ' '; 726228753Smm } 727228753Smm for (; j < 16; j++) { 728228753Smm logprintf("%c ", sep); 729228753Smm sep = ' '; 730228753Smm } 731228753Smm logprintf("%c", sep); 732228753Smm for (j=0; j < 16 && i + j < l; j++) { 733228753Smm int c = p[i + j]; 734228753Smm if (c >= ' ' && c <= 126) 735228753Smm logprintf("%c", c); 736228753Smm else 737228753Smm logprintf("."); 738228753Smm } 739228753Smm logprintf("\n"); 740228753Smm } 741228753Smm} 742228753Smm 743228753Smm/* Verify that two blocks of memory are the same, display the first 744228753Smm * block of differences if they're not. */ 745228753Smmint 746228753Smmassertion_equal_mem(const char *file, int line, 747228753Smm const void *_v1, const char *e1, 748228753Smm const void *_v2, const char *e2, 749228753Smm size_t l, const char *ld, void *extra) 750228753Smm{ 751228753Smm const char *v1 = (const char *)_v1; 752228753Smm const char *v2 = (const char *)_v2; 753228753Smm size_t offset; 754228753Smm 755228753Smm assertion_count(file, line); 756228753Smm if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0)) 757228753Smm return (1); 758248590Smm if (v1 == NULL || v2 == NULL) 759248590Smm return (0); 760228753Smm 761228753Smm failure_start(file, line, "%s != %s", e1, e2); 762228753Smm logprintf(" size %s = %d\n", ld, (int)l); 763228753Smm /* Dump 48 bytes (3 lines) so that the first difference is 764228753Smm * in the second line. */ 765228753Smm offset = 0; 766228753Smm while (l > 64 && memcmp(v1, v2, 32) == 0) { 767228753Smm /* Two lines agree, so step forward one line. */ 768228753Smm v1 += 16; 769228753Smm v2 += 16; 770228753Smm l -= 16; 771228753Smm offset += 16; 772228753Smm } 773228753Smm logprintf(" Dump of %s\n", e1); 774231200Smm hexdump(v1, v2, l < 128 ? l : 128, offset); 775228753Smm logprintf(" Dump of %s\n", e2); 776231200Smm hexdump(v2, v1, l < 128 ? l : 128, offset); 777228753Smm logprintf("\n"); 778228753Smm failure_finish(extra); 779228753Smm return (0); 780228753Smm} 781228753Smm 782299425Smm/* Verify that a block of memory is filled with the specified byte. */ 783299425Smmint 784299425Smmassertion_memory_filled_with(const char *file, int line, 785299425Smm const void *_v1, const char *vd, 786299425Smm size_t l, const char *ld, 787299425Smm char b, const char *bd, void *extra) 788299425Smm{ 789299425Smm const char *v1 = (const char *)_v1; 790299425Smm size_t c = 0; 791299425Smm size_t i; 792299425Smm (void)ld; /* UNUSED */ 793299425Smm 794299425Smm assertion_count(file, line); 795299425Smm 796299425Smm for (i = 0; i < l; ++i) { 797299425Smm if (v1[i] == b) { 798299425Smm ++c; 799299425Smm } 800299425Smm } 801299425Smm if (c == l) 802299425Smm return (1); 803299425Smm 804299425Smm failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd); 805299425Smm logprintf(" Only %d bytes were correct\n", (int)c); 806299425Smm failure_finish(extra); 807299425Smm return (0); 808299425Smm} 809299425Smm 810228753Smm/* Verify that the named file exists and is empty. */ 811228753Smmint 812231200Smmassertion_empty_file(const char *filename, int line, const char *f1) 813228753Smm{ 814228753Smm char buff[1024]; 815228753Smm struct stat st; 816228753Smm ssize_t s; 817228753Smm FILE *f; 818228753Smm 819231200Smm assertion_count(filename, line); 820228753Smm 821228753Smm if (stat(f1, &st) != 0) { 822231200Smm failure_start(filename, line, "Stat failed: %s", f1); 823228753Smm failure_finish(NULL); 824228753Smm return (0); 825228753Smm } 826228753Smm if (st.st_size == 0) 827228753Smm return (1); 828228753Smm 829231200Smm failure_start(filename, line, "File should be empty: %s", f1); 830228753Smm logprintf(" File size: %d\n", (int)st.st_size); 831228753Smm logprintf(" Contents:\n"); 832228753Smm f = fopen(f1, "rb"); 833228753Smm if (f == NULL) { 834228753Smm logprintf(" Unable to open %s\n", f1); 835228753Smm } else { 836228753Smm s = ((off_t)sizeof(buff) < st.st_size) ? 837228753Smm (ssize_t)sizeof(buff) : (ssize_t)st.st_size; 838228753Smm s = fread(buff, 1, s, f); 839228753Smm hexdump(buff, NULL, s, 0); 840228753Smm fclose(f); 841228753Smm } 842228753Smm failure_finish(NULL); 843228753Smm return (0); 844228753Smm} 845228753Smm 846228753Smm/* Verify that the named file exists and is not empty. */ 847228753Smmint 848231200Smmassertion_non_empty_file(const char *filename, int line, const char *f1) 849228753Smm{ 850228753Smm struct stat st; 851228753Smm 852231200Smm assertion_count(filename, line); 853228753Smm 854228753Smm if (stat(f1, &st) != 0) { 855231200Smm failure_start(filename, line, "Stat failed: %s", f1); 856228753Smm failure_finish(NULL); 857228753Smm return (0); 858228753Smm } 859228753Smm if (st.st_size == 0) { 860231200Smm failure_start(filename, line, "File empty: %s", f1); 861228753Smm failure_finish(NULL); 862228753Smm return (0); 863228753Smm } 864228753Smm return (1); 865228753Smm} 866228753Smm 867228753Smm/* Verify that two files have the same contents. */ 868228753Smm/* TODO: hexdump the first bytes that actually differ. */ 869228753Smmint 870231200Smmassertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2) 871228753Smm{ 872228753Smm char buff1[1024]; 873228753Smm char buff2[1024]; 874228753Smm FILE *f1, *f2; 875228753Smm int n1, n2; 876228753Smm 877231200Smm assertion_count(filename, line); 878228753Smm 879228753Smm f1 = fopen(fn1, "rb"); 880228753Smm f2 = fopen(fn2, "rb"); 881248590Smm if (f1 == NULL || f2 == NULL) { 882248590Smm if (f1) fclose(f1); 883248590Smm if (f2) fclose(f2); 884248590Smm return (0); 885248590Smm } 886228753Smm for (;;) { 887248590Smm n1 = (int)fread(buff1, 1, sizeof(buff1), f1); 888248590Smm n2 = (int)fread(buff2, 1, sizeof(buff2), f2); 889228753Smm if (n1 != n2) 890228753Smm break; 891228753Smm if (n1 == 0 && n2 == 0) { 892228753Smm fclose(f1); 893228753Smm fclose(f2); 894228753Smm return (1); 895228753Smm } 896228753Smm if (memcmp(buff1, buff2, n1) != 0) 897228753Smm break; 898228753Smm } 899228753Smm fclose(f1); 900228753Smm fclose(f2); 901231200Smm failure_start(filename, line, "Files not identical"); 902228753Smm logprintf(" file1=\"%s\"\n", fn1); 903228753Smm logprintf(" file2=\"%s\"\n", fn2); 904231200Smm failure_finish(NULL); 905228753Smm return (0); 906228753Smm} 907228753Smm 908228753Smm/* Verify that the named file does exist. */ 909228753Smmint 910231200Smmassertion_file_exists(const char *filename, int line, const char *f) 911228753Smm{ 912231200Smm assertion_count(filename, line); 913228753Smm 914228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 915228753Smm if (!_access(f, 0)) 916228753Smm return (1); 917228753Smm#else 918228753Smm if (!access(f, F_OK)) 919228753Smm return (1); 920228753Smm#endif 921231200Smm failure_start(filename, line, "File should exist: %s", f); 922231200Smm failure_finish(NULL); 923228753Smm return (0); 924228753Smm} 925228753Smm 926228753Smm/* Verify that the named file doesn't exist. */ 927228753Smmint 928231200Smmassertion_file_not_exists(const char *filename, int line, const char *f) 929228753Smm{ 930231200Smm assertion_count(filename, line); 931228753Smm 932228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 933228753Smm if (_access(f, 0)) 934228753Smm return (1); 935228753Smm#else 936228753Smm if (access(f, F_OK)) 937228753Smm return (1); 938228753Smm#endif 939231200Smm failure_start(filename, line, "File should not exist: %s", f); 940231200Smm failure_finish(NULL); 941228753Smm return (0); 942228753Smm} 943228753Smm 944228753Smm/* Compare the contents of a file to a block of memory. */ 945228753Smmint 946231200Smmassertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn) 947228753Smm{ 948228753Smm char *contents; 949228753Smm FILE *f; 950228753Smm int n; 951228753Smm 952231200Smm assertion_count(filename, line); 953228753Smm 954228753Smm f = fopen(fn, "rb"); 955228753Smm if (f == NULL) { 956231200Smm failure_start(filename, line, 957228753Smm "File should exist: %s", fn); 958231200Smm failure_finish(NULL); 959228753Smm return (0); 960228753Smm } 961228753Smm contents = malloc(s * 2); 962248590Smm n = (int)fread(contents, 1, s * 2, f); 963228753Smm fclose(f); 964228753Smm if (n == s && memcmp(buff, contents, s) == 0) { 965228753Smm free(contents); 966228753Smm return (1); 967228753Smm } 968231200Smm failure_start(filename, line, "File contents don't match"); 969228753Smm logprintf(" file=\"%s\"\n", fn); 970228753Smm if (n > 0) 971228753Smm hexdump(contents, buff, n > 512 ? 512 : n, 0); 972228753Smm else { 973228753Smm logprintf(" File empty, contents should be:\n"); 974231200Smm hexdump(buff, NULL, s > 512 ? 512 : s, 0); 975228753Smm } 976231200Smm failure_finish(NULL); 977228753Smm free(contents); 978228753Smm return (0); 979228753Smm} 980228753Smm 981228753Smm/* Check the contents of a text file, being tolerant of line endings. */ 982228753Smmint 983231200Smmassertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn) 984228753Smm{ 985228753Smm char *contents; 986228753Smm const char *btxt, *ftxt; 987228753Smm FILE *f; 988228753Smm int n, s; 989228753Smm 990231200Smm assertion_count(filename, line); 991228753Smm f = fopen(fn, "r"); 992231200Smm if (f == NULL) { 993231200Smm failure_start(filename, line, 994231200Smm "File doesn't exist: %s", fn); 995231200Smm failure_finish(NULL); 996231200Smm return (0); 997231200Smm } 998248590Smm s = (int)strlen(buff); 999228753Smm contents = malloc(s * 2 + 128); 1000248590Smm n = (int)fread(contents, 1, s * 2 + 128 - 1, f); 1001228753Smm if (n >= 0) 1002228753Smm contents[n] = '\0'; 1003228753Smm fclose(f); 1004228753Smm /* Compare texts. */ 1005228753Smm btxt = buff; 1006228753Smm ftxt = (const char *)contents; 1007228753Smm while (*btxt != '\0' && *ftxt != '\0') { 1008228753Smm if (*btxt == *ftxt) { 1009228753Smm ++btxt; 1010228753Smm ++ftxt; 1011228753Smm continue; 1012228753Smm } 1013228753Smm if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') { 1014228753Smm /* Pass over different new line characters. */ 1015228753Smm ++btxt; 1016228753Smm ftxt += 2; 1017228753Smm continue; 1018228753Smm } 1019228753Smm break; 1020228753Smm } 1021228753Smm if (*btxt == '\0' && *ftxt == '\0') { 1022228753Smm free(contents); 1023228753Smm return (1); 1024228753Smm } 1025231200Smm failure_start(filename, line, "Contents don't match"); 1026228753Smm logprintf(" file=\"%s\"\n", fn); 1027231200Smm if (n > 0) { 1028228753Smm hexdump(contents, buff, n, 0); 1029231200Smm logprintf(" expected\n", fn); 1030231200Smm hexdump(buff, contents, s, 0); 1031231200Smm } else { 1032228753Smm logprintf(" File empty, contents should be:\n"); 1033228753Smm hexdump(buff, NULL, s, 0); 1034228753Smm } 1035231200Smm failure_finish(NULL); 1036228753Smm free(contents); 1037228753Smm return (0); 1038228753Smm} 1039228753Smm 1040231200Smm/* Verify that a text file contains the specified lines, regardless of order */ 1041231200Smm/* This could be more efficient if we sorted both sets of lines, etc, but 1042231200Smm * since this is used only for testing and only ever deals with a dozen or so 1043231200Smm * lines at a time, this relatively crude approach is just fine. */ 1044231200Smmint 1045231200Smmassertion_file_contains_lines_any_order(const char *file, int line, 1046231200Smm const char *pathname, const char *lines[]) 1047231200Smm{ 1048231200Smm char *buff; 1049231200Smm size_t buff_size; 1050231200Smm size_t expected_count, actual_count, i, j; 1051248590Smm char **expected = NULL; 1052248590Smm char *p, **actual = NULL; 1053231200Smm char c; 1054231200Smm int expected_failure = 0, actual_failure = 0; 1055231200Smm 1056231200Smm assertion_count(file, line); 1057231200Smm 1058231200Smm buff = slurpfile(&buff_size, "%s", pathname); 1059231200Smm if (buff == NULL) { 1060231200Smm failure_start(pathname, line, "Can't read file: %s", pathname); 1061231200Smm failure_finish(NULL); 1062231200Smm return (0); 1063231200Smm } 1064231200Smm 1065248590Smm /* Make a copy of the provided lines and count up the expected 1066248590Smm * file size. */ 1067231200Smm for (i = 0; lines[i] != NULL; ++i) { 1068231200Smm } 1069231200Smm expected_count = i; 1070248590Smm if (expected_count) { 1071248590Smm expected = malloc(sizeof(char *) * expected_count); 1072248590Smm if (expected == NULL) { 1073248590Smm failure_start(pathname, line, "Can't allocate memory"); 1074248590Smm failure_finish(NULL); 1075299425Smm free(expected); 1076248590Smm return (0); 1077248590Smm } 1078248590Smm for (i = 0; lines[i] != NULL; ++i) { 1079248590Smm expected[i] = strdup(lines[i]); 1080248590Smm } 1081231200Smm } 1082231200Smm 1083231200Smm /* Break the file into lines */ 1084231200Smm actual_count = 0; 1085231200Smm for (c = '\0', p = buff; p < buff + buff_size; ++p) { 1086231200Smm if (*p == '\x0d' || *p == '\x0a') 1087231200Smm *p = '\0'; 1088231200Smm if (c == '\0' && *p != '\0') 1089231200Smm ++actual_count; 1090231200Smm c = *p; 1091231200Smm } 1092248590Smm if (actual_count) { 1093248590Smm actual = calloc(sizeof(char *), actual_count); 1094248590Smm if (actual == NULL) { 1095248590Smm failure_start(pathname, line, "Can't allocate memory"); 1096248590Smm failure_finish(NULL); 1097248590Smm free(expected); 1098248590Smm return (0); 1099231200Smm } 1100248590Smm for (j = 0, p = buff; p < buff + buff_size; 1101248590Smm p += 1 + strlen(p)) { 1102248590Smm if (*p != '\0') { 1103248590Smm actual[j] = p; 1104248590Smm ++j; 1105248590Smm } 1106248590Smm } 1107231200Smm } 1108231200Smm 1109231200Smm /* Erase matching lines from both lists */ 1110231200Smm for (i = 0; i < expected_count; ++i) { 1111231200Smm if (expected[i] == NULL) 1112231200Smm continue; 1113231200Smm for (j = 0; j < actual_count; ++j) { 1114231200Smm if (actual[j] == NULL) 1115231200Smm continue; 1116231200Smm if (strcmp(expected[i], actual[j]) == 0) { 1117231200Smm free(expected[i]); 1118231200Smm expected[i] = NULL; 1119231200Smm actual[j] = NULL; 1120231200Smm break; 1121231200Smm } 1122231200Smm } 1123231200Smm } 1124231200Smm 1125231200Smm /* If there's anything left, it's a failure */ 1126231200Smm for (i = 0; i < expected_count; ++i) { 1127231200Smm if (expected[i] != NULL) 1128231200Smm ++expected_failure; 1129231200Smm } 1130231200Smm for (j = 0; j < actual_count; ++j) { 1131231200Smm if (actual[j] != NULL) 1132231200Smm ++actual_failure; 1133231200Smm } 1134231200Smm if (expected_failure == 0 && actual_failure == 0) { 1135231200Smm free(buff); 1136231200Smm free(expected); 1137231200Smm free(actual); 1138231200Smm return (1); 1139231200Smm } 1140231200Smm failure_start(file, line, "File doesn't match: %s", pathname); 1141231200Smm for (i = 0; i < expected_count; ++i) { 1142231200Smm if (expected[i] != NULL) { 1143231200Smm logprintf(" Expected but not present: %s\n", expected[i]); 1144231200Smm free(expected[i]); 1145231200Smm } 1146231200Smm } 1147231200Smm for (j = 0; j < actual_count; ++j) { 1148231200Smm if (actual[j] != NULL) 1149231200Smm logprintf(" Present but not expected: %s\n", actual[j]); 1150231200Smm } 1151231200Smm failure_finish(NULL); 1152231200Smm free(buff); 1153231200Smm free(expected); 1154231200Smm free(actual); 1155231200Smm return (0); 1156231200Smm} 1157231200Smm 1158228753Smm/* Test that two paths point to the same file. */ 1159228753Smm/* As a side-effect, asserts that both files exist. */ 1160228753Smmstatic int 1161228753Smmis_hardlink(const char *file, int line, 1162228753Smm const char *path1, const char *path2) 1163228753Smm{ 1164228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1165228753Smm BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2; 1166228753Smm int r; 1167228753Smm 1168228753Smm assertion_count(file, line); 1169228753Smm r = my_GetFileInformationByName(path1, &bhfi1); 1170228753Smm if (r == 0) { 1171228753Smm failure_start(file, line, "File %s can't be inspected?", path1); 1172228753Smm failure_finish(NULL); 1173228753Smm return (0); 1174228753Smm } 1175228753Smm r = my_GetFileInformationByName(path2, &bhfi2); 1176228753Smm if (r == 0) { 1177228753Smm failure_start(file, line, "File %s can't be inspected?", path2); 1178228753Smm failure_finish(NULL); 1179228753Smm return (0); 1180228753Smm } 1181228753Smm return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber 1182228753Smm && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh 1183228753Smm && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow); 1184228753Smm#else 1185228753Smm struct stat st1, st2; 1186228753Smm int r; 1187228753Smm 1188228753Smm assertion_count(file, line); 1189228753Smm r = lstat(path1, &st1); 1190228753Smm if (r != 0) { 1191228753Smm failure_start(file, line, "File should exist: %s", path1); 1192228753Smm failure_finish(NULL); 1193228753Smm return (0); 1194228753Smm } 1195228753Smm r = lstat(path2, &st2); 1196228753Smm if (r != 0) { 1197228753Smm failure_start(file, line, "File should exist: %s", path2); 1198228753Smm failure_finish(NULL); 1199228753Smm return (0); 1200228753Smm } 1201228753Smm return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev); 1202228753Smm#endif 1203228753Smm} 1204228753Smm 1205228753Smmint 1206228753Smmassertion_is_hardlink(const char *file, int line, 1207228753Smm const char *path1, const char *path2) 1208228753Smm{ 1209228753Smm if (is_hardlink(file, line, path1, path2)) 1210228753Smm return (1); 1211228753Smm failure_start(file, line, 1212228753Smm "Files %s and %s are not hardlinked", path1, path2); 1213228753Smm failure_finish(NULL); 1214228753Smm return (0); 1215228753Smm} 1216228753Smm 1217228753Smmint 1218228753Smmassertion_is_not_hardlink(const char *file, int line, 1219228753Smm const char *path1, const char *path2) 1220228753Smm{ 1221228753Smm if (!is_hardlink(file, line, path1, path2)) 1222228753Smm return (1); 1223228753Smm failure_start(file, line, 1224228753Smm "Files %s and %s should not be hardlinked", path1, path2); 1225228753Smm failure_finish(NULL); 1226228753Smm return (0); 1227228753Smm} 1228228753Smm 1229228753Smm/* Verify a/b/mtime of 'pathname'. */ 1230228753Smm/* If 'recent', verify that it's within last 10 seconds. */ 1231228753Smmstatic int 1232228753Smmassertion_file_time(const char *file, int line, 1233228753Smm const char *pathname, long t, long nsec, char type, int recent) 1234228753Smm{ 1235228753Smm long long filet, filet_nsec; 1236228753Smm int r; 1237228753Smm 1238228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1239228753Smm#define EPOC_TIME (116444736000000000ULL) 1240248590Smm FILETIME fxtime, fbirthtime, fatime, fmtime; 1241228753Smm ULARGE_INTEGER wintm; 1242228753Smm HANDLE h; 1243248590Smm fxtime.dwLowDateTime = 0; 1244248590Smm fxtime.dwHighDateTime = 0; 1245228753Smm 1246228753Smm assertion_count(file, line); 1247231200Smm /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open 1248231200Smm * a directory file. If not, CreateFile() will fail when 1249231200Smm * the pathname is a directory. */ 1250228753Smm h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL, 1251231200Smm OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 1252228753Smm if (h == INVALID_HANDLE_VALUE) { 1253228753Smm failure_start(file, line, "Can't access %s\n", pathname); 1254228753Smm failure_finish(NULL); 1255228753Smm return (0); 1256228753Smm } 1257228753Smm r = GetFileTime(h, &fbirthtime, &fatime, &fmtime); 1258228753Smm switch (type) { 1259248590Smm case 'a': fxtime = fatime; break; 1260248590Smm case 'b': fxtime = fbirthtime; break; 1261248590Smm case 'm': fxtime = fmtime; break; 1262228753Smm } 1263228753Smm CloseHandle(h); 1264228753Smm if (r == 0) { 1265228753Smm failure_start(file, line, "Can't GetFileTime %s\n", pathname); 1266228753Smm failure_finish(NULL); 1267228753Smm return (0); 1268228753Smm } 1269248590Smm wintm.LowPart = fxtime.dwLowDateTime; 1270248590Smm wintm.HighPart = fxtime.dwHighDateTime; 1271228753Smm filet = (wintm.QuadPart - EPOC_TIME) / 10000000; 1272228753Smm filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100; 1273228753Smm nsec = (nsec / 100) * 100; /* Round the request */ 1274228753Smm#else 1275228753Smm struct stat st; 1276228753Smm 1277228753Smm assertion_count(file, line); 1278228753Smm r = lstat(pathname, &st); 1279228753Smm if (r != 0) { 1280228753Smm failure_start(file, line, "Can't stat %s\n", pathname); 1281228753Smm failure_finish(NULL); 1282228753Smm return (0); 1283228753Smm } 1284228753Smm switch (type) { 1285228753Smm case 'a': filet = st.st_atime; break; 1286228753Smm case 'm': filet = st.st_mtime; break; 1287228753Smm case 'b': filet = 0; break; 1288228753Smm default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1289228753Smm exit(1); 1290228753Smm } 1291228753Smm#if defined(__FreeBSD__) 1292228753Smm switch (type) { 1293228753Smm case 'a': filet_nsec = st.st_atimespec.tv_nsec; break; 1294228753Smm case 'b': filet = st.st_birthtime; 1295302264Smm /* FreeBSD filesystems that don't support birthtime 1296302264Smm * (e.g., UFS1) always return -1 here. */ 1297302264Smm if (filet == -1) { 1298302264Smm return (1); 1299302264Smm } 1300228753Smm filet_nsec = st.st_birthtimespec.tv_nsec; break; 1301228753Smm case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break; 1302228753Smm default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1303228753Smm exit(1); 1304228753Smm } 1305228753Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1306228753Smm filet_nsec = (filet_nsec / 1000) * 1000; 1307228753Smm nsec = (nsec / 1000) * 1000; 1308228753Smm#else 1309228753Smm filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */ 1310228753Smm if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */ 1311228753Smm#if defined(__HAIKU__) 1312228753Smm if (type == 'a') return (1); /* Haiku doesn't have atime. */ 1313228753Smm#endif 1314228753Smm#endif 1315228753Smm#endif 1316228753Smm if (recent) { 1317228753Smm /* Check that requested time is up-to-date. */ 1318228753Smm time_t now = time(NULL); 1319228753Smm if (filet < now - 10 || filet > now + 1) { 1320228753Smm failure_start(file, line, 1321231200Smm "File %s has %ctime %lld, %lld seconds ago\n", 1322228753Smm pathname, type, filet, now - filet); 1323228753Smm failure_finish(NULL); 1324228753Smm return (0); 1325228753Smm } 1326228753Smm } else if (filet != t || filet_nsec != nsec) { 1327228753Smm failure_start(file, line, 1328231200Smm "File %s has %ctime %lld.%09lld, expected %lld.%09lld", 1329228753Smm pathname, type, filet, filet_nsec, t, nsec); 1330228753Smm failure_finish(NULL); 1331228753Smm return (0); 1332228753Smm } 1333228753Smm return (1); 1334228753Smm} 1335228753Smm 1336228753Smm/* Verify atime of 'pathname'. */ 1337228753Smmint 1338228753Smmassertion_file_atime(const char *file, int line, 1339228753Smm const char *pathname, long t, long nsec) 1340228753Smm{ 1341228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'a', 0); 1342228753Smm} 1343228753Smm 1344228753Smm/* Verify atime of 'pathname' is up-to-date. */ 1345228753Smmint 1346228753Smmassertion_file_atime_recent(const char *file, int line, const char *pathname) 1347228753Smm{ 1348228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'a', 1); 1349228753Smm} 1350228753Smm 1351228753Smm/* Verify birthtime of 'pathname'. */ 1352228753Smmint 1353228753Smmassertion_file_birthtime(const char *file, int line, 1354228753Smm const char *pathname, long t, long nsec) 1355228753Smm{ 1356228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'b', 0); 1357228753Smm} 1358228753Smm 1359228753Smm/* Verify birthtime of 'pathname' is up-to-date. */ 1360228753Smmint 1361228753Smmassertion_file_birthtime_recent(const char *file, int line, 1362228753Smm const char *pathname) 1363228753Smm{ 1364228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'b', 1); 1365228753Smm} 1366228753Smm 1367228753Smm/* Verify mtime of 'pathname'. */ 1368228753Smmint 1369228753Smmassertion_file_mtime(const char *file, int line, 1370228753Smm const char *pathname, long t, long nsec) 1371228753Smm{ 1372228753Smm return assertion_file_time(file, line, pathname, t, nsec, 'm', 0); 1373228753Smm} 1374228753Smm 1375228753Smm/* Verify mtime of 'pathname' is up-to-date. */ 1376228753Smmint 1377228753Smmassertion_file_mtime_recent(const char *file, int line, const char *pathname) 1378228753Smm{ 1379228753Smm return assertion_file_time(file, line, pathname, 0, 0, 'm', 1); 1380228753Smm} 1381228753Smm 1382228753Smm/* Verify number of links to 'pathname'. */ 1383228753Smmint 1384228753Smmassertion_file_nlinks(const char *file, int line, 1385228753Smm const char *pathname, int nlinks) 1386228753Smm{ 1387228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1388228753Smm BY_HANDLE_FILE_INFORMATION bhfi; 1389228753Smm int r; 1390228753Smm 1391228753Smm assertion_count(file, line); 1392228753Smm r = my_GetFileInformationByName(pathname, &bhfi); 1393228753Smm if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks) 1394228753Smm return (1); 1395228753Smm failure_start(file, line, "File %s has %d links, expected %d", 1396228753Smm pathname, bhfi.nNumberOfLinks, nlinks); 1397228753Smm failure_finish(NULL); 1398228753Smm return (0); 1399228753Smm#else 1400228753Smm struct stat st; 1401228753Smm int r; 1402228753Smm 1403228753Smm assertion_count(file, line); 1404228753Smm r = lstat(pathname, &st); 1405231297Smm if (r == 0 && (int)st.st_nlink == nlinks) 1406228753Smm return (1); 1407228753Smm failure_start(file, line, "File %s has %d links, expected %d", 1408228753Smm pathname, st.st_nlink, nlinks); 1409228753Smm failure_finish(NULL); 1410228753Smm return (0); 1411228753Smm#endif 1412228753Smm} 1413228753Smm 1414228753Smm/* Verify size of 'pathname'. */ 1415228753Smmint 1416228753Smmassertion_file_size(const char *file, int line, const char *pathname, long size) 1417228753Smm{ 1418228753Smm int64_t filesize; 1419228753Smm int r; 1420228753Smm 1421228753Smm assertion_count(file, line); 1422228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1423228753Smm { 1424228753Smm BY_HANDLE_FILE_INFORMATION bhfi; 1425228753Smm r = !my_GetFileInformationByName(pathname, &bhfi); 1426228753Smm filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow; 1427228753Smm } 1428228753Smm#else 1429228753Smm { 1430228753Smm struct stat st; 1431228753Smm r = lstat(pathname, &st); 1432228753Smm filesize = st.st_size; 1433228753Smm } 1434228753Smm#endif 1435228753Smm if (r == 0 && filesize == size) 1436228753Smm return (1); 1437228753Smm failure_start(file, line, "File %s has size %ld, expected %ld", 1438228753Smm pathname, (long)filesize, (long)size); 1439228753Smm failure_finish(NULL); 1440228753Smm return (0); 1441228753Smm} 1442228753Smm 1443304060Smm/* Verify mode of 'pathname'. */ 1444304060Smmint 1445304060Smmassertion_file_mode(const char *file, int line, const char *pathname, int expected_mode) 1446304060Smm{ 1447304060Smm int mode; 1448304060Smm int r; 1449304060Smm 1450304060Smm assertion_count(file, line); 1451304060Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1452304060Smm failure_start(file, line, "assertFileMode not yet implemented for Windows"); 1453304060Smm#else 1454304060Smm { 1455304060Smm struct stat st; 1456304060Smm r = lstat(pathname, &st); 1457304060Smm mode = (int)(st.st_mode & 0777); 1458304060Smm } 1459304060Smm if (r == 0 && mode == expected_mode) 1460304060Smm return (1); 1461304060Smm failure_start(file, line, "File %s has mode %o, expected %o", 1462304060Smm pathname, mode, expected_mode); 1463304060Smm#endif 1464304060Smm failure_finish(NULL); 1465304060Smm return (0); 1466304060Smm} 1467304060Smm 1468228753Smm/* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */ 1469228753Smmint 1470228753Smmassertion_is_dir(const char *file, int line, const char *pathname, int mode) 1471228753Smm{ 1472228753Smm struct stat st; 1473228753Smm int r; 1474228753Smm 1475228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1476228753Smm (void)mode; /* UNUSED */ 1477228753Smm#endif 1478228753Smm assertion_count(file, line); 1479228753Smm r = lstat(pathname, &st); 1480228753Smm if (r != 0) { 1481228753Smm failure_start(file, line, "Dir should exist: %s", pathname); 1482228753Smm failure_finish(NULL); 1483228753Smm return (0); 1484228753Smm } 1485228753Smm if (!S_ISDIR(st.st_mode)) { 1486228753Smm failure_start(file, line, "%s is not a dir", pathname); 1487228753Smm failure_finish(NULL); 1488228753Smm return (0); 1489228753Smm } 1490228753Smm#if !defined(_WIN32) || defined(__CYGWIN__) 1491228753Smm /* Windows doesn't handle permissions the same way as POSIX, 1492228753Smm * so just ignore the mode tests. */ 1493228753Smm /* TODO: Can we do better here? */ 1494231297Smm if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1495228753Smm failure_start(file, line, "Dir %s has wrong mode", pathname); 1496228753Smm logprintf(" Expected: 0%3o\n", mode); 1497228753Smm logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1498228753Smm failure_finish(NULL); 1499228753Smm return (0); 1500228753Smm } 1501228753Smm#endif 1502228753Smm return (1); 1503228753Smm} 1504228753Smm 1505228753Smm/* Verify that 'pathname' is a regular file. If 'mode' is >= 0, 1506228753Smm * verify that too. */ 1507228753Smmint 1508228753Smmassertion_is_reg(const char *file, int line, const char *pathname, int mode) 1509228753Smm{ 1510228753Smm struct stat st; 1511228753Smm int r; 1512228753Smm 1513228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1514228753Smm (void)mode; /* UNUSED */ 1515228753Smm#endif 1516228753Smm assertion_count(file, line); 1517228753Smm r = lstat(pathname, &st); 1518228753Smm if (r != 0 || !S_ISREG(st.st_mode)) { 1519228753Smm failure_start(file, line, "File should exist: %s", pathname); 1520228753Smm failure_finish(NULL); 1521228753Smm return (0); 1522228753Smm } 1523228753Smm#if !defined(_WIN32) || defined(__CYGWIN__) 1524228753Smm /* Windows doesn't handle permissions the same way as POSIX, 1525228753Smm * so just ignore the mode tests. */ 1526228753Smm /* TODO: Can we do better here? */ 1527231297Smm if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1528228753Smm failure_start(file, line, "File %s has wrong mode", pathname); 1529228753Smm logprintf(" Expected: 0%3o\n", mode); 1530228753Smm logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1531228753Smm failure_finish(NULL); 1532228753Smm return (0); 1533228753Smm } 1534228753Smm#endif 1535228753Smm return (1); 1536228753Smm} 1537228753Smm 1538228753Smm/* Check whether 'pathname' is a symbolic link. If 'contents' is 1539228753Smm * non-NULL, verify that the symlink has those contents. */ 1540228753Smmstatic int 1541228753Smmis_symlink(const char *file, int line, 1542228753Smm const char *pathname, const char *contents) 1543228753Smm{ 1544228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1545228753Smm (void)pathname; /* UNUSED */ 1546228753Smm (void)contents; /* UNUSED */ 1547228753Smm assertion_count(file, line); 1548228753Smm /* Windows sort-of has real symlinks, but they're only usable 1549228753Smm * by privileged users and are crippled even then, so there's 1550228753Smm * really not much point in bothering with this. */ 1551228753Smm return (0); 1552228753Smm#else 1553228753Smm char buff[300]; 1554228753Smm struct stat st; 1555228753Smm ssize_t linklen; 1556228753Smm int r; 1557228753Smm 1558228753Smm assertion_count(file, line); 1559228753Smm r = lstat(pathname, &st); 1560228753Smm if (r != 0) { 1561228753Smm failure_start(file, line, 1562228753Smm "Symlink should exist: %s", pathname); 1563228753Smm failure_finish(NULL); 1564228753Smm return (0); 1565228753Smm } 1566228753Smm if (!S_ISLNK(st.st_mode)) 1567228753Smm return (0); 1568228753Smm if (contents == NULL) 1569228753Smm return (1); 1570228753Smm linklen = readlink(pathname, buff, sizeof(buff)); 1571228753Smm if (linklen < 0) { 1572228753Smm failure_start(file, line, "Can't read symlink %s", pathname); 1573228753Smm failure_finish(NULL); 1574228753Smm return (0); 1575228753Smm } 1576228753Smm buff[linklen] = '\0'; 1577228753Smm if (strcmp(buff, contents) != 0) 1578228753Smm return (0); 1579228753Smm return (1); 1580228753Smm#endif 1581228753Smm} 1582228753Smm 1583228753Smm/* Assert that path is a symlink that (optionally) contains contents. */ 1584228753Smmint 1585228753Smmassertion_is_symlink(const char *file, int line, 1586228753Smm const char *path, const char *contents) 1587228753Smm{ 1588228753Smm if (is_symlink(file, line, path, contents)) 1589228753Smm return (1); 1590228753Smm if (contents) 1591228753Smm failure_start(file, line, "File %s is not a symlink to %s", 1592228753Smm path, contents); 1593228753Smm else 1594228753Smm failure_start(file, line, "File %s is not a symlink", path); 1595228753Smm failure_finish(NULL); 1596228753Smm return (0); 1597228753Smm} 1598228753Smm 1599228753Smm 1600228753Smm/* Create a directory and report any errors. */ 1601228753Smmint 1602228753Smmassertion_make_dir(const char *file, int line, const char *dirname, int mode) 1603228753Smm{ 1604228753Smm assertion_count(file, line); 1605228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1606228753Smm (void)mode; /* UNUSED */ 1607228753Smm if (0 == _mkdir(dirname)) 1608228753Smm return (1); 1609228753Smm#else 1610306669Smm if (0 == mkdir(dirname, mode)) { 1611306669Smm if (0 == chmod(dirname, mode)) { 1612306669Smm assertion_file_mode(file, line, dirname, mode); 1613306669Smm return (1); 1614306669Smm } 1615306669Smm } 1616228753Smm#endif 1617228753Smm failure_start(file, line, "Could not create directory %s", dirname); 1618228753Smm failure_finish(NULL); 1619228753Smm return(0); 1620228753Smm} 1621228753Smm 1622228753Smm/* Create a file with the specified contents and report any failures. */ 1623228753Smmint 1624228753Smmassertion_make_file(const char *file, int line, 1625238825Smm const char *path, int mode, int csize, const void *contents) 1626228753Smm{ 1627228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1628228753Smm /* TODO: Rework this to set file mode as well. */ 1629228753Smm FILE *f; 1630228753Smm (void)mode; /* UNUSED */ 1631228753Smm assertion_count(file, line); 1632228753Smm f = fopen(path, "wb"); 1633228753Smm if (f == NULL) { 1634228753Smm failure_start(file, line, "Could not create file %s", path); 1635228753Smm failure_finish(NULL); 1636228753Smm return (0); 1637228753Smm } 1638228753Smm if (contents != NULL) { 1639238825Smm size_t wsize; 1640238825Smm 1641238825Smm if (csize < 0) 1642238825Smm wsize = strlen(contents); 1643238825Smm else 1644238825Smm wsize = (size_t)csize; 1645238825Smm if (wsize != fwrite(contents, 1, wsize, f)) { 1646228753Smm fclose(f); 1647228753Smm failure_start(file, line, 1648228753Smm "Could not write file %s", path); 1649228753Smm failure_finish(NULL); 1650228753Smm return (0); 1651228753Smm } 1652228753Smm } 1653228753Smm fclose(f); 1654228753Smm return (1); 1655228753Smm#else 1656228753Smm int fd; 1657228753Smm assertion_count(file, line); 1658228753Smm fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644); 1659228753Smm if (fd < 0) { 1660228753Smm failure_start(file, line, "Could not create %s", path); 1661228753Smm failure_finish(NULL); 1662228753Smm return (0); 1663228753Smm } 1664306669Smm if (0 != chmod(path, mode)) { 1665306669Smm failure_start(file, line, "Could not chmod %s", path); 1666306669Smm failure_finish(NULL); 1667306669Smm return (0); 1668306669Smm } 1669228753Smm if (contents != NULL) { 1670238825Smm ssize_t wsize; 1671238825Smm 1672238825Smm if (csize < 0) 1673238825Smm wsize = (ssize_t)strlen(contents); 1674238825Smm else 1675238825Smm wsize = (ssize_t)csize; 1676238825Smm if (wsize != write(fd, contents, wsize)) { 1677228753Smm close(fd); 1678238825Smm failure_start(file, line, 1679238825Smm "Could not write to %s", path); 1680228753Smm failure_finish(NULL); 1681228753Smm return (0); 1682228753Smm } 1683228753Smm } 1684228753Smm close(fd); 1685306669Smm assertion_file_mode(file, line, path, mode); 1686228753Smm return (1); 1687228753Smm#endif 1688228753Smm} 1689228753Smm 1690228753Smm/* Create a hardlink and report any failures. */ 1691228753Smmint 1692228753Smmassertion_make_hardlink(const char *file, int line, 1693228753Smm const char *newpath, const char *linkto) 1694228753Smm{ 1695228753Smm int succeeded; 1696228753Smm 1697228753Smm assertion_count(file, line); 1698228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1699228753Smm succeeded = my_CreateHardLinkA(newpath, linkto); 1700228753Smm#elif HAVE_LINK 1701228753Smm succeeded = !link(linkto, newpath); 1702228753Smm#else 1703228753Smm succeeded = 0; 1704228753Smm#endif 1705228753Smm if (succeeded) 1706228753Smm return (1); 1707228753Smm failure_start(file, line, "Could not create hardlink"); 1708228753Smm logprintf(" New link: %s\n", newpath); 1709228753Smm logprintf(" Old name: %s\n", linkto); 1710228753Smm failure_finish(NULL); 1711228753Smm return(0); 1712228753Smm} 1713228753Smm 1714228753Smm/* Create a symlink and report any failures. */ 1715228753Smmint 1716228753Smmassertion_make_symlink(const char *file, int line, 1717228753Smm const char *newpath, const char *linkto) 1718228753Smm{ 1719228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1720228753Smm int targetIsDir = 0; /* TODO: Fix this */ 1721228753Smm assertion_count(file, line); 1722228753Smm if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir)) 1723228753Smm return (1); 1724228753Smm#elif HAVE_SYMLINK 1725228753Smm assertion_count(file, line); 1726228753Smm if (0 == symlink(linkto, newpath)) 1727228753Smm return (1); 1728228753Smm#endif 1729228753Smm failure_start(file, line, "Could not create symlink"); 1730228753Smm logprintf(" New link: %s\n", newpath); 1731228753Smm logprintf(" Old name: %s\n", linkto); 1732228753Smm failure_finish(NULL); 1733228753Smm return(0); 1734228753Smm} 1735228753Smm 1736228753Smm/* Set umask, report failures. */ 1737228753Smmint 1738228753Smmassertion_umask(const char *file, int line, int mask) 1739228753Smm{ 1740228753Smm assertion_count(file, line); 1741228753Smm (void)file; /* UNUSED */ 1742228753Smm (void)line; /* UNUSED */ 1743228753Smm umask(mask); 1744228753Smm return (1); 1745228753Smm} 1746228753Smm 1747231200Smm/* Set times, report failures. */ 1748231200Smmint 1749231200Smmassertion_utimes(const char *file, int line, 1750231200Smm const char *pathname, long at, long at_nsec, long mt, long mt_nsec) 1751231200Smm{ 1752231200Smm int r; 1753231200Smm 1754231200Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1755231200Smm#define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\ 1756231200Smm + (((nsec)/1000)*10)) 1757231200Smm HANDLE h; 1758231200Smm ULARGE_INTEGER wintm; 1759231200Smm FILETIME fatime, fmtime; 1760231200Smm FILETIME *pat, *pmt; 1761231200Smm 1762231200Smm assertion_count(file, line); 1763231200Smm h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE, 1764231200Smm FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 1765231200Smm FILE_FLAG_BACKUP_SEMANTICS, NULL); 1766231200Smm if (h == INVALID_HANDLE_VALUE) { 1767231200Smm failure_start(file, line, "Can't access %s\n", pathname); 1768231200Smm failure_finish(NULL); 1769231200Smm return (0); 1770231200Smm } 1771231200Smm 1772231200Smm if (at > 0 || at_nsec > 0) { 1773231200Smm wintm.QuadPart = WINTIME(at, at_nsec); 1774231200Smm fatime.dwLowDateTime = wintm.LowPart; 1775231200Smm fatime.dwHighDateTime = wintm.HighPart; 1776231200Smm pat = &fatime; 1777231200Smm } else 1778231200Smm pat = NULL; 1779231200Smm if (mt > 0 || mt_nsec > 0) { 1780231200Smm wintm.QuadPart = WINTIME(mt, mt_nsec); 1781231200Smm fmtime.dwLowDateTime = wintm.LowPart; 1782231200Smm fmtime.dwHighDateTime = wintm.HighPart; 1783231200Smm pmt = &fmtime; 1784231200Smm } else 1785231200Smm pmt = NULL; 1786231200Smm if (pat != NULL || pmt != NULL) 1787231200Smm r = SetFileTime(h, NULL, pat, pmt); 1788231200Smm else 1789231200Smm r = 1; 1790231200Smm CloseHandle(h); 1791231200Smm if (r == 0) { 1792231200Smm failure_start(file, line, "Can't SetFileTime %s\n", pathname); 1793231200Smm failure_finish(NULL); 1794231200Smm return (0); 1795231200Smm } 1796231200Smm return (1); 1797231200Smm#else /* defined(_WIN32) && !defined(__CYGWIN__) */ 1798231200Smm struct stat st; 1799231200Smm struct timeval times[2]; 1800231200Smm 1801231200Smm#if !defined(__FreeBSD__) 1802231200Smm mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */ 1803231200Smm#endif 1804231200Smm if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0) 1805231200Smm return (1); 1806231200Smm 1807231200Smm r = lstat(pathname, &st); 1808231200Smm if (r < 0) { 1809231200Smm failure_start(file, line, "Can't stat %s\n", pathname); 1810231200Smm failure_finish(NULL); 1811231200Smm return (0); 1812231200Smm } 1813231200Smm 1814231200Smm if (mt == 0 && mt_nsec == 0) { 1815231200Smm mt = st.st_mtime; 1816231200Smm#if defined(__FreeBSD__) 1817231200Smm mt_nsec = st.st_mtimespec.tv_nsec; 1818231200Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1819231200Smm mt_nsec = (mt_nsec / 1000) * 1000; 1820231200Smm#endif 1821231200Smm } 1822231200Smm if (at == 0 && at_nsec == 0) { 1823231200Smm at = st.st_atime; 1824231200Smm#if defined(__FreeBSD__) 1825231200Smm at_nsec = st.st_atimespec.tv_nsec; 1826231200Smm /* FreeBSD generally only stores to microsecond res, so round. */ 1827231200Smm at_nsec = (at_nsec / 1000) * 1000; 1828231200Smm#endif 1829231200Smm } 1830231200Smm 1831231200Smm times[1].tv_sec = mt; 1832231200Smm times[1].tv_usec = mt_nsec / 1000; 1833231200Smm 1834231200Smm times[0].tv_sec = at; 1835231200Smm times[0].tv_usec = at_nsec / 1000; 1836231200Smm 1837231200Smm#ifdef HAVE_LUTIMES 1838231200Smm r = lutimes(pathname, times); 1839231200Smm#else 1840231200Smm r = utimes(pathname, times); 1841231200Smm#endif 1842231200Smm if (r < 0) { 1843231200Smm failure_start(file, line, "Can't utimes %s\n", pathname); 1844231200Smm failure_finish(NULL); 1845231200Smm return (0); 1846231200Smm } 1847231200Smm return (1); 1848231200Smm#endif /* defined(_WIN32) && !defined(__CYGWIN__) */ 1849231200Smm} 1850231200Smm 1851238825Smm/* Set nodump, report failures. */ 1852238825Smmint 1853238825Smmassertion_nodump(const char *file, int line, const char *pathname) 1854238825Smm{ 1855238825Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 1856238825Smm int r; 1857238825Smm 1858238825Smm assertion_count(file, line); 1859238825Smm r = chflags(pathname, UF_NODUMP); 1860238825Smm if (r < 0) { 1861238825Smm failure_start(file, line, "Can't set nodump %s\n", pathname); 1862238825Smm failure_finish(NULL); 1863238825Smm return (0); 1864238825Smm } 1865238825Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\ 1866238825Smm && defined(EXT2_NODUMP_FL) 1867238825Smm int fd, r, flags; 1868238825Smm 1869238825Smm assertion_count(file, line); 1870238825Smm fd = open(pathname, O_RDONLY | O_NONBLOCK); 1871238825Smm if (fd < 0) { 1872238825Smm failure_start(file, line, "Can't open %s\n", pathname); 1873238825Smm failure_finish(NULL); 1874238825Smm return (0); 1875238825Smm } 1876238825Smm r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags); 1877238825Smm if (r < 0) { 1878238825Smm failure_start(file, line, "Can't get flags %s\n", pathname); 1879238825Smm failure_finish(NULL); 1880238825Smm return (0); 1881238825Smm } 1882238825Smm flags |= EXT2_NODUMP_FL; 1883238825Smm r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags); 1884238825Smm if (r < 0) { 1885238825Smm failure_start(file, line, "Can't set nodump %s\n", pathname); 1886238825Smm failure_finish(NULL); 1887238825Smm return (0); 1888238825Smm } 1889238825Smm close(fd); 1890238825Smm#else 1891238825Smm (void)pathname; /* UNUSED */ 1892238825Smm assertion_count(file, line); 1893238825Smm#endif 1894238825Smm return (1); 1895238825Smm} 1896238825Smm 1897228753Smm/* 1898228753Smm * 1899228753Smm * UTILITIES for use by tests. 1900228753Smm * 1901228753Smm */ 1902228753Smm 1903228753Smm/* 1904228753Smm * Check whether platform supports symlinks. This is intended 1905228753Smm * for tests to use in deciding whether to bother testing symlink 1906228753Smm * support; if the platform doesn't support symlinks, there's no point 1907228753Smm * in checking whether the program being tested can create them. 1908228753Smm * 1909228753Smm * Note that the first time this test is called, we actually go out to 1910228753Smm * disk to create and verify a symlink. This is necessary because 1911228753Smm * symlink support is actually a property of a particular filesystem 1912228753Smm * and can thus vary between directories on a single system. After 1913228753Smm * the first call, this returns the cached result from memory, so it's 1914228753Smm * safe to call it as often as you wish. 1915228753Smm */ 1916228753Smmint 1917228753SmmcanSymlink(void) 1918228753Smm{ 1919228753Smm /* Remember the test result */ 1920228753Smm static int value = 0, tested = 0; 1921228753Smm if (tested) 1922228753Smm return (value); 1923228753Smm 1924228753Smm ++tested; 1925238825Smm assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a"); 1926228753Smm /* Note: Cygwin has its own symlink() emulation that does not 1927228753Smm * use the Win32 CreateSymbolicLink() function. */ 1928228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1929228753Smm value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0) 1930228753Smm && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0"); 1931228753Smm#elif HAVE_SYMLINK 1932228753Smm value = (0 == symlink("canSymlink.0", "canSymlink.1")) 1933228753Smm && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0"); 1934228753Smm#endif 1935228753Smm return (value); 1936228753Smm} 1937228753Smm 1938228753Smm/* Platform-dependent options for hiding the output of a subcommand. */ 1939228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1940228753Smmstatic const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */ 1941228753Smm#else 1942228753Smmstatic const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */ 1943228753Smm#endif 1944248590Smm/* 1945248590Smm * Can this platform run the bzip2 program? 1946248590Smm */ 1947228753Smmint 1948248590SmmcanBzip2(void) 1949248590Smm{ 1950248590Smm static int tested = 0, value = 0; 1951248590Smm if (!tested) { 1952248590Smm tested = 1; 1953248590Smm if (systemf("bzip2 -d -V %s", redirectArgs) == 0) 1954248590Smm value = 1; 1955248590Smm } 1956248590Smm return (value); 1957248590Smm} 1958248590Smm 1959248590Smm/* 1960248590Smm * Can this platform run the grzip program? 1961248590Smm */ 1962248590Smmint 1963248590SmmcanGrzip(void) 1964248590Smm{ 1965248590Smm static int tested = 0, value = 0; 1966248590Smm if (!tested) { 1967248590Smm tested = 1; 1968248590Smm if (systemf("grzip -V %s", redirectArgs) == 0) 1969248590Smm value = 1; 1970248590Smm } 1971248590Smm return (value); 1972248590Smm} 1973248590Smm 1974248590Smm/* 1975248590Smm * Can this platform run the gzip program? 1976248590Smm */ 1977248590Smmint 1978228753SmmcanGzip(void) 1979228753Smm{ 1980228753Smm static int tested = 0, value = 0; 1981228753Smm if (!tested) { 1982228753Smm tested = 1; 1983228753Smm if (systemf("gzip -V %s", redirectArgs) == 0) 1984228753Smm value = 1; 1985228753Smm } 1986228753Smm return (value); 1987228753Smm} 1988228753Smm 1989228753Smm/* 1990248590Smm * Can this platform run the lrzip program? 1991228753Smm */ 1992228753Smmint 1993248590SmmcanRunCommand(const char *cmd) 1994228753Smm{ 1995248590Smm static int tested = 0, value = 0; 1996248590Smm if (!tested) { 1997248590Smm tested = 1; 1998248590Smm if (systemf("%s %s", cmd, redirectArgs) == 0) 1999248590Smm value = 1; 2000248590Smm } 2001248590Smm return (value); 2002248590Smm} 2003248590Smm 2004248590Smmint 2005248590SmmcanLrzip(void) 2006248590Smm{ 2007228753Smm static int tested = 0, value = 0; 2008228753Smm if (!tested) { 2009228753Smm tested = 1; 2010248590Smm if (systemf("lrzip -V %s", redirectArgs) == 0) 2011228753Smm value = 1; 2012228753Smm } 2013228753Smm return (value); 2014228753Smm} 2015228753Smm 2016228753Smm/* 2017299425Smm * Can this platform run the lz4 program? 2018299425Smm */ 2019299425Smmint 2020299425SmmcanLz4(void) 2021299425Smm{ 2022299425Smm static int tested = 0, value = 0; 2023299425Smm if (!tested) { 2024299425Smm tested = 1; 2025299425Smm if (systemf("lz4 -V %s", redirectArgs) == 0) 2026299425Smm value = 1; 2027299425Smm } 2028299425Smm return (value); 2029299425Smm} 2030299425Smm 2031299425Smm/* 2032248590Smm * Can this platform run the lzip program? 2033248590Smm */ 2034248590Smmint 2035248590SmmcanLzip(void) 2036248590Smm{ 2037248590Smm static int tested = 0, value = 0; 2038248590Smm if (!tested) { 2039248590Smm tested = 1; 2040248590Smm if (systemf("lzip -V %s", redirectArgs) == 0) 2041248590Smm value = 1; 2042248590Smm } 2043248590Smm return (value); 2044248590Smm} 2045248590Smm 2046248590Smm/* 2047248590Smm * Can this platform run the lzma program? 2048248590Smm */ 2049248590Smmint 2050248590SmmcanLzma(void) 2051248590Smm{ 2052248590Smm static int tested = 0, value = 0; 2053248590Smm if (!tested) { 2054248590Smm tested = 1; 2055248590Smm if (systemf("lzma -V %s", redirectArgs) == 0) 2056248590Smm value = 1; 2057248590Smm } 2058248590Smm return (value); 2059248590Smm} 2060248590Smm 2061248590Smm/* 2062248590Smm * Can this platform run the lzop program? 2063248590Smm */ 2064248590Smmint 2065248590SmmcanLzop(void) 2066248590Smm{ 2067248590Smm static int tested = 0, value = 0; 2068248590Smm if (!tested) { 2069248590Smm tested = 1; 2070248590Smm if (systemf("lzop -V %s", redirectArgs) == 0) 2071248590Smm value = 1; 2072248590Smm } 2073248590Smm return (value); 2074248590Smm} 2075248590Smm 2076248590Smm/* 2077248590Smm * Can this platform run the xz program? 2078248590Smm */ 2079248590Smmint 2080248590SmmcanXz(void) 2081248590Smm{ 2082248590Smm static int tested = 0, value = 0; 2083248590Smm if (!tested) { 2084248590Smm tested = 1; 2085248590Smm if (systemf("xz -V %s", redirectArgs) == 0) 2086248590Smm value = 1; 2087248590Smm } 2088248590Smm return (value); 2089248590Smm} 2090248590Smm 2091248590Smm/* 2092238825Smm * Can this filesystem handle nodump flags. 2093238825Smm */ 2094238825Smm#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2095238825Smm 2096238825Smmint 2097238825SmmcanNodump(void) 2098238825Smm{ 2099238825Smm const char *path = "cannodumptest"; 2100238825Smm struct stat sb; 2101238825Smm 2102238825Smm assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2103238825Smm if (chflags(path, UF_NODUMP) < 0) 2104238825Smm return (0); 2105238825Smm if (stat(path, &sb) < 0) 2106238825Smm return (0); 2107238825Smm if (sb.st_flags & UF_NODUMP) 2108238825Smm return (1); 2109238825Smm return (0); 2110238825Smm} 2111238825Smm 2112238825Smm#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\ 2113238825Smm && defined(EXT2_NODUMP_FL) 2114238825Smm 2115238825Smmint 2116238825SmmcanNodump(void) 2117238825Smm{ 2118238825Smm const char *path = "cannodumptest"; 2119238825Smm int fd, r, flags; 2120238825Smm 2121238825Smm assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2122238825Smm fd = open(path, O_RDONLY | O_NONBLOCK); 2123238825Smm if (fd < 0) 2124238825Smm return (0); 2125238825Smm r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags); 2126238825Smm if (r < 0) 2127238825Smm return (0); 2128238825Smm flags |= EXT2_NODUMP_FL; 2129238825Smm r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags); 2130238825Smm if (r < 0) 2131238825Smm return (0); 2132238825Smm close(fd); 2133238825Smm fd = open(path, O_RDONLY | O_NONBLOCK); 2134238825Smm if (fd < 0) 2135238825Smm return (0); 2136238825Smm r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags); 2137238825Smm if (r < 0) 2138238825Smm return (0); 2139238825Smm close(fd); 2140238825Smm if (flags & EXT2_NODUMP_FL) 2141238825Smm return (1); 2142238825Smm return (0); 2143238825Smm} 2144238825Smm 2145238825Smm#else 2146238825Smm 2147238825Smmint 2148238825SmmcanNodump() 2149238825Smm{ 2150238825Smm return (0); 2151238825Smm} 2152238825Smm 2153238825Smm#endif 2154238825Smm 2155238825Smm/* 2156228753Smm * Sleep as needed; useful for verifying disk timestamp changes by 2157228753Smm * ensuring that the wall-clock time has actually changed before we 2158228753Smm * go back to re-read something from disk. 2159228753Smm */ 2160228753Smmvoid 2161228753SmmsleepUntilAfter(time_t t) 2162228753Smm{ 2163228753Smm while (t >= time(NULL)) 2164228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2165228753Smm Sleep(500); 2166228753Smm#else 2167228753Smm sleep(1); 2168228753Smm#endif 2169228753Smm} 2170228753Smm 2171228753Smm/* 2172228753Smm * Call standard system() call, but build up the command line using 2173228753Smm * sprintf() conventions. 2174228753Smm */ 2175228753Smmint 2176228753Smmsystemf(const char *fmt, ...) 2177228753Smm{ 2178228753Smm char buff[8192]; 2179228753Smm va_list ap; 2180228753Smm int r; 2181228753Smm 2182228753Smm va_start(ap, fmt); 2183228753Smm vsprintf(buff, fmt, ap); 2184228753Smm if (verbosity > VERBOSITY_FULL) 2185228753Smm logprintf("Cmd: %s\n", buff); 2186228753Smm r = system(buff); 2187228753Smm va_end(ap); 2188228753Smm return (r); 2189228753Smm} 2190228753Smm 2191228753Smm/* 2192228753Smm * Slurp a file into memory for ease of comparison and testing. 2193228753Smm * Returns size of file in 'sizep' if non-NULL, null-terminates 2194228753Smm * data in memory for ease of use. 2195228753Smm */ 2196228753Smmchar * 2197228753Smmslurpfile(size_t * sizep, const char *fmt, ...) 2198228753Smm{ 2199228753Smm char filename[8192]; 2200228753Smm struct stat st; 2201228753Smm va_list ap; 2202228753Smm char *p; 2203228753Smm ssize_t bytes_read; 2204228753Smm FILE *f; 2205228753Smm int r; 2206228753Smm 2207228753Smm va_start(ap, fmt); 2208228753Smm vsprintf(filename, fmt, ap); 2209228753Smm va_end(ap); 2210228753Smm 2211228753Smm f = fopen(filename, "rb"); 2212228753Smm if (f == NULL) { 2213228753Smm /* Note: No error; non-existent file is okay here. */ 2214228753Smm return (NULL); 2215228753Smm } 2216228753Smm r = fstat(fileno(f), &st); 2217228753Smm if (r != 0) { 2218228753Smm logprintf("Can't stat file %s\n", filename); 2219228753Smm fclose(f); 2220228753Smm return (NULL); 2221228753Smm } 2222228753Smm p = malloc((size_t)st.st_size + 1); 2223228753Smm if (p == NULL) { 2224228753Smm logprintf("Can't allocate %ld bytes of memory to read file %s\n", 2225228753Smm (long int)st.st_size, filename); 2226228753Smm fclose(f); 2227228753Smm return (NULL); 2228228753Smm } 2229228753Smm bytes_read = fread(p, 1, (size_t)st.st_size, f); 2230228753Smm if (bytes_read < st.st_size) { 2231228753Smm logprintf("Can't read file %s\n", filename); 2232228753Smm fclose(f); 2233228753Smm free(p); 2234228753Smm return (NULL); 2235228753Smm } 2236228753Smm p[st.st_size] = '\0'; 2237228753Smm if (sizep != NULL) 2238228753Smm *sizep = (size_t)st.st_size; 2239228753Smm fclose(f); 2240228753Smm return (p); 2241228753Smm} 2242228753Smm 2243299425Smm/* 2244299425Smm * Slurp a file into memory for ease of comparison and testing. 2245299425Smm * Returns size of file in 'sizep' if non-NULL, null-terminates 2246299425Smm * data in memory for ease of use. 2247299425Smm */ 2248299425Smmvoid 2249299425Smmdumpfile(const char *filename, void *data, size_t len) 2250299425Smm{ 2251299425Smm ssize_t bytes_written; 2252299425Smm FILE *f; 2253299425Smm 2254299425Smm f = fopen(filename, "wb"); 2255299425Smm if (f == NULL) { 2256299425Smm logprintf("Can't open file %s for writing\n", filename); 2257299425Smm return; 2258299425Smm } 2259299425Smm bytes_written = fwrite(data, 1, len, f); 2260299425Smm if (bytes_written < (ssize_t)len) 2261299425Smm logprintf("Can't write file %s\n", filename); 2262299425Smm fclose(f); 2263299425Smm} 2264299425Smm 2265228753Smm/* Read a uuencoded file from the reference directory, decode, and 2266228753Smm * write the result into the current directory. */ 2267299425Smm#define VALID_UUDECODE(c) (c >= 32 && c <= 96) 2268228753Smm#define UUDECODE(c) (((c) - 0x20) & 0x3f) 2269228753Smmvoid 2270228753Smmextract_reference_file(const char *name) 2271228753Smm{ 2272228753Smm char buff[1024]; 2273228753Smm FILE *in, *out; 2274228753Smm 2275228753Smm sprintf(buff, "%s/%s.uu", refdir, name); 2276228753Smm in = fopen(buff, "r"); 2277228753Smm failure("Couldn't open reference file %s", buff); 2278228753Smm assert(in != NULL); 2279228753Smm if (in == NULL) 2280228753Smm return; 2281228753Smm /* Read up to and including the 'begin' line. */ 2282228753Smm for (;;) { 2283228753Smm if (fgets(buff, sizeof(buff), in) == NULL) { 2284228753Smm /* TODO: This is a failure. */ 2285228753Smm return; 2286228753Smm } 2287228753Smm if (memcmp(buff, "begin ", 6) == 0) 2288228753Smm break; 2289228753Smm } 2290228753Smm /* Now, decode the rest and write it. */ 2291228753Smm out = fopen(name, "wb"); 2292228753Smm while (fgets(buff, sizeof(buff), in) != NULL) { 2293228753Smm char *p = buff; 2294228753Smm int bytes; 2295228753Smm 2296228753Smm if (memcmp(buff, "end", 3) == 0) 2297228753Smm break; 2298228753Smm 2299228753Smm bytes = UUDECODE(*p++); 2300228753Smm while (bytes > 0) { 2301228753Smm int n = 0; 2302228753Smm /* Write out 1-3 bytes from that. */ 2303228753Smm if (bytes > 0) { 2304299425Smm assert(VALID_UUDECODE(p[0])); 2305299425Smm assert(VALID_UUDECODE(p[1])); 2306228753Smm n = UUDECODE(*p++) << 18; 2307228753Smm n |= UUDECODE(*p++) << 12; 2308228753Smm fputc(n >> 16, out); 2309228753Smm --bytes; 2310228753Smm } 2311228753Smm if (bytes > 0) { 2312299425Smm assert(VALID_UUDECODE(p[0])); 2313228753Smm n |= UUDECODE(*p++) << 6; 2314228753Smm fputc((n >> 8) & 0xFF, out); 2315228753Smm --bytes; 2316228753Smm } 2317228753Smm if (bytes > 0) { 2318299425Smm assert(VALID_UUDECODE(p[0])); 2319228753Smm n |= UUDECODE(*p++); 2320228753Smm fputc(n & 0xFF, out); 2321228753Smm --bytes; 2322228753Smm } 2323228753Smm } 2324228753Smm } 2325228753Smm fclose(out); 2326228753Smm fclose(in); 2327228753Smm} 2328228753Smm 2329299425Smmvoid 2330299425Smmcopy_reference_file(const char *name) 2331299425Smm{ 2332299425Smm char buff[1024]; 2333299425Smm FILE *in, *out; 2334299425Smm size_t rbytes; 2335299425Smm 2336299425Smm sprintf(buff, "%s/%s", refdir, name); 2337299425Smm in = fopen(buff, "rb"); 2338299425Smm failure("Couldn't open reference file %s", buff); 2339299425Smm assert(in != NULL); 2340299425Smm if (in == NULL) 2341299425Smm return; 2342299425Smm /* Now, decode the rest and write it. */ 2343299425Smm /* Not a lot of error checking here; the input better be right. */ 2344299425Smm out = fopen(name, "wb"); 2345299425Smm while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) { 2346299425Smm if (fwrite(buff, 1, rbytes, out) != rbytes) { 2347299425Smm logprintf("Error: fwrite\n"); 2348299425Smm break; 2349299425Smm } 2350299425Smm } 2351299425Smm fclose(out); 2352299425Smm fclose(in); 2353299425Smm} 2354299425Smm 2355231200Smmint 2356231200Smmis_LargeInode(const char *file) 2357231200Smm{ 2358231200Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2359231200Smm BY_HANDLE_FILE_INFORMATION bhfi; 2360231200Smm int r; 2361231200Smm 2362231200Smm r = my_GetFileInformationByName(file, &bhfi); 2363231200Smm if (r != 0) 2364231200Smm return (0); 2365231200Smm return (bhfi.nFileIndexHigh & 0x0000FFFFUL); 2366231200Smm#else 2367231200Smm struct stat st; 2368231200Smm int64_t ino; 2369231200Smm 2370231200Smm if (stat(file, &st) < 0) 2371231200Smm return (0); 2372231200Smm ino = (int64_t)st.st_ino; 2373231200Smm return (ino > 0xffffffff); 2374231200Smm#endif 2375231200Smm} 2376248590Smm 2377248590Smmvoid 2378248590Smmextract_reference_files(const char **names) 2379248590Smm{ 2380248590Smm while (names && *names) 2381248590Smm extract_reference_file(*names++); 2382248590Smm} 2383248590Smm 2384228753Smm/* 2385228753Smm * 2386228753Smm * TEST management 2387228753Smm * 2388228753Smm */ 2389228753Smm 2390228753Smm/* 2391228753Smm * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has 2392228753Smm * a line like 2393228753Smm * DEFINE_TEST(test_function) 2394228753Smm * for each test. 2395228753Smm */ 2396228753Smm 2397228753Smm/* Use "list.h" to declare all of the test functions. */ 2398228753Smm#undef DEFINE_TEST 2399228753Smm#define DEFINE_TEST(name) void name(void); 2400228753Smm#include "list.h" 2401228753Smm 2402228753Smm/* Use "list.h" to create a list of all tests (functions and names). */ 2403228753Smm#undef DEFINE_TEST 2404228753Smm#define DEFINE_TEST(n) { n, #n, 0 }, 2405248590Smmstruct test_list_t tests[] = { 2406228753Smm #include "list.h" 2407228753Smm}; 2408228753Smm 2409228753Smm/* 2410228753Smm * Summarize repeated failures in the just-completed test. 2411228753Smm */ 2412228753Smmstatic void 2413299425Smmtest_summarize(int failed, int skips_num) 2414228753Smm{ 2415228753Smm unsigned int i; 2416228753Smm 2417228753Smm switch (verbosity) { 2418228753Smm case VERBOSITY_SUMMARY_ONLY: 2419228753Smm printf(failed ? "E" : "."); 2420228753Smm fflush(stdout); 2421228753Smm break; 2422228753Smm case VERBOSITY_PASSFAIL: 2423299425Smm printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n"); 2424228753Smm break; 2425228753Smm } 2426228753Smm 2427228753Smm log_console = (verbosity == VERBOSITY_LIGHT_REPORT); 2428228753Smm 2429228753Smm for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { 2430228753Smm if (failed_lines[i].count > 1 && !failed_lines[i].skip) 2431228753Smm logprintf("%s:%d: Summary: Failed %d times\n", 2432231200Smm failed_filename, i, failed_lines[i].count); 2433228753Smm } 2434228753Smm /* Clear the failure history for the next file. */ 2435231200Smm failed_filename = NULL; 2436228753Smm memset(failed_lines, 0, sizeof(failed_lines)); 2437228753Smm} 2438228753Smm 2439228753Smm/* 2440228753Smm * Actually run a single test, with appropriate setup and cleanup. 2441228753Smm */ 2442228753Smmstatic int 2443228753Smmtest_run(int i, const char *tmpdir) 2444228753Smm{ 2445231200Smm char workdir[1024]; 2446228753Smm char logfilename[64]; 2447228753Smm int failures_before = failures; 2448299425Smm int skips_before = skips; 2449228753Smm int oldumask; 2450228753Smm 2451228753Smm switch (verbosity) { 2452228753Smm case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */ 2453228753Smm break; 2454228753Smm case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */ 2455299425Smm printf("%3d: %-64s", i, tests[i].name); 2456228753Smm fflush(stdout); 2457228753Smm break; 2458228753Smm default: /* Title of test, details will follow */ 2459228753Smm printf("%3d: %s\n", i, tests[i].name); 2460228753Smm } 2461228753Smm 2462228753Smm /* Chdir to the top-level work directory. */ 2463228753Smm if (!assertChdir(tmpdir)) { 2464228753Smm fprintf(stderr, 2465228753Smm "ERROR: Can't chdir to top work dir %s\n", tmpdir); 2466228753Smm exit(1); 2467228753Smm } 2468228753Smm /* Create a log file for this test. */ 2469228753Smm sprintf(logfilename, "%s.log", tests[i].name); 2470228753Smm logfile = fopen(logfilename, "w"); 2471228753Smm fprintf(logfile, "%s\n\n", tests[i].name); 2472228753Smm /* Chdir() to a work dir for this specific test. */ 2473231200Smm snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name); 2474231200Smm testworkdir = workdir; 2475231200Smm if (!assertMakeDir(testworkdir, 0755) 2476231200Smm || !assertChdir(testworkdir)) { 2477228753Smm fprintf(stderr, 2478231200Smm "ERROR: Can't chdir to work dir %s\n", testworkdir); 2479228753Smm exit(1); 2480228753Smm } 2481228753Smm /* Explicitly reset the locale before each test. */ 2482228753Smm setlocale(LC_ALL, "C"); 2483228753Smm /* Record the umask before we run the test. */ 2484228753Smm umask(oldumask = umask(0)); 2485228753Smm /* 2486228753Smm * Run the actual test. 2487228753Smm */ 2488228753Smm (*tests[i].func)(); 2489228753Smm /* 2490228753Smm * Clean up and report afterwards. 2491228753Smm */ 2492231200Smm testworkdir = NULL; 2493228753Smm /* Restore umask */ 2494228753Smm umask(oldumask); 2495228753Smm /* Reset locale. */ 2496228753Smm setlocale(LC_ALL, "C"); 2497228753Smm /* Reset directory. */ 2498228753Smm if (!assertChdir(tmpdir)) { 2499228753Smm fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n", 2500228753Smm tmpdir); 2501228753Smm exit(1); 2502228753Smm } 2503228753Smm /* Report per-test summaries. */ 2504228753Smm tests[i].failures = failures - failures_before; 2505299425Smm test_summarize(tests[i].failures, skips - skips_before); 2506228753Smm /* Close the per-test log file. */ 2507228753Smm fclose(logfile); 2508228753Smm logfile = NULL; 2509228753Smm /* If there were no failures, we can remove the work dir and logfile. */ 2510228753Smm if (tests[i].failures == 0) { 2511228753Smm if (!keep_temp_files && assertChdir(tmpdir)) { 2512228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2513228753Smm /* Make sure not to leave empty directories. 2514228753Smm * Sometimes a processing of closing files used by tests 2515228753Smm * is not done, then rmdir will be failed and it will 2516228753Smm * leave a empty test directory. So we should wait a few 2517228753Smm * seconds and retry rmdir. */ 2518228753Smm int r, t; 2519228753Smm for (t = 0; t < 10; t++) { 2520228753Smm if (t > 0) 2521228753Smm Sleep(1000); 2522228753Smm r = systemf("rmdir /S /Q %s", tests[i].name); 2523228753Smm if (r == 0) 2524228753Smm break; 2525228753Smm } 2526228753Smm systemf("del %s", logfilename); 2527228753Smm#else 2528228753Smm systemf("rm -rf %s", tests[i].name); 2529228753Smm systemf("rm %s", logfilename); 2530228753Smm#endif 2531228753Smm } 2532228753Smm } 2533228753Smm /* Return appropriate status. */ 2534228753Smm return (tests[i].failures); 2535228753Smm} 2536228753Smm 2537228753Smm/* 2538228753Smm * 2539228753Smm * 2540228753Smm * MAIN and support routines. 2541228753Smm * 2542228753Smm * 2543228753Smm */ 2544228753Smm 2545228753Smmstatic void 2546228753Smmusage(const char *program) 2547228753Smm{ 2548228753Smm static const int limit = sizeof(tests) / sizeof(tests[0]); 2549228753Smm int i; 2550228753Smm 2551228753Smm printf("Usage: %s [options] <test> <test> ...\n", program); 2552228753Smm printf("Default is to run all tests.\n"); 2553228753Smm printf("Otherwise, specify the numbers of the tests you wish to run.\n"); 2554228753Smm printf("Options:\n"); 2555228753Smm printf(" -d Dump core after any failure, for debugging.\n"); 2556228753Smm printf(" -k Keep all temp files.\n"); 2557228753Smm printf(" Default: temp files for successful tests deleted.\n"); 2558228753Smm#ifdef PROGRAM 2559228753Smm printf(" -p <path> Path to executable to be tested.\n"); 2560228753Smm printf(" Default: path taken from " ENVBASE " environment variable.\n"); 2561228753Smm#endif 2562228753Smm printf(" -q Quiet.\n"); 2563228753Smm printf(" -r <dir> Path to dir containing reference files.\n"); 2564228753Smm printf(" Default: Current directory.\n"); 2565231200Smm printf(" -u Keep running specifies tests until one fails.\n"); 2566228753Smm printf(" -v Verbose.\n"); 2567228753Smm printf("Available tests:\n"); 2568228753Smm for (i = 0; i < limit; i++) 2569228753Smm printf(" %d: %s\n", i, tests[i].name); 2570228753Smm exit(1); 2571228753Smm} 2572228753Smm 2573228753Smmstatic char * 2574228753Smmget_refdir(const char *d) 2575228753Smm{ 2576302003Smm size_t tried_size, buff_size; 2577302003Smm char *buff, *tried, *pwd = NULL, *p = NULL; 2578228753Smm 2579302003Smm#ifdef PATH_MAX 2580302003Smm buff_size = PATH_MAX; 2581302003Smm#else 2582302003Smm buff_size = 8192; 2583302003Smm#endif 2584302003Smm buff = calloc(buff_size, 1); 2585302003Smm if (buff == NULL) { 2586302003Smm fprintf(stderr, "Unable to allocate memory\n"); 2587302003Smm exit(1); 2588302003Smm } 2589302003Smm 2590302003Smm /* Allocate a buffer to hold the various directories we checked. */ 2591302003Smm tried_size = buff_size * 2; 2592302003Smm tried = calloc(tried_size, 1); 2593302003Smm if (tried == NULL) { 2594302003Smm fprintf(stderr, "Unable to allocate memory\n"); 2595302003Smm exit(1); 2596302003Smm } 2597302003Smm 2598228753Smm /* If a dir was specified, try that */ 2599228753Smm if (d != NULL) { 2600228753Smm pwd = NULL; 2601302003Smm snprintf(buff, buff_size, "%s", d); 2602228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2603228753Smm if (p != NULL) goto success; 2604302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 2605302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 2606228753Smm goto failure; 2607228753Smm } 2608228753Smm 2609228753Smm /* Get the current dir. */ 2610231200Smm#ifdef PATH_MAX 2611231200Smm pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 2612231200Smm#else 2613228753Smm pwd = getcwd(NULL, 0); 2614231200Smm#endif 2615228753Smm while (pwd[strlen(pwd) - 1] == '\n') 2616228753Smm pwd[strlen(pwd) - 1] = '\0'; 2617228753Smm 2618228753Smm /* Look for a known file. */ 2619302003Smm snprintf(buff, buff_size, "%s", pwd); 2620228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2621228753Smm if (p != NULL) goto success; 2622302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 2623302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 2624228753Smm 2625302003Smm snprintf(buff, buff_size, "%s/test", pwd); 2626228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2627228753Smm if (p != NULL) goto success; 2628302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 2629302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 2630228753Smm 2631228753Smm#if defined(LIBRARY) 2632302003Smm snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY); 2633228753Smm#else 2634302003Smm snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM); 2635228753Smm#endif 2636228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2637228753Smm if (p != NULL) goto success; 2638302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 2639302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 2640228753Smm 2641231200Smm#if defined(PROGRAM_ALIAS) 2642302003Smm snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS); 2643231200Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2644231200Smm if (p != NULL) goto success; 2645302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 2646302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 2647231200Smm#endif 2648231200Smm 2649228753Smm if (memcmp(pwd, "/usr/obj", 8) == 0) { 2650302003Smm snprintf(buff, buff_size, "%s", pwd + 8); 2651228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2652228753Smm if (p != NULL) goto success; 2653302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 2654302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 2655228753Smm 2656302003Smm snprintf(buff, buff_size, "%s/test", pwd + 8); 2657228753Smm p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2658228753Smm if (p != NULL) goto success; 2659302003Smm strncat(tried, buff, tried_size - strlen(tried) - 1); 2660302003Smm strncat(tried, "\n", tried_size - strlen(tried) - 1); 2661228753Smm } 2662228753Smm 2663228753Smmfailure: 2664228753Smm printf("Unable to locate known reference file %s\n", KNOWNREF); 2665228753Smm printf(" Checked following directories:\n%s\n", tried); 2666299425Smm printf("Use -r option to specify full path to reference directory\n"); 2667228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) 2668228753Smm DebugBreak(); 2669228753Smm#endif 2670228753Smm exit(1); 2671228753Smm 2672228753Smmsuccess: 2673228753Smm free(p); 2674228753Smm free(pwd); 2675302003Smm free(tried); 2676302003Smm 2677302003Smm /* Copy result into a fresh buffer to reduce memory usage. */ 2678302003Smm p = strdup(buff); 2679302003Smm free(buff); 2680302003Smm return p; 2681228753Smm} 2682228753Smm 2683228753Smmint 2684228753Smmmain(int argc, char **argv) 2685228753Smm{ 2686228753Smm static const int limit = sizeof(tests) / sizeof(tests[0]); 2687238825Smm int test_set[sizeof(tests) / sizeof(tests[0])]; 2688238825Smm int i = 0, j = 0, tests_run = 0, tests_failed = 0, option; 2689228753Smm time_t now; 2690228753Smm char *refdir_alloc = NULL; 2691228753Smm const char *progname; 2692231200Smm char **saved_argv; 2693228753Smm const char *tmp, *option_arg, *p; 2694238825Smm char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL; 2695228753Smm char tmpdir_timestamp[256]; 2696228753Smm 2697228753Smm (void)argc; /* UNUSED */ 2698228753Smm 2699231200Smm /* Get the current dir. */ 2700231200Smm#ifdef PATH_MAX 2701231200Smm pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 2702231200Smm#else 2703231200Smm pwd = getcwd(NULL, 0); 2704231200Smm#endif 2705231200Smm while (pwd[strlen(pwd) - 1] == '\n') 2706231200Smm pwd[strlen(pwd) - 1] = '\0'; 2707231200Smm 2708299425Smm#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 2709228753Smm /* To stop to run the default invalid parameter handler. */ 2710228753Smm _set_invalid_parameter_handler(invalid_parameter_handler); 2711228753Smm /* Disable annoying assertion message box. */ 2712228753Smm _CrtSetReportMode(_CRT_ASSERT, 0); 2713228753Smm#endif 2714228753Smm 2715228753Smm /* 2716228753Smm * Name of this program, used to build root of our temp directory 2717228753Smm * tree. 2718228753Smm */ 2719228753Smm progname = p = argv[0]; 2720231200Smm if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL) 2721231200Smm { 2722231200Smm fprintf(stderr, "ERROR: Out of memory."); 2723231200Smm exit(1); 2724231200Smm } 2725231200Smm strcpy(testprogdir, progname); 2726228753Smm while (*p != '\0') { 2727228753Smm /* Support \ or / dir separators for Windows compat. */ 2728228753Smm if (*p == '/' || *p == '\\') 2729231200Smm { 2730228753Smm progname = p + 1; 2731231200Smm i = j; 2732231200Smm } 2733228753Smm ++p; 2734231200Smm j++; 2735228753Smm } 2736231200Smm testprogdir[i] = '\0'; 2737232133Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2738232133Smm if (testprogdir[0] != '/' && testprogdir[0] != '\\' && 2739232133Smm !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') || 2740232133Smm (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) && 2741232133Smm testprogdir[1] == ':' && 2742232133Smm (testprogdir[2] == '/' || testprogdir[2] == '\\'))) 2743232133Smm#else 2744231200Smm if (testprogdir[0] != '/') 2745232133Smm#endif 2746231200Smm { 2747231200Smm /* Fixup path for relative directories. */ 2748231200Smm if ((testprogdir = (char *)realloc(testprogdir, 2749231200Smm strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL) 2750231200Smm { 2751231200Smm fprintf(stderr, "ERROR: Out of memory."); 2752231200Smm exit(1); 2753231200Smm } 2754232133Smm memmove(testprogdir + strlen(pwd) + 1, testprogdir, 2755299425Smm strlen(testprogdir) + 1); 2756232133Smm memcpy(testprogdir, pwd, strlen(pwd)); 2757231200Smm testprogdir[strlen(pwd)] = '/'; 2758231200Smm } 2759228753Smm 2760228753Smm#ifdef PROGRAM 2761228753Smm /* Get the target program from environment, if available. */ 2762228753Smm testprogfile = getenv(ENVBASE); 2763228753Smm#endif 2764228753Smm 2765228753Smm if (getenv("TMPDIR") != NULL) 2766228753Smm tmp = getenv("TMPDIR"); 2767228753Smm else if (getenv("TMP") != NULL) 2768228753Smm tmp = getenv("TMP"); 2769228753Smm else if (getenv("TEMP") != NULL) 2770228753Smm tmp = getenv("TEMP"); 2771228753Smm else if (getenv("TEMPDIR") != NULL) 2772228753Smm tmp = getenv("TEMPDIR"); 2773228753Smm else 2774228753Smm tmp = "/tmp"; 2775228753Smm 2776228753Smm /* Allow -d to be controlled through the environment. */ 2777228753Smm if (getenv(ENVBASE "_DEBUG") != NULL) 2778228753Smm dump_on_failure = 1; 2779228753Smm 2780238825Smm /* Allow -v to be controlled through the environment. */ 2781238825Smm if (getenv("_VERBOSITY_LEVEL") != NULL) 2782238825Smm { 2783238825Smm vlevel = getenv("_VERBOSITY_LEVEL"); 2784238825Smm verbosity = atoi(vlevel); 2785238825Smm if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL) 2786238825Smm { 2787238825Smm /* Unsupported verbosity levels are silently ignored */ 2788238825Smm vlevel = NULL; 2789238825Smm verbosity = VERBOSITY_PASSFAIL; 2790238825Smm } 2791238825Smm } 2792238825Smm 2793228753Smm /* Get the directory holding test files from environment. */ 2794228753Smm refdir = getenv(ENVBASE "_TEST_FILES"); 2795228753Smm 2796228753Smm /* 2797228753Smm * Parse options, without using getopt(), which isn't available 2798228753Smm * on all platforms. 2799228753Smm */ 2800228753Smm ++argv; /* Skip program name */ 2801228753Smm while (*argv != NULL) { 2802228753Smm if (**argv != '-') 2803228753Smm break; 2804228753Smm p = *argv++; 2805228753Smm ++p; /* Skip '-' */ 2806228753Smm while (*p != '\0') { 2807228753Smm option = *p++; 2808228753Smm option_arg = NULL; 2809228753Smm /* If 'opt' takes an argument, parse that. */ 2810228753Smm if (option == 'p' || option == 'r') { 2811228753Smm if (*p != '\0') 2812228753Smm option_arg = p; 2813228753Smm else if (*argv == NULL) { 2814228753Smm fprintf(stderr, 2815228753Smm "Option -%c requires argument.\n", 2816228753Smm option); 2817228753Smm usage(progname); 2818228753Smm } else 2819228753Smm option_arg = *argv++; 2820228753Smm p = ""; /* End of this option word. */ 2821228753Smm } 2822228753Smm 2823228753Smm /* Now, handle the option. */ 2824228753Smm switch (option) { 2825228753Smm case 'd': 2826228753Smm dump_on_failure = 1; 2827228753Smm break; 2828228753Smm case 'k': 2829228753Smm keep_temp_files = 1; 2830228753Smm break; 2831228753Smm case 'p': 2832228753Smm#ifdef PROGRAM 2833228753Smm testprogfile = option_arg; 2834228753Smm#else 2835231200Smm fprintf(stderr, "-p option not permitted\n"); 2836228753Smm usage(progname); 2837228753Smm#endif 2838228753Smm break; 2839228753Smm case 'q': 2840238825Smm if (!vlevel) 2841238825Smm verbosity--; 2842228753Smm break; 2843228753Smm case 'r': 2844228753Smm refdir = option_arg; 2845228753Smm break; 2846231200Smm case 'u': 2847231200Smm until_failure++; 2848231200Smm break; 2849228753Smm case 'v': 2850238825Smm if (!vlevel) 2851238825Smm verbosity++; 2852228753Smm break; 2853228753Smm default: 2854231200Smm fprintf(stderr, "Unrecognized option '%c'\n", 2855231200Smm option); 2856228753Smm usage(progname); 2857228753Smm } 2858228753Smm } 2859228753Smm } 2860228753Smm 2861228753Smm /* 2862228753Smm * Sanity-check that our options make sense. 2863228753Smm */ 2864228753Smm#ifdef PROGRAM 2865228753Smm if (testprogfile == NULL) 2866228753Smm { 2867231200Smm if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 + 2868231200Smm strlen(PROGRAM) + 1)) == NULL) 2869231200Smm { 2870231200Smm fprintf(stderr, "ERROR: Out of memory."); 2871231200Smm exit(1); 2872231200Smm } 2873231200Smm strcpy(tmp2, testprogdir); 2874231200Smm strcat(tmp2, "/"); 2875231200Smm strcat(tmp2, PROGRAM); 2876231200Smm testprogfile = tmp2; 2877231200Smm } 2878231200Smm 2879231200Smm { 2880228753Smm char *testprg; 2881228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 2882228753Smm /* Command.com sometimes rejects '/' separators. */ 2883228753Smm testprg = strdup(testprogfile); 2884228753Smm for (i = 0; testprg[i] != '\0'; i++) { 2885228753Smm if (testprg[i] == '/') 2886228753Smm testprg[i] = '\\'; 2887228753Smm } 2888228753Smm testprogfile = testprg; 2889228753Smm#endif 2890228753Smm /* Quote the name that gets put into shell command lines. */ 2891228753Smm testprg = malloc(strlen(testprogfile) + 3); 2892228753Smm strcpy(testprg, "\""); 2893228753Smm strcat(testprg, testprogfile); 2894228753Smm strcat(testprg, "\""); 2895228753Smm testprog = testprg; 2896228753Smm } 2897228753Smm#endif 2898228753Smm 2899231200Smm#if !defined(_WIN32) && defined(SIGPIPE) 2900231200Smm { /* Ignore SIGPIPE signals */ 2901231200Smm struct sigaction sa; 2902231200Smm sa.sa_handler = SIG_IGN; 2903231200Smm sigemptyset(&sa.sa_mask); 2904231200Smm sa.sa_flags = 0; 2905231200Smm sigaction(SIGPIPE, &sa, NULL); 2906231200Smm } 2907231200Smm#endif 2908231200Smm 2909228753Smm /* 2910228753Smm * Create a temp directory for the following tests. 2911228753Smm * Include the time the tests started as part of the name, 2912228753Smm * to make it easier to track the results of multiple tests. 2913228753Smm */ 2914228753Smm now = time(NULL); 2915228753Smm for (i = 0; ; i++) { 2916228753Smm strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), 2917228753Smm "%Y-%m-%dT%H.%M.%S", 2918228753Smm localtime(&now)); 2919228753Smm sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname, 2920228753Smm tmpdir_timestamp, i); 2921228753Smm if (assertMakeDir(tmpdir,0755)) 2922228753Smm break; 2923228753Smm if (i >= 999) { 2924228753Smm fprintf(stderr, 2925228753Smm "ERROR: Unable to create temp directory %s\n", 2926228753Smm tmpdir); 2927228753Smm exit(1); 2928228753Smm } 2929228753Smm } 2930228753Smm 2931228753Smm /* 2932228753Smm * If the user didn't specify a directory for locating 2933228753Smm * reference files, try to find the reference files in 2934228753Smm * the "usual places." 2935228753Smm */ 2936228753Smm refdir = refdir_alloc = get_refdir(refdir); 2937228753Smm 2938228753Smm /* 2939228753Smm * Banner with basic information. 2940228753Smm */ 2941228753Smm printf("\n"); 2942228753Smm printf("If tests fail or crash, details will be in:\n"); 2943228753Smm printf(" %s\n", tmpdir); 2944228753Smm printf("\n"); 2945228753Smm if (verbosity > VERBOSITY_SUMMARY_ONLY) { 2946228753Smm printf("Reference files will be read from: %s\n", refdir); 2947228753Smm#ifdef PROGRAM 2948228753Smm printf("Running tests on: %s\n", testprog); 2949228753Smm#endif 2950228753Smm printf("Exercising: "); 2951228753Smm fflush(stdout); 2952228753Smm printf("%s\n", EXTRA_VERSION); 2953228753Smm } else { 2954228753Smm printf("Running "); 2955228753Smm fflush(stdout); 2956228753Smm } 2957228753Smm 2958228753Smm /* 2959228753Smm * Run some or all of the individual tests. 2960228753Smm */ 2961231200Smm saved_argv = argv; 2962231200Smm do { 2963231200Smm argv = saved_argv; 2964238825Smm do { 2965238825Smm int test_num; 2966238825Smm 2967248590Smm test_num = get_test_set(test_set, limit, *argv, tests); 2968238825Smm if (test_num < 0) { 2969238825Smm printf("*** INVALID Test %s\n", *argv); 2970238825Smm free(refdir_alloc); 2971248590Smm free(testprogdir); 2972238825Smm usage(progname); 2973238825Smm return (1); 2974238825Smm } 2975238825Smm for (i = 0; i < test_num; i++) { 2976231200Smm tests_run++; 2977238825Smm if (test_run(test_set[i], tmpdir)) { 2978231200Smm tests_failed++; 2979231200Smm if (until_failure) 2980231200Smm goto finish; 2981228753Smm } 2982231200Smm } 2983238825Smm if (*argv != NULL) 2984231200Smm argv++; 2985238825Smm } while (*argv != NULL); 2986231200Smm } while (until_failure); 2987228753Smm 2988231200Smmfinish: 2989231200Smm /* Must be freed after all tests run */ 2990231200Smm free(tmp2); 2991231200Smm free(testprogdir); 2992231200Smm free(pwd); 2993231200Smm 2994228753Smm /* 2995228753Smm * Report summary statistics. 2996228753Smm */ 2997228753Smm if (verbosity > VERBOSITY_SUMMARY_ONLY) { 2998228753Smm printf("\n"); 2999228753Smm printf("Totals:\n"); 3000228753Smm printf(" Tests run: %8d\n", tests_run); 3001228753Smm printf(" Tests failed: %8d\n", tests_failed); 3002228753Smm printf(" Assertions checked:%8d\n", assertions); 3003228753Smm printf(" Assertions failed: %8d\n", failures); 3004228753Smm printf(" Skips reported: %8d\n", skips); 3005228753Smm } 3006228753Smm if (failures) { 3007228753Smm printf("\n"); 3008228753Smm printf("Failing tests:\n"); 3009228753Smm for (i = 0; i < limit; ++i) { 3010228753Smm if (tests[i].failures) 3011228753Smm printf(" %d: %s (%d failures)\n", i, 3012228753Smm tests[i].name, tests[i].failures); 3013228753Smm } 3014228753Smm printf("\n"); 3015228753Smm printf("Details for failing tests: %s\n", tmpdir); 3016228753Smm printf("\n"); 3017228753Smm } else { 3018228753Smm if (verbosity == VERBOSITY_SUMMARY_ONLY) 3019228753Smm printf("\n"); 3020228753Smm printf("%d tests passed, no failures\n", tests_run); 3021228753Smm } 3022228753Smm 3023228753Smm free(refdir_alloc); 3024228753Smm 3025228753Smm /* If the final tmpdir is empty, we can remove it. */ 3026228753Smm /* This should be the usual case when all tests succeed. */ 3027228753Smm assertChdir(".."); 3028228753Smm rmdir(tmpdir); 3029228753Smm 3030228753Smm return (tests_failed ? 1 : 0); 3031228753Smm} 3032