]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/testlib/OpenSSL/Test.pm
Fix the directory target generation
[thirdparty/openssl.git] / test / testlib / OpenSSL / Test.pm
CommitLineData
ac3d0e13
RS
1# Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
2#
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
7
aec27d4d
RL
8package OpenSSL::Test;
9
10use strict;
11use warnings;
12
fd99c6b5
RL
13use Test::More 0.96;
14
aec27d4d
RL
15use Exporter;
16use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
42e0ccdf 17$VERSION = "0.8";
aec27d4d 18@ISA = qw(Exporter);
a00c84f6 19@EXPORT = (@Test::More::EXPORT, qw(setup indir app perlapp test perltest run));
42e0ccdf
RL
20@EXPORT_OK = (@Test::More::EXPORT_OK, qw(bldtop_dir bldtop_file
21 srctop_dir srctop_file
22 pipe with cmdstr quotify));
aec27d4d 23
f5098edb 24=head1 NAME
aec27d4d 25
f5098edb 26OpenSSL::Test - a private extension of Test::More
aec27d4d 27
f5098edb 28=head1 SYNOPSIS
aec27d4d 29
f5098edb 30 use OpenSSL::Test;
aec27d4d 31
f5098edb 32 setup("my_test_name");
aec27d4d 33
f5098edb 34 ok(run(app(["openssl", "version"])), "check for openssl presence");
caadc543 35
f5098edb
RL
36 indir "subdir" => sub {
37 ok(run(test(["sometest", "arg1"], stdout => "foo.txt")),
38 "run sometest with output to foo.txt");
39 };
aec27d4d 40
f5098edb 41=head1 DESCRIPTION
aec27d4d 42
f5098edb
RL
43This module is a private extension of L<Test::More> for testing OpenSSL.
44In addition to the Test::More functions, it also provides functions that
45easily find the diverse programs within a OpenSSL build tree, as well as
46some other useful functions.
aec27d4d 47
42e0ccdf
RL
48This module I<depends> on the environment variables C<$TOP> or C<$SRCTOP>
49and C<$BLDTOP>. Without one of the combinations it refuses to work.
50See L</ENVIRONMENT> below.
aec27d4d 51
f5098edb 52=cut
aec27d4d 53
f5098edb
RL
54use File::Copy;
55use File::Spec::Functions qw/file_name_is_absolute curdir canonpath splitdir
56 catdir catfile splitpath catpath devnull abs2rel
57 rel2abs/;
4500a4cd 58use File::Path 2.00 qw/rmtree mkpath/;
aec27d4d 59
aec27d4d 60
f5098edb
RL
61# The name of the test. This is set by setup() and is used in the other
62# functions to verify that setup() has been used.
63my $test_name = undef;
aec27d4d 64
f5098edb
RL
65# Directories we want to keep track of TOP, APPS, TEST and RESULTS are the
66# ones we're interested in, corresponding to the environment variables TOP
42e0ccdf 67# (mandatory), BIN_D, TEST_D, UTIL_D and RESULT_D.
f5098edb 68my %directories = ();
aec27d4d 69
d1094383
RL
70# The environment variables that gave us the contents in %directories. These
71# get modified whenever we change directories, so that subprocesses can use
72# the values of those environment variables as well
73my @direnv = ();
74
f5098edb
RL
75# A bool saying if we shall stop all testing if the current recipe has failing
76# tests or not. This is set by setup() if the environment variable STOPTEST
77# is defined with a non-empty value.
78my $end_with_bailout = 0;
aec27d4d 79
f5098edb
RL
80# A set of hooks that is affected by with() and may be used in diverse places.
81# All hooks are expected to be CODE references.
82my %hooks = (
aec27d4d 83
f5098edb
RL
84 # exit_checker is used by run() directly after completion of a command.
85 # it receives the exit code from that command and is expected to return
86 # 1 (for success) or 0 (for failure). This is the value that will be
87 # returned by run().
88 # NOTE: When run() gets the option 'capture => 1', this hook is ignored.
89 exit_checker => sub { return shift == 0 ? 1 : 0 },
aec27d4d 90
f5098edb 91 );
aec27d4d 92
a00c84f6
RL
93# Debug flag, to be set manually when needed
94my $debug = 0;
95
f5098edb 96# Declare some utility functions that are defined at the end
42e0ccdf
RL
97sub bldtop_file;
98sub bldtop_dir;
99sub srctop_file;
100sub srctop_dir;
f5098edb 101sub quotify;
aec27d4d 102
f5098edb
RL
103# Declare some private functions that are defined at the end
104sub __env;
105sub __cwd;
106sub __apps_file;
107sub __results_file;
f5098edb
RL
108sub __fixup_cmd;
109sub __build_cmd;
aec27d4d 110
f5098edb 111=head2 Main functions
aec27d4d 112
f5098edb 113The following functions are exported by default when using C<OpenSSL::Test>.
aec27d4d 114
f5098edb 115=cut
aec27d4d 116
f5098edb 117=over 4
aec27d4d 118
f5098edb 119=item B<setup "NAME">
aec27d4d 120
f5098edb
RL
121C<setup> is used for initial setup, and it is mandatory that it's used.
122If it's not used in a OpenSSL test recipe, the rest of the recipe will
123most likely refuse to run.
124
125C<setup> checks for environment variables (see L</ENVIRONMENT> below),
42e0ccdf
RL
126checks that C<$TOP/Configure> or C<$SRCTOP/Configure> exists, C<chdir>
127into the results directory (defined by the C<$RESULT_D> environment
128variable if defined, otherwise C<$BLDTOP/test> or C<$TOP/test>, whichever
129is defined).
f5098edb
RL
130
131=back
132
133=cut
aec27d4d
RL
134
135sub setup {
fa657fc8 136 my $old_test_name = $test_name;
aec27d4d
RL
137 $test_name = shift;
138
139 BAIL_OUT("setup() must receive a name") unless $test_name;
fa657fc8
RL
140 warn "setup() detected test name change. Innocuous, so we continue...\n"
141 if $old_test_name && $old_test_name ne $test_name;
142
143 return if $old_test_name;
144
42e0ccdf
RL
145 BAIL_OUT("setup() needs \$TOP or \$SRCTOP and \$BLDTOP to be defined")
146 unless $ENV{TOP} || ($ENV{SRCTOP} && $ENV{BLDTOP});
147 BAIL_OUT("setup() found both \$TOP and \$SRCTOP or \$BLDTOP...")
148 if $ENV{TOP} && ($ENV{SRCTOP} || $ENV{BLDTOP});
aec27d4d 149
f5098edb 150 __env();
caadc543 151
fa657fc8
RL
152 BAIL_OUT("setup() expects the file Configure in the source top directory")
153 unless -f srctop_file("Configure");
aec27d4d
RL
154
155 __cwd($directories{RESULTS});
aec27d4d
RL
156}
157
f5098edb
RL
158=over 4
159
160=item B<indir "SUBDIR" =E<gt> sub BLOCK, OPTS>
161
162C<indir> is used to run a part of the recipe in a different directory than
163the one C<setup> moved into, usually a subdirectory, given by SUBDIR.
164The part of the recipe that's run there is given by the codeblock BLOCK.
165
166C<indir> takes some additional options OPTS that affect the subdirectory:
167
168=over 4
169
170=item B<create =E<gt> 0|1>
171
172When set to 1 (or any value that perl preceives as true), the subdirectory
173will be created if it doesn't already exist. This happens before BLOCK
174is executed.
175
176=item B<cleanup =E<gt> 0|1>
177
178When set to 1 (or any value that perl preceives as true), the subdirectory
179will be cleaned out and removed. This happens both before and after BLOCK
180is executed.
181
182=back
183
184An example:
185
186 indir "foo" => sub {
187 ok(run(app(["openssl", "version"]), stdout => "foo.txt"));
188 if (ok(open(RESULT, "foo.txt"), "reading foo.txt")) {
189 my $line = <RESULT>;
190 close RESULT;
191 is($line, qr/^OpenSSL 1\./,
192 "check that we're using OpenSSL 1.x.x");
193 }
194 }, create => 1, cleanup => 1;
195
196=back
197
198=cut
199
aec27d4d
RL
200sub indir {
201 my $subdir = shift;
202 my $codeblock = shift;
203 my %opts = @_;
204
205 my $reverse = __cwd($subdir,%opts);
206 BAIL_OUT("FAILURE: indir, \"$subdir\" wasn't possible to move into")
207 unless $reverse;
208
209 $codeblock->();
210
211 __cwd($reverse);
212
213 if ($opts{cleanup}) {
4500a4cd 214 rmtree($subdir, { safe => 0 });
aec27d4d
RL
215 }
216}
217
f5098edb 218=over 4
aec27d4d 219
f5098edb 220=item B<app ARRAYREF, OPTS>
aec27d4d 221
f5098edb 222=item B<test ARRAYREF, OPTS>
aec27d4d 223
f5098edb
RL
224Both of these functions take a reference to a list that is a command and
225its arguments, and some additional options (described further on).
aec27d4d 226
f5098edb 227C<app> expects to find the given command (the first item in the given list
42e0ccdf
RL
228reference) as an executable in C<$BIN_D> (if defined, otherwise C<$TOP/apps>
229or C<$BLDTOP/apps>).
aec27d4d 230
f5098edb 231C<test> expects to find the given command (the first item in the given list
42e0ccdf
RL
232reference) as an executable in C<$TEST_D> (if defined, otherwise C<$TOP/test>
233or C<$BLDTOP/test>).
aec27d4d 234
f5098edb 235Both return a CODEREF to be used by C<run>, C<pipe> or C<cmdstr>.
aec27d4d 236
f5098edb
RL
237The options that both C<app> and C<test> can take are in the form of hash
238values:
aec27d4d 239
f5098edb 240=over 4
aec27d4d 241
f5098edb 242=item B<stdin =E<gt> PATH>
aec27d4d 243
f5098edb 244=item B<stdout =E<gt> PATH>
aec27d4d 245
f5098edb 246=item B<stderr =E<gt> PATH>
aec27d4d 247
f5098edb
RL
248In all three cases, the corresponding standard input, output or error is
249redirected from (for stdin) or to (for the others) a file given by the
250string PATH, I<or>, if the value is C<undef>, C</dev/null> or similar.
aec27d4d 251
f5098edb 252=back
aec27d4d 253
a00c84f6
RL
254=item B<perlapp ARRAYREF, OPTS>
255
256=item B<perltest ARRAYREF, OPTS>
257
258Both these functions function the same way as B<app> and B<test>, except
b8fcd4f0
RL
259that they expect the command to be a perl script. Also, they support one
260more option:
261
262=over 4
263
264=item B<interpreter_args =E<gt> ARRAYref>
265
266The array reference is a set of arguments for perl rather than the script.
267Take care so that none of them can be seen as a script! Flags and their
268eventual arguments only!
269
270=back
271
272An example:
273
274 ok(run(perlapp(["foo.pl", "arg1"],
275 interpreter_args => [ "-I", srctop_dir("test") ])));
a00c84f6 276
f5098edb 277=back
aec27d4d 278
f5098edb 279=cut
aec27d4d
RL
280
281sub app {
282 my $cmd = shift;
283 my %opts = @_;
284 return sub { my $num = shift;
285 return __build_cmd($num, \&__apps_file, $cmd, %opts); }
286}
287
288sub test {
289 my $cmd = shift;
290 my %opts = @_;
291 return sub { my $num = shift;
292 return __build_cmd($num, \&__test_file, $cmd, %opts); }
293}
294
a00c84f6
RL
295sub perlapp {
296 my $cmd = shift;
297 my %opts = @_;
298 return sub { my $num = shift;
299 return __build_cmd($num, \&__perlapps_file, $cmd, %opts); }
300}
301
302sub perltest {
303 my $cmd = shift;
304 my %opts = @_;
305 return sub { my $num = shift;
306 return __build_cmd($num, \&__perltest_file, $cmd, %opts); }
307}
308
f5098edb 309=over 4
aec27d4d 310
f5098edb
RL
311=item B<run CODEREF, OPTS>
312
313This CODEREF is expected to be the value return by C<app> or C<test>,
314anything else will most likely cause an error unless you know what you're
315doing.
316
317C<run> executes the command returned by CODEREF and return either the
318resulting output (if the option C<capture> is set true) or a boolean indicating
319if the command succeeded or not.
320
321The options that C<run> can take are in the form of hash values:
322
323=over 4
324
325=item B<capture =E<gt> 0|1>
326
327If true, the command will be executed with a perl backtick, and C<run> will
328return the resulting output as an array of lines. If false or not given,
329the command will be executed with C<system()>, and C<run> will return 1 if
330the command was successful or 0 if it wasn't.
331
332=back
333
334For further discussion on what is considered a successful command or not, see
335the function C<with> further down.
336
337=back
338
339=cut
aec27d4d
RL
340
341sub run {
b843cdb1 342 my ($cmd, $display_cmd) = shift->(0);
aec27d4d
RL
343 my %opts = @_;
344
345 return () if !$cmd;
346
347 my $prefix = "";
348 if ( $^O eq "VMS" ) { # VMS
349 $prefix = "pipe ";
aec27d4d
RL
350 }
351
352 my @r = ();
353 my $r = 0;
354 my $e = 0;
2ef157af 355
78e91586
RL
356 # In non-verbose, we want to shut up the command interpreter, in case
357 # it has something to complain about. On VMS, it might complain both
358 # on stdout and stderr
359 *save_STDOUT = *STDOUT;
360 *save_STDERR = *STDERR;
361 if ($ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_VERBOSE}) {
362 open STDOUT, ">", devnull();
363 open STDERR, ">", devnull();
364 }
365
2ef157af
RL
366 # The dance we do with $? is the same dance the Unix shells appear to
367 # do. For example, a program that gets aborted (and therefore signals
368 # SIGABRT = 6) will appear to exit with the code 134. We mimic this
369 # to make it easier to compare with a manual run of the command.
aec27d4d
RL
370 if ($opts{capture}) {
371 @r = `$prefix$cmd`;
2ef157af 372 $e = ($? & 0x7f) ? ($? & 0x7f)|0x80 : ($? >> 8);
aec27d4d
RL
373 } else {
374 system("$prefix$cmd");
2ef157af 375 $e = ($? & 0x7f) ? ($? & 0x7f)|0x80 : ($? >> 8);
aec27d4d
RL
376 $r = $hooks{exit_checker}->($e);
377 }
378
78e91586
RL
379 if ($ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_VERBOSE}) {
380 close STDOUT;
381 close STDERR;
382 }
383 *STDOUT = *save_STDOUT;
384 *STDERR = *save_STDERR;
385
349232d1 386 print STDERR "$prefix$display_cmd => $e\n"
3eefcea1
RL
387 if !$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE};
388
aec27d4d
RL
389 # At this point, $? stops being interesting, and unfortunately,
390 # there are Test::More versions that get picky if we leave it
391 # non-zero.
392 $? = 0;
393
aec27d4d
RL
394 if ($opts{capture}) {
395 return @r;
396 } else {
397 return $r;
398 }
399}
400
f5098edb
RL
401END {
402 my $tb = Test::More->builder;
403 my $failure = scalar(grep { $_ == 0; } $tb->summary);
404 if ($failure && $end_with_bailout) {
405 BAIL_OUT("Stoptest!");
406 }
407}
408
409=head2 Utility functions
410
411The following functions are exported on request when using C<OpenSSL::Test>.
412
42e0ccdf
RL
413 # To only get the bldtop_file and srctop_file functions.
414 use OpenSSL::Test qw/bldtop_file srctop_file/;
f5098edb 415
42e0ccdf
RL
416 # To only get the bldtop_file function in addition to the default ones.
417 use OpenSSL::Test qw/:DEFAULT bldtop_file/;
f5098edb
RL
418
419=cut
420
421# Utility functions, exported on request
422
423=over 4
424
42e0ccdf 425=item B<bldtop_dir LIST>
f5098edb
RL
426
427LIST is a list of directories that make up a path from the top of the OpenSSL
42e0ccdf
RL
428build directory (as indicated by the environment variable C<$TOP> or
429C<$BLDTOP>).
430C<bldtop_dir> returns the resulting directory as a string, adapted to the local
f5098edb
RL
431operating system.
432
433=back
434
435=cut
436
42e0ccdf
RL
437sub bldtop_dir {
438 return __bldtop_dir(@_); # This caters for operating systems that have
f5098edb
RL
439 # a very distinct syntax for directories.
440}
441
442=over 4
443
42e0ccdf 444=item B<bldtop_file LIST, FILENAME>
f5098edb
RL
445
446LIST is a list of directories that make up a path from the top of the OpenSSL
42e0ccdf
RL
447build directory (as indicated by the environment variable C<$TOP> or
448C<$BLDTOP>) and FILENAME is the name of a file located in that directory path.
449C<bldtop_file> returns the resulting file path as a string, adapted to the local
f5098edb
RL
450operating system.
451
452=back
453
454=cut
455
42e0ccdf
RL
456sub bldtop_file {
457 return __bldtop_file(@_);
458}
459
460=over 4
461
462=item B<srctop_dir LIST>
463
464LIST is a list of directories that make up a path from the top of the OpenSSL
465source directory (as indicated by the environment variable C<$TOP> or
466C<$SRCTOP>).
467C<srctop_dir> returns the resulting directory as a string, adapted to the local
468operating system.
469
470=back
471
472=cut
473
474sub srctop_dir {
475 return __srctop_dir(@_); # This caters for operating systems that have
476 # a very distinct syntax for directories.
477}
478
479=over 4
480
481=item B<srctop_file LIST, FILENAME>
482
483LIST is a list of directories that make up a path from the top of the OpenSSL
484source directory (as indicated by the environment variable C<$TOP> or
485C<$SRCTOP>) and FILENAME is the name of a file located in that directory path.
486C<srctop_file> returns the resulting file path as a string, adapted to the local
487operating system.
488
489=back
490
491=cut
492
493sub srctop_file {
494 return __srctop_file(@_);
f5098edb
RL
495}
496
497=over 4
498
499=item B<pipe LIST>
500
501LIST is a list of CODEREFs returned by C<app> or C<test>, from which C<pipe>
502creates a new command composed of all the given commands put together in a
503pipe. C<pipe> returns a new CODEREF in the same manner as C<app> or C<test>,
504to be passed to C<run> for execution.
505
506=back
507
508=cut
509
aec27d4d
RL
510sub pipe {
511 my @cmds = @_;
512 return
513 sub {
514 my @cs = ();
515 my @dcs = ();
516 my @els = ();
517 my $counter = 0;
518 foreach (@cmds) {
519 my ($c, $dc, @el) = $_->(++$counter);
520
521 return () if !$c;
522
523 push @cs, $c;
524 push @dcs, $dc;
525 push @els, @el;
526 }
527 return (
528 join(" | ", @cs),
529 join(" | ", @dcs),
530 @els
531 );
532 };
533}
534
f5098edb
RL
535=over 4
536
537=item B<with HASHREF, CODEREF>
538
539C<with> will temporarly install hooks given by the HASHREF and then execute
540the given CODEREF. Hooks are usually expected to have a coderef as value.
541
542The currently available hoosk are:
543
544=over 4
545
546=item B<exit_checker =E<gt> CODEREF>
547
548This hook is executed after C<run> has performed its given command. The
549CODEREF receives the exit code as only argument and is expected to return
5501 (if the exit code indicated success) or 0 (if the exit code indicated
551failure).
552
553=back
554
555=back
556
557=cut
558
559sub with {
560 my $opts = shift;
561 my %opts = %{$opts};
562 my $codeblock = shift;
563
564 my %saved_hooks = ();
565
566 foreach (keys %opts) {
567 $saved_hooks{$_} = $hooks{$_} if exists($hooks{$_});
568 $hooks{$_} = $opts{$_};
569 }
570
571 $codeblock->();
572
573 foreach (keys %saved_hooks) {
574 $hooks{$_} = $saved_hooks{$_};
575 }
576}
577
578=over 4
579
cb2ceb18 580=item B<cmdstr CODEREF, OPTS>
f5098edb
RL
581
582C<cmdstr> takes a CODEREF from C<app> or C<test> and simply returns the
583command as a string.
584
cb2ceb18
RL
585C<cmdstr> takes some additiona options OPTS that affect the string returned:
586
587=over 4
588
589=item B<display =E<gt> 0|1>
590
591When set to 0, the returned string will be with all decorations, such as a
592possible redirect of stderr to the null device. This is suitable if the
593string is to be used directly in a recipe.
594
595When set to 1, the returned string will be without extra decorations. This
596is suitable for display if that is desired (doesn't confuse people with all
597internal stuff), or if it's used to pass a command down to a subprocess.
598
599Default: 0
600
601=back
602
f5098edb
RL
603=back
604
605=cut
606
607sub cmdstr {
b843cdb1 608 my ($cmd, $display_cmd) = shift->(0);
cb2ceb18 609 my %opts = @_;
f5098edb 610
cb2ceb18
RL
611 if ($opts{display}) {
612 return $display_cmd;
613 } else {
614 return $cmd;
615 }
f5098edb
RL
616}
617
618=over 4
619
620=item B<quotify LIST>
621
622LIST is a list of strings that are going to be used as arguments for a
623command, and makes sure to inject quotes and escapes as necessary depending
624on the content of each string.
625
626This can also be used to put quotes around the executable of a command.
627I<This must never ever be done on VMS.>
628
629=back
630
631=cut
aec27d4d
RL
632
633sub quotify {
634 # Unix setup (default if nothing else is mentioned)
635 my $arg_formatter =
636 sub { $_ = shift; /\s|[\{\}\\\$\[\]\*\?\|\&:;<>]/ ? "'$_'" : $_ };
637
638 if ( $^O eq "VMS") { # VMS setup
639 $arg_formatter = sub {
640 $_ = shift;
641 if (/\s|["[:upper:]]/) {
642 s/"/""/g;
643 '"'.$_.'"';
644 } else {
645 $_;
646 }
647 };
648 } elsif ( $^O eq "MSWin32") { # MSWin setup
649 $arg_formatter = sub {
650 $_ = shift;
651 if (/\s|["\|\&\*\;<>]/) {
652 s/(["\\])/\\$1/g;
653 '"'.$_.'"';
654 } else {
655 $_;
656 }
657 };
658 }
659
660 return map { $arg_formatter->($_) } @_;
661}
662
f5098edb
RL
663######################################################################
664# private functions. These are never exported.
665
666=head1 ENVIRONMENT
667
668OpenSSL::Test depends on some environment variables.
669
670=over 4
671
672=item B<TOP>
673
674This environment variable is mandatory. C<setup> will check that it's
675defined and that it's a directory that contains the file C<Configure>.
676If this isn't so, C<setup> will C<BAIL_OUT>.
677
678=item B<BIN_D>
679
680If defined, its value should be the directory where the openssl application
681is located. Defaults to C<$TOP/apps> (adapted to the operating system).
682
683=item B<TEST_D>
684
685If defined, its value should be the directory where the test applications
686are located. Defaults to C<$TOP/test> (adapted to the operating system).
687
f5098edb
RL
688=item B<STOPTEST>
689
690If defined, it puts testing in a different mode, where a recipe with
691failures will result in a C<BAIL_OUT> at the end of its run.
692
693=back
694
695=cut
696
697sub __env {
42e0ccdf
RL
698 $directories{SRCTOP} = $ENV{SRCTOP} || $ENV{TOP};
699 $directories{BLDTOP} = $ENV{BLDTOP} || $ENV{TOP};
fbd361ea
RL
700 $directories{BLDAPPS} = $ENV{BIN_D} || __bldtop_dir("apps");
701 $directories{SRCAPPS} = __srctop_dir("apps");
702 $directories{BLDTEST} = $ENV{TEST_D} || __bldtop_dir("test");
703 $directories{SRCTEST} = __srctop_dir("test");
704 $directories{RESULTS} = $ENV{RESULT_D} || $directories{BLDTEST};
f5098edb 705
d1094383
RL
706 push @direnv, "TOP" if $ENV{TOP};
707 push @direnv, "SRCTOP" if $ENV{SRCTOP};
708 push @direnv, "BLDTOP" if $ENV{BLDTOP};
709 push @direnv, "BIN_D" if $ENV{BIN_D};
710 push @direnv, "TEST_D" if $ENV{TEST_D};
711 push @direnv, "RESULT_D" if $ENV{RESULT_D};
712
f5098edb
RL
713 $end_with_bailout = $ENV{STOPTEST} ? 1 : 0;
714};
715
42e0ccdf
RL
716sub __srctop_file {
717 BAIL_OUT("Must run setup() first") if (! $test_name);
718
719 my $f = pop;
720 return catfile($directories{SRCTOP},@_,$f);
721}
722
723sub __srctop_dir {
724 BAIL_OUT("Must run setup() first") if (! $test_name);
725
726 return catdir($directories{SRCTOP},@_);
727}
728
729sub __bldtop_file {
f5098edb
RL
730 BAIL_OUT("Must run setup() first") if (! $test_name);
731
732 my $f = pop;
42e0ccdf 733 return catfile($directories{BLDTOP},@_,$f);
f5098edb
RL
734}
735
42e0ccdf 736sub __bldtop_dir {
4ada8be2
AP
737 BAIL_OUT("Must run setup() first") if (! $test_name);
738
42e0ccdf 739 return catdir($directories{BLDTOP},@_);
4ada8be2
AP
740}
741
d8a52304
RL
742sub __exeext {
743 my $ext = "";
744 if ($^O eq "VMS" ) { # VMS
745 $ext = ".exe";
746 } elsif ($^O eq "MSWin32") { # Windows
747 $ext = ".exe";
748 }
749 return $ENV{"EXE_EXT"} || $ext;
750}
751
f5098edb
RL
752sub __test_file {
753 BAIL_OUT("Must run setup() first") if (! $test_name);
754
3732f12c
AP
755 my $f = pop;
756 $f = catfile($directories{BLDTEST},@_,$f . __exeext());
fbd361ea
RL
757 $f = catfile($directories{SRCTEST},@_,$f) unless -x $f;
758 return $f;
f5098edb
RL
759}
760
a00c84f6
RL
761sub __perltest_file {
762 BAIL_OUT("Must run setup() first") if (! $test_name);
763
764 my $f = pop;
fbd361ea
RL
765 $f = catfile($directories{BLDTEST},@_,$f);
766 $f = catfile($directories{SRCTEST},@_,$f) unless -f $f;
767 return ($^X, $f);
a00c84f6
RL
768}
769
f5098edb
RL
770sub __apps_file {
771 BAIL_OUT("Must run setup() first") if (! $test_name);
772
3732f12c
AP
773 my $f = pop;
774 $f = catfile($directories{BLDAPPS},@_,$f . __exeext());
fbd361ea
RL
775 $f = catfile($directories{SRCAPPS},@_,$f) unless -x $f;
776 return $f;
f5098edb
RL
777}
778
a00c84f6
RL
779sub __perlapps_file {
780 BAIL_OUT("Must run setup() first") if (! $test_name);
781
782 my $f = pop;
fbd361ea
RL
783 $f = catfile($directories{BLDAPPS},@_,$f);
784 $f = catfile($directories{SRCAPPS},@_,$f) unless -f $f;
785 return ($^X, $f);
a00c84f6
RL
786}
787
f5098edb
RL
788sub __results_file {
789 BAIL_OUT("Must run setup() first") if (! $test_name);
790
791 my $f = pop;
792 return catfile($directories{RESULTS},@_,$f);
793}
794
f5098edb 795sub __cwd {
11b3313c 796 my $dir = catdir(shift);
f5098edb
RL
797 my %opts = @_;
798 my $abscurdir = rel2abs(curdir());
799 my $absdir = rel2abs($dir);
800 my $reverse = abs2rel($abscurdir, $absdir);
801
802 # PARANOIA: if we're not moving anywhere, we do nothing more
803 if ($abscurdir eq $absdir) {
804 return $reverse;
805 }
806
807 # Do not support a move to a different volume for now. Maybe later.
808 BAIL_OUT("FAILURE: \"$dir\" moves to a different volume, not supported")
809 if $reverse eq $abscurdir;
810
811 # If someone happened to give a directory that leads back to the current,
812 # it's extremely silly to do anything more, so just simulate that we did
813 # move.
814 # In this case, we won't even clean it out, for safety's sake.
815 return "." if $reverse eq "";
816
817 $dir = canonpath($dir);
818 if ($opts{create}) {
819 mkpath($dir);
820 }
821
822 # Should we just bail out here as well? I'm unsure.
823 return undef unless chdir($dir);
824
825 if ($opts{cleanup}) {
4500a4cd 826 rmtree(".", { safe => 0, keep_root => 1 });
f5098edb
RL
827 }
828
829 # For each of these directory variables, figure out where they are relative
830 # to the directory we want to move to if they aren't absolute (if they are,
831 # they don't change!)
42e0ccdf 832 my @dirtags = sort keys %directories;
f5098edb
RL
833 foreach (@dirtags) {
834 if (!file_name_is_absolute($directories{$_})) {
835 my $newpath = abs2rel(rel2abs($directories{$_}), rel2abs($dir));
836 $directories{$_} = $newpath;
837 }
838 }
839
d1094383
RL
840 # Treat each environment variable that was used to get us the values in
841 # %directories the same was as the paths in %directories, so any sub
842 # process can use their values properly as well
843 foreach (@direnv) {
844 if (!file_name_is_absolute($ENV{$_})) {
845 my $newpath = abs2rel(rel2abs($ENV{$_}), rel2abs($dir));
846 $ENV{$_} = $newpath;
847 }
848 }
849
a00c84f6 850 if ($debug) {
f5098edb 851 print STDERR "DEBUG: __cwd(), directories and files:\n";
fbd361ea
RL
852 print STDERR " \$directories{BLDTEST} = \"$directories{BLDTEST}\"\n";
853 print STDERR " \$directories{SRCTEST} = \"$directories{SRCTEST}\"\n";
f5098edb 854 print STDERR " \$directories{RESULTS} = \"$directories{RESULTS}\"\n";
fbd361ea
RL
855 print STDERR " \$directories{BLDAPPS} = \"$directories{BLDAPPS}\"\n";
856 print STDERR " \$directories{SRCAPPS} = \"$directories{SRCAPPS}\"\n";
42e0ccdf
RL
857 print STDERR " \$directories{SRCTOP} = \"$directories{SRCTOP}\"\n";
858 print STDERR " \$directories{BLDTOP} = \"$directories{BLDTOP}\"\n";
f5098edb
RL
859 print STDERR "\n";
860 print STDERR " current directory is \"",curdir(),"\"\n";
861 print STDERR " the way back is \"$reverse\"\n";
862 }
863
864 return $reverse;
865}
866
867sub __fixup_cmd {
868 my $prog = shift;
ec307bcc 869 my $exe_shell = shift;
f5098edb 870
42e0ccdf 871 my $prefix = __bldtop_file("util", "shlib_wrap.sh")." ";
f5098edb 872
ec307bcc
RL
873 if (defined($exe_shell)) {
874 $prefix = "$exe_shell ";
4ada8be2 875 } elsif ($^O eq "VMS" ) { # VMS
c10d1bc8 876 $prefix = ($prog =~ /^(?:[\$a-z0-9_]+:)?[<\[]/i ? "mcr " : "mcr []");
f5098edb
RL
877 } elsif ($^O eq "MSWin32") { # Windows
878 $prefix = "";
f5098edb
RL
879 }
880
881 # We test both with and without extension. The reason
a00c84f6
RL
882 # is that we might be passed a complete file spec, with
883 # extension.
884 if ( ! -x $prog ) {
d8a52304 885 my $prog = "$prog";
a00c84f6
RL
886 if ( ! -x $prog ) {
887 $prog = undef;
888 }
889 }
890
891 if (defined($prog)) {
892 # Make sure to quotify the program file on platforms that may
893 # have spaces or similar in their path name.
894 # To our knowledge, VMS is the exception where quotifying should
895 # never happem.
896 ($prog) = quotify($prog) unless $^O eq "VMS";
897 return $prefix.$prog;
f5098edb
RL
898 }
899
900 print STDERR "$prog not found\n";
901 return undef;
902}
903
904sub __build_cmd {
905 BAIL_OUT("Must run setup() first") if (! $test_name);
906
907 my $num = shift;
908 my $path_builder = shift;
e3ff0892
RL
909 # Make a copy to not destroy the caller's array
910 my @cmdarray = ( @{$_[0]} ); shift;
b8fcd4f0 911 my %opts = @_;
a00c84f6
RL
912
913 # We do a little dance, as $path_builder might return a list of
914 # more than one. If so, only the first is to be considered a
915 # program to fix up, the rest is part of the arguments. This
916 # happens for perl scripts, where $path_builder will return
ec307bcc
RL
917 # a list of two, $^X and the script name.
918 # Also, if $path_builder returned more than one, we don't apply
919 # the EXE_SHELL environment variable.
a00c84f6 920 my @prog = ($path_builder->(shift @cmdarray));
ec307bcc
RL
921 my $first = shift @prog;
922 my $exe_shell = @prog ? undef : $ENV{EXE_SHELL};
923 my $cmd = __fixup_cmd($first, $exe_shell);
a00c84f6
RL
924 if (@prog) {
925 if ( ! -f $prog[0] ) {
926 print STDERR "$prog[0] not found\n";
927 $cmd = undef;
928 }
929 }
930 my @args = (@prog, @cmdarray);
b8fcd4f0
RL
931 if (defined($opts{interpreter_args})) {
932 unshift @args, @{$opts{interpreter_args}};
933 }
f5098edb
RL
934
935 return () if !$cmd;
936
937 my $arg_str = "";
938 my $null = devnull();
939
940
941 $arg_str = " ".join(" ", quotify @args) if @args;
942
943 my $fileornull = sub { $_[0] ? $_[0] : $null; };
944 my $stdin = "";
945 my $stdout = "";
946 my $stderr = "";
947 my $saved_stderr = undef;
948 $stdin = " < ".$fileornull->($opts{stdin}) if exists($opts{stdin});
949 $stdout= " > ".$fileornull->($opts{stdout}) if exists($opts{stdout});
950 $stderr=" 2> ".$fileornull->($opts{stderr}) if exists($opts{stderr});
951
f5098edb 952 my $display_cmd = "$cmd$arg_str$stdin$stdout$stderr";
b843cdb1
RL
953
954 $stderr=" 2> ".$null
955 unless $stderr || !$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE};
956
957 $cmd .= "$arg_str$stdin$stdout$stderr";
f5098edb 958
a00c84f6
RL
959 if ($debug) {
960 print STDERR "DEBUG[__build_cmd]: \$cmd = \"$cmd\"\n";
961 print STDERR "DEBUG[__build_cmd]: \$display_cmd = \"$display_cmd\"\n";
962 }
963
b843cdb1 964 return ($cmd, $display_cmd);
f5098edb
RL
965}
966
967=head1 SEE ALSO
968
969L<Test::More>, L<Test::Harness>
970
971=head1 AUTHORS
972
973Richard Levitte E<lt>levitte@openssl.orgE<gt> with assitance and
974inspiration from Andy Polyakov E<lt>appro@openssl.org<gt>.
975
976=cut
977
aec27d4d 9781;