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