blob: f03f40c21a2e0c9312fe1a6c2e6826987542d446 [file] [log] [blame]
Mohammad Azim Khan95402612017-07-19 10:15:54 +01001#line 2 "suites/host_test.function"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01002
3/**
Azim Khan8d686bf2018-07-04 23:29:46 +01004 * \brief Verifies that string is in string parameter format i.e. "<str>"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01005 * It also strips enclosing '"' from the input string.
6 *
7 * \param str String parameter.
8 *
9 * \return 0 if success else 1
10 */
11int verify_string( char **str )
12{
13 if( (*str)[0] != '"' ||
14 (*str)[strlen( *str ) - 1] != '"' )
15 {
16 mbedtls_fprintf( stderr,
17 "Expected string (with \"\") for parameter and got: %s\n", *str );
18 return( -1 );
19 }
20
Azim Khan8d686bf2018-07-04 23:29:46 +010021 ( *str )++;
22 ( *str )[strlen( *str ) - 1] = '\0';
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010023
24 return( 0 );
25}
26
27/**
Azim Khan8d686bf2018-07-04 23:29:46 +010028 * \brief Verifies that string is an integer. Also gives the converted
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010029 * integer value.
30 *
31 * \param str Input string.
32 * \param value Pointer to int for output value.
33 *
34 * \return 0 if success else 1
35 */
36int verify_int( char *str, int *value )
37{
38 size_t i;
39 int minus = 0;
40 int digits = 1;
41 int hex = 0;
42
43 for( i = 0; i < strlen( str ); i++ )
44 {
45 if( i == 0 && str[i] == '-' )
46 {
47 minus = 1;
48 continue;
49 }
50
51 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
52 str[i - 1] == '0' && str[i] == 'x' )
53 {
54 hex = 1;
55 continue;
56 }
57
58 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
59 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
60 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
61 {
62 digits = 0;
63 break;
64 }
65 }
66
67 if( digits )
68 {
69 if( hex )
70 *value = strtol( str, NULL, 16 );
71 else
72 *value = strtol( str, NULL, 10 );
73
74 return( 0 );
75 }
76
77 mbedtls_fprintf( stderr,
78 "Expected integer for parameter and got: %s\n", str );
79 return( KEY_VALUE_MAPPING_NOT_FOUND );
80}
81
82
83/**
84 * \brief Usage string.
85 *
86 */
87#define USAGE \
88 "Usage: %s [OPTIONS] files...\n\n" \
89 " Command line arguments:\n" \
90 " files... One or more test data file. If no file is specified\n" \
91 " the followimg default test case is used:\n" \
92 " %s\n\n" \
93 " Options:\n" \
94 " -v | --verbose Display full information about each test\n" \
95 " -h | --help Display this information\n\n", \
96 argv[0], \
97 "TESTCASE_FILENAME"
98
99
100/**
101 * \brief Read a line from the passed file pointer.
102 *
103 * \param f FILE pointer
104 * \param buf Pointer to memory to hold read line.
105 * \param len Length of the buf.
106 *
107 * \return 0 if success else -1
108 */
109int get_line( FILE *f, char *buf, size_t len )
110{
111 char *ret;
112 int i = 0, str_len = 0, has_string = 0;
113
114 /* Read until we get a valid line */
115 do
116 {
117 ret = fgets( buf, len, f );
118 if( ret == NULL )
119 return( -1 );
120
121 str_len = strlen( buf );
122
123 /* Skip empty line and comment */
124 if ( str_len == 0 || buf[0] == '#' )
125 continue;
126 has_string = 0;
127 for ( i = 0; i < str_len; i++ )
128 {
129 char c = buf[i];
130 if ( c != ' ' && c != '\t' && c != '\n' &&
131 c != '\v' && c != '\f' && c != '\r' )
132 {
133 has_string = 1;
134 break;
135 }
136 }
137 } while( !has_string );
138
139 /* Strip new line and carriage return */
140 ret = buf + strlen( buf );
141 if( ret-- > buf && *ret == '\n' )
142 *ret = '\0';
143 if( ret-- > buf && *ret == '\r' )
144 *ret = '\0';
145
146 return( 0 );
147}
148
149/**
150 * \brief Splits string delimited by ':'. Ignores '\:'.
151 *
152 * \param buf Input string
153 * \param len Input string length
154 * \param params Out params found
155 * \param params_len Out params array len
156 *
157 * \return Count of strings found.
158 */
159static int parse_arguments( char *buf, size_t len, char **params,
160 size_t params_len )
161{
162 size_t cnt = 0, i;
163 char *cur = buf;
164 char *p = buf, *q;
165
166 params[cnt++] = cur;
167
168 while( *p != '\0' && p < buf + len )
169 {
170 if( *p == '\\' )
171 {
172 p++;
173 p++;
174 continue;
175 }
176 if( *p == ':' )
177 {
178 if( p + 1 < buf + len )
179 {
180 cur = p + 1;
181 assert( cnt < params_len );
182 params[cnt++] = cur;
183 }
184 *p = '\0';
185 }
186
187 p++;
188 }
189
190 /* Replace newlines, question marks and colons in strings */
191 for( i = 0; i < cnt; i++ )
192 {
193 p = params[i];
194 q = params[i];
195
196 while( *p != '\0' )
197 {
198 if( *p == '\\' && *(p + 1) == 'n' )
199 {
200 p += 2;
201 *(q++) = '\n';
202 }
203 else if( *p == '\\' && *(p + 1) == ':' )
204 {
205 p += 2;
206 *(q++) = ':';
207 }
208 else if( *p == '\\' && *(p + 1) == '?' )
209 {
210 p += 2;
211 *(q++) = '?';
212 }
213 else
214 *(q++) = *(p++);
215 }
216 *q = '\0';
217 }
218
219 return( cnt );
220}
221
222/**
223 * \brief Converts parameters into test function consumable parameters.
224 * Example: Input: {"int", "0", "char*", "Hello",
225 * "hex", "abef", "exp", "1"}
226 * Output: {
227 * 0, // Verified int
228 * "Hello", // Verified string
229 * 2, { 0xab, 0xef },// Converted len,hex pair
230 * 9600 // Evaluated expression
231 * }
232 *
233 *
234 * \param cnt Input string.
235 * \param params Out array of found strings.
236 * \param int_params_store Memory for storing processed integer parameters.
237 *
238 * \return 0 for success else 1
239 */
240static int convert_params( size_t cnt , char ** params , int * int_params_store )
241{
242 char ** cur = params;
243 char ** out = params;
244 int ret = ( DISPATCH_TEST_SUCCESS );
245
Azim Khan8d686bf2018-07-04 23:29:46 +0100246 while ( cur < params + cnt )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100247 {
248 char * type = *cur++;
249 char * val = *cur++;
250
251 if ( strcmp( type, "char*" ) == 0 )
252 {
253 if ( verify_string( &val ) == 0 )
254 {
255 *out++ = val;
256 }
257 else
258 {
259 ret = ( DISPATCH_INVALID_TEST_DATA );
260 break;
261 }
262 }
263 else if ( strcmp( type, "int" ) == 0 )
264 {
265 if ( verify_int ( val, int_params_store ) == 0 )
266 {
267 *out++ = (char *) int_params_store++;
268 }
269 else
270 {
271 ret = ( DISPATCH_INVALID_TEST_DATA );
272 break;
273 }
274 }
275 else if ( strcmp( type, "hex" ) == 0 )
276 {
Azim Khan184447e2017-05-31 20:29:36 +0100277 if ( verify_string( &val ) == 0 )
278 {
279 int j;
280 *int_params_store = unhexify( (unsigned char *) val, val );
281 printf ("\n");
282 for (j = 0; j < *int_params_store; j++)
283 printf ("%02x ", (uint8_t)val[j]);
284 printf ("\n len %d\n", *int_params_store);
285 *out++ = val;
286 *out++ = (char *)(int_params_store++);
287 }
288 else
289 {
290 ret = ( DISPATCH_INVALID_TEST_DATA );
291 break;
292 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100293 }
294 else if ( strcmp( type, "exp" ) == 0 )
295 {
296 int exp_id = strtol( val, NULL, 10 );
297 if ( get_expression ( exp_id, int_params_store ) == 0 )
298 {
299 *out++ = (char *)int_params_store++;
300 }
301 else
302 {
303 ret = ( DISPATCH_INVALID_TEST_DATA );
304 break;
305 }
306 }
307 else
308 {
309 ret = ( DISPATCH_INVALID_TEST_DATA );
310 break;
311 }
312 }
313 return( ret );
314}
315
316/**
317 * \brief Tests snprintf implementation with test input.
318 *
Azim Khan191e9042017-06-09 12:39:00 +0100319 * \note
320 * At high optimization levels (e.g. gcc -O3), this function may be
321 * inlined in run_test_snprintf. This can trigger a spurious warning about
322 * potential misuse of snprintf from gcc -Wformat-truncation (observed with
323 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
324 * only. They are still valid for other compilers. Avoid this warning by
325 * forbidding inlining of this function by gcc.
326 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100327 * \param n Buffer test length.
328 * \param ref_buf Expected buffer.
329 * \param ref_ret Expected snprintf return value.
330 *
331 * \return 0 for success else 1
332 */
Azim Khan191e9042017-06-09 12:39:00 +0100333#if defined(__GNUC__)
334__attribute__((__noinline__))
335#endif
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100336static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
337{
338 int ret;
339 char buf[10] = "xxxxxxxxx";
340 const char ref[10] = "xxxxxxxxx";
341
Mohammad Azim Khan76135342018-04-12 13:23:01 +0100342 if( n >= sizeof( buf ) )
343 return( -1 );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100344 ret = mbedtls_snprintf( buf, n, "%s", "123" );
345 if( ret < 0 || (size_t) ret >= n )
346 ret = -1;
347
348 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
349 ref_ret != ret ||
350 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
351 {
352 return( 1 );
353 }
354
355 return( 0 );
356}
357
358/**
359 * \brief Tests snprintf implementation.
360 *
361 * \param none
362 *
363 * \return 0 for success else 1
364 */
365static int run_test_snprintf( void )
366{
367 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
368 test_snprintf( 1, "", -1 ) != 0 ||
369 test_snprintf( 2, "1", -1 ) != 0 ||
370 test_snprintf( 3, "12", -1 ) != 0 ||
371 test_snprintf( 4, "123", 3 ) != 0 ||
372 test_snprintf( 5, "123", 3 ) != 0 );
373}
374
375
376/**
377 * \brief Desktop implementation of execute_tests().
378 * Parses command line and executes tests from
379 * supplied or default data file.
380 *
381 * \param argc Command line argument count.
382 * \param argv Argument array.
383 *
384 * \return Program exit status.
385 */
386int execute_tests( int argc , const char ** argv )
387{
388 /* Local Configurations and options */
389 const char *default_filename = "DATA_FILE";
390 const char *test_filename = NULL;
391 const char **test_files = NULL;
392 int testfile_count = 0;
393 int option_verbose = 0;
Azim Khan13c6bfb2017-06-15 14:45:56 +0100394 int function_id = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100395
396 /* Other Local variables */
397 int arg_index = 1;
398 const char *next_arg;
399 int testfile_index, ret, i, cnt;
400 int total_errors = 0, total_tests = 0, total_skipped = 0;
401 FILE *file;
402 char buf[5000];
403 char *params[50];
404 int int_params[50]; // Store for proccessed integer params.
405 void *pointer;
406#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
407 int stdout_fd = -1;
408#endif /* __unix__ || __APPLE__ __MACH__ */
409
410#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
411 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
412 unsigned char alloc_buf[1000000];
413 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
414#endif
415
416 /*
417 * The C standard doesn't guarantee that all-bits-0 is the representation
418 * of a NULL pointer. We do however use that in our code for initializing
419 * structures, which should work on every modern platform. Let's be sure.
420 */
421 memset( &pointer, 0, sizeof( void * ) );
422 if( pointer != NULL )
423 {
424 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
425 return( 1 );
426 }
427
428 /*
429 * Make sure we have a snprintf that correctly zero-terminates
430 */
431 if( run_test_snprintf() != 0 )
432 {
433 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
Azim Khan191e9042017-06-09 12:39:00 +0100434 return( 1 );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100435 }
436
437 while( arg_index < argc )
438 {
439 next_arg = argv[ arg_index ];
440
441 if( strcmp(next_arg, "--verbose" ) == 0 ||
442 strcmp(next_arg, "-v" ) == 0 )
443 {
444 option_verbose = 1;
445 }
446 else if( strcmp(next_arg, "--help" ) == 0 ||
447 strcmp(next_arg, "-h" ) == 0 )
448 {
449 mbedtls_fprintf( stdout, USAGE );
450 mbedtls_exit( EXIT_SUCCESS );
451 }
452 else
453 {
454 /* Not an option, therefore treat all further arguments as the file
455 * list.
456 */
457 test_files = &argv[ arg_index ];
458 testfile_count = argc - arg_index;
459 }
460
461 arg_index++;
462 }
463
464 /* If no files were specified, assume a default */
465 if ( test_files == NULL || testfile_count == 0 )
466 {
467 test_files = &default_filename;
468 testfile_count = 1;
469 }
470
471 /* Initialize the struct that holds information about the last test */
472 memset( &test_info, 0, sizeof( test_info ) );
473
474 /* Now begin to execute the tests in the testfiles */
475 for ( testfile_index = 0;
476 testfile_index < testfile_count;
477 testfile_index++ )
478 {
479 int unmet_dep_count = 0;
480 char *unmet_dependencies[20];
481
482 test_filename = test_files[ testfile_index ];
483
484 file = fopen( test_filename, "r" );
485 if( file == NULL )
486 {
487 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
488 test_filename );
489 return( 1 );
490 }
491
492 while( !feof( file ) )
493 {
494 if( unmet_dep_count > 0 )
495 {
496 mbedtls_fprintf( stderr,
497 "FATAL: Dep count larger than zero at start of loop\n" );
498 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
499 }
500 unmet_dep_count = 0;
501
502 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
503 break;
504 mbedtls_fprintf( stdout, "%s%.66s", test_info.failed ? "\n" : "", buf );
505 mbedtls_fprintf( stdout, " " );
506 for( i = strlen( buf ) + 1; i < 67; i++ )
507 mbedtls_fprintf( stdout, "." );
508 mbedtls_fprintf( stdout, " " );
509 fflush( stdout );
510
511 total_tests++;
512
513 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
514 break;
515 cnt = parse_arguments( buf, strlen( buf ), params,
516 sizeof( params ) / sizeof( params[0] ) );
517
518 if( strcmp( params[0], "depends_on" ) == 0 )
519 {
520 for( i = 1; i < cnt; i++ )
521 {
522 int dep_id = strtol( params[i], NULL, 10 );
523 if( dep_check( dep_id ) != DEPENDENCY_SUPPORTED )
524 {
525 if( 0 == option_verbose )
526 {
527 /* Only one count is needed if not verbose */
528 unmet_dep_count++;
529 break;
530 }
531
532 unmet_dependencies[ unmet_dep_count ] = strdup( params[i] );
533 if( unmet_dependencies[ unmet_dep_count ] == NULL )
534 {
535 mbedtls_fprintf( stderr, "FATAL: Out of memory\n" );
536 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
537 }
538 unmet_dep_count++;
539 }
540 }
541
542 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
543 break;
544 cnt = parse_arguments( buf, strlen( buf ), params,
545 sizeof( params ) / sizeof( params[0] ) );
546 }
547
548 // If there are no unmet dependencies execute the test
549 if( unmet_dep_count == 0 )
550 {
551 test_info.failed = 0;
552
553#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
554 /* Suppress all output from the library unless we're verbose
555 * mode
556 */
557 if( !option_verbose )
558 {
559 stdout_fd = redirect_output( &stdout, "/dev/null" );
560 if( stdout_fd == -1 )
561 {
562 /* Redirection has failed with no stdout so exit */
563 exit( 1 );
564 }
565 }
566#endif /* __unix__ || __APPLE__ __MACH__ */
567
Azim Khan13c6bfb2017-06-15 14:45:56 +0100568 function_id = strtol( params[0], NULL, 10 );
569 if ( (ret = check_test( function_id )) == DISPATCH_TEST_SUCCESS )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100570 {
Azim Khan13c6bfb2017-06-15 14:45:56 +0100571 ret = convert_params( cnt - 1, params + 1, int_params );
572 if ( DISPATCH_TEST_SUCCESS == ret )
573 {
574 ret = dispatch_test( function_id, (void **)( params + 1 ) );
575 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100576 }
577
578#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
579 if( !option_verbose && restore_output( &stdout, stdout_fd ) )
580 {
581 /* Redirection has failed with no stdout so exit */
582 exit( 1 );
583 }
584#endif /* __unix__ || __APPLE__ __MACH__ */
585
586 }
587
588 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
589 {
590 total_skipped++;
591 mbedtls_fprintf( stdout, "----" );
592
593 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
594 {
595 mbedtls_fprintf( stdout, "\n Test Suite not enabled" );
596 }
597
598 if( 1 == option_verbose && unmet_dep_count > 0 )
599 {
600 mbedtls_fprintf( stdout, "\n Unmet dependencies: " );
601 for( i = 0; i < unmet_dep_count; i++ )
602 {
603 mbedtls_fprintf( stdout, "%s ",
604 unmet_dependencies[i] );
605 free( unmet_dependencies[i] );
606 }
607 }
608 mbedtls_fprintf( stdout, "\n" );
609 fflush( stdout );
610
611 unmet_dep_count = 0;
612 }
613 else if( ret == DISPATCH_TEST_SUCCESS )
614 {
615 if( test_info.failed == 0 )
616 {
617 mbedtls_fprintf( stdout, "PASS\n" );
618 }
619 else
620 {
621 total_errors++;
622 mbedtls_fprintf( stdout, "FAILED\n" );
623 mbedtls_fprintf( stdout, " %s\n at line %d, %s\n",
624 test_info.test, test_info.line_no,
625 test_info.filename );
626 }
627 fflush( stdout );
628 }
629 else if( ret == DISPATCH_INVALID_TEST_DATA )
630 {
631 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
632 fclose( file );
633 mbedtls_exit( 2 );
634 }
635 else if( ret == DISPATCH_TEST_FN_NOT_FOUND )
636 {
637 mbedtls_fprintf( stderr, "FAILED: FATAL TEST FUNCTION NOT FUND\n" );
638 fclose( file );
639 mbedtls_exit( 2 );
640 }
641 else
642 total_errors++;
643 }
644 fclose( file );
645
646 /* In case we encounter early end of file */
647 for( i = 0; i < unmet_dep_count; i++ )
648 free( unmet_dependencies[i] );
649 }
650
651 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
652 if( total_errors == 0 )
653 mbedtls_fprintf( stdout, "PASSED" );
654 else
655 mbedtls_fprintf( stdout, "FAILED" );
656
657 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
658 total_tests - total_errors, total_tests, total_skipped );
659
660#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
661 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
662#if defined(MBEDTLS_MEMORY_DEBUG)
663 mbedtls_memory_buffer_alloc_status();
664#endif
665 mbedtls_memory_buffer_alloc_free();
666#endif
667
668#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
669 if( stdout_fd != -1 )
670 close_output( stdout );
671#endif /* __unix__ || __APPLE__ __MACH__ */
672
673 return( total_errors != 0 );
674}