1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ciuse strict;
11e1051a39Sopenharmony_ciuse warnings;
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ciuse File::Spec::Functions;
14e1051a39Sopenharmony_ciuse File::Copy;
15e1051a39Sopenharmony_ciuse File::Basename;
16e1051a39Sopenharmony_ciuse OpenSSL::Glob;
17e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT srctop_file/;
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_cisetup("test_rehash");
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci#If "openssl rehash -help" fails it's most likely because we're on a platform
22e1051a39Sopenharmony_ci#that doesn't support the rehash command (e.g. Windows)
23e1051a39Sopenharmony_ciplan skip_all => "test_rehash is not available on this platform"
24e1051a39Sopenharmony_ci    unless run(app(["openssl", "rehash", "-help"]));
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ciplan tests => 4;
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ciindir "rehash.$$" => sub {
29e1051a39Sopenharmony_ci    prepare();
30e1051a39Sopenharmony_ci    ok(run(app(["openssl", "rehash", curdir()])),
31e1051a39Sopenharmony_ci       'Testing normal rehash operations');
32e1051a39Sopenharmony_ci}, create => 1, cleanup => 1;
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ciindir "rehash.$$" => sub {
35e1051a39Sopenharmony_ci    prepare(sub { chmod 400, $_ foreach (@_); });
36e1051a39Sopenharmony_ci    ok(run(app(["openssl", "rehash", curdir()])),
37e1051a39Sopenharmony_ci       'Testing rehash operations on readonly files');
38e1051a39Sopenharmony_ci}, create => 1, cleanup => 1;
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_ciindir "rehash.$$" => sub {
41e1051a39Sopenharmony_ci    ok(run(app(["openssl", "rehash", curdir()])),
42e1051a39Sopenharmony_ci       'Testing rehash operations on empty directory');
43e1051a39Sopenharmony_ci}, create => 1, cleanup => 1;
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ciindir "rehash.$$" => sub {
46e1051a39Sopenharmony_ci    prepare();
47e1051a39Sopenharmony_ci    chmod 0500, curdir();
48e1051a39Sopenharmony_ci  SKIP: {
49e1051a39Sopenharmony_ci      if (open(FOO, ">unwritable.txt")) {
50e1051a39Sopenharmony_ci          close FOO;
51e1051a39Sopenharmony_ci          skip "It's pointless to run the next test as root", 1;
52e1051a39Sopenharmony_ci      }
53e1051a39Sopenharmony_ci      isnt(run(app(["openssl", "rehash", curdir()])), 1,
54e1051a39Sopenharmony_ci           'Testing rehash operations on readonly directory');
55e1051a39Sopenharmony_ci    }
56e1051a39Sopenharmony_ci    chmod 0700, curdir();       # make it writable again, so cleanup works
57e1051a39Sopenharmony_ci}, create => 1, cleanup => 1;
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_cisub prepare {
60e1051a39Sopenharmony_ci    my @pemsourcefiles = sort glob(srctop_file('test', "*.pem"));
61e1051a39Sopenharmony_ci    my @destfiles = ();
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci    die "There are no source files\n" if scalar @pemsourcefiles == 0;
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci    my $cnt = 0;
66e1051a39Sopenharmony_ci    foreach (@pemsourcefiles) {
67e1051a39Sopenharmony_ci        my $basename = basename($_, ".pem");
68e1051a39Sopenharmony_ci        my $writing = 0;
69e1051a39Sopenharmony_ci
70e1051a39Sopenharmony_ci        open PEM, $_ or die "Can't read $_: $!\n";
71e1051a39Sopenharmony_ci        while (my $line = <PEM>) {
72e1051a39Sopenharmony_ci            if ($line =~ m{^-----BEGIN (?:CERTIFICATE|X509 CRL)-----}) {
73e1051a39Sopenharmony_ci                die "New start in a PEM blob?\n" if $writing;
74e1051a39Sopenharmony_ci                $cnt++;
75e1051a39Sopenharmony_ci                my $destfile =
76e1051a39Sopenharmony_ci                    catfile(curdir(),
77e1051a39Sopenharmony_ci                            $basename . sprintf("-%02d", $cnt) . ".pem");
78e1051a39Sopenharmony_ci                push @destfiles, $destfile;
79e1051a39Sopenharmony_ci                open OUT, '>', $destfile
80e1051a39Sopenharmony_ci                    or die "Can't write $destfile\n";
81e1051a39Sopenharmony_ci                $writing = 1;
82e1051a39Sopenharmony_ci            }
83e1051a39Sopenharmony_ci            print OUT $line if $writing;
84e1051a39Sopenharmony_ci            if ($line =~ m|^-----END |) {
85e1051a39Sopenharmony_ci                close OUT if $writing;
86e1051a39Sopenharmony_ci                $writing = 0;
87e1051a39Sopenharmony_ci            }
88e1051a39Sopenharmony_ci        }
89e1051a39Sopenharmony_ci        die "No end marker in $basename\n" if $writing;
90e1051a39Sopenharmony_ci    }
91e1051a39Sopenharmony_ci    die "No test PEM files produced\n" if $cnt == 0;
92e1051a39Sopenharmony_ci
93e1051a39Sopenharmony_ci    foreach (@_) {
94e1051a39Sopenharmony_ci        die "Internal error, argument is not CODE"
95e1051a39Sopenharmony_ci            unless (ref($_) eq 'CODE');
96e1051a39Sopenharmony_ci        $_->(@destfiles);
97e1051a39Sopenharmony_ci    }
98e1051a39Sopenharmony_ci}
99