t_rbt.c 37.2 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
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
/*
 * Copyright (C) 1998, 1999  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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM 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.
 */

#include	<config.h>

#include	<ctype.h>
#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	<unistd.h>

#include	<isc/boolean.h>

#include	<dns/rbt.h>
#include	<dns/fixedname.h>
William King's avatar
William King committed
30
31
#include	<dns/name.h>
#include	<dns/result.h>
Bob Halley's avatar
Bob Halley committed
32
33
34

#include	<tests/t_api.h>

William King's avatar
William King committed
35
36
#define		BUFLEN	1024
#define		DNSNAMELEN 255
Bob Halley's avatar
Bob Halley committed
37

William King's avatar
William King committed
38
39
char		*progname;
char		*Tokens[T_MAXTOKS];
Bob Halley's avatar
Bob Halley committed
40

William King's avatar
William King committed
41
42
43
44
45
46
47
48
static int	t_dns_rbtnodechain_init(char *dbfile, char *findname,
			char *firstname, char *firstorigin,
			char *nextname, char *nextorigin,
			char *prevname, char *prevorigin,
			char *lastname, char *lastorigin);
static char	*fixedname_totext(dns_fixedname_t *name);
static int	fixedname_cmp(dns_fixedname_t *dns_name, char *txtname);
static char	*dnsname_totext(dns_name_t *name);
49
static int	t_namechk(isc_result_t dns_result, dns_fixedname_t *dns_name, char *exp_name,
Bob Halley's avatar
Bob Halley committed
50
			  dns_fixedname_t *dns_origin, char *exp_origin,
51
			  isc_result_t exp_result);
William King's avatar
William King committed
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

/* parts adapted from the original rbt_test.c */

static int
fixedname_cmp(dns_fixedname_t *dns_name, char *txtname) {
	char	*name;

	name = dnsname_totext(dns_fixedname_name(dns_name));
	if (strcmp(txtname, "NULL") == 0) {
		if ((name == NULL) || (*name == '\0'))
			return(0);
		return(1);
	}
	else {
		return(strcmp(name, txtname));
	}
}

static char *
dnsname_totext(dns_name_t *name) {
	static char	buf[BUFLEN];
	isc_buffer_t	target;

	isc_buffer_init(&target, buf, BUFLEN, ISC_BUFFERTYPE_TEXT);
	dns_name_totext(name, ISC_FALSE, &target);
	*((char *)(target.base) + target.used) = '\0';
	return(target.base);
}

static char *
fixedname_totext(dns_fixedname_t *name) {
	static char	buf[BUFLEN];
	isc_buffer_t	target;

	memset(buf, 0, BUFLEN);
	isc_buffer_init(&target, buf, BUFLEN, ISC_BUFFERTYPE_TEXT);
	dns_name_totext(dns_fixedname_name(name), ISC_FALSE, &target);
	*((char *)(target.base) + target.used) = '\0';
	return(target.base);
}
Bob Halley's avatar
Bob Halley committed
92
93
94

#ifdef	NEED_PRINT_DATA

95
static isc_result_t
Bob Halley's avatar
Bob Halley committed
96
print_data(void *data) {
97
	isc_result_t	dns_result;
Bob Halley's avatar
Bob Halley committed
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
	isc_buffer_t	target;
	char		*buffer[DNSNAMELEN];

	isc_buffer_init(&target, buffer, sizeof(buffer), ISC_BUFFERTYPE_TEXT);

	dns_result = dns_name_totext(data, ISC_FALSE, &target);
	if (dns_result != DNS_R_SUCCESS) {
		t_info("dns_name_totext failed %s\n",
				dns_result_totext(dns_result));
	}
	return(dns_result);
}

#endif	/* NEED_PRINT_DATA */

static int
create_name(char *s, isc_mem_t *mctx, dns_name_t **dns_name) {
	int		nfails;
	int		length;
	isc_result_t	result;
	isc_buffer_t	source;
	isc_buffer_t	target;

	nfails = 0;

	if (s && *s) {

		length = strlen(s);
	
		isc_buffer_init(&source, s, length, ISC_BUFFERTYPE_TEXT);
		isc_buffer_add(&source, length);
	
		/*
		 * The buffer for the actual name will immediately follow the
		 * name structure.
		 */
		*dns_name = isc_mem_get(mctx, sizeof(**dns_name) + DNSNAMELEN);
		if (*dns_name == NULL) {
			t_info("isc_mem_get failed\n");
			++nfails;
		}
	
		dns_name_init(*dns_name, NULL);
		isc_buffer_init(&target, *dns_name + 1, DNSNAMELEN,
					ISC_BUFFERTYPE_BINARY);
	
		result = dns_name_fromtext(*dns_name, &source, dns_rootname,
					   ISC_FALSE, &target);
	
		if (result != DNS_R_SUCCESS) {
			++nfails;
			t_info("dns_name_fromtext(%s) failed %s\n",
			       s, dns_result_totext(result));
		}
	}
	else {
		++nfails;
		t_info("create_name: empty name\n");
	}

	return(nfails);
}

static void
delete_name(void *data, void *arg) {
	dns_name_t *name;

	name = (dns_name_t *) data;
	isc_mem_put((isc_mem_t *) arg, data, sizeof(dns_name_t) + DNSNAMELEN);
}


/* adapted from the original rbt_test.c */

static int
173
t1_add(char *name, dns_rbt_t *rbt, isc_mem_t *mctx, isc_result_t *dns_result) {
Bob Halley's avatar
Bob Halley committed
174
175
176
177
178
179
180
181

	int		nprobs;
	dns_name_t	*dns_name;

	nprobs = 0;
	if (name && dns_result) {
		*dns_result = create_name(name, mctx, &dns_name);
		if (*dns_result == DNS_R_SUCCESS) {
William King's avatar
William King committed
182
183
			if (T_debug)
				t_info("dns_rbt_addname succeeded\n");
Bob Halley's avatar
Bob Halley committed
184
185
186
			*dns_result = dns_rbt_addname(rbt, dns_name, dns_name);
		}
		else {
William King's avatar
William King committed
187
188
189
			t_info("dns_rbt_addname failed %s\n",
		       			dns_result_totext(*dns_result));
			delete_name(dns_name, mctx);
Bob Halley's avatar
Bob Halley committed
190
191
192
193
194
195
196
197
198
199
			++nprobs;
		}
	}
	else {
		++nprobs;
	}
	return(nprobs);
}

static int
200
t1_delete(char *name, dns_rbt_t *rbt, isc_mem_t *mctx, isc_result_t *dns_result) {
Bob Halley's avatar
Bob Halley committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
	int		nprobs;
	dns_name_t	*dns_name;

	nprobs = 0;
	if (name && dns_result) {
		*dns_result = create_name(name, mctx, &dns_name);
		if (*dns_result == DNS_R_SUCCESS) {
			*dns_result = dns_rbt_deletename(rbt, dns_name, ISC_FALSE);
			delete_name(dns_name, mctx);
		}
		else {
			++nprobs;
		}
	}
	else {
		++nprobs;
	}
	return(nprobs);
}

static int
222
t1_search(char *name, dns_rbt_t *rbt, isc_mem_t *mctx, isc_result_t *dns_result) {
Bob Halley's avatar
Bob Halley committed
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

	int		nprobs;
	dns_name_t	*dns_searchname;
	dns_name_t	*dns_foundname;
	dns_fixedname_t	dns_fixedname;
	void		*data;

	nprobs = 0;
	if (name && dns_result) {
		*dns_result = create_name(name, mctx, &dns_searchname);
		if (*dns_result == DNS_R_SUCCESS) {
			dns_fixedname_init(&dns_fixedname);
			dns_foundname = dns_fixedname_name(&dns_fixedname);
			data = NULL;
			*dns_result = dns_rbt_findname(rbt, dns_searchname,
						dns_foundname, &data);
			delete_name(dns_searchname, mctx);
		}
		else {
			++nprobs;
		}
	}
	else {
		++nprobs;
	}
	return(nprobs);
}

William King's avatar
William King committed
251
/* initialize a database from filename */
Bob Halley's avatar
Bob Halley committed
252
static int
William King's avatar
William King committed
253
254
rbt_init(char *filename, dns_rbt_t **rbt, isc_mem_t *mctx) {

Bob Halley's avatar
Bob Halley committed
255
	int		rval;
256
	isc_result_t	dns_result;
William King's avatar
William King committed
257
	char		*p;
Bob Halley's avatar
Bob Halley committed
258
259
260
261
262
	FILE		*fp;

	fp = fopen(filename, "r");
	if (fp == NULL) {
		t_info("No such file %s\n", filename);
William King's avatar
William King committed
263
		return(1);
Bob Halley's avatar
Bob Halley committed
264
265
	}

William King's avatar
William King committed
266
	dns_result = dns_rbt_create(mctx, delete_name, mctx, rbt);
Bob Halley's avatar
Bob Halley committed
267
268
269
270
	if (dns_result != DNS_R_SUCCESS) {
		t_info("dns_rbt_create failed %s\n",
		       		dns_result_totext(dns_result));
		fclose(fp);
William King's avatar
William King committed
271
		return(1);
Bob Halley's avatar
Bob Halley committed
272
273
274
275
276
277
278
279
280
281
282
283
284
	}

	while ((p = t_fgetbs(fp)) != NULL) {

		/* skip any comment lines */
		if ((*p == '#') || (*p == '\0') || (*p == ' ')) {
			free(p);
			continue;
		}

		if (T_debug)
			t_info("adding name %s to the rbt\n", p);

William King's avatar
William King committed
285
		rval = t1_add(p, *rbt, mctx, &dns_result);
Bob Halley's avatar
Bob Halley committed
286
		if ((rval != 0) || (dns_result != DNS_R_SUCCESS)) {
William King's avatar
William King committed
287
288
			t_info("add of %s failed\n", p);
			dns_rbt_destroy(rbt);
Bob Halley's avatar
Bob Halley committed
289
			fclose(fp);
William King's avatar
William King committed
290
			return(1);
Bob Halley's avatar
Bob Halley committed
291
292
293
294
		}
		(void) free(p);
	}
	fclose(fp);
William King's avatar
William King committed
295
296
297
298
	return(0);
}

static int
299
test_rbt_gen(char *filename, char *command, char *testname, isc_result_t exp_result) {
William King's avatar
William King committed
300
301
302
303
	int		rval;
	int		result;
	dns_rbt_t	*rbt;
	isc_result_t	isc_result;
304
	isc_result_t	dns_result;
William King's avatar
William King committed
305
	isc_mem_t	*mctx;
William King's avatar
William King committed
306
	dns_name_t	*dns_name;
William King's avatar
William King committed
307
308

	result = T_UNRESOLVED;
Bob Halley's avatar
Bob Halley committed
309

William King's avatar
William King committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
	if (strcmp(command, "create") != 0)
		t_info("testing using name %s\n", testname);

	mctx = NULL;
	isc_result = isc_mem_create(0, 0, &mctx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mem_create: %s: exiting\n",
		       dns_result_totext(isc_result));
		return(T_UNRESOLVED);
	}

	rbt = NULL;
	if (rbt_init(filename, &rbt, mctx) != 0) {
		if (strcmp(command, "create") == 0)
			result = T_FAIL;
		isc_mem_destroy(&mctx);
		return(result);
	}
Bob Halley's avatar
Bob Halley committed
328
329
330
		
	/* now try the database command */
	if (strcmp(command, "create") == 0) {
William King's avatar
William King committed
331
		result = T_PASS;
Bob Halley's avatar
Bob Halley committed
332
333
	}
	else if (strcmp(command, "add") == 0) {
William King's avatar
William King committed
334
335
336
337
338
339
340
		dns_result = create_name(testname, mctx, &dns_name);
		if (dns_result == DNS_R_SUCCESS) {
			dns_result = dns_rbt_addname(rbt, dns_name, dns_name);

			if (dns_result != DNS_R_SUCCESS)
				delete_name(dns_name, mctx);

Bob Halley's avatar
Bob Halley committed
341
			if (dns_result == exp_result) {
William King's avatar
William King committed
342
343
344
345
346
347
348
349
350
				if (dns_result == DNS_R_SUCCESS) {
					rval = t1_search(testname, rbt, mctx, &dns_result);
					if (rval == 0) {
						if (dns_result == DNS_R_SUCCESS) {
							result = T_PASS;
						}
						else {
							result = T_FAIL;
						}
Bob Halley's avatar
Bob Halley committed
351
352
					}
					else {
William King's avatar
William King committed
353
354
						t_info("t1_search failed\n");
						result = T_UNRESOLVED;
Bob Halley's avatar
Bob Halley committed
355
356
					}
				}
William King's avatar
William King committed
357
358
359
				else {
					result = T_PASS;
				}
Bob Halley's avatar
Bob Halley committed
360
361
			}
			else {
William King's avatar
William King committed
362
				t_info("dns_rbt_addname returned %s, expected %s\n",
Bob Halley's avatar
Bob Halley committed
363
364
365
366
367
					dns_result_totext(dns_result),
					dns_result_totext(exp_result));
				result = T_FAIL;
			}
		}
William King's avatar
William King committed
368
369
370
371
372
		else {
			t_info("create_name failed %s\n",
				dns_result_totext(dns_result));
			result = T_UNRESOLVED;
		}
Bob Halley's avatar
Bob Halley committed
373
374
375
376
	}
	else if ((strcmp(command, "delete") == 0) ||
		(strcmp(command, "nuke") == 0)) {

William King's avatar
William King committed
377
		rval = t1_delete(testname, rbt, mctx, &dns_result);
Bob Halley's avatar
Bob Halley committed
378
379
		if (rval == 0) {
			if (dns_result == exp_result) {
William King's avatar
William King committed
380
				rval = t1_search(testname, rbt, mctx, &dns_result);
Bob Halley's avatar
Bob Halley committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
				if (rval == 0) {
					if (dns_result == DNS_R_SUCCESS) {
						t_info("dns_rbt_deletename didn't "
							"delete the name");
						result = T_FAIL;
					}
					else {
						result = T_PASS;
					}
				}
			}
			else {
				t_info("delete returned %s, expected %s\n",
					dns_result_totext(dns_result),
					dns_result_totext(exp_result));
				result = T_FAIL;
			}
		}
	}
	else if (strcmp(command, "search") == 0) {

William King's avatar
William King committed
402
		rval = t1_search(testname, rbt, mctx, &dns_result);
Bob Halley's avatar
Bob Halley committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
		if (rval == 0) {
			if (dns_result == exp_result) {
				result = T_PASS;
			}
			else {
				t_info("find returned %s, expected %s\n",
					dns_result_totext(dns_result),
					dns_result_totext(exp_result));
				result = T_FAIL;
			}
		}
	}

	dns_rbt_destroy(&rbt);
William King's avatar
William King committed
417
	isc_mem_destroy(&mctx);
Bob Halley's avatar
Bob Halley committed
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
	return(result);
}

static int
test_dns_rbt_x(char *filename) {

	FILE		*fp;
	char		*p;
	int		line;
	int		cnt;
	int		result;
	int		nfails;
	int		nprobs;

	nfails = 0;
	nprobs = 0;

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

			++line;

			/* skip comment lines */
William King's avatar
William King committed
443
			if ((isspace((int)*p)) || (*p == '#'))
Bob Halley's avatar
Bob Halley committed
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
				continue;

			/* name of db file, command, testname, expected result */
			cnt = t_bustline(p, Tokens);
			if (cnt == 4) {
				result = test_rbt_gen(
						Tokens[0],
						Tokens[1],
						Tokens[2],
						t_dns_result_fromtext(Tokens[3]));
				if (result != T_PASS)
					++nfails;
			}
			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))
		result = T_PASS;
	else if (nfails)
		result = T_FAIL;

	return(result);
}


static char	*a1 =	"dns_rbt_create creates a rbt and returns DNS_R_SUCCESS "
			"on success";

static void
t1() {
	int	result;

	t_assert("dns_rbt_create", 1, T_REQUIRED, a1);
	result = test_dns_rbt_x("dns_rbt_create_1_data");
	t_result(result);
}

static char	*a2 =	"dns_rbt_addname adds a name to a database and returns "
			"DNS_R_SUCCESS on success";
static void
t2() {
	int	result;

	t_assert("dns_rbt_addname", 2, T_REQUIRED, a2);
	result = test_dns_rbt_x("dns_rbt_addname_1_data");
	t_result(result);
}

static char	*a3 =	"when name already exists, dns_rbt_addname() returns "
			"DNS_R_EXISTS";

static void
t3() {
	int	result;

	t_assert("dns_rbt_addname", 3, T_REQUIRED, a3);
	result = test_dns_rbt_x("dns_rbt_addname_2_data");
	t_result(result);
}

static char	*a4 =	"when name exists, dns_rbt_deletename() returns "
			"DNS_R_SUCCESS";

static void
t4() {
	int	result;

	t_assert("dns_rbt_deletename", 4, T_REQUIRED, a4);
	result = test_dns_rbt_x("dns_rbt_deletename_1_data");
	t_result(result);
}

static char	*a5 =	"when name does not exist, dns_rbt_deletename() returns "
			"DNS_R_NOTFOUND";
static void
t5() {
	int	result;

	t_assert("dns_rbt_deletename", 5, T_REQUIRED, a5);
	result = test_dns_rbt_x("dns_rbt_deletename_2_data");
	t_result(result);
}

static char	*a6 =	"when name exists and exactly matches the search name, "
			"dns_rbt_findname() returns DNS_R_SUCCESS";

static void
t6() {
	int	result;

	t_assert("dns_rbt_findname", 6, T_REQUIRED, a6);
	result = test_dns_rbt_x("dns_rbt_findname_1_data");
	t_result(result);
}

static char	*a7 =	"when a name does not exist, "
			"dns_rbt_findname returns DNS_R_NOTFOUND";

static void
t7() {
	int	result;

	t_assert("dns_rbt_findname", 7, T_REQUIRED, a7);
	result = test_dns_rbt_x("dns_rbt_findname_2_data");
	t_result(result);
}

static char	*a8 =	"when a superdomain is found with data matching name, "
			"dns_rbt_findname returns DNS_R_PARTIALMATCH";

static void
t8() {
	int	result;

	t_assert("dns_rbt_findname", 8, T_REQUIRED, a8);
	result = test_dns_rbt_x("dns_rbt_findname_3_data");
	t_result(result);
}

William King's avatar
William King committed
576
577
578
579
580
581
582
583
584
585
586

static char	*a9 =	"a call to dns_rbtnodechain_init(chain, mctx) "
			"initializes chain";

static int
t9_walkchain(dns_rbtnodechain_t *chain, dns_rbt_t *rbt) {
	int		cnt;
	int		order;
	unsigned int	nlabels;
	unsigned int	nbits;
	int		nprobs;
587
	isc_result_t	dns_result;
William King's avatar
William King committed
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
	dns_namereln_t	dns_namereln;

	dns_fixedname_t	name;
	dns_fixedname_t	origin;
	dns_fixedname_t	fullname1;
	dns_fixedname_t	fullname2;

	cnt = 0;
	nprobs = 0;

	while (1) {

		if (cnt == 0) {
			dns_fixedname_init(&name);
			dns_fixedname_init(&origin);
			dns_result = dns_rbtnodechain_first(chain, rbt,
						dns_fixedname_name(&name),
						dns_fixedname_name(&origin));
			if (dns_result != DNS_R_NEWORIGIN) {
				t_info("dns_rbtnodechain_first returned %s, "
					"expecting DNS_R_NEWORIGIN\n",
					dns_result_totext(dns_result));
				++nprobs;
				break;
			}
			t_info("first name:\t<%s>\n", fixedname_totext(&name));
			t_info("first origin:\t<%s>\n", fixedname_totext(&origin));
		}
		else {
			dns_fixedname_init(&fullname1);
			dns_result = dns_name_concatenate(dns_fixedname_name(&name),
							dns_name_isabsolute(dns_fixedname_name(&name)) ?
								NULL : dns_fixedname_name(&origin),
							dns_fixedname_name(&fullname1),
							NULL);
			if (dns_result != DNS_R_SUCCESS) {
				t_info("dns_name_concatenate failed %s\n",
						dns_result_totext(dns_result));
				++nprobs;
				break;
			}

			/* expecting origin not to get touched if next doesn't return NEWORIGIN */
			dns_fixedname_init(&name);
			dns_result = dns_rbtnodechain_next(chain, dns_fixedname_name(&name),
						dns_fixedname_name(&origin));
			if ((dns_result != DNS_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
				if (dns_result != DNS_R_NOMORE) {
					t_info("dns_rbtnodechain_next failed %s\n",
							dns_result_totext(dns_result));
					++nprobs;
				}
				break;
			}

			t_info("next name:\t<%s>\n", fixedname_totext(&name));
			t_info("next origin:\t<%s>\n", fixedname_totext(&origin));

			dns_fixedname_init(&fullname2);
			dns_result = dns_name_concatenate(dns_fixedname_name(&name),
							dns_name_isabsolute(dns_fixedname_name(&name)) ?
								NULL : dns_fixedname_name(&origin),
							dns_fixedname_name(&fullname2),
							NULL);
			if (dns_result != DNS_R_SUCCESS) {
				t_info("dns_name_concatenate failed %s\n", dns_result_totext(dns_result));
				++nprobs;
				break;
			}

			t_info("comparing\t<%s>\n", fixedname_totext(&fullname1));
			t_info("\twith\t<%s>\n", fixedname_totext(&fullname2));

			dns_namereln = dns_name_fullcompare(
						dns_fixedname_name(&fullname1),
						dns_fixedname_name(&fullname2),
						&order, &nlabels, &nbits);

			if (order >= 0) {
				t_info("unexpected order %s %s %s\n",
					dnsname_totext(dns_fixedname_name(&fullname1)),
					order == -1 ? "<" : (order == 0 ? "==" : ">"),
					dnsname_totext(dns_fixedname_name(&fullname2)));
				++nprobs;
			}
		}

		++cnt;
	}
	return(nprobs);
}

/* test by exercising the first|last|next|prev funcs in useful ways */

static int
683
t_namechk(isc_result_t dns_result, dns_fixedname_t *dns_name, char *exp_name,
Bob Halley's avatar
Bob Halley committed
684
	  dns_fixedname_t *dns_origin, char *exp_origin,
685
	  isc_result_t exp_result)
Bob Halley's avatar
Bob Halley committed
686
{
William King's avatar
William King committed
687
688
689
690
691
692
693
694
695
696
697
	int	nfails;

	nfails = 0;

	if (fixedname_cmp(dns_name, exp_name)) {
		t_info("\texpected name of %s, got %s\n",
				exp_name, fixedname_totext(dns_name));
		++nfails;
	}
	if (exp_origin != NULL) {
		t_info("checking for DNS_R_NEWORIGIN\n");
Bob Halley's avatar
Bob Halley committed
698
		if (dns_result == exp_result) {
William King's avatar
William King committed
699
			if (fixedname_cmp(dns_origin, exp_origin)) {
Bob Halley's avatar
Bob Halley committed
700
701
702
				t_info("\torigin %s, expected %s\n",
				       fixedname_totext(dns_origin),
				       exp_origin);
William King's avatar
William King committed
703
704
				++nfails;
			}
Bob Halley's avatar
Bob Halley committed
705
		} else {
William King's avatar
William King committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
			t_info("\tgot %s\n", dns_result_totext(dns_result));
			++nfails;
		}
	}
	return(nfails);
}

static int
t_dns_rbtnodechain_init(char *dbfile, char *findname,
			char *nextname, char *nextorigin,
			char *prevname, char *prevorigin,
			char *firstname, char *firstorigin,
			char *lastname, char *lastorigin) {

	int			result;
	int			len;
	int			nfails;
	int			nprobs;
	dns_rbt_t		*rbt;
	dns_rbtnode_t		*node;
	dns_rbtnodechain_t	chain;
	isc_mem_t		*mctx;
	isc_result_t		isc_result;
729
	isc_result_t		dns_result;
William King's avatar
William King committed
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
	dns_fixedname_t		dns_findname;
	dns_fixedname_t		dns_foundname;
	dns_fixedname_t		dns_firstname;
	dns_fixedname_t		dns_lastname;
	dns_fixedname_t		dns_prevname;
	dns_fixedname_t		dns_nextname;
	dns_fixedname_t		dns_origin;
	isc_buffer_t		isc_buffer;

	result = T_UNRESOLVED;

	nfails = 0;
	nprobs = 0;
	mctx = NULL;
	isc_result = isc_mem_create(0, 0, &mctx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mem_create failed %s\n", isc_result_totext(isc_result));
		return(result);
	}

	dns_rbtnodechain_init(&chain, mctx);

	rbt = NULL;
	if (rbt_init(dbfile, &rbt, mctx)) {
		t_info("rbt_init %s failed\n", dbfile);
		isc_mem_destroy(&mctx);
		return(result);
	}

	len = strlen(findname);
	isc_buffer_init(&isc_buffer, findname, len, ISC_BUFFERTYPE_TEXT);
	isc_buffer_add(&isc_buffer, len);

	dns_fixedname_init(&dns_foundname);
	dns_fixedname_init(&dns_findname);
	dns_fixedname_init(&dns_firstname);
	dns_fixedname_init(&dns_origin);
	dns_fixedname_init(&dns_lastname);
	dns_fixedname_init(&dns_prevname);
	dns_fixedname_init(&dns_nextname);

	dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname),
					&isc_buffer, NULL, ISC_FALSE, NULL);

	if (dns_result != DNS_R_SUCCESS) {
		t_info("dns_name_fromtext failed %s\n", dns_result_totext(dns_result));
		return(result);
	}

	/* set the starting node */
Bob Halley's avatar
Bob Halley committed
780
	node = NULL;
William King's avatar
William King committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
	dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
					dns_fixedname_name(&dns_foundname),
					&node, &chain, ISC_TRUE, NULL, NULL);

	if (dns_result != DNS_R_SUCCESS) {
		t_info("dns_rbt_findnode failed %s\n", dns_result_totext(dns_result));
		return(result);
	}

	/* check next */
	t_info("checking for next name of %s and new origin of %s\n",
			nextname, nextorigin);
	dns_result = dns_rbtnodechain_next(&chain,
			dns_fixedname_name(&dns_nextname),
			dns_fixedname_name(&dns_origin));

	if ((dns_result != DNS_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
		t_info("dns_rbtnodechain_next unexpectedly returned %s\n",
				dns_result_totext(dns_result));
	}

Bob Halley's avatar
Bob Halley committed
802
803
	nfails += t_namechk(dns_result, &dns_nextname, nextname, &dns_origin, nextorigin,
			    DNS_R_NEWORIGIN);
William King's avatar
William King committed
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828

	/* set the starting node again */
	node = NULL;
	dns_fixedname_init(&dns_foundname);
	dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
					dns_fixedname_name(&dns_foundname),
					&node, &chain, ISC_TRUE, NULL, NULL);

	if (dns_result != DNS_R_SUCCESS) {
		t_info("\tdns_rbt_findnode failed %s\n", dns_result_totext(dns_result));
		return(result);
	}

	/* check previous */
	t_info("checking for previous name of %s and new origin of %s\n",
			prevname, prevorigin);
	dns_fixedname_init(&dns_origin);
	dns_result = dns_rbtnodechain_prev(&chain,
			dns_fixedname_name(&dns_prevname),
			dns_fixedname_name(&dns_origin));

	if ((dns_result != DNS_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
		t_info("dns_rbtnodechain_prev unexpectedly returned %s\n",
				dns_result_totext(dns_result));
	}
Bob Halley's avatar
Bob Halley committed
829
830
	nfails += t_namechk(dns_result, &dns_prevname, prevname, &dns_origin, prevorigin,
			    DNS_R_NEWORIGIN);
William King's avatar
William King committed
831
832
833
834
835
836
837
838
839
840
841
842
843


	/* check first */
	t_info("checking for first name of %s and new origin of %s\n",
			firstname, firstorigin);
	dns_result = dns_rbtnodechain_first(&chain, rbt,
			dns_fixedname_name(&dns_firstname),
			dns_fixedname_name(&dns_origin));

	if (dns_result != DNS_R_NEWORIGIN) {
		t_info("dns_rbtnodechain_first unexpectedly returned %s\n",
				dns_result_totext(dns_result));
	}
Bob Halley's avatar
Bob Halley committed
844
845
	nfails += t_namechk(dns_result, &dns_firstname, firstname, &dns_origin, firstorigin,
			    DNS_R_NEWORIGIN);
William King's avatar
William King committed
846
847
848
849
850
851
852
853
854
855
856
857


	/* check last */
	t_info("checking for last name of %s\n", lastname);
	dns_result = dns_rbtnodechain_last(&chain, rbt,
			dns_fixedname_name(&dns_lastname),
			dns_fixedname_name(&dns_origin));

	if (dns_result != DNS_R_NEWORIGIN) {
		t_info("dns_rbtnodechain_last unexpectedly returned %s\n",
				dns_result_totext(dns_result));
	}
Bob Halley's avatar
Bob Halley committed
858
859
	nfails += t_namechk(dns_result, &dns_lastname, lastname, &dns_origin, lastorigin,
			    DNS_R_NEWORIGIN);
William King's avatar
William King committed
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965

	/* check node ordering */
	t_info("checking node ordering\n");
	nfails += t9_walkchain(&chain, rbt);

	if (nfails)
		result = T_FAIL;
	else
		result = T_PASS;
		
	dns_rbt_destroy(&rbt);
	isc_mem_destroy(&mctx);
	return(result);
}

static int
test_dns_rbtnodechain_init(char *filename) {

	FILE		*fp;
	char		*p;
	int		line;
	int		cnt;
	int		result;
	int		nfails;
	int		nprobs;

	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((int)*p)) || (*p == '#'))
				continue;

			cnt = t_bustline(p, Tokens);
			if (cnt == 10) {
				result = t_dns_rbtnodechain_init(
						Tokens[0],	/* dbfile */
						Tokens[1],	/* startname */
						Tokens[2],	/* nextname */
						Tokens[3],	/* nextorigin */
						Tokens[4],	/* prevname */
						Tokens[5],	/* prevorigin */
						Tokens[6],	/* firstname */
						Tokens[7],	/* firstorigin */
						Tokens[8],	/* lastname */
						Tokens[9]);	/* lastorigin */
				if (result != T_PASS) {
					if (result == T_FAIL)
						++nfails;
					else
						++nprobs;
				}
			}
			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))
		result = T_PASS;
	else if (nfails)
		result = T_FAIL;

	return(result);
}

static void
t9() {
	int	result;

	t_assert("dns_rbtnodechain_init", 9, T_REQUIRED, a9);
	result = test_dns_rbtnodechain_init("dns_rbtnodechain_init_data");
	t_result(result);
}

static int
t_dns_rbtnodechain_first(char *dbfile, char *expected_firstname,
				char *expected_firstorigin,
				char *expected_nextname,
				char *expected_nextorigin) {

	int			result;
	int			nfails;
	dns_rbt_t		*rbt;
	dns_rbtnodechain_t	chain;
	isc_mem_t		*mctx;
	isc_result_t		isc_result;
966
	isc_result_t		dns_result;
William King's avatar
William King committed
967
968
	dns_fixedname_t		dns_name;
	dns_fixedname_t		dns_origin;
969
	isc_result_t		expected_result;
William King's avatar
William King committed
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004

	result = T_UNRESOLVED;

	nfails = 0;
	mctx = NULL;

	dns_fixedname_init(&dns_name);
	dns_fixedname_init(&dns_origin);

	isc_result = isc_mem_create(0, 0, &mctx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mem_create failed %s\n", isc_result_totext(isc_result));
		return(result);
	}

	dns_rbtnodechain_init(&chain, mctx);

	rbt = NULL;
	if (rbt_init(dbfile, &rbt, mctx)) {
		t_info("rbt_init %s failed\n", dbfile);
		isc_mem_destroy(&mctx);
		return(result);
	}

	t_info("testing for first name of %s, origin of %s\n",
			expected_firstname, expected_firstorigin);

	dns_result = dns_rbtnodechain_first(&chain, rbt,
			dns_fixedname_name(&dns_name),
			dns_fixedname_name(&dns_origin));

	if (dns_result != DNS_R_NEWORIGIN) {
		t_info("dns_rbtnodechain_first unexpectedly returned %s\n",
				dns_result_totext(dns_result));
	}
Bob Halley's avatar
Bob Halley committed
1005
1006
	nfails = t_namechk(dns_result, &dns_name, expected_firstname,
			   &dns_origin, expected_firstorigin, DNS_R_NEWORIGIN);
William King's avatar
William King committed
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019

	dns_fixedname_init(&dns_name);
	dns_result = dns_rbtnodechain_next(&chain,
			dns_fixedname_name(&dns_name),
			dns_fixedname_name(&dns_origin));

	t_info("testing for next name of %s, origin of %s\n",
			expected_nextname, expected_nextorigin);
			
	if ((dns_result != DNS_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
		t_info("dns_rbtnodechain_next unexpectedly returned %s\n",
				dns_result_totext(dns_result));
	}
Bob Halley's avatar
Bob Halley committed
1020
1021
1022
1023
1024
1025
	if (strcasecmp(expected_firstorigin, expected_nextorigin) == 0)
		expected_result = DNS_R_SUCCESS;
	else
		expected_result = DNS_R_NEWORIGIN;
	nfails += t_namechk(dns_result, &dns_name, expected_nextname,
			    &dns_origin, expected_nextorigin, expected_result);
William King's avatar
William King committed
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127

	if (nfails)
		result = T_FAIL;
	else
		result = T_PASS;
		
	dns_rbt_destroy(&rbt);
	isc_mem_destroy(&mctx);
	return(result);
}

static int
test_dns_rbtnodechain_first(char *filename) {

	FILE		*fp;
	char		*p;
	int		line;
	int		cnt;
	int		result;
	int		nfails;
	int		nprobs;

	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((int)*p)) || (*p == '#'))
				continue;

			cnt = t_bustline(p, Tokens);
			if (cnt == 5) {
				result = t_dns_rbtnodechain_first(
						Tokens[0],	/* dbfile */
						Tokens[1],	/* expected firstname */
						Tokens[2],	/* expected firstorigin */
						Tokens[3],	/* expected nextname */
						Tokens[4]);	/* expected nextorigin */
				if (result != T_PASS) {
					if (result == T_FAIL)
						++nfails;
					else
						++nprobs;
				}
			}
			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))
		result = T_PASS;
	else if (nfails)
		result = T_FAIL;

	return(result);
}

static char	*a10 =	"a call to dns_rbtnodechain_first(chain, rbt, name, origin) "
			"sets name to point to the root of the tree, "
			"origin to point to the origin, "
			" and returns DNS_R_NEWORIGIN";

static void
t10() {
	int	result;

	t_assert("dns_rbtnodechain_first", 10, T_REQUIRED, a10);
	result = test_dns_rbtnodechain_first("dns_rbtnodechain_first_data");
	t_result(result);
}

static int
t_dns_rbtnodechain_last(char *dbfile, char *expected_lastname,
				char *expected_lastorigin,
				char *expected_prevname,
				char *expected_prevorigin) {

	int			result;
	int			nfails;
	dns_rbt_t		*rbt;
	dns_rbtnodechain_t	chain;
	isc_mem_t		*mctx;
	isc_result_t		isc_result;
1128
	isc_result_t		dns_result;
William King's avatar
William King committed
1129
1130
	dns_fixedname_t		dns_name;
	dns_fixedname_t		dns_origin;
1131
	isc_result_t		expected_result;
William King's avatar
William King committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165

	result = T_UNRESOLVED;

	nfails = 0;
	mctx = NULL;

	dns_fixedname_init(&dns_name);
	dns_fixedname_init(&dns_origin);

	isc_result = isc_mem_create(0, 0, &mctx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mem_create failed %s\n", isc_result_totext(isc_result));
		return(result);
	}

	dns_rbtnodechain_init(&chain, mctx);

	rbt = NULL;
	if (rbt_init(dbfile, &rbt, mctx)) {
		t_info("rbt_init %s failed\n", dbfile);
		isc_mem_destroy(&mctx);
		return(result);
	}

	t_info("testing for last name of %s, origin of %s\n", expected_lastname, expected_lastorigin);

	dns_result = dns_rbtnodechain_last(&chain, rbt,
			dns_fixedname_name(&dns_name),
			dns_fixedname_name(&dns_origin));

	if (dns_result != DNS_R_NEWORIGIN) {
		t_info("dns_rbtnodechain_last unexpectedly returned %s\n",
			dns_result_totext(dns_result));
	}
Bob Halley's avatar
Bob Halley committed
1166
1167
	nfails = t_namechk(dns_result, &dns_name, expected_lastname, &dns_origin, expected_lastorigin,
			   DNS_R_NEWORIGIN);
William King's avatar
William King committed
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179

	t_info("testing for previous name of %s, origin of %s\n", expected_prevname, expected_prevorigin);

	dns_fixedname_init(&dns_name);
	dns_result = dns_rbtnodechain_prev(&chain,
			dns_fixedname_name(&dns_name),
			dns_fixedname_name(&dns_origin));

	if ((dns_result != DNS_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
		t_info("dns_rbtnodechain_prev unexpectedly returned %s\n",
			dns_result_totext(dns_result));
	}
Bob Halley's avatar
Bob Halley committed
1180
1181
1182
1183
1184
1185
	if (strcasecmp(expected_lastorigin, expected_prevorigin) == 0)
		expected_result = DNS_R_SUCCESS;
	else
		expected_result = DNS_R_NEWORIGIN;
	nfails += t_namechk(dns_result, &dns_name, expected_prevname,
			    &dns_origin, expected_prevorigin, expected_result);
William King's avatar
William King committed
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288

	if (nfails)
		result = T_FAIL;
	else
		result = T_PASS;
		
	dns_rbt_destroy(&rbt);
	isc_mem_destroy(&mctx);
	return(result);
}

static int
test_dns_rbtnodechain_last(char *filename) {

	FILE		*fp;
	char		*p;
	int		line;
	int		cnt;
	int		result;
	int		nfails;
	int		nprobs;

	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((int)*p)) || (*p == '#'))
				continue;

			cnt = t_bustline(p, Tokens);
			if (cnt == 5) {
				result = t_dns_rbtnodechain_last(
						Tokens[0],	/* dbfile */
						Tokens[1],	/* expected lastname */
						Tokens[2],	/* expected lastorigin */
						Tokens[3],	/* expected prevname */
						Tokens[4]);	/* expected prevorigin */
				if (result != T_PASS) {
					if (result == T_FAIL)
						++nfails;
					else
						++nprobs;
				}
			}
			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))
		result = T_PASS;
	else if (nfails)
		result = T_FAIL;

	return(result);
}

static char	*a11 =	"a call to dns_rbtnodechain_last(chain, rbt, name, origin) "
			"sets name to point to the last node of the megatree, "
			"origin to the name of the level above it, "
			"and returns DNS_R_NEWORIGIN";

static void
t11() {
	int	result;

	t_assert("dns_rbtnodechain_last", 11, T_REQUIRED, a11);
	result = test_dns_rbtnodechain_last("dns_rbtnodechain_last_data");
	t_result(result);
}

static int
t_dns_rbtnodechain_next(char *dbfile, char *findname,
			char *nextname, char *nextorigin) {

	int			result;
	int			len;
	int			nfails;
	int			nprobs;
	dns_rbt_t		*rbt;
	dns_rbtnode_t		*node;
	dns_rbtnodechain_t	chain;
	isc_mem_t		*mctx;
	isc_result_t		isc_result;
1289
	isc_result_t		dns_result;
William King's avatar
William King committed
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
	dns_fixedname_t		dns_findname;
	dns_fixedname_t		dns_foundname;
	dns_fixedname_t		dns_nextname;
	dns_fixedname_t		dns_origin;
	isc_buffer_t		isc_buffer;

	result = T_UNRESOLVED;

	nfails = 0;
	nprobs = 0;
	mctx = NULL;
	isc_result = isc_mem_create(0, 0, &mctx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mem_create failed %s\n", isc_result_totext(isc_result));
		return(result);
	}

	dns_rbtnodechain_init(&chain, mctx);

	rbt = NULL;
	if (rbt_init(dbfile, &rbt, mctx)) {
		t_info("rbt_init %s failed\n", dbfile);
		isc_mem_destroy(&mctx);
		return(result);
	}

	len = strlen(findname);
	isc_buffer_init(&isc_buffer, findname, len, ISC_BUFFERTYPE_TEXT);
	isc_buffer_add(&isc_buffer, len);

	dns_fixedname_init(&dns_foundname);
	dns_fixedname_init(&dns_findname);
	dns_fixedname_init(&dns_nextname);
	dns_fixedname_init(&dns_origin);

	dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname),
					&isc_buffer, NULL, ISC_FALSE, NULL);

	if (dns_result != DNS_R_SUCCESS) {
		t_info("dns_name_fromtext failed %s\n", dns_result_totext(dns_result));
		return(result);
	}

	/* set the starting node */
Bob Halley's avatar
Bob Halley committed
1334
	node = NULL;
William King's avatar
William King committed
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
	dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
					dns_fixedname_name(&dns_foundname),
					&node, &chain, ISC_TRUE, NULL, NULL);

	if (dns_result != DNS_R_SUCCESS) {
		t_info("dns_rbt_findnode failed %s\n", dns_result_totext(dns_result));
		return(result);
	}

	/* check next */
	t_info("checking for next name of %s and new origin of %s\n",
			nextname, nextorigin);
	dns_result = dns_rbtnodechain_next(&chain,
			dns_fixedname_name(&dns_nextname),
			dns_fixedname_name(&dns_origin));

	if ((dns_result != DNS_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
		t_info("dns_rbtnodechain_next unexpectedly returned %s\n",
				dns_result_totext(dns_result));
	}

Bob Halley's avatar
Bob Halley committed
1356
1357
	nfails += t_namechk(dns_result, &dns_nextname, nextname, &dns_origin, nextorigin,
			    DNS_R_NEWORIGIN);
William King's avatar
William King committed
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459

	if (nfails)
		result = T_FAIL;
	else
		result = T_PASS;
		
	dns_rbt_destroy(&rbt);
	isc_mem_destroy(&mctx);
	return(result);
}

static int
test_dns_rbtnodechain_next(char *filename) {

	FILE		*fp;
	char		*p;
	int		line;
	int		cnt;
	int		result;
	int		nfails;
	int		nprobs;

	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((int)*p)) || (*p == '#'))
				continue;

			cnt = t_bustline(p, Tokens);
			if (cnt == 4) {
				result = t_dns_rbtnodechain_next(
						Tokens[0],	/* dbfile */
						Tokens[1],	/* findname */
						Tokens[2],	/* expected nextname */
						Tokens[3]);	/* expected nextorigin */
				if (result != T_PASS) {
					if (result == T_FAIL)
						++nfails;
					else
						++nprobs;
				}
			}
			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))
		result = T_PASS;
	else if (nfails)
		result = T_FAIL;

	return(result);
}

static char	*a12 =	"a call to dns_rbtnodechain_next(chain, name, origin) "
			"sets name to point to the next node of the tree "
			"and returns DNS_R_SUCCESS or DNS_R_NEWORIGIN on success";


static void
t12() {
	int	result;

	t_assert("dns_rbtnodechain_next", 12, T_REQUIRED, a12);
	result = test_dns_rbtnodechain_next("dns_rbtnodechain_next_data");
	t_result(result);
}

static int
t_dns_rbtnodechain_prev(char *dbfile, char *findname,
			char *prevname, char *prevorigin) {

	int			result;
	int			len;
	int			nfails;
	int			nprobs;
	dns_rbt_t		*rbt;
	dns_rbtnode_t		*node;
	dns_rbtnodechain_t	chain;
	isc_mem_t		*mctx;
	isc_result_t		isc_result;
1460
	isc_result_t		dns_result;
William King's avatar
William King committed
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
	dns_fixedname_t		dns_findname;
	dns_fixedname_t		dns_foundname;
	dns_fixedname_t		dns_prevname;
	dns_fixedname_t		dns_origin;
	isc_buffer_t		isc_buffer;

	result = T_UNRESOLVED;

	nfails = 0;
	nprobs = 0;
	mctx = NULL;
	isc_result = isc_mem_create(0, 0, &mctx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mem_create failed %s\n", isc_result_totext(isc_result));
		return(result);
	}

	dns_rbtnodechain_init(&chain, mctx);

	rbt = NULL;
	if (rbt_init(dbfile, &rbt, mctx)) {
		t_info("rbt_init %s failed\n", dbfile);
		isc_mem_destroy(&mctx);
		return(result);
	}

	len = strlen(findname);
	isc_buffer_init(&isc_buffer, findname, len, ISC_BUFFERTYPE_TEXT);
	isc_buffer_add(&isc_buffer, len);

	dns_fixedname_init(&dns_foundname);
	dns_fixedname_init(&dns_findname);
	dns_fixedname_init(&dns_prevname);
	dns_fixedname_init(&dns_origin);

	dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname),
					&isc_buffer, NULL, ISC_FALSE, NULL);

	if (dns_result != DNS_R_SUCCESS) {
		t_info("dns_name_fromtext failed %s\n", dns_result_totext(dns_result));
		return(result);
	}

	/* set the starting node */
William King's avatar
William King committed
1505
	node = NULL;
William King's avatar
William King committed
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
	dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
					dns_fixedname_name(&dns_foundname),
					&node, &chain, ISC_TRUE, NULL, NULL);

	if (dns_result != DNS_R_SUCCESS) {
		t_info("dns_rbt_findnode failed %s\n", dns_result_totext(dns_result));
		return(result);
	}

	/* check next */
	t_info("checking for next name of %s and new origin of %s\n",
			prevname, prevorigin);
	dns_result = dns_rbtnodechain_prev(&chain,
			dns_fixedname_name(&dns_prevname),
			dns_fixedname_name(&dns_origin));

	if ((dns_result != DNS_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
		t_info("dns_rbtnodechain_prev unexpectedly returned %s\n",
				dns_result_totext(dns_result));
	}

Bob Halley's avatar
Bob Halley committed
1527
1528
	nfails += t_namechk(dns_result, &dns_prevname, prevname, &dns_origin, prevorigin,
			    DNS_R_NEWORIGIN);
William King's avatar
William King committed
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618

	if (nfails)
		result = T_FAIL;
	else
		result = T_PASS;
		
	dns_rbt_destroy(&rbt);
	isc_mem_destroy(&mctx);
	return(result);
}

static int
test_dns_rbtnodechain_prev(char *filename) {

	FILE		*fp;
	char		*p;
	int		line;
	int		cnt;
	int		result;
	int		nfails;
	int		nprobs;

	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((int)*p)) || (*p == '#'))
				continue;

			cnt = t_bustline(p, Tokens);
			if (cnt == 4) {
				result = t_dns_rbtnodechain_prev(
						Tokens[0],	/* dbfile */
						Tokens[1],	/* findname */
						Tokens[2],	/* expected prevname */
						Tokens[3]);	/* expected prevorigin */
				if (result != T_PASS) {
					if (result == T_FAIL)
						++nfails;
					else
						++nprobs;
				}
			}
			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))
		result = T_PASS;
	else if (nfails)
		result = T_FAIL;

	return(result);
}

static char	*a13 =	"a call to dns_rbtnodechain_prev(chain, name, origin) "
			"sets name to point to the previous node of the tree "
			"and returns DNS_R_SUCCESS or DNS_R_NEWORIGIN on success";

static void
t13() {
	int	result;

	t_assert("dns_rbtnodechain_prev", 13, T_REQUIRED, a13);
	result = test_dns_rbtnodechain_prev("dns_rbtnodechain_prev_data");
	t_result(result);
}



Bob Halley's avatar
Bob Halley committed
1619
testspec_t	T_testlist[] = {
William King's avatar
William King committed
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
	{	t1,	"dns_rbt_create"		},
	{	t2,	"dns_rbt_addname 1"		},
	{	t3,	"dns_rbt_addname 2"		},
	{	t4,	"dns_rbt_deletename 1"		},
	{	t5,	"dns_rbt_deletename 2"		},
	{	t6,	"dns_rbt_findname 1"		},
	{	t7,	"dns_rbt_findname 2"		},
	{	t8,	"dns_rbt_findname 3"		},
	{	t9,	"dns_rbtnodechain_init"		},
	{	t10,	"dns_rbtnodechain_first"	},
	{	t11,	"dns_rbtnodechain_last"		},
	{	t12,	"dns_rbtnodechain_next"		},
	{	t13,	"dns_rbtnodechain_prev"		},
	{	NULL,	NULL				}
Bob Halley's avatar
Bob Halley committed
1634
1635
};