2 # Copyright 2015-2021 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(catdir($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(catdir($srctop, "test", "ct", "log_list.cnf"));
43 ( verbosity => $ENV{HARNESS_VERBOSE} ? 1 : 0,
47 timer => $ENV{HARNESS_TIMER} ? 1 : 0,
51 if ($ENV{HARNESS_VERBOSE}) {
52 print "Warning: HARNESS_JOBS > 1 ignored with HARNESS_VERBOSE\n";
54 $tapargs{jobs} = $jobs;
55 print "Using HARNESS_JOBS=$jobs\n";
59 # Additional OpenSSL special TAP arguments. Because we can't pass them via
60 # TAP::Harness->new(), they will be accessed directly, see the
61 # TAP::Parser::OpenSSL implementation further down
62 my %openssl_args = ();
64 $openssl_args{'failure_verbosity'} = $ENV{HARNESS_VERBOSE} ? 0 :
65 $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} ? 2 :
66 1; # $ENV{HARNESS_VERBOSE_FAILURE}
67 print "Warning: HARNESS_VERBOSE overrides HARNESS_VERBOSE_FAILURE*\n"
68 if ($ENV{HARNESS_VERBOSE} && ($ENV{HARNESS_VERBOSE_FAILURE}
69 || $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS}));
70 print "Warning: HARNESS_VERBOSE_FAILURE_PROGRESS overrides HARNESS_VERBOSE_FAILURE\n"
71 if ($ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} && $ENV{HARNESS_VERBOSE_FAILURE});
73 my $outfilename = $ENV{HARNESS_TAP_COPY};
74 open $openssl_args{'tap_copy'}, ">$outfilename"
75 or die "Trying to create $outfilename: $!\n"
76 if defined $outfilename;
78 my @alltests = find_matching_tests("*");
84 # for parallel test runs, do slow tests first
85 if ($jobs > 1 && $key =~ m/test_ssl_new|test_fuzz/) {
86 $key =~ s/(\d+)-/00-/;
92 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
95 (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
100 if ($arg eq 'alltests') {
101 warn "'alltests' encountered, ignoring everything before that...\n"
103 %tests = map { $_ => 1 } @alltests;
104 } elsif ($arg =~ m/^(-?)(.*)/) {
107 my @matches = find_matching_tests($test);
109 # If '-foo' is the first arg, it's short for 'alltests -foo'
110 if ($sign eq '-' && $initial_arg) {
111 %tests = map { $_ => 1 } @alltests;
114 if (scalar @matches == 0) {
115 warn "Test $test found no match, skipping ",
116 ($sign eq '-' ? "removal" : "addition"),
119 foreach $test (@matches) {
121 delete $tests{$test};
128 warn "I don't know what '$arg' is about, ignoring...\n";
134 sub find_matching_tests {
137 # prep recipes are mandatory
138 my @recipes = glob(catfile($recipesdir,"00-prep_*.t"));
140 if ($glob =~ m|^[\d\[\]\?\-]+$|) {
141 push @recipes, glob(catfile($recipesdir,"$glob-*.t"));
143 push @recipes, glob(catfile($recipesdir,"*-$glob.t"));
148 # The following is quite a bit of hackery to adapt to both TAP::Harness
149 # and Test::Harness, depending on what's available.
150 # The TAP::Harness hack allows support for HARNESS_VERBOSE_FAILURE* and
151 # HARNESS_TAP_COPY, while the Test::Harness hack can't, because the pre
152 # TAP::Harness Test::Harness simply doesn't have support for this sort of
155 # We use eval to avoid undue interruption if TAP::Harness isn't present.
161 package TAP::Parser::OpenSSL;
162 use parent -norequire, 'TAP::Parser';
167 my %opts = %{ shift() };
168 my $failure_verbosity = $openssl_args{failure_verbosity};
169 my @plans = (); # initial level, no plan yet
170 my $output_buffer = "";
172 # We rely heavily on perl closures to make failure verbosity work
173 # We need to do so, because there's no way to safely pass extra
174 # objects down all the way to the TAP::Parser::Result object
175 my @failure_output = ();
177 if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
178 $callbacks{ALL} = sub { # on each line of test output
180 my $fh = $openssl_args{tap_copy};
181 print $fh $self->as_string, "\n"
184 my $failure_verbosity = $openssl_args{failure_verbosity};
185 if ($failure_verbosity > 0) {
186 my $is_plan = $self->is_plan;
187 my $tests_planned = $is_plan && $self->tests_planned;
188 my $is_test = $self->is_test;
189 my $is_ok = $is_test && $self->is_ok;
191 # workaround for parser not coping with sub-test indentation
192 if ($self->is_unknown) {
194 my $indent = $level < 0 ? "" : " " x ($level * 4);
196 ($is_plan, $tests_planned) = (1, $1)
197 if ($self->as_string =~ m/^$indent 1\.\.(\d+)/);
198 ($is_test, $is_ok) = (1, !$1)
199 if ($self->as_string =~ m/^$indent(not )?ok /);
203 push @plans, $tests_planned;
204 $output_buffer = ""; # ignore comments etc. until plan
205 } elsif ($is_test) { # result of a test
206 pop @plans if @plans && --($plans[-1]) <= 0;
207 print $output_buffer if !$is_ok;
208 print "\n".$self->as_string
209 if !$is_ok || $failure_verbosity == 2;
210 print "\n# ------------------------------------------------------------------------------" if !$is_ok;
212 } elsif ($self->as_string ne "") {
213 # typically is_comment or is_unknown
214 $output_buffer .= "\n".$self->as_string;
220 if ($failure_verbosity > 0) {
221 $callbacks{EOF} = sub {
224 # We know we are a TAP::Parser::Aggregator object
225 if (scalar $self->failed > 0 && @failure_output) {
226 # We add an extra empty line, because in the case of a
227 # progress counter, we're still at the end of that progress
229 print $_, "\n" foreach (("", @failure_output));
231 # Echo any trailing comments etc.
232 print "$output_buffer";
236 if (keys %callbacks) {
237 # If %opts already has a callbacks element, the order here
238 # ensures we do not override it
239 %opts = ( callbacks => { %callbacks }, %opts );
242 return $class->SUPER::new({ %opts });
245 package TAP::Harness::OpenSSL;
246 use parent -norequire, 'TAP::Harness';
247 require TAP::Harness;
251 $tapargs{parser_class} = "TAP::Parser::OpenSSL";
252 $package = 'TAP::Harness::OpenSSL';
255 unless (defined $eres) {
257 # Fake TAP::Harness in case it's not loaded
258 package TAP::Harness::fake;
259 use parent 'Test::Harness';
263 my %args = %{ shift() };
265 return bless { %args }, $class;
271 # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
272 # elements, so convert such elements to just be the filename
273 my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
276 if ($self->{switches}) {
277 push @switches, $self->{switches};
280 foreach (@{$self->{lib}}) {
283 # It seems that $switches is getting interpreted with 'eval'
284 # or something like that, and that we need to take care of
285 # backslashes or they will disappear along the way.
286 $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
287 push @switches, "-I$l";
291 $Test::Harness::switches = join(' ', @switches);
292 Test::Harness::runtests(@args);
296 $package = 'TAP::Harness::fake';
300 unless (defined $eres) {
306 my $harness = $package->new(\%tapargs);
308 $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
309 sort { reorder($a) cmp reorder($b) } keys %tests);
311 # $ret->has_errors may be any number, not just 0 or 1. On VMS, numbers
312 # from 2 and on are used as is as VMS statuses, which has severity encoded
313 # in the lower 3 bits. 0 and 1, on the other hand, generate SUCCESS and
314 # FAILURE, so for currect reporting on all platforms, we make sure the only
315 # exit codes are 0 and 1. Double-bang is the trick to do so.
316 exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
318 # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
319 # which simply dies at the end if any test failed, so we don't need to bother
320 # with any exit code in that case.