]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/test-dns-packet.c
Merge pull request #2603 from poettering/drop-compat-libs
[thirdparty/systemd.git] / src / resolve / test-dns-packet.c
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 #include <glob.h>
22
23 #include "alloc-util.h"
24 #include "fileio.h"
25 #include "glob-util.h"
26 #include "log.h"
27 #include "macro.h"
28 #include "resolved-dns-packet.h"
29 #include "resolved-dns-rr.h"
30 #include "string-util.h"
31 #include "strv.h"
32
33 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
34
35 static uint64_t hash(DnsResourceRecord *rr) {
36 struct siphash state;
37
38 siphash24_init(&state, HASH_KEY.bytes);
39 dns_resource_record_hash_func(rr, &state);
40 return siphash24_finalize(&state);
41 }
42
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;
46
47 assert_se(read_full_file(filename, &data, &data_size) >= 0);
48 assert_se(data);
49 assert_se(data_size > 8);
50
51 log_info("============== %s %s==============", filename, canonical ? "canonical " : "");
52
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;
56 const char *s, *s2;
57 uint64_t hash1, hash2;
58
59 packet_size = le64toh( *(uint64_t*)(data + offset) );
60 assert_se(packet_size > 0);
61 assert_se(offset + 8 + packet_size <= data_size);
62
63 assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, 0) >= 0);
64
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);
67
68 s = dns_resource_record_to_string(rr);
69 assert_se(s);
70 puts(s);
71
72 hash1 = hash(rr);
73
74 assert_se(dns_resource_record_to_wire_format(rr, canonical) >= 0);
75
76 assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, 0) >= 0);
77 assert_se(dns_packet_append_blob(p2, rr->wire_format, rr->wire_format_size, NULL) >= 0);
78 assert_se(dns_packet_read_rr(p2, &rr2, NULL, NULL) >= 0);
79
80 s2 = dns_resource_record_to_string(rr);
81 assert_se(s2);
82 assert_se(streq(s, s2));
83
84 hash2 = hash(rr);
85 assert_se(hash1 == hash2);
86 }
87 }
88
89 int main(int argc, char **argv) {
90 int i, N;
91 _cleanup_globfree_ glob_t g = {};
92 _cleanup_strv_free_ char **globs = NULL;
93 char **fnames;
94
95 log_parse_environment();
96
97 if (argc >= 2) {
98 N = argc - 1;
99 fnames = argv + 1;
100 } else {
101 assert_se(glob(RESOLVE_TEST_DIR "/*.pkts", GLOB_NOSORT, NULL, &g) == 0);
102 N = g.gl_pathc;
103 fnames = g.gl_pathv;
104 }
105
106 for (i = 0; i < N; i++) {
107 test_packet_from_file(fnames[i], false);
108 puts("");
109 test_packet_from_file(fnames[i], true);
110 if (i + 1 < N)
111 puts("");
112 }
113
114 return EXIT_SUCCESS;
115 }