From 7871c2e736eae72a92d2260259dcf41d3c282e80 Mon Sep 17 00:00:00 2001 From: TRodziewicz Date: Wed, 7 Jul 2021 17:29:43 +0200 Subject: [PATCH] Adding new macro for tests failing Signed-off-by: TRodziewicz --- ChangeLog.d/issue4680.txt | 2 -- include/mbedtls/error.h | 2 +- library/common.h | 13 +++++++++++++ library/psa_crypto.c | 5 +---- library/psa_crypto_slot_management.c | 9 +-------- scripts/data_files/error.fmt | 2 +- tests/include/test/helpers.h | 9 --------- tests/src/helpers.c | 10 ---------- tests/suites/main_test.function | 3 +-- 9 files changed, 18 insertions(+), 37 deletions(-) delete mode 100644 ChangeLog.d/issue4680.txt diff --git a/ChangeLog.d/issue4680.txt b/ChangeLog.d/issue4680.txt deleted file mode 100644 index e37e4142c..000000000 --- a/ChangeLog.d/issue4680.txt +++ /dev/null @@ -1,2 +0,0 @@ -Changes - * Re-introduce extended checks for psa unlock/wipe key slot. Fixes #4680. diff --git a/include/mbedtls/error.h b/include/mbedtls/error.h index 49a641614..f00b4f5b2 100644 --- a/include/mbedtls/error.h +++ b/include/mbedtls/error.h @@ -129,7 +129,7 @@ extern "C" { * 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_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 /** diff --git a/library/common.h b/library/common.h index a2c8a1e72..9e4b0312b 100644 --- a/library/common.h +++ b/library/common.h @@ -46,6 +46,19 @@ #define MBEDTLS_STATIC_TESTABLE static #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. * * Although structs defined in header files are publicly available, diff --git a/library/psa_crypto.c b/library/psa_crypto.c index f5a879361..8312c0750 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1002,10 +1002,7 @@ psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) if( slot->lock_count != 1 ) { -#if defined(MBEDTLS_TEST_HOOKS) - if( *mbedtls_test_hook_assert_test != NULL ) - ( *mbedtls_test_hook_assert_test )( slot->lock_count == 1, __FILE__, __LINE__ ); -#endif + MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 ); status = PSA_ERROR_CORRUPTION_DETECTED; } diff --git a/library/psa_crypto_slot_management.c b/library/psa_crypto_slot_management.c index 4a0763f00..cb10f6e06 100644 --- a/library/psa_crypto_slot_management.c +++ b/library/psa_crypto_slot_management.c @@ -34,7 +34,6 @@ #include #include -#include "mbedtls/error.h" #if defined(MBEDTLS_PLATFORM_C) #include "mbedtls/platform.h" #else @@ -413,13 +412,7 @@ psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot ) return( PSA_SUCCESS ); } - slot->lock_count = 1; - -#if defined(MBEDTLS_TEST_HOOKS) - if( *mbedtls_test_hook_assert_test != NULL ) - ( *mbedtls_test_hook_assert_test )( slot->lock_count > 0, __FILE__, __LINE__ ); -#endif - + MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count > 0 ); return( PSA_ERROR_CORRUPTION_DETECTED ); } diff --git a/scripts/data_files/error.fmt b/scripts/data_files/error.fmt index 8651a7b39..fd491b858 100644 --- a/scripts/data_files/error.fmt +++ b/scripts/data_files/error.fmt @@ -164,7 +164,7 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen ) #if defined(MBEDTLS_TEST_HOOKS) 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 /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */ diff --git a/tests/include/test/helpers.h b/tests/include/test/helpers.h index 8ee699668..27e5599ed 100644 --- a/tests/include/test/helpers.h +++ b/tests/include/test/helpers.h @@ -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 ); #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 */ diff --git a/tests/src/helpers.c b/tests/src/helpers.c index e35ed62d6..4d3d53da5 100644 --- a/tests/src/helpers.c +++ b/tests/src/helpers.c @@ -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 ) ); } #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 diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function index f34c1bd2f..3044bed35 100644 --- a/tests/suites/main_test.function +++ b/tests/suites/main_test.function @@ -242,10 +242,9 @@ int main( int argc, const char *argv[] ) #endif #if defined(MBEDTLS_TEST_HOOKS) - mbedtls_test_hook_assert_test = &mbedtls_test_assert_test; + mbedtls_test_hook_test_fail = &mbedtls_test_fail; #endif - int ret = mbedtls_test_platform_setup(); if( ret != 0 ) {