]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/runtests.c
Reformat with clang-format
[kerberos/krb5-strength.git] / tests / runtests.c
1 /*
2  * Run a set of tests, reporting results.
3  *
4  * Usage:
5  *
6  *      runtests [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>
7  *      runtests [-hv] [-b <build-dir>] [-s <source-dir>] <test> [<test> ...]
8  *      runtests -o [-h] [-b <build-dir>] [-s <source-dir>] <test>
9  *
10  * In the first case, expects a list of executables located in the given file,
11  * one line per executable.  For each one, runs it as part of a test suite,
12  * reporting results.  In the second case, use the same infrastructure, but
13  * run only the tests listed on the command line.
14  *
15  * Test output should start with a line containing the number of tests
16  * (numbered from 1 to this number), optionally preceded by "1..", although
17  * that line may be given anywhere in the output.  Each additional line should
18  * be in the following format:
19  *
20  *      ok <number>
21  *      not ok <number>
22  *      ok <number> # skip
23  *      not ok <number> # todo
24  *
25  * where <number> is the number of the test.  An optional comment is permitted
26  * after the number if preceded by whitespace.  ok indicates success, not ok
27  * indicates failure.  "# skip" and "# todo" are a special cases of a comment,
28  * and must start with exactly that formatting.  They indicate the test was
29  * skipped for some reason (maybe because it doesn't apply to this platform)
30  * or is testing something known to currently fail.  The text following either
31  * "# skip" or "# todo" and whitespace is the reason.
32  *
33  * As a special case, the first line of the output may be in the form:
34  *
35  *      1..0 # skip some reason
36  *
37  * which indicates that this entire test case should be skipped and gives a
38  * reason.
39  *
40  * Any other lines are ignored, although for compliance with the TAP protocol
41  * all lines other than the ones in the above format should be sent to
42  * standard error rather than standard output and start with #.
43  *
44  * This is a subset of TAP as documented in Test::Harness::TAP or
45  * TAP::Parser::Grammar, which comes with Perl.
46  *
47  * If the -o option is given, instead run a single test and display all of its
48  * output.  This is intended for use with failing tests so that the person
49  * running the test suite can get more details about what failed.
50  *
51  * If built with the C preprocessor symbols C_TAP_SOURCE and C_TAP_BUILD
52  * defined, C TAP Harness will export those values in the environment so that
53  * tests can find the source and build directory and will look for tests under
54  * both directories.  These paths can also be set with the -b and -s
55  * command-line options, which will override anything set at build time.
56  *
57  * If the -v option is given, or the C_TAP_VERBOSE environment variable is set,
58  * display the full output of each test as it runs rather than showing a
59  * summary of the results of each test.
60  *
61  * Any bug reports, bug fixes, and improvements are very much welcome and
62  * should be sent to the e-mail address below.  This program is part of C TAP
63  * Harness <https://www.eyrie.org/~eagle/software/c-tap-harness/>.
64  *
65  * Copyright 2000, 2001, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
66  *     2014, 2015, 2016 Russ Allbery <eagle@eyrie.org>
67  *
68  * Permission is hereby granted, free of charge, to any person obtaining a
69  * copy of this software and associated documentation files (the "Software"),
70  * to deal in the Software without restriction, including without limitation
71  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
72  * and/or sell copies of the Software, and to permit persons to whom the
73  * Software is furnished to do so, subject to the following conditions:
74  *
75  * The above copyright notice and this permission notice shall be included in
76  * all copies or substantial portions of the Software.
77  *
78  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
79  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
80  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
81  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
82  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
83  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
84  * DEALINGS IN THE SOFTWARE.
85  */
86
87 /* Required for fdopen(), getopt(), and putenv(). */
88 #if defined(__STRICT_ANSI__) || defined(PEDANTIC)
89 #    ifndef _XOPEN_SOURCE
90 #        define _XOPEN_SOURCE 500
91 #    endif
92 #endif
93
94 #include <ctype.h>
95 #include <errno.h>
96 #include <fcntl.h>
97 #include <limits.h>
98 #include <stdarg.h>
99 #include <stddef.h>
100 #include <stdio.h>
101 #include <stdlib.h>
102 #include <string.h>
103 #include <strings.h>
104 #include <sys/stat.h>
105 #include <sys/time.h>
106 #include <sys/types.h>
107 #include <sys/wait.h>
108 #include <time.h>
109 #include <unistd.h>
110
111 /* sys/time.h must be included before sys/resource.h on some platforms. */
112 #include <sys/resource.h>
113
114 /* AIX 6.1 (and possibly later) doesn't have WCOREDUMP. */
115 #ifndef WCOREDUMP
116 #    define WCOREDUMP(status) ((unsigned) (status) &0x80)
117 #endif
118
119 /*
120  * POSIX requires that these be defined in <unistd.h>, but they're not always
121  * available.  If one of them has been defined, all the rest almost certainly
122  * have.
123  */
124 #ifndef STDIN_FILENO
125 #    define STDIN_FILENO  0
126 #    define STDOUT_FILENO 1
127 #    define STDERR_FILENO 2
128 #endif
129
130 /*
131  * Used for iterating through arrays.  Returns the number of elements in the
132  * array (useful for a < upper bound in a for loop).
133  */
134 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
135
136 /*
137  * The source and build versions of the tests directory.  This is used to set
138  * the C_TAP_SOURCE and C_TAP_BUILD environment variables (and the SOURCE and
139  * BUILD environment variables set for backward compatibility) and find test
140  * programs, if set.  Normally, this should be set as part of the build
141  * process to the test subdirectories of $(abs_top_srcdir) and
142  * $(abs_top_builddir) respectively.
143  */
144 #ifndef C_TAP_SOURCE
145 #    define C_TAP_SOURCE NULL
146 #endif
147 #ifndef C_TAP_BUILD
148 #    define C_TAP_BUILD NULL
149 #endif
150
151 /* Test status codes. */
152 enum test_status { TEST_FAIL, TEST_PASS, TEST_SKIP, TEST_INVALID };
153
154 /* Really, just a boolean, but this is more self-documenting. */
155 enum test_verbose { CONCISE = 0, VERBOSE = 1 };
156
157 /* Indicates the state of our plan. */
158 enum plan_status {
159     PLAN_INIT,    /* Nothing seen yet. */
160     PLAN_FIRST,   /* Plan seen before any tests. */
161     PLAN_PENDING, /* Test seen and no plan yet. */
162     PLAN_FINAL    /* Plan seen after some tests. */
163 };
164
165 /* Error exit statuses for test processes. */
166 #define CHILDERR_DUP    100 /* Couldn't redirect stderr or stdout. */
167 #define CHILDERR_EXEC   101 /* Couldn't exec child process. */
168 #define CHILDERR_STDIN  102 /* Couldn't open stdin file. */
169 #define CHILDERR_STDERR 103 /* Couldn't open stderr file. */
170
171 /* Structure to hold data for a set of tests. */
172 struct testset {
173     char *file;                /* The file name of the test. */
174     char *path;                /* The path to the test program. */
175     enum plan_status plan;     /* The status of our plan. */
176     unsigned long count;       /* Expected count of tests. */
177     unsigned long current;     /* The last seen test number. */
178     unsigned int length;       /* The length of the last status message. */
179     unsigned long passed;      /* Count of passing tests. */
180     unsigned long failed;      /* Count of failing lists. */
181     unsigned long skipped;     /* Count of skipped tests (passed). */
182     unsigned long allocated;   /* The size of the results table. */
183     enum test_status *results; /* Table of results by test number. */
184     unsigned int aborted;      /* Whether the set was aborted. */
185     int reported;              /* Whether the results were reported. */
186     int status;                /* The exit status of the test. */
187     unsigned int all_skipped;  /* Whether all tests were skipped. */
188     char *reason;              /* Why all tests were skipped. */
189 };
190
191 /* Structure to hold a linked list of test sets. */
192 struct testlist {
193     struct testset *ts;
194     struct testlist *next;
195 };
196
197 /*
198  * Usage message.  Should be used as a printf format with four arguments: the
199  * path to runtests, given three times, and the usage_description.  This is
200  * split into variables to satisfy the pedantic ISO C90 limit on strings.
201  */
202 static const char usage_message[] = "\
203 Usage: %s [-hv] [-b <build-dir>] [-s <source-dir>] <test> ...\n\
204        %s [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>\n\
205        %s -o [-h] [-b <build-dir>] [-s <source-dir>] <test>\n\
206 \n\
207 Options:\n\
208     -b <build-dir>      Set the build directory to <build-dir>\n\
209 %s";
210 static const char usage_extra[] = "\
211     -l <list>           Take the list of tests to run from <test-list>\n\
212     -o                  Run a single test rather than a list of tests\n\
213     -s <source-dir>     Set the source directory to <source-dir>\n\
214     -v                  Show the full output of each test\n\
215 \n\
216 runtests normally runs each test listed on the command line.  With the -l\n\
217 option, it instead runs every test listed in a file.  With the -o option,\n\
218 it instead runs a single test and shows its complete output.\n";
219
220 /*
221  * Header used for test output.  %s is replaced by the file name of the list
222  * of tests.
223  */
224 static const char banner[] = "\n\
225 Running all tests listed in %s.  If any tests fail, run the failing\n\
226 test program with runtests -o to see more details.\n\n";
227
228 /* Header for reports of failed tests. */
229 static const char header[] = "\n\
230 Failed Set                 Fail/Total (%) Skip Stat  Failing Tests\n\
231 -------------------------- -------------- ---- ----  ------------------------";
232
233 /* Include the file name and line number in malloc failures. */
234 #define xcalloc(n, size) x_calloc((n), (size), __FILE__, __LINE__)
235 #define xmalloc(size)    x_malloc((size), __FILE__, __LINE__)
236 #define xstrdup(p)       x_strdup((p), __FILE__, __LINE__)
237 #define xreallocarray(p, n, size) \
238     x_reallocarray((p), (n), (size), __FILE__, __LINE__)
239
240 /*
241  * __attribute__ is available in gcc 2.5 and later, but only with gcc 2.7
242  * could you use the __format__ form of the attributes, which is what we use
243  * (to avoid confusion with other macros).
244  */
245 #ifndef __attribute__
246 #    if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
247 #        define __attribute__(spec) /* empty */
248 #    endif
249 #endif
250
251 /*
252  * We use __alloc_size__, but it was only available in fairly recent versions
253  * of GCC.  Suppress warnings about the unknown attribute if GCC is too old.
254  * We know that we're GCC at this point, so we can use the GCC variadic macro
255  * extension, which will still work with versions of GCC too old to have C99
256  * variadic macro support.
257  */
258 #if !defined(__attribute__) && !defined(__alloc_size__)
259 #    if defined(__GNUC__) && !defined(__clang__)
260 #        if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
261 #            define __alloc_size__(spec, args...) /* empty */
262 #        endif
263 #    endif
264 #endif
265
266 /*
267  * LLVM and Clang pretend to be GCC but don't support all of the __attribute__
268  * settings that GCC does.  For them, suppress warnings about unknown
269  * attributes on declarations.  This unfortunately will affect the entire
270  * compilation context, but there's no push and pop available.
271  */
272 #if !defined(__attribute__) && (defined(__llvm__) || defined(__clang__))
273 #    pragma GCC diagnostic ignored "-Wattributes"
274 #endif
275
276 /* Declare internal functions that benefit from compiler attributes. */
277 static void sysdie(const char *, ...)
278     __attribute__((__nonnull__, __noreturn__, __format__(printf, 1, 2)));
279 static void *x_calloc(size_t, size_t, const char *, int)
280     __attribute__((__alloc_size__(1, 2), __malloc__, __nonnull__));
281 static void *x_malloc(size_t, const char *, int)
282     __attribute__((__alloc_size__(1), __malloc__, __nonnull__));
283 static void *x_reallocarray(void *, size_t, size_t, const char *, int)
284     __attribute__((__alloc_size__(2, 3), __malloc__, __nonnull__(4)));
285 static char *x_strdup(const char *, const char *, int)
286     __attribute__((__malloc__, __nonnull__));
287
288
289 /*
290  * Report a fatal error, including the results of strerror, and exit.
291  */
292 static void
293 sysdie(const char *format, ...)
294 {
295     int oerrno;
296     va_list args;
297
298     oerrno = errno;
299     fflush(stdout);
300     fprintf(stderr, "runtests: ");
301     va_start(args, format);
302     vfprintf(stderr, format, args);
303     va_end(args);
304     fprintf(stderr, ": %s\n", strerror(oerrno));
305     exit(1);
306 }
307
308
309 /*
310  * Allocate zeroed memory, reporting a fatal error and exiting on failure.
311  */
312 static void *
313 x_calloc(size_t n, size_t size, const char *file, int line)
314 {
315     void *p;
316
317     n = (n > 0) ? n : 1;
318     size = (size > 0) ? size : 1;
319     p = calloc(n, size);
320     if (p == NULL)
321         sysdie("failed to calloc %lu bytes at %s line %d",
322                (unsigned long) size, file, line);
323     return p;
324 }
325
326
327 /*
328  * Allocate memory, reporting a fatal error and exiting on failure.
329  */
330 static void *
331 x_malloc(size_t size, const char *file, int line)
332 {
333     void *p;
334
335     p = malloc(size);
336     if (p == NULL)
337         sysdie("failed to malloc %lu bytes at %s line %d",
338                (unsigned long) size, file, line);
339     return p;
340 }
341
342
343 /*
344  * Reallocate memory, reporting a fatal error and exiting on failure.
345  *
346  * We should technically use SIZE_MAX here for the overflow check, but
347  * SIZE_MAX is C99 and we're only assuming C89 + SUSv3, which does not
348  * guarantee that it exists.  They do guarantee that UINT_MAX exists, and we
349  * can assume that UINT_MAX <= SIZE_MAX.  And we should not be allocating
350  * anything anywhere near that large.
351  *
352  * (In theory, C89 and C99 permit size_t to be smaller than unsigned int, but
353  * I disbelieve in the existence of such systems and they will have to cope
354  * without overflow checks.)
355  */
356 static void *
357 x_reallocarray(void *p, size_t n, size_t size, const char *file, int line)
358 {
359     if (n > 0 && UINT_MAX / n <= size)
360         sysdie("realloc too large at %s line %d", file, line);
361     p = realloc(p, n * size);
362     if (p == NULL)
363         sysdie("failed to realloc %lu bytes at %s line %d",
364                (unsigned long) (n * size), file, line);
365     return p;
366 }
367
368
369 /*
370  * Copy a string, reporting a fatal error and exiting on failure.
371  */
372 static char *
373 x_strdup(const char *s, const char *file, int line)
374 {
375     char *p;
376     size_t len;
377
378     len = strlen(s) + 1;
379     p = malloc(len);
380     if (p == NULL)
381         sysdie("failed to strdup %lu bytes at %s line %d", (unsigned long) len,
382                file, line);
383     memcpy(p, s, len);
384     return p;
385 }
386
387
388 /*
389  * Form a new string by concatenating multiple strings.  The arguments must be
390  * terminated by (const char *) 0.
391  *
392  * This function only exists because we can't assume asprintf.  We can't
393  * simulate asprintf with snprintf because we're only assuming SUSv3, which
394  * does not require that snprintf with a NULL buffer return the required
395  * length.  When those constraints are relaxed, this should be ripped out and
396  * replaced with asprintf or a more trivial replacement with snprintf.
397  */
398 static char *
399 concat(const char *first, ...)
400 {
401     va_list args;
402     char *result;
403     const char *string;
404     size_t offset;
405     size_t length = 0;
406
407     /*
408      * Find the total memory required.  Ensure we don't overflow length.  We
409      * aren't guaranteed to have SIZE_MAX, so use UINT_MAX as an acceptable
410      * substitute (see the x_nrealloc comments).
411      */
412     va_start(args, first);
413     for (string = first; string != NULL; string = va_arg(args, const char *)) {
414         if (length >= UINT_MAX - strlen(string)) {
415             errno = EINVAL;
416             sysdie("strings too long in concat");
417         }
418         length += strlen(string);
419     }
420     va_end(args);
421     length++;
422
423     /* Create the string. */
424     result = xmalloc(length);
425     va_start(args, first);
426     offset = 0;
427     for (string = first; string != NULL; string = va_arg(args, const char *)) {
428         memcpy(result + offset, string, strlen(string));
429         offset += strlen(string);
430     }
431     va_end(args);
432     result[offset] = '\0';
433     return result;
434 }
435
436
437 /*
438  * Given a struct timeval, return the number of seconds it represents as a
439  * double.  Use difftime() to convert a time_t to a double.
440  */
441 static double
442 tv_seconds(const struct timeval *tv)
443 {
444     return difftime(tv->tv_sec, 0) + tv->tv_usec * 1e-6;
445 }
446
447
448 /*
449  * Given two struct timevals, return the difference in seconds.
450  */
451 static double
452 tv_diff(const struct timeval *tv1, const struct timeval *tv0)
453 {
454     return tv_seconds(tv1) - tv_seconds(tv0);
455 }
456
457
458 /*
459  * Given two struct timevals, return the sum in seconds as a double.
460  */
461 static double
462 tv_sum(const struct timeval *tv1, const struct timeval *tv2)
463 {
464     return tv_seconds(tv1) + tv_seconds(tv2);
465 }
466
467
468 /*
469  * Given a pointer to a string, skip any leading whitespace and return a
470  * pointer to the first non-whitespace character.
471  */
472 static const char *
473 skip_whitespace(const char *p)
474 {
475     while (isspace((unsigned char) (*p)))
476         p++;
477     return p;
478 }
479
480
481 /*
482  * Start a program, connecting its stdout to a pipe on our end and its stderr
483  * to /dev/null, and storing the file descriptor to read from in the two
484  * argument.  Returns the PID of the new process.  Errors are fatal.
485  */
486 static pid_t
487 test_start(const char *path, int *fd)
488 {
489     int fds[2], infd, errfd;
490     pid_t child;
491
492     /* Create a pipe used to capture the output from the test program. */
493     if (pipe(fds) == -1) {
494         puts("ABORTED");
495         fflush(stdout);
496         sysdie("can't create pipe");
497     }
498
499     /* Fork a child process, massage the file descriptors, and exec. */
500     child = fork();
501     switch (child) {
502     case -1:
503         puts("ABORTED");
504         fflush(stdout);
505         sysdie("can't fork");
506
507     /* In the child.  Set up our standard output. */
508     case 0:
509         close(fds[0]);
510         close(STDOUT_FILENO);
511         if (dup2(fds[1], STDOUT_FILENO) < 0)
512             _exit(CHILDERR_DUP);
513         close(fds[1]);
514
515         /* Point standard input at /dev/null. */
516         close(STDIN_FILENO);
517         infd = open("/dev/null", O_RDONLY);
518         if (infd < 0)
519             _exit(CHILDERR_STDIN);
520         if (infd != STDIN_FILENO) {
521             if (dup2(infd, STDIN_FILENO) < 0)
522                 _exit(CHILDERR_DUP);
523             close(infd);
524         }
525
526         /* Point standard error at /dev/null. */
527         close(STDERR_FILENO);
528         errfd = open("/dev/null", O_WRONLY);
529         if (errfd < 0)
530             _exit(CHILDERR_STDERR);
531         if (errfd != STDERR_FILENO) {
532             if (dup2(errfd, STDERR_FILENO) < 0)
533                 _exit(CHILDERR_DUP);
534             close(errfd);
535         }
536
537         /* Now, exec our process. */
538         if (execl(path, path, (char *) 0) == -1)
539             _exit(CHILDERR_EXEC);
540
541     /* In parent.  Close the extra file descriptor. */
542     default:
543         close(fds[1]);
544         break;
545     }
546     *fd = fds[0];
547     return child;
548 }
549
550
551 /*
552  * Back up over the output saying what test we were executing.
553  */
554 static void
555 test_backspace(struct testset *ts)
556 {
557     unsigned int i;
558
559     if (!isatty(STDOUT_FILENO))
560         return;
561     for (i = 0; i < ts->length; i++)
562         putchar('\b');
563     for (i = 0; i < ts->length; i++)
564         putchar(' ');
565     for (i = 0; i < ts->length; i++)
566         putchar('\b');
567     ts->length = 0;
568 }
569
570
571 /*
572  * Allocate or resize the array of test results to be large enough to contain
573  * the test number in.
574  */
575 static void
576 resize_results(struct testset *ts, unsigned long n)
577 {
578     unsigned long i;
579     size_t s;
580
581     /* If there's already enough space, return quickly. */
582     if (n <= ts->allocated)
583         return;
584
585     /*
586      * If no space has been allocated, do the initial allocation.  Otherwise,
587      * resize.  Start with 32 test cases and then add 1024 with each resize to
588      * try to reduce the number of reallocations.
589      */
590     if (ts->allocated == 0) {
591         s = (n > 32) ? n : 32;
592         ts->results = xcalloc(s, sizeof(enum test_status));
593     } else {
594         s = (n > ts->allocated + 1024) ? n : ts->allocated + 1024;
595         ts->results = xreallocarray(ts->results, s, sizeof(enum test_status));
596     }
597
598     /* Set the results for the newly-allocated test array. */
599     for (i = ts->allocated; i < s; i++)
600         ts->results[i] = TEST_INVALID;
601     ts->allocated = s;
602 }
603
604
605 /*
606  * Report an invalid test number and set the appropriate flags.  Pulled into a
607  * separate function since we do this in several places.
608  */
609 static void
610 invalid_test_number(struct testset *ts, long n, enum test_verbose verbose)
611 {
612     if (!verbose)
613         test_backspace(ts);
614     printf("ABORTED (invalid test number %ld)\n", n);
615     ts->aborted = 1;
616     ts->reported = 1;
617 }
618
619
620 /*
621  * Read the plan line of test output, which should contain the range of test
622  * numbers.  We may initialize the testset structure here if we haven't yet
623  * seen a test.  Return true if initialization succeeded and the test should
624  * continue, false otherwise.
625  */
626 static int
627 test_plan(const char *line, struct testset *ts, enum test_verbose verbose)
628 {
629     long n;
630
631     /*
632      * Accept a plan without the leading 1.. for compatibility with older
633      * versions of runtests.  This will only be allowed if we've not yet seen
634      * a test result.
635      */
636     line = skip_whitespace(line);
637     if (strncmp(line, "1..", 3) == 0)
638         line += 3;
639
640     /*
641      * Get the count and check it for validity.
642      *
643      * If we have something of the form "1..0 # skip foo", the whole file was
644      * skipped; record that.  If we do skip the whole file, zero out all of
645      * our statistics, since they're no longer relevant.
646      *
647      * strtol is called with a second argument to advance the line pointer
648      * past the count to make it simpler to detect the # skip case.
649      */
650     n = strtol(line, (char **) &line, 10);
651     if (n == 0) {
652         line = skip_whitespace(line);
653         if (*line == '#') {
654             line = skip_whitespace(line + 1);
655             if (strncasecmp(line, "skip", 4) == 0) {
656                 line = skip_whitespace(line + 4);
657                 if (*line != '\0') {
658                     ts->reason = xstrdup(line);
659                     ts->reason[strlen(ts->reason) - 1] = '\0';
660                 }
661                 ts->all_skipped = 1;
662                 ts->aborted = 1;
663                 ts->count = 0;
664                 ts->passed = 0;
665                 ts->skipped = 0;
666                 ts->failed = 0;
667                 return 0;
668             }
669         }
670     }
671     if (n <= 0) {
672         puts("ABORTED (invalid test count)");
673         ts->aborted = 1;
674         ts->reported = 1;
675         return 0;
676     }
677
678     /*
679      * If we are doing lazy planning, check the plan against the largest test
680      * number that we saw and fail now if we saw a check outside the plan
681      * range.
682      */
683     if (ts->plan == PLAN_PENDING && (unsigned long) n < ts->count) {
684         invalid_test_number(ts, (long) ts->count, verbose);
685         return 0;
686     }
687
688     /*
689      * Otherwise, allocated or resize the results if needed and update count,
690      * and then record that we've seen a plan.
691      */
692     resize_results(ts, (unsigned long) n);
693     ts->count = (unsigned long) n;
694     if (ts->plan == PLAN_INIT)
695         ts->plan = PLAN_FIRST;
696     else if (ts->plan == PLAN_PENDING)
697         ts->plan = PLAN_FINAL;
698     return 1;
699 }
700
701
702 /*
703  * Given a single line of output from a test, parse it and return the success
704  * status of that test.  Anything printed to stdout not matching the form
705  * /^(not )?ok \d+/ is ignored.  Sets ts->current to the test number that just
706  * reported status.
707  */
708 static void
709 test_checkline(const char *line, struct testset *ts, enum test_verbose verbose)
710 {
711     enum test_status status = TEST_PASS;
712     const char *bail;
713     char *end;
714     long number;
715     unsigned long current;
716     int outlen;
717
718     /* Before anything, check for a test abort. */
719     bail = strstr(line, "Bail out!");
720     if (bail != NULL) {
721         bail = skip_whitespace(bail + strlen("Bail out!"));
722         if (*bail != '\0') {
723             size_t length;
724
725             length = strlen(bail);
726             if (bail[length - 1] == '\n')
727                 length--;
728             if (!verbose)
729                 test_backspace(ts);
730             printf("ABORTED (%.*s)\n", (int) length, bail);
731             ts->reported = 1;
732         }
733         ts->aborted = 1;
734         return;
735     }
736
737     /*
738      * If the given line isn't newline-terminated, it was too big for an
739      * fgets(), which means ignore it.
740      */
741     if (line[strlen(line) - 1] != '\n')
742         return;
743
744     /* If the line begins with a hash mark, ignore it. */
745     if (line[0] == '#')
746         return;
747
748     /* If we haven't yet seen a plan, look for one. */
749     if (ts->plan == PLAN_INIT && isdigit((unsigned char) (*line))) {
750         if (!test_plan(line, ts, verbose))
751             return;
752     } else if (strncmp(line, "1..", 3) == 0) {
753         if (ts->plan == PLAN_PENDING) {
754             if (!test_plan(line, ts, verbose))
755                 return;
756         } else {
757             if (!verbose)
758                 test_backspace(ts);
759             puts("ABORTED (multiple plans)");
760             ts->aborted = 1;
761             ts->reported = 1;
762             return;
763         }
764     }
765
766     /* Parse the line, ignoring something we can't parse. */
767     if (strncmp(line, "not ", 4) == 0) {
768         status = TEST_FAIL;
769         line += 4;
770     }
771     if (strncmp(line, "ok", 2) != 0)
772         return;
773     line = skip_whitespace(line + 2);
774     errno = 0;
775     number = strtol(line, &end, 10);
776     if (errno != 0 || end == line)
777         current = ts->current + 1;
778     else if (number <= 0) {
779         invalid_test_number(ts, number, verbose);
780         return;
781     } else
782         current = (unsigned long) number;
783     if (current > ts->count && ts->plan == PLAN_FIRST) {
784         invalid_test_number(ts, (long) current, verbose);
785         return;
786     }
787
788     /* We have a valid test result.  Tweak the results array if needed. */
789     if (ts->plan == PLAN_INIT || ts->plan == PLAN_PENDING) {
790         ts->plan = PLAN_PENDING;
791         resize_results(ts, current);
792         if (current > ts->count)
793             ts->count = current;
794     }
795
796     /*
797      * Handle directives.  We should probably do something more interesting
798      * with unexpected passes of todo tests.
799      */
800     while (isdigit((unsigned char) (*line)))
801         line++;
802     line = skip_whitespace(line);
803     if (*line == '#') {
804         line = skip_whitespace(line + 1);
805         if (strncasecmp(line, "skip", 4) == 0)
806             status = TEST_SKIP;
807         if (strncasecmp(line, "todo", 4) == 0)
808             status = (status == TEST_FAIL) ? TEST_SKIP : TEST_FAIL;
809     }
810
811     /* Make sure that the test number is in range and not a duplicate. */
812     if (ts->results[current - 1] != TEST_INVALID) {
813         if (!verbose)
814             test_backspace(ts);
815         printf("ABORTED (duplicate test number %lu)\n", current);
816         ts->aborted = 1;
817         ts->reported = 1;
818         return;
819     }
820
821     /* Good results.  Increment our various counters. */
822     switch (status) {
823     case TEST_PASS:
824         ts->passed++;
825         break;
826     case TEST_FAIL:
827         ts->failed++;
828         break;
829     case TEST_SKIP:
830         ts->skipped++;
831         break;
832     case TEST_INVALID:
833         break;
834     }
835     ts->current = current;
836     ts->results[current - 1] = status;
837     if (!verbose && isatty(STDOUT_FILENO)) {
838         test_backspace(ts);
839         if (ts->plan == PLAN_PENDING)
840             outlen = printf("%lu/?", current);
841         else
842             outlen = printf("%lu/%lu", current, ts->count);
843         ts->length = (outlen >= 0) ? (unsigned int) outlen : 0;
844         fflush(stdout);
845     }
846 }
847
848
849 /*
850  * Print out a range of test numbers, returning the number of characters it
851  * took up.  Takes the first number, the last number, the number of characters
852  * already printed on the line, and the limit of number of characters the line
853  * can hold.  Add a comma and a space before the range if chars indicates that
854  * something has already been printed on the line, and print ... instead if
855  * chars plus the space needed would go over the limit (use a limit of 0 to
856  * disable this).
857  */
858 static unsigned int
859 test_print_range(unsigned long first, unsigned long last, unsigned long chars,
860                  unsigned int limit)
861 {
862     unsigned int needed = 0;
863     unsigned long n;
864
865     for (n = first; n > 0; n /= 10)
866         needed++;
867     if (last > first) {
868         for (n = last; n > 0; n /= 10)
869             needed++;
870         needed++;
871     }
872     if (chars > 0)
873         needed += 2;
874     if (limit > 0 && chars + needed > limit) {
875         needed = 0;
876         if (chars <= limit) {
877             if (chars > 0) {
878                 printf(", ");
879                 needed += 2;
880             }
881             printf("...");
882             needed += 3;
883         }
884     } else {
885         if (chars > 0)
886             printf(", ");
887         if (last > first)
888             printf("%lu-", first);
889         printf("%lu", last);
890     }
891     return needed;
892 }
893
894
895 /*
896  * Summarize a single test set.  The second argument is 0 if the set exited
897  * cleanly, a positive integer representing the exit status if it exited
898  * with a non-zero status, and a negative integer representing the signal
899  * that terminated it if it was killed by a signal.
900  */
901 static void
902 test_summarize(struct testset *ts, int status)
903 {
904     unsigned long i;
905     unsigned long missing = 0;
906     unsigned long failed = 0;
907     unsigned long first = 0;
908     unsigned long last = 0;
909
910     if (ts->aborted) {
911         fputs("ABORTED", stdout);
912         if (ts->count > 0)
913             printf(" (passed %lu/%lu)", ts->passed, ts->count - ts->skipped);
914     } else {
915         for (i = 0; i < ts->count; i++) {
916             if (ts->results[i] == TEST_INVALID) {
917                 if (missing == 0)
918                     fputs("MISSED ", stdout);
919                 if (first && i == last)
920                     last = i + 1;
921                 else {
922                     if (first)
923                         test_print_range(first, last, missing - 1, 0);
924                     missing++;
925                     first = i + 1;
926                     last = i + 1;
927                 }
928             }
929         }
930         if (first)
931             test_print_range(first, last, missing - 1, 0);
932         first = 0;
933         last = 0;
934         for (i = 0; i < ts->count; i++) {
935             if (ts->results[i] == TEST_FAIL) {
936                 if (missing && !failed)
937                     fputs("; ", stdout);
938                 if (failed == 0)
939                     fputs("FAILED ", stdout);
940                 if (first && i == last)
941                     last = i + 1;
942                 else {
943                     if (first)
944                         test_print_range(first, last, failed - 1, 0);
945                     failed++;
946                     first = i + 1;
947                     last = i + 1;
948                 }
949             }
950         }
951         if (first)
952             test_print_range(first, last, failed - 1, 0);
953         if (!missing && !failed) {
954             fputs(!status ? "ok" : "dubious", stdout);
955             if (ts->skipped > 0) {
956                 if (ts->skipped == 1)
957                     printf(" (skipped %lu test)", ts->skipped);
958                 else
959                     printf(" (skipped %lu tests)", ts->skipped);
960             }
961         }
962     }
963     if (status > 0)
964         printf(" (exit status %d)", status);
965     else if (status < 0)
966         printf(" (killed by signal %d%s)", -status,
967                WCOREDUMP(ts->status) ? ", core dumped" : "");
968     putchar('\n');
969 }
970
971
972 /*
973  * Given a test set, analyze the results, classify the exit status, handle a
974  * few special error messages, and then pass it along to test_summarize() for
975  * the regular output.  Returns true if the test set ran successfully and all
976  * tests passed or were skipped, false otherwise.
977  */
978 static int
979 test_analyze(struct testset *ts)
980 {
981     if (ts->reported)
982         return 0;
983     if (ts->all_skipped) {
984         if (ts->reason == NULL)
985             puts("skipped");
986         else
987             printf("skipped (%s)\n", ts->reason);
988         return 1;
989     } else if (WIFEXITED(ts->status) && WEXITSTATUS(ts->status) != 0) {
990         switch (WEXITSTATUS(ts->status)) {
991         case CHILDERR_DUP:
992             if (!ts->reported)
993                 puts("ABORTED (can't dup file descriptors)");
994             break;
995         case CHILDERR_EXEC:
996             if (!ts->reported)
997                 puts("ABORTED (execution failed -- not found?)");
998             break;
999         case CHILDERR_STDIN:
1000         case CHILDERR_STDERR:
1001             if (!ts->reported)
1002                 puts("ABORTED (can't open /dev/null)");
1003             break;
1004         default:
1005             test_summarize(ts, WEXITSTATUS(ts->status));
1006             break;
1007         }
1008         return 0;
1009     } else if (WIFSIGNALED(ts->status)) {
1010         test_summarize(ts, -WTERMSIG(ts->status));
1011         return 0;
1012     } else if (ts->plan != PLAN_FIRST && ts->plan != PLAN_FINAL) {
1013         puts("ABORTED (no valid test plan)");
1014         ts->aborted = 1;
1015         return 0;
1016     } else {
1017         test_summarize(ts, 0);
1018         return (ts->failed == 0);
1019     }
1020 }
1021
1022
1023 /*
1024  * Runs a single test set, accumulating and then reporting the results.
1025  * Returns true if the test set was successfully run and all tests passed,
1026  * false otherwise.
1027  */
1028 static int
1029 test_run(struct testset *ts, enum test_verbose verbose)
1030 {
1031     pid_t testpid, child;
1032     int outfd, status;
1033     unsigned long i;
1034     FILE *output;
1035     char buffer[BUFSIZ];
1036
1037     /* Run the test program. */
1038     testpid = test_start(ts->path, &outfd);
1039     output = fdopen(outfd, "r");
1040     if (!output) {
1041         puts("ABORTED");
1042         fflush(stdout);
1043         sysdie("fdopen failed");
1044     }
1045
1046     /*
1047      * Pass each line of output to test_checkline(), and print the line if
1048      * verbosity is requested.
1049      */
1050     while (!ts->aborted && fgets(buffer, sizeof(buffer), output)) {
1051         if (verbose)
1052             printf("%s", buffer);
1053         test_checkline(buffer, ts, verbose);
1054     }
1055     if (ferror(output) || ts->plan == PLAN_INIT)
1056         ts->aborted = 1;
1057     if (!verbose)
1058         test_backspace(ts);
1059
1060     /*
1061      * Consume the rest of the test output, close the output descriptor,
1062      * retrieve the exit status, and pass that information to test_analyze()
1063      * for eventual output.
1064      */
1065     while (fgets(buffer, sizeof(buffer), output))
1066         if (verbose)
1067             printf("%s", buffer);
1068     fclose(output);
1069     child = waitpid(testpid, &ts->status, 0);
1070     if (child == (pid_t) -1) {
1071         if (!ts->reported) {
1072             puts("ABORTED");
1073             fflush(stdout);
1074         }
1075         sysdie("waitpid for %u failed", (unsigned int) testpid);
1076     }
1077     if (ts->all_skipped)
1078         ts->aborted = 0;
1079     status = test_analyze(ts);
1080
1081     /* Convert missing tests to failed tests. */
1082     for (i = 0; i < ts->count; i++) {
1083         if (ts->results[i] == TEST_INVALID) {
1084             ts->failed++;
1085             ts->results[i] = TEST_FAIL;
1086             status = 0;
1087         }
1088     }
1089     return status;
1090 }
1091
1092
1093 /* Summarize a list of test failures. */
1094 static void
1095 test_fail_summary(const struct testlist *fails)
1096 {
1097     struct testset *ts;
1098     unsigned int chars;
1099     unsigned long i, first, last, total;
1100
1101     puts(header);
1102
1103     /* Failed Set                 Fail/Total (%) Skip Stat  Failing (25)
1104        -------------------------- -------------- ---- ----  -------------- */
1105     for (; fails; fails = fails->next) {
1106         ts = fails->ts;
1107         total = ts->count - ts->skipped;
1108         printf("%-26.26s %4lu/%-4lu %3.0f%% %4lu ", ts->file, ts->failed,
1109                total, total ? (ts->failed * 100.0) / total : 0, ts->skipped);
1110         if (WIFEXITED(ts->status))
1111             printf("%4d  ", WEXITSTATUS(ts->status));
1112         else
1113             printf("  --  ");
1114         if (ts->aborted) {
1115             puts("aborted");
1116             continue;
1117         }
1118         chars = 0;
1119         first = 0;
1120         last = 0;
1121         for (i = 0; i < ts->count; i++) {
1122             if (ts->results[i] == TEST_FAIL) {
1123                 if (first != 0 && i == last)
1124                     last = i + 1;
1125                 else {
1126                     if (first != 0)
1127                         chars += test_print_range(first, last, chars, 19);
1128                     first = i + 1;
1129                     last = i + 1;
1130                 }
1131             }
1132         }
1133         if (first != 0)
1134             test_print_range(first, last, chars, 19);
1135         putchar('\n');
1136     }
1137 }
1138
1139
1140 /*
1141  * Check whether a given file path is a valid test.  Currently, this checks
1142  * whether it is executable and is a regular file.  Returns true or false.
1143  */
1144 static int
1145 is_valid_test(const char *path)
1146 {
1147     struct stat st;
1148
1149     if (access(path, X_OK) < 0)
1150         return 0;
1151     if (stat(path, &st) < 0)
1152         return 0;
1153     if (!S_ISREG(st.st_mode))
1154         return 0;
1155     return 1;
1156 }
1157
1158
1159 /*
1160  * Given the name of a test, a pointer to the testset struct, and the source
1161  * and build directories, find the test.  We try first relative to the current
1162  * directory, then in the build directory (if not NULL), then in the source
1163  * directory.  In each of those directories, we first try a "-t" extension and
1164  * then a ".t" extension.  When we find an executable program, we return the
1165  * path to that program.  If none of those paths are executable, just fill in
1166  * the name of the test as is.
1167  *
1168  * The caller is responsible for freeing the path member of the testset
1169  * struct.
1170  */
1171 static char *
1172 find_test(const char *name, const char *source, const char *build)
1173 {
1174     char *path = NULL;
1175     const char *bases[3], *suffix, *base;
1176     unsigned int i, j;
1177     const char *suffixes[3] = {"-t", ".t", ""};
1178
1179     /* Possible base directories. */
1180     bases[0] = ".";
1181     bases[1] = build;
1182     bases[2] = source;
1183
1184     /* Try each suffix with each base. */
1185     for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1186         suffix = suffixes[i];
1187         for (j = 0; j < ARRAY_SIZE(bases); j++) {
1188             base = bases[j];
1189             if (base == NULL)
1190                 continue;
1191             path = concat(base, "/", name, suffix, (const char *) 0);
1192             if (is_valid_test(path))
1193                 return path;
1194             free(path);
1195             path = NULL;
1196         }
1197     }
1198     if (path == NULL)
1199         path = xstrdup(name);
1200     return path;
1201 }
1202
1203
1204 /*
1205  * Read a list of tests from a file, returning the list of tests as a struct
1206  * testlist, or NULL if there were no tests (such as a file containing only
1207  * comments).  Reports an error to standard error and exits if the list of
1208  * tests cannot be read.
1209  */
1210 static struct testlist *
1211 read_test_list(const char *filename)
1212 {
1213     FILE *file;
1214     unsigned int line;
1215     size_t length;
1216     char buffer[BUFSIZ];
1217     const char *testname;
1218     struct testlist *listhead, *current;
1219
1220     /* Create the initial container list that will hold our results. */
1221     listhead = xcalloc(1, sizeof(struct testlist));
1222     current = NULL;
1223
1224     /*
1225      * Open our file of tests to run and read it line by line, creating a new
1226      * struct testlist and struct testset for each line.
1227      */
1228     file = fopen(filename, "r");
1229     if (file == NULL)
1230         sysdie("can't open %s", filename);
1231     line = 0;
1232     while (fgets(buffer, sizeof(buffer), file)) {
1233         line++;
1234         length = strlen(buffer) - 1;
1235         if (buffer[length] != '\n') {
1236             fprintf(stderr, "%s:%u: line too long\n", filename, line);
1237             exit(1);
1238         }
1239         buffer[length] = '\0';
1240
1241         /* Skip comments, leading spaces, and blank lines. */
1242         testname = skip_whitespace(buffer);
1243         if (strlen(testname) == 0)
1244             continue;
1245         if (testname[0] == '#')
1246             continue;
1247
1248         /* Allocate the new testset structure. */
1249         if (current == NULL)
1250             current = listhead;
1251         else {
1252             current->next = xcalloc(1, sizeof(struct testlist));
1253             current = current->next;
1254         }
1255         current->ts = xcalloc(1, sizeof(struct testset));
1256         current->ts->plan = PLAN_INIT;
1257         current->ts->file = xstrdup(testname);
1258     }
1259     fclose(file);
1260
1261     /* If there were no tests, current is still NULL. */
1262     if (current == NULL) {
1263         free(listhead);
1264         return NULL;
1265     }
1266
1267     /* Return the results. */
1268     return listhead;
1269 }
1270
1271
1272 /*
1273  * Build a list of tests from command line arguments.  Takes the argv and argc
1274  * representing the command line arguments and returns a newly allocated test
1275  * list, or NULL if there were no tests.  The caller is responsible for
1276  * freeing.
1277  */
1278 static struct testlist *
1279 build_test_list(char *argv[], int argc)
1280 {
1281     int i;
1282     struct testlist *listhead, *current;
1283
1284     /* Create the initial container list that will hold our results. */
1285     listhead = xcalloc(1, sizeof(struct testlist));
1286     current = NULL;
1287
1288     /* Walk the list of arguments and create test sets for them. */
1289     for (i = 0; i < argc; i++) {
1290         if (current == NULL)
1291             current = listhead;
1292         else {
1293             current->next = xcalloc(1, sizeof(struct testlist));
1294             current = current->next;
1295         }
1296         current->ts = xcalloc(1, sizeof(struct testset));
1297         current->ts->plan = PLAN_INIT;
1298         current->ts->file = xstrdup(argv[i]);
1299     }
1300
1301     /* If there were no tests, current is still NULL. */
1302     if (current == NULL) {
1303         free(listhead);
1304         return NULL;
1305     }
1306
1307     /* Return the results. */
1308     return listhead;
1309 }
1310
1311
1312 /* Free a struct testset. */
1313 static void
1314 free_testset(struct testset *ts)
1315 {
1316     free(ts->file);
1317     free(ts->path);
1318     free(ts->results);
1319     free(ts->reason);
1320     free(ts);
1321 }
1322
1323
1324 /*
1325  * Run a batch of tests.  Takes two additional parameters: the root of the
1326  * source directory and the root of the build directory.  Test programs will
1327  * be first searched for in the current directory, then the build directory,
1328  * then the source directory.  Returns true iff all tests passed, and always
1329  * frees the test list that's passed in.
1330  */
1331 static int
1332 test_batch(struct testlist *tests, const char *source, const char *build,
1333            enum test_verbose verbose)
1334 {
1335     size_t length, i;
1336     size_t longest = 0;
1337     unsigned int count = 0;
1338     struct testset *ts;
1339     struct timeval start, end;
1340     struct rusage stats;
1341     struct testlist *failhead = NULL;
1342     struct testlist *failtail = NULL;
1343     struct testlist *current, *next;
1344     int succeeded;
1345     unsigned long total = 0;
1346     unsigned long passed = 0;
1347     unsigned long skipped = 0;
1348     unsigned long failed = 0;
1349     unsigned long aborted = 0;
1350
1351     /* Walk the list of tests to find the longest name. */
1352     for (current = tests; current != NULL; current = current->next) {
1353         length = strlen(current->ts->file);
1354         if (length > longest)
1355             longest = length;
1356     }
1357
1358     /*
1359      * Add two to longest and round up to the nearest tab stop.  This is how
1360      * wide the column for printing the current test name will be.
1361      */
1362     longest += 2;
1363     if (longest % 8)
1364         longest += 8 - (longest % 8);
1365
1366     /* Start the wall clock timer. */
1367     gettimeofday(&start, NULL);
1368
1369     /* Now, plow through our tests again, running each one. */
1370     for (current = tests; current != NULL; current = current->next) {
1371         ts = current->ts;
1372
1373         /* Print out the name of the test file. */
1374         fputs(ts->file, stdout);
1375         if (verbose)
1376             fputs("\n\n", stdout);
1377         else
1378             for (i = strlen(ts->file); i < longest; i++)
1379                 putchar('.');
1380         if (isatty(STDOUT_FILENO))
1381             fflush(stdout);
1382
1383         /* Run the test. */
1384         ts->path = find_test(ts->file, source, build);
1385         succeeded = test_run(ts, verbose);
1386         fflush(stdout);
1387         if (verbose)
1388             putchar('\n');
1389
1390         /* Record cumulative statistics. */
1391         aborted += ts->aborted;
1392         total += ts->count + ts->all_skipped;
1393         passed += ts->passed;
1394         skipped += ts->skipped + ts->all_skipped;
1395         failed += ts->failed;
1396         count++;
1397
1398         /* If the test fails, we shuffle it over to the fail list. */
1399         if (!succeeded) {
1400             if (failhead == NULL) {
1401                 failhead = xmalloc(sizeof(struct testset));
1402                 failtail = failhead;
1403             } else {
1404                 failtail->next = xmalloc(sizeof(struct testset));
1405                 failtail = failtail->next;
1406             }
1407             failtail->ts = ts;
1408             failtail->next = NULL;
1409         }
1410     }
1411     total -= skipped;
1412
1413     /* Stop the timer and get our child resource statistics. */
1414     gettimeofday(&end, NULL);
1415     getrusage(RUSAGE_CHILDREN, &stats);
1416
1417     /* Summarize the failures and free the failure list. */
1418     if (failhead != NULL) {
1419         test_fail_summary(failhead);
1420         while (failhead != NULL) {
1421             next = failhead->next;
1422             free(failhead);
1423             failhead = next;
1424         }
1425     }
1426
1427     /* Free the memory used by the test lists. */
1428     while (tests != NULL) {
1429         next = tests->next;
1430         free_testset(tests->ts);
1431         free(tests);
1432         tests = next;
1433     }
1434
1435     /* Print out the final test summary. */
1436     putchar('\n');
1437     if (aborted != 0) {
1438         if (aborted == 1)
1439             printf("Aborted %lu test set", aborted);
1440         else
1441             printf("Aborted %lu test sets", aborted);
1442         printf(", passed %lu/%lu tests", passed, total);
1443     } else if (failed == 0)
1444         fputs("All tests successful", stdout);
1445     else
1446         printf("Failed %lu/%lu tests, %.2f%% okay", failed, total,
1447                (total - failed) * 100.0 / total);
1448     if (skipped != 0) {
1449         if (skipped == 1)
1450             printf(", %lu test skipped", skipped);
1451         else
1452             printf(", %lu tests skipped", skipped);
1453     }
1454     puts(".");
1455     printf("Files=%u,  Tests=%lu", count, total);
1456     printf(",  %.2f seconds", tv_diff(&end, &start));
1457     printf(" (%.2f usr + %.2f sys = %.2f CPU)\n", tv_seconds(&stats.ru_utime),
1458            tv_seconds(&stats.ru_stime),
1459            tv_sum(&stats.ru_utime, &stats.ru_stime));
1460     return (failed == 0 && aborted == 0);
1461 }
1462
1463
1464 /*
1465  * Run a single test case.  This involves just running the test program after
1466  * having done the environment setup and finding the test program.
1467  */
1468 static void
1469 test_single(const char *program, const char *source, const char *build)
1470 {
1471     char *path;
1472
1473     path = find_test(program, source, build);
1474     if (execl(path, path, (char *) 0) == -1)
1475         sysdie("cannot exec %s", path);
1476 }
1477
1478
1479 /*
1480  * Main routine.  Set the C_TAP_SOURCE, C_TAP_BUILD, SOURCE, and BUILD
1481  * environment variables and then, given a file listing tests, run each test
1482  * listed.
1483  */
1484 int
1485 main(int argc, char *argv[])
1486 {
1487     int option;
1488     int status = 0;
1489     int single = 0;
1490     enum test_verbose verbose = CONCISE;
1491     char *c_tap_source_env = NULL;
1492     char *c_tap_build_env = NULL;
1493     char *source_env = NULL;
1494     char *build_env = NULL;
1495     const char *program;
1496     const char *shortlist;
1497     const char *list = NULL;
1498     const char *source = C_TAP_SOURCE;
1499     const char *build = C_TAP_BUILD;
1500     struct testlist *tests;
1501
1502     program = argv[0];
1503     while ((option = getopt(argc, argv, "b:hl:os:v")) != EOF) {
1504         switch (option) {
1505         case 'b':
1506             build = optarg;
1507             break;
1508         case 'h':
1509             printf(usage_message, program, program, program, usage_extra);
1510             exit(0);
1511         case 'l':
1512             list = optarg;
1513             break;
1514         case 'o':
1515             single = 1;
1516             break;
1517         case 's':
1518             source = optarg;
1519             break;
1520         case 'v':
1521             verbose = VERBOSE;
1522             break;
1523         default:
1524             exit(1);
1525         }
1526     }
1527     argv += optind;
1528     argc -= optind;
1529     if ((list == NULL && argc < 1) || (list != NULL && argc > 0)) {
1530         fprintf(stderr, usage_message, program, program, program, usage_extra);
1531         exit(1);
1532     }
1533
1534     /*
1535      * If C_TAP_VERBOSE is set in the environment, that also turns on verbose
1536      * mode.
1537      */
1538     if (getenv("C_TAP_VERBOSE") != NULL)
1539         verbose = VERBOSE;
1540
1541     /*
1542      * Set C_TAP_SOURCE and C_TAP_BUILD environment variables.  Also set
1543      * SOURCE and BUILD for backward compatibility, although we're trying to
1544      * migrate to the ones with a C_TAP_* prefix.
1545      */
1546     if (source != NULL) {
1547         c_tap_source_env = concat("C_TAP_SOURCE=", source, (const char *) 0);
1548         if (putenv(c_tap_source_env) != 0)
1549             sysdie("cannot set C_TAP_SOURCE in the environment");
1550         source_env = concat("SOURCE=", source, (const char *) 0);
1551         if (putenv(source_env) != 0)
1552             sysdie("cannot set SOURCE in the environment");
1553     }
1554     if (build != NULL) {
1555         c_tap_build_env = concat("C_TAP_BUILD=", build, (const char *) 0);
1556         if (putenv(c_tap_build_env) != 0)
1557             sysdie("cannot set C_TAP_BUILD in the environment");
1558         build_env = concat("BUILD=", build, (const char *) 0);
1559         if (putenv(build_env) != 0)
1560             sysdie("cannot set BUILD in the environment");
1561     }
1562
1563     /* Run the tests as instructed. */
1564     if (single)
1565         test_single(argv[0], source, build);
1566     else if (list != NULL) {
1567         shortlist = strrchr(list, '/');
1568         if (shortlist == NULL)
1569             shortlist = list;
1570         else
1571             shortlist++;
1572         printf(banner, shortlist);
1573         tests = read_test_list(list);
1574         status = test_batch(tests, source, build, verbose) ? 0 : 1;
1575     } else {
1576         tests = build_test_list(argv, argc);
1577         status = test_batch(tests, source, build, verbose) ? 0 : 1;
1578     }
1579
1580     /* For valgrind cleanliness, free all our memory. */
1581     if (source_env != NULL) {
1582         putenv((char *) "C_TAP_SOURCE=");
1583         putenv((char *) "SOURCE=");
1584         free(c_tap_source_env);
1585         free(source_env);
1586     }
1587     if (build_env != NULL) {
1588         putenv((char *) "C_TAP_BUILD=");
1589         putenv((char *) "BUILD=");
1590         free(c_tap_build_env);
1591         free(build_env);
1592     }
1593     exit(status);
1594 }