]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fdset.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / test / test-fdset.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "fd-util.h"
7 #include "fdset.h"
8 #include "macro.h"
9 #include "tests.h"
10 #include "tmpfile-util.h"
11
12 TEST(fdset_new_fill) {
13 int fd = -EBADF;
14 _cleanup_fdset_free_ FDSet *fdset = NULL;
15 char name[] = "/tmp/test-fdset_new_fill.XXXXXX";
16
17 fd = mkostemp_safe(name);
18 assert_se(fd >= 0);
19 assert_se(fdset_new_fill(&fdset) >= 0);
20 assert_se(fdset_contains(fdset, fd));
21
22 unlink(name);
23 }
24
25 TEST(fdset_put_dup) {
26 _cleanup_close_ int fd = -EBADF;
27 int copyfd = -EBADF;
28 _cleanup_fdset_free_ FDSet *fdset = NULL;
29 char name[] = "/tmp/test-fdset_put_dup.XXXXXX";
30
31 fd = mkostemp_safe(name);
32 assert_se(fd >= 0);
33
34 fdset = fdset_new();
35 assert_se(fdset);
36 copyfd = fdset_put_dup(fdset, fd);
37 assert_se(copyfd >= 0 && copyfd != fd);
38 assert_se(fdset_contains(fdset, copyfd));
39 assert_se(!fdset_contains(fdset, fd));
40
41 unlink(name);
42 }
43
44 TEST(fdset_cloexec) {
45 int fd = -EBADF;
46 _cleanup_fdset_free_ FDSet *fdset = NULL;
47 int flags = -1;
48 char name[] = "/tmp/test-fdset_cloexec.XXXXXX";
49
50 fd = mkostemp_safe(name);
51 assert_se(fd >= 0);
52
53 fdset = fdset_new();
54 assert_se(fdset);
55 assert_se(fdset_put(fdset, fd));
56
57 assert_se(fdset_cloexec(fdset, false) >= 0);
58 flags = fcntl(fd, F_GETFD);
59 assert_se(flags >= 0);
60 assert_se(!(flags & FD_CLOEXEC));
61
62 assert_se(fdset_cloexec(fdset, true) >= 0);
63 flags = fcntl(fd, F_GETFD);
64 assert_se(flags >= 0);
65 assert_se(flags & FD_CLOEXEC);
66
67 unlink(name);
68 }
69
70 TEST(fdset_close_others) {
71 int fd = -EBADF;
72 int copyfd = -EBADF;
73 _cleanup_fdset_free_ FDSet *fdset = NULL;
74 int flags = -1;
75 char name[] = "/tmp/test-fdset_close_others.XXXXXX";
76
77 fd = mkostemp_safe(name);
78 assert_se(fd >= 0);
79
80 fdset = fdset_new();
81 assert_se(fdset);
82 copyfd = fdset_put_dup(fdset, fd);
83 assert_se(copyfd >= 0);
84
85 assert_se(fdset_close_others(fdset) >= 0);
86 flags = fcntl(fd, F_GETFD);
87 assert_se(flags < 0);
88 flags = fcntl(copyfd, F_GETFD);
89 assert_se(flags >= 0);
90
91 unlink(name);
92 }
93
94 TEST(fdset_remove) {
95 _cleanup_close_ int fd = -EBADF;
96 FDSet *fdset = NULL;
97 char name[] = "/tmp/test-fdset_remove.XXXXXX";
98
99 fd = mkostemp_safe(name);
100 assert_se(fd >= 0);
101
102 fdset = fdset_new();
103 assert_se(fdset);
104 assert_se(fdset_put(fdset, fd) >= 0);
105 assert_se(fdset_remove(fdset, fd) >= 0);
106 assert_se(!fdset_contains(fdset, fd));
107 fdset_free(fdset);
108
109 assert_se(fcntl(fd, F_GETFD) >= 0);
110
111 unlink(name);
112 }
113
114 TEST(fdset_iterate) {
115 int fd = -EBADF;
116 FDSet *fdset = NULL;
117 char name[] = "/tmp/test-fdset_iterate.XXXXXX";
118 int c = 0;
119 int a;
120
121 fd = mkostemp_safe(name);
122 assert_se(fd >= 0);
123
124 fdset = fdset_new();
125 assert_se(fdset);
126 assert_se(fdset_put(fdset, fd) >= 0);
127 assert_se(fdset_put(fdset, fd) >= 0);
128 assert_se(fdset_put(fdset, fd) >= 0);
129
130 FDSET_FOREACH(a, fdset) {
131 c++;
132 assert_se(a == fd);
133 }
134 assert_se(c == 1);
135
136 fdset_free(fdset);
137
138 unlink(name);
139 }
140
141 TEST(fdset_isempty) {
142 int fd;
143 _cleanup_fdset_free_ FDSet *fdset = NULL;
144 char name[] = "/tmp/test-fdset_isempty.XXXXXX";
145
146 fd = mkostemp_safe(name);
147 assert_se(fd >= 0);
148
149 fdset = fdset_new();
150 assert_se(fdset);
151
152 assert_se(fdset_isempty(fdset));
153 assert_se(fdset_put(fdset, fd) >= 0);
154 assert_se(!fdset_isempty(fdset));
155
156 unlink(name);
157 }
158
159 TEST(fdset_steal_first) {
160 int fd;
161 _cleanup_fdset_free_ FDSet *fdset = NULL;
162 char name[] = "/tmp/test-fdset_steal_first.XXXXXX";
163
164 fd = mkostemp_safe(name);
165 assert_se(fd >= 0);
166
167 fdset = fdset_new();
168 assert_se(fdset);
169
170 assert_se(fdset_steal_first(fdset) < 0);
171 assert_se(fdset_put(fdset, fd) >= 0);
172 assert_se(fdset_steal_first(fdset) == fd);
173 assert_se(fdset_steal_first(fdset) < 0);
174 assert_se(fdset_put(fdset, fd) >= 0);
175
176 unlink(name);
177 }
178
179 TEST(fdset_new_array) {
180 int fds[] = {10, 11, 12, 13};
181 _cleanup_fdset_free_ FDSet *fdset = NULL;
182
183 assert_se(fdset_new_array(&fdset, fds, 4) >= 0);
184 assert_se(fdset_size(fdset) == 4);
185 assert_se(fdset_contains(fdset, 10));
186 assert_se(fdset_contains(fdset, 11));
187 assert_se(fdset_contains(fdset, 12));
188 assert_se(fdset_contains(fdset, 13));
189 }
190
191 DEFINE_TEST_MAIN(LOG_INFO);