PolarSSL v1.3.1
test_suite_version.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_VERSION_C
4 
5 #include <polarssl/version.h>
6 #endif /* POLARSSL_VERSION_C */
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 #ifdef POLARSSL_VERSION_C
289 
290 #define TEST_SUITE_ACTIVE
291 
292 static int test_assert( int correct, char *test )
293 {
294  if( correct )
295  return( 0 );
296 
297  test_errors++;
298  if( test_errors == 1 )
299  printf( "FAILED\n" );
300  printf( " %s\n", test );
301 
302  return( 1 );
303 }
304 
305 #define TEST_ASSERT( TEST ) \
306  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307  if( test_errors) return; \
308  } while (0)
309 
310 int verify_string( char **str )
311 {
312  if( (*str)[0] != '"' ||
313  (*str)[strlen( *str ) - 1] != '"' )
314  {
315  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
316  return( -1 );
317  }
318 
319  (*str)++;
320  (*str)[strlen( *str ) - 1] = '\0';
321 
322  return( 0 );
323 }
324 
325 int verify_int( char *str, int *value )
326 {
327  size_t i;
328  int minus = 0;
329  int digits = 1;
330  int hex = 0;
331 
332  for( i = 0; i < strlen( str ); i++ )
333  {
334  if( i == 0 && str[i] == '-' )
335  {
336  minus = 1;
337  continue;
338  }
339 
340  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341  str[i - 1] == '0' && str[i] == 'x' )
342  {
343  hex = 1;
344  continue;
345  }
346 
347  if( str[i] < '0' || str[i] > '9' )
348  {
349  digits = 0;
350  break;
351  }
352  }
353 
354  if( digits )
355  {
356  if( hex )
357  *value = strtol( str, NULL, 16 );
358  else
359  *value = strtol( str, NULL, 10 );
360 
361  return( 0 );
362  }
363 
364 
365 
366  printf( "Expected integer for parameter and got: %s\n", str );
367  return( -1 );
368 }
369 
370 void test_suite_check_compiletime_version( char *version_str )
371 {
372  char build_str[100];
373  char build_str_full[100];
374  unsigned int build_int;
375 
376  memset( build_str, 0, 100 );
377  memset( build_str_full, 0, 100 );
378 
379  snprintf (build_str, 100, "%d.%d.%d", POLARSSL_VERSION_MAJOR,
381 
382  snprintf( build_str_full, 100, "PolarSSL %d.%d.%d", POLARSSL_VERSION_MAJOR,
384 
385  build_int = POLARSSL_VERSION_MAJOR << 24 |
386  POLARSSL_VERSION_MINOR << 16 |
388 
389  TEST_ASSERT( build_int == POLARSSL_VERSION_NUMBER );
390  TEST_ASSERT( strcmp( build_str, POLARSSL_VERSION_STRING ) == 0 );
391  TEST_ASSERT( strcmp( build_str_full, POLARSSL_VERSION_STRING_FULL ) == 0 );
392  TEST_ASSERT( strcmp( version_str, POLARSSL_VERSION_STRING ) == 0 );
393 }
394 
395 void test_suite_check_runtime_version( char *version_str )
396 {
397  char build_str[100];
398  char get_str[100];
399  char build_str_full[100];
400  char get_str_full[100];
401  unsigned int get_int;
402 
403  memset( build_str, 0, 100 );
404  memset( get_str, 0, 100 );
405  memset( build_str_full, 0, 100 );
406  memset( get_str_full, 0, 100 );
407 
408  get_int = version_get_number();
409  version_get_string( get_str );
410  version_get_string_full( get_str_full );
411 
412  snprintf( build_str, 100, "%d.%d.%d",
413  (get_int >> 24) & 0xFF,
414  (get_int >> 16) & 0xFF,
415  (get_int >> 8) & 0xFF );
416  snprintf( build_str_full, 100, "PolarSSL %s", version_str );
417 
418  TEST_ASSERT( strcmp( build_str, version_str ) == 0 );
419  TEST_ASSERT( strcmp( build_str_full, get_str_full ) == 0 );
420  TEST_ASSERT( strcmp( version_str, get_str ) == 0 );
421 }
422 
423 
424 #endif /* POLARSSL_VERSION_C */
425 
426 
427 int dep_check( char *str )
428 {
429  if( str == NULL )
430  return( 1 );
431 
432 
433 
434  return( 1 );
435 }
436 
437 int dispatch_test(int cnt, char *params[50])
438 {
439  int ret;
440  ((void) cnt);
441  ((void) params);
442 
443 #if defined(TEST_SUITE_ACTIVE)
444  if( strcmp( params[0], "check_compiletime_version" ) == 0 )
445  {
446 
447  char *param1 = params[1];
448 
449  if( cnt != 2 )
450  {
451  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
452  return( 2 );
453  }
454 
455  if( verify_string( &param1 ) != 0 ) return( 2 );
456 
457  test_suite_check_compiletime_version( param1 );
458  return ( 0 );
459 
460  return ( 3 );
461  }
462  else
463  if( strcmp( params[0], "check_runtime_version" ) == 0 )
464  {
465 
466  char *param1 = params[1];
467 
468  if( cnt != 2 )
469  {
470  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
471  return( 2 );
472  }
473 
474  if( verify_string( &param1 ) != 0 ) return( 2 );
475 
476  test_suite_check_runtime_version( param1 );
477  return ( 0 );
478 
479  return ( 3 );
480  }
481  else
482 
483  {
484  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
485  fflush( stdout );
486  return( 1 );
487  }
488 #else
489  return( 3 );
490 #endif
491  return( ret );
492 }
493 
494 int get_line( FILE *f, char *buf, size_t len )
495 {
496  char *ret;
497 
498  ret = fgets( buf, len, f );
499  if( ret == NULL )
500  return( -1 );
501 
502  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
503  buf[strlen(buf) - 1] = '\0';
504  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
505  buf[strlen(buf) - 1] = '\0';
506 
507  return( 0 );
508 }
509 
510 int parse_arguments( char *buf, size_t len, char *params[50] )
511 {
512  int cnt = 0, i;
513  char *cur = buf;
514  char *p = buf, *q;
515 
516  params[cnt++] = cur;
517 
518  while( *p != '\0' && p < buf + len )
519  {
520  if( *p == '\\' )
521  {
522  *p++;
523  *p++;
524  continue;
525  }
526  if( *p == ':' )
527  {
528  if( p + 1 < buf + len )
529  {
530  cur = p + 1;
531  params[cnt++] = cur;
532  }
533  *p = '\0';
534  }
535 
536  *p++;
537  }
538 
539  // Replace newlines, question marks and colons in strings
540  for( i = 0; i < cnt; i++ )
541  {
542  p = params[i];
543  q = params[i];
544 
545  while( *p != '\0' )
546  {
547  if( *p == '\\' && *(p + 1) == 'n' )
548  {
549  p += 2;
550  *(q++) = '\n';
551  }
552  else if( *p == '\\' && *(p + 1) == ':' )
553  {
554  p += 2;
555  *(q++) = ':';
556  }
557  else if( *p == '\\' && *(p + 1) == '?' )
558  {
559  p += 2;
560  *(q++) = '?';
561  }
562  else
563  *(q++) = *(p++);
564  }
565  *q = '\0';
566  }
567 
568  return( cnt );
569 }
570 
571 int main()
572 {
573  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
574  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_version.data";
575  FILE *file;
576  char buf[5000];
577  char *params[50];
578 
579 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
580  unsigned char alloc_buf[1000000];
581  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
582 #endif
583 
584  file = fopen( filename, "r" );
585  if( file == NULL )
586  {
587  fprintf( stderr, "Failed to open\n" );
588  return( 1 );
589  }
590 
591  while( !feof( file ) )
592  {
593  int skip = 0;
594 
595  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
596  break;
597  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
598  fprintf( stdout, " " );
599  for( i = strlen( buf ) + 1; i < 67; i++ )
600  fprintf( stdout, "." );
601  fprintf( stdout, " " );
602  fflush( stdout );
603 
604  total_tests++;
605 
606  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
607  break;
608  cnt = parse_arguments( buf, strlen(buf), params );
609 
610  if( strcmp( params[0], "depends_on" ) == 0 )
611  {
612  for( i = 1; i < cnt; i++ )
613  if( dep_check( params[i] ) != 0 )
614  skip = 1;
615 
616  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
617  break;
618  cnt = parse_arguments( buf, strlen(buf), params );
619  }
620 
621  if( skip == 0 )
622  {
623  test_errors = 0;
624  ret = dispatch_test( cnt, params );
625  }
626 
627  if( skip == 1 || ret == 3 )
628  {
629  total_skipped++;
630  fprintf( stdout, "----\n" );
631  fflush( stdout );
632  }
633  else if( ret == 0 && test_errors == 0 )
634  {
635  fprintf( stdout, "PASS\n" );
636  fflush( stdout );
637  }
638  else if( ret == 2 )
639  {
640  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
641  fclose(file);
642  exit( 2 );
643  }
644  else
645  total_errors++;
646 
647  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
648  break;
649  if( strlen(buf) != 0 )
650  {
651  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
652  return( 1 );
653  }
654  }
655  fclose(file);
656 
657  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
658  if( total_errors == 0 )
659  fprintf( stdout, "PASSED" );
660  else
661  fprintf( stdout, "FAILED" );
662 
663  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
664  total_tests - total_errors, total_tests, total_skipped );
665 
666 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
667 #if defined(POLARSSL_MEMORY_DEBUG)
668  memory_buffer_alloc_status();
669 #endif
670  memory_buffer_alloc_free();
671 #endif
672 
673  return( total_errors != 0 );
674 }
675 
676