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