Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / bin / tests / db / t_db.c
blobed88f422fa1cde217902b14dfc68ed04638149ae
1 /* $NetBSD: t_db.c,v 1.7 2014/12/10 04:37:53 christos Exp $ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2013 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 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_db.c,v 1.41 2011/03/12 04:59:46 tbox Exp */
22 #include <config.h>
24 #include <ctype.h>
25 #include <stdlib.h>
27 #include <isc/entropy.h>
28 #include <isc/hash.h>
29 #include <isc/mem.h>
30 #include <isc/string.h>
31 #include <isc/util.h>
33 #include <dns/db.h>
34 #include <dns/fixedname.h>
35 #include <dns/rdata.h>
36 #include <dns/rdataclass.h>
37 #include <dns/rdatatype.h>
38 #include <dns/rdatalist.h>
39 #include <dns/rdataset.h>
40 #include <dns/result.h>
42 #include <tests/t_api.h>
44 static isc_result_t
45 t_create(const char *db_type, const char *origin, const char *class,
46 const char *model, isc_mem_t *mctx, dns_db_t **db)
48 int len;
49 isc_result_t dns_result;
50 dns_dbtype_t dbtype;
51 isc_textregion_t region;
52 isc_buffer_t origin_buffer;
53 dns_fixedname_t dns_origin;
54 dns_rdataclass_t rdataclass;
57 dbtype = dns_dbtype_zone;
58 if (strcasecmp(model, "cache") == 0)
59 dbtype = dns_dbtype_cache;
61 dns_fixedname_init(&dns_origin);
62 len = strlen(origin);
63 isc_buffer_constinit(&origin_buffer, origin, len);
64 isc_buffer_add(&origin_buffer, len);
65 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_origin),
66 &origin_buffer, NULL, 0, NULL);
67 if (dns_result != ISC_R_SUCCESS) {
68 t_info("dns_name_fromtext failed %s\n",
69 dns_result_totext(dns_result));
70 return(dns_result);
73 DE_CONST(class, region.base);
74 region.length = strlen(class);
75 dns_result = dns_rdataclass_fromtext(&rdataclass, &region);
76 if (dns_result != ISC_R_SUCCESS) {
77 t_info("dns_rdataclass_fromtext failed %s\n",
78 dns_result_totext(dns_result));
79 return(dns_result);
82 dns_result = dns_db_create(mctx, db_type,
83 dns_fixedname_name(&dns_origin),
84 dbtype, rdataclass, 0, NULL, db);
85 if (dns_result != ISC_R_SUCCESS)
86 t_info("dns_db_create failed %s\n",
87 dns_result_totext(dns_result));
89 return(dns_result);
93 static int
94 t_dns_db_load(char **av) {
95 char *filename;
96 char *db_type;
97 char *origin;
98 char *model;
99 char *class;
100 char *expected_load_result;
101 char *findname;
102 char *find_type;
103 char *expected_find_result;
105 int result;
106 int len;
107 dns_db_t *db;
108 isc_result_t dns_result;
109 isc_result_t isc_result;
110 isc_mem_t *mctx;
111 isc_entropy_t *ectx;
112 dns_dbnode_t *nodep;
113 isc_textregion_t textregion;
114 isc_buffer_t findname_buffer;
115 dns_fixedname_t dns_findname;
116 dns_fixedname_t dns_foundname;
117 dns_rdataset_t rdataset;
118 dns_rdatatype_t rdatatype;
119 dns_dbversion_t *versionp;
120 isc_result_t exp_load_result;
121 isc_result_t exp_find_result;
123 db = NULL;
124 mctx = NULL;
125 ectx = NULL;
126 filename = T_ARG(0);
127 db_type = T_ARG(1);
128 origin = T_ARG(2);
129 model = T_ARG(3);
130 class = T_ARG(4);
131 expected_load_result = T_ARG(5);
132 findname = T_ARG(6);
133 find_type = T_ARG(7);
134 expected_find_result = T_ARG(8);
136 t_info("testing using file %s and name %s\n", filename, findname);
138 exp_load_result = t_dns_result_fromtext(expected_load_result);
139 exp_find_result = t_dns_result_fromtext(expected_find_result);
141 isc_result = isc_mem_create(0, 0, &mctx);
142 if (isc_result != ISC_R_SUCCESS) {
143 t_info("isc_mem_create failed %s\n",
144 isc_result_totext(isc_result));
145 return(T_UNRESOLVED);
148 isc_result = isc_entropy_create(mctx, &ectx);
149 if (isc_result != ISC_R_SUCCESS) {
150 t_info("isc_entropy_create failed %s\n",
151 isc_result_totext(isc_result));
152 isc_mem_destroy(&mctx);
153 return(T_UNRESOLVED);
156 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
157 if (isc_result != ISC_R_SUCCESS) {
158 t_info("isc_hash_create failed %s\n",
159 isc_result_totext(isc_result));
160 isc_entropy_detach(&ectx);
161 isc_mem_destroy(&mctx);
162 return(T_UNRESOLVED);
165 dns_result = t_create(db_type, origin, class, model, mctx, &db);
166 if (dns_result != ISC_R_SUCCESS) {
167 isc_hash_destroy();
168 isc_entropy_detach(&ectx);
169 isc_mem_destroy(&mctx);
170 return(T_UNRESOLVED);
173 dns_result = dns_db_load(db, filename);
174 if (dns_result != exp_load_result) {
175 t_info("dns_db_load returned %s, expected %s\n",
176 dns_result_totext(dns_result),
177 dns_result_totext(exp_load_result));
178 dns_db_detach(&db);
179 isc_hash_destroy();
180 isc_entropy_detach(&ectx);
181 isc_mem_destroy(&mctx);
182 return(T_FAIL);
184 if (dns_result != ISC_R_SUCCESS) {
185 result = T_PASS;
186 goto cleanup_db;
189 dns_fixedname_init(&dns_findname);
190 len = strlen(findname);
191 isc_buffer_init(&findname_buffer, findname, len);
192 isc_buffer_add(&findname_buffer, len);
193 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname),
194 &findname_buffer, NULL, 0, NULL);
195 if (dns_result != ISC_R_SUCCESS) {
196 t_info("dns_name_fromtext failed %s\n",
197 dns_result_totext(dns_result));
198 dns_db_detach(&db);
199 isc_hash_destroy();
200 isc_entropy_detach(&ectx);
201 isc_mem_destroy(&mctx);
202 return(T_UNRESOLVED);
205 textregion.base = find_type;
206 textregion.length = strlen(find_type);
207 dns_result = dns_rdatatype_fromtext(&rdatatype, &textregion);
208 if (dns_result != ISC_R_SUCCESS) {
209 t_info("dns_rdatatype_fromtext %s failed %s\n",
210 find_type,
211 dns_result_totext(dns_result));
212 dns_db_detach(&db);
213 isc_hash_destroy();
214 isc_entropy_detach(&ectx);
215 isc_mem_destroy(&mctx);
216 return(T_UNRESOLVED);
219 versionp = NULL;
220 dns_fixedname_init(&dns_foundname);
221 dns_rdataset_init(&rdataset);
222 if (dns_db_iszone(db))
223 dns_db_currentversion(db, &versionp);
224 nodep = NULL;
226 dns_result = dns_db_find(db,
227 dns_fixedname_name(&dns_findname),
228 versionp,
229 rdatatype,
230 DNS_DBFIND_GLUEOK,
232 &nodep,
233 dns_fixedname_name(&dns_foundname),
234 &rdataset, NULL);
236 if (dns_result != exp_find_result) {
237 t_info("dns_db_find returned %s, expected %s\n",
238 dns_result_totext(dns_result),
239 dns_result_totext(exp_find_result));
240 result = T_FAIL;
241 } else {
242 result = T_PASS;
245 if (dns_result != ISC_R_NOTFOUND) {
246 dns_db_detachnode(db, &nodep);
247 if (dns_rdataset_isassociated(&rdataset))
248 dns_rdataset_disassociate(&rdataset);
251 if (dns_db_iszone(db))
252 dns_db_closeversion(db, &versionp, ISC_FALSE);
253 cleanup_db:
254 dns_db_detach(&db);
255 isc_hash_destroy();
256 isc_entropy_detach(&ectx);
257 isc_mem_destroy(&mctx);
258 return(result);
261 static const char *a1 =
262 "A call to dns_db_load(db, filename) loads the contents of "
263 "the database in filename into db.";
265 static void
266 t1(void) {
267 int result;
269 t_assert("dns_db_load", 1, T_REQUIRED, "%s", a1);
270 result = t_eval("dns_db_load_data", t_dns_db_load, 9);
271 t_result(result);
275 static const char *a2 =
276 "When the database db has cache semantics, a call to "
277 "dns_db_iscache(db) returns ISC_TRUE.";
279 static int
280 t_dns_db_zc_x(char *filename, char *db_type, char *origin, char *class,
281 dns_dbtype_t dbtype, isc_boolean_t(*cf)(dns_db_t *),
282 isc_boolean_t exp_result)
284 int result;
285 int len;
286 dns_db_t *db;
287 isc_result_t dns_result;
288 isc_result_t isc_result;
289 isc_mem_t *mctx;
290 isc_entropy_t *ectx;
291 dns_rdataclass_t rdataclass;
292 isc_textregion_t textregion;
293 isc_buffer_t origin_buffer;
294 dns_fixedname_t dns_origin;
296 db = NULL;
297 mctx = NULL;
298 ectx = NULL;
300 t_info("testing using file %s\n", filename);
302 dns_fixedname_init(&dns_origin);
303 len = strlen(origin);
304 isc_buffer_init(&origin_buffer, origin, len);
305 isc_buffer_add(&origin_buffer, len);
306 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_origin),
307 &origin_buffer, NULL, 0, NULL);
308 if (dns_result != ISC_R_SUCCESS) {
309 t_info("dns_name_fromtext failed %s\n",
310 dns_result_totext(dns_result));
311 return(T_UNRESOLVED);
314 textregion.base = class;
315 textregion.length = strlen(class);
316 dns_result = dns_rdataclass_fromtext(&rdataclass, &textregion);
317 if (dns_result != ISC_R_SUCCESS) {
318 t_info("dns_rdataclass_fromtext failed %s\n",
319 dns_result_totext(dns_result));
320 return(T_UNRESOLVED);
323 isc_result = isc_mem_create(0, 0, &mctx);
324 if (isc_result != ISC_R_SUCCESS) {
325 t_info("isc_mem_create failed %s\n",
326 isc_result_totext(isc_result));
327 return(T_UNRESOLVED);
330 isc_result = isc_entropy_create(mctx, &ectx);
331 if (isc_result != ISC_R_SUCCESS) {
332 t_info("isc_entropy_create failed %s\n",
333 isc_result_totext(isc_result));
334 isc_mem_destroy(&mctx);
335 return(T_UNRESOLVED);
338 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
339 if (isc_result != ISC_R_SUCCESS) {
340 t_info("isc_hash_create failed %s\n",
341 isc_result_totext(isc_result));
342 isc_entropy_detach(&ectx);
343 isc_mem_destroy(&mctx);
344 return(T_UNRESOLVED);
347 dns_result = dns_db_create(mctx, db_type,
348 dns_fixedname_name(&dns_origin),
349 dbtype, rdataclass, 0, NULL, &db);
350 if (dns_result != ISC_R_SUCCESS) {
351 t_info("dns_db_create failed %s\n",
352 dns_result_totext(dns_result));
353 isc_hash_destroy();
354 isc_entropy_detach(&ectx);
355 isc_mem_destroy(&mctx);
356 return(T_UNRESOLVED);
359 dns_result = dns_db_load(db, filename);
360 if (dns_result == ISC_R_SUCCESS) {
361 if ((*cf)(db) == exp_result)
362 result = T_PASS;
363 else
364 result = T_FAIL;
365 } else {
366 t_info("dns_db_load failed %s\n",
367 dns_result_totext(dns_result));
368 result = T_FAIL;
371 dns_db_detach(&db);
372 isc_hash_destroy();
373 isc_entropy_detach(&ectx);
374 isc_mem_destroy(&mctx);
375 return(result);
378 static int
379 test_dns_db_zc_x(const char *filename, dns_dbtype_t dbtype,
380 isc_boolean_t(*cf)(dns_db_t *), isc_boolean_t exp_result)
383 FILE *fp;
384 char *p;
385 int line;
386 int cnt;
387 int result;
388 int nfails;
389 int nprobs;
390 char *tokens[T_MAXTOKS];
392 nfails = 0;
393 nprobs = 0;
395 fp = fopen(filename, "r");
396 if (fp != NULL) {
397 line = 0;
398 while ((p = t_fgetbs(fp)) != NULL) {
400 ++line;
403 * Skip comment lines.
405 if ((isspace((unsigned char)*p)) || (*p == '#')) {
406 (void)free(p);
407 continue;
410 cnt = t_bustline(p, tokens);
411 if (cnt == 4) {
412 result = t_dns_db_zc_x(tokens[0], /* file */
413 tokens[1], /* type */
414 tokens[2], /* origin */
415 tokens[3], /* class */
416 dbtype, /* cache */
417 cf, /* check func */
418 exp_result);/* expect */
419 if (result != T_PASS) {
420 if (result == T_FAIL)
421 ++nfails;
422 else
423 ++nprobs;
425 } else {
426 t_info("bad format in %s at line %d\n",
427 filename, line);
428 ++nprobs;
431 (void)free(p);
433 (void)fclose(fp);
434 } else {
435 t_info("Missing datafile %s\n", filename);
436 ++nprobs;
439 result = T_UNRESOLVED;
441 if (nfails == 0 && nprobs == 0)
442 result = T_PASS;
443 else if (nfails)
444 result = T_FAIL;
446 return(result);
449 static void
450 t2(void) {
451 int result;
453 t_assert("dns_db_iscache", 2, T_REQUIRED, "%s", a2);
454 result = test_dns_db_zc_x("dns_db_iscache_1_data",
455 dns_dbtype_cache, dns_db_iscache, ISC_TRUE);
456 t_result(result);
460 static const char *a3 =
461 "When the database db has zone semantics, a call to "
462 "dns_db_iscache(db) returns ISC_FALSE.";
465 static void
466 t3(void) {
467 int result;
469 t_assert("dns_db_iscache", 3, T_REQUIRED, "%s", a3);
470 result = test_dns_db_zc_x("dns_db_iscache_2_data",
471 dns_dbtype_zone, dns_db_iscache, ISC_FALSE);
472 t_result(result);
476 static const char *a4 =
477 "When the database db has zone semantics, a call to "
478 "dns_db_iszone(db) returns ISC_TRUE.";
481 static void
482 t4(void) {
483 int result;
485 t_assert("dns_db_iszone", 4, T_REQUIRED, "%s", a4);
486 result = test_dns_db_zc_x("dns_db_iszone_1_data",
487 dns_dbtype_zone, dns_db_iszone, ISC_TRUE);
488 t_result(result);
492 static const char *a5 =
493 "When the database db has cache semantics, a call to "
494 "dns_db_iszone(db) returns ISC_FALSE.";
496 static void
497 t5(void) {
498 int result;
500 t_assert("dns_db_iszone", 5, T_REQUIRED, "%s", a5);
501 result = test_dns_db_zc_x("dns_db_iszone_2_data",
502 dns_dbtype_cache, dns_db_iszone, ISC_FALSE);
503 t_result(result);
506 static int
507 t_dns_db_origin(char **av) {
509 char *filename;
510 char *origin;
512 int result;
513 int len;
514 int order;
515 isc_result_t dns_result;
516 isc_result_t isc_result;
517 isc_mem_t *mctx;
518 isc_entropy_t *ectx;
519 dns_db_t *db;
520 dns_fixedname_t dns_origin;
521 dns_fixedname_t dns_dborigin;
522 isc_buffer_t origin_buffer;
524 db = NULL;
525 mctx = NULL;
526 ectx = NULL;
528 filename = T_ARG(0);
529 origin = T_ARG(1);
531 t_info("testing with database %s and origin %s\n",
532 filename, origin);
534 isc_result = isc_mem_create(0, 0, &mctx);
535 if (isc_result != ISC_R_SUCCESS) {
536 t_info("isc_mem_create failed %s\n",
537 isc_result_totext(isc_result));
538 return(T_UNRESOLVED);
541 isc_result = isc_entropy_create(mctx, &ectx);
542 if (isc_result != ISC_R_SUCCESS) {
543 t_info("isc_entropy_create failed %s\n",
544 isc_result_totext(isc_result));
545 isc_mem_destroy(&mctx);
546 return(T_UNRESOLVED);
549 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
550 if (isc_result != ISC_R_SUCCESS) {
551 t_info("isc_hash_create failed %s\n",
552 isc_result_totext(isc_result));
553 isc_entropy_detach(&ectx);
554 isc_mem_destroy(&mctx);
555 return(T_UNRESOLVED);
558 dns_result = t_create("rbt", origin, "in", "isc_true", mctx, &db);
559 if (dns_result != ISC_R_SUCCESS) {
560 t_info("t_create failed %s\n",
561 dns_result_totext(dns_result));
562 isc_hash_destroy();
563 isc_entropy_detach(&ectx);
564 isc_mem_destroy(&mctx);
565 return(T_UNRESOLVED);
567 dns_fixedname_init(&dns_origin);
568 dns_fixedname_init(&dns_dborigin);
570 len = strlen(origin);
571 isc_buffer_init(&origin_buffer, origin, len);
572 isc_buffer_add(&origin_buffer, len);
574 dns_result = dns_db_load(db, filename);
575 if (dns_result != ISC_R_SUCCESS) {
576 t_info("dns_db_load failed %s\n",
577 dns_result_totext(dns_result));
578 dns_db_detach(&db);
579 isc_hash_destroy();
580 isc_entropy_detach(&ectx);
581 isc_mem_destroy(&mctx);
582 return(T_UNRESOLVED);
585 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_origin),
586 &origin_buffer, NULL, 0, NULL);
587 if (dns_result != ISC_R_SUCCESS) {
588 t_info("dns_name_fromtext failed %s\n",
589 dns_result_totext(dns_result));
590 dns_db_detach(&db);
591 isc_hash_destroy();
592 isc_entropy_detach(&ectx);
593 isc_mem_destroy(&mctx);
594 return(T_UNRESOLVED);
596 order = dns_name_compare(dns_fixedname_name(&dns_origin),
597 dns_db_origin(db));
598 if (order == 0) {
599 result = T_PASS;
600 } else {
601 t_info("dns_name_compare returned %d\n", order);
602 result = T_FAIL;
605 dns_db_detach(&db);
606 isc_hash_destroy();
607 isc_entropy_detach(&ectx);
608 isc_mem_destroy(&mctx);
609 return(result);
613 static const char *a6 =
614 "A call to dns_db_origin(db) returns the origin of the database.";
616 static void
617 t6(void) {
618 int result;
620 t_assert("dns_db_origin", 6, T_REQUIRED, "%s", a6);
621 result = t_eval("dns_db_origin_data", t_dns_db_origin, 2);
622 t_result(result);
626 static const char *a7 =
627 "A call to dns_db_class(db) returns the class of the database.";
630 #define CLASSBUFLEN 256
632 static int
633 t_dns_db_class(char **av) {
635 char *filename;
636 char *class;
638 int result;
639 isc_result_t dns_result;
640 isc_result_t isc_result;
641 isc_mem_t *mctx;
642 isc_entropy_t *ectx;
643 dns_db_t *db;
644 dns_rdataclass_t rdataclass;
645 dns_rdataclass_t db_rdataclass;
646 isc_textregion_t textregion;
648 filename = T_ARG(0);
649 class = T_ARG(1);
650 db = NULL;
651 mctx = NULL;
652 ectx = NULL;
654 t_info("testing with database %s and class %s\n",
655 filename, class);
657 textregion.base = class;
658 textregion.length = strlen(class);
659 dns_result = dns_rdataclass_fromtext(&rdataclass, &textregion);
660 if (dns_result != ISC_R_SUCCESS) {
661 t_info("dns_rdataclass_fromtext failed %s\n",
662 dns_result_totext(dns_result));
663 return(T_UNRESOLVED);
666 isc_result = isc_mem_create(0, 0, &mctx);
667 if (isc_result != ISC_R_SUCCESS) {
668 t_info("isc_mem_create failed %s\n",
669 isc_result_totext(isc_result));
670 return(T_UNRESOLVED);
673 isc_result = isc_entropy_create(mctx, &ectx);
674 if (isc_result != ISC_R_SUCCESS) {
675 t_info("isc_entropy_create failed %s\n",
676 isc_result_totext(isc_result));
677 isc_mem_destroy(&mctx);
678 return(T_UNRESOLVED);
681 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
682 if (isc_result != ISC_R_SUCCESS) {
683 t_info("isc_hash_create failed %s\n",
684 isc_result_totext(isc_result));
685 isc_entropy_detach(&ectx);
686 isc_mem_destroy(&mctx);
687 return(T_UNRESOLVED);
690 dns_result = t_create("rbt", ".", class, "isc_true", mctx, &db);
691 if (dns_result != ISC_R_SUCCESS) {
692 t_info("t_create failed %s\n",
693 dns_result_totext(dns_result));
694 isc_hash_destroy();
695 isc_entropy_detach(&ectx);
696 isc_mem_destroy(&mctx);
697 return(T_UNRESOLVED);
700 dns_result = dns_db_load(db, filename);
701 if (dns_result != ISC_R_SUCCESS) {
702 t_info("dns_db_load failed %s\n",
703 dns_result_totext(dns_result));
704 dns_db_detach(&db);
705 isc_hash_destroy();
706 isc_entropy_detach(&ectx);
707 isc_mem_destroy(&mctx);
708 return(T_UNRESOLVED);
711 db_rdataclass = dns_db_class(db);
712 if (db_rdataclass == rdataclass)
713 result = T_PASS;
714 else {
715 char classbuf[DNS_RDATACLASS_FORMATSIZE];
716 dns_rdataclass_format(db_rdataclass,
717 classbuf, sizeof(classbuf));
718 t_info("dns_db_class returned %s, expected %s\n",
719 classbuf, class);
720 result = T_FAIL;
723 dns_db_detach(&db);
724 isc_hash_destroy();
725 isc_entropy_detach(&ectx);
726 isc_mem_destroy(&mctx);
727 return(result);
730 static void
731 t7(void) {
732 int result;
734 t_assert("dns_db_class", 7, T_REQUIRED, "%s", a7);
735 result = t_eval("dns_db_class_data", t_dns_db_class, 2);
736 t_result(result);
740 static const char *a8 =
741 "A call to dns_db_currentversion() opens the current "
742 "version for reading.";
744 static int
745 t_dns_db_currentversion(char **av) {
746 char *filename;
747 char *db_type;
748 char *origin;
749 char *class;
750 char *model;
751 char *findname;
752 char *findtype;
754 int result;
755 int len;
756 dns_db_t *db;
757 isc_result_t dns_result;
758 isc_result_t isc_result;
759 isc_mem_t *mctx;
760 isc_entropy_t *ectx;
761 dns_dbnode_t *nodep;
762 isc_textregion_t textregion;
763 isc_buffer_t findname_buffer;
764 dns_fixedname_t dns_findname;
765 dns_fixedname_t dns_foundname;
766 dns_rdataset_t rdataset;
767 dns_rdatatype_t rdatatype;
768 dns_dbversion_t *cversionp;
769 dns_dbversion_t *nversionp;
771 filename = T_ARG(0);
772 db_type = T_ARG(1);
773 origin = T_ARG(2);
774 class = T_ARG(3);
775 model = T_ARG(4);
776 findname = T_ARG(5);
777 findtype = T_ARG(6);
778 db = NULL;
779 mctx = NULL;
780 ectx = NULL;
782 t_info("testing using file %s and name %s\n", filename, findname);
784 isc_result = isc_mem_create(0, 0, &mctx);
785 if (isc_result != ISC_R_SUCCESS) {
786 t_info("isc_mem_create failed %s\n",
787 isc_result_totext(isc_result));
788 return(T_UNRESOLVED);
791 isc_result = isc_entropy_create(mctx, &ectx);
792 if (isc_result != ISC_R_SUCCESS) {
793 t_info("isc_entropy_create failed %s\n",
794 isc_result_totext(isc_result));
795 isc_mem_destroy(&mctx);
796 return(T_UNRESOLVED);
799 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
800 if (isc_result != ISC_R_SUCCESS) {
801 t_info("isc_hash_create failed %s\n",
802 isc_result_totext(isc_result));
803 isc_entropy_detach(&ectx);
804 isc_mem_destroy(&mctx);
805 return(T_UNRESOLVED);
808 dns_result = t_create(db_type, origin, class, model, mctx, &db);
809 if (dns_result != ISC_R_SUCCESS) {
810 isc_hash_destroy();
811 isc_entropy_detach(&ectx);
812 isc_mem_destroy(&mctx);
813 return(T_UNRESOLVED);
816 dns_result = dns_db_load(db, filename);
817 if (dns_result != ISC_R_SUCCESS) {
818 t_info("dns_db_load returned %s\n",
819 dns_result_totext(dns_result));
820 dns_db_detach(&db);
821 isc_hash_destroy();
822 isc_entropy_detach(&ectx);
823 isc_mem_destroy(&mctx);
824 return(T_UNRESOLVED);
827 dns_fixedname_init(&dns_findname);
828 len = strlen(findname);
829 isc_buffer_init(&findname_buffer, findname, len);
830 isc_buffer_add(&findname_buffer, len);
831 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname),
832 &findname_buffer, NULL, 0, NULL);
833 if (dns_result != ISC_R_SUCCESS) {
834 t_info("dns_name_fromtext failed %s\n",
835 dns_result_totext(dns_result));
836 dns_db_detach(&db);
837 isc_hash_destroy();
838 isc_entropy_detach(&ectx);
839 isc_mem_destroy(&mctx);
840 return(T_UNRESOLVED);
843 textregion.base = findtype;
844 textregion.length = strlen(findtype);
845 dns_result = dns_rdatatype_fromtext(&rdatatype, &textregion);
846 if (dns_result != ISC_R_SUCCESS) {
847 t_info("dns_rdatatype_fromtext %s failed %s\n",
848 findtype,
849 dns_result_totext(dns_result));
850 dns_db_detach(&db);
851 isc_hash_destroy();
852 isc_entropy_detach(&ectx);
853 isc_mem_destroy(&mctx);
854 return(T_UNRESOLVED);
858 * find a name we know is there
861 cversionp = NULL;
862 dns_fixedname_init(&dns_foundname);
863 dns_rdataset_init(&rdataset);
864 dns_db_currentversion(db, &cversionp);
865 nodep = NULL;
867 dns_result = dns_db_find(db,
868 dns_fixedname_name(&dns_findname),
869 cversionp,
870 rdatatype,
873 &nodep,
874 dns_fixedname_name(&dns_foundname),
875 &rdataset, NULL);
877 if (dns_result != ISC_R_SUCCESS) {
878 t_info("unable to find %s using current version\n", findname);
879 dns_db_closeversion(db, &cversionp, ISC_FALSE);
880 dns_db_detach(&db);
881 isc_hash_destroy();
882 isc_entropy_detach(&ectx);
883 isc_mem_destroy(&mctx);
884 return(T_UNRESOLVED);
888 * create a new version
889 * delete the found rdataset in the new version
890 * attempt to find the rdataset again and expect the find to fail
891 * close/commit the new version
892 * attempt to find the rdataset in the current version and
893 * expect the find to succeed
896 nversionp = NULL;
897 dns_result = dns_db_newversion(db, &nversionp);
898 if (dns_result != ISC_R_SUCCESS) {
899 t_info("dns_db_newversion failed %s\n",
900 dns_result_totext(dns_result));
901 dns_db_detachnode(db, &nodep);
902 dns_rdataset_disassociate(&rdataset);
903 dns_db_closeversion(db, &cversionp, ISC_FALSE);
904 dns_db_detach(&db);
905 isc_hash_destroy();
906 isc_entropy_detach(&ectx);
907 isc_mem_destroy(&mctx);
908 return(T_UNRESOLVED);
912 * Delete the found rdataset in the new version.
914 dns_result = dns_db_deleterdataset(db, nodep, nversionp, rdatatype, 0);
915 if (dns_result != ISC_R_SUCCESS) {
916 t_info("dns_db_deleterdataset failed %s\n",
917 dns_result_totext(dns_result));
918 dns_rdataset_disassociate(&rdataset);
919 dns_db_detachnode(db, &nodep);
920 dns_db_closeversion(db, &nversionp, ISC_FALSE);
921 dns_db_closeversion(db, &cversionp, ISC_FALSE);
922 dns_db_detach(&db);
923 isc_hash_destroy();
924 isc_entropy_detach(&ectx);
925 isc_mem_destroy(&mctx);
926 return(T_UNRESOLVED);
930 * Don't need these now.
932 dns_rdataset_disassociate(&rdataset);
933 dns_db_detachnode(db, &nodep);
934 nodep = NULL;
937 * Find the deleted rdataset and expect it to fail.
939 dns_result = dns_db_find(db,
940 dns_fixedname_name(&dns_findname),
941 nversionp,
942 rdatatype,
945 &nodep,
946 dns_fixedname_name(&dns_foundname),
947 &rdataset, NULL);
949 if ((dns_result != ISC_R_NOTFOUND) && (dns_result != DNS_R_NXDOMAIN)) {
950 t_info("unexpectedly found %s using current version\n",
951 findname);
952 dns_db_closeversion(db, &cversionp, ISC_FALSE);
953 dns_db_closeversion(db, &nversionp, ISC_FALSE);
954 dns_db_detach(&db);
955 isc_hash_destroy();
956 isc_entropy_detach(&ectx);
957 isc_mem_destroy(&mctx);
958 return(T_FAIL);
962 * Close/commit the new version.
964 dns_db_closeversion(db, &nversionp, ISC_TRUE);
967 * Find the deleted rdata in the current version.
969 dns_result = dns_db_find(db, dns_fixedname_name(&dns_findname),
970 cversionp, rdatatype, DNS_DBFIND_GLUEOK,
971 0, &nodep, dns_fixedname_name(&dns_foundname),
972 &rdataset, NULL);
975 * And expect it to succeed.
977 if (dns_result == ISC_R_SUCCESS) {
978 result = T_PASS;
979 } else {
980 t_info("cound not find %s using current version\n", findname);
981 dns_db_closeversion(db, &cversionp, ISC_FALSE);
982 dns_db_detach(&db);
983 isc_hash_destroy();
984 isc_entropy_detach(&ectx);
985 isc_mem_destroy(&mctx);
986 result = T_FAIL;
989 dns_db_detachnode(db, &nodep);
990 dns_rdataset_disassociate(&rdataset);
992 dns_db_closeversion(db, &cversionp, ISC_FALSE);
993 dns_db_detach(&db);
994 isc_hash_destroy();
995 isc_entropy_detach(&ectx);
996 isc_mem_destroy(&mctx);
998 return(result);
1001 static void
1002 t8(void) {
1003 int result;
1005 t_assert("dns_db_currentversion", 8, T_REQUIRED, "%s", a8);
1006 result = t_eval("dns_db_currentversion_data",
1007 t_dns_db_currentversion, 7);
1008 t_result(result);
1011 static const char *a9 =
1012 "A call to dns_db_newversion() opens a new version for "
1013 "reading and writing.";
1015 static int
1016 t_dns_db_newversion(char **av) {
1018 char *filename;
1019 char *db_type;
1020 char *origin;
1021 char *class;
1022 char *model;
1023 char *newname;
1024 char *newtype;
1026 int result;
1027 int len;
1028 int rval;
1029 dns_db_t *db;
1030 isc_result_t dns_result;
1031 isc_result_t isc_result;
1032 isc_mem_t *mctx;
1033 isc_entropy_t *ectx;
1034 dns_dbnode_t *nodep;
1035 dns_dbnode_t *found_nodep;
1036 isc_textregion_t textregion;
1037 isc_buffer_t newname_buffer;
1038 dns_fixedname_t dns_newname;
1039 dns_fixedname_t dns_foundname;
1040 dns_rdata_t added_rdata = DNS_RDATA_INIT;
1041 const char * added_rdata_data;
1042 dns_rdataset_t added_rdataset;
1043 dns_rdata_t found_rdata = DNS_RDATA_INIT;
1044 dns_rdataset_t found_rdataset;
1045 dns_rdatatype_t rdatatype;
1046 dns_rdataclass_t rdataclass;
1047 dns_dbversion_t *nversionp;
1048 dns_rdatalist_t rdatalist;
1050 filename = T_ARG(0);
1051 db_type = T_ARG(1);
1052 origin = T_ARG(2);
1053 class = T_ARG(3);
1054 model = T_ARG(4);
1055 newname = T_ARG(5);
1056 newtype = T_ARG(6);
1057 db = NULL;
1058 mctx = NULL;
1059 ectx = NULL;
1062 * Open a new version, add some data, commit it,
1063 * close it, open a new version, and check that changes
1064 * are present.
1067 t_info("testing using file %s and name %s\n", filename, newname);
1069 isc_result = isc_mem_create(0, 0, &mctx);
1070 if (isc_result != ISC_R_SUCCESS) {
1071 t_info("isc_mem_create failed %s\n",
1072 isc_result_totext(isc_result));
1073 return(T_UNRESOLVED);
1076 isc_result = isc_entropy_create(mctx, &ectx);
1077 if (isc_result != ISC_R_SUCCESS) {
1078 t_info("isc_entropy_create failed %s\n",
1079 isc_result_totext(isc_result));
1080 isc_mem_destroy(&mctx);
1081 return(T_UNRESOLVED);
1084 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
1085 if (isc_result != ISC_R_SUCCESS) {
1086 t_info("isc_hash_create failed %s\n",
1087 isc_result_totext(isc_result));
1088 isc_entropy_detach(&ectx);
1089 isc_mem_destroy(&mctx);
1090 return(T_UNRESOLVED);
1093 dns_result = t_create(db_type, origin, class, model, mctx, &db);
1094 if (dns_result != ISC_R_SUCCESS) {
1095 isc_hash_destroy();
1096 isc_entropy_detach(&ectx);
1097 isc_mem_destroy(&mctx);
1098 return(T_UNRESOLVED);
1101 dns_result = dns_db_load(db, filename);
1102 if (dns_result != ISC_R_SUCCESS) {
1103 t_info("dns_db_load returned %s\n",
1104 dns_result_totext(dns_result));
1105 dns_db_detach(&db);
1106 isc_hash_destroy();
1107 isc_entropy_detach(&ectx);
1108 isc_mem_destroy(&mctx);
1109 return(T_UNRESOLVED);
1113 * Add a new name.
1116 dns_fixedname_init(&dns_newname);
1117 len = strlen(newname);
1118 isc_buffer_init(&newname_buffer, newname, len);
1119 isc_buffer_add(&newname_buffer, len);
1120 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_newname),
1121 &newname_buffer, NULL, 0, NULL);
1122 if (dns_result != ISC_R_SUCCESS) {
1123 t_info("dns_name_fromtext failed %s\n",
1124 dns_result_totext(dns_result));
1125 dns_db_detach(&db);
1126 isc_hash_destroy();
1127 isc_entropy_detach(&ectx);
1128 isc_mem_destroy(&mctx);
1129 return(T_UNRESOLVED);
1132 nodep = NULL;
1133 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_newname),
1134 ISC_TRUE, &nodep);
1135 if (dns_result != ISC_R_SUCCESS) {
1136 t_info("dns_db_findnode failed %s\n",
1137 dns_result_totext(dns_result));
1138 dns_db_detach(&db);
1139 isc_hash_destroy();
1140 isc_entropy_detach(&ectx);
1141 isc_mem_destroy(&mctx);
1142 return(T_UNRESOLVED);
1146 * Open a new version and associate some rdata with the new name.
1149 textregion.base = newtype;
1150 textregion.length = strlen(newtype);
1151 dns_result = dns_rdatatype_fromtext(&rdatatype, &textregion);
1153 if (dns_result != ISC_R_SUCCESS) {
1154 t_info("dns_rdatatype_fromtext %s failed %s\n",
1155 newtype,
1156 dns_result_totext(dns_result));
1157 dns_db_detachnode(db, &nodep);
1158 dns_db_detach(&db);
1159 isc_hash_destroy();
1160 isc_entropy_detach(&ectx);
1161 isc_mem_destroy(&mctx);
1162 return(T_UNRESOLVED);
1165 textregion.base = class;
1166 textregion.length = strlen(class);
1167 dns_result = dns_rdataclass_fromtext(&rdataclass, &textregion);
1168 if (dns_result != ISC_R_SUCCESS) {
1169 t_info("dns_rdataclass_fromtext failed %s\n",
1170 dns_result_totext(dns_result));
1171 dns_db_detachnode(db, &nodep);
1172 dns_db_detach(&db);
1173 isc_hash_destroy();
1174 isc_entropy_detach(&ectx);
1175 isc_mem_destroy(&mctx);
1176 return(T_UNRESOLVED);
1179 dns_rdata_init(&added_rdata);
1180 added_rdata_data = "\x10\x00\x00\x01";
1181 DE_CONST(added_rdata_data, added_rdata.data);
1182 added_rdata.length = 4;
1183 added_rdata.rdclass = rdataclass;
1184 added_rdata.type = rdatatype;
1186 dns_rdataset_init(&added_rdataset);
1187 rdatalist.type = rdatatype;
1188 rdatalist.covers = 0;
1189 rdatalist.rdclass = rdataclass;
1190 rdatalist.ttl = 0;
1191 ISC_LIST_INIT(rdatalist.rdata);
1192 ISC_LIST_APPEND(rdatalist.rdata, &added_rdata, link);
1194 dns_result = dns_rdatalist_tordataset(&rdatalist, &added_rdataset);
1195 if (dns_result != ISC_R_SUCCESS) {
1196 t_info("dns_rdatalist_tordataset failed %s\n",
1197 dns_result_totext(dns_result));
1198 dns_db_detachnode(db, &nodep);
1199 dns_db_detach(&db);
1200 isc_hash_destroy();
1201 isc_entropy_detach(&ectx);
1202 isc_mem_destroy(&mctx);
1203 return(T_UNRESOLVED);
1206 nversionp = NULL;
1207 dns_result = dns_db_newversion(db, &nversionp);
1208 if (dns_result != ISC_R_SUCCESS) {
1209 t_info("dns_db_newversion failed %s\n",
1210 dns_result_totext(dns_result));
1211 dns_db_detachnode(db, &nodep);
1212 dns_db_detach(&db);
1213 isc_hash_destroy();
1214 isc_entropy_detach(&ectx);
1215 isc_mem_destroy(&mctx);
1216 return(T_UNRESOLVED);
1219 dns_result = dns_db_addrdataset(db, nodep, nversionp, 0,
1220 &added_rdataset, 0, NULL);
1221 if (dns_result != ISC_R_SUCCESS) {
1222 t_info("dns_db_addrdataset failed %s\n",
1223 dns_result_totext(dns_result));
1224 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1225 dns_db_detachnode(db, &nodep);
1226 dns_db_detach(&db);
1227 isc_hash_destroy();
1228 isc_entropy_detach(&ectx);
1229 isc_mem_destroy(&mctx);
1230 return(T_UNRESOLVED);
1234 * Close and commit the version.
1236 dns_db_closeversion(db, &nversionp, ISC_TRUE);
1237 dns_db_detachnode(db, &nodep);
1238 if (dns_rdataset_isassociated(&added_rdataset))
1239 dns_rdataset_disassociate(&added_rdataset);
1240 nodep = NULL;
1243 * Open a new version and find the data we added.
1245 dns_fixedname_init(&dns_foundname);
1246 dns_rdataset_init(&found_rdataset);
1247 nversionp = NULL;
1248 found_nodep = NULL;
1249 dns_db_newversion(db, &nversionp);
1252 * Find the recently added name and rdata.
1254 dns_result = dns_db_find(db, dns_fixedname_name(&dns_newname),
1255 nversionp, rdatatype, 0, 0, &found_nodep,
1256 dns_fixedname_name(&dns_foundname),
1257 &found_rdataset, NULL);
1259 if (dns_result != ISC_R_SUCCESS) {
1260 /* XXXWPK - NXRRSET ??? reference counts ??? */
1261 t_info("dns_db_find failed %s\n",
1262 dns_result_totext(dns_result));
1263 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1264 dns_db_detachnode(db, &found_nodep);
1265 if (dns_rdataset_isassociated(&found_rdataset))
1266 dns_rdataset_disassociate(&found_rdataset);
1267 dns_db_detach(&db);
1268 isc_hash_destroy();
1269 isc_entropy_detach(&ectx);
1270 isc_mem_destroy(&mctx);
1271 return(T_FAIL);
1274 dns_result = dns_rdataset_first(&found_rdataset);
1275 if (dns_result != ISC_R_SUCCESS) {
1276 t_info("dns_rdataset_first failed %s\n",
1277 dns_result_totext(dns_result));
1278 dns_db_detachnode(db, &nodep);
1279 if (dns_rdataset_isassociated(&found_rdataset))
1280 dns_rdataset_disassociate(&found_rdataset);
1281 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1282 dns_db_detach(&db);
1283 isc_hash_destroy();
1284 isc_entropy_detach(&ectx);
1285 isc_mem_destroy(&mctx);
1286 return(T_FAIL);
1290 * Now make sure its what we expect.
1292 dns_rdata_init(&found_rdata);
1293 dns_rdataset_current(&found_rdataset, &found_rdata);
1294 rval = dns_rdata_compare(&added_rdata, &found_rdata);
1295 if (rval == 0) {
1296 result = T_PASS;
1297 } else {
1298 t_info("dns_rdata_compare returned %d\n", rval);
1299 result = T_FAIL;
1303 * Don't need these now.
1305 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1306 if (dns_rdataset_isassociated(&found_rdataset))
1307 dns_rdataset_disassociate(&found_rdataset);
1308 dns_db_detachnode(db, &found_nodep);
1309 dns_db_detach(&db);
1310 isc_hash_destroy();
1311 isc_entropy_detach(&ectx);
1312 isc_mem_destroy(&mctx);
1314 return(result);
1317 static void
1318 t9(void) {
1319 int result;
1321 t_assert("dns_db_newversion", 9, T_REQUIRED, "%s", a9);
1322 result = t_eval("dns_db_newversion_data", t_dns_db_newversion, 7);
1323 t_result(result);
1326 static const char *a10 =
1327 "When versionp points to a read-write version and commit is "
1328 "ISC_TRUE, a call to dns_db_closeversion(db, versionp, commit) "
1329 "causes all changes made in the version to take effect, "
1330 "and returns ISC_R_SUCCESS.";
1332 static int
1333 t_dns_db_closeversion_1(char **av) {
1334 char *filename;
1335 char *db_type;
1336 char *origin;
1337 char *class;
1338 char *model;
1339 char *new_name;
1340 char *new_type;
1341 char *existing_name;
1342 char *existing_type;
1344 int result;
1345 int len;
1346 int rval;
1347 int nfails;
1348 dns_db_t *db;
1349 isc_result_t dns_result;
1350 isc_result_t isc_result;
1351 isc_mem_t *mctx;
1352 isc_entropy_t *ectx;
1353 dns_dbnode_t *nodep;
1354 isc_textregion_t textregion;
1355 isc_buffer_t name_buffer;
1356 dns_fixedname_t dns_newname;
1357 dns_fixedname_t dns_foundname;
1358 dns_fixedname_t dns_existingname;
1359 dns_rdata_t added_rdata = DNS_RDATA_INIT;
1360 const char * added_rdata_data;
1361 dns_rdataset_t added_rdataset;
1362 dns_rdata_t found_rdata = DNS_RDATA_INIT;
1363 dns_rdataset_t found_rdataset;
1364 dns_rdatatype_t new_rdatatype;
1365 dns_rdatatype_t existing_rdatatype;
1366 dns_rdataclass_t rdataclass;
1367 dns_dbversion_t *nversionp;
1368 dns_dbversion_t *cversionp;
1369 dns_rdatalist_t rdatalist;
1371 filename = T_ARG(0);
1372 db_type = T_ARG(1);
1373 origin = T_ARG(2);
1374 class = T_ARG(3);
1375 model = T_ARG(4);
1376 new_name = T_ARG(5);
1377 new_type = T_ARG(6);
1378 existing_name = T_ARG(7);
1379 existing_type = T_ARG(8);
1381 nfails = 0;
1382 db = NULL;
1383 mctx = NULL;
1384 ectx = NULL;
1387 * Open a new version, add some data,
1388 * remove some data, close with commit, open the current
1389 * version and check that changes are present.
1392 t_info("testing using file %s and name %s\n", filename, new_name);
1394 isc_result = isc_mem_create(0, 0, &mctx);
1395 if (isc_result != ISC_R_SUCCESS) {
1396 t_info("isc_mem_create failed %s\n",
1397 isc_result_totext(isc_result));
1398 return(T_UNRESOLVED);
1401 isc_result = isc_entropy_create(mctx, &ectx);
1402 if (isc_result != ISC_R_SUCCESS) {
1403 t_info("isc_entropy_create failed %s\n",
1404 isc_result_totext(isc_result));
1405 isc_mem_destroy(&mctx);
1406 return(T_UNRESOLVED);
1409 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
1410 if (isc_result != ISC_R_SUCCESS) {
1411 t_info("isc_hash_create failed %s\n",
1412 isc_result_totext(isc_result));
1413 isc_entropy_detach(&ectx);
1414 isc_mem_destroy(&mctx);
1415 return(T_UNRESOLVED);
1418 dns_result = t_create(db_type, origin, class, model, mctx, &db);
1419 if (dns_result != ISC_R_SUCCESS) {
1420 isc_hash_destroy();
1421 isc_entropy_detach(&ectx);
1422 isc_mem_destroy(&mctx);
1423 return(T_UNRESOLVED);
1426 dns_result = dns_db_load(db, filename);
1427 if (dns_result != ISC_R_SUCCESS) {
1428 t_info("dns_db_load returned %s\n",
1429 dns_result_totext(dns_result));
1430 dns_db_detach(&db);
1431 isc_hash_destroy();
1432 isc_entropy_detach(&ectx);
1433 isc_mem_destroy(&mctx);
1434 return(T_UNRESOLVED);
1438 * Remove all rdata for an existing name.
1441 dns_fixedname_init(&dns_existingname);
1442 len = strlen(existing_name);
1443 isc_buffer_init(&name_buffer, existing_name, len);
1444 isc_buffer_add(&name_buffer, len);
1445 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_existingname),
1446 &name_buffer, NULL, 0, NULL);
1447 if (dns_result != ISC_R_SUCCESS) {
1448 t_info("dns_name_fromtext failed %s\n",
1449 dns_result_totext(dns_result));
1450 dns_db_detach(&db);
1451 isc_hash_destroy();
1452 isc_entropy_detach(&ectx);
1453 isc_mem_destroy(&mctx);
1454 return(T_UNRESOLVED);
1457 textregion.base = existing_type;
1458 textregion.length = strlen(existing_type);
1459 dns_result = dns_rdatatype_fromtext(&existing_rdatatype, &textregion);
1460 if (dns_result != ISC_R_SUCCESS) {
1461 t_info("dns_rdatatype_fromtext %s failed %s\n",
1462 existing_type,
1463 dns_result_totext(dns_result));
1464 dns_db_detachnode(db, &nodep);
1465 dns_db_detach(&db);
1466 isc_hash_destroy();
1467 isc_entropy_detach(&ectx);
1468 isc_mem_destroy(&mctx);
1469 return(T_UNRESOLVED);
1472 nodep = NULL;
1473 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_existingname),
1474 ISC_FALSE, &nodep);
1475 if (dns_result != ISC_R_SUCCESS) {
1476 t_info("dns_db_findnode %s\n",
1477 dns_result_totext(dns_result));
1478 dns_db_detach(&db);
1479 isc_hash_destroy();
1480 isc_entropy_detach(&ectx);
1481 isc_mem_destroy(&mctx);
1482 return(T_UNRESOLVED);
1485 /* open a new version */
1486 nversionp = NULL;
1487 dns_result = dns_db_newversion(db, &nversionp);
1488 if (dns_result != ISC_R_SUCCESS) {
1489 t_info("dns_db_newversion failed %s\n",
1490 dns_result_totext(dns_result));
1491 dns_db_detachnode(db, &nodep);
1492 dns_db_detach(&db);
1493 isc_hash_destroy();
1494 isc_entropy_detach(&ectx);
1495 isc_mem_destroy(&mctx);
1496 return(T_UNRESOLVED);
1499 dns_result = dns_db_deleterdataset(db, nodep, nversionp,
1500 existing_rdatatype, 0);
1501 if (dns_result != ISC_R_SUCCESS) {
1502 t_info("dns_db_deleterdataset failed %s\n",
1503 dns_result_totext(dns_result));
1504 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1505 dns_db_detachnode(db, &nodep);
1506 dns_db_detach(&db);
1507 isc_hash_destroy();
1508 isc_entropy_detach(&ectx);
1509 isc_mem_destroy(&mctx);
1510 return(T_UNRESOLVED);
1514 * add a new name and associate some rdata with it
1517 dns_db_detachnode(db, &nodep);
1518 nodep = NULL;
1520 dns_fixedname_init(&dns_newname);
1521 len = strlen(new_name);
1522 isc_buffer_init(&name_buffer, new_name, len);
1523 isc_buffer_add(&name_buffer, len);
1524 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_newname),
1525 &name_buffer, NULL, 0, NULL);
1526 if (dns_result != ISC_R_SUCCESS) {
1527 t_info("dns_name_fromtext failed %s\n",
1528 dns_result_totext(dns_result));
1529 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1530 dns_db_detach(&db);
1531 isc_hash_destroy();
1532 isc_entropy_detach(&ectx);
1533 isc_mem_destroy(&mctx);
1534 return(T_UNRESOLVED);
1537 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_newname),
1538 ISC_TRUE, &nodep);
1539 if (dns_result != ISC_R_SUCCESS) {
1540 t_info("dns_db_findnode failed %s\n",
1541 dns_result_totext(dns_result));
1542 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1543 dns_db_detach(&db);
1544 isc_hash_destroy();
1545 isc_entropy_detach(&ectx);
1546 isc_mem_destroy(&mctx);
1547 return(T_UNRESOLVED);
1551 * associate some rdata with the new name
1554 textregion.base = new_type;
1555 textregion.length = strlen(new_type);
1556 dns_result = dns_rdatatype_fromtext(&new_rdatatype, &textregion);
1557 if (dns_result != ISC_R_SUCCESS) {
1558 t_info("dns_rdatatype_fromtext %s failed %s\n",
1559 new_type,
1560 dns_result_totext(dns_result));
1561 dns_db_detachnode(db, &nodep);
1562 dns_db_detach(&db);
1563 isc_hash_destroy();
1564 isc_entropy_detach(&ectx);
1565 isc_mem_destroy(&mctx);
1566 return(T_UNRESOLVED);
1569 textregion.base = class;
1570 textregion.length = strlen(class);
1571 dns_result = dns_rdataclass_fromtext(&rdataclass, &textregion);
1572 if (dns_result != ISC_R_SUCCESS) {
1573 t_info("dns_rdataclass_fromtext failed %s\n",
1574 dns_result_totext(dns_result));
1575 dns_db_detachnode(db, &nodep);
1576 dns_db_detach(&db);
1577 isc_hash_destroy();
1578 isc_entropy_detach(&ectx);
1579 isc_mem_destroy(&mctx);
1580 return(T_UNRESOLVED);
1583 dns_rdata_init(&added_rdata);
1584 added_rdata_data = "\x10\x00\x00\x01";
1585 DE_CONST(added_rdata_data, added_rdata.data);
1586 added_rdata.length = 4;
1587 added_rdata.rdclass = rdataclass;
1588 added_rdata.type = new_rdatatype;
1590 dns_rdataset_init(&added_rdataset);
1591 rdatalist.type = new_rdatatype;
1592 rdatalist.covers = 0;
1593 rdatalist.rdclass = rdataclass;
1594 rdatalist.ttl = 0;
1595 ISC_LIST_INIT(rdatalist.rdata);
1596 ISC_LIST_APPEND(rdatalist.rdata, &added_rdata, link);
1598 dns_result = dns_rdatalist_tordataset(&rdatalist, &added_rdataset);
1599 if (dns_result != ISC_R_SUCCESS) {
1600 t_info("dns_rdatalist_tordataset failed %s\n",
1601 dns_result_totext(dns_result));
1602 dns_db_detachnode(db, &nodep);
1603 dns_db_detach(&db);
1604 isc_hash_destroy();
1605 isc_entropy_detach(&ectx);
1606 isc_mem_destroy(&mctx);
1607 return(T_UNRESOLVED);
1610 dns_result = dns_db_addrdataset(db, nodep, nversionp, 0,
1611 &added_rdataset, 0, NULL);
1612 if (dns_result != ISC_R_SUCCESS) {
1613 t_info("dns_db_addrdataset failed %s\n",
1614 dns_result_totext(dns_result));
1615 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1616 dns_db_detachnode(db, &nodep);
1617 dns_db_detach(&db);
1618 isc_hash_destroy();
1619 isc_entropy_detach(&ectx);
1620 isc_mem_destroy(&mctx);
1621 return(T_UNRESOLVED);
1624 /* close and commit the version */
1625 dns_db_closeversion(db, &nversionp, ISC_TRUE);
1626 dns_db_detachnode(db, &nodep);
1627 nodep = NULL;
1629 /* open the current version and check changes */
1630 dns_fixedname_init(&dns_foundname);
1631 dns_rdataset_init(&found_rdataset);
1632 cversionp = NULL;
1633 dns_db_currentversion(db, &cversionp);
1635 /* find the recently added name and rdata */
1636 dns_result = dns_db_find(db,
1637 dns_fixedname_name(&dns_newname),
1638 cversionp,
1639 new_rdatatype,
1642 &nodep,
1643 dns_fixedname_name(&dns_foundname),
1644 &found_rdataset, NULL);
1646 if (dns_result != ISC_R_SUCCESS) {
1647 /* XXXWPK NXRRSET ??? reference counting ??? */
1648 t_info("dns_db_find failed %s\n",
1649 dns_result_totext(dns_result));
1650 dns_db_closeversion(db, &cversionp, ISC_FALSE);
1651 dns_db_detachnode(db, &nodep);
1652 if (dns_rdataset_isassociated(&found_rdataset))
1653 dns_rdataset_disassociate(&found_rdataset);
1654 dns_db_detach(&db);
1655 isc_hash_destroy();
1656 isc_entropy_detach(&ectx);
1657 isc_mem_destroy(&mctx);
1658 return(T_FAIL);
1661 dns_result = dns_rdataset_first(&found_rdataset);
1662 if (dns_result != ISC_R_SUCCESS) {
1663 t_info("dns_rdataset_first failed %s\n",
1664 dns_result_totext(dns_result));
1665 dns_db_detachnode(db, &nodep);
1666 if (dns_rdataset_isassociated(&found_rdataset))
1667 dns_rdataset_disassociate(&found_rdataset);
1668 dns_db_closeversion(db, &cversionp, ISC_FALSE);
1669 dns_db_detach(&db);
1670 isc_hash_destroy();
1671 isc_entropy_detach(&ectx);
1672 isc_mem_destroy(&mctx);
1673 return(T_FAIL);
1677 * Now make sure its what we expect.
1679 dns_rdata_init(&found_rdata);
1680 dns_rdataset_current(&found_rdataset, &found_rdata);
1681 rval = dns_rdata_compare(&added_rdata, &found_rdata);
1682 if (rval != 0) {
1683 t_info("dns_rdata_compare returned %d\n", rval);
1684 ++nfails;
1688 * Now check the rdata deletion.
1691 if (dns_rdataset_isassociated(&found_rdataset))
1692 dns_rdataset_disassociate(&found_rdataset);
1693 dns_rdataset_init(&found_rdataset);
1694 dns_db_detachnode(db, &nodep);
1695 nodep = NULL;
1696 dns_fixedname_init(&dns_foundname);
1698 dns_result = dns_db_find(db, dns_fixedname_name(&dns_existingname),
1699 cversionp, existing_rdatatype,
1700 0, 0, &nodep,
1701 dns_fixedname_name(&dns_foundname),
1702 &found_rdataset, NULL);
1705 if ((dns_result != ISC_R_NOTFOUND) && (dns_result != DNS_R_NXDOMAIN)) {
1706 dns_rdataset_disassociate(&found_rdataset);
1707 dns_db_detachnode(db, &nodep);
1708 t_info("dns_db_find %s returned %s\n", existing_name,
1709 dns_result_totext(dns_result));
1710 ++nfails;
1713 dns_db_closeversion(db, &cversionp, ISC_FALSE);
1714 dns_db_detach(&db);
1715 isc_hash_destroy();
1716 isc_entropy_detach(&ectx);
1717 isc_mem_destroy(&mctx);
1719 if (nfails == 0)
1720 result = T_PASS;
1721 else
1722 result = T_FAIL;
1724 return(result);
1727 static void
1728 t10(void) {
1729 int result;
1731 t_assert("dns_db_closeversion", 10, T_REQUIRED, "%s", a10);
1732 result = t_eval("dns_db_closeversion_1_data",
1733 t_dns_db_closeversion_1, 9);
1734 t_result(result);
1737 static const char *a11 =
1738 "When versionp points to a read-write version and commit is "
1739 "ISC_FALSE, a call to dns_db_closeversion(db, versionp, commit) "
1740 "causes all changes made in the version to to be rolled back, "
1741 "and returns ISC_R_SUCCESS.";
1743 static int
1744 t_dns_db_closeversion_2(char **av) {
1745 char *filename;
1746 char *db_type;
1747 char *origin;
1748 char *class;
1749 char *model;
1750 char *new_name;
1751 char *new_type;
1752 char *existing_name;
1753 char *existing_type;
1755 int result;
1756 int len;
1757 int rval;
1758 int nfails;
1759 dns_db_t *db;
1760 isc_result_t dns_result;
1761 isc_result_t isc_result;
1762 isc_mem_t *mctx;
1763 isc_entropy_t *ectx;
1764 dns_dbnode_t *nodep;
1765 isc_textregion_t textregion;
1766 isc_buffer_t name_buffer;
1767 dns_fixedname_t dns_newname;
1768 dns_fixedname_t dns_foundname;
1769 dns_fixedname_t dns_existingname;
1770 dns_rdata_t added_rdata = DNS_RDATA_INIT;
1771 const char * added_rdata_data;
1772 dns_rdataset_t added_rdataset;
1773 dns_rdata_t found_rdata = DNS_RDATA_INIT;
1774 dns_rdataset_t found_rdataset;
1775 dns_rdatatype_t new_rdatatype;
1776 dns_rdatatype_t existing_rdatatype;
1777 dns_rdataclass_t rdataclass;
1778 dns_dbversion_t *nversionp;
1779 dns_dbversion_t *cversionp;
1780 dns_rdatalist_t rdatalist;
1782 filename = T_ARG(0);
1783 db_type = T_ARG(1);
1784 origin = T_ARG(2);
1785 class = T_ARG(3);
1786 model = T_ARG(4);
1787 new_name = T_ARG(5);
1788 new_type = T_ARG(6);
1789 existing_name = T_ARG(7);
1790 existing_type = T_ARG(8);
1792 nfails = 0;
1793 db = NULL;
1794 mctx = NULL;
1795 ectx = NULL;
1798 * Open a new version, add some data,
1799 * remove some data, close with commit, open the current
1800 * version and check that changes are present.
1803 t_info("testing using file %s and name %s\n", filename, new_name);
1805 isc_result = isc_mem_create(0, 0, &mctx);
1806 if (isc_result != ISC_R_SUCCESS) {
1807 t_info("isc_mem_create failed %s\n",
1808 isc_result_totext(isc_result));
1809 return(T_UNRESOLVED);
1812 isc_result = isc_entropy_create(mctx, &ectx);
1813 if (isc_result != ISC_R_SUCCESS) {
1814 t_info("isc_entropy_create failed %s\n",
1815 isc_result_totext(isc_result));
1816 isc_mem_destroy(&mctx);
1817 return(T_UNRESOLVED);
1820 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
1821 if (isc_result != ISC_R_SUCCESS) {
1822 t_info("isc_hash_create failed %s\n",
1823 isc_result_totext(isc_result));
1824 isc_entropy_detach(&ectx);
1825 isc_mem_destroy(&mctx);
1826 return(T_UNRESOLVED);
1829 dns_result = t_create(db_type, origin, class, model, mctx, &db);
1830 if (dns_result != ISC_R_SUCCESS) {
1831 isc_hash_destroy();
1832 isc_entropy_detach(&ectx);
1833 isc_mem_destroy(&mctx);
1834 return(T_UNRESOLVED);
1837 dns_result = dns_db_load(db, filename);
1838 if (dns_result != ISC_R_SUCCESS) {
1839 t_info("dns_db_load returned %s\n",
1840 dns_result_totext(dns_result));
1841 dns_db_detach(&db);
1842 isc_hash_destroy();
1843 isc_entropy_detach(&ectx);
1844 isc_mem_destroy(&mctx);
1845 return(T_UNRESOLVED);
1849 * Remove all rdata for an existing name.
1852 dns_fixedname_init(&dns_existingname);
1853 len = strlen(existing_name);
1854 isc_buffer_init(&name_buffer, existing_name, len);
1855 isc_buffer_add(&name_buffer, len);
1856 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_existingname),
1857 &name_buffer, NULL, 0, NULL);
1858 if (dns_result != ISC_R_SUCCESS) {
1859 t_info("dns_name_fromtext failed %s\n",
1860 dns_result_totext(dns_result));
1861 dns_db_detach(&db);
1862 isc_hash_destroy();
1863 isc_entropy_detach(&ectx);
1864 isc_mem_destroy(&mctx);
1865 return(T_UNRESOLVED);
1868 textregion.base = existing_type;
1869 textregion.length = strlen(existing_type);
1870 dns_result = dns_rdatatype_fromtext(&existing_rdatatype, &textregion);
1871 if (dns_result != ISC_R_SUCCESS) {
1872 t_info("dns_rdatatype_fromtext %s failed %s\n",
1873 existing_type,
1874 dns_result_totext(dns_result));
1875 dns_db_detachnode(db, &nodep);
1876 dns_db_detach(&db);
1877 isc_hash_destroy();
1878 isc_entropy_detach(&ectx);
1879 isc_mem_destroy(&mctx);
1880 return(T_UNRESOLVED);
1883 nodep = NULL;
1884 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_existingname),
1885 ISC_FALSE, &nodep);
1886 if (dns_result != ISC_R_SUCCESS) {
1887 t_info("dns_db_findnode %s\n",
1888 dns_result_totext(dns_result));
1889 dns_db_detach(&db);
1890 isc_hash_destroy();
1891 isc_entropy_detach(&ectx);
1892 isc_mem_destroy(&mctx);
1893 return(T_UNRESOLVED);
1897 * Open a new version.
1899 nversionp = NULL;
1900 dns_result = dns_db_newversion(db, &nversionp);
1901 if (dns_result != ISC_R_SUCCESS) {
1902 t_info("dns_db_newversion failed %s\n",
1903 dns_result_totext(dns_result));
1904 dns_db_detachnode(db, &nodep);
1905 dns_db_detach(&db);
1906 isc_hash_destroy();
1907 isc_entropy_detach(&ectx);
1908 isc_mem_destroy(&mctx);
1909 return(T_UNRESOLVED);
1912 dns_result = dns_db_deleterdataset(db, nodep, nversionp,
1913 existing_rdatatype, 0);
1914 if (dns_result != ISC_R_SUCCESS) {
1915 t_info("dns_db_deleterdataset failed %s\n",
1916 dns_result_totext(dns_result));
1917 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1918 dns_db_detachnode(db, &nodep);
1919 dns_db_detach(&db);
1920 isc_hash_destroy();
1921 isc_entropy_detach(&ectx);
1922 isc_mem_destroy(&mctx);
1923 return(T_UNRESOLVED);
1927 * add a new name and associate some rdata with it
1930 dns_db_detachnode(db, &nodep);
1931 nodep = NULL;
1933 dns_fixedname_init(&dns_newname);
1934 len = strlen(new_name);
1935 isc_buffer_init(&name_buffer, new_name, len);
1936 isc_buffer_add(&name_buffer, len);
1937 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_newname),
1938 &name_buffer, NULL, 0, NULL);
1939 if (dns_result != ISC_R_SUCCESS) {
1940 t_info("dns_name_fromtext failed %s\n",
1941 dns_result_totext(dns_result));
1942 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1943 dns_db_detach(&db);
1944 isc_hash_destroy();
1945 isc_entropy_detach(&ectx);
1946 isc_mem_destroy(&mctx);
1947 return(T_UNRESOLVED);
1950 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_newname),
1951 ISC_TRUE, &nodep);
1952 if (dns_result != ISC_R_SUCCESS) {
1953 t_info("dns_db_findnode failed %s\n",
1954 dns_result_totext(dns_result));
1955 dns_db_closeversion(db, &nversionp, ISC_FALSE);
1956 dns_db_detach(&db);
1957 isc_hash_destroy();
1958 isc_entropy_detach(&ectx);
1959 isc_mem_destroy(&mctx);
1960 return(T_UNRESOLVED);
1963 textregion.base = new_type;
1964 textregion.length = strlen(new_type);
1965 dns_result = dns_rdatatype_fromtext(&new_rdatatype, &textregion);
1966 if (dns_result != ISC_R_SUCCESS) {
1967 t_info("dns_rdatatype_fromtext %s failed %s\n",
1968 new_type, dns_result_totext(dns_result));
1969 dns_db_detachnode(db, &nodep);
1970 dns_db_detach(&db);
1971 isc_hash_destroy();
1972 isc_entropy_detach(&ectx);
1973 isc_mem_destroy(&mctx);
1974 return(T_UNRESOLVED);
1977 textregion.base = class;
1978 textregion.length = strlen(class);
1979 dns_result = dns_rdataclass_fromtext(&rdataclass, &textregion);
1980 if (dns_result != ISC_R_SUCCESS) {
1981 t_info("dns_rdataclass_fromtext failed %s\n",
1982 dns_result_totext(dns_result));
1983 dns_db_detachnode(db, &nodep);
1984 dns_db_detach(&db);
1985 isc_hash_destroy();
1986 isc_entropy_detach(&ectx);
1987 isc_mem_destroy(&mctx);
1988 return(T_UNRESOLVED);
1991 dns_rdata_init(&added_rdata);
1992 added_rdata_data = "\x10\x00\x00\x01";
1993 DE_CONST(added_rdata_data, added_rdata.data);
1994 added_rdata.length = 4;
1995 added_rdata.rdclass = rdataclass;
1996 added_rdata.type = new_rdatatype;
1998 dns_rdataset_init(&added_rdataset);
1999 rdatalist.type = new_rdatatype;
2000 rdatalist.covers = 0;
2001 rdatalist.rdclass = rdataclass;
2002 rdatalist.ttl = 0;
2003 ISC_LIST_INIT(rdatalist.rdata);
2004 ISC_LIST_APPEND(rdatalist.rdata, &added_rdata, link);
2006 dns_result = dns_rdatalist_tordataset(&rdatalist, &added_rdataset);
2007 if (dns_result != ISC_R_SUCCESS) {
2008 t_info("dns_rdatalist_tordataset failed %s\n",
2009 dns_result_totext(dns_result));
2010 dns_db_detachnode(db, &nodep);
2011 dns_db_detach(&db);
2012 isc_hash_destroy();
2013 isc_entropy_detach(&ectx);
2014 isc_mem_destroy(&mctx);
2015 return(T_UNRESOLVED);
2018 dns_result = dns_db_addrdataset(db, nodep, nversionp, 0,
2019 &added_rdataset, 0, NULL);
2020 if (dns_result != ISC_R_SUCCESS) {
2021 t_info("dns_db_addrdataset failed %s\n",
2022 dns_result_totext(dns_result));
2023 dns_db_closeversion(db, &nversionp, ISC_FALSE);
2024 dns_db_detachnode(db, &nodep);
2025 dns_db_detach(&db);
2026 isc_hash_destroy();
2027 isc_entropy_detach(&ectx);
2028 isc_mem_destroy(&mctx);
2029 return(T_UNRESOLVED);
2033 * Check that our changes took.
2035 dns_db_detachnode(db, &nodep);
2036 nodep = NULL;
2037 dns_fixedname_init(&dns_foundname);
2038 dns_rdataset_init(&found_rdataset);
2041 * Find the recently added name and rdata.
2043 dns_result = dns_db_find(db, dns_fixedname_name(&dns_newname),
2044 nversionp, new_rdatatype, 0, 0, &nodep,
2045 dns_fixedname_name(&dns_foundname),
2046 &found_rdataset, NULL);
2048 if ((dns_result == ISC_R_NOTFOUND) ||
2049 (dns_result == DNS_R_NXDOMAIN) ||
2050 (dns_result == DNS_R_NXRRSET)) {
2052 t_info("dns_db_find failed %s\n",
2053 dns_result_totext(dns_result));
2054 dns_db_closeversion(db, &nversionp, ISC_FALSE);
2055 dns_db_detachnode(db, &nodep);
2056 if (dns_rdataset_isassociated(&found_rdataset))
2057 dns_rdataset_disassociate(&found_rdataset);
2058 dns_db_detach(&db);
2059 isc_hash_destroy();
2060 isc_entropy_detach(&ectx);
2061 isc_mem_destroy(&mctx);
2062 return(T_FAIL);
2065 dns_result = dns_rdataset_first(&found_rdataset);
2066 if (dns_result != ISC_R_SUCCESS) {
2067 t_info("dns_rdataset_first failed %s\n",
2068 dns_result_totext(dns_result));
2069 dns_db_detachnode(db, &nodep);
2070 if (dns_rdataset_isassociated(&found_rdataset))
2071 dns_rdataset_disassociate(&found_rdataset);
2072 dns_db_closeversion(db, &nversionp, ISC_FALSE);
2073 dns_db_detach(&db);
2074 isc_hash_destroy();
2075 isc_entropy_detach(&ectx);
2076 isc_mem_destroy(&mctx);
2077 return(T_FAIL);
2081 * Now make sure its what we expect.
2083 dns_rdata_init(&found_rdata);
2084 dns_rdataset_current(&found_rdataset, &found_rdata);
2085 rval = dns_rdata_compare(&added_rdata, &found_rdata);
2086 if (rval != 0) {
2087 t_info("dns_rdata_compare returned %d\n", rval);
2088 ++nfails;
2092 * Now check the rdata deletion.
2094 if (dns_rdataset_isassociated(&found_rdataset))
2095 dns_rdataset_disassociate(&found_rdataset);
2096 dns_rdataset_init(&found_rdataset);
2097 dns_db_detachnode(db, &nodep);
2098 nodep = NULL;
2099 dns_fixedname_init(&dns_foundname);
2101 dns_result = dns_db_find(db,
2102 dns_fixedname_name(&dns_existingname),
2103 nversionp,
2104 existing_rdatatype,
2107 &nodep,
2108 dns_fixedname_name(&dns_foundname),
2109 &found_rdataset, NULL);
2112 if ((dns_result != ISC_R_NOTFOUND) && (dns_result != DNS_R_NXDOMAIN)) {
2113 t_info("dns_db_find %s returned %s\n", existing_name,
2114 dns_result_totext(dns_result));
2115 if (dns_rdataset_isassociated(&found_rdataset))
2116 dns_rdataset_disassociate(&found_rdataset);
2117 dns_db_detachnode(db, &nodep);
2118 ++nfails;
2123 * Close the version without a commit.
2125 dns_db_closeversion(db, &nversionp, ISC_FALSE);
2128 * Open the current version and check changes.
2130 dns_fixedname_init(&dns_foundname);
2131 dns_rdataset_init(&found_rdataset);
2132 cversionp = NULL;
2133 dns_db_currentversion(db, &cversionp);
2136 * Find the recently added name and rdata.
2138 dns_result = dns_db_find(db,
2139 dns_fixedname_name(&dns_newname),
2140 cversionp,
2141 new_rdatatype,
2144 &nodep,
2145 dns_fixedname_name(&dns_foundname),
2146 &found_rdataset, NULL);
2148 if ((dns_result != ISC_R_NOTFOUND) && (dns_result != DNS_R_NXDOMAIN)) {
2149 t_info("dns_db_find %s returned %s\n", new_name,
2150 dns_result_totext(dns_result));
2151 dns_rdataset_disassociate(&found_rdataset);
2152 dns_db_detachnode(db, &nodep);
2153 dns_db_closeversion(db, &cversionp, ISC_FALSE);
2154 dns_db_detach(&db);
2155 isc_hash_destroy();
2156 isc_entropy_detach(&ectx);
2157 isc_mem_destroy(&mctx);
2158 return(T_FAIL);
2162 * Now check the rdata deletion.
2164 nodep = NULL;
2165 dns_rdataset_init(&found_rdataset);
2166 dns_fixedname_init(&dns_foundname);
2168 dns_result = dns_db_find(db, dns_fixedname_name(&dns_existingname),
2169 cversionp, existing_rdatatype, 0, 0,
2170 &nodep, dns_fixedname_name(&dns_foundname),
2171 &found_rdataset, NULL);
2174 if ((dns_result == ISC_R_NOTFOUND) ||
2175 (dns_result == DNS_R_NXDOMAIN) ||
2176 (dns_result == DNS_R_NXRRSET)) {
2178 t_info("dns_db_find %s returned %s\n", existing_name,
2179 dns_result_totext(dns_result));
2180 dns_rdataset_disassociate(&found_rdataset);
2181 dns_db_detachnode(db, &nodep);
2182 ++nfails;
2185 dns_db_detachnode(db, &nodep);
2186 dns_rdataset_disassociate(&found_rdataset);
2187 dns_db_closeversion(db, &cversionp, ISC_FALSE);
2188 dns_db_detach(&db);
2189 isc_hash_destroy();
2190 isc_entropy_detach(&ectx);
2191 isc_mem_destroy(&mctx);
2193 if (nfails == 0)
2194 result = T_PASS;
2195 else
2196 result = T_FAIL;
2198 return(result);
2201 static void
2202 t11(void) {
2203 int result;
2205 t_assert("dns_db_closeversion", 11, T_REQUIRED, "%s", a11);
2206 result = t_eval("dns_db_closeversion_2_data",
2207 t_dns_db_closeversion_2, 9);
2208 t_result(result);
2211 static const char *a12 =
2212 "A call to dns_db_expirenode() marks as stale all records at node "
2213 "which expire at or before 'now'. If 'now' is zero, then the current "
2214 "time will be used.";
2216 static int
2217 t_dns_db_expirenode(char **av) {
2218 char *filename;
2219 char *db_type;
2220 char *origin;
2221 char *class;
2222 char *existing_name;
2223 char *node_xtime;
2224 char *find_xtime;
2225 char *exp_find_result;
2227 int result;
2228 int len;
2229 dns_db_t *db;
2230 isc_result_t dns_result;
2231 isc_result_t exp_result;
2232 isc_result_t isc_result;
2233 isc_mem_t *mctx;
2234 isc_entropy_t *ectx;
2235 dns_dbnode_t *nodep;
2236 isc_buffer_t name_buffer;
2237 dns_fixedname_t dns_foundname;
2238 dns_fixedname_t dns_existingname;
2239 isc_stdtime_t node_expire_time;
2240 isc_stdtime_t find_expire_time;
2241 isc_stdtime_t now;
2242 dns_rdataset_t rdataset;
2244 filename = T_ARG(0);
2245 db_type = T_ARG(1);
2246 origin = T_ARG(2);
2247 class = T_ARG(3);
2248 existing_name = T_ARG(4);
2249 node_xtime = T_ARG(5);
2250 find_xtime = T_ARG(6);
2251 exp_find_result = T_ARG(7);
2252 mctx = NULL;
2253 ectx = NULL;
2256 * Find a node, mark it as stale, do a dns_db_find on the name and
2257 * expect it to fail.
2260 t_info("testing using file %s and name %s\n", filename, existing_name);
2262 node_expire_time = (isc_stdtime_t) strtol(node_xtime, NULL, 10);
2263 find_expire_time = (isc_stdtime_t) strtol(find_xtime, NULL, 10);
2264 exp_result = t_dns_result_fromtext(exp_find_result);
2266 isc_stdtime_get(&now);
2268 dns_fixedname_init(&dns_existingname);
2269 len = strlen(existing_name);
2270 isc_buffer_init(&name_buffer, existing_name, len);
2271 isc_buffer_add(&name_buffer, len);
2272 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_existingname),
2273 &name_buffer, NULL, 0, NULL);
2274 if (dns_result != ISC_R_SUCCESS) {
2275 t_info("dns_name_fromtext failed %s\n",
2276 dns_result_totext(dns_result));
2277 return(T_UNRESOLVED);
2280 isc_result = isc_mem_create(0, 0, &mctx);
2281 if (isc_result != ISC_R_SUCCESS) {
2282 t_info("isc_mem_create failed %s\n",
2283 isc_result_totext(isc_result));
2284 return(T_UNRESOLVED);
2287 isc_result = isc_entropy_create(mctx, &ectx);
2288 if (isc_result != ISC_R_SUCCESS) {
2289 t_info("isc_entropy_create failed %s\n",
2290 isc_result_totext(isc_result));
2291 isc_mem_destroy(&mctx);
2292 return(T_UNRESOLVED);
2295 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
2296 if (isc_result != ISC_R_SUCCESS) {
2297 t_info("isc_hash_create failed %s\n",
2298 isc_result_totext(isc_result));
2299 isc_entropy_detach(&ectx);
2300 isc_mem_destroy(&mctx);
2301 return(T_UNRESOLVED);
2304 db = NULL;
2305 dns_result = t_create(db_type, origin, class, "cache", mctx, &db);
2306 if (dns_result != ISC_R_SUCCESS) {
2307 isc_hash_destroy();
2308 isc_entropy_detach(&ectx);
2309 isc_mem_destroy(&mctx);
2310 return(T_UNRESOLVED);
2313 dns_result = dns_db_load(db, filename);
2314 if (dns_result != ISC_R_SUCCESS) {
2315 t_info("dns_db_load returned %s\n",
2316 dns_result_totext(dns_result));
2317 dns_db_detach(&db);
2318 isc_hash_destroy();
2319 isc_entropy_detach(&ectx);
2320 isc_mem_destroy(&mctx);
2321 return(T_UNRESOLVED);
2324 nodep = NULL;
2327 * Check that the node is there.
2329 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_existingname),
2330 ISC_FALSE, &nodep);
2331 if (dns_result != ISC_R_SUCCESS) {
2332 t_info("unable to find %s\n", existing_name);
2333 dns_db_detach(&db);
2334 isc_hash_destroy();
2335 isc_entropy_detach(&ectx);
2336 isc_mem_destroy(&mctx);
2337 return(T_UNRESOLVED);
2341 * Expire it.
2343 if (node_expire_time != 0)
2344 node_expire_time += now;
2346 dns_result = dns_db_expirenode(db, nodep, node_expire_time);
2347 if (dns_result != ISC_R_SUCCESS) {
2348 t_info("dns_db_expirenode failed %s\n",
2349 dns_result_totext(dns_result));
2350 dns_db_detachnode(db, &nodep);
2351 dns_db_detach(&db);
2352 isc_hash_destroy();
2353 isc_entropy_detach(&ectx);
2354 isc_mem_destroy(&mctx);
2355 return(T_FAIL);
2358 dns_fixedname_init(&dns_foundname);
2359 dns_rdataset_init(&rdataset);
2360 dns_db_detachnode(db, &nodep);
2361 nodep = NULL;
2363 if (find_expire_time != 0)
2364 find_expire_time += now;
2366 dns_result = dns_db_find(db,
2367 dns_fixedname_name(&dns_existingname),
2368 NULL,
2369 dns_rdatatype_any,
2371 find_expire_time,
2372 &nodep,
2373 dns_fixedname_name(&dns_foundname),
2374 &rdataset, NULL);
2376 if (dns_result == exp_result) {
2377 result = T_PASS;
2378 } else {
2379 t_info("dns_db_find %s returned %s\n", existing_name,
2380 dns_result_totext(dns_result));
2381 result = T_FAIL;
2384 if ((dns_result != ISC_R_NOTFOUND) &&
2385 (dns_result != DNS_R_NXDOMAIN) &&
2386 (dns_result != DNS_R_NXRRSET)) {
2389 * Don't need to disassociate the rdataset because
2390 * we're searching with dns_rdatatype_any.
2392 dns_db_detachnode(db, &nodep);
2396 dns_db_detach(&db);
2397 isc_hash_destroy();
2398 isc_entropy_detach(&ectx);
2399 isc_mem_destroy(&mctx);
2401 return(result);
2404 static void
2405 t12(void) {
2406 int result;
2408 t_assert("dns_db_expirenode", 12, T_REQUIRED, "%s", a12);
2409 result = t_eval("dns_db_expirenode_data", t_dns_db_expirenode, 8);
2410 t_result(result);
2413 static const char *a13 =
2414 "If the node name exists, then a call to "
2415 "dns_db_findnode(db, name, ISC_FALSE, nodep) initializes nodep "
2416 "to point to the node and returns ISC_R_SUCCESS, otherwise "
2417 "it returns ISC_R_NOTFOUND.";
2419 static int
2420 t_dns_db_findnode_1(char **av) {
2421 char *filename;
2422 char *db_type;
2423 char *origin;
2424 char *class;
2425 char *model;
2426 char *find_name;
2427 char *find_type;
2428 char *expected_result;
2430 int result;
2431 int len;
2432 dns_db_t *db;
2433 isc_result_t dns_result;
2434 isc_result_t isc_result;
2435 isc_mem_t *mctx;
2436 isc_entropy_t *ectx;
2437 dns_dbnode_t *nodep;
2438 isc_buffer_t name_buffer;
2439 dns_rdataset_t rdataset;
2440 dns_rdatatype_t rdatatype;
2441 isc_textregion_t textregion;
2442 dns_fixedname_t dns_name;
2443 dns_dbversion_t *cversionp;
2444 isc_result_t exp_result;
2446 filename = T_ARG(0);
2447 db_type = T_ARG(1);
2448 origin = T_ARG(2);
2449 class = T_ARG(3);
2450 model = T_ARG(4);
2451 find_name = T_ARG(5);
2452 find_type = T_ARG(6);
2453 expected_result = T_ARG(7);
2455 db = NULL;
2456 mctx = NULL;
2457 ectx = NULL;
2459 t_info("testing using file %s and name %s\n", filename, find_name);
2461 exp_result = t_dns_result_fromtext(expected_result);
2463 textregion.base = find_type;
2464 textregion.length = strlen(find_type);
2465 dns_result = dns_rdatatype_fromtext(&rdatatype, &textregion);
2466 if (dns_result != ISC_R_SUCCESS) {
2467 t_info("dns_rdatatype_fromtext %s failed %s\n",
2468 find_type,
2469 dns_result_totext(dns_result));
2470 return(T_UNRESOLVED);
2473 isc_result = isc_mem_create(0, 0, &mctx);
2474 if (isc_result != ISC_R_SUCCESS) {
2475 t_info("isc_mem_create failed %s\n",
2476 isc_result_totext(isc_result));
2477 return(T_UNRESOLVED);
2480 isc_result = isc_entropy_create(mctx, &ectx);
2481 if (isc_result != ISC_R_SUCCESS) {
2482 t_info("isc_entropy_create failed %s\n",
2483 isc_result_totext(isc_result));
2484 isc_mem_destroy(&mctx);
2485 return(T_UNRESOLVED);
2488 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
2489 if (isc_result != ISC_R_SUCCESS) {
2490 t_info("isc_hash_create failed %s\n",
2491 isc_result_totext(isc_result));
2492 isc_mem_destroy(&mctx);
2493 return(T_UNRESOLVED);
2496 dns_result = t_create(db_type, origin, class, model, mctx, &db);
2497 if (dns_result != ISC_R_SUCCESS) {
2498 isc_mem_destroy(&mctx);
2499 return(T_UNRESOLVED);
2502 dns_result = dns_db_load(db, filename);
2503 if (dns_result != ISC_R_SUCCESS) {
2504 t_info("dns_db_load returned %s\n",
2505 dns_result_totext(dns_result));
2506 dns_db_detach(&db);
2507 isc_mem_destroy(&mctx);
2508 return(T_UNRESOLVED);
2511 nodep = NULL;
2512 dns_fixedname_init(&dns_name);
2514 len = strlen(find_name);
2515 isc_buffer_init(&name_buffer, find_name, len);
2516 isc_buffer_add(&name_buffer, len);
2517 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_name),
2518 &name_buffer, NULL, 0, NULL);
2519 if (dns_result != ISC_R_SUCCESS) {
2520 t_info("dns_name_fromtext failed %s\n",
2521 dns_result_totext(dns_result));
2522 dns_db_detach(&db);
2523 isc_mem_destroy(&mctx);
2524 return(T_UNRESOLVED);
2527 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_name),
2528 ISC_FALSE, &nodep);
2529 if (dns_result != exp_result) {
2530 t_info("dns_db_findnode failed %s\n",
2531 dns_result_totext(dns_result));
2532 if (dns_result == ISC_R_SUCCESS)
2533 dns_db_detachnode(db, &nodep);
2534 dns_db_detach(&db);
2535 isc_mem_destroy(&mctx);
2536 return(T_FAIL);
2540 * if we're expecting the find to succeed and it did,
2541 * check that the node has been initialized
2542 * by checking for the specified type of rdata
2543 * and expecting the search to succeed
2546 if (dns_result == ISC_R_SUCCESS) {
2547 cversionp = NULL;
2548 dns_db_currentversion(db, &cversionp);
2549 dns_rdataset_init(&rdataset);
2551 dns_result = dns_db_findrdataset(db, nodep, cversionp,
2552 rdatatype, 0,
2553 0, &rdataset, NULL);
2554 if (dns_result == ISC_R_SUCCESS) {
2555 dns_rdataset_disassociate(&rdataset);
2556 result = T_PASS;
2557 } else {
2558 t_info("dns_db_findrdataset failed %s\n",
2559 dns_result_totext(dns_result));
2560 result = T_FAIL;
2562 dns_db_closeversion(db, &cversionp, ISC_FALSE);
2563 dns_db_detachnode(db, &nodep);
2564 } else {
2565 result = T_PASS;
2568 dns_db_detach(&db);
2569 isc_hash_destroy();
2570 isc_entropy_detach(&ectx);
2571 isc_mem_destroy(&mctx);
2573 return(result);
2576 static void
2577 t13(void) {
2578 int result;
2580 t_assert("dns_db_findnode", 13, T_REQUIRED, "%s", a13);
2581 result = t_eval("dns_db_findnode_1_data", t_dns_db_findnode_1, 8);
2582 t_result(result);
2585 static const char *a14 =
2586 "If the node name does not exist and create is ISC_TRUE, "
2587 "then a call to dns_db_findnode(db, name, create, nodep) "
2588 "creates the node, initializes nodep to point to the node, "
2589 "and returns ISC_R_SUCCESS.";
2591 static int
2592 t_dns_db_findnode_2(char **av) {
2593 char *filename;
2594 char *db_type;
2595 char *origin;
2596 char *class;
2597 char *model;
2598 char *newname;
2600 int nfails;
2601 int result;
2602 int len;
2603 dns_db_t *db;
2604 isc_result_t dns_result;
2605 isc_result_t isc_result;
2606 isc_mem_t *mctx;
2607 isc_entropy_t *ectx;
2608 dns_dbnode_t *nodep;
2609 dns_dbnode_t *newnodep;
2610 isc_buffer_t name_buffer;
2611 dns_rdataset_t rdataset;
2612 dns_fixedname_t dns_name;
2613 dns_fixedname_t dns_foundname;
2614 dns_dbversion_t *cversionp;
2616 filename = T_ARG(0);
2617 db_type = T_ARG(1);
2618 origin = T_ARG(2);
2619 class = T_ARG(3);
2620 model = T_ARG(4);
2621 newname = T_ARG(5);
2623 db = NULL;
2624 mctx = NULL;
2625 ectx = NULL;
2626 nfails = 0;
2628 t_info("testing using file %s and name %s\n", filename, newname);
2630 isc_result = isc_mem_create(0, 0, &mctx);
2631 if (isc_result != ISC_R_SUCCESS) {
2632 t_info("isc_mem_create failed %s\n",
2633 isc_result_totext(isc_result));
2634 return(T_UNRESOLVED);
2637 isc_result = isc_entropy_create(mctx, &ectx);
2638 if (isc_result != ISC_R_SUCCESS) {
2639 t_info("isc_entropy_create failed %s\n",
2640 isc_result_totext(isc_result));
2641 return(T_UNRESOLVED);
2644 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
2645 if (isc_result != ISC_R_SUCCESS) {
2646 t_info("isc_hash_create failed %s\n",
2647 isc_result_totext(isc_result));
2648 return(T_UNRESOLVED);
2651 dns_result = t_create(db_type, origin, class, model, mctx, &db);
2652 if (dns_result != ISC_R_SUCCESS) {
2653 isc_hash_destroy();
2654 isc_entropy_detach(&ectx);
2655 isc_mem_destroy(&mctx);
2656 return(T_UNRESOLVED);
2659 dns_result = dns_db_load(db, filename);
2660 if (dns_result != ISC_R_SUCCESS) {
2661 t_info("dns_db_load returned %s\n",
2662 dns_result_totext(dns_result));
2663 dns_db_detach(&db);
2664 isc_hash_destroy();
2665 isc_entropy_detach(&ectx);
2666 isc_mem_destroy(&mctx);
2667 return(T_UNRESOLVED);
2670 nodep = NULL;
2671 dns_fixedname_init(&dns_name);
2674 * Make sure the name isn't there
2676 len = strlen(newname);
2677 isc_buffer_init(&name_buffer, newname, len);
2678 isc_buffer_add(&name_buffer, len);
2679 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_name),
2680 &name_buffer, NULL, 0, NULL);
2681 if (dns_result != ISC_R_SUCCESS) {
2682 t_info("dns_name_fromtext returned %s\n",
2683 dns_result_totext(dns_result));
2684 dns_db_detach(&db);
2685 isc_hash_destroy();
2686 isc_entropy_detach(&ectx);
2687 isc_mem_destroy(&mctx);
2688 return(T_UNRESOLVED);
2691 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_name),
2692 ISC_FALSE, &nodep);
2693 if ((dns_result != ISC_R_NOTFOUND) &&
2694 (dns_result != DNS_R_NXDOMAIN) &&
2695 (dns_result != DNS_R_NXRRSET)) {
2696 t_info("dns_db_findnode %s\n",
2697 dns_result_totext(dns_result));
2698 dns_db_detachnode(db, &nodep);
2699 dns_db_detach(&db);
2700 isc_hash_destroy();
2701 isc_entropy_detach(&ectx);
2702 isc_mem_destroy(&mctx);
2703 return(T_UNRESOLVED);
2707 * Add it.
2709 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_name),
2710 ISC_TRUE, &nodep);
2711 if (dns_result != ISC_R_SUCCESS) {
2712 t_info("dns_db_findnode %s\n",
2713 dns_result_totext(dns_result));
2714 dns_db_detach(&db);
2715 isc_hash_destroy();
2716 isc_entropy_detach(&ectx);
2717 isc_mem_destroy(&mctx);
2718 return(T_FAIL);
2722 * Check it.
2724 newnodep = NULL;
2725 dns_rdataset_init(&rdataset);
2726 dns_fixedname_init(&dns_foundname);
2727 cversionp = NULL;
2728 dns_db_currentversion(db, &cversionp);
2731 * First try dns_db_find DNS_R_NXDOMAIN.
2733 dns_result = dns_db_find(db,
2734 dns_fixedname_name(&dns_name),
2735 cversionp,
2736 dns_rdatatype_any,
2739 &newnodep,
2740 dns_fixedname_name(&dns_foundname),
2741 &rdataset, NULL);
2742 if ((dns_result != ISC_R_NOTFOUND) && (dns_result != DNS_R_NXDOMAIN)) {
2743 dns_db_detachnode(db, &newnodep);
2746 if (dns_result != DNS_R_NXDOMAIN) {
2747 t_info("dns_db_find %s\n",
2748 dns_result_totext(dns_result));
2749 ++nfails;
2753 * Then try dns_db_findnode ISC_R_SUCCESS.
2755 dns_result = dns_db_findnode(db, dns_fixedname_name(&dns_name),
2756 ISC_FALSE, &newnodep);
2757 t_info("dns_db_findnode %s\n", dns_result_totext(dns_result));
2758 if (dns_result == ISC_R_SUCCESS) {
2759 dns_db_detachnode(db, &newnodep);
2760 } else {
2761 t_info("dns_db_findnode %s failed %s\n", newname,
2762 dns_result_totext(dns_result));
2763 ++nfails;
2767 dns_db_detachnode(db, &nodep);
2768 dns_db_closeversion(db, &cversionp, ISC_FALSE);
2769 dns_db_detach(&db);
2770 isc_hash_destroy();
2771 isc_entropy_detach(&ectx);
2772 isc_mem_destroy(&mctx);
2774 if (nfails == 0)
2775 result = T_PASS;
2776 else
2777 result = T_FAIL;
2779 return(result);
2782 static void
2783 t14(void) {
2784 int result;
2786 t_assert("dns_db_findnode", 14, T_REQUIRED, "%s", a14);
2787 result = t_eval("dns_db_findnode_2_data", t_dns_db_findnode_2, 6);
2788 t_result(result);
2791 static int
2792 t_dns_db_find_x(char **av) {
2793 char *dbfile;
2794 char *dbtype;
2795 char *dborigin;
2796 char *dbclass;
2797 char *dbmodel;
2798 char *findname;
2799 char *findtype;
2800 char *findopts;
2801 char *findtime;
2802 char *expected_result;
2804 int result;
2805 int len;
2806 int opts;
2807 dns_db_t *db;
2808 isc_result_t dns_result;
2809 isc_result_t isc_result;
2810 isc_stdtime_t ftime;
2811 isc_stdtime_t now;
2812 isc_result_t exp_result;
2813 isc_mem_t *mctx;
2814 isc_entropy_t *ectx;
2815 dns_dbnode_t *nodep;
2816 isc_textregion_t textregion;
2817 isc_buffer_t findname_buffer;
2818 dns_fixedname_t dns_findname;
2819 dns_fixedname_t dns_foundname;
2820 dns_rdataset_t rdataset;
2821 dns_rdatatype_t rdatatype;
2822 dns_dbversion_t *cversionp;
2824 dbfile = T_ARG(0);
2825 dbtype = T_ARG(1);
2826 dborigin = T_ARG(2);
2827 dbclass = T_ARG(3);
2828 dbmodel = T_ARG(4);
2829 findname = T_ARG(5);
2830 findtype = T_ARG(6);
2831 findopts = T_ARG(7);
2832 findtime = T_ARG(8);
2833 expected_result = T_ARG(9);
2834 db = NULL;
2835 mctx = NULL;
2836 ectx = NULL;
2837 opts = 0;
2839 t_info("testing using %s, name %s, type %s\n", dbfile, findname,
2840 findtype);
2842 isc_result = isc_mem_create(0, 0, &mctx);
2843 if (isc_result != ISC_R_SUCCESS) {
2844 t_info("isc_mem_create failed %s\n",
2845 isc_result_totext(isc_result));
2846 return(T_UNRESOLVED);
2849 isc_result = isc_entropy_create(mctx, &ectx);
2850 if (isc_result != ISC_R_SUCCESS) {
2851 t_info("isc_entropy_create failed %s\n",
2852 isc_result_totext(isc_result));
2853 isc_mem_destroy(&mctx);
2854 return(T_UNRESOLVED);
2857 isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
2858 if (isc_result != ISC_R_SUCCESS) {
2859 t_info("isc_hash_create failed %s\n",
2860 isc_result_totext(isc_result));
2861 isc_entropy_detach(&ectx);
2862 isc_mem_destroy(&mctx);
2863 return(T_UNRESOLVED);
2866 dns_result = t_create(dbtype, dborigin, dbclass, dbmodel, mctx, &db);
2867 if (dns_result != ISC_R_SUCCESS) {
2868 isc_hash_destroy();
2869 isc_entropy_detach(&ectx);
2870 isc_mem_destroy(&mctx);
2871 return(T_UNRESOLVED);
2874 dns_result = dns_db_load(db, dbfile);
2875 if (dns_result != ISC_R_SUCCESS) {
2876 t_info("dns_db_load returned %s\n",
2877 dns_result_totext(dns_result));
2878 dns_db_detach(&db);
2879 isc_hash_destroy();
2880 isc_entropy_detach(&ectx);
2881 isc_mem_destroy(&mctx);
2882 return(T_UNRESOLVED);
2885 exp_result = t_dns_result_fromtext(expected_result);
2887 dns_fixedname_init(&dns_findname);
2888 len = strlen(findname);
2889 isc_buffer_init(&findname_buffer, findname, len);
2890 isc_buffer_add(&findname_buffer, len);
2891 dns_result = dns_name_fromtext(dns_fixedname_name(&dns_findname),
2892 &findname_buffer, NULL, 0, NULL);
2893 if (dns_result != ISC_R_SUCCESS) {
2894 t_info("dns_name_fromtext failed %s\n",
2895 dns_result_totext(dns_result));
2896 dns_db_detach(&db);
2897 isc_hash_destroy();
2898 isc_entropy_detach(&ectx);
2899 isc_mem_destroy(&mctx);
2900 return(T_UNRESOLVED);
2903 textregion.base = findtype;
2904 textregion.length = strlen(findtype);
2905 dns_result = dns_rdatatype_fromtext(&rdatatype, &textregion);
2906 if (dns_result != ISC_R_SUCCESS) {
2907 t_info("dns_rdatatype_fromtext %s failed %s\n",
2908 findtype,
2909 dns_result_totext(dns_result));
2910 dns_db_detach(&db);
2911 isc_hash_destroy();
2912 isc_entropy_detach(&ectx);
2913 isc_mem_destroy(&mctx);
2914 return(T_UNRESOLVED);
2917 if (strstr(findopts, "DNS_DBFIND_GLUEOK"))
2918 opts |= DNS_DBFIND_GLUEOK;
2919 if (strstr(findopts, "DNS_DBFIND_VALIDATEGLUE"))
2920 opts |= DNS_DBFIND_VALIDATEGLUE;
2922 isc_stdtime_get(&now);
2924 ftime = strtol(findtime, NULL, 10);
2925 if (ftime != 0)
2926 ftime += now;
2928 cversionp = NULL;
2929 dns_fixedname_init(&dns_foundname);
2930 dns_rdataset_init(&rdataset);
2931 if (dns_db_iszone(db))
2932 dns_db_currentversion(db, &cversionp);
2933 nodep = NULL;
2935 dns_result = dns_db_find(db,
2936 dns_fixedname_name(&dns_findname),
2937 cversionp,
2938 rdatatype,
2939 opts,
2940 ftime,
2941 &nodep,
2942 dns_fixedname_name(&dns_foundname),
2943 &rdataset, NULL);
2945 if (dns_result != exp_result) {
2946 t_info("dns_db_find %s %s unexpectedly returned %s, "
2947 "expected %s\n",
2948 findname, findtype, dns_result_totext(dns_result),
2949 dns_result_totext(exp_result));
2950 result = T_FAIL;
2951 } else {
2952 result = T_PASS;
2955 if ((dns_result != ISC_R_NOTFOUND) && (dns_result != DNS_R_NXDOMAIN)) {
2957 if ((dns_result != DNS_R_NXRRSET) &&
2958 (dns_result != DNS_R_ZONECUT))
2959 if (dns_rdataset_isassociated(&rdataset))
2960 dns_rdataset_disassociate(&rdataset);
2961 dns_db_detachnode(db, &nodep);
2964 if (dns_db_iszone(db))
2965 dns_db_closeversion(db, &cversionp, ISC_FALSE);
2966 dns_db_detach(&db);
2967 isc_hash_destroy();
2968 isc_entropy_detach(&ectx);
2969 isc_mem_destroy(&mctx);
2971 return(result);
2974 static const char *a15 =
2975 "A call to dns_db_find(db, name, version, type, options, now, ...) "
2976 "finds the best match for 'name' and 'type' in version 'version' "
2977 "of 'db'.";
2979 static void
2980 t15(void) {
2981 int result;
2983 t_assert("dns_db_find", 15, T_REQUIRED, "%s", a15);
2984 result = t_eval("dns_db_find_1_data", t_dns_db_find_x, 10);
2985 t_result(result);
2989 static const char *a16 =
2990 "When the desired node and type were found, but are glue, "
2991 "and the DNS_DBFIND_GLUEOK option is set, a call to "
2992 "dns_db_find(db, name, version, type, options, now, ...) "
2993 "returns DNS_R_GLUE.";
2995 static void
2996 t16(void) {
2997 int result;
2999 t_assert("dns_db_find", 16, T_REQUIRED, "%s", a16);
3000 result = t_eval("dns_db_find_2_data", t_dns_db_find_x, 10);
3001 t_result(result);
3004 static const char *a17 =
3005 "A call to dns_db_find() returns DNS_R_DELEGATION when the data "
3006 "requested is beneath a zone cut.";
3008 static void
3009 t17(void) {
3010 int result;
3012 t_assert("dns_db_find", 17, T_REQUIRED, "%s", a17);
3013 result = t_eval("dns_db_find_3_data", t_dns_db_find_x, 10);
3014 t_result(result);
3017 static const char *a18 =
3018 "A call to dns_db_find() returns DNS_R_DELEGATION when type is "
3019 "dns_rdatatype_any and the desired node is a zone cut.";
3021 static void
3022 t18(void) {
3023 int result;
3025 t_assert("dns_db_find", 18, T_REQUIRED, "%s", a18);
3026 result = t_eval("dns_db_find_4_data", t_dns_db_find_x, 10);
3027 t_result(result);
3030 static const char *a19 =
3031 "A call to dns_db_find() returns DNS_R_DNAME when the data "
3032 "requested is beneath a DNAME.";
3034 static void
3035 t19(void) {
3036 int result;
3038 t_assert("dns_db_find", 19, T_REQUIRED, "%s", a19);
3039 result = t_eval("dns_db_find_5_data", t_dns_db_find_x, 10);
3040 t_result(result);
3043 static const char *a20 =
3044 "A call to dns_db_find() returns DNS_R_CNAME when the requested "
3045 "rdataset was not found but there is a CNAME at the desired name.";
3047 static void
3048 t20(void) {
3049 int result;
3051 t_assert("dns_db_find", 20, T_REQUIRED, "%s", a20);
3052 result = t_eval("dns_db_find_6_data", t_dns_db_find_x, 10);
3053 t_result(result);
3056 static const char *a21 =
3057 "A call to dns_db_find() returns DNS_R_NXDOMAIN when name "
3058 "does not exist.";
3060 static void
3061 t21(void) {
3062 int result;
3064 t_assert("dns_db_find", 21, T_REQUIRED, "%s", a21);
3065 result = t_eval("dns_db_find_7_data", t_dns_db_find_x, 10);
3066 t_result(result);
3069 static const char *a22 =
3070 "A call to dns_db_find() returns DNS_R_NXRRSET when "
3071 "the desired name exists, but the desired type does not.";
3073 static void
3074 t22(void) {
3075 int result;
3077 t_assert("dns_db_find", 22, T_REQUIRED, "%s", a22);
3078 result = t_eval("dns_db_find_8_data", t_dns_db_find_x, 10);
3079 t_result(result);
3082 static const char *a23 =
3083 "When db is a cache database, a call to dns_db_find() "
3084 "returns ISC_R_NOTFOUND when the desired name does not exist, "
3085 "and no delegation could be found.";
3087 static void
3088 t23(void) {
3089 int result;
3091 t_assert("dns_db_find", 23, T_REQUIRED, "%s", a23);
3092 result = t_eval("dns_db_find_9_data", t_dns_db_find_x, 10);
3093 t_result(result);
3096 static const char *a24 =
3097 "When db is a cache database, an rdataset will be found only "
3098 "if at least one rdataset at the found node expires after 'now'.";
3100 static void
3101 t24(void) {
3102 int result;
3104 t_assert("dns_db_find", 24, T_REQUIRED, "%s", a24);
3105 result = t_eval("dns_db_find_10_data", t_dns_db_find_x, 10);
3106 t_result(result);
3109 static const char *a25 =
3110 "A call to dns_db_load(db, filename) returns DNS_R_NOTZONETOP "
3111 "when the zone data contains a SOA not at the zone apex.";
3113 static void
3114 t25(void) {
3115 int result;
3117 t_assert("dns_db_load", 25, T_REQUIRED, "%s", a25);
3118 result = t_eval("dns_db_load_soa_not_top", t_dns_db_load, 9);
3119 t_result(result);
3122 testspec_t T_testlist[] = {
3123 { (PFV) t1, "dns_db_load" },
3124 { (PFV) t2, "dns_db_iscache" },
3125 { (PFV) t3, "dns_db_iscache" },
3126 { (PFV) t4, "dns_db_iszone" },
3127 { (PFV) t5, "dns_db_iszone" },
3128 { (PFV) t6, "dns_db_origin" },
3129 { (PFV) t7, "dns_db_class" },
3130 { (PFV) t8, "dns_db_currentversion" },
3131 { (PFV) t9, "dns_db_newversion" },
3132 { (PFV) t10, "dns_db_closeversion" },
3133 { (PFV) t11, "dns_db_closeversion" },
3134 { (PFV) t12, "dns_db_expirenode" },
3135 { (PFV) t13, "dns_db_findnode" },
3136 { (PFV) t14, "dns_db_findnode" },
3137 { (PFV) t15, "dns_db_find" },
3138 { (PFV) t16, "dns_db_find" },
3139 { (PFV) t17, "dns_db_find" },
3140 { (PFV) t18, "dns_db_find" },
3141 { (PFV) t19, "dns_db_find" },
3142 { (PFV) t20, "dns_db_find" },
3143 { (PFV) t21, "dns_db_find" },
3144 { (PFV) t22, "dns_db_find" },
3145 { (PFV) t23, "dns_db_find" },
3146 { (PFV) t24, "dns_db_find" },
3147 { (PFV) t25, "dns_db_load" },
3148 { (PFV) 0, NULL }
3151 #ifdef WIN32
3153 main(int argc, char **argv) {
3154 t_settests(T_testlist);
3155 return (t_main(argc, argv));
3157 #endif