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