Skip to content

Commit

Permalink
Add test for loading provider with include in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
xhanulik committed Oct 19, 2023
1 parent f6fb4e0 commit 00dd95a
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 8 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/cross-compiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/build.info
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
153 changes: 153 additions & 0 deletions test/provider_include_test.c
Original file line number Diff line number Diff line change
@@ -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 <stddef.h>
#include <string.h>
#include <openssl/provider.h>
#include <openssl/params.h>
#include <openssl/core_names.h>
#include <openssl/self_test.h>
#include <openssl/evp.h>
#include "testutil.h"

#ifdef _WIN32
# include <direct.h>
# define DIRSEP "/\\"
# ifndef __BORLANDC__
# define chdir _chdir
# endif
# define DIRSEP_PRESERVE 0
#elif !defined(OPENSSL_NO_POSIX_IO)
# include <unistd.h>
# 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);
}
24 changes: 24 additions & 0 deletions test/recipes/30-test_provider_include.t
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[provider_sect]
default = default_sect

[default_sect]
activate = 1
13 changes: 13 additions & 0 deletions test/recipes/30-test_provider_include_data/null-default.cnf
Original file line number Diff line number Diff line change
@@ -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

10 changes: 10 additions & 0 deletions test/recipes/30-test_provider_include_data/null.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
openssl_conf = openssl_init

[openssl_init]
providers = provider_sect

[provider_sect]
null = null_sect

[null_sect]
activate = 1

0 comments on commit 00dd95a

Please sign in to comment.