2 # Copyright 2015-2020 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};
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"));
43 ( verbosity => $ENV{HARNESS_VERBOSE} ? 1 : 0,
49 $tapargs{jobs} = $jobs if defined $jobs;
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 = ();
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_VERBOSE overrides HARNESS_VERBOSE_FAILURE*\n"
60 if ($ENV{HARNESS_VERBOSE} && ($ENV{HARNESS_VERBOSE_FAILURE}
61 || $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS}));
62 print "Warning: HARNESS_VERBOSE_FAILURE_PROGRESS overrides HARNESS_VERBOSE_FAILURE\n"
63 if ($ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} && $ENV{HARNESS_VERBOSE_FAILURE});
65 my $outfilename = $ENV{HARNESS_TAP_COPY};
66 open $openssl_args{'tap_copy'}, ">$outfilename"
67 or die "Trying to create $outfilename: $!\n"
68 if defined $outfilename;
70 my @alltests = find_matching_tests("*");
76 # for parallel test runs, do slow tests first
77 if (defined $jobs && $jobs > 1 && $key =~ m/test_ssl_new|test_fuzz/) {
78 $key =~ s/(\d+)-/00-/;
84 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
87 (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
92 if ($arg eq 'alltests') {
93 warn "'alltests' encountered, ignoring everything before that...\n"
95 %tests = map { $_ => 1 } @alltests;
96 } elsif ($arg =~ m/^(-?)(.*)/) {
99 my @matches = find_matching_tests($test);
101 # If '-foo' is the first arg, it's short for 'alltests -foo'
102 if ($sign eq '-' && $initial_arg) {
103 %tests = map { $_ => 1 } @alltests;
106 if (scalar @matches == 0) {
107 warn "Test $test found no match, skipping ",
108 ($sign eq '-' ? "removal" : "addition"),
111 foreach $test (@matches) {
113 delete $tests{$test};
120 warn "I don't know what '$arg' is about, ignoring...\n";
126 sub find_matching_tests {
129 if ($glob =~ m|^[\d\[\]\?\-]+$|) {
130 return glob(catfile($recipesdir,"$glob-*.t"));
132 return glob(catfile($recipesdir,"*-$glob.t"));
135 # The following is quite a bit of hackery to adapt to both TAP::Harness
136 # and Test::Harness, depending on what's available.
137 # The TAP::Harness hack allows support for HARNESS_VERBOSE_FAILURE* and
138 # HARNESS_TAP_COPY, while the Test::Harness hack can't, because the pre
139 # TAP::Harness Test::Harness simply doesn't have support for this sort of
142 # We use eval to avoid undue interruption if TAP::Harness isn't present.
148 package TAP::Parser::OpenSSL;
149 use parent 'TAP::Parser';
153 my %opts = %{ shift() };
154 my $failure_verbosity = $openssl_args{failure_verbosity};
155 my @plans = (); # initial level, no plan yet
156 my $output_buffer = "";
158 # We rely heavily on perl closures to make failure verbosity work
159 # We need to do so, because there's no way to safely pass extra
160 # objects down all the way to the TAP::Parser::Result object
161 my @failure_output = ();
163 if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
164 $callbacks{ALL} = sub { # on each line of test output
166 my $fh = $openssl_args{tap_copy};
167 print $fh $self->as_string, "\n"
170 my $failure_verbosity = $openssl_args{failure_verbosity};
171 if ($failure_verbosity > 0) {
172 my $is_plan = $self->is_plan;
173 my $tests_planned = $is_plan && $self->tests_planned;
174 my $is_test = $self->is_test;
175 my $is_ok = $is_test && $self->is_ok;
177 # workaround for parser not coping with sub-test indentation
178 if ($self->is_unknown) {
180 my $indent = $level < 0 ? "" : " " x ($level * 4);
182 ($is_plan, $tests_planned) = (1, $1)
183 if ($self->as_string =~ m/^$indent 1\.\.(\d+)/);
184 ($is_test, $is_ok) = (1, !$1)
185 if ($self->as_string =~ m/^$indent(not )?ok /);
189 push @plans, $tests_planned;
190 $output_buffer = ""; # ignore comments etc. until plan
191 } elsif ($is_test) { # result of a test
192 pop @plans if @plans && --($plans[-1]) <= 0;
193 print $output_buffer if !$is_ok;
194 print "\n".$self->as_string
195 if !$is_ok || $failure_verbosity == 2;
196 print "\n# ------------------------------------------------------------------------------" if !$is_ok;
198 } elsif ($self->as_string ne "") {
199 # typically is_comment or is_unknown
200 $output_buffer .= "\n".$self->as_string;
206 if ($failure_verbosity > 0) {
207 $callbacks{EOF} = sub {
210 # We know we are a TAP::Parser::Aggregator object
211 if (scalar $self->failed > 0 && @failure_output) {
212 # We add an extra empty line, because in the case of a
213 # progress counter, we're still at the end of that progress
215 print $_, "\n" foreach (("", @failure_output));
217 # Echo any trailing comments etc.
218 print "$output_buffer";
222 if (keys %callbacks) {
223 # If %opts already has a callbacks element, the order here
224 # ensures we do not override it
225 %opts = ( callbacks => { %callbacks }, %opts );
228 return $class->SUPER::new({ %opts });
231 package TAP::Harness::OpenSSL;
232 use parent 'TAP::Harness';
236 $tapargs{parser_class} = "TAP::Parser::OpenSSL";
237 $package = 'TAP::Harness::OpenSSL';
240 unless (defined $eres) {
242 # Fake TAP::Harness in case it's not loaded
243 package TAP::Harness::fake;
244 use parent 'Test::Harness';
248 my %args = %{ shift() };
250 return bless { %args }, $class;
256 # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
257 # elements, so convert such elements to just be the filename
258 my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
261 if ($self->{switches}) {
262 push @switches, $self->{switches};
265 foreach (@{$self->{lib}}) {
268 # It seems that $switches is getting interpreted with 'eval'
269 # or something like that, and that we need to take care of
270 # backslashes or they will disappear along the way.
271 $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
272 push @switches, "-I$l";
276 $Test::Harness::switches = join(' ', @switches);
277 Test::Harness::runtests(@args);
281 $package = 'TAP::Harness::fake';
285 unless (defined $eres) {
291 my $harness = $package->new(\%tapargs);
293 $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
294 sort { reorder($a) cmp reorder($b) } keys %tests);
296 # $ret->has_errors may be any number, not just 0 or 1. On VMS, numbers
297 # from 2 and on are used as is as VMS statuses, which has severity encoded
298 # in the lower 3 bits. 0 and 1, on the other hand, generate SUCCESS and
299 # FAILURE, so for currect reporting on all platforms, we make sure the only
300 # exit codes are 0 and 1. Double-bang is the trick to do so.
301 exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
303 # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
304 # which simply dies at the end if any test failed, so we don't need to bother
305 # with any exit code in that case.