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