PolarSSL v1.3.1
test_suite_debug.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_DEBUG_C
4 #ifdef POLARSSL_BIGNUM_C
5 #ifdef POLARSSL_SSL_TLS_C
6 #ifdef POLARSSL_RSA_C
7 
8 #include <polarssl/debug.h>
9 
10 struct buffer_data
11 {
12  char buf[2000];
13  char *ptr;
14 };
15 
16 void string_debug(void *data, int level, const char *str)
17 {
18  struct buffer_data *buffer = (struct buffer_data *) data;
19  ((void) level);
20 
21  memcpy(buffer->ptr, str, strlen(str));
22  buffer->ptr += strlen(str);
23 }
24 #endif /* POLARSSL_DEBUG_C */
25 #endif /* POLARSSL_BIGNUM_C */
26 #endif /* POLARSSL_SSL_TLS_C */
27 #endif /* POLARSSL_RSA_C */
28 
29 
30 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
31 #include "polarssl/memory.h"
32 #endif
33 
34 #ifdef _MSC_VER
35 #include <basetsd.h>
36 typedef UINT32 uint32_t;
37 #else
38 #include <inttypes.h>
39 #endif
40 
41 #include <assert.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 /*
46  * 32-bit integer manipulation macros (big endian)
47  */
48 #ifndef GET_UINT32_BE
49 #define GET_UINT32_BE(n,b,i) \
50 { \
51  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
52  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
53  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
54  | ( (uint32_t) (b)[(i) + 3] ); \
55 }
56 #endif
57 
58 #ifndef PUT_UINT32_BE
59 #define PUT_UINT32_BE(n,b,i) \
60 { \
61  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
62  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
63  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
64  (b)[(i) + 3] = (unsigned char) ( (n) ); \
65 }
66 #endif
67 
68 static int unhexify(unsigned char *obuf, const char *ibuf)
69 {
70  unsigned char c, c2;
71  int len = strlen(ibuf) / 2;
72  assert(!(strlen(ibuf) %1)); // must be even number of bytes
73 
74  while (*ibuf != 0)
75  {
76  c = *ibuf++;
77  if( c >= '0' && c <= '9' )
78  c -= '0';
79  else if( c >= 'a' && c <= 'f' )
80  c -= 'a' - 10;
81  else if( c >= 'A' && c <= 'F' )
82  c -= 'A' - 10;
83  else
84  assert( 0 );
85 
86  c2 = *ibuf++;
87  if( c2 >= '0' && c2 <= '9' )
88  c2 -= '0';
89  else if( c2 >= 'a' && c2 <= 'f' )
90  c2 -= 'a' - 10;
91  else if( c2 >= 'A' && c2 <= 'F' )
92  c2 -= 'A' - 10;
93  else
94  assert( 0 );
95 
96  *obuf++ = ( c << 4 ) | c2;
97  }
98 
99  return len;
100 }
101 
102 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
103 {
104  unsigned char l, h;
105 
106  while (len != 0)
107  {
108  h = (*ibuf) / 16;
109  l = (*ibuf) % 16;
110 
111  if( h < 10 )
112  *obuf++ = '0' + h;
113  else
114  *obuf++ = 'a' + h - 10;
115 
116  if( l < 10 )
117  *obuf++ = '0' + l;
118  else
119  *obuf++ = 'a' + l - 10;
120 
121  ++ibuf;
122  len--;
123  }
124 }
125 
135 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
136 {
137  size_t i;
138 
139  if( rng_state != NULL )
140  rng_state = NULL;
141 
142  for( i = 0; i < len; ++i )
143  output[i] = rand();
144 
145  return( 0 );
146 }
147 
153 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
154 {
155  if( rng_state != NULL )
156  rng_state = NULL;
157 
158  memset( output, 0, len );
159 
160  return( 0 );
161 }
162 
163 typedef struct
164 {
165  unsigned char *buf;
166  size_t length;
167 } rnd_buf_info;
168 
180 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
181 {
182  rnd_buf_info *info = (rnd_buf_info *) rng_state;
183  size_t use_len;
184 
185  if( rng_state == NULL )
186  return( rnd_std_rand( NULL, output, len ) );
187 
188  use_len = len;
189  if( len > info->length )
190  use_len = info->length;
191 
192  if( use_len )
193  {
194  memcpy( output, info->buf, use_len );
195  info->buf += use_len;
196  info->length -= use_len;
197  }
198 
199  if( len - use_len > 0 )
200  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
201 
202  return( 0 );
203 }
204 
212 typedef struct
213 {
214  uint32_t key[16];
215  uint32_t v0, v1;
217 
226 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
227 {
228  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
229  uint32_t i, *k, sum, delta=0x9E3779B9;
230  unsigned char result[4];
231 
232  if( rng_state == NULL )
233  return( rnd_std_rand( NULL, output, len ) );
234 
235  k = info->key;
236 
237  while( len > 0 )
238  {
239  size_t use_len = ( len > 4 ) ? 4 : len;
240  sum = 0;
241 
242  for( i = 0; i < 32; i++ )
243  {
244  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
245  sum += delta;
246  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
247  }
248 
249  PUT_UINT32_BE( info->v0, result, 0 );
250  memcpy( output, result, use_len );
251  len -= use_len;
252  }
253 
254  return( 0 );
255 }
256 
266 static int not_rnd( void *in, unsigned char *out, size_t len )
267 {
268  unsigned char *obuf;
269  const char *ibuf = in;
270  unsigned char c, c2;
271  assert( len == strlen(ibuf) / 2 );
272  assert(!(strlen(ibuf) %1)); // must be even number of bytes
273 
274  obuf = out + (len - 1); // sic
275  while (*ibuf != 0)
276  {
277  c = *ibuf++;
278  if( c >= '0' && c <= '9' )
279  c -= '0';
280  else if( c >= 'a' && c <= 'f' )
281  c -= 'a' - 10;
282  else if( c >= 'A' && c <= 'F' )
283  c -= 'A' - 10;
284  else
285  assert( 0 );
286 
287  c2 = *ibuf++;
288  if( c2 >= '0' && c2 <= '9' )
289  c2 -= '0';
290  else if( c2 >= 'a' && c2 <= 'f' )
291  c2 -= 'a' - 10;
292  else if( c2 >= 'A' && c2 <= 'F' )
293  c2 -= 'A' - 10;
294  else
295  assert( 0 );
296 
297  *obuf-- = ( c << 4 ) | c2; // sic
298  }
299 
300  return( 0 );
301 }
302 
303 
304 #include <stdio.h>
305 #include <string.h>
306 
307 static int test_errors = 0;
308 
309 #ifdef POLARSSL_DEBUG_C
310 #ifdef POLARSSL_BIGNUM_C
311 #ifdef POLARSSL_SSL_TLS_C
312 #ifdef POLARSSL_RSA_C
313 
314 #define TEST_SUITE_ACTIVE
315 
316 static int test_assert( int correct, char *test )
317 {
318  if( correct )
319  return( 0 );
320 
321  test_errors++;
322  if( test_errors == 1 )
323  printf( "FAILED\n" );
324  printf( " %s\n", test );
325 
326  return( 1 );
327 }
328 
329 #define TEST_ASSERT( TEST ) \
330  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
331  if( test_errors) return; \
332  } while (0)
333 
334 int verify_string( char **str )
335 {
336  if( (*str)[0] != '"' ||
337  (*str)[strlen( *str ) - 1] != '"' )
338  {
339  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
340  return( -1 );
341  }
342 
343  (*str)++;
344  (*str)[strlen( *str ) - 1] = '\0';
345 
346  return( 0 );
347 }
348 
349 int verify_int( char *str, int *value )
350 {
351  size_t i;
352  int minus = 0;
353  int digits = 1;
354  int hex = 0;
355 
356  for( i = 0; i < strlen( str ); i++ )
357  {
358  if( i == 0 && str[i] == '-' )
359  {
360  minus = 1;
361  continue;
362  }
363 
364  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
365  str[i - 1] == '0' && str[i] == 'x' )
366  {
367  hex = 1;
368  continue;
369  }
370 
371  if( str[i] < '0' || str[i] > '9' )
372  {
373  digits = 0;
374  break;
375  }
376  }
377 
378  if( digits )
379  {
380  if( hex )
381  *value = strtol( str, NULL, 16 );
382  else
383  *value = strtol( str, NULL, 10 );
384 
385  return( 0 );
386  }
387 
388 
389 
390  printf( "Expected integer for parameter and got: %s\n", str );
391  return( -1 );
392 }
393 
394 #ifdef POLARSSL_FS_IO
395 #ifdef POLARSSL_X509_CRT_PARSE_C
396 void test_suite_debug_print_crt( char *crt_file, char *file, int line, char *prefix,
397  char *result_str )
398 {
399  x509_crt crt;
400  ssl_context ssl;
401  struct buffer_data buffer;
402 
403  x509_crt_init( &crt );
404  memset( &ssl, 0, sizeof( ssl_context ) );
405  memset( buffer.buf, 0, 2000 );
406  buffer.ptr = buffer.buf;
407 
408  ssl_set_dbg(&ssl, string_debug, &buffer);
409 
410  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
411  debug_print_crt( &ssl, 0, file, line, prefix, &crt);
412 
413  TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
414 
415  x509_crt_free( &crt );
416 }
417 #endif /* POLARSSL_FS_IO */
418 #endif /* POLARSSL_X509_CRT_PARSE_C */
419 
420 void test_suite_debug_print_mpi( int radix, char *value, char *file, int line,
421  char *prefix, char *result_str )
422 {
423  ssl_context ssl;
424  struct buffer_data buffer;
425  mpi val;
426 
427  mpi_init( &val );
428 
429  memset( &ssl, 0, sizeof( ssl_context ) );
430  memset( buffer.buf, 0, 2000 );
431  buffer.ptr = buffer.buf;
432 
433  TEST_ASSERT( mpi_read_string( &val, radix, value ) == 0 );
434  ssl_set_dbg(&ssl, string_debug, &buffer);
435 
436  debug_print_mpi( &ssl, 0, file, line, prefix, &val);
437 
438  TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
439 
440  mpi_free( &val );
441 }
442 
443 
444 #endif /* POLARSSL_DEBUG_C */
445 #endif /* POLARSSL_BIGNUM_C */
446 #endif /* POLARSSL_SSL_TLS_C */
447 #endif /* POLARSSL_RSA_C */
448 
449 
450 int dep_check( char *str )
451 {
452  if( str == NULL )
453  return( 1 );
454 
455  if( strcmp( str, "POLARSSL_BASE64_C" ) == 0 )
456  {
457 #if defined(POLARSSL_BASE64_C)
458  return( 0 );
459 #else
460  return( 1 );
461 #endif
462  }
463  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
464  {
465 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
466  return( 0 );
467 #else
468  return( 1 );
469 #endif
470  }
471  if( strcmp( str, "POLARSSL_PEM_C" ) == 0 )
472  {
473 #if defined(POLARSSL_PEM_C)
474  return( 0 );
475 #else
476  return( 1 );
477 #endif
478  }
479  if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
480  {
481 #if defined(POLARSSL_ECP_C)
482  return( 0 );
483 #else
484  return( 1 );
485 #endif
486  }
487 
488 
489  return( 1 );
490 }
491 
492 int dispatch_test(int cnt, char *params[50])
493 {
494  int ret;
495  ((void) cnt);
496  ((void) params);
497 
498 #if defined(TEST_SUITE_ACTIVE)
499  if( strcmp( params[0], "debug_print_crt" ) == 0 )
500  {
501  #ifdef POLARSSL_FS_IO
502  #ifdef POLARSSL_X509_CRT_PARSE_C
503 
504  char *param1 = params[1];
505  char *param2 = params[2];
506  int param3;
507  char *param4 = params[4];
508  char *param5 = params[5];
509 
510  if( cnt != 6 )
511  {
512  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
513  return( 2 );
514  }
515 
516  if( verify_string( &param1 ) != 0 ) return( 2 );
517  if( verify_string( &param2 ) != 0 ) return( 2 );
518  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
519  if( verify_string( &param4 ) != 0 ) return( 2 );
520  if( verify_string( &param5 ) != 0 ) return( 2 );
521 
522  test_suite_debug_print_crt( param1, param2, param3, param4, param5 );
523  return ( 0 );
524  #endif /* POLARSSL_FS_IO */
525  #endif /* POLARSSL_X509_CRT_PARSE_C */
526 
527  return ( 3 );
528  }
529  else
530  if( strcmp( params[0], "debug_print_mpi" ) == 0 )
531  {
532 
533  int param1;
534  char *param2 = params[2];
535  char *param3 = params[3];
536  int param4;
537  char *param5 = params[5];
538  char *param6 = params[6];
539 
540  if( cnt != 7 )
541  {
542  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
543  return( 2 );
544  }
545 
546  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
547  if( verify_string( &param2 ) != 0 ) return( 2 );
548  if( verify_string( &param3 ) != 0 ) return( 2 );
549  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
550  if( verify_string( &param5 ) != 0 ) return( 2 );
551  if( verify_string( &param6 ) != 0 ) return( 2 );
552 
553  test_suite_debug_print_mpi( param1, param2, param3, param4, param5, param6 );
554  return ( 0 );
555 
556  return ( 3 );
557  }
558  else
559 
560  {
561  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
562  fflush( stdout );
563  return( 1 );
564  }
565 #else
566  return( 3 );
567 #endif
568  return( ret );
569 }
570 
571 int get_line( FILE *f, char *buf, size_t len )
572 {
573  char *ret;
574 
575  ret = fgets( buf, len, f );
576  if( ret == NULL )
577  return( -1 );
578 
579  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
580  buf[strlen(buf) - 1] = '\0';
581  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
582  buf[strlen(buf) - 1] = '\0';
583 
584  return( 0 );
585 }
586 
587 int parse_arguments( char *buf, size_t len, char *params[50] )
588 {
589  int cnt = 0, i;
590  char *cur = buf;
591  char *p = buf, *q;
592 
593  params[cnt++] = cur;
594 
595  while( *p != '\0' && p < buf + len )
596  {
597  if( *p == '\\' )
598  {
599  *p++;
600  *p++;
601  continue;
602  }
603  if( *p == ':' )
604  {
605  if( p + 1 < buf + len )
606  {
607  cur = p + 1;
608  params[cnt++] = cur;
609  }
610  *p = '\0';
611  }
612 
613  *p++;
614  }
615 
616  // Replace newlines, question marks and colons in strings
617  for( i = 0; i < cnt; i++ )
618  {
619  p = params[i];
620  q = params[i];
621 
622  while( *p != '\0' )
623  {
624  if( *p == '\\' && *(p + 1) == 'n' )
625  {
626  p += 2;
627  *(q++) = '\n';
628  }
629  else if( *p == '\\' && *(p + 1) == ':' )
630  {
631  p += 2;
632  *(q++) = ':';
633  }
634  else if( *p == '\\' && *(p + 1) == '?' )
635  {
636  p += 2;
637  *(q++) = '?';
638  }
639  else
640  *(q++) = *(p++);
641  }
642  *q = '\0';
643  }
644 
645  return( cnt );
646 }
647 
648 int main()
649 {
650  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
651  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_debug.data";
652  FILE *file;
653  char buf[5000];
654  char *params[50];
655 
656 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
657  unsigned char alloc_buf[1000000];
658  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
659 #endif
660 
661  file = fopen( filename, "r" );
662  if( file == NULL )
663  {
664  fprintf( stderr, "Failed to open\n" );
665  return( 1 );
666  }
667 
668  while( !feof( file ) )
669  {
670  int skip = 0;
671 
672  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
673  break;
674  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
675  fprintf( stdout, " " );
676  for( i = strlen( buf ) + 1; i < 67; i++ )
677  fprintf( stdout, "." );
678  fprintf( stdout, " " );
679  fflush( stdout );
680 
681  total_tests++;
682 
683  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
684  break;
685  cnt = parse_arguments( buf, strlen(buf), params );
686 
687  if( strcmp( params[0], "depends_on" ) == 0 )
688  {
689  for( i = 1; i < cnt; i++ )
690  if( dep_check( params[i] ) != 0 )
691  skip = 1;
692 
693  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
694  break;
695  cnt = parse_arguments( buf, strlen(buf), params );
696  }
697 
698  if( skip == 0 )
699  {
700  test_errors = 0;
701  ret = dispatch_test( cnt, params );
702  }
703 
704  if( skip == 1 || ret == 3 )
705  {
706  total_skipped++;
707  fprintf( stdout, "----\n" );
708  fflush( stdout );
709  }
710  else if( ret == 0 && test_errors == 0 )
711  {
712  fprintf( stdout, "PASS\n" );
713  fflush( stdout );
714  }
715  else if( ret == 2 )
716  {
717  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
718  fclose(file);
719  exit( 2 );
720  }
721  else
722  total_errors++;
723 
724  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
725  break;
726  if( strlen(buf) != 0 )
727  {
728  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
729  return( 1 );
730  }
731  }
732  fclose(file);
733 
734  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
735  if( total_errors == 0 )
736  fprintf( stdout, "PASSED" );
737  else
738  fprintf( stdout, "FAILED" );
739 
740  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
741  total_tests - total_errors, total_tests, total_skipped );
742 
743 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
744 #if defined(POLARSSL_MEMORY_DEBUG)
745  memory_buffer_alloc_status();
746 #endif
747  memory_buffer_alloc_free();
748 #endif
749 
750  return( total_errors != 0 );
751 }
752 
753