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