run_tests.pl 11 KB

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