blob: ac5322e45367460a2e58cc9f346845b41b34f83b [file] [log] [blame]
SimonB15942102016-04-25 21:34:49 +01001#line 1 "main_test.function"
Paul Bakkerde56ca12013-09-15 17:05:21 +02002SUITE_PRE_DEP
3#define TEST_SUITE_ACTIVE
4
Paul Bakker19343182013-08-16 13:31:10 +02005int verify_string( char **str )
6{
7 if( (*str)[0] != '"' ||
8 (*str)[strlen( *str ) - 1] != '"' )
9 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010 mbedtls_printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
Paul Bakker19343182013-08-16 13:31:10 +020011 return( -1 );
12 }
13
14 (*str)++;
15 (*str)[strlen( *str ) - 1] = '\0';
16
17 return( 0 );
18}
19
20int 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é-Gonnard725afd82014-02-01 11:54:28 +010042 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
43 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
44 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
Paul Bakker19343182013-08-16 13:31:10 +020045 {
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
61MAPPING_CODE
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_printf( "Expected integer for parameter and got: %s\n", str );
SimonB8ca7bc42016-04-17 23:24:50 +010064 return( KEY_VALUE_MAPPING_NOT_FOUND );
Paul Bakker19343182013-08-16 13:31:10 +020065}
66
SimonB152ea182016-02-15 23:27:28 +000067
68/*----------------------------------------------------------------------------*/
69/* Test Case code */
70
Paul Bakkerde56ca12013-09-15 17:05:21 +020071FUNCTION_CODE
72SUITE_POST_DEP
73
SimonB15942102016-04-25 21:34:49 +010074#line !LINE_NO! "main_test.function"
75
SimonB152ea182016-02-15 23:27:28 +000076
77/*----------------------------------------------------------------------------*/
78/* Test dispatch code */
79
Paul Bakker19343182013-08-16 13:31:10 +020080int dep_check( char *str )
81{
82 if( str == NULL )
83 return( 1 );
84
85DEP_CHECK_CODE
Simon Butcher65b1fa62016-05-23 23:18:26 +010086#line !LINE_NO! "main_test.function"
Paul Bakker19343182013-08-16 13:31:10 +020087
SimonB8ca7bc42016-04-17 23:24:50 +010088 return( DEPENDENCY_NOT_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +020089}
90
Paul Bakker19343182013-08-16 13:31:10 +020091int dispatch_test(int cnt, char *params[50])
92{
93 int ret;
Paul Bakkerb34fef22013-08-20 12:06:33 +020094 ((void) cnt);
95 ((void) params);
Paul Bakker19343182013-08-16 13:31:10 +020096
Paul Bakkerb34fef22013-08-20 12:06:33 +020097#if defined(TEST_SUITE_ACTIVE)
SimonB8ca7bc42016-04-17 23:24:50 +010098 ret = DISPATCH_TEST_SUCCESS;
99
Simon Butcher65b1fa62016-05-23 23:18:26 +0100100 // Cast to void to avoid compiler warnings
101 (void)ret;
102
Paul Bakker19343182013-08-16 13:31:10 +0200103DISPATCH_FUNCTION
104 {
Simon Butcher65b1fa62016-05-23 23:18:26 +0100105#line !LINE_NO! "main_test.function"
SimonB8ca7bc42016-04-17 23:24:50 +0100106 mbedtls_fprintf( stdout,
107 "FAILED\nSkipping unknown test function '%s'\n",
108 params[0] );
Paul Bakker19343182013-08-16 13:31:10 +0200109 fflush( stdout );
SimonB8ca7bc42016-04-17 23:24:50 +0100110 ret = DISPATCH_TEST_FN_NOT_FOUND;
Paul Bakker19343182013-08-16 13:31:10 +0200111 }
Paul Bakkerb34fef22013-08-20 12:06:33 +0200112#else
SimonB8ca7bc42016-04-17 23:24:50 +0100113 ret = DISPATCH_UNSUPPORTED_SUITE;
Paul Bakkerb34fef22013-08-20 12:06:33 +0200114#endif
Paul Bakker19343182013-08-16 13:31:10 +0200115 return( ret );
116}
117
SimonB152ea182016-02-15 23:27:28 +0000118
119/*----------------------------------------------------------------------------*/
120/* Main Test code */
121
SimonB15942102016-04-25 21:34:49 +0100122#line !LINE_NO! "main_test.function"
123
SimonB8ca7bc42016-04-17 23:24:50 +0100124#define USAGE \
125 "Usage: %s [OPTIONS] files...\n\n" \
126 " Command line arguments:\n" \
127 " files... One or more test data file. If no file is specified\n" \
128 " the followimg default test case is used:\n" \
129 " %s\n\n" \
130 " Options:\n" \
131 " -v | --verbose Display full information about each test\n" \
132 " -h | --help Display this information\n\n", \
133 argv[0], \
SimonB15942102016-04-25 21:34:49 +0100134 "TESTCASE_FILENAME"
SimonB8ca7bc42016-04-17 23:24:50 +0100135
136
Paul Bakker19343182013-08-16 13:31:10 +0200137int get_line( FILE *f, char *buf, size_t len )
138{
139 char *ret;
140
141 ret = fgets( buf, len, f );
142 if( ret == NULL )
143 return( -1 );
144
145 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
146 buf[strlen(buf) - 1] = '\0';
147 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
148 buf[strlen(buf) - 1] = '\0';
149
150 return( 0 );
151}
152
153int parse_arguments( char *buf, size_t len, char *params[50] )
154{
155 int cnt = 0, i;
156 char *cur = buf;
157 char *p = buf, *q;
158
159 params[cnt++] = cur;
160
161 while( *p != '\0' && p < buf + len )
162 {
163 if( *p == '\\' )
164 {
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100165 p++;
166 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200167 continue;
168 }
169 if( *p == ':' )
170 {
171 if( p + 1 < buf + len )
172 {
173 cur = p + 1;
174 params[cnt++] = cur;
175 }
176 *p = '\0';
177 }
178
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100179 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200180 }
181
SimonB0269dad2016-02-17 23:34:30 +0000182 /* Replace newlines, question marks and colons in strings */
Paul Bakker19343182013-08-16 13:31:10 +0200183 for( i = 0; i < cnt; i++ )
184 {
185 p = params[i];
186 q = params[i];
187
188 while( *p != '\0' )
189 {
190 if( *p == '\\' && *(p + 1) == 'n' )
191 {
192 p += 2;
193 *(q++) = '\n';
194 }
195 else if( *p == '\\' && *(p + 1) == ':' )
196 {
197 p += 2;
198 *(q++) = ':';
199 }
200 else if( *p == '\\' && *(p + 1) == '?' )
201 {
202 p += 2;
203 *(q++) = '?';
204 }
205 else
206 *(q++) = *(p++);
207 }
208 *q = '\0';
209 }
210
211 return( cnt );
212}
213
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200214static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
215{
216 int ret;
217 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200218 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200219
220 ret = mbedtls_snprintf( buf, n, "%s", "123" );
221 if( ret < 0 || (size_t) ret >= n )
222 ret = -1;
223
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200224 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
225 ref_ret != ret ||
226 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200227 {
228 return( 1 );
229 }
230
231 return( 0 );
232}
233
234static int run_test_snprintf( void )
235{
236 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200237 test_snprintf( 1, "", -1 ) != 0 ||
238 test_snprintf( 2, "1", -1 ) != 0 ||
239 test_snprintf( 3, "12", -1 ) != 0 ||
240 test_snprintf( 4, "123", 3 ) != 0 ||
241 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200242}
243
Simon Butcheraad787f2016-01-26 22:13:58 +0000244int main(int argc, const char *argv[])
Paul Bakker19343182013-08-16 13:31:10 +0200245{
SimonB8ca7bc42016-04-17 23:24:50 +0100246 /* Local Configurations and options */
SimonB15942102016-04-25 21:34:49 +0100247 const char *default_filename = "TESTCASE_FILENAME";
Simon Butcheraad787f2016-01-26 22:13:58 +0000248 const char *test_filename = NULL;
249 const char **test_files = NULL;
SimonB8ca7bc42016-04-17 23:24:50 +0100250 int testfile_count = 0;
251 int option_verbose = 0;
252
253 /* Other Local variables */
254 int arg_index = 1;
255 const char *next_arg;
256 int testfile_index, ret, i, cnt;
257 int total_errors = 0, total_tests = 0, total_skipped = 0;
Paul Bakker19343182013-08-16 13:31:10 +0200258 FILE *file;
259 char buf[5000];
260 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200261 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100264 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200265 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200267#endif
268
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200269 /*
270 * The C standard doesn't guarantee that all-bits-0 is the representation
271 * of a NULL pointer. We do however use that in our code for initializing
272 * structures, which should work on every modern platform. Let's be sure.
273 */
274 memset( &pointer, 0, sizeof( void * ) );
275 if( pointer != NULL )
276 {
277 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
278 return( 1 );
279 }
280
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200281 /*
282 * Make sure we have a snprintf that correctly zero-terminates
283 */
284 if( run_test_snprintf() != 0 )
285 {
286 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
287 return( 0 );
288 }
289
SimonB8ca7bc42016-04-17 23:24:50 +0100290 while( arg_index < argc)
291 {
292 next_arg = argv[ arg_index ];
293
294 if( strcmp(next_arg, "--verbose" ) == 0 ||
295 strcmp(next_arg, "-v" ) == 0 )
296 {
297 option_verbose = 1;
298 }
299 else if( strcmp(next_arg, "--help" ) == 0 ||
300 strcmp(next_arg, "-h" ) == 0 )
301 {
302 mbedtls_fprintf( stdout, USAGE );
303 mbedtls_exit( EXIT_SUCCESS );
304 }
305 else
306 {
307 /* Not an option, therefore treat all further arguments as the file
308 * list.
309 */
310 test_files = &argv[ arg_index ];
311 testfile_count = argc - arg_index;
312 }
313
314 arg_index++;
315 }
316
317 /* If no files were specified, assume a default */
318 if ( test_files == NULL || testfile_count == 0 )
Paul Bakker19343182013-08-16 13:31:10 +0200319 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000320 test_files = &default_filename;
321 testfile_count = 1;
322 }
Paul Bakker19343182013-08-16 13:31:10 +0200323
SimonB8ca7bc42016-04-17 23:24:50 +0100324 /* Now begin to execute the tests in the testfiles */
Simon Butcheraad787f2016-01-26 22:13:58 +0000325 for ( testfile_index = 0;
326 testfile_index < testfile_count;
327 testfile_index++ )
Paul Bakker19343182013-08-16 13:31:10 +0200328 {
Paul Bakker774180e2016-05-12 15:59:48 +0100329 int unmet_dep_count = 0;
330 char *unmet_dependencies[20];
331
Simon Butcheraad787f2016-01-26 22:13:58 +0000332 test_filename = test_files[ testfile_index ];
Paul Bakker19343182013-08-16 13:31:10 +0200333
Simon Butcheraad787f2016-01-26 22:13:58 +0000334 file = fopen( test_filename, "r" );
335 if( file == NULL )
Paul Bakker19343182013-08-16 13:31:10 +0200336 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000337 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
338 test_filename );
339 return( 1 );
340 }
341
342 while( !feof( file ) )
343 {
Paul Bakker774180e2016-05-12 15:59:48 +0100344 if( unmet_dep_count > 0 )
345 {
346 mbedtls_printf("FATAL: Dep count larger than zero at start of loop\n");
347 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
348 }
349 unmet_dep_count = 0;
Simon Butcheraad787f2016-01-26 22:13:58 +0000350
351 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
352 break;
353 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
354 mbedtls_fprintf( stdout, " " );
355 for( i = strlen( buf ) + 1; i < 67; i++ )
356 mbedtls_fprintf( stdout, "." );
357 mbedtls_fprintf( stdout, " " );
358 fflush( stdout );
359
360 total_tests++;
Paul Bakker19343182013-08-16 13:31:10 +0200361
362 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
363 break;
364 cnt = parse_arguments( buf, strlen(buf), params );
Paul Bakker19343182013-08-16 13:31:10 +0200365
Simon Butcheraad787f2016-01-26 22:13:58 +0000366 if( strcmp( params[0], "depends_on" ) == 0 )
367 {
368 for( i = 1; i < cnt; i++ )
SimonB8ca7bc42016-04-17 23:24:50 +0100369 {
370 if( dep_check( params[i] ) != DEPENDENCY_SUPPORTED )
371 {
Paul Bakker26b60bf2016-05-12 15:55:37 +0100372 if( 0 == option_verbose )
373 {
374 /* Only one count is needed if not verbose */
375 unmet_dep_count++;
376 break;
377 }
378
Paul Bakkera30a72f2016-05-12 15:52:48 +0100379 unmet_dependencies[ unmet_dep_count ] = strdup(params[i]);
380 if( unmet_dependencies[ unmet_dep_count ] == NULL )
SimonB8ca7bc42016-04-17 23:24:50 +0100381 {
382 mbedtls_printf("FATAL: Out of memory\n");
Janos Follath55abc212016-04-18 18:18:48 +0100383 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
SimonB8ca7bc42016-04-17 23:24:50 +0100384 }
385 unmet_dep_count++;
386 }
387 }
Paul Bakker19343182013-08-16 13:31:10 +0200388
Simon Butcheraad787f2016-01-26 22:13:58 +0000389 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
390 break;
391 cnt = parse_arguments( buf, strlen(buf), params );
392 }
SimonB8ca7bc42016-04-17 23:24:50 +0100393
394 // If there are no unmet dependencies execute the test
395 if( unmet_dep_count == 0 )
Simon Butcheraad787f2016-01-26 22:13:58 +0000396 {
397 test_errors = 0;
398 ret = dispatch_test( cnt, params );
399 }
400
SimonB8ca7bc42016-04-17 23:24:50 +0100401 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
Simon Butcheraad787f2016-01-26 22:13:58 +0000402 {
403 total_skipped++;
404 mbedtls_fprintf( stdout, "----\n" );
SimonB8ca7bc42016-04-17 23:24:50 +0100405
406 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
407 {
408 mbedtls_fprintf( stdout, " Test Suite not enabled" );
409 }
410
411 if( 1 == option_verbose && unmet_dep_count > 0 )
412 {
413 mbedtls_fprintf( stdout, " Unmet dependencies: " );
Paul Bakker774180e2016-05-12 15:59:48 +0100414 for( i = 0; i < unmet_dep_count; i++ )
SimonB8ca7bc42016-04-17 23:24:50 +0100415 {
416 mbedtls_fprintf(stdout, "%s ",
Paul Bakker774180e2016-05-12 15:59:48 +0100417 unmet_dependencies[i]);
418 free(unmet_dependencies[i]);
SimonB8ca7bc42016-04-17 23:24:50 +0100419 }
420 mbedtls_fprintf( stdout, "\n" );
421 }
Simon Butcheraad787f2016-01-26 22:13:58 +0000422 fflush( stdout );
Paul Bakker774180e2016-05-12 15:59:48 +0100423
424 unmet_dep_count = 0;
Simon Butcheraad787f2016-01-26 22:13:58 +0000425 }
SimonB8ca7bc42016-04-17 23:24:50 +0100426 else if( ret == DISPATCH_TEST_SUCCESS && test_errors == 0 )
Simon Butcheraad787f2016-01-26 22:13:58 +0000427 {
428 mbedtls_fprintf( stdout, "PASS\n" );
429 fflush( stdout );
430 }
SimonB8ca7bc42016-04-17 23:24:50 +0100431 else if( ret == DISPATCH_INVALID_TEST_DATA )
Simon Butcheraad787f2016-01-26 22:13:58 +0000432 {
433 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
434 fclose(file);
435 mbedtls_exit( 2 );
436 }
437 else
438 total_errors++;
439
440 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
441 break;
442 if( strlen(buf) != 0 )
443 {
444 mbedtls_fprintf( stderr, "Should be empty %d\n",
445 (int) strlen(buf) );
446 return( 1 );
447 }
Paul Bakker19343182013-08-16 13:31:10 +0200448 }
Simon Butcheraad787f2016-01-26 22:13:58 +0000449 fclose(file);
Paul Bakker774180e2016-05-12 15:59:48 +0100450
451 /* In case we encounter early end of file */
452 for( i = 0; i < unmet_dep_count; i++ )
453 free( unmet_dependencies[i] );
Paul Bakker19343182013-08-16 13:31:10 +0200454 }
Paul Bakker19343182013-08-16 13:31:10 +0200455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200457 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200459 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200463 total_tests - total_errors, total_tests, total_skipped );
464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100466 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#if defined(MBEDTLS_MEMORY_DEBUG)
468 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200469#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200471#endif
Paul Bakker19343182013-08-16 13:31:10 +0200472
473 return( total_errors != 0 );
474}
475