No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tests / rbt / t_rbt.c
blobd7fd908aa888b78f581bed767ae07359d465640c
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: t_rbt.c,v 1.33 2009/09/01 00:22:25 jinmei Exp */
22 #include <config.h>
24 #include <ctype.h>
25 #include <stdlib.h>
27 #include <isc/entropy.h>
28 #include <isc/mem.h>
29 #include <isc/util.h>
30 #include <isc/hash.h>
31 #include <isc/string.h>
33 #include <dns/fixedname.h>
34 #include <dns/rbt.h>
35 #include <dns/result.h>
37 #include <tests/t_api.h>
39 #define BUFLEN 1024
40 #define DNSNAMELEN 255
42 char *progname;
43 char *Tokens[T_MAXTOKS];
45 static int
46 t_dns_rbtnodechain_init(char *dbfile, char *findname,
47 char *firstname, char *firstorigin,
48 char *nextname, char *nextorigin,
49 char *prevname, char *prevorigin,
50 char *lastname, char *lastorigin);
51 static char *
52 fixedname_totext(dns_fixedname_t *name);
54 static int
55 fixedname_cmp(dns_fixedname_t *dns_name, char *txtname);
57 static char *
58 dnsname_totext(dns_name_t *name);
60 static int
61 t_namechk(isc_result_t dns_result, dns_fixedname_t *dns_name, char *exp_name,
62 dns_fixedname_t *dns_origin, char *exp_origin,
63 isc_result_t exp_result);
66 * Parts adapted from the original rbt_test.c.
68 static int
69 fixedname_cmp(dns_fixedname_t *dns_name, char *txtname) {
70 char *name;
72 name = dnsname_totext(dns_fixedname_name(dns_name));
73 if (strcmp(txtname, "NULL") == 0) {
74 if ((name == NULL) || (*name == '\0'))
75 return(0);
76 return(1);
77 } else {
78 return(strcmp(name, txtname));
82 static char *
83 dnsname_totext(dns_name_t *name) {
84 static char buf[BUFLEN];
85 isc_buffer_t target;
87 isc_buffer_init(&target, buf, BUFLEN);
88 dns_name_totext(name, ISC_FALSE, &target);
89 *((char *)(target.base) + target.used) = '\0';
90 return(target.base);
93 static char *
94 fixedname_totext(dns_fixedname_t *name) {
95 static char buf[BUFLEN];
96 isc_buffer_t target;
98 memset(buf, 0, BUFLEN);
99 isc_buffer_init(&target, buf, BUFLEN);
100 dns_name_totext(dns_fixedname_name(name), ISC_FALSE, &target);
101 *((char *)(target.base) + target.used) = '\0';
102 return(target.base);
105 #ifdef NEED_PRINT_DATA
107 static isc_result_t
108 print_data(void *data) {
109 isc_result_t dns_result;
110 isc_buffer_t target;
111 char *buffer[DNSNAMELEN];
113 isc_buffer_init(&target, buffer, sizeof(buffer));
115 dns_result = dns_name_totext(data, ISC_FALSE, &target);
116 if (dns_result != ISC_R_SUCCESS) {
117 t_info("dns_name_totext failed %s\n",
118 dns_result_totext(dns_result));
120 return(dns_result);
123 #endif /* NEED_PRINT_DATA */
125 static int
126 create_name(char *s, isc_mem_t *mctx, dns_name_t **dns_name) {
127 int nfails;
128 int length;
129 isc_result_t result;
130 isc_buffer_t source;
131 isc_buffer_t target;
132 dns_name_t *name;
134 nfails = 0;
136 if (s && *s) {
138 length = strlen(s);
140 isc_buffer_init(&source, s, length);
141 isc_buffer_add(&source, length);
144 * The buffer for the actual name will immediately follow the
145 * name structure.
147 name = isc_mem_get(mctx, sizeof(*name) + DNSNAMELEN);
148 if (name == NULL) {
149 t_info("isc_mem_get failed\n");
150 ++nfails;
151 } else {
153 dns_name_init(name, NULL);
154 isc_buffer_init(&target, name + 1, DNSNAMELEN);
156 result = dns_name_fromtext(name, &source, dns_rootname,
157 0, &target);
159 if (result != ISC_R_SUCCESS) {
160 ++nfails;
161 t_info("dns_name_fromtext(%s) failed %s\n",
162 s, dns_result_totext(result));
163 isc_mem_put(mctx, name,
164 sizeof(*name) + DNSNAMELEN);
165 } else
166 *dns_name = name;
168 } else {
169 ++nfails;
170 t_info("create_name: empty name\n");
173 return(nfails);
176 static void
177 delete_name(void *data, void *arg) {
178 isc_mem_put((isc_mem_t *)arg, data, sizeof(dns_name_t) + DNSNAMELEN);
183 * Adapted from the original rbt_test.c.
185 static int
186 t1_add(char *name, dns_rbt_t *rbt, isc_mem_t *mctx, isc_result_t *dns_result) {
187 int nprobs;
188 dns_name_t *dns_name;
190 nprobs = 0;
191 if (name && dns_result) {
192 if (create_name(name, mctx, &dns_name) == 0) {
193 if (T_debug)
194 t_info("dns_rbt_addname succeeded\n");
195 *dns_result = dns_rbt_addname(rbt, dns_name, dns_name);
196 if (*dns_result != ISC_R_SUCCESS) {
197 delete_name(dns_name, mctx);
198 t_info("dns_rbt_addname failed %s\n",
199 dns_result_totext(*dns_result));
200 ++nprobs;
202 } else {
203 ++nprobs;
205 } else {
206 ++nprobs;
208 return(nprobs);
211 static int
212 t1_delete(char *name, dns_rbt_t *rbt, isc_mem_t *mctx,
213 isc_result_t *dns_result)
215 int nprobs;
216 dns_name_t *dns_name;
218 nprobs = 0;
219 if (name && dns_result) {
220 if (create_name(name, mctx, &dns_name) == 0) {
221 *dns_result = dns_rbt_deletename(rbt, dns_name,
222 ISC_FALSE);
223 delete_name(dns_name, mctx);
224 } else {
225 ++nprobs;
227 } else {
228 ++nprobs;
230 return(nprobs);
233 static int
234 t1_search(char *name, dns_rbt_t *rbt, isc_mem_t *mctx,
235 isc_result_t *dns_result)
237 int nprobs;
238 dns_name_t *dns_searchname;
239 dns_name_t *dns_foundname;
240 dns_fixedname_t dns_fixedname;
241 void *data;
243 nprobs = 0;
244 if (name && dns_result) {
245 if (create_name(name, mctx, &dns_searchname) == 0) {
246 dns_fixedname_init(&dns_fixedname);
247 dns_foundname = dns_fixedname_name(&dns_fixedname);
248 data = NULL;
249 *dns_result = dns_rbt_findname(rbt, dns_searchname, 0,
250 dns_foundname, &data);
251 delete_name(dns_searchname, mctx);
252 } else {
253 ++nprobs;
255 } else {
256 ++nprobs;
258 return(nprobs);
262 * Initialize a database from filename.
264 static int
265 rbt_init(char *filename, dns_rbt_t **rbt, isc_mem_t *mctx) {
266 int rval;
267 isc_result_t dns_result;
268 char *p;
269 FILE *fp;
271 fp = fopen(filename, "r");
272 if (fp == NULL) {
273 t_info("No such file %s\n", filename);
274 return(1);
277 dns_result = dns_rbt_create(mctx, delete_name, mctx, rbt);
278 if (dns_result != ISC_R_SUCCESS) {
279 t_info("dns_rbt_create failed %s\n",
280 dns_result_totext(dns_result));
281 fclose(fp);
282 return(1);
285 while ((p = t_fgetbs(fp)) != NULL) {
288 * Skip any comment lines.
290 if ((*p == '#') || (*p == '\0') || (*p == ' ')) {
291 (void)free(p);
292 continue;
295 if (T_debug)
296 t_info("adding name %s to the rbt\n", p);
298 rval = t1_add(p, *rbt, mctx, &dns_result);
299 if ((rval != 0) || (dns_result != ISC_R_SUCCESS)) {
300 t_info("add of %s failed\n", p);
301 dns_rbt_destroy(rbt);
302 fclose(fp);
303 return(1);
305 (void) free(p);
307 fclose(fp);
308 return(0);
311 static int
312 test_rbt_gen(char *filename, char *command, char *testname,
313 isc_result_t exp_result)
315 int rval;
316 int result;
317 dns_rbt_t *rbt;
318 isc_result_t isc_result;
319 isc_result_t dns_result;
320 isc_mem_t *mctx;
321 isc_entropy_t *ectx;
322 dns_name_t *dns_name;
324 result = T_UNRESOLVED;
326 if (strcmp(command, "create") != 0)
327 t_info("testing using name %s\n", testname);
329 mctx = NULL;
330 ectx = NULL;
332 isc_result = isc_mem_create(0, 0, &mctx);
333 if (isc_result != ISC_R_SUCCESS) {
334 t_info("isc_mem_create: %s: exiting\n",
335 dns_result_totext(isc_result));
336 return(T_UNRESOLVED);
339 isc_result = isc_entropy_create(mctx, &ectx);
340 if (isc_result != ISC_R_SUCCESS) {
341 t_info("isc_entropy_create: %s: exiting\n",
342 dns_result_totext(isc_result));
343 isc_mem_destroy(&mctx);
344 return(T_UNRESOLVED);
347 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
348 if (isc_result != ISC_R_SUCCESS) {
349 t_info("isc_hash_create: %s: exiting\n",
350 dns_result_totext(isc_result));
351 isc_entropy_detach(&ectx);
352 isc_mem_destroy(&mctx);
353 return(T_UNRESOLVED);
356 rbt = NULL;
357 if (rbt_init(filename, &rbt, mctx) != 0) {
358 if (strcmp(command, "create") == 0)
359 result = T_FAIL;
360 isc_hash_destroy();
361 isc_entropy_detach(&ectx);
362 isc_mem_destroy(&mctx);
363 return(result);
367 * Now try the database command.
369 if (strcmp(command, "create") == 0) {
370 result = T_PASS;
371 } else if (strcmp(command, "add") == 0) {
372 if (create_name(testname, mctx, &dns_name) == 0) {
373 dns_result = dns_rbt_addname(rbt, dns_name, dns_name);
375 if (dns_result != ISC_R_SUCCESS)
376 delete_name(dns_name, mctx);
378 if (dns_result == exp_result) {
379 if (dns_result == ISC_R_SUCCESS) {
380 rval = t1_search(testname, rbt, mctx,
381 &dns_result);
382 if (rval == 0) {
383 if (dns_result == ISC_R_SUCCESS) {
384 result = T_PASS;
385 } else {
386 result = T_FAIL;
388 } else {
389 t_info("t1_search failed\n");
390 result = T_UNRESOLVED;
392 } else {
393 result = T_PASS;
395 } else {
396 t_info("dns_rbt_addname returned %s, "
397 "expected %s\n",
398 dns_result_totext(dns_result),
399 dns_result_totext(exp_result));
400 result = T_FAIL;
402 } else {
403 t_info("create_name failed %s\n",
404 dns_result_totext(dns_result));
405 result = T_UNRESOLVED;
407 } else if ((strcmp(command, "delete") == 0) ||
408 (strcmp(command, "nuke") == 0)) {
409 rval = t1_delete(testname, rbt, mctx, &dns_result);
410 if (rval == 0) {
411 if (dns_result == exp_result) {
412 rval = t1_search(testname, rbt, mctx,
413 &dns_result);
414 if (rval == 0) {
415 if (dns_result == ISC_R_SUCCESS) {
416 t_info("dns_rbt_deletename "
417 "didn't delete "
418 "the name");
419 result = T_FAIL;
420 } else {
421 result = T_PASS;
424 } else {
425 t_info("delete returned %s, expected %s\n",
426 dns_result_totext(dns_result),
427 dns_result_totext(exp_result));
428 result = T_FAIL;
431 } else if (strcmp(command, "search") == 0) {
432 rval = t1_search(testname, rbt, mctx, &dns_result);
433 if (rval == 0) {
434 if (dns_result == exp_result) {
435 result = T_PASS;
436 } else {
437 t_info("find returned %s, expected %s\n",
438 dns_result_totext(dns_result),
439 dns_result_totext(exp_result));
440 result = T_FAIL;
445 dns_rbt_destroy(&rbt);
446 isc_hash_destroy();
447 isc_entropy_detach(&ectx);
448 isc_mem_destroy(&mctx);
449 return(result);
452 static int
453 test_dns_rbt_x(const char *filename) {
454 FILE *fp;
455 char *p;
456 int line;
457 int cnt;
458 int result;
459 int nfails;
460 int nprobs;
462 nfails = 0;
463 nprobs = 0;
465 fp = fopen(filename, "r");
466 if (fp != NULL) {
467 line = 0;
468 while ((p = t_fgetbs(fp)) != NULL) {
470 ++line;
473 * Skip comment lines.
475 if ((isspace((unsigned char)*p)) || (*p == '#')) {
476 (void)free(p);
477 continue;
481 * Name of db file, command, testname,
482 * expected result.
484 cnt = t_bustline(p, Tokens);
485 if (cnt == 4) {
486 result = test_rbt_gen(Tokens[0], Tokens[1],
487 Tokens[2],
488 t_dns_result_fromtext(Tokens[3]));
489 if (result != T_PASS)
490 ++nfails;
491 } else {
492 t_info("bad format in %s at line %d\n",
493 filename, line);
494 ++nprobs;
497 (void)free(p);
499 (void)fclose(fp);
500 } else {
501 t_info("Missing datafile %s\n", filename);
502 ++nprobs;
505 result = T_UNRESOLVED;
506 if ((nfails == 0) && (nprobs == 0))
507 result = T_PASS;
508 else if (nfails)
509 result = T_FAIL;
511 return(result);
515 static const char *a1 = "dns_rbt_create creates a rbt and returns "
516 "ISC_R_SUCCESS on success";
518 static void
519 t1() {
520 int result;
522 t_assert("dns_rbt_create", 1, T_REQUIRED, "%s", a1);
523 result = test_dns_rbt_x("dns_rbt_create_1_data");
524 t_result(result);
527 static const char *a2 = "dns_rbt_addname adds a name to a database and "
528 "returns ISC_R_SUCCESS on success";
530 static void
531 t2() {
532 int result;
534 t_assert("dns_rbt_addname", 2, T_REQUIRED, "%s", a2);
535 result = test_dns_rbt_x("dns_rbt_addname_1_data");
536 t_result(result);
539 static const char *a3 = "when name already exists, dns_rbt_addname() "
540 "returns ISC_R_EXISTS";
542 static void
543 t3() {
544 int result;
546 t_assert("dns_rbt_addname", 3, T_REQUIRED, "%s", a3);
547 result = test_dns_rbt_x("dns_rbt_addname_2_data");
548 t_result(result);
551 static const char *a4 = "when name exists, dns_rbt_deletename() returns "
552 "ISC_R_SUCCESS";
554 static void
555 t4() {
556 int result;
558 t_assert("dns_rbt_deletename", 4, T_REQUIRED, "%s", a4);
559 result = test_dns_rbt_x("dns_rbt_deletename_1_data");
560 t_result(result);
563 static const char *a5 = "when name does not exist, dns_rbt_deletename() "
564 "returns ISC_R_NOTFOUND";
565 static void
566 t5() {
567 int result;
569 t_assert("dns_rbt_deletename", 5, T_REQUIRED, "%s", a5);
570 result = test_dns_rbt_x("dns_rbt_deletename_2_data");
571 t_result(result);
574 static const char *a6 = "when name exists and exactly matches the "
575 "search name dns_rbt_findname() returns ISC_R_SUCCESS";
577 static void
578 t6() {
579 int result;
581 t_assert("dns_rbt_findname", 6, T_REQUIRED, "%s", a6);
582 result = test_dns_rbt_x("dns_rbt_findname_1_data");
583 t_result(result);
586 static const char *a7 = "when a name does not exist, "
587 "dns_rbt_findname returns ISC_R_NOTFOUND";
589 static void
590 t7() {
591 int result;
593 t_assert("dns_rbt_findname", 7, T_REQUIRED, "%s", a7);
594 result = test_dns_rbt_x("dns_rbt_findname_2_data");
595 t_result(result);
598 static const char *a8 = "when a superdomain is found with data matching name, "
599 "dns_rbt_findname returns DNS_R_PARTIALMATCH";
601 static void
602 t8() {
603 int result;
605 t_assert("dns_rbt_findname", 8, T_REQUIRED, "%s", a8);
606 result = test_dns_rbt_x("dns_rbt_findname_3_data");
607 t_result(result);
611 static const char *a9 = "a call to dns_rbtnodechain_init(chain, mctx) "
612 "initializes chain";
614 static int
615 t9_walkchain(dns_rbtnodechain_t *chain, dns_rbt_t *rbt) {
616 int cnt;
617 int order;
618 unsigned int nlabels;
619 int nprobs;
620 isc_result_t dns_result;
622 dns_fixedname_t name;
623 dns_fixedname_t origin;
624 dns_fixedname_t fullname1;
625 dns_fixedname_t fullname2;
627 cnt = 0;
628 nprobs = 0;
630 do {
632 if (cnt == 0) {
633 dns_fixedname_init(&name);
634 dns_fixedname_init(&origin);
635 dns_result = dns_rbtnodechain_first(chain, rbt,
636 dns_fixedname_name(&name),
637 dns_fixedname_name(&origin));
638 if (dns_result != DNS_R_NEWORIGIN) {
639 t_info("dns_rbtnodechain_first returned %s, "
640 "expecting DNS_R_NEWORIGIN\n",
641 dns_result_totext(dns_result));
642 ++nprobs;
643 break;
645 t_info("first name:\t<%s>\n", fixedname_totext(&name));
646 t_info("first origin:\t<%s>\n",
647 fixedname_totext(&origin));
648 } else {
649 dns_fixedname_init(&fullname1);
650 dns_result = dns_name_concatenate(
651 dns_fixedname_name(&name),
652 dns_name_isabsolute(dns_fixedname_name(&name)) ?
653 NULL : dns_fixedname_name(&origin),
654 dns_fixedname_name(&fullname1), NULL);
655 if (dns_result != ISC_R_SUCCESS) {
656 t_info("dns_name_concatenate failed %s\n",
657 dns_result_totext(dns_result));
658 ++nprobs;
659 break;
663 * Expecting origin not to get touched if next
664 * doesn't return NEWORIGIN.
666 dns_fixedname_init(&name);
667 dns_result = dns_rbtnodechain_next(chain,
668 dns_fixedname_name(&name),
669 dns_fixedname_name(&origin));
670 if ((dns_result != ISC_R_SUCCESS) &&
671 (dns_result != DNS_R_NEWORIGIN)) {
672 if (dns_result != ISC_R_NOMORE) {
673 t_info("dns_rbtnodechain_next "
674 "failed %s\n",
675 dns_result_totext(dns_result));
676 ++nprobs;
678 break;
681 t_info("next name:\t<%s>\n", fixedname_totext(&name));
682 t_info("next origin:\t<%s>\n",
683 fixedname_totext(&origin));
685 dns_fixedname_init(&fullname2);
686 dns_result = dns_name_concatenate(
687 dns_fixedname_name(&name),
688 dns_name_isabsolute(dns_fixedname_name(&name)) ?
689 NULL : dns_fixedname_name(&origin),
690 dns_fixedname_name(&fullname2), NULL);
691 if (dns_result != ISC_R_SUCCESS) {
692 t_info("dns_name_concatenate failed %s\n",
693 dns_result_totext(dns_result));
694 ++nprobs;
695 break;
698 t_info("comparing\t<%s>\n",
699 fixedname_totext(&fullname1));
700 t_info("\twith\t<%s>\n", fixedname_totext(&fullname2));
702 (void)dns_name_fullcompare(
703 dns_fixedname_name(&fullname1),
704 dns_fixedname_name(&fullname2),
705 &order, &nlabels);
707 if (order >= 0) {
708 t_info("unexpected order %s %s %s\n",
709 dnsname_totext(dns_fixedname_name(&fullname1)),
710 order == -1 ? "<" : (order == 0 ? "==" : ">"),
711 dnsname_totext(dns_fixedname_name(&fullname2)));
712 ++nprobs;
716 ++cnt;
717 } while (1);
719 return (nprobs);
723 * Test by exercising the first|last|next|prev funcs in useful ways.
726 static int
727 t_namechk(isc_result_t dns_result, dns_fixedname_t *dns_name, char *exp_name,
728 dns_fixedname_t *dns_origin, char *exp_origin,
729 isc_result_t exp_result)
731 int nfails;
733 nfails = 0;
735 if (fixedname_cmp(dns_name, exp_name)) {
736 t_info("\texpected name of %s, got %s\n",
737 exp_name, fixedname_totext(dns_name));
738 ++nfails;
740 if (exp_origin != NULL) {
741 t_info("checking for DNS_R_NEWORIGIN\n");
742 if (dns_result == exp_result) {
743 if (fixedname_cmp(dns_origin, exp_origin)) {
744 t_info("\torigin %s, expected %s\n",
745 fixedname_totext(dns_origin),
746 exp_origin);
747 ++nfails;
749 } else {
750 t_info("\tgot %s\n", dns_result_totext(dns_result));
751 ++nfails;
754 return(nfails);
757 static int
758 t_dns_rbtnodechain_init(char *dbfile, char *findname,
759 char *nextname, char *nextorigin,
760 char *prevname, char *prevorigin,
761 char *firstname, char *firstorigin,
762 char *lastname, char *lastorigin)
764 int result;
765 int len;
766 int nfails;
767 dns_rbt_t *rbt;
768 dns_rbtnode_t *node;
769 dns_rbtnodechain_t chain;
770 isc_mem_t *mctx;
771 isc_entropy_t *ectx;
772 isc_result_t isc_result;
773 isc_result_t dns_result;
774 dns_fixedname_t dns_findname;
775 dns_fixedname_t dns_foundname;
776 dns_fixedname_t dns_firstname;
777 dns_fixedname_t dns_lastname;
778 dns_fixedname_t dns_prevname;
779 dns_fixedname_t dns_nextname;
780 dns_fixedname_t dns_origin;
781 isc_buffer_t isc_buffer;
783 result = T_UNRESOLVED;
785 nfails = 0;
786 mctx = NULL;
787 ectx = NULL;
789 isc_result = isc_mem_create(0, 0, &mctx);
790 if (isc_result != ISC_R_SUCCESS) {
791 t_info("isc_mem_create failed %s\n",
792 isc_result_totext(isc_result));
793 return(result);
796 isc_result = isc_entropy_create(mctx, &ectx);
797 if (isc_result != ISC_R_SUCCESS) {
798 t_info("isc_entropy_create: %s: exiting\n",
799 dns_result_totext(isc_result));
800 isc_mem_destroy(&mctx);
801 return(T_UNRESOLVED);
804 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
805 if (isc_result != ISC_R_SUCCESS) {
806 t_info("isc_hash_create: %s: exiting\n",
807 dns_result_totext(isc_result));
808 isc_entropy_detach(&ectx);
809 isc_mem_destroy(&mctx);
810 return(T_UNRESOLVED);
813 dns_rbtnodechain_init(&chain, mctx);
815 rbt = NULL;
816 if (rbt_init(dbfile, &rbt, mctx)) {
817 t_info("rbt_init %s failed\n", dbfile);
818 isc_hash_destroy();
819 isc_entropy_detach(&ectx);
820 isc_mem_destroy(&mctx);
821 return(result);
824 len = strlen(findname);
825 isc_buffer_init(&isc_buffer, findname, len);
826 isc_buffer_add(&isc_buffer, len);
828 dns_fixedname_init(&dns_foundname);
829 dns_fixedname_init(&dns_findname);
830 dns_fixedname_init(&dns_firstname);
831 dns_fixedname_init(&dns_origin);
832 dns_fixedname_init(&dns_lastname);
833 dns_fixedname_init(&dns_prevname);
834 dns_fixedname_init(&dns_nextname);
836 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname),
837 &isc_buffer, NULL, 0, NULL);
839 if (dns_result != ISC_R_SUCCESS) {
840 t_info("dns_name_fromtext failed %s\n",
841 dns_result_totext(dns_result));
842 return(result);
846 * Set the starting node.
848 node = NULL;
849 dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
850 dns_fixedname_name(&dns_foundname),
851 &node, &chain, DNS_RBTFIND_EMPTYDATA,
852 NULL, NULL);
854 if (dns_result != ISC_R_SUCCESS) {
855 t_info("dns_rbt_findnode failed %s\n",
856 dns_result_totext(dns_result));
857 return(result);
861 * Check next.
863 t_info("checking for next name of %s and new origin of %s\n",
864 nextname, nextorigin);
865 dns_result = dns_rbtnodechain_next(&chain,
866 dns_fixedname_name(&dns_nextname),
867 dns_fixedname_name(&dns_origin));
869 if ((dns_result != ISC_R_SUCCESS) &&
870 (dns_result != DNS_R_NEWORIGIN)) {
871 t_info("dns_rbtnodechain_next unexpectedly returned %s\n",
872 dns_result_totext(dns_result));
875 nfails += t_namechk(dns_result, &dns_nextname, nextname, &dns_origin,
876 nextorigin, DNS_R_NEWORIGIN);
879 * Set the starting node again.
881 node = NULL;
882 dns_fixedname_init(&dns_foundname);
883 dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
884 dns_fixedname_name(&dns_foundname),
885 &node, &chain, DNS_RBTFIND_EMPTYDATA,
886 NULL, NULL);
888 if (dns_result != ISC_R_SUCCESS) {
889 t_info("\tdns_rbt_findnode failed %s\n",
890 dns_result_totext(dns_result));
891 return(result);
895 * Check previous.
897 t_info("checking for previous name of %s and new origin of %s\n",
898 prevname, prevorigin);
899 dns_fixedname_init(&dns_origin);
900 dns_result = dns_rbtnodechain_prev(&chain,
901 dns_fixedname_name(&dns_prevname),
902 dns_fixedname_name(&dns_origin));
904 if ((dns_result != ISC_R_SUCCESS) &&
905 (dns_result != DNS_R_NEWORIGIN)) {
906 t_info("dns_rbtnodechain_prev unexpectedly returned %s\n",
907 dns_result_totext(dns_result));
909 nfails += t_namechk(dns_result, &dns_prevname, prevname, &dns_origin,
910 prevorigin, DNS_R_NEWORIGIN);
913 * Check first.
915 t_info("checking for first name of %s and new origin of %s\n",
916 firstname, firstorigin);
917 dns_result = dns_rbtnodechain_first(&chain, rbt,
918 dns_fixedname_name(&dns_firstname),
919 dns_fixedname_name(&dns_origin));
921 if (dns_result != DNS_R_NEWORIGIN) {
922 t_info("dns_rbtnodechain_first unexpectedly returned %s\n",
923 dns_result_totext(dns_result));
925 nfails += t_namechk(dns_result, &dns_firstname, firstname,
926 &dns_origin, firstorigin, DNS_R_NEWORIGIN);
929 * Check last.
931 t_info("checking for last name of %s\n", lastname);
932 dns_result = dns_rbtnodechain_last(&chain, rbt,
933 dns_fixedname_name(&dns_lastname),
934 dns_fixedname_name(&dns_origin));
936 if (dns_result != DNS_R_NEWORIGIN) {
937 t_info("dns_rbtnodechain_last unexpectedly returned %s\n",
938 dns_result_totext(dns_result));
940 nfails += t_namechk(dns_result, &dns_lastname, lastname, &dns_origin,
941 lastorigin, DNS_R_NEWORIGIN);
944 * Check node ordering.
946 t_info("checking node ordering\n");
947 nfails += t9_walkchain(&chain, rbt);
949 if (nfails)
950 result = T_FAIL;
951 else
952 result = T_PASS;
954 dns_rbtnodechain_invalidate(&chain);
955 dns_rbt_destroy(&rbt);
957 isc_hash_destroy();
958 isc_entropy_detach(&ectx);
959 isc_mem_destroy(&mctx);
961 return(result);
964 static int
965 test_dns_rbtnodechain_init(const char *filename) {
966 FILE *fp;
967 char *p;
968 int line;
969 int cnt;
970 int result;
971 int nfails;
972 int nprobs;
974 nfails = 0;
975 nprobs = 0;
977 fp = fopen(filename, "r");
978 if (fp != NULL) {
979 line = 0;
980 while ((p = t_fgetbs(fp)) != NULL) {
982 ++line;
985 * Skip comment lines.
987 if ((isspace((unsigned char)*p)) || (*p == '#')) {
988 (void)free(p);
989 continue;
992 cnt = t_bustline(p, Tokens);
993 if (cnt == 10) {
994 result = t_dns_rbtnodechain_init(
995 Tokens[0], /* dbfile */
996 Tokens[1], /* startname */
997 Tokens[2], /* nextname */
998 Tokens[3], /* nextorigin */
999 Tokens[4], /* prevname */
1000 Tokens[5], /* prevorigin */
1001 Tokens[6], /* firstname */
1002 Tokens[7], /* firstorigin */
1003 Tokens[8], /* lastname */
1004 Tokens[9]); /* lastorigin */
1005 if (result != T_PASS) {
1006 if (result == T_FAIL)
1007 ++nfails;
1008 else
1009 ++nprobs;
1011 } else {
1012 t_info("bad format in %s at line %d\n",
1013 filename, line);
1014 ++nprobs;
1017 (void)free(p);
1019 (void)fclose(fp);
1020 } else {
1021 t_info("Missing datafile %s\n", filename);
1022 ++nprobs;
1025 result = T_UNRESOLVED;
1027 if ((nfails == 0) && (nprobs == 0))
1028 result = T_PASS;
1029 else if (nfails)
1030 result = T_FAIL;
1032 return(result);
1035 static void
1036 t9() {
1037 int result;
1039 t_assert("dns_rbtnodechain_init", 9, T_REQUIRED, "%s", a9);
1040 result = test_dns_rbtnodechain_init("dns_rbtnodechain_init_data");
1041 t_result(result);
1044 static int
1045 t_dns_rbtnodechain_first(char *dbfile, char *expected_firstname,
1046 char *expected_firstorigin,
1047 char *expected_nextname,
1048 char *expected_nextorigin)
1050 int result;
1051 int nfails;
1052 dns_rbt_t *rbt;
1053 dns_rbtnodechain_t chain;
1054 isc_mem_t *mctx;
1055 isc_entropy_t *ectx;
1056 isc_result_t isc_result;
1057 isc_result_t dns_result;
1058 dns_fixedname_t dns_name;
1059 dns_fixedname_t dns_origin;
1060 isc_result_t expected_result;
1062 result = T_UNRESOLVED;
1064 nfails = 0;
1065 mctx = NULL;
1066 ectx = NULL;
1068 dns_fixedname_init(&dns_name);
1069 dns_fixedname_init(&dns_origin);
1071 isc_result = isc_mem_create(0, 0, &mctx);
1072 if (isc_result != ISC_R_SUCCESS) {
1073 t_info("isc_mem_create failed %s\n",
1074 isc_result_totext(isc_result));
1075 return(result);
1078 isc_result = isc_entropy_create(mctx, &ectx);
1079 if (isc_result != ISC_R_SUCCESS) {
1080 t_info("isc_entropy_create: %s: exiting\n",
1081 dns_result_totext(isc_result));
1082 isc_mem_destroy(&mctx);
1083 return(T_UNRESOLVED);
1086 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
1087 if (isc_result != ISC_R_SUCCESS) {
1088 t_info("isc_hash_create: %s: exiting\n",
1089 dns_result_totext(isc_result));
1090 isc_entropy_detach(&ectx);
1091 isc_mem_destroy(&mctx);
1092 return(T_UNRESOLVED);
1095 dns_rbtnodechain_init(&chain, mctx);
1097 rbt = NULL;
1098 if (rbt_init(dbfile, &rbt, mctx)) {
1099 t_info("rbt_init %s failed\n", dbfile);
1100 isc_hash_destroy();
1101 isc_entropy_detach(&ectx);
1102 isc_mem_destroy(&mctx);
1103 return(result);
1106 t_info("testing for first name of %s, origin of %s\n",
1107 expected_firstname, expected_firstorigin);
1109 dns_result = dns_rbtnodechain_first(&chain, rbt,
1110 dns_fixedname_name(&dns_name),
1111 dns_fixedname_name(&dns_origin));
1113 if (dns_result != DNS_R_NEWORIGIN)
1114 t_info("dns_rbtnodechain_first unexpectedly returned %s\n",
1115 dns_result_totext(dns_result));
1117 nfails = t_namechk(dns_result, &dns_name, expected_firstname,
1118 &dns_origin, expected_firstorigin, DNS_R_NEWORIGIN);
1120 dns_fixedname_init(&dns_name);
1121 dns_result = dns_rbtnodechain_next(&chain,
1122 dns_fixedname_name(&dns_name),
1123 dns_fixedname_name(&dns_origin));
1125 t_info("testing for next name of %s, origin of %s\n",
1126 expected_nextname, expected_nextorigin);
1128 if ((dns_result != ISC_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN))
1129 t_info("dns_rbtnodechain_next unexpectedly returned %s\n",
1130 dns_result_totext(dns_result));
1132 if (strcasecmp(expected_firstorigin, expected_nextorigin) == 0)
1133 expected_result = ISC_R_SUCCESS;
1134 else
1135 expected_result = DNS_R_NEWORIGIN;
1136 nfails += t_namechk(dns_result, &dns_name, expected_nextname,
1137 &dns_origin, expected_nextorigin, expected_result);
1139 if (nfails)
1140 result = T_FAIL;
1141 else
1142 result = T_PASS;
1144 dns_rbtnodechain_invalidate(&chain);
1146 dns_rbt_destroy(&rbt);
1147 isc_hash_destroy();
1148 isc_entropy_detach(&ectx);
1149 isc_mem_destroy(&mctx);
1150 return(result);
1153 static int
1154 test_dns_rbtnodechain_first(const char *filename) {
1155 FILE *fp;
1156 char *p;
1157 int line;
1158 int cnt;
1159 int result;
1160 int nfails;
1161 int nprobs;
1163 nfails = 0;
1164 nprobs = 0;
1166 fp = fopen(filename, "r");
1167 if (fp != NULL) {
1168 line = 0;
1169 while ((p = t_fgetbs(fp)) != NULL) {
1171 ++line;
1174 * Skip comment lines.
1176 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1177 (void)free(p);
1178 continue;
1181 cnt = t_bustline(p, Tokens);
1182 if (cnt == 5) {
1183 result = t_dns_rbtnodechain_first(
1184 Tokens[0], /* dbfile */
1185 Tokens[1], /* firstname */
1186 Tokens[2], /* firstorigin */
1187 Tokens[3], /* nextname */
1188 Tokens[4]); /* nextorigin */
1189 if (result != T_PASS) {
1190 if (result == T_FAIL)
1191 ++nfails;
1192 else
1193 ++nprobs;
1195 } else {
1196 t_info("bad format in %s at line %d\n",
1197 filename, line);
1198 ++nprobs;
1201 (void)free(p);
1203 (void)fclose(fp);
1204 } else {
1205 t_info("Missing datafile %s\n", filename);
1206 ++nprobs;
1209 result = T_UNRESOLVED;
1211 if ((nfails == 0) && (nprobs == 0))
1212 result = T_PASS;
1213 else if (nfails)
1214 result = T_FAIL;
1216 return(result);
1219 static const char *a10 = "a call to "
1220 "dns_rbtnodechain_first(chain, rbt, name, origin) "
1221 "sets name to point to the root of the tree, "
1222 "origin to point to the origin, "
1223 "and returns DNS_R_NEWORIGIN";
1225 static void
1226 t10() {
1227 int result;
1229 t_assert("dns_rbtnodechain_first", 10, T_REQUIRED, "%s", a10);
1230 result = test_dns_rbtnodechain_first("dns_rbtnodechain_first_data");
1231 t_result(result);
1234 static int
1235 t_dns_rbtnodechain_last(char *dbfile, char *expected_lastname,
1236 char *expected_lastorigin,
1237 char *expected_prevname,
1238 char *expected_prevorigin)
1241 int result;
1242 int nfails;
1243 dns_rbt_t *rbt;
1244 dns_rbtnodechain_t chain;
1245 isc_mem_t *mctx;
1246 isc_entropy_t *ectx;
1247 isc_result_t isc_result;
1248 isc_result_t dns_result;
1249 dns_fixedname_t dns_name;
1250 dns_fixedname_t dns_origin;
1251 isc_result_t expected_result;
1253 result = T_UNRESOLVED;
1255 nfails = 0;
1256 mctx = NULL;
1257 ectx = NULL;
1259 dns_fixedname_init(&dns_name);
1260 dns_fixedname_init(&dns_origin);
1262 isc_result = isc_mem_create(0, 0, &mctx);
1263 if (isc_result != ISC_R_SUCCESS) {
1264 t_info("isc_mem_create failed %s\n",
1265 isc_result_totext(isc_result));
1266 return(result);
1269 isc_result = isc_entropy_create(mctx, &ectx);
1270 if (isc_result != ISC_R_SUCCESS) {
1271 t_info("isc_entropy_create: %s: exiting\n",
1272 dns_result_totext(isc_result));
1273 isc_mem_destroy(&mctx);
1274 return(T_UNRESOLVED);
1277 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
1278 if (isc_result != ISC_R_SUCCESS) {
1279 t_info("isc_hash_create: %s: exiting\n",
1280 dns_result_totext(isc_result));
1281 isc_entropy_detach(&ectx);
1282 isc_mem_destroy(&mctx);
1283 return(T_UNRESOLVED);
1286 dns_rbtnodechain_init(&chain, mctx);
1288 rbt = NULL;
1289 if (rbt_init(dbfile, &rbt, mctx)) {
1290 t_info("rbt_init %s failed\n", dbfile);
1291 isc_hash_destroy();
1292 isc_entropy_detach(&ectx);
1293 isc_mem_destroy(&mctx);
1294 return(result);
1297 t_info("testing for last name of %s, origin of %s\n",
1298 expected_lastname, expected_lastorigin);
1300 dns_result = dns_rbtnodechain_last(&chain, rbt,
1301 dns_fixedname_name(&dns_name),
1302 dns_fixedname_name(&dns_origin));
1304 if (dns_result != DNS_R_NEWORIGIN) {
1305 t_info("dns_rbtnodechain_last unexpectedly returned %s\n",
1306 dns_result_totext(dns_result));
1308 nfails = t_namechk(dns_result, &dns_name, expected_lastname,
1309 &dns_origin, expected_lastorigin, DNS_R_NEWORIGIN);
1311 t_info("testing for previous name of %s, origin of %s\n",
1312 expected_prevname, expected_prevorigin);
1314 dns_fixedname_init(&dns_name);
1315 dns_result = dns_rbtnodechain_prev(&chain,
1316 dns_fixedname_name(&dns_name),
1317 dns_fixedname_name(&dns_origin));
1319 if ((dns_result != ISC_R_SUCCESS) &&
1320 (dns_result != DNS_R_NEWORIGIN)) {
1321 t_info("dns_rbtnodechain_prev unexpectedly returned %s\n",
1322 dns_result_totext(dns_result));
1324 if (strcasecmp(expected_lastorigin, expected_prevorigin) == 0)
1325 expected_result = ISC_R_SUCCESS;
1326 else
1327 expected_result = DNS_R_NEWORIGIN;
1328 nfails += t_namechk(dns_result, &dns_name, expected_prevname,
1329 &dns_origin, expected_prevorigin, expected_result);
1331 if (nfails)
1332 result = T_FAIL;
1333 else
1334 result = T_PASS;
1336 dns_rbtnodechain_invalidate(&chain);
1337 dns_rbt_destroy(&rbt);
1339 isc_hash_destroy();
1340 isc_entropy_detach(&ectx);
1341 isc_mem_destroy(&mctx);
1343 return(result);
1346 static int
1347 test_dns_rbtnodechain_last(const char *filename) {
1348 FILE *fp;
1349 char *p;
1350 int line;
1351 int cnt;
1352 int result;
1353 int nfails;
1354 int nprobs;
1356 nfails = 0;
1357 nprobs = 0;
1359 fp = fopen(filename, "r");
1360 if (fp != NULL) {
1361 line = 0;
1362 while ((p = t_fgetbs(fp)) != NULL) {
1364 ++line;
1367 * Skip comment lines.
1369 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1370 (void)free(p);
1371 continue;
1374 cnt = t_bustline(p, Tokens);
1375 if (cnt == 5) {
1376 result = t_dns_rbtnodechain_last(
1377 Tokens[0], /* dbfile */
1378 Tokens[1], /* lastname */
1379 Tokens[2], /* lastorigin */
1380 Tokens[3], /* prevname */
1381 Tokens[4]); /* prevorigin */
1382 if (result != T_PASS) {
1383 if (result == T_FAIL)
1384 ++nfails;
1385 else
1386 ++nprobs;
1388 } else {
1389 t_info("bad format in %s at line %d\n",
1390 filename, line);
1391 ++nprobs;
1394 (void)free(p);
1396 (void)fclose(fp);
1397 } else {
1398 t_info("Missing datafile %s\n", filename);
1399 ++nprobs;
1402 result = T_UNRESOLVED;
1404 if ((nfails == 0) && (nprobs == 0))
1405 result = T_PASS;
1406 else if (nfails)
1407 result = T_FAIL;
1409 return(result);
1412 static const char *a11 = "a call to "
1413 "dns_rbtnodechain_last(chain, rbt, name, origin) "
1414 "sets name to point to the last node of the megatree, "
1415 "origin to the name of the level above it, "
1416 "and returns DNS_R_NEWORIGIN";
1418 static void
1419 t11() {
1420 int result;
1422 t_assert("dns_rbtnodechain_last", 11, T_REQUIRED, "%s", a11);
1423 result = test_dns_rbtnodechain_last("dns_rbtnodechain_last_data");
1424 t_result(result);
1427 static int
1428 t_dns_rbtnodechain_next(char *dbfile, char *findname,
1429 char *nextname, char *nextorigin)
1432 int result;
1433 int len;
1434 int nfails;
1435 dns_rbt_t *rbt;
1436 dns_rbtnode_t *node;
1437 dns_rbtnodechain_t chain;
1438 isc_mem_t *mctx;
1439 isc_entropy_t *ectx;
1440 isc_result_t isc_result;
1441 isc_result_t dns_result;
1442 dns_fixedname_t dns_findname;
1443 dns_fixedname_t dns_foundname;
1444 dns_fixedname_t dns_nextname;
1445 dns_fixedname_t dns_origin;
1446 isc_buffer_t isc_buffer;
1448 result = T_UNRESOLVED;
1450 nfails = 0;
1451 mctx = NULL;
1452 ectx = NULL;
1454 isc_result = isc_mem_create(0, 0, &mctx);
1455 if (isc_result != ISC_R_SUCCESS) {
1456 t_info("isc_mem_create failed %s\n",
1457 isc_result_totext(isc_result));
1458 return(result);
1461 isc_result = isc_entropy_create(mctx, &ectx);
1462 if (isc_result != ISC_R_SUCCESS) {
1463 t_info("isc_entropy_create: %s: exiting\n",
1464 dns_result_totext(isc_result));
1465 isc_mem_destroy(&mctx);
1466 return(T_UNRESOLVED);
1469 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
1470 if (isc_result != ISC_R_SUCCESS) {
1471 t_info("isc_hash_create: %s: exiting\n",
1472 dns_result_totext(isc_result));
1473 isc_entropy_detach(&ectx);
1474 isc_mem_destroy(&mctx);
1475 return(T_UNRESOLVED);
1478 dns_rbtnodechain_init(&chain, mctx);
1480 rbt = NULL;
1481 if (rbt_init(dbfile, &rbt, mctx)) {
1482 t_info("rbt_init %s failed\n", dbfile);
1483 isc_hash_destroy();
1484 isc_entropy_detach(&ectx);
1485 isc_mem_destroy(&mctx);
1486 return(result);
1489 len = strlen(findname);
1490 isc_buffer_init(&isc_buffer, findname, len);
1491 isc_buffer_add(&isc_buffer, len);
1493 dns_fixedname_init(&dns_foundname);
1494 dns_fixedname_init(&dns_findname);
1495 dns_fixedname_init(&dns_nextname);
1496 dns_fixedname_init(&dns_origin);
1498 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname),
1499 &isc_buffer, NULL, 0, NULL);
1501 if (dns_result != ISC_R_SUCCESS) {
1502 t_info("dns_name_fromtext failed %s\n",
1503 dns_result_totext(dns_result));
1504 return(result);
1508 * Set the starting node.
1510 node = NULL;
1511 dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
1512 dns_fixedname_name(&dns_foundname),
1513 &node, &chain, DNS_RBTFIND_EMPTYDATA,
1514 NULL, NULL);
1516 if (dns_result != ISC_R_SUCCESS) {
1517 t_info("dns_rbt_findnode failed %s\n",
1518 dns_result_totext(dns_result));
1519 return(result);
1523 * Check next.
1525 t_info("checking for next name of %s and new origin of %s\n",
1526 nextname, nextorigin);
1527 dns_result = dns_rbtnodechain_next(&chain,
1528 dns_fixedname_name(&dns_nextname),
1529 dns_fixedname_name(&dns_origin));
1531 if ((dns_result != ISC_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
1532 t_info("dns_rbtnodechain_next unexpectedly returned %s\n",
1533 dns_result_totext(dns_result));
1536 nfails += t_namechk(dns_result, &dns_nextname, nextname, &dns_origin,
1537 nextorigin, DNS_R_NEWORIGIN);
1539 if (nfails)
1540 result = T_FAIL;
1541 else
1542 result = T_PASS;
1544 dns_rbtnodechain_invalidate(&chain);
1545 dns_rbt_destroy(&rbt);
1547 isc_hash_destroy();
1548 isc_entropy_detach(&ectx);
1549 isc_mem_destroy(&mctx);
1551 return(result);
1554 static int
1555 test_dns_rbtnodechain_next(const char *filename) {
1556 FILE *fp;
1557 char *p;
1558 int line;
1559 int cnt;
1560 int result;
1561 int nfails;
1562 int nprobs;
1564 nfails = 0;
1565 nprobs = 0;
1567 fp = fopen(filename, "r");
1568 if (fp != NULL) {
1569 line = 0;
1570 while ((p = t_fgetbs(fp)) != NULL) {
1572 ++line;
1575 * Skip comment lines.
1577 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1578 (void)free(p);
1579 continue;
1582 cnt = t_bustline(p, Tokens);
1583 if (cnt == 4) {
1584 result = t_dns_rbtnodechain_next(
1585 Tokens[0], /* dbfile */
1586 Tokens[1], /* findname */
1587 Tokens[2], /* nextname */
1588 Tokens[3]); /* nextorigin */
1589 if (result != T_PASS) {
1590 if (result == T_FAIL)
1591 ++nfails;
1592 else
1593 ++nprobs;
1595 } else {
1596 t_info("bad format in %s at line %d\n",
1597 filename, line);
1598 ++nprobs;
1601 (void)free(p);
1603 (void)fclose(fp);
1604 } else {
1605 t_info("Missing datafile %s\n", filename);
1606 ++nprobs;
1609 result = T_UNRESOLVED;
1611 if ((nfails == 0) && (nprobs == 0))
1612 result = T_PASS;
1613 else if (nfails)
1614 result = T_FAIL;
1616 return(result);
1619 static const char *a12 = "a call to "
1620 "dns_rbtnodechain_next(chain, name, origin) "
1621 "sets name to point to the next node of the tree "
1622 "and returns ISC_R_SUCCESS or "
1623 "DNS_R_NEWORIGIN on success";
1626 static void
1627 t12() {
1628 int result;
1630 t_assert("dns_rbtnodechain_next", 12, T_REQUIRED, "%s", a12);
1631 result = test_dns_rbtnodechain_next("dns_rbtnodechain_next_data");
1632 t_result(result);
1635 static int
1636 t_dns_rbtnodechain_prev(char *dbfile, char *findname, char *prevname,
1637 char *prevorigin)
1639 int result;
1640 int len;
1641 int nfails;
1642 dns_rbt_t *rbt;
1643 dns_rbtnode_t *node;
1644 dns_rbtnodechain_t chain;
1645 isc_mem_t *mctx;
1646 isc_entropy_t *ectx = NULL;
1647 isc_result_t isc_result;
1648 isc_result_t dns_result;
1649 dns_fixedname_t dns_findname;
1650 dns_fixedname_t dns_foundname;
1651 dns_fixedname_t dns_prevname;
1652 dns_fixedname_t dns_origin;
1653 isc_buffer_t isc_buffer;
1655 result = T_UNRESOLVED;
1657 nfails = 0;
1658 mctx = NULL;
1659 ectx = NULL;
1661 isc_result = isc_mem_create(0, 0, &mctx);
1662 if (isc_result != ISC_R_SUCCESS) {
1663 t_info("isc_mem_create failed %s\n",
1664 isc_result_totext(isc_result));
1665 return(result);
1668 isc_result = isc_entropy_create(mctx, &ectx);
1669 if (isc_result != ISC_R_SUCCESS) {
1670 t_info("isc_entropy_create: %s: exiting\n",
1671 dns_result_totext(isc_result));
1672 isc_mem_destroy(&mctx);
1673 return(T_UNRESOLVED);
1676 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
1677 if (isc_result != ISC_R_SUCCESS) {
1678 t_info("isc_hash_create: %s: exiting\n",
1679 dns_result_totext(isc_result));
1680 isc_entropy_detach(&ectx);
1681 isc_mem_destroy(&mctx);
1682 return(T_UNRESOLVED);
1685 dns_rbtnodechain_init(&chain, mctx);
1687 rbt = NULL;
1688 if (rbt_init(dbfile, &rbt, mctx)) {
1689 t_info("rbt_init %s failed\n", dbfile);
1690 isc_hash_destroy();
1691 isc_entropy_detach(&ectx);
1692 isc_mem_destroy(&mctx);
1693 return(result);
1696 len = strlen(findname);
1697 isc_buffer_init(&isc_buffer, findname, len);
1698 isc_buffer_add(&isc_buffer, len);
1700 dns_fixedname_init(&dns_foundname);
1701 dns_fixedname_init(&dns_findname);
1702 dns_fixedname_init(&dns_prevname);
1703 dns_fixedname_init(&dns_origin);
1705 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname),
1706 &isc_buffer, NULL, 0, NULL);
1708 if (dns_result != ISC_R_SUCCESS) {
1709 t_info("dns_name_fromtext failed %s\n",
1710 dns_result_totext(dns_result));
1711 return(result);
1715 * Set the starting node.
1717 node = NULL;
1718 dns_result = dns_rbt_findnode(rbt, dns_fixedname_name(&dns_findname),
1719 dns_fixedname_name(&dns_foundname),
1720 &node, &chain, DNS_RBTFIND_EMPTYDATA,
1721 NULL, NULL);
1723 if (dns_result != ISC_R_SUCCESS) {
1724 t_info("dns_rbt_findnode failed %s\n",
1725 dns_result_totext(dns_result));
1726 return(result);
1730 * Check next.
1732 t_info("checking for next name of %s and new origin of %s\n",
1733 prevname, prevorigin);
1734 dns_result = dns_rbtnodechain_prev(&chain,
1735 dns_fixedname_name(&dns_prevname),
1736 dns_fixedname_name(&dns_origin));
1738 if ((dns_result != ISC_R_SUCCESS) && (dns_result != DNS_R_NEWORIGIN)) {
1739 t_info("dns_rbtnodechain_prev unexpectedly returned %s\n",
1740 dns_result_totext(dns_result));
1743 nfails += t_namechk(dns_result, &dns_prevname, prevname, &dns_origin,
1744 prevorigin, DNS_R_NEWORIGIN);
1746 if (nfails)
1747 result = T_FAIL;
1748 else
1749 result = T_PASS;
1751 dns_rbtnodechain_invalidate(&chain);
1752 dns_rbt_destroy(&rbt);
1754 isc_hash_destroy();
1755 isc_entropy_detach(&ectx);
1756 isc_mem_destroy(&mctx);
1758 return(result);
1761 static int
1762 test_dns_rbtnodechain_prev(const char *filename) {
1763 FILE *fp;
1764 char *p;
1765 int line;
1766 int cnt;
1767 int result;
1768 int nfails;
1769 int nprobs;
1771 nfails = 0;
1772 nprobs = 0;
1774 fp = fopen(filename, "r");
1775 if (fp != NULL) {
1776 line = 0;
1777 while ((p = t_fgetbs(fp)) != NULL) {
1779 ++line;
1782 * Skip comment lines.
1784 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1785 (void)free(p);
1786 continue;
1789 cnt = t_bustline(p, Tokens);
1790 if (cnt == 4) {
1791 result = t_dns_rbtnodechain_prev(
1792 Tokens[0], /* dbfile */
1793 Tokens[1], /* findname */
1794 Tokens[2], /* prevname */
1795 Tokens[3]); /* prevorigin */
1796 if (result != T_PASS) {
1797 if (result == T_FAIL)
1798 ++nfails;
1799 else
1800 ++nprobs;
1802 } else {
1803 t_info("bad format in %s at line %d\n",
1804 filename, line);
1805 ++nprobs;
1808 (void)free(p);
1810 (void)fclose(fp);
1811 } else {
1812 t_info("Missing datafile %s\n", filename);
1813 ++nprobs;
1816 result = T_UNRESOLVED;
1818 if ((nfails == 0) && (nprobs == 0))
1819 result = T_PASS;
1820 else if (nfails)
1821 result = T_FAIL;
1823 return(result);
1826 static const char *a13 = "a call to "
1827 "dns_rbtnodechain_prev(chain, name, origin) "
1828 "sets name to point to the previous node of the tree "
1829 "and returns ISC_R_SUCCESS or "
1830 "DNS_R_NEWORIGIN on success";
1832 static void
1833 t13() {
1834 int result;
1836 t_assert("dns_rbtnodechain_prev", 13, T_REQUIRED, "%s", a13);
1837 result = test_dns_rbtnodechain_prev("dns_rbtnodechain_prev_data");
1838 t_result(result);
1843 testspec_t T_testlist[] = {
1844 { t1, "dns_rbt_create" },
1845 { t2, "dns_rbt_addname 1" },
1846 { t3, "dns_rbt_addname 2" },
1847 { t4, "dns_rbt_deletename 1" },
1848 { t5, "dns_rbt_deletename 2" },
1849 { t6, "dns_rbt_findname 1" },
1850 { t7, "dns_rbt_findname 2" },
1851 { t8, "dns_rbt_findname 3" },
1852 { t9, "dns_rbtnodechain_init" },
1853 { t10, "dns_rbtnodechain_first" },
1854 { t11, "dns_rbtnodechain_last" },
1855 { t12, "dns_rbtnodechain_next" },
1856 { t13, "dns_rbtnodechain_prev" },
1857 { NULL, NULL }