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 28 /* Every test program should #include "test.h" as the first thing. */ 29 30 /* 31 * The goal of this file (and the matching test.c) is to 32 * simplify the very repetitive test-*.c test programs. 33 */ 34 #if defined(HAVE_CONFIG_H) 35 /* Most POSIX platforms use the 'configure' script to build config.h */ 36 #include "config.h" 37 #elif defined(__FreeBSD__) 38 /* Building as part of FreeBSD system requires a pre-built config.h. */ 39 #include "config_freebsd.h" 40 #elif defined(_WIN32) && !defined(__CYGWIN__) 41 /* Win32 can't run the 'configure' script. */ 42 #include "config_windows.h" 43 #else 44 /* Warn if the library hasn't been (automatically or manually) configured. */ 45 #error Oops: No config.h and no pre-built configuration in test.h. 46 #endif 47 48 #include <sys/types.h> /* Windows requires this before sys/stat.h */ 49 #include <sys/stat.h> 50 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_WINDOWS_H 76 #include <windows.h> 77 #endif 78 79 /* 80 * System-specific tweaks. We really want to minimize these 81 * as much as possible, since they make it harder to understand 82 * the mainline code. 83 */ 84 85 /* Windows (including Visual Studio and MinGW but not Cygwin) */ 86 #if defined(_WIN32) && !defined(__CYGWIN__) 87 #if !defined(__BORLANDC__) 88 #undef chdir 89 #define chdir _chdir 90 #define strdup _strdup 91 #endif 92 #endif 93 94 /* Visual Studio */ 95 #if defined(_MSC_VER) && _MSC_VER < 1900 96 #define snprintf sprintf_s 97 #endif 98 99 #if defined(__BORLANDC__) 100 #pragma warn -8068 /* Constant out of range in comparison. */ 101 #endif 102 103 /* Haiku OS and QNX */ 104 #if defined(__HAIKU__) || defined(__QNXNTO__) 105 /* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */ 106 #include <stdint.h> 107 #endif 108 109 /* Get a real definition for __FBSDID if we can */ 110 #if HAVE_SYS_CDEFS_H 111 #include <sys/cdefs.h> 112 #endif 113 114 /* If not, define it so as to avoid dangling semicolons. */ 115 #ifndef __FBSDID 116 #define __FBSDID(a) struct _undefined_hack 117 #endif 118 119 #ifndef O_BINARY 120 #define O_BINARY 0 121 #endif 122 123 /* 124 * Redefine DEFINE_TEST for use in defining the test functions. 125 */ 126 #undef DEFINE_TEST 127 #define DEFINE_TEST(name) void name(void); void name(void) 128 129 /* An implementation of the standard assert() macro */ 130 #define assert(e) assertion_assert(__FILE__, __LINE__, (e), #e, NULL) 131 /* chdir() and error if it fails */ 132 #define assertChdir(path) \ 133 assertion_chdir(__FILE__, __LINE__, path) 134 /* Assert two integers are the same. Reports value of each one if not. */ 135 #define assertEqualInt(v1,v2) \ 136 assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) 137 /* Assert two strings are the same. Reports value of each one if not. */ 138 #define assertEqualString(v1,v2) \ 139 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0) 140 #define assertEqualUTF8String(v1,v2) \ 141 assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1) 142 /* As above, but v1 and v2 are wchar_t * */ 143 #define assertEqualWString(v1,v2) \ 144 assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) 145 /* As above, but raw blocks of bytes. */ 146 #define assertEqualMem(v1, v2, l) \ 147 assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL) 148 /* Assert that memory is full of a specified byte */ 149 #define assertMemoryFilledWith(v1, l, b) \ 150 assertion_memory_filled_with(__FILE__, __LINE__, (v1), #v1, (l), #l, (b), #b, NULL) 151 /* Assert two files are the same. */ 152 #define assertEqualFile(f1, f2) \ 153 assertion_equal_file(__FILE__, __LINE__, (f1), (f2)) 154 /* Assert that a file is empty. */ 155 #define assertEmptyFile(pathname) \ 156 assertion_empty_file(__FILE__, __LINE__, (pathname)) 157 /* Assert that a file is not empty. */ 158 #define assertNonEmptyFile(pathname) \ 159 assertion_non_empty_file(__FILE__, __LINE__, (pathname)) 160 #define assertFileAtime(pathname, sec, nsec) \ 161 assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec) 162 #define assertFileAtimeRecent(pathname) \ 163 assertion_file_atime_recent(__FILE__, __LINE__, pathname) 164 #define assertFileBirthtime(pathname, sec, nsec) \ 165 assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec) 166 #define assertFileBirthtimeRecent(pathname) \ 167 assertion_file_birthtime_recent(__FILE__, __LINE__, pathname) 168 /* Assert that a file exists; supports printf-style arguments. */ 169 #define assertFileExists(pathname) \ 170 assertion_file_exists(__FILE__, __LINE__, pathname) 171 /* Assert that a file exists. */ 172 #define assertFileNotExists(pathname) \ 173 assertion_file_not_exists(__FILE__, __LINE__, pathname) 174 /* Assert that file contents match a string. */ 175 #define assertFileContents(data, data_size, pathname) \ 176 assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname) 177 #define assertFileMtime(pathname, sec, nsec) \ 178 assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec) 179 #define assertFileMtimeRecent(pathname) \ 180 assertion_file_mtime_recent(__FILE__, __LINE__, pathname) 181 #define assertFileNLinks(pathname, nlinks) \ 182 assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks) 183 #define assertFileSize(pathname, size) \ 184 assertion_file_size(__FILE__, __LINE__, pathname, size) 185 #define assertTextFileContents(text, pathname) \ 186 assertion_text_file_contents(__FILE__, __LINE__, text, pathname) 187 #define assertFileContainsLinesAnyOrder(pathname, lines) \ 188 assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines) 189 #define assertIsDir(pathname, mode) \ 190 assertion_is_dir(__FILE__, __LINE__, pathname, mode) 191 #define assertIsHardlink(path1, path2) \ 192 assertion_is_hardlink(__FILE__, __LINE__, path1, path2) 193 #define assertIsNotHardlink(path1, path2) \ 194 assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2) 195 #define assertIsReg(pathname, mode) \ 196 assertion_is_reg(__FILE__, __LINE__, pathname, mode) 197 #define assertIsSymlink(pathname, contents) \ 198 assertion_is_symlink(__FILE__, __LINE__, pathname, contents) 199 /* Create a directory, report error if it fails. */ 200 #define assertMakeDir(dirname, mode) \ 201 assertion_make_dir(__FILE__, __LINE__, dirname, mode) 202 #define assertMakeFile(path, mode, contents) \ 203 assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents) 204 #define assertMakeBinFile(path, mode, csize, contents) \ 205 assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents) 206 #define assertMakeHardlink(newfile, oldfile) \ 207 assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile) 208 #define assertMakeSymlink(newfile, linkto) \ 209 assertion_make_symlink(__FILE__, __LINE__, newfile, linkto) 210 #define assertNodump(path) \ 211 assertion_nodump(__FILE__, __LINE__, path) 212 #define assertUmask(mask) \ 213 assertion_umask(__FILE__, __LINE__, mask) 214 #define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec) \ 215 assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec) 216 217 /* 218 * This would be simple with C99 variadic macros, but I don't want to 219 * require that. Instead, I insert a function call before each 220 * skipping() call to pass the file and line information down. Crude, 221 * but effective. 222 */ 223 #define skipping \ 224 skipping_setup(__FILE__, __LINE__);test_skipping 225 226 /* Function declarations. These are defined in test_utility.c. */ 227 void failure(const char *fmt, ...); 228 int assertion_assert(const char *, int, int, const char *, void *); 229 int assertion_chdir(const char *, int, const char *); 230 int assertion_empty_file(const char *, int, const char *); 231 int assertion_equal_file(const char *, int, const char *, const char *); 232 int assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *); 233 int assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *); 234 int assertion_memory_filled_with(const char *, int, const void *, const char *, size_t, const char *, char, const char *, void *); 235 int assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int); 236 int assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *); 237 int assertion_file_atime(const char *, int, const char *, long, long); 238 int assertion_file_atime_recent(const char *, int, const char *); 239 int assertion_file_birthtime(const char *, int, const char *, long, long); 240 int assertion_file_birthtime_recent(const char *, int, const char *); 241 int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **); 242 int assertion_file_contents(const char *, int, const void *, int, const char *); 243 int assertion_file_exists(const char *, int, const char *); 244 int assertion_file_mode(const char *, int, const char *, int); 245 int assertion_file_mtime(const char *, int, const char *, long, long); 246 int assertion_file_mtime_recent(const char *, int, const char *); 247 int assertion_file_nlinks(const char *, int, const char *, int); 248 int assertion_file_not_exists(const char *, int, const char *); 249 int assertion_file_size(const char *, int, const char *, long); 250 int assertion_is_dir(const char *, int, const char *, int); 251 int assertion_is_hardlink(const char *, int, const char *, const char *); 252 int assertion_is_not_hardlink(const char *, int, const char *, const char *); 253 int assertion_is_reg(const char *, int, const char *, int); 254 int assertion_is_symlink(const char *, int, const char *, const char *); 255 int assertion_make_dir(const char *, int, const char *, int); 256 int assertion_make_file(const char *, int, const char *, int, int, const void *); 257 int assertion_make_hardlink(const char *, int, const char *newpath, const char *); 258 int assertion_make_symlink(const char *, int, const char *newpath, const char *); 259 int assertion_nodump(const char *, int, const char *); 260 int assertion_non_empty_file(const char *, int, const char *); 261 int assertion_text_file_contents(const char *, int, const char *buff, const char *f); 262 int assertion_umask(const char *, int, int); 263 int assertion_utimes(const char *, int, const char *, long, long, long, long ); 264 265 void skipping_setup(const char *, int); 266 void test_skipping(const char *fmt, ...); 267 268 /* Like sprintf, then system() */ 269 int systemf(const char * fmt, ...); 270 271 /* Delay until time() returns a value after this. */ 272 void sleepUntilAfter(time_t); 273 274 /* Return true if this platform can create symlinks. */ 275 int canSymlink(void); 276 277 /* Return true if this platform can run the "bzip2" program. */ 278 int canBzip2(void); 279 280 /* Return true if this platform can run the "grzip" program. */ 281 int canGrzip(void); 282 283 /* Return true if this platform can run the "gzip" program. */ 284 int canGzip(void); 285 286 /* Return true if this platform can run the specified command. */ 287 int canRunCommand(const char *); 288 289 /* Return true if this platform can run the "lrzip" program. */ 290 int canLrzip(void); 291 292 /* Return true if this platform can run the "lz4" program. */ 293 int canLz4(void); 294 295 /* Return true if this platform can run the "lzip" program. */ 296 int canLzip(void); 297 298 /* Return true if this platform can run the "lzma" program. */ 299 int canLzma(void); 300 301 /* Return true if this platform can run the "lzop" program. */ 302 int canLzop(void); 303 304 /* Return true if this platform can run the "xz" program. */ 305 int canXz(void); 306 307 /* Return true if this filesystem can handle nodump flags. */ 308 int canNodump(void); 309 310 /* Return true if the file has large i-node number(>0xffffffff). */ 311 int is_LargeInode(const char *); 312 313 /* Suck file into string allocated via malloc(). Call free() when done. */ 314 /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */ 315 char *slurpfile(size_t *, const char *fmt, ...); 316 317 /* Dump block of bytes to a file. */ 318 void dumpfile(const char *filename, void *, size_t); 319 320 /* Extracts named reference file to the current directory. */ 321 void extract_reference_file(const char *); 322 /* Copies named reference file to the current directory. */ 323 void copy_reference_file(const char *); 324 325 /* Extracts a list of files to the current directory. 326 * List must be NULL terminated. 327 */ 328 void extract_reference_files(const char **); 329 330 /* Subtract umask from mode */ 331 mode_t umasked(mode_t expected_mode); 332 333 /* Path to working directory for current test */ 334 extern const char *testworkdir; 335 336 /* 337 * Special interfaces for program test harness. 338 */ 339 340 /* Pathname of exe to be tested. */ 341 extern const char *testprogfile; 342 /* Name of exe to use in printf-formatted command strings. */ 343 /* On Windows, this includes leading/trailing quotes. */ 344 extern const char *testprog; 345 346 #ifdef USE_DMALLOC 347 #include <dmalloc.h> 348 #endif 349