Fix pylint issues

Signed-off-by: gabor-mezei-arm <gabor.mezei@arm.com>
This commit is contained in:
gabor-mezei-arm 2021-06-24 14:38:25 +02:00
parent de25402300
commit b92d61b7e6
No known key found for this signature in database
GPG Key ID: 106F5A41ECC305BD

91
tests/scripts/generate_psa_tests.py Executable file → Normal file
View File

@ -248,11 +248,12 @@ class StorageKey(psa_storage.Key):
self.original_usage #type: psa_storage.Expr self.original_usage #type: psa_storage.Expr
class StorageKeyBuilder: class StorageKeyBuilder:
# pylint: disable=too-few-public-methods
def __init__(self, usage_extension: bool) -> None: def __init__(self, usage_extension: bool) -> None:
self.usage_extension = usage_extension #type: bool self.usage_extension = usage_extension #type: bool
def build(self, **kwargs) -> StorageKey: def build(self, **kwargs) -> StorageKey:
return StorageKey(usage_extension = self.usage_extension, **kwargs) return StorageKey(usage_extension=self.usage_extension, **kwargs)
class StorageFormat: class StorageFormat:
"""Storage format stability test cases.""" """Storage format stability test cases."""
@ -270,7 +271,7 @@ class StorageFormat:
self.constructors = info.constructors #type: macro_collector.PSAMacroEnumerator self.constructors = info.constructors #type: macro_collector.PSAMacroEnumerator
self.version = version #type: int self.version = version #type: int
self.forward = forward #type: bool self.forward = forward #type: bool
self.key_builder = StorageKeyBuilder(usage_extension = True) #type: StorageKeyBuilder self.key_builder = StorageKeyBuilder(usage_extension=True) #type: StorageKeyBuilder
def make_test_case(self, key: StorageKey) -> test_case.TestCase: def make_test_case(self, key: StorageKey) -> test_case.TestCase:
"""Construct a storage format test case for the given key. """Construct a storage format test case for the given key.
@ -323,13 +324,12 @@ class StorageFormat:
r'', short) r'', short)
short = re.sub(r'PSA_KEY_[A-Z]+_', r'', short) short = re.sub(r'PSA_KEY_[A-Z]+_', r'', short)
description = 'lifetime: ' + short description = 'lifetime: ' + short
key = self.key_builder.build( key = self.key_builder.build(version=self.version,
version=self.version, id=1, lifetime=lifetime,
id=1, lifetime=lifetime, type='PSA_KEY_TYPE_RAW_DATA', bits=8,
type='PSA_KEY_TYPE_RAW_DATA', bits=8, usage='PSA_KEY_USAGE_EXPORT', alg=0, alg2=0,
usage='PSA_KEY_USAGE_EXPORT', alg=0, alg2=0, material=b'L',
material=b'L', description=description)
description=description)
return key return key
def all_keys_for_lifetimes(self) -> List[StorageKey]: def all_keys_for_lifetimes(self) -> List[StorageKey]:
@ -367,7 +367,7 @@ class StorageFormat:
material=b'K', material=b'K',
description=description) description=description)
def all_keys_for_usage_flags( def generate_keys_for_usage_flags(
self, self,
extra_desc: Optional[str] = None extra_desc: Optional[str] = None
) -> List[StorageKey]: ) -> List[StorageKey]:
@ -405,20 +405,20 @@ class StorageFormat:
r'', r'',
kt.expression) kt.expression)
description = 'type: {} {}-bit'.format(short_expression, bits) description = 'type: {} {}-bit'.format(short_expression, bits)
keys.append(self.key_builder.build( keys.append(self.key_builder.build(version=self.version,
version=self.version, id=1, lifetime=0x00000001,
id=1, lifetime=0x00000001, type=kt.expression, bits=bits,
type=kt.expression, bits=bits, usage=usage_flags, alg=alg, alg2=alg2,
usage=usage_flags, alg=alg, alg2=alg2, material=key_material,
material=key_material, description=description))
description=description))
return keys return keys
def all_keys_for_types(self) -> List[StorageKey]: def all_keys_for_types(self) -> List[StorageKey]:
"""Generate test keys covering key types and their representations.""" """Generate test keys covering key types and their representations."""
key_types = sorted(self.constructors.key_types) key_types = sorted(self.constructors.key_types)
return [key for key_type in self.constructors.generate_expressions(key_types) return [key
for key in self.keys_for_type(key_type)] for key_type in self.constructors.generate_expressions(key_types)
for key in self.keys_for_type(key_type)]
def keys_for_algorithm(self, alg: str) -> List[StorageKey]: def keys_for_algorithm(self, alg: str) -> List[StorageKey]:
"""Generate test keys for the specified algorithm.""" """Generate test keys for the specified algorithm."""
@ -445,8 +445,9 @@ class StorageFormat:
def all_keys_for_algorithms(self) -> List[StorageKey]: def all_keys_for_algorithms(self) -> List[StorageKey]:
"""Generate test keys covering algorithm encodings.""" """Generate test keys covering algorithm encodings."""
algorithms = sorted(self.constructors.algorithms) algorithms = sorted(self.constructors.algorithms)
return [key for alg in self.constructors.generate_expressions(algorithms) return [key
for key in self.keys_for_algorithm(alg)] for alg in self.constructors.generate_expressions(algorithms)
for key in self.keys_for_algorithm(alg)]
def generate_all_keys(self) -> List[StorageKey]: def generate_all_keys(self) -> List[StorageKey]:
"""Generate all keys for the test cases.""" """Generate all keys for the test cases."""
@ -491,7 +492,9 @@ class StorageFormatV0(StorageFormat):
"""Generate test keys covering usage flags.""" """Generate test keys covering usage flags."""
# First generate keys without usage policy extension for # First generate keys without usage policy extension for
# compatibility testing, then generate the keys with extension # compatibility testing, then generate the keys with extension
# to check the extension is working. # to check the extension is working. Finally generate key for all known
# usage flag which needs to be separted because it is not affected by
# usage extension.
keys = [] #type: List[StorageKey] keys = [] #type: List[StorageKey]
prev_builder = self.key_builder prev_builder = self.key_builder
@ -511,6 +514,7 @@ class StorageFormatV0(StorageFormat):
key_type: str, key_type: str,
params: Optional[Iterable[str]] = None params: Optional[Iterable[str]] = None
) -> List[StorageKey]: ) -> List[StorageKey]:
# pylint: disable=too-many-locals
"""Generate test keys for the specified extendable usage flag, """Generate test keys for the specified extendable usage flag,
algorithm and key type combination. algorithm and key type combination.
""" """
@ -527,22 +531,22 @@ class StorageFormatV0(StorageFormat):
alg_expression = re.sub(r'PSA_ALG_', r'', alg) alg_expression = re.sub(r'PSA_ALG_', r'', alg)
alg_expression = re.sub(r',', r', ', re.sub(r' +', r'', alg_expression)) alg_expression = re.sub(r',', r', ', re.sub(r' +', r'', alg_expression))
key_type_expression = re.sub(r'\bPSA_(?:KEY_TYPE|ECC_FAMILY)_', key_type_expression = re.sub(r'\bPSA_(?:KEY_TYPE|ECC_FAMILY)_',
r'', r'',
kt.expression) kt.expression)
description = 'extend {}: {} {} {}-bit'.format( description = 'extend {}: {} {} {}-bit'.format(
usage_expression, alg_expression, key_type_expression, bits) usage_expression, alg_expression, key_type_expression, bits)
keys.append(self.key_builder.build( keys.append(self.key_builder.build(version=self.version,
version=self.version, id=1, lifetime=0x00000001,
id=1, lifetime=0x00000001, type=kt.expression, bits=bits,
type=kt.expression, bits=bits, usage=material_usage_flags,
usage=material_usage_flags, expected_usage=expected_usage_flags,
expected_usage=expected_usage_flags, alg=alg, alg2=alg2,
alg=alg, alg2=alg2, material=key_material,
material=key_material, description=description))
description=description))
return keys return keys
def gather_key_types_for_sign_alg(self) -> Dict[str, List[str]]: def gather_key_types_for_sign_alg(self) -> Dict[str, List[str]]:
# pylint: disable=too-many-locals
"""Match possible key types for sign algorithms.""" """Match possible key types for sign algorithms."""
# To create a valid combinaton both the algorithms and key types # To create a valid combinaton both the algorithms and key types
# must be filtered. Pair them with keywords created from its names. # must be filtered. Pair them with keywords created from its names.
@ -555,10 +559,8 @@ class StorageFormatV0(StorageFormat):
exclusive_keywords = { exclusive_keywords = {
'EDWARDS': 'ECC' 'EDWARDS': 'ECC'
} }
key_types = set(self.constructors.generate_expressions( key_types = set(self.constructors.generate_expressions(self.constructors.key_types))
self.constructors.key_types)) algorithms = set(self.constructors.generate_expressions(self.constructors.sign_algorithms))
algorithms = set(self.constructors.generate_expressions(
self.constructors.sign_algorithms))
alg_with_keys = {} #type: Dict[str, List[str]] alg_with_keys = {} #type: Dict[str, List[str]]
translation_table = str.maketrans('(', '_', ')') translation_table = str.maketrans('(', '_', ')')
for alg in algorithms: for alg in algorithms:
@ -600,17 +602,18 @@ class StorageFormatV0(StorageFormat):
prev_builder = self.key_builder prev_builder = self.key_builder
# Generate the key without usage extension # Generate the key without usage extension
self.key_builder = StorageKeyBuilder(usage_extension = False) self.key_builder = StorageKeyBuilder(usage_extension=False)
alg_with_keys = self.gather_key_types_for_sign_alg() alg_with_keys = self.gather_key_types_for_sign_alg()
key_restrictions = StorageKey.EXTENDABLE_USAGE_FLAGS_KEY_RESTRICTION key_restrictions = StorageKey.EXTENDABLE_USAGE_FLAGS_KEY_RESTRICTION
# Walk through all combintion. The key types must be filtered to fit # Walk through all combintion. The key types must be filtered to fit
# the specific usage flag. # the specific usage flag.
keys += [key for usage in StorageKey.EXTENDABLE_USAGE_FLAGS.keys() keys += [key
for alg in sorted(alg_with_keys.keys()) for usage in StorageKey.EXTENDABLE_USAGE_FLAGS
for key_type in sorted(filter( for alg in sorted(alg_with_keys)
lambda kt: re.match(key_restrictions[usage.string], kt), for key_type in sorted([kt
alg_with_keys[alg])) for kt in alg_with_keys[alg]
for key in self.keys_for_usage_extension(usage, alg, key_type)] if re.match(key_restrictions[usage.string], kt)])
for key in self.keys_for_usage_extension(usage, alg, key_type)]
self.key_builder = prev_builder self.key_builder = prev_builder
return keys return keys