2
0

tconversion.pl 2.8 KB

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