test/recipes/15-test_out_option.t: refine tests
[openssl.git] / test / recipes / 15-test_out_option.t
1 #! /usr/bin/env perl
2 # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9
10 use strict;
11 use warnings;
12
13 use File::Spec;
14 use OpenSSL::Test qw/:DEFAULT srctop_file/;
15 use OpenSSL::Test::Utils;
16
17 setup("test_out_option");
18
19 # Paths that should generate failure when trying to write to them.
20 # Directories are a safe bet for failure on all platforms.
21 # Note that directories must end with a slash here, because of how
22 # File::Spec massages them into directory specs on some platforms.
23 my @failure_paths = (
24     './',
25    );
26 my @success_paths = (
27     'randomname.bin'
28    );
29
30 # Test for trying to create a file in a non-exist directory
31 my $rand_path = "";
32 do {
33     my @chars = ("A".."Z", "a".."z", "0".."9");
34     $rand_path .= $chars[rand @chars] for 1..32;
35 } while (-d File::Spec->catdir('.', $rand_path));
36 $rand_path .= "/randomname.bin";
37
38 push @failure_paths, $rand_path;
39
40 # All explicit cross compilations run a risk of failing this, because the
41 # null device provided by perl might not match what the cross compiled
42 # application expects to see as a null device.  Therefore, we skip the check
43 # of outputing to the null device if the cross compile prefix is set.
44 if ((config('CROSS_COMPILE') // '') eq '') {
45     # Check that we can write to the NULL device
46     push @success_paths, File::Spec->devnull();
47 }
48
49 plan tests => scalar @failure_paths + scalar @success_paths;
50
51 foreach (@failure_paths) {
52     my $path = File::Spec->canonpath($_);
53     ok(!run(app([ 'openssl', 'rand', '-out', $path, '1'])),
54        "invalid output path: $path");
55 }
56 foreach (@success_paths) {
57     my $path = File::Spec->canonpath($_);
58     ok(run(app([ 'openssl', 'rand', '-out', $path, '1'])),
59        "valid output path: $path");
60 }
61
62 END {
63     unlink 'randomname.bin' if -f 'randomname.bin';
64 }