• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 20669300594

02 Jan 2026 09:00PM UTC coverage: 72.677% (-0.02%) from 72.697%
20669300594

push

github

web-flow
clang-tidy: Enable more warnings (#39910)

25 of 27 new or added lines in 3 files covered. (92.59%)

5655 existing lines in 111 files now uncovered.

310023 of 426578 relevant lines covered (72.68%)

1136999.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/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 <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

24
assert_cc(SUN_PATH_LEN == 108);
25

26
TEST(ifname_valid) {
1✔
27
        assert_se( ifname_valid("foo"));
1✔
28
        assert_se( ifname_valid("eth0"));
1✔
29

30
        assert_se(!ifname_valid("0"));
1✔
31
        assert_se(!ifname_valid("99"));
1✔
32
        assert_se( ifname_valid("a99"));
1✔
33
        assert_se( ifname_valid("99a"));
1✔
34

35
        assert_se(!ifname_valid(NULL));
1✔
36
        assert_se(!ifname_valid(""));
1✔
37
        assert_se(!ifname_valid(" "));
1✔
38
        assert_se(!ifname_valid(" foo"));
1✔
39
        assert_se(!ifname_valid("bar\n"));
1✔
40
        assert_se(!ifname_valid("."));
1✔
41
        assert_se(!ifname_valid(".."));
1✔
42
        assert_se(ifname_valid("foo.bar"));
1✔
43
        assert_se(!ifname_valid("x:y"));
1✔
44

45
        assert_se( ifname_valid_full("xxxxxxxxxxxxxxx", 0));
1✔
46
        assert_se(!ifname_valid_full("xxxxxxxxxxxxxxxx", 0));
1✔
47
        assert_se( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
1✔
48
        assert_se( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
1✔
49
        assert_se(!ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE));
1✔
50
        assert_se( ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
1✔
51
        assert_se(!ifname_valid_full("0", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
1✔
52
}
1✔
53

54
static void test_socket_print_unix_one(const char *in, size_t len_in, const char *expected) {
8✔
55
        _cleanup_free_ char *out = NULL, *c = NULL;
×
56

57
        assert_se(len_in <= SUN_PATH_LEN);
8✔
58
        SocketAddress a = { .sockaddr = { .un = { .sun_family = AF_UNIX } },
8✔
59
                            .size = offsetof(struct sockaddr_un, sun_path) + len_in,
8✔
60
                            .type = SOCK_STREAM,
61
        };
62
        memcpy(a.sockaddr.un.sun_path, in, len_in);
8✔
63

64
        assert_se(socket_address_print(&a, &out) >= 0);
8✔
65
        assert_se(c = cescape(in));
8✔
66
        log_info("\"%s\" → \"%s\" (expect \"%s\")", in, out, expected);
8✔
67
        ASSERT_STREQ(out, expected);
8✔
68
}
8✔
69

70
TEST(socket_print_unix) {
1✔
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");
1✔
74
        test_socket_print_unix_one("@abs", 5, "@abs");
1✔
75
        test_socket_print_unix_one("\n", 2, "\\n");
1✔
76
        test_socket_print_unix_one("", 1, "<unnamed>");
1✔
77
        test_socket_print_unix_one("\0", 1, "<unnamed>");
1✔
78
        test_socket_print_unix_one("\0_________________________there's 108 characters in this string_____________________________________________", 108,
1✔
79
                                   "@_________________________there\\'s 108 characters in this string_____________________________________________");
80
        test_socket_print_unix_one("////////////////////////////////////////////////////////////////////////////////////////////////////////////", 108,
1✔
81
                                   "////////////////////////////////////////////////////////////////////////////////////////////////////////////");
82
        test_socket_print_unix_one("\0\a\b\n\255", 6, "@\\a\\b\\n\\255\\000");
1✔
83
}
1✔
84

85
TEST(sockaddr_equal) {
1✔
86
        union sockaddr_union a = {
1✔
87
                .in.sin_family = AF_INET,
88
                .in.sin_port = 0,
89
                .in.sin_addr.s_addr = htobe32(INADDR_ANY),
1✔
90
        };
91
        union sockaddr_union b = {
1✔
92
                .in.sin_family = AF_INET,
93
                .in.sin_port = 0,
94
                .in.sin_addr.s_addr = htobe32(INADDR_ANY),
1✔
95
        };
96
        union sockaddr_union c = {
1✔
97
                .in.sin_family = AF_INET,
98
                .in.sin_port = 0,
99
                .in.sin_addr.s_addr = htobe32(1234),
1✔
100
        };
101
        union sockaddr_union d = {
1✔
102
                .in6.sin6_family = AF_INET6,
103
                .in6.sin6_port = 0,
104
                .in6.sin6_addr = IN6ADDR_ANY_INIT,
105
        };
106
        union sockaddr_union e = {
1✔
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));
1✔
113
        assert_se(sockaddr_equal(&a, &b));
1✔
114
        assert_se(sockaddr_equal(&d, &d));
1✔
115
        assert_se(sockaddr_equal(&e, &e));
1✔
116
        assert_se(!sockaddr_equal(&a, &c));
1✔
117
        assert_se(!sockaddr_equal(&b, &c));
1✔
118
        assert_se(!sockaddr_equal(&a, &e));
1✔
119
}
1✔
120

121
TEST(sockaddr_un_len) {
1✔
122
        static const struct sockaddr_un fs = {
1✔
123
                .sun_family = AF_UNIX,
124
                .sun_path = "/foo/bar/waldo",
125
        };
126

127
        static const struct sockaddr_un abstract = {
1✔
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);
1✔
133
        assert_se(sockaddr_un_len(&abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1));
1✔
134
}
1✔
135

136
TEST(in_addr_is_multicast) {
1✔
137
        union in_addr_union a, b;
1✔
138
        int f;
1✔
139

140
        assert_se(in_addr_from_string_auto("192.168.3.11", &f, &a) >= 0);
1✔
141
        assert_se(in_addr_is_multicast(f, &a) == 0);
1✔
142

143
        assert_se(in_addr_from_string_auto("224.0.0.1", &f, &a) >= 0);
1✔
144
        assert_se(in_addr_is_multicast(f, &a) == 1);
1✔
145

146
        assert_se(in_addr_from_string_auto("FF01:0:0:0:0:0:0:1", &f, &b) >= 0);
1✔
147
        assert_se(in_addr_is_multicast(f, &b) == 1);
1✔
148

149
        assert_se(in_addr_from_string_auto("2001:db8::c:69b:aeff:fe53:743e", &f, &b) >= 0);
1✔
150
        assert_se(in_addr_is_multicast(f, &b) == 0);
1✔
151
}
1✔
152

153
TEST(getpeercred_getpeergroups) {
1✔
154
        int r;
1✔
155

156
        r = ASSERT_OK(pidref_safe_fork("(getpeercred)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
1✔
157

158
        if (r == 0) {
2✔
159
                static const gid_t gids[] = { 3, 4, 5, 6, 7 };
1✔
160
                gid_t *test_gids;
1✔
161
                size_t n_test_gids;
1✔
162
                uid_t test_uid;
1✔
163
                gid_t test_gid;
1✔
164
                struct ucred ucred;
1✔
165
                int pair[2] = EBADF_PAIR;
1✔
166

167
                if (geteuid() == 0 && !userns_has_single_user()) {
1✔
168
                        test_uid = 1;
1✔
169
                        test_gid = 2;
1✔
170
                        test_gids = (gid_t*) gids;
1✔
171
                        n_test_gids = ELEMENTSOF(gids);
1✔
172

173
                        assert_se(fully_set_uid_gid(test_uid, test_gid, test_gids, n_test_gids) >= 0);
1✔
174
                } else {
UNCOV
175
                        long ngroups_max;
×
176

UNCOV
177
                        test_uid = getuid();
×
178
                        test_gid = getgid();
×
179

UNCOV
180
                        ngroups_max = sysconf(_SC_NGROUPS_MAX);
×
181
                        assert_se(ngroups_max > 0);
×
182

UNCOV
183
                        test_gids = newa(gid_t, ngroups_max);
×
184

UNCOV
185
                        r = getgroups(ngroups_max, test_gids);
×
186
                        assert_se(r >= 0);
×
187
                        n_test_gids = (size_t) r;
×
188
                }
189

190
                assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
1✔
191

192
                assert_se(getpeercred(pair[0], &ucred) >= 0);
1✔
193

194
                assert_se(ucred.uid == test_uid);
1✔
195
                assert_se(ucred.gid == test_gid);
1✔
196
                assert_se(ucred.pid == getpid_cached());
1✔
197

198
                {
199
                        _cleanup_free_ gid_t *peer_groups = NULL;
2✔
200

201
                        r = getpeergroups(pair[0], &peer_groups);
1✔
202
                        assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
1✔
203

UNCOV
204
                        if (r >= 0) {
×
205
                                assert_se((size_t) r == n_test_gids);
1✔
206
                                assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
1✔
207
                        }
208
                }
209

210
                safe_close_pair(pair);
1✔
211
                _exit(EXIT_SUCCESS);
1✔
212
        }
213
}
1✔
214

215
TEST(passfd_read) {
1✔
216
        static const char file_contents[] = "test contents for passfd";
1✔
217
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
218
        int r;
1✔
219

220
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
221

222
        r = ASSERT_OK(pidref_safe_fork("(passfd_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
1✔
223

224
        if (r == 0) {
2✔
225
                /* Child */
226
                pair[0] = safe_close(pair[0]);
1✔
227

228
                char tmpfile[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
1✔
229
                assert_se(write_tmpfile(tmpfile, file_contents) == 0);
1✔
230

231
                _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
1✔
232
                assert_se(tmpfd >= 0);
1✔
233
                assert_se(unlink(tmpfile) == 0);
1✔
234

235
                assert_se(send_one_fd(pair[1], tmpfd, MSG_DONTWAIT) == 0);
1✔
236
                _exit(EXIT_SUCCESS);
1✔
237
        }
238

239
        /* Parent */
240
        char buf[64];
1✔
241
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
242
        _cleanup_close_ int fd = -EBADF;
1✔
243

244
        pair[1] = safe_close(pair[1]);
1✔
245

246
        assert_se(receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd) == 0);
1✔
247

248
        assert_se(fd >= 0);
1✔
249
        ssize_t n = read(fd, buf, sizeof(buf)-1);
1✔
250
        assert_se(n >= 0);
1✔
251
        buf[n] = 0;
1✔
252
        ASSERT_STREQ(buf, file_contents);
1✔
253
}
1✔
254

255
TEST(passfd_contents_read) {
1✔
256
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
257
        static const char file_contents[] = "test contents in the file";
1✔
258
        static const char wire_contents[] = "test contents on the wire";
1✔
259
        int r;
1✔
260

261
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
262

263
        r = ASSERT_OK(pidref_safe_fork("(passfd_contents_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
1✔
264

265
        if (r == 0) {
2✔
266
                /* Child */
267
                struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
1✔
268
                char tmpfile[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
1✔
269

270
                pair[0] = safe_close(pair[0]);
1✔
271

272
                assert_se(write_tmpfile(tmpfile, file_contents) == 0);
1✔
273

274
                _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
1✔
275
                assert_se(tmpfd >= 0);
1✔
276
                assert_se(unlink(tmpfile) == 0);
1✔
277

278
                assert_se(send_one_fd_iov(pair[1], tmpfd, &iov, 1, MSG_DONTWAIT) > 0);
1✔
279
                _exit(EXIT_SUCCESS);
1✔
280
        }
281

282
        /* Parent */
283
        char buf[64];
1✔
284
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
285
        _cleanup_close_ int fd = -EBADF;
1✔
286
        ssize_t k;
1✔
287

288
        pair[1] = safe_close(pair[1]);
1✔
289

290
        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
1✔
291
        assert_se(k > 0);
1✔
292
        buf[k] = 0;
1✔
293
        ASSERT_STREQ(buf, wire_contents);
1✔
294

295
        assert_se(fd >= 0);
1✔
296
        r = read(fd, buf, sizeof(buf)-1);
1✔
297
        assert_se(r >= 0);
1✔
298
        buf[r] = 0;
1✔
299
        ASSERT_STREQ(buf, file_contents);
1✔
300
}
1✔
301

302
TEST(pass_many_fds_contents_read) {
1✔
303
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
304
        static const char file_contents[][STRLEN("test contents in the fileX") + 1] = {
1✔
305
                "test contents in the file0",
306
                "test contents in the file1",
307
                "test contents in the file2"
308
        };
309
        static const char wire_contents[] = "test contents on the wire";
1✔
310
        int r;
1✔
311

312
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
313

314
        r = ASSERT_OK(pidref_safe_fork("(passfd_contents_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
1✔
315

316
        if (r == 0) {
2✔
317
                /* Child */
318
                struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
1✔
319
                char tmpfile[][STRLEN("/tmp/test-socket-util-passfd-contents-read-XXXXXX") + 1] = {
1✔
320
                        "/tmp/test-socket-util-passfd-contents-read-XXXXXX",
321
                        "/tmp/test-socket-util-passfd-contents-read-XXXXXX",
322
                        "/tmp/test-socket-util-passfd-contents-read-XXXXXX"
323
                };
324
                int tmpfds[3] = EBADF_TRIPLET;
1✔
325

326
                pair[0] = safe_close(pair[0]);
1✔
327

328
                for (size_t i = 0; i < 3; ++i) {
4✔
329
                        assert_se(write_tmpfile(tmpfile[i], file_contents[i]) == 0);
3✔
330
                        tmpfds[i] = open(tmpfile[i], O_RDONLY);
3✔
331
                        assert_se(tmpfds[i] >= 0);
3✔
332
                        assert_se(unlink(tmpfile[i]) == 0);
3✔
333
                }
334

335
                assert_se(send_many_fds_iov(pair[1], tmpfds, 3, &iov, 1, MSG_DONTWAIT) > 0);
1✔
336
                close_many(tmpfds, 3);
1✔
337
                _exit(EXIT_SUCCESS);
1✔
338
        }
339

340
        /* Parent */
341
        char buf[64];
1✔
342
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
343
        _cleanup_free_ int *fds = NULL;
1✔
344
        size_t n_fds = 0;
1✔
345
        ssize_t k;
1✔
346

347
        pair[1] = safe_close(pair[1]);
1✔
348

349
        k = receive_many_fds_iov(pair[0], &iov, 1, &fds, &n_fds, MSG_DONTWAIT);
1✔
350
        assert_se(k > 0);
1✔
351
        buf[k] = 0;
1✔
352
        ASSERT_STREQ(buf, wire_contents);
1✔
353

354
        assert_se(n_fds == 3);
1✔
355

356
        for (size_t i = 0; i < 3; ++i) {
4✔
357
                assert_se(fds[i] >= 0);
3✔
358
                r = read(fds[i], buf, sizeof(buf)-1);
3✔
359
                assert_se(r >= 0);
3✔
360
                buf[r] = 0;
3✔
361
                ASSERT_STREQ(buf, file_contents[i]);
3✔
362
                safe_close(fds[i]);
3✔
363
        }
364
}
1✔
365

366
TEST(receive_nopassfd) {
1✔
367
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
368
        static const char wire_contents[] = "no fd passed here";
1✔
369
        int r;
1✔
370

371
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
372

373
        r = ASSERT_OK(pidref_safe_fork("(receive_nopassfd)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
1✔
374
        assert_se(r >= 0);
2✔
375

376
        if (r == 0) {
2✔
377
                /* Child */
378
                struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
1✔
379

380
                pair[0] = safe_close(pair[0]);
1✔
381

382
                assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) > 0);
1✔
383
                _exit(EXIT_SUCCESS);
1✔
384
        }
385

386
        /* Parent */
387
        char buf[64];
1✔
388
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
389
        int fd = -999;
1✔
390
        ssize_t k;
1✔
391

392
        pair[1] = safe_close(pair[1]);
1✔
393

394
        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
1✔
395
        assert_se(k > 0);
1✔
396
        buf[k] = 0;
1✔
397
        ASSERT_STREQ(buf, wire_contents);
1✔
398

399
        /* no fd passed here, confirm it was reset */
400
        assert_se(fd == -EBADF);
1✔
401
}
1✔
402

403
TEST(send_nodata_nofd) {
1✔
404
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
405
        int r;
1✔
406

407
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
408

409
        r = ASSERT_OK(pidref_safe_fork("(send_nodata_nofd)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
1✔
410

411
        if (r == 0) {
2✔
412
                /* Child */
413
                pair[0] = safe_close(pair[0]);
1✔
414

415
                assert_se(send_one_fd_iov(pair[1], -1, NULL, 0, MSG_DONTWAIT) == -EINVAL);
1✔
416
                _exit(EXIT_SUCCESS);
1✔
417
        }
418

419
        /* Parent */
420
        char buf[64];
1✔
421
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
422
        int fd = -999;
1✔
423
        ssize_t k;
1✔
424

425
        pair[1] = safe_close(pair[1]);
1✔
426

427
        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
1✔
428
        /* recvmsg() will return errno EAGAIN if nothing was sent */
429
        assert_se(k == -EAGAIN);
1✔
430

431
        /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
432
        assert_se(fd == -999);
1✔
433
}
1✔
434

435
TEST(send_emptydata) {
1✔
436
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
437
        int r;
1✔
438

439
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
440

441
        r = ASSERT_OK(pidref_safe_fork("(send_emptydata)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
1✔
442

443
        if (r == 0) {
2✔
444
                /* Child */
445
                pair[0] = safe_close(pair[0]);
1✔
446

447
                /* This will succeed, since iov is set. */
448
                assert_se(send_one_fd_iov(pair[1], -1, &iovec_empty, 1, MSG_DONTWAIT) == 0);
1✔
449
                _exit(EXIT_SUCCESS);
1✔
450
        }
451

452
        /* Parent */
453
        char buf[64];
1✔
454
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
455
        int fd = -999;
1✔
456
        ssize_t k;
1✔
457

458
        pair[1] = safe_close(pair[1]);
1✔
459

460
        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
1✔
461
        /* receive_one_fd_iov() returns -EIO if an fd is not found and no data was returned. */
462
        assert_se(k == -EIO);
1✔
463

464
        /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
465
        assert_se(fd == -999);
1✔
466
}
1✔
467

468
TEST(flush_accept) {
1✔
469
        _cleanup_close_ int listen_stream, listen_dgram, listen_seqpacket, connect_stream, connect_dgram, connect_seqpacket;
5✔
470
        static const union sockaddr_union sa = { .un.sun_family = AF_UNIX };
1✔
471
        union sockaddr_union lsa;
1✔
472
        socklen_t l;
1✔
473

474
        listen_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
475
        assert_se(listen_stream >= 0);
1✔
476

477
        listen_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
478
        assert_se(listen_dgram >= 0);
1✔
479

480
        listen_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
481
        assert_se(listen_seqpacket >= 0);
1✔
482

483
        assert_se(flush_accept(listen_stream) < 0);
1✔
484
        assert_se(flush_accept(listen_dgram) < 0);
1✔
485
        assert_se(flush_accept(listen_seqpacket) < 0);
1✔
486

487
        assert_se(bind(listen_stream, &sa.sa, sizeof(sa_family_t)) >= 0);
1✔
488
        assert_se(bind(listen_dgram, &sa.sa, sizeof(sa_family_t)) >= 0);
1✔
489
        assert_se(bind(listen_seqpacket, &sa.sa, sizeof(sa_family_t)) >= 0);
1✔
490

491
        assert_se(flush_accept(listen_stream) < 0);
1✔
492
        assert_se(flush_accept(listen_dgram) < 0);
1✔
493
        assert_se(flush_accept(listen_seqpacket) < 0);
1✔
494

495
        assert_se(listen(listen_stream, SOMAXCONN_DELUXE) >= 0);
1✔
496
        assert_se(listen(listen_dgram, SOMAXCONN_DELUXE) < 0);
1✔
497
        assert_se(listen(listen_seqpacket, SOMAXCONN_DELUXE) >= 0);
1✔
498

499
        assert_se(flush_accept(listen_stream) >= 0);
1✔
500
        assert_se(flush_accept(listen_dgram) < 0);
1✔
501
        assert_se(flush_accept(listen_seqpacket) >= 0);
1✔
502

503
        connect_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
504
        assert_se(connect_stream >= 0);
1✔
505

506
        connect_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
507
        assert_se(connect_dgram >= 0);
1✔
508

509
        connect_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
510
        assert_se(connect_seqpacket >= 0);
1✔
511

512
        l = sizeof(lsa);
1✔
513
        assert_se(getsockname(listen_stream, &lsa.sa, &l) >= 0);
1✔
514
        assert_se(connect(connect_stream, &lsa.sa, l) >= 0);
1✔
515

516
        l = sizeof(lsa);
1✔
517
        assert_se(getsockname(listen_dgram, &lsa.sa, &l) >= 0);
1✔
518
        assert_se(connect(connect_dgram, &lsa.sa, l) >= 0);
1✔
519

520
        l = sizeof(lsa);
1✔
521
        assert_se(getsockname(listen_seqpacket, &lsa.sa, &l) >= 0);
1✔
522
        assert_se(connect(connect_seqpacket, &lsa.sa, l) >= 0);
1✔
523

524
        assert_se(flush_accept(listen_stream) >= 0);
1✔
525
        assert_se(flush_accept(listen_dgram) < 0);
1✔
526
        assert_se(flush_accept(listen_seqpacket) >= 0);
1✔
527
}
1✔
528

529
TEST(ipv6_enabled) {
1✔
530
        log_info("IPv6 supported: %s", yes_no(socket_ipv6_is_supported()));
2✔
531
        log_info("IPv6 enabled: %s", yes_no(socket_ipv6_is_enabled()));
2✔
532
}
1✔
533

534
TEST(sockaddr_un_set_path) {
1✔
535
        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
1✔
UNCOV
536
        _cleanup_(unlink_and_freep) char *sh = NULL;
×
537
        _cleanup_free_ char *j = NULL;
1✔
538
        union sockaddr_union sa;
1✔
539
        _cleanup_close_ int fd1, fd2, fd3;
3✔
540

541
        assert_se(mkdtemp_malloc("/tmp/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXX", &t) >= 0);
1✔
542
        assert_se(strlen(t) > SUN_PATH_LEN);
1✔
543

544
        assert_se(j = path_join(t, "sock"));
1✔
545
        assert_se(sockaddr_un_set_path(&sa.un, j) == -ENAMETOOLONG); /* too long for AF_UNIX socket */
1✔
546

547
        assert_se(asprintf(&sh, "/tmp/%" PRIx64, random_u64()) >= 0);
1✔
548
        assert_se(symlink(t, sh) >= 0); /* create temporary symlink, to access it anyway */
1✔
549

550
        free(j);
1✔
551
        assert_se(j = path_join(sh, "sock"));
1✔
552
        assert_se(sockaddr_un_set_path(&sa.un, j) >= 0);
1✔
553

554
        fd1 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1✔
555
        assert_se(fd1 >= 0);
1✔
556
        assert_se(bind(fd1, &sa.sa, sockaddr_len(&sa)) >= 0);
1✔
557
        assert_se(listen(fd1, 1) >= 0);
1✔
558

559
        sh = unlink_and_free(sh); /* remove temporary symlink */
1✔
560

561
        fd2 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1✔
562
        assert_se(fd2 >= 0);
1✔
563
        assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) < 0);
1✔
564
        assert_se(errno == ENOENT); /* we removed the symlink, must fail */
1✔
565

566
        free(j);
1✔
567
        assert_se(j = path_join(t, "sock"));
1✔
568

569
        fd3 = open(j, O_CLOEXEC|O_PATH|O_NOFOLLOW);
1✔
570
        assert_se(fd3 > 0);
1✔
571
        assert_se(sockaddr_un_set_path(&sa.un, FORMAT_PROC_FD_PATH(fd3)) >= 0); /* connect via O_PATH instead, circumventing 108ch limit */
1✔
572

573
        assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) >= 0);
1✔
574
}
1✔
575

576
TEST(getpeerpidref) {
1✔
577
        _cleanup_close_pair_ int fd[2] = EBADF_PAIR;
2✔
578

579
        ASSERT_OK(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fd));
1✔
580

581
        _cleanup_(pidref_done) PidRef pidref0 = PIDREF_NULL, pidref1 = PIDREF_NULL, pidref_self = PIDREF_NULL, pidref_pid1 = PIDREF_NULL;
1✔
582
        ASSERT_OK(getpeerpidref(fd[0], &pidref0));
1✔
583
        ASSERT_OK(getpeerpidref(fd[1], &pidref1));
1✔
584

585
        ASSERT_OK(pidref_set_self(&pidref_self));
1✔
586
        ASSERT_OK(pidref_set_pid(&pidref_pid1, 1));
1✔
587

588
        ASSERT_TRUE(pidref_equal(&pidref0, &pidref1));
1✔
589
        ASSERT_TRUE(pidref_equal(&pidref0, &pidref_self));
1✔
590
        ASSERT_TRUE(pidref_equal(&pidref1, &pidref_self));
1✔
591

592
        ASSERT_TRUE(!pidref_equal(&pidref_self, &pidref_pid1));
1✔
593
        ASSERT_TRUE(!pidref_equal(&pidref1, &pidref_pid1));
1✔
594
        ASSERT_TRUE(!pidref_equal(&pidref0, &pidref_pid1));
1✔
595
}
1✔
596

597
DEFINE_TEST_MAIN(LOG_DEBUG);
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc