]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/test-dns-packet.c
Merge pull request #17549 from yuwata/tiny-fixes
[thirdparty/systemd.git] / src / resolve / test-dns-packet.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
6d99904f
ZJS
2
3#include <net/if.h>
4
dccca82b
LP
5#include "sd-id128.h"
6
6d99904f
ZJS
7#include "alloc-util.h"
8#include "fileio.h"
fff85dbe 9#include "glob-util.h"
6d99904f 10#include "log.h"
fff85dbe 11#include "macro.h"
6d99904f
ZJS
12#include "resolved-dns-packet.h"
13#include "resolved-dns-rr.h"
55890a40 14#include "path-util.h"
6d99904f 15#include "string-util.h"
fff85dbe 16#include "strv.h"
cc100a5a 17#include "tests.h"
aa912320 18#include "unaligned.h"
6d99904f
ZJS
19
20#define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
21
17c8de63
LP
22static void verify_rr_copy(DnsResourceRecord *rr) {
23 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL;
24 const char *a, *b;
25
26 assert_se(copy = dns_resource_record_copy(rr));
27 assert_se(dns_resource_record_equal(copy, rr) > 0);
28
29 assert_se(a = dns_resource_record_to_string(rr));
30 assert_se(b = dns_resource_record_to_string(copy));
31
32 assert_se(streq(a, b));
33}
34
6d99904f
ZJS
35static 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
43static 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
aa912320 59 packet_size = unaligned_read_le64(data + offset);
6d99904f
ZJS
60 assert_se(packet_size > 0);
61 assert_se(offset + 8 + packet_size <= data_size);
62
51027656 63 assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
6d99904f
ZJS
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
17c8de63
LP
68 verify_rr_copy(rr);
69
6d99904f
ZJS
70 s = dns_resource_record_to_string(rr);
71 assert_se(s);
72 puts(s);
73
74 hash1 = hash(rr);
75
76 assert_se(dns_resource_record_to_wire_format(rr, canonical) >= 0);
77
51027656 78 assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
6d99904f
ZJS
79 assert_se(dns_packet_append_blob(p2, rr->wire_format, rr->wire_format_size, NULL) >= 0);
80 assert_se(dns_packet_read_rr(p2, &rr2, NULL, NULL) >= 0);
81
17c8de63
LP
82 verify_rr_copy(rr);
83
6d99904f
ZJS
84 s2 = dns_resource_record_to_string(rr);
85 assert_se(s2);
86 assert_se(streq(s, s2));
87
88 hash2 = hash(rr);
89 assert_se(hash1 == hash2);
90 }
91}
92
93int main(int argc, char **argv) {
fff85dbe
ZJS
94 int i, N;
95 _cleanup_globfree_ glob_t g = {};
fff85dbe 96 char **fnames;
6d99904f
ZJS
97
98 log_parse_environment();
99
fff85dbe
ZJS
100 if (argc >= 2) {
101 N = argc - 1;
102 fnames = argv + 1;
103 } else {
7b432953
ZJS
104 _cleanup_free_ char *pkts_glob = NULL;
105 assert_se(get_testdata_dir("test-resolve/*.pkts", &pkts_glob) >= 0);
55890a40 106 assert_se(glob(pkts_glob, GLOB_NOSORT, NULL, &g) == 0);
fff85dbe
ZJS
107 N = g.gl_pathc;
108 fnames = g.gl_pathv;
109 }
110
111 for (i = 0; i < N; i++) {
112 test_packet_from_file(fnames[i], false);
6d99904f 113 puts("");
fff85dbe
ZJS
114 test_packet_from_file(fnames[i], true);
115 if (i + 1 < N)
6d99904f
ZJS
116 puts("");
117 }
118
119 return EXIT_SUCCESS;
120}