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)
This commit is contained in:
Ron Eldor 2019-06-03 11:38:42 +03:00
parent 56b6e523fa
commit b43fe57d34
2 changed files with 50 additions and 21 deletions

View File

@ -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):

View File

@ -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++ )