]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/test/test-socket-util.c
io.systemd.Unit.List fix context/runtime split (#38172)
[thirdparty/systemd.git] / src / test / test-socket-util.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include <fcntl.h>
4#include <grp.h>
5#include <unistd.h>
6
7#include "alloc-util.h"
8#include "escape.h"
9#include "fd-util.h"
10#include "fs-util.h"
11#include "in-addr-util.h"
12#include "iovec-util.h"
13#include "log.h"
14#include "path-util.h"
15#include "pidref.h"
16#include "process-util.h"
17#include "random-util.h"
18#include "rm-rf.h"
19#include "socket-util.h"
20#include "tests.h"
21#include "tmpfile-util.h"
22#include "user-util.h"
23
24assert_cc(SUN_PATH_LEN == 108);
25
26TEST(ifname_valid) {
27 assert_se( ifname_valid("foo"));
28 assert_se( ifname_valid("eth0"));
29
30 assert_se(!ifname_valid("0"));
31 assert_se(!ifname_valid("99"));
32 assert_se( ifname_valid("a99"));
33 assert_se( ifname_valid("99a"));
34
35 assert_se(!ifname_valid(NULL));
36 assert_se(!ifname_valid(""));
37 assert_se(!ifname_valid(" "));
38 assert_se(!ifname_valid(" foo"));
39 assert_se(!ifname_valid("bar\n"));
40 assert_se(!ifname_valid("."));
41 assert_se(!ifname_valid(".."));
42 assert_se(ifname_valid("foo.bar"));
43 assert_se(!ifname_valid("x:y"));
44
45 assert_se( ifname_valid_full("xxxxxxxxxxxxxxx", 0));
46 assert_se(!ifname_valid_full("xxxxxxxxxxxxxxxx", 0));
47 assert_se( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
48 assert_se( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
49 assert_se(!ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE));
50 assert_se( ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
51 assert_se(!ifname_valid_full("0", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
52}
53
54static void test_socket_print_unix_one(const char *in, size_t len_in, const char *expected) {
55 _cleanup_free_ char *out = NULL, *c = NULL;
56
57 assert_se(len_in <= SUN_PATH_LEN);
58 SocketAddress a = { .sockaddr = { .un = { .sun_family = AF_UNIX } },
59 .size = offsetof(struct sockaddr_un, sun_path) + len_in,
60 .type = SOCK_STREAM,
61 };
62 memcpy(a.sockaddr.un.sun_path, in, len_in);
63
64 assert_se(socket_address_print(&a, &out) >= 0);
65 assert_se(c = cescape(in));
66 log_info("\"%s\" → \"%s\" (expect \"%s\")", in, out, expected);
67 ASSERT_STREQ(out, expected);
68}
69
70TEST(socket_print_unix) {
71 /* Some additional tests for abstract addresses which we don't parse */
72
73 test_socket_print_unix_one("\0\0\0\0", 4, "@\\000\\000\\000");
74 test_socket_print_unix_one("@abs", 5, "@abs");
75 test_socket_print_unix_one("\n", 2, "\\n");
76 test_socket_print_unix_one("", 1, "<unnamed>");
77 test_socket_print_unix_one("\0", 1, "<unnamed>");
78 test_socket_print_unix_one("\0_________________________there's 108 characters in this string_____________________________________________", 108,
79 "@_________________________there\\'s 108 characters in this string_____________________________________________");
80 test_socket_print_unix_one("////////////////////////////////////////////////////////////////////////////////////////////////////////////", 108,
81 "////////////////////////////////////////////////////////////////////////////////////////////////////////////");
82 test_socket_print_unix_one("\0\a\b\n\255", 6, "@\\a\\b\\n\\255\\000");
83}
84
85TEST(sockaddr_equal) {
86 union sockaddr_union a = {
87 .in.sin_family = AF_INET,
88 .in.sin_port = 0,
89 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
90 };
91 union sockaddr_union b = {
92 .in.sin_family = AF_INET,
93 .in.sin_port = 0,
94 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
95 };
96 union sockaddr_union c = {
97 .in.sin_family = AF_INET,
98 .in.sin_port = 0,
99 .in.sin_addr.s_addr = htobe32(1234),
100 };
101 union sockaddr_union d = {
102 .in6.sin6_family = AF_INET6,
103 .in6.sin6_port = 0,
104 .in6.sin6_addr = IN6ADDR_ANY_INIT,
105 };
106 union sockaddr_union e = {
107 .vm.svm_family = AF_VSOCK,
108 .vm.svm_port = 0,
109 .vm.svm_cid = VMADDR_CID_ANY,
110 };
111
112 assert_se(sockaddr_equal(&a, &a));
113 assert_se(sockaddr_equal(&a, &b));
114 assert_se(sockaddr_equal(&d, &d));
115 assert_se(sockaddr_equal(&e, &e));
116 assert_se(!sockaddr_equal(&a, &c));
117 assert_se(!sockaddr_equal(&b, &c));
118 assert_se(!sockaddr_equal(&a, &e));
119}
120
121TEST(sockaddr_un_len) {
122 static const struct sockaddr_un fs = {
123 .sun_family = AF_UNIX,
124 .sun_path = "/foo/bar/waldo",
125 };
126
127 static const struct sockaddr_un abstract = {
128 .sun_family = AF_UNIX,
129 .sun_path = "\0foobar",
130 };
131
132 assert_se(sockaddr_un_len(&fs) == offsetof(struct sockaddr_un, sun_path) + strlen(fs.sun_path) + 1);
133 assert_se(sockaddr_un_len(&abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1));
134}
135
136TEST(in_addr_is_multicast) {
137 union in_addr_union a, b;
138 int f;
139
140 assert_se(in_addr_from_string_auto("192.168.3.11", &f, &a) >= 0);
141 assert_se(in_addr_is_multicast(f, &a) == 0);
142
143 assert_se(in_addr_from_string_auto("224.0.0.1", &f, &a) >= 0);
144 assert_se(in_addr_is_multicast(f, &a) == 1);
145
146 assert_se(in_addr_from_string_auto("FF01:0:0:0:0:0:0:1", &f, &b) >= 0);
147 assert_se(in_addr_is_multicast(f, &b) == 1);
148
149 assert_se(in_addr_from_string_auto("2001:db8::c:69b:aeff:fe53:743e", &f, &b) >= 0);
150 assert_se(in_addr_is_multicast(f, &b) == 0);
151}
152
153TEST(getpeercred_getpeergroups) {
154 int r;
155
156 r = safe_fork("(getpeercred)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
157 assert_se(r >= 0);
158
159 if (r == 0) {
160 static const gid_t gids[] = { 3, 4, 5, 6, 7 };
161 gid_t *test_gids;
162 size_t n_test_gids;
163 uid_t test_uid;
164 gid_t test_gid;
165 struct ucred ucred;
166 int pair[2] = EBADF_PAIR;
167
168 if (geteuid() == 0 && !userns_has_single_user()) {
169 test_uid = 1;
170 test_gid = 2;
171 test_gids = (gid_t*) gids;
172 n_test_gids = ELEMENTSOF(gids);
173
174 assert_se(fully_set_uid_gid(test_uid, test_gid, test_gids, n_test_gids) >= 0);
175 } else {
176 long ngroups_max;
177
178 test_uid = getuid();
179 test_gid = getgid();
180
181 ngroups_max = sysconf(_SC_NGROUPS_MAX);
182 assert_se(ngroups_max > 0);
183
184 test_gids = newa(gid_t, ngroups_max);
185
186 r = getgroups(ngroups_max, test_gids);
187 assert_se(r >= 0);
188 n_test_gids = (size_t) r;
189 }
190
191 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
192
193 assert_se(getpeercred(pair[0], &ucred) >= 0);
194
195 assert_se(ucred.uid == test_uid);
196 assert_se(ucred.gid == test_gid);
197 assert_se(ucred.pid == getpid_cached());
198
199 {
200 _cleanup_free_ gid_t *peer_groups = NULL;
201
202 r = getpeergroups(pair[0], &peer_groups);
203 assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
204
205 if (r >= 0) {
206 assert_se((size_t) r == n_test_gids);
207 assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
208 }
209 }
210
211 safe_close_pair(pair);
212 _exit(EXIT_SUCCESS);
213 }
214}
215
216TEST(passfd_read) {
217 static const char file_contents[] = "test contents for passfd";
218 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
219 int r;
220
221 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
222
223 r = safe_fork("(passfd_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
224 assert_se(r >= 0);
225
226 if (r == 0) {
227 /* Child */
228 pair[0] = safe_close(pair[0]);
229
230 char tmpfile[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
231 assert_se(write_tmpfile(tmpfile, file_contents) == 0);
232
233 _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
234 assert_se(tmpfd >= 0);
235 assert_se(unlink(tmpfile) == 0);
236
237 assert_se(send_one_fd(pair[1], tmpfd, MSG_DONTWAIT) == 0);
238 _exit(EXIT_SUCCESS);
239 }
240
241 /* Parent */
242 char buf[64];
243 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
244 _cleanup_close_ int fd = -EBADF;
245
246 pair[1] = safe_close(pair[1]);
247
248 assert_se(receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd) == 0);
249
250 assert_se(fd >= 0);
251 ssize_t n = read(fd, buf, sizeof(buf)-1);
252 assert_se(n >= 0);
253 buf[n] = 0;
254 ASSERT_STREQ(buf, file_contents);
255}
256
257TEST(passfd_contents_read) {
258 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
259 static const char file_contents[] = "test contents in the file";
260 static const char wire_contents[] = "test contents on the wire";
261 int r;
262
263 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
264
265 r = safe_fork("(passfd_contents_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
266 assert_se(r >= 0);
267
268 if (r == 0) {
269 /* Child */
270 struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
271 char tmpfile[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
272
273 pair[0] = safe_close(pair[0]);
274
275 assert_se(write_tmpfile(tmpfile, file_contents) == 0);
276
277 _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
278 assert_se(tmpfd >= 0);
279 assert_se(unlink(tmpfile) == 0);
280
281 assert_se(send_one_fd_iov(pair[1], tmpfd, &iov, 1, MSG_DONTWAIT) > 0);
282 _exit(EXIT_SUCCESS);
283 }
284
285 /* Parent */
286 char buf[64];
287 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
288 _cleanup_close_ int fd = -EBADF;
289 ssize_t k;
290
291 pair[1] = safe_close(pair[1]);
292
293 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
294 assert_se(k > 0);
295 buf[k] = 0;
296 ASSERT_STREQ(buf, wire_contents);
297
298 assert_se(fd >= 0);
299 r = read(fd, buf, sizeof(buf)-1);
300 assert_se(r >= 0);
301 buf[r] = 0;
302 ASSERT_STREQ(buf, file_contents);
303}
304
305TEST(pass_many_fds_contents_read) {
306 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
307 static const char file_contents[][STRLEN("test contents in the fileX") + 1] = {
308 "test contents in the file0",
309 "test contents in the file1",
310 "test contents in the file2"
311 };
312 static const char wire_contents[] = "test contents on the wire";
313 int r;
314
315 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
316
317 r = safe_fork("(passfd_contents_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
318 assert_se(r >= 0);
319
320 if (r == 0) {
321 /* Child */
322 struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
323 char tmpfile[][STRLEN("/tmp/test-socket-util-passfd-contents-read-XXXXXX") + 1] = {
324 "/tmp/test-socket-util-passfd-contents-read-XXXXXX",
325 "/tmp/test-socket-util-passfd-contents-read-XXXXXX",
326 "/tmp/test-socket-util-passfd-contents-read-XXXXXX"
327 };
328 int tmpfds[3] = EBADF_TRIPLET;
329
330 pair[0] = safe_close(pair[0]);
331
332 for (size_t i = 0; i < 3; ++i) {
333 assert_se(write_tmpfile(tmpfile[i], file_contents[i]) == 0);
334 tmpfds[i] = open(tmpfile[i], O_RDONLY);
335 assert_se(tmpfds[i] >= 0);
336 assert_se(unlink(tmpfile[i]) == 0);
337 }
338
339 assert_se(send_many_fds_iov(pair[1], tmpfds, 3, &iov, 1, MSG_DONTWAIT) > 0);
340 close_many(tmpfds, 3);
341 _exit(EXIT_SUCCESS);
342 }
343
344 /* Parent */
345 char buf[64];
346 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
347 _cleanup_free_ int *fds = NULL;
348 size_t n_fds = 0;
349 ssize_t k;
350
351 pair[1] = safe_close(pair[1]);
352
353 k = receive_many_fds_iov(pair[0], &iov, 1, &fds, &n_fds, MSG_DONTWAIT);
354 assert_se(k > 0);
355 buf[k] = 0;
356 ASSERT_STREQ(buf, wire_contents);
357
358 assert_se(n_fds == 3);
359
360 for (size_t i = 0; i < 3; ++i) {
361 assert_se(fds[i] >= 0);
362 r = read(fds[i], buf, sizeof(buf)-1);
363 assert_se(r >= 0);
364 buf[r] = 0;
365 ASSERT_STREQ(buf, file_contents[i]);
366 safe_close(fds[i]);
367 }
368}
369
370TEST(receive_nopassfd) {
371 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
372 static const char wire_contents[] = "no fd passed here";
373 int r;
374
375 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
376
377 r = safe_fork("(receive_nopassfd)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
378 assert_se(r >= 0);
379
380 if (r == 0) {
381 /* Child */
382 struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
383
384 pair[0] = safe_close(pair[0]);
385
386 assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) > 0);
387 _exit(EXIT_SUCCESS);
388 }
389
390 /* Parent */
391 char buf[64];
392 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
393 int fd = -999;
394 ssize_t k;
395
396 pair[1] = safe_close(pair[1]);
397
398 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
399 assert_se(k > 0);
400 buf[k] = 0;
401 ASSERT_STREQ(buf, wire_contents);
402
403 /* no fd passed here, confirm it was reset */
404 assert_se(fd == -EBADF);
405}
406
407TEST(send_nodata_nofd) {
408 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
409 int r;
410
411 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
412
413 r = safe_fork("(send_nodata_nofd)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
414 assert_se(r >= 0);
415
416 if (r == 0) {
417 /* Child */
418 pair[0] = safe_close(pair[0]);
419
420 assert_se(send_one_fd_iov(pair[1], -1, NULL, 0, MSG_DONTWAIT) == -EINVAL);
421 _exit(EXIT_SUCCESS);
422 }
423
424 /* Parent */
425 char buf[64];
426 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
427 int fd = -999;
428 ssize_t k;
429
430 pair[1] = safe_close(pair[1]);
431
432 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
433 /* recvmsg() will return errno EAGAIN if nothing was sent */
434 assert_se(k == -EAGAIN);
435
436 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
437 assert_se(fd == -999);
438}
439
440TEST(send_emptydata) {
441 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
442 int r;
443
444 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
445
446 r = safe_fork("(send_emptydata)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
447 assert_se(r >= 0);
448
449 if (r == 0) {
450 /* Child */
451 pair[0] = safe_close(pair[0]);
452
453 /* This will succeed, since iov is set. */
454 assert_se(send_one_fd_iov(pair[1], -1, &iovec_empty, 1, MSG_DONTWAIT) == 0);
455 _exit(EXIT_SUCCESS);
456 }
457
458 /* Parent */
459 char buf[64];
460 struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
461 int fd = -999;
462 ssize_t k;
463
464 pair[1] = safe_close(pair[1]);
465
466 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
467 /* receive_one_fd_iov() returns -EIO if an fd is not found and no data was returned. */
468 assert_se(k == -EIO);
469
470 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
471 assert_se(fd == -999);
472}
473
474TEST(flush_accept) {
475 _cleanup_close_ int listen_stream, listen_dgram, listen_seqpacket, connect_stream, connect_dgram, connect_seqpacket;
476 static const union sockaddr_union sa = { .un.sun_family = AF_UNIX };
477 union sockaddr_union lsa;
478 socklen_t l;
479
480 listen_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
481 assert_se(listen_stream >= 0);
482
483 listen_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
484 assert_se(listen_dgram >= 0);
485
486 listen_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
487 assert_se(listen_seqpacket >= 0);
488
489 assert_se(flush_accept(listen_stream) < 0);
490 assert_se(flush_accept(listen_dgram) < 0);
491 assert_se(flush_accept(listen_seqpacket) < 0);
492
493 assert_se(bind(listen_stream, &sa.sa, sizeof(sa_family_t)) >= 0);
494 assert_se(bind(listen_dgram, &sa.sa, sizeof(sa_family_t)) >= 0);
495 assert_se(bind(listen_seqpacket, &sa.sa, sizeof(sa_family_t)) >= 0);
496
497 assert_se(flush_accept(listen_stream) < 0);
498 assert_se(flush_accept(listen_dgram) < 0);
499 assert_se(flush_accept(listen_seqpacket) < 0);
500
501 assert_se(listen(listen_stream, SOMAXCONN_DELUXE) >= 0);
502 assert_se(listen(listen_dgram, SOMAXCONN_DELUXE) < 0);
503 assert_se(listen(listen_seqpacket, SOMAXCONN_DELUXE) >= 0);
504
505 assert_se(flush_accept(listen_stream) >= 0);
506 assert_se(flush_accept(listen_dgram) < 0);
507 assert_se(flush_accept(listen_seqpacket) >= 0);
508
509 connect_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
510 assert_se(connect_stream >= 0);
511
512 connect_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
513 assert_se(connect_dgram >= 0);
514
515 connect_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
516 assert_se(connect_seqpacket >= 0);
517
518 l = sizeof(lsa);
519 assert_se(getsockname(listen_stream, &lsa.sa, &l) >= 0);
520 assert_se(connect(connect_stream, &lsa.sa, l) >= 0);
521
522 l = sizeof(lsa);
523 assert_se(getsockname(listen_dgram, &lsa.sa, &l) >= 0);
524 assert_se(connect(connect_dgram, &lsa.sa, l) >= 0);
525
526 l = sizeof(lsa);
527 assert_se(getsockname(listen_seqpacket, &lsa.sa, &l) >= 0);
528 assert_se(connect(connect_seqpacket, &lsa.sa, l) >= 0);
529
530 assert_se(flush_accept(listen_stream) >= 0);
531 assert_se(flush_accept(listen_dgram) < 0);
532 assert_se(flush_accept(listen_seqpacket) >= 0);
533}
534
535TEST(ipv6_enabled) {
536 log_info("IPv6 supported: %s", yes_no(socket_ipv6_is_supported()));
537 log_info("IPv6 enabled: %s", yes_no(socket_ipv6_is_enabled()));
538}
539
540TEST(sockaddr_un_set_path) {
541 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
542 _cleanup_(unlink_and_freep) char *sh = NULL;
543 _cleanup_free_ char *j = NULL;
544 union sockaddr_union sa;
545 _cleanup_close_ int fd1, fd2, fd3;
546
547 assert_se(mkdtemp_malloc("/tmp/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXX", &t) >= 0);
548 assert_se(strlen(t) > SUN_PATH_LEN);
549
550 assert_se(j = path_join(t, "sock"));
551 assert_se(sockaddr_un_set_path(&sa.un, j) == -ENAMETOOLONG); /* too long for AF_UNIX socket */
552
553 assert_se(asprintf(&sh, "/tmp/%" PRIx64, random_u64()) >= 0);
554 assert_se(symlink(t, sh) >= 0); /* create temporary symlink, to access it anyway */
555
556 free(j);
557 assert_se(j = path_join(sh, "sock"));
558 assert_se(sockaddr_un_set_path(&sa.un, j) >= 0);
559
560 fd1 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
561 assert_se(fd1 >= 0);
562 assert_se(bind(fd1, &sa.sa, sockaddr_len(&sa)) >= 0);
563 assert_se(listen(fd1, 1) >= 0);
564
565 sh = unlink_and_free(sh); /* remove temporary symlink */
566
567 fd2 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
568 assert_se(fd2 >= 0);
569 assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) < 0);
570 assert_se(errno == ENOENT); /* we removed the symlink, must fail */
571
572 free(j);
573 assert_se(j = path_join(t, "sock"));
574
575 fd3 = open(j, O_CLOEXEC|O_PATH|O_NOFOLLOW);
576 assert_se(fd3 > 0);
577 assert_se(sockaddr_un_set_path(&sa.un, FORMAT_PROC_FD_PATH(fd3)) >= 0); /* connect via O_PATH instead, circumventing 108ch limit */
578
579 assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) >= 0);
580}
581
582TEST(getpeerpidref) {
583 _cleanup_close_pair_ int fd[2] = EBADF_PAIR;
584
585 ASSERT_OK(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fd));
586
587 _cleanup_(pidref_done) PidRef pidref0 = PIDREF_NULL, pidref1 = PIDREF_NULL, pidref_self = PIDREF_NULL, pidref_pid1 = PIDREF_NULL;
588 ASSERT_OK(getpeerpidref(fd[0], &pidref0));
589 ASSERT_OK(getpeerpidref(fd[1], &pidref1));
590
591 ASSERT_OK(pidref_set_self(&pidref_self));
592 ASSERT_OK(pidref_set_pid(&pidref_pid1, 1));
593
594 ASSERT_TRUE(pidref_equal(&pidref0, &pidref1));
595 ASSERT_TRUE(pidref_equal(&pidref0, &pidref_self));
596 ASSERT_TRUE(pidref_equal(&pidref1, &pidref_self));
597
598 ASSERT_TRUE(!pidref_equal(&pidref_self, &pidref_pid1));
599 ASSERT_TRUE(!pidref_equal(&pidref1, &pidref_pid1));
600 ASSERT_TRUE(!pidref_equal(&pidref0, &pidref_pid1));
601}
602
603DEFINE_TEST_MAIN(LOG_DEBUG);