]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-dns-domain.c
unit: sent change signal before removing the unit if necessary (#4106)
[thirdparty/systemd.git] / src / test / test-dns-domain.c
CommitLineData
74b2466e
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
b5efdb8a 20#include "alloc-util.h"
4ad7f276 21#include "dns-domain.h"
07630cea
LP
22#include "macro.h"
23#include "string-util.h"
74b2466e
LP
24
25static void test_dns_label_unescape_one(const char *what, const char *expect, size_t buffer_sz, int ret) {
26 char buffer[buffer_sz];
27 int r;
28
29 r = dns_label_unescape(&what, buffer, buffer_sz);
30 assert_se(r == ret);
31
32 if (r < 0)
33 return;
34
35 assert_se(streq(buffer, expect));
36}
37
38static void test_dns_label_unescape(void) {
39 test_dns_label_unescape_one("hallo", "hallo", 6, 5);
37ade128 40 test_dns_label_unescape_one("hallo", "hallo", 4, -ENOBUFS);
74b2466e
LP
41 test_dns_label_unescape_one("", "", 10, 0);
42 test_dns_label_unescape_one("hallo\\.foobar", "hallo.foobar", 20, 12);
43 test_dns_label_unescape_one("hallo.foobar", "hallo", 10, 5);
44 test_dns_label_unescape_one("hallo\n.foobar", "hallo", 20, -EINVAL);
45 test_dns_label_unescape_one("hallo\\", "hallo", 20, -EINVAL);
46 test_dns_label_unescape_one("hallo\\032 ", "hallo ", 20, 7);
47 test_dns_label_unescape_one(".", "", 20, 0);
48 test_dns_label_unescape_one("..", "", 20, -EINVAL);
49 test_dns_label_unescape_one(".foobar", "", 20, -EINVAL);
50 test_dns_label_unescape_one("foobar.", "foobar", 20, 6);
51}
52
54adabf7
BG
53static void test_dns_name_to_wire_format_one(const char *what, const char *expect, size_t buffer_sz, int ret) {
54 uint8_t buffer[buffer_sz];
55 int r;
56
3cd03457 57 r = dns_name_to_wire_format(what, buffer, buffer_sz, false);
54adabf7
BG
58 assert_se(r == ret);
59
60 if (r < 0)
61 return;
62
63 assert_se(!memcmp(buffer, expect, r));
64}
65
66static void test_dns_name_to_wire_format(void) {
50dee79b
LP
67 static const char out0[] = { 0 };
68 static const char out1[] = { 3, 'f', 'o', 'o', 0 };
69 static const char out2[] = { 5, 'h', 'a', 'l', 'l', 'o', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 };
70 static const char out3[] = { 4, ' ', 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 };
71 static const char out4[] = { 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
72 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
73 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
74 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
75 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
76 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
77 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
78 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
79 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
80 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
81 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
82 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
83 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
84 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
85 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
86 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
87 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
88 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
89 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
90 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
91 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
92 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
93 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
94 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
95 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
96 3, 'a', '1', '2', 0 };
54adabf7 97
c6cefd13 98 test_dns_name_to_wire_format_one("", out0, sizeof(out0), sizeof(out0));
54adabf7
BG
99
100 test_dns_name_to_wire_format_one("foo", out1, sizeof(out1), sizeof(out1));
101 test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) + 1, sizeof(out1));
102 test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) - 1, -ENOBUFS);
103
104 test_dns_name_to_wire_format_one("hallo.foo.bar", out2, sizeof(out2), sizeof(out2));
105 test_dns_name_to_wire_format_one("hallo.foo..bar", NULL, 32, -EINVAL);
106
107 test_dns_name_to_wire_format_one("\\032foo.bar", out3, sizeof(out3), sizeof(out3));
50dee79b
LP
108
109 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);
110 test_dns_name_to_wire_format_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12", out4, sizeof(out4), sizeof(out4));
54adabf7
BG
111}
112
642900d3
TG
113static void test_dns_label_unescape_suffix_one(const char *what, const char *expect1, const char *expect2, size_t buffer_sz, int ret1, int ret2) {
114 char buffer[buffer_sz];
115 const char *label;
116 int r;
117
118 label = what + strlen(what);
119
120 r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
121 assert_se(r == ret1);
122 if (r >= 0)
123 assert_se(streq(buffer, expect1));
124
125 r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
126 assert_se(r == ret2);
127 if (r >= 0)
128 assert_se(streq(buffer, expect2));
129}
130
131static void test_dns_label_unescape_suffix(void) {
132 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0);
37ade128 133 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOBUFS, -ENOBUFS);
642900d3
TG
134 test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0);
135 test_dns_label_unescape_suffix_one("hallo\\.foobar", "hallo.foobar", "", 20, 12, 0);
136 test_dns_label_unescape_suffix_one("hallo.foobar", "foobar", "hallo", 10, 6, 5);
137 test_dns_label_unescape_suffix_one("hallo.foobar\n", "foobar", "foobar", 20, -EINVAL, -EINVAL);
138 test_dns_label_unescape_suffix_one("hallo\\", "hallo", "hallo", 20, -EINVAL, -EINVAL);
139 test_dns_label_unescape_suffix_one("hallo\\032 ", "hallo ", "", 20, 7, 0);
140 test_dns_label_unescape_suffix_one(".", "", "", 20, 0, 0);
56512859 141 test_dns_label_unescape_suffix_one("..", "", "", 20, 0, -EINVAL);
642900d3 142 test_dns_label_unescape_suffix_one(".foobar", "foobar", "", 20, 6, -EINVAL);
56512859 143 test_dns_label_unescape_suffix_one("foobar.", "foobar", "", 20, 6, 0);
642900d3
TG
144 test_dns_label_unescape_suffix_one("foo\\\\bar", "foo\\bar", "", 20, 7, 0);
145 test_dns_label_unescape_suffix_one("foo.bar", "bar", "foo", 20, 3, 3);
146 test_dns_label_unescape_suffix_one("foo..bar", "bar", "", 20, 3, -EINVAL);
147 test_dns_label_unescape_suffix_one("foo...bar", "bar", "", 20, 3, -EINVAL);
148 test_dns_label_unescape_suffix_one("foo\\.bar", "foo.bar", "", 20, 7, 0);
149 test_dns_label_unescape_suffix_one("foo\\\\.bar", "bar", "foo\\", 20, 3, 4);
150 test_dns_label_unescape_suffix_one("foo\\\\\\.bar", "foo\\.bar", "", 20, 8, 0);
151}
152
74b2466e
LP
153static void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) {
154 _cleanup_free_ char *t = NULL;
155 int r;
156
422baca0 157 r = dns_label_escape_new(what, l, &t);
0c0cdb06 158 assert_se(r == ret);
74b2466e
LP
159
160 if (r < 0)
161 return;
162
163 assert_se(streq_ptr(expect, t));
164}
165
166static void test_dns_label_escape(void) {
3b37fa73 167 test_dns_label_escape_one("", 0, NULL, -EINVAL);
74b2466e 168 test_dns_label_escape_one("hallo", 5, "hallo", 5);
c7feab76 169 test_dns_label_escape_one("hallo", 6, "hallo\\000", 9);
74b2466e
LP
170 test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31);
171}
172
173static void test_dns_name_normalize_one(const char *what, const char *expect, int ret) {
174 _cleanup_free_ char *t = NULL;
175 int r;
176
177 r = dns_name_normalize(what, &t);
178 assert_se(r == ret);
179
180 if (r < 0)
181 return;
182
183 assert_se(streq_ptr(expect, t));
184}
185
186static void test_dns_name_normalize(void) {
3a519900 187 test_dns_name_normalize_one("", ".", 0);
74b2466e
LP
188 test_dns_name_normalize_one("f", "f", 0);
189 test_dns_name_normalize_one("f.waldi", "f.waldi", 0);
190 test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0);
c7feab76 191 test_dns_name_normalize_one("\\000", "\\000", 0);
74b2466e
LP
192 test_dns_name_normalize_one("..", NULL, -EINVAL);
193 test_dns_name_normalize_one(".foobar", NULL, -EINVAL);
194 test_dns_name_normalize_one("foobar.", "foobar", 0);
3a519900 195 test_dns_name_normalize_one(".", ".", 0);
74b2466e
LP
196}
197
198static void test_dns_name_equal_one(const char *a, const char *b, int ret) {
199 int r;
200
201 r = dns_name_equal(a, b);
202 assert_se(r == ret);
203
204 r = dns_name_equal(b, a);
205 assert_se(r == ret);
206}
207
208static void test_dns_name_equal(void) {
209 test_dns_name_equal_one("", "", true);
210 test_dns_name_equal_one("x", "x", true);
211 test_dns_name_equal_one("x", "x.", true);
212 test_dns_name_equal_one("abc.def", "abc.def", true);
213 test_dns_name_equal_one("abc.def", "ABC.def", true);
214 test_dns_name_equal_one("abc.def", "CBA.def", false);
215 test_dns_name_equal_one("", "xxx", false);
216 test_dns_name_equal_one("ab", "a", false);
c7feab76 217 test_dns_name_equal_one("\\000", "\\000", true);
74b2466e
LP
218 test_dns_name_equal_one(".", "", true);
219 test_dns_name_equal_one(".", ".", true);
220 test_dns_name_equal_one("..", "..", -EINVAL);
221}
222
ae72b22c
TG
223static void test_dns_name_between_one(const char *a, const char *b, const char *c, int ret) {
224 int r;
225
226 r = dns_name_between(a, b, c);
227 assert_se(r == ret);
228
229 r = dns_name_between(c, b, a);
230 if (ret >= 0)
231 assert_se(r == 0);
232 else
233 assert_se(r == ret);
234}
235
236static void test_dns_name_between(void) {
237 /* see https://tools.ietf.org/html/rfc4034#section-6.1
238 Note that we use "\033.z.example" in stead of "\001.z.example" as we
239 consider the latter invalid */
240 test_dns_name_between_one("example", "a.example", "yljkjljk.a.example", true);
241 test_dns_name_between_one("a.example", "yljkjljk.a.example", "Z.a.example", true);
242 test_dns_name_between_one("yljkjljk.a.example", "Z.a.example", "zABC.a.EXAMPLE", true);
243 test_dns_name_between_one("Z.a.example", "zABC.a.EXAMPLE", "z.example", true);
244 test_dns_name_between_one("zABC.a.EXAMPLE", "z.example", "\\033.z.example", true);
245 test_dns_name_between_one("z.example", "\\033.z.example", "*.z.example", true);
246 test_dns_name_between_one("\\033.z.example", "*.z.example", "\\200.z.example", true);
247 test_dns_name_between_one("*.z.example", "\\200.z.example", "example", true);
248 test_dns_name_between_one("\\200.z.example", "example", "a.example", true);
249
250 test_dns_name_between_one("example", "a.example", "example", -EINVAL);
251 test_dns_name_between_one("example", "example", "yljkjljk.a.example", false);
252 test_dns_name_between_one("example", "yljkjljk.a.example", "yljkjljk.a.example", false);
253}
254
74b2466e
LP
255static void test_dns_name_endswith_one(const char *a, const char *b, int ret) {
256 assert_se(dns_name_endswith(a, b) == ret);
257}
258
259static void test_dns_name_endswith(void) {
260 test_dns_name_endswith_one("", "", true);
261 test_dns_name_endswith_one("", "xxx", false);
262 test_dns_name_endswith_one("xxx", "", true);
263 test_dns_name_endswith_one("x", "x", true);
264 test_dns_name_endswith_one("x", "y", false);
265 test_dns_name_endswith_one("x.y", "y", true);
266 test_dns_name_endswith_one("x.y", "Y", true);
267 test_dns_name_endswith_one("x.y", "x", false);
268 test_dns_name_endswith_one("x.y.z", "Z", true);
269 test_dns_name_endswith_one("x.y.z", "y.Z", true);
270 test_dns_name_endswith_one("x.y.z", "x.y.Z", true);
271 test_dns_name_endswith_one("x.y.z", "waldo", false);
272 test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false);
273 test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true);
274 test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL);
275}
276
eb241cdb
LP
277static void test_dns_name_startswith_one(const char *a, const char *b, int ret) {
278 assert_se(dns_name_startswith(a, b) == ret);
279}
280
281static void test_dns_name_startswith(void) {
282 test_dns_name_startswith_one("", "", true);
283 test_dns_name_startswith_one("", "xxx", false);
284 test_dns_name_startswith_one("xxx", "", true);
285 test_dns_name_startswith_one("x", "x", true);
286 test_dns_name_startswith_one("x", "y", false);
287 test_dns_name_startswith_one("x.y", "x.y", true);
288 test_dns_name_startswith_one("x.y", "y.x", false);
289 test_dns_name_startswith_one("x.y", "x", true);
290 test_dns_name_startswith_one("x.y", "X", true);
291 test_dns_name_startswith_one("x.y", "y", false);
292 test_dns_name_startswith_one("x.y", "", true);
293 test_dns_name_startswith_one("x.y", "X", true);
294}
295
dc477e73
LP
296static void test_dns_name_is_root(void) {
297 assert_se(dns_name_is_root(""));
298 assert_se(dns_name_is_root("."));
299 assert_se(!dns_name_is_root("xxx"));
300 assert_se(!dns_name_is_root("xxx."));
301 assert_se(!dns_name_is_root(".."));
74b2466e
LP
302}
303
dc477e73
LP
304static void test_dns_name_is_single_label(void) {
305 assert_se(!dns_name_is_single_label(""));
306 assert_se(!dns_name_is_single_label("."));
307 assert_se(!dns_name_is_single_label(".."));
308 assert_se(dns_name_is_single_label("x"));
309 assert_se(dns_name_is_single_label("x."));
310 assert_se(!dns_name_is_single_label("xx.yy"));
74b2466e
LP
311}
312
b914e211
LP
313static void test_dns_name_reverse_one(const char *address, const char *name) {
314 _cleanup_free_ char *p = NULL;
a7f7d1bd 315 union in_addr_union a, b = {};
b914e211
LP
316 int familya, familyb;
317
318 assert_se(in_addr_from_string_auto(address, &familya, &a) >= 0);
319 assert_se(dns_name_reverse(familya, &a, &p) >= 0);
320 assert_se(streq(p, name));
321 assert_se(dns_name_address(p, &familyb, &b) > 0);
322 assert_se(familya == familyb);
323 assert_se(in_addr_equal(familya, &a, &b));
324}
325
326static void test_dns_name_reverse(void) {
327 test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa");
328 test_dns_name_reverse_one("fe80::47", "7.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa");
9436e8ca
LP
329 test_dns_name_reverse_one("127.0.0.1", "1.0.0.127.in-addr.arpa");
330 test_dns_name_reverse_one("::1", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa");
b914e211
LP
331}
332
9ca45586
LP
333static void test_dns_name_concat_one(const char *a, const char *b, int r, const char *result) {
334 _cleanup_free_ char *p = NULL;
335
336 assert_se(dns_name_concat(a, b, &p) == r);
337 assert_se(streq_ptr(p, result));
338}
339
340static void test_dns_name_concat(void) {
3a519900
LP
341 test_dns_name_concat_one("", "", 0, ".");
342 test_dns_name_concat_one(".", "", 0, ".");
343 test_dns_name_concat_one("", ".", 0, ".");
344 test_dns_name_concat_one(".", ".", 0, ".");
9ca45586
LP
345 test_dns_name_concat_one("foo", "bar", 0, "foo.bar");
346 test_dns_name_concat_one("foo.foo", "bar.bar", 0, "foo.foo.bar.bar");
347 test_dns_name_concat_one("foo", NULL, 0, "foo");
3a519900 348 test_dns_name_concat_one("foo", ".", 0, "foo");
9ca45586 349 test_dns_name_concat_one("foo.", "bar.", 0, "foo.bar");
3a519900
LP
350 test_dns_name_concat_one(NULL, NULL, 0, ".");
351 test_dns_name_concat_one(NULL, ".", 0, ".");
352 test_dns_name_concat_one(NULL, "foo", 0, "foo");
9ca45586
LP
353}
354
355static void test_dns_name_is_valid_one(const char *s, int ret) {
356 assert_se(dns_name_is_valid(s) == ret);
357}
358
359static void test_dns_name_is_valid(void) {
360 test_dns_name_is_valid_one("foo", 1);
361 test_dns_name_is_valid_one("foo.", 1);
362 test_dns_name_is_valid_one("Foo", 1);
363 test_dns_name_is_valid_one("foo.bar", 1);
364 test_dns_name_is_valid_one("foo.bar.baz", 1);
365 test_dns_name_is_valid_one("", 1);
366 test_dns_name_is_valid_one("foo..bar", 0);
367 test_dns_name_is_valid_one(".foo.bar", 0);
368 test_dns_name_is_valid_one("foo.bar.", 1);
369 test_dns_name_is_valid_one("\\zbar", 0);
370 test_dns_name_is_valid_one("ä", 1);
371 test_dns_name_is_valid_one("\n", 0);
1dfbf000
LP
372
373 /* 256 characters*/
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.a12345", 0);
375
376 /* 255 characters*/
377 test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a1234", 0);
378
379 /* 254 characters*/
380 test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a123", 0);
381
382 /* 253 characters*/
383 test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12", 1);
384
385 /* label of 64 chars length */
386 test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a123", 0);
387
388 /* label of 63 chars length */
389 test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a12", 1);
9ca45586
LP
390}
391
0a49b6b6
LP
392static void test_dns_service_name_is_valid(void) {
393 assert_se(dns_service_name_is_valid("Lennart's Compüter"));
394 assert_se(dns_service_name_is_valid("piff.paff"));
395
396 assert_se(!dns_service_name_is_valid(NULL));
397 assert_se(!dns_service_name_is_valid(""));
398 assert_se(!dns_service_name_is_valid("foo\nbar"));
399 assert_se(!dns_service_name_is_valid("foo\201bar"));
400 assert_se(!dns_service_name_is_valid("this is an overly long string that is certainly longer than 63 characters"));
401}
402
7e8131e9
LP
403static void test_dns_srv_type_is_valid(void) {
404
405 assert_se(dns_srv_type_is_valid("_http._tcp"));
406 assert_se(dns_srv_type_is_valid("_foo-bar._tcp"));
407 assert_se(dns_srv_type_is_valid("_w._udp"));
408 assert_se(dns_srv_type_is_valid("_a800._tcp"));
409 assert_se(dns_srv_type_is_valid("_a-800._tcp"));
410
411 assert_se(!dns_srv_type_is_valid(NULL));
412 assert_se(!dns_srv_type_is_valid(""));
413 assert_se(!dns_srv_type_is_valid("x"));
414 assert_se(!dns_srv_type_is_valid("_foo"));
415 assert_se(!dns_srv_type_is_valid("_tcp"));
416 assert_se(!dns_srv_type_is_valid("_"));
417 assert_se(!dns_srv_type_is_valid("_foo."));
418 assert_se(!dns_srv_type_is_valid("_föo._tcp"));
419 assert_se(!dns_srv_type_is_valid("_f\no._tcp"));
420 assert_se(!dns_srv_type_is_valid("_800._tcp"));
421 assert_se(!dns_srv_type_is_valid("_-800._tcp"));
422 assert_se(!dns_srv_type_is_valid("_-foo._tcp"));
423 assert_se(!dns_srv_type_is_valid("_piep._foo._udp"));
0e8eedbb
LP
424}
425
426static void test_dns_service_join_one(const char *a, const char *b, const char *c, int r, const char *d) {
427 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *t = NULL;
428
429 assert_se(dns_service_join(a, b, c, &t) == r);
430 assert_se(streq_ptr(t, d));
431
432 if (r < 0)
433 return;
434
435 assert_se(dns_service_split(t, &x, &y, &z) >= 0);
436 assert_se(streq_ptr(a, x));
437 assert_se(streq_ptr(b, y));
3a519900 438 assert_se(dns_name_equal(c, z) > 0);
0e8eedbb
LP
439}
440
441static void test_dns_service_join(void) {
442 test_dns_service_join_one("", "", "", -EINVAL, NULL);
443 test_dns_service_join_one("", "_http._tcp", "", -EINVAL, NULL);
444 test_dns_service_join_one("", "_http._tcp", "foo", -EINVAL, NULL);
445 test_dns_service_join_one("foo", "", "foo", -EINVAL, NULL);
446 test_dns_service_join_one("foo", "foo", "foo", -EINVAL, NULL);
447
448 test_dns_service_join_one("foo", "_http._tcp", "", 0, "foo._http._tcp");
449 test_dns_service_join_one(NULL, "_http._tcp", "", 0, "_http._tcp");
450 test_dns_service_join_one("foo", "_http._tcp", "foo", 0, "foo._http._tcp.foo");
451 test_dns_service_join_one(NULL, "_http._tcp", "foo", 0, "_http._tcp.foo");
452 test_dns_service_join_one("Lennart's PC", "_pc._tcp", "foo.bar.com", 0, "Lennart\\039s\\032PC._pc._tcp.foo.bar.com");
453 test_dns_service_join_one(NULL, "_pc._tcp", "foo.bar.com", 0, "_pc._tcp.foo.bar.com");
454}
455
456static void test_dns_service_split_one(const char *joined, const char *a, const char *b, const char *c, int r) {
457 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *t = NULL;
458
459 assert_se(dns_service_split(joined, &x, &y, &z) == r);
460 assert_se(streq_ptr(x, a));
461 assert_se(streq_ptr(y, b));
462 assert_se(streq_ptr(z, c));
463
464 if (r < 0)
465 return;
466
467 if (y) {
468 assert_se(dns_service_join(x, y, z, &t) == 0);
3a519900 469 assert_se(dns_name_equal(joined, t) > 0);
0e8eedbb 470 } else
3a519900 471 assert_se(!x && dns_name_equal(z, joined) > 0);
0e8eedbb
LP
472}
473
474static void test_dns_service_split(void) {
3a519900 475 test_dns_service_split_one("", NULL, NULL, ".", 0);
0e8eedbb
LP
476 test_dns_service_split_one("foo", NULL, NULL, "foo", 0);
477 test_dns_service_split_one("foo.bar", NULL, NULL, "foo.bar", 0);
478 test_dns_service_split_one("_foo.bar", NULL, NULL, "_foo.bar", 0);
3a519900
LP
479 test_dns_service_split_one("_foo._bar", NULL, "_foo._bar", ".", 0);
480 test_dns_service_split_one("_meh._foo._bar", "_meh", "_foo._bar", ".", 0);
0e8eedbb 481 test_dns_service_split_one("Wuff\\032Wuff._foo._bar.waldo.com", "Wuff Wuff", "_foo._bar", "waldo.com", 0);
0a49b6b6
LP
482}
483
58db254a
LP
484static void test_dns_name_change_suffix_one(const char *name, const char *old_suffix, const char *new_suffix, int r, const char *result) {
485 _cleanup_free_ char *s = NULL;
486
487 assert_se(dns_name_change_suffix(name, old_suffix, new_suffix, &s) == r);
488 assert_se(streq_ptr(s, result));
489}
490
491static void test_dns_name_change_suffix(void) {
492 test_dns_name_change_suffix_one("foo.bar", "bar", "waldo", 1, "foo.waldo");
493 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "foo.bar.waldi.quux", "piff.paff", 1, "piff.paff");
494 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "bar.waldi.quux", "piff.paff", 1, "foo.piff.paff");
495 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "waldi.quux", "piff.paff", 1, "foo.bar.piff.paff");
496 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "quux", "piff.paff", 1, "foo.bar.waldi.piff.paff");
497 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "", "piff.paff", 1, "foo.bar.waldi.quux.piff.paff");
498 test_dns_name_change_suffix_one("", "", "piff.paff", 1, "piff.paff");
3a519900 499 test_dns_name_change_suffix_one("", "", "", 1, ".");
58db254a
LP
500 test_dns_name_change_suffix_one("a", "b", "c", 0, NULL);
501}
502
e7ff0e0b
LP
503static void test_dns_name_suffix_one(const char *name, unsigned n_labels, const char *result, int ret) {
504 const char *p = NULL;
505
506 assert_se(ret == dns_name_suffix(name, n_labels, &p));
507 assert_se(streq_ptr(p, result));
508}
509
510static void test_dns_name_suffix(void) {
511 test_dns_name_suffix_one("foo.bar", 2, "foo.bar", 0);
512 test_dns_name_suffix_one("foo.bar", 1, "bar", 1);
513 test_dns_name_suffix_one("foo.bar", 0, "", 2);
514 test_dns_name_suffix_one("foo.bar", 3, NULL, -EINVAL);
515 test_dns_name_suffix_one("foo.bar", 4, NULL, -EINVAL);
516
517 test_dns_name_suffix_one("bar", 1, "bar", 0);
518 test_dns_name_suffix_one("bar", 0, "", 1);
519 test_dns_name_suffix_one("bar", 2, NULL, -EINVAL);
520 test_dns_name_suffix_one("bar", 3, NULL, -EINVAL);
521
522 test_dns_name_suffix_one("", 0, "", 0);
523 test_dns_name_suffix_one("", 1, NULL, -EINVAL);
524 test_dns_name_suffix_one("", 2, NULL, -EINVAL);
525}
526
527static void test_dns_name_count_labels_one(const char *name, int n) {
528 assert_se(dns_name_count_labels(name) == n);
529}
530
531static void test_dns_name_count_labels(void) {
532 test_dns_name_count_labels_one("foo.bar.quux.", 3);
533 test_dns_name_count_labels_one("foo.bar.quux", 3);
534 test_dns_name_count_labels_one("foo.bar.", 2);
535 test_dns_name_count_labels_one("foo.bar", 2);
536 test_dns_name_count_labels_one("foo.", 1);
537 test_dns_name_count_labels_one("foo", 1);
538 test_dns_name_count_labels_one("", 0);
539 test_dns_name_count_labels_one(".", 0);
540 test_dns_name_count_labels_one("..", -EINVAL);
541}
542
db5b0e92
LP
543static void test_dns_name_equal_skip_one(const char *a, unsigned n_labels, const char *b, int ret) {
544 assert_se(dns_name_equal_skip(a, n_labels, b) == ret);
545}
546
547static void test_dns_name_equal_skip(void) {
548 test_dns_name_equal_skip_one("foo", 0, "bar", 0);
549 test_dns_name_equal_skip_one("foo", 0, "foo", 1);
550 test_dns_name_equal_skip_one("foo", 1, "foo", 0);
551 test_dns_name_equal_skip_one("foo", 2, "foo", 0);
552
553 test_dns_name_equal_skip_one("foo.bar", 0, "foo.bar", 1);
554 test_dns_name_equal_skip_one("foo.bar", 1, "foo.bar", 0);
555 test_dns_name_equal_skip_one("foo.bar", 2, "foo.bar", 0);
556 test_dns_name_equal_skip_one("foo.bar", 3, "foo.bar", 0);
557
558 test_dns_name_equal_skip_one("foo.bar", 0, "bar", 0);
559 test_dns_name_equal_skip_one("foo.bar", 1, "bar", 1);
560 test_dns_name_equal_skip_one("foo.bar", 2, "bar", 0);
561 test_dns_name_equal_skip_one("foo.bar", 3, "bar", 0);
562
563 test_dns_name_equal_skip_one("foo.bar", 0, "", 0);
564 test_dns_name_equal_skip_one("foo.bar", 1, "", 0);
565 test_dns_name_equal_skip_one("foo.bar", 2, "", 1);
566 test_dns_name_equal_skip_one("foo.bar", 3, "", 0);
567
568 test_dns_name_equal_skip_one("", 0, "", 1);
569 test_dns_name_equal_skip_one("", 1, "", 0);
570 test_dns_name_equal_skip_one("", 1, "foo", 0);
571 test_dns_name_equal_skip_one("", 2, "foo", 0);
572}
573
56512859
LP
574static void test_dns_name_compare_func(void) {
575 assert_se(dns_name_compare_func("", "") == 0);
576 assert_se(dns_name_compare_func("", ".") == 0);
577 assert_se(dns_name_compare_func(".", "") == 0);
578 assert_se(dns_name_compare_func("foo", "foo.") == 0);
579 assert_se(dns_name_compare_func("foo.", "foo") == 0);
580 assert_se(dns_name_compare_func("foo", "foo") == 0);
581 assert_se(dns_name_compare_func("foo.", "foo.") == 0);
582 assert_se(dns_name_compare_func("heise.de", "HEISE.DE.") == 0);
583
584 assert_se(dns_name_compare_func("de.", "heise.de") != 0);
585}
586
b9282bc1
LP
587static void test_dns_name_common_suffix_one(const char *a, const char *b, const char *result) {
588 const char *c;
589
590 assert_se(dns_name_common_suffix(a, b, &c) >= 0);
591 assert_se(streq(c, result));
592}
593
594static void test_dns_name_common_suffix(void) {
595 test_dns_name_common_suffix_one("", "", "");
596 test_dns_name_common_suffix_one("foo", "", "");
597 test_dns_name_common_suffix_one("", "foo", "");
598 test_dns_name_common_suffix_one("foo", "bar", "");
599 test_dns_name_common_suffix_one("bar", "foo", "");
600 test_dns_name_common_suffix_one("foo", "foo", "foo");
601 test_dns_name_common_suffix_one("quux.foo", "foo", "foo");
602 test_dns_name_common_suffix_one("foo", "quux.foo", "foo");
603 test_dns_name_common_suffix_one("this.is.a.short.sentence", "this.is.another.short.sentence", "short.sentence");
604 test_dns_name_common_suffix_one("FOO.BAR", "tEST.bAR", "BAR");
605}
606
0cf40f55
LP
607static void test_dns_name_apply_idna_one(const char *s, const char *result) {
608#ifdef HAVE_LIBIDN
609 _cleanup_free_ char *buf = NULL;
610 assert_se(dns_name_apply_idna(s, &buf) >= 0);
611 assert_se(dns_name_equal(buf, result) > 0);
612#endif
613}
614
615static void test_dns_name_apply_idna(void) {
616 test_dns_name_apply_idna_one("", "");
617 test_dns_name_apply_idna_one("foo", "foo");
618 test_dns_name_apply_idna_one("foo.", "foo");
619 test_dns_name_apply_idna_one("foo.bar", "foo.bar");
620 test_dns_name_apply_idna_one("foo.bar.", "foo.bar");
621 test_dns_name_apply_idna_one("föö", "xn--f-1gaa");
622 test_dns_name_apply_idna_one("föö.", "xn--f-1gaa");
623 test_dns_name_apply_idna_one("föö.bär", "xn--f-1gaa.xn--br-via");
624 test_dns_name_apply_idna_one("föö.bär.", "xn--f-1gaa.xn--br-via");
625}
626
74b2466e
LP
627int main(int argc, char *argv[]) {
628
629 test_dns_label_unescape();
642900d3 630 test_dns_label_unescape_suffix();
74b2466e
LP
631 test_dns_label_escape();
632 test_dns_name_normalize();
633 test_dns_name_equal();
634 test_dns_name_endswith();
eb241cdb 635 test_dns_name_startswith();
ae72b22c 636 test_dns_name_between();
dc477e73
LP
637 test_dns_name_is_root();
638 test_dns_name_is_single_label();
b914e211 639 test_dns_name_reverse();
9ca45586
LP
640 test_dns_name_concat();
641 test_dns_name_is_valid();
54adabf7 642 test_dns_name_to_wire_format();
0a49b6b6 643 test_dns_service_name_is_valid();
7e8131e9 644 test_dns_srv_type_is_valid();
0e8eedbb
LP
645 test_dns_service_join();
646 test_dns_service_split();
58db254a 647 test_dns_name_change_suffix();
e7ff0e0b
LP
648 test_dns_name_suffix();
649 test_dns_name_count_labels();
db5b0e92 650 test_dns_name_equal_skip();
56512859 651 test_dns_name_compare_func();
b9282bc1 652 test_dns_name_common_suffix();
0cf40f55 653 test_dns_name_apply_idna();
74b2466e
LP
654
655 return 0;
656}