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