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