t_rbt.c 37.6 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
		++nfails;
		t_info("create_name: empty name\n");
	}

	return(nfails);
}

static void
delete_name(void *data, void *arg) {
165
	isc_mem_put((isc_mem_t *)arg, data, sizeof(dns_name_t) + DNSNAMELEN);
Bob Halley's avatar
Bob Halley committed
166
167
168
}


169
170
171
/*
 * Adapted from the original rbt_test.c.
 */
Bob Halley's avatar
Bob Halley committed
172
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
	int		nprobs;
	dns_name_t	*dns_name;

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

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

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

static int
220
221
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
	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);
232
		if (*dns_result == ISC_R_SUCCESS) {
Bob Halley's avatar
Bob Halley committed
233
234
235
			dns_fixedname_init(&dns_fixedname);
			dns_foundname = dns_fixedname_name(&dns_fixedname);
			data = NULL;
Bob Halley's avatar
Bob Halley committed
236
			*dns_result = dns_rbt_findname(rbt, dns_searchname, 0,
Bob Halley's avatar
Bob Halley committed
237
238
						dns_foundname, &data);
			delete_name(dns_searchname, mctx);
239
		} else {
Bob Halley's avatar
Bob Halley committed
240
241
			++nprobs;
		}
242
	} else {
Bob Halley's avatar
Bob Halley committed
243
244
245
246
247
		++nprobs;
	}
	return(nprobs);
}

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

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

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

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

static int
297
298
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;
332
	} else if (strcmp(command, "add") == 0) {
William King's avatar
William King committed
333
		dns_result = create_name(testname, mctx, &dns_name);
334
		if (dns_result == ISC_R_SUCCESS) {
William King's avatar
William King committed
335
336
			dns_result = dns_rbt_addname(rbt, dns_name, dns_name);

337
			if (dns_result != ISC_R_SUCCESS)
William King's avatar
William King committed
338
339
				delete_name(dns_name, mctx);

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

432
433
434
			/*
			 * Skip comment lines.
			 */
435
			if ((isspace((unsigned char)*p)) || (*p == '#'))
Bob Halley's avatar
Bob Halley committed
436
437
				continue;

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

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


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

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

485
486
487
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
488
489
490
491
492
493
494
495
496
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);
}

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

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

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

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

532
533
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
534
535
536
537
538
539
540
541
542
543
544

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

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
568
569
570
571
572
573
574
575
576
577
578

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;
579
	isc_result_t	dns_result;
William King's avatar
William King committed
580
581
582
583
584
585
586
587
588

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

	cnt = 0;
	nprobs = 0;

589
	do {
William King's avatar
William King committed
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604

		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));
605
606
			t_info("first origin:\t<%s>\n",
			       fixedname_totext(&origin));
607
		} else {
William King's avatar
William King committed
608
			dns_fixedname_init(&fullname1);
609
610
611
612
613
			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);
614
			if (dns_result != ISC_R_SUCCESS) {
William King's avatar
William King committed
615
616
617
618
619
620
				t_info("dns_name_concatenate failed %s\n",
						dns_result_totext(dns_result));
				++nprobs;
				break;
			}

621
622
623
624
			/*
			 * Expecting origin not to get touched if next
			 * doesn't return NEWORIGIN.
			 */
William King's avatar
William King committed
625
			dns_fixedname_init(&name);
626
627
628
629
630
			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)) {
631
				if (dns_result != ISC_R_NOMORE) {
632
633
634
					t_info("dns_rbtnodechain_next "
					       "failed %s\n",
					       dns_result_totext(dns_result));
William King's avatar
William King committed
635
636
637
638
639
640
					++nprobs;
				}
				break;
			}

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

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

657
658
			t_info("comparing\t<%s>\n",
			       fixedname_totext(&fullname1));
William King's avatar
William King committed
659
660
			t_info("\twith\t<%s>\n", fixedname_totext(&fullname2));

661
			(void)dns_name_fullcompare(
William King's avatar
William King committed
662
663
664
665
666
						dns_fixedname_name(&fullname1),
						dns_fixedname_name(&fullname2),
						&order, &nlabels, &nbits);

			if (order >= 0) {
667
668
669
670
			    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
671
672
673
674
675
				++nprobs;
			}
		}

		++cnt;
676
677
678
	} while (1);

	return (nprobs);
William King's avatar
William King committed
679
680
}

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

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

776
	if (dns_result != ISC_R_SUCCESS) {
777
778
		t_info("dns_name_fromtext failed %s\n",
		       dns_result_totext(dns_result));
William King's avatar
William King committed
779
780
781
		return(result);
	}

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

791
	if (dns_result != ISC_R_SUCCESS) {
792
793
		t_info("dns_rbt_findnode failed %s\n",
		       dns_result_totext(dns_result));
William King's avatar
William King committed
794
795
796
		return(result);
	}

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

806
807
	if ((dns_result != ISC_R_SUCCESS) &&
	    (dns_result != DNS_R_NEWORIGIN)) {
William King's avatar
William King committed
808
		t_info("dns_rbtnodechain_next unexpectedly returned %s\n",
809
		       dns_result_totext(dns_result));
William King's avatar
William King committed
810
811
	}

812
813
	nfails += t_namechk(dns_result, &dns_nextname, nextname, &dns_origin,
			    nextorigin, DNS_R_NEWORIGIN);
William King's avatar
William King committed
814

815
816
817
	/*
	 * Set the starting node again.
	 */
William King's avatar
William King committed
818
819
820
	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
821
822
823
				      dns_fixedname_name(&dns_foundname),
				      &node, &chain, DNS_RBTFIND_EMPTYDATA,
				      NULL, NULL);
William King's avatar
William King committed
824

825
	if (dns_result != ISC_R_SUCCESS) {
826
827
		t_info("\tdns_rbt_findnode failed %s\n",
		       dns_result_totext(dns_result));
William King's avatar
William King committed
828
829
830
		return(result);
	}

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

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

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

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

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

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

880
881
882
	/*
	 * Check node ordering.
	 */
William King's avatar
William King committed
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
	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;

916
917
918
919
			/*
			 * Skip comment lines.
			 */
			if ((isspace((unsigned char)*p)) || (*p == '#'))
William King's avatar
William King committed
920
921
922
923
924
				continue;

			cnt = t_bustline(p, Tokens);
			if (cnt == 10) {
				result = t_dns_rbtnodechain_init(
925
926
927
928
929
930
931
932
933
934
						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
935
936
937
938
939
940
				if (result != T_PASS) {
					if (result == T_FAIL)
						++nfails;
					else
						++nprobs;
				}
941
			} else {
William King's avatar
William King committed
942
943
944
945
946
				t_info("bad format in %s at line %d\n",
						filename, line);
				++nprobs;
			}

947
			(void)free(p);
William King's avatar
William King committed
948
		}
949
950
		(void)fclose(fp);
	} else {
William King's avatar
William King committed
951
952
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
		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,
978
979
				char *expected_nextorigin)
{
William King's avatar
William King committed
980
981
982
983
984
985
	int			result;
	int			nfails;
	dns_rbt_t		*rbt;
	dns_rbtnodechain_t	chain;
	isc_mem_t		*mctx;
	isc_result_t		isc_result;
986
	isc_result_t		dns_result;
William King's avatar
William King committed
987
988
	dns_fixedname_t		dns_name;
	dns_fixedname_t		dns_origin;
989
	isc_result_t		expected_result;
William King's avatar
William King committed
990
991
992
993
994
995
996
997
998
999
1000

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

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

	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;

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

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

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

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

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

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

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

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

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

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

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

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

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

	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;

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

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

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

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

	int			result;
	int			len;
	int			nfails;
	dns_rbt_t		*rbt;
	dns_rbtnode_t		*node;
	dns_rbtnodechain_t	chain;
	isc_mem_t		*mctx;
	isc_result_t		isc_result;
1315
	isc_result_t		dns_result;
William King's avatar
William King committed
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
	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;
	mctx = NULL;
	isc_result = isc_mem_create(0, 0, &mctx);
	if (isc_result != ISC_R_SUCCESS) {
1328
1329
		t_info("isc_mem_create failed %s\n",
		       isc_result_totext(isc_result));
William King's avatar