]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-socket-util.c
sd-bus: drop bloom stuff, it's not needed anymore since kdbus is gone
[thirdparty/systemd.git] / src / test / test-socket-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c182135d
RC
2/***
3 This file is part of systemd
4
5 Copyright 2014 Ronny Chevalier
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
43f2c88d
LP
21#include <sys/types.h>
22#include <unistd.h>
23#include <grp.h>
24
b5efdb8a 25#include "alloc-util.h"
07630cea 26#include "async.h"
3ffd4af2 27#include "fd-util.h"
3b653205 28#include "in-addr-util.h"
059f6c42 29#include "log.h"
07630cea
LP
30#include "macro.h"
31#include "socket-util.h"
32#include "string-util.h"
33#include "util.h"
c182135d 34
ef76dff2
LP
35static void test_ifname_valid(void) {
36 assert(ifname_valid("foo"));
37 assert(ifname_valid("eth0"));
38
39 assert(!ifname_valid("0"));
40 assert(!ifname_valid("99"));
41 assert(ifname_valid("a99"));
42 assert(ifname_valid("99a"));
43
44 assert(!ifname_valid(NULL));
45 assert(!ifname_valid(""));
46 assert(!ifname_valid(" "));
47 assert(!ifname_valid(" foo"));
48 assert(!ifname_valid("bar\n"));
49 assert(!ifname_valid("."));
50 assert(!ifname_valid(".."));
51 assert(ifname_valid("foo.bar"));
7ed95830 52 assert(!ifname_valid("x:y"));
ef76dff2
LP
53
54 assert(ifname_valid("xxxxxxxxxxxxxxx"));
55 assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
56}
57
c182135d
RC
58static void test_socket_address_parse(void) {
59 SocketAddress a;
60
61 assert_se(socket_address_parse(&a, "junk") < 0);
62 assert_se(socket_address_parse(&a, "192.168.1.1") < 0);
63 assert_se(socket_address_parse(&a, ".168.1.1") < 0);
64 assert_se(socket_address_parse(&a, "989.168.1.1") < 0);
65 assert_se(socket_address_parse(&a, "192.168.1.1:65536") < 0);
66 assert_se(socket_address_parse(&a, "192.168.1.1:0") < 0);
67 assert_se(socket_address_parse(&a, "0") < 0);
68 assert_se(socket_address_parse(&a, "65536") < 0);
69
70 assert_se(socket_address_parse(&a, "65535") >= 0);
71
4ebc62ec
MB
72 /* The checks below will pass even if ipv6 is disabled in
73 * kernel. The underlying glibc's inet_pton() is just a string
74 * parser and doesn't make any syscalls. */
75
76 assert_se(socket_address_parse(&a, "[::1]") < 0);
77 assert_se(socket_address_parse(&a, "[::1]8888") < 0);
78 assert_se(socket_address_parse(&a, "::1") < 0);
79 assert_se(socket_address_parse(&a, "[::1]:0") < 0);
80 assert_se(socket_address_parse(&a, "[::1]:65536") < 0);
81 assert_se(socket_address_parse(&a, "[a:b:1]:8888") < 0);
82
83 assert_se(socket_address_parse(&a, "8888") >= 0);
84 assert_se(a.sockaddr.sa.sa_family == (socket_ipv6_is_supported() ? AF_INET6 : AF_INET));
85
86 assert_se(socket_address_parse(&a, "[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:8888") >= 0);
87 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
88
89 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
90 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
c182135d
RC
91
92 assert_se(socket_address_parse(&a, "192.168.1.254:8888") >= 0);
93 assert_se(a.sockaddr.sa.sa_family == AF_INET);
94
95 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
96 assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
97
98 assert_se(socket_address_parse(&a, "@abstract") >= 0);
99 assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
0fc0f14b
SH
100
101 assert_se(socket_address_parse(&a, "vsock::1234") >= 0);
102 assert_se(a.sockaddr.sa.sa_family == AF_VSOCK);
103 assert_se(socket_address_parse(&a, "vsock:2:1234") >= 0);
104 assert_se(a.sockaddr.sa.sa_family == AF_VSOCK);
105 assert_se(socket_address_parse(&a, "vsock:2:1234x") < 0);
106 assert_se(socket_address_parse(&a, "vsock:2x:1234") < 0);
107 assert_se(socket_address_parse(&a, "vsock:2") < 0);
c182135d
RC
108}
109
110static void test_socket_address_parse_netlink(void) {
111 SocketAddress a;
112
113 assert_se(socket_address_parse_netlink(&a, "junk") < 0);
114 assert_se(socket_address_parse_netlink(&a, "") < 0);
115
116 assert_se(socket_address_parse_netlink(&a, "route") >= 0);
117 assert_se(socket_address_parse_netlink(&a, "route 10") >= 0);
118 assert_se(a.sockaddr.sa.sa_family == AF_NETLINK);
119 assert_se(a.protocol == NETLINK_ROUTE);
120}
121
122static void test_socket_address_equal(void) {
123 SocketAddress a;
124 SocketAddress b;
125
126 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
127 assert_se(socket_address_parse(&b, "192.168.1.1:888") >= 0);
128 assert_se(!socket_address_equal(&a, &b));
129
130 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
131 assert_se(socket_address_parse(&b, "192.16.1.1:8888") >= 0);
132 assert_se(!socket_address_equal(&a, &b));
133
134 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
135 assert_se(socket_address_parse(&b, "8888") >= 0);
136 assert_se(!socket_address_equal(&a, &b));
137
138 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
139 assert_se(socket_address_parse(&b, "/foo/bar/") >= 0);
140 assert_se(!socket_address_equal(&a, &b));
141
142 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
143 assert_se(socket_address_parse(&b, "192.168.1.1:8888") >= 0);
144 assert_se(socket_address_equal(&a, &b));
145
146 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
147 assert_se(socket_address_parse(&b, "/foo/bar") >= 0);
148 assert_se(socket_address_equal(&a, &b));
149
150 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
151 assert_se(socket_address_parse(&b, "[::1]:8888") >= 0);
152 assert_se(socket_address_equal(&a, &b));
153
154 assert_se(socket_address_parse(&a, "@abstract") >= 0);
155 assert_se(socket_address_parse(&b, "@abstract") >= 0);
156 assert_se(socket_address_equal(&a, &b));
157
158 assert_se(socket_address_parse_netlink(&a, "firewall") >= 0);
159 assert_se(socket_address_parse_netlink(&b, "firewall") >= 0);
160 assert_se(socket_address_equal(&a, &b));
0fc0f14b
SH
161
162 assert_se(socket_address_parse(&a, "vsock:2:1234") >= 0);
163 assert_se(socket_address_parse(&b, "vsock:2:1234") >= 0);
164 assert_se(socket_address_equal(&a, &b));
165 assert_se(socket_address_parse(&b, "vsock:2:1235") >= 0);
166 assert_se(!socket_address_equal(&a, &b));
167 assert_se(socket_address_parse(&b, "vsock:3:1234") >= 0);
168 assert_se(!socket_address_equal(&a, &b));
c182135d
RC
169}
170
171static void test_socket_address_get_path(void) {
172 SocketAddress a;
173
174 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
175 assert_se(!socket_address_get_path(&a));
176
177 assert_se(socket_address_parse(&a, "@abstract") >= 0);
178 assert_se(!socket_address_get_path(&a));
179
180 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
181 assert_se(!socket_address_get_path(&a));
182
183 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
184 assert_se(streq(socket_address_get_path(&a), "/foo/bar"));
0fc0f14b
SH
185
186 assert_se(socket_address_parse(&a, "vsock:2:1234") >= 0);
187 assert_se(!socket_address_get_path(&a));
c182135d
RC
188}
189
43dc0043
RC
190static void test_socket_address_is(void) {
191 SocketAddress a;
192
193 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
194 assert_se(socket_address_is(&a, "192.168.1.1:8888", SOCK_STREAM));
195 assert_se(!socket_address_is(&a, "route", SOCK_STREAM));
196 assert_se(!socket_address_is(&a, "192.168.1.1:8888", SOCK_RAW));
197}
198
199static void test_socket_address_is_netlink(void) {
200 SocketAddress a;
201
202 assert_se(socket_address_parse_netlink(&a, "route 10") >= 0);
203 assert_se(socket_address_is_netlink(&a, "route 10"));
204 assert_se(!socket_address_is_netlink(&a, "192.168.1.1:8888"));
205 assert_se(!socket_address_is_netlink(&a, "route 1"));
206}
207
e7639886
DM
208static void test_in_addr_is_null(void) {
209
210 union in_addr_union i = {};
211
212 assert_se(in_addr_is_null(AF_INET, &i) == true);
213 assert_se(in_addr_is_null(AF_INET6, &i) == true);
214
215 i.in.s_addr = 0x1000000;
216 assert_se(in_addr_is_null(AF_INET, &i) == false);
217 assert_se(in_addr_is_null(AF_INET6, &i) == false);
218
219 assert_se(in_addr_is_null(-1, &i) == -EAFNOSUPPORT);
220}
221
059f6c42
LP
222static void test_in_addr_prefix_intersect_one(unsigned f, const char *a, unsigned apl, const char *b, unsigned bpl, int result) {
223 union in_addr_union ua, ub;
224
225 assert_se(in_addr_from_string(f, a, &ua) >= 0);
226 assert_se(in_addr_from_string(f, b, &ub) >= 0);
227
228 assert_se(in_addr_prefix_intersect(f, &ua, apl, &ub, bpl) == result);
229}
230
231static void test_in_addr_prefix_intersect(void) {
232
233 test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 32, "255.255.255.254", 32, 0);
234 test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 0, "255.255.255.255", 32, 1);
235 test_in_addr_prefix_intersect_one(AF_INET, "0.0.0.0", 0, "47.11.8.15", 32, 1);
236
237 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.1", 24, 1);
238 test_in_addr_prefix_intersect_one(AF_INET, "2.2.2.2", 24, "1.1.1.1", 24, 0);
239
240 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 25, 1);
241 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 26, 1);
242 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.127", 25, 1);
243 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.255", 25, 0);
244
245 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);
246 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);
247 test_in_addr_prefix_intersect_one(AF_INET6, "::", 0, "beef:beef:beef:beef:beef:beef:beef:beef", 128, 1);
248
249 test_in_addr_prefix_intersect_one(AF_INET6, "1::2", 64, "1::2", 64, 1);
250 test_in_addr_prefix_intersect_one(AF_INET6, "2::2", 64, "1::2", 64, 0);
251
252 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 121, 1);
253 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 122, 1);
254 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::007f", 121, 1);
255 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::00ff", 121, 0);
256}
257
258static void test_in_addr_prefix_next_one(unsigned f, const char *before, unsigned pl, const char *after) {
259 union in_addr_union ubefore, uafter, t;
260
261 assert_se(in_addr_from_string(f, before, &ubefore) >= 0);
262
263 t = ubefore;
264 assert_se((in_addr_prefix_next(f, &t, pl) > 0) == !!after);
265
266 if (after) {
267 assert_se(in_addr_from_string(f, after, &uafter) >= 0);
268 assert_se(in_addr_equal(f, &t, &uafter) > 0);
269 }
270}
271
272static void test_in_addr_prefix_next(void) {
273
274 test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 24, "192.168.1.0");
275 test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 16, "192.169.0.0");
276 test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 20, "192.168.16.0");
277
278 test_in_addr_prefix_next_one(AF_INET, "0.0.0.0", 32, "0.0.0.1");
279 test_in_addr_prefix_next_one(AF_INET, "255.255.255.255", 32, NULL);
280 test_in_addr_prefix_next_one(AF_INET, "255.255.255.0", 24, NULL);
281
282 test_in_addr_prefix_next_one(AF_INET6, "4400::", 128, "4400::0001");
283 test_in_addr_prefix_next_one(AF_INET6, "4400::", 120, "4400::0100");
284 test_in_addr_prefix_next_one(AF_INET6, "4400::", 127, "4400::0002");
285 test_in_addr_prefix_next_one(AF_INET6, "4400::", 8, "4500::");
286 test_in_addr_prefix_next_one(AF_INET6, "4400::", 7, "4600::");
287
288 test_in_addr_prefix_next_one(AF_INET6, "::", 128, "::1");
289
290 test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, NULL);
291 test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", 120, NULL);
292
293}
294
07916bea
RC
295static void test_in_addr_to_string_one(int f, const char *addr) {
296 union in_addr_union ua;
297 _cleanup_free_ char *r = NULL;
298
299 assert_se(in_addr_from_string(f, addr, &ua) >= 0);
300 assert_se(in_addr_to_string(f, &ua, &r) >= 0);
301 printf("test_in_addr_to_string_one: %s == %s\n", addr, r);
302 assert_se(streq(addr, r));
303}
304
305static void test_in_addr_to_string(void) {
306 test_in_addr_to_string_one(AF_INET, "192.168.0.1");
307 test_in_addr_to_string_one(AF_INET, "10.11.12.13");
308 test_in_addr_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
309 test_in_addr_to_string_one(AF_INET6, "::1");
310 test_in_addr_to_string_one(AF_INET6, "fe80::");
311}
312
2817157b
LP
313static void test_in_addr_ifindex_to_string_one(int f, const char *a, int ifindex, const char *b) {
314 _cleanup_free_ char *r = NULL;
315 union in_addr_union ua, uuaa;
316 int ff, ifindex2;
317
318 assert_se(in_addr_from_string(f, a, &ua) >= 0);
319 assert_se(in_addr_ifindex_to_string(f, &ua, ifindex, &r) >= 0);
320 printf("test_in_addr_ifindex_to_string_one: %s == %s\n", b, r);
321 assert_se(streq(b, r));
322
323 assert_se(in_addr_ifindex_from_string_auto(b, &ff, &uuaa, &ifindex2) >= 0);
324 assert_se(ff == f);
325 assert_se(in_addr_equal(f, &ua, &uuaa));
326 assert_se(ifindex2 == ifindex || ifindex2 == 0);
327}
328
329static void test_in_addr_ifindex_to_string(void) {
330 test_in_addr_ifindex_to_string_one(AF_INET, "192.168.0.1", 7, "192.168.0.1");
331 test_in_addr_ifindex_to_string_one(AF_INET, "10.11.12.13", 9, "10.11.12.13");
332 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");
333 test_in_addr_ifindex_to_string_one(AF_INET6, "::1", 11, "::1");
334 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::", 12, "fe80::%12");
335 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::", 0, "fe80::");
336 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::14", 12, "fe80::14%12");
337 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::15", -7, "fe80::15");
338 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::16", LOOPBACK_IFINDEX, "fe80::16%1");
339}
340
341static void test_in_addr_ifindex_from_string_auto(void) {
342 int family, ifindex;
343 union in_addr_union ua;
344
345 /* Most in_addr_ifindex_from_string_auto() invocations have already been tested above, but let's test some more */
346
347 assert_se(in_addr_ifindex_from_string_auto("fe80::17", &family, &ua, &ifindex) >= 0);
348 assert_se(family == AF_INET6);
349 assert_se(ifindex == 0);
350
351 assert_se(in_addr_ifindex_from_string_auto("fe80::18%19", &family, &ua, &ifindex) >= 0);
352 assert_se(family == AF_INET6);
353 assert_se(ifindex == 19);
354
355 assert_se(in_addr_ifindex_from_string_auto("fe80::18%lo", &family, &ua, &ifindex) >= 0);
356 assert_se(family == AF_INET6);
357 assert_se(ifindex == LOOPBACK_IFINDEX);
358
359 assert_se(in_addr_ifindex_from_string_auto("fe80::19%thisinterfacecantexist", &family, &ua, &ifindex) == -ENODEV);
360}
361
b31f535c
ZJS
362static void *connect_thread(void *arg) {
363 union sockaddr_union *sa = arg;
364 _cleanup_close_ int fd = -1;
365
366 fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
bdf7026e 367 assert_se(fd >= 0);
b31f535c
ZJS
368
369 assert_se(connect(fd, &sa->sa, sizeof(sa->in)) == 0);
370
371 return NULL;
372}
373
374static void test_nameinfo_pretty(void) {
eda8090b 375 _cleanup_free_ char *stdin_name = NULL, *localhost = NULL;
b31f535c
ZJS
376
377 union sockaddr_union s = {
378 .in.sin_family = AF_INET,
379 .in.sin_port = 0,
8e38570e 380 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
b31f535c
ZJS
381 };
382 int r;
383
384 union sockaddr_union c = {};
385 socklen_t slen = sizeof(c.in), clen = sizeof(c.in);
386
eda8090b
TA
387 _cleanup_close_ int sfd = -1, cfd = -1;
388 r = getnameinfo_pretty(STDIN_FILENO, &stdin_name);
da927ba9 389 log_info_errno(r, "No connection remote: %m");
b31f535c
ZJS
390
391 assert_se(r < 0);
392
393 sfd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
bdf7026e 394 assert_se(sfd >= 0);
b31f535c
ZJS
395
396 assert_se(bind(sfd, &s.sa, sizeof(s.in)) == 0);
397
398 /* find out the port number */
399 assert_se(getsockname(sfd, &s.sa, &slen) == 0);
400
401 assert_se(listen(sfd, 1) == 0);
402
403 assert_se(asynchronous_job(connect_thread, &s) == 0);
404
405 log_debug("Accepting new connection on fd:%d", sfd);
406 cfd = accept4(sfd, &c.sa, &clen, SOCK_CLOEXEC);
bdf7026e 407 assert_se(cfd >= 0);
b31f535c
ZJS
408
409 r = getnameinfo_pretty(cfd, &localhost);
410 log_info("Connection from %s", localhost);
bdf7026e 411 assert_se(r == 0);
b31f535c
ZJS
412}
413
43dc0043
RC
414static void test_sockaddr_equal(void) {
415 union sockaddr_union a = {
416 .in.sin_family = AF_INET,
417 .in.sin_port = 0,
8e38570e 418 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
43dc0043
RC
419 };
420 union sockaddr_union b = {
421 .in.sin_family = AF_INET,
422 .in.sin_port = 0,
8e38570e 423 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
43dc0043
RC
424 };
425 union sockaddr_union c = {
426 .in.sin_family = AF_INET,
427 .in.sin_port = 0,
8e38570e 428 .in.sin_addr.s_addr = htobe32(1234),
43dc0043
RC
429 };
430 union sockaddr_union d = {
431 .in6.sin6_family = AF_INET6,
432 .in6.sin6_port = 0,
433 .in6.sin6_addr = IN6ADDR_ANY_INIT,
434 };
0fc0f14b
SH
435 union sockaddr_union e = {
436 .vm.svm_family = AF_VSOCK,
437 .vm.svm_port = 0,
438 .vm.svm_cid = VMADDR_CID_ANY,
439 };
43dc0043
RC
440 assert_se(sockaddr_equal(&a, &a));
441 assert_se(sockaddr_equal(&a, &b));
442 assert_se(sockaddr_equal(&d, &d));
0fc0f14b 443 assert_se(sockaddr_equal(&e, &e));
43dc0043
RC
444 assert_se(!sockaddr_equal(&a, &c));
445 assert_se(!sockaddr_equal(&b, &c));
0fc0f14b 446 assert_se(!sockaddr_equal(&a, &e));
43dc0043
RC
447}
448
fc2fffe7
LP
449static void test_sockaddr_un_len(void) {
450 static const struct sockaddr_un fs = {
451 .sun_family = AF_UNIX,
452 .sun_path = "/foo/bar/waldo",
453 };
454
455 static const struct sockaddr_un abstract = {
456 .sun_family = AF_UNIX,
457 .sun_path = "\0foobar",
458 };
459
460 assert_se(SOCKADDR_UN_LEN(fs) == offsetof(struct sockaddr_un, sun_path) + strlen(fs.sun_path));
461 assert_se(SOCKADDR_UN_LEN(abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1));
462}
463
1703b203
SS
464static void test_in_addr_is_multicast(void) {
465 union in_addr_union a, b;
466 int f;
467
468 assert_se(in_addr_from_string_auto("192.168.3.11", &f, &a) >= 0);
469 assert_se(in_addr_is_multicast(f, &a) == 0);
470
471 assert_se(in_addr_from_string_auto("224.0.0.1", &f, &a) >= 0);
472 assert_se(in_addr_is_multicast(f, &a) == 1);
473
474 assert_se(in_addr_from_string_auto("FF01:0:0:0:0:0:0:1", &f, &b) >= 0);
475 assert_se(in_addr_is_multicast(f, &b) == 1);
476
477 assert_se(in_addr_from_string_auto("2001:db8::c:69b:aeff:fe53:743e", &f, &b) >= 0);
478 assert_se(in_addr_is_multicast(f, &b) == 0);
479}
480
43f2c88d
LP
481static void test_getpeercred_getpeergroups(void) {
482 int r;
483
484 r = safe_fork("(getpeercred)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
485 assert_se(r >= 0);
486
487 if (r == 0) {
488 static const gid_t gids[] = { 3, 4, 5, 6, 7 };
489 gid_t *test_gids;
78e2f089 490 _cleanup_free_ gid_t *peer_groups = NULL;
43f2c88d
LP
491 size_t n_test_gids;
492 uid_t test_uid;
493 gid_t test_gid;
494 struct ucred ucred;
495 int pair[2];
496
497 if (geteuid() == 0) {
498 test_uid = 1;
499 test_gid = 2;
500 test_gids = (gid_t*) gids;
501 n_test_gids = ELEMENTSOF(gids);
502
503 assert_se(setgroups(n_test_gids, test_gids) >= 0);
504 assert_se(setresgid(test_gid, test_gid, test_gid) >= 0);
505 assert_se(setresuid(test_uid, test_uid, test_uid) >= 0);
506
507 } else {
508 long ngroups_max;
509
510 test_uid = getuid();
511 test_gid = getgid();
512
513 ngroups_max = sysconf(_SC_NGROUPS_MAX);
514 assert(ngroups_max > 0);
515
516 test_gids = newa(gid_t, ngroups_max);
517
518 r = getgroups(ngroups_max, test_gids);
519 assert_se(r >= 0);
520 n_test_gids = (size_t) r;
521 }
522
523 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
524
525 assert_se(getpeercred(pair[0], &ucred) >= 0);
526
527 assert_se(ucred.uid == test_uid);
528 assert_se(ucred.gid == test_gid);
529 assert_se(ucred.pid == getpid_cached());
530
531 r = getpeergroups(pair[0], &peer_groups);
532 assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
533
534 if (r >= 0) {
535 assert_se((size_t) r == n_test_gids);
536 assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
537 }
538
539 safe_close_pair(pair);
540 }
541}
542
c182135d 543int main(int argc, char *argv[]) {
059f6c42
LP
544
545 log_set_max_level(LOG_DEBUG);
546
ef76dff2
LP
547 test_ifname_valid();
548
c182135d
RC
549 test_socket_address_parse();
550 test_socket_address_parse_netlink();
551 test_socket_address_equal();
552 test_socket_address_get_path();
43dc0043
RC
553 test_socket_address_is();
554 test_socket_address_is_netlink();
059f6c42 555
e7639886 556 test_in_addr_is_null();
059f6c42
LP
557 test_in_addr_prefix_intersect();
558 test_in_addr_prefix_next();
07916bea 559 test_in_addr_to_string();
2817157b
LP
560 test_in_addr_ifindex_to_string();
561 test_in_addr_ifindex_from_string_auto();
059f6c42 562
b31f535c
ZJS
563 test_nameinfo_pretty();
564
43dc0043
RC
565 test_sockaddr_equal();
566
fc2fffe7
LP
567 test_sockaddr_un_len();
568
1703b203
SS
569 test_in_addr_is_multicast();
570
43f2c88d
LP
571 test_getpeercred_getpeergroups();
572
059f6c42 573 return 0;
c182135d 574}