Debian Codegraph @

tests/dns/qp_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #include <isc/random.h>
27 #include <isc/refcount.h>
28 #include <isc/result.h>
29 #include <isc/string.h>
30 #include <isc/urcu.h>
31 #include <isc/util.h>
32
33 #include <dns/name.h>
34 #include <dns/qp.h>
35
36 #include "qp_p.h"
37
38 #include <tests/dns.h>
39 #include <tests/qp.h>
40
41 bool verbose = false;
42
43 ISC_RUN_TEST_IMPL(qpkey_name) {
44 struct {
45 const char *namestr;
46 uint8_t key[512];
47 size_t len;
48 } testcases[] = {
49 {
50 .namestr = "",
51 .key = { 0x02 },
52 .len = 0,
53 },
54 {
55 .namestr = ".",
56 .key = { 0x02, 0x02 },
57 .len = 1,
58 },
59 {
60 .namestr = "\\000",
61 .key = { 0x03, 0x03, 0x02 },
62 .len = 3,
63 },
64 {
65 .namestr = "\\000\\009",
66 .key = { 0x03, 0x03, 0x03, 0x0c, 0x02 },
67 .len = 5,
68 },
69 {
70 .namestr = "com",
71 .key = { 0x16, 0x22, 0x20, 0x02 },
72 .len = 4,
73 },
74 {
75 .namestr = "com.",
76 .key = { 0x02, 0x16, 0x22, 0x20, 0x02 },
77 .len = 5,
78 },
79 {
80 .namestr = "example.com.",
81 .key = { 0x02, 0x16, 0x22, 0x20, 0x02, 0x18, 0x2b, 0x14,
82 0x20, 0x23, 0x1f, 0x18, 0x02 },
83 .len = 13,
84 },
85 {
86 .namestr = "example.com",
87 .key = { 0x16, 0x22, 0x20, 0x02, 0x18, 0x2b, 0x14, 0x20,
88 0x23, 0x1f, 0x18, 0x02 },
89 .len = 12,
90 },
91 {
92 .namestr = "EXAMPLE.COM",
93 .key = { 0x16, 0x22, 0x20, 0x02, 0x18, 0x2b, 0x14, 0x20,
94 0x23, 0x1f, 0x18, 0x02 },
95 .len = 12,
96 },
97 };
98
99 for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) {
100 size_t len;
101 dns_qpkey_t key;
102 dns_fixedname_t fn1, fn2;
103 dns_name_t *in = NULL, *out = NULL;
104 char namebuf[DNS_NAME_FORMATSIZE];
105
106 in = dns_fixedname_initname(&fn1);
107 if (testcases[i].len != 0) {
108 dns_test_namefromstring(testcases[i].namestr, &fn1);
109 }
110 len = dns_qpkey_fromname(key, in);
111 if (verbose) {
112 qp_test_printkey(key, len);
113 }
114
115 assert_int_equal(testcases[i].len, len);
116 assert_memory_equal(testcases[i].key, key, len);
117 /* also check key correctness for empty name */
118 if (len == 0) {
119 assert_int_equal(testcases[i].key[0], ((char *)key)[0]);
120 }
121
122 out = dns_fixedname_initname(&fn2);
123 dns_qpkey_toname(key, len, out);
124 assert_true(dns_name_equal(in, out));
125 /* check that 'out' is properly reset by dns_qpkey_toname */
126 dns_qpkey_toname(key, len, out);
127 dns_name_format(out, namebuf, sizeof(namebuf));
128 }
129 }
130
131 ISC_RUN_TEST_IMPL(qpkey_sort) {
132 struct {
133 const char *namestr;
134 dns_name_t *name;
135 dns_fixedname_t fixed;
136 size_t len;
137 dns_qpkey_t key;
138 } testcases[] = {
139 { .namestr = "." },
140 { .namestr = "\\000." },
141 { .namestr = "\\000.\\000." },
142 { .namestr = "\\000\\009." },
143 { .namestr = "\\007." },
144 { .namestr = "example.com." },
145 { .namestr = "EXAMPLE.COM." },
146 { .namestr = "www.example.com." },
147 { .namestr = "exam.com." },
148 { .namestr = "exams.com." },
149 { .namestr = "exam\\000.com." },
150 };
151
152 for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) {
153 dns_test_namefromstring(testcases[i].namestr,
154 &testcases[i].fixed);
155 testcases[i].name = dns_fixedname_name(&testcases[i].fixed);
156 testcases[i].len = dns_qpkey_fromname(testcases[i].key,
157 testcases[i].name);
158 }
159
160 for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) {
161 for (size_t j = 0; j < ARRAY_SIZE(testcases); j++) {
162 int namecmp = dns_name_compare(testcases[i].name,
163 testcases[j].name);
164 size_t len = ISC_MIN(testcases[i].len,
165 testcases[j].len);
166 /* include extra terminating NOBYTE */
167 int keycmp = memcmp(testcases[i].key, testcases[j].key,
168 len + 1);
169 assert_true((namecmp < 0) == (keycmp < 0));
170 assert_true((namecmp == 0) == (keycmp == 0));
171 assert_true((namecmp > 0) == (keycmp > 0));
172 }
173 }
174 }
175
176 #define ITER_ITEMS 100
177
178 static void
179 check_leaf(void *uctx, void *pval, uint32_t ival) {
180 uint32_t *items = uctx;
181 assert_in_range(ival, 1, ITER_ITEMS - 1);
182 assert_ptr_equal(items + ival, pval);
183 }
184
185 static size_t
186 qpiter_makekey(dns_qpkey_t key, void *uctx, void *pval, uint32_t ival) {
187 check_leaf(uctx, pval, ival);
188
189 char str[8];
190 snprintf(str, sizeof(str), "%03u", ival);
191
192 size_t i = 0;
193 while (str[i] != '\0') {
194 key[i] = str[i] - '0' + SHIFT_BITMAP;
195 i++;
196 }
197 key[i++] = SHIFT_NOBYTE;
198
199 return i;
200 }
201
202 static void
203 getname(void *uctx, char *buf, size_t size) {
204 strlcpy(buf, "test", size);
205 UNUSED(uctx);
206 UNUSED(size);
207 }
208
209 const dns_qpmethods_t qpiter_methods = {
210 check_leaf,
211 check_leaf,
212 qpiter_makekey,
213 getname,
214 };
215
216 ISC_RUN_TEST_IMPL(qpiter) {
217 dns_qp_t *qp = NULL;
218 uint32_t item[ITER_ITEMS] = { 0 };
219 uint32_t order[ITER_ITEMS] = { 0 };
220 dns_qpiter_t qpi;
221 int inserted, n;
222 uint32_t ival;
223 void *pval = NULL;
224 isc_result_t result;
225
226 dns_qp_create(mctx, &qpiter_methods, item, &qp);
227 for (size_t tests = 0; tests < 1234; tests++) {
228 ival = isc_random_uniform(ITER_ITEMS - 1) + 1;
229 pval = &item[ival];
230
231 item[ival] = ival;
232
233 inserted = n = 0;
234
235 /* randomly insert or remove */
236 dns_qpkey_t key;
237 size_t len = qpiter_makekey(key, item, pval, ival);
238 if (dns_qp_insert(qp, pval, ival) == ISC_R_EXISTS) {
239 void *pvald = NULL;
240 uint32_t ivald = 0;
241 dns_qp_deletekey(qp, key, len, &pvald, &ivald);
242 assert_ptr_equal(pval, pvald);
243 assert_int_equal(ival, ivald);
244 item[ival] = 0;
245 }
246
247 /* check that we see only valid items in the correct order */
248 uint32_t prev = 0;
249 dns_qpiter_init(qp, &qpi);
250 while (dns_qpiter_next(&qpi, NULL, &pval, &ival) ==
251 ISC_R_SUCCESS)
252 {
253 assert_in_range(ival, prev + 1, ITER_ITEMS - 1);
254 assert_int_equal(ival, item[ival]);
255 assert_ptr_equal(pval, &item[ival]);
256 order[inserted++] = ival;
257 item[ival] = ~ival;
258 prev = ival;
259 }
260
261 /* ensure we saw every item */
262 for (ival = 0; ival < ITER_ITEMS; ival++) {
263 if (item[ival] != 0) {
264 assert_int_equal(item[ival], ~ival);
265 item[ival] = ival;
266 }
267 }
268
269 /* now iterate backward and check correctness */
270 n = inserted;
271 while (dns_qpiter_prev(&qpi, NULL, NULL, &ival) ==
272 ISC_R_SUCCESS)
273 {
274 --n;
275
276 assert_int_equal(ival, order[n]);
277
278 /* and check current iterator value as well */
279 result = dns_qpiter_current(&qpi, NULL, NULL, &ival);
280 assert_int_equal(result, ISC_R_SUCCESS);
281 assert_int_equal(ival, order[n]);
282 }
283
284 assert_int_equal(n, 0);
285
286 /* ...and forward again */
287 while (dns_qpiter_next(&qpi, NULL, NULL, &ival) ==
288 ISC_R_SUCCESS)
289 {
290 assert_int_equal(ival, order[n]);
291
292 /* and check current iterator value as well */
293 result = dns_qpiter_current(&qpi, NULL, NULL, &ival);
294 assert_int_equal(result, ISC_R_SUCCESS);
295 assert_int_equal(ival, order[n]);
296
297 n++;
298 }
299
300 assert_int_equal(n, inserted);
301
302 /*
303 * if there are enough items inserted, try going
304 * forward a few steps, then back to the start,
305 * to confirm we can change directions while iterating.
306 */
307 if (inserted > 3) {
308 assert_int_equal(
309 dns_qpiter_next(&qpi, NULL, NULL, &ival),
310 ISC_R_SUCCESS);
311 assert_int_equal(ival, order[0]);
312
313 assert_int_equal(
314 dns_qpiter_next(&qpi, NULL, NULL, &ival),
315 ISC_R_SUCCESS);
316 assert_int_equal(ival, order[1]);
317
318 assert_int_equal(
319 dns_qpiter_prev(&qpi, NULL, NULL, &ival),
320 ISC_R_SUCCESS);
321 assert_int_equal(ival, order[0]);
322
323 assert_int_equal(
324 dns_qpiter_next(&qpi, NULL, NULL, &ival),
325 ISC_R_SUCCESS);
326 assert_int_equal(ival, order[1]);
327
328 assert_int_equal(
329 dns_qpiter_prev(&qpi, NULL, NULL, &ival),
330 ISC_R_SUCCESS);
331 assert_int_equal(ival, order[0]);
332
333 assert_int_equal(
334 dns_qpiter_prev(&qpi, NULL, NULL, &ival),
335 ISC_R_NOMORE);
336 }
337 }
338
339 dns_qp_destroy(&qp);
340 }
341
342 static void
343 no_op(void *uctx, void *pval, uint32_t ival) {
344 UNUSED(uctx);
345 UNUSED(pval);
346 UNUSED(ival);
347 }
348
349 static size_t
350 qpkey_fromstring(dns_qpkey_t key, void *uctx, void *pval, uint32_t ival) {
351 dns_fixedname_t fixed;
352
353 UNUSED(uctx);
354 UNUSED(ival);
355 if (*(char *)pval == '\0') {
356 return 0;
357 }
358 dns_test_namefromstring(pval, &fixed);
359 return dns_qpkey_fromname(key, dns_fixedname_name(&fixed));
360 }
361
362 const dns_qpmethods_t string_methods = {
363 no_op,
364 no_op,
365 qpkey_fromstring,
366 getname,
367 };
368
369 struct check_partialmatch {
370 const char *query;
371 isc_result_t result;
372 const char *found;
373 };
374
375 static void
376 check_partialmatch(dns_qp_t *qp, struct check_partialmatch check[]) {
377 for (int i = 0; check[i].query != NULL; i++) {
378 isc_result_t result;
379 dns_fixedname_t fn1, fn2;
380 dns_name_t *name = dns_fixedname_initname(&fn1);
381 dns_name_t *foundname = dns_fixedname_initname(&fn2);
382 void *pval = NULL;
383
384 dns_test_namefromstring(check[i].query, &fn1);
385 result = dns_qp_lookup(qp, name, foundname, NULL, NULL, &pval,
386 NULL);
387
388 #if 0
389 fprintf(stderr, "%s %s (expected %s) "
390 "value \"%s\" (expected \"%s\")\n",
391 check[i].query,
392 isc_result_totext(result),
393 isc_result_totext(check[i].result), (char *)pval,
394 check[i].found);
395 #endif
396
397 assert_int_equal(result, check[i].result);
398 if (result == ISC_R_SUCCESS) {
399 assert_true(dns_name_equal(name, foundname));
400 } else if (result == DNS_R_PARTIALMATCH) {
401 /*
402 * there are cases where we may have passed a
403 * query name that was relative to the zone apex,
404 * and gotten back an absolute name from the
405 * partial match. it's also possible for an
406 * absolute query to get a partial match on a
407 * node that had an empty name. in these cases,
408 * sanity checking the relations between name
409 * and foundname can trigger an assertion, so
410 * let's just skip them.
411 */
412 if (dns_name_isabsolute(name) ==
413 dns_name_isabsolute(foundname))
414 {
415 assert_false(dns_name_equal(name, foundname));
416 assert_true(
417 dns_name_issubdomain(name, foundname));
418 }
419 }
420 if (check[i].found == NULL) {
421 assert_null(pval);
422 } else {
423 assert_string_equal(pval, check[i].found);
424 }
425 }
426 }
427
428 static void
429 insert_str(dns_qp_t *qp, const char *str) {
430 isc_result_t result;
431 uintptr_t pval = (uintptr_t)str;
432 INSIST((pval & 3) == 0);
433 result = dns_qp_insert(qp, (void *)pval, 0);
434 assert_int_equal(result, ISC_R_SUCCESS);
435 }
436
437 ISC_RUN_TEST_IMPL(partialmatch) {
438 isc_result_t result;
439 dns_qp_t *qp = NULL;
440 int i = 0;
441
442 dns_qp_create(mctx, &string_methods, NULL, &qp);
443
444 /*
445 * Fixed size strings [16] should ensure leaf-compatible alignment.
446 */
447 const char insert[][16] = {
448 "a.b.", "b.", "fo.bar.", "foo.bar.",
449 "fooo.bar.", "web.foo.bar.", ".", "",
450 };
451
452 /*
453 * omit the root node for now, otherwise we'll get "partial match"
454 * results when we want "not found".
455 */
456 while (insert[i][0] != '.') {
457 insert_str(qp, insert[i++]);
458 }
459
460 static struct check_partialmatch check1[] = {
461 { "a.b.", ISC_R_SUCCESS, "a.b." },
462 { "b.c.", ISC_R_NOTFOUND, NULL },
463 { "bar.", ISC_R_NOTFOUND, NULL },
464 { "f.bar.", ISC_R_NOTFOUND, NULL },
465 { "foo.bar.", ISC_R_SUCCESS, "foo.bar." },
466 { "foooo.bar.", ISC_R_NOTFOUND, NULL },
467 { "w.foo.bar.", DNS_R_PARTIALMATCH, "foo.bar." },
468 { "www.foo.bar.", DNS_R_PARTIALMATCH, "foo.bar." },
469 { "web.foo.bar.", ISC_R_SUCCESS, "web.foo.bar." },
470 { "webby.foo.bar.", DNS_R_PARTIALMATCH, "foo.bar." },
471 { "my.web.foo.bar.", DNS_R_PARTIALMATCH, "web.foo.bar." },
472 { "my.other.foo.bar.", DNS_R_PARTIALMATCH, "foo.bar." },
473 { NULL, 0, NULL },
474 };
475 check_partialmatch(qp, check1);
476
477 /* what if the trie contains the root? */
478 INSIST(insert[i][0] == '.');
479 insert_str(qp, insert[i++]);
480
481 static struct check_partialmatch check2[] = {
482 { "b.c.", DNS_R_PARTIALMATCH, "." },
483 { "bar.", DNS_R_PARTIALMATCH, "." },
484 { "foo.bar.", ISC_R_SUCCESS, "foo.bar." },
485 { "bar", ISC_R_NOTFOUND, NULL },
486 { NULL, 0, NULL },
487 };
488 check_partialmatch(qp, check2);
489
490 /*
491 * what if entries in the trie are relative to the zone apex
492 * and there's no root node?
493 */
494 dns_qpkey_t rootkey = { SHIFT_NOBYTE };
495 result = dns_qp_deletekey(qp, rootkey, 1, NULL, NULL);
496 assert_int_equal(result, ISC_R_SUCCESS);
497 check_partialmatch(qp, (struct check_partialmatch[]){
498 { "bar", ISC_R_NOTFOUND, NULL },
499 { "bar.", ISC_R_NOTFOUND, NULL },
500 { NULL, 0, NULL },
501 });
502
503 /* what if there's a root node with an empty key? */
504 INSIST(insert[i][0] == '\0');
505 insert_str(qp, insert[i++]);
506 check_partialmatch(qp, (struct check_partialmatch[]){
507 { "bar", DNS_R_PARTIALMATCH, "" },
508 { "bar.", DNS_R_PARTIALMATCH, "" },
509 { NULL, 0, NULL },
510 });
511
512 dns_qp_destroy(&qp);
513 }
514
515 struct check_qpchain {
516 const char *query;
517 isc_result_t result;
518 unsigned int length;
519 const char *names[10];
520 };
521
522 static void
523 check_qpchainiter(dns_qp_t *qp, struct check_qpchain check[],
524 dns_qpiter_t *iter) {
525 for (int i = 0; check[i].query != NULL; i++) {
526 isc_result_t result;
527 dns_fixedname_t fn1;
528 dns_name_t *name = dns_fixedname_initname(&fn1);
529 dns_qpchain_t chain;
530
531 dns_qpchain_init(qp, &chain);
532 dns_test_namefromstring(check[i].query, &fn1);
533 result = dns_qp_lookup(qp, name, NULL, iter, &chain, NULL,
534 NULL);
535 #if 0
536 fprintf(stderr, "%s %s (expected %s), "
537 "len %d (expected %d)\n", check[i].query,
538 isc_result_totext(result),
539 isc_result_totext(check[i].result),
540 dns_qpchain_length(&chain), check[i].length);
541 #endif
542
543 assert_int_equal(result, check[i].result);
544 assert_int_equal(dns_qpchain_length(&chain), check[i].length);
545 for (unsigned int j = 0; j < check[i].length; j++) {
546 dns_fixedname_t fn2, fn3;
547 dns_name_t *expected = dns_fixedname_initname(&fn2);
548 dns_name_t *found = dns_fixedname_initname(&fn3);
549 dns_test_namefromstring(check[i].names[j], &fn2);
550 dns_qpchain_node(&chain, j, found, NULL, NULL);
551 #if 0
552 char nb[DNS_NAME_FORMATSIZE];
553 dns_name_format(found, nb, sizeof(nb));
554 fprintf(stderr, "got %s, expected %s\n", nb,
555 check[i].names[j]);
556 #endif
557 assert_true(dns_name_equal(found, expected));
558 }
559 }
560 }
561
562 static void
563 check_qpchain(dns_qp_t *qp, struct check_qpchain check[]) {
564 dns_qpiter_t iter;
565 dns_qpiter_init(qp, &iter);
566 check_qpchainiter(qp, check, NULL);
567 check_qpchainiter(qp, check, &iter);
568 }
569
570 ISC_RUN_TEST_IMPL(qpchain) {
571 dns_qp_t *qp = NULL;
572 const char insert[][16] = { ".", "a.", "b.",
573 "c.b.a.", "e.d.c.b.a.", "c.b.b.",
574 "c.d.", "a.b.c.d.", "a.b.c.d.e.",
575 "b.a.", "x.k.c.d.", "" };
576 int i = 0;
577
578 dns_qp_create(mctx, &string_methods, NULL, &qp);
579
580 while (insert[i][0] != '\0') {
581 insert_str(qp, insert[i++]);
582 }
583
584 static struct check_qpchain check1[] = {
585 { "b.", ISC_R_SUCCESS, 2, { ".", "b." } },
586 { "b.a.", ISC_R_SUCCESS, 3, { ".", "a.", "b.a." } },
587 { "c.", DNS_R_PARTIALMATCH, 1, { "." } },
588 { "e.d.c.b.a.",
589 ISC_R_SUCCESS,
590 5,
591 { ".", "a.", "b.a.", "c.b.a.", "e.d.c.b.a." } },
592 { "a.b.c.d.", ISC_R_SUCCESS, 3, { ".", "c.d.", "a.b.c.d." } },
593 { "b.c.d.", DNS_R_PARTIALMATCH, 2, { ".", "c.d." } },
594 { "z.x.k.c.d.",
595 DNS_R_PARTIALMATCH,
596 3,
597 { ".", "c.d.", "x.k.c.d." } },
598 { NULL, 0, 0, { NULL } },
599 };
600
601 check_qpchain(qp, check1);
602 dns_qp_destroy(&qp);
603
604 const char insert2[][16] = { "a.", "d.b.a.", "z.d.b.a.", "" };
605
606 i = 0;
607
608 dns_qp_create(mctx, &string_methods, NULL, &qp);
609
610 while (insert2[i][0] != '\0') {
611 insert_str(qp, insert2[i++]);
612 }
613
614 static struct check_qpchain check2[] = {
615 { "f.c.b.a.", DNS_R_PARTIALMATCH, 1, { "a." } },
616 { NULL, 0, 0, { NULL } },
617 };
618
619 check_qpchain(qp, check2);
620 dns_qp_destroy(&qp);
621 }
622
623 struct check_predecessors {
624 const char *query;
625 const char *predecessor;
626 isc_result_t result;
627 int remaining;
628 };
629
630 static void
631 check_predecessors_withchain(dns_qp_t *qp, struct check_predecessors check[],
632 dns_qpchain_t *chain) {
633 isc_result_t result;
634 dns_fixedname_t fn1, fn2;
635 dns_name_t *name = dns_fixedname_initname(&fn1);
636 dns_name_t *pred = dns_fixedname_initname(&fn2);
637 char *namestr = NULL;
638
639 for (int i = 0; check[i].query != NULL; i++) {
640 dns_qpiter_t it;
641
642 dns_test_namefromstring(check[i].query, &fn1);
643
644 /*
645 * normalize the expected predecessor name, in
646 * case it has escaped characters, so we can compare
647 * apples to apples.
648 */
649 dns_fixedname_t fn3;
650 dns_name_t *expred = dns_fixedname_initname(&fn3);
651 char *predstr = NULL;
652 dns_test_namefromstring(check[i].predecessor, &fn3);
653 result = dns_name_tostring(expred, &predstr, mctx);
654 assert_int_equal(result, ISC_R_SUCCESS);
655
656 result = dns_qp_lookup(qp, name, NULL, &it, chain, NULL, NULL);
657 #if 0
658 fprintf(stderr, "%s: expected %s got %s\n", check[i].query,
659 isc_result_totext(check[i].result),
660 isc_result_totext(result));
661 #endif
662 assert_int_equal(result, check[i].result);
663
664 if (result == ISC_R_SUCCESS) {
665 /*
666 * we found an exact match; iterate to find
667 * the predecessor.
668 */
669 result = dns_qpiter_prev(&it, pred, NULL, NULL);
670 if (result == ISC_R_NOMORE) {
671 result = dns_qpiter_prev(&it, pred, NULL, NULL);
672 }
673 } else {
674 /*
675 * we didn't find a match, so the iterator should
676 * already be pointed at the predecessor node.
677 */
678 result = dns_qpiter_current(&it, pred, NULL, NULL);
679 }
680 assert_int_equal(result, ISC_R_SUCCESS);
681
682 result = dns_name_tostring(pred, &namestr, mctx);
683 #if 0
684 fprintf(stderr, "... expected predecessor %s got %s\n",
685 predstr, namestr);
686 #endif
687 assert_int_equal(result, ISC_R_SUCCESS);
688 assert_string_equal(namestr, predstr);
689
690 #if 0
691 fprintf(stderr, "%d: remaining names after %s:\n", i, namestr);
692 #endif
693 isc_mem_free(mctx, namestr);
694 isc_mem_free(mctx, predstr);
695
696 int j = 0;
697 while (dns_qpiter_next(&it, name, NULL, NULL) == ISC_R_SUCCESS)
698 {
699 #if 0
700 result = dns_name_tostring(name, &namestr, mctx);
701 assert_int_equal(result, ISC_R_SUCCESS);
702 fprintf(stderr, "%s%s", j > 0 ? "->" : "", namestr);
703 isc_mem_free(mctx, namestr);
704 #endif
705 j++;
706 }
707 #if 0
708 fprintf(stderr, "\n...expected %d got %d\n",
709 check[i].remaining, j);
710 #endif
711 assert_int_equal(j, check[i].remaining);
712 }
713 }
714
715 static void
716 check_predecessors(dns_qp_t *qp, struct check_predecessors check[]) {
717 dns_qpchain_t chain;
718 dns_qpchain_init(qp, &chain);
719 check_predecessors_withchain(qp, check, NULL);
720 check_predecessors_withchain(qp, check, &chain);
721 }
722
723 ISC_RUN_TEST_IMPL(predecessors) {
724 dns_qp_t *qp = NULL;
725 const char insert[][16] = {
726 "a.", "b.", "c.b.a.", "e.d.c.b.a.",
727 "c.b.b.", "c.d.", "a.b.c.d.", "a.b.c.d.e.",
728 "b.a.", "x.k.c.d.", "moog.", "mooker.",
729 "mooko.", "moon.", "moops.", ""
730 };
731 int i = 0;
732
733 dns_qp_create(mctx, &string_methods, NULL, &qp);
734 while (insert[i][0] != '\0') {
735 insert_str(qp, insert[i++]);
736 }
737
738 /* first check: no root label in the database */
739 static struct check_predecessors check1[] = {
740 { ".", "moops.", ISC_R_NOTFOUND, 0 },
741 { "a.", "moops.", ISC_R_SUCCESS, 0 },
742 { "b.a.", "a.", ISC_R_SUCCESS, 14 },
743 { "b.", "e.d.c.b.a.", ISC_R_SUCCESS, 11 },
744 { "aaa.a.", "a.", DNS_R_PARTIALMATCH, 14 },
745 { "ddd.a.", "e.d.c.b.a.", DNS_R_PARTIALMATCH, 11 },
746 { "d.c.", "c.b.b.", ISC_R_NOTFOUND, 9 },
747 { "1.2.c.b.a.", "c.b.a.", DNS_R_PARTIALMATCH, 12 },
748 { "a.b.c.e.f.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
749 { "z.y.x.", "moops.", ISC_R_NOTFOUND, 0 },
750 { "w.c.d.", "x.k.c.d.", DNS_R_PARTIALMATCH, 6 },
751 { "z.z.z.z.k.c.d.", "x.k.c.d.", DNS_R_PARTIALMATCH, 6 },
752 { "w.k.c.d.", "a.b.c.d.", DNS_R_PARTIALMATCH, 7 },
753 { "d.a.", "e.d.c.b.a.", DNS_R_PARTIALMATCH, 11 },
754 { "0.b.c.d.e.", "x.k.c.d.", ISC_R_NOTFOUND, 6 },
755 { "b.d.", "c.b.b.", ISC_R_NOTFOUND, 9 },
756 { "mon.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
757 { "moor.", "moops.", ISC_R_NOTFOUND, 0 },
758 { "mopbop.", "moops.", ISC_R_NOTFOUND, 0 },
759 { "moppop.", "moops.", ISC_R_NOTFOUND, 0 },
760 { "mopps.", "moops.", ISC_R_NOTFOUND, 0 },
761 { "mopzop.", "moops.", ISC_R_NOTFOUND, 0 },
762 { "mop.", "moops.", ISC_R_NOTFOUND, 0 },
763 { "monbop.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
764 { "monpop.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
765 { "monps.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
766 { "monzop.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
767 { "mon.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
768 { "moop.", "moon.", ISC_R_NOTFOUND, 1 },
769 { "moopser.", "moops.", ISC_R_NOTFOUND, 0 },
770 { "monky.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
771 { "monkey.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
772 { "monker.", "a.b.c.d.e.", ISC_R_NOTFOUND, 5 },
773 { NULL, NULL, 0, 0 }
774 };
775
776 check_predecessors(qp, check1);
777
778 /* second check: add a root label and try again */
779 const char root[16] = ".";
780 insert_str(qp, root);
781
782 static struct check_predecessors check2[] = {
783 { ".", "moops.", ISC_R_SUCCESS, 0 },
784 { "a.", ".", ISC_R_SUCCESS, 15 },
785 { "b.a.", "a.", ISC_R_SUCCESS, 14 },
786 { "b.", "e.d.c.b.a.", ISC_R_SUCCESS, 11 },
787 { "aaa.a.", "a.", DNS_R_PARTIALMATCH, 14 },
788 { "ddd.a.", "e.d.c.b.a.", DNS_R_PARTIALMATCH, 11 },
789 { "d.c.", "c.b.b.", DNS_R_PARTIALMATCH, 9 },
790 { "1.2.c.b.a.", "c.b.a.", DNS_R_PARTIALMATCH, 12 },
791 { "a.b.c.e.f.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
792 { "z.y.x.", "moops.", DNS_R_PARTIALMATCH, 0 },
793 { "w.c.d.", "x.k.c.d.", DNS_R_PARTIALMATCH, 6 },
794 { "z.z.z.z.k.c.d.", "x.k.c.d.", DNS_R_PARTIALMATCH, 6 },
795 { "w.k.c.d.", "a.b.c.d.", DNS_R_PARTIALMATCH, 7 },
796 { "d.a.", "e.d.c.b.a.", DNS_R_PARTIALMATCH, 11 },
797 { "0.b.c.d.e.", "x.k.c.d.", DNS_R_PARTIALMATCH, 6 },
798 { "mon.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
799 { "moor.", "moops.", DNS_R_PARTIALMATCH, 0 },
800 { "mopbop.", "moops.", DNS_R_PARTIALMATCH, 0 },
801 { "moppop.", "moops.", DNS_R_PARTIALMATCH, 0 },
802 { "mopps.", "moops.", DNS_R_PARTIALMATCH, 0 },
803 { "mopzop.", "moops.", DNS_R_PARTIALMATCH, 0 },
804 { "mop.", "moops.", DNS_R_PARTIALMATCH, 0 },
805 { "monbop.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
806 { "monpop.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
807 { "monps.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
808 { "monzop.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
809 { "mon.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
810 { "moop.", "moon.", DNS_R_PARTIALMATCH, 1 },
811 { "moopser.", "moops.", DNS_R_PARTIALMATCH, 0 },
812 { "monky.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
813 { "monkey.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
814 { "monker.", "a.b.c.d.e.", DNS_R_PARTIALMATCH, 5 },
815 { NULL, NULL, 0, 0 }
816 };
817
818 check_predecessors(qp, check2);
819
820 dns_qp_destroy(&qp);
821 }
822
823 /*
824 * this is a regression test for an infinite loop that could
825 * previously occur in fix_iterator()
826 */
827 ISC_RUN_TEST_IMPL(fixiterator) {
828 dns_qp_t *qp = NULL;
829 const char insert1[][32] = { "dynamic.",
830 "a.dynamic.",
831 "aaaa.dynamic.",
832 "cdnskey.dynamic.",
833 "cds.dynamic.",
834 "cname.dynamic.",
835 "dname.dynamic.",
836 "dnskey.dynamic.",
837 "ds.dynamic.",
838 "mx.dynamic.",
839 "ns.dynamic.",
840 "nsec.dynamic.",
841 "private-cdnskey.dynamic.",
842 "private-dnskey.dynamic.",
843 "rrsig.dynamic.",
844 "txt.dynamic.",
845 "trailing.",
846 "" };
847 int i = 0;
848
849 dns_qp_create(mctx, &string_methods, NULL, &qp);
850 while (insert1[i][0] != '\0') {
851 insert_str(qp, insert1[i++]);
852 }
853
854 static struct check_predecessors check1[] = {
855 { "newtext.dynamic.", "mx.dynamic.", DNS_R_PARTIALMATCH, 7 },
856 { "nsd.dynamic.", "ns.dynamic.", DNS_R_PARTIALMATCH, 6 },
857 { "nsf.dynamic.", "nsec.dynamic.", DNS_R_PARTIALMATCH, 5 },
858 { "d.", "trailing.", ISC_R_NOTFOUND, 0 },
859 { "absent.", "trailing.", ISC_R_NOTFOUND, 0 },
860 { "nonexistent.", "txt.dynamic.", ISC_R_NOTFOUND, 1 },
861 { "wayback.", "trailing.", ISC_R_NOTFOUND, 0 },
862 { NULL, NULL, 0, 0 }
863 };
864
865 check_predecessors(qp, check1);
866 dns_qp_destroy(&qp);
867
868 const char insert2[][64] = { ".", "abb.", "abc.", "" };
869 i = 0;
870
871 dns_qp_create(mctx, &string_methods, NULL, &qp);
872 while (insert2[i][0] != '\0') {
873 insert_str(qp, insert2[i++]);
874 }
875
876 static struct check_predecessors check2[] = {
877 { "acb.", "abc.", DNS_R_PARTIALMATCH, 0 },
878 { "acc.", "abc.", DNS_R_PARTIALMATCH, 0 },
879 { "abbb.", "abb.", DNS_R_PARTIALMATCH, 1 },
880 { "aab.", ".", DNS_R_PARTIALMATCH, 2 },
881 { NULL, NULL, 0, 0 }
882 };
883
884 check_predecessors(qp, check2);
885 dns_qp_destroy(&qp);
886
887 const char insert3[][64] = { "example.",
888 "key-is-13779.example.",
889 "key-is-14779.example.",
890 "key-not-13779.example.",
891 "key-not-14779.example.",
892 "" };
893 i = 0;
894
895 dns_qp_create(mctx, &string_methods, NULL, &qp);
896 while (insert3[i][0] != '\0') {
897 insert_str(qp, insert3[i++]);
898 }
899
900 static struct check_predecessors check3[] = { { "key-is-21556.example.",
901 "key-is-14779.example.",
902 DNS_R_PARTIALMATCH, 2 },
903 { NULL, NULL, 0, 0 } };
904
905 check_predecessors(qp, check3);
906 dns_qp_destroy(&qp);
907
908 const char insert4[][64] = { ".", "\\000.", "\\000.\\000.",
909 "\\000\\009.", "" };
910 i = 0;
911
912 dns_qp_create(mctx, &string_methods, NULL, &qp);
913 while (insert4[i][0] != '\0') {
914 insert_str(qp, insert4[i++]);
915 }
916
917 static struct check_predecessors check4[] = {
918 { "\\007.", "\\000\\009.", DNS_R_PARTIALMATCH, 0 },
919 { "\\009.", "\\000\\009.", DNS_R_PARTIALMATCH, 0 },
920 { "\\045.", "\\000\\009.", DNS_R_PARTIALMATCH, 0 },
921 { "\\044.", "\\000\\009.", DNS_R_PARTIALMATCH, 0 },
922 { "\\000.", ".", ISC_R_SUCCESS, 3 },
923 { NULL, NULL, 0, 0 },
924 };
925
926 check_predecessors(qp, check4);
927 dns_qp_destroy(&qp);
928 }
929
930 ISC_TEST_LIST_START
931 ISC_TEST_ENTRY(qpkey_name)
932 ISC_TEST_ENTRY(qpkey_sort)
933 ISC_TEST_ENTRY(qpiter)
934 ISC_TEST_ENTRY(partialmatch)
935 ISC_TEST_ENTRY(qpchain)
936 ISC_TEST_ENTRY(predecessors)
937 ISC_TEST_ENTRY(fixiterator)
938 ISC_TEST_LIST_END
939
940 ISC_TEST_MAIN