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