PolarSSL v1.3.1
test_suite_mdx.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 
4 #include <polarssl/md2.h>
5 #include <polarssl/md4.h>
6 #include <polarssl/md5.h>
7 
8 
9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
10 #include "polarssl/memory.h"
11 #endif
12 
13 #ifdef _MSC_VER
14 #include <basetsd.h>
15 typedef UINT32 uint32_t;
16 #else
17 #include <inttypes.h>
18 #endif
19 
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 /*
25  * 32-bit integer manipulation macros (big endian)
26  */
27 #ifndef GET_UINT32_BE
28 #define GET_UINT32_BE(n,b,i) \
29 { \
30  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33  | ( (uint32_t) (b)[(i) + 3] ); \
34 }
35 #endif
36 
37 #ifndef PUT_UINT32_BE
38 #define PUT_UINT32_BE(n,b,i) \
39 { \
40  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43  (b)[(i) + 3] = (unsigned char) ( (n) ); \
44 }
45 #endif
46 
47 static int unhexify(unsigned char *obuf, const char *ibuf)
48 {
49  unsigned char c, c2;
50  int len = strlen(ibuf) / 2;
51  assert(!(strlen(ibuf) %1)); // must be even number of bytes
52 
53  while (*ibuf != 0)
54  {
55  c = *ibuf++;
56  if( c >= '0' && c <= '9' )
57  c -= '0';
58  else if( c >= 'a' && c <= 'f' )
59  c -= 'a' - 10;
60  else if( c >= 'A' && c <= 'F' )
61  c -= 'A' - 10;
62  else
63  assert( 0 );
64 
65  c2 = *ibuf++;
66  if( c2 >= '0' && c2 <= '9' )
67  c2 -= '0';
68  else if( c2 >= 'a' && c2 <= 'f' )
69  c2 -= 'a' - 10;
70  else if( c2 >= 'A' && c2 <= 'F' )
71  c2 -= 'A' - 10;
72  else
73  assert( 0 );
74 
75  *obuf++ = ( c << 4 ) | c2;
76  }
77 
78  return len;
79 }
80 
81 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
82 {
83  unsigned char l, h;
84 
85  while (len != 0)
86  {
87  h = (*ibuf) / 16;
88  l = (*ibuf) % 16;
89 
90  if( h < 10 )
91  *obuf++ = '0' + h;
92  else
93  *obuf++ = 'a' + h - 10;
94 
95  if( l < 10 )
96  *obuf++ = '0' + l;
97  else
98  *obuf++ = 'a' + l - 10;
99 
100  ++ibuf;
101  len--;
102  }
103 }
104 
114 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
115 {
116  size_t i;
117 
118  if( rng_state != NULL )
119  rng_state = NULL;
120 
121  for( i = 0; i < len; ++i )
122  output[i] = rand();
123 
124  return( 0 );
125 }
126 
132 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
133 {
134  if( rng_state != NULL )
135  rng_state = NULL;
136 
137  memset( output, 0, len );
138 
139  return( 0 );
140 }
141 
142 typedef struct
143 {
144  unsigned char *buf;
145  size_t length;
146 } rnd_buf_info;
147 
159 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
160 {
161  rnd_buf_info *info = (rnd_buf_info *) rng_state;
162  size_t use_len;
163 
164  if( rng_state == NULL )
165  return( rnd_std_rand( NULL, output, len ) );
166 
167  use_len = len;
168  if( len > info->length )
169  use_len = info->length;
170 
171  if( use_len )
172  {
173  memcpy( output, info->buf, use_len );
174  info->buf += use_len;
175  info->length -= use_len;
176  }
177 
178  if( len - use_len > 0 )
179  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
180 
181  return( 0 );
182 }
183 
191 typedef struct
192 {
193  uint32_t key[16];
194  uint32_t v0, v1;
196 
205 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
206 {
207  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
208  uint32_t i, *k, sum, delta=0x9E3779B9;
209  unsigned char result[4];
210 
211  if( rng_state == NULL )
212  return( rnd_std_rand( NULL, output, len ) );
213 
214  k = info->key;
215 
216  while( len > 0 )
217  {
218  size_t use_len = ( len > 4 ) ? 4 : len;
219  sum = 0;
220 
221  for( i = 0; i < 32; i++ )
222  {
223  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
224  sum += delta;
225  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
226  }
227 
228  PUT_UINT32_BE( info->v0, result, 0 );
229  memcpy( output, result, use_len );
230  len -= use_len;
231  }
232 
233  return( 0 );
234 }
235 
245 static int not_rnd( void *in, unsigned char *out, size_t len )
246 {
247  unsigned char *obuf;
248  const char *ibuf = in;
249  unsigned char c, c2;
250  assert( len == strlen(ibuf) / 2 );
251  assert(!(strlen(ibuf) %1)); // must be even number of bytes
252 
253  obuf = out + (len - 1); // sic
254  while (*ibuf != 0)
255  {
256  c = *ibuf++;
257  if( c >= '0' && c <= '9' )
258  c -= '0';
259  else if( c >= 'a' && c <= 'f' )
260  c -= 'a' - 10;
261  else if( c >= 'A' && c <= 'F' )
262  c -= 'A' - 10;
263  else
264  assert( 0 );
265 
266  c2 = *ibuf++;
267  if( c2 >= '0' && c2 <= '9' )
268  c2 -= '0';
269  else if( c2 >= 'a' && c2 <= 'f' )
270  c2 -= 'a' - 10;
271  else if( c2 >= 'A' && c2 <= 'F' )
272  c2 -= 'A' - 10;
273  else
274  assert( 0 );
275 
276  *obuf-- = ( c << 4 ) | c2; // sic
277  }
278 
279  return( 0 );
280 }
281 
282 
283 #include <stdio.h>
284 #include <string.h>
285 
286 static int test_errors = 0;
287 
288 
289 #define TEST_SUITE_ACTIVE
290 
291 static int test_assert( int correct, char *test )
292 {
293  if( correct )
294  return( 0 );
295 
296  test_errors++;
297  if( test_errors == 1 )
298  printf( "FAILED\n" );
299  printf( " %s\n", test );
300 
301  return( 1 );
302 }
303 
304 #define TEST_ASSERT( TEST ) \
305  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
306  if( test_errors) return; \
307  } while (0)
308 
309 int verify_string( char **str )
310 {
311  if( (*str)[0] != '"' ||
312  (*str)[strlen( *str ) - 1] != '"' )
313  {
314  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
315  return( -1 );
316  }
317 
318  (*str)++;
319  (*str)[strlen( *str ) - 1] = '\0';
320 
321  return( 0 );
322 }
323 
324 int verify_int( char *str, int *value )
325 {
326  size_t i;
327  int minus = 0;
328  int digits = 1;
329  int hex = 0;
330 
331  for( i = 0; i < strlen( str ); i++ )
332  {
333  if( i == 0 && str[i] == '-' )
334  {
335  minus = 1;
336  continue;
337  }
338 
339  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
340  str[i - 1] == '0' && str[i] == 'x' )
341  {
342  hex = 1;
343  continue;
344  }
345 
346  if( str[i] < '0' || str[i] > '9' )
347  {
348  digits = 0;
349  break;
350  }
351  }
352 
353  if( digits )
354  {
355  if( hex )
356  *value = strtol( str, NULL, 16 );
357  else
358  *value = strtol( str, NULL, 10 );
359 
360  return( 0 );
361  }
362 
363 
364 
365  printf( "Expected integer for parameter and got: %s\n", str );
366  return( -1 );
367 }
368 
369 #ifdef POLARSSL_MD2_C
370 void test_suite_md2_text( char *text_src_string, char *hex_hash_string )
371 {
372  unsigned char src_str[1000];
373  unsigned char hash_str[1000];
374  unsigned char output[33];
375 
376  memset(src_str, 0x00, 1000);
377  memset(hash_str, 0x00, 1000);
378  memset(output, 0x00, 33);
379 
380  strcpy( (char *) src_str, text_src_string );
381 
382  md2( src_str, strlen( (char *) src_str ), output );
383  hexify( hash_str, output, 16 );
384 
385  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
386 }
387 #endif /* POLARSSL_MD2_C */
388 
389 #ifdef POLARSSL_MD4_C
390 void test_suite_md4_text( char *text_src_string, char *hex_hash_string )
391 {
392  unsigned char src_str[1000];
393  unsigned char hash_str[1000];
394  unsigned char output[33];
395 
396  memset(src_str, 0x00, 1000);
397  memset(hash_str, 0x00, 1000);
398  memset(output, 0x00, 33);
399 
400  strcpy( (char *) src_str, text_src_string );
401 
402  md4( src_str, strlen( (char *) src_str ), output );
403  hexify( hash_str, output, 16 );
404 
405  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
406 }
407 #endif /* POLARSSL_MD4_C */
408 
409 #ifdef POLARSSL_MD5_C
410 void test_suite_md5_text( char *text_src_string, char *hex_hash_string )
411 {
412  unsigned char src_str[1000];
413  unsigned char hash_str[1000];
414  unsigned char output[33];
415 
416  memset(src_str, 0x00, 1000);
417  memset(hash_str, 0x00, 1000);
418  memset(output, 0x00, 33);
419 
420  strcpy( (char *) src_str, text_src_string );
421 
422  md5( src_str, strlen( (char *) src_str ), output );
423  hexify( hash_str, output, 16 );
424 
425  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
426 }
427 #endif /* POLARSSL_MD5_C */
428 
429 #ifdef POLARSSL_MD2_C
430 void test_suite_md2_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
431  char *hex_hash_string )
432 {
433  unsigned char src_str[10000];
434  unsigned char key_str[10000];
435  unsigned char hash_str[10000];
436  unsigned char output[33];
437  int key_len, src_len;
438 
439  memset(src_str, 0x00, 10000);
440  memset(key_str, 0x00, 10000);
441  memset(hash_str, 0x00, 10000);
442  memset(output, 0x00, 33);
443 
444  key_len = unhexify( key_str, hex_key_string );
445  src_len = unhexify( src_str, hex_src_string );
446 
447  md2_hmac( key_str, key_len, src_str, src_len, output );
448  hexify( hash_str, output, 16 );
449 
450  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
451 }
452 #endif /* POLARSSL_MD2_C */
453 
454 #ifdef POLARSSL_MD4_C
455 void test_suite_md4_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
456  char *hex_hash_string )
457 {
458  unsigned char src_str[10000];
459  unsigned char key_str[10000];
460  unsigned char hash_str[10000];
461  unsigned char output[33];
462  int key_len, src_len;
463 
464  memset(src_str, 0x00, 10000);
465  memset(key_str, 0x00, 10000);
466  memset(hash_str, 0x00, 10000);
467  memset(output, 0x00, 33);
468 
469  key_len = unhexify( key_str, hex_key_string );
470  src_len = unhexify( src_str, hex_src_string );
471 
472  md4_hmac( key_str, key_len, src_str, src_len, output );
473  hexify( hash_str, output, 16 );
474 
475  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
476 }
477 #endif /* POLARSSL_MD4_C */
478 
479 #ifdef POLARSSL_MD5_C
480 void test_suite_md5_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
481  char *hex_hash_string )
482 {
483  unsigned char src_str[10000];
484  unsigned char key_str[10000];
485  unsigned char hash_str[10000];
486  unsigned char output[33];
487  int key_len, src_len;
488 
489  memset(src_str, 0x00, 10000);
490  memset(key_str, 0x00, 10000);
491  memset(hash_str, 0x00, 10000);
492  memset(output, 0x00, 33);
493 
494  key_len = unhexify( key_str, hex_key_string );
495  src_len = unhexify( src_str, hex_src_string );
496 
497  md5_hmac( key_str, key_len, src_str, src_len, output );
498  hexify( hash_str, output, 16 );
499 
500  TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
501 }
502 #endif /* POLARSSL_MD5_C */
503 
504 #ifdef POLARSSL_MD2_C
505 #ifdef POLARSSL_FS_IO
506 void test_suite_md2_file( char *filename, char *hex_hash_string )
507 {
508  unsigned char hash_str[65];
509  unsigned char output[33];
510 
511  memset(hash_str, 0x00, 65);
512  memset(output, 0x00, 33);
513 
514  md2_file( filename, output);
515  hexify( hash_str, output, 16 );
516 
517  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
518 }
519 #endif /* POLARSSL_MD2_C */
520 #endif /* POLARSSL_FS_IO */
521 
522 #ifdef POLARSSL_MD4_C
523 #ifdef POLARSSL_FS_IO
524 void test_suite_md4_file( char *filename, char *hex_hash_string )
525 {
526  unsigned char hash_str[65];
527  unsigned char output[33];
528 
529  memset(hash_str, 0x00, 65);
530  memset(output, 0x00, 33);
531 
532  md4_file( filename, output);
533  hexify( hash_str, output, 16 );
534 
535  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
536 }
537 #endif /* POLARSSL_MD4_C */
538 #endif /* POLARSSL_FS_IO */
539 
540 #ifdef POLARSSL_MD5_C
541 #ifdef POLARSSL_FS_IO
542 void test_suite_md5_file( char *filename, char *hex_hash_string )
543 {
544  unsigned char hash_str[65];
545  unsigned char output[33];
546 
547  memset(hash_str, 0x00, 65);
548  memset(output, 0x00, 33);
549 
550  md5_file( filename, output);
551  hexify( hash_str, output, 16 );
552 
553  TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
554 }
555 #endif /* POLARSSL_MD5_C */
556 #endif /* POLARSSL_FS_IO */
557 
558 #ifdef POLARSSL_MD2_C
559 #ifdef POLARSSL_SELF_TEST
560 void test_suite_md2_selftest()
561 {
562  TEST_ASSERT( md2_self_test( 0 ) == 0 );
563 }
564 #endif /* POLARSSL_MD2_C */
565 #endif /* POLARSSL_SELF_TEST */
566 
567 #ifdef POLARSSL_MD4_C
568 #ifdef POLARSSL_SELF_TEST
569 void test_suite_md4_selftest()
570 {
571  TEST_ASSERT( md4_self_test( 0 ) == 0 );
572 }
573 #endif /* POLARSSL_MD4_C */
574 #endif /* POLARSSL_SELF_TEST */
575 
576 #ifdef POLARSSL_MD5_C
577 #ifdef POLARSSL_SELF_TEST
578 void test_suite_md5_selftest()
579 {
580  TEST_ASSERT( md5_self_test( 0 ) == 0 );
581 }
582 #endif /* POLARSSL_MD5_C */
583 #endif /* POLARSSL_SELF_TEST */
584 
585 
586 
587 
588 int dep_check( char *str )
589 {
590  if( str == NULL )
591  return( 1 );
592 
593  if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
594  {
595 #if defined(POLARSSL_SELF_TEST)
596  return( 0 );
597 #else
598  return( 1 );
599 #endif
600  }
601  if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
602  {
603 #if defined(POLARSSL_MD5_C)
604  return( 0 );
605 #else
606  return( 1 );
607 #endif
608  }
609  if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
610  {
611 #if defined(POLARSSL_MD4_C)
612  return( 0 );
613 #else
614  return( 1 );
615 #endif
616  }
617  if( strcmp( str, "POLARSSL_MD2_C" ) == 0 )
618  {
619 #if defined(POLARSSL_MD2_C)
620  return( 0 );
621 #else
622  return( 1 );
623 #endif
624  }
625 
626 
627  return( 1 );
628 }
629 
630 int dispatch_test(int cnt, char *params[50])
631 {
632  int ret;
633  ((void) cnt);
634  ((void) params);
635 
636 #if defined(TEST_SUITE_ACTIVE)
637  if( strcmp( params[0], "md2_text" ) == 0 )
638  {
639  #ifdef POLARSSL_MD2_C
640 
641  char *param1 = params[1];
642  char *param2 = params[2];
643 
644  if( cnt != 3 )
645  {
646  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
647  return( 2 );
648  }
649 
650  if( verify_string( &param1 ) != 0 ) return( 2 );
651  if( verify_string( &param2 ) != 0 ) return( 2 );
652 
653  test_suite_md2_text( param1, param2 );
654  return ( 0 );
655  #endif /* POLARSSL_MD2_C */
656 
657  return ( 3 );
658  }
659  else
660  if( strcmp( params[0], "md4_text" ) == 0 )
661  {
662  #ifdef POLARSSL_MD4_C
663 
664  char *param1 = params[1];
665  char *param2 = params[2];
666 
667  if( cnt != 3 )
668  {
669  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
670  return( 2 );
671  }
672 
673  if( verify_string( &param1 ) != 0 ) return( 2 );
674  if( verify_string( &param2 ) != 0 ) return( 2 );
675 
676  test_suite_md4_text( param1, param2 );
677  return ( 0 );
678  #endif /* POLARSSL_MD4_C */
679 
680  return ( 3 );
681  }
682  else
683  if( strcmp( params[0], "md5_text" ) == 0 )
684  {
685  #ifdef POLARSSL_MD5_C
686 
687  char *param1 = params[1];
688  char *param2 = params[2];
689 
690  if( cnt != 3 )
691  {
692  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
693  return( 2 );
694  }
695 
696  if( verify_string( &param1 ) != 0 ) return( 2 );
697  if( verify_string( &param2 ) != 0 ) return( 2 );
698 
699  test_suite_md5_text( param1, param2 );
700  return ( 0 );
701  #endif /* POLARSSL_MD5_C */
702 
703  return ( 3 );
704  }
705  else
706  if( strcmp( params[0], "md2_hmac" ) == 0 )
707  {
708  #ifdef POLARSSL_MD2_C
709 
710  int param1;
711  char *param2 = params[2];
712  char *param3 = params[3];
713  char *param4 = params[4];
714 
715  if( cnt != 5 )
716  {
717  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
718  return( 2 );
719  }
720 
721  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
722  if( verify_string( &param2 ) != 0 ) return( 2 );
723  if( verify_string( &param3 ) != 0 ) return( 2 );
724  if( verify_string( &param4 ) != 0 ) return( 2 );
725 
726  test_suite_md2_hmac( param1, param2, param3, param4 );
727  return ( 0 );
728  #endif /* POLARSSL_MD2_C */
729 
730  return ( 3 );
731  }
732  else
733  if( strcmp( params[0], "md4_hmac" ) == 0 )
734  {
735  #ifdef POLARSSL_MD4_C
736 
737  int param1;
738  char *param2 = params[2];
739  char *param3 = params[3];
740  char *param4 = params[4];
741 
742  if( cnt != 5 )
743  {
744  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
745  return( 2 );
746  }
747 
748  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
749  if( verify_string( &param2 ) != 0 ) return( 2 );
750  if( verify_string( &param3 ) != 0 ) return( 2 );
751  if( verify_string( &param4 ) != 0 ) return( 2 );
752 
753  test_suite_md4_hmac( param1, param2, param3, param4 );
754  return ( 0 );
755  #endif /* POLARSSL_MD4_C */
756 
757  return ( 3 );
758  }
759  else
760  if( strcmp( params[0], "md5_hmac" ) == 0 )
761  {
762  #ifdef POLARSSL_MD5_C
763 
764  int param1;
765  char *param2 = params[2];
766  char *param3 = params[3];
767  char *param4 = params[4];
768 
769  if( cnt != 5 )
770  {
771  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
772  return( 2 );
773  }
774 
775  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
776  if( verify_string( &param2 ) != 0 ) return( 2 );
777  if( verify_string( &param3 ) != 0 ) return( 2 );
778  if( verify_string( &param4 ) != 0 ) return( 2 );
779 
780  test_suite_md5_hmac( param1, param2, param3, param4 );
781  return ( 0 );
782  #endif /* POLARSSL_MD5_C */
783 
784  return ( 3 );
785  }
786  else
787  if( strcmp( params[0], "md2_file" ) == 0 )
788  {
789  #ifdef POLARSSL_MD2_C
790  #ifdef POLARSSL_FS_IO
791 
792  char *param1 = params[1];
793  char *param2 = params[2];
794 
795  if( cnt != 3 )
796  {
797  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
798  return( 2 );
799  }
800 
801  if( verify_string( &param1 ) != 0 ) return( 2 );
802  if( verify_string( &param2 ) != 0 ) return( 2 );
803 
804  test_suite_md2_file( param1, param2 );
805  return ( 0 );
806  #endif /* POLARSSL_MD2_C */
807  #endif /* POLARSSL_FS_IO */
808 
809  return ( 3 );
810  }
811  else
812  if( strcmp( params[0], "md4_file" ) == 0 )
813  {
814  #ifdef POLARSSL_MD4_C
815  #ifdef POLARSSL_FS_IO
816 
817  char *param1 = params[1];
818  char *param2 = params[2];
819 
820  if( cnt != 3 )
821  {
822  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
823  return( 2 );
824  }
825 
826  if( verify_string( &param1 ) != 0 ) return( 2 );
827  if( verify_string( &param2 ) != 0 ) return( 2 );
828 
829  test_suite_md4_file( param1, param2 );
830  return ( 0 );
831  #endif /* POLARSSL_MD4_C */
832  #endif /* POLARSSL_FS_IO */
833 
834  return ( 3 );
835  }
836  else
837  if( strcmp( params[0], "md5_file" ) == 0 )
838  {
839  #ifdef POLARSSL_MD5_C
840  #ifdef POLARSSL_FS_IO
841 
842  char *param1 = params[1];
843  char *param2 = params[2];
844 
845  if( cnt != 3 )
846  {
847  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
848  return( 2 );
849  }
850 
851  if( verify_string( &param1 ) != 0 ) return( 2 );
852  if( verify_string( &param2 ) != 0 ) return( 2 );
853 
854  test_suite_md5_file( param1, param2 );
855  return ( 0 );
856  #endif /* POLARSSL_MD5_C */
857  #endif /* POLARSSL_FS_IO */
858 
859  return ( 3 );
860  }
861  else
862  if( strcmp( params[0], "md2_selftest" ) == 0 )
863  {
864  #ifdef POLARSSL_MD2_C
865  #ifdef POLARSSL_SELF_TEST
866 
867 
868  if( cnt != 1 )
869  {
870  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
871  return( 2 );
872  }
873 
874 
875  test_suite_md2_selftest( );
876  return ( 0 );
877  #endif /* POLARSSL_MD2_C */
878  #endif /* POLARSSL_SELF_TEST */
879 
880  return ( 3 );
881  }
882  else
883  if( strcmp( params[0], "md4_selftest" ) == 0 )
884  {
885  #ifdef POLARSSL_MD4_C
886  #ifdef POLARSSL_SELF_TEST
887 
888 
889  if( cnt != 1 )
890  {
891  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
892  return( 2 );
893  }
894 
895 
896  test_suite_md4_selftest( );
897  return ( 0 );
898  #endif /* POLARSSL_MD4_C */
899  #endif /* POLARSSL_SELF_TEST */
900 
901  return ( 3 );
902  }
903  else
904  if( strcmp( params[0], "md5_selftest" ) == 0 )
905  {
906  #ifdef POLARSSL_MD5_C
907  #ifdef POLARSSL_SELF_TEST
908 
909 
910  if( cnt != 1 )
911  {
912  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
913  return( 2 );
914  }
915 
916 
917  test_suite_md5_selftest( );
918  return ( 0 );
919  #endif /* POLARSSL_MD5_C */
920  #endif /* POLARSSL_SELF_TEST */
921 
922  return ( 3 );
923  }
924  else
925 
926  {
927  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
928  fflush( stdout );
929  return( 1 );
930  }
931 #else
932  return( 3 );
933 #endif
934  return( ret );
935 }
936 
937 int get_line( FILE *f, char *buf, size_t len )
938 {
939  char *ret;
940 
941  ret = fgets( buf, len, f );
942  if( ret == NULL )
943  return( -1 );
944 
945  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
946  buf[strlen(buf) - 1] = '\0';
947  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
948  buf[strlen(buf) - 1] = '\0';
949 
950  return( 0 );
951 }
952 
953 int parse_arguments( char *buf, size_t len, char *params[50] )
954 {
955  int cnt = 0, i;
956  char *cur = buf;
957  char *p = buf, *q;
958 
959  params[cnt++] = cur;
960 
961  while( *p != '\0' && p < buf + len )
962  {
963  if( *p == '\\' )
964  {
965  *p++;
966  *p++;
967  continue;
968  }
969  if( *p == ':' )
970  {
971  if( p + 1 < buf + len )
972  {
973  cur = p + 1;
974  params[cnt++] = cur;
975  }
976  *p = '\0';
977  }
978 
979  *p++;
980  }
981 
982  // Replace newlines, question marks and colons in strings
983  for( i = 0; i < cnt; i++ )
984  {
985  p = params[i];
986  q = params[i];
987 
988  while( *p != '\0' )
989  {
990  if( *p == '\\' && *(p + 1) == 'n' )
991  {
992  p += 2;
993  *(q++) = '\n';
994  }
995  else if( *p == '\\' && *(p + 1) == ':' )
996  {
997  p += 2;
998  *(q++) = ':';
999  }
1000  else if( *p == '\\' && *(p + 1) == '?' )
1001  {
1002  p += 2;
1003  *(q++) = '?';
1004  }
1005  else
1006  *(q++) = *(p++);
1007  }
1008  *q = '\0';
1009  }
1010 
1011  return( cnt );
1012 }
1013 
1014 int main()
1015 {
1016  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1017  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_mdx.data";
1018  FILE *file;
1019  char buf[5000];
1020  char *params[50];
1021 
1022 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1023  unsigned char alloc_buf[1000000];
1024  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1025 #endif
1026 
1027  file = fopen( filename, "r" );
1028  if( file == NULL )
1029  {
1030  fprintf( stderr, "Failed to open\n" );
1031  return( 1 );
1032  }
1033 
1034  while( !feof( file ) )
1035  {
1036  int skip = 0;
1037 
1038  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1039  break;
1040  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1041  fprintf( stdout, " " );
1042  for( i = strlen( buf ) + 1; i < 67; i++ )
1043  fprintf( stdout, "." );
1044  fprintf( stdout, " " );
1045  fflush( stdout );
1046 
1047  total_tests++;
1048 
1049  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1050  break;
1051  cnt = parse_arguments( buf, strlen(buf), params );
1052 
1053  if( strcmp( params[0], "depends_on" ) == 0 )
1054  {
1055  for( i = 1; i < cnt; i++ )
1056  if( dep_check( params[i] ) != 0 )
1057  skip = 1;
1058 
1059  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1060  break;
1061  cnt = parse_arguments( buf, strlen(buf), params );
1062  }
1063 
1064  if( skip == 0 )
1065  {
1066  test_errors = 0;
1067  ret = dispatch_test( cnt, params );
1068  }
1069 
1070  if( skip == 1 || ret == 3 )
1071  {
1072  total_skipped++;
1073  fprintf( stdout, "----\n" );
1074  fflush( stdout );
1075  }
1076  else if( ret == 0 && test_errors == 0 )
1077  {
1078  fprintf( stdout, "PASS\n" );
1079  fflush( stdout );
1080  }
1081  else if( ret == 2 )
1082  {
1083  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1084  fclose(file);
1085  exit( 2 );
1086  }
1087  else
1088  total_errors++;
1089 
1090  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1091  break;
1092  if( strlen(buf) != 0 )
1093  {
1094  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1095  return( 1 );
1096  }
1097  }
1098  fclose(file);
1099 
1100  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1101  if( total_errors == 0 )
1102  fprintf( stdout, "PASSED" );
1103  else
1104  fprintf( stdout, "FAILED" );
1105 
1106  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1107  total_tests - total_errors, total_tests, total_skipped );
1108 
1109 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1110 #if defined(POLARSSL_MEMORY_DEBUG)
1111  memory_buffer_alloc_status();
1112 #endif
1113  memory_buffer_alloc_free();
1114 #endif
1115 
1116  return( total_errors != 0 );
1117 }
1118 
1119