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