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