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