generate_ssl_tests.pl 4.6 KB

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