Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | """Sanity checks for test data. |
Gilles Peskine | bbb3664 | 2020-07-03 00:30:12 +0200 | [diff] [blame] | 4 | |
| 5 | This program contains a class for traversing test cases that can be used |
| 6 | independently of the checks. |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 7 | """ |
| 8 | |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 9 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 10 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 11 | |
Gilles Peskine | 1fb7aea | 2019-12-02 14:26:04 +0100 | [diff] [blame] | 12 | import argparse |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 13 | import glob |
| 14 | import os |
| 15 | import re |
Yanray Wang | 2354693 | 2023-02-24 14:53:29 +0800 | [diff] [blame] | 16 | import subprocess |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 17 | import sys |
Tomás González | 754f8cd | 2023-08-17 15:11:10 +0100 | [diff] [blame] | 18 | |
Gilles Peskine | 1398556 | 2024-10-03 19:11:27 +0200 | [diff] [blame] | 19 | import scripts_path # pylint: disable=unused-import |
| 20 | from mbedtls_framework import build_tree |
Gilles Peskine | 31e3152 | 2024-10-03 17:23:53 +0200 | [diff] [blame^] | 21 | import collect_test_cases |
Gilles Peskine | 1398556 | 2024-10-03 19:11:27 +0200 | [diff] [blame] | 22 | |
Pengyu Lv | ce980e6 | 2023-11-30 16:53:31 +0800 | [diff] [blame] | 23 | class ScriptOutputError(ValueError): |
| 24 | """A kind of ValueError that indicates we found |
| 25 | the script doesn't list test cases in an expected |
| 26 | pattern. |
| 27 | """ |
| 28 | |
| 29 | @property |
| 30 | def script_name(self): |
| 31 | return super().args[0] |
| 32 | |
| 33 | @property |
| 34 | def idx(self): |
| 35 | return super().args[1] |
| 36 | |
| 37 | @property |
| 38 | def line(self): |
| 39 | return super().args[2] |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 40 | |
| 41 | class Results: |
Darryl Green | 1822061 | 2019-12-17 15:03:59 +0000 | [diff] [blame] | 42 | """Store file and line information about errors or warnings in test suites.""" |
Gilles Peskine | 1fb7aea | 2019-12-02 14:26:04 +0100 | [diff] [blame] | 43 | |
| 44 | def __init__(self, options): |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 45 | self.errors = 0 |
| 46 | self.warnings = 0 |
Gilles Peskine | 1fb7aea | 2019-12-02 14:26:04 +0100 | [diff] [blame] | 47 | self.ignore_warnings = options.quiet |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 48 | |
| 49 | def error(self, file_name, line_number, fmt, *args): |
| 50 | sys.stderr.write(('{}:{}:ERROR:' + fmt + '\n'). |
| 51 | format(file_name, line_number, *args)) |
| 52 | self.errors += 1 |
| 53 | |
| 54 | def warning(self, file_name, line_number, fmt, *args): |
Gilles Peskine | 1fb7aea | 2019-12-02 14:26:04 +0100 | [diff] [blame] | 55 | if not self.ignore_warnings: |
| 56 | sys.stderr.write(('{}:{}:Warning:' + fmt + '\n') |
| 57 | .format(file_name, line_number, *args)) |
| 58 | self.warnings += 1 |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 59 | |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 60 | class TestDescriptionExplorer: |
| 61 | """An iterator over test cases with descriptions. |
Gilles Peskine | d34e9e4 | 2020-06-25 16:16:25 +0200 | [diff] [blame] | 62 | |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 63 | The test cases that have descriptions are: |
| 64 | * Individual unit tests (entries in a .data file) in test suites. |
| 65 | * Individual test cases in ssl-opt.sh. |
| 66 | |
| 67 | This is an abstract class. To use it, derive a class that implements |
| 68 | the process_test_case method, and call walk_all(). |
Gilles Peskine | d34e9e4 | 2020-06-25 16:16:25 +0200 | [diff] [blame] | 69 | """ |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 70 | |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 71 | def process_test_case(self, per_file_state, |
| 72 | file_name, line_number, description): |
| 73 | """Process a test case. |
Gilles Peskine | d34e9e4 | 2020-06-25 16:16:25 +0200 | [diff] [blame] | 74 | |
Gilles Peskine | bbb3664 | 2020-07-03 00:30:12 +0200 | [diff] [blame] | 75 | per_file_state: an object created by new_per_file_state() at the beginning |
| 76 | of each file. |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 77 | file_name: a relative path to the file containing the test case. |
| 78 | line_number: the line number in the given file. |
| 79 | description: the test case description as a byte string. |
Gilles Peskine | d34e9e4 | 2020-06-25 16:16:25 +0200 | [diff] [blame] | 80 | """ |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 81 | raise NotImplementedError |
Gilles Peskine | d34e9e4 | 2020-06-25 16:16:25 +0200 | [diff] [blame] | 82 | |
Gilles Peskine | bbb3664 | 2020-07-03 00:30:12 +0200 | [diff] [blame] | 83 | def new_per_file_state(self): |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 84 | """Return a new per-file state object. |
Gilles Peskine | d34e9e4 | 2020-06-25 16:16:25 +0200 | [diff] [blame] | 85 | |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 86 | The default per-file state object is None. Child classes that require per-file |
| 87 | state may override this method. |
Gilles Peskine | d34e9e4 | 2020-06-25 16:16:25 +0200 | [diff] [blame] | 88 | """ |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 89 | #pylint: disable=no-self-use |
| 90 | return None |
| 91 | |
| 92 | def walk_test_suite(self, data_file_name): |
| 93 | """Iterate over the test cases in the given unit test data file.""" |
| 94 | in_paragraph = False |
Gilles Peskine | bbb3664 | 2020-07-03 00:30:12 +0200 | [diff] [blame] | 95 | descriptions = self.new_per_file_state() # pylint: disable=assignment-from-none |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 96 | with open(data_file_name, 'rb') as data_file: |
| 97 | for line_number, line in enumerate(data_file, 1): |
| 98 | line = line.rstrip(b'\r\n') |
| 99 | if not line: |
| 100 | in_paragraph = False |
| 101 | continue |
| 102 | if line.startswith(b'#'): |
| 103 | continue |
| 104 | if not in_paragraph: |
| 105 | # This is a test case description line. |
| 106 | self.process_test_case(descriptions, |
| 107 | data_file_name, line_number, line) |
| 108 | in_paragraph = True |
| 109 | |
Pengyu Lv | ce980e6 | 2023-11-30 16:53:31 +0800 | [diff] [blame] | 110 | def collect_from_script(self, script_name): |
Tomás González | 4a86da2 | 2023-09-01 17:41:16 +0100 | [diff] [blame] | 111 | """Collect the test cases in a script by calling its listing test cases |
| 112 | option""" |
Gilles Peskine | bbb3664 | 2020-07-03 00:30:12 +0200 | [diff] [blame] | 113 | descriptions = self.new_per_file_state() # pylint: disable=assignment-from-none |
Pengyu Lv | ce980e6 | 2023-11-30 16:53:31 +0800 | [diff] [blame] | 114 | listed = subprocess.check_output(['sh', script_name, '--list-test-cases']) |
Tomás González | 4a86da2 | 2023-09-01 17:41:16 +0100 | [diff] [blame] | 115 | # Assume test file is responsible for printing identical format of |
| 116 | # test case description between --list-test-cases and its OUTCOME.CSV |
Tomás González | 38ecf9f | 2023-09-04 10:23:04 +0100 | [diff] [blame] | 117 | # |
Yanray Wang | 63f0abe | 2023-08-30 18:31:35 +0800 | [diff] [blame] | 118 | # idx indicates the number of test case since there is no line number |
Tomás González | 7f2cddb | 2023-10-27 11:45:26 +0100 | [diff] [blame] | 119 | # in the script for each test case. |
Pengyu Lv | ce980e6 | 2023-11-30 16:53:31 +0800 | [diff] [blame] | 120 | for idx, line in enumerate(listed.splitlines()): |
| 121 | # We are expecting the script to list the test cases in |
| 122 | # `<suite_name>;<description>` pattern. |
| 123 | script_outputs = line.split(b';', 1) |
| 124 | if len(script_outputs) == 2: |
| 125 | suite_name, description = script_outputs |
| 126 | else: |
| 127 | raise ScriptOutputError(script_name, idx, line.decode("utf-8")) |
| 128 | |
Tomás González | 38ecf9f | 2023-09-04 10:23:04 +0100 | [diff] [blame] | 129 | self.process_test_case(descriptions, |
Pengyu Lv | ce980e6 | 2023-11-30 16:53:31 +0800 | [diff] [blame] | 130 | suite_name.decode('utf-8'), |
Tomás González | 38ecf9f | 2023-09-04 10:23:04 +0100 | [diff] [blame] | 131 | idx, |
| 132 | description.rstrip()) |
Yanray Wang | 2354693 | 2023-02-24 14:53:29 +0800 | [diff] [blame] | 133 | |
Gilles Peskine | 6f6ff33 | 2020-06-25 16:40:10 +0200 | [diff] [blame] | 134 | @staticmethod |
| 135 | def collect_test_directories(): |
| 136 | """Get the relative path for the TLS and Crypto test directories.""" |
Gilles Peskine | 1398556 | 2024-10-03 19:11:27 +0200 | [diff] [blame] | 137 | mbedtls_root = build_tree.guess_mbedtls_root() |
| 138 | directories = [os.path.join(mbedtls_root, 'tests'), |
| 139 | os.path.join(mbedtls_root, 'tf-psa-crypto', 'tests')] |
| 140 | directories = [os.path.relpath(p) for p in directories] |
Gilles Peskine | 6f6ff33 | 2020-06-25 16:40:10 +0200 | [diff] [blame] | 141 | return directories |
| 142 | |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 143 | def walk_all(self): |
| 144 | """Iterate over all named test cases.""" |
Gilles Peskine | 6f6ff33 | 2020-06-25 16:40:10 +0200 | [diff] [blame] | 145 | test_directories = self.collect_test_directories() |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 146 | for directory in test_directories: |
| 147 | for data_file_name in glob.glob(os.path.join(directory, 'suites', |
| 148 | '*.data')): |
| 149 | self.walk_test_suite(data_file_name) |
Tomás González | 4a86da2 | 2023-09-01 17:41:16 +0100 | [diff] [blame] | 150 | |
| 151 | for sh_file in ['ssl-opt.sh', 'compat.sh']: |
| 152 | sh_file = os.path.join(directory, sh_file) |
Gilles Peskine | 1398556 | 2024-10-03 19:11:27 +0200 | [diff] [blame] | 153 | if os.path.isfile(sh_file): |
| 154 | self.collect_from_script(sh_file) |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 155 | |
Gilles Peskine | 686c292 | 2022-01-07 15:58:38 +0100 | [diff] [blame] | 156 | class TestDescriptions(TestDescriptionExplorer): |
| 157 | """Collect the available test cases.""" |
| 158 | |
| 159 | def __init__(self): |
| 160 | super().__init__() |
| 161 | self.descriptions = set() |
| 162 | |
| 163 | def process_test_case(self, _per_file_state, |
| 164 | file_name, _line_number, description): |
| 165 | """Record an available test case.""" |
Pengyu Lv | ce980e6 | 2023-11-30 16:53:31 +0800 | [diff] [blame] | 166 | base_name = re.sub(r'\.[^.]*$', '', re.sub(r'.*/', '', file_name)) |
| 167 | key = ';'.join([base_name, description.decode('utf-8')]) |
Gilles Peskine | 686c292 | 2022-01-07 15:58:38 +0100 | [diff] [blame] | 168 | self.descriptions.add(key) |
| 169 | |
| 170 | def collect_available_test_cases(): |
| 171 | """Collect the available test cases.""" |
| 172 | explorer = TestDescriptions() |
| 173 | explorer.walk_all() |
| 174 | return sorted(explorer.descriptions) |
| 175 | |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 176 | class DescriptionChecker(TestDescriptionExplorer): |
| 177 | """Check all test case descriptions. |
| 178 | |
| 179 | * Check that each description is valid (length, allowed character set, etc.). |
| 180 | * Check that there is no duplicated description inside of one test suite. |
| 181 | """ |
| 182 | |
| 183 | def __init__(self, results): |
| 184 | self.results = results |
| 185 | |
Gilles Peskine | bbb3664 | 2020-07-03 00:30:12 +0200 | [diff] [blame] | 186 | def new_per_file_state(self): |
Gilles Peskine | 6f6ff33 | 2020-06-25 16:40:10 +0200 | [diff] [blame] | 187 | """Dictionary mapping descriptions to their line number.""" |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 188 | return {} |
| 189 | |
| 190 | def process_test_case(self, per_file_state, |
| 191 | file_name, line_number, description): |
Gilles Peskine | 6f6ff33 | 2020-06-25 16:40:10 +0200 | [diff] [blame] | 192 | """Check test case descriptions for errors.""" |
| 193 | results = self.results |
| 194 | seen = per_file_state |
| 195 | if description in seen: |
| 196 | results.error(file_name, line_number, |
| 197 | 'Duplicate description (also line {})', |
| 198 | seen[description]) |
| 199 | return |
| 200 | if re.search(br'[\t;]', description): |
| 201 | results.error(file_name, line_number, |
| 202 | 'Forbidden character \'{}\' in description', |
| 203 | re.search(br'[\t;]', description).group(0).decode('ascii')) |
| 204 | if re.search(br'[^ -~]', description): |
| 205 | results.error(file_name, line_number, |
| 206 | 'Non-ASCII character in description') |
| 207 | if len(description) > 66: |
| 208 | results.warning(file_name, line_number, |
| 209 | 'Test description too long ({} > 66)', |
| 210 | len(description)) |
| 211 | seen[description] = line_number |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 212 | |
| 213 | def main(): |
Gilles Peskine | 1fb7aea | 2019-12-02 14:26:04 +0100 | [diff] [blame] | 214 | parser = argparse.ArgumentParser(description=__doc__) |
Gilles Peskine | 7e09105 | 2022-01-07 15:58:55 +0100 | [diff] [blame] | 215 | parser.add_argument('--list-all', |
| 216 | action='store_true', |
| 217 | help='List all test cases, without doing checks') |
Gilles Peskine | 1fb7aea | 2019-12-02 14:26:04 +0100 | [diff] [blame] | 218 | parser.add_argument('--quiet', '-q', |
| 219 | action='store_true', |
| 220 | help='Hide warnings') |
| 221 | parser.add_argument('--verbose', '-v', |
| 222 | action='store_false', dest='quiet', |
| 223 | help='Show warnings (default: on; undoes --quiet)') |
| 224 | options = parser.parse_args() |
Gilles Peskine | 7e09105 | 2022-01-07 15:58:55 +0100 | [diff] [blame] | 225 | if options.list_all: |
| 226 | descriptions = collect_available_test_cases() |
| 227 | sys.stdout.write('\n'.join(descriptions + [''])) |
| 228 | return |
Gilles Peskine | 1fb7aea | 2019-12-02 14:26:04 +0100 | [diff] [blame] | 229 | results = Results(options) |
Gilles Peskine | 78c45db | 2020-06-25 16:34:11 +0200 | [diff] [blame] | 230 | checker = DescriptionChecker(results) |
Pengyu Lv | ce980e6 | 2023-11-30 16:53:31 +0800 | [diff] [blame] | 231 | try: |
| 232 | checker.walk_all() |
| 233 | except ScriptOutputError as e: |
| 234 | results.error(e.script_name, e.idx, |
| 235 | '"{}" should be listed as "<suite_name>;<description>"', |
| 236 | e.line) |
Gilles Peskine | 1fb7aea | 2019-12-02 14:26:04 +0100 | [diff] [blame] | 237 | if (results.warnings or results.errors) and not options.quiet: |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 238 | sys.stderr.write('{}: {} errors, {} warnings\n' |
| 239 | .format(sys.argv[0], results.errors, results.warnings)) |
| 240 | sys.exit(1 if results.errors else 0) |
| 241 | |
| 242 | if __name__ == '__main__': |
| 243 | main() |