From 33908e8429c302d232a1f95cedc444edccf67ae5 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Wed, 29 May 2019 17:17:10 +0300 Subject: [PATCH 1/6] update the test script Update `mbedtls_test.py` script to work with Python 3.7. resolves #2653 --- tests/scripts/mbedtls_test.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/scripts/mbedtls_test.py b/tests/scripts/mbedtls_test.py index ac2912d4c..7e1cebf7d 100755 --- a/tests/scripts/mbedtls_test.py +++ b/tests/scripts/mbedtls_test.py @@ -79,8 +79,7 @@ class TestDataParser(object): split_colon_fn = lambda x: re.sub(r'\\' + split_char, split_char, x) if len(split_char) > 1: raise ValueError('Expected split character. Found string!') - out = map(split_colon_fn, re.split(r'(? Date: Mon, 3 Jun 2019 11:38:42 +0300 Subject: [PATCH 2/6] 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++ ) From 64e45950de379602b41b078982ed135f7a3ee492 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Mon, 3 Jun 2019 13:39:21 +0300 Subject: [PATCH 3/6] Make the script portable to both pythons Make the script work for python3 and for python2 --- tests/scripts/mbedtls_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/scripts/mbedtls_test.py b/tests/scripts/mbedtls_test.py index 62c229631..b7b9dc063 100755 --- a/tests/scripts/mbedtls_test.py +++ b/tests/scripts/mbedtls_test.py @@ -270,7 +270,7 @@ class MbedTlsTest(BaseHostTest): data_bytes += b'S' self.align_32bit(data_bytes) data_bytes += self.int32_to_big_endian_bytes(i) - data_bytes += bytes(param, 'ascii') + data_bytes += bytearray(param, encoding='ascii') data_bytes += b'\0' # Null terminate elif typ == 'hex': binary_data = self.hex_str_bytes(param) @@ -309,7 +309,7 @@ class MbedTlsTest(BaseHostTest): param_bytes, length = self.test_vector_to_bytes(function_id, dependencies, args) - self.send_kv(length.hex(), param_bytes.hex()) + self.send_kv(''.join('{:02x}'.format(x) for x in length), ''.join('{:02x}'.format(x) for x in param_bytes)) @staticmethod def get_result(value): From b22048942260a20049b0c02922440375af318284 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Mon, 3 Jun 2019 16:39:59 +0300 Subject: [PATCH 4/6] Refactor get_byte function Change implementation of `get_byte()` to call `unhexify()`. --- tests/suites/target_test.function | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/tests/suites/target_test.function b/tests/suites/target_test.function index 4248260b0..4522fbf0c 100644 --- a/tests/suites/target_test.function +++ b/tests/suites/target_test.function @@ -69,28 +69,14 @@ int verify_dependencies( uint8_t count, uint8_t * dep_p ) uint8_t receive_byte() { uint8_t byte; - uint8_t c; + uint8_t c[3]; + char *endptr; + c[0] = greentea_getc(); + c[1] = greentea_getc(); + c[2] = '\0'; - 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); + assert( unhexify( &byte, &c ) != 2 ); + return( byte ); } /** From 72662a495ce3e92fe6bad77352e80af387eb0820 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Tue, 25 Jun 2019 14:50:20 +0300 Subject: [PATCH 5/6] Refactor receive_uint32() Call `greentea_getc()` 8 times, and then `unhexify` once, instead of calling `receive_byte()`, which inside calls `greentea_getc()` twice, for every hex digit. --- tests/suites/target_test.function | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/suites/target_test.function b/tests/suites/target_test.function index 4522fbf0c..71675bdf1 100644 --- a/tests/suites/target_test.function +++ b/tests/suites/target_test.function @@ -75,7 +75,7 @@ uint8_t receive_byte() c[1] = greentea_getc(); c[2] = '\0'; - assert( unhexify( &byte, &c ) != 2 ); + assert( unhexify( &byte, c ) != 2 ); return( byte ); } @@ -90,10 +90,17 @@ uint8_t receive_byte() uint32_t receive_uint32() { uint32_t value; - value = receive_byte() << 24; - value |= receive_byte() << 16; - value |= receive_byte() << 8; - value |= receive_byte(); + const uint8_t c[9] = { greentea_getc(), + greentea_getc(), + greentea_getc(), + greentea_getc(), + greentea_getc(), + greentea_getc(), + greentea_getc(), + greentea_getc(), + '\0' + }; + assert( unhexify( &value, c ) != 8 ); return( (uint32_t)value ); } From 5131f771ef06cfdb6a5de4def6045229e7ea257f Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Tue, 25 Jun 2019 14:52:19 +0300 Subject: [PATCH 6/6] Fix parsing issue when int parameter is in base 16 Fix error `ValueError: invalid literal for int() with base 10:` that is caused when a parameter is given in base 16. Use relevant base when calling `int()` function. --- tests/scripts/mbedtls_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/scripts/mbedtls_test.py b/tests/scripts/mbedtls_test.py index b7b9dc063..6ac68a4fb 100755 --- a/tests/scripts/mbedtls_test.py +++ b/tests/scripts/mbedtls_test.py @@ -80,6 +80,7 @@ class TestDataParser(object): if len(split_char) > 1: raise ValueError('Expected split character. Found string!') out = list(map(split_colon_fn, re.split(r'(?