3 #ifdef POLARSSL_DEBUG_C
4 #ifdef POLARSSL_BIGNUM_C
5 #ifdef POLARSSL_SSL_TLS_C
16 void string_debug(
void *data,
int level,
const char *str)
18 struct buffer_data *buffer = (
struct buffer_data *) data;
21 memcpy(buffer->ptr, str, strlen(str));
22 buffer->ptr += strlen(str);
30 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
34 #if defined(WANT_NOT_RND_MPI)
35 #if defined(POLARSSL_BIGNUM_C)
38 #error "not_rnd_mpi() need bignum.c"
44 typedef UINT32 uint32_t;
57 #define GET_UINT32_BE(n,b,i) \
59 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
60 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
61 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
62 | ( (uint32_t) (b)[(i) + 3] ); \
67 #define PUT_UINT32_BE(n,b,i) \
69 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
70 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
71 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
72 (b)[(i) + 3] = (unsigned char) ( (n) ); \
76 static int unhexify(
unsigned char *obuf,
const char *ibuf)
79 int len = strlen(ibuf) / 2;
80 assert(!(strlen(ibuf) %1));
85 if( c >=
'0' && c <=
'9' )
87 else if( c >=
'a' && c <=
'f' )
89 else if( c >=
'A' && c <=
'F' )
95 if( c2 >=
'0' && c2 <=
'9' )
97 else if( c2 >=
'a' && c2 <=
'f' )
99 else if( c2 >=
'A' && c2 <=
'F' )
104 *obuf++ = ( c << 4 ) | c2;
110 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
122 *obuf++ =
'a' + h - 10;
127 *obuf++ =
'a' + l - 10;
143 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
147 if( rng_state != NULL )
150 for( i = 0; i < len; ++i )
161 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
163 if( rng_state != NULL )
166 memset( output, 0, len );
193 if( rng_state == NULL )
202 memcpy( output, info->
buf, use_len );
203 info->
buf += use_len;
207 if( len - use_len > 0 )
208 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
237 uint32_t i, *k, sum, delta=0x9E3779B9;
238 unsigned char result[4];
240 if( rng_state == NULL )
247 size_t use_len = ( len > 4 ) ? 4 : len;
250 for( i = 0; i < 32; i++ )
252 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
254 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
258 memcpy( output, result, use_len );
265 #if defined(WANT_NOT_RND_MPI)
274 #define ciL (sizeof(t_uint))
275 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
276 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
278 char *str = (
char *) in;
287 X.
n = CHARS_TO_LIMBS( len );
293 assert( strlen( str ) / 2 == len );
305 #ifdef POLARSSL_DEBUG_C
306 #ifdef POLARSSL_BIGNUM_C
307 #ifdef POLARSSL_SSL_TLS_C
308 #ifdef POLARSSL_RSA_C
310 #define TEST_SUITE_ACTIVE
318 if( test_errors == 1 )
319 printf(
"FAILED\n" );
320 printf(
" %s\n", test );
325 #define TEST_ASSERT( TEST ) \
326 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
327 if( test_errors) return; \
332 if( (*str)[0] !=
'"' ||
333 (*str)[strlen( *str ) - 1] !=
'"' )
335 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
340 (*str)[strlen( *str ) - 1] =
'\0';
352 for( i = 0; i < strlen( str ); i++ )
354 if( i == 0 && str[i] ==
'-' )
360 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
361 str[i - 1] ==
'0' && str[i] ==
'x' )
367 if( str[i] <
'0' || str[i] >
'9' )
377 *value = strtol( str, NULL, 16 );
379 *value = strtol( str, NULL, 10 );
386 printf(
"Expected integer for parameter and got: %s\n", str );
390 #ifdef POLARSSL_FS_IO
391 #ifdef POLARSSL_X509_CRT_PARSE_C
392 void test_suite_debug_print_crt(
char *crt_file,
char *file,
int line,
char *prefix,
397 struct buffer_data buffer;
401 memset( buffer.buf, 0, 2000 );
402 buffer.ptr = buffer.buf;
409 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
416 void test_suite_debug_print_mpi(
int radix,
char *value,
char *file,
int line,
417 char *prefix,
char *result_str )
420 struct buffer_data buffer;
426 memset( buffer.buf, 0, 2000 );
427 buffer.ptr = buffer.buf;
434 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
451 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
453 #if defined(POLARSSL_ECP_C)
459 if( strcmp( str,
"POLARSSL_BASE64_C" ) == 0 )
461 #if defined(POLARSSL_BASE64_C)
467 if( strcmp( str,
"POLARSSL_PEM_C" ) == 0 )
469 #if defined(POLARSSL_PEM_C)
475 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
477 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
494 #if defined(TEST_SUITE_ACTIVE)
495 if( strcmp( params[0],
"debug_print_crt" ) == 0 )
497 #ifdef POLARSSL_FS_IO
498 #ifdef POLARSSL_X509_CRT_PARSE_C
500 char *param1 = params[1];
501 char *param2 = params[2];
503 char *param4 = params[4];
504 char *param5 = params[5];
508 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
514 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
518 test_suite_debug_print_crt( param1, param2, param3, param4, param5 );
526 if( strcmp( params[0],
"debug_print_mpi" ) == 0 )
530 char *param2 = params[2];
531 char *param3 = params[3];
533 char *param5 = params[5];
534 char *param6 = params[6];
538 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
542 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
545 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
549 test_suite_debug_print_mpi( param1, param2, param3, param4, param5, param6 );
557 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
571 ret = fgets( buf, len, f );
575 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
576 buf[strlen(buf) - 1] =
'\0';
577 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
578 buf[strlen(buf) - 1] =
'\0';
591 while( *p !=
'\0' && p < buf + len )
601 if( p + 1 < buf + len )
613 for( i = 0; i < cnt; i++ )
620 if( *p ==
'\\' && *(p + 1) ==
'n' )
625 else if( *p ==
'\\' && *(p + 1) ==
':' )
630 else if( *p ==
'\\' && *(p + 1) ==
'?' )
646 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
647 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_debug.data";
652 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
653 unsigned char alloc_buf[1000000];
654 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
657 file = fopen( filename,
"r" );
660 fprintf( stderr,
"Failed to open\n" );
664 while( !feof( file ) )
668 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
670 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
671 fprintf( stdout,
" " );
672 for( i = strlen( buf ) + 1; i < 67; i++ )
673 fprintf( stdout,
"." );
674 fprintf( stdout,
" " );
679 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
683 if( strcmp( params[0],
"depends_on" ) == 0 )
685 for( i = 1; i < cnt; i++ )
689 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
700 if( skip == 1 || ret == 3 )
703 fprintf( stdout,
"----\n" );
706 else if( ret == 0 && test_errors == 0 )
708 fprintf( stdout,
"PASS\n" );
713 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
720 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
722 if( strlen(buf) != 0 )
724 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
730 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
731 if( total_errors == 0 )
732 fprintf( stdout,
"PASSED" );
734 fprintf( stdout,
"FAILED" );
736 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
737 total_tests - total_errors, total_tests, total_skipped );
739 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
740 #if defined(POLARSSL_MEMORY_DEBUG)
741 memory_buffer_alloc_status();
743 memory_buffer_alloc_free();
746 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)
Info structure for the pseudo random function.
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 x509_crt_free(x509_crt *crt)
Unallocate all certificate data.
Configuration options (set of defines)
static int test_assert(int correct, char *test)
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
Multi-precision integer library.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#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 x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
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.
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.
int verify_string(char **str)
#define PUT_UINT32_BE(n, b, i)
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.
int verify_int(char *str, int *value)
static int unhexify(unsigned char *obuf, const char *ibuf)
int get_line(FILE *f, char *buf, size_t len)
int x509_crt_parse_file(x509_crt *chain, const char *path)
Load one or more certificates and add them to the chained list.