Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 1 | SUITE_PRE_DEP |
| 2 | #define TEST_SUITE_ACTIVE |
| 3 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 4 | int verify_string( char **str ) |
| 5 | { |
| 6 | if( (*str)[0] != '"' || |
| 7 | (*str)[strlen( *str ) - 1] != '"' ) |
| 8 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9 | mbedtls_printf( "Expected string (with \"\") for parameter and got: %s\n", *str ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 10 | return( -1 ); |
| 11 | } |
| 12 | |
| 13 | (*str)++; |
| 14 | (*str)[strlen( *str ) - 1] = '\0'; |
| 15 | |
| 16 | return( 0 ); |
| 17 | } |
| 18 | |
| 19 | int verify_int( char *str, int *value ) |
| 20 | { |
| 21 | size_t i; |
| 22 | int minus = 0; |
| 23 | int digits = 1; |
| 24 | int hex = 0; |
| 25 | |
| 26 | for( i = 0; i < strlen( str ); i++ ) |
| 27 | { |
| 28 | if( i == 0 && str[i] == '-' ) |
| 29 | { |
| 30 | minus = 1; |
| 31 | continue; |
| 32 | } |
| 33 | |
| 34 | if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) && |
| 35 | str[i - 1] == '0' && str[i] == 'x' ) |
| 36 | { |
| 37 | hex = 1; |
| 38 | continue; |
| 39 | } |
| 40 | |
Manuel Pégourié-Gonnard | 725afd8 | 2014-02-01 11:54:28 +0100 | [diff] [blame] | 41 | if( ! ( ( str[i] >= '0' && str[i] <= '9' ) || |
| 42 | ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) || |
| 43 | ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 44 | { |
| 45 | digits = 0; |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if( digits ) |
| 51 | { |
| 52 | if( hex ) |
| 53 | *value = strtol( str, NULL, 16 ); |
| 54 | else |
| 55 | *value = strtol( str, NULL, 10 ); |
| 56 | |
| 57 | return( 0 ); |
| 58 | } |
| 59 | |
| 60 | MAPPING_CODE |
| 61 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | mbedtls_printf( "Expected integer for parameter and got: %s\n", str ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 63 | return( -1 ); |
| 64 | } |
| 65 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 66 | |
| 67 | /*----------------------------------------------------------------------------*/ |
| 68 | /* Test Case code */ |
| 69 | |
Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 70 | FUNCTION_CODE |
| 71 | SUITE_POST_DEP |
| 72 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 73 | |
| 74 | /*----------------------------------------------------------------------------*/ |
| 75 | /* Test dispatch code */ |
| 76 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 77 | int dep_check( char *str ) |
| 78 | { |
| 79 | if( str == NULL ) |
| 80 | return( 1 ); |
| 81 | |
| 82 | DEP_CHECK_CODE |
| 83 | |
| 84 | return( 1 ); |
| 85 | } |
| 86 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 87 | int dispatch_test(int cnt, char *params[50]) |
| 88 | { |
| 89 | int ret; |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 90 | ((void) cnt); |
| 91 | ((void) params); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 92 | |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 93 | #if defined(TEST_SUITE_ACTIVE) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 94 | DISPATCH_FUNCTION |
| 95 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | mbedtls_fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 97 | fflush( stdout ); |
| 98 | return( 1 ); |
| 99 | } |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 100 | #else |
| 101 | return( 3 ); |
| 102 | #endif |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 103 | return( ret ); |
| 104 | } |
| 105 | |
SimonB | 152ea18 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 106 | |
| 107 | /*----------------------------------------------------------------------------*/ |
| 108 | /* Main Test code */ |
| 109 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 110 | int get_line( FILE *f, char *buf, size_t len ) |
| 111 | { |
| 112 | char *ret; |
| 113 | |
| 114 | ret = fgets( buf, len, f ); |
| 115 | if( ret == NULL ) |
| 116 | return( -1 ); |
| 117 | |
| 118 | if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' ) |
| 119 | buf[strlen(buf) - 1] = '\0'; |
| 120 | if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' ) |
| 121 | buf[strlen(buf) - 1] = '\0'; |
| 122 | |
| 123 | return( 0 ); |
| 124 | } |
| 125 | |
| 126 | int parse_arguments( char *buf, size_t len, char *params[50] ) |
| 127 | { |
| 128 | int cnt = 0, i; |
| 129 | char *cur = buf; |
| 130 | char *p = buf, *q; |
| 131 | |
| 132 | params[cnt++] = cur; |
| 133 | |
| 134 | while( *p != '\0' && p < buf + len ) |
| 135 | { |
| 136 | if( *p == '\\' ) |
| 137 | { |
Manuel Pégourié-Gonnard | 2d5f142 | 2014-01-22 16:01:17 +0100 | [diff] [blame] | 138 | p++; |
| 139 | p++; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 140 | continue; |
| 141 | } |
| 142 | if( *p == ':' ) |
| 143 | { |
| 144 | if( p + 1 < buf + len ) |
| 145 | { |
| 146 | cur = p + 1; |
| 147 | params[cnt++] = cur; |
| 148 | } |
| 149 | *p = '\0'; |
| 150 | } |
| 151 | |
Manuel Pégourié-Gonnard | 2d5f142 | 2014-01-22 16:01:17 +0100 | [diff] [blame] | 152 | p++; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 153 | } |
| 154 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 155 | /* Replace newlines, question marks and colons in strings */ |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 156 | for( i = 0; i < cnt; i++ ) |
| 157 | { |
| 158 | p = params[i]; |
| 159 | q = params[i]; |
| 160 | |
| 161 | while( *p != '\0' ) |
| 162 | { |
| 163 | if( *p == '\\' && *(p + 1) == 'n' ) |
| 164 | { |
| 165 | p += 2; |
| 166 | *(q++) = '\n'; |
| 167 | } |
| 168 | else if( *p == '\\' && *(p + 1) == ':' ) |
| 169 | { |
| 170 | p += 2; |
| 171 | *(q++) = ':'; |
| 172 | } |
| 173 | else if( *p == '\\' && *(p + 1) == '?' ) |
| 174 | { |
| 175 | p += 2; |
| 176 | *(q++) = '?'; |
| 177 | } |
| 178 | else |
| 179 | *(q++) = *(p++); |
| 180 | } |
| 181 | *q = '\0'; |
| 182 | } |
| 183 | |
| 184 | return( cnt ); |
| 185 | } |
| 186 | |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 187 | static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret ) |
| 188 | { |
| 189 | int ret; |
| 190 | char buf[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 191 | const char ref[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 192 | |
| 193 | ret = mbedtls_snprintf( buf, n, "%s", "123" ); |
| 194 | if( ret < 0 || (size_t) ret >= n ) |
| 195 | ret = -1; |
| 196 | |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 197 | if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 || |
| 198 | ref_ret != ret || |
| 199 | memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 ) |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 200 | { |
| 201 | return( 1 ); |
| 202 | } |
| 203 | |
| 204 | return( 0 ); |
| 205 | } |
| 206 | |
| 207 | static int run_test_snprintf( void ) |
| 208 | { |
| 209 | return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 || |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 210 | test_snprintf( 1, "", -1 ) != 0 || |
| 211 | test_snprintf( 2, "1", -1 ) != 0 || |
| 212 | test_snprintf( 3, "12", -1 ) != 0 || |
| 213 | test_snprintf( 4, "123", 3 ) != 0 || |
| 214 | test_snprintf( 5, "123", 3 ) != 0 ); |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 217 | int main(int argc, const char *argv[]) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 218 | { |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 219 | int testfile_index, testfile_count, ret, i, cnt; |
| 220 | int total_errors = 0, total_tests = 0, total_skipped = 0; |
| 221 | const char *default_filename = "TEST_FILENAME"; |
| 222 | const char *test_filename = NULL; |
| 223 | const char **test_files = NULL; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 224 | FILE *file; |
| 225 | char buf[5000]; |
| 226 | char *params[50]; |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 227 | void *pointer; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \ |
Manuel Pégourié-Gonnard | 765bb31 | 2014-11-27 11:55:27 +0100 | [diff] [blame] | 230 | !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC) |
Paul Bakker | 1337aff | 2013-09-29 14:45:34 +0200 | [diff] [blame] | 231 | unsigned char alloc_buf[1000000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 233 | #endif |
| 234 | |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 235 | /* |
| 236 | * The C standard doesn't guarantee that all-bits-0 is the representation |
| 237 | * of a NULL pointer. We do however use that in our code for initializing |
| 238 | * structures, which should work on every modern platform. Let's be sure. |
| 239 | */ |
| 240 | memset( &pointer, 0, sizeof( void * ) ); |
| 241 | if( pointer != NULL ) |
| 242 | { |
| 243 | mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" ); |
| 244 | return( 1 ); |
| 245 | } |
| 246 | |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 247 | /* |
| 248 | * Make sure we have a snprintf that correctly zero-terminates |
| 249 | */ |
| 250 | if( run_test_snprintf() != 0 ) |
| 251 | { |
| 252 | mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" ); |
| 253 | return( 0 ); |
| 254 | } |
| 255 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 256 | if ( argc <= 1 ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 257 | { |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 258 | test_files = &default_filename; |
| 259 | testfile_count = 1; |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | test_files = &argv[1]; |
| 264 | testfile_count = argc - 1; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 267 | for ( testfile_index = 0; |
| 268 | testfile_index < testfile_count; |
| 269 | testfile_index++ ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 270 | { |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 271 | test_filename = test_files[ testfile_index ]; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 272 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 273 | file = fopen( test_filename, "r" ); |
| 274 | if( file == NULL ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 275 | { |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 276 | mbedtls_fprintf( stderr, "Failed to open test file: %s\n", |
| 277 | test_filename ); |
| 278 | return( 1 ); |
| 279 | } |
| 280 | |
| 281 | while( !feof( file ) ) |
| 282 | { |
| 283 | int skip = 0; |
| 284 | |
| 285 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 286 | break; |
| 287 | mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf ); |
| 288 | mbedtls_fprintf( stdout, " " ); |
| 289 | for( i = strlen( buf ) + 1; i < 67; i++ ) |
| 290 | mbedtls_fprintf( stdout, "." ); |
| 291 | mbedtls_fprintf( stdout, " " ); |
| 292 | fflush( stdout ); |
| 293 | |
| 294 | total_tests++; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 295 | |
| 296 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 297 | break; |
| 298 | cnt = parse_arguments( buf, strlen(buf), params ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 299 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 300 | if( strcmp( params[0], "depends_on" ) == 0 ) |
| 301 | { |
| 302 | for( i = 1; i < cnt; i++ ) |
| 303 | if( dep_check( params[i] ) != 0 ) |
| 304 | skip = 1; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 305 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 306 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 307 | break; |
| 308 | cnt = parse_arguments( buf, strlen(buf), params ); |
| 309 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 310 | |
Simon Butcher | aad787f | 2016-01-26 22:13:58 +0000 | [diff] [blame] | 311 | if( skip == 0 ) |
| 312 | { |
| 313 | test_errors = 0; |
| 314 | ret = dispatch_test( cnt, params ); |
| 315 | } |
| 316 | |
| 317 | if( skip == 1 || ret == 3 ) |
| 318 | { |
| 319 | total_skipped++; |
| 320 | mbedtls_fprintf( stdout, "----\n" ); |
| 321 | fflush( stdout ); |
| 322 | } |
| 323 | else if( ret == 0 && test_errors == 0 ) |
| 324 | { |
| 325 | mbedtls_fprintf( stdout, "PASS\n" ); |
| 326 | fflush( stdout ); |
| 327 | } |
| 328 | else if( ret == 2 ) |
| 329 | { |
| 330 | mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" ); |
| 331 | fclose(file); |
| 332 | mbedtls_exit( 2 ); |
| 333 | } |
| 334 | else |
| 335 | total_errors++; |
| 336 | |
| 337 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 338 | break; |
| 339 | if( strlen(buf) != 0 ) |
| 340 | { |
| 341 | mbedtls_fprintf( stderr, "Should be empty %d\n", |
| 342 | (int) strlen(buf) ); |
| 343 | return( 1 ); |
| 344 | } |
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 | fclose(file); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 347 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 349 | mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n"); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 350 | if( total_errors == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 351 | mbedtls_fprintf( stdout, "PASSED" ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 352 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | mbedtls_fprintf( stdout, "FAILED" ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n", |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 356 | total_tests - total_errors, total_tests, total_skipped ); |
| 357 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \ |
Manuel Pégourié-Gonnard | 765bb31 | 2014-11-27 11:55:27 +0100 | [diff] [blame] | 359 | !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | #if defined(MBEDTLS_MEMORY_DEBUG) |
| 361 | mbedtls_memory_buffer_alloc_status(); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 362 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 363 | mbedtls_memory_buffer_alloc_free(); |
Paul Bakker | 1337aff | 2013-09-29 14:45:34 +0200 | [diff] [blame] | 364 | #endif |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 365 | |
| 366 | return( total_errors != 0 ); |
| 367 | } |
| 368 | |