bb39854a4d8302ad974641404194f27ec5f451d5
[openssl.git] / util / perl / OpenSSL / Test.pm
1 # Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
2 #
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
7
8 package OpenSSL::Test;
9
10 use strict;
11 use warnings;
12
13 use Test::More 0.96;
14
15 use Exporter;
16 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
17 $VERSION = "1.0";
18 @ISA = qw(Exporter);
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
23                                          data_file data_dir
24                                          pipe with cmdstr quotify
25                                          openssl_versions
26                                          ok_nofips is_nofips isnt_nofips));
27
28 =head1 NAME
29
30 OpenSSL::Test - a private extension of Test::More
31
32 =head1 SYNOPSIS
33
34   use OpenSSL::Test;
35
36   setup("my_test_name");
37
38   ok(run(app(["openssl", "version"])), "check for openssl presence");
39
40   indir "subdir" => sub {
41     ok(run(test(["sometest", "arg1"], stdout => "foo.txt")),
42        "run sometest with output to foo.txt");
43   };
44
45 =head1 DESCRIPTION
46
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.
51
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.
55
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/>.
60
61 =cut
62
63 use File::Copy;
64 use File::Spec::Functions qw/file_name_is_absolute curdir canonpath splitdir
65                              catdir catfile splitpath catpath devnull abs2rel
66                              rel2abs/;
67 use File::Path 2.00 qw/rmtree mkpath/;
68 use File::Basename;
69 use Cwd qw/getcwd abs_path/;
70
71 my $level = 0;
72
73 # The name of the test.  This is set by setup() and is used in the other
74 # functions to verify that setup() has been used.
75 my $test_name = undef;
76
77 # Directories we want to keep track of TOP, APPS, TEST and RESULTS are the
78 # ones we're interested in, corresponding to the environment variables TOP
79 # (mandatory), BIN_D, TEST_D, UTIL_D and RESULT_D.
80 my %directories = ();
81
82 # The environment variables that gave us the contents in %directories.  These
83 # get modified whenever we change directories, so that subprocesses can use
84 # the values of those environment variables as well
85 my @direnv = ();
86
87 # A bool saying if we shall stop all testing if the current recipe has failing
88 # tests or not.  This is set by setup() if the environment variable STOPTEST
89 # is defined with a non-empty value.
90 my $end_with_bailout = 0;
91
92 # A set of hooks that is affected by with() and may be used in diverse places.
93 # All hooks are expected to be CODE references.
94 my %hooks = (
95
96     # exit_checker is used by run() directly after completion of a command.
97     # it receives the exit code from that command and is expected to return
98     # 1 (for success) or 0 (for failure).  This is the status value that run()
99     # will give back (through the |statusvar| reference and as returned value
100     # when capture => 1 doesn't apply).
101     exit_checker => sub { return shift == 0 ? 1 : 0 },
102
103     );
104
105 # Debug flag, to be set manually when needed
106 my $debug = 0;
107
108 =head2 Main functions
109
110 The following functions are exported by default when using C<OpenSSL::Test>.
111
112 =cut
113
114 =over 4
115
116 =item B<setup "NAME">
117
118 C<setup> is used for initial setup, and it is mandatory that it's used.
119 If it's not used in a OpenSSL test recipe, the rest of the recipe will
120 most likely refuse to run.
121
122 C<setup> checks for environment variables (see L</ENVIRONMENT> below),
123 checks that C<$TOP/Configure> or C<$SRCTOP/Configure> exists, C<chdir>
124 into the results directory (defined by the C<$RESULT_D> environment
125 variable if defined, otherwise C<$BLDTOP/test> or C<$TOP/test>, whichever
126 is defined).
127
128 =back
129
130 =cut
131
132 sub setup {
133     my $old_test_name = $test_name;
134     $test_name = shift;
135     my %opts = @_;
136
137     BAIL_OUT("setup() must receive a name") unless $test_name;
138     warn "setup() detected test name change.  Innocuous, so we continue...\n"
139         if $old_test_name && $old_test_name ne $test_name;
140
141     return if $old_test_name;
142
143     BAIL_OUT("setup() needs \$TOP or \$SRCTOP and \$BLDTOP to be defined")
144         unless $ENV{TOP} || ($ENV{SRCTOP} && $ENV{BLDTOP});
145     BAIL_OUT("setup() found both \$TOP and \$SRCTOP or \$BLDTOP...")
146         if $ENV{TOP} && ($ENV{SRCTOP} || $ENV{BLDTOP});
147
148     __env();
149
150     BAIL_OUT("setup() expects the file Configure in the source top directory")
151         unless -f srctop_file("Configure");
152
153     note "The results of this test will end up in $directories{RESULTS}"
154         unless $opts{quiet};
155
156     __cwd($directories{RESULTS});
157 }
158
159 =over 4
160
161 =item B<indir "SUBDIR" =E<gt> sub BLOCK, OPTS>
162
163 C<indir> is used to run a part of the recipe in a different directory than
164 the one C<setup> moved into, usually a subdirectory, given by SUBDIR.
165 The part of the recipe that's run there is given by the codeblock BLOCK.
166
167 C<indir> takes some additional options OPTS that affect the subdirectory:
168
169 =over 4
170
171 =item B<create =E<gt> 0|1>
172
173 When set to 1 (or any value that perl perceives as true), the subdirectory
174 will be created if it doesn't already exist.  This happens before BLOCK
175 is executed.
176
177 =back
178
179 An example:
180
181   indir "foo" => sub {
182       ok(run(app(["openssl", "version"]), stdout => "foo.txt"));
183       if (ok(open(RESULT, "foo.txt"), "reading foo.txt")) {
184           my $line = <RESULT>;
185           close RESULT;
186           is($line, qr/^OpenSSL 1\./,
187              "check that we're using OpenSSL 1.x.x");
188       }
189   }, create => 1;
190
191 =back
192
193 =cut
194
195 sub indir {
196     my $subdir = shift;
197     my $codeblock = shift;
198     my %opts = @_;
199
200     my $reverse = __cwd($subdir,%opts);
201     BAIL_OUT("FAILURE: indir, \"$subdir\" wasn't possible to move into")
202         unless $reverse;
203
204     $codeblock->();
205
206     __cwd($reverse);
207 }
208
209 =over 4
210
211 =item B<cmd ARRAYREF, OPTS>
212
213 This functions build up a platform dependent command based on the
214 input.  It takes a reference to a list that is the executable or
215 script and its arguments, and some additional options (described
216 further on).  Where necessary, the command will be wrapped in a
217 suitable environment to make sure the correct shared libraries are
218 used (currently only on Unix).
219
220 It returns a CODEREF to be used by C<run>, C<pipe> or C<cmdstr>.
221
222 The options that C<cmd> can take are in the form of hash values:
223
224 =over 4
225
226 =item B<stdin =E<gt> PATH>
227
228 =item B<stdout =E<gt> PATH>
229
230 =item B<stderr =E<gt> PATH>
231
232 In all three cases, the corresponding standard input, output or error is
233 redirected from (for stdin) or to (for the others) a file given by the
234 string PATH, I<or>, if the value is C<undef>, C</dev/null> or similar.
235
236 =back
237
238 =item B<app ARRAYREF, OPTS>
239
240 =item B<test ARRAYREF, OPTS>
241
242 Both of these are specific applications of C<cmd>, with just a couple
243 of small difference:
244
245 C<app> expects to find the given command (the first item in the given list
246 reference) as an executable in C<$BIN_D> (if defined, otherwise C<$TOP/apps>
247 or C<$BLDTOP/apps>).
248
249 C<test> expects to find the given command (the first item in the given list
250 reference) as an executable in C<$TEST_D> (if defined, otherwise C<$TOP/test>
251 or C<$BLDTOP/test>).
252
253 Also, for both C<app> and C<test>, the command may be prefixed with
254 the content of the environment variable C<$EXE_SHELL>, which is useful
255 in case OpenSSL has been cross compiled.
256
257 =item B<perlapp ARRAYREF, OPTS>
258
259 =item B<perltest ARRAYREF, OPTS>
260
261 These are also specific applications of C<cmd>, where the interpreter
262 is predefined to be C<perl>, and they expect the script to be
263 interpreted to reside in the same location as C<app> and C<test>.
264
265 C<perlapp> and C<perltest> will also take the following option:
266
267 =over 4
268
269 =item B<interpreter_args =E<gt> ARRAYref>
270
271 The array reference is a set of arguments for the interpreter rather
272 than the script.  Take care so that none of them can be seen as a
273 script!  Flags and their eventual arguments only!
274
275 =back
276
277 An example:
278
279   ok(run(perlapp(["foo.pl", "arg1"],
280                  interpreter_args => [ "-I", srctop_dir("test") ])));
281
282 =back
283
284 =begin comment
285
286 One might wonder over the complexity of C<apps>, C<fuzz>, C<test>, ...
287 with all the lazy evaluations and all that.  The reason for this is that
288 we want to make sure the directory in which those programs are found are
289 correct at the time these commands are used.  Consider the following code
290 snippet:
291
292   my $cmd = app(["openssl", ...]);
293
294   indir "foo", sub {
295       ok(run($cmd), "Testing foo")
296   };
297
298 If there wasn't this lazy evaluation, the directory where C<openssl> is
299 found would be incorrect at the time C<run> is called, because it was
300 calculated before we moved into the directory "foo".
301
302 =end comment
303
304 =cut
305
306 sub cmd {
307     my $cmd = shift;
308     my %opts = @_;
309     return sub {
310         my $num = shift;
311         # Make a copy to not destroy the caller's array
312         my @cmdargs = ( @$cmd );
313         my @prog = __wrap_cmd(shift @cmdargs, $opts{exe_shell} // ());
314
315         return __decorate_cmd($num, [ @prog, quotify(@cmdargs) ],
316                               %opts);
317     }
318 }
319
320 sub app {
321     my $cmd = shift;
322     my %opts = @_;
323     return sub {
324         my @cmdargs = ( @{$cmd} );
325         my @prog = __fixup_prg(__apps_file(shift @cmdargs, __exeext()));
326         return cmd([ @prog, @cmdargs ],
327                    exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
328     }
329 }
330
331 sub fuzz {
332     my $cmd = shift;
333     my %opts = @_;
334     return sub {
335         my @cmdargs = ( @{$cmd} );
336         my @prog = __fixup_prg(__fuzz_file(shift @cmdargs, __exeext()));
337         return cmd([ @prog, @cmdargs ],
338                    exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
339     }
340 }
341
342 sub test {
343     my $cmd = shift;
344     my %opts = @_;
345     return sub {
346         my @cmdargs = ( @{$cmd} );
347         my @prog = __fixup_prg(__test_file(shift @cmdargs, __exeext()));
348         return cmd([ @prog, @cmdargs ],
349                    exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
350     }
351 }
352
353 sub perlapp {
354     my $cmd = shift;
355     my %opts = @_;
356     return sub {
357         my @interpreter_args = defined $opts{interpreter_args} ?
358             @{$opts{interpreter_args}} : ();
359         my @interpreter = __fixup_prg($^X);
360         my @cmdargs = ( @{$cmd} );
361         my @prog = __apps_file(shift @cmdargs, undef);
362         return cmd([ @interpreter, @interpreter_args,
363                      @prog, @cmdargs ], %opts) -> (shift);
364     }
365 }
366
367 sub perltest {
368     my $cmd = shift;
369     my %opts = @_;
370     return sub {
371         my @interpreter_args = defined $opts{interpreter_args} ?
372             @{$opts{interpreter_args}} : ();
373         my @interpreter = __fixup_prg($^X);
374         my @cmdargs = ( @{$cmd} );
375         my @prog = __test_file(shift @cmdargs, undef);
376         return cmd([ @interpreter, @interpreter_args,
377                      @prog, @cmdargs ], %opts) -> (shift);
378     }
379 }
380
381 =over 4
382
383 =item B<run CODEREF, OPTS>
384
385 CODEREF is expected to be the value return by C<cmd> or any of its
386 derivatives, anything else will most likely cause an error unless you
387 know what you're doing.
388
389 C<run> executes the command returned by CODEREF and return either the
390 resulting output (if the option C<capture> is set true) or a boolean
391 indicating if the command succeeded or not.
392
393 The options that C<run> can take are in the form of hash values:
394
395 =over 4
396
397 =item B<capture =E<gt> 0|1>
398
399 If true, the command will be executed with a perl backtick, and C<run> will
400 return the resulting output as an array of lines.  If false or not given,
401 the command will be executed with C<system()>, and C<run> will return 1 if
402 the command was successful or 0 if it wasn't.
403
404 =item B<prefix =E<gt> EXPR>
405
406 If specified, EXPR will be used as a string to prefix the output from the
407 command.  This is useful if the output contains lines starting with C<ok >
408 or C<not ok > that can disturb Test::Harness.
409
410 =item B<statusvar =E<gt> VARREF>
411
412 If used, B<VARREF> must be a reference to a scalar variable.  It will be
413 assigned a boolean indicating if the command succeeded or not.  This is
414 particularly useful together with B<capture>.
415
416 =back
417
418 For further discussion on what is considered a successful command or not, see
419 the function C<with> further down.
420
421 =back
422
423 =cut
424
425 sub run {
426     my ($cmd, $display_cmd) = shift->(0);
427     my %opts = @_;
428
429     return () if !$cmd;
430
431     my $prefix = "";
432     if ( $^O eq "VMS" ) {       # VMS
433         $prefix = "pipe ";
434     }
435
436     my @r = ();
437     my $r = 0;
438     my $e = 0;
439
440     die "OpenSSL::Test::run(): statusvar value not a scalar reference"
441         if $opts{statusvar} && ref($opts{statusvar}) ne "SCALAR";
442
443     # For some reason, program output, or even output from this function
444     # somehow isn't caught by TAP::Harness (TAP::Parser?) on VMS, so we're
445     # silencing it specifically there until further notice.
446     my $save_STDOUT;
447     my $save_STDERR;
448     if ($^O eq 'VMS') {
449         # In non-verbose, we want to shut up the command interpreter, in case
450         # it has something to complain about.  On VMS, it might complain both
451         # on stdout and stderr
452         if ($ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_VERBOSE}) {
453             open $save_STDOUT, '>&', \*STDOUT or die "Can't dup STDOUT: $!";
454             open $save_STDERR, '>&', \*STDERR or die "Can't dup STDERR: $!";
455             open STDOUT, ">", devnull();
456             open STDERR, ">", devnull();
457         }
458     }
459
460     $ENV{HARNESS_OSSL_LEVEL} = $level + 1;
461
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})) {
467         my $pipe;
468         local $_;
469
470         open($pipe, '-|', "$prefix$cmd") or die "Can't start command: $!";
471         while(<$pipe>) {
472             my $l = ($opts{prefix} // "") . $_;
473             if ($opts{capture}) {
474                 push @r, $l;
475             } else {
476                 print STDOUT $l;
477             }
478         }
479         close $pipe;
480     } else {
481         $ENV{HARNESS_OSSL_PREFIX} = "# ";
482         system("$prefix$cmd");
483         delete $ENV{HARNESS_OSSL_PREFIX};
484     }
485     $e = ($? & 0x7f) ? ($? & 0x7f)|0x80 : ($? >> 8);
486     $r = $hooks{exit_checker}->($e);
487     if ($opts{statusvar}) {
488         ${$opts{statusvar}} = $r;
489     }
490
491     # Restore STDOUT / STDERR on VMS
492     if ($^O eq 'VMS') {
493         if ($ENV{HARNESS_ACTIVE} && !$ENV{HARNESS_VERBOSE}) {
494             close STDOUT;
495             close STDERR;
496             open STDOUT, '>&', $save_STDOUT or die "Can't restore STDOUT: $!";
497             open STDERR, '>&', $save_STDERR or die "Can't restore STDERR: $!";
498         }
499
500         print STDERR "$prefix$display_cmd => $e\n"
501             if !$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE};
502     } else {
503         print STDERR "$prefix$display_cmd => $e\n";
504     }
505
506     # At this point, $? stops being interesting, and unfortunately,
507     # there are Test::More versions that get picky if we leave it
508     # non-zero.
509     $? = 0;
510
511     if ($opts{capture}) {
512         return @r;
513     } else {
514         return $r;
515     }
516 }
517
518 END {
519     my $tb = Test::More->builder;
520     my $failure = scalar(grep { $_ == 0; } $tb->summary);
521     if ($failure && $end_with_bailout) {
522         BAIL_OUT("Stoptest!");
523     }
524 }
525
526 =head2 Utility functions
527
528 The following functions are exported on request when using C<OpenSSL::Test>.
529
530   # To only get the bldtop_file and srctop_file functions.
531   use OpenSSL::Test qw/bldtop_file srctop_file/;
532
533   # To only get the bldtop_file function in addition to the default ones.
534   use OpenSSL::Test qw/:DEFAULT bldtop_file/;
535
536 =cut
537
538 # Utility functions, exported on request
539
540 =over 4
541
542 =item B<bldtop_dir LIST>
543
544 LIST is a list of directories that make up a path from the top of the OpenSSL
545 build directory (as indicated by the environment variable C<$TOP> or
546 C<$BLDTOP>).
547 C<bldtop_dir> returns the resulting directory as a string, adapted to the local
548 operating system.
549
550 =back
551
552 =cut
553
554 sub bldtop_dir {
555     return __bldtop_dir(@_);    # This caters for operating systems that have
556                                 # a very distinct syntax for directories.
557 }
558
559 =over 4
560
561 =item B<bldtop_file LIST, FILENAME>
562
563 LIST is a list of directories that make up a path from the top of the OpenSSL
564 build directory (as indicated by the environment variable C<$TOP> or
565 C<$BLDTOP>) and FILENAME is the name of a file located in that directory path.
566 C<bldtop_file> returns the resulting file path as a string, adapted to the local
567 operating system.
568
569 =back
570
571 =cut
572
573 sub bldtop_file {
574     return __bldtop_file(@_);
575 }
576
577 =over 4
578
579 =item B<srctop_dir LIST>
580
581 LIST is a list of directories that make up a path from the top of the OpenSSL
582 source directory (as indicated by the environment variable C<$TOP> or
583 C<$SRCTOP>).
584 C<srctop_dir> returns the resulting directory as a string, adapted to the local
585 operating system.
586
587 =back
588
589 =cut
590
591 sub srctop_dir {
592     return __srctop_dir(@_);    # This caters for operating systems that have
593                                 # a very distinct syntax for directories.
594 }
595
596 =over 4
597
598 =item B<srctop_file LIST, FILENAME>
599
600 LIST is a list of directories that make up a path from the top of the OpenSSL
601 source directory (as indicated by the environment variable C<$TOP> or
602 C<$SRCTOP>) and FILENAME is the name of a file located in that directory path.
603 C<srctop_file> returns the resulting file path as a string, adapted to the local
604 operating system.
605
606 =back
607
608 =cut
609
610 sub srctop_file {
611     return __srctop_file(@_);
612 }
613
614 =over 4
615
616 =item B<data_dir LIST>
617
618 LIST is a list of directories that make up a path from the data directory
619 associated with the test (see L</DESCRIPTION> above).
620 C<data_dir> returns the resulting directory as a string, adapted to the local
621 operating system.
622
623 =back
624
625 =cut
626
627 sub data_dir {
628     return __data_dir(@_);
629 }
630
631 =over 4
632
633 =item B<data_file LIST, FILENAME>
634
635 LIST is a list of directories that make up a path from the data directory
636 associated with the test (see L</DESCRIPTION> above) and FILENAME is the name
637 of a file located in that directory path.  C<data_file> returns the resulting
638 file path as a string, adapted to the local operating system.
639
640 =back
641
642 =cut
643
644 sub data_file {
645     return __data_file(@_);
646 }
647
648 =over 4
649
650 =item B<pipe LIST>
651
652 LIST is a list of CODEREFs returned by C<app> or C<test>, from which C<pipe>
653 creates a new command composed of all the given commands put together in a
654 pipe.  C<pipe> returns a new CODEREF in the same manner as C<app> or C<test>,
655 to be passed to C<run> for execution.
656
657 =back
658
659 =cut
660
661 sub pipe {
662     my @cmds = @_;
663     return
664         sub {
665             my @cs  = ();
666             my @dcs = ();
667             my @els = ();
668             my $counter = 0;
669             foreach (@cmds) {
670                 my ($c, $dc, @el) = $_->(++$counter);
671
672                 return () if !$c;
673
674                 push @cs, $c;
675                 push @dcs, $dc;
676                 push @els, @el;
677             }
678             return (
679                 join(" | ", @cs),
680                 join(" | ", @dcs),
681                 @els
682                 );
683     };
684 }
685
686 =over 4
687
688 =item B<with HASHREF, CODEREF>
689
690 C<with> will temporarily install hooks given by the HASHREF and then execute
691 the given CODEREF.  Hooks are usually expected to have a coderef as value.
692
693 The currently available hoosk are:
694
695 =over 4
696
697 =item B<exit_checker =E<gt> CODEREF>
698
699 This hook is executed after C<run> has performed its given command.  The
700 CODEREF receives the exit code as only argument and is expected to return
701 1 (if the exit code indicated success) or 0 (if the exit code indicated
702 failure).
703
704 =back
705
706 =back
707
708 =cut
709
710 sub with {
711     my $opts = shift;
712     my %opts = %{$opts};
713     my $codeblock = shift;
714
715     my %saved_hooks = ();
716
717     foreach (keys %opts) {
718         $saved_hooks{$_} = $hooks{$_}   if exists($hooks{$_});
719         $hooks{$_} = $opts{$_};
720     }
721
722     $codeblock->();
723
724     foreach (keys %saved_hooks) {
725         $hooks{$_} = $saved_hooks{$_};
726     }
727 }
728
729 =over 4
730
731 =item B<cmdstr CODEREF, OPTS>
732
733 C<cmdstr> takes a CODEREF from C<app> or C<test> and simply returns the
734 command as a string.
735
736 C<cmdstr> takes some additional options OPTS that affect the string returned:
737
738 =over 4
739
740 =item B<display =E<gt> 0|1>
741
742 When set to 0, the returned string will be with all decorations, such as a
743 possible redirect of stderr to the null device.  This is suitable if the
744 string is to be used directly in a recipe.
745
746 When set to 1, the returned string will be without extra decorations.  This
747 is suitable for display if that is desired (doesn't confuse people with all
748 internal stuff), or if it's used to pass a command down to a subprocess.
749
750 Default: 0
751
752 =back
753
754 =back
755
756 =cut
757
758 sub cmdstr {
759     my ($cmd, $display_cmd) = shift->(0);
760     my %opts = @_;
761
762     if ($opts{display}) {
763         return $display_cmd;
764     } else {
765         return $cmd;
766     }
767 }
768
769 =over 4
770
771 =item B<quotify LIST>
772
773 LIST is a list of strings that are going to be used as arguments for a
774 command, and makes sure to inject quotes and escapes as necessary depending
775 on the content of each string.
776
777 This can also be used to put quotes around the executable of a command.
778 I<This must never ever be done on VMS.>
779
780 =back
781
782 =cut
783
784 sub quotify {
785     # Unix setup (default if nothing else is mentioned)
786     my $arg_formatter =
787         sub { $_ = shift;
788               ($_ eq '' || /\s|[\{\}\\\$\[\]\*\?\|\&:;<>]/) ? "'$_'" : $_ };
789
790     if ( $^O eq "VMS") {        # VMS setup
791         $arg_formatter = sub {
792             $_ = shift;
793             if ($_ eq '' || /\s|["[:upper:]]/) {
794                 s/"/""/g;
795                 '"'.$_.'"';
796             } else {
797                 $_;
798             }
799         };
800     } elsif ( $^O eq "MSWin32") { # MSWin setup
801         $arg_formatter = sub {
802             $_ = shift;
803             if ($_ eq '' || /\s|["\|\&\*\;<>]/) {
804                 s/(["\\])/\\$1/g;
805                 '"'.$_.'"';
806             } else {
807                 $_;
808             }
809         };
810     }
811
812     return map { $arg_formatter->($_) } @_;
813 }
814
815 =over 4
816
817 =item B<openssl_versions>
818
819 Returns a list of two version numbers, the first representing the build
820 version, the second representing the library version.  See opensslv.h for
821 more information on those numbers.
822
823 =back
824
825 =cut
826
827 my @versions = ();
828 sub openssl_versions {
829     unless (@versions) {
830         my %lines =
831             map { s/\R$//;
832                   /^(.*): (.*)$/;
833                   $1 => $2 }
834             run(test(['versions']), capture => 1);
835         @versions = ( $lines{'Build version'}, $lines{'Library version'} );
836     }
837     return @versions;
838 }
839
840 =over 4
841
842 =item B<ok_nofips EXPR, TEST_NAME>
843
844 C<ok_nofips> is equivalent to using C<ok> when the environment variable
845 C<FIPS_MODE> is undefined, otherwise it is equivalent to C<not ok>. This can be
846 used for C<ok> tests that must fail when testing a FIPS provider. The parameters
847 are the same as used by C<ok> which is an expression EXPR followed by the test
848 description TEST_NAME.
849
850 An example:
851
852   ok_nofips(run(app(["md5.pl"])), "md5 should fail in fips mode");
853
854 =item B<is_nofips EXPR1, EXPR2, TEST_NAME>
855
856 C<is_nofips> is equivalent to using C<is> when the environment variable
857 C<FIPS_MODE> is undefined, otherwise it is equivalent to C<isnt>. This can be
858 used for C<is> tests that must fail when testing a FIPS provider. The parameters
859 are the same as used by C<is> which has 2 arguments EXPR1 and EXPR2 that can be
860 compared using eq or ne, followed by a test description TEST_NAME.
861
862 An example:
863
864   is_nofips(ultimate_answer(), 42,  "Meaning of Life");
865
866 =item B<isnt_nofips EXPR1, EXPR2, TEST_NAME>
867
868 C<isnt_nofips> is equivalent to using C<isnt> when the environment variable
869 C<FIPS_MODE> is undefined, otherwise it is equivalent to C<is>. This can be
870 used for C<isnt> tests that must fail when testing a FIPS provider. The
871 parameters are the same as used by C<isnt> which has 2 arguments EXPR1 and EXPR2
872 that can be compared using ne or eq, followed by a test description TEST_NAME.
873
874 An example:
875
876   isnt_nofips($foo, '',  "Got some foo");
877
878 =back
879
880 =cut
881
882 sub ok_nofips {
883     return ok(!$_[0], @_[1..$#_]) if defined $ENV{FIPS_MODE};
884     return ok($_[0], @_[1..$#_]);
885 }
886
887 sub is_nofips {
888     return isnt($_[0], $_[1], @_[2..$#_]) if defined $ENV{FIPS_MODE};
889     return is($_[0], $_[1], @_[2..$#_]);
890 }
891
892 sub isnt_nofips {
893     return is($_[0], $_[1], @_[2..$#_]) if defined $ENV{FIPS_MODE};
894     return isnt($_[0], $_[1], @_[2..$#_]);
895 }
896
897 ######################################################################
898 # private functions.  These are never exported.
899
900 =head1 ENVIRONMENT
901
902 OpenSSL::Test depends on some environment variables.
903
904 =over 4
905
906 =item B<TOP>
907
908 This environment variable is mandatory.  C<setup> will check that it's
909 defined and that it's a directory that contains the file C<Configure>.
910 If this isn't so, C<setup> will C<BAIL_OUT>.
911
912 =item B<BIN_D>
913
914 If defined, its value should be the directory where the openssl application
915 is located.  Defaults to C<$TOP/apps> (adapted to the operating system).
916
917 =item B<TEST_D>
918
919 If defined, its value should be the directory where the test applications
920 are located.  Defaults to C<$TOP/test> (adapted to the operating system).
921
922 =item B<STOPTEST>
923
924 If defined, it puts testing in a different mode, where a recipe with
925 failures will result in a C<BAIL_OUT> at the end of its run.
926
927 =item B<FIPS_MODE>
928
929 If defined it indicates that the FIPS provider is being tested. Tests may use
930 B<ok_nofips>, B<is_nofips> and B<isnt_nofips> to invert test results
931 i.e. Some tests may only work in non FIPS mode.
932
933 =back
934
935 =cut
936
937 sub __env {
938     (my $recipe_datadir = basename($0)) =~ s/\.t$/_data/i;
939
940     $directories{SRCTOP}    = abs_path($ENV{SRCTOP} || $ENV{TOP});
941     $directories{BLDTOP}    = abs_path($ENV{BLDTOP} || $ENV{TOP});
942     $directories{BLDAPPS}   = $ENV{BIN_D}  || __bldtop_dir("apps");
943     $directories{SRCAPPS}   =                 __srctop_dir("apps");
944     $directories{BLDFUZZ}   =                 __bldtop_dir("fuzz");
945     $directories{SRCFUZZ}   =                 __srctop_dir("fuzz");
946     $directories{BLDTEST}   = $ENV{TEST_D} || __bldtop_dir("test");
947     $directories{SRCTEST}   =                 __srctop_dir("test");
948     $directories{SRCDATA}   =                 __srctop_dir("test", "recipes",
949                                                            $recipe_datadir);
950     $directories{RESULTTOP} = $ENV{RESULT_D} || __bldtop_dir("test-runs");
951     $directories{RESULTS}   = catdir($directories{RESULTTOP}, $test_name);
952
953     # Create result directory dynamically
954     rmtree($directories{RESULTS}, { safe => 0, keep_root => 1 });
955     mkpath($directories{RESULTS});
956
957     push @direnv, "TOP"       if $ENV{TOP};
958     push @direnv, "SRCTOP"    if $ENV{SRCTOP};
959     push @direnv, "BLDTOP"    if $ENV{BLDTOP};
960     push @direnv, "BIN_D"     if $ENV{BIN_D};
961     push @direnv, "TEST_D"    if $ENV{TEST_D};
962     push @direnv, "RESULT_D"  if $ENV{RESULT_D};
963
964     $end_with_bailout = $ENV{STOPTEST} ? 1 : 0;
965 };
966
967 # __srctop_file and __srctop_dir are helpers to build file and directory
968 # names on top of the source directory.  They depend on $SRCTOP, and
969 # therefore on the proper use of setup() and when needed, indir().
970 # __bldtop_file and __bldtop_dir do the same thing but relative to $BLDTOP.
971 # __srctop_file and __bldtop_file take the same kind of argument as
972 # File::Spec::Functions::catfile.
973 # Similarly, __srctop_dir and __bldtop_dir take the same kind of argument
974 # as File::Spec::Functions::catdir
975 sub __srctop_file {
976     BAIL_OUT("Must run setup() first") if (! $test_name);
977
978     my $f = pop;
979     return abs2rel(catfile($directories{SRCTOP},@_,$f),getcwd);
980 }
981
982 sub __srctop_dir {
983     BAIL_OUT("Must run setup() first") if (! $test_name);
984
985     return abs2rel(catdir($directories{SRCTOP},@_), getcwd);
986 }
987
988 sub __bldtop_file {
989     BAIL_OUT("Must run setup() first") if (! $test_name);
990
991     my $f = pop;
992     return abs2rel(catfile($directories{BLDTOP},@_,$f), getcwd);
993 }
994
995 sub __bldtop_dir {
996     BAIL_OUT("Must run setup() first") if (! $test_name);
997
998     return abs2rel(catdir($directories{BLDTOP},@_), getcwd);
999 }
1000
1001 # __exeext is a function that returns the platform dependent file extension
1002 # for executable binaries, or the value of the environment variable $EXE_EXT
1003 # if that one is defined.
1004 sub __exeext {
1005     my $ext = "";
1006     if ($^O eq "VMS" ) {        # VMS
1007         $ext = ".exe";
1008     } elsif ($^O eq "MSWin32") { # Windows
1009         $ext = ".exe";
1010     }
1011     return $ENV{"EXE_EXT"} || $ext;
1012 }
1013
1014 # __test_file, __apps_file and __fuzz_file return the full path to a file
1015 # relative to the test/, apps/ or fuzz/ directory in the build tree or the
1016 # source tree, depending on where the file is found.  Note that when looking
1017 # in the build tree, the file name with an added extension is looked for, if
1018 # an extension is given.  The intent is to look for executable binaries (in
1019 # the build tree) or possibly scripts (in the source tree).
1020 # These functions all take the same arguments as File::Spec::Functions::catfile,
1021 # *plus* a mandatory extension argument.  This extension argument can be undef,
1022 # and is ignored in such a case.
1023 sub __test_file {
1024     BAIL_OUT("Must run setup() first") if (! $test_name);
1025
1026     my $e = pop || "";
1027     my $f = pop;
1028     my $out = catfile($directories{BLDTEST},@_,$f . $e);
1029     $out = catfile($directories{SRCTEST},@_,$f) unless -f $out;
1030     return $out;
1031 }
1032
1033 sub __apps_file {
1034     BAIL_OUT("Must run setup() first") if (! $test_name);
1035
1036     my $e = pop || "";
1037     my $f = pop;
1038     my $out = catfile($directories{BLDAPPS},@_,$f . $e);
1039     $out = catfile($directories{SRCAPPS},@_,$f) unless -f $out;
1040     return $out;
1041 }
1042
1043 sub __fuzz_file {
1044     BAIL_OUT("Must run setup() first") if (! $test_name);
1045
1046     my $e = pop || "";
1047     my $f = pop;
1048     my $out = catfile($directories{BLDFUZZ},@_,$f . $e);
1049     $out = catfile($directories{SRCFUZZ},@_,$f) unless -f $out;
1050     return $out;
1051 }
1052
1053 sub __data_file {
1054     BAIL_OUT("Must run setup() first") if (! $test_name);
1055
1056     my $f = pop;
1057     return catfile($directories{SRCDATA},@_,$f);
1058 }
1059
1060 sub __data_dir {
1061     BAIL_OUT("Must run setup() first") if (! $test_name);
1062
1063     return catdir($directories{SRCDATA},@_);
1064 }
1065
1066 sub __results_file {
1067     BAIL_OUT("Must run setup() first") if (! $test_name);
1068
1069     my $f = pop;
1070     return catfile($directories{RESULTS},@_,$f);
1071 }
1072
1073 # __cwd DIR
1074 # __cwd DIR, OPTS
1075 #
1076 # __cwd changes directory to DIR (string) and changes all the relative
1077 # entries in %directories accordingly.  OPTS is an optional series of
1078 # hash style arguments to alter __cwd's behavior:
1079 #
1080 #    create = 0|1       The directory we move to is created if 1, not if 0.
1081
1082 sub __cwd {
1083     my $dir = catdir(shift);
1084     my %opts = @_;
1085     my $abscurdir = rel2abs(curdir());
1086     my $absdir = rel2abs($dir);
1087     my $reverse = abs2rel($abscurdir, $absdir);
1088
1089     # PARANOIA: if we're not moving anywhere, we do nothing more
1090     if ($abscurdir eq $absdir) {
1091         return $reverse;
1092     }
1093
1094     # Do not support a move to a different volume for now.  Maybe later.
1095     BAIL_OUT("FAILURE: \"$dir\" moves to a different volume, not supported")
1096         if $reverse eq $abscurdir;
1097
1098     # If someone happened to give a directory that leads back to the current,
1099     # it's extremely silly to do anything more, so just simulate that we did
1100     # move.
1101     # In this case, we won't even clean it out, for safety's sake.
1102     return "." if $reverse eq "";
1103
1104     $dir = canonpath($dir);
1105     if ($opts{create}) {
1106         mkpath($dir);
1107     }
1108
1109     # We are recalculating the directories we keep track of, but need to save
1110     # away the result for after having moved into the new directory.
1111     my %tmp_directories = ();
1112     my %tmp_ENV = ();
1113
1114     # For each of these directory variables, figure out where they are relative
1115     # to the directory we want to move to if they aren't absolute (if they are,
1116     # they don't change!)
1117     my @dirtags = sort keys %directories;
1118     foreach (@dirtags) {
1119         if (!file_name_is_absolute($directories{$_})) {
1120             my $newpath = abs2rel(rel2abs($directories{$_}), rel2abs($dir));
1121             $tmp_directories{$_} = $newpath;
1122         }
1123     }
1124
1125     # Treat each environment variable that was used to get us the values in
1126     # %directories the same was as the paths in %directories, so any sub
1127     # process can use their values properly as well
1128     foreach (@direnv) {
1129         if (!file_name_is_absolute($ENV{$_})) {
1130             my $newpath = abs2rel(rel2abs($ENV{$_}), rel2abs($dir));
1131             $tmp_ENV{$_} = $newpath;
1132         }
1133     }
1134
1135     # Should we just bail out here as well?  I'm unsure.
1136     return undef unless chdir($dir);
1137
1138     # We put back new values carefully.  Doing the obvious
1139     # %directories = ( %tmp_directories )
1140     # will clear out any value that happens to be an absolute path
1141     foreach (keys %tmp_directories) {
1142         $directories{$_} = $tmp_directories{$_};
1143     }
1144     foreach (keys %tmp_ENV) {
1145         $ENV{$_} = $tmp_ENV{$_};
1146     }
1147
1148     if ($debug) {
1149         print STDERR "DEBUG: __cwd(), directories and files:\n";
1150         print STDERR "  \$directories{BLDTEST} = \"$directories{BLDTEST}\"\n";
1151         print STDERR "  \$directories{SRCTEST} = \"$directories{SRCTEST}\"\n";
1152         print STDERR "  \$directories{SRCDATA} = \"$directories{SRCDATA}\"\n";
1153         print STDERR "  \$directories{RESULTS} = \"$directories{RESULTS}\"\n";
1154         print STDERR "  \$directories{BLDAPPS} = \"$directories{BLDAPPS}\"\n";
1155         print STDERR "  \$directories{SRCAPPS} = \"$directories{SRCAPPS}\"\n";
1156         print STDERR "  \$directories{SRCTOP}  = \"$directories{SRCTOP}\"\n";
1157         print STDERR "  \$directories{BLDTOP}  = \"$directories{BLDTOP}\"\n";
1158         print STDERR "\n";
1159         print STDERR "  current directory is \"",curdir(),"\"\n";
1160         print STDERR "  the way back is \"$reverse\"\n";
1161     }
1162
1163     return $reverse;
1164 }
1165
1166 # __wrap_cmd CMD
1167 # __wrap_cmd CMD, EXE_SHELL
1168 #
1169 # __wrap_cmd "wraps" CMD (string) with a beginning command that makes sure
1170 # the command gets executed with an appropriate environment.  If EXE_SHELL
1171 # is given, it is used as the beginning command.
1172 #
1173 # __wrap_cmd returns a list that should be used to build up a larger list
1174 # of command tokens, or be joined together like this:
1175 #
1176 #    join(" ", __wrap_cmd($cmd))
1177 sub __wrap_cmd {
1178     my $cmd = shift;
1179     my $exe_shell = shift;
1180
1181     my @prefix = ();
1182
1183     if (defined($exe_shell)) {
1184         # If $exe_shell is defined, trust it
1185         @prefix = ( $exe_shell );
1186     } else {
1187         # Otherwise, use the standard wrapper
1188         my $std_wrapper = __bldtop_file("util", "wrap.pl");
1189
1190         if ($^O eq "VMS") {
1191             # On VMS, running random executables without having a command
1192             # symbol means running them with the MCR command.  This is an
1193             # old PDP-11 command that stuck around.  So we get a command
1194             # running perl running the script.
1195             @prefix = ( "MCR", $^X, $std_wrapper );
1196         } elsif ($^O eq "MSWin32") {
1197             # In the Windows case, we run perl explicitly.  We might not
1198             # need it, but that depends on if the user has associated the
1199             # '.pl' extension with a perl interpreter, so better be safe.
1200             @prefix = ( $^X, $std_wrapper );
1201         } else {
1202             # Otherwise, we assume Unix semantics, and trust that the #!
1203             # line activates perl for us.
1204             @prefix = ( $std_wrapper );
1205         }
1206     }
1207
1208     return (@prefix, $cmd);
1209 }
1210
1211 # __fixup_prg PROG
1212 #
1213 # __fixup_prg does whatever fixup is needed to execute an executable binary
1214 # given by PROG (string).
1215 #
1216 # __fixup_prg returns a string with the possibly prefixed program path spec.
1217 sub __fixup_prg {
1218     my $prog = shift;
1219
1220     my $prefix = "";
1221
1222     if ($^O eq "VMS" ) {
1223         $prefix = ($prog =~ /^(?:[\$a-z0-9_]+:)?[<\[]/i ? "mcr " : "mcr []");
1224     }
1225
1226     if (defined($prog)) {
1227         # Make sure to quotify the program file on platforms that may
1228         # have spaces or similar in their path name.
1229         # To our knowledge, VMS is the exception where quotifying should
1230         # never happen.
1231         ($prog) = quotify($prog) unless $^O eq "VMS";
1232         return $prefix.$prog;
1233     }
1234
1235     print STDERR "$prog not found\n";
1236     return undef;
1237 }
1238
1239 # __decorate_cmd NUM, CMDARRAYREF
1240 #
1241 # __decorate_cmd takes a command number NUM and a command token array
1242 # CMDARRAYREF, builds up a command string from them and decorates it
1243 # with necessary redirections.
1244 # __decorate_cmd returns a list of two strings, one with the command
1245 # string to actually be used, the other to be displayed for the user.
1246 # The reason these strings might differ is that we redirect stderr to
1247 # the null device unless we're verbose and unless the user has
1248 # explicitly specified a stderr redirection.
1249 sub __decorate_cmd {
1250     BAIL_OUT("Must run setup() first") if (! $test_name);
1251
1252     my $num = shift;
1253     my $cmd = shift;
1254     my %opts = @_;
1255
1256     my $cmdstr = join(" ", @$cmd);
1257     my $null = devnull();
1258     my $fileornull = sub { $_[0] ? $_[0] : $null; };
1259     my $stdin = "";
1260     my $stdout = "";
1261     my $stderr = "";
1262     my $saved_stderr = undef;
1263     $stdin = " < ".$fileornull->($opts{stdin})  if exists($opts{stdin});
1264     $stdout= " > ".$fileornull->($opts{stdout}) if exists($opts{stdout});
1265     $stderr=" 2> ".$fileornull->($opts{stderr}) if exists($opts{stderr});
1266
1267     my $display_cmd = "$cmdstr$stdin$stdout$stderr";
1268
1269     # VMS program output escapes TAP::Parser
1270     if ($^O eq 'VMS') {
1271         $stderr=" 2> ".$null
1272             unless $stderr || !$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE};
1273     }
1274
1275     $cmdstr .= "$stdin$stdout$stderr";
1276
1277     if ($debug) {
1278         print STDERR "DEBUG[__decorate_cmd]: \$cmdstr = \"$cmdstr\"\n";
1279         print STDERR "DEBUG[__decorate_cmd]: \$display_cmd = \"$display_cmd\"\n";
1280     }
1281
1282     return ($cmdstr, $display_cmd);
1283 }
1284
1285 =head1 SEE ALSO
1286
1287 L<Test::More>, L<Test::Harness>
1288
1289 =head1 AUTHORS
1290
1291 Richard Levitte E<lt>levitte@openssl.orgE<gt> with assistance and
1292 inspiration from Andy Polyakov E<lt>appro@openssl.org<gt>.
1293
1294 =cut
1295
1296 no warnings 'redefine';
1297 sub subtest {
1298     $level++;
1299
1300     Test::More::subtest @_;
1301
1302     $level--;
1303 };
1304
1305 1;