fbrosson | 533407a | 2018-04-04 21:44:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 2 | |
| 3 | # depends-hashes.pl |
| 4 | # |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame^] | 5 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 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 | # |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 20 | # 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é-Gonnard | 9ba9dfb | 2017-06-06 11:51:34 +0200 | [diff] [blame] | 36 | # |
| 37 | # For best effect, run either with cmake disabled, or cmake enabled in a mode |
| 38 | # that includes -Werror. |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 39 | |
| 40 | use warnings; |
| 41 | use strict; |
| 42 | |
| 43 | -d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n"; |
| 44 | |
| 45 | my $config_h = 'include/mbedtls/config.h'; |
| 46 | |
| 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 |
| 50 | my $ssl_sed_cmd = 's/^#define \(MBEDTLS_SSL.*\)/\1/p'; |
| 51 | my @ssl = split( /\s+/, `sed -n -e '$ssl_sed_cmd' $config_h` ); |
| 52 | |
Manuel Pégourié-Gonnard | 7766a2c | 2017-08-21 10:57:57 +0200 | [diff] [blame] | 53 | # for md we want to catch MD5_C but not MD_C, hence the extra dot |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 54 | my $mdx_sed_cmd = 's/^#define \(MBEDTLS_MD..*_C\)/\1/p'; |
| 55 | my $sha_sed_cmd = 's/^#define \(MBEDTLS_SHA.*_C\)/\1/p'; |
Manuel Pégourié-Gonnard | 20f236d | 2019-09-11 10:01:10 +0200 | [diff] [blame] | 56 | my @hash_modules = split( /\s+/, |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 57 | `sed -n -e '$mdx_sed_cmd' -e '$sha_sed_cmd' $config_h` ); |
Manuel Pégourié-Gonnard | 20f236d | 2019-09-11 10:01:10 +0200 | [diff] [blame] | 58 | |
| 59 | # there are also negative options for truncated variants, disabled by default |
| 60 | my $sha_trunc_sed_cmd = 's/^\/\/#define \(MBEDTLS_SHA..._NO_.*\)/\1/p'; |
| 61 | my @hash_negatives = split( /\s+/, |
| 62 | `sed -n -e '$sha_trunc_sed_cmd' $config_h` ); |
| 63 | |
| 64 | # list hash options with corresponding actions |
| 65 | my @hashes = ((map { "unset $_" } @hash_modules), |
| 66 | (map { "set $_" } @hash_negatives)); |
| 67 | |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 68 | system( "cp $config_h $config_h.bak" ) and die; |
| 69 | sub abort { |
| 70 | system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n"; |
Manuel Pégourié-Gonnard | 254eec8 | 2017-10-26 09:47:36 +0200 | [diff] [blame] | 71 | # use an exit code between 1 and 124 for git bisect (die returns 255) |
Manuel Pégourié-Gonnard | a7c4c8a | 2017-07-12 12:15:24 +0200 | [diff] [blame] | 72 | warn $_[0]; |
| 73 | exit 1; |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | for my $hash (@hashes) { |
| 77 | system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n"; |
| 78 | system( "make clean" ) and die; |
| 79 | |
| 80 | print "\n******************************************\n"; |
Manuel Pégourié-Gonnard | 20f236d | 2019-09-11 10:01:10 +0200 | [diff] [blame] | 81 | print "* Testing hash option: $hash\n"; |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 82 | print "******************************************\n"; |
Gilles Peskine | 9004a17 | 2019-09-16 15:20:36 +0200 | [diff] [blame] | 83 | $ENV{MBEDTLS_TEST_CONFIGURATION} = "-$hash"; |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 84 | |
Manuel Pégourié-Gonnard | 20f236d | 2019-09-11 10:01:10 +0200 | [diff] [blame] | 85 | system( "scripts/config.py $hash" ) |
| 86 | and abort "Failed to $hash\n"; |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 87 | |
| 88 | for my $opt (@ssl) { |
Gilles Peskine | 5d46f6a | 2019-07-27 23:52:53 +0200 | [diff] [blame] | 89 | system( "scripts/config.py unset $opt" ) |
Manuel Pégourié-Gonnard | 42a4d30 | 2017-06-06 10:54:01 +0200 | [diff] [blame] | 90 | and abort "Failed to disable $opt\n"; |
| 91 | } |
| 92 | |
| 93 | system( "CFLAGS='-Werror -Wall -Wextra' make lib" ) |
| 94 | and abort "Failed to build lib: $hash\n"; |
| 95 | system( "cd tests && make" ) and abort "Failed to build tests: $hash\n"; |
| 96 | system( "make test" ) and abort "Failed test suite: $hash\n"; |
| 97 | } |
| 98 | |
| 99 | system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n"; |
| 100 | system( "make clean" ) and die; |
| 101 | exit 0; |