quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

generate_bignum_tests.py (6617B)


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