Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | """Generate test data for bignum functions. |
| 3 | |
| 4 | With no arguments, generate all test data. With non-option arguments, |
| 5 | generate only the specified files. |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 6 | |
| 7 | Class structure: |
| 8 | |
Gilles Peskine | 64f2efd | 2022-09-16 21:41:47 +0200 | [diff] [blame] | 9 | Child classes of test_data_generation.BaseTarget (file targets) represent an output |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 10 | file. These indicate where test cases will be written to, for all subclasses of |
Werner Lewis | 52ae326 | 2022-09-14 16:26:54 +0100 | [diff] [blame] | 11 | this target. Multiple file targets should not reuse a `target_basename`. |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 12 | |
Werner Lewis | 52ae326 | 2022-09-14 16:26:54 +0100 | [diff] [blame] | 13 | Each subclass derived from a file target can either be: |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 14 | - A concrete class, representing a test function, which generates test cases. |
| 15 | - An abstract class containing shared methods and attributes, not associated |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 16 | with a test function. An example is BignumOperation, which provides |
| 17 | common features used for bignum binary operations. |
| 18 | |
| 19 | Both concrete and abstract subclasses can be derived from, to implement |
| 20 | additional test cases (see BignumCmp and BignumCmpAbs for examples of deriving |
| 21 | from abstract and concrete classes). |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 22 | |
| 23 | |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 24 | Adding test case generation for a function: |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 25 | |
| 26 | A subclass representing the test function should be added, deriving from a |
Werner Lewis | 52ae326 | 2022-09-14 16:26:54 +0100 | [diff] [blame] | 27 | file target such as BignumTarget. This test class must set/implement the |
Werner Lewis | 81f2444 | 2022-08-25 16:27:05 +0100 | [diff] [blame] | 28 | following: |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 29 | - test_function: the function name from the associated .function file. |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 30 | - test_name: a descriptive name or brief summary to refer to the test |
| 31 | function. |
| 32 | - arguments(): a method to generate the list of arguments required for the |
| 33 | test_function. |
Gilles Peskine | 22514eb | 2022-09-21 23:13:04 +0200 | [diff] [blame] | 34 | - generate_function_tests(): a method to generate TestCases for the function. |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 35 | This should create instances of the class with required input data, and |
| 36 | call `.create_test_case()` to yield the TestCase. |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 37 | |
| 38 | Additional details and other attributes/methods are given in the documentation |
Gilles Peskine | 64f2efd | 2022-09-16 21:41:47 +0200 | [diff] [blame] | 39 | of BaseTarget in test_data_generation.py. |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 40 | """ |
| 41 | |
| 42 | # Copyright The Mbed TLS Contributors |
| 43 | # SPDX-License-Identifier: Apache-2.0 |
| 44 | # |
| 45 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 46 | # not use this file except in compliance with the License. |
| 47 | # You may obtain a copy of the License at |
| 48 | # |
| 49 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 50 | # |
| 51 | # Unless required by applicable law or agreed to in writing, software |
| 52 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 53 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 54 | # See the License for the specific language governing permissions and |
| 55 | # limitations under the License. |
| 56 | |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 57 | import sys |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 58 | |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 59 | from abc import ABCMeta |
Werner Lewis | 1b20e7e | 2022-10-12 14:53:17 +0100 | [diff] [blame] | 60 | from typing import Iterator, List |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 61 | |
| 62 | import scripts_path # pylint: disable=unused-import |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 63 | from mbedtls_dev import test_case |
Gilles Peskine | 64f2efd | 2022-09-16 21:41:47 +0200 | [diff] [blame] | 64 | from mbedtls_dev import test_data_generation |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 65 | from mbedtls_dev import bignum_common |
| 66 | # Import modules containing additional test classes |
| 67 | # Test function classes in these modules will be registered by |
| 68 | # the framework |
Minos Galanakis | 5566eff | 2022-11-07 16:02:21 +0000 | [diff] [blame] | 69 | from mbedtls_dev import bignum_core, bignum_mod_raw # pylint: disable=unused-import |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 70 | |
Gilles Peskine | 64f2efd | 2022-09-16 21:41:47 +0200 | [diff] [blame] | 71 | class BignumTarget(test_data_generation.BaseTarget, metaclass=ABCMeta): |
Werner Lewis | a16b617 | 2022-08-25 11:17:35 +0100 | [diff] [blame] | 72 | #pylint: disable=abstract-method |
Gilles Peskine | ce22066 | 2022-10-21 18:54:43 +0200 | [diff] [blame] | 73 | """Target for bignum (legacy) test case generation.""" |
| 74 | target_basename = 'test_suite_bignum.generated' |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 75 | |
| 76 | |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 77 | class BignumOperation(bignum_common.OperationCommon, BignumTarget, metaclass=ABCMeta): |
| 78 | #pylint: disable=abstract-method |
| 79 | """Common features for bignum operations in legacy tests.""" |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 80 | input_values = [ |
Gilles Peskine | 35af021 | 2022-11-15 20:43:33 +0100 | [diff] [blame] | 81 | "", "0", "-", "-0", |
| 82 | "7b", "-7b", |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 83 | "0000000000000000123", "-0000000000000000123", |
| 84 | "1230000000000000000", "-1230000000000000000" |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 85 | ] |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 86 | |
Gilles Peskine | 35af021 | 2022-11-15 20:43:33 +0100 | [diff] [blame] | 87 | def description_suffix(self) -> str: |
Gilles Peskine | b9b9026 | 2022-11-10 09:15:21 +0100 | [diff] [blame^] | 88 | #pylint: disable=no-self-use # derived classes need self |
Gilles Peskine | 35af021 | 2022-11-15 20:43:33 +0100 | [diff] [blame] | 89 | """Text to add at the end of the test case description.""" |
| 90 | return "" |
| 91 | |
Werner Lewis | 6300b4f | 2022-08-24 17:46:22 +0100 | [diff] [blame] | 92 | def description(self) -> str: |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 93 | """Generate a description for the test case. |
| 94 | |
| 95 | If not set, case_description uses the form A `symbol` B, where symbol |
| 96 | is used to represent the operation. Descriptions of each value are |
| 97 | generated to provide some context to the test case. |
| 98 | """ |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 99 | if not self.case_description: |
| 100 | self.case_description = "{} {} {}".format( |
Werner Lewis | 3dc4519 | 2022-09-12 17:35:27 +0100 | [diff] [blame] | 101 | self.value_description(self.arg_a), |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 102 | self.symbol, |
Werner Lewis | 3dc4519 | 2022-09-12 17:35:27 +0100 | [diff] [blame] | 103 | self.value_description(self.arg_b) |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 104 | ) |
Gilles Peskine | 35af021 | 2022-11-15 20:43:33 +0100 | [diff] [blame] | 105 | description_suffix = self.description_suffix() |
| 106 | if description_suffix: |
| 107 | self.case_description += " " + description_suffix |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 108 | return super().description() |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 109 | |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 110 | @staticmethod |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 111 | def value_description(val) -> str: |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 112 | """Generate a description of the argument val. |
| 113 | |
Werner Lewis | 81f2444 | 2022-08-25 16:27:05 +0100 | [diff] [blame] | 114 | This produces a simple description of the value, which is used in test |
| 115 | case naming to add context. |
Werner Lewis | 169034a | 2022-08-23 16:07:37 +0100 | [diff] [blame] | 116 | """ |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 117 | if val == "": |
| 118 | return "0 (null)" |
Gilles Peskine | 35af021 | 2022-11-15 20:43:33 +0100 | [diff] [blame] | 119 | if val == "-": |
| 120 | return "negative 0 (null)" |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 121 | if val == "0": |
| 122 | return "0 (1 limb)" |
| 123 | |
| 124 | if val[0] == "-": |
| 125 | tmp = "negative" |
| 126 | val = val[1:] |
| 127 | else: |
| 128 | tmp = "positive" |
| 129 | if val[0] == "0": |
| 130 | tmp += " with leading zero limb" |
| 131 | elif len(val) > 10: |
| 132 | tmp = "large " + tmp |
| 133 | return tmp |
| 134 | |
| 135 | @classmethod |
Werner Lewis | 2b527a3 | 2022-08-24 12:42:00 +0100 | [diff] [blame] | 136 | def generate_function_tests(cls) -> Iterator[test_case.TestCase]: |
Werner Lewis | 3dc4519 | 2022-09-12 17:35:27 +0100 | [diff] [blame] | 137 | for a_value, b_value in cls.get_value_pairs(): |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 138 | yield cls(a_value, b_value).create_test_case() |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 139 | |
| 140 | |
| 141 | class BignumCmp(BignumOperation): |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 142 | """Test cases for bignum value comparison.""" |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 143 | count = 0 |
Tom Cosgrove | 119eae2 | 2022-09-21 12:19:18 +0100 | [diff] [blame] | 144 | test_function = "mpi_cmp_mpi" |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 145 | test_name = "MPI compare" |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 146 | input_cases = [ |
| 147 | ("-2", "-3"), |
| 148 | ("-2", "-2"), |
| 149 | ("2b4", "2b5"), |
| 150 | ("2b5", "2b6") |
| 151 | ] |
| 152 | |
Werner Lewis | 3dc4519 | 2022-09-12 17:35:27 +0100 | [diff] [blame] | 153 | def __init__(self, val_a, val_b) -> None: |
| 154 | super().__init__(val_a, val_b) |
| 155 | self._result = int(self.int_a > self.int_b) - int(self.int_a < self.int_b) |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 156 | self.symbol = ["<", "==", ">"][self._result + 1] |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 157 | |
Werner Lewis | 1b20e7e | 2022-10-12 14:53:17 +0100 | [diff] [blame] | 158 | def result(self) -> List[str]: |
| 159 | return [str(self._result)] |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 160 | |
| 161 | |
Werner Lewis | 69a92ce | 2022-07-18 15:49:43 +0100 | [diff] [blame] | 162 | class BignumCmpAbs(BignumCmp): |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 163 | """Test cases for absolute bignum value comparison.""" |
Werner Lewis | 69a92ce | 2022-07-18 15:49:43 +0100 | [diff] [blame] | 164 | count = 0 |
Tom Cosgrove | 119eae2 | 2022-09-21 12:19:18 +0100 | [diff] [blame] | 165 | test_function = "mpi_cmp_abs" |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 166 | test_name = "MPI compare (abs)" |
Werner Lewis | 69a92ce | 2022-07-18 15:49:43 +0100 | [diff] [blame] | 167 | |
Werner Lewis | 3dc4519 | 2022-09-12 17:35:27 +0100 | [diff] [blame] | 168 | def __init__(self, val_a, val_b) -> None: |
| 169 | super().__init__(val_a.strip("-"), val_b.strip("-")) |
Werner Lewis | 69a92ce | 2022-07-18 15:49:43 +0100 | [diff] [blame] | 170 | |
| 171 | |
Werner Lewis | 86caf85 | 2022-07-18 17:22:58 +0100 | [diff] [blame] | 172 | class BignumAdd(BignumOperation): |
Werner Lewis | 6ef5436 | 2022-08-25 12:29:46 +0100 | [diff] [blame] | 173 | """Test cases for bignum value addition.""" |
Werner Lewis | 86caf85 | 2022-07-18 17:22:58 +0100 | [diff] [blame] | 174 | count = 0 |
Werner Lewis | 1fade8a | 2022-09-12 17:34:15 +0100 | [diff] [blame] | 175 | symbol = "+" |
Tom Cosgrove | 119eae2 | 2022-09-21 12:19:18 +0100 | [diff] [blame] | 176 | test_function = "mpi_add_mpi" |
Werner Lewis | 55e638c | 2022-08-23 14:21:53 +0100 | [diff] [blame] | 177 | test_name = "MPI add" |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 178 | input_cases = bignum_common.combination_pairs( |
Werner Lewis | ac446c8 | 2022-09-14 15:12:46 +0100 | [diff] [blame] | 179 | [ |
| 180 | "1c67967269c6", "9cde3", |
| 181 | "-1c67967269c6", "-9cde3", |
| 182 | ] |
Werner Lewis | 9990b30 | 2022-08-24 18:03:30 +0100 | [diff] [blame] | 183 | ) |
Werner Lewis | 86caf85 | 2022-07-18 17:22:58 +0100 | [diff] [blame] | 184 | |
Gilles Peskine | 35af021 | 2022-11-15 20:43:33 +0100 | [diff] [blame] | 185 | def __init__(self, val_a: str, val_b: str) -> None: |
| 186 | super().__init__(val_a, val_b) |
| 187 | self._result = self.int_a + self.int_b |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 35af021 | 2022-11-15 20:43:33 +0100 | [diff] [blame] | 189 | def description_suffix(self) -> str: |
| 190 | if (self.int_a >= 0 and self.int_b >= 0): |
| 191 | return "" # obviously positive result or 0 |
| 192 | if (self.int_a <= 0 and self.int_b <= 0): |
| 193 | return "" # obviously negative result or 0 |
| 194 | # The sign of the result is not obvious, so indicate it |
| 195 | return ", result{}0".format('>' if self._result > 0 else |
| 196 | '<' if self._result < 0 else '=') |
| 197 | |
| 198 | def result(self) -> List[str]: |
| 199 | return [bignum_common.quote_str("{:x}".format(self._result))] |
Werner Lewis | 86caf85 | 2022-07-18 17:22:58 +0100 | [diff] [blame] | 200 | |
Werner Lewis | 8b2df74 | 2022-07-08 13:54:57 +0100 | [diff] [blame] | 201 | if __name__ == '__main__': |
Werner Lewis | c2fb540 | 2022-09-16 17:03:54 +0100 | [diff] [blame] | 202 | # Use the section of the docstring relevant to the CLI as description |
Gilles Peskine | 64f2efd | 2022-09-16 21:41:47 +0200 | [diff] [blame] | 203 | test_data_generation.main(sys.argv[1:], "\n".join(__doc__.splitlines()[:4])) |