blob: f4f4f45bdfe11119267ce04da11bdfbc0c696e7a [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 Crona0c25392020-06-18 10:10:46 +0200280 size_t len;
281
282 TEST_HELPER_ASSERT(
283 mbedtls_test_unhexify( (unsigned char *) val, strlen( val ),
284 val, &len ) == 0 );
285
286 *int_params_store = len;
Azim Khan184447e2017-05-31 20:29:36 +0100287 *out++ = val;
288 *out++ = (char *)(int_params_store++);
289 }
290 else
291 {
292 ret = ( DISPATCH_INVALID_TEST_DATA );
293 break;
294 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100295 }
296 else if ( strcmp( type, "exp" ) == 0 )
297 {
298 int exp_id = strtol( val, NULL, 10 );
299 if ( get_expression ( exp_id, int_params_store ) == 0 )
300 {
301 *out++ = (char *)int_params_store++;
302 }
303 else
304 {
305 ret = ( DISPATCH_INVALID_TEST_DATA );
306 break;
307 }
308 }
309 else
310 {
311 ret = ( DISPATCH_INVALID_TEST_DATA );
312 break;
313 }
314 }
315 return( ret );
316}
317
318/**
319 * \brief Tests snprintf implementation with test input.
320 *
Azim Khan191e9042017-06-09 12:39:00 +0100321 * \note
322 * At high optimization levels (e.g. gcc -O3), this function may be
323 * inlined in run_test_snprintf. This can trigger a spurious warning about
324 * potential misuse of snprintf from gcc -Wformat-truncation (observed with
325 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
326 * only. They are still valid for other compilers. Avoid this warning by
327 * forbidding inlining of this function by gcc.
328 *
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100329 * \param n Buffer test length.
330 * \param ref_buf Expected buffer.
331 * \param ref_ret Expected snprintf return value.
332 *
333 * \return 0 for success else 1
334 */
Azim Khan191e9042017-06-09 12:39:00 +0100335#if defined(__GNUC__)
336__attribute__((__noinline__))
337#endif
Rodrigo Dias Correa80448aa2020-11-10 02:28:50 -0300338static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100339{
340 int ret;
341 char buf[10] = "xxxxxxxxx";
342 const char ref[10] = "xxxxxxxxx";
343
Mohammad Azim Khan76135342018-04-12 13:23:01 +0100344 if( n >= sizeof( buf ) )
345 return( -1 );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100346 ret = mbedtls_snprintf( buf, n, "%s", "123" );
347 if( ret < 0 || (size_t) ret >= n )
348 ret = -1;
349
350 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
351 ref_ret != ret ||
352 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
353 {
354 return( 1 );
355 }
356
357 return( 0 );
358}
359
360/**
361 * \brief Tests snprintf implementation.
362 *
363 * \param none
364 *
365 * \return 0 for success else 1
366 */
367static int run_test_snprintf( void )
368{
369 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
370 test_snprintf( 1, "", -1 ) != 0 ||
371 test_snprintf( 2, "1", -1 ) != 0 ||
372 test_snprintf( 3, "12", -1 ) != 0 ||
373 test_snprintf( 4, "123", 3 ) != 0 ||
374 test_snprintf( 5, "123", 3 ) != 0 );
375}
376
Gilles Peskine51dcc242019-09-16 15:13:48 +0200377/** \brief Write the description of the test case to the outcome CSV file.
378 *
379 * \param outcome_file The file to write to.
380 * If this is \c NULL, this function does nothing.
381 * \param argv0 The test suite name.
382 * \param test_case The test case description.
383 */
384static void write_outcome_entry( FILE *outcome_file,
385 const char *argv0,
386 const char *test_case )
387{
388 /* The non-varying fields are initialized on first use. */
389 static const char *platform = NULL;
390 static const char *configuration = NULL;
391 static const char *test_suite = NULL;
392
393 if( outcome_file == NULL )
394 return;
395
396 if( platform == NULL )
397 {
398 platform = getenv( "MBEDTLS_TEST_PLATFORM" );
399 if( platform == NULL )
400 platform = "unknown";
401 }
402 if( configuration == NULL )
403 {
404 configuration = getenv( "MBEDTLS_TEST_CONFIGURATION" );
405 if( configuration == NULL )
406 configuration = "unknown";
407 }
408 if( test_suite == NULL )
409 {
410 test_suite = strrchr( argv0, '/' );
411 if( test_suite != NULL )
412 test_suite += 1; // skip the '/'
413 else
414 test_suite = argv0;
415 }
416
417 /* Write the beginning of the outcome line.
418 * Ignore errors: writing the outcome file is on a best-effort basis. */
419 mbedtls_fprintf( outcome_file, "%s;%s;%s;%s;",
420 platform, configuration, test_suite, test_case );
421}
422
423/** \brief Write the result of the test case to the outcome CSV file.
424 *
425 * \param outcome_file The file to write to.
426 * If this is \c NULL, this function does nothing.
Ronald Cron67a8a372020-04-01 16:04:41 +0200427 * \param unmet_dep_count The number of unmet dependencies.
428 * \param unmet_dependencies The array of unmet dependencies.
429 * \param missing_unmet_dependencies Non-zero if there was a problem tracking
430 * all unmet dependencies, 0 otherwise.
Chris Jonese60e2ae2021-01-20 17:51:47 +0000431 * \param ret The test dispatch status (DISPATCH_xxx).
432 * \param mbedtls_test_info A pointer to the test info structure.
Gilles Peskine51dcc242019-09-16 15:13:48 +0200433 */
434static void write_outcome_result( FILE *outcome_file,
435 size_t unmet_dep_count,
Gilles Peskine06725c92020-03-30 21:39:09 +0200436 int unmet_dependencies[],
Ronald Cron67a8a372020-04-01 16:04:41 +0200437 int missing_unmet_dependencies,
Gilles Peskine51dcc242019-09-16 15:13:48 +0200438 int ret,
Chris Jonese60e2ae2021-01-20 17:51:47 +0000439 const mbedtls_test_info_t *info )
Gilles Peskine51dcc242019-09-16 15:13:48 +0200440{
441 if( outcome_file == NULL )
442 return;
443
444 /* Write the end of the outcome line.
445 * Ignore errors: writing the outcome file is on a best-effort basis. */
446 switch( ret )
447 {
448 case DISPATCH_TEST_SUCCESS:
449 if( unmet_dep_count > 0 )
450 {
451 size_t i;
452 mbedtls_fprintf( outcome_file, "SKIP" );
453 for( i = 0; i < unmet_dep_count; i++ )
454 {
Gilles Peskine06725c92020-03-30 21:39:09 +0200455 mbedtls_fprintf( outcome_file, "%c%d",
Gilles Peskine51dcc242019-09-16 15:13:48 +0200456 i == 0 ? ';' : ':',
457 unmet_dependencies[i] );
458 }
Ronald Cron67a8a372020-04-01 16:04:41 +0200459 if( missing_unmet_dependencies )
460 mbedtls_fprintf( outcome_file, ":..." );
Gilles Peskine51dcc242019-09-16 15:13:48 +0200461 break;
462 }
463 switch( info->result )
464 {
Chris Jonese60e2ae2021-01-20 17:51:47 +0000465 case MBEDTLS_TEST_RESULT_SUCCESS:
Gilles Peskine51dcc242019-09-16 15:13:48 +0200466 mbedtls_fprintf( outcome_file, "PASS;" );
467 break;
Chris Jonese60e2ae2021-01-20 17:51:47 +0000468 case MBEDTLS_TEST_RESULT_SKIPPED:
Gilles Peskine51dcc242019-09-16 15:13:48 +0200469 mbedtls_fprintf( outcome_file, "SKIP;Runtime skip" );
470 break;
471 default:
472 mbedtls_fprintf( outcome_file, "FAIL;%s:%d:%s",
473 info->filename, info->line_no,
474 info->test );
475 break;
476 }
477 break;
478 case DISPATCH_TEST_FN_NOT_FOUND:
479 mbedtls_fprintf( outcome_file, "FAIL;Test function not found" );
480 break;
481 case DISPATCH_INVALID_TEST_DATA:
482 mbedtls_fprintf( outcome_file, "FAIL;Invalid test data" );
483 break;
484 case DISPATCH_UNSUPPORTED_SUITE:
485 mbedtls_fprintf( outcome_file, "SKIP;Unsupported suite" );
486 break;
487 default:
488 mbedtls_fprintf( outcome_file, "FAIL;Unknown cause" );
489 break;
490 }
491 mbedtls_fprintf( outcome_file, "\n" );
492 fflush( outcome_file );
493}
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100494
495/**
496 * \brief Desktop implementation of execute_tests().
497 * Parses command line and executes tests from
498 * supplied or default data file.
499 *
500 * \param argc Command line argument count.
501 * \param argv Argument array.
502 *
503 * \return Program exit status.
504 */
505int execute_tests( int argc , const char ** argv )
506{
507 /* Local Configurations and options */
508 const char *default_filename = "DATA_FILE";
509 const char *test_filename = NULL;
510 const char **test_files = NULL;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200511 size_t testfile_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100512 int option_verbose = 0;
k-stachowiak03954f22019-09-16 10:23:10 +0200513 size_t function_id = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100514
515 /* Other Local variables */
516 int arg_index = 1;
517 const char *next_arg;
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200518 size_t testfile_index, i, cnt;
519 int ret;
520 unsigned total_errors = 0, total_tests = 0, total_skipped = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100521 FILE *file;
522 char buf[5000];
523 char *params[50];
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100524 /* Store for proccessed integer params. */
525 int int_params[50];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100526 void *pointer;
527#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
528 int stdout_fd = -1;
529#endif /* __unix__ || __APPLE__ __MACH__ */
Gilles Peskine51dcc242019-09-16 15:13:48 +0200530 const char *outcome_file_name = getenv( "MBEDTLS_TEST_OUTCOME_FILE" );
531 FILE *outcome_file = NULL;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100532
533#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
534 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
535 unsigned char alloc_buf[1000000];
536 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
537#endif
538
Gilles Peskine1061ec62021-01-29 21:17:11 +0100539#if defined(MBEDTLS_TEST_MUTEX_USAGE)
540 mbedtls_test_mutex_usage_init( );
541#endif
542
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100543 /*
544 * The C standard doesn't guarantee that all-bits-0 is the representation
545 * of a NULL pointer. We do however use that in our code for initializing
546 * structures, which should work on every modern platform. Let's be sure.
547 */
548 memset( &pointer, 0, sizeof( void * ) );
549 if( pointer != NULL )
550 {
551 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
552 return( 1 );
553 }
554
555 /*
556 * Make sure we have a snprintf that correctly zero-terminates
557 */
558 if( run_test_snprintf() != 0 )
559 {
560 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
Azim Khan191e9042017-06-09 12:39:00 +0100561 return( 1 );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100562 }
563
gabor-mezei-arm88d7eee2020-04-22 11:07:34 +0200564 if( outcome_file_name != NULL && *outcome_file_name != '\0' )
Gilles Peskine9c673232020-01-21 18:03:56 +0100565 {
566 outcome_file = fopen( outcome_file_name, "a" );
567 if( outcome_file == NULL )
568 {
569 mbedtls_fprintf( stderr, "Unable to open outcome file. Continuing anyway.\n" );
570 }
571 }
572
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100573 while( arg_index < argc )
574 {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100575 next_arg = argv[arg_index];
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100576
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100577 if( strcmp( next_arg, "--verbose" ) == 0 ||
578 strcmp( next_arg, "-v" ) == 0 )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100579 {
580 option_verbose = 1;
581 }
582 else if( strcmp(next_arg, "--help" ) == 0 ||
583 strcmp(next_arg, "-h" ) == 0 )
584 {
585 mbedtls_fprintf( stdout, USAGE );
586 mbedtls_exit( EXIT_SUCCESS );
587 }
588 else
589 {
590 /* Not an option, therefore treat all further arguments as the file
591 * list.
592 */
593 test_files = &argv[ arg_index ];
594 testfile_count = argc - arg_index;
595 }
596
597 arg_index++;
598 }
599
600 /* If no files were specified, assume a default */
601 if ( test_files == NULL || testfile_count == 0 )
602 {
603 test_files = &default_filename;
604 testfile_count = 1;
605 }
606
607 /* Initialize the struct that holds information about the last test */
Chris Jones567e0ad2021-02-03 12:07:01 +0000608 mbedtls_test_info_reset( );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100609
610 /* Now begin to execute the tests in the testfiles */
611 for ( testfile_index = 0;
612 testfile_index < testfile_count;
613 testfile_index++ )
614 {
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200615 size_t unmet_dep_count = 0;
Gilles Peskine06725c92020-03-30 21:39:09 +0200616 int unmet_dependencies[20];
Ronald Cron67a8a372020-04-01 16:04:41 +0200617 int missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100618
619 test_filename = test_files[ testfile_index ];
620
621 file = fopen( test_filename, "r" );
622 if( file == NULL )
623 {
624 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
625 test_filename );
Gilles Peskine9c673232020-01-21 18:03:56 +0100626 if( outcome_file != NULL )
627 fclose( outcome_file );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100628 return( 1 );
629 }
630
631 while( !feof( file ) )
632 {
633 if( unmet_dep_count > 0 )
634 {
635 mbedtls_fprintf( stderr,
636 "FATAL: Dep count larger than zero at start of loop\n" );
637 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
638 }
639 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200640 missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100641
642 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
643 break;
Hanno Beckere69d0152019-07-05 13:31:30 +0100644 mbedtls_fprintf( stdout, "%s%.66s",
Chris Jonese60e2ae2021-01-20 17:51:47 +0000645 mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ?
646 "\n" : "", buf );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100647 mbedtls_fprintf( stdout, " " );
648 for( i = strlen( buf ) + 1; i < 67; i++ )
649 mbedtls_fprintf( stdout, "." );
650 mbedtls_fprintf( stdout, " " );
651 fflush( stdout );
Gilles Peskine51dcc242019-09-16 15:13:48 +0200652 write_outcome_entry( outcome_file, argv[0], buf );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100653
654 total_tests++;
655
656 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
657 break;
658 cnt = parse_arguments( buf, strlen( buf ), params,
659 sizeof( params ) / sizeof( params[0] ) );
660
661 if( strcmp( params[0], "depends_on" ) == 0 )
662 {
663 for( i = 1; i < cnt; i++ )
664 {
665 int dep_id = strtol( params[i], NULL, 10 );
666 if( dep_check( dep_id ) != DEPENDENCY_SUPPORTED )
667 {
Ronald Crone1a05a52020-04-01 15:52:06 +0200668 if( unmet_dep_count <
669 ARRAY_LENGTH( unmet_dependencies ) )
670 {
671 unmet_dependencies[unmet_dep_count] = dep_id;
672 unmet_dep_count++;
673 }
Ronald Cron67a8a372020-04-01 16:04:41 +0200674 else
675 {
676 missing_unmet_dependencies = 1;
677 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100678 }
679 }
680
681 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
682 break;
683 cnt = parse_arguments( buf, strlen( buf ), params,
684 sizeof( params ) / sizeof( params[0] ) );
685 }
686
687 // If there are no unmet dependencies execute the test
688 if( unmet_dep_count == 0 )
689 {
Chris Jones567e0ad2021-02-03 12:07:01 +0000690 mbedtls_test_info_reset( );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100691
692#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
693 /* Suppress all output from the library unless we're verbose
694 * mode
695 */
696 if( !option_verbose )
697 {
gufe44067f6e02020-07-30 09:02:27 +0200698 stdout_fd = redirect_output( stdout, "/dev/null" );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100699 if( stdout_fd == -1 )
700 {
701 /* Redirection has failed with no stdout so exit */
702 exit( 1 );
703 }
704 }
705#endif /* __unix__ || __APPLE__ __MACH__ */
706
k-stachowiak03954f22019-09-16 10:23:10 +0200707 function_id = strtoul( params[0], NULL, 10 );
Azim Khan13c6bfb2017-06-15 14:45:56 +0100708 if ( (ret = check_test( function_id )) == DISPATCH_TEST_SUCCESS )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100709 {
Azim Khan13c6bfb2017-06-15 14:45:56 +0100710 ret = convert_params( cnt - 1, params + 1, int_params );
711 if ( DISPATCH_TEST_SUCCESS == ret )
712 {
713 ret = dispatch_test( function_id, (void **)( params + 1 ) );
714 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100715 }
716
717#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +0200718 if( !option_verbose && restore_output( stdout, stdout_fd ) )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100719 {
720 /* Redirection has failed with no stdout so exit */
721 exit( 1 );
722 }
723#endif /* __unix__ || __APPLE__ __MACH__ */
724
725 }
726
Gilles Peskine51dcc242019-09-16 15:13:48 +0200727 write_outcome_result( outcome_file,
728 unmet_dep_count, unmet_dependencies,
Ronald Cron67a8a372020-04-01 16:04:41 +0200729 missing_unmet_dependencies,
Chris Jonese60e2ae2021-01-20 17:51:47 +0000730 ret, &mbedtls_test_info );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100731 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
732 {
733 total_skipped++;
734 mbedtls_fprintf( stdout, "----" );
735
736 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
737 {
738 mbedtls_fprintf( stdout, "\n Test Suite not enabled" );
739 }
740
741 if( 1 == option_verbose && unmet_dep_count > 0 )
742 {
743 mbedtls_fprintf( stdout, "\n Unmet dependencies: " );
744 for( i = 0; i < unmet_dep_count; i++ )
745 {
Gilles Peskine06725c92020-03-30 21:39:09 +0200746 mbedtls_fprintf( stdout, "%d ",
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100747 unmet_dependencies[i] );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100748 }
Ronald Cron67a8a372020-04-01 16:04:41 +0200749 if( missing_unmet_dependencies )
750 mbedtls_fprintf( stdout, "..." );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100751 }
752 mbedtls_fprintf( stdout, "\n" );
753 fflush( stdout );
754
755 unmet_dep_count = 0;
Ronald Cron67a8a372020-04-01 16:04:41 +0200756 missing_unmet_dependencies = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100757 }
758 else if( ret == DISPATCH_TEST_SUCCESS )
759 {
Chris Jonese60e2ae2021-01-20 17:51:47 +0000760 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100761 {
762 mbedtls_fprintf( stdout, "PASS\n" );
763 }
Chris Jonese60e2ae2021-01-20 17:51:47 +0000764 else if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SKIPPED )
Hanno Beckere69d0152019-07-05 13:31:30 +0100765 {
766 mbedtls_fprintf( stdout, "----\n" );
767 total_skipped++;
768 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100769 else
770 {
771 total_errors++;
772 mbedtls_fprintf( stdout, "FAILED\n" );
Gilles Peskine56055912019-03-01 14:26:30 +0100773 mbedtls_fprintf( stdout, " %s\n at ",
Chris Jonese60e2ae2021-01-20 17:51:47 +0000774 mbedtls_test_info.test );
775 if( mbedtls_test_info.step != (unsigned long)( -1 ) )
Gilles Peskine56055912019-03-01 14:26:30 +0100776 {
777 mbedtls_fprintf( stdout, "step %lu, ",
Chris Jonese60e2ae2021-01-20 17:51:47 +0000778 mbedtls_test_info.step );
Gilles Peskine56055912019-03-01 14:26:30 +0100779 }
780 mbedtls_fprintf( stdout, "line %d, %s",
Chris Jonese60e2ae2021-01-20 17:51:47 +0000781 mbedtls_test_info.line_no,
782 mbedtls_test_info.filename );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100783 }
784 fflush( stdout );
785 }
786 else if( ret == DISPATCH_INVALID_TEST_DATA )
787 {
788 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
789 fclose( file );
790 mbedtls_exit( 2 );
791 }
792 else if( ret == DISPATCH_TEST_FN_NOT_FOUND )
793 {
Gilles Peskine31fccc82019-09-16 15:12:37 +0200794 mbedtls_fprintf( stderr, "FAILED: FATAL TEST FUNCTION NOT FOUND\n" );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100795 fclose( file );
796 mbedtls_exit( 2 );
797 }
798 else
799 total_errors++;
800 }
801 fclose( file );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100802 }
803
Gilles Peskine51dcc242019-09-16 15:13:48 +0200804 if( outcome_file != NULL )
805 fclose( outcome_file );
806
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100807 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
808 if( total_errors == 0 )
809 mbedtls_fprintf( stdout, "PASSED" );
810 else
811 mbedtls_fprintf( stdout, "FAILED" );
812
Gilles Peskine3c1c8ea2019-09-16 15:10:47 +0200813 mbedtls_fprintf( stdout, " (%u / %u tests (%u skipped))\n",
814 total_tests - total_errors, total_tests, total_skipped );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100815
816#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
817 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
818#if defined(MBEDTLS_MEMORY_DEBUG)
819 mbedtls_memory_buffer_alloc_status();
820#endif
821 mbedtls_memory_buffer_alloc_free();
822#endif
823
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100824 return( total_errors != 0 );
825}