1 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 # Licensed under the OpenSSL license (the "License"). You may not use
4 # this file except in compliance with the License. You can obtain a copy
5 # in the file LICENSE in the source distribution or at
6 # https://www.openssl.org/source/license.html
16 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
19 @EXPORT = (@Test::More::EXPORT, qw(setup run indir cmd app fuzz test
21 @EXPORT_OK = (@Test::More::EXPORT_OK, qw(bldtop_dir bldtop_file
22 srctop_dir srctop_file
24 pipe with cmdstr quotify));
28 OpenSSL::Test - a private extension of Test::More
34 setup("my_test_name");
36 ok(run(app(["openssl", "version"])), "check for openssl presence");
38 indir "subdir" => sub {
39 ok(run(test(["sometest", "arg1"], stdout => "foo.txt")),
40 "run sometest with output to foo.txt");
45 This module is a private extension of L<Test::More> for testing OpenSSL.
46 In addition to the Test::More functions, it also provides functions that
47 easily find the diverse programs within a OpenSSL build tree, as well as
48 some other useful functions.
50 This module I<depends> on the environment variables C<$TOP> or C<$SRCTOP>
51 and C<$BLDTOP>. Without one of the combinations it refuses to work.
52 See L</ENVIRONMENT> below.
54 With each test recipe, a parallel data directory with (almost) the same name
55 as the recipe is possible in the source directory tree. For example, for a
56 recipe C<$SRCTOP/test/recipes/99-foo.t>, there could be a directory
57 C<$SRCTOP/test/recipes/99-foo_data/>.
62 use File::Spec::Functions qw/file_name_is_absolute curdir canonpath splitdir
63 catdir catfile splitpath catpath devnull abs2rel
65 use File::Path 2.00 qw/rmtree mkpath/;
69 # The name of the test. This is set by setup() and is used in the other
70 # functions to verify that setup() has been used.
71 my $test_name = undef;
73 # Directories we want to keep track of TOP, APPS, TEST and RESULTS are the
74 # ones we're interested in, corresponding to the environment variables TOP
75 # (mandatory), BIN_D, TEST_D, UTIL_D and RESULT_D.
78 # The environment variables that gave us the contents in %directories. These
79 # get modified whenever we change directories, so that subprocesses can use
80 # the values of those environment variables as well
83 # A bool saying if we shall stop all testing if the current recipe has failing
84 # tests or not. This is set by setup() if the environment variable STOPTEST
85 # is defined with a non-empty value.
86 my $end_with_bailout = 0;
88 # A set of hooks that is affected by with() and may be used in diverse places.
89 # All hooks are expected to be CODE references.
92 # exit_checker is used by run() directly after completion of a command.
93 # it receives the exit code from that command and is expected to return
94 # 1 (for success) or 0 (for failure). This is the status value that run()
95 # will give back (through the |statusvar| referens and as returned value
96 # when capture => 1 doesn't apply).
97 exit_checker => sub { return shift == 0 ? 1 : 0 },
101 # Debug flag, to be set manually when needed
104 =head2 Main functions
106 The following functions are exported by default when using C<OpenSSL::Test>.
112 =item B<setup "NAME">
114 C<setup> is used for initial setup, and it is mandatory that it's used.
115 If it's not used in a OpenSSL test recipe, the rest of the recipe will
116 most likely refuse to run.
118 C<setup> checks for environment variables (see L</ENVIRONMENT> below),
119 checks that C<$TOP/Configure> or C<$SRCTOP/Configure> exists, C<chdir>
120 into the results directory (defined by the C<$RESULT_D> environment
121 variable if defined, otherwise C<$BLDTOP/test> or C<$TOP/test>, whichever
129 my $old_test_name = $test_name;
132 BAIL_OUT("setup() must receive a name") unless $test_name;
133 warn "setup() detected test name change. Innocuous, so we continue...\n"
134 if $old_test_name && $old_test_name ne $test_name;
136 return if $old_test_name;
138 BAIL_OUT("setup() needs \$TOP or \$SRCTOP and \$BLDTOP to be defined")
139 unless $ENV{TOP} || ($ENV{SRCTOP} && $ENV{BLDTOP});
140 BAIL_OUT("setup() found both \$TOP and \$SRCTOP or \$BLDTOP...")
141 if $ENV{TOP} && ($ENV{SRCTOP} || $ENV{BLDTOP});
145 BAIL_OUT("setup() expects the file Configure in the source top directory")
146 unless -f srctop_file("Configure");
148 __cwd($directories{RESULTS});
153 =item B<indir "SUBDIR" =E<gt> sub BLOCK, OPTS>
155 C<indir> is used to run a part of the recipe in a different directory than
156 the one C<setup> moved into, usually a subdirectory, given by SUBDIR.
157 The part of the recipe that's run there is given by the codeblock BLOCK.
159 C<indir> takes some additional options OPTS that affect the subdirectory:
163 =item B<create =E<gt> 0|1>
165 When set to 1 (or any value that perl preceives as true), the subdirectory
166 will be created if it doesn't already exist. This happens before BLOCK
169 =item B<cleanup =E<gt> 0|1>
171 When set to 1 (or any value that perl preceives as true), the subdirectory
172 will be cleaned out and removed. This happens both before and after BLOCK
180 ok(run(app(["openssl", "version"]), stdout => "foo.txt"));
181 if (ok(open(RESULT, "foo.txt"), "reading foo.txt")) {
184 is($line, qr/^OpenSSL 1\./,
185 "check that we're using OpenSSL 1.x.x");
187 }, create => 1, cleanup => 1;
195 my $codeblock = shift;
198 my $reverse = __cwd($subdir,%opts);
199 BAIL_OUT("FAILURE: indir, \"$subdir\" wasn't possible to move into")
206 if ($opts{cleanup}) {
207 rmtree($subdir, { safe => 0 });
213 =item B<cmd ARRAYREF, OPTS>
215 This functions build up a platform dependent command based on the
216 input. It takes a reference to a list that is the executable or
217 script and its arguments, and some additional options (described
218 further on). Where necessary, the command will be wrapped in a
219 suitable environment to make sure the correct shared libraries are
220 used (currently only on Unix).
222 It returns a CODEREF to be used by C<run>, C<pipe> or C<cmdstr>.
224 The options that C<cmd> can take are in the form of hash values:
228 =item B<stdin =E<gt> PATH>
230 =item B<stdout =E<gt> PATH>
232 =item B<stderr =E<gt> PATH>
234 In all three cases, the corresponding standard input, output or error is
235 redirected from (for stdin) or to (for the others) a file given by the
236 string PATH, I<or>, if the value is C<undef>, C</dev/null> or similar.
240 =item B<app ARRAYREF, OPTS>
242 =item B<test ARRAYREF, OPTS>
244 Both of these are specific applications of C<cmd>, with just a couple
247 C<app> expects to find the given command (the first item in the given list
248 reference) as an executable in C<$BIN_D> (if defined, otherwise C<$TOP/apps>
251 C<test> expects to find the given command (the first item in the given list
252 reference) as an executable in C<$TEST_D> (if defined, otherwise C<$TOP/test>
255 Also, for both C<app> and C<test>, the command may be prefixed with
256 the content of the environment variable C<$EXE_SHELL>, which is useful
257 in case OpenSSL has been cross compiled.
259 =item B<perlapp ARRAYREF, OPTS>
261 =item B<perltest ARRAYREF, OPTS>
263 These are also specific applications of C<cmd>, where the interpreter
264 is predefined to be C<perl>, and they expect the script to be
265 interpreted to reside in the same location as C<app> and C<test>.
267 C<perlapp> and C<perltest> will also take the following option:
271 =item B<interpreter_args =E<gt> ARRAYref>
273 The array reference is a set of arguments for the interpreter rather
274 than the script. Take care so that none of them can be seen as a
275 script! Flags and their eventual arguments only!
281 ok(run(perlapp(["foo.pl", "arg1"],
282 interpreter_args => [ "-I", srctop_dir("test") ])));
288 One might wonder over the complexity of C<apps>, C<fuzz>, C<test>, ...
289 with all the lazy evaluations and all that. The reason for this is that
290 we want to make sure the directory in which those programs are found are
291 correct at the time these commands are used. Consider the following code
294 my $cmd = app(["openssl", ...]);
297 ok(run($cmd), "Testing foo")
300 If there wasn't this lazy evaluation, the directory where C<openssl> is
301 found would be incorrect at the time C<run> is called, because it was
302 calculated before we moved into the directory "foo".
313 # Make a copy to not destroy the caller's array
314 my @cmdargs = ( @$cmd );
315 my @prog = __wrap_cmd(shift @cmdargs, $opts{exe_shell} // ());
317 return __decorate_cmd($num, [ @prog, quotify(@cmdargs) ],
326 my @cmdargs = ( @{$cmd} );
327 my @prog = __fixup_prg(__apps_file(shift @cmdargs, __exeext()));
328 return cmd([ @prog, @cmdargs ],
329 exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
337 my @cmdargs = ( @{$cmd} );
338 my @prog = __fixup_prg(__fuzz_file(shift @cmdargs, __exeext()));
339 return cmd([ @prog, @cmdargs ],
340 exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
348 my @cmdargs = ( @{$cmd} );
349 my @prog = __fixup_prg(__test_file(shift @cmdargs, __exeext()));
350 return cmd([ @prog, @cmdargs ],
351 exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
359 my @interpreter_args = defined $opts{interpreter_args} ?
360 @{$opts{interpreter_args}} : ();
361 my @interpreter = __fixup_prg($^X);
362 my @cmdargs = ( @{$cmd} );
363 my @prog = __apps_file(shift @cmdargs, undef);
364 return cmd([ @interpreter, @interpreter_args,
365 @prog, @cmdargs ], %opts) -> (shift);
373 my @interpreter_args = defined $opts{interpreter_args} ?
374 @{$opts{interpreter_args}} : ();
375 my @interpreter = __fixup_prg($^X);
376 my @cmdargs = ( @{$cmd} );
377 my @prog = __test_file(shift @cmdargs, undef);
378 return cmd([ @interpreter, @interpreter_args,
379 @prog, @cmdargs ], %opts) -> (shift);
385 =item B<run CODEREF, OPTS>
387 CODEREF is expected to be the value return by C<cmd> or any of its
388 derivatives, anything else will most likely cause an error unless you
389 know what you're doing.
391 C<run> executes the command returned by CODEREF and return either the
392 resulting output (if the option C<capture> is set true) or a boolean
393 indicating if the command succeeded or not.
395 The options that C<run> can take are in the form of hash values:
399 =item B<capture =E<gt> 0|1>
401 If true, the command will be executed with a perl backtick, and C<run> will
402 return the resulting output as an array of lines. If false or not given,
403 the command will be executed with C<system()>, and C<run> will return 1 if
404 the command was successful or 0 if it wasn't.
406 =item B<statusvar =E<gt> VARREF>
408 If used, B<VARREF> must be a reference to a scalar variable. It will be
409 assigned a boolean indicating if the command succeeded or not. This is
410 particularly useful together with B<capture>.
414 For further discussion on what is considered a successful command or not, see
415 the function C<with> further down.
422 my ($cmd, $display_cmd) = shift->(0);
428 if ( $^O eq "VMS" ) { # VMS
436 die "OpenSSL::Test::run(): statusvar value not a scalar reference"
437 if $opts{statusvar} && ref($opts{statusvar}) ne "SCALAR";
439 # In non-verbose, we want to shut up the command interpreter, in case
440 # it has something to complain about. On VMS, it might complain both
441 # on stdout and stderr
444 if ($ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_VERBOSE}) {
445 open $save_STDOUT, '>&', \*STDOUT or die "Can't dup STDOUT: $!";
446 open $save_STDERR, '>&', \*STDERR or die "Can't dup STDERR: $!";
447 open STDOUT, ">", devnull();
448 open STDERR, ">", devnull();
451 # The dance we do with $? is the same dance the Unix shells appear to
452 # do. For example, a program that gets aborted (and therefore signals
453 # SIGABRT = 6) will appear to exit with the code 134. We mimic this
454 # to make it easier to compare with a manual run of the command.
455 if ($opts{capture}) {
458 system("$prefix$cmd");
460 $e = ($? & 0x7f) ? ($? & 0x7f)|0x80 : ($? >> 8);
461 $r = $hooks{exit_checker}->($e);
462 if ($opts{statusvar}) {
463 ${$opts{statusvar}} = $r;
466 if ($ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_VERBOSE}) {
469 open STDOUT, '>&', $save_STDOUT or die "Can't restore STDOUT: $!";
470 open STDERR, '>&', $save_STDERR or die "Can't restore STDERR: $!";
473 print STDERR "$prefix$display_cmd => $e\n"
474 if !$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE};
476 # At this point, $? stops being interesting, and unfortunately,
477 # there are Test::More versions that get picky if we leave it
481 if ($opts{capture}) {
489 my $tb = Test::More->builder;
490 my $failure = scalar(grep { $_ == 0; } $tb->summary);
491 if ($failure && $end_with_bailout) {
492 BAIL_OUT("Stoptest!");
496 =head2 Utility functions
498 The following functions are exported on request when using C<OpenSSL::Test>.
500 # To only get the bldtop_file and srctop_file functions.
501 use OpenSSL::Test qw/bldtop_file srctop_file/;
503 # To only get the bldtop_file function in addition to the default ones.
504 use OpenSSL::Test qw/:DEFAULT bldtop_file/;
508 # Utility functions, exported on request
512 =item B<bldtop_dir LIST>
514 LIST is a list of directories that make up a path from the top of the OpenSSL
515 build directory (as indicated by the environment variable C<$TOP> or
517 C<bldtop_dir> returns the resulting directory as a string, adapted to the local
525 return __bldtop_dir(@_); # This caters for operating systems that have
526 # a very distinct syntax for directories.
531 =item B<bldtop_file LIST, FILENAME>
533 LIST is a list of directories that make up a path from the top of the OpenSSL
534 build directory (as indicated by the environment variable C<$TOP> or
535 C<$BLDTOP>) and FILENAME is the name of a file located in that directory path.
536 C<bldtop_file> returns the resulting file path as a string, adapted to the local
544 return __bldtop_file(@_);
549 =item B<srctop_dir LIST>
551 LIST is a list of directories that make up a path from the top of the OpenSSL
552 source directory (as indicated by the environment variable C<$TOP> or
554 C<srctop_dir> returns the resulting directory as a string, adapted to the local
562 return __srctop_dir(@_); # This caters for operating systems that have
563 # a very distinct syntax for directories.
568 =item B<srctop_file LIST, FILENAME>
570 LIST is a list of directories that make up a path from the top of the OpenSSL
571 source directory (as indicated by the environment variable C<$TOP> or
572 C<$SRCTOP>) and FILENAME is the name of a file located in that directory path.
573 C<srctop_file> returns the resulting file path as a string, adapted to the local
581 return __srctop_file(@_);
586 =item B<data_file LIST, FILENAME>
588 LIST is a list of directories that make up a path from the data directory
589 associated with the test (see L</DESCRIPTION> above) and FILENAME is the name
590 of a file located in that directory path. C<data_file> returns the resulting
591 file path as a string, adapted to the local operating system.
598 return __data_file(@_);
605 LIST is a list of CODEREFs returned by C<app> or C<test>, from which C<pipe>
606 creates a new command composed of all the given commands put together in a
607 pipe. C<pipe> returns a new CODEREF in the same manner as C<app> or C<test>,
608 to be passed to C<run> for execution.
623 my ($c, $dc, @el) = $_->(++$counter);
641 =item B<with HASHREF, CODEREF>
643 C<with> will temporarly install hooks given by the HASHREF and then execute
644 the given CODEREF. Hooks are usually expected to have a coderef as value.
646 The currently available hoosk are:
650 =item B<exit_checker =E<gt> CODEREF>
652 This hook is executed after C<run> has performed its given command. The
653 CODEREF receives the exit code as only argument and is expected to return
654 1 (if the exit code indicated success) or 0 (if the exit code indicated
666 my $codeblock = shift;
668 my %saved_hooks = ();
670 foreach (keys %opts) {
671 $saved_hooks{$_} = $hooks{$_} if exists($hooks{$_});
672 $hooks{$_} = $opts{$_};
677 foreach (keys %saved_hooks) {
678 $hooks{$_} = $saved_hooks{$_};
684 =item B<cmdstr CODEREF, OPTS>
686 C<cmdstr> takes a CODEREF from C<app> or C<test> and simply returns the
689 C<cmdstr> takes some additiona options OPTS that affect the string returned:
693 =item B<display =E<gt> 0|1>
695 When set to 0, the returned string will be with all decorations, such as a
696 possible redirect of stderr to the null device. This is suitable if the
697 string is to be used directly in a recipe.
699 When set to 1, the returned string will be without extra decorations. This
700 is suitable for display if that is desired (doesn't confuse people with all
701 internal stuff), or if it's used to pass a command down to a subprocess.
712 my ($cmd, $display_cmd) = shift->(0);
715 if ($opts{display}) {
724 =item B<quotify LIST>
726 LIST is a list of strings that are going to be used as arguments for a
727 command, and makes sure to inject quotes and escapes as necessary depending
728 on the content of each string.
730 This can also be used to put quotes around the executable of a command.
731 I<This must never ever be done on VMS.>
738 # Unix setup (default if nothing else is mentioned)
740 sub { $_ = shift; /\s|[\{\}\\\$\[\]\*\?\|\&:;<>]/ ? "'$_'" : $_ };
742 if ( $^O eq "VMS") { # VMS setup
743 $arg_formatter = sub {
745 if (/\s|["[:upper:]]/) {
752 } elsif ( $^O eq "MSWin32") { # MSWin setup
753 $arg_formatter = sub {
755 if (/\s|["\|\&\*\;<>]/) {
764 return map { $arg_formatter->($_) } @_;
767 ######################################################################
768 # private functions. These are never exported.
772 OpenSSL::Test depends on some environment variables.
778 This environment variable is mandatory. C<setup> will check that it's
779 defined and that it's a directory that contains the file C<Configure>.
780 If this isn't so, C<setup> will C<BAIL_OUT>.
784 If defined, its value should be the directory where the openssl application
785 is located. Defaults to C<$TOP/apps> (adapted to the operating system).
789 If defined, its value should be the directory where the test applications
790 are located. Defaults to C<$TOP/test> (adapted to the operating system).
794 If defined, it puts testing in a different mode, where a recipe with
795 failures will result in a C<BAIL_OUT> at the end of its run.
802 (my $recipe_datadir = basename($0)) =~ s/\.t$/_data/i;
804 $directories{SRCTOP} = $ENV{SRCTOP} || $ENV{TOP};
805 $directories{BLDTOP} = $ENV{BLDTOP} || $ENV{TOP};
806 $directories{BLDAPPS} = $ENV{BIN_D} || __bldtop_dir("apps");
807 $directories{SRCAPPS} = __srctop_dir("apps");
808 $directories{BLDFUZZ} = __bldtop_dir("fuzz");
809 $directories{SRCFUZZ} = __srctop_dir("fuzz");
810 $directories{BLDTEST} = $ENV{TEST_D} || __bldtop_dir("test");
811 $directories{SRCTEST} = __srctop_dir("test");
812 $directories{SRCDATA} = __srctop_dir("test", "recipes",
814 $directories{RESULTS} = $ENV{RESULT_D} || $directories{BLDTEST};
816 push @direnv, "TOP" if $ENV{TOP};
817 push @direnv, "SRCTOP" if $ENV{SRCTOP};
818 push @direnv, "BLDTOP" if $ENV{BLDTOP};
819 push @direnv, "BIN_D" if $ENV{BIN_D};
820 push @direnv, "TEST_D" if $ENV{TEST_D};
821 push @direnv, "RESULT_D" if $ENV{RESULT_D};
823 $end_with_bailout = $ENV{STOPTEST} ? 1 : 0;
826 # __srctop_file and __srctop_dir are helpers to build file and directory
827 # names on top of the source directory. They depend on $SRCTOP, and
828 # therefore on the proper use of setup() and when needed, indir().
829 # __bldtop_file and __bldtop_dir do the same thing but relative to $BLDTOP.
830 # __srctop_file and __bldtop_file take the same kind of argument as
831 # File::Spec::Functions::catfile.
832 # Similarly, __srctop_dir and __bldtop_dir take the same kind of argument
833 # as File::Spec::Functions::catdir
835 BAIL_OUT("Must run setup() first") if (! $test_name);
838 return catfile($directories{SRCTOP},@_,$f);
842 BAIL_OUT("Must run setup() first") if (! $test_name);
844 return catdir($directories{SRCTOP},@_);
848 BAIL_OUT("Must run setup() first") if (! $test_name);
851 return catfile($directories{BLDTOP},@_,$f);
855 BAIL_OUT("Must run setup() first") if (! $test_name);
857 return catdir($directories{BLDTOP},@_);
860 # __exeext is a function that returns the platform dependent file extension
861 # for executable binaries, or the value of the environment variable $EXE_EXT
862 # if that one is defined.
865 if ($^O eq "VMS" ) { # VMS
867 } elsif ($^O eq "MSWin32") { # Windows
870 return $ENV{"EXE_EXT"} || $ext;
873 # __test_file, __apps_file and __fuzz_file return the full path to a file
874 # relative to the test/, apps/ or fuzz/ directory in the build tree or the
875 # source tree, depending on where the file is found. Note that when looking
876 # in the build tree, the file name with an added extension is looked for, if
877 # an extension is given. The intent is to look for executable binaries (in
878 # the build tree) or possibly scripts (in the source tree).
879 # These functions all take the same arguments as File::Spec::Functions::catfile,
880 # *plus* a mandatory extension argument. This extension argument can be undef,
881 # and is ignored in such a case.
883 BAIL_OUT("Must run setup() first") if (! $test_name);
887 $f = catfile($directories{BLDTEST},@_,$f . $e);
888 $f = catfile($directories{SRCTEST},@_,$f) unless -f $f;
893 BAIL_OUT("Must run setup() first") if (! $test_name);
897 $f = catfile($directories{BLDAPPS},@_,$f . $e);
898 $f = catfile($directories{SRCAPPS},@_,$f) unless -f $f;
903 BAIL_OUT("Must run setup() first") if (! $test_name);
907 $f = catfile($directories{BLDFUZZ},@_,$f . $e);
908 $f = catfile($directories{SRCFUZZ},@_,$f) unless -f $f;
913 BAIL_OUT("Must run setup() first") if (! $test_name);
916 return catfile($directories{SRCDATA},@_,$f);
920 BAIL_OUT("Must run setup() first") if (! $test_name);
923 return catfile($directories{RESULTS},@_,$f);
929 # __cwd changes directory to DIR (string) and changes all the relative
930 # entries in %directories accordingly. OPTS is an optional series of
931 # hash style arguments to alter __cwd's behavior:
933 # create = 0|1 The directory we move to is created if 1, not if 0.
934 # cleanup = 0|1 The directory we move from is removed if 1, not if 0.
937 my $dir = catdir(shift);
939 my $abscurdir = rel2abs(curdir());
940 my $absdir = rel2abs($dir);
941 my $reverse = abs2rel($abscurdir, $absdir);
943 # PARANOIA: if we're not moving anywhere, we do nothing more
944 if ($abscurdir eq $absdir) {
948 # Do not support a move to a different volume for now. Maybe later.
949 BAIL_OUT("FAILURE: \"$dir\" moves to a different volume, not supported")
950 if $reverse eq $abscurdir;
952 # If someone happened to give a directory that leads back to the current,
953 # it's extremely silly to do anything more, so just simulate that we did
955 # In this case, we won't even clean it out, for safety's sake.
956 return "." if $reverse eq "";
958 $dir = canonpath($dir);
963 # We are recalculating the directories we keep track of, but need to save
964 # away the result for after having moved into the new directory.
965 my %tmp_directories = ();
968 # For each of these directory variables, figure out where they are relative
969 # to the directory we want to move to if they aren't absolute (if they are,
970 # they don't change!)
971 my @dirtags = sort keys %directories;
973 if (!file_name_is_absolute($directories{$_})) {
974 my $newpath = abs2rel(rel2abs($directories{$_}), rel2abs($dir));
975 $tmp_directories{$_} = $newpath;
979 # Treat each environment variable that was used to get us the values in
980 # %directories the same was as the paths in %directories, so any sub
981 # process can use their values properly as well
983 if (!file_name_is_absolute($ENV{$_})) {
984 my $newpath = abs2rel(rel2abs($ENV{$_}), rel2abs($dir));
985 $tmp_ENV{$_} = $newpath;
989 # Should we just bail out here as well? I'm unsure.
990 return undef unless chdir($dir);
992 if ($opts{cleanup}) {
993 rmtree(".", { safe => 0, keep_root => 1 });
996 # We put back new values carefully. Doing the obvious
997 # %directories = ( %tmp_irectories )
998 # will clear out any value that happens to be an absolute path
999 foreach (keys %tmp_directories) {
1000 $directories{$_} = $tmp_directories{$_};
1002 foreach (keys %tmp_ENV) {
1003 $ENV{$_} = $tmp_ENV{$_};
1007 print STDERR "DEBUG: __cwd(), directories and files:\n";
1008 print STDERR " \$directories{BLDTEST} = \"$directories{BLDTEST}\"\n";
1009 print STDERR " \$directories{SRCTEST} = \"$directories{SRCTEST}\"\n";
1010 print STDERR " \$directories{SRCDATA} = \"$directories{SRCDATA}\"\n";
1011 print STDERR " \$directories{RESULTS} = \"$directories{RESULTS}\"\n";
1012 print STDERR " \$directories{BLDAPPS} = \"$directories{BLDAPPS}\"\n";
1013 print STDERR " \$directories{SRCAPPS} = \"$directories{SRCAPPS}\"\n";
1014 print STDERR " \$directories{SRCTOP} = \"$directories{SRCTOP}\"\n";
1015 print STDERR " \$directories{BLDTOP} = \"$directories{BLDTOP}\"\n";
1017 print STDERR " current directory is \"",curdir(),"\"\n";
1018 print STDERR " the way back is \"$reverse\"\n";
1025 # __wrap_cmd CMD, EXE_SHELL
1027 # __wrap_cmd "wraps" CMD (string) with a beginning command that makes sure
1028 # the command gets executed with an appropriate environment. If EXE_SHELL
1029 # is given, it is used as the beginning command.
1031 # __wrap_cmd returns a list that should be used to build up a larger list
1032 # of command tokens, or be joined together like this:
1034 # join(" ", __wrap_cmd($cmd))
1037 my $exe_shell = shift;
1039 my @prefix = ( __bldtop_file("util", "shlib_wrap.sh") );
1041 if(defined($exe_shell)) {
1042 @prefix = ( $exe_shell );
1043 } elsif ($^O eq "VMS" || $^O eq "MSWin32") {
1044 # VMS and Windows don't use any wrapper script for the moment
1048 return (@prefix, $cmd);
1053 # __fixup_prg does whatever fixup is needed to execute an executable binary
1054 # given by PROG (string).
1056 # __fixup_prg returns a string with the possibly prefixed program path spec.
1062 if ($^O eq "VMS" ) {
1063 $prefix = ($prog =~ /^(?:[\$a-z0-9_]+:)?[<\[]/i ? "mcr " : "mcr []");
1066 # We test if the program to use exists.
1071 if (defined($prog)) {
1072 # Make sure to quotify the program file on platforms that may
1073 # have spaces or similar in their path name.
1074 # To our knowledge, VMS is the exception where quotifying should
1076 ($prog) = quotify($prog) unless $^O eq "VMS";
1077 return $prefix.$prog;
1080 print STDERR "$prog not found\n";
1084 # __decorate_cmd NUM, CMDARRAYREF
1086 # __decorate_cmd takes a command number NUM and a command token array
1087 # CMDARRAYREF, builds up a command string from them and decorates it
1088 # with necessary redirections.
1089 # __decorate_cmd returns a list of two strings, one with the command
1090 # string to actually be used, the other to be displayed for the user.
1091 # The reason these strings might differ is that we redirect stderr to
1092 # the null device unless we're verbose and unless the user has
1093 # explicitly specified a stderr redirection.
1094 sub __decorate_cmd {
1095 BAIL_OUT("Must run setup() first") if (! $test_name);
1101 my $cmdstr = join(" ", @$cmd);
1102 my $null = devnull();
1103 my $fileornull = sub { $_[0] ? $_[0] : $null; };
1107 my $saved_stderr = undef;
1108 $stdin = " < ".$fileornull->($opts{stdin}) if exists($opts{stdin});
1109 $stdout= " > ".$fileornull->($opts{stdout}) if exists($opts{stdout});
1110 $stderr=" 2> ".$fileornull->($opts{stderr}) if exists($opts{stderr});
1112 my $display_cmd = "$cmdstr$stdin$stdout$stderr";
1114 $stderr=" 2> ".$null
1115 unless $stderr || !$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE};
1117 $cmdstr .= "$stdin$stdout$stderr";
1120 print STDERR "DEBUG[__decorate_cmd]: \$cmdstr = \"$cmdstr\"\n";
1121 print STDERR "DEBUG[__decorate_cmd]: \$display_cmd = \"$display_cmd\"\n";
1124 return ($cmdstr, $display_cmd);
1129 L<Test::More>, L<Test::Harness>
1133 Richard Levitte E<lt>levitte@openssl.orgE<gt> with assitance and
1134 inspiration from Andy Polyakov E<lt>appro@openssl.org<gt>.