2 * test_utils_compat_poll.c
4 * Unit tests for the compatibility layer of poll/epoll API.
6 * Copyright (C) 2019 Yannick Lamarre <ylamarre@efficios.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by as
10 * published by the Free Software Foundation; only version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23 #include <sys/types.h>
28 #include <common/compat/poll.h>
29 #include <common/readwrite.h>
31 /* Verification without trashing test order in the child process */
32 #define childok(e, test, ...) do { \
34 diag(test, ## __VA_ARGS__); \
35 _exit(EXIT_FAILURE); \
40 int lttng_opt_quiet
= 1;
41 int lttng_opt_verbose
;
45 * Non-zero 8-bits arbitrary value below 0x7f to ensure no sign extension
46 * occurs. Used to verify that the value is properly propagated through the
49 #define MAGIC_VALUE ((char) 0x5A)
58 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
59 #define CLOE_VALUE EPOLL_CLOEXEC
61 #define CLOE_VALUE FD_CLOEXEC
64 void test_epoll_compat(void)
67 * Type conversion present to disable warning of anonymous enum from
70 ok((int) LTTNG_CLOEXEC
== (int) CLOE_VALUE
, "epoll's CLOEXEC value");
76 struct lttng_poll_event poll_events
;
78 lttng_poll_init(&poll_events
);
81 ok(lttng_poll_create(NULL
, 1, 0) != 0, "Create over NULL pointer fails");
83 ok(lttng_poll_create(&poll_events
, 0, 0) != 0, "Create with size 0 fails");
85 ok(lttng_poll_create(&poll_events
, 1, 0) == 0, "Create valid poll set succeeds");
87 * lttng_poll_event structure untested due to incompatibility across
88 * sublayers. lttng_poll_clean cannot be tested. There is no success
89 * criteria. Verify set's max size cases.
91 lttng_poll_clean(&poll_events
);
94 /* Tests stuff related to what would be handled with epoll_ctl. */
95 void test_add_del(void)
97 struct lttng_poll_event poll_events
;
99 lttng_poll_init(&poll_events
);
100 ok(lttng_poll_add(NULL
, 1, LPOLLIN
) != 0, "Adding to NULL set fails");
101 ok(lttng_poll_add(&poll_events
, 1, LPOLLIN
) != 0, "Adding to uninitialized structure fails");
102 ok(lttng_poll_add(&poll_events
, -1, LPOLLIN
) != 0, "Adding invalid FD fails");
104 ok(lttng_poll_create(&poll_events
, 1, 0) == 0, "Create a poll set succeeds");
105 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Set created empty");
107 ok(lttng_poll_add(NULL
, 1, LPOLLIN
) != 0, "Adding to NULL set fails");
108 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Set still empty");
109 ok(lttng_poll_add(&poll_events
, -1, LPOLLIN
) != 0, "Adding invalid FD fails");
110 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Set still empty");
112 ok(lttng_poll_add(&poll_events
, 1, LPOLLIN
) == 0, "Adding valid FD succeeds");
113 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Nb of elements incremented");
115 ok(lttng_poll_del(NULL
, 1) != 0, "Removing from NULL set fails");
116 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Number of FD in set unchanged");
118 ok(lttng_poll_del(&poll_events
, -1) != 0, "Removing from negative FD fails");
119 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Number of FD in set unchanged");
121 ok(lttng_poll_del(&poll_events
, 2) == 0, "Removing invalid FD still succeeds");
122 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Number of elements unchanged");
124 ok(lttng_poll_del(&poll_events
, 1) == 0, "Removing valid FD succeeds");
125 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Nb of elements decremented");
127 ok(lttng_poll_del(&poll_events
, 1) != 0, "Removing from empty set fails");
128 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Nb of elements unchanged");
130 lttng_poll_clean(&poll_events
);
133 void test_mod_wait(void)
135 struct lttng_poll_event poll_events
;
136 struct lttng_poll_event cpoll_events
;
140 char rbuf
= 0, tbuf
= MAGIC_VALUE
;
143 lttng_poll_init(&poll_events
);
144 lttng_poll_init(&cpoll_events
);
146 ok(pipe(hupfd
) != -1, "pipe function succeeds");
147 ok(pipe(infd
) != -1, "pipe function succeeds");
151 childok(lttng_poll_create(&cpoll_events
, 1, 0) == 0, "Create valid poll set succeeds");
152 childok(lttng_poll_mod(NULL
, infd
[0], LPOLLIN
) == -1, "lttng_poll_mod with invalid input returns an error");
153 childok(lttng_poll_mod(&cpoll_events
, infd
[0], LPOLLIN
) == -1, "lttng_poll_mod with invalid input returns an error");
154 childok(lttng_poll_add(&cpoll_events
, infd
[0], LPOLLHUP
) == 0, "Add valid FD succeeds");
155 childok(lttng_poll_mod(&cpoll_events
, -1, LPOLLIN
) == -1, "lttng_poll_mod with invalid input returns an error");
156 childok(lttng_poll_mod(&cpoll_events
, hupfd
[0], LPOLLIN
) == 0, "lttng_poll_mod on unincluded FD goes on");
157 childok(lttng_poll_mod(&cpoll_events
, infd
[0], LPOLLIN
) == 0, "Modify event type succeeds");
158 childok(close(infd
[1]) == 0, "Close valid FD succeeds");
159 childok(lttng_poll_wait(&cpoll_events
, -1) == 1, "Wait on close times out");
160 childok(lttng_read(infd
[0], &rbuf
, 1) == 1, "Data is present in the pipe");
161 childok(rbuf
== MAGIC_VALUE
, "Received data is consistent with transmitted data");
162 childok(lttng_poll_del(&cpoll_events
, infd
[0]) == 0, "Removing valid FD succeeds");
163 childok(close(infd
[0]) == 0, "Close valid FD succeeds");
164 childok(close(hupfd
[0]) == 0, "Close valid FD succeeds");
165 childok(close(hupfd
[1]) == 0, "Close valid FD succeeds");
166 lttng_poll_clean(&cpoll_events
);
169 ok(close(hupfd
[1]) == 0, "Close valid FD succeeds");
170 ok(close(infd
[0]) == 0, "Close valid FD succeeds");
172 ok(lttng_poll_wait(NULL
, -1) == -1, "lttng_poll_wait call with invalid input returns error");
174 ok(lttng_poll_create(&poll_events
, 1, 0) == 0, "Create valid poll set succeeds");
175 ok(lttng_poll_wait(&poll_events
, -1) == -1, "lttng_poll_wait call with invalid input returns error");
176 ok(lttng_poll_add(&poll_events
, hupfd
[0], LPOLLHUP
) == 0, "Add valid FD succeeds");
177 ok(lttng_write(infd
[1], &tbuf
, 1) == 1, "Write to pipe succeeds");
178 ok(lttng_poll_wait(&poll_events
, -1) == 1, "Wakes up on one event");
179 ok(lttng_poll_del(&poll_events
, hupfd
[0]) == 0, "Removing valid FD succeeds");
180 ok(close(hupfd
[0]) == 0, "Close valid FD succeeds");
181 ok(close(infd
[1]) == 0, "Close valid FD succeeds");
182 lttng_poll_clean(&poll_events
);
183 ok(waitpid(cpid
, &wstatus
, 0) == cpid
, "Wait for child exit");
184 ok(WIFEXITED(wstatus
) == 1, "Child process exited");
185 ok(WEXITSTATUS(wstatus
) == EXIT_SUCCESS
, "Child process exited with EXIT_SUCCESS");
189 void test_func_def(void)
191 #ifdef LTTNG_POLL_GETFD
197 #ifdef LTTNG_POLL_GETEV
203 #ifdef LTTNG_POLL_GETSZ
209 #ifdef LTTNG_POLL_GET_PREV_FD
210 #define PASS_GET_PREV_FD 1
212 #define PASS_GET_PREV_FD 0
215 ok(lttng_poll_reset
== lttng_poll_reset
, "lttng_poll_reset is defined");
216 ok(lttng_poll_init
== lttng_poll_init
, "lttng_poll_init is defined");
217 ok(PASS_GETFD
, "GETFD is defined");
218 ok(PASS_GETEV
, "GETEV is defined");
219 ok(PASS_GETSZ
, "GETSZ is defined");
220 ok(PASS_GET_PREV_FD
, "GET_PREV_FD is defined");
225 plan_tests(NUM_TESTS
);
233 return exit_status();