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