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)
This commit is contained in:
Matt Caswell 2021-04-16 12:21:50 +01:00
parent 978e323a4d
commit ee203a87ff

View 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;
}