test/run_tests.pl: Improve indentation parsing workaround for VFO and VFP mode
[openssl.git] / test / run_tests.pl
1 #! /usr/bin/env perl
2 # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (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 # Additionally, also recognise VERBOSE_FAILURE and VF.
14 BEGIN {
15     $ENV{HARNESS_VERBOSE} = "yes" if $ENV{VERBOSE} || $ENV{V};
16     $ENV{HARNESS_VERBOSE_FAILURE} = "yes" if $ENV{VERBOSE_FAILURE} || $ENV{VF};
17     $ENV{HARNESS_VERBOSE_FAILURES_ONLY} = "yes"
18         if $ENV{VERBOSE_FAILURES_ONLY} || $ENV{VFO};
19     $ENV{HARNESS_VERBOSE_FAILURES_PROGRESS} = "yes"
20         if $ENV{VERBOSE_FAILURES_PROGRESS} || $ENV{VFP};
21 }
22
23 use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
24 use File::Basename;
25 use FindBin;
26 use lib "$FindBin::Bin/../util/perl";
27 use OpenSSL::Glob;
28
29 my $srctop = $ENV{SRCTOP} || $ENV{TOP};
30 my $bldtop = $ENV{BLDTOP} || $ENV{TOP};
31 my $recipesdir = catdir($srctop, "test", "recipes");
32 my $libdir = rel2abs(catdir($srctop, "util", "perl"));
33
34 $ENV{OPENSSL_CONF} = rel2abs(catdir($srctop, "apps", "openssl.cnf"));
35 $ENV{OPENSSL_CONF_INCLUDE} = rel2abs(catdir($bldtop, "providers"));
36 $ENV{OPENSSL_MODULES} = rel2abs(catdir($bldtop, "providers"));
37 $ENV{OPENSSL_ENGINES} = rel2abs(catdir($bldtop, "engines"));
38 $ENV{CTLOG_FILE} = rel2abs(catdir($srctop, "test", "ct", "log_list.cnf"));
39
40 my %tapargs =
41     ( verbosity         => $ENV{HARNESS_VERBOSE} ? 1 : 0,
42       lib               => [ $libdir ],
43       switches          => '-w',
44       merge             => 1,
45     );
46
47 # Additional OpenSSL special TAP arguments.  Because we can't pass them via
48 # TAP::Harness->new(), they will be accessed directly, see the
49 # TAP::Parser::OpenSSL implementation further down
50 my %openssl_args = ();
51
52 $openssl_args{'failure_verbosity'} = $ENV{HARNESS_VERBOSE} ? 0 :
53     $ENV{HARNESS_VERBOSE_FAILURE} ? 3 :
54     $ENV{HARNESS_VERBOSE_FAILURES_PROGRESS} ? 2 :
55     $ENV{HARNESS_VERBOSE_FAILURES_ONLY} ? 1 : 0;
56
57 my $outfilename = $ENV{HARNESS_TAP_COPY};
58 open $openssl_args{'tap_copy'}, ">$outfilename"
59     or die "Trying to create $outfilename: $!\n"
60     if defined $outfilename;
61
62 my @alltests = find_matching_tests("*");
63 my %tests = ();
64
65 my $initial_arg = 1;
66 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
67     if ($arg eq 'list') {
68         foreach (@alltests) {
69             (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
70             print $x,"\n";
71         }
72         exit 0;
73     }
74     if ($arg eq 'alltests') {
75         warn "'alltests' encountered, ignoring everything before that...\n"
76             unless $initial_arg;
77         %tests = map { $_ => basename($_) } @alltests;
78     } elsif ($arg =~ m/^(-?)(.*)/) {
79         my $sign = $1;
80         my $test = $2;
81         my @matches = find_matching_tests($test);
82
83         # If '-foo' is the first arg, it's short for 'alltests -foo'
84         if ($sign eq '-' && $initial_arg) {
85             %tests = map { $_ => basename($_) } @alltests;
86         }
87
88         if (scalar @matches == 0) {
89             warn "Test $test found no match, skipping ",
90                 ($sign eq '-' ? "removal" : "addition"),
91                 "...\n";
92         } else {
93             foreach $test (@matches) {
94                 if ($sign eq '-') {
95                     delete $tests{$test};
96                 } else {
97                     $tests{$test} = basename($test);
98                 }
99             }
100         }
101     } else {
102         warn "I don't know what '$arg' is about, ignoring...\n";
103     }
104
105     $initial_arg = 0;
106 }
107
108 sub find_matching_tests {
109     my ($glob) = @_;
110
111     if ($glob =~ m|^[\d\[\]\?\-]+$|) {
112         return glob(catfile($recipesdir,"$glob-*.t"));
113     }
114     return glob(catfile($recipesdir,"*-$glob.t"));
115 }
116
117 # The following is quite a bit of hackery to adapt to both TAP::Harness
118 # and Test::Harness, depending on what's available.
119 # The TAP::Harness hack allows support for HARNESS_VERBOSE_FAILURE* and
120 # HARNESS_TAP_COPY, while the Test::Harness hack can't, because the pre
121 # TAP::Harness Test::Harness simply doesn't have support for this sort of
122 # thing.
123 #
124 # We use eval to avoid undue interruption if TAP::Harness isn't present.
125
126 my $package;
127 my $eres;
128
129 $eres = eval {
130     package TAP::Parser::OpenSSL;
131     use parent 'TAP::Parser';
132
133     sub new {
134         my $class = shift;
135         my %opts = %{ shift() };
136         my $failure_verbosity = $openssl_args{failure_verbosity};
137         my @plans = (); # initial level, no plan yet
138         my $output_buffer = "";
139
140         # We rely heavily on perl closures to make failure verbosity work
141         # We need to do so, because there's no way to safely pass extra
142         # objects down all the way to the TAP::Parser::Result object
143         my @failure_output = ();
144         my %callbacks = ();
145         if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
146             $callbacks{ALL} = sub { # on each line of test output
147                 my $self = shift;
148                 my $fh = $openssl_args{tap_copy};
149                 print $fh $self->as_string, "\n"
150                     if defined $fh;
151
152                 my $failure_verbosity = $openssl_args{failure_verbosity};
153                 if ($failure_verbosity == 3) {
154                     push @failure_output, $self->as_string;
155                 } elsif ($failure_verbosity > 0) {
156                     my $is_plan = $self->is_plan;
157                     my $tests_planned = $is_plan && $self->tests_planned;
158                     my $is_test = $self->is_test;
159                     my $is_ok = $is_test && $self->is_ok;
160
161                     # workaround for parser not coping with sub-test indentation
162                     if ($self->is_unknown) {
163                         my $level = $#plans;
164                         my $indent = $level < 0 ? "" : " " x ($level * 4);
165
166                         ($is_plan, $tests_planned) = (1, $1)
167                             if ($self->as_string =~ m/^$indent    1\.\.(\d+)/);
168                         ($is_test, $is_ok) = (1, !$1)
169                             if ($self->as_string =~ m/^$indent(not )?ok /);
170                     }
171
172                     if ($is_plan) {
173                         push @plans, $tests_planned;
174                         $output_buffer = ""; # ignore comments etc. until plan
175                     } elsif ($is_test) { # result of a test
176                         pop @plans if @plans && --($plans[-1]) <= 0;
177                         print $output_buffer if !$is_ok;
178                         print "\n".$self->as_string
179                             if !$is_ok || $failure_verbosity == 2;
180                         $output_buffer = "";
181                     } elsif ($self->as_string ne "") {
182                         # typically is_comment or is_unknown
183                         $output_buffer .= "\n".$self->as_string;
184                     }
185                 }
186             }
187         }
188
189         if ($failure_verbosity > 0) {
190             $callbacks{EOF} = sub {
191                 my $self = shift;
192
193                 # We know we are a TAP::Parser::Aggregator object
194                 if (scalar $self->failed > 0 && @failure_output) {
195                     # We add an extra empty line, because in the case of a
196                     # progress counter, we're still at the end of that progress
197                     # line.
198                     print $_, "\n" foreach (("", @failure_output));
199                 }
200                 # Echo any trailing comments etc.
201                 print "$output_buffer" if $failure_verbosity != 3;
202             };
203         }
204
205         if (keys %callbacks) {
206             # If %opts already has a callbacks element, the order here
207             # ensures we do not override it
208             %opts = ( callbacks => { %callbacks }, %opts );
209         }
210
211         return $class->SUPER::new({ %opts });
212     }
213
214     package TAP::Harness::OpenSSL;
215     use parent 'TAP::Harness';
216
217     package main;
218
219     $tapargs{parser_class} = "TAP::Parser::OpenSSL";
220     $package = 'TAP::Harness::OpenSSL';
221 };
222
223 unless (defined $eres) {
224     $eres = eval {
225         # Fake TAP::Harness in case it's not loaded
226         package TAP::Harness::fake;
227         use parent 'Test::Harness';
228
229         sub new {
230             my $class = shift;
231             my %args = %{ shift() };
232
233             return bless { %args }, $class;
234         }
235
236         sub runtests {
237             my $self = shift;
238
239             # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
240             # elements, so convert such elements to just be the filename
241             my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
242
243             my @switches = ();
244             if ($self->{switches}) {
245                 push @switches, $self->{switches};
246             }
247             if ($self->{lib}) {
248                 foreach (@{$self->{lib}}) {
249                     my $l = $_;
250
251                     # It seems that $switches is getting interpreted with 'eval'
252                     # or something like that, and that we need to take care of
253                     # backslashes or they will disappear along the way.
254                     $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
255                     push @switches, "-I$l";
256                 }
257             }
258
259             $Test::Harness::switches = join(' ', @switches);
260             Test::Harness::runtests(@args);
261         }
262
263         package main;
264         $package = 'TAP::Harness::fake';
265     };
266 }
267
268 unless (defined $eres) {
269     print $@,"\n" if $@;
270     print $!,"\n" if $!;
271     exit 127;
272 }
273
274 my $harness = $package->new(\%tapargs);
275 my $ret =
276     $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), $tests{$_} ] }
277                        sort keys %tests);
278
279 # $ret->has_errors may be any number, not just 0 or 1.  On VMS, numbers
280 # from 2 and on are used as is as VMS statuses, which has severity encoded
281 # in the lower 3 bits.  0 and 1, on the other hand, generate SUCCESS and
282 # FAILURE, so for currect reporting on all platforms, we make sure the only
283 # exit codes are 0 and 1.  Double-bang is the trick to do so.
284 exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
285
286 # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
287 # which simply dies at the end if any test failed, so we don't need to bother
288 # with any exit code in that case.