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