2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 * Various utility routines useful for test programs.
28 * Each test program is linked against this file.
35 __FBSDID("$FreeBSD$");
37 /* Interix doesn't define these in a standard header. */
43 /* Default is to crash and try to force a core dump on failure. */
44 static int dump_on_failure = 1;
45 /* Default is to print some basic information about each test. */
46 static int quiet_flag = 0;
47 /* Cumulative count of component failures. */
48 static int failures = 0;
49 /* Cumulative count of skipped component tests. */
53 * My own implementation of the standard assert() macro emits the
54 * message in the same format as GCC (file:line: message).
55 * It also includes some additional useful information.
56 * This makes it a lot easier to skim through test failures in
59 * It also supports a few special features specifically to simplify
60 * libarchive test harnesses:
61 * failure(fmt, args) -- Stores a text string that gets
62 * printed if the following assertion fails, good for
63 * explaining subtle tests.
64 * assertA(a, cond) -- If the test fails, also prints out any error
65 * message stored in archive object 'a'.
67 static char msg[4096];
70 * For each test source file, we remember how many times each
71 * failure was reported.
73 static const char *failed_filename;
80 /* Count this failure; return the number of previous failures. */
82 previous_failures(const char *filename, int line)
87 if (failed_filename == NULL || strcmp(failed_filename, filename) != 0)
88 memset(failed_lines, 0, sizeof(failed_lines));
89 failed_filename = filename;
91 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
92 if (failed_lines[i].line == line) {
93 count = failed_lines[i].count;
94 failed_lines[i].count++;
97 if (failed_lines[i].line == 0) {
98 failed_lines[i].line = line;
99 failed_lines[i].count = 1;
105 /* Inform user that we're skipping a test. */
106 static const char *skipped_filename;
107 static int skipped_line;
108 void skipping_setup(const char *filename, int line)
110 skipped_filename = filename;
114 test_skipping(const char *fmt, ...)
117 int line = skipped_line;
120 if (previous_failures(skipped_filename, skipped_line))
124 fprintf(stderr, " *** SKIPPING: ");
125 vfprintf(stderr, fmt, ap);
126 fprintf(stderr, "\n");
131 /* Common handling of failed tests. */
133 test_failed(struct archive *a, int line)
139 if (msg[0] != '\0') {
140 fprintf(stderr, " Description: %s\n", msg);
144 fprintf(stderr, " archive error: %s\n", archive_error_string(a));
147 if (dump_on_failure) {
148 fprintf(stderr, " *** forcing core dump so failure can be debugged ***\n");
154 /* Summarize repeated failures in the just-completed test file. */
156 summarize_comparator(const void *a0, const void *b0)
158 const struct line *a = a0, *b = b0;
159 if (a->line == 0 && b->line == 0)
165 return (a->line - b->line);
169 summarize(const char *filename)
173 qsort(failed_lines, sizeof(failed_lines)/sizeof(failed_lines[0]),
174 sizeof(failed_lines[0]), summarize_comparator);
175 for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
176 if (failed_lines[i].line == 0)
178 if (failed_lines[i].count > 1)
179 fprintf(stderr, "%s:%d: Failed %d times\n",
180 failed_filename, failed_lines[i].line,
181 failed_lines[i].count);
183 /* Clear the failure history for the next file. */
184 memset(failed_lines, 0, sizeof(failed_lines));
187 /* Set up a message to display only after a test fails. */
189 failure(const char *fmt, ...)
193 vsprintf(msg, fmt, ap);
197 /* Generic assert() just displays the failed condition. */
199 test_assert(const char *file, int line, int value, const char *condition, struct archive *a)
205 if (previous_failures(file, line))
207 fprintf(stderr, "%s:%d: Assertion failed\n", file, line);
208 fprintf(stderr, " Condition: %s\n", condition);
209 test_failed(a, line);
212 /* assertEqualInt() displays the values of the two integers. */
214 test_assert_equal_int(const char *file, int line,
215 int v1, const char *e1, int v2, const char *e2, struct archive *a)
221 if (previous_failures(file, line))
223 fprintf(stderr, "%s:%d: Assertion failed: Ints not equal\n",
225 fprintf(stderr, " %s=%d\n", e1, v1);
226 fprintf(stderr, " %s=%d\n", e2, v2);
227 test_failed(a, line);
230 /* assertEqualString() displays the values of the two strings. */
232 test_assert_equal_string(const char *file, int line,
233 const char *v1, const char *e1,
234 const char *v2, const char *e2,
237 if (v1 == NULL || v2 == NULL) {
242 } else if (strcmp(v1, v2) == 0) {
246 if (previous_failures(file, line))
248 fprintf(stderr, "%s:%d: Assertion failed: Strings not equal\n",
250 fprintf(stderr, " %s = \"%s\"\n", e1, v1);
251 fprintf(stderr, " %s = \"%s\"\n", e2, v2);
252 test_failed(a, line);
255 /* assertEqualWString() displays the values of the two strings. */
257 test_assert_equal_wstring(const char *file, int line,
258 const wchar_t *v1, const char *e1,
259 const wchar_t *v2, const char *e2,
262 if (wcscmp(v1, v2) == 0) {
266 if (previous_failures(file, line))
268 fprintf(stderr, "%s:%d: Assertion failed: Unicode strings not equal\n",
270 fwprintf(stderr, L" %s = \"%ls\"\n", e1, v1);
271 fwprintf(stderr, L" %s = \"%ls\"\n", e2, v2);
272 test_failed(a, line);
276 * "list.h" is automatically generated; it just has a lot of lines like:
277 * DEFINE_TEST(function_name)
278 * The common "test.h" includes it to declare all of the test functions.
279 * We reuse it here to define a list of all tests to run.
282 #define DEFINE_TEST(n) { n, #n },
283 struct { void (*func)(void); const char *name; } tests[] = {
287 static int test_run(int i, const char *tmpdir)
289 int failures_before = failures;
292 printf("%d: %s\n", i, tests[i].name);
294 * Always explicitly chdir() in case the last test moved us to
299 "ERROR: Couldn't chdir to temp dir %s\n",
303 /* Create a temp directory for this specific test. */
304 if (mkdir(tests[i].name, 0755)) {
306 "ERROR: Couldn't create temp dir ``%s''\n",
310 if (chdir(tests[i].name)) {
312 "ERROR: Couldn't chdir to temp dir ``%s''\n",
317 summarize(tests[i].name);
318 return (failures == failures_before ? 0 : 1);
321 static void usage(void)
323 static const int limit = sizeof(tests) / sizeof(tests[0]);
326 printf("Usage: libarchive_test [options] <test> <test> ...\n");
327 printf("Default is to run all tests.\n");
328 printf("Otherwise, specify the numbers of the tests you wish to run.\n");
329 printf("Options:\n");
330 printf(" -k Keep running after failures.\n");
331 printf(" Default: Core dump after any failure.\n");
332 printf(" -q Quiet.\n");
333 printf("Available tests:\n");
334 for (i = 0; i < limit; i++)
335 printf(" %d: %s\n", i, tests[i].name);
339 int main(int argc, char **argv)
341 static const int limit = sizeof(tests) / sizeof(tests[0]);
342 int i, tests_run = 0, tests_failed = 0, opt;
346 while ((opt = getopt(argc, argv, "kq")) != -1) {
363 * Create a temp directory for the following tests.
364 * Include the time the tests started as part of the name,
365 * to make it easier to track the results of multiple tests.
368 for (i = 0; i < 1000; i++) {
369 strftime(tmpdir, sizeof(tmpdir),
370 "/tmp/libarchive_test.%Y-%m-%dT%H.%M.%S",
372 sprintf(tmpdir + strlen(tmpdir), "-%03d", i);
373 if (mkdir(tmpdir,0755) == 0)
377 fprintf(stderr, "ERROR: Unable to create temp directory %s\n",
383 printf("Running libarchive tests in: %s\n", tmpdir);
384 printf("Exercising %s\n", archive_version());
388 /* Default: Run all tests. */
389 for (i = 0; i < limit; i++) {
390 if (test_run(i, tmpdir))
395 while (*(argv) != NULL) {
397 if (**argv < '0' || **argv > '9' || i < 0 || i >= limit) {
398 printf("*** INVALID Test %s\n", *argv);
401 if (test_run(i, tmpdir))
409 printf("%d of %d test groups reported failures\n",
410 tests_failed, tests_run);
411 printf(" Total of %d individual tests failed.\n", failures);
412 printf(" Total of %d individual tests were skipped.\n", skips);
413 return (tests_failed);