1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_DEBUG_C
8 #ifdef POLARSSL_SSL_TLS_C
18 void string_debug(
void *data,
int level,
const char *str)
20 struct buffer_data *buffer = (
struct buffer_data *) data;
23 memcpy(buffer->ptr, str, strlen(str));
24 buffer->ptr += strlen(str);
27 if( *(buffer->ptr - 1) !=
'\n' )
37 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
41 #if defined(POLARSSL_PLATFORM_C)
44 #define polarssl_malloc malloc
45 #define polarssl_free free
50 typedef UINT32 uint32_t;
63 #define GET_UINT32_BE(n,b,i) \
65 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
66 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
67 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
68 | ( (uint32_t) (b)[(i) + 3] ); \
73 #define PUT_UINT32_BE(n,b,i) \
75 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
76 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
77 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
78 (b)[(i) + 3] = (unsigned char) ( (n) ); \
82 static int unhexify(
unsigned char *obuf,
const char *ibuf)
85 int len = strlen(ibuf) / 2;
86 assert(!(strlen(ibuf) %1));
91 if( c >=
'0' && c <=
'9' )
93 else if( c >=
'a' && c <=
'f' )
95 else if( c >=
'A' && c <=
'F' )
101 if( c2 >=
'0' && c2 <=
'9' )
103 else if( c2 >=
'a' && c2 <=
'f' )
105 else if( c2 >=
'A' && c2 <=
'F' )
110 *obuf++ = ( c << 4 ) | c2;
116 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
128 *obuf++ =
'a' + h - 10;
133 *obuf++ =
'a' + l - 10;
150 size_t actual_len = len != 0 ? len : 1;
155 memset( p, 0x00, actual_len );
174 *olen = strlen(ibuf) / 2;
180 assert( obuf != NULL );
196 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
198 #if !defined(__OpenBSD__)
201 if( rng_state != NULL )
204 for( i = 0; i < len; ++i )
207 if( rng_state != NULL )
210 arc4random_buf( output, len );
221 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
223 if( rng_state != NULL )
226 memset( output, 0, len );
253 if( rng_state == NULL )
262 memcpy( output, info->
buf, use_len );
263 info->
buf += use_len;
267 if( len - use_len > 0 )
268 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
297 uint32_t i, *k, sum, delta=0x9E3779B9;
298 unsigned char result[4], *out = output;
300 if( rng_state == NULL )
307 size_t use_len = ( len > 4 ) ? 4 : len;
310 for( i = 0; i < 32; i++ )
312 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
314 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
318 memcpy( out, result, use_len );
330 #if defined(POLARSSL_PLATFORM_C)
333 #define polarssl_printf printf
334 #define polarssl_malloc malloc
335 #define polarssl_free free
340 #ifdef POLARSSL_DEBUG_C
341 #ifdef POLARSSL_SSL_TLS_C
343 #define TEST_SUITE_ACTIVE
345 static int test_assert(
int correct,
const char *test )
351 if( test_errors == 1 )
352 printf(
"FAILED\n" );
353 printf(
" %s\n", test );
358 #define TEST_ASSERT( TEST ) \
359 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
360 if( test_errors) goto exit; \
365 if( (*str)[0] !=
'"' ||
366 (*str)[strlen( *str ) - 1] !=
'"' )
368 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
373 (*str)[strlen( *str ) - 1] =
'\0';
385 for( i = 0; i < strlen( str ); i++ )
387 if( i == 0 && str[i] ==
'-' )
393 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
394 str[i - 1] ==
'0' && str[i] ==
'x' )
400 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
401 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
402 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
412 *value = strtol( str, NULL, 16 );
414 *value = strtol( str, NULL, 10 );
419 if( strcmp( str,
"-0xFFFF" ) == 0 )
421 *value = ( -0xFFFF );
424 if( strcmp( str,
"POLARSSL_DEBUG_LOG_RAW" ) == 0 )
429 if( strcmp( str,
"POLARSSL_DEBUG_LOG_FULL" ) == 0 )
436 printf(
"Expected integer for parameter and got: %s\n", str );
440 void test_suite_debug_print_msg_threshold(
int threshold,
int level,
char *file,
int line,
444 struct buffer_data buffer;
447 memset( buffer.buf, 0, 2000 );
448 buffer.ptr = buffer.buf;
455 debug_fmt(
"Text message, 2 == %d", 2 ) );
457 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
463 void test_suite_debug_print_ret(
int mode,
char *file,
int line,
char *text,
int value,
467 struct buffer_data buffer;
470 memset( buffer.buf, 0, 2000 );
471 buffer.ptr = buffer.buf;
478 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
484 void test_suite_debug_print_buf(
int mode,
char *file,
int line,
char *text,
485 char *data_string,
char *result_str )
487 unsigned char data[10000];
489 struct buffer_data buffer;
492 memset( &data, 0,
sizeof( data ) );
494 memset( buffer.buf, 0, 2000 );
495 buffer.ptr = buffer.buf;
497 data_len =
unhexify( data, data_string );
504 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
510 #ifdef POLARSSL_FS_IO
511 #ifdef POLARSSL_X509_CRT_PARSE_C
512 void test_suite_debug_print_crt(
int mode,
char *crt_file,
char *file,
int line,
513 char *prefix,
char *result_str )
517 struct buffer_data buffer;
521 memset( buffer.buf, 0, 2000 );
522 buffer.ptr = buffer.buf;
530 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
538 #ifdef POLARSSL_BIGNUM_C
539 void test_suite_debug_print_mpi(
int mode,
int radix,
char *value,
char *file,
int line,
540 char *prefix,
char *result_str )
543 struct buffer_data buffer;
549 memset( buffer.buf, 0, 2000 );
550 buffer.ptr = buffer.buf;
559 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
576 if( strcmp( str,
"POLARSSL_BASE64_C" ) == 0 )
578 #if defined(POLARSSL_BASE64_C)
584 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
586 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
592 if( strcmp( str,
"POLARSSL_PEM_PARSE_C" ) == 0 )
594 #if defined(POLARSSL_PEM_PARSE_C)
600 if( strcmp( str,
"POLARSSL_RSA_C" ) == 0 )
602 #if defined(POLARSSL_RSA_C)
608 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
610 #if defined(POLARSSL_ECP_C)
627 #if defined(TEST_SUITE_ACTIVE)
628 if( strcmp( params[0],
"debug_print_msg_threshold" ) == 0 )
633 char *param3 = params[3];
635 char *param5 = params[5];
639 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
643 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
644 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
646 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
649 test_suite_debug_print_msg_threshold( param1, param2, param3, param4, param5 );
655 if( strcmp( params[0],
"debug_print_ret" ) == 0 )
659 char *param2 = params[2];
661 char *param4 = params[4];
663 char *param6 = params[6];
667 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
671 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
673 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
675 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
678 test_suite_debug_print_ret( param1, param2, param3, param4, param5, param6 );
684 if( strcmp( params[0],
"debug_print_buf" ) == 0 )
688 char *param2 = params[2];
690 char *param4 = params[4];
691 char *param5 = params[5];
692 char *param6 = params[6];
696 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
700 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
702 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
707 test_suite_debug_print_buf( param1, param2, param3, param4, param5, param6 );
713 if( strcmp( params[0],
"debug_print_crt" ) == 0 )
715 #ifdef POLARSSL_FS_IO
716 #ifdef POLARSSL_X509_CRT_PARSE_C
719 char *param2 = params[2];
720 char *param3 = params[3];
722 char *param5 = params[5];
723 char *param6 = params[6];
727 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
731 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
734 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
738 test_suite_debug_print_crt( param1, param2, param3, param4, param5, param6 );
746 if( strcmp( params[0],
"debug_print_mpi" ) == 0 )
748 #ifdef POLARSSL_BIGNUM_C
752 char *param3 = params[3];
753 char *param4 = params[4];
755 char *param6 = params[6];
756 char *param7 = params[7];
760 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 8 );
764 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
765 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
768 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
772 test_suite_debug_print_mpi( param1, param2, param3, param4, param5, param6, param7 );
781 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
795 ret = fgets( buf, len, f );
799 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
800 buf[strlen(buf) - 1] =
'\0';
801 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
802 buf[strlen(buf) - 1] =
'\0';
815 while( *p !=
'\0' && p < buf + len )
825 if( p + 1 < buf + len )
837 for( i = 0; i < cnt; i++ )
844 if( *p ==
'\\' && *(p + 1) ==
'n' )
849 else if( *p ==
'\\' && *(p + 1) ==
':' )
854 else if( *p ==
'\\' && *(p + 1) ==
'?' )
870 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
871 const char *filename =
"/tmp/B.1c06a028-9709-4951-a65b-b24142b09746/BUILD/polarssl-1.3.9/tests/suites/test_suite_debug.data";
876 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
877 unsigned char alloc_buf[1000000];
881 file = fopen( filename,
"r" );
884 fprintf( stderr,
"Failed to open\n" );
888 while( !feof( file ) )
892 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
894 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
895 fprintf( stdout,
" " );
896 for( i = strlen( buf ) + 1; i < 67; i++ )
897 fprintf( stdout,
"." );
898 fprintf( stdout,
" " );
903 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
907 if( strcmp( params[0],
"depends_on" ) == 0 )
909 for( i = 1; i < cnt; i++ )
913 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
924 if( skip == 1 || ret == 3 )
927 fprintf( stdout,
"----\n" );
930 else if( ret == 0 && test_errors == 0 )
932 fprintf( stdout,
"PASS\n" );
937 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
944 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
946 if( strlen(buf) != 0 )
948 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
954 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
955 if( total_errors == 0 )
956 fprintf( stdout,
"PASSED" );
958 fprintf( stdout,
"FAILED" );
960 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
961 total_tests - total_errors, total_tests, total_skipped );
963 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
964 #if defined(POLARSSL_MEMORY_DEBUG)
965 memory_buffer_alloc_status();
970 return( total_errors != 0 );
void debug_print_crt(const ssl_context *ssl, int level, const char *file, int line, const char *text, const x509_crt *crt)
#define POLARSSL_DEBUG_LOG_FULL
Include file:line in log lines.
Memory allocation layer (Deprecated to platform layer)
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
void ssl_set_dbg(ssl_context *ssl, void(*f_dbg)(void *, int, const char *), void *p_dbg)
Set the debug callback.
void debug_print_msg(const ssl_context *ssl, int level, const char *file, int line, const char *text)
void x509_crt_free(x509_crt *crt)
Unallocate all certificate data.
Configuration options (set of defines)
static int test_assert(int correct, const char *test)
void mpi_init(mpi *X)
Initialize one MPI.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
Container for an X.509 certificate.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
void debug_print_mpi(const ssl_context *ssl, int level, const char *file, int line, const char *text, const mpi *X)
void debug_print_buf(const ssl_context *ssl, int level, const char *file, int line, const char *text, unsigned char *buf, size_t len)
void x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
void debug_print_ret(const ssl_context *ssl, int level, const char *file, int line, const char *text, int ret)
void mpi_free(mpi *X)
Unallocate one MPI.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int parse_arguments(char *buf, size_t len, char *params[50])
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
#define POLARSSL_DEBUG_LOG_RAW
Only log raw debug lines.
int verify_string(char **str)
#define PUT_UINT32_BE(n, b, i)
int get_line(FILE *f, char *buf, size_t len)
void debug_set_threshold(int threshold)
Set the level threshold to handle globally.
int dispatch_test(int cnt, char *params[50])
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
void debug_set_log_mode(int log_mode)
Set the log mode for the debug functions globally (Default value: POLARSSL_DEBUG_DFL_MODE) ...
int verify_int(char *str, int *value)
char * debug_fmt(const char *format,...)
static int unhexify(unsigned char *obuf, const char *ibuf)
int x509_crt_parse_file(x509_crt *chain, const char *path)
Load one or more certificates and add them to the chained list.