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