SSL test framework: port resumption tests
[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     }
67
68     # ssl_test expects to find a
69     #
70     # num_tests = n
71     #
72     # directive in the file. It'll then look for configuration directives
73     # for n tests, that each look like this:
74     #
75     # test-n = test-section
76     #
77     # [test-section]
78     # (SSL modules for client and server configuration go here.)
79     #
80     # [test-n]
81     # (Test configuration goes here.)
82     print "num_tests = $num\n\n";
83
84     # The conf module locations must come before everything else, because
85     # they look like
86     #
87     # test-n = test-section
88     #
89     # and you can't mix and match them with sections.
90     my $idx = 0;
91
92     foreach my $test (@ssltests::tests) {
93         my $testname = "${idx}-" . $test->{'name'};
94         print "test-$idx = $testname\n";
95         $idx++;
96     }
97
98     $idx = 0;
99
100     foreach my $test (@ssltests::tests) {
101         my $testname = "${idx}-" . $test->{'name'};
102         my $text = $template->fill_in(
103             HASH => [{ idx => $idx, testname => $testname } , $test],
104             DELIMITERS => [ "{-", "-}" ]);
105         print "# ===========================================================\n\n";
106         print "$text\n";
107         $idx++;
108     }
109 }
110
111 # Shamelessly copied from Configure.
112 sub read_config {
113     my $fname = shift;
114     open(INPUT, "< $fname") or die "Can't open input file '$fname'!\n";
115     local $/ = undef;
116     my $content = <INPUT>;
117     close(INPUT);
118     eval $content;
119     warn $@ if $@;
120 }
121
122 my $input_file = shift;
123 # Reads the tests into ssltests::tests.
124 read_config($input_file);
125 print_templates();
126
127 1;