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