test_suite_debug.function 5.0 KB

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