]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-socket-util.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-socket-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <grp.h>
6
7 #include "alloc-util.h"
8 #include "async.h"
9 #include "exit-status.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "in-addr-util.h"
13 #include "io-util.h"
14 #include "log.h"
15 #include "macro.h"
16 #include "process-util.h"
17 #include "socket-util.h"
18 #include "string-util.h"
19 #include "util.h"
20 #include "tests.h"
21
22 static void test_ifname_valid(void) {
23 assert(ifname_valid("foo"));
24 assert(ifname_valid("eth0"));
25
26 assert(!ifname_valid("0"));
27 assert(!ifname_valid("99"));
28 assert(ifname_valid("a99"));
29 assert(ifname_valid("99a"));
30
31 assert(!ifname_valid(NULL));
32 assert(!ifname_valid(""));
33 assert(!ifname_valid(" "));
34 assert(!ifname_valid(" foo"));
35 assert(!ifname_valid("bar\n"));
36 assert(!ifname_valid("."));
37 assert(!ifname_valid(".."));
38 assert(ifname_valid("foo.bar"));
39 assert(!ifname_valid("x:y"));
40
41 assert(ifname_valid("xxxxxxxxxxxxxxx"));
42 assert(!ifname_valid("xxxxxxxxxxxxxxxx"));
43 }
44
45 static void test_socket_address_parse(void) {
46 SocketAddress a;
47
48 assert_se(socket_address_parse(&a, "junk") < 0);
49 assert_se(socket_address_parse(&a, "192.168.1.1") < 0);
50 assert_se(socket_address_parse(&a, ".168.1.1") < 0);
51 assert_se(socket_address_parse(&a, "989.168.1.1") < 0);
52 assert_se(socket_address_parse(&a, "192.168.1.1:65536") < 0);
53 assert_se(socket_address_parse(&a, "192.168.1.1:0") < 0);
54 assert_se(socket_address_parse(&a, "0") < 0);
55 assert_se(socket_address_parse(&a, "65536") < 0);
56
57 assert_se(socket_address_parse(&a, "65535") >= 0);
58
59 /* The checks below will pass even if ipv6 is disabled in
60 * kernel. The underlying glibc's inet_pton() is just a string
61 * parser and doesn't make any syscalls. */
62
63 assert_se(socket_address_parse(&a, "[::1]") < 0);
64 assert_se(socket_address_parse(&a, "[::1]8888") < 0);
65 assert_se(socket_address_parse(&a, "::1") < 0);
66 assert_se(socket_address_parse(&a, "[::1]:0") < 0);
67 assert_se(socket_address_parse(&a, "[::1]:65536") < 0);
68 assert_se(socket_address_parse(&a, "[a:b:1]:8888") < 0);
69
70 assert_se(socket_address_parse(&a, "8888") >= 0);
71 assert_se(a.sockaddr.sa.sa_family == (socket_ipv6_is_supported() ? AF_INET6 : AF_INET));
72
73 assert_se(socket_address_parse(&a, "[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:8888") >= 0);
74 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
75
76 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
77 assert_se(a.sockaddr.sa.sa_family == AF_INET6);
78
79 assert_se(socket_address_parse(&a, "192.168.1.254:8888") >= 0);
80 assert_se(a.sockaddr.sa.sa_family == AF_INET);
81
82 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
83 assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
84
85 assert_se(socket_address_parse(&a, "@abstract") >= 0);
86 assert_se(a.sockaddr.sa.sa_family == AF_UNIX);
87
88 assert_se(socket_address_parse(&a, "vsock::1234") >= 0);
89 assert_se(a.sockaddr.sa.sa_family == AF_VSOCK);
90 assert_se(socket_address_parse(&a, "vsock:2:1234") >= 0);
91 assert_se(a.sockaddr.sa.sa_family == AF_VSOCK);
92 assert_se(socket_address_parse(&a, "vsock:2:1234x") < 0);
93 assert_se(socket_address_parse(&a, "vsock:2x:1234") < 0);
94 assert_se(socket_address_parse(&a, "vsock:2") < 0);
95 }
96
97 static void test_socket_address_parse_netlink(void) {
98 SocketAddress a;
99
100 assert_se(socket_address_parse_netlink(&a, "junk") < 0);
101 assert_se(socket_address_parse_netlink(&a, "") < 0);
102
103 assert_se(socket_address_parse_netlink(&a, "route") >= 0);
104 assert_se(socket_address_parse_netlink(&a, "route 10") >= 0);
105 assert_se(a.sockaddr.sa.sa_family == AF_NETLINK);
106 assert_se(a.protocol == NETLINK_ROUTE);
107
108 /* With spaces and tabs */
109 assert_se(socket_address_parse_netlink(&a, " kobject-uevent ") >= 0);
110 assert_se(socket_address_parse_netlink(&a, " \t kobject-uevent \t 10 \t") >= 0);
111 assert_se(a.sockaddr.sa.sa_family == AF_NETLINK);
112 assert_se(a.protocol == NETLINK_KOBJECT_UEVENT);
113
114 assert_se(socket_address_parse_netlink(&a, "kobject-uevent\t10") >= 0);
115 assert_se(a.sockaddr.sa.sa_family == AF_NETLINK);
116 assert_se(a.protocol == NETLINK_KOBJECT_UEVENT);
117
118 /* oss-fuzz #6884 */
119 assert_se(socket_address_parse_netlink(&a, "\xff") < 0);
120 }
121
122 static void test_socket_address_equal(void) {
123 SocketAddress a;
124 SocketAddress b;
125
126 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
127 assert_se(socket_address_parse(&b, "192.168.1.1:888") >= 0);
128 assert_se(!socket_address_equal(&a, &b));
129
130 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
131 assert_se(socket_address_parse(&b, "192.16.1.1:8888") >= 0);
132 assert_se(!socket_address_equal(&a, &b));
133
134 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
135 assert_se(socket_address_parse(&b, "8888") >= 0);
136 assert_se(!socket_address_equal(&a, &b));
137
138 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
139 assert_se(socket_address_parse(&b, "/foo/bar/") >= 0);
140 assert_se(!socket_address_equal(&a, &b));
141
142 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
143 assert_se(socket_address_parse(&b, "192.168.1.1:8888") >= 0);
144 assert_se(socket_address_equal(&a, &b));
145
146 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
147 assert_se(socket_address_parse(&b, "/foo/bar") >= 0);
148 assert_se(socket_address_equal(&a, &b));
149
150 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
151 assert_se(socket_address_parse(&b, "[::1]:8888") >= 0);
152 assert_se(socket_address_equal(&a, &b));
153
154 assert_se(socket_address_parse(&a, "@abstract") >= 0);
155 assert_se(socket_address_parse(&b, "@abstract") >= 0);
156 assert_se(socket_address_equal(&a, &b));
157
158 assert_se(socket_address_parse_netlink(&a, "firewall") >= 0);
159 assert_se(socket_address_parse_netlink(&b, "firewall") >= 0);
160 assert_se(socket_address_equal(&a, &b));
161
162 assert_se(socket_address_parse(&a, "vsock:2:1234") >= 0);
163 assert_se(socket_address_parse(&b, "vsock:2:1234") >= 0);
164 assert_se(socket_address_equal(&a, &b));
165 assert_se(socket_address_parse(&b, "vsock:2:1235") >= 0);
166 assert_se(!socket_address_equal(&a, &b));
167 assert_se(socket_address_parse(&b, "vsock:3:1234") >= 0);
168 assert_se(!socket_address_equal(&a, &b));
169 }
170
171 static void test_socket_address_get_path(void) {
172 SocketAddress a;
173
174 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
175 assert_se(!socket_address_get_path(&a));
176
177 assert_se(socket_address_parse(&a, "@abstract") >= 0);
178 assert_se(!socket_address_get_path(&a));
179
180 assert_se(socket_address_parse(&a, "[::1]:8888") >= 0);
181 assert_se(!socket_address_get_path(&a));
182
183 assert_se(socket_address_parse(&a, "/foo/bar") >= 0);
184 assert_se(streq(socket_address_get_path(&a), "/foo/bar"));
185
186 assert_se(socket_address_parse(&a, "vsock:2:1234") >= 0);
187 assert_se(!socket_address_get_path(&a));
188 }
189
190 static void test_socket_address_is(void) {
191 SocketAddress a;
192
193 assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
194 assert_se(socket_address_is(&a, "192.168.1.1:8888", SOCK_STREAM));
195 assert_se(!socket_address_is(&a, "route", SOCK_STREAM));
196 assert_se(!socket_address_is(&a, "192.168.1.1:8888", SOCK_RAW));
197 }
198
199 static void test_socket_address_is_netlink(void) {
200 SocketAddress a;
201
202 assert_se(socket_address_parse_netlink(&a, "route 10") >= 0);
203 assert_se(socket_address_is_netlink(&a, "route 10"));
204 assert_se(!socket_address_is_netlink(&a, "192.168.1.1:8888"));
205 assert_se(!socket_address_is_netlink(&a, "route 1"));
206 }
207
208 static void test_in_addr_is_null(void) {
209
210 union in_addr_union i = {};
211
212 assert_se(in_addr_is_null(AF_INET, &i) == true);
213 assert_se(in_addr_is_null(AF_INET6, &i) == true);
214
215 i.in.s_addr = 0x1000000;
216 assert_se(in_addr_is_null(AF_INET, &i) == false);
217 assert_se(in_addr_is_null(AF_INET6, &i) == false);
218
219 assert_se(in_addr_is_null(-1, &i) == -EAFNOSUPPORT);
220 }
221
222 static void test_in_addr_prefix_intersect_one(unsigned f, const char *a, unsigned apl, const char *b, unsigned bpl, int result) {
223 union in_addr_union ua, ub;
224
225 assert_se(in_addr_from_string(f, a, &ua) >= 0);
226 assert_se(in_addr_from_string(f, b, &ub) >= 0);
227
228 assert_se(in_addr_prefix_intersect(f, &ua, apl, &ub, bpl) == result);
229 }
230
231 static void test_in_addr_prefix_intersect(void) {
232
233 test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 32, "255.255.255.254", 32, 0);
234 test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 0, "255.255.255.255", 32, 1);
235 test_in_addr_prefix_intersect_one(AF_INET, "0.0.0.0", 0, "47.11.8.15", 32, 1);
236
237 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.1", 24, 1);
238 test_in_addr_prefix_intersect_one(AF_INET, "2.2.2.2", 24, "1.1.1.1", 24, 0);
239
240 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 25, 1);
241 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 26, 1);
242 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.127", 25, 1);
243 test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.255", 25, 0);
244
245 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);
246 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);
247 test_in_addr_prefix_intersect_one(AF_INET6, "::", 0, "beef:beef:beef:beef:beef:beef:beef:beef", 128, 1);
248
249 test_in_addr_prefix_intersect_one(AF_INET6, "1::2", 64, "1::2", 64, 1);
250 test_in_addr_prefix_intersect_one(AF_INET6, "2::2", 64, "1::2", 64, 0);
251
252 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 121, 1);
253 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 122, 1);
254 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::007f", 121, 1);
255 test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::00ff", 121, 0);
256 }
257
258 static void test_in_addr_prefix_next_one(unsigned f, const char *before, unsigned pl, const char *after) {
259 union in_addr_union ubefore, uafter, t;
260
261 assert_se(in_addr_from_string(f, before, &ubefore) >= 0);
262
263 t = ubefore;
264 assert_se((in_addr_prefix_next(f, &t, pl) > 0) == !!after);
265
266 if (after) {
267 assert_se(in_addr_from_string(f, after, &uafter) >= 0);
268 assert_se(in_addr_equal(f, &t, &uafter) > 0);
269 }
270 }
271
272 static void test_in_addr_prefix_next(void) {
273
274 test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 24, "192.168.1.0");
275 test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 16, "192.169.0.0");
276 test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 20, "192.168.16.0");
277
278 test_in_addr_prefix_next_one(AF_INET, "0.0.0.0", 32, "0.0.0.1");
279 test_in_addr_prefix_next_one(AF_INET, "255.255.255.255", 32, NULL);
280 test_in_addr_prefix_next_one(AF_INET, "255.255.255.0", 24, NULL);
281
282 test_in_addr_prefix_next_one(AF_INET6, "4400::", 128, "4400::0001");
283 test_in_addr_prefix_next_one(AF_INET6, "4400::", 120, "4400::0100");
284 test_in_addr_prefix_next_one(AF_INET6, "4400::", 127, "4400::0002");
285 test_in_addr_prefix_next_one(AF_INET6, "4400::", 8, "4500::");
286 test_in_addr_prefix_next_one(AF_INET6, "4400::", 7, "4600::");
287
288 test_in_addr_prefix_next_one(AF_INET6, "::", 128, "::1");
289
290 test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, NULL);
291 test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", 120, NULL);
292
293 }
294
295 static void test_in_addr_to_string_one(int f, const char *addr) {
296 union in_addr_union ua;
297 _cleanup_free_ char *r = NULL;
298
299 assert_se(in_addr_from_string(f, addr, &ua) >= 0);
300 assert_se(in_addr_to_string(f, &ua, &r) >= 0);
301 printf("test_in_addr_to_string_one: %s == %s\n", addr, r);
302 assert_se(streq(addr, r));
303 }
304
305 static void test_in_addr_to_string(void) {
306 test_in_addr_to_string_one(AF_INET, "192.168.0.1");
307 test_in_addr_to_string_one(AF_INET, "10.11.12.13");
308 test_in_addr_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
309 test_in_addr_to_string_one(AF_INET6, "::1");
310 test_in_addr_to_string_one(AF_INET6, "fe80::");
311 }
312
313 static void test_in_addr_ifindex_to_string_one(int f, const char *a, int ifindex, const char *b) {
314 _cleanup_free_ char *r = NULL;
315 union in_addr_union ua, uuaa;
316 int ff, ifindex2;
317
318 assert_se(in_addr_from_string(f, a, &ua) >= 0);
319 assert_se(in_addr_ifindex_to_string(f, &ua, ifindex, &r) >= 0);
320 printf("test_in_addr_ifindex_to_string_one: %s == %s\n", b, r);
321 assert_se(streq(b, r));
322
323 assert_se(in_addr_ifindex_from_string_auto(b, &ff, &uuaa, &ifindex2) >= 0);
324 assert_se(ff == f);
325 assert_se(in_addr_equal(f, &ua, &uuaa));
326 assert_se(ifindex2 == ifindex || ifindex2 == 0);
327 }
328
329 static void test_in_addr_ifindex_to_string(void) {
330 test_in_addr_ifindex_to_string_one(AF_INET, "192.168.0.1", 7, "192.168.0.1");
331 test_in_addr_ifindex_to_string_one(AF_INET, "10.11.12.13", 9, "10.11.12.13");
332 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");
333 test_in_addr_ifindex_to_string_one(AF_INET6, "::1", 11, "::1");
334 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::", 12, "fe80::%12");
335 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::", 0, "fe80::");
336 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::14", 12, "fe80::14%12");
337 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::15", -7, "fe80::15");
338 test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::16", LOOPBACK_IFINDEX, "fe80::16%1");
339 }
340
341 static void test_in_addr_ifindex_from_string_auto(void) {
342 int family, ifindex;
343 union in_addr_union ua;
344
345 /* Most in_addr_ifindex_from_string_auto() invocations have already been tested above, but let's test some more */
346
347 assert_se(in_addr_ifindex_from_string_auto("fe80::17", &family, &ua, &ifindex) >= 0);
348 assert_se(family == AF_INET6);
349 assert_se(ifindex == 0);
350
351 assert_se(in_addr_ifindex_from_string_auto("fe80::18%19", &family, &ua, &ifindex) >= 0);
352 assert_se(family == AF_INET6);
353 assert_se(ifindex == 19);
354
355 assert_se(in_addr_ifindex_from_string_auto("fe80::18%lo", &family, &ua, &ifindex) >= 0);
356 assert_se(family == AF_INET6);
357 assert_se(ifindex == LOOPBACK_IFINDEX);
358
359 assert_se(in_addr_ifindex_from_string_auto("fe80::19%thisinterfacecantexist", &family, &ua, &ifindex) == -ENODEV);
360 }
361
362 static void test_sockaddr_equal(void) {
363 union sockaddr_union a = {
364 .in.sin_family = AF_INET,
365 .in.sin_port = 0,
366 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
367 };
368 union sockaddr_union b = {
369 .in.sin_family = AF_INET,
370 .in.sin_port = 0,
371 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
372 };
373 union sockaddr_union c = {
374 .in.sin_family = AF_INET,
375 .in.sin_port = 0,
376 .in.sin_addr.s_addr = htobe32(1234),
377 };
378 union sockaddr_union d = {
379 .in6.sin6_family = AF_INET6,
380 .in6.sin6_port = 0,
381 .in6.sin6_addr = IN6ADDR_ANY_INIT,
382 };
383 union sockaddr_union e = {
384 .vm.svm_family = AF_VSOCK,
385 .vm.svm_port = 0,
386 .vm.svm_cid = VMADDR_CID_ANY,
387 };
388 assert_se(sockaddr_equal(&a, &a));
389 assert_se(sockaddr_equal(&a, &b));
390 assert_se(sockaddr_equal(&d, &d));
391 assert_se(sockaddr_equal(&e, &e));
392 assert_se(!sockaddr_equal(&a, &c));
393 assert_se(!sockaddr_equal(&b, &c));
394 assert_se(!sockaddr_equal(&a, &e));
395 }
396
397 static void test_sockaddr_un_len(void) {
398 static const struct sockaddr_un fs = {
399 .sun_family = AF_UNIX,
400 .sun_path = "/foo/bar/waldo",
401 };
402
403 static const struct sockaddr_un abstract = {
404 .sun_family = AF_UNIX,
405 .sun_path = "\0foobar",
406 };
407
408 assert_se(SOCKADDR_UN_LEN(fs) == offsetof(struct sockaddr_un, sun_path) + strlen(fs.sun_path) + 1);
409 assert_se(SOCKADDR_UN_LEN(abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1));
410 }
411
412 static void test_in_addr_is_multicast(void) {
413 union in_addr_union a, b;
414 int f;
415
416 assert_se(in_addr_from_string_auto("192.168.3.11", &f, &a) >= 0);
417 assert_se(in_addr_is_multicast(f, &a) == 0);
418
419 assert_se(in_addr_from_string_auto("224.0.0.1", &f, &a) >= 0);
420 assert_se(in_addr_is_multicast(f, &a) == 1);
421
422 assert_se(in_addr_from_string_auto("FF01:0:0:0:0:0:0:1", &f, &b) >= 0);
423 assert_se(in_addr_is_multicast(f, &b) == 1);
424
425 assert_se(in_addr_from_string_auto("2001:db8::c:69b:aeff:fe53:743e", &f, &b) >= 0);
426 assert_se(in_addr_is_multicast(f, &b) == 0);
427 }
428
429 static void test_getpeercred_getpeergroups(void) {
430 int r;
431
432 r = safe_fork("(getpeercred)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
433 assert_se(r >= 0);
434
435 if (r == 0) {
436 static const gid_t gids[] = { 3, 4, 5, 6, 7 };
437 gid_t *test_gids;
438 size_t n_test_gids;
439 uid_t test_uid;
440 gid_t test_gid;
441 struct ucred ucred;
442 int pair[2];
443
444 if (geteuid() == 0) {
445 test_uid = 1;
446 test_gid = 2;
447 test_gids = (gid_t*) gids;
448 n_test_gids = ELEMENTSOF(gids);
449
450 assert_se(setgroups(n_test_gids, test_gids) >= 0);
451 assert_se(setresgid(test_gid, test_gid, test_gid) >= 0);
452 assert_se(setresuid(test_uid, test_uid, test_uid) >= 0);
453
454 } else {
455 long ngroups_max;
456
457 test_uid = getuid();
458 test_gid = getgid();
459
460 ngroups_max = sysconf(_SC_NGROUPS_MAX);
461 assert(ngroups_max > 0);
462
463 test_gids = newa(gid_t, ngroups_max);
464
465 r = getgroups(ngroups_max, test_gids);
466 assert_se(r >= 0);
467 n_test_gids = (size_t) r;
468 }
469
470 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
471
472 assert_se(getpeercred(pair[0], &ucred) >= 0);
473
474 assert_se(ucred.uid == test_uid);
475 assert_se(ucred.gid == test_gid);
476 assert_se(ucred.pid == getpid_cached());
477
478 {
479 _cleanup_free_ gid_t *peer_groups = NULL;
480
481 r = getpeergroups(pair[0], &peer_groups);
482 assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
483
484 if (r >= 0) {
485 assert_se((size_t) r == n_test_gids);
486 assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
487 }
488 }
489
490 safe_close_pair(pair);
491 _exit(EXIT_SUCCESS);
492 }
493 }
494
495 static void test_passfd_read(void) {
496 static const char file_contents[] = "test contents for passfd";
497 _cleanup_close_pair_ int pair[2] = { -1, -1 };
498 int r;
499
500 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
501
502 r = safe_fork("(passfd_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
503 assert_se(r >= 0);
504
505 if (r == 0) {
506 /* Child */
507 char tmpfile[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
508 _cleanup_close_ int tmpfd = -1;
509
510 pair[0] = safe_close(pair[0]);
511
512 tmpfd = mkostemp_safe(tmpfile);
513 assert_se(tmpfd >= 0);
514 assert_se(write(tmpfd, file_contents, strlen(file_contents)) == (ssize_t) strlen(file_contents));
515 tmpfd = safe_close(tmpfd);
516
517 tmpfd = open(tmpfile, O_RDONLY);
518 assert_se(tmpfd >= 0);
519 assert_se(unlink(tmpfile) == 0);
520
521 assert_se(send_one_fd(pair[1], tmpfd, MSG_DONTWAIT) == 0);
522 _exit(EXIT_SUCCESS);
523 }
524
525 /* Parent */
526 char buf[64];
527 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
528 _cleanup_close_ int fd = -1;
529
530 pair[1] = safe_close(pair[1]);
531
532 assert_se(receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd) == 0);
533
534 assert_se(fd >= 0);
535 r = read(fd, buf, sizeof(buf)-1);
536 assert_se(r >= 0);
537 buf[r] = 0;
538 assert_se(streq(buf, file_contents));
539 }
540
541 static void test_passfd_contents_read(void) {
542 _cleanup_close_pair_ int pair[2] = { -1, -1 };
543 static const char file_contents[] = "test contents in the file";
544 static const char wire_contents[] = "test contents on the wire";
545 int r;
546
547 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
548
549 r = safe_fork("(passfd_contents_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
550 assert_se(r >= 0);
551
552 if (r == 0) {
553 /* Child */
554 struct iovec iov = IOVEC_INIT_STRING(wire_contents);
555 char tmpfile[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
556 _cleanup_close_ int tmpfd = -1;
557
558 pair[0] = safe_close(pair[0]);
559
560 tmpfd = mkostemp_safe(tmpfile);
561 assert_se(tmpfd >= 0);
562 assert_se(write(tmpfd, file_contents, strlen(file_contents)) == (ssize_t) strlen(file_contents));
563 tmpfd = safe_close(tmpfd);
564
565 tmpfd = open(tmpfile, O_RDONLY);
566 assert_se(tmpfd >= 0);
567 assert_se(unlink(tmpfile) == 0);
568
569 assert_se(send_one_fd_iov(pair[1], tmpfd, &iov, 1, MSG_DONTWAIT) > 0);
570 _exit(EXIT_SUCCESS);
571 }
572
573 /* Parent */
574 char buf[64];
575 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
576 _cleanup_close_ int fd = -1;
577 ssize_t k;
578
579 pair[1] = safe_close(pair[1]);
580
581 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
582 assert_se(k > 0);
583 buf[k] = 0;
584 assert_se(streq(buf, wire_contents));
585
586 assert_se(fd >= 0);
587 r = read(fd, buf, sizeof(buf)-1);
588 assert_se(r >= 0);
589 buf[r] = 0;
590 assert_se(streq(buf, file_contents));
591 }
592
593 static void test_receive_nopassfd(void) {
594 _cleanup_close_pair_ int pair[2] = { -1, -1 };
595 static const char wire_contents[] = "no fd passed here";
596 int r;
597
598 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
599
600 r = safe_fork("(receive_nopassfd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
601 assert_se(r >= 0);
602
603 if (r == 0) {
604 /* Child */
605 struct iovec iov = IOVEC_INIT_STRING(wire_contents);
606
607 pair[0] = safe_close(pair[0]);
608
609 assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) > 0);
610 _exit(EXIT_SUCCESS);
611 }
612
613 /* Parent */
614 char buf[64];
615 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
616 int fd = -999;
617 ssize_t k;
618
619 pair[1] = safe_close(pair[1]);
620
621 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
622 assert_se(k > 0);
623 buf[k] = 0;
624 assert_se(streq(buf, wire_contents));
625
626 /* no fd passed here, confirm it was reset */
627 assert_se(fd == -1);
628 }
629
630 static void test_send_nodata_nofd(void) {
631 _cleanup_close_pair_ int pair[2] = { -1, -1 };
632 int r;
633
634 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
635
636 r = safe_fork("(send_nodata_nofd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
637 assert_se(r >= 0);
638
639 if (r == 0) {
640 /* Child */
641 pair[0] = safe_close(pair[0]);
642
643 assert_se(send_one_fd_iov(pair[1], -1, NULL, 0, MSG_DONTWAIT) == -EINVAL);
644 _exit(EXIT_SUCCESS);
645 }
646
647 /* Parent */
648 char buf[64];
649 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
650 int fd = -999;
651 ssize_t k;
652
653 pair[1] = safe_close(pair[1]);
654
655 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
656 /* recvmsg() will return errno EAGAIN if nothing was sent */
657 assert_se(k == -EAGAIN);
658
659 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
660 assert_se(fd == -999);
661 }
662
663 static void test_send_emptydata(void) {
664 _cleanup_close_pair_ int pair[2] = { -1, -1 };
665 int r;
666
667 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
668
669 r = safe_fork("(send_emptydata)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
670 assert_se(r >= 0);
671
672 if (r == 0) {
673 /* Child */
674 struct iovec iov = IOVEC_INIT_STRING(""); /* zero-length iov */
675 assert_se(iov.iov_len == 0);
676
677 pair[0] = safe_close(pair[0]);
678
679 /* This will succeed, since iov is set. */
680 assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) == 0);
681 _exit(EXIT_SUCCESS);
682 }
683
684 /* Parent */
685 char buf[64];
686 struct iovec iov = IOVEC_INIT(buf, sizeof(buf)-1);
687 int fd = -999;
688 ssize_t k;
689
690 pair[1] = safe_close(pair[1]);
691
692 k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
693 /* receive_one_fd_iov() returns -EIO if an fd is not found and no data was returned. */
694 assert_se(k == -EIO);
695
696 /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
697 assert_se(fd == -999);
698 }
699
700 int main(int argc, char *argv[]) {
701
702 test_setup_logging(LOG_DEBUG);
703
704 test_ifname_valid();
705
706 test_socket_address_parse();
707 test_socket_address_parse_netlink();
708 test_socket_address_equal();
709 test_socket_address_get_path();
710 test_socket_address_is();
711 test_socket_address_is_netlink();
712
713 test_in_addr_is_null();
714 test_in_addr_prefix_intersect();
715 test_in_addr_prefix_next();
716 test_in_addr_to_string();
717 test_in_addr_ifindex_to_string();
718 test_in_addr_ifindex_from_string_auto();
719
720 test_sockaddr_equal();
721
722 test_sockaddr_un_len();
723
724 test_in_addr_is_multicast();
725
726 test_getpeercred_getpeergroups();
727
728 test_passfd_read();
729 test_passfd_contents_read();
730 test_receive_nopassfd();
731 test_send_nodata_nofd();
732 test_send_emptydata();
733
734 return 0;
735 }