blob: 08d99ab8346d28dbf15963e0d4568fc3542666af [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +02002
3# depends-hashes.pl
4#
5# Copyright (c) 2017, ARM Limited, All Rights Reserved
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02006# 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# This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020021#
22# Purpose
23#
24# To test the code dependencies on individual hashes in each test suite. This
25# is a verification step to ensure we don't ship test suites that do not work
26# for some build options.
27#
28# The process is:
29# for each possible hash
30# build the library and test suites with the hash disabled
31# execute the test suites
32#
33# And any test suite with the wrong dependencies will fail.
34#
35# Usage: tests/scripts/depends-hashes.pl
36#
37# This script should be executed from the root of the project directory.
Manuel Pégourié-Gonnard9ba9dfb2017-06-06 11:51:34 +020038#
39# For best effect, run either with cmake disabled, or cmake enabled in a mode
40# that includes -Werror.
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020041
42use warnings;
43use strict;
44
45-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
46
47my $config_h = 'include/mbedtls/config.h';
48
49# as many SSL options depend on specific hashes,
50# and SSL is not in the test suites anyways,
51# disable it to avoid dependcies issues
52my $ssl_sed_cmd = 's/^#define \(MBEDTLS_SSL.*\)/\1/p';
53my @ssl = split( /\s+/, `sed -n -e '$ssl_sed_cmd' $config_h` );
54
Manuel Pégourié-Gonnard7766a2c2017-08-21 10:57:57 +020055# for md we want to catch MD5_C but not MD_C, hence the extra dot
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020056my $mdx_sed_cmd = 's/^#define \(MBEDTLS_MD..*_C\)/\1/p';
57my $sha_sed_cmd = 's/^#define \(MBEDTLS_SHA.*_C\)/\1/p';
Manuel Pégourié-Gonnard20f236d2019-09-11 10:01:10 +020058my @hash_modules = split( /\s+/,
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020059 `sed -n -e '$mdx_sed_cmd' -e '$sha_sed_cmd' $config_h` );
Manuel Pégourié-Gonnard20f236d2019-09-11 10:01:10 +020060
61# there are also negative options for truncated variants, disabled by default
62my $sha_trunc_sed_cmd = 's/^\/\/#define \(MBEDTLS_SHA..._NO_.*\)/\1/p';
63my @hash_negatives = split( /\s+/,
64 `sed -n -e '$sha_trunc_sed_cmd' $config_h` );
65
66# list hash options with corresponding actions
67my @hashes = ((map { "unset $_" } @hash_modules),
68 (map { "set $_" } @hash_negatives));
69
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020070system( "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";
Manuel Pégourié-Gonnard254eec82017-10-26 09:47:36 +020073 # use an exit code between 1 and 124 for git bisect (die returns 255)
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +020074 warn $_[0];
75 exit 1;
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020076}
77
78for my $hash (@hashes) {
79 system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n";
80 system( "make clean" ) and die;
81
82 print "\n******************************************\n";
Manuel Pégourié-Gonnard20f236d2019-09-11 10:01:10 +020083 print "* Testing hash option: $hash\n";
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020084 print "******************************************\n";
Gilles Peskine9004a172019-09-16 15:20:36 +020085 $ENV{MBEDTLS_TEST_CONFIGURATION} = "-$hash";
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020086
Manuel Pégourié-Gonnard20f236d2019-09-11 10:01:10 +020087 system( "scripts/config.py $hash" )
88 and abort "Failed to $hash\n";
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020089
90 for my $opt (@ssl) {
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020091 system( "scripts/config.py unset $opt" )
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020092 and abort "Failed to disable $opt\n";
93 }
94
95 system( "CFLAGS='-Werror -Wall -Wextra' make lib" )
96 and abort "Failed to build lib: $hash\n";
97 system( "cd tests && make" ) and abort "Failed to build tests: $hash\n";
98 system( "make test" ) and abort "Failed test suite: $hash\n";
99}
100
101system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n";
102system( "make clean" ) and die;
103exit 0;