Debian Codegraph @

src/resolve/test-dns-rr.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-json.h"
4
5 #include "dns-packet.h"
6 #include "dns-rr.h"
7 #include "dns-type.h"
8 #include "tests.h"
9
10 static void test_to_json_from_json(DnsResourceRecord *rr) {
11 _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
12 ASSERT_OK(dns_resource_record_to_json(rr, &j));
13
14 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr2 = NULL;
15 ASSERT_OK(dns_resource_record_from_json(j, &rr2));
16
17 ASSERT_TRUE(dns_resource_record_equal(rr, rr2));
18 }
19
20 /* ================================================================
21 * DNS_RESOURCE_RECORD_RDATA()
22 * ================================================================ */
23
24 TEST(dns_resource_record_rdata) {
25 DnsResourceRecord rr = (DnsResourceRecord) {
26 .wire_format = (void *)"abcdefghi",
27 .wire_format_size = 9,
28 .wire_format_rdata_offset = 3
29 };
30
31 const void *ptr = DNS_RESOURCE_RECORD_RDATA(&rr);
32 ASSERT_STREQ(ptr, "defghi");
33
34 size_t size = DNS_RESOURCE_RECORD_RDATA_SIZE(&rr);
35 ASSERT_EQ(size, 6u);
36
37 rr.wire_format = NULL;
38
39 ptr = DNS_RESOURCE_RECORD_RDATA(&rr);
40 ASSERT_NULL(ptr);
41
42 size = DNS_RESOURCE_RECORD_RDATA_SIZE(&rr);
43 ASSERT_EQ(size, 0u);
44 }
45
46 /* ================================================================
47 * dns_resource_key_new()
48 * ================================================================ */
49
50 TEST(dns_resource_key_new) {
51 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
52
53 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
54 ASSERT_NOT_NULL(key);
55
56 ASSERT_EQ(key->class, DNS_CLASS_IN);
57 ASSERT_EQ(key->type, DNS_TYPE_A);
58 ASSERT_STREQ(dns_resource_key_name(key), "www.example.com");
59 }
60
61 /* ================================================================
62 * dns_resource_key_new_redirect()
63 * ================================================================ */
64
65 TEST(dns_resource_key_new_redirect_cname) {
66 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *redirected = NULL;
67 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL;
68
69 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
70 ASSERT_NOT_NULL(key);
71
72 cname = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
73 ASSERT_NOT_NULL(cname);
74 cname->cname.name = strdup("example.com");
75
76 redirected = dns_resource_key_new_redirect(key, cname);
77 ASSERT_NOT_NULL(redirected);
78
79 ASSERT_EQ(redirected->class, DNS_CLASS_IN);
80 ASSERT_EQ(redirected->type, DNS_TYPE_A);
81 ASSERT_STREQ(dns_resource_key_name(redirected), "example.com");
82 }
83
84 TEST(dns_resource_key_new_redirect_cname_no_match) {
85 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *redirected = NULL;
86 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL;
87
88 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "mail.example.com");
89 ASSERT_NOT_NULL(key);
90
91 cname = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
92 ASSERT_NOT_NULL(cname);
93 cname->cname.name = strdup("example.com");
94
95 redirected = dns_resource_key_new_redirect(key, cname);
96 ASSERT_NOT_NULL(redirected);
97
98 ASSERT_EQ(redirected->class, DNS_CLASS_IN);
99 ASSERT_EQ(redirected->type, DNS_TYPE_A);
100 ASSERT_STREQ(dns_resource_key_name(redirected), "example.com");
101 }
102
103 TEST(dns_resource_key_new_redirect_dname) {
104 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *redirected = NULL;
105 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *dname = NULL;
106
107 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
108 ASSERT_NOT_NULL(key);
109
110 dname = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "example.com");
111 ASSERT_NOT_NULL(dname);
112 dname->dname.name = strdup("v2.example.com");
113
114 redirected = dns_resource_key_new_redirect(key, dname);
115 ASSERT_NOT_NULL(redirected);
116
117 ASSERT_EQ(redirected->class, DNS_CLASS_IN);
118 ASSERT_EQ(redirected->type, DNS_TYPE_A);
119 ASSERT_STREQ(dns_resource_key_name(redirected), "www.v2.example.com");
120 }
121
122 TEST(dns_resource_key_new_redirect_dname_no_match) {
123 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *redirected = NULL;
124 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *dname = NULL;
125
126 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.examples.com");
127 ASSERT_NOT_NULL(key);
128
129 dname = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "example.com");
130 ASSERT_NOT_NULL(dname);
131 dname->dname.name = strdup("v2.example.com");
132
133 redirected = dns_resource_key_new_redirect(key, dname);
134 ASSERT_NOT_NULL(redirected);
135
136 ASSERT_EQ(redirected->class, DNS_CLASS_IN);
137 ASSERT_EQ(redirected->type, DNS_TYPE_A);
138 ASSERT_STREQ(dns_resource_key_name(redirected), "www.examples.com");
139 }
140
141 /* ================================================================
142 * dns_resource_key_new_append_suffix()
143 * ================================================================ */
144
145 TEST(dns_resource_key_new_append_suffix_root) {
146 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *source = NULL, *target = NULL;
147
148 source = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
149 ASSERT_NOT_NULL(source);
150
151 ASSERT_OK(dns_resource_key_new_append_suffix(&target, source, (char *)""));
152 ASSERT_NOT_NULL(target);
153 ASSERT_TRUE(target == source);
154
155 ASSERT_OK(dns_resource_key_new_append_suffix(&target, source, (char *)"."));
156 ASSERT_NOT_NULL(target);
157 ASSERT_TRUE(target == source);
158
159 dns_resource_key_unref(source);
160 }
161
162 TEST(dns_resource_key_new_append_suffix_not_root) {
163 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *source = NULL, *target = NULL;
164
165 source = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example");
166 ASSERT_NOT_NULL(source);
167
168 ASSERT_OK(dns_resource_key_new_append_suffix(&target, source, (char *)"com"));
169 ASSERT_NOT_NULL(target);
170 ASSERT_FALSE(target == source);
171 ASSERT_STREQ(dns_resource_key_name(target), "www.example.com");
172 }
173
174 /* ================================================================
175 * dns_resource_key_is_*()
176 * ================================================================ */
177
178 TEST(dns_resource_key_is_address) {
179 DnsResourceKey *key = NULL;
180
181 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
182 ASSERT_NOT_NULL(key);
183 ASSERT_TRUE(dns_resource_key_is_address(key));
184 dns_resource_key_unref(key);
185
186 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
187 ASSERT_NOT_NULL(key);
188 ASSERT_TRUE(dns_resource_key_is_address(key));
189 dns_resource_key_unref(key);
190
191 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A6, "www.example.com");
192 ASSERT_NOT_NULL(key);
193 ASSERT_FALSE(dns_resource_key_is_address(key));
194 dns_resource_key_unref(key);
195
196 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
197 ASSERT_NOT_NULL(key);
198 ASSERT_FALSE(dns_resource_key_is_address(key));
199 dns_resource_key_unref(key);
200 }
201
202 TEST(dns_resource_key_is_dnssd_ptr) {
203 DnsResourceKey *key = NULL;
204
205 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "_tcp.local");
206 ASSERT_NOT_NULL(key);
207 ASSERT_TRUE(dns_resource_key_is_dnssd_ptr(key));
208 dns_resource_key_unref(key);
209
210 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "foo._tcp.local");
211 ASSERT_NOT_NULL(key);
212 ASSERT_TRUE(dns_resource_key_is_dnssd_ptr(key));
213 dns_resource_key_unref(key);
214
215 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "_udp.local");
216 ASSERT_NOT_NULL(key);
217 ASSERT_TRUE(dns_resource_key_is_dnssd_ptr(key));
218 dns_resource_key_unref(key);
219
220 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "bar._udp.local");
221 ASSERT_NOT_NULL(key);
222 ASSERT_TRUE(dns_resource_key_is_dnssd_ptr(key));
223 dns_resource_key_unref(key);
224
225 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "_tcp.local");
226 ASSERT_NOT_NULL(key);
227 ASSERT_FALSE(dns_resource_key_is_dnssd_ptr(key));
228 dns_resource_key_unref(key);
229
230 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "_abc.local");
231 ASSERT_NOT_NULL(key);
232 ASSERT_FALSE(dns_resource_key_is_dnssd_ptr(key));
233 dns_resource_key_unref(key);
234
235 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "foo_tcp.local");
236 ASSERT_NOT_NULL(key);
237 ASSERT_FALSE(dns_resource_key_is_dnssd_ptr(key));
238 dns_resource_key_unref(key);
239 }
240
241 TEST(dns_resource_key_is_dnssd_two_label_ptr) {
242 DnsResourceKey *key = NULL;
243
244 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "_tcp.local");
245 ASSERT_NOT_NULL(key);
246 ASSERT_FALSE(dns_resource_key_is_dnssd_two_label_ptr(key));
247 dns_resource_key_unref(key);
248
249 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "foo._tcp.local");
250 ASSERT_NOT_NULL(key);
251 ASSERT_TRUE(dns_resource_key_is_dnssd_two_label_ptr(key));
252 dns_resource_key_unref(key);
253
254 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "_udp.local");
255 ASSERT_NOT_NULL(key);
256 ASSERT_FALSE(dns_resource_key_is_dnssd_two_label_ptr(key));
257 dns_resource_key_unref(key);
258
259 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "bar._udp.local");
260 ASSERT_NOT_NULL(key);
261 ASSERT_TRUE(dns_resource_key_is_dnssd_two_label_ptr(key));
262 dns_resource_key_unref(key);
263
264 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "foo._tcp.local");
265 ASSERT_NOT_NULL(key);
266 ASSERT_FALSE(dns_resource_key_is_dnssd_two_label_ptr(key));
267 dns_resource_key_unref(key);
268
269 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "foo._abc.local");
270 ASSERT_NOT_NULL(key);
271 ASSERT_FALSE(dns_resource_key_is_dnssd_two_label_ptr(key));
272 dns_resource_key_unref(key);
273
274 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "foo_tcp.local");
275 ASSERT_NOT_NULL(key);
276 ASSERT_FALSE(dns_resource_key_is_dnssd_two_label_ptr(key));
277 dns_resource_key_unref(key);
278 }
279
280 /* ================================================================
281 * dns_resource_key_equal()
282 * ================================================================ */
283
284 TEST(dns_resource_key_equal_same_pointer) {
285 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL;
286
287 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
288 ASSERT_NOT_NULL(a);
289
290 ASSERT_TRUE(dns_resource_key_equal(a, a));
291 }
292
293 TEST(dns_resource_key_equal_equal_name) {
294 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL;
295
296 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
297 ASSERT_NOT_NULL(a);
298
299 b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
300 ASSERT_NOT_NULL(b);
301
302 ASSERT_TRUE(dns_resource_key_equal(a, b));
303 }
304
305 TEST(dns_resource_key_equal_case_insensitive_name) {
306 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL;
307
308 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
309 ASSERT_NOT_NULL(a);
310
311 b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.EXAMPLE.com");
312 ASSERT_NOT_NULL(b);
313
314 ASSERT_TRUE(dns_resource_key_equal(a, b));
315 }
316
317 TEST(dns_resource_key_equal_trailing_dot) {
318 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL;
319
320 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
321 ASSERT_NOT_NULL(a);
322
323 b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com.");
324 ASSERT_NOT_NULL(b);
325
326 ASSERT_TRUE(dns_resource_key_equal(a, b));
327 }
328
329 TEST(dns_resource_key_equal_different_names) {
330 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL;
331
332 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
333 ASSERT_NOT_NULL(a);
334
335 b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.org");
336 ASSERT_NOT_NULL(b);
337
338 ASSERT_FALSE(dns_resource_key_equal(a, b));
339 }
340
341 TEST(dns_resource_key_equal_different_classes) {
342 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL;
343
344 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
345 ASSERT_NOT_NULL(a);
346
347 b = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com");
348 ASSERT_NOT_NULL(b);
349
350 ASSERT_FALSE(dns_resource_key_equal(a, b));
351 }
352
353 TEST(dns_resource_key_equal_different_types) {
354 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL;
355
356 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
357 ASSERT_NOT_NULL(a);
358
359 b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
360 ASSERT_NOT_NULL(b);
361
362 ASSERT_FALSE(dns_resource_key_equal(a, b));
363 }
364
365 /* ================================================================
366 * dns_resource_key_match_rr()
367 * ================================================================ */
368
369 TEST(dns_resource_key_match_rr_simple) {
370 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
371 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
372
373 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
374 ASSERT_NOT_NULL(key);
375 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
376 ASSERT_NOT_NULL(rr);
377
378 ASSERT_TRUE(dns_resource_key_match_rr(key, rr, NULL));
379 }
380
381 TEST(dns_resource_key_match_rr_any_class) {
382 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
383 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
384
385 key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com");
386 ASSERT_NOT_NULL(key);
387 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
388 ASSERT_NOT_NULL(rr);
389
390 ASSERT_TRUE(dns_resource_key_match_rr(key, rr, NULL));
391 }
392
393 TEST(dns_resource_key_match_rr_any_type) {
394 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
395 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
396
397 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com");
398 ASSERT_NOT_NULL(key);
399 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
400 ASSERT_NOT_NULL(rr);
401
402 ASSERT_TRUE(dns_resource_key_match_rr(key, rr, NULL));
403 }
404
405 TEST(dns_resource_key_match_rr_different_type) {
406 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
407 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
408
409 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
410 ASSERT_NOT_NULL(key);
411 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
412 ASSERT_NOT_NULL(rr);
413
414 ASSERT_FALSE(dns_resource_key_match_rr(key, rr, NULL));
415 }
416
417 TEST(dns_resource_key_match_rr_different_name) {
418 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
419 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
420
421 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.other.com");
422 ASSERT_NOT_NULL(key);
423 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
424 ASSERT_NOT_NULL(rr);
425
426 ASSERT_FALSE(dns_resource_key_match_rr(key, rr, NULL));
427 }
428
429 TEST(dns_resource_key_match_rr_case_insensitive_name) {
430 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
431 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
432
433 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.EXAMPLE.com");
434 ASSERT_NOT_NULL(key);
435 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
436 ASSERT_NOT_NULL(rr);
437
438 ASSERT_TRUE(dns_resource_key_match_rr(key, rr, NULL));
439 }
440
441 TEST(dns_resource_key_match_rr_escape_error) {
442 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
443 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
444
445 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.\\example.com");
446 ASSERT_NOT_NULL(key);
447 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
448 ASSERT_NOT_NULL(rr);
449
450 ASSERT_ERROR(dns_resource_key_match_rr(key, rr, NULL), EINVAL);
451 }
452
453 TEST(dns_resource_key_match_rr_search_domain) {
454 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
455 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
456
457 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example");
458 ASSERT_NOT_NULL(key);
459 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
460 ASSERT_NOT_NULL(rr);
461
462 ASSERT_TRUE(dns_resource_key_match_rr(key, rr, "com"));
463 }
464
465 TEST(dns_resource_key_match_rr_no_search_domain) {
466 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
467 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
468
469 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example");
470 ASSERT_NOT_NULL(key);
471 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
472 ASSERT_NOT_NULL(rr);
473
474 ASSERT_FALSE(dns_resource_key_match_rr(key, rr, NULL));
475 }
476
477 TEST(dns_resource_key_match_rr_different_search_domain) {
478 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
479 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
480
481 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example");
482 ASSERT_NOT_NULL(key);
483 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
484 ASSERT_NOT_NULL(rr);
485
486 ASSERT_FALSE(dns_resource_key_match_rr(key, rr, "org"));
487 }
488
489 /* ================================================================
490 * dns_resource_key_match_cname_or_dname()
491 * ================================================================ */
492
493 TEST(dns_resource_key_match_cname_or_dname_simple) {
494 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
495
496 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
497 ASSERT_NOT_NULL(key);
498 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
499 ASSERT_NOT_NULL(cname);
500
501 ASSERT_TRUE(dns_resource_key_match_cname_or_dname(key, cname, NULL));
502 }
503
504 TEST(dns_resource_key_match_cname_or_dname_any_class) {
505 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
506
507 key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com");
508 ASSERT_NOT_NULL(key);
509 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
510 ASSERT_NOT_NULL(cname);
511
512 ASSERT_TRUE(dns_resource_key_match_cname_or_dname(key, cname, NULL));
513 }
514
515 TEST(dns_resource_key_match_cname_or_dname_bad_type) {
516 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
517
518 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_NSEC, "www.example.com");
519 ASSERT_NOT_NULL(key);
520 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
521 ASSERT_NOT_NULL(cname);
522
523 ASSERT_FALSE(dns_resource_key_match_cname_or_dname(key, cname, NULL));
524 }
525
526 TEST(dns_resource_key_match_cname_or_dname_case_insensitive_cname) {
527 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
528
529 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.EXAMPLE.com");
530 ASSERT_NOT_NULL(key);
531 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
532 ASSERT_NOT_NULL(cname);
533
534 ASSERT_TRUE(dns_resource_key_match_cname_or_dname(key, cname, NULL));
535 }
536
537 TEST(dns_resource_key_match_cname_or_dname_prefix_cname) {
538 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
539
540 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
541 ASSERT_NOT_NULL(key);
542 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "example.com");
543 ASSERT_NOT_NULL(cname);
544
545 ASSERT_FALSE(dns_resource_key_match_cname_or_dname(key, cname, NULL));
546 }
547
548 TEST(dns_resource_key_match_cname_or_dname_suffix_cname) {
549 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
550
551 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
552 ASSERT_NOT_NULL(key);
553 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
554 ASSERT_NOT_NULL(cname);
555
556 ASSERT_FALSE(dns_resource_key_match_cname_or_dname(key, cname, NULL));
557 }
558
559 TEST(dns_resource_key_match_cname_or_dname_search_domain_cname_pass) {
560 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
561
562 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example");
563 ASSERT_NOT_NULL(key);
564 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
565 ASSERT_NOT_NULL(cname);
566
567 ASSERT_TRUE(dns_resource_key_match_cname_or_dname(key, cname, "com"));
568 }
569
570 TEST(dns_resource_key_match_cname_or_dname_search_domain_cname_fail) {
571 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
572
573 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example");
574 ASSERT_NOT_NULL(key);
575 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
576 ASSERT_NOT_NULL(cname);
577
578 ASSERT_FALSE(dns_resource_key_match_cname_or_dname(key, cname, "org"));
579 }
580
581 TEST(dns_resource_key_match_cname_or_dname_case_insensitive_dname) {
582 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
583
584 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.EXAMPLE.com");
585 ASSERT_NOT_NULL(key);
586 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DNAME, "www.example.com");
587 ASSERT_NOT_NULL(cname);
588
589 ASSERT_TRUE(dns_resource_key_match_cname_or_dname(key, cname, NULL));
590 }
591
592 TEST(dns_resource_key_match_cname_or_dname_prefix_dname) {
593 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
594
595 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
596 ASSERT_NOT_NULL(key);
597 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DNAME, "example.com");
598 ASSERT_NOT_NULL(cname);
599
600 ASSERT_TRUE(dns_resource_key_match_cname_or_dname(key, cname, NULL));
601 }
602
603 TEST(dns_resource_key_match_cname_or_dname_suffix_dname) {
604 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
605
606 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
607 ASSERT_NOT_NULL(key);
608 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DNAME, "www.example.com");
609 ASSERT_NOT_NULL(cname);
610
611 ASSERT_FALSE(dns_resource_key_match_cname_or_dname(key, cname, NULL));
612 }
613
614 TEST(dns_resource_key_match_cname_or_dname_search_domain_dname_pass) {
615 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
616
617 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example");
618 ASSERT_NOT_NULL(key);
619 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DNAME, "example.com");
620 ASSERT_NOT_NULL(cname);
621
622 ASSERT_TRUE(dns_resource_key_match_cname_or_dname(key, cname, "com"));
623 }
624
625 TEST(dns_resource_key_match_cname_or_dname_search_domain_dname_fail) {
626 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *cname = NULL;
627
628 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example");
629 ASSERT_NOT_NULL(key);
630 cname = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DNAME, "example.com");
631 ASSERT_NOT_NULL(cname);
632
633 ASSERT_FALSE(dns_resource_key_match_cname_or_dname(key, cname, "org"));
634 }
635
636 /* ================================================================
637 * dns_resource_key_match_soa()
638 * ================================================================ */
639
640 TEST(dns_resource_key_match_soa_simple) {
641 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *soa = NULL;
642
643 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
644 ASSERT_NOT_NULL(key);
645 soa = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
646 ASSERT_NOT_NULL(soa);
647
648 ASSERT_TRUE(dns_resource_key_match_soa(key, soa));
649 }
650
651 TEST(dns_resource_key_no_match_soa_any_class) {
652 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *soa = NULL;
653
654 key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com");
655 ASSERT_NOT_NULL(key);
656 soa = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
657 ASSERT_NOT_NULL(soa);
658
659 ASSERT_FALSE(dns_resource_key_match_soa(key, soa));
660 }
661
662 TEST(dns_resource_key_no_match_soa_bad_type) {
663 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *soa = NULL;
664
665 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
666 ASSERT_NOT_NULL(key);
667 soa = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
668 ASSERT_NOT_NULL(soa);
669
670 ASSERT_FALSE(dns_resource_key_match_soa(key, soa));
671 }
672
673 TEST(dns_resource_key_match_soa_child_domain) {
674 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *soa = NULL;
675
676 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
677 ASSERT_NOT_NULL(key);
678 soa = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "example.com");
679 ASSERT_NOT_NULL(soa);
680
681 ASSERT_TRUE(dns_resource_key_match_soa(key, soa));
682 }
683
684 TEST(dns_resource_key_no_match_soa_parent_domain) {
685 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *soa = NULL;
686
687 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
688 ASSERT_NOT_NULL(key);
689 soa = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
690 ASSERT_NOT_NULL(soa);
691
692 ASSERT_FALSE(dns_resource_key_match_soa(key, soa));
693 }
694
695 /* ================================================================
696 * dns_resource_key_to_string()
697 * ================================================================ */
698
699 TEST(dns_resource_key_to_string) {
700 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
701 char str[256];
702
703 ASSERT_NOT_NULL(key);
704
705 char *ans = dns_resource_key_to_string(key, str, 256);
706 ASSERT_TRUE(ans == str);
707 ASSERT_STREQ(ans, "www.example.com IN CNAME");
708 }
709
710 /* ================================================================
711 * dns_resource_key_{to,from}_json()
712 * ================================================================ */
713
714 TEST(dns_resource_key_from_json) {
715 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL, *copy = NULL;
716 _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
717
718 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
719 ASSERT_NOT_NULL(key);
720
721 ASSERT_OK(dns_resource_key_to_json(key, &json));
722 ASSERT_NOT_NULL(json);
723
724 ASSERT_OK(dns_resource_key_from_json(json, &copy));
725 ASSERT_EQ(copy->class, DNS_CLASS_IN);
726 ASSERT_EQ(copy->type, DNS_TYPE_A);
727 ASSERT_STREQ(dns_resource_key_name(copy), "www.example.com");
728 }
729
730 /* ================================================================
731 * dns_resource_key_reduce()
732 * ================================================================ */
733
734 TEST(dns_resource_key_reduce_same_pointer) {
735 DnsResourceKey *a = NULL, *b = NULL;
736
737 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
738 ASSERT_NOT_NULL(a);
739 b = a;
740
741 ASSERT_TRUE(dns_resource_key_reduce(&a, &b));
742 ASSERT_TRUE(a == b);
743
744 dns_resource_key_unref(a);
745 }
746
747 TEST(dns_resource_key_reduce_same_values) {
748 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL;
749
750 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
751 ASSERT_NOT_NULL(a);
752 b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
753 ASSERT_NOT_NULL(b);
754
755 ASSERT_TRUE(a != b);
756
757 ASSERT_TRUE(dns_resource_key_reduce(&a, &b));
758 ASSERT_TRUE(a == b);
759 }
760
761 TEST(dns_resource_key_reduce_different_values) {
762 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL;
763
764 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
765 ASSERT_NOT_NULL(a);
766 b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
767 ASSERT_NOT_NULL(b);
768
769 ASSERT_TRUE(a != b);
770
771 ASSERT_FALSE(dns_resource_key_reduce(&a, &b));
772 ASSERT_TRUE(a != b);
773 }
774
775 TEST(dns_resource_key_reduce_refcount) {
776 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL, *c = NULL;
777
778 a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
779 ASSERT_NOT_NULL(a);
780 b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
781 ASSERT_NOT_NULL(b);
782 c = b;
783
784 ASSERT_TRUE(a != b);
785
786 a->n_ref = 3;
787 b->n_ref = 2;
788
789 ASSERT_TRUE(dns_resource_key_reduce(&a, &b));
790 ASSERT_TRUE(a == b);
791
792 ASSERT_EQ(a->n_ref, 4u);
793 ASSERT_EQ(c->n_ref, 1u);
794
795 /* set refcount so that objects will be freed */
796 a->n_ref = 2;
797 }
798
799 /* ================================================================
800 * dns_resource_record_new_address()
801 * ================================================================ */
802
803 TEST(dns_resource_record_new_address_ipv4) {
804 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
805
806 union in_addr_union addr = { .in.s_addr = htobe32(0xc0a8017f) };
807
808 ASSERT_OK(dns_resource_record_new_address(&rr, AF_INET, &addr, "www.example.com"));
809 ASSERT_NOT_NULL(rr);
810
811 ASSERT_EQ(rr->key->class, DNS_CLASS_IN);
812 ASSERT_EQ(rr->key->type, DNS_TYPE_A);
813 ASSERT_STREQ(dns_resource_key_name(rr->key), "www.example.com");
814 ASSERT_EQ(rr->a.in_addr.s_addr, addr.in.s_addr);
815
816 test_to_json_from_json(rr);
817 }
818
819 TEST(dns_resource_record_new_address_ipv6) {
820 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
821
822 union in_addr_union addr = {
823 .in6.s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 }
824 };
825
826 ASSERT_OK(dns_resource_record_new_address(&rr, AF_INET6, &addr, "www.example.com"));
827 ASSERT_NOT_NULL(rr);
828
829 ASSERT_EQ(rr->key->class, DNS_CLASS_IN);
830 ASSERT_EQ(rr->key->type, DNS_TYPE_AAAA);
831 ASSERT_STREQ(dns_resource_key_name(rr->key), "www.example.com");
832 ASSERT_EQ(memcmp(&rr->aaaa.in6_addr, &addr.in6, sizeof(struct in6_addr)), 0);
833
834 test_to_json_from_json(rr);
835 }
836
837 /* ================================================================
838 * dns_resource_record_new_reverse()
839 * ================================================================ */
840
841 TEST(dns_resource_record_new_reverse) {
842 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
843
844 union in_addr_union addr = { .in.s_addr = htobe32(0xc0a8017f) };
845
846 ASSERT_OK(dns_resource_record_new_reverse(&rr, AF_INET, &addr, "www.example.com"));
847 ASSERT_NOT_NULL(rr);
848
849 ASSERT_EQ(rr->key->class, DNS_CLASS_IN);
850 ASSERT_EQ(rr->key->type, DNS_TYPE_PTR);
851 ASSERT_STREQ(dns_resource_key_name(rr->key), "127.1.168.192.in-addr.arpa");
852 ASSERT_STREQ(rr->ptr.name, "www.example.com");
853 }
854
855 /* ================================================================
856 * dns_resource_record_equal() : general cases
857 * ================================================================ */
858
859 TEST(dns_resource_record_equal_same_pointer) {
860 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL;
861
862 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
863 ASSERT_NOT_NULL(a);
864 ASSERT_TRUE(dns_resource_record_equal(a, a));
865 }
866
867 TEST(dns_resource_record_equal_equal_name) {
868 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
869
870 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
871 ASSERT_NOT_NULL(a);
872 b = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
873 ASSERT_NOT_NULL(b);
874 ASSERT_TRUE(dns_resource_record_equal(a, b));
875 }
876
877 TEST(dns_resource_record_equal_case_insensitive_name) {
878 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
879
880 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
881 ASSERT_NOT_NULL(a);
882 b = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.EXAMPLE.com");
883 ASSERT_NOT_NULL(b);
884 ASSERT_TRUE(dns_resource_record_equal(a, b));
885 }
886
887 TEST(dns_resource_record_equal_trailing_dot) {
888 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
889
890 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
891 ASSERT_NOT_NULL(a);
892 b = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com.");
893 ASSERT_NOT_NULL(b);
894 ASSERT_TRUE(dns_resource_record_equal(a, b));
895 }
896
897 TEST(dns_resource_record_equal_different_names) {
898 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
899
900 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
901 ASSERT_NOT_NULL(a);
902 b = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.org");
903 ASSERT_NOT_NULL(b);
904 ASSERT_FALSE(dns_resource_record_equal(a, b));
905 }
906
907 TEST(dns_resource_record_equal_different_classes) {
908 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
909
910 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
911 ASSERT_NOT_NULL(a);
912 b = dns_resource_record_new_full(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com");
913 ASSERT_NOT_NULL(b);
914 ASSERT_FALSE(dns_resource_record_equal(a, b));
915 }
916
917 TEST(dns_resource_record_equal_different_types) {
918 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
919
920 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
921 ASSERT_NOT_NULL(a);
922 b = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
923 ASSERT_NOT_NULL(b);
924 ASSERT_FALSE(dns_resource_record_equal(a, b));
925 }
926
927 /* ================================================================
928 * dns_resource_record_equal() : A, AAAA
929 * ================================================================ */
930
931 TEST(dns_resource_record_equal_a_copy) {
932 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
933
934 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
935 ASSERT_NOT_NULL(a);
936 a->a.in_addr.s_addr = htobe32(0xc0a8017f);
937
938 b = dns_resource_record_copy(a);
939 ASSERT_NOT_NULL(b);
940 ASSERT_TRUE(dns_resource_record_equal(a, b));
941 }
942
943 TEST(dns_resource_record_equal_a_fail) {
944 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
945
946 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
947 ASSERT_NOT_NULL(a);
948 a->a.in_addr.s_addr = htobe32(0xc0a8017f);
949
950 b = dns_resource_record_copy(a);
951 ASSERT_NOT_NULL(b);
952 b->a.in_addr.s_addr = htobe32(0xc0a80180);
953 ASSERT_FALSE(dns_resource_record_equal(a, b));
954 }
955
956 TEST(dns_resource_record_equal_aaaa_copy) {
957 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
958
959 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
960 ASSERT_NOT_NULL(a);
961 a->aaaa.in6_addr = (struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 } };
962
963 b = dns_resource_record_copy(a);
964 ASSERT_NOT_NULL(b);
965 ASSERT_TRUE(dns_resource_record_equal(a, b));
966 }
967
968 TEST(dns_resource_record_equal_aaaa_fail) {
969 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
970
971 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
972 ASSERT_NOT_NULL(a);
973 a->aaaa.in6_addr = (struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 } };
974
975 b = dns_resource_record_copy(a);
976 ASSERT_NOT_NULL(b);
977 b->aaaa.in6_addr = (struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04 } };
978 ASSERT_FALSE(dns_resource_record_equal(a, b));
979 }
980
981 /* ================================================================
982 * dns_resource_record_equal() : NS
983 * ================================================================ */
984
985 TEST(dns_resource_record_equal_ns_copy) {
986 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
987
988 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NS, "www.example.com");
989 ASSERT_NOT_NULL(a);
990 a->ns.name = strdup("ns1.example.com");
991
992 b = dns_resource_record_copy(a);
993 ASSERT_NOT_NULL(b);
994 ASSERT_TRUE(dns_resource_record_equal(a, b));
995 }
996
997 TEST(dns_resource_record_equal_ns_fail) {
998 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
999
1000 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NS, "www.example.com");
1001 ASSERT_NOT_NULL(a);
1002 a->ns.name = strdup("ns1.example.com");
1003
1004 b = dns_resource_record_copy(a);
1005 ASSERT_NOT_NULL(b);
1006 free(b->ns.name);
1007 b->ns.name = strdup("ns2.example.com");
1008 ASSERT_FALSE(dns_resource_record_equal(a, b));
1009 }
1010
1011 /* ================================================================
1012 * dns_resource_record_equal() : CNAME
1013 * ================================================================ */
1014
1015 TEST(dns_resource_record_equal_cname_copy) {
1016 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1017
1018 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
1019 ASSERT_NOT_NULL(a);
1020 a->cname.name = ASSERT_PTR(strdup("example.com"));
1021
1022 b = dns_resource_record_copy(a);
1023 ASSERT_NOT_NULL(b);
1024 ASSERT_TRUE(dns_resource_record_equal(a, b));
1025
1026 test_to_json_from_json(a);
1027 }
1028
1029 TEST(dns_resource_record_equal_cname_fail) {
1030 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1031
1032 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
1033 ASSERT_NOT_NULL(a);
1034 a->cname.name = strdup("example.com");
1035
1036 b = dns_resource_record_copy(a);
1037 ASSERT_NOT_NULL(b);
1038 free(b->cname.name);
1039 b->cname.name = strdup("example.orb");
1040 ASSERT_FALSE(dns_resource_record_equal(a, b));
1041 }
1042
1043 /* ================================================================
1044 * dns_resource_record_equal() : DNAME
1045 * ================================================================ */
1046
1047 TEST(dns_resource_record_equal_dname) {
1048 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1049
1050 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "www.example.com");
1051 ASSERT_NOT_NULL(a);
1052 a->dname.name = strdup("example.com");
1053
1054 b = dns_resource_record_copy(a);
1055 ASSERT_NOT_NULL(b);
1056 ASSERT_TRUE(dns_resource_record_equal(a, b));
1057 }
1058
1059 TEST(dns_resource_record_equal_dname_fail) {
1060 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1061
1062 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "www.example.com");
1063 ASSERT_NOT_NULL(a);
1064 a->dname.name = strdup("example.com");
1065
1066 b = dns_resource_record_copy(a);
1067 ASSERT_NOT_NULL(b);
1068 free(b->dname.name);
1069 b->dname.name = strdup("example.orb");
1070 ASSERT_FALSE(dns_resource_record_equal(a, b));
1071 }
1072
1073 /* ================================================================
1074 * dns_resource_record_equal() : SOA
1075 * ================================================================ */
1076
1077 TEST(dns_resource_record_equal_soa_copy) {
1078 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1079
1080 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
1081 ASSERT_NOT_NULL(a);
1082 a->soa.mname = strdup("ns.example.com");
1083 a->soa.rname = strdup("admin.example.com");
1084 a->soa.serial = 1111111111;
1085 a->soa.refresh = 86400;
1086 a->soa.retry = 7200;
1087 a->soa.expire = 4000000;
1088 a->soa.minimum = 3600;
1089
1090 b = dns_resource_record_copy(a);
1091 ASSERT_NOT_NULL(b);
1092 ASSERT_TRUE(dns_resource_record_equal(a, b));
1093 }
1094
1095 TEST(dns_resource_record_equal_soa_bad_mname) {
1096 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1097
1098 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
1099 ASSERT_NOT_NULL(a);
1100 a->soa.mname = strdup("ns.example.com");
1101 a->soa.rname = strdup("admin.example.com");
1102 a->soa.serial = 1111111111;
1103 a->soa.refresh = 86400;
1104 a->soa.retry = 7200;
1105 a->soa.expire = 4000000;
1106 a->soa.minimum = 3600;
1107
1108 b = dns_resource_record_copy(a);
1109 ASSERT_NOT_NULL(b);
1110 free(b->soa.mname);
1111 b->soa.mname = strdup("ns.example.org");
1112 ASSERT_FALSE(dns_resource_record_equal(a, b));
1113 }
1114
1115 TEST(dns_resource_record_equal_soa_bad_rname) {
1116 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1117
1118 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
1119 ASSERT_NOT_NULL(a);
1120 a->soa.mname = strdup("ns.example.com");
1121 a->soa.rname = strdup("admin.example.com");
1122 a->soa.serial = 1111111111;
1123 a->soa.refresh = 86400;
1124 a->soa.retry = 7200;
1125 a->soa.expire = 4000000;
1126 a->soa.minimum = 3600;
1127
1128 b = dns_resource_record_copy(a);
1129 ASSERT_NOT_NULL(b);
1130 free(b->soa.rname);
1131 b->soa.rname = strdup("admin.example.org");
1132 ASSERT_FALSE(dns_resource_record_equal(a, b));
1133 }
1134
1135 TEST(dns_resource_record_equal_soa_bad_serial) {
1136 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1137
1138 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
1139 ASSERT_NOT_NULL(a);
1140 a->soa.mname = strdup("ns.example.com");
1141 a->soa.rname = strdup("admin.example.com");
1142 a->soa.serial = 1111111111;
1143 a->soa.refresh = 86400;
1144 a->soa.retry = 7200;
1145 a->soa.expire = 4000000;
1146 a->soa.minimum = 3600;
1147
1148 b = dns_resource_record_copy(a);
1149 ASSERT_NOT_NULL(b);
1150 b->soa.serial = 1111111112;
1151 ASSERT_FALSE(dns_resource_record_equal(a, b));
1152 }
1153
1154 TEST(dns_resource_record_equal_soa_bad_refresh) {
1155 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1156
1157 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
1158 ASSERT_NOT_NULL(a);
1159 a->soa.mname = strdup("ns.example.com");
1160 a->soa.rname = strdup("admin.example.com");
1161 a->soa.serial = 1111111111;
1162 a->soa.refresh = 86400;
1163 a->soa.retry = 7200;
1164 a->soa.expire = 4000000;
1165 a->soa.minimum = 3600;
1166
1167 b = dns_resource_record_copy(a);
1168 ASSERT_NOT_NULL(b);
1169 b->soa.refresh = 86401;
1170 ASSERT_FALSE(dns_resource_record_equal(a, b));
1171 }
1172
1173 TEST(dns_resource_record_equal_soa_bad_retry) {
1174 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1175
1176 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
1177 ASSERT_NOT_NULL(a);
1178 a->soa.mname = strdup("ns.example.com");
1179 a->soa.rname = strdup("admin.example.com");
1180 a->soa.serial = 1111111111;
1181 a->soa.refresh = 86400;
1182 a->soa.retry = 7200;
1183 a->soa.expire = 4000000;
1184 a->soa.minimum = 3600;
1185
1186 b = dns_resource_record_copy(a);
1187 ASSERT_NOT_NULL(b);
1188 b->soa.retry = 7201;
1189 ASSERT_FALSE(dns_resource_record_equal(a, b));
1190 }
1191
1192 TEST(dns_resource_record_equal_soa_bad_expire) {
1193 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1194
1195 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
1196 ASSERT_NOT_NULL(a);
1197 a->soa.mname = strdup("ns.example.com");
1198 a->soa.rname = strdup("admin.example.com");
1199 a->soa.serial = 1111111111;
1200 a->soa.refresh = 86400;
1201 a->soa.retry = 7200;
1202 a->soa.expire = 4000000;
1203 a->soa.minimum = 3600;
1204
1205 b = dns_resource_record_copy(a);
1206 ASSERT_NOT_NULL(b);
1207 b->soa.expire = 4000001;
1208 ASSERT_FALSE(dns_resource_record_equal(a, b));
1209 }
1210
1211 TEST(dns_resource_record_equal_soa_bad_minimum) {
1212 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1213
1214 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
1215 ASSERT_NOT_NULL(a);
1216 a->soa.mname = strdup("ns.example.com");
1217 a->soa.rname = strdup("admin.example.com");
1218 a->soa.serial = 1111111111;
1219 a->soa.refresh = 86400;
1220 a->soa.retry = 7200;
1221 a->soa.expire = 4000000;
1222 a->soa.minimum = 3600;
1223
1224 b = dns_resource_record_copy(a);
1225 ASSERT_NOT_NULL(b);
1226 b->soa.minimum = 3601;
1227 ASSERT_FALSE(dns_resource_record_equal(a, b));
1228 }
1229
1230 /* ================================================================
1231 * dns_resource_record_equal() : PTR
1232 * ================================================================ */
1233
1234 TEST(dns_resource_record_equal_ptr_copy) {
1235 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1236
1237 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "127.1.168.192.in-addr-arpa");
1238 ASSERT_NOT_NULL(a);
1239 a->ptr.name = ASSERT_PTR(strdup("example.com"));
1240
1241 b = dns_resource_record_copy(a);
1242 ASSERT_NOT_NULL(b);
1243 ASSERT_TRUE(dns_resource_record_equal(a, b));
1244
1245 test_to_json_from_json(a);
1246 }
1247
1248 TEST(dns_resource_record_equal_ptr_fail) {
1249 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1250
1251 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "127.1.168.192.in-addr-arpa");
1252 ASSERT_NOT_NULL(a);
1253 a->ptr.name = strdup("example.com");
1254
1255 b = dns_resource_record_copy(a);
1256 ASSERT_NOT_NULL(b);
1257 free(b->ptr.name);
1258 b->ptr.name = strdup("example.org");
1259 ASSERT_FALSE(dns_resource_record_equal(a, b));
1260 }
1261
1262 /* ================================================================
1263 * dns_resource_record_equal() : HINFO
1264 * ================================================================ */
1265
1266 TEST(dns_resource_record_equal_hinfo_copy) {
1267 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1268
1269 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_HINFO, "www.example.com");
1270 ASSERT_NOT_NULL(a);
1271 a->hinfo.cpu = strdup("intel x64");
1272 a->hinfo.os = strdup("linux");
1273
1274 b = dns_resource_record_copy(a);
1275 ASSERT_NOT_NULL(b);
1276 ASSERT_TRUE(dns_resource_record_equal(a, b));
1277 }
1278
1279 TEST(dns_resource_record_equal_hinfo_case_insensitive) {
1280 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1281
1282 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_HINFO, "www.example.com");
1283 ASSERT_NOT_NULL(a);
1284 a->hinfo.cpu = strdup("intel x64");
1285 a->hinfo.os = strdup("linux");
1286
1287 b = dns_resource_record_copy(a);
1288 ASSERT_NOT_NULL(b);
1289 free(b->hinfo.cpu);
1290 b->hinfo.cpu = strdup("INTEL x64");
1291 free(b->hinfo.os);
1292 b->hinfo.os = strdup("LINUX");
1293 ASSERT_TRUE(dns_resource_record_equal(a, b));
1294 }
1295
1296 TEST(dns_resource_record_equal_hinfo_bad_cpu) {
1297 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1298
1299 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_HINFO, "www.example.com");
1300 ASSERT_NOT_NULL(a);
1301 a->hinfo.cpu = strdup("intel x64");
1302 a->hinfo.os = strdup("linux");
1303
1304 b = dns_resource_record_copy(a);
1305 ASSERT_NOT_NULL(b);
1306 free(b->hinfo.cpu);
1307 b->hinfo.cpu = strdup("arm64");
1308 ASSERT_FALSE(dns_resource_record_equal(a, b));
1309 }
1310
1311 TEST(dns_resource_record_equal_hinfo_bad_os) {
1312 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1313
1314 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_HINFO, "www.example.com");
1315 ASSERT_NOT_NULL(a);
1316 a->hinfo.cpu = strdup("intel x64");
1317 a->hinfo.os = strdup("linux");
1318
1319 b = dns_resource_record_copy(a);
1320 ASSERT_NOT_NULL(b);
1321 free(b->hinfo.os);
1322 b->hinfo.os = strdup("windows");
1323 ASSERT_FALSE(dns_resource_record_equal(a, b));
1324 }
1325
1326 /* ================================================================
1327 * dns_resource_record_equal() : MX
1328 * ================================================================ */
1329
1330 TEST(dns_resource_record_equal_mx_copy) {
1331 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1332
1333 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "www.example.com");
1334 ASSERT_NOT_NULL(a);
1335 a->mx.priority = 10;
1336 a->mx.exchange = strdup("mail.example.com");
1337
1338 b = dns_resource_record_copy(a);
1339 ASSERT_NOT_NULL(b);
1340 ASSERT_TRUE(dns_resource_record_equal(a, b));
1341 }
1342
1343 TEST(dns_resource_record_equal_mx_bad_priority) {
1344 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1345
1346 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "www.example.com");
1347 ASSERT_NOT_NULL(a);
1348 a->mx.priority = 10;
1349 a->mx.exchange = strdup("mail.example.com");
1350
1351 b = dns_resource_record_copy(a);
1352 ASSERT_NOT_NULL(b);
1353 b->mx.priority = 9;
1354 ASSERT_FALSE(dns_resource_record_equal(a, b));
1355 }
1356
1357 TEST(dns_resource_record_equal_mx_bad_exchange) {
1358 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1359
1360 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "www.example.com");
1361 ASSERT_NOT_NULL(a);
1362 a->mx.priority = 10;
1363 a->mx.exchange = strdup("mail.example.com");
1364
1365 b = dns_resource_record_copy(a);
1366 ASSERT_NOT_NULL(b);
1367 free(b->mx.exchange);
1368 b->mx.exchange = strdup("mail.example.org");
1369 ASSERT_FALSE(dns_resource_record_equal(a, b));
1370 }
1371
1372 /* ================================================================
1373 * dns_resource_record_equal() : TXT
1374 * ================================================================ */
1375
1376 TEST(dns_resource_record_equal_txt_copy) {
1377 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1378
1379 const char *data = "the quick brown fox and so on";
1380 size_t len = strlen(data);
1381
1382 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_TXT, "www.example.com");
1383 ASSERT_NOT_NULL(a);
1384 a->txt.items = calloc(1, offsetof(DnsTxtItem, data) + len + 1);
1385 ASSERT_NOT_NULL(a->txt.items);
1386 a->txt.items->length = len;
1387 memcpy(a->txt.items->data, data, len);
1388
1389 b = dns_resource_record_copy(a);
1390 ASSERT_NOT_NULL(b);
1391 ASSERT_TRUE(dns_resource_record_equal(a, b));
1392 }
1393
1394 TEST(dns_resource_record_equal_txt_missing) {
1395 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1396
1397 const char *data = "the quick brown fox and so on";
1398 size_t len = strlen(data);
1399
1400 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_TXT, "www.example.com");
1401 ASSERT_NOT_NULL(a);
1402 a->txt.items = calloc(1, offsetof(DnsTxtItem, data) + len + 1);
1403 ASSERT_NOT_NULL(a->txt.items);
1404 a->txt.items->length = len;
1405 memcpy(a->txt.items->data, data, len);
1406
1407 b = dns_resource_record_copy(a);
1408 ASSERT_NOT_NULL(b);
1409 dns_txt_item_free_all(b->txt.items);
1410 dns_txt_item_new_empty(&b->txt.items);
1411 ASSERT_FALSE(dns_resource_record_equal(a, b));
1412 }
1413
1414 TEST(dns_resource_record_equal_txt_different_text) {
1415 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1416
1417 const char *data = "the quick brown fox and so on";
1418 size_t len = strlen(data);
1419
1420 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_TXT, "www.example.com");
1421 ASSERT_NOT_NULL(a);
1422 a->txt.items = calloc(1, offsetof(DnsTxtItem, data) + len + 1);
1423 ASSERT_NOT_NULL(a->txt.items);
1424 a->txt.items->length = len;
1425 memcpy(a->txt.items->data, data, len);
1426
1427 const char *other_data = "jumped over, etc";
1428
1429 b = dns_resource_record_copy(a);
1430 ASSERT_NOT_NULL(b);
1431 b->txt.items->length = strlen(other_data);
1432 memcpy(b->txt.items->data, other_data, strlen(other_data));
1433 ASSERT_FALSE(dns_resource_record_equal(a, b));
1434 }
1435
1436 /* ================================================================
1437 * dns_resource_record_equal() : LOC
1438 * ================================================================ */
1439
1440 TEST(dns_resource_record_equal_loc_copy) {
1441 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1442
1443 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_LOC, "www.example.com");
1444 ASSERT_NOT_NULL(a);
1445 a->loc.version = 0;
1446 a->loc.size = 0x29;
1447 a->loc.horiz_pre = 0x34;
1448 a->loc.vert_pre = 0x53;
1449 a->loc.latitude = 2332887285;
1450 a->loc.longitude = 2146974024;
1451 a->loc.altitude = 10000000;
1452
1453 b = dns_resource_record_copy(a);
1454 ASSERT_NOT_NULL(b);
1455 ASSERT_TRUE(dns_resource_record_equal(a, b));
1456 }
1457
1458 TEST(dns_resource_record_equal_loc_bad_size) {
1459 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1460
1461 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_LOC, "www.example.com");
1462 ASSERT_NOT_NULL(a);
1463 a->loc.version = 0;
1464 a->loc.size = 0x29;
1465 a->loc.horiz_pre = 0x34;
1466 a->loc.vert_pre = 0x53;
1467 a->loc.latitude = 2332887285;
1468 a->loc.longitude = 2146974024;
1469 a->loc.altitude = 10000000;
1470
1471 b = dns_resource_record_copy(a);
1472 ASSERT_NOT_NULL(b);
1473 b->loc.size = 0x28;
1474 ASSERT_FALSE(dns_resource_record_equal(a, b));
1475 }
1476
1477 TEST(dns_resource_record_equal_loc_bad_horiz_pre) {
1478 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1479
1480 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_LOC, "www.example.com");
1481 ASSERT_NOT_NULL(a);
1482 a->loc.version = 0;
1483 a->loc.size = 0x29;
1484 a->loc.horiz_pre = 0x34;
1485 a->loc.vert_pre = 0x53;
1486 a->loc.latitude = 2332887285;
1487 a->loc.longitude = 2146974024;
1488 a->loc.altitude = 10000000;
1489
1490 b = dns_resource_record_copy(a);
1491 ASSERT_NOT_NULL(b);
1492 b->loc.horiz_pre = 0x35;
1493 ASSERT_FALSE(dns_resource_record_equal(a, b));
1494 }
1495
1496 TEST(dns_resource_record_equal_loc_vert_pre) {
1497 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1498
1499 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_LOC, "www.example.com");
1500 ASSERT_NOT_NULL(a);
1501 a->loc.version = 0;
1502 a->loc.size = 0x29;
1503 a->loc.horiz_pre = 0x34;
1504 a->loc.vert_pre = 0x53;
1505 a->loc.latitude = 2332887285;
1506 a->loc.longitude = 2146974024;
1507 a->loc.altitude = 10000000;
1508
1509 b = dns_resource_record_copy(a);
1510 ASSERT_NOT_NULL(b);
1511 b->loc.vert_pre = 0x54;
1512 ASSERT_FALSE(dns_resource_record_equal(a, b));
1513 }
1514
1515 TEST(dns_resource_record_equal_loc_bad_latitude) {
1516 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1517
1518 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_LOC, "www.example.com");
1519 ASSERT_NOT_NULL(a);
1520 a->loc.version = 0;
1521 a->loc.size = 0x29;
1522 a->loc.horiz_pre = 0x34;
1523 a->loc.vert_pre = 0x53;
1524 a->loc.latitude = 2332887285;
1525 a->loc.longitude = 2146974024;
1526 a->loc.altitude = 10000000;
1527
1528 b = dns_resource_record_copy(a);
1529 ASSERT_NOT_NULL(b);
1530 b->loc.latitude = 2332887286;
1531 ASSERT_FALSE(dns_resource_record_equal(a, b));
1532 }
1533
1534 TEST(dns_resource_record_equal_loc_bad_longitude) {
1535 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1536
1537 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_LOC, "www.example.com");
1538 ASSERT_NOT_NULL(a);
1539 a->loc.version = 0;
1540 a->loc.size = 0x29;
1541 a->loc.horiz_pre = 0x34;
1542 a->loc.vert_pre = 0x53;
1543 a->loc.latitude = 2332887285;
1544 a->loc.longitude = 2146974024;
1545 a->loc.altitude = 10000000;
1546
1547 b = dns_resource_record_copy(a);
1548 ASSERT_NOT_NULL(b);
1549 b->loc.longitude = 2146974023;
1550 ASSERT_FALSE(dns_resource_record_equal(a, b));
1551 }
1552
1553 TEST(dns_resource_record_equal_loc_bad_altitude) {
1554 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1555
1556 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_LOC, "www.example.com");
1557 ASSERT_NOT_NULL(a);
1558 a->loc.version = 0;
1559 a->loc.size = 0x29;
1560 a->loc.horiz_pre = 0x34;
1561 a->loc.vert_pre = 0x53;
1562 a->loc.latitude = 2332887285;
1563 a->loc.longitude = 2146974024;
1564 a->loc.altitude = 10000000;
1565
1566 b = dns_resource_record_copy(a);
1567 ASSERT_NOT_NULL(b);
1568 b->loc.altitude = 10000001;
1569 ASSERT_FALSE(dns_resource_record_equal(a, b));
1570 }
1571
1572 /* ================================================================
1573 * dns_resource_record_equal() : SRV
1574 * ================================================================ */
1575
1576 TEST(dns_resource_record_equal_srv_copy) {
1577 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1578
1579 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SRV, "www.example.com");
1580 ASSERT_NOT_NULL(a);
1581 a->srv.name = strdup("mail.example.com");
1582 a->srv.priority = 10;
1583 a->srv.weight = 5;
1584 a->srv.port = 587;
1585
1586 b = dns_resource_record_copy(a);
1587 ASSERT_NOT_NULL(b);
1588 ASSERT_TRUE(dns_resource_record_equal(a, b));
1589 }
1590
1591 TEST(dns_resource_record_equal_srv_bad_name) {
1592 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1593
1594 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SRV, "www.example.com");
1595 ASSERT_NOT_NULL(a);
1596 a->srv.name = strdup("mail.example.com");
1597 a->srv.priority = 10;
1598 a->srv.weight = 5;
1599 a->srv.port = 587;
1600
1601 b = dns_resource_record_copy(a);
1602 ASSERT_NOT_NULL(b);
1603 free(b->srv.name);
1604 b->srv.name = strdup("example.com");
1605 ASSERT_FALSE(dns_resource_record_equal(a, b));
1606 }
1607
1608 TEST(dns_resource_record_equal_srv_bad_priority) {
1609 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1610
1611 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SRV, "www.example.com");
1612 ASSERT_NOT_NULL(a);
1613 a->srv.name = strdup("mail.example.com");
1614 a->srv.priority = 10;
1615 a->srv.weight = 5;
1616 a->srv.port = 587;
1617
1618 b = dns_resource_record_copy(a);
1619 ASSERT_NOT_NULL(b);
1620 b->srv.priority = 9;
1621 ASSERT_FALSE(dns_resource_record_equal(a, b));
1622 }
1623
1624 TEST(dns_resource_record_equal_srv_bad_weight) {
1625 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1626
1627 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SRV, "www.example.com");
1628 ASSERT_NOT_NULL(a);
1629 a->srv.name = strdup("mail.example.com");
1630 a->srv.priority = 10;
1631 a->srv.weight = 5;
1632 a->srv.port = 587;
1633
1634 b = dns_resource_record_copy(a);
1635 ASSERT_NOT_NULL(b);
1636 b->srv.weight = 6;
1637 ASSERT_FALSE(dns_resource_record_equal(a, b));
1638 }
1639
1640 TEST(dns_resource_record_equal_srv_bad_port) {
1641 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1642
1643 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SRV, "www.example.com");
1644 ASSERT_NOT_NULL(a);
1645 a->srv.name = strdup("mail.example.com");
1646 a->srv.priority = 10;
1647 a->srv.weight = 5;
1648 a->srv.port = 587;
1649
1650 b = dns_resource_record_copy(a);
1651 ASSERT_NOT_NULL(b);
1652 b->srv.port = 588;
1653 ASSERT_FALSE(dns_resource_record_equal(a, b));
1654 }
1655
1656 /* ================================================================
1657 * dns_resource_record_equal() : NAPTR
1658 * ================================================================ */
1659
1660 TEST(dns_resource_record_equal_naptr_copy) {
1661 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1662
1663 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NAPTR, "4.3.2.1.5.5.5.0.0.8.1.e164.arpa");
1664 ASSERT_NOT_NULL(a);
1665 a->naptr.order = 102;
1666 a->naptr.preference = 10;
1667 a->naptr.flags = strdup("U");
1668 a->naptr.services = strdup("E2U+sip");
1669 a->naptr.regexp = strdup("!^.*$!sip:[email protected]!");
1670 a->naptr.replacement = strdup("_sip._udp.example.com");
1671
1672 b = dns_resource_record_copy(a);
1673 ASSERT_NOT_NULL(b);
1674 ASSERT_TRUE(dns_resource_record_equal(a, b));
1675 }
1676
1677 TEST(dns_resource_record_equal_naptr_bad_order) {
1678 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1679
1680 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NAPTR, "4.3.2.1.5.5.5.0.0.8.1.e164.arpa");
1681 ASSERT_NOT_NULL(a);
1682 a->naptr.order = 102;
1683 a->naptr.preference = 10;
1684 a->naptr.flags = strdup("U");
1685 a->naptr.services = strdup("E2U+sip");
1686 a->naptr.regexp = strdup("!^.*$!sip:[email protected]!");
1687 a->naptr.replacement = strdup("_sip._udp.example.com");
1688
1689 b = dns_resource_record_copy(a);
1690 ASSERT_NOT_NULL(b);
1691 b->naptr.order = 103;
1692 ASSERT_FALSE(dns_resource_record_equal(a, b));
1693 }
1694
1695 TEST(dns_resource_record_equal_naptr_bad_preference) {
1696 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1697
1698 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NAPTR, "4.3.2.1.5.5.5.0.0.8.1.e164.arpa");
1699 ASSERT_NOT_NULL(a);
1700 a->naptr.order = 102;
1701 a->naptr.preference = 10;
1702 a->naptr.flags = strdup("U");
1703 a->naptr.services = strdup("E2U+sip");
1704 a->naptr.regexp = strdup("!^.*$!sip:[email protected]!");
1705 a->naptr.replacement = strdup("_sip._udp.example.com");
1706
1707 b = dns_resource_record_copy(a);
1708 ASSERT_NOT_NULL(b);
1709 b->naptr.preference = 9;
1710 ASSERT_FALSE(dns_resource_record_equal(a, b));
1711 }
1712
1713 TEST(dns_resource_record_equal_naptr_bad_flags) {
1714 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1715
1716 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NAPTR, "4.3.2.1.5.5.5.0.0.8.1.e164.arpa");
1717 ASSERT_NOT_NULL(a);
1718 a->naptr.order = 102;
1719 a->naptr.preference = 10;
1720 a->naptr.flags = strdup("U");
1721 a->naptr.services = strdup("E2U+sip");
1722 a->naptr.regexp = strdup("!^.*$!sip:[email protected]!");
1723 a->naptr.replacement = strdup("_sip._udp.example.com");
1724
1725 b = dns_resource_record_copy(a);
1726 ASSERT_NOT_NULL(b);
1727 free(b->naptr.flags);
1728 b->naptr.flags = strdup("S");
1729 ASSERT_FALSE(dns_resource_record_equal(a, b));
1730 }
1731
1732 TEST(dns_resource_record_equal_naptr_bad_services) {
1733 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1734
1735 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NAPTR, "4.3.2.1.5.5.5.0.0.8.1.e164.arpa");
1736 ASSERT_NOT_NULL(a);
1737 a->naptr.order = 102;
1738 a->naptr.preference = 10;
1739 a->naptr.flags = strdup("U");
1740 a->naptr.services = strdup("E2U+sip");
1741 a->naptr.regexp = strdup("!^.*$!sip:[email protected]!");
1742 a->naptr.replacement = strdup("_sip._udp.example.com");
1743
1744 b = dns_resource_record_copy(a);
1745 ASSERT_NOT_NULL(b);
1746 free(b->naptr.services);
1747 b->naptr.services = strdup("E2U-sip");
1748 ASSERT_FALSE(dns_resource_record_equal(a, b));
1749 }
1750
1751 TEST(dns_resource_record_equal_naptr_bad_regexp) {
1752 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1753
1754 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NAPTR, "4.3.2.1.5.5.5.0.0.8.1.e164.arpa");
1755 ASSERT_NOT_NULL(a);
1756 a->naptr.order = 102;
1757 a->naptr.preference = 10;
1758 a->naptr.flags = strdup("U");
1759 a->naptr.services = strdup("E2U+sip");
1760 a->naptr.regexp = strdup("!^.*$!sip:[email protected]!");
1761 a->naptr.replacement = strdup("_sip._udp.example.com");
1762
1763 b = dns_resource_record_copy(a);
1764 ASSERT_NOT_NULL(b);
1765 free(b->naptr.regexp);
1766 b->naptr.regexp = strdup("a*");
1767 ASSERT_FALSE(dns_resource_record_equal(a, b));
1768 }
1769
1770 TEST(dns_resource_record_equal_naptr_bad_replacement) {
1771 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1772
1773 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NAPTR, "4.3.2.1.5.5.5.0.0.8.1.e164.arpa");
1774 ASSERT_NOT_NULL(a);
1775 a->naptr.order = 102;
1776 a->naptr.preference = 10;
1777 a->naptr.flags = strdup("U");
1778 a->naptr.services = strdup("E2U+sip");
1779 a->naptr.regexp = strdup("!^.*$!sip:[email protected]!");
1780 a->naptr.replacement = strdup("_sip._udp.example.com");
1781
1782 b = dns_resource_record_copy(a);
1783 ASSERT_NOT_NULL(b);
1784 free(b->naptr.replacement);
1785 b->naptr.replacement = strdup("_sip._udp.example.org");
1786 ASSERT_FALSE(dns_resource_record_equal(a, b));
1787 }
1788
1789 /* ================================================================
1790 * dns_resource_record_equal() : RRSIG
1791 * ================================================================ */
1792
1793 TEST(dns_resource_record_equal_rrsig_copy) {
1794 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1795
1796 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
1797 ASSERT_NOT_NULL(a);
1798 a->rrsig.type_covered = DNS_TYPE_A;
1799 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
1800 a->rrsig.labels = 3;
1801 a->rrsig.original_ttl = 3600;
1802 a->rrsig.expiration = 1720361303;
1803 a->rrsig.inception = 1717769303;
1804 a->rrsig.key_tag = 0x1234;
1805 a->rrsig.signer = strdup("example.com");
1806
1807 const uint8_t signature[] = {
1808 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
1809 };
1810 a->rrsig.signature_size = sizeof(signature);
1811 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
1812
1813 b = dns_resource_record_copy(a);
1814 ASSERT_NOT_NULL(b);
1815 ASSERT_TRUE(dns_resource_record_equal(a, b));
1816 }
1817
1818 TEST(dns_resource_record_equal_rrsig_bad_type_covered) {
1819 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1820
1821 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
1822 ASSERT_NOT_NULL(a);
1823 a->rrsig.type_covered = DNS_TYPE_A;
1824 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
1825 a->rrsig.labels = 3;
1826 a->rrsig.original_ttl = 3600;
1827 a->rrsig.expiration = 1720361303;
1828 a->rrsig.inception = 1717769303;
1829 a->rrsig.key_tag = 0x1234;
1830 a->rrsig.signer = strdup("example.com");
1831
1832 const uint8_t signature[] = {
1833 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
1834 };
1835 a->rrsig.signature_size = sizeof(signature);
1836 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
1837
1838 b = dns_resource_record_copy(a);
1839 ASSERT_NOT_NULL(b);
1840 b->rrsig.type_covered = DNS_TYPE_AAAA;
1841 ASSERT_FALSE(dns_resource_record_equal(a, b));
1842 }
1843
1844 TEST(dns_resource_record_equal_rrsig_bad_algorithm) {
1845 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1846
1847 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
1848 ASSERT_NOT_NULL(a);
1849 a->rrsig.type_covered = DNS_TYPE_A;
1850 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
1851 a->rrsig.labels = 3;
1852 a->rrsig.original_ttl = 3600;
1853 a->rrsig.expiration = 1720361303;
1854 a->rrsig.inception = 1717769303;
1855 a->rrsig.key_tag = 0x1234;
1856 a->rrsig.signer = strdup("example.com");
1857
1858 const uint8_t signature[] = {
1859 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
1860 };
1861 a->rrsig.signature_size = sizeof(signature);
1862 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
1863
1864 b = dns_resource_record_copy(a);
1865 ASSERT_NOT_NULL(b);
1866 b->rrsig.algorithm = DNSSEC_ALGORITHM_DSA;
1867 ASSERT_FALSE(dns_resource_record_equal(a, b));
1868 }
1869
1870 TEST(dns_resource_record_equal_rrsig_bad_labels) {
1871 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1872
1873 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
1874 ASSERT_NOT_NULL(a);
1875 a->rrsig.type_covered = DNS_TYPE_A;
1876 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
1877 a->rrsig.labels = 3;
1878 a->rrsig.original_ttl = 3600;
1879 a->rrsig.expiration = 1720361303;
1880 a->rrsig.inception = 1717769303;
1881 a->rrsig.key_tag = 0x1234;
1882 a->rrsig.signer = strdup("example.com");
1883
1884 const uint8_t signature[] = {
1885 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
1886 };
1887 a->rrsig.signature_size = sizeof(signature);
1888 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
1889
1890 b = dns_resource_record_copy(a);
1891 ASSERT_NOT_NULL(b);
1892 b->rrsig.labels = 2;
1893 ASSERT_FALSE(dns_resource_record_equal(a, b));
1894 }
1895
1896 TEST(dns_resource_record_equal_rrsig_bad_original_ttl) {
1897 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1898
1899 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
1900 ASSERT_NOT_NULL(a);
1901 a->rrsig.type_covered = DNS_TYPE_A;
1902 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
1903 a->rrsig.labels = 3;
1904 a->rrsig.original_ttl = 3600;
1905 a->rrsig.expiration = 1720361303;
1906 a->rrsig.inception = 1717769303;
1907 a->rrsig.key_tag = 0x1234;
1908 a->rrsig.signer = strdup("example.com");
1909
1910 const uint8_t signature[] = {
1911 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
1912 };
1913 a->rrsig.signature_size = sizeof(signature);
1914 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
1915
1916 b = dns_resource_record_copy(a);
1917 ASSERT_NOT_NULL(b);
1918 b->rrsig.original_ttl = 3601;
1919 ASSERT_FALSE(dns_resource_record_equal(a, b));
1920 }
1921
1922 TEST(dns_resource_record_equal_rrsig_bad_expiration) {
1923 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1924
1925 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
1926 ASSERT_NOT_NULL(a);
1927 a->rrsig.type_covered = DNS_TYPE_A;
1928 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
1929 a->rrsig.labels = 3;
1930 a->rrsig.original_ttl = 3600;
1931 a->rrsig.expiration = 1720361303;
1932 a->rrsig.inception = 1717769303;
1933 a->rrsig.key_tag = 0x1234;
1934 a->rrsig.signer = strdup("example.com");
1935
1936 const uint8_t signature[] = {
1937 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
1938 };
1939 a->rrsig.signature_size = sizeof(signature);
1940 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
1941
1942 b = dns_resource_record_copy(a);
1943 ASSERT_NOT_NULL(b);
1944 b->rrsig.expiration = a->rrsig.expiration + 1;
1945 ASSERT_FALSE(dns_resource_record_equal(a, b));
1946 }
1947
1948 TEST(dns_resource_record_equal_rrsig_bad_inception) {
1949 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1950
1951 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
1952 ASSERT_NOT_NULL(a);
1953 a->rrsig.type_covered = DNS_TYPE_A;
1954 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
1955 a->rrsig.labels = 3;
1956 a->rrsig.original_ttl = 3600;
1957 a->rrsig.expiration = 1720361303;
1958 a->rrsig.inception = 1717769303;
1959 a->rrsig.key_tag = 0x1234;
1960 a->rrsig.signer = strdup("example.com");
1961
1962 const uint8_t signature[] = {
1963 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
1964 };
1965 a->rrsig.signature_size = sizeof(signature);
1966 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
1967
1968 b = dns_resource_record_copy(a);
1969 ASSERT_NOT_NULL(b);
1970 b->rrsig.inception = a->rrsig.inception - 1;
1971 ASSERT_FALSE(dns_resource_record_equal(a, b));
1972 }
1973
1974 TEST(dns_resource_record_equal_rrsig_bad_key_tag) {
1975 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
1976
1977 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
1978 ASSERT_NOT_NULL(a);
1979 a->rrsig.type_covered = DNS_TYPE_A;
1980 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
1981 a->rrsig.labels = 3;
1982 a->rrsig.original_ttl = 3600;
1983 a->rrsig.expiration = 1720361303;
1984 a->rrsig.inception = 1717769303;
1985 a->rrsig.key_tag = 0x1234;
1986 a->rrsig.signer = strdup("example.com");
1987
1988 const uint8_t signature[] = {
1989 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
1990 };
1991 a->rrsig.signature_size = sizeof(signature);
1992 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
1993
1994 b = dns_resource_record_copy(a);
1995 ASSERT_NOT_NULL(b);
1996 b->rrsig.key_tag = 0x4321;
1997 ASSERT_FALSE(dns_resource_record_equal(a, b));
1998 }
1999
2000 TEST(dns_resource_record_equal_rrsig_bad_signer) {
2001 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
2002
2003 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
2004 ASSERT_NOT_NULL(a);
2005 a->rrsig.type_covered = DNS_TYPE_A;
2006 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
2007 a->rrsig.labels = 3;
2008 a->rrsig.original_ttl = 3600;
2009 a->rrsig.expiration = 1720361303;
2010 a->rrsig.inception = 1717769303;
2011 a->rrsig.key_tag = 0x1234;
2012 a->rrsig.signer = strdup("example.com");
2013
2014 const uint8_t signature[] = {
2015 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
2016 };
2017 a->rrsig.signature_size = sizeof(signature);
2018 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
2019
2020 b = dns_resource_record_copy(a);
2021 ASSERT_NOT_NULL(b);
2022 free(b->rrsig.signer);
2023 b->rrsig.signer = strdup("www.example.com");
2024 ASSERT_FALSE(dns_resource_record_equal(a, b));
2025 }
2026
2027 TEST(dns_resource_record_equal_rrsig_bad_signature) {
2028 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
2029
2030 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
2031 ASSERT_NOT_NULL(a);
2032 a->rrsig.type_covered = DNS_TYPE_A;
2033 a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
2034 a->rrsig.labels = 3;
2035 a->rrsig.original_ttl = 3600;
2036 a->rrsig.expiration = 1720361303;
2037 a->rrsig.inception = 1717769303;
2038 a->rrsig.key_tag = 0x1234;
2039 a->rrsig.signer = strdup("example.com");
2040
2041 const uint8_t signature[] = {
2042 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
2043 };
2044 a->rrsig.signature_size = sizeof(signature);
2045 a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
2046
2047 b = dns_resource_record_copy(a);
2048 ASSERT_NOT_NULL(b);
2049 b->rrsig.signature_size -= 1;
2050 ASSERT_FALSE(dns_resource_record_equal(a, b));
2051 }
2052
2053 /* ================================================================
2054 * dns_resource_record_equal() : SVCB
2055 * ================================================================ */
2056
2057 static DnsSvcParam* add_svcb_param(DnsResourceRecord *rr, uint16_t key, const char *value, size_t len) {
2058 DnsSvcParam *param = calloc(1, offsetof(DnsSvcParam, value) + len);
2059 ASSERT_NOT_NULL(param);
2060
2061 param->key = key;
2062 param->length = len;
2063
2064 if (value)
2065 memcpy(param->value, value, len);
2066
2067 LIST_APPEND(params, rr->svcb.params, param);
2068 return param;
2069 }
2070
2071 TEST(dns_resource_record_equal_svcb_copy) {
2072 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
2073
2074 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
2075 ASSERT_NOT_NULL(a);
2076 a->svcb.priority = 9;
2077 a->svcb.target_name = strdup("sock.example.com");
2078
2079 add_svcb_param(a, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
2080 add_svcb_param(a, 99, NULL, 0);
2081
2082 b = dns_resource_record_copy(a);
2083 ASSERT_NOT_NULL(b);
2084 ASSERT_TRUE(dns_resource_record_equal(a, b));
2085 }
2086
2087 TEST(dns_resource_record_equal_svcb_bad_priority) {
2088 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
2089
2090 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
2091 ASSERT_NOT_NULL(a);
2092 a->svcb.priority = 9;
2093 a->svcb.target_name = strdup("sock.example.com");
2094
2095 b = dns_resource_record_copy(a);
2096 ASSERT_NOT_NULL(b);
2097 b->svcb.priority = 8;
2098 ASSERT_FALSE(dns_resource_record_equal(a, b));
2099 }
2100
2101 TEST(dns_resource_record_equal_svcb_bad_target_name) {
2102 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
2103
2104 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
2105 ASSERT_NOT_NULL(a);
2106 a->svcb.priority = 9;
2107 a->svcb.target_name = strdup("sock.example.com");
2108
2109 b = dns_resource_record_copy(a);
2110 ASSERT_NOT_NULL(b);
2111 free(b->svcb.target_name);
2112 b->svcb.target_name = strdup("other.example.com");
2113 ASSERT_FALSE(dns_resource_record_equal(a, b));
2114 }
2115
2116 TEST(dns_resource_record_equal_svcb_param_missing) {
2117 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
2118
2119 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
2120 ASSERT_NOT_NULL(a);
2121 a->svcb.priority = 9;
2122 a->svcb.target_name = strdup("sock.example.com");
2123
2124 b = dns_resource_record_copy(a);
2125 ASSERT_NOT_NULL(b);
2126
2127 add_svcb_param(a, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
2128 add_svcb_param(a, 99, NULL, 0);
2129
2130 add_svcb_param(b, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
2131
2132 ASSERT_FALSE(dns_resource_record_equal(a, b));
2133 }
2134
2135 TEST(dns_resource_record_equal_svcb_param_extra) {
2136 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
2137
2138 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
2139 ASSERT_NOT_NULL(a);
2140 a->svcb.priority = 9;
2141 a->svcb.target_name = strdup("sock.example.com");
2142
2143 b = dns_resource_record_copy(a);
2144 ASSERT_NOT_NULL(b);
2145
2146 add_svcb_param(a, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
2147
2148 add_svcb_param(b, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
2149 add_svcb_param(b, 99, NULL, 0);
2150
2151 ASSERT_FALSE(dns_resource_record_equal(a, b));
2152 }
2153
2154 TEST(dns_resource_record_equal_svcb_param_different) {
2155 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
2156
2157 a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
2158 ASSERT_NOT_NULL(a);
2159 a->svcb.priority = 9;
2160 a->svcb.target_name = strdup("sock.example.com");
2161
2162 b = dns_resource_record_copy(a);
2163 ASSERT_NOT_NULL(b);
2164
2165 add_svcb_param(a, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
2166 add_svcb_param(b, DNS_SVC_PARAM_KEY_ALPN, "\x04xmpp", 5);
2167
2168 ASSERT_FALSE(dns_resource_record_equal(a, b));
2169 }
2170
2171 /* ================================================================
2172 * dns_resource_record_to_string()
2173 * ================================================================ */
2174
2175 TEST(dns_resource_record_to_string_a) {
2176 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2177 const char *str;
2178
2179 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
2180 ASSERT_NOT_NULL(rr);
2181 rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
2182
2183 str = dns_resource_record_to_string(rr);
2184 ASSERT_STREQ(str, "www.example.com IN A 192.168.1.127");
2185 }
2186
2187 TEST(dns_resource_record_to_string_aaaa) {
2188 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2189 const char *str;
2190
2191 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
2192 ASSERT_NOT_NULL(rr);
2193 rr->aaaa.in6_addr = (struct in6_addr) {
2194 .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 }
2195 };
2196
2197 str = dns_resource_record_to_string(rr);
2198 ASSERT_STREQ(str, "www.example.com IN AAAA ff02::1:3");
2199 }
2200
2201 TEST(dns_resource_record_to_string_ns) {
2202 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2203 const char *str;
2204
2205 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NS, "www.example.com");
2206 ASSERT_NOT_NULL(rr);
2207 rr->ns.name = strdup("ns1.example.com");
2208
2209 str = dns_resource_record_to_string(rr);
2210 ASSERT_STREQ(str, "www.example.com IN NS ns1.example.com");
2211 }
2212
2213 TEST(dns_resource_record_to_string_cname) {
2214 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2215 const char *str;
2216
2217 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
2218 ASSERT_NOT_NULL(rr);
2219 rr->ns.name = strdup("example.com");
2220
2221 str = dns_resource_record_to_string(rr);
2222 ASSERT_STREQ(str, "www.example.com IN CNAME example.com");
2223 }
2224
2225 TEST(dns_resource_record_to_string_soa) {
2226 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2227 const char *str;
2228
2229 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
2230 ASSERT_NOT_NULL(rr);
2231 rr->soa.mname = strdup("ns0.example.com");
2232 rr->soa.rname = strdup("ns0.example.com");
2233 rr->soa.serial = 1111111111;
2234 rr->soa.refresh = 86400;
2235 rr->soa.retry = 7200;
2236 rr->soa.expire = 4000000;
2237 rr->soa.minimum = 3600;
2238
2239 str = dns_resource_record_to_string(rr);
2240 ASSERT_STREQ(str, "www.example.com IN SOA ns0.example.com ns0.example.com 1111111111 86400 7200 4000000 3600");
2241 }
2242
2243 TEST(dns_resource_record_to_string_ptr) {
2244 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2245 const char *str;
2246
2247 union in_addr_union addr = { .in.s_addr = htobe32(0xc0a8017f) };
2248
2249 ASSERT_OK(dns_resource_record_new_reverse(&rr, AF_INET, &addr, "www.example.com"));
2250 ASSERT_NOT_NULL(rr);
2251
2252 str = dns_resource_record_to_string(rr);
2253 ASSERT_STREQ(str, "127.1.168.192.in-addr.arpa IN PTR www.example.com");
2254 }
2255
2256 TEST(dns_resource_record_to_string_hinfo) {
2257 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2258 const char *str;
2259
2260 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_HINFO, "www.example.com");
2261 ASSERT_NOT_NULL(rr);
2262 rr->hinfo.cpu = strdup("Intel x64");
2263 rr->hinfo.os = strdup("GNU/Linux");
2264
2265 str = dns_resource_record_to_string(rr);
2266 ASSERT_STREQ(str, "www.example.com IN HINFO Intel x64 GNU/Linux");
2267 }
2268
2269 TEST(dns_resource_record_to_string_mx) {
2270 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2271 const char *str;
2272
2273 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "mail.example.com");
2274 ASSERT_NOT_NULL(rr);
2275 rr->mx.priority = 6;
2276 rr->mx.exchange= strdup("exchange.example.com");
2277
2278 str = dns_resource_record_to_string(rr);
2279 ASSERT_STREQ(str, "mail.example.com IN MX 6 exchange.example.com");
2280 }
2281
2282 TEST(dns_resource_record_to_string_srv) {
2283 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2284 const char *str;
2285
2286 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SRV, "_ldap._tcp.example.com");
2287 ASSERT_NOT_NULL(rr);
2288 rr->ttl = 3601;
2289 rr->srv.priority = 17185;
2290 rr->srv.weight = 25976;
2291 rr->srv.port = 389;
2292 rr->srv.name = strdup("cloud.example.com");
2293
2294 str = dns_resource_record_to_string(rr);
2295 ASSERT_STREQ(str, "_ldap._tcp.example.com IN SRV 17185 25976 389 cloud.example.com");
2296 }
2297
2298 TEST(dns_resource_record_to_string_svcb) {
2299 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2300 DnsSvcParam *param = NULL;
2301 const char *str;
2302
2303 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
2304 ASSERT_NOT_NULL(rr);
2305 rr->ttl = 3601;
2306 rr->svcb.priority = 9;
2307 rr->svcb.target_name = strdup("sock.example.com");
2308
2309 add_svcb_param(rr, DNS_SVC_PARAM_KEY_MANDATORY, "\x00\x01\x00\x03", 4);
2310 add_svcb_param(rr, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
2311 add_svcb_param(rr, DNS_SVC_PARAM_KEY_NO_DEFAULT_ALPN, NULL, 0);
2312 add_svcb_param(rr, DNS_SVC_PARAM_KEY_PORT, "\x01\xbb", 2);
2313
2314 param = add_svcb_param(rr, DNS_SVC_PARAM_KEY_IPV4HINT, NULL, 2 * sizeof(struct in_addr));
2315 param->value_in_addr[0].s_addr = htobe32(0x7284fd3a);
2316 param->value_in_addr[1].s_addr = htobe32(0x48bcc7c0);
2317
2318 param = add_svcb_param(rr, DNS_SVC_PARAM_KEY_IPV6HINT, NULL, sizeof(struct in6_addr));
2319 param->value_in6_addr[0] = (struct in6_addr) { .s6_addr = { 0xf2, 0x34, 0x32, 0x2e, 0xb8, 0x25, 0x38, 0x35, 0x2f, 0xd7, 0xdb, 0x7b, 0x28, 0x7e, 0x60, 0xbb } };
2320
2321 /* undefined key */
2322 add_svcb_param(rr, 99, NULL, 0);
2323
2324 str = dns_resource_record_to_string(rr);
2325 ASSERT_STREQ(str, "_443._wss.example.com IN SVCB 9 sock.example.com mandatory=\"\\000\\001\\000\\003\" alpn=\"websocket\" no-default-alpn port=443 ipv4hint=114.132.253.58,72.188.199.192 ipv6hint=f234:322e:b825:3835:2fd7:db7b:287e:60bb key99");
2326 }
2327
2328 /* ================================================================
2329 * dns_resource_record_to_wire_format()
2330 * ================================================================ */
2331
2332 TEST(dns_resource_record_to_wire_format) {
2333 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2334 int r;
2335
2336 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
2337 ASSERT_NOT_NULL(rr);
2338 rr->ttl = 3600;
2339 rr->cname.name = strdup("example.com");
2340
2341 ASSERT_OK(dns_resource_record_to_wire_format(rr, true));
2342
2343 const uint8_t data[] = {
2344 /* name */ 0x03, 'w', 'w', 'w',
2345 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
2346 0x03, 'c', 'o', 'm',
2347 0x00,
2348 /* CNAME */ 0x00, 0x05,
2349 /* IN */ 0x00, 0x01,
2350 /* ttl */ 0x00, 0x00, 0x0e, 0x10,
2351 /* rdata */ 0x00, 0x0d,
2352 /* name */ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
2353 0x03, 'c', 'o', 'm',
2354 0x00
2355 };
2356
2357 ASSERT_EQ(rr->wire_format_size, sizeof(data));
2358 ASSERT_EQ(rr->wire_format_rdata_offset, 27u);
2359 ASSERT_EQ(rr->wire_format_canonical, true);
2360
2361 r = memcmp(rr->wire_format, data, sizeof(data));
2362 ASSERT_EQ(r, 0);
2363 }
2364
2365 /* ================================================================
2366 * dns_resource_record_signer()
2367 * dns_resource_record_is_signer()
2368 * dns_resource_record_source()
2369 * dns_resource_record_is_synthetic()
2370 * ================================================================ */
2371
2372 static void prepare_rrsig(DnsResourceRecord **rr) {
2373 *rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
2374 ASSERT_NOT_NULL(*rr);
2375
2376 (*rr)->ttl = 3600;
2377 (*rr)->rrsig.type_covered = DNS_TYPE_A;
2378 (*rr)->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
2379 (*rr)->rrsig.labels = 1;
2380 (*rr)->rrsig.original_ttl = 3600;
2381 (*rr)->rrsig.expiration = 1720361303;
2382 (*rr)->rrsig.inception = 1717769303;
2383 (*rr)->rrsig.key_tag = 0x1234;
2384 (*rr)->rrsig.signer = strdup("example.com");
2385
2386 /* normally set by dnssec_rrsig_prepare() during dnssec_verify_rrset() */
2387 (*rr)->n_skip_labels_signer = 1;
2388 (*rr)->n_skip_labels_source = 2;
2389 }
2390
2391 TEST(dns_resource_record_signer) {
2392 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2393 const char *signer = NULL;
2394
2395 prepare_rrsig(&rr);
2396
2397 ASSERT_OK(dns_resource_record_signer(rr, &signer));
2398 ASSERT_STREQ(signer, "example.com");
2399 }
2400
2401 TEST(dns_resource_record_is_signer) {
2402 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2403
2404 prepare_rrsig(&rr);
2405
2406 ASSERT_TRUE(dns_resource_record_is_signer(rr, "example.com"));
2407 ASSERT_FALSE(dns_resource_record_is_signer(rr, "www.example.com"));
2408 ASSERT_FALSE(dns_resource_record_is_signer(rr, "com"));
2409 }
2410
2411 TEST(dns_resource_record_source) {
2412 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2413 const char *source = NULL;
2414
2415 prepare_rrsig(&rr);
2416
2417 ASSERT_OK(dns_resource_record_source(rr, &source));
2418 ASSERT_STREQ(source, "com");
2419 }
2420
2421 TEST(dns_resource_record_is_synthetic) {
2422 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
2423
2424 prepare_rrsig(&rr);
2425
2426 ASSERT_TRUE(dns_resource_record_is_synthetic(rr));
2427
2428 rr->n_skip_labels_source = -1;
2429 ASSERT_ERROR(dns_resource_record_is_synthetic(rr), ENODATA);
2430
2431 rr->n_skip_labels_source = 0;
2432 ASSERT_FALSE(dns_resource_record_is_synthetic(rr));
2433
2434 rr->n_skip_labels_source = 1;
2435 ASSERT_TRUE(dns_resource_record_is_synthetic(rr));
2436
2437 rr->key->_name = strdup("*.example.com");
2438 ASSERT_FALSE(dns_resource_record_is_synthetic(rr));
2439 }
2440
2441 /* ================================================================
2442 * dns_resource_record_clamp_ttl()
2443 * ================================================================ */
2444
2445 TEST(dns_resource_record_clamp_ttl_in_place) {
2446 DnsResourceRecord *rr = NULL, *orig = NULL;
2447
2448 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
2449 ASSERT_NOT_NULL(rr);
2450 orig = rr;
2451 rr->ttl = 3600;
2452
2453 ASSERT_FALSE(dns_resource_record_clamp_ttl(&rr, 4800));
2454 ASSERT_EQ(rr->ttl, 3600u);
2455
2456 ASSERT_TRUE(dns_resource_record_clamp_ttl(&rr, 2400));
2457 ASSERT_EQ(rr->ttl, 2400u);
2458
2459 ASSERT_TRUE(rr == orig);
2460
2461 dns_resource_record_unref(rr);
2462 }
2463
2464 TEST(dns_resource_record_clamp_ttl_copy) {
2465 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *orig = NULL;
2466
2467 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
2468 ASSERT_NOT_NULL(rr);
2469 orig = dns_resource_record_ref(rr);
2470 rr->ttl = 3600;
2471
2472 ASSERT_FALSE(dns_resource_record_clamp_ttl(&rr, 4800));
2473 ASSERT_EQ(rr->ttl, 3600u);
2474
2475 ASSERT_TRUE(dns_resource_record_clamp_ttl(&rr, 2400));
2476 ASSERT_EQ(rr->ttl, 2400u);
2477
2478 ASSERT_FALSE(rr == orig);
2479 ASSERT_EQ(orig->ttl, 3600u);
2480 }
2481
2482 static void test_from_json(const char *text, int expected) {
2483 log_notice("Trying to parse as JSON RR: %s", text);
2484 _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
2485 ASSERT_OK(sd_json_parse(text, /* flags= */ 0, &j, /* reterr_line= */ NULL, /* reterr_column= */ NULL));
2486 ASSERT_EQ(dns_resource_record_from_json(j, NULL), expected);
2487 }
2488
2489 TEST(from_bad_json) {
2490 test_from_json("{}", -EBADMSG);
2491 test_from_json("{\"key\":{}}", -ENXIO);
2492 test_from_json("{\"key\":{\"name\":\"foobar\",\"type\":9}}", -EOPNOTSUPP);
2493 test_from_json("{\"key\":{\"name\":\"foobar\"}}", -ENXIO);
2494 test_from_json("{\"key\":{\"type\":9}}", -ENXIO);
2495 test_from_json("{\"key\":{\"name\":\"foobar\",\"type\":1}}", -ENXIO);
2496 test_from_json("{\"key\":{\"name\":\"foobar\",\"type\":1},\"address\":[1,2,3,4]}", 0);
2497 test_from_json("{\"key\":{\"name\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"type\":1},\"address\":[1,2,3,4]}", 0);
2498 test_from_json("{\"key\":{\"name\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"type\":1},\"address\":[1,2,3,4]}", -EBADMSG);
2499 test_from_json("{\"key\":{\"name\":\"a.a\",\"type\":1},\"address\":[1,2,3,4]}", 0);
2500 test_from_json("{\"key\":{\"name\":\"a..a\",\"type\":1},\"address\":[1,2,3,4]}", -EBADMSG);
2501 }
2502
2503 DEFINE_TEST_MAIN(LOG_DEBUG);