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