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-11-09 17:04:41 +01:00
|
|
|
from typing import List
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
import scripts_path # pylint: disable=unused-import
|
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
|
2022-11-30 15:43:31 +01:00
|
|
|
from mbedtls_dev import bignum_core, bignum_mod_raw, bignum_mod # pylint: disable=unused-import
|
2022-07-08 14:54:57 +02:00
|
|
|
|
2022-11-09 13:14:14 +01:00
|
|
|
class BignumTarget(test_data_generation.BaseTarget):
|
2022-11-09 17:04:41 +01:00
|
|
|
#pylint: disable=too-few-public-methods
|
2022-10-21 18:54:43 +02:00
|
|
|
"""Target for bignum (legacy) test case generation."""
|
|
|
|
target_basename = 'test_suite_bignum.generated'
|
2022-07-08 14:54:57 +02:00
|
|
|
|
|
|
|
|
2022-11-09 13:14:14 +01:00
|
|
|
class BignumOperation(bignum_common.OperationCommon, BignumTarget,
|
|
|
|
metaclass=ABCMeta):
|
2022-09-30 17:28:43 +02:00
|
|
|
#pylint: disable=abstract-method
|
|
|
|
"""Common features for bignum operations in legacy tests."""
|
2022-11-20 14:32:54 +01:00
|
|
|
unique_combinations_only = True
|
2022-08-23 15:21:53 +02:00
|
|
|
input_values = [
|
2022-11-15 20:43:33 +01:00
|
|
|
"", "0", "-", "-0",
|
|
|
|
"7b", "-7b",
|
2022-07-08 14:54:57 +02:00
|
|
|
"0000000000000000123", "-0000000000000000123",
|
|
|
|
"1230000000000000000", "-1230000000000000000"
|
2022-09-30 17:28:43 +02:00
|
|
|
]
|
2022-07-08 14:54:57 +02:00
|
|
|
|
2022-11-15 20:43:33 +01:00
|
|
|
def description_suffix(self) -> str:
|
2022-11-10 09:15:21 +01:00
|
|
|
#pylint: disable=no-self-use # derived classes need self
|
2022-11-15 20:43:33 +01:00
|
|
|
"""Text to add at the end of the test case description."""
|
|
|
|
return ""
|
|
|
|
|
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
|
|
|
)
|
2022-11-15 20:43:33 +01:00
|
|
|
description_suffix = self.description_suffix()
|
|
|
|
if description_suffix:
|
|
|
|
self.case_description += " " + description_suffix
|
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)"
|
2022-11-15 20:43:33 +01:00
|
|
|
if val == "-":
|
|
|
|
return "negative 0 (null)"
|
2022-07-08 14:54:57 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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-11-15 20:43:33 +01:00
|
|
|
def __init__(self, val_a: str, val_b: str) -> None:
|
|
|
|
super().__init__(val_a, val_b)
|
|
|
|
self._result = self.int_a + self.int_b
|
2022-09-30 17:28:43 +02:00
|
|
|
|
2022-11-15 20:43:33 +01:00
|
|
|
def description_suffix(self) -> str:
|
|
|
|
if (self.int_a >= 0 and self.int_b >= 0):
|
|
|
|
return "" # obviously positive result or 0
|
|
|
|
if (self.int_a <= 0 and self.int_b <= 0):
|
|
|
|
return "" # obviously negative result or 0
|
|
|
|
# The sign of the result is not obvious, so indicate it
|
|
|
|
return ", result{}0".format('>' if self._result > 0 else
|
|
|
|
'<' if self._result < 0 else '=')
|
|
|
|
|
|
|
|
def result(self) -> List[str]:
|
|
|
|
return [bignum_common.quote_str("{:x}".format(self._result))]
|
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]))
|