xref: /freebsd/contrib/libarchive/test_utils/test_common.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*
2  * Copyright (c) 2003-2017 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 
26 #ifndef	TEST_COMMON_H
27 #define	TEST_COMMON_H
28 
29 /*
30  * The goal of this file (and the matching test.c) is to
31  * simplify the very repetitive test-*.c test programs.
32  */
33 #if defined(HAVE_CONFIG_H)
34 /* Most POSIX platforms use the 'configure' script to build config.h */
35 #include "config.h"
36 #elif defined(__FreeBSD__)
37 /* Building as part of FreeBSD system requires a pre-built config.h. */
38 #include "config_freebsd.h"
39 #elif defined(__NetBSD__)
40 /* Building as part of NetBSD system requires a pre-built config.h. */
41 #include "config_netbsd.h"
42 #elif defined(_WIN32) && !defined(__CYGWIN__)
43 /* Win32 can't run the 'configure' script. */
44 #include "config_windows.h"
45 #else
46 /* Warn if the library hasn't been (automatically or manually) configured. */
47 #error Oops: No config.h and no pre-built configuration in test.h.
48 #endif
49 
50 #include <sys/types.h>  /* Windows requires this before sys/stat.h */
51 #if !HAVE_SUSECONDS_T
52 #define suseconds_t long
53 #endif
54 #include <sys/stat.h>
55 
56 #if HAVE_DIRENT_H
57 #include <dirent.h>
58 #endif
59 #ifdef HAVE_DIRECT_H
60 #include <direct.h>
61 #define dirent direct
62 #endif
63 #include <errno.h>
64 #include <fcntl.h>
65 #ifdef HAVE_IO_H
66 #include <io.h>
67 #endif
68 #ifdef HAVE_STDINT_H
69 #include <stdint.h>
70 #endif
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <ctype.h>
75 #include <time.h>
76 #ifdef HAVE_UNISTD_H
77 #include <unistd.h>
78 #endif
79 #include <wchar.h>
80 #ifdef HAVE_ACL_LIBACL_H
81 #include <acl/libacl.h>
82 #endif
83 #ifdef HAVE_SYS_ACL_H
84 #include <sys/acl.h>
85 #endif
86 #ifdef HAVE_SYS_RICHACL_H
87 #include <sys/richacl.h>
88 #endif
89 #ifdef HAVE_WINDOWS_H
90 #define NOCRYPT
91 #include <windows.h>
92 #include <winioctl.h>
93 #endif
94 
95 /*
96  * System-specific tweaks.  We really want to minimize these
97  * as much as possible, since they make it harder to understand
98  * the mainline code.
99  */
100 
101 /* Windows (including Visual Studio and MinGW but not Cygwin) */
102 #if defined(_WIN32) && !defined(__CYGWIN__)
103 #if !defined(__BORLANDC__)
104 #undef chdir
105 #define chdir _chdir
106 #define strdup _strdup
107 #endif
108 #endif
109 
110 /* Visual Studio */
111 #if defined(_MSC_VER) && _MSC_VER < 1900
112 #define snprintf	sprintf_s
113 #endif
114 
115 #if defined(__BORLANDC__)
116 #pragma warn -8068	/* Constant out of range in comparison. */
117 #endif
118 
119 
120 #if defined(__GNUC__) && (__GNUC__ > 2 || \
121 			  (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
122 # ifdef __MINGW_PRINTF_FORMAT
123 #  define __LA_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
124 # else
125 #  define __LA_PRINTF_FORMAT __printf__
126 # endif
127 # define __LA_PRINTFLIKE(f,a)	__attribute__((__format__(__LA_PRINTF_FORMAT, f, a)))
128 #else
129 # define __LA_PRINTFLIKE(f,a)
130 #endif
131 
132 /* Haiku OS and QNX */
133 #if defined(__HAIKU__) || defined(__QNXNTO__)
134 /* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */
135 #include <stdint.h>
136 #endif
137 
138 #ifndef O_BINARY
139 #define	O_BINARY 0
140 #endif
141 
142 #ifndef __LIBARCHIVE_TEST_COMMON
143 #define __LIBARCHIVE_TEST_COMMON
144 #endif
145 
146 #include "archive_platform_acl.h"
147 #define	ARCHIVE_TEST_ACL_TYPE_POSIX1E	1
148 #define	ARCHIVE_TEST_ACL_TYPE_NFS4	2
149 
150 #include "archive_platform_xattr.h"
151 
152 /*
153  * Redefine DEFINE_TEST for use in defining the test functions.
154  */
155 #undef DEFINE_TEST
156 #define DEFINE_TEST(name) void name(void); void name(void)
157 
158 /* An implementation of the standard assert() macro */
159 #define assert(e)   assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
160 /* chdir() and error if it fails */
161 #define assertChdir(path)  \
162   assertion_chdir(__FILE__, __LINE__, path)
163 /* change file/directory permissions and errors if it fails */
164 #define assertChmod(pathname, mode)				\
165   assertion_chmod(__FILE__, __LINE__, pathname, mode)
166 /* Assert two files have the same file flags */
167 #define assertEqualFflags(patha, pathb)	\
168   assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 0)
169 /* Assert two integers are the same.  Reports value of each one if not. */
170 #define assertEqualInt(v1,v2) \
171   assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
172 #define assertEqualAddress(v1,v2) \
173   assertion_equal_address(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
174 /* Assert two strings are the same.  Reports value of each one if not. */
175 #define assertEqualString(v1,v2)   \
176   assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0)
177 #define assertEqualUTF8String(v1,v2)   \
178   assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1)
179 /* As above, but v1 and v2 are wchar_t * */
180 #define assertEqualWString(v1,v2)   \
181   assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
182 /* As above, but raw blocks of bytes. */
183 #define assertEqualMem(v1, v2, l)	\
184   assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
185 /* Assert that memory is full of a specified byte */
186 #define assertMemoryFilledWith(v1, l, b)					\
187   assertion_memory_filled_with(__FILE__, __LINE__, (v1), #v1, (l), #l, (b), #b, NULL)
188 /* Assert two files are the same. */
189 #define assertEqualFile(f1, f2)	\
190   assertion_equal_file(__FILE__, __LINE__, (f1), (f2))
191 /* Assert that a file is empty. */
192 #define assertEmptyFile(pathname)	\
193   assertion_empty_file(__FILE__, __LINE__, (pathname))
194 /* Assert that a file is not empty. */
195 #define assertNonEmptyFile(pathname)		\
196   assertion_non_empty_file(__FILE__, __LINE__, (pathname))
197 #define assertFileAtime(pathname, sec, nsec)	\
198   assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
199 #define assertFileAtimeRecent(pathname)	\
200   assertion_file_atime_recent(__FILE__, __LINE__, pathname)
201 #define assertFileBirthtime(pathname, sec, nsec)	\
202   assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
203 #define assertFileBirthtimeRecent(pathname) \
204   assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
205 /* Assert that a file exists; supports printf-style arguments. */
206 #define assertFileExists(pathname) \
207   assertion_file_exists(__FILE__, __LINE__, pathname)
208 /* Assert that a file exists. */
209 #define assertFileNotExists(pathname) \
210   assertion_file_not_exists(__FILE__, __LINE__, pathname)
211 /* Assert that file contents match a string. */
212 #define assertFileContents(data, data_size, pathname) \
213   assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname)
214 /* Verify that a file does not contain invalid strings */
215 #define assertFileContainsNoInvalidStrings(pathname, strings) \
216   assertion_file_contains_no_invalid_strings(__FILE__, __LINE__, pathname, strings)
217 #define assertFileMtime(pathname, sec, nsec)	\
218   assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
219 #define assertFileMtimeRecent(pathname) \
220   assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
221 #define assertFileNLinks(pathname, nlinks)  \
222   assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
223 #define assertFileSize(pathname, size)  \
224   assertion_file_size(__FILE__, __LINE__, pathname, size)
225 #define assertFileMode(pathname, mode)  \
226   assertion_file_mode(__FILE__, __LINE__, pathname, mode)
227 #define assertTextFileContents(text, pathname) \
228   assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
229 #define assertFileContainsLinesAnyOrder(pathname, lines)	\
230   assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)
231 #define assertIsDir(pathname, mode)		\
232   assertion_is_dir(__FILE__, __LINE__, pathname, mode)
233 #define assertIsHardlink(path1, path2)	\
234   assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
235 #define assertIsNotHardlink(path1, path2)	\
236   assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
237 #define assertIsReg(pathname, mode)		\
238   assertion_is_reg(__FILE__, __LINE__, pathname, mode)
239 #define assertIsSymlink(pathname, contents, isdir)	\
240   assertion_is_symlink(__FILE__, __LINE__, pathname, contents, isdir)
241 /* Create a directory, report error if it fails. */
242 #define assertMakeDir(dirname, mode)	\
243   assertion_make_dir(__FILE__, __LINE__, dirname, mode)
244 #define assertMakeFile(path, mode, contents) \
245   assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
246 #define assertMakeBinFile(path, mode, csize, contents) \
247   assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
248 #define assertMakeHardlink(newfile, oldfile)	\
249   assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
250 #define assertMakeSymlink(newfile, linkto, targetIsDir)	\
251   assertion_make_symlink(__FILE__, __LINE__, newfile, linkto, targetIsDir)
252 #define assertSetNodump(path)	\
253   assertion_set_nodump(__FILE__, __LINE__, path)
254 #define assertUmask(mask)	\
255   assertion_umask(__FILE__, __LINE__, mask)
256 /* Assert that two files have unequal file flags */
257 #define assertUnequalFflags(patha, pathb)	\
258   assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 1)
259 #define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec)	\
260   assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec)
261 #ifndef PROGRAM
262 #define assertEntrySetAcls(entry, acls, count) \
263   assertion_entry_set_acls(__FILE__, __LINE__, entry, acls, count)
264 #define assertEntryCompareAcls(entry, acls, count, type, mode) \
265   assertion_entry_compare_acls(__FILE__, __LINE__, entry, acls, count, type, mode)
266 #endif
267 
268 /*
269  * This would be simple with C99 variadic macros, but I don't want to
270  * require that.  Instead, I insert a function call before each
271  * skipping() call to pass the file and line information down.  Crude,
272  * but effective.
273  */
274 #define skipping	\
275   skipping_setup(__FILE__, __LINE__);test_skipping
276 
277 /* Function declarations.  These are defined in test_utility.c. */
278 void failure(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);
279 int assertion_assert(const char *, int, int, const char *, void *);
280 int assertion_chdir(const char *, int, const char *);
281 int assertion_chmod(const char *, int, const char *, int);
282 int assertion_compare_fflags(const char *, int, const char *, const char *,
283     int);
284 int assertion_empty_file(const char *, int, const char *);
285 int assertion_equal_file(const char *, int, const char *, const char *);
286 int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
287 int assertion_equal_address(const char *, int, const void *, const char *, const void *, const char *, void *);
288 int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
289 int assertion_memory_filled_with(const char *, int, const void *, const char *, size_t, const char *, char, const char *, void *);
290 int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int);
291 int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
292 int assertion_file_atime(const char *, int, const char *, long, long);
293 int assertion_file_atime_recent(const char *, int, const char *);
294 int assertion_file_birthtime(const char *, int, const char *, long, long);
295 int assertion_file_birthtime_recent(const char *, int, const char *);
296 int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
297 int assertion_file_contains_no_invalid_strings(const char *, int, const char *, const char **);
298 int assertion_file_contents(const char *, int, const void *, int, const char *);
299 int assertion_file_exists(const char *, int, const char *);
300 int assertion_file_mode(const char *, int, const char *, int);
301 int assertion_file_mtime(const char *, int, const char *, long, long);
302 int assertion_file_mtime_recent(const char *, int, const char *);
303 int assertion_file_nlinks(const char *, int, const char *, int);
304 int assertion_file_not_exists(const char *, int, const char *);
305 int assertion_file_size(const char *, int, const char *, long);
306 int assertion_is_dir(const char *, int, const char *, int);
307 int assertion_is_hardlink(const char *, int, const char *, const char *);
308 int assertion_is_not_hardlink(const char *, int, const char *, const char *);
309 int assertion_is_reg(const char *, int, const char *, int);
310 int assertion_is_symlink(const char *, int, const char *, const char *, int);
311 int assertion_make_dir(const char *, int, const char *, int);
312 int assertion_make_file(const char *, int, const char *, int, int, const void *);
313 int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
314 int assertion_make_symlink(const char *, int, const char *newpath, const char *, int);
315 int assertion_non_empty_file(const char *, int, const char *);
316 int assertion_set_nodump(const char *, int, const char *);
317 int assertion_text_file_contents(const char *, int, const char *buff, const char *f);
318 int assertion_umask(const char *, int, int);
319 int assertion_utimes(const char *, int, const char *, time_t, suseconds_t, time_t, suseconds_t);
320 int assertion_version(const char*, int, const char *, const char *);
321 
322 void skipping_setup(const char *, int);
323 void test_skipping(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);
324 
325 /* Like sprintf, then system() */
326 int systemf(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);
327 
328 /* Delay until time() returns a value after this. */
329 void sleepUntilAfter(time_t);
330 
331 /* Return true if this platform can create symlinks. */
332 int canSymlink(void);
333 
334 /* Return true if this platform can run the "bzip2" program. */
335 int canBzip2(void);
336 
337 /* Return true if this platform can run the "grzip" program. */
338 int canGrzip(void);
339 
340 /* Return true if this platform can run the "gzip" program. */
341 int canGzip(void);
342 
343 /* Return true if this platform can run the specified command. */
344 int canRunCommand(const char *);
345 
346 /* Return true if this platform can run the "lrzip" program. */
347 int canLrzip(void);
348 
349 /* Return true if this platform can run the "lz4" program. */
350 int canLz4(void);
351 
352 /* Return true if this platform can run the "zstd" program. */
353 int canZstd(void);
354 
355 /* Return true if this platform can run the "lzip" program. */
356 int canLzip(void);
357 
358 /* Return true if this platform can run the "lzma" program. */
359 int canLzma(void);
360 
361 /* Return true if this platform can run the "lzop" program. */
362 int canLzop(void);
363 
364 /* Return true if this platform can run the "xz" program. */
365 int canXz(void);
366 
367 /* Return true if this filesystem can handle nodump flags. */
368 int canNodump(void);
369 
370 /* Set test ACLs */
371 int setTestAcl(const char *path);
372 
373 /* Get extended attribute */
374 void *getXattr(const char *, const char *, size_t *);
375 
376 /* Set extended attribute */
377 int setXattr(const char *, const char *, const void *, size_t);
378 
379 /* Return true if the file has large i-node number(>0xffffffff). */
380 int is_LargeInode(const char *);
381 
382 #if ARCHIVE_ACL_SUNOS
383 /* Fetch ACLs on Solaris using acl() or facl() */
384 void *sunacl_get(int cmd, int *aclcnt, int fd, const char *path);
385 #endif
386 
387 /* Suck file into string allocated via malloc(). Call free() when done. */
388 /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
389 char *slurpfile(size_t *, const char *fmt, ...) __LA_PRINTFLIKE(2, 3);
390 
391 /* Dump block of bytes to a file. */
392 void dumpfile(const char *filename, void *, size_t);
393 
394 /* Extracts named reference file to the current directory. */
395 void extract_reference_file(const char *);
396 /* Copies named reference file to the current directory. */
397 void copy_reference_file(const char *);
398 
399 /* Extracts a list of files to the current directory.
400  * List must be NULL terminated.
401  */
402 void extract_reference_files(const char **);
403 
404 /* Subtract umask from mode */
405 mode_t umasked(mode_t expected_mode);
406 
407 /* Path to working directory for current test */
408 extern const char *testworkdir;
409 
410 #ifndef PROGRAM
411 /*
412  * Special interfaces for libarchive test harness.
413  */
414 
415 #include "archive.h"
416 #include "archive_entry.h"
417 
418 /* ACL structure */
419 struct archive_test_acl_t {
420 	int type;  /* Type of ACL */
421 	int permset; /* Permissions for this class of users. */
422 	int tag; /* Owner, User, Owning group, group, other, etc. */
423 	int qual; /* GID or UID of user/group, depending on tag. */
424 	const char *name; /* Name of user/group, depending on tag. */
425 };
426 
427 /* Set ACLs */
428 int assertion_entry_set_acls(const char *, int, struct archive_entry *,
429     struct archive_test_acl_t *, int);
430 
431 /* Compare ACLs */
432 int assertion_entry_compare_acls(const char *, int, struct archive_entry *,
433     struct archive_test_acl_t *, int, int, int);
434 
435 /* Special customized read-from-memory interface. */
436 int read_open_memory(struct archive *, const void *, size_t, size_t);
437 /* _minimal version exercises a slightly different set of libarchive APIs. */
438 int read_open_memory_minimal(struct archive *, const void *, size_t, size_t);
439 /* _seek version produces a seekable file. */
440 int read_open_memory_seek(struct archive *, const void *, size_t, size_t);
441 
442 /* Versions of above that accept an archive argument for additional info. */
443 #define assertA(e)   assertion_assert(__FILE__, __LINE__, (e), #e, (a))
444 #define assertEqualIntA(a,v1,v2)   \
445   assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))
446 #define assertEqualStringA(a,v1,v2)   \
447   assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a), 0)
448 
449 #else	/* defined(PROGRAM) */
450 /*
451  * Special interfaces for program test harness.
452  */
453 
454 /* Pathname of exe to be tested. */
455 extern const char *testprogfile;
456 /* Name of exe to use in printf-formatted command strings. */
457 /* On Windows, this includes leading/trailing quotes. */
458 extern const char *testprog;
459 
460 void assertVersion(const char *prog, const char *base);
461 
462 #endif	/* defined(PROGRAM) */
463 
464 #ifdef USE_DMALLOC
465 #include <dmalloc.h>
466 #endif
467 
468 #include "test_utils.h"
469 
470 #endif	/* TEST_COMMON_H */
471