]>
Commit | Line | Data |
---|---|---|
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 ZJS |
23 | |
24 | #include "alloc-util.h" | |
25 | #include "fileio.h" | |
fff85dbe | 26 | #include "glob-util.h" |
6d99904f | 27 | #include "log.h" |
fff85dbe | 28 | #include "macro.h" |
6d99904f ZJS |
29 | #include "resolved-dns-packet.h" |
30 | #include "resolved-dns-rr.h" | |
31 | #include "string-util.h" | |
fff85dbe | 32 | #include "strv.h" |
cc100a5a | 33 | #include "tests.h" |
aa912320 | 34 | #include "unaligned.h" |
6d99904f ZJS |
35 | |
36 | #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1) | |
37 | ||
17c8de63 LP |
38 | static void verify_rr_copy(DnsResourceRecord *rr) { |
39 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL; | |
40 | const char *a, *b; | |
41 | ||
42 | assert_se(copy = dns_resource_record_copy(rr)); | |
43 | assert_se(dns_resource_record_equal(copy, rr) > 0); | |
44 | ||
45 | assert_se(a = dns_resource_record_to_string(rr)); | |
46 | assert_se(b = dns_resource_record_to_string(copy)); | |
47 | ||
48 | assert_se(streq(a, b)); | |
49 | } | |
50 | ||
6d99904f ZJS |
51 | static uint64_t hash(DnsResourceRecord *rr) { |
52 | struct siphash state; | |
53 | ||
54 | siphash24_init(&state, HASH_KEY.bytes); | |
55 | dns_resource_record_hash_func(rr, &state); | |
56 | return siphash24_finalize(&state); | |
57 | } | |
58 | ||
59 | static void test_packet_from_file(const char* filename, bool canonical) { | |
60 | _cleanup_free_ char *data = NULL; | |
61 | size_t data_size, packet_size, offset; | |
62 | ||
63 | assert_se(read_full_file(filename, &data, &data_size) >= 0); | |
64 | assert_se(data); | |
65 | assert_se(data_size > 8); | |
66 | ||
67 | log_info("============== %s %s==============", filename, canonical ? "canonical " : ""); | |
68 | ||
69 | for (offset = 0; offset < data_size; offset += 8 + packet_size) { | |
70 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL, *p2 = NULL; | |
71 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *rr2 = NULL; | |
72 | const char *s, *s2; | |
73 | uint64_t hash1, hash2; | |
74 | ||
aa912320 | 75 | packet_size = unaligned_read_le64(data + offset); |
6d99904f ZJS |
76 | assert_se(packet_size > 0); |
77 | assert_se(offset + 8 + packet_size <= data_size); | |
78 | ||
51027656 | 79 | assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0); |
6d99904f ZJS |
80 | |
81 | assert_se(dns_packet_append_blob(p, data + offset + 8, packet_size, NULL) >= 0); | |
82 | assert_se(dns_packet_read_rr(p, &rr, NULL, NULL) >= 0); | |
83 | ||
17c8de63 LP |
84 | verify_rr_copy(rr); |
85 | ||
6d99904f ZJS |
86 | s = dns_resource_record_to_string(rr); |
87 | assert_se(s); | |
88 | puts(s); | |
89 | ||
90 | hash1 = hash(rr); | |
91 | ||
92 | assert_se(dns_resource_record_to_wire_format(rr, canonical) >= 0); | |
93 | ||
51027656 | 94 | assert_se(dns_packet_new(&p2, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX) >= 0); |
6d99904f ZJS |
95 | assert_se(dns_packet_append_blob(p2, rr->wire_format, rr->wire_format_size, NULL) >= 0); |
96 | assert_se(dns_packet_read_rr(p2, &rr2, NULL, NULL) >= 0); | |
97 | ||
17c8de63 LP |
98 | verify_rr_copy(rr); |
99 | ||
6d99904f ZJS |
100 | s2 = dns_resource_record_to_string(rr); |
101 | assert_se(s2); | |
102 | assert_se(streq(s, s2)); | |
103 | ||
104 | hash2 = hash(rr); | |
105 | assert_se(hash1 == hash2); | |
106 | } | |
107 | } | |
108 | ||
109 | int main(int argc, char **argv) { | |
fff85dbe ZJS |
110 | int i, N; |
111 | _cleanup_globfree_ glob_t g = {}; | |
fff85dbe | 112 | char **fnames; |
6d99904f ZJS |
113 | |
114 | log_parse_environment(); | |
115 | ||
fff85dbe ZJS |
116 | if (argc >= 2) { |
117 | N = argc - 1; | |
118 | fnames = argv + 1; | |
119 | } else { | |
cc100a5a | 120 | assert_se(glob(get_testdata_dir("/test-resolve/*.pkts"), GLOB_NOSORT, NULL, &g) == 0); |
fff85dbe ZJS |
121 | N = g.gl_pathc; |
122 | fnames = g.gl_pathv; | |
123 | } | |
124 | ||
125 | for (i = 0; i < N; i++) { | |
126 | test_packet_from_file(fnames[i], false); | |
6d99904f | 127 | puts(""); |
fff85dbe ZJS |
128 | test_packet_from_file(fnames[i], true); |
129 | if (i + 1 < N) | |
6d99904f ZJS |
130 | puts(""); |
131 | } | |
132 | ||
133 | return EXIT_SUCCESS; | |
134 | } |