Do not use redirection on binary files
[openssl.git] / test / recipes / tconversion.pl
1 #! /usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use File::Compare qw/compare_text/;
7 use File::Copy;
8 use lib 'testlib';
9 use OpenSSL::Test qw/:DEFAULT top_file/;
10
11 my %conversionforms = (
12     # Default conversion forms.  Other series may be added with
13     # specific test types as key.
14     "*"         => [ "d", "p" ],
15     );
16 sub tconversion {
17     my $testtype = shift;
18     my $t = shift;
19     my @conversionforms = 
20         defined($conversionforms{$testtype}) ?
21         @{$conversionforms{$testtype}} :
22         @{$conversionforms{"*"}};
23     my @openssl_args = @_;
24     if (!@openssl_args) { @openssl_args = ($testtype); }
25
26     my $n = scalar @conversionforms;
27     my $totaltests =
28         1                       # for initializing
29         + $n                    # initial conversions from p to all forms (A)
30         + $n*$n                 # conversion from result of A to all forms (B)
31         + 1                     # comparing original test file to p form of A
32         + $n*($n-1);            # comparing first conversion to each fom in A with B
33     $totaltests-- if ($testtype eq "p7d"); # no comparison of original test file
34     plan tests => $totaltests;
35
36     my @cmd = ("openssl", @openssl_args);
37
38     my $init;
39     if (scalar @openssl_args > 0 && $openssl_args[0] eq "pkey") {
40         $init = ok(run(app([@cmd, "-in", $t, "-out", "$testtype-fff.p"])),
41                    'initializing');
42     } else {
43         $init = ok(copy($t, "$testtype-fff.p"), 'initializing');
44     }
45     if (!$init) {
46         diag("Trying to copy $t to $testtype-fff.p : $!");
47     }
48
49   SKIP: {
50       skip "Not initialized, skipping...", 22 unless $init;
51
52       foreach my $to (@conversionforms) {
53           ok(run(app([@cmd,
54                       "-in", "$testtype-fff.p",
55                       "-inform", "p",
56                       "-out", "$testtype-f.$to",
57                       "-outform", $to])),
58              "p -> $to");
59       }
60
61       foreach my $to (@conversionforms) {
62           foreach my $from (@conversionforms) {
63               ok(run(app([@cmd,
64                           "-in", "$testtype-f.$from",
65                           "-inform", $from,
66                           "-out", "$testtype-ff.$from$to",
67                           "-outform", $to])),
68                  "$from -> $to");
69           }
70       }
71
72       if ($testtype ne "p7d") {
73           is(cmp_text("$testtype-fff.p", "$testtype-f.p"), 0,
74              'comparing orig to p');
75       }
76
77       foreach my $to (@conversionforms) {
78           next if $to eq "d";
79           foreach my $from (@conversionforms) {
80               is(cmp_text("$testtype-f.$to", "$testtype-ff.$from$to"), 0,
81                  "comparing $to to $from$to");
82           }
83       }
84     }
85     unlink glob "$testtype-f.*";
86     unlink glob "$testtype-ff.*";
87     unlink glob "$testtype-fff.*";
88 }
89
90 sub cmp_text {
91     return compare_text(@_, sub {
92         $_[0] =~ s/\R//g;
93         $_[1] =~ s/\R//g;
94         return $_[0] ne $_[1];
95     });
96 }
97
98 1;