Skip to content

Commit c9a0722

Browse files
authored
Merge pull request #2 from gilles-peskine-arm/psa-test_macros
PSA tests: use a few common test macros
2 parents 1a76f39 + c08fc1d commit c9a0722

9 files changed

+1279
-1381
lines changed

tests/suites/helpers.function

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ typedef struct data_tag
9090
} \
9191
} while( 0 )
9292

93+
/** Evaluate two expressions and fail the test case if they have different
94+
* values.
95+
*
96+
* \param expr1 An expression to evaluate.
97+
* \param expr2 The expected value of \p expr1. This can be any
98+
* expression, but it is typically a constant.
99+
*/
100+
#define TEST_EQUAL( expr1, expr2 ) \
101+
TEST_ASSERT( ( expr1 ) == ( expr2 ) )
102+
103+
/** Evaluate an expression and fail the test case if it returns an error.
104+
*
105+
* \param expr The expression to evaluate. This is typically a call
106+
* to a \c psa_xxx function that returns a value of type
107+
* #psa_status_t.
108+
*/
109+
#define PSA_ASSERT( expr ) TEST_EQUAL( ( expr ), PSA_SUCCESS )
110+
93111
/** Allocate memory dynamically and fail the test case if this fails.
94112
*
95113
* You must set \p pointer to \c NULL before calling this macro and
@@ -150,6 +168,58 @@ typedef struct data_tag
150168
mbedtls_exit( 1 ); \
151169
}
152170

171+
#if defined(__GNUC__)
172+
/* Test if arg and &(arg)[0] have the same type. This is true if arg is
173+
* an array but not if it's a pointer. */
174+
#define IS_ARRAY_NOT_POINTER( arg ) \
175+
( ! __builtin_types_compatible_p( __typeof__( arg ), \
176+
__typeof__( &( arg )[0] ) ) )
177+
#else
178+
/* On platforms where we don't know how to implement this check,
179+
* omit it. Oh well, a non-portable check is better than nothing. */
180+
#define IS_ARRAY_NOT_POINTER( arg ) 1
181+
#endif
182+
183+
/* A compile-time constant with the value 0. If `const_expr` is not a
184+
* compile-time constant with a nonzero value, cause a compile-time error. */
185+
#define STATIC_ASSERT_EXPR( const_expr ) \
186+
( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
187+
/* Return the scalar value `value` (possibly promoted). This is a compile-time
188+
* constant if `value` is. `condition` must be a compile-time constant.
189+
* If `condition` is false, arrange to cause a compile-time error. */
190+
#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
191+
( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
192+
193+
#define ARRAY_LENGTH_UNSAFE( array ) \
194+
( sizeof( array ) / sizeof( *( array ) ) )
195+
/** Return the number of elements of a static or stack array.
196+
*
197+
* \param array A value of array (not pointer) type.
198+
*
199+
* \return The number of elements of the array.
200+
*/
201+
#define ARRAY_LENGTH( array ) \
202+
( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
203+
ARRAY_LENGTH_UNSAFE( array ) ) )
204+
205+
/** Return the smaller of two values.
206+
*
207+
* \param x An integer-valued expression without side effects.
208+
* \param y An integer-valued expression without side effects.
209+
*
210+
* \return The smaller of \p x and \p y.
211+
*/
212+
#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
213+
214+
/** Return the larger of two values.
215+
*
216+
* \param x An integer-valued expression without side effects.
217+
* \param y An integer-valued expression without side effects.
218+
*
219+
* \return The larger of \p x and \p y.
220+
*/
221+
#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
222+
153223
/*
154224
* 32-bit integer manipulation macros (big endian)
155225
*/

tests/suites/test_suite_psa_crypto.function

Lines changed: 995 additions & 1143 deletions
Large diffs are not rendered by default.

tests/suites/test_suite_psa_crypto_entropy.function

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
#include "mbedtls/entropy.h"
77
#include "mbedtls/entropy_poll.h"
88

9-
/* MAX value support macro */
10-
#if !defined(MAX)
11-
#define MAX(a,b) (((a)>(b))?(a):(b))
12-
#endif
13-
149
/* Calculating the minimum allowed entropy size in bytes */
1510
#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
1611

@@ -52,12 +47,12 @@ void validate_entropy_seed_injection( int seed_length_a,
5247
TEST_ASSERT( ( its_status == PSA_ITS_SUCCESS ) ||
5348
( its_status == PSA_ITS_ERROR_KEY_NOT_FOUND ) );
5449
status = mbedtls_psa_inject_entropy( seed, seed_length_a );
55-
TEST_ASSERT( status == expected_status_a );
50+
TEST_EQUAL( status, expected_status_a );
5651
status = mbedtls_psa_inject_entropy( seed, seed_length_b );
57-
TEST_ASSERT( status == expected_status_b );
58-
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
59-
TEST_ASSERT( psa_generate_random( output,
60-
sizeof( output ) ) == PSA_SUCCESS );
52+
TEST_EQUAL( status, expected_status_b );
53+
PSA_ASSERT( psa_crypto_init( ) );
54+
PSA_ASSERT( psa_generate_random( output,
55+
sizeof( output ) ) );
6156
TEST_ASSERT( memcmp( output, zeros, sizeof( output ) ) != 0 );
6257
exit:
6358
mbedtls_free( seed );
@@ -82,19 +77,19 @@ void run_entropy_inject_with_crypto_init( )
8277
TEST_ASSERT( ( its_status == PSA_ITS_SUCCESS ) ||
8378
( its_status == PSA_ITS_ERROR_KEY_NOT_FOUND ) );
8479
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
85-
TEST_ASSERT( status == PSA_SUCCESS );
80+
PSA_ASSERT( status );
8681
its_status = psa_its_remove( PSA_CRYPTO_ITS_RANDOM_SEED_UID );
87-
TEST_ASSERT( its_status == PSA_ITS_SUCCESS );
82+
TEST_EQUAL( its_status, PSA_ITS_SUCCESS );
8883
status = psa_crypto_init( );
89-
TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_ENTROPY );
84+
TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_ENTROPY );
9085
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
91-
TEST_ASSERT( status == PSA_SUCCESS );
86+
PSA_ASSERT( status );
9287
status = psa_crypto_init( );
93-
TEST_ASSERT( status == PSA_SUCCESS );
88+
PSA_ASSERT( status );
9489
mbedtls_psa_crypto_free( );
9590
/* The seed is written by nv_seed callback functions therefore the injection will fail */
9691
status = mbedtls_psa_inject_entropy( seed, sizeof( seed ) );
97-
TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
92+
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
9893
exit:
9994
psa_its_remove( PSA_CRYPTO_ITS_RANDOM_SEED_UID );
10095
mbedtls_psa_crypto_free( );

tests/suites/test_suite_psa_crypto_hash.function

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
* END_DEPENDENCIES
1616
*/
1717

18-
/* BEGIN_CASE */
18+
/* BEGIN_CASE */
1919
void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
2020
{
2121
psa_algorithm_t alg = alg_arg;
2222
unsigned char actual_hash[PSA_HASH_MAX_SIZE];
2323
size_t actual_hash_length;
2424
psa_hash_operation_t operation;
2525

26-
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
26+
PSA_ASSERT( psa_crypto_init( ) );
2727

28-
TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
29-
TEST_ASSERT( psa_hash_update( &operation,
30-
input->x, input->len ) == PSA_SUCCESS );
31-
TEST_ASSERT( psa_hash_finish( &operation,
32-
actual_hash, sizeof( actual_hash ),
33-
&actual_hash_length ) == PSA_SUCCESS );
28+
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
29+
PSA_ASSERT( psa_hash_update( &operation,
30+
input->x, input->len ) );
31+
PSA_ASSERT( psa_hash_finish( &operation,
32+
actual_hash, sizeof( actual_hash ),
33+
&actual_hash_length ) );
3434
ASSERT_COMPARE( expected_hash->x, expected_hash->len,
3535
actual_hash, actual_hash_length );
3636

@@ -45,15 +45,15 @@ void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
4545
psa_algorithm_t alg = alg_arg;
4646
psa_hash_operation_t operation;
4747

48-
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
48+
PSA_ASSERT( psa_crypto_init( ) );
4949

50-
TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
51-
TEST_ASSERT( psa_hash_update( &operation,
52-
input->x,
53-
input->len ) == PSA_SUCCESS );
54-
TEST_ASSERT( psa_hash_verify( &operation,
55-
expected_hash->x,
56-
expected_hash->len ) == PSA_SUCCESS );
50+
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
51+
PSA_ASSERT( psa_hash_update( &operation,
52+
input->x,
53+
input->len ) );
54+
PSA_ASSERT( psa_hash_verify( &operation,
55+
expected_hash->x,
56+
expected_hash->len ) );
5757

5858
exit:
5959
mbedtls_psa_crypto_free( );
@@ -69,22 +69,21 @@ void hash_multi_part( int alg_arg, data_t *input, data_t *expected_hash )
6969
psa_hash_operation_t operation;
7070
uint32_t len = 0;
7171

72-
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
72+
PSA_ASSERT( psa_crypto_init( ) );
7373

7474
do
7575
{
7676
memset( actual_hash, 0, sizeof( actual_hash ) );
77-
TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
77+
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
7878

79-
TEST_ASSERT( psa_hash_update( &operation,
80-
input->x, len ) == PSA_SUCCESS );
81-
TEST_ASSERT( psa_hash_update( &operation,
82-
input->x + len, input->len - len ) ==
83-
PSA_SUCCESS );
79+
PSA_ASSERT( psa_hash_update( &operation,
80+
input->x, len ) );
81+
PSA_ASSERT( psa_hash_update( &operation,
82+
input->x + len, input->len - len ) );
8483

85-
TEST_ASSERT( psa_hash_finish( &operation,
86-
actual_hash, sizeof( actual_hash ),
87-
&actual_hash_length ) == PSA_SUCCESS );
84+
PSA_ASSERT( psa_hash_finish( &operation,
85+
actual_hash, sizeof( actual_hash ),
86+
&actual_hash_length ) );
8887

8988
ASSERT_COMPARE( expected_hash->x, expected_hash->len,
9089
actual_hash, actual_hash_length );

tests/suites/test_suite_psa_crypto_init.function

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#include "mbedtls/entropy.h"
1313
#include "mbedtls/entropy_poll.h"
1414

15-
#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
16-
#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
17-
1815
#define ENTROPY_MIN_NV_SEED_SIZE \
1916
MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
2017

@@ -142,9 +139,9 @@ void init_deinit( int count )
142139
for( i = 0; i < count; i++ )
143140
{
144141
status = psa_crypto_init( );
145-
TEST_ASSERT( status == PSA_SUCCESS );
142+
PSA_ASSERT( status );
146143
status = psa_crypto_init( );
147-
TEST_ASSERT( status == PSA_SUCCESS );
144+
PSA_ASSERT( status );
148145
mbedtls_psa_crypto_free( );
149146
}
150147
}
@@ -156,7 +153,7 @@ void deinit_without_init( int count )
156153
int i;
157154
for( i = 0; i < count; i++ )
158155
{
159-
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
156+
PSA_ASSERT( psa_crypto_init( ) );
160157
mbedtls_psa_crypto_free( );
161158
}
162159
mbedtls_psa_crypto_free( );
@@ -172,11 +169,11 @@ void validate_module_init_generate_random( int count )
172169
for( i = 0; i < count; i++ )
173170
{
174171
status = psa_crypto_init( );
175-
TEST_ASSERT( status == PSA_SUCCESS );
172+
PSA_ASSERT( status );
176173
mbedtls_psa_crypto_free( );
177174
}
178175
status = psa_generate_random( random, sizeof( random ) );
179-
TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
176+
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
180177
}
181178
/* END_CASE */
182179

@@ -189,11 +186,11 @@ void validate_module_init_key_based( int count )
189186
for( i = 0; i < count; i++ )
190187
{
191188
status = psa_crypto_init( );
192-
TEST_ASSERT( status == PSA_SUCCESS );
189+
PSA_ASSERT( status );
193190
mbedtls_psa_crypto_free( );
194191
}
195192
status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
196-
TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
193+
TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
197194
}
198195
/* END_CASE */
199196

@@ -204,16 +201,14 @@ void custom_entropy_sources( int sources_arg, int expected_init_status_arg )
204201
uint8_t random[10] = { 0 };
205202

206203
custom_entropy_sources_mask = sources_arg;
207-
TEST_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
208-
custom_entropy_init, mbedtls_entropy_free ) ==
209-
PSA_SUCCESS );
204+
PSA_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
205+
custom_entropy_init, mbedtls_entropy_free ) );
210206

211-
TEST_ASSERT( psa_crypto_init( ) == expected_init_status );
207+
TEST_EQUAL( psa_crypto_init( ), expected_init_status );
212208
if( expected_init_status != PSA_SUCCESS )
213209
goto exit;
214210

215-
TEST_ASSERT( psa_generate_random( random, sizeof( random ) ) ==
216-
PSA_SUCCESS );
211+
PSA_ASSERT( psa_generate_random( random, sizeof( random ) ) );
217212

218213
exit:
219214
mbedtls_psa_crypto_free( );
@@ -246,16 +241,14 @@ void fake_entropy_source( int threshold,
246241
fake_entropy_state.length_sequence = lengths;
247242

248243
custom_entropy_sources_mask = ENTROPY_SOURCE_FAKE;
249-
TEST_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
250-
custom_entropy_init, mbedtls_entropy_free ) ==
251-
PSA_SUCCESS );
244+
PSA_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
245+
custom_entropy_init, mbedtls_entropy_free ) );
252246

253-
TEST_ASSERT( psa_crypto_init( ) == expected_init_status );
247+
TEST_EQUAL( psa_crypto_init( ), expected_init_status );
254248
if( expected_init_status != PSA_SUCCESS )
255249
goto exit;
256250

257-
TEST_ASSERT( psa_generate_random( random, sizeof( random ) ) ==
258-
PSA_SUCCESS );
251+
PSA_ASSERT( psa_generate_random( random, sizeof( random ) ) );
259252

260253
exit:
261254
mbedtls_psa_crypto_free( );
@@ -275,16 +268,14 @@ void entropy_from_nv_seed( int seed_size_arg,
275268
TEST_ASSERT( mbedtls_nv_seed_write( seed, seed_size ) >= 0 );
276269

277270
custom_entropy_sources_mask = ENTROPY_SOURCE_NV_SEED;
278-
TEST_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
279-
custom_entropy_init, mbedtls_entropy_free ) ==
280-
PSA_SUCCESS );
271+
PSA_ASSERT( mbedtls_psa_crypto_configure_entropy_sources(
272+
custom_entropy_init, mbedtls_entropy_free ) );
281273

282-
TEST_ASSERT( psa_crypto_init( ) == expected_init_status );
274+
TEST_EQUAL( psa_crypto_init( ), expected_init_status );
283275
if( expected_init_status != PSA_SUCCESS )
284276
goto exit;
285277

286-
TEST_ASSERT( psa_generate_random( random, sizeof( random ) ) ==
287-
PSA_SUCCESS );
278+
PSA_ASSERT( psa_generate_random( random, sizeof( random ) ) );
288279

289280
exit:
290281
mbedtls_free( seed );

0 commit comments

Comments
 (0)