mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-12-23 09:35:39 +00:00
Simplify selftest functions using macros
This reduces clutter, making the functions more readable. Also, it makes lcov see each line as covered. This is not cheating, as the lines that were previously seen as not covered are not supposed to be reached anyway (failing branches of the selftests). Thanks to this and previous test suite enhancements, lcov now sees chacha20.c and poly1305.c at 100% line coverage, and for chachapoly.c only two lines are not covered (error returns from lower-level module that should never happen except perhaps if an alternative implementation returns an unexpected error).
This commit is contained in:
parent
ceb1225d46
commit
c0dfcd4bf1
|
@ -519,18 +519,29 @@ static const size_t test_lengths[2] =
|
|||
375U
|
||||
};
|
||||
|
||||
#define ASSERT( cond, args ) \
|
||||
do \
|
||||
{ \
|
||||
if( ! ( cond ) ) \
|
||||
{ \
|
||||
if( verbose != 0 ) \
|
||||
mbedtls_printf args; \
|
||||
\
|
||||
return( -1 ); \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
int mbedtls_chacha20_self_test( int verbose )
|
||||
{
|
||||
unsigned char output[381];
|
||||
unsigned i;
|
||||
int result;
|
||||
|
||||
for ( i = 0U; i < 2U; i++ )
|
||||
for( i = 0U; i < 2U; i++ )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( " ChaCha20 test %u ", i );
|
||||
}
|
||||
|
||||
result = mbedtls_chacha20_crypt( test_keys[i],
|
||||
test_nonces[i],
|
||||
|
@ -538,36 +549,18 @@ int mbedtls_chacha20_self_test( int verbose )
|
|||
test_lengths[i],
|
||||
test_input[i],
|
||||
output );
|
||||
if ( result != 0)
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "error code: %i\n", result );
|
||||
}
|
||||
|
||||
return( -1 );
|
||||
}
|
||||
ASSERT( 0 == result, ( "error code: %i\n", result ) );
|
||||
|
||||
if ( 0 != memcmp( output, test_output[i], test_lengths[i] ) )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "failed\n" );
|
||||
}
|
||||
ASSERT( 0 == memcmp( output, test_output[i], test_lengths[i] ),
|
||||
( "failed (output)\n" ) );
|
||||
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( "passed\n" );
|
||||
}
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "\n" );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
|
|
@ -452,6 +452,19 @@ static const unsigned char test_mac[1][16] =
|
|||
}
|
||||
};
|
||||
|
||||
#define ASSERT( cond, args ) \
|
||||
do \
|
||||
{ \
|
||||
if( ! ( cond ) ) \
|
||||
{ \
|
||||
if( verbose != 0 ) \
|
||||
mbedtls_printf args; \
|
||||
\
|
||||
return( -1 ); \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
int mbedtls_chachapoly_self_test( int verbose )
|
||||
{
|
||||
mbedtls_chachapoly_context ctx;
|
||||
|
@ -460,24 +473,15 @@ int mbedtls_chachapoly_self_test( int verbose )
|
|||
unsigned char output[200];
|
||||
unsigned char mac[16];
|
||||
|
||||
for ( i = 0U; i < 1U; i++ )
|
||||
for( i = 0U; i < 1U; i++ )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( " ChaCha20-Poly1305 test %u ", i );
|
||||
}
|
||||
|
||||
mbedtls_chachapoly_init( &ctx );
|
||||
|
||||
result = mbedtls_chachapoly_setkey( &ctx, test_key[i] );
|
||||
if ( result != 0 )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "setkey() error code: %i\n", result );
|
||||
}
|
||||
return( -1 );
|
||||
}
|
||||
ASSERT( 0 == result, ( "setkey() error code: %i\n", result ) );
|
||||
|
||||
result = mbedtls_chachapoly_crypt_and_tag( &ctx,
|
||||
MBEDTLS_CHACHAPOLY_ENCRYPT,
|
||||
|
@ -488,45 +492,23 @@ int mbedtls_chachapoly_self_test( int verbose )
|
|||
test_input[i],
|
||||
output,
|
||||
mac );
|
||||
if ( result != 0 )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "crypt_and_tag() error code: %i\n", result );
|
||||
}
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
if ( memcmp( output, test_output[i], test_input_len[i] ) != 0 )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "failure (wrong output)\n" );
|
||||
}
|
||||
return( -1 );
|
||||
}
|
||||
ASSERT( 0 == result, ( "crypt_and_tag() error code: %i\n", result ) );
|
||||
|
||||
if ( memcmp( mac, test_mac[i], 16U ) != 0 )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "failure (wrong MAC)\n" );
|
||||
}
|
||||
return( -1 );
|
||||
}
|
||||
ASSERT( 0 == memcmp( output, test_output[i], test_input_len[i] ),
|
||||
( "failure (wrong output)\n" ) );
|
||||
|
||||
ASSERT( 0 == memcmp( mac, test_mac[i], 16U ),
|
||||
( "failure (wrong MAC)\n" ) );
|
||||
|
||||
mbedtls_chachapoly_free( &ctx );
|
||||
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( "passed\n" );
|
||||
}
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "\n" );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
|
|
@ -487,53 +487,44 @@ static const unsigned char test_mac[2][16] =
|
|||
}
|
||||
};
|
||||
|
||||
#define ASSERT( cond, args ) \
|
||||
do \
|
||||
{ \
|
||||
if( ! ( cond ) ) \
|
||||
{ \
|
||||
if( verbose != 0 ) \
|
||||
mbedtls_printf args; \
|
||||
\
|
||||
return( -1 ); \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
int mbedtls_poly1305_self_test( int verbose )
|
||||
{
|
||||
unsigned char mac[16];
|
||||
unsigned i;
|
||||
int result;
|
||||
|
||||
for ( i = 0U; i < 2U; i++ )
|
||||
for( i = 0U; i < 2U; i++ )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( " Poly1305 test %u ", i );
|
||||
}
|
||||
|
||||
result = mbedtls_poly1305_mac( test_keys[i],
|
||||
test_data[i],
|
||||
test_data_len[i],
|
||||
mac );
|
||||
if ( result != 0 )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "error code: %i\n", result );
|
||||
}
|
||||
ASSERT( 0 == result, ( "error code: %i\n", result ) );
|
||||
|
||||
return( -1 );
|
||||
}
|
||||
ASSERT( 0 == memcmp( mac, test_mac[i], 16U ), ( "failed (mac)\n" ) );
|
||||
|
||||
if ( memcmp( mac, test_mac[i], 16U ) != 0 )
|
||||
{
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "failed\n" );
|
||||
}
|
||||
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
if ( verbose != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
mbedtls_printf( "passed\n" );
|
||||
}
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
{
|
||||
mbedtls_printf( "\n" );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue