Debian Codegraph @

tests/dns/dbversion_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/file.h>
27 #include <isc/result.h>
28 #include <isc/serial.h>
29 #include <isc/stdtime.h>
30 #include <isc/string.h>
31 #include <isc/util.h>
32
33 #include <dns/db.h>
34 #include <dns/nsec3.h>
35 #include <dns/rdatalist.h>
36 #include <dns/rdataset.h>
37 #include <dns/rdatasetiter.h>
38
39 #include <tests/dns.h>
40
41 static char tempname[11] = "dtXXXXXXXX";
42 static dns_db_t *db1 = NULL, *db2 = NULL;
43 static dns_dbversion_t *v1 = NULL, *v2 = NULL;
44
45 /*
46 * The code below enables us to trap assertion failures for testing
47 * purposes. local_callback() is set as the callback function for
48 * isc_assertion_failed(). It calls mock_assert() so that CMOCKA
49 * will be able to see it, then returns to the calling function via
50 * longjmp() so that the abort() call in isc_assertion_failed() will
51 * never be reached. Use check_assertion() to check for assertions
52 * instead of expect_assert_failure().
53 */
54 jmp_buf assertion;
55
56 #define check_assertion(function_call) \
57 do { \
58 const int r = setjmp(assertion); \
59 if (r == 0) { \
60 expect_assert_failure(function_call); \
61 } \
62 } while (false);
63
64 static void
65 local_callback(const char *file, int line, isc_assertiontype_t type,
66 const char *cond) {
67 UNUSED(type);
68
69 mock_assert(1, cond, file, line);
70 longjmp(assertion, 1);
71 }
72
73 static int
74 setup_test(void **state) {
75 isc_result_t res;
76
77 UNUSED(state);
78
79 isc_assertion_setcallback(local_callback);
80
81 res = dns_db_create(mctx, ZONEDB_DEFAULT, dns_rootname, dns_dbtype_zone,
82 dns_rdataclass_in, 0, NULL, &db1);
83 assert_int_equal(res, ISC_R_SUCCESS);
84 dns_db_newversion(db1, &v1);
85 assert_non_null(v1);
86
87 res = dns_db_create(mctx, ZONEDB_DEFAULT, dns_rootname, dns_dbtype_zone,
88 dns_rdataclass_in, 0, NULL, &db2);
89 assert_int_equal(res, ISC_R_SUCCESS);
90 dns_db_newversion(db2, &v2);
91 assert_non_null(v1);
92
93 return 0;
94 }
95
96 static int
97 teardown_test(void **state) {
98 UNUSED(state);
99
100 if (strcmp(tempname, "dtXXXXXXXX") != 0) {
101 unlink(tempname);
102 }
103
104 if (v1 != NULL) {
105 dns_db_closeversion(db1, &v1, false);
106 assert_null(v1);
107 }
108 if (db1 != NULL) {
109 dns_db_detach(&db1);
110 assert_null(db1);
111 }
112
113 if (v2 != NULL) {
114 dns_db_closeversion(db2, &v2, false);
115 assert_null(v2);
116 }
117 if (db2 != NULL) {
118 dns_db_detach(&db2);
119 assert_null(db2);
120 }
121
122 return 0;
123 }
124
125 /*
126 * Check dns_db_attachversion() passes with matching db and version, and
127 * asserts with mis-matching db and version.
128 */
129 ISC_RUN_TEST_IMPL(attachversion) {
130 dns_dbversion_t *v = NULL;
131
132 UNUSED(state);
133
134 dns_db_attachversion(db1, v1, &v);
135 assert_ptr_equal(v, v1);
136 dns_db_closeversion(db1, &v, false);
137 assert_null(v);
138
139 check_assertion(dns_db_attachversion(db1, v2, &v));
140 }
141
142 /*
143 * Check dns_db_closeversion() passes with matching db and version, and
144 * asserts with mis-matching db and version.
145 */
146 ISC_RUN_TEST_IMPL(closeversion) {
147 UNUSED(state);
148
149 assert_non_null(v1);
150 dns_db_closeversion(db1, &v1, false);
151 assert_null(v1);
152
153 check_assertion(dns_db_closeversion(db1, &v2, false));
154 }
155
156 /*
157 * Check dns_db_find() passes with matching db and version, and
158 * asserts with mis-matching db and version.
159 */
160 ISC_RUN_TEST_IMPL(find) {
161 isc_result_t res;
162 dns_rdataset_t rdataset;
163 dns_fixedname_t fixed;
164 dns_name_t *name = NULL;
165
166 UNUSED(state);
167
168 name = dns_fixedname_initname(&fixed);
169
170 dns_rdataset_init(&rdataset);
171 res = dns_db_find(db1, dns_rootname, v1, dns_rdatatype_soa, 0, 0, NULL,
172 name, &rdataset, NULL);
173 assert_int_equal(res, DNS_R_NXDOMAIN);
174
175 if (dns_rdataset_isassociated(&rdataset)) {
176 dns_rdataset_disassociate(&rdataset);
177 }
178
179 dns_rdataset_init(&rdataset);
180 check_assertion((void)dns_db_find(db1, dns_rootname, v2,
181 dns_rdatatype_soa, 0, 0, NULL, name,
182 &rdataset, NULL));
183 }
184
185 /*
186 * Check dns_db_allrdatasets() passes with matching db and version, and
187 * asserts with mis-matching db and version.
188 */
189 ISC_RUN_TEST_IMPL(allrdatasets) {
190 isc_result_t res;
191 dns_dbnode_t *node = NULL;
192 dns_rdatasetiter_t *iterator = NULL;
193
194 UNUSED(state);
195
196 res = dns_db_findnode(db1, dns_rootname, false, &node);
197 assert_int_equal(res, ISC_R_SUCCESS);
198
199 res = dns_db_allrdatasets(db1, node, v1, 0, 0, &iterator);
200 assert_int_equal(res, ISC_R_SUCCESS);
201
202 check_assertion(dns_db_allrdatasets(db1, node, v2, 0, 0, &iterator));
203
204 dns_rdatasetiter_destroy(&iterator);
205 assert_null(iterator);
206
207 dns_db_detachnode(db1, &node);
208 assert_null(node);
209 }
210
211 /*
212 * Check dns_db_findrdataset() passes with matching db and version, and
213 * asserts with mis-matching db and version.
214 */
215 ISC_RUN_TEST_IMPL(findrdataset) {
216 isc_result_t res;
217 dns_rdataset_t rdataset;
218 dns_dbnode_t *node = NULL;
219
220 UNUSED(state);
221
222 res = dns_db_findnode(db1, dns_rootname, false, &node);
223 assert_int_equal(res, ISC_R_SUCCESS);
224
225 dns_rdataset_init(&rdataset);
226 res = dns_db_findrdataset(db1, node, v1, dns_rdatatype_soa, 0, 0,
227 &rdataset, NULL);
228 assert_int_equal(res, ISC_R_NOTFOUND);
229
230 if (dns_rdataset_isassociated(&rdataset)) {
231 dns_rdataset_disassociate(&rdataset);
232 }
233
234 dns_rdataset_init(&rdataset);
235 check_assertion(dns_db_findrdataset(db1, node, v2, dns_rdatatype_soa, 0,
236 0, &rdataset, NULL));
237
238 dns_db_detachnode(db1, &node);
239 assert_null(node);
240 }
241
242 /*
243 * Check dns_db_deleterdataset() passes with matching db and version, and
244 * asserts with mis-matching db and version.
245 */
246 ISC_RUN_TEST_IMPL(deleterdataset) {
247 isc_result_t res;
248 dns_dbnode_t *node = NULL;
249
250 UNUSED(state);
251
252 res = dns_db_findnode(db1, dns_rootname, false, &node);
253 assert_int_equal(res, ISC_R_SUCCESS);
254
255 res = dns_db_deleterdataset(db1, node, v1, dns_rdatatype_soa, 0);
256 assert_int_equal(res, DNS_R_UNCHANGED);
257
258 check_assertion(
259 dns_db_deleterdataset(db1, node, v2, dns_rdatatype_soa, 0));
260 dns_db_detachnode(db1, &node);
261 assert_null(node);
262 }
263
264 /*
265 * Check dns_db_subtractrdataset() passes with matching db and version, and
266 * asserts with mis-matching db and version.
267 */
268 ISC_RUN_TEST_IMPL(subtract) {
269 isc_result_t res;
270 dns_rdataset_t rdataset;
271 dns_rdatalist_t rdatalist;
272 dns_dbnode_t *node = NULL;
273
274 UNUSED(state);
275
276 dns_rdataset_init(&rdataset);
277 dns_rdatalist_init(&rdatalist);
278
279 rdatalist.rdclass = dns_rdataclass_in;
280
281 dns_rdatalist_tordataset(&rdatalist, &rdataset);
282
283 res = dns_db_findnode(db1, dns_rootname, false, &node);
284 assert_int_equal(res, ISC_R_SUCCESS);
285
286 res = dns_db_subtractrdataset(db1, node, v1, &rdataset, 0, NULL);
287 assert_int_equal(res, DNS_R_UNCHANGED);
288
289 if (dns_rdataset_isassociated(&rdataset)) {
290 dns_rdataset_disassociate(&rdataset);
291 }
292
293 dns_rdataset_init(&rdataset);
294 dns_rdatalist_tordataset(&rdatalist, &rdataset);
295
296 check_assertion(
297 dns_db_subtractrdataset(db1, node, v2, &rdataset, 0, NULL));
298
299 dns_db_detachnode(db1, &node);
300 assert_null(node);
301 }
302
303 /*
304 * Check dns_db_addrdataset() passes with matching db and version, and
305 * asserts with mis-matching db and version.
306 */
307 ISC_RUN_TEST_IMPL(addrdataset) {
308 isc_result_t res;
309 dns_rdataset_t rdataset;
310 dns_dbnode_t *node = NULL;
311 dns_rdatalist_t rdatalist;
312
313 UNUSED(state);
314
315 dns_rdataset_init(&rdataset);
316 dns_rdatalist_init(&rdatalist);
317
318 rdatalist.rdclass = dns_rdataclass_in;
319
320 dns_rdatalist_tordataset(&rdatalist, &rdataset);
321
322 res = dns_db_findnode(db1, dns_rootname, false, &node);
323 assert_int_equal(res, ISC_R_SUCCESS);
324
325 res = dns_db_addrdataset(db1, node, v1, 0, &rdataset, 0, NULL);
326 assert_int_equal(res, ISC_R_SUCCESS);
327
328 check_assertion(
329 dns_db_addrdataset(db1, node, v2, 0, &rdataset, 0, NULL));
330
331 dns_db_detachnode(db1, &node);
332 assert_null(node);
333 }
334
335 /*
336 * Check dns_db_getnsec3parameters() passes with matching db and version,
337 * and asserts with mis-matching db and version.
338 */
339 ISC_RUN_TEST_IMPL(getnsec3parameters) {
340 isc_result_t res;
341 dns_hash_t hash;
342 uint8_t flags;
343 uint16_t iterations;
344 unsigned char salt[DNS_NSEC3_SALTSIZE];
345 size_t salt_length = sizeof(salt);
346
347 UNUSED(state);
348
349 res = dns_db_getnsec3parameters(db1, v1, &hash, &flags, &iterations,
350 salt, &salt_length);
351 assert_int_equal(res, ISC_R_NOTFOUND);
352
353 check_assertion(dns_db_getnsec3parameters(
354 db1, v2, &hash, &flags, &iterations, salt, &salt_length));
355 }
356
357 ISC_TEST_LIST_START
358 ISC_TEST_ENTRY_CUSTOM(find, setup_test, teardown_test)
359 ISC_TEST_ENTRY_CUSTOM(allrdatasets, setup_test, teardown_test)
360 ISC_TEST_ENTRY_CUSTOM(findrdataset, setup_test, teardown_test)
361 ISC_TEST_ENTRY_CUSTOM(deleterdataset, setup_test, teardown_test)
362 ISC_TEST_ENTRY_CUSTOM(subtract, setup_test, teardown_test)
363 ISC_TEST_ENTRY_CUSTOM(addrdataset, setup_test, teardown_test)
364 ISC_TEST_ENTRY_CUSTOM(getnsec3parameters, setup_test, teardown_test)
365 ISC_TEST_ENTRY_CUSTOM(attachversion, setup_test, teardown_test)
366 ISC_TEST_ENTRY_CUSTOM(closeversion, setup_test, teardown_test)
367 ISC_TEST_LIST_END
368
369 ISC_TEST_MAIN