blob: 31fc34c52334e94362589ebe2ad60e69944e42be [file] [log] [blame]
Gilles Peskineba94b582019-09-16 19:18:40 +02001#!/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
Gilles Peskine1fb7aea2019-12-02 14:26:04 +010023import argparse
Gilles Peskineba94b582019-09-16 19:18:40 +020024import glob
25import os
26import re
27import sys
28
29class Results:
Darryl Green18220612019-12-17 15:03:59 +000030 """Store file and line information about errors or warnings in test suites."""
Gilles Peskine1fb7aea2019-12-02 14:26:04 +010031
32 def __init__(self, options):
Gilles Peskineba94b582019-09-16 19:18:40 +020033 self.errors = 0
34 self.warnings = 0
Gilles Peskine1fb7aea2019-12-02 14:26:04 +010035 self.ignore_warnings = options.quiet
Gilles Peskineba94b582019-09-16 19:18:40 +020036
37 def error(self, file_name, line_number, fmt, *args):
38 sys.stderr.write(('{}:{}:ERROR:' + fmt + '\n').
39 format(file_name, line_number, *args))
40 self.errors += 1
41
42 def warning(self, file_name, line_number, fmt, *args):
Gilles Peskine1fb7aea2019-12-02 14:26:04 +010043 if not self.ignore_warnings:
44 sys.stderr.write(('{}:{}:Warning:' + fmt + '\n')
45 .format(file_name, line_number, *args))
46 self.warnings += 1
Gilles Peskineba94b582019-09-16 19:18:40 +020047
48def collect_test_directories():
Darryl Green18220612019-12-17 15:03:59 +000049 """Get the relative path for the TLS and Crypto test directories."""
Gilles Peskineba94b582019-09-16 19:18:40 +020050 if os.path.isdir('tests'):
51 tests_dir = 'tests'
52 elif os.path.isdir('suites'):
53 tests_dir = '.'
54 elif os.path.isdir('../suites'):
55 tests_dir = '..'
56 directories = [tests_dir]
57 crypto_tests_dir = os.path.normpath(os.path.join(tests_dir,
58 '../crypto/tests'))
59 if os.path.isdir(crypto_tests_dir):
60 directories.append(crypto_tests_dir)
61 return directories
62
Gilles Peskine32b94212019-09-20 18:00:49 +020063def check_description(results, seen, file_name, line_number, description):
Darryl Green18220612019-12-17 15:03:59 +000064 """Check test case descriptions for errors."""
Gilles Peskine32b94212019-09-20 18:00:49 +020065 if description in seen:
66 results.error(file_name, line_number,
67 'Duplicate description (also line {})',
68 seen[description])
69 return
Gilles Peskinef12ad582019-09-20 18:02:01 +020070 if re.search(br'[\t;]', description):
Gilles Peskine32b94212019-09-20 18:00:49 +020071 results.error(file_name, line_number,
72 'Forbidden character \'{}\' in description',
Gilles Peskinef12ad582019-09-20 18:02:01 +020073 re.search(br'[\t;]', description).group(0).decode('ascii'))
Gilles Peskine57870e82019-09-20 18:02:30 +020074 if re.search(br'[^ -~]', description):
75 results.error(file_name, line_number,
76 'Non-ASCII character in description')
Gilles Peskine32b94212019-09-20 18:00:49 +020077 if len(description) > 66:
78 results.warning(file_name, line_number,
79 'Test description too long ({} > 66)',
80 len(description))
81 seen[description] = line_number
82
Gilles Peskineba94b582019-09-16 19:18:40 +020083def check_test_suite(results, data_file_name):
84 in_paragraph = False
85 descriptions = {}
Gilles Peskinef12ad582019-09-20 18:02:01 +020086 with open(data_file_name, 'rb') as data_file:
87 for line_number, line in enumerate(data_file, 1):
88 line = line.rstrip(b'\r\n')
Gilles Peskineba94b582019-09-16 19:18:40 +020089 if not line:
90 in_paragraph = False
91 continue
Gilles Peskinef12ad582019-09-20 18:02:01 +020092 if line.startswith(b'#'):
Gilles Peskineba94b582019-09-16 19:18:40 +020093 continue
94 if not in_paragraph:
95 # This is a test case description line.
Gilles Peskine32b94212019-09-20 18:00:49 +020096 check_description(results, descriptions,
97 data_file_name, line_number, line)
Gilles Peskineba94b582019-09-16 19:18:40 +020098 in_paragraph = True
99
100def check_ssl_opt_sh(results, file_name):
101 descriptions = {}
Gilles Peskinef12ad582019-09-20 18:02:01 +0200102 with open(file_name, 'rb') as file_contents:
103 for line_number, line in enumerate(file_contents, 1):
Gilles Peskineba94b582019-09-16 19:18:40 +0200104 # 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.
Gilles Peskinef12ad582019-09-20 18:02:01 +0200107 m = re.match(br'\s*run_test\s+"((?:[^\\"]|\\.)*)"', line)
Gilles Peskineba94b582019-09-16 19:18:40 +0200108 if not m:
109 continue
110 description = m.group(1)
Gilles Peskine32b94212019-09-20 18:00:49 +0200111 check_description(results, descriptions,
112 file_name, line_number, description)
Gilles Peskineba94b582019-09-16 19:18:40 +0200113
114def main():
Gilles Peskine1fb7aea2019-12-02 14:26:04 +0100115 parser = argparse.ArgumentParser(description=__doc__)
116 parser.add_argument('--quiet', '-q',
117 action='store_true',
118 help='Hide warnings')
119 parser.add_argument('--verbose', '-v',
120 action='store_false', dest='quiet',
121 help='Show warnings (default: on; undoes --quiet)')
122 options = parser.parse_args()
Gilles Peskineba94b582019-09-16 19:18:40 +0200123 test_directories = collect_test_directories()
Gilles Peskine1fb7aea2019-12-02 14:26:04 +0100124 results = Results(options)
Gilles Peskineba94b582019-09-16 19:18:40 +0200125 for directory in test_directories:
126 for data_file_name in glob.glob(os.path.join(directory, 'suites',
127 '*.data')):
128 check_test_suite(results, data_file_name)
129 ssl_opt_sh = os.path.join(directory, 'ssl-opt.sh')
130 if os.path.exists(ssl_opt_sh):
131 check_ssl_opt_sh(results, ssl_opt_sh)
Gilles Peskine1fb7aea2019-12-02 14:26:04 +0100132 if (results.warnings or results.errors) and not options.quiet:
Gilles Peskineba94b582019-09-16 19:18:40 +0200133 sys.stderr.write('{}: {} errors, {} warnings\n'
134 .format(sys.argv[0], results.errors, results.warnings))
135 sys.exit(1 if results.errors else 0)
136
137if __name__ == '__main__':
138 main()