]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fdset.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-fdset.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "fd-util.h"
7 #include "fdset.h"
8 #include "fileio.h"
9 #include "macro.h"
10 #include "util.h"
11
12 static void test_fdset_new_fill(void) {
13 int fd = -1;
14 _cleanup_fdset_free_ FDSet *fdset = NULL;
15 char name[] = "/tmp/test-fdset_new_fill.XXXXXX";
16
17 fd = mkostemp_safe(name);
18 assert_se(fd >= 0);
19 assert_se(fdset_new_fill(&fdset) >= 0);
20 assert_se(fdset_contains(fdset, fd));
21
22 unlink(name);
23 }
24
25 static void test_fdset_put_dup(void) {
26 _cleanup_close_ int fd = -1;
27 int copyfd = -1;
28 _cleanup_fdset_free_ FDSet *fdset = NULL;
29 char name[] = "/tmp/test-fdset_put_dup.XXXXXX";
30
31 fd = mkostemp_safe(name);
32 assert_se(fd >= 0);
33
34 fdset = fdset_new();
35 assert_se(fdset);
36 copyfd = fdset_put_dup(fdset, fd);
37 assert_se(copyfd >= 0 && copyfd != fd);
38 assert_se(fdset_contains(fdset, copyfd));
39 assert_se(!fdset_contains(fdset, fd));
40
41 unlink(name);
42 }
43
44 static void test_fdset_cloexec(void) {
45 int fd = -1;
46 _cleanup_fdset_free_ FDSet *fdset = NULL;
47 int flags = -1;
48 char name[] = "/tmp/test-fdset_cloexec.XXXXXX";
49
50 fd = mkostemp_safe(name);
51 assert_se(fd >= 0);
52
53 fdset = fdset_new();
54 assert_se(fdset);
55 assert_se(fdset_put(fdset, fd));
56
57 assert_se(fdset_cloexec(fdset, false) >= 0);
58 flags = fcntl(fd, F_GETFD);
59 assert_se(flags >= 0);
60 assert_se(!(flags & FD_CLOEXEC));
61
62 assert_se(fdset_cloexec(fdset, true) >= 0);
63 flags = fcntl(fd, F_GETFD);
64 assert_se(flags >= 0);
65 assert_se(flags & FD_CLOEXEC);
66
67 unlink(name);
68 }
69
70 static void test_fdset_close_others(void) {
71 int fd = -1;
72 int copyfd = -1;
73 _cleanup_fdset_free_ FDSet *fdset = NULL;
74 int flags = -1;
75 char name[] = "/tmp/test-fdset_close_others.XXXXXX";
76
77 fd = mkostemp_safe(name);
78 assert_se(fd >= 0);
79
80 fdset = fdset_new();
81 assert_se(fdset);
82 copyfd = fdset_put_dup(fdset, fd);
83 assert_se(copyfd >= 0);
84
85 assert_se(fdset_close_others(fdset) >= 0);
86 flags = fcntl(fd, F_GETFD);
87 assert_se(flags < 0);
88 flags = fcntl(copyfd, F_GETFD);
89 assert_se(flags >= 0);
90
91 unlink(name);
92 }
93
94 static void test_fdset_remove(void) {
95 _cleanup_close_ int fd = -1;
96 FDSet *fdset = NULL;
97 char name[] = "/tmp/test-fdset_remove.XXXXXX";
98
99 fd = mkostemp_safe(name);
100 assert_se(fd >= 0);
101
102 fdset = fdset_new();
103 assert_se(fdset);
104 assert_se(fdset_put(fdset, fd) >= 0);
105 assert_se(fdset_remove(fdset, fd) >= 0);
106 assert_se(!fdset_contains(fdset, fd));
107 fdset_free(fdset);
108
109 assert_se(fcntl(fd, F_GETFD) >= 0);
110
111 unlink(name);
112 }
113
114 static void test_fdset_iterate(void) {
115 int fd = -1;
116 FDSet *fdset = NULL;
117 char name[] = "/tmp/test-fdset_iterate.XXXXXX";
118 Iterator i;
119 int c = 0;
120 int a;
121
122 fd = mkostemp_safe(name);
123 assert_se(fd >= 0);
124
125 fdset = fdset_new();
126 assert_se(fdset);
127 assert_se(fdset_put(fdset, fd) >= 0);
128 assert_se(fdset_put(fdset, fd) >= 0);
129 assert_se(fdset_put(fdset, fd) >= 0);
130
131 FDSET_FOREACH(a, fdset, i) {
132 c++;
133 assert_se(a == fd);
134 }
135 assert_se(c == 1);
136
137 fdset_free(fdset);
138
139 unlink(name);
140 }
141
142 static void test_fdset_isempty(void) {
143 int fd;
144 _cleanup_fdset_free_ FDSet *fdset = NULL;
145 char name[] = "/tmp/test-fdset_isempty.XXXXXX";
146
147 fd = mkostemp_safe(name);
148 assert_se(fd >= 0);
149
150 fdset = fdset_new();
151 assert_se(fdset);
152
153 assert_se(fdset_isempty(fdset));
154 assert_se(fdset_put(fdset, fd) >= 0);
155 assert_se(!fdset_isempty(fdset));
156
157 unlink(name);
158 }
159
160 static void test_fdset_steal_first(void) {
161 int fd;
162 _cleanup_fdset_free_ FDSet *fdset = NULL;
163 char name[] = "/tmp/test-fdset_steal_first.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_steal_first(fdset) < 0);
172 assert_se(fdset_put(fdset, fd) >= 0);
173 assert_se(fdset_steal_first(fdset) == fd);
174 assert_se(fdset_steal_first(fdset) < 0);
175 assert_se(fdset_put(fdset, fd) >= 0);
176
177 unlink(name);
178 }
179
180 static void test_fdset_new_array(void) {
181 int fds[] = {10, 11, 12, 13};
182 _cleanup_fdset_free_ FDSet *fdset = NULL;
183
184 assert_se(fdset_new_array(&fdset, fds, 4) >= 0);
185 assert_se(fdset_size(fdset) == 4);
186 assert_se(fdset_contains(fdset, 10));
187 assert_se(fdset_contains(fdset, 11));
188 assert_se(fdset_contains(fdset, 12));
189 assert_se(fdset_contains(fdset, 13));
190 }
191
192 int main(int argc, char *argv[]) {
193 test_fdset_new_fill();
194 test_fdset_put_dup();
195 test_fdset_cloexec();
196 test_fdset_close_others();
197 test_fdset_remove();
198 test_fdset_iterate();
199 test_fdset_isempty();
200 test_fdset_steal_first();
201 test_fdset_new_array();
202
203 return 0;
204 }