2
0

15-test_out_option.t 2.3 KB

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