blob: 7ec69b45dd13815a8942ba9694627272e70e8a36 [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 );
Paul Bakker19343182013-08-16 13:31:10 +020063 return( -1 );
64}
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
84 return( 1 );
85}
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)
Paul Bakker19343182013-08-16 13:31:10 +020094DISPATCH_FUNCTION
95 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
Paul Bakker19343182013-08-16 13:31:10 +020097 fflush( stdout );
98 return( 1 );
99 }
Paul Bakkerb34fef22013-08-20 12:06:33 +0200100#else
101 return( 3 );
102#endif
Paul Bakker19343182013-08-16 13:31:10 +0200103 return( ret );
104}
105
SimonB152ea182016-02-15 23:27:28 +0000106
107/*----------------------------------------------------------------------------*/
108/* Main Test code */
109
Paul Bakker19343182013-08-16 13:31:10 +0200110int get_line( FILE *f, char *buf, size_t len )
111{
112 char *ret;
113
114 ret = fgets( buf, len, f );
115 if( ret == NULL )
116 return( -1 );
117
118 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
119 buf[strlen(buf) - 1] = '\0';
120 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
121 buf[strlen(buf) - 1] = '\0';
122
123 return( 0 );
124}
125
126int parse_arguments( char *buf, size_t len, char *params[50] )
127{
128 int cnt = 0, i;
129 char *cur = buf;
130 char *p = buf, *q;
131
132 params[cnt++] = cur;
133
134 while( *p != '\0' && p < buf + len )
135 {
136 if( *p == '\\' )
137 {
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100138 p++;
139 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200140 continue;
141 }
142 if( *p == ':' )
143 {
144 if( p + 1 < buf + len )
145 {
146 cur = p + 1;
147 params[cnt++] = cur;
148 }
149 *p = '\0';
150 }
151
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100152 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200153 }
154
SimonB0269dad2016-02-17 23:34:30 +0000155 /* Replace newlines, question marks and colons in strings */
Paul Bakker19343182013-08-16 13:31:10 +0200156 for( i = 0; i < cnt; i++ )
157 {
158 p = params[i];
159 q = params[i];
160
161 while( *p != '\0' )
162 {
163 if( *p == '\\' && *(p + 1) == 'n' )
164 {
165 p += 2;
166 *(q++) = '\n';
167 }
168 else if( *p == '\\' && *(p + 1) == ':' )
169 {
170 p += 2;
171 *(q++) = ':';
172 }
173 else if( *p == '\\' && *(p + 1) == '?' )
174 {
175 p += 2;
176 *(q++) = '?';
177 }
178 else
179 *(q++) = *(p++);
180 }
181 *q = '\0';
182 }
183
184 return( cnt );
185}
186
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200187static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
188{
189 int ret;
190 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200191 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200192
193 ret = mbedtls_snprintf( buf, n, "%s", "123" );
194 if( ret < 0 || (size_t) ret >= n )
195 ret = -1;
196
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200197 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
198 ref_ret != ret ||
199 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200200 {
201 return( 1 );
202 }
203
204 return( 0 );
205}
206
207static int run_test_snprintf( void )
208{
209 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200210 test_snprintf( 1, "", -1 ) != 0 ||
211 test_snprintf( 2, "1", -1 ) != 0 ||
212 test_snprintf( 3, "12", -1 ) != 0 ||
213 test_snprintf( 4, "123", 3 ) != 0 ||
214 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200215}
216
Simon Butcheraad787f2016-01-26 22:13:58 +0000217int main(int argc, const char *argv[])
Paul Bakker19343182013-08-16 13:31:10 +0200218{
Simon Butcheraad787f2016-01-26 22:13:58 +0000219 int testfile_index, testfile_count, ret, i, cnt;
220 int total_errors = 0, total_tests = 0, total_skipped = 0;
221 const char *default_filename = "TEST_FILENAME";
222 const char *test_filename = NULL;
223 const char **test_files = NULL;
Paul Bakker19343182013-08-16 13:31:10 +0200224 FILE *file;
225 char buf[5000];
226 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200227 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100230 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200231 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200233#endif
234
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200235 /*
236 * The C standard doesn't guarantee that all-bits-0 is the representation
237 * of a NULL pointer. We do however use that in our code for initializing
238 * structures, which should work on every modern platform. Let's be sure.
239 */
240 memset( &pointer, 0, sizeof( void * ) );
241 if( pointer != NULL )
242 {
243 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
244 return( 1 );
245 }
246
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200247 /*
248 * Make sure we have a snprintf that correctly zero-terminates
249 */
250 if( run_test_snprintf() != 0 )
251 {
252 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
253 return( 0 );
254 }
255
Simon Butcheraad787f2016-01-26 22:13:58 +0000256 if ( argc <= 1 )
Paul Bakker19343182013-08-16 13:31:10 +0200257 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000258 test_files = &default_filename;
259 testfile_count = 1;
260 }
261 else
262 {
263 test_files = &argv[1];
264 testfile_count = argc - 1;
Paul Bakker19343182013-08-16 13:31:10 +0200265 }
266
Simon Butcheraad787f2016-01-26 22:13:58 +0000267 for ( testfile_index = 0;
268 testfile_index < testfile_count;
269 testfile_index++ )
Paul Bakker19343182013-08-16 13:31:10 +0200270 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000271 test_filename = test_files[ testfile_index ];
Paul Bakker19343182013-08-16 13:31:10 +0200272
Simon Butcheraad787f2016-01-26 22:13:58 +0000273 file = fopen( test_filename, "r" );
274 if( file == NULL )
Paul Bakker19343182013-08-16 13:31:10 +0200275 {
Simon Butcheraad787f2016-01-26 22:13:58 +0000276 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
277 test_filename );
278 return( 1 );
279 }
280
281 while( !feof( file ) )
282 {
283 int skip = 0;
284
285 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
286 break;
287 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
288 mbedtls_fprintf( stdout, " " );
289 for( i = strlen( buf ) + 1; i < 67; i++ )
290 mbedtls_fprintf( stdout, "." );
291 mbedtls_fprintf( stdout, " " );
292 fflush( stdout );
293
294 total_tests++;
Paul Bakker19343182013-08-16 13:31:10 +0200295
296 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
297 break;
298 cnt = parse_arguments( buf, strlen(buf), params );
Paul Bakker19343182013-08-16 13:31:10 +0200299
Simon Butcheraad787f2016-01-26 22:13:58 +0000300 if( strcmp( params[0], "depends_on" ) == 0 )
301 {
302 for( i = 1; i < cnt; i++ )
303 if( dep_check( params[i] ) != 0 )
304 skip = 1;
Paul Bakker19343182013-08-16 13:31:10 +0200305
Simon Butcheraad787f2016-01-26 22:13:58 +0000306 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
307 break;
308 cnt = parse_arguments( buf, strlen(buf), params );
309 }
Paul Bakker19343182013-08-16 13:31:10 +0200310
Simon Butcheraad787f2016-01-26 22:13:58 +0000311 if( skip == 0 )
312 {
313 test_errors = 0;
314 ret = dispatch_test( cnt, params );
315 }
316
317 if( skip == 1 || ret == 3 )
318 {
319 total_skipped++;
320 mbedtls_fprintf( stdout, "----\n" );
321 fflush( stdout );
322 }
323 else if( ret == 0 && test_errors == 0 )
324 {
325 mbedtls_fprintf( stdout, "PASS\n" );
326 fflush( stdout );
327 }
328 else if( ret == 2 )
329 {
330 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
331 fclose(file);
332 mbedtls_exit( 2 );
333 }
334 else
335 total_errors++;
336
337 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
338 break;
339 if( strlen(buf) != 0 )
340 {
341 mbedtls_fprintf( stderr, "Should be empty %d\n",
342 (int) strlen(buf) );
343 return( 1 );
344 }
Paul Bakker19343182013-08-16 13:31:10 +0200345 }
Simon Butcheraad787f2016-01-26 22:13:58 +0000346 fclose(file);
Paul Bakker19343182013-08-16 13:31:10 +0200347 }
Paul Bakker19343182013-08-16 13:31:10 +0200348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200350 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200352 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200356 total_tests - total_errors, total_tests, total_skipped );
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100359 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_MEMORY_DEBUG)
361 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200364#endif
Paul Bakker19343182013-08-16 13:31:10 +0200365
366 return( total_errors != 0 );
367}
368