2
0

04-test_pem.t 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #! /usr/bin/env perl
  2. # Copyright 2017 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. #
  9. # ======================================================================
  10. use strict;
  11. use warnings;
  12. use File::Compare qw/compare_text/;
  13. use File::Basename;
  14. use OpenSSL::Test qw/:DEFAULT srctop_file data_file/;
  15. use OpenSSL::Test::Utils;
  16. setup("test_pem_reading");
  17. my $testsrc = srctop_file("test", "recipes", basename($0));
  18. my $cmd = "openssl";
  19. # map input PEM file to 1 if it should be accepted; 0 when should be rejected
  20. my %cert_expected = (
  21. "cert-1023line.pem" => 1,
  22. "cert-1024line.pem" => 1,
  23. "cert-1025line.pem" => 1,
  24. "cert-255line.pem" => 1,
  25. "cert-256line.pem" => 1,
  26. "cert-257line.pem" => 1,
  27. "cert-blankline.pem" => 0,
  28. "cert-comment.pem" => 0,
  29. "cert-earlypad.pem" => 0,
  30. "cert-extrapad.pem" => 0,
  31. "cert-infixwhitespace.pem" => 1,
  32. "cert-junk.pem" => 0,
  33. "cert-leadingwhitespace.pem" => 1,
  34. "cert-longline.pem" => 1,
  35. "cert-misalignedpad.pem" => 0,
  36. "cert-onecolumn.pem" => 1,
  37. "cert-oneline.pem" => 1,
  38. "cert-shortandlongline.pem" => 1,
  39. "cert-shortline.pem" => 1,
  40. "cert-threecolumn.pem" => 1,
  41. "cert-trailingwhitespace.pem" => 1,
  42. "cert.pem" => 1
  43. );
  44. my %dsa_expected = (
  45. "dsa-1023line.pem" => 0,
  46. "dsa-1024line.pem" => 0,
  47. "dsa-1025line.pem" => 0,
  48. "dsa-255line.pem" => 0,
  49. "dsa-256line.pem" => 0,
  50. "dsa-257line.pem" => 0,
  51. "dsa-blankline.pem" => 0,
  52. "dsa-comment.pem" => 0,
  53. "dsa-corruptedheader.pem" => 0,
  54. "dsa-corruptiv.pem" => 0,
  55. "dsa-earlypad.pem" => 0,
  56. "dsa-extrapad.pem" => 0,
  57. "dsa-infixwhitespace.pem" => 0,
  58. "dsa-junk.pem" => 0,
  59. "dsa-leadingwhitespace.pem" => 0,
  60. "dsa-longline.pem" => 0,
  61. "dsa-misalignedpad.pem" => 0,
  62. "dsa-onecolumn.pem" => 0,
  63. "dsa-oneline.pem" => 0,
  64. "dsa-onelineheader.pem" => 0,
  65. "dsa-shortandlongline.pem" => 0,
  66. "dsa-shortline.pem" => 0,
  67. "dsa-threecolumn.pem" => 0,
  68. "dsa-trailingwhitespace.pem" => 1,
  69. "dsa.pem" => 1
  70. );
  71. plan tests => scalar keys(%cert_expected) + scalar keys(%dsa_expected) + 2;
  72. foreach my $input (keys %cert_expected) {
  73. my @common = ($cmd, "x509", "-text", "-noout", "-inform", "PEM", "-in");
  74. my @data = run(app([@common, data_file($input)], stderr => undef), capture => 1);
  75. my @match = grep /The Great State of Long-Winded Certificate Field Names Whereby to Increase the Output Size/, @data;
  76. is((scalar @match > 0 ? 1 : 0), $cert_expected{$input});
  77. }
  78. SKIP: {
  79. skip "DSA support disabled, skipping...", (scalar keys %dsa_expected) unless !disabled("dsa");
  80. foreach my $input (keys %dsa_expected) {
  81. my @common = ($cmd, "pkey", "-inform", "PEM", "-passin", "file:" . data_file("wellknown"), "-noout", "-text", "-in");
  82. my @data;
  83. {
  84. local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
  85. @data = run(app([@common, data_file($input)], stderr => undef), capture => 1);
  86. }
  87. my @match = grep /68:42:02:16:63:54:16:eb:06:5c:ab:06:72:3b:78:/, @data;
  88. is((scalar @match > 0 ? 1 : 0), $dsa_expected{$input});
  89. }
  90. }
  91. SKIP: {
  92. skip "RSA support disabled, skipping...", 1 unless !disabled("rsa");
  93. my @common = ($cmd, "pkey", "-inform", "PEM", "-noout", "-text", "-in");
  94. my @data = run(app([@common, data_file("beermug.pem")], stderr => undef), capture => 1);
  95. my @match = grep /00:a0:3a:21:14:5d:cd:b6:d5:a0:3e:49:23:c1:3a:/, @data;
  96. ok(scalar @match > 0 ? 1 : 0);
  97. }
  98. ok(run(test(["pemtest"])), "running pemtest");