Add demo for EC keygen
[openssl.git] / test / run_tests.pl
1 #! /usr/bin/env perl
2 # Copyright 2015-2021 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 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.
15 BEGIN {
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});
22 }
23
24 use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
25 use File::Basename;
26 use FindBin;
27 use lib "$FindBin::Bin/../util/perl";
28 use OpenSSL::Glob;
29
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} // 1;
35
36 $ENV{OPENSSL_CONF} = rel2abs(catdir($srctop, "apps", "openssl.cnf"));
37 $ENV{OPENSSL_CONF_INCLUDE} = rel2abs(catdir($bldtop, "test"));
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"));
41
42 my %tapargs =
43     ( verbosity         => $ENV{HARNESS_VERBOSE} ? 1 : 0,
44       lib               => [ $libdir ],
45       switches          => '-w',
46       merge             => 1,
47       timer             => $ENV{HARNESS_TIMER} ? 1 : 0,
48     );
49
50 if ($jobs > 1) {
51     if ($ENV{HARNESS_VERBOSE}) {
52         print "Warning: HARNESS_JOBS > 1 ignored with HARNESS_VERBOSE\n";
53     } else {
54         $tapargs{jobs} = $jobs;
55         print "Using HARNESS_JOBS=$jobs\n";
56     }
57 }
58
59 # Additional OpenSSL special TAP arguments.  Because we can't pass them via
60 # TAP::Harness->new(), they will be accessed directly, see the
61 # TAP::Parser::OpenSSL implementation further down
62 my %openssl_args = ();
63
64 $openssl_args{'failure_verbosity'} = $ENV{HARNESS_VERBOSE} ? 0 :
65     $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} ? 2 :
66     1; # $ENV{HARNESS_VERBOSE_FAILURE}
67 print "Warning: HARNESS_VERBOSE overrides HARNESS_VERBOSE_FAILURE*\n"
68     if ($ENV{HARNESS_VERBOSE} && ($ENV{HARNESS_VERBOSE_FAILURE}
69                                   || $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS}));
70 print "Warning: HARNESS_VERBOSE_FAILURE_PROGRESS overrides HARNESS_VERBOSE_FAILURE\n"
71     if ($ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} && $ENV{HARNESS_VERBOSE_FAILURE});
72
73 my $outfilename = $ENV{HARNESS_TAP_COPY};
74 open $openssl_args{'tap_copy'}, ">$outfilename"
75     or die "Trying to create $outfilename: $!\n"
76     if defined $outfilename;
77
78 my @alltests = find_matching_tests("*");
79 my %tests = ();
80
81 sub reorder {
82     my $key = pop;
83
84     # for parallel test runs, do slow tests first
85     if ($jobs > 1 && $key =~ m/test_ssl_new|test_fuzz/) {
86         $key =~ s/(\d+)-/00-/;
87     }
88     return $key;
89 }
90
91 my $initial_arg = 1;
92 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
93     if ($arg eq 'list') {
94         foreach (@alltests) {
95             (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
96             print $x,"\n";
97         }
98         exit 0;
99     }
100     if ($arg eq 'alltests') {
101         warn "'alltests' encountered, ignoring everything before that...\n"
102             unless $initial_arg;
103         %tests = map { $_ => 1 } @alltests;
104     } elsif ($arg =~ m/^(-?)(.*)/) {
105         my $sign = $1;
106         my $test = $2;
107         my @matches = find_matching_tests($test);
108
109         # If '-foo' is the first arg, it's short for 'alltests -foo'
110         if ($sign eq '-' && $initial_arg) {
111             %tests = map { $_ => 1 } @alltests;
112         }
113
114         if (scalar @matches == 0) {
115             warn "Test $test found no match, skipping ",
116                 ($sign eq '-' ? "removal" : "addition"),
117                 "...\n";
118         } else {
119             foreach $test (@matches) {
120                 if ($sign eq '-') {
121                     delete $tests{$test};
122                 } else {
123                     $tests{$test} = 1;
124                 }
125             }
126         }
127     } else {
128         warn "I don't know what '$arg' is about, ignoring...\n";
129     }
130
131     $initial_arg = 0;
132 }
133
134 sub find_matching_tests {
135     my ($glob) = @_;
136
137     # prep recipes are mandatory
138     my @recipes = glob(catfile($recipesdir,"00-prep_*.t"));
139
140     if ($glob =~ m|^[\d\[\]\?\-]+$|) {
141         push @recipes, glob(catfile($recipesdir,"$glob-*.t"));
142     } else {
143         push @recipes, glob(catfile($recipesdir,"*-$glob.t"));
144     }
145     return @recipes;
146 }
147
148 # The following is quite a bit of hackery to adapt to both TAP::Harness
149 # and Test::Harness, depending on what's available.
150 # The TAP::Harness hack allows support for HARNESS_VERBOSE_FAILURE* and
151 # HARNESS_TAP_COPY, while the Test::Harness hack can't, because the pre
152 # TAP::Harness Test::Harness simply doesn't have support for this sort of
153 # thing.
154 #
155 # We use eval to avoid undue interruption if TAP::Harness isn't present.
156
157 my $package;
158 my $eres;
159
160 $eres = eval {
161     package TAP::Parser::OpenSSL;
162     use parent -norequire, 'TAP::Parser';
163     require TAP::Parser;
164
165     sub new {
166         my $class = shift;
167         my %opts = %{ shift() };
168         my $failure_verbosity = $openssl_args{failure_verbosity};
169         my @plans = (); # initial level, no plan yet
170         my $output_buffer = "";
171
172         # We rely heavily on perl closures to make failure verbosity work
173         # We need to do so, because there's no way to safely pass extra
174         # objects down all the way to the TAP::Parser::Result object
175         my @failure_output = ();
176         my %callbacks = ();
177         if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
178             $callbacks{ALL} = sub { # on each line of test output
179                 my $self = shift;
180                 my $fh = $openssl_args{tap_copy};
181                 print $fh $self->as_string, "\n"
182                     if defined $fh;
183
184                 my $failure_verbosity = $openssl_args{failure_verbosity};
185                 if ($failure_verbosity > 0) {
186                     my $is_plan = $self->is_plan;
187                     my $tests_planned = $is_plan && $self->tests_planned;
188                     my $is_test = $self->is_test;
189                     my $is_ok = $is_test && $self->is_ok;
190
191                     # workaround for parser not coping with sub-test indentation
192                     if ($self->is_unknown) {
193                         my $level = $#plans;
194                         my $indent = $level < 0 ? "" : " " x ($level * 4);
195
196                         ($is_plan, $tests_planned) = (1, $1)
197                             if ($self->as_string =~ m/^$indent    1\.\.(\d+)/);
198                         ($is_test, $is_ok) = (1, !$1)
199                             if ($self->as_string =~ m/^$indent(not )?ok /);
200                     }
201
202                     if ($is_plan) {
203                         push @plans, $tests_planned;
204                         $output_buffer = ""; # ignore comments etc. until plan
205                     } elsif ($is_test) { # result of a test
206                         pop @plans if @plans && --($plans[-1]) <= 0;
207                         print $output_buffer if !$is_ok;
208                         print "\n".$self->as_string
209                             if !$is_ok || $failure_verbosity == 2;
210                         print "\n# ------------------------------------------------------------------------------" if !$is_ok;
211                         $output_buffer = "";
212                     } elsif ($self->as_string ne "") {
213                         # typically is_comment or is_unknown
214                         $output_buffer .= "\n".$self->as_string;
215                     }
216                 }
217             }
218         }
219
220         if ($failure_verbosity > 0) {
221             $callbacks{EOF} = sub {
222                 my $self = shift;
223
224                 # We know we are a TAP::Parser::Aggregator object
225                 if (scalar $self->failed > 0 && @failure_output) {
226                     # We add an extra empty line, because in the case of a
227                     # progress counter, we're still at the end of that progress
228                     # line.
229                     print $_, "\n" foreach (("", @failure_output));
230                 }
231                 # Echo any trailing comments etc.
232                 print "$output_buffer";
233             };
234         }
235
236         if (keys %callbacks) {
237             # If %opts already has a callbacks element, the order here
238             # ensures we do not override it
239             %opts = ( callbacks => { %callbacks }, %opts );
240         }
241
242         return $class->SUPER::new({ %opts });
243     }
244
245     package TAP::Harness::OpenSSL;
246     use parent -norequire, 'TAP::Harness';
247     require TAP::Harness;
248
249     package main;
250
251     $tapargs{parser_class} = "TAP::Parser::OpenSSL";
252     $package = 'TAP::Harness::OpenSSL';
253 };
254
255 unless (defined $eres) {
256     $eres = eval {
257         # Fake TAP::Harness in case it's not loaded
258         package TAP::Harness::fake;
259         use parent 'Test::Harness';
260
261         sub new {
262             my $class = shift;
263             my %args = %{ shift() };
264
265             return bless { %args }, $class;
266         }
267
268         sub runtests {
269             my $self = shift;
270
271             # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
272             # elements, so convert such elements to just be the filename
273             my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
274
275             my @switches = ();
276             if ($self->{switches}) {
277                 push @switches, $self->{switches};
278             }
279             if ($self->{lib}) {
280                 foreach (@{$self->{lib}}) {
281                     my $l = $_;
282
283                     # It seems that $switches is getting interpreted with 'eval'
284                     # or something like that, and that we need to take care of
285                     # backslashes or they will disappear along the way.
286                     $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
287                     push @switches, "-I$l";
288                 }
289             }
290
291             $Test::Harness::switches = join(' ', @switches);
292             Test::Harness::runtests(@args);
293         }
294
295         package main;
296         $package = 'TAP::Harness::fake';
297     };
298 }
299
300 unless (defined $eres) {
301     print $@,"\n" if $@;
302     print $!,"\n" if $!;
303     exit 127;
304 }
305
306 my $harness = $package->new(\%tapargs);
307 my $ret =
308     $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
309                        sort { reorder($a) cmp reorder($b) } keys %tests);
310
311 # $ret->has_errors may be any number, not just 0 or 1.  On VMS, numbers
312 # from 2 and on are used as is as VMS statuses, which has severity encoded
313 # in the lower 3 bits.  0 and 1, on the other hand, generate SUCCESS and
314 # FAILURE, so for currect reporting on all platforms, we make sure the only
315 # exit codes are 0 and 1.  Double-bang is the trick to do so.
316 exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
317
318 # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
319 # which simply dies at the end if any test failed, so we don't need to bother
320 # with any exit code in that case.