blob: 11117fcdd7393a3fa6a2dc8f5266737b828bbf91 [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]
Przemyslaw Stekiel1b0978b2021-10-15 15:21:51 +020045 self.result = '' #type: str
Gilles Peskinedb2f5752021-01-26 21:27:22 +010046
47 def add_comment(self, *lines: str) -> None:
48 self.comments += lines
49
50 def set_description(self, description: str) -> None:
51 self.description = description
52
53 def set_dependencies(self, dependencies: List[str]) -> None:
54 self.dependencies = dependencies
55
56 def set_function(self, function: str) -> None:
57 self.function = function
58
59 def set_arguments(self, arguments: List[str]) -> None:
60 self.arguments = arguments
61
Przemyslaw Stekiel1b0978b2021-10-15 15:21:51 +020062 def set_result(self, result: str) -> None:
63 self.result = result
64
Gilles Peskinedb2f5752021-01-26 21:27:22 +010065 def check_completeness(self) -> None:
66 if self.description is None:
67 raise MissingDescription
68 if self.function is None:
69 raise MissingFunction
70
Gilles Peskine4fbffcd2021-02-16 18:06:59 +010071 def write(self, out: typing_util.Writable) -> None:
Gilles Peskinedb2f5752021-01-26 21:27:22 +010072 """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')
Przemyslaw Stekiel1b0978b2021-10-15 15:21:51 +020088 out.write(self.function + ':' + ':'.join(self.arguments))
89 if self.result:
90 out.write(':' + self.result + '\n')
91 else:
92 out.write('\n')
Gilles Peskine8ffb5852021-01-26 21:35:01 +010093
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')