t_rbt.c 37.7 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1998, 1999, 2000  Internet Software Consortium.
Bob Halley's avatar
Bob Halley committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 * 
 * 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.
 */

18
#include <config.h>
Bob Halley's avatar
Bob Halley committed
19

20
21
#include <ctype.h>
#include <stdlib.h>
Bob Halley's avatar
Bob Halley committed
22

23
24
#include <isc/mem.h>
#include <isc/string.h>
Bob Halley's avatar
Bob Halley committed
25

26
27
#include <dns/rbt.h>
#include <dns/fixedname.h>
Bob Halley's avatar
Bob Halley committed
28

29
#include <tests/t_api.h>
Bob Halley's avatar
Bob Halley committed
30

William King's avatar
William King committed
31
32
#define		BUFLEN	1024
#define		DNSNAMELEN 255
Bob Halley's avatar
Bob Halley committed
33

William King's avatar
William King committed
34
35
char		*progname;
char		*Tokens[T_MAXTOKS];
Bob Halley's avatar
Bob Halley committed
36

37
38
static int
t_dns_rbtnodechain_init(char *dbfile, char *findname,
William King's avatar
William King committed
39
40
41
42
			char *firstname, char *firstorigin,
			char *nextname, char *nextorigin,
			char *prevname, char *prevorigin,
			char *lastname, char *lastorigin);
43
44
45
46
47
static char *
fixedname_totext(dns_fixedname_t *name);

static int
fixedname_cmp(dns_fixedname_t *dns_name, char *txtname);
William King's avatar
William King committed
48

49
50
static char *
dnsname_totext(dns_name_t *name);
William King's avatar
William King committed
51

52
53
54
55
56
57
58
59
static int
t_namechk(isc_result_t dns_result, dns_fixedname_t *dns_name, char *exp_name,
	  dns_fixedname_t *dns_origin, char *exp_origin,
	  isc_result_t exp_result);

/*
 * Parts adapted from the original rbt_test.c.
 */
William King's avatar
William King committed
60
61
62
63
64
65
66
67
68
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);
69
	} else {
William King's avatar
William King committed
70
71
72
73
74
75
76
77
78
		return(strcmp(name, txtname));
	}
}

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

79
	isc_buffer_init(&target, buf, BUFLEN);
William King's avatar
William King committed
80
81
82
83
84
85
86
87
88
89
90
	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);
91
	isc_buffer_init(&target, buf, BUFLEN);
William King's avatar
William King committed
92
93
94
95
	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
96
97
98

#ifdef	NEED_PRINT_DATA

99
static isc_result_t
Bob Halley's avatar
Bob Halley committed
100
print_data(void *data) {
101
	isc_result_t	dns_result;
Bob Halley's avatar
Bob Halley committed
102
103
104
	isc_buffer_t	target;
	char		*buffer[DNSNAMELEN];

105
	isc_buffer_init(&target, buffer, sizeof(buffer));
Bob Halley's avatar
Bob Halley committed
106
107

	dns_result = dns_name_totext(data, ISC_FALSE, &target);
108
	if (dns_result != ISC_R_SUCCESS) {
Bob Halley's avatar
Bob Halley committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
		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);
	
131
		isc_buffer_init(&source, s, length);
Bob Halley's avatar
Bob Halley committed
132
133
134
135
136
137
138
139
140
141
142
143
144
		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);
145
		isc_buffer_init(&target, *dns_name + 1, DNSNAMELEN);
Bob Halley's avatar
Bob Halley committed
146
147
148
149
	
		result = dns_name_fromtext(*dns_name, &source, dns_rootname,
					   ISC_FALSE, &target);
	
150
		if (result != ISC_R_SUCCESS) {
Bob Halley's avatar
Bob Halley committed
151
152
153
154
			++nfails;
			t_info("dns_name_fromtext(%s) failed %s\n",
			       s, dns_result_totext(result));
		}
155
	} else {
Bob Halley's avatar
Bob Halley committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
		++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);
}


172
173
174
/*
 * Adapted from the original rbt_test.c.
 */
Bob Halley's avatar
Bob Halley committed
175
static int
176
t1_add(char *name, dns_rbt_t *rbt, isc_mem_t *mctx, isc_result_t *dns_result) {
Bob Halley's avatar
Bob Halley committed
177
178
179
180
181
182
	int		nprobs;
	dns_name_t	*dns_name;

	nprobs = 0;
	if (name && dns_result) {
		*dns_result = create_name(name, mctx, &dns_name);
183
		if (*dns_result == ISC_R_SUCCESS) {
William King's avatar
William King committed
184
185
			if (T_debug)
				t_info("dns_rbt_addname succeeded\n");
Bob Halley's avatar
Bob Halley committed
186
			*dns_result = dns_rbt_addname(rbt, dns_name, dns_name);
187
		} else {
William King's avatar
William King committed
188
189
190
			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
191
192
			++nprobs;
		}
193
	} else {
Bob Halley's avatar
Bob Halley committed
194
195
196
197
198
199
		++nprobs;
	}
	return(nprobs);
}

static int
200
201
202
t1_delete(char *name, dns_rbt_t *rbt, isc_mem_t *mctx,
	  isc_result_t *dns_result)
{
Bob Halley's avatar
Bob Halley committed
203
204
205
206
207
208
	int		nprobs;
	dns_name_t	*dns_name;

	nprobs = 0;
	if (name && dns_result) {
		*dns_result = create_name(name, mctx, &dns_name);
209
		if (*dns_result == ISC_R_SUCCESS) {
210
211
			*dns_result = dns_rbt_deletename(rbt, dns_name,
							 ISC_FALSE);
Bob Halley's avatar
Bob Halley committed
212
			delete_name(dns_name, mctx);
213
		} else {
Bob Halley's avatar
Bob Halley committed
214
215
			++nprobs;
		}
216
	} else {
Bob Halley's avatar
Bob Halley committed
217
218
219
220
221
222
		++nprobs;
	}
	return(nprobs);
}

static int
223
224
225
t1_search(char *name, dns_rbt_t *rbt, isc_mem_t *mctx,
	  isc_result_t *dns_result)
{
Bob Halley's avatar
Bob Halley committed
226
227
228
229
230
231
232
233
234
	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);
235
		if (*dns_result == ISC_R_SUCCESS) {
Bob Halley's avatar
Bob Halley committed
236
237
238
			dns_fixedname_init(&dns_fixedname);
			dns_foundname = dns_fixedname_name(&dns_fixedname);
			data = NULL;
Bob Halley's avatar
Bob Halley committed
239
			*dns_result = dns_rbt_findname(rbt, dns_searchname, 0,
Bob Halley's avatar
Bob Halley committed
240
241
						dns_foundname, &data);
			delete_name(dns_searchname, mctx);
242
		} else {
Bob Halley's avatar
Bob Halley committed
243
244
			++nprobs;
		}
245
	} else {
Bob Halley's avatar
Bob Halley committed
246
247
248
249
250
		++nprobs;
	}
	return(nprobs);
}

251
252
253
/*
 * Initialize a database from filename.
 */
Bob Halley's avatar
Bob Halley committed
254
static int
William King's avatar
William King committed
255
rbt_init(char *filename, dns_rbt_t **rbt, isc_mem_t *mctx) {
Bob Halley's avatar
Bob Halley committed
256
	int		rval;
257
	isc_result_t	dns_result;
William King's avatar
William King committed
258
	char		*p;
Bob Halley's avatar
Bob Halley committed
259
260
261
262
263
	FILE		*fp;

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

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

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

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

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

William King's avatar
William King committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
	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
331
332
333
		
	/* now try the database command */
	if (strcmp(command, "create") == 0) {
William King's avatar
William King committed
334
		result = T_PASS;
335
	} else if (strcmp(command, "add") == 0) {
William King's avatar
William King committed
336
		dns_result = create_name(testname, mctx, &dns_name);
337
		if (dns_result == ISC_R_SUCCESS) {
William King's avatar
William King committed
338
339
			dns_result = dns_rbt_addname(rbt, dns_name, dns_name);

340
			if (dns_result != ISC_R_SUCCESS)
William King's avatar
William King committed
341
342
				delete_name(dns_name, mctx);

Bob Halley's avatar
Bob Halley committed
343
			if (dns_result == exp_result) {
344
				if (dns_result == ISC_R_SUCCESS) {
345
346
					rval = t1_search(testname, rbt, mctx,
							 &dns_result);
William King's avatar
William King committed
347
					if (rval == 0) {
348
349
350
351
352
353
					     if (dns_result == ISC_R_SUCCESS) {
						     result = T_PASS;
					     } else {
						     result = T_FAIL;
					     }
					} else {
William King's avatar
William King committed
354
355
						t_info("t1_search failed\n");
						result = T_UNRESOLVED;
Bob Halley's avatar
Bob Halley committed
356
					}
357
				} else {
William King's avatar
William King committed
358
359
					result = T_PASS;
				}
360
361
362
363
364
			} else {
				t_info("dns_rbt_addname returned %s, "
				       "expected %s\n",
				       dns_result_totext(dns_result),
				       dns_result_totext(exp_result));
Bob Halley's avatar
Bob Halley committed
365
366
				result = T_FAIL;
			}
367
		} else {
William King's avatar
William King committed
368
369
370
371
			t_info("create_name failed %s\n",
				dns_result_totext(dns_result));
			result = T_UNRESOLVED;
		}
372
373
	} else if ((strcmp(command, "delete") == 0) ||
		   (strcmp(command, "nuke") == 0)) {
William King's avatar
William King committed
374
		rval = t1_delete(testname, rbt, mctx, &dns_result);
Bob Halley's avatar
Bob Halley committed
375
376
		if (rval == 0) {
			if (dns_result == exp_result) {
377
378
				rval = t1_search(testname, rbt, mctx,
						 &dns_result);
Bob Halley's avatar
Bob Halley committed
379
				if (rval == 0) {
380
					if (dns_result == ISC_R_SUCCESS) {
381
382
383
						t_info("dns_rbt_deletename "
						       "didn't delete "
						       "the name");
Bob Halley's avatar
Bob Halley committed
384
						result = T_FAIL;
385
					} else {
Bob Halley's avatar
Bob Halley committed
386
387
388
						result = T_PASS;
					}
				}
389
			} else {
Bob Halley's avatar
Bob Halley committed
390
391
392
393
394
395
				t_info("delete returned %s, expected %s\n",
					dns_result_totext(dns_result),
					dns_result_totext(exp_result));
				result = T_FAIL;
			}
		}
396
	} else if (strcmp(command, "search") == 0) {
William King's avatar
William King committed
397
		rval = t1_search(testname, rbt, mctx, &dns_result);
Bob Halley's avatar
Bob Halley committed
398
399
400
		if (rval == 0) {
			if (dns_result == exp_result) {
				result = T_PASS;
401
			} else {
Bob Halley's avatar
Bob Halley committed
402
403
404
405
406
407
408
409
410
				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
411
	isc_mem_destroy(&mctx);
Bob Halley's avatar
Bob Halley committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
	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;

435
436
437
			/*
			 * Skip comment lines.
			 */
William King's avatar
William King committed
438
			if ((isspace((int)*p)) || (*p == '#'))
Bob Halley's avatar
Bob Halley committed
439
440
				continue;

441
442
443
444
			/*
			 * Name of db file, command, testname,
			 * expected result.
			 */
Bob Halley's avatar
Bob Halley committed
445
446
			cnt = t_bustline(p, Tokens);
			if (cnt == 4) {
447
448
449
				result = test_rbt_gen(Tokens[0], Tokens[1],
					     Tokens[2],
					     t_dns_result_fromtext(Tokens[3]));
Bob Halley's avatar
Bob Halley committed
450
451
				if (result != T_PASS)
					++nfails;
452
			} else {
Bob Halley's avatar
Bob Halley committed
453
454
455
456
457
				t_info("bad format in %s at line %d\n",
						filename, line);
				++nprobs;
			}

458
			(void)free(p);
Bob Halley's avatar
Bob Halley committed
459
		}
460
461
		(void)fclose(fp);
	} else {
Bob Halley's avatar
Bob Halley committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
		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);
}


476
477
static char	*a1 =	"dns_rbt_create creates a rbt and returns "
			"ISC_R_SUCCESS on success";
Bob Halley's avatar
Bob Halley committed
478
479
480
481
482
483
484
485
486
487

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);
}

488
489
490
static char	*a2 =	"dns_rbt_addname adds a name to a database and "
			"returns ISC_R_SUCCESS on success";

Bob Halley's avatar
Bob Halley committed
491
492
493
494
495
496
497
498
499
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);
}

500
501
static char	*a3 =	"when name already exists, dns_rbt_addname() "
			"returns ISC_R_EXISTS";
Bob Halley's avatar
Bob Halley committed
502
503
504
505
506
507
508
509
510
511
512

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 "
513
			"ISC_R_SUCCESS";
Bob Halley's avatar
Bob Halley committed
514
515
516
517
518
519
520
521
522
523

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);
}

524
525
static char	*a5 =	"when name does not exist, dns_rbt_deletename() "
			"returns ISC_R_NOTFOUND";
Bob Halley's avatar
Bob Halley committed
526
527
528
529
530
531
532
533
534
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);
}

535
536
static char	*a6 =	"when name exists and exactly matches the "
			"search name dns_rbt_findname() returns ISC_R_SUCCESS";
Bob Halley's avatar
Bob Halley committed
537
538
539
540
541
542
543
544
545
546
547

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, "
548
			"dns_rbt_findname returns ISC_R_NOTFOUND";
Bob Halley's avatar
Bob Halley committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570

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
571
572
573
574
575
576
577
578
579
580
581

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;
582
	isc_result_t	dns_result;
William King's avatar
William King committed
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
	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));
610
		} else {
William King's avatar
William King committed
611
			dns_fixedname_init(&fullname1);
612
613
614
615
616
			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);
617
			if (dns_result != ISC_R_SUCCESS) {
William King's avatar
William King committed
618
619
620
621
622
623
				t_info("dns_name_concatenate failed %s\n",
						dns_result_totext(dns_result));
				++nprobs;
				break;
			}

624
625
626
627
			/*
			 * Expecting origin not to get touched if next
			 * doesn't return NEWORIGIN.
			 */
William King's avatar
William King committed
628
			dns_fixedname_init(&name);
629
630
631
632
633
			dns_result = dns_rbtnodechain_next(chain,
						  dns_fixedname_name(&name),
						  dns_fixedname_name(&origin));
			if ((dns_result != ISC_R_SUCCESS) &&
			    (dns_result != DNS_R_NEWORIGIN)) {
634
				if (dns_result != ISC_R_NOMORE) {
635
636
637
					t_info("dns_rbtnodechain_next "
					       "failed %s\n",
					       dns_result_totext(dns_result));
William King's avatar
William King committed
638
639
640
641
642
643
					++nprobs;
				}
				break;
			}

			t_info("next name:\t<%s>\n", fixedname_totext(&name));
644
645
			t_info("next origin:\t<%s>\n",
			       fixedname_totext(&origin));
William King's avatar
William King committed
646
647

			dns_fixedname_init(&fullname2);
648
649
650
651
652
			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);
653
			if (dns_result != ISC_R_SUCCESS) {
654
655
				t_info("dns_name_concatenate failed %s\n",
				       dns_result_totext(dns_result));
William King's avatar
William King committed
656
657
658
659
				++nprobs;
				break;
			}

660
661
			t_info("comparing\t<%s>\n",
			       fixedname_totext(&fullname1));
William King's avatar
William King committed
662
663
664
665
666
667
668
669
			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) {
670
671
672
673
			    t_info("unexpected order %s %s %s\n",
			       dnsname_totext(dns_fixedname_name(&fullname1)),
			       order == -1 ? "<" : (order == 0 ? "==" : ">"),
			       dnsname_totext(dns_fixedname_name(&fullname2)));
William King's avatar
William King committed
674
675
676
677
678
679
680
681
682
				++nprobs;
			}
		}

		++cnt;
	}
	return(nprobs);
}

683
684
685
/*
 * Test by exercising the first|last|next|prev funcs in useful ways.
 */
William King's avatar
William King committed
686
687

static int
688
t_namechk(isc_result_t dns_result, dns_fixedname_t *dns_name, char *exp_name,
Bob Halley's avatar
Bob Halley committed
689
	  dns_fixedname_t *dns_origin, char *exp_origin,
690
	  isc_result_t exp_result)
Bob Halley's avatar
Bob Halley committed
691
{
William King's avatar
William King committed
692
693
694
695
696
697
698
699
700
701
702
	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
703
		if (dns_result == exp_result) {
William King's avatar
William King committed
704
			if (fixedname_cmp(dns_origin, exp_origin)) {
Bob Halley's avatar
Bob Halley committed
705
706
707
				t_info("\torigin %s, expected %s\n",
				       fixedname_totext(dns_origin),
				       exp_origin);
William King's avatar
William King committed
708
709
				++nfails;
			}
Bob Halley's avatar
Bob Halley committed
710
		} else {
William King's avatar
William King committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
			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;
734
	isc_result_t		dns_result;
William King's avatar
William King committed
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
	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) {
751
752
		t_info("isc_mem_create failed %s\n",
		       isc_result_totext(isc_result));
William King's avatar
William King committed
753
754
755
756
757
758
759
760
761
762
763
764
765
		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);
766
	isc_buffer_init(&isc_buffer, findname, len);
William King's avatar
William King committed
767
768
769
770
771
772
773
774
775
776
777
778
779
	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);

780
	if (dns_result != ISC_R_SUCCESS) {
781
782
		t_info("dns_name_fromtext failed %s\n",
		       dns_result_totext(dns_result));
William King's avatar
William King committed
783
784
785
		return(result);
	}

786
787
788
	/*
	 * Set the starting node.
	 */
Bob Halley's avatar
Bob Halley committed
789
	node = NULL;
William King's avatar
William King committed
790
	dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
Bob Halley's avatar
Bob Halley committed
791
792
793
				      dns_fixedname_name(&dns_foundname),
				      &node, &chain, DNS_RBTFIND_EMPTYDATA,
				      NULL, NULL);
William King's avatar
William King committed
794

795
	if (dns_result != ISC_R_SUCCESS) {
796
797
		t_info("dns_rbt_findnode failed %s\n",
		       dns_result_totext(dns_result));
William King's avatar
William King committed
798
799
800
		return(result);
	}

801
802
803
	/*
	 * Check next.
	 */
William King's avatar
William King committed
804
	t_info("checking for next name of %s and new origin of %s\n",
805
	       nextname, nextorigin);
William King's avatar
William King committed
806
	dns_result = dns_rbtnodechain_next(&chain,
807
808
					   dns_fixedname_name(&dns_nextname),
					   dns_fixedname_name(&dns_origin));
William King's avatar
William King committed
809

810
811
	if ((dns_result != ISC_R_SUCCESS) &&
	    (dns_result != DNS_R_NEWORIGIN)) {
William King's avatar
William King committed
812
		t_info("dns_rbtnodechain_next unexpectedly returned %s\n",
813
		       dns_result_totext(dns_result));
William King's avatar
William King committed
814
815
	}

816
817
	nfails += t_namechk(dns_result, &dns_nextname, nextname, &dns_origin,
			    nextorigin, DNS_R_NEWORIGIN);
William King's avatar
William King committed
818

819
820
821
	/*
	 * Set the starting node again.
	 */
William King's avatar
William King committed
822
823
824
	node = NULL;
	dns_fixedname_init(&dns_foundname);
	dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
Bob Halley's avatar
Bob Halley committed
825
826
827
				      dns_fixedname_name(&dns_foundname),
				      &node, &chain, DNS_RBTFIND_EMPTYDATA,
				      NULL, NULL);
William King's avatar
William King committed
828

829
	if (dns_result != ISC_R_SUCCESS) {
830
831
		t_info("\tdns_rbt_findnode failed %s\n",
		       dns_result_totext(dns_result));
William King's avatar
William King committed
832
833
834
		return(result);
	}

835
836
837
	/*
	 * Check previous.
	 */
William King's avatar
William King committed
838
	t_info("checking for previous name of %s and new origin of %s\n",
839
	       prevname, prevorigin);
William King's avatar
William King committed
840
841
	dns_fixedname_init(&dns_origin);
	dns_result = dns_rbtnodechain_prev(&chain,
842
843
					   dns_fixedname_name(&dns_prevname),
					   dns_fixedname_name(&dns_origin));
William King's avatar
William King committed
844

845
846
	if ((dns_result != ISC_R_SUCCESS) &&
	    (dns_result != DNS_R_NEWORIGIN)) {
William King's avatar
William King committed
847
		t_info("dns_rbtnodechain_prev unexpectedly returned %s\n",
848
		       dns_result_totext(dns_result));
William King's avatar
William King committed
849
	}
850
851
	nfails += t_namechk(dns_result, &dns_prevname, prevname, &dns_origin,
			    prevorigin, DNS_R_NEWORIGIN);
William King's avatar
William King committed
852

853
854
855
	/*
	 * Check first.
	 */
William King's avatar
William King committed
856
	t_info("checking for first name of %s and new origin of %s\n",
857
	       firstname, firstorigin);
William King's avatar
William King committed
858
	dns_result = dns_rbtnodechain_first(&chain, rbt,
859
860
					    dns_fixedname_name(&dns_firstname),
					    dns_fixedname_name(&dns_origin));
William King's avatar
William King committed
861
862
863

	if (dns_result != DNS_R_NEWORIGIN) {
		t_info("dns_rbtnodechain_first unexpectedly returned %s\n",
864
		       dns_result_totext(dns_result));
William King's avatar
William King committed
865
	}
866
867
	nfails += t_namechk(dns_result, &dns_firstname, firstname,
			    &dns_origin, firstorigin, DNS_R_NEWORIGIN);
William King's avatar
William King committed
868

869
870
871
	/*
	 * Check last.
	 */
William King's avatar
William King committed
872
873
	t_info("checking for last name of %s\n", lastname);
	dns_result = dns_rbtnodechain_last(&chain, rbt,
874
875
					   dns_fixedname_name(&dns_lastname),
					   dns_fixedname_name(&dns_origin));
William King's avatar
William King committed
876
877
878

	if (dns_result != DNS_R_NEWORIGIN) {
		t_info("dns_rbtnodechain_last unexpectedly returned %s\n",
879
		       dns_result_totext(dns_result));
William King's avatar
William King committed
880
	}
881
882
	nfails += t_namechk(dns_result, &dns_lastname, lastname, &dns_origin,
			    lastorigin, DNS_R_NEWORIGIN);
William King's avatar
William King committed
883

884
885
886
	/*
	 * Check node ordering.
	 */
William King's avatar
William King committed
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
	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(
927
928
929
930
931
932
933
934
935
936
						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 */
William King's avatar
William King committed
937
938
939
940
941
942
				if (result != T_PASS) {
					if (result == T_FAIL)
						++nfails;
					else
						++nprobs;
				}
943
			} else {
William King's avatar
William King committed
944
945
946
947
948
				t_info("bad format in %s at line %d\n",
						filename, line);
				++nprobs;
			}

949
			(void)free(p);
William King's avatar
William King committed
950
		}
951
952
		(void)fclose(fp);
	} else {
William King's avatar
William King committed
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
		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,
980
981
				char *expected_nextorigin)
{
William King's avatar
William King committed
982
983
984
985
986
987
	int			result;
	int			nfails;
	dns_rbt_t		*rbt;
	dns_rbtnodechain_t	chain;
	isc_mem_t		*mctx;
	isc_result_t		isc_result;
988
	isc_result_t		dns_result;
William King's avatar
William King committed
989
990
	dns_fixedname_t		dns_name;
	dns_fixedname_t		dns_origin;
991
	isc_result_t		expected_result;
William King's avatar
William King committed
992
993
994
995
996
997
998
999
1000
1001
1002

	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) {
1003
1004
		t_info("isc_mem_create failed %s\n",
		       isc_result_totext(isc_result));
William King's avatar
William King committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
		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
1028
1029
	nfails = t_namechk(dns_result, &dns_name, expected_firstname,
			   &dns_origin, expected_firstorigin, DNS_R_NEWORIGIN);
William King's avatar
William King committed
1030
1031
1032
1033
1034
1035
1036
1037
1038

	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);
			
1039
	if ((dns_result != ISC_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
William King's avatar
William King committed
1040
1041
1042
		t_info("dns_rbtnodechain_next unexpectedly returned %s\n",
				dns_result_totext(dns_result));
	}
Bob Halley's avatar
Bob Halley committed
1043
	if (strcasecmp(expected_firstorigin, expected_nextorigin) == 0)
1044
		expected_result = ISC_R_SUCCESS;
Bob Halley's avatar
Bob Halley committed
1045
1046
1047
1048
	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
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

	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;

1080
1081
1082
			/*
			 * Skip comment lines.
			 */
William King's avatar
William King committed
1083
1084
1085
1086
1087
1088
			if ((isspace((int)*p)) || (*p == '#'))
				continue;

			cnt = t_bustline(p, Tokens);
			if (cnt == 5) {
				result = t_dns_rbtnodechain_first(
1089
1090
1091
1092
1093
						Tokens[0],  /* dbfile */
						Tokens[1],  /* firstname */
						Tokens[2],  /* firstorigin */
						Tokens[3],  /* nextname */
						Tokens[4]); /* nextorigin */
William King's avatar
William King committed
1094
1095
1096
1097
1098
1099
				if (result != T_PASS) {
					if (result == T_FAIL)
						++nfails;
					else
						++nprobs;
				}
1100
			} else {
William King's avatar
William King committed
1101
1102
1103
1104
1105
				t_info("bad format in %s at line %d\n",
						filename, line);
				++nprobs;
			}

1106
			(void)free(p);
William King's avatar
William King committed
1107
		}
1108
1109
		(void)fclose(fp);
	} else {
William King's avatar
William King committed
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
		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);
}

1124
1125
static char	*a10 =	"a call to "
			"dns_rbtnodechain_first(chain, rbt, name, origin) "
William King's avatar
William King committed
1126
1127
			"sets name to point to the root of the tree, "
			"origin to point to the origin, "
1128
			"and returns DNS_R_NEWORIGIN";
William King's avatar
William King committed
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140

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,
1141
1142
1143
1144
			char *expected_lastorigin,
			char *expected_prevname,
			char *expected_prevorigin)
{
William King's avatar
William King committed
1145
1146
1147
1148
1149
1150
1151

	int			result;
	int			nfails;
	dns_rbt_t		*rbt;
	dns_rbtnodechain_t	chain;
	isc_mem_t		*mctx;
	isc_result_t		isc_result;
1152
	isc_result_t		dns_result;
William King's avatar
William King committed
1153
1154
	dns_fixedname_t		dns_name;
	dns_fixedname_t		dns_origin;
1155
	isc_result_t		expected_result;
William King's avatar
William King committed
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166

	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) {
1167
1168
		t_info("isc_mem_create failed %s\n",
		       isc_result_totext(isc_result));
William King's avatar
William King committed
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
		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);
	}

1181
1182
	t_info("testing for last name of %s, origin of %s\n",
	       expected_lastname, expected_lastorigin);
William King's avatar
William King committed
1183
1184

	dns_result = dns_rbtnodechain_last(&chain, rbt,
1185
1186
					   dns_fixedname_name(&dns_name),
					   dns_fixedname_name(&dns_origin));
William King's avatar
William King committed
1187
1188
1189

	if (dns_result != DNS_R_NEWORIGIN) {
		t_info("dns_rbtnodechain_last unexpectedly returned %s\n",
1190
		       dns_result_totext(dns_result));
William King's avatar
William King committed
1191
	}
1192
1193
	nfails = t_namechk(dns_result, &dns_name, expected_lastname,
			   &dns_origin, expected_lastorigin, DNS_R_NEWORIGIN);
William King's avatar
William King committed
1194

1195
1196
	t_info("testing for previous name of %s, origin of %s\n",
	       expected_prevname, expected_prevorigin);
William King's avatar
William King committed
1197
1198
1199

	dns_fixedname_init(&dns_name);
	dns_result = dns_rbtnodechain_prev(&chain,
1200
1201
					   dns_fixedname_name(&dns_name),
					   dns_fixedname_name(&dns_origin));
William King's avatar
William King committed
1202

1203
1204
	if ((dns_result != ISC_R_SUCCESS) &&
	    (dns_result != DNS_R_NEWORIGIN)) {
William King's avatar
William King committed
1205
		t_info("dns_rbtnodechain_prev unexpectedly returned %s\n",
1206
		       dns_result_totext(dns_result));
William King's avatar
William King committed
1207
	}
Bob Halley's avatar
Bob Halley committed
1208
	if (strcasecmp(expected_lastorigin, expected_prevorigin) == 0)
1209
		expected_result = ISC_R_SUCCESS;
Bob Halley's avatar
Bob Halley committed
1210
1211
1212
1213
	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
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

	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;

1245
1246
1247
			/*
			 * Skip comment lines.
			 */
William King's avatar
William King committed
1248
1249
1250
1251
1252
1253
			if ((isspace((int)*p)) || (*p == '#'))
				continue;

			cnt = t_bustline(p, Tokens);
			if (cnt == 5) {
				result = t_dns_rbtnodechain_last(
1254
1255
1256
1257
1258
						Tokens[0],     /* dbfile */
						Tokens[1],     /* lastname */
						Tokens[2],     /* lastorigin */
						Tokens[3],     /* prevname */
						Tokens[4]);    /* prevorigin */
William King's avatar
William King committed
1259
1260
1261
1262
1263
1264
				if (result != T_PASS) {
					if (result == T_FAIL)
						++nfails;
					else
						++nprobs;
				}
1265
			} else {
William King's avatar
William King committed
1266
				t_info("bad format in %s at line %d\n",
1267
				       filename, line);
William King's avatar
William King committed
1268
1269
1270
				++nprobs;
			}

1271
			(void)free(p);
William King's avatar
William King committed
1272
		}
1273
1274
		(void)fclose(fp);
	} else {
William King's avatar
William King committed
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
		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);
}

1289
1290
static char	*a11 =	"a call to "
			"dns_rbtnodechain_last(chain, rbt, name, origin) "
William King's avatar
William King committed
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
			"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,
1306
1307
			char *nextname, char *nextorigin)
{
William King's avatar
William King committed
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317

	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;
1318
	isc_result_t		dns_result;
William King's avatar
William King committed
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
	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) {
1332
1333
		t_info("isc_mem_create failed %s\n",
		       isc_result_totext(isc_result));
William King's avatar
William King committed
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
		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);
1347
	isc_buffer_init(&isc_buffer, findname, len);
William King's avatar
William King committed
1348
1349
1350
1351
1352
1353
1354
1355
	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),
1356
				       &isc_buffer, NULL, ISC_FALSE, NULL);
William King's avatar
William King committed
1357

1358
	if (dns_result != ISC_R_SUCCESS) {
1359
1360
		t_info("dns_name_fromtext failed %s\n",
		       dns_result_totext(dns_result));
William King's avatar
William King committed
1361
1362
1363
		return(result);
	}

1364
1365
1366
	/*
	 * Set the starting node.
	 */
Bob Halley's avatar
Bob Halley committed
1367
	node = NULL;
William King's avatar
William King committed
1368
	dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
Bob Halley's avatar
Bob Halley committed
1369
1370
1371
				      dns_fixedname_name(&dns_foundname),
				      &node, &chain, DNS_RBTFIND_EMPTYDATA,
				      NULL, NULL);
William King's avatar
William King committed
1372

1373
	if (dns_result != ISC_R_SUCCESS) {
1374
1375
		t_info("dns_rbt_findnode failed %s\n",
		       dns_result_totext(dns_result));