1#!/usr/bin/env perl
2
3# test-ref-configs.pl
4#
5# Copyright The Mbed TLS Contributors
6# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7#
8# Purpose
9#
10# For each reference configuration file in the configs directory, build the
11# configuration, run the test suites and compat.sh
12#
13# Usage: tests/scripts/test-ref-configs.pl [config-name [...]]
14
15use warnings;
16use strict;
17
18my %configs = (
19    'config-ccm-psk-tls1_2.h' => {
20        'compat' => '-m tls12 -f \'^TLS_PSK_WITH_AES_..._CCM_8\'',
21    },
22    'config-ccm-psk-dtls1_2.h' => {
23        'compat' => '-m dtls12 -f \'^TLS_PSK_WITH_AES_..._CCM_8\'',
24        'opt' => ' ',
25        'opt_needs_debug' => 1,
26    },
27    'config-no-entropy.h' => {
28    },
29    'config-suite-b.h' => {
30        'compat' => "-m tls12 -f 'ECDHE_ECDSA.*AES.*GCM' -p mbedTLS",
31        'opt' => ' ',
32        'opt_needs_debug' => 1,
33    },
34    'config-symmetric-only.h' => {
35    },
36    'config-tfm.h' => {
37    },
38    'config-thread.h' => {
39        'opt' => '-f ECJPAKE.*nolog',
40    },
41);
42
43# If no config-name is provided, use all known configs.
44# Otherwise, use the provided names only.
45my @configs_to_test = sort keys %configs;
46if ($#ARGV >= 0) {
47    foreach my $conf_name ( @ARGV ) {
48        if( ! exists $configs{$conf_name} ) {
49            die "Unknown configuration: $conf_name\n";
50        }
51    }
52    @configs_to_test = @ARGV;
53}
54
55-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
56
57my $config_h = 'include/mbedtls/mbedtls_config.h';
58
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";
62    # use an exit code between 1 and 124 for git bisect (die returns 255)
63    warn $_[0];
64    exit 1;
65}
66
67# 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
76sub perform_test {
77    my $conf_file = $_[0];
78    my $data = $_[1];
79    my $test_with_psa = $_[2];
80
81    my $conf_name = $conf_file;
82    if ( $test_with_psa )
83    {
84        $conf_name .= "+PSA";
85    }
86
87    system( "cp $config_h.bak $config_h" ) and die;
88    system( "make clean" ) and die;
89
90    print "\n******************************************\n";
91    print "* Testing configuration: $conf_name\n";
92    print "******************************************\n";
93
94    $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
95
96    system( "cp configs/$conf_file $config_h" )
97        and abort "Failed to activate $conf_file\n";
98
99    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
105    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";
107
108    my $compat = $data->{'compat'};
109    if( $compat )
110    {
111        print "\nrunning compat.sh $compat ($conf_name)\n";
112        system( "tests/compat.sh $compat" )
113            and abort "Failed compat.sh: $conf_name\n";
114    }
115    else
116    {
117        print "\nskipping compat.sh ($conf_name)\n";
118    }
119
120    my $opt = $data->{'opt'};
121    if( $opt )
122    {
123        if( $data->{'opt_needs_debug'} )
124        {
125            print "\nrebuilding with debug traces for ssl-opt ($conf_name)\n";
126            $conf_name .= '+DEBUG';
127            $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
128            system( "make clean" );
129            system( "scripts/config.py set MBEDTLS_DEBUG_C" );
130            system( "scripts/config.py set MBEDTLS_ERROR_C" );
131            system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
132        }
133
134        print "\nrunning ssl-opt.sh $opt ($conf_name)\n";
135        system( "tests/ssl-opt.sh $opt" )
136            and abort "Failed ssl-opt.sh: $conf_name\n";
137    }
138    else
139    {
140        print "\nskipping ssl-opt.sh ($conf_name)\n";
141    }
142}
143
144foreach my $conf ( @configs_to_test ) {
145    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;
148
149    if ( $test_with_psa )
150    {
151        perform_test( $conf, $configs{$conf}, $test_with_psa );
152    }
153    perform_test( $conf, $configs{$conf}, 0 );
154}
155
156system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
157system( "make clean" );
158exit 0;
159