blob: 9e67a4207c81353d19b70c76557c1aa81d87b8c3 [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')
40 .format(file_name, line_number, args))
41 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
57def check_test_suite(results, data_file_name):
58 in_paragraph = False
59 descriptions = {}
60 line_number = 0
61 with open(data_file_name) as data_file:
62 for line in data_file:
63 line_number += 1
64 line = line.rstrip('\r\n')
65 if not line:
66 in_paragraph = False
67 continue
68 if line.startswith('#'):
69 continue
70 if not in_paragraph:
71 # This is a test case description line.
72 if line in descriptions:
73 results.error(data_file_name, line_number,
74 'Duplicate description (also line {}): {}',
75 descriptions[line], line)
76 else:
77 if re.search(r'[\t;]', line):
78 results.error(data_file_name, line_number,
79 'Forbidden character in description')
80 if len(line) > 66:
81 results.warning(data_file_name, line_number,
82 'Test description will be truncated')
83 descriptions[line] = line_number
84 in_paragraph = True
85
86def check_ssl_opt_sh(results, file_name):
87 descriptions = {}
88 line_number = 0
89 with open(file_name) as file_contents:
90 for line in file_contents:
91 line_number += 1
92 # Assume that all run_test calls have the same simple form
93 # with the test description entirely on the same line as the
94 # function name.
Gilles Peskine168858f2019-09-20 17:54:45 +020095 m = re.match(r'\s*run_test\s+"((?:[^\\"]|\\.)*)"', line)
Gilles Peskineba94b582019-09-16 19:18:40 +020096 if not m:
97 continue
98 description = m.group(1)
99 if description in descriptions:
100 results.error(data_file_name, line_number,
101 'Duplicate description (also line {}): {}',
102 descriptions[line], line)
103 else:
104 if re.search(r'[\t;]', line):
105 results.error(data_file_name, line_number,
106 'Forbidden character in description')
107 if len(line) > 66:
108 results.warning(data_file_name, line_number,
109 'Test description will break visual alignment')
110 descriptions[line] = line_number
111
112def main():
113 test_directories = collect_test_directories()
114 results = Results()
115 for directory in test_directories:
116 for data_file_name in glob.glob(os.path.join(directory, 'suites',
117 '*.data')):
118 check_test_suite(results, data_file_name)
119 ssl_opt_sh = os.path.join(directory, 'ssl-opt.sh')
120 if os.path.exists(ssl_opt_sh):
121 check_ssl_opt_sh(results, ssl_opt_sh)
122 if results.warnings or results.errors:
123 sys.stderr.write('{}: {} errors, {} warnings\n'
124 .format(sys.argv[0], results.errors, results.warnings))
125 sys.exit(1 if results.errors else 0)
126
127if __name__ == '__main__':
128 main()