]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-socket-util.c
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 "alloc-util.h"
23 #include "in-addr-util.h"
26 #include "socket-util.h"
27 #include "string-util.h"
30 static void test_ifname_valid(void) {
31 assert(ifname_valid("foo"));
32 assert(ifname_valid("eth0"));
34 assert(!ifname_valid("0"));
35 assert(!ifname_valid("99"));
36 assert(ifname_valid("a99"));
37 assert(ifname_valid("99a"));
39 assert(!ifname_valid(NULL
));
40 assert(!ifname_valid(""));
41 assert(!ifname_valid(" "));
42 assert(!ifname_valid(" foo"));
43 assert(!ifname_valid("bar\n"));
44 assert(!ifname_valid("."));
45 assert(!ifname_valid(".."));
46 assert(ifname_valid("foo.bar"));
47 assert(!ifname_valid("x:y"));
49 assert(ifname_valid("xxxxxxxxxxxxxxx"));
50 assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
53 static void test_socket_address_parse(void) {
56 assert_se(socket_address_parse(&a
, "junk") < 0);
57 assert_se(socket_address_parse(&a
, "192.168.1.1") < 0);
58 assert_se(socket_address_parse(&a
, ".168.1.1") < 0);
59 assert_se(socket_address_parse(&a
, "989.168.1.1") < 0);
60 assert_se(socket_address_parse(&a
, "192.168.1.1:65536") < 0);
61 assert_se(socket_address_parse(&a
, "192.168.1.1:0") < 0);
62 assert_se(socket_address_parse(&a
, "0") < 0);
63 assert_se(socket_address_parse(&a
, "65536") < 0);
65 assert_se(socket_address_parse(&a
, "65535") >= 0);
67 /* The checks below will pass even if ipv6 is disabled in
68 * kernel. The underlying glibc's inet_pton() is just a string
69 * parser and doesn't make any syscalls. */
71 assert_se(socket_address_parse(&a
, "[::1]") < 0);
72 assert_se(socket_address_parse(&a
, "[::1]8888") < 0);
73 assert_se(socket_address_parse(&a
, "::1") < 0);
74 assert_se(socket_address_parse(&a
, "[::1]:0") < 0);
75 assert_se(socket_address_parse(&a
, "[::1]:65536") < 0);
76 assert_se(socket_address_parse(&a
, "[a:b:1]:8888") < 0);
78 assert_se(socket_address_parse(&a
, "8888") >= 0);
79 assert_se(a
.sockaddr
.sa
.sa_family
== (socket_ipv6_is_supported() ? AF_INET6
: AF_INET
));
81 assert_se(socket_address_parse(&a
, "[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:8888") >= 0);
82 assert_se(a
.sockaddr
.sa
.sa_family
== AF_INET6
);
84 assert_se(socket_address_parse(&a
, "[::1]:8888") >= 0);
85 assert_se(a
.sockaddr
.sa
.sa_family
== AF_INET6
);
87 assert_se(socket_address_parse(&a
, "192.168.1.254:8888") >= 0);
88 assert_se(a
.sockaddr
.sa
.sa_family
== AF_INET
);
90 assert_se(socket_address_parse(&a
, "/foo/bar") >= 0);
91 assert_se(a
.sockaddr
.sa
.sa_family
== AF_UNIX
);
93 assert_se(socket_address_parse(&a
, "@abstract") >= 0);
94 assert_se(a
.sockaddr
.sa
.sa_family
== AF_UNIX
);
97 static void test_socket_address_parse_netlink(void) {
100 assert_se(socket_address_parse_netlink(&a
, "junk") < 0);
101 assert_se(socket_address_parse_netlink(&a
, "") < 0);
103 assert_se(socket_address_parse_netlink(&a
, "route") >= 0);
104 assert_se(socket_address_parse_netlink(&a
, "route 10") >= 0);
105 assert_se(a
.sockaddr
.sa
.sa_family
== AF_NETLINK
);
106 assert_se(a
.protocol
== NETLINK_ROUTE
);
109 static void test_socket_address_equal(void) {
113 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
114 assert_se(socket_address_parse(&b
, "192.168.1.1:888") >= 0);
115 assert_se(!socket_address_equal(&a
, &b
));
117 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
118 assert_se(socket_address_parse(&b
, "192.16.1.1:8888") >= 0);
119 assert_se(!socket_address_equal(&a
, &b
));
121 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
122 assert_se(socket_address_parse(&b
, "8888") >= 0);
123 assert_se(!socket_address_equal(&a
, &b
));
125 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
126 assert_se(socket_address_parse(&b
, "/foo/bar/") >= 0);
127 assert_se(!socket_address_equal(&a
, &b
));
129 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
130 assert_se(socket_address_parse(&b
, "192.168.1.1:8888") >= 0);
131 assert_se(socket_address_equal(&a
, &b
));
133 assert_se(socket_address_parse(&a
, "/foo/bar") >= 0);
134 assert_se(socket_address_parse(&b
, "/foo/bar") >= 0);
135 assert_se(socket_address_equal(&a
, &b
));
137 assert_se(socket_address_parse(&a
, "[::1]:8888") >= 0);
138 assert_se(socket_address_parse(&b
, "[::1]:8888") >= 0);
139 assert_se(socket_address_equal(&a
, &b
));
141 assert_se(socket_address_parse(&a
, "@abstract") >= 0);
142 assert_se(socket_address_parse(&b
, "@abstract") >= 0);
143 assert_se(socket_address_equal(&a
, &b
));
145 assert_se(socket_address_parse_netlink(&a
, "firewall") >= 0);
146 assert_se(socket_address_parse_netlink(&b
, "firewall") >= 0);
147 assert_se(socket_address_equal(&a
, &b
));
150 static void test_socket_address_get_path(void) {
153 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
154 assert_se(!socket_address_get_path(&a
));
156 assert_se(socket_address_parse(&a
, "@abstract") >= 0);
157 assert_se(!socket_address_get_path(&a
));
159 assert_se(socket_address_parse(&a
, "[::1]:8888") >= 0);
160 assert_se(!socket_address_get_path(&a
));
162 assert_se(socket_address_parse(&a
, "/foo/bar") >= 0);
163 assert_se(streq(socket_address_get_path(&a
), "/foo/bar"));
166 static void test_socket_address_is(void) {
169 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
170 assert_se(socket_address_is(&a
, "192.168.1.1:8888", SOCK_STREAM
));
171 assert_se(!socket_address_is(&a
, "route", SOCK_STREAM
));
172 assert_se(!socket_address_is(&a
, "192.168.1.1:8888", SOCK_RAW
));
175 static void test_socket_address_is_netlink(void) {
178 assert_se(socket_address_parse_netlink(&a
, "route 10") >= 0);
179 assert_se(socket_address_is_netlink(&a
, "route 10"));
180 assert_se(!socket_address_is_netlink(&a
, "192.168.1.1:8888"));
181 assert_se(!socket_address_is_netlink(&a
, "route 1"));
184 static void test_in_addr_is_null(void) {
186 union in_addr_union i
= {};
188 assert_se(in_addr_is_null(AF_INET
, &i
) == true);
189 assert_se(in_addr_is_null(AF_INET6
, &i
) == true);
191 i
.in
.s_addr
= 0x1000000;
192 assert_se(in_addr_is_null(AF_INET
, &i
) == false);
193 assert_se(in_addr_is_null(AF_INET6
, &i
) == false);
195 assert_se(in_addr_is_null(-1, &i
) == -EAFNOSUPPORT
);
198 static void test_in_addr_prefix_intersect_one(unsigned f
, const char *a
, unsigned apl
, const char *b
, unsigned bpl
, int result
) {
199 union in_addr_union ua
, ub
;
201 assert_se(in_addr_from_string(f
, a
, &ua
) >= 0);
202 assert_se(in_addr_from_string(f
, b
, &ub
) >= 0);
204 assert_se(in_addr_prefix_intersect(f
, &ua
, apl
, &ub
, bpl
) == result
);
207 static void test_in_addr_prefix_intersect(void) {
209 test_in_addr_prefix_intersect_one(AF_INET
, "255.255.255.255", 32, "255.255.255.254", 32, 0);
210 test_in_addr_prefix_intersect_one(AF_INET
, "255.255.255.255", 0, "255.255.255.255", 32, 1);
211 test_in_addr_prefix_intersect_one(AF_INET
, "0.0.0.0", 0, "47.11.8.15", 32, 1);
213 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 24, "1.1.1.1", 24, 1);
214 test_in_addr_prefix_intersect_one(AF_INET
, "2.2.2.2", 24, "1.1.1.1", 24, 0);
216 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 24, "1.1.1.127", 25, 1);
217 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 24, "1.1.1.127", 26, 1);
218 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 25, "1.1.1.127", 25, 1);
219 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 25, "1.1.1.255", 25, 0);
221 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);
222 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);
223 test_in_addr_prefix_intersect_one(AF_INET6
, "::", 0, "beef:beef:beef:beef:beef:beef:beef:beef", 128, 1);
225 test_in_addr_prefix_intersect_one(AF_INET6
, "1::2", 64, "1::2", 64, 1);
226 test_in_addr_prefix_intersect_one(AF_INET6
, "2::2", 64, "1::2", 64, 0);
228 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 120, "1::007f", 121, 1);
229 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 120, "1::007f", 122, 1);
230 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 121, "1::007f", 121, 1);
231 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 121, "1::00ff", 121, 0);
234 static void test_in_addr_prefix_next_one(unsigned f
, const char *before
, unsigned pl
, const char *after
) {
235 union in_addr_union ubefore
, uafter
, t
;
237 assert_se(in_addr_from_string(f
, before
, &ubefore
) >= 0);
240 assert_se((in_addr_prefix_next(f
, &t
, pl
) > 0) == !!after
);
243 assert_se(in_addr_from_string(f
, after
, &uafter
) >= 0);
244 assert_se(in_addr_equal(f
, &t
, &uafter
) > 0);
248 static void test_in_addr_prefix_next(void) {
250 test_in_addr_prefix_next_one(AF_INET
, "192.168.0.0", 24, "192.168.1.0");
251 test_in_addr_prefix_next_one(AF_INET
, "192.168.0.0", 16, "192.169.0.0");
252 test_in_addr_prefix_next_one(AF_INET
, "192.168.0.0", 20, "192.168.16.0");
254 test_in_addr_prefix_next_one(AF_INET
, "0.0.0.0", 32, "0.0.0.1");
255 test_in_addr_prefix_next_one(AF_INET
, "255.255.255.255", 32, NULL
);
256 test_in_addr_prefix_next_one(AF_INET
, "255.255.255.0", 24, NULL
);
258 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 128, "4400::0001");
259 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 120, "4400::0100");
260 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 127, "4400::0002");
261 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 8, "4500::");
262 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 7, "4600::");
264 test_in_addr_prefix_next_one(AF_INET6
, "::", 128, "::1");
266 test_in_addr_prefix_next_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, NULL
);
267 test_in_addr_prefix_next_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", 120, NULL
);
271 static void test_in_addr_to_string_one(int f
, const char *addr
) {
272 union in_addr_union ua
;
273 _cleanup_free_
char *r
= NULL
;
275 assert_se(in_addr_from_string(f
, addr
, &ua
) >= 0);
276 assert_se(in_addr_to_string(f
, &ua
, &r
) >= 0);
277 printf("test_in_addr_to_string_one: %s == %s\n", addr
, r
);
278 assert_se(streq(addr
, r
));
281 static void test_in_addr_to_string(void) {
282 test_in_addr_to_string_one(AF_INET
, "192.168.0.1");
283 test_in_addr_to_string_one(AF_INET
, "10.11.12.13");
284 test_in_addr_to_string_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
285 test_in_addr_to_string_one(AF_INET6
, "::1");
286 test_in_addr_to_string_one(AF_INET6
, "fe80::");
289 static void *connect_thread(void *arg
) {
290 union sockaddr_union
*sa
= arg
;
291 _cleanup_close_
int fd
= -1;
293 fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
296 assert_se(connect(fd
, &sa
->sa
, sizeof(sa
->in
)) == 0);
301 static void test_nameinfo_pretty(void) {
302 _cleanup_free_
char *stdin_name
= NULL
, *localhost
= NULL
;
304 union sockaddr_union s
= {
305 .in
.sin_family
= AF_INET
,
307 .in
.sin_addr
.s_addr
= htonl(INADDR_ANY
),
311 union sockaddr_union c
= {};
312 socklen_t slen
= sizeof(c
.in
), clen
= sizeof(c
.in
);
314 _cleanup_close_
int sfd
= -1, cfd
= -1;
315 r
= getnameinfo_pretty(STDIN_FILENO
, &stdin_name
);
316 log_info_errno(r
, "No connection remote: %m");
320 sfd
= socket(AF_INET
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
323 assert_se(bind(sfd
, &s
.sa
, sizeof(s
.in
)) == 0);
325 /* find out the port number */
326 assert_se(getsockname(sfd
, &s
.sa
, &slen
) == 0);
328 assert_se(listen(sfd
, 1) == 0);
330 assert_se(asynchronous_job(connect_thread
, &s
) == 0);
332 log_debug("Accepting new connection on fd:%d", sfd
);
333 cfd
= accept4(sfd
, &c
.sa
, &clen
, SOCK_CLOEXEC
);
336 r
= getnameinfo_pretty(cfd
, &localhost
);
337 log_info("Connection from %s", localhost
);
341 static void test_sockaddr_equal(void) {
342 union sockaddr_union a
= {
343 .in
.sin_family
= AF_INET
,
345 .in
.sin_addr
.s_addr
= htonl(INADDR_ANY
),
347 union sockaddr_union b
= {
348 .in
.sin_family
= AF_INET
,
350 .in
.sin_addr
.s_addr
= htonl(INADDR_ANY
),
352 union sockaddr_union c
= {
353 .in
.sin_family
= AF_INET
,
355 .in
.sin_addr
.s_addr
= htonl(1234),
357 union sockaddr_union d
= {
358 .in6
.sin6_family
= AF_INET6
,
360 .in6
.sin6_addr
= IN6ADDR_ANY_INIT
,
362 assert_se(sockaddr_equal(&a
, &a
));
363 assert_se(sockaddr_equal(&a
, &b
));
364 assert_se(sockaddr_equal(&d
, &d
));
365 assert_se(!sockaddr_equal(&a
, &c
));
366 assert_se(!sockaddr_equal(&b
, &c
));
369 static void test_sockaddr_un_len(void) {
370 static const struct sockaddr_un fs
= {
371 .sun_family
= AF_UNIX
,
372 .sun_path
= "/foo/bar/waldo",
375 static const struct sockaddr_un abstract
= {
376 .sun_family
= AF_UNIX
,
377 .sun_path
= "\0foobar",
380 assert_se(SOCKADDR_UN_LEN(fs
) == offsetof(struct sockaddr_un
, sun_path
) + strlen(fs
.sun_path
));
381 assert_se(SOCKADDR_UN_LEN(abstract
) == offsetof(struct sockaddr_un
, sun_path
) + 1 + strlen(abstract
.sun_path
+ 1));
384 int main(int argc
, char *argv
[]) {
386 log_set_max_level(LOG_DEBUG
);
390 test_socket_address_parse();
391 test_socket_address_parse_netlink();
392 test_socket_address_equal();
393 test_socket_address_get_path();
394 test_socket_address_is();
395 test_socket_address_is_netlink();
397 test_in_addr_is_null();
398 test_in_addr_prefix_intersect();
399 test_in_addr_prefix_next();
400 test_in_addr_to_string();
402 test_nameinfo_pretty();
404 test_sockaddr_equal();
406 test_sockaddr_un_len();