| 1 |
/* |
| 2 |
* Some utility routines for writing tests. |
| 3 |
* |
| 4 |
* Here are a variety of utility routines for writing tests compatible with |
| 5 |
* the TAP protocol. All routines of the form ok() or is*() take a test |
| 6 |
* number and some number of appropriate arguments, check to be sure the |
| 7 |
* results match the expected output using the arguments, and print out |
| 8 |
* something appropriate for that test number. Other utility routines help in |
| 9 |
* constructing more complex tests, skipping tests, reporting errors, setting |
| 10 |
* up the TAP output format, or finding things in the test environment. |
| 11 |
* |
| 12 |
* This file is part of C TAP Harness. The current version plus supporting |
| 13 |
* documentation is at <https://www.eyrie.org/~eagle/software/c-tap-harness/>. |
| 14 |
* |
| 15 |
* Written by Russ Allbery <[email protected]> |
| 16 |
* Copyright 2009-2019, 2021 Russ Allbery <[email protected]> |
| 17 |
* Copyright 2001-2002, 2004-2008, 2011-2014 |
| 18 |
* The Board of Trustees of the Leland Stanford Junior University |
| 19 |
* |
| 20 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 21 |
* copy of this software and associated documentation files (the "Software"), |
| 22 |
* to deal in the Software without restriction, including without limitation |
| 23 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 24 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 25 |
* Software is furnished to do so, subject to the following conditions: |
| 26 |
* |
| 27 |
* The above copyright notice and this permission notice shall be included in |
| 28 |
* all copies or substantial portions of the Software. |
| 29 |
* |
| 30 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 33 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 35 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 36 |
* DEALINGS IN THE SOFTWARE. |
| 37 |
* |
| 38 |
* SPDX-License-Identifier: MIT |
| 39 |
*/ |
| 40 |
|
| 41 |
#include <errno.h> |
| 42 |
#include <limits.h> |
| 43 |
#include <stdarg.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <string.h> |
| 47 |
#ifdef _WIN32 |
| 48 |
# include <direct.h> |
| 49 |
#else |
| 50 |
# include <sys/stat.h> |
| 51 |
#endif |
| 52 |
#include <sys/types.h> |
| 53 |
#include <unistd.h> |
| 54 |
|
| 55 |
#include <tests/tap/basic.h> |
| 56 |
|
| 57 |
/* Windows provides mkdir and rmdir under different names. */ |
| 58 |
#ifdef _WIN32 |
| 59 |
# define mkdir(p, m) _mkdir(p) |
| 60 |
# define rmdir(p) _rmdir(p) |
| 61 |
#endif |
| 62 |
|
| 63 |
/* |
| 64 |
* The test count. Always contains the number that will be used for the next |
| 65 |
* test status. This is exported to callers of the library. |
| 66 |
*/ |
| 67 |
unsigned long testnum = 1; |
| 68 |
|
| 69 |
/* |
| 70 |
* Status information stored so that we can give a test summary at the end of |
| 71 |
* the test case. We store the planned final test and the count of failures. |
| 72 |
* We can get the highest test count from testnum. |
| 73 |
*/ |
| 74 |
static unsigned long _planned = 0; |
| 75 |
static unsigned long _failed = 0; |
| 76 |
|
| 77 |
/* |
| 78 |
* Store the PID of the process that called plan() and only summarize |
| 79 |
* results when that process exits, so as to not misreport results in forked |
| 80 |
* processes. |
| 81 |
*/ |
| 82 |
static pid_t _process = 0; |
| 83 |
|
| 84 |
/* |
| 85 |
* If true, we're doing lazy planning and will print out the plan based on the |
| 86 |
* last test number at the end of testing. |
| 87 |
*/ |
| 88 |
static int _lazy = 0; |
| 89 |
|
| 90 |
/* |
| 91 |
* If true, the test was aborted by calling bail(). Currently, this is only |
| 92 |
* used to ensure that we pass a false value to any cleanup functions even if |
| 93 |
* all tests to that point have passed. |
| 94 |
*/ |
| 95 |
static int _aborted = 0; |
| 96 |
|
| 97 |
/* |
| 98 |
* Registered cleanup functions. These are stored as a linked list and run in |
| 99 |
* registered order by finish when the test program exits. Each function is |
| 100 |
* passed a boolean value indicating whether all tests were successful. |
| 101 |
*/ |
| 102 |
struct cleanup_func { |
| 103 |
test_cleanup_func func; |
| 104 |
test_cleanup_func_with_data func_with_data; |
| 105 |
void *data; |
| 106 |
struct cleanup_func *next; |
| 107 |
}; |
| 108 |
static struct cleanup_func *cleanup_funcs = NULL; |
| 109 |
|
| 110 |
/* |
| 111 |
* Registered diag files. Any output found in these files will be printed out |
| 112 |
* as if it were passed to diag() before any other output we do. This allows |
| 113 |
* background processes to log to a file and have that output interleaved with |
| 114 |
* the test output. |
| 115 |
*/ |
| 116 |
struct diag_file { |
| 117 |
char *name; |
| 118 |
FILE *file; |
| 119 |
char *buffer; |
| 120 |
size_t bufsize; |
| 121 |
struct diag_file *next; |
| 122 |
}; |
| 123 |
static struct diag_file *diag_files = NULL; |
| 124 |
|
| 125 |
/* |
| 126 |
* Print a specified prefix and then the test description. Handles turning |
| 127 |
* the argument list into a va_args structure suitable for passing to |
| 128 |
* print_desc, which has to be done in a macro. Assumes that format is the |
| 129 |
* argument immediately before the variadic arguments. |
| 130 |
*/ |
| 131 |
#define PRINT_DESC(prefix, format) \ |
| 132 |
do { \ |
| 133 |
if (format != NULL) { \ |
| 134 |
va_list args; \ |
| 135 |
printf("%s", prefix); \ |
| 136 |
va_start(args, format); \ |
| 137 |
vprintf(format, args); \ |
| 138 |
va_end(args); \ |
| 139 |
} \ |
| 140 |
} while (0) |
| 141 |
|
| 142 |
|
| 143 |
/* |
| 144 |
* Form a new string by concatenating multiple strings. The arguments must be |
| 145 |
* terminated by (const char *) 0. |
| 146 |
* |
| 147 |
* This function only exists because we can't assume asprintf. We can't |
| 148 |
* simulate asprintf with snprintf because we're only assuming SUSv3, which |
| 149 |
* does not require that snprintf with a NULL buffer return the required |
| 150 |
* length. When those constraints are relaxed, this should be ripped out and |
| 151 |
* replaced with asprintf or a more trivial replacement with snprintf. |
| 152 |
*/ |
| 153 |
static char * |
| 154 |
concat(const char *first, ...) |
| 155 |
{ |
| 156 |
va_list args; |
| 157 |
char *result; |
| 158 |
const char *string; |
| 159 |
size_t offset; |
| 160 |
size_t length = 0; |
| 161 |
|
| 162 |
/* |
| 163 |
* Find the total memory required. Ensure we don't overflow length. See |
| 164 |
* the comment for breallocarray for why we're using UINT_MAX here. |
| 165 |
*/ |
| 166 |
va_start(args, first); |
| 167 |
for (string = first; string != NULL; string = va_arg(args, const char *)) { |
| 168 |
if (length >= UINT_MAX - strlen(string)) |
| 169 |
bail("strings too long in concat"); |
| 170 |
length += strlen(string); |
| 171 |
} |
| 172 |
va_end(args); |
| 173 |
length++; |
| 174 |
|
| 175 |
/* Create the string. */ |
| 176 |
result = bcalloc_type(length, char); |
| 177 |
va_start(args, first); |
| 178 |
offset = 0; |
| 179 |
for (string = first; string != NULL; string = va_arg(args, const char *)) { |
| 180 |
memcpy(result + offset, string, strlen(string)); |
| 181 |
offset += strlen(string); |
| 182 |
} |
| 183 |
va_end(args); |
| 184 |
result[offset] = '\0'; |
| 185 |
return result; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
/* |
| 190 |
* Helper function for check_diag_files to handle a single line in a diag |
| 191 |
* file. |
| 192 |
* |
| 193 |
* The general scheme here used is as follows: read one line of output. If we |
| 194 |
* get NULL, check for an error. If there was one, bail out of the test |
| 195 |
* program; otherwise, return, and the enclosing loop will check for EOF. |
| 196 |
* |
| 197 |
* If we get some data, see if it ends in a newline. If it doesn't end in a |
| 198 |
* newline, we have one of two cases: our buffer isn't large enough, in which |
| 199 |
* case we resize it and try again, or we have incomplete data in the file, in |
| 200 |
* which case we rewind the file and will try again next time. |
| 201 |
* |
| 202 |
* Returns a boolean indicating whether the last line was incomplete. |
| 203 |
*/ |
| 204 |
static int |
| 205 |
handle_diag_file_line(struct diag_file *file, fpos_t where) |
| 206 |
{ |
| 207 |
int size; |
| 208 |
size_t length; |
| 209 |
|
| 210 |
/* Read the next line from the file. */ |
| 211 |
size = file->bufsize > INT_MAX ? INT_MAX : (int) file->bufsize; |
| 212 |
if (fgets(file->buffer, size, file->file) == NULL) { |
| 213 |
if (ferror(file->file)) |
| 214 |
sysbail("cannot read from %s", file->name); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
/* |
| 219 |
* See if the line ends in a newline. If not, see which error case we |
| 220 |
* have. |
| 221 |
*/ |
| 222 |
length = strlen(file->buffer); |
| 223 |
if (file->buffer[length - 1] != '\n') { |
| 224 |
int incomplete = 0; |
| 225 |
|
| 226 |
/* Check whether we ran out of buffer space and resize if so. */ |
| 227 |
if (length < file->bufsize - 1) |
| 228 |
incomplete = 1; |
| 229 |
else { |
| 230 |
file->bufsize += BUFSIZ; |
| 231 |
file->buffer = |
| 232 |
breallocarray_type(file->buffer, file->bufsize, char); |
| 233 |
} |
| 234 |
|
| 235 |
/* |
| 236 |
* On either incomplete lines or too small of a buffer, rewind |
| 237 |
* and read the file again (on the next pass, if incomplete). |
| 238 |
* It's simpler than trying to double-buffer the file. |
| 239 |
*/ |
| 240 |
if (fsetpos(file->file, &where) < 0) |
| 241 |
sysbail("cannot set position in %s", file->name); |
| 242 |
return incomplete; |
| 243 |
} |
| 244 |
|
| 245 |
/* We saw a complete line. Print it out. */ |
| 246 |
printf("# %s", file->buffer); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
/* |
| 252 |
* Check all registered diag_files for any output. We only print out the |
| 253 |
* output if we see a complete line; otherwise, we wait for the next newline. |
| 254 |
*/ |
| 255 |
static void |
| 256 |
check_diag_files(void) |
| 257 |
{ |
| 258 |
struct diag_file *file; |
| 259 |
fpos_t where; |
| 260 |
int incomplete; |
| 261 |
|
| 262 |
/* |
| 263 |
* Walk through each file and read each line of output available. |
| 264 |
*/ |
| 265 |
for (file = diag_files; file != NULL; file = file->next) { |
| 266 |
clearerr(file->file); |
| 267 |
|
| 268 |
/* Store the current position in case we have to rewind. */ |
| 269 |
if (fgetpos(file->file, &where) < 0) |
| 270 |
sysbail("cannot get position in %s", file->name); |
| 271 |
|
| 272 |
/* Continue until we get EOF or an incomplete line of data. */ |
| 273 |
incomplete = 0; |
| 274 |
while (!feof(file->file) && !incomplete) { |
| 275 |
incomplete = handle_diag_file_line(file, where); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
/* |
| 282 |
* Our exit handler. Called on completion of the test to report a summary of |
| 283 |
* results provided we're still in the original process. This also handles |
| 284 |
* printing out the plan if we used plan_lazy(), although that's suppressed if |
| 285 |
* we never ran a test (due to an early bail, for example), and running any |
| 286 |
* registered cleanup functions. |
| 287 |
*/ |
| 288 |
static void |
| 289 |
finish(void) |
| 290 |
{ |
| 291 |
int success, primary; |
| 292 |
struct cleanup_func *current; |
| 293 |
unsigned long highest = testnum - 1; |
| 294 |
struct diag_file *file, *tmp; |
| 295 |
|
| 296 |
/* Check for pending diag_file output. */ |
| 297 |
check_diag_files(); |
| 298 |
|
| 299 |
/* Free the diag_files. */ |
| 300 |
file = diag_files; |
| 301 |
while (file != NULL) { |
| 302 |
tmp = file; |
| 303 |
file = file->next; |
| 304 |
fclose(tmp->file); |
| 305 |
free(tmp->name); |
| 306 |
free(tmp->buffer); |
| 307 |
free(tmp); |
| 308 |
} |
| 309 |
diag_files = NULL; |
| 310 |
|
| 311 |
/* |
| 312 |
* Determine whether all tests were successful, which is needed before |
| 313 |
* calling cleanup functions since we pass that fact to the functions. |
| 314 |
*/ |
| 315 |
if (_planned == 0 && _lazy) |
| 316 |
_planned = highest; |
| 317 |
success = (!_aborted && _planned == highest && _failed == 0); |
| 318 |
|
| 319 |
/* |
| 320 |
* If there are any registered cleanup functions, we run those first. We |
| 321 |
* always run them, even if we didn't run a test. Don't do anything |
| 322 |
* except free the diag_files and call cleanup functions if we aren't the |
| 323 |
* primary process (the process in which plan or plan_lazy was called), |
| 324 |
* and tell the cleanup functions that fact. |
| 325 |
*/ |
| 326 |
primary = (_process == 0 || getpid() == _process); |
| 327 |
while (cleanup_funcs != NULL) { |
| 328 |
if (cleanup_funcs->func_with_data) { |
| 329 |
void *data = cleanup_funcs->data; |
| 330 |
|
| 331 |
cleanup_funcs->func_with_data(success, primary, data); |
| 332 |
} else { |
| 333 |
cleanup_funcs->func(success, primary); |
| 334 |
} |
| 335 |
current = cleanup_funcs; |
| 336 |
cleanup_funcs = cleanup_funcs->next; |
| 337 |
free(current); |
| 338 |
} |
| 339 |
if (!primary) |
| 340 |
return; |
| 341 |
|
| 342 |
/* Don't do anything further if we never planned a test. */ |
| 343 |
if (_planned == 0) |
| 344 |
return; |
| 345 |
|
| 346 |
/* If we're aborting due to bail, don't print summaries. */ |
| 347 |
if (_aborted) |
| 348 |
return; |
| 349 |
|
| 350 |
/* Print out the lazy plan if needed. */ |
| 351 |
fflush(stderr); |
| 352 |
if (_lazy) |
| 353 |
printf("1..%lu\n", _planned); |
| 354 |
|
| 355 |
/* Print out a summary of the results. */ |
| 356 |
if (_planned > highest) |
| 357 |
diag("Looks like you planned %lu test%s but only ran %lu", _planned, |
| 358 |
(_planned > 1 ? "s" : ""), highest); |
| 359 |
else if (_planned < highest) |
| 360 |
diag("Looks like you planned %lu test%s but ran %lu extra", _planned, |
| 361 |
(_planned > 1 ? "s" : ""), highest - _planned); |
| 362 |
else if (_failed > 0) |
| 363 |
diag("Looks like you failed %lu test%s of %lu", _failed, |
| 364 |
(_failed > 1 ? "s" : ""), _planned); |
| 365 |
else if (_planned != 1) |
| 366 |
diag("All %lu tests successful or skipped", _planned); |
| 367 |
else |
| 368 |
diag("%lu test successful or skipped", _planned); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
/* |
| 373 |
* Initialize things. Turns on line buffering on stdout and then prints out |
| 374 |
* the number of tests in the test suite. We intentionally don't check for |
| 375 |
* pending diag_file output here, since it should really come after the plan. |
| 376 |
*/ |
| 377 |
void |
| 378 |
plan(unsigned long count) |
| 379 |
{ |
| 380 |
if (setvbuf(stdout, NULL, _IOLBF, BUFSIZ) != 0) |
| 381 |
sysdiag("cannot set stdout to line buffered"); |
| 382 |
fflush(stderr); |
| 383 |
printf("1..%lu\n", count); |
| 384 |
testnum = 1; |
| 385 |
_planned = count; |
| 386 |
_process = getpid(); |
| 387 |
if (atexit(finish) != 0) { |
| 388 |
sysdiag("cannot register exit handler"); |
| 389 |
diag("cleanups will not be run"); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
/* |
| 395 |
* Initialize things for lazy planning, where we'll automatically print out a |
| 396 |
* plan at the end of the program. Turns on line buffering on stdout as well. |
| 397 |
*/ |
| 398 |
void |
| 399 |
plan_lazy(void) |
| 400 |
{ |
| 401 |
if (setvbuf(stdout, NULL, _IOLBF, BUFSIZ) != 0) |
| 402 |
sysdiag("cannot set stdout to line buffered"); |
| 403 |
testnum = 1; |
| 404 |
_process = getpid(); |
| 405 |
_lazy = 1; |
| 406 |
if (atexit(finish) != 0) |
| 407 |
sysbail("cannot register exit handler to display plan"); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
/* |
| 412 |
* Skip the entire test suite and exits. Should be called instead of plan(), |
| 413 |
* not after it, since it prints out a special plan line. Ignore diag_file |
| 414 |
* output here, since it's not clear if it's allowed before the plan. |
| 415 |
*/ |
| 416 |
void |
| 417 |
skip_all(const char *format, ...) |
| 418 |
{ |
| 419 |
fflush(stderr); |
| 420 |
printf("1..0 # skip"); |
| 421 |
PRINT_DESC(" ", format); |
| 422 |
putchar('\n'); |
| 423 |
exit(0); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
/* |
| 428 |
* Takes a boolean success value and assumes the test passes if that value |
| 429 |
* is true and fails if that value is false. |
| 430 |
*/ |
| 431 |
int |
| 432 |
ok(int success, const char *format, ...) |
| 433 |
{ |
| 434 |
fflush(stderr); |
| 435 |
check_diag_files(); |
| 436 |
printf("%sok %lu", success ? "" : "not ", testnum++); |
| 437 |
if (!success) |
| 438 |
_failed++; |
| 439 |
PRINT_DESC(" - ", format); |
| 440 |
putchar('\n'); |
| 441 |
return success; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
/* |
| 446 |
* Same as ok(), but takes the format arguments as a va_list. |
| 447 |
*/ |
| 448 |
int |
| 449 |
okv(int success, const char *format, va_list args) |
| 450 |
{ |
| 451 |
fflush(stderr); |
| 452 |
check_diag_files(); |
| 453 |
printf("%sok %lu", success ? "" : "not ", testnum++); |
| 454 |
if (!success) |
| 455 |
_failed++; |
| 456 |
if (format != NULL) { |
| 457 |
printf(" - "); |
| 458 |
vprintf(format, args); |
| 459 |
} |
| 460 |
putchar('\n'); |
| 461 |
return success; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
/* |
| 466 |
* Skip a test. |
| 467 |
*/ |
| 468 |
void |
| 469 |
skip(const char *reason, ...) |
| 470 |
{ |
| 471 |
fflush(stderr); |
| 472 |
check_diag_files(); |
| 473 |
printf("ok %lu # skip", testnum++); |
| 474 |
PRINT_DESC(" ", reason); |
| 475 |
putchar('\n'); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
/* |
| 480 |
* Report the same status on the next count tests. |
| 481 |
*/ |
| 482 |
int |
| 483 |
ok_block(unsigned long count, int success, const char *format, ...) |
| 484 |
{ |
| 485 |
unsigned long i; |
| 486 |
|
| 487 |
fflush(stderr); |
| 488 |
check_diag_files(); |
| 489 |
for (i = 0; i < count; i++) { |
| 490 |
printf("%sok %lu", success ? "" : "not ", testnum++); |
| 491 |
if (!success) |
| 492 |
_failed++; |
| 493 |
PRINT_DESC(" - ", format); |
| 494 |
putchar('\n'); |
| 495 |
} |
| 496 |
return success; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
/* |
| 501 |
* Skip the next count tests. |
| 502 |
*/ |
| 503 |
void |
| 504 |
skip_block(unsigned long count, const char *reason, ...) |
| 505 |
{ |
| 506 |
unsigned long i; |
| 507 |
|
| 508 |
fflush(stderr); |
| 509 |
check_diag_files(); |
| 510 |
for (i = 0; i < count; i++) { |
| 511 |
printf("ok %lu # skip", testnum++); |
| 512 |
PRINT_DESC(" ", reason); |
| 513 |
putchar('\n'); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
/* |
| 519 |
* Takes two boolean values and requires the truth value of both match. |
| 520 |
*/ |
| 521 |
int |
| 522 |
is_bool(int left, int right, const char *format, ...) |
| 523 |
{ |
| 524 |
int success; |
| 525 |
|
| 526 |
fflush(stderr); |
| 527 |
check_diag_files(); |
| 528 |
success = (!!left == !!right); |
| 529 |
if (success) |
| 530 |
printf("ok %lu", testnum++); |
| 531 |
else { |
| 532 |
diag(" left: %s", !!left ? "true" : "false"); |
| 533 |
diag("right: %s", !!right ? "true" : "false"); |
| 534 |
printf("not ok %lu", testnum++); |
| 535 |
_failed++; |
| 536 |
} |
| 537 |
PRINT_DESC(" - ", format); |
| 538 |
putchar('\n'); |
| 539 |
return success; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
/* |
| 544 |
* Takes two integer values and requires they match. |
| 545 |
*/ |
| 546 |
int |
| 547 |
is_int(long left, long right, const char *format, ...) |
| 548 |
{ |
| 549 |
int success; |
| 550 |
|
| 551 |
fflush(stderr); |
| 552 |
check_diag_files(); |
| 553 |
success = (left == right); |
| 554 |
if (success) |
| 555 |
printf("ok %lu", testnum++); |
| 556 |
else { |
| 557 |
diag(" left: %ld", left); |
| 558 |
diag("right: %ld", right); |
| 559 |
printf("not ok %lu", testnum++); |
| 560 |
_failed++; |
| 561 |
} |
| 562 |
PRINT_DESC(" - ", format); |
| 563 |
putchar('\n'); |
| 564 |
return success; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
/* |
| 569 |
* Takes two strings and requires they match (using strcmp). NULL arguments |
| 570 |
* are permitted and handled correctly. |
| 571 |
*/ |
| 572 |
int |
| 573 |
is_string(const char *left, const char *right, const char *format, ...) |
| 574 |
{ |
| 575 |
int success; |
| 576 |
|
| 577 |
fflush(stderr); |
| 578 |
check_diag_files(); |
| 579 |
|
| 580 |
/* Compare the strings, being careful of NULL. */ |
| 581 |
if (left == NULL) |
| 582 |
success = (right == NULL); |
| 583 |
else if (right == NULL) |
| 584 |
success = 0; |
| 585 |
else |
| 586 |
success = (strcmp(left, right) == 0); |
| 587 |
|
| 588 |
/* Report the results. */ |
| 589 |
if (success) |
| 590 |
printf("ok %lu", testnum++); |
| 591 |
else { |
| 592 |
diag(" left: %s", left == NULL ? "(null)" : left); |
| 593 |
diag("right: %s", right == NULL ? "(null)" : right); |
| 594 |
printf("not ok %lu", testnum++); |
| 595 |
_failed++; |
| 596 |
} |
| 597 |
PRINT_DESC(" - ", format); |
| 598 |
putchar('\n'); |
| 599 |
return success; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
/* |
| 604 |
* Takes two unsigned longs and requires they match. On failure, reports them |
| 605 |
* in hex. |
| 606 |
*/ |
| 607 |
int |
| 608 |
is_hex(unsigned long left, unsigned long right, const char *format, ...) |
| 609 |
{ |
| 610 |
int success; |
| 611 |
|
| 612 |
fflush(stderr); |
| 613 |
check_diag_files(); |
| 614 |
success = (left == right); |
| 615 |
if (success) |
| 616 |
printf("ok %lu", testnum++); |
| 617 |
else { |
| 618 |
diag(" left: %lx", (unsigned long) left); |
| 619 |
diag("right: %lx", (unsigned long) right); |
| 620 |
printf("not ok %lu", testnum++); |
| 621 |
_failed++; |
| 622 |
} |
| 623 |
PRINT_DESC(" - ", format); |
| 624 |
putchar('\n'); |
| 625 |
return success; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
/* |
| 630 |
* Takes pointers to a regions of memory and requires that len bytes from each |
| 631 |
* match. Otherwise reports any bytes which didn't match. |
| 632 |
*/ |
| 633 |
int |
| 634 |
is_blob(const void *left, const void *right, size_t len, const char *format, |
| 635 |
...) |
| 636 |
{ |
| 637 |
int success; |
| 638 |
size_t i; |
| 639 |
|
| 640 |
fflush(stderr); |
| 641 |
check_diag_files(); |
| 642 |
success = (memcmp(left, right, len) == 0); |
| 643 |
if (success) |
| 644 |
printf("ok %lu", testnum++); |
| 645 |
else { |
| 646 |
const unsigned char *left_c = (const unsigned char *) left; |
| 647 |
const unsigned char *right_c = (const unsigned char *) right; |
| 648 |
|
| 649 |
for (i = 0; i < len; i++) { |
| 650 |
if (left_c[i] != right_c[i]) |
| 651 |
diag("offset %lu: left %02x, right %02x", (unsigned long) i, |
| 652 |
left_c[i], right_c[i]); |
| 653 |
} |
| 654 |
printf("not ok %lu", testnum++); |
| 655 |
_failed++; |
| 656 |
} |
| 657 |
PRINT_DESC(" - ", format); |
| 658 |
putchar('\n'); |
| 659 |
return success; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
/* |
| 664 |
* Bail out with an error. |
| 665 |
*/ |
| 666 |
void |
| 667 |
bail(const char *format, ...) |
| 668 |
{ |
| 669 |
va_list args; |
| 670 |
|
| 671 |
_aborted = 1; |
| 672 |
fflush(stderr); |
| 673 |
check_diag_files(); |
| 674 |
fflush(stdout); |
| 675 |
printf("Bail out! "); |
| 676 |
va_start(args, format); |
| 677 |
vprintf(format, args); |
| 678 |
va_end(args); |
| 679 |
printf("\n"); |
| 680 |
exit(255); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
/* |
| 685 |
* Bail out with an error, appending strerror(errno). |
| 686 |
*/ |
| 687 |
void |
| 688 |
sysbail(const char *format, ...) |
| 689 |
{ |
| 690 |
va_list args; |
| 691 |
int oerrno = errno; |
| 692 |
|
| 693 |
_aborted = 1; |
| 694 |
fflush(stderr); |
| 695 |
check_diag_files(); |
| 696 |
fflush(stdout); |
| 697 |
printf("Bail out! "); |
| 698 |
va_start(args, format); |
| 699 |
vprintf(format, args); |
| 700 |
va_end(args); |
| 701 |
printf(": %s\n", strerror(oerrno)); |
| 702 |
exit(255); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
/* |
| 707 |
* Report a diagnostic to stderr. Always returns 1 to allow embedding in |
| 708 |
* compound statements. |
| 709 |
*/ |
| 710 |
int |
| 711 |
diag(const char *format, ...) |
| 712 |
{ |
| 713 |
va_list args; |
| 714 |
|
| 715 |
fflush(stderr); |
| 716 |
check_diag_files(); |
| 717 |
fflush(stdout); |
| 718 |
printf("# "); |
| 719 |
va_start(args, format); |
| 720 |
vprintf(format, args); |
| 721 |
va_end(args); |
| 722 |
printf("\n"); |
| 723 |
return 1; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
/* |
| 728 |
* Report a diagnostic to stderr, appending strerror(errno). Always returns 1 |
| 729 |
* to allow embedding in compound statements. |
| 730 |
*/ |
| 731 |
int |
| 732 |
sysdiag(const char *format, ...) |
| 733 |
{ |
| 734 |
va_list args; |
| 735 |
int oerrno = errno; |
| 736 |
|
| 737 |
fflush(stderr); |
| 738 |
check_diag_files(); |
| 739 |
fflush(stdout); |
| 740 |
printf("# "); |
| 741 |
va_start(args, format); |
| 742 |
vprintf(format, args); |
| 743 |
va_end(args); |
| 744 |
printf(": %s\n", strerror(oerrno)); |
| 745 |
return 1; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
/* |
| 750 |
* Register a new file for diag_file processing. |
| 751 |
*/ |
| 752 |
void |
| 753 |
diag_file_add(const char *name) |
| 754 |
{ |
| 755 |
struct diag_file *file, *prev; |
| 756 |
|
| 757 |
file = bcalloc_type(1, struct diag_file); |
| 758 |
file->name = bstrdup(name); |
| 759 |
file->file = fopen(file->name, "r"); |
| 760 |
if (file->file == NULL) |
| 761 |
sysbail("cannot open %s", name); |
| 762 |
file->buffer = bcalloc_type(BUFSIZ, char); |
| 763 |
file->bufsize = BUFSIZ; |
| 764 |
if (diag_files == NULL) |
| 765 |
diag_files = file; |
| 766 |
else { |
| 767 |
for (prev = diag_files; prev->next != NULL; prev = prev->next) |
| 768 |
; |
| 769 |
prev->next = file; |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
/* |
| 775 |
* Remove a file from diag_file processing. If the file is not found, do |
| 776 |
* nothing, since there are some situations where it can be removed twice |
| 777 |
* (such as if it's removed from a cleanup function, since cleanup functions |
| 778 |
* are called after freeing all the diag_files). |
| 779 |
*/ |
| 780 |
void |
| 781 |
diag_file_remove(const char *name) |
| 782 |
{ |
| 783 |
struct diag_file *file; |
| 784 |
struct diag_file **prev = &diag_files; |
| 785 |
|
| 786 |
for (file = diag_files; file != NULL; file = file->next) { |
| 787 |
if (strcmp(file->name, name) == 0) { |
| 788 |
*prev = file->next; |
| 789 |
fclose(file->file); |
| 790 |
free(file->name); |
| 791 |
free(file->buffer); |
| 792 |
free(file); |
| 793 |
return; |
| 794 |
} |
| 795 |
prev = &file->next; |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
/* |
| 801 |
* Allocate cleared memory, reporting a fatal error with bail on failure. |
| 802 |
*/ |
| 803 |
void * |
| 804 |
bcalloc(size_t n, size_t size) |
| 805 |
{ |
| 806 |
void *p; |
| 807 |
|
| 808 |
p = calloc(n, size); |
| 809 |
if (p == NULL) |
| 810 |
sysbail("failed to calloc %lu", (unsigned long) (n * size)); |
| 811 |
return p; |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
/* |
| 816 |
* Allocate memory, reporting a fatal error with bail on failure. |
| 817 |
*/ |
| 818 |
void * |
| 819 |
bmalloc(size_t size) |
| 820 |
{ |
| 821 |
void *p; |
| 822 |
|
| 823 |
p = malloc(size); |
| 824 |
if (p == NULL) |
| 825 |
sysbail("failed to malloc %lu", (unsigned long) size); |
| 826 |
return p; |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
/* |
| 831 |
* Reallocate memory, reporting a fatal error with bail on failure. |
| 832 |
*/ |
| 833 |
void * |
| 834 |
brealloc(void *p, size_t size) |
| 835 |
{ |
| 836 |
p = realloc(p, size); |
| 837 |
if (p == NULL) |
| 838 |
sysbail("failed to realloc %lu bytes", (unsigned long) size); |
| 839 |
return p; |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
/* |
| 844 |
* The same as brealloc, but determine the size by multiplying an element |
| 845 |
* count by a size, similar to calloc. The multiplication is checked for |
| 846 |
* integer overflow. |
| 847 |
* |
| 848 |
* We should technically use SIZE_MAX here for the overflow check, but |
| 849 |
* SIZE_MAX is C99 and we're only assuming C89 + SUSv3, which does not |
| 850 |
* guarantee that it exists. They do guarantee that UINT_MAX exists, and we |
| 851 |
* can assume that UINT_MAX <= SIZE_MAX. |
| 852 |
* |
| 853 |
* (In theory, C89 and C99 permit size_t to be smaller than unsigned int, but |
| 854 |
* I disbelieve in the existence of such systems and they will have to cope |
| 855 |
* without overflow checks.) |
| 856 |
*/ |
| 857 |
void * |
| 858 |
breallocarray(void *p, size_t n, size_t size) |
| 859 |
{ |
| 860 |
if (n > 0 && UINT_MAX / n <= size) |
| 861 |
bail("reallocarray too large"); |
| 862 |
if (n == 0) |
| 863 |
n = 1; |
| 864 |
p = realloc(p, n * size); |
| 865 |
if (p == NULL) |
| 866 |
sysbail("failed to realloc %lu bytes", (unsigned long) (n * size)); |
| 867 |
return p; |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
/* |
| 872 |
* Copy a string, reporting a fatal error with bail on failure. |
| 873 |
*/ |
| 874 |
char * |
| 875 |
bstrdup(const char *s) |
| 876 |
{ |
| 877 |
char *p; |
| 878 |
size_t len; |
| 879 |
|
| 880 |
len = strlen(s) + 1; |
| 881 |
p = (char *) malloc(len); |
| 882 |
if (p == NULL) |
| 883 |
sysbail("failed to strdup %lu bytes", (unsigned long) len); |
| 884 |
memcpy(p, s, len); |
| 885 |
return p; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
/* |
| 890 |
* Copy up to n characters of a string, reporting a fatal error with bail on |
| 891 |
* failure. Don't use the system strndup function, since it may not exist and |
| 892 |
* the TAP library doesn't assume any portability support. |
| 893 |
*/ |
| 894 |
char * |
| 895 |
bstrndup(const char *s, size_t n) |
| 896 |
{ |
| 897 |
const char *p; |
| 898 |
char *copy; |
| 899 |
size_t length; |
| 900 |
|
| 901 |
/* Don't assume that the source string is nul-terminated. */ |
| 902 |
for (p = s; (size_t) (p - s) < n && *p != '\0'; p++) |
| 903 |
; |
| 904 |
length = (size_t) (p - s); |
| 905 |
copy = (char *) malloc(length + 1); |
| 906 |
if (copy == NULL) |
| 907 |
sysbail("failed to strndup %lu bytes", (unsigned long) length); |
| 908 |
memcpy(copy, s, length); |
| 909 |
copy[length] = '\0'; |
| 910 |
return copy; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
/* |
| 915 |
* Locate a test file. Given the partial path to a file, look under |
| 916 |
* C_TAP_BUILD and then C_TAP_SOURCE for the file and return the full path to |
| 917 |
* the file. Returns NULL if the file doesn't exist. A non-NULL return |
| 918 |
* should be freed with test_file_path_free(). |
| 919 |
*/ |
| 920 |
char * |
| 921 |
test_file_path(const char *file) |
| 922 |
{ |
| 923 |
char *base; |
| 924 |
char *path = NULL; |
| 925 |
const char *envs[] = {"C_TAP_BUILD", "C_TAP_SOURCE", NULL}; |
| 926 |
int i; |
| 927 |
|
| 928 |
for (i = 0; envs[i] != NULL; i++) { |
| 929 |
base = getenv(envs[i]); |
| 930 |
if (base == NULL) |
| 931 |
continue; |
| 932 |
path = concat(base, "/", file, (const char *) 0); |
| 933 |
if (access(path, R_OK) == 0) |
| 934 |
break; |
| 935 |
free(path); |
| 936 |
path = NULL; |
| 937 |
} |
| 938 |
return path; |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
/* |
| 943 |
* Free a path returned from test_file_path(). This function exists primarily |
| 944 |
* for Windows, where memory must be freed from the same library domain that |
| 945 |
* it was allocated from. |
| 946 |
*/ |
| 947 |
void |
| 948 |
test_file_path_free(char *path) |
| 949 |
{ |
| 950 |
free(path); |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
/* |
| 955 |
* Create a temporary directory, tmp, under C_TAP_BUILD if set and the current |
| 956 |
* directory if it does not. Returns the path to the temporary directory in |
| 957 |
* newly allocated memory, and calls bail on any failure. The return value |
| 958 |
* should be freed with test_tmpdir_free. |
| 959 |
* |
| 960 |
* This function uses sprintf because it attempts to be independent of all |
| 961 |
* other portability layers. The use immediately after a memory allocation |
| 962 |
* should be safe without using snprintf or strlcpy/strlcat. |
| 963 |
*/ |
| 964 |
char * |
| 965 |
test_tmpdir(void) |
| 966 |
{ |
| 967 |
const char *build; |
| 968 |
char *path = NULL; |
| 969 |
|
| 970 |
build = getenv("C_TAP_BUILD"); |
| 971 |
if (build == NULL) |
| 972 |
build = "."; |
| 973 |
path = concat(build, "/tmp", (const char *) 0); |
| 974 |
if (access(path, X_OK) < 0) |
| 975 |
if (mkdir(path, 0777) < 0) |
| 976 |
sysbail("error creating temporary directory %s", path); |
| 977 |
return path; |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
/* |
| 982 |
* Free a path returned from test_tmpdir() and attempt to remove the |
| 983 |
* directory. If we can't delete the directory, don't worry; something else |
| 984 |
* that hasn't yet cleaned up may still be using it. |
| 985 |
*/ |
| 986 |
void |
| 987 |
test_tmpdir_free(char *path) |
| 988 |
{ |
| 989 |
if (path != NULL) |
| 990 |
rmdir(path); |
| 991 |
free(path); |
| 992 |
} |
| 993 |
|
| 994 |
static void |
| 995 |
register_cleanup(test_cleanup_func func, |
| 996 |
test_cleanup_func_with_data func_with_data, void *data) |
| 997 |
{ |
| 998 |
struct cleanup_func *cleanup, **last; |
| 999 |
|
| 1000 |
cleanup = bcalloc_type(1, struct cleanup_func); |
| 1001 |
cleanup->func = func; |
| 1002 |
cleanup->func_with_data = func_with_data; |
| 1003 |
cleanup->data = data; |
| 1004 |
cleanup->next = NULL; |
| 1005 |
last = &cleanup_funcs; |
| 1006 |
while (*last != NULL) |
| 1007 |
last = &(*last)->next; |
| 1008 |
*last = cleanup; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/* |
| 1012 |
* Register a cleanup function that is called when testing ends. All such |
| 1013 |
* registered functions will be run by finish. |
| 1014 |
*/ |
| 1015 |
void |
| 1016 |
test_cleanup_register(test_cleanup_func func) |
| 1017 |
{ |
| 1018 |
register_cleanup(func, NULL, NULL); |
| 1019 |
} |
| 1020 |
|
| 1021 |
/* |
| 1022 |
* Same as above, but also allows an opaque pointer to be passed to the cleanup |
| 1023 |
* function. |
| 1024 |
*/ |
| 1025 |
void |
| 1026 |
test_cleanup_register_with_data(test_cleanup_func_with_data func, void *data) |
| 1027 |
{ |
| 1028 |
register_cleanup(NULL, func, data); |
| 1029 |
} |