2 * Copyright (c) 2003-2006 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 /* Every test program should #include "test.h" as the first thing. */
31 * The goal of this file (and the matching test.c) is to
32 * simplify the very repetitive test-*.c test programs.
34 #if defined(HAVE_CONFIG_H)
35 /* Most POSIX platforms use the 'configure' script to build 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"
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.
48 #include <sys/types.h> /* Windows requires this before sys/stat.h */
80 * System-specific tweaks. We really want to minimize these
81 * as much as possible, since they make it harder to understand
85 /* Windows (including Visual Studio and MinGW but not Cygwin) */
86 #if defined(_WIN32) && !defined(__CYGWIN__)
87 #if !defined(__BORLANDC__)
90 #define strdup _strdup
95 #if defined(_MSC_VER) && _MSC_VER < 1900
96 #define snprintf sprintf_s
99 #if defined(__BORLANDC__)
100 #pragma warn -8068 /* Constant out of range in comparison. */
103 /* Haiku OS and QNX */
104 #if defined(__HAIKU__) || defined(__QNXNTO__)
105 /* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */
109 /* Get a real definition for __FBSDID if we can */
111 #include <sys/cdefs.h>
114 /* If not, define it so as to avoid dangling semicolons. */
116 #define __FBSDID(a) struct _undefined_hack
124 * If this platform has <sys/acl.h>, acl_create(), acl_init(),
125 * acl_set_file(), and ACL_USER, we assume it has the rest of the
126 * POSIX.1e draft functions used in archive_read_extract.c.
128 #if HAVE_SYS_ACL_H && HAVE_ACL_CREATE_ENTRY && HAVE_ACL_INIT && HAVE_ACL_SET_FILE
130 #define HAVE_POSIX_ACL 1
131 #elif HAVE_ACL_TYPE_EXTENDED
132 #define HAVE_DARWIN_ACL 1
137 * If this platform has <sys/acl.h>, acl_get(), facl_get(), acl_set(),
138 * facl_set() and types aclent_t and ace_t it uses Solaris-style ACL functions
140 #if HAVE_SYS_ACL_H && HAVE_ACL_GET && HAVE_FACL_GET && HAVE_ACL_SET && HAVE_FACL_SET && HAVE_ACLENT_T && HAVE_ACE_T
141 #define HAVE_SUN_ACL 1
144 /* Define if platform supports NFSv4 ACLs */
145 #if (HAVE_POSIX_ACL && HAVE_ACL_TYPE_NFS4) || HAVE_SUN_ACL || HAVE_DARWIN_ACL
146 #define HAVE_NFS4_ACL 1
150 * Redefine DEFINE_TEST for use in defining the test functions.
153 #define DEFINE_TEST(name) void name(void); void name(void)
155 /* An implementation of the standard assert() macro */
156 #define assert(e) assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
157 /* chdir() and error if it fails */
158 #define assertChdir(path) \
159 assertion_chdir(__FILE__, __LINE__, path)
160 /* Assert two integers are the same. Reports value of each one if not. */
161 #define assertEqualInt(v1,v2) \
162 assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
163 /* Assert two strings are the same. Reports value of each one if not. */
164 #define assertEqualString(v1,v2) \
165 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0)
166 #define assertEqualUTF8String(v1,v2) \
167 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1)
168 /* As above, but v1 and v2 are wchar_t * */
169 #define assertEqualWString(v1,v2) \
170 assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
171 /* As above, but raw blocks of bytes. */
172 #define assertEqualMem(v1, v2, l) \
173 assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
174 /* Assert that memory is full of a specified byte */
175 #define assertMemoryFilledWith(v1, l, b) \
176 assertion_memory_filled_with(__FILE__, __LINE__, (v1), #v1, (l), #l, (b), #b, NULL)
177 /* Assert two files are the same. */
178 #define assertEqualFile(f1, f2) \
179 assertion_equal_file(__FILE__, __LINE__, (f1), (f2))
180 /* Assert that a file is empty. */
181 #define assertEmptyFile(pathname) \
182 assertion_empty_file(__FILE__, __LINE__, (pathname))
183 /* Assert that a file is not empty. */
184 #define assertNonEmptyFile(pathname) \
185 assertion_non_empty_file(__FILE__, __LINE__, (pathname))
186 #define assertFileAtime(pathname, sec, nsec) \
187 assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
188 #define assertFileAtimeRecent(pathname) \
189 assertion_file_atime_recent(__FILE__, __LINE__, pathname)
190 #define assertFileBirthtime(pathname, sec, nsec) \
191 assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
192 #define assertFileBirthtimeRecent(pathname) \
193 assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
194 /* Assert that a file exists; supports printf-style arguments. */
195 #define assertFileExists(pathname) \
196 assertion_file_exists(__FILE__, __LINE__, pathname)
197 /* Assert that a file exists. */
198 #define assertFileNotExists(pathname) \
199 assertion_file_not_exists(__FILE__, __LINE__, pathname)
200 /* Assert that file contents match a string. */
201 #define assertFileContents(data, data_size, pathname) \
202 assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname)
203 /* Verify that a file does not contain invalid strings */
204 #define assertFileContainsNoInvalidStrings(pathname, strings) \
205 assertion_file_contains_no_invalid_strings(__FILE__, __LINE__, pathname, strings)
206 #define assertFileMtime(pathname, sec, nsec) \
207 assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
208 #define assertFileMtimeRecent(pathname) \
209 assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
210 #define assertFileNLinks(pathname, nlinks) \
211 assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
212 #define assertFileSize(pathname, size) \
213 assertion_file_size(__FILE__, __LINE__, pathname, size)
214 #define assertFileMode(pathname, mode) \
215 assertion_file_mode(__FILE__, __LINE__, pathname, mode)
216 #define assertTextFileContents(text, pathname) \
217 assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
218 #define assertFileContainsLinesAnyOrder(pathname, lines) \
219 assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)
220 #define assertIsDir(pathname, mode) \
221 assertion_is_dir(__FILE__, __LINE__, pathname, mode)
222 #define assertIsHardlink(path1, path2) \
223 assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
224 #define assertIsNotHardlink(path1, path2) \
225 assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
226 #define assertIsReg(pathname, mode) \
227 assertion_is_reg(__FILE__, __LINE__, pathname, mode)
228 #define assertIsSymlink(pathname, contents) \
229 assertion_is_symlink(__FILE__, __LINE__, pathname, contents)
230 /* Create a directory, report error if it fails. */
231 #define assertMakeDir(dirname, mode) \
232 assertion_make_dir(__FILE__, __LINE__, dirname, mode)
233 #define assertMakeFile(path, mode, contents) \
234 assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
235 #define assertMakeBinFile(path, mode, csize, contents) \
236 assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
237 #define assertMakeHardlink(newfile, oldfile) \
238 assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
239 #define assertMakeSymlink(newfile, linkto) \
240 assertion_make_symlink(__FILE__, __LINE__, newfile, linkto)
241 #define assertNodump(path) \
242 assertion_nodump(__FILE__, __LINE__, path)
243 #define assertUmask(mask) \
244 assertion_umask(__FILE__, __LINE__, mask)
245 #define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec) \
246 assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec)
249 * This would be simple with C99 variadic macros, but I don't want to
250 * require that. Instead, I insert a function call before each
251 * skipping() call to pass the file and line information down. Crude,
255 skipping_setup(__FILE__, __LINE__);test_skipping
257 /* Function declarations. These are defined in test_utility.c. */
258 void failure(const char *fmt, ...);
259 int assertion_assert(const char *, int, int, const char *, void *);
260 int assertion_chdir(const char *, int, const char *);
261 int assertion_empty_file(const char *, int, const char *);
262 int assertion_equal_file(const char *, int, const char *, const char *);
263 int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
264 int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
265 int assertion_memory_filled_with(const char *, int, const void *, const char *, size_t, const char *, char, const char *, void *);
266 int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int);
267 int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
268 int assertion_file_atime(const char *, int, const char *, long, long);
269 int assertion_file_atime_recent(const char *, int, const char *);
270 int assertion_file_birthtime(const char *, int, const char *, long, long);
271 int assertion_file_birthtime_recent(const char *, int, const char *);
272 int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
273 int assertion_file_contains_no_invalid_strings(const char *, int, const char *, const char **);
274 int assertion_file_contents(const char *, int, const void *, int, const char *);
275 int assertion_file_exists(const char *, int, const char *);
276 int assertion_file_mode(const char *, int, const char *, int);
277 int assertion_file_mtime(const char *, int, const char *, long, long);
278 int assertion_file_mtime_recent(const char *, int, const char *);
279 int assertion_file_nlinks(const char *, int, const char *, int);
280 int assertion_file_not_exists(const char *, int, const char *);
281 int assertion_file_size(const char *, int, const char *, long);
282 int assertion_is_dir(const char *, int, const char *, int);
283 int assertion_is_hardlink(const char *, int, const char *, const char *);
284 int assertion_is_not_hardlink(const char *, int, const char *, const char *);
285 int assertion_is_reg(const char *, int, const char *, int);
286 int assertion_is_symlink(const char *, int, const char *, const char *);
287 int assertion_make_dir(const char *, int, const char *, int);
288 int assertion_make_file(const char *, int, const char *, int, int, const void *);
289 int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
290 int assertion_make_symlink(const char *, int, const char *newpath, const char *);
291 int assertion_nodump(const char *, int, const char *);
292 int assertion_non_empty_file(const char *, int, const char *);
293 int assertion_text_file_contents(const char *, int, const char *buff, const char *f);
294 int assertion_umask(const char *, int, int);
295 int assertion_utimes(const char *, int, const char *, long, long, long, long );
297 void skipping_setup(const char *, int);
298 void test_skipping(const char *fmt, ...);
300 /* Like sprintf, then system() */
301 int systemf(const char * fmt, ...);
303 /* Delay until time() returns a value after this. */
304 void sleepUntilAfter(time_t);
306 /* Return true if this platform can create symlinks. */
307 int canSymlink(void);
309 /* Return true if this platform can run the "bzip2" program. */
312 /* Return true if this platform can run the "grzip" program. */
315 /* Return true if this platform can run the "gzip" program. */
318 /* Return true if this platform can run the specified command. */
319 int canRunCommand(const char *);
321 /* Return true if this platform can run the "lrzip" program. */
324 /* Return true if this platform can run the "lz4" program. */
327 /* Return true if this platform can run the "lzip" program. */
330 /* Return true if this platform can run the "lzma" program. */
333 /* Return true if this platform can run the "lzop" program. */
336 /* Return true if this platform can run the "xz" program. */
339 /* Return true if this filesystem can handle nodump flags. */
342 /* Return true if the file has large i-node number(>0xffffffff). */
343 int is_LargeInode(const char *);
345 /* Suck file into string allocated via malloc(). Call free() when done. */
346 /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
347 char *slurpfile(size_t *, const char *fmt, ...);
349 /* Dump block of bytes to a file. */
350 void dumpfile(const char *filename, void *, size_t);
352 /* Extracts named reference file to the current directory. */
353 void extract_reference_file(const char *);
354 /* Copies named reference file to the current directory. */
355 void copy_reference_file(const char *);
357 /* Extracts a list of files to the current directory.
358 * List must be NULL terminated.
360 void extract_reference_files(const char **);
362 /* Subtract umask from mode */
363 mode_t umasked(mode_t expected_mode);
365 /* Path to working directory for current test */
366 extern const char *testworkdir;
369 * Special interfaces for libarchive test harness.
373 #include "archive_entry.h"
376 struct archive_test_acl_t {
377 int type; /* Type of ACL */
378 int permset; /* Permissions for this class of users. */
379 int tag; /* Owner, User, Owning group, group, other, etc. */
380 int qual; /* GID or UID of user/group, depending on tag. */
381 const char *name; /* Name of user/group, depending on tag. */
385 void archive_test_set_acls(struct archive_entry *, struct archive_test_acl_t *,
389 void archive_test_compare_acls(struct archive_entry *,
390 struct archive_test_acl_t *, int, int, int);
392 /* Special customized read-from-memory interface. */
393 int read_open_memory(struct archive *, const void *, size_t, size_t);
394 /* _minimal version exercises a slightly different set of libarchive APIs. */
395 int read_open_memory_minimal(struct archive *, const void *, size_t, size_t);
396 /* _seek version produces a seekable file. */
397 int read_open_memory_seek(struct archive *, const void *, size_t, size_t);
399 /* Versions of above that accept an archive argument for additional info. */
400 #define assertA(e) assertion_assert(__FILE__, __LINE__, (e), #e, (a))
401 #define assertEqualIntA(a,v1,v2) \
402 assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))
403 #define assertEqualStringA(a,v1,v2) \
404 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a), 0)