From 5075f4df18e9cfe56e772288d3daf317f2d7dca8 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Mon, 3 Jun 2019 11:38:42 +0300 Subject: [PATCH] Update the test encoding to support python3 Since Python3 handles encoding differently than Python2, a change in the way the data is encoded and sent to the target is needed. 1. Change the test data to be sent as hex string 2. Convert the characters to binary bytes. This is done because the mbed tools translate the encoding differently (mbed-greentea, and mbed-htrunner) --- tests/scripts/mbedtls_test.py | 26 ++++++++---------- tests/suites/target_test.function | 45 ++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/tests/scripts/mbedtls_test.py b/tests/scripts/mbedtls_test.py index 7e1cebf7d..62c229631 100755 --- a/tests/scripts/mbedtls_test.py +++ b/tests/scripts/mbedtls_test.py @@ -89,24 +89,20 @@ class TestDataParser(object): :param data_f: Data file object :return: """ - while True: - line = data_f.readline().strip() + for line in data_f: + line = line.strip() if not line: - break + continue # Read test name name = line # Check dependencies dependencies = [] - line = data_f.readline().strip() - if not line: - break + line = next(data_f).strip() match = re.search('depends_on:(.*)', line) if match: dependencies = [int(x) for x in match.group(1).split(':')] - line = data_f.readline().strip() - if not line: - break + line = next(data_f).strip() # Read test vectors line = line.replace('\\n', '\n') @@ -265,20 +261,20 @@ class MbedTlsTest(BaseHostTest): for typ, param in parameters: if typ == 'int' or typ == 'exp': i = int(param) - data_bytes += 'I' if typ == 'int' else 'E' + data_bytes += b'I' if typ == 'int' else b'E' self.align_32bit(data_bytes) data_bytes += self.int32_to_big_endian_bytes(i) elif typ == 'char*': param = param.strip('"') i = len(param) + 1 # + 1 for null termination - data_bytes += 'S' + data_bytes += b'S' self.align_32bit(data_bytes) data_bytes += self.int32_to_big_endian_bytes(i) - data_bytes += bytearray(list(param)) - data_bytes += '\0' # Null terminate + data_bytes += bytes(param, 'ascii') + data_bytes += b'\0' # Null terminate elif typ == 'hex': binary_data = self.hex_str_bytes(param) - data_bytes += 'H' + data_bytes += b'H' self.align_32bit(data_bytes) i = len(binary_data) data_bytes += self.int32_to_big_endian_bytes(i) @@ -313,7 +309,7 @@ class MbedTlsTest(BaseHostTest): param_bytes, length = self.test_vector_to_bytes(function_id, dependencies, args) - self.send_kv(bytes(length).decode(), bytes(param_bytes).decode()) + self.send_kv(length.hex(), param_bytes.hex()) @staticmethod def get_result(value): diff --git a/tests/suites/target_test.function b/tests/suites/target_test.function index 56abf2948..4248260b0 100644 --- a/tests/suites/target_test.function +++ b/tests/suites/target_test.function @@ -59,10 +59,43 @@ int verify_dependencies( uint8_t count, uint8_t * dep_p ) return( DEPENDENCY_SUPPORTED ); } +/** + * \brief Receives hex string on serial interface, and converts to a byte. + * + * \param none + * + * \return unsigned int8 + */ +uint8_t receive_byte() +{ + uint8_t byte; + uint8_t c; + + c = greentea_getc(); + if( c >= '0' && c <= '9' ) + c -= '0'; + else if( c >= 'a' && c <= 'f' ) + c = ( c -'a' ) + 10; + else if( c >= 'A' && c <= 'F' ) + c = ( c - 'A' ) + 10; + + byte = c * 0x10; + + c = greentea_getc(); + if( c >= '0' && c <= '9' ) + c -= '0'; + else if( c >= 'a' && c <= 'f' ) + c = ( c -'a' ) + 10; + else if( c >= 'A' && c <= 'F' ) + c = ( c - 'A' ) + 10; + + byte += c ; + return( byte); +} /** * \brief Receives unsigned integer on serial interface. - * Integers are encoded in network order. + * Integers are encoded in network order, and sent as hex ascii string. * * \param none * @@ -71,10 +104,10 @@ int verify_dependencies( uint8_t count, uint8_t * dep_p ) uint32_t receive_uint32() { uint32_t value; - value = (uint8_t)greentea_getc() << 24; - value |= (uint8_t)greentea_getc() << 16; - value |= (uint8_t)greentea_getc() << 8; - value |= (uint8_t)greentea_getc(); + value = receive_byte() << 24; + value |= receive_byte() << 16; + value |= receive_byte() << 8; + value |= receive_byte(); return( (uint32_t)value ); } @@ -132,7 +165,7 @@ uint8_t * receive_data( uint32_t * data_len ) greentea_getc(); // read ';' received after key i.e. *data_len for( i = 0; i < *data_len; i++ ) - data[i] = greentea_getc(); + data[i] = receive_byte(); /* Read closing braces */ for( i = 0; i < 2; i++ )