crypto_se_driver: add an error injection mechanism to the mocks

This commit is contained in:
Unknown 2019-07-10 09:11:01 -04:00 committed by Darryl Green
parent 9fd6b0cb6f
commit 903b5da51c
2 changed files with 71 additions and 24 deletions

View file

@ -1,8 +1,23 @@
SE key importing mock test SE key importing mock test
mock_import: mock_import:PSA_SUCCESS:PSA_SUCCESS:PSA_SUCCESS
SE key importing mock test: alloc failed
mock_import:PSA_ERROR_HARDWARE_FAILURE:PSA_SUCCESS:PSA_ERROR_HARDWARE_FAILURE
SE key importing mock test: import failed
mock_import:PSA_SUCCESS:PSA_ERROR_HARDWARE_FAILURE:PSA_ERROR_HARDWARE_FAILURE
SE key exporting mock test SE key exporting mock test
mock_export: mock_export:PSA_SUCCESS:PSA_SUCCESS
SE key exporting mock test: export failed
mock_export:PSA_ERROR_HARDWARE_FAILURE:PSA_ERROR_HARDWARE_FAILURE
SE key generating mock test SE key generating mock test
mock_generate: mock_generate:PSA_SUCCESS:PSA_SUCCESS:PSA_SUCCESS
SE key generating mock test: alloc failed
mock_generate:PSA_ERROR_HARDWARE_FAILURE:PSA_SUCCESS:PSA_ERROR_HARDWARE_FAILURE
SE key generating mock test: generating failed
mock_generate:PSA_SUCCESS:PSA_ERROR_HARDWARE_FAILURE:PSA_ERROR_HARDWARE_FAILURE

View file

@ -10,6 +10,7 @@ static struct
psa_key_slot_number_t key_slot; psa_key_slot_number_t key_slot;
psa_key_attributes_t attributes; psa_key_attributes_t attributes;
size_t pubkey_size; size_t pubkey_size;
psa_status_t return_value;
} mock_generate_data; } mock_generate_data;
static struct static struct
@ -18,6 +19,7 @@ static struct
psa_key_slot_number_t key_slot; psa_key_slot_number_t key_slot;
psa_key_attributes_t attributes; psa_key_attributes_t attributes;
size_t data_length; size_t data_length;
psa_status_t return_value;
} mock_import_data; } mock_import_data;
static struct static struct
@ -25,17 +27,20 @@ static struct
uint16_t called; uint16_t called;
psa_key_slot_number_t slot_number; psa_key_slot_number_t slot_number;
size_t data_size; size_t data_size;
psa_status_t return_value;
} mock_export_data; } mock_export_data;
static struct static struct
{ {
uint16_t called; uint16_t called;
psa_status_t return_value;
} mock_allocate_data; } mock_allocate_data;
static struct static struct
{ {
uint16_t called; uint16_t called;
psa_key_slot_number_t slot_number; psa_key_slot_number_t slot_number;
psa_status_t return_value;
} mock_destroy_data; } mock_destroy_data;
static void mock_teardown( void ) static void mock_teardown( void )
@ -63,7 +68,7 @@ static psa_status_t mock_generate( psa_drv_se_context_t *drv_context,
mock_generate_data.attributes = *attributes; mock_generate_data.attributes = *attributes;
mock_generate_data.pubkey_size = pubkey_size; mock_generate_data.pubkey_size = pubkey_size;
return( PSA_SUCCESS ); return( mock_generate_data.return_value );
} }
static psa_status_t mock_import( psa_drv_se_context_t *drv_context, static psa_status_t mock_import( psa_drv_se_context_t *drv_context,
@ -82,7 +87,7 @@ static psa_status_t mock_import( psa_drv_se_context_t *drv_context,
mock_import_data.attributes = *attributes; mock_import_data.attributes = *attributes;
mock_import_data.data_length = data_length; mock_import_data.data_length = data_length;
return( PSA_SUCCESS ); return( mock_import_data.return_value );
} }
psa_status_t mock_export( psa_drv_se_context_t *context, psa_status_t mock_export( psa_drv_se_context_t *context,
@ -99,7 +104,7 @@ psa_status_t mock_export( psa_drv_se_context_t *context,
mock_export_data.slot_number = slot_number; mock_export_data.slot_number = slot_number;
mock_export_data.data_size = data_size; mock_export_data.data_size = data_size;
return( PSA_SUCCESS ); return( mock_export_data.return_value );
} }
psa_status_t mock_allocate( psa_drv_se_context_t *drv_context, psa_status_t mock_allocate( psa_drv_se_context_t *drv_context,
@ -117,7 +122,7 @@ psa_status_t mock_allocate( psa_drv_se_context_t *drv_context,
mock_allocate_data.called++; mock_allocate_data.called++;
*key_slot = 0; *key_slot = 0;
return( PSA_SUCCESS ); return( mock_allocate_data.return_value );
} }
psa_status_t mock_destroy( psa_drv_se_context_t *context, psa_status_t mock_destroy( psa_drv_se_context_t *context,
@ -130,7 +135,7 @@ psa_status_t mock_destroy( psa_drv_se_context_t *context,
mock_destroy_data.called++; mock_destroy_data.called++;
mock_destroy_data.slot_number = slot_number; mock_destroy_data.slot_number = slot_number;
return( PSA_SUCCESS ); return( mock_destroy_data.return_value );
} }
/* END_HEADER */ /* END_HEADER */
@ -141,7 +146,9 @@ psa_status_t mock_destroy( psa_drv_se_context_t *context,
*/ */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mock_import( ) void mock_import( int mock_alloc_return_value,
int mock_import_return_value,
int expected_result )
{ {
psa_drv_se_t driver; psa_drv_se_t driver;
psa_drv_se_key_management_t key_management; psa_drv_se_key_management_t key_management;
@ -151,6 +158,8 @@ void mock_import( )
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
const uint8_t key_material[3] = {0xfa, 0xca, 0xde}; const uint8_t key_material[3] = {0xfa, 0xca, 0xde};
mock_allocate_data.return_value = mock_alloc_return_value;
mock_import_data.return_value = mock_import_return_value;
memset( &driver, 0, sizeof( driver ) ); memset( &driver, 0, sizeof( driver ) );
memset( &key_management, 0, sizeof( key_management ) ); memset( &key_management, 0, sizeof( key_management ) );
driver.hal_version = PSA_DRV_SE_HAL_VERSION; driver.hal_version = PSA_DRV_SE_HAL_VERSION;
@ -166,18 +175,27 @@ void mock_import( )
psa_set_key_lifetime( &attributes, lifetime ); psa_set_key_lifetime( &attributes, lifetime );
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
PSA_ASSERT( psa_import_key( &attributes, TEST_ASSERT( psa_import_key( &attributes,
key_material, sizeof( key_material ), key_material, sizeof( key_material ),
&handle ) ); &handle ) == expected_result );
TEST_ASSERT( mock_allocate_data.called == 1 ); TEST_ASSERT( mock_allocate_data.called == 1 );
TEST_ASSERT( mock_import_data.called == 1 ); TEST_ASSERT( mock_import_data.called ==
TEST_ASSERT( mock_import_data.attributes.core.type == PSA_KEY_TYPE_RAW_DATA ); ( mock_alloc_return_value == PSA_SUCCESS? 1 : 0 ) );
TEST_ASSERT( mock_import_data.attributes.core.id ==
PSA_ASSERT( psa_destroy_key( handle ) ); ( mock_alloc_return_value == PSA_SUCCESS? id : 0 ) );
TEST_ASSERT( mock_import_data.attributes.core.lifetime ==
TEST_ASSERT( mock_destroy_data.called == 1 ); ( mock_alloc_return_value == PSA_SUCCESS? lifetime : 0 ) );
TEST_ASSERT( mock_import_data.attributes.core.policy.usage ==
( mock_alloc_return_value == PSA_SUCCESS? PSA_KEY_USAGE_EXPORT : 0 ) );
TEST_ASSERT( mock_import_data.attributes.core.type ==
( mock_alloc_return_value == PSA_SUCCESS? PSA_KEY_TYPE_RAW_DATA : 0 ) );
if( expected_result == PSA_SUCCESS )
{
PSA_ASSERT( psa_destroy_key( handle ) );
TEST_ASSERT( mock_destroy_data.called == 1 );
}
exit: exit:
PSA_DONE( ); PSA_DONE( );
mock_teardown( ); mock_teardown( );
@ -185,7 +203,7 @@ exit:
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mock_export( ) void mock_export( int mock_export_return_value, int expected_result )
{ {
psa_drv_se_t driver; psa_drv_se_t driver;
psa_drv_se_key_management_t key_management; psa_drv_se_key_management_t key_management;
@ -197,6 +215,7 @@ void mock_export( )
uint8_t exported[sizeof( key_material )]; uint8_t exported[sizeof( key_material )];
size_t exported_length; size_t exported_length;
mock_export_data.return_value = mock_export_return_value;
memset( &driver, 0, sizeof( driver ) ); memset( &driver, 0, sizeof( driver ) );
memset( &key_management, 0, sizeof( key_management ) ); memset( &key_management, 0, sizeof( key_management ) );
driver.hal_version = PSA_DRV_SE_HAL_VERSION; driver.hal_version = PSA_DRV_SE_HAL_VERSION;
@ -217,9 +236,9 @@ void mock_export( )
key_material, sizeof( key_material ), key_material, sizeof( key_material ),
&handle ) ); &handle ) );
PSA_ASSERT( psa_export_key( handle, TEST_ASSERT( psa_export_key( handle,
exported, sizeof( exported ), exported, sizeof( exported ),
&exported_length ) ); &exported_length ) == expected_result );
TEST_ASSERT( mock_export_data.called == 1 ); TEST_ASSERT( mock_export_data.called == 1 );
@ -234,7 +253,9 @@ exit:
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mock_generate( ) void mock_generate( int mock_alloc_return_value,
int mock_generate_return_value,
int expected_result )
{ {
psa_drv_se_t driver; psa_drv_se_t driver;
psa_drv_se_key_management_t key_management; psa_drv_se_key_management_t key_management;
@ -243,6 +264,8 @@ void mock_generate( )
psa_key_handle_t handle = 0; psa_key_handle_t handle = 0;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mock_allocate_data.return_value = mock_alloc_return_value;
mock_generate_data.return_value = mock_generate_return_value;
memset( &driver, 0, sizeof( driver ) ); memset( &driver, 0, sizeof( driver ) );
memset( &key_management, 0, sizeof( key_management ) ); memset( &key_management, 0, sizeof( key_management ) );
driver.hal_version = PSA_DRV_SE_HAL_VERSION; driver.hal_version = PSA_DRV_SE_HAL_VERSION;
@ -258,9 +281,18 @@ void mock_generate( )
psa_set_key_lifetime( &attributes, lifetime ); psa_set_key_lifetime( &attributes, lifetime );
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
PSA_ASSERT( psa_generate_key( &attributes, &handle ) ); TEST_ASSERT( psa_generate_key( &attributes, &handle ) == expected_result );
TEST_ASSERT( mock_allocate_data.called == 1 ); TEST_ASSERT( mock_allocate_data.called == 1 );
TEST_ASSERT( mock_generate_data.called == 1 ); TEST_ASSERT( mock_generate_data.called ==
( mock_alloc_return_value == PSA_SUCCESS? 1 : 0 ) );
TEST_ASSERT( mock_generate_data.attributes.core.id ==
( mock_alloc_return_value == PSA_SUCCESS? id : 0 ) );
TEST_ASSERT( mock_generate_data.attributes.core.lifetime ==
( mock_alloc_return_value == PSA_SUCCESS? lifetime : 0 ) );
TEST_ASSERT( mock_generate_data.attributes.core.policy.usage ==
( mock_alloc_return_value == PSA_SUCCESS? PSA_KEY_USAGE_EXPORT : 0 ) );
TEST_ASSERT( mock_generate_data.attributes.core.type ==
( mock_alloc_return_value == PSA_SUCCESS? PSA_KEY_TYPE_RAW_DATA : 0 ) );
if( expected_result == PSA_SUCCESS ) if( expected_result == PSA_SUCCESS )
{ {