xref: /freebsd/crypto/openssl/test/generate_ssl_tests.pl (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubert## SSL testcase generator
10*e0c4386eSCy Schubert
11*e0c4386eSCy Schubertuse strict;
12*e0c4386eSCy Schubertuse warnings;
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubertuse Cwd qw/abs_path/;
15*e0c4386eSCy Schubertuse File::Basename;
16*e0c4386eSCy Schubertuse File::Spec::Functions;
17*e0c4386eSCy Schubert
18*e0c4386eSCy Schubertuse OpenSSL::Test qw/srctop_dir srctop_file/;
19*e0c4386eSCy Schubertuse OpenSSL::Test::Utils;
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubertuse FindBin;
22*e0c4386eSCy Schubertuse lib "$FindBin::Bin/../util/perl";
23*e0c4386eSCy Schubertuse OpenSSL::fallback "$FindBin::Bin/../external/perl/MODULES.txt";
24*e0c4386eSCy Schubertuse Text::Template 1.46;
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubertmy $input_file;
27*e0c4386eSCy Schubertmy $provider;
28*e0c4386eSCy Schubert
29*e0c4386eSCy SchubertBEGIN {
30*e0c4386eSCy Schubert    #Input file may be relative to cwd, but setup below changes the cwd, so
31*e0c4386eSCy Schubert    #figure out the absolute path first
32*e0c4386eSCy Schubert    $input_file = abs_path(shift);
33*e0c4386eSCy Schubert    $provider = shift // '';
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert    OpenSSL::Test::setup("no_test_here", quiet => 1);
36*e0c4386eSCy Schubert}
37*e0c4386eSCy Schubert
38*e0c4386eSCy Schubertuse lib "$FindBin::Bin/ssl-tests";
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubertuse vars qw/@ISA/;
41*e0c4386eSCy Schubertpush (@ISA, qw/Text::Template/);
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubertuse ssltests_base;
44*e0c4386eSCy Schubert
45*e0c4386eSCy Schubertsub print_templates {
46*e0c4386eSCy Schubert    my $source = srctop_file("test", "ssl_test.tmpl");
47*e0c4386eSCy Schubert    my $template = Text::Template->new(TYPE => 'FILE', SOURCE => $source);
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert    print "# Generated with generate_ssl_tests.pl\n\n";
50*e0c4386eSCy Schubert
51*e0c4386eSCy Schubert    my $num = scalar @ssltests::tests;
52*e0c4386eSCy Schubert
53*e0c4386eSCy Schubert    # Add the implicit base configuration.
54*e0c4386eSCy Schubert    foreach my $test (@ssltests::tests) {
55*e0c4386eSCy Schubert        $test->{"server"} = { (%ssltests::base_server, %{$test->{"server"}}) };
56*e0c4386eSCy Schubert        if (defined $test->{"server2"}) {
57*e0c4386eSCy Schubert            $test->{"server2"} = { (%ssltests::base_server, %{$test->{"server2"}}) };
58*e0c4386eSCy Schubert        } else {
59*e0c4386eSCy Schubert            if ($test->{"server"}->{"extra"} &&
60*e0c4386eSCy Schubert                defined $test->{"server"}->{"extra"}->{"ServerNameCallback"}) {
61*e0c4386eSCy Schubert                # Default is the same as server.
62*e0c4386eSCy Schubert                $test->{"reuse_server2"} = 1;
63*e0c4386eSCy Schubert            }
64*e0c4386eSCy Schubert            # Do not emit an empty/duplicate "server2" section.
65*e0c4386eSCy Schubert            $test->{"server2"} = { };
66*e0c4386eSCy Schubert        }
67*e0c4386eSCy Schubert        if (defined $test->{"resume_server"}) {
68*e0c4386eSCy Schubert            $test->{"resume_server"} = { (%ssltests::base_server, %{$test->{"resume_server"}}) };
69*e0c4386eSCy Schubert        } else {
70*e0c4386eSCy Schubert            if (defined $test->{"test"}->{"HandshakeMode"} &&
71*e0c4386eSCy Schubert                 $test->{"test"}->{"HandshakeMode"} eq "Resume") {
72*e0c4386eSCy Schubert                # Default is the same as server.
73*e0c4386eSCy Schubert                $test->{"reuse_resume_server"} = 1;
74*e0c4386eSCy Schubert            }
75*e0c4386eSCy Schubert            # Do not emit an empty/duplicate "resume-server" section.
76*e0c4386eSCy Schubert            $test->{"resume_server"} = { };
77*e0c4386eSCy Schubert        }
78*e0c4386eSCy Schubert        $test->{"client"} = { (%ssltests::base_client, %{$test->{"client"}}) };
79*e0c4386eSCy Schubert        if (defined $test->{"resume_client"}) {
80*e0c4386eSCy Schubert            $test->{"resume_client"} = { (%ssltests::base_client, %{$test->{"resume_client"}}) };
81*e0c4386eSCy Schubert        } else {
82*e0c4386eSCy Schubert            if (defined $test->{"test"}->{"HandshakeMode"} &&
83*e0c4386eSCy Schubert                 $test->{"test"}->{"HandshakeMode"} eq "Resume") {
84*e0c4386eSCy Schubert                # Default is the same as client.
85*e0c4386eSCy Schubert                $test->{"reuse_resume_client"} = 1;
86*e0c4386eSCy Schubert            }
87*e0c4386eSCy Schubert            # Do not emit an empty/duplicate "resume-client" section.
88*e0c4386eSCy Schubert            $test->{"resume_client"} = { };
89*e0c4386eSCy Schubert        }
90*e0c4386eSCy Schubert    }
91*e0c4386eSCy Schubert
92*e0c4386eSCy Schubert    # ssl_test expects to find a
93*e0c4386eSCy Schubert    #
94*e0c4386eSCy Schubert    # num_tests = n
95*e0c4386eSCy Schubert    #
96*e0c4386eSCy Schubert    # directive in the file. It'll then look for configuration directives
97*e0c4386eSCy Schubert    # for n tests, that each look like this:
98*e0c4386eSCy Schubert    #
99*e0c4386eSCy Schubert    # test-n = test-section
100*e0c4386eSCy Schubert    #
101*e0c4386eSCy Schubert    # [test-section]
102*e0c4386eSCy Schubert    # (SSL modules for client and server configuration go here.)
103*e0c4386eSCy Schubert    #
104*e0c4386eSCy Schubert    # [test-n]
105*e0c4386eSCy Schubert    # (Test configuration goes here.)
106*e0c4386eSCy Schubert    print "num_tests = $num\n\n";
107*e0c4386eSCy Schubert
108*e0c4386eSCy Schubert    # The conf module locations must come before everything else, because
109*e0c4386eSCy Schubert    # they look like
110*e0c4386eSCy Schubert    #
111*e0c4386eSCy Schubert    # test-n = test-section
112*e0c4386eSCy Schubert    #
113*e0c4386eSCy Schubert    # and you can't mix and match them with sections.
114*e0c4386eSCy Schubert    my $idx = 0;
115*e0c4386eSCy Schubert
116*e0c4386eSCy Schubert    foreach my $test (@ssltests::tests) {
117*e0c4386eSCy Schubert        my $testname = "${idx}-" . $test->{'name'};
118*e0c4386eSCy Schubert        print "test-$idx = $testname\n";
119*e0c4386eSCy Schubert        $idx++;
120*e0c4386eSCy Schubert    }
121*e0c4386eSCy Schubert
122*e0c4386eSCy Schubert    $idx = 0;
123*e0c4386eSCy Schubert
124*e0c4386eSCy Schubert    foreach my $test (@ssltests::tests) {
125*e0c4386eSCy Schubert        my $testname = "${idx}-" . $test->{'name'};
126*e0c4386eSCy Schubert        my $text = $template->fill_in(
127*e0c4386eSCy Schubert            HASH => [{ idx => $idx, testname => $testname } , $test],
128*e0c4386eSCy Schubert            DELIMITERS => [ "{-", "-}" ]);
129*e0c4386eSCy Schubert        print "# ===========================================================\n\n";
130*e0c4386eSCy Schubert        print "$text\n";
131*e0c4386eSCy Schubert        $idx++;
132*e0c4386eSCy Schubert    }
133*e0c4386eSCy Schubert}
134*e0c4386eSCy Schubert
135*e0c4386eSCy Schubert# Shamelessly copied from Configure.
136*e0c4386eSCy Schubertsub read_config {
137*e0c4386eSCy Schubert    my $fname = shift;
138*e0c4386eSCy Schubert    my $provider = shift;
139*e0c4386eSCy Schubert    local $ssltests::fips_mode = $provider eq "fips";
140*e0c4386eSCy Schubert    local $ssltests::no_deflt_libctx =
141*e0c4386eSCy Schubert        $provider eq "default" || $provider eq "fips";
142*e0c4386eSCy Schubert
143*e0c4386eSCy Schubert    open(INPUT, "< $fname") or die "Can't open input file '$fname'!\n";
144*e0c4386eSCy Schubert    local $/ = undef;
145*e0c4386eSCy Schubert    my $content = <INPUT>;
146*e0c4386eSCy Schubert    close(INPUT);
147*e0c4386eSCy Schubert    eval $content;
148*e0c4386eSCy Schubert    warn $@ if $@;
149*e0c4386eSCy Schubert}
150*e0c4386eSCy Schubert
151*e0c4386eSCy Schubert# Reads the tests into ssltests::tests.
152*e0c4386eSCy Schubertread_config($input_file, $provider);
153*e0c4386eSCy Schubertprint_templates();
154*e0c4386eSCy Schubert
155*e0c4386eSCy Schubert1;
156