Commit 39e32be1 authored by Richard Levitte's avatar Richard Levitte
Browse files

test/recipes/15-test_out_option.t: refine tests



Test writing to the null device.  This should be successful.

Also, refactor so the planned number of tests is calculated.

Reviewed-by: default avatarAndy Polyakov <appro@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/6033)
parent 22f0c72b
Loading
Loading
Loading
Loading
+37 −34
Original line number Diff line number Diff line
@@ -16,46 +16,49 @@ use OpenSSL::Test::Utils;

setup("test_out_option");

plan skip_all => "'-out' option tests are not available on Windows"
    if $^O eq 'MSWin32';

plan tests => 11;

# The following patterns should be tested:
#
# path        dirname
# /usr/       /
# /           /
# .           .
# ..          .

test_illegal_path('/usr/');
test_illegal_path('/');
test_illegal_path('./');
test_illegal_path('../');
# Paths that should generate failure when trying to write to them.
# Directories are a safe bet for failure on all platforms.
# Note that directories must end with a slash here, because of how
# File::Spec massages them into directory specs on some platforms.
my @failure_paths = (
    './',
   );
my @success_paths = (
    'randomname.bin'
   );

# Test for trying to create a file in a non-exist directory
my @chars = ("A".."Z", "a".."z", "0".."9");
my $rand_path = "";
do {
    my @chars = ("A".."Z", "a".."z", "0".."9");
    $rand_path .= $chars[rand @chars] for 1..32;
$rand_path .= "/test.pem";

test_illegal_path($rand_path);
test_legal_path('test.pem');
unlink 'test.pem';
} while (-d File::Spec->catdir('.', $rand_path));
$rand_path .= "/randomname.bin";

sub test_illegal_path {
    my $path = File::Spec->canonpath($_[0]);
push @failure_paths, $rand_path;

    my $start = time();
    ok(!run(app([ 'openssl', 'genrsa', '-out', $path, '16384'])), "invalid output path: $path");
    my $end = time();
    # The above process should exit in 2 seconds if the path is not valid
    ok($end - $start < 2, "check time consumed");
# All explicit cross compilations run a risk of failing this, because the
# null device provided by perl might not match what the cross compiled
# application expects to see as a null device.  Therefore, we skip the check
# of outputing to the null device if the cross compile prefix is set.
if ((config('CROSS_COMPILE') // '') eq '') {
    # Check that we can write to the NULL device
    push @success_paths, File::Spec->devnull();
}

sub test_legal_path {
    my $path = File::Spec->canonpath($_[0]);
plan tests => scalar @failure_paths + scalar @success_paths;

foreach (@failure_paths) {
    my $path = File::Spec->canonpath($_);
    ok(!run(app([ 'openssl', 'rand', '-out', $path, '1'])),
       "invalid output path: $path");
}
foreach (@success_paths) {
    my $path = File::Spec->canonpath($_);
    ok(run(app([ 'openssl', 'rand', '-out', $path, '1'])),
       "valid output path: $path");
}

    ok(run(app([ 'openssl', 'genrsa', '-out', $path, '2048'])), "valid output path: $path");
END {
    unlink 'randomname.bin' if -f 'randomname.bin';
}