4f0d71fbd25decb4ea7afad4e2f26acdd6592cce
[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 => 5;
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   SKIP: {
36       if (!ok(!open(FOO, ">unwritable.txt"),
37               "Testing that we aren't running as a priviledged user, such as root")) {
38           close FOO;
39           skip "It's pointless to run the next test as root", 1;
40       }
41       isnt(run(app(["openssl", "rehash", curdir()])), 1,
42            'Testing rehash operations on readonly directory');
43     }
44     chmod 0700, curdir();       # make it writable again, so cleanup works
45 }, create => 1, cleanup => 1;
46
47 sub prepare {
48     my @sourcefiles =
49         sort map { glob(top_file('certs', 'demo', "*.$_")) } ('pem',
50                                                               'crt',
51                                                               'cer',
52                                                               'crl');
53     my @destfiles = ();
54     foreach (@sourcefiles) {
55         copy($_, curdir());
56         push @destfiles, catfile(curdir(), basename($_));
57     }
58     foreach (@_) {
59         die "Internal error, argument is not CODE"
60             unless (ref($_) eq 'CODE');
61         $_->(@destfiles);
62     }
63 }