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