test_common.h revision 324418
1146773Ssam/* 2146773Ssam * Copyright (c) 2003-2017 Tim Kientzle 3146773Ssam * All rights reserved. 4146773Ssam * 5146773Ssam * Redistribution and use in source and binary forms, with or without 6146773Ssam * modification, are permitted provided that the following conditions 7146773Ssam * are met: 8146773Ssam * 1. Redistributions of source code must retain the above copyright 9146773Ssam * notice, this list of conditions and the following disclaimer. 10146773Ssam * 2. Redistributions in binary form must reproduce the above copyright 11146773Ssam * notice, this list of conditions and the following disclaimer in the 12146773Ssam * documentation and/or other materials provided with the distribution. 13146773Ssam * 14146773Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15146773Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16146773Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17146773Ssam * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18190207Srpaulo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19146773Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20146773Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21146773Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22146773Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23146773Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24146773Ssam * 25146773Ssam * $FreeBSD: stable/10/contrib/libarchive/test_utils/test_common.h 324418 2017-10-08 20:55:45Z mm $ 26146773Ssam */ 27146773Ssam 28146773Ssam#ifndef TEST_COMMON_H 29146773Ssam#define TEST_COMMON_H 30146773Ssam 31146773Ssam/* 32146773Ssam * The goal of this file (and the matching test.c) is to 33146773Ssam * simplify the very repetitive test-*.c test programs. 34146773Ssam */ 35146773Ssam#if defined(HAVE_CONFIG_H) 36146773Ssam/* Most POSIX platforms use the 'configure' script to build config.h */ 37146773Ssam#include "config.h" 38146773Ssam#elif defined(__FreeBSD__) 39146773Ssam/* Building as part of FreeBSD system requires a pre-built config.h. */ 40146773Ssam#include "config_freebsd.h" 41146773Ssam#elif defined(_WIN32) && !defined(__CYGWIN__) 42146773Ssam/* Win32 can't run the 'configure' script. */ 43146773Ssam#include "config_windows.h" 44146773Ssam#else 45146773Ssam/* Warn if the library hasn't been (automatically or manually) configured. */ 46146773Ssam#error Oops: No config.h and no pre-built configuration in test.h. 47214478Srpaulo#endif 48146773Ssam 49146773Ssam#include <sys/types.h> /* Windows requires this before sys/stat.h */ 50146773Ssam#include <sys/stat.h> 51146773Ssam 52146773Ssam#if HAVE_DIRENT_H 53146773Ssam#include <dirent.h> 54146773Ssam#endif 55146773Ssam#ifdef HAVE_DIRECT_H 56146773Ssam#include <direct.h> 57146773Ssam#define dirent direct 58146773Ssam#endif 59146773Ssam#include <errno.h> 60146773Ssam#include <fcntl.h> 61214478Srpaulo#ifdef HAVE_IO_H 62214478Srpaulo#include <io.h> 63214478Srpaulo#endif 64146773Ssam#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_SYS_RICHACL_H 83#include <sys/richacl.h> 84#endif 85#ifdef HAVE_WINDOWS_H 86#include <windows.h> 87#endif 88 89/* 90 * System-specific tweaks. We really want to minimize these 91 * as much as possible, since they make it harder to understand 92 * the mainline code. 93 */ 94 95/* Windows (including Visual Studio and MinGW but not Cygwin) */ 96#if defined(_WIN32) && !defined(__CYGWIN__) 97#if !defined(__BORLANDC__) 98#undef chdir 99#define chdir _chdir 100#define strdup _strdup 101#endif 102#endif 103 104/* Visual Studio */ 105#if defined(_MSC_VER) && _MSC_VER < 1900 106#define snprintf sprintf_s 107#endif 108 109#if defined(__BORLANDC__) 110#pragma warn -8068 /* Constant out of range in comparison. */ 111#endif 112 113/* Haiku OS and QNX */ 114#if defined(__HAIKU__) || defined(__QNXNTO__) 115/* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */ 116#include <stdint.h> 117#endif 118 119/* Get a real definition for __FBSDID if we can */ 120#if HAVE_SYS_CDEFS_H 121#include <sys/cdefs.h> 122#endif 123 124/* If not, define it so as to avoid dangling semicolons. */ 125#ifndef __FBSDID 126#define __FBSDID(a) struct _undefined_hack 127#endif 128 129#ifndef O_BINARY 130#define O_BINARY 0 131#endif 132 133#include "archive_platform_acl.h" 134#define ARCHIVE_TEST_ACL_TYPE_POSIX1E 1 135#define ARCHIVE_TEST_ACL_TYPE_NFS4 2 136 137#include "archive_platform_xattr.h" 138 139/* 140 * Redefine DEFINE_TEST for use in defining the test functions. 141 */ 142#undef DEFINE_TEST 143#define DEFINE_TEST(name) void name(void); void name(void) 144 145/* An implementation of the standard assert() macro */ 146#define assert(e) assertion_assert(__FILE__, __LINE__, (e), #e, NULL) 147/* chdir() and error if it fails */ 148#define assertChdir(path) \ 149 assertion_chdir(__FILE__, __LINE__, path) 150/* Assert two files have the same file flags */ 151#define assertEqualFflags(patha, pathb) \ 152 assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 0) 153/* Assert two integers are the same. Reports value of each one if not. */ 154#define assertEqualInt(v1,v2) \ 155 assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) 156/* Assert two strings are the same. Reports value of each one if not. */ 157#define assertEqualString(v1,v2) \ 158 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0) 159#define assertEqualUTF8String(v1,v2) \ 160 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1) 161/* As above, but v1 and v2 are wchar_t * */ 162#define assertEqualWString(v1,v2) \ 163 assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) 164/* As above, but raw blocks of bytes. */ 165#define assertEqualMem(v1, v2, l) \ 166 assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL) 167/* Assert that memory is full of a specified byte */ 168#define assertMemoryFilledWith(v1, l, b) \ 169 assertion_memory_filled_with(__FILE__, __LINE__, (v1), #v1, (l), #l, (b), #b, NULL) 170/* Assert two files are the same. */ 171#define assertEqualFile(f1, f2) \ 172 assertion_equal_file(__FILE__, __LINE__, (f1), (f2)) 173/* Assert that a file is empty. */ 174#define assertEmptyFile(pathname) \ 175 assertion_empty_file(__FILE__, __LINE__, (pathname)) 176/* Assert that a file is not empty. */ 177#define assertNonEmptyFile(pathname) \ 178 assertion_non_empty_file(__FILE__, __LINE__, (pathname)) 179#define assertFileAtime(pathname, sec, nsec) \ 180 assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec) 181#define assertFileAtimeRecent(pathname) \ 182 assertion_file_atime_recent(__FILE__, __LINE__, pathname) 183#define assertFileBirthtime(pathname, sec, nsec) \ 184 assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec) 185#define assertFileBirthtimeRecent(pathname) \ 186 assertion_file_birthtime_recent(__FILE__, __LINE__, pathname) 187/* Assert that a file exists; supports printf-style arguments. */ 188#define assertFileExists(pathname) \ 189 assertion_file_exists(__FILE__, __LINE__, pathname) 190/* Assert that a file exists. */ 191#define assertFileNotExists(pathname) \ 192 assertion_file_not_exists(__FILE__, __LINE__, pathname) 193/* Assert that file contents match a string. */ 194#define assertFileContents(data, data_size, pathname) \ 195 assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname) 196/* Verify that a file does not contain invalid strings */ 197#define assertFileContainsNoInvalidStrings(pathname, strings) \ 198 assertion_file_contains_no_invalid_strings(__FILE__, __LINE__, pathname, strings) 199#define assertFileMtime(pathname, sec, nsec) \ 200 assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec) 201#define assertFileMtimeRecent(pathname) \ 202 assertion_file_mtime_recent(__FILE__, __LINE__, pathname) 203#define assertFileNLinks(pathname, nlinks) \ 204 assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks) 205#define assertFileSize(pathname, size) \ 206 assertion_file_size(__FILE__, __LINE__, pathname, size) 207#define assertFileMode(pathname, mode) \ 208 assertion_file_mode(__FILE__, __LINE__, pathname, mode) 209#define assertTextFileContents(text, pathname) \ 210 assertion_text_file_contents(__FILE__, __LINE__, text, pathname) 211#define assertFileContainsLinesAnyOrder(pathname, lines) \ 212 assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines) 213#define assertIsDir(pathname, mode) \ 214 assertion_is_dir(__FILE__, __LINE__, pathname, mode) 215#define assertIsHardlink(path1, path2) \ 216 assertion_is_hardlink(__FILE__, __LINE__, path1, path2) 217#define assertIsNotHardlink(path1, path2) \ 218 assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2) 219#define assertIsReg(pathname, mode) \ 220 assertion_is_reg(__FILE__, __LINE__, pathname, mode) 221#define assertIsSymlink(pathname, contents) \ 222 assertion_is_symlink(__FILE__, __LINE__, pathname, contents) 223/* Create a directory, report error if it fails. */ 224#define assertMakeDir(dirname, mode) \ 225 assertion_make_dir(__FILE__, __LINE__, dirname, mode) 226#define assertMakeFile(path, mode, contents) \ 227 assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents) 228#define assertMakeBinFile(path, mode, csize, contents) \ 229 assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents) 230#define assertMakeHardlink(newfile, oldfile) \ 231 assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile) 232#define assertMakeSymlink(newfile, linkto) \ 233 assertion_make_symlink(__FILE__, __LINE__, newfile, linkto) 234#define assertSetNodump(path) \ 235 assertion_set_nodump(__FILE__, __LINE__, path) 236#define assertUmask(mask) \ 237 assertion_umask(__FILE__, __LINE__, mask) 238/* Assert that two files have unequal file flags */ 239#define assertUnequalFflags(patha, pathb) \ 240 assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 1) 241#define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec) \ 242 assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec) 243#ifndef PROGRAM 244#define assertEntrySetAcls(entry, acls, count) \ 245 assertion_entry_set_acls(__FILE__, __LINE__, entry, acls, count) 246#define assertEntryCompareAcls(entry, acls, count, type, mode) \ 247 assertion_entry_compare_acls(__FILE__, __LINE__, entry, acls, count, type, mode) 248#endif 249 250/* 251 * This would be simple with C99 variadic macros, but I don't want to 252 * require that. Instead, I insert a function call before each 253 * skipping() call to pass the file and line information down. Crude, 254 * but effective. 255 */ 256#define skipping \ 257 skipping_setup(__FILE__, __LINE__);test_skipping 258 259/* Function declarations. These are defined in test_utility.c. */ 260void failure(const char *fmt, ...); 261int assertion_assert(const char *, int, int, const char *, void *); 262int assertion_chdir(const char *, int, const char *); 263int assertion_compare_fflags(const char *, int, const char *, const char *, 264 int); 265int assertion_empty_file(const char *, int, const char *); 266int assertion_equal_file(const char *, int, const char *, const char *); 267int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *); 268int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *); 269int assertion_memory_filled_with(const char *, int, const void *, const char *, size_t, const char *, char, const char *, void *); 270int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int); 271int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *); 272int assertion_file_atime(const char *, int, const char *, long, long); 273int assertion_file_atime_recent(const char *, int, const char *); 274int assertion_file_birthtime(const char *, int, const char *, long, long); 275int assertion_file_birthtime_recent(const char *, int, const char *); 276int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **); 277int assertion_file_contains_no_invalid_strings(const char *, int, const char *, const char **); 278int assertion_file_contents(const char *, int, const void *, int, const char *); 279int assertion_file_exists(const char *, int, const char *); 280int assertion_file_mode(const char *, int, const char *, int); 281int assertion_file_mtime(const char *, int, const char *, long, long); 282int assertion_file_mtime_recent(const char *, int, const char *); 283int assertion_file_nlinks(const char *, int, const char *, int); 284int assertion_file_not_exists(const char *, int, const char *); 285int assertion_file_size(const char *, int, const char *, long); 286int assertion_is_dir(const char *, int, const char *, int); 287int assertion_is_hardlink(const char *, int, const char *, const char *); 288int assertion_is_not_hardlink(const char *, int, const char *, const char *); 289int assertion_is_reg(const char *, int, const char *, int); 290int assertion_is_symlink(const char *, int, const char *, const char *); 291int assertion_make_dir(const char *, int, const char *, int); 292int assertion_make_file(const char *, int, const char *, int, int, const void *); 293int assertion_make_hardlink(const char *, int, const char *newpath, const char *); 294int assertion_make_symlink(const char *, int, const char *newpath, const char *); 295int assertion_non_empty_file(const char *, int, const char *); 296int assertion_set_nodump(const char *, int, const char *); 297int assertion_text_file_contents(const char *, int, const char *buff, const char *f); 298int assertion_umask(const char *, int, int); 299int assertion_utimes(const char *, int, const char *, long, long, long, long ); 300int assertion_version(const char*, int, const char *, const char *); 301 302void skipping_setup(const char *, int); 303void test_skipping(const char *fmt, ...); 304 305/* Like sprintf, then system() */ 306int systemf(const char * fmt, ...); 307 308/* Delay until time() returns a value after this. */ 309void sleepUntilAfter(time_t); 310 311/* Return true if this platform can create symlinks. */ 312int canSymlink(void); 313 314/* Return true if this platform can run the "bzip2" program. */ 315int canBzip2(void); 316 317/* Return true if this platform can run the "grzip" program. */ 318int canGrzip(void); 319 320/* Return true if this platform can run the "gzip" program. */ 321int canGzip(void); 322 323/* Return true if this platform can run the specified command. */ 324int canRunCommand(const char *); 325 326/* Return true if this platform can run the "lrzip" program. */ 327int canLrzip(void); 328 329/* Return true if this platform can run the "lz4" program. */ 330int canLz4(void); 331 332/* Return true if this platform can run the "zstd" program. */ 333int canZstd(void); 334 335/* Return true if this platform can run the "lzip" program. */ 336int canLzip(void); 337 338/* Return true if this platform can run the "lzma" program. */ 339int canLzma(void); 340 341/* Return true if this platform can run the "lzop" program. */ 342int canLzop(void); 343 344/* Return true if this platform can run the "xz" program. */ 345int canXz(void); 346 347/* Return true if this filesystem can handle nodump flags. */ 348int canNodump(void); 349 350/* Set test ACLs */ 351int setTestAcl(const char *path); 352 353/* Get extended attribute */ 354const void *getXattr(const char *, const char *, size_t *); 355 356/* Set extended attribute */ 357int setXattr(const char *, const char *, const void *, size_t); 358 359/* Return true if the file has large i-node number(>0xffffffff). */ 360int is_LargeInode(const char *); 361 362#if ARCHIVE_ACL_SUNOS 363/* Fetch ACLs on Solaris using acl() or facl() */ 364void *sunacl_get(int cmd, int *aclcnt, int fd, const char *path); 365#endif 366 367/* Suck file into string allocated via malloc(). Call free() when done. */ 368/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */ 369char *slurpfile(size_t *, const char *fmt, ...); 370 371/* Dump block of bytes to a file. */ 372void dumpfile(const char *filename, void *, size_t); 373 374/* Extracts named reference file to the current directory. */ 375void extract_reference_file(const char *); 376/* Copies named reference file to the current directory. */ 377void copy_reference_file(const char *); 378 379/* Extracts a list of files to the current directory. 380 * List must be NULL terminated. 381 */ 382void extract_reference_files(const char **); 383 384/* Subtract umask from mode */ 385mode_t umasked(mode_t expected_mode); 386 387/* Path to working directory for current test */ 388extern const char *testworkdir; 389 390#ifndef PROGRAM 391/* 392 * Special interfaces for libarchive test harness. 393 */ 394 395#include "archive.h" 396#include "archive_entry.h" 397 398/* ACL structure */ 399struct archive_test_acl_t { 400 int type; /* Type of ACL */ 401 int permset; /* Permissions for this class of users. */ 402 int tag; /* Owner, User, Owning group, group, other, etc. */ 403 int qual; /* GID or UID of user/group, depending on tag. */ 404 const char *name; /* Name of user/group, depending on tag. */ 405}; 406 407/* Set ACLs */ 408int assertion_entry_set_acls(const char *, int, struct archive_entry *, 409 struct archive_test_acl_t *, int); 410 411/* Compare ACLs */ 412int assertion_entry_compare_acls(const char *, int, struct archive_entry *, 413 struct archive_test_acl_t *, int, int, int); 414 415/* Special customized read-from-memory interface. */ 416int read_open_memory(struct archive *, const void *, size_t, size_t); 417/* _minimal version exercises a slightly different set of libarchive APIs. */ 418int read_open_memory_minimal(struct archive *, const void *, size_t, size_t); 419/* _seek version produces a seekable file. */ 420int read_open_memory_seek(struct archive *, const void *, size_t, size_t); 421 422/* Versions of above that accept an archive argument for additional info. */ 423#define assertA(e) assertion_assert(__FILE__, __LINE__, (e), #e, (a)) 424#define assertEqualIntA(a,v1,v2) \ 425 assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a)) 426#define assertEqualStringA(a,v1,v2) \ 427 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a), 0) 428 429#else /* defined(PROGRAM) */ 430/* 431 * Special interfaces for program test harness. 432 */ 433 434/* Pathname of exe to be tested. */ 435extern const char *testprogfile; 436/* Name of exe to use in printf-formatted command strings. */ 437/* On Windows, this includes leading/trailing quotes. */ 438extern const char *testprog; 439 440void assertVersion(const char *prog, const char *base); 441 442#endif /* defined(PROGRAM) */ 443 444#ifdef USE_DMALLOC 445#include <dmalloc.h> 446#endif 447 448#endif /* TEST_COMMON_H */ 449