1 #line 2 "suites/target_test.function"
3 #include "greentea-client/test_env.h"
6 * \brief Increments pointer and asserts that it does not overflow.
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
14 #define INCR_ASSERT(p, start, len, step) do \
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 ) );\
27 * \brief 4 byte align unsigned char pointer
29 * \param p Pointer to byte array
30 * \param start Pointer to start of byte array
31 * \param len Length of byte array
34 #define ALIGN_32BIT(p, start, len) do \
36 uint32_t align = ( - (uintptr_t)( p ) ) % 4; \
37 INCR_ASSERT( ( p ), ( start ), ( len ), align );\
43 * \brief Verify dependencies. Dependency identifiers are
44 * encoded in the buffer as 8 bit unsigned integers.
46 * \param count Number of dependencies.
47 * \param dep_p Pointer to buffer.
49 * \return DEPENDENCY_SUPPORTED if success else DEPENDENCY_NOT_SUPPORTED.
51 int verify_dependencies( uint8_t count, uint8_t * dep_p )
54 for ( i = 0; i < count; i++ )
56 if ( dep_check( (int)(dep_p[i]) ) != DEPENDENCY_SUPPORTED )
57 return( DEPENDENCY_NOT_SUPPORTED );
59 return( DEPENDENCY_SUPPORTED );
64 * \brief Receives unsigned integer on serial interface.
65 * Integers are encoded in network order.
69 * \return unsigned int
71 uint32_t receive_uint32()
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 );
82 * \brief Parses out an unsigned 32 int value from the byte array.
83 * Integers are encoded in network order.
85 * \param p Pointer to byte array
87 * \return unsigned int
89 uint32_t parse_uint32( uint8_t * p )
101 * \brief Receives test data on serial as greentea key,value pair:
102 * {{<length>;<byte array>}}
104 * \param data_len Out pointer to hold received data length.
106 * \return Byte array.
108 uint8_t * receive_data( uint32_t * data_len )
110 uint32_t i = 0, errors = 0;
112 uint8_t * data = NULL;
114 /* Read opening braces */
119 /* Ignore any prevous CR LF characters */
120 if ( c == '\n' || c == '\r' )
127 /* Read data length */
128 *data_len = receive_uint32();
129 data = (uint8_t *)malloc( *data_len );
130 assert( data != NULL );
132 greentea_getc(); // read ';' received after key i.e. *data_len
134 for( i = 0; i < *data_len; i++ )
135 data[i] = greentea_getc();
137 /* Read closing braces */
138 for( i = 0; i < 2; i++ )
159 * \brief Parse the received byte array and count the number of arguments
160 * to the test function passed as type hex.
162 * \param count Parameter count
163 * \param data Received Byte array
164 * \param data_len Byte array length
166 * \return count of hex params
168 uint32_t find_hex_count( uint8_t count, uint8_t * data, uint32_t data_len )
170 uint32_t i = 0, sz = 0;
173 uint32_t hex_count = 0;
177 for( i = 0; i < count; i++ )
180 INCR_ASSERT( p, data, data_len, 1 );
182 /* Align p to 4 bytes for int, expression, string len or hex length */
183 ALIGN_32BIT( p, data, data_len );
185 /* Network to host conversion */
186 sz = (int32_t)parse_uint32( p );
188 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
190 if ( c == 'H' || c == 'S' )
192 INCR_ASSERT( p, data, data_len, sz );
193 hex_count += ( c == 'H' )?1:0;
201 * \brief Parses received byte array for test parameters.
203 * \param count Parameter count
204 * \param data Received Byte array
205 * \param data_len Byte array length
206 * \param error Parsing error out variable.
208 * \return Array of parsed parameters allocated on heap.
209 * Note: Caller has the responsibility to delete
210 * the memory after use.
212 void ** parse_parameters( uint8_t count, uint8_t * data, uint32_t data_len,
215 uint32_t i = 0, hex_count = 0;
217 void ** params = NULL;
221 hex_count = find_hex_count(count, data, data_len);
223 params = (void **)malloc( sizeof( void *) * ( count + hex_count ) );
224 assert( params != NULL );
230 for( i = 0; i < count; i++ )
233 INCR_ASSERT( p, data, data_len, 1 );
235 /* Align p to 4 bytes for int, expression, string len or hex length */
236 ALIGN_32BIT( p, data, data_len );
238 /* Network to host conversion */
239 *( (int32_t *)p ) = (int32_t)parse_uint32( p );
245 if ( get_expression( *( (int32_t *)p ), (int32_t *)p ) )
247 *error = KEY_VALUE_MAPPING_NOT_FOUND;
250 } /* Intentional fall through */
254 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
257 case 'H': /* Intentional fall through */
260 uint32_t * sz = (uint32_t *)p;
261 INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
265 INCR_ASSERT( p, data, data_len, ( *sz ) );
270 *error = DISPATCH_INVALID_TEST_DATA;
288 * \brief Sends greentea key and int value pair to host.
290 * \param key key string
291 * \param value integer value
295 void send_key_integer( char * key, int value )
298 snprintf( str, sizeof( str ), "%d", value );
299 greentea_send_kv( key, str );
303 * \brief Sends test setup failure to the host.
305 * \param failure Test set failure
309 void send_failure( int failure )
311 send_key_integer( "F", failure );
315 * \brief Sends test status to the host.
317 * \param status Test status (PASS=0/FAIL=!0)
321 void send_status( int status )
323 send_key_integer( "R", status );
328 * \brief Embedded implementation of execute_tests().
329 * Ignores command line and received test data
332 * \param argc not used
333 * \param argv not used
335 * \return Program exit status.
337 int execute_tests( int args, const char ** argv )
340 uint32_t data_len = 0;
341 uint8_t count = 0, function_id;
342 void ** params = NULL;
343 uint8_t * data = NULL, * p = NULL;
345 GREENTEA_SETUP( 180, "mbedtls_test" );
346 greentea_send_kv( "GO", " " );
351 test_info.failed = 0;
354 data = receive_data( &data_len );
361 /* Read dependency count */
363 assert( count < data_len );
364 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
365 ret = verify_dependencies( count, p );
366 if ( ret != DEPENDENCY_SUPPORTED )
370 INCR_ASSERT( p, data, data_len, count );
372 /* Read function id */
374 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
375 if ( ( ret = check_test( function_id ) ) != DISPATCH_TEST_SUCCESS )
378 /* Read number of parameters */
380 INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
382 /* Parse parameters if present */
385 params = parse_parameters( count, p, data_len - ( p - data ), &ret );
390 ret = dispatch_test( function_id, params );
409 send_status( test_info.failed );