# Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the OpenSSL license (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
package OpenSSL::Test;
use strict;
use warnings;
use Test::More 0.96;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = "0.8";
@ISA = qw(Exporter);
@EXPORT = (@Test::More::EXPORT, qw(setup run indir cmd app fuzz test
perlapp perltest subtest));
@EXPORT_OK = (@Test::More::EXPORT_OK, qw(bldtop_dir bldtop_file
srctop_dir srctop_file
data_file data_dir
pipe with cmdstr quotify
openssl_versions));
=head1 NAME
OpenSSL::Test - a private extension of Test::More
=head1 SYNOPSIS
use OpenSSL::Test;
setup("my_test_name");
ok(run(app(["openssl", "version"])), "check for openssl presence");
indir "subdir" => sub {
ok(run(test(["sometest", "arg1"], stdout => "foo.txt")),
"run sometest with output to foo.txt");
};
=head1 DESCRIPTION
This module is a private extension of L<Test::More> for testing OpenSSL.
In addition to the Test::More functions, it also provides functions that
easily find the diverse programs within a OpenSSL build tree, as well as
some other useful functions.
This module I<depends> on the environment variables C<$TOP> or C<$SRCTOP>
and C<$BLDTOP>. Without one of the combinations it refuses to work.
See L</ENVIRONMENT> below.
With each test recipe, a parallel data directory with (almost) the same name
as the recipe is possible in the source directory tree. For example, for a
recipe C<$SRCTOP/test/recipes/99-foo.t>, there could be a directory
C<$SRCTOP/test/recipes/99-foo_data/>.
=cut
use File::Copy;
use File::Spec::Functions qw/file_name_is_absolute curdir canonpath splitdir
catdir catfile splitpath catpath devnull abs2rel
rel2abs/;
use File::Path 2.00 qw/rmtree mkpath/;
use File::Basename;
use Cwd qw/abs_path/;
my $level = 0;
# The name of the test. This is set by setup() and is used in the other
# functions to verify that setup() has been used.
my $test_name = undef;
# Directories we want to keep track of TOP, APPS, TEST and RESULTS are the
# ones we're interested in, corresponding to the environment variables TOP
# (mandatory), BIN_D, TEST_D, UTIL_D and RESULT_D.
my %directories = ();
# The environment variables that gave us the contents in %directories. These
# get modified whenever we change directories, so that subprocesses can use
# the values of those environment variables as well
my @direnv = ();
# A bool saying if we shall stop all testing if the current recipe has failing
# tests or not. This is set by setup() if the environment variable STOPTEST
# is defined with a non-empty value.
my $end_with_bailout = 0;
# A set of hooks that is affected by with() and may be used in diverse places.
# All hooks are expected to be CODE references.
my %hooks = (
# exit_checker is used by run() directly after completion of a command.
# it receives the exit code from that command and is expected to return
# 1 (for success) or 0 (for failure). This is the status value that run()
# will give back (through the |statusvar| reference and as returned value
# when capture => 1 doesn't apply).
exit_checker => sub { return shift == 0 ? 1 : 0 },
);
# Debug flag, to be set manually when needed
my $debug = 0;
=head2 Main functions
The following functions are exported by default when using C<OpenSSL::Test>.
=cut
=over 4
=item B<setup "NAME">
C<setup> is used for initial setup, and it is mandatory that it's used.
If it's not used in a OpenSSL test recipe, the rest of the recipe will
most likely refuse to run.
C<setup> checks for environment variables (see L</ENVIRONMENT> below),
checks that C<$TOP/Configure> or C<$SRCTOP/Configure> exists, C<chdir>
into the results directory (defined by the C<$RESULT_D> environment
variable if defined, otherwise C<$BLDTOP/test> or C<$TOP/test>, whichever
is defined).
=back
=cut
sub setup {
my $old_test_name = $test_name;
$test_name = shift;
BAIL_OUT("setup() must receive a name") unless $test_name;
warn "setup() detected test name change. Innocuous, so we continue...\n"
if $old_test_name && $old_test_name ne $test_name;
return if $old_test_name;
BAIL_OUT("setup() needs \$TOP or \$SRCTOP and \$BLDTOP to be defined")
unless $ENV{TOP} || ($ENV{SRCTOP} && $ENV{BLDTOP});