Rich Salz | e0a6519 | 2016-04-19 22:10:43 -0400 | [diff] [blame] | 1 | #! /usr/bin/env perl |
Matt Caswell | 6ec5fce | 2018-05-01 13:34:30 +0100 | [diff] [blame] | 2 | # Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. |
Rich Salz | e0a6519 | 2016-04-19 22:10:43 -0400 | [diff] [blame] | 3 | # |
| 4 | # Licensed under the OpenSSL license (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 |
Richard Levitte | aec27d4 | 2015-04-17 19:44:48 +0200 | [diff] [blame] | 8 | |
| 9 | use strict; |
| 10 | use warnings; |
| 11 | |
Dr. Stephen Henson | be6bdab | 2016-05-08 13:09:56 +0100 | [diff] [blame] | 12 | # Recognise VERBOSE and V which is common on other projects. |
| 13 | BEGIN { |
| 14 | $ENV{HARNESS_VERBOSE} = "yes" if $ENV{VERBOSE} || $ENV{V}; |
| 15 | } |
| 16 | |
Richard Levitte | aec27d4 | 2015-04-17 19:44:48 +0200 | [diff] [blame] | 17 | use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/; |
| 18 | use File::Basename; |
Richard Levitte | 8d2214c | 2017-08-01 22:43:56 +0200 | [diff] [blame] | 19 | use FindBin; |
| 20 | use lib "$FindBin::Bin/../util/perl"; |
| 21 | use OpenSSL::Glob; |
Richard Levitte | 76e0d0b | 2017-05-10 12:58:36 +0200 | [diff] [blame] | 22 | |
Andy Polyakov | 26810b5 | 2017-09-01 23:13:28 +0200 | [diff] [blame] | 23 | my $TAP_Harness = eval { require TAP::Harness } ? "TAP::Harness" |
| 24 | : "OpenSSL::TAP::Harness"; |
Richard Levitte | aec27d4 | 2015-04-17 19:44:48 +0200 | [diff] [blame] | 25 | |
Richard Levitte | 42e0ccd | 2016-01-30 01:05:33 +0100 | [diff] [blame] | 26 | my $srctop = $ENV{SRCTOP} || $ENV{TOP}; |
| 27 | my $bldtop = $ENV{BLDTOP} || $ENV{TOP}; |
| 28 | my $recipesdir = catdir($srctop, "test", "recipes"); |
Richard Levitte | cb6afcd | 2017-08-01 22:10:39 +0200 | [diff] [blame] | 29 | my $libdir = rel2abs(catdir($srctop, "util", "perl")); |
Richard Levitte | aec27d4 | 2015-04-17 19:44:48 +0200 | [diff] [blame] | 30 | |
Kurt Roeckx | d1ae34e | 2018-04-28 23:26:22 +0200 | [diff] [blame] | 31 | $ENV{OPENSSL_CONF} = catdir($srctop, "apps", "openssl.cnf"); |
| 32 | |
Richard Levitte | 76e0d0b | 2017-05-10 12:58:36 +0200 | [diff] [blame] | 33 | my %tapargs = |
| 34 | ( verbosity => $ENV{VERBOSE} || $ENV{V} || $ENV{HARNESS_VERBOSE} ? 1 : 0, |
Richard Levitte | cb6afcd | 2017-08-01 22:10:39 +0200 | [diff] [blame] | 35 | lib => [ $libdir ], |
Richard Levitte | 76e0d0b | 2017-05-10 12:58:36 +0200 | [diff] [blame] | 36 | switches => '-w', |
| 37 | merge => 1 |
| 38 | ); |
Richard Levitte | aec27d4 | 2015-04-17 19:44:48 +0200 | [diff] [blame] | 39 | |
Richard Levitte | 90aeaf6 | 2016-09-01 13:34:32 +0200 | [diff] [blame] | 40 | my @alltests = find_matching_tests("*"); |
| 41 | my %tests = (); |
| 42 | |
| 43 | my $initial_arg = 1; |
| 44 | foreach my $arg (@ARGV ? @ARGV : ('alltests')) { |
| 45 | if ($arg eq 'list') { |
| 46 | foreach (@alltests) { |
| 47 | (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|; |
| 48 | print $x,"\n"; |
| 49 | } |
| 50 | exit 0; |
Richard Levitte | aec27d4 | 2015-04-17 19:44:48 +0200 | [diff] [blame] | 51 | } |
Richard Levitte | 90aeaf6 | 2016-09-01 13:34:32 +0200 | [diff] [blame] | 52 | if ($arg eq 'alltests') { |
| 53 | warn "'alltests' encountered, ignoring everything before that...\n" |
| 54 | unless $initial_arg; |
| 55 | %tests = map { $_ => 1 } @alltests; |
| 56 | } elsif ($arg =~ m/^(-?)(.*)/) { |
| 57 | my $sign = $1; |
| 58 | my $test = $2; |
| 59 | my @matches = find_matching_tests($test); |
| 60 | |
| 61 | # If '-foo' is the first arg, it's short for 'alltests -foo' |
| 62 | if ($sign eq '-' && $initial_arg) { |
| 63 | %tests = map { $_ => 1 } @alltests; |
| 64 | } |
| 65 | |
| 66 | if (scalar @matches == 0) { |
| 67 | warn "Test $test found no match, skipping ", |
| 68 | ($sign eq '-' ? "removal" : "addition"), |
| 69 | "...\n"; |
| 70 | } else { |
| 71 | foreach $test (@matches) { |
| 72 | if ($sign eq '-') { |
| 73 | delete $tests{$test}; |
| 74 | } else { |
| 75 | $tests{$test} = 1; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | warn "I don't know what '$arg' is about, ignoring...\n"; |
| 81 | } |
| 82 | |
| 83 | $initial_arg = 0; |
Richard Levitte | aec27d4 | 2015-04-17 19:44:48 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Richard Levitte | 76e0d0b | 2017-05-10 12:58:36 +0200 | [diff] [blame] | 86 | my $harness = $TAP_Harness->new(\%tapargs); |
Andy Polyakov | c80bbcb | 2017-05-20 12:35:58 +0200 | [diff] [blame] | 87 | my $ret = $harness->runtests(map { abs2rel($_, rel2abs(curdir())); } |
| 88 | sort keys %tests); |
| 89 | |
Richard Levitte | 4549ed1 | 2017-07-07 11:11:33 +0200 | [diff] [blame] | 90 | # $ret->has_errors may be any number, not just 0 or 1. On VMS, numbers |
| 91 | # from 2 and on are used as is as VMS statuses, which has severity encoded |
| 92 | # in the lower 3 bits. 0 and 1, on the other hand, generate SUCCESS and |
| 93 | # FAILURE, so for currect reporting on all platforms, we make sure the only |
| 94 | # exit codes are 0 and 1. Double-bang is the trick to do so. |
| 95 | exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator"); |
| 96 | |
| 97 | # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness, |
| 98 | # which simply dies at the end if any test failed, so we don't need to bother |
| 99 | # with any exit code in that case. |
Richard Levitte | aec27d4 | 2015-04-17 19:44:48 +0200 | [diff] [blame] | 100 | |
Richard Levitte | 90aeaf6 | 2016-09-01 13:34:32 +0200 | [diff] [blame] | 101 | sub find_matching_tests { |
| 102 | my ($glob) = @_; |
| 103 | |
Richard Levitte | e5fd8ca | 2017-03-09 19:08:03 +0100 | [diff] [blame] | 104 | if ($glob =~ m|^[\d\[\]\?\-]+$|) { |
| 105 | return glob(catfile($recipesdir,"$glob-*.t")); |
| 106 | } |
Richard Levitte | 90aeaf6 | 2016-09-01 13:34:32 +0200 | [diff] [blame] | 107 | return glob(catfile($recipesdir,"*-$glob.t")); |
Richard Levitte | 1780e6d | 2015-09-15 14:07:10 +0200 | [diff] [blame] | 108 | } |
Richard Levitte | 76e0d0b | 2017-05-10 12:58:36 +0200 | [diff] [blame] | 109 | |
| 110 | |
| 111 | # Fake TAP::Harness in case it's not loaded |
| 112 | use Test::Harness; |
| 113 | package OpenSSL::TAP::Harness; |
| 114 | |
| 115 | sub new { |
| 116 | my $class = shift; |
| 117 | my %args = %{ shift() }; |
| 118 | |
| 119 | return bless { %args }, $class; |
| 120 | } |
| 121 | |
| 122 | sub runtests { |
| 123 | my $self = shift; |
| 124 | |
| 125 | my @switches = (); |
| 126 | if ($self->{switches}) { |
| 127 | push @switches, $self->{switches}; |
| 128 | } |
| 129 | if ($self->{lib}) { |
| 130 | foreach (@{$self->{lib}}) { |
| 131 | my $l = $_; |
| 132 | |
| 133 | # It seems that $switches is getting interpreted with 'eval' or |
| 134 | # something like that, and that we need to take care of backslashes |
| 135 | # or they will disappear along the way. |
| 136 | $l =~ s|\\|\\\\|g if $^O eq "MSWin32"; |
| 137 | push @switches, "-I$l"; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $Test::Harness::switches = join(' ', @switches); |
| 142 | Test::Harness::runtests(@_); |
| 143 | } |