Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | """Sanity checks for test data. |
| 4 | """ |
| 5 | |
| 6 | # Copyright (C) 2019, Arm Limited, All Rights Reserved |
| 7 | # SPDX-License-Identifier: Apache-2.0 |
| 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. |
| 11 | # You may obtain a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | # See the License for the specific language governing permissions and |
| 19 | # limitations under the License. |
| 20 | # |
| 21 | # This file is part of Mbed TLS (https://tls.mbed.org) |
| 22 | |
| 23 | import glob |
| 24 | import os |
| 25 | import re |
| 26 | import sys |
| 27 | |
| 28 | class Results: |
Darryl Green | fb5faa2 | 2019-12-17 10:17:20 +0000 | [diff] [blame] | 29 | """Store file and line information about errors or warnings in test suites.""" |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 30 | def __init__(self): |
| 31 | self.errors = 0 |
| 32 | self.warnings = 0 |
| 33 | |
| 34 | def error(self, file_name, line_number, fmt, *args): |
| 35 | sys.stderr.write(('{}:{}:ERROR:' + fmt + '\n'). |
| 36 | format(file_name, line_number, *args)) |
| 37 | self.errors += 1 |
| 38 | |
| 39 | def warning(self, file_name, line_number, fmt, *args): |
| 40 | sys.stderr.write(('{}:{}:Warning:' + fmt + '\n') |
Gilles Peskine | 283df2e | 2019-09-20 17:56:29 +0200 | [diff] [blame] | 41 | .format(file_name, line_number, *args)) |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 42 | self.warnings += 1 |
| 43 | |
| 44 | def collect_test_directories(): |
Darryl Green | fb5faa2 | 2019-12-17 10:17:20 +0000 | [diff] [blame] | 45 | """Get the relative path for the TLS and Crypto test directories.""" |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 46 | if os.path.isdir('tests'): |
| 47 | tests_dir = 'tests' |
| 48 | elif os.path.isdir('suites'): |
| 49 | tests_dir = '.' |
| 50 | elif os.path.isdir('../suites'): |
| 51 | tests_dir = '..' |
| 52 | directories = [tests_dir] |
| 53 | crypto_tests_dir = os.path.normpath(os.path.join(tests_dir, |
| 54 | '../crypto/tests')) |
| 55 | if os.path.isdir(crypto_tests_dir): |
| 56 | directories.append(crypto_tests_dir) |
| 57 | return directories |
| 58 | |
Gilles Peskine | 32b9421 | 2019-09-20 18:00:49 +0200 | [diff] [blame] | 59 | def check_description(results, seen, file_name, line_number, description): |
Darryl Green | fb5faa2 | 2019-12-17 10:17:20 +0000 | [diff] [blame] | 60 | """Check test case descriptions for errors.""" |
Gilles Peskine | 32b9421 | 2019-09-20 18:00:49 +0200 | [diff] [blame] | 61 | if description in seen: |
| 62 | results.error(file_name, line_number, |
| 63 | 'Duplicate description (also line {})', |
| 64 | seen[description]) |
| 65 | return |
Gilles Peskine | f12ad58 | 2019-09-20 18:02:01 +0200 | [diff] [blame] | 66 | if re.search(br'[\t;]', description): |
Gilles Peskine | 32b9421 | 2019-09-20 18:00:49 +0200 | [diff] [blame] | 67 | results.error(file_name, line_number, |
| 68 | 'Forbidden character \'{}\' in description', |
Gilles Peskine | f12ad58 | 2019-09-20 18:02:01 +0200 | [diff] [blame] | 69 | re.search(br'[\t;]', description).group(0).decode('ascii')) |
Gilles Peskine | 57870e8 | 2019-09-20 18:02:30 +0200 | [diff] [blame] | 70 | if re.search(br'[^ -~]', description): |
| 71 | results.error(file_name, line_number, |
| 72 | 'Non-ASCII character in description') |
Gilles Peskine | 32b9421 | 2019-09-20 18:00:49 +0200 | [diff] [blame] | 73 | if len(description) > 66: |
| 74 | results.warning(file_name, line_number, |
| 75 | 'Test description too long ({} > 66)', |
| 76 | len(description)) |
| 77 | seen[description] = line_number |
| 78 | |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 79 | def check_test_suite(results, data_file_name): |
| 80 | in_paragraph = False |
| 81 | descriptions = {} |
Gilles Peskine | f12ad58 | 2019-09-20 18:02:01 +0200 | [diff] [blame] | 82 | with open(data_file_name, 'rb') as data_file: |
| 83 | for line_number, line in enumerate(data_file, 1): |
| 84 | line = line.rstrip(b'\r\n') |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 85 | if not line: |
| 86 | in_paragraph = False |
| 87 | continue |
Gilles Peskine | f12ad58 | 2019-09-20 18:02:01 +0200 | [diff] [blame] | 88 | if line.startswith(b'#'): |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 89 | continue |
| 90 | if not in_paragraph: |
| 91 | # This is a test case description line. |
Gilles Peskine | 32b9421 | 2019-09-20 18:00:49 +0200 | [diff] [blame] | 92 | check_description(results, descriptions, |
| 93 | data_file_name, line_number, line) |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 94 | in_paragraph = True |
| 95 | |
| 96 | def check_ssl_opt_sh(results, file_name): |
| 97 | descriptions = {} |
Gilles Peskine | f12ad58 | 2019-09-20 18:02:01 +0200 | [diff] [blame] | 98 | with open(file_name, 'rb') as file_contents: |
| 99 | for line_number, line in enumerate(file_contents, 1): |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 100 | # Assume that all run_test calls have the same simple form |
| 101 | # with the test description entirely on the same line as the |
| 102 | # function name. |
Gilles Peskine | f12ad58 | 2019-09-20 18:02:01 +0200 | [diff] [blame] | 103 | m = re.match(br'\s*run_test\s+"((?:[^\\"]|\\.)*)"', line) |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 104 | if not m: |
| 105 | continue |
| 106 | description = m.group(1) |
Gilles Peskine | 32b9421 | 2019-09-20 18:00:49 +0200 | [diff] [blame] | 107 | check_description(results, descriptions, |
| 108 | file_name, line_number, description) |
Gilles Peskine | ba94b58 | 2019-09-16 19:18:40 +0200 | [diff] [blame] | 109 | |
| 110 | def main(): |
| 111 | test_directories = collect_test_directories() |
| 112 | results = Results() |
| 113 | for directory in test_directories: |
| 114 | for data_file_name in glob.glob(os.path.join(directory, 'suites', |
| 115 | '*.data')): |
| 116 | check_test_suite(results, data_file_name) |
| 117 | ssl_opt_sh = os.path.join(directory, 'ssl-opt.sh') |
| 118 | if os.path.exists(ssl_opt_sh): |
| 119 | check_ssl_opt_sh(results, ssl_opt_sh) |
| 120 | if results.warnings or results.errors: |
| 121 | sys.stderr.write('{}: {} errors, {} warnings\n' |
| 122 | .format(sys.argv[0], results.errors, results.warnings)) |
| 123 | sys.exit(1 if results.errors else 0) |
| 124 | |
| 125 | if __name__ == '__main__': |
| 126 | main() |