]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-socket-util.c
sd-bus: Add sd_bus_message_peek_type docs
[thirdparty/systemd.git] / src / test / test-socket-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <grp.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "async.h"
11 #include "escape.h"
12 #include "exit-status.h"
13 #include "fd-util.h"
14 #include "in-addr-util.h"
15 #include "io-util.h"
16 #include "log.h"
17 #include "macro.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"
23 #include "tests.h"
24 #include "tmpfile-util.h"
25
26 assert_cc(SUN_PATH_LEN == 108);
27
28 static void test_ifname_valid(void) {
29 log_info("/* %s */", __func__);
30
31 assert(ifname_valid("foo"));
32 assert(ifname_valid("eth0"));
33
34 assert(!ifname_valid("0"));
35 assert(!ifname_valid("99"));
36 assert(ifname_valid("a99"));
37 assert(ifname_valid("99a"));
38
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"));
48
49 assert(ifname_valid("xxxxxxxxxxxxxxx"));
50 assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
51 assert(ifname_valid_full("xxxxxxxxxxxxxxxx", true));
52 }
53
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;
56
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,
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_se(streq(out, expected));
68 }
69
70 static void test_socket_print_unix(void) {
71 log_info("/* %s */", __func__);
72
73 /* Some additional tests for abstract addresses which we don't parse */
74
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");
85 }
86
87 static void test_in_addr_is_null(void) {
88 union in_addr_union i = {};
89
90 log_info("/* %s */", __func__);
91
92 assert_se(in_addr_is_null(AF_INET, &i) == true);
93 assert_se(in_addr_is_null(AF_INET6, &i) == true);
94
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);
98
99 assert_se(in_addr_is_null(-1, &i) == -EAFNOSUPPORT);
100 }
101
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;
104
105 assert_se(in_addr_from_string(f, a, &ua) >= 0);
106 assert_se(in_addr_from_string(f, b, &ub) >= 0);
107
108 assert_se(in_addr_prefix_intersect(f, &ua, apl, &ub, bpl) == result);
109 }
110
111 static void test_in_addr_prefix_intersect(void) {
112 log_info("/* %s */", __func__);
113
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);
117
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);
120
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);
125
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);
129
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);
132
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);
137 }
138
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;
141
142 assert_se(in_addr_from_string(f, before, &ubefore) >= 0);
143
144 t = ubefore;
145 assert_se((in_addr_prefix_next(f, &t, pl) > 0) == !!after);
146
147 if (after) {
148 assert_se(in_addr_from_string(f, after, &uafter) >= 0);
149 assert_se(in_addr_equal(f, &t, &uafter) > 0);
150 }
151 }
152
153 static void test_in_addr_prefix_next(void) {
154 log_info("/* %s */", __func__);
155
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");
159
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);
163
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::");
169
170 test_in_addr_prefix_next_one(AF_INET6, "::", 128, "::1");
171
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);
174 }
175
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;
179
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));
184 }
185
186 static void test_in_addr_to_string(void) {
187 log_info("/* %s */", __func__);
188
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::");
194 }
195
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;
199 int ff, ifindex2;
200
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));
205
206 assert_se(in_addr_ifindex_from_string_auto(b, &ff, &uuaa, &ifindex2) >= 0);
207 assert_se(ff == f);
208 assert_se(in_addr_equal(f, &ua, &uuaa));
209 assert_se(ifindex2 == ifindex || ifindex2 == 0);
210 }
211
212 static void test_in_addr_ifindex_to_string(void) {
213 log_info("/* %s */", __func__);
214
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");
224 }
225
226 static void test_in_addr_ifindex_from_string_auto(void) {
227 int family, ifindex;
228 union in_addr_union ua;
229
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 */
232
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);
236
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);
240
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);
244
245 assert_se(in_addr_ifindex_from_string_auto("fe80::19%thisinterfacecantexist", &family, &ua, &ifindex) == -ENODEV);
246 }
247
248 static void test_sockaddr_equal(void) {
249 union sockaddr_union a = {
250 .in.sin_family = AF_INET,
251 .in.sin_port = 0,
252 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
253 };
254 union sockaddr_union b = {
255 .in.sin_family = AF_INET,
256 .in.sin_port = 0,
257 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
258 };
259 union sockaddr_union c = {
260 .in.sin_family = AF_INET,
261 .in.sin_port = 0,
262 .in.sin_addr.s_addr = htobe32(1234),
263 };
264 union sockaddr_union d = {
265 .in6.sin6_family = AF_INET6,
266 .in6.sin6_port = 0,
267 .in6.sin6_addr = IN6ADDR_ANY_INIT,
268 };
269 union sockaddr_union e = {
270 .vm.svm_family = AF_VSOCK,
271 .vm.svm_port = 0,
272 .vm.svm_cid = VMADDR_CID_ANY,
273 };
274
275 log_info("/* %s */", __func__);
276
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));
284 }
285
286 static void test_sockaddr_un_len(void) {
287 log_info("/* %s */", __func__);
288
289 static const struct sockaddr_un fs = {
290 .sun_family = AF_UNIX,
291 .sun_path = "/foo/bar/waldo",
292 };
293
294 static const struct sockaddr_un abstract = {
295 .sun_family = AF_UNIX,
296 .sun_path = "\0foobar",
297 };
298
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));
301 }
302
303 static void test_in_addr_is_multicast(void) {
304 union in_addr_union a, b;
305 int f;
306
307 log_info("/* %s */", __func__);
308
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);
311
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);
314
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);
317
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);
320 }
321
322 static void test_getpeercred_getpeergroups(void) {
323 int r;
324
325 log_info("/* %s */", __func__);
326
327 r = safe_fork("(getpeercred)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
328 assert_se(r >= 0);
329
330 if (r == 0) {
331 static const gid_t gids[] = { 3, 4, 5, 6, 7 };
332 gid_t *test_gids;
333 size_t n_test_gids;
334 uid_t test_uid;
335 gid_t test_gid;
336 struct ucred ucred;
337 int pair[2];
338
339 if (geteuid() == 0) {
340 test_uid = 1;
341 test_gid = 2;
342 test_gids = (gid_t*) gids;
343 n_test_gids = ELEMENTSOF(gids);
344
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);
348
349 } else {
350 long ngroups_max;
351
352 test_uid = getuid();
353 test_gid = getgid();
354
355 ngroups_max = sysconf(_SC_NGROUPS_MAX);
356 assert(ngroups_max > 0);
357
358 test_gids = newa(gid_t, ngroups_max);
359
360 r = getgroups(ngroups_max, test_gids);
361 assert_se(r >= 0);
362 n_test_gids = (size_t) r;
363 }
364
365 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
366
367 assert_se(getpeercred(pair[0], &ucred) >= 0);
368
369 assert_se(ucred.uid == test_uid);
370 assert_se(ucred.gid == test_gid);
371 assert_se(ucred.pid == getpid_cached());
372
373 {
374 _cleanup_free_ gid_t *peer_groups = NULL;
375
376 r = getpeergroups(pair[0], &peer_groups);
377 assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
378
379 if (r >= 0) {
380 assert_se((size_t) r == n_test_gids);
381 assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
382 }
383 }
384
385 safe_close_pair(pair);
386 _exit(EXIT_SUCCESS);
387 }
388 }
389
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 };
393 int r;
394
395 log_info("/* %s */", __func__);
396
397 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
398
399 r = safe_fork("(passfd_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
400 assert_se(r >= 0);
401
402 if (r == 0) {
403 /* Child */
404 char tmpfile[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
405 _cleanup_close_ int tmpfd = -1;
406
407 pair[0] = safe_close(pair[0]);
408
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);
413
414 tmpfd = open(tmpfile, O_RDONLY);
415 assert_se(tmpfd >= 0);
416 assert_se(unlink(tmpfile) == 0);
417
418 assert_se(send_one_fd(pair[1], tmpfd, MSG_DONTWAIT) == 0);
419 _exit(EXIT_SUCCESS);
420 }
421
422 /* Parent */
423 char buf[64];
424 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
425 _cleanup_close_ int fd = -1;
426
427 pair[1] = safe_close(pair[1]);
428
429 assert_se(receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd) == 0);
430
431 assert_se(fd >= 0);
432 r = read(fd, buf, sizeof(buf)-1);
433 assert_se(r >= 0);
434 buf[r] = 0;
435 assert_se(streq(buf, file_contents));
436 }
437
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";
442 int r;
443
444 log_info("/* %s */", __func__);
445
446 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
447
448 r = safe_fork("(passfd_contents_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
449 assert_se(r >= 0);
450
451 if (r == 0) {
452 /* Child */
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;
456
457 pair[0] = safe_close(pair[0]);
458
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);
463
464 tmpfd = open(tmpfile, O_RDONLY);
465 assert_se(tmpfd >= 0);
466 assert_se(unlink(tmpfile) == 0);
467
468 assert_se(send_one_fd_iov(pair[1], tmpfd, &iov, 1, MSG_DONTWAIT) > 0);
469 _exit(EXIT_SUCCESS);
470 }
471
472 /* Parent */
473 char buf[64];
474 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
475 _cleanup_close_ int fd = -1;
476 ssize_t k;
477
478 pair[1] = safe_close(pair[1]);
479
480 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
481 assert_se(k > 0);
482 buf[k] = 0;
483 assert_se(streq(buf, wire_contents));
484
485 assert_se(fd >= 0);
486 r = read(fd, buf, sizeof(buf)-1);
487 assert_se(r >= 0);
488 buf[r] = 0;
489 assert_se(streq(buf, file_contents));
490 }
491
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";
495 int r;
496
497 log_info("/* %s */", __func__);
498
499 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
500
501 r = safe_fork("(receive_nopassfd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
502 assert_se(r >= 0);
503
504 if (r == 0) {
505 /* Child */
506 struct iovec iov = IOVEC_INIT_STRING(wire_contents);
507
508 pair[0] = safe_close(pair[0]);
509
510 assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) > 0);
511 _exit(EXIT_SUCCESS);
512 }
513
514 /* Parent */
515 char buf[64];
516 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
517 int fd = -999;
518 ssize_t k;
519
520 pair[1] = safe_close(pair[1]);
521
522 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
523 assert_se(k > 0);
524 buf[k] = 0;
525 assert_se(streq(buf, wire_contents));
526
527 /* no fd passed here, confirm it was reset */
528 assert_se(fd == -1);
529 }
530
531 static void test_send_nodata_nofd(void) {
532 _cleanup_close_pair_ int pair[2] = { -1, -1 };
533 int r;
534
535 log_info("/* %s */", __func__);
536
537 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
538
539 r = safe_fork("(send_nodata_nofd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
540 assert_se(r >= 0);
541
542 if (r == 0) {
543 /* Child */
544 pair[0] = safe_close(pair[0]);
545
546 assert_se(send_one_fd_iov(pair[1], -1, NULL, 0, MSG_DONTWAIT) == -EINVAL);
547 _exit(EXIT_SUCCESS);
548 }
549
550 /* Parent */
551 char buf[64];
552 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
553 int fd = -999;
554 ssize_t k;
555
556 pair[1] = safe_close(pair[1]);
557
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);
561
562 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
563 assert_se(fd == -999);
564 }
565
566 static void test_send_emptydata(void) {
567 _cleanup_close_pair_ int pair[2] = { -1, -1 };
568 int r;
569
570 log_info("/* %s */", __func__);
571
572 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
573
574 r = safe_fork("(send_emptydata)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
575 assert_se(r >= 0);
576
577 if (r == 0) {
578 /* Child */
579 struct iovec iov = IOVEC_INIT_STRING(""); /* zero-length iov */
580 assert_se(iov.iov_len == 0);
581
582 pair[0] = safe_close(pair[0]);
583
584 /* This will succeed, since iov is set. */
585 assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) == 0);
586 _exit(EXIT_SUCCESS);
587 }
588
589 /* Parent */
590 char buf[64];
591 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
592 int fd = -999;
593 ssize_t k;
594
595 pair[1] = safe_close(pair[1]);
596
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);
600
601 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
602 assert_se(fd == -999);
603 }
604
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;
609 socklen_t l;
610
611 listen_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
612 assert_se(listen_stream >= 0);
613
614 listen_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
615 assert_se(listen_dgram >= 0);
616
617 listen_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
618 assert_se(listen_seqpacket >= 0);
619
620 assert_se(flush_accept(listen_stream) < 0);
621 assert_se(flush_accept(listen_dgram) < 0);
622 assert_se(flush_accept(listen_seqpacket) < 0);
623
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);
627
628 assert_se(flush_accept(listen_stream) < 0);
629 assert_se(flush_accept(listen_dgram) < 0);
630 assert_se(flush_accept(listen_seqpacket) < 0);
631
632 assert_se(listen(listen_stream, SOMAXCONN) >= 0);
633 assert_se(listen(listen_dgram, SOMAXCONN) < 0);
634 assert_se(listen(listen_seqpacket, SOMAXCONN) >= 0);
635
636 assert_se(flush_accept(listen_stream) >= 0);
637 assert_se(flush_accept(listen_dgram) < 0);
638 assert_se(flush_accept(listen_seqpacket) >= 0);
639
640 connect_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
641 assert_se(connect_stream >= 0);
642
643 connect_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
644 assert_se(connect_dgram >= 0);
645
646 connect_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
647 assert_se(connect_seqpacket >= 0);
648
649 l = sizeof(lsa);
650 assert_se(getsockname(listen_stream, &lsa.sa, &l) >= 0);
651 assert_se(connect(connect_stream, &lsa.sa, l) >= 0);
652
653 l = sizeof(lsa);
654 assert_se(getsockname(listen_dgram, &lsa.sa, &l) >= 0);
655 assert_se(connect(connect_dgram, &lsa.sa, l) >= 0);
656
657 l = sizeof(lsa);
658 assert_se(getsockname(listen_seqpacket, &lsa.sa, &l) >= 0);
659 assert_se(connect(connect_seqpacket, &lsa.sa, l) >= 0);
660
661 assert_se(flush_accept(listen_stream) >= 0);
662 assert_se(flush_accept(listen_dgram) < 0);
663 assert_se(flush_accept(listen_seqpacket) >= 0);
664 }
665
666 int main(int argc, char *argv[]) {
667 test_setup_logging(LOG_DEBUG);
668
669 test_ifname_valid();
670
671 test_socket_print_unix();
672
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();
679
680 test_sockaddr_equal();
681
682 test_sockaddr_un_len();
683
684 test_in_addr_is_multicast();
685
686 test_getpeercred_getpeergroups();
687
688 test_passfd_read();
689 test_passfd_contents_read();
690 test_receive_nopassfd();
691 test_send_nodata_nofd();
692 test_send_emptydata();
693 test_flush_accept();
694
695 return 0;
696 }