]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fdset.c
96d5e3817700e0710990ec3205660c3af67099be
[thirdparty/systemd.git] / src / test / test-fdset.c
1 /***
2 This file is part of systemd
3
4 Copyright 2014 Ronny Chevalier
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include "fd-util.h"
24 #include "fdset.h"
25 #include "macro.h"
26 #include "util.h"
27
28 static void test_fdset_new_fill(void) {
29 int fd = -1;
30 _cleanup_fdset_free_ FDSet *fdset = NULL;
31 char name[] = "/tmp/test-fdset_new_fill.XXXXXX";
32
33 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
34 assert_se(fd >= 0);
35 assert_se(fdset_new_fill(&fdset) >= 0);
36 assert_se(fdset_contains(fdset, fd));
37
38 unlink(name);
39 }
40
41 static void test_fdset_put_dup(void) {
42 _cleanup_close_ int fd = -1;
43 int copyfd = -1;
44 _cleanup_fdset_free_ FDSet *fdset = NULL;
45 char name[] = "/tmp/test-fdset_put_dup.XXXXXX";
46
47 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
48 assert_se(fd >= 0);
49
50 fdset = fdset_new();
51 assert_se(fdset);
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));
56
57 unlink(name);
58 }
59
60 static void test_fdset_cloexec(void) {
61 int fd = -1;
62 _cleanup_fdset_free_ FDSet *fdset = NULL;
63 int flags = -1;
64 char name[] = "/tmp/test-fdset_cloexec.XXXXXX";
65
66 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
67 assert_se(fd >= 0);
68
69 fdset = fdset_new();
70 assert_se(fdset);
71 assert_se(fdset_put(fdset, fd));
72
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));
77
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);
82
83 unlink(name);
84 }
85
86 static void test_fdset_close_others(void) {
87 int fd = -1;
88 int copyfd = -1;
89 _cleanup_fdset_free_ FDSet *fdset = NULL;
90 int flags = -1;
91 char name[] = "/tmp/test-fdset_close_others.XXXXXX";
92
93 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
94 assert_se(fd >= 0);
95
96 fdset = fdset_new();
97 assert_se(fdset);
98 copyfd = fdset_put_dup(fdset, fd);
99 assert_se(copyfd >= 0);
100
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);
106
107 unlink(name);
108 }
109
110 static void test_fdset_remove(void) {
111 _cleanup_close_ int fd = -1;
112 FDSet *fdset = NULL;
113 char name[] = "/tmp/test-fdset_remove.XXXXXX";
114
115 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
116 assert_se(fd >= 0);
117
118 fdset = fdset_new();
119 assert_se(fdset);
120 assert_se(fdset_put(fdset, fd) >= 0);
121 assert_se(fdset_remove(fdset, fd) >= 0);
122 assert_se(!fdset_contains(fdset, fd));
123 fdset_free(fdset);
124
125 assert_se(fcntl(fd, F_GETFD) >= 0);
126
127 unlink(name);
128 }
129
130 static void test_fdset_iterate(void) {
131 int fd = -1;
132 FDSet *fdset = NULL;
133 char name[] = "/tmp/test-fdset_iterate.XXXXXX";
134 Iterator i;
135 int c = 0;
136 int a;
137
138 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
139 assert_se(fd >= 0);
140
141 fdset = fdset_new();
142 assert_se(fdset);
143 assert_se(fdset_put(fdset, fd) >= 0);
144 assert_se(fdset_put(fdset, fd) >= 0);
145 assert_se(fdset_put(fdset, fd) >= 0);
146
147 FDSET_FOREACH(a, fdset, i) {
148 c++;
149 assert_se(a == fd);
150 }
151 assert_se(c == 1);
152
153 fdset_free(fdset);
154
155 unlink(name);
156 }
157
158 static void test_fdset_isempty(void) {
159 int fd;
160 _cleanup_fdset_free_ FDSet *fdset = NULL;
161 char name[] = "/tmp/test-fdset_isempty.XXXXXX";
162
163 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
164 assert_se(fd >= 0);
165
166 fdset = fdset_new();
167 assert_se(fdset);
168
169 assert_se(fdset_isempty(fdset));
170 assert_se(fdset_put(fdset, fd) >= 0);
171 assert_se(!fdset_isempty(fdset));
172
173 unlink(name);
174 }
175
176 static void test_fdset_steal_first(void) {
177 int fd;
178 _cleanup_fdset_free_ FDSet *fdset = NULL;
179 char name[] = "/tmp/test-fdset_steal_first.XXXXXX";
180
181 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
182 assert_se(fd >= 0);
183
184 fdset = fdset_new();
185 assert_se(fdset);
186
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);
192
193 unlink(name);
194 }
195
196 static void test_fdset_new_array(void) {
197 int fds[] = {10, 11, 12, 13};
198 _cleanup_fdset_free_ FDSet *fdset = NULL;
199
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));
206 }
207
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();
213 test_fdset_remove();
214 test_fdset_iterate();
215 test_fdset_isempty();
216 test_fdset_steal_first();
217 test_fdset_new_array();
218
219 return 0;
220 }