Add test cases for this -out option check
[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 plan skip_all => "'-out' option tests are not available on Windows or VMS"
20     if $^O =~ /^(VMS|MSWin32)$/;
21
22 plan tests => 11;
23
24 # The following patterns should be tested:
25 #
26 # path        dirname
27 # /usr/       /
28 # /           /
29 # .           .
30 # ..          .
31
32 test_illegal_path('/usr/');
33 test_illegal_path('/');
34 test_illegal_path('.');
35 test_illegal_path('..');
36
37 # Test for trying to create a file in a non-exist directory
38 my @chars = ("A".."Z", "a".."z", "0".."9");
39 my $rand_path = $chars[rand @chars] for 1..32;
40 $rand_path .= "/test.pem";
41
42 test_illegal_path($rand_path);
43 test_legal_path('test.pem');
44 unlink 'test.pem';
45
46 sub test_illegal_path {
47     my ($path) = @_;
48
49     my $start = time();
50     ok(!run(app([ 'openssl', 'genrsa', '-out', $path, '16384'])), "invalid output path: $path");
51     my $end = time();
52     # The above process should exit in 2 seconds if the path is not valid
53     ok($end - $start < 2, "check time consumed");
54 }
55
56 sub test_legal_path {
57     my ($path) = @_;
58
59     ok(run(app([ 'openssl', 'genrsa', '-out', $path, '2048'])), "valid output path: $path");
60 }