Refactor PSA test helpers: don't depend on test_info access

Refactor some PSA test helper functions and macros to avoid depending
on test_info and test_fail inside functions. These identifiers are
only defined in helpers.function, so they're only available in test
suites, and not in test helper modules (tests/src/*.c) which are also
linked into example programs.

This is in preparation for moving function definitions from
psa_crypto_helpers.h to psa_crypto_helpers.c.

No behavior change.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2020-11-24 17:41:07 +01:00
parent 86cadb37d1
commit 1e00565111

View File

@ -26,52 +26,48 @@
#include <psa/crypto.h> #include <psa/crypto.h>
#include <psa_crypto_slot_management.h> #include <psa_crypto_slot_management.h>
static int mbedtls_test_helper_is_psa_pristine( int line, const char *file ) /** Check for things that have not been cleaned up properly in the
* PSA subsystem.
*
* \return NULL if nothing has leaked.
* \return A string literal explaining what has not been cleaned up
* if applicable.
*/
static const char *mbedtls_test_helper_is_psa_leaking( void )
{ {
mbedtls_psa_stats_t stats; mbedtls_psa_stats_t stats;
const char *msg = NULL;
mbedtls_psa_get_stats( &stats ); mbedtls_psa_get_stats( &stats );
if( stats.volatile_slots != 0 ) if( stats.volatile_slots != 0 )
msg = "A volatile slot has not been closed properly."; return( "A volatile slot has not been closed properly." );
else if( stats.persistent_slots != 0 ) if( stats.persistent_slots != 0 )
msg = "A persistent slot has not been closed properly."; return( "A persistent slot has not been closed properly." );
else if( stats.external_slots != 0 ) if( stats.external_slots != 0 )
msg = "An external slot has not been closed properly."; return( "An external slot has not been closed properly." );
else if( stats.half_filled_slots != 0 ) if( stats.half_filled_slots != 0 )
msg = "A half-filled slot has not been cleared properly."; return( "A half-filled slot has not been cleared properly." );
else if( stats.locked_slots != 0 ) if( stats.locked_slots != 0 )
{ return( "Some slots are still marked as locked." );
msg = "Some slots are still marked as locked.";
}
if( msg != NULL ) return( NULL );
test_fail( msg, line, file );
return( msg == NULL );
} }
/** Check that no PSA Crypto key slots are in use. /** Check that no PSA Crypto key slots are in use.
*/ */
#define ASSERT_PSA_PRISTINE( ) \ #define ASSERT_PSA_PRISTINE( ) \
do \ TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )
{ \
if( ! mbedtls_test_helper_is_psa_pristine( __LINE__, __FILE__ ) ) \
goto exit; \
} \
while( 0 )
static void mbedtls_test_helper_psa_done( int line, const char *file )
{
(void) mbedtls_test_helper_is_psa_pristine( line, file );
mbedtls_psa_crypto_free( );
}
/** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots /** Shut down the PSA Crypto subsystem. Expect a clean shutdown, with no slots
* in use. * in use.
*/ */
#define PSA_DONE( ) mbedtls_test_helper_psa_done( __LINE__, __FILE__ ) #define PSA_DONE( ) \
do \
{ \
ASSERT_PSA_PRISTINE( ); \
mbedtls_psa_crypto_free( ); \
} \
while( 0 )