]>
Commit | Line | Data |
---|---|---|
5705e050 | 1 | /* |
83cf7abf | 2 | * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. |
5705e050 | 3 | * |
909f1a2e | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
440e5d80 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
5705e050 MC |
8 | */ |
9 | ||
f1f5ee17 AP |
10 | #ifdef _WIN32 |
11 | # include <windows.h> | |
12 | #endif | |
13 | ||
5705e050 MC |
14 | #include <stdio.h> |
15 | #include <string.h> | |
16 | #include <openssl/async.h> | |
17 | #include <openssl/crypto.h> | |
5705e050 | 18 | |
5705e050 MC |
19 | static int ctr = 0; |
20 | static ASYNC_JOB *currjob = NULL; | |
21 | ||
22 | static int only_pause(void *args) | |
23 | { | |
24 | ASYNC_pause_job(); | |
25 | ||
26 | return 1; | |
27 | } | |
28 | ||
29 | static int add_two(void *args) | |
30 | { | |
31 | ctr++; | |
32 | ASYNC_pause_job(); | |
33 | ctr++; | |
34 | ||
35 | return 2; | |
36 | } | |
37 | ||
38 | static int save_current(void *args) | |
39 | { | |
40 | currjob = ASYNC_get_current_job(); | |
41 | ASYNC_pause_job(); | |
42 | ||
43 | return 1; | |
44 | } | |
45 | ||
ff75a257 MC |
46 | #define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99) |
47 | static int waitfd(void *args) | |
5705e050 | 48 | { |
ff75a257 MC |
49 | ASYNC_JOB *job; |
50 | ASYNC_WAIT_CTX *waitctx; | |
ff75a257 MC |
51 | job = ASYNC_get_current_job(); |
52 | if (job == NULL) | |
53 | return 0; | |
54 | waitctx = ASYNC_get_wait_ctx(job); | |
55 | if (waitctx == NULL) | |
56 | return 0; | |
f44e6364 AG |
57 | |
58 | /* First case: no fd added or removed */ | |
59 | ASYNC_pause_job(); | |
60 | ||
61 | /* Second case: one fd added */ | |
e8aa8b6c | 62 | if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL)) |
ff75a257 | 63 | return 0; |
5705e050 | 64 | ASYNC_pause_job(); |
ff75a257 | 65 | |
f44e6364 AG |
66 | /* Third case: all fd removed */ |
67 | if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx)) | |
68 | return 0; | |
69 | ASYNC_pause_job(); | |
70 | ||
71 | /* Last case: fd added and immediately removed */ | |
72 | if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL)) | |
73 | return 0; | |
ff75a257 MC |
74 | if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx)) |
75 | return 0; | |
5705e050 MC |
76 | |
77 | return 1; | |
78 | } | |
79 | ||
e8dfb5bf MC |
80 | static int blockpause(void *args) |
81 | { | |
82 | ASYNC_block_pause(); | |
83 | ASYNC_pause_job(); | |
84 | ASYNC_unblock_pause(); | |
85 | ASYNC_pause_job(); | |
86 | ||
87 | return 1; | |
88 | } | |
89 | ||
3cb7c5cf | 90 | static int test_ASYNC_init_thread(void) |
5705e050 MC |
91 | { |
92 | ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL; | |
93 | int funcret1, funcret2, funcret3; | |
174a74ef | 94 | ASYNC_WAIT_CTX *waitctx = NULL; |
5705e050 | 95 | |
7b9f8f7f | 96 | if ( !ASYNC_init_thread(2, 0) |
ff75a257 MC |
97 | || (waitctx = ASYNC_WAIT_CTX_new()) == NULL |
98 | || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0) | |
5705e050 | 99 | != ASYNC_PAUSE |
ff75a257 | 100 | || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0) |
5705e050 | 101 | != ASYNC_PAUSE |
ff75a257 | 102 | || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0) |
5705e050 | 103 | != ASYNC_NO_JOBS |
ff75a257 | 104 | || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0) |
5705e050 | 105 | != ASYNC_FINISH |
ff75a257 | 106 | || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0) |
5705e050 | 107 | != ASYNC_PAUSE |
ff75a257 | 108 | || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0) |
5705e050 | 109 | != ASYNC_FINISH |
ff75a257 | 110 | || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0) |
5705e050 MC |
111 | != ASYNC_FINISH |
112 | || funcret1 != 1 | |
113 | || funcret2 != 1 | |
114 | || funcret3 != 1) { | |
7b9f8f7f | 115 | fprintf(stderr, "test_ASYNC_init_thread() failed\n"); |
ff75a257 | 116 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 117 | ASYNC_cleanup_thread(); |
5705e050 MC |
118 | return 0; |
119 | } | |
120 | ||
ff75a257 | 121 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 122 | ASYNC_cleanup_thread(); |
5705e050 MC |
123 | return 1; |
124 | } | |
125 | ||
9f5a87fd PY |
126 | static int test_callback(void *arg) |
127 | { | |
128 | printf("callback test pass\n"); | |
129 | return 1; | |
130 | } | |
131 | ||
132 | static int test_ASYNC_callback_status(void) | |
133 | { | |
134 | ASYNC_WAIT_CTX *waitctx = NULL; | |
135 | int set_arg = 100; | |
136 | ASYNC_callback_fn get_callback; | |
137 | void *get_arg; | |
138 | int set_status = 1; | |
139 | ||
140 | if ( !ASYNC_init_thread(1, 0) | |
141 | || (waitctx = ASYNC_WAIT_CTX_new()) == NULL | |
142 | || ASYNC_WAIT_CTX_set_callback(waitctx, test_callback, (void*)&set_arg) | |
143 | != 1 | |
144 | || ASYNC_WAIT_CTX_get_callback(waitctx, &get_callback, &get_arg) | |
145 | != 1 | |
146 | || test_callback != get_callback | |
147 | || get_arg != (void*)&set_arg | |
148 | || (*get_callback)(get_arg) != 1 | |
149 | || ASYNC_WAIT_CTX_set_status(waitctx, set_status) != 1 | |
150 | || set_status != ASYNC_WAIT_CTX_get_status(waitctx)) { | |
151 | fprintf(stderr, "test_ASYNC_callback_status() failed\n"); | |
152 | ASYNC_WAIT_CTX_free(waitctx); | |
153 | ASYNC_cleanup_thread(); | |
154 | return 0; | |
155 | } | |
156 | ||
157 | ASYNC_WAIT_CTX_free(waitctx); | |
158 | ASYNC_cleanup_thread(); | |
159 | return 1; | |
160 | ||
161 | } | |
162 | ||
3cb7c5cf | 163 | static int test_ASYNC_start_job(void) |
5705e050 MC |
164 | { |
165 | ASYNC_JOB *job = NULL; | |
166 | int funcret; | |
174a74ef | 167 | ASYNC_WAIT_CTX *waitctx = NULL; |
5705e050 MC |
168 | |
169 | ctr = 0; | |
170 | ||
7b9f8f7f | 171 | if ( !ASYNC_init_thread(1, 0) |
ff75a257 MC |
172 | || (waitctx = ASYNC_WAIT_CTX_new()) == NULL |
173 | || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0) | |
174 | != ASYNC_PAUSE | |
5705e050 | 175 | || ctr != 1 |
ff75a257 MC |
176 | || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0) |
177 | != ASYNC_FINISH | |
5705e050 MC |
178 | || ctr != 2 |
179 | || funcret != 2) { | |
180 | fprintf(stderr, "test_ASYNC_start_job() failed\n"); | |
ff75a257 | 181 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 182 | ASYNC_cleanup_thread(); |
5705e050 MC |
183 | return 0; |
184 | } | |
185 | ||
ff75a257 | 186 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 187 | ASYNC_cleanup_thread(); |
5705e050 MC |
188 | return 1; |
189 | } | |
190 | ||
3cb7c5cf | 191 | static int test_ASYNC_get_current_job(void) |
5705e050 MC |
192 | { |
193 | ASYNC_JOB *job = NULL; | |
194 | int funcret; | |
174a74ef | 195 | ASYNC_WAIT_CTX *waitctx = NULL; |
5705e050 MC |
196 | |
197 | currjob = NULL; | |
198 | ||
7b9f8f7f | 199 | if ( !ASYNC_init_thread(1, 0) |
ff75a257 MC |
200 | || (waitctx = ASYNC_WAIT_CTX_new()) == NULL |
201 | || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0) | |
5705e050 MC |
202 | != ASYNC_PAUSE |
203 | || currjob != job | |
ff75a257 | 204 | || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0) |
5705e050 MC |
205 | != ASYNC_FINISH |
206 | || funcret != 1) { | |
207 | fprintf(stderr, "test_ASYNC_get_current_job() failed\n"); | |
ff75a257 | 208 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 209 | ASYNC_cleanup_thread(); |
5705e050 MC |
210 | return 0; |
211 | } | |
212 | ||
ff75a257 | 213 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 214 | ASYNC_cleanup_thread(); |
5705e050 MC |
215 | return 1; |
216 | } | |
217 | ||
3cb7c5cf | 218 | static int test_ASYNC_WAIT_CTX_get_all_fds(void) |
5705e050 MC |
219 | { |
220 | ASYNC_JOB *job = NULL; | |
2b2c78d4 | 221 | int funcret; |
174a74ef | 222 | ASYNC_WAIT_CTX *waitctx = NULL; |
ff75a257 MC |
223 | OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD; |
224 | size_t numfds, numdelfds; | |
5705e050 | 225 | |
7b9f8f7f | 226 | if ( !ASYNC_init_thread(1, 0) |
ff75a257 MC |
227 | || (waitctx = ASYNC_WAIT_CTX_new()) == NULL |
228 | /* On first run we're not expecting any wait fds */ | |
229 | || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0) | |
5705e050 | 230 | != ASYNC_PAUSE |
ff75a257 MC |
231 | || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds) |
232 | || numfds != 0 | |
233 | || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL, | |
234 | &numdelfds) | |
235 | || numfds != 0 | |
236 | || numdelfds != 0 | |
237 | /* On second run we're expecting one added fd */ | |
238 | || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0) | |
5705e050 | 239 | != ASYNC_PAUSE |
ff75a257 MC |
240 | || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds) |
241 | || numfds != 1 | |
242 | || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds) | |
243 | || fd != MAGIC_WAIT_FD | |
244 | || (fd = OSSL_BAD_ASYNC_FD, 0) /* Assign to something else */ | |
245 | || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL, | |
f44e6364 | 246 | &numdelfds) |
ff75a257 MC |
247 | || numfds != 1 |
248 | || numdelfds != 0 | |
249 | || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL, | |
250 | &numdelfds) | |
251 | || fd != MAGIC_WAIT_FD | |
f44e6364 | 252 | /* On third run we expect one deleted fd */ |
ff75a257 | 253 | || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0) |
f44e6364 | 254 | != ASYNC_PAUSE |
ff75a257 MC |
255 | || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds) |
256 | || numfds != 0 | |
257 | || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL, | |
258 | &numdelfds) | |
259 | || numfds != 0 | |
260 | || numdelfds != 1 | |
261 | || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd, | |
262 | &numdelfds) | |
263 | || delfd != MAGIC_WAIT_FD | |
f44e6364 AG |
264 | /* On last run we are not expecting any wait fd */ |
265 | || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0) | |
266 | != ASYNC_FINISH | |
267 | || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds) | |
268 | || numfds != 0 | |
269 | || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL, | |
270 | &numdelfds) | |
271 | || numfds != 0 | |
272 | || numdelfds != 0 | |
5705e050 MC |
273 | || funcret != 1) { |
274 | fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n"); | |
ff75a257 | 275 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 276 | ASYNC_cleanup_thread(); |
5705e050 MC |
277 | return 0; |
278 | } | |
279 | ||
ff75a257 | 280 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 281 | ASYNC_cleanup_thread(); |
5705e050 MC |
282 | return 1; |
283 | } | |
e8dfb5bf | 284 | |
3cb7c5cf | 285 | static int test_ASYNC_block_pause(void) |
e8dfb5bf MC |
286 | { |
287 | ASYNC_JOB *job = NULL; | |
288 | int funcret; | |
174a74ef | 289 | ASYNC_WAIT_CTX *waitctx = NULL; |
e8dfb5bf | 290 | |
7b9f8f7f | 291 | if ( !ASYNC_init_thread(1, 0) |
ff75a257 MC |
292 | || (waitctx = ASYNC_WAIT_CTX_new()) == NULL |
293 | || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0) | |
e8dfb5bf | 294 | != ASYNC_PAUSE |
ff75a257 | 295 | || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0) |
e8dfb5bf MC |
296 | != ASYNC_FINISH |
297 | || funcret != 1) { | |
298 | fprintf(stderr, "test_ASYNC_block_pause() failed\n"); | |
ff75a257 | 299 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 300 | ASYNC_cleanup_thread(); |
e8dfb5bf MC |
301 | return 0; |
302 | } | |
303 | ||
ff75a257 | 304 | ASYNC_WAIT_CTX_free(waitctx); |
7b9f8f7f | 305 | ASYNC_cleanup_thread(); |
e8dfb5bf MC |
306 | return 1; |
307 | } | |
308 | ||
5705e050 MC |
309 | int main(int argc, char **argv) |
310 | { | |
c521edc3 MC |
311 | if (!ASYNC_is_capable()) { |
312 | fprintf(stderr, | |
313 | "OpenSSL build is not ASYNC capable - skipping async tests\n"); | |
314 | } else { | |
315 | CRYPTO_set_mem_debug(1); | |
316 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | |
317 | ||
318 | if ( !test_ASYNC_init_thread() | |
9f5a87fd | 319 | || !test_ASYNC_callback_status() |
c521edc3 MC |
320 | || !test_ASYNC_start_job() |
321 | || !test_ASYNC_get_current_job() | |
322 | || !test_ASYNC_WAIT_CTX_get_all_fds() | |
323 | || !test_ASYNC_block_pause()) { | |
324 | return 1; | |
325 | } | |
5705e050 | 326 | } |
5705e050 MC |
327 | printf("PASS\n"); |
328 | return 0; | |
329 | } |