blob: 8dbb6ed7834cb33c7f127f252bfb4b7c4e5e4c38 [file] [log] [blame]
Tom Rini0344c602024-10-08 13:56:50 -06001#!/usr/bin/env python3
2"""Generate test data for bignum functions.
3
4With no arguments, generate all test data. With non-option arguments,
5generate only the specified files.
6
7Class structure:
8
9Child classes of test_data_generation.BaseTarget (file targets) represent an output
10file. These indicate where test cases will be written to, for all subclasses of
11this target. Multiple file targets should not reuse a `target_basename`.
12
13Each subclass derived from a file target can either be:
14 - A concrete class, representing a test function, which generates test cases.
15 - An abstract class containing shared methods and attributes, not associated
16 with a test function. An example is BignumOperation, which provides
17 common features used for bignum binary operations.
18
19Both concrete and abstract subclasses can be derived from, to implement
20additional test cases (see BignumCmp and BignumCmpAbs for examples of deriving
21from abstract and concrete classes).
22
23
24Adding test case generation for a function:
25
26A subclass representing the test function should be added, deriving from a
27file target such as BignumTarget. This test class must set/implement the
28following:
29 - test_function: the function name from the associated .function file.
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.
34 - generate_function_tests(): a method to generate TestCases for the function.
35 This should create instances of the class with required input data, and
36 call `.create_test_case()` to yield the TestCase.
37
38Additional details and other attributes/methods are given in the documentation
39of BaseTarget in test_data_generation.py.
40"""
41
42# Copyright The Mbed TLS Contributors
43# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
44
45import sys
46
47from abc import ABCMeta
48from typing import List
49
50import scripts_path # pylint: disable=unused-import
51from mbedtls_dev import test_data_generation
52from mbedtls_dev import bignum_common
53# Import modules containing additional test classes
54# Test function classes in these modules will be registered by
55# the framework
56from mbedtls_dev import bignum_core, bignum_mod_raw, bignum_mod # pylint: disable=unused-import
57
58class BignumTarget(test_data_generation.BaseTarget):
59 #pylint: disable=too-few-public-methods
60 """Target for bignum (legacy) test case generation."""
61 target_basename = 'test_suite_bignum.generated'
62
63
64class BignumOperation(bignum_common.OperationCommon, BignumTarget,
65 metaclass=ABCMeta):
66 #pylint: disable=abstract-method
67 """Common features for bignum operations in legacy tests."""
68 unique_combinations_only = True
69 input_values = [
70 "", "0", "-", "-0",
71 "7b", "-7b",
72 "0000000000000000123", "-0000000000000000123",
73 "1230000000000000000", "-1230000000000000000"
74 ]
75
76 def description_suffix(self) -> str:
77 #pylint: disable=no-self-use # derived classes need self
78 """Text to add at the end of the test case description."""
79 return ""
80
81 def description(self) -> str:
82 """Generate a description for the test case.
83
84 If not set, case_description uses the form A `symbol` B, where symbol
85 is used to represent the operation. Descriptions of each value are
86 generated to provide some context to the test case.
87 """
88 if not self.case_description:
89 self.case_description = "{} {} {}".format(
90 self.value_description(self.arg_a),
91 self.symbol,
92 self.value_description(self.arg_b)
93 )
94 description_suffix = self.description_suffix()
95 if description_suffix:
96 self.case_description += " " + description_suffix
97 return super().description()
98
99 @staticmethod
100 def value_description(val) -> str:
101 """Generate a description of the argument val.
102
103 This produces a simple description of the value, which is used in test
104 case naming to add context.
105 """
106 if val == "":
107 return "0 (null)"
108 if val == "-":
109 return "negative 0 (null)"
110 if val == "0":
111 return "0 (1 limb)"
112
113 if val[0] == "-":
114 tmp = "negative"
115 val = val[1:]
116 else:
117 tmp = "positive"
118 if val[0] == "0":
119 tmp += " with leading zero limb"
120 elif len(val) > 10:
121 tmp = "large " + tmp
122 return tmp
123
124
125class BignumCmp(BignumOperation):
126 """Test cases for bignum value comparison."""
127 count = 0
128 test_function = "mpi_cmp_mpi"
129 test_name = "MPI compare"
130 input_cases = [
131 ("-2", "-3"),
132 ("-2", "-2"),
133 ("2b4", "2b5"),
134 ("2b5", "2b6")
135 ]
136
137 def __init__(self, val_a, val_b) -> None:
138 super().__init__(val_a, val_b)
139 self._result = int(self.int_a > self.int_b) - int(self.int_a < self.int_b)
140 self.symbol = ["<", "==", ">"][self._result + 1]
141
142 def result(self) -> List[str]:
143 return [str(self._result)]
144
145
146class BignumCmpAbs(BignumCmp):
147 """Test cases for absolute bignum value comparison."""
148 count = 0
149 test_function = "mpi_cmp_abs"
150 test_name = "MPI compare (abs)"
151
152 def __init__(self, val_a, val_b) -> None:
153 super().__init__(val_a.strip("-"), val_b.strip("-"))
154
155
156class BignumAdd(BignumOperation):
157 """Test cases for bignum value addition."""
158 count = 0
159 symbol = "+"
160 test_function = "mpi_add_mpi"
161 test_name = "MPI add"
162 input_cases = bignum_common.combination_pairs(
163 [
164 "1c67967269c6", "9cde3",
165 "-1c67967269c6", "-9cde3",
166 ]
167 )
168
169 def __init__(self, val_a: str, val_b: str) -> None:
170 super().__init__(val_a, val_b)
171 self._result = self.int_a + self.int_b
172
173 def description_suffix(self) -> str:
174 if (self.int_a >= 0 and self.int_b >= 0):
175 return "" # obviously positive result or 0
176 if (self.int_a <= 0 and self.int_b <= 0):
177 return "" # obviously negative result or 0
178 # The sign of the result is not obvious, so indicate it
179 return ", result{}0".format('>' if self._result > 0 else
180 '<' if self._result < 0 else '=')
181
182 def result(self) -> List[str]:
183 return [bignum_common.quote_str("{:x}".format(self._result))]
184
185if __name__ == '__main__':
186 # Use the section of the docstring relevant to the CLI as description
187 test_data_generation.main(sys.argv[1:], "\n".join(__doc__.splitlines()[:4]))