Gilles Peskine | 0490425 | 2022-09-16 22:02:37 +0200 | [diff] [blame] | 1 | """Common code for test data generation. |
| 2 | |
| 3 | This module defines classes that are of general use to automatically |
| 4 | generate .data files for unit tests, as well as a main function. |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 5 | |
| 6 | These are used both by generate_psa_tests.py and generate_bignum_tests.py. |
| 7 | """ |
| 8 | |
| 9 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 10 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 11 | # |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 12 | |
| 13 | import argparse |
| 14 | import os |
| 15 | import posixpath |
| 16 | import re |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 17 | import inspect |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 18 | |
Werner Lewis | 699e126 | 2022-08-24 12:18:25 +0100 | [diff] [blame] | 19 | from abc import ABCMeta, abstractmethod |
Werner Lewis | 2b527a3 | 2022-08-24 12:42:00 +0100 | [diff] [blame] | 20 | from typing import Callable, Dict, Iterable, Iterator, List, Type, TypeVar |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 21 | |
Gilles Peskine | 15997bd | 2022-09-16 22:35:18 +0200 | [diff] [blame] | 22 | from . import build_tree |
| 23 | from . import test_case |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 24 | |
| 25 | T = TypeVar('T') #pylint: disable=invalid-name |
| 26 | |
| 27 | |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 28 | class BaseTest(metaclass=ABCMeta): |
| 29 | """Base class for test case generation. |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 30 | |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 31 | Attributes: |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 32 | count: Counter for test cases from this class. |
| 33 | case_description: Short description of the test case. This may be |
| 34 | automatically generated using the class, or manually set. |
Werner Lewis | 466f036 | 2022-08-31 17:01:38 +0100 | [diff] [blame] | 35 | dependencies: A list of dependencies required for the test case. |
Werner Lewis | 858cffd | 2022-09-14 13:02:40 +0100 | [diff] [blame] | 36 | show_test_count: Toggle for inclusion of `count` in the test description. |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 37 | test_function: Test function which the class generates cases for. |
| 38 | test_name: A common name or description of the test function. This can |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 39 | be `test_function`, a clearer equivalent, or a short summary of the |
| 40 | test function's purpose. |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 41 | """ |
| 42 | count = 0 |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 43 | case_description = "" |
Werner Lewis | aaf3b79 | 2022-08-31 17:16:44 +0100 | [diff] [blame] | 44 | dependencies = [] # type: List[str] |
Werner Lewis | 858cffd | 2022-09-14 13:02:40 +0100 | [diff] [blame] | 45 | show_test_count = True |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 46 | test_function = "" |
| 47 | test_name = "" |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 48 | |
Werner Lewis | cfd4768 | 2022-08-24 17:04:07 +0100 | [diff] [blame] | 49 | def __new__(cls, *args, **kwargs): |
Werner Lewis | a195ce7 | 2022-08-24 18:09:10 +0100 | [diff] [blame] | 50 | # pylint: disable=unused-argument |
Werner Lewis | cfd4768 | 2022-08-24 17:04:07 +0100 | [diff] [blame] | 51 | cls.count += 1 |
| 52 | return super().__new__(cls) |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 53 | |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 54 | @abstractmethod |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 55 | def arguments(self) -> List[str]: |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 56 | """Get the list of arguments for the test case. |
| 57 | |
| 58 | Override this method to provide the list of arguments required for |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 59 | the `test_function`. |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 60 | |
| 61 | Returns: |
| 62 | List of arguments required for the test function. |
| 63 | """ |
Werner Lewis | 6d654c6 | 2022-08-25 09:56:51 +0100 | [diff] [blame] | 64 | raise NotImplementedError |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 65 | |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 66 | def description(self) -> str: |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 67 | """Create a test case description. |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 68 | |
| 69 | Creates a description of the test case, including a name for the test |
Werner Lewis | 858cffd | 2022-09-14 13:02:40 +0100 | [diff] [blame] | 70 | function, an optional case count, and a description of the specific |
| 71 | test case. This should inform a reader what is being tested, and |
| 72 | provide context for the test case. |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 73 | |
| 74 | Returns: |
| 75 | Description for the test case. |
| 76 | """ |
Werner Lewis | 858cffd | 2022-09-14 13:02:40 +0100 | [diff] [blame] | 77 | if self.show_test_count: |
| 78 | return "{} #{} {}".format( |
| 79 | self.test_name, self.count, self.case_description |
| 80 | ).strip() |
| 81 | else: |
| 82 | return "{} {}".format(self.test_name, self.case_description).strip() |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 83 | |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 84 | |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 85 | def create_test_case(self) -> test_case.TestCase: |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 86 | """Generate TestCase from the instance.""" |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 87 | tc = test_case.TestCase() |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 88 | tc.set_description(self.description()) |
| 89 | tc.set_function(self.test_function) |
| 90 | tc.set_arguments(self.arguments()) |
Werner Lewis | 466f036 | 2022-08-31 17:01:38 +0100 | [diff] [blame] | 91 | tc.set_dependencies(self.dependencies) |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 92 | |
| 93 | return tc |
| 94 | |
| 95 | @classmethod |
Werner Lewis | 2b527a3 | 2022-08-24 12:42:00 +0100 | [diff] [blame] | 96 | @abstractmethod |
| 97 | def generate_function_tests(cls) -> Iterator[test_case.TestCase]: |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 98 | """Generate test cases for the class test function. |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 99 | |
Werner Lewis | 2b527a3 | 2022-08-24 12:42:00 +0100 | [diff] [blame] | 100 | This will be called in classes where `test_function` is set. |
| 101 | Implementations should yield TestCase objects, by creating instances |
| 102 | of the class with appropriate input data, and then calling |
| 103 | `create_test_case()` on each. |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 104 | """ |
Werner Lewis | 6d654c6 | 2022-08-25 09:56:51 +0100 | [diff] [blame] | 105 | raise NotImplementedError |
Werner Lewis | 2b527a3 | 2022-08-24 12:42:00 +0100 | [diff] [blame] | 106 | |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 107 | |
| 108 | class BaseTarget: |
Janos Follath | 351e688 | 2022-11-09 16:04:41 +0000 | [diff] [blame] | 109 | #pylint: disable=too-few-public-methods |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 110 | """Base target for test case generation. |
| 111 | |
| 112 | Child classes of this class represent an output file, and can be referred |
| 113 | to as file targets. These indicate where test cases will be written to for |
| 114 | all subclasses of the file target, which is set by `target_basename`. |
| 115 | |
| 116 | Attributes: |
| 117 | target_basename: Basename of file to write generated tests to. This |
| 118 | should be specified in a child class of BaseTarget. |
| 119 | """ |
| 120 | target_basename = "" |
| 121 | |
Werner Lewis | 2b527a3 | 2022-08-24 12:42:00 +0100 | [diff] [blame] | 122 | @classmethod |
| 123 | def generate_tests(cls) -> Iterator[test_case.TestCase]: |
| 124 | """Generate test cases for the class and its subclasses. |
| 125 | |
| 126 | In classes with `test_function` set, `generate_function_tests()` is |
Werner Lewis | 81f2444 | 2022-08-25 16:27:05 +0100 | [diff] [blame] | 127 | called to generate test cases first. |
Werner Lewis | 2b527a3 | 2022-08-24 12:42:00 +0100 | [diff] [blame] | 128 | |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 129 | In all classes, this method will iterate over its subclasses, and |
| 130 | yield from `generate_tests()` in each. Calling this method on a class X |
| 131 | will yield test cases from all classes derived from X. |
Werner Lewis | 2b527a3 | 2022-08-24 12:42:00 +0100 | [diff] [blame] | 132 | """ |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 133 | if issubclass(cls, BaseTest) and not inspect.isabstract(cls): |
Janos Follath | 351e688 | 2022-11-09 16:04:41 +0000 | [diff] [blame] | 134 | #pylint: disable=no-member |
Werner Lewis | 2b527a3 | 2022-08-24 12:42:00 +0100 | [diff] [blame] | 135 | yield from cls.generate_function_tests() |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 136 | for subclass in sorted(cls.__subclasses__(), key=lambda c: c.__name__): |
| 137 | yield from subclass.generate_tests() |
| 138 | |
| 139 | |
| 140 | class TestGenerator: |
Werner Lewis | 34d6d3e | 2022-09-14 12:59:32 +0100 | [diff] [blame] | 141 | """Generate test cases and write to data files.""" |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 142 | def __init__(self, options) -> None: |
Gilles Peskine | 7b3fa65 | 2022-09-16 22:22:53 +0200 | [diff] [blame] | 143 | self.test_suite_directory = options.directory |
Werner Lewis | 34d6d3e | 2022-09-14 12:59:32 +0100 | [diff] [blame] | 144 | # Update `targets` with an entry for each child class of BaseTarget. |
| 145 | # Each entry represents a file generated by the BaseTarget framework, |
| 146 | # and enables generating the .data files using the CLI. |
Werner Lewis | a4668a6 | 2022-09-02 11:56:34 +0100 | [diff] [blame] | 147 | self.targets.update({ |
| 148 | subclass.target_basename: subclass.generate_tests |
| 149 | for subclass in BaseTarget.__subclasses__() |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 150 | if subclass.target_basename |
Werner Lewis | a4668a6 | 2022-09-02 11:56:34 +0100 | [diff] [blame] | 151 | }) |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 152 | |
| 153 | def filename_for(self, basename: str) -> str: |
| 154 | """The location of the data file with the specified base name.""" |
| 155 | return posixpath.join(self.test_suite_directory, basename + '.data') |
| 156 | |
| 157 | def write_test_data_file(self, basename: str, |
| 158 | test_cases: Iterable[test_case.TestCase]) -> None: |
| 159 | """Write the test cases to a .data file. |
| 160 | |
| 161 | The output file is ``basename + '.data'`` in the test suite directory. |
| 162 | """ |
| 163 | filename = self.filename_for(basename) |
| 164 | test_case.write_data_file(filename, test_cases) |
| 165 | |
| 166 | # Note that targets whose names contain 'test_format' have their content |
| 167 | # validated by `abi_check.py`. |
Werner Lewis | a4668a6 | 2022-09-02 11:56:34 +0100 | [diff] [blame] | 168 | targets = {} # type: Dict[str, Callable[..., Iterable[test_case.TestCase]]] |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 169 | |
| 170 | def generate_target(self, name: str, *target_args) -> None: |
| 171 | """Generate cases and write to data file for a target. |
| 172 | |
| 173 | For target callables which require arguments, override this function |
| 174 | and pass these arguments using super() (see PSATestGenerator). |
| 175 | """ |
Werner Lewis | a4668a6 | 2022-09-02 11:56:34 +0100 | [diff] [blame] | 176 | test_cases = self.targets[name](*target_args) |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 177 | self.write_test_data_file(name, test_cases) |
| 178 | |
Werner Lewis | c2fb540 | 2022-09-16 17:03:54 +0100 | [diff] [blame] | 179 | def main(args, description: str, generator_class: Type[TestGenerator] = TestGenerator): |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 180 | """Command line entry point.""" |
Werner Lewis | c2fb540 | 2022-09-16 17:03:54 +0100 | [diff] [blame] | 181 | parser = argparse.ArgumentParser(description=description) |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 182 | parser.add_argument('--list', action='store_true', |
| 183 | help='List available targets and exit') |
| 184 | parser.add_argument('--list-for-cmake', action='store_true', |
| 185 | help='Print \';\'-separated list of available targets and exit') |
Gilles Peskine | 7b3fa65 | 2022-09-16 22:22:53 +0200 | [diff] [blame] | 186 | # If specified explicitly, this option may be a path relative to the |
| 187 | # current directory when the script is invoked. The default value |
| 188 | # is relative to the mbedtls root, which we don't know yet. So we |
| 189 | # can't set a string as the default value here. |
Werner Lewis | 00d0242 | 2022-09-14 13:39:20 +0100 | [diff] [blame] | 190 | parser.add_argument('--directory', metavar='DIR', |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 191 | help='Output directory (default: tests/suites)') |
| 192 | parser.add_argument('targets', nargs='*', metavar='TARGET', |
| 193 | help='Target file to generate (default: all; "-": none)') |
| 194 | options = parser.parse_args(args) |
Gilles Peskine | 7b3fa65 | 2022-09-16 22:22:53 +0200 | [diff] [blame] | 195 | |
| 196 | # Change to the mbedtls root, to keep things simple. But first, adjust |
| 197 | # command line options that might be relative paths. |
| 198 | if options.directory is None: |
| 199 | options.directory = 'tests/suites' |
| 200 | else: |
| 201 | options.directory = os.path.abspath(options.directory) |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 202 | build_tree.chdir_to_root() |
Gilles Peskine | 7b3fa65 | 2022-09-16 22:22:53 +0200 | [diff] [blame] | 203 | |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 204 | generator = generator_class(options) |
| 205 | if options.list: |
Werner Lewis | a4668a6 | 2022-09-02 11:56:34 +0100 | [diff] [blame] | 206 | for name in sorted(generator.targets): |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 207 | print(generator.filename_for(name)) |
| 208 | return |
| 209 | # List in a cmake list format (i.e. ';'-separated) |
| 210 | if options.list_for_cmake: |
| 211 | print(';'.join(generator.filename_for(name) |
Werner Lewis | a4668a6 | 2022-09-02 11:56:34 +0100 | [diff] [blame] | 212 | for name in sorted(generator.targets)), end='') |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 213 | return |
Werner Lewis | a4668a6 | 2022-09-02 11:56:34 +0100 | [diff] [blame] | 214 | if options.targets: |
| 215 | # Allow "-" as a special case so you can run |
| 216 | # ``generate_xxx_tests.py - $targets`` and it works uniformly whether |
| 217 | # ``$targets`` is empty or not. |
| 218 | options.targets = [os.path.basename(re.sub(r'\.data\Z', r'', target)) |
Werner Lewis | 5601308 | 2022-09-02 12:57:37 +0100 | [diff] [blame] | 219 | for target in options.targets |
| 220 | if target != '-'] |
Werner Lewis | a4668a6 | 2022-09-02 11:56:34 +0100 | [diff] [blame] | 221 | else: |
| 222 | options.targets = sorted(generator.targets) |
Werner Lewis | fbb75e3 | 2022-08-24 11:30:03 +0100 | [diff] [blame] | 223 | for target in options.targets: |
| 224 | generator.generate_target(target) |