From d59391afcd488f4fe44dd7f92cd9a53b8eb87242 Mon Sep 17 00:00:00 2001 From: Azim Khan Date: Thu, 1 Jun 2017 14:04:17 +0100 Subject: [PATCH] Add support for sending hex parameters --- tests/scripts/mbedtls_test.py | 29 ++++++++++++ tests/suites/embedded_test.function | 72 +++++++++++++++++++++++------ 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/tests/scripts/mbedtls_test.py b/tests/scripts/mbedtls_test.py index 19893ffb4..fa5b50706 100644 --- a/tests/scripts/mbedtls_test.py +++ b/tests/scripts/mbedtls_test.py @@ -160,6 +160,28 @@ class MbedTlsTest(BaseHostTest): """ b += bytearray((4 - (len(b))) % 4) + @staticmethod + def hex_str_bytes(hex_str): + """ + Converts Hex string representation to byte array + + :param hex_str: + :return: + """ + assert hex_str[0] == '"' and hex_str[len(hex_str) - 1] == '"', \ + "HEX test parameter missing '\"': %s" % hex_str + hex_str = hex_str.strip('"') + assert len(hex_str) % 2 == 0, "HEX parameter len should be mod of 2: %s" % hex_str + b = bytearray() + + for i in xrange(len(hex_str) / 2): + h = hex_str[i * 2] + hex_str[(i * 2) + 1] + try: + b += bytearray([int(h, 16)]) + except ValueError: + raise ValueError("Invalid HEX value: %s" % hex_str) + return b + def parameters_to_bytes(self, b, parameters): for typ, param in parameters: if typ == 'int' or typ == 'exp': @@ -175,6 +197,13 @@ class MbedTlsTest(BaseHostTest): b += bytearray([((i >> x) & 0xff) for x in [24, 16, 8, 0]]) b += bytearray(list(param)) b += '\0' # Null terminate + elif typ == 'hex': + hb = self.hex_str_bytes(param) + b += 'H' + self.align_32bit(b) + i = len(hb) + b += bytearray([((i >> x) & 0xff) for x in [24, 16, 8, 0]]) + b += hb return b def run_next_test(self): diff --git a/tests/suites/embedded_test.function b/tests/suites/embedded_test.function index e885a0e99..ba5908999 100644 --- a/tests/suites/embedded_test.function +++ b/tests/suites/embedded_test.function @@ -155,6 +155,47 @@ uint8_t * receive_data( uint32_t * data_len ) return( data ); } +/** + * \brief Parses received byte array and finds number of hex parameters. + * + * \param count Parameter count + * \param data Received Byte array + * \param data_len Byte array length + * + * \return count of hex params + */ +uint32_t find_hex_count( uint8_t count, uint8_t * data, uint32_t data_len ) +{ + uint32_t i = 0, sz = 0; + char c; + uint8_t * p = NULL; + uint32_t hex_count = 0; + + p = data; + + for( i = 0; i < count; i++ ) + { + c = (char)*p; + INCR_ASSERT( p, data, data_len, 1 ); + + /* Align p to 4 bytes for int, expression, string len or hex length */ + ALIGN_32BIT( p, data, data_len ); + + /* Network to host conversion */ + sz = (int32_t)parse_uint32( p ); + + INCR_ASSERT( p, data, data_len, sizeof( int32_t ) ); + + if ( c == 'H' || c == 'S' ) + { + INCR_ASSERT( p, data, data_len, sz ); + hex_count += ( c == 'H' )?1:0; + } + } + + return( hex_count ); +} + /** * \brief Parses received byte array for test parameters. * @@ -170,15 +211,16 @@ uint8_t * receive_data( uint32_t * data_len ) void ** parse_parameters( uint8_t count, uint8_t * data, uint32_t data_len, int * error ) { - uint32_t i = 0; + uint32_t i = 0, hex_count = 0; char c; void ** params = NULL; void ** cur = NULL; uint8_t * p = NULL; - params = (void **)malloc( sizeof( void *) * ( count + 1 ) ); + hex_count = find_hex_count(count, data, data_len); + + params = (void **)malloc( sizeof( void *) * ( count + hex_count ) ); assert( params != NULL ); - params[count] = NULL; cur = params; p = data; @@ -211,16 +253,15 @@ void ** parse_parameters( uint8_t count, uint8_t * data, uint32_t data_len, INCR_ASSERT( p, data, data_len, sizeof( int32_t ) ); } break; - case 'H': - { - *cur++ = (void *)p; - } /* Intentional fall through */ + case 'H': /* Intentional fall through */ case 'S': { - uint32_t sz = *( (int32_t *)p ); + uint32_t * sz = (uint32_t *)p; INCR_ASSERT( p, data, data_len, sizeof( int32_t ) ); *cur++ = (void *)p; - INCR_ASSERT( p, data, data_len, sz ); + if ( c == 'H' ) + *cur++ = (void *)sz; + INCR_ASSERT( p, data, data_len, ( *sz ) ); } break; default: @@ -324,7 +365,8 @@ int execute_tests( int args, const char ** argv ) if ( ret != DEPENDENCY_SUPPORTED ) break; - INCR_ASSERT( p, data, data_len, count ); + if ( count ) + INCR_ASSERT( p, data, data_len, count ); /* Read function id */ function_id = *p; @@ -334,9 +376,13 @@ int execute_tests( int args, const char ** argv ) count = *p; INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) ); - params = parse_parameters( count, p, data_len - (p - data), &ret ); - if ( ret ) - break; + /* Parse parameters if present */ + if ( count ) + { + params = parse_parameters( count, p, data_len - ( p - data ), &ret ); + if ( ret ) + break; + } ret = dispatch_test( function_id, params ); }