1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
12 #include "resolved-dns-packet.h"
13 #include "resolved-dns-rr.h"
14 #include "path-util.h"
15 #include "string-util.h"
18 #include "unaligned.h"
20 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
22 static void verify_rr_copy(DnsResourceRecord
*rr
) {
23 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*copy
= NULL
;
26 assert_se(copy
= dns_resource_record_copy(rr
));
27 assert_se(dns_resource_record_equal(copy
, rr
) > 0);
29 assert_se(a
= dns_resource_record_to_string(rr
));
30 assert_se(b
= dns_resource_record_to_string(copy
));
32 assert_se(streq(a
, b
));
35 static uint64_t hash(DnsResourceRecord
*rr
) {
38 siphash24_init(&state
, HASH_KEY
.bytes
);
39 dns_resource_record_hash_func(rr
, &state
);
40 return siphash24_finalize(&state
);
43 static void test_packet_from_file(const char* filename
, bool canonical
) {
44 _cleanup_free_
char *data
= NULL
;
45 size_t data_size
, packet_size
, offset
;
47 assert_se(read_full_file(filename
, &data
, &data_size
) >= 0);
49 assert_se(data_size
> 8);
51 log_info("============== %s %s==============", filename
, canonical
? "canonical " : "");
53 for (offset
= 0; offset
< data_size
; offset
+= 8 + packet_size
) {
54 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
, *p2
= NULL
;
55 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
, *rr2
= NULL
;
57 uint64_t hash1
, hash2
;
59 packet_size
= unaligned_read_le64(data
+ offset
);
60 assert_se(packet_size
> 0);
61 assert_se(offset
+ 8 + packet_size
<= data_size
);
63 assert_se(dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0, DNS_PACKET_SIZE_MAX
) >= 0);
65 assert_se(dns_packet_append_blob(p
, data
+ offset
+ 8, packet_size
, NULL
) >= 0);
66 assert_se(dns_packet_read_rr(p
, &rr
, NULL
, NULL
) >= 0);
70 s
= dns_resource_record_to_string(rr
);
76 assert_se(dns_resource_record_to_wire_format(rr
, canonical
) >= 0);
78 assert_se(dns_packet_new(&p2
, DNS_PROTOCOL_DNS
, 0, DNS_PACKET_SIZE_MAX
) >= 0);
79 assert_se(dns_packet_append_blob(p2
, rr
->wire_format
, rr
->wire_format_size
, NULL
) >= 0);
80 assert_se(dns_packet_read_rr(p2
, &rr2
, NULL
, NULL
) >= 0);
84 s2
= dns_resource_record_to_string(rr
);
86 assert_se(streq(s
, s2
));
89 assert_se(hash1
== hash2
);
93 static void test_dns_resource_record_get_cname_target(void) {
94 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*cname
= NULL
, *dname
= NULL
;
95 _cleanup_free_
char *target
= NULL
;
97 assert_se(cname
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_CNAME
, "quux.foobar"));
98 assert_se(cname
->cname
.name
= strdup("wuff.wuff"));
100 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "waldo"), cname
, &target
) == -EUNATCH
);
101 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "foobar"), cname
, &target
) == -EUNATCH
);
102 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "quux"), cname
, &target
) == -EUNATCH
);
103 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, ""), cname
, &target
) == -EUNATCH
);
104 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "."), cname
, &target
) == -EUNATCH
);
105 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "nope.quux.foobar"), cname
, &target
) == -EUNATCH
);
106 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "quux.foobar"), cname
, &target
) == 0);
107 assert_se(streq(target
, "wuff.wuff"));
108 target
= mfree(target
);
110 assert_se(dname
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_DNAME
, "quux.foobar"));
111 assert_se(dname
->dname
.name
= strdup("wuff.wuff"));
113 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "waldo"), dname
, &target
) == -EUNATCH
);
114 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "foobar"), dname
, &target
) == -EUNATCH
);
115 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "quux"), dname
, &target
) == -EUNATCH
);
116 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, ""), dname
, &target
) == -EUNATCH
);
117 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "."), dname
, &target
) == -EUNATCH
);
118 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "yupp.quux.foobar"), dname
, &target
) == 0);
119 assert_se(streq(target
, "yupp.wuff.wuff"));
120 target
= mfree(target
);
122 assert_se(dns_resource_record_get_cname_target(&DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_A
, "quux.foobar"), cname
, &target
) == 0);
123 assert_se(streq(target
, "wuff.wuff"));
126 int main(int argc
, char **argv
) {
128 _cleanup_globfree_ glob_t g
= {};
131 test_setup_logging(LOG_DEBUG
);
137 _cleanup_free_
char *pkts_glob
= NULL
;
138 assert_se(get_testdata_dir("test-resolve/*.pkts", &pkts_glob
) >= 0);
139 assert_se(glob(pkts_glob
, GLOB_NOSORT
, NULL
, &g
) == 0);
144 for (int i
= 0; i
< N
; i
++) {
145 test_packet_from_file(fnames
[i
], false);
147 test_packet_from_file(fnames
[i
], true);
152 test_dns_resource_record_get_cname_target();