main.c revision 302408
1/* 2 * Copyright (c) 2003-2009 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "test.h" 27#include "test_utils.h" 28#ifdef HAVE_SYS_IOCTL_H 29#include <sys/ioctl.h> 30#endif 31#ifdef HAVE_SYS_TIME_H 32#include <sys/time.h> 33#endif 34#include <errno.h> 35#ifdef HAVE_ICONV_H 36#include <iconv.h> 37#endif 38/* 39 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h. 40 * As the include guards don't agree, the order of include is important. 41 */ 42#ifdef HAVE_LINUX_EXT2_FS_H 43#include <linux/ext2_fs.h> /* for Linux file flags */ 44#endif 45#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__) 46#include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */ 47#endif 48#include <limits.h> 49#include <locale.h> 50#ifdef HAVE_SIGNAL_H 51#include <signal.h> 52#endif 53#include <stdarg.h> 54#include <time.h> 55 56/* 57 * This same file is used pretty much verbatim for all test harnesses. 58 * 59 * The next few lines are the only differences. 60 * TODO: Move this into a separate configuration header, have all test 61 * suites share one copy of this file. 62 */ 63__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/test/main.c 302294 2016-06-30 08:51:50Z mm $"); 64#define KNOWNREF "test_compat_gtar_1.tar.uu" 65#define ENVBASE "LIBARCHIVE" /* Prefix for environment variables. */ 66#undef PROGRAM /* Testing a library, not a program. */ 67#define LIBRARY "libarchive" 68#define EXTRA_DUMP(x) archive_error_string((struct archive *)(x)) 69#define EXTRA_ERRNO(x) archive_errno((struct archive *)(x)) 70#define EXTRA_VERSION archive_version_details() 71 72/* 73 * 74 * Windows support routines 75 * 76 * Note: Configuration is a tricky issue. Using HAVE_* feature macros 77 * in the test harness is dangerous because they cover up 78 * configuration errors. The classic example of this is omitting a 79 * configure check. If libarchive and libarchive_test both look for 80 * the same feature macro, such errors are hard to detect. Platform 81 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can 82 * easily lead to very messy code. It's best to limit yourself 83 * to only the most generic programming techniques in the test harness 84 * and thus avoid conditionals altogether. Where that's not possible, 85 * try to minimize conditionals by grouping platform-specific tests in 86 * one place (e.g., test_acl_freebsd) or by adding new assert() 87 * functions (e.g., assertMakeHardlink()) to cover up platform 88 * differences. Platform-specific coding in libarchive_test is often 89 * a symptom that some capability is missing from libarchive itself. 90 */ 91#if defined(_WIN32) && !defined(__CYGWIN__) 92#include <io.h> 93#include <direct.h> 94#include <windows.h> 95#ifndef F_OK 96#define F_OK (0) 97#endif 98#ifndef S_ISDIR 99#define S_ISDIR(m) ((m) & _S_IFDIR) 100#endif 101#ifndef S_ISREG 102#define S_ISREG(m) ((m) & _S_IFREG) 103#endif 104#if !defined(__BORLANDC__) 105#define access _access 106#undef chdir 107#define chdir _chdir 108#endif 109#ifndef fileno 110#define fileno _fileno 111#endif 112/*#define fstat _fstat64*/ 113#if !defined(__BORLANDC__) 114#define getcwd _getcwd 115#endif 116#define lstat stat 117/*#define lstat _stat64*/ 118/*#define stat _stat64*/ 119#define rmdir _rmdir 120#if !defined(__BORLANDC__) 121#define strdup _strdup 122#define umask _umask 123#endif 124#define int64_t __int64 125#endif 126 127#if defined(HAVE__CrtSetReportMode) 128# include <crtdbg.h> 129#endif 130 131/* Path to working directory for current test */ 132const char *testworkdir; 133#ifdef PROGRAM 134/* Pathname of exe to be tested. */ 135const char *testprogfile; 136/* Name of exe to use in printf-formatted command strings. */ 137/* On Windows, this includes leading/trailing quotes. */ 138const char *testprog; 139#endif 140 141#if defined(_WIN32) && !defined(__CYGWIN__) 142static void *GetFunctionKernel32(const char *); 143static int my_CreateSymbolicLinkA(const char *, const char *, int); 144static int my_CreateHardLinkA(const char *, const char *); 145static int my_GetFileInformationByName(const char *, 146 BY_HANDLE_FILE_INFORMATION *); 147 148static void * 149GetFunctionKernel32(const char *name) 150{ 151 static HINSTANCE lib; 152 static int set; 153 if (!set) { 154 set = 1; 155 lib = LoadLibrary("kernel32.dll"); 156 } 157 if (lib == NULL) { 158 fprintf(stderr, "Can't load kernel32.dll?!\n"); 159 exit(1); 160 } 161 return (void *)GetProcAddress(lib, name); 162} 163 164static int 165my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags) 166{ 167 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD); 168 static int set; 169 if (!set) { 170 set = 1; 171 f = GetFunctionKernel32("CreateSymbolicLinkA"); 172 } 173 return f == NULL ? 0 : (*f)(linkname, target, flags); 174} 175 176static int 177my_CreateHardLinkA(const char *linkname, const char *target) 178{ 179 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES); 180 static int set; 181 if (!set) { 182 set = 1; 183 f = GetFunctionKernel32("CreateHardLinkA"); 184 } 185 return f == NULL ? 0 : (*f)(linkname, target, NULL); 186} 187 188static int 189my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi) 190{ 191 HANDLE h; 192 int r; 193 194 memset(bhfi, 0, sizeof(*bhfi)); 195 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, 196 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 197 if (h == INVALID_HANDLE_VALUE) 198 return (0); 199 r = GetFileInformationByHandle(h, bhfi); 200 CloseHandle(h); 201 return (r); 202} 203#endif 204 205#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 206static void 207invalid_parameter_handler(const wchar_t * expression, 208 const wchar_t * function, const wchar_t * file, 209 unsigned int line, uintptr_t pReserved) 210{ 211 /* nop */ 212} 213#endif 214 215/* 216 * 217 * OPTIONS FLAGS 218 * 219 */ 220 221/* Enable core dump on failure. */ 222static int dump_on_failure = 0; 223/* Default is to remove temp dirs and log data for successful tests. */ 224static int keep_temp_files = 0; 225/* Default is to run the specified tests once and report errors. */ 226static int until_failure = 0; 227/* Default is to just report pass/fail for each test. */ 228static int verbosity = 0; 229#define VERBOSITY_SUMMARY_ONLY -1 /* -q */ 230#define VERBOSITY_PASSFAIL 0 /* Default */ 231#define VERBOSITY_LIGHT_REPORT 1 /* -v */ 232#define VERBOSITY_FULL 2 /* -vv */ 233/* A few places generate even more output for verbosity > VERBOSITY_FULL, 234 * mostly for debugging the test harness itself. */ 235/* Cumulative count of assertion failures. */ 236static int failures = 0; 237/* Cumulative count of reported skips. */ 238static int skips = 0; 239/* Cumulative count of assertions checked. */ 240static int assertions = 0; 241 242/* Directory where uuencoded reference files can be found. */ 243static const char *refdir; 244 245/* 246 * Report log information selectively to console and/or disk log. 247 */ 248static int log_console = 0; 249static FILE *logfile; 250static void 251vlogprintf(const char *fmt, va_list ap) 252{ 253#ifdef va_copy 254 va_list lfap; 255 va_copy(lfap, ap); 256#endif 257 if (log_console) 258 vfprintf(stdout, fmt, ap); 259 if (logfile != NULL) 260#ifdef va_copy 261 vfprintf(logfile, fmt, lfap); 262 va_end(lfap); 263#else 264 vfprintf(logfile, fmt, ap); 265#endif 266} 267 268static void 269logprintf(const char *fmt, ...) 270{ 271 va_list ap; 272 va_start(ap, fmt); 273 vlogprintf(fmt, ap); 274 va_end(ap); 275} 276 277/* Set up a message to display only if next assertion fails. */ 278static char msgbuff[4096]; 279static const char *msg, *nextmsg; 280void 281failure(const char *fmt, ...) 282{ 283 va_list ap; 284 if (fmt == NULL) { 285 nextmsg = NULL; 286 } else { 287 va_start(ap, fmt); 288 vsprintf(msgbuff, fmt, ap); 289 va_end(ap); 290 nextmsg = msgbuff; 291 } 292} 293 294/* 295 * Copy arguments into file-local variables. 296 * This was added to permit vararg assert() functions without needing 297 * variadic wrapper macros. Turns out that the vararg capability is almost 298 * never used, so almost all of the vararg assertions can be simplified 299 * by removing the vararg capability and reworking the wrapper macro to 300 * pass __FILE__, __LINE__ directly into the function instead of using 301 * this hook. I suspect this machinery is used so rarely that we 302 * would be better off just removing it entirely. That would simplify 303 * the code here noticeably. 304 */ 305static const char *skipping_filename; 306static int skipping_line; 307void skipping_setup(const char *filename, int line) 308{ 309 skipping_filename = filename; 310 skipping_line = line; 311} 312 313/* Called at the beginning of each assert() function. */ 314static void 315assertion_count(const char *file, int line) 316{ 317 (void)file; /* UNUSED */ 318 (void)line; /* UNUSED */ 319 ++assertions; 320 /* Proper handling of "failure()" message. */ 321 msg = nextmsg; 322 nextmsg = NULL; 323 /* Uncomment to print file:line after every assertion. 324 * Verbose, but occasionally useful in tracking down crashes. */ 325 /* printf("Checked %s:%d\n", file, line); */ 326} 327 328/* 329 * For each test source file, we remember how many times each 330 * assertion was reported. Cleared before each new test, 331 * used by test_summarize(). 332 */ 333static struct line { 334 int count; 335 int skip; 336} failed_lines[10000]; 337const char *failed_filename; 338 339/* Count this failure, setup up log destination and handle initial report. */ 340static void 341failure_start(const char *filename, int line, const char *fmt, ...) 342{ 343 va_list ap; 344 345 /* Record another failure for this line. */ 346 ++failures; 347 failed_filename = filename; 348 failed_lines[line].count++; 349 350 /* Determine whether to log header to console. */ 351 switch (verbosity) { 352 case VERBOSITY_LIGHT_REPORT: 353 log_console = (failed_lines[line].count < 2); 354 break; 355 default: 356 log_console = (verbosity >= VERBOSITY_FULL); 357 } 358 359 /* Log file:line header for this failure */ 360 va_start(ap, fmt); 361#if _MSC_VER 362 logprintf("%s(%d): ", filename, line); 363#else 364 logprintf("%s:%d: ", filename, line); 365#endif 366 vlogprintf(fmt, ap); 367 va_end(ap); 368 logprintf("\n"); 369 370 if (msg != NULL && msg[0] != '\0') { 371 logprintf(" Description: %s\n", msg); 372 msg = NULL; 373 } 374 375 /* Determine whether to log details to console. */ 376 if (verbosity == VERBOSITY_LIGHT_REPORT) 377 log_console = 0; 378} 379 380/* Complete reporting of failed tests. */ 381/* 382 * The 'extra' hook here is used by libarchive to include libarchive 383 * error messages with assertion failures. It could also be used 384 * to add strerror() output, for example. Just define the EXTRA_DUMP() 385 * macro appropriately. 386 */ 387static void 388failure_finish(void *extra) 389{ 390 (void)extra; /* UNUSED (maybe) */ 391#ifdef EXTRA_DUMP 392 if (extra != NULL) { 393 logprintf(" errno: %d\n", EXTRA_ERRNO(extra)); 394 logprintf(" detail: %s\n", EXTRA_DUMP(extra)); 395 } 396#endif 397 398 if (dump_on_failure) { 399 fprintf(stderr, 400 " *** forcing core dump so failure can be debugged ***\n"); 401 abort(); 402 } 403} 404 405/* Inform user that we're skipping some checks. */ 406void 407test_skipping(const char *fmt, ...) 408{ 409 char buff[1024]; 410 va_list ap; 411 412 va_start(ap, fmt); 413 vsprintf(buff, fmt, ap); 414 va_end(ap); 415 /* Use failure() message if set. */ 416 msg = nextmsg; 417 nextmsg = NULL; 418 /* failure_start() isn't quite right, but is awfully convenient. */ 419 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff); 420 --failures; /* Undo failures++ in failure_start() */ 421 /* Don't failure_finish() here. */ 422 /* Mark as skip, so doesn't count as failed test. */ 423 failed_lines[skipping_line].skip = 1; 424 ++skips; 425} 426 427/* 428 * 429 * ASSERTIONS 430 * 431 */ 432 433/* Generic assert() just displays the failed condition. */ 434int 435assertion_assert(const char *file, int line, int value, 436 const char *condition, void *extra) 437{ 438 assertion_count(file, line); 439 if (!value) { 440 failure_start(file, line, "Assertion failed: %s", condition); 441 failure_finish(extra); 442 } 443 return (value); 444} 445 446/* chdir() and report any errors */ 447int 448assertion_chdir(const char *file, int line, const char *pathname) 449{ 450 assertion_count(file, line); 451 if (chdir(pathname) == 0) 452 return (1); 453 failure_start(file, line, "chdir(\"%s\")", pathname); 454 failure_finish(NULL); 455 return (0); 456 457} 458 459/* Verify two integers are equal. */ 460int 461assertion_equal_int(const char *file, int line, 462 long long v1, const char *e1, long long v2, const char *e2, void *extra) 463{ 464 assertion_count(file, line); 465 if (v1 == v2) 466 return (1); 467 failure_start(file, line, "%s != %s", e1, e2); 468 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1); 469 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2); 470 failure_finish(extra); 471 return (0); 472} 473 474/* 475 * Utility to convert a single UTF-8 sequence. 476 */ 477static int 478_utf8_to_unicode(uint32_t *pwc, const char *s, size_t n) 479{ 480 static const char utf8_count[256] = { 481 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */ 482 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */ 483 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */ 484 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */ 485 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */ 486 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */ 487 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */ 488 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */ 489 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */ 490 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */ 491 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */ 492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */ 493 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */ 494 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */ 495 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */ 496 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */ 497 }; 498 int ch; 499 int cnt; 500 uint32_t wc; 501 502 *pwc = 0; 503 504 /* Sanity check. */ 505 if (n == 0) 506 return (0); 507 /* 508 * Decode 1-4 bytes depending on the value of the first byte. 509 */ 510 ch = (unsigned char)*s; 511 if (ch == 0) 512 return (0); /* Standard: return 0 for end-of-string. */ 513 cnt = utf8_count[ch]; 514 515 /* Invalide sequence or there are not plenty bytes. */ 516 if (n < (size_t)cnt) 517 return (-1); 518 519 /* Make a Unicode code point from a single UTF-8 sequence. */ 520 switch (cnt) { 521 case 1: /* 1 byte sequence. */ 522 *pwc = ch & 0x7f; 523 return (cnt); 524 case 2: /* 2 bytes sequence. */ 525 if ((s[1] & 0xc0) != 0x80) return (-1); 526 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f); 527 return (cnt); 528 case 3: /* 3 bytes sequence. */ 529 if ((s[1] & 0xc0) != 0x80) return (-1); 530 if ((s[2] & 0xc0) != 0x80) return (-1); 531 wc = ((ch & 0x0f) << 12) 532 | ((s[1] & 0x3f) << 6) 533 | (s[2] & 0x3f); 534 if (wc < 0x800) 535 return (-1);/* Overlong sequence. */ 536 break; 537 case 4: /* 4 bytes sequence. */ 538 if (n < 4) 539 return (-1); 540 if ((s[1] & 0xc0) != 0x80) return (-1); 541 if ((s[2] & 0xc0) != 0x80) return (-1); 542 if ((s[3] & 0xc0) != 0x80) return (-1); 543 wc = ((ch & 0x07) << 18) 544 | ((s[1] & 0x3f) << 12) 545 | ((s[2] & 0x3f) << 6) 546 | (s[3] & 0x3f); 547 if (wc < 0x10000) 548 return (-1);/* Overlong sequence. */ 549 break; 550 default: 551 return (-1); 552 } 553 554 /* The code point larger than 0x10FFFF is not leagal 555 * Unicode values. */ 556 if (wc > 0x10FFFF) 557 return (-1); 558 /* Correctly gets a Unicode, returns used bytes. */ 559 *pwc = wc; 560 return (cnt); 561} 562 563static void strdump(const char *e, const char *p, int ewidth, int utf8) 564{ 565 const char *q = p; 566 567 logprintf(" %*s = ", ewidth, e); 568 if (p == NULL) { 569 logprintf("NULL\n"); 570 return; 571 } 572 logprintf("\""); 573 while (*p != '\0') { 574 unsigned int c = 0xff & *p++; 575 switch (c) { 576 case '\a': logprintf("\\a"); break; 577 case '\b': logprintf("\\b"); break; 578 case '\n': logprintf("\\n"); break; 579 case '\r': logprintf("\\r"); break; 580 default: 581 if (c >= 32 && c < 127) 582 logprintf("%c", c); 583 else 584 logprintf("\\x%02X", c); 585 } 586 } 587 logprintf("\""); 588 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q)); 589 590 /* 591 * If the current string is UTF-8, dump its code points. 592 */ 593 if (utf8) { 594 size_t len; 595 uint32_t uc; 596 int n; 597 int cnt = 0; 598 599 p = q; 600 len = strlen(p); 601 logprintf(" ["); 602 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) { 603 if (p != q) 604 logprintf(" "); 605 logprintf("%04X", uc); 606 p += n; 607 len -= n; 608 cnt++; 609 } 610 logprintf("]"); 611 logprintf(" (count %d", cnt); 612 if (n < 0) { 613 logprintf(",unknown %d bytes", len); 614 } 615 logprintf(")"); 616 617 } 618 logprintf("\n"); 619} 620 621/* Verify two strings are equal, dump them if not. */ 622int 623assertion_equal_string(const char *file, int line, 624 const char *v1, const char *e1, 625 const char *v2, const char *e2, 626 void *extra, int utf8) 627{ 628 int l1, l2; 629 630 assertion_count(file, line); 631 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0)) 632 return (1); 633 failure_start(file, line, "%s != %s", e1, e2); 634 l1 = (int)strlen(e1); 635 l2 = (int)strlen(e2); 636 if (l1 < l2) 637 l1 = l2; 638 strdump(e1, v1, l1, utf8); 639 strdump(e2, v2, l1, utf8); 640 failure_finish(extra); 641 return (0); 642} 643 644static void 645wcsdump(const char *e, const wchar_t *w) 646{ 647 logprintf(" %s = ", e); 648 if (w == NULL) { 649 logprintf("(null)"); 650 return; 651 } 652 logprintf("\""); 653 while (*w != L'\0') { 654 unsigned int c = *w++; 655 if (c >= 32 && c < 127) 656 logprintf("%c", c); 657 else if (c < 256) 658 logprintf("\\x%02X", c); 659 else if (c < 0x10000) 660 logprintf("\\u%04X", c); 661 else 662 logprintf("\\U%08X", c); 663 } 664 logprintf("\"\n"); 665} 666 667#ifndef HAVE_WCSCMP 668static int 669wcscmp(const wchar_t *s1, const wchar_t *s2) 670{ 671 672 while (*s1 == *s2++) { 673 if (*s1++ == L'\0') 674 return 0; 675 } 676 if (*s1 > *--s2) 677 return 1; 678 else 679 return -1; 680} 681#endif 682 683/* Verify that two wide strings are equal, dump them if not. */ 684int 685assertion_equal_wstring(const char *file, int line, 686 const wchar_t *v1, const char *e1, 687 const wchar_t *v2, const char *e2, 688 void *extra) 689{ 690 assertion_count(file, line); 691 if (v1 == v2) 692 return (1); 693 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0) 694 return (1); 695 failure_start(file, line, "%s != %s", e1, e2); 696 wcsdump(e1, v1); 697 wcsdump(e2, v2); 698 failure_finish(extra); 699 return (0); 700} 701 702/* 703 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then 704 * any bytes in p that differ from ref will be highlighted with '_' 705 * before and after the hex value. 706 */ 707static void 708hexdump(const char *p, const char *ref, size_t l, size_t offset) 709{ 710 size_t i, j; 711 char sep; 712 713 if (p == NULL) { 714 logprintf("(null)\n"); 715 return; 716 } 717 for(i=0; i < l; i+=16) { 718 logprintf("%04x", (unsigned)(i + offset)); 719 sep = ' '; 720 for (j = 0; j < 16 && i + j < l; j++) { 721 if (ref != NULL && p[i + j] != ref[i + j]) 722 sep = '_'; 723 logprintf("%c%02x", sep, 0xff & (int)p[i+j]); 724 if (ref != NULL && p[i + j] == ref[i + j]) 725 sep = ' '; 726 } 727 for (; j < 16; j++) { 728 logprintf("%c ", sep); 729 sep = ' '; 730 } 731 logprintf("%c", sep); 732 for (j=0; j < 16 && i + j < l; j++) { 733 int c = p[i + j]; 734 if (c >= ' ' && c <= 126) 735 logprintf("%c", c); 736 else 737 logprintf("."); 738 } 739 logprintf("\n"); 740 } 741} 742 743/* Verify that two blocks of memory are the same, display the first 744 * block of differences if they're not. */ 745int 746assertion_equal_mem(const char *file, int line, 747 const void *_v1, const char *e1, 748 const void *_v2, const char *e2, 749 size_t l, const char *ld, void *extra) 750{ 751 const char *v1 = (const char *)_v1; 752 const char *v2 = (const char *)_v2; 753 size_t offset; 754 755 assertion_count(file, line); 756 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0)) 757 return (1); 758 if (v1 == NULL || v2 == NULL) 759 return (0); 760 761 failure_start(file, line, "%s != %s", e1, e2); 762 logprintf(" size %s = %d\n", ld, (int)l); 763 /* Dump 48 bytes (3 lines) so that the first difference is 764 * in the second line. */ 765 offset = 0; 766 while (l > 64 && memcmp(v1, v2, 32) == 0) { 767 /* Two lines agree, so step forward one line. */ 768 v1 += 16; 769 v2 += 16; 770 l -= 16; 771 offset += 16; 772 } 773 logprintf(" Dump of %s\n", e1); 774 hexdump(v1, v2, l < 128 ? l : 128, offset); 775 logprintf(" Dump of %s\n", e2); 776 hexdump(v2, v1, l < 128 ? l : 128, offset); 777 logprintf("\n"); 778 failure_finish(extra); 779 return (0); 780} 781 782/* Verify that a block of memory is filled with the specified byte. */ 783int 784assertion_memory_filled_with(const char *file, int line, 785 const void *_v1, const char *vd, 786 size_t l, const char *ld, 787 char b, const char *bd, void *extra) 788{ 789 const char *v1 = (const char *)_v1; 790 size_t c = 0; 791 size_t i; 792 (void)ld; /* UNUSED */ 793 794 assertion_count(file, line); 795 796 for (i = 0; i < l; ++i) { 797 if (v1[i] == b) { 798 ++c; 799 } 800 } 801 if (c == l) 802 return (1); 803 804 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd); 805 logprintf(" Only %d bytes were correct\n", (int)c); 806 failure_finish(extra); 807 return (0); 808} 809 810/* Verify that the named file exists and is empty. */ 811int 812assertion_empty_file(const char *filename, int line, const char *f1) 813{ 814 char buff[1024]; 815 struct stat st; 816 ssize_t s; 817 FILE *f; 818 819 assertion_count(filename, line); 820 821 if (stat(f1, &st) != 0) { 822 failure_start(filename, line, "Stat failed: %s", f1); 823 failure_finish(NULL); 824 return (0); 825 } 826 if (st.st_size == 0) 827 return (1); 828 829 failure_start(filename, line, "File should be empty: %s", f1); 830 logprintf(" File size: %d\n", (int)st.st_size); 831 logprintf(" Contents:\n"); 832 f = fopen(f1, "rb"); 833 if (f == NULL) { 834 logprintf(" Unable to open %s\n", f1); 835 } else { 836 s = ((off_t)sizeof(buff) < st.st_size) ? 837 (ssize_t)sizeof(buff) : (ssize_t)st.st_size; 838 s = fread(buff, 1, s, f); 839 hexdump(buff, NULL, s, 0); 840 fclose(f); 841 } 842 failure_finish(NULL); 843 return (0); 844} 845 846/* Verify that the named file exists and is not empty. */ 847int 848assertion_non_empty_file(const char *filename, int line, const char *f1) 849{ 850 struct stat st; 851 852 assertion_count(filename, line); 853 854 if (stat(f1, &st) != 0) { 855 failure_start(filename, line, "Stat failed: %s", f1); 856 failure_finish(NULL); 857 return (0); 858 } 859 if (st.st_size == 0) { 860 failure_start(filename, line, "File empty: %s", f1); 861 failure_finish(NULL); 862 return (0); 863 } 864 return (1); 865} 866 867/* Verify that two files have the same contents. */ 868/* TODO: hexdump the first bytes that actually differ. */ 869int 870assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2) 871{ 872 char buff1[1024]; 873 char buff2[1024]; 874 FILE *f1, *f2; 875 int n1, n2; 876 877 assertion_count(filename, line); 878 879 f1 = fopen(fn1, "rb"); 880 f2 = fopen(fn2, "rb"); 881 if (f1 == NULL || f2 == NULL) { 882 if (f1) fclose(f1); 883 if (f2) fclose(f2); 884 return (0); 885 } 886 for (;;) { 887 n1 = (int)fread(buff1, 1, sizeof(buff1), f1); 888 n2 = (int)fread(buff2, 1, sizeof(buff2), f2); 889 if (n1 != n2) 890 break; 891 if (n1 == 0 && n2 == 0) { 892 fclose(f1); 893 fclose(f2); 894 return (1); 895 } 896 if (memcmp(buff1, buff2, n1) != 0) 897 break; 898 } 899 fclose(f1); 900 fclose(f2); 901 failure_start(filename, line, "Files not identical"); 902 logprintf(" file1=\"%s\"\n", fn1); 903 logprintf(" file2=\"%s\"\n", fn2); 904 failure_finish(NULL); 905 return (0); 906} 907 908/* Verify that the named file does exist. */ 909int 910assertion_file_exists(const char *filename, int line, const char *f) 911{ 912 assertion_count(filename, line); 913 914#if defined(_WIN32) && !defined(__CYGWIN__) 915 if (!_access(f, 0)) 916 return (1); 917#else 918 if (!access(f, F_OK)) 919 return (1); 920#endif 921 failure_start(filename, line, "File should exist: %s", f); 922 failure_finish(NULL); 923 return (0); 924} 925 926/* Verify that the named file doesn't exist. */ 927int 928assertion_file_not_exists(const char *filename, int line, const char *f) 929{ 930 assertion_count(filename, line); 931 932#if defined(_WIN32) && !defined(__CYGWIN__) 933 if (_access(f, 0)) 934 return (1); 935#else 936 if (access(f, F_OK)) 937 return (1); 938#endif 939 failure_start(filename, line, "File should not exist: %s", f); 940 failure_finish(NULL); 941 return (0); 942} 943 944/* Compare the contents of a file to a block of memory. */ 945int 946assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn) 947{ 948 char *contents; 949 FILE *f; 950 int n; 951 952 assertion_count(filename, line); 953 954 f = fopen(fn, "rb"); 955 if (f == NULL) { 956 failure_start(filename, line, 957 "File should exist: %s", fn); 958 failure_finish(NULL); 959 return (0); 960 } 961 contents = malloc(s * 2); 962 n = (int)fread(contents, 1, s * 2, f); 963 fclose(f); 964 if (n == s && memcmp(buff, contents, s) == 0) { 965 free(contents); 966 return (1); 967 } 968 failure_start(filename, line, "File contents don't match"); 969 logprintf(" file=\"%s\"\n", fn); 970 if (n > 0) 971 hexdump(contents, buff, n > 512 ? 512 : n, 0); 972 else { 973 logprintf(" File empty, contents should be:\n"); 974 hexdump(buff, NULL, s > 512 ? 512 : s, 0); 975 } 976 failure_finish(NULL); 977 free(contents); 978 return (0); 979} 980 981/* Check the contents of a text file, being tolerant of line endings. */ 982int 983assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn) 984{ 985 char *contents; 986 const char *btxt, *ftxt; 987 FILE *f; 988 int n, s; 989 990 assertion_count(filename, line); 991 f = fopen(fn, "r"); 992 if (f == NULL) { 993 failure_start(filename, line, 994 "File doesn't exist: %s", fn); 995 failure_finish(NULL); 996 return (0); 997 } 998 s = (int)strlen(buff); 999 contents = malloc(s * 2 + 128); 1000 n = (int)fread(contents, 1, s * 2 + 128 - 1, f); 1001 if (n >= 0) 1002 contents[n] = '\0'; 1003 fclose(f); 1004 /* Compare texts. */ 1005 btxt = buff; 1006 ftxt = (const char *)contents; 1007 while (*btxt != '\0' && *ftxt != '\0') { 1008 if (*btxt == *ftxt) { 1009 ++btxt; 1010 ++ftxt; 1011 continue; 1012 } 1013 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') { 1014 /* Pass over different new line characters. */ 1015 ++btxt; 1016 ftxt += 2; 1017 continue; 1018 } 1019 break; 1020 } 1021 if (*btxt == '\0' && *ftxt == '\0') { 1022 free(contents); 1023 return (1); 1024 } 1025 failure_start(filename, line, "Contents don't match"); 1026 logprintf(" file=\"%s\"\n", fn); 1027 if (n > 0) { 1028 hexdump(contents, buff, n, 0); 1029 logprintf(" expected\n", fn); 1030 hexdump(buff, contents, s, 0); 1031 } else { 1032 logprintf(" File empty, contents should be:\n"); 1033 hexdump(buff, NULL, s, 0); 1034 } 1035 failure_finish(NULL); 1036 free(contents); 1037 return (0); 1038} 1039 1040/* Verify that a text file contains the specified lines, regardless of order */ 1041/* This could be more efficient if we sorted both sets of lines, etc, but 1042 * since this is used only for testing and only ever deals with a dozen or so 1043 * lines at a time, this relatively crude approach is just fine. */ 1044int 1045assertion_file_contains_lines_any_order(const char *file, int line, 1046 const char *pathname, const char *lines[]) 1047{ 1048 char *buff; 1049 size_t buff_size; 1050 size_t expected_count, actual_count, i, j; 1051 char **expected = NULL; 1052 char *p, **actual = NULL; 1053 char c; 1054 int expected_failure = 0, actual_failure = 0; 1055 1056 assertion_count(file, line); 1057 1058 buff = slurpfile(&buff_size, "%s", pathname); 1059 if (buff == NULL) { 1060 failure_start(pathname, line, "Can't read file: %s", pathname); 1061 failure_finish(NULL); 1062 return (0); 1063 } 1064 1065 /* Make a copy of the provided lines and count up the expected 1066 * file size. */ 1067 for (i = 0; lines[i] != NULL; ++i) { 1068 } 1069 expected_count = i; 1070 if (expected_count) { 1071 expected = malloc(sizeof(char *) * expected_count); 1072 if (expected == NULL) { 1073 failure_start(pathname, line, "Can't allocate memory"); 1074 failure_finish(NULL); 1075 free(expected); 1076 return (0); 1077 } 1078 for (i = 0; lines[i] != NULL; ++i) { 1079 expected[i] = strdup(lines[i]); 1080 } 1081 } 1082 1083 /* Break the file into lines */ 1084 actual_count = 0; 1085 for (c = '\0', p = buff; p < buff + buff_size; ++p) { 1086 if (*p == '\x0d' || *p == '\x0a') 1087 *p = '\0'; 1088 if (c == '\0' && *p != '\0') 1089 ++actual_count; 1090 c = *p; 1091 } 1092 if (actual_count) { 1093 actual = calloc(sizeof(char *), actual_count); 1094 if (actual == NULL) { 1095 failure_start(pathname, line, "Can't allocate memory"); 1096 failure_finish(NULL); 1097 free(expected); 1098 return (0); 1099 } 1100 for (j = 0, p = buff; p < buff + buff_size; 1101 p += 1 + strlen(p)) { 1102 if (*p != '\0') { 1103 actual[j] = p; 1104 ++j; 1105 } 1106 } 1107 } 1108 1109 /* Erase matching lines from both lists */ 1110 for (i = 0; i < expected_count; ++i) { 1111 if (expected[i] == NULL) 1112 continue; 1113 for (j = 0; j < actual_count; ++j) { 1114 if (actual[j] == NULL) 1115 continue; 1116 if (strcmp(expected[i], actual[j]) == 0) { 1117 free(expected[i]); 1118 expected[i] = NULL; 1119 actual[j] = NULL; 1120 break; 1121 } 1122 } 1123 } 1124 1125 /* If there's anything left, it's a failure */ 1126 for (i = 0; i < expected_count; ++i) { 1127 if (expected[i] != NULL) 1128 ++expected_failure; 1129 } 1130 for (j = 0; j < actual_count; ++j) { 1131 if (actual[j] != NULL) 1132 ++actual_failure; 1133 } 1134 if (expected_failure == 0 && actual_failure == 0) { 1135 free(buff); 1136 free(expected); 1137 free(actual); 1138 return (1); 1139 } 1140 failure_start(file, line, "File doesn't match: %s", pathname); 1141 for (i = 0; i < expected_count; ++i) { 1142 if (expected[i] != NULL) { 1143 logprintf(" Expected but not present: %s\n", expected[i]); 1144 free(expected[i]); 1145 } 1146 } 1147 for (j = 0; j < actual_count; ++j) { 1148 if (actual[j] != NULL) 1149 logprintf(" Present but not expected: %s\n", actual[j]); 1150 } 1151 failure_finish(NULL); 1152 free(buff); 1153 free(expected); 1154 free(actual); 1155 return (0); 1156} 1157 1158/* Test that two paths point to the same file. */ 1159/* As a side-effect, asserts that both files exist. */ 1160static int 1161is_hardlink(const char *file, int line, 1162 const char *path1, const char *path2) 1163{ 1164#if defined(_WIN32) && !defined(__CYGWIN__) 1165 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2; 1166 int r; 1167 1168 assertion_count(file, line); 1169 r = my_GetFileInformationByName(path1, &bhfi1); 1170 if (r == 0) { 1171 failure_start(file, line, "File %s can't be inspected?", path1); 1172 failure_finish(NULL); 1173 return (0); 1174 } 1175 r = my_GetFileInformationByName(path2, &bhfi2); 1176 if (r == 0) { 1177 failure_start(file, line, "File %s can't be inspected?", path2); 1178 failure_finish(NULL); 1179 return (0); 1180 } 1181 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber 1182 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh 1183 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow); 1184#else 1185 struct stat st1, st2; 1186 int r; 1187 1188 assertion_count(file, line); 1189 r = lstat(path1, &st1); 1190 if (r != 0) { 1191 failure_start(file, line, "File should exist: %s", path1); 1192 failure_finish(NULL); 1193 return (0); 1194 } 1195 r = lstat(path2, &st2); 1196 if (r != 0) { 1197 failure_start(file, line, "File should exist: %s", path2); 1198 failure_finish(NULL); 1199 return (0); 1200 } 1201 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev); 1202#endif 1203} 1204 1205int 1206assertion_is_hardlink(const char *file, int line, 1207 const char *path1, const char *path2) 1208{ 1209 if (is_hardlink(file, line, path1, path2)) 1210 return (1); 1211 failure_start(file, line, 1212 "Files %s and %s are not hardlinked", path1, path2); 1213 failure_finish(NULL); 1214 return (0); 1215} 1216 1217int 1218assertion_is_not_hardlink(const char *file, int line, 1219 const char *path1, const char *path2) 1220{ 1221 if (!is_hardlink(file, line, path1, path2)) 1222 return (1); 1223 failure_start(file, line, 1224 "Files %s and %s should not be hardlinked", path1, path2); 1225 failure_finish(NULL); 1226 return (0); 1227} 1228 1229/* Verify a/b/mtime of 'pathname'. */ 1230/* If 'recent', verify that it's within last 10 seconds. */ 1231static int 1232assertion_file_time(const char *file, int line, 1233 const char *pathname, long t, long nsec, char type, int recent) 1234{ 1235 long long filet, filet_nsec; 1236 int r; 1237 1238#if defined(_WIN32) && !defined(__CYGWIN__) 1239#define EPOC_TIME (116444736000000000ULL) 1240 FILETIME fxtime, fbirthtime, fatime, fmtime; 1241 ULARGE_INTEGER wintm; 1242 HANDLE h; 1243 fxtime.dwLowDateTime = 0; 1244 fxtime.dwHighDateTime = 0; 1245 1246 assertion_count(file, line); 1247 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open 1248 * a directory file. If not, CreateFile() will fail when 1249 * the pathname is a directory. */ 1250 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL, 1251 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 1252 if (h == INVALID_HANDLE_VALUE) { 1253 failure_start(file, line, "Can't access %s\n", pathname); 1254 failure_finish(NULL); 1255 return (0); 1256 } 1257 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime); 1258 switch (type) { 1259 case 'a': fxtime = fatime; break; 1260 case 'b': fxtime = fbirthtime; break; 1261 case 'm': fxtime = fmtime; break; 1262 } 1263 CloseHandle(h); 1264 if (r == 0) { 1265 failure_start(file, line, "Can't GetFileTime %s\n", pathname); 1266 failure_finish(NULL); 1267 return (0); 1268 } 1269 wintm.LowPart = fxtime.dwLowDateTime; 1270 wintm.HighPart = fxtime.dwHighDateTime; 1271 filet = (wintm.QuadPart - EPOC_TIME) / 10000000; 1272 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100; 1273 nsec = (nsec / 100) * 100; /* Round the request */ 1274#else 1275 struct stat st; 1276 1277 assertion_count(file, line); 1278 r = lstat(pathname, &st); 1279 if (r != 0) { 1280 failure_start(file, line, "Can't stat %s\n", pathname); 1281 failure_finish(NULL); 1282 return (0); 1283 } 1284 switch (type) { 1285 case 'a': filet = st.st_atime; break; 1286 case 'm': filet = st.st_mtime; break; 1287 case 'b': filet = 0; break; 1288 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1289 exit(1); 1290 } 1291#if defined(__FreeBSD__) 1292 switch (type) { 1293 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break; 1294 case 'b': filet = st.st_birthtime; 1295 /* FreeBSD filesystems that don't support birthtime 1296 * (e.g., UFS1) always return -1 here. */ 1297 if (filet == -1) { 1298 return (1); 1299 } 1300 filet_nsec = st.st_birthtimespec.tv_nsec; break; 1301 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break; 1302 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1303 exit(1); 1304 } 1305 /* FreeBSD generally only stores to microsecond res, so round. */ 1306 filet_nsec = (filet_nsec / 1000) * 1000; 1307 nsec = (nsec / 1000) * 1000; 1308#else 1309 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */ 1310 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */ 1311#if defined(__HAIKU__) 1312 if (type == 'a') return (1); /* Haiku doesn't have atime. */ 1313#endif 1314#endif 1315#endif 1316 if (recent) { 1317 /* Check that requested time is up-to-date. */ 1318 time_t now = time(NULL); 1319 if (filet < now - 10 || filet > now + 1) { 1320 failure_start(file, line, 1321 "File %s has %ctime %lld, %lld seconds ago\n", 1322 pathname, type, filet, now - filet); 1323 failure_finish(NULL); 1324 return (0); 1325 } 1326 } else if (filet != t || filet_nsec != nsec) { 1327 failure_start(file, line, 1328 "File %s has %ctime %lld.%09lld, expected %lld.%09lld", 1329 pathname, type, filet, filet_nsec, t, nsec); 1330 failure_finish(NULL); 1331 return (0); 1332 } 1333 return (1); 1334} 1335 1336/* Verify atime of 'pathname'. */ 1337int 1338assertion_file_atime(const char *file, int line, 1339 const char *pathname, long t, long nsec) 1340{ 1341 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0); 1342} 1343 1344/* Verify atime of 'pathname' is up-to-date. */ 1345int 1346assertion_file_atime_recent(const char *file, int line, const char *pathname) 1347{ 1348 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1); 1349} 1350 1351/* Verify birthtime of 'pathname'. */ 1352int 1353assertion_file_birthtime(const char *file, int line, 1354 const char *pathname, long t, long nsec) 1355{ 1356 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0); 1357} 1358 1359/* Verify birthtime of 'pathname' is up-to-date. */ 1360int 1361assertion_file_birthtime_recent(const char *file, int line, 1362 const char *pathname) 1363{ 1364 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1); 1365} 1366 1367/* Verify mtime of 'pathname'. */ 1368int 1369assertion_file_mtime(const char *file, int line, 1370 const char *pathname, long t, long nsec) 1371{ 1372 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0); 1373} 1374 1375/* Verify mtime of 'pathname' is up-to-date. */ 1376int 1377assertion_file_mtime_recent(const char *file, int line, const char *pathname) 1378{ 1379 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1); 1380} 1381 1382/* Verify number of links to 'pathname'. */ 1383int 1384assertion_file_nlinks(const char *file, int line, 1385 const char *pathname, int nlinks) 1386{ 1387#if defined(_WIN32) && !defined(__CYGWIN__) 1388 BY_HANDLE_FILE_INFORMATION bhfi; 1389 int r; 1390 1391 assertion_count(file, line); 1392 r = my_GetFileInformationByName(pathname, &bhfi); 1393 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks) 1394 return (1); 1395 failure_start(file, line, "File %s has %d links, expected %d", 1396 pathname, bhfi.nNumberOfLinks, nlinks); 1397 failure_finish(NULL); 1398 return (0); 1399#else 1400 struct stat st; 1401 int r; 1402 1403 assertion_count(file, line); 1404 r = lstat(pathname, &st); 1405 if (r == 0 && (int)st.st_nlink == nlinks) 1406 return (1); 1407 failure_start(file, line, "File %s has %d links, expected %d", 1408 pathname, st.st_nlink, nlinks); 1409 failure_finish(NULL); 1410 return (0); 1411#endif 1412} 1413 1414/* Verify size of 'pathname'. */ 1415int 1416assertion_file_size(const char *file, int line, const char *pathname, long size) 1417{ 1418 int64_t filesize; 1419 int r; 1420 1421 assertion_count(file, line); 1422#if defined(_WIN32) && !defined(__CYGWIN__) 1423 { 1424 BY_HANDLE_FILE_INFORMATION bhfi; 1425 r = !my_GetFileInformationByName(pathname, &bhfi); 1426 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow; 1427 } 1428#else 1429 { 1430 struct stat st; 1431 r = lstat(pathname, &st); 1432 filesize = st.st_size; 1433 } 1434#endif 1435 if (r == 0 && filesize == size) 1436 return (1); 1437 failure_start(file, line, "File %s has size %ld, expected %ld", 1438 pathname, (long)filesize, (long)size); 1439 failure_finish(NULL); 1440 return (0); 1441} 1442 1443/* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */ 1444int 1445assertion_is_dir(const char *file, int line, const char *pathname, int mode) 1446{ 1447 struct stat st; 1448 int r; 1449 1450#if defined(_WIN32) && !defined(__CYGWIN__) 1451 (void)mode; /* UNUSED */ 1452#endif 1453 assertion_count(file, line); 1454 r = lstat(pathname, &st); 1455 if (r != 0) { 1456 failure_start(file, line, "Dir should exist: %s", pathname); 1457 failure_finish(NULL); 1458 return (0); 1459 } 1460 if (!S_ISDIR(st.st_mode)) { 1461 failure_start(file, line, "%s is not a dir", pathname); 1462 failure_finish(NULL); 1463 return (0); 1464 } 1465#if !defined(_WIN32) || defined(__CYGWIN__) 1466 /* Windows doesn't handle permissions the same way as POSIX, 1467 * so just ignore the mode tests. */ 1468 /* TODO: Can we do better here? */ 1469 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1470 failure_start(file, line, "Dir %s has wrong mode", pathname); 1471 logprintf(" Expected: 0%3o\n", mode); 1472 logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1473 failure_finish(NULL); 1474 return (0); 1475 } 1476#endif 1477 return (1); 1478} 1479 1480/* Verify that 'pathname' is a regular file. If 'mode' is >= 0, 1481 * verify that too. */ 1482int 1483assertion_is_reg(const char *file, int line, const char *pathname, int mode) 1484{ 1485 struct stat st; 1486 int r; 1487 1488#if defined(_WIN32) && !defined(__CYGWIN__) 1489 (void)mode; /* UNUSED */ 1490#endif 1491 assertion_count(file, line); 1492 r = lstat(pathname, &st); 1493 if (r != 0 || !S_ISREG(st.st_mode)) { 1494 failure_start(file, line, "File should exist: %s", pathname); 1495 failure_finish(NULL); 1496 return (0); 1497 } 1498#if !defined(_WIN32) || defined(__CYGWIN__) 1499 /* Windows doesn't handle permissions the same way as POSIX, 1500 * so just ignore the mode tests. */ 1501 /* TODO: Can we do better here? */ 1502 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1503 failure_start(file, line, "File %s has wrong mode", pathname); 1504 logprintf(" Expected: 0%3o\n", mode); 1505 logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1506 failure_finish(NULL); 1507 return (0); 1508 } 1509#endif 1510 return (1); 1511} 1512 1513/* Check whether 'pathname' is a symbolic link. If 'contents' is 1514 * non-NULL, verify that the symlink has those contents. */ 1515static int 1516is_symlink(const char *file, int line, 1517 const char *pathname, const char *contents) 1518{ 1519#if defined(_WIN32) && !defined(__CYGWIN__) 1520 (void)pathname; /* UNUSED */ 1521 (void)contents; /* UNUSED */ 1522 assertion_count(file, line); 1523 /* Windows sort-of has real symlinks, but they're only usable 1524 * by privileged users and are crippled even then, so there's 1525 * really not much point in bothering with this. */ 1526 return (0); 1527#else 1528 char buff[300]; 1529 struct stat st; 1530 ssize_t linklen; 1531 int r; 1532 1533 assertion_count(file, line); 1534 r = lstat(pathname, &st); 1535 if (r != 0) { 1536 failure_start(file, line, 1537 "Symlink should exist: %s", pathname); 1538 failure_finish(NULL); 1539 return (0); 1540 } 1541 if (!S_ISLNK(st.st_mode)) 1542 return (0); 1543 if (contents == NULL) 1544 return (1); 1545 linklen = readlink(pathname, buff, sizeof(buff)); 1546 if (linklen < 0) { 1547 failure_start(file, line, "Can't read symlink %s", pathname); 1548 failure_finish(NULL); 1549 return (0); 1550 } 1551 buff[linklen] = '\0'; 1552 if (strcmp(buff, contents) != 0) 1553 return (0); 1554 return (1); 1555#endif 1556} 1557 1558/* Assert that path is a symlink that (optionally) contains contents. */ 1559int 1560assertion_is_symlink(const char *file, int line, 1561 const char *path, const char *contents) 1562{ 1563 if (is_symlink(file, line, path, contents)) 1564 return (1); 1565 if (contents) 1566 failure_start(file, line, "File %s is not a symlink to %s", 1567 path, contents); 1568 else 1569 failure_start(file, line, "File %s is not a symlink", path); 1570 failure_finish(NULL); 1571 return (0); 1572} 1573 1574 1575/* Create a directory and report any errors. */ 1576int 1577assertion_make_dir(const char *file, int line, const char *dirname, int mode) 1578{ 1579 assertion_count(file, line); 1580#if defined(_WIN32) && !defined(__CYGWIN__) 1581 (void)mode; /* UNUSED */ 1582 if (0 == _mkdir(dirname)) 1583 return (1); 1584#else 1585 if (0 == mkdir(dirname, mode)) 1586 return (1); 1587#endif 1588 failure_start(file, line, "Could not create directory %s", dirname); 1589 failure_finish(NULL); 1590 return(0); 1591} 1592 1593/* Create a file with the specified contents and report any failures. */ 1594int 1595assertion_make_file(const char *file, int line, 1596 const char *path, int mode, int csize, const void *contents) 1597{ 1598#if defined(_WIN32) && !defined(__CYGWIN__) 1599 /* TODO: Rework this to set file mode as well. */ 1600 FILE *f; 1601 (void)mode; /* UNUSED */ 1602 assertion_count(file, line); 1603 f = fopen(path, "wb"); 1604 if (f == NULL) { 1605 failure_start(file, line, "Could not create file %s", path); 1606 failure_finish(NULL); 1607 return (0); 1608 } 1609 if (contents != NULL) { 1610 size_t wsize; 1611 1612 if (csize < 0) 1613 wsize = strlen(contents); 1614 else 1615 wsize = (size_t)csize; 1616 if (wsize != fwrite(contents, 1, wsize, f)) { 1617 fclose(f); 1618 failure_start(file, line, 1619 "Could not write file %s", path); 1620 failure_finish(NULL); 1621 return (0); 1622 } 1623 } 1624 fclose(f); 1625 return (1); 1626#else 1627 int fd; 1628 assertion_count(file, line); 1629 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644); 1630 if (fd < 0) { 1631 failure_start(file, line, "Could not create %s", path); 1632 failure_finish(NULL); 1633 return (0); 1634 } 1635 if (contents != NULL) { 1636 ssize_t wsize; 1637 1638 if (csize < 0) 1639 wsize = (ssize_t)strlen(contents); 1640 else 1641 wsize = (ssize_t)csize; 1642 if (wsize != write(fd, contents, wsize)) { 1643 close(fd); 1644 failure_start(file, line, 1645 "Could not write to %s", path); 1646 failure_finish(NULL); 1647 return (0); 1648 } 1649 } 1650 close(fd); 1651 return (1); 1652#endif 1653} 1654 1655/* Create a hardlink and report any failures. */ 1656int 1657assertion_make_hardlink(const char *file, int line, 1658 const char *newpath, const char *linkto) 1659{ 1660 int succeeded; 1661 1662 assertion_count(file, line); 1663#if defined(_WIN32) && !defined(__CYGWIN__) 1664 succeeded = my_CreateHardLinkA(newpath, linkto); 1665#elif HAVE_LINK 1666 succeeded = !link(linkto, newpath); 1667#else 1668 succeeded = 0; 1669#endif 1670 if (succeeded) 1671 return (1); 1672 failure_start(file, line, "Could not create hardlink"); 1673 logprintf(" New link: %s\n", newpath); 1674 logprintf(" Old name: %s\n", linkto); 1675 failure_finish(NULL); 1676 return(0); 1677} 1678 1679/* Create a symlink and report any failures. */ 1680int 1681assertion_make_symlink(const char *file, int line, 1682 const char *newpath, const char *linkto) 1683{ 1684#if defined(_WIN32) && !defined(__CYGWIN__) 1685 int targetIsDir = 0; /* TODO: Fix this */ 1686 assertion_count(file, line); 1687 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir)) 1688 return (1); 1689#elif HAVE_SYMLINK 1690 assertion_count(file, line); 1691 if (0 == symlink(linkto, newpath)) 1692 return (1); 1693#endif 1694 failure_start(file, line, "Could not create symlink"); 1695 logprintf(" New link: %s\n", newpath); 1696 logprintf(" Old name: %s\n", linkto); 1697 failure_finish(NULL); 1698 return(0); 1699} 1700 1701/* Set umask, report failures. */ 1702int 1703assertion_umask(const char *file, int line, int mask) 1704{ 1705 assertion_count(file, line); 1706 (void)file; /* UNUSED */ 1707 (void)line; /* UNUSED */ 1708 umask(mask); 1709 return (1); 1710} 1711 1712/* Set times, report failures. */ 1713int 1714assertion_utimes(const char *file, int line, 1715 const char *pathname, long at, long at_nsec, long mt, long mt_nsec) 1716{ 1717 int r; 1718 1719#if defined(_WIN32) && !defined(__CYGWIN__) 1720#define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\ 1721 + (((nsec)/1000)*10)) 1722 HANDLE h; 1723 ULARGE_INTEGER wintm; 1724 FILETIME fatime, fmtime; 1725 FILETIME *pat, *pmt; 1726 1727 assertion_count(file, line); 1728 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE, 1729 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 1730 FILE_FLAG_BACKUP_SEMANTICS, NULL); 1731 if (h == INVALID_HANDLE_VALUE) { 1732 failure_start(file, line, "Can't access %s\n", pathname); 1733 failure_finish(NULL); 1734 return (0); 1735 } 1736 1737 if (at > 0 || at_nsec > 0) { 1738 wintm.QuadPart = WINTIME(at, at_nsec); 1739 fatime.dwLowDateTime = wintm.LowPart; 1740 fatime.dwHighDateTime = wintm.HighPart; 1741 pat = &fatime; 1742 } else 1743 pat = NULL; 1744 if (mt > 0 || mt_nsec > 0) { 1745 wintm.QuadPart = WINTIME(mt, mt_nsec); 1746 fmtime.dwLowDateTime = wintm.LowPart; 1747 fmtime.dwHighDateTime = wintm.HighPart; 1748 pmt = &fmtime; 1749 } else 1750 pmt = NULL; 1751 if (pat != NULL || pmt != NULL) 1752 r = SetFileTime(h, NULL, pat, pmt); 1753 else 1754 r = 1; 1755 CloseHandle(h); 1756 if (r == 0) { 1757 failure_start(file, line, "Can't SetFileTime %s\n", pathname); 1758 failure_finish(NULL); 1759 return (0); 1760 } 1761 return (1); 1762#else /* defined(_WIN32) && !defined(__CYGWIN__) */ 1763 struct stat st; 1764 struct timeval times[2]; 1765 1766#if !defined(__FreeBSD__) 1767 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */ 1768#endif 1769 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0) 1770 return (1); 1771 1772 r = lstat(pathname, &st); 1773 if (r < 0) { 1774 failure_start(file, line, "Can't stat %s\n", pathname); 1775 failure_finish(NULL); 1776 return (0); 1777 } 1778 1779 if (mt == 0 && mt_nsec == 0) { 1780 mt = st.st_mtime; 1781#if defined(__FreeBSD__) 1782 mt_nsec = st.st_mtimespec.tv_nsec; 1783 /* FreeBSD generally only stores to microsecond res, so round. */ 1784 mt_nsec = (mt_nsec / 1000) * 1000; 1785#endif 1786 } 1787 if (at == 0 && at_nsec == 0) { 1788 at = st.st_atime; 1789#if defined(__FreeBSD__) 1790 at_nsec = st.st_atimespec.tv_nsec; 1791 /* FreeBSD generally only stores to microsecond res, so round. */ 1792 at_nsec = (at_nsec / 1000) * 1000; 1793#endif 1794 } 1795 1796 times[1].tv_sec = mt; 1797 times[1].tv_usec = mt_nsec / 1000; 1798 1799 times[0].tv_sec = at; 1800 times[0].tv_usec = at_nsec / 1000; 1801 1802#ifdef HAVE_LUTIMES 1803 r = lutimes(pathname, times); 1804#else 1805 r = utimes(pathname, times); 1806#endif 1807 if (r < 0) { 1808 failure_start(file, line, "Can't utimes %s\n", pathname); 1809 failure_finish(NULL); 1810 return (0); 1811 } 1812 return (1); 1813#endif /* defined(_WIN32) && !defined(__CYGWIN__) */ 1814} 1815 1816/* Set nodump, report failures. */ 1817int 1818assertion_nodump(const char *file, int line, const char *pathname) 1819{ 1820#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 1821 int r; 1822 1823 assertion_count(file, line); 1824 r = chflags(pathname, UF_NODUMP); 1825 if (r < 0) { 1826 failure_start(file, line, "Can't set nodump %s\n", pathname); 1827 failure_finish(NULL); 1828 return (0); 1829 } 1830#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\ 1831 && defined(EXT2_NODUMP_FL) 1832 int fd, r, flags; 1833 1834 assertion_count(file, line); 1835 fd = open(pathname, O_RDONLY | O_NONBLOCK); 1836 if (fd < 0) { 1837 failure_start(file, line, "Can't open %s\n", pathname); 1838 failure_finish(NULL); 1839 return (0); 1840 } 1841 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags); 1842 if (r < 0) { 1843 failure_start(file, line, "Can't get flags %s\n", pathname); 1844 failure_finish(NULL); 1845 return (0); 1846 } 1847 flags |= EXT2_NODUMP_FL; 1848 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags); 1849 if (r < 0) { 1850 failure_start(file, line, "Can't set nodump %s\n", pathname); 1851 failure_finish(NULL); 1852 return (0); 1853 } 1854 close(fd); 1855#else 1856 (void)pathname; /* UNUSED */ 1857 assertion_count(file, line); 1858#endif 1859 return (1); 1860} 1861 1862/* 1863 * 1864 * UTILITIES for use by tests. 1865 * 1866 */ 1867 1868/* 1869 * Check whether platform supports symlinks. This is intended 1870 * for tests to use in deciding whether to bother testing symlink 1871 * support; if the platform doesn't support symlinks, there's no point 1872 * in checking whether the program being tested can create them. 1873 * 1874 * Note that the first time this test is called, we actually go out to 1875 * disk to create and verify a symlink. This is necessary because 1876 * symlink support is actually a property of a particular filesystem 1877 * and can thus vary between directories on a single system. After 1878 * the first call, this returns the cached result from memory, so it's 1879 * safe to call it as often as you wish. 1880 */ 1881int 1882canSymlink(void) 1883{ 1884 /* Remember the test result */ 1885 static int value = 0, tested = 0; 1886 if (tested) 1887 return (value); 1888 1889 ++tested; 1890 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a"); 1891 /* Note: Cygwin has its own symlink() emulation that does not 1892 * use the Win32 CreateSymbolicLink() function. */ 1893#if defined(_WIN32) && !defined(__CYGWIN__) 1894 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0) 1895 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0"); 1896#elif HAVE_SYMLINK 1897 value = (0 == symlink("canSymlink.0", "canSymlink.1")) 1898 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0"); 1899#endif 1900 return (value); 1901} 1902 1903/* Platform-dependent options for hiding the output of a subcommand. */ 1904#if defined(_WIN32) && !defined(__CYGWIN__) 1905static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */ 1906#else 1907static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */ 1908#endif 1909/* 1910 * Can this platform run the bzip2 program? 1911 */ 1912int 1913canBzip2(void) 1914{ 1915 static int tested = 0, value = 0; 1916 if (!tested) { 1917 tested = 1; 1918 if (systemf("bzip2 -d -V %s", redirectArgs) == 0) 1919 value = 1; 1920 } 1921 return (value); 1922} 1923 1924/* 1925 * Can this platform run the grzip program? 1926 */ 1927int 1928canGrzip(void) 1929{ 1930 static int tested = 0, value = 0; 1931 if (!tested) { 1932 tested = 1; 1933 if (systemf("grzip -V %s", redirectArgs) == 0) 1934 value = 1; 1935 } 1936 return (value); 1937} 1938 1939/* 1940 * Can this platform run the gzip program? 1941 */ 1942int 1943canGzip(void) 1944{ 1945 static int tested = 0, value = 0; 1946 if (!tested) { 1947 tested = 1; 1948 if (systemf("gzip -V %s", redirectArgs) == 0) 1949 value = 1; 1950 } 1951 return (value); 1952} 1953 1954/* 1955 * Can this platform run the lrzip program? 1956 */ 1957int 1958canRunCommand(const char *cmd) 1959{ 1960 static int tested = 0, value = 0; 1961 if (!tested) { 1962 tested = 1; 1963 if (systemf("%s %s", cmd, redirectArgs) == 0) 1964 value = 1; 1965 } 1966 return (value); 1967} 1968 1969int 1970canLrzip(void) 1971{ 1972 static int tested = 0, value = 0; 1973 if (!tested) { 1974 tested = 1; 1975 if (systemf("lrzip -V %s", redirectArgs) == 0) 1976 value = 1; 1977 } 1978 return (value); 1979} 1980 1981/* 1982 * Can this platform run the lz4 program? 1983 */ 1984int 1985canLz4(void) 1986{ 1987 static int tested = 0, value = 0; 1988 if (!tested) { 1989 tested = 1; 1990 if (systemf("lz4 -V %s", redirectArgs) == 0) 1991 value = 1; 1992 } 1993 return (value); 1994} 1995 1996/* 1997 * Can this platform run the lzip program? 1998 */ 1999int 2000canLzip(void) 2001{ 2002 static int tested = 0, value = 0; 2003 if (!tested) { 2004 tested = 1; 2005 if (systemf("lzip -V %s", redirectArgs) == 0) 2006 value = 1; 2007 } 2008 return (value); 2009} 2010 2011/* 2012 * Can this platform run the lzma program? 2013 */ 2014int 2015canLzma(void) 2016{ 2017 static int tested = 0, value = 0; 2018 if (!tested) { 2019 tested = 1; 2020 if (systemf("lzma -V %s", redirectArgs) == 0) 2021 value = 1; 2022 } 2023 return (value); 2024} 2025 2026/* 2027 * Can this platform run the lzop program? 2028 */ 2029int 2030canLzop(void) 2031{ 2032 static int tested = 0, value = 0; 2033 if (!tested) { 2034 tested = 1; 2035 if (systemf("lzop -V %s", redirectArgs) == 0) 2036 value = 1; 2037 } 2038 return (value); 2039} 2040 2041/* 2042 * Can this platform run the xz program? 2043 */ 2044int 2045canXz(void) 2046{ 2047 static int tested = 0, value = 0; 2048 if (!tested) { 2049 tested = 1; 2050 if (systemf("xz -V %s", redirectArgs) == 0) 2051 value = 1; 2052 } 2053 return (value); 2054} 2055 2056/* 2057 * Can this filesystem handle nodump flags. 2058 */ 2059#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2060 2061int 2062canNodump(void) 2063{ 2064 const char *path = "cannodumptest"; 2065 struct stat sb; 2066 2067 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2068 if (chflags(path, UF_NODUMP) < 0) 2069 return (0); 2070 if (stat(path, &sb) < 0) 2071 return (0); 2072 if (sb.st_flags & UF_NODUMP) 2073 return (1); 2074 return (0); 2075} 2076 2077#elif defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)\ 2078 && defined(EXT2_NODUMP_FL) 2079 2080int 2081canNodump(void) 2082{ 2083 const char *path = "cannodumptest"; 2084 int fd, r, flags; 2085 2086 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2087 fd = open(path, O_RDONLY | O_NONBLOCK); 2088 if (fd < 0) 2089 return (0); 2090 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags); 2091 if (r < 0) 2092 return (0); 2093 flags |= EXT2_NODUMP_FL; 2094 r = ioctl(fd, EXT2_IOC_SETFLAGS, &flags); 2095 if (r < 0) 2096 return (0); 2097 close(fd); 2098 fd = open(path, O_RDONLY | O_NONBLOCK); 2099 if (fd < 0) 2100 return (0); 2101 r = ioctl(fd, EXT2_IOC_GETFLAGS, &flags); 2102 if (r < 0) 2103 return (0); 2104 close(fd); 2105 if (flags & EXT2_NODUMP_FL) 2106 return (1); 2107 return (0); 2108} 2109 2110#else 2111 2112int 2113canNodump() 2114{ 2115 return (0); 2116} 2117 2118#endif 2119 2120/* 2121 * Sleep as needed; useful for verifying disk timestamp changes by 2122 * ensuring that the wall-clock time has actually changed before we 2123 * go back to re-read something from disk. 2124 */ 2125void 2126sleepUntilAfter(time_t t) 2127{ 2128 while (t >= time(NULL)) 2129#if defined(_WIN32) && !defined(__CYGWIN__) 2130 Sleep(500); 2131#else 2132 sleep(1); 2133#endif 2134} 2135 2136/* 2137 * Call standard system() call, but build up the command line using 2138 * sprintf() conventions. 2139 */ 2140int 2141systemf(const char *fmt, ...) 2142{ 2143 char buff[8192]; 2144 va_list ap; 2145 int r; 2146 2147 va_start(ap, fmt); 2148 vsprintf(buff, fmt, ap); 2149 if (verbosity > VERBOSITY_FULL) 2150 logprintf("Cmd: %s\n", buff); 2151 r = system(buff); 2152 va_end(ap); 2153 return (r); 2154} 2155 2156/* 2157 * Slurp a file into memory for ease of comparison and testing. 2158 * Returns size of file in 'sizep' if non-NULL, null-terminates 2159 * data in memory for ease of use. 2160 */ 2161char * 2162slurpfile(size_t * sizep, const char *fmt, ...) 2163{ 2164 char filename[8192]; 2165 struct stat st; 2166 va_list ap; 2167 char *p; 2168 ssize_t bytes_read; 2169 FILE *f; 2170 int r; 2171 2172 va_start(ap, fmt); 2173 vsprintf(filename, fmt, ap); 2174 va_end(ap); 2175 2176 f = fopen(filename, "rb"); 2177 if (f == NULL) { 2178 /* Note: No error; non-existent file is okay here. */ 2179 return (NULL); 2180 } 2181 r = fstat(fileno(f), &st); 2182 if (r != 0) { 2183 logprintf("Can't stat file %s\n", filename); 2184 fclose(f); 2185 return (NULL); 2186 } 2187 p = malloc((size_t)st.st_size + 1); 2188 if (p == NULL) { 2189 logprintf("Can't allocate %ld bytes of memory to read file %s\n", 2190 (long int)st.st_size, filename); 2191 fclose(f); 2192 return (NULL); 2193 } 2194 bytes_read = fread(p, 1, (size_t)st.st_size, f); 2195 if (bytes_read < st.st_size) { 2196 logprintf("Can't read file %s\n", filename); 2197 fclose(f); 2198 free(p); 2199 return (NULL); 2200 } 2201 p[st.st_size] = '\0'; 2202 if (sizep != NULL) 2203 *sizep = (size_t)st.st_size; 2204 fclose(f); 2205 return (p); 2206} 2207 2208/* 2209 * Slurp a file into memory for ease of comparison and testing. 2210 * Returns size of file in 'sizep' if non-NULL, null-terminates 2211 * data in memory for ease of use. 2212 */ 2213void 2214dumpfile(const char *filename, void *data, size_t len) 2215{ 2216 ssize_t bytes_written; 2217 FILE *f; 2218 2219 f = fopen(filename, "wb"); 2220 if (f == NULL) { 2221 logprintf("Can't open file %s for writing\n", filename); 2222 return; 2223 } 2224 bytes_written = fwrite(data, 1, len, f); 2225 if (bytes_written < (ssize_t)len) 2226 logprintf("Can't write file %s\n", filename); 2227 fclose(f); 2228} 2229 2230/* Read a uuencoded file from the reference directory, decode, and 2231 * write the result into the current directory. */ 2232#define VALID_UUDECODE(c) (c >= 32 && c <= 96) 2233#define UUDECODE(c) (((c) - 0x20) & 0x3f) 2234void 2235extract_reference_file(const char *name) 2236{ 2237 char buff[1024]; 2238 FILE *in, *out; 2239 2240 sprintf(buff, "%s/%s.uu", refdir, name); 2241 in = fopen(buff, "r"); 2242 failure("Couldn't open reference file %s", buff); 2243 assert(in != NULL); 2244 if (in == NULL) 2245 return; 2246 /* Read up to and including the 'begin' line. */ 2247 for (;;) { 2248 if (fgets(buff, sizeof(buff), in) == NULL) { 2249 /* TODO: This is a failure. */ 2250 return; 2251 } 2252 if (memcmp(buff, "begin ", 6) == 0) 2253 break; 2254 } 2255 /* Now, decode the rest and write it. */ 2256 out = fopen(name, "wb"); 2257 while (fgets(buff, sizeof(buff), in) != NULL) { 2258 char *p = buff; 2259 int bytes; 2260 2261 if (memcmp(buff, "end", 3) == 0) 2262 break; 2263 2264 bytes = UUDECODE(*p++); 2265 while (bytes > 0) { 2266 int n = 0; 2267 /* Write out 1-3 bytes from that. */ 2268 if (bytes > 0) { 2269 assert(VALID_UUDECODE(p[0])); 2270 assert(VALID_UUDECODE(p[1])); 2271 n = UUDECODE(*p++) << 18; 2272 n |= UUDECODE(*p++) << 12; 2273 fputc(n >> 16, out); 2274 --bytes; 2275 } 2276 if (bytes > 0) { 2277 assert(VALID_UUDECODE(p[0])); 2278 n |= UUDECODE(*p++) << 6; 2279 fputc((n >> 8) & 0xFF, out); 2280 --bytes; 2281 } 2282 if (bytes > 0) { 2283 assert(VALID_UUDECODE(p[0])); 2284 n |= UUDECODE(*p++); 2285 fputc(n & 0xFF, out); 2286 --bytes; 2287 } 2288 } 2289 } 2290 fclose(out); 2291 fclose(in); 2292} 2293 2294void 2295copy_reference_file(const char *name) 2296{ 2297 char buff[1024]; 2298 FILE *in, *out; 2299 size_t rbytes; 2300 2301 sprintf(buff, "%s/%s", refdir, name); 2302 in = fopen(buff, "rb"); 2303 failure("Couldn't open reference file %s", buff); 2304 assert(in != NULL); 2305 if (in == NULL) 2306 return; 2307 /* Now, decode the rest and write it. */ 2308 /* Not a lot of error checking here; the input better be right. */ 2309 out = fopen(name, "wb"); 2310 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) { 2311 if (fwrite(buff, 1, rbytes, out) != rbytes) { 2312 logprintf("Error: fwrite\n"); 2313 break; 2314 } 2315 } 2316 fclose(out); 2317 fclose(in); 2318} 2319 2320int 2321is_LargeInode(const char *file) 2322{ 2323#if defined(_WIN32) && !defined(__CYGWIN__) 2324 BY_HANDLE_FILE_INFORMATION bhfi; 2325 int r; 2326 2327 r = my_GetFileInformationByName(file, &bhfi); 2328 if (r != 0) 2329 return (0); 2330 return (bhfi.nFileIndexHigh & 0x0000FFFFUL); 2331#else 2332 struct stat st; 2333 int64_t ino; 2334 2335 if (stat(file, &st) < 0) 2336 return (0); 2337 ino = (int64_t)st.st_ino; 2338 return (ino > 0xffffffff); 2339#endif 2340} 2341 2342void 2343extract_reference_files(const char **names) 2344{ 2345 while (names && *names) 2346 extract_reference_file(*names++); 2347} 2348 2349/* 2350 * 2351 * TEST management 2352 * 2353 */ 2354 2355/* 2356 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has 2357 * a line like 2358 * DEFINE_TEST(test_function) 2359 * for each test. 2360 */ 2361 2362/* Use "list.h" to declare all of the test functions. */ 2363#undef DEFINE_TEST 2364#define DEFINE_TEST(name) void name(void); 2365#include "list.h" 2366 2367/* Use "list.h" to create a list of all tests (functions and names). */ 2368#undef DEFINE_TEST 2369#define DEFINE_TEST(n) { n, #n, 0 }, 2370struct test_list_t tests[] = { 2371 #include "list.h" 2372}; 2373 2374/* 2375 * Summarize repeated failures in the just-completed test. 2376 */ 2377static void 2378test_summarize(int failed, int skips_num) 2379{ 2380 unsigned int i; 2381 2382 switch (verbosity) { 2383 case VERBOSITY_SUMMARY_ONLY: 2384 printf(failed ? "E" : "."); 2385 fflush(stdout); 2386 break; 2387 case VERBOSITY_PASSFAIL: 2388 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n"); 2389 break; 2390 } 2391 2392 log_console = (verbosity == VERBOSITY_LIGHT_REPORT); 2393 2394 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { 2395 if (failed_lines[i].count > 1 && !failed_lines[i].skip) 2396 logprintf("%s:%d: Summary: Failed %d times\n", 2397 failed_filename, i, failed_lines[i].count); 2398 } 2399 /* Clear the failure history for the next file. */ 2400 failed_filename = NULL; 2401 memset(failed_lines, 0, sizeof(failed_lines)); 2402} 2403 2404/* 2405 * Actually run a single test, with appropriate setup and cleanup. 2406 */ 2407static int 2408test_run(int i, const char *tmpdir) 2409{ 2410 char workdir[1024]; 2411 char logfilename[64]; 2412 int failures_before = failures; 2413 int skips_before = skips; 2414 int oldumask; 2415 2416 switch (verbosity) { 2417 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */ 2418 break; 2419 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */ 2420 printf("%3d: %-64s", i, tests[i].name); 2421 fflush(stdout); 2422 break; 2423 default: /* Title of test, details will follow */ 2424 printf("%3d: %s\n", i, tests[i].name); 2425 } 2426 2427 /* Chdir to the top-level work directory. */ 2428 if (!assertChdir(tmpdir)) { 2429 fprintf(stderr, 2430 "ERROR: Can't chdir to top work dir %s\n", tmpdir); 2431 exit(1); 2432 } 2433 /* Create a log file for this test. */ 2434 sprintf(logfilename, "%s.log", tests[i].name); 2435 logfile = fopen(logfilename, "w"); 2436 fprintf(logfile, "%s\n\n", tests[i].name); 2437 /* Chdir() to a work dir for this specific test. */ 2438 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name); 2439 testworkdir = workdir; 2440 if (!assertMakeDir(testworkdir, 0755) 2441 || !assertChdir(testworkdir)) { 2442 fprintf(stderr, 2443 "ERROR: Can't chdir to work dir %s\n", testworkdir); 2444 exit(1); 2445 } 2446 /* Explicitly reset the locale before each test. */ 2447 setlocale(LC_ALL, "C"); 2448 /* Record the umask before we run the test. */ 2449 umask(oldumask = umask(0)); 2450 /* 2451 * Run the actual test. 2452 */ 2453 (*tests[i].func)(); 2454 /* 2455 * Clean up and report afterwards. 2456 */ 2457 testworkdir = NULL; 2458 /* Restore umask */ 2459 umask(oldumask); 2460 /* Reset locale. */ 2461 setlocale(LC_ALL, "C"); 2462 /* Reset directory. */ 2463 if (!assertChdir(tmpdir)) { 2464 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n", 2465 tmpdir); 2466 exit(1); 2467 } 2468 /* Report per-test summaries. */ 2469 tests[i].failures = failures - failures_before; 2470 test_summarize(tests[i].failures, skips - skips_before); 2471 /* Close the per-test log file. */ 2472 fclose(logfile); 2473 logfile = NULL; 2474 /* If there were no failures, we can remove the work dir and logfile. */ 2475 if (tests[i].failures == 0) { 2476 if (!keep_temp_files && assertChdir(tmpdir)) { 2477#if defined(_WIN32) && !defined(__CYGWIN__) 2478 /* Make sure not to leave empty directories. 2479 * Sometimes a processing of closing files used by tests 2480 * is not done, then rmdir will be failed and it will 2481 * leave a empty test directory. So we should wait a few 2482 * seconds and retry rmdir. */ 2483 int r, t; 2484 for (t = 0; t < 10; t++) { 2485 if (t > 0) 2486 Sleep(1000); 2487 r = systemf("rmdir /S /Q %s", tests[i].name); 2488 if (r == 0) 2489 break; 2490 } 2491 systemf("del %s", logfilename); 2492#else 2493 systemf("rm -rf %s", tests[i].name); 2494 systemf("rm %s", logfilename); 2495#endif 2496 } 2497 } 2498 /* Return appropriate status. */ 2499 return (tests[i].failures); 2500} 2501 2502/* 2503 * 2504 * 2505 * MAIN and support routines. 2506 * 2507 * 2508 */ 2509 2510static void 2511usage(const char *program) 2512{ 2513 static const int limit = sizeof(tests) / sizeof(tests[0]); 2514 int i; 2515 2516 printf("Usage: %s [options] <test> <test> ...\n", program); 2517 printf("Default is to run all tests.\n"); 2518 printf("Otherwise, specify the numbers of the tests you wish to run.\n"); 2519 printf("Options:\n"); 2520 printf(" -d Dump core after any failure, for debugging.\n"); 2521 printf(" -k Keep all temp files.\n"); 2522 printf(" Default: temp files for successful tests deleted.\n"); 2523#ifdef PROGRAM 2524 printf(" -p <path> Path to executable to be tested.\n"); 2525 printf(" Default: path taken from " ENVBASE " environment variable.\n"); 2526#endif 2527 printf(" -q Quiet.\n"); 2528 printf(" -r <dir> Path to dir containing reference files.\n"); 2529 printf(" Default: Current directory.\n"); 2530 printf(" -u Keep running specifies tests until one fails.\n"); 2531 printf(" -v Verbose.\n"); 2532 printf("Available tests:\n"); 2533 for (i = 0; i < limit; i++) 2534 printf(" %d: %s\n", i, tests[i].name); 2535 exit(1); 2536} 2537 2538static char * 2539get_refdir(const char *d) 2540{ 2541 size_t tried_size, buff_size; 2542 char *buff, *tried, *pwd = NULL, *p = NULL; 2543 2544#ifdef PATH_MAX 2545 buff_size = PATH_MAX; 2546#else 2547 buff_size = 8192; 2548#endif 2549 buff = calloc(buff_size, 1); 2550 if (buff == NULL) { 2551 fprintf(stderr, "Unable to allocate memory\n"); 2552 exit(1); 2553 } 2554 2555 /* Allocate a buffer to hold the various directories we checked. */ 2556 tried_size = buff_size * 2; 2557 tried = calloc(tried_size, 1); 2558 if (tried == NULL) { 2559 fprintf(stderr, "Unable to allocate memory\n"); 2560 exit(1); 2561 } 2562 2563 /* If a dir was specified, try that */ 2564 if (d != NULL) { 2565 pwd = NULL; 2566 snprintf(buff, buff_size, "%s", d); 2567 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2568 if (p != NULL) goto success; 2569 strncat(tried, buff, tried_size - strlen(tried) - 1); 2570 strncat(tried, "\n", tried_size - strlen(tried) - 1); 2571 goto failure; 2572 } 2573 2574 /* Get the current dir. */ 2575#ifdef PATH_MAX 2576 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 2577#else 2578 pwd = getcwd(NULL, 0); 2579#endif 2580 while (pwd[strlen(pwd) - 1] == '\n') 2581 pwd[strlen(pwd) - 1] = '\0'; 2582 2583 /* Look for a known file. */ 2584 snprintf(buff, buff_size, "%s", pwd); 2585 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2586 if (p != NULL) goto success; 2587 strncat(tried, buff, tried_size - strlen(tried) - 1); 2588 strncat(tried, "\n", tried_size - strlen(tried) - 1); 2589 2590 snprintf(buff, buff_size, "%s/test", pwd); 2591 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2592 if (p != NULL) goto success; 2593 strncat(tried, buff, tried_size - strlen(tried) - 1); 2594 strncat(tried, "\n", tried_size - strlen(tried) - 1); 2595 2596#if defined(LIBRARY) 2597 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY); 2598#else 2599 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM); 2600#endif 2601 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2602 if (p != NULL) goto success; 2603 strncat(tried, buff, tried_size - strlen(tried) - 1); 2604 strncat(tried, "\n", tried_size - strlen(tried) - 1); 2605 2606#if defined(PROGRAM_ALIAS) 2607 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS); 2608 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2609 if (p != NULL) goto success; 2610 strncat(tried, buff, tried_size - strlen(tried) - 1); 2611 strncat(tried, "\n", tried_size - strlen(tried) - 1); 2612#endif 2613 2614 if (memcmp(pwd, "/usr/obj", 8) == 0) { 2615 snprintf(buff, buff_size, "%s", pwd + 8); 2616 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2617 if (p != NULL) goto success; 2618 strncat(tried, buff, tried_size - strlen(tried) - 1); 2619 strncat(tried, "\n", tried_size - strlen(tried) - 1); 2620 2621 snprintf(buff, buff_size, "%s/test", pwd + 8); 2622 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 2623 if (p != NULL) goto success; 2624 strncat(tried, buff, tried_size - strlen(tried) - 1); 2625 strncat(tried, "\n", tried_size - strlen(tried) - 1); 2626 } 2627 2628failure: 2629 printf("Unable to locate known reference file %s\n", KNOWNREF); 2630 printf(" Checked following directories:\n%s\n", tried); 2631 printf("Use -r option to specify full path to reference directory\n"); 2632#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) 2633 DebugBreak(); 2634#endif 2635 exit(1); 2636 2637success: 2638 free(p); 2639 free(pwd); 2640 free(tried); 2641 2642 /* Copy result into a fresh buffer to reduce memory usage. */ 2643 p = strdup(buff); 2644 free(buff); 2645 return p; 2646} 2647 2648int 2649main(int argc, char **argv) 2650{ 2651 static const int limit = sizeof(tests) / sizeof(tests[0]); 2652 int test_set[sizeof(tests) / sizeof(tests[0])]; 2653 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option; 2654 time_t now; 2655 char *refdir_alloc = NULL; 2656 const char *progname; 2657 char **saved_argv; 2658 const char *tmp, *option_arg, *p; 2659 char tmpdir[256], *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL; 2660 char tmpdir_timestamp[256]; 2661 2662 (void)argc; /* UNUSED */ 2663 2664 /* Get the current dir. */ 2665#ifdef PATH_MAX 2666 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 2667#else 2668 pwd = getcwd(NULL, 0); 2669#endif 2670 while (pwd[strlen(pwd) - 1] == '\n') 2671 pwd[strlen(pwd) - 1] = '\0'; 2672 2673#if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 2674 /* To stop to run the default invalid parameter handler. */ 2675 _set_invalid_parameter_handler(invalid_parameter_handler); 2676 /* Disable annoying assertion message box. */ 2677 _CrtSetReportMode(_CRT_ASSERT, 0); 2678#endif 2679 2680 /* 2681 * Name of this program, used to build root of our temp directory 2682 * tree. 2683 */ 2684 progname = p = argv[0]; 2685 if ((testprogdir = (char *)malloc(strlen(progname) + 1)) == NULL) 2686 { 2687 fprintf(stderr, "ERROR: Out of memory."); 2688 exit(1); 2689 } 2690 strcpy(testprogdir, progname); 2691 while (*p != '\0') { 2692 /* Support \ or / dir separators for Windows compat. */ 2693 if (*p == '/' || *p == '\\') 2694 { 2695 progname = p + 1; 2696 i = j; 2697 } 2698 ++p; 2699 j++; 2700 } 2701 testprogdir[i] = '\0'; 2702#if defined(_WIN32) && !defined(__CYGWIN__) 2703 if (testprogdir[0] != '/' && testprogdir[0] != '\\' && 2704 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') || 2705 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) && 2706 testprogdir[1] == ':' && 2707 (testprogdir[2] == '/' || testprogdir[2] == '\\'))) 2708#else 2709 if (testprogdir[0] != '/') 2710#endif 2711 { 2712 /* Fixup path for relative directories. */ 2713 if ((testprogdir = (char *)realloc(testprogdir, 2714 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL) 2715 { 2716 fprintf(stderr, "ERROR: Out of memory."); 2717 exit(1); 2718 } 2719 memmove(testprogdir + strlen(pwd) + 1, testprogdir, 2720 strlen(testprogdir) + 1); 2721 memcpy(testprogdir, pwd, strlen(pwd)); 2722 testprogdir[strlen(pwd)] = '/'; 2723 } 2724 2725#ifdef PROGRAM 2726 /* Get the target program from environment, if available. */ 2727 testprogfile = getenv(ENVBASE); 2728#endif 2729 2730 if (getenv("TMPDIR") != NULL) 2731 tmp = getenv("TMPDIR"); 2732 else if (getenv("TMP") != NULL) 2733 tmp = getenv("TMP"); 2734 else if (getenv("TEMP") != NULL) 2735 tmp = getenv("TEMP"); 2736 else if (getenv("TEMPDIR") != NULL) 2737 tmp = getenv("TEMPDIR"); 2738 else 2739 tmp = "/tmp"; 2740 2741 /* Allow -d to be controlled through the environment. */ 2742 if (getenv(ENVBASE "_DEBUG") != NULL) 2743 dump_on_failure = 1; 2744 2745 /* Allow -v to be controlled through the environment. */ 2746 if (getenv("_VERBOSITY_LEVEL") != NULL) 2747 { 2748 vlevel = getenv("_VERBOSITY_LEVEL"); 2749 verbosity = atoi(vlevel); 2750 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL) 2751 { 2752 /* Unsupported verbosity levels are silently ignored */ 2753 vlevel = NULL; 2754 verbosity = VERBOSITY_PASSFAIL; 2755 } 2756 } 2757 2758 /* Get the directory holding test files from environment. */ 2759 refdir = getenv(ENVBASE "_TEST_FILES"); 2760 2761 /* 2762 * Parse options, without using getopt(), which isn't available 2763 * on all platforms. 2764 */ 2765 ++argv; /* Skip program name */ 2766 while (*argv != NULL) { 2767 if (**argv != '-') 2768 break; 2769 p = *argv++; 2770 ++p; /* Skip '-' */ 2771 while (*p != '\0') { 2772 option = *p++; 2773 option_arg = NULL; 2774 /* If 'opt' takes an argument, parse that. */ 2775 if (option == 'p' || option == 'r') { 2776 if (*p != '\0') 2777 option_arg = p; 2778 else if (*argv == NULL) { 2779 fprintf(stderr, 2780 "Option -%c requires argument.\n", 2781 option); 2782 usage(progname); 2783 } else 2784 option_arg = *argv++; 2785 p = ""; /* End of this option word. */ 2786 } 2787 2788 /* Now, handle the option. */ 2789 switch (option) { 2790 case 'd': 2791 dump_on_failure = 1; 2792 break; 2793 case 'k': 2794 keep_temp_files = 1; 2795 break; 2796 case 'p': 2797#ifdef PROGRAM 2798 testprogfile = option_arg; 2799#else 2800 fprintf(stderr, "-p option not permitted\n"); 2801 usage(progname); 2802#endif 2803 break; 2804 case 'q': 2805 if (!vlevel) 2806 verbosity--; 2807 break; 2808 case 'r': 2809 refdir = option_arg; 2810 break; 2811 case 'u': 2812 until_failure++; 2813 break; 2814 case 'v': 2815 if (!vlevel) 2816 verbosity++; 2817 break; 2818 default: 2819 fprintf(stderr, "Unrecognized option '%c'\n", 2820 option); 2821 usage(progname); 2822 } 2823 } 2824 } 2825 2826 /* 2827 * Sanity-check that our options make sense. 2828 */ 2829#ifdef PROGRAM 2830 if (testprogfile == NULL) 2831 { 2832 if ((tmp2 = (char *)malloc(strlen(testprogdir) + 1 + 2833 strlen(PROGRAM) + 1)) == NULL) 2834 { 2835 fprintf(stderr, "ERROR: Out of memory."); 2836 exit(1); 2837 } 2838 strcpy(tmp2, testprogdir); 2839 strcat(tmp2, "/"); 2840 strcat(tmp2, PROGRAM); 2841 testprogfile = tmp2; 2842 } 2843 2844 { 2845 char *testprg; 2846#if defined(_WIN32) && !defined(__CYGWIN__) 2847 /* Command.com sometimes rejects '/' separators. */ 2848 testprg = strdup(testprogfile); 2849 for (i = 0; testprg[i] != '\0'; i++) { 2850 if (testprg[i] == '/') 2851 testprg[i] = '\\'; 2852 } 2853 testprogfile = testprg; 2854#endif 2855 /* Quote the name that gets put into shell command lines. */ 2856 testprg = malloc(strlen(testprogfile) + 3); 2857 strcpy(testprg, "\""); 2858 strcat(testprg, testprogfile); 2859 strcat(testprg, "\""); 2860 testprog = testprg; 2861 } 2862#endif 2863 2864#if !defined(_WIN32) && defined(SIGPIPE) 2865 { /* Ignore SIGPIPE signals */ 2866 struct sigaction sa; 2867 sa.sa_handler = SIG_IGN; 2868 sigemptyset(&sa.sa_mask); 2869 sa.sa_flags = 0; 2870 sigaction(SIGPIPE, &sa, NULL); 2871 } 2872#endif 2873 2874 /* 2875 * Create a temp directory for the following tests. 2876 * Include the time the tests started as part of the name, 2877 * to make it easier to track the results of multiple tests. 2878 */ 2879 now = time(NULL); 2880 for (i = 0; ; i++) { 2881 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), 2882 "%Y-%m-%dT%H.%M.%S", 2883 localtime(&now)); 2884 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname, 2885 tmpdir_timestamp, i); 2886 if (assertMakeDir(tmpdir,0755)) 2887 break; 2888 if (i >= 999) { 2889 fprintf(stderr, 2890 "ERROR: Unable to create temp directory %s\n", 2891 tmpdir); 2892 exit(1); 2893 } 2894 } 2895 2896 /* 2897 * If the user didn't specify a directory for locating 2898 * reference files, try to find the reference files in 2899 * the "usual places." 2900 */ 2901 refdir = refdir_alloc = get_refdir(refdir); 2902 2903 /* 2904 * Banner with basic information. 2905 */ 2906 printf("\n"); 2907 printf("If tests fail or crash, details will be in:\n"); 2908 printf(" %s\n", tmpdir); 2909 printf("\n"); 2910 if (verbosity > VERBOSITY_SUMMARY_ONLY) { 2911 printf("Reference files will be read from: %s\n", refdir); 2912#ifdef PROGRAM 2913 printf("Running tests on: %s\n", testprog); 2914#endif 2915 printf("Exercising: "); 2916 fflush(stdout); 2917 printf("%s\n", EXTRA_VERSION); 2918 } else { 2919 printf("Running "); 2920 fflush(stdout); 2921 } 2922 2923 /* 2924 * Run some or all of the individual tests. 2925 */ 2926 saved_argv = argv; 2927 do { 2928 argv = saved_argv; 2929 do { 2930 int test_num; 2931 2932 test_num = get_test_set(test_set, limit, *argv, tests); 2933 if (test_num < 0) { 2934 printf("*** INVALID Test %s\n", *argv); 2935 free(refdir_alloc); 2936 free(testprogdir); 2937 usage(progname); 2938 return (1); 2939 } 2940 for (i = 0; i < test_num; i++) { 2941 tests_run++; 2942 if (test_run(test_set[i], tmpdir)) { 2943 tests_failed++; 2944 if (until_failure) 2945 goto finish; 2946 } 2947 } 2948 if (*argv != NULL) 2949 argv++; 2950 } while (*argv != NULL); 2951 } while (until_failure); 2952 2953finish: 2954 /* Must be freed after all tests run */ 2955 free(tmp2); 2956 free(testprogdir); 2957 free(pwd); 2958 2959 /* 2960 * Report summary statistics. 2961 */ 2962 if (verbosity > VERBOSITY_SUMMARY_ONLY) { 2963 printf("\n"); 2964 printf("Totals:\n"); 2965 printf(" Tests run: %8d\n", tests_run); 2966 printf(" Tests failed: %8d\n", tests_failed); 2967 printf(" Assertions checked:%8d\n", assertions); 2968 printf(" Assertions failed: %8d\n", failures); 2969 printf(" Skips reported: %8d\n", skips); 2970 } 2971 if (failures) { 2972 printf("\n"); 2973 printf("Failing tests:\n"); 2974 for (i = 0; i < limit; ++i) { 2975 if (tests[i].failures) 2976 printf(" %d: %s (%d failures)\n", i, 2977 tests[i].name, tests[i].failures); 2978 } 2979 printf("\n"); 2980 printf("Details for failing tests: %s\n", tmpdir); 2981 printf("\n"); 2982 } else { 2983 if (verbosity == VERBOSITY_SUMMARY_ONLY) 2984 printf("\n"); 2985 printf("%d tests passed, no failures\n", tests_run); 2986 } 2987 2988 free(refdir_alloc); 2989 2990 /* If the final tmpdir is empty, we can remove it. */ 2991 /* This should be the usual case when all tests succeed. */ 2992 assertChdir(".."); 2993 rmdir(tmpdir); 2994 2995 return (tests_failed ? 1 : 0); 2996} 2997