1200483Srwatson/*
2200483Srwatson * Copyright (c) 2003-2017 Tim Kientzle
3200483Srwatson * All rights reserved.
4200483Srwatson *
5200483Srwatson * Redistribution and use in source and binary forms, with or without
6200483Srwatson * modification, are permitted provided that the following conditions
7200483Srwatson * are met:
8200483Srwatson * 1. Redistributions of source code must retain the above copyright
9200483Srwatson *    notice, this list of conditions and the following disclaimer.
10200483Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11200483Srwatson *    notice, this list of conditions and the following disclaimer in the
12200483Srwatson *    documentation and/or other materials provided with the distribution.
13200483Srwatson *
14200483Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15200483Srwatson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16200483Srwatson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17200483Srwatson * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18200483Srwatson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19200483Srwatson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20200483Srwatson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21200483Srwatson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22200483Srwatson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23200483Srwatson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24200483Srwatson *
25200483Srwatson * $FreeBSD: stable/10/contrib/libarchive/test_utils/test_common.h 368708 2020-12-16 22:25:40Z mm $
26200483Srwatson */
27200483Srwatson
28200483Srwatson#ifndef	TEST_COMMON_H
29200483Srwatson#define	TEST_COMMON_H
30200483Srwatson
31200483Srwatson/*
32200483Srwatson * The goal of this file (and the matching test.c) is to
33200483Srwatson * simplify the very repetitive test-*.c test programs.
34200483Srwatson */
35200483Srwatson#if defined(HAVE_CONFIG_H)
36200483Srwatson/* Most POSIX platforms use the 'configure' script to build config.h */
37200483Srwatson#include "config.h"
38200483Srwatson#elif defined(__FreeBSD__)
39200483Srwatson/* Building as part of FreeBSD system requires a pre-built config.h. */
40200483Srwatson#include "config_freebsd.h"
41200483Srwatson#elif defined(__NetBSD__)
42200483Srwatson/* Building as part of NetBSD system requires a pre-built config.h. */
43200483Srwatson#include "config_netbsd.h"
44200483Srwatson#elif defined(_WIN32) && !defined(__CYGWIN__)
45200483Srwatson/* Win32 can't run the 'configure' script. */
46200483Srwatson#include "config_windows.h"
47200483Srwatson#else
48200483Srwatson/* Warn if the library hasn't been (automatically or manually) configured. */
49200483Srwatson#error Oops: No config.h and no pre-built configuration in test.h.
50200483Srwatson#endif
51200483Srwatson
52200483Srwatson#include <sys/types.h>  /* Windows requires this before sys/stat.h */
53200483Srwatson#include <sys/stat.h>
54200483Srwatson
55200483Srwatson#if HAVE_DIRENT_H
56200483Srwatson#include <dirent.h>
57200483Srwatson#endif
58200483Srwatson#ifdef HAVE_DIRECT_H
59200483Srwatson#include <direct.h>
60200483Srwatson#define dirent direct
61200483Srwatson#endif
62200483Srwatson#include <errno.h>
63200483Srwatson#include <fcntl.h>
64200483Srwatson#ifdef HAVE_IO_H
65200483Srwatson#include <io.h>
66200483Srwatson#endif
67200483Srwatson#ifdef HAVE_STDINT_H
68200483Srwatson#include <stdint.h>
69200483Srwatson#endif
70200483Srwatson#include <stdio.h>
71200483Srwatson#include <stdlib.h>
72200483Srwatson#include <string.h>
73200483Srwatson#include <ctype.h>
74200483Srwatson#include <time.h>
75200483Srwatson#ifdef HAVE_UNISTD_H
76200483Srwatson#include <unistd.h>
77200483Srwatson#endif
78200483Srwatson#include <wchar.h>
79200483Srwatson#ifdef HAVE_ACL_LIBACL_H
80200483Srwatson#include <acl/libacl.h>
81200483Srwatson#endif
82200483Srwatson#ifdef HAVE_SYS_ACL_H
83200483Srwatson#include <sys/acl.h>
84200483Srwatson#endif
85200483Srwatson#ifdef HAVE_SYS_RICHACL_H
86200483Srwatson#include <sys/richacl.h>
87200483Srwatson#endif
88200483Srwatson#ifdef HAVE_WINDOWS_H
89200483Srwatson#define NOCRYPT
90200483Srwatson#include <windows.h>
91200483Srwatson#include <winioctl.h>
92200483Srwatson#endif
93200483Srwatson
94200483Srwatson/*
95200483Srwatson * System-specific tweaks.  We really want to minimize these
96200483Srwatson * as much as possible, since they make it harder to understand
97200483Srwatson * the mainline code.
98200483Srwatson */
99200483Srwatson
100200483Srwatson/* Windows (including Visual Studio and MinGW but not Cygwin) */
101200483Srwatson#if defined(_WIN32) && !defined(__CYGWIN__)
102200483Srwatson#if !defined(__BORLANDC__)
103200483Srwatson#undef chdir
104200483Srwatson#define chdir _chdir
105200483Srwatson#define strdup _strdup
106200483Srwatson#endif
107200483Srwatson#endif
108200483Srwatson
109200483Srwatson/* Visual Studio */
110200483Srwatson#if defined(_MSC_VER) && _MSC_VER < 1900
111200483Srwatson#define snprintf	sprintf_s
112200483Srwatson#endif
113200483Srwatson
114200483Srwatson#if defined(__BORLANDC__)
115200483Srwatson#pragma warn -8068	/* Constant out of range in comparison. */
116200483Srwatson#endif
117200483Srwatson
118200483Srwatson
119200483Srwatson#if defined(__GNUC__) && (__GNUC__ > 2 || \
120200483Srwatson			  (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
121200483Srwatson# ifdef __MINGW_PRINTF_FORMAT
122200483Srwatson#  define __LA_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
123200483Srwatson# else
124200483Srwatson#  define __LA_PRINTF_FORMAT __printf__
125200483Srwatson# endif
126200483Srwatson# define __LA_PRINTFLIKE(f,a)	__attribute__((__format__(__LA_PRINTF_FORMAT, f, a)))
127200483Srwatson#else
128200483Srwatson# define __LA_PRINTFLIKE(f,a)
129200483Srwatson#endif
130200483Srwatson
131200483Srwatson/* Haiku OS and QNX */
132200483Srwatson#if defined(__HAIKU__) || defined(__QNXNTO__)
133200483Srwatson/* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */
134200483Srwatson#include <stdint.h>
135200483Srwatson#endif
136200483Srwatson
137200483Srwatson/* Get a real definition for __FBSDID if we can */
138200483Srwatson#if HAVE_SYS_CDEFS_H
139200483Srwatson#include <sys/cdefs.h>
140200483Srwatson#endif
141200483Srwatson
142200483Srwatson/* If not, define it so as to avoid dangling semicolons. */
143200483Srwatson#ifndef __FBSDID
144200483Srwatson#define	__FBSDID(a)     struct _undefined_hack
145200483Srwatson#endif
146200483Srwatson
147200483Srwatson#ifndef O_BINARY
148200483Srwatson#define	O_BINARY 0
149200483Srwatson#endif
150200483Srwatson
151200483Srwatson#ifndef __LIBARCHIVE_TEST_COMMON
152200483Srwatson#define __LIBARCHIVE_TEST_COMMON
153200483Srwatson#endif
154200483Srwatson
155200483Srwatson#include "archive_platform_acl.h"
156200483Srwatson#define	ARCHIVE_TEST_ACL_TYPE_POSIX1E	1
157200483Srwatson#define	ARCHIVE_TEST_ACL_TYPE_NFS4	2
158200483Srwatson
159200483Srwatson#include "archive_platform_xattr.h"
160200483Srwatson
161200483Srwatson/*
162200483Srwatson * Redefine DEFINE_TEST for use in defining the test functions.
163200483Srwatson */
164200483Srwatson#undef DEFINE_TEST
165200483Srwatson#define DEFINE_TEST(name) void name(void); void name(void)
166200483Srwatson
167200483Srwatson/* An implementation of the standard assert() macro */
168200483Srwatson#define assert(e)   assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
169200483Srwatson/* chdir() and error if it fails */
170200483Srwatson#define assertChdir(path)  \
171200483Srwatson  assertion_chdir(__FILE__, __LINE__, path)
172200483Srwatson/* change file/directory permissions and errors if it fails */
173200483Srwatson#define assertChmod(pathname, mode)				\
174200483Srwatson  assertion_chmod(__FILE__, __LINE__, pathname, mode)
175200483Srwatson/* Assert two files have the same file flags */
176200483Srwatson#define assertEqualFflags(patha, pathb)	\
177200483Srwatson  assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 0)
178200483Srwatson/* Assert two integers are the same.  Reports value of each one if not. */
179#define assertEqualInt(v1,v2) \
180  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
181/* Assert two strings are the same.  Reports value of each one if not. */
182#define assertEqualString(v1,v2)   \
183  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0)
184#define assertEqualUTF8String(v1,v2)   \
185  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1)
186/* As above, but v1 and v2 are wchar_t * */
187#define assertEqualWString(v1,v2)   \
188  assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
189/* As above, but raw blocks of bytes. */
190#define assertEqualMem(v1, v2, l)	\
191  assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
192/* Assert that memory is full of a specified byte */
193#define assertMemoryFilledWith(v1, l, b)					\
194  assertion_memory_filled_with(__FILE__, __LINE__, (v1), #v1, (l), #l, (b), #b, NULL)
195/* Assert two files are the same. */
196#define assertEqualFile(f1, f2)	\
197  assertion_equal_file(__FILE__, __LINE__, (f1), (f2))
198/* Assert that a file is empty. */
199#define assertEmptyFile(pathname)	\
200  assertion_empty_file(__FILE__, __LINE__, (pathname))
201/* Assert that a file is not empty. */
202#define assertNonEmptyFile(pathname)		\
203  assertion_non_empty_file(__FILE__, __LINE__, (pathname))
204#define assertFileAtime(pathname, sec, nsec)	\
205  assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
206#define assertFileAtimeRecent(pathname)	\
207  assertion_file_atime_recent(__FILE__, __LINE__, pathname)
208#define assertFileBirthtime(pathname, sec, nsec)	\
209  assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
210#define assertFileBirthtimeRecent(pathname) \
211  assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
212/* Assert that a file exists; supports printf-style arguments. */
213#define assertFileExists(pathname) \
214  assertion_file_exists(__FILE__, __LINE__, pathname)
215/* Assert that a file exists. */
216#define assertFileNotExists(pathname) \
217  assertion_file_not_exists(__FILE__, __LINE__, pathname)
218/* Assert that file contents match a string. */
219#define assertFileContents(data, data_size, pathname) \
220  assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname)
221/* Verify that a file does not contain invalid strings */
222#define assertFileContainsNoInvalidStrings(pathname, strings) \
223  assertion_file_contains_no_invalid_strings(__FILE__, __LINE__, pathname, strings)
224#define assertFileMtime(pathname, sec, nsec)	\
225  assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
226#define assertFileMtimeRecent(pathname) \
227  assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
228#define assertFileNLinks(pathname, nlinks)  \
229  assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
230#define assertFileSize(pathname, size)  \
231  assertion_file_size(__FILE__, __LINE__, pathname, size)
232#define assertFileMode(pathname, mode)  \
233  assertion_file_mode(__FILE__, __LINE__, pathname, mode)
234#define assertTextFileContents(text, pathname) \
235  assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
236#define assertFileContainsLinesAnyOrder(pathname, lines)	\
237  assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)
238#define assertIsDir(pathname, mode)		\
239  assertion_is_dir(__FILE__, __LINE__, pathname, mode)
240#define assertIsHardlink(path1, path2)	\
241  assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
242#define assertIsNotHardlink(path1, path2)	\
243  assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
244#define assertIsReg(pathname, mode)		\
245  assertion_is_reg(__FILE__, __LINE__, pathname, mode)
246#define assertIsSymlink(pathname, contents, isdir)	\
247  assertion_is_symlink(__FILE__, __LINE__, pathname, contents, isdir)
248/* Create a directory, report error if it fails. */
249#define assertMakeDir(dirname, mode)	\
250  assertion_make_dir(__FILE__, __LINE__, dirname, mode)
251#define assertMakeFile(path, mode, contents) \
252  assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
253#define assertMakeBinFile(path, mode, csize, contents) \
254  assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
255#define assertMakeHardlink(newfile, oldfile)	\
256  assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
257#define assertMakeSymlink(newfile, linkto, targetIsDir)	\
258  assertion_make_symlink(__FILE__, __LINE__, newfile, linkto, targetIsDir)
259#define assertSetNodump(path)	\
260  assertion_set_nodump(__FILE__, __LINE__, path)
261#define assertUmask(mask)	\
262  assertion_umask(__FILE__, __LINE__, mask)
263/* Assert that two files have unequal file flags */
264#define assertUnequalFflags(patha, pathb)	\
265  assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 1)
266#define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec)	\
267  assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec)
268#ifndef PROGRAM
269#define assertEntrySetAcls(entry, acls, count) \
270  assertion_entry_set_acls(__FILE__, __LINE__, entry, acls, count)
271#define assertEntryCompareAcls(entry, acls, count, type, mode) \
272  assertion_entry_compare_acls(__FILE__, __LINE__, entry, acls, count, type, mode)
273#endif
274
275/*
276 * This would be simple with C99 variadic macros, but I don't want to
277 * require that.  Instead, I insert a function call before each
278 * skipping() call to pass the file and line information down.  Crude,
279 * but effective.
280 */
281#define skipping	\
282  skipping_setup(__FILE__, __LINE__);test_skipping
283
284/* Function declarations.  These are defined in test_utility.c. */
285void failure(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);
286int assertion_assert(const char *, int, int, const char *, void *);
287int assertion_chdir(const char *, int, const char *);
288int assertion_chmod(const char *, int, const char *, int);
289int assertion_compare_fflags(const char *, int, const char *, const char *,
290    int);
291int assertion_empty_file(const char *, int, const char *);
292int assertion_equal_file(const char *, int, const char *, const char *);
293int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
294int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
295int assertion_memory_filled_with(const char *, int, const void *, const char *, size_t, const char *, char, const char *, void *);
296int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int);
297int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
298int assertion_file_atime(const char *, int, const char *, long, long);
299int assertion_file_atime_recent(const char *, int, const char *);
300int assertion_file_birthtime(const char *, int, const char *, long, long);
301int assertion_file_birthtime_recent(const char *, int, const char *);
302int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
303int assertion_file_contains_no_invalid_strings(const char *, int, const char *, const char **);
304int assertion_file_contents(const char *, int, const void *, int, const char *);
305int assertion_file_exists(const char *, int, const char *);
306int assertion_file_mode(const char *, int, const char *, int);
307int assertion_file_mtime(const char *, int, const char *, long, long);
308int assertion_file_mtime_recent(const char *, int, const char *);
309int assertion_file_nlinks(const char *, int, const char *, int);
310int assertion_file_not_exists(const char *, int, const char *);
311int assertion_file_size(const char *, int, const char *, long);
312int assertion_is_dir(const char *, int, const char *, int);
313int assertion_is_hardlink(const char *, int, const char *, const char *);
314int assertion_is_not_hardlink(const char *, int, const char *, const char *);
315int assertion_is_reg(const char *, int, const char *, int);
316int assertion_is_symlink(const char *, int, const char *, const char *, int);
317int assertion_make_dir(const char *, int, const char *, int);
318int assertion_make_file(const char *, int, const char *, int, int, const void *);
319int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
320int assertion_make_symlink(const char *, int, const char *newpath, const char *, int);
321int assertion_non_empty_file(const char *, int, const char *);
322int assertion_set_nodump(const char *, int, const char *);
323int assertion_text_file_contents(const char *, int, const char *buff, const char *f);
324int assertion_umask(const char *, int, int);
325int assertion_utimes(const char *, int, const char *, long, long, long, long );
326int assertion_version(const char*, int, const char *, const char *);
327
328void skipping_setup(const char *, int);
329void test_skipping(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);
330
331/* Like sprintf, then system() */
332int systemf(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);
333
334/* Delay until time() returns a value after this. */
335void sleepUntilAfter(time_t);
336
337/* Return true if this platform can create symlinks. */
338int canSymlink(void);
339
340/* Return true if this platform can run the "bzip2" program. */
341int canBzip2(void);
342
343/* Return true if this platform can run the "grzip" program. */
344int canGrzip(void);
345
346/* Return true if this platform can run the "gzip" program. */
347int canGzip(void);
348
349/* Return true if this platform can run the specified command. */
350int canRunCommand(const char *);
351
352/* Return true if this platform can run the "lrzip" program. */
353int canLrzip(void);
354
355/* Return true if this platform can run the "lz4" program. */
356int canLz4(void);
357
358/* Return true if this platform can run the "zstd" program. */
359int canZstd(void);
360
361/* Return true if this platform can run the "lzip" program. */
362int canLzip(void);
363
364/* Return true if this platform can run the "lzma" program. */
365int canLzma(void);
366
367/* Return true if this platform can run the "lzop" program. */
368int canLzop(void);
369
370/* Return true if this platform can run the "xz" program. */
371int canXz(void);
372
373/* Return true if this filesystem can handle nodump flags. */
374int canNodump(void);
375
376/* Set test ACLs */
377int setTestAcl(const char *path);
378
379/* Get extended attribute */
380void *getXattr(const char *, const char *, size_t *);
381
382/* Set extended attribute */
383int setXattr(const char *, const char *, const void *, size_t);
384
385/* Return true if the file has large i-node number(>0xffffffff). */
386int is_LargeInode(const char *);
387
388#if ARCHIVE_ACL_SUNOS
389/* Fetch ACLs on Solaris using acl() or facl() */
390void *sunacl_get(int cmd, int *aclcnt, int fd, const char *path);
391#endif
392
393/* Suck file into string allocated via malloc(). Call free() when done. */
394/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
395char *slurpfile(size_t *, const char *fmt, ...) __LA_PRINTFLIKE(2, 3);
396
397/* Dump block of bytes to a file. */
398void dumpfile(const char *filename, void *, size_t);
399
400/* Extracts named reference file to the current directory. */
401void extract_reference_file(const char *);
402/* Copies named reference file to the current directory. */
403void copy_reference_file(const char *);
404
405/* Extracts a list of files to the current directory.
406 * List must be NULL terminated.
407 */
408void extract_reference_files(const char **);
409
410/* Subtract umask from mode */
411mode_t umasked(mode_t expected_mode);
412
413/* Path to working directory for current test */
414extern const char *testworkdir;
415
416#ifndef PROGRAM
417/*
418 * Special interfaces for libarchive test harness.
419 */
420
421#include "archive.h"
422#include "archive_entry.h"
423
424/* ACL structure */
425struct archive_test_acl_t {
426	int type;  /* Type of ACL */
427	int permset; /* Permissions for this class of users. */
428	int tag; /* Owner, User, Owning group, group, other, etc. */
429	int qual; /* GID or UID of user/group, depending on tag. */
430	const char *name; /* Name of user/group, depending on tag. */
431};
432
433/* Set ACLs */
434int assertion_entry_set_acls(const char *, int, struct archive_entry *,
435    struct archive_test_acl_t *, int);
436
437/* Compare ACLs */
438int assertion_entry_compare_acls(const char *, int, struct archive_entry *,
439    struct archive_test_acl_t *, int, int, int);
440
441/* Special customized read-from-memory interface. */
442int read_open_memory(struct archive *, const void *, size_t, size_t);
443/* _minimal version exercises a slightly different set of libarchive APIs. */
444int read_open_memory_minimal(struct archive *, const void *, size_t, size_t);
445/* _seek version produces a seekable file. */
446int read_open_memory_seek(struct archive *, const void *, size_t, size_t);
447
448/* Versions of above that accept an archive argument for additional info. */
449#define assertA(e)   assertion_assert(__FILE__, __LINE__, (e), #e, (a))
450#define assertEqualIntA(a,v1,v2)   \
451  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))
452#define assertEqualStringA(a,v1,v2)   \
453  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a), 0)
454
455#else	/* defined(PROGRAM) */
456/*
457 * Special interfaces for program test harness.
458 */
459
460/* Pathname of exe to be tested. */
461extern const char *testprogfile;
462/* Name of exe to use in printf-formatted command strings. */
463/* On Windows, this includes leading/trailing quotes. */
464extern const char *testprog;
465
466void assertVersion(const char *prog, const char *base);
467
468#endif	/* defined(PROGRAM) */
469
470#ifdef USE_DMALLOC
471#include <dmalloc.h>
472#endif
473
474#endif	/* TEST_COMMON_H */
475