blob: a60db941b27acdca8f23045879be58d722b90fe0 [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";
217
218 ret = mbedtls_snprintf( buf, n, "%s", "123" );
219 if( ret < 0 || (size_t) ret >= n )
220 ret = -1;
221
222 if( memcmp( ref_buf, buf, sizeof buf ) != 0 ||
223 ref_ret != ret )
224 {
225 return( 1 );
226 }
227
228 return( 0 );
229}
230
231static int run_test_snprintf( void )
232{
233 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
234 test_snprintf( 1, "\0xxxxxxxx", -1 ) != 0 ||
235 test_snprintf( 2, "1\0xxxxxxx", -1 ) != 0 ||
236 test_snprintf( 3, "12\0xxxxxx", -1 ) != 0 ||
237 test_snprintf( 4, "123\0xxxxx", 3 ) != 0 ||
238 test_snprintf( 5, "123\0xxxxx", 3 ) != 0 );
239}
240
Paul Bakker19343182013-08-16 13:31:10 +0200241int main()
242{
243 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
244 const char *filename = "TEST_FILENAME";
245 FILE *file;
246 char buf[5000];
247 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200248 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100251 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200252 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200254#endif
255
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200256 /*
257 * The C standard doesn't guarantee that all-bits-0 is the representation
258 * of a NULL pointer. We do however use that in our code for initializing
259 * structures, which should work on every modern platform. Let's be sure.
260 */
261 memset( &pointer, 0, sizeof( void * ) );
262 if( pointer != NULL )
263 {
264 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
265 return( 1 );
266 }
267
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200268 /*
269 * Make sure we have a snprintf that correctly zero-terminates
270 */
271 if( run_test_snprintf() != 0 )
272 {
273 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
274 return( 0 );
275 }
276
Paul Bakker19343182013-08-16 13:31:10 +0200277 file = fopen( filename, "r" );
278 if( file == NULL )
279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_fprintf( stderr, "Failed to open\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200281 return( 1 );
282 }
283
284 while( !feof( file ) )
285 {
286 int skip = 0;
287
288 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
289 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
291 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200292 for( i = strlen( buf ) + 1; i < 67; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_fprintf( stdout, "." );
294 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200295 fflush( stdout );
296
297 total_tests++;
298
299 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
300 break;
301 cnt = parse_arguments( buf, strlen(buf), params );
302
303 if( strcmp( params[0], "depends_on" ) == 0 )
304 {
305 for( i = 1; i < cnt; i++ )
306 if( dep_check( params[i] ) != 0 )
307 skip = 1;
308
309 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
310 break;
311 cnt = parse_arguments( buf, strlen(buf), params );
312 }
313
314 if( skip == 0 )
315 {
316 test_errors = 0;
317 ret = dispatch_test( cnt, params );
318 }
319
320 if( skip == 1 || ret == 3 )
321 {
322 total_skipped++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_fprintf( stdout, "----\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200324 fflush( stdout );
325 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200326 else if( ret == 0 && test_errors == 0 )
Paul Bakker19343182013-08-16 13:31:10 +0200327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_fprintf( stdout, "PASS\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200329 fflush( stdout );
330 }
331 else if( ret == 2 )
332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200334 fclose(file);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_exit( 2 );
Paul Bakker19343182013-08-16 13:31:10 +0200336 }
337 else
338 total_errors++;
339
340 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
341 break;
342 if( strlen(buf) != 0 )
343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200345 return( 1 );
346 }
347 }
348 fclose(file);
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200351 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200357 total_tests - total_errors, total_tests, total_skipped );
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100360 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_MEMORY_DEBUG)
362 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200365#endif
Paul Bakker19343182013-08-16 13:31:10 +0200366
367 return( total_errors != 0 );
368}
369