1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
|
/*
* Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2003 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: t_api.c,v 1.48.2.1.2.10 2006/01/04 23:50:21 marka Exp $ */
#include <config.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <isc/boolean.h>
#include <isc/commandline.h>
#include <isc/print.h>
#include <isc/string.h>
#include <isc/mem.h>
#include <dns/compress.h>
#include <dns/result.h>
#include "include/tests/t_api.h"
static const char *Usage =
"\t-a : run all tests\n"
"\t-b <dir> : chdir to dir before running tests"
"\t-c <config_file> : use specified config file\n"
"\t-d <debug_level> : set debug level to debug_level\n"
"\t-h : print test info\n"
"\t-u : print usage info\n"
"\t-n <test_name> : run specified test name\n"
"\t-t <test_number> : run specified test number\n"
"\t-x : don't execute tests in a subproc\n"
"\t-q <timeout> : use 'timeout' as the timeout value\n";
/*
* -a --> run all tests
* -b dir --> chdir to dir before running tests
* -c config --> use config file 'config'
* -d --> turn on api debugging
* -h --> print out available test names
* -u --> print usage info
* -n name --> run test named name
* -tn --> run test n
* -x --> don't execute testcases in a subproc
* -q timeout --> use 'timeout' as the timeout value
*/
#define T_MAXTESTS 256 /* must be 0 mod 8 */
#define T_MAXENV 256
#define T_DEFAULT_CONFIG "t_config"
#define T_BUFSIZ 256
#define T_BIGBUF 4096
#define T_TCTOUT 60
int T_debug;
int T_timeout;
pid_t T_pid;
static const char * T_config;
static char T_tvec[T_MAXTESTS / 8];
static char * T_env[T_MAXENV + 1];
static char T_buf[T_BIGBUF];
static char * T_dir;
static int
t_initconf(const char *path);
static int
t_dumpconf(const char *path);
static int
t_putinfo(const char *key, const char *info);
static char *
t_getdate(char *buf, size_t buflen);
static void
printhelp(void);
static void
printusage(void);
static int T_int;
static void
t_sighandler(int sig) {
T_int = sig;
}
int
main(int argc, char **argv) {
int c;
int tnum;
int subprocs;
pid_t deadpid;
int status;
int len;
isc_boolean_t first;
testspec_t *pts;
struct sigaction sa;
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
first = ISC_TRUE;
subprocs = 1;
T_timeout = T_TCTOUT;
/*
* -a option is now default.
*/
memset(T_tvec, 0xffff, sizeof(T_tvec));
/*
* Parse args.
*/
while ((c = isc_commandline_parse(argc, argv, ":at:c:d:n:huxq:b:"))
!= -1) {
if (c == 'a') {
/*
* Flag all tests to be run.
*/
memset(T_tvec, 0xffff, sizeof(T_tvec));
}
else if (c == 'b') {
T_dir = isc_commandline_argument;
}
else if (c == 't') {
tnum = atoi(isc_commandline_argument);
if ((tnum > 0) && (tnum < T_MAXTESTS)) {
if (first) {
/*
* Turn off effect of -a default
* and allow multiple -t and -n
* options.
*/
memset(T_tvec, 0, sizeof(T_tvec));
first = ISC_FALSE;
}
/*
* Flag test tnum to be run.
*/
tnum -= 1;
T_tvec[tnum / 8] |= (0x01 << (tnum % 8));
}
}
else if (c == 'c') {
T_config = isc_commandline_argument;
}
else if (c == 'd') {
T_debug = atoi(isc_commandline_argument);
}
else if (c == 'n') {
pts = &T_testlist[0];
tnum = 0;
while (pts->pfv != NULL) {
if (! strcmp(pts->func_name,
isc_commandline_argument)) {
if (first) {
memset(T_tvec, 0,
sizeof(T_tvec));
first = ISC_FALSE;
}
T_tvec[tnum/8] |= (0x01 << (tnum%8));
break;
}
++pts;
++tnum;
}
if (pts->pfv == NULL) {
fprintf(stderr, "no such test %s\n",
isc_commandline_argument);
exit(1);
}
}
else if (c == 'h') {
printhelp();
exit(0);
}
else if (c == 'u') {
printusage();
exit(0);
}
else if (c == 'x') {
subprocs = 0;
}
else if (c == 'q') {
T_timeout = atoi(isc_commandline_argument);
}
else if (c == ':') {
fprintf(stderr, "Option -%c requires an argument\n",
isc_commandline_option);
exit(1);
}
else if (c == '?') {
fprintf(stderr, "Unrecognized option -%c\n",
isc_commandline_option);
exit(1);
}
}
/*
* Set cwd.
*/
if (T_dir != NULL)
(void) chdir(T_dir);
/*
* We don't want buffered output.
*/
(void)setbuf(stdout, NULL);
(void)setbuf(stderr, NULL);
/*
* Setup signals.
*/
sa.sa_flags = 0;
sigfillset(&sa.sa_mask);
#ifdef SIGCHLD
/*
* This is mostly here for NetBSD's pthread implementation, until
* people catch up to the latest unproven-pthread package.
*/
sa.sa_handler = SIG_DFL;
(void)sigaction(SIGCHLD, &sa, NULL);
#endif
sa.sa_handler = t_sighandler;
(void)sigaction(SIGINT, &sa, NULL);
(void)sigaction(SIGALRM, &sa, NULL);
/*
* Output start stanza to journal.
*/
snprintf(T_buf, sizeof(T_buf), "%s:", argv[0]);
len = strlen(T_buf);
(void) t_getdate(T_buf + len, T_BIGBUF - len);
t_putinfo("S", T_buf);
/*
* Setup the test environment using the config file.
*/
if (T_config == NULL)
T_config = T_DEFAULT_CONFIG;
t_initconf(T_config);
if (T_debug)
t_dumpconf(T_config);
/*
* Now invoke all the test cases.
*/
tnum = 0;
pts = &T_testlist[0];
while (*pts->pfv != NULL) {
if (T_tvec[tnum / 8] & (0x01 << (tnum % 8))) {
if (subprocs) {
T_pid = fork();
if (T_pid == 0) {
(*pts->pfv)();
exit(0);
} else if (T_pid > 0) {
T_int = 0;
sa.sa_handler = t_sighandler;
(void)sigaction(SIGALRM, &sa, NULL);
alarm(T_timeout);
deadpid = (pid_t) -1;
while (deadpid != T_pid) {
deadpid =
waitpid(T_pid, &status, 0);
if (deadpid == T_pid) {
if (WIFSIGNALED(status)) {
if (WTERMSIG(status) ==
SIGTERM)
t_info(
"the test case timed out\n");
else
t_info(
"the test case caused exception %d\n",
WTERMSIG(status));
t_result(T_UNRESOLVED);
}
} else if ((deadpid == -1) &&
(errno == EINTR) &&
T_int) {
kill(T_pid, SIGTERM);
T_int = 0;
}
else if ((deadpid == -1) &&
((errno == ECHILD) ||
(errno == ESRCH)))
break;
}
alarm(0);
sa.sa_handler = SIG_IGN;
(void)sigaction(SIGALRM, &sa, NULL);
} else {
t_info("fork failed, errno == %d\n",
errno);
t_result(T_UNRESOLVED);
}
}
else {
(*pts->pfv)();
}
}
++pts;
++tnum;
}
snprintf(T_buf, sizeof(T_buf), "%s:", argv[0]);
len = strlen(T_buf);
(void) t_getdate(T_buf + len, T_BIGBUF - len);
t_putinfo("E", T_buf);
return(0);
}
void
t_assert(const char *component, int anum, int class, const char *what, ...) {
va_list args;
(void)printf("T:%s:%d:%s\n", component, anum, class == T_REQUIRED ?
"A" : "C");
/*
* Format text to a buffer.
*/
va_start(args, what);
(void)vsnprintf(T_buf, sizeof(T_buf), what, args);
va_end(args);
(void)t_putinfo("A", T_buf);
(void)printf("\n");
}
void
t_info(const char *format, ...) {
va_list args;
va_start(args, format);
(void) vsnprintf(T_buf, sizeof(T_buf), format, args);
va_end(args);
(void) t_putinfo("I", T_buf);
}
void
t_result(int result) {
const char *p;
switch (result) {
case T_PASS:
p = "PASS";
break;
case T_FAIL:
p = "FAIL";
break;
case T_UNRESOLVED:
p = "UNRESOLVED";
break;
case T_UNSUPPORTED:
p = "UNSUPPORTED";
break;
case T_UNTESTED:
p = "UNTESTED";
break;
case T_THREADONLY:
p = "THREADONLY";
break;
default:
p = "UNKNOWN";
break;
}
printf("R:%s\n", p);
}
char *
t_getenv(const char *name) {
char *n;
char **p;
size_t len;
n = NULL;
if (name && *name) {
p = &T_env[0];
len = strlen(name);
while (*p != NULL) {
if (strncmp(*p, name, len) == 0) {
if ( *(*p + len) == '=') {
n = *p + len + 1;
break;
}
}
++p;
}
}
return(n);
}
/*
*
* Read in the config file at path, initializing T_env.
*
* note: no format checking for now ...
*
*/
static int
t_initconf(const char *path) {
int n;
int rval;
char **p;
FILE *fp;
rval = -1;
fp = fopen(path, "r");
if (fp != NULL) {
n = 0;
p = &T_env[0];
while (n < T_MAXENV) {
*p = t_fgetbs(fp);
if (*p == NULL)
break;
if ((**p == '#') || (strchr(*p, '=') == NULL)) {
/*
* Skip comments and other junk.
*/
(void)free(*p);
continue;
}
++p; ++n;
}
(void)fclose(fp);
rval = 0;
}
return (rval);
}
/*
*
* Dump T_env to stdout.
*
*/
static int
t_dumpconf(const char *path) {
int rval;
char **p;
FILE *fp;
rval = -1;
fp = fopen(path, "r");
if (fp != NULL) {
p = &T_env[0];
while (*p != NULL) {
printf("C:%s\n", *p);
++p;
}
(void) fclose(fp);
rval = 0;
}
return(rval);
}
/*
*
* Read a newline or EOF terminated string from fp.
* On success:
* return a malloc'd buf containing the string with
* the newline converted to a '\0'.
* On error:
* return NULL.
*
* Caller is responsible for freeing buf.
*
*/
char *
t_fgetbs(FILE *fp) {
int c;
size_t n;
size_t size;
char *buf;
char *p;
n = 0;
size = T_BUFSIZ;
buf = (char *) malloc(T_BUFSIZ * sizeof(char));
if (buf != NULL) {
p = buf;
while ((c = fgetc(fp)) != EOF) {
if (c == '\n')
break;
*p++ = c;
++n;
if ( n >= size ) {
size += T_BUFSIZ;
buf = (char *)realloc(buf,
size * sizeof(char));
if (buf == NULL)
break;
p = buf + n;
}
}
*p = '\0';
if (c == EOF && n == 0U) {
free(buf);
return (NULL);
}
return (buf);
} else {
fprintf(stderr, "malloc failed %d", errno);
return(NULL);
}
}
/*
*
* Put info to log, using key.
* For now, just dump it out.
* Later format into pretty lines.
*
*/
static int
t_putinfo(const char *key, const char *info) {
int rval;
/*
* For now.
*/
rval = printf("%s:%s", key, info);
return(rval);
}
static char *
t_getdate(char *buf, size_t buflen) {
size_t n;
time_t t;
struct tm *p;
t = time(NULL);
p = localtime(&t);
n = strftime(buf, buflen - 1, "%A %d %B %H:%M:%S %Y\n", p);
return(n != 0U ? buf : NULL);
}
/*
* Some generally used utilities.
*/
struct dns_errormap {
isc_result_t result;
const char *text;
} dns_errormap[] = {
{ ISC_R_SUCCESS, "ISC_R_SUCCESS" },
{ ISC_R_EXISTS, "ISC_R_EXISTS" },
{ ISC_R_NOTFOUND, "ISC_R_NOTFOUND" },
{ ISC_R_NOSPACE, "ISC_R_NOSPACE" },
{ ISC_R_UNEXPECTED, "ISC_R_UNEXPECTED" },
{ ISC_R_UNEXPECTEDEND, "ISC_R_UNEXPECTEDEND" },
{ ISC_R_RANGE, "ISC_R_RANGE" },
{ DNS_R_LABELTOOLONG, "DNS_R_LABELTOOLONG" },
{ DNS_R_BADESCAPE, "DNS_R_BADESCAPE" },
/* { DNS_R_BADBITSTRING, "DNS_R_BADBITSTRING" }, */
/* { DNS_R_BITSTRINGTOOLONG, "DNS_R_BITSTRINGTOOLONG"}, */
{ DNS_R_EMPTYLABEL, "DNS_R_EMPTYLABEL" },
{ DNS_R_BADDOTTEDQUAD, "DNS_R_BADDOTTEDQUAD" },
{ DNS_R_UNKNOWN, "DNS_R_UNKNOWN" },
{ DNS_R_BADLABELTYPE, "DNS_R_BADLABELTYPE" },
{ DNS_R_BADPOINTER, "DNS_R_BADPOINTER" },
{ DNS_R_TOOMANYHOPS, "DNS_R_TOOMANYHOPS" },
{ DNS_R_DISALLOWED, "DNS_R_DISALLOWED" },
{ DNS_R_EXTRATOKEN, "DNS_R_EXTRATOKEN" },
{ DNS_R_EXTRADATA, "DNS_R_EXTRADATA" },
{ DNS_R_TEXTTOOLONG, "DNS_R_TEXTTOOLONG" },
{ DNS_R_SYNTAX, "DNS_R_SYNTAX" },
{ DNS_R_BADCKSUM, "DNS_R_BADCKSUM" },
{ DNS_R_BADAAAA, "DNS_R_BADAAAA" },
{ DNS_R_NOOWNER, "DNS_R_NOOWNER" },
{ DNS_R_NOTTL, "DNS_R_NOTTL" },
{ DNS_R_BADCLASS, "DNS_R_BADCLASS" },
{ DNS_R_PARTIALMATCH, "DNS_R_PARTIALMATCH" },
{ DNS_R_NEWORIGIN, "DNS_R_NEWORIGIN" },
{ DNS_R_UNCHANGED, "DNS_R_UNCHANGED" },
{ DNS_R_BADTTL, "DNS_R_BADTTL" },
{ DNS_R_NOREDATA, "DNS_R_NOREDATA" },
{ DNS_R_CONTINUE, "DNS_R_CONTINUE" },
{ DNS_R_DELEGATION, "DNS_R_DELEGATION" },
{ DNS_R_GLUE, "DNS_R_GLUE" },
{ DNS_R_DNAME, "DNS_R_DNAME" },
{ DNS_R_CNAME, "DNS_R_CNAME" },
{ DNS_R_NXDOMAIN, "DNS_R_NXDOMAIN" },
{ DNS_R_NXRRSET, "DNS_R_NXRRSET" },
{ DNS_R_BADDB, "DNS_R_BADDB" },
{ DNS_R_ZONECUT, "DNS_R_ZONECUT" },
{ DNS_R_NOTZONETOP, "DNS_R_NOTZONETOP" },
{ DNS_R_SEENINCLUDE, "DNS_R_SEENINCLUDE" },
{ DNS_R_SINGLETON, "DNS_R_SINGLETON" },
{ (isc_result_t)0, NULL }
};
isc_result_t
t_dns_result_fromtext(char *name) {
isc_result_t result;
struct dns_errormap *pmap;
result = ISC_R_UNEXPECTED;
pmap = dns_errormap;
while (pmap->text != NULL) {
if (strcmp(name, pmap->text) == 0)
break;
++pmap;
}
if (pmap->text != NULL)
result = pmap->result;
return (result);
}
struct dc_method_map {
unsigned int dc_method;
const char *text;
} dc_method_map[] = {
{ DNS_COMPRESS_NONE, "DNS_COMPRESS_NONE" },
{ DNS_COMPRESS_GLOBAL14, "DNS_COMPRESS_GLOBAL14" },
{ DNS_COMPRESS_ALL, "DNS_COMPRESS_ALL" },
{ 0, NULL }
};
unsigned int
t_dc_method_fromtext(char *name) {
unsigned int dc_method;
struct dc_method_map *pmap;
dc_method = DNS_COMPRESS_NONE;
pmap = dc_method_map;
while (pmap->text != NULL) {
if (strcmp(name, pmap->text) == 0)
break;
++pmap;
}
if (pmap->text != NULL)
dc_method = pmap->dc_method;
return(dc_method);
}
int
t_bustline(char *line, char **toks) {
int cnt;
char *p;
cnt = 0;
if (line && *line) {
while ((p = strtok(line, "\t")) && (cnt < T_MAXTOKS)) {
*toks++ = p;
line = NULL;
++cnt;
}
}
return(cnt);
}
static void
printhelp(void) {
int cnt;
testspec_t *pts;
cnt = 1;
pts = &T_testlist[0];
printf("Available tests:\n");
while (pts->func_name) {
printf("\t%d\t%s\n", cnt, pts->func_name);
++pts;
++cnt;
}
}
static void
printusage(void) {
printf("Usage:\n%s\n", Usage);
}
int
t_eval(const char *filename, int (*func)(char **), int nargs) {
FILE *fp;
char *p;
int line;
int cnt;
int result;
int nfails;
int nprobs;
int npass;
char *tokens[T_MAXTOKS + 1];
npass = 0;
nfails = 0;
nprobs = 0;
fp = fopen(filename, "r");
if (fp != NULL) {
line = 0;
while ((p = t_fgetbs(fp)) != NULL) {
++line;
/*
* Skip comment lines.
*/
if ((isspace((unsigned char)*p)) || (*p == '#')) {
(void)free(p);
continue;
}
cnt = t_bustline(p, tokens);
if (cnt == nargs) {
result = func(tokens);
switch (result) {
case T_PASS:
++npass;
break;
case T_FAIL:
++nfails;
break;
case T_UNTESTED:
break;
default:
++nprobs;
break;
}
} else {
t_info("bad format in %s at line %d\n",
filename, line);
++nprobs;
}
(void)free(p);
}
(void)fclose(fp);
} else {
t_info("Missing datafile %s\n", filename);
++nprobs;
}
result = T_UNRESOLVED;
if (nfails == 0 && nprobs == 0 && npass > 0)
result = T_PASS;
else if (nfails > 0)
result = T_FAIL;
else if (npass == 0)
result = T_UNTESTED;
return (result);
}
|