1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd
5 Copyright 2014 Ronny Chevalier
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 static void test_fdset_new_fill(void) {
32 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
33 char name
[] = "/tmp/test-fdset_new_fill.XXXXXX";
35 fd
= mkostemp_safe(name
);
37 assert_se(fdset_new_fill(&fdset
) >= 0);
38 assert_se(fdset_contains(fdset
, fd
));
43 static void test_fdset_put_dup(void) {
44 _cleanup_close_
int fd
= -1;
46 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
47 char name
[] = "/tmp/test-fdset_put_dup.XXXXXX";
49 fd
= mkostemp_safe(name
);
54 copyfd
= fdset_put_dup(fdset
, fd
);
55 assert_se(copyfd
>= 0 && copyfd
!= fd
);
56 assert_se(fdset_contains(fdset
, copyfd
));
57 assert_se(!fdset_contains(fdset
, fd
));
62 static void test_fdset_cloexec(void) {
64 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
66 char name
[] = "/tmp/test-fdset_cloexec.XXXXXX";
68 fd
= mkostemp_safe(name
);
73 assert_se(fdset_put(fdset
, fd
));
75 assert_se(fdset_cloexec(fdset
, false) >= 0);
76 flags
= fcntl(fd
, F_GETFD
);
77 assert_se(flags
>= 0);
78 assert_se(!(flags
& FD_CLOEXEC
));
80 assert_se(fdset_cloexec(fdset
, true) >= 0);
81 flags
= fcntl(fd
, F_GETFD
);
82 assert_se(flags
>= 0);
83 assert_se(flags
& FD_CLOEXEC
);
88 static void test_fdset_close_others(void) {
91 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
93 char name
[] = "/tmp/test-fdset_close_others.XXXXXX";
95 fd
= mkostemp_safe(name
);
100 copyfd
= fdset_put_dup(fdset
, fd
);
101 assert_se(copyfd
>= 0);
103 assert_se(fdset_close_others(fdset
) >= 0);
104 flags
= fcntl(fd
, F_GETFD
);
105 assert_se(flags
< 0);
106 flags
= fcntl(copyfd
, F_GETFD
);
107 assert_se(flags
>= 0);
112 static void test_fdset_remove(void) {
113 _cleanup_close_
int fd
= -1;
115 char name
[] = "/tmp/test-fdset_remove.XXXXXX";
117 fd
= mkostemp_safe(name
);
122 assert_se(fdset_put(fdset
, fd
) >= 0);
123 assert_se(fdset_remove(fdset
, fd
) >= 0);
124 assert_se(!fdset_contains(fdset
, fd
));
127 assert_se(fcntl(fd
, F_GETFD
) >= 0);
132 static void test_fdset_iterate(void) {
135 char name
[] = "/tmp/test-fdset_iterate.XXXXXX";
140 fd
= mkostemp_safe(name
);
145 assert_se(fdset_put(fdset
, fd
) >= 0);
146 assert_se(fdset_put(fdset
, fd
) >= 0);
147 assert_se(fdset_put(fdset
, fd
) >= 0);
149 FDSET_FOREACH(a
, fdset
, i
) {
160 static void test_fdset_isempty(void) {
162 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
163 char name
[] = "/tmp/test-fdset_isempty.XXXXXX";
165 fd
= mkostemp_safe(name
);
171 assert_se(fdset_isempty(fdset
));
172 assert_se(fdset_put(fdset
, fd
) >= 0);
173 assert_se(!fdset_isempty(fdset
));
178 static void test_fdset_steal_first(void) {
180 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
181 char name
[] = "/tmp/test-fdset_steal_first.XXXXXX";
183 fd
= mkostemp_safe(name
);
189 assert_se(fdset_steal_first(fdset
) < 0);
190 assert_se(fdset_put(fdset
, fd
) >= 0);
191 assert_se(fdset_steal_first(fdset
) == fd
);
192 assert_se(fdset_steal_first(fdset
) < 0);
193 assert_se(fdset_put(fdset
, fd
) >= 0);
198 static void test_fdset_new_array(void) {
199 int fds
[] = {10, 11, 12, 13};
200 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
202 assert_se(fdset_new_array(&fdset
, fds
, 4) >= 0);
203 assert_se(fdset_size(fdset
) == 4);
204 assert_se(fdset_contains(fdset
, 10));
205 assert_se(fdset_contains(fdset
, 11));
206 assert_se(fdset_contains(fdset
, 12));
207 assert_se(fdset_contains(fdset
, 13));
210 int main(int argc
, char *argv
[]) {
211 test_fdset_new_fill();
212 test_fdset_put_dup();
213 test_fdset_cloexec();
214 test_fdset_close_others();
216 test_fdset_iterate();
217 test_fdset_isempty();
218 test_fdset_steal_first();
219 test_fdset_new_array();