1228753Smm/* 2228753Smm * Copyright (c) 2003-2006 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm * 25228763Smm * $FreeBSD$ 26228753Smm */ 27228753Smm 28228753Smm/* Every test program should #include "test.h" as the first thing. */ 29228753Smm 30228753Smm/* 31228753Smm * The goal of this file (and the matching test.c) is to 32228753Smm * simplify the very repetitive test-*.c test programs. 33228753Smm */ 34228753Smm#if defined(HAVE_CONFIG_H) 35228753Smm/* Most POSIX platforms use the 'configure' script to build config.h */ 36228753Smm#include "config.h" 37228753Smm#elif defined(__FreeBSD__) 38228753Smm/* Building as part of FreeBSD system requires a pre-built config.h. */ 39228753Smm#include "config_freebsd.h" 40228753Smm#elif defined(_WIN32) && !defined(__CYGWIN__) 41228753Smm/* Win32 can't run the 'configure' script. */ 42228753Smm#include "config_windows.h" 43228753Smm#else 44228753Smm/* Warn if the library hasn't been (automatically or manually) configured. */ 45228753Smm#error Oops: No config.h and no pre-built configuration in test.h. 46228753Smm#endif 47228753Smm 48228753Smm#include <sys/types.h> /* Windows requires this before sys/stat.h */ 49228753Smm#include <sys/stat.h> 50228753Smm 51228753Smm#if HAVE_DIRENT_H 52228753Smm#include <dirent.h> 53228753Smm#endif 54228753Smm#ifdef HAVE_DIRECT_H 55228753Smm#include <direct.h> 56228753Smm#define dirent direct 57228753Smm#endif 58228753Smm#include <errno.h> 59228753Smm#include <fcntl.h> 60228753Smm#ifdef HAVE_IO_H 61228753Smm#include <io.h> 62228753Smm#endif 63232153Smm#ifdef HAVE_STDINT_H 64232153Smm#include <stdint.h> 65232153Smm#endif 66228753Smm#include <stdio.h> 67228753Smm#include <stdlib.h> 68228753Smm#include <string.h> 69228753Smm#include <time.h> 70228753Smm#ifdef HAVE_UNISTD_H 71228753Smm#include <unistd.h> 72228753Smm#endif 73228753Smm#include <wchar.h> 74228753Smm#ifdef HAVE_WINDOWS_H 75228753Smm#include <windows.h> 76228753Smm#endif 77228753Smm 78228753Smm/* 79228753Smm * System-specific tweaks. We really want to minimize these 80228753Smm * as much as possible, since they make it harder to understand 81228753Smm * the mainline code. 82228753Smm */ 83228753Smm 84228753Smm/* Windows (including Visual Studio and MinGW but not Cygwin) */ 85228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 86228753Smm#if !defined(__BORLANDC__) 87228753Smm#define strdup _strdup 88228753Smm#endif 89228753Smm#endif 90228753Smm 91228753Smm/* Visual Studio */ 92228753Smm#ifdef _MSC_VER 93228753Smm#define snprintf sprintf_s 94228753Smm#endif 95228753Smm 96232153Smm#if defined(__BORLANDC__) 97232153Smm#pragma warn -8068 /* Constant out of range in comparison. */ 98228753Smm#endif 99228753Smm 100232153Smm/* Haiku OS and QNX */ 101228753Smm#if defined(__HAIKU__) || defined(__QNXNTO__) 102228753Smm/* Haiku and QNX have typedefs in stdint.h (needed for int64_t) */ 103228753Smm#include <stdint.h> 104228753Smm#endif 105228753Smm 106228753Smm/* Get a real definition for __FBSDID if we can */ 107228753Smm#if HAVE_SYS_CDEFS_H 108228753Smm#include <sys/cdefs.h> 109228753Smm#endif 110228753Smm 111228753Smm/* If not, define it so as to avoid dangling semicolons. */ 112228753Smm#ifndef __FBSDID 113228753Smm#define __FBSDID(a) struct _undefined_hack 114228753Smm#endif 115228753Smm 116228753Smm#ifndef O_BINARY 117228753Smm#define O_BINARY 0 118228753Smm#endif 119228753Smm 120228753Smm/* 121228753Smm * Redefine DEFINE_TEST for use in defining the test functions. 122228753Smm */ 123228753Smm#undef DEFINE_TEST 124228753Smm#define DEFINE_TEST(name) void name(void); void name(void) 125228753Smm 126228753Smm/* An implementation of the standard assert() macro */ 127228753Smm#define assert(e) assertion_assert(__FILE__, __LINE__, (e), #e, NULL) 128228753Smm/* chdir() and error if it fails */ 129228753Smm#define assertChdir(path) \ 130228753Smm assertion_chdir(__FILE__, __LINE__, path) 131228753Smm/* Assert two integers are the same. Reports value of each one if not. */ 132228753Smm#define assertEqualInt(v1,v2) \ 133228753Smm assertion_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) 134228753Smm/* Assert two strings are the same. Reports value of each one if not. */ 135228753Smm#define assertEqualString(v1,v2) \ 136232153Smm assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 0) 137232153Smm#define assertEqualUTF8String(v1,v2) \ 138232153Smm assertion_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL, 1) 139228753Smm/* As above, but v1 and v2 are wchar_t * */ 140228753Smm#define assertEqualWString(v1,v2) \ 141228753Smm assertion_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL) 142228753Smm/* As above, but raw blocks of bytes. */ 143228753Smm#define assertEqualMem(v1, v2, l) \ 144228753Smm assertion_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL) 145232153Smm/* Assert two files are the same. */ 146232153Smm#define assertEqualFile(f1, f2) \ 147232153Smm assertion_equal_file(__FILE__, __LINE__, (f1), (f2)) 148232153Smm/* Assert that a file is empty. */ 149232153Smm#define assertEmptyFile(pathname) \ 150232153Smm assertion_empty_file(__FILE__, __LINE__, (pathname)) 151232153Smm/* Assert that a file is not empty. */ 152232153Smm#define assertNonEmptyFile(pathname) \ 153232153Smm assertion_non_empty_file(__FILE__, __LINE__, (pathname)) 154228753Smm#define assertFileAtime(pathname, sec, nsec) \ 155228753Smm assertion_file_atime(__FILE__, __LINE__, pathname, sec, nsec) 156228753Smm#define assertFileAtimeRecent(pathname) \ 157228753Smm assertion_file_atime_recent(__FILE__, __LINE__, pathname) 158228753Smm#define assertFileBirthtime(pathname, sec, nsec) \ 159228753Smm assertion_file_birthtime(__FILE__, __LINE__, pathname, sec, nsec) 160228753Smm#define assertFileBirthtimeRecent(pathname) \ 161228753Smm assertion_file_birthtime_recent(__FILE__, __LINE__, pathname) 162228753Smm/* Assert that a file exists; supports printf-style arguments. */ 163232153Smm#define assertFileExists(pathname) \ 164232153Smm assertion_file_exists(__FILE__, __LINE__, pathname) 165232153Smm/* Assert that a file exists. */ 166232153Smm#define assertFileNotExists(pathname) \ 167232153Smm assertion_file_not_exists(__FILE__, __LINE__, pathname) 168232153Smm/* Assert that file contents match a string. */ 169232153Smm#define assertFileContents(data, data_size, pathname) \ 170232153Smm assertion_file_contents(__FILE__, __LINE__, data, data_size, pathname) 171228753Smm#define assertFileMtime(pathname, sec, nsec) \ 172228753Smm assertion_file_mtime(__FILE__, __LINE__, pathname, sec, nsec) 173228753Smm#define assertFileMtimeRecent(pathname) \ 174228753Smm assertion_file_mtime_recent(__FILE__, __LINE__, pathname) 175228753Smm#define assertFileNLinks(pathname, nlinks) \ 176228753Smm assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks) 177228753Smm#define assertFileSize(pathname, size) \ 178228753Smm assertion_file_size(__FILE__, __LINE__, pathname, size) 179232153Smm#define assertTextFileContents(text, pathname) \ 180232153Smm assertion_text_file_contents(__FILE__, __LINE__, text, pathname) 181232153Smm#define assertFileContainsLinesAnyOrder(pathname, lines) \ 182232153Smm assertion_file_contains_lines_any_order(__FILE__, __LINE__, pathname, lines) 183228753Smm#define assertIsDir(pathname, mode) \ 184228753Smm assertion_is_dir(__FILE__, __LINE__, pathname, mode) 185228753Smm#define assertIsHardlink(path1, path2) \ 186228753Smm assertion_is_hardlink(__FILE__, __LINE__, path1, path2) 187228753Smm#define assertIsNotHardlink(path1, path2) \ 188228753Smm assertion_is_not_hardlink(__FILE__, __LINE__, path1, path2) 189228753Smm#define assertIsReg(pathname, mode) \ 190228753Smm assertion_is_reg(__FILE__, __LINE__, pathname, mode) 191228753Smm#define assertIsSymlink(pathname, contents) \ 192228753Smm assertion_is_symlink(__FILE__, __LINE__, pathname, contents) 193228753Smm/* Create a directory, report error if it fails. */ 194228753Smm#define assertMakeDir(dirname, mode) \ 195228753Smm assertion_make_dir(__FILE__, __LINE__, dirname, mode) 196228753Smm#define assertMakeFile(path, mode, contents) \ 197238856Smm assertion_make_file(__FILE__, __LINE__, path, mode, -1, contents) 198238856Smm#define assertMakeBinFile(path, mode, csize, contents) \ 199238856Smm assertion_make_file(__FILE__, __LINE__, path, mode, csize, contents) 200228753Smm#define assertMakeHardlink(newfile, oldfile) \ 201228753Smm assertion_make_hardlink(__FILE__, __LINE__, newfile, oldfile) 202228753Smm#define assertMakeSymlink(newfile, linkto) \ 203228753Smm assertion_make_symlink(__FILE__, __LINE__, newfile, linkto) 204238856Smm#define assertNodump(path) \ 205238856Smm assertion_nodump(__FILE__, __LINE__, path) 206228753Smm#define assertUmask(mask) \ 207228753Smm assertion_umask(__FILE__, __LINE__, mask) 208232153Smm#define assertUtimes(pathname, atime, atime_nsec, mtime, mtime_nsec) \ 209232153Smm assertion_utimes(__FILE__, __LINE__, pathname, atime, atime_nsec, mtime, mtime_nsec) 210228753Smm 211228753Smm/* 212228753Smm * This would be simple with C99 variadic macros, but I don't want to 213228753Smm * require that. Instead, I insert a function call before each 214228753Smm * skipping() call to pass the file and line information down. Crude, 215228753Smm * but effective. 216228753Smm */ 217228753Smm#define skipping \ 218232153Smm skipping_setup(__FILE__, __LINE__);test_skipping 219228753Smm 220228753Smm/* Function declarations. These are defined in test_utility.c. */ 221228753Smmvoid failure(const char *fmt, ...); 222228753Smmint assertion_assert(const char *, int, int, const char *, void *); 223228753Smmint assertion_chdir(const char *, int, const char *); 224232153Smmint assertion_empty_file(const char *, int, const char *); 225232153Smmint assertion_equal_file(const char *, int, const char *, const char *); 226228753Smmint assertion_equal_int(const char *, int, long long, const char *, long long, const char *, void *); 227228753Smmint assertion_equal_mem(const char *, int, const void *, const char *, const void *, const char *, size_t, const char *, void *); 228232153Smmint assertion_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *, int); 229228753Smmint assertion_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *); 230228753Smmint assertion_file_atime(const char *, int, const char *, long, long); 231228753Smmint assertion_file_atime_recent(const char *, int, const char *); 232228753Smmint assertion_file_birthtime(const char *, int, const char *, long, long); 233228753Smmint assertion_file_birthtime_recent(const char *, int, const char *); 234232153Smmint assertion_file_contains_lines_any_order(const char *, int, const char *, const char **); 235232153Smmint assertion_file_contents(const char *, int, const void *, int, const char *); 236232153Smmint assertion_file_exists(const char *, int, const char *); 237228753Smmint assertion_file_mtime(const char *, int, const char *, long, long); 238228753Smmint assertion_file_mtime_recent(const char *, int, const char *); 239228753Smmint assertion_file_nlinks(const char *, int, const char *, int); 240232153Smmint assertion_file_not_exists(const char *, int, const char *); 241228753Smmint assertion_file_size(const char *, int, const char *, long); 242228753Smmint assertion_is_dir(const char *, int, const char *, int); 243228753Smmint assertion_is_hardlink(const char *, int, const char *, const char *); 244228753Smmint assertion_is_not_hardlink(const char *, int, const char *, const char *); 245228753Smmint assertion_is_reg(const char *, int, const char *, int); 246228753Smmint assertion_is_symlink(const char *, int, const char *, const char *); 247228753Smmint assertion_make_dir(const char *, int, const char *, int); 248238856Smmint assertion_make_file(const char *, int, const char *, int, int, const void *); 249228753Smmint assertion_make_hardlink(const char *, int, const char *newpath, const char *); 250228753Smmint assertion_make_symlink(const char *, int, const char *newpath, const char *); 251238856Smmint assertion_nodump(const char *, int, const char *); 252232153Smmint assertion_non_empty_file(const char *, int, const char *); 253232153Smmint assertion_text_file_contents(const char *, int, const char *buff, const char *f); 254228753Smmint assertion_umask(const char *, int, int); 255232153Smmint assertion_utimes(const char *, int, const char *, long, long, long, long ); 256228753Smm 257232153Smmvoid skipping_setup(const char *, int); 258228753Smmvoid test_skipping(const char *fmt, ...); 259228753Smm 260228753Smm/* Like sprintf, then system() */ 261228753Smmint systemf(const char * fmt, ...); 262228753Smm 263228753Smm/* Delay until time() returns a value after this. */ 264228753Smmvoid sleepUntilAfter(time_t); 265228753Smm 266228753Smm/* Return true if this platform can create symlinks. */ 267228753Smmint canSymlink(void); 268228753Smm 269248616Smm/* Return true if this platform can run the "bzip2" program. */ 270248616Smmint canBzip2(void); 271248616Smm 272248616Smm/* Return true if this platform can run the "grzip" program. */ 273248616Smmint canGrzip(void); 274248616Smm 275228753Smm/* Return true if this platform can run the "gzip" program. */ 276228753Smmint canGzip(void); 277228753Smm 278248616Smm/* Return true if this platform can run the "lrzip" program. */ 279248616Smmint canLrzip(void); 280228753Smm 281248616Smm/* Return true if this platform can run the "lzip" program. */ 282248616Smmint canLzip(void); 283248616Smm 284248616Smm/* Return true if this platform can run the "lzma" program. */ 285248616Smmint canLzma(void); 286248616Smm 287248616Smm/* Return true if this platform can run the "lzop" program. */ 288248616Smmint canLzop(void); 289248616Smm 290248616Smm/* Return true if this platform can run the "xz" program. */ 291248616Smmint canXz(void); 292248616Smm 293238856Smm/* Return true if this filesystem can handle nodump flags. */ 294238856Smmint canNodump(void); 295238856Smm 296232153Smm/* Return true if the file has large i-node number(>0xffffffff). */ 297232153Smmint is_LargeInode(const char *); 298232153Smm 299228753Smm/* Suck file into string allocated via malloc(). Call free() when done. */ 300228753Smm/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */ 301228753Smmchar *slurpfile(size_t *, const char *fmt, ...); 302228753Smm 303228753Smm/* Extracts named reference file to the current directory. */ 304228753Smmvoid extract_reference_file(const char *); 305228753Smm 306232153Smm/* Path to working directory for current test */ 307232153Smmconst char *testworkdir; 308232153Smm 309228753Smm/* 310228753Smm * Special interfaces for program test harness. 311228753Smm */ 312228753Smm 313228753Smm/* Pathname of exe to be tested. */ 314228753Smmconst char *testprogfile; 315228753Smm/* Name of exe to use in printf-formatted command strings. */ 316228753Smm/* On Windows, this includes leading/trailing quotes. */ 317228753Smmconst char *testprog; 318232153Smm 319232153Smm#ifdef USE_DMALLOC 320232153Smm#include <dmalloc.h> 321232153Smm#endif 322