]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/test-dns-packet.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / resolve / test-dns-packet.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2016 Zbigniew Jędrzejewski-Szmek
4 ***/
5
6 #include <net/if.h>
7 #include <glob.h>
8
9 #include "sd-id128.h"
10
11 #include "alloc-util.h"
12 #include "fileio.h"
13 #include "glob-util.h"
14 #include "log.h"
15 #include "macro.h"
16 #include "resolved-dns-packet.h"
17 #include "resolved-dns-rr.h"
18 #include "string-util.h"
19 #include "strv.h"
20 #include "tests.h"
21 #include "unaligned.h"
22
23 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
24
25 static void verify_rr_copy(DnsResourceRecord *rr) {
26 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL;
27 const char *a, *b;
28
29 assert_se(copy = dns_resource_record_copy(rr));
30 assert_se(dns_resource_record_equal(copy, rr) > 0);
31
32 assert_se(a = dns_resource_record_to_string(rr));
33 assert_se(b = dns_resource_record_to_string(copy));
34
35 assert_se(streq(a, b));
36 }
37
38 static uint64_t hash(DnsResourceRecord *rr) {
39 struct siphash state;
40
41 siphash24_init(&state, HASH_KEY.bytes);
42 dns_resource_record_hash_func(rr, &state);
43 return siphash24_finalize(&state);
44 }
45
46 static void test_packet_from_file(const char* filename, bool canonical) {
47 _cleanup_free_ char *data = NULL;
48 size_t data_size, packet_size, offset;
49
50 assert_se(read_full_file(filename, &data, &data_size) >= 0);
51 assert_se(data);
52 assert_se(data_size > 8);
53
54 log_info("============== %s %s==============", filename, canonical ? "canonical " : "");
55
56 for (offset = 0; offset < data_size; offset += 8 + packet_size) {
57 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL, *p2 = NULL;
58 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *rr2 = NULL;
59 const char *s, *s2;
60 uint64_t hash1, hash2;
61
62 packet_size = unaligned_read_le64(data + offset);
63 assert_se(packet_size > 0);
64 assert_se(offset + 8 + packet_size <= data_size);
65
66 assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
67
68 assert_se(dns_packet_append_blob(p, data + offset + 8, packet_size, NULL) >= 0);
69 assert_se(dns_packet_read_rr(p, &rr, NULL, NULL) >= 0);
70
71 verify_rr_copy(rr);
72
73 s = dns_resource_record_to_string(rr);
74 assert_se(s);
75 puts(s);
76
77 hash1 = hash(rr);
78
79 assert_se(dns_resource_record_to_wire_format(rr, canonical) >= 0);
80
81 assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
82 assert_se(dns_packet_append_blob(p2, rr->wire_format, rr->wire_format_size, NULL) >= 0);
83 assert_se(dns_packet_read_rr(p2, &rr2, NULL, NULL) >= 0);
84
85 verify_rr_copy(rr);
86
87 s2 = dns_resource_record_to_string(rr);
88 assert_se(s2);
89 assert_se(streq(s, s2));
90
91 hash2 = hash(rr);
92 assert_se(hash1 == hash2);
93 }
94 }
95
96 int main(int argc, char **argv) {
97 int i, N;
98 _cleanup_globfree_ glob_t g = {};
99 char **fnames;
100
101 log_parse_environment();
102
103 if (argc >= 2) {
104 N = argc - 1;
105 fnames = argv + 1;
106 } else {
107 assert_se(glob(get_testdata_dir("/test-resolve/*.pkts"), GLOB_NOSORT, NULL, &g) == 0);
108 N = g.gl_pathc;
109 fnames = g.gl_pathv;
110 }
111
112 for (i = 0; i < N; i++) {
113 test_packet_from_file(fnames[i], false);
114 puts("");
115 test_packet_from_file(fnames[i], true);
116 if (i + 1 < N)
117 puts("");
118 }
119
120 return EXIT_SUCCESS;
121 }