Use adapted test_get_libctx() for simpler test setup and better error reporting

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/13001)
This commit is contained in:
Dr. David von Oheimb 2020-09-26 15:21:48 +02:00
parent 5ea64b456b
commit bca7ad6efd
14 changed files with 58 additions and 97 deletions

View File

@ -1434,18 +1434,9 @@ int setup_tests(void)
}
}
prov_null = OSSL_PROVIDER_load(NULL, "null");
if (prov_null == NULL) {
opt_printf_stderr("Failed to load null provider into default libctx\n");
if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
return 0;
}
libctx = OSSL_LIB_CTX_new();
if (libctx == NULL
|| !OSSL_LIB_CTX_load_config(libctx, config_file)) {
opt_printf_stderr("Failed to load config\n");
return 0;
}
OSSL_SELF_TEST_set_callback(libctx, self_test_events, &self_test_args);
ADD_ALL_TESTS(cipher_enc_dec_test, OSSL_NELEM(cipher_enc_data));

View File

@ -706,7 +706,7 @@ IF[{- !$disabled{tests} -}]
SOURCE[ssl_old_test]=ssl_old_test.c helpers/predefined_dhparams.c
INCLUDE[ssl_old_test]=.. ../include ../apps/include
DEPEND[ssl_old_test]=../libcrypto.a ../libssl.a
DEPEND[ssl_old_test]=../libcrypto.a ../libssl.a libtestutil.a
ENDIF
PROGRAMS{noinst}=asn1_time_test

View File

@ -366,7 +366,7 @@ int setup_tests(void)
return 0;
}
if (!test_get_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
return 0;
if (!TEST_ptr(server_key = load_pem_key(server_key_f, libctx))

View File

@ -561,7 +561,7 @@ int setup_tests(void)
return 0;
}
if (!test_get_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
return 0;
if (!TEST_ptr(newkey = load_pem_key(newkey_f, libctx))

View File

@ -538,7 +538,7 @@ int setup_tests(void)
return 0;
}
if (!test_get_libctx(&libctx, &default_null_provider, &provider, 10, USAGE))
if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 10, USAGE))
return 0;
if (!TEST_ptr(loadedkey = load_pem_key(server_key_f, libctx))

View File

@ -145,7 +145,7 @@ int setup_tests(void)
return 0;
}
if (!test_get_libctx(&libctx, &default_null_provider, &provider, 1, USAGE))
if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 1, USAGE))
return 0;
if (!TEST_ptr(request = load_pkimsg(request_f))) {

View File

@ -600,7 +600,7 @@ int setup_tests(void)
return 0;
}
if (!test_get_libctx(&libctx, &default_null_provider, &provider, 14, USAGE))
if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 14, USAGE))
return 0;
/* Load certificates for cert chain */

View File

@ -272,13 +272,7 @@ static int test_d2i_PrivateKey_ex(void) {
int setup_tests(void)
{
mainctx = OSSL_LIB_CTX_new();
if (!TEST_ptr(mainctx))
return 0;
nullprov = OSSL_PROVIDER_load(NULL, "null");
if (!TEST_ptr(nullprov)) {
if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) {
OSSL_LIB_CTX_free(mainctx);
mainctx = NULL;
return 0;

View File

@ -648,19 +648,7 @@ int setup_tests(void)
}
}
nullprov = OSSL_PROVIDER_load(NULL, "null");
if (!TEST_ptr(nullprov))
return 0;
libctx = OSSL_LIB_CTX_new();
if (!TEST_ptr(libctx))
return 0;
if (config_file != NULL
&& !TEST_true(OSSL_LIB_CTX_load_config(libctx, config_file)))
return 0;
libprov = OSSL_PROVIDER_load(libctx, prov_name);
if (!TEST_ptr(libprov))
if (!test_get_libctx(&libctx, &nullprov, config_file, &libprov, prov_name))
return 0;
#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH)

View File

@ -3590,22 +3590,12 @@ int setup_tests(void)
}
/*
* Load the provider via configuration into the created library context.
* Load the 'null' provider into the default library context to ensure that
* the the tests do not fallback to using the default provider.
*/
prov_null = OSSL_PROVIDER_load(NULL, "null");
if (prov_null == NULL) {
opt_printf_stderr("Failed to load null provider into default libctx\n");
if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
return 0;
}
/* load the provider via configuration into the created library context */
libctx = OSSL_LIB_CTX_new();
if (libctx == NULL
|| !OSSL_LIB_CTX_load_config(libctx, config_file)) {
TEST_error("Failed to load config %s\n", config_file);
return 0;
}
n = test_get_argument_count();
if (n == 0)

View File

@ -57,6 +57,7 @@
# include <openssl/ct.h>
#endif
#include <openssl/provider.h>
#include "testutil.h"
/*
* Or gethostname won't be declared properly
@ -103,7 +104,6 @@ static unsigned int psk_server_callback(SSL *ssl, const char *identity,
unsigned int max_psk_len);
#endif
static BIO *bio_err = NULL;
static BIO *bio_stdout = NULL;
#ifndef OPENSSL_NO_NEXTPROTONEG
@ -1344,22 +1344,9 @@ int main(int argc, char *argv[])
}
#endif
if (provider != NULL) {
defctxnull = OSSL_PROVIDER_load(NULL, "null");
if (defctxnull == NULL)
goto end;
libctx = OSSL_LIB_CTX_new();
if (libctx == NULL)
goto end;
if (config != NULL
&& !OSSL_LIB_CTX_load_config(libctx, config))
goto end;
thisprov = OSSL_PROVIDER_load(libctx, provider);
if (thisprov == NULL)
goto end;
}
if (provider != NULL
&& !test_get_libctx(&libctx, &defctxnull, config, &thisprov, provider))
goto end;
c_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
s_ctx = SSL_CTX_new_ex(libctx, NULL, meth);

View File

@ -530,7 +530,7 @@ int setup_tests(void)
return 0;
}
if (!test_get_libctx(&libctx, &defctxnull, &thisprov, 1, USAGE))
if (!test_arg_libctx(&libctx, &defctxnull, &thisprov, 1, USAGE))
return 0;
ADD_ALL_TESTS(test_handshake, (int)num_tests);

View File

@ -205,8 +205,10 @@ size_t test_get_argument_count(void);
*/
int test_skip_common_options(void);
int test_get_libctx(OSSL_LIB_CTX **libctx,
OSSL_PROVIDER **default_null_provider,
int test_get_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
const char *config_file,
OSSL_PROVIDER **provider, const char *module_name);
int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
OSSL_PROVIDER **provider, int argn, const char *usage);
/*

View File

@ -11,8 +11,40 @@
#include <openssl/provider.h>
#include <string.h>
int test_get_libctx(OSSL_LIB_CTX **libctx,
OSSL_PROVIDER **default_null_provider,
int test_get_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
const char *config_file,
OSSL_PROVIDER **provider, const char *module_name)
{
if ((*libctx = OSSL_LIB_CTX_new()) == NULL) {
opt_printf_stderr("Failed to create libctx\n");
goto err;
}
if (default_null_prov != NULL
&& (*default_null_prov = OSSL_PROVIDER_load(NULL, "null")) == NULL) {
opt_printf_stderr("Failed to load null provider into default libctx\n");
goto err;
}
if (config_file != NULL
&& !OSSL_LIB_CTX_load_config(*libctx, config_file)) {
opt_printf_stderr("Error loading config from file %s\n", config_file);
goto err;
}
if (module_name != NULL
&& (*provider = OSSL_PROVIDER_load(*libctx, module_name)) == NULL) {
opt_printf_stderr("Failed to load provider %s\n", module_name);
goto err;
}
return 1;
err:
ERR_print_errors_fp(stderr);
return 0;
}
int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
OSSL_PROVIDER **provider, int argn, const char *usage)
{
const char *module_name;
@ -21,31 +53,8 @@ int test_get_libctx(OSSL_LIB_CTX **libctx,
TEST_error("usage: <prog> %s", usage);
return 0;
}
if (strcmp(module_name, "none") != 0) {
const char *config_fname = test_get_argument(argn + 1);
*default_null_provider = OSSL_PROVIDER_load(NULL, "null");
*libctx = OSSL_LIB_CTX_new();
if (!TEST_ptr(*libctx)) {
TEST_error("Failed to create libctx\n");
goto err;
}
if (config_fname != NULL
&& !TEST_true(OSSL_LIB_CTX_load_config(*libctx, config_fname))) {
TEST_error("Error loading config file %s\n", config_fname);
goto err;
}
*provider = OSSL_PROVIDER_load(*libctx, module_name);
if (!TEST_ptr(*provider)) {
TEST_error("Failed to load provider %s\n", module_name);
goto err;
}
}
return 1;
err:
ERR_print_errors_fp(stderr);
return 0;
if (strcmp(module_name, "none") == 0)
return 1;
return test_get_libctx(libctx, default_null_prov,
test_get_argument(argn + 1), provider, module_name);
}