1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ci# For manually running these tests, set specific environment variables like this:
10e1051a39Sopenharmony_ci# CTLOG_FILE=test/ct/log_list.cnf
11e1051a39Sopenharmony_ci# TEST_CERTS_DIR=test/certs
12e1051a39Sopenharmony_ci# For details on the environment variables needed, see test/README.ssltest.md
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ciuse strict;
15e1051a39Sopenharmony_ciuse warnings;
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ciuse File::Basename;
18e1051a39Sopenharmony_ciuse File::Compare qw/compare_text/;
19e1051a39Sopenharmony_ciuse OpenSSL::Glob;
20e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT srctop_dir srctop_file bldtop_file bldtop_dir/;
21e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils qw/disabled alldisabled available_protocols/;
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ciBEGIN {
24e1051a39Sopenharmony_cisetup("test_ssl_new");
25e1051a39Sopenharmony_ci}
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ciuse lib srctop_dir('Configurations');
28e1051a39Sopenharmony_ciuse lib bldtop_dir('.');
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_cimy $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci$ENV{TEST_CERTS_DIR} = srctop_dir("test", "certs");
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_cimy @conf_srcs =  glob(srctop_file("test", "ssl-tests", "*.cnf.in"));
35e1051a39Sopenharmony_cimap { s/;.*// } @conf_srcs if $^O eq "VMS";
36e1051a39Sopenharmony_cimy @conf_files = map { basename($_, ".in") } @conf_srcs;
37e1051a39Sopenharmony_cimap { s/\^// } @conf_files if $^O eq "VMS";
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci# We hard-code the number of tests to double-check that the globbing above
40e1051a39Sopenharmony_ci# finds all files as expected.
41e1051a39Sopenharmony_ciplan tests => 30;
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci# Some test results depend on the configuration of enabled protocols. We only
44e1051a39Sopenharmony_ci# verify generated sources in the default configuration.
45e1051a39Sopenharmony_cimy $is_default_tls = (disabled("ssl3") && !disabled("tls1") &&
46e1051a39Sopenharmony_ci                      !disabled("tls1_1") && !disabled("tls1_2") &&
47e1051a39Sopenharmony_ci                      !disabled("tls1_3") && (!disabled("ec") || !disabled("dh")));
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_cimy $is_default_dtls = (!disabled("dtls1") && !disabled("dtls1_2"));
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_cimy @all_pre_tls1_3 = ("ssl3", "tls1", "tls1_1", "tls1_2");
52e1051a39Sopenharmony_cimy $no_tls = alldisabled(available_protocols("tls"));
53e1051a39Sopenharmony_cimy $no_tls_below1_3 = $no_tls || (disabled("tls1_2") && !disabled("tls1_3"));
54e1051a39Sopenharmony_ciif (!$no_tls && $no_tls_below1_3 && disabled("ec") && disabled("dh")) {
55e1051a39Sopenharmony_ci  $no_tls = 1;
56e1051a39Sopenharmony_ci}
57e1051a39Sopenharmony_cimy $no_pre_tls1_3 = alldisabled(@all_pre_tls1_3);
58e1051a39Sopenharmony_cimy $no_dtls = alldisabled(available_protocols("dtls"));
59e1051a39Sopenharmony_cimy $no_npn = disabled("nextprotoneg");
60e1051a39Sopenharmony_cimy $no_ct = disabled("ct");
61e1051a39Sopenharmony_cimy $no_ec = disabled("ec");
62e1051a39Sopenharmony_cimy $no_dh = disabled("dh");
63e1051a39Sopenharmony_cimy $no_dsa = disabled("dsa");
64e1051a39Sopenharmony_cimy $no_ec2m = disabled("ec2m");
65e1051a39Sopenharmony_cimy $no_ocsp = disabled("ocsp");
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci# Add your test here if the test conf.in generates test cases and/or
68e1051a39Sopenharmony_ci# expectations dynamically based on the OpenSSL compile-time config.
69e1051a39Sopenharmony_cimy %conf_dependent_tests = (
70e1051a39Sopenharmony_ci  "02-protocol-version.cnf" => !$is_default_tls,
71e1051a39Sopenharmony_ci  "04-client_auth.cnf" => !$is_default_tls || !$is_default_dtls
72e1051a39Sopenharmony_ci                           || !disabled("sctp"),
73e1051a39Sopenharmony_ci  "05-sni.cnf" => disabled("tls1_1"),
74e1051a39Sopenharmony_ci  "07-dtls-protocol-version.cnf" => !$is_default_dtls || !disabled("sctp"),
75e1051a39Sopenharmony_ci  "10-resumption.cnf" => !$is_default_tls || $no_ec,
76e1051a39Sopenharmony_ci  "11-dtls_resumption.cnf" => !$is_default_dtls || !disabled("sctp"),
77e1051a39Sopenharmony_ci  "16-dtls-certstatus.cnf" => !$is_default_dtls || !disabled("sctp"),
78e1051a39Sopenharmony_ci  "17-renegotiate.cnf" => disabled("tls1_2"),
79e1051a39Sopenharmony_ci  "18-dtls-renegotiate.cnf" => disabled("dtls1_2") || !disabled("sctp"),
80e1051a39Sopenharmony_ci  "19-mac-then-encrypt.cnf" => !$is_default_tls,
81e1051a39Sopenharmony_ci  "20-cert-select.cnf" => !$is_default_tls || $no_dh || $no_dsa,
82e1051a39Sopenharmony_ci  "22-compression.cnf" => !$is_default_tls,
83e1051a39Sopenharmony_ci  "25-cipher.cnf" => disabled("poly1305") || disabled("chacha"),
84e1051a39Sopenharmony_ci  "27-ticket-appdata.cnf" => !$is_default_tls,
85e1051a39Sopenharmony_ci  "28-seclevel.cnf" => disabled("tls1_2") || $no_ec,
86e1051a39Sopenharmony_ci  "30-extended-master-secret.cnf" => disabled("tls1_2"),
87e1051a39Sopenharmony_ci);
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci# Add your test here if it should be skipped for some compile-time
90e1051a39Sopenharmony_ci# configurations. Default is $no_tls but some tests have different skip
91e1051a39Sopenharmony_ci# conditions.
92e1051a39Sopenharmony_cimy %skip = (
93e1051a39Sopenharmony_ci  "06-sni-ticket.cnf" => $no_tls_below1_3,
94e1051a39Sopenharmony_ci  "07-dtls-protocol-version.cnf" => $no_dtls,
95e1051a39Sopenharmony_ci  "08-npn.cnf" => (disabled("tls1") && disabled("tls1_1")
96e1051a39Sopenharmony_ci                    && disabled("tls1_2")) || $no_npn,
97e1051a39Sopenharmony_ci  "10-resumption.cnf" => disabled("tls1_1") || disabled("tls1_2"),
98e1051a39Sopenharmony_ci  "11-dtls_resumption.cnf" => disabled("dtls1") || disabled("dtls1_2"),
99e1051a39Sopenharmony_ci  "12-ct.cnf" => $no_tls || $no_ct || $no_ec,
100e1051a39Sopenharmony_ci  # We could run some of these tests without TLS 1.2 if we had a per-test
101e1051a39Sopenharmony_ci  # disable instruction but that's a bizarre configuration not worth
102e1051a39Sopenharmony_ci  # special-casing for.
103e1051a39Sopenharmony_ci  # TODO(TLS 1.3): We should review this once we have TLS 1.3.
104e1051a39Sopenharmony_ci  "13-fragmentation.cnf" => disabled("tls1_2"),
105e1051a39Sopenharmony_ci  "14-curves.cnf" => disabled("tls1_2") || disabled("tls1_3")
106e1051a39Sopenharmony_ci                     || $no_ec || $no_ec2m,
107e1051a39Sopenharmony_ci  "15-certstatus.cnf" => $no_tls || $no_ocsp,
108e1051a39Sopenharmony_ci  "16-dtls-certstatus.cnf" => $no_dtls || $no_ocsp,
109e1051a39Sopenharmony_ci  "17-renegotiate.cnf" => $no_tls_below1_3,
110e1051a39Sopenharmony_ci  "18-dtls-renegotiate.cnf" => $no_dtls,
111e1051a39Sopenharmony_ci  "19-mac-then-encrypt.cnf" => $no_pre_tls1_3,
112e1051a39Sopenharmony_ci  "20-cert-select.cnf" => disabled("tls1_2") || $no_ec,
113e1051a39Sopenharmony_ci  "21-key-update.cnf" => disabled("tls1_3") || ($no_ec && $no_dh),
114e1051a39Sopenharmony_ci  "22-compression.cnf" => disabled("zlib") || $no_tls,
115e1051a39Sopenharmony_ci  "23-srp.cnf" => (disabled("tls1") && disabled ("tls1_1")
116e1051a39Sopenharmony_ci                    && disabled("tls1_2")) || disabled("srp"),
117e1051a39Sopenharmony_ci  "24-padding.cnf" => disabled("tls1_3") || ($no_ec && $no_dh),
118e1051a39Sopenharmony_ci  "25-cipher.cnf" => disabled("ec") || disabled("tls1_2"),
119e1051a39Sopenharmony_ci  "26-tls13_client_auth.cnf" => disabled("tls1_3") || ($no_ec && $no_dh),
120e1051a39Sopenharmony_ci  "29-dtls-sctp-label-bug.cnf" => disabled("sctp") || disabled("sock"),
121e1051a39Sopenharmony_ci);
122e1051a39Sopenharmony_ci
123e1051a39Sopenharmony_ciforeach my $conf (@conf_files) {
124e1051a39Sopenharmony_ci    subtest "Test configuration $conf" => sub {
125e1051a39Sopenharmony_ci        plan tests => 6 + ($no_fips ? 0 : 3);
126e1051a39Sopenharmony_ci        test_conf($conf,
127e1051a39Sopenharmony_ci                  $conf_dependent_tests{$conf} || $^O eq "VMS" ?  0 : 1,
128e1051a39Sopenharmony_ci                  defined($skip{$conf}) ? $skip{$conf} : $no_tls,
129e1051a39Sopenharmony_ci                  "none");
130e1051a39Sopenharmony_ci        test_conf($conf,
131e1051a39Sopenharmony_ci                  0,
132e1051a39Sopenharmony_ci                  defined($skip{$conf}) ? $skip{$conf} : $no_tls,
133e1051a39Sopenharmony_ci                  "default");
134e1051a39Sopenharmony_ci        test_conf($conf,
135e1051a39Sopenharmony_ci                  0,
136e1051a39Sopenharmony_ci                  defined($skip{$conf}) ? $skip{$conf} : $no_tls,
137e1051a39Sopenharmony_ci                  "fips") unless $no_fips;
138e1051a39Sopenharmony_ci    }
139e1051a39Sopenharmony_ci}
140e1051a39Sopenharmony_ci
141e1051a39Sopenharmony_cisub test_conf {
142e1051a39Sopenharmony_ci    my ($conf, $check_source, $skip, $provider) = @_;
143e1051a39Sopenharmony_ci
144e1051a39Sopenharmony_ci    my $conf_file = srctop_file("test", "ssl-tests", $conf);
145e1051a39Sopenharmony_ci    my $input_file = $conf_file . ".in";
146e1051a39Sopenharmony_ci    my $output_file = $conf . "." . $provider;
147e1051a39Sopenharmony_ci    my $run_test = 1;
148e1051a39Sopenharmony_ci
149e1051a39Sopenharmony_ci  SKIP: {
150e1051a39Sopenharmony_ci      # "Test" 1. Generate the source.
151e1051a39Sopenharmony_ci      skip 'failure', 2 unless
152e1051a39Sopenharmony_ci        ok(run(perltest(["generate_ssl_tests.pl", $input_file, $provider],
153e1051a39Sopenharmony_ci                        interpreter_args => [ "-I", srctop_dir("util", "perl")],
154e1051a39Sopenharmony_ci                        stdout => $output_file)),
155e1051a39Sopenharmony_ci           "Getting output from generate_ssl_tests.pl.");
156e1051a39Sopenharmony_ci
157e1051a39Sopenharmony_ci    SKIP: {
158e1051a39Sopenharmony_ci        # Test 2. Compare against existing output in test/ssl-tests/
159e1051a39Sopenharmony_ci        skip "Skipping generated source test for $conf", 1
160e1051a39Sopenharmony_ci          if !$check_source;
161e1051a39Sopenharmony_ci
162e1051a39Sopenharmony_ci        $run_test = is(cmp_text($output_file, $conf_file), 0,
163e1051a39Sopenharmony_ci                       "Comparing generated $output_file with $conf_file.");
164e1051a39Sopenharmony_ci      }
165e1051a39Sopenharmony_ci
166e1051a39Sopenharmony_ci      # Test 3. Run the test.
167e1051a39Sopenharmony_ci      skip "No tests available; skipping tests", 1 if $skip;
168e1051a39Sopenharmony_ci      skip "Stale sources; skipping tests", 1 if !$run_test;
169e1051a39Sopenharmony_ci
170e1051a39Sopenharmony_ci      my $msg = "running CTLOG_FILE=test/ct/log_list.cnf". # $ENV{CTLOG_FILE}.
171e1051a39Sopenharmony_ci          " TEST_CERTS_DIR=test/certs". # $ENV{TEST_CERTS_DIR}.
172e1051a39Sopenharmony_ci          " test/ssl_test test/ssl-tests/$conf $provider";
173e1051a39Sopenharmony_ci      if ($provider eq "fips") {
174e1051a39Sopenharmony_ci          ok(run(test(["ssl_test", $output_file, $provider,
175e1051a39Sopenharmony_ci                       srctop_file("test", "fips-and-base.cnf")])), $msg);
176e1051a39Sopenharmony_ci      } else {
177e1051a39Sopenharmony_ci          ok(run(test(["ssl_test", $output_file, $provider])), $msg);
178e1051a39Sopenharmony_ci      }
179e1051a39Sopenharmony_ci    }
180e1051a39Sopenharmony_ci}
181e1051a39Sopenharmony_ci
182e1051a39Sopenharmony_cisub cmp_text {
183e1051a39Sopenharmony_ci    return compare_text(@_, sub {
184e1051a39Sopenharmony_ci        $_[0] =~ s/\R//g;
185e1051a39Sopenharmony_ci        $_[1] =~ s/\R//g;
186e1051a39Sopenharmony_ci        return $_[0] ne $_[1];
187e1051a39Sopenharmony_ci    });
188e1051a39Sopenharmony_ci}
189