Add an Apple privacy info file for OpenSSL
[openssl.git] / test / run_tests.pl
1 #! /usr/bin/env perl
2 # Copyright 2015-2022 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(catfile($srctop, "apps", "openssl.cnf"));
37 $ENV{OPENSSL_CONF_INCLUDE} = rel2abs(catdir($bldtop, "test"));
38 $ENV{OPENSSL_MODULES} = rel2abs(catdir($bldtop, "providers"));
39 $ENV{OPENSSL_ENGINES} = rel2abs(catdir($bldtop, "engines"));
40 $ENV{CTLOG_FILE} = rel2abs(catfile($srctop, "test", "ct", "log_list.cnf"));
41
42 # On platforms that support this, this will ensure malloc returns data that is
43 # set to a non-zero value. Can be helpful for detecting uninitialized reads in
44 # some situations.
45 $ENV{'MALLOC_PERTURB_'} = '128' if !defined $ENV{'MALLOC_PERTURB_'};
46
47 my %tapargs =
48     ( verbosity         => $ENV{HARNESS_VERBOSE} ? 1 : 0,
49       lib               => [ $libdir ],
50       switches          => '-w',
51       merge             => 1,
52       timer             => $ENV{HARNESS_TIMER} ? 1 : 0,
53     );
54
55 if ($jobs > 1) {
56     if ($ENV{HARNESS_VERBOSE}) {
57         print "Warning: HARNESS_JOBS > 1 ignored with HARNESS_VERBOSE\n";
58     } else {
59         $tapargs{jobs} = $jobs;
60         print "Using HARNESS_JOBS=$jobs\n";
61     }
62 }
63
64 # Additional OpenSSL special TAP arguments.  Because we can't pass them via
65 # TAP::Harness->new(), they will be accessed directly, see the
66 # TAP::Parser::OpenSSL implementation further down
67 my %openssl_args = ();
68
69 $openssl_args{'failure_verbosity'} = $ENV{HARNESS_VERBOSE} ? 0 :
70     $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} ? 2 :
71     1; # $ENV{HARNESS_VERBOSE_FAILURE}
72 print "Warning: HARNESS_VERBOSE overrides HARNESS_VERBOSE_FAILURE*\n"
73     if ($ENV{HARNESS_VERBOSE} && ($ENV{HARNESS_VERBOSE_FAILURE}
74                                   || $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS}));
75 print "Warning: HARNESS_VERBOSE_FAILURE_PROGRESS overrides HARNESS_VERBOSE_FAILURE\n"
76     if ($ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} && $ENV{HARNESS_VERBOSE_FAILURE});
77
78 my $outfilename = $ENV{HARNESS_TAP_COPY};
79 open $openssl_args{'tap_copy'}, ">$outfilename"
80     or die "Trying to create $outfilename: $!\n"
81     if defined $outfilename;
82
83 my @alltests = find_matching_tests("*");
84 my %tests = ();
85
86 sub reorder {
87     my $key = pop;
88
89     # for parallel test runs, do slow tests first
90     if ($jobs > 1 && $key =~ m/test_ssl_new|test_fuzz/) {
91         $key =~ s/(\d+)-/01-/;
92     }
93     return $key;
94 }
95
96 my $initial_arg = 1;
97 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
98     if ($arg eq 'list') {
99         foreach (@alltests) {
100             (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
101             print $x,"\n";
102         }
103         exit 0;
104     }
105     if ($arg eq 'alltests') {
106         warn "'alltests' encountered, ignoring everything before that...\n"
107             unless $initial_arg;
108         %tests = map { $_ => 1 } @alltests;
109     } elsif ($arg =~ m/^(-?)(.*)/) {
110         my $sign = $1;
111         my $test = $2;
112         my @matches = find_matching_tests($test);
113
114         # If '-foo' is the first arg, it's short for 'alltests -foo'
115         if ($sign eq '-' && $initial_arg) {
116             %tests = map { $_ => 1 } @alltests;
117         }
118
119         if (scalar @matches == 0) {
120             warn "Test $test found no match, skipping ",
121                 ($sign eq '-' ? "removal" : "addition"),
122                 "...\n";
123         } else {
124             foreach $test (@matches) {
125                 if ($sign eq '-') {
126                     delete $tests{$test};
127                 } else {
128                     $tests{$test} = 1;
129                 }
130             }
131         }
132     } else {
133         warn "I don't know what '$arg' is about, ignoring...\n";
134     }
135
136     $initial_arg = 0;
137 }
138
139 # prep recipes are mandatory and need to be always run first
140 my @preps = glob(catfile($recipesdir,"00-prep_*.t"));
141 foreach my $test (@preps) {
142     delete $tests{$test};
143 }
144
145 sub find_matching_tests {
146     my ($glob) = @_;
147
148     if ($glob =~ m|^[\d\[\]\?\-]+$|) {
149         return glob(catfile($recipesdir,"$glob-*.t"));
150     }
151
152     return glob(catfile($recipesdir,"*-$glob.t"));
153 }
154
155 # The following is quite a bit of hackery to adapt to both TAP::Harness
156 # and Test::Harness, depending on what's available.
157 # The TAP::Harness hack allows support for HARNESS_VERBOSE_FAILURE* and
158 # HARNESS_TAP_COPY, while the Test::Harness hack can't, because the pre
159 # TAP::Harness Test::Harness simply doesn't have support for this sort of
160 # thing.
161 #
162 # We use eval to avoid undue interruption if TAP::Harness isn't present.
163
164 my $package;
165 my $eres;
166
167 $eres = eval {
168     package TAP::Parser::OpenSSL;
169     use parent -norequire, 'TAP::Parser';
170     require TAP::Parser;
171
172     sub new {
173         my $class = shift;
174         my %opts = %{ shift() };
175         my $failure_verbosity = $openssl_args{failure_verbosity};
176         my @plans = (); # initial level, no plan yet
177         my $output_buffer = "";
178
179         # We rely heavily on perl closures to make failure verbosity work
180         # We need to do so, because there's no way to safely pass extra
181         # objects down all the way to the TAP::Parser::Result object
182         my @failure_output = ();
183         my %callbacks = ();
184         if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
185             $callbacks{ALL} = sub { # on each line of test output
186                 my $self = shift;
187                 my $fh = $openssl_args{tap_copy};
188                 print $fh $self->as_string, "\n"
189                     if defined $fh;
190
191                 my $failure_verbosity = $openssl_args{failure_verbosity};
192                 if ($failure_verbosity > 0) {
193                     my $is_plan = $self->is_plan;
194                     my $tests_planned = $is_plan && $self->tests_planned;
195                     my $is_test = $self->is_test;
196                     my $is_ok = $is_test && $self->is_ok;
197
198                     # workaround for parser not coping with sub-test indentation
199                     if ($self->is_unknown) {
200                         my $level = $#plans;
201                         my $indent = $level < 0 ? "" : " " x ($level * 4);
202
203                         ($is_plan, $tests_planned) = (1, $1)
204                             if ($self->as_string =~ m/^$indent    1\.\.(\d+)/);
205                         ($is_test, $is_ok) = (1, !$1)
206                             if ($self->as_string =~ m/^$indent(not )?ok /);
207                     }
208
209                     if ($is_plan) {
210                         push @plans, $tests_planned;
211                         $output_buffer = ""; # ignore comments etc. until plan
212                     } elsif ($is_test) { # result of a test
213                         pop @plans if @plans && --($plans[-1]) <= 0;
214                         print $output_buffer if !$is_ok;
215                         print "\n".$self->as_string
216                             if !$is_ok || $failure_verbosity == 2;
217                         print "\n# ------------------------------------------------------------------------------" if !$is_ok;
218                         $output_buffer = "";
219                     } elsif ($self->as_string ne "") {
220                         # typically is_comment or is_unknown
221                         $output_buffer .= "\n".$self->as_string;
222                     }
223                 }
224             }
225         }
226
227         if ($failure_verbosity > 0) {
228             $callbacks{EOF} = sub {
229                 my $self = shift;
230
231                 # We know we are a TAP::Parser::Aggregator object
232                 if (scalar $self->failed > 0 && @failure_output) {
233                     # We add an extra empty line, because in the case of a
234                     # progress counter, we're still at the end of that progress
235                     # line.
236                     print $_, "\n" foreach (("", @failure_output));
237                 }
238                 # Echo any trailing comments etc.
239                 print "$output_buffer";
240             };
241         }
242
243         if (keys %callbacks) {
244             # If %opts already has a callbacks element, the order here
245             # ensures we do not override it
246             %opts = ( callbacks => { %callbacks }, %opts );
247         }
248
249         return $class->SUPER::new({ %opts });
250     }
251
252     package TAP::Harness::OpenSSL;
253     use parent -norequire, 'TAP::Harness';
254     require TAP::Harness;
255
256     package main;
257
258     $tapargs{parser_class} = "TAP::Parser::OpenSSL";
259     $package = 'TAP::Harness::OpenSSL';
260 };
261
262 unless (defined $eres) {
263     $eres = eval {
264         # Fake TAP::Harness in case it's not loaded
265         package TAP::Harness::fake;
266         use parent 'Test::Harness';
267
268         sub new {
269             my $class = shift;
270             my %args = %{ shift() };
271
272             return bless { %args }, $class;
273         }
274
275         sub runtests {
276             my $self = shift;
277
278             # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
279             # elements, so convert such elements to just be the filename
280             my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
281
282             my @switches = ();
283             if ($self->{switches}) {
284                 push @switches, $self->{switches};
285             }
286             if ($self->{lib}) {
287                 foreach (@{$self->{lib}}) {
288                     my $l = $_;
289
290                     # It seems that $switches is getting interpreted with 'eval'
291                     # or something like that, and that we need to take care of
292                     # backslashes or they will disappear along the way.
293                     $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
294                     push @switches, "-I$l";
295                 }
296             }
297
298             $Test::Harness::switches = join(' ', @switches);
299             Test::Harness::runtests(@args);
300         }
301
302         package main;
303         $package = 'TAP::Harness::fake';
304     };
305 }
306
307 unless (defined $eres) {
308     print $@,"\n" if $@;
309     print $!,"\n" if $!;
310     exit 127;
311 }
312
313 my $harness = $package->new(\%tapargs);
314 my $ret =
315     $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
316                        @preps);
317
318 if (ref($ret) ne "TAP::Parser::Aggregator" || !$ret->has_errors) {
319     $ret =
320         $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
321                            sort { reorder($a) cmp reorder($b) } keys %tests);
322 }
323
324 # If this is a TAP::Parser::Aggregator, $ret->has_errors is the count of
325 # tests that failed.  We don't bother with that exact number, just exit
326 # with an appropriate exit code when it isn't zero.
327 if (ref($ret) eq "TAP::Parser::Aggregator") {
328     exit 0 unless $ret->has_errors;
329     exit 1 unless $^O eq 'VMS';
330     # On VMS, perl converts an exit 1 to SS$_ABORT (%SYSTEM-F-ABORT), which
331     # is a bit harsh.  As per perl recommendations, we explicitly use the
332     # same VMS status code as typical C programs would for exit(1), except
333     # we set the error severity rather than success.
334     # Ref: https://perldoc.perl.org/perlport#exit
335     #      https://perldoc.perl.org/perlvms#$?
336     exit  0x35a000              # C facility code
337         + 8                     # 1 << 3 (to make space for the 3 severity bits)
338         + 2                     # severity: E(rror)
339         + 0x10000000;           # bit 28 set => the shell stays silent
340 }
341
342 # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
343 # which simply dies at the end if any test failed, so we don't need to bother
344 # with any exit code in that case.