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