Commit | Line | Data |
---|---|---|
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> | |
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 | ||
41b42b3b JG |
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 | ||
b12e3724 | 45 | #ifdef HAVE_EPOLL |
89226f60 | 46 | #define NUM_TESTS 47 |
b12e3724 | 47 | #else |
89226f60 | 48 | #define NUM_TESTS 46 |
b12e3724 YL |
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 | ||
af8cf559 | 58 | static |
b12e3724 YL |
59 | void test_epoll_compat(void) |
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 | ||
f7f52067 | 69 | static void test_alloc(void) |
b12e3724 YL |
70 | { |
71 | struct lttng_poll_event poll_events; | |
72 | ||
73 | lttng_poll_init(&poll_events); | |
74 | ||
75 | /* Null pointer */ | |
76 | ok(lttng_poll_create(NULL, 1, 0) != 0, "Create over NULL pointer fails"); | |
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. */ | |
f7f52067 | 90 | static void test_add_del(void) |
b12e3724 YL |
91 | { |
92 | struct lttng_poll_event poll_events; | |
93 | ||
94 | lttng_poll_init(&poll_events); | |
95 | ok(lttng_poll_add(NULL, 1, LPOLLIN) != 0, "Adding to NULL set fails"); | |
96 | ok(lttng_poll_add(&poll_events, 1, LPOLLIN) != 0, "Adding to uninitialized structure fails"); | |
97 | ok(lttng_poll_add(&poll_events, -1, LPOLLIN) != 0, "Adding invalid FD fails"); | |
98 | ||
89226f60 | 99 | ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create a poll set succeeds"); |
b12e3724 YL |
100 | ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set created empty"); |
101 | ||
102 | ok(lttng_poll_add(NULL, 1, LPOLLIN) != 0, "Adding to NULL set fails"); | |
103 | ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set still empty"); | |
104 | ok(lttng_poll_add(&poll_events, -1, LPOLLIN) != 0, "Adding invalid FD fails"); | |
105 | ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Set still empty"); | |
106 | ||
107 | ok(lttng_poll_add(&poll_events, 1, LPOLLIN) == 0, "Adding valid FD succeeds"); | |
108 | ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Nb of elements incremented"); | |
109 | ||
110 | ok(lttng_poll_del(NULL, 1) != 0, "Removing from NULL set fails"); | |
111 | ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Number of FD in set unchanged"); | |
112 | ||
113 | ok(lttng_poll_del(&poll_events, -1) != 0, "Removing from negative FD fails"); | |
114 | ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Number of FD in set unchanged"); | |
115 | ||
116 | ok(lttng_poll_del(&poll_events, 2) == 0, "Removing invalid FD still succeeds"); | |
117 | ok(LTTNG_POLL_GETNB(&poll_events) == 1, "Number of elements unchanged"); | |
118 | ||
119 | ok(lttng_poll_del(&poll_events, 1) == 0, "Removing valid FD succeeds"); | |
120 | ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Nb of elements decremented"); | |
121 | ||
122 | ok(lttng_poll_del(&poll_events, 1) != 0, "Removing from empty set fails"); | |
123 | ok(LTTNG_POLL_GETNB(&poll_events) == 0, "Nb of elements unchanged"); | |
124 | ||
125 | lttng_poll_clean(&poll_events); | |
126 | } | |
127 | ||
f7f52067 | 128 | static void test_mod_wait(void) |
b12e3724 YL |
129 | { |
130 | struct lttng_poll_event poll_events; | |
131 | struct lttng_poll_event cpoll_events; | |
132 | int hupfd[2]; | |
133 | int infd[2]; | |
134 | pid_t cpid; | |
135 | char rbuf = 0, tbuf = MAGIC_VALUE; | |
136 | int wstatus; | |
137 | ||
138 | lttng_poll_init(&poll_events); | |
139 | lttng_poll_init(&cpoll_events); | |
140 | ||
141 | ok(pipe(hupfd) != -1, "pipe function succeeds"); | |
142 | ok(pipe(infd) != -1, "pipe function succeeds"); | |
143 | ||
144 | cpid = fork(); | |
145 | if (cpid == 0) { | |
146 | childok(lttng_poll_create(&cpoll_events, 1, 0) == 0, "Create valid poll set succeeds"); | |
147 | childok(lttng_poll_mod(NULL, infd[0], LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error"); | |
148 | childok(lttng_poll_mod(&cpoll_events, infd[0], LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error"); | |
149 | childok(lttng_poll_add(&cpoll_events, infd[0], LPOLLHUP) == 0, "Add valid FD succeeds"); | |
150 | childok(lttng_poll_mod(&cpoll_events, -1, LPOLLIN) == -1, "lttng_poll_mod with invalid input returns an error"); | |
151 | childok(lttng_poll_mod(&cpoll_events, hupfd[0], LPOLLIN) == 0, "lttng_poll_mod on unincluded FD goes on"); | |
152 | childok(lttng_poll_mod(&cpoll_events, infd[0], LPOLLIN) == 0, "Modify event type succeeds"); | |
153 | childok(close(infd[1]) == 0, "Close valid FD succeeds"); | |
154 | childok(lttng_poll_wait(&cpoll_events, -1) == 1, "Wait on close times out"); | |
155 | childok(lttng_read(infd[0], &rbuf, 1) == 1, "Data is present in the pipe"); | |
156 | childok(rbuf == MAGIC_VALUE, "Received data is consistent with transmitted data"); | |
157 | childok(lttng_poll_del(&cpoll_events, infd[0]) == 0, "Removing valid FD succeeds"); | |
158 | childok(close(infd[0]) == 0, "Close valid FD succeeds"); | |
159 | childok(close(hupfd[0]) == 0, "Close valid FD succeeds"); | |
160 | childok(close(hupfd[1]) == 0, "Close valid FD succeeds"); | |
161 | lttng_poll_clean(&cpoll_events); | |
162 | _exit(EXIT_SUCCESS); | |
163 | } else { | |
164 | ok(close(hupfd[1]) == 0, "Close valid FD succeeds"); | |
165 | ok(close(infd[0]) == 0, "Close valid FD succeeds"); | |
166 | ||
167 | ok(lttng_poll_wait(NULL, -1) == -1, "lttng_poll_wait call with invalid input returns error"); | |
168 | ||
169 | ok(lttng_poll_create(&poll_events, 1, 0) == 0, "Create valid poll set succeeds"); | |
170 | ok(lttng_poll_wait(&poll_events, -1) == -1, "lttng_poll_wait call with invalid input returns error"); | |
171 | ok(lttng_poll_add(&poll_events, hupfd[0], LPOLLHUP) == 0, "Add valid FD succeeds"); | |
172 | ok(lttng_write(infd[1], &tbuf, 1) == 1, "Write to pipe succeeds"); | |
173 | ok(lttng_poll_wait(&poll_events, -1) == 1, "Wakes up on one event"); | |
174 | ok(lttng_poll_del(&poll_events, hupfd[0]) == 0, "Removing valid FD succeeds"); | |
175 | ok(close(hupfd[0]) == 0, "Close valid FD succeeds"); | |
176 | ok(close(infd[1]) == 0, "Close valid FD succeeds"); | |
177 | lttng_poll_clean(&poll_events); | |
178 | ok(waitpid(cpid, &wstatus, 0) == cpid, "Wait for child exit"); | |
179 | ok(WIFEXITED(wstatus) == 1, "Child process exited"); | |
180 | ok(WEXITSTATUS(wstatus) == EXIT_SUCCESS, "Child process exited with EXIT_SUCCESS"); | |
181 | } | |
182 | } | |
183 | ||
f7f52067 | 184 | static void test_func_def(void) |
b12e3724 YL |
185 | { |
186 | #ifdef LTTNG_POLL_GETFD | |
187 | #define PASS_GETFD 1 | |
188 | #else | |
189 | #define PASS_GETFD 0 | |
190 | #endif | |
191 | ||
192 | #ifdef LTTNG_POLL_GETEV | |
193 | #define PASS_GETEV 1 | |
194 | #else | |
195 | #define PASS_GETEV 0 | |
196 | #endif | |
197 | ||
198 | #ifdef LTTNG_POLL_GETSZ | |
199 | #define PASS_GETSZ 1 | |
200 | #else | |
201 | #define PASS_GETSZ 0 | |
202 | #endif | |
203 | ||
204 | #ifdef LTTNG_POLL_GET_PREV_FD | |
205 | #define PASS_GET_PREV_FD 1 | |
206 | #else | |
207 | #define PASS_GET_PREV_FD 0 | |
208 | #endif | |
209 | ||
210 | ok(lttng_poll_reset == lttng_poll_reset, "lttng_poll_reset is defined"); | |
211 | ok(lttng_poll_init == lttng_poll_init , "lttng_poll_init is defined"); | |
212 | ok(PASS_GETFD, "GETFD is defined"); | |
213 | ok(PASS_GETEV, "GETEV is defined"); | |
214 | ok(PASS_GETSZ, "GETSZ is defined"); | |
215 | ok(PASS_GET_PREV_FD, "GET_PREV_FD is defined"); | |
216 | } | |
217 | ||
218 | int main(void) | |
219 | { | |
220 | plan_tests(NUM_TESTS); | |
221 | #ifdef HAVE_EPOLL | |
222 | test_epoll_compat(); | |
223 | #endif | |
224 | test_func_def(); | |
225 | test_alloc(); | |
226 | test_add_del(); | |
227 | test_mod_wait(); | |
228 | return exit_status(); | |
229 | } |