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