Add memory_buffer_alloc_self_test()

This commit is contained in:
Manuel Pégourié-Gonnard 2014-11-27 11:33:55 +01:00 committed by Paul Bakker
parent 5cb4b31057
commit 5ba1d52f96
3 changed files with 145 additions and 3 deletions

View File

@ -115,6 +115,15 @@ void memory_buffer_alloc_status( void );
*/ */
int memory_buffer_alloc_verify( void ); int memory_buffer_alloc_verify( void );
#if defined(POLARSSL_SELF_TEST)
/**
* \brief Checkup routine
*
* \return 0 if successful, or 1 if a test failed
*/
int memory_buffer_alloc_self_test( int verbose );
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -586,4 +586,130 @@ void memory_buffer_alloc_free()
polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) ); polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
} }
#if defined(POLARSSL_SELF_TEST)
static int check_pointer( void *p )
{
if( p == NULL )
return( -1 );
if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
return( -1 );
return( 0 );
}
static int check_all_free( )
{
if( heap.current_alloc_size != 0 ||
heap.first != heap.first_free ||
(void *) heap.first != (void *) heap.buf )
{
return( -1 );
}
return( 0 );
}
#define TEST_ASSERT( condition ) \
if( ! (condition) ) \
{ \
if( verbose != 0 ) \
polarssl_printf( "failed\n" ); \
\
ret = 1; \
goto cleanup; \
}
int memory_buffer_alloc_self_test( int verbose )
{
int ret = 0;
unsigned char buf[1024];
unsigned char *p, *q, *r;
if( verbose != 0 )
polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
memory_buffer_alloc_init( buf, sizeof( buf ) );
p = polarssl_malloc( 1 );
q = polarssl_malloc( 128 );
r = polarssl_malloc( 16 );
TEST_ASSERT( check_pointer( p ) == 0 &&
check_pointer( q ) == 0 &&
check_pointer( r ) == 0 );
polarssl_free( r );
polarssl_free( q );
polarssl_free( p );
TEST_ASSERT( check_all_free( ) == 0 );
memory_buffer_alloc_free( );
if( verbose != 0 )
polarssl_printf( "passed\n" );
if( verbose != 0 )
polarssl_printf( " MBA test #2 (buf not aligned): " );
memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
p = polarssl_malloc( 1 );
q = polarssl_malloc( 128 );
r = polarssl_malloc( 16 );
TEST_ASSERT( check_pointer( p ) == 0 &&
check_pointer( q ) == 0 &&
check_pointer( r ) == 0 );
polarssl_free( r );
polarssl_free( q );
polarssl_free( p );
TEST_ASSERT( check_all_free( ) == 0 );
memory_buffer_alloc_free( );
if( verbose != 0 )
polarssl_printf( "passed\n" );
if( verbose != 0 )
polarssl_printf( " MBA test #3 (full): " );
memory_buffer_alloc_init( buf, sizeof( buf ) );
p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
TEST_ASSERT( check_pointer( p ) == 0 );
TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
polarssl_free( p );
p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
q = polarssl_malloc( 16 );
TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
polarssl_free( q );
TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
polarssl_free( p );
TEST_ASSERT( check_all_free( ) == 0 );
memory_buffer_alloc_free( );
if( verbose != 0 )
polarssl_printf( "passed\n" );
cleanup:
memory_buffer_alloc_free( );
return( ret );
}
#endif /* POLARSSL_SELF_TEST */
#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */ #endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */

View File

@ -226,16 +226,23 @@ int main( int argc, char *argv[] )
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG) #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
memory_buffer_alloc_status(); memory_buffer_alloc_status();
#endif #endif
}
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
memory_buffer_alloc_free();
if( ( ret = memory_buffer_alloc_self_test( v ) ) != 0 )
return( ret );
#endif
if( v != 0 )
{
printf( " [ All tests passed ]\n\n" ); printf( " [ All tests passed ]\n\n" );
#if defined(_WIN32) #if defined(_WIN32)
printf( " Press Enter to exit this program.\n" ); printf( " Press Enter to exit this program.\n" );
fflush( stdout ); getchar(); fflush( stdout ); getchar();
#endif #endif
} }
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
memory_buffer_alloc_free();
#endif
return( ret ); return( ret );
} }