blob: c5d6cd86bea48b245223e85b1996a8483272c03b [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 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000324 test_filename = test_files[ testfile_index ];
Paul Bakker19343182013-08-16 13:31:10 +0200325
Simon Butcheraad787f2016-01-26 22:13:58 +0000326 file = fopen( test_filename, "r" );
327 if( file == NULL )
Paul Bakker19343182013-08-16 13:31:10 +0200328 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000329 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
330 test_filename );
331 return( 1 );
332 }
333
334 while( !feof( file ) )
335 {
SimonB8ca7bc42016-04-17 23:24:50 +0100336 int unmet_dep_count = 0;
337 char *unmet_dependencies[20];
Simon Butcheraad787f2016-01-26 22:13:58 +0000338
339 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
340 break;
341 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
342 mbedtls_fprintf( stdout, " " );
343 for( i = strlen( buf ) + 1; i < 67; i++ )
344 mbedtls_fprintf( stdout, "." );
345 mbedtls_fprintf( stdout, " " );
346 fflush( stdout );
347
348 total_tests++;
Paul Bakker19343182013-08-16 13:31:10 +0200349
350 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
351 break;
352 cnt = parse_arguments( buf, strlen(buf), params );
Paul Bakker19343182013-08-16 13:31:10 +0200353
Simon Butcheraad787f2016-01-26 22:13:58 +0000354 if( strcmp( params[0], "depends_on" ) == 0 )
355 {
356 for( i = 1; i < cnt; i++ )
SimonB8ca7bc42016-04-17 23:24:50 +0100357 {
358 if( dep_check( params[i] ) != DEPENDENCY_SUPPORTED )
359 {
360 unmet_dependencies[ i-1 ] = strdup(params[i]);
361 if( unmet_dependencies[ i-1 ] == NULL )
362 {
363 mbedtls_printf("FATAL: Out of memory\n");
Janos Follath55abc212016-04-18 18:18:48 +0100364 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
SimonB8ca7bc42016-04-17 23:24:50 +0100365 }
366 unmet_dep_count++;
367 }
368 }
Paul Bakker19343182013-08-16 13:31:10 +0200369
Simon Butcheraad787f2016-01-26 22:13:58 +0000370 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
371 break;
372 cnt = parse_arguments( buf, strlen(buf), params );
373 }
SimonB8ca7bc42016-04-17 23:24:50 +0100374
375 // If there are no unmet dependencies execute the test
376 if( unmet_dep_count == 0 )
Simon Butcheraad787f2016-01-26 22:13:58 +0000377 {
378 test_errors = 0;
379 ret = dispatch_test( cnt, params );
380 }
381
SimonB8ca7bc42016-04-17 23:24:50 +0100382 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
Simon Butcheraad787f2016-01-26 22:13:58 +0000383 {
384 total_skipped++;
385 mbedtls_fprintf( stdout, "----\n" );
SimonB8ca7bc42016-04-17 23:24:50 +0100386
387 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
388 {
389 mbedtls_fprintf( stdout, " Test Suite not enabled" );
390 }
391
392 if( 1 == option_verbose && unmet_dep_count > 0 )
393 {
394 mbedtls_fprintf( stdout, " Unmet dependencies: " );
395 while( unmet_dep_count > 0)
396 {
397 mbedtls_fprintf(stdout, "%s ",
398 unmet_dependencies[unmet_dep_count - 1]);
399 free(unmet_dependencies[unmet_dep_count - 1]);
400 unmet_dep_count--;
401 }
402 mbedtls_fprintf( stdout, "\n" );
403 }
Simon Butcheraad787f2016-01-26 22:13:58 +0000404 fflush( stdout );
405 }
SimonB8ca7bc42016-04-17 23:24:50 +0100406 else if( ret == DISPATCH_TEST_SUCCESS && test_errors == 0 )
Simon Butcheraad787f2016-01-26 22:13:58 +0000407 {
408 mbedtls_fprintf( stdout, "PASS\n" );
409 fflush( stdout );
410 }
SimonB8ca7bc42016-04-17 23:24:50 +0100411 else if( ret == DISPATCH_INVALID_TEST_DATA )
Simon Butcheraad787f2016-01-26 22:13:58 +0000412 {
413 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
414 fclose(file);
415 mbedtls_exit( 2 );
416 }
417 else
418 total_errors++;
419
420 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
421 break;
422 if( strlen(buf) != 0 )
423 {
424 mbedtls_fprintf( stderr, "Should be empty %d\n",
425 (int) strlen(buf) );
426 return( 1 );
427 }
Paul Bakker19343182013-08-16 13:31:10 +0200428 }
Simon Butcheraad787f2016-01-26 22:13:58 +0000429 fclose(file);
Paul Bakker19343182013-08-16 13:31:10 +0200430 }
Paul Bakker19343182013-08-16 13:31:10 +0200431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200433 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200435 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200439 total_tests - total_errors, total_tests, total_skipped );
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100442 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_MEMORY_DEBUG)
444 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200445#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200447#endif
Paul Bakker19343182013-08-16 13:31:10 +0200448
449 return( total_errors != 0 );
450}
451