blob: ba59089998107804f0ac32f015283dd9566d21bd [file] [log] [blame]
Mohammad Azim Khanfff49042017-03-28 01:48:31 +01001#line 2 "embedded_test.function"
2
3#include "greentea-client/test_env_c.h"
4
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{ \
16 assert( p >= start ); \
17 assert( sizeof( *p ) == sizeof( *start ) ); \
18 /* <= is checked to support use inside a loop where \
19 pointer is incremented after reading data. */ \
20 assert( (uint32_t)( (p - start) + step ) <= len ); \
21 p += step; \
22} \
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 */
34#define ALIGN_32BIT(p, start, len) do \
35{ \
36 uint32_t align = ( - (uintptr_t)p ) % 4; \
37 INCR_ASSERT(p, start, len, align); \
38} \
39while( 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
62
63/**
64 * \brief Receives unsigned integer on serial interface.
65 * Integers are encoded in network order.
66 *
67 * \param none
68 *
69 * \return unsigned int
70 */
71uint32_t receive_uint32()
72{
73 uint32_t value;
74 value = (uint8_t)greentea_getc() << 24;
75 value |= (uint8_t)greentea_getc() << 16;
76 value |= (uint8_t)greentea_getc() << 8;
77 value |= (uint8_t)greentea_getc();
78 return( (uint32_t)value );
79}
80
81/**
82 * \brief Parses out an unsigned 32 int value from the byte array.
83 * Integers are encoded in network order.
84 *
85 * \param p Pointer to byte array
86 *
87 * \return unsigned int
88 */
89uint32_t parse_uint32( uint8_t * p )
90{
91 uint32_t value;
92 value = *p++ << 24;
93 value |= *p++ << 16;
94 value |= *p++ << 8;
95 value |= *p;
96 return( value );
97}
98
99
100/**
101 * \brief Receives test data on serial as greentea key,value pair:
102 * {{<length>;<byte array>}}
103 *
104 * \param data_len Out pointer to hold received data length.
105 *
106 * \return Byte array.
107 */
108uint8_t * receive_data( uint32_t * data_len )
109{
110 uint32_t i = 0, errors = 0;
111 char c;
112 uint8_t * data = NULL;
113
114 /* Read opening braces */
115 i = 0;
116 while ( i < 2 )
117 {
118 c = greentea_getc();
119 /* Ignore any prevous CR LF characters */
120 if ( c == '\n' || c == '\r' )
121 continue;
122 i++;
123 if ( c != '{' )
124 return( NULL );
125 }
126
127 /* Read data length */
128 *data_len = receive_uint32();
129 data = (uint8_t *)malloc( *data_len );
130 assert( data != NULL );
131
132 greentea_getc(); // read ';' received after key i.e. *data_len
133
134 for( i = 0; i < *data_len; i++ )
135 data[i] = greentea_getc();
136
137 /* Read closing braces */
138 for( i = 0; i < 2; i++ )
139 {
140 c = greentea_getc();
141 if ( c != '}' )
142 {
143 errors++;
144 break;
145 }
146 }
147
148 if ( errors )
149 {
150 free( data );
151 data = NULL;
152 *data_len = 0;
153 }
154
155 return( data );
156}
157
158/**
Azim Khand59391a2017-06-01 14:04:17 +0100159 * \brief Parses received byte array and finds number of hex parameters.
160 *
161 * \param count Parameter count
162 * \param data Received Byte array
163 * \param data_len Byte array length
164 *
165 * \return count of hex params
166 */
167uint32_t find_hex_count( uint8_t count, uint8_t * data, uint32_t data_len )
168{
169 uint32_t i = 0, sz = 0;
170 char c;
171 uint8_t * p = NULL;
172 uint32_t hex_count = 0;
173
174 p = data;
175
176 for( i = 0; i < count; i++ )
177 {
178 c = (char)*p;
179 INCR_ASSERT( p, data, data_len, 1 );
180
181 /* Align p to 4 bytes for int, expression, string len or hex length */
182 ALIGN_32BIT( p, data, data_len );
183
184 /* Network to host conversion */
185 sz = (int32_t)parse_uint32( p );
186
187 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
188
189 if ( c == 'H' || c == 'S' )
190 {
191 INCR_ASSERT( p, data, data_len, sz );
192 hex_count += ( c == 'H' )?1:0;
193 }
194 }
195
196 return( hex_count );
197}
198
199/**
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100200 * \brief Parses received byte array for test parameters.
201 *
202 * \param count Parameter count
203 * \param data Received Byte array
204 * \param data_len Byte array length
205 * \param error Parsing error out variable.
206 *
207 * \return Array of parsed parameters allocated on heap.
208 * Note: Caller has the responsibility to delete
209 * the memory after use.
210 */
211void ** parse_parameters( uint8_t count, uint8_t * data, uint32_t data_len,
212 int * error )
213{
Azim Khand59391a2017-06-01 14:04:17 +0100214 uint32_t i = 0, hex_count = 0;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100215 char c;
216 void ** params = NULL;
217 void ** cur = NULL;
218 uint8_t * p = NULL;
219
Azim Khand59391a2017-06-01 14:04:17 +0100220 hex_count = find_hex_count(count, data, data_len);
221
222 params = (void **)malloc( sizeof( void *) * ( count + hex_count ) );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100223 assert( params != NULL );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100224 cur = params;
225
226 p = data;
227
228 /* Parameters */
229 for( i = 0; i < count; i++ )
230 {
231 c = (char)*p;
232 INCR_ASSERT( p, data, data_len, 1 );
233
234 /* Align p to 4 bytes for int, expression, string len or hex length */
235 ALIGN_32BIT( p, data, data_len );
236
237 /* Network to host conversion */
238 *( (int32_t *)p ) = (int32_t)parse_uint32( p );
239
240 switch( c )
241 {
242 case 'E':
243 {
244 if ( get_expression( *( (int32_t *)p ), (int32_t *)p ) )
245 {
246 *error = KEY_VALUE_MAPPING_NOT_FOUND;
247 goto exit;
248 }
249 } /* Intentional fall through */
250 case 'I':
251 {
252 *cur++ = (void *)p;
253 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
254 }
255 break;
Azim Khand59391a2017-06-01 14:04:17 +0100256 case 'H': /* Intentional fall through */
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100257 case 'S':
258 {
Azim Khand59391a2017-06-01 14:04:17 +0100259 uint32_t * sz = (uint32_t *)p;
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100260 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
261 *cur++ = (void *)p;
Azim Khand59391a2017-06-01 14:04:17 +0100262 if ( c == 'H' )
263 *cur++ = (void *)sz;
264 INCR_ASSERT( p, data, data_len, ( *sz ) );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100265 }
266 break;
267 default:
268 {
269 *error = DISPATCH_INVALID_TEST_DATA;
270 goto exit;
271 }
272 break;
273 }
274 }
275
276exit:
277 if ( *error )
278 {
279 free( params );
280 params = NULL;
281 }
282
283 return( params );
284}
285
286/**
287 * \brief Sends greentea key and int value pair to host.
288 *
289 * \param key key string
290 * \param value integer value
291 *
292 * \return void
293 */
294void send_key_integer( char * key, int value )
295{
296 char str[50];
297 snprintf( str, sizeof( str ), "%d", value );
Azim Khan05746322017-05-23 13:00:35 +0100298 greentea_send_kv( key, str );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100299}
300
301/**
302 * \brief Sends test setup failure to the host.
303 *
304 * \param failure Test set failure
305 *
306 * \return void
307 */
308void send_failure( int failure )
309{
310 send_key_integer( "F", failure );
311}
312
313/**
314 * \brief Sends test status to the host.
315 *
316 * \param status Test status (PASS=0/FAIL=!0)
317 *
318 * \return void
319 */
320void send_status( int status )
321{
322 send_key_integer( "R", status );
323}
324
325
326/**
327 * \brief Embedded implementation of execute_tests().
328 * Ignores command line and received test data
329 * on serial.
330 *
331 * \param argc not used
332 * \param argv not used
333 *
334 * \return Program exit status.
335 */
336int execute_tests( int args, const char ** argv )
337{
338 int ret = 0;
339 uint32_t data_len = 0;
340 uint8_t count = 0, function_id;
341 void ** params = NULL;
342 uint8_t * data = NULL, * p = NULL;
343
Azim Khan05746322017-05-23 13:00:35 +0100344 GREENTEA_SETUP( 180, "mbedtls_test" );
345 greentea_send_kv( "GO", " " );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100346
347 while ( 1 )
348 {
349 ret = 0;
350 test_info.failed = 0;
351 data_len = 0;
352
353 data = receive_data( &data_len );
354 if ( data == NULL )
355 continue;
356 p = data;
357
358 do
359 {
360 /* Read dependency count */
361 count = *p;
362 assert( count < data_len );
363 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
364 ret = verify_dependencies( count, p );
365 if ( ret != DEPENDENCY_SUPPORTED )
366 break;
367
Azim Khand59391a2017-06-01 14:04:17 +0100368 if ( count )
369 INCR_ASSERT( p, data, data_len, count );
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100370
371 /* Read function id */
372 function_id = *p;
373 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
374
375 /* Read number of parameters */
376 count = *p;
377 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
378
Azim Khand59391a2017-06-01 14:04:17 +0100379 /* Parse parameters if present */
380 if ( count )
381 {
382 params = parse_parameters( count, p, data_len - ( p - data ), &ret );
383 if ( ret )
384 break;
385 }
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100386
387 ret = dispatch_test( function_id, params );
388 }
389 while ( 0 );
390
391 if ( data )
392 {
393 free(data);
394 data = NULL;
395 }
396
397 if ( params )
398 {
399 free( params );
400 params = NULL;
401 }
402
403 if ( ret )
404 send_failure( ret );
405 else
406 send_status( test_info.failed );
407 }
408 return( 0 );
409}
410