generate_ssl_tests.pl 4.7 KB

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