9 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
12 @EXPORT = (@Test::More::EXPORT, qw(setup indir app test run));
13 @EXPORT_OK = (@Test::More::EXPORT_OK, qw(top_dir top_file pipe with cmdstr
18 OpenSSL::Test - a private extension of Test::More
24 setup("my_test_name");
26 ok(run(app(["openssl", "version"])), "check for openssl presence");
28 indir "subdir" => sub {
29 ok(run(test(["sometest", "arg1"], stdout => "foo.txt")),
30 "run sometest with output to foo.txt");
35 This module is a private extension of L<Test::More> for testing OpenSSL.
36 In addition to the Test::More functions, it also provides functions that
37 easily find the diverse programs within a OpenSSL build tree, as well as
38 some other useful functions.
40 This module I<depends> on the environment variable C<$TOP>. Without it,
41 it refuses to work. See L</ENVIRONMENT> below.
46 use File::Spec::Functions qw/file_name_is_absolute curdir canonpath splitdir
47 catdir catfile splitpath catpath devnull abs2rel
49 use File::Path 2.00 qw/remove_tree mkpath/;
52 # The name of the test. This is set by setup() and is used in the other
53 # functions to verify that setup() has been used.
54 my $test_name = undef;
56 # Directories we want to keep track of TOP, APPS, TEST and RESULTS are the
57 # ones we're interested in, corresponding to the environment variables TOP
58 # (mandatory), BIN_D, TEST_D and RESULT_D.
61 # A bool saying if we shall stop all testing if the current recipe has failing
62 # tests or not. This is set by setup() if the environment variable STOPTEST
63 # is defined with a non-empty value.
64 my $end_with_bailout = 0;
66 # A set of hooks that is affected by with() and may be used in diverse places.
67 # All hooks are expected to be CODE references.
70 # exit_checker is used by run() directly after completion of a command.
71 # it receives the exit code from that command and is expected to return
72 # 1 (for success) or 0 (for failure). This is the value that will be
74 # NOTE: When run() gets the option 'capture => 1', this hook is ignored.
75 exit_checker => sub { return shift == 0 ? 1 : 0 },
79 # Declare some utility functions that are defined at the end
84 # Declare some private functions that are defined at the end
95 The following functions are exported by default when using C<OpenSSL::Test>.
101 =item B<setup "NAME">
103 C<setup> is used for initial setup, and it is mandatory that it's used.
104 If it's not used in a OpenSSL test recipe, the rest of the recipe will
105 most likely refuse to run.
107 C<setup> checks for environment variables (see L</ENVIRONMENT> below),
108 check that C<$TOP/Configure> exists, C<chdir> into the results directory
109 (defined by the C<$RESULT_D> environment variable if defined, otherwise
110 C<$TEST_D> if defined, otherwise C<$TOP/test>).
119 BAIL_OUT("setup() must receive a name") unless $test_name;
120 BAIL_OUT("setup() needs \$TOP to be defined") unless $ENV{TOP};
124 BAIL_OUT("setup() expects the file Configure in the \$TOP directory")
125 unless -f top_file("Configure");
127 __cwd($directories{RESULTS});
129 # Loop in case we're on a platform with more than one file generation
130 1 while unlink(__test_log());
135 =item B<indir "SUBDIR" =E<gt> sub BLOCK, OPTS>
137 C<indir> is used to run a part of the recipe in a different directory than
138 the one C<setup> moved into, usually a subdirectory, given by SUBDIR.
139 The part of the recipe that's run there is given by the codeblock BLOCK.
141 C<indir> takes some additional options OPTS that affect the subdirectory:
145 =item B<create =E<gt> 0|1>
147 When set to 1 (or any value that perl preceives as true), the subdirectory
148 will be created if it doesn't already exist. This happens before BLOCK
151 =item B<cleanup =E<gt> 0|1>
153 When set to 1 (or any value that perl preceives as true), the subdirectory
154 will be cleaned out and removed. This happens both before and after BLOCK
162 ok(run(app(["openssl", "version"]), stdout => "foo.txt"));
163 if (ok(open(RESULT, "foo.txt"), "reading foo.txt")) {
166 is($line, qr/^OpenSSL 1\./,
167 "check that we're using OpenSSL 1.x.x");
169 }, create => 1, cleanup => 1;
177 my $codeblock = shift;
180 my $reverse = __cwd($subdir,%opts);
181 BAIL_OUT("FAILURE: indir, \"$subdir\" wasn't possible to move into")
188 if ($opts{cleanup}) {
189 remove_tree($subdir, { safe => 0 });
195 =item B<app ARRAYREF, OPTS>
197 =item B<test ARRAYREF, OPTS>
199 Both of these functions take a reference to a list that is a command and
200 its arguments, and some additional options (described further on).
202 C<app> expects to find the given command (the first item in the given list
203 reference) as an executable in C<$BIN_D> (if defined, otherwise C<$TOP/apps>).
205 C<test> expects to find the given command (the first item in the given list
206 reference) as an executable in C<$TEST_D> (if defined, otherwise C<$TOP/test>).
208 Both return a CODEREF to be used by C<run>, C<pipe> or C<cmdstr>.
210 The options that both C<app> and C<test> can take are in the form of hash
215 =item B<stdin =E<gt> PATH>
217 =item B<stdout =E<gt> PATH>
219 =item B<stderr =E<gt> PATH>
221 In all three cases, the corresponding standard input, output or error is
222 redirected from (for stdin) or to (for the others) a file given by the
223 string PATH, I<or>, if the value is C<undef>, C</dev/null> or similar.
234 return sub { my $num = shift;
235 return __build_cmd($num, \&__apps_file, $cmd, %opts); }
241 return sub { my $num = shift;
242 return __build_cmd($num, \&__test_file, $cmd, %opts); }
247 =item B<run CODEREF, OPTS>
249 This CODEREF is expected to be the value return by C<app> or C<test>,
250 anything else will most likely cause an error unless you know what you're
253 C<run> executes the command returned by CODEREF and return either the
254 resulting output (if the option C<capture> is set true) or a boolean indicating
255 if the command succeeded or not.
257 The options that C<run> can take are in the form of hash values:
261 =item B<capture =E<gt> 0|1>
263 If true, the command will be executed with a perl backtick, and C<run> will
264 return the resulting output as an array of lines. If false or not given,
265 the command will be executed with C<system()>, and C<run> will return 1 if
266 the command was successful or 0 if it wasn't.
270 For further discussion on what is considered a successful command or not, see
271 the function C<with> further down.
278 my ($cmd, $display_cmd, %errlogs) = shift->(0);
284 if ( $^O eq "VMS" ) { # VMS
291 if ($opts{capture}) {
295 system("$prefix$cmd");
297 $r = $hooks{exit_checker}->($e);
300 # At this point, $? stops being interesting, and unfortunately,
301 # there are Test::More versions that get picky if we leave it
305 open ERR, ">>", __test_log();
306 { local $| = 1; print ERR "$display_cmd => $e\n"; }
307 foreach (keys %errlogs) {
309 copy($_,$errlogs{$_}) if defined($errlogs{$_});
314 if ($opts{capture}) {
322 my $tb = Test::More->builder;
323 my $failure = scalar(grep { $_ == 0; } $tb->summary);
324 if ($failure && $end_with_bailout) {
325 BAIL_OUT("Stoptest!");
329 =head2 Utility functions
331 The following functions are exported on request when using C<OpenSSL::Test>.
333 # To only get the top_file function.
334 use OpenSSL::Test qw/top_file/;
336 # To only get the top_file function in addition to the default ones.
337 use OpenSSL::Test qw/:DEFAULT top_file/;
341 # Utility functions, exported on request
345 =item B<top_dir LIST>
347 LIST is a list of directories that make up a path from the top of the OpenSSL
348 source directory (as indicated by the environment variable C<$TOP>).
349 C<top_dir> returns the resulting directory as a string, adapted to the local
357 return __top_dir(@_); # This caters for operating systems that have
358 # a very distinct syntax for directories.
363 =item B<top_file LIST, FILENAME>
365 LIST is a list of directories that make up a path from the top of the OpenSSL
366 source directory (as indicated by the environment variable C<$TOP>) and
367 FILENAME is the name of a file located in that directory path.
368 C<top_file> returns the resulting file path as a string, adapted to the local
376 return __top_file(@_);
383 LIST is a list of CODEREFs returned by C<app> or C<test>, from which C<pipe>
384 creates a new command composed of all the given commands put together in a
385 pipe. C<pipe> returns a new CODEREF in the same manner as C<app> or C<test>,
386 to be passed to C<run> for execution.
401 my ($c, $dc, @el) = $_->(++$counter);
419 =item B<with HASHREF, CODEREF>
421 C<with> will temporarly install hooks given by the HASHREF and then execute
422 the given CODEREF. Hooks are usually expected to have a coderef as value.
424 The currently available hoosk are:
428 =item B<exit_checker =E<gt> CODEREF>
430 This hook is executed after C<run> has performed its given command. The
431 CODEREF receives the exit code as only argument and is expected to return
432 1 (if the exit code indicated success) or 0 (if the exit code indicated
444 my $codeblock = shift;
446 my %saved_hooks = ();
448 foreach (keys %opts) {
449 $saved_hooks{$_} = $hooks{$_} if exists($hooks{$_});
450 $hooks{$_} = $opts{$_};
455 foreach (keys %saved_hooks) {
456 $hooks{$_} = $saved_hooks{$_};
462 =item B<cmdstr CODEREF>
464 C<cmdstr> takes a CODEREF from C<app> or C<test> and simply returns the
472 my ($cmd, $display_cmd, %errlogs) = shift->(0);
479 =item B<quotify LIST>
481 LIST is a list of strings that are going to be used as arguments for a
482 command, and makes sure to inject quotes and escapes as necessary depending
483 on the content of each string.
485 This can also be used to put quotes around the executable of a command.
486 I<This must never ever be done on VMS.>
493 # Unix setup (default if nothing else is mentioned)
495 sub { $_ = shift; /\s|[\{\}\\\$\[\]\*\?\|\&:;<>]/ ? "'$_'" : $_ };
497 if ( $^O eq "VMS") { # VMS setup
498 $arg_formatter = sub {
500 if (/\s|["[:upper:]]/) {
507 } elsif ( $^O eq "MSWin32") { # MSWin setup
508 $arg_formatter = sub {
510 if (/\s|["\|\&\*\;<>]/) {
519 return map { $arg_formatter->($_) } @_;
522 ######################################################################
523 # private functions. These are never exported.
527 OpenSSL::Test depends on some environment variables.
533 This environment variable is mandatory. C<setup> will check that it's
534 defined and that it's a directory that contains the file C<Configure>.
535 If this isn't so, C<setup> will C<BAIL_OUT>.
539 If defined, its value should be the directory where the openssl application
540 is located. Defaults to C<$TOP/apps> (adapted to the operating system).
544 If defined, its value should be the directory where the test applications
545 are located. Defaults to C<$TOP/test> (adapted to the operating system).
549 If defined, its value should be the directory where the log files are
550 located. Defaults to C<$TEST_D>.
554 If defined, it puts testing in a different mode, where a recipe with
555 failures will result in a C<BAIL_OUT> at the end of its run.
562 $directories{TOP} = $ENV{TOP},
563 $directories{APPS} = $ENV{BIN_D} || catdir($directories{TOP},"apps");
564 $directories{TEST} = $ENV{TEST_D} || catdir($directories{TOP},"test");
565 $directories{RESULTS} = $ENV{RESULT_D} || $directories{TEST};
567 $end_with_bailout = $ENV{STOPTEST} ? 1 : 0;
571 BAIL_OUT("Must run setup() first") if (! $test_name);
574 return catfile($directories{TOP},@_,$f);
578 BAIL_OUT("Must run setup() first") if (! $test_name);
580 return catdir($directories{TOP},@_);
584 BAIL_OUT("Must run setup() first") if (! $test_name);
587 return catfile($directories{TEST},@_,$f);
591 BAIL_OUT("Must run setup() first") if (! $test_name);
594 return catfile($directories{APPS},@_,$f);
598 BAIL_OUT("Must run setup() first") if (! $test_name);
601 return catfile($directories{RESULTS},@_,$f);
605 return __results_file("$test_name.log");
609 my $dir = catdir(shift);
611 my $abscurdir = rel2abs(curdir());
612 my $absdir = rel2abs($dir);
613 my $reverse = abs2rel($abscurdir, $absdir);
615 # PARANOIA: if we're not moving anywhere, we do nothing more
616 if ($abscurdir eq $absdir) {
620 # Do not support a move to a different volume for now. Maybe later.
621 BAIL_OUT("FAILURE: \"$dir\" moves to a different volume, not supported")
622 if $reverse eq $abscurdir;
624 # If someone happened to give a directory that leads back to the current,
625 # it's extremely silly to do anything more, so just simulate that we did
627 # In this case, we won't even clean it out, for safety's sake.
628 return "." if $reverse eq "";
630 $dir = canonpath($dir);
635 # Should we just bail out here as well? I'm unsure.
636 return undef unless chdir($dir);
638 if ($opts{cleanup}) {
639 remove_tree(".", { safe => 0, keep_root => 1 });
642 # For each of these directory variables, figure out where they are relative
643 # to the directory we want to move to if they aren't absolute (if they are,
644 # they don't change!)
645 my @dirtags = ("TOP", "TEST", "APPS", "RESULTS");
647 if (!file_name_is_absolute($directories{$_})) {
648 my $newpath = abs2rel(rel2abs($directories{$_}), rel2abs($dir));
649 $directories{$_} = $newpath;
654 print STDERR "DEBUG: __cwd(), directories and files:\n";
655 print STDERR " \$directories{TEST} = \"$directories{TEST}\"\n";
656 print STDERR " \$directories{RESULTS} = \"$directories{RESULTS}\"\n";
657 print STDERR " \$directories{APPS} = \"$directories{APPS}\"\n";
658 print STDERR " \$directories{TOP} = \"$directories{TOP}\"\n";
659 print STDERR " \$test_log = \"",__test_log(),"\"\n";
661 print STDERR " current directory is \"",curdir(),"\"\n";
662 print STDERR " the way back is \"$reverse\"\n";
671 my $prefix = __top_file("util", "shlib_wrap.sh")." ";
672 my $ext = $ENV{"EXE_EXT"} || "";
674 if (defined($ENV{EXE_SHELL})) {
675 $prefix = "$ENV{EXE_SHELL} ";
676 } elsif ($^O eq "VMS" ) { # VMS
677 $prefix = ($prog =~ /^[<\[]/ ? "mcr " : "mcr []");
679 } elsif ($^O eq "MSWin32") { # Windows
684 # We test both with and without extension. The reason
685 # is that we might, for example, be passed a Perl script
687 my $file = "$prog$ext";
689 return $prefix.$file;
690 } elsif ( -f $prog ) {
694 print STDERR "$prog not found\n";
699 BAIL_OUT("Must run setup() first") if (! $test_name);
702 my $path_builder = shift;
703 # Make a copy to not destroy the caller's array
704 my @cmdarray = ( @{$_[0]} ); shift;
705 my $cmd = __fixup_cmd($path_builder->(shift @cmdarray));
706 my @args = @cmdarray;
712 my $null = devnull();
715 $arg_str = " ".join(" ", quotify @args) if @args;
717 my $fileornull = sub { $_[0] ? $_[0] : $null; };
721 my $saved_stderr = undef;
722 $stdin = " < ".$fileornull->($opts{stdin}) if exists($opts{stdin});
723 $stdout= " > ".$fileornull->($opts{stdout}) if exists($opts{stdout});
724 $stderr=" 2> ".$fileornull->($opts{stderr}) if exists($opts{stderr});
726 $saved_stderr = $opts{stderr} if defined($opts{stderr});
729 __results_file($num ? "$test_name.$num.tmp_err" : "$test_name.tmp_err");
730 my $display_cmd = "$cmd$arg_str$stdin$stdout$stderr";
731 $cmd .= "$arg_str$stdin$stdout 2> $errlog";
733 return ($cmd, $display_cmd, $errlog => $saved_stderr);
738 L<Test::More>, L<Test::Harness>
742 Richard Levitte E<lt>levitte@openssl.orgE<gt> with assitance and
743 inspiration from Andy Polyakov E<lt>appro@openssl.org<gt>.