2022-07-08 14:54:57 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Generate test data for bignum functions.
|
|
|
|
|
|
|
|
With no arguments, generate all test data. With non-option arguments,
|
|
|
|
generate only the specified files.
|
2022-08-23 17:07:37 +02:00
|
|
|
|
|
|
|
Class structure:
|
|
|
|
|
|
|
|
Target classes are directly derived from test_generation.BaseTarget,
|
|
|
|
representing a target file. These indicate where test cases will be written
|
|
|
|
to in classes derived from the Target. Multiple Target classes must not
|
|
|
|
represent the same target_basename.
|
|
|
|
|
|
|
|
Each subclass derived from a Target can either be:
|
|
|
|
- A concrete class, representing a test function, which generates test cases.
|
|
|
|
- An abstract class containing shared methods and attributes, not associated
|
|
|
|
with a test function. An example is BignumOperation, which provides common
|
|
|
|
features used in binary bignum operations.
|
|
|
|
|
|
|
|
|
|
|
|
Adding test generation for a function:
|
|
|
|
|
|
|
|
A subclass representing the test function should be added, deriving from a
|
|
|
|
Target class or a descendant. This subclass must set/implement the following:
|
|
|
|
- test_function: the function name from the associated .function file.
|
|
|
|
- arguments(): generation of the arguments required for the test_function.
|
|
|
|
- generate_function_test(): generation of the test cases for the function.
|
|
|
|
|
|
|
|
Additional details and other attributes/methods are given in the documentation
|
|
|
|
of BaseTarget in test_generation.py.
|
2022-07-08 14:54:57 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright The Mbed TLS Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import itertools
|
|
|
|
import sys
|
2022-08-23 17:07:37 +02:00
|
|
|
|
|
|
|
from abc import abstractmethod
|
2022-08-24 12:30:03 +02:00
|
|
|
from typing import Callable, Dict, Iterator, List, Optional, Tuple, TypeVar
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
import scripts_path # pylint: disable=unused-import
|
|
|
|
from mbedtls_dev import test_case
|
2022-08-24 12:30:03 +02:00
|
|
|
from mbedtls_dev import test_generation
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
T = TypeVar('T') #pylint: disable=invalid-name
|
|
|
|
|
|
|
|
def hex_to_int(val):
|
|
|
|
return int(val, 16) if val else 0
|
|
|
|
|
|
|
|
def quote_str(val):
|
|
|
|
return "\"{}\"".format(val)
|
|
|
|
|
|
|
|
|
2022-08-24 12:30:03 +02:00
|
|
|
class BignumTarget(test_generation.BaseTarget):
|
2022-07-08 14:54:57 +02:00
|
|
|
"""Target for bignum (mpi) test case generation."""
|
2022-08-23 15:21:53 +02:00
|
|
|
target_basename = 'test_suite_mpi.generated'
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BignumOperation(BignumTarget):
|
2022-08-23 17:07:37 +02:00
|
|
|
"""Common features for test cases covering binary bignum 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.
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
Attributes:
|
2022-08-23 17:07:37 +02:00
|
|
|
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.
|
2022-08-23 15:21:53 +02:00
|
|
|
input_cases: List of tuples containing pairs of test case inputs. This
|
2022-07-08 14:54:57 +02:00
|
|
|
can be used to implement specific pairs of inputs.
|
|
|
|
"""
|
2022-08-23 15:21:53 +02:00
|
|
|
symbol = ""
|
|
|
|
input_values = [
|
2022-07-08 14:54:57 +02:00
|
|
|
"", "0", "7b", "-7b",
|
|
|
|
"0000000000000000123", "-0000000000000000123",
|
|
|
|
"1230000000000000000", "-1230000000000000000"
|
2022-07-20 15:13:44 +02:00
|
|
|
] # type: List[str]
|
|
|
|
input_cases = [] # type: List[Tuple[str, ...]]
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
def __init__(self, val_l: str, val_r: str) -> None:
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.arg_l = val_l
|
|
|
|
self.arg_r = val_r
|
|
|
|
self.int_l = hex_to_int(val_l)
|
|
|
|
self.int_r = hex_to_int(val_r)
|
|
|
|
|
2022-08-23 15:21:53 +02:00
|
|
|
def arguments(self):
|
|
|
|
return [quote_str(self.arg_l), quote_str(self.arg_r), self.result()]
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
def description(self):
|
2022-08-23 17:07:37 +02:00
|
|
|
"""Generate a description for the test case.
|
|
|
|
|
|
|
|
If not set, case_description uses the form A `symbol` B, where symbol
|
|
|
|
is used to represent the operation. Descriptions of each value are
|
|
|
|
generated to provide some context to the test case.
|
|
|
|
"""
|
2022-08-23 15:21:53 +02:00
|
|
|
if not self.case_description:
|
|
|
|
self.case_description = "{} {} {}".format(
|
|
|
|
self.value_description(self.arg_l),
|
|
|
|
self.symbol,
|
|
|
|
self.value_description(self.arg_r)
|
|
|
|
)
|
|
|
|
return super().description()
|
|
|
|
|
2022-08-23 17:07:37 +02:00
|
|
|
@abstractmethod
|
2022-07-08 14:54:57 +02:00
|
|
|
def result(self) -> Optional[str]:
|
2022-08-23 17:07:37 +02:00
|
|
|
"""Get the result of the operation.
|
|
|
|
|
|
|
|
This may be calculated during initialization and stored as `_result`,
|
|
|
|
or calculated when the method is called.
|
|
|
|
"""
|
|
|
|
pass
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2022-08-23 15:21:53 +02:00
|
|
|
def value_description(val) -> str:
|
2022-08-23 17:07:37 +02:00
|
|
|
"""Generate a description of the argument val.
|
|
|
|
|
|
|
|
This produces a simple description of the value, which are used in test
|
|
|
|
case naming, to avoid most generated cases only being numbered.
|
|
|
|
"""
|
2022-07-08 14:54:57 +02:00
|
|
|
if val == "":
|
|
|
|
return "0 (null)"
|
|
|
|
if val == "0":
|
|
|
|
return "0 (1 limb)"
|
|
|
|
|
|
|
|
if val[0] == "-":
|
|
|
|
tmp = "negative"
|
|
|
|
val = val[1:]
|
|
|
|
else:
|
|
|
|
tmp = "positive"
|
|
|
|
if val[0] == "0":
|
|
|
|
tmp += " with leading zero limb"
|
|
|
|
elif len(val) > 10:
|
|
|
|
tmp = "large " + tmp
|
|
|
|
return tmp
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_value_pairs(cls) -> Iterator[Tuple[str, ...]]:
|
2022-08-23 17:07:37 +02:00
|
|
|
"""Generator for pairs of inputs.
|
|
|
|
|
|
|
|
Combinations are first generated from all input values, and then
|
|
|
|
specific cases provided.
|
|
|
|
"""
|
2022-08-23 17:07:19 +02:00
|
|
|
yield from itertools.combinations(cls.input_values, 2)
|
|
|
|
yield from cls.input_cases
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def generate_tests(cls) -> Iterator[test_case.TestCase]:
|
2022-08-23 15:21:53 +02:00
|
|
|
if cls.test_function:
|
2022-07-08 14:54:57 +02:00
|
|
|
# Generate tests for the current class
|
|
|
|
for l_value, r_value in cls.get_value_pairs():
|
|
|
|
cur_op = cls(l_value, r_value)
|
|
|
|
yield cur_op.create_test_case()
|
|
|
|
# Once current class completed, check descendants
|
|
|
|
yield from super().generate_tests()
|
|
|
|
|
|
|
|
|
|
|
|
class BignumCmp(BignumOperation):
|
|
|
|
"""Target for bignum comparison test cases."""
|
|
|
|
count = 0
|
2022-08-23 15:21:53 +02:00
|
|
|
test_function = "mbedtls_mpi_cmp_mpi"
|
|
|
|
test_name = "MPI compare"
|
2022-07-08 14:54:57 +02:00
|
|
|
input_cases = [
|
|
|
|
("-2", "-3"),
|
|
|
|
("-2", "-2"),
|
|
|
|
("2b4", "2b5"),
|
|
|
|
("2b5", "2b6")
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, val_l, val_r):
|
|
|
|
super().__init__(val_l, val_r)
|
2022-08-24 17:37:44 +02:00
|
|
|
self._result = int(self.int_l > self.int_r) - int(self.int_l < self.int_r)
|
2022-08-23 15:21:53 +02:00
|
|
|
self.symbol = ["<", "==", ">"][self._result + 1]
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
def result(self):
|
|
|
|
return str(self._result)
|
|
|
|
|
|
|
|
|
2022-07-18 16:49:43 +02:00
|
|
|
class BignumCmpAbs(BignumCmp):
|
2022-08-23 17:07:37 +02:00
|
|
|
"""Target for bignum comparison, absolute variant."""
|
2022-07-18 16:49:43 +02:00
|
|
|
count = 0
|
2022-08-23 15:21:53 +02:00
|
|
|
test_function = "mbedtls_mpi_cmp_abs"
|
|
|
|
test_name = "MPI compare (abs)"
|
2022-07-18 16:49:43 +02:00
|
|
|
|
|
|
|
def __init__(self, val_l, val_r):
|
|
|
|
super().__init__(val_l.strip("-"), val_r.strip("-"))
|
|
|
|
|
|
|
|
|
2022-07-18 18:22:58 +02:00
|
|
|
class BignumAdd(BignumOperation):
|
|
|
|
"""Target for bignum addition test cases."""
|
|
|
|
count = 0
|
2022-08-23 15:21:53 +02:00
|
|
|
test_function = "mbedtls_mpi_add_mpi"
|
|
|
|
test_name = "MPI add"
|
2022-07-18 18:22:58 +02:00
|
|
|
input_cases = list(itertools.combinations(
|
|
|
|
[
|
|
|
|
"1c67967269c6", "9cde3",
|
|
|
|
"-1c67967269c6", "-9cde3",
|
|
|
|
], 2
|
|
|
|
))
|
|
|
|
|
|
|
|
def __init__(self, val_l, val_r):
|
|
|
|
super().__init__(val_l, val_r)
|
2022-08-23 15:21:53 +02:00
|
|
|
self.symbol = "+"
|
2022-07-18 18:22:58 +02:00
|
|
|
|
|
|
|
def result(self):
|
|
|
|
return quote_str(hex(self.int_l + self.int_r).replace("0x", "", 1))
|
|
|
|
|
|
|
|
|
2022-08-24 12:30:03 +02:00
|
|
|
class BignumTestGenerator(test_generation.TestGenerator):
|
|
|
|
"""Test generator subclass including bignum targets."""
|
2022-07-08 14:54:57 +02:00
|
|
|
TARGETS = {
|
2022-08-23 15:21:53 +02:00
|
|
|
subclass.target_basename: subclass.generate_tests for subclass in
|
2022-08-24 12:30:03 +02:00
|
|
|
test_generation.BaseTarget.__subclasses__()
|
|
|
|
} # type: Dict[str, Callable[[], test_case.TestCase]]
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-08-24 12:30:03 +02:00
|
|
|
test_generation.main(sys.argv[1:], BignumTestGenerator)
|