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>
64 #if (defined(OPENSSL_SYS_UNIX) || defined(OPENSSL_SYS_CYGWIN)) && defined(OPENSSL_THREADS)
66 # if _POSIX_VERSION >= 200112L
73 #if !defined(ASYNC_POSIX) && !defined(ASYNC_WIN)
80 static ASYNC_JOB *currjob = NULL;
82 static int only_pause(void *args)
89 static int add_two(void *args)
98 static int save_current(void *args)
100 currjob = ASYNC_get_current_job();
106 static int wake(void *args)
109 ASYNC_wake(ASYNC_get_current_job());
111 ASYNC_clear_wake(ASYNC_get_current_job());
116 static int blockpause(void *args)
120 ASYNC_unblock_pause();
126 static int test_ASYNC_init_thread()
128 ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL;
129 int funcret1, funcret2, funcret3;
131 if ( !ASYNC_init_thread(2, 0)
132 || ASYNC_start_job(&job1, &funcret1, only_pause, NULL, 0)
134 || ASYNC_start_job(&job2, &funcret2, only_pause, NULL, 0)
136 || ASYNC_start_job(&job3, &funcret3, only_pause, NULL, 0)
138 || ASYNC_start_job(&job1, &funcret1, only_pause, NULL, 0)
140 || ASYNC_start_job(&job3, &funcret3, only_pause, NULL, 0)
142 || ASYNC_start_job(&job2, &funcret2, only_pause, NULL, 0)
144 || ASYNC_start_job(&job3, &funcret3, only_pause, NULL, 0)
149 fprintf(stderr, "test_ASYNC_init_thread() failed\n");
150 ASYNC_cleanup_thread();
154 ASYNC_cleanup_thread();
158 static int test_ASYNC_start_job()
160 ASYNC_JOB *job = NULL;
165 if ( !ASYNC_init_thread(1, 0)
166 || ASYNC_start_job(&job, &funcret, add_two, NULL, 0) != ASYNC_PAUSE
168 || ASYNC_start_job(&job, &funcret, add_two, NULL, 0) != ASYNC_FINISH
171 fprintf(stderr, "test_ASYNC_start_job() failed\n");
172 ASYNC_cleanup_thread();
176 ASYNC_cleanup_thread();
180 static int test_ASYNC_get_current_job()
182 ASYNC_JOB *job = NULL;
187 if ( !ASYNC_init_thread(1, 0)
188 || ASYNC_start_job(&job, &funcret, save_current, NULL, 0)
191 || ASYNC_start_job(&job, &funcret, save_current, NULL, 0)
194 fprintf(stderr, "test_ASYNC_get_current_job() failed\n");
195 ASYNC_cleanup_thread();
199 ASYNC_cleanup_thread();
203 static int hasdata(OSSL_ASYNC_FD fd)
209 openssl_fdset(fd, &checkfds);
210 memset(&tv, 0, sizeof tv);
211 if (select(fd + 1, (void *)&checkfds, NULL, NULL, &tv) < 0)
213 if (FD_ISSET(fd, &checkfds))
219 if (PeekNamedPipe(fd, NULL, 0, NULL, &avail, NULL) && avail > 0)
226 static int test_ASYNC_get_wait_fd()
228 ASYNC_JOB *job = NULL;
232 if ( !ASYNC_init_thread(1, 0)
233 || ASYNC_start_job(&job, &funcret, wake, NULL, 0)
235 || (fd = ASYNC_get_wait_fd(job)) < 0
237 || ASYNC_start_job(&job, &funcret, save_current, NULL, 0)
240 || (ASYNC_clear_wake(job), 0)
242 || (ASYNC_wake(job), 0)
244 || ASYNC_start_job(&job, &funcret, save_current, NULL, 0)
247 fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n");
248 ASYNC_cleanup_thread();
252 ASYNC_cleanup_thread();
256 static int test_ASYNC_block_pause()
258 ASYNC_JOB *job = NULL;
261 if ( !ASYNC_init_thread(1, 0)
262 || ASYNC_start_job(&job, &funcret, blockpause, NULL, 0)
264 || ASYNC_start_job(&job, &funcret, blockpause, NULL, 0)
267 fprintf(stderr, "test_ASYNC_block_pause() failed\n");
268 ASYNC_cleanup_thread();
272 ASYNC_cleanup_thread();
278 int main(int argc, char **argv)
282 fprintf(stderr, "NULL implementation - skipping async tests\n");
284 CRYPTO_set_mem_debug(1);
285 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
287 if ( !test_ASYNC_init_thread()
288 || !test_ASYNC_start_job()
289 || !test_ASYNC_get_current_job()
290 || !test_ASYNC_get_wait_fd()
291 || !test_ASYNC_block_pause()) {