From 16229d872644289856c9ce5df0bb65aa8733053c Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 21 Nov 2024 17:26:00 +0100 Subject: [PATCH] Switch crypto_data_tests.py to automatic dependencies In `psa_test_cases.TestCase`: * Implement basic support for automatic dependencies, by calling `psa_information.automatic_dependencies`. * Support an alternative dependency prefix. No changes to the generated file. Signed-off-by: Gilles Peskine --- scripts/mbedtls_framework/crypto_data_tests.py | 12 +----------- scripts/mbedtls_framework/psa_test_case.py | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/scripts/mbedtls_framework/crypto_data_tests.py b/scripts/mbedtls_framework/crypto_data_tests.py index 5aef5c574..1d46e3f2c 100644 --- a/scripts/mbedtls_framework/crypto_data_tests.py +++ b/scripts/mbedtls_framework/crypto_data_tests.py @@ -16,15 +16,6 @@ from . import psa_test_case from . import test_case -def psa_low_level_dependencies(*expressions: str) -> List[str]: - """Infer dependencies of a PSA low-level test case by looking for PSA_xxx symbols. - - This function generates MBEDTLS_PSA_BUILTIN_xxx symbols. - """ - return psa_information.automatic_dependencies(*expressions, - prefix='MBEDTLS_PSA_BUILTIN_') - - class HashPSALowLevel: """Generate test cases for the PSA low-level hash interface.""" @@ -68,7 +59,7 @@ class HashPSALowLevel: function: str, note: str, arguments: List[str]) -> test_case.TestCase: """Construct one test case involving a hash.""" - tc = psa_test_case.TestCase() + tc = psa_test_case.TestCase(dependency_prefix='MBEDTLS_PSA_BUILTIN_') tc.set_description('{}{} {}' .format(function, ' ' + note if note else '', @@ -76,7 +67,6 @@ class HashPSALowLevel: tc.set_function(function) tc.set_arguments([alg.expression] + ['"{}"'.format(arg) for arg in arguments]) - tc.set_dependencies(psa_low_level_dependencies(alg.expression)) return tc def test_cases_for_hash(self, diff --git a/scripts/mbedtls_framework/psa_test_case.py b/scripts/mbedtls_framework/psa_test_case.py index c6839b84d..57172053e 100644 --- a/scripts/mbedtls_framework/psa_test_case.py +++ b/scripts/mbedtls_framework/psa_test_case.py @@ -9,6 +9,7 @@ import os import re from typing import FrozenSet, List, Optional, Set +from . import psa_information from . import test_case @@ -57,16 +58,24 @@ class TestCase(test_case.TestCase): involved in a given test case. """ - def __init__(self) -> None: + def __init__(self, dependency_prefix: Optional[str] = None) -> None: + """Construct a test case for a PSA Crypto API call. + + `dependency_prefix`: prefix to use in dependencies. Defaults to + ``'PSA_WANT_'``. Use ``'MBEDTLS_PSA_BUILTIN_'`` + when specifically testing builtin implementations. + """ super().__init__() del self.dependencies self.manual_dependencies = [] #type: List[str] self.automatic_dependencies = set() #type: Set[str] + self.dependency_prefix = dependency_prefix #type: Optional[str] - @staticmethod - def infer_dependencies(_arguments: List[str]) -> List[str]: + def infer_dependencies(self, arguments: List[str]) -> List[str]: """Infer dependencies based on the test case arguments.""" - return [] # not implemented yet + dependencies = psa_information.automatic_dependencies(*arguments, + prefix=self.dependency_prefix) + return dependencies def set_arguments(self, arguments: List[str]) -> None: """Set test case arguments and automatically infer dependencies."""