test_common.h 18 KB

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