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