main.c revision 228753
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 <errno.h> 28#include <locale.h> 29#include <stdarg.h> 30#include <time.h> 31 32/* 33 * This same file is used pretty much verbatim for all test harnesses. 34 * 35 * The next few lines are the only differences. 36 * TODO: Move this into a separate configuration header, have all test 37 * suites share one copy of this file. 38 */ 39__FBSDID("$FreeBSD: head/lib/libarchive/test/main.c 201247 2009-12-30 05:59:21Z kientzle $"); 40#define KNOWNREF "test_compat_gtar_1.tar.uu" 41#define ENVBASE "LIBARCHIVE" /* Prefix for environment variables. */ 42#undef PROGRAM /* Testing a library, not a program. */ 43#define LIBRARY "libarchive" 44#define EXTRA_DUMP(x) archive_error_string((struct archive *)(x)) 45#define EXTRA_VERSION archive_version() 46 47/* 48 * 49 * Windows support routines 50 * 51 * Note: Configuration is a tricky issue. Using HAVE_* feature macros 52 * in the test harness is dangerous because they cover up 53 * configuration errors. The classic example of this is omitting a 54 * configure check. If libarchive and libarchive_test both look for 55 * the same feature macro, such errors are hard to detect. Platform 56 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can 57 * easily lead to very messy code. It's best to limit yourself 58 * to only the most generic programming techniques in the test harness 59 * and thus avoid conditionals altogether. Where that's not possible, 60 * try to minimize conditionals by grouping platform-specific tests in 61 * one place (e.g., test_acl_freebsd) or by adding new assert() 62 * functions (e.g., assertMakeHardlink()) to cover up platform 63 * differences. Platform-specific coding in libarchive_test is often 64 * a symptom that some capability is missing from libarchive itself. 65 */ 66#if defined(_WIN32) && !defined(__CYGWIN__) 67#include <io.h> 68#include <windows.h> 69#ifndef F_OK 70#define F_OK (0) 71#endif 72#ifndef S_ISDIR 73#define S_ISDIR(m) ((m) & _S_IFDIR) 74#endif 75#ifndef S_ISREG 76#define S_ISREG(m) ((m) & _S_IFREG) 77#endif 78#if !defined(__BORLANDC__) 79#define access _access 80#define chdir _chdir 81#endif 82#ifndef fileno 83#define fileno _fileno 84#endif 85/*#define fstat _fstat64*/ 86#if !defined(__BORLANDC__) 87#define getcwd _getcwd 88#endif 89#define lstat stat 90/*#define lstat _stat64*/ 91/*#define stat _stat64*/ 92#define rmdir _rmdir 93#if !defined(__BORLANDC__) 94#define strdup _strdup 95#define umask _umask 96#endif 97#define int64_t __int64 98#endif 99 100#if defined(HAVE__CrtSetReportMode) 101# include <crtdbg.h> 102#endif 103 104#if defined(_WIN32) && !defined(__CYGWIN__) 105void *GetFunctionKernel32(const char *name) 106{ 107 static HINSTANCE lib; 108 static int set; 109 if (!set) { 110 set = 1; 111 lib = LoadLibrary("kernel32.dll"); 112 } 113 if (lib == NULL) { 114 fprintf(stderr, "Can't load kernel32.dll?!\n"); 115 exit(1); 116 } 117 return (void *)GetProcAddress(lib, name); 118} 119 120static int 121my_CreateSymbolicLinkA(const char *linkname, const char *target, int flags) 122{ 123 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD); 124 static int set; 125 if (!set) { 126 set = 1; 127 f = GetFunctionKernel32("CreateSymbolicLinkA"); 128 } 129 return f == NULL ? 0 : (*f)(linkname, target, flags); 130} 131 132static int 133my_CreateHardLinkA(const char *linkname, const char *target) 134{ 135 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES); 136 static int set; 137 if (!set) { 138 set = 1; 139 f = GetFunctionKernel32("CreateHardLinkA"); 140 } 141 return f == NULL ? 0 : (*f)(linkname, target, NULL); 142} 143 144int 145my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi) 146{ 147 HANDLE h; 148 int r; 149 150 memset(bhfi, 0, sizeof(*bhfi)); 151 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, 152 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 153 if (h == INVALID_HANDLE_VALUE) 154 return (0); 155 r = GetFileInformationByHandle(h, bhfi); 156 CloseHandle(h); 157 return (r); 158} 159#endif 160 161#if defined(HAVE__CrtSetReportMode) 162static void 163invalid_parameter_handler(const wchar_t * expression, 164 const wchar_t * function, const wchar_t * file, 165 unsigned int line, uintptr_t pReserved) 166{ 167 /* nop */ 168} 169#endif 170 171/* 172 * 173 * OPTIONS FLAGS 174 * 175 */ 176 177/* Enable core dump on failure. */ 178static int dump_on_failure = 0; 179/* Default is to remove temp dirs and log data for successful tests. */ 180static int keep_temp_files = 0; 181/* Default is to just report pass/fail for each test. */ 182static int verbosity = 0; 183#define VERBOSITY_SUMMARY_ONLY -1 /* -q */ 184#define VERBOSITY_PASSFAIL 0 /* Default */ 185#define VERBOSITY_LIGHT_REPORT 1 /* -v */ 186#define VERBOSITY_FULL 2 /* -vv */ 187/* A few places generate even more output for verbosity > VERBOSITY_FULL, 188 * mostly for debugging the test harness itself. */ 189/* Cumulative count of assertion failures. */ 190static int failures = 0; 191/* Cumulative count of reported skips. */ 192static int skips = 0; 193/* Cumulative count of assertions checked. */ 194static int assertions = 0; 195 196/* Directory where uuencoded reference files can be found. */ 197static const char *refdir; 198 199/* 200 * Report log information selectively to console and/or disk log. 201 */ 202static int log_console = 0; 203static FILE *logfile; 204static void 205vlogprintf(const char *fmt, va_list ap) 206{ 207#ifdef va_copy 208 va_list lfap; 209 va_copy(lfap, ap); 210#endif 211 if (log_console) 212 vfprintf(stdout, fmt, ap); 213 if (logfile != NULL) 214#ifdef va_copy 215 vfprintf(logfile, fmt, lfap); 216 va_end(lfap); 217#else 218 vfprintf(logfile, fmt, ap); 219#endif 220} 221 222static void 223logprintf(const char *fmt, ...) 224{ 225 va_list ap; 226 va_start(ap, fmt); 227 vlogprintf(fmt, ap); 228 va_end(ap); 229} 230 231/* Set up a message to display only if next assertion fails. */ 232static char msgbuff[4096]; 233static const char *msg, *nextmsg; 234void 235failure(const char *fmt, ...) 236{ 237 va_list ap; 238 va_start(ap, fmt); 239 vsprintf(msgbuff, fmt, ap); 240 va_end(ap); 241 nextmsg = msgbuff; 242} 243 244/* 245 * Copy arguments into file-local variables. 246 * This was added to permit vararg assert() functions without needing 247 * variadic wrapper macros. Turns out that the vararg capability is almost 248 * never used, so almost all of the vararg assertions can be simplified 249 * by removing the vararg capability and reworking the wrapper macro to 250 * pass __FILE__, __LINE__ directly into the function instead of using 251 * this hook. I suspect this machinery is used so rarely that we 252 * would be better off just removing it entirely. That would simplify 253 * the code here noticably. 254 */ 255static const char *test_filename; 256static int test_line; 257static void *test_extra; 258void assertion_setup(const char *filename, int line) 259{ 260 test_filename = filename; 261 test_line = line; 262} 263 264/* Called at the beginning of each assert() function. */ 265static void 266assertion_count(const char *file, int line) 267{ 268 (void)file; /* UNUSED */ 269 (void)line; /* UNUSED */ 270 ++assertions; 271 /* Proper handling of "failure()" message. */ 272 msg = nextmsg; 273 nextmsg = NULL; 274 /* Uncomment to print file:line after every assertion. 275 * Verbose, but occasionally useful in tracking down crashes. */ 276 /* printf("Checked %s:%d\n", file, line); */ 277} 278 279/* 280 * For each test source file, we remember how many times each 281 * assertion was reported. Cleared before each new test, 282 * used by test_summarize(). 283 */ 284static struct line { 285 int count; 286 int skip; 287} failed_lines[10000]; 288 289/* Count this failure, setup up log destination and handle initial report. */ 290static void 291failure_start(const char *filename, int line, const char *fmt, ...) 292{ 293 va_list ap; 294 295 /* Record another failure for this line. */ 296 ++failures; 297 /* test_filename = filename; */ 298 failed_lines[line].count++; 299 300 /* Determine whether to log header to console. */ 301 switch (verbosity) { 302 case VERBOSITY_LIGHT_REPORT: 303 log_console = (failed_lines[line].count < 2); 304 break; 305 default: 306 log_console = (verbosity >= VERBOSITY_FULL); 307 } 308 309 /* Log file:line header for this failure */ 310 va_start(ap, fmt); 311#if _MSC_VER 312 logprintf("%s(%d): ", filename, line); 313#else 314 logprintf("%s:%d: ", filename, line); 315#endif 316 vlogprintf(fmt, ap); 317 va_end(ap); 318 logprintf("\n"); 319 320 if (msg != NULL && msg[0] != '\0') { 321 logprintf(" Description: %s\n", msg); 322 msg = NULL; 323 } 324 325 /* Determine whether to log details to console. */ 326 if (verbosity == VERBOSITY_LIGHT_REPORT) 327 log_console = 0; 328} 329 330/* Complete reporting of failed tests. */ 331/* 332 * The 'extra' hook here is used by libarchive to include libarchive 333 * error messages with assertion failures. It could also be used 334 * to add strerror() output, for example. Just define the EXTRA_DUMP() 335 * macro appropriately. 336 */ 337static void 338failure_finish(void *extra) 339{ 340 (void)extra; /* UNUSED (maybe) */ 341#ifdef EXTRA_DUMP 342 if (extra != NULL) 343 logprintf(" detail: %s\n", EXTRA_DUMP(extra)); 344#endif 345 346 if (dump_on_failure) { 347 fprintf(stderr, 348 " *** forcing core dump so failure can be debugged ***\n"); 349 abort(); 350 exit(1); 351 } 352} 353 354/* Inform user that we're skipping some checks. */ 355void 356test_skipping(const char *fmt, ...) 357{ 358 char buff[1024]; 359 va_list ap; 360 361 va_start(ap, fmt); 362 vsprintf(buff, fmt, ap); 363 va_end(ap); 364 /* failure_start() isn't quite right, but is awfully convenient. */ 365 failure_start(test_filename, test_line, "SKIPPING: %s", buff); 366 --failures; /* Undo failures++ in failure_start() */ 367 /* Don't failure_finish() here. */ 368 /* Mark as skip, so doesn't count as failed test. */ 369 failed_lines[test_line].skip = 1; 370 ++skips; 371} 372 373/* 374 * 375 * ASSERTIONS 376 * 377 */ 378 379/* Generic assert() just displays the failed condition. */ 380int 381assertion_assert(const char *file, int line, int value, 382 const char *condition, void *extra) 383{ 384 assertion_count(file, line); 385 if (!value) { 386 failure_start(file, line, "Assertion failed: %s", condition); 387 failure_finish(extra); 388 } 389 return (value); 390} 391 392/* chdir() and report any errors */ 393int 394assertion_chdir(const char *file, int line, const char *pathname) 395{ 396 assertion_count(file, line); 397 if (chdir(pathname) == 0) 398 return (1); 399 failure_start(file, line, "chdir(\"%s\")", pathname); 400 failure_finish(NULL); 401 return (0); 402 403} 404 405/* Verify two integers are equal. */ 406int 407assertion_equal_int(const char *file, int line, 408 long long v1, const char *e1, long long v2, const char *e2, void *extra) 409{ 410 assertion_count(file, line); 411 if (v1 == v2) 412 return (1); 413 failure_start(file, line, "%s != %s", e1, e2); 414 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1); 415 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2); 416 failure_finish(extra); 417 return (0); 418} 419 420static void strdump(const char *e, const char *p) 421{ 422 const char *q = p; 423 424 logprintf(" %s = ", e); 425 if (p == NULL) { 426 logprintf("NULL"); 427 return; 428 } 429 logprintf("\""); 430 while (*p != '\0') { 431 unsigned int c = 0xff & *p++; 432 switch (c) { 433 case '\a': printf("\a"); break; 434 case '\b': printf("\b"); break; 435 case '\n': printf("\n"); break; 436 case '\r': printf("\r"); break; 437 default: 438 if (c >= 32 && c < 127) 439 logprintf("%c", c); 440 else 441 logprintf("\\x%02X", c); 442 } 443 } 444 logprintf("\""); 445 logprintf(" (length %d)\n", q == NULL ? -1 : (int)strlen(q)); 446} 447 448/* Verify two strings are equal, dump them if not. */ 449int 450assertion_equal_string(const char *file, int line, 451 const char *v1, const char *e1, 452 const char *v2, const char *e2, 453 void *extra) 454{ 455 assertion_count(file, line); 456 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0)) 457 return (1); 458 failure_start(file, line, "%s != %s", e1, e2); 459 strdump(e1, v1); 460 strdump(e2, v2); 461 failure_finish(extra); 462 return (0); 463} 464 465static void 466wcsdump(const char *e, const wchar_t *w) 467{ 468 logprintf(" %s = ", e); 469 if (w == NULL) { 470 logprintf("(null)"); 471 return; 472 } 473 logprintf("\""); 474 while (*w != L'\0') { 475 unsigned int c = *w++; 476 if (c >= 32 && c < 127) 477 logprintf("%c", c); 478 else if (c < 256) 479 logprintf("\\x%02X", c); 480 else if (c < 0x10000) 481 logprintf("\\u%04X", c); 482 else 483 logprintf("\\U%08X", c); 484 } 485 logprintf("\"\n"); 486} 487 488#ifndef HAVE_WCSCMP 489static int 490wcscmp(const wchar_t *s1, const wchar_t *s2) 491{ 492 493 while (*s1 == *s2++) { 494 if (*s1++ == L'\0') 495 return 0; 496 } 497 if (*s1 > *--s2) 498 return 1; 499 else 500 return -1; 501} 502#endif 503 504/* Verify that two wide strings are equal, dump them if not. */ 505int 506assertion_equal_wstring(const char *file, int line, 507 const wchar_t *v1, const char *e1, 508 const wchar_t *v2, const char *e2, 509 void *extra) 510{ 511 assertion_count(file, line); 512 if (v1 == v2 || wcscmp(v1, v2) == 0) 513 return (1); 514 failure_start(file, line, "%s != %s", e1, e2); 515 wcsdump(e1, v1); 516 wcsdump(e2, v2); 517 failure_finish(extra); 518 return (0); 519} 520 521/* 522 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then 523 * any bytes in p that differ from ref will be highlighted with '_' 524 * before and after the hex value. 525 */ 526static void 527hexdump(const char *p, const char *ref, size_t l, size_t offset) 528{ 529 size_t i, j; 530 char sep; 531 532 if (p == NULL) { 533 logprintf("(null)\n"); 534 return; 535 } 536 for(i=0; i < l; i+=16) { 537 logprintf("%04x", (unsigned)(i + offset)); 538 sep = ' '; 539 for (j = 0; j < 16 && i + j < l; j++) { 540 if (ref != NULL && p[i + j] != ref[i + j]) 541 sep = '_'; 542 logprintf("%c%02x", sep, 0xff & (int)p[i+j]); 543 if (ref != NULL && p[i + j] == ref[i + j]) 544 sep = ' '; 545 } 546 for (; j < 16; j++) { 547 logprintf("%c ", sep); 548 sep = ' '; 549 } 550 logprintf("%c", sep); 551 for (j=0; j < 16 && i + j < l; j++) { 552 int c = p[i + j]; 553 if (c >= ' ' && c <= 126) 554 logprintf("%c", c); 555 else 556 logprintf("."); 557 } 558 logprintf("\n"); 559 } 560} 561 562/* Verify that two blocks of memory are the same, display the first 563 * block of differences if they're not. */ 564int 565assertion_equal_mem(const char *file, int line, 566 const void *_v1, const char *e1, 567 const void *_v2, const char *e2, 568 size_t l, const char *ld, void *extra) 569{ 570 const char *v1 = (const char *)_v1; 571 const char *v2 = (const char *)_v2; 572 size_t offset; 573 574 assertion_count(file, line); 575 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0)) 576 return (1); 577 578 failure_start(file, line, "%s != %s", e1, e2); 579 logprintf(" size %s = %d\n", ld, (int)l); 580 /* Dump 48 bytes (3 lines) so that the first difference is 581 * in the second line. */ 582 offset = 0; 583 while (l > 64 && memcmp(v1, v2, 32) == 0) { 584 /* Two lines agree, so step forward one line. */ 585 v1 += 16; 586 v2 += 16; 587 l -= 16; 588 offset += 16; 589 } 590 logprintf(" Dump of %s\n", e1); 591 hexdump(v1, v2, l < 64 ? l : 64, offset); 592 logprintf(" Dump of %s\n", e2); 593 hexdump(v2, v1, l < 64 ? l : 64, offset); 594 logprintf("\n"); 595 failure_finish(extra); 596 return (0); 597} 598 599/* Verify that the named file exists and is empty. */ 600int 601assertion_empty_file(const char *f1fmt, ...) 602{ 603 char buff[1024]; 604 char f1[1024]; 605 struct stat st; 606 va_list ap; 607 ssize_t s; 608 FILE *f; 609 610 assertion_count(test_filename, test_line); 611 va_start(ap, f1fmt); 612 vsprintf(f1, f1fmt, ap); 613 va_end(ap); 614 615 if (stat(f1, &st) != 0) { 616 failure_start(test_filename, test_line, "Stat failed: %s", f1); 617 failure_finish(NULL); 618 return (0); 619 } 620 if (st.st_size == 0) 621 return (1); 622 623 failure_start(test_filename, test_line, "File should be empty: %s", f1); 624 logprintf(" File size: %d\n", (int)st.st_size); 625 logprintf(" Contents:\n"); 626 f = fopen(f1, "rb"); 627 if (f == NULL) { 628 logprintf(" Unable to open %s\n", f1); 629 } else { 630 s = ((off_t)sizeof(buff) < st.st_size) ? 631 (ssize_t)sizeof(buff) : (ssize_t)st.st_size; 632 s = fread(buff, 1, s, f); 633 hexdump(buff, NULL, s, 0); 634 fclose(f); 635 } 636 failure_finish(NULL); 637 return (0); 638} 639 640/* Verify that the named file exists and is not empty. */ 641int 642assertion_non_empty_file(const char *f1fmt, ...) 643{ 644 char f1[1024]; 645 struct stat st; 646 va_list ap; 647 648 assertion_count(test_filename, test_line); 649 va_start(ap, f1fmt); 650 vsprintf(f1, f1fmt, ap); 651 va_end(ap); 652 653 if (stat(f1, &st) != 0) { 654 failure_start(test_filename, test_line, "Stat failed: %s", f1); 655 failure_finish(NULL); 656 return (0); 657 } 658 if (st.st_size == 0) { 659 failure_start(test_filename, test_line, "File empty: %s", f1); 660 failure_finish(NULL); 661 return (0); 662 } 663 return (1); 664} 665 666/* Verify that two files have the same contents. */ 667/* TODO: hexdump the first bytes that actually differ. */ 668int 669assertion_equal_file(const char *fn1, const char *f2pattern, ...) 670{ 671 char fn2[1024]; 672 va_list ap; 673 char buff1[1024]; 674 char buff2[1024]; 675 FILE *f1, *f2; 676 int n1, n2; 677 678 assertion_count(test_filename, test_line); 679 va_start(ap, f2pattern); 680 vsprintf(fn2, f2pattern, ap); 681 va_end(ap); 682 683 f1 = fopen(fn1, "rb"); 684 f2 = fopen(fn2, "rb"); 685 for (;;) { 686 n1 = fread(buff1, 1, sizeof(buff1), f1); 687 n2 = fread(buff2, 1, sizeof(buff2), f2); 688 if (n1 != n2) 689 break; 690 if (n1 == 0 && n2 == 0) { 691 fclose(f1); 692 fclose(f2); 693 return (1); 694 } 695 if (memcmp(buff1, buff2, n1) != 0) 696 break; 697 } 698 fclose(f1); 699 fclose(f2); 700 failure_start(test_filename, test_line, "Files not identical"); 701 logprintf(" file1=\"%s\"\n", fn1); 702 logprintf(" file2=\"%s\"\n", fn2); 703 failure_finish(test_extra); 704 return (0); 705} 706 707/* Verify that the named file does exist. */ 708int 709assertion_file_exists(const char *fpattern, ...) 710{ 711 char f[1024]; 712 va_list ap; 713 714 assertion_count(test_filename, test_line); 715 va_start(ap, fpattern); 716 vsprintf(f, fpattern, ap); 717 va_end(ap); 718 719#if defined(_WIN32) && !defined(__CYGWIN__) 720 if (!_access(f, 0)) 721 return (1); 722#else 723 if (!access(f, F_OK)) 724 return (1); 725#endif 726 failure_start(test_filename, test_line, "File should exist: %s", f); 727 failure_finish(test_extra); 728 return (0); 729} 730 731/* Verify that the named file doesn't exist. */ 732int 733assertion_file_not_exists(const char *fpattern, ...) 734{ 735 char f[1024]; 736 va_list ap; 737 738 assertion_count(test_filename, test_line); 739 va_start(ap, fpattern); 740 vsprintf(f, fpattern, ap); 741 va_end(ap); 742 743#if defined(_WIN32) && !defined(__CYGWIN__) 744 if (_access(f, 0)) 745 return (1); 746#else 747 if (access(f, F_OK)) 748 return (1); 749#endif 750 failure_start(test_filename, test_line, "File should not exist: %s", f); 751 failure_finish(test_extra); 752 return (0); 753} 754 755/* Compare the contents of a file to a block of memory. */ 756int 757assertion_file_contents(const void *buff, int s, const char *fpattern, ...) 758{ 759 char fn[1024]; 760 va_list ap; 761 char *contents; 762 FILE *f; 763 int n; 764 765 assertion_count(test_filename, test_line); 766 va_start(ap, fpattern); 767 vsprintf(fn, fpattern, ap); 768 va_end(ap); 769 770 f = fopen(fn, "rb"); 771 if (f == NULL) { 772 failure_start(test_filename, test_line, 773 "File should exist: %s", fn); 774 failure_finish(test_extra); 775 return (0); 776 } 777 contents = malloc(s * 2); 778 n = fread(contents, 1, s * 2, f); 779 fclose(f); 780 if (n == s && memcmp(buff, contents, s) == 0) { 781 free(contents); 782 return (1); 783 } 784 failure_start(test_filename, test_line, "File contents don't match"); 785 logprintf(" file=\"%s\"\n", fn); 786 if (n > 0) 787 hexdump(contents, buff, n > 512 ? 512 : n, 0); 788 else { 789 logprintf(" File empty, contents should be:\n"); 790 hexdump(buff, NULL, s > 512 ? 512 : n, 0); 791 } 792 failure_finish(test_extra); 793 free(contents); 794 return (0); 795} 796 797/* Check the contents of a text file, being tolerant of line endings. */ 798int 799assertion_text_file_contents(const char *buff, const char *fn) 800{ 801 char *contents; 802 const char *btxt, *ftxt; 803 FILE *f; 804 int n, s; 805 806 assertion_count(test_filename, test_line); 807 f = fopen(fn, "r"); 808 s = strlen(buff); 809 contents = malloc(s * 2 + 128); 810 n = fread(contents, 1, s * 2 + 128 - 1, f); 811 if (n >= 0) 812 contents[n] = '\0'; 813 fclose(f); 814 /* Compare texts. */ 815 btxt = buff; 816 ftxt = (const char *)contents; 817 while (*btxt != '\0' && *ftxt != '\0') { 818 if (*btxt == *ftxt) { 819 ++btxt; 820 ++ftxt; 821 continue; 822 } 823 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') { 824 /* Pass over different new line characters. */ 825 ++btxt; 826 ftxt += 2; 827 continue; 828 } 829 break; 830 } 831 if (*btxt == '\0' && *ftxt == '\0') { 832 free(contents); 833 return (1); 834 } 835 failure_start(test_filename, test_line, "Contents don't match"); 836 logprintf(" file=\"%s\"\n", fn); 837 if (n > 0) 838 hexdump(contents, buff, n, 0); 839 else { 840 logprintf(" File empty, contents should be:\n"); 841 hexdump(buff, NULL, s, 0); 842 } 843 failure_finish(test_extra); 844 free(contents); 845 return (0); 846} 847 848/* Test that two paths point to the same file. */ 849/* As a side-effect, asserts that both files exist. */ 850static int 851is_hardlink(const char *file, int line, 852 const char *path1, const char *path2) 853{ 854#if defined(_WIN32) && !defined(__CYGWIN__) 855 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2; 856 int r; 857 858 assertion_count(file, line); 859 r = my_GetFileInformationByName(path1, &bhfi1); 860 if (r == 0) { 861 failure_start(file, line, "File %s can't be inspected?", path1); 862 failure_finish(NULL); 863 return (0); 864 } 865 r = my_GetFileInformationByName(path2, &bhfi2); 866 if (r == 0) { 867 failure_start(file, line, "File %s can't be inspected?", path2); 868 failure_finish(NULL); 869 return (0); 870 } 871 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber 872 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh 873 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow); 874#else 875 struct stat st1, st2; 876 int r; 877 878 assertion_count(file, line); 879 r = lstat(path1, &st1); 880 if (r != 0) { 881 failure_start(file, line, "File should exist: %s", path1); 882 failure_finish(NULL); 883 return (0); 884 } 885 r = lstat(path2, &st2); 886 if (r != 0) { 887 failure_start(file, line, "File should exist: %s", path2); 888 failure_finish(NULL); 889 return (0); 890 } 891 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev); 892#endif 893} 894 895int 896assertion_is_hardlink(const char *file, int line, 897 const char *path1, const char *path2) 898{ 899 if (is_hardlink(file, line, path1, path2)) 900 return (1); 901 failure_start(file, line, 902 "Files %s and %s are not hardlinked", path1, path2); 903 failure_finish(NULL); 904 return (0); 905} 906 907int 908assertion_is_not_hardlink(const char *file, int line, 909 const char *path1, const char *path2) 910{ 911 if (!is_hardlink(file, line, path1, path2)) 912 return (1); 913 failure_start(file, line, 914 "Files %s and %s should not be hardlinked", path1, path2); 915 failure_finish(NULL); 916 return (0); 917} 918 919/* Verify a/b/mtime of 'pathname'. */ 920/* If 'recent', verify that it's within last 10 seconds. */ 921static int 922assertion_file_time(const char *file, int line, 923 const char *pathname, long t, long nsec, char type, int recent) 924{ 925 long long filet, filet_nsec; 926 int r; 927 928#if defined(_WIN32) && !defined(__CYGWIN__) 929#define EPOC_TIME (116444736000000000ULL) 930 FILETIME ftime, fbirthtime, fatime, fmtime; 931 ULARGE_INTEGER wintm; 932 HANDLE h; 933 ftime.dwLowDateTime = 0; 934 ftime.dwHighDateTime = 0; 935 936 assertion_count(file, line); 937 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL, 938 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 939 if (h == INVALID_HANDLE_VALUE) { 940 failure_start(file, line, "Can't access %s\n", pathname); 941 failure_finish(NULL); 942 return (0); 943 } 944 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime); 945 switch (type) { 946 case 'a': ftime = fatime; break; 947 case 'b': ftime = fbirthtime; break; 948 case 'm': ftime = fmtime; break; 949 } 950 CloseHandle(h); 951 if (r == 0) { 952 failure_start(file, line, "Can't GetFileTime %s\n", pathname); 953 failure_finish(NULL); 954 return (0); 955 } 956 wintm.LowPart = ftime.dwLowDateTime; 957 wintm.HighPart = ftime.dwHighDateTime; 958 filet = (wintm.QuadPart - EPOC_TIME) / 10000000; 959 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100; 960 nsec = (nsec / 100) * 100; /* Round the request */ 961#else 962 struct stat st; 963 964 assertion_count(file, line); 965 r = lstat(pathname, &st); 966 if (r != 0) { 967 failure_start(file, line, "Can't stat %s\n", pathname); 968 failure_finish(NULL); 969 return (0); 970 } 971 switch (type) { 972 case 'a': filet = st.st_atime; break; 973 case 'm': filet = st.st_mtime; break; 974 case 'b': filet = 0; break; 975 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 976 exit(1); 977 } 978#if defined(__FreeBSD__) 979 switch (type) { 980 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break; 981 case 'b': filet = st.st_birthtime; 982 filet_nsec = st.st_birthtimespec.tv_nsec; break; 983 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break; 984 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 985 exit(1); 986 } 987 /* FreeBSD generally only stores to microsecond res, so round. */ 988 filet_nsec = (filet_nsec / 1000) * 1000; 989 nsec = (nsec / 1000) * 1000; 990#else 991 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */ 992 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */ 993#if defined(__HAIKU__) 994 if (type == 'a') return (1); /* Haiku doesn't have atime. */ 995#endif 996#endif 997#endif 998 if (recent) { 999 /* Check that requested time is up-to-date. */ 1000 time_t now = time(NULL); 1001 if (filet < now - 10 || filet > now + 1) { 1002 failure_start(file, line, 1003 "File %s has %ctime %ld, %ld seconds ago\n", 1004 pathname, type, filet, now - filet); 1005 failure_finish(NULL); 1006 return (0); 1007 } 1008 } else if (filet != t || filet_nsec != nsec) { 1009 failure_start(file, line, 1010 "File %s has %ctime %ld.%09ld, expected %ld.%09ld", 1011 pathname, type, filet, filet_nsec, t, nsec); 1012 failure_finish(NULL); 1013 return (0); 1014 } 1015 return (1); 1016} 1017 1018/* Verify atime of 'pathname'. */ 1019int 1020assertion_file_atime(const char *file, int line, 1021 const char *pathname, long t, long nsec) 1022{ 1023 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0); 1024} 1025 1026/* Verify atime of 'pathname' is up-to-date. */ 1027int 1028assertion_file_atime_recent(const char *file, int line, const char *pathname) 1029{ 1030 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1); 1031} 1032 1033/* Verify birthtime of 'pathname'. */ 1034int 1035assertion_file_birthtime(const char *file, int line, 1036 const char *pathname, long t, long nsec) 1037{ 1038 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0); 1039} 1040 1041/* Verify birthtime of 'pathname' is up-to-date. */ 1042int 1043assertion_file_birthtime_recent(const char *file, int line, 1044 const char *pathname) 1045{ 1046 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1); 1047} 1048 1049/* Verify mtime of 'pathname'. */ 1050int 1051assertion_file_mtime(const char *file, int line, 1052 const char *pathname, long t, long nsec) 1053{ 1054 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0); 1055} 1056 1057/* Verify mtime of 'pathname' is up-to-date. */ 1058int 1059assertion_file_mtime_recent(const char *file, int line, const char *pathname) 1060{ 1061 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1); 1062} 1063 1064/* Verify number of links to 'pathname'. */ 1065int 1066assertion_file_nlinks(const char *file, int line, 1067 const char *pathname, int nlinks) 1068{ 1069#if defined(_WIN32) && !defined(__CYGWIN__) 1070 BY_HANDLE_FILE_INFORMATION bhfi; 1071 int r; 1072 1073 assertion_count(file, line); 1074 r = my_GetFileInformationByName(pathname, &bhfi); 1075 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks) 1076 return (1); 1077 failure_start(file, line, "File %s has %d links, expected %d", 1078 pathname, bhfi.nNumberOfLinks, nlinks); 1079 failure_finish(NULL); 1080 return (0); 1081#else 1082 struct stat st; 1083 int r; 1084 1085 assertion_count(file, line); 1086 r = lstat(pathname, &st); 1087 if (r == 0 && st.st_nlink == nlinks) 1088 return (1); 1089 failure_start(file, line, "File %s has %d links, expected %d", 1090 pathname, st.st_nlink, nlinks); 1091 failure_finish(NULL); 1092 return (0); 1093#endif 1094} 1095 1096/* Verify size of 'pathname'. */ 1097int 1098assertion_file_size(const char *file, int line, const char *pathname, long size) 1099{ 1100 int64_t filesize; 1101 int r; 1102 1103 assertion_count(file, line); 1104#if defined(_WIN32) && !defined(__CYGWIN__) 1105 { 1106 BY_HANDLE_FILE_INFORMATION bhfi; 1107 r = !my_GetFileInformationByName(pathname, &bhfi); 1108 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow; 1109 } 1110#else 1111 { 1112 struct stat st; 1113 r = lstat(pathname, &st); 1114 filesize = st.st_size; 1115 } 1116#endif 1117 if (r == 0 && filesize == size) 1118 return (1); 1119 failure_start(file, line, "File %s has size %ld, expected %ld", 1120 pathname, (long)filesize, (long)size); 1121 failure_finish(NULL); 1122 return (0); 1123} 1124 1125/* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */ 1126int 1127assertion_is_dir(const char *file, int line, const char *pathname, int mode) 1128{ 1129 struct stat st; 1130 int r; 1131 1132#if defined(_WIN32) && !defined(__CYGWIN__) 1133 (void)mode; /* UNUSED */ 1134#endif 1135 assertion_count(file, line); 1136 r = lstat(pathname, &st); 1137 if (r != 0) { 1138 failure_start(file, line, "Dir should exist: %s", pathname); 1139 failure_finish(NULL); 1140 return (0); 1141 } 1142 if (!S_ISDIR(st.st_mode)) { 1143 failure_start(file, line, "%s is not a dir", pathname); 1144 failure_finish(NULL); 1145 return (0); 1146 } 1147#if !defined(_WIN32) || defined(__CYGWIN__) 1148 /* Windows doesn't handle permissions the same way as POSIX, 1149 * so just ignore the mode tests. */ 1150 /* TODO: Can we do better here? */ 1151 if (mode >= 0 && mode != (st.st_mode & 07777)) { 1152 failure_start(file, line, "Dir %s has wrong mode", pathname); 1153 logprintf(" Expected: 0%3o\n", mode); 1154 logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1155 failure_finish(NULL); 1156 return (0); 1157 } 1158#endif 1159 return (1); 1160} 1161 1162/* Verify that 'pathname' is a regular file. If 'mode' is >= 0, 1163 * verify that too. */ 1164int 1165assertion_is_reg(const char *file, int line, const char *pathname, int mode) 1166{ 1167 struct stat st; 1168 int r; 1169 1170#if defined(_WIN32) && !defined(__CYGWIN__) 1171 (void)mode; /* UNUSED */ 1172#endif 1173 assertion_count(file, line); 1174 r = lstat(pathname, &st); 1175 if (r != 0 || !S_ISREG(st.st_mode)) { 1176 failure_start(file, line, "File should exist: %s", pathname); 1177 failure_finish(NULL); 1178 return (0); 1179 } 1180#if !defined(_WIN32) || defined(__CYGWIN__) 1181 /* Windows doesn't handle permissions the same way as POSIX, 1182 * so just ignore the mode tests. */ 1183 /* TODO: Can we do better here? */ 1184 if (mode >= 0 && mode != (st.st_mode & 07777)) { 1185 failure_start(file, line, "File %s has wrong mode", pathname); 1186 logprintf(" Expected: 0%3o\n", mode); 1187 logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1188 failure_finish(NULL); 1189 return (0); 1190 } 1191#endif 1192 return (1); 1193} 1194 1195/* Check whether 'pathname' is a symbolic link. If 'contents' is 1196 * non-NULL, verify that the symlink has those contents. */ 1197static int 1198is_symlink(const char *file, int line, 1199 const char *pathname, const char *contents) 1200{ 1201#if defined(_WIN32) && !defined(__CYGWIN__) 1202 (void)pathname; /* UNUSED */ 1203 (void)contents; /* UNUSED */ 1204 assertion_count(file, line); 1205 /* Windows sort-of has real symlinks, but they're only usable 1206 * by privileged users and are crippled even then, so there's 1207 * really not much point in bothering with this. */ 1208 return (0); 1209#else 1210 char buff[300]; 1211 struct stat st; 1212 ssize_t linklen; 1213 int r; 1214 1215 assertion_count(file, line); 1216 r = lstat(pathname, &st); 1217 if (r != 0) { 1218 failure_start(file, line, 1219 "Symlink should exist: %s", pathname); 1220 failure_finish(NULL); 1221 return (0); 1222 } 1223 if (!S_ISLNK(st.st_mode)) 1224 return (0); 1225 if (contents == NULL) 1226 return (1); 1227 linklen = readlink(pathname, buff, sizeof(buff)); 1228 if (linklen < 0) { 1229 failure_start(file, line, "Can't read symlink %s", pathname); 1230 failure_finish(NULL); 1231 return (0); 1232 } 1233 buff[linklen] = '\0'; 1234 if (strcmp(buff, contents) != 0) 1235 return (0); 1236 return (1); 1237#endif 1238} 1239 1240/* Assert that path is a symlink that (optionally) contains contents. */ 1241int 1242assertion_is_symlink(const char *file, int line, 1243 const char *path, const char *contents) 1244{ 1245 if (is_symlink(file, line, path, contents)) 1246 return (1); 1247 if (contents) 1248 failure_start(file, line, "File %s is not a symlink to %s", 1249 path, contents); 1250 else 1251 failure_start(file, line, "File %s is not a symlink", path); 1252 failure_finish(NULL); 1253 return (0); 1254} 1255 1256 1257/* Create a directory and report any errors. */ 1258int 1259assertion_make_dir(const char *file, int line, const char *dirname, int mode) 1260{ 1261 assertion_count(file, line); 1262#if defined(_WIN32) && !defined(__CYGWIN__) 1263 (void)mode; /* UNUSED */ 1264 if (0 == _mkdir(dirname)) 1265 return (1); 1266#else 1267 if (0 == mkdir(dirname, mode)) 1268 return (1); 1269#endif 1270 failure_start(file, line, "Could not create directory %s", dirname); 1271 failure_finish(NULL); 1272 return(0); 1273} 1274 1275/* Create a file with the specified contents and report any failures. */ 1276int 1277assertion_make_file(const char *file, int line, 1278 const char *path, int mode, const char *contents) 1279{ 1280#if defined(_WIN32) && !defined(__CYGWIN__) 1281 /* TODO: Rework this to set file mode as well. */ 1282 FILE *f; 1283 (void)mode; /* UNUSED */ 1284 assertion_count(file, line); 1285 f = fopen(path, "wb"); 1286 if (f == NULL) { 1287 failure_start(file, line, "Could not create file %s", path); 1288 failure_finish(NULL); 1289 return (0); 1290 } 1291 if (contents != NULL) { 1292 if (strlen(contents) 1293 != fwrite(contents, 1, strlen(contents), f)) { 1294 fclose(f); 1295 failure_start(file, line, 1296 "Could not write file %s", path); 1297 failure_finish(NULL); 1298 return (0); 1299 } 1300 } 1301 fclose(f); 1302 return (1); 1303#else 1304 int fd; 1305 assertion_count(file, line); 1306 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644); 1307 if (fd < 0) { 1308 failure_start(file, line, "Could not create %s", path); 1309 failure_finish(NULL); 1310 return (0); 1311 } 1312 if (contents != NULL) { 1313 if ((ssize_t)strlen(contents) 1314 != write(fd, contents, strlen(contents))) { 1315 close(fd); 1316 failure_start(file, line, "Could not write to %s", path); 1317 failure_finish(NULL); 1318 return (0); 1319 } 1320 } 1321 close(fd); 1322 return (1); 1323#endif 1324} 1325 1326/* Create a hardlink and report any failures. */ 1327int 1328assertion_make_hardlink(const char *file, int line, 1329 const char *newpath, const char *linkto) 1330{ 1331 int succeeded; 1332 1333 assertion_count(file, line); 1334#if defined(_WIN32) && !defined(__CYGWIN__) 1335 succeeded = my_CreateHardLinkA(newpath, linkto); 1336#elif HAVE_LINK 1337 succeeded = !link(linkto, newpath); 1338#else 1339 succeeded = 0; 1340#endif 1341 if (succeeded) 1342 return (1); 1343 failure_start(file, line, "Could not create hardlink"); 1344 logprintf(" New link: %s\n", newpath); 1345 logprintf(" Old name: %s\n", linkto); 1346 failure_finish(NULL); 1347 return(0); 1348} 1349 1350/* Create a symlink and report any failures. */ 1351int 1352assertion_make_symlink(const char *file, int line, 1353 const char *newpath, const char *linkto) 1354{ 1355#if defined(_WIN32) && !defined(__CYGWIN__) 1356 int targetIsDir = 0; /* TODO: Fix this */ 1357 assertion_count(file, line); 1358 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir)) 1359 return (1); 1360#elif HAVE_SYMLINK 1361 assertion_count(file, line); 1362 if (0 == symlink(linkto, newpath)) 1363 return (1); 1364#endif 1365 failure_start(file, line, "Could not create symlink"); 1366 logprintf(" New link: %s\n", newpath); 1367 logprintf(" Old name: %s\n", linkto); 1368 failure_finish(NULL); 1369 return(0); 1370} 1371 1372/* Set umask, report failures. */ 1373int 1374assertion_umask(const char *file, int line, int mask) 1375{ 1376 assertion_count(file, line); 1377 (void)file; /* UNUSED */ 1378 (void)line; /* UNUSED */ 1379 umask(mask); 1380 return (1); 1381} 1382 1383/* 1384 * 1385 * UTILITIES for use by tests. 1386 * 1387 */ 1388 1389/* 1390 * Check whether platform supports symlinks. This is intended 1391 * for tests to use in deciding whether to bother testing symlink 1392 * support; if the platform doesn't support symlinks, there's no point 1393 * in checking whether the program being tested can create them. 1394 * 1395 * Note that the first time this test is called, we actually go out to 1396 * disk to create and verify a symlink. This is necessary because 1397 * symlink support is actually a property of a particular filesystem 1398 * and can thus vary between directories on a single system. After 1399 * the first call, this returns the cached result from memory, so it's 1400 * safe to call it as often as you wish. 1401 */ 1402int 1403canSymlink(void) 1404{ 1405 /* Remember the test result */ 1406 static int value = 0, tested = 0; 1407 if (tested) 1408 return (value); 1409 1410 ++tested; 1411 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, "a"); 1412 /* Note: Cygwin has its own symlink() emulation that does not 1413 * use the Win32 CreateSymbolicLink() function. */ 1414#if defined(_WIN32) && !defined(__CYGWIN__) 1415 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0) 1416 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0"); 1417#elif HAVE_SYMLINK 1418 value = (0 == symlink("canSymlink.0", "canSymlink.1")) 1419 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0"); 1420#endif 1421 return (value); 1422} 1423 1424/* 1425 * Can this platform run the gzip program? 1426 */ 1427/* Platform-dependent options for hiding the output of a subcommand. */ 1428#if defined(_WIN32) && !defined(__CYGWIN__) 1429static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */ 1430#else 1431static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */ 1432#endif 1433int 1434canGzip(void) 1435{ 1436 static int tested = 0, value = 0; 1437 if (!tested) { 1438 tested = 1; 1439 if (systemf("gzip -V %s", redirectArgs) == 0) 1440 value = 1; 1441 } 1442 return (value); 1443} 1444 1445/* 1446 * Can this platform run the gunzip program? 1447 */ 1448int 1449canGunzip(void) 1450{ 1451 static int tested = 0, value = 0; 1452 if (!tested) { 1453 tested = 1; 1454 if (systemf("gunzip -V %s", redirectArgs) == 0) 1455 value = 1; 1456 } 1457 return (value); 1458} 1459 1460/* 1461 * Sleep as needed; useful for verifying disk timestamp changes by 1462 * ensuring that the wall-clock time has actually changed before we 1463 * go back to re-read something from disk. 1464 */ 1465void 1466sleepUntilAfter(time_t t) 1467{ 1468 while (t >= time(NULL)) 1469#if defined(_WIN32) && !defined(__CYGWIN__) 1470 Sleep(500); 1471#else 1472 sleep(1); 1473#endif 1474} 1475 1476/* 1477 * Call standard system() call, but build up the command line using 1478 * sprintf() conventions. 1479 */ 1480int 1481systemf(const char *fmt, ...) 1482{ 1483 char buff[8192]; 1484 va_list ap; 1485 int r; 1486 1487 va_start(ap, fmt); 1488 vsprintf(buff, fmt, ap); 1489 if (verbosity > VERBOSITY_FULL) 1490 logprintf("Cmd: %s\n", buff); 1491 r = system(buff); 1492 va_end(ap); 1493 return (r); 1494} 1495 1496/* 1497 * Slurp a file into memory for ease of comparison and testing. 1498 * Returns size of file in 'sizep' if non-NULL, null-terminates 1499 * data in memory for ease of use. 1500 */ 1501char * 1502slurpfile(size_t * sizep, const char *fmt, ...) 1503{ 1504 char filename[8192]; 1505 struct stat st; 1506 va_list ap; 1507 char *p; 1508 ssize_t bytes_read; 1509 FILE *f; 1510 int r; 1511 1512 va_start(ap, fmt); 1513 vsprintf(filename, fmt, ap); 1514 va_end(ap); 1515 1516 f = fopen(filename, "rb"); 1517 if (f == NULL) { 1518 /* Note: No error; non-existent file is okay here. */ 1519 return (NULL); 1520 } 1521 r = fstat(fileno(f), &st); 1522 if (r != 0) { 1523 logprintf("Can't stat file %s\n", filename); 1524 fclose(f); 1525 return (NULL); 1526 } 1527 p = malloc((size_t)st.st_size + 1); 1528 if (p == NULL) { 1529 logprintf("Can't allocate %ld bytes of memory to read file %s\n", 1530 (long int)st.st_size, filename); 1531 fclose(f); 1532 return (NULL); 1533 } 1534 bytes_read = fread(p, 1, (size_t)st.st_size, f); 1535 if (bytes_read < st.st_size) { 1536 logprintf("Can't read file %s\n", filename); 1537 fclose(f); 1538 free(p); 1539 return (NULL); 1540 } 1541 p[st.st_size] = '\0'; 1542 if (sizep != NULL) 1543 *sizep = (size_t)st.st_size; 1544 fclose(f); 1545 return (p); 1546} 1547 1548/* Read a uuencoded file from the reference directory, decode, and 1549 * write the result into the current directory. */ 1550#define UUDECODE(c) (((c) - 0x20) & 0x3f) 1551void 1552extract_reference_file(const char *name) 1553{ 1554 char buff[1024]; 1555 FILE *in, *out; 1556 1557 sprintf(buff, "%s/%s.uu", refdir, name); 1558 in = fopen(buff, "r"); 1559 failure("Couldn't open reference file %s", buff); 1560 assert(in != NULL); 1561 if (in == NULL) 1562 return; 1563 /* Read up to and including the 'begin' line. */ 1564 for (;;) { 1565 if (fgets(buff, sizeof(buff), in) == NULL) { 1566 /* TODO: This is a failure. */ 1567 return; 1568 } 1569 if (memcmp(buff, "begin ", 6) == 0) 1570 break; 1571 } 1572 /* Now, decode the rest and write it. */ 1573 /* Not a lot of error checking here; the input better be right. */ 1574 out = fopen(name, "wb"); 1575 while (fgets(buff, sizeof(buff), in) != NULL) { 1576 char *p = buff; 1577 int bytes; 1578 1579 if (memcmp(buff, "end", 3) == 0) 1580 break; 1581 1582 bytes = UUDECODE(*p++); 1583 while (bytes > 0) { 1584 int n = 0; 1585 /* Write out 1-3 bytes from that. */ 1586 if (bytes > 0) { 1587 n = UUDECODE(*p++) << 18; 1588 n |= UUDECODE(*p++) << 12; 1589 fputc(n >> 16, out); 1590 --bytes; 1591 } 1592 if (bytes > 0) { 1593 n |= UUDECODE(*p++) << 6; 1594 fputc((n >> 8) & 0xFF, out); 1595 --bytes; 1596 } 1597 if (bytes > 0) { 1598 n |= UUDECODE(*p++); 1599 fputc(n & 0xFF, out); 1600 --bytes; 1601 } 1602 } 1603 } 1604 fclose(out); 1605 fclose(in); 1606} 1607 1608/* 1609 * 1610 * TEST management 1611 * 1612 */ 1613 1614/* 1615 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has 1616 * a line like 1617 * DEFINE_TEST(test_function) 1618 * for each test. 1619 */ 1620 1621/* Use "list.h" to declare all of the test functions. */ 1622#undef DEFINE_TEST 1623#define DEFINE_TEST(name) void name(void); 1624#include "list.h" 1625 1626/* Use "list.h" to create a list of all tests (functions and names). */ 1627#undef DEFINE_TEST 1628#define DEFINE_TEST(n) { n, #n, 0 }, 1629struct { void (*func)(void); const char *name; int failures; } tests[] = { 1630 #include "list.h" 1631}; 1632 1633/* 1634 * Summarize repeated failures in the just-completed test. 1635 */ 1636static void 1637test_summarize(const char *filename, int failed) 1638{ 1639 unsigned int i; 1640 1641 switch (verbosity) { 1642 case VERBOSITY_SUMMARY_ONLY: 1643 printf(failed ? "E" : "."); 1644 fflush(stdout); 1645 break; 1646 case VERBOSITY_PASSFAIL: 1647 printf(failed ? "FAIL\n" : "ok\n"); 1648 break; 1649 } 1650 1651 log_console = (verbosity == VERBOSITY_LIGHT_REPORT); 1652 1653 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { 1654 if (failed_lines[i].count > 1 && !failed_lines[i].skip) 1655 logprintf("%s:%d: Summary: Failed %d times\n", 1656 filename, i, failed_lines[i].count); 1657 } 1658 /* Clear the failure history for the next file. */ 1659 memset(failed_lines, 0, sizeof(failed_lines)); 1660} 1661 1662/* 1663 * Actually run a single test, with appropriate setup and cleanup. 1664 */ 1665static int 1666test_run(int i, const char *tmpdir) 1667{ 1668 char logfilename[64]; 1669 int failures_before = failures; 1670 int oldumask; 1671 1672 switch (verbosity) { 1673 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */ 1674 break; 1675 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */ 1676 printf("%3d: %-50s", i, tests[i].name); 1677 fflush(stdout); 1678 break; 1679 default: /* Title of test, details will follow */ 1680 printf("%3d: %s\n", i, tests[i].name); 1681 } 1682 1683 /* Chdir to the top-level work directory. */ 1684 if (!assertChdir(tmpdir)) { 1685 fprintf(stderr, 1686 "ERROR: Can't chdir to top work dir %s\n", tmpdir); 1687 exit(1); 1688 } 1689 /* Create a log file for this test. */ 1690 sprintf(logfilename, "%s.log", tests[i].name); 1691 logfile = fopen(logfilename, "w"); 1692 fprintf(logfile, "%s\n\n", tests[i].name); 1693 /* Chdir() to a work dir for this specific test. */ 1694 if (!assertMakeDir(tests[i].name, 0755) 1695 || !assertChdir(tests[i].name)) { 1696 fprintf(stderr, 1697 "ERROR: Can't chdir to work dir %s/%s\n", 1698 tmpdir, tests[i].name); 1699 exit(1); 1700 } 1701 /* Explicitly reset the locale before each test. */ 1702 setlocale(LC_ALL, "C"); 1703 /* Record the umask before we run the test. */ 1704 umask(oldumask = umask(0)); 1705 /* 1706 * Run the actual test. 1707 */ 1708 (*tests[i].func)(); 1709 /* 1710 * Clean up and report afterwards. 1711 */ 1712 /* Restore umask */ 1713 umask(oldumask); 1714 /* Reset locale. */ 1715 setlocale(LC_ALL, "C"); 1716 /* Reset directory. */ 1717 if (!assertChdir(tmpdir)) { 1718 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n", 1719 tmpdir); 1720 exit(1); 1721 } 1722 /* Report per-test summaries. */ 1723 tests[i].failures = failures - failures_before; 1724 test_summarize(test_filename, tests[i].failures); 1725 /* Close the per-test log file. */ 1726 fclose(logfile); 1727 logfile = NULL; 1728 /* If there were no failures, we can remove the work dir and logfile. */ 1729 if (tests[i].failures == 0) { 1730 if (!keep_temp_files && assertChdir(tmpdir)) { 1731#if defined(_WIN32) && !defined(__CYGWIN__) 1732 /* Make sure not to leave empty directories. 1733 * Sometimes a processing of closing files used by tests 1734 * is not done, then rmdir will be failed and it will 1735 * leave a empty test directory. So we should wait a few 1736 * seconds and retry rmdir. */ 1737 int r, t; 1738 for (t = 0; t < 10; t++) { 1739 if (t > 0) 1740 Sleep(1000); 1741 r = systemf("rmdir /S /Q %s", tests[i].name); 1742 if (r == 0) 1743 break; 1744 } 1745 systemf("del %s", logfilename); 1746#else 1747 systemf("rm -rf %s", tests[i].name); 1748 systemf("rm %s", logfilename); 1749#endif 1750 } 1751 } 1752 /* Return appropriate status. */ 1753 return (tests[i].failures); 1754} 1755 1756/* 1757 * 1758 * 1759 * MAIN and support routines. 1760 * 1761 * 1762 */ 1763 1764static void 1765usage(const char *program) 1766{ 1767 static const int limit = sizeof(tests) / sizeof(tests[0]); 1768 int i; 1769 1770 printf("Usage: %s [options] <test> <test> ...\n", program); 1771 printf("Default is to run all tests.\n"); 1772 printf("Otherwise, specify the numbers of the tests you wish to run.\n"); 1773 printf("Options:\n"); 1774 printf(" -d Dump core after any failure, for debugging.\n"); 1775 printf(" -k Keep all temp files.\n"); 1776 printf(" Default: temp files for successful tests deleted.\n"); 1777#ifdef PROGRAM 1778 printf(" -p <path> Path to executable to be tested.\n"); 1779 printf(" Default: path taken from " ENVBASE " environment variable.\n"); 1780#endif 1781 printf(" -q Quiet.\n"); 1782 printf(" -r <dir> Path to dir containing reference files.\n"); 1783 printf(" Default: Current directory.\n"); 1784 printf(" -v Verbose.\n"); 1785 printf("Available tests:\n"); 1786 for (i = 0; i < limit; i++) 1787 printf(" %d: %s\n", i, tests[i].name); 1788 exit(1); 1789} 1790 1791static char * 1792get_refdir(const char *d) 1793{ 1794 char tried[512] = { '\0' }; 1795 char buff[128]; 1796 char *pwd, *p; 1797 1798 /* If a dir was specified, try that */ 1799 if (d != NULL) { 1800 pwd = NULL; 1801 snprintf(buff, sizeof(buff), "%s", d); 1802 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 1803 if (p != NULL) goto success; 1804 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 1805 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 1806 goto failure; 1807 } 1808 1809 /* Get the current dir. */ 1810 pwd = getcwd(NULL, 0); 1811 while (pwd[strlen(pwd) - 1] == '\n') 1812 pwd[strlen(pwd) - 1] = '\0'; 1813 1814 /* Look for a known file. */ 1815 snprintf(buff, sizeof(buff), "%s", pwd); 1816 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 1817 if (p != NULL) goto success; 1818 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 1819 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 1820 1821 snprintf(buff, sizeof(buff), "%s/test", pwd); 1822 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 1823 if (p != NULL) goto success; 1824 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 1825 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 1826 1827#if defined(LIBRARY) 1828 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY); 1829#else 1830 snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM); 1831#endif 1832 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 1833 if (p != NULL) goto success; 1834 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 1835 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 1836 1837 if (memcmp(pwd, "/usr/obj", 8) == 0) { 1838 snprintf(buff, sizeof(buff), "%s", pwd + 8); 1839 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 1840 if (p != NULL) goto success; 1841 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 1842 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 1843 1844 snprintf(buff, sizeof(buff), "%s/test", pwd + 8); 1845 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 1846 if (p != NULL) goto success; 1847 strncat(tried, buff, sizeof(tried) - strlen(tried) - 1); 1848 strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1); 1849 } 1850 1851failure: 1852 printf("Unable to locate known reference file %s\n", KNOWNREF); 1853 printf(" Checked following directories:\n%s\n", tried); 1854#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) 1855 DebugBreak(); 1856#endif 1857 exit(1); 1858 1859success: 1860 free(p); 1861 free(pwd); 1862 return strdup(buff); 1863} 1864 1865int 1866main(int argc, char **argv) 1867{ 1868 static const int limit = sizeof(tests) / sizeof(tests[0]); 1869 int i, tests_run = 0, tests_failed = 0, option; 1870 time_t now; 1871 char *refdir_alloc = NULL; 1872 const char *progname; 1873 const char *tmp, *option_arg, *p; 1874 char tmpdir[256]; 1875 char tmpdir_timestamp[256]; 1876 1877 (void)argc; /* UNUSED */ 1878 1879#if defined(HAVE__CrtSetReportMode) 1880 /* To stop to run the default invalid parameter handler. */ 1881 _set_invalid_parameter_handler(invalid_parameter_handler); 1882 /* Disable annoying assertion message box. */ 1883 _CrtSetReportMode(_CRT_ASSERT, 0); 1884#endif 1885 1886 /* 1887 * Name of this program, used to build root of our temp directory 1888 * tree. 1889 */ 1890 progname = p = argv[0]; 1891 while (*p != '\0') { 1892 /* Support \ or / dir separators for Windows compat. */ 1893 if (*p == '/' || *p == '\\') 1894 progname = p + 1; 1895 ++p; 1896 } 1897 1898#ifdef PROGRAM 1899 /* Get the target program from environment, if available. */ 1900 testprogfile = getenv(ENVBASE); 1901#endif 1902 1903 if (getenv("TMPDIR") != NULL) 1904 tmp = getenv("TMPDIR"); 1905 else if (getenv("TMP") != NULL) 1906 tmp = getenv("TMP"); 1907 else if (getenv("TEMP") != NULL) 1908 tmp = getenv("TEMP"); 1909 else if (getenv("TEMPDIR") != NULL) 1910 tmp = getenv("TEMPDIR"); 1911 else 1912 tmp = "/tmp"; 1913 1914 /* Allow -d to be controlled through the environment. */ 1915 if (getenv(ENVBASE "_DEBUG") != NULL) 1916 dump_on_failure = 1; 1917 1918 /* Get the directory holding test files from environment. */ 1919 refdir = getenv(ENVBASE "_TEST_FILES"); 1920 1921 /* 1922 * Parse options, without using getopt(), which isn't available 1923 * on all platforms. 1924 */ 1925 ++argv; /* Skip program name */ 1926 while (*argv != NULL) { 1927 if (**argv != '-') 1928 break; 1929 p = *argv++; 1930 ++p; /* Skip '-' */ 1931 while (*p != '\0') { 1932 option = *p++; 1933 option_arg = NULL; 1934 /* If 'opt' takes an argument, parse that. */ 1935 if (option == 'p' || option == 'r') { 1936 if (*p != '\0') 1937 option_arg = p; 1938 else if (*argv == NULL) { 1939 fprintf(stderr, 1940 "Option -%c requires argument.\n", 1941 option); 1942 usage(progname); 1943 } else 1944 option_arg = *argv++; 1945 p = ""; /* End of this option word. */ 1946 } 1947 1948 /* Now, handle the option. */ 1949 switch (option) { 1950 case 'd': 1951 dump_on_failure = 1; 1952 break; 1953 case 'k': 1954 keep_temp_files = 1; 1955 break; 1956 case 'p': 1957#ifdef PROGRAM 1958 testprogfile = option_arg; 1959#else 1960 usage(progname); 1961#endif 1962 break; 1963 case 'q': 1964 verbosity--; 1965 break; 1966 case 'r': 1967 refdir = option_arg; 1968 break; 1969 case 'v': 1970 verbosity++; 1971 break; 1972 default: 1973 usage(progname); 1974 } 1975 } 1976 } 1977 1978 /* 1979 * Sanity-check that our options make sense. 1980 */ 1981#ifdef PROGRAM 1982 if (testprogfile == NULL) 1983 usage(progname); 1984 { 1985 char *testprg; 1986#if defined(_WIN32) && !defined(__CYGWIN__) 1987 /* Command.com sometimes rejects '/' separators. */ 1988 testprg = strdup(testprogfile); 1989 for (i = 0; testprg[i] != '\0'; i++) { 1990 if (testprg[i] == '/') 1991 testprg[i] = '\\'; 1992 } 1993 testprogfile = testprg; 1994#endif 1995 /* Quote the name that gets put into shell command lines. */ 1996 testprg = malloc(strlen(testprogfile) + 3); 1997 strcpy(testprg, "\""); 1998 strcat(testprg, testprogfile); 1999 strcat(testprg, "\""); 2000 testprog = testprg; 2001 } 2002#endif 2003 2004 /* 2005 * Create a temp directory for the following tests. 2006 * Include the time the tests started as part of the name, 2007 * to make it easier to track the results of multiple tests. 2008 */ 2009 now = time(NULL); 2010 for (i = 0; ; i++) { 2011 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), 2012 "%Y-%m-%dT%H.%M.%S", 2013 localtime(&now)); 2014 sprintf(tmpdir, "%s/%s.%s-%03d", tmp, progname, 2015 tmpdir_timestamp, i); 2016 if (assertMakeDir(tmpdir,0755)) 2017 break; 2018 if (i >= 999) { 2019 fprintf(stderr, 2020 "ERROR: Unable to create temp directory %s\n", 2021 tmpdir); 2022 exit(1); 2023 } 2024 } 2025 2026 /* 2027 * If the user didn't specify a directory for locating 2028 * reference files, try to find the reference files in 2029 * the "usual places." 2030 */ 2031 refdir = refdir_alloc = get_refdir(refdir); 2032 2033 /* 2034 * Banner with basic information. 2035 */ 2036 printf("\n"); 2037 printf("If tests fail or crash, details will be in:\n"); 2038 printf(" %s\n", tmpdir); 2039 printf("\n"); 2040 if (verbosity > VERBOSITY_SUMMARY_ONLY) { 2041 printf("Reference files will be read from: %s\n", refdir); 2042#ifdef PROGRAM 2043 printf("Running tests on: %s\n", testprog); 2044#endif 2045 printf("Exercising: "); 2046 fflush(stdout); 2047 printf("%s\n", EXTRA_VERSION); 2048 } else { 2049 printf("Running "); 2050 fflush(stdout); 2051 } 2052 2053 /* 2054 * Run some or all of the individual tests. 2055 */ 2056 if (*argv == NULL) { 2057 /* Default: Run all tests. */ 2058 for (i = 0; i < limit; i++) { 2059 if (test_run(i, tmpdir)) 2060 tests_failed++; 2061 tests_run++; 2062 } 2063 } else { 2064 while (*(argv) != NULL) { 2065 if (**argv >= '0' && **argv <= '9') { 2066 i = atoi(*argv); 2067 if (i < 0 || i >= limit) { 2068 printf("*** INVALID Test %s\n", *argv); 2069 free(refdir_alloc); 2070 usage(progname); 2071 /* usage() never returns */ 2072 } 2073 } else { 2074 for (i = 0; i < limit; ++i) { 2075 if (strcmp(*argv, tests[i].name) == 0) 2076 break; 2077 } 2078 if (i >= limit) { 2079 printf("*** INVALID Test ``%s''\n", 2080 *argv); 2081 free(refdir_alloc); 2082 usage(progname); 2083 /* usage() never returns */ 2084 } 2085 } 2086 if (test_run(i, tmpdir)) 2087 tests_failed++; 2088 tests_run++; 2089 argv++; 2090 } 2091 } 2092 2093 /* 2094 * Report summary statistics. 2095 */ 2096 if (verbosity > VERBOSITY_SUMMARY_ONLY) { 2097 printf("\n"); 2098 printf("Totals:\n"); 2099 printf(" Tests run: %8d\n", tests_run); 2100 printf(" Tests failed: %8d\n", tests_failed); 2101 printf(" Assertions checked:%8d\n", assertions); 2102 printf(" Assertions failed: %8d\n", failures); 2103 printf(" Skips reported: %8d\n", skips); 2104 } 2105 if (failures) { 2106 printf("\n"); 2107 printf("Failing tests:\n"); 2108 for (i = 0; i < limit; ++i) { 2109 if (tests[i].failures) 2110 printf(" %d: %s (%d failures)\n", i, 2111 tests[i].name, tests[i].failures); 2112 } 2113 printf("\n"); 2114 printf("Details for failing tests: %s\n", tmpdir); 2115 printf("\n"); 2116 } else { 2117 if (verbosity == VERBOSITY_SUMMARY_ONLY) 2118 printf("\n"); 2119 printf("%d tests passed, no failures\n", tests_run); 2120 } 2121 2122 free(refdir_alloc); 2123 2124 /* If the final tmpdir is empty, we can remove it. */ 2125 /* This should be the usual case when all tests succeed. */ 2126 assertChdir(".."); 2127 rmdir(tmpdir); 2128 2129 return (tests_failed ? 1 : 0); 2130} 2131