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