blob: 4248260b0b559c04e4888a114399b353df402b7c [file] [log] [blame]
Mohammad Azim Khan95402612017-07-19 10:15:54 +01001#line 2 "suites/target_test.function"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01002
Azim Khan3e5d0002017-06-05 13:16:10 +01003#include "greentea-client/test_env.h"
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01004
5/**
6 * \brief Increments pointer and asserts that it does not overflow.
7 *
8 * \param p Pointer to byte array
9 * \param start Pointer to start of byte array
10 * \param len Length of byte array
11 * \param step Increment size
12 *
13 */
14#define INCR_ASSERT(p, start, len, step) do \
15{ \
Azim Khand61a4382017-07-07 16:17:27 +010016 assert( ( p ) >= ( start ) ); \
17 assert( sizeof( *( p ) ) == sizeof( *( start ) ) ); \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010018 /* <= is checked to support use inside a loop where \
19 pointer is incremented after reading data. */ \
Azim Khan36e5fac2017-09-08 17:23:23 +010020 assert( (uint32_t)( ( ( p ) - ( start ) ) + ( step ) ) <= ( len ) );\
Azim Khan05d83fa2017-09-10 22:57:19 +010021 ( p ) += ( step ); \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010022} \
23while( 0 )
24
25
26/**
27 * \brief 4 byte align unsigned char pointer
28 *
29 * \param p Pointer to byte array
30 * \param start Pointer to start of byte array
31 * \param len Length of byte array
32 *
33 */
Mohammad Azim Khand2d01122018-07-18 17:48:37 +010034#define ALIGN_32BIT(p, start, len) do \
35{ \
36 uint32_t align = ( - (uintptr_t)( p ) ) % 4; \
37 INCR_ASSERT( ( p ), ( start ), ( len ), align );\
38} \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010039while( 0 )
40
41
42/**
43 * \brief Verify dependencies. Dependency identifiers are
44 * encoded in the buffer as 8 bit unsigned integers.
45 *
46 * \param count Number of dependencies.
47 * \param dep_p Pointer to buffer.
48 *
49 * \return DEPENDENCY_SUPPORTED if success else DEPENDENCY_NOT_SUPPORTED.
50 */
51int verify_dependencies( uint8_t count, uint8_t * dep_p )
52{
53 uint8_t i;
54 for ( i = 0; i < count; i++ )
55 {
56 if ( dep_check( (int)(dep_p[i]) ) != DEPENDENCY_SUPPORTED )
57 return( DEPENDENCY_NOT_SUPPORTED );
58 }
59 return( DEPENDENCY_SUPPORTED );
60}
61
Ron Eldor5075f4d2019-06-03 11:38:42 +030062/**
63 * \brief Receives hex string on serial interface, and converts to a byte.
64 *
65 * \param none
66 *
67 * \return unsigned int8
68 */
69uint8_t receive_byte()
70{
71 uint8_t byte;
72 uint8_t c;
73
74 c = greentea_getc();
75 if( c >= '0' && c <= '9' )
76 c -= '0';
77 else if( c >= 'a' && c <= 'f' )
78 c = ( c -'a' ) + 10;
79 else if( c >= 'A' && c <= 'F' )
80 c = ( c - 'A' ) + 10;
81
82 byte = c * 0x10;
83
84 c = greentea_getc();
85 if( c >= '0' && c <= '9' )
86 c -= '0';
87 else if( c >= 'a' && c <= 'f' )
88 c = ( c -'a' ) + 10;
89 else if( c >= 'A' && c <= 'F' )
90 c = ( c - 'A' ) + 10;
91
92 byte += c ;
93 return( byte);
94}
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010095
96/**
97 * \brief Receives unsigned integer on serial interface.
Ron Eldor5075f4d2019-06-03 11:38:42 +030098 * Integers are encoded in network order, and sent as hex ascii string.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010099 *
100 * \param none
101 *
102 * \return unsigned int
103 */
104uint32_t receive_uint32()
105{
106 uint32_t value;
Ron Eldor5075f4d2019-06-03 11:38:42 +0300107 value = receive_byte() << 24;
108 value |= receive_byte() << 16;
109 value |= receive_byte() << 8;
110 value |= receive_byte();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100111 return( (uint32_t)value );
112}
113
114/**
115 * \brief Parses out an unsigned 32 int value from the byte array.
116 * Integers are encoded in network order.
117 *
118 * \param p Pointer to byte array
119 *
120 * \return unsigned int
121 */
122uint32_t parse_uint32( uint8_t * p )
123{
124 uint32_t value;
125 value = *p++ << 24;
126 value |= *p++ << 16;
127 value |= *p++ << 8;
128 value |= *p;
129 return( value );
130}
131
132
133/**
134 * \brief Receives test data on serial as greentea key,value pair:
135 * {{<length>;<byte array>}}
136 *
137 * \param data_len Out pointer to hold received data length.
138 *
139 * \return Byte array.
140 */
141uint8_t * receive_data( uint32_t * data_len )
142{
143 uint32_t i = 0, errors = 0;
144 char c;
145 uint8_t * data = NULL;
146
147 /* Read opening braces */
148 i = 0;
149 while ( i < 2 )
150 {
151 c = greentea_getc();
152 /* Ignore any prevous CR LF characters */
153 if ( c == '\n' || c == '\r' )
154 continue;
155 i++;
156 if ( c != '{' )
157 return( NULL );
158 }
159
160 /* Read data length */
161 *data_len = receive_uint32();
162 data = (uint8_t *)malloc( *data_len );
163 assert( data != NULL );
164
165 greentea_getc(); // read ';' received after key i.e. *data_len
166
167 for( i = 0; i < *data_len; i++ )
Ron Eldor5075f4d2019-06-03 11:38:42 +0300168 data[i] = receive_byte();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100169
170 /* Read closing braces */
171 for( i = 0; i < 2; i++ )
172 {
173 c = greentea_getc();
174 if ( c != '}' )
175 {
176 errors++;
177 break;
178 }
179 }
180
181 if ( errors )
182 {
183 free( data );
184 data = NULL;
185 *data_len = 0;
186 }
187
188 return( data );
189}
190
191/**
Azim Khan05d83fa2017-09-10 22:57:19 +0100192 * \brief Parse the received byte array and count the number of arguments
193 * to the test function passed as type hex.
Azim Khand59391a2017-06-01 14:04:17 +0100194 *
195 * \param count Parameter count
196 * \param data Received Byte array
197 * \param data_len Byte array length
198 *
199 * \return count of hex params
200 */
201uint32_t find_hex_count( uint8_t count, uint8_t * data, uint32_t data_len )
202{
203 uint32_t i = 0, sz = 0;
204 char c;
205 uint8_t * p = NULL;
206 uint32_t hex_count = 0;
207
208 p = data;
209
210 for( i = 0; i < count; i++ )
211 {
212 c = (char)*p;
213 INCR_ASSERT( p, data, data_len, 1 );
214
215 /* Align p to 4 bytes for int, expression, string len or hex length */
216 ALIGN_32BIT( p, data, data_len );
217
218 /* Network to host conversion */
219 sz = (int32_t)parse_uint32( p );
220
221 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
222
223 if ( c == 'H' || c == 'S' )
224 {
225 INCR_ASSERT( p, data, data_len, sz );
226 hex_count += ( c == 'H' )?1:0;
227 }
228 }
229
230 return( hex_count );
231}
232
233/**
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100234 * \brief Parses received byte array for test parameters.
235 *
236 * \param count Parameter count
237 * \param data Received Byte array
238 * \param data_len Byte array length
239 * \param error Parsing error out variable.
240 *
241 * \return Array of parsed parameters allocated on heap.
242 * Note: Caller has the responsibility to delete
243 * the memory after use.
244 */
245void ** parse_parameters( uint8_t count, uint8_t * data, uint32_t data_len,
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100246 int * error )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100247{
Azim Khand59391a2017-06-01 14:04:17 +0100248 uint32_t i = 0, hex_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100249 char c;
250 void ** params = NULL;
251 void ** cur = NULL;
252 uint8_t * p = NULL;
253
Azim Khand59391a2017-06-01 14:04:17 +0100254 hex_count = find_hex_count(count, data, data_len);
255
256 params = (void **)malloc( sizeof( void *) * ( count + hex_count ) );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100257 assert( params != NULL );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100258 cur = params;
259
260 p = data;
261
262 /* Parameters */
263 for( i = 0; i < count; i++ )
264 {
265 c = (char)*p;
266 INCR_ASSERT( p, data, data_len, 1 );
267
268 /* Align p to 4 bytes for int, expression, string len or hex length */
269 ALIGN_32BIT( p, data, data_len );
270
271 /* Network to host conversion */
272 *( (int32_t *)p ) = (int32_t)parse_uint32( p );
273
274 switch( c )
275 {
276 case 'E':
277 {
278 if ( get_expression( *( (int32_t *)p ), (int32_t *)p ) )
279 {
280 *error = KEY_VALUE_MAPPING_NOT_FOUND;
281 goto exit;
282 }
283 } /* Intentional fall through */
284 case 'I':
285 {
286 *cur++ = (void *)p;
287 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
288 }
289 break;
Azim Khand59391a2017-06-01 14:04:17 +0100290 case 'H': /* Intentional fall through */
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100291 case 'S':
292 {
Azim Khand59391a2017-06-01 14:04:17 +0100293 uint32_t * sz = (uint32_t *)p;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100294 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
295 *cur++ = (void *)p;
Azim Khand59391a2017-06-01 14:04:17 +0100296 if ( c == 'H' )
297 *cur++ = (void *)sz;
298 INCR_ASSERT( p, data, data_len, ( *sz ) );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100299 }
300 break;
301 default:
302 {
303 *error = DISPATCH_INVALID_TEST_DATA;
304 goto exit;
305 }
306 break;
307 }
308 }
309
310exit:
311 if ( *error )
312 {
313 free( params );
314 params = NULL;
315 }
316
317 return( params );
318}
319
320/**
321 * \brief Sends greentea key and int value pair to host.
322 *
323 * \param key key string
324 * \param value integer value
325 *
326 * \return void
327 */
328void send_key_integer( char * key, int value )
329{
330 char str[50];
331 snprintf( str, sizeof( str ), "%d", value );
Azim Khan05746322017-05-23 13:00:35 +0100332 greentea_send_kv( key, str );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100333}
334
335/**
336 * \brief Sends test setup failure to the host.
337 *
338 * \param failure Test set failure
339 *
340 * \return void
341 */
342void send_failure( int failure )
343{
344 send_key_integer( "F", failure );
345}
346
347/**
348 * \brief Sends test status to the host.
349 *
350 * \param status Test status (PASS=0/FAIL=!0)
351 *
352 * \return void
353 */
354void send_status( int status )
355{
356 send_key_integer( "R", status );
357}
358
359
360/**
361 * \brief Embedded implementation of execute_tests().
362 * Ignores command line and received test data
363 * on serial.
364 *
365 * \param argc not used
366 * \param argv not used
367 *
368 * \return Program exit status.
369 */
370int execute_tests( int args, const char ** argv )
371{
372 int ret = 0;
373 uint32_t data_len = 0;
374 uint8_t count = 0, function_id;
375 void ** params = NULL;
376 uint8_t * data = NULL, * p = NULL;
377
Azim Khan05746322017-05-23 13:00:35 +0100378 GREENTEA_SETUP( 180, "mbedtls_test" );
379 greentea_send_kv( "GO", " " );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100380
381 while ( 1 )
382 {
383 ret = 0;
384 test_info.failed = 0;
385 data_len = 0;
386
387 data = receive_data( &data_len );
388 if ( data == NULL )
389 continue;
390 p = data;
391
392 do
393 {
394 /* Read dependency count */
395 count = *p;
396 assert( count < data_len );
397 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
398 ret = verify_dependencies( count, p );
399 if ( ret != DEPENDENCY_SUPPORTED )
400 break;
401
Azim Khand59391a2017-06-01 14:04:17 +0100402 if ( count )
403 INCR_ASSERT( p, data, data_len, count );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100404
405 /* Read function id */
406 function_id = *p;
407 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
Azim Khan13c6bfb2017-06-15 14:45:56 +0100408 if ( ( ret = check_test( function_id ) ) != DISPATCH_TEST_SUCCESS )
409 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100410
411 /* Read number of parameters */
412 count = *p;
413 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
414
Azim Khand59391a2017-06-01 14:04:17 +0100415 /* Parse parameters if present */
416 if ( count )
417 {
418 params = parse_parameters( count, p, data_len - ( p - data ), &ret );
419 if ( ret )
420 break;
421 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100422
423 ret = dispatch_test( function_id, params );
424 }
425 while ( 0 );
426
427 if ( data )
428 {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100429 free( data );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100430 data = NULL;
431 }
432
433 if ( params )
434 {
435 free( params );
436 params = NULL;
437 }
438
439 if ( ret )
440 send_failure( ret );
441 else
442 send_status( test_info.failed );
443 }
444 return( 0 );
445}
446