tests: Move to kernel style SPDX license identifiers
[lttng-tools.git] / tests / unit / test_utils_compat_poll.c
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 <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>
24
25 /* Verification without trashing test order in the child process */
26 #define childok(e, test, ...) do { \
27 if (!(e)) { \
28 diag(test, ## __VA_ARGS__); \
29 _exit(EXIT_FAILURE); \
30 } \
31 } while(0)
32
33 /* For error.h */
34 int lttng_opt_quiet = 1;
35 int lttng_opt_verbose;
36 int lttng_opt_mi;
37
38 /*
39 * Non-zero 8-bits arbitrary value below 0x7f to ensure no sign extension
40 * occurs. Used to verify that the value is properly propagated through the
41 * pipe.
42 */
43 #define MAGIC_VALUE ((char) 0x5A)
44
45 #ifdef HAVE_EPOLL
46 #define NUM_TESTS 47
47 #else
48 #define NUM_TESTS 46
49 #endif
50
51 #ifdef HAVE_EPOLL
52 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
53 #define CLOE_VALUE EPOLL_CLOEXEC
54 #else
55 #define CLOE_VALUE FD_CLOEXEC
56 #endif
57
58 void test_epoll_compat(void)
59 {
60 /*
61 * Type conversion present to disable warning of anonymous enum from
62 * compiler.
63 */
64 ok((int) LTTNG_CLOEXEC == (int) CLOE_VALUE, "epoll's CLOEXEC value");
65 }
66 #endif
67
68 static void test_alloc(void)
69 {
70 struct lttng_poll_event poll_events;
71
72 lttng_poll_init(&poll_events);
73
74 /* Null pointer */
75 ok(lttng_poll_create(NULL, 1, 0) != 0, "Create over NULL pointer fails");
76 /* Size 0 */
77 ok(lttng_poll_create(&poll_events, 0, 0) != 0, "Create with size 0 fails");
78 /* without CLOEXEC */
79 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create valid poll set succeeds");
80 /*
81 * lttng_poll_event structure untested due to incompatibility across
82 * sublayers. lttng_poll_clean cannot be tested. There is no success
83 * criteria. Verify set's max size cases.
84 */
85 lttng_poll_clean(&poll_events);
86 }
87
88 /* Tests stuff related to what would be handled with epoll_ctl. */
89 static void test_add_del(void)
90 {
91 struct lttng_poll_event poll_events;
92
93 lttng_poll_init(&poll_events);
94 ok(lttng_poll_add(NULL, 1, LPOLLIN) != 0, "Adding to NULL set fails");
95 ok(lttng_poll_add(&poll_events, 1, LPOLLIN) != 0, "Adding to uninitialized structure fails");
96 ok(lttng_poll_add(&poll_events, -1, LPOLLIN) != 0, "Adding invalid FD fails");
97
98 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create a poll set succeeds");
99 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set created empty");
100
101 ok(lttng_poll_add(NULL, 1, LPOLLIN) != 0, "Adding to NULL set fails");
102 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set still empty");
103 ok(lttng_poll_add(&poll_events, -1, LPOLLIN) != 0, "Adding invalid FD fails");
104 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set still empty");
105
106 ok(lttng_poll_add(&poll_events, 1, LPOLLIN) == 0, "Adding valid FD succeeds");
107 ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Nb of elements incremented");
108
109 ok(lttng_poll_del(NULL, 1) != 0, "Removing from NULL set fails");
110 ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Number of FD in set unchanged");
111
112 ok(lttng_poll_del(&poll_events, -1) != 0, "Removing from negative FD fails");
113 ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Number of FD in set unchanged");
114
115 ok(lttng_poll_del(&poll_events, 2) == 0, "Removing invalid FD still succeeds");
116 ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Number of elements unchanged");
117
118 ok(lttng_poll_del(&poll_events, 1) == 0, "Removing valid FD succeeds");
119 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Nb of elements decremented");
120
121 ok(lttng_poll_del(&poll_events, 1) != 0, "Removing from empty set fails");
122 ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Nb of elements unchanged");
123
124 lttng_poll_clean(&poll_events);
125 }
126
127 static void test_mod_wait(void)
128 {
129 struct lttng_poll_event poll_events;
130 struct lttng_poll_event cpoll_events;
131 int hupfd[2];
132 int infd[2];
133 pid_t cpid;
134 char rbuf = 0, tbuf = MAGIC_VALUE;
135 int wstatus;
136
137 lttng_poll_init(&poll_events);
138 lttng_poll_init(&cpoll_events);
139
140 ok(pipe(hupfd) != -1, "pipe function succeeds");
141 ok(pipe(infd) != -1, "pipe function succeeds");
142
143 cpid = fork();
144 if (cpid == 0) {
145 childok(lttng_poll_create(&cpoll_events, 1, 0) == 0, "Create valid poll set succeeds");
146 childok(lttng_poll_mod(NULL, infd[0], LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error");
147 childok(lttng_poll_mod(&cpoll_events, infd[0], LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error");
148 childok(lttng_poll_add(&cpoll_events, infd[0], LPOLLHUP) == 0, "Add valid FD succeeds");
149 childok(lttng_poll_mod(&cpoll_events, -1, LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error");
150 childok(lttng_poll_mod(&cpoll_events, hupfd[0], LPOLLIN) == 0, "lttng_poll_mod on unincluded FD goes on");
151 childok(lttng_poll_mod(&cpoll_events, infd[0], LPOLLIN) == 0, "Modify event type succeeds");
152 childok(close(infd[1]) == 0, "Close valid FD succeeds");
153 childok(lttng_poll_wait(&cpoll_events, -1) == 1, "Wait on close times out");
154 childok(lttng_read(infd[0], &rbuf, 1) == 1, "Data is present in the pipe");
155 childok(rbuf == MAGIC_VALUE, "Received data is consistent with transmitted data");
156 childok(lttng_poll_del(&cpoll_events, infd[0]) == 0, "Removing valid FD succeeds");
157 childok(close(infd[0]) == 0, "Close valid FD succeeds");
158 childok(close(hupfd[0]) == 0, "Close valid FD succeeds");
159 childok(close(hupfd[1]) == 0, "Close valid FD succeeds");
160 lttng_poll_clean(&cpoll_events);
161 _exit(EXIT_SUCCESS);
162 } else {
163 ok(close(hupfd[1]) == 0, "Close valid FD succeeds");
164 ok(close(infd[0]) == 0, "Close valid FD succeeds");
165
166 ok(lttng_poll_wait(NULL, -1) == -1, "lttng_poll_wait call with invalid input returns error");
167
168 ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create valid poll set succeeds");
169 ok(lttng_poll_wait(&poll_events, -1) == -1, "lttng_poll_wait call with invalid input returns error");
170 ok(lttng_poll_add(&poll_events, hupfd[0], LPOLLHUP) == 0, "Add valid FD succeeds");
171 ok(lttng_write(infd[1], &tbuf, 1) == 1, "Write to pipe succeeds");
172 ok(lttng_poll_wait(&poll_events, -1) == 1, "Wakes up on one event");
173 ok(lttng_poll_del(&poll_events, hupfd[0]) == 0, "Removing valid FD succeeds");
174 ok(close(hupfd[0]) == 0, "Close valid FD succeeds");
175 ok(close(infd[1]) == 0, "Close valid FD succeeds");
176 lttng_poll_clean(&poll_events);
177 ok(waitpid(cpid, &wstatus, 0) == cpid, "Wait for child exit");
178 ok(WIFEXITED(wstatus) == 1, "Child process exited");
179 ok(WEXITSTATUS(wstatus) == EXIT_SUCCESS, "Child process exited with EXIT_SUCCESS");
180 }
181 }
182
183 static void test_func_def(void)
184 {
185 #ifdef LTTNG_POLL_GETFD
186 #define PASS_GETFD 1
187 #else
188 #define PASS_GETFD 0
189 #endif
190
191 #ifdef LTTNG_POLL_GETEV
192 #define PASS_GETEV 1
193 #else
194 #define PASS_GETEV 0
195 #endif
196
197 #ifdef LTTNG_POLL_GETSZ
198 #define PASS_GETSZ 1
199 #else
200 #define PASS_GETSZ 0
201 #endif
202
203 #ifdef LTTNG_POLL_GET_PREV_FD
204 #define PASS_GET_PREV_FD 1
205 #else
206 #define PASS_GET_PREV_FD 0
207 #endif
208
209 ok(lttng_poll_reset == lttng_poll_reset, "lttng_poll_reset is defined");
210 ok(lttng_poll_init == lttng_poll_init , "lttng_poll_init is defined");
211 ok(PASS_GETFD, "GETFD is defined");
212 ok(PASS_GETEV, "GETEV is defined");
213 ok(PASS_GETSZ, "GETSZ is defined");
214 ok(PASS_GET_PREV_FD, "GET_PREV_FD is defined");
215 }
216
217 int main(void)
218 {
219 plan_tests(NUM_TESTS);
220 #ifdef HAVE_EPOLL
221 test_epoll_compat();
222 #endif
223 test_func_def();
224 test_alloc();
225 test_add_del();
226 test_mod_wait();
227 return exit_status();
228 }
This page took 0.035069 seconds and 5 git commands to generate.