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