mbedtls/tests/suites/test_suite_debug.function

221 lines
6.1 KiB
Text
Raw Normal View History

/* BEGIN_HEADER */
2015-03-09 18:05:11 +01:00
#include "mbedtls/debug.h"
#include "string.h"
struct buffer_data
{
char buf[2000];
char *ptr;
};
2015-06-23 16:34:24 +02:00
void string_debug(void *data, int level, const char *file, int line, const char *str)
{
struct buffer_data *buffer = (struct buffer_data *) data;
2015-06-23 16:34:24 +02:00
char *p = buffer->ptr;
2011-07-13 16:53:58 +02:00
((void) level);
2015-06-23 16:34:24 +02:00
memcpy( p, file, strlen( file ) );
p += strlen( file );
*p++ = '(';
*p++ = '0' + ( line / 1000 ) % 10;
*p++ = '0' + ( line / 100 ) % 10;
*p++ = '0' + ( line / 10 ) % 10;
*p++ = '0' + ( line / 1 ) % 10;
*p++ = ')';
*p++ = ':';
*p++ = ' ';
#if defined(MBEDTLS_THREADING_C)
/* Skip "thread ID" (up to the first space) as it is not predictable */
while( *str++ != ' ' );
#endif
2015-06-23 16:34:24 +02:00
memcpy( p, str, strlen( str ) );
p += strlen( str );
/* Detect if debug messages output partial lines and mark them */
2015-06-23 16:34:24 +02:00
if( p[-1] != '\n' )
*p++ = '*';
buffer->ptr = p;
}
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
* END_DEPENDENCIES
*/
/* BEGIN_CASE */
2017-05-30 15:23:15 +02:00
void debug_print_msg_threshold( int threshold, int level, char * file,
int line, char * result_str )
{
mbedtls_ssl_context ssl;
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config conf;
struct buffer_data buffer;
mbedtls_ssl_init( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_init( &conf );
memset( buffer.buf, 0, 2000 );
buffer.ptr = buffer.buf;
mbedtls_ssl_config_defaults( &conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT );
mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
2015-05-04 14:56:36 +02:00
TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
mbedtls_debug_set_threshold( threshold );
2015-06-29 20:14:19 +02:00
mbedtls_debug_print_msg( &ssl, level, file, line,
"Text message, 2 == %d", 2 );
TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
exit:
mbedtls_ssl_free( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_free( &conf );
}
/* END_CASE */
/* BEGIN_CASE */
2017-05-30 15:23:15 +02:00
void mbedtls_debug_print_ret( char * file, int line, char * text, int value,
char * result_str )
{
mbedtls_ssl_context ssl;
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config conf;
struct buffer_data buffer;
mbedtls_ssl_init( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_init( &conf );
memset( buffer.buf, 0, 2000 );
buffer.ptr = buffer.buf;
mbedtls_ssl_config_defaults( &conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT );
2015-05-11 09:50:24 +02:00
mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
exit:
mbedtls_ssl_free( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_free( &conf );
}
/* END_CASE */
/* BEGIN_CASE */
2017-05-30 15:23:15 +02:00
void mbedtls_debug_print_buf( char * file, int line, char * text,
data_t * data, char * result_str )
{
mbedtls_ssl_context ssl;
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config conf;
struct buffer_data buffer;
mbedtls_ssl_init( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_init( &conf );
memset( buffer.buf, 0, 2000 );
buffer.ptr = buffer.buf;
mbedtls_ssl_config_defaults( &conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT );
2015-05-11 09:50:24 +02:00
mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
2017-06-09 05:32:58 +02:00
mbedtls_debug_print_buf( &ssl, 0, file, line, text, data->x, data->len );
TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
exit:
mbedtls_ssl_free( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_free( &conf );
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
2017-05-30 15:23:15 +02:00
void mbedtls_debug_print_crt( char * crt_file, char * file, int line,
char * prefix, char * result_str )
{
mbedtls_x509_crt crt;
mbedtls_ssl_context ssl;
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config conf;
struct buffer_data buffer;
mbedtls_ssl_init( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_init( &conf );
mbedtls_x509_crt_init( &crt );
memset( buffer.buf, 0, 2000 );
buffer.ptr = buffer.buf;
mbedtls_ssl_config_defaults( &conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT );
2015-05-11 09:50:24 +02:00
mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
2013-01-03 11:33:48 +01:00
exit:
mbedtls_x509_crt_free( &crt );
mbedtls_ssl_free( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_free( &conf );
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
void mbedtls_debug_print_mpi( char * value, char * file, int line,
2017-05-30 15:23:15 +02:00
char * prefix, char * result_str )
{
mbedtls_ssl_context ssl;
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config conf;
struct buffer_data buffer;
mbedtls_mpi val;
mbedtls_ssl_init( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_init( &conf );
mbedtls_mpi_init( &val );
memset( buffer.buf, 0, 2000 );
buffer.ptr = buffer.buf;
mbedtls_ssl_config_defaults( &conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT );
mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
2015-05-04 14:56:36 +02:00
TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &val, value ) == 0 );
mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
exit:
mbedtls_mpi_free( &val );
mbedtls_ssl_free( &ssl );
2015-05-04 14:56:36 +02:00
mbedtls_ssl_config_free( &conf );
}
/* END_CASE */