Mohammad Azim Khan | 9540261 | 2017-07-19 10:15:54 +0100 | [diff] [blame] | 1 | #line 2 "suites/host_test.function" |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 2 | |
| 3 | /** |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 4 | * \brief Verifies that string is in string parameter format i.e. "<str>" |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 5 | * It also strips enclosing '"' from the input string. |
| 6 | * |
| 7 | * \param str String parameter. |
| 8 | * |
| 9 | * \return 0 if success else 1 |
| 10 | */ |
| 11 | int verify_string( char **str ) |
| 12 | { |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 13 | if( ( *str )[0] != '"' || |
| 14 | ( *str )[strlen( *str ) - 1] != '"' ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 15 | { |
| 16 | mbedtls_fprintf( stderr, |
| 17 | "Expected string (with \"\") for parameter and got: %s\n", *str ); |
| 18 | return( -1 ); |
| 19 | } |
| 20 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 21 | ( *str )++; |
| 22 | ( *str )[strlen( *str ) - 1] = '\0'; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 23 | |
| 24 | return( 0 ); |
| 25 | } |
| 26 | |
| 27 | /** |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 28 | * \brief Verifies that string is an integer. Also gives the converted |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 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 | */ |
| 36 | int 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 Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 52 | str[i - 1] == '0' && ( str[i] == 'x' || str[i] == 'X' ) ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 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" \ |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 90 | " 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 Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 93 | " %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 | */ |
| 110 | int 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 | */ |
| 160 | static 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 Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 169 | while( *p != '\0' && p < ( buf + len ) ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 170 | { |
| 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 Peskine | e7655df | 2019-06-07 14:52:07 +0200 | [diff] [blame] | 182 | TEST_HELPER_ASSERT( cnt < params_len ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 183 | 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 Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 199 | if( *p == '\\' && *( p + 1 ) == 'n' ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 200 | { |
| 201 | p += 2; |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 202 | *( q++ ) = '\n'; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 203 | } |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 204 | else if( *p == '\\' && *( p + 1 ) == ':' ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 205 | { |
| 206 | p += 2; |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 207 | *( q++ ) = ':'; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 208 | } |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 209 | else if( *p == '\\' && *( p + 1 ) == '?' ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 210 | { |
| 211 | p += 2; |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 212 | *( q++ ) = '?'; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 213 | } |
| 214 | else |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 215 | *( q++ ) = *( p++ ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 216 | } |
| 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 Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 235 | * \param cnt Parameter array count. |
| 236 | * \param params Out array of found parameters. |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 237 | * \param int_params_store Memory for storing processed integer parameters. |
| 238 | * |
| 239 | * \return 0 for success else 1 |
| 240 | */ |
| 241 | static int convert_params( size_t cnt , char ** params , int * int_params_store ) |
| 242 | { |
| 243 | char ** cur = params; |
| 244 | char ** out = params; |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 245 | int ret = DISPATCH_TEST_SUCCESS; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 246 | |
Azim Khan | 8d686bf | 2018-07-04 23:29:46 +0100 | [diff] [blame] | 247 | while ( cur < params + cnt ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 248 | { |
| 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 Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 266 | if ( verify_int( val, int_params_store ) == 0 ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 267 | { |
| 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 Khan | 184447e | 2017-05-31 20:29:36 +0100 | [diff] [blame] | 278 | if ( verify_string( &val ) == 0 ) |
| 279 | { |
Azim Khan | 184447e | 2017-05-31 20:29:36 +0100 | [diff] [blame] | 280 | *int_params_store = unhexify( (unsigned char *) val, val ); |
Azim Khan | 184447e | 2017-05-31 20:29:36 +0100 | [diff] [blame] | 281 | *out++ = val; |
| 282 | *out++ = (char *)(int_params_store++); |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | ret = ( DISPATCH_INVALID_TEST_DATA ); |
| 287 | break; |
| 288 | } |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 289 | } |
| 290 | else if ( strcmp( type, "exp" ) == 0 ) |
| 291 | { |
| 292 | int exp_id = strtol( val, NULL, 10 ); |
| 293 | if ( get_expression ( exp_id, int_params_store ) == 0 ) |
| 294 | { |
| 295 | *out++ = (char *)int_params_store++; |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | ret = ( DISPATCH_INVALID_TEST_DATA ); |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | ret = ( DISPATCH_INVALID_TEST_DATA ); |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | return( ret ); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * \brief Tests snprintf implementation with test input. |
| 314 | * |
Azim Khan | 191e904 | 2017-06-09 12:39:00 +0100 | [diff] [blame] | 315 | * \note |
| 316 | * At high optimization levels (e.g. gcc -O3), this function may be |
| 317 | * inlined in run_test_snprintf. This can trigger a spurious warning about |
| 318 | * potential misuse of snprintf from gcc -Wformat-truncation (observed with |
| 319 | * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc |
| 320 | * only. They are still valid for other compilers. Avoid this warning by |
| 321 | * forbidding inlining of this function by gcc. |
| 322 | * |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 323 | * \param n Buffer test length. |
| 324 | * \param ref_buf Expected buffer. |
| 325 | * \param ref_ret Expected snprintf return value. |
| 326 | * |
| 327 | * \return 0 for success else 1 |
| 328 | */ |
Azim Khan | 191e904 | 2017-06-09 12:39:00 +0100 | [diff] [blame] | 329 | #if defined(__GNUC__) |
| 330 | __attribute__((__noinline__)) |
| 331 | #endif |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 332 | static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret ) |
| 333 | { |
| 334 | int ret; |
| 335 | char buf[10] = "xxxxxxxxx"; |
| 336 | const char ref[10] = "xxxxxxxxx"; |
| 337 | |
Mohammad Azim Khan | 7613534 | 2018-04-12 13:23:01 +0100 | [diff] [blame] | 338 | if( n >= sizeof( buf ) ) |
| 339 | return( -1 ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 340 | ret = mbedtls_snprintf( buf, n, "%s", "123" ); |
| 341 | if( ret < 0 || (size_t) ret >= n ) |
| 342 | ret = -1; |
| 343 | |
| 344 | if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 || |
| 345 | ref_ret != ret || |
| 346 | memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 ) |
| 347 | { |
| 348 | return( 1 ); |
| 349 | } |
| 350 | |
| 351 | return( 0 ); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * \brief Tests snprintf implementation. |
| 356 | * |
| 357 | * \param none |
| 358 | * |
| 359 | * \return 0 for success else 1 |
| 360 | */ |
| 361 | static int run_test_snprintf( void ) |
| 362 | { |
| 363 | return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 || |
| 364 | test_snprintf( 1, "", -1 ) != 0 || |
| 365 | test_snprintf( 2, "1", -1 ) != 0 || |
| 366 | test_snprintf( 3, "12", -1 ) != 0 || |
| 367 | test_snprintf( 4, "123", 3 ) != 0 || |
| 368 | test_snprintf( 5, "123", 3 ) != 0 ); |
| 369 | } |
| 370 | |
Gilles Peskine | 51dcc24 | 2019-09-16 15:13:48 +0200 | [diff] [blame] | 371 | /** \brief Write the description of the test case to the outcome CSV file. |
| 372 | * |
| 373 | * \param outcome_file The file to write to. |
| 374 | * If this is \c NULL, this function does nothing. |
| 375 | * \param argv0 The test suite name. |
| 376 | * \param test_case The test case description. |
| 377 | */ |
| 378 | static void write_outcome_entry( FILE *outcome_file, |
| 379 | const char *argv0, |
| 380 | const char *test_case ) |
| 381 | { |
| 382 | /* The non-varying fields are initialized on first use. */ |
| 383 | static const char *platform = NULL; |
| 384 | static const char *configuration = NULL; |
| 385 | static const char *test_suite = NULL; |
| 386 | |
| 387 | if( outcome_file == NULL ) |
| 388 | return; |
| 389 | |
| 390 | if( platform == NULL ) |
| 391 | { |
| 392 | platform = getenv( "MBEDTLS_TEST_PLATFORM" ); |
| 393 | if( platform == NULL ) |
| 394 | platform = "unknown"; |
| 395 | } |
| 396 | if( configuration == NULL ) |
| 397 | { |
| 398 | configuration = getenv( "MBEDTLS_TEST_CONFIGURATION" ); |
| 399 | if( configuration == NULL ) |
| 400 | configuration = "unknown"; |
| 401 | } |
| 402 | if( test_suite == NULL ) |
| 403 | { |
| 404 | test_suite = strrchr( argv0, '/' ); |
| 405 | if( test_suite != NULL ) |
| 406 | test_suite += 1; // skip the '/' |
| 407 | else |
| 408 | test_suite = argv0; |
| 409 | } |
| 410 | |
| 411 | /* Write the beginning of the outcome line. |
| 412 | * Ignore errors: writing the outcome file is on a best-effort basis. */ |
| 413 | mbedtls_fprintf( outcome_file, "%s;%s;%s;%s;", |
| 414 | platform, configuration, test_suite, test_case ); |
| 415 | } |
| 416 | |
| 417 | /** \brief Write the result of the test case to the outcome CSV file. |
| 418 | * |
| 419 | * \param outcome_file The file to write to. |
| 420 | * If this is \c NULL, this function does nothing. |
| 421 | * \param unmet_dep_count The number of unmet dependencies. |
| 422 | * \param unmet_dependencies The array of unmet dependencies. |
| 423 | * \param ret The test dispatch status (DISPATCH_xxx). |
| 424 | * \param test_info A pointer to the test info structure. |
| 425 | */ |
| 426 | static void write_outcome_result( FILE *outcome_file, |
| 427 | size_t unmet_dep_count, |
| 428 | char *unmet_dependencies[], |
| 429 | int ret, |
| 430 | const test_info_t *info ) |
| 431 | { |
| 432 | if( outcome_file == NULL ) |
| 433 | return; |
| 434 | |
| 435 | /* Write the end of the outcome line. |
| 436 | * Ignore errors: writing the outcome file is on a best-effort basis. */ |
| 437 | switch( ret ) |
| 438 | { |
| 439 | case DISPATCH_TEST_SUCCESS: |
| 440 | if( unmet_dep_count > 0 ) |
| 441 | { |
| 442 | size_t i; |
| 443 | mbedtls_fprintf( outcome_file, "SKIP" ); |
| 444 | for( i = 0; i < unmet_dep_count; i++ ) |
| 445 | { |
| 446 | mbedtls_fprintf( outcome_file, "%c%s", |
| 447 | i == 0 ? ';' : ':', |
| 448 | unmet_dependencies[i] ); |
| 449 | } |
| 450 | break; |
| 451 | } |
| 452 | switch( info->result ) |
| 453 | { |
| 454 | case TEST_RESULT_SUCCESS: |
| 455 | mbedtls_fprintf( outcome_file, "PASS;" ); |
| 456 | break; |
| 457 | case TEST_RESULT_SKIPPED: |
| 458 | mbedtls_fprintf( outcome_file, "SKIP;Runtime skip" ); |
| 459 | break; |
| 460 | default: |
| 461 | mbedtls_fprintf( outcome_file, "FAIL;%s:%d:%s", |
| 462 | info->filename, info->line_no, |
| 463 | info->test ); |
| 464 | break; |
| 465 | } |
| 466 | break; |
| 467 | case DISPATCH_TEST_FN_NOT_FOUND: |
| 468 | mbedtls_fprintf( outcome_file, "FAIL;Test function not found" ); |
| 469 | break; |
| 470 | case DISPATCH_INVALID_TEST_DATA: |
| 471 | mbedtls_fprintf( outcome_file, "FAIL;Invalid test data" ); |
| 472 | break; |
| 473 | case DISPATCH_UNSUPPORTED_SUITE: |
| 474 | mbedtls_fprintf( outcome_file, "SKIP;Unsupported suite" ); |
| 475 | break; |
| 476 | default: |
| 477 | mbedtls_fprintf( outcome_file, "FAIL;Unknown cause" ); |
| 478 | break; |
| 479 | } |
| 480 | mbedtls_fprintf( outcome_file, "\n" ); |
| 481 | fflush( outcome_file ); |
| 482 | } |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 483 | |
| 484 | /** |
| 485 | * \brief Desktop implementation of execute_tests(). |
| 486 | * Parses command line and executes tests from |
| 487 | * supplied or default data file. |
| 488 | * |
| 489 | * \param argc Command line argument count. |
| 490 | * \param argv Argument array. |
| 491 | * |
| 492 | * \return Program exit status. |
| 493 | */ |
| 494 | int execute_tests( int argc , const char ** argv ) |
| 495 | { |
| 496 | /* Local Configurations and options */ |
| 497 | const char *default_filename = "DATA_FILE"; |
| 498 | const char *test_filename = NULL; |
| 499 | const char **test_files = NULL; |
Gilles Peskine | 3c1c8ea | 2019-09-16 15:10:47 +0200 | [diff] [blame] | 500 | size_t testfile_count = 0; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 501 | int option_verbose = 0; |
Azim Khan | 13c6bfb | 2017-06-15 14:45:56 +0100 | [diff] [blame] | 502 | int function_id = 0; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 503 | |
| 504 | /* Other Local variables */ |
| 505 | int arg_index = 1; |
| 506 | const char *next_arg; |
Gilles Peskine | 3c1c8ea | 2019-09-16 15:10:47 +0200 | [diff] [blame] | 507 | size_t testfile_index, i, cnt; |
| 508 | int ret; |
| 509 | unsigned total_errors = 0, total_tests = 0, total_skipped = 0; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 510 | FILE *file; |
| 511 | char buf[5000]; |
| 512 | char *params[50]; |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 513 | /* Store for proccessed integer params. */ |
| 514 | int int_params[50]; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 515 | void *pointer; |
| 516 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 517 | int stdout_fd = -1; |
| 518 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
Gilles Peskine | 51dcc24 | 2019-09-16 15:13:48 +0200 | [diff] [blame] | 519 | const char *outcome_file_name = getenv( "MBEDTLS_TEST_OUTCOME_FILE" ); |
| 520 | FILE *outcome_file = NULL; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 521 | |
| 522 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \ |
| 523 | !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC) |
| 524 | unsigned char alloc_buf[1000000]; |
| 525 | mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) ); |
| 526 | #endif |
| 527 | |
| 528 | /* |
| 529 | * The C standard doesn't guarantee that all-bits-0 is the representation |
| 530 | * of a NULL pointer. We do however use that in our code for initializing |
| 531 | * structures, which should work on every modern platform. Let's be sure. |
| 532 | */ |
| 533 | memset( &pointer, 0, sizeof( void * ) ); |
| 534 | if( pointer != NULL ) |
| 535 | { |
| 536 | mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" ); |
| 537 | return( 1 ); |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * Make sure we have a snprintf that correctly zero-terminates |
| 542 | */ |
| 543 | if( run_test_snprintf() != 0 ) |
| 544 | { |
| 545 | mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" ); |
Azim Khan | 191e904 | 2017-06-09 12:39:00 +0100 | [diff] [blame] | 546 | return( 1 ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 547 | } |
| 548 | |
Gilles Peskine | 9c67323 | 2020-01-21 18:03:56 +0100 | [diff] [blame] | 549 | if( outcome_file_name != NULL ) |
| 550 | { |
| 551 | outcome_file = fopen( outcome_file_name, "a" ); |
| 552 | if( outcome_file == NULL ) |
| 553 | { |
| 554 | mbedtls_fprintf( stderr, "Unable to open outcome file. Continuing anyway.\n" ); |
| 555 | } |
| 556 | } |
| 557 | |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 558 | while( arg_index < argc ) |
| 559 | { |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 560 | next_arg = argv[arg_index]; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 561 | |
Mohammad Azim Khan | d2d0112 | 2018-07-18 17:48:37 +0100 | [diff] [blame] | 562 | if( strcmp( next_arg, "--verbose" ) == 0 || |
| 563 | strcmp( next_arg, "-v" ) == 0 ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 564 | { |
| 565 | option_verbose = 1; |
| 566 | } |
| 567 | else if( strcmp(next_arg, "--help" ) == 0 || |
| 568 | strcmp(next_arg, "-h" ) == 0 ) |
| 569 | { |
| 570 | mbedtls_fprintf( stdout, USAGE ); |
| 571 | mbedtls_exit( EXIT_SUCCESS ); |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | /* Not an option, therefore treat all further arguments as the file |
| 576 | * list. |
| 577 | */ |
| 578 | test_files = &argv[ arg_index ]; |
| 579 | testfile_count = argc - arg_index; |
| 580 | } |
| 581 | |
| 582 | arg_index++; |
| 583 | } |
| 584 | |
| 585 | /* If no files were specified, assume a default */ |
| 586 | if ( test_files == NULL || testfile_count == 0 ) |
| 587 | { |
| 588 | test_files = &default_filename; |
| 589 | testfile_count = 1; |
| 590 | } |
| 591 | |
| 592 | /* Initialize the struct that holds information about the last test */ |
| 593 | memset( &test_info, 0, sizeof( test_info ) ); |
| 594 | |
| 595 | /* Now begin to execute the tests in the testfiles */ |
| 596 | for ( testfile_index = 0; |
| 597 | testfile_index < testfile_count; |
| 598 | testfile_index++ ) |
| 599 | { |
Gilles Peskine | 3c1c8ea | 2019-09-16 15:10:47 +0200 | [diff] [blame] | 600 | size_t unmet_dep_count = 0; |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 601 | char *unmet_dependencies[20]; |
| 602 | |
| 603 | test_filename = test_files[ testfile_index ]; |
| 604 | |
| 605 | file = fopen( test_filename, "r" ); |
| 606 | if( file == NULL ) |
| 607 | { |
| 608 | mbedtls_fprintf( stderr, "Failed to open test file: %s\n", |
| 609 | test_filename ); |
Gilles Peskine | 9c67323 | 2020-01-21 18:03:56 +0100 | [diff] [blame] | 610 | if( outcome_file != NULL ) |
| 611 | fclose( outcome_file ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 612 | return( 1 ); |
| 613 | } |
| 614 | |
| 615 | while( !feof( file ) ) |
| 616 | { |
| 617 | if( unmet_dep_count > 0 ) |
| 618 | { |
| 619 | mbedtls_fprintf( stderr, |
| 620 | "FATAL: Dep count larger than zero at start of loop\n" ); |
| 621 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
| 622 | } |
| 623 | unmet_dep_count = 0; |
| 624 | |
| 625 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 626 | break; |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 627 | mbedtls_fprintf( stdout, "%s%.66s", |
| 628 | test_info.result == TEST_RESULT_FAILED ? "\n" : "", buf ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 629 | mbedtls_fprintf( stdout, " " ); |
| 630 | for( i = strlen( buf ) + 1; i < 67; i++ ) |
| 631 | mbedtls_fprintf( stdout, "." ); |
| 632 | mbedtls_fprintf( stdout, " " ); |
| 633 | fflush( stdout ); |
Gilles Peskine | 51dcc24 | 2019-09-16 15:13:48 +0200 | [diff] [blame] | 634 | write_outcome_entry( outcome_file, argv[0], buf ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 635 | |
| 636 | total_tests++; |
| 637 | |
| 638 | if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 ) |
| 639 | break; |
| 640 | cnt = parse_arguments( buf, strlen( buf ), params, |
| 641 | sizeof( params ) / sizeof( params[0] ) ); |
| 642 | |
| 643 | if( strcmp( params[0], "depends_on" ) == 0 ) |
| 644 | { |
| 645 | for( i = 1; i < cnt; i++ ) |
| 646 | { |
| 647 | int dep_id = strtol( params[i], NULL, 10 ); |
| 648 | if( dep_check( dep_id ) != DEPENDENCY_SUPPORTED ) |
| 649 | { |
| 650 | if( 0 == option_verbose ) |
| 651 | { |
| 652 | /* Only one count is needed if not verbose */ |
| 653 | unmet_dep_count++; |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | unmet_dependencies[ unmet_dep_count ] = strdup( params[i] ); |
| 658 | if( unmet_dependencies[ unmet_dep_count ] == NULL ) |
| 659 | { |
| 660 | mbedtls_fprintf( stderr, "FATAL: Out of memory\n" ); |
| 661 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
| 662 | } |
| 663 | unmet_dep_count++; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 ) |
| 668 | break; |
| 669 | cnt = parse_arguments( buf, strlen( buf ), params, |
| 670 | sizeof( params ) / sizeof( params[0] ) ); |
| 671 | } |
| 672 | |
| 673 | // If there are no unmet dependencies execute the test |
| 674 | if( unmet_dep_count == 0 ) |
| 675 | { |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 676 | test_info.result = TEST_RESULT_SUCCESS; |
Simon Butcher | 6542f6c | 2018-12-09 22:09:59 +0000 | [diff] [blame] | 677 | test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_IDLE; |
Gilles Peskine | 5605591 | 2019-03-01 14:26:30 +0100 | [diff] [blame] | 678 | test_info.step = (unsigned long)( -1 ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 679 | |
| 680 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 681 | /* Suppress all output from the library unless we're verbose |
| 682 | * mode |
| 683 | */ |
| 684 | if( !option_verbose ) |
| 685 | { |
| 686 | stdout_fd = redirect_output( &stdout, "/dev/null" ); |
| 687 | if( stdout_fd == -1 ) |
| 688 | { |
| 689 | /* Redirection has failed with no stdout so exit */ |
| 690 | exit( 1 ); |
| 691 | } |
| 692 | } |
| 693 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
| 694 | |
Azim Khan | 13c6bfb | 2017-06-15 14:45:56 +0100 | [diff] [blame] | 695 | function_id = strtol( params[0], NULL, 10 ); |
| 696 | if ( (ret = check_test( function_id )) == DISPATCH_TEST_SUCCESS ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 697 | { |
Azim Khan | 13c6bfb | 2017-06-15 14:45:56 +0100 | [diff] [blame] | 698 | ret = convert_params( cnt - 1, params + 1, int_params ); |
| 699 | if ( DISPATCH_TEST_SUCCESS == ret ) |
| 700 | { |
| 701 | ret = dispatch_test( function_id, (void **)( params + 1 ) ); |
| 702 | } |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 706 | if( !option_verbose && restore_output( &stdout, stdout_fd ) ) |
| 707 | { |
| 708 | /* Redirection has failed with no stdout so exit */ |
| 709 | exit( 1 ); |
| 710 | } |
| 711 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
| 712 | |
| 713 | } |
| 714 | |
Gilles Peskine | 51dcc24 | 2019-09-16 15:13:48 +0200 | [diff] [blame] | 715 | write_outcome_result( outcome_file, |
| 716 | unmet_dep_count, unmet_dependencies, |
| 717 | ret, &test_info ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 718 | if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE ) |
| 719 | { |
| 720 | total_skipped++; |
| 721 | mbedtls_fprintf( stdout, "----" ); |
| 722 | |
| 723 | if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE ) |
| 724 | { |
| 725 | mbedtls_fprintf( stdout, "\n Test Suite not enabled" ); |
| 726 | } |
| 727 | |
| 728 | if( 1 == option_verbose && unmet_dep_count > 0 ) |
| 729 | { |
| 730 | mbedtls_fprintf( stdout, "\n Unmet dependencies: " ); |
| 731 | for( i = 0; i < unmet_dep_count; i++ ) |
| 732 | { |
| 733 | mbedtls_fprintf( stdout, "%s ", |
| 734 | unmet_dependencies[i] ); |
| 735 | free( unmet_dependencies[i] ); |
| 736 | } |
| 737 | } |
| 738 | mbedtls_fprintf( stdout, "\n" ); |
| 739 | fflush( stdout ); |
| 740 | |
| 741 | unmet_dep_count = 0; |
| 742 | } |
| 743 | else if( ret == DISPATCH_TEST_SUCCESS ) |
| 744 | { |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 745 | if( test_info.result == TEST_RESULT_SUCCESS ) |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 746 | { |
| 747 | mbedtls_fprintf( stdout, "PASS\n" ); |
| 748 | } |
Hanno Becker | e69d015 | 2019-07-05 13:31:30 +0100 | [diff] [blame] | 749 | else if( test_info.result == TEST_RESULT_SKIPPED ) |
| 750 | { |
| 751 | mbedtls_fprintf( stdout, "----\n" ); |
| 752 | total_skipped++; |
| 753 | } |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 754 | else |
| 755 | { |
| 756 | total_errors++; |
| 757 | mbedtls_fprintf( stdout, "FAILED\n" ); |
Gilles Peskine | 5605591 | 2019-03-01 14:26:30 +0100 | [diff] [blame] | 758 | mbedtls_fprintf( stdout, " %s\n at ", |
| 759 | test_info.test ); |
| 760 | if( test_info.step != (unsigned long)( -1 ) ) |
| 761 | { |
| 762 | mbedtls_fprintf( stdout, "step %lu, ", |
| 763 | test_info.step ); |
| 764 | } |
| 765 | mbedtls_fprintf( stdout, "line %d, %s", |
| 766 | test_info.line_no, test_info.filename ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 767 | } |
| 768 | fflush( stdout ); |
| 769 | } |
| 770 | else if( ret == DISPATCH_INVALID_TEST_DATA ) |
| 771 | { |
| 772 | mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" ); |
| 773 | fclose( file ); |
| 774 | mbedtls_exit( 2 ); |
| 775 | } |
| 776 | else if( ret == DISPATCH_TEST_FN_NOT_FOUND ) |
| 777 | { |
Gilles Peskine | 31fccc8 | 2019-09-16 15:12:37 +0200 | [diff] [blame] | 778 | mbedtls_fprintf( stderr, "FAILED: FATAL TEST FUNCTION NOT FOUND\n" ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 779 | fclose( file ); |
| 780 | mbedtls_exit( 2 ); |
| 781 | } |
| 782 | else |
| 783 | total_errors++; |
| 784 | } |
| 785 | fclose( file ); |
| 786 | |
| 787 | /* In case we encounter early end of file */ |
| 788 | for( i = 0; i < unmet_dep_count; i++ ) |
| 789 | free( unmet_dependencies[i] ); |
| 790 | } |
| 791 | |
Gilles Peskine | 51dcc24 | 2019-09-16 15:13:48 +0200 | [diff] [blame] | 792 | if( outcome_file != NULL ) |
| 793 | fclose( outcome_file ); |
| 794 | |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 795 | mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n"); |
| 796 | if( total_errors == 0 ) |
| 797 | mbedtls_fprintf( stdout, "PASSED" ); |
| 798 | else |
| 799 | mbedtls_fprintf( stdout, "FAILED" ); |
| 800 | |
Gilles Peskine | 3c1c8ea | 2019-09-16 15:10:47 +0200 | [diff] [blame] | 801 | mbedtls_fprintf( stdout, " (%u / %u tests (%u skipped))\n", |
| 802 | total_tests - total_errors, total_tests, total_skipped ); |
Mohammad Azim Khan | fff4904 | 2017-03-28 01:48:31 +0100 | [diff] [blame] | 803 | |
| 804 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \ |
| 805 | !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC) |
| 806 | #if defined(MBEDTLS_MEMORY_DEBUG) |
| 807 | mbedtls_memory_buffer_alloc_status(); |
| 808 | #endif |
| 809 | mbedtls_memory_buffer_alloc_free(); |
| 810 | #endif |
| 811 | |
| 812 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 813 | if( stdout_fd != -1 ) |
| 814 | close_output( stdout ); |
| 815 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
| 816 | |
| 817 | return( total_errors != 0 ); |
| 818 | } |