t_api.c 16 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
2
 * Copyright (C) 2004, 2005, 2007-2010, 2013, 2014, 2016  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2003  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
Bob Halley's avatar
Bob Halley committed
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * 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.
Bob Halley's avatar
Bob Halley committed
16 17
 */

18
/* $Id: t_api.c,v 1.68 2010/12/21 04:20:23 marka Exp $ */
19 20

/*! \file */
David Lawrence's avatar
David Lawrence committed
21

22
#include <config.h>
David Lawrence's avatar
David Lawrence committed
23

24 25 26 27 28 29 30 31 32
#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>
Bob Halley's avatar
Bob Halley committed
33

34
#ifndef WIN32
35
#include <sys/wait.h>
36 37 38
#else
#include <direct.h>
#endif
39

40 41
#include <isc/boolean.h>
#include <isc/commandline.h>
42
#include <isc/print.h>
43
#include <isc/string.h>
44
#include <isc/mem.h>
45

46 47 48
#include <dns/compress.h>
#include <dns/result.h>

49
#include "include/tests/t_api.h"
Bob Halley's avatar
Bob Halley committed
50

David Lawrence's avatar
David Lawrence committed
51 52
static const char *Usage =
		"\t-a               : run all tests\n"
53 54 55 56 57 58 59 60 61
		"\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";
62
/*!<
Bob Halley's avatar
Bob Halley committed
63
 *		-a		-->	run all tests
64
 *		-b dir		-->	chdir to dir before running tests
Bob Halley's avatar
Bob Halley committed
65 66 67
 *		-c config	-->	use config file 'config'
 *		-d		-->	turn on api debugging
 *		-h		-->	print out available test names
William King's avatar
William King committed
68 69 70
 *		-u		-->	print usage info
 *		-n name		-->	run test named name
 *		-tn		-->	run test n
71 72
 *		-x		-->	don't execute testcases in a subproc
 *		-q timeout	-->	use 'timeout' as the timeout value
Bob Halley's avatar
Bob Halley committed
73 74
 */

75
#define	T_MAXTESTS		256	/*% must be 0 mod 8 */
Bob Halley's avatar
Bob Halley committed
76 77
#define	T_MAXENV		256
#define	T_DEFAULT_CONFIG	"t_config"
78
#define	T_BUFSIZ		256
Bob Halley's avatar
Bob Halley committed
79
#define	T_BIGBUF		4096
80 81

#define	T_TCTOUT		60
Bob Halley's avatar
Bob Halley committed
82

David Lawrence's avatar
David Lawrence committed
83 84 85 86 87 88 89 90
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;
91 92 93
#ifdef WIN32
static testspec_t	T_testlist[T_MAXTESTS];
#endif
David Lawrence's avatar
David Lawrence committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

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);
William King's avatar
William King committed
112 113

static int	T_int;
114 115

static void
116 117
t_sighandler(int sig) {
	T_int = sig;
118
}
Bob Halley's avatar
Bob Halley committed
119 120

int
121 122 123 124 125 126
#ifndef WIN32
main(int argc, char **argv)
#else
t_main(int argc, char **argv)
#endif
{
William King's avatar
William King committed
127 128
	int			c;
	int			tnum;
129
#ifndef WIN32
William King's avatar
William King committed
130 131 132
	int			subprocs;
	pid_t			deadpid;
	int			status;
133
#endif
William King's avatar
William King committed
134
	int			len;
135
	isc_boolean_t		first;
William King's avatar
William King committed
136
	testspec_t		*pts;
137
#ifndef WIN32
William King's avatar
William King committed
138
	struct sigaction	sa;
139
#endif
Bob Halley's avatar
Bob Halley committed
140

141
	isc_mem_debugging = ISC_MEM_DEBUGRECORD;
142
	first = ISC_TRUE;
143
#ifndef WIN32
144
	subprocs = 1;
145
#endif
146
	T_timeout = T_TCTOUT;
147

148 149 150
	/*
	 * -a option is now default.
	 */
151
	memset(T_tvec, 0xff, sizeof(T_tvec));
William King's avatar
William King committed
152

153 154 155 156 157
	/*
	 * Parse args.
	 */
	while ((c = isc_commandline_parse(argc, argv, ":at:c:d:n:huxq:b:"))
	       != -1) {
Bob Halley's avatar
Bob Halley committed
158
		if (c == 'a') {
159 160 161
			/*
			 * Flag all tests to be run.
			 */
162
			memset(T_tvec, 0xff, sizeof(T_tvec));
Bob Halley's avatar
Bob Halley committed
163
		}
164 165 166
		else if (c == 'b') {
			T_dir = isc_commandline_argument;
		}
Bob Halley's avatar
Bob Halley committed
167
		else if (c == 't') {
168
			tnum = atoi(isc_commandline_argument);
Bob Halley's avatar
Bob Halley committed
169
			if ((tnum > 0) && (tnum < T_MAXTESTS)) {
170 171
				if (first) {
					/*
172
					 * Turn off effect of -a default
173
					 * and allow multiple -t and -n
174
					 * options.
175 176
					 */
					memset(T_tvec, 0, sizeof(T_tvec));
177
					first = ISC_FALSE;
178
				}
179 180 181
				/*
				 * Flag test tnum to be run.
				 */
Bob Halley's avatar
Bob Halley committed
182 183 184 185 186
				tnum -= 1;
				T_tvec[tnum / 8] |= (0x01 << (tnum % 8));
			}
		}
		else if (c == 'c') {
187
			T_config = isc_commandline_argument;
Bob Halley's avatar
Bob Halley committed
188 189
		}
		else if (c == 'd') {
190
			T_debug = atoi(isc_commandline_argument);
Bob Halley's avatar
Bob Halley committed
191 192 193 194 195
		}
		else if (c == 'n') {
			pts = &T_testlist[0];
			tnum = 0;
			while (pts->pfv != NULL) {
196 197
				if (! strcmp(pts->func_name,
					     isc_commandline_argument)) {
198
					if (first) {
199 200 201
						memset(T_tvec, 0,
						       sizeof(T_tvec));
						first = ISC_FALSE;
202
					}
Bob Halley's avatar
Bob Halley committed
203 204 205 206 207 208 209
					T_tvec[tnum/8] |= (0x01 << (tnum%8));
					break;
				}
				++pts;
				++tnum;
			}
			if (pts->pfv == NULL) {
210 211
				fprintf(stderr, "no such test %s\n",
					isc_commandline_argument);
Bob Halley's avatar
Bob Halley committed
212 213 214 215 216 217 218 219 220 221 222
				exit(1);
			}
		}
		else if (c == 'h') {
			printhelp();
			exit(0);
		}
		else if (c == 'u') {
			printusage();
			exit(0);
		}
223
		else if (c == 'x') {
224
#ifndef WIN32
225
			subprocs = 0;
226
#endif
227 228
		}
		else if (c == 'q') {
229
			T_timeout = atoi(isc_commandline_argument);
230
		}
Bob Halley's avatar
Bob Halley committed
231 232
		else if (c == ':') {
			fprintf(stderr, "Option -%c requires an argument\n",
233
						isc_commandline_option);
Bob Halley's avatar
Bob Halley committed
234 235 236
			exit(1);
		}
		else if (c == '?') {
237 238
			fprintf(stderr, "Unrecognized option -%c\n",
				isc_commandline_option);
Bob Halley's avatar
Bob Halley committed
239 240 241 242
			exit(1);
		}
	}

243 244 245
	/*
	 * Set cwd.
	 */
William King's avatar
William King committed
246

247 248 249 250
	if (T_dir != NULL && chdir(T_dir) != 0) {
		fprintf(stderr, "chdir %s failed\n", T_dir);
		exit(1);
	}
251

252 253 254
	/*
	 * We don't want buffered output.
	 */
William King's avatar
William King committed
255

256 257
	(void)setbuf(stdout, NULL);
	(void)setbuf(stderr, NULL);
258

259 260 261
	/*
	 * Setup signals.
	 */
William King's avatar
William King committed
262

263
#ifndef WIN32
William King's avatar
William King committed
264 265
	sa.sa_flags = 0;
	sigfillset(&sa.sa_mask);
266 267

	sa.sa_handler = t_sighandler;
268 269
	(void)sigaction(SIGINT,  &sa, NULL);
	(void)sigaction(SIGALRM, &sa, NULL);
270
#endif
William King's avatar
William King committed
271

272 273 274
	/*
	 * Output start stanza to journal.
	 */
275

276
	snprintf(T_buf, sizeof(T_buf), "%s:", argv[0]);
277 278
	len = strlen(T_buf);
	(void) t_getdate(T_buf + len, T_BIGBUF - len);
William King's avatar
William King committed
279
	t_putinfo("S", T_buf);
Bob Halley's avatar
Bob Halley committed
280

281 282 283
	/*
	 * Setup the test environment using the config file.
	 */
William King's avatar
William King committed
284

Bob Halley's avatar
Bob Halley committed
285 286 287 288 289 290 291
	if (T_config == NULL)
		T_config = T_DEFAULT_CONFIG;

	t_initconf(T_config);
	if (T_debug)
		t_dumpconf(T_config);

292 293 294
	/*
	 * Now invoke all the test cases.
	 */
William King's avatar
William King committed
295

Bob Halley's avatar
Bob Halley committed
296 297
	tnum = 0;
	pts = &T_testlist[0];
298
	while (pts->pfv != NULL) {
Bob Halley's avatar
Bob Halley committed
299
		if (T_tvec[tnum / 8] & (0x01 << (tnum % 8))) {
300
#ifndef WIN32
301 302 303 304 305
			if (subprocs) {
				T_pid = fork();
				if (T_pid == 0) {
					(*pts->pfv)();
					exit(0);
306
				} else if (T_pid > 0) {
307

William King's avatar
William King committed
308
					T_int = 0;
309
					sa.sa_handler = t_sighandler;
310
					(void)sigaction(SIGALRM, &sa, NULL);
311 312 313 314
					alarm(T_timeout);

					deadpid = (pid_t) -1;
					while (deadpid != T_pid) {
315 316 317 318 319 320 321 322 323 324
					    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(
Automatic Updater's avatar
Automatic Updater committed
325 326
					 "the test case caused exception %d\n",
							     WTERMSIG(status));
327 328 329 330 331 332 333 334 335 336 337 338
							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;
339 340 341
					}

					alarm(0);
342
					sa.sa_handler = SIG_IGN;
343
					(void)sigaction(SIGALRM, &sa, NULL);
344 345 346
				} else {
					t_info("fork failed, errno == %d\n",
					       errno);
347 348 349 350 351 352
					t_result(T_UNRESOLVED);
				}
			}
			else {
				(*pts->pfv)();
			}
353 354 355
#else
			(*pts->pfv)();
#endif
Bob Halley's avatar
Bob Halley committed
356 357 358 359 360
		}
		++pts;
		++tnum;
	}

361
	snprintf(T_buf, sizeof(T_buf), "%s:", argv[0]);
362 363
	len = strlen(T_buf);
	(void) t_getdate(T_buf + len, T_BIGBUF - len);
364 365
	t_putinfo("E", T_buf);

Bob Halley's avatar
Bob Halley committed
366 367 368 369
	return(0);
}

void
370
t_assert(const char *component, int anum, int class, const char *what, ...) {
Bob Halley's avatar
Bob Halley committed
371
	va_list	args;
372
	char buf[T_BIGBUF];
Bob Halley's avatar
Bob Halley committed
373

374 375
	(void)printf("T:%s:%d:%s\n", component, anum, class == T_REQUIRED ?
		     "A" : "C");
Bob Halley's avatar
Bob Halley committed
376

377 378 379
	/*
	 * Format text to a buffer.
	 */
Bob Halley's avatar
Bob Halley committed
380
	va_start(args, what);
381
	(void)vsnprintf(buf, sizeof(buf), what, args);
Bob Halley's avatar
Bob Halley committed
382 383
	va_end(args);

384
	(void)t_putinfo("A", buf);
385
	(void)printf("\n");
Bob Halley's avatar
Bob Halley committed
386 387 388
}

void
389
t_info(const char *format, ...) {
Bob Halley's avatar
Bob Halley committed
390
	va_list	args;
391
	char buf[T_BIGBUF];
Bob Halley's avatar
Bob Halley committed
392 393

	va_start(args, format);
394
	(void) vsnprintf(buf, sizeof(buf), format, args);
Bob Halley's avatar
Bob Halley committed
395
	va_end(args);
396
	(void) t_putinfo("I", buf);
Bob Halley's avatar
Bob Halley committed
397 398 399
}

void
400
t_result(int result) {
David Lawrence's avatar
David Lawrence committed
401
	const char *p;
Bob Halley's avatar
Bob Halley committed
402 403 404 405 406 407 408 409 410 411 412

	switch (result) {
		case T_PASS:
			p = "PASS";
			break;
		case T_FAIL:
			p = "FAIL";
			break;
		case T_UNRESOLVED:
			p = "UNRESOLVED";
			break;
413 414
		case T_SKIPPED:
			p = "SKIPPED";
Bob Halley's avatar
Bob Halley committed
415 416 417 418
			break;
		case T_UNTESTED:
			p = "UNTESTED";
			break;
419 420 421
		case T_THREADONLY:
			p = "THREADONLY";
			break;
422 423 424
		case T_PKCS11ONLY:
			p = "PKCS11ONLY";
			break;
Bob Halley's avatar
Bob Halley committed
425 426 427 428 429 430 431 432
		default:
			p = "UNKNOWN";
			break;
	}
	printf("R:%s\n", p);
}

char *
433
t_getenv(const char *name) {
Bob Halley's avatar
Bob Halley committed
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
	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);
}

/*
 *
459
 * Read in the config file at path, initializing T_env.
Bob Halley's avatar
Bob Halley committed
460 461 462 463 464 465
 *
 * note: no format checking for now ...
 *
 */

static int
David Lawrence's avatar
David Lawrence committed
466
t_initconf(const char *path) {
Bob Halley's avatar
Bob Halley committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

	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)) {
484 485 486 487
				/*
				 * Skip comments and other junk.
				 */
				(void)free(*p);
Bob Halley's avatar
Bob Halley committed
488 489 490 491
				continue;
			}
			++p; ++n;
		}
492
		(void)fclose(fp);
Bob Halley's avatar
Bob Halley committed
493 494 495
		rval = 0;
	}

496
	return (rval);
Bob Halley's avatar
Bob Halley committed
497 498 499 500
}

/*
 *
501
 * Dump T_env to stdout.
Bob Halley's avatar
Bob Halley committed
502 503 504 505
 *
 */

static int
David Lawrence's avatar
David Lawrence committed
506
t_dumpconf(const char *path) {
Bob Halley's avatar
Bob Halley committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	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);
}

/*
 *
527 528
 * Read a newline or EOF terminated string from fp.
 * On success:
Bob Halley's avatar
Bob Halley committed
529 530
 *	return a malloc'd buf containing the string with
 *	the newline converted to a '\0'.
531 532
 * On error:
 *	return NULL.
Bob Halley's avatar
Bob Halley committed
533
 *
534
 * Caller is responsible for freeing buf.
Bob Halley's avatar
Bob Halley committed
535 536 537 538
 *
 */

char *
539
t_fgetbs(FILE *fp) {
Bob Halley's avatar
Bob Halley committed
540 541 542
	int	c;
	size_t	n;
	size_t	size;
543
	char	*buf, *old;
Bob Halley's avatar
Bob Halley committed
544 545
	char	*p;

546 547 548
	n = 0;
	size = T_BUFSIZ;
	old = buf = (char *) malloc(T_BUFSIZ * sizeof(char));
Bob Halley's avatar
Bob Halley committed
549 550 551 552 553

	if (buf != NULL) {
		p = buf;
		while ((c = fgetc(fp)) != EOF) {

554
			if ((c == '\r') || (c == '\n'))
Bob Halley's avatar
Bob Halley committed
555 556 557 558 559 560
				break;

			*p++ = c;
			++n;
			if ( n >= size ) {
				size += T_BUFSIZ;
561 562
				buf = (char *)realloc(buf,
						      size * sizeof(char));
Bob Halley's avatar
Bob Halley committed
563
				if (buf == NULL)
564 565
					goto err;
				old = buf;
Bob Halley's avatar
Bob Halley committed
566 567 568 569
				p = buf + n;
			}
		}
		*p = '\0';
570 571 572 573 574
		if (c == EOF && n == 0U) {
			free(buf);
			return (NULL);
		}
		return (buf);
575
	} else {
576 577 578 579
 err:
		if (old != NULL)
			free(old);
		fprintf(stderr, "malloc/realloc failed %d", errno);
580
		return(NULL);
Bob Halley's avatar
Bob Halley committed
581 582 583 584 585
	}
}

/*
 *
586 587 588
 * Put info to log, using key.
 * For now, just dump it out.
 * Later format into pretty lines.
Bob Halley's avatar
Bob Halley committed
589 590 591 592
 *
 */

static int
593
t_putinfo(const char *key, const char *info) {
Bob Halley's avatar
Bob Halley committed
594 595
	int	rval;

596 597 598
	/*
	 * For now.
	 */
Bob Halley's avatar
Bob Halley committed
599 600 601 602 603
	rval = printf("%s:%s", key, info);
	return(rval);
}

static char *
604
t_getdate(char *buf, size_t buflen) {
605
	size_t		n;
Bob Halley's avatar
Bob Halley committed
606 607 608 609 610
	time_t		t;
	struct tm	*p;

	t = time(NULL);
	p = localtime(&t);
611
	n = strftime(buf, buflen - 1, "%A %d %B %H:%M:%S %Y\n", p);
612
	return(n != 0U ? buf : NULL);
Bob Halley's avatar
Bob Halley committed
613 614
}

615 616 617
/*
 * Some generally used utilities.
 */
Bob Halley's avatar
Bob Halley committed
618
struct dns_errormap {
619
	isc_result_t	result;
David Lawrence's avatar
David Lawrence committed
620
	const char *text;
Bob Halley's avatar
Bob Halley committed
621
} dns_errormap[] = {
622 623 624
	{ ISC_R_SUCCESS,		"ISC_R_SUCCESS"		},
	{ ISC_R_EXISTS,			"ISC_R_EXISTS"		},
	{ ISC_R_NOTFOUND,		"ISC_R_NOTFOUND"	},
Michael Graff's avatar
Michael Graff committed
625 626 627
	{ ISC_R_NOSPACE,		"ISC_R_NOSPACE"		},
	{ ISC_R_UNEXPECTED,		"ISC_R_UNEXPECTED"	},
	{ ISC_R_UNEXPECTEDEND,		"ISC_R_UNEXPECTEDEND"	},
David Lawrence's avatar
David Lawrence committed
628
	{ ISC_R_RANGE,			"ISC_R_RANGE"		},
629 630
	{ DNS_R_LABELTOOLONG,		"DNS_R_LABELTOOLONG"	},
	{ DNS_R_BADESCAPE,		"DNS_R_BADESCAPE"	},
Mark Andrews's avatar
Mark Andrews committed
631 632
	/* { DNS_R_BADBITSTRING,	"DNS_R_BADBITSTRING"	}, */
	/* { DNS_R_BITSTRINGTOOLONG,	"DNS_R_BITSTRINGTOOLONG"}, */
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
	{ 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"		},
Mark Andrews's avatar
Mark Andrews committed
663
	{ DNS_R_NOTZONETOP,		"DNS_R_NOTZONETOP"	},
664
	{ DNS_R_SEENINCLUDE,		"DNS_R_SEENINCLUDE"	},
Mark Andrews's avatar
Mark Andrews committed
665
	{ DNS_R_SINGLETON,		"DNS_R_SINGLETON"	},
666
	{ (isc_result_t)0, NULL }
Bob Halley's avatar
Bob Halley committed
667 668
};

669
isc_result_t
Bob Halley's avatar
Bob Halley committed
670 671
t_dns_result_fromtext(char *name) {

672
	isc_result_t		result;
Bob Halley's avatar
Bob Halley committed
673 674
	struct dns_errormap	*pmap;

675
	result = ISC_R_UNEXPECTED;
Bob Halley's avatar
Bob Halley committed
676 677 678 679 680 681 682 683 684 685 686

	pmap = dns_errormap;
	while (pmap->text != NULL) {
		if (strcmp(name, pmap->text) == 0)
			break;
		++pmap;
	}

	if (pmap->text != NULL)
		result = pmap->result;

687
	return (result);
Bob Halley's avatar
Bob Halley committed
688 689
}

690
struct dc_method_map {
David Lawrence's avatar
David Lawrence committed
691 692
	unsigned int	dc_method;
	const char 	*text;
693
} dc_method_map[] = {
694

695 696 697 698 699 700
	{	DNS_COMPRESS_NONE,	"DNS_COMPRESS_NONE"	},
	{	DNS_COMPRESS_GLOBAL14,	"DNS_COMPRESS_GLOBAL14"	},
	{	DNS_COMPRESS_ALL,	"DNS_COMPRESS_ALL"	},
	{	0,			NULL			}
};

David Lawrence's avatar
David Lawrence committed
701
unsigned int
702
t_dc_method_fromtext(char *name) {
David Lawrence's avatar
David Lawrence committed
703
	unsigned int		dc_method;
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	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);
}

Bob Halley's avatar
Bob Halley committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
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
738
printhelp(void) {
Bob Halley's avatar
Bob Halley committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
	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
754
printusage(void) {
Bob Halley's avatar
Bob Halley committed
755 756 757
	printf("Usage:\n%s\n", Usage);
}

758
int
David Lawrence's avatar
David Lawrence committed
759
t_eval(const char *filename, int (*func)(char **), int nargs) {
760 761 762 763 764
	FILE		*fp;
	char		*p;
	int		line;
	int		cnt;
	int		result;
765
	int		tresult;
766 767
	int		nfails;
	int		nprobs;
768
	int		npass;
769 770
	char		*tokens[T_MAXTOKS + 1];

771
	tresult = T_UNTESTED;
772
	npass = 0;
773 774 775 776 777 778 779 780 781 782
	nfails = 0;
	nprobs = 0;

	fp = fopen(filename, "r");
	if (fp != NULL) {
		line = 0;
		while ((p = t_fgetbs(fp)) != NULL) {

			++line;

783 784 785
			/*
			 * Skip comment lines.
			 */
786 787
			if ((isspace((unsigned char)*p)) || (*p == '#')) {
				(void)free(p);
788
				continue;
789
			}
790 791 792

			cnt = t_bustline(p, tokens);
			if (cnt == nargs) {
793 794
				tresult = func(tokens);
				switch (tresult) {
795 796 797 798 799 800
				case T_PASS:
					++npass;
					break;
				case T_FAIL:
					++nfails;
					break;
801
				case T_SKIPPED:
802 803 804 805 806
				case T_UNTESTED:
					break;
				default:
					++nprobs;
					break;
807
				}
808
			} else {
809 810 811 812 813
				t_info("bad format in %s at line %d\n",
						filename, line);
				++nprobs;
			}

814
			(void)free(p);
815
		}
816 817
		(void)fclose(fp);
	} else {
818 819 820 821 822 823
		t_info("Missing datafile %s\n", filename);
		++nprobs;
	}

	result = T_UNRESOLVED;

824
	if (nfails == 0 && nprobs == 0 && npass > 0)
825
		result = T_PASS;
826
	else if (nfails > 0)
827
		result = T_FAIL;
828
	else if (npass == 0)
829
		result = tresult;
830

831
	return (result);
832
}
833 834 835 836 837 838 839 840 841 842 843

#ifdef WIN32
void
t_settests(const testspec_t list[]) {
	int			tnum;
	const testspec_t	*pts;

	memset(T_testlist, 0, sizeof(T_testlist));

	pts = &list[0];
	for (tnum = 0; tnum < T_MAXTESTS - 1; pts++, tnum++) {
844
		if (pts->pfv == NULL)
845 846 847 848 849
			break;
		T_testlist[tnum] = *pts;
	}
}
#endif