blob: 525df5b2430fcbe435e02956134cd8cb7f40907c [file] [log] [blame]
Paul Bakkerde56ca12013-09-15 17:05:21 +02001SUITE_PRE_DEP
2#define TEST_SUITE_ACTIVE
3
Paul Bakker19343182013-08-16 13:31:10 +02004int verify_string( char **str )
5{
6 if( (*str)[0] != '"' ||
7 (*str)[strlen( *str ) - 1] != '"' )
8 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009 mbedtls_printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
Paul Bakker19343182013-08-16 13:31:10 +020010 return( -1 );
11 }
12
13 (*str)++;
14 (*str)[strlen( *str ) - 1] = '\0';
15
16 return( 0 );
17}
18
19int 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é-Gonnard725afd82014-02-01 11:54:28 +010041 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
42 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
43 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
Paul Bakker19343182013-08-16 13:31:10 +020044 {
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
60MAPPING_CODE
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_printf( "Expected integer for parameter and got: %s\n", str );
SimonB8ca7bc42016-04-17 23:24:50 +010063 return( KEY_VALUE_MAPPING_NOT_FOUND );
Paul Bakker19343182013-08-16 13:31:10 +020064}
65
SimonB152ea182016-02-15 23:27:28 +000066
67/*----------------------------------------------------------------------------*/
68/* Test Case code */
69
Paul Bakkerde56ca12013-09-15 17:05:21 +020070FUNCTION_CODE
71SUITE_POST_DEP
72
SimonB152ea182016-02-15 23:27:28 +000073
74/*----------------------------------------------------------------------------*/
75/* Test dispatch code */
76
Paul Bakker19343182013-08-16 13:31:10 +020077int dep_check( char *str )
78{
79 if( str == NULL )
80 return( 1 );
81
82DEP_CHECK_CODE
83
SimonB8ca7bc42016-04-17 23:24:50 +010084 return( DEPENDENCY_NOT_SUPPORTED );
Paul Bakker19343182013-08-16 13:31:10 +020085}
86
Paul Bakker19343182013-08-16 13:31:10 +020087int dispatch_test(int cnt, char *params[50])
88{
89 int ret;
Paul Bakkerb34fef22013-08-20 12:06:33 +020090 ((void) cnt);
91 ((void) params);
Paul Bakker19343182013-08-16 13:31:10 +020092
Paul Bakkerb34fef22013-08-20 12:06:33 +020093#if defined(TEST_SUITE_ACTIVE)
SimonB8ca7bc42016-04-17 23:24:50 +010094 ret = DISPATCH_TEST_SUCCESS;
95
Paul Bakker19343182013-08-16 13:31:10 +020096DISPATCH_FUNCTION
97 {
SimonB8ca7bc42016-04-17 23:24:50 +010098 mbedtls_fprintf( stdout,
99 "FAILED\nSkipping unknown test function '%s'\n",
100 params[0] );
Paul Bakker19343182013-08-16 13:31:10 +0200101 fflush( stdout );
SimonB8ca7bc42016-04-17 23:24:50 +0100102 ret = DISPATCH_TEST_FN_NOT_FOUND;
Paul Bakker19343182013-08-16 13:31:10 +0200103 }
Paul Bakkerb34fef22013-08-20 12:06:33 +0200104#else
SimonB8ca7bc42016-04-17 23:24:50 +0100105 ret = DISPATCH_UNSUPPORTED_SUITE;
Paul Bakkerb34fef22013-08-20 12:06:33 +0200106#endif
Paul Bakker19343182013-08-16 13:31:10 +0200107 return( ret );
108}
109
SimonB152ea182016-02-15 23:27:28 +0000110
111/*----------------------------------------------------------------------------*/
112/* Main Test code */
113
SimonB8ca7bc42016-04-17 23:24:50 +0100114#define USAGE \
115 "Usage: %s [OPTIONS] files...\n\n" \
116 " Command line arguments:\n" \
117 " files... One or more test data file. If no file is specified\n" \
118 " the followimg default test case is used:\n" \
119 " %s\n\n" \
120 " Options:\n" \
121 " -v | --verbose Display full information about each test\n" \
122 " -h | --help Display this information\n\n", \
123 argv[0], \
124 "TEST_FILENAME"
125
126
Paul Bakker19343182013-08-16 13:31:10 +0200127int get_line( FILE *f, char *buf, size_t len )
128{
129 char *ret;
130
131 ret = fgets( buf, len, f );
132 if( ret == NULL )
133 return( -1 );
134
135 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
136 buf[strlen(buf) - 1] = '\0';
137 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
138 buf[strlen(buf) - 1] = '\0';
139
140 return( 0 );
141}
142
143int 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é-Gonnard2d5f1422014-01-22 16:01:17 +0100155 p++;
156 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200157 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é-Gonnard2d5f1422014-01-22 16:01:17 +0100169 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200170 }
171
SimonB0269dad2016-02-17 23:34:30 +0000172 /* Replace newlines, question marks and colons in strings */
Paul Bakker19343182013-08-16 13:31:10 +0200173 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é-Gonnard7b6dcbe2015-06-22 10:48:01 +0200204static 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é-Gonnard4b00f082015-06-26 11:24:32 +0200208 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200209
210 ret = mbedtls_snprintf( buf, n, "%s", "123" );
211 if( ret < 0 || (size_t) ret >= n )
212 ret = -1;
213
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200214 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é-Gonnard7b6dcbe2015-06-22 10:48:01 +0200217 {
218 return( 1 );
219 }
220
221 return( 0 );
222}
223
224static int run_test_snprintf( void )
225{
226 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200227 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é-Gonnard7b6dcbe2015-06-22 10:48:01 +0200232}
233
Simon Butcheraad787f2016-01-26 22:13:58 +0000234int main(int argc, const char *argv[])
Paul Bakker19343182013-08-16 13:31:10 +0200235{
SimonB8ca7bc42016-04-17 23:24:50 +0100236 /* Local Configurations and options */
Simon Butcheraad787f2016-01-26 22:13:58 +0000237 const char *default_filename = "TEST_FILENAME";
238 const char *test_filename = NULL;
239 const char **test_files = NULL;
SimonB8ca7bc42016-04-17 23:24:50 +0100240 int testfile_count = 0;
241 int option_verbose = 0;
242
243 /* Other Local variables */
244 int arg_index = 1;
245 const char *next_arg;
246 int testfile_index, ret, i, cnt;
247 int total_errors = 0, total_tests = 0, total_skipped = 0;
Paul Bakker19343182013-08-16 13:31:10 +0200248 FILE *file;
249 char buf[5000];
250 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200251 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100254 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200255 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200257#endif
258
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200259 /*
260 * The C standard doesn't guarantee that all-bits-0 is the representation
261 * of a NULL pointer. We do however use that in our code for initializing
262 * structures, which should work on every modern platform. Let's be sure.
263 */
264 memset( &pointer, 0, sizeof( void * ) );
265 if( pointer != NULL )
266 {
267 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
268 return( 1 );
269 }
270
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200271 /*
272 * Make sure we have a snprintf that correctly zero-terminates
273 */
274 if( run_test_snprintf() != 0 )
275 {
276 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
277 return( 0 );
278 }
279
SimonB8ca7bc42016-04-17 23:24:50 +0100280 while( arg_index < argc)
281 {
282 next_arg = argv[ arg_index ];
283
284 if( strcmp(next_arg, "--verbose" ) == 0 ||
285 strcmp(next_arg, "-v" ) == 0 )
286 {
287 option_verbose = 1;
288 }
289 else if( strcmp(next_arg, "--help" ) == 0 ||
290 strcmp(next_arg, "-h" ) == 0 )
291 {
292 mbedtls_fprintf( stdout, USAGE );
293 mbedtls_exit( EXIT_SUCCESS );
294 }
295 else
296 {
297 /* Not an option, therefore treat all further arguments as the file
298 * list.
299 */
300 test_files = &argv[ arg_index ];
301 testfile_count = argc - arg_index;
302 }
303
304 arg_index++;
305 }
306
307 /* If no files were specified, assume a default */
308 if ( test_files == NULL || testfile_count == 0 )
Paul Bakker19343182013-08-16 13:31:10 +0200309 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000310 test_files = &default_filename;
311 testfile_count = 1;
312 }
Paul Bakker19343182013-08-16 13:31:10 +0200313
SimonB8ca7bc42016-04-17 23:24:50 +0100314 /* Now begin to execute the tests in the testfiles */
Simon Butcheraad787f2016-01-26 22:13:58 +0000315 for ( testfile_index = 0;
316 testfile_index < testfile_count;
317 testfile_index++ )
Paul Bakker19343182013-08-16 13:31:10 +0200318 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000319 test_filename = test_files[ testfile_index ];
Paul Bakker19343182013-08-16 13:31:10 +0200320
Simon Butcheraad787f2016-01-26 22:13:58 +0000321 file = fopen( test_filename, "r" );
322 if( file == NULL )
Paul Bakker19343182013-08-16 13:31:10 +0200323 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000324 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
325 test_filename );
326 return( 1 );
327 }
328
329 while( !feof( file ) )
330 {
SimonB8ca7bc42016-04-17 23:24:50 +0100331 int unmet_dep_count = 0;
332 char *unmet_dependencies[20];
Simon Butcheraad787f2016-01-26 22:13:58 +0000333
334 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
335 break;
336 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
337 mbedtls_fprintf( stdout, " " );
338 for( i = strlen( buf ) + 1; i < 67; i++ )
339 mbedtls_fprintf( stdout, "." );
340 mbedtls_fprintf( stdout, " " );
341 fflush( stdout );
342
343 total_tests++;
Paul Bakker19343182013-08-16 13:31:10 +0200344
345 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
346 break;
347 cnt = parse_arguments( buf, strlen(buf), params );
Paul Bakker19343182013-08-16 13:31:10 +0200348
Simon Butcheraad787f2016-01-26 22:13:58 +0000349 if( strcmp( params[0], "depends_on" ) == 0 )
350 {
351 for( i = 1; i < cnt; i++ )
SimonB8ca7bc42016-04-17 23:24:50 +0100352 {
353 if( dep_check( params[i] ) != DEPENDENCY_SUPPORTED )
354 {
355 unmet_dependencies[ i-1 ] = strdup(params[i]);
356 if( unmet_dependencies[ i-1 ] == NULL )
357 {
358 mbedtls_printf("FATAL: Out of memory\n");
359 mbedtls_exit( MBEDTLS_PLATFORM_STD_EXIT_FAILURE );
360 }
361 unmet_dep_count++;
362 }
363 }
Paul Bakker19343182013-08-16 13:31:10 +0200364
Simon Butcheraad787f2016-01-26 22:13:58 +0000365 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
366 break;
367 cnt = parse_arguments( buf, strlen(buf), params );
368 }
SimonB8ca7bc42016-04-17 23:24:50 +0100369
370 // If there are no unmet dependencies execute the test
371 if( unmet_dep_count == 0 )
Simon Butcheraad787f2016-01-26 22:13:58 +0000372 {
373 test_errors = 0;
374 ret = dispatch_test( cnt, params );
375 }
376
SimonB8ca7bc42016-04-17 23:24:50 +0100377 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
Simon Butcheraad787f2016-01-26 22:13:58 +0000378 {
379 total_skipped++;
380 mbedtls_fprintf( stdout, "----\n" );
SimonB8ca7bc42016-04-17 23:24:50 +0100381
382 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
383 {
384 mbedtls_fprintf( stdout, " Test Suite not enabled" );
385 }
386
387 if( 1 == option_verbose && unmet_dep_count > 0 )
388 {
389 mbedtls_fprintf( stdout, " Unmet dependencies: " );
390 while( unmet_dep_count > 0)
391 {
392 mbedtls_fprintf(stdout, "%s ",
393 unmet_dependencies[unmet_dep_count - 1]);
394 free(unmet_dependencies[unmet_dep_count - 1]);
395 unmet_dep_count--;
396 }
397 mbedtls_fprintf( stdout, "\n" );
398 }
Simon Butcheraad787f2016-01-26 22:13:58 +0000399 fflush( stdout );
400 }
SimonB8ca7bc42016-04-17 23:24:50 +0100401 else if( ret == DISPATCH_TEST_SUCCESS && test_errors == 0 )
Simon Butcheraad787f2016-01-26 22:13:58 +0000402 {
403 mbedtls_fprintf( stdout, "PASS\n" );
404 fflush( stdout );
405 }
SimonB8ca7bc42016-04-17 23:24:50 +0100406 else if( ret == DISPATCH_INVALID_TEST_DATA )
Simon Butcheraad787f2016-01-26 22:13:58 +0000407 {
408 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
409 fclose(file);
410 mbedtls_exit( 2 );
411 }
412 else
413 total_errors++;
414
415 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
416 break;
417 if( strlen(buf) != 0 )
418 {
419 mbedtls_fprintf( stderr, "Should be empty %d\n",
420 (int) strlen(buf) );
421 return( 1 );
422 }
Paul Bakker19343182013-08-16 13:31:10 +0200423 }
Simon Butcheraad787f2016-01-26 22:13:58 +0000424 fclose(file);
Paul Bakker19343182013-08-16 13:31:10 +0200425 }
Paul Bakker19343182013-08-16 13:31:10 +0200426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200428 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200430 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200434 total_tests - total_errors, total_tests, total_skipped );
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100437 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_MEMORY_DEBUG)
439 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200440#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200442#endif
Paul Bakker19343182013-08-16 13:31:10 +0200443
444 return( total_errors != 0 );
445}
446