2 This file is part of systemd
4 Copyright 2016 Zbigniew Jędrzejewski-Szmek
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
25 #include "glob-util.h"
28 #include "resolved-dns-packet.h"
29 #include "resolved-dns-rr.h"
30 #include "string-util.h"
32 #include "unaligned.h"
34 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
36 static uint64_t hash(DnsResourceRecord
*rr
) {
39 siphash24_init(&state
, HASH_KEY
.bytes
);
40 dns_resource_record_hash_func(rr
, &state
);
41 return siphash24_finalize(&state
);
44 static void test_packet_from_file(const char* filename
, bool canonical
) {
45 _cleanup_free_
char *data
= NULL
;
46 size_t data_size
, packet_size
, offset
;
48 assert_se(read_full_file(filename
, &data
, &data_size
) >= 0);
50 assert_se(data_size
> 8);
52 log_info("============== %s %s==============", filename
, canonical
? "canonical " : "");
54 for (offset
= 0; offset
< data_size
; offset
+= 8 + packet_size
) {
55 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
, *p2
= NULL
;
56 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
, *rr2
= NULL
;
58 uint64_t hash1
, hash2
;
60 packet_size
= unaligned_read_le64(data
+ offset
);
61 assert_se(packet_size
> 0);
62 assert_se(offset
+ 8 + packet_size
<= data_size
);
64 assert_se(dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0) >= 0);
66 assert_se(dns_packet_append_blob(p
, data
+ offset
+ 8, packet_size
, NULL
) >= 0);
67 assert_se(dns_packet_read_rr(p
, &rr
, NULL
, NULL
) >= 0);
69 s
= dns_resource_record_to_string(rr
);
75 assert_se(dns_resource_record_to_wire_format(rr
, canonical
) >= 0);
77 assert_se(dns_packet_new(&p2
, DNS_PROTOCOL_DNS
, 0) >= 0);
78 assert_se(dns_packet_append_blob(p2
, rr
->wire_format
, rr
->wire_format_size
, NULL
) >= 0);
79 assert_se(dns_packet_read_rr(p2
, &rr2
, NULL
, NULL
) >= 0);
81 s2
= dns_resource_record_to_string(rr
);
83 assert_se(streq(s
, s2
));
86 assert_se(hash1
== hash2
);
90 int main(int argc
, char **argv
) {
92 _cleanup_globfree_ glob_t g
= {};
95 log_parse_environment();
101 assert_se(glob(RESOLVE_TEST_DIR
"/*.pkts", GLOB_NOSORT
, NULL
, &g
) == 0);
106 for (i
= 0; i
< N
; i
++) {
107 test_packet_from_file(fnames
[i
], false);
109 test_packet_from_file(fnames
[i
], true);