| 1 |
/* |
| 2 |
* Run a set of tests, reporting results. |
| 3 |
* |
| 4 |
* Test suite driver that runs a set of tests implementing a subset of the |
| 5 |
* Test Anything Protocol (TAP) and reports the results. |
| 6 |
* |
| 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/>. |
| 10 |
* |
| 11 |
* Copyright 2000-2001, 2004, 2006-2019, 2022 Russ Allbery <[email protected]> |
| 12 |
* |
| 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: |
| 19 |
* |
| 20 |
* The above copyright notice and this permission notice shall be included in |
| 21 |
* all copies or substantial portions of the Software. |
| 22 |
* |
| 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. |
| 30 |
* |
| 31 |
* SPDX-License-Identifier: MIT |
| 32 |
*/ |
| 33 |
|
| 34 |
/* |
| 35 |
* Usage: |
| 36 |
* |
| 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> |
| 40 |
* |
| 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. |
| 46 |
* |
| 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: |
| 51 |
* |
| 52 |
* ok <number> |
| 53 |
* not ok <number> |
| 54 |
* ok <number> # skip |
| 55 |
* not ok <number> # todo |
| 56 |
* |
| 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. |
| 64 |
* |
| 65 |
* As a special case, the first line of the output may be in the form: |
| 66 |
* |
| 67 |
* 1..0 # skip some reason |
| 68 |
* |
| 69 |
* which indicates that this entire test case should be skipped and gives a |
| 70 |
* reason. |
| 71 |
* |
| 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 #. |
| 75 |
* |
| 76 |
* This is a subset of TAP as documented in Test::Harness::TAP or |
| 77 |
* TAP::Parser::Grammar, which comes with Perl. |
| 78 |
* |
| 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. |
| 82 |
* |
| 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. |
| 88 |
* |
| 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. |
| 92 |
*/ |
| 93 |
|
| 94 |
/* Required for fdopen(), getopt(), and putenv(). */ |
| 95 |
#if defined(__STRICT_ANSI__) || defined(PEDANTIC) |
| 96 |
# ifndef _XOPEN_SOURCE |
| 97 |
# define _XOPEN_SOURCE 500 |
| 98 |
# endif |
| 99 |
#endif |
| 100 |
|
| 101 |
#include <ctype.h> |
| 102 |
#include <errno.h> |
| 103 |
#include <fcntl.h> |
| 104 |
#include <limits.h> |
| 105 |
#include <stdarg.h> |
| 106 |
#include <stddef.h> |
| 107 |
#include <stdio.h> |
| 108 |
#include <stdlib.h> |
| 109 |
#include <string.h> |
| 110 |
#include <strings.h> |
| 111 |
#include <sys/stat.h> |
| 112 |
#include <sys/time.h> |
| 113 |
#include <sys/types.h> |
| 114 |
#include <sys/wait.h> |
| 115 |
#include <time.h> |
| 116 |
#include <unistd.h> |
| 117 |
|
| 118 |
/* sys/time.h must be included before sys/resource.h on some platforms. */ |
| 119 |
#include <sys/resource.h> |
| 120 |
|
| 121 |
/* AIX 6.1 (and possibly later) doesn't have WCOREDUMP. */ |
| 122 |
#ifndef WCOREDUMP |
| 123 |
# define WCOREDUMP(status) ((unsigned) (status) &0x80) |
| 124 |
#endif |
| 125 |
|
| 126 |
/* |
| 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 |
| 129 |
* have. |
| 130 |
*/ |
| 131 |
#ifndef STDIN_FILENO |
| 132 |
# define STDIN_FILENO 0 |
| 133 |
# define STDOUT_FILENO 1 |
| 134 |
# define STDERR_FILENO 2 |
| 135 |
#endif |
| 136 |
|
| 137 |
/* |
| 138 |
* Used for iterating through arrays. Returns the number of elements in the |
| 139 |
* array (useful for a < upper bound in a for loop). |
| 140 |
*/ |
| 141 |
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) |
| 142 |
|
| 143 |
/* |
| 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. |
| 150 |
*/ |
| 151 |
#ifndef C_TAP_SOURCE |
| 152 |
# define C_TAP_SOURCE NULL |
| 153 |
#endif |
| 154 |
#ifndef C_TAP_BUILD |
| 155 |
# define C_TAP_BUILD NULL |
| 156 |
#endif |
| 157 |
|
| 158 |
/* Test status codes. */ |
| 159 |
enum test_status { |
| 160 |
TEST_FAIL, |
| 161 |
TEST_PASS, |
| 162 |
TEST_SKIP, |
| 163 |
TEST_INVALID |
| 164 |
}; |
| 165 |
|
| 166 |
/* Really, just a boolean, but this is more self-documenting. */ |
| 167 |
enum test_verbose { |
| 168 |
CONCISE = 0, |
| 169 |
VERBOSE = 1 |
| 170 |
}; |
| 171 |
|
| 172 |
/* Indicates the state of our plan. */ |
| 173 |
enum plan_status { |
| 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. */ |
| 178 |
}; |
| 179 |
|
| 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. */ |
| 185 |
|
| 186 |
/* Structure to hold data for a set of tests. */ |
| 187 |
struct testset { |
| 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. */ |
| 204 |
}; |
| 205 |
|
| 206 |
/* Structure to hold a linked list of test sets. */ |
| 207 |
struct testlist { |
| 208 |
struct testset *ts; |
| 209 |
struct testlist *next; |
| 210 |
}; |
| 211 |
|
| 212 |
/* |
| 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. |
| 216 |
*/ |
| 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\ |
| 221 |
\n\ |
| 222 |
Options:\n\ |
| 223 |
-b <build-dir> Set the build directory to <build-dir>\n\ |
| 224 |
%s"; |
| 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\ |
| 230 |
\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"; |
| 234 |
|
| 235 |
/* |
| 236 |
* Header used for test output. %s is replaced by the file name of the list |
| 237 |
* of tests. |
| 238 |
*/ |
| 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"; |
| 242 |
|
| 243 |
/* Header for reports of failed tests. */ |
| 244 |
static const char header[] = "\n\ |
| 245 |
Failed Set Fail/Total (%) Skip Stat Failing Tests\n\ |
| 246 |
-------------------------- -------------- ---- ---- ------------------------"; |
| 247 |
|
| 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__)) |
| 256 |
|
| 257 |
/* |
| 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). |
| 261 |
*/ |
| 262 |
#ifndef __attribute__ |
| 263 |
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) |
| 264 |
# define __attribute__(spec) /* empty */ |
| 265 |
# endif |
| 266 |
#endif |
| 267 |
|
| 268 |
/* |
| 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. |
| 274 |
*/ |
| 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 */ |
| 279 |
# endif |
| 280 |
# endif |
| 281 |
#endif |
| 282 |
|
| 283 |
/* |
| 284 |
* Suppress the argument to __malloc__ in Clang (not supported in at least |
| 285 |
* version 13) and GCC versions prior to 11. |
| 286 |
*/ |
| 287 |
#if !defined(__attribute__) && !defined(__malloc__) |
| 288 |
# if defined(__clang__) || __GNUC__ < 11 |
| 289 |
# define __malloc__(dalloc) __malloc__ |
| 290 |
# endif |
| 291 |
#endif |
| 292 |
|
| 293 |
/* |
| 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. |
| 298 |
*/ |
| 299 |
#if !defined(__attribute__) && (defined(__llvm__) || defined(__clang__)) |
| 300 |
# pragma GCC diagnostic ignored "-Wattributes" |
| 301 |
#endif |
| 302 |
|
| 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__)); |
| 318 |
|
| 319 |
|
| 320 |
/* |
| 321 |
* Report a fatal error and exit. |
| 322 |
*/ |
| 323 |
static void |
| 324 |
die(const char *format, ...) |
| 325 |
{ |
| 326 |
va_list args; |
| 327 |
|
| 328 |
fflush(stdout); |
| 329 |
fprintf(stderr, "runtests: "); |
| 330 |
va_start(args, format); |
| 331 |
vfprintf(stderr, format, args); |
| 332 |
va_end(args); |
| 333 |
fprintf(stderr, "\n"); |
| 334 |
exit(1); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
/* |
| 339 |
* Report a fatal error, including the results of strerror, and exit. |
| 340 |
*/ |
| 341 |
static void |
| 342 |
sysdie(const char *format, ...) |
| 343 |
{ |
| 344 |
int oerrno; |
| 345 |
va_list args; |
| 346 |
|
| 347 |
oerrno = errno; |
| 348 |
fflush(stdout); |
| 349 |
fprintf(stderr, "runtests: "); |
| 350 |
va_start(args, format); |
| 351 |
vfprintf(stderr, format, args); |
| 352 |
va_end(args); |
| 353 |
fprintf(stderr, ": %s\n", strerror(oerrno)); |
| 354 |
exit(1); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
/* |
| 359 |
* Allocate zeroed memory, reporting a fatal error and exiting on failure. |
| 360 |
*/ |
| 361 |
static void * |
| 362 |
x_calloc(size_t n, size_t size, const char *file, int line) |
| 363 |
{ |
| 364 |
void *p; |
| 365 |
|
| 366 |
n = (n > 0) ? n : 1; |
| 367 |
size = (size > 0) ? size : 1; |
| 368 |
p = calloc(n, size); |
| 369 |
if (p == NULL) |
| 370 |
sysdie("failed to calloc %lu bytes at %s line %d", |
| 371 |
(unsigned long) size, file, line); |
| 372 |
return p; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
/* |
| 377 |
* Allocate memory, reporting a fatal error and exiting on failure. |
| 378 |
*/ |
| 379 |
static void * |
| 380 |
x_malloc(size_t size, const char *file, int line) |
| 381 |
{ |
| 382 |
void *p; |
| 383 |
|
| 384 |
p = malloc(size); |
| 385 |
if (p == NULL) |
| 386 |
sysdie("failed to malloc %lu bytes at %s line %d", |
| 387 |
(unsigned long) size, file, line); |
| 388 |
return p; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
/* |
| 393 |
* Reallocate memory, reporting a fatal error and exiting on failure. |
| 394 |
* |
| 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. |
| 400 |
* |
| 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.) |
| 404 |
*/ |
| 405 |
static void * |
| 406 |
x_reallocarray(void *p, size_t n, size_t size, const char *file, int line) |
| 407 |
{ |
| 408 |
n = (n > 0) ? n : 1; |
| 409 |
size = (size > 0) ? size : 1; |
| 410 |
|
| 411 |
if (n > 0 && UINT_MAX / n <= size) |
| 412 |
sysdie("realloc too large at %s line %d", file, line); |
| 413 |
p = realloc(p, n * size); |
| 414 |
if (p == NULL) |
| 415 |
sysdie("failed to realloc %lu bytes at %s line %d", |
| 416 |
(unsigned long) (n * size), file, line); |
| 417 |
return p; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
/* |
| 422 |
* Copy a string, reporting a fatal error and exiting on failure. |
| 423 |
*/ |
| 424 |
static char * |
| 425 |
x_strdup(const char *s, const char *file, int line) |
| 426 |
{ |
| 427 |
char *p; |
| 428 |
size_t len; |
| 429 |
|
| 430 |
len = strlen(s) + 1; |
| 431 |
p = (char *) malloc(len); |
| 432 |
if (p == NULL) |
| 433 |
sysdie("failed to strdup %lu bytes at %s line %d", (unsigned long) len, |
| 434 |
file, line); |
| 435 |
memcpy(p, s, len); |
| 436 |
return p; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
/* |
| 441 |
* Copy the first n characters of a string, reporting a fatal error and |
| 442 |
* existing on failure. |
| 443 |
* |
| 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 |
| 446 |
* requirement. |
| 447 |
*/ |
| 448 |
char * |
| 449 |
x_strndup(const char *s, size_t size, const char *file, int line) |
| 450 |
{ |
| 451 |
const char *p; |
| 452 |
size_t len; |
| 453 |
char *copy; |
| 454 |
|
| 455 |
/* Don't assume that the source string is nul-terminated. */ |
| 456 |
for (p = s; (size_t) (p - s) < size && *p != '\0'; p++) |
| 457 |
; |
| 458 |
len = (size_t) (p - s); |
| 459 |
copy = (char *) malloc(len + 1); |
| 460 |
if (copy == NULL) |
| 461 |
sysdie("failed to strndup %lu bytes at %s line %d", |
| 462 |
(unsigned long) len, file, line); |
| 463 |
memcpy(copy, s, len); |
| 464 |
copy[len] = '\0'; |
| 465 |
return copy; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
/* |
| 470 |
* Form a new string by concatenating multiple strings. The arguments must be |
| 471 |
* terminated by (const char *) 0. |
| 472 |
* |
| 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. |
| 478 |
*/ |
| 479 |
static char * |
| 480 |
concat(const char *first, ...) |
| 481 |
{ |
| 482 |
va_list args; |
| 483 |
char *result; |
| 484 |
const char *string; |
| 485 |
size_t offset; |
| 486 |
size_t length = 0; |
| 487 |
|
| 488 |
/* |
| 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). |
| 492 |
*/ |
| 493 |
va_start(args, first); |
| 494 |
for (string = first; string != NULL; string = va_arg(args, const char *)) { |
| 495 |
if (length >= UINT_MAX - strlen(string)) { |
| 496 |
errno = EINVAL; |
| 497 |
sysdie("strings too long in concat"); |
| 498 |
} |
| 499 |
length += strlen(string); |
| 500 |
} |
| 501 |
va_end(args); |
| 502 |
length++; |
| 503 |
|
| 504 |
/* Create the string. */ |
| 505 |
result = xmalloc(length); |
| 506 |
va_start(args, first); |
| 507 |
offset = 0; |
| 508 |
for (string = first; string != NULL; string = va_arg(args, const char *)) { |
| 509 |
memcpy(result + offset, string, strlen(string)); |
| 510 |
offset += strlen(string); |
| 511 |
} |
| 512 |
va_end(args); |
| 513 |
result[offset] = '\0'; |
| 514 |
return result; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
/* |
| 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. |
| 521 |
*/ |
| 522 |
static double |
| 523 |
tv_seconds(const struct timeval *tv) |
| 524 |
{ |
| 525 |
return difftime(tv->tv_sec, 0) + (double) tv->tv_usec * 1e-6; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
/* |
| 530 |
* Given two struct timevals, return the difference in seconds. |
| 531 |
*/ |
| 532 |
static double |
| 533 |
tv_diff(const struct timeval *tv1, const struct timeval *tv0) |
| 534 |
{ |
| 535 |
return tv_seconds(tv1) - tv_seconds(tv0); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
/* |
| 540 |
* Given two struct timevals, return the sum in seconds as a double. |
| 541 |
*/ |
| 542 |
static double |
| 543 |
tv_sum(const struct timeval *tv1, const struct timeval *tv2) |
| 544 |
{ |
| 545 |
return tv_seconds(tv1) + tv_seconds(tv2); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
/* |
| 550 |
* Given a pointer to a string, skip any leading whitespace and return a |
| 551 |
* pointer to the first non-whitespace character. |
| 552 |
*/ |
| 553 |
static const char * |
| 554 |
skip_whitespace(const char *p) |
| 555 |
{ |
| 556 |
while (isspace((unsigned char) (*p))) |
| 557 |
p++; |
| 558 |
return p; |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
/* |
| 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. |
| 565 |
*/ |
| 566 |
static const char * |
| 567 |
skip_non_whitespace(const char *p) |
| 568 |
{ |
| 569 |
while (*p != '\0' && !isspace((unsigned char) (*p))) |
| 570 |
p++; |
| 571 |
return p; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
/* |
| 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. |
| 579 |
*/ |
| 580 |
static pid_t |
| 581 |
test_start(char *const *command, int *fd) |
| 582 |
{ |
| 583 |
int fds[2], infd, errfd; |
| 584 |
pid_t child; |
| 585 |
|
| 586 |
/* Create a pipe used to capture the output from the test program. */ |
| 587 |
if (pipe(fds) == -1) { |
| 588 |
puts("ABORTED"); |
| 589 |
fflush(stdout); |
| 590 |
sysdie("can't create pipe"); |
| 591 |
} |
| 592 |
|
| 593 |
/* Fork a child process, massage the file descriptors, and exec. */ |
| 594 |
child = fork(); |
| 595 |
switch (child) { |
| 596 |
case -1: |
| 597 |
puts("ABORTED"); |
| 598 |
fflush(stdout); |
| 599 |
sysdie("can't fork"); |
| 600 |
|
| 601 |
/* In the child. Set up our standard output. */ |
| 602 |
case 0: |
| 603 |
close(fds[0]); |
| 604 |
close(STDOUT_FILENO); |
| 605 |
if (dup2(fds[1], STDOUT_FILENO) < 0) |
| 606 |
_exit(CHILDERR_DUP); |
| 607 |
close(fds[1]); |
| 608 |
|
| 609 |
/* Point standard input at /dev/null. */ |
| 610 |
close(STDIN_FILENO); |
| 611 |
infd = open("/dev/null", O_RDONLY); |
| 612 |
if (infd < 0) |
| 613 |
_exit(CHILDERR_STDIN); |
| 614 |
if (infd != STDIN_FILENO) { |
| 615 |
if (dup2(infd, STDIN_FILENO) < 0) |
| 616 |
_exit(CHILDERR_DUP); |
| 617 |
close(infd); |
| 618 |
} |
| 619 |
|
| 620 |
/* Point standard error at /dev/null. */ |
| 621 |
close(STDERR_FILENO); |
| 622 |
errfd = open("/dev/null", O_WRONLY); |
| 623 |
if (errfd < 0) |
| 624 |
_exit(CHILDERR_STDERR); |
| 625 |
if (errfd != STDERR_FILENO) { |
| 626 |
if (dup2(errfd, STDERR_FILENO) < 0) |
| 627 |
_exit(CHILDERR_DUP); |
| 628 |
close(errfd); |
| 629 |
} |
| 630 |
|
| 631 |
/* Now, exec our process. */ |
| 632 |
if (execv(command[0], command) == -1) |
| 633 |
_exit(CHILDERR_EXEC); |
| 634 |
break; |
| 635 |
|
| 636 |
/* In parent. Close the extra file descriptor. */ |
| 637 |
default: |
| 638 |
close(fds[1]); |
| 639 |
break; |
| 640 |
} |
| 641 |
*fd = fds[0]; |
| 642 |
return child; |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
/* |
| 647 |
* Back up over the output saying what test we were executing. |
| 648 |
*/ |
| 649 |
static void |
| 650 |
test_backspace(struct testset *ts) |
| 651 |
{ |
| 652 |
unsigned int i; |
| 653 |
|
| 654 |
if (!isatty(STDOUT_FILENO)) |
| 655 |
return; |
| 656 |
for (i = 0; i < ts->length; i++) |
| 657 |
putchar('\b'); |
| 658 |
for (i = 0; i < ts->length; i++) |
| 659 |
putchar(' '); |
| 660 |
for (i = 0; i < ts->length; i++) |
| 661 |
putchar('\b'); |
| 662 |
ts->length = 0; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
/* |
| 667 |
* Allocate or resize the array of test results to be large enough to contain |
| 668 |
* the test number in. |
| 669 |
*/ |
| 670 |
static void |
| 671 |
resize_results(struct testset *ts, unsigned long n) |
| 672 |
{ |
| 673 |
unsigned long i; |
| 674 |
size_t s; |
| 675 |
|
| 676 |
/* If there's already enough space, return quickly. */ |
| 677 |
if (n <= ts->allocated) |
| 678 |
return; |
| 679 |
|
| 680 |
/* |
| 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. |
| 684 |
*/ |
| 685 |
if (ts->allocated == 0) { |
| 686 |
s = (n > 32) ? n : 32; |
| 687 |
ts->results = xcalloc(s, enum test_status); |
| 688 |
} else { |
| 689 |
s = (n > ts->allocated + 1024) ? n : ts->allocated + 1024; |
| 690 |
ts->results = xreallocarray(ts->results, s, enum test_status); |
| 691 |
} |
| 692 |
|
| 693 |
/* Set the results for the newly-allocated test array. */ |
| 694 |
for (i = ts->allocated; i < s; i++) |
| 695 |
ts->results[i] = TEST_INVALID; |
| 696 |
ts->allocated = s; |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
/* |
| 701 |
* Report an invalid test number and set the appropriate flags. Pulled into a |
| 702 |
* separate function since we do this in several places. |
| 703 |
*/ |
| 704 |
static void |
| 705 |
invalid_test_number(struct testset *ts, long n, enum test_verbose verbose) |
| 706 |
{ |
| 707 |
if (!verbose) |
| 708 |
test_backspace(ts); |
| 709 |
printf("ABORTED (invalid test number %ld)\n", n); |
| 710 |
ts->aborted = 1; |
| 711 |
ts->reported = 1; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
/* |
| 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. |
| 720 |
*/ |
| 721 |
static int |
| 722 |
test_plan(const char *line, struct testset *ts, enum test_verbose verbose) |
| 723 |
{ |
| 724 |
long n; |
| 725 |
|
| 726 |
/* |
| 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 |
| 729 |
* a test result. |
| 730 |
*/ |
| 731 |
line = skip_whitespace(line); |
| 732 |
if (strncmp(line, "1..", 3) == 0) |
| 733 |
line += 3; |
| 734 |
|
| 735 |
/* |
| 736 |
* Get the count and check it for validity. |
| 737 |
* |
| 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. |
| 741 |
* |
| 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. |
| 744 |
*/ |
| 745 |
n = strtol(line, (char **) &line, 10); |
| 746 |
if (n == 0) { |
| 747 |
line = skip_whitespace(line); |
| 748 |
if (*line == '#') { |
| 749 |
line = skip_whitespace(line + 1); |
| 750 |
if (strncasecmp(line, "skip", 4) == 0) { |
| 751 |
line = skip_whitespace(line + 4); |
| 752 |
if (*line != '\0') { |
| 753 |
ts->reason = xstrdup(line); |
| 754 |
ts->reason[strlen(ts->reason) - 1] = '\0'; |
| 755 |
} |
| 756 |
ts->all_skipped = 1; |
| 757 |
ts->aborted = 1; |
| 758 |
ts->count = 0; |
| 759 |
ts->passed = 0; |
| 760 |
ts->skipped = 0; |
| 761 |
ts->failed = 0; |
| 762 |
return 0; |
| 763 |
} |
| 764 |
} |
| 765 |
} |
| 766 |
if (n <= 0) { |
| 767 |
puts("ABORTED (invalid test count)"); |
| 768 |
ts->aborted = 1; |
| 769 |
ts->reported = 1; |
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
/* |
| 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 |
| 776 |
* range. |
| 777 |
*/ |
| 778 |
if (ts->plan == PLAN_PENDING && (unsigned long) n < ts->count) { |
| 779 |
invalid_test_number(ts, (long) ts->count, verbose); |
| 780 |
return 0; |
| 781 |
} |
| 782 |
|
| 783 |
/* |
| 784 |
* Otherwise, allocated or resize the results if needed and update count, |
| 785 |
* and then record that we've seen a plan. |
| 786 |
*/ |
| 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; |
| 793 |
return 1; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
/* |
| 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 |
| 801 |
* reported status. |
| 802 |
*/ |
| 803 |
static void |
| 804 |
test_checkline(const char *line, struct testset *ts, enum test_verbose verbose) |
| 805 |
{ |
| 806 |
enum test_status status = TEST_PASS; |
| 807 |
const char *bail; |
| 808 |
char *end; |
| 809 |
long number; |
| 810 |
unsigned long current; |
| 811 |
int outlen; |
| 812 |
|
| 813 |
/* Before anything, check for a test abort. */ |
| 814 |
bail = strstr(line, "Bail out!"); |
| 815 |
if (bail != NULL) { |
| 816 |
bail = skip_whitespace(bail + strlen("Bail out!")); |
| 817 |
if (*bail != '\0') { |
| 818 |
size_t length; |
| 819 |
|
| 820 |
length = strlen(bail); |
| 821 |
if (bail[length - 1] == '\n') |
| 822 |
length--; |
| 823 |
if (!verbose) |
| 824 |
test_backspace(ts); |
| 825 |
printf("ABORTED (%.*s)\n", (int) length, bail); |
| 826 |
ts->reported = 1; |
| 827 |
} |
| 828 |
ts->aborted = 1; |
| 829 |
return; |
| 830 |
} |
| 831 |
|
| 832 |
/* |
| 833 |
* If the given line isn't newline-terminated, it was too big for an |
| 834 |
* fgets(), which means ignore it. |
| 835 |
*/ |
| 836 |
if (line[strlen(line) - 1] != '\n') |
| 837 |
return; |
| 838 |
|
| 839 |
/* If the line begins with a hash mark, ignore it. */ |
| 840 |
if (line[0] == '#') |
| 841 |
return; |
| 842 |
|
| 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)) |
| 846 |
return; |
| 847 |
} else if (strncmp(line, "1..", 3) == 0) { |
| 848 |
if (ts->plan == PLAN_PENDING) { |
| 849 |
if (!test_plan(line, ts, verbose)) |
| 850 |
return; |
| 851 |
} else { |
| 852 |
if (!verbose) |
| 853 |
test_backspace(ts); |
| 854 |
puts("ABORTED (multiple plans)"); |
| 855 |
ts->aborted = 1; |
| 856 |
ts->reported = 1; |
| 857 |
return; |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
/* Parse the line, ignoring something we can't parse. */ |
| 862 |
if (strncmp(line, "not ", 4) == 0) { |
| 863 |
status = TEST_FAIL; |
| 864 |
line += 4; |
| 865 |
} |
| 866 |
if (strncmp(line, "ok", 2) != 0) |
| 867 |
return; |
| 868 |
line = skip_whitespace(line + 2); |
| 869 |
errno = 0; |
| 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); |
| 875 |
return; |
| 876 |
} else |
| 877 |
current = (unsigned long) number; |
| 878 |
if (current > ts->count && ts->plan == PLAN_FIRST) { |
| 879 |
invalid_test_number(ts, (long) current, verbose); |
| 880 |
return; |
| 881 |
} |
| 882 |
|
| 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) |
| 888 |
ts->count = current; |
| 889 |
} |
| 890 |
|
| 891 |
/* |
| 892 |
* Handle directives. We should probably do something more interesting |
| 893 |
* with unexpected passes of todo tests. |
| 894 |
*/ |
| 895 |
while (isdigit((unsigned char) (*line))) |
| 896 |
line++; |
| 897 |
line = skip_whitespace(line); |
| 898 |
if (*line == '#') { |
| 899 |
line = skip_whitespace(line + 1); |
| 900 |
if (strncasecmp(line, "skip", 4) == 0) |
| 901 |
status = TEST_SKIP; |
| 902 |
if (strncasecmp(line, "todo", 4) == 0) |
| 903 |
status = (status == TEST_FAIL) ? TEST_SKIP : TEST_FAIL; |
| 904 |
} |
| 905 |
|
| 906 |
/* Make sure that the test number is in range and not a duplicate. */ |
| 907 |
if (ts->results[current - 1] != TEST_INVALID) { |
| 908 |
if (!verbose) |
| 909 |
test_backspace(ts); |
| 910 |
printf("ABORTED (duplicate test number %lu)\n", current); |
| 911 |
ts->aborted = 1; |
| 912 |
ts->reported = 1; |
| 913 |
return; |
| 914 |
} |
| 915 |
|
| 916 |
/* Good results. Increment our various counters. */ |
| 917 |
switch (status) { |
| 918 |
case TEST_PASS: |
| 919 |
ts->passed++; |
| 920 |
break; |
| 921 |
case TEST_FAIL: |
| 922 |
ts->failed++; |
| 923 |
break; |
| 924 |
case TEST_SKIP: |
| 925 |
ts->skipped++; |
| 926 |
break; |
| 927 |
case TEST_INVALID: |
| 928 |
break; |
| 929 |
} |
| 930 |
ts->current = current; |
| 931 |
ts->results[current - 1] = status; |
| 932 |
if (!verbose && isatty(STDOUT_FILENO)) { |
| 933 |
test_backspace(ts); |
| 934 |
if (ts->plan == PLAN_PENDING) |
| 935 |
outlen = printf("%lu/?", current); |
| 936 |
else |
| 937 |
outlen = printf("%lu/%lu", current, ts->count); |
| 938 |
ts->length = (outlen >= 0) ? (unsigned int) outlen : 0; |
| 939 |
fflush(stdout); |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
/* |
| 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 |
| 951 |
* disable this). |
| 952 |
*/ |
| 953 |
static unsigned int |
| 954 |
test_print_range(unsigned long first, unsigned long last, unsigned long chars, |
| 955 |
unsigned int limit) |
| 956 |
{ |
| 957 |
unsigned int needed = 0; |
| 958 |
unsigned long n; |
| 959 |
|
| 960 |
for (n = first; n > 0; n /= 10) |
| 961 |
needed++; |
| 962 |
if (last > first) { |
| 963 |
for (n = last; n > 0; n /= 10) |
| 964 |
needed++; |
| 965 |
needed++; |
| 966 |
} |
| 967 |
if (chars > 0) |
| 968 |
needed += 2; |
| 969 |
if (limit > 0 && chars + needed > limit) { |
| 970 |
needed = 0; |
| 971 |
if (chars <= limit) { |
| 972 |
if (chars > 0) { |
| 973 |
printf(", "); |
| 974 |
needed += 2; |
| 975 |
} |
| 976 |
printf("..."); |
| 977 |
needed += 3; |
| 978 |
} |
| 979 |
} else { |
| 980 |
if (chars > 0) |
| 981 |
printf(", "); |
| 982 |
if (last > first) |
| 983 |
printf("%lu-", first); |
| 984 |
printf("%lu", last); |
| 985 |
} |
| 986 |
return needed; |
| 987 |
} |
| 988 |
|
| 989 |
|
| 990 |
/* |
| 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. |
| 995 |
*/ |
| 996 |
static void |
| 997 |
test_summarize(struct testset *ts, int status) |
| 998 |
{ |
| 999 |
unsigned long i; |
| 1000 |
unsigned long missing = 0; |
| 1001 |
unsigned long failed = 0; |
| 1002 |
unsigned long first = 0; |
| 1003 |
unsigned long last = 0; |
| 1004 |
|
| 1005 |
if (ts->aborted) { |
| 1006 |
fputs("ABORTED", stdout); |
| 1007 |
if (ts->count > 0) |
| 1008 |
printf(" (passed %lu/%lu)", ts->passed, ts->count - ts->skipped); |
| 1009 |
} else { |
| 1010 |
for (i = 0; i < ts->count; i++) { |
| 1011 |
if (ts->results[i] == TEST_INVALID) { |
| 1012 |
if (missing == 0) |
| 1013 |
fputs("MISSED ", stdout); |
| 1014 |
if (first && i == last) |
| 1015 |
last = i + 1; |
| 1016 |
else { |
| 1017 |
if (first) |
| 1018 |
test_print_range(first, last, missing - 1, 0); |
| 1019 |
missing++; |
| 1020 |
first = i + 1; |
| 1021 |
last = i + 1; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
if (first) |
| 1026 |
test_print_range(first, last, missing - 1, 0); |
| 1027 |
first = 0; |
| 1028 |
last = 0; |
| 1029 |
for (i = 0; i < ts->count; i++) { |
| 1030 |
if (ts->results[i] == TEST_FAIL) { |
| 1031 |
if (missing && !failed) |
| 1032 |
fputs("; ", stdout); |
| 1033 |
if (failed == 0) |
| 1034 |
fputs("FAILED ", stdout); |
| 1035 |
if (first && i == last) |
| 1036 |
last = i + 1; |
| 1037 |
else { |
| 1038 |
if (first) |
| 1039 |
test_print_range(first, last, failed - 1, 0); |
| 1040 |
failed++; |
| 1041 |
first = i + 1; |
| 1042 |
last = i + 1; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} |
| 1046 |
if (first) |
| 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); |
| 1053 |
else |
| 1054 |
printf(" (skipped %lu tests)", ts->skipped); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
} |
| 1058 |
if (status > 0) |
| 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" : ""); |
| 1063 |
putchar('\n'); |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
/* |
| 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. |
| 1072 |
*/ |
| 1073 |
static int |
| 1074 |
test_analyze(struct testset *ts) |
| 1075 |
{ |
| 1076 |
if (ts->reported) |
| 1077 |
return 0; |
| 1078 |
if (ts->all_skipped) { |
| 1079 |
if (ts->reason == NULL) |
| 1080 |
puts("skipped"); |
| 1081 |
else |
| 1082 |
printf("skipped (%s)\n", ts->reason); |
| 1083 |
return 1; |
| 1084 |
} else if (WIFEXITED(ts->status) && WEXITSTATUS(ts->status) != 0) { |
| 1085 |
switch (WEXITSTATUS(ts->status)) { |
| 1086 |
case CHILDERR_DUP: |
| 1087 |
if (!ts->reported) |
| 1088 |
puts("ABORTED (can't dup file descriptors)"); |
| 1089 |
break; |
| 1090 |
case CHILDERR_EXEC: |
| 1091 |
if (!ts->reported) |
| 1092 |
puts("ABORTED (execution failed -- not found?)"); |
| 1093 |
break; |
| 1094 |
case CHILDERR_STDIN: |
| 1095 |
case CHILDERR_STDERR: |
| 1096 |
if (!ts->reported) |
| 1097 |
puts("ABORTED (can't open /dev/null)"); |
| 1098 |
break; |
| 1099 |
default: |
| 1100 |
test_summarize(ts, WEXITSTATUS(ts->status)); |
| 1101 |
break; |
| 1102 |
} |
| 1103 |
return 0; |
| 1104 |
} else if (WIFSIGNALED(ts->status)) { |
| 1105 |
test_summarize(ts, -WTERMSIG(ts->status)); |
| 1106 |
return 0; |
| 1107 |
} else if (ts->plan != PLAN_FIRST && ts->plan != PLAN_FINAL) { |
| 1108 |
puts("ABORTED (no valid test plan)"); |
| 1109 |
ts->aborted = 1; |
| 1110 |
return 0; |
| 1111 |
} else { |
| 1112 |
test_summarize(ts, 0); |
| 1113 |
return (ts->failed == 0); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
/* |
| 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, |
| 1121 |
* false otherwise. |
| 1122 |
*/ |
| 1123 |
static int |
| 1124 |
test_run(struct testset *ts, enum test_verbose verbose) |
| 1125 |
{ |
| 1126 |
pid_t testpid, child; |
| 1127 |
int outfd, status; |
| 1128 |
unsigned long i; |
| 1129 |
FILE *output; |
| 1130 |
char buffer[BUFSIZ]; |
| 1131 |
|
| 1132 |
/* Run the test program. */ |
| 1133 |
testpid = test_start(ts->command, &outfd); |
| 1134 |
output = fdopen(outfd, "r"); |
| 1135 |
if (!output) { |
| 1136 |
puts("ABORTED"); |
| 1137 |
fflush(stdout); |
| 1138 |
sysdie("fdopen failed"); |
| 1139 |
} |
| 1140 |
|
| 1141 |
/* |
| 1142 |
* Pass each line of output to test_checkline(), and print the line if |
| 1143 |
* verbosity is requested. |
| 1144 |
*/ |
| 1145 |
while (!ts->aborted && fgets(buffer, sizeof(buffer), output)) { |
| 1146 |
if (verbose) |
| 1147 |
printf("%s", buffer); |
| 1148 |
test_checkline(buffer, ts, verbose); |
| 1149 |
} |
| 1150 |
if (ferror(output) || ts->plan == PLAN_INIT) |
| 1151 |
ts->aborted = 1; |
| 1152 |
if (!verbose) |
| 1153 |
test_backspace(ts); |
| 1154 |
|
| 1155 |
/* |
| 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. |
| 1159 |
*/ |
| 1160 |
while (fgets(buffer, sizeof(buffer), output)) |
| 1161 |
if (verbose) |
| 1162 |
printf("%s", buffer); |
| 1163 |
fclose(output); |
| 1164 |
child = waitpid(testpid, &ts->status, 0); |
| 1165 |
if (child == (pid_t) -1) { |
| 1166 |
if (!ts->reported) { |
| 1167 |
puts("ABORTED"); |
| 1168 |
fflush(stdout); |
| 1169 |
} |
| 1170 |
sysdie("waitpid for %u failed", (unsigned int) testpid); |
| 1171 |
} |
| 1172 |
if (ts->all_skipped) |
| 1173 |
ts->aborted = 0; |
| 1174 |
status = test_analyze(ts); |
| 1175 |
|
| 1176 |
/* Convert missing tests to failed tests. */ |
| 1177 |
for (i = 0; i < ts->count; i++) { |
| 1178 |
if (ts->results[i] == TEST_INVALID) { |
| 1179 |
ts->failed++; |
| 1180 |
ts->results[i] = TEST_FAIL; |
| 1181 |
status = 0; |
| 1182 |
} |
| 1183 |
} |
| 1184 |
return status; |
| 1185 |
} |
| 1186 |
|
| 1187 |
|
| 1188 |
/* Summarize a list of test failures. */ |
| 1189 |
static void |
| 1190 |
test_fail_summary(const struct testlist *fails) |
| 1191 |
{ |
| 1192 |
struct testset *ts; |
| 1193 |
unsigned int chars; |
| 1194 |
unsigned long i, first, last, total; |
| 1195 |
double failed; |
| 1196 |
|
| 1197 |
puts(header); |
| 1198 |
|
| 1199 |
/* Failed Set Fail/Total (%) Skip Stat Failing (25) |
| 1200 |
-------------------------- -------------- ---- ---- -------------- */ |
| 1201 |
for (; fails; fails = fails->next) { |
| 1202 |
ts = fails->ts; |
| 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, |
| 1207 |
ts->skipped); |
| 1208 |
if (WIFEXITED(ts->status)) |
| 1209 |
printf("%4d ", WEXITSTATUS(ts->status)); |
| 1210 |
else |
| 1211 |
printf(" -- "); |
| 1212 |
if (ts->aborted) { |
| 1213 |
puts("aborted"); |
| 1214 |
continue; |
| 1215 |
} |
| 1216 |
chars = 0; |
| 1217 |
first = 0; |
| 1218 |
last = 0; |
| 1219 |
for (i = 0; i < ts->count; i++) { |
| 1220 |
if (ts->results[i] == TEST_FAIL) { |
| 1221 |
if (first != 0 && i == last) |
| 1222 |
last = i + 1; |
| 1223 |
else { |
| 1224 |
if (first != 0) |
| 1225 |
chars += test_print_range(first, last, chars, 19); |
| 1226 |
first = i + 1; |
| 1227 |
last = i + 1; |
| 1228 |
} |
| 1229 |
} |
| 1230 |
} |
| 1231 |
if (first != 0) |
| 1232 |
test_print_range(first, last, chars, 19); |
| 1233 |
putchar('\n'); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
/* |
| 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. |
| 1241 |
*/ |
| 1242 |
static int |
| 1243 |
is_valid_test(const char *path) |
| 1244 |
{ |
| 1245 |
struct stat st; |
| 1246 |
|
| 1247 |
if (access(path, X_OK) < 0) |
| 1248 |
return 0; |
| 1249 |
if (stat(path, &st) < 0) |
| 1250 |
return 0; |
| 1251 |
if (!S_ISREG(st.st_mode)) |
| 1252 |
return 0; |
| 1253 |
return 1; |
| 1254 |
} |
| 1255 |
|
| 1256 |
|
| 1257 |
/* |
| 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. |
| 1265 |
* |
| 1266 |
* The caller is responsible for freeing the path member of the testset |
| 1267 |
* struct. |
| 1268 |
*/ |
| 1269 |
static char * |
| 1270 |
find_test(const char *name, const char *source, const char *build) |
| 1271 |
{ |
| 1272 |
char *path = NULL; |
| 1273 |
const char *bases[3], *suffix, *base; |
| 1274 |
unsigned int i, j; |
| 1275 |
const char *suffixes[3] = {"-t", ".t", ""}; |
| 1276 |
|
| 1277 |
/* Possible base directories. */ |
| 1278 |
bases[0] = "."; |
| 1279 |
bases[1] = build; |
| 1280 |
bases[2] = source; |
| 1281 |
|
| 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++) { |
| 1286 |
base = bases[j]; |
| 1287 |
if (base == NULL) |
| 1288 |
continue; |
| 1289 |
path = concat(base, "/", name, suffix, (const char *) 0); |
| 1290 |
if (is_valid_test(path)) |
| 1291 |
return path; |
| 1292 |
free(path); |
| 1293 |
path = NULL; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
if (path == NULL) |
| 1297 |
path = xstrdup(name); |
| 1298 |
return path; |
| 1299 |
} |
| 1300 |
|
| 1301 |
|
| 1302 |
/* |
| 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. |
| 1305 |
* |
| 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: |
| 1310 |
* |
| 1311 |
* valgrind |
| 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. |
| 1316 |
* |
| 1317 |
* libtool |
| 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. |
| 1323 |
*/ |
| 1324 |
static void |
| 1325 |
parse_test_list_line(const char *line, struct testset *ts, const char *source, |
| 1326 |
const char *build) |
| 1327 |
{ |
| 1328 |
const char *p, *end, *option, *libtool; |
| 1329 |
const char *valgrind = NULL; |
| 1330 |
unsigned int use_libtool = 0; |
| 1331 |
unsigned int use_valgrind = 0; |
| 1332 |
size_t len, i; |
| 1333 |
|
| 1334 |
/* Determine the name of the test. */ |
| 1335 |
p = skip_non_whitespace(line); |
| 1336 |
ts->file = xstrndup(line, p - line); |
| 1337 |
|
| 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) { |
| 1343 |
use_libtool = 1; |
| 1344 |
} else if (strncmp(p, "valgrind", end - p) == 0) { |
| 1345 |
valgrind = getenv("C_TAP_VALGRIND"); |
| 1346 |
use_valgrind = (valgrind != NULL); |
| 1347 |
} else { |
| 1348 |
option = xstrndup(p, end - p); |
| 1349 |
die("unknown test list option %s", option); |
| 1350 |
} |
| 1351 |
p = skip_whitespace(end); |
| 1352 |
} |
| 1353 |
|
| 1354 |
/* Construct the argv to run the test. First, find the length. */ |
| 1355 |
len = 1; |
| 1356 |
if (use_valgrind && valgrind != NULL) { |
| 1357 |
p = skip_whitespace(valgrind); |
| 1358 |
while (*p != '\0') { |
| 1359 |
len++; |
| 1360 |
p = skip_whitespace(skip_non_whitespace(p)); |
| 1361 |
} |
| 1362 |
if (use_libtool) |
| 1363 |
len += 2; |
| 1364 |
} |
| 1365 |
|
| 1366 |
/* Now, build the command. */ |
| 1367 |
ts->command = xcalloc(len + 1, char *); |
| 1368 |
i = 0; |
| 1369 |
if (use_valgrind && valgrind != NULL) { |
| 1370 |
if (use_libtool) { |
| 1371 |
libtool = getenv("C_TAP_LIBTOOL"); |
| 1372 |
if (libtool == NULL) |
| 1373 |
die("valgrind with libtool requested, but C_TAP_LIBTOOL is not" |
| 1374 |
" set"); |
| 1375 |
ts->command[i++] = xstrdup(libtool); |
| 1376 |
ts->command[i++] = xstrdup("--mode=execute"); |
| 1377 |
} |
| 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); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
if (i != len - 1) |
| 1386 |
die("internal error while constructing command line"); |
| 1387 |
ts->command[i++] = find_test(ts->file, source, build); |
| 1388 |
ts->command[i] = NULL; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
/* |
| 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. |
| 1397 |
*/ |
| 1398 |
static struct testlist * |
| 1399 |
read_test_list(const char *filename, const char *source, const char *build) |
| 1400 |
{ |
| 1401 |
FILE *file; |
| 1402 |
unsigned int line; |
| 1403 |
size_t length; |
| 1404 |
char buffer[BUFSIZ]; |
| 1405 |
const char *start; |
| 1406 |
struct testlist *listhead, *current; |
| 1407 |
|
| 1408 |
/* Create the initial container list that will hold our results. */ |
| 1409 |
listhead = xcalloc(1, struct testlist); |
| 1410 |
current = NULL; |
| 1411 |
|
| 1412 |
/* |
| 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. |
| 1415 |
*/ |
| 1416 |
file = fopen(filename, "r"); |
| 1417 |
if (file == NULL) |
| 1418 |
sysdie("can't open %s", filename); |
| 1419 |
line = 0; |
| 1420 |
while (fgets(buffer, sizeof(buffer), file)) { |
| 1421 |
line++; |
| 1422 |
length = strlen(buffer) - 1; |
| 1423 |
if (buffer[length] != '\n') { |
| 1424 |
fprintf(stderr, "%s:%u: line too long\n", filename, line); |
| 1425 |
exit(1); |
| 1426 |
} |
| 1427 |
buffer[length] = '\0'; |
| 1428 |
|
| 1429 |
/* Skip comments, leading spaces, and blank lines. */ |
| 1430 |
start = skip_whitespace(buffer); |
| 1431 |
if (strlen(start) == 0) |
| 1432 |
continue; |
| 1433 |
if (start[0] == '#') |
| 1434 |
continue; |
| 1435 |
|
| 1436 |
/* Allocate the new testset structure. */ |
| 1437 |
if (current == NULL) |
| 1438 |
current = listhead; |
| 1439 |
else { |
| 1440 |
current->next = xcalloc(1, struct testlist); |
| 1441 |
current = current->next; |
| 1442 |
} |
| 1443 |
current->ts = xcalloc(1, struct testset); |
| 1444 |
current->ts->plan = PLAN_INIT; |
| 1445 |
|
| 1446 |
/* Parse the line and store the results in the testset struct. */ |
| 1447 |
parse_test_list_line(start, current->ts, source, build); |
| 1448 |
} |
| 1449 |
fclose(file); |
| 1450 |
|
| 1451 |
/* If there were no tests, current is still NULL. */ |
| 1452 |
if (current == NULL) { |
| 1453 |
free(listhead); |
| 1454 |
return NULL; |
| 1455 |
} |
| 1456 |
|
| 1457 |
/* Return the results. */ |
| 1458 |
return listhead; |
| 1459 |
} |
| 1460 |
|
| 1461 |
|
| 1462 |
/* |
| 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 |
| 1466 |
* freeing. |
| 1467 |
*/ |
| 1468 |
static struct testlist * |
| 1469 |
build_test_list(char *argv[], int argc, const char *source, const char *build) |
| 1470 |
{ |
| 1471 |
int i; |
| 1472 |
struct testlist *listhead, *current; |
| 1473 |
|
| 1474 |
/* Create the initial container list that will hold our results. */ |
| 1475 |
listhead = xcalloc(1, struct testlist); |
| 1476 |
current = NULL; |
| 1477 |
|
| 1478 |
/* Walk the list of arguments and create test sets for them. */ |
| 1479 |
for (i = 0; i < argc; i++) { |
| 1480 |
if (current == NULL) |
| 1481 |
current = listhead; |
| 1482 |
else { |
| 1483 |
current->next = xcalloc(1, struct testlist); |
| 1484 |
current = current->next; |
| 1485 |
} |
| 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; |
| 1492 |
} |
| 1493 |
|
| 1494 |
/* If there were no tests, current is still NULL. */ |
| 1495 |
if (current == NULL) { |
| 1496 |
free(listhead); |
| 1497 |
return NULL; |
| 1498 |
} |
| 1499 |
|
| 1500 |
/* Return the results. */ |
| 1501 |
return listhead; |
| 1502 |
} |
| 1503 |
|
| 1504 |
|
| 1505 |
/* Free a struct testset. */ |
| 1506 |
static void |
| 1507 |
free_testset(struct testset *ts) |
| 1508 |
{ |
| 1509 |
size_t i; |
| 1510 |
|
| 1511 |
free(ts->file); |
| 1512 |
for (i = 0; ts->command[i] != NULL; i++) |
| 1513 |
free(ts->command[i]); |
| 1514 |
free(ts->command); |
| 1515 |
free(ts->results); |
| 1516 |
free(ts->reason); |
| 1517 |
free(ts); |
| 1518 |
} |
| 1519 |
|
| 1520 |
|
| 1521 |
/* |
| 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. |
| 1527 |
*/ |
| 1528 |
static int |
| 1529 |
test_batch(struct testlist *tests, enum test_verbose verbose) |
| 1530 |
{ |
| 1531 |
size_t length, i; |
| 1532 |
size_t longest = 0; |
| 1533 |
unsigned int count = 0; |
| 1534 |
struct testset *ts; |
| 1535 |
struct timeval start, end; |
| 1536 |
struct rusage stats; |
| 1537 |
struct testlist *failhead = NULL; |
| 1538 |
struct testlist *failtail = NULL; |
| 1539 |
struct testlist *current, *next; |
| 1540 |
int succeeded; |
| 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; |
| 1546 |
|
| 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) |
| 1551 |
longest = length; |
| 1552 |
} |
| 1553 |
|
| 1554 |
/* |
| 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. |
| 1557 |
*/ |
| 1558 |
longest += 2; |
| 1559 |
if (longest % 8) |
| 1560 |
longest += 8 - (longest % 8); |
| 1561 |
|
| 1562 |
/* Start the wall clock timer. */ |
| 1563 |
gettimeofday(&start, NULL); |
| 1564 |
|
| 1565 |
/* Now, plow through our tests again, running each one. */ |
| 1566 |
for (current = tests; current != NULL; current = current->next) { |
| 1567 |
ts = current->ts; |
| 1568 |
|
| 1569 |
/* Print out the name of the test file. */ |
| 1570 |
fputs(ts->file, stdout); |
| 1571 |
if (verbose) |
| 1572 |
fputs("\n\n", stdout); |
| 1573 |
else |
| 1574 |
for (i = strlen(ts->file); i < longest; i++) |
| 1575 |
putchar('.'); |
| 1576 |
if (isatty(STDOUT_FILENO)) |
| 1577 |
fflush(stdout); |
| 1578 |
|
| 1579 |
/* Run the test. */ |
| 1580 |
succeeded = test_run(ts, verbose); |
| 1581 |
fflush(stdout); |
| 1582 |
if (verbose) |
| 1583 |
putchar('\n'); |
| 1584 |
|
| 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; |
| 1591 |
count++; |
| 1592 |
|
| 1593 |
/* If the test fails, we shuffle it over to the fail list. */ |
| 1594 |
if (!succeeded) { |
| 1595 |
if (failhead == NULL) { |
| 1596 |
failhead = xcalloc(1, struct testlist); |
| 1597 |
failtail = failhead; |
| 1598 |
} else { |
| 1599 |
failtail->next = xcalloc(1, struct testlist); |
| 1600 |
failtail = failtail->next; |
| 1601 |
} |
| 1602 |
failtail->ts = ts; |
| 1603 |
failtail->next = NULL; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
total -= skipped; |
| 1607 |
|
| 1608 |
/* Stop the timer and get our child resource statistics. */ |
| 1609 |
gettimeofday(&end, NULL); |
| 1610 |
getrusage(RUSAGE_CHILDREN, &stats); |
| 1611 |
|
| 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; |
| 1617 |
free(failhead); |
| 1618 |
failhead = next; |
| 1619 |
} |
| 1620 |
} |
| 1621 |
|
| 1622 |
/* Free the memory used by the test lists. */ |
| 1623 |
while (tests != NULL) { |
| 1624 |
next = tests->next; |
| 1625 |
free_testset(tests->ts); |
| 1626 |
free(tests); |
| 1627 |
tests = next; |
| 1628 |
} |
| 1629 |
|
| 1630 |
/* Print out the final test summary. */ |
| 1631 |
putchar('\n'); |
| 1632 |
if (aborted != 0) { |
| 1633 |
if (aborted == 1) |
| 1634 |
printf("Aborted %lu test set", aborted); |
| 1635 |
else |
| 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); |
| 1640 |
else |
| 1641 |
printf("Failed %lu/%lu tests, %.2f%% okay", failed, total, |
| 1642 |
(double) (total - failed) * 100.0 / (double) total); |
| 1643 |
if (skipped != 0) { |
| 1644 |
if (skipped == 1) |
| 1645 |
printf(", %lu test skipped", skipped); |
| 1646 |
else |
| 1647 |
printf(", %lu tests skipped", skipped); |
| 1648 |
} |
| 1649 |
puts("."); |
| 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); |
| 1656 |
} |
| 1657 |
|
| 1658 |
|
| 1659 |
/* |
| 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. |
| 1662 |
*/ |
| 1663 |
static void |
| 1664 |
test_single(const char *program, const char *source, const char *build) |
| 1665 |
{ |
| 1666 |
char *path; |
| 1667 |
|
| 1668 |
path = find_test(program, source, build); |
| 1669 |
if (execl(path, path, (char *) 0) == -1) |
| 1670 |
sysdie("cannot exec %s", path); |
| 1671 |
} |
| 1672 |
|
| 1673 |
|
| 1674 |
/* |
| 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 |
| 1677 |
* listed. |
| 1678 |
*/ |
| 1679 |
int |
| 1680 |
main(int argc, char *argv[]) |
| 1681 |
{ |
| 1682 |
int option; |
| 1683 |
int status = 0; |
| 1684 |
int single = 0; |
| 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; |
| 1696 |
|
| 1697 |
program = argv[0]; |
| 1698 |
while ((option = getopt(argc, argv, "b:hl:os:v")) != EOF) { |
| 1699 |
switch (option) { |
| 1700 |
case 'b': |
| 1701 |
build = optarg; |
| 1702 |
break; |
| 1703 |
case 'h': |
| 1704 |
printf(usage_message, program, program, program, usage_extra); |
| 1705 |
exit(0); |
| 1706 |
case 'l': |
| 1707 |
list = optarg; |
| 1708 |
break; |
| 1709 |
case 'o': |
| 1710 |
single = 1; |
| 1711 |
break; |
| 1712 |
case 's': |
| 1713 |
source = optarg; |
| 1714 |
break; |
| 1715 |
case 'v': |
| 1716 |
verbose = VERBOSE; |
| 1717 |
break; |
| 1718 |
default: |
| 1719 |
exit(1); |
| 1720 |
} |
| 1721 |
} |
| 1722 |
argv += optind; |
| 1723 |
argc -= optind; |
| 1724 |
if ((list == NULL && argc < 1) || (list != NULL && argc > 0)) { |
| 1725 |
fprintf(stderr, usage_message, program, program, program, usage_extra); |
| 1726 |
exit(1); |
| 1727 |
} |
| 1728 |
|
| 1729 |
/* |
| 1730 |
* If C_TAP_VERBOSE is set in the environment, that also turns on verbose |
| 1731 |
* mode. |
| 1732 |
*/ |
| 1733 |
if (getenv("C_TAP_VERBOSE") != NULL) |
| 1734 |
verbose = VERBOSE; |
| 1735 |
|
| 1736 |
/* |
| 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. |
| 1740 |
*/ |
| 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"); |
| 1748 |
} |
| 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"); |
| 1756 |
} |
| 1757 |
|
| 1758 |
/* Run the tests as instructed. */ |
| 1759 |
if (single) |
| 1760 |
test_single(argv[0], source, build); |
| 1761 |
else if (list != NULL) { |
| 1762 |
shortlist = strrchr(list, '/'); |
| 1763 |
if (shortlist == NULL) |
| 1764 |
shortlist = list; |
| 1765 |
else |
| 1766 |
shortlist++; |
| 1767 |
printf(banner, shortlist); |
| 1768 |
tests = read_test_list(list, source, build); |
| 1769 |
status = test_batch(tests, verbose) ? 0 : 1; |
| 1770 |
} else { |
| 1771 |
tests = build_test_list(argv, argc, source, build); |
| 1772 |
status = test_batch(tests, verbose) ? 0 : 1; |
| 1773 |
} |
| 1774 |
|
| 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); |
| 1780 |
free(source_env); |
| 1781 |
} |
| 1782 |
if (build_env != NULL) { |
| 1783 |
putenv((char *) "C_TAP_BUILD="); |
| 1784 |
putenv((char *) "BUILD="); |
| 1785 |
free(c_tap_build_env); |
| 1786 |
free(build_env); |
| 1787 |
} |
| 1788 |
exit(status); |
| 1789 |
} |