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