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};
19 use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
22 use lib "$FindBin::Bin/../util/perl";
25 my $srctop = $ENV{SRCTOP} || $ENV{TOP};
26 my $bldtop = $ENV{BLDTOP} || $ENV{TOP};
27 my $recipesdir = catdir($srctop, "test", "recipes");
28 my $libdir = rel2abs(catdir($srctop, "util", "perl"));
30 $ENV{OPENSSL_CONF} = rel2abs(catdir($srctop, "apps", "openssl.cnf"));
31 $ENV{OPENSSL_CONF_INCLUDE} = rel2abs(catdir($bldtop, "providers"));
32 $ENV{OPENSSL_MODULES} = rel2abs(catdir($bldtop, "providers"));
33 $ENV{OPENSSL_ENGINES} = rel2abs(catdir($bldtop, "engines"));
34 $ENV{CTLOG_FILE} = rel2abs(catdir($srctop, "test", "ct", "log_list.cnf"));
37 ( verbosity => $ENV{HARNESS_VERBOSE} ? 1 : 0,
43 # Additional OpenSSL special TAP arguments. Because we can't pass them via
44 # TAP::Harness->new(), they will be accessed directly, see the
45 # TAP::Parser::OpenSSL implementation further down
46 my %openssl_args = ();
48 $openssl_args{'failure_verbosity'} =
49 $ENV{HARNESS_VERBOSE_FAILURE} && $tapargs{verbosity} < 1 ? 1 : 0;
51 my $outfilename = $ENV{HARNESS_TAP_COPY};
52 open $openssl_args{'tap_copy'}, ">$outfilename"
53 or die "Trying to create $outfilename: $!\n"
54 if defined $outfilename;
56 my @alltests = find_matching_tests("*");
60 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
63 (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
68 if ($arg eq 'alltests') {
69 warn "'alltests' encountered, ignoring everything before that...\n"
71 %tests = map { $_ => basename($_) } @alltests;
72 } elsif ($arg =~ m/^(-?)(.*)/) {
75 my @matches = find_matching_tests($test);
77 # If '-foo' is the first arg, it's short for 'alltests -foo'
78 if ($sign eq '-' && $initial_arg) {
79 %tests = map { $_ => basename($_) } @alltests;
82 if (scalar @matches == 0) {
83 warn "Test $test found no match, skipping ",
84 ($sign eq '-' ? "removal" : "addition"),
87 foreach $test (@matches) {
91 $tests{$test} = basename($test);
96 warn "I don't know what '$arg' is about, ignoring...\n";
102 sub find_matching_tests {
105 if ($glob =~ m|^[\d\[\]\?\-]+$|) {
106 return glob(catfile($recipesdir,"$glob-*.t"));
108 return glob(catfile($recipesdir,"*-$glob.t"));
111 # The following is quite a bit of hackery to adapt to both TAP::Harness
112 # and Test::Harness, depending on what's available.
113 # The TAP::Harness hack allows support for HARNESS_VERBOSE_FAILURE and
114 # HARNESS_TAP_COPY, while the Test::Harness hack can't, because the pre
115 # TAP::Harness Test::Harness simply doesn't have support for this sort of
118 # We use eval to avoid undue interruption if TAP::Harness isn't present.
124 package TAP::Parser::OpenSSL;
125 use parent 'TAP::Parser';
129 my %opts = %{ shift() };
131 # We rely heavily on perl closures to make failure verbosity work
132 # We need to do so, because there's no way to safely pass extra
133 # objects down all the way to the TAP::Parser::Result object
134 my @failure_output = ();
136 if ($openssl_args{failure_verbosity}
137 || defined $openssl_args{tap_copy}) {
138 $callbacks{ALL} = sub {
140 my $fh = $openssl_args{tap_copy};
142 print $fh $self->as_string, "\n"
144 push @failure_output, $self->as_string
145 if $openssl_args{failure_verbosity} > 0;
149 if ($openssl_args{failure_verbosity} > 0) {
150 $callbacks{EOF} = sub {
153 # We know we are a TAP::Parser::Aggregator object
154 if (scalar $self->failed > 0 && @failure_output) {
155 # We add an extra empty line, because in the case of a
156 # progress counter, we're still at the end of that progress
158 print $_, "\n" foreach (("", @failure_output));
163 if (keys %callbacks) {
164 # If %opts already has a callbacks element, the order here
165 # ensures we do not override it
166 %opts = ( callbacks => { %callbacks }, %opts );
169 return $class->SUPER::new({ %opts });
172 package TAP::Harness::OpenSSL;
173 use parent 'TAP::Harness';
177 $tapargs{parser_class} = "TAP::Parser::OpenSSL";
178 $package = 'TAP::Harness::OpenSSL';
181 unless (defined $eres) {
183 # Fake TAP::Harness in case it's not loaded
184 package TAP::Harness::fake;
185 use parent 'Test::Harness';
189 my %args = %{ shift() };
191 return bless { %args }, $class;
197 # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
198 # elements, so convert such elements to just be the filename
199 my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
202 if ($self->{switches}) {
203 push @switches, $self->{switches};
206 foreach (@{$self->{lib}}) {
209 # It seems that $switches is getting interpreted with 'eval'
210 # or something like that, and that we need to take care of
211 # backslashes or they will disappear along the way.
212 $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
213 push @switches, "-I$l";
217 $Test::Harness::switches = join(' ', @switches);
218 Test::Harness::runtests(@args);
222 $package = 'TAP::Harness::fake';
226 unless (defined $eres) {
232 my $harness = $package->new(\%tapargs);
234 $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), $tests{$_} ] }
237 # $ret->has_errors may be any number, not just 0 or 1. On VMS, numbers
238 # from 2 and on are used as is as VMS statuses, which has severity encoded
239 # in the lower 3 bits. 0 and 1, on the other hand, generate SUCCESS and
240 # FAILURE, so for currect reporting on all platforms, we make sure the only
241 # exit codes are 0 and 1. Double-bang is the trick to do so.
242 exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
244 # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
245 # which simply dies at the end if any test failed, so we don't need to bother
246 # with any exit code in that case.