1171eec2f75ed1280018b5ffa057174a95946990
[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/:bsd_glob/;
20 use Module::Load::Conditional qw(can_load);
21
22 my $TAP_Harness = can_load(modules => { 'TAP::Harness' => undef }) 
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 $libdir = rel2abs(catdir($srctop, "util", "perl"));
29
30 my %tapargs =
31     ( verbosity => $ENV{VERBOSE} || $ENV{V} || $ENV{HARNESS_VERBOSE} ? 1 : 0,
32       lib       => [ $libdir ],
33       switches  => '-w',
34       merge     => 1
35     );
36
37 my @alltests = find_matching_tests("*");
38 my %tests = ();
39
40 my $initial_arg = 1;
41 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
42     if ($arg eq 'list') {
43         foreach (@alltests) {
44             (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
45             print $x,"\n";
46         }
47         exit 0;
48     }
49     if ($arg eq 'alltests') {
50         warn "'alltests' encountered, ignoring everything before that...\n"
51             unless $initial_arg;
52         %tests = map { $_ => 1 } @alltests;
53     } elsif ($arg =~ m/^(-?)(.*)/) {
54         my $sign = $1;
55         my $test = $2;
56         my @matches = find_matching_tests($test);
57
58         # If '-foo' is the first arg, it's short for 'alltests -foo'
59         if ($sign eq '-' && $initial_arg) {
60             %tests = map { $_ => 1 } @alltests;
61         }
62
63         if (scalar @matches == 0) {
64             warn "Test $test found no match, skipping ",
65                 ($sign eq '-' ? "removal" : "addition"),
66                 "...\n";
67         } else {
68             foreach $test (@matches) {
69                 if ($sign eq '-') {
70                     delete $tests{$test};
71                 } else {
72                     $tests{$test} = 1;
73                 }
74             }
75         }
76     } else {
77         warn "I don't know what '$arg' is about, ignoring...\n";
78     }
79
80     $initial_arg = 0;
81 }
82
83 my $harness = $TAP_Harness->new(\%tapargs);
84 my $ret = $harness->runtests(map { abs2rel($_, rel2abs(curdir())); }
85                                  sort keys %tests);
86
87 # $ret->has_errors may be any number, not just 0 or 1.  On VMS, numbers
88 # from 2 and on are used as is as VMS statuses, which has severity encoded
89 # in the lower 3 bits.  0 and 1, on the other hand, generate SUCCESS and
90 # FAILURE, so for currect reporting on all platforms, we make sure the only
91 # exit codes are 0 and 1.  Double-bang is the trick to do so.
92 exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
93
94 # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
95 # which simply dies at the end if any test failed, so we don't need to bother
96 # with any exit code in that case.
97
98 sub find_matching_tests {
99     my ($glob) = @_;
100
101     if ($glob =~ m|^[\d\[\]\?\-]+$|) {
102         return glob(catfile($recipesdir,"$glob-*.t"));
103     }
104     return glob(catfile($recipesdir,"*-$glob.t"));
105 }
106
107
108 # Fake TAP::Harness in case it's not loaded
109 use Test::Harness;
110 package OpenSSL::TAP::Harness;
111
112 sub new {
113     my $class = shift;
114     my %args = %{ shift() };
115
116     return bless { %args }, $class;
117 }
118
119 sub runtests {
120     my $self = shift;
121
122     my @switches = ();
123     if ($self->{switches}) {
124         push @switches, $self->{switches};
125     }
126     if ($self->{lib}) {
127         foreach (@{$self->{lib}}) {
128             my $l = $_;
129
130             # It seems that $switches is getting interpreted with 'eval' or
131             # something like that, and that we need to take care of backslashes
132             # or they will disappear along the way.
133             $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
134             push @switches, "-I$l";
135         }
136     }
137
138     $Test::Harness::switches = join(' ', @switches);
139     Test::Harness::runtests(@_);
140 }