blob: c57fa0707cfe62207170ba830ec06c34ab665595 [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{
Mohammad Azim Khand2d01122018-07-18 17:48:37 +010013 if( ( *str )[0] != '"' ||
14 ( *str )[strlen( *str ) - 1] != '"' )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010015 {
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 ) ) &&
Mohammad Azim Khand2d01122018-07-18 17:48:37 +010052 str[i - 1] == '0' && ( str[i] == 'x' || str[i] == 'X' ) )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010053 {
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" \
Mohammad Azim Khand2d01122018-07-18 17:48:37 +010090 " files... One or more test data files. If no file is\n" \
91 " specified the following default test case\n" \
92 " file is used:\n" \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010093 " %s\n\n" \
94 " Options:\n" \
95 " -v | --verbose Display full information about each test\n" \
96 " -h | --help Display this information\n\n", \
97 argv[0], \
98 "TESTCASE_FILENAME"
99
100
101/**
102 * \brief Read a line from the passed file pointer.
103 *
104 * \param f FILE pointer
105 * \param buf Pointer to memory to hold read line.
106 * \param len Length of the buf.
107 *
108 * \return 0 if success else -1
109 */
110int get_line( FILE *f, char *buf, size_t len )
111{
112 char *ret;
113 int i = 0, str_len = 0, has_string = 0;
114
115 /* Read until we get a valid line */
116 do
117 {
118 ret = fgets( buf, len, f );
119 if( ret == NULL )
120 return( -1 );
121
122 str_len = strlen( buf );
123
124 /* Skip empty line and comment */
125 if ( str_len == 0 || buf[0] == '#' )
126 continue;
127 has_string = 0;
128 for ( i = 0; i < str_len; i++ )
129 {
130 char c = buf[i];
131 if ( c != ' ' && c != '\t' && c != '\n' &&
132 c != '\v' && c != '\f' && c != '\r' )
133 {
134 has_string = 1;
135 break;
136 }
137 }
138 } while( !has_string );
139
140 /* Strip new line and carriage return */
141 ret = buf + strlen( buf );
142 if( ret-- > buf && *ret == '\n' )
143 *ret = '\0';
144 if( ret-- > buf && *ret == '\r' )
145 *ret = '\0';
146
147 return( 0 );
148}
149
150/**
151 * \brief Splits string delimited by ':'. Ignores '\:'.
152 *
153 * \param buf Input string
154 * \param len Input string length
155 * \param params Out params found
156 * \param params_len Out params array len
157 *
158 * \return Count of strings found.
159 */
160static int parse_arguments( char *buf, size_t len, char **params,
161 size_t params_len )
162{
163 size_t cnt = 0, i;
164 char *cur = buf;
165 char *p = buf, *q;
166
167 params[cnt++] = cur;
168
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100169 while( *p != '\0' && p < ( buf + len ) )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100170 {
171 if( *p == '\\' )
172 {
173 p++;
174 p++;
175 continue;
176 }
177 if( *p == ':' )
178 {
179 if( p + 1 < buf + len )
180 {
181 cur = p + 1;
Gilles Peskinee7655df2019-06-07 14:52:07 +0200182 TEST_HELPER_ASSERT( cnt < params_len );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100183 params[cnt++] = cur;
184 }
185 *p = '\0';
186 }
187
188 p++;
189 }
190
191 /* Replace newlines, question marks and colons in strings */
192 for( i = 0; i < cnt; i++ )
193 {
194 p = params[i];
195 q = params[i];
196
197 while( *p != '\0' )
198 {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100199 if( *p == '\\' && *( p + 1 ) == 'n' )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100200 {
201 p += 2;
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100202 *( q++ ) = '\n';
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100203 }
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100204 else if( *p == '\\' && *( p + 1 ) == ':' )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100205 {
206 p += 2;
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100207 *( q++ ) = ':';
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100208 }
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100209 else if( *p == '\\' && *( p + 1 ) == '?' )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100210 {
211 p += 2;
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100212 *( q++ ) = '?';
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100213 }
214 else
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100215 *( q++ ) = *( p++ );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100216 }
217 *q = '\0';
218 }
219
220 return( cnt );
221}
222
223/**
224 * \brief Converts parameters into test function consumable parameters.
225 * Example: Input: {"int", "0", "char*", "Hello",
226 * "hex", "abef", "exp", "1"}
227 * Output: {
228 * 0, // Verified int
229 * "Hello", // Verified string
230 * 2, { 0xab, 0xef },// Converted len,hex pair
231 * 9600 // Evaluated expression
232 * }
233 *
234 *
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100235 * \param cnt Parameter array count.
236 * \param params Out array of found parameters.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100237 * \param int_params_store Memory for storing processed integer parameters.
238 *
239 * \return 0 for success else 1
240 */
241static int convert_params( size_t cnt , char ** params , int * int_params_store )
242{
243 char ** cur = params;
244 char ** out = params;
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100245 int ret = DISPATCH_TEST_SUCCESS;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100246
Azim Khan8d686bf2018-07-04 23:29:46 +0100247 while ( cur < params + cnt )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100248 {
249 char * type = *cur++;
250 char * val = *cur++;
251
252 if ( strcmp( type, "char*" ) == 0 )
253 {
254 if ( verify_string( &val ) == 0 )
255 {
256 *out++ = val;
257 }
258 else
259 {
260 ret = ( DISPATCH_INVALID_TEST_DATA );
261 break;
262 }
263 }
264 else if ( strcmp( type, "int" ) == 0 )
265 {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100266 if ( verify_int( val, int_params_store ) == 0 )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100267 {
268 *out++ = (char *) int_params_store++;
269 }
270 else
271 {
272 ret = ( DISPATCH_INVALID_TEST_DATA );
273 break;
274 }
275 }
276 else if ( strcmp( type, "hex" ) == 0 )
277 {
Azim Khan184447e2017-05-31 20:29:36 +0100278 if ( verify_string( &val ) == 0 )
279 {
Ronald Cronff31eab2020-06-08 17:20:59 +0200280 *int_params_store = mbedtls_test_unhexify(
281 (unsigned char *) val, val );
Azim Khan184447e2017-05-31 20:29:36 +0100282 *out++ = val;
283 *out++ = (char *)(int_params_store++);
284 }
285 else
286 {
287 ret = ( DISPATCH_INVALID_TEST_DATA );
288 break;
289 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100290 }
291 else if ( strcmp( type, "exp" ) == 0 )
292 {
293 int exp_id = strtol( val, NULL, 10 );
294 if ( get_expression ( exp_id, int_params_store ) == 0 )
295 {
296 *out++ = (char *)int_params_store++;
297 }
298 else
299 {
300 ret = ( DISPATCH_INVALID_TEST_DATA );
301 break;
302 }
303 }
304 else
305 {
306 ret = ( DISPATCH_INVALID_TEST_DATA );
307 break;
308 }
309 }
310 return( ret );
311}
312
313/**
314 * \brief Tests snprintf implementation with test input.
315 *
Azim Khan191e9042017-06-09 12:39:00 +0100316 * \note
317 * At high optimization levels (e.g. gcc -O3), this function may be
318 * inlined in run_test_snprintf. This can trigger a spurious warning about
319 * potential misuse of snprintf from gcc -Wformat-truncation (observed with
320 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
321 * only. They are still valid for other compilers. Avoid this warning by
322 * forbidding inlining of this function by gcc.
323 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100324 * \param n Buffer test length.
325 * \param ref_buf Expected buffer.
326 * \param ref_ret Expected snprintf return value.
327 *
328 * \return 0 for success else 1
329 */
Azim Khan191e9042017-06-09 12:39:00 +0100330#if defined(__GNUC__)
331__attribute__((__noinline__))
332#endif
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100333static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
334{
335 int ret;
336 char buf[10] = "xxxxxxxxx";
337 const char ref[10] = "xxxxxxxxx";
338
Mohammad Azim Khan76135342018-04-12 13:23:01 +0100339 if( n >= sizeof( buf ) )
340 return( -1 );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100341 ret = mbedtls_snprintf( buf, n, "%s", "123" );
342 if( ret < 0 || (size_t) ret >= n )
343 ret = -1;
344
345 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
346 ref_ret != ret ||
347 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
348 {
349 return( 1 );
350 }
351
352 return( 0 );
353}
354
355/**
356 * \brief Tests snprintf implementation.
357 *
358 * \param none
359 *
360 * \return 0 for success else 1
361 */
362static int run_test_snprintf( void )
363{
364 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
365 test_snprintf( 1, "", -1 ) != 0 ||
366 test_snprintf( 2, "1", -1 ) != 0 ||
367 test_snprintf( 3, "12", -1 ) != 0 ||
368 test_snprintf( 4, "123", 3 ) != 0 ||
369 test_snprintf( 5, "123", 3 ) != 0 );
370}
371
Gilles Peskine51dcc242019-09-16 15:13:48 +0200372/** \brief Write the description of the test case to the outcome CSV file.
373 *
374 * \param outcome_file The file to write to.
375 * If this is \c NULL, this function does nothing.
376 * \param argv0 The test suite name.
377 * \param test_case The test case description.
378 */
379static void write_outcome_entry( FILE *outcome_file,
380 const char *argv0,
381 const char *test_case )
382{
383 /* The non-varying fields are initialized on first use. */
384 static const char *platform = NULL;
385 static const char *configuration = NULL;
386 static const char *test_suite = NULL;
387
388 if( outcome_file == NULL )
389 return;
390
391 if( platform == NULL )
392 {
393 platform = getenv( "MBEDTLS_TEST_PLATFORM" );
394 if( platform == NULL )
395 platform = "unknown";
396 }
397 if( configuration == NULL )
398 {
399 configuration = getenv( "MBEDTLS_TEST_CONFIGURATION" );
400 if( configuration == NULL )
401 configuration = "unknown";
402 }
403 if( test_suite == NULL )
404 {
405 test_suite = strrchr( argv0, '/' );
406 if( test_suite != NULL )
407 test_suite += 1; // skip the '/'
408 else
409 test_suite = argv0;
410 }
411
412 /* Write the beginning of the outcome line.
413 * Ignore errors: writing the outcome file is on a best-effort basis. */
414 mbedtls_fprintf( outcome_file, "%s;%s;%s;%s;",
415 platform, configuration, test_suite, test_case );
416}
417
418/** \brief Write the result of the test case to the outcome CSV file.
419 *
420 * \param outcome_file The file to write to.
421 * If this is \c NULL, this function does nothing.
Ronald Cron67a8a372020-04-01 16:04:41 +0200422 * \param unmet_dep_count The number of unmet dependencies.
423 * \param unmet_dependencies The array of unmet dependencies.
424 * \param missing_unmet_dependencies Non-zero if there was a problem tracking
425 * all unmet dependencies, 0 otherwise.
Gilles Peskine51dcc242019-09-16 15:13:48 +0200426 * \param ret The test dispatch status (DISPATCH_xxx).
427 * \param test_info A pointer to the test info structure.
428 */
429static void write_outcome_result( FILE *outcome_file,
430 size_t unmet_dep_count,
Gilles Peskine06725c92020-03-30 21:39:09 +0200431 int unmet_dependencies[],
Ronald Cron67a8a372020-04-01 16:04:41 +0200432 int missing_unmet_dependencies,
Gilles Peskine51dcc242019-09-16 15:13:48 +0200433 int ret,
434 const test_info_t *info )
435{
436 if( outcome_file == NULL )
437 return;
438
439 /* Write the end of the outcome line.
440 * Ignore errors: writing the outcome file is on a best-effort basis. */
441 switch( ret )
442 {
443 case DISPATCH_TEST_SUCCESS:
444 if( unmet_dep_count > 0 )
445 {
446 size_t i;
447 mbedtls_fprintf( outcome_file, "SKIP" );
448 for( i = 0; i < unmet_dep_count; i++ )
449 {
Gilles Peskine06725c92020-03-30 21:39:09 +0200450 mbedtls_fprintf( outcome_file, "%c%d",
Gilles Peskine51dcc242019-09-16 15:13:48 +0200451 i == 0 ? ';' : ':',
452 unmet_dependencies[i] );
453 }
Ronald Cron67a8a372020-04-01 16:04:41 +0200454 if( missing_unmet_dependencies )
455 mbedtls_fprintf( outcome_file, ":..." );
Gilles Peskine51dcc242019-09-16 15:13:48 +0200456 break;
457 }
458 switch( info->result )
459 {
460 case TEST_RESULT_SUCCESS:
461 mbedtls_fprintf( outcome_file, "PASS;" );
462 break;
463 case TEST_RESULT_SKIPPED:
464 mbedtls_fprintf( outcome_file, "SKIP;Runtime skip" );
465 break;
466 default:
467 mbedtls_fprintf( outcome_file, "FAIL;%s:%d:%s",
468 info->filename, info->line_no,
469 info->test );
470 break;
471 }
472 break;
473 case DISPATCH_TEST_FN_NOT_FOUND:
474 mbedtls_fprintf( outcome_file, "FAIL;Test function not found" );
475 break;
476 case DISPATCH_INVALID_TEST_DATA:
477 mbedtls_fprintf( outcome_file, "FAIL;Invalid test data" );
478 break;
479 case DISPATCH_UNSUPPORTED_SUITE:
480 mbedtls_fprintf( outcome_file, "SKIP;Unsupported suite" );
481 break;
482 default:
483 mbedtls_fprintf( outcome_file, "FAIL;Unknown cause" );
484 break;
485 }
486 mbedtls_fprintf( outcome_file, "\n" );
487 fflush( outcome_file );
488}
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100489
490/**
491 * \brief Desktop implementation of execute_tests().
492 * Parses command line and executes tests from
493 * supplied or default data file.
494 *
495 * \param argc Command line argument count.
496 * \param argv Argument array.
497 *
498 * \return Program exit status.
499 */
500int execute_tests( int argc , const char ** argv )
501{
502 /* Local Configurations and options */
503 const char *default_filename = "DATA_FILE";
504 const char *test_filename = NULL;
505 const char **test_files = NULL;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200506 size_t testfile_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100507 int option_verbose = 0;
k-stachowiak03954f22019-09-16 10:23:10 +0200508 size_t function_id = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100509
510 /* Other Local variables */
511 int arg_index = 1;
512 const char *next_arg;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200513 size_t testfile_index, i, cnt;
514 int ret;
515 unsigned total_errors = 0, total_tests = 0, total_skipped = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100516 FILE *file;
517 char buf[5000];
518 char *params[50];
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100519 /* Store for proccessed integer params. */
520 int int_params[50];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100521 void *pointer;
522#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
523 int stdout_fd = -1;
524#endif /* __unix__ || __APPLE__ __MACH__ */
Gilles Peskine51dcc242019-09-16 15:13:48 +0200525 const char *outcome_file_name = getenv( "MBEDTLS_TEST_OUTCOME_FILE" );
526 FILE *outcome_file = NULL;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100527
528#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
529 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
530 unsigned char alloc_buf[1000000];
531 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
532#endif
533
534 /*
535 * The C standard doesn't guarantee that all-bits-0 is the representation
536 * of a NULL pointer. We do however use that in our code for initializing
537 * structures, which should work on every modern platform. Let's be sure.
538 */
539 memset( &pointer, 0, sizeof( void * ) );
540 if( pointer != NULL )
541 {
542 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
543 return( 1 );
544 }
545
546 /*
547 * Make sure we have a snprintf that correctly zero-terminates
548 */
549 if( run_test_snprintf() != 0 )
550 {
551 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
Azim Khan191e9042017-06-09 12:39:00 +0100552 return( 1 );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100553 }
554
gabor-mezei-arm88d7eee2020-04-22 11:07:34 +0200555 if( outcome_file_name != NULL && *outcome_file_name != '\0' )
Gilles Peskine9c673232020-01-21 18:03:56 +0100556 {
557 outcome_file = fopen( outcome_file_name, "a" );
558 if( outcome_file == NULL )
559 {
560 mbedtls_fprintf( stderr, "Unable to open outcome file. Continuing anyway.\n" );
561 }
562 }
563
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100564 while( arg_index < argc )
565 {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100566 next_arg = argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100567
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100568 if( strcmp( next_arg, "--verbose" ) == 0 ||
569 strcmp( next_arg, "-v" ) == 0 )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100570 {
571 option_verbose = 1;
572 }
573 else if( strcmp(next_arg, "--help" ) == 0 ||
574 strcmp(next_arg, "-h" ) == 0 )
575 {
576 mbedtls_fprintf( stdout, USAGE );
577 mbedtls_exit( EXIT_SUCCESS );
578 }
579 else
580 {
581 /* Not an option, therefore treat all further arguments as the file
582 * list.
583 */
584 test_files = &argv[ arg_index ];
585 testfile_count = argc - arg_index;
586 }
587
588 arg_index++;
589 }
590
591 /* If no files were specified, assume a default */
592 if ( test_files == NULL || testfile_count == 0 )
593 {
594 test_files = &default_filename;
595 testfile_count = 1;
596 }
597
598 /* Initialize the struct that holds information about the last test */
599 memset( &test_info, 0, sizeof( test_info ) );
600
601 /* Now begin to execute the tests in the testfiles */
602 for ( testfile_index = 0;
603 testfile_index < testfile_count;
604 testfile_index++ )
605 {
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200606 size_t unmet_dep_count = 0;
Gilles Peskine06725c92020-03-30 21:39:09 +0200607 int unmet_dependencies[20];
Ronald Cron67a8a372020-04-01 16:04:41 +0200608 int missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100609
610 test_filename = test_files[ testfile_index ];
611
612 file = fopen( test_filename, "r" );
613 if( file == NULL )
614 {
615 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
616 test_filename );
Gilles Peskine9c673232020-01-21 18:03:56 +0100617 if( outcome_file != NULL )
618 fclose( outcome_file );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100619 return( 1 );
620 }
621
622 while( !feof( file ) )
623 {
624 if( unmet_dep_count > 0 )
625 {
626 mbedtls_fprintf( stderr,
627 "FATAL: Dep count larger than zero at start of loop\n" );
628 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
629 }
630 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200631 missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100632
633 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
634 break;
Hanno Beckere69d0152019-07-05 13:31:30 +0100635 mbedtls_fprintf( stdout, "%s%.66s",
636 test_info.result == TEST_RESULT_FAILED ? "\n" : "", buf );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100637 mbedtls_fprintf( stdout, " " );
638 for( i = strlen( buf ) + 1; i < 67; i++ )
639 mbedtls_fprintf( stdout, "." );
640 mbedtls_fprintf( stdout, " " );
641 fflush( stdout );
Gilles Peskine51dcc242019-09-16 15:13:48 +0200642 write_outcome_entry( outcome_file, argv[0], buf );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100643
644 total_tests++;
645
646 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
647 break;
648 cnt = parse_arguments( buf, strlen( buf ), params,
649 sizeof( params ) / sizeof( params[0] ) );
650
651 if( strcmp( params[0], "depends_on" ) == 0 )
652 {
653 for( i = 1; i < cnt; i++ )
654 {
655 int dep_id = strtol( params[i], NULL, 10 );
656 if( dep_check( dep_id ) != DEPENDENCY_SUPPORTED )
657 {
Ronald Crone1a05a52020-04-01 15:52:06 +0200658 if( unmet_dep_count <
659 ARRAY_LENGTH( unmet_dependencies ) )
660 {
661 unmet_dependencies[unmet_dep_count] = dep_id;
662 unmet_dep_count++;
663 }
Ronald Cron67a8a372020-04-01 16:04:41 +0200664 else
665 {
666 missing_unmet_dependencies = 1;
667 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100668 }
669 }
670
671 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
672 break;
673 cnt = parse_arguments( buf, strlen( buf ), params,
674 sizeof( params ) / sizeof( params[0] ) );
675 }
676
677 // If there are no unmet dependencies execute the test
678 if( unmet_dep_count == 0 )
679 {
Hanno Beckere69d0152019-07-05 13:31:30 +0100680 test_info.result = TEST_RESULT_SUCCESS;
Simon Butcher6542f6c2018-12-09 22:09:59 +0000681 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_IDLE;
Gilles Peskine56055912019-03-01 14:26:30 +0100682 test_info.step = (unsigned long)( -1 );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100683
684#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
685 /* Suppress all output from the library unless we're verbose
686 * mode
687 */
688 if( !option_verbose )
689 {
690 stdout_fd = redirect_output( &stdout, "/dev/null" );
691 if( stdout_fd == -1 )
692 {
693 /* Redirection has failed with no stdout so exit */
694 exit( 1 );
695 }
696 }
697#endif /* __unix__ || __APPLE__ __MACH__ */
698
k-stachowiak03954f22019-09-16 10:23:10 +0200699 function_id = strtoul( params[0], NULL, 10 );
Azim Khan13c6bfb2017-06-15 14:45:56 +0100700 if ( (ret = check_test( function_id )) == DISPATCH_TEST_SUCCESS )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100701 {
Azim Khan13c6bfb2017-06-15 14:45:56 +0100702 ret = convert_params( cnt - 1, params + 1, int_params );
703 if ( DISPATCH_TEST_SUCCESS == ret )
704 {
705 ret = dispatch_test( function_id, (void **)( params + 1 ) );
706 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100707 }
708
709#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
710 if( !option_verbose && restore_output( &stdout, stdout_fd ) )
711 {
712 /* Redirection has failed with no stdout so exit */
713 exit( 1 );
714 }
715#endif /* __unix__ || __APPLE__ __MACH__ */
716
717 }
718
Gilles Peskine51dcc242019-09-16 15:13:48 +0200719 write_outcome_result( outcome_file,
720 unmet_dep_count, unmet_dependencies,
Ronald Cron67a8a372020-04-01 16:04:41 +0200721 missing_unmet_dependencies,
Gilles Peskine51dcc242019-09-16 15:13:48 +0200722 ret, &test_info );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100723 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
724 {
725 total_skipped++;
726 mbedtls_fprintf( stdout, "----" );
727
728 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
729 {
730 mbedtls_fprintf( stdout, "\n Test Suite not enabled" );
731 }
732
733 if( 1 == option_verbose && unmet_dep_count > 0 )
734 {
735 mbedtls_fprintf( stdout, "\n Unmet dependencies: " );
736 for( i = 0; i < unmet_dep_count; i++ )
737 {
Gilles Peskine06725c92020-03-30 21:39:09 +0200738 mbedtls_fprintf( stdout, "%d ",
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100739 unmet_dependencies[i] );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100740 }
Ronald Cron67a8a372020-04-01 16:04:41 +0200741 if( missing_unmet_dependencies )
742 mbedtls_fprintf( stdout, "..." );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100743 }
744 mbedtls_fprintf( stdout, "\n" );
745 fflush( stdout );
746
747 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200748 missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100749 }
750 else if( ret == DISPATCH_TEST_SUCCESS )
751 {
Hanno Beckere69d0152019-07-05 13:31:30 +0100752 if( test_info.result == TEST_RESULT_SUCCESS )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100753 {
754 mbedtls_fprintf( stdout, "PASS\n" );
755 }
Hanno Beckere69d0152019-07-05 13:31:30 +0100756 else if( test_info.result == TEST_RESULT_SKIPPED )
757 {
758 mbedtls_fprintf( stdout, "----\n" );
759 total_skipped++;
760 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100761 else
762 {
763 total_errors++;
764 mbedtls_fprintf( stdout, "FAILED\n" );
Gilles Peskine56055912019-03-01 14:26:30 +0100765 mbedtls_fprintf( stdout, " %s\n at ",
766 test_info.test );
767 if( test_info.step != (unsigned long)( -1 ) )
768 {
769 mbedtls_fprintf( stdout, "step %lu, ",
770 test_info.step );
771 }
772 mbedtls_fprintf( stdout, "line %d, %s",
773 test_info.line_no, test_info.filename );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100774 }
775 fflush( stdout );
776 }
777 else if( ret == DISPATCH_INVALID_TEST_DATA )
778 {
779 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
780 fclose( file );
781 mbedtls_exit( 2 );
782 }
783 else if( ret == DISPATCH_TEST_FN_NOT_FOUND )
784 {
Gilles Peskine31fccc82019-09-16 15:12:37 +0200785 mbedtls_fprintf( stderr, "FAILED: FATAL TEST FUNCTION NOT FOUND\n" );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100786 fclose( file );
787 mbedtls_exit( 2 );
788 }
789 else
790 total_errors++;
791 }
792 fclose( file );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100793 }
794
Gilles Peskine51dcc242019-09-16 15:13:48 +0200795 if( outcome_file != NULL )
796 fclose( outcome_file );
797
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100798 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
799 if( total_errors == 0 )
800 mbedtls_fprintf( stdout, "PASSED" );
801 else
802 mbedtls_fprintf( stdout, "FAILED" );
803
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200804 mbedtls_fprintf( stdout, " (%u / %u tests (%u skipped))\n",
805 total_tests - total_errors, total_tests, total_skipped );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100806
807#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
808 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
809#if defined(MBEDTLS_MEMORY_DEBUG)
810 mbedtls_memory_buffer_alloc_status();
811#endif
812 mbedtls_memory_buffer_alloc_free();
813#endif
814
815#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
816 if( stdout_fd != -1 )
817 close_output( stdout );
818#endif /* __unix__ || __APPLE__ __MACH__ */
819
820 return( total_errors != 0 );
821}