Use an iterator when constructing test cases

It's more pythonic, and more importantly more readable.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-02-17 15:11:05 +01:00
parent 0298bdae8d
commit 3d7783909a

View File

@ -24,7 +24,7 @@ import argparse
import os
import re
import sys
from typing import Callable, Dict, FrozenSet, Iterable, List, Optional, TypeVar
from typing import Callable, Dict, FrozenSet, Iterable, Iterator, List, Optional, TypeVar
import scripts_path # pylint: disable=unused-import
from mbedtls_dev import crypto_knowledge
@ -139,7 +139,7 @@ class NotSupported:
kt: crypto_knowledge.KeyType,
param: Optional[int] = None,
param_descr: str = '',
) -> List[test_case.TestCase]:
) -> Iterator[test_case.TestCase]:
"""Return test cases exercising key creation when the given type is unsupported.
If param is present and not None, emit test cases conditioned on this
@ -149,7 +149,7 @@ class NotSupported:
if kt.name in self.ALWAYS_SUPPORTED:
# Don't generate test cases for key types that are always supported.
# They would be skipped in all configurations, which is noise.
return []
return
import_dependencies = [('!' if param is None else '') +
psa_want_symbol(kt.name)]
if kt.params is not None:
@ -160,44 +160,39 @@ class NotSupported:
generate_dependencies = []
else:
generate_dependencies = import_dependencies
test_cases = []
for bits in kt.sizes_to_test():
test_cases.append(test_case_for_key_type_not_supported(
yield test_case_for_key_type_not_supported(
'import', kt.expression, bits,
finish_family_dependencies(import_dependencies, bits),
test_case.hex_string(kt.key_material(bits)),
param_descr=param_descr,
))
)
if not generate_dependencies and param is not None:
# If generation is impossible for this key type, rather than
# supported or not depending on implementation capabilities,
# only generate the test case once.
continue
test_cases.append(test_case_for_key_type_not_supported(
yield test_case_for_key_type_not_supported(
'generate', kt.expression, bits,
finish_family_dependencies(generate_dependencies, bits),
str(bits),
param_descr=param_descr,
))
)
# To be added: derive
return test_cases
def generate_not_supported(self) -> List[test_case.TestCase]:
def test_cases_for_not_supported(self) -> Iterator[test_case.TestCase]:
"""Generate test cases that exercise the creation of keys of unsupported types."""
test_cases = []
for key_type in sorted(self.constructors.key_types):
kt = crypto_knowledge.KeyType(key_type)
test_cases += self.test_cases_for_key_type_not_supported(kt)
# To be added: parametrized key types FFDH
yield from self.test_cases_for_key_type_not_supported(kt)
for curve_family in sorted(self.constructors.ecc_curves):
for constr in ('PSA_KEY_TYPE_ECC_KEY_PAIR',
'PSA_KEY_TYPE_ECC_PUBLIC_KEY'):
kt = crypto_knowledge.KeyType(constr, [curve_family])
test_cases += self.test_cases_for_key_type_not_supported(
yield from self.test_cases_for_key_type_not_supported(
kt, param_descr='type')
test_cases += self.test_cases_for_key_type_not_supported(
yield from self.test_cases_for_key_type_not_supported(
kt, 0, param_descr='curve')
return test_cases
class TestGenerator:
@ -228,7 +223,7 @@ class TestGenerator:
TARGETS = {
'test_suite_psa_crypto_not_supported.generated':
lambda info: NotSupported(info).generate_not_supported(),
lambda info: NotSupported(info).test_cases_for_not_supported(),
} #type: Dict[str, Callable[[Information], Iterable[test_case.TestCase]]]
def generate_target(self, name: str) -> None: