]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-dns-domain.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Lennart Poettering
6 #include "alloc-util.h"
7 #include "dns-domain.h"
9 #include "string-util.h"
11 static void test_dns_label_unescape_one(const char *what
, const char *expect
, size_t buffer_sz
, int ret
) {
12 char buffer
[buffer_sz
];
15 r
= dns_label_unescape(&what
, buffer
, buffer_sz
);
21 assert_se(streq(buffer
, expect
));
24 static void test_dns_label_unescape(void) {
25 test_dns_label_unescape_one("hallo", "hallo", 6, 5);
26 test_dns_label_unescape_one("hallo", "hallo", 4, -ENOBUFS
);
27 test_dns_label_unescape_one("", "", 10, 0);
28 test_dns_label_unescape_one("hallo\\.foobar", "hallo.foobar", 20, 12);
29 test_dns_label_unescape_one("hallo.foobar", "hallo", 10, 5);
30 test_dns_label_unescape_one("hallo\n.foobar", "hallo", 20, -EINVAL
);
31 test_dns_label_unescape_one("hallo\\", "hallo", 20, -EINVAL
);
32 test_dns_label_unescape_one("hallo\\032 ", "hallo ", 20, 7);
33 test_dns_label_unescape_one(".", "", 20, 0);
34 test_dns_label_unescape_one("..", "", 20, -EINVAL
);
35 test_dns_label_unescape_one(".foobar", "", 20, -EINVAL
);
36 test_dns_label_unescape_one("foobar.", "foobar", 20, 6);
37 test_dns_label_unescape_one("foobar..", "foobar", 20, -EINVAL
);
40 static void test_dns_name_to_wire_format_one(const char *what
, const char *expect
, size_t buffer_sz
, int ret
) {
41 uint8_t buffer
[buffer_sz
];
44 r
= dns_name_to_wire_format(what
, buffer
, buffer_sz
, false);
50 assert_se(!memcmp(buffer
, expect
, r
));
53 static void test_dns_name_to_wire_format(void) {
54 static const char out0
[] = { 0 };
55 static const char out1
[] = { 3, 'f', 'o', 'o', 0 };
56 static const char out2
[] = { 5, 'h', 'a', 'l', 'l', 'o', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 };
57 static const char out3
[] = { 4, ' ', 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 };
58 static const char out4
[] = { 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
59 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
60 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
61 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
62 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
63 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
64 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
65 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
66 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
67 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
68 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
69 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
70 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
71 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
72 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
73 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
74 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
75 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
76 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
77 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
78 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
79 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
80 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
81 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
82 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
83 3, 'a', '1', '2', 0 };
85 test_dns_name_to_wire_format_one("", out0
, sizeof(out0
), sizeof(out0
));
87 test_dns_name_to_wire_format_one("foo", out1
, sizeof(out1
), sizeof(out1
));
88 test_dns_name_to_wire_format_one("foo", out1
, sizeof(out1
) + 1, sizeof(out1
));
89 test_dns_name_to_wire_format_one("foo", out1
, sizeof(out1
) - 1, -ENOBUFS
);
91 test_dns_name_to_wire_format_one("hallo.foo.bar", out2
, sizeof(out2
), sizeof(out2
));
92 test_dns_name_to_wire_format_one("hallo.foo..bar", NULL
, 32, -EINVAL
);
94 test_dns_name_to_wire_format_one("\\032foo.bar", out3
, sizeof(out3
), sizeof(out3
));
96 test_dns_name_to_wire_format_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a123", NULL
, 500, -EINVAL
);
97 test_dns_name_to_wire_format_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12", out4
, sizeof(out4
), sizeof(out4
));
100 static void test_dns_label_unescape_suffix_one(const char *what
, const char *expect1
, const char *expect2
, size_t buffer_sz
, int ret1
, int ret2
) {
101 char buffer
[buffer_sz
];
105 label
= what
+ strlen(what
);
107 r
= dns_label_unescape_suffix(what
, &label
, buffer
, buffer_sz
);
108 assert_se(r
== ret1
);
110 assert_se(streq(buffer
, expect1
));
112 r
= dns_label_unescape_suffix(what
, &label
, buffer
, buffer_sz
);
113 assert_se(r
== ret2
);
115 assert_se(streq(buffer
, expect2
));
118 static void test_dns_label_unescape_suffix(void) {
119 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0);
120 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOBUFS
, -ENOBUFS
);
121 test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0);
122 test_dns_label_unescape_suffix_one("hallo\\.foobar", "hallo.foobar", "", 20, 12, 0);
123 test_dns_label_unescape_suffix_one("hallo.foobar", "foobar", "hallo", 10, 6, 5);
124 test_dns_label_unescape_suffix_one("hallo.foobar\n", "foobar", "foobar", 20, -EINVAL
, -EINVAL
);
125 test_dns_label_unescape_suffix_one("hallo\\", "hallo", "hallo", 20, -EINVAL
, -EINVAL
);
126 test_dns_label_unescape_suffix_one("hallo\\032 ", "hallo ", "", 20, 7, 0);
127 test_dns_label_unescape_suffix_one(".", "", "", 20, 0, 0);
128 test_dns_label_unescape_suffix_one("..", "", "", 20, 0, -EINVAL
);
129 test_dns_label_unescape_suffix_one(".foobar", "foobar", "", 20, 6, -EINVAL
);
130 test_dns_label_unescape_suffix_one("foobar.", "foobar", "", 20, 6, 0);
131 test_dns_label_unescape_suffix_one("foo\\\\bar", "foo\\bar", "", 20, 7, 0);
132 test_dns_label_unescape_suffix_one("foo.bar", "bar", "foo", 20, 3, 3);
133 test_dns_label_unescape_suffix_one("foo..bar", "bar", "", 20, 3, -EINVAL
);
134 test_dns_label_unescape_suffix_one("foo...bar", "bar", "", 20, 3, -EINVAL
);
135 test_dns_label_unescape_suffix_one("foo\\.bar", "foo.bar", "", 20, 7, 0);
136 test_dns_label_unescape_suffix_one("foo\\\\.bar", "bar", "foo\\", 20, 3, 4);
137 test_dns_label_unescape_suffix_one("foo\\\\\\.bar", "foo\\.bar", "", 20, 8, 0);
140 static void test_dns_label_escape_one(const char *what
, size_t l
, const char *expect
, int ret
) {
141 _cleanup_free_
char *t
= NULL
;
144 r
= dns_label_escape_new(what
, l
, &t
);
150 assert_se(streq_ptr(expect
, t
));
153 static void test_dns_label_escape(void) {
154 test_dns_label_escape_one("", 0, NULL
, -EINVAL
);
155 test_dns_label_escape_one("hallo", 5, "hallo", 5);
156 test_dns_label_escape_one("hallo", 6, "hallo\\000", 9);
157 test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31);
160 static void test_dns_name_normalize_one(const char *what
, const char *expect
, int ret
) {
161 _cleanup_free_
char *t
= NULL
;
164 r
= dns_name_normalize(what
, &t
);
170 assert_se(streq_ptr(expect
, t
));
173 static void test_dns_name_normalize(void) {
174 test_dns_name_normalize_one("", ".", 0);
175 test_dns_name_normalize_one("f", "f", 0);
176 test_dns_name_normalize_one("f.waldi", "f.waldi", 0);
177 test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0);
178 test_dns_name_normalize_one("\\000", "\\000", 0);
179 test_dns_name_normalize_one("..", NULL
, -EINVAL
);
180 test_dns_name_normalize_one(".foobar", NULL
, -EINVAL
);
181 test_dns_name_normalize_one("foobar.", "foobar", 0);
182 test_dns_name_normalize_one(".", ".", 0);
185 static void test_dns_name_equal_one(const char *a
, const char *b
, int ret
) {
188 r
= dns_name_equal(a
, b
);
191 r
= dns_name_equal(b
, a
);
195 static void test_dns_name_equal(void) {
196 test_dns_name_equal_one("", "", true);
197 test_dns_name_equal_one("x", "x", true);
198 test_dns_name_equal_one("x", "x.", true);
199 test_dns_name_equal_one("abc.def", "abc.def", true);
200 test_dns_name_equal_one("abc.def", "ABC.def", true);
201 test_dns_name_equal_one("abc.def", "CBA.def", false);
202 test_dns_name_equal_one("", "xxx", false);
203 test_dns_name_equal_one("ab", "a", false);
204 test_dns_name_equal_one("\\000", "\\000", true);
205 test_dns_name_equal_one(".", "", true);
206 test_dns_name_equal_one(".", ".", true);
207 test_dns_name_equal_one("..", "..", -EINVAL
);
210 static void test_dns_name_between_one(const char *a
, const char *b
, const char *c
, int ret
) {
213 r
= dns_name_between(a
, b
, c
);
216 r
= dns_name_between(c
, b
, a
);
218 assert_se(r
== 0 || dns_name_equal(a
, c
) > 0);
223 static void test_dns_name_between(void) {
224 /* see https://tools.ietf.org/html/rfc4034#section-6.1
225 Note that we use "\033.z.example" in stead of "\001.z.example" as we
226 consider the latter invalid */
227 test_dns_name_between_one("example", "a.example", "yljkjljk.a.example", true);
228 test_dns_name_between_one("a.example", "yljkjljk.a.example", "Z.a.example", true);
229 test_dns_name_between_one("yljkjljk.a.example", "Z.a.example", "zABC.a.EXAMPLE", true);
230 test_dns_name_between_one("Z.a.example", "zABC.a.EXAMPLE", "z.example", true);
231 test_dns_name_between_one("zABC.a.EXAMPLE", "z.example", "\\033.z.example", true);
232 test_dns_name_between_one("z.example", "\\033.z.example", "*.z.example", true);
233 test_dns_name_between_one("\\033.z.example", "*.z.example", "\\200.z.example", true);
234 test_dns_name_between_one("*.z.example", "\\200.z.example", "example", true);
235 test_dns_name_between_one("\\200.z.example", "example", "a.example", true);
237 test_dns_name_between_one("example", "a.example", "example", true);
238 test_dns_name_between_one("example", "example", "example", false);
239 test_dns_name_between_one("example", "example", "yljkjljk.a.example", false);
240 test_dns_name_between_one("example", "yljkjljk.a.example", "yljkjljk.a.example", false);
241 test_dns_name_between_one("hkps.pool.sks-keyservers.net", "_pgpkey-https._tcp.hkps.pool.sks-keyservers.net", "ipv4.pool.sks-keyservers.net", true);
244 static void test_dns_name_endswith_one(const char *a
, const char *b
, int ret
) {
245 assert_se(dns_name_endswith(a
, b
) == ret
);
248 static void test_dns_name_endswith(void) {
249 test_dns_name_endswith_one("", "", true);
250 test_dns_name_endswith_one("", "xxx", false);
251 test_dns_name_endswith_one("xxx", "", true);
252 test_dns_name_endswith_one("x", "x", true);
253 test_dns_name_endswith_one("x", "y", false);
254 test_dns_name_endswith_one("x.y", "y", true);
255 test_dns_name_endswith_one("x.y", "Y", true);
256 test_dns_name_endswith_one("x.y", "x", false);
257 test_dns_name_endswith_one("x.y.z", "Z", true);
258 test_dns_name_endswith_one("x.y.z", "y.Z", true);
259 test_dns_name_endswith_one("x.y.z", "x.y.Z", true);
260 test_dns_name_endswith_one("x.y.z", "waldo", false);
261 test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false);
262 test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true);
263 test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL
);
266 static void test_dns_name_startswith_one(const char *a
, const char *b
, int ret
) {
267 assert_se(dns_name_startswith(a
, b
) == ret
);
270 static void test_dns_name_startswith(void) {
271 test_dns_name_startswith_one("", "", true);
272 test_dns_name_startswith_one("", "xxx", false);
273 test_dns_name_startswith_one("xxx", "", true);
274 test_dns_name_startswith_one("x", "x", true);
275 test_dns_name_startswith_one("x", "y", false);
276 test_dns_name_startswith_one("x.y", "x.y", true);
277 test_dns_name_startswith_one("x.y", "y.x", false);
278 test_dns_name_startswith_one("x.y", "x", true);
279 test_dns_name_startswith_one("x.y", "X", true);
280 test_dns_name_startswith_one("x.y", "y", false);
281 test_dns_name_startswith_one("x.y", "", true);
282 test_dns_name_startswith_one("x.y", "X", true);
285 static void test_dns_name_is_root(void) {
286 assert_se(dns_name_is_root(""));
287 assert_se(dns_name_is_root("."));
288 assert_se(!dns_name_is_root("xxx"));
289 assert_se(!dns_name_is_root("xxx."));
290 assert_se(!dns_name_is_root(".."));
293 static void test_dns_name_is_single_label(void) {
294 assert_se(!dns_name_is_single_label(""));
295 assert_se(!dns_name_is_single_label("."));
296 assert_se(!dns_name_is_single_label(".."));
297 assert_se(dns_name_is_single_label("x"));
298 assert_se(dns_name_is_single_label("x."));
299 assert_se(!dns_name_is_single_label("xx.yy"));
302 static void test_dns_name_reverse_one(const char *address
, const char *name
) {
303 _cleanup_free_
char *p
= NULL
;
304 union in_addr_union a
, b
= {};
305 int familya
, familyb
;
307 assert_se(in_addr_from_string_auto(address
, &familya
, &a
) >= 0);
308 assert_se(dns_name_reverse(familya
, &a
, &p
) >= 0);
309 assert_se(streq(p
, name
));
310 assert_se(dns_name_address(p
, &familyb
, &b
) > 0);
311 assert_se(familya
== familyb
);
312 assert_se(in_addr_equal(familya
, &a
, &b
));
315 static void test_dns_name_reverse(void) {
316 test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa");
317 test_dns_name_reverse_one("fe80::47", "7.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa");
318 test_dns_name_reverse_one("127.0.0.1", "1.0.0.127.in-addr.arpa");
319 test_dns_name_reverse_one("::1", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa");
322 static void test_dns_name_concat_one(const char *a
, const char *b
, int r
, const char *result
) {
323 _cleanup_free_
char *p
= NULL
;
325 assert_se(dns_name_concat(a
, b
, &p
) == r
);
326 assert_se(streq_ptr(p
, result
));
329 static void test_dns_name_concat(void) {
330 test_dns_name_concat_one("", "", 0, ".");
331 test_dns_name_concat_one(".", "", 0, ".");
332 test_dns_name_concat_one("", ".", 0, ".");
333 test_dns_name_concat_one(".", ".", 0, ".");
334 test_dns_name_concat_one("foo", "bar", 0, "foo.bar");
335 test_dns_name_concat_one("foo.foo", "bar.bar", 0, "foo.foo.bar.bar");
336 test_dns_name_concat_one("foo", NULL
, 0, "foo");
337 test_dns_name_concat_one("foo", ".", 0, "foo");
338 test_dns_name_concat_one("foo.", "bar.", 0, "foo.bar");
339 test_dns_name_concat_one(NULL
, NULL
, 0, ".");
340 test_dns_name_concat_one(NULL
, ".", 0, ".");
341 test_dns_name_concat_one(NULL
, "foo", 0, "foo");
344 static void test_dns_name_is_valid_one(const char *s
, int ret
) {
345 assert_se(dns_name_is_valid(s
) == ret
);
348 static void test_dns_name_is_valid(void) {
349 test_dns_name_is_valid_one("foo", 1);
350 test_dns_name_is_valid_one("foo.", 1);
351 test_dns_name_is_valid_one("foo..", 0);
352 test_dns_name_is_valid_one("Foo", 1);
353 test_dns_name_is_valid_one("foo.bar", 1);
354 test_dns_name_is_valid_one("foo.bar.baz", 1);
355 test_dns_name_is_valid_one("", 1);
356 test_dns_name_is_valid_one("foo..bar", 0);
357 test_dns_name_is_valid_one(".foo.bar", 0);
358 test_dns_name_is_valid_one("foo.bar.", 1);
359 test_dns_name_is_valid_one("foo.bar..", 0);
360 test_dns_name_is_valid_one("\\zbar", 0);
361 test_dns_name_is_valid_one("ä", 1);
362 test_dns_name_is_valid_one("\n", 0);
365 test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345", 0);
368 test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a1234", 0);
371 test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a123", 0);
374 test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12", 1);
376 /* label of 64 chars length */
377 test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a123", 0);
379 /* label of 63 chars length */
380 test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a12", 1);
383 static void test_dns_service_name_is_valid(void) {
384 assert_se(dns_service_name_is_valid("Lennart's Compüter"));
385 assert_se(dns_service_name_is_valid("piff.paff"));
387 assert_se(!dns_service_name_is_valid(NULL
));
388 assert_se(!dns_service_name_is_valid(""));
389 assert_se(!dns_service_name_is_valid("foo\nbar"));
390 assert_se(!dns_service_name_is_valid("foo\201bar"));
391 assert_se(!dns_service_name_is_valid("this is an overly long string that is certainly longer than 63 characters"));
394 static void test_dns_srv_type_is_valid(void) {
396 assert_se(dns_srv_type_is_valid("_http._tcp"));
397 assert_se(dns_srv_type_is_valid("_foo-bar._tcp"));
398 assert_se(dns_srv_type_is_valid("_w._udp"));
399 assert_se(dns_srv_type_is_valid("_a800._tcp"));
400 assert_se(dns_srv_type_is_valid("_a-800._tcp"));
402 assert_se(!dns_srv_type_is_valid(NULL
));
403 assert_se(!dns_srv_type_is_valid(""));
404 assert_se(!dns_srv_type_is_valid("x"));
405 assert_se(!dns_srv_type_is_valid("_foo"));
406 assert_se(!dns_srv_type_is_valid("_tcp"));
407 assert_se(!dns_srv_type_is_valid("_"));
408 assert_se(!dns_srv_type_is_valid("_foo."));
409 assert_se(!dns_srv_type_is_valid("_föo._tcp"));
410 assert_se(!dns_srv_type_is_valid("_f\no._tcp"));
411 assert_se(!dns_srv_type_is_valid("_800._tcp"));
412 assert_se(!dns_srv_type_is_valid("_-800._tcp"));
413 assert_se(!dns_srv_type_is_valid("_-foo._tcp"));
414 assert_se(!dns_srv_type_is_valid("_piep._foo._udp"));
417 static void test_dnssd_srv_type_is_valid(void) {
419 assert_se(dnssd_srv_type_is_valid("_http._tcp"));
420 assert_se(dnssd_srv_type_is_valid("_foo-bar._tcp"));
421 assert_se(dnssd_srv_type_is_valid("_w._udp"));
422 assert_se(dnssd_srv_type_is_valid("_a800._tcp"));
423 assert_se(dnssd_srv_type_is_valid("_a-800._tcp"));
425 assert_se(!dnssd_srv_type_is_valid(NULL
));
426 assert_se(!dnssd_srv_type_is_valid(""));
427 assert_se(!dnssd_srv_type_is_valid("x"));
428 assert_se(!dnssd_srv_type_is_valid("_foo"));
429 assert_se(!dnssd_srv_type_is_valid("_tcp"));
430 assert_se(!dnssd_srv_type_is_valid("_"));
431 assert_se(!dnssd_srv_type_is_valid("_foo."));
432 assert_se(!dnssd_srv_type_is_valid("_föo._tcp"));
433 assert_se(!dnssd_srv_type_is_valid("_f\no._tcp"));
434 assert_se(!dnssd_srv_type_is_valid("_800._tcp"));
435 assert_se(!dnssd_srv_type_is_valid("_-800._tcp"));
436 assert_se(!dnssd_srv_type_is_valid("_-foo._tcp"));
437 assert_se(!dnssd_srv_type_is_valid("_piep._foo._udp"));
438 assert_se(!dnssd_srv_type_is_valid("_foo._unknown"));
441 static void test_dns_service_join_one(const char *a
, const char *b
, const char *c
, int r
, const char *d
) {
442 _cleanup_free_
char *x
= NULL
, *y
= NULL
, *z
= NULL
, *t
= NULL
;
444 assert_se(dns_service_join(a
, b
, c
, &t
) == r
);
445 assert_se(streq_ptr(t
, d
));
450 assert_se(dns_service_split(t
, &x
, &y
, &z
) >= 0);
451 assert_se(streq_ptr(a
, x
));
452 assert_se(streq_ptr(b
, y
));
453 assert_se(dns_name_equal(c
, z
) > 0);
456 static void test_dns_service_join(void) {
457 test_dns_service_join_one("", "", "", -EINVAL
, NULL
);
458 test_dns_service_join_one("", "_http._tcp", "", -EINVAL
, NULL
);
459 test_dns_service_join_one("", "_http._tcp", "foo", -EINVAL
, NULL
);
460 test_dns_service_join_one("foo", "", "foo", -EINVAL
, NULL
);
461 test_dns_service_join_one("foo", "foo", "foo", -EINVAL
, NULL
);
463 test_dns_service_join_one("foo", "_http._tcp", "", 0, "foo._http._tcp");
464 test_dns_service_join_one(NULL
, "_http._tcp", "", 0, "_http._tcp");
465 test_dns_service_join_one("foo", "_http._tcp", "foo", 0, "foo._http._tcp.foo");
466 test_dns_service_join_one(NULL
, "_http._tcp", "foo", 0, "_http._tcp.foo");
467 test_dns_service_join_one("Lennart's PC", "_pc._tcp", "foo.bar.com", 0, "Lennart\\039s\\032PC._pc._tcp.foo.bar.com");
468 test_dns_service_join_one(NULL
, "_pc._tcp", "foo.bar.com", 0, "_pc._tcp.foo.bar.com");
471 static void test_dns_service_split_one(const char *joined
, const char *a
, const char *b
, const char *c
, int r
) {
472 _cleanup_free_
char *x
= NULL
, *y
= NULL
, *z
= NULL
, *t
= NULL
;
474 assert_se(dns_service_split(joined
, &x
, &y
, &z
) == r
);
475 assert_se(streq_ptr(x
, a
));
476 assert_se(streq_ptr(y
, b
));
477 assert_se(streq_ptr(z
, c
));
483 assert_se(dns_service_join(x
, y
, z
, &t
) == 0);
484 assert_se(dns_name_equal(joined
, t
) > 0);
486 assert_se(!x
&& dns_name_equal(z
, joined
) > 0);
489 static void test_dns_service_split(void) {
490 test_dns_service_split_one("", NULL
, NULL
, ".", 0);
491 test_dns_service_split_one("foo", NULL
, NULL
, "foo", 0);
492 test_dns_service_split_one("foo.bar", NULL
, NULL
, "foo.bar", 0);
493 test_dns_service_split_one("_foo.bar", NULL
, NULL
, "_foo.bar", 0);
494 test_dns_service_split_one("_foo._bar", NULL
, "_foo._bar", ".", 0);
495 test_dns_service_split_one("_meh._foo._bar", "_meh", "_foo._bar", ".", 0);
496 test_dns_service_split_one("Wuff\\032Wuff._foo._bar.waldo.com", "Wuff Wuff", "_foo._bar", "waldo.com", 0);
499 static void test_dns_name_change_suffix_one(const char *name
, const char *old_suffix
, const char *new_suffix
, int r
, const char *result
) {
500 _cleanup_free_
char *s
= NULL
;
502 assert_se(dns_name_change_suffix(name
, old_suffix
, new_suffix
, &s
) == r
);
503 assert_se(streq_ptr(s
, result
));
506 static void test_dns_name_change_suffix(void) {
507 test_dns_name_change_suffix_one("foo.bar", "bar", "waldo", 1, "foo.waldo");
508 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "foo.bar.waldi.quux", "piff.paff", 1, "piff.paff");
509 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "bar.waldi.quux", "piff.paff", 1, "foo.piff.paff");
510 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "waldi.quux", "piff.paff", 1, "foo.bar.piff.paff");
511 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "quux", "piff.paff", 1, "foo.bar.waldi.piff.paff");
512 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "", "piff.paff", 1, "foo.bar.waldi.quux.piff.paff");
513 test_dns_name_change_suffix_one("", "", "piff.paff", 1, "piff.paff");
514 test_dns_name_change_suffix_one("", "", "", 1, ".");
515 test_dns_name_change_suffix_one("a", "b", "c", 0, NULL
);
518 static void test_dns_name_suffix_one(const char *name
, unsigned n_labels
, const char *result
, int ret
) {
519 const char *p
= NULL
;
521 assert_se(ret
== dns_name_suffix(name
, n_labels
, &p
));
522 assert_se(streq_ptr(p
, result
));
525 static void test_dns_name_suffix(void) {
526 test_dns_name_suffix_one("foo.bar", 2, "foo.bar", 0);
527 test_dns_name_suffix_one("foo.bar", 1, "bar", 1);
528 test_dns_name_suffix_one("foo.bar", 0, "", 2);
529 test_dns_name_suffix_one("foo.bar", 3, NULL
, -EINVAL
);
530 test_dns_name_suffix_one("foo.bar", 4, NULL
, -EINVAL
);
532 test_dns_name_suffix_one("bar", 1, "bar", 0);
533 test_dns_name_suffix_one("bar", 0, "", 1);
534 test_dns_name_suffix_one("bar", 2, NULL
, -EINVAL
);
535 test_dns_name_suffix_one("bar", 3, NULL
, -EINVAL
);
537 test_dns_name_suffix_one("", 0, "", 0);
538 test_dns_name_suffix_one("", 1, NULL
, -EINVAL
);
539 test_dns_name_suffix_one("", 2, NULL
, -EINVAL
);
542 static void test_dns_name_count_labels_one(const char *name
, int n
) {
543 assert_se(dns_name_count_labels(name
) == n
);
546 static void test_dns_name_count_labels(void) {
547 test_dns_name_count_labels_one("foo.bar.quux.", 3);
548 test_dns_name_count_labels_one("foo.bar.quux", 3);
549 test_dns_name_count_labels_one("foo.bar.", 2);
550 test_dns_name_count_labels_one("foo.bar", 2);
551 test_dns_name_count_labels_one("foo.", 1);
552 test_dns_name_count_labels_one("foo", 1);
553 test_dns_name_count_labels_one("", 0);
554 test_dns_name_count_labels_one(".", 0);
555 test_dns_name_count_labels_one("..", -EINVAL
);
558 static void test_dns_name_equal_skip_one(const char *a
, unsigned n_labels
, const char *b
, int ret
) {
559 assert_se(dns_name_equal_skip(a
, n_labels
, b
) == ret
);
562 static void test_dns_name_equal_skip(void) {
563 test_dns_name_equal_skip_one("foo", 0, "bar", 0);
564 test_dns_name_equal_skip_one("foo", 0, "foo", 1);
565 test_dns_name_equal_skip_one("foo", 1, "foo", 0);
566 test_dns_name_equal_skip_one("foo", 2, "foo", 0);
568 test_dns_name_equal_skip_one("foo.bar", 0, "foo.bar", 1);
569 test_dns_name_equal_skip_one("foo.bar", 1, "foo.bar", 0);
570 test_dns_name_equal_skip_one("foo.bar", 2, "foo.bar", 0);
571 test_dns_name_equal_skip_one("foo.bar", 3, "foo.bar", 0);
573 test_dns_name_equal_skip_one("foo.bar", 0, "bar", 0);
574 test_dns_name_equal_skip_one("foo.bar", 1, "bar", 1);
575 test_dns_name_equal_skip_one("foo.bar", 2, "bar", 0);
576 test_dns_name_equal_skip_one("foo.bar", 3, "bar", 0);
578 test_dns_name_equal_skip_one("foo.bar", 0, "", 0);
579 test_dns_name_equal_skip_one("foo.bar", 1, "", 0);
580 test_dns_name_equal_skip_one("foo.bar", 2, "", 1);
581 test_dns_name_equal_skip_one("foo.bar", 3, "", 0);
583 test_dns_name_equal_skip_one("", 0, "", 1);
584 test_dns_name_equal_skip_one("", 1, "", 0);
585 test_dns_name_equal_skip_one("", 1, "foo", 0);
586 test_dns_name_equal_skip_one("", 2, "foo", 0);
589 static void test_dns_name_compare_func(void) {
590 assert_se(dns_name_compare_func("", "") == 0);
591 assert_se(dns_name_compare_func("", ".") == 0);
592 assert_se(dns_name_compare_func(".", "") == 0);
593 assert_se(dns_name_compare_func("foo", "foo.") == 0);
594 assert_se(dns_name_compare_func("foo.", "foo") == 0);
595 assert_se(dns_name_compare_func("foo", "foo") == 0);
596 assert_se(dns_name_compare_func("foo.", "foo.") == 0);
597 assert_se(dns_name_compare_func("heise.de", "HEISE.DE.") == 0);
599 assert_se(dns_name_compare_func("de.", "heise.de") != 0);
602 static void test_dns_name_common_suffix_one(const char *a
, const char *b
, const char *result
) {
605 assert_se(dns_name_common_suffix(a
, b
, &c
) >= 0);
606 assert_se(streq(c
, result
));
609 static void test_dns_name_common_suffix(void) {
610 test_dns_name_common_suffix_one("", "", "");
611 test_dns_name_common_suffix_one("foo", "", "");
612 test_dns_name_common_suffix_one("", "foo", "");
613 test_dns_name_common_suffix_one("foo", "bar", "");
614 test_dns_name_common_suffix_one("bar", "foo", "");
615 test_dns_name_common_suffix_one("foo", "foo", "foo");
616 test_dns_name_common_suffix_one("quux.foo", "foo", "foo");
617 test_dns_name_common_suffix_one("foo", "quux.foo", "foo");
618 test_dns_name_common_suffix_one("this.is.a.short.sentence", "this.is.another.short.sentence", "short.sentence");
619 test_dns_name_common_suffix_one("FOO.BAR", "tEST.bAR", "BAR");
622 static void test_dns_name_apply_idna_one(const char *s
, int expected
, const char *result
) {
623 _cleanup_free_
char *buf
= NULL
;
626 r
= dns_name_apply_idna(s
, &buf
);
627 log_debug("dns_name_apply_idna: \"%s\" → %d/\"%s\" (expected %d/\"%s\")",
628 s
, r
, strnull(buf
), expected
, strnull(result
));
630 /* Different libidn2 versions are more and less accepting
631 * of underscore-prefixed names. So let's list the lowest
632 * expected return value. */
633 assert_se(r
>= expected
);
635 assert_se(dns_name_equal(buf
, result
) == 1);
638 static void test_dns_name_apply_idna(void) {
639 #if HAVE_LIBIDN2 || HAVE_LIBIDN
645 /* IDNA2008 forbids names with hyphens in third and fourth positions
646 * (https://tools.ietf.org/html/rfc5891#section-4.2.3.1).
647 * IDNA2003 does not have this restriction
648 * (https://tools.ietf.org/html/rfc3490#section-5).
649 * This means that when using libidn we will transform and test more
650 * labels. If registrars follow IDNA2008 we'll just be performing a
659 test_dns_name_apply_idna_one("", ret
, "");
660 test_dns_name_apply_idna_one("foo", ret
, "foo");
661 test_dns_name_apply_idna_one("foo.", ret
, "foo");
662 test_dns_name_apply_idna_one("foo.bar", ret
, "foo.bar");
663 test_dns_name_apply_idna_one("foo.bar.", ret
, "foo.bar");
664 test_dns_name_apply_idna_one("föö", ret
, "xn--f-1gaa");
665 test_dns_name_apply_idna_one("föö.", ret
, "xn--f-1gaa");
666 test_dns_name_apply_idna_one("föö.bär", ret
, "xn--f-1gaa.xn--br-via");
667 test_dns_name_apply_idna_one("föö.bär.", ret
, "xn--f-1gaa.xn--br-via");
668 test_dns_name_apply_idna_one("xn--f-1gaa.xn--br-via", ret
, "xn--f-1gaa.xn--br-via");
670 test_dns_name_apply_idna_one("_443._tcp.fedoraproject.org", ret2
,
671 "_443._tcp.fedoraproject.org");
672 test_dns_name_apply_idna_one("_443", ret2
, "_443");
673 test_dns_name_apply_idna_one("gateway", ret
, "gateway");
674 test_dns_name_apply_idna_one("_gateway", ret2
, "_gateway");
676 test_dns_name_apply_idna_one("r3---sn-ab5l6ne7.googlevideo.com", ret2
,
677 ret2
? "r3---sn-ab5l6ne7.googlevideo.com" : "");
680 static void test_dns_name_is_valid_or_address(void) {
681 assert_se(dns_name_is_valid_or_address(NULL
) == 0);
682 assert_se(dns_name_is_valid_or_address("") == 0);
683 assert_se(dns_name_is_valid_or_address("foobar") > 0);
684 assert_se(dns_name_is_valid_or_address("foobar.com") > 0);
685 assert_se(dns_name_is_valid_or_address("foobar..com") == 0);
686 assert_se(dns_name_is_valid_or_address("foobar.com.") > 0);
687 assert_se(dns_name_is_valid_or_address("127.0.0.1") > 0);
688 assert_se(dns_name_is_valid_or_address("::") > 0);
689 assert_se(dns_name_is_valid_or_address("::1") > 0);
692 int main(int argc
, char *argv
[]) {
693 log_set_max_level(LOG_DEBUG
);
694 log_parse_environment();
697 test_dns_label_unescape();
698 test_dns_label_unescape_suffix();
699 test_dns_label_escape();
700 test_dns_name_normalize();
701 test_dns_name_equal();
702 test_dns_name_endswith();
703 test_dns_name_startswith();
704 test_dns_name_between();
705 test_dns_name_is_root();
706 test_dns_name_is_single_label();
707 test_dns_name_reverse();
708 test_dns_name_concat();
709 test_dns_name_is_valid();
710 test_dns_name_to_wire_format();
711 test_dns_service_name_is_valid();
712 test_dns_srv_type_is_valid();
713 test_dnssd_srv_type_is_valid();
714 test_dns_service_join();
715 test_dns_service_split();
716 test_dns_name_change_suffix();
717 test_dns_name_suffix();
718 test_dns_name_count_labels();
719 test_dns_name_equal_skip();
720 test_dns_name_compare_func();
721 test_dns_name_common_suffix();
722 test_dns_name_apply_idna();
723 test_dns_name_is_valid_or_address();