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