2 This file is part of systemd
4 Copyright 2014 Ronny Chevalier
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 static void test_fdset_new_fill(void) {
30 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
31 char name
[] = "/tmp/test-fdset_new_fill.XXXXXX";
33 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
35 assert_se(fdset_new_fill(&fdset
) >= 0);
36 assert_se(fdset_contains(fdset
, fd
));
41 static void test_fdset_put_dup(void) {
42 _cleanup_close_
int fd
= -1;
44 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
45 char name
[] = "/tmp/test-fdset_put_dup.XXXXXX";
47 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
52 copyfd
= fdset_put_dup(fdset
, fd
);
53 assert_se(copyfd
>= 0 && copyfd
!= fd
);
54 assert_se(fdset_contains(fdset
, copyfd
));
55 assert_se(!fdset_contains(fdset
, fd
));
60 static void test_fdset_cloexec(void) {
62 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
64 char name
[] = "/tmp/test-fdset_cloexec.XXXXXX";
66 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
71 assert_se(fdset_put(fdset
, fd
));
73 assert_se(fdset_cloexec(fdset
, false) >= 0);
74 flags
= fcntl(fd
, F_GETFD
);
75 assert_se(flags
>= 0);
76 assert_se(!(flags
& FD_CLOEXEC
));
78 assert_se(fdset_cloexec(fdset
, true) >= 0);
79 flags
= fcntl(fd
, F_GETFD
);
80 assert_se(flags
>= 0);
81 assert_se(flags
& FD_CLOEXEC
);
86 static void test_fdset_close_others(void) {
89 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
91 char name
[] = "/tmp/test-fdset_close_others.XXXXXX";
93 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
98 copyfd
= fdset_put_dup(fdset
, fd
);
99 assert_se(copyfd
>= 0);
101 assert_se(fdset_close_others(fdset
) >= 0);
102 flags
= fcntl(fd
, F_GETFD
);
103 assert_se(flags
< 0);
104 flags
= fcntl(copyfd
, F_GETFD
);
105 assert_se(flags
>= 0);
110 static void test_fdset_remove(void) {
111 _cleanup_close_
int fd
= -1;
113 char name
[] = "/tmp/test-fdset_remove.XXXXXX";
115 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
120 assert_se(fdset_put(fdset
, fd
) >= 0);
121 assert_se(fdset_remove(fdset
, fd
) >= 0);
122 assert_se(!fdset_contains(fdset
, fd
));
125 assert_se(fcntl(fd
, F_GETFD
) >= 0);
130 static void test_fdset_iterate(void) {
133 char name
[] = "/tmp/test-fdset_iterate.XXXXXX";
138 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
143 assert_se(fdset_put(fdset
, fd
) >= 0);
144 assert_se(fdset_put(fdset
, fd
) >= 0);
145 assert_se(fdset_put(fdset
, fd
) >= 0);
147 FDSET_FOREACH(a
, fdset
, i
) {
158 static void test_fdset_isempty(void) {
160 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
161 char name
[] = "/tmp/test-fdset_isempty.XXXXXX";
163 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
169 assert_se(fdset_isempty(fdset
));
170 assert_se(fdset_put(fdset
, fd
) >= 0);
171 assert_se(!fdset_isempty(fdset
));
176 static void test_fdset_steal_first(void) {
178 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
179 char name
[] = "/tmp/test-fdset_steal_first.XXXXXX";
181 fd
= mkostemp_safe(name
, O_RDWR
|O_CLOEXEC
);
187 assert_se(fdset_steal_first(fdset
) < 0);
188 assert_se(fdset_put(fdset
, fd
) >= 0);
189 assert_se(fdset_steal_first(fdset
) == fd
);
190 assert_se(fdset_steal_first(fdset
) < 0);
191 assert_se(fdset_put(fdset
, fd
) >= 0);
196 static void test_fdset_new_array(void) {
197 int fds
[] = {10, 11, 12, 13};
198 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
200 assert_se(fdset_new_array(&fdset
, fds
, 4) >= 0);
201 assert_se(fdset_size(fdset
) == 4);
202 assert_se(fdset_contains(fdset
, 10));
203 assert_se(fdset_contains(fdset
, 11));
204 assert_se(fdset_contains(fdset
, 12));
205 assert_se(fdset_contains(fdset
, 13));
208 int main(int argc
, char *argv
[]) {
209 test_fdset_new_fill();
210 test_fdset_put_dup();
211 test_fdset_cloexec();
212 test_fdset_close_others();
214 test_fdset_iterate();
215 test_fdset_isempty();
216 test_fdset_steal_first();
217 test_fdset_new_array();