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