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