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