blob: 5557de327672689044e923272301136d19a6b1ba [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
Dave Rodgman16799db2023-11-02 19:47:20 +00006# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02007#
Simon Butcherf95c1762016-11-10 17:25:58 +00008# Purpose
9#
10# For each reference configuration file in the configs directory, build the
Gilles Peskine7dc97042020-02-26 19:48:43 +010011# configuration, run the test suites and compat.sh
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020012#
13# Usage: tests/scripts/test-ref-configs.pl [config-name [...]]
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020014
15use warnings;
16use strict;
17
18my %configs = (
Gilles Peskine7a78a1f2020-11-09 14:44:04 +010019 'config-ccm-psk-tls1_2.h' => {
Ronald Cronf47f5452024-05-14 10:51:27 +020020 'compat' => '-m tls12 -f \'^TLS_PSK_WITH_AES_..._CCM_8\'',
Gilles Peskine7a78a1f2020-11-09 14:44:04 +010021 },
Gilles Peskine168f17c2022-02-25 19:28:00 +010022 'config-ccm-psk-dtls1_2.h' => {
Ronald Cronf47f5452024-05-14 10:51:27 +020023 'compat' => '-m dtls12 -f \'^TLS_PSK_WITH_AES_..._CCM_8\'',
Gilles Peskinec6d197b2022-02-25 21:00:16 +010024 'opt' => ' ',
25 'opt_needs_debug' => 1,
Gilles Peskine168f17c2022-02-25 19:28:00 +010026 },
Gilles Peskine25fdebf2020-11-09 15:15:17 +010027 'config-no-entropy.h' => {
28 },
Manuel Pégourié-Gonnardeb47b872015-10-20 14:07:03 +020029 'config-suite-b.h' => {
Ronald Cronf47f5452024-05-14 10:51:27 +020030 'compat' => "-m tls12 -f 'ECDHE_ECDSA.*AES.*GCM' -p mbedTLS",
Gilles Peskinec6d197b2022-02-25 21:00:16 +010031 'opt' => ' ',
32 'opt_needs_debug' => 1,
Manuel Pégourié-Gonnardeb47b872015-10-20 14:07:03 +020033 },
Gilles Peskine12230eb2020-02-26 19:02:33 +010034 'config-symmetric-only.h' => {
35 },
Gilles Peskine5baf6672023-09-06 17:17:10 +020036 'config-tfm.h' => {
Gilles Peskine5baf6672023-09-06 17:17:10 +020037 },
Gilles Peskine12230eb2020-02-26 19:02:33 +010038 'config-thread.h' => {
Gilles Peskine7dc97042020-02-26 19:48:43 +010039 'opt' => '-f ECJPAKE.*nolog',
Gilles Peskine12230eb2020-02-26 19:02:33 +010040 },
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020041);
42
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020043# If no config-name is provided, use all known configs.
44# Otherwise, use the provided names only.
Gilles Peskined7df8772022-02-26 18:16:07 +010045my @configs_to_test = sort keys %configs;
Paul Bakker30a30622013-12-19 17:09:49 +010046if ($#ARGV >= 0) {
Gilles Peskined7df8772022-02-26 18:16:07 +010047 foreach my $conf_name ( @ARGV ) {
48 if( ! exists $configs{$conf_name} ) {
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020049 die "Unknown configuration: $conf_name\n";
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020050 }
Paul Bakker30a30622013-12-19 17:09:49 +010051 }
Gilles Peskined7df8772022-02-26 18:16:07 +010052 @configs_to_test = @ARGV;
Paul Bakker30a30622013-12-19 17:09:49 +010053}
54
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020055-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
56
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020057my $config_h = 'include/mbedtls/mbedtls_config.h';
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020058
59system( "cp $config_h $config_h.bak" ) and die;
60sub abort {
61 system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
Manuel Pégourié-Gonnard254eec82017-10-26 09:47:36 +020062 # use an exit code between 1 and 124 for git bisect (die returns 255)
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +020063 warn $_[0];
64 exit 1;
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020065}
66
Gilles Peskine581bfcf2019-10-11 17:19:45 +020067# Create a seedfile for configurations that enable MBEDTLS_ENTROPY_NV_SEED.
68# For test purposes, this doesn't have to be cryptographically random.
69if (!-e "tests/seedfile" || -s "tests/seedfile" < 64) {
70 local *SEEDFILE;
71 open SEEDFILE, ">tests/seedfile" or die;
72 print SEEDFILE "*" x 64 or die;
73 close SEEDFILE or die;
74}
75
Andrzej Kurek01005b92022-01-17 15:32:02 +010076sub perform_test {
Gilles Peskinea2665412022-03-14 19:05:48 +010077 my $conf_file = $_[0];
Andrzej Kurek01005b92022-01-17 15:32:02 +010078 my $data = $_[1];
79 my $test_with_psa = $_[2];
80
Gilles Peskinea2665412022-03-14 19:05:48 +010081 my $conf_name = $conf_file;
82 if ( $test_with_psa )
83 {
84 $conf_name .= "+PSA";
85 }
86
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020087 system( "cp $config_h.bak $config_h" ) and die;
88 system( "make clean" ) and die;
89
90 print "\n******************************************\n";
Gilles Peskinea2665412022-03-14 19:05:48 +010091 print "* Testing configuration: $conf_name\n";
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020092 print "******************************************\n";
Andrzej Kurek01005b92022-01-17 15:32:02 +010093
Gilles Peskinea2665412022-03-14 19:05:48 +010094 $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020095
Gilles Peskinea2665412022-03-14 19:05:48 +010096 system( "cp configs/$conf_file $config_h" )
97 and abort "Failed to activate $conf_file\n";
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020098
Andrzej Kurek01005b92022-01-17 15:32:02 +010099 if ( $test_with_psa )
100 {
101 system( "scripts/config.py set MBEDTLS_PSA_CRYPTO_C" );
102 system( "scripts/config.py set MBEDTLS_USE_PSA_CRYPTO" );
103 }
104
Gilles Peskinea2665412022-03-14 19:05:48 +0100105 system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
106 system( "make test" ) and abort "Failed test suite: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100107
108 my $compat = $data->{'compat'};
109 if( $compat )
110 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100111 print "\nrunning compat.sh $compat ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100112 system( "tests/compat.sh $compat" )
Gilles Peskinea2665412022-03-14 19:05:48 +0100113 and abort "Failed compat.sh: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100114 }
115 else
116 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100117 print "\nskipping compat.sh ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100118 }
119
120 my $opt = $data->{'opt'};
121 if( $opt )
122 {
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100123 if( $data->{'opt_needs_debug'} )
124 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100125 print "\nrebuilding with debug traces for ssl-opt ($conf_name)\n";
126 $conf_name .= '+DEBUG';
127 $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100128 system( "make clean" );
129 system( "scripts/config.py set MBEDTLS_DEBUG_C" );
130 system( "scripts/config.py set MBEDTLS_ERROR_C" );
Gilles Peskinea2665412022-03-14 19:05:48 +0100131 system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100132 }
133
Gilles Peskinea2665412022-03-14 19:05:48 +0100134 print "\nrunning ssl-opt.sh $opt ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100135 system( "tests/ssl-opt.sh $opt" )
Gilles Peskinea2665412022-03-14 19:05:48 +0100136 and abort "Failed ssl-opt.sh: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100137 }
138 else
139 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100140 print "\nskipping ssl-opt.sh ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100141 }
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200142}
143
Gilles Peskined7df8772022-02-26 18:16:07 +0100144foreach my $conf ( @configs_to_test ) {
Ronald Cron85267512024-05-17 14:11:31 +0200145 system("grep '//#define MBEDTLS_USE_PSA_CRYPTO' configs/$conf > /dev/null");
146 die "grep ... configs/$conf: $!" if $? != 0 && $? != 0x100;
147 my $test_with_psa = $? == 0;
Ronald Crone3283ed2024-05-15 11:22:04 +0200148
Andrzej Kurek01005b92022-01-17 15:32:02 +0100149 if ( $test_with_psa )
150 {
Gilles Peskined7df8772022-02-26 18:16:07 +0100151 perform_test( $conf, $configs{$conf}, $test_with_psa );
Andrzej Kurek01005b92022-01-17 15:32:02 +0100152 }
Gilles Peskined7df8772022-02-26 18:16:07 +0100153 perform_test( $conf, $configs{$conf}, 0 );
Andrzej Kurek01005b92022-01-17 15:32:02 +0100154}
155
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200156system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
157system( "make clean" );
158exit 0;