]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-socket-util.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / test / test-socket-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c182135d 2
ca78ad1d
ZJS
3#include <fcntl.h>
4#include <grp.h>
5#include <sys/stat.h>
43f2c88d
LP
6#include <sys/types.h>
7#include <unistd.h>
43f2c88d 8
b5efdb8a 9#include "alloc-util.h"
07630cea 10#include "async.h"
15dca371 11#include "escape.h"
8a3386ab 12#include "exit-status.h"
3ffd4af2 13#include "fd-util.h"
3b653205 14#include "in-addr-util.h"
8a3386ab 15#include "io-util.h"
059f6c42 16#include "log.h"
07630cea 17#include "macro.h"
dccca82b 18#include "process-util.h"
07630cea
LP
19#include "socket-util.h"
20#include "string-util.h"
6d7c4033 21#include "tests.h"
e4de7287 22#include "tmpfile-util.h"
c182135d 23
5c3fa98d
ZJS
24assert_cc(SUN_PATH_LEN == 108);
25
ef76dff2 26static void test_ifname_valid(void) {
041103a6
ZJS
27 log_info("/* %s */", __func__);
28
2313524a
ZJS
29 assert( ifname_valid("foo"));
30 assert( ifname_valid("eth0"));
ef76dff2
LP
31
32 assert(!ifname_valid("0"));
33 assert(!ifname_valid("99"));
2313524a
ZJS
34 assert( ifname_valid("a99"));
35 assert( ifname_valid("99a"));
ef76dff2
LP
36
37 assert(!ifname_valid(NULL));
38 assert(!ifname_valid(""));
39 assert(!ifname_valid(" "));
40 assert(!ifname_valid(" foo"));
41 assert(!ifname_valid("bar\n"));
42 assert(!ifname_valid("."));
43 assert(!ifname_valid(".."));
44 assert(ifname_valid("foo.bar"));
7ed95830 45 assert(!ifname_valid("x:y"));
ef76dff2 46
2313524a
ZJS
47 assert( ifname_valid_full("xxxxxxxxxxxxxxx", 0));
48 assert(!ifname_valid_full("xxxxxxxxxxxxxxxx", 0));
49 assert( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
50 assert( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
51 assert(!ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE));
52 assert( ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
53 assert(!ifname_valid_full("0", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
ef76dff2
LP
54}
55
15dca371
ZJS
56static void test_socket_print_unix_one(const char *in, size_t len_in, const char *expected) {
57 _cleanup_free_ char *out = NULL, *c = NULL;
58
58ce85f6 59 assert(len_in <= SUN_PATH_LEN);
15dca371
ZJS
60 SocketAddress a = { .sockaddr = { .un = { .sun_family = AF_UNIX } },
61 .size = offsetof(struct sockaddr_un, sun_path) + len_in,
62 .type = SOCK_STREAM,
63 };
64 memcpy(a.sockaddr.un.sun_path, in, len_in);
65
66 assert_se(socket_address_print(&a, &out) >= 0);
67 assert_se(c = cescape(in));
68 log_info("\"%s\" → \"%s\" (expect \"%s\")", in, out, expected);
69 assert_se(streq(out, expected));
70}
71
72static void test_socket_print_unix(void) {
73 log_info("/* %s */", __func__);
74
75 /* Some additional tests for abstract addresses which we don't parse */
76
77 test_socket_print_unix_one("\0\0\0\0", 4, "@\\000\\000\\000");
78 test_socket_print_unix_one("@abs", 5, "@abs");
79 test_socket_print_unix_one("\n", 2, "\\n");
80 test_socket_print_unix_one("", 1, "<unnamed>");
81 test_socket_print_unix_one("\0", 1, "<unnamed>");
82 test_socket_print_unix_one("\0_________________________there's 108 characters in this string_____________________________________________", 108,
83 "@_________________________there\\'s 108 characters in this string_____________________________________________");
84 test_socket_print_unix_one("////////////////////////////////////////////////////////////////////////////////////////////////////////////", 108,
85 "////////////////////////////////////////////////////////////////////////////////////////////////////////////");
15dca371
ZJS
86 test_socket_print_unix_one("\0\a\b\n\255", 6, "@\\a\\b\\n\\255\\000");
87}
88
43dc0043
RC
89static void test_sockaddr_equal(void) {
90 union sockaddr_union a = {
91 .in.sin_family = AF_INET,
92 .in.sin_port = 0,
8e38570e 93 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
43dc0043
RC
94 };
95 union sockaddr_union b = {
96 .in.sin_family = AF_INET,
97 .in.sin_port = 0,
8e38570e 98 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
43dc0043
RC
99 };
100 union sockaddr_union c = {
101 .in.sin_family = AF_INET,
102 .in.sin_port = 0,
8e38570e 103 .in.sin_addr.s_addr = htobe32(1234),
43dc0043
RC
104 };
105 union sockaddr_union d = {
106 .in6.sin6_family = AF_INET6,
107 .in6.sin6_port = 0,
108 .in6.sin6_addr = IN6ADDR_ANY_INIT,
109 };
0fc0f14b
SH
110 union sockaddr_union e = {
111 .vm.svm_family = AF_VSOCK,
112 .vm.svm_port = 0,
113 .vm.svm_cid = VMADDR_CID_ANY,
114 };
041103a6
ZJS
115
116 log_info("/* %s */", __func__);
117
43dc0043
RC
118 assert_se(sockaddr_equal(&a, &a));
119 assert_se(sockaddr_equal(&a, &b));
120 assert_se(sockaddr_equal(&d, &d));
0fc0f14b 121 assert_se(sockaddr_equal(&e, &e));
43dc0043
RC
122 assert_se(!sockaddr_equal(&a, &c));
123 assert_se(!sockaddr_equal(&b, &c));
0fc0f14b 124 assert_se(!sockaddr_equal(&a, &e));
43dc0043
RC
125}
126
fc2fffe7 127static void test_sockaddr_un_len(void) {
041103a6
ZJS
128 log_info("/* %s */", __func__);
129
fc2fffe7
LP
130 static const struct sockaddr_un fs = {
131 .sun_family = AF_UNIX,
132 .sun_path = "/foo/bar/waldo",
133 };
134
135 static const struct sockaddr_un abstract = {
136 .sun_family = AF_UNIX,
137 .sun_path = "\0foobar",
138 };
139
69995bff 140 assert_se(SOCKADDR_UN_LEN(fs) == offsetof(struct sockaddr_un, sun_path) + strlen(fs.sun_path) + 1);
fc2fffe7
LP
141 assert_se(SOCKADDR_UN_LEN(abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1));
142}
143
1703b203
SS
144static void test_in_addr_is_multicast(void) {
145 union in_addr_union a, b;
146 int f;
147
041103a6
ZJS
148 log_info("/* %s */", __func__);
149
1703b203
SS
150 assert_se(in_addr_from_string_auto("192.168.3.11", &f, &a) >= 0);
151 assert_se(in_addr_is_multicast(f, &a) == 0);
152
153 assert_se(in_addr_from_string_auto("224.0.0.1", &f, &a) >= 0);
154 assert_se(in_addr_is_multicast(f, &a) == 1);
155
156 assert_se(in_addr_from_string_auto("FF01:0:0:0:0:0:0:1", &f, &b) >= 0);
157 assert_se(in_addr_is_multicast(f, &b) == 1);
158
159 assert_se(in_addr_from_string_auto("2001:db8::c:69b:aeff:fe53:743e", &f, &b) >= 0);
160 assert_se(in_addr_is_multicast(f, &b) == 0);
161}
162
43f2c88d
LP
163static void test_getpeercred_getpeergroups(void) {
164 int r;
165
041103a6
ZJS
166 log_info("/* %s */", __func__);
167
43f2c88d
LP
168 r = safe_fork("(getpeercred)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
169 assert_se(r >= 0);
170
171 if (r == 0) {
172 static const gid_t gids[] = { 3, 4, 5, 6, 7 };
173 gid_t *test_gids;
43f2c88d
LP
174 size_t n_test_gids;
175 uid_t test_uid;
176 gid_t test_gid;
177 struct ucred ucred;
178 int pair[2];
179
180 if (geteuid() == 0) {
181 test_uid = 1;
182 test_gid = 2;
183 test_gids = (gid_t*) gids;
184 n_test_gids = ELEMENTSOF(gids);
185
186 assert_se(setgroups(n_test_gids, test_gids) >= 0);
187 assert_se(setresgid(test_gid, test_gid, test_gid) >= 0);
188 assert_se(setresuid(test_uid, test_uid, test_uid) >= 0);
189
190 } else {
191 long ngroups_max;
192
193 test_uid = getuid();
194 test_gid = getgid();
195
196 ngroups_max = sysconf(_SC_NGROUPS_MAX);
197 assert(ngroups_max > 0);
198
199 test_gids = newa(gid_t, ngroups_max);
200
201 r = getgroups(ngroups_max, test_gids);
202 assert_se(r >= 0);
203 n_test_gids = (size_t) r;
204 }
205
206 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
207
208 assert_se(getpeercred(pair[0], &ucred) >= 0);
209
210 assert_se(ucred.uid == test_uid);
211 assert_se(ucred.gid == test_gid);
212 assert_se(ucred.pid == getpid_cached());
213
181c4ba7
ZJS
214 {
215 _cleanup_free_ gid_t *peer_groups = NULL;
43f2c88d 216
181c4ba7
ZJS
217 r = getpeergroups(pair[0], &peer_groups);
218 assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
219
220 if (r >= 0) {
221 assert_se((size_t) r == n_test_gids);
222 assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
223 }
43f2c88d
LP
224 }
225
226 safe_close_pair(pair);
8a3386ab 227 _exit(EXIT_SUCCESS);
43f2c88d
LP
228 }
229}
230
8a3386ab
FB
231static void test_passfd_read(void) {
232 static const char file_contents[] = "test contents for passfd";
233 _cleanup_close_pair_ int pair[2] = { -1, -1 };
234 int r;
235
041103a6
ZJS
236 log_info("/* %s */", __func__);
237
8a3386ab
FB
238 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
239
240 r = safe_fork("(passfd_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
241 assert_se(r >= 0);
242
243 if (r == 0) {
244 /* Child */
245 char tmpfile[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
246 _cleanup_close_ int tmpfd = -1;
247
248 pair[0] = safe_close(pair[0]);
249
250 tmpfd = mkostemp_safe(tmpfile);
251 assert_se(tmpfd >= 0);
252 assert_se(write(tmpfd, file_contents, strlen(file_contents)) == (ssize_t) strlen(file_contents));
253 tmpfd = safe_close(tmpfd);
254
255 tmpfd = open(tmpfile, O_RDONLY);
256 assert_se(tmpfd >= 0);
257 assert_se(unlink(tmpfile) == 0);
258
259 assert_se(send_one_fd(pair[1], tmpfd, MSG_DONTWAIT) == 0);
260 _exit(EXIT_SUCCESS);
261 }
262
263 /* Parent */
264 char buf[64];
265 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
266 _cleanup_close_ int fd = -1;
267
268 pair[1] = safe_close(pair[1]);
269
270 assert_se(receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd) == 0);
271
272 assert_se(fd >= 0);
273 r = read(fd, buf, sizeof(buf)-1);
274 assert_se(r >= 0);
275 buf[r] = 0;
276 assert_se(streq(buf, file_contents));
277}
278
279static void test_passfd_contents_read(void) {
280 _cleanup_close_pair_ int pair[2] = { -1, -1 };
281 static const char file_contents[] = "test contents in the file";
282 static const char wire_contents[] = "test contents on the wire";
283 int r;
284
041103a6
ZJS
285 log_info("/* %s */", __func__);
286
8a3386ab
FB
287 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
288
289 r = safe_fork("(passfd_contents_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
290 assert_se(r >= 0);
291
292 if (r == 0) {
293 /* Child */
294 struct iovec iov = IOVEC_INIT_STRING(wire_contents);
295 char tmpfile[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
296 _cleanup_close_ int tmpfd = -1;
297
298 pair[0] = safe_close(pair[0]);
299
300 tmpfd = mkostemp_safe(tmpfile);
301 assert_se(tmpfd >= 0);
302 assert_se(write(tmpfd, file_contents, strlen(file_contents)) == (ssize_t) strlen(file_contents));
303 tmpfd = safe_close(tmpfd);
304
305 tmpfd = open(tmpfile, O_RDONLY);
306 assert_se(tmpfd >= 0);
307 assert_se(unlink(tmpfile) == 0);
308
309 assert_se(send_one_fd_iov(pair[1], tmpfd, &iov, 1, MSG_DONTWAIT) > 0);
310 _exit(EXIT_SUCCESS);
311 }
312
313 /* Parent */
314 char buf[64];
315 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
316 _cleanup_close_ int fd = -1;
317 ssize_t k;
318
319 pair[1] = safe_close(pair[1]);
320
321 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
322 assert_se(k > 0);
323 buf[k] = 0;
324 assert_se(streq(buf, wire_contents));
325
326 assert_se(fd >= 0);
327 r = read(fd, buf, sizeof(buf)-1);
328 assert_se(r >= 0);
329 buf[r] = 0;
330 assert_se(streq(buf, file_contents));
331}
332
333static void test_receive_nopassfd(void) {
334 _cleanup_close_pair_ int pair[2] = { -1, -1 };
335 static const char wire_contents[] = "no fd passed here";
336 int r;
337
041103a6
ZJS
338 log_info("/* %s */", __func__);
339
8a3386ab
FB
340 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
341
342 r = safe_fork("(receive_nopassfd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
343 assert_se(r >= 0);
344
345 if (r == 0) {
346 /* Child */
347 struct iovec iov = IOVEC_INIT_STRING(wire_contents);
348
349 pair[0] = safe_close(pair[0]);
350
351 assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) > 0);
352 _exit(EXIT_SUCCESS);
353 }
354
355 /* Parent */
356 char buf[64];
357 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
358 int fd = -999;
359 ssize_t k;
360
361 pair[1] = safe_close(pair[1]);
362
363 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
364 assert_se(k > 0);
365 buf[k] = 0;
366 assert_se(streq(buf, wire_contents));
367
368 /* no fd passed here, confirm it was reset */
369 assert_se(fd == -1);
370}
371
372static void test_send_nodata_nofd(void) {
373 _cleanup_close_pair_ int pair[2] = { -1, -1 };
374 int r;
375
041103a6
ZJS
376 log_info("/* %s */", __func__);
377
8a3386ab
FB
378 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
379
380 r = safe_fork("(send_nodata_nofd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
381 assert_se(r >= 0);
382
383 if (r == 0) {
384 /* Child */
385 pair[0] = safe_close(pair[0]);
386
387 assert_se(send_one_fd_iov(pair[1], -1, NULL, 0, MSG_DONTWAIT) == -EINVAL);
388 _exit(EXIT_SUCCESS);
389 }
390
391 /* Parent */
392 char buf[64];
393 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
394 int fd = -999;
395 ssize_t k;
396
397 pair[1] = safe_close(pair[1]);
398
399 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
400 /* recvmsg() will return errno EAGAIN if nothing was sent */
401 assert_se(k == -EAGAIN);
402
403 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
404 assert_se(fd == -999);
405}
406
407static void test_send_emptydata(void) {
408 _cleanup_close_pair_ int pair[2] = { -1, -1 };
409 int r;
410
041103a6
ZJS
411 log_info("/* %s */", __func__);
412
8a3386ab
FB
413 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
414
415 r = safe_fork("(send_emptydata)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
416 assert_se(r >= 0);
417
418 if (r == 0) {
419 /* Child */
420 struct iovec iov = IOVEC_INIT_STRING(""); /* zero-length iov */
421 assert_se(iov.iov_len == 0);
422
423 pair[0] = safe_close(pair[0]);
424
425 /* This will succeed, since iov is set. */
426 assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) == 0);
427 _exit(EXIT_SUCCESS);
428 }
429
430 /* Parent */
431 char buf[64];
432 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
433 int fd = -999;
434 ssize_t k;
435
436 pair[1] = safe_close(pair[1]);
437
438 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
439 /* receive_one_fd_iov() returns -EIO if an fd is not found and no data was returned. */
440 assert_se(k == -EIO);
441
442 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
443 assert_se(fd == -999);
444}
445
5b116c37
LP
446static void test_flush_accept(void) {
447 _cleanup_close_ int listen_stream = -1, listen_dgram = -1, listen_seqpacket = 1, connect_stream = -1, connect_dgram = -1, connect_seqpacket = -1;
448 static const union sockaddr_union sa = { .un.sun_family = AF_UNIX };
449 union sockaddr_union lsa;
450 socklen_t l;
451
452 listen_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
453 assert_se(listen_stream >= 0);
454
455 listen_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
456 assert_se(listen_dgram >= 0);
457
458 listen_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
459 assert_se(listen_seqpacket >= 0);
460
461 assert_se(flush_accept(listen_stream) < 0);
462 assert_se(flush_accept(listen_dgram) < 0);
463 assert_se(flush_accept(listen_seqpacket) < 0);
464
465 assert_se(bind(listen_stream, &sa.sa, sizeof(sa_family_t)) >= 0);
466 assert_se(bind(listen_dgram, &sa.sa, sizeof(sa_family_t)) >= 0);
467 assert_se(bind(listen_seqpacket, &sa.sa, sizeof(sa_family_t)) >= 0);
468
469 assert_se(flush_accept(listen_stream) < 0);
470 assert_se(flush_accept(listen_dgram) < 0);
471 assert_se(flush_accept(listen_seqpacket) < 0);
472
473 assert_se(listen(listen_stream, SOMAXCONN) >= 0);
474 assert_se(listen(listen_dgram, SOMAXCONN) < 0);
475 assert_se(listen(listen_seqpacket, SOMAXCONN) >= 0);
476
477 assert_se(flush_accept(listen_stream) >= 0);
478 assert_se(flush_accept(listen_dgram) < 0);
479 assert_se(flush_accept(listen_seqpacket) >= 0);
480
481 connect_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
482 assert_se(connect_stream >= 0);
483
484 connect_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
485 assert_se(connect_dgram >= 0);
486
487 connect_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
488 assert_se(connect_seqpacket >= 0);
489
490 l = sizeof(lsa);
491 assert_se(getsockname(listen_stream, &lsa.sa, &l) >= 0);
492 assert_se(connect(connect_stream, &lsa.sa, l) >= 0);
493
494 l = sizeof(lsa);
495 assert_se(getsockname(listen_dgram, &lsa.sa, &l) >= 0);
496 assert_se(connect(connect_dgram, &lsa.sa, l) >= 0);
497
498 l = sizeof(lsa);
499 assert_se(getsockname(listen_seqpacket, &lsa.sa, &l) >= 0);
500 assert_se(connect(connect_seqpacket, &lsa.sa, l) >= 0);
501
502 assert_se(flush_accept(listen_stream) >= 0);
503 assert_se(flush_accept(listen_dgram) < 0);
504 assert_se(flush_accept(listen_seqpacket) >= 0);
505}
506
c182135d 507int main(int argc, char *argv[]) {
6d7c4033 508 test_setup_logging(LOG_DEBUG);
059f6c42 509
ef76dff2 510 test_ifname_valid();
15dca371 511 test_socket_print_unix();
43dc0043 512 test_sockaddr_equal();
fc2fffe7 513 test_sockaddr_un_len();
1703b203 514 test_in_addr_is_multicast();
43f2c88d 515 test_getpeercred_getpeergroups();
8a3386ab
FB
516 test_passfd_read();
517 test_passfd_contents_read();
518 test_receive_nopassfd();
519 test_send_nodata_nofd();
520 test_send_emptydata();
5b116c37 521 test_flush_accept();
8a3386ab 522
059f6c42 523 return 0;
c182135d 524}