From 6d872a838df78518508b5661d98da62a097317b1 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Sat, 30 Mar 2019 22:10:39 +0100 Subject: [PATCH 1/1] Add test for the provider configuration module We reuse test/provider_internal_test.c and test/p_test.c, and get it loaded one more time via the configuration file test/provider_internal_test.conf To support different platform standards regarding module extensions, we generate test/provider_internal_test.conf Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/8549) --- .gitignore | 2 ++ test/build.info | 2 ++ test/p_test.c | 20 ++++++++--- test/provider_internal_test.c | 43 +++++++++++++++++------- test/provider_internal_test.conf.in | 13 +++++++ test/recipes/02-test_internal_provider.t | 5 +-- 6 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 test/provider_internal_test.conf.in diff --git a/.gitignore b/.gitignore index 61c68f4c89..b32122c64a 100644 --- a/.gitignore +++ b/.gitignore @@ -60,6 +60,8 @@ Makefile /test/versions /test/ossl_shim/ossl_shim /test/rsa_complex +# Other generated files in test/ +/test/provider_internal_test.conf # Certain files that get created by tests on the fly /test/test-runs diff --git a/test/build.info b/test/build.info index 973536dc51..25abb068f3 100644 --- a/test/build.info +++ b/test/build.info @@ -616,6 +616,8 @@ IF[{- !$disabled{tests} -}] DEFINE[provider_test]=OPENSSL_NO_MODULE DEFINE[provider_internal_test]=OPENSSL_NO_MODULE ENDIF + DEPEND[]=provider_internal_test.conf + GENERATE[provider_internal_test.conf]=provider_internal_test.conf.in PROGRAMS{noinst}=params_test SOURCE[params_test]=params_test.c diff --git a/test/p_test.c b/test/p_test.c index 9e1ba8edb3..bf13a0a070 100644 --- a/test/p_test.c +++ b/test/p_test.c @@ -52,21 +52,33 @@ static int p_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[]) if (strcmp(p->key, "greeting") == 0) { static char *opensslv = NULL; static char *provname = NULL; + static char *greeting = NULL; static OSSL_PARAM counter_request[] = { + /* Known libcrypto provided parameters */ { "openssl-version", OSSL_PARAM_UTF8_PTR, &opensslv, sizeof(&opensslv), NULL }, { "provider-name", OSSL_PARAM_UTF8_PTR, &provname, sizeof(&provname), NULL}, + + /* This might be present, if there's such a configuration */ + { "greeting", OSSL_PARAM_UTF8_PTR, + &greeting, sizeof(&greeting), NULL }, + { NULL, 0, NULL, 0, NULL } }; char buf[256]; size_t buf_l; if (c_get_params(prov, counter_request)) { - const char *versionp = *(void **)counter_request[0].data; - const char *namep = *(void **)counter_request[1].data; - sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!", - versionp, namep); + if (greeting) { + strcpy(buf, greeting); + } else { + const char *versionp = *(void **)counter_request[0].data; + const char *namep = *(void **)counter_request[1].data; + + sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!", + versionp, namep); + } } else { sprintf(buf, "Howdy stranger..."); } diff --git a/test/provider_internal_test.c b/test/provider_internal_test.c index cbb85c3687..54e6714ab1 100644 --- a/test/provider_internal_test.c +++ b/test/provider_internal_test.c @@ -8,6 +8,7 @@ */ #include +#include #include "internal/provider.h" #include "testutil.h" @@ -20,20 +21,11 @@ static OSSL_PARAM greeting_request[] = { { NULL, 0, NULL, 0, NULL } }; -static int test_provider(OSSL_PROVIDER *prov) +static int test_provider(OSSL_PROVIDER *prov, const char *expected_greeting) { - const char *name = NULL; const char *greeting = NULL; - char expected_greeting[256]; int ret = 0; - if (!TEST_ptr(name = ossl_provider_name(prov))) - return 0; - - BIO_snprintf(expected_greeting, sizeof(expected_greeting), - "Hello OpenSSL %.20s, greetings from %s!", - OPENSSL_VERSION_STR, name); - ret = TEST_true(ossl_provider_activate(prov)) && TEST_true(ossl_provider_get_params(prov, greeting_request)) @@ -41,10 +33,22 @@ static int test_provider(OSSL_PROVIDER *prov) && TEST_size_t_gt(greeting_request[0].data_size, 0) && TEST_str_eq(greeting, expected_greeting); + TEST_info("Got this greeting: %s\n", greeting); ossl_provider_free(prov); return ret; } +static const char *expected_greeting1(const char *name) +{ + static char expected_greeting[256] = ""; + + snprintf(expected_greeting, sizeof(expected_greeting), + "Hello OpenSSL %.20s, greetings from %s!", + OPENSSL_VERSION_STR, name); + + return expected_greeting; +} + static int test_builtin_provider(void) { const char *name = "p_test_builtin"; @@ -53,7 +57,7 @@ static int test_builtin_provider(void) return TEST_ptr(prov = ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME)) - && test_provider(prov); + && test_provider(prov, expected_greeting1(name)); } #ifndef OPENSSL_NO_MODULE @@ -64,7 +68,21 @@ static int test_loaded_provider(void) return TEST_ptr(prov = ossl_provider_new(NULL, name, NULL)) - && test_provider(prov); + && test_provider(prov, expected_greeting1(name)); +} + +static int test_configured_provider(void) +{ + const char *name = "p_test_configured"; + OSSL_PROVIDER *prov = NULL; + /* This MUST match the config file */ + const char *expected_greeting = + "Hello OpenSSL, greetings from Test Provider"; + + return + OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL) + && TEST_ptr(prov = ossl_provider_find(NULL, name)) + && test_provider(prov, expected_greeting); } #endif @@ -73,6 +91,7 @@ int setup_tests(void) ADD_TEST(test_builtin_provider); #ifndef OPENSSL_NO_MODULE ADD_TEST(test_loaded_provider); + ADD_TEST(test_configured_provider); #endif return 1; } diff --git a/test/provider_internal_test.conf.in b/test/provider_internal_test.conf.in new file mode 100644 index 0000000000..12c292437e --- /dev/null +++ b/test/provider_internal_test.conf.in @@ -0,0 +1,13 @@ +{- use platform -} +openssl_conf = openssl_init + +[openssl_init] +providers = providers + +[providers] +p_test_configured = p_test_configured + +[p_test_configured] +module = {- platform->dso('p_test') -} +activate = 1 +greeting = Hello OpenSSL, greetings from Test Provider diff --git a/test/recipes/02-test_internal_provider.t b/test/recipes/02-test_internal_provider.t index 8275eb2725..615d17a8d9 100644 --- a/test/recipes/02-test_internal_provider.t +++ b/test/recipes/02-test_internal_provider.t @@ -7,12 +7,13 @@ # https://www.openssl.org/source/license.html use strict; -use OpenSSL::Test qw(:DEFAULT bldtop_dir); +use OpenSSL::Test qw(:DEFAULT bldtop_dir bldtop_file); use OpenSSL::Test::Simple; use OpenSSL::Test::Utils; setup("test_internal_provider"); -$ENV{"OPENSSL_MODULES"} = bldtop_dir("test"); +$ENV{OPENSSL_MODULES} = bldtop_dir("test"); +$ENV{OPENSSL_CONF} = bldtop_file("test", "provider_internal_test.conf"); simple_test("test_internal_provider", "provider_internal_test"); -- 2.34.1