/usr.bin/cpio/test/test.h

https://bitbucket.org/freebsd/freebsd-head/ · C Header · 285 lines · 172 code · 26 blank · 87 comment · 3 complexity · db718f083e2a578e24b61eca0e16b3b5 MD5 · raw file

  1. /*
  2. * Copyright (c) 2003-2006 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. /* Every test program should #include "test.h" as the first thing. */
  28. /*
  29. * The goal of this file (and the matching test.c) is to
  30. * simplify the very repetitive test-*.c test programs.
  31. */
  32. #if defined(HAVE_CONFIG_H)
  33. /* Most POSIX platforms use the 'configure' script to build config.h */
  34. #include "config.h"
  35. #elif defined(__FreeBSD__)
  36. /* Building as part of FreeBSD system requires a pre-built config.h. */
  37. #include "config_freebsd.h"
  38. #elif defined(_WIN32) && !defined(__CYGWIN__)
  39. /* Win32 can't run the 'configure' script. */
  40. #include "config_windows.h"
  41. #else
  42. /* Warn if the library hasn't been (automatically or manually) configured. */
  43. #error Oops: No config.h and no pre-built configuration in test.h.
  44. #endif
  45. #include <sys/types.h> /* Windows requires this before sys/stat.h */
  46. #include <sys/stat.h>
  47. #ifdef USE_DMALLOC
  48. #include <dmalloc.h>
  49. #endif
  50. #if HAVE_DIRENT_H
  51. #include <dirent.h>
  52. #endif
  53. #ifdef HAVE_DIRECT_H
  54. #include <direct.h>
  55. #define dirent direct
  56. #endif
  57. #include <errno.h>
  58. #include <fcntl.h>
  59. #ifdef HAVE_IO_H
  60. #include <io.h>
  61. #endif
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <time.h>
  66. #ifdef HAVE_UNISTD_H
  67. #include <unistd.h>
  68. #endif
  69. #include <wchar.h>
  70. #ifdef HAVE_WINDOWS_H
  71. #include <windows.h>
  72. #endif
  73. /*
  74. * System-specific tweaks. We really want to minimize these
  75. * as much as possible, since they make it harder to understand
  76. * the mainline code.
  77. */
  78. /* Windows (including Visual Studio and MinGW but not Cygwin) */
  79. #if defined(_WIN32) && !defined(__CYGWIN__)
  80. #include "../cpio_windows.h"
  81. #if !defined(__BORLANDC__)
  82. #define strdup _strdup
  83. #endif
  84. #define LOCALE_DE "deu"
  85. #else
  86. #define LOCALE_DE "de_DE.UTF-8"
  87. #endif
  88. /* Visual Studio */
  89. #ifdef _MSC_VER
  90. #define snprintf sprintf_s
  91. #endif
  92. /* Cygwin */
  93. #if defined(__CYGWIN__)
  94. /* Cygwin-1.7.x is lazy about populating nlinks, so don't
  95. * expect it to be accurate. */
  96. # define NLINKS_INACCURATE_FOR_DIRS
  97. #endif
  98. #if defined(__HAIKU__) || defined(__QNXNTO__)
  99. /* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */
  100. #include <stdint.h>
  101. #endif
  102. /* Get a real definition for __FBSDID if we can */
  103. #if HAVE_SYS_CDEFS_H
  104. #include <sys/cdefs.h>
  105. #endif
  106. /* If not, define it so as to avoid dangling semicolons. */
  107. #ifndef __FBSDID
  108. #define __FBSDID(a) struct _undefined_hack
  109. #endif
  110. #ifndef O_BINARY
  111. #define O_BINARY 0
  112. #endif
  113. /*
  114. * Redefine DEFINE_TEST for use in defining the test functions.
  115. */
  116. #undef DEFINE_TEST
  117. #define DEFINE_TEST(name) void name(void); void name(void)
  118. /* An implementation of the standard assert() macro */
  119. #define assert(e) assertion_assert(__FILE__, __LINE__, (e), #e, NULL)
  120. /* chdir() and error if it fails */
  121. #define assertChdir(path) \
  122. assertion_chdir(__FILE__, __LINE__, path)
  123. /* Assert two integers are the same. Reports value of each one if not. */
  124. #define assertEqualInt(v1,v2) \
  125. assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
  126. /* Assert two strings are the same. Reports value of each one if not. */
  127. #define assertEqualString(v1,v2) \
  128. assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
  129. /* As above, but v1 and v2 are wchar_t * */
  130. #define assertEqualWString(v1,v2) \
  131. assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
  132. /* As above, but raw blocks of bytes. */
  133. #define assertEqualMem(v1, v2, l) \
  134. assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
  135. /* Assert two files are the same; allow printf-style expansion of second name.
  136. * See below for comments about variable arguments here...
  137. */
  138. #define assertEqualFile \
  139. assertion_setup(__FILE__, __LINE__);assertion_equal_file
  140. /* Assert that a file is empty; supports printf-style arguments. */
  141. #define assertEmptyFile \
  142. assertion_setup(__FILE__, __LINE__);assertion_empty_file
  143. /* Assert that a file is not empty; supports printf-style arguments. */
  144. #define assertNonEmptyFile \
  145. assertion_setup(__FILE__, __LINE__);assertion_non_empty_file
  146. #define assertFileAtime(pathname, sec, nsec) \
  147. assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec)
  148. #define assertFileAtimeRecent(pathname) \
  149. assertion_file_atime_recent(__FILE__, __LINE__, pathname)
  150. #define assertFileBirthtime(pathname, sec, nsec) \
  151. assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec)
  152. #define assertFileBirthtimeRecent(pathname) \
  153. assertion_file_birthtime_recent(__FILE__, __LINE__, pathname)
  154. /* Assert that a file exists; supports printf-style arguments. */
  155. #define assertFileExists \
  156. assertion_setup(__FILE__, __LINE__);assertion_file_exists
  157. /* Assert that a file exists; supports printf-style arguments. */
  158. #define assertFileNotExists \
  159. assertion_setup(__FILE__, __LINE__);assertion_file_not_exists
  160. /* Assert that file contents match a string; supports printf-style arguments. */
  161. #define assertFileContents \
  162. assertion_setup(__FILE__, __LINE__);assertion_file_contents
  163. #define assertFileMtime(pathname, sec, nsec) \
  164. assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec)
  165. #define assertFileMtimeRecent(pathname) \
  166. assertion_file_mtime_recent(__FILE__, __LINE__, pathname)
  167. #define assertFileNLinks(pathname, nlinks) \
  168. assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
  169. #define assertFileSize(pathname, size) \
  170. assertion_file_size(__FILE__, __LINE__, pathname, size)
  171. #define assertTextFileContents \
  172. assertion_setup(__FILE__, __LINE__);assertion_text_file_contents
  173. #define assertIsDir(pathname, mode) \
  174. assertion_is_dir(__FILE__, __LINE__, pathname, mode)
  175. #define assertIsHardlink(path1, path2) \
  176. assertion_is_hardlink(__FILE__, __LINE__, path1, path2)
  177. #define assertIsNotHardlink(path1, path2) \
  178. assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2)
  179. #define assertIsReg(pathname, mode) \
  180. assertion_is_reg(__FILE__, __LINE__, pathname, mode)
  181. #define assertIsSymlink(pathname, contents) \
  182. assertion_is_symlink(__FILE__, __LINE__, pathname, contents)
  183. /* Create a directory, report error if it fails. */
  184. #define assertMakeDir(dirname, mode) \
  185. assertion_make_dir(__FILE__, __LINE__, dirname, mode)
  186. #define assertMakeFile(path, mode, contents) \
  187. assertion_make_file(__FILE__, __LINE__, path, mode, contents)
  188. #define assertMakeHardlink(newfile, oldfile) \
  189. assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile)
  190. #define assertMakeSymlink(newfile, linkto) \
  191. assertion_make_symlink(__FILE__, __LINE__, newfile, linkto)
  192. #define assertUmask(mask) \
  193. assertion_umask(__FILE__, __LINE__, mask)
  194. /*
  195. * This would be simple with C99 variadic macros, but I don't want to
  196. * require that. Instead, I insert a function call before each
  197. * skipping() call to pass the file and line information down. Crude,
  198. * but effective.
  199. */
  200. #define skipping \
  201. assertion_setup(__FILE__, __LINE__);test_skipping
  202. /* Function declarations. These are defined in test_utility.c. */
  203. void failure(const char *fmt, ...);
  204. int assertion_assert(const char *, int, int, const char *, void *);
  205. int assertion_chdir(const char *, int, const char *);
  206. int assertion_empty_file(const char *, ...);
  207. int assertion_equal_file(const char *, const char *, ...);
  208. int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *);
  209. int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *);
  210. int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
  211. int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
  212. int assertion_file_atime(const char *, int, const char *, long, long);
  213. int assertion_file_atime_recent(const char *, int, const char *);
  214. int assertion_file_birthtime(const char *, int, const char *, long, long);
  215. int assertion_file_birthtime_recent(const char *, int, const char *);
  216. int assertion_file_contents(const void *, int, const char *, ...);
  217. int assertion_file_exists(const char *, ...);
  218. int assertion_file_mtime(const char *, int, const char *, long, long);
  219. int assertion_file_mtime_recent(const char *, int, const char *);
  220. int assertion_file_nlinks(const char *, int, const char *, int);
  221. int assertion_file_not_exists(const char *, ...);
  222. int assertion_file_size(const char *, int, const char *, long);
  223. int assertion_is_dir(const char *, int, const char *, int);
  224. int assertion_is_hardlink(const char *, int, const char *, const char *);
  225. int assertion_is_not_hardlink(const char *, int, const char *, const char *);
  226. int assertion_is_reg(const char *, int, const char *, int);
  227. int assertion_is_symlink(const char *, int, const char *, const char *);
  228. int assertion_make_dir(const char *, int, const char *, int);
  229. int assertion_make_file(const char *, int, const char *, int, const char *);
  230. int assertion_make_hardlink(const char *, int, const char *newpath, const char *);
  231. int assertion_make_symlink(const char *, int, const char *newpath, const char *);
  232. int assertion_non_empty_file(const char *, ...);
  233. int assertion_text_file_contents(const char *buff, const char *f);
  234. int assertion_umask(const char *, int, int);
  235. void assertion_setup(const char *, int);
  236. void test_skipping(const char *fmt, ...);
  237. /* Like sprintf, then system() */
  238. int systemf(const char * fmt, ...);
  239. /* Delay until time() returns a value after this. */
  240. void sleepUntilAfter(time_t);
  241. /* Return true if this platform can create symlinks. */
  242. int canSymlink(void);
  243. /* Return true if this platform can run the "gzip" program. */
  244. int canGzip(void);
  245. /* Return true if this platform can run the "gunzip" program. */
  246. int canGunzip(void);
  247. /* Suck file into string allocated via malloc(). Call free() when done. */
  248. /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
  249. char *slurpfile(size_t *, const char *fmt, ...);
  250. /* Extracts named reference file to the current directory. */
  251. void extract_reference_file(const char *);
  252. /*
  253. * Special interfaces for program test harness.
  254. */
  255. /* Pathname of exe to be tested. */
  256. const char *testprogfile;
  257. /* Name of exe to use in printf-formatted command strings. */
  258. /* On Windows, this includes leading/trailing quotes. */
  259. const char *testprog;