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