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