zonemgr_test.c revision 1.2 1 1.1 christos /* $NetBSD: zonemgr_test.c,v 1.2 2024/02/21 22:52:50 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos *
6 1.1 christos * SPDX-License-Identifier: MPL-2.0
7 1.1 christos *
8 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
9 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
10 1.1 christos * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 1.1 christos *
12 1.1 christos * See the COPYRIGHT file distributed with this work for additional
13 1.1 christos * information regarding copyright ownership.
14 1.1 christos */
15 1.1 christos
16 1.1 christos #include <inttypes.h>
17 1.1 christos #include <sched.h> /* IWYU pragma: keep */
18 1.1 christos #include <setjmp.h>
19 1.1 christos #include <stdarg.h>
20 1.1 christos #include <stddef.h>
21 1.1 christos #include <stdlib.h>
22 1.1 christos #include <string.h>
23 1.1 christos #include <unistd.h>
24 1.1 christos
25 1.1 christos #define UNIT_TESTING
26 1.1 christos #include <cmocka.h>
27 1.1 christos
28 1.1 christos #include <isc/buffer.h>
29 1.1 christos #include <isc/task.h>
30 1.1 christos #include <isc/timer.h>
31 1.1 christos #include <isc/util.h>
32 1.1 christos
33 1.1 christos #include <dns/name.h>
34 1.1 christos #include <dns/view.h>
35 1.1 christos #include <dns/zone.h>
36 1.1 christos
37 1.1 christos #include <tests/dns.h>
38 1.1 christos
39 1.1 christos /* create zone manager */
40 1.1 christos ISC_RUN_TEST_IMPL(dns_zonemgr_create) {
41 1.1 christos dns_zonemgr_t *myzonemgr = NULL;
42 1.1 christos isc_result_t result;
43 1.1 christos
44 1.1 christos UNUSED(state);
45 1.1 christos
46 1.1 christos result = dns_zonemgr_create(mctx, taskmgr, timermgr, netmgr,
47 1.1 christos &myzonemgr);
48 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
49 1.1 christos
50 1.1 christos dns_zonemgr_shutdown(myzonemgr);
51 1.1 christos dns_zonemgr_detach(&myzonemgr);
52 1.1 christos assert_null(myzonemgr);
53 1.1 christos }
54 1.1 christos
55 1.1 christos /* manage and release a zone */
56 1.1 christos ISC_RUN_TEST_IMPL(dns_zonemgr_managezone) {
57 1.1 christos dns_zonemgr_t *myzonemgr = NULL;
58 1.1 christos dns_zone_t *zone = NULL;
59 1.1 christos isc_result_t result;
60 1.1 christos
61 1.1 christos UNUSED(state);
62 1.1 christos
63 1.1 christos result = dns_zonemgr_create(mctx, taskmgr, timermgr, netmgr,
64 1.1 christos &myzonemgr);
65 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
66 1.1 christos
67 1.1 christos result = dns_test_makezone("foo", &zone, NULL, false);
68 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
69 1.1 christos
70 1.1 christos /* This should not succeed until the dns_zonemgr_setsize() is run */
71 1.1 christos result = dns_zonemgr_managezone(myzonemgr, zone);
72 1.1 christos assert_int_equal(result, ISC_R_FAILURE);
73 1.1 christos
74 1.1 christos assert_int_equal(dns_zonemgr_getcount(myzonemgr, DNS_ZONESTATE_ANY), 0);
75 1.1 christos
76 1.1 christos result = dns_zonemgr_setsize(myzonemgr, 1);
77 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
78 1.1 christos
79 1.1 christos /* Now it should succeed */
80 1.1 christos result = dns_zonemgr_managezone(myzonemgr, zone);
81 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
82 1.1 christos
83 1.1 christos assert_int_equal(dns_zonemgr_getcount(myzonemgr, DNS_ZONESTATE_ANY), 1);
84 1.1 christos
85 1.1 christos dns_zonemgr_releasezone(myzonemgr, zone);
86 1.1 christos dns_zone_detach(&zone);
87 1.1 christos
88 1.1 christos assert_int_equal(dns_zonemgr_getcount(myzonemgr, DNS_ZONESTATE_ANY), 0);
89 1.1 christos
90 1.1 christos dns_zonemgr_shutdown(myzonemgr);
91 1.1 christos dns_zonemgr_detach(&myzonemgr);
92 1.1 christos assert_null(myzonemgr);
93 1.1 christos }
94 1.1 christos
95 1.1 christos /* create and release a zone */
96 1.1 christos ISC_RUN_TEST_IMPL(dns_zonemgr_createzone) {
97 1.1 christos dns_zonemgr_t *myzonemgr = NULL;
98 1.1 christos dns_zone_t *zone = NULL;
99 1.1 christos isc_result_t result;
100 1.1 christos
101 1.1 christos UNUSED(state);
102 1.1 christos
103 1.1 christos result = dns_zonemgr_create(mctx, taskmgr, timermgr, netmgr,
104 1.1 christos &myzonemgr);
105 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
106 1.1 christos
107 1.1 christos /* This should not succeed until the dns_zonemgr_setsize() is run */
108 1.1 christos result = dns_zonemgr_createzone(myzonemgr, &zone);
109 1.1 christos assert_int_equal(result, ISC_R_FAILURE);
110 1.1 christos
111 1.1 christos result = dns_zonemgr_setsize(myzonemgr, 1);
112 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
113 1.1 christos
114 1.1 christos /* Now it should succeed */
115 1.1 christos result = dns_zonemgr_createzone(myzonemgr, &zone);
116 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
117 1.1 christos assert_non_null(zone);
118 1.1 christos
119 1.1 christos result = dns_zonemgr_managezone(myzonemgr, zone);
120 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
121 1.1 christos
122 1.1 christos dns_zone_detach(&zone);
123 1.1 christos
124 1.1 christos dns_zonemgr_shutdown(myzonemgr);
125 1.1 christos dns_zonemgr_detach(&myzonemgr);
126 1.1 christos assert_null(myzonemgr);
127 1.1 christos }
128 1.1 christos
129 1.1 christos /* manage and release a zone */
130 1.1 christos ISC_RUN_TEST_IMPL(dns_zonemgr_unreachable) {
131 1.1 christos dns_zonemgr_t *myzonemgr = NULL;
132 1.1 christos dns_zone_t *zone = NULL;
133 1.1 christos isc_sockaddr_t addr1, addr2;
134 1.1 christos struct in_addr in;
135 1.1 christos isc_result_t result;
136 1.1 christos isc_time_t now;
137 1.1 christos
138 1.1 christos UNUSED(state);
139 1.1 christos
140 1.1 christos TIME_NOW(&now);
141 1.1 christos
142 1.1 christos result = dns_zonemgr_create(mctx, taskmgr, timermgr, netmgr,
143 1.1 christos &myzonemgr);
144 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
145 1.1 christos
146 1.1 christos result = dns_test_makezone("foo", &zone, NULL, false);
147 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
148 1.1 christos
149 1.1 christos result = dns_zonemgr_setsize(myzonemgr, 1);
150 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
151 1.1 christos
152 1.1 christos result = dns_zonemgr_managezone(myzonemgr, zone);
153 1.1 christos assert_int_equal(result, ISC_R_SUCCESS);
154 1.1 christos
155 1.1 christos in.s_addr = inet_addr("10.53.0.1");
156 1.1 christos isc_sockaddr_fromin(&addr1, &in, 2112);
157 1.1 christos in.s_addr = inet_addr("10.53.0.2");
158 1.1 christos isc_sockaddr_fromin(&addr2, &in, 5150);
159 1.1 christos assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now));
160 1.1 christos /*
161 1.1 christos * We require multiple unreachableadd calls to mark a server as
162 1.1 christos * unreachable.
163 1.1 christos */
164 1.1 christos dns_zonemgr_unreachableadd(myzonemgr, &addr1, &addr2, &now);
165 1.1 christos assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now));
166 1.1 christos dns_zonemgr_unreachableadd(myzonemgr, &addr1, &addr2, &now);
167 1.1 christos assert_true(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now));
168 1.1 christos
169 1.1 christos in.s_addr = inet_addr("10.53.0.3");
170 1.1 christos isc_sockaddr_fromin(&addr2, &in, 5150);
171 1.1 christos assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now));
172 1.1 christos /*
173 1.1 christos * We require multiple unreachableadd calls to mark a server as
174 1.1 christos * unreachable.
175 1.1 christos */
176 1.1 christos dns_zonemgr_unreachableadd(myzonemgr, &addr1, &addr2, &now);
177 1.1 christos dns_zonemgr_unreachableadd(myzonemgr, &addr1, &addr2, &now);
178 1.1 christos assert_true(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now));
179 1.1 christos
180 1.1 christos dns_zonemgr_unreachabledel(myzonemgr, &addr1, &addr2);
181 1.1 christos assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now));
182 1.1 christos
183 1.1 christos in.s_addr = inet_addr("10.53.0.2");
184 1.1 christos isc_sockaddr_fromin(&addr2, &in, 5150);
185 1.1 christos assert_true(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now));
186 1.1 christos dns_zonemgr_unreachabledel(myzonemgr, &addr1, &addr2);
187 1.1 christos assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now));
188 1.1 christos
189 1.1 christos dns_zonemgr_releasezone(myzonemgr, zone);
190 1.1 christos dns_zone_detach(&zone);
191 1.1 christos dns_zonemgr_shutdown(myzonemgr);
192 1.1 christos dns_zonemgr_detach(&myzonemgr);
193 1.1 christos assert_null(myzonemgr);
194 1.1 christos }
195 1.1 christos
196 1.1 christos /*
197 1.1 christos * XXX:
198 1.1 christos * dns_zonemgr API calls that are not yet part of this unit test:
199 1.1 christos *
200 1.1 christos * - dns_zonemgr_attach
201 1.1 christos * - dns_zonemgr_forcemaint
202 1.1 christos * - dns_zonemgr_resumexfrs
203 1.1 christos * - dns_zonemgr_shutdown
204 1.1 christos * - dns_zonemgr_setsize
205 1.1 christos * - dns_zonemgr_settransfersin
206 1.1 christos * - dns_zonemgr_getttransfersin
207 1.1 christos * - dns_zonemgr_settransfersperns
208 1.1 christos * - dns_zonemgr_getttransfersperns
209 1.1 christos * - dns_zonemgr_setiolimit
210 1.1 christos * - dns_zonemgr_getiolimit
211 1.1 christos * - dns_zonemgr_dbdestroyed
212 1.1 christos * - dns_zonemgr_setserialqueryrate
213 1.1 christos * - dns_zonemgr_getserialqueryrate
214 1.1 christos */
215 1.1 christos
216 1.1 christos ISC_TEST_LIST_START
217 1.1 christos
218 1.1 christos ISC_TEST_ENTRY_CUSTOM(dns_zonemgr_create, setup_managers, teardown_managers)
219 1.1 christos ISC_TEST_ENTRY_CUSTOM(dns_zonemgr_managezone, setup_managers, teardown_managers)
220 1.1 christos ISC_TEST_ENTRY_CUSTOM(dns_zonemgr_createzone, setup_managers, teardown_managers)
221 1.1 christos ISC_TEST_ENTRY_CUSTOM(dns_zonemgr_unreachable, setup_managers,
222 1.1 christos teardown_managers)
223 1.1 christos
224 1.1 christos ISC_TEST_LIST_END
225 1.1 christos
226 1.1 christos ISC_TEST_MAIN
227