blob: 948f72e11f16d3896db3ddf695d27f80765f6d73 [file] [log] [blame]
Paul Bakkerde56ca12013-09-15 17:05:21 +02001SUITE_PRE_DEP
Gilles Peskinebe807262018-03-13 16:07:01 +01002#line !LINE_NO! "main_test.function"
Paul Bakkerde56ca12013-09-15 17:05:21 +02003#define TEST_SUITE_ACTIVE
4
Paul Bakker19343182013-08-16 13:31:10 +02005int verify_string( char **str )
6{
7 if( (*str)[0] != '"' ||
8 (*str)[strlen( *str ) - 1] != '"' )
9 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010 mbedtls_printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
Paul Bakker19343182013-08-16 13:31:10 +020011 return( -1 );
12 }
13
14 (*str)++;
15 (*str)[strlen( *str ) - 1] = '\0';
16
17 return( 0 );
18}
19
20int verify_int( char *str, int *value )
21{
22 size_t i;
23 int minus = 0;
24 int digits = 1;
25 int hex = 0;
26
27 for( i = 0; i < strlen( str ); i++ )
28 {
29 if( i == 0 && str[i] == '-' )
30 {
31 minus = 1;
32 continue;
33 }
34
35 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
36 str[i - 1] == '0' && str[i] == 'x' )
37 {
38 hex = 1;
39 continue;
40 }
41
Manuel Pégourié-Gonnard725afd82014-02-01 11:54:28 +010042 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
43 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
44 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
Paul Bakker19343182013-08-16 13:31:10 +020045 {
46 digits = 0;
47 break;
48 }
49 }
50
51 if( digits )
52 {
53 if( hex )
54 *value = strtol( str, NULL, 16 );
55 else
56 *value = strtol( str, NULL, 10 );
57
58 return( 0 );
59 }
60
61MAPPING_CODE
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_printf( "Expected integer for parameter and got: %s\n", str );
Paul Bakker19343182013-08-16 13:31:10 +020064 return( -1 );
65}
66
SimonB0284f582016-02-15 23:27:28 +000067
68/*----------------------------------------------------------------------------*/
69/* Test Case code */
70
Paul Bakkerde56ca12013-09-15 17:05:21 +020071FUNCTION_CODE
72SUITE_POST_DEP
Gilles Peskinebe807262018-03-13 16:07:01 +010073#line !LINE_NO! "main_test.function"
Paul Bakkerde56ca12013-09-15 17:05:21 +020074
SimonB0284f582016-02-15 23:27:28 +000075
76/*----------------------------------------------------------------------------*/
77/* Test dispatch code */
78
Paul Bakker19343182013-08-16 13:31:10 +020079int dep_check( char *str )
80{
81 if( str == NULL )
82 return( 1 );
83
84DEP_CHECK_CODE
Gilles Peskinebe807262018-03-13 16:07:01 +010085#line !LINE_NO! "main_test.function"
Paul Bakker19343182013-08-16 13:31:10 +020086
87 return( 1 );
88}
89
Paul Bakker19343182013-08-16 13:31:10 +020090int dispatch_test(int cnt, char *params[50])
91{
92 int ret;
Paul Bakkerb34fef22013-08-20 12:06:33 +020093 ((void) cnt);
94 ((void) params);
Paul Bakker19343182013-08-16 13:31:10 +020095
Paul Bakkerb34fef22013-08-20 12:06:33 +020096#if defined(TEST_SUITE_ACTIVE)
Paul Bakker19343182013-08-16 13:31:10 +020097DISPATCH_FUNCTION
98 {
Gilles Peskinebe807262018-03-13 16:07:01 +010099#line !LINE_NO! "main_test.function"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
Paul Bakker19343182013-08-16 13:31:10 +0200101 fflush( stdout );
102 return( 1 );
103 }
Paul Bakkerb34fef22013-08-20 12:06:33 +0200104#else
105 return( 3 );
106#endif
Paul Bakker19343182013-08-16 13:31:10 +0200107 return( ret );
108}
109
SimonB0284f582016-02-15 23:27:28 +0000110
111/*----------------------------------------------------------------------------*/
112/* Main Test code */
113
Gilles Peskinee38900b2017-09-29 15:45:12 +0200114/** Retrieve one input line into buf, which must have room for len
115 * bytes. The trailing line break (if any) is stripped from the result.
116 * Lines beginning with the character '#' are skipped. Lines that are
117 * more than len-1 bytes long including the trailing line break are
118 * truncated; note that the following bytes remain in the input stream.
119 *
120 * \return 0 on success, -1 on error or end of file
121 */
Paul Bakker19343182013-08-16 13:31:10 +0200122int get_line( FILE *f, char *buf, size_t len )
123{
124 char *ret;
125
Gilles Peskinee38900b2017-09-29 15:45:12 +0200126 do
127 {
128 ret = fgets( buf, len, f );
129 if( ret == NULL )
130 return( -1 );
131 }
132 while( buf[0] == '#' );
Paul Bakker19343182013-08-16 13:31:10 +0200133
Gilles Peskinee38900b2017-09-29 15:45:12 +0200134 ret = buf + strlen( buf );
135 if( ret-- > buf && *ret == '\n' )
136 *ret = '\0';
137 if( ret-- > buf && *ret == '\r' )
138 *ret = '\0';
Paul Bakker19343182013-08-16 13:31:10 +0200139
140 return( 0 );
141}
142
143int parse_arguments( char *buf, size_t len, char *params[50] )
144{
145 int cnt = 0, i;
146 char *cur = buf;
147 char *p = buf, *q;
148
149 params[cnt++] = cur;
150
151 while( *p != '\0' && p < buf + len )
152 {
153 if( *p == '\\' )
154 {
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100155 p++;
156 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200157 continue;
158 }
159 if( *p == ':' )
160 {
161 if( p + 1 < buf + len )
162 {
163 cur = p + 1;
164 params[cnt++] = cur;
165 }
166 *p = '\0';
167 }
168
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100169 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200170 }
171
SimonB8bcd5492016-02-17 23:34:30 +0000172 /* Replace newlines, question marks and colons in strings */
Paul Bakker19343182013-08-16 13:31:10 +0200173 for( i = 0; i < cnt; i++ )
174 {
175 p = params[i];
176 q = params[i];
177
178 while( *p != '\0' )
179 {
180 if( *p == '\\' && *(p + 1) == 'n' )
181 {
182 p += 2;
183 *(q++) = '\n';
184 }
185 else if( *p == '\\' && *(p + 1) == ':' )
186 {
187 p += 2;
188 *(q++) = ':';
189 }
190 else if( *p == '\\' && *(p + 1) == '?' )
191 {
192 p += 2;
193 *(q++) = '?';
194 }
195 else
196 *(q++) = *(p++);
197 }
198 *q = '\0';
199 }
200
201 return( cnt );
202}
203
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200204static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
205{
206 int ret;
207 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200208 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200209
210 ret = mbedtls_snprintf( buf, n, "%s", "123" );
211 if( ret < 0 || (size_t) ret >= n )
212 ret = -1;
213
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200214 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
215 ref_ret != ret ||
216 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200217 {
218 return( 1 );
219 }
220
221 return( 0 );
222}
223
224static int run_test_snprintf( void )
225{
226 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200227 test_snprintf( 1, "", -1 ) != 0 ||
228 test_snprintf( 2, "1", -1 ) != 0 ||
229 test_snprintf( 3, "12", -1 ) != 0 ||
230 test_snprintf( 4, "123", 3 ) != 0 ||
231 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200232}
233
Paul Bakker19343182013-08-16 13:31:10 +0200234int main()
235{
236 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
237 const char *filename = "TEST_FILENAME";
238 FILE *file;
239 char buf[5000];
240 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200241 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100244 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200245 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200247#endif
248
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200249 /*
250 * The C standard doesn't guarantee that all-bits-0 is the representation
251 * of a NULL pointer. We do however use that in our code for initializing
252 * structures, which should work on every modern platform. Let's be sure.
253 */
254 memset( &pointer, 0, sizeof( void * ) );
255 if( pointer != NULL )
256 {
257 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
258 return( 1 );
259 }
260
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200261 /*
262 * Make sure we have a snprintf that correctly zero-terminates
263 */
264 if( run_test_snprintf() != 0 )
265 {
266 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
267 return( 0 );
268 }
269
Paul Bakker19343182013-08-16 13:31:10 +0200270 file = fopen( filename, "r" );
271 if( file == NULL )
272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_fprintf( stderr, "Failed to open\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200274 return( 1 );
275 }
276
277 while( !feof( file ) )
278 {
279 int skip = 0;
280
281 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
282 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
284 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200285 for( i = strlen( buf ) + 1; i < 67; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_fprintf( stdout, "." );
287 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200288 fflush( stdout );
289
290 total_tests++;
291
292 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
293 break;
294 cnt = parse_arguments( buf, strlen(buf), params );
295
296 if( strcmp( params[0], "depends_on" ) == 0 )
297 {
298 for( i = 1; i < cnt; i++ )
299 if( dep_check( params[i] ) != 0 )
300 skip = 1;
301
302 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
303 break;
304 cnt = parse_arguments( buf, strlen(buf), params );
305 }
306
307 if( skip == 0 )
308 {
309 test_errors = 0;
310 ret = dispatch_test( cnt, params );
311 }
312
313 if( skip == 1 || ret == 3 )
314 {
315 total_skipped++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_fprintf( stdout, "----\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200317 fflush( stdout );
318 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200319 else if( ret == 0 && test_errors == 0 )
Paul Bakker19343182013-08-16 13:31:10 +0200320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_fprintf( stdout, "PASS\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200322 fflush( stdout );
323 }
324 else if( ret == 2 )
325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200327 fclose(file);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_exit( 2 );
Paul Bakker19343182013-08-16 13:31:10 +0200329 }
330 else
331 total_errors++;
332
333 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
334 break;
335 if( strlen(buf) != 0 )
336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200338 return( 1 );
339 }
340 }
341 fclose(file);
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200344 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200346 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200350 total_tests - total_errors, total_tests, total_skipped );
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100353 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_MEMORY_DEBUG)
355 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200356#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200358#endif
Paul Bakker19343182013-08-16 13:31:10 +0200359
360 return( total_errors != 0 );
361}
362