Reject duplicate -addext parameters
[openssl.git] / test / recipes / 25-test_req.t
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
9
10 use strict;
11 use warnings;
12
13 use OpenSSL::Test::Utils;
14 use OpenSSL::Test qw/:DEFAULT srctop_file/;
15
16 setup("test_req");
17
18 plan tests => 8;
19
20 require_ok(srctop_file('test','recipes','tconversion.pl'));
21
22 open RND, ">>", ".rnd";
23 print RND "string to make the random number generator think it has randomness";
24 close RND;
25
26 # Check for duplicate -addext parameters
27 my $val = "subjectAltName=DNS:example.com";
28 my $val2 = " " . $val;
29 my $val3 = $val;
30 $val3 =~ s/=/    =/;
31 ok(!run(app(["openssl", "req", "-new", "-addext", $val, "-addext", $val])));
32 ok(!run(app(["openssl", "req", "-new", "-addext", $val, "-addext", $val2])));
33 ok(!run(app(["openssl", "req", "-new", "-addext", $val, "-addext", $val3])));
34 ok(!run(app(["openssl", "req", "-new", "-addext", $val2, "-addext", $val3])));
35
36 subtest "generating certificate requests" => sub {
37     my @req_new;
38     if (disabled("rsa")) {
39         @req_new = ("-newkey", "dsa:".srctop_file("apps", "dsa512.pem"));
40     } else {
41         @req_new = ("-new");
42         note("There should be a 2 sequences of .'s and some +'s.");
43         note("There should not be more that at most 80 per line");
44     }
45
46     plan tests => 2;
47
48     ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
49                 @req_new, "-out", "testreq.pem"])),
50        "Generating request");
51
52     ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
53                 "-verify", "-in", "testreq.pem", "-noout"])),
54        "Verifying signature on request");
55 };
56
57 my @openssl_args = ("req", "-config", srctop_file("apps", "openssl.cnf"));
58
59 run_conversion('req conversions',
60                "testreq.pem");
61 run_conversion('req conversions -- testreq2',
62                srctop_file("test", "testreq2.pem"));
63
64 unlink "testkey.pem", "testreq.pem";
65
66 sub run_conversion {
67     my $title = shift;
68     my $reqfile = shift;
69
70     subtest $title => sub {
71         run(app(["openssl", @openssl_args,
72                  "-in", $reqfile, "-inform", "p",
73                  "-noout", "-text"],
74                 stderr => "req-check.err", stdout => undef));
75         open DATA, "req-check.err";
76       SKIP: {
77           plan skip_all => "skipping req conversion test for $reqfile"
78               if grep /Unknown Public Key/, map { s/\R//; } <DATA>;
79
80           tconversion("req", $reqfile, @openssl_args);
81         }
82         close DATA;
83         unlink "req-check.err";
84
85         done_testing();
86     };
87 }