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/>.
29 static void test_fdset_new_fill(void) {
31 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
32 char name
[] = "/tmp/test-fdset_new_fill.XXXXXX";
34 fd
= mkostemp_safe(name
);
36 assert_se(fdset_new_fill(&fdset
) >= 0);
37 assert_se(fdset_contains(fdset
, fd
));
42 static void test_fdset_put_dup(void) {
43 _cleanup_close_
int fd
= -1;
45 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
46 char name
[] = "/tmp/test-fdset_put_dup.XXXXXX";
48 fd
= mkostemp_safe(name
);
53 copyfd
= fdset_put_dup(fdset
, fd
);
54 assert_se(copyfd
>= 0 && copyfd
!= fd
);
55 assert_se(fdset_contains(fdset
, copyfd
));
56 assert_se(!fdset_contains(fdset
, fd
));
61 static void test_fdset_cloexec(void) {
63 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
65 char name
[] = "/tmp/test-fdset_cloexec.XXXXXX";
67 fd
= mkostemp_safe(name
);
72 assert_se(fdset_put(fdset
, fd
));
74 assert_se(fdset_cloexec(fdset
, false) >= 0);
75 flags
= fcntl(fd
, F_GETFD
);
76 assert_se(flags
>= 0);
77 assert_se(!(flags
& FD_CLOEXEC
));
79 assert_se(fdset_cloexec(fdset
, true) >= 0);
80 flags
= fcntl(fd
, F_GETFD
);
81 assert_se(flags
>= 0);
82 assert_se(flags
& FD_CLOEXEC
);
87 static void test_fdset_close_others(void) {
90 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
92 char name
[] = "/tmp/test-fdset_close_others.XXXXXX";
94 fd
= mkostemp_safe(name
);
99 copyfd
= fdset_put_dup(fdset
, fd
);
100 assert_se(copyfd
>= 0);
102 assert_se(fdset_close_others(fdset
) >= 0);
103 flags
= fcntl(fd
, F_GETFD
);
104 assert_se(flags
< 0);
105 flags
= fcntl(copyfd
, F_GETFD
);
106 assert_se(flags
>= 0);
111 static void test_fdset_remove(void) {
112 _cleanup_close_
int fd
= -1;
114 char name
[] = "/tmp/test-fdset_remove.XXXXXX";
116 fd
= mkostemp_safe(name
);
121 assert_se(fdset_put(fdset
, fd
) >= 0);
122 assert_se(fdset_remove(fdset
, fd
) >= 0);
123 assert_se(!fdset_contains(fdset
, fd
));
126 assert_se(fcntl(fd
, F_GETFD
) >= 0);
131 static void test_fdset_iterate(void) {
134 char name
[] = "/tmp/test-fdset_iterate.XXXXXX";
139 fd
= mkostemp_safe(name
);
144 assert_se(fdset_put(fdset
, fd
) >= 0);
145 assert_se(fdset_put(fdset
, fd
) >= 0);
146 assert_se(fdset_put(fdset
, fd
) >= 0);
148 FDSET_FOREACH(a
, fdset
, i
) {
159 static void test_fdset_isempty(void) {
161 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
162 char name
[] = "/tmp/test-fdset_isempty.XXXXXX";
164 fd
= mkostemp_safe(name
);
170 assert_se(fdset_isempty(fdset
));
171 assert_se(fdset_put(fdset
, fd
) >= 0);
172 assert_se(!fdset_isempty(fdset
));
177 static void test_fdset_steal_first(void) {
179 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
180 char name
[] = "/tmp/test-fdset_steal_first.XXXXXX";
182 fd
= mkostemp_safe(name
);
188 assert_se(fdset_steal_first(fdset
) < 0);
189 assert_se(fdset_put(fdset
, fd
) >= 0);
190 assert_se(fdset_steal_first(fdset
) == fd
);
191 assert_se(fdset_steal_first(fdset
) < 0);
192 assert_se(fdset_put(fdset
, fd
) >= 0);
197 static void test_fdset_new_array(void) {
198 int fds
[] = {10, 11, 12, 13};
199 _cleanup_fdset_free_ FDSet
*fdset
= NULL
;
201 assert_se(fdset_new_array(&fdset
, fds
, 4) >= 0);
202 assert_se(fdset_size(fdset
) == 4);
203 assert_se(fdset_contains(fdset
, 10));
204 assert_se(fdset_contains(fdset
, 11));
205 assert_se(fdset_contains(fdset
, 12));
206 assert_se(fdset_contains(fdset
, 13));
209 int main(int argc
, char *argv
[]) {
210 test_fdset_new_fill();
211 test_fdset_put_dup();
212 test_fdset_cloexec();
213 test_fdset_close_others();
215 test_fdset_iterate();
216 test_fdset_isempty();
217 test_fdset_steal_first();
218 test_fdset_new_array();