]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/test-dns-packet.c
Merge pull request #8025 from sourcejedi/pid1_journal_or2
[thirdparty/systemd.git] / src / resolve / test-dns-packet.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
6d99904f
ZJS
2/***
3 This file is part of systemd
4
5 Copyright 2016 Zbigniew Jędrzejewski-Szmek
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
21#include <net/if.h>
fff85dbe 22#include <glob.h>
6d99904f 23
dccca82b
LP
24#include "sd-id128.h"
25
6d99904f
ZJS
26#include "alloc-util.h"
27#include "fileio.h"
fff85dbe 28#include "glob-util.h"
6d99904f 29#include "log.h"
fff85dbe 30#include "macro.h"
6d99904f
ZJS
31#include "resolved-dns-packet.h"
32#include "resolved-dns-rr.h"
33#include "string-util.h"
fff85dbe 34#include "strv.h"
cc100a5a 35#include "tests.h"
aa912320 36#include "unaligned.h"
6d99904f
ZJS
37
38#define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
39
17c8de63
LP
40static void verify_rr_copy(DnsResourceRecord *rr) {
41 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL;
42 const char *a, *b;
43
44 assert_se(copy = dns_resource_record_copy(rr));
45 assert_se(dns_resource_record_equal(copy, rr) > 0);
46
47 assert_se(a = dns_resource_record_to_string(rr));
48 assert_se(b = dns_resource_record_to_string(copy));
49
50 assert_se(streq(a, b));
51}
52
6d99904f
ZJS
53static uint64_t hash(DnsResourceRecord *rr) {
54 struct siphash state;
55
56 siphash24_init(&state, HASH_KEY.bytes);
57 dns_resource_record_hash_func(rr, &state);
58 return siphash24_finalize(&state);
59}
60
61static void test_packet_from_file(const char* filename, bool canonical) {
62 _cleanup_free_ char *data = NULL;
63 size_t data_size, packet_size, offset;
64
65 assert_se(read_full_file(filename, &data, &data_size) >= 0);
66 assert_se(data);
67 assert_se(data_size > 8);
68
69 log_info("============== %s %s==============", filename, canonical ? "canonical " : "");
70
71 for (offset = 0; offset < data_size; offset += 8 + packet_size) {
72 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL, *p2 = NULL;
73 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *rr2 = NULL;
74 const char *s, *s2;
75 uint64_t hash1, hash2;
76
aa912320 77 packet_size = unaligned_read_le64(data + offset);
6d99904f
ZJS
78 assert_se(packet_size > 0);
79 assert_se(offset + 8 + packet_size <= data_size);
80
51027656 81 assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
6d99904f
ZJS
82
83 assert_se(dns_packet_append_blob(p, data + offset + 8, packet_size, NULL) >= 0);
84 assert_se(dns_packet_read_rr(p, &rr, NULL, NULL) >= 0);
85
17c8de63
LP
86 verify_rr_copy(rr);
87
6d99904f
ZJS
88 s = dns_resource_record_to_string(rr);
89 assert_se(s);
90 puts(s);
91
92 hash1 = hash(rr);
93
94 assert_se(dns_resource_record_to_wire_format(rr, canonical) >= 0);
95
51027656 96 assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0);
6d99904f
ZJS
97 assert_se(dns_packet_append_blob(p2, rr->wire_format, rr->wire_format_size, NULL) >= 0);
98 assert_se(dns_packet_read_rr(p2, &rr2, NULL, NULL) >= 0);
99
17c8de63
LP
100 verify_rr_copy(rr);
101
6d99904f
ZJS
102 s2 = dns_resource_record_to_string(rr);
103 assert_se(s2);
104 assert_se(streq(s, s2));
105
106 hash2 = hash(rr);
107 assert_se(hash1 == hash2);
108 }
109}
110
111int main(int argc, char **argv) {
fff85dbe
ZJS
112 int i, N;
113 _cleanup_globfree_ glob_t g = {};
fff85dbe 114 char **fnames;
6d99904f
ZJS
115
116 log_parse_environment();
117
fff85dbe
ZJS
118 if (argc >= 2) {
119 N = argc - 1;
120 fnames = argv + 1;
121 } else {
cc100a5a 122 assert_se(glob(get_testdata_dir("/test-resolve/*.pkts"), GLOB_NOSORT, NULL, &g) == 0);
fff85dbe
ZJS
123 N = g.gl_pathc;
124 fnames = g.gl_pathv;
125 }
126
127 for (i = 0; i < N; i++) {
128 test_packet_from_file(fnames[i], false);
6d99904f 129 puts("");
fff85dbe
ZJS
130 test_packet_from_file(fnames[i], true);
131 if (i + 1 < N)
6d99904f
ZJS
132 puts("");
133 }
134
135 return EXIT_SUCCESS;
136}