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 b53d5aa
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 1 deletion.
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
155 changes: 155 additions & 0 deletions test/provider_include_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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;
int return_path = 0;

if (s == NULL)
return NULL;

while ((p = strpbrk(p, DIRSEP)) != NULL) {
last = p++;
return_path++;
}
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 b53d5aa

Please sign in to comment.