1/*
2 * Copyright (c) 2003-2006 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: src/usr.bin/tar/test/test.h,v 1.4 2008/08/21 07:04:57 kientzle Exp $
26 */
27
28/* Every test program should #include "test.h" as the first thing. */
29
30/*
31 * The goal of this file (and the matching test.c) is to
32 * simplify the very repetitive test-*.c test programs.
33 */
34#if defined(HAVE_CONFIG_H)
35/* Most POSIX platforms use the 'configure' script to build config.h */
36#include "config.h"
37#elif defined(__FreeBSD__)
38/* Building as part of FreeBSD system requires a pre-built config.h. */
39#include "config_freebsd.h"
40#elif defined(_WIN32) && !defined(__CYGWIN__)
41/* Win32 can't run the 'configure' script. */
42#include "config_windows.h"
43#else
44/* Warn if the library hasn't been (automatically or manually) configured. */
45#error Oops: No config.h and no pre-built configuration in test.h.
46#endif
47
48#include <sys/types.h>  /* Windows requires this before sys/stat.h */
49#include <sys/stat.h>
50
51#ifdef USE_DMALLOC
52#include <dmalloc.h>
53#endif
54#if HAVE_DIRENT_H
55#include <dirent.h>
56#endif
57#ifdef HAVE_DIRECT_H
58#include <direct.h>
59#define dirent direct
60#endif
61#include <errno.h>
62#include <fcntl.h>
63#ifdef HAVE_IO_H
64#include <io.h>
65#endif
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <time.h>
70#ifdef HAVE_UNISTD_H
71#include <unistd.h>
72#endif
73#include <wchar.h>
74#ifdef HAVE_WINDOWS_H
75#include <windows.h>
76#endif
77
78/*
79 * System-specific tweaks.  We really want to minimize these
80 * as much as possible, since they make it harder to understand
81 * the mainline code.
82 */
83
84/* Windows (including Visual Studio and MinGW but not Cygwin) */
85#if defined(_WIN32) && !defined(__CYGWIN__)
86#include "../bsdtar_windows.h"
87#if !defined(__BORLANDC__)
88#define strdup _strdup
89#endif
90#define LOCALE_DE	"deu"
91#else
92#define LOCALE_DE	"de_DE.UTF-8"
93#endif
94
95/* Visual Studio */
96#ifdef _MSC_VER
97#define snprintf	sprintf_s
98#endif
99
100/* Cygwin */
101#if defined(__CYGWIN__)
102/* Cygwin-1.7.x is lazy about populating nlinks, so don't
103 * expect it to be accurate. */
104# define NLINKS_INACCURATE_FOR_DIRS
105#endif
106
107/* Haiku OS */
108#if defined(__HAIKU__)
109/* Haiku has typedefs in stdint.h (needed for int64_t) */
110#include <stdint.h>
111#endif
112
113/* Get a real definition for __FBSDID if we can */
114#if HAVE_SYS_CDEFS_H
115#include <sys/cdefs.h>
116#endif
117
118/* If not, define it so as to avoid dangling semicolons. */
119#ifndef __FBSDID
120#define	__FBSDID(a)     struct _undefined_hack
121#endif
122
123#ifndef O_BINARY
124#define	O_BINARY 0
125#endif
126
127/*
128 * Redefine DEFINE_TEST for use in defining the test functions.
129 */
130#undef DEFINE_TEST
131#define DEFINE_TEST(name) void name(void); void name(void)
132
133/* An implementation of the standard assert() macro */
134#define assert(e)   assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
135/* chdir() and error if it fails */
136#define assertChdir(path)  \
137  assertion_chdir(__FILE__, __LINE__, path)
138/* Assert two integers are the same.  Reports value of each one if not. */
139#define assertEqualInt(v1,v2) \
140  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
141/* Assert two strings are the same.  Reports value of each one if not. */
142#define assertEqualString(v1,v2)   \
143  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
144/* As above, but v1 and v2 are wchar_t * */
145#define assertEqualWString(v1,v2)   \
146  assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
147/* As above, but raw blocks of bytes. */
148#define assertEqualMem(v1, v2, l)	\
149  assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
150/* Assert two files are the same; allow printf-style expansion of second name.
151 * See below for comments about variable arguments here...
152 */
153#define assertEqualFile		\
154  assertion_setup(__FILE__, __LINE__);assertion_equal_file
155/* Assert that a file is empty; supports printf-style arguments. */
156#define assertEmptyFile		\
157  assertion_setup(__FILE__, __LINE__);assertion_empty_file
158/* Assert that a file is not empty; supports printf-style arguments. */
159#define assertNonEmptyFile		\
160  assertion_setup(__FILE__, __LINE__);assertion_non_empty_file
161#define assertFileAtime(pathname, sec, nsec)	\
162  assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
163#define assertFileAtimeRecent(pathname)	\
164  assertion_file_atime_recent(__FILE__, __LINE__, pathname)
165#define assertFileBirthtime(pathname, sec, nsec)	\
166  assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
167#define assertFileBirthtimeRecent(pathname) \
168  assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
169/* Assert that a file exists; supports printf-style arguments. */
170#define assertFileExists		\
171  assertion_setup(__FILE__, __LINE__);assertion_file_exists
172/* Assert that a file exists; supports printf-style arguments. */
173#define assertFileNotExists		\
174  assertion_setup(__FILE__, __LINE__);assertion_file_not_exists
175/* Assert that file contents match a string; supports printf-style arguments. */
176#define assertFileContents             \
177  assertion_setup(__FILE__, __LINE__);assertion_file_contents
178#define assertFileMtime(pathname, sec, nsec)	\
179  assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
180#define assertFileMtimeRecent(pathname) \
181  assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
182#define assertFileNLinks(pathname, nlinks)  \
183  assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
184#define assertFileSize(pathname, size)  \
185  assertion_file_size(__FILE__, __LINE__, pathname, size)
186#define assertTextFileContents         \
187  assertion_setup(__FILE__, __LINE__);assertion_text_file_contents
188#define assertFileContainsLinesAnyOrder(pathname, lines)	\
189	assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)
190#define assertIsDir(pathname, mode)		\
191  assertion_is_dir(__FILE__, __LINE__, pathname, mode)
192#define assertIsHardlink(path1, path2)	\
193  assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
194#define assertIsNotHardlink(path1, path2)	\
195  assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
196#define assertIsReg(pathname, mode)		\
197  assertion_is_reg(__FILE__, __LINE__, pathname, mode)
198#define assertIsSymlink(pathname, contents)	\
199  assertion_is_symlink(__FILE__, __LINE__, pathname, contents)
200/* Create a directory, report error if it fails. */
201#define assertMakeDir(dirname, mode)	\
202  assertion_make_dir(__FILE__, __LINE__, dirname, mode)
203#define assertMakeFile(path, mode, contents) \
204  assertion_make_file(__FILE__, __LINE__, path, mode, contents)
205#define assertMakeHardlink(newfile, oldfile)	\
206  assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
207#define assertMakeSymlink(newfile, linkto)	\
208  assertion_make_symlink(__FILE__, __LINE__, newfile, linkto)
209#define assertUmask(mask)	\
210  assertion_umask(__FILE__, __LINE__, mask)
211
212/*
213 * This would be simple with C99 variadic macros, but I don't want to
214 * require that.  Instead, I insert a function call before each
215 * skipping() call to pass the file and line information down.  Crude,
216 * but effective.
217 */
218#define skipping	\
219  assertion_setup(__FILE__, __LINE__);test_skipping
220
221/* Function declarations.  These are defined in test_utility.c. */
222void failure(const char *fmt, ...);
223int assertion_assert(const char *, int, int, const char *, void *);
224int assertion_chdir(const char *, int, const char *);
225int assertion_empty_file(const char *, ...);
226int assertion_equal_file(const char *, const char *, ...);
227int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
228int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
229int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
230int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
231int assertion_file_atime(const char *, int, const char *, long, long);
232int assertion_file_atime_recent(const char *, int, const char *);
233int assertion_file_birthtime(const char *, int, const char *, long, long);
234int assertion_file_birthtime_recent(const char *, int, const char *);
235int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
236int assertion_file_contents(const void *, int, const char *, ...);
237int assertion_file_exists(const char *, ...);
238int assertion_file_mtime(const char *, int, const char *, long, long);
239int assertion_file_mtime_recent(const char *, int, const char *);
240int assertion_file_nlinks(const char *, int, const char *, int);
241int assertion_file_not_exists(const char *, ...);
242int assertion_file_size(const char *, int, const char *, long);
243int assertion_is_dir(const char *, int, const char *, int);
244int assertion_is_hardlink(const char *, int, const char *, const char *);
245int assertion_is_not_hardlink(const char *, int, const char *, const char *);
246int assertion_is_reg(const char *, int, const char *, int);
247int assertion_is_symlink(const char *, int, const char *, const char *);
248int assertion_make_dir(const char *, int, const char *, int);
249int assertion_make_file(const char *, int, const char *, int, const char *);
250int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
251int assertion_make_symlink(const char *, int, const char *newpath, const char *);
252int assertion_non_empty_file(const char *, ...);
253int assertion_text_file_contents(const char *buff, const char *f);
254int assertion_umask(const char *, int, int);
255void assertion_setup(const char *, int);
256
257void test_skipping(const char *fmt, ...);
258
259/* Like sprintf, then system() */
260int systemf(const char * fmt, ...);
261
262/* Delay until time() returns a value after this. */
263void sleepUntilAfter(time_t);
264
265/* Return true if this platform can create symlinks. */
266int canSymlink(void);
267
268/* Return true if this platform can run the "gzip" program. */
269int canGzip(void);
270
271/* Return true if this platform can run the "gunzip" program. */
272int canGunzip(void);
273
274/* Suck file into string allocated via malloc(). Call free() when done. */
275/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
276char *slurpfile(size_t *, const char *fmt, ...);
277
278/* Extracts named reference file to the current directory. */
279void extract_reference_file(const char *);
280
281/*
282 * Special interfaces for program test harness.
283 */
284
285/* Pathname of exe to be tested. */
286const char *testprogfile;
287/* Name of exe to use in printf-formatted command strings. */
288/* On Windows, this includes leading/trailing quotes. */
289const char *testprog;
290