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