2 # Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
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
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.
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});
24 use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
27 use lib "$FindBin::Bin/../util/perl";
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;
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"));
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
45 $ENV{'MALLOC_PERTURB_'} = '128' if !defined $ENV{'MALLOC_PERTURB_'};
48 ( verbosity => $ENV{HARNESS_VERBOSE} ? 1 : 0,
52 timer => $ENV{HARNESS_TIMER} ? 1 : 0,
56 if ($ENV{HARNESS_VERBOSE}) {
57 print "Warning: HARNESS_JOBS > 1 ignored with HARNESS_VERBOSE\n";
59 $tapargs{jobs} = $jobs;
60 print "Using HARNESS_JOBS=$jobs\n";
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 = ();
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});
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;
83 my @alltests = find_matching_tests("*");
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-/;
97 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
100 (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
105 if ($arg eq 'alltests') {
106 warn "'alltests' encountered, ignoring everything before that...\n"
108 %tests = map { $_ => 1 } @alltests;
109 } elsif ($arg =~ m/^(-?)(.*)/) {
112 my @matches = find_matching_tests($test);
114 # If '-foo' is the first arg, it's short for 'alltests -foo'
115 if ($sign eq '-' && $initial_arg) {
116 %tests = map { $_ => 1 } @alltests;
119 if (scalar @matches == 0) {
120 warn "Test $test found no match, skipping ",
121 ($sign eq '-' ? "removal" : "addition"),
124 foreach $test (@matches) {
126 delete $tests{$test};
133 warn "I don't know what '$arg' is about, ignoring...\n";
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};
145 sub find_matching_tests {
148 if ($glob =~ m|^[\d\[\]\?\-]+$|) {
149 return glob(catfile($recipesdir,"$glob-*.t"));
152 return glob(catfile($recipesdir,"*-$glob.t"));
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
162 # We use eval to avoid undue interruption if TAP::Harness isn't present.
168 package TAP::Parser::OpenSSL;
169 use parent -norequire, 'TAP::Parser';
174 my %opts = %{ shift() };
175 my $failure_verbosity = $openssl_args{failure_verbosity};
176 my @plans = (); # initial level, no plan yet
177 my $output_buffer = "";
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 = ();
184 if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
185 $callbacks{ALL} = sub { # on each line of test output
187 my $fh = $openssl_args{tap_copy};
188 print $fh $self->as_string, "\n"
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;
198 # workaround for parser not coping with sub-test indentation
199 if ($self->is_unknown) {
201 my $indent = $level < 0 ? "" : " " x ($level * 4);
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 /);
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;
219 } elsif ($self->as_string ne "") {
220 # typically is_comment or is_unknown
221 $output_buffer .= "\n".$self->as_string;
227 if ($failure_verbosity > 0) {
228 $callbacks{EOF} = sub {
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
236 print $_, "\n" foreach (("", @failure_output));
238 # Echo any trailing comments etc.
239 print "$output_buffer";
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 );
249 return $class->SUPER::new({ %opts });
252 package TAP::Harness::OpenSSL;
253 use parent -norequire, 'TAP::Harness';
254 require TAP::Harness;
258 $tapargs{parser_class} = "TAP::Parser::OpenSSL";
259 $package = 'TAP::Harness::OpenSSL';
262 unless (defined $eres) {
264 # Fake TAP::Harness in case it's not loaded
265 package TAP::Harness::fake;
266 use parent 'Test::Harness';
270 my %args = %{ shift() };
272 return bless { %args }, $class;
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] : $_ } @_;
283 if ($self->{switches}) {
284 push @switches, $self->{switches};
287 foreach (@{$self->{lib}}) {
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";
298 $Test::Harness::switches = join(' ', @switches);
299 Test::Harness::runtests(@args);
303 $package = 'TAP::Harness::fake';
307 unless (defined $eres) {
313 my $harness = $package->new(\%tapargs);
315 $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
318 if (ref($ret) ne "TAP::Parser::Aggregator" || !$ret->has_errors) {
320 $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
321 sort { reorder($a) cmp reorder($b) } keys %tests);
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
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.