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