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
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20# Purpose
21#
22# For each reference configuration file in the configs directory, build the
23# configuration, run the test suites and compat.sh
24#
25# Usage: tests/scripts/test-ref-configs.pl [config-name [...]]
26
27use warnings;
28use strict;
29
30my %configs = (
31    'config-ccm-psk-tls1_2.h' => {
32        'compat' => '-m tls12 -f \'^TLS-PSK-WITH-AES-...-CCM-8\'',
33        'test_again_with_use_psa' => 1
34    },
35    'config-ccm-psk-dtls1_2.h' => {
36        'compat' => '-m dtls12 -f \'^TLS-PSK-WITH-AES-...-CCM-8\'',
37        'opt' => ' ',
38        'opt_needs_debug' => 1,
39        'test_again_with_use_psa' => 1
40    },
41    'config-no-entropy.h' => {
42    },
43    'config-suite-b.h' => {
44        'compat' => "-m tls12 -f 'ECDHE-ECDSA.*AES.*GCM' -p mbedTLS",
45        'test_again_with_use_psa' => 1,
46        'opt' => ' ',
47        'opt_needs_debug' => 1,
48    },
49    'config-symmetric-only.h' => {
50        'test_again_with_use_psa' => 0, # Uses PSA by default, no need to test it twice
51    },
52    'config-thread.h' => {
53        'opt' => '-f ECJPAKE.*nolog',
54        'test_again_with_use_psa' => 1,
55    },
56);
57
58# If no config-name is provided, use all known configs.
59# Otherwise, use the provided names only.
60my @configs_to_test = sort keys %configs;
61if ($#ARGV >= 0) {
62    foreach my $conf_name ( @ARGV ) {
63        if( ! exists $configs{$conf_name} ) {
64            die "Unknown configuration: $conf_name\n";
65        }
66    }
67    @configs_to_test = @ARGV;
68}
69
70-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
71
72my $config_h = 'include/mbedtls/mbedtls_config.h';
73
74system( "cp $config_h $config_h.bak" ) and die;
75sub abort {
76    system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
77    # use an exit code between 1 and 124 for git bisect (die returns 255)
78    warn $_[0];
79    exit 1;
80}
81
82# Create a seedfile for configurations that enable MBEDTLS_ENTROPY_NV_SEED.
83# For test purposes, this doesn't have to be cryptographically random.
84if (!-e "tests/seedfile" || -s "tests/seedfile" < 64) {
85    local *SEEDFILE;
86    open SEEDFILE, ">tests/seedfile" or die;
87    print SEEDFILE "*" x 64 or die;
88    close SEEDFILE or die;
89}
90
91sub perform_test {
92    my $conf_file = $_[0];
93    my $data = $_[1];
94    my $test_with_psa = $_[2];
95
96    my $conf_name = $conf_file;
97    if ( $test_with_psa )
98    {
99        $conf_name .= "+PSA";
100    }
101
102    system( "cp $config_h.bak $config_h" ) and die;
103    system( "make clean" ) and die;
104
105    print "\n******************************************\n";
106    print "* Testing configuration: $conf_name\n";
107    print "******************************************\n";
108
109    $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
110
111    system( "cp configs/$conf_file $config_h" )
112        and abort "Failed to activate $conf_file\n";
113
114    if ( $test_with_psa )
115    {
116        system( "scripts/config.py set MBEDTLS_PSA_CRYPTO_C" );
117        system( "scripts/config.py set MBEDTLS_USE_PSA_CRYPTO" );
118    }
119
120    system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
121    system( "make test" ) and abort "Failed test suite: $conf_name\n";
122
123    my $compat = $data->{'compat'};
124    if( $compat )
125    {
126        print "\nrunning compat.sh $compat ($conf_name)\n";
127        system( "tests/compat.sh $compat" )
128            and abort "Failed compat.sh: $conf_name\n";
129    }
130    else
131    {
132        print "\nskipping compat.sh ($conf_name)\n";
133    }
134
135    my $opt = $data->{'opt'};
136    if( $opt )
137    {
138        if( $data->{'opt_needs_debug'} )
139        {
140            print "\nrebuilding with debug traces for ssl-opt ($conf_name)\n";
141            $conf_name .= '+DEBUG';
142            $ENV{MBEDTLS_TEST_CONFIGURATION} = $conf_name;
143            system( "make clean" );
144            system( "scripts/config.py set MBEDTLS_DEBUG_C" );
145            system( "scripts/config.py set MBEDTLS_ERROR_C" );
146            system( "CFLAGS='-Os -Werror -Wall -Wextra' make" ) and abort "Failed to build: $conf_name\n";
147        }
148
149        print "\nrunning ssl-opt.sh $opt ($conf_name)\n";
150        system( "tests/ssl-opt.sh $opt" )
151            and abort "Failed ssl-opt.sh: $conf_name\n";
152    }
153    else
154    {
155        print "\nskipping ssl-opt.sh ($conf_name)\n";
156    }
157}
158
159foreach my $conf ( @configs_to_test ) {
160    my $test_with_psa = $configs{$conf}{'test_again_with_use_psa'};
161    if ( $test_with_psa )
162    {
163        perform_test( $conf, $configs{$conf}, $test_with_psa );
164    }
165    perform_test( $conf, $configs{$conf}, 0 );
166}
167
168system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
169system( "make clean" );
170exit 0;
171