2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
18 #include <../apps/apps.h>
21 static ASYNC_JOB *currjob = NULL;
23 static int only_pause(void *args)
30 static int add_two(void *args)
39 static int save_current(void *args)
41 currjob = ASYNC_get_current_job();
47 #define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
48 static int waitfd(void *args)
51 ASYNC_WAIT_CTX *waitctx;
52 job = ASYNC_get_current_job();
55 waitctx = ASYNC_get_wait_ctx(job);
59 /* First case: no fd added or removed */
62 /* Second case: one fd added */
63 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
67 /* Third case: all fd removed */
68 if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
72 /* Last case: fd added and immediately removed */
73 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
75 if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
81 static int blockpause(void *args)
85 ASYNC_unblock_pause();
91 static int test_ASYNC_init_thread()
93 ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL;
94 int funcret1, funcret2, funcret3;
95 ASYNC_WAIT_CTX *waitctx = NULL;
97 if ( !ASYNC_init_thread(2, 0)
98 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
99 || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
101 || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
103 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
105 || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
107 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
109 || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
111 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
116 fprintf(stderr, "test_ASYNC_init_thread() failed\n");
117 ASYNC_WAIT_CTX_free(waitctx);
118 ASYNC_cleanup_thread();
122 ASYNC_WAIT_CTX_free(waitctx);
123 ASYNC_cleanup_thread();
127 static int test_ASYNC_start_job()
129 ASYNC_JOB *job = NULL;
131 ASYNC_WAIT_CTX *waitctx = NULL;
135 if ( !ASYNC_init_thread(1, 0)
136 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
137 || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
140 || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
144 fprintf(stderr, "test_ASYNC_start_job() failed\n");
145 ASYNC_WAIT_CTX_free(waitctx);
146 ASYNC_cleanup_thread();
150 ASYNC_WAIT_CTX_free(waitctx);
151 ASYNC_cleanup_thread();
155 static int test_ASYNC_get_current_job()
157 ASYNC_JOB *job = NULL;
159 ASYNC_WAIT_CTX *waitctx = NULL;
163 if ( !ASYNC_init_thread(1, 0)
164 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
165 || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
168 || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
171 fprintf(stderr, "test_ASYNC_get_current_job() failed\n");
172 ASYNC_WAIT_CTX_free(waitctx);
173 ASYNC_cleanup_thread();
177 ASYNC_WAIT_CTX_free(waitctx);
178 ASYNC_cleanup_thread();
182 static int test_ASYNC_WAIT_CTX_get_all_fds()
184 ASYNC_JOB *job = NULL;
186 ASYNC_WAIT_CTX *waitctx = NULL;
187 OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD;
188 size_t numfds, numdelfds;
190 if ( !ASYNC_init_thread(1, 0)
191 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
192 /* On first run we're not expecting any wait fds */
193 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
195 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
197 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
201 /* On second run we're expecting one added fd */
202 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
204 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
206 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds)
207 || fd != MAGIC_WAIT_FD
208 || (fd = OSSL_BAD_ASYNC_FD, 0) /* Assign to something else */
209 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
213 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL,
215 || fd != MAGIC_WAIT_FD
216 /* On third run we expect one deleted fd */
217 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
219 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
221 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
225 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd,
227 || delfd != MAGIC_WAIT_FD
228 /* On last run we are not expecting any wait fd */
229 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
231 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
233 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
238 fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n");
239 ASYNC_WAIT_CTX_free(waitctx);
240 ASYNC_cleanup_thread();
244 ASYNC_WAIT_CTX_free(waitctx);
245 ASYNC_cleanup_thread();
249 static int test_ASYNC_block_pause()
251 ASYNC_JOB *job = NULL;
253 ASYNC_WAIT_CTX *waitctx = NULL;
255 if ( !ASYNC_init_thread(1, 0)
256 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
257 || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
259 || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
262 fprintf(stderr, "test_ASYNC_block_pause() failed\n");
263 ASYNC_WAIT_CTX_free(waitctx);
264 ASYNC_cleanup_thread();
268 ASYNC_WAIT_CTX_free(waitctx);
269 ASYNC_cleanup_thread();
273 int main(int argc, char **argv)
275 if (!ASYNC_is_capable()) {
277 "OpenSSL build is not ASYNC capable - skipping async tests\n");
279 CRYPTO_set_mem_debug(1);
280 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
282 if ( !test_ASYNC_init_thread()
283 || !test_ASYNC_start_job()
284 || !test_ASYNC_get_current_job()
285 || !test_ASYNC_WAIT_CTX_get_all_fds()
286 || !test_ASYNC_block_pause()) {