blob: 4522fbf0ce65b0199c763ed58132660cc3f222b4 [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;
Ron Eldorb2204892019-06-03 16:39:59 +030072 uint8_t c[3];
73 char *endptr;
74 c[0] = greentea_getc();
75 c[1] = greentea_getc();
76 c[2] = '\0';
Ron Eldor5075f4d2019-06-03 11:38:42 +030077
Ron Eldorb2204892019-06-03 16:39:59 +030078 assert( unhexify( &byte, &c ) != 2 );
79 return( byte );
Ron Eldor5075f4d2019-06-03 11:38:42 +030080}
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010081
82/**
83 * \brief Receives unsigned integer on serial interface.
Ron Eldor5075f4d2019-06-03 11:38:42 +030084 * Integers are encoded in network order, and sent as hex ascii string.
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010085 *
86 * \param none
87 *
88 * \return unsigned int
89 */
90uint32_t receive_uint32()
91{
92 uint32_t value;
Ron Eldor5075f4d2019-06-03 11:38:42 +030093 value = receive_byte() << 24;
94 value |= receive_byte() << 16;
95 value |= receive_byte() << 8;
96 value |= receive_byte();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +010097 return( (uint32_t)value );
98}
99
100/**
101 * \brief Parses out an unsigned 32 int value from the byte array.
102 * Integers are encoded in network order.
103 *
104 * \param p Pointer to byte array
105 *
106 * \return unsigned int
107 */
108uint32_t parse_uint32( uint8_t * p )
109{
110 uint32_t value;
111 value = *p++ << 24;
112 value |= *p++ << 16;
113 value |= *p++ << 8;
114 value |= *p;
115 return( value );
116}
117
118
119/**
120 * \brief Receives test data on serial as greentea key,value pair:
121 * {{<length>;<byte array>}}
122 *
123 * \param data_len Out pointer to hold received data length.
124 *
125 * \return Byte array.
126 */
127uint8_t * receive_data( uint32_t * data_len )
128{
129 uint32_t i = 0, errors = 0;
130 char c;
131 uint8_t * data = NULL;
132
133 /* Read opening braces */
134 i = 0;
135 while ( i < 2 )
136 {
137 c = greentea_getc();
138 /* Ignore any prevous CR LF characters */
139 if ( c == '\n' || c == '\r' )
140 continue;
141 i++;
142 if ( c != '{' )
143 return( NULL );
144 }
145
146 /* Read data length */
147 *data_len = receive_uint32();
148 data = (uint8_t *)malloc( *data_len );
149 assert( data != NULL );
150
151 greentea_getc(); // read ';' received after key i.e. *data_len
152
153 for( i = 0; i < *data_len; i++ )
Ron Eldor5075f4d2019-06-03 11:38:42 +0300154 data[i] = receive_byte();
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100155
156 /* Read closing braces */
157 for( i = 0; i < 2; i++ )
158 {
159 c = greentea_getc();
160 if ( c != '}' )
161 {
162 errors++;
163 break;
164 }
165 }
166
167 if ( errors )
168 {
169 free( data );
170 data = NULL;
171 *data_len = 0;
172 }
173
174 return( data );
175}
176
177/**
Azim Khan05d83fa2017-09-10 22:57:19 +0100178 * \brief Parse the received byte array and count the number of arguments
179 * to the test function passed as type hex.
Azim Khand59391a2017-06-01 14:04:17 +0100180 *
181 * \param count Parameter count
182 * \param data Received Byte array
183 * \param data_len Byte array length
184 *
185 * \return count of hex params
186 */
187uint32_t find_hex_count( uint8_t count, uint8_t * data, uint32_t data_len )
188{
189 uint32_t i = 0, sz = 0;
190 char c;
191 uint8_t * p = NULL;
192 uint32_t hex_count = 0;
193
194 p = data;
195
196 for( i = 0; i < count; i++ )
197 {
198 c = (char)*p;
199 INCR_ASSERT( p, data, data_len, 1 );
200
201 /* Align p to 4 bytes for int, expression, string len or hex length */
202 ALIGN_32BIT( p, data, data_len );
203
204 /* Network to host conversion */
205 sz = (int32_t)parse_uint32( p );
206
207 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
208
209 if ( c == 'H' || c == 'S' )
210 {
211 INCR_ASSERT( p, data, data_len, sz );
212 hex_count += ( c == 'H' )?1:0;
213 }
214 }
215
216 return( hex_count );
217}
218
219/**
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100220 * \brief Parses received byte array for test parameters.
221 *
222 * \param count Parameter count
223 * \param data Received Byte array
224 * \param data_len Byte array length
225 * \param error Parsing error out variable.
226 *
227 * \return Array of parsed parameters allocated on heap.
228 * Note: Caller has the responsibility to delete
229 * the memory after use.
230 */
231void ** parse_parameters( uint8_t count, uint8_t * data, uint32_t data_len,
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100232 int * error )
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100233{
Azim Khand59391a2017-06-01 14:04:17 +0100234 uint32_t i = 0, hex_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100235 char c;
236 void ** params = NULL;
237 void ** cur = NULL;
238 uint8_t * p = NULL;
239
Azim Khand59391a2017-06-01 14:04:17 +0100240 hex_count = find_hex_count(count, data, data_len);
241
242 params = (void **)malloc( sizeof( void *) * ( count + hex_count ) );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100243 assert( params != NULL );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100244 cur = params;
245
246 p = data;
247
248 /* Parameters */
249 for( i = 0; i < count; i++ )
250 {
251 c = (char)*p;
252 INCR_ASSERT( p, data, data_len, 1 );
253
254 /* Align p to 4 bytes for int, expression, string len or hex length */
255 ALIGN_32BIT( p, data, data_len );
256
257 /* Network to host conversion */
258 *( (int32_t *)p ) = (int32_t)parse_uint32( p );
259
260 switch( c )
261 {
262 case 'E':
263 {
264 if ( get_expression( *( (int32_t *)p ), (int32_t *)p ) )
265 {
266 *error = KEY_VALUE_MAPPING_NOT_FOUND;
267 goto exit;
268 }
269 } /* Intentional fall through */
270 case 'I':
271 {
272 *cur++ = (void *)p;
273 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
274 }
275 break;
Azim Khand59391a2017-06-01 14:04:17 +0100276 case 'H': /* Intentional fall through */
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100277 case 'S':
278 {
Azim Khand59391a2017-06-01 14:04:17 +0100279 uint32_t * sz = (uint32_t *)p;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100280 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
281 *cur++ = (void *)p;
Azim Khand59391a2017-06-01 14:04:17 +0100282 if ( c == 'H' )
283 *cur++ = (void *)sz;
284 INCR_ASSERT( p, data, data_len, ( *sz ) );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100285 }
286 break;
287 default:
288 {
289 *error = DISPATCH_INVALID_TEST_DATA;
290 goto exit;
291 }
292 break;
293 }
294 }
295
296exit:
297 if ( *error )
298 {
299 free( params );
300 params = NULL;
301 }
302
303 return( params );
304}
305
306/**
307 * \brief Sends greentea key and int value pair to host.
308 *
309 * \param key key string
310 * \param value integer value
311 *
312 * \return void
313 */
314void send_key_integer( char * key, int value )
315{
316 char str[50];
317 snprintf( str, sizeof( str ), "%d", value );
Azim Khan05746322017-05-23 13:00:35 +0100318 greentea_send_kv( key, str );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100319}
320
321/**
322 * \brief Sends test setup failure to the host.
323 *
324 * \param failure Test set failure
325 *
326 * \return void
327 */
328void send_failure( int failure )
329{
330 send_key_integer( "F", failure );
331}
332
333/**
334 * \brief Sends test status to the host.
335 *
336 * \param status Test status (PASS=0/FAIL=!0)
337 *
338 * \return void
339 */
340void send_status( int status )
341{
342 send_key_integer( "R", status );
343}
344
345
346/**
347 * \brief Embedded implementation of execute_tests().
348 * Ignores command line and received test data
349 * on serial.
350 *
351 * \param argc not used
352 * \param argv not used
353 *
354 * \return Program exit status.
355 */
356int execute_tests( int args, const char ** argv )
357{
358 int ret = 0;
359 uint32_t data_len = 0;
360 uint8_t count = 0, function_id;
361 void ** params = NULL;
362 uint8_t * data = NULL, * p = NULL;
363
Azim Khan05746322017-05-23 13:00:35 +0100364 GREENTEA_SETUP( 180, "mbedtls_test" );
365 greentea_send_kv( "GO", " " );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100366
367 while ( 1 )
368 {
369 ret = 0;
370 test_info.failed = 0;
371 data_len = 0;
372
373 data = receive_data( &data_len );
374 if ( data == NULL )
375 continue;
376 p = data;
377
378 do
379 {
380 /* Read dependency count */
381 count = *p;
382 assert( count < data_len );
383 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
384 ret = verify_dependencies( count, p );
385 if ( ret != DEPENDENCY_SUPPORTED )
386 break;
387
Azim Khand59391a2017-06-01 14:04:17 +0100388 if ( count )
389 INCR_ASSERT( p, data, data_len, count );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100390
391 /* Read function id */
392 function_id = *p;
393 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
Azim Khan13c6bfb2017-06-15 14:45:56 +0100394 if ( ( ret = check_test( function_id ) ) != DISPATCH_TEST_SUCCESS )
395 break;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100396
397 /* Read number of parameters */
398 count = *p;
399 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
400
Azim Khand59391a2017-06-01 14:04:17 +0100401 /* Parse parameters if present */
402 if ( count )
403 {
404 params = parse_parameters( count, p, data_len - ( p - data ), &ret );
405 if ( ret )
406 break;
407 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100408
409 ret = dispatch_test( function_id, params );
410 }
411 while ( 0 );
412
413 if ( data )
414 {
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100415 free( data );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100416 data = NULL;
417 }
418
419 if ( params )
420 {
421 free( params );
422 params = NULL;
423 }
424
425 if ( ret )
426 send_failure( ret );
427 else
428 send_status( test_info.failed );
429 }
430 return( 0 );
431}
432