Remove misleading diagnostics on pinned sender cert in OSSL_CMP_validate_msg()
[openssl.git] / test / run_tests.pl
1 #! /usr/bin/env perl
2 # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
8
9 use strict;
10 use warnings;
11
12 # Recognise VERBOSE and V which is common on other projects.
13 # Additionally, also recognise VERBOSE_FAILURE and VF.
14 BEGIN {
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};
21 }
22
23 use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
24 use File::Basename;
25 use FindBin;
26 use lib "$FindBin::Bin/../util/perl";
27 use OpenSSL::Glob;
28
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"));
33
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"));
39
40 my %tapargs =
41     ( verbosity         => $ENV{HARNESS_VERBOSE} ? 1 : 0,
42       lib               => [ $libdir ],
43       switches          => '-w',
44       merge             => 1,
45     );
46
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 = ();
51
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;
56
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;
61
62 my @alltests = find_matching_tests("*");
63 my %tests = ();
64
65 my $initial_arg = 1;
66 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
67     if ($arg eq 'list') {
68         foreach (@alltests) {
69             (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
70             print $x,"\n";
71         }
72         exit 0;
73     }
74     if ($arg eq 'alltests') {
75         warn "'alltests' encountered, ignoring everything before that...\n"
76             unless $initial_arg;
77         %tests = map { $_ => basename($_) } @alltests;
78     } elsif ($arg =~ m/^(-?)(.*)/) {
79         my $sign = $1;
80         my $test = $2;
81         my @matches = find_matching_tests($test);
82
83         # If '-foo' is the first arg, it's short for 'alltests -foo'
84         if ($sign eq '-' && $initial_arg) {
85             %tests = map { $_ => basename($_) } @alltests;
86         }
87
88         if (scalar @matches == 0) {
89             warn "Test $test found no match, skipping ",
90                 ($sign eq '-' ? "removal" : "addition"),
91                 "...\n";
92         } else {
93             foreach $test (@matches) {
94                 if ($sign eq '-') {
95                     delete $tests{$test};
96                 } else {
97                     $tests{$test} = basename($test);
98                 }
99             }
100         }
101     } else {
102         warn "I don't know what '$arg' is about, ignoring...\n";
103     }
104
105     $initial_arg = 0;
106 }
107
108 sub find_matching_tests {
109     my ($glob) = @_;
110
111     if ($glob =~ m|^[\d\[\]\?\-]+$|) {
112         return glob(catfile($recipesdir,"$glob-*.t"));
113     }
114     return glob(catfile($recipesdir,"*-$glob.t"));
115 }
116
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
122 # thing.
123 #
124 # We use eval to avoid undue interruption if TAP::Harness isn't present.
125
126 my $package;
127 my $eres;
128
129 $eres = eval {
130     package TAP::Parser::OpenSSL;
131     use parent 'TAP::Parser';
132
133     sub new {
134         my $class = shift;
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 = "";
140
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 = ();
145         my %callbacks = ();
146         if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
147             $callbacks{ALL} = sub { # on each line of test output
148                 my $self = shift;
149                 my $fh = $openssl_args{tap_copy};
150                 print $fh $self->as_string, "\n"
151                     if defined $fh;
152
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 /);
167                     }
168                     if ($is_plan) {
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;
176                         $output_buffer = "";
177                     } else { # typically comment or unknown
178                         $output_buffer .= $self->as_string."\n";
179                     }
180                 }
181             }
182         }
183
184         if ($failure_verbosity > 0) {
185             $callbacks{EOF} = sub {
186                 my $self = shift;
187
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
192                     # line.
193                     print $_, "\n" foreach (("", @failure_output));
194                 }
195                 # Echo any trailing comments etc.
196                 print "$output_buffer" if $failure_verbosity != 3;
197             };
198         }
199
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 );
204         }
205
206         return $class->SUPER::new({ %opts });
207     }
208
209     package TAP::Harness::OpenSSL;
210     use parent 'TAP::Harness';
211
212     package main;
213
214     $tapargs{parser_class} = "TAP::Parser::OpenSSL";
215     $package = 'TAP::Harness::OpenSSL';
216 };
217
218 unless (defined $eres) {
219     $eres = eval {
220         # Fake TAP::Harness in case it's not loaded
221         package TAP::Harness::fake;
222         use parent 'Test::Harness';
223
224         sub new {
225             my $class = shift;
226             my %args = %{ shift() };
227
228             return bless { %args }, $class;
229         }
230
231         sub runtests {
232             my $self = shift;
233
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] : $_ } @_;
237
238             my @switches = ();
239             if ($self->{switches}) {
240                 push @switches, $self->{switches};
241             }
242             if ($self->{lib}) {
243                 foreach (@{$self->{lib}}) {
244                     my $l = $_;
245
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";
251                 }
252             }
253
254             $Test::Harness::switches = join(' ', @switches);
255             Test::Harness::runtests(@args);
256         }
257
258         package main;
259         $package = 'TAP::Harness::fake';
260     };
261 }
262
263 unless (defined $eres) {
264     print $@,"\n" if $@;
265     print $!,"\n" if $!;
266     exit 127;
267 }
268
269 my $harness = $package->new(\%tapargs);
270 my $ret =
271     $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), $tests{$_} ] }
272                        sort keys %tests);
273
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");
280
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.