2 * Written by Matt Caswell for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
60 #include <openssl/async.h>
61 #include <openssl/crypto.h>
62 #include <../apps/apps.h>
65 static ASYNC_JOB *currjob = NULL;
67 static int only_pause(void *args)
74 static int add_two(void *args)
83 static int save_current(void *args)
85 currjob = ASYNC_get_current_job();
91 #define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
92 static int waitfd(void *args)
95 ASYNC_WAIT_CTX *waitctx;
97 job = ASYNC_get_current_job();
100 waitctx = ASYNC_get_wait_ctx(job);
103 if(!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
107 if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
113 static int blockpause(void *args)
117 ASYNC_unblock_pause();
123 static int test_ASYNC_init_thread()
125 ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL;
126 int funcret1, funcret2, funcret3;
127 ASYNC_WAIT_CTX *waitctx = NULL;
129 if ( !ASYNC_init_thread(2, 0)
130 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
131 || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
133 || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
135 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
137 || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
139 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
141 || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
143 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
148 fprintf(stderr, "test_ASYNC_init_thread() failed\n");
149 ASYNC_WAIT_CTX_free(waitctx);
150 ASYNC_cleanup_thread();
154 ASYNC_WAIT_CTX_free(waitctx);
155 ASYNC_cleanup_thread();
159 static int test_ASYNC_start_job()
161 ASYNC_JOB *job = NULL;
163 ASYNC_WAIT_CTX *waitctx = NULL;
167 if ( !ASYNC_init_thread(1, 0)
168 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
169 || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
172 || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
176 fprintf(stderr, "test_ASYNC_start_job() failed\n");
177 ASYNC_WAIT_CTX_free(waitctx);
178 ASYNC_cleanup_thread();
182 ASYNC_WAIT_CTX_free(waitctx);
183 ASYNC_cleanup_thread();
187 static int test_ASYNC_get_current_job()
189 ASYNC_JOB *job = NULL;
191 ASYNC_WAIT_CTX *waitctx = NULL;
195 if ( !ASYNC_init_thread(1, 0)
196 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
197 || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
200 || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
203 fprintf(stderr, "test_ASYNC_get_current_job() failed\n");
204 ASYNC_WAIT_CTX_free(waitctx);
205 ASYNC_cleanup_thread();
209 ASYNC_WAIT_CTX_free(waitctx);
210 ASYNC_cleanup_thread();
214 static int test_ASYNC_WAIT_CTX_get_all_fds()
216 ASYNC_JOB *job = NULL;
218 ASYNC_WAIT_CTX *waitctx = NULL;
219 OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD;
220 size_t numfds, numdelfds;
222 if ( !ASYNC_init_thread(1, 0)
223 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
224 /* On first run we're not expecting any wait fds */
225 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
227 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
229 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
233 /* On second run we're expecting one added fd */
234 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
236 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
238 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds)
239 || fd != MAGIC_WAIT_FD
240 || (fd = OSSL_BAD_ASYNC_FD, 0) /* Assign to something else */
241 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
245 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL,
247 || fd != MAGIC_WAIT_FD
248 /* On final run we expect one deleted fd */
249 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
251 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
253 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
257 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd,
259 || delfd != MAGIC_WAIT_FD
261 fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n");
262 ASYNC_WAIT_CTX_free(waitctx);
263 ASYNC_cleanup_thread();
267 ASYNC_WAIT_CTX_free(waitctx);
268 ASYNC_cleanup_thread();
272 static int test_ASYNC_block_pause()
274 ASYNC_JOB *job = NULL;
276 ASYNC_WAIT_CTX *waitctx = NULL;
278 if ( !ASYNC_init_thread(1, 0)
279 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
280 || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
282 || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
285 fprintf(stderr, "test_ASYNC_block_pause() failed\n");
286 ASYNC_WAIT_CTX_free(waitctx);
287 ASYNC_cleanup_thread();
291 ASYNC_WAIT_CTX_free(waitctx);
292 ASYNC_cleanup_thread();
296 int main(int argc, char **argv)
298 if (!ASYNC_is_capable()) {
300 "OpenSSL build is not ASYNC capable - skipping async tests\n");
302 CRYPTO_set_mem_debug(1);
303 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
305 if ( !test_ASYNC_init_thread()
306 || !test_ASYNC_start_job()
307 || !test_ASYNC_get_current_job()
308 || !test_ASYNC_WAIT_CTX_get_all_fds()
309 || !test_ASYNC_block_pause()) {