xref: /freebsd/contrib/libarchive/test_utils/test_common.h (revision eb5165bb491138f60d9004bc4c781490016d9288)
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 /* change file/directory ownership and errors if it fails */
167 #define assertChown(pathname, user, group)			\
168   assertion_chown(__FILE__, __LINE__, pathname, user, group)
169 /* Assert two files have the same file flags */
170 #define assertEqualFflags(patha, pathb)	\
171   assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 0)
172 /* Assert two integers are the same.  Reports value of each one if not. */
173 #define assertEqualInt(v1,v2) \
174   assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
175 #define assertEqualAddress(v1,v2) \
176   assertion_equal_address(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
177 /* Assert two strings are the same.  Reports value of each one if not. */
178 #define assertEqualString(v1,v2)   \
179   assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0)
180 #define assertEqualUTF8String(v1,v2)   \
181   assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1)
182 /* As above, but v1 and v2 are wchar_t * */
183 #define assertEqualWString(v1,v2)   \
184   assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
185 /* As above, but raw blocks of bytes. */
186 #define assertEqualMem(v1, v2, l)	\
187   assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
188 /* Assert that memory is full of a specified byte */
189 #define assertMemoryFilledWith(v1, l, b)					\
190   assertion_memory_filled_with(__FILE__, __LINE__, (v1), #v1, (l), #l, (b), #b, NULL)
191 /* Assert two files are the same. */
192 #define assertEqualFile(f1, f2)	\
193   assertion_equal_file(__FILE__, __LINE__, (f1), (f2))
194 /* Assert that a file is empty. */
195 #define assertEmptyFile(pathname)	\
196   assertion_empty_file(__FILE__, __LINE__, (pathname))
197 /* Assert that a file is not empty. */
198 #define assertNonEmptyFile(pathname)		\
199   assertion_non_empty_file(__FILE__, __LINE__, (pathname))
200 #define assertFileAtime(pathname, sec, nsec)	\
201   assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
202 #define assertFileAtimeRecent(pathname)	\
203   assertion_file_atime_recent(__FILE__, __LINE__, pathname)
204 #define assertFileBirthtime(pathname, sec, nsec)	\
205   assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
206 #define assertFileBirthtimeRecent(pathname) \
207   assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
208 /* Assert that a file exists; supports printf-style arguments. */
209 #define assertFileExists(pathname) \
210   assertion_file_exists(__FILE__, __LINE__, pathname)
211 /* Assert that a file exists. */
212 #define assertFileNotExists(pathname) \
213   assertion_file_not_exists(__FILE__, __LINE__, pathname)
214 /* Assert that file contents match a string. */
215 #define assertFileContents(data, data_size, pathname) \
216   assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname)
217 /* Verify that a file does not contain invalid strings */
218 #define assertFileContainsNoInvalidStrings(pathname, strings) \
219   assertion_file_contains_no_invalid_strings(__FILE__, __LINE__, pathname, strings)
220 #define assertFileMtime(pathname, sec, nsec)	\
221   assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
222 #define assertFileMtimeRecent(pathname) \
223   assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
224 #define assertFileNLinks(pathname, nlinks)  \
225   assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
226 #define assertFileSize(pathname, size)  \
227   assertion_file_size(__FILE__, __LINE__, pathname, size)
228 #define assertFileMode(pathname, mode)  \
229   assertion_file_mode(__FILE__, __LINE__, pathname, mode)
230 #define assertTextFileContents(text, pathname) \
231   assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
232 #define assertFileContainsLinesAnyOrder(pathname, lines)	\
233   assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines)
234 #define assertIsDir(pathname, mode)		\
235   assertion_is_dir(__FILE__, __LINE__, pathname, mode)
236 #define assertIsHardlink(path1, path2)	\
237   assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
238 #define assertIsNotHardlink(path1, path2)	\
239   assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
240 #define assertIsReg(pathname, mode)		\
241   assertion_is_reg(__FILE__, __LINE__, pathname, mode)
242 #define assertIsSymlink(pathname, contents, isdir)	\
243   assertion_is_symlink(__FILE__, __LINE__, pathname, contents, isdir)
244 /* Create a directory, report error if it fails. */
245 #define assertMakeDir(dirname, mode)	\
246   assertion_make_dir(__FILE__, __LINE__, dirname, mode)
247 #define assertMakeFile(path, mode, contents) \
248   assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents)
249 #define assertMakeBinFile(path, mode, csize, contents) \
250   assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents)
251 #define assertMakeHardlink(newfile, oldfile)	\
252   assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
253 #define assertMakeSymlink(newfile, linkto, targetIsDir)	\
254   assertion_make_symlink(__FILE__, __LINE__, newfile, linkto, targetIsDir)
255 #define assertSetNodump(path)	\
256   assertion_set_nodump(__FILE__, __LINE__, path)
257 #define assertUmask(mask)	\
258   assertion_umask(__FILE__, __LINE__, mask)
259 /* Assert that two files have unequal file flags */
260 #define assertUnequalFflags(patha, pathb)	\
261   assertion_compare_fflags(__FILE__, __LINE__, patha, pathb, 1)
262 #define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec)	\
263   assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec)
264 #ifndef PROGRAM
265 #define assertEntrySetAcls(entry, acls, count) \
266   assertion_entry_set_acls(__FILE__, __LINE__, entry, acls, count)
267 #define assertEntryCompareAcls(entry, acls, count, type, mode) \
268   assertion_entry_compare_acls(__FILE__, __LINE__, entry, acls, count, type, mode)
269 #endif
270 
271 /*
272  * This would be simple with C99 variadic macros, but I don't want to
273  * require that.  Instead, I insert a function call before each
274  * skipping() call to pass the file and line information down.  Crude,
275  * but effective.
276  */
277 #define skipping	\
278   skipping_setup(__FILE__, __LINE__);test_skipping
279 
280 /* Function declarations.  These are defined in test_utility.c. */
281 void failure(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);
282 int assertion_assert(const char *, int, int, const char *, void *);
283 int assertion_chdir(const char *, int, const char *);
284 int assertion_chmod(const char *, int, const char *, int);
285 int assertion_chown(const char *, int, const char *, int, int);
286 int assertion_compare_fflags(const char *, int, const char *, const char *,
287     int);
288 int assertion_empty_file(const char *, int, const char *);
289 int assertion_equal_file(const char *, int, const char *, const char *);
290 int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
291 int assertion_equal_address(const char *, int, const void *, const char *, const void *, const char *, void *);
292 int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
293 int assertion_memory_filled_with(const char *, int, const void *, const char *, size_t, const char *, char, const char *, void *);
294 int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int);
295 int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
296 int assertion_file_atime(const char *, int, const char *, long, long);
297 int assertion_file_atime_recent(const char *, int, const char *);
298 int assertion_file_birthtime(const char *, int, const char *, long, long);
299 int assertion_file_birthtime_recent(const char *, int, const char *);
300 int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
301 int assertion_file_contains_no_invalid_strings(const char *, int, const char *, const char **);
302 int assertion_file_contents(const char *, int, const void *, int, const char *);
303 int assertion_file_exists(const char *, int, const char *);
304 int assertion_file_mode(const char *, int, const char *, int);
305 int assertion_file_mtime(const char *, int, const char *, long, long);
306 int assertion_file_mtime_recent(const char *, int, const char *);
307 int assertion_file_nlinks(const char *, int, const char *, int);
308 int assertion_file_not_exists(const char *, int, const char *);
309 int assertion_file_size(const char *, int, const char *, long);
310 int assertion_is_dir(const char *, int, const char *, int);
311 int assertion_is_hardlink(const char *, int, const char *, const char *);
312 int assertion_is_not_hardlink(const char *, int, const char *, const char *);
313 int assertion_is_reg(const char *, int, const char *, int);
314 int assertion_is_symlink(const char *, int, const char *, const char *, int);
315 int assertion_make_dir(const char *, int, const char *, int);
316 int assertion_make_file(const char *, int, const char *, int, int, const void *);
317 int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
318 int assertion_make_symlink(const char *, int, const char *newpath, const char *, int);
319 int assertion_non_empty_file(const char *, int, const char *);
320 int assertion_set_nodump(const char *, int, const char *);
321 int assertion_text_file_contents(const char *, int, const char *buff, const char *f);
322 int assertion_umask(const char *, int, int);
323 int assertion_utimes(const char *, int, const char *, time_t, suseconds_t, time_t, suseconds_t);
324 int assertion_version(const char*, int, const char *, const char *);
325 
326 void skipping_setup(const char *, int);
327 void test_skipping(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);
328 
329 /* Like sprintf, then system() */
330 int systemf(const char *fmt, ...) __LA_PRINTFLIKE(1, 2);
331 
332 /* Delay until time() returns a value after this. */
333 void sleepUntilAfter(time_t);
334 
335 /* Return true if this platform can create symlinks. */
336 int canSymlink(void);
337 
338 /* Return true if this platform can run the "bzip2" program. */
339 int canBzip2(void);
340 
341 /* Return true if this platform can run the "grzip" program. */
342 int canGrzip(void);
343 
344 /* Return true if this platform can run the "gzip" program. */
345 int canGzip(void);
346 
347 /* Return true if this platform can run the specified command.
348  *
349  * Result can be optionally cached with `*tested`:
350  *   - 0 if not tested yet
351  *   - <0 if already tested negative
352  *   - >0 if already tested positive
353  */
354 int canRunCommand(const char *cmd, int *tested);
355 
356 /* Return true if this platform can run the "lrzip" program. */
357 int canLrzip(void);
358 
359 /* Return true if this platform can run the "lz4" program. */
360 int canLz4(void);
361 
362 /* Return true if this platform can run the "zstd" program. */
363 int canZstd(void);
364 
365 /* Return true if this platform can run the "lzip" program. */
366 int canLzip(void);
367 
368 /* Return true if this platform can run the "lzma" program. */
369 int canLzma(void);
370 
371 /* Return true if this platform can run the "lzop" program. */
372 int canLzop(void);
373 
374 /* Return true if this platform can run the "xz" program. */
375 int canXz(void);
376 
377 /* Return true if this filesystem can handle nodump flags. */
378 int canNodump(void);
379 
380 /* Set test ACLs */
381 int setTestAcl(const char *path);
382 
383 /* Get extended attribute */
384 void *getXattr(const char *, const char *, size_t *);
385 
386 /* Set extended attribute */
387 int setXattr(const char *, const char *, const void *, size_t);
388 
389 /* Return true if the file has large i-node number(>0xffffffff). */
390 int is_LargeInode(const char *);
391 
392 #if ARCHIVE_ACL_SUNOS
393 /* Fetch ACLs on Solaris using acl() or facl() */
394 void *sunacl_get(int cmd, int *aclcnt, int fd, const char *path);
395 #endif
396 
397 /* Suck file into string allocated via malloc(). Call free() when done. */
398 /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
399 char *slurpfile(size_t *, const char *fmt, ...) __LA_PRINTFLIKE(2, 3);
400 
401 /* Dump block of bytes to a file. */
402 void dumpfile(const char *filename, void *, size_t);
403 
404 /* Extracts named reference file to the current directory. */
405 void extract_reference_file(const char *);
406 /* Copies named reference file to the current directory. */
407 void copy_reference_file(const char *);
408 
409 /* Extracts a list of files to the current directory.
410  * List must be NULL terminated.
411  */
412 void extract_reference_files(const char **);
413 
414 /* Subtract umask from mode */
415 mode_t umasked(mode_t expected_mode);
416 
417 /* Path to working directory for current test */
418 extern const char *testworkdir;
419 
420 #ifndef PROGRAM
421 /*
422  * Special interfaces for libarchive test harness.
423  */
424 
425 #include "archive.h"
426 #include "archive_entry.h"
427 
428 /* ACL structure */
429 struct archive_test_acl_t {
430 	int type;  /* Type of ACL */
431 	int permset; /* Permissions for this class of users. */
432 	int tag; /* Owner, User, Owning group, group, other, etc. */
433 	int qual; /* GID or UID of user/group, depending on tag. */
434 	const char *name; /* Name of user/group, depending on tag. */
435 };
436 
437 /* Set ACLs */
438 int assertion_entry_set_acls(const char *, int, struct archive_entry *,
439     struct archive_test_acl_t *, int);
440 
441 /* Compare ACLs */
442 int assertion_entry_compare_acls(const char *, int, struct archive_entry *,
443     struct archive_test_acl_t *, int, int, int);
444 
445 /* Special customized read-from-memory interface. */
446 int read_open_memory(struct archive *, const void *, size_t, size_t);
447 /* _minimal version exercises a slightly different set of libarchive APIs. */
448 int read_open_memory_minimal(struct archive *, const void *, size_t, size_t);
449 /* _seek version produces a seekable file. */
450 int read_open_memory_seek(struct archive *, const void *, size_t, size_t);
451 
452 /* Versions of above that accept an archive argument for additional info. */
453 #define assertA(e)   assertion_assert(__FILE__, __LINE__, (e), #e, (a))
454 #define assertEqualIntA(a,v1,v2)   \
455   assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))
456 #define assertEqualStringA(a,v1,v2)   \
457   assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a), 0)
458 
459 #else	/* defined(PROGRAM) */
460 /*
461  * Special interfaces for program test harness.
462  */
463 
464 /* Pathname of exe to be tested. */
465 extern const char *testprogfile;
466 /* Name of exe to use in printf-formatted command strings. */
467 /* On Windows, this includes leading/trailing quotes. */
468 extern const char *testprog;
469 
470 void assertVersion(const char *prog, const char *base);
471 
472 #endif	/* defined(PROGRAM) */
473 
474 #ifdef USE_DMALLOC
475 #include <dmalloc.h>
476 #endif
477 
478 #include "test_utils.h"
479 
480 #endif	/* !TEST_COMMON_H */
481