Add a helper script for key file format conversion tests
[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 Test::More;
9 use lib 'testlib';
10 use OpenSSL::Test qw/:DEFAULT top_file/;
11
12 my %conversionforms = (
13     "*"         => [ "d", "p" ],
14     x509        => [ "d", "n", "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                       "-outform", $to],
57                      stdout => "$testtype-f.$to")), "p -> $to");
58       }
59
60       foreach my $to (@conversionforms) {
61           foreach my $from (@conversionforms) {
62               ok(run(app([@cmd,
63                           "-in", "$testtype-f.$from",
64                           "-inform", $from,
65                           "-outform", $to],
66                          stdout => "$testtype-ff.$from$to")), "$from -> $to");
67           }
68       }
69
70       if ($testtype ne "p7d") {
71           is(compare_text("$testtype-fff.p", "$testtype-f.p"), 0,
72              'comparing orig to p');
73       }
74
75       foreach my $to (@conversionforms) {
76           next if $to eq "d";
77           foreach my $from (@conversionforms) {
78               is(compare_text("$testtype-f.$to", "$testtype-ff.$from$to"), 0,
79                  "comparing $to to $from$to");
80           }
81       }
82     }
83     unlink glob "$testtype-f.*";
84     unlink glob "$testtype-ff.*";
85     unlink glob "$testtype-fff.*";
86 }
87
88 1;