blob: f1ef9175c351a46fc9a92bc5201f539a5ed8442b [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02007#define mbedtls_exit exit
8#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#define mbedtls_fprintf fprintf
11#define mbedtls_printf printf
Rich Evans77d36382015-01-30 12:12:11 +000012#endif
13
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/memory_buffer_alloc.h"
Manuel Pégourié-Gonnard0ac1d2d2015-01-26 14:58:04 +010016#endif
17
Paul Bakker19343182013-08-16 13:31:10 +020018static int test_errors = 0;
19
Paul Bakkerde56ca12013-09-15 17:05:21 +020020SUITE_PRE_DEP
21#define TEST_SUITE_ACTIVE
22
Paul Bakker8fc30b12013-11-25 13:29:43 +010023static int test_assert( int correct, const char *test )
Paul Bakker19343182013-08-16 13:31:10 +020024{
25 if( correct )
26 return( 0 );
27
28 test_errors++;
Paul Bakker55a7e902013-08-19 14:02:10 +020029 if( test_errors == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 mbedtls_printf( "FAILED\n" );
31 mbedtls_printf( " %s\n", test );
Paul Bakker19343182013-08-16 13:31:10 +020032
33 return( 1 );
34}
35
Paul Bakkerbb20f4b2013-08-20 12:41:33 +020036#define TEST_ASSERT( TEST ) \
37 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
Paul Bakker318d0fe2014-07-10 14:59:25 +020038 if( test_errors) goto exit; \
Paul Bakkerbb20f4b2013-08-20 12:41:33 +020039 } while (0)
Paul Bakker19343182013-08-16 13:31:10 +020040
41int verify_string( char **str )
42{
43 if( (*str)[0] != '"' ||
44 (*str)[strlen( *str ) - 1] != '"' )
45 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
Paul Bakker19343182013-08-16 13:31:10 +020047 return( -1 );
48 }
49
50 (*str)++;
51 (*str)[strlen( *str ) - 1] = '\0';
52
53 return( 0 );
54}
55
56int verify_int( char *str, int *value )
57{
58 size_t i;
59 int minus = 0;
60 int digits = 1;
61 int hex = 0;
62
63 for( i = 0; i < strlen( str ); i++ )
64 {
65 if( i == 0 && str[i] == '-' )
66 {
67 minus = 1;
68 continue;
69 }
70
71 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
72 str[i - 1] == '0' && str[i] == 'x' )
73 {
74 hex = 1;
75 continue;
76 }
77
Manuel Pégourié-Gonnard725afd82014-02-01 11:54:28 +010078 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
79 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
80 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
Paul Bakker19343182013-08-16 13:31:10 +020081 {
82 digits = 0;
83 break;
84 }
85 }
86
87 if( digits )
88 {
89 if( hex )
90 *value = strtol( str, NULL, 16 );
91 else
92 *value = strtol( str, NULL, 10 );
93
94 return( 0 );
95 }
96
97MAPPING_CODE
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_printf( "Expected integer for parameter and got: %s\n", str );
Paul Bakker19343182013-08-16 13:31:10 +0200100 return( -1 );
101}
102
Paul Bakkerde56ca12013-09-15 17:05:21 +0200103FUNCTION_CODE
104SUITE_POST_DEP
105
Paul Bakker19343182013-08-16 13:31:10 +0200106int dep_check( char *str )
107{
108 if( str == NULL )
109 return( 1 );
110
111DEP_CHECK_CODE
112
113 return( 1 );
114}
115
Paul Bakker19343182013-08-16 13:31:10 +0200116int dispatch_test(int cnt, char *params[50])
117{
118 int ret;
Paul Bakkerb34fef22013-08-20 12:06:33 +0200119 ((void) cnt);
120 ((void) params);
Paul Bakker19343182013-08-16 13:31:10 +0200121
Paul Bakkerb34fef22013-08-20 12:06:33 +0200122#if defined(TEST_SUITE_ACTIVE)
Paul Bakker19343182013-08-16 13:31:10 +0200123DISPATCH_FUNCTION
124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
Paul Bakker19343182013-08-16 13:31:10 +0200126 fflush( stdout );
127 return( 1 );
128 }
Paul Bakkerb34fef22013-08-20 12:06:33 +0200129#else
130 return( 3 );
131#endif
Paul Bakker19343182013-08-16 13:31:10 +0200132 return( ret );
133}
134
135int get_line( FILE *f, char *buf, size_t len )
136{
137 char *ret;
138
139 ret = fgets( buf, len, f );
140 if( ret == NULL )
141 return( -1 );
142
143 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
144 buf[strlen(buf) - 1] = '\0';
145 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
146 buf[strlen(buf) - 1] = '\0';
147
148 return( 0 );
149}
150
151int parse_arguments( char *buf, size_t len, char *params[50] )
152{
153 int cnt = 0, i;
154 char *cur = buf;
155 char *p = buf, *q;
156
157 params[cnt++] = cur;
158
159 while( *p != '\0' && p < buf + len )
160 {
161 if( *p == '\\' )
162 {
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100163 p++;
164 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200165 continue;
166 }
167 if( *p == ':' )
168 {
169 if( p + 1 < buf + len )
170 {
171 cur = p + 1;
172 params[cnt++] = cur;
173 }
174 *p = '\0';
175 }
176
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100177 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200178 }
179
180 // Replace newlines, question marks and colons in strings
181 for( i = 0; i < cnt; i++ )
182 {
183 p = params[i];
184 q = params[i];
185
186 while( *p != '\0' )
187 {
188 if( *p == '\\' && *(p + 1) == 'n' )
189 {
190 p += 2;
191 *(q++) = '\n';
192 }
193 else if( *p == '\\' && *(p + 1) == ':' )
194 {
195 p += 2;
196 *(q++) = ':';
197 }
198 else if( *p == '\\' && *(p + 1) == '?' )
199 {
200 p += 2;
201 *(q++) = '?';
202 }
203 else
204 *(q++) = *(p++);
205 }
206 *q = '\0';
207 }
208
209 return( cnt );
210}
211
212int main()
213{
214 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
215 const char *filename = "TEST_FILENAME";
216 FILE *file;
217 char buf[5000];
218 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200219 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100222 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200223 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200225#endif
226
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200227 /*
228 * The C standard doesn't guarantee that all-bits-0 is the representation
229 * of a NULL pointer. We do however use that in our code for initializing
230 * structures, which should work on every modern platform. Let's be sure.
231 */
232 memset( &pointer, 0, sizeof( void * ) );
233 if( pointer != NULL )
234 {
235 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
236 return( 1 );
237 }
238
Paul Bakker19343182013-08-16 13:31:10 +0200239 file = fopen( filename, "r" );
240 if( file == NULL )
241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_fprintf( stderr, "Failed to open\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200243 return( 1 );
244 }
245
246 while( !feof( file ) )
247 {
248 int skip = 0;
249
250 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
251 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
253 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200254 for( i = strlen( buf ) + 1; i < 67; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_fprintf( stdout, "." );
256 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200257 fflush( stdout );
258
259 total_tests++;
260
261 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
262 break;
263 cnt = parse_arguments( buf, strlen(buf), params );
264
265 if( strcmp( params[0], "depends_on" ) == 0 )
266 {
267 for( i = 1; i < cnt; i++ )
268 if( dep_check( params[i] ) != 0 )
269 skip = 1;
270
271 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
272 break;
273 cnt = parse_arguments( buf, strlen(buf), params );
274 }
275
276 if( skip == 0 )
277 {
278 test_errors = 0;
279 ret = dispatch_test( cnt, params );
280 }
281
282 if( skip == 1 || ret == 3 )
283 {
284 total_skipped++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_fprintf( stdout, "----\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200286 fflush( stdout );
287 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200288 else if( ret == 0 && test_errors == 0 )
Paul Bakker19343182013-08-16 13:31:10 +0200289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_fprintf( stdout, "PASS\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200291 fflush( stdout );
292 }
293 else if( ret == 2 )
294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200296 fclose(file);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_exit( 2 );
Paul Bakker19343182013-08-16 13:31:10 +0200298 }
299 else
300 total_errors++;
301
302 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
303 break;
304 if( strlen(buf) != 0 )
305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200307 return( 1 );
308 }
309 }
310 fclose(file);
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200313 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200315 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200319 total_tests - total_errors, total_tests, total_skipped );
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100322 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_MEMORY_DEBUG)
324 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200327#endif
Paul Bakker19343182013-08-16 13:31:10 +0200328
329 return( total_errors != 0 );
330}
331