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