test.h revision 307798
1116525Sru/*
2116525Sru * Copyright (c) 2003-2006 Tim Kientzle
321495Sjmacd * All rights reserved.
4116525Sru *
5116525Sru * Redistribution and use in source and binary forms, with or without
6116525Sru * modification, are permitted provided that the following conditions
7116525Sru * are met:
821495Sjmacd * 1. Redistributions of source code must retain the above copyright
9116525Sru *    notice, this list of conditions and the following disclaimer.
1021495Sjmacd * 2. Redistributions in binary form must reproduce the above copyright
11116525Sru *    notice, this list of conditions and the following disclaimer in the
12116525Sru *    documentation and/or other materials provided with the distribution.
13116525Sru *
14116525Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15116525Sru * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16116525Sru * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17116525Sru * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18116525Sru * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19116525Sru * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20116525Sru * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21116525Sru * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22116525Sru * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23116525Sru * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24116525Sru *
25116525Sru * $FreeBSD: stable/10/contrib/libarchive/tar/test/test.h 307798 2016-10-22 21:41:28Z mm $
26116525Sru */
27116525Sru
28116525Sru/* Every test program should #include "test.h" as the first thing. */
29116525Sru
30116525Sru/*
31116525Sru * The goal of this file (and the matching test.c) is to
32116525Sru * simplify the very repetitive test-*.c test programs.
33116525Sru */
34116525Sru#if defined(HAVE_CONFIG_H)
35116525Sru/* Most POSIX platforms use the 'configure' script to build config.h */
36116525Sru#include "config.h"
37116525Sru#elif defined(__FreeBSD__)
38116525Sru/* Building as part of FreeBSD system requires a pre-built config.h. */
39116525Sru#include "config_freebsd.h"
40116525Sru#elif defined(_WIN32) && !defined(__CYGWIN__)
41116525Sru/* Win32 can't run the 'configure' script. */
42116525Sru#include "config_windows.h"
43116525Sru#else
44116525Sru/* Warn if the library hasn't been (automatically or manually) configured. */
45116525Sru#error Oops: No config.h and no pre-built configuration in test.h.
46116525Sru#endif
47116525Sru
48116525Sru#include <sys/types.h>  /* Windows requires this before sys/stat.h */
49116525Sru#include <sys/stat.h>
50116525Sru
51116525Sru#if HAVE_DIRENT_H
52116525Sru#include <dirent.h>
53116525Sru#endif
54116525Sru#ifdef HAVE_DIRECT_H
55116525Sru#include <direct.h>
56116525Sru#define dirent direct
57116525Sru#endif
58116525Sru#include <errno.h>
59116525Sru#include <fcntl.h>
60116525Sru#ifdef HAVE_IO_H
61116525Sru#include <io.h>
62116525Sru#endif
63116525Sru#ifdef HAVE_STDINT_H
64116525Sru#include <stdint.h>
65116525Sru#endif
66116525Sru#include <stdio.h>
67116525Sru#include <stdlib.h>
68116525Sru#include <string.h>
69116525Sru#include <ctype.h>
70116525Sru#include <time.h>
71116525Sru#ifdef HAVE_UNISTD_H
72116525Sru#include <unistd.h>
73116525Sru#endif
74116525Sru#include <wchar.h>
75116525Sru#ifdef HAVE_WINDOWS_H
76116525Sru#include <windows.h>
77116525Sru#endif
78116525Sru
79116525Sru/*
80116525Sru * System-specific tweaks.  We really want to minimize these
81116525Sru * as much as possible, since they make it harder to understand
82116525Sru * the mainline code.
83116525Sru */
84116525Sru
85116525Sru/* Windows (including Visual Studio and MinGW but not Cygwin) */
86116525Sru#if defined(_WIN32) && !defined(__CYGWIN__)
87116525Sru#if !defined(__BORLANDC__)
88116525Sru#undef chdir
89116525Sru#define chdir _chdir
90116525Sru#define strdup _strdup
91116525Sru#endif
92116525Sru#endif
93116525Sru
94116525Sru/* Visual Studio */
95116525Sru#if defined(_MSC_VER) && _MSC_VER < 1900
96116525Sru#define snprintf	sprintf_s
97116525Sru#endif
98116525Sru
99116525Sru#if defined(__BORLANDC__)
100116525Sru#pragma warn -8068	/* Constant out of range in comparison. */
101116525Sru#endif
102116525Sru
103116525Sru/* Haiku OS and QNX */
104116525Sru#if defined(__HAIKU__) || defined(__QNXNTO__)
105116525Sru/* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */
106116525Sru#include <stdint.h>
107116525Sru#endif
108116525Sru
109116525Sru/* Get a real definition for __FBSDID if we can */
110116525Sru#if HAVE_SYS_CDEFS_H
111116525Sru#include <sys/cdefs.h>
112116525Sru#endif
113116525Sru
114116525Sru/* If not, define it so as to avoid dangling semicolons. */
115116525Sru#ifndef __FBSDID
116116525Sru#define	__FBSDID(a)     struct _undefined_hack
117116525Sru#endif
118116525Sru
119116525Sru#ifndef O_BINARY
120116525Sru#define	O_BINARY 0
121116525Sru#endif
122116525Sru
123116525Sru/*
124116525Sru * Redefine DEFINE_TEST for use in defining the test functions.
125116525Sru */
126116525Sru#undef DEFINE_TEST
127116525Sru#define DEFINE_TEST(name) void name(void); void name(void)
128116525Sru
129116525Sru/* An implementation of the standard assert() macro */
130116525Sru#define assert(e)   assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
131116525Sru/* chdir() and error if it fails */
132116525Sru#define assertChdir(path)  \
133116525Sru  assertion_chdir(__FILE__, __LINE__, path)
134116525Sru/* Assert two integers are the same.  Reports value of each one if not. */
135116525Sru#define assertEqualInt(v1,v2) \
136116525Sru  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
137116525Sru/* Assert two strings are the same.  Reports value of each one if not. */
138116525Sru#define assertEqualString(v1,v2)   \
139116525Sru  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0)
140116525Sru#define assertEqualUTF8String(v1,v2)   \
141116525Sru  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1)
142116525Sru/* As above, but v1 and v2 are wchar_t * */
143116525Sru#define assertEqualWString(v1,v2)   \
144116525Sru  assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
145116525Sru/* As above, but raw blocks of bytes. */
146116525Sru#define assertEqualMem(v1, v2, l)	\
147116525Sru  assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
148116525Sru/* Assert that memory is full of a specified byte */
149116525Sru#define assertMemoryFilledWith(v1, l, b)					\
150116525Sru  assertion_memory_filled_with(__FILE__, __LINE__, (v1), #v1, (l), #l, (b), #b, NULL)
151116525Sru/* Assert two files are the same. */
152116525Sru#define assertEqualFile(f1, f2)	\
153116525Sru  assertion_equal_file(__FILE__, __LINE__, (f1), (f2))
154116525Sru/* Assert that a file is empty. */
155116525Sru#define assertEmptyFile(pathname)	\
156116525Sru  assertion_empty_file(__FILE__, __LINE__, (pathname))
157116525Sru/* Assert that a file is not empty. */
158116525Sru#define assertNonEmptyFile(pathname)		\
159116525Sru  assertion_non_empty_file(__FILE__, __LINE__, (pathname))
160116525Sru#define assertFileAtime(pathname, sec, nsec)	\
161116525Sru  assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
162116525Sru#define assertFileAtimeRecent(pathname)	\
163116525Sru  assertion_file_atime_recent(__FILE__, __LINE__, pathname)
164116525Sru#define assertFileBirthtime(pathname, sec, nsec)	\
165116525Sru  assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
166116525Sru#define assertFileBirthtimeRecent(pathname) \
167116525Sru  assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
168116525Sru/* Assert that a file exists; supports printf-style arguments. */
169116525Sru#define assertFileExists(pathname) \
170116525Sru  assertion_file_exists(__FILE__, __LINE__, pathname)
171116525Sru/* Assert that a file exists. */
172116525Sru#define assertFileNotExists(pathname) \
173116525Sru  assertion_file_not_exists(__FILE__, __LINE__, pathname)
174116525Sru/* Assert that file contents match a string. */
175116525Sru#define assertFileContents(data, data_size, pathname) \
176116525Sru  assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname)
177116525Sru/* Verify that a file does not contain invalid strings */
178116525Sru#define assertFileContainsNoInvalidStrings(pathname, strings) \
179116525Sru  assertion_file_contains_no_invalid_strings(__FILE__, __LINE__, pathname, strings)
180116525Sru#define assertFileMtime(pathname, sec, nsec)	\
181116525Sru  assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
182116525Sru#define assertFileMtimeRecent(pathname) \
183116525Sru  assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
184116525Sru#define assertFileNLinks(pathname, nlinks)  \
185116525Sru  assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
186116525Sru#define assertFileSize(pathname, size)  \
187116525Sru  assertion_file_size(__FILE__, __LINE__, pathname, size)
188116525Sru#define assertFileMode(pathname, mode)  \
189116525Sru  assertion_file_mode(__FILE__, __LINE__, pathname, mode)
190116525Sru#define assertTextFileContents(text, pathname) \
191116525Sru  assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
192116525Sru#define assertFileContainsLinesAnyOrder(pathname, lines)	\
193116525Sru  assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)
194116525Sru#define assertIsDir(pathname, mode)		\
195116525Sru  assertion_is_dir(__FILE__, __LINE__, pathname, mode)
196116525Sru#define assertIsHardlink(path1, path2)	\
197116525Sru  assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
198116525Sru#define assertIsNotHardlink(path1, path2)	\
199116525Sru  assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
200116525Sru#define assertIsReg(pathname, mode)		\
201116525Sru  assertion_is_reg(__FILE__, __LINE__, pathname, mode)
202116525Sru#define assertIsSymlink(pathname, contents)	\
203116525Sru  assertion_is_symlink(__FILE__, __LINE__, pathname, contents)
204116525Sru/* Create a directory, report error if it fails. */
205116525Sru#define assertMakeDir(dirname, mode)	\
206116525Sru  assertion_make_dir(__FILE__, __LINE__, dirname, mode)
207116525Sru#define assertMakeFile(path, mode, contents) \
208116525Sru  assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
209116525Sru#define assertMakeBinFile(path, mode, csize, contents) \
210116525Sru  assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
211116525Sru#define assertMakeHardlink(newfile, oldfile)	\
212116525Sru  assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
213116525Sru#define assertMakeSymlink(newfile, linkto)	\
214116525Sru  assertion_make_symlink(__FILE__, __LINE__, newfile, linkto)
215116525Sru#define assertNodump(path)      \
216116525Sru  assertion_nodump(__FILE__, __LINE__, path)
217116525Sru#define assertUmask(mask)	\
218116525Sru  assertion_umask(__FILE__, __LINE__, mask)
219116525Sru#define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec)	\
220116525Sru  assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec)
221116525Sru
222116525Sru/*
223116525Sru * This would be simple with C99 variadic macros, but I don't want to
224116525Sru * require that.  Instead, I insert a function call before each
225116525Sru * skipping() call to pass the file and line information down.  Crude,
226116525Sru * but effective.
227116525Sru */
228116525Sru#define skipping	\
229116525Sru  skipping_setup(__FILE__, __LINE__);test_skipping
230116525Sru
231116525Sru/* Function declarations.  These are defined in test_utility.c. */
232116525Sruvoid failure(const char *fmt, ...);
233116525Sruint assertion_assert(const char *, int, int, const char *, void *);
234116525Sruint assertion_chdir(const char *, int, const char *);
235116525Sruint assertion_empty_file(const char *, int, const char *);
236116525Sruint assertion_equal_file(const char *, int, const char *, const char *);
237116525Sruint assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
238116525Sruint assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
239116525Sruint assertion_memory_filled_with(const char *, int, const void *, const char *, size_t, const char *, char, const char *, void *);
240116525Sruint assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int);
241116525Sruint assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
242116525Sruint assertion_file_atime(const char *, int, const char *, long, long);
243116525Sruint assertion_file_atime_recent(const char *, int, const char *);
244116525Sruint assertion_file_birthtime(const char *, int, const char *, long, long);
245116525Sruint assertion_file_birthtime_recent(const char *, int, const char *);
246116525Sruint assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
247116525Sruint assertion_file_contains_no_invalid_strings(const char *, int, const char *, const char **);
248116525Sruint assertion_file_contents(const char *, int, const void *, int, const char *);
249116525Sruint assertion_file_exists(const char *, int, const char *);
250116525Sruint assertion_file_mode(const char *, int, const char *, int);
251116525Sruint assertion_file_mtime(const char *, int, const char *, long, long);
252116525Sruint assertion_file_mtime_recent(const char *, int, const char *);
253116525Sruint assertion_file_nlinks(const char *, int, const char *, int);
254116525Sruint assertion_file_not_exists(const char *, int, const char *);
255116525Sruint assertion_file_size(const char *, int, const char *, long);
256116525Sruint assertion_is_dir(const char *, int, const char *, int);
257116525Sruint assertion_is_hardlink(const char *, int, const char *, const char *);
258116525Sruint assertion_is_not_hardlink(const char *, int, const char *, const char *);
259116525Sruint assertion_is_reg(const char *, int, const char *, int);
260116525Sruint assertion_is_symlink(const char *, int, const char *, const char *);
261116525Sruint assertion_make_dir(const char *, int, const char *, int);
262116525Sruint assertion_make_file(const char *, int, const char *, int, int, const void *);
263116525Sruint assertion_make_hardlink(const char *, int, const char *newpath, const char *);
264116525Sruint assertion_make_symlink(const char *, int, const char *newpath, const char *);
265116525Sruint assertion_nodump(const char *, int, const char *);
266116525Sruint assertion_non_empty_file(const char *, int, const char *);
267116525Sruint assertion_text_file_contents(const char *, int, const char *buff, const char *f);
268116525Sruint assertion_umask(const char *, int, int);
269116525Sruint assertion_utimes(const char *, int, const char *, long, long, long, long );
270116525Sru
271116525Sruvoid skipping_setup(const char *, int);
272116525Sruvoid test_skipping(const char *fmt, ...);
273116525Sru
274116525Sru/* Like sprintf, then system() */
275116525Sruint systemf(const char * fmt, ...);
276116525Sru
277116525Sru/* Delay until time() returns a value after this. */
278116525Sruvoid sleepUntilAfter(time_t);
279116525Sru
280116525Sru/* Return true if this platform can create symlinks. */
281116525Sruint canSymlink(void);
282116525Sru
283116525Sru/* Return true if this platform can run the "bzip2" program. */
284116525Sruint canBzip2(void);
285116525Sru
286116525Sru/* Return true if this platform can run the "grzip" program. */
287116525Sruint canGrzip(void);
288116525Sru
289116525Sru/* Return true if this platform can run the "gzip" program. */
290116525Sruint canGzip(void);
291116525Sru
292116525Sru/* Return true if this platform can run the specified command. */
293116525Sruint canRunCommand(const char *);
294116525Sru
295116525Sru/* Return true if this platform can run the "lrzip" program. */
296116525Sruint canLrzip(void);
297116525Sru
298116525Sru/* Return true if this platform can run the "lz4" program. */
299116525Sruint canLz4(void);
300116525Sru
301116525Sru/* Return true if this platform can run the "lzip" program. */
302116525Sruint canLzip(void);
303116525Sru
304116525Sru/* Return true if this platform can run the "lzma" program. */
305116525Sruint canLzma(void);
306116525Sru
307116525Sru/* Return true if this platform can run the "lzop" program. */
308116525Sruint canLzop(void);
309116525Sru
310116525Sru/* Return true if this platform can run the "xz" program. */
311116525Sruint canXz(void);
312116525Sru
313116525Sru/* Return true if this filesystem can handle nodump flags. */
314116525Sruint canNodump(void);
315116525Sru
316116525Sru/* Return true if the file has large i-node number(>0xffffffff). */
317116525Sruint is_LargeInode(const char *);
318116525Sru
319116525Sru/* Suck file into string allocated via malloc(). Call free() when done. */
320116525Sru/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
321116525Sruchar *slurpfile(size_t *, const char *fmt, ...);
322116525Sru
323116525Sru/* Dump block of bytes to a file. */
324116525Sruvoid dumpfile(const char *filename, void *, size_t);
325116525Sru
326116525Sru/* Extracts named reference file to the current directory. */
327116525Sruvoid extract_reference_file(const char *);
328116525Sru/* Copies named reference file to the current directory. */
329116525Sruvoid copy_reference_file(const char *);
330116525Sru
331116525Sru/* Extracts a list of files to the current directory.
332116525Sru * List must be NULL terminated.
333116525Sru */
334116525Sruvoid extract_reference_files(const char **);
335116525Sru
336116525Sru/* Subtract umask from mode */
337116525Srumode_t umasked(mode_t expected_mode);
338116525Sru
339116525Sru/* Path to working directory for current test */
340116525Sruextern const char *testworkdir;
341
342/*
343 * Special interfaces for program test harness.
344 */
345
346/* Pathname of exe to be tested. */
347extern const char *testprogfile;
348/* Name of exe to use in printf-formatted command strings. */
349/* On Windows, this includes leading/trailing quotes. */
350extern const char *testprog;
351
352#ifdef USE_DMALLOC
353#include <dmalloc.h>
354#endif
355