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:
|
|
|
|
|
2022-09-16 21:41:47 +02:00
|
|
|
Child classes of test_data_generation.BaseTarget (file targets) represent an output
|
2022-08-25 13:29:46 +02:00
|
|
|
file. These indicate where test cases will be written to, for all subclasses of
|
2022-09-14 17:26:54 +02:00
|
|
|
this target. Multiple file targets should not reuse a `target_basename`.
|
2022-08-23 17:07:37 +02:00
|
|
|
|
2022-09-14 17:26:54 +02:00
|
|
|
Each subclass derived from a file target can either be:
|
2022-08-23 17:07:37 +02:00
|
|
|
- A concrete class, representing a test function, which generates test cases.
|
|
|
|
- An abstract class containing shared methods and attributes, not associated
|
2022-08-25 13:29:46 +02:00
|
|
|
with a test function. An example is BignumOperation, which provides
|
|
|
|
common features used for bignum binary operations.
|
2022-08-23 17:07:37 +02:00
|
|
|
|
2022-08-25 13:29:46 +02:00
|
|
|
Both concrete and abstract subclasses can be derived from, to implement
|
|
|
|
additional test cases (see BignumCmp and BignumCmpAbs for examples of deriving
|
|
|
|
from abstract and concrete classes).
|
2022-08-23 17:07:37 +02:00
|
|
|
|
2022-08-25 13:29:46 +02:00
|
|
|
|
|
|
|
Adding test case generation for a function:
|
2022-08-23 17:07:37 +02:00
|
|
|
|
|
|
|
A subclass representing the test function should be added, deriving from a
|
2022-09-14 17:26:54 +02:00
|
|
|
file target such as BignumTarget. This test class must set/implement the
|
2022-08-25 17:27:05 +02:00
|
|
|
following:
|
2022-08-23 17:07:37 +02:00
|
|
|
- test_function: the function name from the associated .function file.
|
2022-08-25 13:29:46 +02:00
|
|
|
- test_name: a descriptive name or brief summary to refer to the test
|
|
|
|
function.
|
|
|
|
- arguments(): a method to generate the list of arguments required for the
|
|
|
|
test_function.
|
2022-09-21 23:13:04 +02:00
|
|
|
- generate_function_tests(): a method to generate TestCases for the function.
|
2022-08-25 13:29:46 +02:00
|
|
|
This should create instances of the class with required input data, and
|
|
|
|
call `.create_test_case()` to yield the TestCase.
|
2022-08-23 17:07:37 +02:00
|
|
|
|
|
|
|
Additional details and other attributes/methods are given in the documentation
|
2022-09-16 21:41:47 +02:00
|
|
|
of BaseTarget in test_data_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 sys
|
2022-08-23 17:07:37 +02:00
|
|
|
|
2022-09-30 17:28:43 +02:00
|
|
|
from abc import ABCMeta
|
2022-10-12 15:53:17 +02:00
|
|
|
from typing import Iterator, List
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
import scripts_path # pylint: disable=unused-import
|
|
|
|
from mbedtls_dev import test_case
|
2022-09-16 21:41:47 +02:00
|
|
|
from mbedtls_dev import test_data_generation
|
2022-09-30 17:28:43 +02:00
|
|
|
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
|
2022-07-08 14:54:57 +02:00
|
|
|
|
2022-09-16 21:41:47 +02:00
|
|
|
class BignumTarget(test_data_generation.BaseTarget, metaclass=ABCMeta):
|
2022-08-25 12:17:35 +02:00
|
|
|
#pylint: disable=abstract-method
|
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
|
|
|
|
|
|
|
|
2022-09-30 17:28:43 +02:00
|
|
|
class BignumOperation(bignum_common.OperationCommon, BignumTarget, metaclass=ABCMeta):
|
|
|
|
#pylint: disable=abstract-method
|
|
|
|
"""Common features for bignum operations in legacy tests."""
|
2022-08-23 15:21:53 +02:00
|
|
|
input_values = [
|
2022-07-08 14:54:57 +02:00
|
|
|
"", "0", "7b", "-7b",
|
|
|
|
"0000000000000000123", "-0000000000000000123",
|
|
|
|
"1230000000000000000", "-1230000000000000000"
|
2022-09-30 17:28:43 +02:00
|
|
|
]
|
2022-07-08 14:54:57 +02:00
|
|
|
|
2022-08-24 18:46:22 +02:00
|
|
|
def description(self) -> str:
|
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(
|
2022-09-12 18:35:27 +02:00
|
|
|
self.value_description(self.arg_a),
|
2022-08-23 15:21:53 +02:00
|
|
|
self.symbol,
|
2022-09-12 18:35:27 +02:00
|
|
|
self.value_description(self.arg_b)
|
2022-08-23 15:21:53 +02:00
|
|
|
)
|
|
|
|
return super().description()
|
|
|
|
|
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.
|
|
|
|
|
2022-08-25 17:27:05 +02:00
|
|
|
This produces a simple description of the value, which is used in test
|
|
|
|
case naming to add context.
|
2022-08-23 17:07:37 +02:00
|
|
|
"""
|
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
|
2022-08-24 13:42:00 +02:00
|
|
|
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
2022-09-12 18:35:27 +02:00
|
|
|
for a_value, b_value in cls.get_value_pairs():
|
2022-09-30 17:28:43 +02:00
|
|
|
yield cls(a_value, b_value).create_test_case()
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BignumCmp(BignumOperation):
|
2022-08-25 13:29:46 +02:00
|
|
|
"""Test cases for bignum value comparison."""
|
2022-07-08 14:54:57 +02:00
|
|
|
count = 0
|
2022-09-21 13:19:18 +02:00
|
|
|
test_function = "mpi_cmp_mpi"
|
2022-08-23 15:21:53 +02:00
|
|
|
test_name = "MPI compare"
|
2022-07-08 14:54:57 +02:00
|
|
|
input_cases = [
|
|
|
|
("-2", "-3"),
|
|
|
|
("-2", "-2"),
|
|
|
|
("2b4", "2b5"),
|
|
|
|
("2b5", "2b6")
|
|
|
|
]
|
|
|
|
|
2022-09-12 18:35:27 +02:00
|
|
|
def __init__(self, val_a, val_b) -> None:
|
|
|
|
super().__init__(val_a, val_b)
|
|
|
|
self._result = int(self.int_a > self.int_b) - int(self.int_a < self.int_b)
|
2022-08-23 15:21:53 +02:00
|
|
|
self.symbol = ["<", "==", ">"][self._result + 1]
|
2022-07-08 14:54:57 +02:00
|
|
|
|
2022-10-12 15:53:17 +02:00
|
|
|
def result(self) -> List[str]:
|
|
|
|
return [str(self._result)]
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
|
2022-07-18 16:49:43 +02:00
|
|
|
class BignumCmpAbs(BignumCmp):
|
2022-08-25 13:29:46 +02:00
|
|
|
"""Test cases for absolute bignum value comparison."""
|
2022-07-18 16:49:43 +02:00
|
|
|
count = 0
|
2022-09-21 13:19:18 +02:00
|
|
|
test_function = "mpi_cmp_abs"
|
2022-08-23 15:21:53 +02:00
|
|
|
test_name = "MPI compare (abs)"
|
2022-07-18 16:49:43 +02:00
|
|
|
|
2022-09-12 18:35:27 +02:00
|
|
|
def __init__(self, val_a, val_b) -> None:
|
|
|
|
super().__init__(val_a.strip("-"), val_b.strip("-"))
|
2022-07-18 16:49:43 +02:00
|
|
|
|
|
|
|
|
2022-07-18 18:22:58 +02:00
|
|
|
class BignumAdd(BignumOperation):
|
2022-08-25 13:29:46 +02:00
|
|
|
"""Test cases for bignum value addition."""
|
2022-07-18 18:22:58 +02:00
|
|
|
count = 0
|
2022-09-12 18:34:15 +02:00
|
|
|
symbol = "+"
|
2022-09-21 13:19:18 +02:00
|
|
|
test_function = "mpi_add_mpi"
|
2022-08-23 15:21:53 +02:00
|
|
|
test_name = "MPI add"
|
2022-09-30 17:28:43 +02:00
|
|
|
input_cases = bignum_common.combination_pairs(
|
2022-09-14 16:12:46 +02:00
|
|
|
[
|
|
|
|
"1c67967269c6", "9cde3",
|
|
|
|
"-1c67967269c6", "-9cde3",
|
|
|
|
]
|
2022-08-24 19:03:30 +02:00
|
|
|
)
|
2022-07-18 18:22:58 +02:00
|
|
|
|
2022-10-12 15:53:17 +02:00
|
|
|
def result(self) -> List[str]:
|
|
|
|
return [bignum_common.quote_str("{:x}").format(self.int_a + self.int_b)]
|
2022-09-30 17:28:43 +02:00
|
|
|
|
2022-07-18 18:22:58 +02:00
|
|
|
|
2022-07-08 14:54:57 +02:00
|
|
|
if __name__ == '__main__':
|
2022-09-16 18:03:54 +02:00
|
|
|
# Use the section of the docstring relevant to the CLI as description
|
2022-09-16 21:41:47 +02:00
|
|
|
test_data_generation.main(sys.argv[1:], "\n".join(__doc__.splitlines()[:4]))
|