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