blob: cd17066fdd5a86ddad8aa4b4ff40071a9cf3c14c [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#
Bence Szépkúti1e148272020-08-07 13:07:28 +02005# Copyright The Mbed TLS Contributors
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#
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020020# Purpose
21#
22# To test the code dependencies on individual hashes in each test suite. This
23# is a verification step to ensure we don't ship test suites that do not work
24# for some build options.
25#
26# The process is:
27# for each possible hash
28# build the library and test suites with the hash disabled
29# execute the test suites
30#
31# And any test suite with the wrong dependencies will fail.
32#
33# Usage: tests/scripts/depends-hashes.pl
34#
35# This script should be executed from the root of the project directory.
Manuel Pégourié-Gonnard9ba9dfb2017-06-06 11:51:34 +020036#
37# For best effect, run either with cmake disabled, or cmake enabled in a mode
38# that includes -Werror.
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020039
40use warnings;
41use strict;
42
43-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
44
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020045my $config_h = 'include/mbedtls/mbedtls_config.h';
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020046
47# as many SSL options depend on specific hashes,
48# and SSL is not in the test suites anyways,
49# disable it to avoid dependcies issues
50my $ssl_sed_cmd = 's/^#define \(MBEDTLS_SSL.*\)/\1/p';
51my @ssl = split( /\s+/, `sed -n -e '$ssl_sed_cmd' $config_h` );
52
Mateusz Starzyk6fce30f2021-04-19 14:35:00 +020053# Each element of this array holds list of configuration options that
54# should be tested together. Certain options depend on eachother and
55# separating them would generate invalid configurations.
56my @hash_configs = (
Mateusz Starzyk6fce30f2021-04-19 14:35:00 +020057 ['unset MBEDTLS_MD5_C'],
58 ['unset MBEDTLS_SHA512_C', 'unset MBEDTLS_SHA384_C '],
59 ['unset MBEDTLS_SHA384_C'],
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020060 ['unset MBEDTLS_SHA256_C', 'unset MBEDTLS_SHA224_C'],
Mateusz Starzyk6fce30f2021-04-19 14:35:00 +020061 ['unset MBEDTLS_SHA1_C'],
62);
Manuel Pégourié-Gonnard20f236d2019-09-11 10:01:10 +020063
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020064system( "cp $config_h $config_h.bak" ) and die;
65sub abort {
66 system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
Manuel Pégourié-Gonnard254eec82017-10-26 09:47:36 +020067 # use an exit code between 1 and 124 for git bisect (die returns 255)
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +020068 warn $_[0];
69 exit 1;
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020070}
71
Mateusz Starzyk6fce30f2021-04-19 14:35:00 +020072for my $hash_config (@hash_configs) {
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020073 system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n";
74 system( "make clean" ) and die;
75
Mateusz Starzyk6fce30f2021-04-19 14:35:00 +020076 my $hash_config_string = join(', ', @$hash_config);
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020077
Mateusz Starzyk6fce30f2021-04-19 14:35:00 +020078 print "\n******************************************\n";
79 print "* Testing hash options: $hash_config_string\n";
80 print "******************************************\n";
81 $ENV{MBEDTLS_TEST_CONFIGURATION} = "-$hash_config_string";
82
83 for my $hash (@$hash_config) {
84 system( "scripts/config.py $hash" )
85 and abort "Failed to $hash\n";
86 }
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020087
88 for my $opt (@ssl) {
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020089 system( "scripts/config.py unset $opt" )
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020090 and abort "Failed to disable $opt\n";
91 }
92
93 system( "CFLAGS='-Werror -Wall -Wextra' make lib" )
Mateusz Starzyk6fce30f2021-04-19 14:35:00 +020094 and abort "Failed to build lib: $hash_config_string\n";
95 system( "cd tests && make" ) and abort "Failed to build tests: $hash_config_string\n";
96 system( "make test" ) and abort "Failed test suite: $hash_config_string\n";
Manuel Pégourié-Gonnard42a4d302017-06-06 10:54:01 +020097}
98
99system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n";
100system( "make clean" ) and die;
101exit 0;