blob: 3d6a520c4166cbc3cf80df3e4be98c6e10b9890d [file] [log] [blame]
Gilles Peskine693611e2024-06-11 19:32:22 +02001#!/usr/bin/env python3
2"""Generate test data for configuration reporting.
3"""
4
5# Copyright The Mbed TLS Contributors
6# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7
8import re
9import sys
10from typing import Iterable, Iterator, List, Optional, Tuple
11
12import project_scripts # pylint: disable=unused-import
13import config
14from mbedtls_framework import test_case
15from mbedtls_framework import test_data_generation
16
17
Gilles Peskine5454a842024-05-29 16:37:38 +020018def single_setting_case(setting: config.Setting, when_on: bool,
19 dependencies: List[str],
20 note: Optional[str]) -> test_case.TestCase:
Gilles Peskine693611e2024-06-11 19:32:22 +020021 """Construct a test case for a boolean setting.
22
23 This test case passes if the setting and its dependencies are enabled,
24 and is skipped otherwise.
25
26 * setting: the setting to be tested.
27 * when_on: True to test with the setting enabled, or False to test
28 with the setting disabled.
29 * dependencies: extra dependencies for the test case.
Gilles Peskine5454a842024-05-29 16:37:38 +020030 * note: a note to add after the setting name in the test description.
Gilles Peskine693611e2024-06-11 19:32:22 +020031 This is generally a summary of dependencies, and is generally empty
32 if the given setting is only tested once.
33 """
34 base = setting.name if when_on else '!' + setting.name
35 tc = test_case.TestCase()
36 tc.set_function('pass')
37 description_suffix = ' (' + note + ')' if note else ''
38 tc.set_description('Config: ' + base + description_suffix)
39 tc.set_dependencies([base] + dependencies)
40 return tc
41
42
Gilles Peskinec79ecea2024-05-23 16:32:39 +020043PSA_WANT_KEY_TYPE_KEY_PAIR_RE = \
44 re.compile(r'(?P<prefix>PSA_WANT_KEY_TYPE_(?P<type>\w+)_KEY_PAIR_)(?P<operation>\w+)\Z')
45
Gilles Peskine5454a842024-05-29 16:37:38 +020046# If foo is a setting that is only meaningful when bar is enabled, set
Gilles Peskinec79ecea2024-05-23 16:32:39 +020047# SUPER_SETTINGS[foo]=bar. More generally, bar can be a colon-separated
Gilles Peskine5454a842024-05-29 16:37:38 +020048# list of settings, meaning that all the settings must be enabled. Each setting
Gilles Peskinec79ecea2024-05-23 16:32:39 +020049# can be prefixed with '!' to negate it. This is the same syntax as a
50# depends_on directive in test data.
Gilles Peskine5454a842024-05-29 16:37:38 +020051# See also `find_super_setting`.
Gilles Peskinec79ecea2024-05-23 16:32:39 +020052SUPER_SETTINGS = {
53 'MBEDTLS_AESCE_C': 'MBEDTLS_AES_C',
54 'MBEDTLS_AESNI_C': 'MBEDTLS_AES_C',
55 'MBEDTLS_ERROR_STRERROR_DUMMY': '!MBEDTLS_ERROR_C',
56 'MBEDTLS_GENPRIME': 'MBEDTLS_RSA_C',
57 'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES': 'MBEDTLS_ENTROPY_C',
58 'MBEDTLS_NO_PLATFORM_ENTROPY': 'MBEDTLS_ENTROPY_C',
59 'MBEDTLS_PKCS1_V15': 'MBEDTLS_RSA_C',
60 'MBEDTLS_PKCS1_V21': 'MBEDTLS_RSA_C',
Gilles Peskinec08d5bf2024-05-28 19:18:31 +020061 'MBEDTLS_PSA_CRYPTO_CLIENT': '!MBEDTLS_PSA_CRYPTO_C',
Gilles Peskinec79ecea2024-05-23 16:32:39 +020062 'MBEDTLS_PSA_INJECT_ENTROPY': 'MBEDTLS_PSA_CRYPTO_C',
63 'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS': 'MBEDTLS_PSA_CRYPTO_C',
64}
65
Gilles Peskine5454a842024-05-29 16:37:38 +020066def find_super_setting(cfg: config.Config,
67 setting: config.Setting) -> Optional[str]:
68 """If setting is only meaningful when some setting is enabled, return that setting.
Gilles Peskinec79ecea2024-05-23 16:32:39 +020069
Gilles Peskine5454a842024-05-29 16:37:38 +020070 The return value can be a colon-separated list of settings, if the setting
71 is only meaningful when all of these settings are enabled. Settings can be
Gilles Peskinec79ecea2024-05-23 16:32:39 +020072 negated by prefixing them with '!'. This is the same syntax as a
73 depends_on directive in test data.
74 """
75 #pylint: disable=too-many-return-statements
76 name = setting.name
77 if name in SUPER_SETTINGS:
78 return SUPER_SETTINGS[name]
79 if name.startswith('MBEDTLS_') and not name.endswith('_C'):
80 if name.startswith('MBEDTLS_CIPHER_PADDING_'):
81 return 'MBEDTLS_CIPHER_C:MBEDTLS_CIPHER_MODE_CBC'
82 if name.startswith('MBEDTLS_PK_PARSE_EC_'):
83 return 'MBEDTLS_PK_C:MBEDTLS_PK_HAVE_ECC_KEYS'
Gilles Peskine5454a842024-05-29 16:37:38 +020084 # For TLS settings, insist on having them once off and once on in
Gilles Peskinef75c70b2024-05-28 19:18:46 +020085 # a configuration where both client support and server support are
Gilles Peskine5454a842024-05-29 16:37:38 +020086 # enabled. The settings are also meaningful when only one side is
Gilles Peskinef75c70b2024-05-28 19:18:46 +020087 # enabled, but there isn't much point in having separate records
88 # for client-side and server-side, so we keep things simple.
89 # Requiring both sides to be enabled also means we know we'll run
90 # tests that only run Mbed TLS against itself, which only run in
91 # configurations with both sides enabled.
Gilles Peskinec79ecea2024-05-23 16:32:39 +020092 if name.startswith('MBEDTLS_SSL_TLS1_3_') or \
93 name == 'MBEDTLS_SSL_EARLY_DATA':
94 return 'MBEDTLS_SSL_CLI_C:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_PROTO_TLS1_3'
95 if name.startswith('MBEDTLS_SSL_DTLS_'):
96 return 'MBEDTLS_SSL_CLI_C:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_PROTO_DTLS'
97 if name.startswith('MBEDTLS_SSL_'):
98 return 'MBEDTLS_SSL_CLI_C:MBEDTLS_SSL_SRV_C'
Gilles Peskine556249e2024-05-23 19:37:20 +020099 for pos in re.finditer(r'_', name):
100 super_name = name[:pos.start()] + '_C'
Gilles Peskinec79ecea2024-05-23 16:32:39 +0200101 if cfg.known(super_name):
102 return super_name
Gilles Peskine556249e2024-05-23 19:37:20 +0200103 m = PSA_WANT_KEY_TYPE_KEY_PAIR_RE.match(name)
Gilles Peskinec79ecea2024-05-23 16:32:39 +0200104 if m and m.group('operation') != 'BASIC':
105 return m.group('prefix') + 'BASIC'
106 return None
107
Gilles Peskine5454a842024-05-29 16:37:38 +0200108def conditions_for_setting(cfg: config.Config,
109 setting: config.Setting
110 ) -> Iterator[Tuple[List[str], str]]:
Gilles Peskine693611e2024-06-11 19:32:22 +0200111 """Enumerate the conditions under which to test the given setting.
112
Gilles Peskine5454a842024-05-29 16:37:38 +0200113 * cfg: all configuration settings.
Gilles Peskine693611e2024-06-11 19:32:22 +0200114 * setting: the setting to be tested.
115
116 Generate a stream of conditions, i.e. extra dependencies to test with
117 together with a human-readable explanation of each dependency. Some
118 typical cases:
119
120 * By default, generate a one-element stream with no extra dependencies.
Gilles Peskine5454a842024-05-29 16:37:38 +0200121 * If the setting is ignored unless some other setting is enabled, generate
122 a one-element stream with that other setting as an extra dependency.
123 * If the setting is known to interact with some other setting, generate
124 a stream with one element where this setting is on and one where it's off.
Gilles Peskine693611e2024-06-11 19:32:22 +0200125 * To skip the setting altogether, generate an empty stream.
126 """
127 name = setting.name
128 if name.endswith('_ALT') and not config.is_seamless_alt(name):
129 # We don't test alt implementations, except (most) platform alts
130 return
Gilles Peskine5454a842024-05-29 16:37:38 +0200131 super_setting = find_super_setting(cfg, setting)
Gilles Peskinec79ecea2024-05-23 16:32:39 +0200132 if super_setting:
133 yield [super_setting], ''
134 return
Gilles Peskine693611e2024-06-11 19:32:22 +0200135 yield [], ''
136
137
Gilles Peskine5454a842024-05-29 16:37:38 +0200138def enumerate_boolean_setting_cases(cfg: config.Config
Gilles Peskine693611e2024-06-11 19:32:22 +0200139 ) -> Iterable[test_case.TestCase]:
Gilles Peskine5454a842024-05-29 16:37:38 +0200140 """Emit test cases for all boolean settings."""
Gilles Peskine693611e2024-06-11 19:32:22 +0200141 for name in sorted(cfg.settings.keys()):
142 setting = cfg.settings[name]
143 if not name.startswith('PSA_WANT_') and setting.value:
144 continue # non-boolean setting
145 for when_on in True, False:
Gilles Peskine5454a842024-05-29 16:37:38 +0200146 for deps, note in conditions_for_setting(cfg, setting):
147 yield single_setting_case(setting, when_on, deps, note)
Gilles Peskine693611e2024-06-11 19:32:22 +0200148
149
150
151class ConfigTestGenerator(test_data_generation.TestGenerator):
152 """Generate test cases for configuration reporting."""
153
Gilles Peskine5454a842024-05-29 16:37:38 +0200154 def __init__(self, settings):
Gilles Peskine693611e2024-06-11 19:32:22 +0200155 self.mbedtls_config = config.ConfigFile()
156 self.targets['test_suite_config.mbedtls_boolean'] = \
Gilles Peskine5454a842024-05-29 16:37:38 +0200157 lambda: enumerate_boolean_setting_cases(self.mbedtls_config)
Gilles Peskine693611e2024-06-11 19:32:22 +0200158 self.psa_config = config.ConfigFile('include/psa/crypto_config.h')
159 self.targets['test_suite_config.psa_boolean'] = \
Gilles Peskine5454a842024-05-29 16:37:38 +0200160 lambda: enumerate_boolean_setting_cases(self.psa_config)
161 super().__init__(settings)
Gilles Peskine693611e2024-06-11 19:32:22 +0200162
163
164if __name__ == '__main__':
165 test_data_generation.main(sys.argv[1:], __doc__, ConfigTestGenerator)