40-test_rehash.t 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2016 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::Functions;
  11. use File::Copy;
  12. use File::Basename;
  13. use if $^O ne "VMS", 'File::Glob' => qw/glob/;
  14. use OpenSSL::Test qw/:DEFAULT srctop_file/;
  15. setup("test_rehash");
  16. #If "openssl rehash -help" fails it's most likely because we're on a platform
  17. #that doesn't support the rehash command (e.g. Windows)
  18. plan skip_all => "test_rehash is not available on this platform"
  19. unless run(app(["openssl", "rehash", "-help"]));
  20. plan tests => 5;
  21. indir "rehash.$$" => sub {
  22. prepare();
  23. ok(run(app(["openssl", "rehash", curdir()])),
  24. 'Testing normal rehash operations');
  25. }, create => 1, cleanup => 1;
  26. indir "rehash.$$" => sub {
  27. prepare(sub { chmod 400, $_ foreach (@_); });
  28. ok(run(app(["openssl", "rehash", curdir()])),
  29. 'Testing rehash operations on readonly files');
  30. }, create => 1, cleanup => 1;
  31. indir "rehash.$$" => sub {
  32. ok(run(app(["openssl", "rehash", curdir()])),
  33. 'Testing rehash operations on empty directory');
  34. }, create => 1, cleanup => 1;
  35. indir "rehash.$$" => sub {
  36. prepare();
  37. chmod 0500, curdir();
  38. SKIP: {
  39. if (!ok(!open(FOO, ">unwritable.txt"),
  40. "Testing that we aren't running as a privileged user, such as root")) {
  41. close FOO;
  42. skip "It's pointless to run the next test as root", 1;
  43. }
  44. isnt(run(app(["openssl", "rehash", curdir()])), 1,
  45. 'Testing rehash operations on readonly directory');
  46. }
  47. chmod 0700, curdir(); # make it writable again, so cleanup works
  48. }, create => 1, cleanup => 1;
  49. sub prepare {
  50. my @pemsourcefiles = sort glob(srctop_file('test', "*.pem"));
  51. my @destfiles = ();
  52. die "There are no source files\n" if scalar @pemsourcefiles == 0;
  53. my $cnt = 0;
  54. foreach (@pemsourcefiles) {
  55. my $basename = basename($_, ".pem");
  56. my $writing = 0;
  57. open PEM, $_ or die "Can't read $_: $!\n";
  58. while (my $line = <PEM>) {
  59. if ($line =~ m{^-----BEGIN (?:CERTIFICATE|X509 CRL)-----}) {
  60. die "New start in a PEM blob?\n" if $writing;
  61. $cnt++;
  62. my $destfile =
  63. catfile(curdir(),
  64. $basename . sprintf("-%02d", $cnt) . ".pem");
  65. push @destfiles, $destfile;
  66. open OUT, '>', $destfile
  67. or die "Can't write $destfile\n";
  68. $writing = 1;
  69. }
  70. print OUT $line if $writing;
  71. if ($line =~ m|^-----END |) {
  72. close OUT if $writing;
  73. $writing = 0;
  74. }
  75. }
  76. die "No end marker in $basename\n" if $writing;
  77. }
  78. die "No test PEM files produced\n" if $cnt == 0;
  79. foreach (@_) {
  80. die "Internal error, argument is not CODE"
  81. unless (ref($_) eq 'CODE');
  82. $_->(@destfiles);
  83. }
  84. }