xref: /freebsd/contrib/libarchive/cpio/test/test.h (revision 145992504973bd16cf3518af9ba5ce185fefa82a)
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$
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 #if HAVE_DIRENT_H
52 #include <dirent.h>
53 #endif
54 #ifdef HAVE_DIRECT_H
55 #include <direct.h>
56 #define dirent direct
57 #endif
58 #include <errno.h>
59 #include <fcntl.h>
60 #ifdef HAVE_IO_H
61 #include <io.h>
62 #endif
63 #ifdef HAVE_STDINT_H
64 #include <stdint.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 #if !defined(__BORLANDC__)
87 #define strdup _strdup
88 #endif
89 #endif
90 
91 /* Visual Studio */
92 #ifdef _MSC_VER
93 #define snprintf	sprintf_s
94 #endif
95 
96 #if defined(__BORLANDC__)
97 #pragma warn -8068	/* Constant out of range in comparison. */
98 #endif
99 
100 /* Haiku OS and QNX */
101 #if defined(__HAIKU__) || defined(__QNXNTO__)
102 /* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */
103 #include <stdint.h>
104 #endif
105 
106 /* Get a real definition for __FBSDID if we can */
107 #if HAVE_SYS_CDEFS_H
108 #include <sys/cdefs.h>
109 #endif
110 
111 /* If not, define it so as to avoid dangling semicolons. */
112 #ifndef __FBSDID
113 #define	__FBSDID(a)     struct _undefined_hack
114 #endif
115 
116 #ifndef O_BINARY
117 #define	O_BINARY 0
118 #endif
119 
120 /*
121  * Redefine DEFINE_TEST for use in defining the test functions.
122  */
123 #undef DEFINE_TEST
124 #define DEFINE_TEST(name) void name(void); void name(void)
125 
126 /* An implementation of the standard assert() macro */
127 #define assert(e)   assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
128 /* chdir() and error if it fails */
129 #define assertChdir(path)  \
130   assertion_chdir(__FILE__, __LINE__, path)
131 /* Assert two integers are the same.  Reports value of each one if not. */
132 #define assertEqualInt(v1,v2) \
133   assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
134 /* Assert two strings are the same.  Reports value of each one if not. */
135 #define assertEqualString(v1,v2)   \
136   assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0)
137 #define assertEqualUTF8String(v1,v2)   \
138   assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1)
139 /* As above, but v1 and v2 are wchar_t * */
140 #define assertEqualWString(v1,v2)   \
141   assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
142 /* As above, but raw blocks of bytes. */
143 #define assertEqualMem(v1, v2, l)	\
144   assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
145 /* Assert two files are the same. */
146 #define assertEqualFile(f1, f2)	\
147   assertion_equal_file(__FILE__, __LINE__, (f1), (f2))
148 /* Assert that a file is empty. */
149 #define assertEmptyFile(pathname)	\
150   assertion_empty_file(__FILE__, __LINE__, (pathname))
151 /* Assert that a file is not empty. */
152 #define assertNonEmptyFile(pathname)		\
153   assertion_non_empty_file(__FILE__, __LINE__, (pathname))
154 #define assertFileAtime(pathname, sec, nsec)	\
155   assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
156 #define assertFileAtimeRecent(pathname)	\
157   assertion_file_atime_recent(__FILE__, __LINE__, pathname)
158 #define assertFileBirthtime(pathname, sec, nsec)	\
159   assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
160 #define assertFileBirthtimeRecent(pathname) \
161   assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
162 /* Assert that a file exists; supports printf-style arguments. */
163 #define assertFileExists(pathname) \
164   assertion_file_exists(__FILE__, __LINE__, pathname)
165 /* Assert that a file exists. */
166 #define assertFileNotExists(pathname) \
167   assertion_file_not_exists(__FILE__, __LINE__, pathname)
168 /* Assert that file contents match a string. */
169 #define assertFileContents(data, data_size, pathname) \
170   assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname)
171 #define assertFileMtime(pathname, sec, nsec)	\
172   assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
173 #define assertFileMtimeRecent(pathname) \
174   assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
175 #define assertFileNLinks(pathname, nlinks)  \
176   assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
177 #define assertFileSize(pathname, size)  \
178   assertion_file_size(__FILE__, __LINE__, pathname, size)
179 #define assertTextFileContents(text, pathname) \
180   assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
181 #define assertFileContainsLinesAnyOrder(pathname, lines)	\
182   assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)
183 #define assertIsDir(pathname, mode)		\
184   assertion_is_dir(__FILE__, __LINE__, pathname, mode)
185 #define assertIsHardlink(path1, path2)	\
186   assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
187 #define assertIsNotHardlink(path1, path2)	\
188   assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
189 #define assertIsReg(pathname, mode)		\
190   assertion_is_reg(__FILE__, __LINE__, pathname, mode)
191 #define assertIsSymlink(pathname, contents)	\
192   assertion_is_symlink(__FILE__, __LINE__, pathname, contents)
193 /* Create a directory, report error if it fails. */
194 #define assertMakeDir(dirname, mode)	\
195   assertion_make_dir(__FILE__, __LINE__, dirname, mode)
196 #define assertMakeFile(path, mode, contents) \
197   assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
198 #define assertMakeBinFile(path, mode, csize, contents) \
199   assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
200 #define assertMakeHardlink(newfile, oldfile)	\
201   assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
202 #define assertMakeSymlink(newfile, linkto)	\
203   assertion_make_symlink(__FILE__, __LINE__, newfile, linkto)
204 #define assertNodump(path)      \
205   assertion_nodump(__FILE__, __LINE__, path)
206 #define assertUmask(mask)	\
207   assertion_umask(__FILE__, __LINE__, mask)
208 #define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec)	\
209   assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec)
210 
211 /*
212  * This would be simple with C99 variadic macros, but I don't want to
213  * require that.  Instead, I insert a function call before each
214  * skipping() call to pass the file and line information down.  Crude,
215  * but effective.
216  */
217 #define skipping	\
218   skipping_setup(__FILE__, __LINE__);test_skipping
219 
220 /* Function declarations.  These are defined in test_utility.c. */
221 void failure(const char *fmt, ...);
222 int assertion_assert(const char *, int, int, const char *, void *);
223 int assertion_chdir(const char *, int, const char *);
224 int assertion_empty_file(const char *, int, const char *);
225 int assertion_equal_file(const char *, int, const char *, const char *);
226 int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
227 int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
228 int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int);
229 int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
230 int assertion_file_atime(const char *, int, const char *, long, long);
231 int assertion_file_atime_recent(const char *, int, const char *);
232 int assertion_file_birthtime(const char *, int, const char *, long, long);
233 int assertion_file_birthtime_recent(const char *, int, const char *);
234 int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
235 int assertion_file_contents(const char *, int, const void *, int, const char *);
236 int assertion_file_exists(const char *, int, const char *);
237 int assertion_file_mtime(const char *, int, const char *, long, long);
238 int assertion_file_mtime_recent(const char *, int, const char *);
239 int assertion_file_nlinks(const char *, int, const char *, int);
240 int assertion_file_not_exists(const char *, int, const char *);
241 int assertion_file_size(const char *, int, const char *, long);
242 int assertion_is_dir(const char *, int, const char *, int);
243 int assertion_is_hardlink(const char *, int, const char *, const char *);
244 int assertion_is_not_hardlink(const char *, int, const char *, const char *);
245 int assertion_is_reg(const char *, int, const char *, int);
246 int assertion_is_symlink(const char *, int, const char *, const char *);
247 int assertion_make_dir(const char *, int, const char *, int);
248 int assertion_make_file(const char *, int, const char *, int, int, const void *);
249 int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
250 int assertion_make_symlink(const char *, int, const char *newpath, const char *);
251 int assertion_nodump(const char *, int, const char *);
252 int assertion_non_empty_file(const char *, int, const char *);
253 int assertion_text_file_contents(const char *, int, const char *buff, const char *f);
254 int assertion_umask(const char *, int, int);
255 int assertion_utimes(const char *, int, const char *, long, long, long, long );
256 
257 void skipping_setup(const char *, int);
258 void test_skipping(const char *fmt, ...);
259 
260 /* Like sprintf, then system() */
261 int systemf(const char * fmt, ...);
262 
263 /* Delay until time() returns a value after this. */
264 void sleepUntilAfter(time_t);
265 
266 /* Return true if this platform can create symlinks. */
267 int canSymlink(void);
268 
269 /* Return true if this platform can run the "gzip" program. */
270 int canGzip(void);
271 
272 /* Return true if this platform can run the "gunzip" program. */
273 int canGunzip(void);
274 
275 /* Return true if this filesystem can handle nodump flags. */
276 int canNodump(void);
277 
278 /* Return true if the file has large i-node number(>0xffffffff). */
279 int is_LargeInode(const char *);
280 
281 /* Suck file into string allocated via malloc(). Call free() when done. */
282 /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
283 char *slurpfile(size_t *, const char *fmt, ...);
284 
285 /* Extracts named reference file to the current directory. */
286 void extract_reference_file(const char *);
287 
288 /* Path to working directory for current test */
289 const char *testworkdir;
290 
291 /*
292  * Special interfaces for program test harness.
293  */
294 
295 /* Pathname of exe to be tested. */
296 const char *testprogfile;
297 /* Name of exe to use in printf-formatted command strings. */
298 /* On Windows, this includes leading/trailing quotes. */
299 const char *testprog;
300 
301 #ifdef USE_DMALLOC
302 #include <dmalloc.h>
303 #endif
304