blob: 420ee76977fa96bb22413d85b4d0459e0d0bcd74 [file] [log] [blame]
Rich Evans77d36382015-01-30 12:12:11 +00001#include <string.h>
2
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/platform.h"
Rich Evans77d36382015-01-30 12:12:11 +00005#else
Rich Evans012acfc2015-01-30 12:12:11 +00006#include <stdio.h>
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +02007#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#define mbedtls_exit exit
9#define mbedtls_free free
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020010#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#define mbedtls_fprintf fprintf
12#define mbedtls_printf printf
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020013#define mbedtls_snprintf snprintf
Rich Evans77d36382015-01-30 12:12:11 +000014#endif
15
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/memory_buffer_alloc.h"
Manuel Pégourié-Gonnard0ac1d2d2015-01-26 14:58:04 +010018#endif
19
Paul Bakker19343182013-08-16 13:31:10 +020020static int test_errors = 0;
21
Paul Bakkerde56ca12013-09-15 17:05:21 +020022SUITE_PRE_DEP
23#define TEST_SUITE_ACTIVE
24
Manuel Pégourié-Gonnarde91e21c2015-06-22 18:47:07 +020025static void test_fail( const char *test )
Paul Bakker19343182013-08-16 13:31:10 +020026{
Paul Bakker19343182013-08-16 13:31:10 +020027 test_errors++;
Paul Bakker55a7e902013-08-19 14:02:10 +020028 if( test_errors == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_printf( "FAILED\n" );
30 mbedtls_printf( " %s\n", test );
Paul Bakker19343182013-08-16 13:31:10 +020031}
32
Paul Bakkerbb20f4b2013-08-20 12:41:33 +020033#define TEST_ASSERT( TEST ) \
Manuel Pégourié-Gonnarde91e21c2015-06-22 18:47:07 +020034 do { \
35 if( ! (TEST) ) \
36 { \
37 test_fail( #TEST ); \
38 goto exit; \
39 } \
40 } while( 0 )
Paul Bakker19343182013-08-16 13:31:10 +020041
42int verify_string( char **str )
43{
44 if( (*str)[0] != '"' ||
45 (*str)[strlen( *str ) - 1] != '"' )
46 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
Paul Bakker19343182013-08-16 13:31:10 +020048 return( -1 );
49 }
50
51 (*str)++;
52 (*str)[strlen( *str ) - 1] = '\0';
53
54 return( 0 );
55}
56
57int verify_int( char *str, int *value )
58{
59 size_t i;
60 int minus = 0;
61 int digits = 1;
62 int hex = 0;
63
64 for( i = 0; i < strlen( str ); i++ )
65 {
66 if( i == 0 && str[i] == '-' )
67 {
68 minus = 1;
69 continue;
70 }
71
72 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
73 str[i - 1] == '0' && str[i] == 'x' )
74 {
75 hex = 1;
76 continue;
77 }
78
Manuel Pégourié-Gonnard725afd82014-02-01 11:54:28 +010079 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
80 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
81 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
Paul Bakker19343182013-08-16 13:31:10 +020082 {
83 digits = 0;
84 break;
85 }
86 }
87
88 if( digits )
89 {
90 if( hex )
91 *value = strtol( str, NULL, 16 );
92 else
93 *value = strtol( str, NULL, 10 );
94
95 return( 0 );
96 }
97
98MAPPING_CODE
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_printf( "Expected integer for parameter and got: %s\n", str );
Paul Bakker19343182013-08-16 13:31:10 +0200101 return( -1 );
102}
103
Paul Bakkerde56ca12013-09-15 17:05:21 +0200104FUNCTION_CODE
105SUITE_POST_DEP
106
Paul Bakker19343182013-08-16 13:31:10 +0200107int dep_check( char *str )
108{
109 if( str == NULL )
110 return( 1 );
111
112DEP_CHECK_CODE
113
114 return( 1 );
115}
116
Paul Bakker19343182013-08-16 13:31:10 +0200117int dispatch_test(int cnt, char *params[50])
118{
119 int ret;
Paul Bakkerb34fef22013-08-20 12:06:33 +0200120 ((void) cnt);
121 ((void) params);
Paul Bakker19343182013-08-16 13:31:10 +0200122
Paul Bakkerb34fef22013-08-20 12:06:33 +0200123#if defined(TEST_SUITE_ACTIVE)
Paul Bakker19343182013-08-16 13:31:10 +0200124DISPATCH_FUNCTION
125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
Paul Bakker19343182013-08-16 13:31:10 +0200127 fflush( stdout );
128 return( 1 );
129 }
Paul Bakkerb34fef22013-08-20 12:06:33 +0200130#else
131 return( 3 );
132#endif
Paul Bakker19343182013-08-16 13:31:10 +0200133 return( ret );
134}
135
136int get_line( FILE *f, char *buf, size_t len )
137{
138 char *ret;
139
140 ret = fgets( buf, len, f );
141 if( ret == NULL )
142 return( -1 );
143
144 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
145 buf[strlen(buf) - 1] = '\0';
146 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
147 buf[strlen(buf) - 1] = '\0';
148
149 return( 0 );
150}
151
152int parse_arguments( char *buf, size_t len, char *params[50] )
153{
154 int cnt = 0, i;
155 char *cur = buf;
156 char *p = buf, *q;
157
158 params[cnt++] = cur;
159
160 while( *p != '\0' && p < buf + len )
161 {
162 if( *p == '\\' )
163 {
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100164 p++;
165 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200166 continue;
167 }
168 if( *p == ':' )
169 {
170 if( p + 1 < buf + len )
171 {
172 cur = p + 1;
173 params[cnt++] = cur;
174 }
175 *p = '\0';
176 }
177
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100178 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200179 }
180
181 // Replace newlines, question marks and colons in strings
182 for( i = 0; i < cnt; i++ )
183 {
184 p = params[i];
185 q = params[i];
186
187 while( *p != '\0' )
188 {
189 if( *p == '\\' && *(p + 1) == 'n' )
190 {
191 p += 2;
192 *(q++) = '\n';
193 }
194 else if( *p == '\\' && *(p + 1) == ':' )
195 {
196 p += 2;
197 *(q++) = ':';
198 }
199 else if( *p == '\\' && *(p + 1) == '?' )
200 {
201 p += 2;
202 *(q++) = '?';
203 }
204 else
205 *(q++) = *(p++);
206 }
207 *q = '\0';
208 }
209
210 return( cnt );
211}
212
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200213static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
214{
215 int ret;
216 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200217 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200218
219 ret = mbedtls_snprintf( buf, n, "%s", "123" );
220 if( ret < 0 || (size_t) ret >= n )
221 ret = -1;
222
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200223 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
224 ref_ret != ret ||
225 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200226 {
227 return( 1 );
228 }
229
230 return( 0 );
231}
232
233static int run_test_snprintf( void )
234{
235 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200236 test_snprintf( 1, "", -1 ) != 0 ||
237 test_snprintf( 2, "1", -1 ) != 0 ||
238 test_snprintf( 3, "12", -1 ) != 0 ||
239 test_snprintf( 4, "123", 3 ) != 0 ||
240 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200241}
242
Paul Bakker19343182013-08-16 13:31:10 +0200243int main()
244{
245 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
246 const char *filename = "TEST_FILENAME";
247 FILE *file;
248 char buf[5000];
249 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200250 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100253 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200254 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200256#endif
257
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200258 /*
259 * The C standard doesn't guarantee that all-bits-0 is the representation
260 * of a NULL pointer. We do however use that in our code for initializing
261 * structures, which should work on every modern platform. Let's be sure.
262 */
263 memset( &pointer, 0, sizeof( void * ) );
264 if( pointer != NULL )
265 {
266 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
267 return( 1 );
268 }
269
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200270 /*
271 * Make sure we have a snprintf that correctly zero-terminates
272 */
273 if( run_test_snprintf() != 0 )
274 {
275 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
276 return( 0 );
277 }
278
Paul Bakker19343182013-08-16 13:31:10 +0200279 file = fopen( filename, "r" );
280 if( file == NULL )
281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_fprintf( stderr, "Failed to open\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200283 return( 1 );
284 }
285
286 while( !feof( file ) )
287 {
288 int skip = 0;
289
290 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
291 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
293 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200294 for( i = strlen( buf ) + 1; i < 67; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_fprintf( stdout, "." );
296 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200297 fflush( stdout );
298
299 total_tests++;
300
301 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
302 break;
303 cnt = parse_arguments( buf, strlen(buf), params );
304
305 if( strcmp( params[0], "depends_on" ) == 0 )
306 {
307 for( i = 1; i < cnt; i++ )
308 if( dep_check( params[i] ) != 0 )
309 skip = 1;
310
311 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
312 break;
313 cnt = parse_arguments( buf, strlen(buf), params );
314 }
315
316 if( skip == 0 )
317 {
318 test_errors = 0;
319 ret = dispatch_test( cnt, params );
320 }
321
322 if( skip == 1 || ret == 3 )
323 {
324 total_skipped++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_fprintf( stdout, "----\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200326 fflush( stdout );
327 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200328 else if( ret == 0 && test_errors == 0 )
Paul Bakker19343182013-08-16 13:31:10 +0200329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_fprintf( stdout, "PASS\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200331 fflush( stdout );
332 }
333 else if( ret == 2 )
334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200336 fclose(file);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_exit( 2 );
Paul Bakker19343182013-08-16 13:31:10 +0200338 }
339 else
340 total_errors++;
341
342 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
343 break;
344 if( strlen(buf) != 0 )
345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200347 return( 1 );
348 }
349 }
350 fclose(file);
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200353 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200355 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200359 total_tests - total_errors, total_tests, total_skipped );
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100362 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_MEMORY_DEBUG)
364 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200367#endif
Paul Bakker19343182013-08-16 13:31:10 +0200368
369 return( total_errors != 0 );
370}
371