]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-socket-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
12 #include "exit-status.h"
14 #include "in-addr-util.h"
18 #include "missing_network.h"
19 #include "process-util.h"
20 #include "socket-netlink.h"
21 #include "socket-util.h"
22 #include "string-util.h"
24 #include "tmpfile-util.h"
26 assert_cc(SUN_PATH_LEN
== 108);
28 static void test_ifname_valid(void) {
29 log_info("/* %s */", __func__
);
31 assert(ifname_valid("foo"));
32 assert(ifname_valid("eth0"));
34 assert(!ifname_valid("0"));
35 assert(!ifname_valid("99"));
36 assert(ifname_valid("a99"));
37 assert(ifname_valid("99a"));
39 assert(!ifname_valid(NULL
));
40 assert(!ifname_valid(""));
41 assert(!ifname_valid(" "));
42 assert(!ifname_valid(" foo"));
43 assert(!ifname_valid("bar\n"));
44 assert(!ifname_valid("."));
45 assert(!ifname_valid(".."));
46 assert(ifname_valid("foo.bar"));
47 assert(!ifname_valid("x:y"));
49 assert(ifname_valid("xxxxxxxxxxxxxxx"));
50 assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
51 assert(ifname_valid_full("xxxxxxxxxxxxxxxx", true));
54 static void test_socket_print_unix_one(const char *in
, size_t len_in
, const char *expected
) {
55 _cleanup_free_
char *out
= NULL
, *c
= NULL
;
57 assert(len_in
<= SUN_PATH_LEN
);
58 SocketAddress a
= { .sockaddr
= { .un
= { .sun_family
= AF_UNIX
} },
59 .size
= offsetof(struct sockaddr_un
, sun_path
) + len_in
,
62 memcpy(a
.sockaddr
.un
.sun_path
, in
, len_in
);
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_se(streq(out
, expected
));
70 static void test_socket_print_unix(void) {
71 log_info("/* %s */", __func__
);
73 /* Some additional tests for abstract addresses which we don't parse */
75 test_socket_print_unix_one("\0\0\0\0", 4, "@\\000\\000\\000");
76 test_socket_print_unix_one("@abs", 5, "@abs");
77 test_socket_print_unix_one("\n", 2, "\\n");
78 test_socket_print_unix_one("", 1, "<unnamed>");
79 test_socket_print_unix_one("\0", 1, "<unnamed>");
80 test_socket_print_unix_one("\0_________________________there's 108 characters in this string_____________________________________________", 108,
81 "@_________________________there\\'s 108 characters in this string_____________________________________________");
82 test_socket_print_unix_one("////////////////////////////////////////////////////////////////////////////////////////////////////////////", 108,
83 "////////////////////////////////////////////////////////////////////////////////////////////////////////////");
84 test_socket_print_unix_one("\0\a\b\n\255", 6, "@\\a\\b\\n\\255\\000");
87 static void test_in_addr_is_null(void) {
88 union in_addr_union i
= {};
90 log_info("/* %s */", __func__
);
92 assert_se(in_addr_is_null(AF_INET
, &i
) == true);
93 assert_se(in_addr_is_null(AF_INET6
, &i
) == true);
95 i
.in
.s_addr
= 0x1000000;
96 assert_se(in_addr_is_null(AF_INET
, &i
) == false);
97 assert_se(in_addr_is_null(AF_INET6
, &i
) == false);
99 assert_se(in_addr_is_null(-1, &i
) == -EAFNOSUPPORT
);
102 static void test_in_addr_prefix_intersect_one(unsigned f
, const char *a
, unsigned apl
, const char *b
, unsigned bpl
, int result
) {
103 union in_addr_union ua
, ub
;
105 assert_se(in_addr_from_string(f
, a
, &ua
) >= 0);
106 assert_se(in_addr_from_string(f
, b
, &ub
) >= 0);
108 assert_se(in_addr_prefix_intersect(f
, &ua
, apl
, &ub
, bpl
) == result
);
111 static void test_in_addr_prefix_intersect(void) {
112 log_info("/* %s */", __func__
);
114 test_in_addr_prefix_intersect_one(AF_INET
, "255.255.255.255", 32, "255.255.255.254", 32, 0);
115 test_in_addr_prefix_intersect_one(AF_INET
, "255.255.255.255", 0, "255.255.255.255", 32, 1);
116 test_in_addr_prefix_intersect_one(AF_INET
, "0.0.0.0", 0, "47.11.8.15", 32, 1);
118 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 24, "1.1.1.1", 24, 1);
119 test_in_addr_prefix_intersect_one(AF_INET
, "2.2.2.2", 24, "1.1.1.1", 24, 0);
121 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 24, "1.1.1.127", 25, 1);
122 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 24, "1.1.1.127", 26, 1);
123 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 25, "1.1.1.127", 25, 1);
124 test_in_addr_prefix_intersect_one(AF_INET
, "1.1.1.1", 25, "1.1.1.255", 25, 0);
126 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);
127 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);
128 test_in_addr_prefix_intersect_one(AF_INET6
, "::", 0, "beef:beef:beef:beef:beef:beef:beef:beef", 128, 1);
130 test_in_addr_prefix_intersect_one(AF_INET6
, "1::2", 64, "1::2", 64, 1);
131 test_in_addr_prefix_intersect_one(AF_INET6
, "2::2", 64, "1::2", 64, 0);
133 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 120, "1::007f", 121, 1);
134 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 120, "1::007f", 122, 1);
135 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 121, "1::007f", 121, 1);
136 test_in_addr_prefix_intersect_one(AF_INET6
, "1::1", 121, "1::00ff", 121, 0);
139 static void test_in_addr_prefix_next_one(unsigned f
, const char *before
, unsigned pl
, const char *after
) {
140 union in_addr_union ubefore
, uafter
, t
;
142 assert_se(in_addr_from_string(f
, before
, &ubefore
) >= 0);
145 assert_se((in_addr_prefix_next(f
, &t
, pl
) > 0) == !!after
);
148 assert_se(in_addr_from_string(f
, after
, &uafter
) >= 0);
149 assert_se(in_addr_equal(f
, &t
, &uafter
) > 0);
153 static void test_in_addr_prefix_next(void) {
154 log_info("/* %s */", __func__
);
156 test_in_addr_prefix_next_one(AF_INET
, "192.168.0.0", 24, "192.168.1.0");
157 test_in_addr_prefix_next_one(AF_INET
, "192.168.0.0", 16, "192.169.0.0");
158 test_in_addr_prefix_next_one(AF_INET
, "192.168.0.0", 20, "192.168.16.0");
160 test_in_addr_prefix_next_one(AF_INET
, "0.0.0.0", 32, "0.0.0.1");
161 test_in_addr_prefix_next_one(AF_INET
, "255.255.255.255", 32, NULL
);
162 test_in_addr_prefix_next_one(AF_INET
, "255.255.255.0", 24, NULL
);
164 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 128, "4400::0001");
165 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 120, "4400::0100");
166 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 127, "4400::0002");
167 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 8, "4500::");
168 test_in_addr_prefix_next_one(AF_INET6
, "4400::", 7, "4600::");
170 test_in_addr_prefix_next_one(AF_INET6
, "::", 128, "::1");
172 test_in_addr_prefix_next_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, NULL
);
173 test_in_addr_prefix_next_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", 120, NULL
);
176 static void test_in_addr_to_string_one(int f
, const char *addr
) {
177 union in_addr_union ua
;
178 _cleanup_free_
char *r
= NULL
;
180 assert_se(in_addr_from_string(f
, addr
, &ua
) >= 0);
181 assert_se(in_addr_to_string(f
, &ua
, &r
) >= 0);
182 printf("test_in_addr_to_string_one: %s == %s\n", addr
, r
);
183 assert_se(streq(addr
, r
));
186 static void test_in_addr_to_string(void) {
187 log_info("/* %s */", __func__
);
189 test_in_addr_to_string_one(AF_INET
, "192.168.0.1");
190 test_in_addr_to_string_one(AF_INET
, "10.11.12.13");
191 test_in_addr_to_string_one(AF_INET6
, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
192 test_in_addr_to_string_one(AF_INET6
, "::1");
193 test_in_addr_to_string_one(AF_INET6
, "fe80::");
196 static void test_in_addr_ifindex_to_string_one(int f
, const char *a
, int ifindex
, const char *b
) {
197 _cleanup_free_
char *r
= NULL
;
198 union in_addr_union ua
, uuaa
;
201 assert_se(in_addr_from_string(f
, a
, &ua
) >= 0);
202 assert_se(in_addr_ifindex_to_string(f
, &ua
, ifindex
, &r
) >= 0);
203 printf("test_in_addr_ifindex_to_string_one: %s == %s\n", b
, r
);
204 assert_se(streq(b
, r
));
206 assert_se(in_addr_ifindex_from_string_auto(b
, &ff
, &uuaa
, &ifindex2
) >= 0);
208 assert_se(in_addr_equal(f
, &ua
, &uuaa
));
209 assert_se(ifindex2
== ifindex
|| ifindex2
== 0);
212 static void test_in_addr_ifindex_to_string(void) {
213 log_info("/* %s */", __func__
);
215 test_in_addr_ifindex_to_string_one(AF_INET
, "192.168.0.1", 7, "192.168.0.1");
216 test_in_addr_ifindex_to_string_one(AF_INET
, "10.11.12.13", 9, "10.11.12.13");
217 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");
218 test_in_addr_ifindex_to_string_one(AF_INET6
, "::1", 11, "::1");
219 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::", 12, "fe80::%12");
220 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::", 0, "fe80::");
221 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::14", 12, "fe80::14%12");
222 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::15", -7, "fe80::15");
223 test_in_addr_ifindex_to_string_one(AF_INET6
, "fe80::16", LOOPBACK_IFINDEX
, "fe80::16%1");
226 static void test_in_addr_ifindex_from_string_auto(void) {
228 union in_addr_union ua
;
230 log_info("/* %s */", __func__
);
231 /* Most in_addr_ifindex_from_string_auto() invocations have already been tested above, but let's test some more */
233 assert_se(in_addr_ifindex_from_string_auto("fe80::17", &family
, &ua
, &ifindex
) >= 0);
234 assert_se(family
== AF_INET6
);
235 assert_se(ifindex
== 0);
237 assert_se(in_addr_ifindex_from_string_auto("fe80::18%19", &family
, &ua
, &ifindex
) >= 0);
238 assert_se(family
== AF_INET6
);
239 assert_se(ifindex
== 19);
241 assert_se(in_addr_ifindex_from_string_auto("fe80::18%lo", &family
, &ua
, &ifindex
) >= 0);
242 assert_se(family
== AF_INET6
);
243 assert_se(ifindex
== LOOPBACK_IFINDEX
);
245 assert_se(in_addr_ifindex_from_string_auto("fe80::19%thisinterfacecantexist", &family
, &ua
, &ifindex
) == -ENODEV
);
248 static void test_sockaddr_equal(void) {
249 union sockaddr_union a
= {
250 .in
.sin_family
= AF_INET
,
252 .in
.sin_addr
.s_addr
= htobe32(INADDR_ANY
),
254 union sockaddr_union b
= {
255 .in
.sin_family
= AF_INET
,
257 .in
.sin_addr
.s_addr
= htobe32(INADDR_ANY
),
259 union sockaddr_union c
= {
260 .in
.sin_family
= AF_INET
,
262 .in
.sin_addr
.s_addr
= htobe32(1234),
264 union sockaddr_union d
= {
265 .in6
.sin6_family
= AF_INET6
,
267 .in6
.sin6_addr
= IN6ADDR_ANY_INIT
,
269 union sockaddr_union e
= {
270 .vm
.svm_family
= AF_VSOCK
,
272 .vm
.svm_cid
= VMADDR_CID_ANY
,
275 log_info("/* %s */", __func__
);
277 assert_se(sockaddr_equal(&a
, &a
));
278 assert_se(sockaddr_equal(&a
, &b
));
279 assert_se(sockaddr_equal(&d
, &d
));
280 assert_se(sockaddr_equal(&e
, &e
));
281 assert_se(!sockaddr_equal(&a
, &c
));
282 assert_se(!sockaddr_equal(&b
, &c
));
283 assert_se(!sockaddr_equal(&a
, &e
));
286 static void test_sockaddr_un_len(void) {
287 log_info("/* %s */", __func__
);
289 static const struct sockaddr_un fs
= {
290 .sun_family
= AF_UNIX
,
291 .sun_path
= "/foo/bar/waldo",
294 static const struct sockaddr_un abstract
= {
295 .sun_family
= AF_UNIX
,
296 .sun_path
= "\0foobar",
299 assert_se(SOCKADDR_UN_LEN(fs
) == offsetof(struct sockaddr_un
, sun_path
) + strlen(fs
.sun_path
) + 1);
300 assert_se(SOCKADDR_UN_LEN(abstract
) == offsetof(struct sockaddr_un
, sun_path
) + 1 + strlen(abstract
.sun_path
+ 1));
303 static void test_in_addr_is_multicast(void) {
304 union in_addr_union a
, b
;
307 log_info("/* %s */", __func__
);
309 assert_se(in_addr_from_string_auto("192.168.3.11", &f
, &a
) >= 0);
310 assert_se(in_addr_is_multicast(f
, &a
) == 0);
312 assert_se(in_addr_from_string_auto("224.0.0.1", &f
, &a
) >= 0);
313 assert_se(in_addr_is_multicast(f
, &a
) == 1);
315 assert_se(in_addr_from_string_auto("FF01:0:0:0:0:0:0:1", &f
, &b
) >= 0);
316 assert_se(in_addr_is_multicast(f
, &b
) == 1);
318 assert_se(in_addr_from_string_auto("2001:db8::c:69b:aeff:fe53:743e", &f
, &b
) >= 0);
319 assert_se(in_addr_is_multicast(f
, &b
) == 0);
322 static void test_getpeercred_getpeergroups(void) {
325 log_info("/* %s */", __func__
);
327 r
= safe_fork("(getpeercred)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
331 static const gid_t gids
[] = { 3, 4, 5, 6, 7 };
339 if (geteuid() == 0) {
342 test_gids
= (gid_t
*) gids
;
343 n_test_gids
= ELEMENTSOF(gids
);
345 assert_se(setgroups(n_test_gids
, test_gids
) >= 0);
346 assert_se(setresgid(test_gid
, test_gid
, test_gid
) >= 0);
347 assert_se(setresuid(test_uid
, test_uid
, test_uid
) >= 0);
355 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
356 assert(ngroups_max
> 0);
358 test_gids
= newa(gid_t
, ngroups_max
);
360 r
= getgroups(ngroups_max
, test_gids
);
362 n_test_gids
= (size_t) r
;
365 assert_se(socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) >= 0);
367 assert_se(getpeercred(pair
[0], &ucred
) >= 0);
369 assert_se(ucred
.uid
== test_uid
);
370 assert_se(ucred
.gid
== test_gid
);
371 assert_se(ucred
.pid
== getpid_cached());
374 _cleanup_free_ gid_t
*peer_groups
= NULL
;
376 r
= getpeergroups(pair
[0], &peer_groups
);
377 assert_se(r
>= 0 || IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
));
380 assert_se((size_t) r
== n_test_gids
);
381 assert_se(memcmp(peer_groups
, test_gids
, sizeof(gid_t
) * n_test_gids
) == 0);
385 safe_close_pair(pair
);
390 static void test_passfd_read(void) {
391 static const char file_contents
[] = "test contents for passfd";
392 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
395 log_info("/* %s */", __func__
);
397 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
399 r
= safe_fork("(passfd_read)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
404 char tmpfile
[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
405 _cleanup_close_
int tmpfd
= -1;
407 pair
[0] = safe_close(pair
[0]);
409 tmpfd
= mkostemp_safe(tmpfile
);
410 assert_se(tmpfd
>= 0);
411 assert_se(write(tmpfd
, file_contents
, strlen(file_contents
)) == (ssize_t
) strlen(file_contents
));
412 tmpfd
= safe_close(tmpfd
);
414 tmpfd
= open(tmpfile
, O_RDONLY
);
415 assert_se(tmpfd
>= 0);
416 assert_se(unlink(tmpfile
) == 0);
418 assert_se(send_one_fd(pair
[1], tmpfd
, MSG_DONTWAIT
) == 0);
424 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
425 _cleanup_close_
int fd
= -1;
427 pair
[1] = safe_close(pair
[1]);
429 assert_se(receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
) == 0);
432 r
= read(fd
, buf
, sizeof(buf
)-1);
435 assert_se(streq(buf
, file_contents
));
438 static void test_passfd_contents_read(void) {
439 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
440 static const char file_contents
[] = "test contents in the file";
441 static const char wire_contents
[] = "test contents on the wire";
444 log_info("/* %s */", __func__
);
446 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
448 r
= safe_fork("(passfd_contents_read)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
453 struct iovec iov
= IOVEC_INIT_STRING(wire_contents
);
454 char tmpfile
[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
455 _cleanup_close_
int tmpfd
= -1;
457 pair
[0] = safe_close(pair
[0]);
459 tmpfd
= mkostemp_safe(tmpfile
);
460 assert_se(tmpfd
>= 0);
461 assert_se(write(tmpfd
, file_contents
, strlen(file_contents
)) == (ssize_t
) strlen(file_contents
));
462 tmpfd
= safe_close(tmpfd
);
464 tmpfd
= open(tmpfile
, O_RDONLY
);
465 assert_se(tmpfd
>= 0);
466 assert_se(unlink(tmpfile
) == 0);
468 assert_se(send_one_fd_iov(pair
[1], tmpfd
, &iov
, 1, MSG_DONTWAIT
) > 0);
474 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
475 _cleanup_close_
int fd
= -1;
478 pair
[1] = safe_close(pair
[1]);
480 k
= receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
);
483 assert_se(streq(buf
, wire_contents
));
486 r
= read(fd
, buf
, sizeof(buf
)-1);
489 assert_se(streq(buf
, file_contents
));
492 static void test_receive_nopassfd(void) {
493 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
494 static const char wire_contents
[] = "no fd passed here";
497 log_info("/* %s */", __func__
);
499 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
501 r
= safe_fork("(receive_nopassfd)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
506 struct iovec iov
= IOVEC_INIT_STRING(wire_contents
);
508 pair
[0] = safe_close(pair
[0]);
510 assert_se(send_one_fd_iov(pair
[1], -1, &iov
, 1, MSG_DONTWAIT
) > 0);
516 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
520 pair
[1] = safe_close(pair
[1]);
522 k
= receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
);
525 assert_se(streq(buf
, wire_contents
));
527 /* no fd passed here, confirm it was reset */
531 static void test_send_nodata_nofd(void) {
532 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
535 log_info("/* %s */", __func__
);
537 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
539 r
= safe_fork("(send_nodata_nofd)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
544 pair
[0] = safe_close(pair
[0]);
546 assert_se(send_one_fd_iov(pair
[1], -1, NULL
, 0, MSG_DONTWAIT
) == -EINVAL
);
552 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
556 pair
[1] = safe_close(pair
[1]);
558 k
= receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
);
559 /* recvmsg() will return errno EAGAIN if nothing was sent */
560 assert_se(k
== -EAGAIN
);
562 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
563 assert_se(fd
== -999);
566 static void test_send_emptydata(void) {
567 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
570 log_info("/* %s */", __func__
);
572 assert_se(socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) >= 0);
574 r
= safe_fork("(send_emptydata)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
579 struct iovec iov
= IOVEC_INIT_STRING(""); /* zero-length iov */
580 assert_se(iov
.iov_len
== 0);
582 pair
[0] = safe_close(pair
[0]);
584 /* This will succeed, since iov is set. */
585 assert_se(send_one_fd_iov(pair
[1], -1, &iov
, 1, MSG_DONTWAIT
) == 0);
591 struct iovec iov
= IOVEC_INIT(buf
, sizeof(buf
)-1);
595 pair
[1] = safe_close(pair
[1]);
597 k
= receive_one_fd_iov(pair
[0], &iov
, 1, MSG_DONTWAIT
, &fd
);
598 /* receive_one_fd_iov() returns -EIO if an fd is not found and no data was returned. */
599 assert_se(k
== -EIO
);
601 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
602 assert_se(fd
== -999);
605 static void test_flush_accept(void) {
606 _cleanup_close_
int listen_stream
= -1, listen_dgram
= -1, listen_seqpacket
= 1, connect_stream
= -1, connect_dgram
= -1, connect_seqpacket
= -1;
607 static const union sockaddr_union sa
= { .un
.sun_family
= AF_UNIX
};
608 union sockaddr_union lsa
;
611 listen_stream
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
612 assert_se(listen_stream
>= 0);
614 listen_dgram
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
615 assert_se(listen_dgram
>= 0);
617 listen_seqpacket
= socket(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
618 assert_se(listen_seqpacket
>= 0);
620 assert_se(flush_accept(listen_stream
) < 0);
621 assert_se(flush_accept(listen_dgram
) < 0);
622 assert_se(flush_accept(listen_seqpacket
) < 0);
624 assert_se(bind(listen_stream
, &sa
.sa
, sizeof(sa_family_t
)) >= 0);
625 assert_se(bind(listen_dgram
, &sa
.sa
, sizeof(sa_family_t
)) >= 0);
626 assert_se(bind(listen_seqpacket
, &sa
.sa
, sizeof(sa_family_t
)) >= 0);
628 assert_se(flush_accept(listen_stream
) < 0);
629 assert_se(flush_accept(listen_dgram
) < 0);
630 assert_se(flush_accept(listen_seqpacket
) < 0);
632 assert_se(listen(listen_stream
, SOMAXCONN
) >= 0);
633 assert_se(listen(listen_dgram
, SOMAXCONN
) < 0);
634 assert_se(listen(listen_seqpacket
, SOMAXCONN
) >= 0);
636 assert_se(flush_accept(listen_stream
) >= 0);
637 assert_se(flush_accept(listen_dgram
) < 0);
638 assert_se(flush_accept(listen_seqpacket
) >= 0);
640 connect_stream
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
641 assert_se(connect_stream
>= 0);
643 connect_dgram
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
644 assert_se(connect_dgram
>= 0);
646 connect_seqpacket
= socket(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
647 assert_se(connect_seqpacket
>= 0);
650 assert_se(getsockname(listen_stream
, &lsa
.sa
, &l
) >= 0);
651 assert_se(connect(connect_stream
, &lsa
.sa
, l
) >= 0);
654 assert_se(getsockname(listen_dgram
, &lsa
.sa
, &l
) >= 0);
655 assert_se(connect(connect_dgram
, &lsa
.sa
, l
) >= 0);
658 assert_se(getsockname(listen_seqpacket
, &lsa
.sa
, &l
) >= 0);
659 assert_se(connect(connect_seqpacket
, &lsa
.sa
, l
) >= 0);
661 assert_se(flush_accept(listen_stream
) >= 0);
662 assert_se(flush_accept(listen_dgram
) < 0);
663 assert_se(flush_accept(listen_seqpacket
) >= 0);
666 int main(int argc
, char *argv
[]) {
667 test_setup_logging(LOG_DEBUG
);
671 test_socket_print_unix();
673 test_in_addr_is_null();
674 test_in_addr_prefix_intersect();
675 test_in_addr_prefix_next();
676 test_in_addr_to_string();
677 test_in_addr_ifindex_to_string();
678 test_in_addr_ifindex_from_string_auto();
680 test_sockaddr_equal();
682 test_sockaddr_un_len();
684 test_in_addr_is_multicast();
686 test_getpeercred_getpeergroups();
689 test_passfd_contents_read();
690 test_receive_nopassfd();
691 test_send_nodata_nofd();
692 test_send_emptydata();