target_test.function 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #line 2 "suites/target_test.function"
  2. #include "greentea-client/test_env.h"
  3. /**
  4. * \brief Increments pointer and asserts that it does not overflow.
  5. *
  6. * \param p Pointer to byte array
  7. * \param start Pointer to start of byte array
  8. * \param len Length of byte array
  9. * \param step Increment size
  10. *
  11. */
  12. #define INCR_ASSERT(p, start, len, step) do \
  13. { \
  14. TEST_HELPER_ASSERT( ( p ) >= ( start ) ); \
  15. TEST_HELPER_ASSERT( sizeof( *( p ) ) == sizeof( *( start ) ) ); \
  16. /* <= is checked to support use inside a loop where \
  17. pointer is incremented after reading data. */ \
  18. TEST_HELPER_ASSERT( (uint32_t)( ( ( p ) - ( start ) ) + ( step ) ) <= ( len ) );\
  19. ( p ) += ( step ); \
  20. } \
  21. while( 0 )
  22. /**
  23. * \brief 4 byte align unsigned char pointer
  24. *
  25. * \param p Pointer to byte array
  26. * \param start Pointer to start of byte array
  27. * \param len Length of byte array
  28. *
  29. */
  30. #define ALIGN_32BIT(p, start, len) do \
  31. { \
  32. uint32_t align = ( - (uintptr_t)( p ) ) % 4; \
  33. INCR_ASSERT( ( p ), ( start ), ( len ), align );\
  34. } \
  35. while( 0 )
  36. /**
  37. * \brief Verify dependencies. Dependency identifiers are
  38. * encoded in the buffer as 8 bit unsigned integers.
  39. *
  40. * \param count Number of dependencies.
  41. * \param dep_p Pointer to buffer.
  42. *
  43. * \return DEPENDENCY_SUPPORTED if success else DEPENDENCY_NOT_SUPPORTED.
  44. */
  45. int verify_dependencies( uint8_t count, uint8_t * dep_p )
  46. {
  47. uint8_t i;
  48. for ( i = 0; i < count; i++ )
  49. {
  50. if ( dep_check( (int)(dep_p[i]) ) != DEPENDENCY_SUPPORTED )
  51. return( DEPENDENCY_NOT_SUPPORTED );
  52. }
  53. return( DEPENDENCY_SUPPORTED );
  54. }
  55. /**
  56. * \brief Receives hex string on serial interface, and converts to a byte.
  57. *
  58. * \param none
  59. *
  60. * \return unsigned int8
  61. */
  62. uint8_t receive_byte()
  63. {
  64. uint8_t byte;
  65. uint8_t c[3];
  66. char *endptr;
  67. c[0] = greentea_getc();
  68. c[1] = greentea_getc();
  69. c[2] = '\0';
  70. TEST_HELPER_ASSERT( mbedtls_test_unhexify( &byte, c ) != 2 );
  71. return( byte );
  72. }
  73. /**
  74. * \brief Receives unsigned integer on serial interface.
  75. * Integers are encoded in network order, and sent as hex ascii string.
  76. *
  77. * \param none
  78. *
  79. * \return unsigned int
  80. */
  81. uint32_t receive_uint32()
  82. {
  83. uint32_t value;
  84. const uint8_t c_be[8] = { greentea_getc(),
  85. greentea_getc(),
  86. greentea_getc(),
  87. greentea_getc(),
  88. greentea_getc(),
  89. greentea_getc(),
  90. greentea_getc(),
  91. greentea_getc()
  92. };
  93. const uint8_t c[9] = { c_be[6], c_be[7], c_be[4], c_be[5], c_be[2],
  94. c_be[3], c_be[0], c_be[1], '\0' };
  95. TEST_HELPER_ASSERT( mbedtls_test_unhexify( (uint8_t*)&value, c ) != 8 );
  96. return( value );
  97. }
  98. /**
  99. * \brief Parses out an unsigned 32 int value from the byte array.
  100. * Integers are encoded in network order.
  101. *
  102. * \param p Pointer to byte array
  103. *
  104. * \return unsigned int
  105. */
  106. uint32_t parse_uint32( uint8_t * p )
  107. {
  108. uint32_t value;
  109. value = *p++ << 24;
  110. value |= *p++ << 16;
  111. value |= *p++ << 8;
  112. value |= *p;
  113. return( value );
  114. }
  115. /**
  116. * \brief Receives test data on serial as greentea key,value pair:
  117. * {{<length>;<byte array>}}
  118. *
  119. * \param data_len Out pointer to hold received data length.
  120. *
  121. * \return Byte array.
  122. */
  123. uint8_t * receive_data( uint32_t * data_len )
  124. {
  125. uint32_t i = 0, errors = 0;
  126. char c;
  127. uint8_t * data = NULL;
  128. /* Read opening braces */
  129. i = 0;
  130. while ( i < 2 )
  131. {
  132. c = greentea_getc();
  133. /* Ignore any prevous CR LF characters */
  134. if ( c == '\n' || c == '\r' )
  135. continue;
  136. i++;
  137. if ( c != '{' )
  138. return( NULL );
  139. }
  140. /* Read data length */
  141. *data_len = receive_uint32();
  142. data = (uint8_t *)malloc( *data_len );
  143. TEST_HELPER_ASSERT( data != NULL );
  144. greentea_getc(); // read ';' received after key i.e. *data_len
  145. for( i = 0; i < *data_len; i++ )
  146. data[i] = receive_byte();
  147. /* Read closing braces */
  148. for( i = 0; i < 2; i++ )
  149. {
  150. c = greentea_getc();
  151. if ( c != '}' )
  152. {
  153. errors++;
  154. break;
  155. }
  156. }
  157. if ( errors )
  158. {
  159. free( data );
  160. data = NULL;
  161. *data_len = 0;
  162. }
  163. return( data );
  164. }
  165. /**
  166. * \brief Parse the received byte array and count the number of arguments
  167. * to the test function passed as type hex.
  168. *
  169. * \param count Parameter count
  170. * \param data Received Byte array
  171. * \param data_len Byte array length
  172. *
  173. * \return count of hex params
  174. */
  175. uint32_t find_hex_count( uint8_t count, uint8_t * data, uint32_t data_len )
  176. {
  177. uint32_t i = 0, sz = 0;
  178. char c;
  179. uint8_t * p = NULL;
  180. uint32_t hex_count = 0;
  181. p = data;
  182. for( i = 0; i < count; i++ )
  183. {
  184. c = (char)*p;
  185. INCR_ASSERT( p, data, data_len, 1 );
  186. /* Align p to 4 bytes for int, expression, string len or hex length */
  187. ALIGN_32BIT( p, data, data_len );
  188. /* Network to host conversion */
  189. sz = (int32_t)parse_uint32( p );
  190. INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
  191. if ( c == 'H' || c == 'S' )
  192. {
  193. INCR_ASSERT( p, data, data_len, sz );
  194. hex_count += ( c == 'H' )?1:0;
  195. }
  196. }
  197. return( hex_count );
  198. }
  199. /**
  200. * \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. */
  211. void ** parse_parameters( uint8_t count, uint8_t * data, uint32_t data_len,
  212. int * error )
  213. {
  214. uint32_t i = 0, hex_count = 0;
  215. char c;
  216. void ** params = NULL;
  217. void ** cur = NULL;
  218. uint8_t * p = NULL;
  219. hex_count = find_hex_count(count, data, data_len);
  220. params = (void **)malloc( sizeof( void *) * ( count + hex_count ) );
  221. TEST_HELPER_ASSERT( params != NULL );
  222. cur = params;
  223. p = data;
  224. /* Parameters */
  225. for( i = 0; i < count; i++ )
  226. {
  227. c = (char)*p;
  228. INCR_ASSERT( p, data, data_len, 1 );
  229. /* Align p to 4 bytes for int, expression, string len or hex length */
  230. ALIGN_32BIT( p, data, data_len );
  231. /* Network to host conversion */
  232. *( (int32_t *)p ) = (int32_t)parse_uint32( p );
  233. switch( c )
  234. {
  235. case 'E':
  236. {
  237. if ( get_expression( *( (int32_t *)p ), (int32_t *)p ) )
  238. {
  239. *error = KEY_VALUE_MAPPING_NOT_FOUND;
  240. goto exit;
  241. }
  242. } /* Intentional fall through */
  243. case 'I':
  244. {
  245. *cur++ = (void *)p;
  246. INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
  247. }
  248. break;
  249. case 'H': /* Intentional fall through */
  250. case 'S':
  251. {
  252. uint32_t * sz = (uint32_t *)p;
  253. INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
  254. *cur++ = (void *)p;
  255. if ( c == 'H' )
  256. *cur++ = (void *)sz;
  257. INCR_ASSERT( p, data, data_len, ( *sz ) );
  258. }
  259. break;
  260. default:
  261. {
  262. *error = DISPATCH_INVALID_TEST_DATA;
  263. goto exit;
  264. }
  265. break;
  266. }
  267. }
  268. exit:
  269. if ( *error )
  270. {
  271. free( params );
  272. params = NULL;
  273. }
  274. return( params );
  275. }
  276. /**
  277. * \brief Sends greentea key and int value pair to host.
  278. *
  279. * \param key key string
  280. * \param value integer value
  281. *
  282. * \return void
  283. */
  284. void send_key_integer( char * key, int value )
  285. {
  286. char str[50];
  287. snprintf( str, sizeof( str ), "%d", value );
  288. greentea_send_kv( key, str );
  289. }
  290. /**
  291. * \brief Sends test setup failure to the host.
  292. *
  293. * \param failure Test set failure
  294. *
  295. * \return void
  296. */
  297. void send_failure( int failure )
  298. {
  299. send_key_integer( "F", failure );
  300. }
  301. /**
  302. * \brief Sends test status to the host.
  303. *
  304. * \param status Test status (PASS=0/FAIL=!0)
  305. *
  306. * \return void
  307. */
  308. void send_status( int status )
  309. {
  310. send_key_integer( "R", status );
  311. }
  312. /**
  313. * \brief Embedded implementation of execute_tests().
  314. * Ignores command line and received test data
  315. * on serial.
  316. *
  317. * \param argc not used
  318. * \param argv not used
  319. *
  320. * \return Program exit status.
  321. */
  322. int execute_tests( int args, const char ** argv )
  323. {
  324. int ret = 0;
  325. uint32_t data_len = 0;
  326. uint8_t count = 0, function_id;
  327. void ** params = NULL;
  328. uint8_t * data = NULL, * p = NULL;
  329. GREENTEA_SETUP( 800, "mbedtls_test" );
  330. greentea_send_kv( "GO", " " );
  331. while ( 1 )
  332. {
  333. ret = 0;
  334. test_info.result = TEST_RESULT_SUCCESS;
  335. data_len = 0;
  336. data = receive_data( &data_len );
  337. if ( data == NULL )
  338. continue;
  339. p = data;
  340. do
  341. {
  342. /* Read dependency count */
  343. count = *p;
  344. TEST_HELPER_ASSERT( count < data_len );
  345. INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
  346. ret = verify_dependencies( count, p );
  347. if ( ret != DEPENDENCY_SUPPORTED )
  348. break;
  349. if ( count )
  350. INCR_ASSERT( p, data, data_len, count );
  351. /* Read function id */
  352. function_id = *p;
  353. INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
  354. if ( ( ret = check_test( function_id ) ) != DISPATCH_TEST_SUCCESS )
  355. break;
  356. /* Read number of parameters */
  357. count = *p;
  358. INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
  359. /* Parse parameters if present */
  360. if ( count )
  361. {
  362. params = parse_parameters( count, p, data_len - ( p - data ), &ret );
  363. if ( ret )
  364. break;
  365. }
  366. ret = dispatch_test( function_id, params );
  367. }
  368. while ( 0 );
  369. if ( data )
  370. {
  371. free( data );
  372. data = NULL;
  373. }
  374. if ( params )
  375. {
  376. free( params );
  377. params = NULL;
  378. }
  379. if ( ret )
  380. send_failure( ret );
  381. else
  382. send_status( test_info.result );
  383. }
  384. return( 0 );
  385. }