autoconf: Add required headers to net/if.h test
[openafs.git] / tests / runtests.c
1 /*
2  * Run a set of tests, reporting results.
3  *
4  * Usage:
5  *
6  *      runtests <test-list>
7  *
8  * Expects a list of executables located in the given file, one line per
9  * executable.  For each one, runs it as part of a test suite, reporting
10  * results.  Test output should start with a line containing the number of
11  * tests (numbered from 1 to this number), optionally preceded by "1..",
12  * although that line may be given anywhere in the output.  Each additional
13  * line should be in the following format:
14  *
15  *      ok <number>
16  *      not ok <number>
17  *      ok <number> # skip
18  *      not ok <number> # todo
19  *
20  * where <number> is the number of the test.  An optional comment is permitted
21  * after the number if preceded by whitespace.  ok indicates success, not ok
22  * indicates failure.  "# skip" and "# todo" are a special cases of a comment,
23  * and must start with exactly that formatting.  They indicate the test was
24  * skipped for some reason (maybe because it doesn't apply to this platform)
25  * or is testing something known to currently fail.  The text following either
26  * "# skip" or "# todo" and whitespace is the reason.
27  *
28  * As a special case, the first line of the output may be in the form:
29  *
30  *      1..0 # skip some reason
31  *
32  * which indicates that this entire test case should be skipped and gives a
33  * reason.
34  *
35  * Any other lines are ignored, although for compliance with the TAP protocol
36  * all lines other than the ones in the above format should be sent to
37  * standard error rather than standard output and start with #.
38  *
39  * This is a subset of TAP as documented in Test::Harness::TAP or
40  * TAP::Parser::Grammar, which comes with Perl.
41  *
42  * Any bug reports, bug fixes, and improvements are very much welcome and
43  * should be sent to the e-mail address below.
44  *
45  * Copyright 2000, 2001, 2004, 2006, 2007, 2008, 2009, 2010
46  *     Russ Allbery <rra@stanford.edu>
47  *
48  * Permission is hereby granted, free of charge, to any person obtaining a
49  * copy of this software and associated documentation files (the "Software"),
50  * to deal in the Software without restriction, including without limitation
51  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
52  * and/or sell copies of the Software, and to permit persons to whom the
53  * Software is furnished to do so, subject to the following conditions:
54  *
55  * The above copyright notice and this permission notice shall be included in
56  * all copies or substantial portions of the Software.
57  *
58  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
61  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
63  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
64  * DEALINGS IN THE SOFTWARE.
65 */
66
67 #include <ctype.h>
68 #include <errno.h>
69 #include <fcntl.h>
70 #include <stdarg.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <sys/stat.h>
75 #include <sys/time.h>
76 #include <sys/types.h>
77 #include <sys/wait.h>
78 #include <time.h>
79 #include <unistd.h>
80
81 /* sys/time.h must be included before sys/resource.h on some platforms. */
82 #include <sys/resource.h>
83
84 /* AIX doesn't have WCOREDUMP. */
85 #ifndef WCOREDUMP
86 # define WCOREDUMP(status)      ((unsigned)(status) & 0x80)
87 #endif
88
89 /*
90  * The source and build versions of the tests directory.  This is used to set
91  * the SOURCE and BUILD environment variables and find test programs, if set.
92  * Normally, this should be set as part of the build process to the test
93  * subdirectories of $(abs_top_srcdir) and $(abs_top_builddir) respectively.
94  */
95 #ifndef SOURCE
96 # define SOURCE NULL
97 #endif
98 #ifndef BUILD
99 # define BUILD NULL
100 #endif
101
102 /* Test status codes. */
103 enum test_status {
104     TEST_FAIL,
105     TEST_PASS,
106     TEST_SKIP,
107     TEST_INVALID
108 };
109
110 /* Indicates the state of our plan. */
111 enum plan_status {
112     PLAN_INIT,                  /* Nothing seen yet. */
113     PLAN_FIRST,                 /* Plan seen before any tests. */
114     PLAN_PENDING,               /* Test seen and no plan yet. */
115     PLAN_FINAL                  /* Plan seen after some tests. */
116 };
117
118 /* Error exit statuses for test processes. */
119 #define CHILDERR_DUP    100     /* Couldn't redirect stderr or stdout. */
120 #define CHILDERR_EXEC   101     /* Couldn't exec child process. */
121 #define CHILDERR_STDERR 102     /* Couldn't open stderr file. */
122
123 /* Structure to hold data for a set of tests. */
124 struct testset {
125     char *file;                 /* The file name of the test. */
126     char *path;                 /* The path to the test program. */
127     enum plan_status plan;      /* The status of our plan. */
128     unsigned long count;        /* Expected count of tests. */
129     unsigned long current;      /* The last seen test number. */
130     unsigned int length;        /* The length of the last status message. */
131     unsigned long passed;       /* Count of passing tests. */
132     unsigned long failed;       /* Count of failing lists. */
133     unsigned long skipped;      /* Count of skipped tests (passed). */
134     unsigned long allocated;    /* The size of the results table. */
135     enum test_status *results;  /* Table of results by test number. */
136     int aborted;                /* Whether the set as aborted. */
137     int reported;               /* Whether the results were reported. */
138     int status;                 /* The exit status of the test. */
139     int all_skipped;            /* Whether all tests were skipped. */
140     char *reason;               /* Why all tests were skipped. */
141 };
142
143 /* Structure to hold a linked list of test sets. */
144 struct testlist {
145     struct testset *ts;
146     struct testlist *next;
147 };
148
149 /*
150  * Header used for test output.  %s is replaced by the file name of the list
151  * of tests.
152  */
153 static const char banner[] = "\n\
154 Running all tests listed in %s.  If any tests fail, run the failing\n\
155 test program with runtests -o to see more details.\n\n";
156
157 /* Header for reports of failed tests. */
158 static const char header[] = "\n\
159 Failed Set                 Fail/Total (%) Skip Stat  Failing Tests\n\
160 -------------------------- -------------- ---- ----  ------------------------";
161
162 /* Include the file name and line number in malloc failures. */
163 #define xmalloc(size)     x_malloc((size), __FILE__, __LINE__)
164 #define xrealloc(p, size) x_realloc((p), (size), __FILE__, __LINE__)
165 #define xstrdup(p)        x_strdup((p), __FILE__, __LINE__)
166
167
168 /*
169  * Report a fatal error, including the results of strerror, and exit.
170  */
171 static void
172 sysdie(const char *format, ...)
173 {
174     int oerrno;
175     va_list args;
176
177     oerrno = errno;
178     fflush(stdout);
179     fprintf(stderr, "runtests: ");
180     va_start(args, format);
181     vfprintf(stderr, format, args);
182     va_end(args);
183     fprintf(stderr, ": %s\n", strerror(oerrno));
184     exit(1);
185 }
186
187
188 /*
189  * Allocate memory, reporting a fatal error and exiting on failure.
190  */
191 static void *
192 x_malloc(size_t size, const char *file, int line)
193 {
194     void *p;
195
196     p = malloc(size);
197     if (p == NULL)
198         sysdie("failed to malloc %lu bytes at %s line %d",
199                (unsigned long) size, file, line);
200     return p;
201 }
202
203
204 /*
205  * Reallocate memory, reporting a fatal error and exiting on failure.
206  */
207 static void *
208 x_realloc(void *p, size_t size, const char *file, int line)
209 {
210     p = realloc(p, size);
211     if (p == NULL)
212         sysdie("failed to realloc %lu bytes at %s line %d",
213                (unsigned long) size, file, line);
214     return p;
215 }
216
217
218 /*
219  * Copy a string, reporting a fatal error and exiting on failure.
220  */
221 static char *
222 x_strdup(const char *s, const char *file, int line)
223 {
224     char *p;
225     size_t len;
226
227     len = strlen(s) + 1;
228     p = malloc(len);
229     if (p == NULL)
230         sysdie("failed to strdup %lu bytes at %s line %d",
231                (unsigned long) len, file, line);
232     memcpy(p, s, len);
233     return p;
234 }
235
236
237 /*
238  * Given a struct timeval, return the number of seconds it represents as a
239  * double.  Use difftime() to convert a time_t to a double.
240  */
241 static double
242 tv_seconds(const struct timeval *tv)
243 {
244     return difftime(tv->tv_sec, 0) + tv->tv_usec * 1e-6;
245 }
246
247
248 /*
249  * Given two struct timevals, return the difference in seconds.
250  */
251 static double
252 tv_diff(const struct timeval *tv1, const struct timeval *tv0)
253 {
254     return tv_seconds(tv1) - tv_seconds(tv0);
255 }
256
257
258 /*
259  * Given two struct timevals, return the sum in seconds as a double.
260  */
261 static double
262 tv_sum(const struct timeval *tv1, const struct timeval *tv2)
263 {
264     return tv_seconds(tv1) + tv_seconds(tv2);
265 }
266
267
268 /*
269  * Given a pointer to a string, skip any leading whitespace and return a
270  * pointer to the first non-whitespace character.
271  */
272 static const char *
273 skip_whitespace(const char *p)
274 {
275     while (isspace((unsigned char)(*p)))
276         p++;
277     return p;
278 }
279
280
281 /*
282  * Start a program, connecting its stdout to a pipe on our end and its stderr
283  * to /dev/null, and storing the file descriptor to read from in the two
284  * argument.  Returns the PID of the new process.  Errors are fatal.
285  */
286 static pid_t
287 test_start(const char *path, int *fd)
288 {
289     int fds[2], errfd;
290     pid_t child;
291
292     if (pipe(fds) == -1) {
293         puts("ABORTED");
294         fflush(stdout);
295         sysdie("can't create pipe");
296     }
297     child = fork();
298     if (child == (pid_t) -1) {
299         puts("ABORTED");
300         fflush(stdout);
301         sysdie("can't fork");
302     } else if (child == 0) {
303         /* In child.  Set up our stdout and stderr. */
304         errfd = open("/dev/null", O_WRONLY);
305         if (errfd < 0)
306             _exit(CHILDERR_STDERR);
307         if (dup2(errfd, 2) == -1)
308             _exit(CHILDERR_DUP);
309         close(fds[0]);
310         if (dup2(fds[1], 1) == -1)
311             _exit(CHILDERR_DUP);
312
313         /* Now, exec our process. */
314         if (execl(path, path, (char *) 0) == -1)
315             _exit(CHILDERR_EXEC);
316     } else {
317         /* In parent.  Close the extra file descriptor. */
318         close(fds[1]);
319     }
320     *fd = fds[0];
321     return child;
322 }
323
324
325 /*
326  * Back up over the output saying what test we were executing.
327  */
328 static void
329 test_backspace(struct testset *ts)
330 {
331     unsigned int i;
332
333     if (!isatty(STDOUT_FILENO))
334         return;
335     for (i = 0; i < ts->length; i++)
336         putchar('\b');
337     for (i = 0; i < ts->length; i++)
338         putchar(' ');
339     for (i = 0; i < ts->length; i++)
340         putchar('\b');
341     ts->length = 0;
342 }
343
344
345 /*
346  * Read the plan line of test output, which should contain the range of test
347  * numbers.  We may initialize the testset structure here if we haven't yet
348  * seen a test.  Return true if initialization succeeded and the test should
349  * continue, false otherwise.
350  */
351 static int
352 test_plan(const char *line, struct testset *ts)
353 {
354     unsigned long i;
355     long n;
356
357     /*
358      * Accept a plan without the leading 1.. for compatibility with older
359      * versions of runtests.  This will only be allowed if we've not yet seen
360      * a test result.
361      */
362     line = skip_whitespace(line);
363     if (strncmp(line, "1..", 3) == 0)
364         line += 3;
365
366     /*
367      * Get the count, check it for validity, and initialize the struct.  If we
368      * have something of the form "1..0 # skip foo", the whole file was
369      * skipped; record that.  If we do skip the whole file, zero out all of
370      * our statistics, since they're no longer relevant.
371      */
372     n = strtol(line, (char **) &line, 10);
373     if (n == 0) {
374         line = skip_whitespace(line);
375         if (*line == '#') {
376             line = skip_whitespace(line + 1);
377             if (strncasecmp(line, "skip", 4) == 0) {
378                 line = skip_whitespace(line + 4);
379                 if (*line != '\0') {
380                     ts->reason = xstrdup(line);
381                     ts->reason[strlen(ts->reason) - 1] = '\0';
382                 }
383                 ts->all_skipped = 1;
384                 ts->aborted = 1;
385                 ts->count = 0;
386                 ts->passed = 0;
387                 ts->skipped = 0;
388                 ts->failed = 0;
389                 return 0;
390             }
391         }
392     }
393     if (n <= 0) {
394         puts("ABORTED (invalid test count)");
395         ts->aborted = 1;
396         ts->reported = 1;
397         return 0;
398     }
399     if (ts->plan == PLAN_INIT && ts->allocated == 0) {
400         ts->count = n;
401         ts->allocated = n;
402         ts->plan = PLAN_FIRST;
403         ts->results = xmalloc(ts->count * sizeof(enum test_status));
404         for (i = 0; i < ts->count; i++)
405             ts->results[i] = TEST_INVALID;
406     } else if (ts->plan == PLAN_PENDING) {
407         if ((unsigned long) n < ts->count) {
408             printf("ABORTED (invalid test number %lu)\n", ts->count);
409             ts->aborted = 1;
410             ts->reported = 1;
411             return 0;
412         }
413         ts->count = n;
414         if ((unsigned long) n > ts->allocated) {
415             ts->results = xrealloc(ts->results, n * sizeof(enum test_status));
416             for (i = ts->allocated; i < ts->count; i++)
417                 ts->results[i] = TEST_INVALID;
418             ts->allocated = n;
419         }
420         ts->plan = PLAN_FINAL;
421     }
422     return 1;
423 }
424
425
426 /*
427  * Given a single line of output from a test, parse it and return the success
428  * status of that test.  Anything printed to stdout not matching the form
429  * /^(not )?ok \d+/ is ignored.  Sets ts->current to the test number that just
430  * reported status.
431  */
432 static void
433 test_checkline(const char *line, struct testset *ts)
434 {
435     enum test_status status = TEST_PASS;
436     const char *bail;
437     char *end;
438     long number;
439     unsigned long i, current;
440
441     /* Before anything, check for a test abort. */
442     bail = strstr(line, "Bail out!");
443     if (bail != NULL) {
444         bail = skip_whitespace(bail + strlen("Bail out!"));
445         if (*bail != '\0') {
446             size_t length;
447
448             length = strlen(bail);
449             if (bail[length - 1] == '\n')
450                 length--;
451             test_backspace(ts);
452             printf("ABORTED (%.*s)\n", (int)length, bail);
453             ts->reported = 1;
454         }
455         ts->aborted = 1;
456         return;
457     }
458
459     /*
460      * If the given line isn't newline-terminated, it was too big for an
461      * fgets(), which means ignore it.
462      */
463     if (line[strlen(line) - 1] != '\n')
464         return;
465
466     /* If the line begins with a hash mark, ignore it. */
467     if (line[0] == '#')
468         return;
469
470     /* If we haven't yet seen a plan, look for one. */
471     if (ts->plan == PLAN_INIT && isdigit((unsigned char)(*line))) {
472         if (!test_plan(line, ts))
473             return;
474     } else if (strncmp(line, "1..", 3) == 0) {
475         if (ts->plan == PLAN_PENDING) {
476             if (!test_plan(line, ts))
477                 return;
478         } else {
479             puts("ABORTED (multiple plans)");
480             ts->aborted = 1;
481             ts->reported = 1;
482             return;
483         }
484     }
485
486     /* Parse the line, ignoring something we can't parse. */
487     if (strncmp(line, "not ", 4) == 0) {
488         status = TEST_FAIL;
489         line += 4;
490     }
491     if (strncmp(line, "ok", 2) != 0)
492         return;
493     line = skip_whitespace(line + 2);
494     errno = 0;
495     number = strtol(line, &end, 10);
496     if (errno != 0 || end == line)
497         number = ts->current + 1;
498     current = number;
499     if (number <= 0 || (current > ts->count && ts->plan == PLAN_FIRST)) {
500         test_backspace(ts);
501         printf("ABORTED (invalid test number %lu)\n", current);
502         ts->aborted = 1;
503         ts->reported = 1;
504         return;
505     }
506
507     /* We have a valid test result.  Tweak the results array if needed. */
508     if (ts->plan == PLAN_INIT || ts->plan == PLAN_PENDING) {
509         ts->plan = PLAN_PENDING;
510         if (current > ts->count)
511             ts->count = current;
512         if (current > ts->allocated) {
513             unsigned long n;
514
515             n = (ts->allocated == 0) ? 32 : ts->allocated * 2;
516             if (n < current)
517                 n = current;
518             ts->results = xrealloc(ts->results, n * sizeof(enum test_status));
519             for (i = ts->allocated; i < n; i++)
520                 ts->results[i] = TEST_INVALID;
521             ts->allocated = n;
522         }
523     }
524
525     /*
526      * Handle directives.  We should probably do something more interesting
527      * with unexpected passes of todo tests.
528      */
529     while (isdigit((unsigned char)(*line)))
530         line++;
531     line = skip_whitespace(line);
532     if (*line == '#') {
533         line = skip_whitespace(line + 1);
534         if (strncasecmp(line, "skip", 4) == 0)
535             status = TEST_SKIP;
536         if (strncasecmp(line, "todo", 4) == 0)
537             status = (status == TEST_FAIL) ? TEST_SKIP : TEST_FAIL;
538     }
539
540     /* Make sure that the test number is in range and not a duplicate. */
541     if (ts->results[current - 1] != TEST_INVALID) {
542         test_backspace(ts);
543         printf("ABORTED (duplicate test number %lu)\n", current);
544         ts->aborted = 1;
545         ts->reported = 1;
546         return;
547     }
548
549     /* Good results.  Increment our various counters. */
550     switch (status) {
551         case TEST_PASS: ts->passed++;   break;
552         case TEST_FAIL: ts->failed++;   break;
553         case TEST_SKIP: ts->skipped++;  break;
554         default:                        break;
555     }
556     ts->current = current;
557     ts->results[current - 1] = status;
558     test_backspace(ts);
559     if (isatty(STDOUT_FILENO)) {
560         ts->length = printf("%lu/%lu", current, ts->count);
561         fflush(stdout);
562     }
563 }
564
565
566 /*
567  * Print out a range of test numbers, returning the number of characters it
568  * took up.  Add a comma and a space before the range if chars indicates that
569  * something has already been printed on the line, and print ... instead if
570  * chars plus the space needed would go over the limit (use a limit of 0 to
571  * disable this.
572  */
573 static unsigned int
574 test_print_range(unsigned long first, unsigned long last, unsigned int chars,
575                  unsigned int limit)
576 {
577     unsigned int needed = 0;
578     unsigned int out = 0;
579     unsigned long n;
580
581     if (chars > 0) {
582         needed += 2;
583         if (!limit || chars <= limit) out += printf(", ");
584     }
585     for (n = first; n > 0; n /= 10)
586         needed++;
587     if (last > first) {
588         for (n = last; n > 0; n /= 10)
589             needed++;
590         needed++;
591     }
592     if (limit && chars + needed > limit) {
593         if (chars <= limit)
594             out += printf("...");
595     } else {
596         if (last > first)
597             out += printf("%lu-", first);
598         out += printf("%lu", last);
599     }
600     return out;
601 }
602
603
604 /*
605  * Summarize a single test set.  The second argument is 0 if the set exited
606  * cleanly, a positive integer representing the exit status if it exited
607  * with a non-zero status, and a negative integer representing the signal
608  * that terminated it if it was killed by a signal.
609  */
610 static void
611 test_summarize(struct testset *ts, int status)
612 {
613     unsigned long i;
614     unsigned long missing = 0;
615     unsigned long failed = 0;
616     unsigned long first = 0;
617     unsigned long last = 0;
618
619     if (ts->aborted) {
620         fputs("ABORTED", stdout);
621         if (ts->count > 0)
622             printf(" (passed %lu/%lu)", ts->passed, ts->count - ts->skipped);
623     } else {
624         for (i = 0; i < ts->count; i++) {
625             if (ts->results[i] == TEST_INVALID) {
626                 if (missing == 0)
627                     fputs("MISSED ", stdout);
628                 if (first && i == last)
629                     last = i + 1;
630                 else {
631                     if (first)
632                         test_print_range(first, last, missing - 1, 0);
633                     missing++;
634                     first = i + 1;
635                     last = i + 1;
636                 }
637             }
638         }
639         if (first)
640             test_print_range(first, last, missing - 1, 0);
641         first = 0;
642         last = 0;
643         for (i = 0; i < ts->count; i++) {
644             if (ts->results[i] == TEST_FAIL) {
645                 if (missing && !failed)
646                     fputs("; ", stdout);
647                 if (failed == 0)
648                     fputs("FAILED ", stdout);
649                 if (first && i == last)
650                     last = i + 1;
651                 else {
652                     if (first)
653                         test_print_range(first, last, failed - 1, 0);
654                     failed++;
655                     first = i + 1;
656                     last = i + 1;
657                 }
658             }
659         }
660         if (first)
661             test_print_range(first, last, failed - 1, 0);
662         if (!missing && !failed) {
663             fputs(!status ? "ok" : "dubious", stdout);
664             if (ts->skipped > 0) {
665                 if (ts->skipped == 1)
666                     printf(" (skipped %lu test)", ts->skipped);
667                 else
668                     printf(" (skipped %lu tests)", ts->skipped);
669             }
670         }
671     }
672     if (status > 0)
673         printf(" (exit status %d)", status);
674     else if (status < 0)
675         printf(" (killed by signal %d%s)", -status,
676                WCOREDUMP(ts->status) ? ", core dumped" : "");
677     putchar('\n');
678 }
679
680
681 /*
682  * Given a test set, analyze the results, classify the exit status, handle a
683  * few special error messages, and then pass it along to test_summarize() for
684  * the regular output.  Returns true if the test set ran successfully and all
685  * tests passed or were skipped, false otherwise.
686  */
687 static int
688 test_analyze(struct testset *ts)
689 {
690     if (ts->reported)
691         return 0;
692     if (ts->all_skipped) {
693         if (ts->reason == NULL)
694             puts("skipped");
695         else
696             printf("skipped (%s)\n", ts->reason);
697         return 1;
698     } else if (WIFEXITED(ts->status) && WEXITSTATUS(ts->status) != 0) {
699         switch (WEXITSTATUS(ts->status)) {
700         case CHILDERR_DUP:
701             if (!ts->reported)
702                 puts("ABORTED (can't dup file descriptors)");
703             break;
704         case CHILDERR_EXEC:
705             if (!ts->reported)
706                 puts("ABORTED (execution failed -- not found?)");
707             break;
708         case CHILDERR_STDERR:
709             if (!ts->reported)
710                 puts("ABORTED (can't open /dev/null)");
711             break;
712         default:
713             test_summarize(ts, WEXITSTATUS(ts->status));
714             break;
715         }
716         return 0;
717     } else if (WIFSIGNALED(ts->status)) {
718         test_summarize(ts, -WTERMSIG(ts->status));
719         return 0;
720     } else if (ts->plan != PLAN_FIRST && ts->plan != PLAN_FINAL) {
721         puts("ABORTED (no valid test plan)");
722         ts->aborted = 1;
723         return 0;
724     } else {
725         test_summarize(ts, 0);
726         return (ts->failed == 0);
727     }
728 }
729
730
731 /*
732  * Runs a single test set, accumulating and then reporting the results.
733  * Returns true if the test set was successfully run and all tests passed,
734  * false otherwise.
735  */
736 static int
737 test_run(struct testset *ts)
738 {
739     pid_t testpid, child;
740     int outfd, status;
741     unsigned long i;
742     FILE *output;
743     char buffer[BUFSIZ];
744
745     /* Run the test program. */
746     testpid = test_start(ts->path, &outfd);
747     output = fdopen(outfd, "r");
748     if (!output) {
749         puts("ABORTED");
750         fflush(stdout);
751         sysdie("fdopen failed");
752     }
753
754     /* Pass each line of output to test_checkline(). */
755     while (!ts->aborted && fgets(buffer, sizeof(buffer), output))
756         test_checkline(buffer, ts);
757     if (ferror(output) || ts->plan == PLAN_INIT)
758         ts->aborted = 1;
759     test_backspace(ts);
760
761     /*
762      * Consume the rest of the test output, close the output descriptor,
763      * retrieve the exit status, and pass that information to test_analyze()
764      * for eventual output.
765      */
766     while (fgets(buffer, sizeof(buffer), output))
767         ;
768     fclose(output);
769     child = waitpid(testpid, &ts->status, 0);
770     if (child == (pid_t) -1) {
771         if (!ts->reported) {
772             puts("ABORTED");
773             fflush(stdout);
774         }
775         sysdie("waitpid for %u failed", (unsigned int) testpid);
776     }
777     if (ts->all_skipped)
778         ts->aborted = 0;
779     status = test_analyze(ts);
780
781     /* Convert missing tests to failed tests. */
782     for (i = 0; i < ts->count; i++) {
783         if (ts->results[i] == TEST_INVALID) {
784             ts->failed++;
785             ts->results[i] = TEST_FAIL;
786             status = 0;
787         }
788     }
789     return status;
790 }
791
792
793 /* Summarize a list of test failures. */
794 static void
795 test_fail_summary(const struct testlist *fails)
796 {
797     struct testset *ts;
798     unsigned int chars;
799     unsigned long i, first, last, total;
800
801     puts(header);
802
803     /* Failed Set                 Fail/Total (%) Skip Stat  Failing (25)
804        -------------------------- -------------- ---- ----  -------------- */
805     for (; fails; fails = fails->next) {
806         ts = fails->ts;
807         total = ts->count - ts->skipped;
808         printf("%-26.26s %4lu/%-4lu %3.0f%% %4lu ", ts->file, ts->failed,
809                total, total ? (ts->failed * 100.0) / total : 0,
810                ts->skipped);
811         if (WIFEXITED(ts->status))
812             printf("%4d  ", WEXITSTATUS(ts->status));
813         else
814             printf("  --  ");
815         if (ts->aborted) {
816             puts("aborted");
817             continue;
818         }
819         chars = 0;
820         first = 0;
821         last = 0;
822         for (i = 0; i < ts->count; i++) {
823             if (ts->results[i] == TEST_FAIL) {
824                 if (first != 0 && i == last)
825                     last = i + 1;
826                 else {
827                     if (first != 0)
828                         chars += test_print_range(first, last, chars, 20);
829                     first = i + 1;
830                     last = i + 1;
831                 }
832             }
833         }
834         if (first != 0)
835             test_print_range(first, last, chars, 20);
836         putchar('\n');
837         free(ts->file);
838         free(ts->path);
839         free(ts->results);
840         if (ts->reason != NULL)
841             free(ts->reason);
842         free(ts);
843     }
844 }
845
846
847 /*
848  * Given the name of a test, a pointer to the testset struct, and the source
849  * and build directories, find the test.  We try first relative to the current
850  * directory, then in the build directory (if not NULL), then in the source
851  * directory.  In each of those directories, we first try a "-t" extension and
852  * then a ".t" extension.  When we find an executable program, we fill in the
853  * path member of the testset struct.  If none of those paths are executable,
854  * just fill in the name of the test with "-t" appended.
855  *
856  * The caller is responsible for freeing the path member of the testset
857  * struct.
858  */
859 static void
860 find_test(const char *name, struct testset *ts, const char *source,
861           const char *build)
862 {
863     char *path;
864     const char *bases[] = { ".", build, source, NULL };
865     unsigned int i;
866
867     for (i = 0; bases[i] != NULL; i++) {
868         path = xmalloc(strlen(bases[i]) + strlen(name) + 4);
869         sprintf(path, "%s/%s-t", bases[i], name);
870         if (access(path, X_OK) != 0)
871             path[strlen(path) - 2] = '.';
872         if (access(path, X_OK) == 0)
873             break;
874         free(path);
875         path = NULL;
876     }
877     if (path == NULL) {
878         path = xmalloc(strlen(name) + 3);
879         sprintf(path, "%s-t", name);
880     }
881     ts->path = path;
882 }
883
884
885 /*
886  * Run a batch of tests from a given file listing each test on a line by
887  * itself.  Takes two additional parameters: the root of the source directory
888  * and the root of the build directory.  Test programs will be first searched
889  * for in the current directory, then the build directory, then the source
890  * directory.  The file must be rewindable.  Returns true iff all tests
891  * passed.
892  */
893 static int
894 test_batch(const char *testlist, const char *source, const char *build)
895 {
896     FILE *tests;
897     unsigned int length, i;
898     unsigned int longest = 0;
899     char buffer[BUFSIZ];
900     unsigned int line;
901     struct testset ts, *tmp;
902     struct timeval start, end;
903     struct rusage stats;
904     struct testlist *failhead = NULL;
905     struct testlist *failtail = NULL;
906     struct testlist *next;
907     unsigned long total = 0;
908     unsigned long passed = 0;
909     unsigned long skipped = 0;
910     unsigned long failed = 0;
911     unsigned long aborted = 0;
912
913     /*
914      * Open our file of tests to run and scan it, checking for lines that
915      * are too long and searching for the longest line.
916      */
917     tests = fopen(testlist, "r");
918     if (!tests)
919         sysdie("can't open %s", testlist);
920     line = 0;
921     while (fgets(buffer, sizeof(buffer), tests)) {
922         line++;
923         length = strlen(buffer) - 1;
924         if (buffer[length] != '\n') {
925             fprintf(stderr, "%s:%u: line too long\n", testlist, line);
926             exit(1);
927         }
928         if (length > longest)
929             longest = length;
930     }
931     if (fseek(tests, 0, SEEK_SET) == -1)
932         sysdie("can't rewind %s", testlist);
933
934     /*
935      * Add two to longest and round up to the nearest tab stop.  This is how
936      * wide the column for printing the current test name will be.
937      */
938     longest += 2;
939     if (longest % 8)
940         longest += 8 - (longest % 8);
941
942     /* Start the wall clock timer. */
943     gettimeofday(&start, NULL);
944
945     /*
946      * Now, plow through our tests again, running each one.  Check line
947      * length again out of paranoia.
948      */
949     line = 0;
950     while (fgets(buffer, sizeof(buffer), tests)) {
951         line++;
952         length = strlen(buffer) - 1;
953         if (buffer[length] != '\n') {
954             fprintf(stderr, "%s:%u: line too long\n", testlist, line);
955             exit(1);
956         }
957         buffer[length] = '\0';
958         fputs(buffer, stdout);
959         for (i = length; i < longest; i++)
960             putchar('.');
961         if (isatty(STDOUT_FILENO))
962             fflush(stdout);
963         memset(&ts, 0, sizeof(ts));
964         ts.plan = PLAN_INIT;
965         ts.file = xstrdup(buffer);
966         find_test(buffer, &ts, source, build);
967         ts.reason = NULL;
968         if (test_run(&ts)) {
969             free(ts.file);
970             free(ts.path);
971             free(ts.results);
972             if (ts.reason != NULL)
973                 free(ts.reason);
974         } else {
975             tmp = xmalloc(sizeof(struct testset));
976             memcpy(tmp, &ts, sizeof(struct testset));
977             if (!failhead) {
978                 failhead = xmalloc(sizeof(struct testset));
979                 failhead->ts = tmp;
980                 failhead->next = NULL;
981                 failtail = failhead;
982             } else {
983                 failtail->next = xmalloc(sizeof(struct testset));
984                 failtail = failtail->next;
985                 failtail->ts = tmp;
986                 failtail->next = NULL;
987             }
988         }
989         aborted += ts.aborted;
990         total += ts.count + ts.all_skipped;
991         passed += ts.passed;
992         skipped += ts.skipped + ts.all_skipped;
993         failed += ts.failed;
994     }
995     total -= skipped;
996
997     /* Stop the timer and get our child resource statistics. */
998     gettimeofday(&end, NULL);
999     getrusage(RUSAGE_CHILDREN, &stats);
1000
1001     /* Print out our final results. */
1002     if (failhead != NULL) {
1003         test_fail_summary(failhead);
1004         while (failhead != NULL) {
1005             next = failhead->next;
1006             free(failhead);
1007             failhead = next;
1008         }
1009     }
1010     putchar('\n');
1011     if (aborted != 0) {
1012         if (aborted == 1)
1013             printf("Aborted %lu test set", aborted);
1014         else
1015             printf("Aborted %lu test sets", aborted);
1016         printf(", passed %lu/%lu tests", passed, total);
1017     }
1018     else if (failed == 0)
1019         fputs("All tests successful", stdout);
1020     else
1021         printf("Failed %lu/%lu tests, %.2f%% okay", failed, total,
1022                (total - failed) * 100.0 / total);
1023     if (skipped != 0) {
1024         if (skipped == 1)
1025             printf(", %lu test skipped", skipped);
1026         else
1027             printf(", %lu tests skipped", skipped);
1028     }
1029     puts(".");
1030     printf("Files=%u,  Tests=%lu", line, total);
1031     printf(",  %.2f seconds", tv_diff(&end, &start));
1032     printf(" (%.2f usr + %.2f sys = %.2f CPU)\n",
1033            tv_seconds(&stats.ru_utime), tv_seconds(&stats.ru_stime),
1034            tv_sum(&stats.ru_utime, &stats.ru_stime));
1035     return (failed == 0 && aborted == 0);
1036 }
1037
1038
1039 /*
1040  * Run a single test case.  This involves just running the test program after
1041  * having done the environment setup and finding the test program.
1042  */
1043 static void
1044 test_single(const char *program, const char *source, const char *build)
1045 {
1046     struct testset ts;
1047
1048     memset(&ts, 0, sizeof(ts));
1049     find_test(program, &ts, source, build);
1050     if (execl(ts.path, ts.path, (char *) 0) == -1)
1051         sysdie("cannot exec %s", ts.path);
1052 }
1053
1054
1055 /*
1056  * Main routine.  Set the SOURCE and BUILD environment variables and then,
1057  * given a file listing tests, run each test listed.
1058  */
1059 int
1060 main(int argc, char *argv[])
1061 {
1062     int option;
1063     int single = 0;
1064     char *setting;
1065     const char *list;
1066     const char *source = SOURCE;
1067     const char *build = BUILD;
1068
1069     while ((option = getopt(argc, argv, "b:os:")) != EOF) {
1070         switch (option) {
1071         case 'b':
1072             build = optarg;
1073             break;
1074         case 'o':
1075             single = 1;
1076             break;
1077         case 's':
1078             source = optarg;
1079             break;
1080         default:
1081             exit(1);
1082         }
1083     }
1084     argc -= optind;
1085     argv += optind;
1086     if (argc != 1) {
1087         fprintf(stderr, "Usage: runtests <test-list>\n");
1088         exit(1);
1089     }
1090
1091     if (source != NULL) {
1092         setting = xmalloc(strlen("SOURCE=") + strlen(source) + 1);
1093         sprintf(setting, "SOURCE=%s", source);
1094         if (putenv(setting) != 0)
1095             sysdie("cannot set SOURCE in the environment");
1096     }
1097     if (build != NULL) {
1098         setting = xmalloc(strlen("BUILD=") + strlen(build) + 1);
1099         sprintf(setting, "BUILD=%s", build);
1100         if (putenv(setting) != 0)
1101             sysdie("cannot set BUILD in the environment");
1102     }
1103
1104     if (single) {
1105         test_single(argv[0], source, build);
1106         exit(0);
1107     } else {
1108         list = strrchr(argv[0], '/');
1109         if (list == NULL)
1110             list = argv[0];
1111         else
1112             list++;
1113         printf(banner, list);
1114         exit(test_batch(argv[0], source, build) ? 0 : 1);
1115     }
1116 }