]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/test-dns-domain.c
nss-myhostname: don't include assert.h twice
[thirdparty/systemd.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 "resolved-dns-domain.h"
24
25 static void test_dns_label_unescape_one(const char *what, const char *expect, size_t buffer_sz, int ret) {
26 char buffer[buffer_sz];
27 int r;
28
29 r = dns_label_unescape(&what, buffer, buffer_sz);
30 assert_se(r == ret);
31
32 if (r < 0)
33 return;
34
35 assert_se(streq(buffer, expect));
36 }
37
38 static void test_dns_label_unescape(void) {
39 test_dns_label_unescape_one("hallo", "hallo", 6, 5);
40 test_dns_label_unescape_one("hallo", "hallo", 4, -ENOSPC);
41 test_dns_label_unescape_one("", "", 10, 0);
42 test_dns_label_unescape_one("hallo\\.foobar", "hallo.foobar", 20, 12);
43 test_dns_label_unescape_one("hallo.foobar", "hallo", 10, 5);
44 test_dns_label_unescape_one("hallo\n.foobar", "hallo", 20, -EINVAL);
45 test_dns_label_unescape_one("hallo\\", "hallo", 20, -EINVAL);
46 test_dns_label_unescape_one("hallo\\032 ", "hallo ", 20, 7);
47 test_dns_label_unescape_one(".", "", 20, 0);
48 test_dns_label_unescape_one("..", "", 20, -EINVAL);
49 test_dns_label_unescape_one(".foobar", "", 20, -EINVAL);
50 test_dns_label_unescape_one("foobar.", "foobar", 20, 6);
51 }
52
53 static void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) {
54 _cleanup_free_ char *t = NULL;
55 int r;
56
57 r = dns_label_escape(what, l, &t);
58 assert(r == ret);
59
60 if (r < 0)
61 return;
62
63 assert_se(streq_ptr(expect, t));
64 }
65
66 static void test_dns_label_escape(void) {
67 test_dns_label_escape_one("", 0, "", 0);
68 test_dns_label_escape_one("hallo", 5, "hallo", 5);
69 test_dns_label_escape_one("hallo", 6, NULL, -EINVAL);
70 test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31);
71 }
72
73 static void test_dns_name_normalize_one(const char *what, const char *expect, int ret) {
74 _cleanup_free_ char *t = NULL;
75 int r;
76
77 r = dns_name_normalize(what, &t);
78 assert_se(r == ret);
79
80 if (r < 0)
81 return;
82
83 assert_se(streq_ptr(expect, t));
84 }
85
86 static void test_dns_name_normalize(void) {
87 test_dns_name_normalize_one("", "", 0);
88 test_dns_name_normalize_one("f", "f", 0);
89 test_dns_name_normalize_one("f.waldi", "f.waldi", 0);
90 test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0);
91 test_dns_name_normalize_one("\\000", NULL, -EINVAL);
92 test_dns_name_normalize_one("..", NULL, -EINVAL);
93 test_dns_name_normalize_one(".foobar", NULL, -EINVAL);
94 test_dns_name_normalize_one("foobar.", "foobar", 0);
95 test_dns_name_normalize_one(".", "", 0);
96 }
97
98 static void test_dns_name_equal_one(const char *a, const char *b, int ret) {
99 int r;
100
101 r = dns_name_equal(a, b);
102 assert_se(r == ret);
103
104 r = dns_name_equal(b, a);
105 assert_se(r == ret);
106 }
107
108 static void test_dns_name_equal(void) {
109 test_dns_name_equal_one("", "", true);
110 test_dns_name_equal_one("x", "x", true);
111 test_dns_name_equal_one("x", "x.", true);
112 test_dns_name_equal_one("abc.def", "abc.def", true);
113 test_dns_name_equal_one("abc.def", "ABC.def", true);
114 test_dns_name_equal_one("abc.def", "CBA.def", false);
115 test_dns_name_equal_one("", "xxx", false);
116 test_dns_name_equal_one("ab", "a", false);
117 test_dns_name_equal_one("\\000", "xxxx", -EINVAL);
118 test_dns_name_equal_one(".", "", true);
119 test_dns_name_equal_one(".", ".", true);
120 test_dns_name_equal_one("..", "..", -EINVAL);
121 }
122
123 static void test_dns_name_endswith_one(const char *a, const char *b, int ret) {
124 assert_se(dns_name_endswith(a, b) == ret);
125 }
126
127 static void test_dns_name_endswith(void) {
128 test_dns_name_endswith_one("", "", true);
129 test_dns_name_endswith_one("", "xxx", false);
130 test_dns_name_endswith_one("xxx", "", true);
131 test_dns_name_endswith_one("x", "x", true);
132 test_dns_name_endswith_one("x", "y", false);
133 test_dns_name_endswith_one("x.y", "y", true);
134 test_dns_name_endswith_one("x.y", "Y", true);
135 test_dns_name_endswith_one("x.y", "x", false);
136 test_dns_name_endswith_one("x.y.z", "Z", true);
137 test_dns_name_endswith_one("x.y.z", "y.Z", true);
138 test_dns_name_endswith_one("x.y.z", "x.y.Z", true);
139 test_dns_name_endswith_one("x.y.z", "waldo", false);
140 test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false);
141 test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true);
142 test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL);
143 }
144
145 static void test_dns_name_root(void) {
146 assert_se(dns_name_root("") == true);
147 assert_se(dns_name_root(".") == true);
148 assert_se(dns_name_root("xxx") == false);
149 assert_se(dns_name_root("xxx.") == false);
150 assert_se(dns_name_root("..") == -EINVAL);
151 }
152
153 static void test_dns_name_single_label(void) {
154 assert_se(dns_name_single_label("") == false);
155 assert_se(dns_name_single_label(".") == false);
156 assert_se(dns_name_single_label("..") == -EINVAL);
157 assert_se(dns_name_single_label("x") == true);
158 assert_se(dns_name_single_label("x.") == true);
159 assert_se(dns_name_single_label("xx.yy") == false);
160 }
161
162 int main(int argc, char *argv[]) {
163
164 test_dns_label_unescape();
165 test_dns_label_escape();
166 test_dns_name_normalize();
167 test_dns_name_equal();
168 test_dns_name_endswith();
169 test_dns_name_root();
170 test_dns_name_single_label();
171
172 return 0;
173 }