2 * Run a set of tests, reporting results.
4 * Test suite driver that runs a set of tests implementing a subset of the
5 * Test Anything Protocol (TAP) and reports the results.
7 * Any bug reports, bug fixes, and improvements are very much welcome and
8 * should be sent to the e-mail address below. This program is part of C TAP
9 * Harness <https://www.eyrie.org/~eagle/software/c-tap-harness/>.
11 * Copyright 2000-2001, 2004, 2006-2019, 2022 Russ Allbery <eagle@eyrie.org>
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the "Software"),
15 * to deal in the Software without restriction, including without limitation
16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 * and/or sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 * DEALINGS IN THE SOFTWARE.
31 * SPDX-License-Identifier: MIT
37 * runtests [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>
38 * runtests [-hv] [-b <build-dir>] [-s <source-dir>] <test> [<test> ...]
39 * runtests -o [-h] [-b <build-dir>] [-s <source-dir>] <test>
41 * In the first case, expects a list of executables located in the given file,
42 * one line per executable, possibly followed by a space-separated list of
43 * options. For each one, runs it as part of a test suite, reporting results.
44 * In the second case, use the same infrastructure, but run only the tests
45 * listed on the command line.
47 * Test output should start with a line containing the number of tests
48 * (numbered from 1 to this number), optionally preceded by "1..", although
49 * that line may be given anywhere in the output. Each additional line should
50 * be in the following format:
55 * not ok <number> # todo
57 * where <number> is the number of the test. An optional comment is permitted
58 * after the number if preceded by whitespace. ok indicates success, not ok
59 * indicates failure. "# skip" and "# todo" are a special cases of a comment,
60 * and must start with exactly that formatting. They indicate the test was
61 * skipped for some reason (maybe because it doesn't apply to this platform)
62 * or is testing something known to currently fail. The text following either
63 * "# skip" or "# todo" and whitespace is the reason.
65 * As a special case, the first line of the output may be in the form:
67 * 1..0 # skip some reason
69 * which indicates that this entire test case should be skipped and gives a
72 * Any other lines are ignored, although for compliance with the TAP protocol
73 * all lines other than the ones in the above format should be sent to
74 * standard error rather than standard output and start with #.
76 * This is a subset of TAP as documented in Test::Harness::TAP or
77 * TAP::Parser::Grammar, which comes with Perl.
79 * If the -o option is given, instead run a single test and display all of its
80 * output. This is intended for use with failing tests so that the person
81 * running the test suite can get more details about what failed.
83 * If built with the C preprocessor symbols C_TAP_SOURCE and C_TAP_BUILD
84 * defined, C TAP Harness will export those values in the environment so that
85 * tests can find the source and build directory and will look for tests under
86 * both directories. These paths can also be set with the -b and -s
87 * command-line options, which will override anything set at build time.
89 * If the -v option is given, or the C_TAP_VERBOSE environment variable is set,
90 * display the full output of each test as it runs rather than showing a
91 * summary of the results of each test.
94 /* Required for fdopen(), getopt(), and putenv(). */
95 #if defined(__STRICT_ANSI__) || defined(PEDANTIC)
96 # ifndef _XOPEN_SOURCE
97 # define _XOPEN_SOURCE 500
111 #include <sys/stat.h>
112 #include <sys/time.h>
113 #include <sys/types.h>
114 #include <sys/wait.h>
118 /* sys/time.h must be included before sys/resource.h on some platforms. */
119 #include <sys/resource.h>
121 /* AIX 6.1 (and possibly later) doesn't have WCOREDUMP. */
123 # define WCOREDUMP(status) ((unsigned) (status) &0x80)
127 * POSIX requires that these be defined in <unistd.h>, but they're not always
128 * available. If one of them has been defined, all the rest almost certainly
132 # define STDIN_FILENO 0
133 # define STDOUT_FILENO 1
134 # define STDERR_FILENO 2
138 * Used for iterating through arrays. Returns the number of elements in the
139 * array (useful for a < upper bound in a for loop).
141 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
144 * The source and build versions of the tests directory. This is used to set
145 * the C_TAP_SOURCE and C_TAP_BUILD environment variables (and the SOURCE and
146 * BUILD environment variables set for backward compatibility) and find test
147 * programs, if set. Normally, this should be set as part of the build
148 * process to the test subdirectories of $(abs_top_srcdir) and
149 * $(abs_top_builddir) respectively.
152 # define C_TAP_SOURCE NULL
155 # define C_TAP_BUILD NULL
158 /* Test status codes. */
166 /* Really, just a boolean, but this is more self-documenting. */
172 /* Indicates the state of our plan. */
174 PLAN_INIT
, /* Nothing seen yet. */
175 PLAN_FIRST
, /* Plan seen before any tests. */
176 PLAN_PENDING
, /* Test seen and no plan yet. */
177 PLAN_FINAL
/* Plan seen after some tests. */
180 /* Error exit statuses for test processes. */
181 #define CHILDERR_DUP 100 /* Couldn't redirect stderr or stdout. */
182 #define CHILDERR_EXEC 101 /* Couldn't exec child process. */
183 #define CHILDERR_STDIN 102 /* Couldn't open stdin file. */
184 #define CHILDERR_STDERR 103 /* Couldn't open stderr file. */
186 /* Structure to hold data for a set of tests. */
188 char *file
; /* The file name of the test. */
189 char **command
; /* The argv vector to run the command. */
190 enum plan_status plan
; /* The status of our plan. */
191 unsigned long count
; /* Expected count of tests. */
192 unsigned long current
; /* The last seen test number. */
193 unsigned int length
; /* The length of the last status message. */
194 unsigned long passed
; /* Count of passing tests. */
195 unsigned long failed
; /* Count of failing lists. */
196 unsigned long skipped
; /* Count of skipped tests (passed). */
197 unsigned long allocated
; /* The size of the results table. */
198 enum test_status
*results
; /* Table of results by test number. */
199 unsigned int aborted
; /* Whether the set was aborted. */
200 unsigned int reported
; /* Whether the results were reported. */
201 int status
; /* The exit status of the test. */
202 unsigned int all_skipped
; /* Whether all tests were skipped. */
203 char *reason
; /* Why all tests were skipped. */
206 /* Structure to hold a linked list of test sets. */
209 struct testlist
*next
;
213 * Usage message. Should be used as a printf format with four arguments: the
214 * path to runtests, given three times, and the usage_description. This is
215 * split into variables to satisfy the pedantic ISO C90 limit on strings.
217 static const char usage_message
[] = "\
218 Usage: %s [-hv] [-b <build-dir>] [-s <source-dir>] <test> ...\n\
219 %s [-hv] [-b <build-dir>] [-s <source-dir>] -l <test-list>\n\
220 %s -o [-h] [-b <build-dir>] [-s <source-dir>] <test>\n\
223 -b <build-dir> Set the build directory to <build-dir>\n\
225 static const char usage_extra
[] = "\
226 -l <list> Take the list of tests to run from <test-list>\n\
227 -o Run a single test rather than a list of tests\n\
228 -s <source-dir> Set the source directory to <source-dir>\n\
229 -v Show the full output of each test\n\
231 runtests normally runs each test listed on the command line. With the -l\n\
232 option, it instead runs every test listed in a file. With the -o option,\n\
233 it instead runs a single test and shows its complete output.\n";
236 * Header used for test output. %s is replaced by the file name of the list
239 static const char banner
[] = "\n\
240 Running all tests listed in %s. If any tests fail, run the failing\n\
241 test program with runtests -o to see more details.\n\n";
243 /* Header for reports of failed tests. */
244 static const char header
[] = "\n\
245 Failed Set Fail/Total (%) Skip Stat Failing Tests\n\
246 -------------------------- -------------- ---- ---- ------------------------";
248 /* Include the file name and line number in malloc failures. */
249 #define xcalloc(n, type) \
250 ((type *) x_calloc((n), sizeof(type), __FILE__, __LINE__))
251 #define xmalloc(size) ((char *) x_malloc((size), __FILE__, __LINE__))
252 #define xstrdup(p) x_strdup((p), __FILE__, __LINE__)
253 #define xstrndup(p, size) x_strndup((p), (size), __FILE__, __LINE__)
254 #define xreallocarray(p, n, type) \
255 ((type *) x_reallocarray((p), (n), sizeof(type), __FILE__, __LINE__))
258 * __attribute__ is available in gcc 2.5 and later, but only with gcc 2.7
259 * could you use the __format__ form of the attributes, which is what we use
260 * (to avoid confusion with other macros).
262 #ifndef __attribute__
263 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
264 # define __attribute__(spec) /* empty */
269 * We use __alloc_size__, but it was only available in fairly recent versions
270 * of GCC. Suppress warnings about the unknown attribute if GCC is too old.
271 * We know that we're GCC at this point, so we can use the GCC variadic macro
272 * extension, which will still work with versions of GCC too old to have C99
273 * variadic macro support.
275 #if !defined(__attribute__) && !defined(__alloc_size__)
276 # if defined(__GNUC__) && !defined(__clang__)
277 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
278 # define __alloc_size__(spec, args...) /* empty */
284 * Suppress the argument to __malloc__ in Clang (not supported in at least
285 * version 13) and GCC versions prior to 11.
287 #if !defined(__attribute__) && !defined(__malloc__)
288 # if defined(__clang__) || __GNUC__ < 11
289 # define __malloc__(dalloc) __malloc__
294 * LLVM and Clang pretend to be GCC but don't support all of the __attribute__
295 * settings that GCC does. For them, suppress warnings about unknown
296 * attributes on declarations. This unfortunately will affect the entire
297 * compilation context, but there's no push and pop available.
299 #if !defined(__attribute__) && (defined(__llvm__) || defined(__clang__))
300 # pragma GCC diagnostic ignored "-Wattributes"
303 /* Declare internal functions that benefit from compiler attributes. */
304 static void die(const char *, ...)
305 __attribute__((__nonnull__
, __noreturn__
, __format__(printf
, 1, 2)));
306 static void sysdie(const char *, ...)
307 __attribute__((__nonnull__
, __noreturn__
, __format__(printf
, 1, 2)));
308 static void *x_calloc(size_t, size_t, const char *, int)
309 __attribute__((__alloc_size__(1, 2), __malloc__(free
), __nonnull__
));
310 static void *x_malloc(size_t, const char *, int)
311 __attribute__((__alloc_size__(1), __malloc__(free
), __nonnull__
));
312 static void *x_reallocarray(void *, size_t, size_t, const char *, int)
313 __attribute__((__alloc_size__(2, 3), __malloc__(free
), __nonnull__(4)));
314 static char *x_strdup(const char *, const char *, int)
315 __attribute__((__malloc__(free
), __nonnull__
));
316 static char *x_strndup(const char *, size_t, const char *, int)
317 __attribute__((__malloc__(free
), __nonnull__
));
321 * Report a fatal error and exit.
324 die(const char *format
, ...)
329 fprintf(stderr
, "runtests: ");
330 va_start(args
, format
);
331 vfprintf(stderr
, format
, args
);
333 fprintf(stderr
, "\n");
339 * Report a fatal error, including the results of strerror, and exit.
342 sysdie(const char *format
, ...)
349 fprintf(stderr
, "runtests: ");
350 va_start(args
, format
);
351 vfprintf(stderr
, format
, args
);
353 fprintf(stderr
, ": %s\n", strerror(oerrno
));
359 * Allocate zeroed memory, reporting a fatal error and exiting on failure.
362 x_calloc(size_t n
, size_t size
, const char *file
, int line
)
367 size
= (size
> 0) ? size
: 1;
370 sysdie("failed to calloc %lu bytes at %s line %d",
371 (unsigned long) size
, file
, line
);
377 * Allocate memory, reporting a fatal error and exiting on failure.
380 x_malloc(size_t size
, const char *file
, int line
)
386 sysdie("failed to malloc %lu bytes at %s line %d",
387 (unsigned long) size
, file
, line
);
393 * Reallocate memory, reporting a fatal error and exiting on failure.
395 * We should technically use SIZE_MAX here for the overflow check, but
396 * SIZE_MAX is C99 and we're only assuming C89 + SUSv3, which does not
397 * guarantee that it exists. They do guarantee that UINT_MAX exists, and we
398 * can assume that UINT_MAX <= SIZE_MAX. And we should not be allocating
399 * anything anywhere near that large.
401 * (In theory, C89 and C99 permit size_t to be smaller than unsigned int, but
402 * I disbelieve in the existence of such systems and they will have to cope
403 * without overflow checks.)
406 x_reallocarray(void *p
, size_t n
, size_t size
, const char *file
, int line
)
409 size
= (size
> 0) ? size
: 1;
411 if (UINT_MAX
/ n
<= size
)
412 sysdie("realloc too large at %s line %d", file
, line
);
413 p
= realloc(p
, n
* size
);
415 sysdie("failed to realloc %lu bytes at %s line %d",
416 (unsigned long) (n
* size
), file
, line
);
422 * Copy a string, reporting a fatal error and exiting on failure.
425 x_strdup(const char *s
, const char *file
, int line
)
431 p
= (char *) malloc(len
);
433 sysdie("failed to strdup %lu bytes at %s line %d", (unsigned long) len
,
441 * Copy the first n characters of a string, reporting a fatal error and
442 * existing on failure.
444 * Avoid using the system strndup function since it may not exist (on Mac OS
445 * X, for example), and there's no need to introduce another portability
449 x_strndup(const char *s
, size_t size
, const char *file
, int line
)
455 /* Don't assume that the source string is nul-terminated. */
456 for (p
= s
; (size_t) (p
- s
) < size
&& *p
!= '\0'; p
++)
458 len
= (size_t) (p
- s
);
459 copy
= (char *) malloc(len
+ 1);
461 sysdie("failed to strndup %lu bytes at %s line %d",
462 (unsigned long) len
, file
, line
);
463 memcpy(copy
, s
, len
);
470 * Form a new string by concatenating multiple strings. The arguments must be
471 * terminated by (const char *) 0.
473 * This function only exists because we can't assume asprintf. We can't
474 * simulate asprintf with snprintf because we're only assuming SUSv3, which
475 * does not require that snprintf with a NULL buffer return the required
476 * length. When those constraints are relaxed, this should be ripped out and
477 * replaced with asprintf or a more trivial replacement with snprintf.
480 concat(const char *first
, ...)
489 * Find the total memory required. Ensure we don't overflow length. We
490 * aren't guaranteed to have SIZE_MAX, so use UINT_MAX as an acceptable
491 * substitute (see the x_nrealloc comments).
493 va_start(args
, first
);
494 for (string
= first
; string
!= NULL
; string
= va_arg(args
, const char *)) {
495 if (length
>= UINT_MAX
- strlen(string
)) {
497 sysdie("strings too long in concat");
499 length
+= strlen(string
);
504 /* Create the string. */
505 result
= xmalloc(length
);
506 va_start(args
, first
);
508 for (string
= first
; string
!= NULL
; string
= va_arg(args
, const char *)) {
509 memcpy(result
+ offset
, string
, strlen(string
));
510 offset
+= strlen(string
);
513 result
[offset
] = '\0';
519 * Given a struct timeval, return the number of seconds it represents as a
520 * double. Use difftime() to convert a time_t to a double.
523 tv_seconds(const struct timeval
*tv
)
525 return difftime(tv
->tv_sec
, 0) + (double) tv
->tv_usec
* 1e-6;
530 * Given two struct timevals, return the difference in seconds.
533 tv_diff(const struct timeval
*tv1
, const struct timeval
*tv0
)
535 return tv_seconds(tv1
) - tv_seconds(tv0
);
540 * Given two struct timevals, return the sum in seconds as a double.
543 tv_sum(const struct timeval
*tv1
, const struct timeval
*tv2
)
545 return tv_seconds(tv1
) + tv_seconds(tv2
);
550 * Given a pointer to a string, skip any leading whitespace and return a
551 * pointer to the first non-whitespace character.
554 skip_whitespace(const char *p
)
556 while (isspace((unsigned char) (*p
)))
563 * Given a pointer to a string, skip any non-whitespace characters and return
564 * a pointer to the first whitespace character, or to the end of the string.
567 skip_non_whitespace(const char *p
)
569 while (*p
!= '\0' && !isspace((unsigned char) (*p
)))
576 * Start a program, connecting its stdout to a pipe on our end and its stderr
577 * to /dev/null, and storing the file descriptor to read from in the two
578 * argument. Returns the PID of the new process. Errors are fatal.
581 test_start(char *const *command
, int *fd
)
583 int fds
[2], infd
, errfd
;
586 /* Create a pipe used to capture the output from the test program. */
587 if (pipe(fds
) == -1) {
590 sysdie("can't create pipe");
593 /* Fork a child process, massage the file descriptors, and exec. */
599 sysdie("can't fork");
601 /* In the child. Set up our standard output. */
604 close(STDOUT_FILENO
);
605 if (dup2(fds
[1], STDOUT_FILENO
) < 0)
609 /* Point standard input at /dev/null. */
611 infd
= open("/dev/null", O_RDONLY
);
613 _exit(CHILDERR_STDIN
);
614 if (infd
!= STDIN_FILENO
) {
615 if (dup2(infd
, STDIN_FILENO
) < 0)
620 /* Point standard error at /dev/null. */
621 close(STDERR_FILENO
);
622 errfd
= open("/dev/null", O_WRONLY
);
624 _exit(CHILDERR_STDERR
);
625 if (errfd
!= STDERR_FILENO
) {
626 if (dup2(errfd
, STDERR_FILENO
) < 0)
631 /* Now, exec our process. */
632 if (execv(command
[0], command
) == -1)
633 _exit(CHILDERR_EXEC
);
636 /* In parent. Close the extra file descriptor. */
647 * Back up over the output saying what test we were executing.
650 test_backspace(struct testset
*ts
)
654 if (!isatty(STDOUT_FILENO
))
656 for (i
= 0; i
< ts
->length
; i
++)
658 for (i
= 0; i
< ts
->length
; i
++)
660 for (i
= 0; i
< ts
->length
; i
++)
667 * Allocate or resize the array of test results to be large enough to contain
668 * the test number in.
671 resize_results(struct testset
*ts
, unsigned long n
)
676 /* If there's already enough space, return quickly. */
677 if (n
<= ts
->allocated
)
681 * If no space has been allocated, do the initial allocation. Otherwise,
682 * resize. Start with 32 test cases and then add 1024 with each resize to
683 * try to reduce the number of reallocations.
685 if (ts
->allocated
== 0) {
686 s
= (n
> 32) ? n
: 32;
687 ts
->results
= xcalloc(s
, enum test_status
);
689 s
= (n
> ts
->allocated
+ 1024) ? n
: ts
->allocated
+ 1024;
690 ts
->results
= xreallocarray(ts
->results
, s
, enum test_status
);
693 /* Set the results for the newly-allocated test array. */
694 for (i
= ts
->allocated
; i
< s
; i
++)
695 ts
->results
[i
] = TEST_INVALID
;
701 * Report an invalid test number and set the appropriate flags. Pulled into a
702 * separate function since we do this in several places.
705 invalid_test_number(struct testset
*ts
, long n
, enum test_verbose verbose
)
709 printf("ABORTED (invalid test number %ld)\n", n
);
716 * Read the plan line of test output, which should contain the range of test
717 * numbers. We may initialize the testset structure here if we haven't yet
718 * seen a test. Return true if initialization succeeded and the test should
719 * continue, false otherwise.
722 test_plan(const char *line
, struct testset
*ts
, enum test_verbose verbose
)
727 * Accept a plan without the leading 1.. for compatibility with older
728 * versions of runtests. This will only be allowed if we've not yet seen
731 line
= skip_whitespace(line
);
732 if (strncmp(line
, "1..", 3) == 0)
736 * Get the count and check it for validity.
738 * If we have something of the form "1..0 # skip foo", the whole file was
739 * skipped; record that. If we do skip the whole file, zero out all of
740 * our statistics, since they're no longer relevant.
742 * strtol is called with a second argument to advance the line pointer
743 * past the count to make it simpler to detect the # skip case.
745 n
= strtol(line
, (char **) &line
, 10);
747 line
= skip_whitespace(line
);
749 line
= skip_whitespace(line
+ 1);
750 if (strncasecmp(line
, "skip", 4) == 0) {
751 line
= skip_whitespace(line
+ 4);
753 ts
->reason
= xstrdup(line
);
754 ts
->reason
[strlen(ts
->reason
) - 1] = '\0';
767 puts("ABORTED (invalid test count)");
774 * If we are doing lazy planning, check the plan against the largest test
775 * number that we saw and fail now if we saw a check outside the plan
778 if (ts
->plan
== PLAN_PENDING
&& (unsigned long) n
< ts
->count
) {
779 invalid_test_number(ts
, (long) ts
->count
, verbose
);
784 * Otherwise, allocated or resize the results if needed and update count,
785 * and then record that we've seen a plan.
787 resize_results(ts
, (unsigned long) n
);
788 ts
->count
= (unsigned long) n
;
789 if (ts
->plan
== PLAN_INIT
)
790 ts
->plan
= PLAN_FIRST
;
791 else if (ts
->plan
== PLAN_PENDING
)
792 ts
->plan
= PLAN_FINAL
;
798 * Given a single line of output from a test, parse it and return the success
799 * status of that test. Anything printed to stdout not matching the form
800 * /^(not )?ok \d+/ is ignored. Sets ts->current to the test number that just
804 test_checkline(const char *line
, struct testset
*ts
, enum test_verbose verbose
)
806 enum test_status status
= TEST_PASS
;
810 unsigned long current
;
813 /* Before anything, check for a test abort. */
814 bail
= strstr(line
, "Bail out!");
816 bail
= skip_whitespace(bail
+ strlen("Bail out!"));
820 length
= strlen(bail
);
821 if (bail
[length
- 1] == '\n')
825 printf("ABORTED (%.*s)\n", (int) length
, bail
);
833 * If the given line isn't newline-terminated, it was too big for an
834 * fgets(), which means ignore it.
836 if (line
[strlen(line
) - 1] != '\n')
839 /* If the line begins with a hash mark, ignore it. */
843 /* If we haven't yet seen a plan, look for one. */
844 if (ts
->plan
== PLAN_INIT
&& isdigit((unsigned char) (*line
))) {
845 if (!test_plan(line
, ts
, verbose
))
847 } else if (strncmp(line
, "1..", 3) == 0) {
848 if (ts
->plan
== PLAN_PENDING
) {
849 if (!test_plan(line
, ts
, verbose
))
854 puts("ABORTED (multiple plans)");
861 /* Parse the line, ignoring something we can't parse. */
862 if (strncmp(line
, "not ", 4) == 0) {
866 if (strncmp(line
, "ok", 2) != 0)
868 line
= skip_whitespace(line
+ 2);
870 number
= strtol(line
, &end
, 10);
871 if (errno
!= 0 || end
== line
)
872 current
= ts
->current
+ 1;
873 else if (number
<= 0) {
874 invalid_test_number(ts
, number
, verbose
);
877 current
= (unsigned long) number
;
878 if (current
> ts
->count
&& ts
->plan
== PLAN_FIRST
) {
879 invalid_test_number(ts
, (long) current
, verbose
);
883 /* We have a valid test result. Tweak the results array if needed. */
884 if (ts
->plan
== PLAN_INIT
|| ts
->plan
== PLAN_PENDING
) {
885 ts
->plan
= PLAN_PENDING
;
886 resize_results(ts
, current
);
887 if (current
> ts
->count
)
892 * Handle directives. We should probably do something more interesting
893 * with unexpected passes of todo tests.
895 while (isdigit((unsigned char) (*line
)))
897 line
= skip_whitespace(line
);
899 line
= skip_whitespace(line
+ 1);
900 if (strncasecmp(line
, "skip", 4) == 0)
902 if (strncasecmp(line
, "todo", 4) == 0)
903 status
= (status
== TEST_FAIL
) ? TEST_SKIP
: TEST_FAIL
;
906 /* Make sure that the test number is in range and not a duplicate. */
907 if (ts
->results
[current
- 1] != TEST_INVALID
) {
910 printf("ABORTED (duplicate test number %lu)\n", current
);
916 /* Good results. Increment our various counters. */
930 ts
->current
= current
;
931 ts
->results
[current
- 1] = status
;
932 if (!verbose
&& isatty(STDOUT_FILENO
)) {
934 if (ts
->plan
== PLAN_PENDING
)
935 outlen
= printf("%lu/?", current
);
937 outlen
= printf("%lu/%lu", current
, ts
->count
);
938 ts
->length
= (outlen
>= 0) ? (unsigned int) outlen
: 0;
945 * Print out a range of test numbers, returning the number of characters it
946 * took up. Takes the first number, the last number, the number of characters
947 * already printed on the line, and the limit of number of characters the line
948 * can hold. Add a comma and a space before the range if chars indicates that
949 * something has already been printed on the line, and print ... instead if
950 * chars plus the space needed would go over the limit (use a limit of 0 to
954 test_print_range(unsigned long first
, unsigned long last
, unsigned long chars
,
957 unsigned int needed
= 0;
960 for (n
= first
; n
> 0; n
/= 10)
963 for (n
= last
; n
> 0; n
/= 10)
969 if (limit
> 0 && chars
+ needed
> limit
) {
971 if (chars
<= limit
) {
983 printf("%lu-", first
);
991 * Summarize a single test set. The second argument is 0 if the set exited
992 * cleanly, a positive integer representing the exit status if it exited
993 * with a non-zero status, and a negative integer representing the signal
994 * that terminated it if it was killed by a signal.
997 test_summarize(struct testset
*ts
, int status
)
1000 unsigned long missing
= 0;
1001 unsigned long failed
= 0;
1002 unsigned long first
= 0;
1003 unsigned long last
= 0;
1006 fputs("ABORTED", stdout
);
1008 printf(" (passed %lu/%lu)", ts
->passed
, ts
->count
- ts
->skipped
);
1010 for (i
= 0; i
< ts
->count
; i
++) {
1011 if (ts
->results
[i
] == TEST_INVALID
) {
1013 fputs("MISSED ", stdout
);
1014 if (first
&& i
== last
)
1018 test_print_range(first
, last
, missing
- 1, 0);
1026 test_print_range(first
, last
, missing
- 1, 0);
1029 for (i
= 0; i
< ts
->count
; i
++) {
1030 if (ts
->results
[i
] == TEST_FAIL
) {
1031 if (missing
&& !failed
)
1032 fputs("; ", stdout
);
1034 fputs("FAILED ", stdout
);
1035 if (first
&& i
== last
)
1039 test_print_range(first
, last
, failed
- 1, 0);
1047 test_print_range(first
, last
, failed
- 1, 0);
1048 if (!missing
&& !failed
) {
1049 fputs(!status
? "ok" : "dubious", stdout
);
1050 if (ts
->skipped
> 0) {
1051 if (ts
->skipped
== 1)
1052 printf(" (skipped %lu test)", ts
->skipped
);
1054 printf(" (skipped %lu tests)", ts
->skipped
);
1059 printf(" (exit status %d)", status
);
1060 else if (status
< 0)
1061 printf(" (killed by signal %d%s)", -status
,
1062 WCOREDUMP(ts
->status
) ? ", core dumped" : "");
1068 * Given a test set, analyze the results, classify the exit status, handle a
1069 * few special error messages, and then pass it along to test_summarize() for
1070 * the regular output. Returns true if the test set ran successfully and all
1071 * tests passed or were skipped, false otherwise.
1074 test_analyze(struct testset
*ts
)
1078 if (ts
->all_skipped
) {
1079 if (ts
->reason
== NULL
)
1082 printf("skipped (%s)\n", ts
->reason
);
1084 } else if (WIFEXITED(ts
->status
) && WEXITSTATUS(ts
->status
) != 0) {
1085 switch (WEXITSTATUS(ts
->status
)) {
1088 puts("ABORTED (can't dup file descriptors)");
1092 puts("ABORTED (execution failed -- not found?)");
1094 case CHILDERR_STDIN
:
1095 case CHILDERR_STDERR
:
1097 puts("ABORTED (can't open /dev/null)");
1100 test_summarize(ts
, WEXITSTATUS(ts
->status
));
1104 } else if (WIFSIGNALED(ts
->status
)) {
1105 test_summarize(ts
, -WTERMSIG(ts
->status
));
1107 } else if (ts
->plan
!= PLAN_FIRST
&& ts
->plan
!= PLAN_FINAL
) {
1108 puts("ABORTED (no valid test plan)");
1112 test_summarize(ts
, 0);
1113 return (ts
->failed
== 0);
1119 * Runs a single test set, accumulating and then reporting the results.
1120 * Returns true if the test set was successfully run and all tests passed,
1124 test_run(struct testset
*ts
, enum test_verbose verbose
)
1126 pid_t testpid
, child
;
1130 char buffer
[BUFSIZ
];
1132 /* Run the test program. */
1133 testpid
= test_start(ts
->command
, &outfd
);
1134 output
= fdopen(outfd
, "r");
1138 sysdie("fdopen failed");
1142 * Pass each line of output to test_checkline(), and print the line if
1143 * verbosity is requested.
1145 while (!ts
->aborted
&& fgets(buffer
, sizeof(buffer
), output
)) {
1147 printf("%s", buffer
);
1148 test_checkline(buffer
, ts
, verbose
);
1150 if (ferror(output
) || ts
->plan
== PLAN_INIT
)
1156 * Consume the rest of the test output, close the output descriptor,
1157 * retrieve the exit status, and pass that information to test_analyze()
1158 * for eventual output.
1160 while (fgets(buffer
, sizeof(buffer
), output
))
1162 printf("%s", buffer
);
1164 child
= waitpid(testpid
, &ts
->status
, 0);
1165 if (child
== (pid_t
) -1) {
1166 if (!ts
->reported
) {
1170 sysdie("waitpid for %u failed", (unsigned int) testpid
);
1172 if (ts
->all_skipped
)
1174 status
= test_analyze(ts
);
1176 /* Convert missing tests to failed tests. */
1177 for (i
= 0; i
< ts
->count
; i
++) {
1178 if (ts
->results
[i
] == TEST_INVALID
) {
1180 ts
->results
[i
] = TEST_FAIL
;
1188 /* Summarize a list of test failures. */
1190 test_fail_summary(const struct testlist
*fails
)
1194 unsigned long i
, first
, last
, total
;
1199 /* Failed Set Fail/Total (%) Skip Stat Failing (25)
1200 -------------------------- -------------- ---- ---- -------------- */
1201 for (; fails
; fails
= fails
->next
) {
1203 total
= ts
->count
- ts
->skipped
;
1204 failed
= (double) ts
->failed
;
1205 printf("%-26.26s %4lu/%-4lu %3.0f%% %4lu ", ts
->file
, ts
->failed
,
1206 total
, total
? (failed
* 100.0) / (double) total
: 0,
1208 if (WIFEXITED(ts
->status
))
1209 printf("%4d ", WEXITSTATUS(ts
->status
));
1219 for (i
= 0; i
< ts
->count
; i
++) {
1220 if (ts
->results
[i
] == TEST_FAIL
) {
1221 if (first
!= 0 && i
== last
)
1225 chars
+= test_print_range(first
, last
, chars
, 19);
1232 test_print_range(first
, last
, chars
, 19);
1239 * Check whether a given file path is a valid test. Currently, this checks
1240 * whether it is executable and is a regular file. Returns true or false.
1243 is_valid_test(const char *path
)
1247 if (access(path
, X_OK
) < 0)
1249 if (stat(path
, &st
) < 0)
1251 if (!S_ISREG(st
.st_mode
))
1258 * Given the name of a test, a pointer to the testset struct, and the source
1259 * and build directories, find the test. We try first relative to the current
1260 * directory, then in the build directory (if not NULL), then in the source
1261 * directory. In each of those directories, we first try a "-t" extension and
1262 * then a ".t" extension. When we find an executable program, we return the
1263 * path to that program. If none of those paths are executable, just fill in
1264 * the name of the test as is.
1266 * The caller is responsible for freeing the path member of the testset
1270 find_test(const char *name
, const char *source
, const char *build
)
1273 const char *bases
[3], *suffix
, *base
;
1275 const char *suffixes
[3] = {"-t", ".t", ""};
1277 /* Possible base directories. */
1282 /* Try each suffix with each base. */
1283 for (i
= 0; i
< ARRAY_SIZE(suffixes
); i
++) {
1284 suffix
= suffixes
[i
];
1285 for (j
= 0; j
< ARRAY_SIZE(bases
); j
++) {
1289 path
= concat(base
, "/", name
, suffix
, (const char *) 0);
1290 if (is_valid_test(path
))
1297 path
= xstrdup(name
);
1303 * Parse a single line of a test list and store the test name and command to
1304 * execute it in the given testset struct.
1306 * Normally, each line is just the name of the test, which is located in the
1307 * test directory and turned into a command to run. However, each line may
1308 * have whitespace-separated options, which change the command that's run.
1309 * Current supported options are:
1312 * Run the test under valgrind if C_TAP_VALGRIND is set. The contents
1313 * of that environment variable are taken as the valgrind command (with
1314 * options) to run. The command is parsed with a simple split on
1315 * whitespace and no quoting is supported.
1318 * If running under valgrind, use libtool to invoke valgrind. This avoids
1319 * running valgrind on the wrapper shell script generated by libtool. If
1320 * set, C_TAP_LIBTOOL must be set to the full path to the libtool program
1321 * to use to run valgrind and thus the test. Ignored if the test isn't
1322 * being run under valgrind.
1325 parse_test_list_line(const char *line
, struct testset
*ts
, const char *source
,
1328 const char *p
, *end
, *option
, *libtool
;
1329 const char *valgrind
= NULL
;
1330 unsigned int use_libtool
= 0;
1331 unsigned int use_valgrind
= 0;
1334 /* Determine the name of the test. */
1335 p
= skip_non_whitespace(line
);
1336 ts
->file
= xstrndup(line
, p
- line
);
1338 /* Check if any test options are set. */
1339 p
= skip_whitespace(p
);
1340 while (*p
!= '\0') {
1341 end
= skip_non_whitespace(p
);
1342 if (strncmp(p
, "libtool", end
- p
) == 0) {
1344 } else if (strncmp(p
, "valgrind", end
- p
) == 0) {
1345 valgrind
= getenv("C_TAP_VALGRIND");
1346 use_valgrind
= (valgrind
!= NULL
);
1348 option
= xstrndup(p
, end
- p
);
1349 die("unknown test list option %s", option
);
1351 p
= skip_whitespace(end
);
1354 /* Construct the argv to run the test. First, find the length. */
1356 if (use_valgrind
&& valgrind
!= NULL
) {
1357 p
= skip_whitespace(valgrind
);
1358 while (*p
!= '\0') {
1360 p
= skip_whitespace(skip_non_whitespace(p
));
1366 /* Now, build the command. */
1367 ts
->command
= xcalloc(len
+ 1, char *);
1369 if (use_valgrind
&& valgrind
!= NULL
) {
1371 libtool
= getenv("C_TAP_LIBTOOL");
1372 if (libtool
== NULL
)
1373 die("valgrind with libtool requested, but C_TAP_LIBTOOL is not"
1375 ts
->command
[i
++] = xstrdup(libtool
);
1376 ts
->command
[i
++] = xstrdup("--mode=execute");
1378 p
= skip_whitespace(valgrind
);
1379 while (*p
!= '\0') {
1380 end
= skip_non_whitespace(p
);
1381 ts
->command
[i
++] = xstrndup(p
, end
- p
);
1382 p
= skip_whitespace(end
);
1386 die("internal error while constructing command line");
1387 ts
->command
[i
++] = find_test(ts
->file
, source
, build
);
1388 ts
->command
[i
] = NULL
;
1393 * Read a list of tests from a file, returning the list of tests as a struct
1394 * testlist, or NULL if there were no tests (such as a file containing only
1395 * comments). Reports an error to standard error and exits if the list of
1396 * tests cannot be read.
1398 static struct testlist
*
1399 read_test_list(const char *filename
, const char *source
, const char *build
)
1404 char buffer
[BUFSIZ
];
1406 struct testlist
*listhead
, *current
;
1408 /* Create the initial container list that will hold our results. */
1409 listhead
= xcalloc(1, struct testlist
);
1413 * Open our file of tests to run and read it line by line, creating a new
1414 * struct testlist and struct testset for each line.
1416 file
= fopen(filename
, "r");
1418 sysdie("can't open %s", filename
);
1420 while (fgets(buffer
, sizeof(buffer
), file
)) {
1422 length
= strlen(buffer
) - 1;
1423 if (buffer
[length
] != '\n') {
1424 fprintf(stderr
, "%s:%u: line too long\n", filename
, line
);
1427 buffer
[length
] = '\0';
1429 /* Skip comments, leading spaces, and blank lines. */
1430 start
= skip_whitespace(buffer
);
1431 if (strlen(start
) == 0)
1433 if (start
[0] == '#')
1436 /* Allocate the new testset structure. */
1437 if (current
== NULL
)
1440 current
->next
= xcalloc(1, struct testlist
);
1441 current
= current
->next
;
1443 current
->ts
= xcalloc(1, struct testset
);
1444 current
->ts
->plan
= PLAN_INIT
;
1446 /* Parse the line and store the results in the testset struct. */
1447 parse_test_list_line(start
, current
->ts
, source
, build
);
1451 /* If there were no tests, current is still NULL. */
1452 if (current
== NULL
) {
1457 /* Return the results. */
1463 * Build a list of tests from command line arguments. Takes the argv and argc
1464 * representing the command line arguments and returns a newly allocated test
1465 * list, or NULL if there were no tests. The caller is responsible for
1468 static struct testlist
*
1469 build_test_list(char *argv
[], int argc
, const char *source
, const char *build
)
1472 struct testlist
*listhead
, *current
;
1474 /* Create the initial container list that will hold our results. */
1475 listhead
= xcalloc(1, struct testlist
);
1478 /* Walk the list of arguments and create test sets for them. */
1479 for (i
= 0; i
< argc
; i
++) {
1480 if (current
== NULL
)
1483 current
->next
= xcalloc(1, struct testlist
);
1484 current
= current
->next
;
1486 current
->ts
= xcalloc(1, struct testset
);
1487 current
->ts
->plan
= PLAN_INIT
;
1488 current
->ts
->file
= xstrdup(argv
[i
]);
1489 current
->ts
->command
= xcalloc(2, char *);
1490 current
->ts
->command
[0] = find_test(current
->ts
->file
, source
, build
);
1491 current
->ts
->command
[1] = NULL
;
1494 /* If there were no tests, current is still NULL. */
1495 if (current
== NULL
) {
1500 /* Return the results. */
1505 /* Free a struct testset. */
1507 free_testset(struct testset
*ts
)
1512 for (i
= 0; ts
->command
[i
] != NULL
; i
++)
1513 free(ts
->command
[i
]);
1522 * Run a batch of tests. Takes two additional parameters: the root of the
1523 * source directory and the root of the build directory. Test programs will
1524 * be first searched for in the current directory, then the build directory,
1525 * then the source directory. Returns true iff all tests passed, and always
1526 * frees the test list that's passed in.
1529 test_batch(struct testlist
*tests
, enum test_verbose verbose
)
1533 unsigned int count
= 0;
1535 struct timeval start
, end
;
1536 struct rusage stats
;
1537 struct testlist
*failhead
= NULL
;
1538 struct testlist
*failtail
= NULL
;
1539 struct testlist
*current
, *next
;
1541 unsigned long total
= 0;
1542 unsigned long passed
= 0;
1543 unsigned long skipped
= 0;
1544 unsigned long failed
= 0;
1545 unsigned long aborted
= 0;
1547 /* Walk the list of tests to find the longest name. */
1548 for (current
= tests
; current
!= NULL
; current
= current
->next
) {
1549 length
= strlen(current
->ts
->file
);
1550 if (length
> longest
)
1555 * Add two to longest and round up to the nearest tab stop. This is how
1556 * wide the column for printing the current test name will be.
1560 longest
+= 8 - (longest
% 8);
1562 /* Start the wall clock timer. */
1563 gettimeofday(&start
, NULL
);
1565 /* Now, plow through our tests again, running each one. */
1566 for (current
= tests
; current
!= NULL
; current
= current
->next
) {
1569 /* Print out the name of the test file. */
1570 fputs(ts
->file
, stdout
);
1572 fputs("\n\n", stdout
);
1574 for (i
= strlen(ts
->file
); i
< longest
; i
++)
1576 if (isatty(STDOUT_FILENO
))
1580 succeeded
= test_run(ts
, verbose
);
1585 /* Record cumulative statistics. */
1586 aborted
+= ts
->aborted
;
1587 total
+= ts
->count
+ ts
->all_skipped
;
1588 passed
+= ts
->passed
;
1589 skipped
+= ts
->skipped
+ ts
->all_skipped
;
1590 failed
+= ts
->failed
;
1593 /* If the test fails, we shuffle it over to the fail list. */
1595 if (failhead
== NULL
) {
1596 failhead
= xcalloc(1, struct testlist
);
1597 failtail
= failhead
;
1599 failtail
->next
= xcalloc(1, struct testlist
);
1600 failtail
= failtail
->next
;
1603 failtail
->next
= NULL
;
1608 /* Stop the timer and get our child resource statistics. */
1609 gettimeofday(&end
, NULL
);
1610 getrusage(RUSAGE_CHILDREN
, &stats
);
1612 /* Summarize the failures and free the failure list. */
1613 if (failhead
!= NULL
) {
1614 test_fail_summary(failhead
);
1615 while (failhead
!= NULL
) {
1616 next
= failhead
->next
;
1622 /* Free the memory used by the test lists. */
1623 while (tests
!= NULL
) {
1625 free_testset(tests
->ts
);
1630 /* Print out the final test summary. */
1634 printf("Aborted %lu test set", aborted
);
1636 printf("Aborted %lu test sets", aborted
);
1637 printf(", passed %lu/%lu tests", passed
, total
);
1638 } else if (failed
== 0)
1639 fputs("All tests successful", stdout
);
1641 printf("Failed %lu/%lu tests, %.2f%% okay", failed
, total
,
1642 (double) (total
- failed
) * 100.0 / (double) total
);
1645 printf(", %lu test skipped", skipped
);
1647 printf(", %lu tests skipped", skipped
);
1650 printf("Files=%u, Tests=%lu", count
, total
);
1651 printf(", %.2f seconds", tv_diff(&end
, &start
));
1652 printf(" (%.2f usr + %.2f sys = %.2f CPU)\n", tv_seconds(&stats
.ru_utime
),
1653 tv_seconds(&stats
.ru_stime
),
1654 tv_sum(&stats
.ru_utime
, &stats
.ru_stime
));
1655 return (failed
== 0 && aborted
== 0);
1660 * Run a single test case. This involves just running the test program after
1661 * having done the environment setup and finding the test program.
1664 test_single(const char *program
, const char *source
, const char *build
)
1668 path
= find_test(program
, source
, build
);
1669 if (execl(path
, path
, (char *) 0) == -1)
1670 sysdie("cannot exec %s", path
);
1675 * Main routine. Set the C_TAP_SOURCE, C_TAP_BUILD, SOURCE, and BUILD
1676 * environment variables and then, given a file listing tests, run each test
1680 main(int argc
, char *argv
[])
1685 enum test_verbose verbose
= CONCISE
;
1686 char *c_tap_source_env
= NULL
;
1687 char *c_tap_build_env
= NULL
;
1688 char *source_env
= NULL
;
1689 char *build_env
= NULL
;
1690 const char *program
;
1691 const char *shortlist
;
1692 const char *list
= NULL
;
1693 const char *source
= C_TAP_SOURCE
;
1694 const char *build
= C_TAP_BUILD
;
1695 struct testlist
*tests
;
1698 while ((option
= getopt(argc
, argv
, "b:hl:os:v")) != EOF
) {
1704 printf(usage_message
, program
, program
, program
, usage_extra
);
1724 if ((list
== NULL
&& argc
< 1) || (list
!= NULL
&& argc
> 0)) {
1725 fprintf(stderr
, usage_message
, program
, program
, program
, usage_extra
);
1730 * If C_TAP_VERBOSE is set in the environment, that also turns on verbose
1733 if (getenv("C_TAP_VERBOSE") != NULL
)
1737 * Set C_TAP_SOURCE and C_TAP_BUILD environment variables. Also set
1738 * SOURCE and BUILD for backward compatibility, although we're trying to
1739 * migrate to the ones with a C_TAP_* prefix.
1741 if (source
!= NULL
) {
1742 c_tap_source_env
= concat("C_TAP_SOURCE=", source
, (const char *) 0);
1743 if (putenv(c_tap_source_env
) != 0)
1744 sysdie("cannot set C_TAP_SOURCE in the environment");
1745 source_env
= concat("SOURCE=", source
, (const char *) 0);
1746 if (putenv(source_env
) != 0)
1747 sysdie("cannot set SOURCE in the environment");
1749 if (build
!= NULL
) {
1750 c_tap_build_env
= concat("C_TAP_BUILD=", build
, (const char *) 0);
1751 if (putenv(c_tap_build_env
) != 0)
1752 sysdie("cannot set C_TAP_BUILD in the environment");
1753 build_env
= concat("BUILD=", build
, (const char *) 0);
1754 if (putenv(build_env
) != 0)
1755 sysdie("cannot set BUILD in the environment");
1758 /* Run the tests as instructed. */
1760 test_single(argv
[0], source
, build
);
1761 else if (list
!= NULL
) {
1762 shortlist
= strrchr(list
, '/');
1763 if (shortlist
== NULL
)
1767 printf(banner
, shortlist
);
1768 tests
= read_test_list(list
, source
, build
);
1769 status
= test_batch(tests
, verbose
) ? 0 : 1;
1771 tests
= build_test_list(argv
, argc
, source
, build
);
1772 status
= test_batch(tests
, verbose
) ? 0 : 1;
1775 /* For valgrind cleanliness, free all our memory. */
1776 if (source_env
!= NULL
) {
1777 putenv((char *) "C_TAP_SOURCE=");
1778 putenv((char *) "SOURCE=");
1779 free(c_tap_source_env
);
1782 if (build_env
!= NULL
) {
1783 putenv((char *) "C_TAP_BUILD=");
1784 putenv((char *) "BUILD=");
1785 free(c_tap_build_env
);