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