]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-socket-util.c
nss-myhostname: move local address listing logic into shared, so that we can make...
[thirdparty/systemd.git] / src / test / test-socket-util.c
CommitLineData
c182135d
RC
1/***
2 This file is part of systemd
3
4 Copyright 2014 Ronny Chevalier
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include "socket-util.h"
21#include "util.h"
22#include "macro.h"
059f6c42 23#include "log.h"
c182135d
RC
24
25static void test_socket_address_parse(void) {
26 SocketAddress a;
27
28 assert_se(socket_address_parse(&a, "junk") < 0);
29 assert_se(socket_address_parse(&a, "192.168.1.1") < 0);
30 assert_se(socket_address_parse(&a, ".168.1.1") < 0);
31 assert_se(socket_address_parse(&a, "989.168.1.1") < 0);
32 assert_se(socket_address_parse(&a, "192.168.1.1:65536") < 0);
33 assert_se(socket_address_parse(&a, "192.168.1.1:0") < 0);
34 assert_se(socket_address_parse(&a, "0") < 0);
35 assert_se(socket_address_parse(&a, "65536") < 0);
36
37 assert_se(socket_address_parse(&a, "65535") >= 0);
38
39 if (socket_ipv6_is_supported()) {
40 assert_se(socket_address_parse(&a, "[::1]") < 0);
41 assert_se(socket_address_parse(&a, "[::1]8888") < 0);
42 assert_se(socket_address_parse(&a, "::1") < 0);
43 assert_se(socket_address_parse(&a, "[::1]:0") < 0);
44 assert_se(socket_address_parse(&a, "[::1]:65536") < 0);
45 assert_se(socket_address_parse(&a, "[a:b:1]:8888") < 0);
46
47 assert_se(socket_address_parse(&a, "8888") >= 0);
48 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
49
50 assert_se(socket_address_parse(&a, "[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:8888") >= 0);
51 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
52
53 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
54 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
55 } else {
56 assert_se(socket_address_parse(&a, "[::1]:8888") < 0);
57
58 assert_se(socket_address_parse(&a, "8888") >= 0);
59 assert_se(a.sockaddr.sa.sa_family == AF_INET);
60 }
61
62 assert_se(socket_address_parse(&a, "192.168.1.254:8888") >= 0);
63 assert_se(a.sockaddr.sa.sa_family == AF_INET);
64
65 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
66 assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
67
68 assert_se(socket_address_parse(&a, "@abstract") >= 0);
69 assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
70}
71
72static void test_socket_address_parse_netlink(void) {
73 SocketAddress a;
74
75 assert_se(socket_address_parse_netlink(&a, "junk") < 0);
76 assert_se(socket_address_parse_netlink(&a, "") < 0);
77
78 assert_se(socket_address_parse_netlink(&a, "route") >= 0);
79 assert_se(socket_address_parse_netlink(&a, "route 10") >= 0);
80 assert_se(a.sockaddr.sa.sa_family == AF_NETLINK);
81 assert_se(a.protocol == NETLINK_ROUTE);
82}
83
84static void test_socket_address_equal(void) {
85 SocketAddress a;
86 SocketAddress b;
87
88 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
89 assert_se(socket_address_parse(&b, "192.168.1.1:888") >= 0);
90 assert_se(!socket_address_equal(&a, &b));
91
92 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
93 assert_se(socket_address_parse(&b, "192.16.1.1:8888") >= 0);
94 assert_se(!socket_address_equal(&a, &b));
95
96 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
97 assert_se(socket_address_parse(&b, "8888") >= 0);
98 assert_se(!socket_address_equal(&a, &b));
99
100 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
101 assert_se(socket_address_parse(&b, "/foo/bar/") >= 0);
102 assert_se(!socket_address_equal(&a, &b));
103
104 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
105 assert_se(socket_address_parse(&b, "192.168.1.1:8888") >= 0);
106 assert_se(socket_address_equal(&a, &b));
107
108 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
109 assert_se(socket_address_parse(&b, "/foo/bar") >= 0);
110 assert_se(socket_address_equal(&a, &b));
111
112 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
113 assert_se(socket_address_parse(&b, "[::1]:8888") >= 0);
114 assert_se(socket_address_equal(&a, &b));
115
116 assert_se(socket_address_parse(&a, "@abstract") >= 0);
117 assert_se(socket_address_parse(&b, "@abstract") >= 0);
118 assert_se(socket_address_equal(&a, &b));
119
120 assert_se(socket_address_parse_netlink(&a, "firewall") >= 0);
121 assert_se(socket_address_parse_netlink(&b, "firewall") >= 0);
122 assert_se(socket_address_equal(&a, &b));
123}
124
125static void test_socket_address_get_path(void) {
126 SocketAddress a;
127
128 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
129 assert_se(!socket_address_get_path(&a));
130
131 assert_se(socket_address_parse(&a, "@abstract") >= 0);
132 assert_se(!socket_address_get_path(&a));
133
134 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
135 assert_se(!socket_address_get_path(&a));
136
137 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
138 assert_se(streq(socket_address_get_path(&a), "/foo/bar"));
139}
140
059f6c42
LP
141static void test_in_addr_prefix_intersect_one(unsigned f, const char *a, unsigned apl, const char *b, unsigned bpl, int result) {
142 union in_addr_union ua, ub;
143
144 assert_se(in_addr_from_string(f, a, &ua) >= 0);
145 assert_se(in_addr_from_string(f, b, &ub) >= 0);
146
147 assert_se(in_addr_prefix_intersect(f, &ua, apl, &ub, bpl) == result);
148}
149
150static void test_in_addr_prefix_intersect(void) {
151
152 test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 32, "255.255.255.254", 32, 0);
153 test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 0, "255.255.255.255", 32, 1);
154 test_in_addr_prefix_intersect_one(AF_INET, "0.0.0.0", 0, "47.11.8.15", 32, 1);
155
156 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.1", 24, 1);
157 test_in_addr_prefix_intersect_one(AF_INET, "2.2.2.2", 24, "1.1.1.1", 24, 0);
158
159 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 25, 1);
160 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 26, 1);
161 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.127", 25, 1);
162 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.255", 25, 0);
163
164 test_in_addr_prefix_intersect_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe", 128, 0);
165 test_in_addr_prefix_intersect_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, 1);
166 test_in_addr_prefix_intersect_one(AF_INET6, "::", 0, "beef:beef:beef:beef:beef:beef:beef:beef", 128, 1);
167
168 test_in_addr_prefix_intersect_one(AF_INET6, "1::2", 64, "1::2", 64, 1);
169 test_in_addr_prefix_intersect_one(AF_INET6, "2::2", 64, "1::2", 64, 0);
170
171 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 121, 1);
172 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 122, 1);
173 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::007f", 121, 1);
174 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::00ff", 121, 0);
175}
176
177static void test_in_addr_prefix_next_one(unsigned f, const char *before, unsigned pl, const char *after) {
178 union in_addr_union ubefore, uafter, t;
179
180 assert_se(in_addr_from_string(f, before, &ubefore) >= 0);
181
182 t = ubefore;
183 assert_se((in_addr_prefix_next(f, &t, pl) > 0) == !!after);
184
185 if (after) {
186 assert_se(in_addr_from_string(f, after, &uafter) >= 0);
187 assert_se(in_addr_equal(f, &t, &uafter) > 0);
188 }
189}
190
191static void test_in_addr_prefix_next(void) {
192
193 test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 24, "192.168.1.0");
194 test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 16, "192.169.0.0");
195 test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 20, "192.168.16.0");
196
197 test_in_addr_prefix_next_one(AF_INET, "0.0.0.0", 32, "0.0.0.1");
198 test_in_addr_prefix_next_one(AF_INET, "255.255.255.255", 32, NULL);
199 test_in_addr_prefix_next_one(AF_INET, "255.255.255.0", 24, NULL);
200
201 test_in_addr_prefix_next_one(AF_INET6, "4400::", 128, "4400::0001");
202 test_in_addr_prefix_next_one(AF_INET6, "4400::", 120, "4400::0100");
203 test_in_addr_prefix_next_one(AF_INET6, "4400::", 127, "4400::0002");
204 test_in_addr_prefix_next_one(AF_INET6, "4400::", 8, "4500::");
205 test_in_addr_prefix_next_one(AF_INET6, "4400::", 7, "4600::");
206
207 test_in_addr_prefix_next_one(AF_INET6, "::", 128, "::1");
208
209 test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, NULL);
210 test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", 120, NULL);
211
212}
213
c182135d 214int main(int argc, char *argv[]) {
059f6c42
LP
215
216 log_set_max_level(LOG_DEBUG);
217
c182135d
RC
218 test_socket_address_parse();
219 test_socket_address_parse_netlink();
220 test_socket_address_equal();
221 test_socket_address_get_path();
059f6c42
LP
222
223 test_in_addr_prefix_intersect();
224 test_in_addr_prefix_next();
225
226 return 0;
c182135d 227}