tconversion.pl 3.0 KB

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