Skip to content

Commit b8cde4e

Browse files
Consolidate invalid-handle tests
Consolidate the invalid-handle tests from test_suite_psa_crypto and test_suite_psa_crypto_slot_management. Start with the code in test_suite_psa_crypto_slot_management and adapt it to test one invalid handle value per run of the test function.
1 parent 04129a0 commit b8cde4e

File tree

4 files changed

+60
-67
lines changed

4 files changed

+60
-67
lines changed

tests/suites/test_suite_psa_crypto.data

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,6 @@ persistence_attributes:0x1234:3:0x1235:0x1235:3
2222
PSA key attributes: slot number
2323
slot_number_attribute:
2424

25-
psa_destroy_key(0)
26-
destroy_invalid:0:PSA_SUCCESS
27-
28-
psa_destroy_key(invalid)
29-
destroy_invalid:1:PSA_ERROR_INVALID_HANDLE
30-
31-
psa_destroy_key(huge)
32-
destroy_invalid:-1:PSA_ERROR_INVALID_HANDLE
33-
34-
psa_close_key(0)
35-
close_invalid:0:PSA_SUCCESS
36-
37-
psa_close_key(invalid)
38-
close_invalid:1:PSA_ERROR_INVALID_HANDLE
39-
40-
psa_close_key(huge)
41-
close_invalid:-1:PSA_ERROR_INVALID_HANDLE
42-
4325
PSA import/export raw: 1 bytes
4426
import_export:"2a":PSA_KEY_TYPE_RAW_DATA:PSA_KEY_USAGE_EXPORT:0:8:0:PSA_SUCCESS:1
4527

tests/suites/test_suite_psa_crypto.function

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,34 +1268,6 @@ void slot_number_attribute( )
12681268
}
12691269
/* END_CASE */
12701270

1271-
/* BEGIN_CASE */
1272-
void destroy_invalid( int handle_arg, int expected_status_arg )
1273-
{
1274-
psa_key_handle_t handle = handle_arg;
1275-
psa_status_t expected_status = expected_status_arg;
1276-
1277-
PSA_ASSERT( psa_crypto_init( ) );
1278-
TEST_EQUAL( psa_destroy_key( handle ), expected_status );
1279-
1280-
exit:
1281-
PSA_DONE( );
1282-
}
1283-
/* END_CASE */
1284-
1285-
/* BEGIN_CASE */
1286-
void close_invalid( int handle_arg, int expected_status_arg )
1287-
{
1288-
psa_key_handle_t handle = handle_arg;
1289-
psa_status_t expected_status = expected_status_arg;
1290-
1291-
PSA_ASSERT( psa_crypto_init( ) );
1292-
TEST_EQUAL( psa_close_key( handle ), expected_status );
1293-
1294-
exit:
1295-
PSA_DONE( );
1296-
}
1297-
/* END_CASE */
1298-
12991271
/* BEGIN_CASE */
13001272
void import_with_policy( int type_arg,
13011273
int usage_arg, int alg_arg,

tests/suites/test_suite_psa_crypto_slot_management.data

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,17 @@ Copy persistent to same
148148
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
149149
copy_to_occupied:PSA_KEY_LIFETIME_PERSISTENT:1:PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY:PSA_ALG_CTR:PSA_KEY_TYPE_AES:"404142434445464748494a4b4c4d4e4f":PSA_KEY_LIFETIME_PERSISTENT:1:PSA_KEY_USAGE_EXPORT:PSA_ALG_CTR:PSA_KEY_TYPE_AES:"404142434445464748494a4b4c4d4e4f"
150150

151-
Close/destroy invalid handle
152-
invalid_handle:
151+
invalid handle: 0
152+
invalid_handle:INVALID_HANDLE_0:PSA_SUCCESS:PSA_ERROR_INVALID_HANDLE
153+
154+
invalid handle: never opened
155+
invalid_handle:INVALID_HANDLE_UNOPENED:PSA_ERROR_INVALID_HANDLE:PSA_ERROR_INVALID_HANDLE
156+
157+
invalid handle: already closed
158+
invalid_handle:INVALID_HANDLE_CLOSED:PSA_ERROR_INVALID_HANDLE:PSA_ERROR_INVALID_HANDLE
159+
160+
invalid handle: huge
161+
invalid_handle:INVALID_HANDLE_HUGE:PSA_ERROR_INVALID_HANDLE:PSA_ERROR_INVALID_HANDLE
153162

154163
Open many transient handles
155164
many_transient_handles:42

tests/suites/test_suite_psa_crypto_slot_management.function

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ typedef enum
2020
CLOSE_AFTER,
2121
} reopen_policy_t;
2222

23+
typedef enum
24+
{
25+
INVALID_HANDLE_0,
26+
INVALID_HANDLE_UNOPENED,
27+
INVALID_HANDLE_CLOSED,
28+
INVALID_HANDLE_HUGE,
29+
} invalid_handle_construction_t;
30+
2331
/* All test functions that create persistent keys must call
2432
* `TEST_USES_KEY_ID( key_id )` before creating a persistent key with this
2533
* identifier, and must call psa_purge_key_storage() in their cleanup
@@ -625,9 +633,13 @@ exit:
625633
/* END_CASE */
626634

627635
/* BEGIN_CASE */
628-
void invalid_handle( )
636+
void invalid_handle( int handle_construction,
637+
int close_status_arg, int usage_status_arg )
629638
{
630-
psa_key_handle_t handle1 = 0;
639+
psa_key_handle_t valid_handle = 0;
640+
psa_key_handle_t invalid_handle = 0;
641+
psa_status_t close_status = close_status_arg;
642+
psa_status_t usage_status = usage_status_arg;
631643
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
632644
uint8_t material[1] = "a";
633645

@@ -639,32 +651,50 @@ void invalid_handle( )
639651
psa_set_key_algorithm( &attributes, 0 );
640652
PSA_ASSERT( psa_import_key( &attributes,
641653
material, sizeof( material ),
642-
&handle1 ) );
643-
TEST_ASSERT( handle1 != 0 );
654+
&valid_handle ) );
655+
TEST_ASSERT( valid_handle != 0 );
644656

645-
/* Attempt to close and destroy some invalid handles. */
646-
if( handle1 - 1 != 0 )
647-
{
648-
TEST_EQUAL( psa_close_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
649-
TEST_EQUAL( psa_destroy_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
650-
}
651-
if( handle1 + 1 != 0 )
657+
/* Construct an invalid handle as specified in the test case data. */
658+
switch( handle_construction )
652659
{
653-
TEST_EQUAL( psa_close_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
654-
TEST_EQUAL( psa_destroy_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
660+
case INVALID_HANDLE_0:
661+
invalid_handle = 0;
662+
break;
663+
case INVALID_HANDLE_UNOPENED:
664+
/* We can't easily construct a handle that's never been opened
665+
* without knowing how the implementation constructs handle
666+
* values. The current test code assumes that valid handles
667+
* are in a range between 1 and some maximum. */
668+
if( valid_handle == 1 )
669+
invalid_handle = 2;
670+
else
671+
invalid_handle = valid_handle - 1;
672+
break;
673+
case INVALID_HANDLE_CLOSED:
674+
PSA_ASSERT( psa_import_key( &attributes,
675+
material, sizeof( material ),
676+
&invalid_handle ) );
677+
PSA_ASSERT( psa_destroy_key( invalid_handle ) );
678+
break;
679+
case INVALID_HANDLE_HUGE:
680+
invalid_handle = (psa_key_handle_t) ( -1 );
681+
break;
682+
default:
683+
TEST_ASSERT( ! "unknown handle construction" );
655684
}
656685

657-
/* 0 is special: it isn't a valid handle, but close/destroy
658-
* succeeds on it. */
659-
TEST_EQUAL( psa_close_key( 0 ), PSA_SUCCESS );
660-
TEST_EQUAL( psa_destroy_key( 0 ), PSA_SUCCESS );
686+
/* Attempt to use the invalid handle. */
687+
TEST_EQUAL( psa_get_key_attributes( invalid_handle, &attributes ),
688+
usage_status );
689+
TEST_EQUAL( psa_close_key( invalid_handle ), close_status );
690+
TEST_EQUAL( psa_destroy_key( invalid_handle ), close_status );
661691

662692
/* After all this, check that the original handle is intact. */
663-
PSA_ASSERT( psa_get_key_attributes( handle1, &attributes ) );
693+
PSA_ASSERT( psa_get_key_attributes( valid_handle, &attributes ) );
664694
TEST_EQUAL( psa_get_key_type( &attributes ), PSA_KEY_TYPE_RAW_DATA );
665695
TEST_EQUAL( psa_get_key_bits( &attributes ),
666696
PSA_BYTES_TO_BITS( sizeof( material ) ) );
667-
PSA_ASSERT( psa_close_key( handle1 ) );
697+
PSA_ASSERT( psa_close_key( valid_handle ) );
668698

669699
exit:
670700
PSA_DONE( );

0 commit comments

Comments
 (0)