testutil: Add provider.c with test_get_libctx(), to use at least for SSL and CMP

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/11808)
This commit is contained in:
Dr. David von Oheimb 2020-08-12 07:46:57 +02:00
parent 06cee80a84
commit 1bb6f70da3
4 changed files with 82 additions and 41 deletions

View File

@ -22,7 +22,7 @@ IF[{- !$disabled{tests} -}]
testutil/driver.c testutil/tests.c testutil/cb.c testutil/stanza.c \
testutil/format_output.c \
testutil/test_cleanup.c testutil/main.c testutil/testutil_init.c \
testutil/options.c testutil/test_options.c \
testutil/options.c testutil/test_options.c testutil/provider.c \
testutil/apps_mem.c testutil/random.c $LIBAPPSSRC
INCLUDE[libtestutil.a]=../include ../apps/include ..
DEPEND[libtestutil.a]=../libcrypto

View File

@ -513,12 +513,12 @@ err:
return ret;
}
OPT_TEST_DECLARE_USAGE("conf_file modulename [fips_conf_file]\n")
#define USAGE "conf_file module_name [module_conf_file]\n"
OPT_TEST_DECLARE_USAGE(USAGE)
int setup_tests(void)
{
long num_tests;
const char *modulename;
if (!test_skip_common_options()) {
TEST_error("Error parsing test options\n");
@ -529,29 +529,14 @@ int setup_tests(void)
/* argv[1] should point to the test conf file */
|| !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)
|| !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests",
&num_tests), 0))
&num_tests), 0)) {
TEST_error("usage: ssl_test %s", USAGE);
return 0;
if (!TEST_ptr(modulename = test_get_argument(1)))
return 0;
if (strcmp(modulename, "none") != 0) {
const char *configfile = test_get_argument(2);
defctxnull = OSSL_PROVIDER_load(NULL, "null");
libctx = OPENSSL_CTX_new();
if (!TEST_ptr(libctx))
return 0;
if (configfile != NULL
&& !TEST_true(OPENSSL_CTX_load_config(libctx, configfile)))
return 0;
thisprov = OSSL_PROVIDER_load(libctx, modulename);
if (!TEST_ptr(thisprov))
return 0;
}
if (!test_get_libctx(&libctx, &defctxnull, &thisprov, 1, USAGE))
return 0;
ADD_ALL_TESTS(test_handshake, (int)num_tests);
return 1;
}

View File

@ -10,12 +10,13 @@
#ifndef OSSL_TESTUTIL_H
# define OSSL_TESTUTIL_H
#include <stdarg.h>
# include <stdarg.h>
#include <openssl/err.h>
#include <openssl/e_os2.h>
#include <openssl/bn.h>
#include "opt.h"
# include <openssl/provider.h>
# include <openssl/err.h>
# include <openssl/e_os2.h>
# include <openssl/bn.h>
# include "opt.h"
/*-
* Simple unit tests should implement setup_tests().
@ -125,7 +126,7 @@
/* The default test enum which should be common to all tests */
#define OPT_TEST_ENUM \
# define OPT_TEST_ENUM \
OPT_TEST_HELP = 500, \
OPT_TEST_LIST, \
OPT_TEST_SINGLE, \
@ -134,7 +135,7 @@
OPT_TEST_SEED
/* The Default test OPTIONS common to all tests (without a usage string) */
#define OPT_TEST_OPTIONS \
# define OPT_TEST_OPTIONS \
{ OPT_HELP_STR, 1, '-', "Valid options are:\n" }, \
{ "help", OPT_TEST_HELP, '-', "Display this summary" }, \
{ "list", OPT_TEST_LIST, '-', "Display the list of tests available" }, \
@ -144,12 +145,12 @@
{ "seed", OPT_TEST_SEED, 'n', "Seed value to randomize tests with" }
/* The Default test OPTIONS common to all tests starting with an additional usage string */
#define OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage) \
# define OPT_TEST_OPTIONS_WITH_EXTRA_USAGE(usage) \
{ OPT_HELP_STR, 1, '-', "Usage: %s [options] " usage }, \
OPT_TEST_OPTIONS
/* The Default test OPTIONS common to all tests with an default usage string */
#define OPT_TEST_OPTIONS_DEFAULT_USAGE \
# define OPT_TEST_OPTIONS_DEFAULT_USAGE \
{ OPT_HELP_STR, 1, '-', "Usage: %s [options]\n" }, \
OPT_TEST_OPTIONS
@ -157,7 +158,7 @@
* Optional Cases that need to be ignored by the test app when using opt_next(),
* (that are handled internally).
*/
#define OPT_TEST_CASES \
# define OPT_TEST_CASES \
OPT_TEST_HELP: \
case OPT_TEST_LIST: \
case OPT_TEST_SINGLE: \
@ -179,7 +180,7 @@
* well as the additional options that need to be handled.
* (3) case OPT_TEST_CASES: break; inside the opt_next() handling code.
*/
#define OPT_TEST_DECLARE_USAGE(usage_str) \
# define OPT_TEST_DECLARE_USAGE(usage_str) \
const OPTIONS *test_get_options(void) \
{ \
enum { OPT_TEST_ENUM }; \
@ -204,6 +205,10 @@ size_t test_get_argument_count(void);
*/
int test_skip_common_options(void);
int test_get_libctx(OPENSSL_CTX **libctx,
OSSL_PROVIDER **default_null_provider,
OSSL_PROVIDER **provider, int argn, const char *usage);
/*
* Internal helpers. Test programs shouldn't use these directly, but should
* rather link to one of the helper main() methods.
@ -235,17 +240,17 @@ const OPTIONS *test_get_options(void);
* Test assumption verification helpers.
*/
#define PRINTF_FORMAT(a, b)
#if defined(__GNUC__) && defined(__STDC_VERSION__)
# define PRINTF_FORMAT(a, b)
# if defined(__GNUC__) && defined(__STDC_VERSION__)
/*
* Because we support the 'z' modifier, which made its appearance in C99,
* we can't use __attribute__ with pre C99 dialects.
*/
# if __STDC_VERSION__ >= 199901L
# undef PRINTF_FORMAT
# define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
# if __STDC_VERSION__ >= 199901L
# undef PRINTF_FORMAT
# define PRINTF_FORMAT(a, b) __attribute__ ((format(printf, a, b)))
# endif
# endif
#endif
# define DECLARE_COMPARISON(type, name, opname) \
int test_ ## name ## _ ## opname(const char *, int, \
@ -503,7 +508,7 @@ void test_output_memory(const char *name, const unsigned char *m, size_t l);
/*
* Utilities to parse a test file.
*/
#define TESTMAXPAIRS 150
# define TESTMAXPAIRS 150
typedef struct pair_st {
char *key;

51
test/testutil/provider.c Normal file
View File

@ -0,0 +1,51 @@
/*
* Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "../testutil.h"
#include <openssl/provider.h>
#include <string.h>
int test_get_libctx(OPENSSL_CTX **libctx,
OSSL_PROVIDER **default_null_provider,
OSSL_PROVIDER **provider, int argn, const char *usage)
{
const char *module_name;
if (!TEST_ptr(module_name = test_get_argument(argn))) {
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 = OPENSSL_CTX_new();
if (!TEST_ptr(*libctx)) {
TEST_error("Failed to create libctx\n");
goto err;
}
if (config_fname != NULL
&& !TEST_true(OPENSSL_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;
}