blob: d01e1432b63d11ed5352c7a86df7e351ebe8b1cb [file] [log] [blame]
Gilles Peskinedb2f5752021-01-26 21:27:22 +01001"""Library for generating Mbed TLS test data.
2"""
3
4# Copyright The Mbed TLS Contributors
5# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18
19import binascii
Gilles Peskine505cc642021-01-27 18:30:40 +010020import os
Gilles Peskine8ffb5852021-01-26 21:35:01 +010021import sys
Gilles Peskine4fbffcd2021-02-16 18:06:59 +010022from typing import Iterable, List, Optional
Gilles Peskinedb2f5752021-01-26 21:27:22 +010023
Gilles Peskine4fbffcd2021-02-16 18:06:59 +010024from mbedtls_dev import typing_util
Gilles Peskinedb2f5752021-01-26 21:27:22 +010025
26def hex_string(data: bytes) -> str:
27 return '"' + binascii.hexlify(data).decode('ascii') + '"'
28
29
30class MissingDescription(Exception):
31 pass
32
33class MissingFunction(Exception):
34 pass
35
36class TestCase:
37 """An Mbed TLS test case."""
38
39 def __init__(self, description: Optional[str] = None):
40 self.comments = [] #type: List[str]
41 self.description = description #type: Optional[str]
42 self.dependencies = [] #type: List[str]
43 self.function = None #type: Optional[str]
44 self.arguments = [] #type: List[str]
45
46 def add_comment(self, *lines: str) -> None:
47 self.comments += lines
48
49 def set_description(self, description: str) -> None:
50 self.description = description
51
52 def set_dependencies(self, dependencies: List[str]) -> None:
53 self.dependencies = dependencies
54
55 def set_function(self, function: str) -> None:
56 self.function = function
57
58 def set_arguments(self, arguments: List[str]) -> None:
59 self.arguments = arguments
60
61 def check_completeness(self) -> None:
62 if self.description is None:
63 raise MissingDescription
64 if self.function is None:
65 raise MissingFunction
66
Gilles Peskine4fbffcd2021-02-16 18:06:59 +010067 def write(self, out: typing_util.Writable) -> None:
Gilles Peskinedb2f5752021-01-26 21:27:22 +010068 """Write the .data file paragraph for this test case.
69
70 The output starts and ends with a single newline character. If the
71 surrounding code writes lines (consisting of non-newline characters
72 and a final newline), you will end up with a blank line before, but
73 not after the test case.
74 """
75 self.check_completeness()
76 assert self.description is not None # guide mypy
77 assert self.function is not None # guide mypy
78 out.write('\n')
79 for line in self.comments:
80 out.write('# ' + line + '\n')
81 out.write(self.description + '\n')
82 if self.dependencies:
83 out.write('depends_on:' + ':'.join(self.dependencies) + '\n')
84 out.write(self.function + ':' + ':'.join(self.arguments) + '\n')
Gilles Peskine8ffb5852021-01-26 21:35:01 +010085
86
87
88def write_data_file(filename: str,
89 test_cases: Iterable[TestCase],
90 caller: Optional[str] = None) -> None:
91 """Write the test cases to the specified file.
92
93 If the file already exists, it is overwritten.
94 """
95 if caller is None:
Gilles Peskine505cc642021-01-27 18:30:40 +010096 caller = os.path.basename(sys.argv[0])
Gilles Peskine8ffb5852021-01-26 21:35:01 +010097 with open(filename, 'w') as out:
98 out.write('# Automatically generated by {}. Do not edit!\n'
99 .format(caller))
100 for tc in test_cases:
101 tc.write(out)
102 out.write('\n# End of automatically generated file.\n')