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 */
27 #include <isc/entropy.h>
31 #include <isc/string.h>
33 #include <dns/fixedname.h>
35 #include <dns/result.h>
37 #include <tests/t_api.h>
40 #define DNSNAMELEN 255
43 char *Tokens
[T_MAXTOKS
];
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
);
52 fixedname_totext(dns_fixedname_t
*name
);
55 fixedname_cmp(dns_fixedname_t
*dns_name
, char *txtname
);
58 dnsname_totext(dns_name_t
*name
);
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.
69 fixedname_cmp(dns_fixedname_t
*dns_name
, char *txtname
) {
72 name
= dnsname_totext(dns_fixedname_name(dns_name
));
73 if (strcmp(txtname
, "NULL") == 0) {
74 if ((name
== NULL
) || (*name
== '\0'))
78 return(strcmp(name
, txtname
));
83 dnsname_totext(dns_name_t
*name
) {
84 static char buf
[BUFLEN
];
87 isc_buffer_init(&target
, buf
, BUFLEN
);
88 dns_name_totext(name
, ISC_FALSE
, &target
);
89 *((char *)(target
.base
) + target
.used
) = '\0';
94 fixedname_totext(dns_fixedname_t
*name
) {
95 static char buf
[BUFLEN
];
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';
105 #ifdef NEED_PRINT_DATA
108 print_data(void *data
) {
109 isc_result_t dns_result
;
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
));
123 #endif /* NEED_PRINT_DATA */
126 create_name(char *s
, isc_mem_t
*mctx
, dns_name_t
**dns_name
) {
140 isc_buffer_init(&source
, s
, length
);
141 isc_buffer_add(&source
, length
);
144 * The buffer for the actual name will immediately follow the
147 name
= isc_mem_get(mctx
, sizeof(*name
) + DNSNAMELEN
);
149 t_info("isc_mem_get failed\n");
153 dns_name_init(name
, NULL
);
154 isc_buffer_init(&target
, name
+ 1, DNSNAMELEN
);
156 result
= dns_name_fromtext(name
, &source
, dns_rootname
,
159 if (result
!= ISC_R_SUCCESS
) {
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
);
170 t_info("create_name: empty name\n");
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.
186 t1_add(char *name
, dns_rbt_t
*rbt
, isc_mem_t
*mctx
, isc_result_t
*dns_result
) {
188 dns_name_t
*dns_name
;
191 if (name
&& dns_result
) {
192 if (create_name(name
, mctx
, &dns_name
) == 0) {
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
));
212 t1_delete(char *name
, dns_rbt_t
*rbt
, isc_mem_t
*mctx
,
213 isc_result_t
*dns_result
)
216 dns_name_t
*dns_name
;
219 if (name
&& dns_result
) {
220 if (create_name(name
, mctx
, &dns_name
) == 0) {
221 *dns_result
= dns_rbt_deletename(rbt
, dns_name
,
223 delete_name(dns_name
, mctx
);
234 t1_search(char *name
, dns_rbt_t
*rbt
, isc_mem_t
*mctx
,
235 isc_result_t
*dns_result
)
238 dns_name_t
*dns_searchname
;
239 dns_name_t
*dns_foundname
;
240 dns_fixedname_t dns_fixedname
;
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
);
249 *dns_result
= dns_rbt_findname(rbt
, dns_searchname
, 0,
250 dns_foundname
, &data
);
251 delete_name(dns_searchname
, mctx
);
262 * Initialize a database from filename.
265 rbt_init(char *filename
, dns_rbt_t
**rbt
, isc_mem_t
*mctx
) {
267 isc_result_t dns_result
;
271 fp
= fopen(filename
, "r");
273 t_info("No such file %s\n", filename
);
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
));
285 while ((p
= t_fgetbs(fp
)) != NULL
) {
288 * Skip any comment lines.
290 if ((*p
== '#') || (*p
== '\0') || (*p
== ' ')) {
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
);
312 test_rbt_gen(char *filename
, char *command
, char *testname
,
313 isc_result_t exp_result
)
318 isc_result_t isc_result
;
319 isc_result_t dns_result
;
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
);
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
);
357 if (rbt_init(filename
, &rbt
, mctx
) != 0) {
358 if (strcmp(command
, "create") == 0)
361 isc_entropy_detach(&ectx
);
362 isc_mem_destroy(&mctx
);
367 * Now try the database command.
369 if (strcmp(command
, "create") == 0) {
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
,
383 if (dns_result
== ISC_R_SUCCESS
) {
389 t_info("t1_search failed\n");
390 result
= T_UNRESOLVED
;
396 t_info("dns_rbt_addname returned %s, "
398 dns_result_totext(dns_result
),
399 dns_result_totext(exp_result
));
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
);
411 if (dns_result
== exp_result
) {
412 rval
= t1_search(testname
, rbt
, mctx
,
415 if (dns_result
== ISC_R_SUCCESS
) {
416 t_info("dns_rbt_deletename "
425 t_info("delete returned %s, expected %s\n",
426 dns_result_totext(dns_result
),
427 dns_result_totext(exp_result
));
431 } else if (strcmp(command
, "search") == 0) {
432 rval
= t1_search(testname
, rbt
, mctx
, &dns_result
);
434 if (dns_result
== exp_result
) {
437 t_info("find returned %s, expected %s\n",
438 dns_result_totext(dns_result
),
439 dns_result_totext(exp_result
));
445 dns_rbt_destroy(&rbt
);
447 isc_entropy_detach(&ectx
);
448 isc_mem_destroy(&mctx
);
453 test_dns_rbt_x(const char *filename
) {
465 fp
= fopen(filename
, "r");
468 while ((p
= t_fgetbs(fp
)) != NULL
) {
473 * Skip comment lines.
475 if ((isspace((unsigned char)*p
)) || (*p
== '#')) {
481 * Name of db file, command, testname,
484 cnt
= t_bustline(p
, Tokens
);
486 result
= test_rbt_gen(Tokens
[0], Tokens
[1],
488 t_dns_result_fromtext(Tokens
[3]));
489 if (result
!= T_PASS
)
492 t_info("bad format in %s at line %d\n",
501 t_info("Missing datafile %s\n", filename
);
505 result
= T_UNRESOLVED
;
506 if ((nfails
== 0) && (nprobs
== 0))
515 static const char *a1
= "dns_rbt_create creates a rbt and returns "
516 "ISC_R_SUCCESS on success";
522 t_assert("dns_rbt_create", 1, T_REQUIRED
, "%s", a1
);
523 result
= test_dns_rbt_x("dns_rbt_create_1_data");
527 static const char *a2
= "dns_rbt_addname adds a name to a database and "
528 "returns ISC_R_SUCCESS on success";
534 t_assert("dns_rbt_addname", 2, T_REQUIRED
, "%s", a2
);
535 result
= test_dns_rbt_x("dns_rbt_addname_1_data");
539 static const char *a3
= "when name already exists, dns_rbt_addname() "
540 "returns ISC_R_EXISTS";
546 t_assert("dns_rbt_addname", 3, T_REQUIRED
, "%s", a3
);
547 result
= test_dns_rbt_x("dns_rbt_addname_2_data");
551 static const char *a4
= "when name exists, dns_rbt_deletename() returns "
558 t_assert("dns_rbt_deletename", 4, T_REQUIRED
, "%s", a4
);
559 result
= test_dns_rbt_x("dns_rbt_deletename_1_data");
563 static const char *a5
= "when name does not exist, dns_rbt_deletename() "
564 "returns ISC_R_NOTFOUND";
569 t_assert("dns_rbt_deletename", 5, T_REQUIRED
, "%s", a5
);
570 result
= test_dns_rbt_x("dns_rbt_deletename_2_data");
574 static const char *a6
= "when name exists and exactly matches the "
575 "search name dns_rbt_findname() returns ISC_R_SUCCESS";
581 t_assert("dns_rbt_findname", 6, T_REQUIRED
, "%s", a6
);
582 result
= test_dns_rbt_x("dns_rbt_findname_1_data");
586 static const char *a7
= "when a name does not exist, "
587 "dns_rbt_findname returns ISC_R_NOTFOUND";
593 t_assert("dns_rbt_findname", 7, T_REQUIRED
, "%s", a7
);
594 result
= test_dns_rbt_x("dns_rbt_findname_2_data");
598 static const char *a8
= "when a superdomain is found with data matching name, "
599 "dns_rbt_findname returns DNS_R_PARTIALMATCH";
605 t_assert("dns_rbt_findname", 8, T_REQUIRED
, "%s", a8
);
606 result
= test_dns_rbt_x("dns_rbt_findname_3_data");
611 static const char *a9
= "a call to dns_rbtnodechain_init(chain, mctx) "
615 t9_walkchain(dns_rbtnodechain_t
*chain
, dns_rbt_t
*rbt
) {
618 unsigned int nlabels
;
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
;
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
));
645 t_info("first name:\t<%s>\n", fixedname_totext(&name
));
646 t_info("first origin:\t<%s>\n",
647 fixedname_totext(&origin
));
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
));
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 "
675 dns_result_totext(dns_result
));
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
));
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
),
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
)));
723 * Test by exercising the first|last|next|prev funcs in useful ways.
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
)
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
));
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
),
750 t_info("\tgot %s\n", dns_result_totext(dns_result
));
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
)
769 dns_rbtnodechain_t chain
;
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
;
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
));
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
);
816 if (rbt_init(dbfile
, &rbt
, mctx
)) {
817 t_info("rbt_init %s failed\n", dbfile
);
819 isc_entropy_detach(&ectx
);
820 isc_mem_destroy(&mctx
);
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
));
846 * Set the starting node.
849 dns_result
= dns_rbt_findnode(rbt
, dns_fixedname_name(&dns_findname
),
850 dns_fixedname_name(&dns_foundname
),
851 &node
, &chain
, DNS_RBTFIND_EMPTYDATA
,
854 if (dns_result
!= ISC_R_SUCCESS
) {
855 t_info("dns_rbt_findnode failed %s\n",
856 dns_result_totext(dns_result
));
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.
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
,
888 if (dns_result
!= ISC_R_SUCCESS
) {
889 t_info("\tdns_rbt_findnode failed %s\n",
890 dns_result_totext(dns_result
));
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
);
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
);
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
);
954 dns_rbtnodechain_invalidate(&chain
);
955 dns_rbt_destroy(&rbt
);
958 isc_entropy_detach(&ectx
);
959 isc_mem_destroy(&mctx
);
965 test_dns_rbtnodechain_init(const char *filename
) {
977 fp
= fopen(filename
, "r");
980 while ((p
= t_fgetbs(fp
)) != NULL
) {
985 * Skip comment lines.
987 if ((isspace((unsigned char)*p
)) || (*p
== '#')) {
992 cnt
= t_bustline(p
, Tokens
);
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
)
1012 t_info("bad format in %s at line %d\n",
1021 t_info("Missing datafile %s\n", filename
);
1025 result
= T_UNRESOLVED
;
1027 if ((nfails
== 0) && (nprobs
== 0))
1039 t_assert("dns_rbtnodechain_init", 9, T_REQUIRED
, "%s", a9
);
1040 result
= test_dns_rbtnodechain_init("dns_rbtnodechain_init_data");
1045 t_dns_rbtnodechain_first(char *dbfile
, char *expected_firstname
,
1046 char *expected_firstorigin
,
1047 char *expected_nextname
,
1048 char *expected_nextorigin
)
1053 dns_rbtnodechain_t chain
;
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
;
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
));
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
);
1098 if (rbt_init(dbfile
, &rbt
, mctx
)) {
1099 t_info("rbt_init %s failed\n", dbfile
);
1101 isc_entropy_detach(&ectx
);
1102 isc_mem_destroy(&mctx
);
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
;
1135 expected_result
= DNS_R_NEWORIGIN
;
1136 nfails
+= t_namechk(dns_result
, &dns_name
, expected_nextname
,
1137 &dns_origin
, expected_nextorigin
, expected_result
);
1144 dns_rbtnodechain_invalidate(&chain
);
1146 dns_rbt_destroy(&rbt
);
1148 isc_entropy_detach(&ectx
);
1149 isc_mem_destroy(&mctx
);
1154 test_dns_rbtnodechain_first(const char *filename
) {
1166 fp
= fopen(filename
, "r");
1169 while ((p
= t_fgetbs(fp
)) != NULL
) {
1174 * Skip comment lines.
1176 if ((isspace((unsigned char)*p
)) || (*p
== '#')) {
1181 cnt
= t_bustline(p
, Tokens
);
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
)
1196 t_info("bad format in %s at line %d\n",
1205 t_info("Missing datafile %s\n", filename
);
1209 result
= T_UNRESOLVED
;
1211 if ((nfails
== 0) && (nprobs
== 0))
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";
1229 t_assert("dns_rbtnodechain_first", 10, T_REQUIRED
, "%s", a10
);
1230 result
= test_dns_rbtnodechain_first("dns_rbtnodechain_first_data");
1235 t_dns_rbtnodechain_last(char *dbfile
, char *expected_lastname
,
1236 char *expected_lastorigin
,
1237 char *expected_prevname
,
1238 char *expected_prevorigin
)
1244 dns_rbtnodechain_t chain
;
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
;
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
));
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
);
1289 if (rbt_init(dbfile
, &rbt
, mctx
)) {
1290 t_info("rbt_init %s failed\n", dbfile
);
1292 isc_entropy_detach(&ectx
);
1293 isc_mem_destroy(&mctx
);
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
;
1327 expected_result
= DNS_R_NEWORIGIN
;
1328 nfails
+= t_namechk(dns_result
, &dns_name
, expected_prevname
,
1329 &dns_origin
, expected_prevorigin
, expected_result
);
1336 dns_rbtnodechain_invalidate(&chain
);
1337 dns_rbt_destroy(&rbt
);
1340 isc_entropy_detach(&ectx
);
1341 isc_mem_destroy(&mctx
);
1347 test_dns_rbtnodechain_last(const char *filename
) {
1359 fp
= fopen(filename
, "r");
1362 while ((p
= t_fgetbs(fp
)) != NULL
) {
1367 * Skip comment lines.
1369 if ((isspace((unsigned char)*p
)) || (*p
== '#')) {
1374 cnt
= t_bustline(p
, Tokens
);
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
)
1389 t_info("bad format in %s at line %d\n",
1398 t_info("Missing datafile %s\n", filename
);
1402 result
= T_UNRESOLVED
;
1404 if ((nfails
== 0) && (nprobs
== 0))
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";
1422 t_assert("dns_rbtnodechain_last", 11, T_REQUIRED
, "%s", a11
);
1423 result
= test_dns_rbtnodechain_last("dns_rbtnodechain_last_data");
1428 t_dns_rbtnodechain_next(char *dbfile
, char *findname
,
1429 char *nextname
, char *nextorigin
)
1436 dns_rbtnode_t
*node
;
1437 dns_rbtnodechain_t chain
;
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
;
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
));
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
);
1481 if (rbt_init(dbfile
, &rbt
, mctx
)) {
1482 t_info("rbt_init %s failed\n", dbfile
);
1484 isc_entropy_detach(&ectx
);
1485 isc_mem_destroy(&mctx
);
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
));
1508 * Set the starting node.
1511 dns_result
= dns_rbt_findnode(rbt
, dns_fixedname_name(&dns_findname
),
1512 dns_fixedname_name(&dns_foundname
),
1513 &node
, &chain
, DNS_RBTFIND_EMPTYDATA
,
1516 if (dns_result
!= ISC_R_SUCCESS
) {
1517 t_info("dns_rbt_findnode failed %s\n",
1518 dns_result_totext(dns_result
));
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
);
1544 dns_rbtnodechain_invalidate(&chain
);
1545 dns_rbt_destroy(&rbt
);
1548 isc_entropy_detach(&ectx
);
1549 isc_mem_destroy(&mctx
);
1555 test_dns_rbtnodechain_next(const char *filename
) {
1567 fp
= fopen(filename
, "r");
1570 while ((p
= t_fgetbs(fp
)) != NULL
) {
1575 * Skip comment lines.
1577 if ((isspace((unsigned char)*p
)) || (*p
== '#')) {
1582 cnt
= t_bustline(p
, Tokens
);
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
)
1596 t_info("bad format in %s at line %d\n",
1605 t_info("Missing datafile %s\n", filename
);
1609 result
= T_UNRESOLVED
;
1611 if ((nfails
== 0) && (nprobs
== 0))
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";
1630 t_assert("dns_rbtnodechain_next", 12, T_REQUIRED
, "%s", a12
);
1631 result
= test_dns_rbtnodechain_next("dns_rbtnodechain_next_data");
1636 t_dns_rbtnodechain_prev(char *dbfile
, char *findname
, char *prevname
,
1643 dns_rbtnode_t
*node
;
1644 dns_rbtnodechain_t chain
;
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
;
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
));
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
);
1688 if (rbt_init(dbfile
, &rbt
, mctx
)) {
1689 t_info("rbt_init %s failed\n", dbfile
);
1691 isc_entropy_detach(&ectx
);
1692 isc_mem_destroy(&mctx
);
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
));
1715 * Set the starting node.
1718 dns_result
= dns_rbt_findnode(rbt
, dns_fixedname_name(&dns_findname
),
1719 dns_fixedname_name(&dns_foundname
),
1720 &node
, &chain
, DNS_RBTFIND_EMPTYDATA
,
1723 if (dns_result
!= ISC_R_SUCCESS
) {
1724 t_info("dns_rbt_findnode failed %s\n",
1725 dns_result_totext(dns_result
));
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
);
1751 dns_rbtnodechain_invalidate(&chain
);
1752 dns_rbt_destroy(&rbt
);
1755 isc_entropy_detach(&ectx
);
1756 isc_mem_destroy(&mctx
);
1762 test_dns_rbtnodechain_prev(const char *filename
) {
1774 fp
= fopen(filename
, "r");
1777 while ((p
= t_fgetbs(fp
)) != NULL
) {
1782 * Skip comment lines.
1784 if ((isspace((unsigned char)*p
)) || (*p
== '#')) {
1789 cnt
= t_bustline(p
, Tokens
);
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
)
1803 t_info("bad format in %s at line %d\n",
1812 t_info("Missing datafile %s\n", filename
);
1816 result
= T_UNRESOLVED
;
1818 if ((nfails
== 0) && (nprobs
== 0))
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";
1836 t_assert("dns_rbtnodechain_prev", 13, T_REQUIRED
, "%s", a13
);
1837 result
= test_dns_rbtnodechain_prev("dns_rbtnodechain_prev_data");
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" },