]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/test/test.h
This commit was generated by cvs2svn to compensate for changes in r177391,
[FreeBSD/FreeBSD.git] / lib / libarchive / 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 #ifndef _FILE_OFFSET_BITS
35 #define _FILE_OFFSET_BITS 64
36 #endif
37
38 #include <dirent.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/stat.h>
45 #ifndef _WIN32
46 #include <unistd.h>
47 #endif
48 #include <wchar.h>
49
50 #ifdef USE_DMALLOC
51 #include <dmalloc.h>
52 #endif
53
54 #if defined(HAVE_CONFIG_H)
55 /* Most POSIX platforms use the 'configure' script to build config.h */
56 #include "../../config.h"
57 #elif defined(__FreeBSD__)
58 /* Building as part of FreeBSD system requires a pre-built config.h. */
59 #include "../config_freebsd.h"
60 #elif defined(_WIN32)
61 /* Win32 can't run the 'configure' script. */
62 #include "../config_windows.h"
63 #else
64 /* Warn if the library hasn't been (automatically or manually) configured. */
65 #error Oops: No config.h and no pre-built configuration in test.h.
66 #endif
67
68 /* No non-FreeBSD platform will have __FBSDID, so just define it here. */
69 #ifdef __FreeBSD__
70 #include <sys/cdefs.h>  /* For __FBSDID */
71 #else
72 #define __FBSDID(a)     /* null */
73 #endif
74
75 /*
76  * Redefine DEFINE_TEST for use in defining the test functions.
77  */
78 #undef DEFINE_TEST
79 #define DEFINE_TEST(name) void name(void); void name(void)
80
81 /* An implementation of the standard assert() macro */
82 #define assert(e)   test_assert(__FILE__, __LINE__, (e), #e, NULL)
83
84 /* Assert two integers are the same.  Reports value of each one if not. */
85 #define assertEqualInt(v1,v2)   \
86   test_assert_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
87
88 /* Assert two strings are the same.  Reports value of each one if not. */
89 #define assertEqualString(v1,v2)   \
90   test_assert_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
91 /* As above, but v1 and v2 are wchar_t * */
92 #define assertEqualWString(v1,v2)   \
93   test_assert_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
94 /* As above, but raw blocks of bytes. */
95 #define assertEqualMem(v1, v2, l)       \
96   test_assert_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
97 /* Assert two files are the same; allow printf-style expansion of second name.
98  * See below for comments about variable arguments here...
99  */
100 #define assertEqualFile         \
101   test_setup(__FILE__, __LINE__);test_assert_equal_file
102 /* Assert that a file is empty; supports printf-style arguments. */
103 #define assertEmptyFile         \
104   test_setup(__FILE__, __LINE__);test_assert_empty_file
105
106 /*
107  * This would be simple with C99 variadic macros, but I don't want to
108  * require that.  Instead, I insert a function call before each
109  * skipping() call to pass the file and line information down.  Crude,
110  * but effective.
111  */
112 #define skipping        \
113   test_setup(__FILE__, __LINE__);test_skipping
114
115 /* Function declarations.  These are defined in test_utility.c. */
116 void failure(const char *fmt, ...);
117 void test_setup(const char *, int);
118 void test_skipping(const char *fmt, ...);
119 int test_assert(const char *, int, int, const char *, void *);
120 int test_assert_empty_file(const char *, ...);
121 int test_assert_equal_file(const char *, const char *, ...);
122 int test_assert_equal_int(const char *, int, int, const char *, int, const char *, void *);
123 int test_assert_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
124 int test_assert_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
125 int test_assert_equal_mem(const char *, int, const char *, const char *, const char *, const char *, size_t, const char *, void *);
126 int test_assert_file_contents(const void *, int, const char *, ...);
127
128 /* Like sprintf, then system() */
129 int systemf(const char * fmt, ...);
130
131 /* Suck file into string allocated via malloc(). Call free() when done. */
132 /* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
133 char *slurpfile(size_t *, const char *fmt, ...);
134
135 /* Extracts named reference file to the current directory. */
136 void extract_reference_file(const char *);
137
138 /*
139  * Special interfaces for libarchive test harness.
140  */
141
142 #include "archive.h"
143 #include "archive_entry.h"
144
145 /* Special customized read-from-memory interface. */
146 int read_open_memory(struct archive *, void *, size_t, size_t);
147
148 /*
149  * ARCHIVE_VERSION_STAMP first appeared in 1.9 and libarchive 2.2.4.
150  * We can approximate it for earlier versions, though.
151  * This is used to disable tests of features not present in the current
152  * version.
153  */
154 #ifndef ARCHIVE_VERSION_STAMP
155 #define ARCHIVE_VERSION_STAMP   \
156                 (ARCHIVE_API_VERSION * 1000000 + ARCHIVE_API_FEATURE * 1000)
157 #endif
158
159 /* Versions of above that accept an archive argument for additional info. */
160 #define assertA(e)   test_assert(__FILE__, __LINE__, (e), #e, (a))
161 #define assertEqualIntA(a,v1,v2)   \
162   test_assert_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))
163 #define assertEqualStringA(a,v1,v2)   \
164   test_assert_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (a))