]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
74b2466e | 2 | /*** |
74b2466e | 3 | Copyright 2014 Lennart Poettering |
74b2466e LP |
4 | ***/ |
5 | ||
b5efdb8a | 6 | #include "alloc-util.h" |
4ad7f276 | 7 | #include "dns-domain.h" |
07630cea LP |
8 | #include "macro.h" |
9 | #include "string-util.h" | |
74b2466e LP |
10 | |
11 | static void test_dns_label_unescape_one(const char *what, const char *expect, size_t buffer_sz, int ret) { | |
12 | char buffer[buffer_sz]; | |
13 | int r; | |
14 | ||
15 | r = dns_label_unescape(&what, buffer, buffer_sz); | |
16 | assert_se(r == ret); | |
17 | ||
18 | if (r < 0) | |
19 | return; | |
20 | ||
21 | assert_se(streq(buffer, expect)); | |
22 | } | |
23 | ||
24 | static void test_dns_label_unescape(void) { | |
25 | test_dns_label_unescape_one("hallo", "hallo", 6, 5); | |
37ade128 | 26 | test_dns_label_unescape_one("hallo", "hallo", 4, -ENOBUFS); |
74b2466e LP |
27 | test_dns_label_unescape_one("", "", 10, 0); |
28 | test_dns_label_unescape_one("hallo\\.foobar", "hallo.foobar", 20, 12); | |
29 | test_dns_label_unescape_one("hallo.foobar", "hallo", 10, 5); | |
30 | test_dns_label_unescape_one("hallo\n.foobar", "hallo", 20, -EINVAL); | |
31 | test_dns_label_unescape_one("hallo\\", "hallo", 20, -EINVAL); | |
32 | test_dns_label_unescape_one("hallo\\032 ", "hallo ", 20, 7); | |
33 | test_dns_label_unescape_one(".", "", 20, 0); | |
34 | test_dns_label_unescape_one("..", "", 20, -EINVAL); | |
35 | test_dns_label_unescape_one(".foobar", "", 20, -EINVAL); | |
36 | test_dns_label_unescape_one("foobar.", "foobar", 20, 6); | |
f35c467d | 37 | test_dns_label_unescape_one("foobar..", "foobar", 20, -EINVAL); |
74b2466e LP |
38 | } |
39 | ||
54adabf7 BG |
40 | static void test_dns_name_to_wire_format_one(const char *what, const char *expect, size_t buffer_sz, int ret) { |
41 | uint8_t buffer[buffer_sz]; | |
42 | int r; | |
43 | ||
3cd03457 | 44 | r = dns_name_to_wire_format(what, buffer, buffer_sz, false); |
54adabf7 BG |
45 | assert_se(r == ret); |
46 | ||
47 | if (r < 0) | |
48 | return; | |
49 | ||
50 | assert_se(!memcmp(buffer, expect, r)); | |
51 | } | |
52 | ||
53 | static void test_dns_name_to_wire_format(void) { | |
50dee79b LP |
54 | static const char out0[] = { 0 }; |
55 | static const char out1[] = { 3, 'f', 'o', 'o', 0 }; | |
56 | static const char out2[] = { 5, 'h', 'a', 'l', 'l', 'o', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 }; | |
57 | static const char out3[] = { 4, ' ', 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 }; | |
58 | static const char out4[] = { 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
59 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
60 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
61 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
62 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
63 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
64 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
65 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
66 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
67 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
68 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
69 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
70 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
71 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
72 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
73 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
74 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
75 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
76 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
77 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
78 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
79 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
80 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
81 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
82 | 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', | |
83 | 3, 'a', '1', '2', 0 }; | |
54adabf7 | 84 | |
c6cefd13 | 85 | test_dns_name_to_wire_format_one("", out0, sizeof(out0), sizeof(out0)); |
54adabf7 BG |
86 | |
87 | test_dns_name_to_wire_format_one("foo", out1, sizeof(out1), sizeof(out1)); | |
88 | test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) + 1, sizeof(out1)); | |
89 | test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) - 1, -ENOBUFS); | |
90 | ||
91 | test_dns_name_to_wire_format_one("hallo.foo.bar", out2, sizeof(out2), sizeof(out2)); | |
92 | test_dns_name_to_wire_format_one("hallo.foo..bar", NULL, 32, -EINVAL); | |
93 | ||
94 | test_dns_name_to_wire_format_one("\\032foo.bar", out3, sizeof(out3), sizeof(out3)); | |
50dee79b LP |
95 | |
96 | test_dns_name_to_wire_format_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a123", NULL, 500, -EINVAL); | |
97 | test_dns_name_to_wire_format_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12", out4, sizeof(out4), sizeof(out4)); | |
54adabf7 BG |
98 | } |
99 | ||
642900d3 TG |
100 | static void test_dns_label_unescape_suffix_one(const char *what, const char *expect1, const char *expect2, size_t buffer_sz, int ret1, int ret2) { |
101 | char buffer[buffer_sz]; | |
102 | const char *label; | |
103 | int r; | |
104 | ||
105 | label = what + strlen(what); | |
106 | ||
107 | r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz); | |
108 | assert_se(r == ret1); | |
109 | if (r >= 0) | |
110 | assert_se(streq(buffer, expect1)); | |
111 | ||
112 | r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz); | |
113 | assert_se(r == ret2); | |
114 | if (r >= 0) | |
115 | assert_se(streq(buffer, expect2)); | |
116 | } | |
117 | ||
118 | static void test_dns_label_unescape_suffix(void) { | |
119 | test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0); | |
37ade128 | 120 | test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOBUFS, -ENOBUFS); |
642900d3 TG |
121 | test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0); |
122 | test_dns_label_unescape_suffix_one("hallo\\.foobar", "hallo.foobar", "", 20, 12, 0); | |
123 | test_dns_label_unescape_suffix_one("hallo.foobar", "foobar", "hallo", 10, 6, 5); | |
124 | test_dns_label_unescape_suffix_one("hallo.foobar\n", "foobar", "foobar", 20, -EINVAL, -EINVAL); | |
125 | test_dns_label_unescape_suffix_one("hallo\\", "hallo", "hallo", 20, -EINVAL, -EINVAL); | |
126 | test_dns_label_unescape_suffix_one("hallo\\032 ", "hallo ", "", 20, 7, 0); | |
127 | test_dns_label_unescape_suffix_one(".", "", "", 20, 0, 0); | |
56512859 | 128 | test_dns_label_unescape_suffix_one("..", "", "", 20, 0, -EINVAL); |
642900d3 | 129 | test_dns_label_unescape_suffix_one(".foobar", "foobar", "", 20, 6, -EINVAL); |
56512859 | 130 | test_dns_label_unescape_suffix_one("foobar.", "foobar", "", 20, 6, 0); |
642900d3 TG |
131 | test_dns_label_unescape_suffix_one("foo\\\\bar", "foo\\bar", "", 20, 7, 0); |
132 | test_dns_label_unescape_suffix_one("foo.bar", "bar", "foo", 20, 3, 3); | |
133 | test_dns_label_unescape_suffix_one("foo..bar", "bar", "", 20, 3, -EINVAL); | |
134 | test_dns_label_unescape_suffix_one("foo...bar", "bar", "", 20, 3, -EINVAL); | |
135 | test_dns_label_unescape_suffix_one("foo\\.bar", "foo.bar", "", 20, 7, 0); | |
136 | test_dns_label_unescape_suffix_one("foo\\\\.bar", "bar", "foo\\", 20, 3, 4); | |
137 | test_dns_label_unescape_suffix_one("foo\\\\\\.bar", "foo\\.bar", "", 20, 8, 0); | |
138 | } | |
139 | ||
74b2466e LP |
140 | static void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) { |
141 | _cleanup_free_ char *t = NULL; | |
142 | int r; | |
143 | ||
422baca0 | 144 | r = dns_label_escape_new(what, l, &t); |
0c0cdb06 | 145 | assert_se(r == ret); |
74b2466e LP |
146 | |
147 | if (r < 0) | |
148 | return; | |
149 | ||
150 | assert_se(streq_ptr(expect, t)); | |
151 | } | |
152 | ||
153 | static void test_dns_label_escape(void) { | |
3b37fa73 | 154 | test_dns_label_escape_one("", 0, NULL, -EINVAL); |
74b2466e | 155 | test_dns_label_escape_one("hallo", 5, "hallo", 5); |
c7feab76 | 156 | test_dns_label_escape_one("hallo", 6, "hallo\\000", 9); |
74b2466e LP |
157 | test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31); |
158 | } | |
159 | ||
160 | static void test_dns_name_normalize_one(const char *what, const char *expect, int ret) { | |
161 | _cleanup_free_ char *t = NULL; | |
162 | int r; | |
163 | ||
164 | r = dns_name_normalize(what, &t); | |
165 | assert_se(r == ret); | |
166 | ||
167 | if (r < 0) | |
168 | return; | |
169 | ||
170 | assert_se(streq_ptr(expect, t)); | |
171 | } | |
172 | ||
173 | static void test_dns_name_normalize(void) { | |
3a519900 | 174 | test_dns_name_normalize_one("", ".", 0); |
74b2466e LP |
175 | test_dns_name_normalize_one("f", "f", 0); |
176 | test_dns_name_normalize_one("f.waldi", "f.waldi", 0); | |
177 | test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0); | |
c7feab76 | 178 | test_dns_name_normalize_one("\\000", "\\000", 0); |
74b2466e LP |
179 | test_dns_name_normalize_one("..", NULL, -EINVAL); |
180 | test_dns_name_normalize_one(".foobar", NULL, -EINVAL); | |
181 | test_dns_name_normalize_one("foobar.", "foobar", 0); | |
3a519900 | 182 | test_dns_name_normalize_one(".", ".", 0); |
74b2466e LP |
183 | } |
184 | ||
185 | static void test_dns_name_equal_one(const char *a, const char *b, int ret) { | |
186 | int r; | |
187 | ||
188 | r = dns_name_equal(a, b); | |
189 | assert_se(r == ret); | |
190 | ||
191 | r = dns_name_equal(b, a); | |
192 | assert_se(r == ret); | |
193 | } | |
194 | ||
195 | static void test_dns_name_equal(void) { | |
196 | test_dns_name_equal_one("", "", true); | |
197 | test_dns_name_equal_one("x", "x", true); | |
198 | test_dns_name_equal_one("x", "x.", true); | |
199 | test_dns_name_equal_one("abc.def", "abc.def", true); | |
200 | test_dns_name_equal_one("abc.def", "ABC.def", true); | |
201 | test_dns_name_equal_one("abc.def", "CBA.def", false); | |
202 | test_dns_name_equal_one("", "xxx", false); | |
203 | test_dns_name_equal_one("ab", "a", false); | |
c7feab76 | 204 | test_dns_name_equal_one("\\000", "\\000", true); |
74b2466e LP |
205 | test_dns_name_equal_one(".", "", true); |
206 | test_dns_name_equal_one(".", ".", true); | |
207 | test_dns_name_equal_one("..", "..", -EINVAL); | |
208 | } | |
209 | ||
ae72b22c TG |
210 | static void test_dns_name_between_one(const char *a, const char *b, const char *c, int ret) { |
211 | int r; | |
212 | ||
213 | r = dns_name_between(a, b, c); | |
214 | assert_se(r == ret); | |
215 | ||
216 | r = dns_name_between(c, b, a); | |
217 | if (ret >= 0) | |
59f2725c | 218 | assert_se(r == 0 || dns_name_equal(a, c) > 0); |
ae72b22c TG |
219 | else |
220 | assert_se(r == ret); | |
221 | } | |
222 | ||
223 | static void test_dns_name_between(void) { | |
224 | /* see https://tools.ietf.org/html/rfc4034#section-6.1 | |
225 | Note that we use "\033.z.example" in stead of "\001.z.example" as we | |
226 | consider the latter invalid */ | |
227 | test_dns_name_between_one("example", "a.example", "yljkjljk.a.example", true); | |
228 | test_dns_name_between_one("a.example", "yljkjljk.a.example", "Z.a.example", true); | |
229 | test_dns_name_between_one("yljkjljk.a.example", "Z.a.example", "zABC.a.EXAMPLE", true); | |
230 | test_dns_name_between_one("Z.a.example", "zABC.a.EXAMPLE", "z.example", true); | |
231 | test_dns_name_between_one("zABC.a.EXAMPLE", "z.example", "\\033.z.example", true); | |
232 | test_dns_name_between_one("z.example", "\\033.z.example", "*.z.example", true); | |
233 | test_dns_name_between_one("\\033.z.example", "*.z.example", "\\200.z.example", true); | |
234 | test_dns_name_between_one("*.z.example", "\\200.z.example", "example", true); | |
235 | test_dns_name_between_one("\\200.z.example", "example", "a.example", true); | |
236 | ||
59f2725c LP |
237 | test_dns_name_between_one("example", "a.example", "example", true); |
238 | test_dns_name_between_one("example", "example", "example", false); | |
ae72b22c TG |
239 | test_dns_name_between_one("example", "example", "yljkjljk.a.example", false); |
240 | test_dns_name_between_one("example", "yljkjljk.a.example", "yljkjljk.a.example", false); | |
0b491556 | 241 | test_dns_name_between_one("hkps.pool.sks-keyservers.net", "_pgpkey-https._tcp.hkps.pool.sks-keyservers.net", "ipv4.pool.sks-keyservers.net", true); |
ae72b22c TG |
242 | } |
243 | ||
74b2466e LP |
244 | static void test_dns_name_endswith_one(const char *a, const char *b, int ret) { |
245 | assert_se(dns_name_endswith(a, b) == ret); | |
246 | } | |
247 | ||
248 | static void test_dns_name_endswith(void) { | |
249 | test_dns_name_endswith_one("", "", true); | |
250 | test_dns_name_endswith_one("", "xxx", false); | |
251 | test_dns_name_endswith_one("xxx", "", true); | |
252 | test_dns_name_endswith_one("x", "x", true); | |
253 | test_dns_name_endswith_one("x", "y", false); | |
254 | test_dns_name_endswith_one("x.y", "y", true); | |
255 | test_dns_name_endswith_one("x.y", "Y", true); | |
256 | test_dns_name_endswith_one("x.y", "x", false); | |
257 | test_dns_name_endswith_one("x.y.z", "Z", true); | |
258 | test_dns_name_endswith_one("x.y.z", "y.Z", true); | |
259 | test_dns_name_endswith_one("x.y.z", "x.y.Z", true); | |
260 | test_dns_name_endswith_one("x.y.z", "waldo", false); | |
261 | test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false); | |
262 | test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true); | |
263 | test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL); | |
264 | } | |
265 | ||
eb241cdb LP |
266 | static void test_dns_name_startswith_one(const char *a, const char *b, int ret) { |
267 | assert_se(dns_name_startswith(a, b) == ret); | |
268 | } | |
269 | ||
270 | static void test_dns_name_startswith(void) { | |
271 | test_dns_name_startswith_one("", "", true); | |
272 | test_dns_name_startswith_one("", "xxx", false); | |
273 | test_dns_name_startswith_one("xxx", "", true); | |
274 | test_dns_name_startswith_one("x", "x", true); | |
275 | test_dns_name_startswith_one("x", "y", false); | |
276 | test_dns_name_startswith_one("x.y", "x.y", true); | |
277 | test_dns_name_startswith_one("x.y", "y.x", false); | |
278 | test_dns_name_startswith_one("x.y", "x", true); | |
279 | test_dns_name_startswith_one("x.y", "X", true); | |
280 | test_dns_name_startswith_one("x.y", "y", false); | |
281 | test_dns_name_startswith_one("x.y", "", true); | |
282 | test_dns_name_startswith_one("x.y", "X", true); | |
283 | } | |
284 | ||
dc477e73 LP |
285 | static void test_dns_name_is_root(void) { |
286 | assert_se(dns_name_is_root("")); | |
287 | assert_se(dns_name_is_root(".")); | |
288 | assert_se(!dns_name_is_root("xxx")); | |
289 | assert_se(!dns_name_is_root("xxx.")); | |
290 | assert_se(!dns_name_is_root("..")); | |
74b2466e LP |
291 | } |
292 | ||
dc477e73 LP |
293 | static void test_dns_name_is_single_label(void) { |
294 | assert_se(!dns_name_is_single_label("")); | |
295 | assert_se(!dns_name_is_single_label(".")); | |
296 | assert_se(!dns_name_is_single_label("..")); | |
297 | assert_se(dns_name_is_single_label("x")); | |
298 | assert_se(dns_name_is_single_label("x.")); | |
299 | assert_se(!dns_name_is_single_label("xx.yy")); | |
74b2466e LP |
300 | } |
301 | ||
b914e211 LP |
302 | static void test_dns_name_reverse_one(const char *address, const char *name) { |
303 | _cleanup_free_ char *p = NULL; | |
a7f7d1bd | 304 | union in_addr_union a, b = {}; |
b914e211 LP |
305 | int familya, familyb; |
306 | ||
307 | assert_se(in_addr_from_string_auto(address, &familya, &a) >= 0); | |
308 | assert_se(dns_name_reverse(familya, &a, &p) >= 0); | |
309 | assert_se(streq(p, name)); | |
310 | assert_se(dns_name_address(p, &familyb, &b) > 0); | |
311 | assert_se(familya == familyb); | |
312 | assert_se(in_addr_equal(familya, &a, &b)); | |
313 | } | |
314 | ||
315 | static void test_dns_name_reverse(void) { | |
316 | test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa"); | |
317 | test_dns_name_reverse_one("fe80::47", "7.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa"); | |
9436e8ca LP |
318 | test_dns_name_reverse_one("127.0.0.1", "1.0.0.127.in-addr.arpa"); |
319 | test_dns_name_reverse_one("::1", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa"); | |
b914e211 LP |
320 | } |
321 | ||
9ca45586 LP |
322 | static void test_dns_name_concat_one(const char *a, const char *b, int r, const char *result) { |
323 | _cleanup_free_ char *p = NULL; | |
324 | ||
325 | assert_se(dns_name_concat(a, b, &p) == r); | |
326 | assert_se(streq_ptr(p, result)); | |
327 | } | |
328 | ||
329 | static void test_dns_name_concat(void) { | |
3a519900 LP |
330 | test_dns_name_concat_one("", "", 0, "."); |
331 | test_dns_name_concat_one(".", "", 0, "."); | |
332 | test_dns_name_concat_one("", ".", 0, "."); | |
333 | test_dns_name_concat_one(".", ".", 0, "."); | |
9ca45586 LP |
334 | test_dns_name_concat_one("foo", "bar", 0, "foo.bar"); |
335 | test_dns_name_concat_one("foo.foo", "bar.bar", 0, "foo.foo.bar.bar"); | |
336 | test_dns_name_concat_one("foo", NULL, 0, "foo"); | |
3a519900 | 337 | test_dns_name_concat_one("foo", ".", 0, "foo"); |
9ca45586 | 338 | test_dns_name_concat_one("foo.", "bar.", 0, "foo.bar"); |
3a519900 LP |
339 | test_dns_name_concat_one(NULL, NULL, 0, "."); |
340 | test_dns_name_concat_one(NULL, ".", 0, "."); | |
341 | test_dns_name_concat_one(NULL, "foo", 0, "foo"); | |
9ca45586 LP |
342 | } |
343 | ||
344 | static void test_dns_name_is_valid_one(const char *s, int ret) { | |
345 | assert_se(dns_name_is_valid(s) == ret); | |
346 | } | |
347 | ||
348 | static void test_dns_name_is_valid(void) { | |
349 | test_dns_name_is_valid_one("foo", 1); | |
350 | test_dns_name_is_valid_one("foo.", 1); | |
f35c467d | 351 | test_dns_name_is_valid_one("foo..", 0); |
9ca45586 LP |
352 | test_dns_name_is_valid_one("Foo", 1); |
353 | test_dns_name_is_valid_one("foo.bar", 1); | |
354 | test_dns_name_is_valid_one("foo.bar.baz", 1); | |
355 | test_dns_name_is_valid_one("", 1); | |
356 | test_dns_name_is_valid_one("foo..bar", 0); | |
357 | test_dns_name_is_valid_one(".foo.bar", 0); | |
358 | test_dns_name_is_valid_one("foo.bar.", 1); | |
f35c467d | 359 | test_dns_name_is_valid_one("foo.bar..", 0); |
9ca45586 LP |
360 | test_dns_name_is_valid_one("\\zbar", 0); |
361 | test_dns_name_is_valid_one("ä", 1); | |
362 | test_dns_name_is_valid_one("\n", 0); | |
1dfbf000 | 363 | |
13e785f7 | 364 | /* 256 characters */ |
1dfbf000 LP |
365 | test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345", 0); |
366 | ||
13e785f7 | 367 | /* 255 characters */ |
1dfbf000 LP |
368 | test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a1234", 0); |
369 | ||
13e785f7 | 370 | /* 254 characters */ |
1dfbf000 LP |
371 | test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a123", 0); |
372 | ||
13e785f7 | 373 | /* 253 characters */ |
1dfbf000 LP |
374 | test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12", 1); |
375 | ||
376 | /* label of 64 chars length */ | |
377 | test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a123", 0); | |
378 | ||
379 | /* label of 63 chars length */ | |
380 | test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a12", 1); | |
9ca45586 LP |
381 | } |
382 | ||
0a49b6b6 LP |
383 | static void test_dns_service_name_is_valid(void) { |
384 | assert_se(dns_service_name_is_valid("Lennart's Compüter")); | |
385 | assert_se(dns_service_name_is_valid("piff.paff")); | |
386 | ||
387 | assert_se(!dns_service_name_is_valid(NULL)); | |
388 | assert_se(!dns_service_name_is_valid("")); | |
389 | assert_se(!dns_service_name_is_valid("foo\nbar")); | |
390 | assert_se(!dns_service_name_is_valid("foo\201bar")); | |
391 | assert_se(!dns_service_name_is_valid("this is an overly long string that is certainly longer than 63 characters")); | |
392 | } | |
393 | ||
7e8131e9 LP |
394 | static void test_dns_srv_type_is_valid(void) { |
395 | ||
396 | assert_se(dns_srv_type_is_valid("_http._tcp")); | |
397 | assert_se(dns_srv_type_is_valid("_foo-bar._tcp")); | |
398 | assert_se(dns_srv_type_is_valid("_w._udp")); | |
399 | assert_se(dns_srv_type_is_valid("_a800._tcp")); | |
400 | assert_se(dns_srv_type_is_valid("_a-800._tcp")); | |
401 | ||
402 | assert_se(!dns_srv_type_is_valid(NULL)); | |
403 | assert_se(!dns_srv_type_is_valid("")); | |
404 | assert_se(!dns_srv_type_is_valid("x")); | |
405 | assert_se(!dns_srv_type_is_valid("_foo")); | |
406 | assert_se(!dns_srv_type_is_valid("_tcp")); | |
407 | assert_se(!dns_srv_type_is_valid("_")); | |
408 | assert_se(!dns_srv_type_is_valid("_foo.")); | |
409 | assert_se(!dns_srv_type_is_valid("_föo._tcp")); | |
410 | assert_se(!dns_srv_type_is_valid("_f\no._tcp")); | |
411 | assert_se(!dns_srv_type_is_valid("_800._tcp")); | |
412 | assert_se(!dns_srv_type_is_valid("_-800._tcp")); | |
413 | assert_se(!dns_srv_type_is_valid("_-foo._tcp")); | |
414 | assert_se(!dns_srv_type_is_valid("_piep._foo._udp")); | |
0e8eedbb LP |
415 | } |
416 | ||
154ae087 DR |
417 | static void test_dnssd_srv_type_is_valid(void) { |
418 | ||
419 | assert_se(dnssd_srv_type_is_valid("_http._tcp")); | |
420 | assert_se(dnssd_srv_type_is_valid("_foo-bar._tcp")); | |
421 | assert_se(dnssd_srv_type_is_valid("_w._udp")); | |
422 | assert_se(dnssd_srv_type_is_valid("_a800._tcp")); | |
423 | assert_se(dnssd_srv_type_is_valid("_a-800._tcp")); | |
424 | ||
425 | assert_se(!dnssd_srv_type_is_valid(NULL)); | |
426 | assert_se(!dnssd_srv_type_is_valid("")); | |
427 | assert_se(!dnssd_srv_type_is_valid("x")); | |
428 | assert_se(!dnssd_srv_type_is_valid("_foo")); | |
429 | assert_se(!dnssd_srv_type_is_valid("_tcp")); | |
430 | assert_se(!dnssd_srv_type_is_valid("_")); | |
431 | assert_se(!dnssd_srv_type_is_valid("_foo.")); | |
432 | assert_se(!dnssd_srv_type_is_valid("_föo._tcp")); | |
433 | assert_se(!dnssd_srv_type_is_valid("_f\no._tcp")); | |
434 | assert_se(!dnssd_srv_type_is_valid("_800._tcp")); | |
435 | assert_se(!dnssd_srv_type_is_valid("_-800._tcp")); | |
436 | assert_se(!dnssd_srv_type_is_valid("_-foo._tcp")); | |
437 | assert_se(!dnssd_srv_type_is_valid("_piep._foo._udp")); | |
438 | assert_se(!dnssd_srv_type_is_valid("_foo._unknown")); | |
439 | } | |
440 | ||
0e8eedbb LP |
441 | static void test_dns_service_join_one(const char *a, const char *b, const char *c, int r, const char *d) { |
442 | _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *t = NULL; | |
443 | ||
444 | assert_se(dns_service_join(a, b, c, &t) == r); | |
445 | assert_se(streq_ptr(t, d)); | |
446 | ||
447 | if (r < 0) | |
448 | return; | |
449 | ||
450 | assert_se(dns_service_split(t, &x, &y, &z) >= 0); | |
451 | assert_se(streq_ptr(a, x)); | |
452 | assert_se(streq_ptr(b, y)); | |
3a519900 | 453 | assert_se(dns_name_equal(c, z) > 0); |
0e8eedbb LP |
454 | } |
455 | ||
456 | static void test_dns_service_join(void) { | |
457 | test_dns_service_join_one("", "", "", -EINVAL, NULL); | |
458 | test_dns_service_join_one("", "_http._tcp", "", -EINVAL, NULL); | |
459 | test_dns_service_join_one("", "_http._tcp", "foo", -EINVAL, NULL); | |
460 | test_dns_service_join_one("foo", "", "foo", -EINVAL, NULL); | |
461 | test_dns_service_join_one("foo", "foo", "foo", -EINVAL, NULL); | |
462 | ||
463 | test_dns_service_join_one("foo", "_http._tcp", "", 0, "foo._http._tcp"); | |
464 | test_dns_service_join_one(NULL, "_http._tcp", "", 0, "_http._tcp"); | |
465 | test_dns_service_join_one("foo", "_http._tcp", "foo", 0, "foo._http._tcp.foo"); | |
466 | test_dns_service_join_one(NULL, "_http._tcp", "foo", 0, "_http._tcp.foo"); | |
467 | test_dns_service_join_one("Lennart's PC", "_pc._tcp", "foo.bar.com", 0, "Lennart\\039s\\032PC._pc._tcp.foo.bar.com"); | |
468 | test_dns_service_join_one(NULL, "_pc._tcp", "foo.bar.com", 0, "_pc._tcp.foo.bar.com"); | |
469 | } | |
470 | ||
471 | static void test_dns_service_split_one(const char *joined, const char *a, const char *b, const char *c, int r) { | |
472 | _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *t = NULL; | |
473 | ||
474 | assert_se(dns_service_split(joined, &x, &y, &z) == r); | |
475 | assert_se(streq_ptr(x, a)); | |
476 | assert_se(streq_ptr(y, b)); | |
477 | assert_se(streq_ptr(z, c)); | |
478 | ||
479 | if (r < 0) | |
480 | return; | |
481 | ||
482 | if (y) { | |
483 | assert_se(dns_service_join(x, y, z, &t) == 0); | |
3a519900 | 484 | assert_se(dns_name_equal(joined, t) > 0); |
0e8eedbb | 485 | } else |
3a519900 | 486 | assert_se(!x && dns_name_equal(z, joined) > 0); |
0e8eedbb LP |
487 | } |
488 | ||
489 | static void test_dns_service_split(void) { | |
3a519900 | 490 | test_dns_service_split_one("", NULL, NULL, ".", 0); |
0e8eedbb LP |
491 | test_dns_service_split_one("foo", NULL, NULL, "foo", 0); |
492 | test_dns_service_split_one("foo.bar", NULL, NULL, "foo.bar", 0); | |
493 | test_dns_service_split_one("_foo.bar", NULL, NULL, "_foo.bar", 0); | |
3a519900 LP |
494 | test_dns_service_split_one("_foo._bar", NULL, "_foo._bar", ".", 0); |
495 | test_dns_service_split_one("_meh._foo._bar", "_meh", "_foo._bar", ".", 0); | |
0e8eedbb | 496 | test_dns_service_split_one("Wuff\\032Wuff._foo._bar.waldo.com", "Wuff Wuff", "_foo._bar", "waldo.com", 0); |
0a49b6b6 LP |
497 | } |
498 | ||
58db254a LP |
499 | static void test_dns_name_change_suffix_one(const char *name, const char *old_suffix, const char *new_suffix, int r, const char *result) { |
500 | _cleanup_free_ char *s = NULL; | |
501 | ||
502 | assert_se(dns_name_change_suffix(name, old_suffix, new_suffix, &s) == r); | |
503 | assert_se(streq_ptr(s, result)); | |
504 | } | |
505 | ||
506 | static void test_dns_name_change_suffix(void) { | |
507 | test_dns_name_change_suffix_one("foo.bar", "bar", "waldo", 1, "foo.waldo"); | |
508 | test_dns_name_change_suffix_one("foo.bar.waldi.quux", "foo.bar.waldi.quux", "piff.paff", 1, "piff.paff"); | |
509 | test_dns_name_change_suffix_one("foo.bar.waldi.quux", "bar.waldi.quux", "piff.paff", 1, "foo.piff.paff"); | |
510 | test_dns_name_change_suffix_one("foo.bar.waldi.quux", "waldi.quux", "piff.paff", 1, "foo.bar.piff.paff"); | |
511 | test_dns_name_change_suffix_one("foo.bar.waldi.quux", "quux", "piff.paff", 1, "foo.bar.waldi.piff.paff"); | |
512 | test_dns_name_change_suffix_one("foo.bar.waldi.quux", "", "piff.paff", 1, "foo.bar.waldi.quux.piff.paff"); | |
513 | test_dns_name_change_suffix_one("", "", "piff.paff", 1, "piff.paff"); | |
3a519900 | 514 | test_dns_name_change_suffix_one("", "", "", 1, "."); |
58db254a LP |
515 | test_dns_name_change_suffix_one("a", "b", "c", 0, NULL); |
516 | } | |
517 | ||
e7ff0e0b LP |
518 | static void test_dns_name_suffix_one(const char *name, unsigned n_labels, const char *result, int ret) { |
519 | const char *p = NULL; | |
520 | ||
521 | assert_se(ret == dns_name_suffix(name, n_labels, &p)); | |
522 | assert_se(streq_ptr(p, result)); | |
523 | } | |
524 | ||
525 | static void test_dns_name_suffix(void) { | |
526 | test_dns_name_suffix_one("foo.bar", 2, "foo.bar", 0); | |
527 | test_dns_name_suffix_one("foo.bar", 1, "bar", 1); | |
528 | test_dns_name_suffix_one("foo.bar", 0, "", 2); | |
529 | test_dns_name_suffix_one("foo.bar", 3, NULL, -EINVAL); | |
530 | test_dns_name_suffix_one("foo.bar", 4, NULL, -EINVAL); | |
531 | ||
532 | test_dns_name_suffix_one("bar", 1, "bar", 0); | |
533 | test_dns_name_suffix_one("bar", 0, "", 1); | |
534 | test_dns_name_suffix_one("bar", 2, NULL, -EINVAL); | |
535 | test_dns_name_suffix_one("bar", 3, NULL, -EINVAL); | |
536 | ||
537 | test_dns_name_suffix_one("", 0, "", 0); | |
538 | test_dns_name_suffix_one("", 1, NULL, -EINVAL); | |
539 | test_dns_name_suffix_one("", 2, NULL, -EINVAL); | |
540 | } | |
541 | ||
542 | static void test_dns_name_count_labels_one(const char *name, int n) { | |
543 | assert_se(dns_name_count_labels(name) == n); | |
544 | } | |
545 | ||
546 | static void test_dns_name_count_labels(void) { | |
547 | test_dns_name_count_labels_one("foo.bar.quux.", 3); | |
548 | test_dns_name_count_labels_one("foo.bar.quux", 3); | |
549 | test_dns_name_count_labels_one("foo.bar.", 2); | |
550 | test_dns_name_count_labels_one("foo.bar", 2); | |
551 | test_dns_name_count_labels_one("foo.", 1); | |
552 | test_dns_name_count_labels_one("foo", 1); | |
553 | test_dns_name_count_labels_one("", 0); | |
554 | test_dns_name_count_labels_one(".", 0); | |
555 | test_dns_name_count_labels_one("..", -EINVAL); | |
556 | } | |
557 | ||
db5b0e92 LP |
558 | static void test_dns_name_equal_skip_one(const char *a, unsigned n_labels, const char *b, int ret) { |
559 | assert_se(dns_name_equal_skip(a, n_labels, b) == ret); | |
560 | } | |
561 | ||
562 | static void test_dns_name_equal_skip(void) { | |
563 | test_dns_name_equal_skip_one("foo", 0, "bar", 0); | |
564 | test_dns_name_equal_skip_one("foo", 0, "foo", 1); | |
565 | test_dns_name_equal_skip_one("foo", 1, "foo", 0); | |
566 | test_dns_name_equal_skip_one("foo", 2, "foo", 0); | |
567 | ||
568 | test_dns_name_equal_skip_one("foo.bar", 0, "foo.bar", 1); | |
569 | test_dns_name_equal_skip_one("foo.bar", 1, "foo.bar", 0); | |
570 | test_dns_name_equal_skip_one("foo.bar", 2, "foo.bar", 0); | |
571 | test_dns_name_equal_skip_one("foo.bar", 3, "foo.bar", 0); | |
572 | ||
573 | test_dns_name_equal_skip_one("foo.bar", 0, "bar", 0); | |
574 | test_dns_name_equal_skip_one("foo.bar", 1, "bar", 1); | |
575 | test_dns_name_equal_skip_one("foo.bar", 2, "bar", 0); | |
576 | test_dns_name_equal_skip_one("foo.bar", 3, "bar", 0); | |
577 | ||
578 | test_dns_name_equal_skip_one("foo.bar", 0, "", 0); | |
579 | test_dns_name_equal_skip_one("foo.bar", 1, "", 0); | |
580 | test_dns_name_equal_skip_one("foo.bar", 2, "", 1); | |
581 | test_dns_name_equal_skip_one("foo.bar", 3, "", 0); | |
582 | ||
583 | test_dns_name_equal_skip_one("", 0, "", 1); | |
584 | test_dns_name_equal_skip_one("", 1, "", 0); | |
585 | test_dns_name_equal_skip_one("", 1, "foo", 0); | |
586 | test_dns_name_equal_skip_one("", 2, "foo", 0); | |
587 | } | |
588 | ||
56512859 LP |
589 | static void test_dns_name_compare_func(void) { |
590 | assert_se(dns_name_compare_func("", "") == 0); | |
591 | assert_se(dns_name_compare_func("", ".") == 0); | |
592 | assert_se(dns_name_compare_func(".", "") == 0); | |
593 | assert_se(dns_name_compare_func("foo", "foo.") == 0); | |
594 | assert_se(dns_name_compare_func("foo.", "foo") == 0); | |
595 | assert_se(dns_name_compare_func("foo", "foo") == 0); | |
596 | assert_se(dns_name_compare_func("foo.", "foo.") == 0); | |
597 | assert_se(dns_name_compare_func("heise.de", "HEISE.DE.") == 0); | |
598 | ||
599 | assert_se(dns_name_compare_func("de.", "heise.de") != 0); | |
600 | } | |
601 | ||
b9282bc1 LP |
602 | static void test_dns_name_common_suffix_one(const char *a, const char *b, const char *result) { |
603 | const char *c; | |
604 | ||
605 | assert_se(dns_name_common_suffix(a, b, &c) >= 0); | |
606 | assert_se(streq(c, result)); | |
607 | } | |
608 | ||
609 | static void test_dns_name_common_suffix(void) { | |
610 | test_dns_name_common_suffix_one("", "", ""); | |
611 | test_dns_name_common_suffix_one("foo", "", ""); | |
612 | test_dns_name_common_suffix_one("", "foo", ""); | |
613 | test_dns_name_common_suffix_one("foo", "bar", ""); | |
614 | test_dns_name_common_suffix_one("bar", "foo", ""); | |
615 | test_dns_name_common_suffix_one("foo", "foo", "foo"); | |
616 | test_dns_name_common_suffix_one("quux.foo", "foo", "foo"); | |
617 | test_dns_name_common_suffix_one("foo", "quux.foo", "foo"); | |
618 | test_dns_name_common_suffix_one("this.is.a.short.sentence", "this.is.another.short.sentence", "short.sentence"); | |
619 | test_dns_name_common_suffix_one("FOO.BAR", "tEST.bAR", "BAR"); | |
620 | } | |
621 | ||
ad1f3fe6 | 622 | static void test_dns_name_apply_idna_one(const char *s, int expected, const char *result) { |
0cf40f55 | 623 | _cleanup_free_ char *buf = NULL; |
ad1f3fe6 ZJS |
624 | int r; |
625 | ||
626 | r = dns_name_apply_idna(s, &buf); | |
627 | log_debug("dns_name_apply_idna: \"%s\" → %d/\"%s\" (expected %d/\"%s\")", | |
628 | s, r, strnull(buf), expected, strnull(result)); | |
629 | ||
0fe36dd9 ZJS |
630 | /* Different libidn2 versions are more and less accepting |
631 | * of underscore-prefixed names. So let's list the lowest | |
632 | * expected return value. */ | |
633 | assert_se(r >= expected); | |
ad1f3fe6 ZJS |
634 | if (expected == 1) |
635 | assert_se(dns_name_equal(buf, result) == 1); | |
0cf40f55 LP |
636 | } |
637 | ||
638 | static void test_dns_name_apply_idna(void) { | |
349cc4a5 | 639 | #if HAVE_LIBIDN2 || HAVE_LIBIDN |
ad1f3fe6 ZJS |
640 | const int ret = 1; |
641 | #else | |
642 | const int ret = 0; | |
643 | #endif | |
644 | ||
645 | /* IDNA2008 forbids names with hyphens in third and fourth positions | |
646 | * (https://tools.ietf.org/html/rfc5891#section-4.2.3.1). | |
647 | * IDNA2003 does not have this restriction | |
648 | * (https://tools.ietf.org/html/rfc3490#section-5). | |
649 | * This means that when using libidn we will transform and test more | |
650 | * labels. If registrars follow IDNA2008 we'll just be performing a | |
651 | * useless lookup. | |
652 | */ | |
349cc4a5 | 653 | #if HAVE_LIBIDN |
ad1f3fe6 ZJS |
654 | const int ret2 = 1; |
655 | #else | |
656 | const int ret2 = 0; | |
657 | #endif | |
658 | ||
659 | test_dns_name_apply_idna_one("", ret, ""); | |
660 | test_dns_name_apply_idna_one("foo", ret, "foo"); | |
661 | test_dns_name_apply_idna_one("foo.", ret, "foo"); | |
662 | test_dns_name_apply_idna_one("foo.bar", ret, "foo.bar"); | |
663 | test_dns_name_apply_idna_one("foo.bar.", ret, "foo.bar"); | |
664 | test_dns_name_apply_idna_one("föö", ret, "xn--f-1gaa"); | |
665 | test_dns_name_apply_idna_one("föö.", ret, "xn--f-1gaa"); | |
666 | test_dns_name_apply_idna_one("föö.bär", ret, "xn--f-1gaa.xn--br-via"); | |
667 | test_dns_name_apply_idna_one("föö.bär.", ret, "xn--f-1gaa.xn--br-via"); | |
668 | test_dns_name_apply_idna_one("xn--f-1gaa.xn--br-via", ret, "xn--f-1gaa.xn--br-via"); | |
669 | ||
0926f348 ZJS |
670 | test_dns_name_apply_idna_one("_443._tcp.fedoraproject.org", ret2, |
671 | "_443._tcp.fedoraproject.org"); | |
672 | test_dns_name_apply_idna_one("_443", ret2, "_443"); | |
673 | test_dns_name_apply_idna_one("gateway", ret, "gateway"); | |
674 | test_dns_name_apply_idna_one("_gateway", ret2, "_gateway"); | |
675 | ||
ad1f3fe6 ZJS |
676 | test_dns_name_apply_idna_one("r3---sn-ab5l6ne7.googlevideo.com", ret2, |
677 | ret2 ? "r3---sn-ab5l6ne7.googlevideo.com" : ""); | |
0cf40f55 LP |
678 | } |
679 | ||
08a4849e LP |
680 | static void test_dns_name_is_valid_or_address(void) { |
681 | assert_se(dns_name_is_valid_or_address(NULL) == 0); | |
682 | assert_se(dns_name_is_valid_or_address("") == 0); | |
683 | assert_se(dns_name_is_valid_or_address("foobar") > 0); | |
684 | assert_se(dns_name_is_valid_or_address("foobar.com") > 0); | |
685 | assert_se(dns_name_is_valid_or_address("foobar..com") == 0); | |
686 | assert_se(dns_name_is_valid_or_address("foobar.com.") > 0); | |
687 | assert_se(dns_name_is_valid_or_address("127.0.0.1") > 0); | |
688 | assert_se(dns_name_is_valid_or_address("::") > 0); | |
689 | assert_se(dns_name_is_valid_or_address("::1") > 0); | |
690 | } | |
691 | ||
74b2466e | 692 | int main(int argc, char *argv[]) { |
ad1f3fe6 ZJS |
693 | log_set_max_level(LOG_DEBUG); |
694 | log_parse_environment(); | |
695 | log_open(); | |
74b2466e LP |
696 | |
697 | test_dns_label_unescape(); | |
642900d3 | 698 | test_dns_label_unescape_suffix(); |
74b2466e LP |
699 | test_dns_label_escape(); |
700 | test_dns_name_normalize(); | |
701 | test_dns_name_equal(); | |
702 | test_dns_name_endswith(); | |
eb241cdb | 703 | test_dns_name_startswith(); |
ae72b22c | 704 | test_dns_name_between(); |
dc477e73 LP |
705 | test_dns_name_is_root(); |
706 | test_dns_name_is_single_label(); | |
b914e211 | 707 | test_dns_name_reverse(); |
9ca45586 LP |
708 | test_dns_name_concat(); |
709 | test_dns_name_is_valid(); | |
54adabf7 | 710 | test_dns_name_to_wire_format(); |
0a49b6b6 | 711 | test_dns_service_name_is_valid(); |
7e8131e9 | 712 | test_dns_srv_type_is_valid(); |
154ae087 | 713 | test_dnssd_srv_type_is_valid(); |
0e8eedbb LP |
714 | test_dns_service_join(); |
715 | test_dns_service_split(); | |
58db254a | 716 | test_dns_name_change_suffix(); |
e7ff0e0b LP |
717 | test_dns_name_suffix(); |
718 | test_dns_name_count_labels(); | |
db5b0e92 | 719 | test_dns_name_equal_skip(); |
56512859 | 720 | test_dns_name_compare_func(); |
b9282bc1 | 721 | test_dns_name_common_suffix(); |
0cf40f55 | 722 | test_dns_name_apply_idna(); |
08a4849e | 723 | test_dns_name_is_valid_or_address(); |
74b2466e LP |
724 | |
725 | return 0; | |
726 | } |