run_tests.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, "test"));
  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+)-/01-/;
  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. # prep recipes are mandatory and need to be always run first
  117. my @preps = glob(catfile($recipesdir,"00-prep_*.t"));
  118. foreach my $test (@preps) {
  119. delete $tests{$test};
  120. }
  121. sub find_matching_tests {
  122. my ($glob) = @_;
  123. if ($glob =~ m|^[\d\[\]\?\-]+$|) {
  124. return glob(catfile($recipesdir,"$glob-*.t"));
  125. }
  126. return glob(catfile($recipesdir,"*-$glob.t"));
  127. }
  128. # The following is quite a bit of hackery to adapt to both TAP::Harness
  129. # and Test::Harness, depending on what's available.
  130. # The TAP::Harness hack allows support for HARNESS_VERBOSE_FAILURE* and
  131. # HARNESS_TAP_COPY, while the Test::Harness hack can't, because the pre
  132. # TAP::Harness Test::Harness simply doesn't have support for this sort of
  133. # thing.
  134. #
  135. # We use eval to avoid undue interruption if TAP::Harness isn't present.
  136. my $package;
  137. my $eres;
  138. $eres = eval {
  139. package TAP::Parser::OpenSSL;
  140. use parent -norequire, 'TAP::Parser';
  141. require TAP::Parser;
  142. sub new {
  143. my $class = shift;
  144. my %opts = %{ shift() };
  145. my $failure_verbosity = $openssl_args{failure_verbosity};
  146. my @plans = (); # initial level, no plan yet
  147. my $output_buffer = "";
  148. # We rely heavily on perl closures to make failure verbosity work
  149. # We need to do so, because there's no way to safely pass extra
  150. # objects down all the way to the TAP::Parser::Result object
  151. my @failure_output = ();
  152. my %callbacks = ();
  153. if ($failure_verbosity > 0 || defined $openssl_args{tap_copy}) {
  154. $callbacks{ALL} = sub { # on each line of test output
  155. my $self = shift;
  156. my $fh = $openssl_args{tap_copy};
  157. print $fh $self->as_string, "\n"
  158. if defined $fh;
  159. my $failure_verbosity = $openssl_args{failure_verbosity};
  160. if ($failure_verbosity > 0) {
  161. my $is_plan = $self->is_plan;
  162. my $tests_planned = $is_plan && $self->tests_planned;
  163. my $is_test = $self->is_test;
  164. my $is_ok = $is_test && $self->is_ok;
  165. # workaround for parser not coping with sub-test indentation
  166. if ($self->is_unknown) {
  167. my $level = $#plans;
  168. my $indent = $level < 0 ? "" : " " x ($level * 4);
  169. ($is_plan, $tests_planned) = (1, $1)
  170. if ($self->as_string =~ m/^$indent 1\.\.(\d+)/);
  171. ($is_test, $is_ok) = (1, !$1)
  172. if ($self->as_string =~ m/^$indent(not )?ok /);
  173. }
  174. if ($is_plan) {
  175. push @plans, $tests_planned;
  176. $output_buffer = ""; # ignore comments etc. until plan
  177. } elsif ($is_test) { # result of a test
  178. pop @plans if @plans && --($plans[-1]) <= 0;
  179. print $output_buffer if !$is_ok;
  180. print "\n".$self->as_string
  181. if !$is_ok || $failure_verbosity == 2;
  182. print "\n# ------------------------------------------------------------------------------" if !$is_ok;
  183. $output_buffer = "";
  184. } elsif ($self->as_string ne "") {
  185. # typically is_comment or is_unknown
  186. $output_buffer .= "\n".$self->as_string;
  187. }
  188. }
  189. }
  190. }
  191. if ($failure_verbosity > 0) {
  192. $callbacks{EOF} = sub {
  193. my $self = shift;
  194. # We know we are a TAP::Parser::Aggregator object
  195. if (scalar $self->failed > 0 && @failure_output) {
  196. # We add an extra empty line, because in the case of a
  197. # progress counter, we're still at the end of that progress
  198. # line.
  199. print $_, "\n" foreach (("", @failure_output));
  200. }
  201. # Echo any trailing comments etc.
  202. print "$output_buffer";
  203. };
  204. }
  205. if (keys %callbacks) {
  206. # If %opts already has a callbacks element, the order here
  207. # ensures we do not override it
  208. %opts = ( callbacks => { %callbacks }, %opts );
  209. }
  210. return $class->SUPER::new({ %opts });
  211. }
  212. package TAP::Harness::OpenSSL;
  213. use parent -norequire, 'TAP::Harness';
  214. require TAP::Harness;
  215. package main;
  216. $tapargs{parser_class} = "TAP::Parser::OpenSSL";
  217. $package = 'TAP::Harness::OpenSSL';
  218. };
  219. unless (defined $eres) {
  220. $eres = eval {
  221. # Fake TAP::Harness in case it's not loaded
  222. package TAP::Harness::fake;
  223. use parent 'Test::Harness';
  224. sub new {
  225. my $class = shift;
  226. my %args = %{ shift() };
  227. return bless { %args }, $class;
  228. }
  229. sub runtests {
  230. my $self = shift;
  231. # Pre TAP::Harness Test::Harness doesn't support [ filename, name ]
  232. # elements, so convert such elements to just be the filename
  233. my @args = map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @_;
  234. my @switches = ();
  235. if ($self->{switches}) {
  236. push @switches, $self->{switches};
  237. }
  238. if ($self->{lib}) {
  239. foreach (@{$self->{lib}}) {
  240. my $l = $_;
  241. # It seems that $switches is getting interpreted with 'eval'
  242. # or something like that, and that we need to take care of
  243. # backslashes or they will disappear along the way.
  244. $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
  245. push @switches, "-I$l";
  246. }
  247. }
  248. $Test::Harness::switches = join(' ', @switches);
  249. Test::Harness::runtests(@args);
  250. }
  251. package main;
  252. $package = 'TAP::Harness::fake';
  253. };
  254. }
  255. unless (defined $eres) {
  256. print $@,"\n" if $@;
  257. print $!,"\n" if $!;
  258. exit 127;
  259. }
  260. my $harness = $package->new(\%tapargs);
  261. my $ret =
  262. $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
  263. @preps);
  264. die if $ret->has_errors;
  265. $ret =
  266. $harness->runtests(map { [ abs2rel($_, rel2abs(curdir())), basename($_) ] }
  267. sort { reorder($a) cmp reorder($b) } keys %tests);
  268. # $ret->has_errors may be any number, not just 0 or 1. On VMS, numbers
  269. # from 2 and on are used as is as VMS statuses, which has severity encoded
  270. # in the lower 3 bits. 0 and 1, on the other hand, generate SUCCESS and
  271. # FAILURE, so for currect reporting on all platforms, we make sure the only
  272. # exit codes are 0 and 1. Double-bang is the trick to do so.
  273. exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
  274. # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
  275. # which simply dies at the end if any test failed, so we don't need to bother
  276. # with any exit code in that case.