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