chiark / gitweb /
test-socket-util: silence warnings
[elogind.git] / src / test / test-socket-util.c
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 "in-addr-util.h"
22 #include "util.h"
23 #include "macro.h"
24 #include "log.h"
25 #include "async.h"
26
27 static void test_socket_address_parse(void) {
28         SocketAddress a;
29
30         assert_se(socket_address_parse(&a, "junk") < 0);
31         assert_se(socket_address_parse(&a, "192.168.1.1") < 0);
32         assert_se(socket_address_parse(&a, ".168.1.1") < 0);
33         assert_se(socket_address_parse(&a, "989.168.1.1") < 0);
34         assert_se(socket_address_parse(&a, "192.168.1.1:65536") < 0);
35         assert_se(socket_address_parse(&a, "192.168.1.1:0") < 0);
36         assert_se(socket_address_parse(&a, "0") < 0);
37         assert_se(socket_address_parse(&a, "65536") < 0);
38
39         assert_se(socket_address_parse(&a, "65535") >= 0);
40
41         if (socket_ipv6_is_supported()) {
42                 assert_se(socket_address_parse(&a, "[::1]") < 0);
43                 assert_se(socket_address_parse(&a, "[::1]8888") < 0);
44                 assert_se(socket_address_parse(&a, "::1") < 0);
45                 assert_se(socket_address_parse(&a, "[::1]:0") < 0);
46                 assert_se(socket_address_parse(&a, "[::1]:65536") < 0);
47                 assert_se(socket_address_parse(&a, "[a:b:1]:8888") < 0);
48
49                 assert_se(socket_address_parse(&a, "8888") >= 0);
50                 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
51
52                 assert_se(socket_address_parse(&a, "[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:8888") >= 0);
53                 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
54
55                 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
56                 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
57         } else {
58                 assert_se(socket_address_parse(&a, "[::1]:8888") < 0);
59
60                 assert_se(socket_address_parse(&a, "8888") >= 0);
61                 assert_se(a.sockaddr.sa.sa_family == AF_INET);
62         }
63
64         assert_se(socket_address_parse(&a, "192.168.1.254:8888") >= 0);
65         assert_se(a.sockaddr.sa.sa_family == AF_INET);
66
67         assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
68         assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
69
70         assert_se(socket_address_parse(&a, "@abstract") >= 0);
71         assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
72 }
73
74 static void test_socket_address_parse_netlink(void) {
75         SocketAddress a;
76
77         assert_se(socket_address_parse_netlink(&a, "junk") < 0);
78         assert_se(socket_address_parse_netlink(&a, "") < 0);
79
80         assert_se(socket_address_parse_netlink(&a, "route") >= 0);
81         assert_se(socket_address_parse_netlink(&a, "route 10") >= 0);
82         assert_se(a.sockaddr.sa.sa_family == AF_NETLINK);
83         assert_se(a.protocol == NETLINK_ROUTE);
84 }
85
86 static void test_socket_address_equal(void) {
87         SocketAddress a;
88         SocketAddress b;
89
90         assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
91         assert_se(socket_address_parse(&b, "192.168.1.1:888") >= 0);
92         assert_se(!socket_address_equal(&a, &b));
93
94         assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
95         assert_se(socket_address_parse(&b, "192.16.1.1:8888") >= 0);
96         assert_se(!socket_address_equal(&a, &b));
97
98         assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
99         assert_se(socket_address_parse(&b, "8888") >= 0);
100         assert_se(!socket_address_equal(&a, &b));
101
102         assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
103         assert_se(socket_address_parse(&b, "/foo/bar/") >= 0);
104         assert_se(!socket_address_equal(&a, &b));
105
106         assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
107         assert_se(socket_address_parse(&b, "192.168.1.1:8888") >= 0);
108         assert_se(socket_address_equal(&a, &b));
109
110         assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
111         assert_se(socket_address_parse(&b, "/foo/bar") >= 0);
112         assert_se(socket_address_equal(&a, &b));
113
114         assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
115         assert_se(socket_address_parse(&b, "[::1]:8888") >= 0);
116         assert_se(socket_address_equal(&a, &b));
117
118         assert_se(socket_address_parse(&a, "@abstract") >= 0);
119         assert_se(socket_address_parse(&b, "@abstract") >= 0);
120         assert_se(socket_address_equal(&a, &b));
121
122         assert_se(socket_address_parse_netlink(&a, "firewall") >= 0);
123         assert_se(socket_address_parse_netlink(&b, "firewall") >= 0);
124         assert_se(socket_address_equal(&a, &b));
125 }
126
127 static void test_socket_address_get_path(void) {
128         SocketAddress a;
129
130         assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
131         assert_se(!socket_address_get_path(&a));
132
133         assert_se(socket_address_parse(&a, "@abstract") >= 0);
134         assert_se(!socket_address_get_path(&a));
135
136         assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
137         assert_se(!socket_address_get_path(&a));
138
139         assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
140         assert_se(streq(socket_address_get_path(&a), "/foo/bar"));
141 }
142
143 static void test_in_addr_prefix_intersect_one(unsigned f, const char *a, unsigned apl, const char *b, unsigned bpl, int result) {
144         union in_addr_union ua, ub;
145
146         assert_se(in_addr_from_string(f, a, &ua) >= 0);
147         assert_se(in_addr_from_string(f, b, &ub) >= 0);
148
149         assert_se(in_addr_prefix_intersect(f, &ua, apl, &ub, bpl) == result);
150 }
151
152 static void test_in_addr_prefix_intersect(void) {
153
154         test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 32, "255.255.255.254", 32, 0);
155         test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 0, "255.255.255.255", 32, 1);
156         test_in_addr_prefix_intersect_one(AF_INET, "0.0.0.0", 0, "47.11.8.15", 32, 1);
157
158         test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.1", 24, 1);
159         test_in_addr_prefix_intersect_one(AF_INET, "2.2.2.2", 24, "1.1.1.1", 24, 0);
160
161         test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 25, 1);
162         test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 26, 1);
163         test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.127", 25, 1);
164         test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.255", 25, 0);
165
166         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);
167         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);
168         test_in_addr_prefix_intersect_one(AF_INET6, "::", 0, "beef:beef:beef:beef:beef:beef:beef:beef", 128, 1);
169
170         test_in_addr_prefix_intersect_one(AF_INET6, "1::2", 64, "1::2", 64, 1);
171         test_in_addr_prefix_intersect_one(AF_INET6, "2::2", 64, "1::2", 64, 0);
172
173         test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 121, 1);
174         test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 122, 1);
175         test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::007f", 121, 1);
176         test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::00ff", 121, 0);
177 }
178
179 static void test_in_addr_prefix_next_one(unsigned f, const char *before, unsigned pl, const char *after) {
180         union in_addr_union ubefore, uafter, t;
181
182         assert_se(in_addr_from_string(f, before, &ubefore) >= 0);
183
184         t = ubefore;
185         assert_se((in_addr_prefix_next(f, &t, pl) > 0) == !!after);
186
187         if (after) {
188                 assert_se(in_addr_from_string(f, after, &uafter) >= 0);
189                 assert_se(in_addr_equal(f, &t, &uafter) > 0);
190         }
191 }
192
193 static void test_in_addr_prefix_next(void) {
194
195         test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 24, "192.168.1.0");
196         test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 16, "192.169.0.0");
197         test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 20, "192.168.16.0");
198
199         test_in_addr_prefix_next_one(AF_INET, "0.0.0.0", 32, "0.0.0.1");
200         test_in_addr_prefix_next_one(AF_INET, "255.255.255.255", 32, NULL);
201         test_in_addr_prefix_next_one(AF_INET, "255.255.255.0", 24, NULL);
202
203         test_in_addr_prefix_next_one(AF_INET6, "4400::", 128, "4400::0001");
204         test_in_addr_prefix_next_one(AF_INET6, "4400::", 120, "4400::0100");
205         test_in_addr_prefix_next_one(AF_INET6, "4400::", 127, "4400::0002");
206         test_in_addr_prefix_next_one(AF_INET6, "4400::", 8, "4500::");
207         test_in_addr_prefix_next_one(AF_INET6, "4400::", 7, "4600::");
208
209         test_in_addr_prefix_next_one(AF_INET6, "::", 128, "::1");
210
211         test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, NULL);
212         test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", 120, NULL);
213
214 }
215
216 static void *connect_thread(void *arg) {
217         union sockaddr_union *sa = arg;
218         _cleanup_close_ int fd = -1;
219
220         fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
221         assert(fd >= 0);
222
223         assert_se(connect(fd, &sa->sa, sizeof(sa->in)) == 0);
224
225         return NULL;
226 }
227
228 static void test_nameinfo_pretty(void) {
229         _cleanup_free_ char *stdin_name = NULL, *localhost = NULL;
230
231         union sockaddr_union s = {
232                 .in.sin_family = AF_INET,
233                 .in.sin_port = 0,
234                 .in.sin_addr.s_addr = htonl(INADDR_ANY),
235         };
236         int r;
237
238         union sockaddr_union c = {};
239         socklen_t slen = sizeof(c.in), clen = sizeof(c.in);
240
241         _cleanup_close_ int sfd = -1, cfd = -1;
242         r = getnameinfo_pretty(STDIN_FILENO, &stdin_name);
243         log_info("No connection remote: %s", strerror(-r));
244
245         assert_se(r < 0);
246
247         sfd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
248         assert(sfd >= 0);
249
250         assert_se(bind(sfd, &s.sa, sizeof(s.in)) == 0);
251
252         /* find out the port number */
253         assert_se(getsockname(sfd, &s.sa, &slen) == 0);
254
255         assert_se(listen(sfd, 1) == 0);
256
257         assert_se(asynchronous_job(connect_thread, &s) == 0);
258
259         log_debug("Accepting new connection on fd:%d", sfd);
260         cfd = accept4(sfd, &c.sa, &clen, SOCK_CLOEXEC);
261         assert(cfd >= 0);
262
263         r = getnameinfo_pretty(cfd, &localhost);
264         log_info("Connection from %s", localhost);
265         assert(r == 0);
266 }
267
268 int main(int argc, char *argv[]) {
269
270         log_set_max_level(LOG_DEBUG);
271
272         test_socket_address_parse();
273         test_socket_address_parse_netlink();
274         test_socket_address_equal();
275         test_socket_address_get_path();
276
277         test_in_addr_prefix_intersect();
278         test_in_addr_prefix_next();
279
280         test_nameinfo_pretty();
281
282         return 0;
283 }