2017-08-02 15:47:13 +02:00
|
|
|
# Greentea host test script for on-target tests.
|
|
|
|
#
|
2018-03-06 12:49:41 +01:00
|
|
|
# Copyright (C) 2018, ARM Limited, All Rights Reserved
|
2017-08-02 15:47:13 +02:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
# This file is part of mbed TLS (https://tls.mbed.org)
|
2017-04-01 04:18:20 +02:00
|
|
|
|
2017-07-19 11:15:54 +02:00
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
"""
|
|
|
|
Greentea host test script for on-target tests.
|
2017-07-19 11:15:54 +02:00
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
Host test script for testing mbed TLS test suites on target. Implements
|
|
|
|
BaseHostTest to handle key,value pairs (events) coming from mbed TLS
|
|
|
|
tests. Reads data file corresponding to the executing binary and dispatches
|
|
|
|
test cases.
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
|
2017-04-01 04:18:20 +02:00
|
|
|
import re
|
|
|
|
import os
|
2017-07-07 16:40:26 +02:00
|
|
|
import binascii
|
2017-04-01 04:18:20 +02:00
|
|
|
from mbed_host_tests import BaseHostTest, event_callback
|
|
|
|
|
|
|
|
|
|
|
|
class TestDataParser(object):
|
|
|
|
"""
|
|
|
|
parser for mbedtls test data files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Constructor
|
|
|
|
"""
|
|
|
|
self.tests = []
|
|
|
|
|
|
|
|
def parse(self, data_file):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
Data file parser.
|
2017-04-01 04:18:20 +02:00
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
:param data_file: Data file path
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
|
|
|
with open(data_file, 'r') as f:
|
|
|
|
self.__parse(f)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __escaped_split(str, ch):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
Splits str on ch except when escaped.
|
|
|
|
|
|
|
|
:param str: String to split
|
|
|
|
:param ch: Split character
|
|
|
|
:return: List of splits
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
|
|
|
if len(ch) > 1:
|
|
|
|
raise ValueError('Expected split character. Found string!')
|
|
|
|
out = []
|
|
|
|
part = ''
|
|
|
|
escape = False
|
|
|
|
for i in range(len(str)):
|
|
|
|
if not escape and str[i] == ch:
|
|
|
|
out.append(part)
|
|
|
|
part = ''
|
|
|
|
else:
|
|
|
|
part += str[i]
|
|
|
|
escape = not escape and str[i] == '\\'
|
|
|
|
if len(part):
|
|
|
|
out.append(part)
|
|
|
|
return out
|
|
|
|
|
|
|
|
def __parse(self, file):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
Parses data file using supplied file object.
|
|
|
|
|
|
|
|
:param file: Data file object
|
|
|
|
:return:
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
2017-07-07 16:40:26 +02:00
|
|
|
for line in file:
|
2017-04-01 04:18:20 +02:00
|
|
|
line = line.strip()
|
|
|
|
if len(line) == 0:
|
|
|
|
continue
|
|
|
|
# Read test name
|
|
|
|
name = line
|
|
|
|
|
|
|
|
# Check dependencies
|
|
|
|
deps = []
|
2017-07-07 16:40:26 +02:00
|
|
|
line = file.next().strip()
|
2017-04-01 04:18:20 +02:00
|
|
|
m = re.search('depends_on\:(.*)', line)
|
|
|
|
if m:
|
|
|
|
deps = [int(x) for x in m.group(1).split(':')]
|
2017-07-07 16:40:26 +02:00
|
|
|
line = file.next().strip()
|
2017-04-01 04:18:20 +02:00
|
|
|
|
|
|
|
# Read test vectors
|
2017-07-07 16:40:26 +02:00
|
|
|
line = line.replace('\\n', '\n')
|
2017-04-01 04:18:20 +02:00
|
|
|
parts = self.__escaped_split(line, ':')
|
|
|
|
function = int(parts[0])
|
|
|
|
x = parts[1:]
|
|
|
|
l = len(x)
|
|
|
|
assert l % 2 == 0, "Number of test arguments should be even: %s" % line
|
|
|
|
args = [(x[i * 2], x[(i * 2) + 1]) for i in range(len(x)/2)]
|
|
|
|
self.tests.append((name, function, deps, args))
|
|
|
|
|
|
|
|
def get_test_data(self):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
Returns test data.
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
|
|
|
return self.tests
|
|
|
|
|
|
|
|
|
|
|
|
class MbedTlsTest(BaseHostTest):
|
|
|
|
"""
|
2017-07-07 16:40:26 +02:00
|
|
|
Event handler for mbedtls unit tests. This script is loaded at run time
|
|
|
|
by htrun while executing mbedtls unit tests.
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
|
|
|
# From suites/helpers.function
|
|
|
|
DEPENDENCY_SUPPORTED = 0
|
|
|
|
KEY_VALUE_MAPPING_FOUND = DEPENDENCY_SUPPORTED
|
|
|
|
DISPATCH_TEST_SUCCESS = DEPENDENCY_SUPPORTED
|
|
|
|
|
|
|
|
KEY_VALUE_MAPPING_NOT_FOUND = -1
|
|
|
|
DEPENDENCY_NOT_SUPPORTED = -2
|
|
|
|
DISPATCH_TEST_FN_NOT_FOUND = -3
|
|
|
|
DISPATCH_INVALID_TEST_DATA = -4
|
|
|
|
DISPATCH_UNSUPPORTED_SUITE = -5
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
Constructor initialises test index to 0.
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
|
|
|
super(MbedTlsTest, self).__init__()
|
|
|
|
self.tests = []
|
|
|
|
self.test_index = -1
|
|
|
|
self.dep_index = 0
|
|
|
|
self.error_str = dict()
|
|
|
|
self.error_str[self.DEPENDENCY_SUPPORTED] = 'DEPENDENCY_SUPPORTED'
|
|
|
|
self.error_str[self.KEY_VALUE_MAPPING_NOT_FOUND] = 'KEY_VALUE_MAPPING_NOT_FOUND'
|
|
|
|
self.error_str[self.DEPENDENCY_NOT_SUPPORTED] = 'DEPENDENCY_NOT_SUPPORTED'
|
|
|
|
self.error_str[self.DISPATCH_TEST_FN_NOT_FOUND] = 'DISPATCH_TEST_FN_NOT_FOUND'
|
|
|
|
self.error_str[self.DISPATCH_INVALID_TEST_DATA] = 'DISPATCH_INVALID_TEST_DATA'
|
|
|
|
self.error_str[self.DISPATCH_UNSUPPORTED_SUITE] = 'DISPATCH_UNSUPPORTED_SUITE'
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
Setup hook implementation. Reads test suite data file and parses out tests.
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
|
|
|
binary_path = self.get_config_item('image_path')
|
|
|
|
script_dir = os.path.split(os.path.abspath(__file__))[0]
|
|
|
|
suite_name = os.path.splitext(os.path.basename(binary_path))[0]
|
|
|
|
data_file = ".".join((suite_name, 'data'))
|
|
|
|
data_file = os.path.join(script_dir, '..', 'mbedtls', suite_name, data_file)
|
|
|
|
if os.path.exists(data_file):
|
|
|
|
self.log("Running tests from %s" % data_file)
|
|
|
|
parser = TestDataParser()
|
|
|
|
parser.parse(data_file)
|
|
|
|
self.tests = parser.get_test_data()
|
|
|
|
self.print_test_info()
|
|
|
|
else:
|
|
|
|
self.log("Data file not found: %s" % data_file)
|
|
|
|
self.notify_complete(False)
|
|
|
|
|
|
|
|
def print_test_info(self):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
Prints test summary read by Greentea to detect test cases.
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
|
|
|
self.log('{{__testcase_count;%d}}' % len(self.tests))
|
|
|
|
for name, _, _, _ in self.tests:
|
|
|
|
self.log('{{__testcase_name;%s}}' % name)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def align_32bit(b):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
4 byte aligns input byte array.
|
2017-04-01 04:18:20 +02:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
b += bytearray((4 - (len(b))) % 4)
|
|
|
|
|
2017-06-01 15:04:17 +02:00
|
|
|
@staticmethod
|
|
|
|
def hex_str_bytes(hex_str):
|
|
|
|
"""
|
|
|
|
Converts Hex string representation to byte array
|
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
:param hex_str: Hex in string format.
|
|
|
|
:return: Output Byte array
|
2017-06-01 15:04:17 +02:00
|
|
|
"""
|
|
|
|
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
|
2017-07-07 16:40:26 +02:00
|
|
|
|
|
|
|
b = binascii.unhexlify(hex_str)
|
|
|
|
return b
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def int32_to_bigendian_bytes(i):
|
|
|
|
"""
|
|
|
|
Coverts i to bytearray in big endian format.
|
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
:param i: Input integer
|
|
|
|
:return: Output bytes array in big endian or network order
|
2017-07-07 16:40:26 +02:00
|
|
|
"""
|
|
|
|
b = bytearray([((i >> x) & 0xff) for x in [24, 16, 8, 0]])
|
2017-06-01 15:04:17 +02:00
|
|
|
return b
|
|
|
|
|
2017-07-07 16:40:26 +02:00
|
|
|
def test_vector_to_bytes(self, function_id, deps, parameters):
|
|
|
|
"""
|
|
|
|
Converts test vector into a byte array that can be sent to the target.
|
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
:param function_id: Test Function Identifier
|
|
|
|
:param deps: Dependency list
|
|
|
|
:param parameters: Test function input parameters
|
|
|
|
:return: Byte array and its length
|
2017-07-07 16:40:26 +02:00
|
|
|
"""
|
|
|
|
b = bytearray([len(deps)])
|
|
|
|
if len(deps):
|
|
|
|
b += bytearray(deps)
|
|
|
|
b += bytearray([function_id, len(parameters)])
|
2017-04-01 04:18:20 +02:00
|
|
|
for typ, param in parameters:
|
|
|
|
if typ == 'int' or typ == 'exp':
|
|
|
|
i = int(param)
|
|
|
|
b += 'I' if typ == 'int' else 'E'
|
|
|
|
self.align_32bit(b)
|
2017-07-07 16:40:26 +02:00
|
|
|
b += self.int32_to_bigendian_bytes(i)
|
2017-04-01 04:18:20 +02:00
|
|
|
elif typ == 'char*':
|
|
|
|
param = param.strip('"')
|
|
|
|
i = len(param) + 1 # + 1 for null termination
|
|
|
|
b += 'S'
|
|
|
|
self.align_32bit(b)
|
2017-07-07 16:40:26 +02:00
|
|
|
b += self.int32_to_bigendian_bytes(i)
|
2017-04-01 04:18:20 +02:00
|
|
|
b += bytearray(list(param))
|
|
|
|
b += '\0' # Null terminate
|
2017-06-01 15:04:17 +02:00
|
|
|
elif typ == 'hex':
|
|
|
|
hb = self.hex_str_bytes(param)
|
|
|
|
b += 'H'
|
|
|
|
self.align_32bit(b)
|
|
|
|
i = len(hb)
|
2017-07-07 16:40:26 +02:00
|
|
|
b += self.int32_to_bigendian_bytes(i)
|
2017-06-01 15:04:17 +02:00
|
|
|
b += hb
|
2017-07-07 16:40:26 +02:00
|
|
|
length = self.int32_to_bigendian_bytes(len(b))
|
|
|
|
return b, length
|
2017-04-01 04:18:20 +02:00
|
|
|
|
|
|
|
def run_next_test(self):
|
|
|
|
"""
|
|
|
|
Send next test function to the target.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.test_index += 1
|
|
|
|
self.dep_index = 0
|
|
|
|
if self.test_index < len(self.tests):
|
2017-07-07 16:40:26 +02:00
|
|
|
name, function_id, deps, args = self.tests[self.test_index]
|
|
|
|
self.run_test(name, function_id, deps, args)
|
2017-04-01 04:18:20 +02:00
|
|
|
else:
|
|
|
|
self.notify_complete(True)
|
|
|
|
|
2017-07-07 16:40:26 +02:00
|
|
|
def run_test(self, name, function_id, deps, args):
|
|
|
|
"""
|
|
|
|
Runs the test.
|
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
:param name: Test name
|
|
|
|
:param function_id: function identifier
|
|
|
|
:param deps: Dependencies list
|
|
|
|
:param args: test parameters
|
2017-07-07 16:40:26 +02:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
self.log("Running: %s" % name)
|
|
|
|
|
|
|
|
bytes, length = self.test_vector_to_bytes(function_id, deps, args)
|
|
|
|
self.send_kv(length, bytes)
|
|
|
|
|
2017-04-01 04:18:20 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_result(value):
|
2017-08-02 15:47:13 +02:00
|
|
|
"""
|
|
|
|
Converts result from string type to integer
|
|
|
|
:param value: Result code in string
|
|
|
|
:return: Integer result code
|
|
|
|
"""
|
2017-04-01 04:18:20 +02:00
|
|
|
try:
|
|
|
|
return int(value)
|
|
|
|
except ValueError:
|
|
|
|
ValueError("Result should return error number. Instead received %s" % value)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@event_callback('GO')
|
|
|
|
def on_go(self, key, value, timestamp):
|
2017-08-02 15:47:13 +02:00
|
|
|
"""
|
|
|
|
Called on key "GO". Kicks off test execution.
|
|
|
|
|
|
|
|
:param key: Event key
|
|
|
|
:param value: Value. ignored
|
|
|
|
:param timestamp: Timestamp ignored.
|
|
|
|
:return:
|
|
|
|
"""
|
2017-04-01 04:18:20 +02:00
|
|
|
self.run_next_test()
|
|
|
|
|
|
|
|
@event_callback("R")
|
|
|
|
def on_result(self, key, value, timestamp):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
Handle result. Prints test start, finish prints required by Greentea to detect test execution.
|
2017-04-01 04:18:20 +02:00
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
:param key: Event key
|
|
|
|
:param value: Value. ignored
|
|
|
|
:param timestamp: Timestamp ignored.
|
|
|
|
:return:
|
2017-04-01 04:18:20 +02:00
|
|
|
"""
|
|
|
|
int_val = self.get_result(value)
|
|
|
|
name, function, deps, args = self.tests[self.test_index]
|
2017-05-31 21:33:39 +02:00
|
|
|
self.log('{{__testcase_start;%s}}' % name)
|
2017-04-01 04:18:20 +02:00
|
|
|
self.log('{{__testcase_finish;%s;%d;%d}}' % (name, int_val == 0,
|
|
|
|
int_val != 0))
|
|
|
|
self.run_next_test()
|
|
|
|
|
|
|
|
@event_callback("F")
|
|
|
|
def on_failure(self, key, value, timestamp):
|
|
|
|
"""
|
2017-08-02 15:47:13 +02:00
|
|
|
Handles test execution failure. That means dependency not supported or
|
|
|
|
Test function not supported. Hence marking test as skipped.
|
2017-04-01 04:18:20 +02:00
|
|
|
|
2017-08-02 15:47:13 +02:00
|
|
|
:param key: Event key
|
|
|
|
:param value: Value. ignored
|
|
|
|
:param timestamp: Timestamp ignored.
|
2017-04-01 04:18:20 +02:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
int_val = self.get_result(value)
|
|
|
|
name, function, deps, args = self.tests[self.test_index]
|
|
|
|
if int_val in self.error_str:
|
|
|
|
err = self.error_str[int_val]
|
|
|
|
else:
|
|
|
|
err = 'Unknown error'
|
|
|
|
# For skip status, do not write {{__testcase_finish;...}}
|
|
|
|
self.log("Error: %s" % err)
|
|
|
|
self.run_next_test()
|