2 This file is part of systemd
4 Copyright 2014 Ronny Chevalier
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.
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.
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/>.
20 #include "socket-util.h"
21 #include "in-addr-util.h"
27 static void test_socket_address_parse(void) {
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);
39 assert_se(socket_address_parse(&a, "65535") >= 0);
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);
49 assert_se(socket_address_parse(&a, "8888") >= 0);
50 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
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);
55 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
56 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
58 assert_se(socket_address_parse(&a, "[::1]:8888") < 0);
60 assert_se(socket_address_parse(&a, "8888") >= 0);
61 assert_se(a.sockaddr.sa.sa_family == AF_INET);
64 assert_se(socket_address_parse(&a, "192.168.1.254:8888") >= 0);
65 assert_se(a.sockaddr.sa.sa_family == AF_INET);
67 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
68 assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
70 assert_se(socket_address_parse(&a, "@abstract") >= 0);
71 assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
74 static void test_socket_address_parse_netlink(void) {
77 assert_se(socket_address_parse_netlink(&a, "junk") < 0);
78 assert_se(socket_address_parse_netlink(&a, "") < 0);
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);
86 static void test_socket_address_equal(void) {
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));
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));
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));
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));
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));
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));
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));
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));
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));
127 static void test_socket_address_get_path(void) {
130 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
131 assert_se(!socket_address_get_path(&a));
133 assert_se(socket_address_parse(&a, "@abstract") >= 0);
134 assert_se(!socket_address_get_path(&a));
136 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
137 assert_se(!socket_address_get_path(&a));
139 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
140 assert_se(streq(socket_address_get_path(&a), "/foo/bar"));
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;
146 assert_se(in_addr_from_string(f, a, &ua) >= 0);
147 assert_se(in_addr_from_string(f, b, &ub) >= 0);
149 assert_se(in_addr_prefix_intersect(f, &ua, apl, &ub, bpl) == result);
152 static void test_in_addr_prefix_intersect(void) {
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);
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);
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);
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);
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);
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);
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;
182 assert_se(in_addr_from_string(f, before, &ubefore) >= 0);
185 assert_se((in_addr_prefix_next(f, &t, pl) > 0) == !!after);
188 assert_se(in_addr_from_string(f, after, &uafter) >= 0);
189 assert_se(in_addr_equal(f, &t, &uafter) > 0);
193 static void test_in_addr_prefix_next(void) {
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");
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);
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::");
209 test_in_addr_prefix_next_one(AF_INET6, "::", 128, "::1");
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);
216 static void *connect_thread(void *arg) {
217 union sockaddr_union *sa = arg;
218 _cleanup_close_ int fd = -1;
220 fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
223 assert_se(connect(fd, &sa->sa, sizeof(sa->in)) == 0);
228 static void test_nameinfo_pretty(void) {
229 _cleanup_free_ char *stdin_name = NULL, *localhost = NULL;
231 union sockaddr_union s = {
232 .in.sin_family = AF_INET,
234 .in.sin_addr.s_addr = htonl(INADDR_ANY),
238 union sockaddr_union c = {};
239 socklen_t slen = sizeof(c.in), clen = sizeof(c.in);
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));
247 sfd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
250 assert_se(bind(sfd, &s.sa, sizeof(s.in)) == 0);
252 /* find out the port number */
253 assert_se(getsockname(sfd, &s.sa, &slen) == 0);
255 assert_se(listen(sfd, 1) == 0);
257 assert_se(asynchronous_job(connect_thread, &s) == 0);
259 log_debug("Accepting new connection on fd:%d", sfd);
260 cfd = accept4(sfd, &c.sa, &clen, SOCK_CLOEXEC);
263 r = getnameinfo_pretty(cfd, &localhost);
264 log_info("Connection from %s", localhost);
268 int main(int argc, char *argv[]) {
270 log_set_max_level(LOG_DEBUG);
272 test_socket_address_parse();
273 test_socket_address_parse_netlink();
274 test_socket_address_equal();
275 test_socket_address_get_path();
277 test_in_addr_prefix_intersect();
278 test_in_addr_prefix_next();
280 test_nameinfo_pretty();