test.h revision 228763
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: head/contrib/libarchive/tar/test/test.h 228763 2011-12-21 11:13:29Z mm $
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#ifdef USE_DMALLOC
52228753Smm#include <dmalloc.h>
53228753Smm#endif
54228753Smm#if HAVE_DIRENT_H
55228753Smm#include <dirent.h>
56228753Smm#endif
57228753Smm#ifdef HAVE_DIRECT_H
58228753Smm#include <direct.h>
59228753Smm#define dirent direct
60228753Smm#endif
61228753Smm#include <errno.h>
62228753Smm#include <fcntl.h>
63228753Smm#ifdef HAVE_IO_H
64228753Smm#include <io.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#include "../bsdtar_windows.h"
87228753Smm#if !defined(__BORLANDC__)
88228753Smm#define strdup _strdup
89228753Smm#endif
90228753Smm#define LOCALE_DE	"deu"
91228753Smm#else
92228753Smm#define LOCALE_DE	"de_DE.UTF-8"
93228753Smm#endif
94228753Smm
95228753Smm/* Visual Studio */
96228753Smm#ifdef _MSC_VER
97228753Smm#define snprintf	sprintf_s
98228753Smm#endif
99228753Smm
100228753Smm/* Cygwin */
101228753Smm#if defined(__CYGWIN__)
102228753Smm/* Cygwin-1.7.x is lazy about populating nlinks, so don't
103228753Smm * expect it to be accurate. */
104228753Smm# define NLINKS_INACCURATE_FOR_DIRS
105228753Smm#endif
106228753Smm
107228753Smm#if defined(__HAIKU__) || defined(__QNXNTO__)
108228753Smm/* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */
109228753Smm#include <stdint.h>
110228753Smm#endif
111228753Smm
112228753Smm/* Get a real definition for __FBSDID if we can */
113228753Smm#if HAVE_SYS_CDEFS_H
114228753Smm#include <sys/cdefs.h>
115228753Smm#endif
116228753Smm
117228753Smm/* If not, define it so as to avoid dangling semicolons. */
118228753Smm#ifndef __FBSDID
119228753Smm#define	__FBSDID(a)     struct _undefined_hack
120228753Smm#endif
121228753Smm
122228753Smm#ifndef O_BINARY
123228753Smm#define	O_BINARY 0
124228753Smm#endif
125228753Smm
126228753Smm/*
127228753Smm * Redefine DEFINE_TEST for use in defining the test functions.
128228753Smm */
129228753Smm#undef DEFINE_TEST
130228753Smm#define DEFINE_TEST(name) void name(void); void name(void)
131228753Smm
132228753Smm/* An implementation of the standard assert() macro */
133228753Smm#define assert(e)   assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
134228753Smm/* chdir() and error if it fails */
135228753Smm#define assertChdir(path)  \
136228753Smm  assertion_chdir(__FILE__, __LINE__, path)
137228753Smm/* Assert two integers are the same.  Reports value of each one if not. */
138228753Smm#define assertEqualInt(v1,v2) \
139228753Smm  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
140228753Smm/* Assert two strings are the same.  Reports value of each one if not. */
141228753Smm#define assertEqualString(v1,v2)   \
142228753Smm  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
143228753Smm/* As above, but v1 and v2 are wchar_t * */
144228753Smm#define assertEqualWString(v1,v2)   \
145228753Smm  assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
146228753Smm/* As above, but raw blocks of bytes. */
147228753Smm#define assertEqualMem(v1, v2, l)	\
148228753Smm  assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
149228753Smm/* Assert two files are the same; allow printf-style expansion of second name.
150228753Smm * See below for comments about variable arguments here...
151228753Smm */
152228753Smm#define assertEqualFile		\
153228753Smm  assertion_setup(__FILE__, __LINE__);assertion_equal_file
154228753Smm/* Assert that a file is empty; supports printf-style arguments. */
155228753Smm#define assertEmptyFile		\
156228753Smm  assertion_setup(__FILE__, __LINE__);assertion_empty_file
157228753Smm/* Assert that a file is not empty; supports printf-style arguments. */
158228753Smm#define assertNonEmptyFile		\
159228753Smm  assertion_setup(__FILE__, __LINE__);assertion_non_empty_file
160228753Smm#define assertFileAtime(pathname, sec, nsec)	\
161228753Smm  assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
162228753Smm#define assertFileAtimeRecent(pathname)	\
163228753Smm  assertion_file_atime_recent(__FILE__, __LINE__, pathname)
164228753Smm#define assertFileBirthtime(pathname, sec, nsec)	\
165228753Smm  assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
166228753Smm#define assertFileBirthtimeRecent(pathname) \
167228753Smm  assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
168228753Smm/* Assert that a file exists; supports printf-style arguments. */
169228753Smm#define assertFileExists		\
170228753Smm  assertion_setup(__FILE__, __LINE__);assertion_file_exists
171228753Smm/* Assert that a file exists; supports printf-style arguments. */
172228753Smm#define assertFileNotExists		\
173228753Smm  assertion_setup(__FILE__, __LINE__);assertion_file_not_exists
174228753Smm/* Assert that file contents match a string; supports printf-style arguments. */
175228753Smm#define assertFileContents             \
176228753Smm  assertion_setup(__FILE__, __LINE__);assertion_file_contents
177228753Smm#define assertFileMtime(pathname, sec, nsec)	\
178228753Smm  assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
179228753Smm#define assertFileMtimeRecent(pathname) \
180228753Smm  assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
181228753Smm#define assertFileNLinks(pathname, nlinks)  \
182228753Smm  assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
183228753Smm#define assertFileSize(pathname, size)  \
184228753Smm  assertion_file_size(__FILE__, __LINE__, pathname, size)
185228753Smm#define assertTextFileContents         \
186228753Smm  assertion_setup(__FILE__, __LINE__);assertion_text_file_contents
187228753Smm#define assertFileContainsLinesAnyOrder(pathname, lines)	\
188228753Smm	assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)
189228753Smm#define assertIsDir(pathname, mode)		\
190228753Smm  assertion_is_dir(__FILE__, __LINE__, pathname, mode)
191228753Smm#define assertIsHardlink(path1, path2)	\
192228753Smm  assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
193228753Smm#define assertIsNotHardlink(path1, path2)	\
194228753Smm  assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
195228753Smm#define assertIsReg(pathname, mode)		\
196228753Smm  assertion_is_reg(__FILE__, __LINE__, pathname, mode)
197228753Smm#define assertIsSymlink(pathname, contents)	\
198228753Smm  assertion_is_symlink(__FILE__, __LINE__, pathname, contents)
199228753Smm/* Create a directory, report error if it fails. */
200228753Smm#define assertMakeDir(dirname, mode)	\
201228753Smm  assertion_make_dir(__FILE__, __LINE__, dirname, mode)
202228753Smm#define assertMakeFile(path, mode, contents) \
203228753Smm  assertion_make_file(__FILE__, __LINE__, path, mode, contents)
204228753Smm#define assertMakeHardlink(newfile, oldfile)	\
205228753Smm  assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
206228753Smm#define assertMakeSymlink(newfile, linkto)	\
207228753Smm  assertion_make_symlink(__FILE__, __LINE__, newfile, linkto)
208228753Smm#define assertUmask(mask)	\
209228753Smm  assertion_umask(__FILE__, __LINE__, mask)
210228753Smm
211228753Smm/*
212228753Smm * This would be simple with C99 variadic macros, but I don't want to
213228753Smm * require that.  Instead, I insert a function call before each
214228753Smm * skipping() call to pass the file and line information down.  Crude,
215228753Smm * but effective.
216228753Smm */
217228753Smm#define skipping	\
218228753Smm  assertion_setup(__FILE__, __LINE__);test_skipping
219228753Smm
220228753Smm/* Function declarations.  These are defined in test_utility.c. */
221228753Smmvoid failure(const char *fmt, ...);
222228753Smmint assertion_assert(const char *, int, int, const char *, void *);
223228753Smmint assertion_chdir(const char *, int, const char *);
224228753Smmint assertion_empty_file(const char *, ...);
225228753Smmint assertion_equal_file(const char *, const char *, ...);
226228753Smmint assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
227228753Smmint assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
228228753Smmint assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
229228753Smmint assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
230228753Smmint assertion_file_atime(const char *, int, const char *, long, long);
231228753Smmint assertion_file_atime_recent(const char *, int, const char *);
232228753Smmint assertion_file_birthtime(const char *, int, const char *, long, long);
233228753Smmint assertion_file_birthtime_recent(const char *, int, const char *);
234228753Smmint assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
235228753Smmint assertion_file_contents(const void *, int, const char *, ...);
236228753Smmint assertion_file_exists(const char *, ...);
237228753Smmint assertion_file_mtime(const char *, int, const char *, long, long);
238228753Smmint assertion_file_mtime_recent(const char *, int, const char *);
239228753Smmint assertion_file_nlinks(const char *, int, const char *, int);
240228753Smmint assertion_file_not_exists(const char *, ...);
241228753Smmint assertion_file_size(const char *, int, const char *, long);
242228753Smmint assertion_is_dir(const char *, int, const char *, int);
243228753Smmint assertion_is_hardlink(const char *, int, const char *, const char *);
244228753Smmint assertion_is_not_hardlink(const char *, int, const char *, const char *);
245228753Smmint assertion_is_reg(const char *, int, const char *, int);
246228753Smmint assertion_is_symlink(const char *, int, const char *, const char *);
247228753Smmint assertion_make_dir(const char *, int, const char *, int);
248228753Smmint assertion_make_file(const char *, int, const char *, int, const char *);
249228753Smmint assertion_make_hardlink(const char *, int, const char *newpath, const char *);
250228753Smmint assertion_make_symlink(const char *, int, const char *newpath, const char *);
251228753Smmint assertion_non_empty_file(const char *, ...);
252228753Smmint assertion_text_file_contents(const char *buff, const char *f);
253228753Smmint assertion_umask(const char *, int, int);
254228753Smmvoid assertion_setup(const char *, int);
255228753Smm
256228753Smmvoid test_skipping(const char *fmt, ...);
257228753Smm
258228753Smm/* Like sprintf, then system() */
259228753Smmint systemf(const char * fmt, ...);
260228753Smm
261228753Smm/* Delay until time() returns a value after this. */
262228753Smmvoid sleepUntilAfter(time_t);
263228753Smm
264228753Smm/* Return true if this platform can create symlinks. */
265228753Smmint canSymlink(void);
266228753Smm
267228753Smm/* Return true if this platform can run the "gzip" program. */
268228753Smmint canGzip(void);
269228753Smm
270228753Smm/* Return true if this platform can run the "gunzip" program. */
271228753Smmint canGunzip(void);
272228753Smm
273228753Smm/* Suck file into string allocated via malloc(). Call free() when done. */
274228753Smm/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
275228753Smmchar *slurpfile(size_t *, const char *fmt, ...);
276228753Smm
277228753Smm/* Extracts named reference file to the current directory. */
278228753Smmvoid extract_reference_file(const char *);
279228753Smm
280228753Smm/*
281228753Smm * Special interfaces for program test harness.
282228753Smm */
283228753Smm
284228753Smm/* Pathname of exe to be tested. */
285228753Smmconst char *testprogfile;
286228753Smm/* Name of exe to use in printf-formatted command strings. */
287228753Smm/* On Windows, this includes leading/trailing quotes. */
288228753Smmconst char *testprog;
289