Debian Codegraph @

tests/dns/nametree_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 <stdbool.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #define UNIT_TESTING
25 #include <cmocka.h>
26
27 #include <isc/base64.h>
28 #include <isc/buffer.h>
29 #include <isc/md.h>
30 #include <isc/util.h>
31
32 #include <dns/fixedname.h>
33 #include <dns/name.h>
34 #include <dns/nametree.h>
35
36 #include <dst/dst.h>
37
38 #include <tests/dns.h>
39
40 dns_nametree_t *booltree = NULL;
41 dns_nametree_t *bitstree = NULL;
42 dns_nametree_t *counttree = NULL;
43
44 /*
45 * Test utilities. In general, these assume input parameters are valid
46 * (checking with assert_int_equal, thus aborting if not) and unlikely run time
47 * errors (such as memory allocation failure) won't happen. This helps keep
48 * the test code concise.
49 */
50
51 /* Common setup: create trees of each type with a few keys */
52 static int
53 setup(void **state ISC_ATTR_UNUSED) {
54 dns_fixedname_t fn;
55 dns_name_t *name = dns_fixedname_name(&fn);
56
57 dns_nametree_create(mctx, DNS_NAMETREE_BOOL, "bool test", &booltree);
58 dns_nametree_create(mctx, DNS_NAMETREE_BITS, "bits test", &bitstree);
59 dns_nametree_create(mctx, DNS_NAMETREE_COUNT, "count test", &counttree);
60
61 /* Add a positive boolean node */
62 dns_test_namefromstring("example.com.", &fn);
63 assert_int_equal(dns_nametree_add(booltree, name, true), ISC_R_SUCCESS);
64
65 /* Add assorted bits to a bitfield node */
66 assert_int_equal(dns_nametree_add(bitstree, name, 1), ISC_R_SUCCESS);
67 assert_int_equal(dns_nametree_add(bitstree, name, 9), ISC_R_SUCCESS);
68 assert_int_equal(dns_nametree_add(bitstree, name, 53), ISC_R_SUCCESS);
69
70 /* Add negative boolean nodes with and without parents */
71 dns_test_namefromstring("negative.example.com.", &fn);
72 assert_int_equal(dns_nametree_add(booltree, name, false),
73 ISC_R_SUCCESS);
74 dns_test_namefromstring("negative.example.org.", &fn);
75 assert_int_equal(dns_nametree_add(booltree, name, false),
76 ISC_R_SUCCESS);
77
78 /* Add a bitfield node under a parent */
79 dns_test_namefromstring("sub.example.com.", &fn);
80 assert_int_equal(dns_nametree_add(bitstree, name, 2), ISC_R_SUCCESS);
81
82 return 0;
83 }
84
85 static int
86 teardown(void **state ISC_ATTR_UNUSED) {
87 dns_nametree_detach(&booltree);
88 dns_nametree_detach(&bitstree);
89 dns_nametree_detach(&counttree);
90 rcu_barrier();
91 return 0;
92 }
93
94 ISC_RUN_TEST_IMPL(add_bool) {
95 dns_ntnode_t *node = NULL;
96 dns_fixedname_t fn;
97 dns_name_t *name = dns_fixedname_name(&fn);
98
99 /*
100 * Getting the node for example.com should succeed.
101 */
102 dns_test_namefromstring("example.com.", &fn);
103 assert_int_equal(dns_nametree_find(booltree, name, &node),
104 ISC_R_SUCCESS);
105 dns_ntnode_detach(&node);
106
107 /*
108 * Try to add the same name. This should fail.
109 */
110 assert_int_equal(dns_nametree_add(booltree, name, false), ISC_R_EXISTS);
111 assert_int_equal(dns_nametree_find(booltree, name, &node),
112 ISC_R_SUCCESS);
113 dns_ntnode_detach(&node);
114
115 /*
116 * Try to add a new name.
117 */
118 dns_test_namefromstring("newname.com.", &fn);
119 assert_int_equal(dns_nametree_add(booltree, name, true), ISC_R_SUCCESS);
120 assert_int_equal(dns_nametree_find(booltree, name, &node),
121 ISC_R_SUCCESS);
122 dns_ntnode_detach(&node);
123 }
124
125 ISC_RUN_TEST_IMPL(add_bits) {
126 dns_ntnode_t *node = NULL;
127 dns_fixedname_t fn;
128 dns_name_t *name = dns_fixedname_name(&fn);
129
130 /*
131 * Getting the node for example.com should succeed.
132 */
133 dns_test_namefromstring("example.com.", &fn);
134 assert_int_equal(dns_nametree_find(booltree, name, &node),
135 ISC_R_SUCCESS);
136 dns_ntnode_detach(&node);
137
138 /*
139 * Try to add the same name. This should succeed.
140 */
141 assert_int_equal(dns_nametree_add(bitstree, name, 1), ISC_R_SUCCESS);
142 assert_int_equal(dns_nametree_add(bitstree, name, 2), ISC_R_SUCCESS);
143 assert_int_equal(dns_nametree_add(bitstree, name, 3), ISC_R_SUCCESS);
144 assert_int_equal(dns_nametree_find(booltree, name, &node),
145 ISC_R_SUCCESS);
146 dns_ntnode_detach(&node);
147
148 /*
149 * Try to add a new name.
150 */
151 dns_test_namefromstring("newname.com.", &fn);
152 assert_int_equal(dns_nametree_add(booltree, name, true), ISC_R_SUCCESS);
153 assert_int_equal(dns_nametree_find(booltree, name, &node),
154 ISC_R_SUCCESS);
155 dns_ntnode_detach(&node);
156 }
157
158 ISC_RUN_TEST_IMPL(add_count) {
159 dns_fixedname_t fn;
160 dns_name_t *name = dns_fixedname_name(&fn);
161
162 /* add a counter node five times */
163 dns_test_namefromstring("example.com.", &fn);
164 assert_int_equal(dns_nametree_add(counttree, name, 0), ISC_R_SUCCESS);
165 assert_int_equal(dns_nametree_add(counttree, name, 0), ISC_R_SUCCESS);
166 assert_int_equal(dns_nametree_add(counttree, name, 0), ISC_R_SUCCESS);
167 assert_int_equal(dns_nametree_add(counttree, name, 0), ISC_R_SUCCESS);
168 assert_int_equal(dns_nametree_add(counttree, name, 0), ISC_R_SUCCESS);
169
170 /* delete it five times, checking coverage each time */
171 assert_true(dns_nametree_covered(counttree, name, NULL, 0));
172 assert_int_equal(dns_nametree_delete(counttree, name), ISC_R_SUCCESS);
173
174 assert_true(dns_nametree_covered(counttree, name, NULL, 0));
175 assert_int_equal(dns_nametree_delete(counttree, name), ISC_R_SUCCESS);
176
177 assert_true(dns_nametree_covered(counttree, name, NULL, 0));
178 assert_int_equal(dns_nametree_delete(counttree, name), ISC_R_SUCCESS);
179
180 assert_true(dns_nametree_covered(counttree, name, NULL, 0));
181 assert_int_equal(dns_nametree_delete(counttree, name), ISC_R_SUCCESS);
182
183 assert_true(dns_nametree_covered(counttree, name, NULL, 0));
184 assert_int_equal(dns_nametree_delete(counttree, name), ISC_R_SUCCESS);
185
186 assert_false(dns_nametree_covered(counttree, name, NULL, 0));
187 assert_int_equal(dns_nametree_delete(counttree, name), ISC_R_NOTFOUND);
188 }
189
190 ISC_RUN_TEST_IMPL(covered_bool) {
191 dns_fixedname_t fn, fn2;
192 dns_name_t *name = dns_fixedname_initname(&fn);
193 dns_name_t *found = dns_fixedname_initname(&fn2);
194 char buf[DNS_NAME_FORMATSIZE];
195 const char *yesnames[] = { "example.com.", "sub.example.com.", NULL };
196 const char *nonames[] = { "whatever.com.", "negative.example.com.",
197 "example.org.", "negative.example.org.",
198 NULL };
199
200 for (const char **n = yesnames; *n != NULL; n++) {
201 dns_test_namefromstring(*n, &fn);
202 assert_true(dns_nametree_covered(booltree, name, NULL, 0));
203 }
204 for (const char **n = nonames; *n != NULL; n++) {
205 dns_test_namefromstring(*n, &fn);
206 assert_false(dns_nametree_covered(booltree, name, NULL, 0));
207 }
208
209 /* Check that the found name is as expected */
210 dns_test_namefromstring("other.example.com.", &fn);
211 assert_true(dns_nametree_covered(booltree, name, found, 0));
212 dns_name_format(found, buf, sizeof(buf));
213 assert_string_equal(buf, "example.com");
214 }
215
216 ISC_RUN_TEST_IMPL(covered_bits) {
217 dns_fixedname_t fn;
218 dns_name_t *name = dns_fixedname_name(&fn);
219
220 /* check existing bit values */
221 dns_test_namefromstring("example.com.", &fn);
222 assert_false(dns_nametree_covered(bitstree, name, NULL, 0));
223 assert_true(dns_nametree_covered(bitstree, name, NULL, 1));
224 assert_false(dns_nametree_covered(bitstree, name, NULL, 2));
225 assert_false(dns_nametree_covered(bitstree, name, NULL, 3));
226 assert_true(dns_nametree_covered(bitstree, name, NULL, 9));
227 assert_true(dns_nametree_covered(bitstree, name, NULL, 53));
228 assert_false(dns_nametree_covered(bitstree, name, NULL, 288));
229
230 /* add a small bit value, test again */
231 assert_int_equal(dns_nametree_add(bitstree, name, 3), ISC_R_SUCCESS);
232 assert_true(dns_nametree_covered(bitstree, name, NULL, 3));
233
234 /* add a large bit value, test again */
235 assert_false(dns_nametree_covered(bitstree, name, NULL, 615));
236 assert_int_equal(dns_nametree_add(bitstree, name, 615), ISC_R_SUCCESS);
237 assert_true(dns_nametree_covered(bitstree, name, NULL, 615));
238 assert_int_equal(dns_nametree_add(bitstree, name, 999), ISC_R_SUCCESS);
239 assert_true(dns_nametree_covered(bitstree, name, NULL, 999));
240 assert_false(dns_nametree_covered(bitstree, name, NULL, 998));
241
242 /* check existing bit values for subdomain */
243 dns_test_namefromstring("sub.example.com.", &fn);
244 assert_false(dns_nametree_covered(bitstree, name, NULL, 0));
245 assert_false(dns_nametree_covered(bitstree, name, NULL, 1));
246 assert_true(dns_nametree_covered(bitstree, name, NULL, 2));
247 assert_false(dns_nametree_covered(bitstree, name, NULL, 3));
248 assert_false(dns_nametree_covered(bitstree, name, NULL, 9));
249 assert_false(dns_nametree_covered(bitstree, name, NULL, 53));
250 assert_false(dns_nametree_covered(bitstree, name, NULL, 288));
251
252 /* check nonexistent subdomain is all false */
253 dns_test_namefromstring("other.example.com", &fn);
254 assert_false(dns_nametree_covered(bitstree, name, NULL, 0));
255 assert_false(dns_nametree_covered(bitstree, name, NULL, 1));
256 assert_false(dns_nametree_covered(bitstree, name, NULL, 2));
257 assert_false(dns_nametree_covered(bitstree, name, NULL, 3));
258 assert_false(dns_nametree_covered(bitstree, name, NULL, 9));
259 assert_false(dns_nametree_covered(bitstree, name, NULL, 53));
260 assert_false(dns_nametree_covered(bitstree, name, NULL, 288));
261
262 /* check nonexistent domain is all false */
263 dns_test_namefromstring("anyname.", &fn);
264 assert_false(dns_nametree_covered(bitstree, name, NULL, 0));
265 assert_false(dns_nametree_covered(bitstree, name, NULL, 1));
266 assert_false(dns_nametree_covered(bitstree, name, NULL, 2));
267 assert_false(dns_nametree_covered(bitstree, name, NULL, 3));
268 assert_false(dns_nametree_covered(bitstree, name, NULL, 9));
269 assert_false(dns_nametree_covered(bitstree, name, NULL, 53));
270 assert_false(dns_nametree_covered(bitstree, name, NULL, 288));
271 }
272
273 ISC_RUN_TEST_IMPL(delete) {
274 dns_fixedname_t fn;
275 dns_name_t *name = dns_fixedname_name(&fn);
276
277 /* name doesn't match */
278 dns_test_namefromstring("example.org.", &fn);
279 assert_int_equal(dns_nametree_delete(booltree, name), ISC_R_NOTFOUND);
280
281 /* subdomain match is the same as no match */
282 dns_test_namefromstring("sub.example.org.", &fn);
283 assert_int_equal(dns_nametree_delete(booltree, name), ISC_R_NOTFOUND);
284
285 /*
286 * delete requires exact match: this should return SUCCESS on
287 * the first try, then NOTFOUND on the second even though an
288 * ancestor does exist.
289 */
290 dns_test_namefromstring("negative.example.com.", &fn);
291 assert_int_equal(dns_nametree_delete(booltree, name), ISC_R_SUCCESS);
292 assert_int_equal(dns_nametree_delete(booltree, name), ISC_R_NOTFOUND);
293
294 dns_test_namefromstring("negative.example.org.", &fn);
295 assert_int_equal(dns_nametree_delete(booltree, name), ISC_R_SUCCESS);
296 assert_int_equal(dns_nametree_delete(booltree, name), ISC_R_NOTFOUND);
297 }
298
299 ISC_RUN_TEST_IMPL(find) {
300 dns_ntnode_t *node = NULL;
301 dns_fixedname_t fn;
302 dns_name_t *name = dns_fixedname_name(&fn);
303
304 /*
305 * dns_nametree_find() requires exact name match. It matches node
306 * that has a null key, too.
307 */
308 dns_test_namefromstring("example.org.", &fn);
309 assert_int_equal(dns_nametree_find(booltree, name, &node),
310 ISC_R_NOTFOUND);
311 dns_test_namefromstring("sub.example.com.", &fn);
312 assert_int_equal(dns_nametree_find(booltree, name, &node),
313 ISC_R_NOTFOUND);
314 dns_test_namefromstring("example.com.", &fn);
315 assert_int_equal(dns_nametree_find(booltree, name, &node),
316 ISC_R_SUCCESS);
317 dns_ntnode_detach(&node);
318 }
319
320 ISC_TEST_LIST_START
321 ISC_TEST_ENTRY_CUSTOM(add_bool, setup, teardown)
322 ISC_TEST_ENTRY_CUSTOM(add_bits, setup, teardown)
323 ISC_TEST_ENTRY_CUSTOM(add_count, setup, teardown)
324 ISC_TEST_ENTRY_CUSTOM(covered_bool, setup, teardown)
325 ISC_TEST_ENTRY_CUSTOM(covered_bits, setup, teardown)
326 ISC_TEST_ENTRY_CUSTOM(delete, setup, teardown)
327 ISC_TEST_ENTRY_CUSTOM(find, setup, teardown)
328 ISC_TEST_LIST_END
329
330 ISC_TEST_MAIN