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