Add OpenSSL copyright to .pl files
[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         $test->{"client"} = { (%ssltests::base_client, %{$test->{"client"}}) };
47     }
48
49     # ssl_test expects to find a
50     #
51     # num_tests = n
52     #
53     # directive in the file. It'll then look for configuration directives
54     # for n tests, that each look like this:
55     #
56     # test-n = test-section
57     #
58     # [test-section]
59     # (SSL modules for client and server configuration go here.)
60     #
61     # [test-n]
62     # (Test configuration goes here.)
63     print "num_tests = $num\n\n";
64
65     # The conf module locations must come before everything else, because
66     # they look like
67     #
68     # test-n = test-section
69     #
70     # and you can't mix and match them with sections.
71     my $idx = 0;
72
73     foreach my $test (@ssltests::tests) {
74         my $testname = "${idx}-" . $test->{'name'};
75         print "test-$idx = $testname\n";
76         $idx++;
77     }
78
79     $idx = 0;
80
81     foreach my $test (@ssltests::tests) {
82         my $testname = "${idx}-" . $test->{'name'};
83         my $text = $template->fill_in(
84             HASH => [{ idx => $idx, testname => $testname } , $test],
85             DELIMITERS => [ "{-", "-}" ]);
86         print "# ===========================================================\n\n";
87         print "$text\n";
88         $idx++;
89     }
90 }
91
92 # Shamelessly copied from Configure.
93 sub read_config {
94     my $fname = shift;
95     open(INPUT, "< $fname")
96         or die "Can't open input file '$fname'!\n";
97     local $/ = undef;
98     my $content = <INPUT>;
99     close(INPUT);
100     eval $content;
101     warn $@ if $@;
102 }
103
104 my $input_file = shift;
105 # Reads the tests into ssltests::tests.
106 read_config($input_file);
107 print_templates();
108
109 1;