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