Adding new macro for tests failing

Signed-off-by: TRodziewicz <tomasz.rodziewicz@mobica.com>
This commit is contained in:
TRodziewicz 2021-07-07 17:29:43 +02:00
parent 5f58dfc9d9
commit 7871c2e736
9 changed files with 18 additions and 37 deletions

View file

@ -1,2 +0,0 @@
Changes
* Re-introduce extended checks for psa unlock/wipe key slot. Fixes #4680.

View file

@ -129,7 +129,7 @@ extern "C" {
* Only used when invasive testing is enabled via MBEDTLS_TEST_HOOKS. * Only used when invasive testing is enabled via MBEDTLS_TEST_HOOKS.
*/ */
extern void (*mbedtls_test_hook_error_add)( int, int, const char *, int ); extern void (*mbedtls_test_hook_error_add)( int, int, const char *, int );
extern void (*mbedtls_test_hook_assert_test)( int test, const char * file, int line ); extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
#endif #endif
/** /**

View file

@ -46,6 +46,19 @@
#define MBEDTLS_STATIC_TESTABLE static #define MBEDTLS_STATIC_TESTABLE static
#endif #endif
#if defined(MBEDTLS_TEST_HOOKS)
extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
do { \
if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
{ \
( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
} \
} while( 0 )
#else
#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
#endif /* defined(MBEDTLS_TEST_HOOKS) */
/** Allow library to access its structs' private members. /** Allow library to access its structs' private members.
* *
* Although structs defined in header files are publicly available, * Although structs defined in header files are publicly available,

View file

@ -1002,10 +1002,7 @@ psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
if( slot->lock_count != 1 ) if( slot->lock_count != 1 )
{ {
#if defined(MBEDTLS_TEST_HOOKS) MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 );
if( *mbedtls_test_hook_assert_test != NULL )
( *mbedtls_test_hook_assert_test )( slot->lock_count == 1, __FILE__, __LINE__ );
#endif
status = PSA_ERROR_CORRUPTION_DETECTED; status = PSA_ERROR_CORRUPTION_DETECTED;
} }

View file

@ -34,7 +34,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "mbedtls/error.h"
#if defined(MBEDTLS_PLATFORM_C) #if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
#else #else
@ -413,13 +412,7 @@ psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot )
return( PSA_SUCCESS ); return( PSA_SUCCESS );
} }
slot->lock_count = 1; MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count > 0 );
#if defined(MBEDTLS_TEST_HOOKS)
if( *mbedtls_test_hook_assert_test != NULL )
( *mbedtls_test_hook_assert_test )( slot->lock_count > 0, __FILE__, __LINE__ );
#endif
return( PSA_ERROR_CORRUPTION_DETECTED ); return( PSA_ERROR_CORRUPTION_DETECTED );
} }

View file

@ -164,7 +164,7 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
#if defined(MBEDTLS_TEST_HOOKS) #if defined(MBEDTLS_TEST_HOOKS)
void (*mbedtls_test_hook_error_add)( int, int, const char *, int ); void (*mbedtls_test_hook_error_add)( int, int, const char *, int );
void (*mbedtls_test_hook_assert_test)( int, const char *, int ); void (*mbedtls_test_hook_test_fail)( const char *, int, const char *);
#endif #endif
#endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */ #endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */

View file

@ -231,13 +231,4 @@ void mbedtls_test_err_add_check( int high, int low,
int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s ); int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s );
#endif /* MBEDTLS_BIGNUM_C */ #endif /* MBEDTLS_BIGNUM_C */
/**
* \brief Check value in first parameter.
*
* \note If the check fails, fail the test currently being run.
*/
#if defined(MBEDTLS_TEST_HOOKS)
void mbedtls_test_assert_test( int test, const char * file, int line );
#endif
#endif /* TEST_HELPERS_H */ #endif /* TEST_HELPERS_H */

View file

@ -274,13 +274,3 @@ int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s )
return( mbedtls_mpi_read_string( X, radix, s ) ); return( mbedtls_mpi_read_string( X, radix, s ) );
} }
#endif #endif
#if defined(MBEDTLS_TEST_HOOKS)
void mbedtls_test_assert_test( int test, const char * file, int line )
{
if ( !test )
{
mbedtls_test_fail( "Test hook - test assertion failed.", line, file );
}
}
#endif

View file

@ -242,10 +242,9 @@ int main( int argc, const char *argv[] )
#endif #endif
#if defined(MBEDTLS_TEST_HOOKS) #if defined(MBEDTLS_TEST_HOOKS)
mbedtls_test_hook_assert_test = &mbedtls_test_assert_test; mbedtls_test_hook_test_fail = &mbedtls_test_fail;
#endif #endif
int ret = mbedtls_test_platform_setup(); int ret = mbedtls_test_platform_setup();
if( ret != 0 ) if( ret != 0 )
{ {