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 my @plans = (); # initial level, no plan yet
138 my $output_buffer = "";
140 # We rely heavily on perl closures to make failure verbosity work
141 # We need to do so, because there's no way to safely pass extra
142 # objects down all the way to the TAP::Parser::Result object
143 my @failure_output = ();
145 if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
146 $callbacks{ALL} = sub { # on each line of test output
148 my $fh = $openssl_args{tap_copy};
149 print $fh $self->as_string, "\n"
152 my $failure_verbosity = $openssl_args{failure_verbosity};
153 if ($failure_verbosity == 3) {
154 push @failure_output, $self->as_string;
155 } elsif ($failure_verbosity > 0) {
156 my $is_plan = $self->is_plan;
157 my $tests_planned = $is_plan && $self->tests_planned;
158 my $is_test = $self->is_test;
159 my $is_ok = $is_test && $self->is_ok;
161 # workaround for parser not coping with sub-test indentation
162 if ($self->is_unknown) {
164 my $indent = $level < 0 ? "" : " " x ($level * 4);
166 ($is_plan, $tests_planned) = (1, $1)
167 if ($self->as_string =~ m/^$indent 1\.\.(\d+)/);
168 ($is_test, $is_ok) = (1, !$1)
169 if ($self->as_string =~ m/^$indent(not )?ok /);
173 push @plans, $tests_planned;
174 $output_buffer = ""; # ignore comments etc. until plan
175 } elsif ($is_test) { # result of a test
176 pop @plans if @plans && --($plans[-1]) <= 0;
177 print $output_buffer if !$is_ok;
178 print "\n".$self->as_string
179 if !$is_ok || $failure_verbosity == 2;
181 } elsif ($self->as_string ne "") {
182 # typically is_comment or is_unknown
183 $output_buffer .= "\n".$self->as_string;
189 if ($failure_verbosity > 0) {
190 $callbacks{EOF} = sub {
193 # We know we are a TAP::Parser::Aggregator object
194 if (scalar $self->failed > 0 && @failure_output) {
195 # We add an extra empty line, because in the case of a
196 # progress counter, we're still at the end of that progress
198 print $_, "\n" foreach (("", @failure_output));
200 # Echo any trailing comments etc.
201 print "$output_buffer" if $failure_verbosity != 3;
205 if (keys %callbacks) {
206 # If %opts already has a callbacks element, the order here
207 # ensures we do not override it
208 %opts = ( callbacks => { %callbacks }, %opts );
211 return $class->SUPER::new({ %opts });
214 package TAP::Harness::OpenSSL;
215 use parent 'TAP::Harness';
219 $tapargs{parser_class} = "TAP::Parser::OpenSSL";
220 $package = 'TAP::Harness::OpenSSL';
223 unless (defined $eres) {
225 # Fake TAP::Harness in case it's not loaded
226 package TAP::Harness::fake;
227 use parent 'Test::Harness';
231 my %args = %{ shift() };
233 return bless { %args }, $class;
239 # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
240 # elements, so convert such elements to just be the filename
241 my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
244 if ($self->{switches}) {
245 push @switches, $self->{switches};
248 foreach (@{$self->{lib}}) {
251 # It seems that $switches is getting interpreted with 'eval'
252 # or something like that, and that we need to take care of
253 # backslashes or they will disappear along the way.
254 $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
255 push @switches, "-I$l";
259 $Test::Harness::switches = join(' ', @switches);
260 Test::Harness::runtests(@args);
264 $package = 'TAP::Harness::fake';
268 unless (defined $eres) {
274 my $harness = $package->new(\%tapargs);
276 $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), $tests{$_} ] }
279 # $ret->has_errors may be any number, not just 0 or 1. On VMS, numbers
280 # from 2 and on are used as is as VMS statuses, which has severity encoded
281 # in the lower 3 bits. 0 and 1, on the other hand, generate SUCCESS and
282 # FAILURE, so for currect reporting on all platforms, we make sure the only
283 # exit codes are 0 and 1. Double-bang is the trick to do so.
284 exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
286 # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
287 # which simply dies at the end if any test failed, so we don't need to bother
288 # with any exit code in that case.