blob: a6ed8fc90dedd5719a9377c380410f3a475cae11 [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 Peskine8ffb5852021-01-26 21:35:01 +010020import sys
21from typing import Any, Iterable, List, Optional
Gilles Peskinedb2f5752021-01-26 21:27:22 +010022import typing_extensions #pylint: disable=import-error
23
24class Writable(typing_extensions.Protocol):
25 """Abstract class for typing hints."""
26 # pylint: disable=no-self-use,too-few-public-methods,unused-argument
27 def write(self, text: str) -> Any:
28 ...
29
30
31def hex_string(data: bytes) -> str:
32 return '"' + binascii.hexlify(data).decode('ascii') + '"'
33
34
35class MissingDescription(Exception):
36 pass
37
38class MissingFunction(Exception):
39 pass
40
41class TestCase:
42 """An Mbed TLS test case."""
43
44 def __init__(self, description: Optional[str] = None):
45 self.comments = [] #type: List[str]
46 self.description = description #type: Optional[str]
47 self.dependencies = [] #type: List[str]
48 self.function = None #type: Optional[str]
49 self.arguments = [] #type: List[str]
50
51 def add_comment(self, *lines: str) -> None:
52 self.comments += lines
53
54 def set_description(self, description: str) -> None:
55 self.description = description
56
57 def set_dependencies(self, dependencies: List[str]) -> None:
58 self.dependencies = dependencies
59
60 def set_function(self, function: str) -> None:
61 self.function = function
62
63 def set_arguments(self, arguments: List[str]) -> None:
64 self.arguments = arguments
65
66 def check_completeness(self) -> None:
67 if self.description is None:
68 raise MissingDescription
69 if self.function is None:
70 raise MissingFunction
71
72 def write(self, out: Writable) -> None:
73 """Write the .data file paragraph for this test case.
74
75 The output starts and ends with a single newline character. If the
76 surrounding code writes lines (consisting of non-newline characters
77 and a final newline), you will end up with a blank line before, but
78 not after the test case.
79 """
80 self.check_completeness()
81 assert self.description is not None # guide mypy
82 assert self.function is not None # guide mypy
83 out.write('\n')
84 for line in self.comments:
85 out.write('# ' + line + '\n')
86 out.write(self.description + '\n')
87 if self.dependencies:
88 out.write('depends_on:' + ':'.join(self.dependencies) + '\n')
89 out.write(self.function + ':' + ':'.join(self.arguments) + '\n')
Gilles Peskine8ffb5852021-01-26 21:35:01 +010090
91
92
93def write_data_file(filename: str,
94 test_cases: Iterable[TestCase],
95 caller: Optional[str] = None) -> None:
96 """Write the test cases to the specified file.
97
98 If the file already exists, it is overwritten.
99 """
100 if caller is None:
101 caller = sys.argv[0]
102 with open(filename, 'w') as out:
103 out.write('# Automatically generated by {}. Do not edit!\n'
104 .format(caller))
105 for tc in test_cases:
106 tc.write(out)
107 out.write('\n# End of automatically generated file.\n')