aa1dba024236bbc51fa0663e9b0a6ab41e1edde8
[openssl.git] / test / run_tests.pl
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 use strict;
10 use warnings;
11
12 # Recognise VERBOSE and V which is common on other projects.
13 BEGIN {
14     $ENV{HARNESS_VERBOSE} = "yes" if $ENV{VERBOSE} || $ENV{V};
15 }
16
17 use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
18 use File::Basename;
19 use if $^O ne "VMS", 'File::Glob' => qw/glob/;
20 use Module::Load::Conditional qw(can_load);
21
22 my $TAP_Harness = can_load({modules => [ 'TAP::Harness' ]})
23     ? 'TAP::Harness' : 'OpenSSL::TAP::Harness';
24
25 my $srctop = $ENV{SRCTOP} || $ENV{TOP};
26 my $bldtop = $ENV{BLDTOP} || $ENV{TOP};
27 my $recipesdir = catdir($srctop, "test", "recipes");
28 my $testlib = catdir($srctop, "test", "testlib");
29 my $utillib = catdir($srctop, "util");
30
31 my %tapargs =
32     ( verbosity => $ENV{VERBOSE} || $ENV{V} || $ENV{HARNESS_VERBOSE} ? 1 : 0,
33       lib       => [ $testlib, $utillib ],
34       switches  => '-w',
35       merge     => 1
36     );
37
38 my @alltests = find_matching_tests("*");
39 my %tests = ();
40
41 my $initial_arg = 1;
42 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
43     if ($arg eq 'list') {
44         foreach (@alltests) {
45             (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
46             print $x,"\n";
47         }
48         exit 0;
49     }
50     if ($arg eq 'alltests') {
51         warn "'alltests' encountered, ignoring everything before that...\n"
52             unless $initial_arg;
53         %tests = map { $_ => 1 } @alltests;
54     } elsif ($arg =~ m/^(-?)(.*)/) {
55         my $sign = $1;
56         my $test = $2;
57         my @matches = find_matching_tests($test);
58
59         # If '-foo' is the first arg, it's short for 'alltests -foo'
60         if ($sign eq '-' && $initial_arg) {
61             %tests = map { $_ => 1 } @alltests;
62         }
63
64         if (scalar @matches == 0) {
65             warn "Test $test found no match, skipping ",
66                 ($sign eq '-' ? "removal" : "addition"),
67                 "...\n";
68         } else {
69             foreach $test (@matches) {
70                 if ($sign eq '-') {
71                     delete $tests{$test};
72                 } else {
73                     $tests{$test} = 1;
74                 }
75             }
76         }
77     } else {
78         warn "I don't know what '$arg' is about, ignoring...\n";
79     }
80
81     $initial_arg = 0;
82 }
83
84 my $harness = $TAP_Harness->new(\%tapargs);
85 $harness->runtests(map { abs2rel($_, rel2abs(curdir())); } sort keys %tests);
86
87 sub find_matching_tests {
88     my ($glob) = @_;
89
90     if ($glob =~ m|^[\d\[\]\?\-]+$|) {
91         return glob(catfile($recipesdir,"$glob-*.t"));
92     }
93     return glob(catfile($recipesdir,"*-$glob.t"));
94 }
95
96
97 # Fake TAP::Harness in case it's not loaded
98 use Test::Harness;
99 package OpenSSL::TAP::Harness;
100
101 sub new {
102     my $class = shift;
103     my %args = %{ shift() };
104
105     return bless { %args }, $class;
106 }
107
108 sub runtests {
109     my $self = shift;
110
111     my @switches = ();
112     if ($self->{switches}) {
113         push @switches, $self->{switches};
114     }
115     if ($self->{lib}) {
116         foreach (@{$self->{lib}}) {
117             my $l = $_;
118
119             # It seems that $switches is getting interpreted with 'eval' or
120             # something like that, and that we need to take care of backslashes
121             # or they will disappear along the way.
122             $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
123             push @switches, "-I$l";
124         }
125     }
126
127     $Test::Harness::switches = join(' ', @switches);
128     Test::Harness::runtests(@_);
129 }