1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "tmpfile-util.h"
12 static void test_fdset_new_fill(void) {
14 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
15 char name
[] = "/tmp/test-fdset_new_fill.XXXXXX";
17 fd
= mkostemp_safe(name
);
19 assert_se(fdset_new_fill(&fdset
) >= 0);
20 assert_se(fdset_contains(fdset
, fd
));
25 static void test_fdset_put_dup(void) {
26 _cleanup_close_
int fd
= -1;
28 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
29 char name
[] = "/tmp/test-fdset_put_dup.XXXXXX";
31 fd
= mkostemp_safe(name
);
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
));
44 static void test_fdset_cloexec(void) {
46 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
48 char name
[] = "/tmp/test-fdset_cloexec.XXXXXX";
50 fd
= mkostemp_safe(name
);
55 assert_se(fdset_put(fdset
, fd
));
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
));
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
);
70 static void test_fdset_close_others(void) {
73 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
75 char name
[] = "/tmp/test-fdset_close_others.XXXXXX";
77 fd
= mkostemp_safe(name
);
82 copyfd
= fdset_put_dup(fdset
, fd
);
83 assert_se(copyfd
>= 0);
85 assert_se(fdset_close_others(fdset
) >= 0);
86 flags
= fcntl(fd
, F_GETFD
);
88 flags
= fcntl(copyfd
, F_GETFD
);
89 assert_se(flags
>= 0);
94 static void test_fdset_remove(void) {
95 _cleanup_close_
int fd
= -1;
97 char name
[] = "/tmp/test-fdset_remove.XXXXXX";
99 fd
= mkostemp_safe(name
);
104 assert_se(fdset_put(fdset
, fd
) >= 0);
105 assert_se(fdset_remove(fdset
, fd
) >= 0);
106 assert_se(!fdset_contains(fdset
, fd
));
109 assert_se(fcntl(fd
, F_GETFD
) >= 0);
114 static void test_fdset_iterate(void) {
117 char name
[] = "/tmp/test-fdset_iterate.XXXXXX";
122 fd
= mkostemp_safe(name
);
127 assert_se(fdset_put(fdset
, fd
) >= 0);
128 assert_se(fdset_put(fdset
, fd
) >= 0);
129 assert_se(fdset_put(fdset
, fd
) >= 0);
131 FDSET_FOREACH(a
, fdset
, i
) {
142 static void test_fdset_isempty(void) {
144 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
145 char name
[] = "/tmp/test-fdset_isempty.XXXXXX";
147 fd
= mkostemp_safe(name
);
153 assert_se(fdset_isempty(fdset
));
154 assert_se(fdset_put(fdset
, fd
) >= 0);
155 assert_se(!fdset_isempty(fdset
));
160 static void test_fdset_steal_first(void) {
162 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
163 char name
[] = "/tmp/test-fdset_steal_first.XXXXXX";
165 fd
= mkostemp_safe(name
);
171 assert_se(fdset_steal_first(fdset
) < 0);
172 assert_se(fdset_put(fdset
, fd
) >= 0);
173 assert_se(fdset_steal_first(fdset
) == fd
);
174 assert_se(fdset_steal_first(fdset
) < 0);
175 assert_se(fdset_put(fdset
, fd
) >= 0);
180 static void test_fdset_new_array(void) {
181 int fds
[] = {10, 11, 12, 13};
182 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
184 assert_se(fdset_new_array(&fdset
, fds
, 4) >= 0);
185 assert_se(fdset_size(fdset
) == 4);
186 assert_se(fdset_contains(fdset
, 10));
187 assert_se(fdset_contains(fdset
, 11));
188 assert_se(fdset_contains(fdset
, 12));
189 assert_se(fdset_contains(fdset
, 13));
192 int main(int argc
, char *argv
[]) {
193 test_fdset_new_fill();
194 test_fdset_put_dup();
195 test_fdset_cloexec();
196 test_fdset_close_others();
198 test_fdset_iterate();
199 test_fdset_isempty();
200 test_fdset_steal_first();
201 test_fdset_new_array();