Line data Source code
1 : /*
2 : * Unit tests for source4/rpc_server/dnsserver/dnsutils.c
3 : *
4 : * Copyright (C) Catalyst.NET Ltd 2018
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : *
19 : */
20 :
21 : /*
22 : * from cmocka.c:
23 : * These headers or their equivalents should be included prior to
24 : * including
25 : * this header file.
26 : *
27 : * #include <stdarg.h>
28 : * #include <stddef.h>
29 : * #include <setjmp.h>
30 : *
31 : * This allows test applications to use custom definitions of C standard
32 : * library functions and types.
33 : *
34 : */
35 :
36 : #include <stdarg.h>
37 : #include <stddef.h>
38 : #include <setjmp.h>
39 : #include <cmocka.h>
40 :
41 :
42 : #include "../dnsserver/dnsutils.c"
43 :
44 :
45 : /*
46 : * Test setting of an empty ZONE_MASTER_SERVERS property
47 : */
48 1 : static void test_dnsserver_init_zoneinfo_master_servers_empty(void **state)
49 : {
50 1 : struct dnsserver_zone *zone = NULL;
51 1 : struct dnsserver_serverinfo *serverinfo = NULL;
52 1 : struct dnsserver_zoneinfo *zoneinfo = NULL;
53 1 : struct dnsp_DnsProperty *property = NULL;
54 :
55 1 : TALLOC_CTX *ctx = talloc_new(NULL);
56 :
57 : /*
58 : * Setup the zone data
59 : */
60 1 : zone = talloc_zero(ctx, struct dnsserver_zone);
61 1 : assert_non_null(zone);
62 1 : zone->name = "test";
63 :
64 : /*
65 : * Set up an empty ZONE_MASTER_SERVERS property
66 : */
67 1 : property = talloc_zero_array(ctx, struct dnsp_DnsProperty, 1);
68 1 : assert_non_null(property);
69 1 : property->id = DSPROPERTY_ZONE_MASTER_SERVERS;
70 1 : property->data.master_servers.addrCount = 0;
71 1 : property->data.master_servers.addrArray = NULL;
72 :
73 1 : zone->tmp_props = property;
74 1 : zone->num_props = 1;
75 :
76 :
77 : /*
78 : * Setup the server info
79 : */
80 1 : serverinfo = talloc_zero(ctx, struct dnsserver_serverinfo);
81 1 : assert_non_null(serverinfo);
82 :
83 : /*
84 : * call dnsserver_init_zoneinfo
85 : */
86 1 : zoneinfo = dnsserver_init_zoneinfo(zone, serverinfo);
87 :
88 : /*
89 : * Check results
90 : */
91 1 : assert_non_null(zoneinfo);
92 1 : assert_non_null(zoneinfo->aipLocalMasters);
93 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrCount, 0);
94 1 : assert_null(zoneinfo->aipLocalMasters->AddrArray);
95 :
96 1 : TALLOC_FREE(ctx);
97 1 : }
98 :
99 : /*
100 : * Test setting of a non empty ZONE_MASTER_SERVERS property
101 : */
102 1 : static void test_dnsserver_init_zoneinfo_master_servers(void **state)
103 : {
104 1 : struct dnsserver_zone *zone = NULL;
105 1 : struct dnsserver_serverinfo *serverinfo = NULL;
106 1 : struct dnsserver_zoneinfo *zoneinfo = NULL;
107 1 : struct dnsp_DnsProperty *property = NULL;
108 :
109 1 : TALLOC_CTX *ctx = talloc_new(NULL);
110 :
111 : /*
112 : * Setup the zone data
113 : */
114 1 : zone = talloc_zero(ctx, struct dnsserver_zone);
115 1 : assert_non_null(zone);
116 1 : zone->name = "test";
117 :
118 : /*
119 : * Set up an empty ZONE_MASTER_SERVERS property
120 : */
121 1 : property = talloc_zero_array(ctx, struct dnsp_DnsProperty, 1);
122 1 : assert_non_null(property);
123 1 : property->id = DSPROPERTY_ZONE_MASTER_SERVERS;
124 1 : property->data.master_servers.addrCount = 4;
125 2 : property->data.master_servers.addrArray =
126 1 : talloc_zero_array(ctx, uint32_t, 4);
127 1 : property->data.master_servers.addrArray[0] = 1000;
128 1 : property->data.master_servers.addrArray[1] = 1001;
129 1 : property->data.master_servers.addrArray[2] = 1002;
130 1 : property->data.master_servers.addrArray[3] = 1003;
131 :
132 1 : zone->tmp_props = property;
133 1 : zone->num_props = 1;
134 :
135 :
136 : /*
137 : * Setup the server info
138 : */
139 1 : serverinfo = talloc_zero(ctx, struct dnsserver_serverinfo);
140 1 : assert_non_null(serverinfo);
141 :
142 : /*
143 : * call dnsserver_init_zoneinfo
144 : */
145 1 : zoneinfo = dnsserver_init_zoneinfo(zone, serverinfo);
146 :
147 : /*
148 : * Check results
149 : */
150 1 : assert_non_null(zoneinfo);
151 1 : assert_non_null(zoneinfo->aipLocalMasters);
152 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrCount, 4);
153 1 : assert_non_null(zoneinfo->aipLocalMasters->AddrArray);
154 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrArray[0], 1000);
155 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrArray[1], 1001);
156 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrArray[2], 1002);
157 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrArray[3], 1003);
158 :
159 : /*
160 : * Ensure that we're working with a copy of the property data
161 : * and not a reference.
162 : * The pointers should be different, and we should be able to change
163 : * the values in the property without affecting the zoneinfo data
164 : */
165 1 : assert_true(zoneinfo->aipLocalMasters->AddrArray !=
166 : property->data.master_servers.addrArray);
167 1 : property->data.master_servers.addrArray[0] = 0;
168 1 : property->data.master_servers.addrArray[1] = 1;
169 1 : property->data.master_servers.addrArray[2] = 2;
170 1 : property->data.master_servers.addrArray[3] = 3;
171 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrArray[0], 1000);
172 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrArray[1], 1001);
173 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrArray[2], 1002);
174 1 : assert_int_equal(zoneinfo->aipLocalMasters->AddrArray[3], 1003);
175 :
176 1 : TALLOC_FREE(ctx);
177 1 : }
178 :
179 : /*
180 : * Test setting of an empty ZONE_SCAVENGING_SERVERS property
181 : */
182 1 : static void test_dnsserver_init_zoneinfo_scavenging_servers_empty(void **state)
183 : {
184 1 : struct dnsserver_zone *zone = NULL;
185 1 : struct dnsserver_serverinfo *serverinfo = NULL;
186 1 : struct dnsserver_zoneinfo *zoneinfo = NULL;
187 1 : struct dnsp_DnsProperty *property = NULL;
188 :
189 1 : TALLOC_CTX *ctx = talloc_new(NULL);
190 :
191 : /*
192 : * Setup the zone data
193 : */
194 1 : zone = talloc_zero(ctx, struct dnsserver_zone);
195 1 : assert_non_null(zone);
196 1 : zone->name = "test";
197 :
198 1 : property = talloc_zero_array(ctx, struct dnsp_DnsProperty, 1);
199 1 : assert_non_null(property);
200 1 : property->id = DSPROPERTY_ZONE_SCAVENGING_SERVERS;
201 1 : property->data.servers.addrCount = 0;
202 1 : property->data.servers.addrArray = NULL;
203 :
204 1 : zone->tmp_props = property;
205 1 : zone->num_props = 1;
206 :
207 :
208 1 : serverinfo = talloc_zero(ctx, struct dnsserver_serverinfo);
209 1 : assert_non_null(serverinfo);
210 :
211 1 : zoneinfo = dnsserver_init_zoneinfo(zone, serverinfo);
212 :
213 1 : assert_non_null(zoneinfo);
214 1 : assert_non_null(zoneinfo->aipScavengeServers);
215 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrCount, 0);
216 1 : assert_null(zoneinfo->aipScavengeServers->AddrArray);
217 :
218 1 : TALLOC_FREE(ctx);
219 1 : }
220 :
221 : /*
222 : * Test setting of a non empty ZONE_SCAVENGING_SERVERS property
223 : */
224 1 : static void test_dnsserver_init_zoneinfo_scavenging_servers(void **state)
225 : {
226 1 : struct dnsserver_zone *zone = NULL;
227 1 : struct dnsserver_serverinfo *serverinfo = NULL;
228 1 : struct dnsserver_zoneinfo *zoneinfo = NULL;
229 1 : struct dnsp_DnsProperty *property = NULL;
230 :
231 1 : TALLOC_CTX *ctx = talloc_new(NULL);
232 :
233 : /*
234 : * Setup the zone data
235 : */
236 1 : zone = talloc_zero(ctx, struct dnsserver_zone);
237 1 : assert_non_null(zone);
238 1 : zone->name = "test";
239 :
240 1 : property = talloc_zero_array(ctx, struct dnsp_DnsProperty, 1);
241 1 : assert_non_null(property);
242 1 : property->id = DSPROPERTY_ZONE_SCAVENGING_SERVERS;
243 1 : property->data.servers.addrCount = 4;
244 1 : property->data.servers.addrArray = talloc_zero_array(ctx, uint32_t, 4);
245 1 : property->data.servers.addrArray[0] = 1000;
246 1 : property->data.servers.addrArray[1] = 1001;
247 1 : property->data.servers.addrArray[2] = 1002;
248 1 : property->data.servers.addrArray[3] = 1003;
249 :
250 1 : zone->tmp_props = property;
251 1 : zone->num_props = 1;
252 :
253 :
254 1 : serverinfo = talloc_zero(ctx, struct dnsserver_serverinfo);
255 1 : assert_non_null(serverinfo);
256 :
257 1 : zoneinfo = dnsserver_init_zoneinfo(zone, serverinfo);
258 :
259 1 : assert_non_null(zoneinfo);
260 1 : assert_non_null(zoneinfo->aipScavengeServers);
261 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrCount, 4);
262 1 : assert_non_null(zoneinfo->aipScavengeServers->AddrArray);
263 1 : assert_non_null(zoneinfo->aipScavengeServers->AddrArray);
264 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrArray[0], 1000);
265 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrArray[1], 1001);
266 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrArray[2], 1002);
267 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrArray[3], 1003);
268 :
269 : /*
270 : * Ensure that we're working with a copy of the property data
271 : * and not a reference.
272 : * The pointers should be different, and we should be able to change
273 : * the values in the property without affecting the zoneinfo data
274 : */
275 1 : assert_true(zoneinfo->aipScavengeServers->AddrArray !=
276 : property->data.servers.addrArray);
277 1 : property->data.servers.addrArray[0] = 0;
278 1 : property->data.servers.addrArray[1] = 1;
279 1 : property->data.servers.addrArray[2] = 2;
280 1 : property->data.servers.addrArray[3] = 3;
281 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrArray[0], 1000);
282 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrArray[1], 1001);
283 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrArray[2], 1002);
284 1 : assert_int_equal(zoneinfo->aipScavengeServers->AddrArray[3], 1003);
285 :
286 :
287 1 : TALLOC_FREE(ctx);
288 1 : }
289 1 : int main(int argc, const char **argv)
290 : {
291 1 : const struct CMUnitTest tests[] = {
292 : cmocka_unit_test(
293 : test_dnsserver_init_zoneinfo_master_servers_empty),
294 : cmocka_unit_test(
295 : test_dnsserver_init_zoneinfo_master_servers),
296 : cmocka_unit_test(
297 : test_dnsserver_init_zoneinfo_scavenging_servers_empty),
298 : cmocka_unit_test(
299 : test_dnsserver_init_zoneinfo_scavenging_servers),
300 : };
301 :
302 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
303 1 : return cmocka_run_group_tests(tests, NULL, NULL);
304 : }
|