Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.28] Add bignum test case generation script #6307

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
545911f
Add bignum test generation framework
wernerlewis Jul 8, 2022
423f99b
Add test generation for bignum cmp variant
wernerlewis Jul 18, 2022
5c1173b
Add test case generation for bignum add
wernerlewis Jul 18, 2022
1c413bd
Sort tests when generating cases
wernerlewis Jul 20, 2022
1bdee22
Remove set() to preserve test case order
wernerlewis Jul 20, 2022
d76c5ed
Fix type issues
wernerlewis Jul 20, 2022
f0910ae
Remove is None from if statement
wernerlewis Jul 20, 2022
92bb1cd
Fix incorrect indentation
wernerlewis Jul 20, 2022
dcad1e9
Separate common test generation classes/functions
wernerlewis Aug 24, 2022
70d3f3d
Remove abbreviations and clarify attributes
wernerlewis Aug 23, 2022
02998c4
Remove unneeded list concatenation
wernerlewis Aug 23, 2022
1c2a732
Convert bools to int before arithmetic
wernerlewis Aug 24, 2022
008d90d
Add details to docstrings
wernerlewis Aug 23, 2022
47e37b3
Use ABCMeta for abstract classes
wernerlewis Aug 24, 2022
c34d037
Split generate_tests to reduce code complexity
wernerlewis Aug 24, 2022
cace1aa
Use __new__() for case counting
wernerlewis Aug 24, 2022
6d04142
Remove trailing whitespace in description
wernerlewis Aug 24, 2022
9509f44
Add missing typing
wernerlewis Aug 24, 2022
478a4ce
Use typing casts for fixed-width tuples
wernerlewis Aug 24, 2022
486d258
Disable pylint unused arg in __new__
wernerlewis Aug 24, 2022
d77d33d
Raise NotImplementedError in abstract methods
wernerlewis Aug 25, 2022
412c497
Fix TARGET types and code style
wernerlewis Aug 25, 2022
b29f59f
Disable abstract check in pylint
wernerlewis Aug 25, 2022
b03420f
Clarify documentation
wernerlewis Aug 25, 2022
ac86390
Use argparser default for targets
wernerlewis Aug 25, 2022
6f67bae
Fix trailing whitespace
wernerlewis Aug 25, 2022
2b0f7d8
Modify wording in docstrings
wernerlewis Aug 25, 2022
18f94d8
Use `combinations_with_replacement` for inputs
wernerlewis Aug 31, 2022
486b341
Add dependencies attribute to BaseTarget
wernerlewis Aug 31, 2022
6cc5e5f
Use Python 3.5 style typing for dependencies
wernerlewis Aug 31, 2022
0d07e86
Rework TestGenerator to add file targets
wernerlewis Sep 2, 2022
e53be35
Remove unused imports
wernerlewis Sep 2, 2022
3edcee7
Use simpler int to hex string conversion
wernerlewis Sep 2, 2022
46c09a6
Move symbol definition out of __init__
wernerlewis Sep 12, 2022
8b2d14b
Replace L/R inputs with A/B
wernerlewis Sep 12, 2022
f518276
Update comments/docstrings in TestGenerator
wernerlewis Sep 14, 2022
113ddd0
Add toggle for test case count in descriptions
wernerlewis Sep 14, 2022
1965d48
Use typing.cast instead of unqualified cast
wernerlewis Sep 14, 2022
38c2491
Add combination_pairs helper function
wernerlewis Sep 14, 2022
64334d9
Update references to file targets in docstrings
wernerlewis Sep 14, 2022
4ed94a4
Use a script specific description in CLI help
wernerlewis Sep 16, 2022
05998a0
Add file generated by generate_bignum_tests.py
wernerlewis Sep 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 204 additions & 0 deletions scripts/mbedtls_dev/test_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
"""Common test generation classes and main function.

These are used both by generate_psa_tests.py and generate_bignum_tests.py.
"""

# 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 argparse
import os
import posixpath
import re

from abc import ABCMeta, abstractmethod
from typing import Callable, Dict, Iterable, Iterator, List, Type, TypeVar

from mbedtls_dev import test_case

T = TypeVar('T') #pylint: disable=invalid-name


class BaseTarget(metaclass=ABCMeta):
"""Base target for test case generation.

Child classes of this class represent an output file, and can be referred
to as file targets. These indicate where test cases will be written to for
all subclasses of the file target, which is set by `target_basename`.

Attributes:
count: Counter for test cases from this class.
case_description: Short description of the test case. This may be
automatically generated using the class, or manually set.
dependencies: A list of dependencies required for the test case.
show_test_count: Toggle for inclusion of `count` in the test description.
target_basename: Basename of file to write generated tests to. This
should be specified in a child class of BaseTarget.
test_function: Test function which the class generates cases for.
test_name: A common name or description of the test function. This can
be `test_function`, a clearer equivalent, or a short summary of the
test function's purpose.
"""
count = 0
case_description = ""
dependencies = [] # type: List[str]
show_test_count = True
target_basename = ""
test_function = ""
test_name = ""

def __new__(cls, *args, **kwargs):
# pylint: disable=unused-argument
cls.count += 1
return super().__new__(cls)

@abstractmethod
def arguments(self) -> List[str]:
"""Get the list of arguments for the test case.

Override this method to provide the list of arguments required for
the `test_function`.

Returns:
List of arguments required for the test function.
"""
raise NotImplementedError

def description(self) -> str:
"""Create a test case description.

Creates a description of the test case, including a name for the test
function, an optional case count, and a description of the specific
test case. This should inform a reader what is being tested, and
provide context for the test case.

Returns:
Description for the test case.
"""
if self.show_test_count:
return "{} #{} {}".format(
self.test_name, self.count, self.case_description
).strip()
else:
return "{} {}".format(self.test_name, self.case_description).strip()


def create_test_case(self) -> test_case.TestCase:
"""Generate TestCase from the instance."""
tc = test_case.TestCase()
tc.set_description(self.description())
tc.set_function(self.test_function)
tc.set_arguments(self.arguments())
tc.set_dependencies(self.dependencies)

return tc

@classmethod
@abstractmethod
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
"""Generate test cases for the class test function.

This will be called in classes where `test_function` is set.
Implementations should yield TestCase objects, by creating instances
of the class with appropriate input data, and then calling
`create_test_case()` on each.
"""
raise NotImplementedError

@classmethod
def generate_tests(cls) -> Iterator[test_case.TestCase]:
"""Generate test cases for the class and its subclasses.

In classes with `test_function` set, `generate_function_tests()` is
called to generate test cases first.

In all classes, this method will iterate over its subclasses, and
yield from `generate_tests()` in each. Calling this method on a class X
will yield test cases from all classes derived from X.
"""
if cls.test_function:
yield from cls.generate_function_tests()
for subclass in sorted(cls.__subclasses__(), key=lambda c: c.__name__):
yield from subclass.generate_tests()


class TestGenerator:
"""Generate test cases and write to data files."""
def __init__(self, options) -> None:
self.test_suite_directory = self.get_option(options, 'directory',
'tests/suites')
# Update `targets` with an entry for each child class of BaseTarget.
# Each entry represents a file generated by the BaseTarget framework,
# and enables generating the .data files using the CLI.
self.targets.update({
subclass.target_basename: subclass.generate_tests
for subclass in BaseTarget.__subclasses__()
})

@staticmethod
def get_option(options, name: str, default: T) -> T:
value = getattr(options, name, None)
return default if value is None else value

def filename_for(self, basename: str) -> str:
"""The location of the data file with the specified base name."""
return posixpath.join(self.test_suite_directory, basename + '.data')

def write_test_data_file(self, basename: str,
test_cases: Iterable[test_case.TestCase]) -> None:
"""Write the test cases to a .data file.

The output file is ``basename + '.data'`` in the test suite directory.
"""
filename = self.filename_for(basename)
test_case.write_data_file(filename, test_cases)

# Note that targets whose names contain 'test_format' have their content
# validated by `abi_check.py`.
targets = {} # type: Dict[str, Callable[..., Iterable[test_case.TestCase]]]

def generate_target(self, name: str, *target_args) -> None:
"""Generate cases and write to data file for a target.

For target callables which require arguments, override this function
and pass these arguments using super() (see PSATestGenerator).
"""
test_cases = self.targets[name](*target_args)
self.write_test_data_file(name, test_cases)

def main(args, description: str, generator_class: Type[TestGenerator] = TestGenerator):
"""Command line entry point."""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--list', action='store_true',
help='List available targets and exit')
parser.add_argument('targets', nargs='*', metavar='TARGET',
help='Target file to generate (default: all; "-": none)')
options = parser.parse_args(args)
generator = generator_class(options)
if options.list:
for name in sorted(generator.targets):
print(generator.filename_for(name))
return
if options.targets:
# Allow "-" as a special case so you can run
# ``generate_xxx_tests.py - $targets`` and it works uniformly whether
# ``$targets`` is empty or not.
options.targets = [os.path.basename(re.sub(r'\.data\Z', r'', target))
for target in options.targets
if target != '-']
else:
options.targets = sorted(generator.targets)
for target in options.targets:
generator.generate_target(target)
1 change: 1 addition & 0 deletions tests/scripts/check-generated-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ check scripts/generate_query_config.pl programs/test/query_config.c
check scripts/generate_features.pl library/version_features.c
check scripts/generate_visualc_files.pl visualc/VS2010
check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c
check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list)
check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list)
Loading