summaryrefslogtreecommitdiffstats
path: root/test/asynctest.c
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2020-06-26 11:02:29 +0100
committerRichard Levitte <levitte@openssl.org>2020-06-28 10:55:52 +0200
commit92db29e5e8c26af79f740dfd3bddfeae9b5345a8 (patch)
treed77c72df549dd716bd160ac87ada49193e0ba295 /test/asynctest.c
parent6c689e58f70726cfa5533bc10f9669abce199fb8 (diff)
Add a test to make sure ASYNC aware code gets the right default libctx
Even if a fibre changes the default libctx - or the main application code changes it, the "current" default libctx should remain consistent. Reviewed-by: Richard Levitte <levitte@openssl.org> (Merged from https://github.com/openssl/openssl/pull/12228)
Diffstat (limited to 'test/asynctest.c')
-rw-r--r--test/asynctest.c110
1 files changed, 109 insertions, 1 deletions
diff --git a/test/asynctest.c b/test/asynctest.c
index dc357da8b7..13b66993c5 100644
--- a/test/asynctest.c
+++ b/test/asynctest.c
@@ -43,6 +43,39 @@ static int save_current(void *args)
return 1;
}
+static int change_deflt_libctx(void *args)
+{
+ OPENSSL_CTX *libctx = OPENSSL_CTX_new();
+ OPENSSL_CTX *oldctx, *tmpctx;
+ int ret = 0;
+
+ if (libctx == NULL)
+ return 0;
+
+ oldctx = OPENSSL_CTX_set0_default(libctx);
+ ASYNC_pause_job();
+
+ /* Check the libctx is set up as we expect */
+ tmpctx = OPENSSL_CTX_set0_default(oldctx);
+ if (tmpctx != libctx)
+ goto err;
+
+ /* Set it back again to continue to use our own libctx */
+ oldctx = OPENSSL_CTX_set0_default(libctx);
+ ASYNC_pause_job();
+
+ /* Check the libctx is set up as we expect */
+ tmpctx = OPENSSL_CTX_set0_default(oldctx);
+ if (tmpctx != libctx)
+ goto err;
+
+ ret = 1;
+ err:
+ OPENSSL_CTX_free(libctx);
+ return ret;
+}
+
+
#define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
static int waitfd(void *args)
{
@@ -306,6 +339,80 @@ static int test_ASYNC_block_pause(void)
return 1;
}
+static int test_ASYNC_start_job_with_libctx(void)
+{
+ ASYNC_JOB *job = NULL;
+ int funcret;
+ ASYNC_WAIT_CTX *waitctx = NULL;
+ OPENSSL_CTX *libctx = OPENSSL_CTX_new();
+ OPENSSL_CTX *oldctx, *tmpctx, *globalctx;
+ int ret = 0;
+
+ if (libctx == NULL) {
+ fprintf(stderr,
+ "test_ASYNC_start_job_with_libctx() failed to create libctx\n");
+ goto err;
+ }
+
+ globalctx = oldctx = OPENSSL_CTX_set0_default(libctx);
+
+ if ((waitctx = ASYNC_WAIT_CTX_new()) == NULL
+ || ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx,
+ NULL, 0)
+ != ASYNC_PAUSE) {
+ fprintf(stderr,
+ "test_ASYNC_start_job_with_libctx() failed to start job\n");
+ goto err;
+ }
+
+ /* Reset the libctx temporarily to find out what it is*/
+ tmpctx = OPENSSL_CTX_set0_default(oldctx);
+ oldctx = OPENSSL_CTX_set0_default(tmpctx);
+ if (tmpctx != libctx) {
+ fprintf(stderr,
+ "test_ASYNC_start_job_with_libctx() failed - unexpected libctx\n");
+ goto err;
+ }
+
+ if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
+ != ASYNC_PAUSE) {
+ fprintf(stderr,
+ "test_ASYNC_start_job_with_libctx() - restarting job failed\n");
+ goto err;
+ }
+
+ /* Reset the libctx and continue with the global default libctx */
+ tmpctx = OPENSSL_CTX_set0_default(oldctx);
+ if (tmpctx != libctx) {
+ fprintf(stderr,
+ "test_ASYNC_start_job_with_libctx() failed - unexpected libctx\n");
+ goto err;
+ }
+
+ if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
+ != ASYNC_FINISH
+ || funcret != 1) {
+ fprintf(stderr,
+ "test_ASYNC_start_job_with_libctx() - finishing job failed\n");
+ goto err;
+ }
+
+ /* Reset the libctx temporarily to find out what it is*/
+ tmpctx = OPENSSL_CTX_set0_default(libctx);
+ OPENSSL_CTX_set0_default(tmpctx);
+ if (tmpctx != globalctx) {
+ fprintf(stderr,
+ "test_ASYNC_start_job_with_libctx() failed - global libctx check failed\n");
+ goto err;
+ }
+
+ ret = 1;
+ err:
+ ASYNC_WAIT_CTX_free(waitctx);
+ OPENSSL_CTX_free(libctx);
+ return ret;
+}
+
int main(int argc, char **argv)
{
if (!ASYNC_is_capable()) {
@@ -317,7 +424,8 @@ int main(int argc, char **argv)
|| !test_ASYNC_start_job()
|| !test_ASYNC_get_current_job()
|| !test_ASYNC_WAIT_CTX_get_all_fds()
- || !test_ASYNC_block_pause()) {
+ || !test_ASYNC_block_pause()
+ || !test_ASYNC_start_job_with_libctx()) {
return 1;
}
}