blob: 7fee3d876bf11d164d838652e5761b8d0f756076 [file] [log] [blame]
Paul Bakkerde56ca12013-09-15 17:05:21 +02001SUITE_PRE_DEP
2#define TEST_SUITE_ACTIVE
3
Paul Bakker19343182013-08-16 13:31:10 +02004int verify_string( char **str )
5{
6 if( (*str)[0] != '"' ||
7 (*str)[strlen( *str ) - 1] != '"' )
8 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009 mbedtls_printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
Paul Bakker19343182013-08-16 13:31:10 +020010 return( -1 );
11 }
12
13 (*str)++;
14 (*str)[strlen( *str ) - 1] = '\0';
15
16 return( 0 );
17}
18
19int verify_int( char *str, int *value )
20{
21 size_t i;
22 int minus = 0;
23 int digits = 1;
24 int hex = 0;
25
26 for( i = 0; i < strlen( str ); i++ )
27 {
28 if( i == 0 && str[i] == '-' )
29 {
30 minus = 1;
31 continue;
32 }
33
34 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
35 str[i - 1] == '0' && str[i] == 'x' )
36 {
37 hex = 1;
38 continue;
39 }
40
Manuel Pégourié-Gonnard725afd82014-02-01 11:54:28 +010041 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
42 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
43 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
Paul Bakker19343182013-08-16 13:31:10 +020044 {
45 digits = 0;
46 break;
47 }
48 }
49
50 if( digits )
51 {
52 if( hex )
53 *value = strtol( str, NULL, 16 );
54 else
55 *value = strtol( str, NULL, 10 );
56
57 return( 0 );
58 }
59
60MAPPING_CODE
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_printf( "Expected integer for parameter and got: %s\n", str );
Paul Bakker19343182013-08-16 13:31:10 +020063 return( -1 );
64}
65
SimonB0284f582016-02-15 23:27:28 +000066
67/*----------------------------------------------------------------------------*/
68/* Test Case code */
69
Paul Bakkerde56ca12013-09-15 17:05:21 +020070FUNCTION_CODE
71SUITE_POST_DEP
72
SimonB0284f582016-02-15 23:27:28 +000073
74/*----------------------------------------------------------------------------*/
75/* Test dispatch code */
76
Paul Bakker19343182013-08-16 13:31:10 +020077int dep_check( char *str )
78{
79 if( str == NULL )
80 return( 1 );
81
82DEP_CHECK_CODE
83
84 return( 1 );
85}
86
Paul Bakker19343182013-08-16 13:31:10 +020087int dispatch_test(int cnt, char *params[50])
88{
89 int ret;
Paul Bakkerb34fef22013-08-20 12:06:33 +020090 ((void) cnt);
91 ((void) params);
Paul Bakker19343182013-08-16 13:31:10 +020092
Paul Bakkerb34fef22013-08-20 12:06:33 +020093#if defined(TEST_SUITE_ACTIVE)
Paul Bakker19343182013-08-16 13:31:10 +020094DISPATCH_FUNCTION
95 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
Paul Bakker19343182013-08-16 13:31:10 +020097 fflush( stdout );
98 return( 1 );
99 }
Paul Bakkerb34fef22013-08-20 12:06:33 +0200100#else
101 return( 3 );
102#endif
Paul Bakker19343182013-08-16 13:31:10 +0200103 return( ret );
104}
105
SimonB0284f582016-02-15 23:27:28 +0000106
107/*----------------------------------------------------------------------------*/
108/* Main Test code */
109
Gilles Peskinee38900b2017-09-29 15:45:12 +0200110/** Retrieve one input line into buf, which must have room for len
111 * bytes. The trailing line break (if any) is stripped from the result.
112 * Lines beginning with the character '#' are skipped. Lines that are
113 * more than len-1 bytes long including the trailing line break are
114 * truncated; note that the following bytes remain in the input stream.
115 *
116 * \return 0 on success, -1 on error or end of file
117 */
Paul Bakker19343182013-08-16 13:31:10 +0200118int get_line( FILE *f, char *buf, size_t len )
119{
120 char *ret;
121
Gilles Peskinee38900b2017-09-29 15:45:12 +0200122 do
123 {
124 ret = fgets( buf, len, f );
125 if( ret == NULL )
126 return( -1 );
127 }
128 while( buf[0] == '#' );
Paul Bakker19343182013-08-16 13:31:10 +0200129
Gilles Peskinee38900b2017-09-29 15:45:12 +0200130 ret = buf + strlen( buf );
131 if( ret-- > buf && *ret == '\n' )
132 *ret = '\0';
133 if( ret-- > buf && *ret == '\r' )
134 *ret = '\0';
Paul Bakker19343182013-08-16 13:31:10 +0200135
136 return( 0 );
137}
138
139int parse_arguments( char *buf, size_t len, char *params[50] )
140{
141 int cnt = 0, i;
142 char *cur = buf;
143 char *p = buf, *q;
144
145 params[cnt++] = cur;
146
147 while( *p != '\0' && p < buf + len )
148 {
149 if( *p == '\\' )
150 {
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100151 p++;
152 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200153 continue;
154 }
155 if( *p == ':' )
156 {
157 if( p + 1 < buf + len )
158 {
159 cur = p + 1;
160 params[cnt++] = cur;
161 }
162 *p = '\0';
163 }
164
Manuel Pégourié-Gonnard2d5f1422014-01-22 16:01:17 +0100165 p++;
Paul Bakker19343182013-08-16 13:31:10 +0200166 }
167
SimonB8bcd5492016-02-17 23:34:30 +0000168 /* Replace newlines, question marks and colons in strings */
Paul Bakker19343182013-08-16 13:31:10 +0200169 for( i = 0; i < cnt; i++ )
170 {
171 p = params[i];
172 q = params[i];
173
174 while( *p != '\0' )
175 {
176 if( *p == '\\' && *(p + 1) == 'n' )
177 {
178 p += 2;
179 *(q++) = '\n';
180 }
181 else if( *p == '\\' && *(p + 1) == ':' )
182 {
183 p += 2;
184 *(q++) = ':';
185 }
186 else if( *p == '\\' && *(p + 1) == '?' )
187 {
188 p += 2;
189 *(q++) = '?';
190 }
191 else
192 *(q++) = *(p++);
193 }
194 *q = '\0';
195 }
196
197 return( cnt );
198}
199
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200200static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
201{
202 int ret;
203 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200204 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200205
206 ret = mbedtls_snprintf( buf, n, "%s", "123" );
207 if( ret < 0 || (size_t) ret >= n )
208 ret = -1;
209
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200210 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
211 ref_ret != ret ||
212 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200213 {
214 return( 1 );
215 }
216
217 return( 0 );
218}
219
220static int run_test_snprintf( void )
221{
222 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200223 test_snprintf( 1, "", -1 ) != 0 ||
224 test_snprintf( 2, "1", -1 ) != 0 ||
225 test_snprintf( 3, "12", -1 ) != 0 ||
226 test_snprintf( 4, "123", 3 ) != 0 ||
227 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200228}
229
Paul Bakker19343182013-08-16 13:31:10 +0200230int main()
231{
232 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
233 const char *filename = "TEST_FILENAME";
234 FILE *file;
235 char buf[5000];
236 char *params[50];
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200237 void *pointer;
Paul Bakker19343182013-08-16 13:31:10 +0200238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100240 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Paul Bakker1337aff2013-09-29 14:45:34 +0200241 unsigned char alloc_buf[1000000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200243#endif
244
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200245 /*
246 * The C standard doesn't guarantee that all-bits-0 is the representation
247 * of a NULL pointer. We do however use that in our code for initializing
248 * structures, which should work on every modern platform. Let's be sure.
249 */
250 memset( &pointer, 0, sizeof( void * ) );
251 if( pointer != NULL )
252 {
253 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
254 return( 1 );
255 }
256
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200257 /*
258 * Make sure we have a snprintf that correctly zero-terminates
259 */
260 if( run_test_snprintf() != 0 )
261 {
262 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
263 return( 0 );
264 }
265
Paul Bakker19343182013-08-16 13:31:10 +0200266 file = fopen( filename, "r" );
267 if( file == NULL )
268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_fprintf( stderr, "Failed to open\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200270 return( 1 );
271 }
272
273 while( !feof( file ) )
274 {
275 int skip = 0;
276
277 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
278 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
280 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200281 for( i = strlen( buf ) + 1; i < 67; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_fprintf( stdout, "." );
283 mbedtls_fprintf( stdout, " " );
Paul Bakker19343182013-08-16 13:31:10 +0200284 fflush( stdout );
285
286 total_tests++;
287
288 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
289 break;
290 cnt = parse_arguments( buf, strlen(buf), params );
291
292 if( strcmp( params[0], "depends_on" ) == 0 )
293 {
294 for( i = 1; i < cnt; i++ )
295 if( dep_check( params[i] ) != 0 )
296 skip = 1;
297
298 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
299 break;
300 cnt = parse_arguments( buf, strlen(buf), params );
301 }
302
303 if( skip == 0 )
304 {
305 test_errors = 0;
306 ret = dispatch_test( cnt, params );
307 }
308
309 if( skip == 1 || ret == 3 )
310 {
311 total_skipped++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_fprintf( stdout, "----\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200313 fflush( stdout );
314 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200315 else if( ret == 0 && test_errors == 0 )
Paul Bakker19343182013-08-16 13:31:10 +0200316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_fprintf( stdout, "PASS\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200318 fflush( stdout );
319 }
320 else if( ret == 2 )
321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
Paul Bakker19343182013-08-16 13:31:10 +0200323 fclose(file);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_exit( 2 );
Paul Bakker19343182013-08-16 13:31:10 +0200325 }
326 else
327 total_errors++;
328
329 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
330 break;
331 if( strlen(buf) != 0 )
332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
Paul Bakker19343182013-08-16 13:31:10 +0200334 return( 1 );
335 }
336 }
337 fclose(file);
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
Paul Bakker19343182013-08-16 13:31:10 +0200340 if( total_errors == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_fprintf( stdout, "PASSED" );
Paul Bakker19343182013-08-16 13:31:10 +0200342 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_fprintf( stdout, "FAILED" );
Paul Bakker19343182013-08-16 13:31:10 +0200344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
Paul Bakker19343182013-08-16 13:31:10 +0200346 total_tests - total_errors, total_tests, total_skipped );
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
Manuel Pégourié-Gonnard765bb312014-11-27 11:55:27 +0100349 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#if defined(MBEDTLS_MEMORY_DEBUG)
351 mbedtls_memory_buffer_alloc_status();
Paul Bakker19343182013-08-16 13:31:10 +0200352#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_memory_buffer_alloc_free();
Paul Bakker1337aff2013-09-29 14:45:34 +0200354#endif
Paul Bakker19343182013-08-16 13:31:10 +0200355
356 return( total_errors != 0 );
357}
358