blob: 9198293d4662c6424e18b102e4caa7e2bef7b9e2 [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 Cron64027622024-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 Cron64027622024-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 },
Manuel Pégourié-Gonnardeb47b872015-10-20 14:07:03 +020027 'config-suite-b.h' => {
Ronald Cron64027622024-05-14 10:51:27 +020028 'compat' => "-m tls12 -f 'ECDHE_ECDSA.*AES.*GCM' -p mbedTLS",
Gilles Peskinec6d197b2022-02-25 21:00:16 +010029 'opt' => ' ',
30 'opt_needs_debug' => 1,
Manuel Pégourié-Gonnardeb47b872015-10-20 14:07:03 +020031 },
Gilles Peskine12230eb2020-02-26 19:02:33 +010032 'config-symmetric-only.h' => {
33 },
Gilles Peskine5baf6672023-09-06 17:17:10 +020034 'config-tfm.h' => {
Gilles Peskine5baf6672023-09-06 17:17:10 +020035 },
Gilles Peskine12230eb2020-02-26 19:02:33 +010036 'config-thread.h' => {
Gilles Peskine7dc97042020-02-26 19:48:43 +010037 'opt' => '-f ECJPAKE.*nolog',
Gilles Peskine12230eb2020-02-26 19:02:33 +010038 },
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020039);
40
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020041# If no config-name is provided, use all known configs.
42# Otherwise, use the provided names only.
Gilles Peskined7df8772022-02-26 18:16:07 +010043my @configs_to_test = sort keys %configs;
Paul Bakker30a30622013-12-19 17:09:49 +010044if ($#ARGV >= 0) {
Gilles Peskined7df8772022-02-26 18:16:07 +010045 foreach my $conf_name ( @ARGV ) {
46 if( ! exists $configs{$conf_name} ) {
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020047 die "Unknown configuration: $conf_name\n";
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020048 }
Paul Bakker30a30622013-12-19 17:09:49 +010049 }
Gilles Peskined7df8772022-02-26 18:16:07 +010050 @configs_to_test = @ARGV;
Paul Bakker30a30622013-12-19 17:09:49 +010051}
52
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020053-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
54
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020055my $config_h = 'include/mbedtls/mbedtls_config.h';
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020056
57system( "cp $config_h $config_h.bak" ) and die;
58sub abort {
59 system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
Manuel Pégourié-Gonnard254eec82017-10-26 09:47:36 +020060 # use an exit code between 1 and 124 for git bisect (die returns 255)
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +020061 warn $_[0];
62 exit 1;
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020063}
64
Gilles Peskine581bfcf2019-10-11 17:19:45 +020065# Create a seedfile for configurations that enable MBEDTLS_ENTROPY_NV_SEED.
66# For test purposes, this doesn't have to be cryptographically random.
67if (!-e "tests/seedfile" || -s "tests/seedfile" < 64) {
68 local *SEEDFILE;
69 open SEEDFILE, ">tests/seedfile" or die;
70 print SEEDFILE "*" x 64 or die;
71 close SEEDFILE or die;
72}
73
Andrzej Kurek01005b92022-01-17 15:32:02 +010074sub perform_test {
Gilles Peskinea2665412022-03-14 19:05:48 +010075 my $conf_file = $_[0];
Andrzej Kurek01005b92022-01-17 15:32:02 +010076 my $data = $_[1];
77 my $test_with_psa = $_[2];
78
Gilles Peskinea2665412022-03-14 19:05:48 +010079 my $conf_name = $conf_file;
80 if ( $test_with_psa )
81 {
82 $conf_name .= "+PSA";
83 }
84
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020085 system( "cp $config_h.bak $config_h" ) and die;
86 system( "make clean" ) and die;
87
88 print "\n******************************************\n";
Gilles Peskinea2665412022-03-14 19:05:48 +010089 print "* Testing configuration: $conf_name\n";
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020090 print "******************************************\n";
Andrzej Kurek01005b92022-01-17 15:32:02 +010091
Gilles Peskinea2665412022-03-14 19:05:48 +010092 $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020093
Gilles Peskinea2665412022-03-14 19:05:48 +010094 system( "cp configs/$conf_file $config_h" )
95 and abort "Failed to activate $conf_file\n";
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020096
Andrzej Kurek01005b92022-01-17 15:32:02 +010097 if ( $test_with_psa )
98 {
99 system( "scripts/config.py set MBEDTLS_PSA_CRYPTO_C" );
100 system( "scripts/config.py set MBEDTLS_USE_PSA_CRYPTO" );
101 }
102
Gilles Peskinea2665412022-03-14 19:05:48 +0100103 system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
104 system( "make test" ) and abort "Failed test suite: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100105
106 my $compat = $data->{'compat'};
107 if( $compat )
108 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100109 print "\nrunning compat.sh $compat ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100110 system( "tests/compat.sh $compat" )
Gilles Peskinea2665412022-03-14 19:05:48 +0100111 and abort "Failed compat.sh: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100112 }
113 else
114 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100115 print "\nskipping compat.sh ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100116 }
117
118 my $opt = $data->{'opt'};
119 if( $opt )
120 {
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100121 if( $data->{'opt_needs_debug'} )
122 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100123 print "\nrebuilding with debug traces for ssl-opt ($conf_name)\n";
124 $conf_name .= '+DEBUG';
125 $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100126 system( "make clean" );
127 system( "scripts/config.py set MBEDTLS_DEBUG_C" );
128 system( "scripts/config.py set MBEDTLS_ERROR_C" );
Gilles Peskinea2665412022-03-14 19:05:48 +0100129 system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100130 }
131
Gilles Peskinea2665412022-03-14 19:05:48 +0100132 print "\nrunning ssl-opt.sh $opt ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100133 system( "tests/ssl-opt.sh $opt" )
Gilles Peskinea2665412022-03-14 19:05:48 +0100134 and abort "Failed ssl-opt.sh: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100135 }
136 else
137 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100138 print "\nskipping ssl-opt.sh ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100139 }
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200140}
141
Gilles Peskined7df8772022-02-26 18:16:07 +0100142foreach my $conf ( @configs_to_test ) {
Ronald Cronb30cd3b2024-05-17 14:11:31 +0200143 system("grep '//#define MBEDTLS_USE_PSA_CRYPTO' configs/$conf > /dev/null");
144 die "grep ... configs/$conf: $!" if $? != 0 && $? != 0x100;
145 my $test_with_psa = $? == 0;
Ronald Cron4dd66312024-05-15 11:22:04 +0200146
Andrzej Kurek01005b92022-01-17 15:32:02 +0100147 if ( $test_with_psa )
148 {
Gilles Peskined7df8772022-02-26 18:16:07 +0100149 perform_test( $conf, $configs{$conf}, $test_with_psa );
Andrzej Kurek01005b92022-01-17 15:32:02 +0100150 }
Gilles Peskined7df8772022-02-26 18:16:07 +0100151 perform_test( $conf, $configs{$conf}, 0 );
Andrzej Kurek01005b92022-01-17 15:32:02 +0100152}
153
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200154system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
155system( "make clean" );
156exit 0;