1 /*
2 * Copyright 2021-2024 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 #include <sys/stat.h>
11 #include <openssl/evp.h>
12 #include <openssl/conf.h>
13 #include "testutil.h"
14
15 static char *configfile = NULL;
16 static char *recurseconfigfile = NULL;
17 static char *pathedconfig = NULL;
18
19 /*
20 * Test to make sure there are no leaks or failures from loading the config
21 * file twice.
22 */
test_double_config(void)23 static int test_double_config(void)
24 {
25 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
26 int testresult = 0;
27 EVP_MD *sha256 = NULL;
28
29 if (!TEST_ptr(ctx))
30 return 0;
31
32 if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
33 goto err;
34 if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
35 goto err;
36
37 /* Check we can actually fetch something */
38 sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
39 if (!TEST_ptr(sha256))
40 goto err;
41
42 testresult = 1;
43 err:
44 EVP_MD_free(sha256);
45 OSSL_LIB_CTX_free(ctx);
46 return testresult;
47 }
48
test_recursive_config(void)49 static int test_recursive_config(void)
50 {
51 OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
52 int testresult = 0;
53 unsigned long err;
54
55 if (!TEST_ptr(ctx))
56 goto err;
57
58 if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile)))
59 goto err;
60
61 err = ERR_peek_error();
62 /* We expect to get a recursion error here */
63 if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE)
64 testresult = 1;
65 err:
66 OSSL_LIB_CTX_free(ctx);
67 return testresult;
68 }
69
70 #define P_TEST_PATH "/../test/p_test.so"
test_path_config(void)71 static int test_path_config(void)
72 {
73 OSSL_LIB_CTX *ctx = NULL;
74 OSSL_PROVIDER *prov;
75 int testresult = 0;
76 struct stat sbuf;
77 char *module_path = getenv("OPENSSL_MODULES");
78 char *full_path = NULL;
79 int rc;
80
81 if (!TEST_ptr(module_path))
82 return 0;
83
84 full_path = OPENSSL_zalloc(strlen(module_path) + strlen(P_TEST_PATH) + 1);
85 if (!TEST_ptr(full_path))
86 return 0;
87
88 strcpy(full_path, module_path);
89 full_path = strcat(full_path, P_TEST_PATH);
90 TEST_info("full path is %s", full_path);
91 rc = stat(full_path, &sbuf);
92 OPENSSL_free(full_path);
93 if (rc == -1)
94 return TEST_skip("Skipping modulepath test as provider not present");
95
96 if (!TEST_ptr(pathedconfig))
97 return 0;
98
99 ctx = OSSL_LIB_CTX_new();
100 if (!TEST_ptr(ctx))
101 return 0;
102
103 if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, pathedconfig)))
104 goto err;
105
106 /* attempt to manually load the test provider */
107 if (!TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "test")))
108 goto err;
109
110 OSSL_PROVIDER_unload(prov);
111
112 testresult = 1;
113 err:
114 OSSL_LIB_CTX_free(ctx);
115 return testresult;
116 }
117
118 OPT_TEST_DECLARE_USAGE("configfile\n")
119
setup_tests(void)120 int setup_tests(void)
121 {
122 if (!test_skip_common_options()) {
123 TEST_error("Error parsing test options\n");
124 return 0;
125 }
126
127 if (!TEST_ptr(configfile = test_get_argument(0)))
128 return 0;
129
130 if (!TEST_ptr(recurseconfigfile = test_get_argument(1)))
131 return 0;
132
133 if (!TEST_ptr(pathedconfig = test_get_argument(2)))
134 return 0;
135
136 ADD_TEST(test_recursive_config);
137 ADD_TEST(test_double_config);
138 ADD_TEST(test_path_config);
139 return 1;
140 }
141