Debian Codegraph @

tests/dns/rbt_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 <ctype.h>
15 #include <fcntl.h>
16 #include <inttypes.h>
17 #include <sched.h> /* IWYU pragma: keep */
18 #include <setjmp.h>
19 #include <stdarg.h>
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
26
27 #define UNIT_TESTING
28 #include <cmocka.h>
29
30 #include <isc/buffer.h>
31 #include <isc/file.h>
32 #include <isc/hash.h>
33 #include <isc/mem.h>
34 #include <isc/os.h>
35 #include <isc/random.h>
36 #include <isc/result.h>
37 #include <isc/stdio.h>
38 #include <isc/string.h>
39 #include <isc/thread.h>
40 #include <isc/time.h>
41 #include <isc/timer.h>
42 #include <isc/util.h>
43
44 #include <dns/compress.h>
45 #include <dns/fixedname.h>
46 #include <dns/log.h>
47 #include <dns/name.h>
48 #include <dns/rbt.h>
49
50 #include <dst/dst.h>
51
52 #include <tests/dns.h>
53
54 typedef struct {
55 dns_rbt_t *rbt;
56 dns_rbt_t *rbt_distances;
57 } test_context_t;
58
59 /* The initial structure of domain tree will be as follows:
60 *
61 * .
62 * |
63 * b
64 * / \
65 * a d.e.f
66 * / | \
67 * c | g.h
68 * | |
69 * w.y i
70 * / | \ \
71 * x | z k
72 * | |
73 * p j
74 * / \
75 * o q
76 */
77
78 /* The full absolute names of the nodes in the tree (the tree also
79 * contains "." which is not included in this list).
80 */
81 static const char *const domain_names[] = {
82 "c.", "b.", "a.", "x.d.e.f.",
83 "z.d.e.f.", "g.h.", "i.g.h.", "o.w.y.d.e.f.",
84 "j.z.d.e.f.", "p.w.y.d.e.f.", "q.w.y.d.e.f.", "k.g.h."
85 };
86
87 static const size_t domain_names_count =
88 (sizeof(domain_names) / sizeof(domain_names[0]));
89
90 /* These are set as the node data for the tree used in distances check
91 * (for the names in domain_names[] above).
92 */
93 static const int node_distances[] = { 3, 1, 2, 2, 2, 3, 1, 2, 1, 1, 2, 2 };
94
95 /*
96 * The domain order should be:
97 * ., a, b, c, d.e.f, x.d.e.f, w.y.d.e.f, o.w.y.d.e.f, p.w.y.d.e.f,
98 * q.w.y.d.e.f, z.d.e.f, j.z.d.e.f, g.h, i.g.h, k.g.h
99 * . (no data, can't be found)
100 * |
101 * b
102 * / \
103 * a d.e.f
104 * / | \
105 * c | g.h
106 * | |
107 * w.y i
108 * / | \ \
109 * x | z k
110 * | |
111 * p j
112 * / \
113 * o q
114 */
115
116 static const char *const ordered_names[] = {
117 "a.", "b.", "c.", "d.e.f.",
118 "x.d.e.f.", "w.y.d.e.f.", "o.w.y.d.e.f.", "p.w.y.d.e.f.",
119 "q.w.y.d.e.f.", "z.d.e.f.", "j.z.d.e.f.", "g.h.",
120 "i.g.h.", "k.g.h."
121 };
122
123 static const size_t ordered_names_count =
124 (sizeof(ordered_names) / sizeof(*ordered_names));
125
126 static void
127 delete_data(void *data, void *arg) {
128 UNUSED(arg);
129
130 isc_mem_put(mctx, data, sizeof(size_t));
131 }
132
133 static test_context_t *
134 test_context_setup(void) {
135 test_context_t *ctx;
136 isc_result_t result;
137 size_t i;
138
139 ctx = isc_mem_get(mctx, sizeof(*ctx));
140 assert_non_null(ctx);
141
142 ctx->rbt = NULL;
143 result = dns_rbt_create(mctx, delete_data, NULL, &ctx->rbt);
144 assert_int_equal(result, ISC_R_SUCCESS);
145
146 ctx->rbt_distances = NULL;
147 result = dns_rbt_create(mctx, delete_data, NULL, &ctx->rbt_distances);
148 assert_int_equal(result, ISC_R_SUCCESS);
149
150 for (i = 0; i < domain_names_count; i++) {
151 size_t *n;
152 dns_fixedname_t fname;
153 dns_name_t *name = NULL;
154 dns_rbtnode_t *node = NULL;
155
156 dns_test_namefromstring(domain_names[i], &fname);
157
158 name = dns_fixedname_name(&fname);
159
160 n = isc_mem_get(mctx, sizeof(size_t));
161 assert_non_null(n);
162 *n = i + 1;
163 result = dns_rbt_addnode(ctx->rbt, name, &node);
164 assert_int_equal(result, ISC_R_SUCCESS);
165 node->data = n;
166
167 node = NULL;
168 n = isc_mem_get(mctx, sizeof(size_t));
169 assert_non_null(n);
170 *n = node_distances[i];
171 result = dns_rbt_addnode(ctx->rbt_distances, name, &node);
172 node->data = n;
173 assert_int_equal(result, ISC_R_SUCCESS);
174 }
175
176 return ctx;
177 }
178
179 static void
180 test_context_teardown(test_context_t *ctx) {
181 dns_rbt_destroy(&ctx->rbt, 0);
182 dns_rbt_destroy(&ctx->rbt_distances, 0);
183
184 isc_mem_put(mctx, ctx, sizeof(*ctx));
185 }
186
187 /*
188 * Walk the tree and ensure that all the test nodes are present.
189 */
190 static void
191 check_test_data(dns_rbt_t *rbt) {
192 dns_fixedname_t fixed;
193 isc_result_t result;
194 dns_name_t *foundname;
195 size_t i;
196
197 foundname = dns_fixedname_initname(&fixed);
198
199 for (i = 0; i < domain_names_count; i++) {
200 dns_fixedname_t fname;
201 dns_rbtnode_t *node = NULL;
202 dns_name_t *name = NULL;
203 size_t *n = NULL;
204
205 dns_test_namefromstring(domain_names[i], &fname);
206
207 name = dns_fixedname_name(&fname);
208 n = NULL;
209 result = dns_rbt_findnode(rbt, name, foundname, &node, NULL, 0,
210 NULL, NULL);
211 assert_int_equal(result, ISC_R_SUCCESS);
212 n = node->data;
213 assert_int_equal(*n, i + 1);
214 }
215 }
216
217 /* Test the creation of an rbt */
218 ISC_RUN_TEST_IMPL(rbt_create) {
219 test_context_t *ctx;
220 bool tree_ok;
221
222 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
223
224 ctx = test_context_setup();
225
226 check_test_data(ctx->rbt);
227
228 tree_ok = dns__rbt_checkproperties(ctx->rbt);
229 assert_true(tree_ok);
230
231 test_context_teardown(ctx);
232 }
233
234 /* Test dns_rbt_nodecount() on a tree */
235 ISC_RUN_TEST_IMPL(rbt_nodecount) {
236 test_context_t *ctx;
237
238 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
239
240 ctx = test_context_setup();
241
242 assert_int_equal(15, dns_rbt_nodecount(ctx->rbt));
243
244 test_context_teardown(ctx);
245 }
246
247 /* Test dns_rbtnode_get_distance() on a tree */
248 ISC_RUN_TEST_IMPL(rbtnode_get_distance) {
249 isc_result_t result;
250 test_context_t *ctx;
251 const char *name_str = "a.";
252 dns_fixedname_t fname;
253 dns_name_t *name;
254 dns_rbtnode_t *node = NULL;
255 dns_rbtnodechain_t chain;
256
257 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
258
259 ctx = test_context_setup();
260
261 dns_test_namefromstring(name_str, &fname);
262 name = dns_fixedname_name(&fname);
263
264 dns_rbtnodechain_init(&chain);
265
266 result = dns_rbt_findnode(ctx->rbt_distances, name, NULL, &node, &chain,
267 0, NULL, NULL);
268 assert_int_equal(result, ISC_R_SUCCESS);
269
270 while (node != NULL) {
271 const size_t *distance = (const size_t *)node->data;
272 if (distance != NULL) {
273 assert_int_equal(*distance,
274 dns__rbtnode_getdistance(node));
275 }
276 result = dns_rbtnodechain_next(&chain, NULL, NULL);
277 if (result == ISC_R_NOMORE) {
278 break;
279 }
280 dns_rbtnodechain_current(&chain, NULL, NULL, &node);
281 }
282
283 assert_int_equal(result, ISC_R_NOMORE);
284
285 dns_rbtnodechain_invalidate(&chain);
286
287 test_context_teardown(ctx);
288 }
289
290 /*
291 * Test tree balance, inserting names in random order.
292 *
293 * This test checks an important performance-related property of
294 * the red-black tree, which is important for us: the longest
295 * path from a sub-tree's root to a node is no more than
296 * 2log(n). This check verifies that the tree is balanced.
297 */
298 ISC_RUN_TEST_IMPL(rbt_check_distance_random) {
299 dns_rbt_t *mytree = NULL;
300 const unsigned int log_num_nodes = 16;
301 isc_result_t result;
302 bool tree_ok;
303 int i;
304
305 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
306
307 result = dns_rbt_create(mctx, delete_data, NULL, &mytree);
308 assert_int_equal(result, ISC_R_SUCCESS);
309
310 /* Names are inserted in random order. */
311
312 /* Make a large 65536 node top-level domain tree, i.e., the
313 * following code inserts names such as:
314 *
315 * savoucnsrkrqzpkqypbygwoiliawpbmz.
316 * wkadamcbbpjtundbxcmuayuycposvngx.
317 * wzbpznemtooxdpjecdxynsfztvnuyfao.
318 * yueojmhyffslpvfmgyfwioxegfhepnqq.
319 */
320 for (i = 0; i < (1 << log_num_nodes); i++) {
321 size_t *n = NULL;
322 char namebuf[34];
323
324 n = isc_mem_get(mctx, sizeof(size_t));
325 assert_non_null(n);
326 *n = i + 1;
327
328 while (1) {
329 int j;
330 dns_fixedname_t fname;
331 dns_rbtnode_t *node = NULL;
332 dns_name_t *name = NULL;
333
334 for (j = 0; j < 32; j++) {
335 uint32_t v = isc_random_uniform(26);
336 namebuf[j] = 'a' + v;
337 }
338 namebuf[32] = '.';
339 namebuf[33] = 0;
340
341 dns_test_namefromstring(namebuf, &fname);
342 name = dns_fixedname_name(&fname);
343
344 result = dns_rbt_addnode(mytree, name, &node);
345 node->data = n;
346 if (result == ISC_R_SUCCESS) {
347 break;
348 }
349 }
350 }
351
352 /* 1 (root . node) + (1 << log_num_nodes) */
353 assert_int_equal(1U + (1U << log_num_nodes), dns_rbt_nodecount(mytree));
354
355 /* The distance from each node to its sub-tree root must be less
356 * than 2 * log(n).
357 */
358 assert_true((2U * log_num_nodes) >= dns__rbt_getheight(mytree));
359
360 /* Also check RB tree properties */
361 tree_ok = dns__rbt_checkproperties(mytree);
362 assert_true(tree_ok);
363
364 dns_rbt_destroy(&mytree, 0);
365 }
366
367 /*
368 * Test tree balance, inserting names in sorted order.
369 *
370 * This test checks an important performance-related property of
371 * the red-black tree, which is important for us: the longest
372 * path from a sub-tree's root to a node is no more than
373 * 2log(n). This check verifies that the tree is balanced.
374 */
375 ISC_RUN_TEST_IMPL(rbt_check_distance_ordered) {
376 dns_rbt_t *mytree = NULL;
377 const unsigned int log_num_nodes = 16;
378 isc_result_t result;
379 bool tree_ok;
380 int i;
381
382 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
383
384 result = dns_rbt_create(mctx, delete_data, NULL, &mytree);
385 assert_int_equal(result, ISC_R_SUCCESS);
386
387 /* Names are inserted in sorted order. */
388
389 /* Make a large 65536 node top-level domain tree, i.e., the
390 * following code inserts names such as:
391 *
392 * name00000000.
393 * name00000001.
394 * name00000002.
395 * name00000003.
396 */
397 for (i = 0; i < (1 << log_num_nodes); i++) {
398 size_t *n;
399 char namebuf[14];
400 dns_fixedname_t fname;
401 dns_name_t *name = NULL;
402 dns_rbtnode_t *node = NULL;
403
404 n = isc_mem_get(mctx, sizeof(size_t));
405 assert_non_null(n);
406 *n = i + 1;
407
408 snprintf(namebuf, sizeof(namebuf), "name%08x.", i);
409 dns_test_namefromstring(namebuf, &fname);
410 name = dns_fixedname_name(&fname);
411
412 result = dns_rbt_addnode(mytree, name, &node);
413 assert_int_equal(result, ISC_R_SUCCESS);
414 node->data = n;
415 }
416
417 /* 1 (root . node) + (1 << log_num_nodes) */
418 assert_int_equal(1U + (1U << log_num_nodes), dns_rbt_nodecount(mytree));
419
420 /* The distance from each node to its sub-tree root must be less
421 * than 2 * log(n).
422 */
423 assert_true((2U * log_num_nodes) >= dns__rbt_getheight(mytree));
424
425 /* Also check RB tree properties */
426 tree_ok = dns__rbt_checkproperties(mytree);
427 assert_true(tree_ok);
428
429 dns_rbt_destroy(&mytree, 0);
430 }
431
432 static isc_result_t
433 insert_helper(dns_rbt_t *rbt, const char *namestr, dns_rbtnode_t **node) {
434 dns_fixedname_t fname;
435 dns_name_t *name;
436
437 dns_test_namefromstring(namestr, &fname);
438 name = dns_fixedname_name(&fname);
439
440 return dns_rbt_addnode(rbt, name, node);
441 }
442
443 static bool
444 compare_labelsequences(dns_rbtnode_t *node, const char *labelstr) {
445 dns_name_t name;
446 isc_result_t result;
447 char *nodestr = NULL;
448 bool is_equal;
449
450 dns_name_init(&name, NULL);
451 dns_rbt_namefromnode(node, &name);
452
453 result = dns_name_tostring(&name, &nodestr, mctx);
454 assert_int_equal(result, ISC_R_SUCCESS);
455
456 is_equal = strcmp(labelstr, nodestr) == 0 ? true : false;
457
458 isc_mem_free(mctx, nodestr);
459
460 return is_equal;
461 }
462
463 /* Test insertion into a tree */
464 ISC_RUN_TEST_IMPL(rbt_insert) {
465 isc_result_t result;
466 test_context_t *ctx;
467 dns_rbtnode_t *node;
468
469 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
470
471 ctx = test_context_setup();
472
473 /* Check node count before beginning. */
474 assert_int_equal(15, dns_rbt_nodecount(ctx->rbt));
475
476 /* Try to insert a node that already exists. */
477 node = NULL;
478 result = insert_helper(ctx->rbt, "d.e.f.", &node);
479 assert_int_equal(result, ISC_R_EXISTS);
480
481 /* Node count must not have changed. */
482 assert_int_equal(15, dns_rbt_nodecount(ctx->rbt));
483
484 /* Try to insert a node that doesn't exist. */
485 node = NULL;
486 result = insert_helper(ctx->rbt, "0.", &node);
487 assert_int_equal(result, ISC_R_SUCCESS);
488 assert_true(compare_labelsequences(node, "0"));
489
490 /* Node count must have increased. */
491 assert_int_equal(16, dns_rbt_nodecount(ctx->rbt));
492
493 /* Another. */
494 node = NULL;
495 result = insert_helper(ctx->rbt, "example.com.", &node);
496 assert_int_equal(result, ISC_R_SUCCESS);
497 assert_non_null(node);
498 assert_null(node->data);
499
500 /* Node count must have increased. */
501 assert_int_equal(17, dns_rbt_nodecount(ctx->rbt));
502
503 /* Re-adding it should return EXISTS */
504 node = NULL;
505 result = insert_helper(ctx->rbt, "example.com.", &node);
506 assert_int_equal(result, ISC_R_EXISTS);
507
508 /* Node count must not have changed. */
509 assert_int_equal(17, dns_rbt_nodecount(ctx->rbt));
510
511 /* Fission the node d.e.f */
512 node = NULL;
513 result = insert_helper(ctx->rbt, "k.e.f.", &node);
514 assert_int_equal(result, ISC_R_SUCCESS);
515 assert_true(compare_labelsequences(node, "k"));
516
517 /* Node count must have incremented twice ("d.e.f" fissioned to
518 * "d" and "e.f", and the newly added "k").
519 */
520 assert_int_equal(19, dns_rbt_nodecount(ctx->rbt));
521
522 /* Fission the node "g.h" */
523 node = NULL;
524 result = insert_helper(ctx->rbt, "h.", &node);
525 assert_int_equal(result, ISC_R_SUCCESS);
526 assert_true(compare_labelsequences(node, "h"));
527
528 /* Node count must have incremented ("g.h" fissioned to "g" and
529 * "h").
530 */
531 assert_int_equal(20, dns_rbt_nodecount(ctx->rbt));
532
533 /* Add child domains */
534
535 node = NULL;
536 result = insert_helper(ctx->rbt, "m.p.w.y.d.e.f.", &node);
537 assert_int_equal(result, ISC_R_SUCCESS);
538 assert_true(compare_labelsequences(node, "m"));
539 assert_int_equal(21, dns_rbt_nodecount(ctx->rbt));
540
541 node = NULL;
542 result = insert_helper(ctx->rbt, "n.p.w.y.d.e.f.", &node);
543 assert_int_equal(result, ISC_R_SUCCESS);
544 assert_true(compare_labelsequences(node, "n"));
545 assert_int_equal(22, dns_rbt_nodecount(ctx->rbt));
546
547 node = NULL;
548 result = insert_helper(ctx->rbt, "l.a.", &node);
549 assert_int_equal(result, ISC_R_SUCCESS);
550 assert_true(compare_labelsequences(node, "l"));
551 assert_int_equal(23, dns_rbt_nodecount(ctx->rbt));
552
553 node = NULL;
554 result = insert_helper(ctx->rbt, "r.d.e.f.", &node);
555 assert_int_equal(result, ISC_R_SUCCESS);
556 node = NULL;
557 result = insert_helper(ctx->rbt, "s.d.e.f.", &node);
558 assert_int_equal(result, ISC_R_SUCCESS);
559 assert_int_equal(25, dns_rbt_nodecount(ctx->rbt));
560
561 node = NULL;
562 result = insert_helper(ctx->rbt, "h.w.y.d.e.f.", &node);
563 assert_int_equal(result, ISC_R_SUCCESS);
564
565 /* Add more nodes one by one to cover left and right rotation
566 * functions.
567 */
568 node = NULL;
569 result = insert_helper(ctx->rbt, "f.", &node);
570 assert_int_equal(result, ISC_R_SUCCESS);
571
572 node = NULL;
573 result = insert_helper(ctx->rbt, "m.", &node);
574 assert_int_equal(result, ISC_R_SUCCESS);
575
576 node = NULL;
577 result = insert_helper(ctx->rbt, "nm.", &node);
578 assert_int_equal(result, ISC_R_SUCCESS);
579
580 node = NULL;
581 result = insert_helper(ctx->rbt, "om.", &node);
582 assert_int_equal(result, ISC_R_SUCCESS);
583
584 node = NULL;
585 result = insert_helper(ctx->rbt, "k.", &node);
586 assert_int_equal(result, ISC_R_SUCCESS);
587
588 node = NULL;
589 result = insert_helper(ctx->rbt, "l.", &node);
590 assert_int_equal(result, ISC_R_SUCCESS);
591
592 node = NULL;
593 result = insert_helper(ctx->rbt, "fe.", &node);
594 assert_int_equal(result, ISC_R_SUCCESS);
595
596 node = NULL;
597 result = insert_helper(ctx->rbt, "ge.", &node);
598 assert_int_equal(result, ISC_R_SUCCESS);
599
600 node = NULL;
601 result = insert_helper(ctx->rbt, "i.", &node);
602 assert_int_equal(result, ISC_R_SUCCESS);
603
604 node = NULL;
605 result = insert_helper(ctx->rbt, "ae.", &node);
606 assert_int_equal(result, ISC_R_SUCCESS);
607
608 node = NULL;
609 result = insert_helper(ctx->rbt, "n.", &node);
610 assert_int_equal(result, ISC_R_SUCCESS);
611
612 test_context_teardown(ctx);
613 }
614
615 /*
616 * Test removal from a tree
617 *
618 * This testcase checks that after node removal, the binary-search tree is
619 * valid and all nodes that are supposed to exist are present in the
620 * correct order. It mainly tests DomainTree as a BST, and not particularly
621 * as a red-black tree. This test checks node deletion when upper nodes
622 * have data.
623 */
624 static isc_result_t
625 deletename(dns_rbt_t *mytree, const dns_name_t *name) {
626 isc_result_t result;
627 dns_rbtnode_t *node = NULL;
628
629 result = dns_rbt_findnode(mytree, name, NULL, &node, NULL, 0, NULL,
630 NULL);
631 if (result == ISC_R_SUCCESS) {
632 if (node->data != NULL) {
633 result = dns_rbt_deletenode(mytree, node, false);
634 } else {
635 result = ISC_R_NOTFOUND;
636 }
637 } else if (result == DNS_R_PARTIALMATCH) {
638 result = ISC_R_NOTFOUND;
639 }
640
641 return result;
642 }
643
644 ISC_RUN_TEST_IMPL(rbt_remove) {
645 isc_result_t result;
646 size_t j;
647
648 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
649
650 /*
651 * Delete single nodes and check if the rest of the nodes exist.
652 */
653 for (j = 0; j < ordered_names_count; j++) {
654 dns_rbt_t *mytree = NULL;
655 dns_rbtnode_t *node;
656 size_t i;
657 size_t *n;
658 bool tree_ok;
659 dns_rbtnodechain_t chain;
660 size_t start_node;
661
662 /* Create a tree. */
663 result = dns_rbt_create(mctx, delete_data, NULL, &mytree);
664 assert_int_equal(result, ISC_R_SUCCESS);
665
666 /* Insert test data into the tree. */
667 for (i = 0; i < domain_names_count; i++) {
668 node = NULL;
669 result = insert_helper(mytree, domain_names[i], &node);
670 assert_int_equal(result, ISC_R_SUCCESS);
671 }
672
673 /* Check that all names exist in order. */
674 for (i = 0; i < ordered_names_count; i++) {
675 dns_fixedname_t fname;
676 dns_name_t *name;
677
678 dns_test_namefromstring(ordered_names[i], &fname);
679
680 name = dns_fixedname_name(&fname);
681 node = NULL;
682 result = dns_rbt_findnode(mytree, name, NULL, &node,
683 NULL, DNS_RBTFIND_EMPTYDATA,
684 NULL, NULL);
685 assert_int_equal(result, ISC_R_SUCCESS);
686
687 /* Add node data */
688 assert_non_null(node);
689 assert_null(node->data);
690
691 n = isc_mem_get(mctx, sizeof(size_t));
692 assert_non_null(n);
693 *n = i;
694
695 node->data = n;
696 }
697
698 /* Now, delete the j'th node from the tree. */
699 {
700 dns_fixedname_t fname;
701 dns_name_t *name;
702
703 dns_test_namefromstring(ordered_names[j], &fname);
704
705 name = dns_fixedname_name(&fname);
706
707 result = deletename(mytree, name);
708 assert_int_equal(result, ISC_R_SUCCESS);
709 }
710
711 /* Check RB tree properties. */
712 tree_ok = dns__rbt_checkproperties(mytree);
713 assert_true(tree_ok);
714
715 dns_rbtnodechain_init(&chain);
716
717 /* Now, walk through nodes in order. */
718 if (j == 0) {
719 /*
720 * Node for ordered_names[0] was already deleted
721 * above. We start from node 1.
722 */
723 dns_fixedname_t fname;
724 dns_name_t *name;
725
726 dns_test_namefromstring(ordered_names[0], &fname);
727 name = dns_fixedname_name(&fname);
728 node = NULL;
729 result = dns_rbt_findnode(mytree, name, NULL, &node,
730 NULL, 0, NULL, NULL);
731 assert_int_equal(result, ISC_R_NOTFOUND);
732
733 dns_test_namefromstring(ordered_names[1], &fname);
734 name = dns_fixedname_name(&fname);
735 node = NULL;
736 result = dns_rbt_findnode(mytree, name, NULL, &node,
737 &chain, 0, NULL, NULL);
738 assert_int_equal(result, ISC_R_SUCCESS);
739 start_node = 1;
740 } else {
741 /* Start from node 0. */
742 dns_fixedname_t fname;
743 dns_name_t *name;
744
745 dns_test_namefromstring(ordered_names[0], &fname);
746 name = dns_fixedname_name(&fname);
747 node = NULL;
748 result = dns_rbt_findnode(mytree, name, NULL, &node,
749 &chain, 0, NULL, NULL);
750 assert_int_equal(result, ISC_R_SUCCESS);
751 start_node = 0;
752 }
753
754 /*
755 * node and chain have been set by the code above at
756 * this point.
757 */
758 for (i = start_node; i < ordered_names_count; i++) {
759 dns_fixedname_t fname_j, fname_i;
760 dns_name_t *name_j, *name_i;
761
762 dns_test_namefromstring(ordered_names[j], &fname_j);
763 name_j = dns_fixedname_name(&fname_j);
764 dns_test_namefromstring(ordered_names[i], &fname_i);
765 name_i = dns_fixedname_name(&fname_i);
766
767 if (dns_name_equal(name_i, name_j)) {
768 /*
769 * This may be true for the last node if
770 * we seek ahead in the loop using
771 * dns_rbtnodechain_next() below.
772 */
773 if (node == NULL) {
774 break;
775 }
776
777 /* All ordered nodes have data
778 * initially. If any node is empty, it
779 * means it was removed, but an empty
780 * node exists because it is a
781 * super-domain. Just skip it.
782 */
783 if (node->data == NULL) {
784 result = dns_rbtnodechain_next(
785 &chain, NULL, NULL);
786 if (result == ISC_R_NOMORE) {
787 node = NULL;
788 } else {
789 dns_rbtnodechain_current(
790 &chain, NULL, NULL,
791 &node);
792 }
793 }
794 continue;
795 }
796
797 assert_non_null(node);
798
799 n = (size_t *)node->data;
800 if (n != NULL) {
801 /* printf("n=%zu, i=%zu\n", *n, i); */
802 assert_int_equal(*n, i);
803 }
804
805 result = dns_rbtnodechain_next(&chain, NULL, NULL);
806 if (result == ISC_R_NOMORE) {
807 node = NULL;
808 } else {
809 dns_rbtnodechain_current(&chain, NULL, NULL,
810 &node);
811 }
812 }
813
814 /* We should have reached the end of the tree. */
815 assert_null(node);
816
817 dns_rbt_destroy(&mytree, 0);
818 }
819 }
820
821 static void
822 insert_nodes(dns_rbt_t *mytree, char **names, size_t *names_count,
823 uint32_t num_names) {
824 uint32_t i;
825 dns_rbtnode_t *node;
826
827 for (i = 0; i < num_names; i++) {
828 size_t *n;
829 char namebuf[34];
830
831 n = isc_mem_get(mctx, sizeof(size_t));
832 assert_non_null(n);
833
834 *n = i; /* Unused value */
835
836 while (1) {
837 int j;
838 dns_fixedname_t fname;
839 dns_name_t *name;
840 isc_result_t result;
841
842 for (j = 0; j < 32; j++) {
843 uint32_t v = isc_random_uniform(26);
844 namebuf[j] = 'a' + v;
845 }
846 namebuf[32] = '.';
847 namebuf[33] = 0;
848
849 dns_test_namefromstring(namebuf, &fname);
850 name = dns_fixedname_name(&fname);
851
852 node = NULL;
853 result = dns_rbt_addnode(mytree, name, &node);
854 if (result == ISC_R_SUCCESS) {
855 node->data = n;
856 names[*names_count] = isc_mem_strdup(mctx,
857 namebuf);
858 assert_non_null(names[*names_count]);
859 *names_count += 1;
860 break;
861 }
862 }
863 }
864 }
865
866 static void
867 remove_nodes(dns_rbt_t *mytree, char **names, size_t *names_count,
868 uint32_t num_names) {
869 uint32_t i;
870
871 UNUSED(mytree);
872
873 for (i = 0; i < num_names; i++) {
874 isc_result_t result;
875 dns_fixedname_t fname;
876 dns_name_t *name = NULL;
877 uint32_t num;
878
879 num = isc_random_uniform(*names_count);
880
881 dns_test_namefromstring(names[num], &fname);
882 name = dns_fixedname_name(&fname);
883
884 result = deletename(mytree, name);
885 assert_int_equal(result, ISC_R_SUCCESS);
886
887 isc_mem_free(mctx, names[num]);
888 if (*names_count > 0) {
889 names[num] = names[*names_count - 1];
890 names[*names_count - 1] = NULL;
891 *names_count -= 1;
892 }
893 }
894 }
895
896 static void
897 check_tree(dns_rbt_t *mytree, char **names, size_t names_count) {
898 bool tree_ok;
899
900 UNUSED(names);
901
902 assert_int_equal(names_count + 1, dns_rbt_nodecount(mytree));
903
904 /*
905 * The distance from each node to its sub-tree root must be less
906 * than 2 * log_2(1024).
907 */
908 assert_true((2 * 10) >= dns__rbt_getheight(mytree));
909
910 /* Also check RB tree properties */
911 tree_ok = dns__rbt_checkproperties(mytree);
912 assert_true(tree_ok);
913 }
914
915 /*
916 * Test insert and remove in a loop.
917 *
918 * What is the best way to test our red-black tree code? It is
919 * not a good method to test every case handled in the actual
920 * code itself. This is because our approach itself may be
921 * incorrect.
922 *
923 * We test our code at the interface level here by exercising the
924 * tree randomly multiple times, checking that red-black tree
925 * properties are valid, and all the nodes that are supposed to be
926 * in the tree exist and are in order.
927 *
928 * NOTE: These tests are run within a single tree level in the
929 * forest. The number of nodes in the tree level doesn't grow
930 * over 1024.
931 */
932 ISC_RUN_TEST_IMPL(rbt_insert_and_remove) {
933 isc_result_t result;
934 dns_rbt_t *mytree = NULL;
935 size_t *n = NULL;
936 dns_rbtnode_t *node = NULL;
937 char *names[1024];
938 size_t names_count;
939 int i;
940
941 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
942
943 result = dns_rbt_create(mctx, delete_data, NULL, &mytree);
944 assert_int_equal(result, ISC_R_SUCCESS);
945
946 n = isc_mem_get(mctx, sizeof(size_t));
947 assert_non_null(n);
948 result = dns_rbt_addnode(mytree, dns_rootname, &node);
949 assert_int_equal(result, ISC_R_SUCCESS);
950 node->data = n;
951
952 memset(names, 0, sizeof(names));
953 names_count = 0;
954
955 /* Repeat the insert/remove test some 4096 times */
956 for (i = 0; i < 4096; i++) {
957 uint32_t num_names;
958
959 if (names_count < 1024) {
960 num_names = isc_random_uniform(1024 - names_count);
961 num_names++;
962 } else {
963 num_names = 0;
964 }
965
966 insert_nodes(mytree, names, &names_count, num_names);
967 check_tree(mytree, names, names_count);
968
969 if (names_count > 0) {
970 num_names = isc_random_uniform(names_count);
971 num_names++;
972 } else {
973 num_names = 0;
974 }
975
976 remove_nodes(mytree, names, &names_count, num_names);
977 check_tree(mytree, names, names_count);
978 }
979
980 /* Remove the rest of the nodes */
981 remove_nodes(mytree, names, &names_count, names_count);
982 check_tree(mytree, names, names_count);
983
984 for (i = 0; i < 1024; i++) {
985 if (names[i] != NULL) {
986 isc_mem_free(mctx, names[i]);
987 }
988 }
989
990 result = deletename(mytree, dns_rootname);
991 assert_int_equal(result, ISC_R_SUCCESS);
992 assert_int_equal(dns_rbt_nodecount(mytree), 0);
993
994 dns_rbt_destroy(&mytree, 0);
995 }
996
997 /* Test nodechain */
998 ISC_RUN_TEST_IMPL(rbt_nodechain) {
999 isc_result_t result;
1000 test_context_t *ctx;
1001 dns_fixedname_t fname, found, expect;
1002 dns_name_t *name, *foundname, *expected;
1003 dns_rbtnode_t *node = NULL;
1004 dns_rbtnodechain_t chain;
1005
1006 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
1007
1008 ctx = test_context_setup();
1009
1010 dns_rbtnodechain_init(&chain);
1011
1012 dns_test_namefromstring("a.", &fname);
1013 name = dns_fixedname_name(&fname);
1014
1015 result = dns_rbt_findnode(ctx->rbt, name, NULL, &node, &chain, 0, NULL,
1016 NULL);
1017 assert_int_equal(result, ISC_R_SUCCESS);
1018
1019 foundname = dns_fixedname_initname(&found);
1020
1021 dns_test_namefromstring("a.", &expect);
1022 expected = dns_fixedname_name(&expect);
1023 UNUSED(expected);
1024
1025 result = dns_rbtnodechain_first(&chain, ctx->rbt, foundname, NULL);
1026 assert_int_equal(result, DNS_R_NEWORIGIN);
1027 assert_int_equal(dns_name_countlabels(foundname), 0);
1028
1029 result = dns_rbtnodechain_prev(&chain, NULL, NULL);
1030 assert_int_equal(result, ISC_R_NOMORE);
1031
1032 result = dns_rbtnodechain_next(&chain, NULL, NULL);
1033 assert_int_equal(result, ISC_R_SUCCESS);
1034
1035 result = dns_rbtnodechain_next(&chain, NULL, NULL);
1036 assert_int_equal(result, ISC_R_SUCCESS);
1037
1038 result = dns_rbtnodechain_last(&chain, ctx->rbt, NULL, NULL);
1039 assert_int_equal(result, DNS_R_NEWORIGIN);
1040
1041 result = dns_rbtnodechain_next(&chain, NULL, NULL);
1042 assert_int_equal(result, ISC_R_NOMORE);
1043
1044 result = dns_rbtnodechain_last(&chain, ctx->rbt, NULL, NULL);
1045 assert_int_equal(result, DNS_R_NEWORIGIN);
1046
1047 result = dns_rbtnodechain_prev(&chain, NULL, NULL);
1048 assert_int_equal(result, ISC_R_SUCCESS);
1049
1050 dns_rbtnodechain_invalidate(&chain);
1051
1052 test_context_teardown(ctx);
1053 }
1054
1055 /* Test name lengths */
1056 ISC_RUN_TEST_IMPL(rbtnode_namelen) {
1057 isc_result_t result;
1058 test_context_t *ctx = NULL;
1059 dns_rbtnode_t *node;
1060 unsigned int len;
1061
1062 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
1063
1064 ctx = test_context_setup();
1065
1066 node = NULL;
1067 result = insert_helper(ctx->rbt, ".", &node);
1068 len = dns__rbtnode_namelen(node);
1069 assert_int_equal(result, ISC_R_EXISTS);
1070 assert_int_equal(len, 1);
1071 node = NULL;
1072
1073 result = insert_helper(ctx->rbt, "a.b.c.d.e.f.g.h.i.j.k.l.m.", &node);
1074 len = dns__rbtnode_namelen(node);
1075 assert_int_equal(result, ISC_R_SUCCESS);
1076 assert_int_equal(len, 27);
1077
1078 node = NULL;
1079 result = insert_helper(ctx->rbt, "isc.org.", &node);
1080 len = dns__rbtnode_namelen(node);
1081 assert_int_equal(result, ISC_R_SUCCESS);
1082 assert_int_equal(len, 9);
1083
1084 node = NULL;
1085 result = insert_helper(ctx->rbt, "example.com.", &node);
1086 len = dns__rbtnode_namelen(node);
1087 assert_int_equal(result, ISC_R_SUCCESS);
1088 assert_int_equal(len, 13);
1089
1090 test_context_teardown(ctx);
1091 }
1092
1093 #if defined(DNS_BENCHMARK_TESTS) && !defined(__SANITIZE_THREAD__)
1094
1095 /*
1096 * XXXMUKS: Don't delete this code. It is useful in benchmarking the
1097 * RBT, but we don't require it as part of the unit test runs.
1098 */
1099
1100 static dns_fixedname_t *fnames;
1101 static dns_name_t **names;
1102 static int *values;
1103
1104 static void *
1105 find_thread(void *arg) {
1106 dns_rbt_t *mytree;
1107 isc_result_t result;
1108 dns_rbtnode_t *node;
1109 unsigned int j, i;
1110 unsigned int start = 0;
1111
1112 mytree = (dns_rbt_t *)arg;
1113 while (start == 0) {
1114 start = random() % 4000000;
1115 }
1116
1117 /* Query 32 million random names from it in each thread */
1118 for (j = 0; j < 8; j++) {
1119 for (i = start; i != start - 1; i = (i + 1) % 4000000) {
1120 node = NULL;
1121 result = dns_rbt_findnode(mytree, names[i], NULL, &node,
1122 NULL, DNS_RBTFIND_EMPTYDATA,
1123 NULL, NULL);
1124 assert_int_equal(result, ISC_R_SUCCESS);
1125 assert_non_null(node);
1126 assert_int_equal(values[i], (intptr_t)node->data);
1127 }
1128 }
1129
1130 return NULL;
1131 }
1132
1133 /* Benchmark RBT implementation */
1134 ISC_RUN_TEST_IMPL(benchmark) {
1135 isc_result_t result;
1136 char namestr[sizeof("name18446744073709551616.example.org.")];
1137 unsigned int r;
1138 dns_rbt_t *mytree;
1139 dns_rbtnode_t *node;
1140 unsigned int i;
1141 unsigned int maxvalue = 1000000;
1142 isc_time_t ts1, ts2;
1143 double t;
1144 unsigned int nthreads;
1145 isc_thread_t threads[32];
1146
1147 srandom(time(NULL));
1148
1149 debug_mem_record = false;
1150
1151 fnames = (dns_fixedname_t *)malloc(4000000 * sizeof(dns_fixedname_t));
1152 names = (dns_name_t **)malloc(4000000 * sizeof(dns_name_t *));
1153 values = (int *)malloc(4000000 * sizeof(int));
1154
1155 for (i = 0; i < 4000000; i++) {
1156 r = ((unsigned long)random()) % maxvalue;
1157 snprintf(namestr, sizeof(namestr), "name%u.example.org.", r);
1158 dns_test_namefromstring(namestr, &fnames[i]);
1159 names[i] = dns_fixedname_name(&fnames[i]);
1160 values[i] = r;
1161 }
1162
1163 /* Create a tree. */
1164 mytree = NULL;
1165 result = dns_rbt_create(mctx, NULL, NULL, &mytree);
1166 assert_int_equal(result, ISC_R_SUCCESS);
1167
1168 /* Insert test data into the tree. */
1169 for (i = 0; i < maxvalue; i++) {
1170 snprintf(namestr, sizeof(namestr), "name%u.example.org.", i);
1171 node = NULL;
1172 result = insert_helper(mytree, namestr, &node);
1173 assert_int_equal(result, ISC_R_SUCCESS);
1174 node->data = (void *)(intptr_t)i;
1175 }
1176
1177 ts1 = isc_time_now();
1178
1179 nthreads = ISC_MIN(isc_os_ncpus(), 32);
1180 nthreads = ISC_MAX(nthreads, 1);
1181 for (i = 0; i < nthreads; i++) {
1182 isc_thread_create(find_thread, mytree, &threads[i]);
1183 }
1184
1185 for (i = 0; i < nthreads; i++) {
1186 isc_thread_join(threads[i], NULL);
1187 }
1188
1189 ts2 = isc_time_now();
1190
1191 t = isc_time_microdiff(&ts2, &ts1);
1192
1193 printf("%u findnode calls, %f seconds, %f calls/second\n",
1194 nthreads * 8 * 4000000, t / 1000000.0,
1195 (nthreads * 8 * 4000000) / (t / 1000000.0));
1196
1197 free(values);
1198 free(names);
1199 free(fnames);
1200
1201 dns_rbt_destroy(&mytree, 0);
1202 }
1203 #endif /* defined(DNS_BENCHMARK_TESTS) && !defined(__SANITIZE_THREAD__) */
1204
1205 ISC_TEST_LIST_START
1206 ISC_TEST_ENTRY(rbt_create)
1207 ISC_TEST_ENTRY(rbt_nodecount)
1208 ISC_TEST_ENTRY(rbtnode_get_distance)
1209 ISC_TEST_ENTRY(rbt_check_distance_random)
1210 ISC_TEST_ENTRY(rbt_check_distance_ordered)
1211 ISC_TEST_ENTRY(rbt_insert)
1212 ISC_TEST_ENTRY(rbt_remove)
1213 ISC_TEST_ENTRY(rbt_insert_and_remove)
1214 ISC_TEST_ENTRY(rbt_nodechain)
1215 ISC_TEST_ENTRY(rbtnode_namelen)
1216 #if defined(DNS_BENCHMARK_TESTS) && !defined(__SANITIZE_THREAD__)
1217 ISC_TEST_ENTRY(benchmark)
1218 #endif /* defined(DNS_BENCHMARK_TESTS) && !defined(__SANITIZE_THREAD__) */
1219
1220 ISC_TEST_LIST_END
1221
1222 ISC_TEST_MAIN