blob: f18248578223d77a18f13516525822ef74e40e8a [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
86
SimonB8ca7bc42016-04-17 23:24:50 +010087 return( DEPENDENCY_NOT_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +020088}
89
Paul Bakker19343182013-08-16 13:31:10 +020090int dispatch_test(int cnt, char *params[50])
91{
92 int ret;
Paul Bakkerb34fef22013-08-20 12:06:33 +020093 ((void) cnt);
94 ((void) params);
Paul Bakker19343182013-08-16 13:31:10 +020095
Paul Bakkerb34fef22013-08-20 12:06:33 +020096#if defined(TEST_SUITE_ACTIVE)
SimonB8ca7bc42016-04-17 23:24:50 +010097 ret = DISPATCH_TEST_SUCCESS;
98
Paul Bakker19343182013-08-16 13:31:10 +020099DISPATCH_FUNCTION
100 {
SimonB8ca7bc42016-04-17 23:24:50 +0100101 mbedtls_fprintf( stdout,
102 "FAILED\nSkipping unknown test function '%s'\n",
103 params[0] );
Paul Bakker19343182013-08-16 13:31:10 +0200104 fflush( stdout );
SimonB8ca7bc42016-04-17 23:24:50 +0100105 ret = DISPATCH_TEST_FN_NOT_FOUND;
Paul Bakker19343182013-08-16 13:31:10 +0200106 }
Paul Bakkerb34fef22013-08-20 12:06:33 +0200107#else
SimonB8ca7bc42016-04-17 23:24:50 +0100108 ret = DISPATCH_UNSUPPORTED_SUITE;
Paul Bakkerb34fef22013-08-20 12:06:33 +0200109#endif
Paul Bakker19343182013-08-16 13:31:10 +0200110 return( ret );
111}
112
SimonB152ea182016-02-15 23:27:28 +0000113
114/*----------------------------------------------------------------------------*/
115/* Main Test code */
116
SimonB15942102016-04-25 21:34:49 +0100117#line !LINE_NO! "main_test.function"
118
SimonB8ca7bc42016-04-17 23:24:50 +0100119#define USAGE \
120 "Usage: %s [OPTIONS] files...\n\n" \
121 " Command line arguments:\n" \
122 " files... One or more test data file. If no file is specified\n" \
123 " the followimg default test case is used:\n" \
124 " %s\n\n" \
125 " Options:\n" \
126 " -v | --verbose Display full information about each test\n" \
127 " -h | --help Display this information\n\n", \
128 argv[0], \
SimonB15942102016-04-25 21:34:49 +0100129 "TESTCASE_FILENAME"
SimonB8ca7bc42016-04-17 23:24:50 +0100130
131
Paul Bakker19343182013-08-16 13:31:10 +0200132int get_line( FILE *f, char *buf, size_t len )
133{
134 char *ret;
135
136 ret = fgets( buf, len, f );
137 if( ret == NULL )
138 return( -1 );
139
140 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
141 buf[strlen(buf) - 1] = '\0';
142 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
143 buf[strlen(buf) - 1] = '\0';
144
145 return( 0 );
146}
147
148int parse_arguments( char *buf, size_t len, char *params[50] )
149{
150 int cnt = 0, i;
151 char *cur = buf;
152 char *p = buf, *q;
153
154 params[cnt++] = cur;
155
156 while( *p != '\0' && p < buf + len )
157 {
158 if( *p == '\\' )
159 {
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100160 p++;
161 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200162 continue;
163 }
164 if( *p == ':' )
165 {
166 if( p + 1 < buf + len )
167 {
168 cur = p + 1;
169 params[cnt++] = cur;
170 }
171 *p = '\0';
172 }
173
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100174 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200175 }
176
SimonB0269dad2016-02-17 23:34:30 +0000177 /* Replace newlines, question marks and colons in strings */
Paul Bakker19343182013-08-16 13:31:10 +0200178 for( i = 0; i < cnt; i++ )
179 {
180 p = params[i];
181 q = params[i];
182
183 while( *p != '\0' )
184 {
185 if( *p == '\\' && *(p + 1) == 'n' )
186 {
187 p += 2;
188 *(q++) = '\n';
189 }
190 else if( *p == '\\' && *(p + 1) == ':' )
191 {
192 p += 2;
193 *(q++) = ':';
194 }
195 else if( *p == '\\' && *(p + 1) == '?' )
196 {
197 p += 2;
198 *(q++) = '?';
199 }
200 else
201 *(q++) = *(p++);
202 }
203 *q = '\0';
204 }
205
206 return( cnt );
207}
208
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200209static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
210{
211 int ret;
212 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200213 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200214
215 ret = mbedtls_snprintf( buf, n, "%s", "123" );
216 if( ret < 0 || (size_t) ret >= n )
217 ret = -1;
218
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200219 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
220 ref_ret != ret ||
221 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200222 {
223 return( 1 );
224 }
225
226 return( 0 );
227}
228
229static int run_test_snprintf( void )
230{
231 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200232 test_snprintf( 1, "", -1 ) != 0 ||
233 test_snprintf( 2, "1", -1 ) != 0 ||
234 test_snprintf( 3, "12", -1 ) != 0 ||
235 test_snprintf( 4, "123", 3 ) != 0 ||
236 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200237}
238
Simon Butcheraad787f2016-01-26 22:13:58 +0000239int main(int argc, const char *argv[])
Paul Bakker19343182013-08-16 13:31:10 +0200240{
SimonB8ca7bc42016-04-17 23:24:50 +0100241 /* Local Configurations and options */
SimonB15942102016-04-25 21:34:49 +0100242 const char *default_filename = "TESTCASE_FILENAME";
Simon Butcheraad787f2016-01-26 22:13:58 +0000243 const char *test_filename = NULL;
244 const char **test_files = NULL;
SimonB8ca7bc42016-04-17 23:24:50 +0100245 int testfile_count = 0;
246 int option_verbose = 0;
247
248 /* Other Local variables */
249 int arg_index = 1;
250 const char *next_arg;
251 int testfile_index, ret, i, cnt;
252 int total_errors = 0, total_tests = 0, total_skipped = 0;
Paul Bakker19343182013-08-16 13:31:10 +0200253 FILE *file;
254 char buf[5000];
255 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200256 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100259 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200260 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200262#endif
263
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200264 /*
265 * The C standard doesn't guarantee that all-bits-0 is the representation
266 * of a NULL pointer. We do however use that in our code for initializing
267 * structures, which should work on every modern platform. Let's be sure.
268 */
269 memset( &pointer, 0, sizeof( void * ) );
270 if( pointer != NULL )
271 {
272 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
273 return( 1 );
274 }
275
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200276 /*
277 * Make sure we have a snprintf that correctly zero-terminates
278 */
279 if( run_test_snprintf() != 0 )
280 {
281 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
282 return( 0 );
283 }
284
SimonB8ca7bc42016-04-17 23:24:50 +0100285 while( arg_index < argc)
286 {
287 next_arg = argv[ arg_index ];
288
289 if( strcmp(next_arg, "--verbose" ) == 0 ||
290 strcmp(next_arg, "-v" ) == 0 )
291 {
292 option_verbose = 1;
293 }
294 else if( strcmp(next_arg, "--help" ) == 0 ||
295 strcmp(next_arg, "-h" ) == 0 )
296 {
297 mbedtls_fprintf( stdout, USAGE );
298 mbedtls_exit( EXIT_SUCCESS );
299 }
300 else
301 {
302 /* Not an option, therefore treat all further arguments as the file
303 * list.
304 */
305 test_files = &argv[ arg_index ];
306 testfile_count = argc - arg_index;
307 }
308
309 arg_index++;
310 }
311
312 /* If no files were specified, assume a default */
313 if ( test_files == NULL || testfile_count == 0 )
Paul Bakker19343182013-08-16 13:31:10 +0200314 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000315 test_files = &default_filename;
316 testfile_count = 1;
317 }
Paul Bakker19343182013-08-16 13:31:10 +0200318
SimonB8ca7bc42016-04-17 23:24:50 +0100319 /* Now begin to execute the tests in the testfiles */
Simon Butcheraad787f2016-01-26 22:13:58 +0000320 for ( testfile_index = 0;
321 testfile_index < testfile_count;
322 testfile_index++ )
Paul Bakker19343182013-08-16 13:31:10 +0200323 {
Paul Bakker774180e2016-05-12 15:59:48 +0100324 int unmet_dep_count = 0;
325 char *unmet_dependencies[20];
326
Simon Butcheraad787f2016-01-26 22:13:58 +0000327 test_filename = test_files[ testfile_index ];
Paul Bakker19343182013-08-16 13:31:10 +0200328
Simon Butcheraad787f2016-01-26 22:13:58 +0000329 file = fopen( test_filename, "r" );
330 if( file == NULL )
Paul Bakker19343182013-08-16 13:31:10 +0200331 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000332 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
333 test_filename );
334 return( 1 );
335 }
336
337 while( !feof( file ) )
338 {
Paul Bakker774180e2016-05-12 15:59:48 +0100339 if( unmet_dep_count > 0 )
340 {
341 mbedtls_printf("FATAL: Dep count larger than zero at start of loop\n");
342 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
343 }
344 unmet_dep_count = 0;
Simon Butcheraad787f2016-01-26 22:13:58 +0000345
346 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
347 break;
348 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
349 mbedtls_fprintf( stdout, " " );
350 for( i = strlen( buf ) + 1; i < 67; i++ )
351 mbedtls_fprintf( stdout, "." );
352 mbedtls_fprintf( stdout, " " );
353 fflush( stdout );
354
355 total_tests++;
Paul Bakker19343182013-08-16 13:31:10 +0200356
357 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
358 break;
359 cnt = parse_arguments( buf, strlen(buf), params );
Paul Bakker19343182013-08-16 13:31:10 +0200360
Simon Butcheraad787f2016-01-26 22:13:58 +0000361 if( strcmp( params[0], "depends_on" ) == 0 )
362 {
363 for( i = 1; i < cnt; i++ )
SimonB8ca7bc42016-04-17 23:24:50 +0100364 {
365 if( dep_check( params[i] ) != DEPENDENCY_SUPPORTED )
366 {
Paul Bakker26b60bf2016-05-12 15:55:37 +0100367 if( 0 == option_verbose )
368 {
369 /* Only one count is needed if not verbose */
370 unmet_dep_count++;
371 break;
372 }
373
Paul Bakkera30a72f2016-05-12 15:52:48 +0100374 unmet_dependencies[ unmet_dep_count ] = strdup(params[i]);
375 if( unmet_dependencies[ unmet_dep_count ] == NULL )
SimonB8ca7bc42016-04-17 23:24:50 +0100376 {
377 mbedtls_printf("FATAL: Out of memory\n");
Janos Follath55abc212016-04-18 18:18:48 +0100378 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
SimonB8ca7bc42016-04-17 23:24:50 +0100379 }
380 unmet_dep_count++;
381 }
382 }
Paul Bakker19343182013-08-16 13:31:10 +0200383
Simon Butcheraad787f2016-01-26 22:13:58 +0000384 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
385 break;
386 cnt = parse_arguments( buf, strlen(buf), params );
387 }
SimonB8ca7bc42016-04-17 23:24:50 +0100388
389 // If there are no unmet dependencies execute the test
390 if( unmet_dep_count == 0 )
Simon Butcheraad787f2016-01-26 22:13:58 +0000391 {
392 test_errors = 0;
393 ret = dispatch_test( cnt, params );
394 }
395
SimonB8ca7bc42016-04-17 23:24:50 +0100396 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
Simon Butcheraad787f2016-01-26 22:13:58 +0000397 {
398 total_skipped++;
399 mbedtls_fprintf( stdout, "----\n" );
SimonB8ca7bc42016-04-17 23:24:50 +0100400
401 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
402 {
403 mbedtls_fprintf( stdout, " Test Suite not enabled" );
404 }
405
406 if( 1 == option_verbose && unmet_dep_count > 0 )
407 {
408 mbedtls_fprintf( stdout, " Unmet dependencies: " );
Paul Bakker774180e2016-05-12 15:59:48 +0100409 for( i = 0; i < unmet_dep_count; i++ )
SimonB8ca7bc42016-04-17 23:24:50 +0100410 {
411 mbedtls_fprintf(stdout, "%s ",
Paul Bakker774180e2016-05-12 15:59:48 +0100412 unmet_dependencies[i]);
413 free(unmet_dependencies[i]);
SimonB8ca7bc42016-04-17 23:24:50 +0100414 }
415 mbedtls_fprintf( stdout, "\n" );
416 }
Simon Butcheraad787f2016-01-26 22:13:58 +0000417 fflush( stdout );
Paul Bakker774180e2016-05-12 15:59:48 +0100418
419 unmet_dep_count = 0;
Simon Butcheraad787f2016-01-26 22:13:58 +0000420 }
SimonB8ca7bc42016-04-17 23:24:50 +0100421 else if( ret == DISPATCH_TEST_SUCCESS && test_errors == 0 )
Simon Butcheraad787f2016-01-26 22:13:58 +0000422 {
423 mbedtls_fprintf( stdout, "PASS\n" );
424 fflush( stdout );
425 }
SimonB8ca7bc42016-04-17 23:24:50 +0100426 else if( ret == DISPATCH_INVALID_TEST_DATA )
Simon Butcheraad787f2016-01-26 22:13:58 +0000427 {
428 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
429 fclose(file);
430 mbedtls_exit( 2 );
431 }
432 else
433 total_errors++;
434
435 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
436 break;
437 if( strlen(buf) != 0 )
438 {
439 mbedtls_fprintf( stderr, "Should be empty %d\n",
440 (int) strlen(buf) );
441 return( 1 );
442 }
Paul Bakker19343182013-08-16 13:31:10 +0200443 }
Simon Butcheraad787f2016-01-26 22:13:58 +0000444 fclose(file);
Paul Bakker774180e2016-05-12 15:59:48 +0100445
446 /* In case we encounter early end of file */
447 for( i = 0; i < unmet_dep_count; i++ )
448 free( unmet_dependencies[i] );
Paul Bakker19343182013-08-16 13:31:10 +0200449 }
Paul Bakker19343182013-08-16 13:31:10 +0200450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200452 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200454 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200458 total_tests - total_errors, total_tests, total_skipped );
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100461 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#if defined(MBEDTLS_MEMORY_DEBUG)
463 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200464#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200466#endif
Paul Bakker19343182013-08-16 13:31:10 +0200467
468 return( total_errors != 0 );
469}
470