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