• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
19import binascii
20import os
21import sys
22from typing import Iterable, List, Optional
23
24from mbedtls_dev import typing_util
25
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
67    def write(self, out: typing_util.Writable) -> None:
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')
84        out.write(self.function + ':' + ':'.join(self.arguments) + '\n')
85
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:
94        caller = os.path.basename(sys.argv[0])
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')
101