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