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 #ifdef HAVE_LINUX_FS_H 49 #include <linux/fs.h> 50 #endif 51 #include <limits.h> 52 #include <locale.h> 53 #ifdef HAVE_SIGNAL_H 54 #include <signal.h> 55 #endif 56 #include <stdarg.h> 57 #include <time.h> 58 59 #ifdef HAVE_SIGNAL_H 60 #endif 61 #ifdef HAVE_ACL_LIBACL_H 62 #include <acl/libacl.h> 63 #endif 64 #ifdef HAVE_SYS_TYPES_H 65 #include <sys/types.h> 66 #endif 67 #ifdef HAVE_SYS_ACL_H 68 #include <sys/acl.h> 69 #endif 70 #ifdef HAVE_SYS_EA_H 71 #include <sys/ea.h> 72 #endif 73 #ifdef HAVE_SYS_EXTATTR_H 74 #include <sys/extattr.h> 75 #endif 76 #if HAVE_SYS_XATTR_H 77 #include <sys/xattr.h> 78 #elif HAVE_ATTR_XATTR_H 79 #include <attr/xattr.h> 80 #endif 81 #ifdef HAVE_SYS_RICHACL_H 82 #include <sys/richacl.h> 83 #endif 84 #if HAVE_MEMBERSHIP_H 85 #include <membership.h> 86 #endif 87 88 /* 89 * 90 * Windows support routines 91 * 92 * Note: Configuration is a tricky issue. Using HAVE_* feature macros 93 * in the test harness is dangerous because they cover up 94 * configuration errors. The classic example of this is omitting a 95 * configure check. If libarchive and libarchive_test both look for 96 * the same feature macro, such errors are hard to detect. Platform 97 * macros (e.g., _WIN32 or __GNUC__) are a little better, but can 98 * easily lead to very messy code. It's best to limit yourself 99 * to only the most generic programming techniques in the test harness 100 * and thus avoid conditionals altogether. Where that's not possible, 101 * try to minimize conditionals by grouping platform-specific tests in 102 * one place (e.g., test_acl_freebsd) or by adding new assert() 103 * functions (e.g., assertMakeHardlink()) to cover up platform 104 * differences. Platform-specific coding in libarchive_test is often 105 * a symptom that some capability is missing from libarchive itself. 106 */ 107 #if defined(_WIN32) && !defined(__CYGWIN__) 108 #include <io.h> 109 #include <direct.h> 110 #include <windows.h> 111 #ifndef F_OK 112 #define F_OK (0) 113 #endif 114 #ifndef S_ISDIR 115 #define S_ISDIR(m) ((m) & _S_IFDIR) 116 #endif 117 #ifndef S_ISREG 118 #define S_ISREG(m) ((m) & _S_IFREG) 119 #endif 120 #if !defined(__BORLANDC__) 121 #define access _access 122 #undef chdir 123 #define chdir _chdir 124 #endif 125 #ifndef fileno 126 #define fileno _fileno 127 #endif 128 /*#define fstat _fstat64*/ 129 #if !defined(__BORLANDC__) 130 #define getcwd _getcwd 131 #endif 132 #define lstat stat 133 /*#define lstat _stat64*/ 134 /*#define stat _stat64*/ 135 #define rmdir _rmdir 136 #if !defined(__BORLANDC__) 137 #define strdup _strdup 138 #define umask _umask 139 #endif 140 #define int64_t __int64 141 #endif 142 143 #if defined(HAVE__CrtSetReportMode) 144 # include <crtdbg.h> 145 #endif 146 147 mode_t umasked(mode_t expected_mode) 148 { 149 mode_t mode = umask(0); 150 umask(mode); 151 return expected_mode & ~mode; 152 } 153 154 /* Path to working directory for current test */ 155 const char *testworkdir; 156 #ifdef PROGRAM 157 /* Pathname of exe to be tested. */ 158 const char *testprogfile; 159 /* Name of exe to use in printf-formatted command strings. */ 160 /* On Windows, this includes leading/trailing quotes. */ 161 const char *testprog; 162 #endif 163 164 #if defined(_WIN32) && !defined(__CYGWIN__) 165 static void *GetFunctionKernel32(const char *); 166 static int my_CreateSymbolicLinkA(const char *, const char *, int); 167 static int my_CreateHardLinkA(const char *, const char *); 168 static int my_GetFileInformationByName(const char *, 169 BY_HANDLE_FILE_INFORMATION *); 170 171 typedef struct _REPARSE_DATA_BUFFER { 172 ULONG ReparseTag; 173 USHORT ReparseDataLength; 174 USHORT Reserved; 175 union { 176 struct { 177 USHORT SubstituteNameOffset; 178 USHORT SubstituteNameLength; 179 USHORT PrintNameOffset; 180 USHORT PrintNameLength; 181 ULONG Flags; 182 WCHAR PathBuffer[1]; 183 } SymbolicLinkReparseBuffer; 184 struct { 185 USHORT SubstituteNameOffset; 186 USHORT SubstituteNameLength; 187 USHORT PrintNameOffset; 188 USHORT PrintNameLength; 189 WCHAR PathBuffer[1]; 190 } MountPointReparseBuffer; 191 struct { 192 UCHAR DataBuffer[1]; 193 } GenericReparseBuffer; 194 } DUMMYUNIONNAME; 195 } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; 196 197 static void * 198 GetFunctionKernel32(const char *name) 199 { 200 static HINSTANCE lib; 201 static int set; 202 if (!set) { 203 set = 1; 204 lib = LoadLibrary("kernel32.dll"); 205 } 206 if (lib == NULL) { 207 fprintf(stderr, "Can't load kernel32.dll?!\n"); 208 exit(1); 209 } 210 return (void *)GetProcAddress(lib, name); 211 } 212 213 static int 214 my_CreateSymbolicLinkA(const char *linkname, const char *target, 215 int targetIsDir) 216 { 217 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD); 218 DWORD attrs; 219 static int set; 220 int ret, tmpflags, llen, tlen; 221 int flags = 0; 222 char *src, *tgt, *p; 223 if (!set) { 224 set = 1; 225 f = GetFunctionKernel32("CreateSymbolicLinkA"); 226 } 227 if (f == NULL) 228 return (0); 229 230 tlen = strlen(target); 231 llen = strlen(linkname); 232 233 if (tlen == 0 || llen == 0) 234 return (0); 235 236 tgt = malloc((tlen + 1) * sizeof(char)); 237 if (tgt == NULL) 238 return (0); 239 src = malloc((llen + 1) * sizeof(char)); 240 if (src == NULL) { 241 free(tgt); 242 return (0); 243 } 244 245 /* 246 * Translate slashes to backslashes 247 */ 248 p = src; 249 while(*linkname != '\0') { 250 if (*linkname == '/') 251 *p = '\\'; 252 else 253 *p = *linkname; 254 linkname++; 255 p++; 256 } 257 *p = '\0'; 258 259 p = tgt; 260 while(*target != '\0') { 261 if (*target == '/') 262 *p = '\\'; 263 else 264 *p = *target; 265 target++; 266 p++; 267 } 268 *p = '\0'; 269 270 /* 271 * Each test has to specify if a file or a directory symlink 272 * should be created. 273 */ 274 if (targetIsDir) { 275 #if defined(SYMBOLIC_LINK_FLAG_DIRECTORY) 276 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; 277 #else 278 flags |= 0x1; 279 #endif 280 } 281 282 #if defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) 283 tmpflags = flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; 284 #else 285 tmpflags = flags | 0x2; 286 #endif 287 /* 288 * Windows won't overwrite existing links 289 */ 290 attrs = GetFileAttributesA(linkname); 291 if (attrs != INVALID_FILE_ATTRIBUTES) { 292 if (attrs & FILE_ATTRIBUTE_DIRECTORY) 293 RemoveDirectoryA(linkname); 294 else 295 DeleteFileA(linkname); 296 } 297 298 ret = (*f)(src, tgt, tmpflags); 299 /* 300 * Prior to Windows 10 the SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE 301 * is not understood 302 */ 303 if (!ret) 304 ret = (*f)(src, tgt, flags); 305 306 free(src); 307 free(tgt); 308 return (ret); 309 } 310 311 static int 312 my_CreateHardLinkA(const char *linkname, const char *target) 313 { 314 static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES); 315 static int set; 316 if (!set) { 317 set = 1; 318 f = GetFunctionKernel32("CreateHardLinkA"); 319 } 320 return f == NULL ? 0 : (*f)(linkname, target, NULL); 321 } 322 323 static int 324 my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi) 325 { 326 HANDLE h; 327 int r; 328 329 memset(bhfi, 0, sizeof(*bhfi)); 330 h = CreateFile(path, FILE_READ_ATTRIBUTES, 0, NULL, 331 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 332 if (h == INVALID_HANDLE_VALUE) 333 return (0); 334 r = GetFileInformationByHandle(h, bhfi); 335 CloseHandle(h); 336 return (r); 337 } 338 #endif 339 340 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 341 static void 342 invalid_parameter_handler(const wchar_t * expression, 343 const wchar_t * function, const wchar_t * file, 344 unsigned int line, uintptr_t pReserved) 345 { 346 /* nop */ 347 // Silence unused-parameter compiler warnings. 348 (void)expression; 349 (void)function; 350 (void)file; 351 (void)line; 352 (void)pReserved; 353 } 354 #endif 355 356 /* 357 * 358 * OPTIONS FLAGS 359 * 360 */ 361 362 /* Enable core dump on failure. */ 363 static int dump_on_failure = 0; 364 /* Default is to remove temp dirs and log data for successful tests. */ 365 static int keep_temp_files = 0; 366 /* Default is to run the specified tests once and report errors. */ 367 static int until_failure = 0; 368 /* Default is to just report pass/fail for each test. */ 369 static int verbosity = 0; 370 #define VERBOSITY_SUMMARY_ONLY -1 /* -q */ 371 #define VERBOSITY_PASSFAIL 0 /* Default */ 372 #define VERBOSITY_LIGHT_REPORT 1 /* -v */ 373 #define VERBOSITY_FULL 2 /* -vv */ 374 /* A few places generate even more output for verbosity > VERBOSITY_FULL, 375 * mostly for debugging the test harness itself. */ 376 /* Cumulative count of assertion failures. */ 377 static int failures = 0; 378 /* Cumulative count of reported skips. */ 379 static int skips = 0; 380 /* Cumulative count of assertions checked. */ 381 static int assertions = 0; 382 383 /* Directory where uuencoded reference files can be found. */ 384 static const char *refdir; 385 386 /* 387 * Report log information selectively to console and/or disk log. 388 */ 389 static int log_console = 0; 390 static FILE *logfile; 391 static void __LA_PRINTFLIKE(1, 0) 392 vlogprintf(const char *fmt, va_list ap) 393 { 394 #ifdef va_copy 395 va_list lfap; 396 va_copy(lfap, ap); 397 #endif 398 if (log_console) 399 vfprintf(stdout, fmt, ap); 400 if (logfile != NULL) 401 #ifdef va_copy 402 vfprintf(logfile, fmt, lfap); 403 va_end(lfap); 404 #else 405 vfprintf(logfile, fmt, ap); 406 #endif 407 } 408 409 static void __LA_PRINTFLIKE(1, 2) 410 logprintf(const char *fmt, ...) 411 { 412 va_list ap; 413 va_start(ap, fmt); 414 vlogprintf(fmt, ap); 415 va_end(ap); 416 } 417 418 /* Set up a message to display only if next assertion fails. */ 419 static char msgbuff[4096]; 420 static const char *msg, *nextmsg; 421 void 422 failure(const char *fmt, ...) 423 { 424 va_list ap; 425 if (fmt == NULL) { 426 nextmsg = NULL; 427 } else { 428 va_start(ap, fmt); 429 vsnprintf(msgbuff, sizeof(msgbuff), fmt, ap); 430 va_end(ap); 431 nextmsg = msgbuff; 432 } 433 } 434 435 /* 436 * Copy arguments into file-local variables. 437 * This was added to permit vararg assert() functions without needing 438 * variadic wrapper macros. Turns out that the vararg capability is almost 439 * never used, so almost all of the vararg assertions can be simplified 440 * by removing the vararg capability and reworking the wrapper macro to 441 * pass __FILE__, __LINE__ directly into the function instead of using 442 * this hook. I suspect this machinery is used so rarely that we 443 * would be better off just removing it entirely. That would simplify 444 * the code here noticeably. 445 */ 446 static const char *skipping_filename; 447 static int skipping_line; 448 void skipping_setup(const char *filename, int line) 449 { 450 skipping_filename = filename; 451 skipping_line = line; 452 } 453 454 /* Called at the beginning of each assert() function. */ 455 static void 456 assertion_count(const char *file, int line) 457 { 458 (void)file; /* UNUSED */ 459 (void)line; /* UNUSED */ 460 ++assertions; 461 /* Proper handling of "failure()" message. */ 462 msg = nextmsg; 463 nextmsg = NULL; 464 /* Uncomment to print file:line after every assertion. 465 * Verbose, but occasionally useful in tracking down crashes. */ 466 /* printf("Checked %s:%d\n", file, line); */ 467 } 468 469 /* 470 * For each test source file, we remember how many times each 471 * assertion was reported. Cleared before each new test, 472 * used by test_summarize(). 473 */ 474 static struct line { 475 int count; 476 int skip; 477 } failed_lines[10000]; 478 static const char *failed_filename; 479 480 /* Count this failure, setup up log destination and handle initial report. */ 481 static void __LA_PRINTFLIKE(3, 4) 482 failure_start(const char *filename, int line, const char *fmt, ...) 483 { 484 va_list ap; 485 486 /* Record another failure for this line. */ 487 ++failures; 488 failed_filename = filename; 489 failed_lines[line].count++; 490 491 /* Determine whether to log header to console. */ 492 switch (verbosity) { 493 case VERBOSITY_LIGHT_REPORT: 494 log_console = (failed_lines[line].count < 2); 495 break; 496 default: 497 log_console = (verbosity >= VERBOSITY_FULL); 498 } 499 500 /* Log file:line header for this failure */ 501 va_start(ap, fmt); 502 #if _MSC_VER 503 logprintf("%s(%d): ", filename, line); 504 #else 505 logprintf("%s:%d: ", filename, line); 506 #endif 507 vlogprintf(fmt, ap); 508 va_end(ap); 509 logprintf("\n"); 510 511 if (msg != NULL && msg[0] != '\0') { 512 logprintf(" Description: %s\n", msg); 513 msg = NULL; 514 } 515 516 /* Determine whether to log details to console. */ 517 if (verbosity == VERBOSITY_LIGHT_REPORT) 518 log_console = 0; 519 } 520 521 /* Complete reporting of failed tests. */ 522 /* 523 * The 'extra' hook here is used by libarchive to include libarchive 524 * error messages with assertion failures. It could also be used 525 * to add strerror() output, for example. Just define the EXTRA_DUMP() 526 * macro appropriately. 527 */ 528 static void 529 failure_finish(void *extra) 530 { 531 (void)extra; /* UNUSED (maybe) */ 532 #ifdef EXTRA_DUMP 533 if (extra != NULL) { 534 logprintf(" errno: %d\n", EXTRA_ERRNO(extra)); 535 logprintf(" detail: %s\n", EXTRA_DUMP(extra)); 536 } 537 #endif 538 539 if (dump_on_failure) { 540 fprintf(stderr, 541 " *** forcing core dump so failure can be debugged ***\n"); 542 abort(); 543 } 544 } 545 546 /* Inform user that we're skipping some checks. */ 547 void 548 test_skipping(const char *fmt, ...) 549 { 550 char buff[1024]; 551 va_list ap; 552 553 va_start(ap, fmt); 554 vsnprintf(buff, sizeof(buff), fmt, ap); 555 va_end(ap); 556 /* Use failure() message if set. */ 557 msg = nextmsg; 558 nextmsg = NULL; 559 /* failure_start() isn't quite right, but is awfully convenient. */ 560 failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff); 561 --failures; /* Undo failures++ in failure_start() */ 562 /* Don't failure_finish() here. */ 563 /* Mark as skip, so doesn't count as failed test. */ 564 failed_lines[skipping_line].skip = 1; 565 ++skips; 566 } 567 568 /* 569 * 570 * ASSERTIONS 571 * 572 */ 573 574 /* Generic assert() just displays the failed condition. */ 575 int 576 assertion_assert(const char *file, int line, int value, 577 const char *condition, void *extra) 578 { 579 assertion_count(file, line); 580 if (!value) { 581 failure_start(file, line, "Assertion failed: %s", condition); 582 failure_finish(extra); 583 } 584 return (value); 585 } 586 587 /* chdir() and report any errors */ 588 int 589 assertion_chdir(const char *file, int line, const char *pathname) 590 { 591 assertion_count(file, line); 592 if (chdir(pathname) == 0) 593 return (1); 594 failure_start(file, line, "chdir(\"%s\")", pathname); 595 failure_finish(NULL); 596 return (0); 597 598 } 599 600 /* change file/directory permissions and errors if it fails */ 601 int 602 assertion_chmod(const char *file, int line, const char *pathname, int mode) 603 { 604 assertion_count(file, line); 605 if (chmod(pathname, mode) == 0) 606 return (1); 607 failure_start(file, line, "chmod(\"%s\", %4.o)", pathname, mode); 608 failure_finish(NULL); 609 return (0); 610 611 } 612 613 /* Verify two integers are equal. */ 614 int 615 assertion_equal_int(const char *file, int line, 616 long long v1, const char *e1, long long v2, const char *e2, void *extra) 617 { 618 assertion_count(file, line); 619 if (v1 == v2) 620 return (1); 621 failure_start(file, line, "%s != %s", e1, e2); 622 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1); 623 logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2); 624 failure_finish(extra); 625 return (0); 626 } 627 628 /* 629 * Utility to convert a single UTF-8 sequence. 630 */ 631 static int 632 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n) 633 { 634 static const char utf8_count[256] = { 635 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */ 636 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */ 637 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */ 638 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */ 639 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */ 640 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */ 641 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */ 642 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */ 643 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */ 644 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */ 645 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */ 646 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */ 647 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */ 648 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */ 649 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */ 650 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */ 651 }; 652 int ch; 653 int cnt; 654 uint32_t wc; 655 656 *pwc = 0; 657 658 /* Sanity check. */ 659 if (n == 0) 660 return (0); 661 /* 662 * Decode 1-4 bytes depending on the value of the first byte. 663 */ 664 ch = (unsigned char)*s; 665 if (ch == 0) 666 return (0); /* Standard: return 0 for end-of-string. */ 667 cnt = utf8_count[ch]; 668 669 /* Invalid sequence or there are not plenty bytes. */ 670 if (n < (size_t)cnt) 671 return (-1); 672 673 /* Make a Unicode code point from a single UTF-8 sequence. */ 674 switch (cnt) { 675 case 1: /* 1 byte sequence. */ 676 *pwc = ch & 0x7f; 677 return (cnt); 678 case 2: /* 2 bytes sequence. */ 679 if ((s[1] & 0xc0) != 0x80) return (-1); 680 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f); 681 return (cnt); 682 case 3: /* 3 bytes sequence. */ 683 if ((s[1] & 0xc0) != 0x80) return (-1); 684 if ((s[2] & 0xc0) != 0x80) return (-1); 685 wc = ((ch & 0x0f) << 12) 686 | ((s[1] & 0x3f) << 6) 687 | (s[2] & 0x3f); 688 if (wc < 0x800) 689 return (-1);/* Overlong sequence. */ 690 break; 691 case 4: /* 4 bytes sequence. */ 692 if (n < 4) 693 return (-1); 694 if ((s[1] & 0xc0) != 0x80) return (-1); 695 if ((s[2] & 0xc0) != 0x80) return (-1); 696 if ((s[3] & 0xc0) != 0x80) return (-1); 697 wc = ((ch & 0x07) << 18) 698 | ((s[1] & 0x3f) << 12) 699 | ((s[2] & 0x3f) << 6) 700 | (s[3] & 0x3f); 701 if (wc < 0x10000) 702 return (-1);/* Overlong sequence. */ 703 break; 704 default: 705 return (-1); 706 } 707 708 /* The code point larger than 0x10FFFF is not legal 709 * Unicode values. */ 710 if (wc > 0x10FFFF) 711 return (-1); 712 /* Correctly gets a Unicode, returns used bytes. */ 713 *pwc = wc; 714 return (cnt); 715 } 716 717 static void strdump(const char *e, const char *p, int ewidth, int utf8) 718 { 719 const char *q = p; 720 721 logprintf(" %*s = ", ewidth, e); 722 if (p == NULL) { 723 logprintf("NULL\n"); 724 return; 725 } 726 logprintf("\""); 727 while (*p != '\0') { 728 unsigned int c = 0xff & *p++; 729 switch (c) { 730 case '\a': logprintf("\\a"); break; 731 case '\b': logprintf("\\b"); break; 732 case '\n': logprintf("\\n"); break; 733 case '\r': logprintf("\\r"); break; 734 default: 735 if (c >= 32 && c < 127) 736 logprintf("%c", c); 737 else 738 logprintf("\\x%02X", c); 739 } 740 } 741 logprintf("\""); 742 logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q)); 743 744 /* 745 * If the current string is UTF-8, dump its code points. 746 */ 747 if (utf8) { 748 size_t len; 749 uint32_t uc; 750 int n; 751 int cnt = 0; 752 753 p = q; 754 len = strlen(p); 755 logprintf(" ["); 756 while ((n = _utf8_to_unicode(&uc, p, len)) > 0) { 757 if (p != q) 758 logprintf(" "); 759 logprintf("%04X", uc); 760 p += n; 761 len -= n; 762 cnt++; 763 } 764 logprintf("]"); 765 logprintf(" (count %d", cnt); 766 if (n < 0) { 767 logprintf(",unknown %zu bytes", len); 768 } 769 logprintf(")"); 770 771 } 772 logprintf("\n"); 773 } 774 775 /* Verify two strings are equal, dump them if not. */ 776 int 777 assertion_equal_string(const char *file, int line, 778 const char *v1, const char *e1, 779 const char *v2, const char *e2, 780 void *extra, int utf8) 781 { 782 int l1, l2; 783 784 assertion_count(file, line); 785 if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0)) 786 return (1); 787 failure_start(file, line, "%s != %s", e1, e2); 788 l1 = (int)strlen(e1); 789 l2 = (int)strlen(e2); 790 if (l1 < l2) 791 l1 = l2; 792 strdump(e1, v1, l1, utf8); 793 strdump(e2, v2, l1, utf8); 794 failure_finish(extra); 795 return (0); 796 } 797 798 static void 799 wcsdump(const char *e, const wchar_t *w) 800 { 801 logprintf(" %s = ", e); 802 if (w == NULL) { 803 logprintf("(null)"); 804 return; 805 } 806 logprintf("\""); 807 while (*w != L'\0') { 808 unsigned int c = *w++; 809 if (c >= 32 && c < 127) 810 logprintf("%c", c); 811 else if (c < 256) 812 logprintf("\\x%02X", c); 813 else if (c < 0x10000) 814 logprintf("\\u%04X", c); 815 else 816 logprintf("\\U%08X", c); 817 } 818 logprintf("\"\n"); 819 } 820 821 #ifndef HAVE_WCSCMP 822 static int 823 wcscmp(const wchar_t *s1, const wchar_t *s2) 824 { 825 826 while (*s1 == *s2++) { 827 if (*s1++ == L'\0') 828 return 0; 829 } 830 if (*s1 > *--s2) 831 return 1; 832 else 833 return -1; 834 } 835 #endif 836 837 /* Verify that two wide strings are equal, dump them if not. */ 838 int 839 assertion_equal_wstring(const char *file, int line, 840 const wchar_t *v1, const char *e1, 841 const wchar_t *v2, const char *e2, 842 void *extra) 843 { 844 assertion_count(file, line); 845 if (v1 == v2) 846 return (1); 847 if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0) 848 return (1); 849 failure_start(file, line, "%s != %s", e1, e2); 850 wcsdump(e1, v1); 851 wcsdump(e2, v2); 852 failure_finish(extra); 853 return (0); 854 } 855 856 /* 857 * Pretty standard hexdump routine. As a bonus, if ref != NULL, then 858 * any bytes in p that differ from ref will be highlighted with '_' 859 * before and after the hex value. 860 */ 861 static void 862 hexdump(const char *p, const char *ref, size_t l, size_t offset) 863 { 864 size_t i, j; 865 char sep; 866 867 if (p == NULL) { 868 logprintf("(null)\n"); 869 return; 870 } 871 for(i=0; i < l; i+=16) { 872 logprintf("%04x", (unsigned)(i + offset)); 873 sep = ' '; 874 for (j = 0; j < 16 && i + j < l; j++) { 875 if (ref != NULL && p[i + j] != ref[i + j]) 876 sep = '_'; 877 logprintf("%c%02x", sep, 0xff & (int)p[i+j]); 878 if (ref != NULL && p[i + j] == ref[i + j]) 879 sep = ' '; 880 } 881 for (; j < 16; j++) { 882 logprintf("%c ", sep); 883 sep = ' '; 884 } 885 logprintf("%c", sep); 886 for (j=0; j < 16 && i + j < l; j++) { 887 int c = p[i + j]; 888 if (c >= ' ' && c <= 126) 889 logprintf("%c", c); 890 else 891 logprintf("."); 892 } 893 logprintf("\n"); 894 } 895 } 896 897 /* Verify that two blocks of memory are the same, display the first 898 * block of differences if they're not. */ 899 int 900 assertion_equal_mem(const char *file, int line, 901 const void *_v1, const char *e1, 902 const void *_v2, const char *e2, 903 size_t l, const char *ld, void *extra) 904 { 905 const char *v1 = (const char *)_v1; 906 const char *v2 = (const char *)_v2; 907 size_t offset; 908 909 assertion_count(file, line); 910 if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0)) 911 return (1); 912 if (v1 == NULL || v2 == NULL) 913 return (0); 914 915 failure_start(file, line, "%s != %s", e1, e2); 916 logprintf(" size %s = %d\n", ld, (int)l); 917 /* Dump 48 bytes (3 lines) so that the first difference is 918 * in the second line. */ 919 offset = 0; 920 while (l > 64 && memcmp(v1, v2, 32) == 0) { 921 /* Two lines agree, so step forward one line. */ 922 v1 += 16; 923 v2 += 16; 924 l -= 16; 925 offset += 16; 926 } 927 logprintf(" Dump of %s\n", e1); 928 hexdump(v1, v2, l < 128 ? l : 128, offset); 929 logprintf(" Dump of %s\n", e2); 930 hexdump(v2, v1, l < 128 ? l : 128, offset); 931 logprintf("\n"); 932 failure_finish(extra); 933 return (0); 934 } 935 936 /* Verify that a block of memory is filled with the specified byte. */ 937 int 938 assertion_memory_filled_with(const char *file, int line, 939 const void *_v1, const char *vd, 940 size_t l, const char *ld, 941 char b, const char *bd, void *extra) 942 { 943 const char *v1 = (const char *)_v1; 944 size_t c = 0; 945 size_t i; 946 (void)ld; /* UNUSED */ 947 948 assertion_count(file, line); 949 950 for (i = 0; i < l; ++i) { 951 if (v1[i] == b) { 952 ++c; 953 } 954 } 955 if (c == l) 956 return (1); 957 958 failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd); 959 logprintf(" Only %d bytes were correct\n", (int)c); 960 failure_finish(extra); 961 return (0); 962 } 963 964 /* Verify that the named file exists and is empty. */ 965 int 966 assertion_empty_file(const char *filename, int line, const char *f1) 967 { 968 char buff[1024]; 969 struct stat st; 970 ssize_t s; 971 FILE *f; 972 973 assertion_count(filename, line); 974 975 if (stat(f1, &st) != 0) { 976 failure_start(filename, line, "Stat failed: %s", f1); 977 failure_finish(NULL); 978 return (0); 979 } 980 if (st.st_size == 0) 981 return (1); 982 983 failure_start(filename, line, "File should be empty: %s", f1); 984 logprintf(" File size: %d\n", (int)st.st_size); 985 logprintf(" Contents:\n"); 986 f = fopen(f1, "rb"); 987 if (f == NULL) { 988 logprintf(" Unable to open %s\n", f1); 989 } else { 990 s = ((off_t)sizeof(buff) < st.st_size) ? 991 (ssize_t)sizeof(buff) : (ssize_t)st.st_size; 992 s = fread(buff, 1, s, f); 993 hexdump(buff, NULL, s, 0); 994 fclose(f); 995 } 996 failure_finish(NULL); 997 return (0); 998 } 999 1000 /* Verify that the named file exists and is not empty. */ 1001 int 1002 assertion_non_empty_file(const char *filename, int line, const char *f1) 1003 { 1004 struct stat st; 1005 1006 assertion_count(filename, line); 1007 1008 if (stat(f1, &st) != 0) { 1009 failure_start(filename, line, "Stat failed: %s", f1); 1010 failure_finish(NULL); 1011 return (0); 1012 } 1013 if (st.st_size == 0) { 1014 failure_start(filename, line, "File empty: %s", f1); 1015 failure_finish(NULL); 1016 return (0); 1017 } 1018 return (1); 1019 } 1020 1021 /* Verify that two files have the same contents. */ 1022 /* TODO: hexdump the first bytes that actually differ. */ 1023 int 1024 assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2) 1025 { 1026 char buff1[1024]; 1027 char buff2[1024]; 1028 FILE *f1, *f2; 1029 int n1, n2; 1030 1031 assertion_count(filename, line); 1032 1033 f1 = fopen(fn1, "rb"); 1034 f2 = fopen(fn2, "rb"); 1035 if (f1 == NULL || f2 == NULL) { 1036 if (f1) fclose(f1); 1037 if (f2) fclose(f2); 1038 return (0); 1039 } 1040 for (;;) { 1041 n1 = (int)fread(buff1, 1, sizeof(buff1), f1); 1042 n2 = (int)fread(buff2, 1, sizeof(buff2), f2); 1043 if (n1 != n2) 1044 break; 1045 if (n1 == 0 && n2 == 0) { 1046 fclose(f1); 1047 fclose(f2); 1048 return (1); 1049 } 1050 if (memcmp(buff1, buff2, n1) != 0) 1051 break; 1052 } 1053 fclose(f1); 1054 fclose(f2); 1055 failure_start(filename, line, "Files not identical"); 1056 logprintf(" file1=\"%s\"\n", fn1); 1057 logprintf(" file2=\"%s\"\n", fn2); 1058 failure_finish(NULL); 1059 return (0); 1060 } 1061 1062 /* Verify that the named file does exist. */ 1063 int 1064 assertion_file_exists(const char *filename, int line, const char *f) 1065 { 1066 assertion_count(filename, line); 1067 1068 #if defined(_WIN32) && !defined(__CYGWIN__) 1069 if (!_access(f, 0)) 1070 return (1); 1071 #else 1072 if (!access(f, F_OK)) 1073 return (1); 1074 #endif 1075 failure_start(filename, line, "File should exist: %s", f); 1076 failure_finish(NULL); 1077 return (0); 1078 } 1079 1080 /* Verify that the named file doesn't exist. */ 1081 int 1082 assertion_file_not_exists(const char *filename, int line, const char *f) 1083 { 1084 assertion_count(filename, line); 1085 1086 #if defined(_WIN32) && !defined(__CYGWIN__) 1087 if (_access(f, 0)) 1088 return (1); 1089 #else 1090 if (access(f, F_OK)) 1091 return (1); 1092 #endif 1093 failure_start(filename, line, "File should not exist: %s", f); 1094 failure_finish(NULL); 1095 return (0); 1096 } 1097 1098 /* Compare the contents of a file to a block of memory. */ 1099 int 1100 assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn) 1101 { 1102 char *contents; 1103 FILE *f; 1104 int n; 1105 1106 assertion_count(filename, line); 1107 1108 f = fopen(fn, "rb"); 1109 if (f == NULL) { 1110 failure_start(filename, line, 1111 "File should exist: %s", fn); 1112 failure_finish(NULL); 1113 return (0); 1114 } 1115 contents = malloc(s * 2); 1116 n = (int)fread(contents, 1, s * 2, f); 1117 fclose(f); 1118 if (n == s && memcmp(buff, contents, s) == 0) { 1119 free(contents); 1120 return (1); 1121 } 1122 failure_start(filename, line, "File contents don't match"); 1123 logprintf(" file=\"%s\"\n", fn); 1124 if (n > 0) 1125 hexdump(contents, buff, n > 512 ? 512 : n, 0); 1126 else { 1127 logprintf(" File empty, contents should be:\n"); 1128 hexdump(buff, NULL, s > 512 ? 512 : s, 0); 1129 } 1130 failure_finish(NULL); 1131 free(contents); 1132 return (0); 1133 } 1134 1135 /* Check the contents of a text file, being tolerant of line endings. */ 1136 int 1137 assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn) 1138 { 1139 char *contents; 1140 const char *btxt, *ftxt; 1141 FILE *f; 1142 int n, s; 1143 1144 assertion_count(filename, line); 1145 f = fopen(fn, "r"); 1146 if (f == NULL) { 1147 failure_start(filename, line, 1148 "File doesn't exist: %s", fn); 1149 failure_finish(NULL); 1150 return (0); 1151 } 1152 s = (int)strlen(buff); 1153 contents = malloc(s * 2 + 128); 1154 n = (int)fread(contents, 1, s * 2 + 128 - 1, f); 1155 if (n >= 0) 1156 contents[n] = '\0'; 1157 fclose(f); 1158 /* Compare texts. */ 1159 btxt = buff; 1160 ftxt = (const char *)contents; 1161 while (*btxt != '\0' && *ftxt != '\0') { 1162 if (*btxt == *ftxt) { 1163 ++btxt; 1164 ++ftxt; 1165 continue; 1166 } 1167 if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') { 1168 /* Pass over different new line characters. */ 1169 ++btxt; 1170 ftxt += 2; 1171 continue; 1172 } 1173 break; 1174 } 1175 if (*btxt == '\0' && *ftxt == '\0') { 1176 free(contents); 1177 return (1); 1178 } 1179 failure_start(filename, line, "Contents don't match"); 1180 logprintf(" file=\"%s\"\n", fn); 1181 if (n > 0) { 1182 hexdump(contents, buff, n, 0); 1183 logprintf(" expected\n"); 1184 hexdump(buff, contents, s, 0); 1185 } else { 1186 logprintf(" File empty, contents should be:\n"); 1187 hexdump(buff, NULL, s, 0); 1188 } 1189 failure_finish(NULL); 1190 free(contents); 1191 return (0); 1192 } 1193 1194 /* Verify that a text file contains the specified lines, regardless of order */ 1195 /* This could be more efficient if we sorted both sets of lines, etc, but 1196 * since this is used only for testing and only ever deals with a dozen or so 1197 * lines at a time, this relatively crude approach is just fine. */ 1198 int 1199 assertion_file_contains_lines_any_order(const char *file, int line, 1200 const char *pathname, const char *lines[]) 1201 { 1202 char *buff; 1203 size_t buff_size; 1204 size_t expected_count, actual_count, i, j; 1205 char **expected = NULL; 1206 char *p, **actual = NULL; 1207 char c; 1208 int expected_failure = 0, actual_failure = 0; 1209 1210 assertion_count(file, line); 1211 1212 buff = slurpfile(&buff_size, "%s", pathname); 1213 if (buff == NULL) { 1214 failure_start(pathname, line, "Can't read file: %s", pathname); 1215 failure_finish(NULL); 1216 return (0); 1217 } 1218 1219 /* Make a copy of the provided lines and count up the expected 1220 * file size. */ 1221 for (i = 0; lines[i] != NULL; ++i) { 1222 } 1223 expected_count = i; 1224 if (expected_count) { 1225 expected = malloc(sizeof(char *) * expected_count); 1226 if (expected == NULL) { 1227 failure_start(pathname, line, "Can't allocate memory"); 1228 failure_finish(NULL); 1229 free(expected); 1230 free(buff); 1231 return (0); 1232 } 1233 for (i = 0; lines[i] != NULL; ++i) { 1234 expected[i] = strdup(lines[i]); 1235 } 1236 } 1237 1238 /* Break the file into lines */ 1239 actual_count = 0; 1240 for (c = '\0', p = buff; p < buff + buff_size; ++p) { 1241 if (*p == '\x0d' || *p == '\x0a') 1242 *p = '\0'; 1243 if (c == '\0' && *p != '\0') 1244 ++actual_count; 1245 c = *p; 1246 } 1247 if (actual_count) { 1248 actual = calloc(sizeof(char *), actual_count); 1249 if (actual == NULL) { 1250 failure_start(pathname, line, "Can't allocate memory"); 1251 failure_finish(NULL); 1252 free(expected); 1253 free(buff); 1254 return (0); 1255 } 1256 for (j = 0, p = buff; p < buff + buff_size; 1257 p += 1 + strlen(p)) { 1258 if (*p != '\0') { 1259 actual[j] = p; 1260 ++j; 1261 } 1262 } 1263 } 1264 1265 /* Erase matching lines from both lists */ 1266 for (i = 0; i < expected_count; ++i) { 1267 if (expected[i] == NULL) 1268 continue; 1269 for (j = 0; j < actual_count; ++j) { 1270 if (actual[j] == NULL) 1271 continue; 1272 if (strcmp(expected[i], actual[j]) == 0) { 1273 free(expected[i]); 1274 expected[i] = NULL; 1275 actual[j] = NULL; 1276 break; 1277 } 1278 } 1279 } 1280 1281 /* If there's anything left, it's a failure */ 1282 for (i = 0; i < expected_count; ++i) { 1283 if (expected[i] != NULL) 1284 ++expected_failure; 1285 } 1286 for (j = 0; j < actual_count; ++j) { 1287 if (actual[j] != NULL) 1288 ++actual_failure; 1289 } 1290 if (expected_failure == 0 && actual_failure == 0) { 1291 free(buff); 1292 free(expected); 1293 free(actual); 1294 return (1); 1295 } 1296 failure_start(file, line, "File doesn't match: %s", pathname); 1297 for (i = 0; i < expected_count; ++i) { 1298 if (expected[i] != NULL) { 1299 logprintf(" Expected but not present: %s\n", expected[i]); 1300 free(expected[i]); 1301 } 1302 } 1303 for (j = 0; j < actual_count; ++j) { 1304 if (actual[j] != NULL) 1305 logprintf(" Present but not expected: %s\n", actual[j]); 1306 } 1307 failure_finish(NULL); 1308 free(buff); 1309 free(expected); 1310 free(actual); 1311 return (0); 1312 } 1313 1314 /* Verify that a text file does not contains the specified strings */ 1315 int 1316 assertion_file_contains_no_invalid_strings(const char *file, int line, 1317 const char *pathname, const char *strings[]) 1318 { 1319 char *buff; 1320 int i; 1321 1322 buff = slurpfile(NULL, "%s", pathname); 1323 if (buff == NULL) { 1324 failure_start(file, line, "Can't read file: %s", pathname); 1325 failure_finish(NULL); 1326 return (0); 1327 } 1328 1329 for (i = 0; strings[i] != NULL; ++i) { 1330 if (strstr(buff, strings[i]) != NULL) { 1331 failure_start(file, line, "Invalid string in %s: %s", pathname, 1332 strings[i]); 1333 failure_finish(NULL); 1334 free(buff); 1335 return(0); 1336 } 1337 } 1338 1339 free(buff); 1340 return (0); 1341 } 1342 1343 /* Test that two paths point to the same file. */ 1344 /* As a side-effect, asserts that both files exist. */ 1345 static int 1346 is_hardlink(const char *file, int line, 1347 const char *path1, const char *path2) 1348 { 1349 #if defined(_WIN32) && !defined(__CYGWIN__) 1350 BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2; 1351 int r; 1352 1353 assertion_count(file, line); 1354 r = my_GetFileInformationByName(path1, &bhfi1); 1355 if (r == 0) { 1356 failure_start(file, line, "File %s can't be inspected?", path1); 1357 failure_finish(NULL); 1358 return (0); 1359 } 1360 r = my_GetFileInformationByName(path2, &bhfi2); 1361 if (r == 0) { 1362 failure_start(file, line, "File %s can't be inspected?", path2); 1363 failure_finish(NULL); 1364 return (0); 1365 } 1366 return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber 1367 && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh 1368 && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow); 1369 #else 1370 struct stat st1, st2; 1371 int r; 1372 1373 assertion_count(file, line); 1374 r = lstat(path1, &st1); 1375 if (r != 0) { 1376 failure_start(file, line, "File should exist: %s", path1); 1377 failure_finish(NULL); 1378 return (0); 1379 } 1380 r = lstat(path2, &st2); 1381 if (r != 0) { 1382 failure_start(file, line, "File should exist: %s", path2); 1383 failure_finish(NULL); 1384 return (0); 1385 } 1386 return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev); 1387 #endif 1388 } 1389 1390 int 1391 assertion_is_hardlink(const char *file, int line, 1392 const char *path1, const char *path2) 1393 { 1394 if (is_hardlink(file, line, path1, path2)) 1395 return (1); 1396 failure_start(file, line, 1397 "Files %s and %s are not hardlinked", path1, path2); 1398 failure_finish(NULL); 1399 return (0); 1400 } 1401 1402 int 1403 assertion_is_not_hardlink(const char *file, int line, 1404 const char *path1, const char *path2) 1405 { 1406 if (!is_hardlink(file, line, path1, path2)) 1407 return (1); 1408 failure_start(file, line, 1409 "Files %s and %s should not be hardlinked", path1, path2); 1410 failure_finish(NULL); 1411 return (0); 1412 } 1413 1414 /* Verify a/b/mtime of 'pathname'. */ 1415 /* If 'recent', verify that it's within last 10 seconds. */ 1416 static int 1417 assertion_file_time(const char *file, int line, 1418 const char *pathname, long t, long nsec, char type, int recent) 1419 { 1420 long long filet, filet_nsec; 1421 int r; 1422 1423 #if defined(_WIN32) && !defined(__CYGWIN__) 1424 #define EPOC_TIME (116444736000000000ULL) 1425 FILETIME fxtime, fbirthtime, fatime, fmtime; 1426 ULARGE_INTEGER wintm; 1427 HANDLE h; 1428 fxtime.dwLowDateTime = 0; 1429 fxtime.dwHighDateTime = 0; 1430 1431 assertion_count(file, line); 1432 /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open 1433 * a directory file. If not, CreateFile() will fail when 1434 * the pathname is a directory. */ 1435 h = CreateFile(pathname, FILE_READ_ATTRIBUTES, 0, NULL, 1436 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 1437 if (h == INVALID_HANDLE_VALUE) { 1438 failure_start(file, line, "Can't access %s\n", pathname); 1439 failure_finish(NULL); 1440 return (0); 1441 } 1442 r = GetFileTime(h, &fbirthtime, &fatime, &fmtime); 1443 switch (type) { 1444 case 'a': fxtime = fatime; break; 1445 case 'b': fxtime = fbirthtime; break; 1446 case 'm': fxtime = fmtime; break; 1447 } 1448 CloseHandle(h); 1449 if (r == 0) { 1450 failure_start(file, line, "Can't GetFileTime %s\n", pathname); 1451 failure_finish(NULL); 1452 return (0); 1453 } 1454 wintm.LowPart = fxtime.dwLowDateTime; 1455 wintm.HighPart = fxtime.dwHighDateTime; 1456 filet = (wintm.QuadPart - EPOC_TIME) / 10000000; 1457 filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100; 1458 nsec = (nsec / 100) * 100; /* Round the request */ 1459 #else 1460 struct stat st; 1461 1462 assertion_count(file, line); 1463 r = lstat(pathname, &st); 1464 if (r != 0) { 1465 failure_start(file, line, "Can't stat %s\n", pathname); 1466 failure_finish(NULL); 1467 return (0); 1468 } 1469 switch (type) { 1470 case 'a': filet = st.st_atime; break; 1471 case 'm': filet = st.st_mtime; break; 1472 case 'b': filet = 0; break; 1473 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1474 exit(1); 1475 } 1476 #if defined(__FreeBSD__) 1477 switch (type) { 1478 case 'a': filet_nsec = st.st_atimespec.tv_nsec; break; 1479 case 'b': filet = st.st_birthtime; 1480 /* FreeBSD filesystems that don't support birthtime 1481 * (e.g., UFS1) always return -1 here. */ 1482 if (filet == -1) { 1483 return (1); 1484 } 1485 filet_nsec = st.st_birthtimespec.tv_nsec; break; 1486 case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break; 1487 default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type); 1488 exit(1); 1489 } 1490 /* FreeBSD generally only stores to microsecond res, so round. */ 1491 filet_nsec = (filet_nsec / 1000) * 1000; 1492 nsec = (nsec / 1000) * 1000; 1493 #else 1494 filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */ 1495 if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */ 1496 #if defined(__HAIKU__) 1497 if (type == 'a') return (1); /* Haiku doesn't have atime. */ 1498 #endif 1499 #endif 1500 #endif 1501 if (recent) { 1502 /* Check that requested time is up-to-date. */ 1503 time_t now = time(NULL); 1504 if (filet < now - 10 || filet > now + 1) { 1505 failure_start(file, line, 1506 "File %s has %ctime %lld, %lld seconds ago\n", 1507 pathname, type, filet, now - filet); 1508 failure_finish(NULL); 1509 return (0); 1510 } 1511 } else if (filet != t || filet_nsec != nsec) { 1512 failure_start(file, line, 1513 "File %s has %ctime %lld.%09lld, expected %ld.%09ld", 1514 pathname, type, filet, filet_nsec, t, nsec); 1515 failure_finish(NULL); 1516 return (0); 1517 } 1518 return (1); 1519 } 1520 1521 /* Verify atime of 'pathname'. */ 1522 int 1523 assertion_file_atime(const char *file, int line, 1524 const char *pathname, long t, long nsec) 1525 { 1526 return assertion_file_time(file, line, pathname, t, nsec, 'a', 0); 1527 } 1528 1529 /* Verify atime of 'pathname' is up-to-date. */ 1530 int 1531 assertion_file_atime_recent(const char *file, int line, const char *pathname) 1532 { 1533 return assertion_file_time(file, line, pathname, 0, 0, 'a', 1); 1534 } 1535 1536 /* Verify birthtime of 'pathname'. */ 1537 int 1538 assertion_file_birthtime(const char *file, int line, 1539 const char *pathname, long t, long nsec) 1540 { 1541 return assertion_file_time(file, line, pathname, t, nsec, 'b', 0); 1542 } 1543 1544 /* Verify birthtime of 'pathname' is up-to-date. */ 1545 int 1546 assertion_file_birthtime_recent(const char *file, int line, 1547 const char *pathname) 1548 { 1549 return assertion_file_time(file, line, pathname, 0, 0, 'b', 1); 1550 } 1551 1552 /* Verify mode of 'pathname'. */ 1553 int 1554 assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode) 1555 { 1556 int mode; 1557 int r; 1558 1559 assertion_count(file, line); 1560 #if defined(_WIN32) && !defined(__CYGWIN__) 1561 failure_start(file, line, "assertFileMode not yet implemented for Windows"); 1562 (void)mode; /* UNUSED */ 1563 (void)r; /* UNUSED */ 1564 (void)pathname; /* UNUSED */ 1565 (void)expected_mode; /* UNUSED */ 1566 #else 1567 { 1568 struct stat st; 1569 r = lstat(pathname, &st); 1570 mode = (int)(st.st_mode & 0777); 1571 } 1572 if (r == 0 && mode == expected_mode) 1573 return (1); 1574 failure_start(file, line, "File %s has mode %o, expected %o", 1575 pathname, mode, expected_mode); 1576 #endif 1577 failure_finish(NULL); 1578 return (0); 1579 } 1580 1581 /* Verify mtime of 'pathname'. */ 1582 int 1583 assertion_file_mtime(const char *file, int line, 1584 const char *pathname, long t, long nsec) 1585 { 1586 return assertion_file_time(file, line, pathname, t, nsec, 'm', 0); 1587 } 1588 1589 /* Verify mtime of 'pathname' is up-to-date. */ 1590 int 1591 assertion_file_mtime_recent(const char *file, int line, const char *pathname) 1592 { 1593 return assertion_file_time(file, line, pathname, 0, 0, 'm', 1); 1594 } 1595 1596 /* Verify number of links to 'pathname'. */ 1597 int 1598 assertion_file_nlinks(const char *file, int line, 1599 const char *pathname, int nlinks) 1600 { 1601 #if defined(_WIN32) && !defined(__CYGWIN__) 1602 BY_HANDLE_FILE_INFORMATION bhfi; 1603 int r; 1604 1605 assertion_count(file, line); 1606 r = my_GetFileInformationByName(pathname, &bhfi); 1607 if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks) 1608 return (1); 1609 failure_start(file, line, "File %s has %jd links, expected %d", 1610 pathname, (intmax_t)bhfi.nNumberOfLinks, nlinks); 1611 failure_finish(NULL); 1612 return (0); 1613 #else 1614 struct stat st; 1615 int r; 1616 1617 assertion_count(file, line); 1618 r = lstat(pathname, &st); 1619 if (r == 0 && (int)st.st_nlink == nlinks) 1620 return (1); 1621 failure_start(file, line, "File %s has %jd links, expected %d", 1622 pathname, (intmax_t)st.st_nlink, nlinks); 1623 failure_finish(NULL); 1624 return (0); 1625 #endif 1626 } 1627 1628 /* Verify size of 'pathname'. */ 1629 int 1630 assertion_file_size(const char *file, int line, const char *pathname, long size) 1631 { 1632 int64_t filesize; 1633 int r; 1634 1635 assertion_count(file, line); 1636 #if defined(_WIN32) && !defined(__CYGWIN__) 1637 { 1638 BY_HANDLE_FILE_INFORMATION bhfi; 1639 r = !my_GetFileInformationByName(pathname, &bhfi); 1640 filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow; 1641 } 1642 #else 1643 { 1644 struct stat st; 1645 r = lstat(pathname, &st); 1646 filesize = st.st_size; 1647 } 1648 #endif 1649 if (r == 0 && filesize == size) 1650 return (1); 1651 failure_start(file, line, "File %s has size %ld, expected %ld", 1652 pathname, (long)filesize, (long)size); 1653 failure_finish(NULL); 1654 return (0); 1655 } 1656 1657 /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */ 1658 int 1659 assertion_is_dir(const char *file, int line, const char *pathname, int mode) 1660 { 1661 struct stat st; 1662 int r; 1663 1664 #if defined(_WIN32) && !defined(__CYGWIN__) 1665 (void)mode; /* UNUSED */ 1666 #endif 1667 assertion_count(file, line); 1668 r = lstat(pathname, &st); 1669 if (r != 0) { 1670 failure_start(file, line, "Dir should exist: %s", pathname); 1671 failure_finish(NULL); 1672 return (0); 1673 } 1674 if (!S_ISDIR(st.st_mode)) { 1675 failure_start(file, line, "%s is not a dir", pathname); 1676 failure_finish(NULL); 1677 return (0); 1678 } 1679 #if !defined(_WIN32) || defined(__CYGWIN__) 1680 /* Windows doesn't handle permissions the same way as POSIX, 1681 * so just ignore the mode tests. */ 1682 /* TODO: Can we do better here? */ 1683 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1684 failure_start(file, line, "Dir %s has wrong mode", pathname); 1685 logprintf(" Expected: 0%3o\n", mode); 1686 logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1687 failure_finish(NULL); 1688 return (0); 1689 } 1690 #endif 1691 return (1); 1692 } 1693 1694 /* Verify that 'pathname' is a regular file. If 'mode' is >= 0, 1695 * verify that too. */ 1696 int 1697 assertion_is_reg(const char *file, int line, const char *pathname, int mode) 1698 { 1699 struct stat st; 1700 int r; 1701 1702 #if defined(_WIN32) && !defined(__CYGWIN__) 1703 (void)mode; /* UNUSED */ 1704 #endif 1705 assertion_count(file, line); 1706 r = lstat(pathname, &st); 1707 if (r != 0 || !S_ISREG(st.st_mode)) { 1708 failure_start(file, line, "File should exist: %s", pathname); 1709 failure_finish(NULL); 1710 return (0); 1711 } 1712 #if !defined(_WIN32) || defined(__CYGWIN__) 1713 /* Windows doesn't handle permissions the same way as POSIX, 1714 * so just ignore the mode tests. */ 1715 /* TODO: Can we do better here? */ 1716 if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) { 1717 failure_start(file, line, "File %s has wrong mode", pathname); 1718 logprintf(" Expected: 0%3o\n", mode); 1719 logprintf(" Found: 0%3o\n", st.st_mode & 07777); 1720 failure_finish(NULL); 1721 return (0); 1722 } 1723 #endif 1724 return (1); 1725 } 1726 1727 /* 1728 * Check whether 'pathname' is a symbolic link. If 'contents' is 1729 * non-NULL, verify that the symlink has those contents. 1730 * 1731 * On platforms with directory symlinks, set isdir to 0 to test for a file 1732 * symlink and to 1 to test for a directory symlink. On other platforms 1733 * the variable is ignored. 1734 */ 1735 static int 1736 is_symlink(const char *file, int line, 1737 const char *pathname, const char *contents, int isdir) 1738 { 1739 #if defined(_WIN32) && !defined(__CYGWIN__) 1740 HANDLE h; 1741 DWORD inbytes; 1742 REPARSE_DATA_BUFFER *buf; 1743 BY_HANDLE_FILE_INFORMATION st; 1744 size_t len, len2; 1745 wchar_t *linknamew, *contentsw; 1746 const char *p; 1747 char *s, *pn; 1748 int ret = 0; 1749 BYTE *indata; 1750 const DWORD flag = FILE_FLAG_BACKUP_SEMANTICS | 1751 FILE_FLAG_OPEN_REPARSE_POINT; 1752 1753 /* Replace slashes with backslashes in pathname */ 1754 pn = malloc((strlen(pathname) + 1) * sizeof(char)); 1755 p = pathname; 1756 s = pn; 1757 while(*p != '\0') { 1758 if(*p == '/') 1759 *s = '\\'; 1760 else 1761 *s = *p; 1762 p++; 1763 s++; 1764 } 1765 *s = '\0'; 1766 1767 h = CreateFileA(pn, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 1768 flag, NULL); 1769 free(pn); 1770 if (h == INVALID_HANDLE_VALUE) { 1771 failure_start(file, line, "Can't access %s\n", pathname); 1772 failure_finish(NULL); 1773 return (0); 1774 } 1775 ret = GetFileInformationByHandle(h, &st); 1776 if (ret == 0) { 1777 failure_start(file, line, 1778 "Can't stat: %s", pathname); 1779 failure_finish(NULL); 1780 } else if ((st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) { 1781 failure_start(file, line, 1782 "Not a symlink: %s", pathname); 1783 failure_finish(NULL); 1784 ret = 0; 1785 } 1786 if (isdir && ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)) { 1787 failure_start(file, line, 1788 "Not a directory symlink: %s", pathname); 1789 failure_finish(NULL); 1790 ret = 0; 1791 } 1792 if (!isdir && 1793 ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) { 1794 failure_start(file, line, 1795 "Not a file symlink: %s", pathname); 1796 failure_finish(NULL); 1797 ret = 0; 1798 } 1799 if (ret == 0) { 1800 CloseHandle(h); 1801 return (0); 1802 } 1803 1804 indata = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); 1805 ret = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, indata, 1806 1024, &inbytes, NULL); 1807 CloseHandle(h); 1808 if (ret == 0) { 1809 free(indata); 1810 failure_start(file, line, 1811 "Could not retrieve symlink target: %s", pathname); 1812 failure_finish(NULL); 1813 return (0); 1814 } 1815 1816 buf = (REPARSE_DATA_BUFFER *) indata; 1817 if (buf->ReparseTag != IO_REPARSE_TAG_SYMLINK) { 1818 free(indata); 1819 /* File is not a symbolic link */ 1820 failure_start(file, line, 1821 "Not a symlink: %s", pathname); 1822 failure_finish(NULL); 1823 return (0); 1824 } 1825 1826 if (contents == NULL) { 1827 free(indata); 1828 return (1); 1829 } 1830 1831 len = buf->SymbolicLinkReparseBuffer.SubstituteNameLength; 1832 1833 linknamew = malloc(len + sizeof(wchar_t)); 1834 if (linknamew == NULL) { 1835 free(indata); 1836 return (0); 1837 } 1838 1839 memcpy(linknamew, &((BYTE *)buf->SymbolicLinkReparseBuffer.PathBuffer) 1840 [buf->SymbolicLinkReparseBuffer.SubstituteNameOffset], len); 1841 free(indata); 1842 1843 linknamew[len / sizeof(wchar_t)] = L'\0'; 1844 1845 contentsw = malloc(len + sizeof(wchar_t)); 1846 if (contentsw == NULL) { 1847 free(linknamew); 1848 return (0); 1849 } 1850 1851 len2 = mbsrtowcs(contentsw, &contents, (len + sizeof(wchar_t) 1852 / sizeof(wchar_t)), NULL); 1853 1854 if (len2 > 0 && wcscmp(linknamew, contentsw) != 0) 1855 ret = 1; 1856 1857 free(linknamew); 1858 free(contentsw); 1859 return (ret); 1860 #else 1861 char buff[300]; 1862 struct stat st; 1863 ssize_t linklen; 1864 int r; 1865 1866 (void)isdir; /* UNUSED */ 1867 assertion_count(file, line); 1868 r = lstat(pathname, &st); 1869 if (r != 0) { 1870 failure_start(file, line, 1871 "Symlink should exist: %s", pathname); 1872 failure_finish(NULL); 1873 return (0); 1874 } 1875 if (!S_ISLNK(st.st_mode)) 1876 return (0); 1877 if (contents == NULL) 1878 return (1); 1879 linklen = readlink(pathname, buff, sizeof(buff) - 1); 1880 if (linklen < 0) { 1881 failure_start(file, line, "Can't read symlink %s", pathname); 1882 failure_finish(NULL); 1883 return (0); 1884 } 1885 buff[linklen] = '\0'; 1886 if (strcmp(buff, contents) != 0) 1887 return (0); 1888 return (1); 1889 #endif 1890 } 1891 1892 /* Assert that path is a symlink that (optionally) contains contents. */ 1893 int 1894 assertion_is_symlink(const char *file, int line, 1895 const char *path, const char *contents, int isdir) 1896 { 1897 if (is_symlink(file, line, path, contents, isdir)) 1898 return (1); 1899 if (contents) 1900 failure_start(file, line, "File %s is not a symlink to %s", 1901 path, contents); 1902 else 1903 failure_start(file, line, "File %s is not a symlink", path); 1904 failure_finish(NULL); 1905 return (0); 1906 } 1907 1908 1909 /* Create a directory and report any errors. */ 1910 int 1911 assertion_make_dir(const char *file, int line, const char *dirname, int mode) 1912 { 1913 assertion_count(file, line); 1914 #if defined(_WIN32) && !defined(__CYGWIN__) 1915 (void)mode; /* UNUSED */ 1916 if (0 == _mkdir(dirname)) 1917 return (1); 1918 #else 1919 if (0 == mkdir(dirname, mode)) { 1920 if (0 == chmod(dirname, mode)) { 1921 assertion_file_mode(file, line, dirname, mode); 1922 return (1); 1923 } 1924 } 1925 #endif 1926 failure_start(file, line, "Could not create directory %s", dirname); 1927 failure_finish(NULL); 1928 return(0); 1929 } 1930 1931 /* Create a file with the specified contents and report any failures. */ 1932 int 1933 assertion_make_file(const char *file, int line, 1934 const char *path, int mode, int csize, const void *contents) 1935 { 1936 #if defined(_WIN32) && !defined(__CYGWIN__) 1937 /* TODO: Rework this to set file mode as well. */ 1938 FILE *f; 1939 (void)mode; /* UNUSED */ 1940 assertion_count(file, line); 1941 f = fopen(path, "wb"); 1942 if (f == NULL) { 1943 failure_start(file, line, "Could not create file %s", path); 1944 failure_finish(NULL); 1945 return (0); 1946 } 1947 if (contents != NULL) { 1948 size_t wsize; 1949 1950 if (csize < 0) 1951 wsize = strlen(contents); 1952 else 1953 wsize = (size_t)csize; 1954 if (wsize != fwrite(contents, 1, wsize, f)) { 1955 fclose(f); 1956 failure_start(file, line, 1957 "Could not write file %s", path); 1958 failure_finish(NULL); 1959 return (0); 1960 } 1961 } 1962 fclose(f); 1963 return (1); 1964 #else 1965 int fd; 1966 assertion_count(file, line); 1967 fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644); 1968 if (fd < 0) { 1969 failure_start(file, line, "Could not create %s", path); 1970 failure_finish(NULL); 1971 return (0); 1972 } 1973 #ifdef HAVE_FCHMOD 1974 if (0 != fchmod(fd, mode)) 1975 #else 1976 if (0 != chmod(path, mode)) 1977 #endif 1978 { 1979 failure_start(file, line, "Could not chmod %s", path); 1980 failure_finish(NULL); 1981 close(fd); 1982 return (0); 1983 } 1984 if (contents != NULL) { 1985 ssize_t wsize; 1986 1987 if (csize < 0) 1988 wsize = (ssize_t)strlen(contents); 1989 else 1990 wsize = (ssize_t)csize; 1991 if (wsize != write(fd, contents, wsize)) { 1992 close(fd); 1993 failure_start(file, line, 1994 "Could not write to %s", path); 1995 failure_finish(NULL); 1996 close(fd); 1997 return (0); 1998 } 1999 } 2000 close(fd); 2001 assertion_file_mode(file, line, path, mode); 2002 return (1); 2003 #endif 2004 } 2005 2006 /* Create a hardlink and report any failures. */ 2007 int 2008 assertion_make_hardlink(const char *file, int line, 2009 const char *newpath, const char *linkto) 2010 { 2011 int succeeded; 2012 2013 assertion_count(file, line); 2014 #if defined(_WIN32) && !defined(__CYGWIN__) 2015 succeeded = my_CreateHardLinkA(newpath, linkto); 2016 #elif HAVE_LINK 2017 succeeded = !link(linkto, newpath); 2018 #else 2019 succeeded = 0; 2020 #endif 2021 if (succeeded) 2022 return (1); 2023 failure_start(file, line, "Could not create hardlink"); 2024 logprintf(" New link: %s\n", newpath); 2025 logprintf(" Old name: %s\n", linkto); 2026 failure_finish(NULL); 2027 return(0); 2028 } 2029 2030 /* 2031 * Create a symlink and report any failures. 2032 * 2033 * Windows symlinks need to know if the target is a directory. 2034 */ 2035 int 2036 assertion_make_symlink(const char *file, int line, 2037 const char *newpath, const char *linkto, int targetIsDir) 2038 { 2039 #if defined(_WIN32) && !defined(__CYGWIN__) 2040 assertion_count(file, line); 2041 if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir)) 2042 return (1); 2043 #elif HAVE_SYMLINK 2044 (void)targetIsDir; /* UNUSED */ 2045 assertion_count(file, line); 2046 if (0 == symlink(linkto, newpath)) 2047 return (1); 2048 #else 2049 (void)targetIsDir; /* UNUSED */ 2050 #endif 2051 failure_start(file, line, "Could not create symlink"); 2052 logprintf(" New link: %s\n", newpath); 2053 logprintf(" Old name: %s\n", linkto); 2054 failure_finish(NULL); 2055 return(0); 2056 } 2057 2058 /* Set umask, report failures. */ 2059 int 2060 assertion_umask(const char *file, int line, int mask) 2061 { 2062 assertion_count(file, line); 2063 (void)file; /* UNUSED */ 2064 (void)line; /* UNUSED */ 2065 umask(mask); 2066 return (1); 2067 } 2068 2069 /* Set times, report failures. */ 2070 int 2071 assertion_utimes(const char *file, int line, 2072 const char *pathname, long at, long at_nsec, long mt, long mt_nsec) 2073 { 2074 int r; 2075 2076 #if defined(_WIN32) && !defined(__CYGWIN__) 2077 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\ 2078 + (((nsec)/1000)*10)) 2079 HANDLE h; 2080 ULARGE_INTEGER wintm; 2081 FILETIME fatime, fmtime; 2082 FILETIME *pat, *pmt; 2083 2084 assertion_count(file, line); 2085 h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE, 2086 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 2087 FILE_FLAG_BACKUP_SEMANTICS, NULL); 2088 if (h == INVALID_HANDLE_VALUE) { 2089 failure_start(file, line, "Can't access %s\n", pathname); 2090 failure_finish(NULL); 2091 return (0); 2092 } 2093 2094 if (at > 0 || at_nsec > 0) { 2095 wintm.QuadPart = WINTIME(at, at_nsec); 2096 fatime.dwLowDateTime = wintm.LowPart; 2097 fatime.dwHighDateTime = wintm.HighPart; 2098 pat = &fatime; 2099 } else 2100 pat = NULL; 2101 if (mt > 0 || mt_nsec > 0) { 2102 wintm.QuadPart = WINTIME(mt, mt_nsec); 2103 fmtime.dwLowDateTime = wintm.LowPart; 2104 fmtime.dwHighDateTime = wintm.HighPart; 2105 pmt = &fmtime; 2106 } else 2107 pmt = NULL; 2108 if (pat != NULL || pmt != NULL) 2109 r = SetFileTime(h, NULL, pat, pmt); 2110 else 2111 r = 1; 2112 CloseHandle(h); 2113 if (r == 0) { 2114 failure_start(file, line, "Can't SetFileTime %s\n", pathname); 2115 failure_finish(NULL); 2116 return (0); 2117 } 2118 return (1); 2119 #else /* defined(_WIN32) && !defined(__CYGWIN__) */ 2120 struct stat st; 2121 struct timeval times[2]; 2122 2123 #if !defined(__FreeBSD__) 2124 mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */ 2125 #endif 2126 if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0) 2127 return (1); 2128 2129 r = lstat(pathname, &st); 2130 if (r < 0) { 2131 failure_start(file, line, "Can't stat %s\n", pathname); 2132 failure_finish(NULL); 2133 return (0); 2134 } 2135 2136 if (mt == 0 && mt_nsec == 0) { 2137 mt = st.st_mtime; 2138 #if defined(__FreeBSD__) 2139 mt_nsec = st.st_mtimespec.tv_nsec; 2140 /* FreeBSD generally only stores to microsecond res, so round. */ 2141 mt_nsec = (mt_nsec / 1000) * 1000; 2142 #endif 2143 } 2144 if (at == 0 && at_nsec == 0) { 2145 at = st.st_atime; 2146 #if defined(__FreeBSD__) 2147 at_nsec = st.st_atimespec.tv_nsec; 2148 /* FreeBSD generally only stores to microsecond res, so round. */ 2149 at_nsec = (at_nsec / 1000) * 1000; 2150 #endif 2151 } 2152 2153 times[1].tv_sec = mt; 2154 times[1].tv_usec = mt_nsec / 1000; 2155 2156 times[0].tv_sec = at; 2157 times[0].tv_usec = at_nsec / 1000; 2158 2159 #ifdef HAVE_LUTIMES 2160 r = lutimes(pathname, times); 2161 #else 2162 r = utimes(pathname, times); 2163 #endif 2164 if (r < 0) { 2165 failure_start(file, line, "Can't utimes %s\n", pathname); 2166 failure_finish(NULL); 2167 return (0); 2168 } 2169 return (1); 2170 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */ 2171 } 2172 2173 /* Compare file flags */ 2174 int 2175 assertion_compare_fflags(const char *file, int line, const char *patha, 2176 const char *pathb, int nomatch) 2177 { 2178 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2179 struct stat sa, sb; 2180 2181 assertion_count(file, line); 2182 2183 if (stat(patha, &sa) < 0) 2184 return (0); 2185 if (stat(pathb, &sb) < 0) 2186 return (0); 2187 if (!nomatch && sa.st_flags != sb.st_flags) { 2188 failure_start(file, line, "File flags should be identical: " 2189 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb, 2190 sb.st_flags); 2191 failure_finish(NULL); 2192 return (0); 2193 } 2194 if (nomatch && sa.st_flags == sb.st_flags) { 2195 failure_start(file, line, "File flags should be different: " 2196 "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb, 2197 sb.st_flags); 2198 failure_finish(NULL); 2199 return (0); 2200 } 2201 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \ 2202 defined(FS_NODUMP_FL)) || \ 2203 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \ 2204 && defined(EXT2_NODUMP_FL)) 2205 int fd, r, flagsa, flagsb; 2206 2207 assertion_count(file, line); 2208 fd = open(patha, O_RDONLY | O_NONBLOCK); 2209 if (fd < 0) { 2210 failure_start(file, line, "Can't open %s\n", patha); 2211 failure_finish(NULL); 2212 return (0); 2213 } 2214 r = ioctl(fd, 2215 #ifdef FS_IOC_GETFLAGS 2216 FS_IOC_GETFLAGS, 2217 #else 2218 EXT2_IOC_GETFLAGS, 2219 #endif 2220 &flagsa); 2221 close(fd); 2222 if (r < 0) { 2223 failure_start(file, line, "Can't get flags %s\n", patha); 2224 failure_finish(NULL); 2225 return (0); 2226 } 2227 fd = open(pathb, O_RDONLY | O_NONBLOCK); 2228 if (fd < 0) { 2229 failure_start(file, line, "Can't open %s\n", pathb); 2230 failure_finish(NULL); 2231 return (0); 2232 } 2233 r = ioctl(fd, 2234 #ifdef FS_IOC_GETFLAGS 2235 FS_IOC_GETFLAGS, 2236 #else 2237 EXT2_IOC_GETFLAGS, 2238 #endif 2239 &flagsb); 2240 close(fd); 2241 if (r < 0) { 2242 failure_start(file, line, "Can't get flags %s\n", pathb); 2243 failure_finish(NULL); 2244 return (0); 2245 } 2246 if (!nomatch && flagsa != flagsb) { 2247 failure_start(file, line, "File flags should be identical: " 2248 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb); 2249 failure_finish(NULL); 2250 return (0); 2251 } 2252 if (nomatch && flagsa == flagsb) { 2253 failure_start(file, line, "File flags should be different: " 2254 "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb); 2255 failure_finish(NULL); 2256 return (0); 2257 } 2258 #else 2259 (void)patha; /* UNUSED */ 2260 (void)pathb; /* UNUSED */ 2261 (void)nomatch; /* UNUSED */ 2262 assertion_count(file, line); 2263 #endif 2264 return (1); 2265 } 2266 2267 /* Set nodump, report failures. */ 2268 int 2269 assertion_set_nodump(const char *file, int line, const char *pathname) 2270 { 2271 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2272 int r; 2273 2274 assertion_count(file, line); 2275 r = chflags(pathname, UF_NODUMP); 2276 if (r < 0) { 2277 failure_start(file, line, "Can't set nodump %s\n", pathname); 2278 failure_finish(NULL); 2279 return (0); 2280 } 2281 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \ 2282 defined(FS_NODUMP_FL)) || \ 2283 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \ 2284 && defined(EXT2_NODUMP_FL)) 2285 int fd, r, flags; 2286 2287 assertion_count(file, line); 2288 fd = open(pathname, O_RDONLY | O_NONBLOCK); 2289 if (fd < 0) { 2290 failure_start(file, line, "Can't open %s\n", pathname); 2291 failure_finish(NULL); 2292 return (0); 2293 } 2294 r = ioctl(fd, 2295 #ifdef FS_IOC_GETFLAGS 2296 FS_IOC_GETFLAGS, 2297 #else 2298 EXT2_IOC_GETFLAGS, 2299 #endif 2300 &flags); 2301 if (r < 0) { 2302 failure_start(file, line, "Can't get flags %s\n", pathname); 2303 failure_finish(NULL); 2304 return (0); 2305 } 2306 #ifdef FS_NODUMP_FL 2307 flags |= FS_NODUMP_FL; 2308 #else 2309 flags |= EXT2_NODUMP_FL; 2310 #endif 2311 2312 r = ioctl(fd, 2313 #ifdef FS_IOC_SETFLAGS 2314 FS_IOC_SETFLAGS, 2315 #else 2316 EXT2_IOC_SETFLAGS, 2317 #endif 2318 &flags); 2319 if (r < 0) { 2320 failure_start(file, line, "Can't set nodump %s\n", pathname); 2321 failure_finish(NULL); 2322 return (0); 2323 } 2324 close(fd); 2325 #else 2326 (void)pathname; /* UNUSED */ 2327 assertion_count(file, line); 2328 #endif 2329 return (1); 2330 } 2331 2332 #ifdef PROGRAM 2333 static void assert_version_id(char **qq, size_t *ss) 2334 { 2335 char *q = *qq; 2336 size_t s = *ss; 2337 2338 /* Version number is a series of digits and periods. */ 2339 while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) { 2340 ++q; 2341 --s; 2342 } 2343 2344 if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') { 2345 q += 3; 2346 s -= 3; 2347 } 2348 2349 /* Skip a single trailing a,b,c, or d. */ 2350 if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd') 2351 ++q; 2352 2353 /* Version number terminated by space. */ 2354 failure("No space after version: ``%s''", q); 2355 assert(s > 1); 2356 failure("No space after version: ``%s''", q); 2357 assert(*q == ' '); 2358 2359 ++q; --s; 2360 2361 *qq = q; 2362 *ss = s; 2363 } 2364 2365 2366 /* 2367 * Check program version 2368 */ 2369 void assertVersion(const char *prog, const char *base) 2370 { 2371 int r; 2372 char *p, *q; 2373 size_t s; 2374 size_t prog_len = strlen(base); 2375 2376 r = systemf("%s --version >version.stdout 2>version.stderr", prog); 2377 if (r != 0) 2378 r = systemf("%s -W version >version.stdout 2>version.stderr", 2379 prog); 2380 2381 failure("Unable to run either %s --version or %s -W version", 2382 prog, prog); 2383 if (!assert(r == 0)) 2384 return; 2385 2386 /* --version should generate nothing to stdout. */ 2387 assertEmptyFile("version.stderr"); 2388 2389 /* Verify format of version message. */ 2390 q = p = slurpfile(&s, "version.stdout"); 2391 2392 /* Version message should start with name of program, then space. */ 2393 assert(s > prog_len + 1); 2394 2395 failure("Version must start with '%s': ``%s''", base, p); 2396 if (!assertEqualMem(q, base, prog_len)) { 2397 free(p); 2398 return; 2399 } 2400 2401 q += prog_len; s -= prog_len; 2402 2403 assert(*q == ' '); 2404 q++; s--; 2405 2406 assert_version_id(&q, &s); 2407 2408 /* Separator. */ 2409 failure("No `-' between program name and versions: ``%s''", p); 2410 assertEqualMem(q, "- ", 2); 2411 q += 2; s -= 2; 2412 2413 failure("Not long enough for libarchive version: ``%s''", p); 2414 assert(s > 11); 2415 2416 failure("Libarchive version must start with `libarchive': ``%s''", p); 2417 assertEqualMem(q, "libarchive ", 11); 2418 2419 q += 11; s -= 11; 2420 2421 assert_version_id(&q, &s); 2422 2423 /* Skip arbitrary third-party version numbers. */ 2424 while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' || 2425 isalnum((unsigned char)*q))) { 2426 ++q; 2427 --s; 2428 } 2429 2430 /* All terminated by end-of-line. */ 2431 assert(s >= 1); 2432 2433 /* Skip an optional CR character (e.g., Windows) */ 2434 failure("Version output must end with \\n or \\r\\n"); 2435 2436 if (*q == '\r') { ++q; --s; } 2437 assertEqualMem(q, "\n", 1); 2438 2439 free(p); 2440 } 2441 #endif /* PROGRAM */ 2442 2443 /* 2444 * 2445 * UTILITIES for use by tests. 2446 * 2447 */ 2448 2449 /* 2450 * Check whether platform supports symlinks. This is intended 2451 * for tests to use in deciding whether to bother testing symlink 2452 * support; if the platform doesn't support symlinks, there's no point 2453 * in checking whether the program being tested can create them. 2454 * 2455 * Note that the first time this test is called, we actually go out to 2456 * disk to create and verify a symlink. This is necessary because 2457 * symlink support is actually a property of a particular filesystem 2458 * and can thus vary between directories on a single system. After 2459 * the first call, this returns the cached result from memory, so it's 2460 * safe to call it as often as you wish. 2461 */ 2462 int 2463 canSymlink(void) 2464 { 2465 /* Remember the test result */ 2466 static int value = 0, tested = 0; 2467 if (tested) 2468 return (value); 2469 2470 ++tested; 2471 assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a"); 2472 /* Note: Cygwin has its own symlink() emulation that does not 2473 * use the Win32 CreateSymbolicLink() function. */ 2474 #if defined(_WIN32) && !defined(__CYGWIN__) 2475 value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0) 2476 && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0", 2477 0); 2478 #elif HAVE_SYMLINK 2479 value = (0 == symlink("canSymlink.0", "canSymlink.1")) 2480 && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0", 2481 0); 2482 #endif 2483 return (value); 2484 } 2485 2486 /* Platform-dependent options for hiding the output of a subcommand. */ 2487 #if defined(_WIN32) && !defined(__CYGWIN__) 2488 static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */ 2489 #else 2490 static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */ 2491 #endif 2492 /* 2493 * Can this platform run the bzip2 program? 2494 */ 2495 int 2496 canBzip2(void) 2497 { 2498 static int tested = 0, value = 0; 2499 if (!tested) { 2500 tested = 1; 2501 if (systemf("bzip2 --help %s", redirectArgs) == 0) 2502 value = 1; 2503 } 2504 return (value); 2505 } 2506 2507 /* 2508 * Can this platform run the grzip program? 2509 */ 2510 int 2511 canGrzip(void) 2512 { 2513 static int tested = 0, value = 0; 2514 if (!tested) { 2515 tested = 1; 2516 if (systemf("grzip -V %s", redirectArgs) == 0) 2517 value = 1; 2518 } 2519 return (value); 2520 } 2521 2522 /* 2523 * Can this platform run the gzip program? 2524 */ 2525 int 2526 canGzip(void) 2527 { 2528 static int tested = 0, value = 0; 2529 if (!tested) { 2530 tested = 1; 2531 if (systemf("gzip --help %s", redirectArgs) == 0) 2532 value = 1; 2533 } 2534 return (value); 2535 } 2536 2537 /* 2538 * Can this platform run the lrzip program? 2539 */ 2540 int 2541 canRunCommand(const char *cmd) 2542 { 2543 static int tested = 0, value = 0; 2544 if (!tested) { 2545 tested = 1; 2546 if (systemf("%s %s", cmd, redirectArgs) == 0) 2547 value = 1; 2548 } 2549 return (value); 2550 } 2551 2552 int 2553 canLrzip(void) 2554 { 2555 static int tested = 0, value = 0; 2556 if (!tested) { 2557 tested = 1; 2558 if (systemf("lrzip -V %s", redirectArgs) == 0) 2559 value = 1; 2560 } 2561 return (value); 2562 } 2563 2564 /* 2565 * Can this platform run the lz4 program? 2566 */ 2567 int 2568 canLz4(void) 2569 { 2570 static int tested = 0, value = 0; 2571 if (!tested) { 2572 tested = 1; 2573 if (systemf("lz4 --help %s", redirectArgs) == 0) 2574 value = 1; 2575 } 2576 return (value); 2577 } 2578 2579 /* 2580 * Can this platform run the zstd program? 2581 */ 2582 int 2583 canZstd(void) 2584 { 2585 static int tested = 0, value = 0; 2586 if (!tested) { 2587 tested = 1; 2588 if (systemf("zstd --help %s", redirectArgs) == 0) 2589 value = 1; 2590 } 2591 return (value); 2592 } 2593 2594 /* 2595 * Can this platform run the lzip program? 2596 */ 2597 int 2598 canLzip(void) 2599 { 2600 static int tested = 0, value = 0; 2601 if (!tested) { 2602 tested = 1; 2603 if (systemf("lzip --help %s", redirectArgs) == 0) 2604 value = 1; 2605 } 2606 return (value); 2607 } 2608 2609 /* 2610 * Can this platform run the lzma program? 2611 */ 2612 int 2613 canLzma(void) 2614 { 2615 static int tested = 0, value = 0; 2616 if (!tested) { 2617 tested = 1; 2618 if (systemf("lzma --help %s", redirectArgs) == 0) 2619 value = 1; 2620 } 2621 return (value); 2622 } 2623 2624 /* 2625 * Can this platform run the lzop program? 2626 */ 2627 int 2628 canLzop(void) 2629 { 2630 static int tested = 0, value = 0; 2631 if (!tested) { 2632 tested = 1; 2633 if (systemf("lzop --help %s", redirectArgs) == 0) 2634 value = 1; 2635 } 2636 return (value); 2637 } 2638 2639 /* 2640 * Can this platform run the xz program? 2641 */ 2642 int 2643 canXz(void) 2644 { 2645 static int tested = 0, value = 0; 2646 if (!tested) { 2647 tested = 1; 2648 if (systemf("xz --help %s", redirectArgs) == 0) 2649 value = 1; 2650 } 2651 return (value); 2652 } 2653 2654 /* 2655 * Can this filesystem handle nodump flags. 2656 */ 2657 int 2658 canNodump(void) 2659 { 2660 #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP) 2661 const char *path = "cannodumptest"; 2662 struct stat sb; 2663 2664 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2665 if (chflags(path, UF_NODUMP) < 0) 2666 return (0); 2667 if (stat(path, &sb) < 0) 2668 return (0); 2669 if (sb.st_flags & UF_NODUMP) 2670 return (1); 2671 #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \ 2672 && defined(FS_NODUMP_FL)) || \ 2673 (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \ 2674 && defined(EXT2_NODUMP_FL)) 2675 const char *path = "cannodumptest"; 2676 int fd, r, flags; 2677 2678 assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL); 2679 fd = open(path, O_RDONLY | O_NONBLOCK); 2680 if (fd < 0) 2681 return (0); 2682 r = ioctl(fd, 2683 #ifdef FS_IOC_GETFLAGS 2684 FS_IOC_GETFLAGS, 2685 #else 2686 EXT2_IOC_GETFLAGS, 2687 #endif 2688 &flags); 2689 if (r < 0) 2690 return (0); 2691 #ifdef FS_NODUMP_FL 2692 flags |= FS_NODUMP_FL; 2693 #else 2694 flags |= EXT2_NODUMP_FL; 2695 #endif 2696 r = ioctl(fd, 2697 #ifdef FS_IOC_SETFLAGS 2698 FS_IOC_SETFLAGS, 2699 #else 2700 EXT2_IOC_SETFLAGS, 2701 #endif 2702 &flags); 2703 if (r < 0) 2704 return (0); 2705 close(fd); 2706 fd = open(path, O_RDONLY | O_NONBLOCK); 2707 if (fd < 0) 2708 return (0); 2709 r = ioctl(fd, 2710 #ifdef FS_IOC_GETFLAGS 2711 FS_IOC_GETFLAGS, 2712 #else 2713 EXT2_IOC_GETFLAGS, 2714 #endif 2715 &flags); 2716 if (r < 0) 2717 return (0); 2718 close(fd); 2719 #ifdef FS_NODUMP_FL 2720 if (flags & FS_NODUMP_FL) 2721 #else 2722 if (flags & EXT2_NODUMP_FL) 2723 #endif 2724 return (1); 2725 #endif 2726 return (0); 2727 } 2728 2729 /* Get extended attribute value from a path */ 2730 void * 2731 getXattr(const char *path, const char *name, size_t *sizep) 2732 { 2733 void *value = NULL; 2734 #if ARCHIVE_XATTR_SUPPORT 2735 ssize_t size; 2736 #if ARCHIVE_XATTR_LINUX 2737 size = lgetxattr(path, name, NULL, 0); 2738 #elif ARCHIVE_XATTR_DARWIN 2739 size = getxattr(path, name, NULL, 0, 0, XATTR_NOFOLLOW); 2740 #elif ARCHIVE_XATTR_AIX 2741 size = lgetea(path, name, NULL, 0); 2742 #elif ARCHIVE_XATTR_FREEBSD 2743 size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5, 2744 NULL, 0); 2745 #endif 2746 2747 if (size >= 0) { 2748 value = malloc(size); 2749 #if ARCHIVE_XATTR_LINUX 2750 size = lgetxattr(path, name, value, size); 2751 #elif ARCHIVE_XATTR_DARWIN 2752 size = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW); 2753 #elif ARCHIVE_XATTR_AIX 2754 size = lgetea(path, name, value, size); 2755 #elif ARCHIVE_XATTR_FREEBSD 2756 size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5, 2757 value, size); 2758 #endif 2759 if (size < 0) { 2760 free(value); 2761 value = NULL; 2762 } 2763 } 2764 if (size < 0) 2765 *sizep = 0; 2766 else 2767 *sizep = (size_t)size; 2768 #else /* !ARCHIVE_XATTR_SUPPORT */ 2769 (void)path; /* UNUSED */ 2770 (void)name; /* UNUSED */ 2771 *sizep = 0; 2772 #endif /* !ARCHIVE_XATTR_SUPPORT */ 2773 return (value); 2774 } 2775 2776 /* 2777 * Set extended attribute on a path 2778 * Returns 0 on error, 1 on success 2779 */ 2780 int 2781 setXattr(const char *path, const char *name, const void *value, size_t size) 2782 { 2783 #if ARCHIVE_XATTR_SUPPORT 2784 #if ARCHIVE_XATTR_LINUX 2785 if (lsetxattr(path, name, value, size, 0) == 0) 2786 #elif ARCHIVE_XATTR_DARWIN 2787 if (setxattr(path, name, value, size, 0, XATTR_NOFOLLOW) == 0) 2788 #elif ARCHIVE_XATTR_AIX 2789 if (lsetea(path, name, value, size, 0) == 0) 2790 #elif ARCHIVE_XATTR_FREEBSD 2791 if (extattr_set_link(path, EXTATTR_NAMESPACE_USER, name + 5, value, 2792 size) > -1) 2793 #else 2794 if (0) 2795 #endif 2796 return (1); 2797 #else /* !ARCHIVE_XATTR_SUPPORT */ 2798 (void)path; /* UNUSED */ 2799 (void)name; /* UNUSED */ 2800 (void)value; /* UNUSED */ 2801 (void)size; /* UNUSED */ 2802 #endif /* !ARCHIVE_XATTR_SUPPORT */ 2803 return (0); 2804 } 2805 2806 #if ARCHIVE_ACL_SUNOS 2807 /* Fetch ACLs on Solaris using acl() or facl() */ 2808 void * 2809 sunacl_get(int cmd, int *aclcnt, int fd, const char *path) 2810 { 2811 int cnt, cntcmd; 2812 size_t size; 2813 void *aclp; 2814 2815 if (cmd == GETACL) { 2816 cntcmd = GETACLCNT; 2817 size = sizeof(aclent_t); 2818 } 2819 #if ARCHIVE_ACL_SUNOS_NFS4 2820 else if (cmd == ACE_GETACL) { 2821 cntcmd = ACE_GETACLCNT; 2822 size = sizeof(ace_t); 2823 } 2824 #endif 2825 else { 2826 errno = EINVAL; 2827 *aclcnt = -1; 2828 return (NULL); 2829 } 2830 2831 aclp = NULL; 2832 cnt = -2; 2833 while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) { 2834 if (path != NULL) 2835 cnt = acl(path, cntcmd, 0, NULL); 2836 else 2837 cnt = facl(fd, cntcmd, 0, NULL); 2838 2839 if (cnt > 0) { 2840 if (aclp == NULL) 2841 aclp = malloc(cnt * size); 2842 else 2843 aclp = realloc(NULL, cnt * size); 2844 if (aclp != NULL) { 2845 if (path != NULL) 2846 cnt = acl(path, cmd, cnt, aclp); 2847 else 2848 cnt = facl(fd, cmd, cnt, aclp); 2849 } 2850 } else { 2851 free(aclp); 2852 aclp = NULL; 2853 break; 2854 } 2855 } 2856 2857 *aclcnt = cnt; 2858 return (aclp); 2859 } 2860 #endif /* ARCHIVE_ACL_SUNOS */ 2861 2862 /* 2863 * Set test ACLs on a path 2864 * Return values: 2865 * 0: error setting ACLs 2866 * ARCHIVE_TEST_ACL_TYPE_POSIX1E: POSIX.1E ACLs have been set 2867 * ARCHIVE_TEST_ACL_TYPE_NFS4: NFSv4 or extended ACLs have been set 2868 */ 2869 int 2870 setTestAcl(const char *path) 2871 { 2872 #if ARCHIVE_ACL_SUPPORT 2873 int r = 1; 2874 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_DARWIN 2875 acl_t acl; 2876 #endif 2877 #if ARCHIVE_ACL_LIBRICHACL 2878 struct richacl *richacl; 2879 #endif 2880 #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD 2881 const char *acltext_posix1e = "user:1:rw-," 2882 "group:15:r-x," 2883 "user::rwx," 2884 "group::rwx," 2885 "other::r-x," 2886 "mask::rwx"; 2887 #elif ARCHIVE_ACL_SUNOS /* Solaris POSIX.1e */ 2888 aclent_t aclp_posix1e[] = { 2889 { USER_OBJ, -1, 4 | 2 | 1 }, 2890 { USER, 1, 4 | 2 }, 2891 { GROUP_OBJ, -1, 4 | 2 | 1 }, 2892 { GROUP, 15, 4 | 1 }, 2893 { CLASS_OBJ, -1, 4 | 2 | 1 }, 2894 { OTHER_OBJ, -1, 4 | 2 | 1 } 2895 }; 2896 #endif 2897 #if ARCHIVE_ACL_FREEBSD /* FreeBSD NFS4 */ 2898 const char *acltext_nfs4 = "user:1:rwpaRcs::allow:1," 2899 "group:15:rxaRcs::allow:15," 2900 "owner@:rwpxaARWcCos::allow," 2901 "group@:rwpxaRcs::allow," 2902 "everyone@:rxaRcs::allow"; 2903 #elif ARCHIVE_ACL_LIBRICHACL 2904 const char *acltext_nfs4 = "owner:rwpxaARWcCoS::mask," 2905 "group:rwpxaRcS::mask," 2906 "other:rxaRcS::mask," 2907 "user:1:rwpaRcS::allow," 2908 "group:15:rxaRcS::allow," 2909 "owner@:rwpxaARWcCoS::allow," 2910 "group@:rwpxaRcS::allow," 2911 "everyone@:rxaRcS::allow"; 2912 #elif ARCHIVE_ACL_SUNOS_NFS4 /* Solaris NFS4 */ 2913 ace_t aclp_nfs4[] = { 2914 { 1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA | 2915 ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | ACE_READ_ACL | 2916 ACE_SYNCHRONIZE, 0, ACE_ACCESS_ALLOWED_ACE_TYPE }, 2917 { 15, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES | 2918 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE, 2919 ACE_IDENTIFIER_GROUP, ACE_ACCESS_ALLOWED_ACE_TYPE }, 2920 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA | 2921 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_WRITE_ATTRIBUTES | 2922 ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS | 2923 ACE_READ_ACL | ACE_WRITE_ACL | ACE_WRITE_OWNER | ACE_SYNCHRONIZE, 2924 ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE }, 2925 { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA | 2926 ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | 2927 ACE_READ_ACL | ACE_SYNCHRONIZE, ACE_GROUP | ACE_IDENTIFIER_GROUP, 2928 ACE_ACCESS_ALLOWED_ACE_TYPE }, 2929 { -1, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES | 2930 ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE, 2931 ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE } 2932 }; 2933 #elif ARCHIVE_ACL_DARWIN /* Mac OS X */ 2934 acl_entry_t aclent; 2935 acl_permset_t permset; 2936 const uid_t uid = 1; 2937 uuid_t uuid; 2938 int i; 2939 const acl_perm_t acl_perms[] = { 2940 ACL_READ_DATA, 2941 ACL_WRITE_DATA, 2942 ACL_APPEND_DATA, 2943 ACL_EXECUTE, 2944 ACL_READ_ATTRIBUTES, 2945 ACL_READ_EXTATTRIBUTES, 2946 ACL_READ_SECURITY, 2947 #if HAVE_DECL_ACL_SYNCHRONIZE 2948 ACL_SYNCHRONIZE 2949 #endif 2950 }; 2951 #endif /* ARCHIVE_ACL_DARWIN */ 2952 2953 #if ARCHIVE_ACL_FREEBSD 2954 acl = acl_from_text(acltext_nfs4); 2955 failure("acl_from_text() error: %s", strerror(errno)); 2956 if (assert(acl != NULL) == 0) 2957 return (0); 2958 #elif ARCHIVE_ACL_LIBRICHACL 2959 richacl = richacl_from_text(acltext_nfs4, NULL, NULL); 2960 failure("richacl_from_text() error: %s", strerror(errno)); 2961 if (assert(richacl != NULL) == 0) 2962 return (0); 2963 #elif ARCHIVE_ACL_DARWIN 2964 acl = acl_init(1); 2965 failure("acl_init() error: %s", strerror(errno)); 2966 if (assert(acl != NULL) == 0) 2967 return (0); 2968 r = acl_create_entry(&acl, &aclent); 2969 failure("acl_create_entry() error: %s", strerror(errno)); 2970 if (assertEqualInt(r, 0) == 0) 2971 goto testacl_free; 2972 r = acl_set_tag_type(aclent, ACL_EXTENDED_ALLOW); 2973 failure("acl_set_tag_type() error: %s", strerror(errno)); 2974 if (assertEqualInt(r, 0) == 0) 2975 goto testacl_free; 2976 r = acl_get_permset(aclent, &permset); 2977 failure("acl_get_permset() error: %s", strerror(errno)); 2978 if (assertEqualInt(r, 0) == 0) 2979 goto testacl_free; 2980 for (i = 0; i < (int)(sizeof(acl_perms) / sizeof(acl_perms[0])); i++) { 2981 r = acl_add_perm(permset, acl_perms[i]); 2982 failure("acl_add_perm() error: %s", strerror(errno)); 2983 if (assertEqualInt(r, 0) == 0) 2984 goto testacl_free; 2985 } 2986 r = acl_set_permset(aclent, permset); 2987 failure("acl_set_permset() error: %s", strerror(errno)); 2988 if (assertEqualInt(r, 0) == 0) 2989 goto testacl_free; 2990 r = mbr_uid_to_uuid(uid, uuid); 2991 failure("mbr_uid_to_uuid() error: %s", strerror(errno)); 2992 if (assertEqualInt(r, 0) == 0) 2993 goto testacl_free; 2994 r = acl_set_qualifier(aclent, uuid); 2995 failure("acl_set_qualifier() error: %s", strerror(errno)); 2996 if (assertEqualInt(r, 0) == 0) 2997 goto testacl_free; 2998 #endif /* ARCHIVE_ACL_DARWIN */ 2999 3000 #if ARCHIVE_ACL_NFS4 3001 #if ARCHIVE_ACL_FREEBSD 3002 r = acl_set_file(path, ACL_TYPE_NFS4, acl); 3003 acl_free(acl); 3004 #elif ARCHIVE_ACL_LIBRICHACL 3005 r = richacl_set_file(path, richacl); 3006 richacl_free(richacl); 3007 #elif ARCHIVE_ACL_SUNOS_NFS4 3008 r = acl(path, ACE_SETACL, 3009 (int)(sizeof(aclp_nfs4)/sizeof(aclp_nfs4[0])), aclp_nfs4); 3010 #elif ARCHIVE_ACL_DARWIN 3011 r = acl_set_file(path, ACL_TYPE_EXTENDED, acl); 3012 acl_free(acl); 3013 #endif 3014 if (r == 0) 3015 return (ARCHIVE_TEST_ACL_TYPE_NFS4); 3016 #endif /* ARCHIVE_ACL_NFS4 */ 3017 3018 #if ARCHIVE_ACL_POSIX1E 3019 #if ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_LIBACL 3020 acl = acl_from_text(acltext_posix1e); 3021 failure("acl_from_text() error: %s", strerror(errno)); 3022 if (assert(acl != NULL) == 0) 3023 return (0); 3024 3025 r = acl_set_file(path, ACL_TYPE_ACCESS, acl); 3026 acl_free(acl); 3027 #elif ARCHIVE_ACL_SUNOS 3028 r = acl(path, SETACL, 3029 (int)(sizeof(aclp_posix1e)/sizeof(aclp_posix1e[0])), aclp_posix1e); 3030 #endif 3031 if (r == 0) 3032 return (ARCHIVE_TEST_ACL_TYPE_POSIX1E); 3033 else 3034 return (0); 3035 #endif /* ARCHIVE_ACL_POSIX1E */ 3036 #if ARCHIVE_ACL_DARWIN 3037 testacl_free: 3038 acl_free(acl); 3039 #endif 3040 #endif /* ARCHIVE_ACL_SUPPORT */ 3041 (void)path; /* UNUSED */ 3042 return (0); 3043 } 3044 3045 /* 3046 * Sleep as needed; useful for verifying disk timestamp changes by 3047 * ensuring that the wall-clock time has actually changed before we 3048 * go back to re-read something from disk. 3049 */ 3050 void 3051 sleepUntilAfter(time_t t) 3052 { 3053 while (t >= time(NULL)) 3054 #if defined(_WIN32) && !defined(__CYGWIN__) 3055 Sleep(500); 3056 #else 3057 sleep(1); 3058 #endif 3059 } 3060 3061 /* 3062 * Call standard system() call, but build up the command line using 3063 * sprintf() conventions. 3064 */ 3065 int 3066 systemf(const char *fmt, ...) 3067 { 3068 char buff[8192]; 3069 va_list ap; 3070 int r; 3071 3072 va_start(ap, fmt); 3073 vsnprintf(buff, sizeof(buff), fmt, ap); 3074 if (verbosity > VERBOSITY_FULL) 3075 logprintf("Cmd: %s\n", buff); 3076 r = system(buff); 3077 va_end(ap); 3078 return (r); 3079 } 3080 3081 /* 3082 * Slurp a file into memory for ease of comparison and testing. 3083 * Returns size of file in 'sizep' if non-NULL, null-terminates 3084 * data in memory for ease of use. 3085 */ 3086 char * 3087 slurpfile(size_t * sizep, const char *fmt, ...) 3088 { 3089 char filename[8192]; 3090 struct stat st; 3091 va_list ap; 3092 char *p; 3093 ssize_t bytes_read; 3094 FILE *f; 3095 int r; 3096 3097 va_start(ap, fmt); 3098 vsnprintf(filename, sizeof(filename), fmt, ap); 3099 va_end(ap); 3100 3101 f = fopen(filename, "rb"); 3102 if (f == NULL) { 3103 /* Note: No error; non-existent file is okay here. */ 3104 return (NULL); 3105 } 3106 r = fstat(fileno(f), &st); 3107 if (r != 0) { 3108 logprintf("Can't stat file %s\n", filename); 3109 fclose(f); 3110 return (NULL); 3111 } 3112 p = malloc((size_t)st.st_size + 1); 3113 if (p == NULL) { 3114 logprintf("Can't allocate %ld bytes of memory to read file %s\n", 3115 (long int)st.st_size, filename); 3116 fclose(f); 3117 return (NULL); 3118 } 3119 bytes_read = fread(p, 1, (size_t)st.st_size, f); 3120 if (bytes_read < st.st_size) { 3121 logprintf("Can't read file %s\n", filename); 3122 fclose(f); 3123 free(p); 3124 return (NULL); 3125 } 3126 p[st.st_size] = '\0'; 3127 if (sizep != NULL) 3128 *sizep = (size_t)st.st_size; 3129 fclose(f); 3130 return (p); 3131 } 3132 3133 /* 3134 * Slurp a file into memory for ease of comparison and testing. 3135 * Returns size of file in 'sizep' if non-NULL, null-terminates 3136 * data in memory for ease of use. 3137 */ 3138 void 3139 dumpfile(const char *filename, void *data, size_t len) 3140 { 3141 ssize_t bytes_written; 3142 FILE *f; 3143 3144 f = fopen(filename, "wb"); 3145 if (f == NULL) { 3146 logprintf("Can't open file %s for writing\n", filename); 3147 return; 3148 } 3149 bytes_written = fwrite(data, 1, len, f); 3150 if (bytes_written < (ssize_t)len) 3151 logprintf("Can't write file %s\n", filename); 3152 fclose(f); 3153 } 3154 3155 /* Read a uuencoded file from the reference directory, decode, and 3156 * write the result into the current directory. */ 3157 #define VALID_UUDECODE(c) (c >= 32 && c <= 96) 3158 #define UUDECODE(c) (((c) - 0x20) & 0x3f) 3159 void 3160 extract_reference_file(const char *name) 3161 { 3162 char buff[1024]; 3163 FILE *in, *out; 3164 3165 snprintf(buff, sizeof(buff), "%s/%s.uu", refdir, name); 3166 in = fopen(buff, "r"); 3167 failure("Couldn't open reference file %s", buff); 3168 assert(in != NULL); 3169 if (in == NULL) 3170 return; 3171 /* Read up to and including the 'begin' line. */ 3172 for (;;) { 3173 if (fgets(buff, sizeof(buff), in) == NULL) { 3174 /* TODO: This is a failure. */ 3175 return; 3176 } 3177 if (memcmp(buff, "begin ", 6) == 0) 3178 break; 3179 } 3180 /* Now, decode the rest and write it. */ 3181 out = fopen(name, "wb"); 3182 while (fgets(buff, sizeof(buff), in) != NULL) { 3183 char *p = buff; 3184 int bytes; 3185 3186 if (memcmp(buff, "end", 3) == 0) 3187 break; 3188 3189 bytes = UUDECODE(*p++); 3190 while (bytes > 0) { 3191 int n = 0; 3192 /* Write out 1-3 bytes from that. */ 3193 assert(VALID_UUDECODE(p[0])); 3194 assert(VALID_UUDECODE(p[1])); 3195 n = UUDECODE(*p++) << 18; 3196 n |= UUDECODE(*p++) << 12; 3197 fputc(n >> 16, out); 3198 --bytes; 3199 if (bytes > 0) { 3200 assert(VALID_UUDECODE(p[0])); 3201 n |= UUDECODE(*p++) << 6; 3202 fputc((n >> 8) & 0xFF, out); 3203 --bytes; 3204 } 3205 if (bytes > 0) { 3206 assert(VALID_UUDECODE(p[0])); 3207 n |= UUDECODE(*p++); 3208 fputc(n & 0xFF, out); 3209 --bytes; 3210 } 3211 } 3212 } 3213 fclose(out); 3214 fclose(in); 3215 } 3216 3217 void 3218 copy_reference_file(const char *name) 3219 { 3220 char buff[1024]; 3221 FILE *in, *out; 3222 size_t rbytes; 3223 3224 snprintf(buff, sizeof(buff), "%s/%s", refdir, name); 3225 in = fopen(buff, "rb"); 3226 failure("Couldn't open reference file %s", buff); 3227 assert(in != NULL); 3228 if (in == NULL) 3229 return; 3230 /* Now, decode the rest and write it. */ 3231 /* Not a lot of error checking here; the input better be right. */ 3232 out = fopen(name, "wb"); 3233 while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) { 3234 if (fwrite(buff, 1, rbytes, out) != rbytes) { 3235 logprintf("Error: fwrite\n"); 3236 break; 3237 } 3238 } 3239 fclose(out); 3240 fclose(in); 3241 } 3242 3243 int 3244 is_LargeInode(const char *file) 3245 { 3246 #if defined(_WIN32) && !defined(__CYGWIN__) 3247 BY_HANDLE_FILE_INFORMATION bhfi; 3248 int r; 3249 3250 r = my_GetFileInformationByName(file, &bhfi); 3251 if (r != 0) 3252 return (0); 3253 return (bhfi.nFileIndexHigh & 0x0000FFFFUL); 3254 #else 3255 struct stat st; 3256 int64_t ino; 3257 3258 if (stat(file, &st) < 0) 3259 return (0); 3260 ino = (int64_t)st.st_ino; 3261 return (ino > 0xffffffff); 3262 #endif 3263 } 3264 3265 void 3266 extract_reference_files(const char **names) 3267 { 3268 while (names && *names) 3269 extract_reference_file(*names++); 3270 } 3271 3272 #ifndef PROGRAM 3273 /* Set ACLs */ 3274 int 3275 assertion_entry_set_acls(const char *file, int line, struct archive_entry *ae, 3276 struct archive_test_acl_t *acls, int n) 3277 { 3278 int i, r, ret; 3279 3280 assertion_count(file, line); 3281 3282 ret = 0; 3283 archive_entry_acl_clear(ae); 3284 for (i = 0; i < n; i++) { 3285 r = archive_entry_acl_add_entry(ae, 3286 acls[i].type, acls[i].permset, acls[i].tag, 3287 acls[i].qual, acls[i].name); 3288 if (r != 0) { 3289 ret = 1; 3290 failure_start(file, line, "type=%#010x, " 3291 "permset=%#010x, tag=%d, qual=%d name=%s", 3292 acls[i].type, acls[i].permset, acls[i].tag, 3293 acls[i].qual, acls[i].name); 3294 failure_finish(NULL); 3295 } 3296 } 3297 3298 return (ret); 3299 } 3300 3301 static int 3302 archive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset, 3303 int tag, int qual, const char *name) 3304 { 3305 if (type != acl->type) 3306 return (0); 3307 if (permset != acl->permset) 3308 return (0); 3309 if (tag != acl->tag) 3310 return (0); 3311 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ) 3312 return (1); 3313 if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) 3314 return (1); 3315 if (tag == ARCHIVE_ENTRY_ACL_EVERYONE) 3316 return (1); 3317 if (tag == ARCHIVE_ENTRY_ACL_OTHER) 3318 return (1); 3319 if (qual != acl->qual) 3320 return (0); 3321 if (name == NULL) { 3322 if (acl->name == NULL || acl->name[0] == '\0') 3323 return (1); 3324 return (0); 3325 } 3326 if (acl->name == NULL) { 3327 if (name[0] == '\0') 3328 return (1); 3329 return (0); 3330 } 3331 return (0 == strcmp(name, acl->name)); 3332 } 3333 3334 /* Compare ACLs */ 3335 int 3336 assertion_entry_compare_acls(const char *file, int line, 3337 struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt, 3338 int want_type, int mode) 3339 { 3340 int *marker; 3341 int i, r, n, ret; 3342 int type, permset, tag, qual; 3343 int matched; 3344 const char *name; 3345 3346 assertion_count(file, line); 3347 3348 ret = 0; 3349 n = 0; 3350 marker = malloc(sizeof(marker[0]) * cnt); 3351 3352 for (i = 0; i < cnt; i++) { 3353 if ((acls[i].type & want_type) != 0) { 3354 marker[n] = i; 3355 n++; 3356 } 3357 } 3358 3359 if (n == 0) { 3360 failure_start(file, line, "No ACL's to compare, type mask: %d", 3361 want_type); 3362 return (1); 3363 } 3364 3365 while (0 == (r = archive_entry_acl_next(ae, want_type, 3366 &type, &permset, &tag, &qual, &name))) { 3367 for (i = 0, matched = 0; i < n && !matched; i++) { 3368 if (archive_test_acl_match(&acls[marker[i]], type, 3369 permset, tag, qual, name)) { 3370 /* We found a match; remove it. */ 3371 marker[i] = marker[n - 1]; 3372 n--; 3373 matched = 1; 3374 } 3375 } 3376 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS 3377 && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) { 3378 if (!matched) { 3379 failure_start(file, line, "No match for " 3380 "user_obj perm"); 3381 failure_finish(NULL); 3382 ret = 1; 3383 } 3384 if ((permset << 6) != (mode & 0700)) { 3385 failure_start(file, line, "USER_OBJ permset " 3386 "(%02o) != user mode (%02o)", permset, 3387 07 & (mode >> 6)); 3388 failure_finish(NULL); 3389 ret = 1; 3390 } 3391 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS 3392 && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) { 3393 if (!matched) { 3394 failure_start(file, line, "No match for " 3395 "group_obj perm"); 3396 failure_finish(NULL); 3397 ret = 1; 3398 } 3399 if ((permset << 3) != (mode & 0070)) { 3400 failure_start(file, line, "GROUP_OBJ permset " 3401 "(%02o) != group mode (%02o)", permset, 3402 07 & (mode >> 3)); 3403 failure_finish(NULL); 3404 ret = 1; 3405 } 3406 } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS 3407 && tag == ARCHIVE_ENTRY_ACL_OTHER) { 3408 if (!matched) { 3409 failure_start(file, line, "No match for " 3410 "other perm"); 3411 failure_finish(NULL); 3412 ret = 1; 3413 } 3414 if ((permset << 0) != (mode & 0007)) { 3415 failure_start(file, line, "OTHER permset " 3416 "(%02o) != other mode (%02o)", permset, 3417 mode & 07); 3418 failure_finish(NULL); 3419 ret = 1; 3420 } 3421 } else if (matched != 1) { 3422 failure_start(file, line, "Could not find match for " 3423 "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d," 3424 "name=``%s'')", type, permset, tag, qual, name); 3425 failure_finish(NULL); 3426 ret = 1; 3427 } 3428 } 3429 if (r != ARCHIVE_EOF) { 3430 failure_start(file, line, "Should not exit before EOF"); 3431 failure_finish(NULL); 3432 ret = 1; 3433 } 3434 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 && 3435 (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) { 3436 failure_start(file, line, "Mode (%02o) and entry mode (%02o) " 3437 "mismatch", mode, archive_entry_mode(ae)); 3438 failure_finish(NULL); 3439 ret = 1; 3440 } 3441 if (n != 0) { 3442 failure_start(file, line, "Could not find match for ACL " 3443 "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')", 3444 acls[marker[0]].type, acls[marker[0]].permset, 3445 acls[marker[0]].tag, acls[marker[0]].qual, 3446 acls[marker[0]].name); 3447 failure_finish(NULL); 3448 ret = 1; 3449 /* Number of ACLs not matched should == 0 */ 3450 } 3451 free(marker); 3452 return (ret); 3453 } 3454 #endif /* !defined(PROGRAM) */ 3455 3456 /* 3457 * 3458 * TEST management 3459 * 3460 */ 3461 3462 /* 3463 * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has 3464 * a line like 3465 * DEFINE_TEST(test_function) 3466 * for each test. 3467 */ 3468 struct test_list_t 3469 { 3470 void (*func)(void); 3471 const char *name; 3472 int failures; 3473 }; 3474 3475 /* Use "list.h" to declare all of the test functions. */ 3476 #undef DEFINE_TEST 3477 #define DEFINE_TEST(name) void name(void); 3478 #include "list.h" 3479 3480 /* Use "list.h" to create a list of all tests (functions and names). */ 3481 #undef DEFINE_TEST 3482 #define DEFINE_TEST(n) { n, #n, 0 }, 3483 static struct test_list_t tests[] = { 3484 #include "list.h" 3485 }; 3486 3487 /* 3488 * Summarize repeated failures in the just-completed test. 3489 */ 3490 static void 3491 test_summarize(int failed, int skips_num) 3492 { 3493 unsigned int i; 3494 3495 switch (verbosity) { 3496 case VERBOSITY_SUMMARY_ONLY: 3497 printf(failed ? "E" : "."); 3498 fflush(stdout); 3499 break; 3500 case VERBOSITY_PASSFAIL: 3501 printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n"); 3502 break; 3503 } 3504 3505 log_console = (verbosity == VERBOSITY_LIGHT_REPORT); 3506 3507 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) { 3508 if (failed_lines[i].count > 1 && !failed_lines[i].skip) 3509 logprintf("%s:%d: Summary: Failed %d times\n", 3510 failed_filename, i, failed_lines[i].count); 3511 } 3512 /* Clear the failure history for the next file. */ 3513 failed_filename = NULL; 3514 memset(failed_lines, 0, sizeof(failed_lines)); 3515 } 3516 3517 /* 3518 * Actually run a single test, with appropriate setup and cleanup. 3519 */ 3520 static int 3521 test_run(int i, const char *tmpdir) 3522 { 3523 #ifdef PATH_MAX 3524 char workdir[PATH_MAX * 2]; 3525 #else 3526 char workdir[1024 * 2]; 3527 #endif 3528 char logfilename[64]; 3529 int failures_before = failures; 3530 int skips_before = skips; 3531 int oldumask; 3532 3533 switch (verbosity) { 3534 case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */ 3535 break; 3536 case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */ 3537 printf("%3d: %-64s", i, tests[i].name); 3538 fflush(stdout); 3539 break; 3540 default: /* Title of test, details will follow */ 3541 printf("%3d: %s\n", i, tests[i].name); 3542 } 3543 3544 /* Chdir to the top-level work directory. */ 3545 if (!assertChdir(tmpdir)) { 3546 fprintf(stderr, 3547 "ERROR: Can't chdir to top work dir %s\n", tmpdir); 3548 exit(1); 3549 } 3550 /* Create a log file for this test. */ 3551 snprintf(logfilename, sizeof(logfilename), "%s.log", tests[i].name); 3552 logfile = fopen(logfilename, "w"); 3553 fprintf(logfile, "%s\n\n", tests[i].name); 3554 /* Chdir() to a work dir for this specific test. */ 3555 snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name); 3556 testworkdir = workdir; 3557 if (!assertMakeDir(testworkdir, 0755) 3558 || !assertChdir(testworkdir)) { 3559 fprintf(stderr, 3560 "ERROR: Can't chdir to work dir %s\n", testworkdir); 3561 exit(1); 3562 } 3563 /* Explicitly reset the locale before each test. */ 3564 setlocale(LC_ALL, "C"); 3565 /* Record the umask before we run the test. */ 3566 umask(oldumask = umask(0)); 3567 /* 3568 * Run the actual test. 3569 */ 3570 (*tests[i].func)(); 3571 /* 3572 * Clean up and report afterwards. 3573 */ 3574 testworkdir = NULL; 3575 /* Restore umask */ 3576 umask(oldumask); 3577 /* Reset locale. */ 3578 setlocale(LC_ALL, "C"); 3579 /* Reset directory. */ 3580 if (!assertChdir(tmpdir)) { 3581 fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n", 3582 tmpdir); 3583 exit(1); 3584 } 3585 /* Report per-test summaries. */ 3586 tests[i].failures = failures - failures_before; 3587 test_summarize(tests[i].failures, skips - skips_before); 3588 /* Close the per-test log file. */ 3589 fclose(logfile); 3590 logfile = NULL; 3591 /* If there were no failures, we can remove the work dir and logfile. */ 3592 if (tests[i].failures == 0) { 3593 if (!keep_temp_files && assertChdir(tmpdir)) { 3594 #if defined(_WIN32) && !defined(__CYGWIN__) 3595 /* Make sure not to leave empty directories. 3596 * Sometimes a processing of closing files used by tests 3597 * is not done, then rmdir will be failed and it will 3598 * leave a empty test directory. So we should wait a few 3599 * seconds and retry rmdir. */ 3600 int r, t; 3601 for (t = 0; t < 10; t++) { 3602 if (t > 0) 3603 Sleep(1000); 3604 r = systemf("rmdir /S /Q %s", tests[i].name); 3605 if (r == 0) 3606 break; 3607 } 3608 systemf("del %s", logfilename); 3609 #else 3610 systemf("rm -rf %s", tests[i].name); 3611 systemf("rm %s", logfilename); 3612 #endif 3613 } 3614 } 3615 /* Return appropriate status. */ 3616 return (tests[i].failures); 3617 } 3618 3619 /* 3620 * 3621 * 3622 * MAIN and support routines. 3623 * 3624 * 3625 */ 3626 3627 static void 3628 usage(const char *program) 3629 { 3630 static const int limit = sizeof(tests) / sizeof(tests[0]); 3631 int i; 3632 3633 printf("Usage: %s [options] <test> <test> ...\n", program); 3634 printf("Default is to run all tests.\n"); 3635 printf("Otherwise, specify the numbers of the tests you wish to run.\n"); 3636 printf("Options:\n"); 3637 printf(" -d Dump core after any failure, for debugging.\n"); 3638 printf(" -k Keep all temp files.\n"); 3639 printf(" Default: temp files for successful tests deleted.\n"); 3640 #ifdef PROGRAM 3641 printf(" -p <path> Path to executable to be tested.\n"); 3642 printf(" Default: path taken from " ENVBASE " environment variable.\n"); 3643 #endif 3644 printf(" -q Quiet.\n"); 3645 printf(" -r <dir> Path to dir containing reference files.\n"); 3646 printf(" Default: Current directory.\n"); 3647 printf(" -u Keep running specifies tests until one fails.\n"); 3648 printf(" -v Verbose.\n"); 3649 printf("Available tests:\n"); 3650 for (i = 0; i < limit; i++) 3651 printf(" %d: %s\n", i, tests[i].name); 3652 exit(1); 3653 } 3654 3655 static char * 3656 get_refdir(const char *d) 3657 { 3658 size_t tried_size, buff_size; 3659 char *buff, *tried, *pwd = NULL, *p = NULL; 3660 3661 #ifdef PATH_MAX 3662 buff_size = PATH_MAX; 3663 #else 3664 buff_size = 8192; 3665 #endif 3666 buff = calloc(buff_size, 1); 3667 if (buff == NULL) { 3668 fprintf(stderr, "Unable to allocate memory\n"); 3669 exit(1); 3670 } 3671 3672 /* Allocate a buffer to hold the various directories we checked. */ 3673 tried_size = buff_size * 2; 3674 tried = calloc(tried_size, 1); 3675 if (tried == NULL) { 3676 fprintf(stderr, "Unable to allocate memory\n"); 3677 exit(1); 3678 } 3679 3680 /* If a dir was specified, try that */ 3681 if (d != NULL) { 3682 pwd = NULL; 3683 snprintf(buff, buff_size, "%s", d); 3684 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3685 if (p != NULL) goto success; 3686 strncat(tried, buff, tried_size - strlen(tried) - 1); 3687 strncat(tried, "\n", tried_size - strlen(tried) - 1); 3688 goto failure; 3689 } 3690 3691 /* Get the current dir. */ 3692 #if defined(PATH_MAX) && !defined(__GLIBC__) 3693 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 3694 #else 3695 pwd = getcwd(NULL, 0); 3696 #endif 3697 while (pwd[strlen(pwd) - 1] == '\n') 3698 pwd[strlen(pwd) - 1] = '\0'; 3699 3700 /* Look for a known file. */ 3701 snprintf(buff, buff_size, "%s", pwd); 3702 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3703 if (p != NULL) goto success; 3704 strncat(tried, buff, tried_size - strlen(tried) - 1); 3705 strncat(tried, "\n", tried_size - strlen(tried) - 1); 3706 3707 snprintf(buff, buff_size, "%s/test", pwd); 3708 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3709 if (p != NULL) goto success; 3710 strncat(tried, buff, tried_size - strlen(tried) - 1); 3711 strncat(tried, "\n", tried_size - strlen(tried) - 1); 3712 3713 #if defined(LIBRARY) 3714 snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY); 3715 #else 3716 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM); 3717 #endif 3718 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3719 if (p != NULL) goto success; 3720 strncat(tried, buff, tried_size - strlen(tried) - 1); 3721 strncat(tried, "\n", tried_size - strlen(tried) - 1); 3722 3723 #if defined(PROGRAM_ALIAS) 3724 snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS); 3725 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3726 if (p != NULL) goto success; 3727 strncat(tried, buff, tried_size - strlen(tried) - 1); 3728 strncat(tried, "\n", tried_size - strlen(tried) - 1); 3729 #endif 3730 3731 if (memcmp(pwd, "/usr/obj", 8) == 0) { 3732 snprintf(buff, buff_size, "%s", pwd + 8); 3733 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3734 if (p != NULL) goto success; 3735 strncat(tried, buff, tried_size - strlen(tried) - 1); 3736 strncat(tried, "\n", tried_size - strlen(tried) - 1); 3737 3738 snprintf(buff, buff_size, "%s/test", pwd + 8); 3739 p = slurpfile(NULL, "%s/%s", buff, KNOWNREF); 3740 if (p != NULL) goto success; 3741 strncat(tried, buff, tried_size - strlen(tried) - 1); 3742 strncat(tried, "\n", tried_size - strlen(tried) - 1); 3743 } 3744 3745 failure: 3746 printf("Unable to locate known reference file %s\n", KNOWNREF); 3747 printf(" Checked following directories:\n%s\n", tried); 3748 printf("Use -r option to specify full path to reference directory\n"); 3749 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG) 3750 DebugBreak(); 3751 #endif 3752 exit(1); 3753 3754 success: 3755 free(p); 3756 free(pwd); 3757 free(tried); 3758 3759 /* Copy result into a fresh buffer to reduce memory usage. */ 3760 p = strdup(buff); 3761 free(buff); 3762 return p; 3763 } 3764 3765 /* Filter tests against a glob pattern. Returns non-zero if test matches 3766 * pattern, zero otherwise. A '^' at the beginning of the pattern negates 3767 * the return values (i.e. returns zero for a match, non-zero otherwise. 3768 */ 3769 static int 3770 test_filter(const char *pattern, const char *test) 3771 { 3772 int retval = 0; 3773 int negate = 0; 3774 const char *p = pattern; 3775 const char *t = test; 3776 3777 if (p[0] == '^') 3778 { 3779 negate = 1; 3780 p++; 3781 } 3782 3783 while (1) 3784 { 3785 if (p[0] == '\\') 3786 p++; 3787 else if (p[0] == '*') 3788 { 3789 while (p[0] == '*') 3790 p++; 3791 if (p[0] == '\\') 3792 p++; 3793 if ((t = strchr(t, p[0])) == 0) 3794 break; 3795 } 3796 if (p[0] != t[0]) 3797 break; 3798 if (p[0] == '\0') { 3799 retval = 1; 3800 break; 3801 } 3802 p++; 3803 t++; 3804 } 3805 3806 return (negate) ? !retval : retval; 3807 } 3808 3809 static int 3810 get_test_set(int *test_set, int limit, const char *test) 3811 { 3812 int start, end; 3813 int idx = 0; 3814 3815 if (test == NULL) { 3816 /* Default: Run all tests. */ 3817 for (;idx < limit; idx++) 3818 test_set[idx] = idx; 3819 return (limit); 3820 } 3821 if (*test >= '0' && *test <= '9') { 3822 const char *vp = test; 3823 start = 0; 3824 while (*vp >= '0' && *vp <= '9') { 3825 start *= 10; 3826 start += *vp - '0'; 3827 ++vp; 3828 } 3829 if (*vp == '\0') { 3830 end = start; 3831 } else if (*vp == '-') { 3832 ++vp; 3833 if (*vp == '\0') { 3834 end = limit - 1; 3835 } else { 3836 end = 0; 3837 while (*vp >= '0' && *vp <= '9') { 3838 end *= 10; 3839 end += *vp - '0'; 3840 ++vp; 3841 } 3842 } 3843 } else 3844 return (-1); 3845 if (start < 0 || end >= limit || start > end) 3846 return (-1); 3847 while (start <= end) 3848 test_set[idx++] = start++; 3849 } else { 3850 for (start = 0; start < limit; ++start) { 3851 const char *name = tests[start].name; 3852 if (test_filter(test, name)) 3853 test_set[idx++] = start; 3854 } 3855 } 3856 return ((idx == 0)?-1:idx); 3857 } 3858 3859 int 3860 main(int argc, char **argv) 3861 { 3862 static const int limit = sizeof(tests) / sizeof(tests[0]); 3863 int test_set[sizeof(tests) / sizeof(tests[0])]; 3864 int i = 0, j = 0, tests_run = 0, tests_failed = 0, option; 3865 int testprogdir_len; 3866 #ifdef PROGRAM 3867 int tmp2_len; 3868 #endif 3869 time_t now; 3870 struct tm *tmptr; 3871 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S) 3872 struct tm tmbuf; 3873 #endif 3874 #if defined(HAVE__LOCALTIME64_S) 3875 errno_t terr; 3876 __time64_t tmptime; 3877 #endif 3878 char *refdir_alloc = NULL; 3879 const char *progname; 3880 char **saved_argv; 3881 const char *tmp, *option_arg, *p; 3882 #ifdef PATH_MAX 3883 char tmpdir[PATH_MAX]; 3884 #else 3885 char tmpdir[256]; 3886 #endif 3887 char *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL; 3888 char tmpdir_timestamp[32]; 3889 3890 (void)argc; /* UNUSED */ 3891 3892 /* Get the current dir. */ 3893 #if defined(PATH_MAX) && !defined(__GLIBC__) 3894 pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */ 3895 #else 3896 pwd = getcwd(NULL, 0); 3897 #endif 3898 while (pwd[strlen(pwd) - 1] == '\n') 3899 pwd[strlen(pwd) - 1] = '\0'; 3900 3901 #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__) 3902 /* To stop to run the default invalid parameter handler. */ 3903 _set_invalid_parameter_handler(invalid_parameter_handler); 3904 /* Disable annoying assertion message box. */ 3905 _CrtSetReportMode(_CRT_ASSERT, 0); 3906 #endif 3907 3908 /* 3909 * Name of this program, used to build root of our temp directory 3910 * tree. 3911 */ 3912 progname = p = argv[0]; 3913 testprogdir_len = strlen(progname) + 1; 3914 if ((testprogdir = (char *)malloc(testprogdir_len)) == NULL) 3915 { 3916 fprintf(stderr, "ERROR: Out of memory."); 3917 exit(1); 3918 } 3919 strncpy(testprogdir, progname, testprogdir_len); 3920 while (*p != '\0') { 3921 /* Support \ or / dir separators for Windows compat. */ 3922 if (*p == '/' || *p == '\\') 3923 { 3924 progname = p + 1; 3925 i = j; 3926 } 3927 ++p; 3928 j++; 3929 } 3930 testprogdir[i] = '\0'; 3931 #if defined(_WIN32) && !defined(__CYGWIN__) 3932 if (testprogdir[0] != '/' && testprogdir[0] != '\\' && 3933 !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') || 3934 (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) && 3935 testprogdir[1] == ':' && 3936 (testprogdir[2] == '/' || testprogdir[2] == '\\'))) 3937 #else 3938 if (testprogdir[0] != '/') 3939 #endif 3940 { 3941 /* Fixup path for relative directories. */ 3942 if ((testprogdir = (char *)realloc(testprogdir, 3943 strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL) 3944 { 3945 fprintf(stderr, "ERROR: Out of memory."); 3946 exit(1); 3947 } 3948 memmove(testprogdir + strlen(pwd) + 1, testprogdir, 3949 strlen(testprogdir) + 1); 3950 memcpy(testprogdir, pwd, strlen(pwd)); 3951 testprogdir[strlen(pwd)] = '/'; 3952 } 3953 3954 #ifdef PROGRAM 3955 /* Get the target program from environment, if available. */ 3956 testprogfile = getenv(ENVBASE); 3957 #endif 3958 3959 if (getenv("TMPDIR") != NULL) 3960 tmp = getenv("TMPDIR"); 3961 else if (getenv("TMP") != NULL) 3962 tmp = getenv("TMP"); 3963 else if (getenv("TEMP") != NULL) 3964 tmp = getenv("TEMP"); 3965 else if (getenv("TEMPDIR") != NULL) 3966 tmp = getenv("TEMPDIR"); 3967 else 3968 tmp = "/tmp"; 3969 3970 /* Allow -d to be controlled through the environment. */ 3971 if (getenv(ENVBASE "_DEBUG") != NULL) 3972 dump_on_failure = 1; 3973 3974 /* Allow -v to be controlled through the environment. */ 3975 if (getenv("_VERBOSITY_LEVEL") != NULL) 3976 { 3977 vlevel = getenv("_VERBOSITY_LEVEL"); 3978 verbosity = atoi(vlevel); 3979 if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL) 3980 { 3981 /* Unsupported verbosity levels are silently ignored */ 3982 vlevel = NULL; 3983 verbosity = VERBOSITY_PASSFAIL; 3984 } 3985 } 3986 3987 /* Get the directory holding test files from environment. */ 3988 refdir = getenv(ENVBASE "_TEST_FILES"); 3989 3990 /* 3991 * Parse options, without using getopt(), which isn't available 3992 * on all platforms. 3993 */ 3994 ++argv; /* Skip program name */ 3995 while (*argv != NULL) { 3996 if (**argv != '-') 3997 break; 3998 p = *argv++; 3999 ++p; /* Skip '-' */ 4000 while (*p != '\0') { 4001 option = *p++; 4002 option_arg = NULL; 4003 /* If 'opt' takes an argument, parse that. */ 4004 if (option == 'p' || option == 'r') { 4005 if (*p != '\0') 4006 option_arg = p; 4007 else if (*argv == NULL) { 4008 fprintf(stderr, 4009 "Option -%c requires argument.\n", 4010 option); 4011 usage(progname); 4012 } else 4013 option_arg = *argv++; 4014 p = ""; /* End of this option word. */ 4015 } 4016 4017 /* Now, handle the option. */ 4018 switch (option) { 4019 case 'd': 4020 dump_on_failure = 1; 4021 break; 4022 case 'k': 4023 keep_temp_files = 1; 4024 break; 4025 case 'p': 4026 #ifdef PROGRAM 4027 testprogfile = option_arg; 4028 #else 4029 fprintf(stderr, "-p option not permitted\n"); 4030 usage(progname); 4031 #endif 4032 break; 4033 case 'q': 4034 if (!vlevel) 4035 verbosity--; 4036 break; 4037 case 'r': 4038 refdir = option_arg; 4039 break; 4040 case 'u': 4041 until_failure++; 4042 break; 4043 case 'v': 4044 if (!vlevel) 4045 verbosity++; 4046 break; 4047 default: 4048 fprintf(stderr, "Unrecognized option '%c'\n", 4049 option); 4050 usage(progname); 4051 } 4052 } 4053 } 4054 4055 /* 4056 * Sanity-check that our options make sense. 4057 */ 4058 #ifdef PROGRAM 4059 if (testprogfile == NULL) 4060 { 4061 tmp2_len = strlen(testprogdir) + 1 + strlen(PROGRAM) + 1; 4062 if ((tmp2 = (char *)malloc(tmp2_len)) == NULL) 4063 { 4064 fprintf(stderr, "ERROR: Out of memory."); 4065 exit(1); 4066 } 4067 strncpy(tmp2, testprogdir, tmp2_len); 4068 strncat(tmp2, "/", tmp2_len); 4069 strncat(tmp2, PROGRAM, tmp2_len); 4070 testprogfile = tmp2; 4071 } 4072 4073 { 4074 char *testprg; 4075 int testprg_len; 4076 #if defined(_WIN32) && !defined(__CYGWIN__) 4077 /* Command.com sometimes rejects '/' separators. */ 4078 testprg = strdup(testprogfile); 4079 for (i = 0; testprg[i] != '\0'; i++) { 4080 if (testprg[i] == '/') 4081 testprg[i] = '\\'; 4082 } 4083 testprogfile = testprg; 4084 #endif 4085 /* Quote the name that gets put into shell command lines. */ 4086 testprg_len = strlen(testprogfile) + 3; 4087 testprg = malloc(testprg_len); 4088 strncpy(testprg, "\"", testprg_len); 4089 strncat(testprg, testprogfile, testprg_len); 4090 strncat(testprg, "\"", testprg_len); 4091 testprog = testprg; 4092 } 4093 #endif 4094 4095 #if !defined(_WIN32) && defined(SIGPIPE) 4096 { /* Ignore SIGPIPE signals */ 4097 struct sigaction sa; 4098 sa.sa_handler = SIG_IGN; 4099 sigemptyset(&sa.sa_mask); 4100 sa.sa_flags = 0; 4101 sigaction(SIGPIPE, &sa, NULL); 4102 } 4103 #endif 4104 4105 /* 4106 * Create a temp directory for the following tests. 4107 * Include the time the tests started as part of the name, 4108 * to make it easier to track the results of multiple tests. 4109 */ 4110 now = time(NULL); 4111 for (i = 0; ; i++) { 4112 #if defined(HAVE_LOCALTIME_R) 4113 tmptr = localtime_r(&now, &tmbuf); 4114 #elif defined(HAVE__LOCALTIME64_S) 4115 tmptime = now; 4116 terr = _localtime64_s(&tmbuf, &tmptime); 4117 if (terr) 4118 tmptr = NULL; 4119 else 4120 tmptr = &tmbuf; 4121 #else 4122 tmptr = localtime(&now); 4123 #endif 4124 strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), 4125 "%Y-%m-%dT%H.%M.%S", tmptr); 4126 if ((strlen(tmp) + 1 + strlen(progname) + 1 + 4127 strlen(tmpdir_timestamp) + 1 + 3) > 4128 (sizeof(tmpdir) / sizeof(char))) { 4129 fprintf(stderr, 4130 "ERROR: Temp directory pathname too long\n"); 4131 exit(1); 4132 } 4133 snprintf(tmpdir, sizeof(tmpdir), "%s/%s.%s-%03d", tmp, 4134 progname, tmpdir_timestamp, i); 4135 if (assertMakeDir(tmpdir,0755)) 4136 break; 4137 if (i >= 999) { 4138 fprintf(stderr, 4139 "ERROR: Unable to create temp directory %s\n", 4140 tmpdir); 4141 exit(1); 4142 } 4143 } 4144 4145 /* 4146 * If the user didn't specify a directory for locating 4147 * reference files, try to find the reference files in 4148 * the "usual places." 4149 */ 4150 refdir = refdir_alloc = get_refdir(refdir); 4151 4152 /* 4153 * Banner with basic information. 4154 */ 4155 printf("\n"); 4156 printf("If tests fail or crash, details will be in:\n"); 4157 printf(" %s\n", tmpdir); 4158 printf("\n"); 4159 if (verbosity > VERBOSITY_SUMMARY_ONLY) { 4160 printf("Reference files will be read from: %s\n", refdir); 4161 #ifdef PROGRAM 4162 printf("Running tests on: %s\n", testprog); 4163 #endif 4164 printf("Exercising: "); 4165 fflush(stdout); 4166 printf("%s\n", EXTRA_VERSION); 4167 } else { 4168 printf("Running "); 4169 fflush(stdout); 4170 } 4171 4172 /* 4173 * Run some or all of the individual tests. 4174 */ 4175 saved_argv = argv; 4176 do { 4177 argv = saved_argv; 4178 do { 4179 int test_num; 4180 4181 test_num = get_test_set(test_set, limit, *argv); 4182 if (test_num < 0) { 4183 printf("*** INVALID Test %s\n", *argv); 4184 free(refdir_alloc); 4185 free(testprogdir); 4186 usage(progname); 4187 return (1); 4188 } 4189 for (i = 0; i < test_num; i++) { 4190 tests_run++; 4191 if (test_run(test_set[i], tmpdir)) { 4192 tests_failed++; 4193 if (until_failure) 4194 goto finish; 4195 } 4196 } 4197 if (*argv != NULL) 4198 argv++; 4199 } while (*argv != NULL); 4200 } while (until_failure); 4201 4202 finish: 4203 /* Must be freed after all tests run */ 4204 free(tmp2); 4205 free(testprogdir); 4206 free(pwd); 4207 4208 /* 4209 * Report summary statistics. 4210 */ 4211 if (verbosity > VERBOSITY_SUMMARY_ONLY) { 4212 printf("\n"); 4213 printf("Totals:\n"); 4214 printf(" Tests run: %8d\n", tests_run); 4215 printf(" Tests failed: %8d\n", tests_failed); 4216 printf(" Assertions checked:%8d\n", assertions); 4217 printf(" Assertions failed: %8d\n", failures); 4218 printf(" Skips reported: %8d\n", skips); 4219 } 4220 if (failures) { 4221 printf("\n"); 4222 printf("Failing tests:\n"); 4223 for (i = 0; i < limit; ++i) { 4224 if (tests[i].failures) 4225 printf(" %d: %s (%d failures)\n", i, 4226 tests[i].name, tests[i].failures); 4227 } 4228 printf("\n"); 4229 printf("Details for failing tests: %s\n", tmpdir); 4230 printf("\n"); 4231 } else { 4232 if (verbosity == VERBOSITY_SUMMARY_ONLY) 4233 printf("\n"); 4234 printf("%d tests passed, no failures\n", tests_run); 4235 } 4236 4237 free(refdir_alloc); 4238 4239 /* If the final tmpdir is empty, we can remove it. */ 4240 /* This should be the usual case when all tests succeed. */ 4241 assertChdir(".."); 4242 rmdir(tmpdir); 4243 4244 return (tests_failed ? 1 : 0); 4245 } 4246