1228753Smm/*
2228753Smm * Copyright (c) 2003-2006 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm *
25228763Smm * $FreeBSD: releng/10.2/contrib/libarchive/libarchive/test/test.h 306941 2016-10-10 07:18:54Z delphij $
26228753Smm */
27228753Smm
28228753Smm/* Every test program should #include "test.h" as the first thing. */
29228753Smm
30228753Smm/*
31228753Smm * The goal of this file (and the matching test.c) is to
32228753Smm * simplify the very repetitive test-*.c test programs.
33228753Smm */
34228753Smm#if defined(HAVE_CONFIG_H)
35228753Smm/* Most POSIX platforms use the 'configure' script to build config.h */
36228753Smm#include "config.h"
37228753Smm#elif defined(__FreeBSD__)
38228753Smm/* Building as part of FreeBSD system requires a pre-built config.h. */
39228753Smm#include "config_freebsd.h"
40228753Smm#elif defined(_WIN32) && !defined(__CYGWIN__)
41228753Smm/* Win32 can't run the 'configure' script. */
42228753Smm#include "config_windows.h"
43228753Smm#else
44228753Smm/* Warn if the library hasn't been (automatically or manually) configured. */
45228753Smm#error Oops: No config.h and no pre-built configuration in test.h.
46228753Smm#endif
47228753Smm
48228753Smm#include <sys/types.h>  /* Windows requires this before sys/stat.h */
49228753Smm#include <sys/stat.h>
50228753Smm
51228753Smm#if HAVE_DIRENT_H
52228753Smm#include <dirent.h>
53228753Smm#endif
54228753Smm#ifdef HAVE_DIRECT_H
55228753Smm#include <direct.h>
56228753Smm#define dirent direct
57228753Smm#endif
58228753Smm#include <errno.h>
59228753Smm#include <fcntl.h>
60228753Smm#ifdef HAVE_IO_H
61228753Smm#include <io.h>
62228753Smm#endif
63228753Smm#ifdef HAVE_STDINT_H
64228753Smm#include <stdint.h>
65228753Smm#endif
66228753Smm#include <stdio.h>
67228753Smm#include <stdlib.h>
68228753Smm#include <string.h>
69228753Smm#include <time.h>
70228753Smm#ifdef HAVE_UNISTD_H
71228753Smm#include <unistd.h>
72228753Smm#endif
73228753Smm#include <wchar.h>
74228753Smm#ifdef HAVE_WINDOWS_H
75228753Smm#include <windows.h>
76228753Smm#endif
77228753Smm
78228753Smm/*
79228753Smm * System-specific tweaks.  We really want to minimize these
80228753Smm * as much as possible, since they make it harder to understand
81228753Smm * the mainline code.
82228753Smm */
83228753Smm
84228753Smm/* Windows (including Visual Studio and MinGW but not Cygwin) */
85228753Smm#if defined(_WIN32) && !defined(__CYGWIN__)
86228753Smm#if !defined(__BORLANDC__)
87228753Smm#define strdup _strdup
88228753Smm#endif
89228753Smm#endif
90228753Smm
91228753Smm/* Visual Studio */
92228753Smm#ifdef _MSC_VER
93228753Smm#define snprintf	sprintf_s
94228753Smm#endif
95228753Smm
96228753Smm#if defined(__BORLANDC__)
97228753Smm#pragma warn -8068	/* Constant out of range in comparison. */
98228753Smm#endif
99228753Smm
100232153Smm/* Haiku OS and QNX */
101232153Smm#if defined(__HAIKU__) || defined(__QNXNTO__)
102232153Smm/* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */
103228753Smm#include <stdint.h>
104228753Smm#endif
105228753Smm
106228753Smm/* Get a real definition for __FBSDID if we can */
107228753Smm#if HAVE_SYS_CDEFS_H
108228753Smm#include <sys/cdefs.h>
109228753Smm#endif
110228753Smm
111228753Smm/* If not, define it so as to avoid dangling semicolons. */
112228753Smm#ifndef __FBSDID
113228753Smm#define	__FBSDID(a)     struct _undefined_hack
114228753Smm#endif
115228753Smm
116228753Smm#ifndef O_BINARY
117228753Smm#define	O_BINARY 0
118228753Smm#endif
119228753Smm
120228753Smm/*
121228753Smm * Redefine DEFINE_TEST for use in defining the test functions.
122228753Smm */
123228753Smm#undef DEFINE_TEST
124228753Smm#define DEFINE_TEST(name) void name(void); void name(void)
125228753Smm
126228753Smm/* An implementation of the standard assert() macro */
127228753Smm#define assert(e)   assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
128228753Smm/* chdir() and error if it fails */
129228753Smm#define assertChdir(path)  \
130228753Smm  assertion_chdir(__FILE__, __LINE__, path)
131228753Smm/* Assert two integers are the same.  Reports value of each one if not. */
132228753Smm#define assertEqualInt(v1,v2) \
133228753Smm  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
134228753Smm/* Assert two strings are the same.  Reports value of each one if not. */
135228753Smm#define assertEqualString(v1,v2)   \
136232153Smm  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0)
137232153Smm#define assertEqualUTF8String(v1,v2)   \
138232153Smm  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1)
139228753Smm/* As above, but v1 and v2 are wchar_t * */
140228753Smm#define assertEqualWString(v1,v2)   \
141228753Smm  assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
142228753Smm/* As above, but raw blocks of bytes. */
143228753Smm#define assertEqualMem(v1, v2, l)	\
144228753Smm  assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
145232153Smm/* Assert two files are the same. */
146232153Smm#define assertEqualFile(f1, f2)	\
147232153Smm  assertion_equal_file(__FILE__, __LINE__, (f1), (f2))
148232153Smm/* Assert that a file is empty. */
149232153Smm#define assertEmptyFile(pathname)	\
150232153Smm  assertion_empty_file(__FILE__, __LINE__, (pathname))
151232153Smm/* Assert that a file is not empty. */
152232153Smm#define assertNonEmptyFile(pathname)		\
153232153Smm  assertion_non_empty_file(__FILE__, __LINE__, (pathname))
154228753Smm#define assertFileAtime(pathname, sec, nsec)	\
155228753Smm  assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
156228753Smm#define assertFileAtimeRecent(pathname)	\
157228753Smm  assertion_file_atime_recent(__FILE__, __LINE__, pathname)
158228753Smm#define assertFileBirthtime(pathname, sec, nsec)	\
159228753Smm  assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
160228753Smm#define assertFileBirthtimeRecent(pathname) \
161228753Smm  assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
162228753Smm/* Assert that a file exists; supports printf-style arguments. */
163232153Smm#define assertFileExists(pathname) \
164232153Smm  assertion_file_exists(__FILE__, __LINE__, pathname)
165232153Smm/* Assert that a file exists. */
166232153Smm#define assertFileNotExists(pathname) \
167232153Smm  assertion_file_not_exists(__FILE__, __LINE__, pathname)
168232153Smm/* Assert that file contents match a string. */
169232153Smm#define assertFileContents(data, data_size, pathname) \
170232153Smm  assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname)
171228753Smm#define assertFileMtime(pathname, sec, nsec)	\
172228753Smm  assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
173228753Smm#define assertFileMtimeRecent(pathname) \
174228753Smm  assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
175228753Smm#define assertFileNLinks(pathname, nlinks)  \
176228753Smm  assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
177228753Smm#define assertFileSize(pathname, size)  \
178228753Smm  assertion_file_size(__FILE__, __LINE__, pathname, size)
179306941Sdelphij#define assertFileMode(pathname, mode)  \
180306941Sdelphij  assertion_file_mode(__FILE__, __LINE__, pathname, mode)
181232153Smm#define assertTextFileContents(text, pathname) \
182232153Smm  assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
183232153Smm#define assertFileContainsLinesAnyOrder(pathname, lines)	\
184232153Smm  assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)
185228753Smm#define assertIsDir(pathname, mode)		\
186228753Smm  assertion_is_dir(__FILE__, __LINE__, pathname, mode)
187228753Smm#define assertIsHardlink(path1, path2)	\
188228753Smm  assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
189228753Smm#define assertIsNotHardlink(path1, path2)	\
190228753Smm  assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
191228753Smm#define assertIsReg(pathname, mode)		\
192228753Smm  assertion_is_reg(__FILE__, __LINE__, pathname, mode)
193228753Smm#define assertIsSymlink(pathname, contents)	\
194228753Smm  assertion_is_symlink(__FILE__, __LINE__, pathname, contents)
195228753Smm/* Create a directory, report error if it fails. */
196228753Smm#define assertMakeDir(dirname, mode)	\
197228753Smm  assertion_make_dir(__FILE__, __LINE__, dirname, mode)
198228753Smm#define assertMakeFile(path, mode, contents) \
199238856Smm  assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
200238856Smm#define assertMakeBinFile(path, mode, csize, contents) \
201238856Smm  assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
202228753Smm#define assertMakeHardlink(newfile, oldfile)	\
203228753Smm  assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
204228753Smm#define assertMakeSymlink(newfile, linkto)	\
205228753Smm  assertion_make_symlink(__FILE__, __LINE__, newfile, linkto)
206238856Smm#define assertNodump(path)	\
207238856Smm  assertion_nodump(__FILE__, __LINE__, path)
208228753Smm#define assertUmask(mask)	\
209228753Smm  assertion_umask(__FILE__, __LINE__, mask)
210232153Smm#define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec)	\
211232153Smm  assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec)
212228753Smm
213228753Smm/*
214228753Smm * This would be simple with C99 variadic macros, but I don't want to
215228753Smm * require that.  Instead, I insert a function call before each
216228753Smm * skipping() call to pass the file and line information down.  Crude,
217228753Smm * but effective.
218228753Smm */
219228753Smm#define skipping	\
220232153Smm  skipping_setup(__FILE__, __LINE__);test_skipping
221228753Smm
222228753Smm/* Function declarations.  These are defined in test_utility.c. */
223228753Smmvoid failure(const char *fmt, ...);
224228753Smmint assertion_assert(const char *, int, int, const char *, void *);
225228753Smmint assertion_chdir(const char *, int, const char *);
226232153Smmint assertion_empty_file(const char *, int, const char *);
227232153Smmint assertion_equal_file(const char *, int, const char *, const char *);
228228753Smmint assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
229228753Smmint assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
230232153Smmint assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int);
231228753Smmint assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
232228753Smmint assertion_file_atime(const char *, int, const char *, long, long);
233228753Smmint assertion_file_atime_recent(const char *, int, const char *);
234228753Smmint assertion_file_birthtime(const char *, int, const char *, long, long);
235228753Smmint assertion_file_birthtime_recent(const char *, int, const char *);
236232153Smmint assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
237232153Smmint assertion_file_contents(const char *, int, const void *, int, const char *);
238232153Smmint assertion_file_exists(const char *, int, const char *);
239228753Smmint assertion_file_mtime(const char *, int, const char *, long, long);
240228753Smmint assertion_file_mtime_recent(const char *, int, const char *);
241228753Smmint assertion_file_nlinks(const char *, int, const char *, int);
242232153Smmint assertion_file_not_exists(const char *, int, const char *);
243228753Smmint assertion_file_size(const char *, int, const char *, long);
244306941Sdelphijint assertion_file_mode(const char *, int, const char *, int);
245228753Smmint assertion_is_dir(const char *, int, const char *, int);
246228753Smmint assertion_is_hardlink(const char *, int, const char *, const char *);
247228753Smmint assertion_is_not_hardlink(const char *, int, const char *, const char *);
248228753Smmint assertion_is_reg(const char *, int, const char *, int);
249228753Smmint assertion_is_symlink(const char *, int, const char *, const char *);
250228753Smmint assertion_make_dir(const char *, int, const char *, int);
251238856Smmint assertion_make_file(const char *, int, const char *, int, int, const void *);
252228753Smmint assertion_make_hardlink(const char *, int, const char *newpath, const char *);
253228753Smmint assertion_make_symlink(const char *, int, const char *newpath, const char *);
254238856Smmint assertion_nodump(const char *, int, const char *);
255232153Smmint assertion_non_empty_file(const char *, int, const char *);
256232153Smmint assertion_text_file_contents(const char *, int, const char *buff, const char *f);
257228753Smmint assertion_umask(const char *, int, int);
258232153Smmint assertion_utimes(const char *, int, const char *, long, long, long, long );
259228753Smm
260232153Smmvoid skipping_setup(const char *, int);
261228753Smmvoid test_skipping(const char *fmt, ...);
262228753Smm
263228753Smm/* Like sprintf, then system() */
264228753Smmint systemf(const char * fmt, ...);
265228753Smm
266228753Smm/* Delay until time() returns a value after this. */
267228753Smmvoid sleepUntilAfter(time_t);
268228753Smm
269228753Smm/* Return true if this platform can create symlinks. */
270228753Smmint canSymlink(void);
271228753Smm
272248616Smm/* Return true if this platform can run the "bzip2" program. */
273248616Smmint canBzip2(void);
274248616Smm
275248616Smm/* Return true if this platform can run the "grzip" program. */
276248616Smmint canGrzip(void);
277248616Smm
278228753Smm/* Return true if this platform can run the "gzip" program. */
279228753Smmint canGzip(void);
280228753Smm
281248616Smm/* Return true if this platform can run the specified command. */
282248616Smmint canRunCommand(const char *);
283228753Smm
284248616Smm/* Return true if this platform can run the "lrzip" program. */
285248616Smmint canLrzip(void);
286248616Smm
287248616Smm/* Return true if this platform can run the "lzip" program. */
288248616Smmint canLzip(void);
289248616Smm
290248616Smm/* Return true if this platform can run the "lzma" program. */
291248616Smmint canLzma(void);
292248616Smm
293248616Smm/* Return true if this platform can run the "lzop" program. */
294248616Smmint canLzop(void);
295248616Smm
296248616Smm/* Return true if this platform can run the "xz" program. */
297248616Smmint canXz(void);
298248616Smm
299238856Smm/* Return true if this filesystem can handle nodump flags. */
300238856Smmint canNodump(void);
301238856Smm
302232153Smm/* Return true if the file has large i-node number(>0xffffffff). */
303232153Smmint is_LargeInode(const char *);
304232153Smm
305228753Smm/* Suck file into string allocated via malloc(). Call free() when done. */
306228753Smm/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
307228753Smmchar *slurpfile(size_t *, const char *fmt, ...);
308228753Smm
309228753Smm/* Extracts named reference file to the current directory. */
310228753Smmvoid extract_reference_file(const char *);
311228753Smm
312248616Smm/* Extracts a list of files to the current directory.
313248616Smm * List must be NULL terminated.
314248616Smm */
315248616Smmvoid extract_reference_files(const char **);
316248616Smm
317232153Smm/* Path to working directory for current test */
318232153Smmconst char *testworkdir;
319232153Smm
320228753Smm/*
321228753Smm * Special interfaces for libarchive test harness.
322228753Smm */
323228753Smm
324228753Smm#include "archive.h"
325228753Smm#include "archive_entry.h"
326228753Smm
327228753Smm/* Special customized read-from-memory interface. */
328228753Smmint read_open_memory(struct archive *, void *, size_t, size_t);
329238856Smm/* _minimal version exercises a slightly different set of libarchive APIs. */
330238856Smmint read_open_memory_minimal(struct archive *, void *, size_t, size_t);
331232153Smm/* _seek version produces a seekable file. */
332232153Smmint read_open_memory_seek(struct archive *, void *, size_t, size_t);
333228753Smm
334228753Smm/* Versions of above that accept an archive argument for additional info. */
335228753Smm#define assertA(e)   assertion_assert(__FILE__, __LINE__, (e), #e, (a))
336228753Smm#define assertEqualIntA(a,v1,v2)   \
337228753Smm  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))
338228753Smm#define assertEqualStringA(a,v1,v2)   \
339232153Smm  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a), 0)
340228753Smm
341228753Smm#ifdef USE_DMALLOC
342228753Smm#include <dmalloc.h>
343228753Smm#endif
344