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