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

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

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

96.83
/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 <net/if_arp.h>
6
#include <sys/stat.h>
7
#include <sys/types.h>
8
#include <unistd.h>
9

10
#include "alloc-util.h"
11
#include "async.h"
12
#include "escape.h"
13
#include "exit-status.h"
14
#include "fd-util.h"
15
#include "fs-util.h"
16
#include "in-addr-util.h"
17
#include "iovec-util.h"
18
#include "log.h"
19
#include "macro.h"
20
#include "path-util.h"
21
#include "process-util.h"
22
#include "random-util.h"
23
#include "rm-rf.h"
24
#include "socket-util.h"
25
#include "string-util.h"
26
#include "tests.h"
27
#include "tmpfile-util.h"
28
#include "user-util.h"
29

30
assert_cc(SUN_PATH_LEN == 108);
31

32
TEST(ifname_valid) {
1✔
33
        assert_se( ifname_valid("foo"));
1✔
34
        assert_se( ifname_valid("eth0"));
1✔
35

36
        assert_se(!ifname_valid("0"));
1✔
37
        assert_se(!ifname_valid("99"));
1✔
38
        assert_se( ifname_valid("a99"));
1✔
39
        assert_se( ifname_valid("99a"));
1✔
40

41
        assert_se(!ifname_valid(NULL));
1✔
42
        assert_se(!ifname_valid(""));
1✔
43
        assert_se(!ifname_valid(" "));
1✔
44
        assert_se(!ifname_valid(" foo"));
1✔
45
        assert_se(!ifname_valid("bar\n"));
1✔
46
        assert_se(!ifname_valid("."));
1✔
47
        assert_se(!ifname_valid(".."));
1✔
48
        assert_se(ifname_valid("foo.bar"));
1✔
49
        assert_se(!ifname_valid("x:y"));
1✔
50

51
        assert_se( ifname_valid_full("xxxxxxxxxxxxxxx", 0));
1✔
52
        assert_se(!ifname_valid_full("xxxxxxxxxxxxxxxx", 0));
1✔
53
        assert_se( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
1✔
54
        assert_se( ifname_valid_full("xxxxxxxxxxxxxxxx", IFNAME_VALID_ALTERNATIVE));
1✔
55
        assert_se(!ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE));
1✔
56
        assert_se( ifname_valid_full("999", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
1✔
57
        assert_se(!ifname_valid_full("0", IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC));
1✔
58
}
1✔
59

60
static void test_socket_print_unix_one(const char *in, size_t len_in, const char *expected) {
8✔
UNCOV
61
        _cleanup_free_ char *out = NULL, *c = NULL;
×
62

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

70
        assert_se(socket_address_print(&a, &out) >= 0);
8✔
71
        assert_se(c = cescape(in));
8✔
72
        log_info("\"%s\" → \"%s\" (expect \"%s\")", in, out, expected);
8✔
73
        ASSERT_STREQ(out, expected);
8✔
74
}
8✔
75

76
TEST(socket_print_unix) {
1✔
77
        /* Some additional tests for abstract addresses which we don't parse */
78

79
        test_socket_print_unix_one("\0\0\0\0", 4, "@\\000\\000\\000");
1✔
80
        test_socket_print_unix_one("@abs", 5, "@abs");
1✔
81
        test_socket_print_unix_one("\n", 2, "\\n");
1✔
82
        test_socket_print_unix_one("", 1, "<unnamed>");
1✔
83
        test_socket_print_unix_one("\0", 1, "<unnamed>");
1✔
84
        test_socket_print_unix_one("\0_________________________there's 108 characters in this string_____________________________________________", 108,
1✔
85
                                   "@_________________________there\\'s 108 characters in this string_____________________________________________");
86
        test_socket_print_unix_one("////////////////////////////////////////////////////////////////////////////////////////////////////////////", 108,
1✔
87
                                   "////////////////////////////////////////////////////////////////////////////////////////////////////////////");
88
        test_socket_print_unix_one("\0\a\b\n\255", 6, "@\\a\\b\\n\\255\\000");
1✔
89
}
1✔
90

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

118
        assert_se(sockaddr_equal(&a, &a));
1✔
119
        assert_se(sockaddr_equal(&a, &b));
1✔
120
        assert_se(sockaddr_equal(&d, &d));
1✔
121
        assert_se(sockaddr_equal(&e, &e));
1✔
122
        assert_se(!sockaddr_equal(&a, &c));
1✔
123
        assert_se(!sockaddr_equal(&b, &c));
1✔
124
        assert_se(!sockaddr_equal(&a, &e));
1✔
125
}
1✔
126

127
TEST(sockaddr_un_len) {
1✔
128
        static const struct sockaddr_un fs = {
1✔
129
                .sun_family = AF_UNIX,
130
                .sun_path = "/foo/bar/waldo",
131
        };
132

133
        static const struct sockaddr_un abstract = {
1✔
134
                .sun_family = AF_UNIX,
135
                .sun_path = "\0foobar",
136
        };
137

138
        assert_se(sockaddr_un_len(&fs) == offsetof(struct sockaddr_un, sun_path) + strlen(fs.sun_path) + 1);
1✔
139
        assert_se(sockaddr_un_len(&abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1));
1✔
140
}
1✔
141

142
TEST(in_addr_is_multicast) {
1✔
143
        union in_addr_union a, b;
1✔
144
        int f;
1✔
145

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

149
        assert_se(in_addr_from_string_auto("224.0.0.1", &f, &a) >= 0);
1✔
150
        assert_se(in_addr_is_multicast(f, &a) == 1);
1✔
151

152
        assert_se(in_addr_from_string_auto("FF01:0:0:0:0:0:0:1", &f, &b) >= 0);
1✔
153
        assert_se(in_addr_is_multicast(f, &b) == 1);
1✔
154

155
        assert_se(in_addr_from_string_auto("2001:db8::c:69b:aeff:fe53:743e", &f, &b) >= 0);
1✔
156
        assert_se(in_addr_is_multicast(f, &b) == 0);
1✔
157
}
1✔
158

159
TEST(getpeercred_getpeergroups) {
1✔
160
        int r;
1✔
161

162
        r = safe_fork("(getpeercred)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
1✔
163
        assert_se(r >= 0);
2✔
164

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

174
                if (geteuid() == 0 && !userns_has_single_user()) {
1✔
175
                        test_uid = 1;
1✔
176
                        test_gid = 2;
1✔
177
                        test_gids = (gid_t*) gids;
1✔
178
                        n_test_gids = ELEMENTSOF(gids);
1✔
179

180
                        assert_se(fully_set_uid_gid(test_uid, test_gid, test_gids, n_test_gids) >= 0);
1✔
181
                } else {
UNCOV
182
                        long ngroups_max;
×
183

184
                        test_uid = getuid();
×
UNCOV
185
                        test_gid = getgid();
×
186

187
                        ngroups_max = sysconf(_SC_NGROUPS_MAX);
×
UNCOV
188
                        assert_se(ngroups_max > 0);
×
189

UNCOV
190
                        test_gids = newa(gid_t, ngroups_max);
×
191

192
                        r = getgroups(ngroups_max, test_gids);
×
193
                        assert_se(r >= 0);
×
UNCOV
194
                        n_test_gids = (size_t) r;
×
195
                }
196

197
                assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0);
1✔
198

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

201
                assert_se(ucred.uid == test_uid);
1✔
202
                assert_se(ucred.gid == test_gid);
1✔
203
                assert_se(ucred.pid == getpid_cached());
1✔
204

205
                {
206
                        _cleanup_free_ gid_t *peer_groups = NULL;
2✔
207

208
                        r = getpeergroups(pair[0], &peer_groups);
1✔
209
                        assert_se(r >= 0 || IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT));
1✔
210

UNCOV
211
                        if (r >= 0) {
×
212
                                assert_se((size_t) r == n_test_gids);
1✔
213
                                assert_se(memcmp(peer_groups, test_gids, sizeof(gid_t) * n_test_gids) == 0);
1✔
214
                        }
215
                }
216

217
                safe_close_pair(pair);
1✔
218
                _exit(EXIT_SUCCESS);
1✔
219
        }
220
}
1✔
221

222
TEST(passfd_read) {
1✔
223
        static const char file_contents[] = "test contents for passfd";
1✔
224
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
225
        int r;
1✔
226

227
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
228

229
        r = safe_fork("(passfd_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
1✔
230
        assert_se(r >= 0);
2✔
231

232
        if (r == 0) {
2✔
233
                /* Child */
234
                pair[0] = safe_close(pair[0]);
1✔
235

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

239
                _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
1✔
240
                assert_se(tmpfd >= 0);
1✔
241
                assert_se(unlink(tmpfile) == 0);
1✔
242

243
                assert_se(send_one_fd(pair[1], tmpfd, MSG_DONTWAIT) == 0);
1✔
244
                _exit(EXIT_SUCCESS);
1✔
245
        }
246

247
        /* Parent */
248
        char buf[64];
1✔
249
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
250
        _cleanup_close_ int fd = -EBADF;
1✔
251

252
        pair[1] = safe_close(pair[1]);
1✔
253

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

256
        assert_se(fd >= 0);
1✔
257
        ssize_t n = read(fd, buf, sizeof(buf)-1);
1✔
258
        assert_se(n >= 0);
1✔
259
        buf[n] = 0;
1✔
260
        ASSERT_STREQ(buf, file_contents);
1✔
261
}
1✔
262

263
TEST(passfd_contents_read) {
1✔
264
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
265
        static const char file_contents[] = "test contents in the file";
1✔
266
        static const char wire_contents[] = "test contents on the wire";
1✔
267
        int r;
1✔
268

269
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
270

271
        r = safe_fork("(passfd_contents_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
1✔
272
        assert_se(r >= 0);
2✔
273

274
        if (r == 0) {
2✔
275
                /* Child */
276
                struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
1✔
277
                char tmpfile[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
1✔
278

279
                pair[0] = safe_close(pair[0]);
1✔
280

281
                assert_se(write_tmpfile(tmpfile, file_contents) == 0);
1✔
282

283
                _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
1✔
284
                assert_se(tmpfd >= 0);
1✔
285
                assert_se(unlink(tmpfile) == 0);
1✔
286

287
                assert_se(send_one_fd_iov(pair[1], tmpfd, &iov, 1, MSG_DONTWAIT) > 0);
1✔
288
                _exit(EXIT_SUCCESS);
1✔
289
        }
290

291
        /* Parent */
292
        char buf[64];
1✔
293
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
294
        _cleanup_close_ int fd = -EBADF;
1✔
295
        ssize_t k;
1✔
296

297
        pair[1] = safe_close(pair[1]);
1✔
298

299
        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
1✔
300
        assert_se(k > 0);
1✔
301
        buf[k] = 0;
1✔
302
        ASSERT_STREQ(buf, wire_contents);
1✔
303

304
        assert_se(fd >= 0);
1✔
305
        r = read(fd, buf, sizeof(buf)-1);
1✔
306
        assert_se(r >= 0);
1✔
307
        buf[r] = 0;
1✔
308
        ASSERT_STREQ(buf, file_contents);
1✔
309
}
1✔
310

311
TEST(pass_many_fds_contents_read) {
1✔
312
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
313
        static const char file_contents[][STRLEN("test contents in the fileX") + 1] = {
1✔
314
                "test contents in the file0",
315
                "test contents in the file1",
316
                "test contents in the file2"
317
        };
318
        static const char wire_contents[] = "test contents on the wire";
1✔
319
        int r;
1✔
320

321
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
322

323
        r = safe_fork("(passfd_contents_read)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
1✔
324
        assert_se(r >= 0);
2✔
325

326
        if (r == 0) {
2✔
327
                /* Child */
328
                struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
1✔
329
                char tmpfile[][STRLEN("/tmp/test-socket-util-passfd-contents-read-XXXXXX") + 1] = {
1✔
330
                        "/tmp/test-socket-util-passfd-contents-read-XXXXXX",
331
                        "/tmp/test-socket-util-passfd-contents-read-XXXXXX",
332
                        "/tmp/test-socket-util-passfd-contents-read-XXXXXX"
333
                };
334
                int tmpfds[3] = EBADF_TRIPLET;
1✔
335

336
                pair[0] = safe_close(pair[0]);
1✔
337

338
                for (size_t i = 0; i < 3; ++i) {
4✔
339
                        assert_se(write_tmpfile(tmpfile[i], file_contents[i]) == 0);
3✔
340
                        tmpfds[i] = open(tmpfile[i], O_RDONLY);
3✔
341
                        assert_se(tmpfds[i] >= 0);
3✔
342
                        assert_se(unlink(tmpfile[i]) == 0);
3✔
343
                }
344

345
                assert_se(send_many_fds_iov(pair[1], tmpfds, 3, &iov, 1, MSG_DONTWAIT) > 0);
1✔
346
                close_many(tmpfds, 3);
1✔
347
                _exit(EXIT_SUCCESS);
1✔
348
        }
349

350
        /* Parent */
351
        char buf[64];
1✔
352
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
353
        _cleanup_free_ int *fds = NULL;
1✔
354
        size_t n_fds = 0;
1✔
355
        ssize_t k;
1✔
356

357
        pair[1] = safe_close(pair[1]);
1✔
358

359
        k = receive_many_fds_iov(pair[0], &iov, 1, &fds, &n_fds, MSG_DONTWAIT);
1✔
360
        assert_se(k > 0);
1✔
361
        buf[k] = 0;
1✔
362
        ASSERT_STREQ(buf, wire_contents);
1✔
363

364
        assert_se(n_fds == 3);
1✔
365

366
        for (size_t i = 0; i < 3; ++i) {
4✔
367
                assert_se(fds[i] >= 0);
3✔
368
                r = read(fds[i], buf, sizeof(buf)-1);
3✔
369
                assert_se(r >= 0);
3✔
370
                buf[r] = 0;
3✔
371
                ASSERT_STREQ(buf, file_contents[i]);
3✔
372
                safe_close(fds[i]);
3✔
373
        }
374
}
1✔
375

376
TEST(receive_nopassfd) {
1✔
377
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
378
        static const char wire_contents[] = "no fd passed here";
1✔
379
        int r;
1✔
380

381
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
382

383
        r = safe_fork("(receive_nopassfd)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
1✔
384
        assert_se(r >= 0);
2✔
385

386
        if (r == 0) {
2✔
387
                /* Child */
388
                struct iovec iov = IOVEC_MAKE_STRING(wire_contents);
1✔
389

390
                pair[0] = safe_close(pair[0]);
1✔
391

392
                assert_se(send_one_fd_iov(pair[1], -1, &iov, 1, MSG_DONTWAIT) > 0);
1✔
393
                _exit(EXIT_SUCCESS);
1✔
394
        }
395

396
        /* Parent */
397
        char buf[64];
1✔
398
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
399
        int fd = -999;
1✔
400
        ssize_t k;
1✔
401

402
        pair[1] = safe_close(pair[1]);
1✔
403

404
        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
1✔
405
        assert_se(k > 0);
1✔
406
        buf[k] = 0;
1✔
407
        ASSERT_STREQ(buf, wire_contents);
1✔
408

409
        /* no fd passed here, confirm it was reset */
410
        assert_se(fd == -EBADF);
1✔
411
}
1✔
412

413
TEST(send_nodata_nofd) {
1✔
414
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
415
        int r;
1✔
416

417
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
418

419
        r = safe_fork("(send_nodata_nofd)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
1✔
420
        assert_se(r >= 0);
2✔
421

422
        if (r == 0) {
2✔
423
                /* Child */
424
                pair[0] = safe_close(pair[0]);
1✔
425

426
                assert_se(send_one_fd_iov(pair[1], -1, NULL, 0, MSG_DONTWAIT) == -EINVAL);
1✔
427
                _exit(EXIT_SUCCESS);
1✔
428
        }
429

430
        /* Parent */
431
        char buf[64];
1✔
432
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
433
        int fd = -999;
1✔
434
        ssize_t k;
1✔
435

436
        pair[1] = safe_close(pair[1]);
1✔
437

438
        k = receive_one_fd_iov(pair[0], &iov, 1, MSG_DONTWAIT, &fd);
1✔
439
        /* recvmsg() will return errno EAGAIN if nothing was sent */
440
        assert_se(k == -EAGAIN);
1✔
441

442
        /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
443
        assert_se(fd == -999);
1✔
444
}
1✔
445

446
TEST(send_emptydata) {
1✔
447
        _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
2✔
448
        int r;
1✔
449

450
        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0);
1✔
451

452
        r = safe_fork("(send_emptydata)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);
1✔
453
        assert_se(r >= 0);
2✔
454

455
        if (r == 0) {
2✔
456
                /* Child */
457
                pair[0] = safe_close(pair[0]);
1✔
458

459
                /* This will succeed, since iov is set. */
460
                assert_se(send_one_fd_iov(pair[1], -1, &iovec_empty, 1, MSG_DONTWAIT) == 0);
1✔
461
                _exit(EXIT_SUCCESS);
1✔
462
        }
463

464
        /* Parent */
465
        char buf[64];
1✔
466
        struct iovec iov = IOVEC_MAKE(buf, sizeof(buf)-1);
1✔
467
        int fd = -999;
1✔
468
        ssize_t k;
1✔
469

470
        pair[1] = safe_close(pair[1]);
1✔
471

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

476
        /* receive_one_fd_iov returned error, so confirm &fd wasn't touched */
477
        assert_se(fd == -999);
1✔
478
}
1✔
479

480
TEST(flush_accept) {
1✔
481
        _cleanup_close_ int listen_stream, listen_dgram, listen_seqpacket, connect_stream, connect_dgram, connect_seqpacket;
5✔
482
        static const union sockaddr_union sa = { .un.sun_family = AF_UNIX };
1✔
483
        union sockaddr_union lsa;
1✔
484
        socklen_t l;
1✔
485

486
        listen_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
487
        assert_se(listen_stream >= 0);
1✔
488

489
        listen_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
490
        assert_se(listen_dgram >= 0);
1✔
491

492
        listen_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
493
        assert_se(listen_seqpacket >= 0);
1✔
494

495
        assert_se(flush_accept(listen_stream) < 0);
1✔
496
        assert_se(flush_accept(listen_dgram) < 0);
1✔
497
        assert_se(flush_accept(listen_seqpacket) < 0);
1✔
498

499
        assert_se(bind(listen_stream, &sa.sa, sizeof(sa_family_t)) >= 0);
1✔
500
        assert_se(bind(listen_dgram, &sa.sa, sizeof(sa_family_t)) >= 0);
1✔
501
        assert_se(bind(listen_seqpacket, &sa.sa, sizeof(sa_family_t)) >= 0);
1✔
502

503
        assert_se(flush_accept(listen_stream) < 0);
1✔
504
        assert_se(flush_accept(listen_dgram) < 0);
1✔
505
        assert_se(flush_accept(listen_seqpacket) < 0);
1✔
506

507
        assert_se(listen(listen_stream, SOMAXCONN_DELUXE) >= 0);
1✔
508
        assert_se(listen(listen_dgram, SOMAXCONN_DELUXE) < 0);
1✔
509
        assert_se(listen(listen_seqpacket, SOMAXCONN_DELUXE) >= 0);
1✔
510

511
        assert_se(flush_accept(listen_stream) >= 0);
1✔
512
        assert_se(flush_accept(listen_dgram) < 0);
1✔
513
        assert_se(flush_accept(listen_seqpacket) >= 0);
1✔
514

515
        connect_stream = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
516
        assert_se(connect_stream >= 0);
1✔
517

518
        connect_dgram = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
519
        assert_se(connect_dgram >= 0);
1✔
520

521
        connect_seqpacket = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1✔
522
        assert_se(connect_seqpacket >= 0);
1✔
523

524
        l = sizeof(lsa);
1✔
525
        assert_se(getsockname(listen_stream, &lsa.sa, &l) >= 0);
1✔
526
        assert_se(connect(connect_stream, &lsa.sa, l) >= 0);
1✔
527

528
        l = sizeof(lsa);
1✔
529
        assert_se(getsockname(listen_dgram, &lsa.sa, &l) >= 0);
1✔
530
        assert_se(connect(connect_dgram, &lsa.sa, l) >= 0);
1✔
531

532
        l = sizeof(lsa);
1✔
533
        assert_se(getsockname(listen_seqpacket, &lsa.sa, &l) >= 0);
1✔
534
        assert_se(connect(connect_seqpacket, &lsa.sa, l) >= 0);
1✔
535

536
        assert_se(flush_accept(listen_stream) >= 0);
1✔
537
        assert_se(flush_accept(listen_dgram) < 0);
1✔
538
        assert_se(flush_accept(listen_seqpacket) >= 0);
1✔
539
}
1✔
540

541
TEST(ipv6_enabled) {
1✔
542
        log_info("IPv6 supported: %s", yes_no(socket_ipv6_is_supported()));
2✔
543
        log_info("IPv6 enabled: %s", yes_no(socket_ipv6_is_enabled()));
2✔
544
}
1✔
545

546
TEST(sockaddr_un_set_path) {
1✔
547
        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
1✔
UNCOV
548
        _cleanup_(unlink_and_freep) char *sh = NULL;
×
549
        _cleanup_free_ char *j = NULL;
1✔
550
        union sockaddr_union sa;
1✔
551
        _cleanup_close_ int fd1, fd2, fd3;
3✔
552

553
        assert_se(mkdtemp_malloc("/tmp/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXX", &t) >= 0);
1✔
554
        assert_se(strlen(t) > SUN_PATH_LEN);
1✔
555

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

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

562
        free(j);
1✔
563
        assert_se(j = path_join(sh, "sock"));
1✔
564
        assert_se(sockaddr_un_set_path(&sa.un, j) >= 0);
1✔
565

566
        fd1 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1✔
567
        assert_se(fd1 >= 0);
1✔
568
        assert_se(bind(fd1, &sa.sa, sockaddr_len(&sa)) >= 0);
1✔
569
        assert_se(listen(fd1, 1) >= 0);
1✔
570

571
        sh = unlink_and_free(sh); /* remove temporary symlink */
1✔
572

573
        fd2 = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1✔
574
        assert_se(fd2 >= 0);
1✔
575
        assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) < 0);
1✔
576
        assert_se(errno == ENOENT); /* we removed the symlink, must fail */
1✔
577

578
        free(j);
1✔
579
        assert_se(j = path_join(t, "sock"));
1✔
580

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

585
        assert_se(connect(fd2, &sa.sa, sockaddr_len(&sa)) >= 0);
1✔
586
}
1✔
587

588
TEST(getpeerpidref) {
1✔
589
        _cleanup_close_pair_ int fd[2] = EBADF_PAIR;
2✔
590

591
        ASSERT_OK(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fd));
1✔
592

593
        _cleanup_(pidref_done) PidRef pidref0 = PIDREF_NULL, pidref1 = PIDREF_NULL, pidref_self = PIDREF_NULL, pidref_pid1 = PIDREF_NULL;
1✔
594
        ASSERT_OK(getpeerpidref(fd[0], &pidref0));
1✔
595
        ASSERT_OK(getpeerpidref(fd[1], &pidref1));
1✔
596

597
        ASSERT_OK(pidref_set_self(&pidref_self));
1✔
598
        ASSERT_OK(pidref_set_pid(&pidref_pid1, 1));
1✔
599

600
        ASSERT_TRUE(pidref_equal(&pidref0, &pidref1));
1✔
601
        ASSERT_TRUE(pidref_equal(&pidref0, &pidref_self));
1✔
602
        ASSERT_TRUE(pidref_equal(&pidref1, &pidref_self));
1✔
603

604
        ASSERT_TRUE(!pidref_equal(&pidref_self, &pidref_pid1));
1✔
605
        ASSERT_TRUE(!pidref_equal(&pidref1, &pidref_pid1));
1✔
606
        ASSERT_TRUE(!pidref_equal(&pidref0, &pidref_pid1));
1✔
607
}
1✔
608

609
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