]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/asynctest.c
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 #define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
107 static int waitfd(void *args
)
110 ASYNC_WAIT_CTX
*waitctx
;
112 job
= ASYNC_get_current_job();
115 waitctx
= ASYNC_get_wait_ctx(job
);
118 if(!ASYNC_WAIT_CTX_set_wait_fd(waitctx
, waitctx
, MAGIC_WAIT_FD
, NULL
, NULL
))
122 if (!ASYNC_WAIT_CTX_clear_fd(waitctx
, waitctx
))
128 static int blockpause(void *args
)
132 ASYNC_unblock_pause();
138 static int test_ASYNC_init_thread()
140 ASYNC_JOB
*job1
= NULL
, *job2
= NULL
, *job3
= NULL
;
141 int funcret1
, funcret2
, funcret3
;
142 ASYNC_WAIT_CTX
*waitctx
;
144 if ( !ASYNC_init_thread(2, 0)
145 || (waitctx
= ASYNC_WAIT_CTX_new()) == NULL
146 || ASYNC_start_job(&job1
, waitctx
, &funcret1
, only_pause
, NULL
, 0)
148 || ASYNC_start_job(&job2
, waitctx
, &funcret2
, only_pause
, NULL
, 0)
150 || ASYNC_start_job(&job3
, waitctx
, &funcret3
, only_pause
, NULL
, 0)
152 || ASYNC_start_job(&job1
, waitctx
, &funcret1
, only_pause
, NULL
, 0)
154 || ASYNC_start_job(&job3
, waitctx
, &funcret3
, only_pause
, NULL
, 0)
156 || ASYNC_start_job(&job2
, waitctx
, &funcret2
, only_pause
, NULL
, 0)
158 || ASYNC_start_job(&job3
, waitctx
, &funcret3
, only_pause
, NULL
, 0)
163 fprintf(stderr
, "test_ASYNC_init_thread() failed\n");
164 ASYNC_WAIT_CTX_free(waitctx
);
165 ASYNC_cleanup_thread();
169 ASYNC_WAIT_CTX_free(waitctx
);
170 ASYNC_cleanup_thread();
174 static int test_ASYNC_start_job()
176 ASYNC_JOB
*job
= NULL
;
178 ASYNC_WAIT_CTX
*waitctx
;
182 if ( !ASYNC_init_thread(1, 0)
183 || (waitctx
= ASYNC_WAIT_CTX_new()) == NULL
184 || ASYNC_start_job(&job
, waitctx
, &funcret
, add_two
, NULL
, 0)
187 || ASYNC_start_job(&job
, waitctx
, &funcret
, add_two
, NULL
, 0)
191 fprintf(stderr
, "test_ASYNC_start_job() failed\n");
192 ASYNC_WAIT_CTX_free(waitctx
);
193 ASYNC_cleanup_thread();
197 ASYNC_WAIT_CTX_free(waitctx
);
198 ASYNC_cleanup_thread();
202 static int test_ASYNC_get_current_job()
204 ASYNC_JOB
*job
= NULL
;
206 ASYNC_WAIT_CTX
*waitctx
;
210 if ( !ASYNC_init_thread(1, 0)
211 || (waitctx
= ASYNC_WAIT_CTX_new()) == NULL
212 || ASYNC_start_job(&job
, waitctx
, &funcret
, save_current
, NULL
, 0)
215 || ASYNC_start_job(&job
, waitctx
, &funcret
, save_current
, NULL
, 0)
218 fprintf(stderr
, "test_ASYNC_get_current_job() failed\n");
219 ASYNC_WAIT_CTX_free(waitctx
);
220 ASYNC_cleanup_thread();
224 ASYNC_WAIT_CTX_free(waitctx
);
225 ASYNC_cleanup_thread();
229 static int test_ASYNC_WAIT_CTX_get_all_fds()
231 ASYNC_JOB
*job
= NULL
;
233 ASYNC_WAIT_CTX
*waitctx
;
234 OSSL_ASYNC_FD fd
= OSSL_BAD_ASYNC_FD
, delfd
= OSSL_BAD_ASYNC_FD
;
235 size_t numfds
, numdelfds
;
237 if ( !ASYNC_init_thread(1, 0)
238 || (waitctx
= ASYNC_WAIT_CTX_new()) == NULL
239 /* On first run we're not expecting any wait fds */
240 || ASYNC_start_job(&job
, waitctx
, &funcret
, waitfd
, NULL
, 0)
242 || !ASYNC_WAIT_CTX_get_all_fds(waitctx
, NULL
, &numfds
)
244 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx
, NULL
, &numfds
, NULL
,
248 /* On second run we're expecting one added 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_all_fds(waitctx
, &fd
, &numfds
)
254 || fd
!= MAGIC_WAIT_FD
255 || (fd
= OSSL_BAD_ASYNC_FD
, 0) /* Assign to something else */
256 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx
, NULL
, &numfds
, NULL
,
260 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx
, &fd
, &numfds
, NULL
,
262 || fd
!= MAGIC_WAIT_FD
263 /* On final run we expect one deleted fd */
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 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx
, NULL
, &numfds
, &delfd
,
274 || delfd
!= MAGIC_WAIT_FD
276 fprintf(stderr
, "test_ASYNC_get_wait_fd() failed\n");
277 ASYNC_WAIT_CTX_free(waitctx
);
278 ASYNC_cleanup_thread();
282 ASYNC_WAIT_CTX_free(waitctx
);
283 ASYNC_cleanup_thread();
287 static int test_ASYNC_block_pause()
289 ASYNC_JOB
*job
= NULL
;
291 ASYNC_WAIT_CTX
*waitctx
;
293 if ( !ASYNC_init_thread(1, 0)
294 || (waitctx
= ASYNC_WAIT_CTX_new()) == NULL
295 || ASYNC_start_job(&job
, waitctx
, &funcret
, blockpause
, NULL
, 0)
297 || ASYNC_start_job(&job
, waitctx
, &funcret
, blockpause
, NULL
, 0)
300 fprintf(stderr
, "test_ASYNC_block_pause() failed\n");
301 ASYNC_WAIT_CTX_free(waitctx
);
302 ASYNC_cleanup_thread();
306 ASYNC_WAIT_CTX_free(waitctx
);
307 ASYNC_cleanup_thread();
313 int main(int argc
, char **argv
)
317 fprintf(stderr
, "NULL implementation - skipping async tests\n");
319 CRYPTO_set_mem_debug(1);
320 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
322 if ( !test_ASYNC_init_thread()
323 || !test_ASYNC_start_job()
324 || !test_ASYNC_get_current_job()
325 || !test_ASYNC_WAIT_CTX_get_all_fds()
326 || !test_ASYNC_block_pause()) {