chiark / gitweb /
sd-rtnl: introduce sd_rtnl_new_from_netlink
[elogind.git] / src / resolve / test-dns-domain.c
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
22 #include "log.h"
23 #include "macro.h"
24 #include "resolved-dns-domain.h"
25
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];
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
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);
52 }
53
54 static void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) {
55         _cleanup_free_ char *t = NULL;
56         int r;
57
58         r = dns_label_escape(what, l, &t);
59         assert_se(r == ret);
60
61         if (r < 0)
62                 return;
63
64         assert_se(streq_ptr(expect, t));
65 }
66
67 static void test_dns_label_escape(void) {
68         test_dns_label_escape_one("", 0, "", 0);
69         test_dns_label_escape_one("hallo", 5, "hallo", 5);
70         test_dns_label_escape_one("hallo", 6, NULL, -EINVAL);
71         test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31);
72 }
73
74 static void test_dns_name_normalize_one(const char *what, const char *expect, int ret) {
75         _cleanup_free_ char *t = NULL;
76         int r;
77
78         r = dns_name_normalize(what, &t);
79         assert_se(r == ret);
80
81         if (r < 0)
82                 return;
83
84         assert_se(streq_ptr(expect, t));
85 }
86
87 static void test_dns_name_normalize(void) {
88         test_dns_name_normalize_one("", "", 0);
89         test_dns_name_normalize_one("f", "f", 0);
90         test_dns_name_normalize_one("f.waldi", "f.waldi", 0);
91         test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0);
92         test_dns_name_normalize_one("\\000", NULL, -EINVAL);
93         test_dns_name_normalize_one("..", NULL, -EINVAL);
94         test_dns_name_normalize_one(".foobar", NULL, -EINVAL);
95         test_dns_name_normalize_one("foobar.", "foobar", 0);
96         test_dns_name_normalize_one(".", "", 0);
97 }
98
99 static void test_dns_name_equal_one(const char *a, const char *b, int ret) {
100         int r;
101
102         r = dns_name_equal(a, b);
103         assert_se(r == ret);
104
105         r = dns_name_equal(b, a);
106         assert_se(r == ret);
107 }
108
109 static void test_dns_name_equal(void) {
110         test_dns_name_equal_one("", "", true);
111         test_dns_name_equal_one("x", "x", true);
112         test_dns_name_equal_one("x", "x.", true);
113         test_dns_name_equal_one("abc.def", "abc.def", true);
114         test_dns_name_equal_one("abc.def", "ABC.def", true);
115         test_dns_name_equal_one("abc.def", "CBA.def", false);
116         test_dns_name_equal_one("", "xxx", false);
117         test_dns_name_equal_one("ab", "a", false);
118         test_dns_name_equal_one("\\000", "xxxx", -EINVAL);
119         test_dns_name_equal_one(".", "", true);
120         test_dns_name_equal_one(".", ".", true);
121         test_dns_name_equal_one("..", "..", -EINVAL);
122 }
123
124 static void test_dns_name_endswith_one(const char *a, const char *b, int ret) {
125         assert_se(dns_name_endswith(a, b) == ret);
126 }
127
128 static void test_dns_name_endswith(void) {
129         test_dns_name_endswith_one("", "", true);
130         test_dns_name_endswith_one("", "xxx", false);
131         test_dns_name_endswith_one("xxx", "", true);
132         test_dns_name_endswith_one("x", "x", true);
133         test_dns_name_endswith_one("x", "y", false);
134         test_dns_name_endswith_one("x.y", "y", true);
135         test_dns_name_endswith_one("x.y", "Y", true);
136         test_dns_name_endswith_one("x.y", "x", false);
137         test_dns_name_endswith_one("x.y.z", "Z", true);
138         test_dns_name_endswith_one("x.y.z", "y.Z", true);
139         test_dns_name_endswith_one("x.y.z", "x.y.Z", true);
140         test_dns_name_endswith_one("x.y.z", "waldo", false);
141         test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false);
142         test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true);
143         test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL);
144 }
145
146 static void test_dns_name_root(void) {
147         assert_se(dns_name_root("") == true);
148         assert_se(dns_name_root(".") == true);
149         assert_se(dns_name_root("xxx") == false);
150         assert_se(dns_name_root("xxx.") == false);
151         assert_se(dns_name_root("..") == -EINVAL);
152 }
153
154 static void test_dns_name_single_label(void) {
155         assert_se(dns_name_single_label("") == false);
156         assert_se(dns_name_single_label(".") == false);
157         assert_se(dns_name_single_label("..") == -EINVAL);
158         assert_se(dns_name_single_label("x") == true);
159         assert_se(dns_name_single_label("x.") == true);
160         assert_se(dns_name_single_label("xx.yy") == false);
161 }
162
163 static void test_dns_name_reverse_one(const char *address, const char *name) {
164         _cleanup_free_ char *p = NULL;
165         union in_addr_union a, b;
166         int familya, familyb;
167
168         assert_se(in_addr_from_string_auto(address, &familya, &a) >= 0);
169         assert_se(dns_name_reverse(familya, &a, &p) >= 0);
170         assert_se(streq(p, name));
171         assert_se(dns_name_address(p, &familyb, &b) > 0);
172         assert_se(familya == familyb);
173         assert_se(in_addr_equal(familya, &a, &b));
174 }
175
176 static void test_dns_name_reverse(void) {
177         test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa");
178         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");
179 }
180
181 int main(int argc, char *argv[]) {
182
183         test_dns_label_unescape();
184         test_dns_label_escape();
185         test_dns_name_normalize();
186         test_dns_name_equal();
187         test_dns_name_endswith();
188         test_dns_name_root();
189         test_dns_name_single_label();
190         test_dns_name_reverse();
191
192         return 0;
193 }