run_tests.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. use strict;
  9. use warnings;
  10. # Recognise VERBOSE aka V which is common on other projects.
  11. # Additionally, recognise VERBOSE_FAILURE aka VF aka REPORT_FAILURES
  12. # and recognise VERBOSE_FAILURE_PROGRESS aka VFP aka REPORT_FAILURES_PROGRESS.
  13. BEGIN {
  14. $ENV{HARNESS_VERBOSE} = "yes" if $ENV{VERBOSE} || $ENV{V};
  15. $ENV{HARNESS_VERBOSE_FAILURE} = "yes"
  16. if $ENV{VERBOSE_FAILURE} || $ENV{VF} || $ENV{REPORT_FAILURES};
  17. $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} = "yes"
  18. if ($ENV{VERBOSE_FAILURE_PROGRESS} || $ENV{VFP}
  19. || $ENV{REPORT_FAILURES_PROGRESS});
  20. }
  21. use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
  22. use File::Basename;
  23. use FindBin;
  24. use lib "$FindBin::Bin/../util/perl";
  25. use OpenSSL::Glob;
  26. my $srctop = $ENV{SRCTOP} || $ENV{TOP};
  27. my $bldtop = $ENV{BLDTOP} || $ENV{TOP};
  28. my $recipesdir = catdir($srctop, "test", "recipes");
  29. my $libdir = rel2abs(catdir($srctop, "util", "perl"));
  30. my $jobs = $ENV{HARNESS_JOBS} // 1;
  31. $ENV{OPENSSL_CONF} = rel2abs(catdir($srctop, "apps", "openssl.cnf"));
  32. $ENV{OPENSSL_CONF_INCLUDE} = rel2abs(catdir($bldtop, "providers"));
  33. $ENV{OPENSSL_MODULES} = rel2abs(catdir($bldtop, "providers"));
  34. $ENV{OPENSSL_ENGINES} = rel2abs(catdir($bldtop, "engines"));
  35. $ENV{CTLOG_FILE} = rel2abs(catdir($srctop, "test", "ct", "log_list.cnf"));
  36. my %tapargs =
  37. ( verbosity => $ENV{HARNESS_VERBOSE} ? 1 : 0,
  38. lib => [ $libdir ],
  39. switches => '-w',
  40. merge => 1,
  41. );
  42. $tapargs{jobs} = $jobs if $jobs > 1;
  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. $openssl_args{'failure_verbosity'} = $ENV{HARNESS_VERBOSE} ? 0 :
  48. $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} ? 2 :
  49. 1; # $ENV{HARNESS_VERBOSE_FAILURE}
  50. print "Warning: HARNESS_JOBS > 1 overrides HARNESS_VERBOSE\n"
  51. if $jobs > 1;
  52. print "Warning: HARNESS_VERBOSE overrides HARNESS_VERBOSE_FAILURE*\n"
  53. if ($ENV{HARNESS_VERBOSE} && ($ENV{HARNESS_VERBOSE_FAILURE}
  54. || $ENV{HARNESS_VERBOSE_FAILURE_PROGRESS}));
  55. print "Warning: HARNESS_VERBOSE_FAILURE_PROGRESS overrides HARNESS_VERBOSE_FAILURE\n"
  56. if ($ENV{HARNESS_VERBOSE_FAILURE_PROGRESS} && $ENV{HARNESS_VERBOSE_FAILURE});
  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. my @alltests = find_matching_tests("*");
  62. my %tests = ();
  63. sub reorder {
  64. my $key = pop;
  65. # for parallel test runs, do slow tests first
  66. if (defined $jobs && $jobs > 1 && $key =~ m/test_ssl_new|test_fuzz/) {
  67. $key =~ s/(\d+)-/00-/;
  68. }
  69. return $key;
  70. }
  71. my $initial_arg = 1;
  72. foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
  73. if ($arg eq 'list') {
  74. foreach (@alltests) {
  75. (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
  76. print $x,"\n";
  77. }
  78. exit 0;
  79. }
  80. if ($arg eq 'alltests') {
  81. warn "'alltests' encountered, ignoring everything before that...\n"
  82. unless $initial_arg;
  83. %tests = map { $_ => 1 } @alltests;
  84. } elsif ($arg =~ m/^(-?)(.*)/) {
  85. my $sign = $1;
  86. my $test = $2;
  87. my @matches = find_matching_tests($test);
  88. # If '-foo' is the first arg, it's short for 'alltests -foo'
  89. if ($sign eq '-' && $initial_arg) {
  90. %tests = map { $_ => 1 } @alltests;
  91. }
  92. if (scalar @matches == 0) {
  93. warn "Test $test found no match, skipping ",
  94. ($sign eq '-' ? "removal" : "addition"),
  95. "...\n";
  96. } else {
  97. foreach $test (@matches) {
  98. if ($sign eq '-') {
  99. delete $tests{$test};
  100. } else {
  101. $tests{$test} = 1;
  102. }
  103. }
  104. }
  105. } else {
  106. warn "I don't know what '$arg' is about, ignoring...\n";
  107. }
  108. $initial_arg = 0;
  109. }
  110. sub find_matching_tests {
  111. my ($glob) = @_;
  112. if ($glob =~ m|^[\d\[\]\?\-]+$|) {
  113. return glob(catfile($recipesdir,"$glob-*.t"));
  114. }
  115. return glob(catfile($recipesdir,"*-$glob.t"));
  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. my $package;
  126. my $eres;
  127. $eres = eval {
  128. package TAP::Parser::OpenSSL;
  129. use parent -norequire, 'TAP::Parser';
  130. require TAP::Parser;
  131. sub new {
  132. my $class = shift;
  133. my %opts = %{ shift() };
  134. my $failure_verbosity = $openssl_args{failure_verbosity};
  135. my @plans = (); # initial level, no plan yet
  136. my $output_buffer = "";
  137. # We rely heavily on perl closures to make failure verbosity work
  138. # We need to do so, because there's no way to safely pass extra
  139. # objects down all the way to the TAP::Parser::Result object
  140. my @failure_output = ();
  141. my %callbacks = ();
  142. if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
  143. $callbacks{ALL} = sub { # on each line of test output
  144. my $self = shift;
  145. my $fh = $openssl_args{tap_copy};
  146. print $fh $self->as_string, "\n"
  147. if defined $fh;
  148. my $failure_verbosity = $openssl_args{failure_verbosity};
  149. if ($failure_verbosity > 0) {
  150. my $is_plan = $self->is_plan;
  151. my $tests_planned = $is_plan && $self->tests_planned;
  152. my $is_test = $self->is_test;
  153. my $is_ok = $is_test && $self->is_ok;
  154. # workaround for parser not coping with sub-test indentation
  155. if ($self->is_unknown) {
  156. my $level = $#plans;
  157. my $indent = $level < 0 ? "" : " " x ($level * 4);
  158. ($is_plan, $tests_planned) = (1, $1)
  159. if ($self->as_string =~ m/^$indent 1\.\.(\d+)/);
  160. ($is_test, $is_ok) = (1, !$1)
  161. if ($self->as_string =~ m/^$indent(not )?ok /);
  162. }
  163. if ($is_plan) {
  164. push @plans, $tests_planned;
  165. $output_buffer = ""; # ignore comments etc. until plan
  166. } elsif ($is_test) { # result of a test
  167. pop @plans if @plans && --($plans[-1]) <= 0;
  168. print $output_buffer if !$is_ok;
  169. print "\n".$self->as_string
  170. if !$is_ok || $failure_verbosity == 2;
  171. print "\n# ------------------------------------------------------------------------------" if !$is_ok;
  172. $output_buffer = "";
  173. } elsif ($self->as_string ne "") {
  174. # typically is_comment or is_unknown
  175. $output_buffer .= "\n".$self->as_string;
  176. }
  177. }
  178. }
  179. }
  180. if ($failure_verbosity > 0) {
  181. $callbacks{EOF} = sub {
  182. my $self = shift;
  183. # We know we are a TAP::Parser::Aggregator object
  184. if (scalar $self->failed > 0 && @failure_output) {
  185. # We add an extra empty line, because in the case of a
  186. # progress counter, we're still at the end of that progress
  187. # line.
  188. print $_, "\n" foreach (("", @failure_output));
  189. }
  190. # Echo any trailing comments etc.
  191. print "$output_buffer";
  192. };
  193. }
  194. if (keys %callbacks) {
  195. # If %opts already has a callbacks element, the order here
  196. # ensures we do not override it
  197. %opts = ( callbacks => { %callbacks }, %opts );
  198. }
  199. return $class->SUPER::new({ %opts });
  200. }
  201. package TAP::Harness::OpenSSL;
  202. use parent -norequire, 'TAP::Harness';
  203. require TAP::Harness;
  204. package main;
  205. $tapargs{parser_class} = "TAP::Parser::OpenSSL";
  206. $package = 'TAP::Harness::OpenSSL';
  207. };
  208. unless (defined $eres) {
  209. $eres = eval {
  210. # Fake TAP::Harness in case it's not loaded
  211. package TAP::Harness::fake;
  212. use parent 'Test::Harness';
  213. sub new {
  214. my $class = shift;
  215. my %args = %{ shift() };
  216. return bless { %args }, $class;
  217. }
  218. sub runtests {
  219. my $self = shift;
  220. # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
  221. # elements, so convert such elements to just be the filename
  222. my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
  223. my @switches = ();
  224. if ($self->{switches}) {
  225. push @switches, $self->{switches};
  226. }
  227. if ($self->{lib}) {
  228. foreach (@{$self->{lib}}) {
  229. my $l = $_;
  230. # It seems that $switches is getting interpreted with 'eval'
  231. # or something like that, and that we need to take care of
  232. # backslashes or they will disappear along the way.
  233. $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
  234. push @switches, "-I$l";
  235. }
  236. }
  237. $Test::Harness::switches = join(' ', @switches);
  238. Test::Harness::runtests(@args);
  239. }
  240. package main;
  241. $package = 'TAP::Harness::fake';
  242. };
  243. }
  244. unless (defined $eres) {
  245. print $@,"\n" if $@;
  246. print $!,"\n" if $!;
  247. exit 127;
  248. }
  249. my $harness = $package->new(\%tapargs);
  250. my $ret =
  251. $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
  252. sort { reorder($a) cmp reorder($b) } keys %tests);
  253. # $ret->has_errors may be any number, not just 0 or 1. On VMS, numbers
  254. # from 2 and on are used as is as VMS statuses, which has severity encoded
  255. # in the lower 3 bits. 0 and 1, on the other hand, generate SUCCESS and
  256. # FAILURE, so for currect reporting on all platforms, we make sure the only
  257. # exit codes are 0 and 1. Double-bang is the trick to do so.
  258. exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
  259. # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
  260. # which simply dies at the end if any test failed, so we don't need to bother
  261. # with any exit code in that case.