Add a simple test for the new rehash command
[openssl.git] / test / recipes / 40-test_rehash.t
1 #! /usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use File::Spec::Functions;
7 use File::Copy;
8 use File::Basename;
9 use OpenSSL::Test qw/:DEFAULT top_file/;
10
11 setup("test_rehash");
12
13 plan tests => 4;
14
15 indir "rehash.$$" => sub {
16     prepare();
17     ok(run(app(["openssl", "rehash", curdir()])),
18        'Testing normal rehash operations');
19 }, create => 1, cleanup => 1;
20
21 indir "rehash.$$" => sub {
22     prepare(sub { chmod 400, $_ foreach (@_); });
23     ok(run(app(["openssl", "rehash", curdir()])),
24        'Testing rehash operations on readonly files');
25 }, create => 1, cleanup => 1;
26
27 indir "rehash.$$" => sub {
28     ok(run(app(["openssl", "rehash", curdir()])),
29        'Testing rehash operations on empty directory');
30 }, create => 1, cleanup => 1;
31
32 indir "rehash.$$" => sub {
33     prepare();
34     chmod 0500, curdir();
35     isnt(run(app(["openssl", "rehash", curdir()])), 1,
36          'Testing rehash operations on readonly directory');
37     chmod 0700, curdir();       # make it writable again, so cleanup works
38 }, create => 1, cleanup => 1;
39
40 sub prepare {
41     my @sourcefiles =
42         sort map { glob(top_file('certs', 'demo', "*.$_")) } ('pem',
43                                                               'crt',
44                                                               'cer',
45                                                               'crl');
46     my @destfiles = ();
47     foreach (@sourcefiles) {
48         copy($_, curdir());
49         push @destfiles, catfile(curdir(), basename($_));
50     }
51     foreach (@_) {
52         die "Internal error, argument is not CODE"
53             unless (ref($_) eq 'CODE');
54         $_->(@destfiles);
55     }
56 }