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