Path: blob/main/contrib/libarchive/test_utils/test_common.h
104526 views
/*1* Copyright (c) 2003-2017 Tim Kientzle2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425#ifndef TEST_COMMON_H26#define TEST_COMMON_H2728/*29* The goal of this file (and the matching test.c) is to30* simplify the very repetitive test-*.c test programs.31*/32#if defined(HAVE_CONFIG_H)33/* Most POSIX platforms use the 'configure' script to build config.h */34#include "config.h"35#elif defined(__FreeBSD__)36/* Building as part of FreeBSD system requires a pre-built config.h. */37#include "config_freebsd.h"38#elif defined(__NetBSD__)39/* Building as part of NetBSD system requires a pre-built config.h. */40#include "config_netbsd.h"41#elif defined(_WIN32) && !defined(__CYGWIN__)42/* Win32 can't run the 'configure' script. */43#include "config_windows.h"44#else45/* 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#endif4849#include <sys/types.h> /* Windows requires this before sys/stat.h */50#if !HAVE_SUSECONDS_T51#define suseconds_t long52#endif53#include <sys/stat.h>5455#if HAVE_DIRENT_H56#include <dirent.h>57#endif58#ifdef HAVE_DIRECT_H59#include <direct.h>60#define dirent direct61#endif62#include <errno.h>63#include <fcntl.h>64#ifdef HAVE_IO_H65#include <io.h>66#endif67#ifdef HAVE_STDINT_H68#include <stdint.h>69#endif70#include <stdio.h>71#include <stdlib.h>72#include <string.h>73#include <ctype.h>74#include <time.h>75#ifdef HAVE_UNISTD_H76#include <unistd.h>77#endif78#include <wchar.h>79#ifdef HAVE_ACL_LIBACL_H80#include <acl/libacl.h>81#endif82#ifdef HAVE_SYS_ACL_H83#include <sys/acl.h>84#endif85#ifdef HAVE_SYS_RICHACL_H86#include <sys/richacl.h>87#endif88#ifdef HAVE_WINDOWS_H89#define NOCRYPT90#include <windows.h>91#include <winioctl.h>92#endif9394/*95* System-specific tweaks. We really want to minimize these96* as much as possible, since they make it harder to understand97* the mainline code.98*/99100/* Windows (including Visual Studio and MinGW but not Cygwin) */101#if defined(_WIN32) && !defined(__CYGWIN__)102#if !defined(__BORLANDC__)103#undef chdir104#define chdir _chdir105#define strdup _strdup106#endif107#endif108109/* Visual Studio */110#if defined(_MSC_VER) && _MSC_VER < 1900111#define snprintf sprintf_s112#endif113114#if defined(__BORLANDC__)115#pragma warn -8068 /* Constant out of range in comparison. */116#endif117118119#if defined(__GNUC__) && (__GNUC__ > 2 || \120(__GNUC__ == 2 && __GNUC_MINOR__ >= 7))121# ifdef __MINGW_PRINTF_FORMAT122# define __LA_PRINTF_FORMAT __MINGW_PRINTF_FORMAT123# else124# define __LA_PRINTF_FORMAT __printf__125# endif126# define __LA_PRINTFLIKE(f,a) __attribute__((__format__(__LA_PRINTF_FORMAT, f, a)))127#else128# define __LA_PRINTFLIKE(f,a)129#endif130131/* 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#endif136137#ifndef O_BINARY138#define O_BINARY 0139#endif140141#ifndef __LIBARCHIVE_TEST_COMMON142#define __LIBARCHIVE_TEST_COMMON143#endif144145#include "archive_platform_acl.h"146#define ARCHIVE_TEST_ACL_TYPE_POSIX1E 1147#define ARCHIVE_TEST_ACL_TYPE_NFS4 2148149#include "archive_platform_xattr.h"150151/*152* Redefine DEFINE_TEST for use in defining the test functions.153*/154#undef DEFINE_TEST155#define DEFINE_TEST(name) void name(void); void name(void)156157/* An implementation of the standard assert() macro */158#define assert(e) assertion_assert(__FILE__, __LINE__, (e), #e, NULL)159/* chdir() and error if it fails */160#define assertChdir(path) \161assertion_chdir(__FILE__, __LINE__, path)162/* change file/directory permissions and errors if it fails */163#define assertChmod(pathname, mode) \164assertion_chmod(__FILE__, __LINE__, pathname, mode)165/* Assert two files have the same file flags */166#define assertEqualFflags(patha, pathb) \167assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 0)168/* Assert two integers are the same. Reports value of each one if not. */169#define assertEqualInt(v1,v2) \170assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)171#define assertEqualAddress(v1,v2) \172assertion_equal_address(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)173/* Assert two strings are the same. Reports value of each one if not. */174#define assertEqualString(v1,v2) \175assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0)176#define assertEqualUTF8String(v1,v2) \177assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1)178/* As above, but v1 and v2 are wchar_t * */179#define assertEqualWString(v1,v2) \180assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)181/* As above, but raw blocks of bytes. */182#define assertEqualMem(v1, v2, l) \183assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)184/* Assert that memory is full of a specified byte */185#define assertMemoryFilledWith(v1, l, b) \186assertion_memory_filled_with(__FILE__, __LINE__, (v1), #v1, (l), #l, (b), #b, NULL)187/* Assert two files are the same. */188#define assertEqualFile(f1, f2) \189assertion_equal_file(__FILE__, __LINE__, (f1), (f2))190/* Assert that a file is empty. */191#define assertEmptyFile(pathname) \192assertion_empty_file(__FILE__, __LINE__, (pathname))193/* Assert that a file is not empty. */194#define assertNonEmptyFile(pathname) \195assertion_non_empty_file(__FILE__, __LINE__, (pathname))196#define assertFileAtime(pathname, sec, nsec) \197assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)198#define assertFileAtimeRecent(pathname) \199assertion_file_atime_recent(__FILE__, __LINE__, pathname)200#define assertFileBirthtime(pathname, sec, nsec) \201assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)202#define assertFileBirthtimeRecent(pathname) \203assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)204/* Assert that a file exists; supports printf-style arguments. */205#define assertFileExists(pathname) \206assertion_file_exists(__FILE__, __LINE__, pathname)207/* Assert that a file exists. */208#define assertFileNotExists(pathname) \209assertion_file_not_exists(__FILE__, __LINE__, pathname)210/* Assert that file contents match a string. */211#define assertFileContents(data, data_size, pathname) \212assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname)213/* Verify that a file does not contain invalid strings */214#define assertFileContainsNoInvalidStrings(pathname, strings) \215assertion_file_contains_no_invalid_strings(__FILE__, __LINE__, pathname, strings)216#define assertFileMtime(pathname, sec, nsec) \217assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)218#define assertFileMtimeRecent(pathname) \219assertion_file_mtime_recent(__FILE__, __LINE__, pathname)220#define assertFileNLinks(pathname, nlinks) \221assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)222#define assertFileSize(pathname, size) \223assertion_file_size(__FILE__, __LINE__, pathname, size)224#define assertFileMode(pathname, mode) \225assertion_file_mode(__FILE__, __LINE__, pathname, mode)226#define assertTextFileContents(text, pathname) \227assertion_text_file_contents(__FILE__, __LINE__, text, pathname)228#define assertFileContainsLinesAnyOrder(pathname, lines) \229assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)230#define assertIsDir(pathname, mode) \231assertion_is_dir(__FILE__, __LINE__, pathname, mode)232#define assertIsHardlink(path1, path2) \233assertion_is_hardlink(__FILE__, __LINE__, path1, path2)234#define assertIsNotHardlink(path1, path2) \235assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)236#define assertIsReg(pathname, mode) \237assertion_is_reg(__FILE__, __LINE__, pathname, mode)238#define assertIsSymlink(pathname, contents, isdir) \239assertion_is_symlink(__FILE__, __LINE__, pathname, contents, isdir)240/* Create a directory, report error if it fails. */241#define assertMakeDir(dirname, mode) \242assertion_make_dir(__FILE__, __LINE__, dirname, mode)243#define assertMakeFile(path, mode, contents) \244assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)245#define assertMakeBinFile(path, mode, csize, contents) \246assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)247#define assertMakeHardlink(newfile, oldfile) \248assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)249#define assertMakeSymlink(newfile, linkto, targetIsDir) \250assertion_make_symlink(__FILE__, __LINE__, newfile, linkto, targetIsDir)251#define assertSetNodump(path) \252assertion_set_nodump(__FILE__, __LINE__, path)253#define assertUmask(mask) \254assertion_umask(__FILE__, __LINE__, mask)255/* Assert that two files have unequal file flags */256#define assertUnequalFflags(patha, pathb) \257assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 1)258#define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec) \259assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec)260#ifndef PROGRAM261#define assertEntrySetAcls(entry, acls, count) \262assertion_entry_set_acls(__FILE__, __LINE__, entry, acls, count)263#define assertEntryCompareAcls(entry, acls, count, type, mode) \264assertion_entry_compare_acls(__FILE__, __LINE__, entry, acls, count, type, mode)265#endif266267/*268* This would be simple with C99 variadic macros, but I don't want to269* require that. Instead, I insert a function call before each270* skipping() call to pass the file and line information down. Crude,271* but effective.272*/273#define skipping \274skipping_setup(__FILE__, __LINE__);test_skipping275276/* Function declarations. These are defined in test_utility.c. */277void failure(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);278int assertion_assert(const char *, int, int, const char *, void *);279int assertion_chdir(const char *, int, const char *);280int assertion_chmod(const char *, int, const char *, int);281int assertion_compare_fflags(const char *, int, const char *, const char *,282int);283int assertion_empty_file(const char *, int, const char *);284int assertion_equal_file(const char *, int, const char *, const char *);285int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);286int assertion_equal_address(const char *, int, const void *, const char *, const void *, const char *, void *);287int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);288int assertion_memory_filled_with(const char *, int, const void *, const char *, size_t, const char *, char, const char *, void *);289int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int);290int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);291int assertion_file_atime(const char *, int, const char *, long, long);292int assertion_file_atime_recent(const char *, int, const char *);293int assertion_file_birthtime(const char *, int, const char *, long, long);294int assertion_file_birthtime_recent(const char *, int, const char *);295int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);296int assertion_file_contains_no_invalid_strings(const char *, int, const char *, const char **);297int assertion_file_contents(const char *, int, const void *, int, const char *);298int assertion_file_exists(const char *, int, const char *);299int assertion_file_mode(const char *, int, const char *, int);300int assertion_file_mtime(const char *, int, const char *, long, long);301int assertion_file_mtime_recent(const char *, int, const char *);302int assertion_file_nlinks(const char *, int, const char *, int);303int assertion_file_not_exists(const char *, int, const char *);304int assertion_file_size(const char *, int, const char *, long);305int assertion_is_dir(const char *, int, const char *, int);306int assertion_is_hardlink(const char *, int, const char *, const char *);307int assertion_is_not_hardlink(const char *, int, const char *, const char *);308int assertion_is_reg(const char *, int, const char *, int);309int assertion_is_symlink(const char *, int, const char *, const char *, int);310int assertion_make_dir(const char *, int, const char *, int);311int assertion_make_file(const char *, int, const char *, int, int, const void *);312int assertion_make_hardlink(const char *, int, const char *newpath, const char *);313int assertion_make_symlink(const char *, int, const char *newpath, const char *, int);314int assertion_non_empty_file(const char *, int, const char *);315int assertion_set_nodump(const char *, int, const char *);316int assertion_text_file_contents(const char *, int, const char *buff, const char *f);317int assertion_umask(const char *, int, int);318int assertion_utimes(const char *, int, const char *, time_t, suseconds_t, time_t, suseconds_t);319int assertion_version(const char*, int, const char *, const char *);320321void skipping_setup(const char *, int);322void test_skipping(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);323324/* Like sprintf, then system() */325int systemf(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);326327/* Delay until time() returns a value after this. */328void sleepUntilAfter(time_t);329330/* Return true if this platform can create symlinks. */331int canSymlink(void);332333/* Return true if this platform can run the "bzip2" program. */334int canBzip2(void);335336/* Return true if this platform can run the "grzip" program. */337int canGrzip(void);338339/* Return true if this platform can run the "gzip" program. */340int canGzip(void);341342/* Return true if this platform can run the specified command.343*344* Result can be optionally cached with `*tested`:345* - 0 if not tested yet346* - <0 if already tested negative347* - >0 if already tested positive348*/349int canRunCommand(const char *cmd, int *tested);350351/* Return true if this platform can run the "lrzip" program. */352int canLrzip(void);353354/* Return true if this platform can run the "lz4" program. */355int canLz4(void);356357/* Return true if this platform can run the "zstd" program. */358int canZstd(void);359360/* Return true if this platform can run the "lzip" program. */361int canLzip(void);362363/* Return true if this platform can run the "lzma" program. */364int canLzma(void);365366/* Return true if this platform can run the "lzop" program. */367int canLzop(void);368369/* Return true if this platform can run the "xz" program. */370int canXz(void);371372/* Return true if this filesystem can handle nodump flags. */373int canNodump(void);374375/* Set test ACLs */376int setTestAcl(const char *path);377378/* Get extended attribute */379void *getXattr(const char *, const char *, size_t *);380381/* Set extended attribute */382int setXattr(const char *, const char *, const void *, size_t);383384/* Return true if the file has large i-node number(>0xffffffff). */385int is_LargeInode(const char *);386387#if ARCHIVE_ACL_SUNOS388/* Fetch ACLs on Solaris using acl() or facl() */389void *sunacl_get(int cmd, int *aclcnt, int fd, const char *path);390#endif391392/* Suck file into string allocated via malloc(). Call free() when done. */393/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */394char *slurpfile(size_t *, const char *fmt, ...) __LA_PRINTFLIKE(2, 3);395396/* Dump block of bytes to a file. */397void dumpfile(const char *filename, void *, size_t);398399/* Extracts named reference file to the current directory. */400void extract_reference_file(const char *);401/* Copies named reference file to the current directory. */402void copy_reference_file(const char *);403404/* Extracts a list of files to the current directory.405* List must be NULL terminated.406*/407void extract_reference_files(const char **);408409/* Subtract umask from mode */410mode_t umasked(mode_t expected_mode);411412/* Path to working directory for current test */413extern const char *testworkdir;414415#ifndef PROGRAM416/*417* Special interfaces for libarchive test harness.418*/419420#include "archive.h"421#include "archive_entry.h"422423/* ACL structure */424struct archive_test_acl_t {425int type; /* Type of ACL */426int permset; /* Permissions for this class of users. */427int tag; /* Owner, User, Owning group, group, other, etc. */428int qual; /* GID or UID of user/group, depending on tag. */429const char *name; /* Name of user/group, depending on tag. */430};431432/* Set ACLs */433int assertion_entry_set_acls(const char *, int, struct archive_entry *,434struct archive_test_acl_t *, int);435436/* Compare ACLs */437int assertion_entry_compare_acls(const char *, int, struct archive_entry *,438struct archive_test_acl_t *, int, int, int);439440/* Special customized read-from-memory interface. */441int read_open_memory(struct archive *, const void *, size_t, size_t);442/* _minimal version exercises a slightly different set of libarchive APIs. */443int read_open_memory_minimal(struct archive *, const void *, size_t, size_t);444/* _seek version produces a seekable file. */445int read_open_memory_seek(struct archive *, const void *, size_t, size_t);446447/* Versions of above that accept an archive argument for additional info. */448#define assertA(e) assertion_assert(__FILE__, __LINE__, (e), #e, (a))449#define assertEqualIntA(a,v1,v2) \450assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))451#define assertEqualStringA(a,v1,v2) \452assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a), 0)453454#else /* defined(PROGRAM) */455/*456* Special interfaces for program test harness.457*/458459/* Pathname of exe to be tested. */460extern const char *testprogfile;461/* Name of exe to use in printf-formatted command strings. */462/* On Windows, this includes leading/trailing quotes. */463extern const char *testprog;464465void assertVersion(const char *prog, const char *base);466467#endif /* defined(PROGRAM) */468469#ifdef USE_DMALLOC470#include <dmalloc.h>471#endif472473#include "test_utils.h"474475#endif /* !TEST_COMMON_H */476477478