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