Tests for PSA ITS over stdio files

This commit is contained in:
Gilles Peskine 2019-02-24 12:26:08 +01:00
parent bc1f272750
commit b0c642abae
2 changed files with 74 additions and 69 deletions

View file

@ -35,31 +35,31 @@ Removed file
nonexistent:0:1 nonexistent:0:1
Get 0 bytes of 10 at 10 Get 0 bytes of 10 at 10
get_at:0:"40414243444546474849":10:0:PSA_ITS_SUCCESS get_at:0:"40414243444546474849":10:0:PSA_SUCCESS
Get 1 byte of 10 at 9 Get 1 byte of 10 at 9
get_at:0:"40414243444546474849":9:1:PSA_ITS_SUCCESS get_at:0:"40414243444546474849":9:1:PSA_SUCCESS
Get 0 bytes of 10 at 0 Get 0 bytes of 10 at 0
get_at:0:"40414243444546474849":0:0:PSA_ITS_SUCCESS get_at:0:"40414243444546474849":0:0:PSA_SUCCESS
Get 1 byte of 10 at 0 Get 1 byte of 10 at 0
get_at:0:"40414243444546474849":1:0:PSA_ITS_SUCCESS get_at:0:"40414243444546474849":1:0:PSA_SUCCESS
Get 2 bytes of 10 at 1 Get 2 bytes of 10 at 1
get_at:0:"40414243444546474849":1:2:PSA_ITS_SUCCESS get_at:0:"40414243444546474849":1:2:PSA_SUCCESS
Get 1 byte of 10 at 10: out of range Get 1 byte of 10 at 10: out of range
get_at:0:"40414243444546474849":10:1:PSA_ITS_ERROR_INCORRECT_SIZE get_at:0:"40414243444546474849":10:1:PSA_ERROR_INVALID_ARGUMENT
Get 1 byte of 10 at 11: out of range Get 1 byte of 10 at 11: out of range
get_at:0:"40414243444546474849":11:1:PSA_ITS_ERROR_INCORRECT_SIZE get_at:0:"40414243444546474849":11:1:PSA_ERROR_INVALID_ARGUMENT
Get 0 bytes of 10 at 11: out of range Get 0 bytes of 10 at 11: out of range
get_at:0:"40414243444546474849":11:1:PSA_ITS_ERROR_INCORRECT_SIZE get_at:0:"40414243444546474849":11:1:PSA_ERROR_INVALID_ARGUMENT
Get -1 byte of 10 at 10: out of range Get -1 byte of 10 at 10: out of range
get_at:0:"40414243444546474849":10:-1:PSA_ITS_ERROR_INCORRECT_SIZE get_at:0:"40414243444546474849":10:-1:PSA_ERROR_INVALID_ARGUMENT
Get 1 byte of 10 at -1: out of range Get 1 byte of 10 at -1: out of range
get_at:0:"40414243444546474849":-1:1:PSA_ITS_ERROR_INCORRECT_SIZE get_at:0:"40414243444546474849":-1:1:PSA_ERROR_INVALID_ARGUMENT

View file

@ -1,53 +1,58 @@
/* BEGIN_HEADER */ /* BEGIN_HEADER */
#include "../library/psa_its_file.h" #include "../library/psa_crypto_its.h"
/* Internal definitions of the implementation, copied for the sake of /* Internal definitions of the implementation, copied for the sake of
* some of the tests and of the cleanup code. */ * some of the tests and of the cleanup code. */
#define PSA_ITS_STORAGE_PREFIX "" #define PSA_ITS_STORAGE_PREFIX ""
#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08lx.psa_its" #define PSA_ITS_STORAGE_FILENAME_PATTERN "%08lx%08lx"
#define PSA_ITS_STORAGE_SUFFIX ".psa_its"
#define PSA_ITS_STORAGE_FILENAME_LENGTH \ #define PSA_ITS_STORAGE_FILENAME_LENGTH \
( sizeof( PSA_ITS_STORAGE_PREFIX ) + 16 ) ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
#define PSA_ITS_STORAGE_TEMP PSA_ITS_STORAGE_PREFIX "tempfile.psa_its" 16 + /*UID (64-bit number in hex)*/ \
sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
static void psa_its_fill_filename( uint32_t uid, char *filename ) 1 /*terminating null byte*/ )
#define PSA_ITS_STORAGE_TEMP \
PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
{ {
snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH, /* Break up the UID into two 32-bit pieces so as not to rely on
"%s" PSA_ITS_STORAGE_FILENAME_PATTERN, * long long support in snprintf. */
PSA_ITS_STORAGE_PREFIX, (unsigned long) uid ); mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
"%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
PSA_ITS_STORAGE_PREFIX,
(unsigned long) ( uid >> 32 ),
(unsigned long) ( uid & 0xffffffff ),
PSA_ITS_STORAGE_SUFFIX );
} }
#define MAX( m, n ) ( ( m ) > ( n ) ? ( m ) : ( n ) )
#define ITS_ASSERT( expr ) \
TEST_ASSERT( ( expr ) == PSA_ITS_SUCCESS )
/* Maximum uid used by the test, recorded so that cleanup() can delete /* Maximum uid used by the test, recorded so that cleanup() can delete
* all files. 0xffffffff is excluded. */ * all files. 0xffffffffffffffff is always cleaned up, so it does not
static uint32_t uid_max = 0; * need to and should not be taken into account for uid_max. */
static psa_storage_uid_t uid_max = 0;
static void cleanup( void ) static void cleanup( void )
{ {
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
uint32_t uid; psa_storage_uid_t uid;
for( uid = 0; uid < uid_max; uid++ ) for( uid = 0; uid < uid_max; uid++ )
{ {
psa_its_fill_filename( uid, filename ); psa_its_fill_filename( uid, filename );
remove( filename ); remove( filename );
} }
psa_its_fill_filename( 0xffffffff, filename ); psa_its_fill_filename( (psa_storage_uid_t)( -1 ), filename );
remove( filename ); remove( filename );
remove( PSA_ITS_STORAGE_TEMP ); remove( PSA_ITS_STORAGE_TEMP );
uid_max = 0; uid_max = 0;
} }
static psa_its_status_t psa_its_set_wrap( uint32_t uid, static psa_status_t psa_its_set_wrap( psa_storage_uid_t uid,
uint32_t data_length, uint32_t data_length,
const void *p_data, const void *p_data,
psa_its_create_flags_t create_flags ) psa_storage_create_flags_t create_flags )
{ {
if( uid_max != 0xffffffff && uid_max < uid ) if( uid_max != (psa_storage_uid_t)( -1 ) && uid_max < uid )
uid_max = uid; uid_max = uid;
return psa_its_set( uid, data_length, p_data, create_flags ); return( psa_its_set( uid, data_length, p_data, create_flags ) );
} }
/* END_HEADER */ /* END_HEADER */
@ -60,22 +65,22 @@ static psa_its_status_t psa_its_set_wrap( uint32_t uid,
/* BEGIN_CASE */ /* BEGIN_CASE */
void set_get_remove( int uid_arg, int flags_arg, data_t *data ) void set_get_remove( int uid_arg, int flags_arg, data_t *data )
{ {
uint32_t uid = uid_arg; psa_storage_uid_t uid = uid_arg;
uint32_t flags = flags_arg; uint32_t flags = flags_arg;
struct psa_its_info_t info; struct psa_storage_info_t info;
unsigned char *buffer = NULL; unsigned char *buffer = NULL;
ASSERT_ALLOC( buffer, data->len ); ASSERT_ALLOC( buffer, data->len );
ITS_ASSERT( psa_its_set_wrap( uid, data->len, data->x, flags ) ); PSA_ASSERT( psa_its_set_wrap( uid, data->len, data->x, flags ) );
ITS_ASSERT( psa_its_get_info( uid, &info ) ); PSA_ASSERT( psa_its_get_info( uid, &info ) );
TEST_ASSERT( info.size == data->len ); TEST_ASSERT( info.size == data->len );
TEST_ASSERT( info.flags == flags ); TEST_ASSERT( info.flags == flags );
ITS_ASSERT( psa_its_get( uid, 0, data->len, buffer ) ); PSA_ASSERT( psa_its_get( uid, 0, data->len, buffer ) );
ASSERT_COMPARE( data->x, data->len, buffer, data->len ); ASSERT_COMPARE( data->x, data->len, buffer, data->len );
ITS_ASSERT( psa_its_remove( uid ) ); PSA_ASSERT( psa_its_remove( uid ) );
exit: exit:
mbedtls_free( buffer ); mbedtls_free( buffer );
@ -88,29 +93,29 @@ void set_overwrite( int uid_arg,
int flags1_arg, data_t *data1, int flags1_arg, data_t *data1,
int flags2_arg, data_t *data2 ) int flags2_arg, data_t *data2 )
{ {
uint32_t uid = uid_arg; psa_storage_uid_t uid = uid_arg;
uint32_t flags1 = flags1_arg; uint32_t flags1 = flags1_arg;
uint32_t flags2 = flags2_arg; uint32_t flags2 = flags2_arg;
struct psa_its_info_t info; struct psa_storage_info_t info;
unsigned char *buffer = NULL; unsigned char *buffer = NULL;
ASSERT_ALLOC( buffer, MAX( data1->len, data2->len ) ); ASSERT_ALLOC( buffer, MAX( data1->len, data2->len ) );
ITS_ASSERT( psa_its_set_wrap( uid, data1->len, data1->x, flags1 ) ); PSA_ASSERT( psa_its_set_wrap( uid, data1->len, data1->x, flags1 ) );
ITS_ASSERT( psa_its_get_info( uid, &info ) ); PSA_ASSERT( psa_its_get_info( uid, &info ) );
TEST_ASSERT( info.size == data1->len ); TEST_ASSERT( info.size == data1->len );
TEST_ASSERT( info.flags == flags1 ); TEST_ASSERT( info.flags == flags1 );
ITS_ASSERT( psa_its_get( uid, 0, data1->len, buffer ) ); PSA_ASSERT( psa_its_get( uid, 0, data1->len, buffer ) );
ASSERT_COMPARE( data1->x, data1->len, buffer, data1->len ); ASSERT_COMPARE( data1->x, data1->len, buffer, data1->len );
ITS_ASSERT( psa_its_set_wrap( uid, data2->len, data2->x, flags2 ) ); PSA_ASSERT( psa_its_set_wrap( uid, data2->len, data2->x, flags2 ) );
ITS_ASSERT( psa_its_get_info( uid, &info ) ); PSA_ASSERT( psa_its_get_info( uid, &info ) );
TEST_ASSERT( info.size == data2->len ); TEST_ASSERT( info.size == data2->len );
TEST_ASSERT( info.flags == flags2 ); TEST_ASSERT( info.flags == flags2 );
ITS_ASSERT( psa_its_get( uid, 0, data2->len, buffer ) ); PSA_ASSERT( psa_its_get( uid, 0, data2->len, buffer ) );
ASSERT_COMPARE( data2->x, data2->len, buffer, data2->len ); ASSERT_COMPARE( data2->x, data2->len, buffer, data2->len );
ITS_ASSERT( psa_its_remove( uid ) ); PSA_ASSERT( psa_its_remove( uid ) );
exit: exit:
mbedtls_free( buffer ); mbedtls_free( buffer );
@ -121,8 +126,8 @@ exit:
/* BEGIN_CASE */ /* BEGIN_CASE */
void set_multiple( int first_id, int count ) void set_multiple( int first_id, int count )
{ {
uint32_t uid0 = first_id; psa_storage_uid_t uid0 = first_id;
uint32_t uid; psa_storage_uid_t uid;
char stored[40]; char stored[40];
char retrieved[40]; char retrieved[40];
@ -131,19 +136,19 @@ void set_multiple( int first_id, int count )
{ {
mbedtls_snprintf( stored, sizeof( stored ), mbedtls_snprintf( stored, sizeof( stored ),
"Content of file 0x%08lx", (unsigned long) uid ); "Content of file 0x%08lx", (unsigned long) uid );
ITS_ASSERT( psa_its_set_wrap( uid, sizeof( stored ), stored, 0 ) ); PSA_ASSERT( psa_its_set_wrap( uid, sizeof( stored ), stored, 0 ) );
} }
for( uid = uid0; uid < uid0 + count; uid++ ) for( uid = uid0; uid < uid0 + count; uid++ )
{ {
mbedtls_snprintf( stored, sizeof( stored ), mbedtls_snprintf( stored, sizeof( stored ),
"Content of file 0x%08lx", (unsigned long) uid ); "Content of file 0x%08lx", (unsigned long) uid );
ITS_ASSERT( psa_its_get( uid, 0, sizeof( stored ), retrieved ) ); PSA_ASSERT( psa_its_get( uid, 0, sizeof( stored ), retrieved ) );
ASSERT_COMPARE( retrieved, sizeof( stored ), ASSERT_COMPARE( retrieved, sizeof( stored ),
stored, sizeof( stored ) ); stored, sizeof( stored ) );
ITS_ASSERT( psa_its_remove( uid ) ); PSA_ASSERT( psa_its_remove( uid ) );
TEST_ASSERT( psa_its_get( uid, 0, 0, NULL ) == TEST_ASSERT( psa_its_get( uid, 0, 0, NULL ) ==
PSA_ITS_ERROR_KEY_NOT_FOUND ); PSA_ERROR_DOES_NOT_EXIST );
} }
exit: exit:
@ -154,20 +159,20 @@ exit:
/* BEGIN_CASE */ /* BEGIN_CASE */
void nonexistent( int uid_arg, int create_and_remove ) void nonexistent( int uid_arg, int create_and_remove )
{ {
uint32_t uid = uid_arg; psa_storage_uid_t uid = uid_arg;
struct psa_its_info_t info; struct psa_storage_info_t info;
if( create_and_remove ) if( create_and_remove )
{ {
ITS_ASSERT( psa_its_set_wrap( uid, 0, NULL, 0 ) ); PSA_ASSERT( psa_its_set_wrap( uid, 0, NULL, 0 ) );
ITS_ASSERT( psa_its_remove( uid ) ); PSA_ASSERT( psa_its_remove( uid ) );
} }
TEST_ASSERT( psa_its_remove( uid ) == PSA_ITS_ERROR_KEY_NOT_FOUND ); TEST_ASSERT( psa_its_remove( uid ) == PSA_ERROR_DOES_NOT_EXIST );
TEST_ASSERT( psa_its_get_info( uid, &info ) == TEST_ASSERT( psa_its_get_info( uid, &info ) ==
PSA_ITS_ERROR_KEY_NOT_FOUND ); PSA_ERROR_DOES_NOT_EXIST );
TEST_ASSERT( psa_its_get( uid, 0, 0, NULL ) == TEST_ASSERT( psa_its_get( uid, 0, 0, NULL ) ==
PSA_ITS_ERROR_KEY_NOT_FOUND ); PSA_ERROR_DOES_NOT_EXIST );
exit: exit:
cleanup( ); cleanup( );
@ -179,9 +184,9 @@ void get_at( int uid_arg, data_t *data,
int offset, int length_arg, int offset, int length_arg,
int expected_status ) int expected_status )
{ {
uint32_t uid = uid_arg; psa_storage_uid_t uid = uid_arg;
unsigned char *buffer = NULL; unsigned char *buffer = NULL;
psa_its_status_t ist; psa_status_t status;
size_t length = length_arg >= 0 ? length_arg : 0; size_t length = length_arg >= 0 ? length_arg : 0;
unsigned char *trailer; unsigned char *trailer;
size_t i; size_t i;
@ -190,16 +195,16 @@ void get_at( int uid_arg, data_t *data,
trailer = buffer + length; trailer = buffer + length;
memset( trailer, '-', 16 ); memset( trailer, '-', 16 );
ITS_ASSERT( psa_its_set_wrap( uid, data->len, data->x, 0 ) ); PSA_ASSERT( psa_its_set_wrap( uid, data->len, data->x, 0 ) );
ist = psa_its_get( uid, offset, length_arg, buffer ); status = psa_its_get( uid, offset, length_arg, buffer );
TEST_ASSERT( ist == (psa_its_status_t) expected_status ); TEST_ASSERT( status == (psa_status_t) expected_status );
if( ist == PSA_ITS_SUCCESS ) if( status == PSA_SUCCESS )
ASSERT_COMPARE( data->x + offset, length, ASSERT_COMPARE( data->x + offset, length,
buffer, length ); buffer, length );
for( i = 0; i < 16; i++ ) for( i = 0; i < 16; i++ )
TEST_ASSERT( trailer[i] == '-' ); TEST_ASSERT( trailer[i] == '-' );
ITS_ASSERT( psa_its_remove( uid ) ); PSA_ASSERT( psa_its_remove( uid ) );
exit: exit:
mbedtls_free( buffer ); mbedtls_free( buffer );