Debian Codegraph @

tests/dns/zt_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 <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #define UNIT_TESTING
25 #include <cmocka.h>
26
27 #include <isc/atomic.h>
28 #include <isc/buffer.h>
29 #include <isc/loop.h>
30 #include <isc/timer.h>
31 #include <isc/urcu.h>
32 #include <isc/util.h>
33
34 #include <dns/db.h>
35 #include <dns/name.h>
36 #include <dns/view.h>
37 #include <dns/zone.h>
38 #include <dns/zt.h>
39
40 #include <tests/dns.h>
41
42 static dns_db_t *db = NULL;
43 static FILE *zonefile, *origfile;
44 static dns_view_t *view = NULL;
45
46 static isc_result_t
47 count_zone(dns_zone_t *zone, void *uap) {
48 int *nzones = (int *)uap;
49
50 UNUSED(zone);
51
52 *nzones += 1;
53 return ISC_R_SUCCESS;
54 }
55
56 /* apply a function to a zone table */
57 ISC_LOOP_TEST_IMPL(apply) {
58 isc_result_t result;
59 dns_zone_t *zone = NULL;
60 dns_zt_t *zt = NULL;
61 int nzones = 0;
62
63 result = dns_test_makezone("foo", &zone, NULL, true);
64 assert_int_equal(result, ISC_R_SUCCESS);
65
66 view = dns_zone_getview(zone);
67 rcu_read_lock();
68 zt = rcu_dereference(view->zonetable);
69 rcu_read_unlock();
70
71 assert_non_null(zt);
72
73 assert_int_equal(nzones, 0);
74 result = dns_view_apply(view, false, NULL, count_zone, &nzones);
75 assert_int_equal(result, ISC_R_SUCCESS);
76 assert_int_equal(nzones, 1);
77
78 /* These steps are necessary so the zone can be detached properly */
79 dns_test_setupzonemgr();
80 result = dns_test_managezone(zone);
81 assert_int_equal(result, ISC_R_SUCCESS);
82 dns_test_releasezone(zone);
83 dns_test_closezonemgr();
84
85 /* The view was left attached in dns_test_makezone() */
86 dns_view_detach(&view);
87 dns_zone_detach(&zone);
88 isc_loopmgr_shutdown(loopmgr);
89 }
90
91 static isc_result_t
92 load_done_last(void *uap) {
93 dns_zone_t *zone = uap;
94 isc_result_t result;
95
96 /* The zone should now be loaded; test it */
97 result = dns_zone_getdb(zone, &db);
98 assert_int_equal(result, ISC_R_SUCCESS);
99
100 assert_non_null(db);
101 if (db != NULL) {
102 dns_db_detach(&db);
103 }
104
105 dns_test_releasezone(zone);
106 dns_test_closezonemgr();
107
108 dns_zone_detach(&zone);
109 dns_view_detach(&view);
110
111 isc_loopmgr_shutdown(loopmgr);
112
113 return ISC_R_SUCCESS;
114 }
115
116 static isc_result_t
117 load_done_new_only(void *uap) {
118 dns_zone_t *zone = uap;
119 isc_result_t result;
120
121 /* The zone should now be loaded; test it */
122 result = dns_zone_getdb(zone, &db);
123 assert_int_equal(result, ISC_R_SUCCESS);
124 dns_db_detach(&db);
125
126 dns_zone_asyncload(zone, true, load_done_last, zone);
127
128 return ISC_R_SUCCESS;
129 }
130
131 static isc_result_t
132 load_done_first(void *uap) {
133 dns_zone_t *zone = uap;
134 isc_result_t result;
135
136 /* The zone should now be loaded; test it */
137 result = dns_zone_getdb(zone, &db);
138 assert_int_equal(result, ISC_R_SUCCESS);
139 dns_db_detach(&db);
140
141 /*
142 * Add something to zone file, reload zone with newonly - it should
143 * not be reloaded.
144 */
145 fprintf(zonefile, "\nb in b 1.2.3.4\n");
146 fflush(zonefile);
147 fclose(zonefile);
148
149 dns_zone_asyncload(zone, true, load_done_new_only, zone);
150
151 return ISC_R_SUCCESS;
152 }
153
154 /* asynchronous zone load */
155 ISC_LOOP_TEST_IMPL(asyncload_zone) {
156 isc_result_t result;
157 int n;
158 dns_zone_t *zone = NULL;
159 dns_zt_t *zt = NULL;
160 char buf[4096];
161
162 result = dns_test_makezone("foo", &zone, NULL, true);
163 assert_int_equal(result, ISC_R_SUCCESS);
164
165 dns_test_setupzonemgr();
166 result = dns_test_managezone(zone);
167 assert_int_equal(result, ISC_R_SUCCESS);
168
169 view = dns_zone_getview(zone);
170 rcu_read_lock();
171 zt = rcu_dereference(view->zonetable);
172 rcu_read_unlock();
173 assert_non_null(zt);
174
175 assert_false(dns__zone_loadpending(zone));
176 zonefile = fopen("./zone.data", "wb");
177 assert_non_null(zonefile);
178 origfile = fopen(TESTS_DIR "/testdata/zt/zone1.db", "r+b");
179 assert_non_null(origfile);
180 n = fread(buf, 1, 4096, origfile);
181 fclose(origfile);
182 fwrite(buf, 1, n, zonefile);
183 fflush(zonefile);
184
185 dns_zone_setfile(zone, "./zone.data", dns_masterformat_text,
186 &dns_master_style_default);
187
188 dns_zone_asyncload(zone, false, load_done_first, zone);
189 }
190
191 dns_zone_t *zone1 = NULL, *zone2 = NULL, *zone3 = NULL;
192
193 static isc_result_t
194 all_done(void *arg ISC_ATTR_UNUSED) {
195 isc_result_t result;
196
197 /* Both zones should now be loaded; test them */
198 result = dns_zone_getdb(zone1, &db);
199 assert_int_equal(result, ISC_R_SUCCESS);
200 assert_non_null(db);
201 if (db != NULL) {
202 dns_db_detach(&db);
203 }
204
205 result = dns_zone_getdb(zone2, &db);
206 assert_int_equal(result, ISC_R_SUCCESS);
207 assert_non_null(db);
208 if (db != NULL) {
209 dns_db_detach(&db);
210 }
211
212 dns_test_releasezone(zone3);
213 dns_test_releasezone(zone2);
214 dns_test_releasezone(zone1);
215 dns_test_closezonemgr();
216
217 dns_zone_detach(&zone1);
218 dns_zone_detach(&zone2);
219 dns_zone_detach(&zone3);
220 dns_view_detach(&view);
221
222 isc_loopmgr_shutdown(loopmgr);
223 return ISC_R_SUCCESS;
224 }
225
226 /* asynchronous zone table load */
227 ISC_LOOP_TEST_IMPL(asyncload_zt) {
228 isc_result_t result;
229 dns_zt_t *zt = NULL;
230 atomic_bool done;
231
232 atomic_init(&done, false);
233
234 result = dns_test_makezone("foo", &zone1, NULL, true);
235 assert_int_equal(result, ISC_R_SUCCESS);
236 dns_zone_setfile(zone1, TESTS_DIR "/testdata/zt/zone1.db",
237 dns_masterformat_text, &dns_master_style_default);
238 view = dns_zone_getview(zone1);
239
240 result = dns_test_makezone("bar", &zone2, view, false);
241 assert_int_equal(result, ISC_R_SUCCESS);
242 dns_zone_setfile(zone2, TESTS_DIR "/testdata/zt/zone1.db",
243 dns_masterformat_text, &dns_master_style_default);
244
245 /* This one will fail to load */
246 result = dns_test_makezone("fake", &zone3, view, false);
247 assert_int_equal(result, ISC_R_SUCCESS);
248 dns_zone_setfile(zone3, TESTS_DIR "/testdata/zt/nonexistent.db",
249 dns_masterformat_text, &dns_master_style_default);
250
251 rcu_read_lock();
252 zt = rcu_dereference(view->zonetable);
253 rcu_read_unlock();
254 assert_non_null(zt);
255
256 dns_test_setupzonemgr();
257 result = dns_test_managezone(zone1);
258 assert_int_equal(result, ISC_R_SUCCESS);
259 result = dns_test_managezone(zone2);
260 assert_int_equal(result, ISC_R_SUCCESS);
261 result = dns_test_managezone(zone3);
262 assert_int_equal(result, ISC_R_SUCCESS);
263
264 assert_false(dns__zone_loadpending(zone1));
265 assert_false(dns__zone_loadpending(zone2));
266 assert_false(atomic_load(&done));
267
268 rcu_read_lock();
269 zt = rcu_dereference(view->zonetable);
270 dns_zt_asyncload(zt, false, all_done, NULL);
271 rcu_read_unlock();
272 }
273
274 ISC_TEST_LIST_START
275 ISC_TEST_ENTRY_CUSTOM(apply, setup_managers, teardown_managers)
276 ISC_TEST_ENTRY_CUSTOM(asyncload_zone, setup_managers, teardown_managers)
277 ISC_TEST_ENTRY_CUSTOM(asyncload_zt, setup_managers, teardown_managers)
278 ISC_TEST_LIST_END
279
280 ISC_TEST_MAIN