1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #ifdef _WIN32
11*e0c4386eSCy Schubert # include <windows.h>
12*e0c4386eSCy Schubert #endif
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubert #include <stdio.h>
15*e0c4386eSCy Schubert #include <string.h>
16*e0c4386eSCy Schubert #include <openssl/async.h>
17*e0c4386eSCy Schubert #include <openssl/crypto.h>
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubert static int ctr = 0;
20*e0c4386eSCy Schubert static ASYNC_JOB *currjob = NULL;
21*e0c4386eSCy Schubert
only_pause(void * args)22*e0c4386eSCy Schubert static int only_pause(void *args)
23*e0c4386eSCy Schubert {
24*e0c4386eSCy Schubert ASYNC_pause_job();
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubert return 1;
27*e0c4386eSCy Schubert }
28*e0c4386eSCy Schubert
add_two(void * args)29*e0c4386eSCy Schubert static int add_two(void *args)
30*e0c4386eSCy Schubert {
31*e0c4386eSCy Schubert ctr++;
32*e0c4386eSCy Schubert ASYNC_pause_job();
33*e0c4386eSCy Schubert ctr++;
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert return 2;
36*e0c4386eSCy Schubert }
37*e0c4386eSCy Schubert
save_current(void * args)38*e0c4386eSCy Schubert static int save_current(void *args)
39*e0c4386eSCy Schubert {
40*e0c4386eSCy Schubert currjob = ASYNC_get_current_job();
41*e0c4386eSCy Schubert ASYNC_pause_job();
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert return 1;
44*e0c4386eSCy Schubert }
45*e0c4386eSCy Schubert
change_deflt_libctx(void * args)46*e0c4386eSCy Schubert static int change_deflt_libctx(void *args)
47*e0c4386eSCy Schubert {
48*e0c4386eSCy Schubert OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
49*e0c4386eSCy Schubert OSSL_LIB_CTX *oldctx, *tmpctx;
50*e0c4386eSCy Schubert int ret = 0;
51*e0c4386eSCy Schubert
52*e0c4386eSCy Schubert if (libctx == NULL)
53*e0c4386eSCy Schubert return 0;
54*e0c4386eSCy Schubert
55*e0c4386eSCy Schubert oldctx = OSSL_LIB_CTX_set0_default(libctx);
56*e0c4386eSCy Schubert ASYNC_pause_job();
57*e0c4386eSCy Schubert
58*e0c4386eSCy Schubert /* Check the libctx is set up as we expect */
59*e0c4386eSCy Schubert tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
60*e0c4386eSCy Schubert if (tmpctx != libctx)
61*e0c4386eSCy Schubert goto err;
62*e0c4386eSCy Schubert
63*e0c4386eSCy Schubert /* Set it back again to continue to use our own libctx */
64*e0c4386eSCy Schubert oldctx = OSSL_LIB_CTX_set0_default(libctx);
65*e0c4386eSCy Schubert ASYNC_pause_job();
66*e0c4386eSCy Schubert
67*e0c4386eSCy Schubert /* Check the libctx is set up as we expect */
68*e0c4386eSCy Schubert tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
69*e0c4386eSCy Schubert if (tmpctx != libctx)
70*e0c4386eSCy Schubert goto err;
71*e0c4386eSCy Schubert
72*e0c4386eSCy Schubert ret = 1;
73*e0c4386eSCy Schubert err:
74*e0c4386eSCy Schubert OSSL_LIB_CTX_free(libctx);
75*e0c4386eSCy Schubert return ret;
76*e0c4386eSCy Schubert }
77*e0c4386eSCy Schubert
78*e0c4386eSCy Schubert
79*e0c4386eSCy Schubert #define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
waitfd(void * args)80*e0c4386eSCy Schubert static int waitfd(void *args)
81*e0c4386eSCy Schubert {
82*e0c4386eSCy Schubert ASYNC_JOB *job;
83*e0c4386eSCy Schubert ASYNC_WAIT_CTX *waitctx;
84*e0c4386eSCy Schubert job = ASYNC_get_current_job();
85*e0c4386eSCy Schubert if (job == NULL)
86*e0c4386eSCy Schubert return 0;
87*e0c4386eSCy Schubert waitctx = ASYNC_get_wait_ctx(job);
88*e0c4386eSCy Schubert if (waitctx == NULL)
89*e0c4386eSCy Schubert return 0;
90*e0c4386eSCy Schubert
91*e0c4386eSCy Schubert /* First case: no fd added or removed */
92*e0c4386eSCy Schubert ASYNC_pause_job();
93*e0c4386eSCy Schubert
94*e0c4386eSCy Schubert /* Second case: one fd added */
95*e0c4386eSCy Schubert if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
96*e0c4386eSCy Schubert return 0;
97*e0c4386eSCy Schubert ASYNC_pause_job();
98*e0c4386eSCy Schubert
99*e0c4386eSCy Schubert /* Third case: all fd removed */
100*e0c4386eSCy Schubert if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
101*e0c4386eSCy Schubert return 0;
102*e0c4386eSCy Schubert ASYNC_pause_job();
103*e0c4386eSCy Schubert
104*e0c4386eSCy Schubert /* Last case: fd added and immediately removed */
105*e0c4386eSCy Schubert if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
106*e0c4386eSCy Schubert return 0;
107*e0c4386eSCy Schubert if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
108*e0c4386eSCy Schubert return 0;
109*e0c4386eSCy Schubert
110*e0c4386eSCy Schubert return 1;
111*e0c4386eSCy Schubert }
112*e0c4386eSCy Schubert
blockpause(void * args)113*e0c4386eSCy Schubert static int blockpause(void *args)
114*e0c4386eSCy Schubert {
115*e0c4386eSCy Schubert ASYNC_block_pause();
116*e0c4386eSCy Schubert ASYNC_pause_job();
117*e0c4386eSCy Schubert ASYNC_unblock_pause();
118*e0c4386eSCy Schubert ASYNC_pause_job();
119*e0c4386eSCy Schubert
120*e0c4386eSCy Schubert return 1;
121*e0c4386eSCy Schubert }
122*e0c4386eSCy Schubert
test_ASYNC_init_thread(void)123*e0c4386eSCy Schubert static int test_ASYNC_init_thread(void)
124*e0c4386eSCy Schubert {
125*e0c4386eSCy Schubert ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL;
126*e0c4386eSCy Schubert int funcret1, funcret2, funcret3;
127*e0c4386eSCy Schubert ASYNC_WAIT_CTX *waitctx = NULL;
128*e0c4386eSCy Schubert
129*e0c4386eSCy Schubert if ( !ASYNC_init_thread(2, 0)
130*e0c4386eSCy Schubert || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
131*e0c4386eSCy Schubert || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
132*e0c4386eSCy Schubert != ASYNC_PAUSE
133*e0c4386eSCy Schubert || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
134*e0c4386eSCy Schubert != ASYNC_PAUSE
135*e0c4386eSCy Schubert || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
136*e0c4386eSCy Schubert != ASYNC_NO_JOBS
137*e0c4386eSCy Schubert || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
138*e0c4386eSCy Schubert != ASYNC_FINISH
139*e0c4386eSCy Schubert || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
140*e0c4386eSCy Schubert != ASYNC_PAUSE
141*e0c4386eSCy Schubert || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
142*e0c4386eSCy Schubert != ASYNC_FINISH
143*e0c4386eSCy Schubert || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
144*e0c4386eSCy Schubert != ASYNC_FINISH
145*e0c4386eSCy Schubert || funcret1 != 1
146*e0c4386eSCy Schubert || funcret2 != 1
147*e0c4386eSCy Schubert || funcret3 != 1) {
148*e0c4386eSCy Schubert fprintf(stderr, "test_ASYNC_init_thread() failed\n");
149*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
150*e0c4386eSCy Schubert ASYNC_cleanup_thread();
151*e0c4386eSCy Schubert return 0;
152*e0c4386eSCy Schubert }
153*e0c4386eSCy Schubert
154*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
155*e0c4386eSCy Schubert ASYNC_cleanup_thread();
156*e0c4386eSCy Schubert return 1;
157*e0c4386eSCy Schubert }
158*e0c4386eSCy Schubert
test_callback(void * arg)159*e0c4386eSCy Schubert static int test_callback(void *arg)
160*e0c4386eSCy Schubert {
161*e0c4386eSCy Schubert printf("callback test pass\n");
162*e0c4386eSCy Schubert return 1;
163*e0c4386eSCy Schubert }
164*e0c4386eSCy Schubert
test_ASYNC_callback_status(void)165*e0c4386eSCy Schubert static int test_ASYNC_callback_status(void)
166*e0c4386eSCy Schubert {
167*e0c4386eSCy Schubert ASYNC_WAIT_CTX *waitctx = NULL;
168*e0c4386eSCy Schubert int set_arg = 100;
169*e0c4386eSCy Schubert ASYNC_callback_fn get_callback;
170*e0c4386eSCy Schubert void *get_arg;
171*e0c4386eSCy Schubert int set_status = 1;
172*e0c4386eSCy Schubert
173*e0c4386eSCy Schubert if ( !ASYNC_init_thread(1, 0)
174*e0c4386eSCy Schubert || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
175*e0c4386eSCy Schubert || ASYNC_WAIT_CTX_set_callback(waitctx, test_callback, (void*)&set_arg)
176*e0c4386eSCy Schubert != 1
177*e0c4386eSCy Schubert || ASYNC_WAIT_CTX_get_callback(waitctx, &get_callback, &get_arg)
178*e0c4386eSCy Schubert != 1
179*e0c4386eSCy Schubert || test_callback != get_callback
180*e0c4386eSCy Schubert || get_arg != (void*)&set_arg
181*e0c4386eSCy Schubert || (*get_callback)(get_arg) != 1
182*e0c4386eSCy Schubert || ASYNC_WAIT_CTX_set_status(waitctx, set_status) != 1
183*e0c4386eSCy Schubert || set_status != ASYNC_WAIT_CTX_get_status(waitctx)) {
184*e0c4386eSCy Schubert fprintf(stderr, "test_ASYNC_callback_status() failed\n");
185*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
186*e0c4386eSCy Schubert ASYNC_cleanup_thread();
187*e0c4386eSCy Schubert return 0;
188*e0c4386eSCy Schubert }
189*e0c4386eSCy Schubert
190*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
191*e0c4386eSCy Schubert ASYNC_cleanup_thread();
192*e0c4386eSCy Schubert return 1;
193*e0c4386eSCy Schubert
194*e0c4386eSCy Schubert }
195*e0c4386eSCy Schubert
test_ASYNC_start_job(void)196*e0c4386eSCy Schubert static int test_ASYNC_start_job(void)
197*e0c4386eSCy Schubert {
198*e0c4386eSCy Schubert ASYNC_JOB *job = NULL;
199*e0c4386eSCy Schubert int funcret;
200*e0c4386eSCy Schubert ASYNC_WAIT_CTX *waitctx = NULL;
201*e0c4386eSCy Schubert
202*e0c4386eSCy Schubert ctr = 0;
203*e0c4386eSCy Schubert
204*e0c4386eSCy Schubert if ( !ASYNC_init_thread(1, 0)
205*e0c4386eSCy Schubert || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
206*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
207*e0c4386eSCy Schubert != ASYNC_PAUSE
208*e0c4386eSCy Schubert || ctr != 1
209*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
210*e0c4386eSCy Schubert != ASYNC_FINISH
211*e0c4386eSCy Schubert || ctr != 2
212*e0c4386eSCy Schubert || funcret != 2) {
213*e0c4386eSCy Schubert fprintf(stderr, "test_ASYNC_start_job() failed\n");
214*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
215*e0c4386eSCy Schubert ASYNC_cleanup_thread();
216*e0c4386eSCy Schubert return 0;
217*e0c4386eSCy Schubert }
218*e0c4386eSCy Schubert
219*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
220*e0c4386eSCy Schubert ASYNC_cleanup_thread();
221*e0c4386eSCy Schubert return 1;
222*e0c4386eSCy Schubert }
223*e0c4386eSCy Schubert
test_ASYNC_get_current_job(void)224*e0c4386eSCy Schubert static int test_ASYNC_get_current_job(void)
225*e0c4386eSCy Schubert {
226*e0c4386eSCy Schubert ASYNC_JOB *job = NULL;
227*e0c4386eSCy Schubert int funcret;
228*e0c4386eSCy Schubert ASYNC_WAIT_CTX *waitctx = NULL;
229*e0c4386eSCy Schubert
230*e0c4386eSCy Schubert currjob = NULL;
231*e0c4386eSCy Schubert
232*e0c4386eSCy Schubert if ( !ASYNC_init_thread(1, 0)
233*e0c4386eSCy Schubert || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
234*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
235*e0c4386eSCy Schubert != ASYNC_PAUSE
236*e0c4386eSCy Schubert || currjob != job
237*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
238*e0c4386eSCy Schubert != ASYNC_FINISH
239*e0c4386eSCy Schubert || funcret != 1) {
240*e0c4386eSCy Schubert fprintf(stderr, "test_ASYNC_get_current_job() failed\n");
241*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
242*e0c4386eSCy Schubert ASYNC_cleanup_thread();
243*e0c4386eSCy Schubert return 0;
244*e0c4386eSCy Schubert }
245*e0c4386eSCy Schubert
246*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
247*e0c4386eSCy Schubert ASYNC_cleanup_thread();
248*e0c4386eSCy Schubert return 1;
249*e0c4386eSCy Schubert }
250*e0c4386eSCy Schubert
test_ASYNC_WAIT_CTX_get_all_fds(void)251*e0c4386eSCy Schubert static int test_ASYNC_WAIT_CTX_get_all_fds(void)
252*e0c4386eSCy Schubert {
253*e0c4386eSCy Schubert ASYNC_JOB *job = NULL;
254*e0c4386eSCy Schubert int funcret;
255*e0c4386eSCy Schubert ASYNC_WAIT_CTX *waitctx = NULL;
256*e0c4386eSCy Schubert OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD;
257*e0c4386eSCy Schubert size_t numfds, numdelfds;
258*e0c4386eSCy Schubert
259*e0c4386eSCy Schubert if ( !ASYNC_init_thread(1, 0)
260*e0c4386eSCy Schubert || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
261*e0c4386eSCy Schubert /* On first run we're not expecting any wait fds */
262*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
263*e0c4386eSCy Schubert != ASYNC_PAUSE
264*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
265*e0c4386eSCy Schubert || numfds != 0
266*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
267*e0c4386eSCy Schubert &numdelfds)
268*e0c4386eSCy Schubert || numfds != 0
269*e0c4386eSCy Schubert || numdelfds != 0
270*e0c4386eSCy Schubert /* On second run we're expecting one added fd */
271*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
272*e0c4386eSCy Schubert != ASYNC_PAUSE
273*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
274*e0c4386eSCy Schubert || numfds != 1
275*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds)
276*e0c4386eSCy Schubert || fd != MAGIC_WAIT_FD
277*e0c4386eSCy Schubert || (fd = OSSL_BAD_ASYNC_FD, 0) /* Assign to something else */
278*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
279*e0c4386eSCy Schubert &numdelfds)
280*e0c4386eSCy Schubert || numfds != 1
281*e0c4386eSCy Schubert || numdelfds != 0
282*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL,
283*e0c4386eSCy Schubert &numdelfds)
284*e0c4386eSCy Schubert || fd != MAGIC_WAIT_FD
285*e0c4386eSCy Schubert /* On third run we expect one deleted fd */
286*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
287*e0c4386eSCy Schubert != ASYNC_PAUSE
288*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
289*e0c4386eSCy Schubert || numfds != 0
290*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
291*e0c4386eSCy Schubert &numdelfds)
292*e0c4386eSCy Schubert || numfds != 0
293*e0c4386eSCy Schubert || numdelfds != 1
294*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd,
295*e0c4386eSCy Schubert &numdelfds)
296*e0c4386eSCy Schubert || delfd != MAGIC_WAIT_FD
297*e0c4386eSCy Schubert /* On last run we are not expecting any wait fd */
298*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
299*e0c4386eSCy Schubert != ASYNC_FINISH
300*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
301*e0c4386eSCy Schubert || numfds != 0
302*e0c4386eSCy Schubert || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
303*e0c4386eSCy Schubert &numdelfds)
304*e0c4386eSCy Schubert || numfds != 0
305*e0c4386eSCy Schubert || numdelfds != 0
306*e0c4386eSCy Schubert || funcret != 1) {
307*e0c4386eSCy Schubert fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n");
308*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
309*e0c4386eSCy Schubert ASYNC_cleanup_thread();
310*e0c4386eSCy Schubert return 0;
311*e0c4386eSCy Schubert }
312*e0c4386eSCy Schubert
313*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
314*e0c4386eSCy Schubert ASYNC_cleanup_thread();
315*e0c4386eSCy Schubert return 1;
316*e0c4386eSCy Schubert }
317*e0c4386eSCy Schubert
test_ASYNC_block_pause(void)318*e0c4386eSCy Schubert static int test_ASYNC_block_pause(void)
319*e0c4386eSCy Schubert {
320*e0c4386eSCy Schubert ASYNC_JOB *job = NULL;
321*e0c4386eSCy Schubert int funcret;
322*e0c4386eSCy Schubert ASYNC_WAIT_CTX *waitctx = NULL;
323*e0c4386eSCy Schubert
324*e0c4386eSCy Schubert if ( !ASYNC_init_thread(1, 0)
325*e0c4386eSCy Schubert || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
326*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
327*e0c4386eSCy Schubert != ASYNC_PAUSE
328*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
329*e0c4386eSCy Schubert != ASYNC_FINISH
330*e0c4386eSCy Schubert || funcret != 1) {
331*e0c4386eSCy Schubert fprintf(stderr, "test_ASYNC_block_pause() failed\n");
332*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
333*e0c4386eSCy Schubert ASYNC_cleanup_thread();
334*e0c4386eSCy Schubert return 0;
335*e0c4386eSCy Schubert }
336*e0c4386eSCy Schubert
337*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
338*e0c4386eSCy Schubert ASYNC_cleanup_thread();
339*e0c4386eSCy Schubert return 1;
340*e0c4386eSCy Schubert }
341*e0c4386eSCy Schubert
test_ASYNC_start_job_ex(void)342*e0c4386eSCy Schubert static int test_ASYNC_start_job_ex(void)
343*e0c4386eSCy Schubert {
344*e0c4386eSCy Schubert ASYNC_JOB *job = NULL;
345*e0c4386eSCy Schubert int funcret;
346*e0c4386eSCy Schubert ASYNC_WAIT_CTX *waitctx = NULL;
347*e0c4386eSCy Schubert OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
348*e0c4386eSCy Schubert OSSL_LIB_CTX *oldctx, *tmpctx, *globalctx;
349*e0c4386eSCy Schubert int ret = 0;
350*e0c4386eSCy Schubert
351*e0c4386eSCy Schubert if (libctx == NULL) {
352*e0c4386eSCy Schubert fprintf(stderr,
353*e0c4386eSCy Schubert "test_ASYNC_start_job_ex() failed to create libctx\n");
354*e0c4386eSCy Schubert goto err;
355*e0c4386eSCy Schubert }
356*e0c4386eSCy Schubert
357*e0c4386eSCy Schubert globalctx = oldctx = OSSL_LIB_CTX_set0_default(libctx);
358*e0c4386eSCy Schubert
359*e0c4386eSCy Schubert if ((waitctx = ASYNC_WAIT_CTX_new()) == NULL
360*e0c4386eSCy Schubert || ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx,
361*e0c4386eSCy Schubert NULL, 0)
362*e0c4386eSCy Schubert != ASYNC_PAUSE) {
363*e0c4386eSCy Schubert fprintf(stderr,
364*e0c4386eSCy Schubert "test_ASYNC_start_job_ex() failed to start job\n");
365*e0c4386eSCy Schubert goto err;
366*e0c4386eSCy Schubert }
367*e0c4386eSCy Schubert
368*e0c4386eSCy Schubert /* Reset the libctx temporarily to find out what it is*/
369*e0c4386eSCy Schubert tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
370*e0c4386eSCy Schubert oldctx = OSSL_LIB_CTX_set0_default(tmpctx);
371*e0c4386eSCy Schubert if (tmpctx != libctx) {
372*e0c4386eSCy Schubert fprintf(stderr,
373*e0c4386eSCy Schubert "test_ASYNC_start_job_ex() failed - unexpected libctx\n");
374*e0c4386eSCy Schubert goto err;
375*e0c4386eSCy Schubert }
376*e0c4386eSCy Schubert
377*e0c4386eSCy Schubert if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
378*e0c4386eSCy Schubert != ASYNC_PAUSE) {
379*e0c4386eSCy Schubert fprintf(stderr,
380*e0c4386eSCy Schubert "test_ASYNC_start_job_ex() - restarting job failed\n");
381*e0c4386eSCy Schubert goto err;
382*e0c4386eSCy Schubert }
383*e0c4386eSCy Schubert
384*e0c4386eSCy Schubert /* Reset the libctx and continue with the global default libctx */
385*e0c4386eSCy Schubert tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
386*e0c4386eSCy Schubert if (tmpctx != libctx) {
387*e0c4386eSCy Schubert fprintf(stderr,
388*e0c4386eSCy Schubert "test_ASYNC_start_job_ex() failed - unexpected libctx\n");
389*e0c4386eSCy Schubert goto err;
390*e0c4386eSCy Schubert }
391*e0c4386eSCy Schubert
392*e0c4386eSCy Schubert if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
393*e0c4386eSCy Schubert != ASYNC_FINISH
394*e0c4386eSCy Schubert || funcret != 1) {
395*e0c4386eSCy Schubert fprintf(stderr,
396*e0c4386eSCy Schubert "test_ASYNC_start_job_ex() - finishing job failed\n");
397*e0c4386eSCy Schubert goto err;
398*e0c4386eSCy Schubert }
399*e0c4386eSCy Schubert
400*e0c4386eSCy Schubert /* Reset the libctx temporarily to find out what it is*/
401*e0c4386eSCy Schubert tmpctx = OSSL_LIB_CTX_set0_default(libctx);
402*e0c4386eSCy Schubert OSSL_LIB_CTX_set0_default(tmpctx);
403*e0c4386eSCy Schubert if (tmpctx != globalctx) {
404*e0c4386eSCy Schubert fprintf(stderr,
405*e0c4386eSCy Schubert "test_ASYNC_start_job_ex() failed - global libctx check failed\n");
406*e0c4386eSCy Schubert goto err;
407*e0c4386eSCy Schubert }
408*e0c4386eSCy Schubert
409*e0c4386eSCy Schubert ret = 1;
410*e0c4386eSCy Schubert err:
411*e0c4386eSCy Schubert ASYNC_WAIT_CTX_free(waitctx);
412*e0c4386eSCy Schubert ASYNC_cleanup_thread();
413*e0c4386eSCy Schubert OSSL_LIB_CTX_free(libctx);
414*e0c4386eSCy Schubert return ret;
415*e0c4386eSCy Schubert }
416*e0c4386eSCy Schubert
main(int argc,char ** argv)417*e0c4386eSCy Schubert int main(int argc, char **argv)
418*e0c4386eSCy Schubert {
419*e0c4386eSCy Schubert if (!ASYNC_is_capable()) {
420*e0c4386eSCy Schubert fprintf(stderr,
421*e0c4386eSCy Schubert "OpenSSL build is not ASYNC capable - skipping async tests\n");
422*e0c4386eSCy Schubert } else {
423*e0c4386eSCy Schubert if (!test_ASYNC_init_thread()
424*e0c4386eSCy Schubert || !test_ASYNC_callback_status()
425*e0c4386eSCy Schubert || !test_ASYNC_start_job()
426*e0c4386eSCy Schubert || !test_ASYNC_get_current_job()
427*e0c4386eSCy Schubert || !test_ASYNC_WAIT_CTX_get_all_fds()
428*e0c4386eSCy Schubert || !test_ASYNC_block_pause()
429*e0c4386eSCy Schubert || !test_ASYNC_start_job_ex()) {
430*e0c4386eSCy Schubert return 1;
431*e0c4386eSCy Schubert }
432*e0c4386eSCy Schubert }
433*e0c4386eSCy Schubert printf("PASS\n");
434*e0c4386eSCy Schubert return 0;
435*e0c4386eSCy Schubert }
436