tconversion.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2021 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. "pvk" => [ "d", "p", "pvk" ],
  19. );
  20. sub tconversion {
  21. my %opts = @_;
  22. die "Missing option -type" unless $opts{-type};
  23. die "Missing option -in" unless $opts{-in};
  24. my $testtype = $opts{-type};
  25. my $t = $opts{-in};
  26. my $prefix = $opts{-prefix} // $testtype;
  27. my @conversionforms =
  28. defined($conversionforms{$testtype}) ?
  29. @{$conversionforms{$testtype}} :
  30. @{$conversionforms{"*"}};
  31. my @openssl_args;
  32. if (defined $opts{-args}) {
  33. @openssl_args = @{$opts{-args}} if ref $opts{-args} eq 'ARRAY';
  34. @openssl_args = ($opts{-args}) if ref $opts{-args} eq '';
  35. }
  36. @openssl_args = ($testtype) unless @openssl_args;
  37. my $n = scalar @conversionforms;
  38. my $totaltests =
  39. 1 # for initializing
  40. + $n # initial conversions from p to all forms (A)
  41. + $n*$n # conversion from result of A to all forms (B)
  42. + 1 # comparing original test file to p form of A
  43. + $n*($n-1); # comparing first conversion to each form in A with B
  44. $totaltests-- if ($testtype eq "p7d"); # no comparison of original test file
  45. $totaltests -= $n if ($testtype eq "pvk"); # no comparisons of the pvk form
  46. plan tests => $totaltests;
  47. my @cmd = ("openssl", @openssl_args);
  48. my $init;
  49. if (scalar @openssl_args > 0 && $openssl_args[0] eq "pkey") {
  50. $init = ok(run(app([@cmd, "-in", $t, "-out", "$prefix-fff.p"])),
  51. 'initializing');
  52. } else {
  53. $init = ok(copy($t, "$prefix-fff.p"), 'initializing');
  54. }
  55. if (!$init) {
  56. diag("Trying to copy $t to $prefix-fff.p : $!");
  57. }
  58. SKIP: {
  59. skip "Not initialized, skipping...", 22 unless $init;
  60. foreach my $to (@conversionforms) {
  61. ok(run(app([@cmd,
  62. "-in", "$prefix-fff.p",
  63. "-inform", "p",
  64. "-out", "$prefix-f.$to",
  65. "-outform", $to])),
  66. "p -> $to");
  67. }
  68. foreach my $to (@conversionforms) {
  69. foreach my $from (@conversionforms) {
  70. ok(run(app([@cmd,
  71. "-in", "$prefix-f.$from",
  72. "-inform", $from,
  73. "-out", "$prefix-ff.$from$to",
  74. "-outform", $to])),
  75. "$from -> $to");
  76. }
  77. }
  78. if ($testtype ne "p7d") {
  79. is(cmp_text("$prefix-fff.p", "$prefix-f.p"), 0,
  80. 'comparing orig to p');
  81. }
  82. foreach my $to (@conversionforms) {
  83. next if $to eq "d" or $to eq "pvk";
  84. foreach my $from (@conversionforms) {
  85. is(cmp_text("$prefix-f.$to", "$prefix-ff.$from$to"), 0,
  86. "comparing $to to $from$to");
  87. }
  88. }
  89. }
  90. }
  91. sub cmp_text {
  92. return compare_text(@_, sub {
  93. $_[0] =~ s/\R//g;
  94. $_[1] =~ s/\R//g;
  95. return $_[0] ne $_[1];
  96. });
  97. }
  98. sub file_contains {
  99. $_ = shift @_;
  100. my $pattern = shift @_;
  101. open(DATA, $_) or return 0;
  102. $_= join('', <DATA>);
  103. close(DATA);
  104. s/\s+/ /g; # take multiple whitespace (including newline) as single space
  105. return m/$pattern/ ? 1 : 0;
  106. }
  107. sub cert_contains {
  108. my $cert = shift @_;
  109. my $pattern = shift @_;
  110. my $expected = shift @_;
  111. my $name = shift @_;
  112. my $out = "cert_contains.out";
  113. run(app(["openssl", "x509", "-noout", "-text", "-in", $cert, "-out", $out]));
  114. is(file_contains($out, $pattern), $expected, ($name ? "$name: " : "").
  115. "$cert should ".($expected ? "" : "not ")."contain: \"$pattern\"");
  116. # not unlinking $out
  117. }
  118. sub uniq (@) {
  119. my %seen = ();
  120. grep { not $seen{$_}++ } @_;
  121. }
  122. sub file_n_different_lines {
  123. my $filename = shift @_;
  124. open(DATA, $filename) or return 0;
  125. chomp(my @lines = <DATA>);
  126. close(DATA);
  127. return scalar(uniq @lines);
  128. }
  129. sub cert_ext_has_n_different_lines {
  130. my $cert = shift @_;
  131. my $expected = shift @_;
  132. my $exts = shift @_;
  133. my $name = shift @_;
  134. my $out = "cert_n_different_exts.out";
  135. run(app(["openssl", "x509", "-noout", "-ext", $exts,
  136. "-in", $cert, "-out", $out]));
  137. is(file_n_different_lines($out), $expected, ($name ? "$name: " : "").
  138. "$cert '$exts' output should contain $expected different lines");
  139. # not unlinking $out
  140. }
  141. 1;