1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-28 00:21:48 +03:00

Add module for bignum_core test generation

Separate file is added for classes used to generate cases for tests
in bignum_core.function. Common elements of the BignumOperation class
are added to classes in a new common file, for use across files.

Signed-off-by: Werner Lewis <werner.lewis@arm.com>
This commit is contained in:
Werner Lewis
2022-09-30 16:28:43 +01:00
parent 0c6ea12145
commit 99e8178fa7
6 changed files with 203 additions and 71 deletions

View File

@ -54,34 +54,19 @@ of BaseTarget in test_data_generation.py.
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
import sys
import typing
from abc import ABCMeta, abstractmethod
from typing import Iterator, List, Tuple, TypeVar
from abc import ABCMeta
from typing import Iterator
import scripts_path # pylint: disable=unused-import
from mbedtls_dev import test_case
from mbedtls_dev import test_data_generation
T = TypeVar('T') #pylint: disable=invalid-name
def hex_to_int(val: str) -> int:
return int(val, 16) if val else 0
def quote_str(val) -> str:
return "\"{}\"".format(val)
def combination_pairs(values: List[T]) -> List[Tuple[T, T]]:
"""Return all pair combinations from input values."""
# The return value is cast, as older versions of mypy are unable to derive
# the specific type returned by itertools.combinations_with_replacement.
return typing.cast(
List[Tuple[T, T]],
list(itertools.combinations_with_replacement(values, 2))
)
from mbedtls_dev import bignum_common
# Import modules containing additional test classes
# Test function classes in these modules will be registered by
# the framework
from mbedtls_dev import bignum_core # pylint: disable=unused-import
class BignumTarget(test_data_generation.BaseTarget, metaclass=ABCMeta):
#pylint: disable=abstract-method
@ -89,36 +74,14 @@ class BignumTarget(test_data_generation.BaseTarget, metaclass=ABCMeta):
target_basename = 'test_suite_mpi.generated'
class BignumOperation(BignumTarget, metaclass=ABCMeta):
"""Common features for bignum binary operations.
This adds functionality common in binary operation tests. This includes
generation of case descriptions, using descriptions of values and symbols
to represent the operation or result.
Attributes:
symbol: Symbol used for the operation in case description.
input_values: List of values to use as test case inputs. These are
combined to produce pairs of values.
input_cases: List of tuples containing pairs of test case inputs. This
can be used to implement specific pairs of inputs.
"""
symbol = ""
class BignumOperation(bignum_common.OperationCommon, BignumTarget, metaclass=ABCMeta):
#pylint: disable=abstract-method
"""Common features for bignum operations in legacy tests."""
input_values = [
"", "0", "7b", "-7b",
"0000000000000000123", "-0000000000000000123",
"1230000000000000000", "-1230000000000000000"
] # type: List[str]
input_cases = [] # type: List[Tuple[str, str]]
def __init__(self, val_a: str, val_b: str) -> None:
self.arg_a = val_a
self.arg_b = val_b
self.int_a = hex_to_int(val_a)
self.int_b = hex_to_int(val_b)
def arguments(self) -> List[str]:
return [quote_str(self.arg_a), quote_str(self.arg_b), self.result()]
]
def description(self) -> str:
"""Generate a description for the test case.
@ -135,15 +98,6 @@ class BignumOperation(BignumTarget, metaclass=ABCMeta):
)
return super().description()
@abstractmethod
def result(self) -> str:
"""Get the result of the operation.
This could be calculated during initialization and stored as `_result`
and then returned, or calculated when the method is called.
"""
raise NotImplementedError
@staticmethod
def value_description(val) -> str:
"""Generate a description of the argument val.
@ -167,21 +121,10 @@ class BignumOperation(BignumTarget, metaclass=ABCMeta):
tmp = "large " + tmp
return tmp
@classmethod
def get_value_pairs(cls) -> Iterator[Tuple[str, str]]:
"""Generator to yield pairs of inputs.
Combinations are first generated from all input values, and then
specific cases provided.
"""
yield from combination_pairs(cls.input_values)
yield from cls.input_cases
@classmethod
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
for a_value, b_value in cls.get_value_pairs():
cur_op = cls(a_value, b_value)
yield cur_op.create_test_case()
yield cls(a_value, b_value).create_test_case()
class BignumCmp(BignumOperation):
@ -221,7 +164,7 @@ class BignumAdd(BignumOperation):
symbol = "+"
test_function = "mpi_add_mpi"
test_name = "MPI add"
input_cases = combination_pairs(
input_cases = bignum_common.combination_pairs(
[
"1c67967269c6", "9cde3",
"-1c67967269c6", "-9cde3",
@ -229,7 +172,8 @@ class BignumAdd(BignumOperation):
)
def result(self) -> str:
return quote_str("{:x}".format(self.int_a + self.int_b))
return bignum_common.quote_str("{:x}").format(self.int_a + self.int_b)
if __name__ == '__main__':
# Use the section of the docstring relevant to the CLI as description