PolarSSL v1.3.2
test_suite_base64.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_BASE64_C
4 
5 #include <polarssl/base64.h>
6 #endif /* POLARSSL_BASE64_C */
7 
8 
9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
10 #include "polarssl/memory.h"
11 #endif
12 
13 #if defined(WANT_NOT_RND_MPI)
14 #if defined(POLARSSL_BIGNUM_C)
15 #include "polarssl/bignum.h"
16 #else
17 #error "not_rnd_mpi() need bignum.c"
18 #endif
19 #endif
20 
21 #ifdef _MSC_VER
22 #include <basetsd.h>
23 typedef UINT32 uint32_t;
24 #else
25 #include <inttypes.h>
26 #endif
27 
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 /*
33  * 32-bit integer manipulation macros (big endian)
34  */
35 #ifndef GET_UINT32_BE
36 #define GET_UINT32_BE(n,b,i) \
37 { \
38  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
39  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
40  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
41  | ( (uint32_t) (b)[(i) + 3] ); \
42 }
43 #endif
44 
45 #ifndef PUT_UINT32_BE
46 #define PUT_UINT32_BE(n,b,i) \
47 { \
48  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
49  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
50  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
51  (b)[(i) + 3] = (unsigned char) ( (n) ); \
52 }
53 #endif
54 
55 static int unhexify(unsigned char *obuf, const char *ibuf)
56 {
57  unsigned char c, c2;
58  int len = strlen(ibuf) / 2;
59  assert(!(strlen(ibuf) %1)); // must be even number of bytes
60 
61  while (*ibuf != 0)
62  {
63  c = *ibuf++;
64  if( c >= '0' && c <= '9' )
65  c -= '0';
66  else if( c >= 'a' && c <= 'f' )
67  c -= 'a' - 10;
68  else if( c >= 'A' && c <= 'F' )
69  c -= 'A' - 10;
70  else
71  assert( 0 );
72 
73  c2 = *ibuf++;
74  if( c2 >= '0' && c2 <= '9' )
75  c2 -= '0';
76  else if( c2 >= 'a' && c2 <= 'f' )
77  c2 -= 'a' - 10;
78  else if( c2 >= 'A' && c2 <= 'F' )
79  c2 -= 'A' - 10;
80  else
81  assert( 0 );
82 
83  *obuf++ = ( c << 4 ) | c2;
84  }
85 
86  return len;
87 }
88 
89 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
90 {
91  unsigned char l, h;
92 
93  while (len != 0)
94  {
95  h = (*ibuf) / 16;
96  l = (*ibuf) % 16;
97 
98  if( h < 10 )
99  *obuf++ = '0' + h;
100  else
101  *obuf++ = 'a' + h - 10;
102 
103  if( l < 10 )
104  *obuf++ = '0' + l;
105  else
106  *obuf++ = 'a' + l - 10;
107 
108  ++ibuf;
109  len--;
110  }
111 }
112 
122 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
123 {
124  size_t i;
125 
126  if( rng_state != NULL )
127  rng_state = NULL;
128 
129  for( i = 0; i < len; ++i )
130  output[i] = rand();
131 
132  return( 0 );
133 }
134 
140 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
141 {
142  if( rng_state != NULL )
143  rng_state = NULL;
144 
145  memset( output, 0, len );
146 
147  return( 0 );
148 }
149 
150 typedef struct
151 {
152  unsigned char *buf;
153  size_t length;
154 } rnd_buf_info;
155 
167 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
168 {
169  rnd_buf_info *info = (rnd_buf_info *) rng_state;
170  size_t use_len;
171 
172  if( rng_state == NULL )
173  return( rnd_std_rand( NULL, output, len ) );
174 
175  use_len = len;
176  if( len > info->length )
177  use_len = info->length;
178 
179  if( use_len )
180  {
181  memcpy( output, info->buf, use_len );
182  info->buf += use_len;
183  info->length -= use_len;
184  }
185 
186  if( len - use_len > 0 )
187  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
188 
189  return( 0 );
190 }
191 
199 typedef struct
200 {
201  uint32_t key[16];
202  uint32_t v0, v1;
204 
213 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
214 {
215  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
216  uint32_t i, *k, sum, delta=0x9E3779B9;
217  unsigned char result[4];
218 
219  if( rng_state == NULL )
220  return( rnd_std_rand( NULL, output, len ) );
221 
222  k = info->key;
223 
224  while( len > 0 )
225  {
226  size_t use_len = ( len > 4 ) ? 4 : len;
227  sum = 0;
228 
229  for( i = 0; i < 32; i++ )
230  {
231  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
232  sum += delta;
233  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
234  }
235 
236  PUT_UINT32_BE( info->v0, result, 0 );
237  memcpy( output, result, use_len );
238  len -= use_len;
239  }
240 
241  return( 0 );
242 }
243 
244 #if defined(WANT_NOT_RND_MPI)
245 
253 #define ciL (sizeof(t_uint)) /* chars in limb */
254 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
255 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
256 {
257  char *str = (char *) in;
258  mpi X;
259 
260  /*
261  * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
262  * just reconstruct the rest in order to be able to call mpi_read_string()
263  */
264  X.s = 1;
265  X.p = (t_uint *) out;
266  X.n = CHARS_TO_LIMBS( len );
267 
268  /*
269  * If str is too long, mpi_read_string() will try to allocate a new buffer
270  * for X.p, which we want to avoid at all costs.
271  */
272  assert( strlen( str ) / 2 == len );
273 
274  return( mpi_read_string( &X, 16, str ) );
275 }
276 #endif /* WANT_NOT_RND_MPI */
277 
278 
279 #include <stdio.h>
280 #include <string.h>
281 
282 static int test_errors = 0;
283 
284 #ifdef POLARSSL_BASE64_C
285 
286 #define TEST_SUITE_ACTIVE
287 
288 static int test_assert( int correct, char *test )
289 {
290  if( correct )
291  return( 0 );
292 
293  test_errors++;
294  if( test_errors == 1 )
295  printf( "FAILED\n" );
296  printf( " %s\n", test );
297 
298  return( 1 );
299 }
300 
301 #define TEST_ASSERT( TEST ) \
302  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
303  if( test_errors) return; \
304  } while (0)
305 
306 int verify_string( char **str )
307 {
308  if( (*str)[0] != '"' ||
309  (*str)[strlen( *str ) - 1] != '"' )
310  {
311  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
312  return( -1 );
313  }
314 
315  (*str)++;
316  (*str)[strlen( *str ) - 1] = '\0';
317 
318  return( 0 );
319 }
320 
321 int verify_int( char *str, int *value )
322 {
323  size_t i;
324  int minus = 0;
325  int digits = 1;
326  int hex = 0;
327 
328  for( i = 0; i < strlen( str ); i++ )
329  {
330  if( i == 0 && str[i] == '-' )
331  {
332  minus = 1;
333  continue;
334  }
335 
336  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
337  str[i - 1] == '0' && str[i] == 'x' )
338  {
339  hex = 1;
340  continue;
341  }
342 
343  if( str[i] < '0' || str[i] > '9' )
344  {
345  digits = 0;
346  break;
347  }
348  }
349 
350  if( digits )
351  {
352  if( hex )
353  *value = strtol( str, NULL, 16 );
354  else
355  *value = strtol( str, NULL, 10 );
356 
357  return( 0 );
358  }
359 
360  if( strcmp( str, "POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL" ) == 0 )
361  {
363  return( 0 );
364  }
365  if( strcmp( str, "POLARSSL_ERR_BASE64_INVALID_CHARACTER" ) == 0 )
366  {
368  return( 0 );
369  }
370 
371 
372  printf( "Expected integer for parameter and got: %s\n", str );
373  return( -1 );
374 }
375 
376 void test_suite_base64_encode( char *src_string, char *dst_string, int dst_buf_size,
377  int result )
378 {
379  unsigned char src_str[1000];
380  unsigned char dst_str[1000];
381  size_t len = dst_buf_size;
382 
383  memset(src_str, 0x00, 1000);
384  memset(dst_str, 0x00, 1000);
385 
386  strcpy( (char *) src_str, src_string );
387  TEST_ASSERT( base64_encode( dst_str, &len, src_str, strlen( (char *) src_str ) ) == result );
388  if( result == 0 )
389  {
390  TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
391  }
392 }
393 
394 void test_suite_base64_decode( char *src_string, char *dst_string, int result )
395 {
396  unsigned char src_str[1000];
397  unsigned char dst_str[1000];
398  size_t len = 1000;
399  int res;
400 
401  memset(src_str, 0x00, 1000);
402  memset(dst_str, 0x00, 1000);
403 
404  strcpy( (char *) src_str, src_string );
405  TEST_ASSERT( res = base64_decode( dst_str, &len, src_str, strlen( (char *) src_str ) ) == result );
406  if( result == 0 )
407  {
408  TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
409  }
410 }
411 
412 #ifdef POLARSSL_SELF_TEST
413 void test_suite_base64_selftest()
414 {
415  TEST_ASSERT( base64_self_test( 0 ) == 0 );
416 }
417 #endif /* POLARSSL_SELF_TEST */
418 
419 
420 #endif /* POLARSSL_BASE64_C */
421 
422 
423 int dep_check( char *str )
424 {
425  if( str == NULL )
426  return( 1 );
427 
428  if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
429  {
430 #if defined(POLARSSL_SELF_TEST)
431  return( 0 );
432 #else
433  return( 1 );
434 #endif
435  }
436 
437 
438  return( 1 );
439 }
440 
441 int dispatch_test(int cnt, char *params[50])
442 {
443  int ret;
444  ((void) cnt);
445  ((void) params);
446 
447 #if defined(TEST_SUITE_ACTIVE)
448  if( strcmp( params[0], "base64_encode" ) == 0 )
449  {
450 
451  char *param1 = params[1];
452  char *param2 = params[2];
453  int param3;
454  int param4;
455 
456  if( cnt != 5 )
457  {
458  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
459  return( 2 );
460  }
461 
462  if( verify_string( &param1 ) != 0 ) return( 2 );
463  if( verify_string( &param2 ) != 0 ) return( 2 );
464  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
465  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
466 
467  test_suite_base64_encode( param1, param2, param3, param4 );
468  return ( 0 );
469 
470  return ( 3 );
471  }
472  else
473  if( strcmp( params[0], "base64_decode" ) == 0 )
474  {
475 
476  char *param1 = params[1];
477  char *param2 = params[2];
478  int param3;
479 
480  if( cnt != 4 )
481  {
482  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
483  return( 2 );
484  }
485 
486  if( verify_string( &param1 ) != 0 ) return( 2 );
487  if( verify_string( &param2 ) != 0 ) return( 2 );
488  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
489 
490  test_suite_base64_decode( param1, param2, param3 );
491  return ( 0 );
492 
493  return ( 3 );
494  }
495  else
496  if( strcmp( params[0], "base64_selftest" ) == 0 )
497  {
498  #ifdef POLARSSL_SELF_TEST
499 
500 
501  if( cnt != 1 )
502  {
503  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
504  return( 2 );
505  }
506 
507 
508  test_suite_base64_selftest( );
509  return ( 0 );
510  #endif /* POLARSSL_SELF_TEST */
511 
512  return ( 3 );
513  }
514  else
515 
516  {
517  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
518  fflush( stdout );
519  return( 1 );
520  }
521 #else
522  return( 3 );
523 #endif
524  return( ret );
525 }
526 
527 int get_line( FILE *f, char *buf, size_t len )
528 {
529  char *ret;
530 
531  ret = fgets( buf, len, f );
532  if( ret == NULL )
533  return( -1 );
534 
535  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
536  buf[strlen(buf) - 1] = '\0';
537  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
538  buf[strlen(buf) - 1] = '\0';
539 
540  return( 0 );
541 }
542 
543 int parse_arguments( char *buf, size_t len, char *params[50] )
544 {
545  int cnt = 0, i;
546  char *cur = buf;
547  char *p = buf, *q;
548 
549  params[cnt++] = cur;
550 
551  while( *p != '\0' && p < buf + len )
552  {
553  if( *p == '\\' )
554  {
555  *p++;
556  *p++;
557  continue;
558  }
559  if( *p == ':' )
560  {
561  if( p + 1 < buf + len )
562  {
563  cur = p + 1;
564  params[cnt++] = cur;
565  }
566  *p = '\0';
567  }
568 
569  *p++;
570  }
571 
572  // Replace newlines, question marks and colons in strings
573  for( i = 0; i < cnt; i++ )
574  {
575  p = params[i];
576  q = params[i];
577 
578  while( *p != '\0' )
579  {
580  if( *p == '\\' && *(p + 1) == 'n' )
581  {
582  p += 2;
583  *(q++) = '\n';
584  }
585  else if( *p == '\\' && *(p + 1) == ':' )
586  {
587  p += 2;
588  *(q++) = ':';
589  }
590  else if( *p == '\\' && *(p + 1) == '?' )
591  {
592  p += 2;
593  *(q++) = '?';
594  }
595  else
596  *(q++) = *(p++);
597  }
598  *q = '\0';
599  }
600 
601  return( cnt );
602 }
603 
604 int main()
605 {
606  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
607  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_base64.data";
608  FILE *file;
609  char buf[5000];
610  char *params[50];
611 
612 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
613  unsigned char alloc_buf[1000000];
614  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
615 #endif
616 
617  file = fopen( filename, "r" );
618  if( file == NULL )
619  {
620  fprintf( stderr, "Failed to open\n" );
621  return( 1 );
622  }
623 
624  while( !feof( file ) )
625  {
626  int skip = 0;
627 
628  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
629  break;
630  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
631  fprintf( stdout, " " );
632  for( i = strlen( buf ) + 1; i < 67; i++ )
633  fprintf( stdout, "." );
634  fprintf( stdout, " " );
635  fflush( stdout );
636 
637  total_tests++;
638 
639  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
640  break;
641  cnt = parse_arguments( buf, strlen(buf), params );
642 
643  if( strcmp( params[0], "depends_on" ) == 0 )
644  {
645  for( i = 1; i < cnt; i++ )
646  if( dep_check( params[i] ) != 0 )
647  skip = 1;
648 
649  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
650  break;
651  cnt = parse_arguments( buf, strlen(buf), params );
652  }
653 
654  if( skip == 0 )
655  {
656  test_errors = 0;
657  ret = dispatch_test( cnt, params );
658  }
659 
660  if( skip == 1 || ret == 3 )
661  {
662  total_skipped++;
663  fprintf( stdout, "----\n" );
664  fflush( stdout );
665  }
666  else if( ret == 0 && test_errors == 0 )
667  {
668  fprintf( stdout, "PASS\n" );
669  fflush( stdout );
670  }
671  else if( ret == 2 )
672  {
673  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
674  fclose(file);
675  exit( 2 );
676  }
677  else
678  total_errors++;
679 
680  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
681  break;
682  if( strlen(buf) != 0 )
683  {
684  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
685  return( 1 );
686  }
687  }
688  fclose(file);
689 
690  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
691  if( total_errors == 0 )
692  fprintf( stdout, "PASSED" );
693  else
694  fprintf( stdout, "FAILED" );
695 
696  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
697  total_tests - total_errors, total_tests, total_skipped );
698 
699 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
700 #if defined(POLARSSL_MEMORY_DEBUG)
701  memory_buffer_alloc_status();
702 #endif
703  memory_buffer_alloc_free();
704 #endif
705 
706  return( total_errors != 0 );
707 }
708 
709 
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int base64_decode(unsigned char *dst, size_t *dlen, const unsigned char *src, size_t slen)
Decode a base64-formatted buffer.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
Info structure for the pseudo random function.
int s
Definition: bignum.h:173
Configuration options (set of defines)
static int unhexify(unsigned char *obuf, const char *ibuf)
MPI structure.
Definition: bignum.h:171
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
Multi-precision integer library.
int dep_check(char *str)
#define TEST_ASSERT(TEST)
int base64_self_test(int verbose)
Checkup routine.
#define POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL
Output buffer too small.
Definition: base64.h:32
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])
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
RFC 1521 base64 encoding/decoding.
t_uint * p
Definition: bignum.h:175
int verify_string(char **str)
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
unsigned char * buf
int verify_int(char *str, int *value)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
static int test_errors
#define PUT_UINT32_BE(n, b, i)
#define POLARSSL_ERR_BASE64_INVALID_CHARACTER
Invalid character in input.
Definition: base64.h:33
int get_line(FILE *f, char *buf, size_t len)
int base64_encode(unsigned char *dst, size_t *dlen, const unsigned char *src, size_t slen)
Encode a buffer into base64 format.