Adjust length of some strncpy() calls
[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 }
18
19 use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
20 use File::Basename;
21 use FindBin;
22 use lib "$FindBin::Bin/../util/perl";
23 use OpenSSL::Glob;
24
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"));
29
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"));
35
36 my %tapargs =
37     ( verbosity         => $ENV{HARNESS_VERBOSE} ? 1 : 0,
38       lib               => [ $libdir ],
39       switches          => '-w',
40       merge             => 1,
41     );
42
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 = ();
47
48 $openssl_args{'failure_verbosity'} =
49     $ENV{HARNESS_VERBOSE_FAILURE} && $tapargs{verbosity} < 1 ? 1 : 0;
50
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;
55
56 my @alltests = find_matching_tests("*");
57 my %tests = ();
58
59 my $initial_arg = 1;
60 foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
61     if ($arg eq 'list') {
62         foreach (@alltests) {
63             (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
64             print $x,"\n";
65         }
66         exit 0;
67     }
68     if ($arg eq 'alltests') {
69         warn "'alltests' encountered, ignoring everything before that...\n"
70             unless $initial_arg;
71         %tests = map { $_ => basename($_) } @alltests;
72     } elsif ($arg =~ m/^(-?)(.*)/) {
73         my $sign = $1;
74         my $test = $2;
75         my @matches = find_matching_tests($test);
76
77         # If '-foo' is the first arg, it's short for 'alltests -foo'
78         if ($sign eq '-' && $initial_arg) {
79             %tests = map { $_ => basename($_) } @alltests;
80         }
81
82         if (scalar @matches == 0) {
83             warn "Test $test found no match, skipping ",
84                 ($sign eq '-' ? "removal" : "addition"),
85                 "...\n";
86         } else {
87             foreach $test (@matches) {
88                 if ($sign eq '-') {
89                     delete $tests{$test};
90                 } else {
91                     $tests{$test} = basename($test);
92                 }
93             }
94         }
95     } else {
96         warn "I don't know what '$arg' is about, ignoring...\n";
97     }
98
99     $initial_arg = 0;
100 }
101
102 sub find_matching_tests {
103     my ($glob) = @_;
104
105     if ($glob =~ m|^[\d\[\]\?\-]+$|) {
106         return glob(catfile($recipesdir,"$glob-*.t"));
107     }
108     return glob(catfile($recipesdir,"*-$glob.t"));
109 }
110
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
116 # thing.
117 #
118 # We use eval to avoid undue interruption if TAP::Harness isn't present.
119
120 my $package;
121 my $eres;
122
123 $eres = eval {
124     package TAP::Parser::OpenSSL;
125     use parent 'TAP::Parser';
126
127     sub new {
128         my $class = shift;
129         my %opts = %{ shift() };
130
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 = ();
135         my %callbacks = ();
136         if ($openssl_args{failure_verbosity}
137             || defined $openssl_args{tap_copy}) {
138             $callbacks{ALL} = sub {
139                 my $self = shift;
140                 my $fh = $openssl_args{tap_copy};
141
142                 print $fh $self->as_string, "\n"
143                     if defined $fh;
144                 push @failure_output, $self->as_string
145                     if $openssl_args{failure_verbosity} > 0;
146             };
147         }
148
149         if ($openssl_args{failure_verbosity} > 0) {
150             $callbacks{EOF} = sub {
151                 my $self = shift;
152
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
157                     # line.
158                     print $_, "\n" foreach (("", @failure_output));
159                 }
160             };
161         }
162
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 );
167         }
168
169         return $class->SUPER::new({ %opts });
170     }
171
172     package TAP::Harness::OpenSSL;
173     use parent 'TAP::Harness';
174
175     package main;
176
177     $tapargs{parser_class} = "TAP::Parser::OpenSSL";
178     $package = 'TAP::Harness::OpenSSL';
179 };
180
181 unless (defined $eres) {
182     $eres = eval {
183         # Fake TAP::Harness in case it's not loaded
184         package TAP::Harness::fake;
185         use parent 'Test::Harness';
186
187         sub new {
188             my $class = shift;
189             my %args = %{ shift() };
190
191             return bless { %args }, $class;
192         }
193
194         sub runtests {
195             my $self = shift;
196
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] : $_ } @_;
200
201             my @switches = ();
202             if ($self->{switches}) {
203                 push @switches, $self->{switches};
204             }
205             if ($self->{lib}) {
206                 foreach (@{$self->{lib}}) {
207                     my $l = $_;
208
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";
214                 }
215             }
216
217             $Test::Harness::switches = join(' ', @switches);
218             Test::Harness::runtests(@args);
219         }
220
221         package main;
222         $package = 'TAP::Harness::fake';
223     };
224 }
225
226 unless (defined $eres) {
227     print $@,"\n" if $@;
228     print $!,"\n" if $!;
229     exit 127;
230 }
231
232 my $harness = $package->new(\%tapargs);
233 my $ret =
234     $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), $tests{$_} ] }
235                        sort keys %tests);
236
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");
243
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.