blob: 8f087036789837be6ec77938234cddade890806c [file] [log] [blame]
Gilles Peskine04904252022-09-16 22:02:37 +02001"""Library for constructing an Mbed TLS test case.
Gilles Peskinedb2f5752021-01-26 21:27:22 +01002"""
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 Peskine15997bd2022-09-16 22:35:18 +020024from . 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')
Przemyslaw Stekiel729c2442021-11-09 14:40:12 +010084 out.write(self.function + ':' + ':'.join(self.arguments) + '\n')
Gilles Peskine8ffb5852021-01-26 21:35:01 +010085
86def write_data_file(filename: str,
87 test_cases: Iterable[TestCase],
88 caller: Optional[str] = None) -> None:
89 """Write the test cases to the specified file.
90
91 If the file already exists, it is overwritten.
92 """
93 if caller is None:
Gilles Peskine505cc642021-01-27 18:30:40 +010094 caller = os.path.basename(sys.argv[0])
Gilles Peskineb3ea98c2022-09-21 22:00:06 +020095 tempfile = filename + '.new'
96 with open(tempfile, 'w') as out:
Gilles Peskine8ffb5852021-01-26 21:35:01 +010097 out.write('# Automatically generated by {}. Do not edit!\n'
98 .format(caller))
99 for tc in test_cases:
100 tc.write(out)
101 out.write('\n# End of automatically generated file.\n')
Gilles Peskineb3ea98c2022-09-21 22:00:06 +0200102 os.replace(tempfile, filename)