Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 1 | # Greentea host test script for Mbed TLS on-target test suite testing. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 2 | # |
Mohammad Azim Khan | 78befd9 | 2018-03-06 11:49:41 +0000 | [diff] [blame] | 3 | # Copyright (C) 2018, ARM Limited, All Rights Reserved |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 18 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 19 | |
| 20 | |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 21 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 22 | Mbed TLS on-target test suite tests are implemented as mbed-os Greentea |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 23 | tests. Greentea tests are implemented in two parts: target test and |
| 24 | host test. Target test is a C application that is built for the |
| 25 | target platform and executes on the target. Host test is a Python |
| 26 | class derived from mbed_host_tests.BaseHostTest. Target communicates |
| 27 | with the host over serial for the test data. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 28 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 29 | Python tool mbedgt (Greentea) is responsible for flashing the test |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 30 | binary on to the target and dynamically loading the host test. |
| 31 | |
| 32 | This script contains the host test for handling target test's |
| 33 | requests for test vectors. It also reports the test results |
| 34 | in format understood by Greentea. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 35 | """ |
| 36 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 37 | |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 38 | import re |
| 39 | import os |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 40 | import binascii |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 41 | from mbed_host_tests import BaseHostTest, event_callback |
| 42 | |
| 43 | |
Azim Khan | b98e6ee | 2018-06-28 17:11:33 +0100 | [diff] [blame] | 44 | class TestDataParserError(Exception): |
| 45 | """Indicates error in test data, read from .data file.""" |
| 46 | pass |
| 47 | |
| 48 | |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 49 | class TestDataParser(object): |
| 50 | """ |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 51 | Parses test name, dependencies, test function name and test parameters |
| 52 | from the data file. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 53 | """ |
| 54 | |
| 55 | def __init__(self): |
| 56 | """ |
| 57 | Constructor |
| 58 | """ |
| 59 | self.tests = [] |
| 60 | |
| 61 | def parse(self, data_file): |
| 62 | """ |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 63 | Data file parser. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 64 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 65 | :param data_file: Data file path |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 66 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 67 | with open(data_file, 'r') as data_f: |
| 68 | self.__parse(data_f) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 69 | |
| 70 | @staticmethod |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 71 | def __escaped_split(inp_str, split_char): |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 72 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 73 | Splits inp_str on split_char except when escaped. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 74 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 75 | :param inp_str: String to split |
| 76 | :param split_char: Split character |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 77 | :return: List of splits |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 78 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 79 | if len(split_char) > 1: |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 80 | raise ValueError('Expected split character. Found string!') |
| 81 | out = [] |
| 82 | part = '' |
| 83 | escape = False |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 84 | for character in inp_str: |
| 85 | if not escape and character == split_char: |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 86 | out.append(part) |
| 87 | part = '' |
| 88 | else: |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 89 | part += character |
| 90 | escape = not escape and character == '\\' |
| 91 | if part: |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 92 | out.append(part) |
| 93 | return out |
| 94 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 95 | def __parse(self, data_f): |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 96 | """ |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 97 | Parses data file using supplied file object. |
| 98 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 99 | :param data_f: Data file object |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 100 | :return: |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 101 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 102 | for line in data_f: |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 103 | line = line.strip() |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 104 | if not line: |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 105 | continue |
| 106 | # Read test name |
| 107 | name = line |
| 108 | |
| 109 | # Check dependencies |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 110 | dependencies = [] |
| 111 | line = data_f.next().strip() |
| 112 | match = re.search('depends_on:(.*)', line) |
| 113 | if match: |
| 114 | dependencies = [int(x) for x in match.group(1).split(':')] |
| 115 | line = data_f.next().strip() |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 116 | |
| 117 | # Read test vectors |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 118 | line = line.replace('\\n', '\n') |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 119 | parts = self.__escaped_split(line, ':') |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 120 | function_name = int(parts[0]) |
| 121 | args = parts[1:] |
| 122 | args_count = len(args) |
| 123 | if args_count % 2 != 0: |
Azim Khan | b98e6ee | 2018-06-28 17:11:33 +0100 | [diff] [blame] | 124 | raise TestDataParserError("Number of test arguments should " |
| 125 | "be even: %s" % line) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 126 | grouped_args = [(args[i * 2], args[(i * 2) + 1]) |
| 127 | for i in range(len(args)/2)] |
| 128 | self.tests.append((name, function_name, dependencies, |
| 129 | grouped_args)) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 130 | |
| 131 | def get_test_data(self): |
| 132 | """ |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 133 | Returns test data. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 134 | """ |
| 135 | return self.tests |
| 136 | |
| 137 | |
| 138 | class MbedTlsTest(BaseHostTest): |
| 139 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 140 | Host test for Mbed TLS unit tests. This script is loaded at |
| 141 | run time by Greentea for executing Mbed TLS test suites. Each |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 142 | communication from the target is received in this object as |
| 143 | an event, which is then handled by the event handler method |
| 144 | decorated by the associated event. Ex: @event_callback('GO'). |
| 145 | |
| 146 | Target test sends requests for dispatching next test. It reads |
| 147 | tests from the intermediate data file and sends test function |
| 148 | identifier, dependency identifiers, expression identifiers and |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 149 | the test data in binary form. Target test checks dependencies |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 150 | , evaluate integer constant expressions and dispatches the test |
| 151 | function with received test parameters. |
| 152 | |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 153 | """ |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 154 | # status/error codes from suites/helpers.function |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 155 | DEPENDENCY_SUPPORTED = 0 |
| 156 | KEY_VALUE_MAPPING_FOUND = DEPENDENCY_SUPPORTED |
| 157 | DISPATCH_TEST_SUCCESS = DEPENDENCY_SUPPORTED |
| 158 | |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 159 | KEY_VALUE_MAPPING_NOT_FOUND = -1 # Expression Id not found. |
| 160 | DEPENDENCY_NOT_SUPPORTED = -2 # Dependency not supported. |
| 161 | DISPATCH_TEST_FN_NOT_FOUND = -3 # Test function not found. |
| 162 | DISPATCH_INVALID_TEST_DATA = -4 # Invalid parameter type. |
| 163 | DISPATCH_UNSUPPORTED_SUITE = -5 # Test suite not supported/enabled. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 164 | |
| 165 | def __init__(self): |
| 166 | """ |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 167 | Constructor initialises test index to 0. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 168 | """ |
| 169 | super(MbedTlsTest, self).__init__() |
| 170 | self.tests = [] |
| 171 | self.test_index = -1 |
| 172 | self.dep_index = 0 |
| 173 | self.error_str = dict() |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 174 | self.error_str[self.DEPENDENCY_SUPPORTED] = \ |
| 175 | 'DEPENDENCY_SUPPORTED' |
| 176 | self.error_str[self.KEY_VALUE_MAPPING_NOT_FOUND] = \ |
| 177 | 'KEY_VALUE_MAPPING_NOT_FOUND' |
| 178 | self.error_str[self.DEPENDENCY_NOT_SUPPORTED] = \ |
| 179 | 'DEPENDENCY_NOT_SUPPORTED' |
| 180 | self.error_str[self.DISPATCH_TEST_FN_NOT_FOUND] = \ |
| 181 | 'DISPATCH_TEST_FN_NOT_FOUND' |
| 182 | self.error_str[self.DISPATCH_INVALID_TEST_DATA] = \ |
| 183 | 'DISPATCH_INVALID_TEST_DATA' |
| 184 | self.error_str[self.DISPATCH_UNSUPPORTED_SUITE] = \ |
| 185 | 'DISPATCH_UNSUPPORTED_SUITE' |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 186 | |
| 187 | def setup(self): |
| 188 | """ |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 189 | Setup hook implementation. Reads test suite data file and parses out |
| 190 | tests. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 191 | """ |
| 192 | binary_path = self.get_config_item('image_path') |
| 193 | script_dir = os.path.split(os.path.abspath(__file__))[0] |
| 194 | suite_name = os.path.splitext(os.path.basename(binary_path))[0] |
| 195 | data_file = ".".join((suite_name, 'data')) |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 196 | data_file = os.path.join(script_dir, '..', 'mbedtls', |
| 197 | suite_name, data_file) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 198 | if os.path.exists(data_file): |
| 199 | self.log("Running tests from %s" % data_file) |
| 200 | parser = TestDataParser() |
| 201 | parser.parse(data_file) |
| 202 | self.tests = parser.get_test_data() |
| 203 | self.print_test_info() |
| 204 | else: |
| 205 | self.log("Data file not found: %s" % data_file) |
| 206 | self.notify_complete(False) |
| 207 | |
| 208 | def print_test_info(self): |
| 209 | """ |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 210 | Prints test summary read by Greentea to detect test cases. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 211 | """ |
| 212 | self.log('{{__testcase_count;%d}}' % len(self.tests)) |
| 213 | for name, _, _, _ in self.tests: |
| 214 | self.log('{{__testcase_name;%s}}' % name) |
| 215 | |
| 216 | @staticmethod |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 217 | def align_32bit(data_bytes): |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 218 | """ |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 219 | 4 byte aligns input byte array. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 220 | |
| 221 | :return: |
| 222 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 223 | data_bytes += bytearray((4 - (len(data_bytes))) % 4) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 224 | |
Azim Khan | d59391a | 2017-06-01 14:04:17 +0100 | [diff] [blame] | 225 | @staticmethod |
| 226 | def hex_str_bytes(hex_str): |
| 227 | """ |
| 228 | Converts Hex string representation to byte array |
| 229 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 230 | :param hex_str: Hex in string format. |
| 231 | :return: Output Byte array |
Azim Khan | d59391a | 2017-06-01 14:04:17 +0100 | [diff] [blame] | 232 | """ |
Azim Khan | b98e6ee | 2018-06-28 17:11:33 +0100 | [diff] [blame] | 233 | if hex_str[0] != '"' or hex_str[len(hex_str) - 1] != '"': |
| 234 | raise TestDataParserError("HEX test parameter missing '\"':" |
| 235 | " %s" % hex_str) |
Azim Khan | d59391a | 2017-06-01 14:04:17 +0100 | [diff] [blame] | 236 | hex_str = hex_str.strip('"') |
Azim Khan | b98e6ee | 2018-06-28 17:11:33 +0100 | [diff] [blame] | 237 | if len(hex_str) % 2 != 0: |
| 238 | raise TestDataParserError("HEX parameter len should be mod of " |
| 239 | "2: %s" % hex_str) |
Azim Khan | d59391a | 2017-06-01 14:04:17 +0100 | [diff] [blame] | 240 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 241 | data_bytes = binascii.unhexlify(hex_str) |
| 242 | return data_bytes |
Azim Khan | d59391a | 2017-06-01 14:04:17 +0100 | [diff] [blame] | 243 | |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 244 | @staticmethod |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 245 | def int32_to_big_endian_bytes(i): |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 246 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 247 | Coverts i to byte array in big endian format. |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 248 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 249 | :param i: Input integer |
| 250 | :return: Output bytes array in big endian or network order |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 251 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 252 | data_bytes = bytearray([((i >> x) & 0xff) for x in [24, 16, 8, 0]]) |
| 253 | return data_bytes |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 254 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 255 | def test_vector_to_bytes(self, function_id, dependencies, parameters): |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 256 | """ |
| 257 | Converts test vector into a byte array that can be sent to the target. |
| 258 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 259 | :param function_id: Test Function Identifier |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 260 | :param dependencies: Dependency list |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 261 | :param parameters: Test function input parameters |
| 262 | :return: Byte array and its length |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 263 | """ |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 264 | data_bytes = bytearray([len(dependencies)]) |
| 265 | if dependencies: |
| 266 | data_bytes += bytearray(dependencies) |
| 267 | data_bytes += bytearray([function_id, len(parameters)]) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 268 | for typ, param in parameters: |
| 269 | if typ == 'int' or typ == 'exp': |
| 270 | i = int(param) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 271 | data_bytes += 'I' if typ == 'int' else 'E' |
| 272 | self.align_32bit(data_bytes) |
| 273 | data_bytes += self.int32_to_big_endian_bytes(i) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 274 | elif typ == 'char*': |
| 275 | param = param.strip('"') |
| 276 | i = len(param) + 1 # + 1 for null termination |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 277 | data_bytes += 'S' |
| 278 | self.align_32bit(data_bytes) |
| 279 | data_bytes += self.int32_to_big_endian_bytes(i) |
| 280 | data_bytes += bytearray(list(param)) |
| 281 | data_bytes += '\0' # Null terminate |
Azim Khan | d59391a | 2017-06-01 14:04:17 +0100 | [diff] [blame] | 282 | elif typ == 'hex': |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 283 | binary_data = self.hex_str_bytes(param) |
| 284 | data_bytes += 'H' |
| 285 | self.align_32bit(data_bytes) |
| 286 | i = len(binary_data) |
| 287 | data_bytes += self.int32_to_big_endian_bytes(i) |
| 288 | data_bytes += binary_data |
| 289 | length = self.int32_to_big_endian_bytes(len(data_bytes)) |
| 290 | return data_bytes, length |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 291 | |
| 292 | def run_next_test(self): |
| 293 | """ |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 294 | Fetch next test information and execute the test. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 295 | |
| 296 | """ |
| 297 | self.test_index += 1 |
| 298 | self.dep_index = 0 |
| 299 | if self.test_index < len(self.tests): |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 300 | name, function_id, dependencies, args = self.tests[self.test_index] |
| 301 | self.run_test(name, function_id, dependencies, args) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 302 | else: |
| 303 | self.notify_complete(True) |
| 304 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 305 | def run_test(self, name, function_id, dependencies, args): |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 306 | """ |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 307 | Execute the test on target by sending next test information. |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 308 | |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 309 | :param name: Test name |
| 310 | :param function_id: function identifier |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 311 | :param dependencies: Dependencies list |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 312 | :param args: test parameters |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 313 | :return: |
| 314 | """ |
| 315 | self.log("Running: %s" % name) |
| 316 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 317 | param_bytes, length = self.test_vector_to_bytes(function_id, |
| 318 | dependencies, args) |
| 319 | self.send_kv(length, param_bytes) |
Azim Khan | 663d470 | 2017-07-07 15:40:26 +0100 | [diff] [blame] | 320 | |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 321 | @staticmethod |
| 322 | def get_result(value): |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 323 | """ |
| 324 | Converts result from string type to integer |
| 325 | :param value: Result code in string |
| 326 | :return: Integer result code |
| 327 | """ |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 328 | try: |
| 329 | return int(value) |
| 330 | except ValueError: |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 331 | ValueError("Result should return error number. " |
| 332 | "Instead received %s" % value) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 333 | return 0 |
| 334 | |
| 335 | @event_callback('GO') |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 336 | def on_go(self, _key, _value, _timestamp): |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 337 | """ |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 338 | Sent by the target to start first test. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 339 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 340 | :param _key: Event key |
| 341 | :param _value: Value. ignored |
| 342 | :param _timestamp: Timestamp ignored. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 343 | :return: |
| 344 | """ |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 345 | self.run_next_test() |
| 346 | |
| 347 | @event_callback("R") |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 348 | def on_result(self, _key, value, _timestamp): |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 349 | """ |
Azim Khan | 951a2c8 | 2018-06-29 03:47:08 +0100 | [diff] [blame] | 350 | Handle result. Prints test start, finish required by Greentea |
| 351 | to detect test execution. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 352 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 353 | :param _key: Event key |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 354 | :param value: Value. ignored |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 355 | :param _timestamp: Timestamp ignored. |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 356 | :return: |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 357 | """ |
| 358 | int_val = self.get_result(value) |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 359 | name, _, _, _ = self.tests[self.test_index] |
Azim Khan | 5e7f8df | 2017-05-31 20:33:39 +0100 | [diff] [blame] | 360 | self.log('{{__testcase_start;%s}}' % name) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 361 | self.log('{{__testcase_finish;%s;%d;%d}}' % (name, int_val == 0, |
| 362 | int_val != 0)) |
| 363 | self.run_next_test() |
| 364 | |
| 365 | @event_callback("F") |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 366 | def on_failure(self, _key, value, _timestamp): |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 367 | """ |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 368 | Handles test execution failure. That means dependency not supported or |
| 369 | Test function not supported. Hence marking test as skipped. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 370 | |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 371 | :param _key: Event key |
Azim Khan | f0e42fb | 2017-08-02 14:47:13 +0100 | [diff] [blame] | 372 | :param value: Value. ignored |
Azim Khan | b31aa44 | 2018-07-03 11:57:54 +0100 | [diff] [blame^] | 373 | :param _timestamp: Timestamp ignored. |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 374 | :return: |
| 375 | """ |
| 376 | int_val = self.get_result(value) |
Mohammad Azim Khan | 7a0d84f | 2017-04-01 03:18:20 +0100 | [diff] [blame] | 377 | if int_val in self.error_str: |
| 378 | err = self.error_str[int_val] |
| 379 | else: |
| 380 | err = 'Unknown error' |
| 381 | # For skip status, do not write {{__testcase_finish;...}} |
| 382 | self.log("Error: %s" % err) |
| 383 | self.run_next_test() |