1e0c4386eSCy Schubert /*
244096ebdSEnji Cooper * Copyright 2021-2024 The OpenSSL Project Authors. All Rights Reserved.
3e0c4386eSCy Schubert *
4e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8e0c4386eSCy Schubert */
9e0c4386eSCy Schubert
1044096ebdSEnji Cooper #include <sys/stat.h>
11e0c4386eSCy Schubert #include <openssl/evp.h>
12e0c4386eSCy Schubert #include <openssl/conf.h>
13e0c4386eSCy Schubert #include "testutil.h"
14e0c4386eSCy Schubert
15e0c4386eSCy Schubert static char *configfile = NULL;
16e0c4386eSCy Schubert static char *recurseconfigfile = NULL;
1744096ebdSEnji Cooper static char *pathedconfig = NULL;
18e0c4386eSCy Schubert
19e0c4386eSCy Schubert /*
20e0c4386eSCy Schubert * Test to make sure there are no leaks or failures from loading the config
21e0c4386eSCy Schubert * file twice.
22e0c4386eSCy Schubert */
test_double_config(void)23e0c4386eSCy Schubert static int test_double_config(void)
24e0c4386eSCy Schubert {
25e0c4386eSCy Schubert OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
26e0c4386eSCy Schubert int testresult = 0;
27e0c4386eSCy Schubert EVP_MD *sha256 = NULL;
28e0c4386eSCy Schubert
29e0c4386eSCy Schubert if (!TEST_ptr(ctx))
30e0c4386eSCy Schubert return 0;
31e0c4386eSCy Schubert
32e0c4386eSCy Schubert if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
33*a7148ab3SEnji Cooper goto err;
34e0c4386eSCy Schubert if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
35*a7148ab3SEnji Cooper goto err;
36e0c4386eSCy Schubert
37e0c4386eSCy Schubert /* Check we can actually fetch something */
38e0c4386eSCy Schubert sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
39e0c4386eSCy Schubert if (!TEST_ptr(sha256))
40e0c4386eSCy Schubert goto err;
41e0c4386eSCy Schubert
42e0c4386eSCy Schubert testresult = 1;
43e0c4386eSCy Schubert err:
44e0c4386eSCy Schubert EVP_MD_free(sha256);
45e0c4386eSCy Schubert OSSL_LIB_CTX_free(ctx);
46e0c4386eSCy Schubert return testresult;
47e0c4386eSCy Schubert }
48e0c4386eSCy Schubert
test_recursive_config(void)49e0c4386eSCy Schubert static int test_recursive_config(void)
50e0c4386eSCy Schubert {
51e0c4386eSCy Schubert OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
52e0c4386eSCy Schubert int testresult = 0;
53e0c4386eSCy Schubert unsigned long err;
54e0c4386eSCy Schubert
55e0c4386eSCy Schubert if (!TEST_ptr(ctx))
56e0c4386eSCy Schubert goto err;
57e0c4386eSCy Schubert
58e0c4386eSCy Schubert if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile)))
59e0c4386eSCy Schubert goto err;
60e0c4386eSCy Schubert
61e0c4386eSCy Schubert err = ERR_peek_error();
62e0c4386eSCy Schubert /* We expect to get a recursion error here */
63e0c4386eSCy Schubert if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE)
64e0c4386eSCy Schubert testresult = 1;
65e0c4386eSCy Schubert err:
66e0c4386eSCy Schubert OSSL_LIB_CTX_free(ctx);
67e0c4386eSCy Schubert return testresult;
68e0c4386eSCy Schubert }
69e0c4386eSCy Schubert
7044096ebdSEnji Cooper #define P_TEST_PATH "/../test/p_test.so"
test_path_config(void)7144096ebdSEnji Cooper static int test_path_config(void)
7244096ebdSEnji Cooper {
7344096ebdSEnji Cooper OSSL_LIB_CTX *ctx = NULL;
7444096ebdSEnji Cooper OSSL_PROVIDER *prov;
7544096ebdSEnji Cooper int testresult = 0;
7644096ebdSEnji Cooper struct stat sbuf;
7744096ebdSEnji Cooper char *module_path = getenv("OPENSSL_MODULES");
7844096ebdSEnji Cooper char *full_path = NULL;
7944096ebdSEnji Cooper int rc;
8044096ebdSEnji Cooper
8144096ebdSEnji Cooper if (!TEST_ptr(module_path))
8244096ebdSEnji Cooper return 0;
8344096ebdSEnji Cooper
8444096ebdSEnji Cooper full_path = OPENSSL_zalloc(strlen(module_path) + strlen(P_TEST_PATH) + 1);
8544096ebdSEnji Cooper if (!TEST_ptr(full_path))
8644096ebdSEnji Cooper return 0;
8744096ebdSEnji Cooper
8844096ebdSEnji Cooper strcpy(full_path, module_path);
8944096ebdSEnji Cooper full_path = strcat(full_path, P_TEST_PATH);
9044096ebdSEnji Cooper TEST_info("full path is %s", full_path);
9144096ebdSEnji Cooper rc = stat(full_path, &sbuf);
9244096ebdSEnji Cooper OPENSSL_free(full_path);
9344096ebdSEnji Cooper if (rc == -1)
9444096ebdSEnji Cooper return TEST_skip("Skipping modulepath test as provider not present");
9544096ebdSEnji Cooper
9644096ebdSEnji Cooper if (!TEST_ptr(pathedconfig))
9744096ebdSEnji Cooper return 0;
9844096ebdSEnji Cooper
9944096ebdSEnji Cooper ctx = OSSL_LIB_CTX_new();
10044096ebdSEnji Cooper if (!TEST_ptr(ctx))
10144096ebdSEnji Cooper return 0;
10244096ebdSEnji Cooper
10344096ebdSEnji Cooper if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, pathedconfig)))
10444096ebdSEnji Cooper goto err;
10544096ebdSEnji Cooper
10644096ebdSEnji Cooper /* attempt to manually load the test provider */
10744096ebdSEnji Cooper if (!TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "test")))
10844096ebdSEnji Cooper goto err;
10944096ebdSEnji Cooper
11044096ebdSEnji Cooper OSSL_PROVIDER_unload(prov);
11144096ebdSEnji Cooper
11244096ebdSEnji Cooper testresult = 1;
11344096ebdSEnji Cooper err:
11444096ebdSEnji Cooper OSSL_LIB_CTX_free(ctx);
11544096ebdSEnji Cooper return testresult;
11644096ebdSEnji Cooper }
11744096ebdSEnji Cooper
118e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("configfile\n")
119e0c4386eSCy Schubert
setup_tests(void)120e0c4386eSCy Schubert int setup_tests(void)
121e0c4386eSCy Schubert {
122e0c4386eSCy Schubert if (!test_skip_common_options()) {
123e0c4386eSCy Schubert TEST_error("Error parsing test options\n");
124e0c4386eSCy Schubert return 0;
125e0c4386eSCy Schubert }
126e0c4386eSCy Schubert
127e0c4386eSCy Schubert if (!TEST_ptr(configfile = test_get_argument(0)))
128e0c4386eSCy Schubert return 0;
129e0c4386eSCy Schubert
130e0c4386eSCy Schubert if (!TEST_ptr(recurseconfigfile = test_get_argument(1)))
131e0c4386eSCy Schubert return 0;
132e0c4386eSCy Schubert
13344096ebdSEnji Cooper if (!TEST_ptr(pathedconfig = test_get_argument(2)))
13444096ebdSEnji Cooper return 0;
13544096ebdSEnji Cooper
136e0c4386eSCy Schubert ADD_TEST(test_recursive_config);
137e0c4386eSCy Schubert ADD_TEST(test_double_config);
13844096ebdSEnji Cooper ADD_TEST(test_path_config);
139e0c4386eSCy Schubert return 1;
140e0c4386eSCy Schubert }
141