depends-hashes.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env perl
  2. # depends-hashes.pl
  3. #
  4. # Copyright The Mbed TLS Contributors
  5. # SPDX-License-Identifier: Apache-2.0
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. # not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. # Purpose
  20. #
  21. # To test the code dependencies on individual hashes in each test suite. This
  22. # is a verification step to ensure we don't ship test suites that do not work
  23. # for some build options.
  24. #
  25. # The process is:
  26. # for each possible hash
  27. # build the library and test suites with the hash disabled
  28. # execute the test suites
  29. #
  30. # And any test suite with the wrong dependencies will fail.
  31. #
  32. # Usage: tests/scripts/depends-hashes.pl
  33. #
  34. # This script should be executed from the root of the project directory.
  35. #
  36. # For best effect, run either with cmake disabled, or cmake enabled in a mode
  37. # that includes -Werror.
  38. use warnings;
  39. use strict;
  40. -d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
  41. my $config_h = 'include/mbedtls/config.h';
  42. # as many SSL options depend on specific hashes,
  43. # and SSL is not in the test suites anyways,
  44. # disable it to avoid dependcies issues
  45. my $ssl_sed_cmd = 's/^#define \(MBEDTLS_SSL.*\)/\1/p';
  46. my @ssl = split( /\s+/, `sed -n -e '$ssl_sed_cmd' $config_h` );
  47. # for md we want to catch MD5_C but not MD_C, hence the extra dot
  48. my $mdx_sed_cmd = 's/^#define \(MBEDTLS_MD..*_C\)/\1/p';
  49. my $sha_sed_cmd = 's/^#define \(MBEDTLS_SHA.*_C\)/\1/p';
  50. my @hash_modules = split( /\s+/,
  51. `sed -n -e '$mdx_sed_cmd' -e '$sha_sed_cmd' $config_h` );
  52. # there are also negative options for truncated variants, disabled by default
  53. my $sha_trunc_sed_cmd = 's/^\/\/#define \(MBEDTLS_SHA..._NO_.*\)/\1/p';
  54. my @hash_negatives = split( /\s+/,
  55. `sed -n -e '$sha_trunc_sed_cmd' $config_h` );
  56. # list hash options with corresponding actions
  57. my @hashes = ((map { "unset $_" } @hash_modules),
  58. (map { "set $_" } @hash_negatives));
  59. system( "cp $config_h $config_h.bak" ) and die;
  60. sub abort {
  61. system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
  62. # use an exit code between 1 and 124 for git bisect (die returns 255)
  63. warn $_[0];
  64. exit 1;
  65. }
  66. for my $hash (@hashes) {
  67. system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n";
  68. system( "make clean" ) and die;
  69. print "\n******************************************\n";
  70. print "* Testing hash option: $hash\n";
  71. print "******************************************\n";
  72. $ENV{MBEDTLS_TEST_CONFIGURATION} = "-$hash";
  73. system( "scripts/config.py $hash" )
  74. and abort "Failed to $hash\n";
  75. for my $opt (@ssl) {
  76. system( "scripts/config.py unset $opt" )
  77. and abort "Failed to disable $opt\n";
  78. }
  79. system( "CFLAGS='-Werror -Wall -Wextra' make lib" )
  80. and abort "Failed to build lib: $hash\n";
  81. system( "cd tests && make" ) and abort "Failed to build tests: $hash\n";
  82. system( "make test" ) and abort "Failed test suite: $hash\n";
  83. }
  84. system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n";
  85. system( "make clean" ) and die;
  86. exit 0;