1 /* 2 * Copyright (c) 2003-2017 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 * $FreeBSD$ 26 */ 27 28 #ifndef TEST_COMMON_H 29 #define TEST_COMMON_H 30 31 /* 32 * The goal of this file (and the matching test.c) is to 33 * simplify the very repetitive test-*.c test programs. 34 */ 35 #if defined(HAVE_CONFIG_H) 36 /* Most POSIX platforms use the 'configure' script to build config.h */ 37 #include "config.h" 38 #elif defined(__FreeBSD__) 39 /* Building as part of FreeBSD system requires a pre-built config.h. */ 40 #include "config_freebsd.h" 41 #elif defined(_WIN32) && !defined(__CYGWIN__) 42 /* Win32 can't run the 'configure' script. */ 43 #include "config_windows.h" 44 #else 45 /* Warn if the library hasn't been (automatically or manually) configured. */ 46 #error Oops: No config.h and no pre-built configuration in test.h. 47 #endif 48 49 #include <sys/types.h> /* Windows requires this before sys/stat.h */ 50 #include <sys/stat.h> 51 52 #if HAVE_DIRENT_H 53 #include <dirent.h> 54 #endif 55 #ifdef HAVE_DIRECT_H 56 #include <direct.h> 57 #define dirent direct 58 #endif 59 #include <errno.h> 60 #include <fcntl.h> 61 #ifdef HAVE_IO_H 62 #include <io.h> 63 #endif 64 #ifdef HAVE_STDINT_H 65 #include <stdint.h> 66 #endif 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <ctype.h> 71 #include <time.h> 72 #ifdef HAVE_UNISTD_H 73 #include <unistd.h> 74 #endif 75 #include <wchar.h> 76 #ifdef HAVE_ACL_LIBACL_H 77 #include <acl/libacl.h> 78 #endif 79 #ifdef HAVE_SYS_ACL_H 80 #include <sys/acl.h> 81 #endif 82 #ifdef HAVE_WINDOWS_H 83 #include <windows.h> 84 #endif 85 86 /* 87 * System-specific tweaks. We really want to minimize these 88 * as much as possible, since they make it harder to understand 89 * the mainline code. 90 */ 91 92 /* Windows (including Visual Studio and MinGW but not Cygwin) */ 93 #if defined(_WIN32) && !defined(__CYGWIN__) 94 #if !defined(__BORLANDC__) 95 #undef chdir 96 #define chdir _chdir 97 #define strdup _strdup 98 #endif 99 #endif 100 101 /* Visual Studio */ 102 #if defined(_MSC_VER) && _MSC_VER < 1900 103 #define snprintf sprintf_s 104 #endif 105 106 #if defined(__BORLANDC__) 107 #pragma warn -8068 /* Constant out of range in comparison. */ 108 #endif 109 110 /* Haiku OS and QNX */ 111 #if defined(__HAIKU__) || defined(__QNXNTO__) 112 /* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */ 113 #include <stdint.h> 114 #endif 115 116 /* Get a real definition for __FBSDID if we can */ 117 #if HAVE_SYS_CDEFS_H 118 #include <sys/cdefs.h> 119 #endif 120 121 /* If not, define it so as to avoid dangling semicolons. */ 122 #ifndef __FBSDID 123 #define __FBSDID(a) struct _undefined_hack 124 #endif 125 126 #ifndef O_BINARY 127 #define O_BINARY 0 128 #endif 129 130 /* 131 * If this platform has <sys/acl.h>, acl_create(), acl_init(), 132 * acl_set_file(), and ACL_USER, we assume it has the rest of the 133 * POSIX.1e draft functions used in archive_read_extract.c. 134 */ 135 #if HAVE_SYS_ACL_H && HAVE_ACL_CREATE_ENTRY && HAVE_ACL_INIT && HAVE_ACL_SET_FILE 136 #if HAVE_DECL_ACL_USER 137 #define HAVE_POSIX_ACL 1 138 #elif HAVE_DECL_ACL_TYPE_EXTENDED 139 #define HAVE_DARWIN_ACL 1 140 #endif 141 #if HAVE_DECL_ACL_TYPE_NFS4 142 #define HAVE_FREEBSD_NFS4_ACL 1 143 #endif 144 #endif 145 146 /* 147 * If this platform has <sys/acl.h>, acl_get(), facl_get(), acl_set(), 148 * facl_set() and types aclent_t and ace_t it uses Solaris-style ACL functions 149 */ 150 #if HAVE_SYS_ACL_H && HAVE_ACL && HAVE_FACL && HAVE_ACLENT_T && \ 151 HAVE_DECL_GETACL && HAVE_DECL_GETACLCNT && HAVE_DECL_SETACL 152 #define HAVE_SUN_ACL 1 153 #if HAVE_ACE_T && HAVE_DECL_ACE_GETACL && HAVE_DECL_ACE_GETACLCNT && \ 154 HAVE_DECL_ACE_SETACL 155 #define HAVE_SUN_NFS4_ACL 1 156 #endif 157 #endif 158 159 /* Define if platform supports NFSv4 ACLs */ 160 #if HAVE_FREEBSD_NFS4_ACL || HAVE_SUN_NFS4_ACL || HAVE_DARWIN_ACL 161 #define HAVE_NFS4_ACL 1 162 #endif 163 164 #define ARCHIVE_TEST_ACL_TYPE_POSIX1E 1 165 #define ARCHIVE_TEST_ACL_TYPE_NFS4 2 166 167 /* 168 * Redefine DEFINE_TEST for use in defining the test functions. 169 */ 170 #undef DEFINE_TEST 171 #define DEFINE_TEST(name) void name(void); void name(void) 172 173 /* An implementation of the standard assert() macro */ 174 #define assert(e) assertion_assert(__FILE__, __LINE__, (e), #e, NULL) 175 /* chdir() and error if it fails */ 176 #define assertChdir(path) \ 177 assertion_chdir(__FILE__, __LINE__, path) 178 /* Assert two files have the same file flags */ 179 #define assertEqualFflags(patha, pathb) \ 180 assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 0) 181 /* Assert two integers are the same. Reports value of each one if not. */ 182 #define assertEqualInt(v1,v2) \ 183 assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) 184 /* Assert two strings are the same. Reports value of each one if not. */ 185 #define assertEqualString(v1,v2) \ 186 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0) 187 #define assertEqualUTF8String(v1,v2) \ 188 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1) 189 /* As above, but v1 and v2 are wchar_t * */ 190 #define assertEqualWString(v1,v2) \ 191 assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) 192 /* As above, but raw blocks of bytes. */ 193 #define assertEqualMem(v1, v2, l) \ 194 assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL) 195 /* Assert that memory is full of a specified byte */ 196 #define assertMemoryFilledWith(v1, l, b) \ 197 assertion_memory_filled_with(__FILE__, __LINE__, (v1), #v1, (l), #l, (b), #b, NULL) 198 /* Assert two files are the same. */ 199 #define assertEqualFile(f1, f2) \ 200 assertion_equal_file(__FILE__, __LINE__, (f1), (f2)) 201 /* Assert that a file is empty. */ 202 #define assertEmptyFile(pathname) \ 203 assertion_empty_file(__FILE__, __LINE__, (pathname)) 204 /* Assert that a file is not empty. */ 205 #define assertNonEmptyFile(pathname) \ 206 assertion_non_empty_file(__FILE__, __LINE__, (pathname)) 207 #define assertFileAtime(pathname, sec, nsec) \ 208 assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec) 209 #define assertFileAtimeRecent(pathname) \ 210 assertion_file_atime_recent(__FILE__, __LINE__, pathname) 211 #define assertFileBirthtime(pathname, sec, nsec) \ 212 assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec) 213 #define assertFileBirthtimeRecent(pathname) \ 214 assertion_file_birthtime_recent(__FILE__, __LINE__, pathname) 215 /* Assert that a file exists; supports printf-style arguments. */ 216 #define assertFileExists(pathname) \ 217 assertion_file_exists(__FILE__, __LINE__, pathname) 218 /* Assert that a file exists. */ 219 #define assertFileNotExists(pathname) \ 220 assertion_file_not_exists(__FILE__, __LINE__, pathname) 221 /* Assert that file contents match a string. */ 222 #define assertFileContents(data, data_size, pathname) \ 223 assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname) 224 /* Verify that a file does not contain invalid strings */ 225 #define assertFileContainsNoInvalidStrings(pathname, strings) \ 226 assertion_file_contains_no_invalid_strings(__FILE__, __LINE__, pathname, strings) 227 #define assertFileMtime(pathname, sec, nsec) \ 228 assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec) 229 #define assertFileMtimeRecent(pathname) \ 230 assertion_file_mtime_recent(__FILE__, __LINE__, pathname) 231 #define assertFileNLinks(pathname, nlinks) \ 232 assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks) 233 #define assertFileSize(pathname, size) \ 234 assertion_file_size(__FILE__, __LINE__, pathname, size) 235 #define assertFileMode(pathname, mode) \ 236 assertion_file_mode(__FILE__, __LINE__, pathname, mode) 237 #define assertTextFileContents(text, pathname) \ 238 assertion_text_file_contents(__FILE__, __LINE__, text, pathname) 239 #define assertFileContainsLinesAnyOrder(pathname, lines) \ 240 assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines) 241 #define assertIsDir(pathname, mode) \ 242 assertion_is_dir(__FILE__, __LINE__, pathname, mode) 243 #define assertIsHardlink(path1, path2) \ 244 assertion_is_hardlink(__FILE__, __LINE__, path1, path2) 245 #define assertIsNotHardlink(path1, path2) \ 246 assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2) 247 #define assertIsReg(pathname, mode) \ 248 assertion_is_reg(__FILE__, __LINE__, pathname, mode) 249 #define assertIsSymlink(pathname, contents) \ 250 assertion_is_symlink(__FILE__, __LINE__, pathname, contents) 251 /* Create a directory, report error if it fails. */ 252 #define assertMakeDir(dirname, mode) \ 253 assertion_make_dir(__FILE__, __LINE__, dirname, mode) 254 #define assertMakeFile(path, mode, contents) \ 255 assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents) 256 #define assertMakeBinFile(path, mode, csize, contents) \ 257 assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents) 258 #define assertMakeHardlink(newfile, oldfile) \ 259 assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile) 260 #define assertMakeSymlink(newfile, linkto) \ 261 assertion_make_symlink(__FILE__, __LINE__, newfile, linkto) 262 #define assertSetNodump(path) \ 263 assertion_set_nodump(__FILE__, __LINE__, path) 264 #define assertUmask(mask) \ 265 assertion_umask(__FILE__, __LINE__, mask) 266 /* Assert that two files have unequal file flags */ 267 #define assertUnequalFflags(patha, pathb) \ 268 assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 1) 269 #define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec) \ 270 assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec) 271 #ifndef PROGRAM 272 #define assertEntrySetAcls(entry, acls, count) \ 273 assertion_entry_set_acls(__FILE__, __LINE__, entry, acls, count) 274 #define assertEntryCompareAcls(entry, acls, count, type, mode) \ 275 assertion_entry_compare_acls(__FILE__, __LINE__, entry, acls, count, type, mode) 276 #endif 277 278 /* 279 * This would be simple with C99 variadic macros, but I don't want to 280 * require that. Instead, I insert a function call before each 281 * skipping() call to pass the file and line information down. Crude, 282 * but effective. 283 */ 284 #define skipping \ 285 skipping_setup(__FILE__, __LINE__);test_skipping 286 287 /* Function declarations. These are defined in test_utility.c. */ 288 void failure(const char *fmt, ...); 289 int assertion_assert(const char *, int, int, const char *, void *); 290 int assertion_chdir(const char *, int, const char *); 291 int assertion_compare_fflags(const char *, int, const char *, const char *, 292 int); 293 int assertion_empty_file(const char *, int, const char *); 294 int assertion_equal_file(const char *, int, const char *, const char *); 295 int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *); 296 int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *); 297 int assertion_memory_filled_with(const char *, int, const void *, const char *, size_t, const char *, char, const char *, void *); 298 int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int); 299 int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *); 300 int assertion_file_atime(const char *, int, const char *, long, long); 301 int assertion_file_atime_recent(const char *, int, const char *); 302 int assertion_file_birthtime(const char *, int, const char *, long, long); 303 int assertion_file_birthtime_recent(const char *, int, const char *); 304 int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **); 305 int assertion_file_contains_no_invalid_strings(const char *, int, const char *, const char **); 306 int assertion_file_contents(const char *, int, const void *, int, const char *); 307 int assertion_file_exists(const char *, int, const char *); 308 int assertion_file_mode(const char *, int, const char *, int); 309 int assertion_file_mtime(const char *, int, const char *, long, long); 310 int assertion_file_mtime_recent(const char *, int, const char *); 311 int assertion_file_nlinks(const char *, int, const char *, int); 312 int assertion_file_not_exists(const char *, int, const char *); 313 int assertion_file_size(const char *, int, const char *, long); 314 int assertion_is_dir(const char *, int, const char *, int); 315 int assertion_is_hardlink(const char *, int, const char *, const char *); 316 int assertion_is_not_hardlink(const char *, int, const char *, const char *); 317 int assertion_is_reg(const char *, int, const char *, int); 318 int assertion_is_symlink(const char *, int, const char *, const char *); 319 int assertion_make_dir(const char *, int, const char *, int); 320 int assertion_make_file(const char *, int, const char *, int, int, const void *); 321 int assertion_make_hardlink(const char *, int, const char *newpath, const char *); 322 int assertion_make_symlink(const char *, int, const char *newpath, const char *); 323 int assertion_non_empty_file(const char *, int, const char *); 324 int assertion_set_nodump(const char *, int, const char *); 325 int assertion_text_file_contents(const char *, int, const char *buff, const char *f); 326 int assertion_umask(const char *, int, int); 327 int assertion_utimes(const char *, int, const char *, long, long, long, long ); 328 int assertion_version(const char*, int, const char *, const char *); 329 330 void skipping_setup(const char *, int); 331 void test_skipping(const char *fmt, ...); 332 333 /* Like sprintf, then system() */ 334 int systemf(const char * fmt, ...); 335 336 /* Delay until time() returns a value after this. */ 337 void sleepUntilAfter(time_t); 338 339 /* Return true if this platform can create symlinks. */ 340 int canSymlink(void); 341 342 /* Return true if this platform can run the "bzip2" program. */ 343 int canBzip2(void); 344 345 /* Return true if this platform can run the "grzip" program. */ 346 int canGrzip(void); 347 348 /* Return true if this platform can run the "gzip" program. */ 349 int canGzip(void); 350 351 /* Return true if this platform can run the specified command. */ 352 int canRunCommand(const char *); 353 354 /* Return true if this platform can run the "lrzip" program. */ 355 int canLrzip(void); 356 357 /* Return true if this platform can run the "lz4" program. */ 358 int canLz4(void); 359 360 /* Return true if this platform can run the "lzip" program. */ 361 int canLzip(void); 362 363 /* Return true if this platform can run the "lzma" program. */ 364 int canLzma(void); 365 366 /* Return true if this platform can run the "lzop" program. */ 367 int canLzop(void); 368 369 /* Return true if this platform can run the "xz" program. */ 370 int canXz(void); 371 372 /* Return true if this filesystem can handle nodump flags. */ 373 int canNodump(void); 374 375 /* Set test ACLs */ 376 int setTestAcl(const char *path); 377 378 /* Return true if the file has large i-node number(>0xffffffff). */ 379 int is_LargeInode(const char *); 380 381 #if HAVE_SUN_ACL 382 /* Fetch ACLs on Solaris using acl() or facl() */ 383 void *sunacl_get(int cmd, int *aclcnt, int fd, const char *path); 384 #endif 385 386 /* Suck file into string allocated via malloc(). Call free() when done. */ 387 /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */ 388 char *slurpfile(size_t *, const char *fmt, ...); 389 390 /* Dump block of bytes to a file. */ 391 void dumpfile(const char *filename, void *, size_t); 392 393 /* Extracts named reference file to the current directory. */ 394 void extract_reference_file(const char *); 395 /* Copies named reference file to the current directory. */ 396 void copy_reference_file(const char *); 397 398 /* Extracts a list of files to the current directory. 399 * List must be NULL terminated. 400 */ 401 void extract_reference_files(const char **); 402 403 /* Subtract umask from mode */ 404 mode_t umasked(mode_t expected_mode); 405 406 /* Path to working directory for current test */ 407 extern const char *testworkdir; 408 409 #ifndef PROGRAM 410 /* 411 * Special interfaces for libarchive test harness. 412 */ 413 414 #include "archive.h" 415 #include "archive_entry.h" 416 417 /* ACL structure */ 418 struct archive_test_acl_t { 419 int type; /* Type of ACL */ 420 int permset; /* Permissions for this class of users. */ 421 int tag; /* Owner, User, Owning group, group, other, etc. */ 422 int qual; /* GID or UID of user/group, depending on tag. */ 423 const char *name; /* Name of user/group, depending on tag. */ 424 }; 425 426 /* Set ACLs */ 427 int assertion_entry_set_acls(const char *, int, struct archive_entry *, 428 struct archive_test_acl_t *, int); 429 430 /* Compare ACLs */ 431 int assertion_entry_compare_acls(const char *, int, struct archive_entry *, 432 struct archive_test_acl_t *, int, int, int); 433 434 /* Special customized read-from-memory interface. */ 435 int read_open_memory(struct archive *, const void *, size_t, size_t); 436 /* _minimal version exercises a slightly different set of libarchive APIs. */ 437 int read_open_memory_minimal(struct archive *, const void *, size_t, size_t); 438 /* _seek version produces a seekable file. */ 439 int read_open_memory_seek(struct archive *, const void *, size_t, size_t); 440 441 /* Versions of above that accept an archive argument for additional info. */ 442 #define assertA(e) assertion_assert(__FILE__, __LINE__, (e), #e, (a)) 443 #define assertEqualIntA(a,v1,v2) \ 444 assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a)) 445 #define assertEqualStringA(a,v1,v2) \ 446 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a), 0) 447 448 #else /* defined(PROGRAM) */ 449 /* 450 * Special interfaces for program test harness. 451 */ 452 453 /* Pathname of exe to be tested. */ 454 extern const char *testprogfile; 455 /* Name of exe to use in printf-formatted command strings. */ 456 /* On Windows, this includes leading/trailing quotes. */ 457 extern const char *testprog; 458 459 void assertVersion(const char *prog, const char *base); 460 461 #endif /* defined(PROGRAM) */ 462 463 #ifdef USE_DMALLOC 464 #include <dmalloc.h> 465 #endif 466 467 #endif /* TEST_COMMON_H */ 468