blob: ba13288774020107e4999f06546bd17c2465674a [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
Paul Bakker8fc30b12013-11-25 13:29:43 +010025static int test_assert( int correct, const char *test )
Paul Bakker19343182013-08-16 13:31:10 +020026{
27 if( correct )
28 return( 0 );
29
30 test_errors++;
Paul Bakker55a7e902013-08-19 14:02:10 +020031 if( test_errors == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_printf( "FAILED\n" );
33 mbedtls_printf( " %s\n", test );
Paul Bakker19343182013-08-16 13:31:10 +020034
35 return( 1 );
36}
37
Paul Bakkerbb20f4b2013-08-20 12:41:33 +020038#define TEST_ASSERT( TEST ) \
39 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
Paul Bakker318d0fe2014-07-10 14:59:25 +020040 if( test_errors) goto exit; \
Paul Bakkerbb20f4b2013-08-20 12:41:33 +020041 } while (0)
Paul Bakker19343182013-08-16 13:31:10 +020042
43int verify_string( char **str )
44{
45 if( (*str)[0] != '"' ||
46 (*str)[strlen( *str ) - 1] != '"' )
47 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
Paul Bakker19343182013-08-16 13:31:10 +020049 return( -1 );
50 }
51
52 (*str)++;
53 (*str)[strlen( *str ) - 1] = '\0';
54
55 return( 0 );
56}
57
58int verify_int( char *str, int *value )
59{
60 size_t i;
61 int minus = 0;
62 int digits = 1;
63 int hex = 0;
64
65 for( i = 0; i < strlen( str ); i++ )
66 {
67 if( i == 0 && str[i] == '-' )
68 {
69 minus = 1;
70 continue;
71 }
72
73 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
74 str[i - 1] == '0' && str[i] == 'x' )
75 {
76 hex = 1;
77 continue;
78 }
79
Manuel Pégourié-Gonnard725afd82014-02-01 11:54:28 +010080 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
81 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
82 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
Paul Bakker19343182013-08-16 13:31:10 +020083 {
84 digits = 0;
85 break;
86 }
87 }
88
89 if( digits )
90 {
91 if( hex )
92 *value = strtol( str, NULL, 16 );
93 else
94 *value = strtol( str, NULL, 10 );
95
96 return( 0 );
97 }
98
99MAPPING_CODE
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_printf( "Expected integer for parameter and got: %s\n", str );
Paul Bakker19343182013-08-16 13:31:10 +0200102 return( -1 );
103}
104
Paul Bakkerde56ca12013-09-15 17:05:21 +0200105FUNCTION_CODE
106SUITE_POST_DEP
107
Paul Bakker19343182013-08-16 13:31:10 +0200108int dep_check( char *str )
109{
110 if( str == NULL )
111 return( 1 );
112
113DEP_CHECK_CODE
114
115 return( 1 );
116}
117
Paul Bakker19343182013-08-16 13:31:10 +0200118int dispatch_test(int cnt, char *params[50])
119{
120 int ret;
Paul Bakkerb34fef22013-08-20 12:06:33 +0200121 ((void) cnt);
122 ((void) params);
Paul Bakker19343182013-08-16 13:31:10 +0200123
Paul Bakkerb34fef22013-08-20 12:06:33 +0200124#if defined(TEST_SUITE_ACTIVE)
Paul Bakker19343182013-08-16 13:31:10 +0200125DISPATCH_FUNCTION
126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
Paul Bakker19343182013-08-16 13:31:10 +0200128 fflush( stdout );
129 return( 1 );
130 }
Paul Bakkerb34fef22013-08-20 12:06:33 +0200131#else
132 return( 3 );
133#endif
Paul Bakker19343182013-08-16 13:31:10 +0200134 return( ret );
135}
136
137int get_line( FILE *f, char *buf, size_t len )
138{
139 char *ret;
140
141 ret = fgets( buf, len, f );
142 if( ret == NULL )
143 return( -1 );
144
145 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
146 buf[strlen(buf) - 1] = '\0';
147 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
148 buf[strlen(buf) - 1] = '\0';
149
150 return( 0 );
151}
152
153int parse_arguments( char *buf, size_t len, char *params[50] )
154{
155 int cnt = 0, i;
156 char *cur = buf;
157 char *p = buf, *q;
158
159 params[cnt++] = cur;
160
161 while( *p != '\0' && p < buf + len )
162 {
163 if( *p == '\\' )
164 {
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100165 p++;
166 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200167 continue;
168 }
169 if( *p == ':' )
170 {
171 if( p + 1 < buf + len )
172 {
173 cur = p + 1;
174 params[cnt++] = cur;
175 }
176 *p = '\0';
177 }
178
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100179 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200180 }
181
182 // Replace newlines, question marks and colons in strings
183 for( i = 0; i < cnt; i++ )
184 {
185 p = params[i];
186 q = params[i];
187
188 while( *p != '\0' )
189 {
190 if( *p == '\\' && *(p + 1) == 'n' )
191 {
192 p += 2;
193 *(q++) = '\n';
194 }
195 else if( *p == '\\' && *(p + 1) == ':' )
196 {
197 p += 2;
198 *(q++) = ':';
199 }
200 else if( *p == '\\' && *(p + 1) == '?' )
201 {
202 p += 2;
203 *(q++) = '?';
204 }
205 else
206 *(q++) = *(p++);
207 }
208 *q = '\0';
209 }
210
211 return( cnt );
212}
213
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200214static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
215{
216 int ret;
217 char buf[10] = "xxxxxxxxx";
218
219 ret = mbedtls_snprintf( buf, n, "%s", "123" );
220 if( ret < 0 || (size_t) ret >= n )
221 ret = -1;
222
223 if( memcmp( ref_buf, buf, sizeof buf ) != 0 ||
224 ref_ret != ret )
225 {
226 return( 1 );
227 }
228
229 return( 0 );
230}
231
232static int run_test_snprintf( void )
233{
234 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
235 test_snprintf( 1, "\0xxxxxxxx", -1 ) != 0 ||
236 test_snprintf( 2, "1\0xxxxxxx", -1 ) != 0 ||
237 test_snprintf( 3, "12\0xxxxxx", -1 ) != 0 ||
238 test_snprintf( 4, "123\0xxxxx", 3 ) != 0 ||
239 test_snprintf( 5, "123\0xxxxx", 3 ) != 0 );
240}
241
Paul Bakker19343182013-08-16 13:31:10 +0200242int main()
243{
244 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
245 const char *filename = "TEST_FILENAME";
246 FILE *file;
247 char buf[5000];
248 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200249 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100252 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200253 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200255#endif
256
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200257 /*
258 * The C standard doesn't guarantee that all-bits-0 is the representation
259 * of a NULL pointer. We do however use that in our code for initializing
260 * structures, which should work on every modern platform. Let's be sure.
261 */
262 memset( &pointer, 0, sizeof( void * ) );
263 if( pointer != NULL )
264 {
265 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
266 return( 1 );
267 }
268
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200269 /*
270 * Make sure we have a snprintf that correctly zero-terminates
271 */
272 if( run_test_snprintf() != 0 )
273 {
274 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
275 return( 0 );
276 }
277
Paul Bakker19343182013-08-16 13:31:10 +0200278 file = fopen( filename, "r" );
279 if( file == NULL )
280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_fprintf( stderr, "Failed to open\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200282 return( 1 );
283 }
284
285 while( !feof( file ) )
286 {
287 int skip = 0;
288
289 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
290 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
292 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200293 for( i = strlen( buf ) + 1; i < 67; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_fprintf( stdout, "." );
295 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200296 fflush( stdout );
297
298 total_tests++;
299
300 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
301 break;
302 cnt = parse_arguments( buf, strlen(buf), params );
303
304 if( strcmp( params[0], "depends_on" ) == 0 )
305 {
306 for( i = 1; i < cnt; i++ )
307 if( dep_check( params[i] ) != 0 )
308 skip = 1;
309
310 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
311 break;
312 cnt = parse_arguments( buf, strlen(buf), params );
313 }
314
315 if( skip == 0 )
316 {
317 test_errors = 0;
318 ret = dispatch_test( cnt, params );
319 }
320
321 if( skip == 1 || ret == 3 )
322 {
323 total_skipped++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_fprintf( stdout, "----\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200325 fflush( stdout );
326 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200327 else if( ret == 0 && test_errors == 0 )
Paul Bakker19343182013-08-16 13:31:10 +0200328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_fprintf( stdout, "PASS\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200330 fflush( stdout );
331 }
332 else if( ret == 2 )
333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200335 fclose(file);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_exit( 2 );
Paul Bakker19343182013-08-16 13:31:10 +0200337 }
338 else
339 total_errors++;
340
341 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
342 break;
343 if( strlen(buf) != 0 )
344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200346 return( 1 );
347 }
348 }
349 fclose(file);
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200352 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200354 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200358 total_tests - total_errors, total_tests, total_skipped );
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100361 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_MEMORY_DEBUG)
363 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200366#endif
Paul Bakker19343182013-08-16 13:31:10 +0200367
368 return( total_errors != 0 );
369}
370