blob: 8a37a9702c94fbc79bda836c256f0c798decd52e [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
23import glob
24import os
25import re
26import sys
27
28class Results:
29 def __init__(self):
30 self.errors = 0
31 self.warnings = 0
32
33 def error(self, file_name, line_number, fmt, *args):
34 sys.stderr.write(('{}:{}:ERROR:' + fmt + '\n').
35 format(file_name, line_number, *args))
36 self.errors += 1
37
38 def warning(self, file_name, line_number, fmt, *args):
39 sys.stderr.write(('{}:{}:Warning:' + fmt + '\n')
Gilles Peskine283df2e2019-09-20 17:56:29 +020040 .format(file_name, line_number, *args))
Gilles Peskineba94b582019-09-16 19:18:40 +020041 self.warnings += 1
42
43def collect_test_directories():
44 if os.path.isdir('tests'):
45 tests_dir = 'tests'
46 elif os.path.isdir('suites'):
47 tests_dir = '.'
48 elif os.path.isdir('../suites'):
49 tests_dir = '..'
50 directories = [tests_dir]
51 crypto_tests_dir = os.path.normpath(os.path.join(tests_dir,
52 '../crypto/tests'))
53 if os.path.isdir(crypto_tests_dir):
54 directories.append(crypto_tests_dir)
55 return directories
56
Gilles Peskine32b94212019-09-20 18:00:49 +020057def check_description(results, seen, file_name, line_number, description):
58 if description in seen:
59 results.error(file_name, line_number,
60 'Duplicate description (also line {})',
61 seen[description])
62 return
Gilles Peskinef12ad582019-09-20 18:02:01 +020063 if re.search(br'[\t;]', description):
Gilles Peskine32b94212019-09-20 18:00:49 +020064 results.error(file_name, line_number,
65 'Forbidden character \'{}\' in description',
Gilles Peskinef12ad582019-09-20 18:02:01 +020066 re.search(br'[\t;]', description).group(0).decode('ascii'))
Gilles Peskine32b94212019-09-20 18:00:49 +020067 if len(description) > 66:
68 results.warning(file_name, line_number,
69 'Test description too long ({} > 66)',
70 len(description))
71 seen[description] = line_number
72
Gilles Peskineba94b582019-09-16 19:18:40 +020073def check_test_suite(results, data_file_name):
74 in_paragraph = False
75 descriptions = {}
Gilles Peskinef12ad582019-09-20 18:02:01 +020076 with open(data_file_name, 'rb') as data_file:
77 for line_number, line in enumerate(data_file, 1):
78 line = line.rstrip(b'\r\n')
Gilles Peskineba94b582019-09-16 19:18:40 +020079 if not line:
80 in_paragraph = False
81 continue
Gilles Peskinef12ad582019-09-20 18:02:01 +020082 if line.startswith(b'#'):
Gilles Peskineba94b582019-09-16 19:18:40 +020083 continue
84 if not in_paragraph:
85 # This is a test case description line.
Gilles Peskine32b94212019-09-20 18:00:49 +020086 check_description(results, descriptions,
87 data_file_name, line_number, line)
Gilles Peskineba94b582019-09-16 19:18:40 +020088 in_paragraph = True
89
90def check_ssl_opt_sh(results, file_name):
91 descriptions = {}
Gilles Peskinef12ad582019-09-20 18:02:01 +020092 with open(file_name, 'rb') as file_contents:
93 for line_number, line in enumerate(file_contents, 1):
Gilles Peskineba94b582019-09-16 19:18:40 +020094 # Assume that all run_test calls have the same simple form
95 # with the test description entirely on the same line as the
96 # function name.
Gilles Peskinef12ad582019-09-20 18:02:01 +020097 m = re.match(br'\s*run_test\s+"((?:[^\\"]|\\.)*)"', line)
Gilles Peskineba94b582019-09-16 19:18:40 +020098 if not m:
99 continue
100 description = m.group(1)
Gilles Peskine32b94212019-09-20 18:00:49 +0200101 check_description(results, descriptions,
102 file_name, line_number, description)
Gilles Peskineba94b582019-09-16 19:18:40 +0200103
104def main():
105 test_directories = collect_test_directories()
106 results = Results()
107 for directory in test_directories:
108 for data_file_name in glob.glob(os.path.join(directory, 'suites',
109 '*.data')):
110 check_test_suite(results, data_file_name)
111 ssl_opt_sh = os.path.join(directory, 'ssl-opt.sh')
112 if os.path.exists(ssl_opt_sh):
113 check_ssl_opt_sh(results, ssl_opt_sh)
114 if results.warnings or results.errors:
115 sys.stderr.write('{}: {} errors, {} warnings\n'
116 .format(sys.argv[0], results.errors, results.warnings))
117 sys.exit(1 if results.errors else 0)
118
119if __name__ == '__main__':
120 main()