Slot management tests: more robust storage purge
Record what key ids have been used in a test case and purge them. The
cleanup code no longer requires the key identifiers used in the tests
to be in a certain small range.
diff --git a/tests/suites/test_suite_psa_crypto_slot_management.function b/tests/suites/test_suite_psa_crypto_slot_management.function
index 03b7197..267353e 100644
--- a/tests/suites/test_suite_psa_crypto_slot_management.function
+++ b/tests/suites/test_suite_psa_crypto_slot_management.function
@@ -23,31 +23,47 @@
} reopen_policy_t;
/* All test functions that create persistent keys must call
- * `TEST_MAX_KEY_ID( key_id )` before creating a persistent key with this
+ * `TEST_USES_KEY_ID( key_id )` before creating a persistent key with this
* identifier, and must call psa_purge_key_storage() in their cleanup
* code. */
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
-/* There is no API to purge all keys. For this test suite, require that
- * all key IDs be less than a certain maximum, or a well-known value
- * which corresponds to a file that does not contain a key. */
-#define MAX_KEY_ID_FOR_TEST 32
-#define KEY_ID_IS_WELL_KNOWN( key_id ) \
- ( ( key_id ) == PSA_CRYPTO_ITS_RANDOM_SEED_UID )
-#define TEST_MAX_KEY_ID( key_id ) \
- TEST_ASSERT( ( key_id ) <= MAX_KEY_ID_FOR_TEST || \
- KEY_ID_IS_WELL_KNOWN( key_id ) )
-void psa_purge_key_storage( void )
+static psa_key_id_t key_ids_used_in_test[9];
+static size_t num_key_ids_used;
+
+/* Record a key id as potentially used in a test case. */
+static int test_uses_key_id( psa_key_id_t key_id )
{
- psa_key_id_t i;
- /* The tests may have potentially created key ids from 1 to
- * MAX_KEY_ID_FOR_TEST. In addition, run the destroy function on key id
- * 0, which file-based storage uses as a temporary file. */
- for( i = 0; i <= MAX_KEY_ID_FOR_TEST; i++ )
- psa_destroy_persistent_key( i );
+ size_t i;
+ if( key_id > PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
+ {
+ /* Don't touch key id values that designate non-key files. */
+ return( 1 );
+ }
+ for( i = 0; i < num_key_ids_used ; i++ )
+ {
+ if( key_id == key_ids_used_in_test[i] )
+ return( 1 );
+ }
+ if( num_key_ids_used == ARRAY_LENGTH( key_ids_used_in_test ) )
+ return( 0 );
+ key_ids_used_in_test[num_key_ids_used] = key_id;
+ ++num_key_ids_used;
+ return( 1 );
+}
+#define TEST_USES_KEY_ID( key_id ) \
+ TEST_ASSERT( test_uses_key_id( key_id ) )
+
+/* Destroy all key ids that may have been created by the current test case. */
+static void psa_purge_key_storage( void )
+{
+ size_t i;
+ for( i = 0; i < num_key_ids_used; i++ )
+ psa_destroy_persistent_key( key_ids_used_in_test[i] );
+ num_key_ids_used = 0;
}
#else
-#define TEST_MAX_KEY_ID( key_id ) ( (void) ( key_id ) )
+#define TEST_USES_KEY_ID( key_id ) ( (void) ( key_id ) )
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
/* END_HEADER */
@@ -122,7 +138,7 @@
psa_key_handle_t handle = 0;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
- TEST_MAX_KEY_ID( id );
+ TEST_USES_KEY_ID( id );
PSA_ASSERT( psa_crypto_init( ) );
@@ -200,7 +216,7 @@
size_t reexported_length;
reopen_policy_t reopen_policy = reopen_policy_arg;
- TEST_MAX_KEY_ID( id );
+ TEST_USES_KEY_ID( id );
PSA_ASSERT( psa_crypto_init( ) );
@@ -279,7 +295,7 @@
psa_key_handle_t handle = 0xdead;
uint8_t material[1] = {'k'};
- TEST_MAX_KEY_ID( id );
+ TEST_USES_KEY_ID( id );
PSA_ASSERT( psa_crypto_init( ) );
@@ -323,8 +339,8 @@
psa_algorithm_t expected_alg = expected_alg_arg;
uint8_t *export_buffer = NULL;
- TEST_MAX_KEY_ID( source_id );
- TEST_MAX_KEY_ID( target_id );
+ TEST_USES_KEY_ID( source_id );
+ TEST_USES_KEY_ID( target_id );
PSA_ASSERT( psa_crypto_init( ) );
@@ -427,8 +443,8 @@
psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
- TEST_MAX_KEY_ID( source_id );
- TEST_MAX_KEY_ID( target_id );
+ TEST_USES_KEY_ID( source_id );
+ TEST_USES_KEY_ID( target_id );
PSA_ASSERT( psa_crypto_init( ) );