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