| Gilles Peskine | db2f575 | 2021-01-26 21:27:22 +0100 | [diff] [blame] | 1 | """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 |  | 
|  | 19 | import binascii | 
| Gilles Peskine | 505cc64 | 2021-01-27 18:30:40 +0100 | [diff] [blame] | 20 | import os | 
| Gilles Peskine | 8ffb585 | 2021-01-26 21:35:01 +0100 | [diff] [blame] | 21 | import sys | 
| Gilles Peskine | 4fbffcd | 2021-02-16 18:06:59 +0100 | [diff] [blame] | 22 | from typing import Iterable, List, Optional | 
| Gilles Peskine | db2f575 | 2021-01-26 21:27:22 +0100 | [diff] [blame] | 23 |  | 
| Gilles Peskine | 4fbffcd | 2021-02-16 18:06:59 +0100 | [diff] [blame] | 24 | from mbedtls_dev import typing_util | 
| Gilles Peskine | db2f575 | 2021-01-26 21:27:22 +0100 | [diff] [blame] | 25 |  | 
|  | 26 | def hex_string(data: bytes) -> str: | 
|  | 27 | return '"' + binascii.hexlify(data).decode('ascii') + '"' | 
|  | 28 |  | 
|  | 29 |  | 
|  | 30 | class MissingDescription(Exception): | 
|  | 31 | pass | 
|  | 32 |  | 
|  | 33 | class MissingFunction(Exception): | 
|  | 34 | pass | 
|  | 35 |  | 
|  | 36 | class 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 Peskine | 4fbffcd | 2021-02-16 18:06:59 +0100 | [diff] [blame] | 67 | def write(self, out: typing_util.Writable) -> None: | 
| Gilles Peskine | db2f575 | 2021-01-26 21:27:22 +0100 | [diff] [blame] | 68 | """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 Stekiel | 729c244 | 2021-11-09 14:40:12 +0100 | [diff] [blame] | 84 | out.write(self.function + ':' + ':'.join(self.arguments) + '\n') | 
| Gilles Peskine | 8ffb585 | 2021-01-26 21:35:01 +0100 | [diff] [blame] | 85 |  | 
|  | 86 | def 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 Peskine | 505cc64 | 2021-01-27 18:30:40 +0100 | [diff] [blame] | 94 | caller = os.path.basename(sys.argv[0]) | 
| Gilles Peskine | 8ffb585 | 2021-01-26 21:35:01 +0100 | [diff] [blame] | 95 | with open(filename, 'w') as out: | 
|  | 96 | out.write('# Automatically generated by {}. Do not edit!\n' | 
|  | 97 | .format(caller)) | 
|  | 98 | for tc in test_cases: | 
|  | 99 | tc.write(out) | 
|  | 100 | out.write('\n# End of automatically generated file.\n') |