blob: 6e7fd075c7d83103c82b1a1788211f71abb6b644 [file] [log] [blame]
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01001#line 2 "suites/desktop_test.function"
2
3/**
4 * \brief Varifies that string is in string parameter format i.e. "<str>"
5 * 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
21 (*str)++;
22 (*str)[strlen( *str ) - 1] = '\0';
23
24 return( 0 );
25}
26
27/**
28 * \brief Varifies that string is an integer. Also gives the converted
29 * 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
246 while ( cur - params < (int) cnt )
247 {
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 {
277 *int_params_store = unhexify( (unsigned char *) val, val );
278 *out++ = (char *)int_params_store++;
279 *out++ = val;
280 }
281 else if ( strcmp( type, "exp" ) == 0 )
282 {
283 int exp_id = strtol( val, NULL, 10 );
284 if ( get_expression ( exp_id, int_params_store ) == 0 )
285 {
286 *out++ = (char *)int_params_store++;
287 }
288 else
289 {
290 ret = ( DISPATCH_INVALID_TEST_DATA );
291 break;
292 }
293 }
294 else
295 {
296 ret = ( DISPATCH_INVALID_TEST_DATA );
297 break;
298 }
299 }
300 return( ret );
301}
302
303/**
304 * \brief Tests snprintf implementation with test input.
305 *
306 * \param n Buffer test length.
307 * \param ref_buf Expected buffer.
308 * \param ref_ret Expected snprintf return value.
309 *
310 * \return 0 for success else 1
311 */
312static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
313{
314 int ret;
315 char buf[10] = "xxxxxxxxx";
316 const char ref[10] = "xxxxxxxxx";
317
318 ret = mbedtls_snprintf( buf, n, "%s", "123" );
319 if( ret < 0 || (size_t) ret >= n )
320 ret = -1;
321
322 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
323 ref_ret != ret ||
324 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
325 {
326 return( 1 );
327 }
328
329 return( 0 );
330}
331
332/**
333 * \brief Tests snprintf implementation.
334 *
335 * \param none
336 *
337 * \return 0 for success else 1
338 */
339static int run_test_snprintf( void )
340{
341 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
342 test_snprintf( 1, "", -1 ) != 0 ||
343 test_snprintf( 2, "1", -1 ) != 0 ||
344 test_snprintf( 3, "12", -1 ) != 0 ||
345 test_snprintf( 4, "123", 3 ) != 0 ||
346 test_snprintf( 5, "123", 3 ) != 0 );
347}
348
349
350/**
351 * \brief Desktop implementation of execute_tests().
352 * Parses command line and executes tests from
353 * supplied or default data file.
354 *
355 * \param argc Command line argument count.
356 * \param argv Argument array.
357 *
358 * \return Program exit status.
359 */
360int execute_tests( int argc , const char ** argv )
361{
362 /* Local Configurations and options */
363 const char *default_filename = "DATA_FILE";
364 const char *test_filename = NULL;
365 const char **test_files = NULL;
366 int testfile_count = 0;
367 int option_verbose = 0;
368
369 /* Other Local variables */
370 int arg_index = 1;
371 const char *next_arg;
372 int testfile_index, ret, i, cnt;
373 int total_errors = 0, total_tests = 0, total_skipped = 0;
374 FILE *file;
375 char buf[5000];
376 char *params[50];
377 int int_params[50]; // Store for proccessed integer params.
378 void *pointer;
379#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
380 int stdout_fd = -1;
381#endif /* __unix__ || __APPLE__ __MACH__ */
382
383#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
384 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
385 unsigned char alloc_buf[1000000];
386 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
387#endif
388
389 /*
390 * The C standard doesn't guarantee that all-bits-0 is the representation
391 * of a NULL pointer. We do however use that in our code for initializing
392 * structures, which should work on every modern platform. Let's be sure.
393 */
394 memset( &pointer, 0, sizeof( void * ) );
395 if( pointer != NULL )
396 {
397 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
398 return( 1 );
399 }
400
401 /*
402 * Make sure we have a snprintf that correctly zero-terminates
403 */
404 if( run_test_snprintf() != 0 )
405 {
406 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
407 return( 0 );
408 }
409
410 while( arg_index < argc )
411 {
412 next_arg = argv[ arg_index ];
413
414 if( strcmp(next_arg, "--verbose" ) == 0 ||
415 strcmp(next_arg, "-v" ) == 0 )
416 {
417 option_verbose = 1;
418 }
419 else if( strcmp(next_arg, "--help" ) == 0 ||
420 strcmp(next_arg, "-h" ) == 0 )
421 {
422 mbedtls_fprintf( stdout, USAGE );
423 mbedtls_exit( EXIT_SUCCESS );
424 }
425 else
426 {
427 /* Not an option, therefore treat all further arguments as the file
428 * list.
429 */
430 test_files = &argv[ arg_index ];
431 testfile_count = argc - arg_index;
432 }
433
434 arg_index++;
435 }
436
437 /* If no files were specified, assume a default */
438 if ( test_files == NULL || testfile_count == 0 )
439 {
440 test_files = &default_filename;
441 testfile_count = 1;
442 }
443
444 /* Initialize the struct that holds information about the last test */
445 memset( &test_info, 0, sizeof( test_info ) );
446
447 /* Now begin to execute the tests in the testfiles */
448 for ( testfile_index = 0;
449 testfile_index < testfile_count;
450 testfile_index++ )
451 {
452 int unmet_dep_count = 0;
453 char *unmet_dependencies[20];
454
455 test_filename = test_files[ testfile_index ];
456
457 file = fopen( test_filename, "r" );
458 if( file == NULL )
459 {
460 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
461 test_filename );
462 return( 1 );
463 }
464
465 while( !feof( file ) )
466 {
467 if( unmet_dep_count > 0 )
468 {
469 mbedtls_fprintf( stderr,
470 "FATAL: Dep count larger than zero at start of loop\n" );
471 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
472 }
473 unmet_dep_count = 0;
474
475 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
476 break;
477 mbedtls_fprintf( stdout, "%s%.66s", test_info.failed ? "\n" : "", buf );
478 mbedtls_fprintf( stdout, " " );
479 for( i = strlen( buf ) + 1; i < 67; i++ )
480 mbedtls_fprintf( stdout, "." );
481 mbedtls_fprintf( stdout, " " );
482 fflush( stdout );
483
484 total_tests++;
485
486 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
487 break;
488 cnt = parse_arguments( buf, strlen( buf ), params,
489 sizeof( params ) / sizeof( params[0] ) );
490
491 if( strcmp( params[0], "depends_on" ) == 0 )
492 {
493 for( i = 1; i < cnt; i++ )
494 {
495 int dep_id = strtol( params[i], NULL, 10 );
496 if( dep_check( dep_id ) != DEPENDENCY_SUPPORTED )
497 {
498 if( 0 == option_verbose )
499 {
500 /* Only one count is needed if not verbose */
501 unmet_dep_count++;
502 break;
503 }
504
505 unmet_dependencies[ unmet_dep_count ] = strdup( params[i] );
506 if( unmet_dependencies[ unmet_dep_count ] == NULL )
507 {
508 mbedtls_fprintf( stderr, "FATAL: Out of memory\n" );
509 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
510 }
511 unmet_dep_count++;
512 }
513 }
514
515 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
516 break;
517 cnt = parse_arguments( buf, strlen( buf ), params,
518 sizeof( params ) / sizeof( params[0] ) );
519 }
520
521 // If there are no unmet dependencies execute the test
522 if( unmet_dep_count == 0 )
523 {
524 test_info.failed = 0;
525
526#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
527 /* Suppress all output from the library unless we're verbose
528 * mode
529 */
530 if( !option_verbose )
531 {
532 stdout_fd = redirect_output( &stdout, "/dev/null" );
533 if( stdout_fd == -1 )
534 {
535 /* Redirection has failed with no stdout so exit */
536 exit( 1 );
537 }
538 }
539#endif /* __unix__ || __APPLE__ __MACH__ */
540
541 ret = convert_params( cnt - 1, params + 1, int_params );
542 if ( DISPATCH_TEST_SUCCESS == ret )
543 {
544 int function_id = strtol( params[0], NULL, 10 );
545 ret = dispatch_test( function_id, (void **)( params + 1 ) );
546 }
547
548#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
549 if( !option_verbose && restore_output( &stdout, stdout_fd ) )
550 {
551 /* Redirection has failed with no stdout so exit */
552 exit( 1 );
553 }
554#endif /* __unix__ || __APPLE__ __MACH__ */
555
556 }
557
558 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
559 {
560 total_skipped++;
561 mbedtls_fprintf( stdout, "----" );
562
563 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
564 {
565 mbedtls_fprintf( stdout, "\n Test Suite not enabled" );
566 }
567
568 if( 1 == option_verbose && unmet_dep_count > 0 )
569 {
570 mbedtls_fprintf( stdout, "\n Unmet dependencies: " );
571 for( i = 0; i < unmet_dep_count; i++ )
572 {
573 mbedtls_fprintf( stdout, "%s ",
574 unmet_dependencies[i] );
575 free( unmet_dependencies[i] );
576 }
577 }
578 mbedtls_fprintf( stdout, "\n" );
579 fflush( stdout );
580
581 unmet_dep_count = 0;
582 }
583 else if( ret == DISPATCH_TEST_SUCCESS )
584 {
585 if( test_info.failed == 0 )
586 {
587 mbedtls_fprintf( stdout, "PASS\n" );
588 }
589 else
590 {
591 total_errors++;
592 mbedtls_fprintf( stdout, "FAILED\n" );
593 mbedtls_fprintf( stdout, " %s\n at line %d, %s\n",
594 test_info.test, test_info.line_no,
595 test_info.filename );
596 }
597 fflush( stdout );
598 }
599 else if( ret == DISPATCH_INVALID_TEST_DATA )
600 {
601 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
602 fclose( file );
603 mbedtls_exit( 2 );
604 }
605 else if( ret == DISPATCH_TEST_FN_NOT_FOUND )
606 {
607 mbedtls_fprintf( stderr, "FAILED: FATAL TEST FUNCTION NOT FUND\n" );
608 fclose( file );
609 mbedtls_exit( 2 );
610 }
611 else
612 total_errors++;
613 }
614 fclose( file );
615
616 /* In case we encounter early end of file */
617 for( i = 0; i < unmet_dep_count; i++ )
618 free( unmet_dependencies[i] );
619 }
620
621 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
622 if( total_errors == 0 )
623 mbedtls_fprintf( stdout, "PASSED" );
624 else
625 mbedtls_fprintf( stdout, "FAILED" );
626
627 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
628 total_tests - total_errors, total_tests, total_skipped );
629
630#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
631 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
632#if defined(MBEDTLS_MEMORY_DEBUG)
633 mbedtls_memory_buffer_alloc_status();
634#endif
635 mbedtls_memory_buffer_alloc_free();
636#endif
637
638#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
639 if( stdout_fd != -1 )
640 close_output( stdout );
641#endif /* __unix__ || __APPLE__ __MACH__ */
642
643 return( total_errors != 0 );
644}