]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/test-dns-packet.c
test-dns-packet: add framework to read and dump packets
[thirdparty/systemd.git] / src / resolve / test-dns-packet.c
CommitLineData
6d99904f
ZJS
1/***
2 This file is part of systemd
3
4 Copyright 2016 Zbigniew Jędrzejewski-Szmek
5
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.
10
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.
15
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/>.
18***/
19
20#include <net/if.h>
21
22#include "alloc-util.h"
23#include "fileio.h"
24#include "macro.h"
25#include "log.h"
26#include "resolved-dns-packet.h"
27#include "resolved-dns-rr.h"
28#include "string-util.h"
29
30#define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
31
32static uint64_t hash(DnsResourceRecord *rr) {
33 struct siphash state;
34
35 siphash24_init(&state, HASH_KEY.bytes);
36 dns_resource_record_hash_func(rr, &state);
37 return siphash24_finalize(&state);
38}
39
40static void test_packet_from_file(const char* filename, bool canonical) {
41 _cleanup_free_ char *data = NULL;
42 size_t data_size, packet_size, offset;
43
44 assert_se(read_full_file(filename, &data, &data_size) >= 0);
45 assert_se(data);
46 assert_se(data_size > 8);
47
48 log_info("============== %s %s==============", filename, canonical ? "canonical " : "");
49
50 for (offset = 0; offset < data_size; offset += 8 + packet_size) {
51 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL, *p2 = NULL;
52 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *rr2 = NULL;
53 const char *s, *s2;
54 uint64_t hash1, hash2;
55
56 packet_size = le64toh( *(uint64_t*)(data + offset) );
57 assert_se(packet_size > 0);
58 assert_se(offset + 8 + packet_size <= data_size);
59
60 assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, 0) >= 0);
61
62 assert_se(dns_packet_append_blob(p, data + offset + 8, packet_size, NULL) >= 0);
63 assert_se(dns_packet_read_rr(p, &rr, NULL, NULL) >= 0);
64
65 s = dns_resource_record_to_string(rr);
66 assert_se(s);
67 puts(s);
68
69 hash1 = hash(rr);
70
71 assert_se(dns_resource_record_to_wire_format(rr, canonical) >= 0);
72
73 assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, 0) >= 0);
74 assert_se(dns_packet_append_blob(p2, rr->wire_format, rr->wire_format_size, NULL) >= 0);
75 assert_se(dns_packet_read_rr(p2, &rr2, NULL, NULL) >= 0);
76
77 s2 = dns_resource_record_to_string(rr);
78 assert_se(s2);
79 assert_se(streq(s, s2));
80
81 hash2 = hash(rr);
82 assert_se(hash1 == hash2);
83 }
84}
85
86int main(int argc, char **argv) {
87 int i;
88
89 log_parse_environment();
90
91 for (i = 1; i < argc; i++) {
92 test_packet_from_file(argv[i], false);
93 puts("");
94 test_packet_from_file(argv[i], true);
95 if (i + 1 < argc)
96 puts("");
97 }
98
99 return EXIT_SUCCESS;
100}