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

systemd / systemd / 25351067578

04 May 2026 06:45PM UTC coverage: 72.674% (+0.7%) from 71.943%
25351067578

push

github

keszybz
test: suppress PCR public key auto-loading in TEST-70-TPM2 dditest

The dditest block calls systemd-repart with Encrypt=tpm2 but without
--tpm2-public-key-pcrs=. Since systemd-stub drops
/run/systemd/tpm2-pcr-public-key.pem when booting from a signed UKI
systemd-repart auto-loads it and enrolls a signed PCR policy, and
then systemd-cryptsetup tpm2-device=auto has no matching signature file,
so unlock fails.

--tpm2-public-key= is not enough as the default kicks in then.

Follow-up for cd18656d4

325892 of 448432 relevant lines covered (72.67%)

1195556.07 hits per line

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

96.54
/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 <linux/pkt_sched.h>
6
#include <netinet/ip.h>
7
#include <unistd.h>
8

9
#include "alloc-util.h"
10
#include "escape.h"
11
#include "fd-util.h"
12
#include "fs-util.h"
13
#include "in-addr-util.h"
14
#include "iovec-util.h"
15
#include "log.h"
16
#include "path-util.h"
17
#include "pidref.h"
18
#include "process-util.h"
19
#include "random-util.h"
20
#include "rm-rf.h"
21
#include "socket-util.h"
22
#include "tests.h"
23
#include "tmpfile-util.h"
24
#include "user-util.h"
25

26
assert_cc(SUN_PATH_LEN == 108);
27

28
TEST(ifname_valid) {
1✔
29
        assert_se( ifname_valid("foo"));
1✔
30
        assert_se( ifname_valid("eth0"));
1✔
31

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

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

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

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

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

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

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

87
TEST(sockaddr_equal) {
1✔
88
        union sockaddr_union a = {
1✔
89
                .in.sin_family = AF_INET,
90
                .in.sin_port = 0,
91
                .in.sin_addr.s_addr = htobe32(INADDR_ANY),
1✔
92
        };
93
        union sockaddr_union b = {
1✔
94
                .in.sin_family = AF_INET,
95
                .in.sin_port = 0,
96
                .in.sin_addr.s_addr = htobe32(INADDR_ANY),
1✔
97
        };
98
        union sockaddr_union c = {
1✔
99
                .in.sin_family = AF_INET,
100
                .in.sin_port = 0,
101
                .in.sin_addr.s_addr = htobe32(1234),
1✔
102
        };
103
        union sockaddr_union d = {
1✔
104
                .in6.sin6_family = AF_INET6,
105
                .in6.sin6_port = 0,
106
                .in6.sin6_addr = IN6ADDR_ANY_INIT,
107
        };
108
        union sockaddr_union e = {
1✔
109
                .vm.svm_family = AF_VSOCK,
110
                .vm.svm_port = 0,
111
                .vm.svm_cid = VMADDR_CID_ANY,
112
        };
113

114
        assert_se(sockaddr_equal(&a, &a));
1✔
115
        assert_se(sockaddr_equal(&a, &b));
1✔
116
        assert_se(sockaddr_equal(&d, &d));
1✔
117
        assert_se(sockaddr_equal(&e, &e));
1✔
118
        assert_se(!sockaddr_equal(&a, &c));
1✔
119
        assert_se(!sockaddr_equal(&b, &c));
1✔
120
        assert_se(!sockaddr_equal(&a, &e));
1✔
121
}
1✔
122

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

129
        static const struct sockaddr_un abstract = {
1✔
130
                .sun_family = AF_UNIX,
131
                .sun_path = "\0foobar",
132
        };
133

134
        assert_se(sockaddr_un_len(&fs) == offsetof(struct sockaddr_un, sun_path) + strlen(fs.sun_path) + 1);
1✔
135
        assert_se(sockaddr_un_len(&abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1));
1✔
136
}
1✔
137

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

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

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

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

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

155
TEST(getpeercred_getpeergroups) {
1✔
156
        int r;
1✔
157

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

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

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

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

179
                        test_uid = getuid();
×
180
                        test_gid = getgid();
×
181

182
                        ngroups_max = sysconf(_SC_NGROUPS_MAX);
×
183
                        assert_se(ngroups_max > 0);
×
184

185
                        test_gids = newa(gid_t, ngroups_max);
×
186

187
                        r = getgroups(ngroups_max, test_gids);
×
188
                        assert_se(r >= 0);
×
189
                        n_test_gids = (size_t) r;
×
190
                }
191

192
                assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
1✔
193

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

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

200
                {
201
                        _cleanup_free_ gid_t *peer_groups = NULL;
2✔
202

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

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

212
                safe_close_pair(pair);
1✔
213
                _exit(EXIT_SUCCESS);
1✔
214
        }
215
}
1✔
216

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

222
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
223

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

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

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

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

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

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

246
        pair[1] = safe_close(pair[1]);
1✔
247

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

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

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

263
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
264

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

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

272
                pair[0] = safe_close(pair[0]);
1✔
273

274
                assert_se(write_tmpfile(tmpfile, file_contents) == 0);
1✔
275

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

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

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

290
        pair[1] = safe_close(pair[1]);
1✔
291

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

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

304
TEST(receive_nopassfd) {
1✔
305
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
306
        static const char wire_contents[] = "no fd passed here";
1✔
307
        int r;
1✔
308

309
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
310

311
        r = ASSERT_OK(pidref_safe_fork("(receive_nopassfd)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL));
1✔
312
        assert_se(r >= 0);
2✔
313

314
        if (r == 0) {
2✔
315
                /* Child */
316
                struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
1✔
317

318
                pair[0] = safe_close(pair[0]);
1✔
319

320
                assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) > 0);
1✔
321
                _exit(EXIT_SUCCESS);
1✔
322
        }
323

324
        /* Parent */
325
        char buf[64];
1✔
326
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
327
        int fd = -999;
1✔
328
        ssize_t k;
1✔
329

330
        pair[1] = safe_close(pair[1]);
1✔
331

332
        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
1✔
333
        assert_se(k > 0);
1✔
334
        buf[k] = 0;
1✔
335
        ASSERT_STREQ(buf, wire_contents);
1✔
336

337
        /* no fd passed here, confirm it was reset */
338
        assert_se(fd == -EBADF);
1✔
339
}
1✔
340

341
TEST(send_nodata_nofd) {
1✔
342
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
343
        int r;
1✔
344

345
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
346

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

349
        if (r == 0) {
2✔
350
                /* Child */
351
                pair[0] = safe_close(pair[0]);
1✔
352

353
                assert_se(send_one_fd_iov(pair[1], -1, NULL, 0, MSG_DONTWAIT) == -EINVAL);
1✔
354
                _exit(EXIT_SUCCESS);
1✔
355
        }
356

357
        /* Parent */
358
        char buf[64];
1✔
359
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
360
        int fd = -999;
1✔
361
        ssize_t k;
1✔
362

363
        pair[1] = safe_close(pair[1]);
1✔
364

365
        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
1✔
366
        /* recvmsg() will return errno EAGAIN if nothing was sent */
367
        assert_se(k == -EAGAIN);
1✔
368

369
        /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
370
        assert_se(fd == -999);
1✔
371
}
1✔
372

373
TEST(send_emptydata) {
1✔
374
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
375
        int r;
1✔
376

377
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
378

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

381
        if (r == 0) {
2✔
382
                /* Child */
383
                pair[0] = safe_close(pair[0]);
1✔
384

385
                /* This will succeed, since iov is set. */
386
                assert_se(send_one_fd_iov(pair[1], -1, &iovec_empty, 1, MSG_DONTWAIT) == 0);
1✔
387
                _exit(EXIT_SUCCESS);
1✔
388
        }
389

390
        /* Parent */
391
        char buf[64];
1✔
392
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
393
        int fd = -999;
1✔
394
        ssize_t k;
1✔
395

396
        pair[1] = safe_close(pair[1]);
1✔
397

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

402
        /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
403
        assert_se(fd == -999);
1✔
404
}
1✔
405

406
TEST(flush_accept) {
1✔
407
        _cleanup_close_ int listen_stream, listen_dgram, listen_seqpacket, connect_stream, connect_dgram, connect_seqpacket;
5✔
408
        static const union sockaddr_union sa = { .un.sun_family = AF_UNIX };
1✔
409
        union sockaddr_union lsa;
1✔
410
        socklen_t l;
1✔
411

412
        listen_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
413
        assert_se(listen_stream >= 0);
1✔
414

415
        listen_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
416
        assert_se(listen_dgram >= 0);
1✔
417

418
        listen_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
419
        assert_se(listen_seqpacket >= 0);
1✔
420

421
        assert_se(flush_accept(listen_stream) < 0);
1✔
422
        assert_se(flush_accept(listen_dgram) < 0);
1✔
423
        assert_se(flush_accept(listen_seqpacket) < 0);
1✔
424

425
        assert_se(bind(listen_stream, &sa.sa, sizeof(sa_family_t)) >= 0);
1✔
426
        assert_se(bind(listen_dgram, &sa.sa, sizeof(sa_family_t)) >= 0);
1✔
427
        assert_se(bind(listen_seqpacket, &sa.sa, sizeof(sa_family_t)) >= 0);
1✔
428

429
        assert_se(flush_accept(listen_stream) < 0);
1✔
430
        assert_se(flush_accept(listen_dgram) < 0);
1✔
431
        assert_se(flush_accept(listen_seqpacket) < 0);
1✔
432

433
        assert_se(listen(listen_stream, SOMAXCONN_DELUXE) >= 0);
1✔
434
        assert_se(listen(listen_dgram, SOMAXCONN_DELUXE) < 0);
1✔
435
        assert_se(listen(listen_seqpacket, SOMAXCONN_DELUXE) >= 0);
1✔
436

437
        assert_se(flush_accept(listen_stream) >= 0);
1✔
438
        assert_se(flush_accept(listen_dgram) < 0);
1✔
439
        assert_se(flush_accept(listen_seqpacket) >= 0);
1✔
440

441
        connect_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
442
        assert_se(connect_stream >= 0);
1✔
443

444
        connect_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
445
        assert_se(connect_dgram >= 0);
1✔
446

447
        connect_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
448
        assert_se(connect_seqpacket >= 0);
1✔
449

450
        l = sizeof(lsa);
1✔
451
        assert_se(getsockname(listen_stream, &lsa.sa, &l) >= 0);
1✔
452
        assert_se(connect(connect_stream, &lsa.sa, l) >= 0);
1✔
453

454
        l = sizeof(lsa);
1✔
455
        assert_se(getsockname(listen_dgram, &lsa.sa, &l) >= 0);
1✔
456
        assert_se(connect(connect_dgram, &lsa.sa, l) >= 0);
1✔
457

458
        l = sizeof(lsa);
1✔
459
        assert_se(getsockname(listen_seqpacket, &lsa.sa, &l) >= 0);
1✔
460
        assert_se(connect(connect_seqpacket, &lsa.sa, l) >= 0);
1✔
461

462
        assert_se(flush_accept(listen_stream) >= 0);
1✔
463
        assert_se(flush_accept(listen_dgram) < 0);
1✔
464
        assert_se(flush_accept(listen_seqpacket) >= 0);
1✔
465
}
1✔
466

467
TEST(ipv6_enabled) {
1✔
468
        log_info("IPv6 supported: %s", yes_no(socket_ipv6_is_supported()));
1✔
469
        log_info("IPv6 enabled: %s", yes_no(socket_ipv6_is_enabled()));
1✔
470
}
1✔
471

472
TEST(sockaddr_un_set_path) {
1✔
473
        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
1✔
474
        _cleanup_(unlink_and_freep) char *sh = NULL;
×
475
        _cleanup_free_ char *j = NULL;
1✔
476
        union sockaddr_union sa;
1✔
477
        _cleanup_close_ int fd1, fd2, fd3;
3✔
478

479
        assert_se(mkdtemp_malloc("/tmp/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXX", &t) >= 0);
1✔
480
        assert_se(strlen(t) > SUN_PATH_LEN);
1✔
481

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

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

488
        free(j);
1✔
489
        assert_se(j = path_join(sh, "sock"));
1✔
490
        assert_se(sockaddr_un_set_path(&sa.un, j) >= 0);
1✔
491

492
        fd1 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1✔
493
        assert_se(fd1 >= 0);
1✔
494
        assert_se(bind(fd1, &sa.sa, sockaddr_len(&sa)) >= 0);
1✔
495
        assert_se(listen(fd1, 1) >= 0);
1✔
496

497
        sh = unlink_and_free(sh); /* remove temporary symlink */
1✔
498

499
        fd2 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1✔
500
        assert_se(fd2 >= 0);
1✔
501
        assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) < 0);
1✔
502
        assert_se(errno == ENOENT); /* we removed the symlink, must fail */
1✔
503

504
        free(j);
1✔
505
        assert_se(j = path_join(t, "sock"));
1✔
506

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

511
        assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) >= 0);
1✔
512
}
1✔
513

514
TEST(getpeerpidref) {
1✔
515
        _cleanup_close_pair_ int fd[2] = EBADF_PAIR;
2✔
516

517
        ASSERT_OK(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fd));
1✔
518

519
        _cleanup_(pidref_done) PidRef pidref0 = PIDREF_NULL, pidref1 = PIDREF_NULL, pidref_self = PIDREF_NULL, pidref_pid1 = PIDREF_NULL;
1✔
520
        ASSERT_OK(getpeerpidref(fd[0], &pidref0));
1✔
521
        ASSERT_OK(getpeerpidref(fd[1], &pidref1));
1✔
522

523
        ASSERT_OK(pidref_set_self(&pidref_self));
1✔
524
        ASSERT_OK(pidref_set_pid(&pidref_pid1, 1));
1✔
525

526
        ASSERT_TRUE(pidref_equal(&pidref0, &pidref1));
1✔
527
        ASSERT_TRUE(pidref_equal(&pidref0, &pidref_self));
1✔
528
        ASSERT_TRUE(pidref_equal(&pidref1, &pidref_self));
1✔
529

530
        ASSERT_TRUE(!pidref_equal(&pidref_self, &pidref_pid1));
1✔
531
        ASSERT_TRUE(!pidref_equal(&pidref1, &pidref_pid1));
1✔
532
        ASSERT_TRUE(!pidref_equal(&pidref0, &pidref_pid1));
1✔
533
}
1✔
534

535
TEST(tos_to_priority) {
1✔
536
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS7), TC_PRIO_CONTROL);
1✔
537
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS6), TC_PRIO_CONTROL);
1✔
538
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS5), TC_PRIO_INTERACTIVE);
1✔
539
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS4), TC_PRIO_INTERACTIVE);
1✔
540
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS3), TC_PRIO_INTERACTIVE_BULK);
1✔
541
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS2), TC_PRIO_INTERACTIVE_BULK);
1✔
542
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS1), TC_PRIO_BULK);
1✔
543
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS0), TC_PRIO_BESTEFFORT);
1✔
544

545
        /* check if lower bits are correctly filtered. */
546
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS7 | IPTOS_LOWDELAY), TC_PRIO_CONTROL);
1✔
547
        ASSERT_EQ(tos_to_priority(IPTOS_CLASS_CS1 | IPTOS_LOWCOST), TC_PRIO_BULK);
1✔
548

549
        ASSERT_EQ(tos_to_priority(0x00), TC_PRIO_BESTEFFORT);
1✔
550
        ASSERT_EQ(tos_to_priority(0xff), TC_PRIO_CONTROL);
1✔
551
}
1✔
552

553
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