lttng-ctl: health: remove unreachable condition
[lttng-tools.git] / tests / unit / test_utils_compat_poll.c
1 /*
2 * test_utils_compat_poll.c
3 *
4 * Unit tests for the compatibility layer of poll/epoll API.
5 *
6 * Copyright (C) 2019 Yannick Lamarre <ylamarre@efficios.com>
7 *
8 * SPDX-License-Identifier: GPL-2.0-only
9 *
10 */
11
12 #include <assert.h>
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19
20 #include <tap/tap.h>
21
22 #include <common/compat/poll.h>
23 #include <common/readwrite.h>
24 #include <common/pipe.h>
25 #include <common/dynamic-array.h>
26
27 /* Verification without trashing test order in the child process */
28 #define childok(e, test, ...) do { \
29 if (!(e)) { \
30 diag(test, ## __VA_ARGS__); \
31 _exit(EXIT_FAILURE); \
32 } \
33 } while(0)
34
35 /* For error.h */
36 int lttng_opt_quiet = 1;
37 int lttng_opt_verbose;
38 int lttng_opt_mi;
39
40 /*
41 * Non-zero 8-bits arbitrary value below 0x7f to ensure no sign extension
42 * occurs. Used to verify that the value is properly propagated through the
43 * pipe.
44 */
45 #define MAGIC_VALUE ((char) 0x5A)
46
47 #ifdef HAVE_EPOLL
48 #define NUM_TESTS 48
49 #else
50 #define NUM_TESTS 47
51 #endif
52
53 #ifdef HAVE_EPOLL
54 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
55 #define CLOE_VALUE EPOLL_CLOEXEC
56 #else
57 #define CLOE_VALUE FD_CLOEXEC
58 #endif
59
60 static
61 void test_epoll_compat(void)
62 {
63 /*
64 * Type conversion present to disable warning of anonymous enum from
65 * compiler.
66 */
67 ok((int) LTTNG_CLOEXEC == (int) CLOE_VALUE, "epoll's CLOEXEC value");
68 }
69 #endif
70
71 static void test_alloc(void)
72 {
73 struct lttng_poll_event poll_events;
74
75 lttng_poll_init(&poll_events);
76
77 /* Null pointer */
78 ok(lttng_poll_create(NULL, 1, 0) != 0, "Create over NULL pointer fails");
79 /* Size 0 */
80 ok(lttng_poll_create(&poll_events, 0, 0) != 0, "Create with size 0 fails");
81 /* without CLOEXEC */
82 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create valid poll set succeeds");
83 /*
84 * lttng_poll_event structure untested due to incompatibility across
85 * sublayers. lttng_poll_clean cannot be tested. There is no success
86 * criteria. Verify set's max size cases.
87 */
88 lttng_poll_clean(&poll_events);
89 }
90
91 /* Tests stuff related to what would be handled with epoll_ctl. */
92 static void test_add_del(void)
93 {
94 struct lttng_poll_event poll_events;
95
96 lttng_poll_init(&poll_events);
97 ok(lttng_poll_add(NULL, 1, LPOLLIN) != 0, "Adding to NULL set fails");
98 ok(lttng_poll_add(&poll_events, 1, LPOLLIN) != 0, "Adding to uninitialized structure fails");
99 ok(lttng_poll_add(&poll_events, -1, LPOLLIN) != 0, "Adding invalid FD fails");
100
101 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create a poll set succeeds");
102 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set created empty");
103
104 ok(lttng_poll_add(NULL, 1, LPOLLIN) != 0, "Adding to NULL set fails");
105 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set still empty");
106 ok(lttng_poll_add(&poll_events, -1, LPOLLIN) != 0, "Adding invalid FD fails");
107 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set still empty");
108
109 ok(lttng_poll_add(&poll_events, 1, LPOLLIN) == 0, "Adding valid FD succeeds");
110 ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Nb of elements incremented");
111
112 ok(lttng_poll_del(NULL, 1) != 0, "Removing from NULL set fails");
113 ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Number of FD in set unchanged");
114
115 ok(lttng_poll_del(&poll_events, -1) != 0, "Removing from negative FD fails");
116 ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Number of FD in set unchanged");
117
118 ok(lttng_poll_del(&poll_events, 2) == 0, "Removing invalid FD still succeeds");
119 ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Number of elements unchanged");
120
121 ok(lttng_poll_del(&poll_events, 1) == 0, "Removing valid FD succeeds");
122 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Nb of elements decremented");
123
124 ok(lttng_poll_del(&poll_events, 1) != 0, "Removing from empty set fails");
125 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Nb of elements unchanged");
126
127 lttng_poll_clean(&poll_events);
128 }
129
130 static void test_mod_wait(void)
131 {
132 struct lttng_poll_event poll_events;
133 struct lttng_poll_event cpoll_events;
134 int hupfd[2];
135 int infd[2];
136 pid_t cpid;
137 char rbuf = 0, tbuf = MAGIC_VALUE;
138 int wstatus;
139
140 lttng_poll_init(&poll_events);
141 lttng_poll_init(&cpoll_events);
142
143 ok(pipe(hupfd) != -1, "pipe function succeeds");
144 ok(pipe(infd) != -1, "pipe function succeeds");
145
146 cpid = fork();
147 if (cpid == 0) {
148 childok(lttng_poll_create(&cpoll_events, 1, 0) == 0, "Create valid poll set succeeds");
149 childok(lttng_poll_mod(NULL, infd[0], LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error");
150 childok(lttng_poll_mod(&cpoll_events, infd[0], LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error");
151 childok(lttng_poll_add(&cpoll_events, infd[0], LPOLLHUP) == 0, "Add valid FD succeeds");
152 childok(lttng_poll_mod(&cpoll_events, -1, LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error");
153 childok(lttng_poll_mod(&cpoll_events, hupfd[0], LPOLLIN) == 0, "lttng_poll_mod on unincluded FD goes on");
154 childok(lttng_poll_mod(&cpoll_events, infd[0], LPOLLIN) == 0, "Modify event type succeeds");
155 childok(close(infd[1]) == 0, "Close valid FD succeeds");
156 childok(lttng_poll_wait(&cpoll_events, -1) == 1, "Wait on close times out");
157 childok(lttng_read(infd[0], &rbuf, 1) == 1, "Data is present in the pipe");
158 childok(rbuf == MAGIC_VALUE, "Received data is consistent with transmitted data");
159 childok(lttng_poll_del(&cpoll_events, infd[0]) == 0, "Removing valid FD succeeds");
160 childok(close(infd[0]) == 0, "Close valid FD succeeds");
161 childok(close(hupfd[0]) == 0, "Close valid FD succeeds");
162 childok(close(hupfd[1]) == 0, "Close valid FD succeeds");
163 lttng_poll_clean(&cpoll_events);
164 _exit(EXIT_SUCCESS);
165 } else {
166 ok(close(hupfd[1]) == 0, "Close valid FD succeeds");
167 ok(close(infd[0]) == 0, "Close valid FD succeeds");
168
169 ok(lttng_poll_wait(NULL, -1) == -1, "lttng_poll_wait call with invalid input returns error");
170
171 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create valid poll set succeeds");
172 ok(lttng_poll_wait(&poll_events, -1) == -1, "lttng_poll_wait call with invalid input returns error");
173 ok(lttng_poll_add(&poll_events, hupfd[0], LPOLLHUP) == 0, "Add valid FD succeeds");
174 ok(lttng_write(infd[1], &tbuf, 1) == 1, "Write to pipe succeeds");
175 ok(lttng_poll_wait(&poll_events, -1) == 1, "Wakes up on one event");
176 ok(lttng_poll_del(&poll_events, hupfd[0]) == 0, "Removing valid FD succeeds");
177 ok(close(hupfd[0]) == 0, "Close valid FD succeeds");
178 ok(close(infd[1]) == 0, "Close valid FD succeeds");
179 lttng_poll_clean(&poll_events);
180 ok(waitpid(cpid, &wstatus, 0) == cpid, "Wait for child exit");
181 ok(WIFEXITED(wstatus) == 1, "Child process exited");
182 ok(WEXITSTATUS(wstatus) == EXIT_SUCCESS, "Child process exited with EXIT_SUCCESS");
183 }
184 }
185
186 static void destroy_pipe(void *pipe)
187 {
188 lttng_pipe_destroy(pipe);
189 }
190
191 static int run_active_set_combination(unsigned int fd_count,
192 unsigned int active_fds_mask)
193 {
194 int ret = 0;
195 unsigned int i;
196 const unsigned int active_fds_count = __builtin_popcount(active_fds_mask);
197 struct lttng_poll_event poll_events;
198 struct lttng_dynamic_pointer_array pipes;
199 struct lttng_pipe *pipe = NULL;
200
201 lttng_poll_init(&poll_events);
202 lttng_dynamic_pointer_array_init(&pipes, destroy_pipe);
203
204 ret = lttng_poll_create(&poll_events, fd_count, 0);
205 if (ret) {
206 diag("Failed to create poll set for %u file descriptors",
207 fd_count);
208 goto end;
209 }
210
211 for (i = 0; i < fd_count; i++) {
212 pipe = lttng_pipe_open(0);
213
214 if (!pipe) {
215 diag("Failed to allocate pipe");
216 ret = -1;
217 goto end;
218 }
219
220 ret = lttng_poll_add(&poll_events, lttng_pipe_get_readfd(pipe),
221 LPOLLIN);
222 if (ret) {
223 diag("Failed to add file descriptor to poll set");
224 ret = -1;
225 goto end;
226 }
227
228 ret = lttng_dynamic_pointer_array_add_pointer(&pipes, pipe);
229 if (ret) {
230 diag("Failed to add pipe to pipes array");
231 ret = -1;
232 goto end;
233 }
234
235 /* Ownership transferred to the pointer array. */
236 pipe = NULL;
237 }
238
239 /* Write one byte for all active fds that should be active. */
240 for (i = 0; i < fd_count; i++) {
241 struct lttng_pipe *pipe;
242
243 /* Should this fd be made active? */
244 if (!(active_fds_mask & (1 << i))) {
245 continue;
246 }
247
248 pipe = lttng_dynamic_pointer_array_get_pointer(&pipes, i);
249
250 ret = lttng_pipe_write(pipe, &(char) {'a'}, sizeof(char));
251 if (ret != sizeof(char)) {
252 diag("Failed to write to pipe");
253 ret = -1;
254 goto end;
255 }
256 }
257
258 ret = lttng_poll_wait(&poll_events, 0);
259 if (ret != active_fds_count) {
260 diag("lttng_poll_wait returned %d, expected %u active file descriptors",
261 ret, active_fds_count);
262 ret = -1;
263 goto end;
264 } else {
265 /* Success! */
266 ret = 0;
267 }
268
269 end:
270 lttng_dynamic_pointer_array_reset(&pipes);
271 lttng_poll_clean(&poll_events);
272 lttng_pipe_destroy(pipe);
273 return ret;
274 }
275
276 static void test_active_set_combinations(unsigned int fd_count)
277 {
278 unsigned int i, all_active_mask = 0;
279
280 /* Do you really want to test more than 4,294,967,295 combinations? */
281 assert(fd_count <= 32);
282
283 for (i = 0; i < fd_count; i++) {
284 all_active_mask |= (1 << i);
285 }
286
287 for (i = 0; i <= all_active_mask; i++) {
288 const int ret = run_active_set_combination(fd_count, i);
289
290 if (ret) {
291 goto fail;
292 }
293 }
294
295 pass("Test all combinations of active file descriptors for %u file descriptors", fd_count);
296 return;
297 fail:
298 fail("Test all combinations of active file descriptors for %u file descriptors", fd_count);
299 }
300
301 static void test_func_def(void)
302 {
303 #ifdef LTTNG_POLL_GETFD
304 #define PASS_GETFD 1
305 #else
306 #define PASS_GETFD 0
307 #endif
308
309 #ifdef LTTNG_POLL_GETEV
310 #define PASS_GETEV 1
311 #else
312 #define PASS_GETEV 0
313 #endif
314
315 #ifdef LTTNG_POLL_GETSZ
316 #define PASS_GETSZ 1
317 #else
318 #define PASS_GETSZ 0
319 #endif
320
321 #ifdef LTTNG_POLL_GET_PREV_FD
322 #define PASS_GET_PREV_FD 1
323 #else
324 #define PASS_GET_PREV_FD 0
325 #endif
326
327 ok(lttng_poll_reset == lttng_poll_reset, "lttng_poll_reset is defined");
328 ok(lttng_poll_init == lttng_poll_init , "lttng_poll_init is defined");
329 ok(PASS_GETFD, "GETFD is defined");
330 ok(PASS_GETEV, "GETEV is defined");
331 ok(PASS_GETSZ, "GETSZ is defined");
332 ok(PASS_GET_PREV_FD, "GET_PREV_FD is defined");
333 }
334
335 int main(void)
336 {
337 plan_tests(NUM_TESTS);
338 #ifdef HAVE_EPOLL
339 test_epoll_compat();
340 #endif
341 test_func_def();
342 test_alloc();
343 test_add_del();
344 test_mod_wait();
345 test_active_set_combinations(8);
346 return exit_status();
347 }
This page took 0.035907 seconds and 4 git commands to generate.