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:
Gilles Peskine 2021-04-27 21:03:43 +02:00
parent c7e1ea074a
commit 8b4a38176a
2 changed files with 52 additions and 5 deletions

View File

@ -312,11 +312,37 @@ class OpFail:
def __init__(self, info: Information) -> None:
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]:
"""Generate all test cases for operations that must fail."""
#pylint: disable=no-self-use
return # To do
yield #pylint: disable=unreachable
algorithms = sorted(self.constructors.algorithms)
for alg in self.constructors.generate_expressions(algorithms):
yield from self.test_cases_for_algorithm(alg)
class StorageKey(psa_storage.Key):

View File

@ -11,8 +11,29 @@
* END_DEPENDENCIES
*/
/* BEGIN_CASE depends_on:NEVER_USED */
void test_suites_must_have_at_least_one_function( )
/* BEGIN_CASE */
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 */