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 *
25229592Smm * $FreeBSD$
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#define	LOCALE_UTF8	NULL
90228753Smm#else
91228753Smm#define LOCALE_UTF8	"de_DE.UTF-8"
92228753Smm#endif
93228753Smm
94228753Smm/* Visual Studio */
95228753Smm#ifdef _MSC_VER
96228753Smm#define snprintf	sprintf_s
97228753Smm#endif
98228753Smm
99228753Smm#if defined(__BORLANDC__)
100228753Smm#pragma warn -8068	/* Constant out of range in comparison. */
101228753Smm#endif
102228753Smm
103228753Smm/* Cygwin */
104228753Smm#if defined(__CYGWIN__)
105228753Smm/* Cygwin-1.7.x is lazy about populating nlinks, so don't
106228753Smm * expect it to be accurate. */
107228753Smm# define NLINKS_INACCURATE_FOR_DIRS
108228753Smm#endif
109228753Smm
110228753Smm/* Haiku OS */
111228753Smm#if defined(__HAIKU__)
112228753Smm/* Haiku has typedefs in stdint.h (needed for int64_t) */
113228753Smm#include <stdint.h>
114228753Smm#endif
115228753Smm
116228753Smm/* Get a real definition for __FBSDID if we can */
117228753Smm#if HAVE_SYS_CDEFS_H
118228753Smm#include <sys/cdefs.h>
119228753Smm#endif
120228753Smm
121228753Smm/* If not, define it so as to avoid dangling semicolons. */
122228753Smm#ifndef __FBSDID
123228753Smm#define	__FBSDID(a)     struct _undefined_hack
124228753Smm#endif
125228753Smm
126228753Smm#ifndef O_BINARY
127228753Smm#define	O_BINARY 0
128228753Smm#endif
129228753Smm
130228753Smm/*
131228753Smm * Redefine DEFINE_TEST for use in defining the test functions.
132228753Smm */
133228753Smm#undef DEFINE_TEST
134228753Smm#define DEFINE_TEST(name) void name(void); void name(void)
135228753Smm
136228753Smm/* An implementation of the standard assert() macro */
137228753Smm#define assert(e)   assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
138228753Smm/* chdir() and error if it fails */
139228753Smm#define assertChdir(path)  \
140228753Smm  assertion_chdir(__FILE__, __LINE__, path)
141228753Smm/* Assert two integers are the same.  Reports value of each one if not. */
142228753Smm#define assertEqualInt(v1,v2) \
143228753Smm  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
144228753Smm/* Assert two strings are the same.  Reports value of each one if not. */
145228753Smm#define assertEqualString(v1,v2)   \
146228753Smm  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
147228753Smm/* As above, but v1 and v2 are wchar_t * */
148228753Smm#define assertEqualWString(v1,v2)   \
149228753Smm  assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
150228753Smm/* As above, but raw blocks of bytes. */
151228753Smm#define assertEqualMem(v1, v2, l)	\
152228753Smm  assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
153228753Smm/* Assert two files are the same; allow printf-style expansion of second name.
154228753Smm * See below for comments about variable arguments here...
155228753Smm */
156228753Smm#define assertEqualFile		\
157228753Smm  assertion_setup(__FILE__, __LINE__);assertion_equal_file
158228753Smm/* Assert that a file is empty; supports printf-style arguments. */
159228753Smm#define assertEmptyFile		\
160228753Smm  assertion_setup(__FILE__, __LINE__);assertion_empty_file
161228753Smm/* Assert that a file is not empty; supports printf-style arguments. */
162228753Smm#define assertNonEmptyFile		\
163228753Smm  assertion_setup(__FILE__, __LINE__);assertion_non_empty_file
164228753Smm#define assertFileAtime(pathname, sec, nsec)	\
165228753Smm  assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
166228753Smm#define assertFileAtimeRecent(pathname)	\
167228753Smm  assertion_file_atime_recent(__FILE__, __LINE__, pathname)
168228753Smm#define assertFileBirthtime(pathname, sec, nsec)	\
169228753Smm  assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
170228753Smm#define assertFileBirthtimeRecent(pathname) \
171228753Smm  assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
172228753Smm/* Assert that a file exists; supports printf-style arguments. */
173228753Smm#define assertFileExists		\
174228753Smm  assertion_setup(__FILE__, __LINE__);assertion_file_exists
175228753Smm/* Assert that a file exists; supports printf-style arguments. */
176228753Smm#define assertFileNotExists		\
177228753Smm  assertion_setup(__FILE__, __LINE__);assertion_file_not_exists
178228753Smm/* Assert that file contents match a string; supports printf-style arguments. */
179228753Smm#define assertFileContents             \
180228753Smm  assertion_setup(__FILE__, __LINE__);assertion_file_contents
181228753Smm#define assertFileMtime(pathname, sec, nsec)	\
182228753Smm  assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
183228753Smm#define assertFileMtimeRecent(pathname) \
184228753Smm  assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
185228753Smm#define assertFileNLinks(pathname, nlinks)  \
186228753Smm  assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
187228753Smm#define assertFileSize(pathname, size)  \
188228753Smm  assertion_file_size(__FILE__, __LINE__, pathname, size)
189228753Smm#define assertTextFileContents         \
190228753Smm  assertion_setup(__FILE__, __LINE__);assertion_text_file_contents
191228753Smm#define assertIsDir(pathname, mode)		\
192228753Smm  assertion_is_dir(__FILE__, __LINE__, pathname, mode)
193228753Smm#define assertIsHardlink(path1, path2)	\
194228753Smm  assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
195228753Smm#define assertIsNotHardlink(path1, path2)	\
196228753Smm  assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
197228753Smm#define assertIsReg(pathname, mode)		\
198228753Smm  assertion_is_reg(__FILE__, __LINE__, pathname, mode)
199228753Smm#define assertIsSymlink(pathname, contents)	\
200228753Smm  assertion_is_symlink(__FILE__, __LINE__, pathname, contents)
201228753Smm/* Create a directory, report error if it fails. */
202228753Smm#define assertMakeDir(dirname, mode)	\
203228753Smm  assertion_make_dir(__FILE__, __LINE__, dirname, mode)
204228753Smm#define assertMakeFile(path, mode, contents) \
205228753Smm  assertion_make_file(__FILE__, __LINE__, path, mode, contents)
206228753Smm#define assertMakeHardlink(newfile, oldfile)	\
207228753Smm  assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
208228753Smm#define assertMakeSymlink(newfile, linkto)	\
209228753Smm  assertion_make_symlink(__FILE__, __LINE__, newfile, linkto)
210228753Smm#define assertUmask(mask)	\
211228753Smm  assertion_umask(__FILE__, __LINE__, mask)
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	\
220228753Smm  assertion_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 *);
226228753Smmint assertion_empty_file(const char *, ...);
227228753Smmint assertion_equal_file(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 *);
230228753Smmint assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
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 *);
236228753Smmint assertion_file_contents(const void *, int, const char *, ...);
237228753Smmint assertion_file_exists(const char *, ...);
238228753Smmint assertion_file_mtime(const char *, int, const char *, long, long);
239228753Smmint assertion_file_mtime_recent(const char *, int, const char *);
240228753Smmint assertion_file_nlinks(const char *, int, const char *, int);
241228753Smmint assertion_file_not_exists(const char *, ...);
242228753Smmint assertion_file_size(const char *, int, const char *, long);
243228753Smmint assertion_is_dir(const char *, int, const char *, int);
244228753Smmint assertion_is_hardlink(const char *, int, const char *, const char *);
245228753Smmint assertion_is_not_hardlink(const char *, int, const char *, const char *);
246228753Smmint assertion_is_reg(const char *, int, const char *, int);
247228753Smmint assertion_is_symlink(const char *, int, const char *, const char *);
248228753Smmint assertion_make_dir(const char *, int, const char *, int);
249228753Smmint assertion_make_file(const char *, int, const char *, int, const char *);
250228753Smmint assertion_make_hardlink(const char *, int, const char *newpath, const char *);
251228753Smmint assertion_make_symlink(const char *, int, const char *newpath, const char *);
252228753Smmint assertion_non_empty_file(const char *, ...);
253228753Smmint assertion_text_file_contents(const char *buff, const char *f);
254228753Smmint assertion_umask(const char *, int, int);
255228753Smmvoid assertion_setup(const char *, int);
256228753Smm
257228753Smmvoid test_skipping(const char *fmt, ...);
258228753Smm
259228753Smm/* Like sprintf, then system() */
260228753Smmint systemf(const char * fmt, ...);
261228753Smm
262228753Smm/* Delay until time() returns a value after this. */
263228753Smmvoid sleepUntilAfter(time_t);
264228753Smm
265228753Smm/* Return true if this platform can create symlinks. */
266228753Smmint canSymlink(void);
267228753Smm
268228753Smm/* Return true if this platform can run the "gzip" program. */
269228753Smmint canGzip(void);
270228753Smm
271228753Smm/* Return true if this platform can run the "gunzip" program. */
272228753Smmint canGunzip(void);
273228753Smm
274228753Smm/* Suck file into string allocated via malloc(). Call free() when done. */
275228753Smm/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
276228753Smmchar *slurpfile(size_t *, const char *fmt, ...);
277228753Smm
278228753Smm/* Extracts named reference file to the current directory. */
279228753Smmvoid extract_reference_file(const char *);
280228753Smm
281228753Smm/*
282228753Smm * Special interfaces for libarchive test harness.
283228753Smm */
284228753Smm
285228753Smm#include "archive.h"
286228753Smm#include "archive_entry.h"
287228753Smm
288228753Smm/* Special customized read-from-memory interface. */
289228753Smmint read_open_memory(struct archive *, void *, size_t, size_t);
290228753Smm/* "2" version exercises a slightly different set of libarchive APIs. */
291228753Smmint read_open_memory2(struct archive *, void *, size_t, size_t);
292228753Smm
293228753Smm/* Versions of above that accept an archive argument for additional info. */
294228753Smm#define assertA(e)   assertion_assert(__FILE__, __LINE__, (e), #e, (a))
295228753Smm#define assertEqualIntA(a,v1,v2)   \
296228753Smm  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))
297228753Smm#define assertEqualStringA(a,v1,v2)   \
298228753Smm  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))
299228753Smm
300228753Smm#ifdef USE_DMALLOC
301228753Smm#include <dmalloc.h>
302228753Smm#endif
303