Clean-up: tests: fix -Wshadow error in run_active_set_combination
[lttng-tools.git] / tests / unit / test_utils_compat_poll.c
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
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>
efeeaae1
JG
24#include <common/pipe.h>
25#include <common/dynamic-array.h>
b12e3724
YL
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 */
36int lttng_opt_quiet = 1;
37int lttng_opt_verbose;
38int lttng_opt_mi;
39
41b42b3b
JG
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
b12e3724 47#ifdef HAVE_EPOLL
efeeaae1 48#define NUM_TESTS 48
b12e3724 49#else
efeeaae1 50#define NUM_TESTS 47
b12e3724
YL
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
f8e06d39 60static
b12e3724
YL
61void 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
f7f52067 71static void test_alloc(void)
b12e3724
YL
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. */
f7f52067 92static void test_add_del(void)
b12e3724
YL
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
89226f60 101 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create a poll set succeeds");
b12e3724
YL
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
f7f52067 130static void test_mod_wait(void)
b12e3724
YL
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
efeeaae1
JG
186static void destroy_pipe(void *pipe)
187{
188 lttng_pipe_destroy(pipe);
189}
190
191static 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++) {
c26ada1f 241 struct lttng_pipe *borrowed_pipe;
efeeaae1
JG
242
243 /* Should this fd be made active? */
244 if (!(active_fds_mask & (1 << i))) {
245 continue;
246 }
247
c26ada1f
SM
248 borrowed_pipe = lttng_dynamic_pointer_array_get_pointer(
249 &pipes, i);
efeeaae1 250
c26ada1f
SM
251 ret = lttng_pipe_write(
252 borrowed_pipe, &(char){'a'}, sizeof(char));
efeeaae1
JG
253 if (ret != sizeof(char)) {
254 diag("Failed to write to pipe");
255 ret = -1;
256 goto end;
257 }
258 }
259
260 ret = lttng_poll_wait(&poll_events, 0);
261 if (ret != active_fds_count) {
262 diag("lttng_poll_wait returned %d, expected %u active file descriptors",
263 ret, active_fds_count);
264 ret = -1;
265 goto end;
266 } else {
267 /* Success! */
268 ret = 0;
269 }
270
271end:
272 lttng_dynamic_pointer_array_reset(&pipes);
273 lttng_poll_clean(&poll_events);
274 lttng_pipe_destroy(pipe);
275 return ret;
276}
277
278static void test_active_set_combinations(unsigned int fd_count)
279{
280 unsigned int i, all_active_mask = 0;
281
282 /* Do you really want to test more than 4,294,967,295 combinations? */
283 assert(fd_count <= 32);
284
285 for (i = 0; i < fd_count; i++) {
286 all_active_mask |= (1 << i);
287 }
288
289 for (i = 0; i <= all_active_mask; i++) {
290 const int ret = run_active_set_combination(fd_count, i);
291
292 if (ret) {
293 goto fail;
294 }
295 }
296
297 pass("Test all combinations of active file descriptors for %u file descriptors", fd_count);
298 return;
299fail:
300 fail("Test all combinations of active file descriptors for %u file descriptors", fd_count);
301}
302
f7f52067 303static void test_func_def(void)
b12e3724
YL
304{
305#ifdef LTTNG_POLL_GETFD
306#define PASS_GETFD 1
307#else
308#define PASS_GETFD 0
309#endif
310
311#ifdef LTTNG_POLL_GETEV
312#define PASS_GETEV 1
313#else
314#define PASS_GETEV 0
315#endif
316
317#ifdef LTTNG_POLL_GETSZ
318#define PASS_GETSZ 1
319#else
320#define PASS_GETSZ 0
321#endif
322
323#ifdef LTTNG_POLL_GET_PREV_FD
324#define PASS_GET_PREV_FD 1
325#else
326#define PASS_GET_PREV_FD 0
327#endif
328
329 ok(lttng_poll_reset == lttng_poll_reset, "lttng_poll_reset is defined");
330 ok(lttng_poll_init == lttng_poll_init , "lttng_poll_init is defined");
331 ok(PASS_GETFD, "GETFD is defined");
332 ok(PASS_GETEV, "GETEV is defined");
333 ok(PASS_GETSZ, "GETSZ is defined");
334 ok(PASS_GET_PREV_FD, "GET_PREV_FD is defined");
335}
336
337int main(void)
338{
339 plan_tests(NUM_TESTS);
340#ifdef HAVE_EPOLL
341 test_epoll_compat();
342#endif
343 test_func_def();
344 test_alloc();
345 test_add_del();
346 test_mod_wait();
efeeaae1 347 test_active_set_combinations(8);
b12e3724
YL
348 return exit_status();
349}
This page took 0.070054 seconds and 4 git commands to generate.