Debian Codegraph @

src/resolve/test-dns-zone.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "dns-answer.h"
4 #include "dns-packet.h"
5 #include "dns-rr.h"
6 #include "dns-type.h"
7 #include "resolved-dns-scope.h"
8 #include "resolved-dns-zone.h"
9 #include "resolved-manager.h"
10 #include "tests.h"
11
12 static void dns_scope_freep(DnsScope **s) {
13 POINTER_MAY_BE_NULL(s);
14
15 if (s != NULL && *s != NULL)
16 dns_scope_free(*s);
17 }
18
19 /* ================================================================
20 * dns_zone_put()
21 * ================================================================ */
22
23 TEST(dns_zone_put_simple) {
24 Manager manager = {};
25 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
26 DnsZone *zone = NULL;
27 DnsZoneItem *item = NULL;
28 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
29
30 ASSERT_OK(dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET));
31 ASSERT_NOT_NULL(scope);
32 zone = &scope->zone;
33
34 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
35 ASSERT_NOT_NULL(rr);
36
37 ASSERT_TRUE(dns_zone_is_empty(zone));
38
39 ASSERT_OK(dns_zone_put(zone, scope, rr, 0));
40
41 ASSERT_FALSE(dns_zone_is_empty(zone));
42
43 item = dns_zone_get(zone, rr);
44 ASSERT_NOT_NULL(item);
45 ASSERT_EQ((int)item->state, DNS_ZONE_ITEM_ESTABLISHED);
46 }
47
48 TEST(dns_zone_put_any_class_is_invalid) {
49 Manager manager = {};
50 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
51 DnsZone *zone = NULL;
52 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
53
54 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
55 ASSERT_NOT_NULL(scope);
56 zone = &scope->zone;
57
58 rr = dns_resource_record_new_full(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com");
59 ASSERT_NOT_NULL(rr);
60
61 ASSERT_ERROR(dns_zone_put(zone, scope, rr, 0), EINVAL);
62
63 ASSERT_TRUE(dns_zone_is_empty(zone));
64 }
65
66 TEST(dns_zone_put_any_type_is_invalid) {
67 Manager manager = {};
68 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
69 DnsZone *zone = NULL;
70 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
71
72 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
73 ASSERT_NOT_NULL(scope);
74 zone = &scope->zone;
75
76 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com");
77 ASSERT_NOT_NULL(rr);
78
79 ASSERT_ERROR(dns_zone_put(zone, scope, rr, 0), EINVAL);
80
81 ASSERT_TRUE(dns_zone_is_empty(zone));
82 }
83
84 /* ================================================================
85 * dns_zone_remove_rr()
86 * ================================================================ */
87
88 TEST(dns_zone_remove_rr_match) {
89 Manager manager = {};
90 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
91 DnsZone *zone = NULL;
92 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr_in = NULL, *rr_out = NULL;
93
94 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
95 ASSERT_NOT_NULL(scope);
96 zone = &scope->zone;
97
98 rr_in = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
99 ASSERT_NOT_NULL(rr_in);
100 rr_in->a.in_addr.s_addr = htobe32(0xc0a8017f);
101
102 ASSERT_OK(dns_zone_put(zone, scope, rr_in, 0));
103
104 rr_out = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
105 ASSERT_NOT_NULL(rr_out);
106 rr_out->a.in_addr.s_addr = htobe32(0xc0a8017f);
107
108 ASSERT_NOT_NULL(dns_zone_get(zone, rr_in));
109 dns_zone_remove_rr(zone, rr_out);
110 ASSERT_NULL(dns_zone_get(zone, rr_in));
111 }
112
113 TEST(dns_zone_remove_rr_match_one) {
114 Manager manager = {};
115 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
116 DnsZone *zone = NULL;
117 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr_in = NULL, *rr_out = NULL;
118
119 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
120 ASSERT_NOT_NULL(scope);
121 zone = &scope->zone;
122
123 rr_in = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
124 ASSERT_NOT_NULL(rr_in);
125 rr_in->a.in_addr.s_addr = htobe32(0xc0a8017f);
126
127 ASSERT_OK(dns_zone_put(zone, scope, rr_in, 0));
128 dns_resource_record_unref(rr_in);
129
130 rr_in = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "example.com");
131 ASSERT_NOT_NULL(rr_in);
132 rr_in->cname.name = strdup("www.example.com");
133
134 ASSERT_OK(dns_zone_put(zone, scope, rr_in, 0));
135
136 rr_out = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
137 ASSERT_NOT_NULL(rr_out);
138 rr_out->a.in_addr.s_addr = htobe32(0xc0a8017f);
139
140 ASSERT_NOT_NULL(dns_zone_get(zone, rr_out));
141 dns_zone_remove_rr(zone, rr_out);
142 ASSERT_NULL(dns_zone_get(zone, rr_out));
143 ASSERT_NOT_NULL(dns_zone_get(zone, rr_in));
144 }
145
146 TEST(dns_zone_remove_rr_different_payload) {
147 Manager manager = {};
148 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
149 DnsZone *zone = NULL;
150 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr_in = NULL, *rr_out = NULL;
151
152 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
153 ASSERT_NOT_NULL(scope);
154 zone = &scope->zone;
155
156 rr_in = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
157 ASSERT_NOT_NULL(rr_in);
158 rr_in->a.in_addr.s_addr = htobe32(0xc0a8017f);
159
160 ASSERT_OK(dns_zone_put(zone, scope, rr_in, 0));
161
162 rr_out = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
163 ASSERT_NOT_NULL(rr_out);
164 rr_out->a.in_addr.s_addr = htobe32(0xc0a80180);
165
166 ASSERT_NOT_NULL(dns_zone_get(zone, rr_in));
167 dns_zone_remove_rr(zone, rr_out);
168 ASSERT_NOT_NULL(dns_zone_get(zone, rr_in));
169 }
170
171 /* ================================================================
172 * dns_zone_remove_rrs_by_key()
173 * ================================================================ */
174
175 TEST(dns_zone_remove_rrs_by_key) {
176 Manager manager = {};
177 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
178 DnsZone *zone = NULL;
179 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr1 = NULL, *rr2 = NULL, *rr3 = NULL;
180 DnsResourceKey *key = NULL;
181
182 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
183 ASSERT_NOT_NULL(scope);
184 zone = &scope->zone;
185
186 rr1 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
187 ASSERT_NOT_NULL(rr1);
188 dns_zone_put(zone, scope, rr1, 0);
189
190 rr2 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
191 ASSERT_NOT_NULL(rr2);
192 dns_zone_put(zone, scope, rr2, 0);
193
194 rr3 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "example.com");
195 ASSERT_NOT_NULL(rr3);
196 rr3->cname.name = strdup("www.example.com");
197 dns_zone_put(zone, scope, rr3, 0);
198
199 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
200 ASSERT_NOT_NULL(key);
201 ASSERT_OK(dns_zone_remove_rrs_by_key(zone, key));
202 ASSERT_NOT_NULL(dns_zone_get(zone, rr3));
203 dns_resource_key_unref(key);
204
205 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "example.com");
206 ASSERT_NOT_NULL(key);
207 ASSERT_OK(dns_zone_remove_rrs_by_key(zone, key));
208 ASSERT_NULL(dns_zone_get(zone, rr3));
209 dns_resource_key_unref(key);
210
211 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com");
212 ASSERT_NOT_NULL(key);
213 ASSERT_OK(dns_zone_remove_rrs_by_key(zone, key));
214 ASSERT_NULL(dns_zone_get(zone, rr1));
215 ASSERT_NULL(dns_zone_get(zone, rr2));
216 dns_resource_key_unref(key);
217 }
218
219 /* ================================================================
220 * dns_zone_lookup()
221 * ================================================================ */
222
223 static void add_zone_rrs(DnsScope *scope) {
224 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr1 = NULL, *rr2 = NULL, *rr3 = NULL, *rr4 = NULL;
225
226 rr1 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
227 ASSERT_NOT_NULL(rr1);
228 dns_zone_put(&scope->zone, scope, rr1, 0);
229
230 rr2 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
231 ASSERT_NOT_NULL(rr2);
232 dns_zone_put(&scope->zone, scope, rr2, 0);
233
234 rr3 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "example.com");
235 ASSERT_NOT_NULL(rr3);
236 rr3->cname.name = strdup("www.example.com");
237 dns_zone_put(&scope->zone, scope, rr3, 0);
238
239 rr4 = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NS, "app.example.com");
240 ASSERT_NOT_NULL(rr4);
241 rr4->cname.name = strdup("ns1.app.example.com");
242 dns_zone_put(&scope->zone, scope, rr4, 0);
243 }
244
245 TEST(dns_zone_lookup_match_a) {
246 Manager manager = {};
247 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
248 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *qkey = NULL;
249 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
250 bool tentative;
251
252 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
253 ASSERT_NOT_NULL(scope);
254 add_zone_rrs(scope);
255
256 qkey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
257 ASSERT_NOT_NULL(qkey);
258 ASSERT_TRUE(dns_zone_lookup(&scope->zone, qkey, 1, &answer, &soa, &tentative));
259 ASSERT_FALSE(tentative);
260
261 ASSERT_EQ(dns_answer_size(answer), 1u);
262 ASSERT_EQ(dns_answer_size(soa), 0u);
263
264 ASSERT_TRUE(dns_answer_match_key(answer, qkey, NULL));
265 }
266
267 TEST(dns_zone_lookup_match_cname) {
268 Manager manager = {};
269 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
270 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *qkey = NULL;
271 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
272 bool tentative;
273
274 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
275 ASSERT_NOT_NULL(scope);
276 add_zone_rrs(scope);
277
278 qkey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "example.com");
279 ASSERT_NOT_NULL(qkey);
280 ASSERT_TRUE(dns_zone_lookup(&scope->zone, qkey, 1, &answer, &soa, &tentative));
281 ASSERT_FALSE(tentative);
282
283 ASSERT_EQ(dns_answer_size(answer), 1u);
284 ASSERT_EQ(dns_answer_size(soa), 0u);
285
286 ASSERT_TRUE(dns_answer_match_key(answer, qkey, NULL));
287 }
288
289 TEST(dns_zone_lookup_match_any) {
290 Manager manager = {};
291 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
292 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *qkey = NULL;
293 DnsResourceKey *akey = NULL;
294 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
295 bool tentative;
296
297 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
298 ASSERT_NOT_NULL(scope);
299 add_zone_rrs(scope);
300
301 qkey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com");
302 ASSERT_NOT_NULL(qkey);
303 ASSERT_TRUE(dns_zone_lookup(&scope->zone, qkey, 1, &answer, &soa, &tentative));
304 ASSERT_FALSE(tentative);
305
306 ASSERT_EQ(dns_answer_size(answer), 2u);
307 ASSERT_EQ(dns_answer_size(soa), 0u);
308
309 akey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
310 ASSERT_NOT_NULL(akey);
311 ASSERT_TRUE(dns_answer_match_key(answer, akey, NULL));
312 dns_resource_key_unref(akey);
313
314 akey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
315 ASSERT_NOT_NULL(akey);
316 ASSERT_TRUE(dns_answer_match_key(answer, akey, NULL));
317 dns_resource_key_unref(akey);
318 }
319
320 TEST(dns_zone_lookup_match_any_apex) {
321 Manager manager = {};
322 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
323 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *qkey = NULL;
324 DnsResourceKey *akey = NULL;
325 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
326 bool tentative;
327
328 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
329 ASSERT_NOT_NULL(scope);
330 add_zone_rrs(scope);
331
332 qkey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "example.com");
333 ASSERT_NOT_NULL(qkey);
334 ASSERT_TRUE(dns_zone_lookup(&scope->zone, qkey, 1, &answer, &soa, &tentative));
335 ASSERT_FALSE(tentative);
336
337 ASSERT_EQ(dns_answer_size(answer), 1u);
338 ASSERT_EQ(dns_answer_size(soa), 0u);
339
340 akey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "example.com");
341 ASSERT_NOT_NULL(akey);
342 ASSERT_TRUE(dns_answer_match_key(answer, akey, NULL));
343 dns_resource_key_unref(akey);
344 }
345
346 TEST(dns_zone_lookup_match_nothing) {
347 Manager manager = {};
348 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
349 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *qkey = NULL;
350 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
351 bool tentative;
352
353 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
354 ASSERT_NOT_NULL(scope);
355 add_zone_rrs(scope);
356
357 qkey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "nope.example.com");
358 ASSERT_NOT_NULL(qkey);
359 ASSERT_FALSE(dns_zone_lookup(&scope->zone, qkey, 1, &answer, &soa, &tentative));
360 ASSERT_FALSE(tentative);
361
362 ASSERT_EQ(dns_answer_size(answer), 0u);
363 ASSERT_EQ(dns_answer_size(soa), 0u);
364 }
365
366 TEST(dns_zone_lookup_match_nothing_with_soa) {
367 Manager manager = {};
368 _cleanup_(dns_scope_freep) DnsScope *scope = NULL;
369 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *qkey = NULL;
370 DnsResourceKey *akey = NULL;
371 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
372 bool tentative;
373
374 dns_scope_new(&manager, &scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_INET);
375 ASSERT_NOT_NULL(scope);
376 add_zone_rrs(scope);
377
378 qkey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
379 ASSERT_NOT_NULL(qkey);
380 ASSERT_TRUE(dns_zone_lookup(&scope->zone, qkey, 1, &answer, &soa, &tentative));
381 ASSERT_FALSE(tentative);
382
383 ASSERT_EQ(dns_answer_size(answer), 0u);
384 ASSERT_EQ(dns_answer_size(soa), 1u);
385
386 akey = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "example.com");
387 ASSERT_NOT_NULL(akey);
388 ASSERT_TRUE(dns_answer_match_key(soa, akey, NULL));
389 dns_resource_key_unref(akey);
390 }
391
392 DEFINE_TEST_MAIN(LOG_DEBUG);