2016-04-22 11:21:51 +00:00
|
|
|
#! /usr/bin/env perl
|
2018-02-13 12:51:29 +00:00
|
|
|
# Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2016-04-22 11:21:51 +00:00
|
|
|
#
|
|
|
|
# Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
# this file except in compliance with the License. You can obtain a copy
|
|
|
|
# in the file LICENSE in the source distribution or at
|
|
|
|
# https://www.openssl.org/source/license.html
|
|
|
|
|
2015-09-09 15:36:21 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use File::Spec::Functions;
|
|
|
|
use File::Copy;
|
|
|
|
use File::Basename;
|
2017-08-01 20:43:56 +00:00
|
|
|
use OpenSSL::Glob;
|
2017-02-10 21:50:24 +00:00
|
|
|
use OpenSSL::Test qw/:DEFAULT srctop_file/;
|
2015-09-09 15:36:21 +00:00
|
|
|
|
|
|
|
setup("test_rehash");
|
|
|
|
|
2015-09-22 14:16:29 +00:00
|
|
|
#If "openssl rehash -help" fails it's most likely because we're on a platform
|
|
|
|
#that doesn't support the rehash command (e.g. Windows)
|
|
|
|
plan skip_all => "test_rehash is not available on this platform"
|
|
|
|
unless run(app(["openssl", "rehash", "-help"]));
|
|
|
|
|
2018-01-29 05:14:53 +00:00
|
|
|
plan tests => 4;
|
2015-09-09 15:36:21 +00:00
|
|
|
|
|
|
|
indir "rehash.$$" => sub {
|
|
|
|
prepare();
|
|
|
|
ok(run(app(["openssl", "rehash", curdir()])),
|
|
|
|
'Testing normal rehash operations');
|
|
|
|
}, create => 1, cleanup => 1;
|
|
|
|
|
|
|
|
indir "rehash.$$" => sub {
|
|
|
|
prepare(sub { chmod 400, $_ foreach (@_); });
|
|
|
|
ok(run(app(["openssl", "rehash", curdir()])),
|
|
|
|
'Testing rehash operations on readonly files');
|
|
|
|
}, create => 1, cleanup => 1;
|
|
|
|
|
|
|
|
indir "rehash.$$" => sub {
|
|
|
|
ok(run(app(["openssl", "rehash", curdir()])),
|
|
|
|
'Testing rehash operations on empty directory');
|
|
|
|
}, create => 1, cleanup => 1;
|
|
|
|
|
|
|
|
indir "rehash.$$" => sub {
|
|
|
|
prepare();
|
|
|
|
chmod 0500, curdir();
|
2015-09-18 19:27:29 +00:00
|
|
|
SKIP: {
|
2018-01-29 05:14:53 +00:00
|
|
|
if (open(FOO, ">unwritable.txt")) {
|
2015-09-18 19:27:29 +00:00
|
|
|
close FOO;
|
|
|
|
skip "It's pointless to run the next test as root", 1;
|
|
|
|
}
|
|
|
|
isnt(run(app(["openssl", "rehash", curdir()])), 1,
|
|
|
|
'Testing rehash operations on readonly directory');
|
|
|
|
}
|
2015-09-09 15:36:21 +00:00
|
|
|
chmod 0700, curdir(); # make it writable again, so cleanup works
|
|
|
|
}, create => 1, cleanup => 1;
|
|
|
|
|
|
|
|
sub prepare {
|
2017-02-10 21:50:24 +00:00
|
|
|
my @pemsourcefiles = sort glob(srctop_file('test', "*.pem"));
|
2015-09-09 15:36:21 +00:00
|
|
|
my @destfiles = ();
|
2017-02-10 21:50:24 +00:00
|
|
|
|
|
|
|
die "There are no source files\n" if scalar @pemsourcefiles == 0;
|
|
|
|
|
|
|
|
my $cnt = 0;
|
|
|
|
foreach (@pemsourcefiles) {
|
|
|
|
my $basename = basename($_, ".pem");
|
|
|
|
my $writing = 0;
|
|
|
|
|
|
|
|
open PEM, $_ or die "Can't read $_: $!\n";
|
|
|
|
while (my $line = <PEM>) {
|
|
|
|
if ($line =~ m{^-----BEGIN (?:CERTIFICATE|X509 CRL)-----}) {
|
|
|
|
die "New start in a PEM blob?\n" if $writing;
|
|
|
|
$cnt++;
|
|
|
|
my $destfile =
|
|
|
|
catfile(curdir(),
|
|
|
|
$basename . sprintf("-%02d", $cnt) . ".pem");
|
|
|
|
push @destfiles, $destfile;
|
|
|
|
open OUT, '>', $destfile
|
|
|
|
or die "Can't write $destfile\n";
|
|
|
|
$writing = 1;
|
|
|
|
}
|
|
|
|
print OUT $line if $writing;
|
|
|
|
if ($line =~ m|^-----END |) {
|
|
|
|
close OUT if $writing;
|
|
|
|
$writing = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
die "No end marker in $basename\n" if $writing;
|
2015-09-09 15:36:21 +00:00
|
|
|
}
|
2017-02-10 21:50:24 +00:00
|
|
|
die "No test PEM files produced\n" if $cnt == 0;
|
|
|
|
|
2015-09-09 15:36:21 +00:00
|
|
|
foreach (@_) {
|
|
|
|
die "Internal error, argument is not CODE"
|
|
|
|
unless (ref($_) eq 'CODE');
|
|
|
|
$_->(@destfiles);
|
|
|
|
}
|
|
|
|
}
|