1#! /usr/bin/env perl
2# Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (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
10use strict;
11use warnings;
12
13use File::Spec;
14use OpenSSL::Test qw/:DEFAULT srctop_file/;
15use OpenSSL::Test::Utils;
16
17setup("test_out_option");
18
19plan tests => 4;
20
21# Test 1
22SKIP: {
23    # Paths that should generate failure when trying to write to them.
24    # Directories are a safe bet for failure on most platforms.
25    # Notably, this isn't true on OpenVMS, as a default file name is
26    # appended under the hood when trying to "write" to a directory spec.
27    # From observation, that file is '.' (i.e. a file with no file name
28    # and no extension), so '[]' gets translated to '[].'
29    skip 'Directories become writable files on OpenVMS', 1 if $^O eq 'VMS';
30
31    # Note that directories must end with a slash here, because of how
32    # File::Spec massages them into directory specs on some platforms.
33    my $path = File::Spec->canonpath('./');
34    ok(!run(app([ 'openssl', 'rand', '-out', $path, '1'])),
35       "invalid output path: $path");
36}
37
38# Test 2
39{
40    my $path = File::Spec->canonpath('randomname.bin');
41    ok(run(app([ 'openssl', 'rand', '-out', $path, '1'])),
42       "valid output path: $path");
43}
44
45# Test 3
46{
47    # Test for trying to create a file in a non-exist directory
48    my $rand_path = "";
49    do {
50        my @chars = ("A".."Z", "a".."z", "0".."9");
51        $rand_path .= $chars[rand @chars] for 1..32;
52    } while (-d File::Spec->catdir('.', $rand_path));
53    $rand_path .= "/randomname.bin";
54
55    my $path = File::Spec->canonpath($rand_path);
56    ok(!run(app([ 'openssl', 'rand', '-out', $path, '1'])),
57       "invalid output path: $path");
58}
59
60# Test 4
61SKIP: {
62    skip "It's not safe to use perl's idea of the NULL device in an explicitly cross compiled build", 1
63        unless (config('CROSS_COMPILE') // '') eq '';
64
65    my $path = File::Spec->canonpath(File::Spec->devnull());
66    ok(run(app([ 'openssl', 'rand', '-out', $path, '1'])),
67       "valid output path: $path");
68}
69
70# Cleanup
71END {
72    unlink 'randomname.bin' if -f 'randomname.bin';
73}
74