mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-02-06 12:40:02 +00:00
Generate test cases for hash operation failure
Test that hash operation functions fail when given a hash algorithm that is not supported or an algorithm that is not a hash. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
parent
c7e1ea074a
commit
8b4a38176a
@ -312,11 +312,37 @@ class OpFail:
|
|||||||
def __init__(self, info: Information) -> None:
|
def __init__(self, info: Information) -> None:
|
||||||
self.constructors = info.constructors
|
self.constructors = info.constructors
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def hash_test_cases(alg: str) -> Iterator[test_case.TestCase]:
|
||||||
|
"""Generate hash failure test cases for the specified algorithm."""
|
||||||
|
tc = test_case.TestCase()
|
||||||
|
is_hash = (alg.startswith('PSA_ALG_SHA') or
|
||||||
|
alg.startswith('PSA_ALG_MD') or
|
||||||
|
alg in frozenset(['PSA_ALG_RIPEMD160', 'PSA_ALG_ANY_HASH']))
|
||||||
|
if is_hash:
|
||||||
|
descr = 'not supported'
|
||||||
|
status = 'PSA_ERROR_NOT_SUPPORTED'
|
||||||
|
dependencies = ['!PSA_WANT_' + alg[4:]]
|
||||||
|
else:
|
||||||
|
descr = 'invalid'
|
||||||
|
status = 'PSA_ERROR_INVALID_ARGUMENT'
|
||||||
|
dependencies = automatic_dependencies(alg)
|
||||||
|
tc.set_description('PSA hash {}: {}'
|
||||||
|
.format(descr, re.sub(r'PSA_ALG_', r'', alg)))
|
||||||
|
tc.set_dependencies(dependencies)
|
||||||
|
tc.set_function('hash_fail')
|
||||||
|
tc.set_arguments([alg, status])
|
||||||
|
yield tc
|
||||||
|
|
||||||
|
def test_cases_for_algorithm(self, alg: str) -> Iterator[test_case.TestCase]:
|
||||||
|
"""Generate operation failure test cases for the specified algorithm."""
|
||||||
|
yield from self.hash_test_cases(alg)
|
||||||
|
|
||||||
def all_test_cases(self) -> Iterator[test_case.TestCase]:
|
def all_test_cases(self) -> Iterator[test_case.TestCase]:
|
||||||
"""Generate all test cases for operations that must fail."""
|
"""Generate all test cases for operations that must fail."""
|
||||||
#pylint: disable=no-self-use
|
algorithms = sorted(self.constructors.algorithms)
|
||||||
return # To do
|
for alg in self.constructors.generate_expressions(algorithms):
|
||||||
yield #pylint: disable=unreachable
|
yield from self.test_cases_for_algorithm(alg)
|
||||||
|
|
||||||
|
|
||||||
class StorageKey(psa_storage.Key):
|
class StorageKey(psa_storage.Key):
|
||||||
|
@ -11,8 +11,29 @@
|
|||||||
* END_DEPENDENCIES
|
* END_DEPENDENCIES
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* BEGIN_CASE depends_on:NEVER_USED */
|
/* BEGIN_CASE */
|
||||||
void test_suites_must_have_at_least_one_function( )
|
void hash_fail( int alg_arg, int expected_status_arg )
|
||||||
{
|
{
|
||||||
|
psa_status_t expected_status = expected_status_arg;
|
||||||
|
psa_algorithm_t alg = alg_arg;
|
||||||
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
||||||
|
uint8_t input[1] = {'A'};
|
||||||
|
uint8_t output[PSA_HASH_MAX_SIZE] = {0};
|
||||||
|
size_t length = SIZE_MAX;
|
||||||
|
|
||||||
|
PSA_INIT( );
|
||||||
|
|
||||||
|
TEST_EQUAL( expected_status,
|
||||||
|
psa_hash_setup( &operation, alg ) );
|
||||||
|
TEST_EQUAL( expected_status,
|
||||||
|
psa_hash_compute( alg, input, sizeof( input ),
|
||||||
|
output, sizeof( output ), &length ) );
|
||||||
|
TEST_EQUAL( expected_status,
|
||||||
|
psa_hash_compare( alg, input, sizeof( input ),
|
||||||
|
output, sizeof( output ) ) );
|
||||||
|
|
||||||
|
exit:
|
||||||
|
psa_hash_abort( &operation );
|
||||||
|
PSA_DONE( );
|
||||||
}
|
}
|
||||||
/* END_CASE */
|
/* END_CASE */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user