vscode: Add configurations to run the executables under the debugger
[lttng-tools.git] / tests / unit / test_utils_compat_poll.cpp
CommitLineData
b12e3724
YL
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 *
9d16b343 8 * SPDX-License-Identifier: GPL-2.0-only
b12e3724 9 *
b12e3724
YL
10 */
11
28ab034a
JG
12#include <common/compat/poll.hpp>
13#include <common/dynamic-array.hpp>
14#include <common/pipe.hpp>
15#include <common/readwrite.hpp>
16
b12e3724
YL
17#include <inttypes.h>
18#include <stdio.h>
19#include <string.h>
b12e3724
YL
20#include <sys/types.h>
21#include <sys/wait.h>
e30c79d2 22#include <tap/tap.h>
28ab034a 23#include <unistd.h>
e30c79d2 24
b12e3724 25/* Verification without trashing test order in the child process */
28ab034a
JG
26#define childok(e, test, ...) \
27 do { \
28 if (!(e)) { \
29 diag(test, ##__VA_ARGS__); \
30 _exit(EXIT_FAILURE); \
31 } \
32 } while (0)
b12e3724
YL
33
34/* For error.h */
35int lttng_opt_quiet = 1;
36int lttng_opt_verbose;
37int lttng_opt_mi;
38
41b42b3b
JG
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
b12e3724 46#ifdef HAVE_EPOLL
efeeaae1 47#define NUM_TESTS 48
b12e3724 48#else
efeeaae1 49#define NUM_TESTS 47
b12e3724
YL
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
cd9adb8b 59static void test_epoll_compat()
b12e3724
YL
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
cd9adb8b 69static void test_alloc()
b12e3724
YL
70{
71 struct lttng_poll_event poll_events;
72
73 lttng_poll_init(&poll_events);
74
75 /* Null pointer */
cd9adb8b 76 ok(lttng_poll_create(nullptr, 1, 0) != 0, "Create over NULL pointer fails");
b12e3724
YL
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. */
cd9adb8b 90static void test_add_del()
b12e3724
YL
91{
92 struct lttng_poll_event poll_events;
93
94 lttng_poll_init(&poll_events);
cd9adb8b 95 ok(lttng_poll_add(nullptr, 1, LPOLLIN) != 0, "Adding to NULL set fails");
28ab034a
JG
96 ok(lttng_poll_add(&poll_events, 1, LPOLLIN) != 0,
97 "Adding to uninitialized structure fails");
b12e3724
YL
98 ok(lttng_poll_add(&poll_events, -1, LPOLLIN) != 0, "Adding invalid FD fails");
99
89226f60 100 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create a poll set succeeds");
b12e3724
YL
101 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set created empty");
102
cd9adb8b 103 ok(lttng_poll_add(nullptr, 1, LPOLLIN) != 0, "Adding to NULL set fails");
b12e3724
YL
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
cd9adb8b 111 ok(lttng_poll_del(nullptr, 1) != 0, "Removing from NULL set fails");
b12e3724
YL
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
cd9adb8b 129static void test_mod_wait()
b12e3724
YL
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) {
28ab034a
JG
147 childok(lttng_poll_create(&cpoll_events, 1, 0) == 0,
148 "Create valid poll set succeeds");
cd9adb8b 149 childok(lttng_poll_mod(nullptr, infd[0], LPOLLIN) == -1,
28ab034a
JG
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");
b12e3724
YL
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
cd9adb8b 175 ok(lttng_poll_wait(nullptr, -1) == -1,
28ab034a 176 "lttng_poll_wait call with invalid input returns error");
b12e3724
YL
177
178 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create valid poll set succeeds");
28ab034a
JG
179 ok(lttng_poll_wait(&poll_events, -1) == -1,
180 "lttng_poll_wait call with invalid input returns error");
1524f98c 181 ok(lttng_poll_add(&poll_events, hupfd[0], LPOLLIN) == 0, "Add valid FD succeeds");
b12e3724
YL
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
efeeaae1
JG
194static void destroy_pipe(void *pipe)
195{
740da7d5 196 lttng_pipe_destroy((lttng_pipe *) pipe);
efeeaae1
JG
197}
198
28ab034a 199static int run_active_set_combination(unsigned int fd_count, unsigned int active_fds_mask)
efeeaae1
JG
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;
cd9adb8b 206 struct lttng_pipe *pipe = nullptr;
efeeaae1
JG
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) {
28ab034a 213 diag("Failed to create poll set for %u file descriptors", fd_count);
efeeaae1
JG
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
28ab034a 226 ret = lttng_poll_add(&poll_events, lttng_pipe_get_readfd(pipe), LPOLLIN);
efeeaae1
JG
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. */
cd9adb8b 241 pipe = nullptr;
efeeaae1
JG
242 }
243
244 /* Write one byte for all active fds that should be active. */
245 for (i = 0; i < fd_count; i++) {
c26ada1f 246 struct lttng_pipe *borrowed_pipe;
efeeaae1
JG
247
248 /* Should this fd be made active? */
249 if (!(active_fds_mask & (1 << i))) {
250 continue;
251 }
252
28ab034a 253 borrowed_pipe = (lttng_pipe *) lttng_dynamic_pointer_array_get_pointer(&pipes, i);
efeeaae1 254
740da7d5 255 char c = 'a';
28ab034a 256 ret = lttng_pipe_write(borrowed_pipe, &c, sizeof(char));
efeeaae1
JG
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",
28ab034a
JG
267 ret,
268 active_fds_count);
efeeaae1
JG
269 ret = -1;
270 goto end;
271 } else {
272 /* Success! */
273 ret = 0;
274 }
275
276end:
277 lttng_dynamic_pointer_array_reset(&pipes);
278 lttng_poll_clean(&poll_events);
279 lttng_pipe_destroy(pipe);
280 return ret;
281}
282
283static 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? */
a0377dfe 288 LTTNG_ASSERT(fd_count <= 32);
efeeaae1
JG
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;
304fail:
305 fail("Test all combinations of active file descriptors for %u file descriptors", fd_count);
306}
307
cd9adb8b 308static void test_func_def()
b12e3724
YL
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");
28ab034a 335 ok(lttng_poll_init == lttng_poll_init, "lttng_poll_init is defined");
b12e3724
YL
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
cd9adb8b 342int main()
b12e3724
YL
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();
efeeaae1 352 test_active_set_combinations(8);
b12e3724
YL
353 return exit_status();
354}
This page took 0.060934 seconds and 4 git commands to generate.