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