SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 1 | #line 1 "main_test.function" |
Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 2 | SUITE_PRE_DEP |
| 3 | #define TEST_SUITE_ACTIVE |
| 4 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 5 | int verify_string( char **str ) |
| 6 | { |
| 7 | if( (*str)[0] != '"' || |
| 8 | (*str)[strlen( *str ) - 1] != '"' ) |
| 9 | { |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame^] | 10 | mbedtls_fprintf( stderr, |
| 11 | "Expected string (with \"\") for parameter and got: %s\n", *str ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 12 | return( -1 ); |
| 13 | } |
| 14 | |
| 15 | (*str)++; |
| 16 | (*str)[strlen( *str ) - 1] = '\0'; |
| 17 | |
| 18 | return( 0 ); |
| 19 | } |
| 20 | |
| 21 | int verify_int( char *str, int *value ) |
| 22 | { |
| 23 | size_t i; |
| 24 | int minus = 0; |
| 25 | int digits = 1; |
| 26 | int hex = 0; |
| 27 | |
| 28 | for( i = 0; i < strlen( str ); i++ ) |
| 29 | { |
| 30 | if( i == 0 && str[i] == '-' ) |
| 31 | { |
| 32 | minus = 1; |
| 33 | continue; |
| 34 | } |
| 35 | |
| 36 | if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) && |
| 37 | str[i - 1] == '0' && str[i] == 'x' ) |
| 38 | { |
| 39 | hex = 1; |
| 40 | continue; |
| 41 | } |
| 42 | |
Manuel Pégourié-Gonnard | 725afd8 | 2014-02-01 11:54:28 +0100 | [diff] [blame] | 43 | if( ! ( ( str[i] >= '0' && str[i] <= '9' ) || |
| 44 | ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) || |
| 45 | ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 46 | { |
| 47 | digits = 0; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if( digits ) |
| 53 | { |
| 54 | if( hex ) |
| 55 | *value = strtol( str, NULL, 16 ); |
| 56 | else |
| 57 | *value = strtol( str, NULL, 10 ); |
| 58 | |
| 59 | return( 0 ); |
| 60 | } |
| 61 | |
| 62 | MAPPING_CODE |
| 63 | |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame^] | 64 | mbedtls_fprintf( stderr, |
| 65 | "Expected integer for parameter and got: %s\n", str ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 66 | return( KEY_VALUE_MAPPING_NOT_FOUND ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 67 | } |
| 68 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 69 | |
| 70 | /*----------------------------------------------------------------------------*/ |
| 71 | /* Test Case code */ |
| 72 | |
Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 73 | FUNCTION_CODE |
| 74 | SUITE_POST_DEP |
| 75 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 76 | #line !LINE_NO! "main_test.function" |
| 77 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 78 | |
| 79 | /*----------------------------------------------------------------------------*/ |
| 80 | /* Test dispatch code */ |
| 81 | |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame^] | 82 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 83 | #include <unistd.h> |
| 84 | #endif |
| 85 | #include <stdio.h> |
| 86 | |
| 87 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 88 | int dep_check( char *str ) |
| 89 | { |
| 90 | if( str == NULL ) |
| 91 | return( 1 ); |
| 92 | |
| 93 | DEP_CHECK_CODE |
Simon Butcher | 65b1fa6 | 2016-05-23 23:18:26 +0100 | [diff] [blame] | 94 | #line !LINE_NO! "main_test.function" |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 95 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 96 | return( DEPENDENCY_NOT_SUPPORTED ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 99 | int dispatch_test(int cnt, char *params[50]) |
| 100 | { |
| 101 | int ret; |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 102 | ((void) cnt); |
| 103 | ((void) params); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 104 | |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 105 | #if defined(TEST_SUITE_ACTIVE) |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 106 | ret = DISPATCH_TEST_SUCCESS; |
| 107 | |
Simon Butcher | 65b1fa6 | 2016-05-23 23:18:26 +0100 | [diff] [blame] | 108 | // Cast to void to avoid compiler warnings |
| 109 | (void)ret; |
| 110 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 111 | DISPATCH_FUNCTION |
| 112 | { |
Simon Butcher | 65b1fa6 | 2016-05-23 23:18:26 +0100 | [diff] [blame] | 113 | #line !LINE_NO! "main_test.function" |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 114 | mbedtls_fprintf( stdout, |
| 115 | "FAILED\nSkipping unknown test function '%s'\n", |
| 116 | params[0] ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 117 | fflush( stdout ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 118 | ret = DISPATCH_TEST_FN_NOT_FOUND; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 119 | } |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 120 | #else |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 121 | ret = DISPATCH_UNSUPPORTED_SUITE; |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 122 | #endif |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 123 | return( ret ); |
| 124 | } |
| 125 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 126 | |
| 127 | /*----------------------------------------------------------------------------*/ |
| 128 | /* Main Test code */ |
| 129 | |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 130 | #line !LINE_NO! "main_test.function" |
| 131 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 132 | #define USAGE \ |
| 133 | "Usage: %s [OPTIONS] files...\n\n" \ |
| 134 | " Command line arguments:\n" \ |
| 135 | " files... One or more test data file. If no file is specified\n" \ |
| 136 | " the followimg default test case is used:\n" \ |
| 137 | " %s\n\n" \ |
| 138 | " Options:\n" \ |
| 139 | " -v | --verbose Display full information about each test\n" \ |
| 140 | " -h | --help Display this information\n\n", \ |
| 141 | argv[0], \ |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 142 | "TESTCASE_FILENAME" |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 143 | |
| 144 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 145 | int get_line( FILE *f, char *buf, size_t len ) |
| 146 | { |
| 147 | char *ret; |
| 148 | |
| 149 | ret = fgets( buf, len, f ); |
| 150 | if( ret == NULL ) |
| 151 | return( -1 ); |
| 152 | |
| 153 | if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' ) |
| 154 | buf[strlen(buf) - 1] = '\0'; |
| 155 | if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' ) |
| 156 | buf[strlen(buf) - 1] = '\0'; |
| 157 | |
| 158 | return( 0 ); |
| 159 | } |
| 160 | |
| 161 | int parse_arguments( char *buf, size_t len, char *params[50] ) |
| 162 | { |
| 163 | int cnt = 0, i; |
| 164 | char *cur = buf; |
| 165 | char *p = buf, *q; |
| 166 | |
| 167 | params[cnt++] = cur; |
| 168 | |
| 169 | while( *p != '\0' && p < buf + len ) |
| 170 | { |
| 171 | if( *p == '\\' ) |
| 172 | { |
Manuel Pégourié-Gonnard | 2d5f142 | 2014-01-22 16:01:17 +0100 | [diff] [blame] | 173 | p++; |
| 174 | p++; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 175 | continue; |
| 176 | } |
| 177 | if( *p == ':' ) |
| 178 | { |
| 179 | if( p + 1 < buf + len ) |
| 180 | { |
| 181 | cur = p + 1; |
| 182 | params[cnt++] = cur; |
| 183 | } |
| 184 | *p = '\0'; |
| 185 | } |
| 186 | |
Manuel Pégourié-Gonnard | 2d5f142 | 2014-01-22 16:01:17 +0100 | [diff] [blame] | 187 | p++; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 188 | } |
| 189 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 190 | /* Replace newlines, question marks and colons in strings */ |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 222 | static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret ) |
| 223 | { |
| 224 | int ret; |
| 225 | char buf[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 226 | const char ref[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 227 | |
| 228 | ret = mbedtls_snprintf( buf, n, "%s", "123" ); |
| 229 | if( ret < 0 || (size_t) ret >= n ) |
| 230 | ret = -1; |
| 231 | |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 232 | if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 || |
| 233 | ref_ret != ret || |
| 234 | memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 ) |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 235 | { |
| 236 | return( 1 ); |
| 237 | } |
| 238 | |
| 239 | return( 0 ); |
| 240 | } |
| 241 | |
| 242 | static int run_test_snprintf( void ) |
| 243 | { |
| 244 | return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 || |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 245 | test_snprintf( 1, "", -1 ) != 0 || |
| 246 | test_snprintf( 2, "1", -1 ) != 0 || |
| 247 | test_snprintf( 3, "12", -1 ) != 0 || |
| 248 | test_snprintf( 4, "123", 3 ) != 0 || |
| 249 | test_snprintf( 5, "123", 3 ) != 0 ); |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 252 | int main(int argc, const char *argv[]) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 253 | { |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 254 | /* Local Configurations and options */ |
SimonB | 1594210 | 2016-04-25 21:34:49 +0100 | [diff] [blame] | 255 | const char *default_filename = "TESTCASE_FILENAME"; |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 256 | const char *test_filename = NULL; |
| 257 | const char **test_files = NULL; |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 258 | int testfile_count = 0; |
| 259 | int option_verbose = 0; |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame^] | 260 | int tests_stdout; |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 261 | |
| 262 | /* Other Local variables */ |
| 263 | int arg_index = 1; |
| 264 | const char *next_arg; |
| 265 | int testfile_index, ret, i, cnt; |
| 266 | int total_errors = 0, total_tests = 0, total_skipped = 0; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 267 | FILE *file; |
| 268 | char buf[5000]; |
| 269 | char *params[50]; |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 270 | void *pointer; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 271 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \ |
Manuel Pégourié-Gonnard | 765bb31 | 2014-11-27 11:55:27 +0100 | [diff] [blame] | 273 | !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC) |
Paul Bakker | 1337aff | 2013-09-29 14:45:34 +0200 | [diff] [blame] | 274 | unsigned char alloc_buf[1000000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 276 | #endif |
| 277 | |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 278 | /* |
| 279 | * The C standard doesn't guarantee that all-bits-0 is the representation |
| 280 | * of a NULL pointer. We do however use that in our code for initializing |
| 281 | * structures, which should work on every modern platform. Let's be sure. |
| 282 | */ |
| 283 | memset( &pointer, 0, sizeof( void * ) ); |
| 284 | if( pointer != NULL ) |
| 285 | { |
| 286 | mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" ); |
| 287 | return( 1 ); |
| 288 | } |
| 289 | |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 290 | /* |
| 291 | * Make sure we have a snprintf that correctly zero-terminates |
| 292 | */ |
| 293 | if( run_test_snprintf() != 0 ) |
| 294 | { |
| 295 | mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" ); |
| 296 | return( 0 ); |
| 297 | } |
| 298 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 299 | while( arg_index < argc) |
| 300 | { |
| 301 | next_arg = argv[ arg_index ]; |
| 302 | |
| 303 | if( strcmp(next_arg, "--verbose" ) == 0 || |
| 304 | strcmp(next_arg, "-v" ) == 0 ) |
| 305 | { |
| 306 | option_verbose = 1; |
| 307 | } |
| 308 | else if( strcmp(next_arg, "--help" ) == 0 || |
| 309 | strcmp(next_arg, "-h" ) == 0 ) |
| 310 | { |
| 311 | mbedtls_fprintf( stdout, USAGE ); |
| 312 | mbedtls_exit( EXIT_SUCCESS ); |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | /* Not an option, therefore treat all further arguments as the file |
| 317 | * list. |
| 318 | */ |
| 319 | test_files = &argv[ arg_index ]; |
| 320 | testfile_count = argc - arg_index; |
| 321 | } |
| 322 | |
| 323 | arg_index++; |
| 324 | } |
| 325 | |
| 326 | /* If no files were specified, assume a default */ |
| 327 | if ( test_files == NULL || testfile_count == 0 ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 328 | { |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 329 | test_files = &default_filename; |
| 330 | testfile_count = 1; |
| 331 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 332 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 333 | /* Now begin to execute the tests in the testfiles */ |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 334 | for ( testfile_index = 0; |
| 335 | testfile_index < testfile_count; |
| 336 | testfile_index++ ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 337 | { |
Paul Bakker | 774180e | 2016-05-12 15:59:48 +0100 | [diff] [blame] | 338 | int unmet_dep_count = 0; |
| 339 | char *unmet_dependencies[20]; |
| 340 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 341 | test_filename = test_files[ testfile_index ]; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 342 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 343 | file = fopen( test_filename, "r" ); |
| 344 | if( file == NULL ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 345 | { |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 346 | mbedtls_fprintf( stderr, "Failed to open test file: %s\n", |
| 347 | test_filename ); |
| 348 | return( 1 ); |
| 349 | } |
| 350 | |
| 351 | while( !feof( file ) ) |
| 352 | { |
Paul Bakker | 774180e | 2016-05-12 15:59:48 +0100 | [diff] [blame] | 353 | if( unmet_dep_count > 0 ) |
| 354 | { |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame^] | 355 | mbedtls_fprintf( stderr, |
| 356 | "FATAL: Dep count larger than zero at start of loop\n"); |
Paul Bakker | 774180e | 2016-05-12 15:59:48 +0100 | [diff] [blame] | 357 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
| 358 | } |
| 359 | unmet_dep_count = 0; |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 360 | |
| 361 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 362 | break; |
| 363 | mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf ); |
| 364 | mbedtls_fprintf( stdout, " " ); |
| 365 | for( i = strlen( buf ) + 1; i < 67; i++ ) |
| 366 | mbedtls_fprintf( stdout, "." ); |
| 367 | mbedtls_fprintf( stdout, " " ); |
| 368 | fflush( stdout ); |
| 369 | |
| 370 | total_tests++; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 371 | |
| 372 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 373 | break; |
| 374 | cnt = parse_arguments( buf, strlen(buf), params ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 375 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 376 | if( strcmp( params[0], "depends_on" ) == 0 ) |
| 377 | { |
| 378 | for( i = 1; i < cnt; i++ ) |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 379 | { |
| 380 | if( dep_check( params[i] ) != DEPENDENCY_SUPPORTED ) |
| 381 | { |
Paul Bakker | 26b60bf | 2016-05-12 15:55:37 +0100 | [diff] [blame] | 382 | if( 0 == option_verbose ) |
| 383 | { |
| 384 | /* Only one count is needed if not verbose */ |
| 385 | unmet_dep_count++; |
| 386 | break; |
| 387 | } |
| 388 | |
Paul Bakker | a30a72f | 2016-05-12 15:52:48 +0100 | [diff] [blame] | 389 | unmet_dependencies[ unmet_dep_count ] = strdup(params[i]); |
| 390 | if( unmet_dependencies[ unmet_dep_count ] == NULL ) |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 391 | { |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame^] | 392 | mbedtls_fprintf( stderr, "FATAL: Out of memory\n"); |
Janos Follath | 55abc21 | 2016-04-18 18:18:48 +0100 | [diff] [blame] | 393 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 394 | } |
| 395 | unmet_dep_count++; |
| 396 | } |
| 397 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 398 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 399 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 400 | break; |
| 401 | cnt = parse_arguments( buf, strlen(buf), params ); |
| 402 | } |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 403 | |
| 404 | // If there are no unmet dependencies execute the test |
| 405 | if( unmet_dep_count == 0 ) |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 406 | { |
| 407 | test_errors = 0; |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame^] | 408 | |
| 409 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 410 | /* Suppress all output from the library unless we're verbose |
| 411 | * mode |
| 412 | */ |
| 413 | if( !option_verbose ) |
| 414 | { |
| 415 | /* Redirect all stdout output to /dev/null */ |
| 416 | tests_stdout = dup( fileno(stdout) ); |
| 417 | if( tests_stdout == -1 ) |
| 418 | { |
| 419 | /* Redirection has failed with no stdout so exit */ |
| 420 | exit(1); |
| 421 | } |
| 422 | |
| 423 | fflush( stdout ); |
| 424 | fclose( stdout ); |
| 425 | stdout = fopen("/dev/null", "w" ); |
| 426 | if( stdout == NULL ) |
| 427 | { |
| 428 | /* Redirection has failed with no stdout so exit */ |
| 429 | exit(1); |
| 430 | } |
| 431 | } |
| 432 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
| 433 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 434 | ret = dispatch_test( cnt, params ); |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame^] | 435 | |
| 436 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 437 | if( !option_verbose ) |
| 438 | { |
| 439 | /* Restore stdout */ |
| 440 | fflush( stdout ); |
| 441 | fclose( stdout ); |
| 442 | |
| 443 | stdout = fdopen ( tests_stdout, "w"); |
| 444 | if( stdout == NULL ) |
| 445 | { |
| 446 | /* Redirection has failed with no stdout so exit */ |
| 447 | exit(1); |
| 448 | } |
| 449 | } |
| 450 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
| 451 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 452 | } |
| 453 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 454 | if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE ) |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 455 | { |
| 456 | total_skipped++; |
| 457 | mbedtls_fprintf( stdout, "----\n" ); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 458 | |
| 459 | if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE ) |
| 460 | { |
| 461 | mbedtls_fprintf( stdout, " Test Suite not enabled" ); |
| 462 | } |
| 463 | |
| 464 | if( 1 == option_verbose && unmet_dep_count > 0 ) |
| 465 | { |
| 466 | mbedtls_fprintf( stdout, " Unmet dependencies: " ); |
Paul Bakker | 774180e | 2016-05-12 15:59:48 +0100 | [diff] [blame] | 467 | for( i = 0; i < unmet_dep_count; i++ ) |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 468 | { |
| 469 | mbedtls_fprintf(stdout, "%s ", |
Paul Bakker | 774180e | 2016-05-12 15:59:48 +0100 | [diff] [blame] | 470 | unmet_dependencies[i]); |
| 471 | free(unmet_dependencies[i]); |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 472 | } |
| 473 | mbedtls_fprintf( stdout, "\n" ); |
| 474 | } |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 475 | fflush( stdout ); |
Paul Bakker | 774180e | 2016-05-12 15:59:48 +0100 | [diff] [blame] | 476 | |
| 477 | unmet_dep_count = 0; |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 478 | } |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 479 | else if( ret == DISPATCH_TEST_SUCCESS && test_errors == 0 ) |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 480 | { |
| 481 | mbedtls_fprintf( stdout, "PASS\n" ); |
| 482 | fflush( stdout ); |
| 483 | } |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 484 | else if( ret == DISPATCH_INVALID_TEST_DATA ) |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 485 | { |
| 486 | mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" ); |
| 487 | fclose(file); |
| 488 | mbedtls_exit( 2 ); |
| 489 | } |
| 490 | else |
| 491 | total_errors++; |
| 492 | |
| 493 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 494 | break; |
| 495 | if( strlen(buf) != 0 ) |
| 496 | { |
| 497 | mbedtls_fprintf( stderr, "Should be empty %d\n", |
| 498 | (int) strlen(buf) ); |
| 499 | return( 1 ); |
| 500 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 501 | } |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 502 | fclose(file); |
Paul Bakker | 774180e | 2016-05-12 15:59:48 +0100 | [diff] [blame] | 503 | |
| 504 | /* In case we encounter early end of file */ |
| 505 | for( i = 0; i < unmet_dep_count; i++ ) |
| 506 | free( unmet_dependencies[i] ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 507 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n"); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 510 | if( total_errors == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 511 | mbedtls_fprintf( stdout, "PASSED" ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 512 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 513 | mbedtls_fprintf( stdout, "FAILED" ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 515 | mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n", |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 516 | total_tests - total_errors, total_tests, total_skipped ); |
| 517 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \ |
Manuel Pégourié-Gonnard | 765bb31 | 2014-11-27 11:55:27 +0100 | [diff] [blame] | 519 | !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 520 | #if defined(MBEDTLS_MEMORY_DEBUG) |
| 521 | mbedtls_memory_buffer_alloc_status(); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 522 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 523 | mbedtls_memory_buffer_alloc_free(); |
Paul Bakker | 1337aff | 2013-09-29 14:45:34 +0200 | [diff] [blame] | 524 | #endif |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 525 | |
| 526 | return( total_errors != 0 ); |
| 527 | } |
| 528 | |