2
0

tconversion.pl 2.8 KB

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