| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 OpenSSL::Test qw/:DEFAULT/; | 
|  | 16 |  | 
|  | 17 | my %conversionforms = ( | 
|  | 18 | # Default conversion forms.  Other series may be added with | 
|  | 19 | # specific test types as key. | 
|  | 20 | "*"		=> [ "d", "p" ], | 
|  | 21 | "msb"	=> [ "d", "p", "msblob" ], | 
|  | 22 | ); | 
|  | 23 | sub tconversion { | 
|  | 24 | my $testtype = shift; | 
|  | 25 | my $t = shift; | 
|  | 26 | my @conversionforms = | 
|  | 27 | defined($conversionforms{$testtype}) ? | 
|  | 28 | @{$conversionforms{$testtype}} : | 
|  | 29 | @{$conversionforms{"*"}}; | 
|  | 30 | my @openssl_args = @_; | 
|  | 31 | if (!@openssl_args) { @openssl_args = ($testtype); } | 
|  | 32 |  | 
|  | 33 | my $n = scalar @conversionforms; | 
|  | 34 | my $totaltests = | 
|  | 35 | 1			# for initializing | 
|  | 36 | + $n			# initial conversions from p to all forms (A) | 
|  | 37 | + $n*$n			# conversion from result of A to all forms (B) | 
|  | 38 | + 1			# comparing original test file to p form of A | 
|  | 39 | + $n*($n-1);		# comparing first conversion to each fom in A with B | 
|  | 40 | $totaltests-- if ($testtype eq "p7d"); # no comparison of original test file | 
|  | 41 | plan tests => $totaltests; | 
|  | 42 |  | 
|  | 43 | my @cmd = ("openssl", @openssl_args); | 
|  | 44 |  | 
|  | 45 | my $init; | 
|  | 46 | if (scalar @openssl_args > 0 && $openssl_args[0] eq "pkey") { | 
|  | 47 | $init = ok(run(app([@cmd, "-in", $t, "-out", "$testtype-fff.p"])), | 
|  | 48 | 'initializing'); | 
|  | 49 | } else { | 
|  | 50 | $init = ok(copy($t, "$testtype-fff.p"), 'initializing'); | 
|  | 51 | } | 
|  | 52 | if (!$init) { | 
|  | 53 | diag("Trying to copy $t to $testtype-fff.p : $!"); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | SKIP: { | 
|  | 57 | skip "Not initialized, skipping...", 22 unless $init; | 
|  | 58 |  | 
|  | 59 | foreach my $to (@conversionforms) { | 
|  | 60 | ok(run(app([@cmd, | 
|  | 61 | "-in", "$testtype-fff.p", | 
|  | 62 | "-inform", "p", | 
|  | 63 | "-out", "$testtype-f.$to", | 
|  | 64 | "-outform", $to])), | 
|  | 65 | "p -> $to"); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | foreach my $to (@conversionforms) { | 
|  | 69 | foreach my $from (@conversionforms) { | 
|  | 70 | ok(run(app([@cmd, | 
|  | 71 | "-in", "$testtype-f.$from", | 
|  | 72 | "-inform", $from, | 
|  | 73 | "-out", "$testtype-ff.$from$to", | 
|  | 74 | "-outform", $to])), | 
|  | 75 | "$from -> $to"); | 
|  | 76 | } | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | if ($testtype ne "p7d") { | 
|  | 80 | is(cmp_text("$testtype-fff.p", "$testtype-f.p"), 0, | 
|  | 81 | 'comparing orig to p'); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | foreach my $to (@conversionforms) { | 
|  | 85 | next if $to eq "d"; | 
|  | 86 | foreach my $from (@conversionforms) { | 
|  | 87 | is(cmp_text("$testtype-f.$to", "$testtype-ff.$from$to"), 0, | 
|  | 88 | "comparing $to to $from$to"); | 
|  | 89 | } | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 | unlink glob "$testtype-f.*"; | 
|  | 93 | unlink glob "$testtype-ff.*"; | 
|  | 94 | unlink glob "$testtype-fff.*"; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | sub cmp_text { | 
|  | 98 | return compare_text(@_, sub { | 
|  | 99 | $_[0] =~ s/\R//g; | 
|  | 100 | $_[1] =~ s/\R//g; | 
|  | 101 | return $_[0] ne $_[1]; | 
|  | 102 | }); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | 1; |