From 6598f4d32b36f6e2f9f8b4cc750187dd2b36dbca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veronika=20Hanul=C3=ADkov=C3=A1?= Date: Thu, 12 Oct 2023 14:38:06 +0200 Subject: [PATCH] Add config tests for including provider config files --- test/conf_include_test.c | 78 +++++++++++++++++-- test/recipes/90-test_includes.t | 14 +++- .../conf-includes-prov/inc-default.cnf | 5 ++ .../conf-includes-prov/inc-legacy.cnf | 5 ++ .../includes-prov-dir.cnf | 17 ++++ 5 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 test/recipes/90-test_includes_data/conf-includes-prov/inc-default.cnf create mode 100644 test/recipes/90-test_includes_data/conf-includes-prov/inc-legacy.cnf create mode 100644 test/recipes/90-test_includes_data/includes-prov-dir.cnf diff --git a/test/conf_include_test.c b/test/conf_include_test.c index 2481a2380b7643..1e2eba8a57e4fa 100644 --- a/test/conf_include_test.c +++ b/test/conf_include_test.c @@ -36,29 +36,34 @@ # define DIRSEP_PRESERVE 0 #endif +static OSSL_LIB_CTX *libctx = NULL; +static char *rel_conf_file = NULL; + /* changes path to that of the filename */ -static int change_path(const char *file) +static char * change_path(const char *file) { char *s = OPENSSL_strdup(file); char *p = s; char *last = NULL; int ret = 0; + char *new_config_name = NULL; if (s == NULL) - return -1; + return NULL; while ((p = strpbrk(p, DIRSEP)) != NULL) { last = p++; } if (last == NULL) goto err; + last[DIRSEP_PRESERVE] = 0; - - TEST_note("changing path to %s", s); ret = chdir(s); + if (ret == 0) + new_config_name = strdup(last + DIRSEP_PRESERVE + 1); err: OPENSSL_free(s); - return ret; + return new_config_name; } /* @@ -68,6 +73,7 @@ static int change_path(const char *file) static CONF *conf; static BIO *in; static int expect_failure = 0; +static int test_providers = 0; static int test_load_config(void) { @@ -174,10 +180,60 @@ static int test_check_overflow(void) return 1; } +static int test_activated_providers(void) +{ + long val = 0; + + if (!TEST_int_eq(NCONF_get_number(conf, "null_sect", "activate", &val), 1) + || !TEST_int_eq(val, 1)) { + TEST_note("null provider not activated"); + return 0; + } + if (!TEST_int_eq(NCONF_get_number(conf, "default_sect", "activate", &val), 1) + || !TEST_int_eq(val, 1)) { + TEST_note("default provider not activated"); + return 0; + } + if (!TEST_int_eq(NCONF_get_number(conf, "legacy_sect", "activate", &val), 1) + || !TEST_int_eq(val, 1)) { + TEST_note("legacy provider not activated"); + return 0; + } + + return 1; +} + +static int test_available_providers(void) +{ + libctx = OSSL_LIB_CTX_new(); + if (!TEST_ptr(libctx)) + return 0; + + if (!TEST_ptr(rel_conf_file) || !OSSL_LIB_CTX_load_config(libctx, rel_conf_file)) { + opt_printf_stderr("Failed to load config\n"); + return 0; + } + + if (OSSL_PROVIDER_available(libctx, "null") != 1) { + opt_printf_stderr("Null provider is missing\n"); + return 0; + } + if (OSSL_PROVIDER_available(libctx, "default") != 1) { + opt_printf_stderr("Default provider is missing\n"); + return 0; + } + if (OSSL_PROVIDER_available(libctx, "legacy") != 1) { + opt_printf_stderr("Legacy provider is missing\n"); + return 0; + } + return 1; +} + typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_FAIL, + OPT_TEST_PROV, OPT_TEST_ENUM } OPTION_CHOICE; @@ -186,6 +242,7 @@ const OPTIONS *test_get_options(void) static const OPTIONS test_options[] = { OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("conf_file\n"), { "f", OPT_FAIL, '-', "A failure is expected" }, + { "providers", OPT_TEST_PROV, '-', "Test for activated default and legacy providers"}, { NULL } }; return test_options; @@ -193,7 +250,7 @@ const OPTIONS *test_get_options(void) int setup_tests(void) { - const char *conf_file; + char *conf_file = NULL; OPTION_CHOICE o; if (!TEST_ptr(conf = NCONF_new(NULL))) @@ -204,6 +261,8 @@ int setup_tests(void) case OPT_FAIL: expect_failure = 1; break; + case OPT_TEST_PROV: + test_providers = 1; case OPT_TEST_CASES: break; default: @@ -222,16 +281,21 @@ int setup_tests(void) * For this test we need to chdir as we use relative * path names in the config files. */ - change_path(conf_file); + rel_conf_file = change_path(conf_file); ADD_TEST(test_load_config); ADD_TEST(test_check_null_numbers); ADD_TEST(test_check_overflow); + if (test_providers != 0) { + ADD_TEST(test_activated_providers); + ADD_TEST(test_available_providers); + } return 1; } void cleanup_tests(void) { + OPENSSL_free(rel_conf_file); BIO_vfree(in); NCONF_free(conf); CONF_modules_unload(1); diff --git a/test/recipes/90-test_includes.t b/test/recipes/90-test_includes.t index b931d4ec4a0a5f..5ff61910c570e7 100644 --- a/test/recipes/90-test_includes.t +++ b/test/recipes/90-test_includes.t @@ -2,8 +2,9 @@ use strict; use warnings; -use OpenSSL::Test qw/:DEFAULT data_file/; +use OpenSSL::Test qw/:DEFAULT bldtop_dir data_file/; use OpenSSL::Test::Utils; +use Cwd qw(abs_path); setup("test_includes"); @@ -13,9 +14,11 @@ plan skip_all => "test_includes doesn't work without posix-io" delete $ENV{OPENSSL_CONF_INCLUDE}; plan tests => # The number of tests being performed - 6 + 7 + ($^O eq "VMS" ? 2 : 0); +$ENV{OPENSSL_MODULES} = abs_path(bldtop_dir("providers")); + ok(run(test(["conf_include_test", data_file("includes.cnf")])), "test directory includes"); ok(run(test(["conf_include_test", data_file("includes-file.cnf")])), "test file includes"); ok(run(test(["conf_include_test", data_file("includes-eq.cnf")])), "test includes with equal character"); @@ -28,3 +31,10 @@ if ($^O eq "VMS") { } ok(run(test(["conf_include_test", "-f", data_file("includes-broken.cnf")])), "test broken includes"); ok(run(test(["conf_include_test", "-f", data_file("incdir.cnf")])), "test includedir"); + +SKIP: { + skip "Skipping legacy test", 1 + if disabled("legacy"); + ok(run(test(["conf_include_test", "-providers", data_file("includes-prov-dir.cnf")])), + "test directory includes with provider configs"); +} diff --git a/test/recipes/90-test_includes_data/conf-includes-prov/inc-default.cnf b/test/recipes/90-test_includes_data/conf-includes-prov/inc-default.cnf new file mode 100644 index 00000000000000..dc272ed292e491 --- /dev/null +++ b/test/recipes/90-test_includes_data/conf-includes-prov/inc-default.cnf @@ -0,0 +1,5 @@ +[provider_sect] +default = default_sect + +[default_sect] +activate = 1 diff --git a/test/recipes/90-test_includes_data/conf-includes-prov/inc-legacy.cnf b/test/recipes/90-test_includes_data/conf-includes-prov/inc-legacy.cnf new file mode 100644 index 00000000000000..1987d66bce7480 --- /dev/null +++ b/test/recipes/90-test_includes_data/conf-includes-prov/inc-legacy.cnf @@ -0,0 +1,5 @@ +[provider_sect] +legacy = legacy_sect + +[legacy_sect] +activate = 1 diff --git a/test/recipes/90-test_includes_data/includes-prov-dir.cnf b/test/recipes/90-test_includes_data/includes-prov-dir.cnf new file mode 100644 index 00000000000000..c7ad216083acb2 --- /dev/null +++ b/test/recipes/90-test_includes_data/includes-prov-dir.cnf @@ -0,0 +1,17 @@ +# +# Example configuration file using includes to load providers. +# + +openssl_conf = openssl_init + +[openssl_init] +providers = provider_sect + +[provider_sect] +null = null_sect + +[null_sect] +activate = 1 + +.include conf-includes +.include conf-includes-prov