switch to c-tap-harness 2.1
[hkl.git] / tests / runtests.c
blob5518c39b8204bb8b19899ca4bc19e824cc8fff89
1 /*
2 * Run a set of tests, reporting results.
4 * Usage:
6 * runtests [-b <build-dir>] [-s <source-dir>] <test-list>
7 * runtests -o [-b <build-dir>] [-s <source-dir>] <test>
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:
16 * ok <number>
17 * not ok <number>
18 * ok <number> # skip
19 * not ok <number> # todo
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.
29 * As a special case, the first line of the output may be in the form:
31 * 1..0 # skip some reason
33 * which indicates that this entire test case should be skipped and gives a
34 * reason.
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 #.
40 * This is a subset of TAP as documented in Test::Harness::TAP or
41 * TAP::Parser::Grammar, which comes with Perl.
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.
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.
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/>.
57 * Copyright 2000, 2001, 2004, 2006, 2007, 2008, 2009, 2010, 2011
58 * Russ Allbery <rra@stanford.edu>
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:
67 * The above copyright notice and this permission notice shall be included in
68 * all copies or substantial portions of the Software.
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.
79 /* Required for fdopen(), getopt(), and putenv(). */
80 #if defined(__STRICT_ANSI__) || defined(PEDANTIC)
81 # ifndef _XOPEN_SOURCE
82 # define _XOPEN_SOURCE 500
83 # endif
84 #endif
86 #include <ctype.h>
87 #include <errno.h>
88 #include <fcntl.h>
89 #include <stdarg.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <strings.h>
94 #include <sys/stat.h>
95 #include <sys/time.h>
96 #include <sys/types.h>
97 #include <sys/wait.h>
98 #include <time.h>
99 #include <unistd.h>
101 /* sys/time.h must be included before sys/resource.h on some platforms. */
102 #include <sys/resource.h>
104 /* AIX doesn't have WCOREDUMP. */
105 #ifndef WCOREDUMP
106 # define WCOREDUMP(status) ((unsigned)(status) & 0x80)
107 #endif
110 * Used for iterating through arrays. Returns the number of elements in the
111 * array (useful for a < upper bound in a for loop).
113 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
116 * The source and build versions of the tests directory. This is used to set
117 * the SOURCE and BUILD environment variables and find test programs, if set.
118 * Normally, this should be set as part of the build process to the test
119 * subdirectories of $(abs_top_srcdir) and $(abs_top_builddir) respectively.
121 #ifndef SOURCE
122 # define SOURCE NULL
123 #endif
124 #ifndef BUILD
125 # define BUILD NULL
126 #endif
128 /* Test status codes. */
129 enum test_status {
130 TEST_FAIL,
131 TEST_PASS,
132 TEST_SKIP,
133 TEST_INVALID
136 /* Indicates the state of our plan. */
137 enum plan_status {
138 PLAN_INIT, /* Nothing seen yet. */
139 PLAN_FIRST, /* Plan seen before any tests. */
140 PLAN_PENDING, /* Test seen and no plan yet. */
141 PLAN_FINAL /* Plan seen after some tests. */
144 /* Error exit statuses for test processes. */
145 #define CHILDERR_DUP 100 /* Couldn't redirect stderr or stdout. */
146 #define CHILDERR_EXEC 101 /* Couldn't exec child process. */
147 #define CHILDERR_STDERR 102 /* Couldn't open stderr file. */
149 /* Structure to hold data for a set of tests. */
150 struct testset {
151 char *file; /* The file name of the test. */
152 char *path; /* The path to the test program. */
153 enum plan_status plan; /* The status of our plan. */
154 unsigned long count; /* Expected count of tests. */
155 unsigned long current; /* The last seen test number. */
156 unsigned int length; /* The length of the last status message. */
157 unsigned long passed; /* Count of passing tests. */
158 unsigned long failed; /* Count of failing lists. */
159 unsigned long skipped; /* Count of skipped tests (passed). */
160 unsigned long allocated; /* The size of the results table. */
161 enum test_status *results; /* Table of results by test number. */
162 unsigned int aborted; /* Whether the set was aborted. */
163 int reported; /* Whether the results were reported. */
164 int status; /* The exit status of the test. */
165 unsigned int all_skipped; /* Whether all tests were skipped. */
166 char *reason; /* Why all tests were skipped. */
169 /* Structure to hold a linked list of test sets. */
170 struct testlist {
171 struct testset *ts;
172 struct testlist *next;
176 * Usage message. Should be used as a printf format with two arguments: the
177 * path to runtests, given twice.
179 static const char usage_message[] = "\
180 Usage: %s [-b <build-dir>] [-s <source-dir>] <test> ...\n\
181 %s [-b <build-dir>] [-s <source-dir>] -l <test-list>\n\
182 %s -o [-b <build-dir>] [-s <source-dir>] <test>\n\
184 Options:\n\
185 -b <build-dir> Set the build directory to <build-dir>\n\
186 -l <list> Take the list of tests to run from <test-list>\n\
187 -o Run a single test rather than a list of tests\n\
188 -s <source-dir> Set the source directory to <source-dir>\n\
190 runtests normally runs each test listed on the command line. With the -l\n\
191 option, it instead runs every test listed in a file. With the -o option,\n\
192 it instead runs a single test and shows its complete output.\n";
195 * Header used for test output. %s is replaced by the file name of the list
196 * of tests.
198 static const char banner[] = "\n\
199 Running all tests listed in %s. If any tests fail, run the failing\n\
200 test program with runtests -o to see more details.\n\n";
202 /* Header for reports of failed tests. */
203 static const char header[] = "\n\
204 Failed Set Fail/Total (%) Skip Stat Failing Tests\n\
205 -------------------------- -------------- ---- ---- ------------------------";
207 /* Include the file name and line number in malloc failures. */
208 #define xcalloc(n, size) x_calloc((n), (size), __FILE__, __LINE__)
209 #define xmalloc(size) x_malloc((size), __FILE__, __LINE__)
210 #define xrealloc(p, size) x_realloc((p), (size), __FILE__, __LINE__)
211 #define xstrdup(p) x_strdup((p), __FILE__, __LINE__)
214 * __attribute__ is available in gcc 2.5 and later, but only with gcc 2.7
215 * could you use the __format__ form of the attributes, which is what we use
216 * (to avoid confusion with other macros).
218 #ifndef __attribute__
219 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
220 # define __attribute__(spec) /* empty */
221 # endif
222 #endif
225 * We use __alloc_size__, but it was only available in fairly recent versions
226 * of GCC. Suppress warnings about the unknown attribute if GCC is too old.
227 * We know that we're GCC at this point, so we can use the GCC variadic macro
228 * extension, which will still work with versions of GCC too old to have C99
229 * variadic macro support.
231 #if !defined(__attribute__) && !defined(__alloc_size__)
232 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
233 # define __alloc_size__(spec, args...) /* empty */
234 # endif
235 #endif
238 * LLVM and Clang pretend to be GCC but don't support all of the __attribute__
239 * settings that GCC does. For them, suppress warnings about unknown
240 * attributes on declarations. This unfortunately will affect the entire
241 * compilation context, but there's no push and pop available.
243 #if !defined(__attribute__) && (defined(__llvm__) || defined(__clang__))
244 # pragma GCC diagnostic ignored "-Wattributes"
245 #endif
247 /* Declare internal functions that benefit from compiler attributes. */
248 static void sysdie(const char *, ...)
249 __attribute__((__nonnull__, __noreturn__, __format__(printf, 1, 2)));
250 static void *x_calloc(size_t, size_t, const char *, int)
251 __attribute__((__alloc_size__(1, 2), __malloc__, __nonnull__));
252 static void *x_malloc(size_t, const char *, int)
253 __attribute__((__alloc_size__(1), __malloc__, __nonnull__));
254 static void *x_realloc(void *, size_t, const char *, int)
255 __attribute__((__alloc_size__(2), __malloc__, __nonnull__(3)));
256 static char *x_strdup(const char *, const char *, int)
257 __attribute__((__malloc__, __nonnull__));
261 * Report a fatal error, including the results of strerror, and exit.
263 static void
264 sysdie(const char *format, ...)
266 int oerrno;
267 va_list args;
269 oerrno = errno;
270 fflush(stdout);
271 fprintf(stderr, "runtests: ");
272 va_start(args, format);
273 vfprintf(stderr, format, args);
274 va_end(args);
275 fprintf(stderr, ": %s\n", strerror(oerrno));
276 exit(1);
281 * Allocate zeroed memory, reporting a fatal error and exiting on failure.
283 static void *
284 x_calloc(size_t n, size_t size, const char *file, int line)
286 void *p;
288 n = (n > 0) ? n : 1;
289 size = (size > 0) ? size : 1;
290 p = calloc(n, size);
291 if (p == NULL)
292 sysdie("failed to calloc %lu bytes at %s line %d",
293 (unsigned long) size, file, line);
294 return p;
299 * Allocate memory, reporting a fatal error and exiting on failure.
301 static void *
302 x_malloc(size_t size, const char *file, int line)
304 void *p;
306 p = malloc(size);
307 if (p == NULL)
308 sysdie("failed to malloc %lu bytes at %s line %d",
309 (unsigned long) size, file, line);
310 return p;
315 * Reallocate memory, reporting a fatal error and exiting on failure.
317 static void *
318 x_realloc(void *p, size_t size, const char *file, int line)
320 p = realloc(p, size);
321 if (p == NULL)
322 sysdie("failed to realloc %lu bytes at %s line %d",
323 (unsigned long) size, file, line);
324 return p;
329 * Copy a string, reporting a fatal error and exiting on failure.
331 static char *
332 x_strdup(const char *s, const char *file, int line)
334 char *p;
335 size_t len;
337 len = strlen(s) + 1;
338 p = malloc(len);
339 if (p == NULL)
340 sysdie("failed to strdup %lu bytes at %s line %d",
341 (unsigned long) len, file, line);
342 memcpy(p, s, len);
343 return p;
348 * Given a struct timeval, return the number of seconds it represents as a
349 * double. Use difftime() to convert a time_t to a double.
351 static double
352 tv_seconds(const struct timeval *tv)
354 return difftime(tv->tv_sec, 0) + tv->tv_usec * 1e-6;
359 * Given two struct timevals, return the difference in seconds.
361 static double
362 tv_diff(const struct timeval *tv1, const struct timeval *tv0)
364 return tv_seconds(tv1) - tv_seconds(tv0);
369 * Given two struct timevals, return the sum in seconds as a double.
371 static double
372 tv_sum(const struct timeval *tv1, const struct timeval *tv2)
374 return tv_seconds(tv1) + tv_seconds(tv2);
379 * Given a pointer to a string, skip any leading whitespace and return a
380 * pointer to the first non-whitespace character.
382 static const char *
383 skip_whitespace(const char *p)
385 while (isspace((unsigned char)(*p)))
386 p++;
387 return p;
392 * Start a program, connecting its stdout to a pipe on our end and its stderr
393 * to /dev/null, and storing the file descriptor to read from in the two
394 * argument. Returns the PID of the new process. Errors are fatal.
396 static pid_t
397 test_start(const char *path, int *fd)
399 int fds[2], errfd;
400 pid_t child;
402 if (pipe(fds) == -1) {
403 puts("ABORTED");
404 fflush(stdout);
405 sysdie("can't create pipe");
407 child = fork();
408 if (child == (pid_t) -1) {
409 puts("ABORTED");
410 fflush(stdout);
411 sysdie("can't fork");
412 } else if (child == 0) {
413 /* In child. Set up our stdout and stderr. */
414 errfd = open("/dev/null", O_WRONLY);
415 if (errfd < 0)
416 _exit(CHILDERR_STDERR);
417 if (dup2(errfd, 2) == -1)
418 _exit(CHILDERR_DUP);
419 close(fds[0]);
420 if (dup2(fds[1], 1) == -1)
421 _exit(CHILDERR_DUP);
423 /* Now, exec our process. */
424 if (execl(path, path, (char *) 0) == -1)
425 _exit(CHILDERR_EXEC);
426 } else {
427 /* In parent. Close the extra file descriptor. */
428 close(fds[1]);
430 *fd = fds[0];
431 return child;
436 * Back up over the output saying what test we were executing.
438 static void
439 test_backspace(struct testset *ts)
441 unsigned int i;
443 if (!isatty(STDOUT_FILENO))
444 return;
445 for (i = 0; i < ts->length; i++)
446 putchar('\b');
447 for (i = 0; i < ts->length; i++)
448 putchar(' ');
449 for (i = 0; i < ts->length; i++)
450 putchar('\b');
451 ts->length = 0;
456 * Read the plan line of test output, which should contain the range of test
457 * numbers. We may initialize the testset structure here if we haven't yet
458 * seen a test. Return true if initialization succeeded and the test should
459 * continue, false otherwise.
461 static int
462 test_plan(const char *line, struct testset *ts)
464 unsigned long i;
465 long n;
468 * Accept a plan without the leading 1.. for compatibility with older
469 * versions of runtests. This will only be allowed if we've not yet seen
470 * a test result.
472 line = skip_whitespace(line);
473 if (strncmp(line, "1..", 3) == 0)
474 line += 3;
477 * Get the count, check it for validity, and initialize the struct. If we
478 * have something of the form "1..0 # skip foo", the whole file was
479 * skipped; record that. If we do skip the whole file, zero out all of
480 * our statistics, since they're no longer relevant. strtol is called
481 * with a second argument to advance the line pointer past the count to
482 * make it simpler to detect the # skip case.
484 n = strtol(line, (char **) &line, 10);
485 if (n == 0) {
486 line = skip_whitespace(line);
487 if (*line == '#') {
488 line = skip_whitespace(line + 1);
489 if (strncasecmp(line, "skip", 4) == 0) {
490 line = skip_whitespace(line + 4);
491 if (*line != '\0') {
492 ts->reason = xstrdup(line);
493 ts->reason[strlen(ts->reason) - 1] = '\0';
495 ts->all_skipped = 1;
496 ts->aborted = 1;
497 ts->count = 0;
498 ts->passed = 0;
499 ts->skipped = 0;
500 ts->failed = 0;
501 return 0;
505 if (n <= 0) {
506 puts("ABORTED (invalid test count)");
507 ts->aborted = 1;
508 ts->reported = 1;
509 return 0;
511 if (ts->plan == PLAN_INIT && ts->allocated == 0) {
512 ts->count = n;
513 ts->allocated = n;
514 ts->plan = PLAN_FIRST;
515 ts->results = xmalloc(ts->count * sizeof(enum test_status));
516 for (i = 0; i < ts->count; i++)
517 ts->results[i] = TEST_INVALID;
518 } else if (ts->plan == PLAN_PENDING) {
519 if ((unsigned long) n < ts->count) {
520 test_backspace(ts);
521 printf("ABORTED (invalid test number %lu)\n", ts->count);
522 ts->aborted = 1;
523 ts->reported = 1;
524 return 0;
526 ts->count = n;
527 if ((unsigned long) n > ts->allocated) {
528 ts->results = xrealloc(ts->results, n * sizeof(enum test_status));
529 for (i = ts->allocated; i < ts->count; i++)
530 ts->results[i] = TEST_INVALID;
531 ts->allocated = n;
533 ts->plan = PLAN_FINAL;
535 return 1;
540 * Given a single line of output from a test, parse it and return the success
541 * status of that test. Anything printed to stdout not matching the form
542 * /^(not )?ok \d+/ is ignored. Sets ts->current to the test number that just
543 * reported status.
545 static void
546 test_checkline(const char *line, struct testset *ts)
548 enum test_status status = TEST_PASS;
549 const char *bail;
550 char *end;
551 long number;
552 unsigned long i, current;
553 int outlen;
555 /* Before anything, check for a test abort. */
556 bail = strstr(line, "Bail out!");
557 if (bail != NULL) {
558 bail = skip_whitespace(bail + strlen("Bail out!"));
559 if (*bail != '\0') {
560 size_t length;
562 length = strlen(bail);
563 if (bail[length - 1] == '\n')
564 length--;
565 test_backspace(ts);
566 printf("ABORTED (%.*s)\n", (int) length, bail);
567 ts->reported = 1;
569 ts->aborted = 1;
570 return;
574 * If the given line isn't newline-terminated, it was too big for an
575 * fgets(), which means ignore it.
577 if (line[strlen(line) - 1] != '\n')
578 return;
580 /* If the line begins with a hash mark, ignore it. */
581 if (line[0] == '#')
582 return;
584 /* If we haven't yet seen a plan, look for one. */
585 if (ts->plan == PLAN_INIT && isdigit((unsigned char)(*line))) {
586 if (!test_plan(line, ts))
587 return;
588 } else if (strncmp(line, "1..", 3) == 0) {
589 if (ts->plan == PLAN_PENDING) {
590 if (!test_plan(line, ts))
591 return;
592 } else {
593 test_backspace(ts);
594 puts("ABORTED (multiple plans)");
595 ts->aborted = 1;
596 ts->reported = 1;
597 return;
601 /* Parse the line, ignoring something we can't parse. */
602 if (strncmp(line, "not ", 4) == 0) {
603 status = TEST_FAIL;
604 line += 4;
606 if (strncmp(line, "ok", 2) != 0)
607 return;
608 line = skip_whitespace(line + 2);
609 errno = 0;
610 number = strtol(line, &end, 10);
611 if (errno != 0 || end == line)
612 number = ts->current + 1;
613 current = number;
614 if (number <= 0 || (current > ts->count && ts->plan == PLAN_FIRST)) {
615 test_backspace(ts);
616 printf("ABORTED (invalid test number %lu)\n", current);
617 ts->aborted = 1;
618 ts->reported = 1;
619 return;
622 /* We have a valid test result. Tweak the results array if needed. */
623 if (ts->plan == PLAN_INIT || ts->plan == PLAN_PENDING) {
624 ts->plan = PLAN_PENDING;
625 if (current > ts->count)
626 ts->count = current;
627 if (current > ts->allocated) {
628 unsigned long n;
630 n = (ts->allocated == 0) ? 32 : ts->allocated * 2;
631 if (n < current)
632 n = current;
633 ts->results = xrealloc(ts->results, n * sizeof(enum test_status));
634 for (i = ts->allocated; i < n; i++)
635 ts->results[i] = TEST_INVALID;
636 ts->allocated = n;
641 * Handle directives. We should probably do something more interesting
642 * with unexpected passes of todo tests.
644 while (isdigit((unsigned char)(*line)))
645 line++;
646 line = skip_whitespace(line);
647 if (*line == '#') {
648 line = skip_whitespace(line + 1);
649 if (strncasecmp(line, "skip", 4) == 0)
650 status = TEST_SKIP;
651 if (strncasecmp(line, "todo", 4) == 0)
652 status = (status == TEST_FAIL) ? TEST_SKIP : TEST_FAIL;
655 /* Make sure that the test number is in range and not a duplicate. */
656 if (ts->results[current - 1] != TEST_INVALID) {
657 test_backspace(ts);
658 printf("ABORTED (duplicate test number %lu)\n", current);
659 ts->aborted = 1;
660 ts->reported = 1;
661 return;
664 /* Good results. Increment our various counters. */
665 switch (status) {
666 case TEST_PASS: ts->passed++; break;
667 case TEST_FAIL: ts->failed++; break;
668 case TEST_SKIP: ts->skipped++; break;
669 case TEST_INVALID: break;
671 ts->current = current;
672 ts->results[current - 1] = status;
673 if (isatty(STDOUT_FILENO)) {
674 test_backspace(ts);
675 if (ts->plan == PLAN_PENDING)
676 outlen = printf("%lu/?", current);
677 else
678 outlen = printf("%lu/%lu", current, ts->count);
679 ts->length = (outlen >= 0) ? outlen : 0;
680 fflush(stdout);
686 * Print out a range of test numbers, returning the number of characters it
687 * took up. Takes the first number, the last number, the number of characters
688 * already printed on the line, and the limit of number of characters the line
689 * can hold. Add a comma and a space before the range if chars indicates that
690 * something has already been printed on the line, and print ... instead if
691 * chars plus the space needed would go over the limit (use a limit of 0 to
692 * disable this).
694 static unsigned int
695 test_print_range(unsigned long first, unsigned long last, unsigned int chars,
696 unsigned int limit)
698 unsigned int needed = 0;
699 unsigned long n;
701 for (n = first; n > 0; n /= 10)
702 needed++;
703 if (last > first) {
704 for (n = last; n > 0; n /= 10)
705 needed++;
706 needed++;
708 if (chars > 0)
709 needed += 2;
710 if (limit > 0 && chars + needed > limit) {
711 needed = 0;
712 if (chars <= limit) {
713 if (chars > 0) {
714 printf(", ");
715 needed += 2;
717 printf("...");
718 needed += 3;
720 } else {
721 if (chars > 0)
722 printf(", ");
723 if (last > first)
724 printf("%lu-", first);
725 printf("%lu", last);
727 return needed;
732 * Summarize a single test set. The second argument is 0 if the set exited
733 * cleanly, a positive integer representing the exit status if it exited
734 * with a non-zero status, and a negative integer representing the signal
735 * that terminated it if it was killed by a signal.
737 static void
738 test_summarize(struct testset *ts, int status)
740 unsigned long i;
741 unsigned long missing = 0;
742 unsigned long failed = 0;
743 unsigned long first = 0;
744 unsigned long last = 0;
746 if (ts->aborted) {
747 fputs("ABORTED", stdout);
748 if (ts->count > 0)
749 printf(" (passed %lu/%lu)", ts->passed, ts->count - ts->skipped);
750 } else {
751 for (i = 0; i < ts->count; i++) {
752 if (ts->results[i] == TEST_INVALID) {
753 if (missing == 0)
754 fputs("MISSED ", stdout);
755 if (first && i == last)
756 last = i + 1;
757 else {
758 if (first)
759 test_print_range(first, last, missing - 1, 0);
760 missing++;
761 first = i + 1;
762 last = i + 1;
766 if (first)
767 test_print_range(first, last, missing - 1, 0);
768 first = 0;
769 last = 0;
770 for (i = 0; i < ts->count; i++) {
771 if (ts->results[i] == TEST_FAIL) {
772 if (missing && !failed)
773 fputs("; ", stdout);
774 if (failed == 0)
775 fputs("FAILED ", stdout);
776 if (first && i == last)
777 last = i + 1;
778 else {
779 if (first)
780 test_print_range(first, last, failed - 1, 0);
781 failed++;
782 first = i + 1;
783 last = i + 1;
787 if (first)
788 test_print_range(first, last, failed - 1, 0);
789 if (!missing && !failed) {
790 fputs(!status ? "ok" : "dubious", stdout);
791 if (ts->skipped > 0) {
792 if (ts->skipped == 1)
793 printf(" (skipped %lu test)", ts->skipped);
794 else
795 printf(" (skipped %lu tests)", ts->skipped);
799 if (status > 0)
800 printf(" (exit status %d)", status);
801 else if (status < 0)
802 printf(" (killed by signal %d%s)", -status,
803 WCOREDUMP(ts->status) ? ", core dumped" : "");
804 putchar('\n');
809 * Given a test set, analyze the results, classify the exit status, handle a
810 * few special error messages, and then pass it along to test_summarize() for
811 * the regular output. Returns true if the test set ran successfully and all
812 * tests passed or were skipped, false otherwise.
814 static int
815 test_analyze(struct testset *ts)
817 if (ts->reported)
818 return 0;
819 if (ts->all_skipped) {
820 if (ts->reason == NULL)
821 puts("skipped");
822 else
823 printf("skipped (%s)\n", ts->reason);
824 return 1;
825 } else if (WIFEXITED(ts->status) && WEXITSTATUS(ts->status) != 0) {
826 switch (WEXITSTATUS(ts->status)) {
827 case CHILDERR_DUP:
828 if (!ts->reported)
829 puts("ABORTED (can't dup file descriptors)");
830 break;
831 case CHILDERR_EXEC:
832 if (!ts->reported)
833 puts("ABORTED (execution failed -- not found?)");
834 break;
835 case CHILDERR_STDERR:
836 if (!ts->reported)
837 puts("ABORTED (can't open /dev/null)");
838 break;
839 default:
840 test_summarize(ts, WEXITSTATUS(ts->status));
841 break;
843 return 0;
844 } else if (WIFSIGNALED(ts->status)) {
845 test_summarize(ts, -WTERMSIG(ts->status));
846 return 0;
847 } else if (ts->plan != PLAN_FIRST && ts->plan != PLAN_FINAL) {
848 puts("ABORTED (no valid test plan)");
849 ts->aborted = 1;
850 return 0;
851 } else {
852 test_summarize(ts, 0);
853 return (ts->failed == 0);
859 * Runs a single test set, accumulating and then reporting the results.
860 * Returns true if the test set was successfully run and all tests passed,
861 * false otherwise.
863 static int
864 test_run(struct testset *ts)
866 pid_t testpid, child;
867 int outfd, status;
868 unsigned long i;
869 FILE *output;
870 char buffer[BUFSIZ];
872 /* Run the test program. */
873 testpid = test_start(ts->path, &outfd);
874 output = fdopen(outfd, "r");
875 if (!output) {
876 puts("ABORTED");
877 fflush(stdout);
878 sysdie("fdopen failed");
881 /* Pass each line of output to test_checkline(). */
882 while (!ts->aborted && fgets(buffer, sizeof(buffer), output))
883 test_checkline(buffer, ts);
884 if (ferror(output) || ts->plan == PLAN_INIT)
885 ts->aborted = 1;
886 test_backspace(ts);
889 * Consume the rest of the test output, close the output descriptor,
890 * retrieve the exit status, and pass that information to test_analyze()
891 * for eventual output.
893 while (fgets(buffer, sizeof(buffer), output))
895 fclose(output);
896 child = waitpid(testpid, &ts->status, 0);
897 if (child == (pid_t) -1) {
898 if (!ts->reported) {
899 puts("ABORTED");
900 fflush(stdout);
902 sysdie("waitpid for %u failed", (unsigned int) testpid);
904 if (ts->all_skipped)
905 ts->aborted = 0;
906 status = test_analyze(ts);
908 /* Convert missing tests to failed tests. */
909 for (i = 0; i < ts->count; i++) {
910 if (ts->results[i] == TEST_INVALID) {
911 ts->failed++;
912 ts->results[i] = TEST_FAIL;
913 status = 0;
916 return status;
920 /* Summarize a list of test failures. */
921 static void
922 test_fail_summary(const struct testlist *fails)
924 struct testset *ts;
925 unsigned int chars;
926 unsigned long i, first, last, total;
928 puts(header);
930 /* Failed Set Fail/Total (%) Skip Stat Failing (25)
931 -------------------------- -------------- ---- ---- -------------- */
932 for (; fails; fails = fails->next) {
933 ts = fails->ts;
934 total = ts->count - ts->skipped;
935 printf("%-26.26s %4lu/%-4lu %3.0f%% %4lu ", ts->file, ts->failed,
936 total, total ? (ts->failed * 100.0) / total : 0,
937 ts->skipped);
938 if (WIFEXITED(ts->status))
939 printf("%4d ", WEXITSTATUS(ts->status));
940 else
941 printf(" -- ");
942 if (ts->aborted) {
943 puts("aborted");
944 continue;
946 chars = 0;
947 first = 0;
948 last = 0;
949 for (i = 0; i < ts->count; i++) {
950 if (ts->results[i] == TEST_FAIL) {
951 if (first != 0 && i == last)
952 last = i + 1;
953 else {
954 if (first != 0)
955 chars += test_print_range(first, last, chars, 19);
956 first = i + 1;
957 last = i + 1;
961 if (first != 0)
962 test_print_range(first, last, chars, 19);
963 putchar('\n');
969 * Check whether a given file path is a valid test. Currently, this checks
970 * whether it is executable and is a regular file. Returns true or false.
972 static int
973 is_valid_test(const char *path)
975 struct stat st;
977 if (access(path, X_OK) < 0)
978 return 0;
979 if (stat(path, &st) < 0)
980 return 0;
981 if (!S_ISREG(st.st_mode))
982 return 0;
983 return 1;
988 * Given the name of a test, a pointer to the testset struct, and the source
989 * and build directories, find the test. We try first relative to the current
990 * directory, then in the build directory (if not NULL), then in the source
991 * directory. In each of those directories, we first try a "-t" extension and
992 * then a ".t" extension. When we find an executable program, we return the
993 * path to that program. If none of those paths are executable, just fill in
994 * the name of the test as is.
996 * The caller is responsible for freeing the path member of the testset
997 * struct.
999 static char *
1000 find_test(const char *name, const char *source, const char *build)
1002 char *path;
1003 const char *bases[3], *suffix, *base;
1004 unsigned int i, j;
1005 const char *suffixes[3] = { "-t", ".t", "" };
1007 /* Possible base directories. */
1008 bases[0] = ".";
1009 bases[1] = build;
1010 bases[2] = source;
1012 /* Try each suffix with each base. */
1013 for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
1014 suffix = suffixes[i];
1015 for (j = 0; j < ARRAY_SIZE(bases); j++) {
1016 base = bases[j];
1017 if (base == NULL)
1018 continue;
1019 path = xmalloc(strlen(base) + strlen(name) + strlen(suffix) + 2);
1020 sprintf(path, "%s/%s%s", base, name, suffix);
1021 if (is_valid_test(path))
1022 return path;
1023 free(path);
1024 path = NULL;
1027 if (path == NULL)
1028 path = xstrdup(name);
1029 return path;
1034 * Read a list of tests from a file, returning the list of tests as a struct
1035 * testlist. Reports an error to standard error and exits if the list of
1036 * tests cannot be read.
1038 static struct testlist *
1039 read_test_list(const char *filename)
1041 FILE *file;
1042 unsigned int line;
1043 size_t length;
1044 char buffer[BUFSIZ];
1045 struct testlist *listhead, *current;
1047 /* Create the initial container list that will hold our results. */
1048 listhead = xmalloc(sizeof(struct testlist));
1049 listhead->ts = NULL;
1050 listhead->next = NULL;
1051 current = NULL;
1054 * Open our file of tests to run and read it line by line, creating a new
1055 * struct testlist and struct testset for each line.
1057 file = fopen(filename, "r");
1058 if (file == NULL)
1059 sysdie("can't open %s", filename);
1060 line = 0;
1061 while (fgets(buffer, sizeof(buffer), file)) {
1062 line++;
1063 length = strlen(buffer) - 1;
1064 if (buffer[length] != '\n') {
1065 fprintf(stderr, "%s:%u: line too long\n", filename, line);
1066 exit(1);
1068 buffer[length] = '\0';
1069 if (current == NULL)
1070 current = listhead;
1071 else {
1072 current->next = xmalloc(sizeof(struct testlist));
1073 current = current->next;
1074 current->next = NULL;
1076 current->ts = xcalloc(1, sizeof(struct testset));
1077 current->ts->plan = PLAN_INIT;
1078 current->ts->file = xstrdup(buffer);
1079 current->ts->reason = NULL;
1081 fclose(file);
1083 /* Return the results. */
1084 return listhead;
1089 * Build a list of tests from command line arguments. Takes the argv and argc
1090 * representing the command line arguments and returns a newly allocated test
1091 * list. The caller is responsible for freeing.
1093 static struct testlist *
1094 build_test_list(char *argv[], int argc)
1096 int i;
1097 struct testlist *listhead, *current;
1099 /* Create the initial container list that will hold our results. */
1100 listhead = xmalloc(sizeof(struct testlist));
1101 listhead->ts = NULL;
1102 listhead->next = NULL;
1103 current = NULL;
1105 /* Walk the list of arguments and create test sets for them. */
1106 for (i = 0; i < argc; i++) {
1107 if (current == NULL)
1108 current = listhead;
1109 else {
1110 current->next = xmalloc(sizeof(struct testlist));
1111 current = current->next;
1112 current->next = NULL;
1114 current->ts = xcalloc(1, sizeof(struct testset));
1115 current->ts->plan = PLAN_INIT;
1116 current->ts->file = xstrdup(argv[i]);
1117 current->ts->reason = NULL;
1120 /* Return the results. */
1121 return listhead;
1125 /* Free a struct testset. */
1126 static void
1127 free_testset(struct testset *ts)
1129 free(ts->file);
1130 free(ts->path);
1131 free(ts->results);
1132 if (ts->reason != NULL)
1133 free(ts->reason);
1134 free(ts);
1139 * Run a batch of tests. Takes two additional parameters: the root of the
1140 * source directory and the root of the build directory. Test programs will
1141 * be first searched for in the current directory, then the build directory,
1142 * then the source directory. Returns true iff all tests passed, and always
1143 * frees the test list that's passed in.
1145 static int
1146 test_batch(struct testlist *tests, const char *source, const char *build)
1148 size_t length;
1149 unsigned int i;
1150 unsigned int longest = 0;
1151 unsigned int count = 0;
1152 struct testset *ts;
1153 struct timeval start, end;
1154 struct rusage stats;
1155 struct testlist *failhead = NULL;
1156 struct testlist *failtail = NULL;
1157 struct testlist *current, *next;
1158 int succeeded;
1159 unsigned long total = 0;
1160 unsigned long passed = 0;
1161 unsigned long skipped = 0;
1162 unsigned long failed = 0;
1163 unsigned long aborted = 0;
1165 /* Walk the list of tests to find the longest name. */
1166 for (current = tests; current != NULL; current = current->next) {
1167 length = strlen(current->ts->file);
1168 if (length > longest)
1169 longest = length;
1173 * Add two to longest and round up to the nearest tab stop. This is how
1174 * wide the column for printing the current test name will be.
1176 longest += 2;
1177 if (longest % 8)
1178 longest += 8 - (longest % 8);
1180 /* Start the wall clock timer. */
1181 gettimeofday(&start, NULL);
1183 /* Now, plow through our tests again, running each one. */
1184 for (current = tests; current != NULL; current = current->next) {
1185 ts = current->ts;
1187 /* Print out the name of the test file. */
1188 fputs(ts->file, stdout);
1189 for (i = strlen(ts->file); i < longest; i++)
1190 putchar('.');
1191 if (isatty(STDOUT_FILENO))
1192 fflush(stdout);
1194 /* Run the test. */
1195 ts->path = find_test(ts->file, source, build);
1196 succeeded = test_run(ts);
1197 fflush(stdout);
1199 /* Record cumulative statistics. */
1200 aborted += ts->aborted;
1201 total += ts->count + ts->all_skipped;
1202 passed += ts->passed;
1203 skipped += ts->skipped + ts->all_skipped;
1204 failed += ts->failed;
1205 count++;
1207 /* If the test fails, we shuffle it over to the fail list. */
1208 if (!succeeded) {
1209 if (failhead == NULL) {
1210 failhead = xmalloc(sizeof(struct testset));
1211 failtail = failhead;
1212 } else {
1213 failtail->next = xmalloc(sizeof(struct testset));
1214 failtail = failtail->next;
1216 failtail->ts = ts;
1217 failtail->next = NULL;
1220 total -= skipped;
1222 /* Stop the timer and get our child resource statistics. */
1223 gettimeofday(&end, NULL);
1224 getrusage(RUSAGE_CHILDREN, &stats);
1226 /* Summarize the failures and free the failure list. */
1227 if (failhead != NULL) {
1228 test_fail_summary(failhead);
1229 while (failhead != NULL) {
1230 next = failhead->next;
1231 free(failhead);
1232 failhead = next;
1236 /* Free the memory used by the test lists. */
1237 while (tests != NULL) {
1238 next = tests->next;
1239 free_testset(tests->ts);
1240 free(tests);
1241 tests = next;
1244 /* Print out the final test summary. */
1245 putchar('\n');
1246 if (aborted != 0) {
1247 if (aborted == 1)
1248 printf("Aborted %lu test set", aborted);
1249 else
1250 printf("Aborted %lu test sets", aborted);
1251 printf(", passed %lu/%lu tests", passed, total);
1253 else if (failed == 0)
1254 fputs("All tests successful", stdout);
1255 else
1256 printf("Failed %lu/%lu tests, %.2f%% okay", failed, total,
1257 (total - failed) * 100.0 / total);
1258 if (skipped != 0) {
1259 if (skipped == 1)
1260 printf(", %lu test skipped", skipped);
1261 else
1262 printf(", %lu tests skipped", skipped);
1264 puts(".");
1265 printf("Files=%u, Tests=%lu", count, total);
1266 printf(", %.2f seconds", tv_diff(&end, &start));
1267 printf(" (%.2f usr + %.2f sys = %.2f CPU)\n",
1268 tv_seconds(&stats.ru_utime), tv_seconds(&stats.ru_stime),
1269 tv_sum(&stats.ru_utime, &stats.ru_stime));
1270 return (failed == 0 && aborted == 0);
1275 * Run a single test case. This involves just running the test program after
1276 * having done the environment setup and finding the test program.
1278 static void
1279 test_single(const char *program, const char *source, const char *build)
1281 char *path;
1283 path = find_test(program, source, build);
1284 if (execl(path, path, (char *) 0) == -1)
1285 sysdie("cannot exec %s", path);
1290 * Main routine. Set the SOURCE and BUILD environment variables and then,
1291 * given a file listing tests, run each test listed.
1294 main(int argc, char *argv[])
1296 int option;
1297 int status = 0;
1298 int single = 0;
1299 char *source_env = NULL;
1300 char *build_env = NULL;
1301 const char *shortlist;
1302 const char *list = NULL;
1303 const char *source = SOURCE;
1304 const char *build = BUILD;
1305 struct testlist *tests;
1307 while ((option = getopt(argc, argv, "b:hl:os:")) != EOF) {
1308 switch (option) {
1309 case 'b':
1310 build = optarg;
1311 break;
1312 case 'h':
1313 printf(usage_message, argv[0], argv[0], argv[0]);
1314 exit(0);
1315 break;
1316 case 'l':
1317 list = optarg;
1318 break;
1319 case 'o':
1320 single = 1;
1321 break;
1322 case 's':
1323 source = optarg;
1324 break;
1325 default:
1326 exit(1);
1329 argv += optind;
1330 argc -= optind;
1331 if ((list == NULL && argc < 1) || (list != NULL && argc > 0)) {
1332 fprintf(stderr, usage_message, argv[0], argv[0], argv[0]);
1333 exit(1);
1336 /* Set SOURCE and BUILD environment variables. */
1337 if (source != NULL) {
1338 source_env = xmalloc(strlen("SOURCE=") + strlen(source) + 1);
1339 sprintf(source_env, "SOURCE=%s", source);
1340 if (putenv(source_env) != 0)
1341 sysdie("cannot set SOURCE in the environment");
1343 if (build != NULL) {
1344 build_env = xmalloc(strlen("BUILD=") + strlen(build) + 1);
1345 sprintf(build_env, "BUILD=%s", build);
1346 if (putenv(build_env) != 0)
1347 sysdie("cannot set BUILD in the environment");
1350 /* Run the tests as instructed. */
1351 if (single)
1352 test_single(argv[0], source, build);
1353 else if (list != NULL) {
1354 shortlist = strrchr(list, '/');
1355 if (shortlist == NULL)
1356 shortlist = list;
1357 else
1358 shortlist++;
1359 printf(banner, shortlist);
1360 tests = read_test_list(list);
1361 status = test_batch(tests, source, build) ? 0 : 1;
1362 } else {
1363 tests = build_test_list(argv, argc);
1364 status = test_batch(tests, source, build) ? 0 : 1;
1367 /* For valgrind cleanliness, free all our memory. */
1368 if (source_env != NULL) {
1369 putenv((char *) "SOURCE=");
1370 free(source_env);
1372 if (build_env != NULL) {
1373 putenv((char *) "BUILD=");
1374 free(build_env);
1376 exit(status);