1/* BEGIN_HEADER */ 2#include "mbedtls/debug.h" 3 4struct buffer_data 5{ 6 char buf[2000]; 7 char *ptr; 8}; 9 10void string_debug(void *data, int level, const char *file, int line, const char *str) 11{ 12 struct buffer_data *buffer = (struct buffer_data *) data; 13 char *p = buffer->ptr; 14 ((void) level); 15 16 memcpy( p, file, strlen( file ) ); 17 p += strlen( file ); 18 19 *p++ = '('; 20 *p++ = '0' + ( line / 1000 ) % 10; 21 *p++ = '0' + ( line / 100 ) % 10; 22 *p++ = '0' + ( line / 10 ) % 10; 23 *p++ = '0' + ( line / 1 ) % 10; 24 *p++ = ')'; 25 *p++ = ':'; 26 *p++ = ' '; 27 28#if defined(MBEDTLS_THREADING_C) 29 /* Skip "thread ID" (up to the first space) as it is not predictable */ 30 while( *str++ != ' ' ); 31#endif 32 33 memcpy( p, str, strlen( str ) ); 34 p += strlen( str ); 35 36 /* Detect if debug messages output partial lines and mark them */ 37 if( p[-1] != '\n' ) 38 *p++ = '*'; 39 40 buffer->ptr = p; 41} 42/* END_HEADER */ 43 44/* BEGIN_DEPENDENCIES 45 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C 46 * END_DEPENDENCIES 47 */ 48 49/* BEGIN_CASE */ 50void debug_print_msg_threshold( int threshold, int level, char *file, int line, 51 char *result_str ) 52{ 53 mbedtls_ssl_context ssl; 54 mbedtls_ssl_config conf; 55 struct buffer_data buffer; 56 57 mbedtls_ssl_init( &ssl ); 58 mbedtls_ssl_config_init( &conf ); 59 memset( buffer.buf, 0, 2000 ); 60 buffer.ptr = buffer.buf; 61 62 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); 63 64 mbedtls_debug_set_threshold( threshold ); 65 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer); 66 67 mbedtls_debug_print_msg( &ssl, level, file, line, 68 "Text message, 2 == %d", 2 ); 69 70 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 ); 71 72exit: 73 mbedtls_ssl_free( &ssl ); 74 mbedtls_ssl_config_free( &conf ); 75} 76/* END_CASE */ 77 78/* BEGIN_CASE */ 79void mbedtls_debug_print_ret( char *file, int line, char *text, int value, 80 char *result_str ) 81{ 82 mbedtls_ssl_context ssl; 83 mbedtls_ssl_config conf; 84 struct buffer_data buffer; 85 86 mbedtls_ssl_init( &ssl ); 87 mbedtls_ssl_config_init( &conf ); 88 memset( buffer.buf, 0, 2000 ); 89 buffer.ptr = buffer.buf; 90 91 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); 92 93 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer); 94 95 mbedtls_debug_print_ret( &ssl, 0, file, line, text, value); 96 97 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 ); 98 99exit: 100 mbedtls_ssl_free( &ssl ); 101 mbedtls_ssl_config_free( &conf ); 102} 103/* END_CASE */ 104 105/* BEGIN_CASE */ 106void mbedtls_debug_print_buf( char *file, int line, char *text, 107 char *data_string, char *result_str ) 108{ 109 unsigned char data[10000]; 110 mbedtls_ssl_context ssl; 111 mbedtls_ssl_config conf; 112 struct buffer_data buffer; 113 size_t data_len; 114 115 mbedtls_ssl_init( &ssl ); 116 mbedtls_ssl_config_init( &conf ); 117 memset( &data, 0, sizeof( data ) ); 118 memset( buffer.buf, 0, 2000 ); 119 buffer.ptr = buffer.buf; 120 121 data_len = unhexify( data, data_string ); 122 123 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); 124 125 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer); 126 127 mbedtls_debug_print_buf( &ssl, 0, file, line, text, data, data_len ); 128 129 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 ); 130 131exit: 132 mbedtls_ssl_free( &ssl ); 133 mbedtls_ssl_config_free( &conf ); 134} 135/* END_CASE */ 136 137/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ 138void mbedtls_debug_print_crt( char *crt_file, char *file, int line, 139 char *prefix, char *result_str ) 140{ 141 mbedtls_x509_crt crt; 142 mbedtls_ssl_context ssl; 143 mbedtls_ssl_config conf; 144 struct buffer_data buffer; 145 146 mbedtls_ssl_init( &ssl ); 147 mbedtls_ssl_config_init( &conf ); 148 mbedtls_x509_crt_init( &crt ); 149 memset( buffer.buf, 0, 2000 ); 150 buffer.ptr = buffer.buf; 151 152 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); 153 154 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer); 155 156 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); 157 mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt); 158 159 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 ); 160 161exit: 162 mbedtls_x509_crt_free( &crt ); 163 mbedtls_ssl_free( &ssl ); 164 mbedtls_ssl_config_free( &conf ); 165} 166/* END_CASE */ 167 168/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */ 169void mbedtls_debug_print_mpi( int radix, char *value, char *file, int line, 170 char *prefix, char *result_str ) 171{ 172 mbedtls_ssl_context ssl; 173 mbedtls_ssl_config conf; 174 struct buffer_data buffer; 175 mbedtls_mpi val; 176 177 mbedtls_ssl_init( &ssl ); 178 mbedtls_ssl_config_init( &conf ); 179 mbedtls_mpi_init( &val ); 180 memset( buffer.buf, 0, 2000 ); 181 buffer.ptr = buffer.buf; 182 183 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); 184 185 TEST_ASSERT( mbedtls_mpi_read_string( &val, radix, value ) == 0 ); 186 187 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer); 188 189 mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val); 190 191 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 ); 192 193exit: 194 mbedtls_mpi_free( &val ); 195 mbedtls_ssl_free( &ssl ); 196 mbedtls_ssl_config_free( &conf ); 197} 198/* END_CASE */ 199