]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-dns-domain.c
resolved: never use data from failed transactions
[thirdparty/systemd.git] / src / test / test-dns-domain.c
CommitLineData
74b2466e
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
b5efdb8a 22#include "alloc-util.h"
4ad7f276 23#include "dns-domain.h"
07630cea
LP
24#include "macro.h"
25#include "string-util.h"
74b2466e
LP
26
27static void test_dns_label_unescape_one(const char *what, const char *expect, size_t buffer_sz, int ret) {
28 char buffer[buffer_sz];
29 int r;
30
31 r = dns_label_unescape(&what, buffer, buffer_sz);
32 assert_se(r == ret);
33
34 if (r < 0)
35 return;
36
37 assert_se(streq(buffer, expect));
38}
39
40static void test_dns_label_unescape(void) {
41 test_dns_label_unescape_one("hallo", "hallo", 6, 5);
37ade128 42 test_dns_label_unescape_one("hallo", "hallo", 4, -ENOBUFS);
74b2466e
LP
43 test_dns_label_unescape_one("", "", 10, 0);
44 test_dns_label_unescape_one("hallo\\.foobar", "hallo.foobar", 20, 12);
45 test_dns_label_unescape_one("hallo.foobar", "hallo", 10, 5);
46 test_dns_label_unescape_one("hallo\n.foobar", "hallo", 20, -EINVAL);
47 test_dns_label_unescape_one("hallo\\", "hallo", 20, -EINVAL);
48 test_dns_label_unescape_one("hallo\\032 ", "hallo ", 20, 7);
49 test_dns_label_unescape_one(".", "", 20, 0);
50 test_dns_label_unescape_one("..", "", 20, -EINVAL);
51 test_dns_label_unescape_one(".foobar", "", 20, -EINVAL);
52 test_dns_label_unescape_one("foobar.", "foobar", 20, 6);
53}
54
54adabf7
BG
55static void test_dns_name_to_wire_format_one(const char *what, const char *expect, size_t buffer_sz, int ret) {
56 uint8_t buffer[buffer_sz];
57 int r;
58
3cd03457 59 r = dns_name_to_wire_format(what, buffer, buffer_sz, false);
54adabf7
BG
60 assert_se(r == ret);
61
62 if (r < 0)
63 return;
64
65 assert_se(!memcmp(buffer, expect, r));
66}
67
68static void test_dns_name_to_wire_format(void) {
50dee79b
LP
69 static const char out0[] = { 0 };
70 static const char out1[] = { 3, 'f', 'o', 'o', 0 };
71 static const char out2[] = { 5, 'h', 'a', 'l', 'l', 'o', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 };
72 static const char out3[] = { 4, ' ', 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 };
73 static const char out4[] = { 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 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
97 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8',
98 3, 'a', '1', '2', 0 };
54adabf7 99
c6cefd13 100 test_dns_name_to_wire_format_one("", out0, sizeof(out0), sizeof(out0));
54adabf7
BG
101
102 test_dns_name_to_wire_format_one("foo", out1, sizeof(out1), sizeof(out1));
103 test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) + 1, sizeof(out1));
104 test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) - 1, -ENOBUFS);
105
106 test_dns_name_to_wire_format_one("hallo.foo.bar", out2, sizeof(out2), sizeof(out2));
107 test_dns_name_to_wire_format_one("hallo.foo..bar", NULL, 32, -EINVAL);
108
109 test_dns_name_to_wire_format_one("\\032foo.bar", out3, sizeof(out3), sizeof(out3));
50dee79b
LP
110
111 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);
112 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
113}
114
642900d3
TG
115static void test_dns_label_unescape_suffix_one(const char *what, const char *expect1, const char *expect2, size_t buffer_sz, int ret1, int ret2) {
116 char buffer[buffer_sz];
117 const char *label;
118 int r;
119
120 label = what + strlen(what);
121
122 r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
123 assert_se(r == ret1);
124 if (r >= 0)
125 assert_se(streq(buffer, expect1));
126
127 r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
128 assert_se(r == ret2);
129 if (r >= 0)
130 assert_se(streq(buffer, expect2));
131}
132
133static void test_dns_label_unescape_suffix(void) {
134 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0);
37ade128 135 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOBUFS, -ENOBUFS);
642900d3
TG
136 test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0);
137 test_dns_label_unescape_suffix_one("hallo\\.foobar", "hallo.foobar", "", 20, 12, 0);
138 test_dns_label_unescape_suffix_one("hallo.foobar", "foobar", "hallo", 10, 6, 5);
139 test_dns_label_unescape_suffix_one("hallo.foobar\n", "foobar", "foobar", 20, -EINVAL, -EINVAL);
140 test_dns_label_unescape_suffix_one("hallo\\", "hallo", "hallo", 20, -EINVAL, -EINVAL);
141 test_dns_label_unescape_suffix_one("hallo\\032 ", "hallo ", "", 20, 7, 0);
142 test_dns_label_unescape_suffix_one(".", "", "", 20, 0, 0);
143 test_dns_label_unescape_suffix_one("..", "", "", 20, 0, 0);
144 test_dns_label_unescape_suffix_one(".foobar", "foobar", "", 20, 6, -EINVAL);
145 test_dns_label_unescape_suffix_one("foobar.", "", "foobar", 20, 0, 6);
146 test_dns_label_unescape_suffix_one("foo\\\\bar", "foo\\bar", "", 20, 7, 0);
147 test_dns_label_unescape_suffix_one("foo.bar", "bar", "foo", 20, 3, 3);
148 test_dns_label_unescape_suffix_one("foo..bar", "bar", "", 20, 3, -EINVAL);
149 test_dns_label_unescape_suffix_one("foo...bar", "bar", "", 20, 3, -EINVAL);
150 test_dns_label_unescape_suffix_one("foo\\.bar", "foo.bar", "", 20, 7, 0);
151 test_dns_label_unescape_suffix_one("foo\\\\.bar", "bar", "foo\\", 20, 3, 4);
152 test_dns_label_unescape_suffix_one("foo\\\\\\.bar", "foo\\.bar", "", 20, 8, 0);
153}
154
74b2466e
LP
155static void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) {
156 _cleanup_free_ char *t = NULL;
157 int r;
158
422baca0 159 r = dns_label_escape_new(what, l, &t);
0c0cdb06 160 assert_se(r == ret);
74b2466e
LP
161
162 if (r < 0)
163 return;
164
165 assert_se(streq_ptr(expect, t));
166}
167
168static void test_dns_label_escape(void) {
3b37fa73 169 test_dns_label_escape_one("", 0, NULL, -EINVAL);
74b2466e
LP
170 test_dns_label_escape_one("hallo", 5, "hallo", 5);
171 test_dns_label_escape_one("hallo", 6, NULL, -EINVAL);
172 test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31);
173}
174
175static void test_dns_name_normalize_one(const char *what, const char *expect, int ret) {
176 _cleanup_free_ char *t = NULL;
177 int r;
178
179 r = dns_name_normalize(what, &t);
180 assert_se(r == ret);
181
182 if (r < 0)
183 return;
184
185 assert_se(streq_ptr(expect, t));
186}
187
188static void test_dns_name_normalize(void) {
189 test_dns_name_normalize_one("", "", 0);
190 test_dns_name_normalize_one("f", "f", 0);
191 test_dns_name_normalize_one("f.waldi", "f.waldi", 0);
192 test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0);
193 test_dns_name_normalize_one("\\000", NULL, -EINVAL);
194 test_dns_name_normalize_one("..", NULL, -EINVAL);
195 test_dns_name_normalize_one(".foobar", NULL, -EINVAL);
196 test_dns_name_normalize_one("foobar.", "foobar", 0);
197 test_dns_name_normalize_one(".", "", 0);
198}
199
200static void test_dns_name_equal_one(const char *a, const char *b, int ret) {
201 int r;
202
203 r = dns_name_equal(a, b);
204 assert_se(r == ret);
205
206 r = dns_name_equal(b, a);
207 assert_se(r == ret);
208}
209
210static void test_dns_name_equal(void) {
211 test_dns_name_equal_one("", "", true);
212 test_dns_name_equal_one("x", "x", true);
213 test_dns_name_equal_one("x", "x.", true);
214 test_dns_name_equal_one("abc.def", "abc.def", true);
215 test_dns_name_equal_one("abc.def", "ABC.def", true);
216 test_dns_name_equal_one("abc.def", "CBA.def", false);
217 test_dns_name_equal_one("", "xxx", false);
218 test_dns_name_equal_one("ab", "a", false);
219 test_dns_name_equal_one("\\000", "xxxx", -EINVAL);
220 test_dns_name_equal_one(".", "", true);
221 test_dns_name_equal_one(".", ".", true);
222 test_dns_name_equal_one("..", "..", -EINVAL);
223}
224
ae72b22c
TG
225static void test_dns_name_between_one(const char *a, const char *b, const char *c, int ret) {
226 int r;
227
228 r = dns_name_between(a, b, c);
229 assert_se(r == ret);
230
231 r = dns_name_between(c, b, a);
232 if (ret >= 0)
233 assert_se(r == 0);
234 else
235 assert_se(r == ret);
236}
237
238static void test_dns_name_between(void) {
239 /* see https://tools.ietf.org/html/rfc4034#section-6.1
240 Note that we use "\033.z.example" in stead of "\001.z.example" as we
241 consider the latter invalid */
242 test_dns_name_between_one("example", "a.example", "yljkjljk.a.example", true);
243 test_dns_name_between_one("a.example", "yljkjljk.a.example", "Z.a.example", true);
244 test_dns_name_between_one("yljkjljk.a.example", "Z.a.example", "zABC.a.EXAMPLE", true);
245 test_dns_name_between_one("Z.a.example", "zABC.a.EXAMPLE", "z.example", true);
246 test_dns_name_between_one("zABC.a.EXAMPLE", "z.example", "\\033.z.example", true);
247 test_dns_name_between_one("z.example", "\\033.z.example", "*.z.example", true);
248 test_dns_name_between_one("\\033.z.example", "*.z.example", "\\200.z.example", true);
249 test_dns_name_between_one("*.z.example", "\\200.z.example", "example", true);
250 test_dns_name_between_one("\\200.z.example", "example", "a.example", true);
251
252 test_dns_name_between_one("example", "a.example", "example", -EINVAL);
253 test_dns_name_between_one("example", "example", "yljkjljk.a.example", false);
254 test_dns_name_between_one("example", "yljkjljk.a.example", "yljkjljk.a.example", false);
255}
256
74b2466e
LP
257static void test_dns_name_endswith_one(const char *a, const char *b, int ret) {
258 assert_se(dns_name_endswith(a, b) == ret);
259}
260
261static void test_dns_name_endswith(void) {
262 test_dns_name_endswith_one("", "", true);
263 test_dns_name_endswith_one("", "xxx", false);
264 test_dns_name_endswith_one("xxx", "", true);
265 test_dns_name_endswith_one("x", "x", true);
266 test_dns_name_endswith_one("x", "y", false);
267 test_dns_name_endswith_one("x.y", "y", true);
268 test_dns_name_endswith_one("x.y", "Y", true);
269 test_dns_name_endswith_one("x.y", "x", false);
270 test_dns_name_endswith_one("x.y.z", "Z", true);
271 test_dns_name_endswith_one("x.y.z", "y.Z", true);
272 test_dns_name_endswith_one("x.y.z", "x.y.Z", true);
273 test_dns_name_endswith_one("x.y.z", "waldo", false);
274 test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false);
275 test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true);
276 test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL);
277}
278
dc477e73
LP
279static void test_dns_name_is_root(void) {
280 assert_se(dns_name_is_root(""));
281 assert_se(dns_name_is_root("."));
282 assert_se(!dns_name_is_root("xxx"));
283 assert_se(!dns_name_is_root("xxx."));
284 assert_se(!dns_name_is_root(".."));
74b2466e
LP
285}
286
dc477e73
LP
287static void test_dns_name_is_single_label(void) {
288 assert_se(!dns_name_is_single_label(""));
289 assert_se(!dns_name_is_single_label("."));
290 assert_se(!dns_name_is_single_label(".."));
291 assert_se(dns_name_is_single_label("x"));
292 assert_se(dns_name_is_single_label("x."));
293 assert_se(!dns_name_is_single_label("xx.yy"));
74b2466e
LP
294}
295
b914e211
LP
296static void test_dns_name_reverse_one(const char *address, const char *name) {
297 _cleanup_free_ char *p = NULL;
a7f7d1bd 298 union in_addr_union a, b = {};
b914e211
LP
299 int familya, familyb;
300
301 assert_se(in_addr_from_string_auto(address, &familya, &a) >= 0);
302 assert_se(dns_name_reverse(familya, &a, &p) >= 0);
303 assert_se(streq(p, name));
304 assert_se(dns_name_address(p, &familyb, &b) > 0);
305 assert_se(familya == familyb);
306 assert_se(in_addr_equal(familya, &a, &b));
307}
308
309static void test_dns_name_reverse(void) {
310 test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa");
311 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
312 test_dns_name_reverse_one("127.0.0.1", "1.0.0.127.in-addr.arpa");
313 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
314}
315
9ca45586
LP
316static void test_dns_name_concat_one(const char *a, const char *b, int r, const char *result) {
317 _cleanup_free_ char *p = NULL;
318
319 assert_se(dns_name_concat(a, b, &p) == r);
320 assert_se(streq_ptr(p, result));
321}
322
323static void test_dns_name_concat(void) {
324 test_dns_name_concat_one("foo", "bar", 0, "foo.bar");
325 test_dns_name_concat_one("foo.foo", "bar.bar", 0, "foo.foo.bar.bar");
326 test_dns_name_concat_one("foo", NULL, 0, "foo");
327 test_dns_name_concat_one("foo.", "bar.", 0, "foo.bar");
328}
329
330static void test_dns_name_is_valid_one(const char *s, int ret) {
331 assert_se(dns_name_is_valid(s) == ret);
332}
333
334static void test_dns_name_is_valid(void) {
335 test_dns_name_is_valid_one("foo", 1);
336 test_dns_name_is_valid_one("foo.", 1);
337 test_dns_name_is_valid_one("Foo", 1);
338 test_dns_name_is_valid_one("foo.bar", 1);
339 test_dns_name_is_valid_one("foo.bar.baz", 1);
340 test_dns_name_is_valid_one("", 1);
341 test_dns_name_is_valid_one("foo..bar", 0);
342 test_dns_name_is_valid_one(".foo.bar", 0);
343 test_dns_name_is_valid_one("foo.bar.", 1);
344 test_dns_name_is_valid_one("\\zbar", 0);
345 test_dns_name_is_valid_one("ä", 1);
346 test_dns_name_is_valid_one("\n", 0);
1dfbf000
LP
347
348 /* 256 characters*/
349 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);
350
351 /* 255 characters*/
352 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);
353
354 /* 254 characters*/
355 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);
356
357 /* 253 characters*/
358 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);
359
360 /* label of 64 chars length */
361 test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a123", 0);
362
363 /* label of 63 chars length */
364 test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a12", 1);
9ca45586
LP
365}
366
0a49b6b6
LP
367static void test_dns_service_name_is_valid(void) {
368 assert_se(dns_service_name_is_valid("Lennart's Compüter"));
369 assert_se(dns_service_name_is_valid("piff.paff"));
370
371 assert_se(!dns_service_name_is_valid(NULL));
372 assert_se(!dns_service_name_is_valid(""));
373 assert_se(!dns_service_name_is_valid("foo\nbar"));
374 assert_se(!dns_service_name_is_valid("foo\201bar"));
375 assert_se(!dns_service_name_is_valid("this is an overly long string that is certainly longer than 63 characters"));
376}
377
7e8131e9
LP
378static void test_dns_srv_type_is_valid(void) {
379
380 assert_se(dns_srv_type_is_valid("_http._tcp"));
381 assert_se(dns_srv_type_is_valid("_foo-bar._tcp"));
382 assert_se(dns_srv_type_is_valid("_w._udp"));
383 assert_se(dns_srv_type_is_valid("_a800._tcp"));
384 assert_se(dns_srv_type_is_valid("_a-800._tcp"));
385
386 assert_se(!dns_srv_type_is_valid(NULL));
387 assert_se(!dns_srv_type_is_valid(""));
388 assert_se(!dns_srv_type_is_valid("x"));
389 assert_se(!dns_srv_type_is_valid("_foo"));
390 assert_se(!dns_srv_type_is_valid("_tcp"));
391 assert_se(!dns_srv_type_is_valid("_"));
392 assert_se(!dns_srv_type_is_valid("_foo."));
393 assert_se(!dns_srv_type_is_valid("_föo._tcp"));
394 assert_se(!dns_srv_type_is_valid("_f\no._tcp"));
395 assert_se(!dns_srv_type_is_valid("_800._tcp"));
396 assert_se(!dns_srv_type_is_valid("_-800._tcp"));
397 assert_se(!dns_srv_type_is_valid("_-foo._tcp"));
398 assert_se(!dns_srv_type_is_valid("_piep._foo._udp"));
0e8eedbb
LP
399}
400
401static void test_dns_service_join_one(const char *a, const char *b, const char *c, int r, const char *d) {
402 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *t = NULL;
403
404 assert_se(dns_service_join(a, b, c, &t) == r);
405 assert_se(streq_ptr(t, d));
406
407 if (r < 0)
408 return;
409
410 assert_se(dns_service_split(t, &x, &y, &z) >= 0);
411 assert_se(streq_ptr(a, x));
412 assert_se(streq_ptr(b, y));
413 assert_se(streq_ptr(c, z));
414}
415
416static void test_dns_service_join(void) {
417 test_dns_service_join_one("", "", "", -EINVAL, NULL);
418 test_dns_service_join_one("", "_http._tcp", "", -EINVAL, NULL);
419 test_dns_service_join_one("", "_http._tcp", "foo", -EINVAL, NULL);
420 test_dns_service_join_one("foo", "", "foo", -EINVAL, NULL);
421 test_dns_service_join_one("foo", "foo", "foo", -EINVAL, NULL);
422
423 test_dns_service_join_one("foo", "_http._tcp", "", 0, "foo._http._tcp");
424 test_dns_service_join_one(NULL, "_http._tcp", "", 0, "_http._tcp");
425 test_dns_service_join_one("foo", "_http._tcp", "foo", 0, "foo._http._tcp.foo");
426 test_dns_service_join_one(NULL, "_http._tcp", "foo", 0, "_http._tcp.foo");
427 test_dns_service_join_one("Lennart's PC", "_pc._tcp", "foo.bar.com", 0, "Lennart\\039s\\032PC._pc._tcp.foo.bar.com");
428 test_dns_service_join_one(NULL, "_pc._tcp", "foo.bar.com", 0, "_pc._tcp.foo.bar.com");
429}
430
431static void test_dns_service_split_one(const char *joined, const char *a, const char *b, const char *c, int r) {
432 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *t = NULL;
433
434 assert_se(dns_service_split(joined, &x, &y, &z) == r);
435 assert_se(streq_ptr(x, a));
436 assert_se(streq_ptr(y, b));
437 assert_se(streq_ptr(z, c));
438
439 if (r < 0)
440 return;
441
442 if (y) {
443 assert_se(dns_service_join(x, y, z, &t) == 0);
444 assert_se(streq_ptr(joined, t));
445 } else
446 assert_se(!x && streq_ptr(z, joined));
447}
448
449static void test_dns_service_split(void) {
450 test_dns_service_split_one("", NULL, NULL, "", 0);
451 test_dns_service_split_one("foo", NULL, NULL, "foo", 0);
452 test_dns_service_split_one("foo.bar", NULL, NULL, "foo.bar", 0);
453 test_dns_service_split_one("_foo.bar", NULL, NULL, "_foo.bar", 0);
454 test_dns_service_split_one("_foo._bar", NULL, "_foo._bar", "", 0);
455 test_dns_service_split_one("_meh._foo._bar", "_meh", "_foo._bar", "", 0);
456 test_dns_service_split_one("Wuff\\032Wuff._foo._bar.waldo.com", "Wuff Wuff", "_foo._bar", "waldo.com", 0);
0a49b6b6
LP
457}
458
58db254a
LP
459static void test_dns_name_change_suffix_one(const char *name, const char *old_suffix, const char *new_suffix, int r, const char *result) {
460 _cleanup_free_ char *s = NULL;
461
462 assert_se(dns_name_change_suffix(name, old_suffix, new_suffix, &s) == r);
463 assert_se(streq_ptr(s, result));
464}
465
466static void test_dns_name_change_suffix(void) {
467 test_dns_name_change_suffix_one("foo.bar", "bar", "waldo", 1, "foo.waldo");
468 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "foo.bar.waldi.quux", "piff.paff", 1, "piff.paff");
469 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "bar.waldi.quux", "piff.paff", 1, "foo.piff.paff");
470 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "waldi.quux", "piff.paff", 1, "foo.bar.piff.paff");
471 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "quux", "piff.paff", 1, "foo.bar.waldi.piff.paff");
472 test_dns_name_change_suffix_one("foo.bar.waldi.quux", "", "piff.paff", 1, "foo.bar.waldi.quux.piff.paff");
473 test_dns_name_change_suffix_one("", "", "piff.paff", 1, "piff.paff");
474 test_dns_name_change_suffix_one("", "", "", 1, "");
475 test_dns_name_change_suffix_one("a", "b", "c", 0, NULL);
476}
477
74b2466e
LP
478int main(int argc, char *argv[]) {
479
480 test_dns_label_unescape();
642900d3 481 test_dns_label_unescape_suffix();
74b2466e
LP
482 test_dns_label_escape();
483 test_dns_name_normalize();
484 test_dns_name_equal();
485 test_dns_name_endswith();
ae72b22c 486 test_dns_name_between();
dc477e73
LP
487 test_dns_name_is_root();
488 test_dns_name_is_single_label();
b914e211 489 test_dns_name_reverse();
9ca45586
LP
490 test_dns_name_concat();
491 test_dns_name_is_valid();
54adabf7 492 test_dns_name_to_wire_format();
0a49b6b6 493 test_dns_service_name_is_valid();
7e8131e9 494 test_dns_srv_type_is_valid();
0e8eedbb
LP
495 test_dns_service_join();
496 test_dns_service_split();
58db254a 497 test_dns_name_change_suffix();
74b2466e
LP
498
499 return 0;
500}