]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/test-dns-packet.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / resolve / test-dns-packet.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <net/if.h>
4 #include <glob.h>
5
6 #include "sd-id128.h"
7
8 #include "alloc-util.h"
9 #include "fileio.h"
10 #include "glob-util.h"
11 #include "log.h"
12 #include "macro.h"
13 #include "resolved-dns-packet.h"
14 #include "resolved-dns-rr.h"
15 #include "path-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "tests.h"
19 #include "unaligned.h"
20
21 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
22
23 static void verify_rr_copy(DnsResourceRecord *rr) {
24 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL;
25 const char *a, *b;
26
27 assert_se(copy = dns_resource_record_copy(rr));
28 assert_se(dns_resource_record_equal(copy, rr) > 0);
29
30 assert_se(a = dns_resource_record_to_string(rr));
31 assert_se(b = dns_resource_record_to_string(copy));
32
33 assert_se(streq(a, b));
34 }
35
36 static uint64_t hash(DnsResourceRecord *rr) {
37 struct siphash state;
38
39 siphash24_init(&state, HASH_KEY.bytes);
40 dns_resource_record_hash_func(rr, &state);
41 return siphash24_finalize(&state);
42 }
43
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;
47
48 assert_se(read_full_file(filename, &data, &data_size) >= 0);
49 assert_se(data);
50 assert_se(data_size > 8);
51
52 log_info("============== %s %s==============", filename, canonical ? "canonical " : "");
53
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;
57 const char *s, *s2;
58 uint64_t hash1, hash2;
59
60 packet_size = unaligned_read_le64(data + offset);
61 assert_se(packet_size > 0);
62 assert_se(offset + 8 + packet_size <= data_size);
63
64 assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
65
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);
68
69 verify_rr_copy(rr);
70
71 s = dns_resource_record_to_string(rr);
72 assert_se(s);
73 puts(s);
74
75 hash1 = hash(rr);
76
77 assert_se(dns_resource_record_to_wire_format(rr, canonical) >= 0);
78
79 assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
80 assert_se(dns_packet_append_blob(p2, rr->wire_format, rr->wire_format_size, NULL) >= 0);
81 assert_se(dns_packet_read_rr(p2, &rr2, NULL, NULL) >= 0);
82
83 verify_rr_copy(rr);
84
85 s2 = dns_resource_record_to_string(rr);
86 assert_se(s2);
87 assert_se(streq(s, s2));
88
89 hash2 = hash(rr);
90 assert_se(hash1 == hash2);
91 }
92 }
93
94 int main(int argc, char **argv) {
95 int i, N;
96 _cleanup_free_ char *pkts_glob = NULL;
97 _cleanup_globfree_ glob_t g = {};
98 char **fnames;
99
100 log_parse_environment();
101
102 if (argc >= 2) {
103 N = argc - 1;
104 fnames = argv + 1;
105 } else {
106 pkts_glob = path_join(NULL, get_testdata_dir(), "test-resolve/*.pkts");
107 assert_se(glob(pkts_glob, 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 }