1*e0c4386eSCy Schubert /* 2*e0c4386eSCy Schubert * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. 3*e0c4386eSCy Schubert * 4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use 5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy 6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at 7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html 8*e0c4386eSCy Schubert */ 9*e0c4386eSCy Schubert 10*e0c4386eSCy Schubert #include <openssl/evp.h> 11*e0c4386eSCy Schubert #include <openssl/conf.h> 12*e0c4386eSCy Schubert #include "testutil.h" 13*e0c4386eSCy Schubert 14*e0c4386eSCy Schubert static char *configfile = NULL; 15*e0c4386eSCy Schubert static char *recurseconfigfile = NULL; 16*e0c4386eSCy Schubert 17*e0c4386eSCy Schubert /* 18*e0c4386eSCy Schubert * Test to make sure there are no leaks or failures from loading the config 19*e0c4386eSCy Schubert * file twice. 20*e0c4386eSCy Schubert */ 21*e0c4386eSCy Schubert static int test_double_config(void) 22*e0c4386eSCy Schubert { 23*e0c4386eSCy Schubert OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); 24*e0c4386eSCy Schubert int testresult = 0; 25*e0c4386eSCy Schubert EVP_MD *sha256 = NULL; 26*e0c4386eSCy Schubert 27*e0c4386eSCy Schubert if (!TEST_ptr(configfile)) 28*e0c4386eSCy Schubert return 0; 29*e0c4386eSCy Schubert if (!TEST_ptr(ctx)) 30*e0c4386eSCy Schubert return 0; 31*e0c4386eSCy Schubert 32*e0c4386eSCy Schubert if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile))) 33*e0c4386eSCy Schubert return 0; 34*e0c4386eSCy Schubert if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile))) 35*e0c4386eSCy Schubert return 0; 36*e0c4386eSCy Schubert 37*e0c4386eSCy Schubert /* Check we can actually fetch something */ 38*e0c4386eSCy Schubert sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL); 39*e0c4386eSCy Schubert if (!TEST_ptr(sha256)) 40*e0c4386eSCy Schubert goto err; 41*e0c4386eSCy Schubert 42*e0c4386eSCy Schubert testresult = 1; 43*e0c4386eSCy Schubert err: 44*e0c4386eSCy Schubert EVP_MD_free(sha256); 45*e0c4386eSCy Schubert OSSL_LIB_CTX_free(ctx); 46*e0c4386eSCy Schubert return testresult; 47*e0c4386eSCy Schubert } 48*e0c4386eSCy Schubert 49*e0c4386eSCy Schubert static int test_recursive_config(void) 50*e0c4386eSCy Schubert { 51*e0c4386eSCy Schubert OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); 52*e0c4386eSCy Schubert int testresult = 0; 53*e0c4386eSCy Schubert unsigned long err; 54*e0c4386eSCy Schubert 55*e0c4386eSCy Schubert if (!TEST_ptr(recurseconfigfile)) 56*e0c4386eSCy Schubert goto err; 57*e0c4386eSCy Schubert 58*e0c4386eSCy Schubert if (!TEST_ptr(ctx)) 59*e0c4386eSCy Schubert goto err; 60*e0c4386eSCy Schubert 61*e0c4386eSCy Schubert if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile))) 62*e0c4386eSCy Schubert goto err; 63*e0c4386eSCy Schubert 64*e0c4386eSCy Schubert err = ERR_peek_error(); 65*e0c4386eSCy Schubert /* We expect to get a recursion error here */ 66*e0c4386eSCy Schubert if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE) 67*e0c4386eSCy Schubert testresult = 1; 68*e0c4386eSCy Schubert err: 69*e0c4386eSCy Schubert OSSL_LIB_CTX_free(ctx); 70*e0c4386eSCy Schubert return testresult; 71*e0c4386eSCy Schubert } 72*e0c4386eSCy Schubert 73*e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("configfile\n") 74*e0c4386eSCy Schubert 75*e0c4386eSCy Schubert int setup_tests(void) 76*e0c4386eSCy Schubert { 77*e0c4386eSCy Schubert if (!test_skip_common_options()) { 78*e0c4386eSCy Schubert TEST_error("Error parsing test options\n"); 79*e0c4386eSCy Schubert return 0; 80*e0c4386eSCy Schubert } 81*e0c4386eSCy Schubert 82*e0c4386eSCy Schubert if (!TEST_ptr(configfile = test_get_argument(0))) 83*e0c4386eSCy Schubert return 0; 84*e0c4386eSCy Schubert 85*e0c4386eSCy Schubert if (!TEST_ptr(recurseconfigfile = test_get_argument(1))) 86*e0c4386eSCy Schubert return 0; 87*e0c4386eSCy Schubert 88*e0c4386eSCy Schubert ADD_TEST(test_recursive_config); 89*e0c4386eSCy Schubert ADD_TEST(test_double_config); 90*e0c4386eSCy Schubert return 1; 91*e0c4386eSCy Schubert } 92