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