diff --git a/.github/workflows/cross-compiles.yml b/.github/workflows/cross-compiles.yml index 79af07c701678..45804f9dc2a11 100644 --- a/.github/workflows/cross-compiles.yml +++ b/.github/workflows/cross-compiles.yml @@ -59,30 +59,30 @@ jobs: arch: arm-linux-gnueabi, libs: libc6-dev-armel-cross, target: linux-armv4, - tests: -test_includes -test_store -test_x509_store + tests: -test_includes -test_store -test_x509_store -test_provider_include }, { arch: arm-linux-gnueabihf, libs: libc6-dev-armhf-cross, target: linux-armv4, - tests: -test_includes -test_store -test_x509_store + tests: -test_includes -test_store -test_x509_store -test_provider_include }, { arch: hppa-linux-gnu, libs: libc6-dev-hppa-cross, target: -static linux-generic32, fips: no, - tests: -test_includes -test_store -test_x509_store + tests: -test_includes -test_store -test_x509_store -test_provider_include }, { arch: m68k-linux-gnu, libs: libc6-dev-m68k-cross, target: -static -m68040 linux-latomic -Wno-stringop-overflow, fips: no, - tests: -test_includes -test_store -test_x509_store + tests: -test_includes -test_store -test_x509_store -test_provider_include }, { arch: mips-linux-gnu, libs: libc6-dev-mips-cross, target: -static linux-mips32, fips: no, - tests: -test_includes -test_store -test_x509_store + tests: -test_includes -test_store -test_x509_store -test_provider_include }, { arch: mips64-linux-gnuabi64, libs: libc6-dev-mips64-cross, @@ -92,7 +92,7 @@ jobs: arch: mipsel-linux-gnu, libs: libc6-dev-mipsel-cross, target: linux-mips32, - tests: -test_includes -test_store -test_x509_store + tests: -test_includes -test_store -test_x509_store -test_provider_include }, { arch: powerpc64le-linux-gnu, libs: libc6-dev-ppc64el-cross, @@ -112,7 +112,7 @@ jobs: arch: sh4-linux-gnu, libs: libc6-dev-sh4-cross, target: no-async linux-latomic, - tests: -test_includes -test_store -test_x509_store + tests: -test_includes -test_store -test_x509_store -test_provider_include }, # These build with shared libraries but they crash when run diff --git a/test/build.info b/test/build.info index 73f8de1f7a125..0b20f41a91eed 100644 --- a/test/build.info +++ b/test/build.info @@ -62,7 +62,7 @@ IF[{- !$disabled{tests} -}] bio_readbuffer_test user_property_test pkcs7_test upcallstest \ provfetchtest prov_config_test rand_test ca_internals_test \ bio_tfo_test membio_test bio_dgram_test list_test fips_version_test \ - x509_test hpke_test pairwise_fail_test nodefltctxtest + x509_test hpke_test pairwise_fail_test nodefltctxtest provider_include_test IF[{- !$disabled{'rpk'} -}] PROGRAMS{noinst}=rpktest @@ -1136,6 +1136,10 @@ ENDIF INCLUDE[cert_comp_test]=../include ../apps/include .. DEPEND[cert_comp_test]=../libcrypto ../libssl libtestutil.a + SOURCE[provider_include_test]=provider_include_test.c + INCLUDE[provider_include_test]=../include ../apps/include + DEPEND[provider_include_test]=../libcrypto libtestutil.a + {- use File::Spec::Functions; use File::Basename; diff --git a/test/provider_include_test.c b/test/provider_include_test.c new file mode 100644 index 0000000000000..5af0e3f07c162 --- /dev/null +++ b/test/provider_include_test.c @@ -0,0 +1,153 @@ +/* + * Copyright 2023 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 +#include +#include +#include +#include +#include +#include +#include "testutil.h" + +#ifdef _WIN32 +# include +# define DIRSEP "/\\" +# ifndef __BORLANDC__ +# define chdir _chdir +# endif +# define DIRSEP_PRESERVE 0 +#elif !defined(OPENSSL_NO_POSIX_IO) +# include +# ifndef OPENSSL_SYS_VMS +# define DIRSEP "/" +# define DIRSEP_PRESERVE 0 +# else +# define DIRSEP "/]:" +# define DIRSEP_PRESERVE 1 +# endif +#else +/* the test does not work without chdir() */ +# define chdir(x) (-1); +# define DIRSEP "/" +# define DIRSEP_PRESERVE 0 +#endif + +typedef enum OPTION_choice { + OPT_ERR = -1, + OPT_EOF = 0, + OPT_FAIL, + OPT_TEST_ENUM +} OPTION_CHOICE; + +static OSSL_LIB_CTX *libctx = NULL; +static int expect_failure = 0; + +/* changes path to that of the filename and returns new config filename */ +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 NULL; + + while ((p = strpbrk(p, DIRSEP)) != NULL) { + last = p++; + } + if (last == NULL) + goto err; + + last[DIRSEP_PRESERVE] = 0; + ret = chdir(s); + if (ret == 0) + new_config_name = strdup(last + DIRSEP_PRESERVE + 1); + err: + OPENSSL_free(s); + return new_config_name; +} + +static int test_include_default_provider(void) +{ + if (OSSL_PROVIDER_available(libctx, "null") != 1) { + if (expect_failure) + return 1; + opt_printf_stderr("Null provider is missing\n"); + return 0; + } + if (OSSL_PROVIDER_available(libctx, "default") != 1) { + if (expect_failure) + return 1; + opt_printf_stderr("Default provider is missing\n"); + return 0; + } + if (expect_failure) + return 0; + return 1; +} + +const OPTIONS *test_get_options(void) +{ + static const OPTIONS test_options[] = { + OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("config_file\n"), + { "f", OPT_FAIL, '-', "A failure is expected" }, + { NULL } + }; + return test_options; +} + +int setup_tests(void) +{ + OPTION_CHOICE o; + char *config_file = NULL; + + while ((o = opt_next()) != OPT_EOF) { + switch (o) { + case OPT_FAIL: + expect_failure = 1; + break; + case OPT_TEST_CASES: + break; + default: + case OPT_ERR: + return 0; + } + } + + libctx = OSSL_LIB_CTX_new(); + if (!TEST_ptr(libctx)) + return 0; + /* + * For this test we need to chdir as we use relative + * path names in the config files. + */ + config_file = test_get_argument(0); + if (!TEST_ptr(config_file)) { + opt_printf_stderr("No file argument\n"); + return 0; + } + config_file = change_path(config_file); + if (!TEST_ptr(config_file) || !OSSL_LIB_CTX_load_config(libctx, config_file)) { + OPENSSL_free(config_file); + opt_printf_stderr("Failed to load config\n"); + return 0; + } + OPENSSL_free(config_file); + + ADD_TEST(test_include_default_provider); + return 1; +} + +void cleanup_tests(void) +{ + OSSL_LIB_CTX_free(libctx); +} diff --git a/test/recipes/30-test_provider_include.t b/test/recipes/30-test_provider_include.t new file mode 100644 index 0000000000000..493bdb2ed8f56 --- /dev/null +++ b/test/recipes/30-test_provider_include.t @@ -0,0 +1,24 @@ +#! /usr/bin/env perl +# Copyright 2023 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 + +use strict; +use warnings; +use OpenSSL::Test qw/:DEFAULT data_file/; +use OpenSSL::Test::Utils; + +setup("test_provider_include"); + +plan skip_all => "test_provider_include doesn't work without posix-io" + if disabled("posix-io"); + +delete $ENV{OPENSSL_CONF_INCLUDE}; + +plan tests => 2; + +ok(run(test(["provider_include_test", data_file("null-default.cnf")])), "test null and default provider availability"); +ok(run(test(["provider_include_test", "-f", data_file("null.cnf")])), "test default provider unavailability"); diff --git a/test/recipes/30-test_provider_include_data/default-dir/default.cnf b/test/recipes/30-test_provider_include_data/default-dir/default.cnf new file mode 100644 index 0000000000000..dc272ed292e49 --- /dev/null +++ b/test/recipes/30-test_provider_include_data/default-dir/default.cnf @@ -0,0 +1,5 @@ +[provider_sect] +default = default_sect + +[default_sect] +activate = 1 diff --git a/test/recipes/30-test_provider_include_data/null-default.cnf b/test/recipes/30-test_provider_include_data/null-default.cnf new file mode 100644 index 0000000000000..a2e1e33d19a9a --- /dev/null +++ b/test/recipes/30-test_provider_include_data/null-default.cnf @@ -0,0 +1,13 @@ +openssl_conf = openssl_init + +[openssl_init] +providers = provider_sect + +[provider_sect] +null = null_sect + +[null_sect] +activate = 1 + +.include default-dir + diff --git a/test/recipes/30-test_provider_include_data/null.cnf b/test/recipes/30-test_provider_include_data/null.cnf new file mode 100644 index 0000000000000..9c0fa28a554ba --- /dev/null +++ b/test/recipes/30-test_provider_include_data/null.cnf @@ -0,0 +1,10 @@ +openssl_conf = openssl_init + +[openssl_init] +providers = provider_sect + +[provider_sect] +null = null_sect + +[null_sect] +activate = 1