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