Configure,test/recipes: "pin" glob to File::Glob::glob.
[openssl.git] / test / recipes / 40-test_rehash.t
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
9
10 use strict;
11 use warnings;
12
13 use File::Spec::Functions;
14 use File::Copy;
15 use File::Basename;
16 if ($^O ne "VMS") {
17     use File::Glob qw/glob/;
18 }
19 use OpenSSL::Test qw/:DEFAULT bldtop_file/;
20
21 setup("test_rehash");
22
23 #If "openssl rehash -help" fails it's most likely because we're on a platform
24 #that doesn't support the rehash command (e.g. Windows)
25 plan skip_all => "test_rehash is not available on this platform"
26     unless run(app(["openssl", "rehash", "-help"]));
27
28 plan tests => 5;
29
30 indir "rehash.$$" => sub {
31     prepare();
32     ok(run(app(["openssl", "rehash", curdir()])),
33        'Testing normal rehash operations');
34 }, create => 1, cleanup => 1;
35
36 indir "rehash.$$" => sub {
37     prepare(sub { chmod 400, $_ foreach (@_); });
38     ok(run(app(["openssl", "rehash", curdir()])),
39        'Testing rehash operations on readonly files');
40 }, create => 1, cleanup => 1;
41
42 indir "rehash.$$" => sub {
43     ok(run(app(["openssl", "rehash", curdir()])),
44        'Testing rehash operations on empty directory');
45 }, create => 1, cleanup => 1;
46
47 indir "rehash.$$" => sub {
48     prepare();
49     chmod 0500, curdir();
50   SKIP: {
51       if (!ok(!open(FOO, ">unwritable.txt"),
52               "Testing that we aren't running as a privileged user, such as root")) {
53           close FOO;
54           skip "It's pointless to run the next test as root", 1;
55       }
56       isnt(run(app(["openssl", "rehash", curdir()])), 1,
57            'Testing rehash operations on readonly directory');
58     }
59     chmod 0700, curdir();       # make it writable again, so cleanup works
60 }, create => 1, cleanup => 1;
61
62 sub prepare {
63     my @sourcefiles =
64         sort map { glob(bldtop_file('certs', 'demo', "*.$_")) } ('pem',
65                                                                  'crt',
66                                                                  'cer',
67                                                                  'crl');
68     my @destfiles = ();
69     foreach (@sourcefiles) {
70         copy($_, curdir());
71         push @destfiles, catfile(curdir(), basename($_));
72     }
73     foreach (@_) {
74         die "Internal error, argument is not CODE"
75             unless (ref($_) eq 'CODE');
76         $_->(@destfiles);
77     }
78 }