blob: d84ed042c4e8ed64286b8e72aec2556fe82ec3b5 [file] [log] [blame]
Gilles Peskineba94b582019-09-16 19:18:40 +02001#!/usr/bin/env python3
2
3"""Sanity checks for test data.
Gilles Peskinebbb36642020-07-03 00:30:12 +02004
5This program contains a class for traversing test cases that can be used
6independently of the checks.
Gilles Peskineba94b582019-09-16 19:18:40 +02007"""
8
Bence Szépkúti1e148272020-08-07 13:07:28 +02009# Copyright The Mbed TLS Contributors
Gilles Peskineba94b582019-09-16 19:18:40 +020010# 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 Peskineba94b582019-09-16 19:18:40 +020023
Gilles Peskine1fb7aea2019-12-02 14:26:04 +010024import argparse
Gilles Peskineba94b582019-09-16 19:18:40 +020025import glob
26import os
27import re
28import sys
29
30class Results:
Darryl Green18220612019-12-17 15:03:59 +000031 """Store file and line information about errors or warnings in test suites."""
Gilles Peskine1fb7aea2019-12-02 14:26:04 +010032
33 def __init__(self, options):
Gilles Peskineba94b582019-09-16 19:18:40 +020034 self.errors = 0
35 self.warnings = 0
Gilles Peskine1fb7aea2019-12-02 14:26:04 +010036 self.ignore_warnings = options.quiet
Gilles Peskineba94b582019-09-16 19:18:40 +020037
38 def error(self, file_name, line_number, fmt, *args):
39 sys.stderr.write(('{}:{}:ERROR:' + fmt + '\n').
40 format(file_name, line_number, *args))
41 self.errors += 1
42
43 def warning(self, file_name, line_number, fmt, *args):
Gilles Peskine1fb7aea2019-12-02 14:26:04 +010044 if not self.ignore_warnings:
45 sys.stderr.write(('{}:{}:Warning:' + fmt + '\n')
46 .format(file_name, line_number, *args))
47 self.warnings += 1
Gilles Peskineba94b582019-09-16 19:18:40 +020048
Gilles Peskine78c45db2020-06-25 16:34:11 +020049class TestDescriptionExplorer:
50 """An iterator over test cases with descriptions.
Gilles Peskined34e9e42020-06-25 16:16:25 +020051
Gilles Peskine78c45db2020-06-25 16:34:11 +020052The test cases that have descriptions are:
53* Individual unit tests (entries in a .data file) in test suites.
54* Individual test cases in ssl-opt.sh.
55
56This is an abstract class. To use it, derive a class that implements
57the process_test_case method, and call walk_all().
Gilles Peskined34e9e42020-06-25 16:16:25 +020058"""
Gilles Peskineba94b582019-09-16 19:18:40 +020059
Gilles Peskine78c45db2020-06-25 16:34:11 +020060 def process_test_case(self, per_file_state,
61 file_name, line_number, description):
62 """Process a test case.
Gilles Peskined34e9e42020-06-25 16:16:25 +020063
Gilles Peskinebbb36642020-07-03 00:30:12 +020064per_file_state: an object created by new_per_file_state() at the beginning
65 of each file.
Gilles Peskine78c45db2020-06-25 16:34:11 +020066file_name: a relative path to the file containing the test case.
67line_number: the line number in the given file.
68description: the test case description as a byte string.
Gilles Peskined34e9e42020-06-25 16:16:25 +020069"""
Gilles Peskine78c45db2020-06-25 16:34:11 +020070 raise NotImplementedError
Gilles Peskined34e9e42020-06-25 16:16:25 +020071
Gilles Peskinebbb36642020-07-03 00:30:12 +020072 def new_per_file_state(self):
Gilles Peskine78c45db2020-06-25 16:34:11 +020073 """Return a new per-file state object.
Gilles Peskined34e9e42020-06-25 16:16:25 +020074
Gilles Peskine78c45db2020-06-25 16:34:11 +020075The default per-file state object is None. Child classes that require per-file
76state may override this method.
Gilles Peskined34e9e42020-06-25 16:16:25 +020077"""
Gilles Peskine78c45db2020-06-25 16:34:11 +020078 #pylint: disable=no-self-use
79 return None
80
81 def walk_test_suite(self, data_file_name):
82 """Iterate over the test cases in the given unit test data file."""
83 in_paragraph = False
Gilles Peskinebbb36642020-07-03 00:30:12 +020084 descriptions = self.new_per_file_state() # pylint: disable=assignment-from-none
Gilles Peskine78c45db2020-06-25 16:34:11 +020085 with open(data_file_name, 'rb') as data_file:
86 for line_number, line in enumerate(data_file, 1):
87 line = line.rstrip(b'\r\n')
88 if not line:
89 in_paragraph = False
90 continue
91 if line.startswith(b'#'):
92 continue
93 if not in_paragraph:
94 # This is a test case description line.
95 self.process_test_case(descriptions,
96 data_file_name, line_number, line)
97 in_paragraph = True
98
99 def walk_ssl_opt_sh(self, file_name):
100 """Iterate over the test cases in ssl-opt.sh or a file with a similar format."""
Gilles Peskinebbb36642020-07-03 00:30:12 +0200101 descriptions = self.new_per_file_state() # pylint: disable=assignment-from-none
Gilles Peskine78c45db2020-06-25 16:34:11 +0200102 with open(file_name, 'rb') as file_contents:
103 for line_number, line in enumerate(file_contents, 1):
104 # Assume that all run_test calls have the same simple form
105 # with the test description entirely on the same line as the
106 # function name.
107 m = re.match(br'\s*run_test\s+"((?:[^\\"]|\\.)*)"', line)
108 if not m:
109 continue
110 description = m.group(1)
111 self.process_test_case(descriptions,
112 file_name, line_number, description)
113
Gilles Peskine6f6ff332020-06-25 16:40:10 +0200114 @staticmethod
115 def collect_test_directories():
116 """Get the relative path for the TLS and Crypto test directories."""
117 if os.path.isdir('tests'):
118 tests_dir = 'tests'
119 elif os.path.isdir('suites'):
120 tests_dir = '.'
121 elif os.path.isdir('../suites'):
122 tests_dir = '..'
123 directories = [tests_dir]
124 return directories
125
Gilles Peskine78c45db2020-06-25 16:34:11 +0200126 def walk_all(self):
127 """Iterate over all named test cases."""
Gilles Peskine6f6ff332020-06-25 16:40:10 +0200128 test_directories = self.collect_test_directories()
Gilles Peskine78c45db2020-06-25 16:34:11 +0200129 for directory in test_directories:
130 for data_file_name in glob.glob(os.path.join(directory, 'suites',
131 '*.data')):
132 self.walk_test_suite(data_file_name)
133 ssl_opt_sh = os.path.join(directory, 'ssl-opt.sh')
134 if os.path.exists(ssl_opt_sh):
135 self.walk_ssl_opt_sh(ssl_opt_sh)
Jerry Yuf17a60f2021-11-26 20:40:17 +0800136 for ssl_opt_file_name in glob.glob(os.path.join(directory, 'opt-testcases',
137 '*.sh')):
138 self.walk_ssl_opt_sh(ssl_opt_file_name)
Gilles Peskine78c45db2020-06-25 16:34:11 +0200139
Gilles Peskine686c2922022-01-07 15:58:38 +0100140class TestDescriptions(TestDescriptionExplorer):
141 """Collect the available test cases."""
142
143 def __init__(self):
144 super().__init__()
145 self.descriptions = set()
146
147 def process_test_case(self, _per_file_state,
148 file_name, _line_number, description):
149 """Record an available test case."""
150 base_name = re.sub(r'\.[^.]*$', '', re.sub(r'.*/', '', file_name))
151 key = ';'.join([base_name, description.decode('utf-8')])
152 self.descriptions.add(key)
153
154def collect_available_test_cases():
155 """Collect the available test cases."""
156 explorer = TestDescriptions()
157 explorer.walk_all()
158 return sorted(explorer.descriptions)
159
Gilles Peskine78c45db2020-06-25 16:34:11 +0200160class DescriptionChecker(TestDescriptionExplorer):
161 """Check all test case descriptions.
162
163* Check that each description is valid (length, allowed character set, etc.).
164* Check that there is no duplicated description inside of one test suite.
165"""
166
167 def __init__(self, results):
168 self.results = results
169
Gilles Peskinebbb36642020-07-03 00:30:12 +0200170 def new_per_file_state(self):
Gilles Peskine6f6ff332020-06-25 16:40:10 +0200171 """Dictionary mapping descriptions to their line number."""
Gilles Peskine78c45db2020-06-25 16:34:11 +0200172 return {}
173
174 def process_test_case(self, per_file_state,
175 file_name, line_number, description):
Gilles Peskine6f6ff332020-06-25 16:40:10 +0200176 """Check test case descriptions for errors."""
177 results = self.results
178 seen = per_file_state
179 if description in seen:
180 results.error(file_name, line_number,
181 'Duplicate description (also line {})',
182 seen[description])
183 return
184 if re.search(br'[\t;]', description):
185 results.error(file_name, line_number,
186 'Forbidden character \'{}\' in description',
187 re.search(br'[\t;]', description).group(0).decode('ascii'))
188 if re.search(br'[^ -~]', description):
189 results.error(file_name, line_number,
190 'Non-ASCII character in description')
191 if len(description) > 66:
192 results.warning(file_name, line_number,
193 'Test description too long ({} > 66)',
194 len(description))
195 seen[description] = line_number
Gilles Peskineba94b582019-09-16 19:18:40 +0200196
197def main():
Gilles Peskine1fb7aea2019-12-02 14:26:04 +0100198 parser = argparse.ArgumentParser(description=__doc__)
Gilles Peskine7e091052022-01-07 15:58:55 +0100199 parser.add_argument('--list-all',
200 action='store_true',
201 help='List all test cases, without doing checks')
Gilles Peskine1fb7aea2019-12-02 14:26:04 +0100202 parser.add_argument('--quiet', '-q',
203 action='store_true',
204 help='Hide warnings')
205 parser.add_argument('--verbose', '-v',
206 action='store_false', dest='quiet',
207 help='Show warnings (default: on; undoes --quiet)')
208 options = parser.parse_args()
Gilles Peskine7e091052022-01-07 15:58:55 +0100209 if options.list_all:
210 descriptions = collect_available_test_cases()
211 sys.stdout.write('\n'.join(descriptions + ['']))
212 return
Gilles Peskine1fb7aea2019-12-02 14:26:04 +0100213 results = Results(options)
Gilles Peskine78c45db2020-06-25 16:34:11 +0200214 checker = DescriptionChecker(results)
215 checker.walk_all()
Gilles Peskine1fb7aea2019-12-02 14:26:04 +0100216 if (results.warnings or results.errors) and not options.quiet:
Gilles Peskineba94b582019-09-16 19:18:40 +0200217 sys.stderr.write('{}: {} errors, {} warnings\n'
218 .format(sys.argv[0], results.errors, results.warnings))
219 sys.exit(1 if results.errors else 0)
220
221if __name__ == '__main__':
222 main()