xref: /freebsd/crypto/openssl/test/context_internal_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert /* Internal tests for the OpenSSL library context */
11*e0c4386eSCy Schubert 
12*e0c4386eSCy Schubert #include "internal/cryptlib.h"
13*e0c4386eSCy Schubert #include "testutil.h"
14*e0c4386eSCy Schubert 
15*e0c4386eSCy Schubert /*
16*e0c4386eSCy Schubert  * Everything between BEGIN EXAMPLE and END EXAMPLE is copied from
17*e0c4386eSCy Schubert  * doc/internal/man3/ossl_lib_ctx_get_data.pod
18*e0c4386eSCy Schubert  */
19*e0c4386eSCy Schubert 
20*e0c4386eSCy Schubert /*
21*e0c4386eSCy Schubert  * ======================================================================
22*e0c4386eSCy Schubert  * BEGIN EXAMPLE
23*e0c4386eSCy Schubert  */
24*e0c4386eSCy Schubert 
25*e0c4386eSCy Schubert typedef struct foo_st {
26*e0c4386eSCy Schubert     int i;
27*e0c4386eSCy Schubert     void *data;
28*e0c4386eSCy Schubert } FOO;
29*e0c4386eSCy Schubert 
foo_new(OSSL_LIB_CTX * ctx)30*e0c4386eSCy Schubert static void *foo_new(OSSL_LIB_CTX *ctx)
31*e0c4386eSCy Schubert {
32*e0c4386eSCy Schubert     FOO *ptr = OPENSSL_zalloc(sizeof(*ptr));
33*e0c4386eSCy Schubert     if (ptr != NULL)
34*e0c4386eSCy Schubert         ptr->i = 42;
35*e0c4386eSCy Schubert     return ptr;
36*e0c4386eSCy Schubert }
foo_free(void * ptr)37*e0c4386eSCy Schubert static void foo_free(void *ptr)
38*e0c4386eSCy Schubert {
39*e0c4386eSCy Schubert     OPENSSL_free(ptr);
40*e0c4386eSCy Schubert }
41*e0c4386eSCy Schubert static const OSSL_LIB_CTX_METHOD foo_method = {
42*e0c4386eSCy Schubert     OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
43*e0c4386eSCy Schubert     foo_new,
44*e0c4386eSCy Schubert     foo_free
45*e0c4386eSCy Schubert };
46*e0c4386eSCy Schubert 
47*e0c4386eSCy Schubert /*
48*e0c4386eSCy Schubert  * END EXAMPLE
49*e0c4386eSCy Schubert  * ======================================================================
50*e0c4386eSCy Schubert  */
51*e0c4386eSCy Schubert 
test_context(OSSL_LIB_CTX * ctx)52*e0c4386eSCy Schubert static int test_context(OSSL_LIB_CTX *ctx)
53*e0c4386eSCy Schubert {
54*e0c4386eSCy Schubert     FOO *data = NULL;
55*e0c4386eSCy Schubert 
56*e0c4386eSCy Schubert     return TEST_ptr(data = ossl_lib_ctx_get_data(ctx, 0, &foo_method))
57*e0c4386eSCy Schubert         /* OPENSSL_zalloc in foo_new() initialized it to zero */
58*e0c4386eSCy Schubert         && TEST_int_eq(data->i, 42);
59*e0c4386eSCy Schubert }
60*e0c4386eSCy Schubert 
test_app_context(void)61*e0c4386eSCy Schubert static int test_app_context(void)
62*e0c4386eSCy Schubert {
63*e0c4386eSCy Schubert     OSSL_LIB_CTX *ctx = NULL;
64*e0c4386eSCy Schubert     int result =
65*e0c4386eSCy Schubert         TEST_ptr(ctx = OSSL_LIB_CTX_new())
66*e0c4386eSCy Schubert         && test_context(ctx);
67*e0c4386eSCy Schubert 
68*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(ctx);
69*e0c4386eSCy Schubert     return result;
70*e0c4386eSCy Schubert }
71*e0c4386eSCy Schubert 
test_def_context(void)72*e0c4386eSCy Schubert static int test_def_context(void)
73*e0c4386eSCy Schubert {
74*e0c4386eSCy Schubert     return test_context(NULL);
75*e0c4386eSCy Schubert }
76*e0c4386eSCy Schubert 
test_set0_default(void)77*e0c4386eSCy Schubert static int test_set0_default(void)
78*e0c4386eSCy Schubert {
79*e0c4386eSCy Schubert     OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default();
80*e0c4386eSCy Schubert     OSSL_LIB_CTX *local = OSSL_LIB_CTX_new();
81*e0c4386eSCy Schubert     OSSL_LIB_CTX *prev;
82*e0c4386eSCy Schubert     int testresult = 0;
83*e0c4386eSCy Schubert     FOO *data = NULL;
84*e0c4386eSCy Schubert 
85*e0c4386eSCy Schubert     if (!TEST_ptr(global)
86*e0c4386eSCy Schubert             || !TEST_ptr(local)
87*e0c4386eSCy Schubert             || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL))
88*e0c4386eSCy Schubert             || !TEST_ptr(data = ossl_lib_ctx_get_data(local, 0, &foo_method)))
89*e0c4386eSCy Schubert         goto err;
90*e0c4386eSCy Schubert 
91*e0c4386eSCy Schubert     /* Set local "i" value to 43. Global "i" should be 42 */
92*e0c4386eSCy Schubert     data->i++;
93*e0c4386eSCy Schubert     if (!TEST_int_eq(data->i, 43))
94*e0c4386eSCy Schubert         goto err;
95*e0c4386eSCy Schubert 
96*e0c4386eSCy Schubert     /* The default context should still be the "global" default */
97*e0c4386eSCy Schubert     if (!TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
98*e0c4386eSCy Schubert             || !TEST_int_eq(data->i, 42))
99*e0c4386eSCy Schubert         goto err;
100*e0c4386eSCy Schubert 
101*e0c4386eSCy Schubert     /* Check we can change the local default context */
102*e0c4386eSCy Schubert     if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local))
103*e0c4386eSCy Schubert             || !TEST_ptr_eq(global, prev)
104*e0c4386eSCy Schubert             || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
105*e0c4386eSCy Schubert             || !TEST_int_eq(data->i, 43))
106*e0c4386eSCy Schubert         goto err;
107*e0c4386eSCy Schubert 
108*e0c4386eSCy Schubert     /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */
109*e0c4386eSCy Schubert     if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL))
110*e0c4386eSCy Schubert             || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
111*e0c4386eSCy Schubert             || !TEST_int_eq(data->i, 43))
112*e0c4386eSCy Schubert         goto err;
113*e0c4386eSCy Schubert 
114*e0c4386eSCy Schubert     /* Global default should be unchanged */
115*e0c4386eSCy Schubert     if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default()))
116*e0c4386eSCy Schubert         goto err;
117*e0c4386eSCy Schubert 
118*e0c4386eSCy Schubert     /* Check we can swap back to the global default */
119*e0c4386eSCy Schubert    if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global))
120*e0c4386eSCy Schubert             || !TEST_ptr_eq(local, prev)
121*e0c4386eSCy Schubert             || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
122*e0c4386eSCy Schubert             || !TEST_int_eq(data->i, 42))
123*e0c4386eSCy Schubert         goto err;
124*e0c4386eSCy Schubert 
125*e0c4386eSCy Schubert     testresult = 1;
126*e0c4386eSCy Schubert  err:
127*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(local);
128*e0c4386eSCy Schubert     return testresult;
129*e0c4386eSCy Schubert }
130*e0c4386eSCy Schubert 
setup_tests(void)131*e0c4386eSCy Schubert int setup_tests(void)
132*e0c4386eSCy Schubert {
133*e0c4386eSCy Schubert     ADD_TEST(test_app_context);
134*e0c4386eSCy Schubert     ADD_TEST(test_def_context);
135*e0c4386eSCy Schubert     ADD_TEST(test_set0_default);
136*e0c4386eSCy Schubert     return 1;
137*e0c4386eSCy Schubert }
138