]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-dns-domain.c
util-lib: split our string related calls from util.[ch] into its own file string...
[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
4ad7f276 22#include "dns-domain.h"
07630cea
LP
23#include "macro.h"
24#include "string-util.h"
74b2466e
LP
25
26static void test_dns_label_unescape_one(const char *what, const char *expect, size_t buffer_sz, int ret) {
27 char buffer[buffer_sz];
28 int r;
29
30 r = dns_label_unescape(&what, buffer, buffer_sz);
31 assert_se(r == ret);
32
33 if (r < 0)
34 return;
35
36 assert_se(streq(buffer, expect));
37}
38
39static 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);
52}
53
642900d3
TG
54static 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];
56 const char *label;
57 int r;
58
59 label = what + strlen(what);
60
61 r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
62 assert_se(r == ret1);
63 if (r >= 0)
64 assert_se(streq(buffer, expect1));
65
66 r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
67 assert_se(r == ret2);
68 if (r >= 0)
69 assert_se(streq(buffer, expect2));
70}
71
72static 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);
92}
93
74b2466e
LP
94static void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) {
95 _cleanup_free_ char *t = NULL;
96 int r;
97
98 r = dns_label_escape(what, l, &t);
0c0cdb06 99 assert_se(r == ret);
74b2466e
LP
100
101 if (r < 0)
102 return;
103
104 assert_se(streq_ptr(expect, t));
105}
106
107static 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);
112}
113
114static void test_dns_name_normalize_one(const char *what, const char *expect, int ret) {
115 _cleanup_free_ char *t = NULL;
116 int r;
117
118 r = dns_name_normalize(what, &t);
119 assert_se(r == ret);
120
121 if (r < 0)
122 return;
123
124 assert_se(streq_ptr(expect, t));
125}
126
127static 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);
137}
138
139static void test_dns_name_equal_one(const char *a, const char *b, int ret) {
140 int r;
141
142 r = dns_name_equal(a, b);
143 assert_se(r == ret);
144
145 r = dns_name_equal(b, a);
146 assert_se(r == ret);
147}
148
149static 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);
162}
163
ae72b22c
TG
164static void test_dns_name_between_one(const char *a, const char *b, const char *c, int ret) {
165 int r;
166
167 r = dns_name_between(a, b, c);
168 assert_se(r == ret);
169
170 r = dns_name_between(c, b, a);
171 if (ret >= 0)
172 assert_se(r == 0);
173 else
174 assert_se(r == ret);
175}
176
177static 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);
190
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);
194}
195
74b2466e
LP
196static void test_dns_name_endswith_one(const char *a, const char *b, int ret) {
197 assert_se(dns_name_endswith(a, b) == ret);
198}
199
200static 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);
216}
217
218static 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);
224}
225
226static 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);
233}
234
b914e211
LP
235static void test_dns_name_reverse_one(const char *address, const char *name) {
236 _cleanup_free_ char *p = NULL;
a7f7d1bd 237 union in_addr_union a, b = {};
b914e211
LP
238 int familya, familyb;
239
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));
246}
247
248static 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");
9436e8ca
LP
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");
b914e211
LP
253}
254
9ca45586
LP
255static void test_dns_name_concat_one(const char *a, const char *b, int r, const char *result) {
256 _cleanup_free_ char *p = NULL;
257
258 assert_se(dns_name_concat(a, b, &p) == r);
259 assert_se(streq_ptr(p, result));
260}
261
262static 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");
267}
268
269static void test_dns_name_is_valid_one(const char *s, int ret) {
270 assert_se(dns_name_is_valid(s) == ret);
271}
272
273static 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);
286}
287
74b2466e
LP
288int main(int argc, char *argv[]) {
289
290 test_dns_label_unescape();
642900d3 291 test_dns_label_unescape_suffix();
74b2466e
LP
292 test_dns_label_escape();
293 test_dns_name_normalize();
294 test_dns_name_equal();
295 test_dns_name_endswith();
ae72b22c 296 test_dns_name_between();
74b2466e
LP
297 test_dns_name_root();
298 test_dns_name_single_label();
b914e211 299 test_dns_name_reverse();
9ca45586
LP
300 test_dns_name_concat();
301 test_dns_name_is_valid();
74b2466e
LP
302
303 return 0;
304}