]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Add a test for OSSL_LIB_CTX_set0_default
authorMatt Caswell <matt@openssl.org>
Fri, 16 Apr 2021 11:21:50 +0000 (12:21 +0100)
committerMatt Caswell <matt@openssl.org>
Mon, 19 Apr 2021 09:52:18 +0000 (10:52 +0100)
Also includes testing for OSSL_LIB_CTX_get0_global_default().

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tim Hudson <tjh@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14890)

test/context_internal_test.c

index 0b786adf6754848c6a558c5f2a8c15fc8b1fa855..a8759084695bbe851190a79fee130cf95e590803 100644 (file)
@@ -73,9 +73,64 @@ static int test_def_context(void)
     return test_context(NULL);
 }
 
+static int test_set0_default(void)
+{
+    OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default();
+    OSSL_LIB_CTX *local = OSSL_LIB_CTX_new();
+    OSSL_LIB_CTX *prev;
+    int testresult = 0;
+    FOO *data = NULL;
+
+    if (!TEST_ptr(global)
+            || !TEST_ptr(local)
+            || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL))
+            || !TEST_ptr(data = ossl_lib_ctx_get_data(local, 0, &foo_method)))
+        goto err;
+
+    /* Set local "i" value to 43. Global "i" should be 42 */
+    data->i++;
+    if (!TEST_int_eq(data->i, 43))
+        goto err;
+
+    /* The default context should still be the "global" default */
+    if (!TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
+            || !TEST_int_eq(data->i, 42))
+        goto err;
+
+    /* Check we can change the local default context */
+    if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local))
+            || !TEST_ptr_eq(global, prev)
+            || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
+            || !TEST_int_eq(data->i, 43))
+        goto err;
+
+    /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */
+    if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL))
+            || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
+            || !TEST_int_eq(data->i, 43))
+        goto err;
+
+    /* Global default should be unchanged */
+    if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default()))
+        goto err;
+
+    /* Check we can swap back to the global default */
+   if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global))
+            || !TEST_ptr_eq(local, prev)
+            || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
+            || !TEST_int_eq(data->i, 42))
+        goto err;
+
+    testresult = 1;
+ err:
+    OSSL_LIB_CTX_free(local);
+    return testresult;
+}
+
 int setup_tests(void)
 {
     ADD_TEST(test_app_context);
     ADD_TEST(test_def_context);
+    ADD_TEST(test_set0_default);
     return 1;
 }