]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/cpio/test/test.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / cpio / test / test.h
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 #if !defined(_WIN32) || defined(__CYGWIN__)
49 #include <dirent.h>
50 #else
51 #include "../cpio_windows.h"
52 #endif
53 #if defined(__CYGWIN__)
54 /* In cygwin-1.7.x, the .nlinks field of directories is
55  * deliberately inaccurate, because to populate it requires
56  * stat'ing every file in the directory, which is slow.
57  * So, as an optimization cygwin doesn't do that in newer
58  * releases; all correct applications on any platform should
59  * never rely on it being > 1, so this optimization doesn't
60  * impact the operation of correctly coded applications.
61  * Therefore, the cpio test should not check its accuracy
62  */
63 # define NLINKS_INACCURATE_FOR_DIRS
64 #endif
65 #include <errno.h>
66 #include <fcntl.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <sys/stat.h>
71 #if !defined(_WIN32) || defined(__CYGWIN__)
72 #include <unistd.h>
73 #endif
74 #include <time.h>
75 #include <wchar.h>
76
77 #ifdef USE_DMALLOC
78 #include <dmalloc.h>
79 #endif
80
81 /* No non-FreeBSD platform will have __FBSDID, so just define it here. */
82 #ifdef __FreeBSD__
83 #include <sys/cdefs.h>  /* For __FBSDID */
84 #else
85 #undef __FBSDID
86 #define __FBSDID(a)     struct _undefined_hack
87 #endif
88
89 /*
90  * Redefine DEFINE_TEST for use in defining the test functions.
91  */
92 #undef DEFINE_TEST
93 #define DEFINE_TEST(name) void name(void); void name(void)
94
95 /* An implementation of the standard assert() macro */
96 #define assert(e)   test_assert(__FILE__, __LINE__, (e), #e, NULL)
97
98 /* Assert two integers are the same.  Reports value of each one if not. */
99 #define assertEqualInt(v1,v2)   \
100   test_assert_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
101
102 /* Assert two strings are the same.  Reports value of each one if not. */
103 #define assertEqualString(v1,v2)   \
104   test_assert_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
105 /* As above, but v1 and v2 are wchar_t * */
106 #define assertEqualWString(v1,v2)   \
107   test_assert_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
108 /* As above, but raw blocks of bytes. */
109 #define assertEqualMem(v1, v2, l)       \
110   test_assert_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
111 /* Assert two files are the same; allow printf-style expansion of second name.
112  * See below for comments about variable arguments here...
113  */
114 #define assertEqualFile         \
115   test_setup(__FILE__, __LINE__);test_assert_equal_file
116 /* Assert that a file is empty; supports printf-style arguments. */
117 #define assertEmptyFile         \
118   test_setup(__FILE__, __LINE__);test_assert_empty_file
119 /* Assert that a file exists; supports printf-style arguments. */
120 #define assertFileExists                \
121   test_setup(__FILE__, __LINE__);test_assert_file_exists
122 /* Assert that a file exists; supports printf-style arguments. */
123 #define assertFileNotExists             \
124   test_setup(__FILE__, __LINE__);test_assert_file_not_exists
125 /* Assert that file contents match a string; supports printf-style arguments. */
126 #define assertFileContents             \
127   test_setup(__FILE__, __LINE__);test_assert_file_contents
128 #define assertTextFileContents         \
129   test_setup(__FILE__, __LINE__);test_assert_text_file_contents
130
131 /*
132  * This would be simple with C99 variadic macros, but I don't want to
133  * require that.  Instead, I insert a function call before each
134  * skipping() call to pass the file and line information down.  Crude,
135  * but effective.
136  */
137 #define skipping        \
138   test_setup(__FILE__, __LINE__);test_skipping
139
140 /* Function declarations.  These are defined in test_utility.c. */
141 void failure(const char *fmt, ...);
142 void test_setup(const char *, int);
143 void test_skipping(const char *fmt, ...);
144 int test_assert(const char *, int, int, const char *, void *);
145 int test_assert_empty_file(const char *, ...);
146 int test_assert_equal_file(const char *, const char *, ...);
147 int test_assert_equal_int(const char *, int, int, const char *, int, const char *, void *);
148 int test_assert_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
149 int test_assert_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
150 int test_assert_equal_mem(const char *, int, const char *, const char *, const char *, const char *, size_t, const char *, void *);
151 int test_assert_file_contents(const void *, int, const char *, ...);
152 int test_assert_text_file_contents(const char *buff, const char *f);
153 int test_assert_file_exists(const char *, ...);
154 int test_assert_file_not_exists(const char *, ...);
155
156 /* Like sprintf, then system() */
157 int systemf(const char * fmt, ...);
158
159 /* Suck file into string allocated via malloc(). Call free() when done. */
160 /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
161 char *slurpfile(size_t *, const char *fmt, ...);
162
163 /* Extracts named reference file to the current directory. */
164 void extract_reference_file(const char *);
165
166 /*
167  * Special interfaces for program test harness.
168  */
169
170 /* Pathname of exe to be tested. */
171 const char *testprog;