blob: 8ef18510c5d24425c6e03213e004fb190cfe18f9 [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
20from typing import Any, List, Optional
21import typing_extensions #pylint: disable=import-error
22
23class Writable(typing_extensions.Protocol):
24 """Abstract class for typing hints."""
25 # pylint: disable=no-self-use,too-few-public-methods,unused-argument
26 def write(self, text: str) -> Any:
27 ...
28
29
30def hex_string(data: bytes) -> str:
31 return '"' + binascii.hexlify(data).decode('ascii') + '"'
32
33
34class MissingDescription(Exception):
35 pass
36
37class MissingFunction(Exception):
38 pass
39
40class TestCase:
41 """An Mbed TLS test case."""
42
43 def __init__(self, description: Optional[str] = None):
44 self.comments = [] #type: List[str]
45 self.description = description #type: Optional[str]
46 self.dependencies = [] #type: List[str]
47 self.function = None #type: Optional[str]
48 self.arguments = [] #type: List[str]
49
50 def add_comment(self, *lines: str) -> None:
51 self.comments += lines
52
53 def set_description(self, description: str) -> None:
54 self.description = description
55
56 def set_dependencies(self, dependencies: List[str]) -> None:
57 self.dependencies = dependencies
58
59 def set_function(self, function: str) -> None:
60 self.function = function
61
62 def set_arguments(self, arguments: List[str]) -> None:
63 self.arguments = arguments
64
65 def check_completeness(self) -> None:
66 if self.description is None:
67 raise MissingDescription
68 if self.function is None:
69 raise MissingFunction
70
71 def write(self, out: Writable) -> None:
72 """Write the .data file paragraph for this test case.
73
74 The output starts and ends with a single newline character. If the
75 surrounding code writes lines (consisting of non-newline characters
76 and a final newline), you will end up with a blank line before, but
77 not after the test case.
78 """
79 self.check_completeness()
80 assert self.description is not None # guide mypy
81 assert self.function is not None # guide mypy
82 out.write('\n')
83 for line in self.comments:
84 out.write('# ' + line + '\n')
85 out.write(self.description + '\n')
86 if self.dependencies:
87 out.write('depends_on:' + ':'.join(self.dependencies) + '\n')
88 out.write(self.function + ':' + ':'.join(self.arguments) + '\n')