Fix and simplify test assertions

mbedtls_test_fail does not copy the failure explanation string, so
passing a string on the stack doesn't work. This fixes a garbage
message that would appear if a test triggered a non-implemented code
path.

More generally, just use TEST_ASSERT instead of explicitly calling
mbedtls_test_fail, since we aren't playing any tricks with the error
location.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-02-14 01:34:21 +01:00
parent cc9db30851
commit 2385f71abd

View File

@ -291,9 +291,7 @@ static int exercise_signature_key( mbedtls_svc_key_id_t key,
hash_alg = KNOWN_SUPPORTED_HASH_ALG; hash_alg = KNOWN_SUPPORTED_HASH_ALG;
alg ^= PSA_ALG_ANY_HASH ^ hash_alg; alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
#else #else
mbedtls_test_fail( "No hash algorithm for hash-and-sign testing", TEST_ASSERT( ! "No hash algorithm for hash-and-sign testing" );
__LINE__, __FILE__ );
return( 1 );
#endif #endif
} }
@ -813,7 +811,7 @@ int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
psa_key_usage_t usage, psa_key_usage_t usage,
psa_algorithm_t alg ) psa_algorithm_t alg )
{ {
int ok; int ok = 0;
if( ! check_key_attributes_sanity( key ) ) if( ! check_key_attributes_sanity( key ) )
return( 0 ); return( 0 );
@ -837,18 +835,12 @@ int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
ok = exercise_key_agreement_key( key, usage, alg ); ok = exercise_key_agreement_key( key, usage, alg );
else else
{ TEST_ASSERT( ! "No code to exercise this category of algorithm" );
char message[40];
mbedtls_snprintf( message, sizeof( message ),
"No code to exercise alg=0x%08lx",
(unsigned long) alg );
mbedtls_test_fail( message, __LINE__, __FILE__ );
ok = 0;
}
ok = ok && exercise_export_key( key, usage ); ok = ok && exercise_export_key( key, usage );
ok = ok && exercise_export_public_key( key ); ok = ok && exercise_export_public_key( key );
exit:
return( ok ); return( ok );
} }