Rename C++ header files to .hpp
[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
b12e3724
YL
12#include <inttypes.h>
13#include <stdio.h>
14#include <string.h>
15#include <unistd.h>
16#include <sys/types.h>
17#include <sys/wait.h>
18
19#include <tap/tap.h>
20
c9e313bc
SM
21#include <common/compat/poll.hpp>
22#include <common/readwrite.hpp>
23#include <common/pipe.hpp>
24#include <common/dynamic-array.hpp>
b12e3724
YL
25
26/* Verification without trashing test order in the child process */
27#define childok(e, test, ...) do { \
28 if (!(e)) { \
29 diag(test, ## __VA_ARGS__); \
30 _exit(EXIT_FAILURE); \
31 } \
32} while(0)
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
f8e06d39 59static
b12e3724
YL
60void test_epoll_compat(void)
61{
62 /*
63 * Type conversion present to disable warning of anonymous enum from
64 * compiler.
65 */
66 ok((int) LTTNG_CLOEXEC == (int) CLOE_VALUE, "epoll's CLOEXEC value");
67}
68#endif
69
f7f52067 70static void test_alloc(void)
b12e3724
YL
71{
72 struct lttng_poll_event poll_events;
73
74 lttng_poll_init(&poll_events);
75
76 /* Null pointer */
77 ok(lttng_poll_create(NULL, 1, 0) != 0, "Create over NULL pointer fails");
78 /* Size 0 */
79 ok(lttng_poll_create(&poll_events, 0, 0) != 0, "Create with size 0 fails");
80 /* without CLOEXEC */
81 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create valid poll set succeeds");
82 /*
83 * lttng_poll_event structure untested due to incompatibility across
84 * sublayers. lttng_poll_clean cannot be tested. There is no success
85 * criteria. Verify set's max size cases.
86 */
87 lttng_poll_clean(&poll_events);
88}
89
90/* Tests stuff related to what would be handled with epoll_ctl. */
f7f52067 91static void test_add_del(void)
b12e3724
YL
92{
93 struct lttng_poll_event poll_events;
94
95 lttng_poll_init(&poll_events);
96 ok(lttng_poll_add(NULL, 1, LPOLLIN) != 0, "Adding to NULL set fails");
97 ok(lttng_poll_add(&poll_events, 1, LPOLLIN) != 0, "Adding to uninitialized structure fails");
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
103 ok(lttng_poll_add(NULL, 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(NULL, 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
f7f52067 129static void test_mod_wait(void)
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) {
147 childok(lttng_poll_create(&cpoll_events, 1, 0) == 0, "Create valid poll set succeeds");
148 childok(lttng_poll_mod(NULL, infd[0], LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error");
149 childok(lttng_poll_mod(&cpoll_events, infd[0], LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error");
150 childok(lttng_poll_add(&cpoll_events, infd[0], LPOLLHUP) == 0, "Add valid FD succeeds");
151 childok(lttng_poll_mod(&cpoll_events, -1, LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error");
152 childok(lttng_poll_mod(&cpoll_events, hupfd[0], LPOLLIN) == 0, "lttng_poll_mod on unincluded FD goes on");
153 childok(lttng_poll_mod(&cpoll_events, infd[0], LPOLLIN) == 0, "Modify event type succeeds");
154 childok(close(infd[1]) == 0, "Close valid FD succeeds");
155 childok(lttng_poll_wait(&cpoll_events, -1) == 1, "Wait on close times out");
156 childok(lttng_read(infd[0], &rbuf, 1) == 1, "Data is present in the pipe");
157 childok(rbuf == MAGIC_VALUE, "Received data is consistent with transmitted data");
158 childok(lttng_poll_del(&cpoll_events, infd[0]) == 0, "Removing valid FD succeeds");
159 childok(close(infd[0]) == 0, "Close valid FD succeeds");
160 childok(close(hupfd[0]) == 0, "Close valid FD succeeds");
161 childok(close(hupfd[1]) == 0, "Close valid FD succeeds");
162 lttng_poll_clean(&cpoll_events);
163 _exit(EXIT_SUCCESS);
164 } else {
165 ok(close(hupfd[1]) == 0, "Close valid FD succeeds");
166 ok(close(infd[0]) == 0, "Close valid FD succeeds");
167
168 ok(lttng_poll_wait(NULL, -1) == -1, "lttng_poll_wait call with invalid input returns error");
169
170 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create valid poll set succeeds");
171 ok(lttng_poll_wait(&poll_events, -1) == -1, "lttng_poll_wait call with invalid input returns error");
172 ok(lttng_poll_add(&poll_events, hupfd[0], LPOLLHUP) == 0, "Add valid FD succeeds");
173 ok(lttng_write(infd[1], &tbuf, 1) == 1, "Write to pipe succeeds");
174 ok(lttng_poll_wait(&poll_events, -1) == 1, "Wakes up on one event");
175 ok(lttng_poll_del(&poll_events, hupfd[0]) == 0, "Removing valid FD succeeds");
176 ok(close(hupfd[0]) == 0, "Close valid FD succeeds");
177 ok(close(infd[1]) == 0, "Close valid FD succeeds");
178 lttng_poll_clean(&poll_events);
179 ok(waitpid(cpid, &wstatus, 0) == cpid, "Wait for child exit");
180 ok(WIFEXITED(wstatus) == 1, "Child process exited");
181 ok(WEXITSTATUS(wstatus) == EXIT_SUCCESS, "Child process exited with EXIT_SUCCESS");
182 }
183}
184
efeeaae1
JG
185static void destroy_pipe(void *pipe)
186{
740da7d5 187 lttng_pipe_destroy((lttng_pipe *) pipe);
efeeaae1
JG
188}
189
190static int run_active_set_combination(unsigned int fd_count,
191 unsigned int active_fds_mask)
192{
193 int ret = 0;
194 unsigned int i;
195 const unsigned int active_fds_count = __builtin_popcount(active_fds_mask);
196 struct lttng_poll_event poll_events;
197 struct lttng_dynamic_pointer_array pipes;
198 struct lttng_pipe *pipe = NULL;
199
200 lttng_poll_init(&poll_events);
201 lttng_dynamic_pointer_array_init(&pipes, destroy_pipe);
202
203 ret = lttng_poll_create(&poll_events, fd_count, 0);
204 if (ret) {
205 diag("Failed to create poll set for %u file descriptors",
206 fd_count);
207 goto end;
208 }
209
210 for (i = 0; i < fd_count; i++) {
211 pipe = lttng_pipe_open(0);
212
213 if (!pipe) {
214 diag("Failed to allocate pipe");
215 ret = -1;
216 goto end;
217 }
218
219 ret = lttng_poll_add(&poll_events, lttng_pipe_get_readfd(pipe),
220 LPOLLIN);
221 if (ret) {
222 diag("Failed to add file descriptor to poll set");
223 ret = -1;
224 goto end;
225 }
226
227 ret = lttng_dynamic_pointer_array_add_pointer(&pipes, pipe);
228 if (ret) {
229 diag("Failed to add pipe to pipes array");
230 ret = -1;
231 goto end;
232 }
233
234 /* Ownership transferred to the pointer array. */
235 pipe = NULL;
236 }
237
238 /* Write one byte for all active fds that should be active. */
239 for (i = 0; i < fd_count; i++) {
c26ada1f 240 struct lttng_pipe *borrowed_pipe;
efeeaae1
JG
241
242 /* Should this fd be made active? */
243 if (!(active_fds_mask & (1 << i))) {
244 continue;
245 }
246
740da7d5
SM
247 borrowed_pipe =
248 (lttng_pipe *) lttng_dynamic_pointer_array_get_pointer(
c26ada1f 249 &pipes, i);
efeeaae1 250
740da7d5 251 char c = 'a';
c26ada1f 252 ret = lttng_pipe_write(
740da7d5 253 borrowed_pipe, &c, sizeof(char));
efeeaae1
JG
254 if (ret != sizeof(char)) {
255 diag("Failed to write to pipe");
256 ret = -1;
257 goto end;
258 }
259 }
260
261 ret = lttng_poll_wait(&poll_events, 0);
262 if (ret != active_fds_count) {
263 diag("lttng_poll_wait returned %d, expected %u active file descriptors",
264 ret, active_fds_count);
265 ret = -1;
266 goto end;
267 } else {
268 /* Success! */
269 ret = 0;
270 }
271
272end:
273 lttng_dynamic_pointer_array_reset(&pipes);
274 lttng_poll_clean(&poll_events);
275 lttng_pipe_destroy(pipe);
276 return ret;
277}
278
279static void test_active_set_combinations(unsigned int fd_count)
280{
281 unsigned int i, all_active_mask = 0;
282
283 /* Do you really want to test more than 4,294,967,295 combinations? */
a0377dfe 284 LTTNG_ASSERT(fd_count <= 32);
efeeaae1
JG
285
286 for (i = 0; i < fd_count; i++) {
287 all_active_mask |= (1 << i);
288 }
289
290 for (i = 0; i <= all_active_mask; i++) {
291 const int ret = run_active_set_combination(fd_count, i);
292
293 if (ret) {
294 goto fail;
295 }
296 }
297
298 pass("Test all combinations of active file descriptors for %u file descriptors", fd_count);
299 return;
300fail:
301 fail("Test all combinations of active file descriptors for %u file descriptors", fd_count);
302}
303
f7f52067 304static void test_func_def(void)
b12e3724
YL
305{
306#ifdef LTTNG_POLL_GETFD
307#define PASS_GETFD 1
308#else
309#define PASS_GETFD 0
310#endif
311
312#ifdef LTTNG_POLL_GETEV
313#define PASS_GETEV 1
314#else
315#define PASS_GETEV 0
316#endif
317
318#ifdef LTTNG_POLL_GETSZ
319#define PASS_GETSZ 1
320#else
321#define PASS_GETSZ 0
322#endif
323
324#ifdef LTTNG_POLL_GET_PREV_FD
325#define PASS_GET_PREV_FD 1
326#else
327#define PASS_GET_PREV_FD 0
328#endif
329
330 ok(lttng_poll_reset == lttng_poll_reset, "lttng_poll_reset is defined");
331 ok(lttng_poll_init == lttng_poll_init , "lttng_poll_init is defined");
332 ok(PASS_GETFD, "GETFD is defined");
333 ok(PASS_GETEV, "GETEV is defined");
334 ok(PASS_GETSZ, "GETSZ is defined");
335 ok(PASS_GET_PREV_FD, "GET_PREV_FD is defined");
336}
337
338int main(void)
339{
340 plan_tests(NUM_TESTS);
341#ifdef HAVE_EPOLL
342 test_epoll_compat();
343#endif
344 test_func_def();
345 test_alloc();
346 test_add_del();
347 test_mod_wait();
efeeaae1 348 test_active_set_combinations(8);
b12e3724
YL
349 return exit_status();
350}
This page took 0.048062 seconds and 4 git commands to generate.