run_tests.pl 4.0 KB

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