2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
16 #include <openssl/async.h>
17 #include <openssl/crypto.h>
20 static ASYNC_JOB *currjob = NULL;
21 static int custom_alloc_used = 0;
22 static int custom_free_used = 0;
24 static int only_pause(void *args)
31 static int add_two(void *args)
40 static int save_current(void *args)
42 currjob = ASYNC_get_current_job();
48 static int change_deflt_libctx(void *args)
50 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
51 OSSL_LIB_CTX *oldctx, *tmpctx;
57 oldctx = OSSL_LIB_CTX_set0_default(libctx);
60 /* Check the libctx is set up as we expect */
61 tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
65 /* Set it back again to continue to use our own libctx */
66 oldctx = OSSL_LIB_CTX_set0_default(libctx);
69 /* Check the libctx is set up as we expect */
70 tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
76 OSSL_LIB_CTX_free(libctx);
81 #define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
82 static int waitfd(void *args)
85 ASYNC_WAIT_CTX *waitctx;
86 job = ASYNC_get_current_job();
89 waitctx = ASYNC_get_wait_ctx(job);
93 /* First case: no fd added or removed */
96 /* Second case: one fd added */
97 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
101 /* Third case: all fd removed */
102 if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
106 /* Last case: fd added and immediately removed */
107 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
109 if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
115 static int blockpause(void *args)
119 ASYNC_unblock_pause();
125 static int test_ASYNC_init_thread(void)
127 ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL;
128 int funcret1, funcret2, funcret3;
129 ASYNC_WAIT_CTX *waitctx = NULL;
131 if ( !ASYNC_init_thread(2, 0)
132 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
133 || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
135 || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
137 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
139 || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
141 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
143 || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
145 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
150 fprintf(stderr, "test_ASYNC_init_thread() failed\n");
151 ASYNC_WAIT_CTX_free(waitctx);
152 ASYNC_cleanup_thread();
156 ASYNC_WAIT_CTX_free(waitctx);
157 ASYNC_cleanup_thread();
161 static int test_callback(void *arg)
163 printf("callback test pass\n");
167 static int test_ASYNC_callback_status(void)
169 ASYNC_WAIT_CTX *waitctx = NULL;
171 ASYNC_callback_fn get_callback;
175 if ( !ASYNC_init_thread(1, 0)
176 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
177 || ASYNC_WAIT_CTX_set_callback(waitctx, test_callback, (void*)&set_arg)
179 || ASYNC_WAIT_CTX_get_callback(waitctx, &get_callback, &get_arg)
181 || test_callback != get_callback
182 || get_arg != (void*)&set_arg
183 || (*get_callback)(get_arg) != 1
184 || ASYNC_WAIT_CTX_set_status(waitctx, set_status) != 1
185 || set_status != ASYNC_WAIT_CTX_get_status(waitctx)) {
186 fprintf(stderr, "test_ASYNC_callback_status() failed\n");
187 ASYNC_WAIT_CTX_free(waitctx);
188 ASYNC_cleanup_thread();
192 ASYNC_WAIT_CTX_free(waitctx);
193 ASYNC_cleanup_thread();
198 static int test_ASYNC_start_job(void)
200 ASYNC_JOB *job = NULL;
202 ASYNC_WAIT_CTX *waitctx = NULL;
206 if ( !ASYNC_init_thread(1, 0)
207 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
208 || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
211 || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
215 fprintf(stderr, "test_ASYNC_start_job() failed\n");
216 ASYNC_WAIT_CTX_free(waitctx);
217 ASYNC_cleanup_thread();
221 ASYNC_WAIT_CTX_free(waitctx);
222 ASYNC_cleanup_thread();
226 static int test_ASYNC_get_current_job(void)
228 ASYNC_JOB *job = NULL;
230 ASYNC_WAIT_CTX *waitctx = NULL;
234 if ( !ASYNC_init_thread(1, 0)
235 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
236 || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
239 || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
242 fprintf(stderr, "test_ASYNC_get_current_job() failed\n");
243 ASYNC_WAIT_CTX_free(waitctx);
244 ASYNC_cleanup_thread();
248 ASYNC_WAIT_CTX_free(waitctx);
249 ASYNC_cleanup_thread();
253 static int test_ASYNC_WAIT_CTX_get_all_fds(void)
255 ASYNC_JOB *job = NULL;
257 ASYNC_WAIT_CTX *waitctx = NULL;
258 OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD;
259 size_t numfds, numdelfds;
261 if ( !ASYNC_init_thread(1, 0)
262 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
263 /* On first run we're not expecting any wait fds */
264 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
266 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
268 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
272 /* On second run we're expecting one added fd */
273 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
275 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
277 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds)
278 || fd != MAGIC_WAIT_FD
279 || (fd = OSSL_BAD_ASYNC_FD, 0) /* Assign to something else */
280 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
284 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL,
286 || fd != MAGIC_WAIT_FD
287 /* On third run we expect one deleted fd */
288 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
290 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
292 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
296 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd,
298 || delfd != MAGIC_WAIT_FD
299 /* On last run we are not expecting any wait fd */
300 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
302 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
304 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
309 fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n");
310 ASYNC_WAIT_CTX_free(waitctx);
311 ASYNC_cleanup_thread();
315 ASYNC_WAIT_CTX_free(waitctx);
316 ASYNC_cleanup_thread();
320 static int test_ASYNC_block_pause(void)
322 ASYNC_JOB *job = NULL;
324 ASYNC_WAIT_CTX *waitctx = NULL;
326 if ( !ASYNC_init_thread(1, 0)
327 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
328 || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
330 || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
333 fprintf(stderr, "test_ASYNC_block_pause() failed\n");
334 ASYNC_WAIT_CTX_free(waitctx);
335 ASYNC_cleanup_thread();
339 ASYNC_WAIT_CTX_free(waitctx);
340 ASYNC_cleanup_thread();
344 static int test_ASYNC_start_job_ex(void)
346 ASYNC_JOB *job = NULL;
348 ASYNC_WAIT_CTX *waitctx = NULL;
349 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
350 OSSL_LIB_CTX *oldctx, *tmpctx, *globalctx;
353 if (libctx == NULL) {
355 "test_ASYNC_start_job_ex() failed to create libctx\n");
359 globalctx = oldctx = OSSL_LIB_CTX_set0_default(libctx);
361 if ((waitctx = ASYNC_WAIT_CTX_new()) == NULL
362 || ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx,
366 "test_ASYNC_start_job_ex() failed to start job\n");
370 /* Reset the libctx temporarily to find out what it is*/
371 tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
372 oldctx = OSSL_LIB_CTX_set0_default(tmpctx);
373 if (tmpctx != libctx) {
375 "test_ASYNC_start_job_ex() failed - unexpected libctx\n");
379 if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
382 "test_ASYNC_start_job_ex() - restarting job failed\n");
386 /* Reset the libctx and continue with the global default libctx */
387 tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
388 if (tmpctx != libctx) {
390 "test_ASYNC_start_job_ex() failed - unexpected libctx\n");
394 if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
398 "test_ASYNC_start_job_ex() - finishing job failed\n");
402 /* Reset the libctx temporarily to find out what it is*/
403 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
404 OSSL_LIB_CTX_set0_default(tmpctx);
405 if (tmpctx != globalctx) {
407 "test_ASYNC_start_job_ex() failed - global libctx check failed\n");
413 ASYNC_WAIT_CTX_free(waitctx);
414 ASYNC_cleanup_thread();
415 OSSL_LIB_CTX_free(libctx);
419 static void *test_alloc_stack(size_t *num)
421 custom_alloc_used = 1;
422 return OPENSSL_malloc(*num);
425 static void test_free_stack(void *addr)
427 custom_free_used = 1;
431 static int test_ASYNC_set_mem_functions(void)
433 ASYNC_stack_alloc_fn alloc_fn;
434 ASYNC_stack_free_fn free_fn;
436 /* Not all platforms support this */
437 if (ASYNC_set_mem_functions(test_alloc_stack, test_free_stack) == 0) return 1;
439 ASYNC_get_mem_functions(&alloc_fn, &free_fn);
441 if ((alloc_fn != test_alloc_stack) || (free_fn != test_free_stack)) {
443 "test_ASYNC_set_mem_functions() - setting and retrieving custom allocators failed\n");
447 if (!ASYNC_init_thread(1, 1)) {
449 "test_ASYNC_set_mem_functions() - failed initialising ctx pool\n");
452 ASYNC_cleanup_thread();
454 if (!custom_alloc_used || !custom_free_used) {
456 "test_ASYNC_set_mem_functions() - custom allocation functions not used\n");
464 int main(int argc, char **argv)
466 if (!ASYNC_is_capable()) {
468 "OpenSSL build is not ASYNC capable - skipping async tests\n");
470 if (!test_ASYNC_init_thread()
471 || !test_ASYNC_callback_status()
472 || !test_ASYNC_start_job()
473 || !test_ASYNC_get_current_job()
474 || !test_ASYNC_WAIT_CTX_get_all_fds()
475 || !test_ASYNC_block_pause()
476 || !test_ASYNC_start_job_ex()
477 || !test_ASYNC_set_mem_functions()) {