diff options
author | Matt Caswell <matt@openssl.org> | 2021-04-16 12:21:50 +0100 |
---|---|---|
committer | Matt Caswell <matt@openssl.org> | 2021-04-19 10:52:18 +0100 |
commit | ee203a87ff1ff1af46a5ff11f761bdd07a5503e4 (patch) | |
tree | 424723f4ad21258c78549c7ef4f8a5e3b662ba98 | |
parent | 978e323a4dbc9e790c13cc479b68c260677dc4c4 (diff) |
Add a test for OSSL_LIB_CTX_set0_default
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)
-rw-r--r-- | test/context_internal_test.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test/context_internal_test.c b/test/context_internal_test.c index 0b786adf67..a875908469 100644 --- a/test/context_internal_test.c +++ b/test/context_internal_test.c @@ -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; } |