blob: 2e9751077427451fb17c38162309f5807fb01a8f [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 = (
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020019);
20
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020021# If no config-name is provided, use all known configs.
22# Otherwise, use the provided names only.
Gilles Peskined7df8772022-02-26 18:16:07 +010023my @configs_to_test = sort keys %configs;
Paul Bakker30a30622013-12-19 17:09:49 +010024if ($#ARGV >= 0) {
Gilles Peskined7df8772022-02-26 18:16:07 +010025 foreach my $conf_name ( @ARGV ) {
26 if( ! exists $configs{$conf_name} ) {
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020027 die "Unknown configuration: $conf_name\n";
Manuel Pégourié-Gonnard827b6ce2014-04-30 12:05:29 +020028 }
Paul Bakker30a30622013-12-19 17:09:49 +010029 }
Gilles Peskined7df8772022-02-26 18:16:07 +010030 @configs_to_test = @ARGV;
Paul Bakker30a30622013-12-19 17:09:49 +010031}
32
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020033-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
34
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020035my $config_h = 'include/mbedtls/mbedtls_config.h';
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020036
37system( "cp $config_h $config_h.bak" ) and die;
38sub abort {
39 system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
Manuel Pégourié-Gonnard254eec82017-10-26 09:47:36 +020040 # use an exit code between 1 and 124 for git bisect (die returns 255)
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +020041 warn $_[0];
42 exit 1;
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020043}
44
Gilles Peskine581bfcf2019-10-11 17:19:45 +020045# Create a seedfile for configurations that enable MBEDTLS_ENTROPY_NV_SEED.
46# For test purposes, this doesn't have to be cryptographically random.
47if (!-e "tests/seedfile" || -s "tests/seedfile" < 64) {
48 local *SEEDFILE;
49 open SEEDFILE, ">tests/seedfile" or die;
50 print SEEDFILE "*" x 64 or die;
51 close SEEDFILE or die;
52}
53
Andrzej Kurek01005b92022-01-17 15:32:02 +010054sub perform_test {
Gilles Peskinea2665412022-03-14 19:05:48 +010055 my $conf_file = $_[0];
Andrzej Kurek01005b92022-01-17 15:32:02 +010056 my $data = $_[1];
57 my $test_with_psa = $_[2];
58
Gilles Peskinea2665412022-03-14 19:05:48 +010059 my $conf_name = $conf_file;
60 if ( $test_with_psa )
61 {
62 $conf_name .= "+PSA";
63 }
64
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020065 system( "cp $config_h.bak $config_h" ) and die;
66 system( "make clean" ) and die;
67
68 print "\n******************************************\n";
Gilles Peskinea2665412022-03-14 19:05:48 +010069 print "* Testing configuration: $conf_name\n";
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020070 print "******************************************\n";
Andrzej Kurek01005b92022-01-17 15:32:02 +010071
Gilles Peskinea2665412022-03-14 19:05:48 +010072 $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020073
Gilles Peskinea2665412022-03-14 19:05:48 +010074 system( "cp configs/$conf_file $config_h" )
75 and abort "Failed to activate $conf_file\n";
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +020076
Andrzej Kurek01005b92022-01-17 15:32:02 +010077 if ( $test_with_psa )
78 {
79 system( "scripts/config.py set MBEDTLS_PSA_CRYPTO_C" );
80 system( "scripts/config.py set MBEDTLS_USE_PSA_CRYPTO" );
81 }
82
Gilles Peskinea2665412022-03-14 19:05:48 +010083 system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
84 system( "make test" ) and abort "Failed test suite: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +010085
86 my $compat = $data->{'compat'};
87 if( $compat )
88 {
Gilles Peskinea2665412022-03-14 19:05:48 +010089 print "\nrunning compat.sh $compat ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +010090 system( "tests/compat.sh $compat" )
Gilles Peskinea2665412022-03-14 19:05:48 +010091 and abort "Failed compat.sh: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +010092 }
93 else
94 {
Gilles Peskinea2665412022-03-14 19:05:48 +010095 print "\nskipping compat.sh ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +010096 }
97
98 my $opt = $data->{'opt'};
99 if( $opt )
100 {
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100101 if( $data->{'opt_needs_debug'} )
102 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100103 print "\nrebuilding with debug traces for ssl-opt ($conf_name)\n";
104 $conf_name .= '+DEBUG';
105 $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100106 system( "make clean" );
107 system( "scripts/config.py set MBEDTLS_DEBUG_C" );
108 system( "scripts/config.py set MBEDTLS_ERROR_C" );
Gilles Peskinea2665412022-03-14 19:05:48 +0100109 system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
Gilles Peskinec6d197b2022-02-25 21:00:16 +0100110 }
111
Gilles Peskinea2665412022-03-14 19:05:48 +0100112 print "\nrunning ssl-opt.sh $opt ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100113 system( "tests/ssl-opt.sh $opt" )
Gilles Peskinea2665412022-03-14 19:05:48 +0100114 and abort "Failed ssl-opt.sh: $conf_name\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100115 }
116 else
117 {
Gilles Peskinea2665412022-03-14 19:05:48 +0100118 print "\nskipping ssl-opt.sh ($conf_name)\n";
Gilles Peskine7dc97042020-02-26 19:48:43 +0100119 }
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200120}
121
Gilles Peskined7df8772022-02-26 18:16:07 +0100122foreach my $conf ( @configs_to_test ) {
Ronald Cronb30cd3b2024-05-17 14:11:31 +0200123 system("grep '//#define MBEDTLS_USE_PSA_CRYPTO' configs/$conf > /dev/null");
124 die "grep ... configs/$conf: $!" if $? != 0 && $? != 0x100;
125 my $test_with_psa = $? == 0;
Ronald Cron4dd66312024-05-15 11:22:04 +0200126
Andrzej Kurek01005b92022-01-17 15:32:02 +0100127 if ( $test_with_psa )
128 {
Gilles Peskined7df8772022-02-26 18:16:07 +0100129 perform_test( $conf, $configs{$conf}, $test_with_psa );
Andrzej Kurek01005b92022-01-17 15:32:02 +0100130 }
Gilles Peskined7df8772022-02-26 18:16:07 +0100131 perform_test( $conf, $configs{$conf}, 0 );
Andrzej Kurek01005b92022-01-17 15:32:02 +0100132}
133
Manuel Pégourié-Gonnard64985402013-09-20 16:22:42 +0200134system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
135system( "make clean" );
136exit 0;