OPENAFS-SA-2017-001: rx: Sanity-check received MTU and twind values
[pkg-k5-afs_openafs.git] / tests / runtests.c
blobaf057a325b4c8dbf54d2c76abbe30a0fbe39e00e
1 /*
2 * Run a set of tests, reporting results.
4 * Usage:
6 * runtests <test-list>
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:
15 * ok <number>
16 * not ok <number>
17 * ok <number> # skip
18 * not ok <number> # todo
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.
28 * As a special case, the first line of the output may be in the form:
30 * 1..0 # skip some reason
32 * which indicates that this entire test case should be skipped and gives a
33 * reason.
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 #.
39 * This is a subset of TAP as documented in Test::Harness::TAP or
40 * TAP::Parser::Grammar, which comes with Perl.
42 * Any bug reports, bug fixes, and improvements are very much welcome and
43 * should be sent to the e-mail address below.
45 * Copyright 2000, 2001, 2004, 2006, 2007, 2008, 2009, 2010
46 * Russ Allbery <rra@stanford.edu>
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:
55 * The above copyright notice and this permission notice shall be included in
56 * all copies or substantial portions of the Software.
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.
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>
81 /* sys/time.h must be included before sys/resource.h on some platforms. */
82 #include <sys/resource.h>
84 /* AIX doesn't have WCOREDUMP. */
85 #ifndef WCOREDUMP
86 # define WCOREDUMP(status) ((unsigned)(status) & 0x80)
87 #endif
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.
95 #ifndef SOURCE
96 # define SOURCE NULL
97 #endif
98 #ifndef BUILD
99 # define BUILD NULL
100 #endif
102 /* Test status codes. */
103 enum test_status {
104 TEST_FAIL,
105 TEST_PASS,
106 TEST_SKIP,
107 TEST_INVALID
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. */
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. */
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. */
143 /* Structure to hold a linked list of test sets. */
144 struct testlist {
145 struct testset *ts;
146 struct testlist *next;
150 * Header used for test output. %s is replaced by the file name of the list
151 * of tests.
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";
157 /* Header for reports of failed tests. */
158 static const char header[] = "\n\
159 Failed Set Fail/Total (%) Skip Stat Failing Tests\n\
160 -------------------------- -------------- ---- ---- ------------------------";
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__)
169 * Report a fatal error, including the results of strerror, and exit.
171 static void
172 sysdie(const char *format, ...)
174 int oerrno;
175 va_list args;
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);
189 * Allocate memory, reporting a fatal error and exiting on failure.
191 static void *
192 x_malloc(size_t size, const char *file, int line)
194 void *p;
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;
205 * Reallocate memory, reporting a fatal error and exiting on failure.
207 static void *
208 x_realloc(void *p, size_t size, const char *file, int line)
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;
219 * Copy a string, reporting a fatal error and exiting on failure.
221 static char *
222 x_strdup(const char *s, const char *file, int line)
224 char *p;
225 size_t len;
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;
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.
241 static double
242 tv_seconds(const struct timeval *tv)
244 return difftime(tv->tv_sec, 0) + tv->tv_usec * 1e-6;
249 * Given two struct timevals, return the difference in seconds.
251 static double
252 tv_diff(const struct timeval *tv1, const struct timeval *tv0)
254 return tv_seconds(tv1) - tv_seconds(tv0);
259 * Given two struct timevals, return the sum in seconds as a double.
261 static double
262 tv_sum(const struct timeval *tv1, const struct timeval *tv2)
264 return tv_seconds(tv1) + tv_seconds(tv2);
269 * Given a pointer to a string, skip any leading whitespace and return a
270 * pointer to the first non-whitespace character.
272 static const char *
273 skip_whitespace(const char *p)
275 while (isspace((unsigned char)(*p)))
276 p++;
277 return p;
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.
286 static pid_t
287 test_start(const char *path, int *fd)
289 int fds[2], errfd;
290 pid_t child;
292 if (pipe(fds) == -1) {
293 puts("ABORTED");
294 fflush(stdout);
295 sysdie("can't create pipe");
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);
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]);
320 *fd = fds[0];
321 return child;
326 * Back up over the output saying what test we were executing.
328 static void
329 test_backspace(struct testset *ts)
331 unsigned int i;
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;
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.
351 static int
352 test_plan(const char *line, struct testset *ts)
354 unsigned long i;
355 long n;
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.
362 line = skip_whitespace(line);
363 if (strncmp(line, "1..", 3) == 0)
364 line += 3;
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.
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';
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;
393 if (n <= 0) {
394 puts("ABORTED (invalid test count)");
395 ts->aborted = 1;
396 ts->reported = 1;
397 return 0;
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;
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;
420 ts->plan = PLAN_FINAL;
422 return 1;
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.
432 static void
433 test_checkline(const char *line, struct testset *ts)
435 enum test_status status = TEST_PASS;
436 const char *bail;
437 char *end;
438 long number;
439 unsigned long i, current;
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;
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;
455 ts->aborted = 1;
456 return;
460 * If the given line isn't newline-terminated, it was too big for an
461 * fgets(), which means ignore it.
463 if (line[strlen(line) - 1] != '\n')
464 return;
466 /* If the line begins with a hash mark, ignore it. */
467 if (line[0] == '#')
468 return;
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;
486 /* Parse the line, ignoring something we can't parse. */
487 if (strncmp(line, "not ", 4) == 0) {
488 status = TEST_FAIL;
489 line += 4;
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;
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;
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;
526 * Handle directives. We should probably do something more interesting
527 * with unexpected passes of todo tests.
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;
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;
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;
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);
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.
573 static unsigned int
574 test_print_range(unsigned long first, unsigned long last, unsigned int chars,
575 unsigned int limit)
577 unsigned int needed = 0;
578 unsigned int out = 0;
579 unsigned long n;
581 if (chars > 0) {
582 needed += 2;
583 if (!limit || chars <= limit) out += printf(", ");
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++;
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);
600 return out;
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.
610 static void
611 test_summarize(struct testset *ts, int status)
613 unsigned long i;
614 unsigned long missing = 0;
615 unsigned long failed = 0;
616 unsigned long first = 0;
617 unsigned long last = 0;
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;
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;
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);
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');
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.
687 static int
688 test_analyze(struct testset *ts)
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;
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);
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.
736 static int
737 test_run(struct testset *ts)
739 pid_t testpid, child;
740 int outfd, status;
741 unsigned long i;
742 FILE *output;
743 char buffer[BUFSIZ];
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");
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);
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.
766 while (fgets(buffer, sizeof(buffer), output))
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);
775 sysdie("waitpid for %u failed", (unsigned int) testpid);
777 if (ts->all_skipped)
778 ts->aborted = 0;
779 status = test_analyze(ts);
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;
789 return status;
793 /* Summarize a list of test failures. */
794 static void
795 test_fail_summary(const struct testlist *fails)
797 struct testset *ts;
798 unsigned int chars;
799 unsigned long i, first, last, total;
801 puts(header);
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;
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;
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);
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.
856 * The caller is responsible for freeing the path member of the testset
857 * struct.
859 static void
860 find_test(const char *name, struct testset *ts, const char *source,
861 const char *build)
863 char *path;
864 const char *bases[] = { ".", build, source, NULL };
865 unsigned int i;
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;
877 if (path == NULL) {
878 path = xmalloc(strlen(name) + 3);
879 sprintf(path, "%s-t", name);
881 ts->path = path;
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.
893 static int
894 test_batch(const char *testlist, const char *source, const char *build)
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;
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.
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);
928 if (length > longest)
929 longest = length;
931 if (fseek(tests, 0, SEEK_SET) == -1)
932 sysdie("can't rewind %s", testlist);
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.
938 longest += 2;
939 if (longest % 8)
940 longest += 8 - (longest % 8);
942 /* Start the wall clock timer. */
943 gettimeofday(&start, NULL);
946 * Now, plow through our tests again, running each one. Check line
947 * length again out of paranoia.
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);
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;
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;
995 total -= skipped;
997 /* Stop the timer and get our child resource statistics. */
998 gettimeofday(&end, NULL);
999 getrusage(RUSAGE_CHILDREN, &stats);
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;
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);
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);
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);
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.
1043 static void
1044 test_single(const char *program, const char *source, const char *build)
1046 struct testset ts;
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);
1056 * Main routine. Set the SOURCE and BUILD environment variables and then,
1057 * given a file listing tests, run each test listed.
1060 main(int argc, char *argv[])
1062 int option;
1063 int single = 0;
1064 char *setting;
1065 const char *list;
1066 const char *source = SOURCE;
1067 const char *build = BUILD;
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);
1084 argc -= optind;
1085 argv += optind;
1086 if (argc != 1) {
1087 fprintf(stderr, "Usage: runtests <test-list>\n");
1088 exit(1);
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");
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");
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);