2022-09-16 22:02:37 +02:00
|
|
|
"""Common code for test data generation.
|
|
|
|
|
|
|
|
This module defines classes that are of general use to automatically
|
|
|
|
generate .data files for unit tests, as well as a main function.
|
2022-08-24 12:30:03 +02:00
|
|
|
|
|
|
|
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
|
2022-08-23 17:07:37 +02:00
|
|
|
|
2022-08-24 13:18:25 +02:00
|
|
|
from abc import ABCMeta, abstractmethod
|
2022-08-24 13:42:00 +02:00
|
|
|
from typing import Callable, Dict, Iterable, Iterator, List, Type, TypeVar
|
2022-08-24 12:30:03 +02:00
|
|
|
|
2022-09-16 22:35:18 +02:00
|
|
|
from . import build_tree
|
|
|
|
from . import test_case
|
2022-08-24 12:30:03 +02:00
|
|
|
|
|
|
|
T = TypeVar('T') #pylint: disable=invalid-name
|
|
|
|
|
|
|
|
|
2022-08-24 13:18:25 +02:00
|
|
|
class BaseTarget(metaclass=ABCMeta):
|
2022-08-24 12:30:03 +02:00
|
|
|
"""Base target for test case generation.
|
|
|
|
|
2022-09-14 17:26:54 +02:00
|
|
|
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`.
|
2022-08-25 13:29:46 +02:00
|
|
|
|
2022-08-24 12:30:03 +02:00
|
|
|
Attributes:
|
2022-08-23 15:21:53 +02:00
|
|
|
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.
|
2022-08-31 18:01:38 +02:00
|
|
|
dependencies: A list of dependencies required for the test case.
|
2022-09-14 14:02:40 +02:00
|
|
|
show_test_count: Toggle for inclusion of `count` in the test description.
|
2022-08-23 15:21:53 +02:00
|
|
|
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
|
2022-08-25 13:29:46 +02:00
|
|
|
be `test_function`, a clearer equivalent, or a short summary of the
|
|
|
|
test function's purpose.
|
2022-08-24 12:30:03 +02:00
|
|
|
"""
|
|
|
|
count = 0
|
2022-08-23 15:21:53 +02:00
|
|
|
case_description = ""
|
2022-08-31 18:16:44 +02:00
|
|
|
dependencies = [] # type: List[str]
|
2022-09-14 14:02:40 +02:00
|
|
|
show_test_count = True
|
2022-08-23 15:21:53 +02:00
|
|
|
target_basename = ""
|
|
|
|
test_function = ""
|
|
|
|
test_name = ""
|
2022-08-24 12:30:03 +02:00
|
|
|
|
2022-08-24 18:04:07 +02:00
|
|
|
def __new__(cls, *args, **kwargs):
|
2022-08-24 19:09:10 +02:00
|
|
|
# pylint: disable=unused-argument
|
2022-08-24 18:04:07 +02:00
|
|
|
cls.count += 1
|
|
|
|
return super().__new__(cls)
|
2022-08-24 12:30:03 +02:00
|
|
|
|
2022-08-23 17:07:37 +02:00
|
|
|
@abstractmethod
|
2022-08-23 15:21:53 +02:00
|
|
|
def arguments(self) -> List[str]:
|
2022-08-23 17:07:37 +02:00
|
|
|
"""Get the list of arguments for the test case.
|
|
|
|
|
|
|
|
Override this method to provide the list of arguments required for
|
2022-08-25 13:29:46 +02:00
|
|
|
the `test_function`.
|
2022-08-23 17:07:37 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of arguments required for the test function.
|
|
|
|
"""
|
2022-08-25 10:56:51 +02:00
|
|
|
raise NotImplementedError
|
2022-08-24 12:30:03 +02:00
|
|
|
|
|
|
|
def description(self) -> str:
|
2022-08-25 13:29:46 +02:00
|
|
|
"""Create a test case description.
|
2022-08-23 17:07:37 +02:00
|
|
|
|
|
|
|
Creates a description of the test case, including a name for the test
|
2022-09-14 14:02:40 +02:00
|
|
|
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.
|
2022-08-23 17:07:37 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Description for the test case.
|
|
|
|
"""
|
2022-09-14 14:02:40 +02:00
|
|
|
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()
|
2022-08-24 12:30:03 +02:00
|
|
|
|
2022-08-23 17:07:37 +02:00
|
|
|
|
2022-08-24 12:30:03 +02:00
|
|
|
def create_test_case(self) -> test_case.TestCase:
|
2022-08-25 13:29:46 +02:00
|
|
|
"""Generate TestCase from the instance."""
|
2022-08-24 12:30:03 +02:00
|
|
|
tc = test_case.TestCase()
|
2022-08-23 15:21:53 +02:00
|
|
|
tc.set_description(self.description())
|
|
|
|
tc.set_function(self.test_function)
|
|
|
|
tc.set_arguments(self.arguments())
|
2022-08-31 18:01:38 +02:00
|
|
|
tc.set_dependencies(self.dependencies)
|
2022-08-24 12:30:03 +02:00
|
|
|
|
|
|
|
return tc
|
|
|
|
|
|
|
|
@classmethod
|
2022-08-24 13:42:00 +02:00
|
|
|
@abstractmethod
|
|
|
|
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
2022-08-25 13:29:46 +02:00
|
|
|
"""Generate test cases for the class test function.
|
2022-08-24 13:42:00 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2022-08-25 10:56:51 +02:00
|
|
|
raise NotImplementedError
|
2022-08-24 13:42:00 +02:00
|
|
|
|
|
|
|
@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
|
2022-08-25 17:27:05 +02:00
|
|
|
called to generate test cases first.
|
2022-08-24 13:42:00 +02:00
|
|
|
|
2022-08-25 13:29:46 +02:00
|
|
|
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.
|
2022-08-23 17:07:37 +02:00
|
|
|
"""
|
2022-08-24 13:42:00 +02:00
|
|
|
if cls.test_function:
|
|
|
|
yield from cls.generate_function_tests()
|
2022-08-24 12:30:03 +02:00
|
|
|
for subclass in sorted(cls.__subclasses__(), key=lambda c: c.__name__):
|
|
|
|
yield from subclass.generate_tests()
|
|
|
|
|
|
|
|
|
|
|
|
class TestGenerator:
|
2022-09-14 13:59:32 +02:00
|
|
|
"""Generate test cases and write to data files."""
|
2022-08-24 12:30:03 +02:00
|
|
|
def __init__(self, options) -> None:
|
2022-09-16 22:22:53 +02:00
|
|
|
self.test_suite_directory = options.directory
|
2022-09-14 13:59:32 +02:00
|
|
|
# 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.
|
2022-09-02 12:56:34 +02:00
|
|
|
self.targets.update({
|
|
|
|
subclass.target_basename: subclass.generate_tests
|
|
|
|
for subclass in BaseTarget.__subclasses__()
|
2022-09-30 17:28:43 +02:00
|
|
|
if subclass.target_basename
|
2022-09-02 12:56:34 +02:00
|
|
|
})
|
2022-08-24 12:30:03 +02:00
|
|
|
|
|
|
|
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`.
|
2022-09-02 12:56:34 +02:00
|
|
|
targets = {} # type: Dict[str, Callable[..., Iterable[test_case.TestCase]]]
|
2022-08-24 12:30:03 +02:00
|
|
|
|
|
|
|
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).
|
|
|
|
"""
|
2022-09-02 12:56:34 +02:00
|
|
|
test_cases = self.targets[name](*target_args)
|
2022-08-24 12:30:03 +02:00
|
|
|
self.write_test_data_file(name, test_cases)
|
|
|
|
|
2022-09-16 18:03:54 +02:00
|
|
|
def main(args, description: str, generator_class: Type[TestGenerator] = TestGenerator):
|
2022-08-24 12:30:03 +02:00
|
|
|
"""Command line entry point."""
|
2022-09-16 18:03:54 +02:00
|
|
|
parser = argparse.ArgumentParser(description=description)
|
2022-08-24 12:30:03 +02:00
|
|
|
parser.add_argument('--list', action='store_true',
|
|
|
|
help='List available targets and exit')
|
|
|
|
parser.add_argument('--list-for-cmake', action='store_true',
|
|
|
|
help='Print \';\'-separated list of available targets and exit')
|
2022-09-16 22:22:53 +02:00
|
|
|
# If specified explicitly, this option may be a path relative to the
|
|
|
|
# current directory when the script is invoked. The default value
|
|
|
|
# is relative to the mbedtls root, which we don't know yet. So we
|
|
|
|
# can't set a string as the default value here.
|
2022-09-14 14:39:20 +02:00
|
|
|
parser.add_argument('--directory', metavar='DIR',
|
2022-08-24 12:30:03 +02:00
|
|
|
help='Output directory (default: tests/suites)')
|
|
|
|
parser.add_argument('targets', nargs='*', metavar='TARGET',
|
|
|
|
help='Target file to generate (default: all; "-": none)')
|
|
|
|
options = parser.parse_args(args)
|
2022-09-16 22:22:53 +02:00
|
|
|
|
|
|
|
# Change to the mbedtls root, to keep things simple. But first, adjust
|
|
|
|
# command line options that might be relative paths.
|
|
|
|
if options.directory is None:
|
|
|
|
options.directory = 'tests/suites'
|
|
|
|
else:
|
|
|
|
options.directory = os.path.abspath(options.directory)
|
2022-08-24 12:30:03 +02:00
|
|
|
build_tree.chdir_to_root()
|
2022-09-16 22:22:53 +02:00
|
|
|
|
2022-08-24 12:30:03 +02:00
|
|
|
generator = generator_class(options)
|
|
|
|
if options.list:
|
2022-09-02 12:56:34 +02:00
|
|
|
for name in sorted(generator.targets):
|
2022-08-24 12:30:03 +02:00
|
|
|
print(generator.filename_for(name))
|
|
|
|
return
|
|
|
|
# List in a cmake list format (i.e. ';'-separated)
|
|
|
|
if options.list_for_cmake:
|
|
|
|
print(';'.join(generator.filename_for(name)
|
2022-09-02 12:56:34 +02:00
|
|
|
for name in sorted(generator.targets)), end='')
|
2022-08-24 12:30:03 +02:00
|
|
|
return
|
2022-09-02 12:56:34 +02:00
|
|
|
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))
|
2022-09-02 13:57:37 +02:00
|
|
|
for target in options.targets
|
|
|
|
if target != '-']
|
2022-09-02 12:56:34 +02:00
|
|
|
else:
|
|
|
|
options.targets = sorted(generator.targets)
|
2022-08-24 12:30:03 +02:00
|
|
|
for target in options.targets:
|
|
|
|
generator.generate_target(target)
|