Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Copyright (c) 2018, Arm Limited, All Rights Reserved. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # This file is part of Mbed TLS (https://tls.mbed.org) |
| 19 | |
| 20 | """Test Mbed TLS with a subset of algorithms. |
| 21 | """ |
| 22 | |
| 23 | import argparse |
| 24 | import os |
| 25 | import re |
| 26 | import shutil |
| 27 | import subprocess |
| 28 | import sys |
| 29 | import traceback |
| 30 | |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 31 | class Colors: |
| 32 | """Minimalistic support for colored output. |
| 33 | Each field of an object of this class is either None if colored output |
| 34 | is not possible or not desired, or a pair of strings (start, stop) such |
| 35 | that outputting start switches the text color to the desired color and |
| 36 | stop switches the text color back to the default.""" |
| 37 | red = None |
| 38 | green = None |
| 39 | bold_red = None |
| 40 | bold_green = None |
| 41 | def __init__(self, options=None): |
| 42 | if not options or options.color in ['no', 'never']: |
| 43 | want_color = False |
| 44 | elif options.color in ['yes', 'always']: |
| 45 | want_color = True |
| 46 | else: |
| 47 | want_color = sys.stderr.isatty() |
| 48 | if want_color: |
| 49 | # Assume ANSI compatible terminal |
| 50 | normal = '\033[0m' |
| 51 | self.red = ('\033[31m', normal) |
| 52 | self.green = ('\033[32m', normal) |
| 53 | self.bold_red = ('\033[1;31m', normal) |
| 54 | self.bold_green = ('\033[1;32m', normal) |
| 55 | NO_COLORS = Colors(None) |
| 56 | |
| 57 | def log_line(text, prefix='depends.py:', suffix='', color=None): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 58 | """Print a status message.""" |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 59 | if color != None: |
| 60 | prefix = color[0] + prefix |
| 61 | suffix = suffix + color[1] |
| 62 | sys.stderr.write(prefix + ' ' + text + suffix + '\n') |
Gilles Peskine | 46c8256 | 2019-01-29 18:42:55 +0100 | [diff] [blame] | 63 | sys.stderr.flush() |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 64 | |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 65 | def log_command(cmd): |
| 66 | """Print a trace of the specified command. |
| 67 | cmd is a list of strings: a command name and its arguments.""" |
| 68 | log_line(' '.join(cmd), prefix='+') |
| 69 | |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 70 | def backup_config(options): |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 71 | """Back up the library configuration file (config.h). |
| 72 | If the backup file already exists, it is presumed to be the desired backup, |
| 73 | so don't make another backup.""" |
| 74 | if os.path.exists(options.config_backup): |
| 75 | options.own_backup = False |
| 76 | else: |
| 77 | options.own_backup = True |
| 78 | shutil.copy(options.config, options.config_backup) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 79 | |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 80 | def restore_config(options): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 81 | """Restore the library configuration file (config.h). |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 82 | Remove the backup file if it was saved earlier.""" |
| 83 | if options.own_backup: |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 84 | shutil.move(options.config_backup, options.config) |
| 85 | else: |
| 86 | shutil.copy(options.config_backup, options.config) |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 87 | |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 88 | def run_config_pl(options, args): |
| 89 | """Run scripts/config.pl with the specified arguments.""" |
| 90 | cmd = ['scripts/config.pl'] |
| 91 | if options.config != 'include/mbedtls/config.h': |
| 92 | cmd += ['--file', options.config] |
| 93 | cmd += args |
| 94 | log_command(cmd) |
| 95 | subprocess.check_call(cmd) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 96 | |
| 97 | class Job: |
| 98 | """A job builds the library in a specific configuration and runs some tests.""" |
| 99 | def __init__(self, name, config_settings, commands): |
| 100 | """Build a job object. |
| 101 | The job uses the configuration described by config_settings. This is a |
| 102 | dictionary where the keys are preprocessor symbols and the values are |
| 103 | booleans or strings. A boolean indicates whether or not to #define the |
| 104 | symbol. With a string, the symbol is #define'd to that value. |
| 105 | After setting the configuration, the job runs the programs specified by |
| 106 | commands. This is a list of lists of strings; each list of string is a |
| 107 | command name and its arguments and is passed to subprocess.call with |
| 108 | shell=False.""" |
| 109 | self.name = name |
| 110 | self.config_settings = config_settings |
| 111 | self.commands = commands |
| 112 | |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 113 | def announce(self, colors, what): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 114 | '''Announce the start or completion of a job. |
| 115 | If what is None, announce the start of the job. |
| 116 | If what is True, announce that the job has passed. |
| 117 | If what is False, announce that the job has failed.''' |
| 118 | if what is True: |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 119 | log_line(self.name + ' PASSED', color=colors.green) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 120 | elif what is False: |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 121 | log_line(self.name + ' FAILED', color=colors.red) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 122 | else: |
| 123 | log_line('starting ' + self.name) |
| 124 | |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 125 | def set_reference_config(self, options): |
| 126 | """Change the library configuration file (config.h) to the reference state. |
| 127 | The reference state is the one from which the tested configurations are |
| 128 | derived.""" |
| 129 | # Turn off memory management options that are not relevant to |
| 130 | # the tests and slow them down. |
| 131 | run_config_pl(options, ['full']) |
| 132 | run_config_pl(options, ['unset', 'MBEDTLS_MEMORY_BACKTRACE']) |
| 133 | run_config_pl(options, ['unset', 'MBEDTLS_MEMORY_BUFFER_ALLOC_C']) |
| 134 | run_config_pl(options, ['unset', 'MBEDTLS_MEMORY_DEBUG']) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 135 | |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 136 | def configure(self, options): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 137 | '''Set library configuration options as required for the job. |
| 138 | config_file_name indicates which file to modify.''' |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 139 | self.set_reference_config(options) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 140 | for key, value in sorted(self.config_settings.items()): |
| 141 | if value is True: |
| 142 | args = ['set', key] |
| 143 | elif value is False: |
| 144 | args = ['unset', key] |
| 145 | else: |
| 146 | args = ['set', key, value] |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 147 | run_config_pl(options, args) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 148 | |
| 149 | def test(self, options): |
| 150 | '''Run the job's build and test commands. |
| 151 | Return True if all the commands succeed and False otherwise. |
| 152 | If options.keep_going is false, stop as soon as one command fails. Otherwise |
| 153 | run all the commands, except that if the first command fails, none of the |
| 154 | other commands are run (typically, the first command is a build command |
| 155 | and subsequent commands are tests that cannot run if the build failed).''' |
| 156 | built = False |
| 157 | success = True |
| 158 | for command in self.commands: |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 159 | log_command(command) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 160 | ret = subprocess.call(command) |
| 161 | if ret != 0: |
| 162 | if command[0] not in ['make', options.make_command]: |
| 163 | log_line('*** [{}] Error {}'.format(' '.join(command), ret)) |
| 164 | if not options.keep_going or not built: |
| 165 | return False |
| 166 | success = False |
| 167 | built = True |
| 168 | return success |
| 169 | |
| 170 | # SSL/TLS versions up to 1.1 and corresponding options. These require |
| 171 | # both MD5 and SHA-1. |
| 172 | ssl_pre_1_2_dependencies = ['MBEDTLS_SSL_CBC_RECORD_SPLITTING', |
| 173 | 'MBEDTLS_SSL_PROTO_SSL3', |
| 174 | 'MBEDTLS_SSL_PROTO_TLS1', |
| 175 | 'MBEDTLS_SSL_PROTO_TLS1_1'] |
| 176 | |
| 177 | # If the configuration option A requires B, make sure that |
| 178 | # B in reverse_dependencies[A]. |
Gilles Peskine | 584c24a | 2019-01-29 19:30:40 +0100 | [diff] [blame] | 179 | # All the information here should be contained in check_config.h. This |
| 180 | # file includes a copy because it changes rarely and it would be a pain |
| 181 | # to extract automatically. |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 182 | reverse_dependencies = { |
| 183 | 'MBEDTLS_ECDSA_C': ['MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED'], |
| 184 | 'MBEDTLS_ECP_C': ['MBEDTLS_ECDSA_C', |
| 185 | 'MBEDTLS_ECDH_C', |
| 186 | 'MBEDTLS_ECJPAKE_C', |
Gilles Peskine | 584c24a | 2019-01-29 19:30:40 +0100 | [diff] [blame] | 187 | 'MBEDTLS_ECP_RESTARTABLE', |
| 188 | 'MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED', |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 189 | 'MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED', |
| 190 | 'MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED', |
| 191 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED', |
| 192 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED', |
| 193 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED'], |
Gilles Peskine | 584c24a | 2019-01-29 19:30:40 +0100 | [diff] [blame] | 194 | 'MBEDTLS_ECP_DP_SECP256R1_ENABLED': ['MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED'], |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 195 | 'MBEDTLS_MD5_C': ssl_pre_1_2_dependencies, |
| 196 | 'MBEDTLS_PKCS1_V21': ['MBEDTLS_X509_RSASSA_PSS_SUPPORT'], |
| 197 | 'MBEDTLS_PKCS1_V15': ['MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED', |
| 198 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED', |
| 199 | 'MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED', |
| 200 | 'MBEDTLS_KEY_EXCHANGE_RSA_ENABLED'], |
| 201 | 'MBEDTLS_RSA_C': ['MBEDTLS_X509_RSASSA_PSS_SUPPORT', |
| 202 | 'MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED', |
| 203 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED', |
| 204 | 'MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED', |
| 205 | 'MBEDTLS_KEY_EXCHANGE_RSA_ENABLED'], |
| 206 | 'MBEDTLS_SHA1_C': ssl_pre_1_2_dependencies, |
Gilles Peskine | 584c24a | 2019-01-29 19:30:40 +0100 | [diff] [blame] | 207 | 'MBEDTLS_SHA256_C': ['MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED', |
| 208 | 'MBEDTLS_ENTROPY_FORCE_SHA256'], |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 209 | 'MBEDTLS_X509_RSASSA_PSS_SUPPORT': [], |
| 210 | } |
| 211 | |
| 212 | def turn_off_dependencies(config_settings): |
| 213 | """For every option turned off config_settings, also turn off what depends on it. |
| 214 | An option O is turned off if config_settings[O] is False.""" |
| 215 | for key, value in sorted(config_settings.items()): |
| 216 | if value is not False: |
| 217 | continue |
| 218 | for dep in reverse_dependencies.get(key, []): |
| 219 | config_settings[dep] = False |
| 220 | |
| 221 | class Domain: |
| 222 | """A domain is a set of jobs that all relate to a particular configuration aspect.""" |
| 223 | pass |
| 224 | |
| 225 | class ExclusiveDomain(Domain): |
| 226 | """A domain consisting of a set of conceptually-equivalent settings. |
| 227 | Establish a list of configuration symbols. For each symbol, run a test job |
| 228 | with this symbol set and the others unset, and a test job with this symbol |
| 229 | unset and the others set.""" |
Gilles Peskine | b1284cf | 2019-01-29 18:56:03 +0100 | [diff] [blame] | 230 | def __init__(self, symbols, commands, exclude=None): |
| 231 | """Build a domain for the specified list of configuration symbols. |
| 232 | The domain contains two sets of jobs: jobs that enable one of the elements |
| 233 | of symbols and disable the others, and jobs that disable one of the elements |
| 234 | of symbols and enable the others. |
| 235 | Each job runs the specified commands. |
| 236 | If exclude is a regular expression, skip generated jobs whose description |
| 237 | would match this regular expression.""" |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 238 | self.jobs = [] |
| 239 | for invert in [False, True]: |
| 240 | base_config_settings = {} |
| 241 | for symbol in symbols: |
| 242 | base_config_settings[symbol] = invert |
| 243 | for symbol in symbols: |
| 244 | description = '!' + symbol if invert else symbol |
Gilles Peskine | b1284cf | 2019-01-29 18:56:03 +0100 | [diff] [blame] | 245 | if exclude and re.match(exclude, description): |
| 246 | continue |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 247 | config_settings = base_config_settings.copy() |
| 248 | config_settings[symbol] = not invert |
| 249 | turn_off_dependencies(config_settings) |
| 250 | job = Job(description, config_settings, commands) |
| 251 | self.jobs.append(job) |
| 252 | |
| 253 | class ComplementaryDomain: |
| 254 | """A domain consisting of a set of loosely-related settings. |
| 255 | Establish a list of configuration symbols. For each symbol, run a test job |
| 256 | with this symbol unset.""" |
| 257 | def __init__(self, symbols, commands): |
Gilles Peskine | b1284cf | 2019-01-29 18:56:03 +0100 | [diff] [blame] | 258 | """Build a domain for the specified list of configuration symbols. |
| 259 | Each job in the domain disables one of the specified symbols. |
| 260 | Each job runs the specified commands.""" |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 261 | self.jobs = [] |
| 262 | for symbol in symbols: |
| 263 | description = '!' + symbol |
| 264 | config_settings = {symbol: False} |
| 265 | turn_off_dependencies(config_settings) |
| 266 | job = Job(description, config_settings, commands) |
| 267 | self.jobs.append(job) |
| 268 | |
| 269 | class DomainData: |
| 270 | """Collect data about the library.""" |
| 271 | def collect_config_symbols(self, options): |
| 272 | """Read the list of settings from config.h. |
| 273 | Return them in a generator.""" |
| 274 | with open(options.config) as config_file: |
| 275 | rx = re.compile(r'\s*(?://\s*)?#define\s+(\w+)\s*(?:$|/[/*])') |
| 276 | for line in config_file: |
| 277 | m = re.match(rx, line) |
| 278 | if m: |
| 279 | yield m.group(1) |
| 280 | |
| 281 | def config_symbols_matching(self, regexp): |
| 282 | """List the config.h settings matching regexp.""" |
| 283 | return [symbol for symbol in self.all_config_symbols |
| 284 | if re.match(regexp, symbol)] |
| 285 | |
| 286 | def __init__(self, options): |
| 287 | """Gather data about the library and establish a list of domains to test.""" |
| 288 | build_command = [options.make_command, 'CFLAGS=-Werror'] |
| 289 | build_and_test = [build_command, [options.make_command, 'test']] |
| 290 | self.all_config_symbols = set(self.collect_config_symbols(options)) |
| 291 | # Find hash modules by name. |
| 292 | hash_symbols = self.config_symbols_matching(r'MBEDTLS_(MD|RIPEMD|SHA)[0-9]+_C\Z') |
| 293 | # Find elliptic curve enabling macros by name. |
| 294 | curve_symbols = self.config_symbols_matching(r'MBEDTLS_ECP_DP_\w+_ENABLED\Z') |
| 295 | # Find key exchange enabling macros by name. |
| 296 | key_exchange_symbols = self.config_symbols_matching(r'MBEDTLS_KEY_EXCHANGE_\w+_ENABLED\Z') |
| 297 | self.domains = { |
| 298 | # Elliptic curves. Run the test suites. |
| 299 | 'curves': ExclusiveDomain(curve_symbols, build_and_test), |
| 300 | # Hash algorithms. Exclude configurations with only one |
| 301 | # hash which is obsolete. Run the test suites. |
Gilles Peskine | b1284cf | 2019-01-29 18:56:03 +0100 | [diff] [blame] | 302 | 'hashes': ExclusiveDomain(hash_symbols, build_and_test, |
| 303 | exclude=r'MBEDTLS_(MD|RIPEMD|SHA1_)'), |
Gilles Peskine | c3b4dee | 2019-01-29 19:33:05 +0100 | [diff] [blame^] | 304 | # Key exchange types. Only build the library and the sample |
| 305 | # programs. |
| 306 | 'kex': ExclusiveDomain(key_exchange_symbols, |
| 307 | [build_command + ['lib'], |
| 308 | build_command + ['-C', 'programs']]), |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 309 | 'pkalgs': ComplementaryDomain(['MBEDTLS_ECDSA_C', |
| 310 | 'MBEDTLS_ECP_C', |
| 311 | 'MBEDTLS_PKCS1_V21', |
| 312 | 'MBEDTLS_PKCS1_V15', |
| 313 | 'MBEDTLS_RSA_C', |
| 314 | 'MBEDTLS_X509_RSASSA_PSS_SUPPORT'], |
| 315 | build_and_test), |
| 316 | } |
| 317 | self.jobs = {} |
| 318 | for domain in self.domains.values(): |
| 319 | for job in domain.jobs: |
| 320 | self.jobs[job.name] = job |
| 321 | |
| 322 | def get_jobs(self, name): |
| 323 | """Return the list of jobs identified by the given name. |
| 324 | A name can either be the name of a domain or the name of one specific job.""" |
| 325 | if name in self.domains: |
| 326 | return sorted(self.domains[name].jobs, key=lambda job: job.name) |
| 327 | else: |
| 328 | return [self.jobs[name]] |
| 329 | |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 330 | def run(options, job, colors=NO_COLORS): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 331 | """Run the specified job (a Job instance).""" |
| 332 | subprocess.check_call([options.make_command, 'clean']) |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 333 | job.announce(colors, None) |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 334 | job.configure(options) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 335 | success = job.test(options) |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 336 | job.announce(colors, success) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 337 | return success |
| 338 | |
| 339 | def main(options, domain_data): |
| 340 | """Run the desired jobs. |
| 341 | domain_data should be a DomainData instance that describes the available |
| 342 | domains and jobs. |
| 343 | Run the jobs listed in options.domains.""" |
| 344 | if not hasattr(options, 'config_backup'): |
| 345 | options.config_backup = options.config + '.bak' |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 346 | colors = Colors(options) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 347 | jobs = [] |
| 348 | failures = [] |
| 349 | successes = [] |
| 350 | for name in options.domains: |
| 351 | jobs += domain_data.get_jobs(name) |
| 352 | backup_config(options) |
| 353 | try: |
| 354 | for job in jobs: |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 355 | success = run(options, job, colors=colors) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 356 | if not success: |
| 357 | if options.keep_going: |
| 358 | failures.append(job.name) |
| 359 | else: |
| 360 | return False |
| 361 | else: |
| 362 | successes.append(job.name) |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 363 | restore_config(options) |
| 364 | except: |
| 365 | # Restore the configuration, except in stop-on-error mode if there |
| 366 | # was an error, where we leave the failing configuration up for |
| 367 | # developer convenience. |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 368 | if options.keep_going: |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 369 | restore_config(options) |
| 370 | raise |
Gilles Peskine | e85163b | 2019-01-29 18:50:03 +0100 | [diff] [blame] | 371 | if successes: |
| 372 | log_line('{} passed'.format(' '.join(successes)), color=colors.bold_green) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 373 | if failures: |
Gilles Peskine | e85163b | 2019-01-29 18:50:03 +0100 | [diff] [blame] | 374 | log_line('{} FAILED'.format(' '.join(failures)), color=colors.bold_red) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 375 | return False |
| 376 | else: |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 377 | return True |
| 378 | |
| 379 | |
| 380 | if __name__ == '__main__': |
| 381 | try: |
| 382 | parser = argparse.ArgumentParser(description=__doc__) |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 383 | parser.add_argument('--color', metavar='WHEN', |
| 384 | help='Colorize the output (always/auto/never)', |
| 385 | choices=['always', 'auto', 'never'], default='auto') |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 386 | parser.add_argument('-c', '--config', metavar='FILE', |
| 387 | help='Configuration file to modify', |
| 388 | default='include/mbedtls/config.h') |
| 389 | parser.add_argument('-C', '--directory', metavar='DIR', |
| 390 | help='Change to this directory before anything else', |
| 391 | default='.') |
| 392 | parser.add_argument('-k', '--keep-going', |
| 393 | help='Try all configurations even if some fail (default)', |
| 394 | action='store_true', dest='keep_going', default=True) |
| 395 | parser.add_argument('-e', '--no-keep-going', |
| 396 | help='Stop as soon as a configuration fails', |
| 397 | action='store_false', dest='keep_going') |
| 398 | parser.add_argument('--list-jobs', |
| 399 | help='List supported jobs and exit', |
| 400 | action='append_const', dest='list', const='jobs') |
| 401 | parser.add_argument('--list-domains', |
| 402 | help='List supported domains and exit', |
| 403 | action='append_const', dest='list', const='domains') |
| 404 | parser.add_argument('--make-command', metavar='CMD', |
| 405 | help='Command to run instead of make (e.g. gmake)', |
| 406 | action='store', default='make') |
| 407 | parser.add_argument('domains', metavar='DOMAIN', nargs='*', |
| 408 | help='The domain(s) to test (default: all)', |
| 409 | default=True) |
| 410 | options = parser.parse_args() |
| 411 | os.chdir(options.directory) |
| 412 | domain_data = DomainData(options) |
| 413 | if options.domains == True: |
| 414 | options.domains = sorted(domain_data.domains.keys()) |
| 415 | if options.list: |
| 416 | for what in options.list: |
| 417 | for key in sorted(getattr(domain_data, what).keys()): |
| 418 | print(key) |
| 419 | exit(0) |
| 420 | else: |
| 421 | sys.exit(0 if main(options, domain_data) else 1) |
| 422 | except SystemExit: |
| 423 | raise |
| 424 | except: |
| 425 | traceback.print_exc() |
| 426 | exit(3) |