1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Ronny Chevalier
15 static void test_fdset_new_fill(void) {
17 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
18 char name
[] = "/tmp/test-fdset_new_fill.XXXXXX";
20 fd
= mkostemp_safe(name
);
22 assert_se(fdset_new_fill(&fdset
) >= 0);
23 assert_se(fdset_contains(fdset
, fd
));
28 static void test_fdset_put_dup(void) {
29 _cleanup_close_
int fd
= -1;
31 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
32 char name
[] = "/tmp/test-fdset_put_dup.XXXXXX";
34 fd
= mkostemp_safe(name
);
39 copyfd
= fdset_put_dup(fdset
, fd
);
40 assert_se(copyfd
>= 0 && copyfd
!= fd
);
41 assert_se(fdset_contains(fdset
, copyfd
));
42 assert_se(!fdset_contains(fdset
, fd
));
47 static void test_fdset_cloexec(void) {
49 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
51 char name
[] = "/tmp/test-fdset_cloexec.XXXXXX";
53 fd
= mkostemp_safe(name
);
58 assert_se(fdset_put(fdset
, fd
));
60 assert_se(fdset_cloexec(fdset
, false) >= 0);
61 flags
= fcntl(fd
, F_GETFD
);
62 assert_se(flags
>= 0);
63 assert_se(!(flags
& FD_CLOEXEC
));
65 assert_se(fdset_cloexec(fdset
, true) >= 0);
66 flags
= fcntl(fd
, F_GETFD
);
67 assert_se(flags
>= 0);
68 assert_se(flags
& FD_CLOEXEC
);
73 static void test_fdset_close_others(void) {
76 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
78 char name
[] = "/tmp/test-fdset_close_others.XXXXXX";
80 fd
= mkostemp_safe(name
);
85 copyfd
= fdset_put_dup(fdset
, fd
);
86 assert_se(copyfd
>= 0);
88 assert_se(fdset_close_others(fdset
) >= 0);
89 flags
= fcntl(fd
, F_GETFD
);
91 flags
= fcntl(copyfd
, F_GETFD
);
92 assert_se(flags
>= 0);
97 static void test_fdset_remove(void) {
98 _cleanup_close_
int fd
= -1;
100 char name
[] = "/tmp/test-fdset_remove.XXXXXX";
102 fd
= mkostemp_safe(name
);
107 assert_se(fdset_put(fdset
, fd
) >= 0);
108 assert_se(fdset_remove(fdset
, fd
) >= 0);
109 assert_se(!fdset_contains(fdset
, fd
));
112 assert_se(fcntl(fd
, F_GETFD
) >= 0);
117 static void test_fdset_iterate(void) {
120 char name
[] = "/tmp/test-fdset_iterate.XXXXXX";
125 fd
= mkostemp_safe(name
);
130 assert_se(fdset_put(fdset
, fd
) >= 0);
131 assert_se(fdset_put(fdset
, fd
) >= 0);
132 assert_se(fdset_put(fdset
, fd
) >= 0);
134 FDSET_FOREACH(a
, fdset
, i
) {
145 static void test_fdset_isempty(void) {
147 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
148 char name
[] = "/tmp/test-fdset_isempty.XXXXXX";
150 fd
= mkostemp_safe(name
);
156 assert_se(fdset_isempty(fdset
));
157 assert_se(fdset_put(fdset
, fd
) >= 0);
158 assert_se(!fdset_isempty(fdset
));
163 static void test_fdset_steal_first(void) {
165 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
166 char name
[] = "/tmp/test-fdset_steal_first.XXXXXX";
168 fd
= mkostemp_safe(name
);
174 assert_se(fdset_steal_first(fdset
) < 0);
175 assert_se(fdset_put(fdset
, fd
) >= 0);
176 assert_se(fdset_steal_first(fdset
) == fd
);
177 assert_se(fdset_steal_first(fdset
) < 0);
178 assert_se(fdset_put(fdset
, fd
) >= 0);
183 static void test_fdset_new_array(void) {
184 int fds
[] = {10, 11, 12, 13};
185 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
187 assert_se(fdset_new_array(&fdset
, fds
, 4) >= 0);
188 assert_se(fdset_size(fdset
) == 4);
189 assert_se(fdset_contains(fdset
, 10));
190 assert_se(fdset_contains(fdset
, 11));
191 assert_se(fdset_contains(fdset
, 12));
192 assert_se(fdset_contains(fdset
, 13));
195 int main(int argc
, char *argv
[]) {
196 test_fdset_new_fill();
197 test_fdset_put_dup();
198 test_fdset_cloexec();
199 test_fdset_close_others();
201 test_fdset_iterate();
202 test_fdset_isempty();
203 test_fdset_steal_first();
204 test_fdset_new_array();