blob: 9fd98be8d02379cf23b4bbb0f1d8693ee1f571d5 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +02002
Simon Butcherf95c1762016-11-10 17:25:58 +00003# test-ref-configs.pl
4#
Bence Szépkúti1e148272020-08-07 13:07:28 +02005# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02006# SPDX-License-Identifier: Apache-2.0
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
Simon Butcherf95c1762016-11-10 17:25:58 +000020# Purpose
21#
22# For each reference configuration file in the configs directory, build the
Gilles Peskine7dc97042020-02-26 19:48:43 +010023# configuration, run the test suites and compat.sh
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020024#
25# Usage: tests/scripts/test-ref-configs.pl [config-name [...]]
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020026
27use warnings;
28use strict;
29
30my %configs = (
Gilles Peskine7a78a1f2020-11-09 14:44:04 +010031 'config-ccm-psk-tls1_2.h' => {
Xiaofei Bai8b5c3822021-12-02 08:43:35 +000032 'compat' => '-m tls12 -f \'^TLS-PSK-WITH-AES-...-CCM-8\'',
Andrzej Kurek01005b92022-01-17 15:32:02 +010033 'test_again_with_use_psa' => 1
Gilles Peskine7a78a1f2020-11-09 14:44:04 +010034 },
Gilles Peskine168f17c2022-02-25 19:28:00 +010035 'config-ccm-psk-dtls1_2.h' => {
36 'compat' => '-m dtls12 -f \'^TLS-PSK-WITH-AES-...-CCM-8\'',
Gilles Peskinec6d197b2022-02-25 21:00:16 +010037 'opt' => ' ',
38 'opt_needs_debug' => 1,
Gilles Peskine168f17c2022-02-25 19:28:00 +010039 'test_again_with_use_psa' => 1
40 },
Gilles Peskine25fdebf2020-11-09 15:15:17 +010041 'config-no-entropy.h' => {
42 },
Manuel Pégourié-Gonnardeb47b872015-10-20 14:07:03 +020043 'config-suite-b.h' => {
Xiaofei Bai8b5c3822021-12-02 08:43:35 +000044 'compat' => "-m tls12 -f 'ECDHE-ECDSA.*AES.*GCM' -p mbedTLS",
Andrzej Kurek01005b92022-01-17 15:32:02 +010045 'test_again_with_use_psa' => 1,
Gilles Peskinec6d197b2022-02-25 21:00:16 +010046 'opt' => ' ',
47 'opt_needs_debug' => 1,
Manuel Pégourié-Gonnardeb47b872015-10-20 14:07:03 +020048 },
Gilles Peskine12230eb2020-02-26 19:02:33 +010049 'config-symmetric-only.h' => {
Andrzej Kurek01005b92022-01-17 15:32:02 +010050 'test_again_with_use_psa' => 0, # Uses PSA by default, no need to test it twice
Gilles Peskine12230eb2020-02-26 19:02:33 +010051 },
Gilles Peskine12230eb2020-02-26 19:02:33 +010052 'config-thread.h' => {
Gilles Peskine7dc97042020-02-26 19:48:43 +010053 'opt' => '-f ECJPAKE.*nolog',
Andrzej Kurek01005b92022-01-17 15:32:02 +010054 'test_again_with_use_psa' => 1,
Gilles Peskine12230eb2020-02-26 19:02:33 +010055 },
Yanray Wang0c98f9f2023-09-06 15:47:49 +080056 'config-tfm.h' => {
57 'test_again_with_use_psa' => 0, # Uses PSA by default, no need to test it twice
58 },
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020059);
60
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020061# If no config-name is provided, use all known configs.
62# Otherwise, use the provided names only.
Gilles Peskined7df8772022-02-26 18:16:07 +010063my @configs_to_test = sort keys %configs;
Paul Bakker30a30622013-12-19 17:09:49 +010064if ($#ARGV >= 0) {
Gilles Peskined7df8772022-02-26 18:16:07 +010065 foreach my $conf_name ( @ARGV ) {
66 if( ! exists $configs{$conf_name} ) {
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020067 die "Unknown configuration: $conf_name\n";
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020068 }
Paul Bakker30a30622013-12-19 17:09:49 +010069 }
Gilles Peskined7df8772022-02-26 18:16:07 +010070 @configs_to_test = @ARGV;
Paul Bakker30a30622013-12-19 17:09:49 +010071}
72
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020073-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
74
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020075my $config_h = 'include/mbedtls/mbedtls_config.h';
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020076
77system( "cp $config_h $config_h.bak" ) and die;
78sub abort {
79 system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
Manuel Pégourié-Gonnard254eec82017-10-26 09:47:36 +020080 # use an exit code between 1 and 124 for git bisect (die returns 255)
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +020081 warn $_[0];
82 exit 1;
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020083}
84
Gilles Peskine581bfcf2019-10-11 17:19:45 +020085# Create a seedfile for configurations that enable MBEDTLS_ENTROPY_NV_SEED.
86# For test purposes, this doesn't have to be cryptographically random.
87if (!-e "tests/seedfile" || -s "tests/seedfile" < 64) {
88 local *SEEDFILE;
89 open SEEDFILE, ">tests/seedfile" or die;
90 print SEEDFILE "*" x 64 or die;
91 close SEEDFILE or die;
92}
93
Andrzej Kurek01005b92022-01-17 15:32:02 +010094sub perform_test {
Gilles Peskinea2665412022-03-14 19:05:48 +010095 my $conf_file = $_[0];
Andrzej Kurek01005b92022-01-17 15:32:02 +010096 my $data = $_[1];
97 my $test_with_psa = $_[2];
98
Gilles Peskinea2665412022-03-14 19:05:48 +010099 my $conf_name = $conf_file;
100 if ( $test_with_psa )
101 {
102 $conf_name .= "+PSA";
103 }
104
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200105 system( "cp $config_h.bak $config_h" ) and die;
106 system( "make clean" ) and die;
107
108 print "\n******************************************\n";
Gilles Peskinea2665412022-03-14 19:05:48 +0100109 print "* Testing configuration: $conf_name\n";
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200110 print "******************************************\n";
Andrzej Kurek01005b92022-01-17 15:32:02 +0100111
Gilles Peskinea2665412022-03-14 19:05:48 +0100112 $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200113
Gilles Peskinea2665412022-03-14 19:05:48 +0100114 system( "cp configs/$conf_file $config_h" )
115 and abort "Failed to activate $conf_file\n";
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200116
Andrzej Kurek01005b92022-01-17 15:32:02 +0100117 if ( $test_with_psa )
118 {
119 system( "scripts/config.py set MBEDTLS_PSA_CRYPTO_C" );
120 system( "scripts/config.py set MBEDTLS_USE_PSA_CRYPTO" );
121 }
122
Gilles Peskinea2665412022-03-14 19:05:48 +0100123 system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
124 system( "make test" ) and abort "Failed test suite: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100125
126 my $compat = $data->{'compat'};
127 if( $compat )
128 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100129 print "\nrunning compat.sh $compat ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100130 system( "tests/compat.sh $compat" )
Gilles Peskinea2665412022-03-14 19:05:48 +0100131 and abort "Failed compat.sh: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100132 }
133 else
134 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100135 print "\nskipping compat.sh ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100136 }
137
138 my $opt = $data->{'opt'};
139 if( $opt )
140 {
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100141 if( $data->{'opt_needs_debug'} )
142 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100143 print "\nrebuilding with debug traces for ssl-opt ($conf_name)\n";
144 $conf_name .= '+DEBUG';
145 $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100146 system( "make clean" );
147 system( "scripts/config.py set MBEDTLS_DEBUG_C" );
148 system( "scripts/config.py set MBEDTLS_ERROR_C" );
Gilles Peskinea2665412022-03-14 19:05:48 +0100149 system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100150 }
151
Gilles Peskinea2665412022-03-14 19:05:48 +0100152 print "\nrunning ssl-opt.sh $opt ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100153 system( "tests/ssl-opt.sh $opt" )
Gilles Peskinea2665412022-03-14 19:05:48 +0100154 and abort "Failed ssl-opt.sh: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100155 }
156 else
157 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100158 print "\nskipping ssl-opt.sh ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100159 }
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200160}
161
Gilles Peskined7df8772022-02-26 18:16:07 +0100162foreach my $conf ( @configs_to_test ) {
163 my $test_with_psa = $configs{$conf}{'test_again_with_use_psa'};
Andrzej Kurek01005b92022-01-17 15:32:02 +0100164 if ( $test_with_psa )
165 {
Gilles Peskined7df8772022-02-26 18:16:07 +0100166 perform_test( $conf, $configs{$conf}, $test_with_psa );
Andrzej Kurek01005b92022-01-17 15:32:02 +0100167 }
Gilles Peskined7df8772022-02-26 18:16:07 +0100168 perform_test( $conf, $configs{$conf}, 0 );
Andrzej Kurek01005b92022-01-17 15:32:02 +0100169}
170
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200171system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
172system( "make clean" );
173exit 0;