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 and V which is common on other projects.
13 # Additionally, also recognise VERBOSE_FAILURE and VF.
15 $ENV{HARNESS_VERBOSE} = "yes" if $ENV{VERBOSE} || $ENV{V};
16 $ENV{HARNESS_VERBOSE_FAILURE} = "yes" if $ENV{VERBOSE_FAILURE} || $ENV{VF};
17 $ENV{HARNESS_VERBOSE_FAILURES_ONLY} = "yes"
18 if $ENV{VERBOSE_FAILURES_ONLY} || $ENV{VFO};
19 $ENV{HARNESS_VERBOSE_FAILURES_PROGRESS} = "yes"
20 if $ENV{VERBOSE_FAILURES_PROGRESS} || $ENV{VFP};
23 use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
26 use lib "$FindBin::Bin/../util/perl";
29 my $srctop = $ENV{SRCTOP} || $ENV{TOP};
30 my $bldtop = $ENV{BLDTOP} || $ENV{TOP};
31 my $recipesdir = catdir($srctop, "test", "recipes");
32 my $libdir = rel2abs(catdir($srctop, "util", "perl"));
34 $ENV{OPENSSL_CONF} = rel2abs(catdir($srctop, "apps", "openssl.cnf"));
35 $ENV{OPENSSL_CONF_INCLUDE} = rel2abs(catdir($bldtop, "providers"));
36 $ENV{OPENSSL_MODULES} = rel2abs(catdir($bldtop, "providers"));
37 $ENV{OPENSSL_ENGINES} = rel2abs(catdir($bldtop, "engines"));
38 $ENV{CTLOG_FILE} = rel2abs(catdir($srctop, "test", "ct", "log_list.cnf"));
41 ( verbosity => $ENV{HARNESS_VERBOSE} ? 1 : 0,
47 # Additional OpenSSL special TAP arguments. Because we can't pass them via
48 # TAP::Harness->new(), they will be accessed directly, see the
49 # TAP::Parser::OpenSSL implementation further down
50 my %openssl_args = ();
52 $openssl_args{'failure_verbosity'} = $ENV{HARNESS_VERBOSE} ? 0 :
53 $ENV{HARNESS_VERBOSE_FAILURE} ? 3 :
54 $ENV{HARNESS_VERBOSE_FAILURES_PROGRESS} ? 2 :
55 $ENV{HARNESS_VERBOSE_FAILURES_ONLY} ? 1 : 0;
57 my $outfilename = $ENV{HARNESS_TAP_COPY};
58 open $openssl_args{'tap_copy'}, ">$outfilename"
59 or die "Trying to create $outfilename: $!\n"
60 if defined $outfilename;
62 my @alltests = find_matching_tests("*");
66 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
69 (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
74 if ($arg eq 'alltests') {
75 warn "'alltests' encountered, ignoring everything before that...\n"
77 %tests = map { $_ => basename($_) } @alltests;
78 } elsif ($arg =~ m/^(-?)(.*)/) {
81 my @matches = find_matching_tests($test);
83 # If '-foo' is the first arg, it's short for 'alltests -foo'
84 if ($sign eq '-' && $initial_arg) {
85 %tests = map { $_ => basename($_) } @alltests;
88 if (scalar @matches == 0) {
89 warn "Test $test found no match, skipping ",
90 ($sign eq '-' ? "removal" : "addition"),
93 foreach $test (@matches) {
97 $tests{$test} = basename($test);
102 warn "I don't know what '$arg' is about, ignoring...\n";
108 sub find_matching_tests {
111 if ($glob =~ m|^[\d\[\]\?\-]+$|) {
112 return glob(catfile($recipesdir,"$glob-*.t"));
114 return glob(catfile($recipesdir,"*-$glob.t"));
117 # The following is quite a bit of hackery to adapt to both TAP::Harness
118 # and Test::Harness, depending on what's available.
119 # The TAP::Harness hack allows support for HARNESS_VERBOSE_FAILURE* and
120 # HARNESS_TAP_COPY, while the Test::Harness hack can't, because the pre
121 # TAP::Harness Test::Harness simply doesn't have support for this sort of
124 # We use eval to avoid undue interruption if TAP::Harness isn't present.
130 package TAP::Parser::OpenSSL;
131 use parent 'TAP::Parser';
135 my %opts = %{ shift() };
136 my $failure_verbosity = $openssl_args{failure_verbosity};
137 print "\n" if $failure_verbosity == 1 || $failure_verbosity == 2;
138 my @plans = (); # initial level, no plan yet
139 my $output_buffer = "";
141 # We rely heavily on perl closures to make failure verbosity work
142 # We need to do so, because there's no way to safely pass extra
143 # objects down all the way to the TAP::Parser::Result object
144 my @failure_output = ();
146 if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
147 $callbacks{ALL} = sub { # on each line of test output
149 my $fh = $openssl_args{tap_copy};
150 print $fh $self->as_string, "\n"
153 my $failure_verbosity = $openssl_args{failure_verbosity};
154 if ($failure_verbosity == 3) {
155 push @failure_output, $self->as_string;
156 } elsif ($failure_verbosity > 0) {
157 my $is_plan = $self->is_plan;
158 my $tests_planned = $is_plan && $self->tests_planned;
159 my $is_test = $self->is_test;
160 my $is_ok = $is_test && $self->is_ok;
161 # workaround in case parser not coping with indentation:
162 if ($self->is_unknown) {
163 ($is_plan, $tests_planned) = (1, $1)
164 if ($self->as_string =~ m/^\s+1\.\.(\d+)/);
165 ($is_test, $is_ok) = (1, !$1)
166 if ($self->as_string =~ m/^\s+(not )?ok /);
169 push @plans, $tests_planned;
170 $output_buffer = ""; # ignore comments etc. until plan
171 } elsif ($is_test) { # result of a test
172 pop @plans if @plans && --($plans[-1]) <= 0;
173 print $output_buffer if !$is_ok;
174 print $self->as_string."\n"
175 if !$is_ok || $failure_verbosity == 2;
177 } else { # typically comment or unknown
178 $output_buffer .= $self->as_string."\n";
184 if ($failure_verbosity > 0) {
185 $callbacks{EOF} = sub {
188 # We know we are a TAP::Parser::Aggregator object
189 if (scalar $self->failed > 0 && @failure_output) {
190 # We add an extra empty line, because in the case of a
191 # progress counter, we're still at the end of that progress
193 print $_, "\n" foreach (("", @failure_output));
195 # Echo any trailing comments etc.
196 print "$output_buffer" if $failure_verbosity != 3;
200 if (keys %callbacks) {
201 # If %opts already has a callbacks element, the order here
202 # ensures we do not override it
203 %opts = ( callbacks => { %callbacks }, %opts );
206 return $class->SUPER::new({ %opts });
209 package TAP::Harness::OpenSSL;
210 use parent 'TAP::Harness';
214 $tapargs{parser_class} = "TAP::Parser::OpenSSL";
215 $package = 'TAP::Harness::OpenSSL';
218 unless (defined $eres) {
220 # Fake TAP::Harness in case it's not loaded
221 package TAP::Harness::fake;
222 use parent 'Test::Harness';
226 my %args = %{ shift() };
228 return bless { %args }, $class;
234 # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
235 # elements, so convert such elements to just be the filename
236 my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
239 if ($self->{switches}) {
240 push @switches, $self->{switches};
243 foreach (@{$self->{lib}}) {
246 # It seems that $switches is getting interpreted with 'eval'
247 # or something like that, and that we need to take care of
248 # backslashes or they will disappear along the way.
249 $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
250 push @switches, "-I$l";
254 $Test::Harness::switches = join(' ', @switches);
255 Test::Harness::runtests(@args);
259 $package = 'TAP::Harness::fake';
263 unless (defined $eres) {
269 my $harness = $package->new(\%tapargs);
271 $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), $tests{$_} ] }
274 # $ret->has_errors may be any number, not just 0 or 1. On VMS, numbers
275 # from 2 and on are used as is as VMS statuses, which has severity encoded
276 # in the lower 3 bits. 0 and 1, on the other hand, generate SUCCESS and
277 # FAILURE, so for currect reporting on all platforms, we make sure the only
278 # exit codes are 0 and 1. Double-bang is the trick to do so.
279 exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
281 # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
282 # which simply dies at the end if any test failed, so we don't need to bother
283 # with any exit code in that case.