1/*
2 * Copyright 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#include <openssl/evp.h>
11#include <openssl/conf.h>
12#include "testutil.h"
13
14static char *configfile = NULL;
15static char *recurseconfigfile = NULL;
16
17/*
18 * Test to make sure there are no leaks or failures from loading the config
19 * file twice.
20 */
21static int test_double_config(void)
22{
23    OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
24    int testresult = 0;
25    EVP_MD *sha256 = NULL;
26
27    if (!TEST_ptr(configfile))
28        return 0;
29    if (!TEST_ptr(ctx))
30        return 0;
31
32    if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
33        return 0;
34    if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
35        return 0;
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
49static 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(recurseconfigfile))
56        goto err;
57
58    if (!TEST_ptr(ctx))
59        goto err;
60
61    if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile)))
62        goto err;
63
64    err = ERR_peek_error();
65    /* We expect to get a recursion error here */
66    if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE)
67        testresult = 1;
68 err:
69    OSSL_LIB_CTX_free(ctx);
70    return testresult;
71}
72
73OPT_TEST_DECLARE_USAGE("configfile\n")
74
75int setup_tests(void)
76{
77    if (!test_skip_common_options()) {
78        TEST_error("Error parsing test options\n");
79        return 0;
80    }
81
82    if (!TEST_ptr(configfile = test_get_argument(0)))
83        return 0;
84
85    if (!TEST_ptr(recurseconfigfile = test_get_argument(1)))
86        return 0;
87
88    ADD_TEST(test_recursive_config);
89    ADD_TEST(test_double_config);
90    return 1;
91}
92