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