Add a test to make sure ASYNC aware code gets the right default libctx
authorMatt Caswell <matt@openssl.org>
Fri, 26 Jun 2020 10:02:29 +0000 (11:02 +0100)
committerRichard Levitte <levitte@openssl.org>
Sun, 28 Jun 2020 08:55:52 +0000 (10:55 +0200)
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)

test/asynctest.c

index dc357da8b7a4a8b574064857529665941e8adae8..13b66993c5c304d8e9b4048310f54e6a0896aa1c 100644 (file)
@@ -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;
         }
     }