Change OpenSSL::Test to be an extension of Test::More
[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     "*"         => [ "d", "p" ],
13     x509        => [ "d", "n", "p" ],
14     );
15 sub tconversion {
16     my $testtype = shift;
17     my $t = shift;
18     my @conversionforms = 
19         defined($conversionforms{$testtype}) ?
20         @{$conversionforms{$testtype}} :
21         @{$conversionforms{"*"}};
22     my @openssl_args = @_;
23     if (!@openssl_args) { @openssl_args = ($testtype); }
24
25     my $n = scalar @conversionforms;
26     my $totaltests =
27         1                       # for initializing
28         + $n                    # initial conversions from p to all forms (A)
29         + $n*$n                 # conversion from result of A to all forms (B)
30         + 1                     # comparing original test file to p form of A
31         + $n*($n-1);            # comparing first conversion to each fom in A with B
32     $totaltests-- if ($testtype eq "p7d"); # no comparison of original test file
33     plan tests => $totaltests;
34
35     my @cmd = ("openssl", @openssl_args);
36
37     my $init;
38     if (scalar @openssl_args > 0 && $openssl_args[0] eq "pkey") {
39         $init = ok(run(app([@cmd, "-in", $t, "-out", "$testtype-fff.p"])),
40                    'initializing');
41     } else {
42         $init = ok(copy($t, "$testtype-fff.p"), 'initializing');
43     }
44     if (!$init) {
45         diag("Trying to copy $t to $testtype-fff.p : $!");
46     }
47
48   SKIP: {
49       skip "Not initialized, skipping...", 22 unless $init;
50
51       foreach my $to (@conversionforms) {
52           ok(run(app([@cmd,
53                       "-in", "$testtype-fff.p",
54                       "-inform", "p",
55                       "-outform", $to],
56                      stdout => "$testtype-f.$to")), "p -> $to");
57       }
58
59       foreach my $to (@conversionforms) {
60           foreach my $from (@conversionforms) {
61               ok(run(app([@cmd,
62                           "-in", "$testtype-f.$from",
63                           "-inform", $from,
64                           "-outform", $to],
65                          stdout => "$testtype-ff.$from$to")), "$from -> $to");
66           }
67       }
68
69       if ($testtype ne "p7d") {
70           is(compare_text("$testtype-fff.p", "$testtype-f.p"), 0,
71              'comparing orig to p');
72       }
73
74       foreach my $to (@conversionforms) {
75           next if $to eq "d";
76           foreach my $from (@conversionforms) {
77               is(compare_text("$testtype-f.$to", "$testtype-ff.$from$to"), 0,
78                  "comparing $to to $from$to");
79           }
80       }
81     }
82     unlink glob "$testtype-f.*";
83     unlink glob "$testtype-ff.*";
84     unlink glob "$testtype-fff.*";
85 }
86
87 1;