]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/test/main.c
This commit was generated by cvs2svn to compensate for changes in r172668,
[FreeBSD/FreeBSD.git] / lib / libarchive / test / main.c
1 /*
2  * Copyright (c) 2003-2007 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
26 /*
27  * Various utility routines useful for test programs.
28  * Each test program is linked against this file.
29  */
30 #include <errno.h>
31 #include <stdarg.h>
32 #include <time.h>
33
34 #include "test.h"
35 __FBSDID("$FreeBSD$");
36
37 /* Interix doesn't define these in a standard header. */
38 #if __INTERIX__
39 extern char *optarg;
40 extern int optind;
41 #endif
42
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. */
50 static int skips = 0;
51
52 /*
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
57  * Emacs.  ;-)
58  *
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'.
66  */
67 static char msg[4096];
68
69 /*
70  * For each test source file, we remember how many times each
71  * failure was reported.
72  */
73 static const char *failed_filename;
74 static struct line {
75         int line;
76         int count;
77 }  failed_lines[1000];
78
79
80 /* Count this failure; return the number of previous failures. */
81 static int
82 previous_failures(const char *filename, int line)
83 {
84         int i;
85         int count;
86
87         if (failed_filename == NULL || strcmp(failed_filename, filename) != 0)
88                 memset(failed_lines, 0, sizeof(failed_lines));
89         failed_filename = filename;
90
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++;
95                         return (count);
96                 }
97                 if (failed_lines[i].line == 0) {
98                         failed_lines[i].line = line;
99                         failed_lines[i].count = 1;
100                         return (0);
101                 }
102         }
103 }
104
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)
109 {
110         skipped_filename = filename;
111         skipped_line = line;
112 }
113 void
114 test_skipping(const char *fmt, ...)
115 {
116         int i;
117         int line = skipped_line;
118         va_list ap;
119
120         if (previous_failures(skipped_filename, skipped_line))
121                 return;
122
123         va_start(ap, fmt);
124         fprintf(stderr, " *** SKIPPING: ");
125         vfprintf(stderr, fmt, ap);
126         fprintf(stderr, "\n");
127         va_end(ap);
128         ++skips;
129 }
130
131 /* Common handling of failed tests. */
132 static void
133 test_failed(struct archive *a, int line)
134 {
135         int i;
136
137         failures ++;
138
139         if (msg[0] != '\0') {
140                 fprintf(stderr, "   Description: %s\n", msg);
141                 msg[0] = '\0';
142         }
143         if (a != NULL) {
144                 fprintf(stderr, "   archive error: %s\n", archive_error_string(a));
145         }
146
147         if (dump_on_failure) {
148                 fprintf(stderr, " *** forcing core dump so failure can be debugged ***\n");
149                 *(char *)(NULL) = 0;
150                 exit(1);
151         }
152 }
153
154 /* Summarize repeated failures in the just-completed test file. */
155 int
156 summarize_comparator(const void *a0, const void *b0)
157 {
158         const struct line *a = a0, *b = b0;
159         if (a->line == 0 && b->line == 0)
160                 return (0);
161         if (a->line == 0)
162                 return (1);
163         if (b->line == 0)
164                 return (-1);
165         return (a->line - b->line);
166 }
167
168 void
169 summarize(const char *filename)
170 {
171         int i;
172
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)
177                         break;
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);
182         }
183         /* Clear the failure history for the next file. */
184         memset(failed_lines, 0, sizeof(failed_lines));
185 }
186
187 /* Set up a message to display only after a test fails. */
188 void
189 failure(const char *fmt, ...)
190 {
191         va_list ap;
192         va_start(ap, fmt);
193         vsprintf(msg, fmt, ap);
194         va_end(ap);
195 }
196
197 /* Generic assert() just displays the failed condition. */
198 void
199 test_assert(const char *file, int line, int value, const char *condition, struct archive *a)
200 {
201         if (value) {
202                 msg[0] = '\0';
203                 return;
204         }
205         if (previous_failures(file, line))
206                 return;
207         fprintf(stderr, "%s:%d: Assertion failed\n", file, line);
208         fprintf(stderr, "   Condition: %s\n", condition);
209         test_failed(a, line);
210 }
211
212 /* assertEqualInt() displays the values of the two integers. */
213 void
214 test_assert_equal_int(const char *file, int line,
215     int v1, const char *e1, int v2, const char *e2, struct archive *a)
216 {
217         if (v1 == v2) {
218                 msg[0] = '\0';
219                 return;
220         }
221         if (previous_failures(file, line))
222                 return;
223         fprintf(stderr, "%s:%d: Assertion failed: Ints not equal\n",
224             file, line);
225         fprintf(stderr, "      %s=%d\n", e1, v1);
226         fprintf(stderr, "      %s=%d\n", e2, v2);
227         test_failed(a, line);
228 }
229
230 /* assertEqualString() displays the values of the two strings. */
231 void
232 test_assert_equal_string(const char *file, int line,
233     const char *v1, const char *e1,
234     const char *v2, const char *e2,
235     struct archive *a)
236 {
237         if (v1 == NULL || v2 == NULL) {
238                 if (v1 == v2) {
239                         msg[0] = '\0';
240                         return;
241                 }
242         } else if (strcmp(v1, v2) == 0) {
243                 msg[0] = '\0';
244                 return;
245         }
246         if (previous_failures(file, line))
247                 return;
248         fprintf(stderr, "%s:%d: Assertion failed: Strings not equal\n",
249             file, line);
250         fprintf(stderr, "      %s = \"%s\"\n", e1, v1);
251         fprintf(stderr, "      %s = \"%s\"\n", e2, v2);
252         test_failed(a, line);
253 }
254
255 /* assertEqualWString() displays the values of the two strings. */
256 void
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,
260     struct archive *a)
261 {
262         if (wcscmp(v1, v2) == 0) {
263                 msg[0] = '\0';
264                 return;
265         }
266         if (previous_failures(file, line))
267                 return;
268         fprintf(stderr, "%s:%d: Assertion failed: Unicode strings not equal\n",
269             file, line);
270         fwprintf(stderr, L"      %s = \"%ls\"\n", e1, v1);
271         fwprintf(stderr, L"      %s = \"%ls\"\n", e2, v2);
272         test_failed(a, line);
273 }
274
275 /*
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.
280  */
281 #undef DEFINE_TEST
282 #define DEFINE_TEST(n) { n, #n },
283 struct { void (*func)(void); const char *name; } tests[] = {
284         #include "list.h"
285 };
286
287 static int test_run(int i, const char *tmpdir)
288 {
289         int failures_before = failures;
290
291         if (!quiet_flag)
292                 printf("%d: %s\n", i, tests[i].name);
293         /*
294          * Always explicitly chdir() in case the last test moved us to
295          * a strange place.
296          */
297         if (chdir(tmpdir)) {
298                 fprintf(stderr,
299                     "ERROR: Couldn't chdir to temp dir %s\n",
300                     tmpdir);
301                 exit(1);
302         }
303         /* Create a temp directory for this specific test. */
304         if (mkdir(tests[i].name, 0755)) {
305                 fprintf(stderr,
306                     "ERROR: Couldn't create temp dir ``%s''\n",
307                     tests[i].name);
308                 exit(1);
309         }
310         if (chdir(tests[i].name)) {
311                 fprintf(stderr,
312                     "ERROR: Couldn't chdir to temp dir ``%s''\n",
313                     tests[i].name);
314                 exit(1);
315         }
316         (*tests[i].func)();
317         summarize(tests[i].name);
318         return (failures == failures_before ? 0 : 1);
319 }
320
321 static void usage(void)
322 {
323         static const int limit = sizeof(tests) / sizeof(tests[0]);
324         int i;
325
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);
336         exit(1);
337 }
338
339 int main(int argc, char **argv)
340 {
341         static const int limit = sizeof(tests) / sizeof(tests[0]);
342         int i, tests_run = 0, tests_failed = 0, opt;
343         time_t now;
344         char tmpdir[256];
345
346         while ((opt = getopt(argc, argv, "kq")) != -1) {
347                 switch (opt) {
348                 case 'k':
349                         dump_on_failure = 0;
350                         break;
351                 case 'q':
352                         quiet_flag = 1;
353                         break;
354                 case '?':
355                 default:
356                         usage();
357                 }
358         }
359         argc -= optind;
360         argv += optind;
361
362         /*
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.
366          */
367         now = time(NULL);
368         for (i = 0; i < 1000; i++) {
369                 strftime(tmpdir, sizeof(tmpdir),
370                     "/tmp/libarchive_test.%Y-%m-%dT%H.%M.%S",
371                     localtime(&now));
372                 sprintf(tmpdir + strlen(tmpdir), "-%03d", i);
373                 if (mkdir(tmpdir,0755) == 0)
374                         break;
375                 if (errno == EEXIST)
376                         continue;
377                 fprintf(stderr, "ERROR: Unable to create temp directory %s\n",
378                     tmpdir);
379                 exit(1);
380         }
381
382         if (!quiet_flag) {
383                 printf("Running libarchive tests in: %s\n", tmpdir);
384                 printf("Exercising %s\n", archive_version());
385         }
386
387         if (argc == 0) {
388                 /* Default: Run all tests. */
389                 for (i = 0; i < limit; i++) {
390                         if (test_run(i, tmpdir))
391                                 tests_failed++;
392                         tests_run++;
393                 }
394         } else {
395                 while (*(argv) != NULL) {
396                         i = atoi(*argv);
397                         if (**argv < '0' || **argv > '9' || i < 0 || i >= limit) {
398                                 printf("*** INVALID Test %s\n", *argv);
399                                 usage();
400                         } else {
401                                 if (test_run(i, tmpdir))
402                                         tests_failed++;
403                                 tests_run++;
404                         }
405                         argv++;
406                 }
407         }
408         printf("\n");
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);
414 }