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