1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd
5 Copyright 2016 Zbigniew Jędrzejewski-Szmek
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
26 #include "glob-util.h"
29 #include "resolved-dns-packet.h"
30 #include "resolved-dns-rr.h"
31 #include "string-util.h"
34 #include "unaligned.h"
36 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
38 static void verify_rr_copy(DnsResourceRecord
*rr
) {
39 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*copy
= NULL
;
42 assert_se(copy
= dns_resource_record_copy(rr
));
43 assert_se(dns_resource_record_equal(copy
, rr
) > 0);
45 assert_se(a
= dns_resource_record_to_string(rr
));
46 assert_se(b
= dns_resource_record_to_string(copy
));
48 assert_se(streq(a
, b
));
51 static uint64_t hash(DnsResourceRecord
*rr
) {
54 siphash24_init(&state
, HASH_KEY
.bytes
);
55 dns_resource_record_hash_func(rr
, &state
);
56 return siphash24_finalize(&state
);
59 static void test_packet_from_file(const char* filename
, bool canonical
) {
60 _cleanup_free_
char *data
= NULL
;
61 size_t data_size
, packet_size
, offset
;
63 assert_se(read_full_file(filename
, &data
, &data_size
) >= 0);
65 assert_se(data_size
> 8);
67 log_info("============== %s %s==============", filename
, canonical
? "canonical " : "");
69 for (offset
= 0; offset
< data_size
; offset
+= 8 + packet_size
) {
70 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
, *p2
= NULL
;
71 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
, *rr2
= NULL
;
73 uint64_t hash1
, hash2
;
75 packet_size
= unaligned_read_le64(data
+ offset
);
76 assert_se(packet_size
> 0);
77 assert_se(offset
+ 8 + packet_size
<= data_size
);
79 assert_se(dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0, DNS_PACKET_SIZE_MAX
) >= 0);
81 assert_se(dns_packet_append_blob(p
, data
+ offset
+ 8, packet_size
, NULL
) >= 0);
82 assert_se(dns_packet_read_rr(p
, &rr
, NULL
, NULL
) >= 0);
86 s
= dns_resource_record_to_string(rr
);
92 assert_se(dns_resource_record_to_wire_format(rr
, canonical
) >= 0);
94 assert_se(dns_packet_new(&p2
, DNS_PROTOCOL_DNS
, 0, DNS_PACKET_SIZE_MAX
) >= 0);
95 assert_se(dns_packet_append_blob(p2
, rr
->wire_format
, rr
->wire_format_size
, NULL
) >= 0);
96 assert_se(dns_packet_read_rr(p2
, &rr2
, NULL
, NULL
) >= 0);
100 s2
= dns_resource_record_to_string(rr
);
102 assert_se(streq(s
, s2
));
105 assert_se(hash1
== hash2
);
109 int main(int argc
, char **argv
) {
111 _cleanup_globfree_ glob_t g
= {};
114 log_parse_environment();
120 assert_se(glob(get_testdata_dir("/test-resolve/*.pkts"), GLOB_NOSORT
, NULL
, &g
) == 0);
125 for (i
= 0; i
< N
; i
++) {
126 test_packet_from_file(fnames
[i
], false);
128 test_packet_from_file(fnames
[i
], true);