]>
jfr.im git - solanum.git/blob - tests/runtests.c
2 * Run a set of tests, reporting results.
6 * runtests [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>
7 * runtests [-hv] [-b <build-dir>] [-s <source-dir>] <test> [<test> ...]
8 * runtests -o [-h] [-b <build-dir>] [-s <source-dir>] <test>
10 * In the first case, expects a list of executables located in the given file,
11 * one line per executable. For each one, runs it as part of a test suite,
12 * reporting results. In the second case, use the same infrastructure, but
13 * run only the tests listed on the command line.
15 * Test output should start with a line containing the number of tests
16 * (numbered from 1 to this number), optionally preceded by "1..", although
17 * that line may be given anywhere in the output. Each additional line should
18 * be in the following format:
23 * not ok <number> # todo
25 * where <number> is the number of the test. An optional comment is permitted
26 * after the number if preceded by whitespace. ok indicates success, not ok
27 * indicates failure. "# skip" and "# todo" are a special cases of a comment,
28 * and must start with exactly that formatting. They indicate the test was
29 * skipped for some reason (maybe because it doesn't apply to this platform)
30 * or is testing something known to currently fail. The text following either
31 * "# skip" or "# todo" and whitespace is the reason.
33 * As a special case, the first line of the output may be in the form:
35 * 1..0 # skip some reason
37 * which indicates that this entire test case should be skipped and gives a
40 * Any other lines are ignored, although for compliance with the TAP protocol
41 * all lines other than the ones in the above format should be sent to
42 * standard error rather than standard output and start with #.
44 * This is a subset of TAP as documented in Test::Harness::TAP or
45 * TAP::Parser::Grammar, which comes with Perl.
47 * If the -o option is given, instead run a single test and display all of its
48 * output. This is intended for use with failing tests so that the person
49 * running the test suite can get more details about what failed.
51 * If built with the C preprocessor symbols C_TAP_SOURCE and C_TAP_BUILD
52 * defined, C TAP Harness will export those values in the environment so that
53 * tests can find the source and build directory and will look for tests under
54 * both directories. These paths can also be set with the -b and -s
55 * command-line options, which will override anything set at build time.
57 * If the -v option is given, or the C_TAP_VERBOSE environment variable is set,
58 * display the full output of each test as it runs rather than showing a
59 * summary of the results of each test.
61 * Any bug reports, bug fixes, and improvements are very much welcome and
62 * should be sent to the e-mail address below. This program is part of C TAP
63 * Harness <https://www.eyrie.org/~eagle/software/c-tap-harness/>.
65 * Copyright 2000, 2001, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
66 * 2014, 2015, 2016 Russ Allbery <eagle@eyrie.org>
68 * Permission is hereby granted, free of charge, to any person obtaining a
69 * copy of this software and associated documentation files (the "Software"),
70 * to deal in the Software without restriction, including without limitation
71 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
72 * and/or sell copies of the Software, and to permit persons to whom the
73 * Software is furnished to do so, subject to the following conditions:
75 * The above copyright notice and this permission notice shall be included in
76 * all copies or substantial portions of the Software.
78 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
79 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
80 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
81 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
82 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
83 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
84 * DEALINGS IN THE SOFTWARE.
87 /* Required for fdopen(), getopt(), and putenv(). */
88 #if defined(__STRICT_ANSI__) || defined(PEDANTIC)
89 # ifndef _XOPEN_SOURCE
90 # define _XOPEN_SOURCE 500
104 #include <sys/stat.h>
105 #include <sys/time.h>
106 #include <sys/types.h>
107 #include <sys/wait.h>
111 /* sys/time.h must be included before sys/resource.h on some platforms. */
112 #include <sys/resource.h>
114 /* AIX 6.1 (and possibly later) doesn't have WCOREDUMP. */
116 # define WCOREDUMP(status) ((unsigned)(status) & 0x80)
120 * POSIX requires that these be defined in <unistd.h>, but they're not always
121 * available. If one of them has been defined, all the rest almost certainly
125 # define STDIN_FILENO 0
126 # define STDOUT_FILENO 1
127 # define STDERR_FILENO 2
131 * Used for iterating through arrays. Returns the number of elements in the
132 * array (useful for a < upper bound in a for loop).
134 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
137 * The source and build versions of the tests directory. This is used to set
138 * the C_TAP_SOURCE and C_TAP_BUILD environment variables (and the SOURCE and
139 * BUILD environment variables set for backward compatibility) and find test
140 * programs, if set. Normally, this should be set as part of the build
141 * process to the test subdirectories of $(abs_top_srcdir) and
142 * $(abs_top_builddir) respectively.
145 # define C_TAP_SOURCE NULL
148 # define C_TAP_BUILD NULL
151 /* Test status codes. */
159 /* Really, just a boolean, but this is more self-documenting. */
165 /* Indicates the state of our plan. */
167 PLAN_INIT
, /* Nothing seen yet. */
168 PLAN_FIRST
, /* Plan seen before any tests. */
169 PLAN_PENDING
, /* Test seen and no plan yet. */
170 PLAN_FINAL
/* Plan seen after some tests. */
173 /* Error exit statuses for test processes. */
174 #define CHILDERR_DUP 100 /* Couldn't redirect stderr or stdout. */
175 #define CHILDERR_EXEC 101 /* Couldn't exec child process. */
176 #define CHILDERR_STDIN 102 /* Couldn't open stdin file. */
177 #define CHILDERR_STDERR 103 /* Couldn't open stderr file. */
179 /* Structure to hold data for a set of tests. */
181 char *file
; /* The file name of the test. */
182 char *path
; /* The path to the test program. */
183 enum plan_status plan
; /* The status of our plan. */
184 unsigned long count
; /* Expected count of tests. */
185 unsigned long current
; /* The last seen test number. */
186 unsigned int length
; /* The length of the last status message. */
187 unsigned long passed
; /* Count of passing tests. */
188 unsigned long failed
; /* Count of failing lists. */
189 unsigned long skipped
; /* Count of skipped tests (passed). */
190 unsigned long allocated
; /* The size of the results table. */
191 enum test_status
*results
; /* Table of results by test number. */
192 unsigned int aborted
; /* Whether the set was aborted. */
193 int reported
; /* Whether the results were reported. */
194 int status
; /* The exit status of the test. */
195 unsigned int all_skipped
; /* Whether all tests were skipped. */
196 char *reason
; /* Why all tests were skipped. */
199 /* Structure to hold a linked list of test sets. */
202 struct testlist
*next
;
206 * Usage message. Should be used as a printf format with four arguments: the
207 * path to runtests, given three times, and the usage_description. This is
208 * split into variables to satisfy the pedantic ISO C90 limit on strings.
210 static const char usage_message
[] = "\
211 Usage: %s [-hv] [-b <build-dir>] [-s <source-dir>] <test> ...\n\
212 %s [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>\n\
213 %s -o [-h] [-b <build-dir>] [-s <source-dir>] <test>\n\
216 -b <build-dir> Set the build directory to <build-dir>\n\
218 static const char usage_extra
[] = "\
219 -l <list> Take the list of tests to run from <test-list>\n\
220 -o Run a single test rather than a list of tests\n\
221 -s <source-dir> Set the source directory to <source-dir>\n\
222 -v Show the full output of each test\n\
224 runtests normally runs each test listed on the command line. With the -l\n\
225 option, it instead runs every test listed in a file. With the -o option,\n\
226 it instead runs a single test and shows its complete output.\n";
229 * Header used for test output. %s is replaced by the file name of the list
232 static const char banner
[] = "\n\
233 Running all tests listed in %s. If any tests fail, run the failing\n\
234 test program with runtests -o to see more details.\n\n";
236 /* Header for reports of failed tests. */
237 static const char header
[] = "\n\
238 Failed Set Fail/Total (%) Skip Stat Failing Tests\n\
239 -------------------------- -------------- ---- ---- ------------------------";
241 /* Include the file name and line number in malloc failures. */
242 #define xcalloc(n, size) x_calloc((n), (size), __FILE__, __LINE__)
243 #define xmalloc(size) x_malloc((size), __FILE__, __LINE__)
244 #define xstrdup(p) x_strdup((p), __FILE__, __LINE__)
245 #define xreallocarray(p, n, size) \
246 x_reallocarray((p), (n), (size), __FILE__, __LINE__)
249 * __attribute__ is available in gcc 2.5 and later, but only with gcc 2.7
250 * could you use the __format__ form of the attributes, which is what we use
251 * (to avoid confusion with other macros).
253 #ifndef __attribute__
254 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
255 # define __attribute__(spec) /* empty */
260 * We use __alloc_size__, but it was only available in fairly recent versions
261 * of GCC. Suppress warnings about the unknown attribute if GCC is too old.
262 * We know that we're GCC at this point, so we can use the GCC variadic macro
263 * extension, which will still work with versions of GCC too old to have C99
264 * variadic macro support.
266 #if !defined(__attribute__) && !defined(__alloc_size__)
267 # if defined(__GNUC__) && !defined(__clang__)
268 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
269 # define __alloc_size__(spec, args...) /* empty */
275 * LLVM and Clang pretend to be GCC but don't support all of the __attribute__
276 * settings that GCC does. For them, suppress warnings about unknown
277 * attributes on declarations. This unfortunately will affect the entire
278 * compilation context, but there's no push and pop available.
280 #if !defined(__attribute__) && (defined(__llvm__) || defined(__clang__))
281 # pragma GCC diagnostic ignored "-Wattributes"
284 /* Declare internal functions that benefit from compiler attributes. */
285 static void sysdie(const char *, ...)
286 __attribute__((__nonnull__
, __noreturn__
, __format__(printf
, 1, 2)));
287 static void *x_calloc(size_t, size_t, const char *, int)
288 __attribute__((__alloc_size__(1, 2), __malloc__
, __nonnull__
));
289 static void *x_malloc(size_t, const char *, int)
290 __attribute__((__alloc_size__(1), __malloc__
, __nonnull__
));
291 static void *x_reallocarray(void *, size_t, size_t, const char *, int)
292 __attribute__((__alloc_size__(2, 3), __malloc__
, __nonnull__(4)));
293 static char *x_strdup(const char *, const char *, int)
294 __attribute__((__malloc__
, __nonnull__
));
298 * Report a fatal error, including the results of strerror, and exit.
301 sysdie(const char *format
, ...)
308 fprintf(stderr
, "runtests: ");
309 va_start(args
, format
);
310 vfprintf(stderr
, format
, args
);
312 fprintf(stderr
, ": %s\n", strerror(oerrno
));
318 * Allocate zeroed memory, reporting a fatal error and exiting on failure.
321 x_calloc(size_t n
, size_t size
, const char *file
, int line
)
326 size
= (size
> 0) ? size
: 1;
329 sysdie("failed to calloc %lu bytes at %s line %d",
330 (unsigned long) size
, file
, line
);
336 * Allocate memory, reporting a fatal error and exiting on failure.
339 x_malloc(size_t size
, const char *file
, int line
)
345 sysdie("failed to malloc %lu bytes at %s line %d",
346 (unsigned long) size
, file
, line
);
352 * Reallocate memory, reporting a fatal error and exiting on failure.
354 * We should technically use SIZE_MAX here for the overflow check, but
355 * SIZE_MAX is C99 and we're only assuming C89 + SUSv3, which does not
356 * guarantee that it exists. They do guarantee that UINT_MAX exists, and we
357 * can assume that UINT_MAX <= SIZE_MAX. And we should not be allocating
358 * anything anywhere near that large.
360 * (In theory, C89 and C99 permit size_t to be smaller than unsigned int, but
361 * I disbelieve in the existence of such systems and they will have to cope
362 * without overflow checks.)
365 x_reallocarray(void *p
, size_t n
, size_t size
, const char *file
, int line
)
367 if (n
> 0 && UINT_MAX
/ n
<= size
)
368 sysdie("realloc too large at %s line %d", file
, line
);
369 p
= realloc(p
, n
* size
);
371 sysdie("failed to realloc %lu bytes at %s line %d",
372 (unsigned long) (n
* size
), file
, line
);
378 * Copy a string, reporting a fatal error and exiting on failure.
381 x_strdup(const char *s
, const char *file
, int line
)
389 sysdie("failed to strdup %lu bytes at %s line %d",
390 (unsigned long) len
, file
, line
);
397 * Form a new string by concatenating multiple strings. The arguments must be
398 * terminated by (const char *) 0.
400 * This function only exists because we can't assume asprintf. We can't
401 * simulate asprintf with snprintf because we're only assuming SUSv3, which
402 * does not require that snprintf with a NULL buffer return the required
403 * length. When those constraints are relaxed, this should be ripped out and
404 * replaced with asprintf or a more trivial replacement with snprintf.
407 concat(const char *first
, ...)
416 * Find the total memory required. Ensure we don't overflow length. We
417 * aren't guaranteed to have SIZE_MAX, so use UINT_MAX as an acceptable
418 * substitute (see the x_nrealloc comments).
420 va_start(args
, first
);
421 for (string
= first
; string
!= NULL
; string
= va_arg(args
, const char *)) {
422 if (length
>= UINT_MAX
- strlen(string
)) {
424 sysdie("strings too long in concat");
426 length
+= strlen(string
);
431 /* Create the string. */
432 result
= xmalloc(length
);
433 va_start(args
, first
);
435 for (string
= first
; string
!= NULL
; string
= va_arg(args
, const char *)) {
436 memcpy(result
+ offset
, string
, strlen(string
));
437 offset
+= strlen(string
);
440 result
[offset
] = '\0';
446 * Given a struct timeval, return the number of seconds it represents as a
447 * double. Use difftime() to convert a time_t to a double.
450 tv_seconds(const struct timeval
*tv
)
452 return difftime(tv
->tv_sec
, 0) + tv
->tv_usec
* 1e-6;
457 * Given two struct timevals, return the difference in seconds.
460 tv_diff(const struct timeval
*tv1
, const struct timeval
*tv0
)
462 return tv_seconds(tv1
) - tv_seconds(tv0
);
467 * Given two struct timevals, return the sum in seconds as a double.
470 tv_sum(const struct timeval
*tv1
, const struct timeval
*tv2
)
472 return tv_seconds(tv1
) + tv_seconds(tv2
);
477 * Given a pointer to a string, skip any leading whitespace and return a
478 * pointer to the first non-whitespace character.
481 skip_whitespace(const char *p
)
483 while (isspace((unsigned char)(*p
)))
490 * Start a program, connecting its stdout to a pipe on our end and its stderr
491 * to /dev/null, and storing the file descriptor to read from in the two
492 * argument. Returns the PID of the new process. Errors are fatal.
495 test_start(const char *path
, int *fd
)
497 int fds
[2], infd
, errfd
;
500 /* Create a pipe used to capture the output from the test program. */
501 if (pipe(fds
) == -1) {
504 sysdie("can't create pipe");
507 /* Fork a child process, massage the file descriptors, and exec. */
513 sysdie("can't fork");
515 /* In the child. Set up our standard output. */
518 close(STDOUT_FILENO
);
519 if (dup2(fds
[1], STDOUT_FILENO
) < 0)
523 /* Point standard input at /dev/null. */
525 infd
= open("/dev/null", O_RDONLY
);
527 _exit(CHILDERR_STDIN
);
528 if (infd
!= STDIN_FILENO
) {
529 if (dup2(infd
, STDIN_FILENO
) < 0)
534 /* Point standard error at /dev/null. */
535 close(STDERR_FILENO
);
536 errfd
= open("/dev/null", O_WRONLY
);
538 _exit(CHILDERR_STDERR
);
539 if (errfd
!= STDERR_FILENO
) {
540 if (dup2(errfd
, STDERR_FILENO
) < 0)
545 /* Now, exec our process. */
546 if (execl(path
, path
, (char *) 0) == -1)
547 _exit(CHILDERR_EXEC
);
550 /* In parent. Close the extra file descriptor. */
561 * Back up over the output saying what test we were executing.
564 test_backspace(struct testset
*ts
)
568 if (!isatty(STDOUT_FILENO
))
570 for (i
= 0; i
< ts
->length
; i
++)
572 for (i
= 0; i
< ts
->length
; i
++)
574 for (i
= 0; i
< ts
->length
; i
++)
581 * Allocate or resize the array of test results to be large enough to contain
582 * the test number in.
585 resize_results(struct testset
*ts
, unsigned long n
)
590 /* If there's already enough space, return quickly. */
591 if (n
<= ts
->allocated
)
595 * If no space has been allocated, do the initial allocation. Otherwise,
596 * resize. Start with 32 test cases and then add 1024 with each resize to
597 * try to reduce the number of reallocations.
599 if (ts
->allocated
== 0) {
600 s
= (n
> 32) ? n
: 32;
601 ts
->results
= xcalloc(s
, sizeof(enum test_status
));
603 s
= (n
> ts
->allocated
+ 1024) ? n
: ts
->allocated
+ 1024;
604 ts
->results
= xreallocarray(ts
->results
, s
, sizeof(enum test_status
));
607 /* Set the results for the newly-allocated test array. */
608 for (i
= ts
->allocated
; i
< s
; i
++)
609 ts
->results
[i
] = TEST_INVALID
;
615 * Report an invalid test number and set the appropriate flags. Pulled into a
616 * separate function since we do this in several places.
619 invalid_test_number(struct testset
*ts
, long n
, enum test_verbose verbose
)
623 printf("ABORTED (invalid test number %ld)\n", n
);
630 * Read the plan line of test output, which should contain the range of test
631 * numbers. We may initialize the testset structure here if we haven't yet
632 * seen a test. Return true if initialization succeeded and the test should
633 * continue, false otherwise.
636 test_plan(const char *line
, struct testset
*ts
, enum test_verbose verbose
)
641 * Accept a plan without the leading 1.. for compatibility with older
642 * versions of runtests. This will only be allowed if we've not yet seen
645 line
= skip_whitespace(line
);
646 if (strncmp(line
, "1..", 3) == 0)
650 * Get the count and check it for validity.
652 * If we have something of the form "1..0 # skip foo", the whole file was
653 * skipped; record that. If we do skip the whole file, zero out all of
654 * our statistics, since they're no longer relevant.
656 * strtol is called with a second argument to advance the line pointer
657 * past the count to make it simpler to detect the # skip case.
659 n
= strtol(line
, (char **) &line
, 10);
661 line
= skip_whitespace(line
);
663 line
= skip_whitespace(line
+ 1);
664 if (strncasecmp(line
, "skip", 4) == 0) {
665 line
= skip_whitespace(line
+ 4);
667 ts
->reason
= xstrdup(line
);
668 ts
->reason
[strlen(ts
->reason
) - 1] = '\0';
681 puts("ABORTED (invalid test count)");
688 * If we are doing lazy planning, check the plan against the largest test
689 * number that we saw and fail now if we saw a check outside the plan
692 if (ts
->plan
== PLAN_PENDING
&& (unsigned long) n
< ts
->count
) {
693 invalid_test_number(ts
, (long) ts
->count
, verbose
);
698 * Otherwise, allocated or resize the results if needed and update count,
699 * and then record that we've seen a plan.
701 resize_results(ts
, (unsigned long) n
);
702 ts
->count
= (unsigned long) n
;
703 if (ts
->plan
== PLAN_INIT
)
704 ts
->plan
= PLAN_FIRST
;
705 else if (ts
->plan
== PLAN_PENDING
)
706 ts
->plan
= PLAN_FINAL
;
712 * Given a single line of output from a test, parse it and return the success
713 * status of that test. Anything printed to stdout not matching the form
714 * /^(not )?ok \d+/ is ignored. Sets ts->current to the test number that just
718 test_checkline(const char *line
, struct testset
*ts
,
719 enum test_verbose verbose
)
721 enum test_status status
= TEST_PASS
;
725 unsigned long current
;
728 /* Before anything, check for a test abort. */
729 bail
= strstr(line
, "Bail out!");
731 bail
= skip_whitespace(bail
+ strlen("Bail out!"));
735 length
= strlen(bail
);
736 if (bail
[length
- 1] == '\n')
740 printf("ABORTED (%.*s)\n", (int) length
, bail
);
748 * If the given line isn't newline-terminated, it was too big for an
749 * fgets(), which means ignore it.
751 if (line
[strlen(line
) - 1] != '\n')
754 /* If the line begins with a hash mark, ignore it. */
758 /* If we haven't yet seen a plan, look for one. */
759 if (ts
->plan
== PLAN_INIT
&& isdigit((unsigned char)(*line
))) {
760 if (!test_plan(line
, ts
, verbose
))
762 } else if (strncmp(line
, "1..", 3) == 0) {
763 if (ts
->plan
== PLAN_PENDING
) {
764 if (!test_plan(line
, ts
, verbose
))
769 puts("ABORTED (multiple plans)");
776 /* Parse the line, ignoring something we can't parse. */
777 if (strncmp(line
, "not ", 4) == 0) {
781 if (strncmp(line
, "ok", 2) != 0)
783 line
= skip_whitespace(line
+ 2);
785 number
= strtol(line
, &end
, 10);
786 if (errno
!= 0 || end
== line
)
787 current
= ts
->current
+ 1;
788 else if (number
<= 0) {
789 invalid_test_number(ts
, number
, verbose
);
792 current
= (unsigned long) number
;
793 if (current
> ts
->count
&& ts
->plan
== PLAN_FIRST
) {
794 invalid_test_number(ts
, (long) current
, verbose
);
798 /* We have a valid test result. Tweak the results array if needed. */
799 if (ts
->plan
== PLAN_INIT
|| ts
->plan
== PLAN_PENDING
) {
800 ts
->plan
= PLAN_PENDING
;
801 resize_results(ts
, current
);
802 if (current
> ts
->count
)
807 * Handle directives. We should probably do something more interesting
808 * with unexpected passes of todo tests.
810 while (isdigit((unsigned char)(*line
)))
812 line
= skip_whitespace(line
);
814 line
= skip_whitespace(line
+ 1);
815 if (strncasecmp(line
, "skip", 4) == 0)
817 if (strncasecmp(line
, "todo", 4) == 0)
818 status
= (status
== TEST_FAIL
) ? TEST_SKIP
: TEST_FAIL
;
821 /* Make sure that the test number is in range and not a duplicate. */
822 if (ts
->results
[current
- 1] != TEST_INVALID
) {
825 printf("ABORTED (duplicate test number %lu)\n", current
);
831 /* Good results. Increment our various counters. */
833 case TEST_PASS
: ts
->passed
++; break;
834 case TEST_FAIL
: ts
->failed
++; break;
835 case TEST_SKIP
: ts
->skipped
++; break;
836 case TEST_INVALID
: break;
838 ts
->current
= current
;
839 ts
->results
[current
- 1] = status
;
840 if (!verbose
&& isatty(STDOUT_FILENO
)) {
842 if (ts
->plan
== PLAN_PENDING
)
843 outlen
= printf("%lu/?", current
);
845 outlen
= printf("%lu/%lu", current
, ts
->count
);
846 ts
->length
= (outlen
>= 0) ? (unsigned int) outlen
: 0;
853 * Print out a range of test numbers, returning the number of characters it
854 * took up. Takes the first number, the last number, the number of characters
855 * already printed on the line, and the limit of number of characters the line
856 * can hold. Add a comma and a space before the range if chars indicates that
857 * something has already been printed on the line, and print ... instead if
858 * chars plus the space needed would go over the limit (use a limit of 0 to
862 test_print_range(unsigned long first
, unsigned long last
, unsigned long chars
,
865 unsigned int needed
= 0;
868 for (n
= first
; n
> 0; n
/= 10)
871 for (n
= last
; n
> 0; n
/= 10)
877 if (limit
> 0 && chars
+ needed
> limit
) {
879 if (chars
<= limit
) {
891 printf("%lu-", first
);
899 * Summarize a single test set. The second argument is 0 if the set exited
900 * cleanly, a positive integer representing the exit status if it exited
901 * with a non-zero status, and a negative integer representing the signal
902 * that terminated it if it was killed by a signal.
905 test_summarize(struct testset
*ts
, int status
)
908 unsigned long missing
= 0;
909 unsigned long failed
= 0;
910 unsigned long first
= 0;
911 unsigned long last
= 0;
914 fputs("ABORTED", stdout
);
916 printf(" (passed %lu/%lu)", ts
->passed
, ts
->count
- ts
->skipped
);
918 for (i
= 0; i
< ts
->count
; i
++) {
919 if (ts
->results
[i
] == TEST_INVALID
) {
921 fputs("MISSED ", stdout
);
922 if (first
&& i
== last
)
926 test_print_range(first
, last
, missing
- 1, 0);
934 test_print_range(first
, last
, missing
- 1, 0);
937 for (i
= 0; i
< ts
->count
; i
++) {
938 if (ts
->results
[i
] == TEST_FAIL
) {
939 if (missing
&& !failed
)
942 fputs("FAILED ", stdout
);
943 if (first
&& i
== last
)
947 test_print_range(first
, last
, failed
- 1, 0);
955 test_print_range(first
, last
, failed
- 1, 0);
956 if (!missing
&& !failed
) {
957 fputs(!status
? "ok" : "dubious", stdout
);
958 if (ts
->skipped
> 0) {
959 if (ts
->skipped
== 1)
960 printf(" (skipped %lu test)", ts
->skipped
);
962 printf(" (skipped %lu tests)", ts
->skipped
);
967 printf(" (exit status %d)", status
);
969 printf(" (killed by signal %d%s)", -status
,
970 WCOREDUMP(ts
->status
) ? ", core dumped" : "");
976 * Given a test set, analyze the results, classify the exit status, handle a
977 * few special error messages, and then pass it along to test_summarize() for
978 * the regular output. Returns true if the test set ran successfully and all
979 * tests passed or were skipped, false otherwise.
982 test_analyze(struct testset
*ts
)
986 if (ts
->all_skipped
) {
987 if (ts
->reason
== NULL
)
990 printf("skipped (%s)\n", ts
->reason
);
992 } else if (WIFEXITED(ts
->status
) && WEXITSTATUS(ts
->status
) != 0) {
993 switch (WEXITSTATUS(ts
->status
)) {
996 puts("ABORTED (can't dup file descriptors)");
1000 puts("ABORTED (execution failed -- not found?)");
1002 case CHILDERR_STDIN
:
1003 case CHILDERR_STDERR
:
1005 puts("ABORTED (can't open /dev/null)");
1008 test_summarize(ts
, WEXITSTATUS(ts
->status
));
1012 } else if (WIFSIGNALED(ts
->status
)) {
1013 test_summarize(ts
, -WTERMSIG(ts
->status
));
1015 } else if (ts
->plan
!= PLAN_FIRST
&& ts
->plan
!= PLAN_FINAL
) {
1016 puts("ABORTED (no valid test plan)");
1020 test_summarize(ts
, 0);
1021 return (ts
->failed
== 0);
1027 * Runs a single test set, accumulating and then reporting the results.
1028 * Returns true if the test set was successfully run and all tests passed,
1032 test_run(struct testset
*ts
, enum test_verbose verbose
)
1034 pid_t testpid
, child
;
1038 char buffer
[BUFSIZ
];
1040 /* Run the test program. */
1041 testpid
= test_start(ts
->path
, &outfd
);
1042 output
= fdopen(outfd
, "r");
1046 sysdie("fdopen failed");
1050 * Pass each line of output to test_checkline(), and print the line if
1051 * verbosity is requested.
1053 while (!ts
->aborted
&& fgets(buffer
, sizeof(buffer
), output
)) {
1055 printf("%s", buffer
);
1056 test_checkline(buffer
, ts
, verbose
);
1058 if (ferror(output
) || ts
->plan
== PLAN_INIT
)
1064 * Consume the rest of the test output, close the output descriptor,
1065 * retrieve the exit status, and pass that information to test_analyze()
1066 * for eventual output.
1068 while (fgets(buffer
, sizeof(buffer
), output
))
1070 printf("%s", buffer
);
1072 child
= waitpid(testpid
, &ts
->status
, 0);
1073 if (child
== (pid_t
) -1) {
1074 if (!ts
->reported
) {
1078 sysdie("waitpid for %u failed", (unsigned int) testpid
);
1080 if (ts
->all_skipped
)
1082 status
= test_analyze(ts
);
1084 /* Convert missing tests to failed tests. */
1085 for (i
= 0; i
< ts
->count
; i
++) {
1086 if (ts
->results
[i
] == TEST_INVALID
) {
1088 ts
->results
[i
] = TEST_FAIL
;
1096 /* Summarize a list of test failures. */
1098 test_fail_summary(const struct testlist
*fails
)
1102 unsigned long i
, first
, last
, total
;
1106 /* Failed Set Fail/Total (%) Skip Stat Failing (25)
1107 -------------------------- -------------- ---- ---- -------------- */
1108 for (; fails
; fails
= fails
->next
) {
1110 total
= ts
->count
- ts
->skipped
;
1111 printf("%-26.26s %4lu/%-4lu %3.0f%% %4lu ", ts
->file
, ts
->failed
,
1112 total
, total
? (ts
->failed
* 100.0) / total
: 0,
1114 if (WIFEXITED(ts
->status
))
1115 printf("%4d ", WEXITSTATUS(ts
->status
));
1125 for (i
= 0; i
< ts
->count
; i
++) {
1126 if (ts
->results
[i
] == TEST_FAIL
) {
1127 if (first
!= 0 && i
== last
)
1131 chars
+= test_print_range(first
, last
, chars
, 19);
1138 test_print_range(first
, last
, chars
, 19);
1145 * Check whether a given file path is a valid test. Currently, this checks
1146 * whether it is executable and is a regular file. Returns true or false.
1149 is_valid_test(const char *path
)
1153 if (access(path
, X_OK
) < 0)
1155 if (stat(path
, &st
) < 0)
1157 if (!S_ISREG(st
.st_mode
))
1164 * Given the name of a test, a pointer to the testset struct, and the source
1165 * and build directories, find the test. We try first relative to the current
1166 * directory, then in the build directory (if not NULL), then in the source
1167 * directory. In each of those directories, we first try a "-t" extension and
1168 * then a ".t" extension. When we find an executable program, we return the
1169 * path to that program. If none of those paths are executable, just fill in
1170 * the name of the test as is.
1172 * The caller is responsible for freeing the path member of the testset
1176 find_test(const char *name
, const char *source
, const char *build
)
1179 const char *bases
[3], *suffix
, *base
;
1181 const char *suffixes
[3] = { "-t", ".t", "" };
1183 /* Possible base directories. */
1188 /* Try each suffix with each base. */
1189 for (i
= 0; i
< ARRAY_SIZE(suffixes
); i
++) {
1190 suffix
= suffixes
[i
];
1191 for (j
= 0; j
< ARRAY_SIZE(bases
); j
++) {
1195 path
= concat(base
, "/", name
, suffix
, (const char *) 0);
1196 if (is_valid_test(path
))
1203 path
= xstrdup(name
);
1209 * Read a list of tests from a file, returning the list of tests as a struct
1210 * testlist, or NULL if there were no tests (such as a file containing only
1211 * comments). Reports an error to standard error and exits if the list of
1212 * tests cannot be read.
1214 static struct testlist
*
1215 read_test_list(const char *filename
)
1220 char buffer
[BUFSIZ
];
1221 const char *testname
;
1222 struct testlist
*listhead
, *current
;
1224 /* Create the initial container list that will hold our results. */
1225 listhead
= xcalloc(1, sizeof(struct testlist
));
1229 * Open our file of tests to run and read it line by line, creating a new
1230 * struct testlist and struct testset for each line.
1232 file
= fopen(filename
, "r");
1234 sysdie("can't open %s", filename
);
1236 while (fgets(buffer
, sizeof(buffer
), file
)) {
1238 length
= strlen(buffer
) - 1;
1239 if (buffer
[length
] != '\n') {
1240 fprintf(stderr
, "%s:%u: line too long\n", filename
, line
);
1243 buffer
[length
] = '\0';
1245 /* Skip comments, leading spaces, and blank lines. */
1246 testname
= skip_whitespace(buffer
);
1247 if (strlen(testname
) == 0)
1249 if (testname
[0] == '#')
1252 /* Allocate the new testset structure. */
1253 if (current
== NULL
)
1256 current
->next
= xcalloc(1, sizeof(struct testlist
));
1257 current
= current
->next
;
1259 current
->ts
= xcalloc(1, sizeof(struct testset
));
1260 current
->ts
->plan
= PLAN_INIT
;
1261 current
->ts
->file
= xstrdup(testname
);
1265 /* If there were no tests, current is still NULL. */
1266 if (current
== NULL
) {
1271 /* Return the results. */
1277 * Build a list of tests from command line arguments. Takes the argv and argc
1278 * representing the command line arguments and returns a newly allocated test
1279 * list, or NULL if there were no tests. The caller is responsible for
1282 static struct testlist
*
1283 build_test_list(char *argv
[], int argc
)
1286 struct testlist
*listhead
, *current
;
1288 /* Create the initial container list that will hold our results. */
1289 listhead
= xcalloc(1, sizeof(struct testlist
));
1292 /* Walk the list of arguments and create test sets for them. */
1293 for (i
= 0; i
< argc
; i
++) {
1294 if (current
== NULL
)
1297 current
->next
= xcalloc(1, sizeof(struct testlist
));
1298 current
= current
->next
;
1300 current
->ts
= xcalloc(1, sizeof(struct testset
));
1301 current
->ts
->plan
= PLAN_INIT
;
1302 current
->ts
->file
= xstrdup(argv
[i
]);
1305 /* If there were no tests, current is still NULL. */
1306 if (current
== NULL
) {
1311 /* Return the results. */
1316 /* Free a struct testset. */
1318 free_testset(struct testset
*ts
)
1329 * Run a batch of tests. Takes two additional parameters: the root of the
1330 * source directory and the root of the build directory. Test programs will
1331 * be first searched for in the current directory, then the build directory,
1332 * then the source directory. Returns true iff all tests passed, and always
1333 * frees the test list that's passed in.
1336 test_batch(struct testlist
*tests
, const char *source
, const char *build
,
1337 enum test_verbose verbose
)
1341 unsigned int count
= 0;
1343 struct timeval start
, end
;
1344 struct rusage stats
;
1345 struct testlist
*failhead
= NULL
;
1346 struct testlist
*failtail
= NULL
;
1347 struct testlist
*current
, *next
;
1349 unsigned long total
= 0;
1350 unsigned long passed
= 0;
1351 unsigned long skipped
= 0;
1352 unsigned long failed
= 0;
1353 unsigned long aborted
= 0;
1355 /* Walk the list of tests to find the longest name. */
1356 for (current
= tests
; current
!= NULL
; current
= current
->next
) {
1357 length
= strlen(current
->ts
->file
);
1358 if (length
> longest
)
1363 * Add two to longest and round up to the nearest tab stop. This is how
1364 * wide the column for printing the current test name will be.
1368 longest
+= 8 - (longest
% 8);
1370 /* Start the wall clock timer. */
1371 gettimeofday(&start
, NULL
);
1373 /* Now, plow through our tests again, running each one. */
1374 for (current
= tests
; current
!= NULL
; current
= current
->next
) {
1377 /* Print out the name of the test file. */
1378 fputs(ts
->file
, stdout
);
1380 fputs("\n\n", stdout
);
1382 for (i
= strlen(ts
->file
); i
< longest
; i
++)
1384 if (isatty(STDOUT_FILENO
))
1388 ts
->path
= find_test(ts
->file
, source
, build
);
1389 succeeded
= test_run(ts
, verbose
);
1394 /* Record cumulative statistics. */
1395 aborted
+= ts
->aborted
;
1396 total
+= ts
->count
+ ts
->all_skipped
;
1397 passed
+= ts
->passed
;
1398 skipped
+= ts
->skipped
+ ts
->all_skipped
;
1399 failed
+= ts
->failed
;
1402 /* If the test fails, we shuffle it over to the fail list. */
1404 if (failhead
== NULL
) {
1405 failhead
= xmalloc(sizeof(struct testset
));
1406 failtail
= failhead
;
1408 failtail
->next
= xmalloc(sizeof(struct testset
));
1409 failtail
= failtail
->next
;
1412 failtail
->next
= NULL
;
1417 /* Stop the timer and get our child resource statistics. */
1418 gettimeofday(&end
, NULL
);
1419 getrusage(RUSAGE_CHILDREN
, &stats
);
1421 /* Summarize the failures and free the failure list. */
1422 if (failhead
!= NULL
) {
1423 test_fail_summary(failhead
);
1424 while (failhead
!= NULL
) {
1425 next
= failhead
->next
;
1431 /* Free the memory used by the test lists. */
1432 while (tests
!= NULL
) {
1434 free_testset(tests
->ts
);
1439 /* Print out the final test summary. */
1443 printf("Aborted %lu test set", aborted
);
1445 printf("Aborted %lu test sets", aborted
);
1446 printf(", passed %lu/%lu tests", passed
, total
);
1448 else if (failed
== 0)
1449 fputs("All tests successful", stdout
);
1451 printf("Failed %lu/%lu tests, %.2f%% okay", failed
, total
,
1452 (total
- failed
) * 100.0 / total
);
1455 printf(", %lu test skipped", skipped
);
1457 printf(", %lu tests skipped", skipped
);
1460 printf("Files=%u, Tests=%lu", count
, total
);
1461 printf(", %.2f seconds", tv_diff(&end
, &start
));
1462 printf(" (%.2f usr + %.2f sys = %.2f CPU)\n",
1463 tv_seconds(&stats
.ru_utime
), tv_seconds(&stats
.ru_stime
),
1464 tv_sum(&stats
.ru_utime
, &stats
.ru_stime
));
1465 return (failed
== 0 && aborted
== 0);
1470 * Run a single test case. This involves just running the test program after
1471 * having done the environment setup and finding the test program.
1474 test_single(const char *program
, const char *source
, const char *build
)
1478 path
= find_test(program
, source
, build
);
1479 if (execl(path
, path
, (char *) 0) == -1)
1480 sysdie("cannot exec %s", path
);
1485 * Main routine. Set the C_TAP_SOURCE, C_TAP_BUILD, SOURCE, and BUILD
1486 * environment variables and then, given a file listing tests, run each test
1490 main(int argc
, char *argv
[])
1495 enum test_verbose verbose
= CONCISE
;
1496 char *c_tap_source_env
= NULL
;
1497 char *c_tap_build_env
= NULL
;
1498 char *source_env
= NULL
;
1499 char *build_env
= NULL
;
1500 const char *program
;
1501 const char *shortlist
;
1502 const char *list
= NULL
;
1503 const char *source
= C_TAP_SOURCE
;
1504 const char *build
= C_TAP_BUILD
;
1505 struct testlist
*tests
;
1508 while ((option
= getopt(argc
, argv
, "b:hl:os:v")) != EOF
) {
1514 printf(usage_message
, program
, program
, program
, usage_extra
);
1534 if ((list
== NULL
&& argc
< 1) || (list
!= NULL
&& argc
> 0)) {
1535 fprintf(stderr
, usage_message
, program
, program
, program
, usage_extra
);
1540 * If C_TAP_VERBOSE is set in the environment, that also turns on verbose
1543 if (getenv("C_TAP_VERBOSE") != NULL
)
1547 * Set C_TAP_SOURCE and C_TAP_BUILD environment variables. Also set
1548 * SOURCE and BUILD for backward compatibility, although we're trying to
1549 * migrate to the ones with a C_TAP_* prefix.
1551 if (source
!= NULL
) {
1552 c_tap_source_env
= concat("C_TAP_SOURCE=", source
, (const char *) 0);
1553 if (putenv(c_tap_source_env
) != 0)
1554 sysdie("cannot set C_TAP_SOURCE in the environment");
1555 source_env
= concat("SOURCE=", source
, (const char *) 0);
1556 if (putenv(source_env
) != 0)
1557 sysdie("cannot set SOURCE in the environment");
1559 if (build
!= NULL
) {
1560 c_tap_build_env
= concat("C_TAP_BUILD=", build
, (const char *) 0);
1561 if (putenv(c_tap_build_env
) != 0)
1562 sysdie("cannot set C_TAP_BUILD in the environment");
1563 build_env
= concat("BUILD=", build
, (const char *) 0);
1564 if (putenv(build_env
) != 0)
1565 sysdie("cannot set BUILD in the environment");
1568 /* Run the tests as instructed. */
1570 test_single(argv
[0], source
, build
);
1571 else if (list
!= NULL
) {
1572 shortlist
= strrchr(list
, '/');
1573 if (shortlist
== NULL
)
1577 printf(banner
, shortlist
);
1578 tests
= read_test_list(list
);
1579 status
= test_batch(tests
, source
, build
, verbose
) ? 0 : 1;
1581 tests
= build_test_list(argv
, argc
);
1582 status
= test_batch(tests
, source
, build
, verbose
) ? 0 : 1;
1585 /* For valgrind cleanliness, free all our memory. */
1586 if (source_env
!= NULL
) {
1587 putenv((char *) "C_TAP_SOURCE=");
1588 putenv((char *) "SOURCE=");
1589 free(c_tap_source_env
);
1592 if (build_env
!= NULL
) {
1593 putenv((char *) "C_TAP_BUILD=");
1594 putenv((char *) "BUILD=");
1595 free(c_tap_build_env
);