1 # Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
3 # Licensed under the Apache License 2.0 (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
20 perlapp perltest subtest));
21 @EXPORT_OK = (@Test::More
::EXPORT_OK
, qw(bldtop_dir bldtop_file
22 srctop_dir srctop_file
24 pipe with cmdstr quotify
26 ok_nofips is_nofips isnt_nofips));
30 OpenSSL::Test - a private extension of Test::More
36 setup("my_test_name");
38 ok(run(app(["openssl", "version"])), "check for openssl presence");
40 indir "subdir" => sub {
41 ok(run(test(["sometest", "arg1"], stdout => "foo.txt")),
42 "run sometest with output to foo.txt");
47 This module is a private extension of L<Test::More> for testing OpenSSL.
48 In addition to the Test::More functions, it also provides functions that
49 easily find the diverse programs within a OpenSSL build tree, as well as
50 some other useful functions.
52 This module I<depends> on the environment variables C<$TOP> or C<$SRCTOP>
53 and C<$BLDTOP>. Without one of the combinations it refuses to work.
54 See L</ENVIRONMENT> below.
56 With each test recipe, a parallel data directory with (almost) the same name
57 as the recipe is possible in the source directory tree. For example, for a
58 recipe C<$SRCTOP/test/recipes/99-foo.t>, there could be a directory
59 C<$SRCTOP/test/recipes/99-foo_data/>.
64 use File
::Spec
::Functions qw
/file_name_is_absolute curdir canonpath splitdir
65 catdir catfile splitpath catpath devnull abs2rel
67 use File
::Path
2.00 qw
/rmtree mkpath/;
72 # The name of the test. This is set by setup() and is used in the other
73 # functions to verify that setup() has been used.
74 my $test_name = undef;
76 # Directories we want to keep track of TOP, APPS, TEST and RESULTS are the
77 # ones we're interested in, corresponding to the environment variables TOP
78 # (mandatory), BIN_D, TEST_D, UTIL_D and RESULT_D.
81 # The environment variables that gave us the contents in %directories. These
82 # get modified whenever we change directories, so that subprocesses can use
83 # the values of those environment variables as well
86 # A bool saying if we shall stop all testing if the current recipe has failing
87 # tests or not. This is set by setup() if the environment variable STOPTEST
88 # is defined with a non-empty value.
89 my $end_with_bailout = 0;
91 # A set of hooks that is affected by with() and may be used in diverse places.
92 # All hooks are expected to be CODE references.
95 # exit_checker is used by run() directly after completion of a command.
96 # it receives the exit code from that command and is expected to return
97 # 1 (for success) or 0 (for failure). This is the status value that run()
98 # will give back (through the |statusvar| reference and as returned value
99 # when capture => 1 doesn't apply).
100 exit_checker
=> sub { return shift == 0 ?
1 : 0 },
104 # Debug flag, to be set manually when needed
107 =head2 Main functions
109 The following functions are exported by default when using C<OpenSSL::Test>.
115 =item B<setup "NAME">
117 C<setup> is used for initial setup, and it is mandatory that it's used.
118 If it's not used in a OpenSSL test recipe, the rest of the recipe will
119 most likely refuse to run.
121 C<setup> checks for environment variables (see L</ENVIRONMENT> below),
122 checks that C<$TOP/Configure> or C<$SRCTOP/Configure> exists, C<chdir>
123 into the results directory (defined by the C<$RESULT_D> environment
124 variable if defined, otherwise C<$BLDTOP/test> or C<$TOP/test>, whichever
132 my $old_test_name = $test_name;
135 BAIL_OUT
("setup() must receive a name") unless $test_name;
136 warn "setup() detected test name change. Innocuous, so we continue...\n"
137 if $old_test_name && $old_test_name ne $test_name;
139 return if $old_test_name;
141 BAIL_OUT
("setup() needs \$TOP or \$SRCTOP and \$BLDTOP to be defined")
142 unless $ENV{TOP
} || ($ENV{SRCTOP
} && $ENV{BLDTOP
});
143 BAIL_OUT
("setup() found both \$TOP and \$SRCTOP or \$BLDTOP...")
144 if $ENV{TOP
} && ($ENV{SRCTOP
} || $ENV{BLDTOP
});
148 BAIL_OUT
("setup() expects the file Configure in the source top directory")
149 unless -f srctop_file
("Configure");
151 __cwd
($directories{RESULTS
});
156 =item B<indir "SUBDIR" =E<gt> sub BLOCK, OPTS>
158 C<indir> is used to run a part of the recipe in a different directory than
159 the one C<setup> moved into, usually a subdirectory, given by SUBDIR.
160 The part of the recipe that's run there is given by the codeblock BLOCK.
162 C<indir> takes some additional options OPTS that affect the subdirectory:
166 =item B<create =E<gt> 0|1>
168 When set to 1 (or any value that perl perceives as true), the subdirectory
169 will be created if it doesn't already exist. This happens before BLOCK
172 =item B<cleanup =E<gt> 0|1>
174 When set to 1 (or any value that perl perceives as true), the subdirectory
175 will be cleaned out and removed. This happens both before and after BLOCK
183 ok(run(app(["openssl", "version"]), stdout => "foo.txt"));
184 if (ok(open(RESULT, "foo.txt"), "reading foo.txt")) {
187 is($line, qr/^OpenSSL 1\./,
188 "check that we're using OpenSSL 1.x.x");
190 }, create => 1, cleanup => 1;
198 my $codeblock = shift;
201 my $reverse = __cwd
($subdir,%opts);
202 BAIL_OUT
("FAILURE: indir, \"$subdir\" wasn't possible to move into")
209 if ($opts{cleanup
}) {
210 rmtree
($subdir, { safe
=> 0 });
216 =item B<cmd ARRAYREF, OPTS>
218 This functions build up a platform dependent command based on the
219 input. It takes a reference to a list that is the executable or
220 script and its arguments, and some additional options (described
221 further on). Where necessary, the command will be wrapped in a
222 suitable environment to make sure the correct shared libraries are
223 used (currently only on Unix).
225 It returns a CODEREF to be used by C<run>, C<pipe> or C<cmdstr>.
227 The options that C<cmd> can take are in the form of hash values:
231 =item B<stdin =E<gt> PATH>
233 =item B<stdout =E<gt> PATH>
235 =item B<stderr =E<gt> PATH>
237 In all three cases, the corresponding standard input, output or error is
238 redirected from (for stdin) or to (for the others) a file given by the
239 string PATH, I<or>, if the value is C<undef>, C</dev/null> or similar.
243 =item B<app ARRAYREF, OPTS>
245 =item B<test ARRAYREF, OPTS>
247 Both of these are specific applications of C<cmd>, with just a couple
250 C<app> expects to find the given command (the first item in the given list
251 reference) as an executable in C<$BIN_D> (if defined, otherwise C<$TOP/apps>
254 C<test> expects to find the given command (the first item in the given list
255 reference) as an executable in C<$TEST_D> (if defined, otherwise C<$TOP/test>
258 Also, for both C<app> and C<test>, the command may be prefixed with
259 the content of the environment variable C<$EXE_SHELL>, which is useful
260 in case OpenSSL has been cross compiled.
262 =item B<perlapp ARRAYREF, OPTS>
264 =item B<perltest ARRAYREF, OPTS>
266 These are also specific applications of C<cmd>, where the interpreter
267 is predefined to be C<perl>, and they expect the script to be
268 interpreted to reside in the same location as C<app> and C<test>.
270 C<perlapp> and C<perltest> will also take the following option:
274 =item B<interpreter_args =E<gt> ARRAYref>
276 The array reference is a set of arguments for the interpreter rather
277 than the script. Take care so that none of them can be seen as a
278 script! Flags and their eventual arguments only!
284 ok(run(perlapp(["foo.pl", "arg1"],
285 interpreter_args => [ "-I", srctop_dir("test") ])));
291 One might wonder over the complexity of C<apps>, C<fuzz>, C<test>, ...
292 with all the lazy evaluations and all that. The reason for this is that
293 we want to make sure the directory in which those programs are found are
294 correct at the time these commands are used. Consider the following code
297 my $cmd = app(["openssl", ...]);
300 ok(run($cmd), "Testing foo")
303 If there wasn't this lazy evaluation, the directory where C<openssl> is
304 found would be incorrect at the time C<run> is called, because it was
305 calculated before we moved into the directory "foo".
316 # Make a copy to not destroy the caller's array
317 my @cmdargs = ( @
$cmd );
318 my @prog = __wrap_cmd
(shift @cmdargs, $opts{exe_shell
} // ());
320 return __decorate_cmd
($num, [ @prog, quotify
(@cmdargs) ],
329 my @cmdargs = ( @
{$cmd} );
330 my @prog = __fixup_prg
(__apps_file
(shift @cmdargs, __exeext
()));
331 return cmd
([ @prog, @cmdargs ],
332 exe_shell
=> $ENV{EXE_SHELL
}, %opts) -> (shift);
340 my @cmdargs = ( @
{$cmd} );
341 my @prog = __fixup_prg
(__fuzz_file
(shift @cmdargs, __exeext
()));
342 return cmd
([ @prog, @cmdargs ],
343 exe_shell
=> $ENV{EXE_SHELL
}, %opts) -> (shift);
351 my @cmdargs = ( @
{$cmd} );
352 my @prog = __fixup_prg
(__test_file
(shift @cmdargs, __exeext
()));
353 return cmd
([ @prog, @cmdargs ],
354 exe_shell
=> $ENV{EXE_SHELL
}, %opts) -> (shift);
362 my @interpreter_args = defined $opts{interpreter_args
} ?
363 @
{$opts{interpreter_args
}} : ();
364 my @interpreter = __fixup_prg
($^X
);
365 my @cmdargs = ( @
{$cmd} );
366 my @prog = __apps_file
(shift @cmdargs, undef);
367 return cmd
([ @interpreter, @interpreter_args,
368 @prog, @cmdargs ], %opts) -> (shift);
376 my @interpreter_args = defined $opts{interpreter_args
} ?
377 @
{$opts{interpreter_args
}} : ();
378 my @interpreter = __fixup_prg
($^X
);
379 my @cmdargs = ( @
{$cmd} );
380 my @prog = __test_file
(shift @cmdargs, undef);
381 return cmd
([ @interpreter, @interpreter_args,
382 @prog, @cmdargs ], %opts) -> (shift);
388 =item B<run CODEREF, OPTS>
390 CODEREF is expected to be the value return by C<cmd> or any of its
391 derivatives, anything else will most likely cause an error unless you
392 know what you're doing.
394 C<run> executes the command returned by CODEREF and return either the
395 resulting output (if the option C<capture> is set true) or a boolean
396 indicating if the command succeeded or not.
398 The options that C<run> can take are in the form of hash values:
402 =item B<capture =E<gt> 0|1>
404 If true, the command will be executed with a perl backtick, and C<run> will
405 return the resulting output as an array of lines. If false or not given,
406 the command will be executed with C<system()>, and C<run> will return 1 if
407 the command was successful or 0 if it wasn't.
409 =item B<prefix =E<gt> EXPR>
411 If specified, EXPR will be used as a string to prefix the output from the
412 command. This is useful if the output contains lines starting with C<ok >
413 or C<not ok > that can disturb Test::Harness.
415 =item B<statusvar =E<gt> VARREF>
417 If used, B<VARREF> must be a reference to a scalar variable. It will be
418 assigned a boolean indicating if the command succeeded or not. This is
419 particularly useful together with B<capture>.
423 For further discussion on what is considered a successful command or not, see
424 the function C<with> further down.
431 my ($cmd, $display_cmd) = shift->(0);
437 if ( $^O
eq "VMS" ) { # VMS
445 die "OpenSSL::Test::run(): statusvar value not a scalar reference"
446 if $opts{statusvar
} && ref($opts{statusvar
}) ne "SCALAR";
448 # In non-verbose, we want to shut up the command interpreter, in case
449 # it has something to complain about. On VMS, it might complain both
450 # on stdout and stderr
453 if ($ENV{HARNESS_ACTIVE
} && !$ENV{HARNESS_VERBOSE
}) {
454 open $save_STDOUT, '>&', \
*STDOUT
or die "Can't dup STDOUT: $!";
455 open $save_STDERR, '>&', \
*STDERR
or die "Can't dup STDERR: $!";
456 open STDOUT
, ">", devnull
();
457 open STDERR
, ">", devnull
();
460 $ENV{HARNESS_OSSL_LEVEL
} = $level + 1;
462 # The dance we do with $? is the same dance the Unix shells appear to
463 # do. For example, a program that gets aborted (and therefore signals
464 # SIGABRT = 6) will appear to exit with the code 134. We mimic this
465 # to make it easier to compare with a manual run of the command.
466 if ($opts{capture
} || defined($opts{prefix
})) {
470 open($pipe, '-|', "$prefix$cmd") or die "Can't start command: $!";
472 my $l = ($opts{prefix
} // "") . $_;
473 if ($opts{capture
}) {
481 $ENV{HARNESS_OSSL_PREFIX
} = "# ";
482 system("$prefix$cmd");
483 delete $ENV{HARNESS_OSSL_PREFIX
};
485 $e = ($?
& 0x7f) ?
($?
& 0x7f)|0x80 : ($?
>> 8);
486 $r = $hooks{exit_checker
}->($e);
487 if ($opts{statusvar
}) {
488 ${$opts{statusvar
}} = $r;
491 if ($ENV{HARNESS_ACTIVE
} && !$ENV{HARNESS_VERBOSE
}) {
494 open STDOUT
, '>&', $save_STDOUT or die "Can't restore STDOUT: $!";
495 open STDERR
, '>&', $save_STDERR or die "Can't restore STDERR: $!";
498 print STDERR
"$prefix$display_cmd => $e\n"
499 if !$ENV{HARNESS_ACTIVE
} || $ENV{HARNESS_VERBOSE
};
501 # At this point, $? stops being interesting, and unfortunately,
502 # there are Test::More versions that get picky if we leave it
506 if ($opts{capture
}) {
514 my $tb = Test
::More
->builder;
515 my $failure = scalar(grep { $_ == 0; } $tb->summary);
516 if ($failure && $end_with_bailout) {
517 BAIL_OUT
("Stoptest!");
521 =head2 Utility functions
523 The following functions are exported on request when using C<OpenSSL::Test>.
525 # To only get the bldtop_file and srctop_file functions.
526 use OpenSSL::Test qw/bldtop_file srctop_file/;
528 # To only get the bldtop_file function in addition to the default ones.
529 use OpenSSL::Test qw/:DEFAULT bldtop_file/;
533 # Utility functions, exported on request
537 =item B<bldtop_dir LIST>
539 LIST is a list of directories that make up a path from the top of the OpenSSL
540 build directory (as indicated by the environment variable C<$TOP> or
542 C<bldtop_dir> returns the resulting directory as a string, adapted to the local
550 return __bldtop_dir
(@_); # This caters for operating systems that have
551 # a very distinct syntax for directories.
556 =item B<bldtop_file LIST, FILENAME>
558 LIST is a list of directories that make up a path from the top of the OpenSSL
559 build directory (as indicated by the environment variable C<$TOP> or
560 C<$BLDTOP>) and FILENAME is the name of a file located in that directory path.
561 C<bldtop_file> returns the resulting file path as a string, adapted to the local
569 return __bldtop_file
(@_);
574 =item B<srctop_dir LIST>
576 LIST is a list of directories that make up a path from the top of the OpenSSL
577 source directory (as indicated by the environment variable C<$TOP> or
579 C<srctop_dir> returns the resulting directory as a string, adapted to the local
587 return __srctop_dir
(@_); # This caters for operating systems that have
588 # a very distinct syntax for directories.
593 =item B<srctop_file LIST, FILENAME>
595 LIST is a list of directories that make up a path from the top of the OpenSSL
596 source directory (as indicated by the environment variable C<$TOP> or
597 C<$SRCTOP>) and FILENAME is the name of a file located in that directory path.
598 C<srctop_file> returns the resulting file path as a string, adapted to the local
606 return __srctop_file
(@_);
611 =item B<data_dir LIST>
613 LIST is a list of directories that make up a path from the data directory
614 associated with the test (see L</DESCRIPTION> above).
615 C<data_dir> returns the resulting directory as a string, adapted to the local
623 return __data_dir
(@_);
628 =item B<data_file LIST, FILENAME>
630 LIST is a list of directories that make up a path from the data directory
631 associated with the test (see L</DESCRIPTION> above) and FILENAME is the name
632 of a file located in that directory path. C<data_file> returns the resulting
633 file path as a string, adapted to the local operating system.
640 return __data_file
(@_);
647 LIST is a list of CODEREFs returned by C<app> or C<test>, from which C<pipe>
648 creates a new command composed of all the given commands put together in a
649 pipe. C<pipe> returns a new CODEREF in the same manner as C<app> or C<test>,
650 to be passed to C<run> for execution.
665 my ($c, $dc, @el) = $_->(++$counter);
683 =item B<with HASHREF, CODEREF>
685 C<with> will temporarily install hooks given by the HASHREF and then execute
686 the given CODEREF. Hooks are usually expected to have a coderef as value.
688 The currently available hoosk are:
692 =item B<exit_checker =E<gt> CODEREF>
694 This hook is executed after C<run> has performed its given command. The
695 CODEREF receives the exit code as only argument and is expected to return
696 1 (if the exit code indicated success) or 0 (if the exit code indicated
708 my $codeblock = shift;
710 my %saved_hooks = ();
712 foreach (keys %opts) {
713 $saved_hooks{$_} = $hooks{$_} if exists($hooks{$_});
714 $hooks{$_} = $opts{$_};
719 foreach (keys %saved_hooks) {
720 $hooks{$_} = $saved_hooks{$_};
726 =item B<cmdstr CODEREF, OPTS>
728 C<cmdstr> takes a CODEREF from C<app> or C<test> and simply returns the
731 C<cmdstr> takes some additional options OPTS that affect the string returned:
735 =item B<display =E<gt> 0|1>
737 When set to 0, the returned string will be with all decorations, such as a
738 possible redirect of stderr to the null device. This is suitable if the
739 string is to be used directly in a recipe.
741 When set to 1, the returned string will be without extra decorations. This
742 is suitable for display if that is desired (doesn't confuse people with all
743 internal stuff), or if it's used to pass a command down to a subprocess.
754 my ($cmd, $display_cmd) = shift->(0);
757 if ($opts{display
}) {
766 =item B<quotify LIST>
768 LIST is a list of strings that are going to be used as arguments for a
769 command, and makes sure to inject quotes and escapes as necessary depending
770 on the content of each string.
772 This can also be used to put quotes around the executable of a command.
773 I<This must never ever be done on VMS.>
780 # Unix setup (default if nothing else is mentioned)
783 ($_ eq '' || /\s|[\{\}\\\$\[\]\*\?\|\&:;<>]/) ?
"'$_'" : $_ };
785 if ( $^O
eq "VMS") { # VMS setup
786 $arg_formatter = sub {
788 if ($_ eq '' || /\s|["[:upper:]]/) {
795 } elsif ( $^O
eq "MSWin32") { # MSWin setup
796 $arg_formatter = sub {
798 if ($_ eq '' || /\s|["\|\&\*\;<>]/) {
807 return map { $arg_formatter->($_) } @_;
812 =item B<openssl_versions>
814 Returns a list of two version numbers, the first representing the build
815 version, the second representing the library version. See opensslv.h for
816 more information on those numbers.
823 sub openssl_versions
{
829 run
(test
(['versions']), capture
=> 1);
830 @versions = ( $lines{'Build version'}, $lines{'Library version'} );
837 =item B<ok_nofips EXPR, TEST_NAME>
839 C<ok_nofips> is equivalent to using C<ok> when the environment variable
840 C<FIPS_MODE> is undefined, otherwise it is equivalent to C<not ok>. This can be
841 used for C<ok> tests that must fail when testing a FIPS provider. The parameters
842 are the same as used by C<ok> which is an expression EXPR followed by the test
843 description TEST_NAME.
847 ok_nofips(run(app(["md5.pl"])), "md5 should fail in fips mode");
849 =item B<is_nofips EXPR1, EXPR2, TEST_NAME>
851 C<is_nofips> is equivalent to using C<is> when the environment variable
852 C<FIPS_MODE> is undefined, otherwise it is equivalent to C<isnt>. This can be
853 used for C<is> tests that must fail when testing a FIPS provider. The parameters
854 are the same as used by C<is> which has 2 arguments EXPR1 and EXPR2 that can be
855 compared using eq or ne, followed by a test description TEST_NAME.
859 is_nofips(ultimate_answer(), 42, "Meaning of Life");
861 =item B<isnt_nofips EXPR1, EXPR2, TEST_NAME>
863 C<isnt_nofips> is equivalent to using C<isnt> when the environment variable
864 C<FIPS_MODE> is undefined, otherwise it is equivalent to C<is>. This can be
865 used for C<isnt> tests that must fail when testing a FIPS provider. The
866 parameters are the same as used by C<isnt> which has 2 arguments EXPR1 and EXPR2
867 that can be compared using ne or eq, followed by a test description TEST_NAME.
871 isnt_nofips($foo, '', "Got some foo");
878 return ok
(!$_[0], @_[1..$#_]) if defined $ENV{FIPS_MODE
};
879 return ok
($_[0], @_[1..$#_]);
883 return isnt
($_[0], $_[1], @_[2..$#_]) if defined $ENV{FIPS_MODE
};
884 return is
($_[0], $_[1], @_[2..$#_]);
888 return is
($_[0], $_[1], @_[2..$#_]) if defined $ENV{FIPS_MODE
};
889 return isnt
($_[0], $_[1], @_[2..$#_]);
892 ######################################################################
893 # private functions. These are never exported.
897 OpenSSL::Test depends on some environment variables.
903 This environment variable is mandatory. C<setup> will check that it's
904 defined and that it's a directory that contains the file C<Configure>.
905 If this isn't so, C<setup> will C<BAIL_OUT>.
909 If defined, its value should be the directory where the openssl application
910 is located. Defaults to C<$TOP/apps> (adapted to the operating system).
914 If defined, its value should be the directory where the test applications
915 are located. Defaults to C<$TOP/test> (adapted to the operating system).
919 If defined, it puts testing in a different mode, where a recipe with
920 failures will result in a C<BAIL_OUT> at the end of its run.
924 If defined it indicates that the FIPS provider is being tested. Tests may use
925 B<ok_nofips>, B<is_nofips> and B<isnt_nofips> to invert test results
926 i.e. Some tests may only work in non FIPS mode.
933 (my $recipe_datadir = basename
($0)) =~ s/\.t$/_data/i;
935 $directories{SRCTOP
} = $ENV{SRCTOP
} || $ENV{TOP
};
936 $directories{BLDTOP
} = $ENV{BLDTOP
} || $ENV{TOP
};
937 $directories{BLDAPPS
} = $ENV{BIN_D
} || __bldtop_dir
("apps");
938 $directories{SRCAPPS
} = __srctop_dir
("apps");
939 $directories{BLDFUZZ
} = __bldtop_dir
("fuzz");
940 $directories{SRCFUZZ
} = __srctop_dir
("fuzz");
941 $directories{BLDTEST
} = $ENV{TEST_D
} || __bldtop_dir
("test");
942 $directories{SRCTEST
} = __srctop_dir
("test");
943 $directories{SRCDATA
} = __srctop_dir
("test", "recipes",
945 $directories{RESULTS
} = $ENV{RESULT_D
} || $directories{BLDTEST
};
947 push @direnv, "TOP" if $ENV{TOP
};
948 push @direnv, "SRCTOP" if $ENV{SRCTOP
};
949 push @direnv, "BLDTOP" if $ENV{BLDTOP
};
950 push @direnv, "BIN_D" if $ENV{BIN_D
};
951 push @direnv, "TEST_D" if $ENV{TEST_D
};
952 push @direnv, "RESULT_D" if $ENV{RESULT_D
};
954 $end_with_bailout = $ENV{STOPTEST
} ?
1 : 0;
957 # __srctop_file and __srctop_dir are helpers to build file and directory
958 # names on top of the source directory. They depend on $SRCTOP, and
959 # therefore on the proper use of setup() and when needed, indir().
960 # __bldtop_file and __bldtop_dir do the same thing but relative to $BLDTOP.
961 # __srctop_file and __bldtop_file take the same kind of argument as
962 # File::Spec::Functions::catfile.
963 # Similarly, __srctop_dir and __bldtop_dir take the same kind of argument
964 # as File::Spec::Functions::catdir
966 BAIL_OUT
("Must run setup() first") if (! $test_name);
969 return catfile
($directories{SRCTOP
},@_,$f);
973 BAIL_OUT
("Must run setup() first") if (! $test_name);
975 return catdir
($directories{SRCTOP
},@_);
979 BAIL_OUT
("Must run setup() first") if (! $test_name);
982 return catfile
($directories{BLDTOP
},@_,$f);
986 BAIL_OUT
("Must run setup() first") if (! $test_name);
988 return catdir
($directories{BLDTOP
},@_);
991 # __exeext is a function that returns the platform dependent file extension
992 # for executable binaries, or the value of the environment variable $EXE_EXT
993 # if that one is defined.
996 if ($^O
eq "VMS" ) { # VMS
998 } elsif ($^O
eq "MSWin32") { # Windows
1001 return $ENV{"EXE_EXT"} || $ext;
1004 # __test_file, __apps_file and __fuzz_file return the full path to a file
1005 # relative to the test/, apps/ or fuzz/ directory in the build tree or the
1006 # source tree, depending on where the file is found. Note that when looking
1007 # in the build tree, the file name with an added extension is looked for, if
1008 # an extension is given. The intent is to look for executable binaries (in
1009 # the build tree) or possibly scripts (in the source tree).
1010 # These functions all take the same arguments as File::Spec::Functions::catfile,
1011 # *plus* a mandatory extension argument. This extension argument can be undef,
1012 # and is ignored in such a case.
1014 BAIL_OUT
("Must run setup() first") if (! $test_name);
1018 my $out = catfile
($directories{BLDTEST
},@_,$f . $e);
1019 $out = catfile
($directories{SRCTEST
},@_,$f) unless -f
$out;
1024 BAIL_OUT
("Must run setup() first") if (! $test_name);
1028 my $out = catfile
($directories{BLDAPPS
},@_,$f . $e);
1029 $out = catfile
($directories{SRCAPPS
},@_,$f) unless -f
$out;
1034 BAIL_OUT
("Must run setup() first") if (! $test_name);
1038 my $out = catfile
($directories{BLDFUZZ
},@_,$f . $e);
1039 $out = catfile
($directories{SRCFUZZ
},@_,$f) unless -f
$out;
1044 BAIL_OUT
("Must run setup() first") if (! $test_name);
1047 return catfile
($directories{SRCDATA
},@_,$f);
1051 BAIL_OUT
("Must run setup() first") if (! $test_name);
1053 return catdir
($directories{SRCDATA
},@_);
1056 sub __results_file
{
1057 BAIL_OUT
("Must run setup() first") if (! $test_name);
1060 return catfile
($directories{RESULTS
},@_,$f);
1066 # __cwd changes directory to DIR (string) and changes all the relative
1067 # entries in %directories accordingly. OPTS is an optional series of
1068 # hash style arguments to alter __cwd's behavior:
1070 # create = 0|1 The directory we move to is created if 1, not if 0.
1071 # cleanup = 0|1 The directory we move from is removed if 1, not if 0.
1074 my $dir = catdir
(shift);
1076 my $abscurdir = rel2abs
(curdir
());
1077 my $absdir = rel2abs
($dir);
1078 my $reverse = abs2rel
($abscurdir, $absdir);
1080 # PARANOIA: if we're not moving anywhere, we do nothing more
1081 if ($abscurdir eq $absdir) {
1085 # Do not support a move to a different volume for now. Maybe later.
1086 BAIL_OUT
("FAILURE: \"$dir\" moves to a different volume, not supported")
1087 if $reverse eq $abscurdir;
1089 # If someone happened to give a directory that leads back to the current,
1090 # it's extremely silly to do anything more, so just simulate that we did
1092 # In this case, we won't even clean it out, for safety's sake.
1093 return "." if $reverse eq "";
1095 $dir = canonpath
($dir);
1096 if ($opts{create
}) {
1100 # We are recalculating the directories we keep track of, but need to save
1101 # away the result for after having moved into the new directory.
1102 my %tmp_directories = ();
1105 # For each of these directory variables, figure out where they are relative
1106 # to the directory we want to move to if they aren't absolute (if they are,
1107 # they don't change!)
1108 my @dirtags = sort keys %directories;
1109 foreach (@dirtags) {
1110 if (!file_name_is_absolute
($directories{$_})) {
1111 my $newpath = abs2rel
(rel2abs
($directories{$_}), rel2abs
($dir));
1112 $tmp_directories{$_} = $newpath;
1116 # Treat each environment variable that was used to get us the values in
1117 # %directories the same was as the paths in %directories, so any sub
1118 # process can use their values properly as well
1120 if (!file_name_is_absolute
($ENV{$_})) {
1121 my $newpath = abs2rel
(rel2abs
($ENV{$_}), rel2abs
($dir));
1122 $tmp_ENV{$_} = $newpath;
1126 # Should we just bail out here as well? I'm unsure.
1127 return undef unless chdir($dir);
1129 if ($opts{cleanup
}) {
1130 rmtree
(".", { safe
=> 0, keep_root
=> 1 });
1133 # We put back new values carefully. Doing the obvious
1134 # %directories = ( %tmp_directories )
1135 # will clear out any value that happens to be an absolute path
1136 foreach (keys %tmp_directories) {
1137 $directories{$_} = $tmp_directories{$_};
1139 foreach (keys %tmp_ENV) {
1140 $ENV{$_} = $tmp_ENV{$_};
1144 print STDERR
"DEBUG: __cwd(), directories and files:\n";
1145 print STDERR
" \$directories{BLDTEST} = \"$directories{BLDTEST}\"\n";
1146 print STDERR
" \$directories{SRCTEST} = \"$directories{SRCTEST}\"\n";
1147 print STDERR
" \$directories{SRCDATA} = \"$directories{SRCDATA}\"\n";
1148 print STDERR
" \$directories{RESULTS} = \"$directories{RESULTS}\"\n";
1149 print STDERR
" \$directories{BLDAPPS} = \"$directories{BLDAPPS}\"\n";
1150 print STDERR
" \$directories{SRCAPPS} = \"$directories{SRCAPPS}\"\n";
1151 print STDERR
" \$directories{SRCTOP} = \"$directories{SRCTOP}\"\n";
1152 print STDERR
" \$directories{BLDTOP} = \"$directories{BLDTOP}\"\n";
1154 print STDERR
" current directory is \"",curdir
(),"\"\n";
1155 print STDERR
" the way back is \"$reverse\"\n";
1162 # __wrap_cmd CMD, EXE_SHELL
1164 # __wrap_cmd "wraps" CMD (string) with a beginning command that makes sure
1165 # the command gets executed with an appropriate environment. If EXE_SHELL
1166 # is given, it is used as the beginning command.
1168 # __wrap_cmd returns a list that should be used to build up a larger list
1169 # of command tokens, or be joined together like this:
1171 # join(" ", __wrap_cmd($cmd))
1174 my $exe_shell = shift;
1176 my @prefix = ( __bldtop_file
("util", "shlib_wrap.sh") );
1178 if(defined($exe_shell)) {
1179 @prefix = ( $exe_shell );
1180 } elsif ($^O
eq "VMS" || $^O
eq "MSWin32") {
1181 # VMS and Windows don't use any wrapper script for the moment
1185 return (@prefix, $cmd);
1190 # __fixup_prg does whatever fixup is needed to execute an executable binary
1191 # given by PROG (string).
1193 # __fixup_prg returns a string with the possibly prefixed program path spec.
1199 if ($^O
eq "VMS" ) {
1200 $prefix = ($prog =~ /^(?:[\$a-z0-9_]+:)?[<\[]/i ?
"mcr " : "mcr []");
1203 if (defined($prog)) {
1204 # Make sure to quotify the program file on platforms that may
1205 # have spaces or similar in their path name.
1206 # To our knowledge, VMS is the exception where quotifying should
1208 ($prog) = quotify
($prog) unless $^O
eq "VMS";
1209 return $prefix.$prog;
1212 print STDERR
"$prog not found\n";
1216 # __decorate_cmd NUM, CMDARRAYREF
1218 # __decorate_cmd takes a command number NUM and a command token array
1219 # CMDARRAYREF, builds up a command string from them and decorates it
1220 # with necessary redirections.
1221 # __decorate_cmd returns a list of two strings, one with the command
1222 # string to actually be used, the other to be displayed for the user.
1223 # The reason these strings might differ is that we redirect stderr to
1224 # the null device unless we're verbose and unless the user has
1225 # explicitly specified a stderr redirection.
1226 sub __decorate_cmd
{
1227 BAIL_OUT
("Must run setup() first") if (! $test_name);
1233 my $cmdstr = join(" ", @
$cmd);
1234 my $null = devnull
();
1235 my $fileornull = sub { $_[0] ?
$_[0] : $null; };
1239 my $saved_stderr = undef;
1240 $stdin = " < ".$fileornull->($opts{stdin
}) if exists($opts{stdin
});
1241 $stdout= " > ".$fileornull->($opts{stdout
}) if exists($opts{stdout
});
1242 $stderr=" 2> ".$fileornull->($opts{stderr
}) if exists($opts{stderr
});
1244 my $display_cmd = "$cmdstr$stdin$stdout$stderr";
1246 $stderr=" 2> ".$null
1247 unless $stderr || !$ENV{HARNESS_ACTIVE
} || $ENV{HARNESS_VERBOSE
};
1249 $cmdstr .= "$stdin$stdout$stderr";
1252 print STDERR
"DEBUG[__decorate_cmd]: \$cmdstr = \"$cmdstr\"\n";
1253 print STDERR
"DEBUG[__decorate_cmd]: \$display_cmd = \"$display_cmd\"\n";
1256 return ($cmdstr, $display_cmd);
1261 L<Test::More>, L<Test::Harness>
1265 Richard Levitte E<lt>levitte@openssl.orgE<gt> with assistance and
1266 inspiration from Andy Polyakov E<lt>appro@openssl.org<gt>.
1270 no warnings
'redefine';
1274 Test
::More
::subtest
@_;