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$
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
63232153Smm#ifdef HAVE_STDINT_H
64232153Smm#include <stdint.h>
65232153Smm#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__)
87232153Smm#undef chdir
88232153Smm#define chdir _chdir
89228753Smm#define strdup _strdup
90228753Smm#endif
91228753Smm#endif
92228753Smm
93228753Smm/* Visual Studio */
94228753Smm#ifdef _MSC_VER
95228753Smm#define snprintf	sprintf_s
96228753Smm#endif
97228753Smm
98232153Smm#if defined(__BORLANDC__)
99232153Smm#pragma warn -8068	/* Constant out of range in comparison. */
100228753Smm#endif
101228753Smm
102232153Smm/* Haiku OS and QNX */
103228753Smm#if defined(__HAIKU__) || defined(__QNXNTO__)
104228753Smm/* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */
105228753Smm#include <stdint.h>
106228753Smm#endif
107228753Smm
108228753Smm/* Get a real definition for __FBSDID if we can */
109228753Smm#if HAVE_SYS_CDEFS_H
110228753Smm#include <sys/cdefs.h>
111228753Smm#endif
112228753Smm
113228753Smm/* If not, define it so as to avoid dangling semicolons. */
114228753Smm#ifndef __FBSDID
115228753Smm#define	__FBSDID(a)     struct _undefined_hack
116228753Smm#endif
117228753Smm
118228753Smm#ifndef O_BINARY
119228753Smm#define	O_BINARY 0
120228753Smm#endif
121228753Smm
122228753Smm/*
123228753Smm * Redefine DEFINE_TEST for use in defining the test functions.
124228753Smm */
125228753Smm#undef DEFINE_TEST
126228753Smm#define DEFINE_TEST(name) void name(void); void name(void)
127228753Smm
128228753Smm/* An implementation of the standard assert() macro */
129228753Smm#define assert(e)   assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
130228753Smm/* chdir() and error if it fails */
131228753Smm#define assertChdir(path)  \
132228753Smm  assertion_chdir(__FILE__, __LINE__, path)
133228753Smm/* Assert two integers are the same.  Reports value of each one if not. */
134228753Smm#define assertEqualInt(v1,v2) \
135228753Smm  assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
136228753Smm/* Assert two strings are the same.  Reports value of each one if not. */
137228753Smm#define assertEqualString(v1,v2)   \
138232153Smm  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0)
139232153Smm#define assertEqualUTF8String(v1,v2)   \
140232153Smm  assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1)
141228753Smm/* As above, but v1 and v2 are wchar_t * */
142228753Smm#define assertEqualWString(v1,v2)   \
143228753Smm  assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
144228753Smm/* As above, but raw blocks of bytes. */
145228753Smm#define assertEqualMem(v1, v2, l)	\
146228753Smm  assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
147232153Smm/* Assert two files are the same. */
148232153Smm#define assertEqualFile(f1, f2)	\
149232153Smm  assertion_equal_file(__FILE__, __LINE__, (f1), (f2))
150232153Smm/* Assert that a file is empty. */
151232153Smm#define assertEmptyFile(pathname)	\
152232153Smm  assertion_empty_file(__FILE__, __LINE__, (pathname))
153232153Smm/* Assert that a file is not empty. */
154232153Smm#define assertNonEmptyFile(pathname)		\
155232153Smm  assertion_non_empty_file(__FILE__, __LINE__, (pathname))
156228753Smm#define assertFileAtime(pathname, sec, nsec)	\
157228753Smm  assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
158228753Smm#define assertFileAtimeRecent(pathname)	\
159228753Smm  assertion_file_atime_recent(__FILE__, __LINE__, pathname)
160228753Smm#define assertFileBirthtime(pathname, sec, nsec)	\
161228753Smm  assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
162228753Smm#define assertFileBirthtimeRecent(pathname) \
163228753Smm  assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
164228753Smm/* Assert that a file exists; supports printf-style arguments. */
165232153Smm#define assertFileExists(pathname) \
166232153Smm  assertion_file_exists(__FILE__, __LINE__, pathname)
167232153Smm/* Assert that a file exists. */
168232153Smm#define assertFileNotExists(pathname) \
169232153Smm  assertion_file_not_exists(__FILE__, __LINE__, pathname)
170232153Smm/* Assert that file contents match a string. */
171232153Smm#define assertFileContents(data, data_size, pathname) \
172232153Smm  assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname)
173228753Smm#define assertFileMtime(pathname, sec, nsec)	\
174228753Smm  assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
175228753Smm#define assertFileMtimeRecent(pathname) \
176228753Smm  assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
177228753Smm#define assertFileNLinks(pathname, nlinks)  \
178228753Smm  assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
179228753Smm#define assertFileSize(pathname, size)  \
180228753Smm  assertion_file_size(__FILE__, __LINE__, pathname, size)
181232153Smm#define assertTextFileContents(text, pathname) \
182232153Smm  assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
183228753Smm#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 *);
236228753Smmint 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);
244228753Smmint assertion_is_dir(const char *, int, const char *, int);
245228753Smmint assertion_is_hardlink(const char *, int, const char *, const char *);
246228753Smmint assertion_is_not_hardlink(const char *, int, const char *, const char *);
247228753Smmint assertion_is_reg(const char *, int, const char *, int);
248228753Smmint assertion_is_symlink(const char *, int, const char *, const char *);
249228753Smmint assertion_make_dir(const char *, int, const char *, int);
250238856Smmint assertion_make_file(const char *, int, const char *, int, int, const void *);
251228753Smmint assertion_make_hardlink(const char *, int, const char *newpath, const char *);
252228753Smmint assertion_make_symlink(const char *, int, const char *newpath, const char *);
253238856Smmint assertion_nodump(const char *, int, const char *);
254232153Smmint assertion_non_empty_file(const char *, int, const char *);
255232153Smmint assertion_text_file_contents(const char *, int, const char *buff, const char *f);
256228753Smmint assertion_umask(const char *, int, int);
257232153Smmint assertion_utimes(const char *, int, const char *, long, long, long, long );
258228753Smm
259232153Smmvoid skipping_setup(const char *, int);
260228753Smmvoid test_skipping(const char *fmt, ...);
261228753Smm
262228753Smm/* Like sprintf, then system() */
263228753Smmint systemf(const char * fmt, ...);
264228753Smm
265228753Smm/* Delay until time() returns a value after this. */
266228753Smmvoid sleepUntilAfter(time_t);
267228753Smm
268228753Smm/* Return true if this platform can create symlinks. */
269228753Smmint canSymlink(void);
270228753Smm
271248616Smm/* Return true if this platform can run the "bzip2" program. */
272248616Smmint canBzip2(void);
273248616Smm
274248616Smm/* Return true if this platform can run the "grzip" program. */
275248616Smmint canGrzip(void);
276248616Smm
277228753Smm/* Return true if this platform can run the "gzip" program. */
278228753Smmint canGzip(void);
279228753Smm
280248616Smm/* Return true if this platform can run the "lrzip" program. */
281248616Smmint canLrzip(void);
282228753Smm
283248616Smm/* Return true if this platform can run the "lzip" program. */
284248616Smmint canLzip(void);
285248616Smm
286248616Smm/* Return true if this platform can run the "lzma" program. */
287248616Smmint canLzma(void);
288248616Smm
289248616Smm/* Return true if this platform can run the "lzop" program. */
290248616Smmint canLzop(void);
291248616Smm
292248616Smm/* Return true if this platform can run the "xz" program. */
293248616Smmint canXz(void);
294248616Smm
295238856Smm/* Return true if this filesystem can handle nodump flags. */
296238856Smmint canNodump(void);
297238856Smm
298232153Smm/* Return true if the file has large i-node number(>0xffffffff). */
299232153Smmint is_LargeInode(const char *);
300232153Smm
301228753Smm/* Suck file into string allocated via malloc(). Call free() when done. */
302228753Smm/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
303228753Smmchar *slurpfile(size_t *, const char *fmt, ...);
304228753Smm
305228753Smm/* Extracts named reference file to the current directory. */
306228753Smmvoid extract_reference_file(const char *);
307228753Smm
308232153Smm/* Path to working directory for current test */
309232153Smmconst char *testworkdir;
310232153Smm
311228753Smm/*
312228753Smm * Special interfaces for program test harness.
313228753Smm */
314228753Smm
315228753Smm/* Pathname of exe to be tested. */
316228753Smmconst char *testprogfile;
317228753Smm/* Name of exe to use in printf-formatted command strings. */
318228753Smm/* On Windows, this includes leading/trailing quotes. */
319228753Smmconst char *testprog;
320232153Smm
321232153Smm#ifdef USE_DMALLOC
322232153Smm#include <dmalloc.h>
323232153Smm#endif
324