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