| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
|  | 2 |  | 
|  | 3 | """Test helper for the Mbed TLS configuration file tool | 
|  | 4 |  | 
|  | 5 | Run config.py with various parameters and write the results to files. | 
|  | 6 |  | 
|  | 7 | This is a harness to help regression testing, not a functional tester. | 
|  | 8 | Sample usage: | 
|  | 9 |  | 
|  | 10 | test_config_script.py -d old | 
|  | 11 | ## Modify config.py and/or config.h ## | 
|  | 12 | test_config_script.py -d new | 
|  | 13 | diff -ru old new | 
|  | 14 | """ | 
|  | 15 |  | 
|  | 16 | ## Copyright (C) 2019, ARM Limited, All Rights Reserved | 
|  | 17 | ## SPDX-License-Identifier: Apache-2.0 | 
|  | 18 | ## | 
|  | 19 | ## Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 20 | ## not use this file except in compliance with the License. | 
|  | 21 | ## You may obtain a copy of the License at | 
|  | 22 | ## | 
|  | 23 | ## http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 24 | ## | 
|  | 25 | ## Unless required by applicable law or agreed to in writing, software | 
|  | 26 | ## distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 27 | ## WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 28 | ## See the License for the specific language governing permissions and | 
|  | 29 | ## limitations under the License. | 
|  | 30 | ## | 
|  | 31 | ## This file is part of Mbed TLS (https://tls.mbed.org) | 
|  | 32 |  | 
|  | 33 | import argparse | 
|  | 34 | import glob | 
|  | 35 | import os | 
|  | 36 | import re | 
|  | 37 | import shutil | 
|  | 38 | import subprocess | 
|  | 39 |  | 
|  | 40 | OUTPUT_FILE_PREFIX = 'config-' | 
|  | 41 |  | 
|  | 42 | def output_file_name(directory, stem, extension): | 
|  | 43 | return os.path.join(directory, | 
|  | 44 | '{}{}.{}'.format(OUTPUT_FILE_PREFIX, | 
|  | 45 | stem, extension)) | 
|  | 46 |  | 
|  | 47 | def cleanup_directory(directory): | 
|  | 48 | """Remove old output files.""" | 
|  | 49 | for extension in []: | 
|  | 50 | pattern = output_file_name(directory, '*', extension) | 
|  | 51 | filenames = glob.glob(pattern) | 
|  | 52 | for filename in filenames: | 
|  | 53 | os.remove(filename) | 
|  | 54 |  | 
|  | 55 | def prepare_directory(directory): | 
|  | 56 | """Create the output directory if it doesn't exist yet. | 
|  | 57 |  | 
|  | 58 | If there are old output files, remove them. | 
|  | 59 | """ | 
|  | 60 | if os.path.exists(directory): | 
|  | 61 | cleanup_directory(directory) | 
|  | 62 | else: | 
|  | 63 | os.makedirs(directory) | 
|  | 64 |  | 
|  | 65 | def guess_presets_from_help(help_text): | 
|  | 66 | """Figure out what presets the script supports. | 
|  | 67 |  | 
|  | 68 | help_text should be the output from running the script with --help. | 
|  | 69 | """ | 
|  | 70 | # Try the output format from config.py | 
|  | 71 | hits = re.findall(r'\{([-\w,]+)\}', help_text) | 
|  | 72 | for hit in hits: | 
|  | 73 | words = set(hit.split(',')) | 
|  | 74 | if 'get' in words and 'set' in words and 'unset' in words: | 
|  | 75 | words.remove('get') | 
|  | 76 | words.remove('set') | 
|  | 77 | words.remove('unset') | 
|  | 78 | return words | 
|  | 79 | # Try the output format from config.pl | 
|  | 80 | hits = re.findall(r'\n +([-\w]+) +- ', help_text) | 
|  | 81 | if hits: | 
|  | 82 | return hits | 
|  | 83 | raise Exception("Unable to figure out supported presets. Pass the '-p' option.") | 
|  | 84 |  | 
|  | 85 | def list_presets(options): | 
|  | 86 | """Return the list of presets to test. | 
|  | 87 |  | 
|  | 88 | The list is taken from the command line if present, otherwise it is | 
|  | 89 | extracted from running the config script with --help. | 
|  | 90 | """ | 
|  | 91 | if options.presets: | 
|  | 92 | return re.split(r'[ ,]+', options.presets) | 
|  | 93 | else: | 
|  | 94 | help_text = subprocess.run([options.script, '--help'], | 
| Gilles Peskine | e0c84ac | 2020-03-24 18:47:53 +0100 | [diff] [blame] | 95 | check=False, # config.pl --help returns 255 | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 96 | stdout=subprocess.PIPE, | 
|  | 97 | stderr=subprocess.STDOUT).stdout | 
|  | 98 | return guess_presets_from_help(help_text.decode('ascii')) | 
|  | 99 |  | 
| Gilles Peskine | 16a25e0 | 2019-09-19 12:19:24 +0200 | [diff] [blame] | 100 | def run_one(options, args, stem_prefix='', input_file=None): | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 101 | """Run the config script with the given arguments. | 
|  | 102 |  | 
| Gilles Peskine | 16a25e0 | 2019-09-19 12:19:24 +0200 | [diff] [blame] | 103 | Take the original content from input_file if specified, defaulting | 
|  | 104 | to options.input_file if input_file is None. | 
|  | 105 |  | 
|  | 106 | Write the following files, where xxx contains stem_prefix followed by | 
|  | 107 | a filename-friendly encoding of args: | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 108 | * config-xxx.h: modified file. | 
|  | 109 | * config-xxx.out: standard output. | 
|  | 110 | * config-xxx.err: standard output. | 
|  | 111 | * config-xxx.status: exit code. | 
| Gilles Peskine | 16a25e0 | 2019-09-19 12:19:24 +0200 | [diff] [blame] | 112 |  | 
|  | 113 | Return ("xxx+", "path/to/config-xxx.h") which can be used as | 
|  | 114 | stem_prefix and input_file to call this function again with new args. | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 115 | """ | 
| Gilles Peskine | 16a25e0 | 2019-09-19 12:19:24 +0200 | [diff] [blame] | 116 | if input_file is None: | 
|  | 117 | input_file = options.input_file | 
|  | 118 | stem = stem_prefix + '-'.join(args) | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 119 | data_filename = output_file_name(options.output_directory, stem, 'h') | 
|  | 120 | stdout_filename = output_file_name(options.output_directory, stem, 'out') | 
|  | 121 | stderr_filename = output_file_name(options.output_directory, stem, 'err') | 
|  | 122 | status_filename = output_file_name(options.output_directory, stem, 'status') | 
| Gilles Peskine | 16a25e0 | 2019-09-19 12:19:24 +0200 | [diff] [blame] | 123 | shutil.copy(input_file, data_filename) | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 124 | # Pass only the file basename, not the full path, to avoid getting the | 
|  | 125 | # directory name in error messages, which would make comparisons | 
|  | 126 | # between output directories more difficult. | 
|  | 127 | cmd = [os.path.abspath(options.script), | 
|  | 128 | '-f', os.path.basename(data_filename)] | 
|  | 129 | with open(stdout_filename, 'wb') as out: | 
|  | 130 | with open(stderr_filename, 'wb') as err: | 
|  | 131 | status = subprocess.call(cmd + args, | 
|  | 132 | cwd=options.output_directory, | 
|  | 133 | stdin=subprocess.DEVNULL, | 
|  | 134 | stdout=out, stderr=err) | 
|  | 135 | with open(status_filename, 'w') as status_file: | 
|  | 136 | status_file.write('{}\n'.format(status)) | 
| Gilles Peskine | 16a25e0 | 2019-09-19 12:19:24 +0200 | [diff] [blame] | 137 | return stem + "+", data_filename | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 138 |  | 
| Gilles Peskine | fd7ad33 | 2019-09-19 12:18:23 +0200 | [diff] [blame] | 139 | ### A list of symbols to test with. | 
|  | 140 | ### This script currently tests what happens when you change a symbol from | 
|  | 141 | ### having a value to not having a value or vice versa. This is not | 
|  | 142 | ### necessarily useful behavior, and we may not consider it a bug if | 
|  | 143 | ### config.py stops handling that case correctly. | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 144 | TEST_SYMBOLS = [ | 
| Gilles Peskine | fd7ad33 | 2019-09-19 12:18:23 +0200 | [diff] [blame] | 145 | 'CUSTOM_SYMBOL', # does not exist | 
|  | 146 | 'MBEDTLS_AES_C', # set, no value | 
|  | 147 | 'MBEDTLS_MPI_MAX_SIZE', # unset, has a value | 
|  | 148 | 'MBEDTLS_NO_UDBL_DIVISION', # unset, in "System support" | 
|  | 149 | 'MBEDTLS_PLATFORM_ZEROIZE_ALT', # unset, in "Customisation configuration options" | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 150 | ] | 
|  | 151 |  | 
|  | 152 | def run_all(options): | 
|  | 153 | """Run all the command lines to test.""" | 
|  | 154 | presets = list_presets(options) | 
|  | 155 | for preset in presets: | 
|  | 156 | run_one(options, [preset]) | 
|  | 157 | for symbol in TEST_SYMBOLS: | 
| Gilles Peskine | 61695e7 | 2019-09-13 15:17:01 +0200 | [diff] [blame] | 158 | run_one(options, ['get', symbol]) | 
| Gilles Peskine | 16a25e0 | 2019-09-19 12:19:24 +0200 | [diff] [blame] | 159 | (stem, filename) = run_one(options, ['set', symbol]) | 
|  | 160 | run_one(options, ['get', symbol], stem_prefix=stem, input_file=filename) | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 161 | run_one(options, ['--force', 'set', symbol]) | 
| Gilles Peskine | 16a25e0 | 2019-09-19 12:19:24 +0200 | [diff] [blame] | 162 | (stem, filename) = run_one(options, ['set', symbol, 'value']) | 
|  | 163 | run_one(options, ['get', symbol], stem_prefix=stem, input_file=filename) | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 164 | run_one(options, ['--force', 'set', symbol, 'value']) | 
| Gilles Peskine | f686042 | 2019-09-04 22:51:47 +0200 | [diff] [blame] | 165 | run_one(options, ['unset', symbol]) | 
| Gilles Peskine | 878acd6 | 2019-08-01 23:32:38 +0200 | [diff] [blame] | 166 |  | 
|  | 167 | def main(): | 
|  | 168 | """Command line entry point.""" | 
|  | 169 | parser = argparse.ArgumentParser(description=__doc__, | 
|  | 170 | formatter_class=argparse.RawDescriptionHelpFormatter) | 
|  | 171 | parser.add_argument('-d', metavar='DIR', | 
|  | 172 | dest='output_directory', required=True, | 
|  | 173 | help="""Output directory.""") | 
|  | 174 | parser.add_argument('-f', metavar='FILE', | 
|  | 175 | dest='input_file', default='include/mbedtls/config.h', | 
|  | 176 | help="""Config file (default: %(default)s).""") | 
|  | 177 | parser.add_argument('-p', metavar='PRESET,...', | 
|  | 178 | dest='presets', | 
|  | 179 | help="""Presets to test (default: guessed from --help).""") | 
|  | 180 | parser.add_argument('-s', metavar='FILE', | 
|  | 181 | dest='script', default='scripts/config.py', | 
|  | 182 | help="""Configuration script (default: %(default)s).""") | 
|  | 183 | options = parser.parse_args() | 
|  | 184 | prepare_directory(options.output_directory) | 
|  | 185 | run_all(options) | 
|  | 186 |  | 
|  | 187 | if __name__ == '__main__': | 
|  | 188 | main() |