PolarSSL v1.3.9
test_suite_base64.c
Go to the documentation of this file.
1 #if !defined(POLARSSL_CONFIG_FILE)
2 #include <polarssl/config.h>
3 #else
4 #include POLARSSL_CONFIG_FILE
5 #endif
6 
7 #ifdef POLARSSL_BASE64_C
8 
9 #include <polarssl/base64.h>
10 #endif /* POLARSSL_BASE64_C */
11 
12 
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #include "polarssl/memory.h"
15 #endif
16 
17 #if defined(POLARSSL_PLATFORM_C)
18 #include "polarssl/platform.h"
19 #else
20 #define polarssl_malloc malloc
21 #define polarssl_free free
22 #endif
23 
24 #ifdef _MSC_VER
25 #include <basetsd.h>
26 typedef UINT32 uint32_t;
27 #else
28 #include <inttypes.h>
29 #endif
30 
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 /*
36  * 32-bit integer manipulation macros (big endian)
37  */
38 #ifndef GET_UINT32_BE
39 #define GET_UINT32_BE(n,b,i) \
40 { \
41  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44  | ( (uint32_t) (b)[(i) + 3] ); \
45 }
46 #endif
47 
48 #ifndef PUT_UINT32_BE
49 #define PUT_UINT32_BE(n,b,i) \
50 { \
51  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54  (b)[(i) + 3] = (unsigned char) ( (n) ); \
55 }
56 #endif
57 
58 static int unhexify(unsigned char *obuf, const char *ibuf)
59 {
60  unsigned char c, c2;
61  int len = strlen(ibuf) / 2;
62  assert(!(strlen(ibuf) %1)); // must be even number of bytes
63 
64  while (*ibuf != 0)
65  {
66  c = *ibuf++;
67  if( c >= '0' && c <= '9' )
68  c -= '0';
69  else if( c >= 'a' && c <= 'f' )
70  c -= 'a' - 10;
71  else if( c >= 'A' && c <= 'F' )
72  c -= 'A' - 10;
73  else
74  assert( 0 );
75 
76  c2 = *ibuf++;
77  if( c2 >= '0' && c2 <= '9' )
78  c2 -= '0';
79  else if( c2 >= 'a' && c2 <= 'f' )
80  c2 -= 'a' - 10;
81  else if( c2 >= 'A' && c2 <= 'F' )
82  c2 -= 'A' - 10;
83  else
84  assert( 0 );
85 
86  *obuf++ = ( c << 4 ) | c2;
87  }
88 
89  return len;
90 }
91 
92 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
93 {
94  unsigned char l, h;
95 
96  while (len != 0)
97  {
98  h = (*ibuf) / 16;
99  l = (*ibuf) % 16;
100 
101  if( h < 10 )
102  *obuf++ = '0' + h;
103  else
104  *obuf++ = 'a' + h - 10;
105 
106  if( l < 10 )
107  *obuf++ = '0' + l;
108  else
109  *obuf++ = 'a' + l - 10;
110 
111  ++ibuf;
112  len--;
113  }
114 }
115 
123 static unsigned char *zero_alloc( size_t len )
124 {
125  void *p;
126  size_t actual_len = len != 0 ? len : 1;
127 
128  p = polarssl_malloc( actual_len );
129  assert( p != NULL );
130 
131  memset( p, 0x00, actual_len );
132 
133  return( p );
134 }
135 
146 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
147 {
148  unsigned char *obuf;
149 
150  *olen = strlen(ibuf) / 2;
151 
152  if( *olen == 0 )
153  return( zero_alloc( *olen ) );
154 
155  obuf = polarssl_malloc( *olen );
156  assert( obuf != NULL );
157 
158  (void) unhexify( obuf, ibuf );
159 
160  return( obuf );
161 }
162 
172 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
173 {
174 #if !defined(__OpenBSD__)
175  size_t i;
176 
177  if( rng_state != NULL )
178  rng_state = NULL;
179 
180  for( i = 0; i < len; ++i )
181  output[i] = rand();
182 #else
183  if( rng_state != NULL )
184  rng_state = NULL;
185 
186  arc4random_buf( output, len );
187 #endif /* !OpenBSD */
188 
189  return( 0 );
190 }
191 
197 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
198 {
199  if( rng_state != NULL )
200  rng_state = NULL;
201 
202  memset( output, 0, len );
203 
204  return( 0 );
205 }
206 
207 typedef struct
208 {
209  unsigned char *buf;
210  size_t length;
211 } rnd_buf_info;
212 
224 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
225 {
226  rnd_buf_info *info = (rnd_buf_info *) rng_state;
227  size_t use_len;
228 
229  if( rng_state == NULL )
230  return( rnd_std_rand( NULL, output, len ) );
231 
232  use_len = len;
233  if( len > info->length )
234  use_len = info->length;
235 
236  if( use_len )
237  {
238  memcpy( output, info->buf, use_len );
239  info->buf += use_len;
240  info->length -= use_len;
241  }
242 
243  if( len - use_len > 0 )
244  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
245 
246  return( 0 );
247 }
248 
256 typedef struct
257 {
258  uint32_t key[16];
259  uint32_t v0, v1;
261 
270 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
271 {
272  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
273  uint32_t i, *k, sum, delta=0x9E3779B9;
274  unsigned char result[4], *out = output;
275 
276  if( rng_state == NULL )
277  return( rnd_std_rand( NULL, output, len ) );
278 
279  k = info->key;
280 
281  while( len > 0 )
282  {
283  size_t use_len = ( len > 4 ) ? 4 : len;
284  sum = 0;
285 
286  for( i = 0; i < 32; i++ )
287  {
288  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
289  sum += delta;
290  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
291  }
292 
293  PUT_UINT32_BE( info->v0, result, 0 );
294  memcpy( out, result, use_len );
295  len -= use_len;
296  out += 4;
297  }
298 
299  return( 0 );
300 }
301 
302 
303 #include <stdio.h>
304 #include <string.h>
305 
306 #if defined(POLARSSL_PLATFORM_C)
307 #include "polarssl/platform.h"
308 #else
309 #define polarssl_printf printf
310 #define polarssl_malloc malloc
311 #define polarssl_free free
312 #endif
313 
314 static int test_errors = 0;
315 
316 #ifdef POLARSSL_BASE64_C
317 
318 #define TEST_SUITE_ACTIVE
319 
320 static int test_assert( int correct, const char *test )
321 {
322  if( correct )
323  return( 0 );
324 
325  test_errors++;
326  if( test_errors == 1 )
327  printf( "FAILED\n" );
328  printf( " %s\n", test );
329 
330  return( 1 );
331 }
332 
333 #define TEST_ASSERT( TEST ) \
334  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
335  if( test_errors) goto exit; \
336  } while (0)
337 
338 int verify_string( char **str )
339 {
340  if( (*str)[0] != '"' ||
341  (*str)[strlen( *str ) - 1] != '"' )
342  {
343  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
344  return( -1 );
345  }
346 
347  (*str)++;
348  (*str)[strlen( *str ) - 1] = '\0';
349 
350  return( 0 );
351 }
352 
353 int verify_int( char *str, int *value )
354 {
355  size_t i;
356  int minus = 0;
357  int digits = 1;
358  int hex = 0;
359 
360  for( i = 0; i < strlen( str ); i++ )
361  {
362  if( i == 0 && str[i] == '-' )
363  {
364  minus = 1;
365  continue;
366  }
367 
368  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369  str[i - 1] == '0' && str[i] == 'x' )
370  {
371  hex = 1;
372  continue;
373  }
374 
375  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
376  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
377  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
378  {
379  digits = 0;
380  break;
381  }
382  }
383 
384  if( digits )
385  {
386  if( hex )
387  *value = strtol( str, NULL, 16 );
388  else
389  *value = strtol( str, NULL, 10 );
390 
391  return( 0 );
392  }
393 
394  if( strcmp( str, "POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL" ) == 0 )
395  {
397  return( 0 );
398  }
399  if( strcmp( str, "POLARSSL_ERR_BASE64_INVALID_CHARACTER" ) == 0 )
400  {
402  return( 0 );
403  }
404 
405 
406  printf( "Expected integer for parameter and got: %s\n", str );
407  return( -1 );
408 }
409 
410 void test_suite_base64_encode( char *src_string, char *dst_string, int dst_buf_size,
411  int result )
412 {
413  unsigned char src_str[1000];
414  unsigned char dst_str[1000];
415  size_t len = dst_buf_size;
416 
417  memset(src_str, 0x00, 1000);
418  memset(dst_str, 0x00, 1000);
419 
420  strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
421  TEST_ASSERT( base64_encode( dst_str, &len, src_str, strlen( (char *) src_str ) ) == result );
422  if( result == 0 )
423  {
424  TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
425  }
426 
427 exit:
428  return;
429 }
430 
431 void test_suite_base64_decode( char *src_string, char *dst_string, int result )
432 {
433  unsigned char src_str[1000];
434  unsigned char dst_str[1000];
435  size_t len = 1000;
436  int res;
437 
438  memset(src_str, 0x00, 1000);
439  memset(dst_str, 0x00, 1000);
440 
441  strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
442  res = base64_decode( dst_str, &len, src_str, strlen( (char *) src_str ) );
443  TEST_ASSERT( res == result );
444  if( result == 0 )
445  {
446  TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
447  }
448 
449 exit:
450  return;
451 }
452 
453 void test_suite_base64_encode_hex( char *src_hex, char *dst, int dst_buf_size,
454  int result )
455 {
456  unsigned char *src = NULL, *res = NULL;
457  size_t len = dst_buf_size, src_len;
458 
459  src = unhexify_alloc( src_hex, &src_len );
460  res = zero_alloc( dst_buf_size );
461 
462  TEST_ASSERT( base64_encode( res, &len, src, src_len ) == result );
463  if( result == 0 )
464  {
465  TEST_ASSERT( len == strlen( dst ) );
466  TEST_ASSERT( memcmp( dst, res, len ) == 0 );
467  }
468 
469 exit:
470  polarssl_free( src );
471  polarssl_free( res );
472 }
473 
474 void test_suite_base64_decode_hex( char *src, char *dst_hex, int dst_buf_size,
475  int result )
476 {
477  unsigned char *dst = NULL, *res = NULL;
478  size_t len = dst_buf_size, dst_len;
479 
480  dst = unhexify_alloc( dst_hex, &dst_len );
481  res = zero_alloc( dst_buf_size );
482 
483  TEST_ASSERT( base64_decode( res, &len, (unsigned char *) src,
484  strlen( src ) ) == result );
485  if( result == 0 )
486  {
487  TEST_ASSERT( len == dst_len );
488  TEST_ASSERT( memcmp( dst, res, len ) == 0 );
489  }
490 
491 exit:
492  polarssl_free( dst );
493  polarssl_free( res );
494 }
495 
496 void test_suite_base64_decode_hex_src( char *src_hex, char *dst_ref, int result )
497 {
498  unsigned char dst[1000] = { 0 };
499  unsigned char *src;
500  size_t src_len, len;
501 
502  src = unhexify_alloc( src_hex, &src_len );
503 
504  len = sizeof( dst );
505  TEST_ASSERT( base64_decode( dst, &len, src, src_len ) == result );
506  if( result == 0 )
507  {
508  TEST_ASSERT( len == strlen( dst_ref ) );
509  TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
510  }
511 
512 exit:
513  polarssl_free( src );
514 }
515 
516 #ifdef POLARSSL_SELF_TEST
517 void test_suite_base64_selftest()
518 {
519  TEST_ASSERT( base64_self_test( 0 ) == 0 );
520 
521 exit:
522  return;
523 }
524 #endif /* POLARSSL_SELF_TEST */
525 
526 
527 #endif /* POLARSSL_BASE64_C */
528 
529 
530 int dep_check( char *str )
531 {
532  if( str == NULL )
533  return( 1 );
534 
535  if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
536  {
537 #if defined(POLARSSL_SELF_TEST)
538  return( 0 );
539 #else
540  return( 1 );
541 #endif
542  }
543 
544 
545  return( 1 );
546 }
547 
548 int dispatch_test(int cnt, char *params[50])
549 {
550  int ret;
551  ((void) cnt);
552  ((void) params);
553 
554 #if defined(TEST_SUITE_ACTIVE)
555  if( strcmp( params[0], "base64_encode" ) == 0 )
556  {
557 
558  char *param1 = params[1];
559  char *param2 = params[2];
560  int param3;
561  int param4;
562 
563  if( cnt != 5 )
564  {
565  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
566  return( 2 );
567  }
568 
569  if( verify_string( &param1 ) != 0 ) return( 2 );
570  if( verify_string( &param2 ) != 0 ) return( 2 );
571  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
572  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
573 
574  test_suite_base64_encode( param1, param2, param3, param4 );
575  return ( 0 );
576 
577  return ( 3 );
578  }
579  else
580  if( strcmp( params[0], "base64_decode" ) == 0 )
581  {
582 
583  char *param1 = params[1];
584  char *param2 = params[2];
585  int param3;
586 
587  if( cnt != 4 )
588  {
589  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
590  return( 2 );
591  }
592 
593  if( verify_string( &param1 ) != 0 ) return( 2 );
594  if( verify_string( &param2 ) != 0 ) return( 2 );
595  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
596 
597  test_suite_base64_decode( param1, param2, param3 );
598  return ( 0 );
599 
600  return ( 3 );
601  }
602  else
603  if( strcmp( params[0], "base64_encode_hex" ) == 0 )
604  {
605 
606  char *param1 = params[1];
607  char *param2 = params[2];
608  int param3;
609  int param4;
610 
611  if( cnt != 5 )
612  {
613  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
614  return( 2 );
615  }
616 
617  if( verify_string( &param1 ) != 0 ) return( 2 );
618  if( verify_string( &param2 ) != 0 ) return( 2 );
619  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
620  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
621 
622  test_suite_base64_encode_hex( param1, param2, param3, param4 );
623  return ( 0 );
624 
625  return ( 3 );
626  }
627  else
628  if( strcmp( params[0], "base64_decode_hex" ) == 0 )
629  {
630 
631  char *param1 = params[1];
632  char *param2 = params[2];
633  int param3;
634  int param4;
635 
636  if( cnt != 5 )
637  {
638  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
639  return( 2 );
640  }
641 
642  if( verify_string( &param1 ) != 0 ) return( 2 );
643  if( verify_string( &param2 ) != 0 ) return( 2 );
644  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
645  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
646 
647  test_suite_base64_decode_hex( param1, param2, param3, param4 );
648  return ( 0 );
649 
650  return ( 3 );
651  }
652  else
653  if( strcmp( params[0], "base64_decode_hex_src" ) == 0 )
654  {
655 
656  char *param1 = params[1];
657  char *param2 = params[2];
658  int param3;
659 
660  if( cnt != 4 )
661  {
662  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
663  return( 2 );
664  }
665 
666  if( verify_string( &param1 ) != 0 ) return( 2 );
667  if( verify_string( &param2 ) != 0 ) return( 2 );
668  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
669 
670  test_suite_base64_decode_hex_src( param1, param2, param3 );
671  return ( 0 );
672 
673  return ( 3 );
674  }
675  else
676  if( strcmp( params[0], "base64_selftest" ) == 0 )
677  {
678  #ifdef POLARSSL_SELF_TEST
679 
680 
681  if( cnt != 1 )
682  {
683  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
684  return( 2 );
685  }
686 
687 
688  test_suite_base64_selftest( );
689  return ( 0 );
690  #endif /* POLARSSL_SELF_TEST */
691 
692  return ( 3 );
693  }
694  else
695 
696  {
697  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
698  fflush( stdout );
699  return( 1 );
700  }
701 #else
702  return( 3 );
703 #endif
704  return( ret );
705 }
706 
707 int get_line( FILE *f, char *buf, size_t len )
708 {
709  char *ret;
710 
711  ret = fgets( buf, len, f );
712  if( ret == NULL )
713  return( -1 );
714 
715  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
716  buf[strlen(buf) - 1] = '\0';
717  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
718  buf[strlen(buf) - 1] = '\0';
719 
720  return( 0 );
721 }
722 
723 int parse_arguments( char *buf, size_t len, char *params[50] )
724 {
725  int cnt = 0, i;
726  char *cur = buf;
727  char *p = buf, *q;
728 
729  params[cnt++] = cur;
730 
731  while( *p != '\0' && p < buf + len )
732  {
733  if( *p == '\\' )
734  {
735  p++;
736  p++;
737  continue;
738  }
739  if( *p == ':' )
740  {
741  if( p + 1 < buf + len )
742  {
743  cur = p + 1;
744  params[cnt++] = cur;
745  }
746  *p = '\0';
747  }
748 
749  p++;
750  }
751 
752  // Replace newlines, question marks and colons in strings
753  for( i = 0; i < cnt; i++ )
754  {
755  p = params[i];
756  q = params[i];
757 
758  while( *p != '\0' )
759  {
760  if( *p == '\\' && *(p + 1) == 'n' )
761  {
762  p += 2;
763  *(q++) = '\n';
764  }
765  else if( *p == '\\' && *(p + 1) == ':' )
766  {
767  p += 2;
768  *(q++) = ':';
769  }
770  else if( *p == '\\' && *(p + 1) == '?' )
771  {
772  p += 2;
773  *(q++) = '?';
774  }
775  else
776  *(q++) = *(p++);
777  }
778  *q = '\0';
779  }
780 
781  return( cnt );
782 }
783 
784 int main()
785 {
786  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
787  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_base64.data";
788  FILE *file;
789  char buf[5000];
790  char *params[50];
791 
792 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
793  unsigned char alloc_buf[1000000];
794  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
795 #endif
796 
797  file = fopen( filename, "r" );
798  if( file == NULL )
799  {
800  fprintf( stderr, "Failed to open\n" );
801  return( 1 );
802  }
803 
804  while( !feof( file ) )
805  {
806  int skip = 0;
807 
808  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
809  break;
810  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
811  fprintf( stdout, " " );
812  for( i = strlen( buf ) + 1; i < 67; i++ )
813  fprintf( stdout, "." );
814  fprintf( stdout, " " );
815  fflush( stdout );
816 
817  total_tests++;
818 
819  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
820  break;
821  cnt = parse_arguments( buf, strlen(buf), params );
822 
823  if( strcmp( params[0], "depends_on" ) == 0 )
824  {
825  for( i = 1; i < cnt; i++ )
826  if( dep_check( params[i] ) != 0 )
827  skip = 1;
828 
829  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
830  break;
831  cnt = parse_arguments( buf, strlen(buf), params );
832  }
833 
834  if( skip == 0 )
835  {
836  test_errors = 0;
837  ret = dispatch_test( cnt, params );
838  }
839 
840  if( skip == 1 || ret == 3 )
841  {
842  total_skipped++;
843  fprintf( stdout, "----\n" );
844  fflush( stdout );
845  }
846  else if( ret == 0 && test_errors == 0 )
847  {
848  fprintf( stdout, "PASS\n" );
849  fflush( stdout );
850  }
851  else if( ret == 2 )
852  {
853  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
854  fclose(file);
855  exit( 2 );
856  }
857  else
858  total_errors++;
859 
860  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
861  break;
862  if( strlen(buf) != 0 )
863  {
864  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
865  return( 1 );
866  }
867  }
868  fclose(file);
869 
870  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
871  if( total_errors == 0 )
872  fprintf( stdout, "PASSED" );
873  else
874  fprintf( stdout, "FAILED" );
875 
876  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
877  total_tests - total_errors, total_tests, total_skipped );
878 
879 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
880 #if defined(POLARSSL_MEMORY_DEBUG)
881  memory_buffer_alloc_status();
882 #endif
884 #endif
885 
886  return( total_errors != 0 );
887 }
888 
889