• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        'test_again_with_use_psa' => 1
22    },
23    'config-ccm-psk-dtls1_2.h' => {
24        'compat' => '-m dtls12 -f \'^TLS-PSK-WITH-AES-...-CCM-8\'',
25        'opt' => ' ',
26        'opt_needs_debug' => 1,
27        'test_again_with_use_psa' => 1
28    },
29    'config-mini-tls1_1.h' => {
30        'compat' => '-m tls1_1 -f \'^DES-CBC3-SHA$\|^TLS-RSA-WITH-3DES-EDE-CBC-SHA$\'', #',
31        ## Skip ssl-opt testing for now because ssl-opt.sh is missing a lot
32        ## of requires_xxx so it would try to run tests that don't apply.
33        # 'opt' => ' ',
34        # 'opt_needs_debug' => 1,
35        'test_again_with_use_psa' => 1
36    },
37    'config-no-entropy.h' => {
38    },
39    'config-suite-b.h' => {
40        'compat' => "-m tls12 -f 'ECDHE-ECDSA.*AES.*GCM' -p mbedTLS",
41        'test_again_with_use_psa' => 1,
42        'opt' => ' ',
43        'opt_needs_debug' => 1,
44    },
45    'config-symmetric-only.h' => {
46        'test_again_with_use_psa' => 0, # Uses PSA by default, no need to test it twice
47    },
48    'config-thread.h' => {
49        'opt' => '-f ECJPAKE.*nolog',
50        'test_again_with_use_psa' => 1,
51    },
52);
53
54# If no config-name is provided, use all known configs.
55# Otherwise, use the provided names only.
56my @configs_to_test = sort keys %configs;
57if ($#ARGV >= 0) {
58    foreach my $conf_name ( @ARGV ) {
59        if( ! exists $configs{$conf_name} ) {
60            die "Unknown configuration: $conf_name\n";
61        }
62    }
63    @configs_to_test = @ARGV;
64}
65
66-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
67
68my $config_h = 'include/mbedtls/config.h';
69
70system( "cp $config_h $config_h.bak" ) and die;
71sub abort {
72    system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
73    # use an exit code between 1 and 124 for git bisect (die returns 255)
74    warn $_[0];
75    exit 1;
76}
77
78# Create a seedfile for configurations that enable MBEDTLS_ENTROPY_NV_SEED.
79# For test purposes, this doesn't have to be cryptographically random.
80if (!-e "tests/seedfile" || -s "tests/seedfile" < 64) {
81    local *SEEDFILE;
82    open SEEDFILE, ">tests/seedfile" or die;
83    print SEEDFILE "*" x 64 or die;
84    close SEEDFILE or die;
85}
86
87sub perform_test {
88    my $conf_file = $_[0];
89    my $data = $_[1];
90    my $test_with_psa = $_[2];
91
92    my $conf_name = $conf_file;
93    if ( $test_with_psa )
94    {
95        $conf_name .= "+PSA";
96    }
97
98    system( "cp $config_h.bak $config_h" ) and die;
99    system( "make clean" ) and die;
100
101    print "\n******************************************\n";
102    print "* Testing configuration: $conf_name\n";
103    print "******************************************\n";
104
105    $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
106
107    system( "cp configs/$conf_file $config_h" )
108        and abort "Failed to activate $conf_file\n";
109
110    if ( $test_with_psa )
111    {
112        system( "scripts/config.py set MBEDTLS_PSA_CRYPTO_C" );
113        system( "scripts/config.py set MBEDTLS_USE_PSA_CRYPTO" );
114    }
115
116    system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
117    system( "make test" ) and abort "Failed test suite: $conf_name\n";
118
119    my $compat = $data->{'compat'};
120    if( $compat )
121    {
122        print "\nrunning compat.sh $compat ($conf_name)\n";
123        system( "tests/compat.sh $compat" )
124            and abort "Failed compat.sh: $conf_name\n";
125    }
126    else
127    {
128        print "\nskipping compat.sh ($conf_name)\n";
129    }
130
131    my $opt = $data->{'opt'};
132    if( $opt )
133    {
134        if( $data->{'opt_needs_debug'} )
135        {
136            print "\nrebuilding with debug traces for ssl-opt ($conf_name)\n";
137            $conf_name .= '+DEBUG';
138            $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
139            system( "make clean" );
140            system( "scripts/config.py set MBEDTLS_DEBUG_C" );
141            system( "scripts/config.py set MBEDTLS_ERROR_C" );
142            system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
143        }
144
145        print "\nrunning ssl-opt.sh $opt ($conf_name)\n";
146        system( "tests/ssl-opt.sh $opt" )
147            and abort "Failed ssl-opt.sh: $conf_name\n";
148    }
149    else
150    {
151        print "\nskipping ssl-opt.sh ($conf_name)\n";
152    }
153}
154
155foreach my $conf ( @configs_to_test ) {
156    my $test_with_psa = $configs{$conf}{'test_again_with_use_psa'};
157    if ( $test_with_psa )
158    {
159        perform_test( $conf, $configs{$conf}, $test_with_psa );
160    }
161    perform_test( $conf, $configs{$conf}, 0 );
162}
163
164system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
165system( "make clean" );
166exit 0;
167