]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-dns-domain.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
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.
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.
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/>.
22 #include "dns-domain.h"
24 #include "string-util.h"
26 static void test_dns_label_unescape_one(const char *what
, const char *expect
, size_t buffer_sz
, int ret
) {
27 char buffer
[buffer_sz
];
30 r
= dns_label_unescape(&what
, buffer
, buffer_sz
);
36 assert_se(streq(buffer
, expect
));
39 static void test_dns_label_unescape(void) {
40 test_dns_label_unescape_one("hallo", "hallo", 6, 5);
41 test_dns_label_unescape_one("hallo", "hallo", 4, -ENOSPC
);
42 test_dns_label_unescape_one("", "", 10, 0);
43 test_dns_label_unescape_one("hallo\\.foobar", "hallo.foobar", 20, 12);
44 test_dns_label_unescape_one("hallo.foobar", "hallo", 10, 5);
45 test_dns_label_unescape_one("hallo\n.foobar", "hallo", 20, -EINVAL
);
46 test_dns_label_unescape_one("hallo\\", "hallo", 20, -EINVAL
);
47 test_dns_label_unescape_one("hallo\\032 ", "hallo ", 20, 7);
48 test_dns_label_unescape_one(".", "", 20, 0);
49 test_dns_label_unescape_one("..", "", 20, -EINVAL
);
50 test_dns_label_unescape_one(".foobar", "", 20, -EINVAL
);
51 test_dns_label_unescape_one("foobar.", "foobar", 20, 6);
54 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
) {
55 char buffer
[buffer_sz
];
59 label
= what
+ strlen(what
);
61 r
= dns_label_unescape_suffix(what
, &label
, buffer
, buffer_sz
);
64 assert_se(streq(buffer
, expect1
));
66 r
= dns_label_unescape_suffix(what
, &label
, buffer
, buffer_sz
);
69 assert_se(streq(buffer
, expect2
));
72 static void test_dns_label_unescape_suffix(void) {
73 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0);
74 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOSPC
, -ENOSPC
);
75 test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0);
76 test_dns_label_unescape_suffix_one("hallo\\.foobar", "hallo.foobar", "", 20, 12, 0);
77 test_dns_label_unescape_suffix_one("hallo.foobar", "foobar", "hallo", 10, 6, 5);
78 test_dns_label_unescape_suffix_one("hallo.foobar\n", "foobar", "foobar", 20, -EINVAL
, -EINVAL
);
79 test_dns_label_unescape_suffix_one("hallo\\", "hallo", "hallo", 20, -EINVAL
, -EINVAL
);
80 test_dns_label_unescape_suffix_one("hallo\\032 ", "hallo ", "", 20, 7, 0);
81 test_dns_label_unescape_suffix_one(".", "", "", 20, 0, 0);
82 test_dns_label_unescape_suffix_one("..", "", "", 20, 0, 0);
83 test_dns_label_unescape_suffix_one(".foobar", "foobar", "", 20, 6, -EINVAL
);
84 test_dns_label_unescape_suffix_one("foobar.", "", "foobar", 20, 0, 6);
85 test_dns_label_unescape_suffix_one("foo\\\\bar", "foo\\bar", "", 20, 7, 0);
86 test_dns_label_unescape_suffix_one("foo.bar", "bar", "foo", 20, 3, 3);
87 test_dns_label_unescape_suffix_one("foo..bar", "bar", "", 20, 3, -EINVAL
);
88 test_dns_label_unescape_suffix_one("foo...bar", "bar", "", 20, 3, -EINVAL
);
89 test_dns_label_unescape_suffix_one("foo\\.bar", "foo.bar", "", 20, 7, 0);
90 test_dns_label_unescape_suffix_one("foo\\\\.bar", "bar", "foo\\", 20, 3, 4);
91 test_dns_label_unescape_suffix_one("foo\\\\\\.bar", "foo\\.bar", "", 20, 8, 0);
94 static void test_dns_label_escape_one(const char *what
, size_t l
, const char *expect
, int ret
) {
95 _cleanup_free_
char *t
= NULL
;
98 r
= dns_label_escape(what
, l
, &t
);
104 assert_se(streq_ptr(expect
, t
));
107 static void test_dns_label_escape(void) {
108 test_dns_label_escape_one("", 0, "", 0);
109 test_dns_label_escape_one("hallo", 5, "hallo", 5);
110 test_dns_label_escape_one("hallo", 6, NULL
, -EINVAL
);
111 test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31);
114 static void test_dns_name_normalize_one(const char *what
, const char *expect
, int ret
) {
115 _cleanup_free_
char *t
= NULL
;
118 r
= dns_name_normalize(what
, &t
);
124 assert_se(streq_ptr(expect
, t
));
127 static void test_dns_name_normalize(void) {
128 test_dns_name_normalize_one("", "", 0);
129 test_dns_name_normalize_one("f", "f", 0);
130 test_dns_name_normalize_one("f.waldi", "f.waldi", 0);
131 test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0);
132 test_dns_name_normalize_one("\\000", NULL
, -EINVAL
);
133 test_dns_name_normalize_one("..", NULL
, -EINVAL
);
134 test_dns_name_normalize_one(".foobar", NULL
, -EINVAL
);
135 test_dns_name_normalize_one("foobar.", "foobar", 0);
136 test_dns_name_normalize_one(".", "", 0);
139 static void test_dns_name_equal_one(const char *a
, const char *b
, int ret
) {
142 r
= dns_name_equal(a
, b
);
145 r
= dns_name_equal(b
, a
);
149 static void test_dns_name_equal(void) {
150 test_dns_name_equal_one("", "", true);
151 test_dns_name_equal_one("x", "x", true);
152 test_dns_name_equal_one("x", "x.", true);
153 test_dns_name_equal_one("abc.def", "abc.def", true);
154 test_dns_name_equal_one("abc.def", "ABC.def", true);
155 test_dns_name_equal_one("abc.def", "CBA.def", false);
156 test_dns_name_equal_one("", "xxx", false);
157 test_dns_name_equal_one("ab", "a", false);
158 test_dns_name_equal_one("\\000", "xxxx", -EINVAL
);
159 test_dns_name_equal_one(".", "", true);
160 test_dns_name_equal_one(".", ".", true);
161 test_dns_name_equal_one("..", "..", -EINVAL
);
164 static void test_dns_name_between_one(const char *a
, const char *b
, const char *c
, int ret
) {
167 r
= dns_name_between(a
, b
, c
);
170 r
= dns_name_between(c
, b
, a
);
177 static void test_dns_name_between(void) {
178 /* see https://tools.ietf.org/html/rfc4034#section-6.1
179 Note that we use "\033.z.example" in stead of "\001.z.example" as we
180 consider the latter invalid */
181 test_dns_name_between_one("example", "a.example", "yljkjljk.a.example", true);
182 test_dns_name_between_one("a.example", "yljkjljk.a.example", "Z.a.example", true);
183 test_dns_name_between_one("yljkjljk.a.example", "Z.a.example", "zABC.a.EXAMPLE", true);
184 test_dns_name_between_one("Z.a.example", "zABC.a.EXAMPLE", "z.example", true);
185 test_dns_name_between_one("zABC.a.EXAMPLE", "z.example", "\\033.z.example", true);
186 test_dns_name_between_one("z.example", "\\033.z.example", "*.z.example", true);
187 test_dns_name_between_one("\\033.z.example", "*.z.example", "\\200.z.example", true);
188 test_dns_name_between_one("*.z.example", "\\200.z.example", "example", true);
189 test_dns_name_between_one("\\200.z.example", "example", "a.example", true);
191 test_dns_name_between_one("example", "a.example", "example", -EINVAL
);
192 test_dns_name_between_one("example", "example", "yljkjljk.a.example", false);
193 test_dns_name_between_one("example", "yljkjljk.a.example", "yljkjljk.a.example", false);
196 static void test_dns_name_endswith_one(const char *a
, const char *b
, int ret
) {
197 assert_se(dns_name_endswith(a
, b
) == ret
);
200 static void test_dns_name_endswith(void) {
201 test_dns_name_endswith_one("", "", true);
202 test_dns_name_endswith_one("", "xxx", false);
203 test_dns_name_endswith_one("xxx", "", true);
204 test_dns_name_endswith_one("x", "x", true);
205 test_dns_name_endswith_one("x", "y", false);
206 test_dns_name_endswith_one("x.y", "y", true);
207 test_dns_name_endswith_one("x.y", "Y", true);
208 test_dns_name_endswith_one("x.y", "x", false);
209 test_dns_name_endswith_one("x.y.z", "Z", true);
210 test_dns_name_endswith_one("x.y.z", "y.Z", true);
211 test_dns_name_endswith_one("x.y.z", "x.y.Z", true);
212 test_dns_name_endswith_one("x.y.z", "waldo", false);
213 test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false);
214 test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true);
215 test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL
);
218 static void test_dns_name_root(void) {
219 assert_se(dns_name_root("") == true);
220 assert_se(dns_name_root(".") == true);
221 assert_se(dns_name_root("xxx") == false);
222 assert_se(dns_name_root("xxx.") == false);
223 assert_se(dns_name_root("..") == -EINVAL
);
226 static void test_dns_name_single_label(void) {
227 assert_se(dns_name_single_label("") == false);
228 assert_se(dns_name_single_label(".") == false);
229 assert_se(dns_name_single_label("..") == -EINVAL
);
230 assert_se(dns_name_single_label("x") == true);
231 assert_se(dns_name_single_label("x.") == true);
232 assert_se(dns_name_single_label("xx.yy") == false);
235 static void test_dns_name_reverse_one(const char *address
, const char *name
) {
236 _cleanup_free_
char *p
= NULL
;
237 union in_addr_union a
, b
= {};
238 int familya
, familyb
;
240 assert_se(in_addr_from_string_auto(address
, &familya
, &a
) >= 0);
241 assert_se(dns_name_reverse(familya
, &a
, &p
) >= 0);
242 assert_se(streq(p
, name
));
243 assert_se(dns_name_address(p
, &familyb
, &b
) > 0);
244 assert_se(familya
== familyb
);
245 assert_se(in_addr_equal(familya
, &a
, &b
));
248 static void test_dns_name_reverse(void) {
249 test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa");
250 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");
251 test_dns_name_reverse_one("127.0.0.1", "1.0.0.127.in-addr.arpa");
252 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");
255 static void test_dns_name_concat_one(const char *a
, const char *b
, int r
, const char *result
) {
256 _cleanup_free_
char *p
= NULL
;
258 assert_se(dns_name_concat(a
, b
, &p
) == r
);
259 assert_se(streq_ptr(p
, result
));
262 static void test_dns_name_concat(void) {
263 test_dns_name_concat_one("foo", "bar", 0, "foo.bar");
264 test_dns_name_concat_one("foo.foo", "bar.bar", 0, "foo.foo.bar.bar");
265 test_dns_name_concat_one("foo", NULL
, 0, "foo");
266 test_dns_name_concat_one("foo.", "bar.", 0, "foo.bar");
269 static void test_dns_name_is_valid_one(const char *s
, int ret
) {
270 assert_se(dns_name_is_valid(s
) == ret
);
273 static void test_dns_name_is_valid(void) {
274 test_dns_name_is_valid_one("foo", 1);
275 test_dns_name_is_valid_one("foo.", 1);
276 test_dns_name_is_valid_one("Foo", 1);
277 test_dns_name_is_valid_one("foo.bar", 1);
278 test_dns_name_is_valid_one("foo.bar.baz", 1);
279 test_dns_name_is_valid_one("", 1);
280 test_dns_name_is_valid_one("foo..bar", 0);
281 test_dns_name_is_valid_one(".foo.bar", 0);
282 test_dns_name_is_valid_one("foo.bar.", 1);
283 test_dns_name_is_valid_one("\\zbar", 0);
284 test_dns_name_is_valid_one("รค", 1);
285 test_dns_name_is_valid_one("\n", 0);
288 int main(int argc
, char *argv
[]) {
290 test_dns_label_unescape();
291 test_dns_label_unescape_suffix();
292 test_dns_label_escape();
293 test_dns_name_normalize();
294 test_dns_name_equal();
295 test_dns_name_endswith();
296 test_dns_name_between();
297 test_dns_name_root();
298 test_dns_name_single_label();
299 test_dns_name_reverse();
300 test_dns_name_concat();
301 test_dns_name_is_valid();