Adjust ssl_test_new for SHA1 security level
[openssl.git] / test / context_internal_test.c
1 /*
2  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
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
17  * doc/internal/man3/ossl_lib_ctx_get_data.pod
18  */
19
20 /*
21  * ======================================================================
22  * BEGIN EXAMPLE
23  */
24
25 typedef struct foo_st {
26     int i;
27     void *data;
28 } FOO;
29
30 static void *foo_new(OSSL_LIB_CTX *ctx)
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 }
41 static const OSSL_LIB_CTX_METHOD foo_method = {
42     foo_new,
43     foo_free
44 };
45
46 /*
47  * END EXAMPLE
48  * ======================================================================
49  */
50
51 static int test_context(OSSL_LIB_CTX *ctx)
52 {
53     FOO *data = NULL;
54
55     return TEST_ptr(data = ossl_lib_ctx_get_data(ctx, 0, &foo_method))
56         /* OPENSSL_zalloc in foo_new() initialized it to zero */
57         && TEST_int_eq(data->i, 42);
58 }
59
60 static int test_app_context(void)
61 {
62     OSSL_LIB_CTX *ctx = NULL;
63     int result =
64         TEST_ptr(ctx = OSSL_LIB_CTX_new())
65         && test_context(ctx);
66
67     OSSL_LIB_CTX_free(ctx);
68     return result;
69 }
70
71 static int test_def_context(void)
72 {
73     return test_context(NULL);
74 }
75
76 static int test_set0_default(void)
77 {
78     OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default();
79     OSSL_LIB_CTX *local = OSSL_LIB_CTX_new();
80     OSSL_LIB_CTX *prev;
81     int testresult = 0;
82     FOO *data = NULL;
83
84     if (!TEST_ptr(global)
85             || !TEST_ptr(local)
86             || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL))
87             || !TEST_ptr(data = ossl_lib_ctx_get_data(local, 0, &foo_method)))
88         goto err;
89
90     /* Set local "i" value to 43. Global "i" should be 42 */
91     data->i++;
92     if (!TEST_int_eq(data->i, 43))
93         goto err;
94
95     /* The default context should still be the "global" default */
96     if (!TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
97             || !TEST_int_eq(data->i, 42))
98         goto err;
99
100     /* Check we can change the local default context */
101     if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local))
102             || !TEST_ptr_eq(global, prev)
103             || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
104             || !TEST_int_eq(data->i, 43))
105         goto err;
106
107     /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */
108     if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL))
109             || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
110             || !TEST_int_eq(data->i, 43))
111         goto err;
112
113     /* Global default should be unchanged */
114     if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default()))
115         goto err;
116
117     /* Check we can swap back to the global default */
118    if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global))
119             || !TEST_ptr_eq(local, prev)
120             || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
121             || !TEST_int_eq(data->i, 42))
122         goto err;
123
124     testresult = 1;
125  err:
126     OSSL_LIB_CTX_free(local);
127     return testresult;
128 }
129
130 int setup_tests(void)
131 {
132     ADD_TEST(test_app_context);
133     ADD_TEST(test_def_context);
134     ADD_TEST(test_set0_default);
135     return 1;
136 }