12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217 |
- package OpenSSL::Test;
- use strict;
- use warnings;
- use Test::More 0.96;
- use Exporter;
- use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
- $VERSION = "0.8";
- @ISA = qw(Exporter);
- @EXPORT = (@Test::More::EXPORT, qw(setup run indir cmd app fuzz test
- perlapp perltest subtest));
- @EXPORT_OK = (@Test::More::EXPORT_OK, qw(bldtop_dir bldtop_file
- srctop_dir srctop_file
- data_file data_dir
- pipe with cmdstr quotify
- openssl_versions));
- use File::Copy;
- use File::Spec::Functions qw/file_name_is_absolute curdir canonpath splitdir
- catdir catfile splitpath catpath devnull abs2rel
- rel2abs/;
- use File::Path 2.00 qw/rmtree mkpath/;
- use File::Basename;
- use Cwd qw/getcwd abs_path/;
- my $level = 0;
- my $test_name = undef;
- my %directories = ();
- my @direnv = ();
- my $end_with_bailout = 0;
- my %hooks = (
-
-
-
-
-
- exit_checker => sub { return shift == 0 ? 1 : 0 },
- );
- my $debug = 0;
- sub setup {
- my $old_test_name = $test_name;
- $test_name = shift;
- BAIL_OUT("setup() must receive a name") unless $test_name;
- warn "setup() detected test name change. Innocuous, so we continue...\n"
- if $old_test_name && $old_test_name ne $test_name;
- return if $old_test_name;
- BAIL_OUT("setup() needs \$TOP or \$SRCTOP and \$BLDTOP to be defined")
- unless $ENV{TOP} || ($ENV{SRCTOP} && $ENV{BLDTOP});
- BAIL_OUT("setup() found both \$TOP and \$SRCTOP or \$BLDTOP...")
- if $ENV{TOP} && ($ENV{SRCTOP} || $ENV{BLDTOP});
- __env();
- BAIL_OUT("setup() expects the file Configure in the source top directory")
- unless -f srctop_file("Configure");
- __cwd($directories{RESULTS});
- }
- sub indir {
- my $subdir = shift;
- my $codeblock = shift;
- my %opts = @_;
- my $reverse = __cwd($subdir,%opts);
- BAIL_OUT("FAILURE: indir, \"$subdir\" wasn't possible to move into")
- unless $reverse;
- $codeblock->();
- __cwd($reverse);
- if ($opts{cleanup}) {
- rmtree($subdir, { safe => 0 });
- }
- }
- sub cmd {
- my $cmd = shift;
- my %opts = @_;
- return sub {
- my $num = shift;
-
- my @cmdargs = ( @$cmd );
- my @prog = __wrap_cmd(shift @cmdargs, $opts{exe_shell} // ());
- return __decorate_cmd($num, [ @prog, quotify(@cmdargs) ],
- %opts);
- }
- }
- sub app {
- my $cmd = shift;
- my %opts = @_;
- return sub {
- my @cmdargs = ( @{$cmd} );
- my @prog = __fixup_prg(__apps_file(shift @cmdargs, __exeext()));
- return cmd([ @prog, @cmdargs ],
- exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
- }
- }
- sub fuzz {
- my $cmd = shift;
- my %opts = @_;
- return sub {
- my @cmdargs = ( @{$cmd} );
- my @prog = __fixup_prg(__fuzz_file(shift @cmdargs, __exeext()));
- return cmd([ @prog, @cmdargs ],
- exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
- }
- }
- sub test {
- my $cmd = shift;
- my %opts = @_;
- return sub {
- my @cmdargs = ( @{$cmd} );
- my @prog = __fixup_prg(__test_file(shift @cmdargs, __exeext()));
- return cmd([ @prog, @cmdargs ],
- exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
- }
- }
- sub perlapp {
- my $cmd = shift;
- my %opts = @_;
- return sub {
- my @interpreter_args = defined $opts{interpreter_args} ?
- @{$opts{interpreter_args}} : ();
- my @interpreter = __fixup_prg($^X);
- my @cmdargs = ( @{$cmd} );
- my @prog = __apps_file(shift @cmdargs, undef);
- return cmd([ @interpreter, @interpreter_args,
- @prog, @cmdargs ], %opts) -> (shift);
- }
- }
- sub perltest {
- my $cmd = shift;
- my %opts = @_;
- return sub {
- my @interpreter_args = defined $opts{interpreter_args} ?
- @{$opts{interpreter_args}} : ();
- my @interpreter = __fixup_prg($^X);
- my @cmdargs = ( @{$cmd} );
- my @prog = __test_file(shift @cmdargs, undef);
- return cmd([ @interpreter, @interpreter_args,
- @prog, @cmdargs ], %opts) -> (shift);
- }
- }
- sub run {
- my ($cmd, $display_cmd) = shift->(0);
- my %opts = @_;
- return () if !$cmd;
- my $prefix = "";
- if ( $^O eq "VMS" ) {
- $prefix = "pipe ";
- }
- my @r = ();
- my $r = 0;
- my $e = 0;
- die "OpenSSL::Test::run(): statusvar value not a scalar reference"
- if $opts{statusvar} && ref($opts{statusvar}) ne "SCALAR";
-
-
-
- my $save_STDOUT;
- my $save_STDERR;
- if ($ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_VERBOSE}) {
- open $save_STDOUT, '>&', \*STDOUT or die "Can't dup STDOUT: $!";
- open $save_STDERR, '>&', \*STDERR or die "Can't dup STDERR: $!";
- open STDOUT, ">", devnull();
- open STDERR, ">", devnull();
- }
- $ENV{HARNESS_OSSL_LEVEL} = $level + 1;
-
-
-
-
- if ($opts{capture} || defined($opts{prefix})) {
- my $pipe;
- local $_;
- open($pipe, '-|', "$prefix$cmd") or die "Can't start command: $!";
- while(<$pipe>) {
- my $l = ($opts{prefix} // "") . $_;
- if ($opts{capture}) {
- push @r, $l;
- } else {
- print STDOUT $l;
- }
- }
- close $pipe;
- } else {
- $ENV{HARNESS_OSSL_PREFIX} = "# ";
- system("$prefix$cmd");
- delete $ENV{HARNESS_OSSL_PREFIX};
- }
- $e = ($? & 0x7f) ? ($? & 0x7f)|0x80 : ($? >> 8);
- $r = $hooks{exit_checker}->($e);
- if ($opts{statusvar}) {
- ${$opts{statusvar}} = $r;
- }
- if ($ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_VERBOSE}) {
- close STDOUT;
- close STDERR;
- open STDOUT, '>&', $save_STDOUT or die "Can't restore STDOUT: $!";
- open STDERR, '>&', $save_STDERR or die "Can't restore STDERR: $!";
- }
- print STDERR "$prefix$display_cmd => $e\n"
- if !$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE};
-
-
-
- $? = 0;
- if ($opts{capture}) {
- return @r;
- } else {
- return $r;
- }
- }
- END {
- my $tb = Test::More->builder;
- my $failure = scalar(grep { $_ == 0; } $tb->summary);
- if ($failure && $end_with_bailout) {
- BAIL_OUT("Stoptest!");
- }
- }
- sub bldtop_dir {
- return __bldtop_dir(@_);
-
- }
- sub bldtop_file {
- return __bldtop_file(@_);
- }
- sub srctop_dir {
- return __srctop_dir(@_);
-
- }
- sub srctop_file {
- return __srctop_file(@_);
- }
- sub data_dir {
- return __data_dir(@_);
- }
- sub data_file {
- return __data_file(@_);
- }
- sub pipe {
- my @cmds = @_;
- return
- sub {
- my @cs = ();
- my @dcs = ();
- my @els = ();
- my $counter = 0;
- foreach (@cmds) {
- my ($c, $dc, @el) = $_->(++$counter);
- return () if !$c;
- push @cs, $c;
- push @dcs, $dc;
- push @els, @el;
- }
- return (
- join(" | ", @cs),
- join(" | ", @dcs),
- @els
- );
- };
- }
- sub with {
- my $opts = shift;
- my %opts = %{$opts};
- my $codeblock = shift;
- my %saved_hooks = ();
- foreach (keys %opts) {
- $saved_hooks{$_} = $hooks{$_} if exists($hooks{$_});
- $hooks{$_} = $opts{$_};
- }
- $codeblock->();
- foreach (keys %saved_hooks) {
- $hooks{$_} = $saved_hooks{$_};
- }
- }
- sub cmdstr {
- my ($cmd, $display_cmd) = shift->(0);
- my %opts = @_;
- if ($opts{display}) {
- return $display_cmd;
- } else {
- return $cmd;
- }
- }
- sub quotify {
-
- my $arg_formatter =
- sub { $_ = shift;
- ($_ eq '' || /\s|[\{\}\\\$\[\]\*\?\|\&:;<>]/) ? "'$_'" : $_ };
- if ( $^O eq "VMS") {
- $arg_formatter = sub {
- $_ = shift;
- if ($_ eq '' || /\s|["[:upper:]]/) {
- s/"/""/g;
- '"'.$_.'"';
- } else {
- $_;
- }
- };
- } elsif ( $^O eq "MSWin32") {
- $arg_formatter = sub {
- $_ = shift;
- if ($_ eq '' || /\s|["\|\&\*\;<>]/) {
- s/(["\\])/\\$1/g;
- '"'.$_.'"';
- } else {
- $_;
- }
- };
- }
- return map { $arg_formatter->($_) } @_;
- }
- my @versions = ();
- sub openssl_versions {
- unless (@versions) {
- my %lines =
- map { s/\R$//;
- /^(.*): (0x[[:xdigit:]]{8})$/;
- die "Weird line: $_" unless defined $1;
- $1 => hex($2) }
- run(test(['versions']), capture => 1);
- @versions = ( $lines{'Build version'}, $lines{'Library version'} );
- }
- return @versions;
- }
- sub __env {
- (my $recipe_datadir = basename($0)) =~ s/\.t$/_data/i;
- $directories{SRCTOP} = abs_path($ENV{SRCTOP} || $ENV{TOP});
- $directories{BLDTOP} = abs_path($ENV{BLDTOP} || $ENV{TOP});
- $directories{BLDAPPS} = $ENV{BIN_D} || __bldtop_dir("apps");
- $directories{SRCAPPS} = __srctop_dir("apps");
- $directories{BLDFUZZ} = __bldtop_dir("fuzz");
- $directories{SRCFUZZ} = __srctop_dir("fuzz");
- $directories{BLDTEST} = $ENV{TEST_D} || __bldtop_dir("test");
- $directories{SRCTEST} = __srctop_dir("test");
- $directories{SRCDATA} = __srctop_dir("test", "recipes",
- $recipe_datadir);
- $directories{RESULTS} = $ENV{RESULT_D} || $directories{BLDTEST};
- push @direnv, "TOP" if $ENV{TOP};
- push @direnv, "SRCTOP" if $ENV{SRCTOP};
- push @direnv, "BLDTOP" if $ENV{BLDTOP};
- push @direnv, "BIN_D" if $ENV{BIN_D};
- push @direnv, "TEST_D" if $ENV{TEST_D};
- push @direnv, "RESULT_D" if $ENV{RESULT_D};
- $end_with_bailout = $ENV{STOPTEST} ? 1 : 0;
- };
- sub __srctop_file {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- my $f = pop;
- return abs2rel(catfile($directories{SRCTOP},@_,$f),getcwd);
- }
- sub __srctop_dir {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- return abs2rel(catdir($directories{SRCTOP},@_), getcwd);
- }
- sub __bldtop_file {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- my $f = pop;
- return abs2rel(catfile($directories{BLDTOP},@_,$f), getcwd);
- }
- sub __bldtop_dir {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- return abs2rel(catdir($directories{BLDTOP},@_), getcwd);
- }
- sub __exeext {
- my $ext = "";
- if ($^O eq "VMS" ) {
- $ext = ".exe";
- } elsif ($^O eq "MSWin32") {
- $ext = ".exe";
- }
- return $ENV{"EXE_EXT"} || $ext;
- }
- sub __test_file {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- my $e = pop || "";
- my $f = pop;
- my $out = catfile($directories{BLDTEST},@_,$f . $e);
- $out = catfile($directories{SRCTEST},@_,$f) unless -f $out;
- return $out;
- }
- sub __apps_file {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- my $e = pop || "";
- my $f = pop;
- my $out = catfile($directories{BLDAPPS},@_,$f . $e);
- $out = catfile($directories{SRCAPPS},@_,$f) unless -f $out;
- return $out;
- }
- sub __fuzz_file {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- my $e = pop || "";
- my $f = pop;
- my $out = catfile($directories{BLDFUZZ},@_,$f . $e);
- $out = catfile($directories{SRCFUZZ},@_,$f) unless -f $out;
- return $out;
- }
- sub __data_file {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- my $f = pop;
- return catfile($directories{SRCDATA},@_,$f);
- }
- sub __data_dir {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- return catdir($directories{SRCDATA},@_);
- }
- sub __results_file {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- my $f = pop;
- return catfile($directories{RESULTS},@_,$f);
- }
- sub __cwd {
- my $dir = catdir(shift);
- my %opts = @_;
- my $abscurdir = rel2abs(curdir());
- my $absdir = rel2abs($dir);
- my $reverse = abs2rel($abscurdir, $absdir);
-
- if ($abscurdir eq $absdir) {
- return $reverse;
- }
-
- BAIL_OUT("FAILURE: \"$dir\" moves to a different volume, not supported")
- if $reverse eq $abscurdir;
-
-
-
-
- return "." if $reverse eq "";
- $dir = canonpath($dir);
- if ($opts{create}) {
- mkpath($dir);
- }
-
-
- my %tmp_directories = ();
- my %tmp_ENV = ();
-
-
-
- my @dirtags = sort keys %directories;
- foreach (@dirtags) {
- if (!file_name_is_absolute($directories{$_})) {
- my $newpath = abs2rel(rel2abs($directories{$_}), rel2abs($dir));
- $tmp_directories{$_} = $newpath;
- }
- }
-
-
-
- foreach (@direnv) {
- if (!file_name_is_absolute($ENV{$_})) {
- my $newpath = abs2rel(rel2abs($ENV{$_}), rel2abs($dir));
- $tmp_ENV{$_} = $newpath;
- }
- }
-
- return undef unless chdir($dir);
- if ($opts{cleanup}) {
- rmtree(".", { safe => 0, keep_root => 1 });
- }
-
-
-
- foreach (keys %tmp_directories) {
- $directories{$_} = $tmp_directories{$_};
- }
- foreach (keys %tmp_ENV) {
- $ENV{$_} = $tmp_ENV{$_};
- }
- if ($debug) {
- print STDERR "DEBUG: __cwd(), directories and files:\n";
- print STDERR " \$directories{BLDTEST} = \"$directories{BLDTEST}\"\n";
- print STDERR " \$directories{SRCTEST} = \"$directories{SRCTEST}\"\n";
- print STDERR " \$directories{SRCDATA} = \"$directories{SRCDATA}\"\n";
- print STDERR " \$directories{RESULTS} = \"$directories{RESULTS}\"\n";
- print STDERR " \$directories{BLDAPPS} = \"$directories{BLDAPPS}\"\n";
- print STDERR " \$directories{SRCAPPS} = \"$directories{SRCAPPS}\"\n";
- print STDERR " \$directories{SRCTOP} = \"$directories{SRCTOP}\"\n";
- print STDERR " \$directories{BLDTOP} = \"$directories{BLDTOP}\"\n";
- print STDERR "\n";
- print STDERR " current directory is \"",curdir(),"\"\n";
- print STDERR " the way back is \"$reverse\"\n";
- }
- return $reverse;
- }
- sub __wrap_cmd {
- my $cmd = shift;
- my $exe_shell = shift;
- my @prefix = ( __bldtop_file("util", "shlib_wrap.sh") );
- if(defined($exe_shell)) {
- @prefix = ( $exe_shell );
- } elsif ($^O eq "VMS" || $^O eq "MSWin32") {
-
- @prefix = ();
- }
- return (@prefix, $cmd);
- }
- sub __fixup_prg {
- my $prog = shift;
- my $prefix = "";
- if ($^O eq "VMS" ) {
- $prefix = ($prog =~ /^(?:[\$a-z0-9_]+:)?[<\[]/i ? "mcr " : "mcr []");
- }
- if (defined($prog)) {
-
-
-
-
- ($prog) = quotify($prog) unless $^O eq "VMS";
- return $prefix.$prog;
- }
- print STDERR "$prog not found\n";
- return undef;
- }
- sub __decorate_cmd {
- BAIL_OUT("Must run setup() first") if (! $test_name);
- my $num = shift;
- my $cmd = shift;
- my %opts = @_;
- my $cmdstr = join(" ", @$cmd);
- my $null = devnull();
- my $fileornull = sub { $_[0] ? $_[0] : $null; };
- my $stdin = "";
- my $stdout = "";
- my $stderr = "";
- my $saved_stderr = undef;
- $stdin = " < ".$fileornull->($opts{stdin}) if exists($opts{stdin});
- $stdout= " > ".$fileornull->($opts{stdout}) if exists($opts{stdout});
- $stderr=" 2> ".$fileornull->($opts{stderr}) if exists($opts{stderr});
- my $display_cmd = "$cmdstr$stdin$stdout$stderr";
- $stderr=" 2> ".$null
- unless $stderr || !$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE};
- $cmdstr .= "$stdin$stdout$stderr";
- if ($debug) {
- print STDERR "DEBUG[__decorate_cmd]: \$cmdstr = \"$cmdstr\"\n";
- print STDERR "DEBUG[__decorate_cmd]: \$display_cmd = \"$display_cmd\"\n";
- }
- return ($cmdstr, $display_cmd);
- }
- no warnings 'redefine';
- sub subtest {
- $level++;
- Test::More::subtest @_;
- $level--;
- };
- 1;
|