]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-socket-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
12 #include "exit-status.h"
14 #include "in-addr-util.h"
18 #include "missing_network.h"
19 #include "process-util.h"
20 #include "socket-util.h"
21 #include "string-util.h"
23 #include "tmpfile-util.h"
25 static void test_ifname_valid(void) {
26 log_info("/* %s */", __func__
);
28 assert(ifname_valid("foo"));
29 assert(ifname_valid("eth0"));
31 assert(!ifname_valid("0"));
32 assert(!ifname_valid("99"));
33 assert(ifname_valid("a99"));
34 assert(ifname_valid("99a"));
36 assert(!ifname_valid(NULL
));
37 assert(!ifname_valid(""));
38 assert(!ifname_valid(" "));
39 assert(!ifname_valid(" foo"));
40 assert(!ifname_valid("bar\n"));
41 assert(!ifname_valid("."));
42 assert(!ifname_valid(".."));
43 assert(ifname_valid("foo.bar"));
44 assert(!ifname_valid("x:y"));
46 assert(ifname_valid("xxxxxxxxxxxxxxx"));
47 assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
50 static void test_socket_address_parse_one(const char *in
, int ret
, int family
, const char *expected
) {
52 _cleanup_free_
char *out
= NULL
;
55 r
= socket_address_parse(&a
, in
);
57 assert_se(socket_address_print(&a
, &out
) >= 0);
59 log_info("\"%s\" → %s → \"%s\" (expect \"%s\")", in
,
60 r
>= 0 ? "✓" : "✗", empty_to_dash(out
), r
>= 0 ? expected
?: in
: "-");
63 assert_se(a
.sockaddr
.sa
.sa_family
== family
);
64 assert_se(streq(out
, expected
?: in
));
68 #define SUN_PATH_LEN (sizeof(((struct sockaddr_un){}).sun_path))
69 assert_cc(sizeof(((struct sockaddr_un
){}).sun_path
) == 108);
71 static void test_socket_address_parse(void) {
72 log_info("/* %s */", __func__
);
74 test_socket_address_parse_one("junk", -EINVAL
, 0, NULL
);
75 test_socket_address_parse_one("192.168.1.1", -EINVAL
, 0, NULL
);
76 test_socket_address_parse_one(".168.1.1", -EINVAL
, 0, NULL
);
77 test_socket_address_parse_one("989.168.1.1", -EINVAL
, 0, NULL
);
78 test_socket_address_parse_one("192.168.1.1:65536", -ERANGE
, 0, NULL
);
79 test_socket_address_parse_one("192.168.1.1:0", -EINVAL
, 0, NULL
);
80 test_socket_address_parse_one("0", -EINVAL
, 0, NULL
);
81 test_socket_address_parse_one("65536", -ERANGE
, 0, NULL
);
83 const int default_family
= socket_ipv6_is_supported() ? AF_INET6
: AF_INET
;
85 test_socket_address_parse_one("65535", 0, default_family
, "[::]:65535");
87 /* The checks below will pass even if ipv6 is disabled in
88 * kernel. The underlying glibc's inet_pton() is just a string
89 * parser and doesn't make any syscalls. */
91 test_socket_address_parse_one("[::1]", -EINVAL
, 0, NULL
);
92 test_socket_address_parse_one("[::1]8888", -EINVAL
, 0, NULL
);
93 test_socket_address_parse_one("::1", -EINVAL
, 0, NULL
);
94 test_socket_address_parse_one("[::1]:0", -EINVAL
, 0, NULL
);
95 test_socket_address_parse_one("[::1]:65536", -ERANGE
, 0, NULL
);
96 test_socket_address_parse_one("[a:b:1]:8888", -EINVAL
, 0, NULL
);
98 test_socket_address_parse_one("8888", 0, default_family
, "[::]:8888");
99 test_socket_address_parse_one("[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:8888", 0, AF_INET6
,
100 "[2001:db8:0:85a3::ac1f:8001]:8888");
101 test_socket_address_parse_one("[::1]:8888", 0, AF_INET6
, NULL
);
102 test_socket_address_parse_one("192.168.1.254:8888", 0, AF_INET
, NULL
);
103 test_socket_address_parse_one("/foo/bar", 0, AF_UNIX
, NULL
);
104 test_socket_address_parse_one("/", 0, AF_UNIX
, NULL
);
105 test_socket_address_parse_one("@abstract", 0, AF_UNIX
, NULL
);
108 char aaa
[SUN_PATH_LEN
+ 1] = "@";
110 memset(aaa
+ 1, 'a', SUN_PATH_LEN
- 1);
113 test_socket_address_parse_one(aaa
, -EINVAL
, 0, NULL
);
115 aaa
[SUN_PATH_LEN
- 1] = '\0';
116 test_socket_address_parse_one(aaa
, 0, AF_UNIX
, NULL
);
119 test_socket_address_parse_one("vsock:2:1234", 0, AF_VSOCK
, NULL
);
120 test_socket_address_parse_one("vsock::1234", 0, AF_VSOCK
, NULL
);
121 test_socket_address_parse_one("vsock:2:1234x", -EINVAL
, 0, NULL
);
122 test_socket_address_parse_one("vsock:2x:1234", -EINVAL
, 0, NULL
);
123 test_socket_address_parse_one("vsock:2", -EINVAL
, 0, NULL
);
126 static void test_socket_print_unix_one(const char *in
, size_t len_in
, const char *expected
) {
127 _cleanup_free_
char *out
= NULL
, *c
= NULL
;
129 SocketAddress a
= { .sockaddr
= { .un
= { .sun_family
= AF_UNIX
} },
130 .size
= offsetof(struct sockaddr_un
, sun_path
) + len_in
,
133 memcpy(a
.sockaddr
.un
.sun_path
, in
, len_in
);
135 assert_se(socket_address_print(&a
, &out
) >= 0);
136 assert_se(c
= cescape(in
));
137 log_info("\"%s\" → \"%s\" (expect \"%s\")", in
, out
, expected
);
138 assert_se(streq(out
, expected
));
141 static void test_socket_print_unix(void) {
142 log_info("/* %s */", __func__
);
144 /* Some additional tests for abstract addresses which we don't parse */
146 test_socket_print_unix_one("\0\0\0\0", 4, "@\\000\\000\\000");
147 test_socket_print_unix_one("@abs", 5, "@abs");
148 test_socket_print_unix_one("\n", 2, "\\n");
149 test_socket_print_unix_one("", 1, "<unnamed>");
150 test_socket_print_unix_one("\0", 1, "<unnamed>");
151 test_socket_print_unix_one("\0_________________________there's 108 characters in this string_____________________________________________", 108,
152 "@_________________________there\\'s 108 characters in this string_____________________________________________");
153 test_socket_print_unix_one("////////////////////////////////////////////////////////////////////////////////////////////////////////////", 108,
154 "////////////////////////////////////////////////////////////////////////////////////////////////////////////");
155 test_socket_print_unix_one("////////////////////////////////////////////////////////////////////////////////////////////////////////////", 109,
156 "////////////////////////////////////////////////////////////////////////////////////////////////////////////");
157 test_socket_print_unix_one("\0\a\b\n\255", 6, "@\\a\\b\\n\\255\\000");
160 static void test_socket_address_parse_netlink(void) {
163 log_info("/* %s */", __func__
);
165 assert_se(socket_address_parse_netlink(&a
, "junk") < 0);
166 assert_se(socket_address_parse_netlink(&a
, "") < 0);
168 assert_se(socket_address_parse_netlink(&a
, "route") >= 0);
169 assert_se(a
.sockaddr
.nl
.nl_family
== AF_NETLINK
);
170 assert_se(a
.sockaddr
.nl
.nl_groups
== 0);
171 assert_se(a
.protocol
== NETLINK_ROUTE
);
172 assert_se(socket_address_parse_netlink(&a
, "route") >= 0);
173 assert_se(socket_address_parse_netlink(&a
, "route 10") >= 0);
174 assert_se(a
.sockaddr
.nl
.nl_family
== AF_NETLINK
);
175 assert_se(a
.sockaddr
.nl
.nl_groups
== 10);
176 assert_se(a
.protocol
== NETLINK_ROUTE
);
178 /* With spaces and tabs */
179 assert_se(socket_address_parse_netlink(&a
, " kobject-uevent ") >= 0);
180 assert_se(a
.sockaddr
.nl
.nl_family
== AF_NETLINK
);
181 assert_se(a
.sockaddr
.nl
.nl_groups
== 0);
182 assert_se(a
.protocol
== NETLINK_KOBJECT_UEVENT
);
183 assert_se(socket_address_parse_netlink(&a
, " \t kobject-uevent \t 10") >= 0);
184 assert_se(a
.sockaddr
.nl
.nl_family
== AF_NETLINK
);
185 assert_se(a
.sockaddr
.nl
.nl_groups
== 10);
186 assert_se(a
.protocol
== NETLINK_KOBJECT_UEVENT
);
187 assert_se(socket_address_parse_netlink(&a
, "kobject-uevent\t10") >= 0);
188 assert_se(a
.sockaddr
.nl
.nl_family
== AF_NETLINK
);
189 assert_se(a
.sockaddr
.nl
.nl_groups
== 10);
190 assert_se(a
.protocol
== NETLINK_KOBJECT_UEVENT
);
192 /* trailing space is not supported */
193 assert_se(socket_address_parse_netlink(&a
, "kobject-uevent\t10 ") < 0);
195 /* Group must be unsigned */
196 assert_se(socket_address_parse_netlink(&a
, "kobject-uevent -1") < 0);
199 assert_se(socket_address_parse_netlink(&a
, "\xff") < 0);
202 static void test_socket_address_equal(void) {
205 log_info("/* %s */", __func__
);
207 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
208 assert_se(socket_address_parse(&b
, "192.168.1.1:888") >= 0);
209 assert_se(!socket_address_equal(&a
, &b
));
211 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
212 assert_se(socket_address_parse(&b
, "192.16.1.1:8888") >= 0);
213 assert_se(!socket_address_equal(&a
, &b
));
215 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
216 assert_se(socket_address_parse(&b
, "8888") >= 0);
217 assert_se(!socket_address_equal(&a
, &b
));
219 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
220 assert_se(socket_address_parse(&b
, "/foo/bar/") >= 0);
221 assert_se(!socket_address_equal(&a
, &b
));
223 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
224 assert_se(socket_address_parse(&b
, "192.168.1.1:8888") >= 0);
225 assert_se(socket_address_equal(&a
, &b
));
227 assert_se(socket_address_parse(&a
, "/foo/bar") >= 0);
228 assert_se(socket_address_parse(&b
, "/foo/bar") >= 0);
229 assert_se(socket_address_equal(&a
, &b
));
231 assert_se(socket_address_parse(&a
, "[::1]:8888") >= 0);
232 assert_se(socket_address_parse(&b
, "[::1]:8888") >= 0);
233 assert_se(socket_address_equal(&a
, &b
));
235 assert_se(socket_address_parse(&a
, "@abstract") >= 0);
236 assert_se(socket_address_parse(&b
, "@abstract") >= 0);
237 assert_se(socket_address_equal(&a
, &b
));
239 assert_se(socket_address_parse_netlink(&a
, "firewall") >= 0);
240 assert_se(socket_address_parse_netlink(&b
, "firewall") >= 0);
241 assert_se(socket_address_equal(&a
, &b
));
243 assert_se(socket_address_parse(&a
, "vsock:2:1234") >= 0);
244 assert_se(socket_address_parse(&b
, "vsock:2:1234") >= 0);
245 assert_se(socket_address_equal(&a
, &b
));
246 assert_se(socket_address_parse(&b
, "vsock:2:1235") >= 0);
247 assert_se(!socket_address_equal(&a
, &b
));
248 assert_se(socket_address_parse(&b
, "vsock:3:1234") >= 0);
249 assert_se(!socket_address_equal(&a
, &b
));
252 static void test_socket_address_get_path(void) {
255 log_info("/* %s */", __func__
);
257 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
258 assert_se(!socket_address_get_path(&a
));
260 assert_se(socket_address_parse(&a
, "@abstract") >= 0);
261 assert_se(!socket_address_get_path(&a
));
263 assert_se(socket_address_parse(&a
, "[::1]:8888") >= 0);
264 assert_se(!socket_address_get_path(&a
));
266 assert_se(socket_address_parse(&a
, "/foo/bar") >= 0);
267 assert_se(streq(socket_address_get_path(&a
), "/foo/bar"));
269 assert_se(socket_address_parse(&a
, "vsock:2:1234") >= 0);
270 assert_se(!socket_address_get_path(&a
));
273 static void test_socket_address_is(void) {
276 log_info("/* %s */", __func__
);
278 assert_se(socket_address_parse(&a
, "192.168.1.1:8888") >= 0);
279 assert_se(socket_address_is(&a
, "192.168.1.1:8888", SOCK_STREAM
));
280 assert_se(!socket_address_is(&a
, "route", SOCK_STREAM
));
281 assert_se(!socket_address_is(&a
, "192.168.1.1:8888", SOCK_RAW
));
284 static void test_socket_address_is_netlink(void) {
287 log_info("/* %s */", __func__
);
289 assert_se(socket_address_parse_netlink(&a
, "route 10") >= 0);
290 assert_se(socket_address_is_netlink(&a
, "route 10"));
291 assert_se(!socket_address_is_netlink(&a
, "192.168.1.1:8888"));
292 assert_se(!socket_address_is_netlink(&a
, "route 1"));
295 static void test_in_addr_is_null(void) {
296 union in_addr_union i
= {};
298 log_info("/* %s */", __func__
);
300 assert_se(in_addr_is_null(AF_INET
, &i
) == true);
301 assert_se(in_addr_is_null(AF_INET6
, &i
) == true);
303 i
.in
.s_addr
= 0x1000000;
304 assert_se(in_addr_is_null(AF_INET
, &i
) == false);
305 assert_se(in_addr_is_null(AF_INET6
, &i
) == false);
307 assert_se(in_addr_is_null(-1, &i
) == -EAFNOSUPPORT
);
310 static void test_in_addr_prefix_intersect_one(unsigned f
, const char *a
, unsigned apl
, const char *b
, unsigned bpl
, int result
) {
311 union in_addr_union ua
, ub
;
313 assert_se(in_addr_from_string(f
, a
, &ua
) >= 0);
314 assert_se(in_addr_from_string(f
, b
, &ub
) >= 0);
316 assert_se(in_addr_prefix_intersect(f
, &ua
, apl
, &ub
, bpl
) == result
);
319 static void test_in_addr_prefix_intersect(void) {
320 log_info("/* %s */", __func__
);
322 test_in_addr_prefix_intersect_one(AF_INET
, "255.255.255.255", 32, "255.255.255.254", 32, 0);
323 test_in_addr_prefix_intersect_one(AF_INET
, "255.255.255.255", 0, "255.255.255.255", 32, 1);
324 test_in_addr_prefix_intersect_one(AF_INET
, "0.0.0.0", 0, "47.11.8.15", 32, 1);
326 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 24, "1.1.1.1", 24, 1);
327 test_in_addr_prefix_intersect_one(AF_INET
, "2.2.2.2", 24, "1.1.1.1", 24, 0);
329 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 24, "1.1.1.127", 25, 1);
330 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 24, "1.1.1.127", 26, 1);
331 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 25, "1.1.1.127", 25, 1);
332 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 25, "1.1.1.255", 25, 0);
334 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);
335 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);
336 test_in_addr_prefix_intersect_one(AF_INET6
, "::", 0, "beef:beef:beef:beef:beef:beef:beef:beef", 128, 1);
338 test_in_addr_prefix_intersect_one(AF_INET6
, "1::2", 64, "1::2", 64, 1);
339 test_in_addr_prefix_intersect_one(AF_INET6
, "2::2", 64, "1::2", 64, 0);
341 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 120, "1::007f", 121, 1);
342 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 120, "1::007f", 122, 1);
343 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 121, "1::007f", 121, 1);
344 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 121, "1::00ff", 121, 0);
347 static void test_in_addr_prefix_next_one(unsigned f
, const char *before
, unsigned pl
, const char *after
) {
348 union in_addr_union ubefore
, uafter
, t
;
350 assert_se(in_addr_from_string(f
, before
, &ubefore
) >= 0);
353 assert_se((in_addr_prefix_next(f
, &t
, pl
) > 0) == !!after
);
356 assert_se(in_addr_from_string(f
, after
, &uafter
) >= 0);
357 assert_se(in_addr_equal(f
, &t
, &uafter
) > 0);
361 static void test_in_addr_prefix_next(void) {
362 log_info("/* %s */", __func__
);
364 test_in_addr_prefix_next_one(AF_INET
, "192.168.0.0", 24, "192.168.1.0");
365 test_in_addr_prefix_next_one(AF_INET
, "192.168.0.0", 16, "192.169.0.0");
366 test_in_addr_prefix_next_one(AF_INET
, "192.168.0.0", 20, "192.168.16.0");
368 test_in_addr_prefix_next_one(AF_INET
, "0.0.0.0", 32, "0.0.0.1");
369 test_in_addr_prefix_next_one(AF_INET
, "255.255.255.255", 32, NULL
);
370 test_in_addr_prefix_next_one(AF_INET
, "255.255.255.0", 24, NULL
);
372 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 128, "4400::0001");
373 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 120, "4400::0100");
374 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 127, "4400::0002");
375 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 8, "4500::");
376 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 7, "4600::");
378 test_in_addr_prefix_next_one(AF_INET6
, "::", 128, "::1");
380 test_in_addr_prefix_next_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, NULL
);
381 test_in_addr_prefix_next_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", 120, NULL
);
384 static void test_in_addr_to_string_one(int f
, const char *addr
) {
385 union in_addr_union ua
;
386 _cleanup_free_
char *r
= NULL
;
388 assert_se(in_addr_from_string(f
, addr
, &ua
) >= 0);
389 assert_se(in_addr_to_string(f
, &ua
, &r
) >= 0);
390 printf("test_in_addr_to_string_one: %s == %s\n", addr
, r
);
391 assert_se(streq(addr
, r
));
394 static void test_in_addr_to_string(void) {
395 log_info("/* %s */", __func__
);
397 test_in_addr_to_string_one(AF_INET
, "192.168.0.1");
398 test_in_addr_to_string_one(AF_INET
, "10.11.12.13");
399 test_in_addr_to_string_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
400 test_in_addr_to_string_one(AF_INET6
, "::1");
401 test_in_addr_to_string_one(AF_INET6
, "fe80::");
404 static void test_in_addr_ifindex_to_string_one(int f
, const char *a
, int ifindex
, const char *b
) {
405 _cleanup_free_
char *r
= NULL
;
406 union in_addr_union ua
, uuaa
;
409 assert_se(in_addr_from_string(f
, a
, &ua
) >= 0);
410 assert_se(in_addr_ifindex_to_string(f
, &ua
, ifindex
, &r
) >= 0);
411 printf("test_in_addr_ifindex_to_string_one: %s == %s\n", b
, r
);
412 assert_se(streq(b
, r
));
414 assert_se(in_addr_ifindex_from_string_auto(b
, &ff
, &uuaa
, &ifindex2
) >= 0);
416 assert_se(in_addr_equal(f
, &ua
, &uuaa
));
417 assert_se(ifindex2
== ifindex
|| ifindex2
== 0);
420 static void test_in_addr_ifindex_to_string(void) {
421 log_info("/* %s */", __func__
);
423 test_in_addr_ifindex_to_string_one(AF_INET
, "192.168.0.1", 7, "192.168.0.1");
424 test_in_addr_ifindex_to_string_one(AF_INET
, "10.11.12.13", 9, "10.11.12.13");
425 test_in_addr_ifindex_to_string_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 10, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
426 test_in_addr_ifindex_to_string_one(AF_INET6
, "::1", 11, "::1");
427 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::", 12, "fe80::%12");
428 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::", 0, "fe80::");
429 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::14", 12, "fe80::14%12");
430 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::15", -7, "fe80::15");
431 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::16", LOOPBACK_IFINDEX
, "fe80::16%1");
434 static void test_in_addr_ifindex_from_string_auto(void) {
436 union in_addr_union ua
;
438 log_info("/* %s */", __func__
);
439 /* Most in_addr_ifindex_from_string_auto() invocations have already been tested above, but let's test some more */
441 assert_se(in_addr_ifindex_from_string_auto("fe80::17", &family
, &ua
, &ifindex
) >= 0);
442 assert_se(family
== AF_INET6
);
443 assert_se(ifindex
== 0);
445 assert_se(in_addr_ifindex_from_string_auto("fe80::18%19", &family
, &ua
, &ifindex
) >= 0);
446 assert_se(family
== AF_INET6
);
447 assert_se(ifindex
== 19);
449 assert_se(in_addr_ifindex_from_string_auto("fe80::18%lo", &family
, &ua
, &ifindex
) >= 0);
450 assert_se(family
== AF_INET6
);
451 assert_se(ifindex
== LOOPBACK_IFINDEX
);
453 assert_se(in_addr_ifindex_from_string_auto("fe80::19%thisinterfacecantexist", &family
, &ua
, &ifindex
) == -ENODEV
);
456 static void test_sockaddr_equal(void) {
457 union sockaddr_union a
= {
458 .in
.sin_family
= AF_INET
,
460 .in
.sin_addr
.s_addr
= htobe32(INADDR_ANY
),
462 union sockaddr_union b
= {
463 .in
.sin_family
= AF_INET
,
465 .in
.sin_addr
.s_addr
= htobe32(INADDR_ANY
),
467 union sockaddr_union c
= {
468 .in
.sin_family
= AF_INET
,
470 .in
.sin_addr
.s_addr
= htobe32(1234),
472 union sockaddr_union d
= {
473 .in6
.sin6_family
= AF_INET6
,
475 .in6
.sin6_addr
= IN6ADDR_ANY_INIT
,
477 union sockaddr_union e
= {
478 .vm
.svm_family
= AF_VSOCK
,
480 .vm
.svm_cid
= VMADDR_CID_ANY
,
483 log_info("/* %s */", __func__
);
485 assert_se(sockaddr_equal(&a
, &a
));
486 assert_se(sockaddr_equal(&a
, &b
));
487 assert_se(sockaddr_equal(&d
, &d
));
488 assert_se(sockaddr_equal(&e
, &e
));
489 assert_se(!sockaddr_equal(&a
, &c
));
490 assert_se(!sockaddr_equal(&b
, &c
));
491 assert_se(!sockaddr_equal(&a
, &e
));
494 static void test_sockaddr_un_len(void) {
495 log_info("/* %s */", __func__
);
497 static const struct sockaddr_un fs
= {
498 .sun_family
= AF_UNIX
,
499 .sun_path
= "/foo/bar/waldo",
502 static const struct sockaddr_un abstract
= {
503 .sun_family
= AF_UNIX
,
504 .sun_path
= "\0foobar",
507 assert_se(SOCKADDR_UN_LEN(fs
) == offsetof(struct sockaddr_un
, sun_path
) + strlen(fs
.sun_path
) + 1);
508 assert_se(SOCKADDR_UN_LEN(abstract
) == offsetof(struct sockaddr_un
, sun_path
) + 1 + strlen(abstract
.sun_path
+ 1));
511 static void test_in_addr_is_multicast(void) {
512 union in_addr_union a
, b
;
515 log_info("/* %s */", __func__
);
517 assert_se(in_addr_from_string_auto("192.168.3.11", &f
, &a
) >= 0);
518 assert_se(in_addr_is_multicast(f
, &a
) == 0);
520 assert_se(in_addr_from_string_auto("224.0.0.1", &f
, &a
) >= 0);
521 assert_se(in_addr_is_multicast(f
, &a
) == 1);
523 assert_se(in_addr_from_string_auto("FF01:0:0:0:0:0:0:1", &f
, &b
) >= 0);
524 assert_se(in_addr_is_multicast(f
, &b
) == 1);
526 assert_se(in_addr_from_string_auto("2001:db8::c:69b:aeff:fe53:743e", &f
, &b
) >= 0);
527 assert_se(in_addr_is_multicast(f
, &b
) == 0);
530 static void test_getpeercred_getpeergroups(void) {
533 log_info("/* %s */", __func__
);
535 r
= safe_fork("(getpeercred)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
539 static const gid_t gids
[] = { 3, 4, 5, 6, 7 };
547 if (geteuid() == 0) {
550 test_gids
= (gid_t
*) gids
;
551 n_test_gids
= ELEMENTSOF(gids
);
553 assert_se(setgroups(n_test_gids
, test_gids
) >= 0);
554 assert_se(setresgid(test_gid
, test_gid
, test_gid
) >= 0);
555 assert_se(setresuid(test_uid
, test_uid
, test_uid
) >= 0);
563 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
564 assert(ngroups_max
> 0);
566 test_gids
= newa(gid_t
, ngroups_max
);
568 r
= getgroups(ngroups_max
, test_gids
);
570 n_test_gids
= (size_t) r
;
573 assert_se(socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) >= 0);
575 assert_se(getpeercred(pair
[0], &ucred
) >= 0);
577 assert_se(ucred
.uid
== test_uid
);
578 assert_se(ucred
.gid
== test_gid
);
579 assert_se(ucred
.pid
== getpid_cached());
582 _cleanup_free_ gid_t
*peer_groups
= NULL
;
584 r
= getpeergroups(pair
[0], &peer_groups
);
585 assert_se(r
>= 0 || IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
));
588 assert_se((size_t) r
== n_test_gids
);
589 assert_se(memcmp(peer_groups
, test_gids
, sizeof(gid_t
) * n_test_gids
) == 0);
593 safe_close_pair(pair
);
598 static void test_passfd_read(void) {
599 static const char file_contents
[] = "test contents for passfd";
600 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
603 log_info("/* %s */", __func__
);
605 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
607 r
= safe_fork("(passfd_read)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
612 char tmpfile
[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
613 _cleanup_close_
int tmpfd
= -1;
615 pair
[0] = safe_close(pair
[0]);
617 tmpfd
= mkostemp_safe(tmpfile
);
618 assert_se(tmpfd
>= 0);
619 assert_se(write(tmpfd
, file_contents
, strlen(file_contents
)) == (ssize_t
) strlen(file_contents
));
620 tmpfd
= safe_close(tmpfd
);
622 tmpfd
= open(tmpfile
, O_RDONLY
);
623 assert_se(tmpfd
>= 0);
624 assert_se(unlink(tmpfile
) == 0);
626 assert_se(send_one_fd(pair
[1], tmpfd
, MSG_DONTWAIT
) == 0);
632 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
633 _cleanup_close_
int fd
= -1;
635 pair
[1] = safe_close(pair
[1]);
637 assert_se(receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
) == 0);
640 r
= read(fd
, buf
, sizeof(buf
)-1);
643 assert_se(streq(buf
, file_contents
));
646 static void test_passfd_contents_read(void) {
647 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
648 static const char file_contents
[] = "test contents in the file";
649 static const char wire_contents
[] = "test contents on the wire";
652 log_info("/* %s */", __func__
);
654 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
656 r
= safe_fork("(passfd_contents_read)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
661 struct iovec iov
= IOVEC_INIT_STRING(wire_contents
);
662 char tmpfile
[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
663 _cleanup_close_
int tmpfd
= -1;
665 pair
[0] = safe_close(pair
[0]);
667 tmpfd
= mkostemp_safe(tmpfile
);
668 assert_se(tmpfd
>= 0);
669 assert_se(write(tmpfd
, file_contents
, strlen(file_contents
)) == (ssize_t
) strlen(file_contents
));
670 tmpfd
= safe_close(tmpfd
);
672 tmpfd
= open(tmpfile
, O_RDONLY
);
673 assert_se(tmpfd
>= 0);
674 assert_se(unlink(tmpfile
) == 0);
676 assert_se(send_one_fd_iov(pair
[1], tmpfd
, &iov
, 1, MSG_DONTWAIT
) > 0);
682 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
683 _cleanup_close_
int fd
= -1;
686 pair
[1] = safe_close(pair
[1]);
688 k
= receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
);
691 assert_se(streq(buf
, wire_contents
));
694 r
= read(fd
, buf
, sizeof(buf
)-1);
697 assert_se(streq(buf
, file_contents
));
700 static void test_receive_nopassfd(void) {
701 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
702 static const char wire_contents
[] = "no fd passed here";
705 log_info("/* %s */", __func__
);
707 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
709 r
= safe_fork("(receive_nopassfd)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
714 struct iovec iov
= IOVEC_INIT_STRING(wire_contents
);
716 pair
[0] = safe_close(pair
[0]);
718 assert_se(send_one_fd_iov(pair
[1], -1, &iov
, 1, MSG_DONTWAIT
) > 0);
724 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
728 pair
[1] = safe_close(pair
[1]);
730 k
= receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
);
733 assert_se(streq(buf
, wire_contents
));
735 /* no fd passed here, confirm it was reset */
739 static void test_send_nodata_nofd(void) {
740 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
743 log_info("/* %s */", __func__
);
745 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
747 r
= safe_fork("(send_nodata_nofd)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
752 pair
[0] = safe_close(pair
[0]);
754 assert_se(send_one_fd_iov(pair
[1], -1, NULL
, 0, MSG_DONTWAIT
) == -EINVAL
);
760 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
764 pair
[1] = safe_close(pair
[1]);
766 k
= receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
);
767 /* recvmsg() will return errno EAGAIN if nothing was sent */
768 assert_se(k
== -EAGAIN
);
770 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
771 assert_se(fd
== -999);
774 static void test_send_emptydata(void) {
775 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
778 log_info("/* %s */", __func__
);
780 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
782 r
= safe_fork("(send_emptydata)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
787 struct iovec iov
= IOVEC_INIT_STRING(""); /* zero-length iov */
788 assert_se(iov
.iov_len
== 0);
790 pair
[0] = safe_close(pair
[0]);
792 /* This will succeed, since iov is set. */
793 assert_se(send_one_fd_iov(pair
[1], -1, &iov
, 1, MSG_DONTWAIT
) == 0);
799 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
803 pair
[1] = safe_close(pair
[1]);
805 k
= receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
);
806 /* receive_one_fd_iov() returns -EIO if an fd is not found and no data was returned. */
807 assert_se(k
== -EIO
);
809 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
810 assert_se(fd
== -999);
813 static void test_flush_accept(void) {
814 _cleanup_close_
int listen_stream
= -1, listen_dgram
= -1, listen_seqpacket
= 1, connect_stream
= -1, connect_dgram
= -1, connect_seqpacket
= -1;
815 static const union sockaddr_union sa
= { .un
.sun_family
= AF_UNIX
};
816 union sockaddr_union lsa
;
819 listen_stream
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
820 assert_se(listen_stream
>= 0);
822 listen_dgram
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
823 assert_se(listen_dgram
>= 0);
825 listen_seqpacket
= socket(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
826 assert_se(listen_seqpacket
>= 0);
828 assert_se(flush_accept(listen_stream
) < 0);
829 assert_se(flush_accept(listen_dgram
) < 0);
830 assert_se(flush_accept(listen_seqpacket
) < 0);
832 assert_se(bind(listen_stream
, &sa
.sa
, sizeof(sa_family_t
)) >= 0);
833 assert_se(bind(listen_dgram
, &sa
.sa
, sizeof(sa_family_t
)) >= 0);
834 assert_se(bind(listen_seqpacket
, &sa
.sa
, sizeof(sa_family_t
)) >= 0);
836 assert_se(flush_accept(listen_stream
) < 0);
837 assert_se(flush_accept(listen_dgram
) < 0);
838 assert_se(flush_accept(listen_seqpacket
) < 0);
840 assert_se(listen(listen_stream
, SOMAXCONN
) >= 0);
841 assert_se(listen(listen_dgram
, SOMAXCONN
) < 0);
842 assert_se(listen(listen_seqpacket
, SOMAXCONN
) >= 0);
844 assert_se(flush_accept(listen_stream
) >= 0);
845 assert_se(flush_accept(listen_dgram
) < 0);
846 assert_se(flush_accept(listen_seqpacket
) >= 0);
848 connect_stream
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
849 assert_se(connect_stream
>= 0);
851 connect_dgram
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
852 assert_se(connect_dgram
>= 0);
854 connect_seqpacket
= socket(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
855 assert_se(connect_seqpacket
>= 0);
858 assert_se(getsockname(listen_stream
, &lsa
.sa
, &l
) >= 0);
859 assert_se(connect(connect_stream
, &lsa
.sa
, l
) >= 0);
862 assert_se(getsockname(listen_dgram
, &lsa
.sa
, &l
) >= 0);
863 assert_se(connect(connect_dgram
, &lsa
.sa
, l
) >= 0);
866 assert_se(getsockname(listen_seqpacket
, &lsa
.sa
, &l
) >= 0);
867 assert_se(connect(connect_seqpacket
, &lsa
.sa
, l
) >= 0);
869 assert_se(flush_accept(listen_stream
) >= 0);
870 assert_se(flush_accept(listen_dgram
) < 0);
871 assert_se(flush_accept(listen_seqpacket
) >= 0);
874 int main(int argc
, char *argv
[]) {
875 test_setup_logging(LOG_DEBUG
);
879 test_socket_address_parse();
880 test_socket_print_unix();
881 test_socket_address_parse_netlink();
882 test_socket_address_equal();
883 test_socket_address_get_path();
884 test_socket_address_is();
885 test_socket_address_is_netlink();
887 test_in_addr_is_null();
888 test_in_addr_prefix_intersect();
889 test_in_addr_prefix_next();
890 test_in_addr_to_string();
891 test_in_addr_ifindex_to_string();
892 test_in_addr_ifindex_from_string_auto();
894 test_sockaddr_equal();
896 test_sockaddr_un_len();
898 test_in_addr_is_multicast();
900 test_getpeercred_getpeergroups();
903 test_passfd_contents_read();
904 test_receive_nopassfd();
905 test_send_nodata_nofd();
906 test_send_emptydata();