From b0c642abae0685e109840c7920016b50eacaf50f Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sun, 24 Feb 2019 12:26:08 +0100 Subject: [PATCH] Tests for PSA ITS over stdio files --- tests/suites/test_suite_psa_its.data | 20 ++-- tests/suites/test_suite_psa_its.function | 123 ++++++++++++----------- 2 files changed, 74 insertions(+), 69 deletions(-) diff --git a/tests/suites/test_suite_psa_its.data b/tests/suites/test_suite_psa_its.data index d6c0e1545..6c4353698 100644 --- a/tests/suites/test_suite_psa_its.data +++ b/tests/suites/test_suite_psa_its.data @@ -35,31 +35,31 @@ Removed file nonexistent:0:1 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_at:0:"40414243444546474849":9:1:PSA_ITS_SUCCESS +get_at:0:"40414243444546474849":9:1:PSA_SUCCESS 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_at:0:"40414243444546474849":1:0:PSA_ITS_SUCCESS +get_at:0:"40414243444546474849":1:0:PSA_SUCCESS 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_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_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_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_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_at:0:"40414243444546474849":-1:1:PSA_ITS_ERROR_INCORRECT_SIZE +get_at:0:"40414243444546474849":-1:1:PSA_ERROR_INVALID_ARGUMENT diff --git a/tests/suites/test_suite_psa_its.function b/tests/suites/test_suite_psa_its.function index 70d4578bd..867f64f6b 100644 --- a/tests/suites/test_suite_psa_its.function +++ b/tests/suites/test_suite_psa_its.function @@ -1,53 +1,58 @@ /* BEGIN_HEADER */ -#include "../library/psa_its_file.h" +#include "../library/psa_crypto_its.h" /* Internal definitions of the implementation, copied for the sake of * some of the tests and of the cleanup code. */ #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 \ - ( sizeof( PSA_ITS_STORAGE_PREFIX ) + 16 ) -#define PSA_ITS_STORAGE_TEMP PSA_ITS_STORAGE_PREFIX "tempfile.psa_its" - -static void psa_its_fill_filename( uint32_t uid, char *filename ) + ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \ + 16 + /*UID (64-bit number in hex)*/ \ + sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \ + 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, - "%s" PSA_ITS_STORAGE_FILENAME_PATTERN, - PSA_ITS_STORAGE_PREFIX, (unsigned long) uid ); + /* Break up the UID into two 32-bit pieces so as not to rely on + * long long support in snprintf. */ + 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 - * all files. 0xffffffff is excluded. */ -static uint32_t uid_max = 0; + * all files. 0xffffffffffffffff is always cleaned up, so it does not + * need to and should not be taken into account for uid_max. */ +static psa_storage_uid_t uid_max = 0; static void cleanup( void ) { char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; - uint32_t uid; + psa_storage_uid_t uid; for( uid = 0; uid < uid_max; uid++ ) { psa_its_fill_filename( uid, filename ); remove( filename ); } - psa_its_fill_filename( 0xffffffff, filename ); + psa_its_fill_filename( (psa_storage_uid_t)( -1 ), filename ); remove( filename ); remove( PSA_ITS_STORAGE_TEMP ); uid_max = 0; } -static psa_its_status_t psa_its_set_wrap( uint32_t uid, - uint32_t data_length, - const void *p_data, - psa_its_create_flags_t create_flags ) +static psa_status_t psa_its_set_wrap( psa_storage_uid_t uid, + uint32_t data_length, + const void *p_data, + 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; - return psa_its_set( uid, data_length, p_data, create_flags ); + return( psa_its_set( uid, data_length, p_data, create_flags ) ); } /* END_HEADER */ @@ -60,22 +65,22 @@ static psa_its_status_t psa_its_set_wrap( uint32_t uid, /* BEGIN_CASE */ 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; - struct psa_its_info_t info; + struct psa_storage_info_t info; unsigned char *buffer = NULL; 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.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 ); - ITS_ASSERT( psa_its_remove( uid ) ); + PSA_ASSERT( psa_its_remove( uid ) ); exit: mbedtls_free( buffer ); @@ -88,29 +93,29 @@ void set_overwrite( int uid_arg, int flags1_arg, data_t *data1, 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 flags2 = flags2_arg; - struct psa_its_info_t info; + struct psa_storage_info_t info; unsigned char *buffer = NULL; ASSERT_ALLOC( buffer, MAX( data1->len, data2->len ) ); - ITS_ASSERT( psa_its_set_wrap( uid, data1->len, data1->x, flags1 ) ); - ITS_ASSERT( psa_its_get_info( uid, &info ) ); + PSA_ASSERT( psa_its_set_wrap( uid, data1->len, data1->x, flags1 ) ); + PSA_ASSERT( psa_its_get_info( uid, &info ) ); TEST_ASSERT( info.size == data1->len ); 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 ); - ITS_ASSERT( psa_its_set_wrap( uid, data2->len, data2->x, flags2 ) ); - ITS_ASSERT( psa_its_get_info( uid, &info ) ); + PSA_ASSERT( psa_its_set_wrap( uid, data2->len, data2->x, flags2 ) ); + PSA_ASSERT( psa_its_get_info( uid, &info ) ); TEST_ASSERT( info.size == data2->len ); 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 ); - ITS_ASSERT( psa_its_remove( uid ) ); + PSA_ASSERT( psa_its_remove( uid ) ); exit: mbedtls_free( buffer ); @@ -121,8 +126,8 @@ exit: /* BEGIN_CASE */ void set_multiple( int first_id, int count ) { - uint32_t uid0 = first_id; - uint32_t uid; + psa_storage_uid_t uid0 = first_id; + psa_storage_uid_t uid; char stored[40]; char retrieved[40]; @@ -131,19 +136,19 @@ void set_multiple( int first_id, int count ) { mbedtls_snprintf( stored, sizeof( stored ), "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++ ) { mbedtls_snprintf( stored, sizeof( stored ), "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 ), stored, sizeof( stored ) ); - ITS_ASSERT( psa_its_remove( uid ) ); + PSA_ASSERT( psa_its_remove( uid ) ); TEST_ASSERT( psa_its_get( uid, 0, 0, NULL ) == - PSA_ITS_ERROR_KEY_NOT_FOUND ); + PSA_ERROR_DOES_NOT_EXIST ); } exit: @@ -154,20 +159,20 @@ exit: /* BEGIN_CASE */ void nonexistent( int uid_arg, int create_and_remove ) { - uint32_t uid = uid_arg; - struct psa_its_info_t info; + psa_storage_uid_t uid = uid_arg; + struct psa_storage_info_t info; if( create_and_remove ) { - ITS_ASSERT( psa_its_set_wrap( uid, 0, NULL, 0 ) ); - ITS_ASSERT( psa_its_remove( uid ) ); + PSA_ASSERT( psa_its_set_wrap( uid, 0, NULL, 0 ) ); + 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 ) == - PSA_ITS_ERROR_KEY_NOT_FOUND ); + PSA_ERROR_DOES_NOT_EXIST ); TEST_ASSERT( psa_its_get( uid, 0, 0, NULL ) == - PSA_ITS_ERROR_KEY_NOT_FOUND ); + PSA_ERROR_DOES_NOT_EXIST ); exit: cleanup( ); @@ -179,9 +184,9 @@ void get_at( int uid_arg, data_t *data, int offset, int length_arg, int expected_status ) { - uint32_t uid = uid_arg; + psa_storage_uid_t uid = uid_arg; unsigned char *buffer = NULL; - psa_its_status_t ist; + psa_status_t status; size_t length = length_arg >= 0 ? length_arg : 0; unsigned char *trailer; size_t i; @@ -190,16 +195,16 @@ void get_at( int uid_arg, data_t *data, trailer = buffer + length; 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 ); - TEST_ASSERT( ist == (psa_its_status_t) expected_status ); - if( ist == PSA_ITS_SUCCESS ) + status = psa_its_get( uid, offset, length_arg, buffer ); + TEST_ASSERT( status == (psa_status_t) expected_status ); + if( status == PSA_SUCCESS ) ASSERT_COMPARE( data->x + offset, length, buffer, length ); for( i = 0; i < 16; i++ ) TEST_ASSERT( trailer[i] == '-' ); - ITS_ASSERT( psa_its_remove( uid ) ); + PSA_ASSERT( psa_its_remove( uid ) ); exit: mbedtls_free( buffer );