]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-dns-domain.c
Merge pull request #1909 from keszybz/filetriggers-v2
[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);
42 test_dns_label_unescape_one("hallo", "hallo", 4, -ENOSPC);
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
642900d3
TG
55static void test_dns_label_unescape_suffix_one(const char *what, const char *expect1, const char *expect2, size_t buffer_sz, int ret1, int ret2) {
56 char buffer[buffer_sz];
57 const char *label;
58 int r;
59
60 label = what + strlen(what);
61
62 r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
63 assert_se(r == ret1);
64 if (r >= 0)
65 assert_se(streq(buffer, expect1));
66
67 r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
68 assert_se(r == ret2);
69 if (r >= 0)
70 assert_se(streq(buffer, expect2));
71}
72
73static void test_dns_label_unescape_suffix(void) {
74 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0);
75 test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOSPC, -ENOSPC);
76 test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0);
77 test_dns_label_unescape_suffix_one("hallo\\.foobar", "hallo.foobar", "", 20, 12, 0);
78 test_dns_label_unescape_suffix_one("hallo.foobar", "foobar", "hallo", 10, 6, 5);
79 test_dns_label_unescape_suffix_one("hallo.foobar\n", "foobar", "foobar", 20, -EINVAL, -EINVAL);
80 test_dns_label_unescape_suffix_one("hallo\\", "hallo", "hallo", 20, -EINVAL, -EINVAL);
81 test_dns_label_unescape_suffix_one("hallo\\032 ", "hallo ", "", 20, 7, 0);
82 test_dns_label_unescape_suffix_one(".", "", "", 20, 0, 0);
83 test_dns_label_unescape_suffix_one("..", "", "", 20, 0, 0);
84 test_dns_label_unescape_suffix_one(".foobar", "foobar", "", 20, 6, -EINVAL);
85 test_dns_label_unescape_suffix_one("foobar.", "", "foobar", 20, 0, 6);
86 test_dns_label_unescape_suffix_one("foo\\\\bar", "foo\\bar", "", 20, 7, 0);
87 test_dns_label_unescape_suffix_one("foo.bar", "bar", "foo", 20, 3, 3);
88 test_dns_label_unescape_suffix_one("foo..bar", "bar", "", 20, 3, -EINVAL);
89 test_dns_label_unescape_suffix_one("foo...bar", "bar", "", 20, 3, -EINVAL);
90 test_dns_label_unescape_suffix_one("foo\\.bar", "foo.bar", "", 20, 7, 0);
91 test_dns_label_unescape_suffix_one("foo\\\\.bar", "bar", "foo\\", 20, 3, 4);
92 test_dns_label_unescape_suffix_one("foo\\\\\\.bar", "foo\\.bar", "", 20, 8, 0);
93}
94
74b2466e
LP
95static void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) {
96 _cleanup_free_ char *t = NULL;
97 int r;
98
99 r = dns_label_escape(what, l, &t);
0c0cdb06 100 assert_se(r == ret);
74b2466e
LP
101
102 if (r < 0)
103 return;
104
105 assert_se(streq_ptr(expect, t));
106}
107
108static void test_dns_label_escape(void) {
109 test_dns_label_escape_one("", 0, "", 0);
110 test_dns_label_escape_one("hallo", 5, "hallo", 5);
111 test_dns_label_escape_one("hallo", 6, NULL, -EINVAL);
112 test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31);
113}
114
115static void test_dns_name_normalize_one(const char *what, const char *expect, int ret) {
116 _cleanup_free_ char *t = NULL;
117 int r;
118
119 r = dns_name_normalize(what, &t);
120 assert_se(r == ret);
121
122 if (r < 0)
123 return;
124
125 assert_se(streq_ptr(expect, t));
126}
127
128static void test_dns_name_normalize(void) {
129 test_dns_name_normalize_one("", "", 0);
130 test_dns_name_normalize_one("f", "f", 0);
131 test_dns_name_normalize_one("f.waldi", "f.waldi", 0);
132 test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0);
133 test_dns_name_normalize_one("\\000", NULL, -EINVAL);
134 test_dns_name_normalize_one("..", NULL, -EINVAL);
135 test_dns_name_normalize_one(".foobar", NULL, -EINVAL);
136 test_dns_name_normalize_one("foobar.", "foobar", 0);
137 test_dns_name_normalize_one(".", "", 0);
138}
139
140static void test_dns_name_equal_one(const char *a, const char *b, int ret) {
141 int r;
142
143 r = dns_name_equal(a, b);
144 assert_se(r == ret);
145
146 r = dns_name_equal(b, a);
147 assert_se(r == ret);
148}
149
150static void test_dns_name_equal(void) {
151 test_dns_name_equal_one("", "", true);
152 test_dns_name_equal_one("x", "x", true);
153 test_dns_name_equal_one("x", "x.", true);
154 test_dns_name_equal_one("abc.def", "abc.def", true);
155 test_dns_name_equal_one("abc.def", "ABC.def", true);
156 test_dns_name_equal_one("abc.def", "CBA.def", false);
157 test_dns_name_equal_one("", "xxx", false);
158 test_dns_name_equal_one("ab", "a", false);
159 test_dns_name_equal_one("\\000", "xxxx", -EINVAL);
160 test_dns_name_equal_one(".", "", true);
161 test_dns_name_equal_one(".", ".", true);
162 test_dns_name_equal_one("..", "..", -EINVAL);
163}
164
ae72b22c
TG
165static void test_dns_name_between_one(const char *a, const char *b, const char *c, int ret) {
166 int r;
167
168 r = dns_name_between(a, b, c);
169 assert_se(r == ret);
170
171 r = dns_name_between(c, b, a);
172 if (ret >= 0)
173 assert_se(r == 0);
174 else
175 assert_se(r == ret);
176}
177
178static void test_dns_name_between(void) {
179 /* see https://tools.ietf.org/html/rfc4034#section-6.1
180 Note that we use "\033.z.example" in stead of "\001.z.example" as we
181 consider the latter invalid */
182 test_dns_name_between_one("example", "a.example", "yljkjljk.a.example", true);
183 test_dns_name_between_one("a.example", "yljkjljk.a.example", "Z.a.example", true);
184 test_dns_name_between_one("yljkjljk.a.example", "Z.a.example", "zABC.a.EXAMPLE", true);
185 test_dns_name_between_one("Z.a.example", "zABC.a.EXAMPLE", "z.example", true);
186 test_dns_name_between_one("zABC.a.EXAMPLE", "z.example", "\\033.z.example", true);
187 test_dns_name_between_one("z.example", "\\033.z.example", "*.z.example", true);
188 test_dns_name_between_one("\\033.z.example", "*.z.example", "\\200.z.example", true);
189 test_dns_name_between_one("*.z.example", "\\200.z.example", "example", true);
190 test_dns_name_between_one("\\200.z.example", "example", "a.example", true);
191
192 test_dns_name_between_one("example", "a.example", "example", -EINVAL);
193 test_dns_name_between_one("example", "example", "yljkjljk.a.example", false);
194 test_dns_name_between_one("example", "yljkjljk.a.example", "yljkjljk.a.example", false);
195}
196
74b2466e
LP
197static void test_dns_name_endswith_one(const char *a, const char *b, int ret) {
198 assert_se(dns_name_endswith(a, b) == ret);
199}
200
201static void test_dns_name_endswith(void) {
202 test_dns_name_endswith_one("", "", true);
203 test_dns_name_endswith_one("", "xxx", false);
204 test_dns_name_endswith_one("xxx", "", true);
205 test_dns_name_endswith_one("x", "x", true);
206 test_dns_name_endswith_one("x", "y", false);
207 test_dns_name_endswith_one("x.y", "y", true);
208 test_dns_name_endswith_one("x.y", "Y", true);
209 test_dns_name_endswith_one("x.y", "x", false);
210 test_dns_name_endswith_one("x.y.z", "Z", true);
211 test_dns_name_endswith_one("x.y.z", "y.Z", true);
212 test_dns_name_endswith_one("x.y.z", "x.y.Z", true);
213 test_dns_name_endswith_one("x.y.z", "waldo", false);
214 test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false);
215 test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true);
216 test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL);
217}
218
219static void test_dns_name_root(void) {
220 assert_se(dns_name_root("") == true);
221 assert_se(dns_name_root(".") == true);
222 assert_se(dns_name_root("xxx") == false);
223 assert_se(dns_name_root("xxx.") == false);
224 assert_se(dns_name_root("..") == -EINVAL);
225}
226
227static void test_dns_name_single_label(void) {
228 assert_se(dns_name_single_label("") == false);
229 assert_se(dns_name_single_label(".") == false);
230 assert_se(dns_name_single_label("..") == -EINVAL);
231 assert_se(dns_name_single_label("x") == true);
232 assert_se(dns_name_single_label("x.") == true);
233 assert_se(dns_name_single_label("xx.yy") == false);
234}
235
b914e211
LP
236static void test_dns_name_reverse_one(const char *address, const char *name) {
237 _cleanup_free_ char *p = NULL;
a7f7d1bd 238 union in_addr_union a, b = {};
b914e211
LP
239 int familya, familyb;
240
241 assert_se(in_addr_from_string_auto(address, &familya, &a) >= 0);
242 assert_se(dns_name_reverse(familya, &a, &p) >= 0);
243 assert_se(streq(p, name));
244 assert_se(dns_name_address(p, &familyb, &b) > 0);
245 assert_se(familya == familyb);
246 assert_se(in_addr_equal(familya, &a, &b));
247}
248
249static void test_dns_name_reverse(void) {
250 test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa");
251 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
252 test_dns_name_reverse_one("127.0.0.1", "1.0.0.127.in-addr.arpa");
253 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
254}
255
9ca45586
LP
256static void test_dns_name_concat_one(const char *a, const char *b, int r, const char *result) {
257 _cleanup_free_ char *p = NULL;
258
259 assert_se(dns_name_concat(a, b, &p) == r);
260 assert_se(streq_ptr(p, result));
261}
262
263static void test_dns_name_concat(void) {
264 test_dns_name_concat_one("foo", "bar", 0, "foo.bar");
265 test_dns_name_concat_one("foo.foo", "bar.bar", 0, "foo.foo.bar.bar");
266 test_dns_name_concat_one("foo", NULL, 0, "foo");
267 test_dns_name_concat_one("foo.", "bar.", 0, "foo.bar");
268}
269
270static void test_dns_name_is_valid_one(const char *s, int ret) {
271 assert_se(dns_name_is_valid(s) == ret);
272}
273
274static void test_dns_name_is_valid(void) {
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", 1);
278 test_dns_name_is_valid_one("foo.bar", 1);
279 test_dns_name_is_valid_one("foo.bar.baz", 1);
280 test_dns_name_is_valid_one("", 1);
281 test_dns_name_is_valid_one("foo..bar", 0);
282 test_dns_name_is_valid_one(".foo.bar", 0);
283 test_dns_name_is_valid_one("foo.bar.", 1);
284 test_dns_name_is_valid_one("\\zbar", 0);
285 test_dns_name_is_valid_one("รค", 1);
286 test_dns_name_is_valid_one("\n", 0);
287}
288
74b2466e
LP
289int main(int argc, char *argv[]) {
290
291 test_dns_label_unescape();
642900d3 292 test_dns_label_unescape_suffix();
74b2466e
LP
293 test_dns_label_escape();
294 test_dns_name_normalize();
295 test_dns_name_equal();
296 test_dns_name_endswith();
ae72b22c 297 test_dns_name_between();
74b2466e
LP
298 test_dns_name_root();
299 test_dns_name_single_label();
b914e211 300 test_dns_name_reverse();
9ca45586
LP
301 test_dns_name_concat();
302 test_dns_name_is_valid();
74b2466e
LP
303
304 return 0;
305}