Release Mbed Crypto 0.1.0a
diff --git a/tests/scripts/generate_test_code.py b/tests/scripts/generate_test_code.py
new file mode 100755
index 0000000..345df53
--- /dev/null
+++ b/tests/scripts/generate_test_code.py
@@ -0,0 +1,729 @@
+#!/usr/bin/env python3
+# Test suites code generator.
+#
+# Copyright (C) 2018, ARM Limited, All Rights Reserved
+# 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 Crypto (https://tls.mbed.org)
+
+"""
+Test Suite code generator.
+
+Generates a test source file using following input files:
+
+test_suite_xyz.function - Read test functions from test suite functions file.
+test_suite_xyz.data - Read test functions and their dependencies to generate
+ dispatch and dependency check code.
+main template - Substitute generated test function dispatch code, dependency
+ checking code.
+platform .function - Read host or target platform implementation for
+ dispatching test cases from .data file.
+helper .function - Read common reusable functions.
+"""
+
+
+import io
+import os
+import re
+import sys
+import argparse
+import shutil
+
+
+BEGIN_HEADER_REGEX = '/\*\s*BEGIN_HEADER\s*\*/'
+END_HEADER_REGEX = '/\*\s*END_HEADER\s*\*/'
+
+BEGIN_SUITE_HELPERS_REGEX = '/\*\s*BEGIN_SUITE_HELPERS\s*\*/'
+END_SUITE_HELPERS_REGEX = '/\*\s*END_SUITE_HELPERS\s*\*/'
+
+BEGIN_DEP_REGEX = 'BEGIN_DEPENDENCIES'
+END_DEP_REGEX = 'END_DEPENDENCIES'
+
+BEGIN_CASE_REGEX = '/\*\s*BEGIN_CASE\s*(.*?)\s*\*/'
+END_CASE_REGEX = '/\*\s*END_CASE\s*\*/'
+
+
+class InvalidFileFormat(Exception):
+ """
+ Exception to indicate invalid file format.
+ """
+ pass
+
+
+class FileWrapper(io.FileIO):
+ """
+ File wrapper class. Provides reading with line no. tracking.
+ """
+
+ def __init__(self, file_name):
+ """
+ Init file handle.
+
+ :param file_name: File path to open.
+ """
+ super(FileWrapper, self).__init__(file_name, 'r')
+ self.line_no = 0
+
+ # Override the generator function in a way that works in both Python 2
+ # and Python 3.
+ def __next__(self):
+ """
+ Iterator return impl.
+ :return: Line read from file.
+ """
+ parent = super(FileWrapper, self)
+ if hasattr(parent, '__next__'):
+ line = parent.__next__() # Python 3
+ else:
+ line = parent.next() # Python 2
+ if line:
+ self.line_no += 1
+ # Convert byte array to string with correct encoding
+ return line.decode(sys.getdefaultencoding())
+ return None
+ next = __next__
+
+
+def split_dep(dep):
+ """
+ Split NOT character '!' from dependency. Used by gen_deps()
+
+ :param dep: Dependency list
+ :return: list of tuples where index 0 has '!' if there was a '!' before the dependency string
+ """
+ return ('!', dep[1:]) if dep[0] == '!' else ('', dep)
+
+
+def gen_deps(deps):
+ """
+ Generates dependency i.e. if def and endif code
+
+ :param deps: List of dependencies.
+ :return: if defined and endif code with macro annotations for readability.
+ """
+ dep_start = ''.join(['#if %sdefined(%s)\n' % split_dep(x) for x in deps])
+ dep_end = ''.join(['#endif /* %s */\n' % x for x in reversed(deps)])
+
+ return dep_start, dep_end
+
+
+def gen_deps_one_line(deps):
+ """
+ Generates dependency checks in one line. Useful for writing code in #else case.
+
+ :param deps: List of dependencies.
+ :return: ifdef code
+ """
+ defines = ('#if ' if len(deps) else '') + ' && '.join(['%sdefined(%s)' % split_dep(x) for x in deps])
+ return defines
+
+
+def gen_function_wrapper(name, locals, args_dispatch):
+ """
+ Creates test function wrapper code. A wrapper has the code to unpack parameters from parameters[] array.
+
+ :param name: Test function name
+ :param locals: Local variables declaration code
+ :param args_dispatch: List of dispatch arguments. Ex: ['(char *)params[0]', '*((int *)params[1])']
+ :return: Test function wrapper.
+ """
+ # Then create the wrapper
+ wrapper = '''
+void {name}_wrapper( void ** params )
+{{
+{unused_params}{locals}
+ {name}( {args} );
+}}
+'''.format(name=name,
+ unused_params='' if args_dispatch else ' (void) params;\n',
+ args=', '.join(args_dispatch),
+ locals=locals)
+ return wrapper
+
+
+def gen_dispatch(name, deps):
+ """
+ Generates dispatch code for the test function table.
+
+ :param name: Test function name
+ :param deps: List of dependencies
+ :return: Dispatch code.
+ """
+ if len(deps):
+ ifdef = gen_deps_one_line(deps)
+ dispatch_code = '''
+{ifdef}
+ {name}_wrapper,
+#else
+ NULL,
+#endif
+'''.format(ifdef=ifdef, name=name)
+ else:
+ dispatch_code = '''
+ {name}_wrapper,
+'''.format(name=name)
+
+ return dispatch_code
+
+
+def parse_until_pattern(funcs_f, end_regex):
+ """
+ Parses function headers or helper code until end pattern.
+
+ :param funcs_f: file object for .functions file
+ :param end_regex: Pattern to stop parsing
+ :return: Test suite headers code
+ """
+ headers = '#line %d "%s"\n' % (funcs_f.line_no + 1, funcs_f.name)
+ for line in funcs_f:
+ if re.search(end_regex, line):
+ break
+ headers += line
+ else:
+ raise InvalidFileFormat("file: %s - end pattern [%s] not found!" % (funcs_f.name, end_regex))
+
+ return headers
+
+
+def parse_suite_deps(funcs_f):
+ """
+ Parses test suite dependencies.
+
+ :param funcs_f: file object for .functions file
+ :return: List of test suite dependencies.
+ """
+ deps = []
+ for line in funcs_f:
+ m = re.search('depends_on\:(.*)', line.strip())
+ if m:
+ deps += [x.strip() for x in m.group(1).split(':')]
+ if re.search(END_DEP_REGEX, line):
+ break
+ else:
+ raise InvalidFileFormat("file: %s - end dependency pattern [%s] not found!" % (funcs_f.name, END_DEP_REGEX))
+
+ return deps
+
+
+def parse_function_deps(line):
+ """
+ Parses function dependencies.
+
+ :param line: Line from .functions file that has dependencies.
+ :return: List of dependencies.
+ """
+ deps = []
+ m = re.search(BEGIN_CASE_REGEX, line)
+ dep_str = m.group(1)
+ if len(dep_str):
+ m = re.search('depends_on:(.*)', dep_str)
+ if m:
+ deps = [x.strip() for x in m.group(1).strip().split(':')]
+ return deps
+
+
+def parse_function_signature(line):
+ """
+ Parsing function signature
+
+ :param line: Line from .functions file that has a function signature.
+ :return: function name, argument list, local variables for wrapper function and argument dispatch code.
+ """
+ args = []
+ locals = ''
+ args_dispatch = []
+ m = re.search('\s*void\s+(\w+)\s*\(', line, re.I)
+ if not m:
+ raise ValueError("Test function should return 'void'\n%s" % line)
+ name = m.group(1)
+ line = line[len(m.group(0)):]
+ arg_idx = 0
+ for arg in line[:line.find(')')].split(','):
+ arg = arg.strip()
+ if arg == '':
+ continue
+ if re.search('int\s+.*', arg.strip()):
+ args.append('int')
+ args_dispatch.append('*( (int *) params[%d] )' % arg_idx)
+ elif re.search('char\s*\*\s*.*', arg.strip()):
+ args.append('char*')
+ args_dispatch.append('(char *) params[%d]' % arg_idx)
+ elif re.search('HexParam_t\s*\*\s*.*', arg.strip()):
+ args.append('hex')
+ # create a structure
+ locals += """ HexParam_t hex%d = {%s, %s};
+""" % (arg_idx, '(uint8_t *) params[%d]' % arg_idx, '*( (uint32_t *) params[%d] )' % (arg_idx + 1))
+
+ args_dispatch.append('&hex%d' % arg_idx)
+ arg_idx += 1
+ else:
+ raise ValueError("Test function arguments can only be 'int', 'char *' or 'HexParam_t'\n%s" % line)
+ arg_idx += 1
+
+ return name, args, locals, args_dispatch
+
+
+def parse_function_code(funcs_f, deps, suite_deps):
+ """
+ Parses out a function from function file object and generates function and dispatch code.
+
+ :param funcs_f: file object of the functions file.
+ :param deps: List of dependencies
+ :param suite_deps: List of test suite dependencies
+ :return: Function name, arguments, function code and dispatch code.
+ """
+ code = '#line %d "%s"\n' % (funcs_f.line_no + 1, funcs_f.name)
+ for line in funcs_f:
+ # Check function signature
+ m = re.match('.*?\s+(\w+)\s*\(', line, re.I)
+ if m:
+ # check if we have full signature i.e. split in more lines
+ if not re.match('.*\)', line):
+ for lin in funcs_f:
+ line += lin
+ if re.search('.*?\)', line):
+ break
+ name, args, locals, args_dispatch = parse_function_signature(line)
+ code += line.replace(name, 'test_' + name)
+ name = 'test_' + name
+ break
+ else:
+ raise InvalidFileFormat("file: %s - Test functions not found!" % funcs_f.name)
+
+ for line in funcs_f:
+ if re.search(END_CASE_REGEX, line):
+ break
+ code += line
+ else:
+ raise InvalidFileFormat("file: %s - end case pattern [%s] not found!" % (funcs_f.name, END_CASE_REGEX))
+
+ # Add exit label if not present
+ if code.find('exit:') == -1:
+ s = code.rsplit('}', 1)
+ if len(s) == 2:
+ code = """exit:
+ ;;
+}""".join(s)
+
+ code += gen_function_wrapper(name, locals, args_dispatch)
+ ifdef, endif = gen_deps(deps)
+ dispatch_code = gen_dispatch(name, suite_deps + deps)
+ return name, args, ifdef + code + endif, dispatch_code
+
+
+def parse_functions(funcs_f):
+ """
+ Returns functions code pieces
+
+ :param funcs_f: file object of the functions file.
+ :return: List of test suite dependencies, test function dispatch code, function code and
+ a dict with function identifiers and arguments info.
+ """
+ suite_headers = ''
+ suite_helpers = ''
+ suite_deps = []
+ suite_functions = ''
+ func_info = {}
+ function_idx = 0
+ dispatch_code = ''
+ for line in funcs_f:
+ if re.search(BEGIN_HEADER_REGEX, line):
+ headers = parse_until_pattern(funcs_f, END_HEADER_REGEX)
+ suite_headers += headers
+ elif re.search(BEGIN_SUITE_HELPERS_REGEX, line):
+ helpers = parse_until_pattern(funcs_f, END_SUITE_HELPERS_REGEX)
+ suite_helpers += helpers
+ elif re.search(BEGIN_DEP_REGEX, line):
+ deps = parse_suite_deps(funcs_f)
+ suite_deps += deps
+ elif re.search(BEGIN_CASE_REGEX, line):
+ deps = parse_function_deps(line)
+ func_name, args, func_code, func_dispatch = parse_function_code(funcs_f, deps, suite_deps)
+ suite_functions += func_code
+ # Generate dispatch code and enumeration info
+ assert func_name not in func_info, "file: %s - function %s re-declared at line %d" % \
+ (funcs_f.name, func_name, funcs_f.line_no)
+ func_info[func_name] = (function_idx, args)
+ dispatch_code += '/* Function Id: %d */\n' % function_idx
+ dispatch_code += func_dispatch
+ function_idx += 1
+
+ ifdef, endif = gen_deps(suite_deps)
+ func_code = ifdef + suite_headers + suite_helpers + suite_functions + endif
+ return suite_deps, dispatch_code, func_code, func_info
+
+
+def escaped_split(str, ch):
+ """
+ Split str on character ch but ignore escaped \{ch}
+ Since return value is used to write back to the intermediate data file.
+ Any escape characters in the input are retained in the output.
+
+ :param str: String to split
+ :param ch: split character
+ :return: List of splits
+ """
+ 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_test_data(data_f, debug=False):
+ """
+ Parses .data file
+
+ :param data_f: file object of the data file.
+ :return: Generator that yields test name, function name, dependency list and function argument list.
+ """
+ STATE_READ_NAME = 0
+ STATE_READ_ARGS = 1
+ state = STATE_READ_NAME
+ deps = []
+ name = ''
+ for line in data_f:
+ line = line.strip()
+ if len(line) and line[0] == '#': # Skip comments
+ continue
+
+ # Blank line indicates end of test
+ if len(line) == 0:
+ assert state != STATE_READ_ARGS, "Newline before arguments. " \
+ "Test function and arguments missing for %s" % name
+ continue
+
+ if state == STATE_READ_NAME:
+ # Read test name
+ name = line
+ state = STATE_READ_ARGS
+ elif state == STATE_READ_ARGS:
+ # Check dependencies
+ m = re.search('depends_on\:(.*)', line)
+ if m:
+ deps = [x.strip() for x in m.group(1).split(':') if len(x.strip())]
+ else:
+ # Read test vectors
+ parts = escaped_split(line, ':')
+ function = parts[0]
+ args = parts[1:]
+ yield name, function, deps, args
+ deps = []
+ state = STATE_READ_NAME
+ assert state != STATE_READ_ARGS, "Newline before arguments. " \
+ "Test function and arguments missing for %s" % name
+
+
+def gen_dep_check(dep_id, dep):
+ """
+ Generate code for the dependency.
+
+ :param dep_id: Dependency identifier
+ :param dep: Dependency macro
+ :return: Dependency check code
+ """
+ assert dep_id > -1, "Dependency Id should be a positive integer."
+ noT, dep = ('!', dep[1:]) if dep[0] == '!' else ('', dep)
+ assert len(dep) > 0, "Dependency should not be an empty string."
+ dep_check = '''
+ case {id}:
+ {{
+#if {noT}defined({macro})
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }}
+ break;'''.format(noT=noT, macro=dep, id=dep_id)
+ return dep_check
+
+
+def gen_expression_check(exp_id, exp):
+ """
+ Generates code for expression check
+
+ :param exp_id: Expression Identifier
+ :param exp: Expression/Macro
+ :return: Expression check code
+ """
+ assert exp_id > -1, "Expression Id should be a positive integer."
+ assert len(exp) > 0, "Expression should not be an empty string."
+ exp_code = '''
+ case {exp_id}:
+ {{
+ *out_value = {expression};
+ }}
+ break;'''.format(exp_id=exp_id, expression=exp)
+ return exp_code
+
+
+def write_deps(out_data_f, test_deps, unique_deps):
+ """
+ Write dependencies to intermediate test data file.
+ It also returns dependency check code.
+
+ :param out_data_f: Output intermediate data file
+ :param test_deps: Dependencies
+ :param unique_deps: Mutable list to track unique dependencies that are global to this re-entrant function.
+ :return: returns dependency check code.
+ """
+ dep_check_code = ''
+ if len(test_deps):
+ out_data_f.write('depends_on')
+ for dep in test_deps:
+ if dep not in unique_deps:
+ unique_deps.append(dep)
+ dep_id = unique_deps.index(dep)
+ dep_check_code += gen_dep_check(dep_id, dep)
+ else:
+ dep_id = unique_deps.index(dep)
+ out_data_f.write(':' + str(dep_id))
+ out_data_f.write('\n')
+ return dep_check_code
+
+
+def write_parameters(out_data_f, test_args, func_args, unique_expressions):
+ """
+ Writes test parameters to the intermediate data file.
+ Also generates expression code.
+
+ :param out_data_f: Output intermediate data file
+ :param test_args: Test parameters
+ :param func_args: Function arguments
+ :param unique_expressions: Mutable list to track unique expressions that are global to this re-entrant function.
+ :return: Returns expression check code.
+ """
+ expression_code = ''
+ for i in range(len(test_args)):
+ typ = func_args[i]
+ val = test_args[i]
+
+ # check if val is a non literal int val
+ if typ == 'int' and not re.match('(\d+$)|((0x)?[0-9a-fA-F]+$)', val): # its an expression
+ typ = 'exp'
+ if val not in unique_expressions:
+ unique_expressions.append(val)
+ # exp_id can be derived from len(). But for readability and consistency with case of existing let's
+ # use index().
+ exp_id = unique_expressions.index(val)
+ expression_code += gen_expression_check(exp_id, val)
+ val = exp_id
+ else:
+ val = unique_expressions.index(val)
+ out_data_f.write(':' + typ + ':' + str(val))
+ out_data_f.write('\n')
+ return expression_code
+
+
+def gen_suite_deps_checks(suite_deps, dep_check_code, expression_code):
+ """
+ Adds preprocessor checks for test suite dependencies.
+
+ :param suite_deps: Test suite dependencies read from the .functions file.
+ :param dep_check_code: Dependency check code
+ :param expression_code: Expression check code
+ :return: Dependency and expression code guarded by test suite dependencies.
+ """
+ if len(suite_deps):
+ ifdef = gen_deps_one_line(suite_deps)
+ dep_check_code = '''
+{ifdef}
+{code}
+#endif
+'''.format(ifdef=ifdef, code=dep_check_code)
+ expression_code = '''
+{ifdef}
+{code}
+#endif
+'''.format(ifdef=ifdef, code=expression_code)
+ return dep_check_code, expression_code
+
+
+def gen_from_test_data(data_f, out_data_f, func_info, suite_deps):
+ """
+ Generates dependency checks, expression code and intermediate data file from test data file.
+
+ :param data_f: Data file object
+ :param out_data_f:Output intermediate data file
+ :param func_info: Dict keyed by function and with function id and arguments info
+ :param suite_deps: Test suite deps
+ :return: Returns dependency and expression check code
+ """
+ unique_deps = []
+ unique_expressions = []
+ dep_check_code = ''
+ expression_code = ''
+ for test_name, function_name, test_deps, test_args in parse_test_data(data_f):
+ out_data_f.write(test_name + '\n')
+
+ # Write deps
+ dep_check_code += write_deps(out_data_f, test_deps, unique_deps)
+
+ # Write test function name
+ test_function_name = 'test_' + function_name
+ assert test_function_name in func_info, "Function %s not found!" % test_function_name
+ func_id, func_args = func_info[test_function_name]
+ out_data_f.write(str(func_id))
+
+ # Write parameters
+ assert len(test_args) == len(func_args), \
+ "Invalid number of arguments in test %s. See function %s signature." % (test_name, function_name)
+ expression_code += write_parameters(out_data_f, test_args, func_args, unique_expressions)
+
+ # Write a newline as test case separator
+ out_data_f.write('\n')
+
+ dep_check_code, expression_code = gen_suite_deps_checks(suite_deps, dep_check_code, expression_code)
+ return dep_check_code, expression_code
+
+
+def generate_code(funcs_file, data_file, template_file, platform_file, help_file, suites_dir, c_file, out_data_file):
+ """
+ Generate mbed-os test code.
+
+ :param funcs_file: Functions file object
+ :param data_file: Data file object
+ :param template_file: Template file object
+ :param platform_file: Platform file object
+ :param help_file: Helper functions file object
+ :param suites_dir: Test suites dir
+ :param c_file: Output C file object
+ :param out_data_file: Output intermediate data file object
+ :return:
+ """
+ for name, path in [('Functions file', funcs_file),
+ ('Data file', data_file),
+ ('Template file', template_file),
+ ('Platform file', platform_file),
+ ('Help code file', help_file),
+ ('Suites dir', suites_dir)]:
+ if not os.path.exists(path):
+ raise IOError("ERROR: %s [%s] not found!" % (name, path))
+
+ snippets = {'generator_script' : os.path.basename(__file__)}
+
+ # Read helpers
+ with open(help_file, 'r') as help_f, open(platform_file, 'r') as platform_f:
+ snippets['test_common_helper_file'] = help_file
+ snippets['test_common_helpers'] = help_f.read()
+ snippets['test_platform_file'] = platform_file
+ snippets['platform_code'] = platform_f.read().replace('DATA_FILE',
+ out_data_file.replace('\\', '\\\\')) # escape '\'
+
+ # Function code
+ with FileWrapper(funcs_file) as funcs_f, open(data_file, 'r') as data_f, open(out_data_file, 'w') as out_data_f:
+ suite_deps, dispatch_code, func_code, func_info = parse_functions(funcs_f)
+ snippets['functions_code'] = func_code
+ snippets['dispatch_code'] = dispatch_code
+ dep_check_code, expression_code = gen_from_test_data(data_f, out_data_f, func_info, suite_deps)
+ snippets['dep_check_code'] = dep_check_code
+ snippets['expression_code'] = expression_code
+
+ snippets['test_file'] = c_file
+ snippets['test_main_file'] = template_file
+ snippets['test_case_file'] = funcs_file
+ snippets['test_case_data_file'] = data_file
+ # Read Template
+ # Add functions
+ #
+ with open(template_file, 'r') as template_f, open(c_file, 'w') as c_f:
+ line_no = 1
+ for line in template_f.readlines():
+ snippets['line_no'] = line_no + 1 # Increment as it sets next line number
+ code = line.format(**snippets)
+ c_f.write(code)
+ line_no += 1
+
+
+def check_cmd():
+ """
+ Command line parser.
+
+ :return:
+ """
+ parser = argparse.ArgumentParser(description='Generate code for mbed-os tests.')
+
+ parser.add_argument("-f", "--functions-file",
+ dest="funcs_file",
+ help="Functions file",
+ metavar="FUNCTIONS",
+ required=True)
+
+ parser.add_argument("-d", "--data-file",
+ dest="data_file",
+ help="Data file",
+ metavar="DATA",
+ required=True)
+
+ parser.add_argument("-t", "--template-file",
+ dest="template_file",
+ help="Template file",
+ metavar="TEMPLATE",
+ required=True)
+
+ parser.add_argument("-s", "--suites-dir",
+ dest="suites_dir",
+ help="Suites dir",
+ metavar="SUITES",
+ required=True)
+
+ parser.add_argument("--help-file",
+ dest="help_file",
+ help="Help file",
+ metavar="HELPER",
+ required=True)
+
+ parser.add_argument("-p", "--platform-file",
+ dest="platform_file",
+ help="Platform code file",
+ metavar="PLATFORM_FILE",
+ required=True)
+
+ parser.add_argument("-o", "--out-dir",
+ dest="out_dir",
+ help="Dir where generated code and scripts are copied",
+ metavar="OUT_DIR",
+ required=True)
+
+ args = parser.parse_args()
+
+ data_file_name = os.path.basename(args.data_file)
+ data_name = os.path.splitext(data_file_name)[0]
+
+ out_c_file = os.path.join(args.out_dir, data_name + '.c')
+ out_data_file = os.path.join(args.out_dir, data_file_name)
+
+ out_c_file_dir = os.path.dirname(out_c_file)
+ out_data_file_dir = os.path.dirname(out_data_file)
+ for d in [out_c_file_dir, out_data_file_dir]:
+ if not os.path.exists(d):
+ os.makedirs(d)
+
+ generate_code(args.funcs_file, args.data_file, args.template_file, args.platform_file,
+ args.help_file, args.suites_dir, out_c_file, out_data_file)
+
+
+if __name__ == "__main__":
+ check_cmd()
diff --git a/tests/scripts/mbedtls_test.py b/tests/scripts/mbedtls_test.py
new file mode 100755
index 0000000..cdd7524
--- /dev/null
+++ b/tests/scripts/mbedtls_test.py
@@ -0,0 +1,342 @@
+# Greentea host test script for on-target tests.
+#
+# Copyright (C) 2018, ARM Limited, All Rights Reserved
+# 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 Crypto (https://tls.mbed.org)
+
+
+"""
+Greentea host test script for on-target tests.
+
+Host test script for testing Mbed Crypto test suites on target. Implements
+BaseHostTest to handle key,value pairs (events) coming from Mbed Crypto
+tests. Reads data file corresponding to the executing binary and dispatches
+test cases.
+"""
+
+
+import re
+import os
+import binascii
+from mbed_host_tests import BaseHostTest, event_callback
+
+
+class TestDataParser(object):
+ """
+ parser for mbedcrypto test data files.
+ """
+
+ def __init__(self):
+ """
+ Constructor
+ """
+ self.tests = []
+
+ def parse(self, data_file):
+ """
+ Data file parser.
+
+ :param data_file: Data file path
+ """
+ with open(data_file, 'r') as f:
+ self.__parse(f)
+
+ @staticmethod
+ def __escaped_split(str, ch):
+ """
+ Splits str on ch except when escaped.
+
+ :param str: String to split
+ :param ch: Split character
+ :return: List of splits
+ """
+ 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):
+ """
+ Parses data file using supplied file object.
+
+ :param file: Data file object
+ :return:
+ """
+ for line in file:
+ line = line.strip()
+ if len(line) == 0:
+ continue
+ # Read test name
+ name = line
+
+ # Check dependencies
+ deps = []
+ line = file.next().strip()
+ m = re.search('depends_on\:(.*)', line)
+ if m:
+ deps = [int(x) for x in m.group(1).split(':')]
+ line = file.next().strip()
+
+ # Read test vectors
+ line = line.replace('\\n', '\n')
+ 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):
+ """
+ Returns test data.
+ """
+ return self.tests
+
+
+class MbedCryptoTest(BaseHostTest):
+ """
+ Event handler for mbedcrypto unit tests. This script is loaded at run time
+ by htrun while executing mbedcrypto unit tests.
+ """
+ # 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):
+ """
+ Constructor initialises test index to 0.
+ """
+ super(MbedCryptoTest, 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):
+ """
+ Setup hook implementation. Reads test suite data file and parses out tests.
+ """
+ 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, '..', 'mbedcrypto', 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):
+ """
+ Prints test summary read by Greentea to detect test cases.
+ """
+ self.log('{{__testcase_count;%d}}' % len(self.tests))
+ for name, _, _, _ in self.tests:
+ self.log('{{__testcase_name;%s}}' % name)
+
+ @staticmethod
+ def align_32bit(b):
+ """
+ 4 byte aligns input byte array.
+
+ :return:
+ """
+ b += bytearray((4 - (len(b))) % 4)
+
+ @staticmethod
+ def hex_str_bytes(hex_str):
+ """
+ Converts Hex string representation to byte array
+
+ :param hex_str: Hex in string format.
+ :return: Output Byte array
+ """
+ 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
+
+ b = binascii.unhexlify(hex_str)
+ return b
+
+ @staticmethod
+ def int32_to_bigendian_bytes(i):
+ """
+ Coverts i to bytearray in big endian format.
+
+ :param i: Input integer
+ :return: Output bytes array in big endian or network order
+ """
+ b = bytearray([((i >> x) & 0xff) for x in [24, 16, 8, 0]])
+ return b
+
+ def test_vector_to_bytes(self, function_id, deps, parameters):
+ """
+ Converts test vector into a byte array that can be sent to the target.
+
+ :param function_id: Test Function Identifier
+ :param deps: Dependency list
+ :param parameters: Test function input parameters
+ :return: Byte array and its length
+ """
+ b = bytearray([len(deps)])
+ if len(deps):
+ b += bytearray(deps)
+ b += bytearray([function_id, len(parameters)])
+ 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)
+ b += self.int32_to_bigendian_bytes(i)
+ elif typ == 'char*':
+ param = param.strip('"')
+ i = len(param) + 1 # + 1 for null termination
+ b += 'S'
+ self.align_32bit(b)
+ b += self.int32_to_bigendian_bytes(i)
+ b += bytearray(list(param))
+ b += '\0' # Null terminate
+ elif typ == 'hex':
+ hb = self.hex_str_bytes(param)
+ b += 'H'
+ self.align_32bit(b)
+ i = len(hb)
+ b += self.int32_to_bigendian_bytes(i)
+ b += hb
+ length = self.int32_to_bigendian_bytes(len(b))
+ return b, length
+
+ 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):
+ name, function_id, deps, args = self.tests[self.test_index]
+ self.run_test(name, function_id, deps, args)
+ else:
+ self.notify_complete(True)
+
+ def run_test(self, name, function_id, deps, args):
+ """
+ Runs the test.
+
+ :param name: Test name
+ :param function_id: function identifier
+ :param deps: Dependencies list
+ :param args: test parameters
+ :return:
+ """
+ self.log("Running: %s" % name)
+
+ bytes, length = self.test_vector_to_bytes(function_id, deps, args)
+ self.send_kv(length, bytes)
+
+ @staticmethod
+ def get_result(value):
+ """
+ Converts result from string type to integer
+ :param value: Result code in string
+ :return: Integer result code
+ """
+ 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):
+ """
+ Called on key "GO". Kicks off test execution.
+
+ :param key: Event key
+ :param value: Value. ignored
+ :param timestamp: Timestamp ignored.
+ :return:
+ """
+ self.run_next_test()
+
+ @event_callback("R")
+ def on_result(self, key, value, timestamp):
+ """
+ Handle result. Prints test start, finish prints required by Greentea to detect test execution.
+
+ :param key: Event key
+ :param value: Value. ignored
+ :param timestamp: Timestamp ignored.
+ :return:
+ """
+ int_val = self.get_result(value)
+ name, function, deps, args = self.tests[self.test_index]
+ self.log('{{__testcase_start;%s}}' % name)
+ 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):
+ """
+ Handles test execution failure. That means dependency not supported or
+ Test function not supported. Hence marking test as skipped.
+
+ :param key: Event key
+ :param value: Value. ignored
+ :param timestamp: Timestamp ignored.
+ :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()
diff --git a/tests/scripts/run-test-suites.pl b/tests/scripts/run-test-suites.pl
new file mode 100755
index 0000000..5de20f8
--- /dev/null
+++ b/tests/scripts/run-test-suites.pl
@@ -0,0 +1,101 @@
+#!/usr/bin/perl
+
+# run-test-suites.pl
+#
+# This file is part of Mbed Crypto (https://tls.mbed.org)
+#
+# Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
+#
+# Purpose
+#
+# Executes all the available test suites, and provides a basic summary of the
+# results.
+#
+# Usage: run-test-suites.pl [-v]
+#
+# Options :
+# -v|--verbose - Provide a pass/fail/skip breakdown per test suite and
+# in total
+#
+
+use warnings;
+use strict;
+
+use utf8;
+use open qw(:std utf8);
+
+use constant FALSE => 0;
+use constant TRUE => 1;
+
+my $verbose;
+my $switch = shift;
+if ( defined($switch) && ( $switch eq "-v" || $switch eq "--verbose" ) ) {
+ $verbose = TRUE;
+}
+
+# All test suites = executable files, excluding source files, debug
+# and profiling information, etc. We can't just grep {! /\./} because
+#some of our test cases' base names contain a dot.
+my @suites = grep { -x $_ || /\.exe$/ } glob 'test_suite_*';
+die "$0: no test suite found\n" unless @suites;
+
+# in case test suites are linked dynamically
+$ENV{'LD_LIBRARY_PATH'} = '../library';
+$ENV{'DYLD_LIBRARY_PATH'} = '../library';
+
+my $prefix = $^O eq "MSWin32" ? '' : './';
+
+my ($failed_suites, $total_tests_run, $failed, $suite_cases_passed,
+ $suite_cases_failed, $suite_cases_skipped, $total_cases_passed,
+ $total_cases_failed, $total_cases_skipped );
+
+for my $suite (@suites)
+{
+ print "$suite ", "." x ( 72 - length($suite) - 2 - 4 ), " ";
+ my $result = `$prefix$suite`;
+
+ $suite_cases_passed = () = $result =~ /.. PASS/g;
+ $suite_cases_failed = () = $result =~ /.. FAILED/g;
+ $suite_cases_skipped = () = $result =~ /.. ----/g;
+
+ if( $result =~ /PASSED/ ) {
+ print "PASS\n";
+ } else {
+ $failed_suites++;
+ print "FAIL\n";
+ }
+
+ my ($passed, $tests, $skipped) = $result =~ /([0-9]*) \/ ([0-9]*) tests.*?([0-9]*) skipped/;
+ $total_tests_run += $tests - $skipped;
+
+ if ( $verbose ) {
+ print "(test cases passed:", $suite_cases_passed,
+ " failed:", $suite_cases_failed,
+ " skipped:", $suite_cases_skipped,
+ " of total:", ($suite_cases_passed + $suite_cases_failed +
+ $suite_cases_skipped),
+ ")\n"
+ }
+
+ $total_cases_passed += $suite_cases_passed;
+ $total_cases_failed += $suite_cases_failed;
+ $total_cases_skipped += $suite_cases_skipped;
+}
+
+print "-" x 72, "\n";
+print $failed_suites ? "FAILED" : "PASSED";
+printf " (%d suites, %d tests run)\n", scalar @suites, $total_tests_run;
+
+if ( $verbose ) {
+ print " test cases passed :", $total_cases_passed, "\n";
+ print " failed :", $total_cases_failed, "\n";
+ print " skipped :", $total_cases_skipped, "\n";
+ print " of tests executed :", ( $total_cases_passed + $total_cases_failed ),
+ "\n";
+ print " of available tests :",
+ ( $total_cases_passed + $total_cases_failed + $total_cases_skipped ),
+ "\n"
+ }
+
+exit( $failed_suites ? 1 : 0 );
+
diff --git a/tests/scripts/test_generate_test_code.py b/tests/scripts/test_generate_test_code.py
new file mode 100755
index 0000000..9adb441
--- /dev/null
+++ b/tests/scripts/test_generate_test_code.py
@@ -0,0 +1,1524 @@
+# Unit test for generate_test_code.py
+#
+# Copyright (C) 2018, ARM Limited, All Rights Reserved
+# 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 Crypto (https://tls.mbed.org)
+
+from StringIO import StringIO
+from unittest import TestCase, main as unittest_main
+from mock import patch
+from generate_test_code import *
+
+
+"""
+Unit tests for generate_test_code.py
+"""
+
+
+class GenDep(TestCase):
+ """
+ Test suite for function gen_dep()
+ """
+
+ def test_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = ['DEP1', 'DEP2']
+ dep_start, dep_end = gen_deps(deps)
+ ifdef1, ifdef2 = dep_start.splitlines()
+ endif1, endif2 = dep_end.splitlines()
+ self.assertEqual(ifdef1, '#if defined(DEP1)', 'ifdef generated incorrectly')
+ self.assertEqual(ifdef2, '#if defined(DEP2)', 'ifdef generated incorrectly')
+ self.assertEqual(endif1, '#endif /* DEP2 */', 'endif generated incorrectly')
+ self.assertEqual(endif2, '#endif /* DEP1 */', 'endif generated incorrectly')
+
+ def test_disabled_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = ['!DEP1', '!DEP2']
+ dep_start, dep_end = gen_deps(deps)
+ ifdef1, ifdef2 = dep_start.splitlines()
+ endif1, endif2 = dep_end.splitlines()
+ self.assertEqual(ifdef1, '#if !defined(DEP1)', 'ifdef generated incorrectly')
+ self.assertEqual(ifdef2, '#if !defined(DEP2)', 'ifdef generated incorrectly')
+ self.assertEqual(endif1, '#endif /* !DEP2 */', 'endif generated incorrectly')
+ self.assertEqual(endif2, '#endif /* !DEP1 */', 'endif generated incorrectly')
+
+ def test_mixed_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = ['!DEP1', 'DEP2']
+ dep_start, dep_end = gen_deps(deps)
+ ifdef1, ifdef2 = dep_start.splitlines()
+ endif1, endif2 = dep_end.splitlines()
+ self.assertEqual(ifdef1, '#if !defined(DEP1)', 'ifdef generated incorrectly')
+ self.assertEqual(ifdef2, '#if defined(DEP2)', 'ifdef generated incorrectly')
+ self.assertEqual(endif1, '#endif /* DEP2 */', 'endif generated incorrectly')
+ self.assertEqual(endif2, '#endif /* !DEP1 */', 'endif generated incorrectly')
+
+ def test_empty_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = []
+ dep_start, dep_end = gen_deps(deps)
+ self.assertEqual(dep_start, '', 'ifdef generated incorrectly')
+ self.assertEqual(dep_end, '', 'ifdef generated incorrectly')
+
+ def test_large_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = []
+ count = 10
+ for i in range(count):
+ deps.append('DEP%d' % i)
+ dep_start, dep_end = gen_deps(deps)
+ self.assertEqual(len(dep_start.splitlines()), count, 'ifdef generated incorrectly')
+ self.assertEqual(len(dep_end.splitlines()), count, 'ifdef generated incorrectly')
+
+
+class GenDepOneLine(TestCase):
+ """
+ Test Suite for testing gen_deps_one_line()
+ """
+
+ def test_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = ['DEP1', 'DEP2']
+ dep_str = gen_deps_one_line(deps)
+ self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)', 'ifdef generated incorrectly')
+
+ def test_disabled_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = ['!DEP1', '!DEP2']
+ dep_str = gen_deps_one_line(deps)
+ self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)', 'ifdef generated incorrectly')
+
+ def test_mixed_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = ['!DEP1', 'DEP2']
+ dep_str = gen_deps_one_line(deps)
+ self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)', 'ifdef generated incorrectly')
+
+ def test_empty_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = []
+ dep_str = gen_deps_one_line(deps)
+ self.assertEqual(dep_str, '', 'ifdef generated incorrectly')
+
+ def test_large_deps_list(self):
+ """
+ Test that gen_dep() correctly creates deps for given dependency list.
+ :return:
+ """
+ deps = []
+ count = 10
+ for i in range(count):
+ deps.append('DEP%d' % i)
+ dep_str = gen_deps_one_line(deps)
+ expected = '#if ' + ' && '.join(['defined(%s)' % x for x in deps])
+ self.assertEqual(dep_str, expected, 'ifdef generated incorrectly')
+
+
+class GenFunctionWrapper(TestCase):
+ """
+ Test Suite for testing gen_function_wrapper()
+ """
+
+ def test_params_unpack(self):
+ """
+ Test that params are properly unpacked in the function call.
+
+ :return:
+ """
+ code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
+ expected = '''
+void test_a_wrapper( void ** params )
+{
+
+
+ test_a( a, b, c, d );
+}
+'''
+ self.assertEqual(code, expected)
+
+ def test_local(self):
+ """
+ Test that params are properly unpacked in the function call.
+
+ :return:
+ """
+ code = gen_function_wrapper('test_a', 'int x = 1;', ('x', 'b', 'c', 'd'))
+ expected = '''
+void test_a_wrapper( void ** params )
+{
+
+int x = 1;
+ test_a( x, b, c, d );
+}
+'''
+ self.assertEqual(code, expected)
+
+ def test_empty_params(self):
+ """
+ Test that params are properly unpacked in the function call.
+
+ :return:
+ """
+ code = gen_function_wrapper('test_a', '', ())
+ expected = '''
+void test_a_wrapper( void ** params )
+{
+ (void)params;
+
+ test_a( );
+}
+'''
+ self.assertEqual(code, expected)
+
+
+class GenDispatch(TestCase):
+ """
+ Test suite for testing gen_dispatch()
+ """
+
+ def test_dispatch(self):
+ """
+ Test that dispatch table entry is generated correctly.
+ :return:
+ """
+ code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
+ expected = '''
+#if defined(DEP1) && defined(DEP2)
+ test_a_wrapper,
+#else
+ NULL,
+#endif
+'''
+ self.assertEqual(code, expected)
+
+ def test_empty_deps(self):
+ """
+ Test empty dependency list.
+ :return:
+ """
+ code = gen_dispatch('test_a', [])
+ expected = '''
+ test_a_wrapper,
+'''
+ self.assertEqual(code, expected)
+
+
+class StringIOWrapper(StringIO, object):
+ """
+ file like class to mock file object in tests.
+ """
+ def __init__(self, file_name, data, line_no = 1):
+ """
+ Init file handle.
+
+ :param file_name:
+ :param data:
+ :param line_no:
+ """
+ super(StringIOWrapper, self).__init__(data)
+ self.line_no = line_no
+ self.name = file_name
+
+ def next(self):
+ """
+ Iterator return impl.
+ :return:
+ """
+ line = super(StringIOWrapper, self).next()
+ return line
+
+ def readline(self, limit=0):
+ """
+ Wrap the base class readline.
+
+ :param limit:
+ :return:
+ """
+ line = super(StringIOWrapper, self).readline()
+ if line:
+ self.line_no += 1
+ return line
+
+
+class ParseUntilPattern(TestCase):
+ """
+ Test Suite for testing parse_until_pattern().
+ """
+
+ def test_suite_headers(self):
+ """
+ Test that suite headers are parsed correctly.
+
+ :return:
+ """
+ data = '''#include "mbedcrypto/ecp.h"
+
+#define ECP_PF_UNKNOWN -1
+/* END_HEADER */
+'''
+ expected = '''#line 1 "test_suite_ut.function"
+#include "mbedcrypto/ecp.h"
+
+#define ECP_PF_UNKNOWN -1
+'''
+ s = StringIOWrapper('test_suite_ut.function', data, line_no=0)
+ headers = parse_until_pattern(s, END_HEADER_REGEX)
+ self.assertEqual(headers, expected)
+
+ def test_line_no(self):
+ """
+ Test that #line is set to correct line no. in source .function file.
+
+ :return:
+ """
+ data = '''#include "mbedcrypto/ecp.h"
+
+#define ECP_PF_UNKNOWN -1
+/* END_HEADER */
+'''
+ offset_line_no = 5
+ expected = '''#line %d "test_suite_ut.function"
+#include "mbedcrypto/ecp.h"
+
+#define ECP_PF_UNKNOWN -1
+''' % (offset_line_no + 1)
+ s = StringIOWrapper('test_suite_ut.function', data, offset_line_no)
+ headers = parse_until_pattern(s, END_HEADER_REGEX)
+ self.assertEqual(headers, expected)
+
+ def test_no_end_header_comment(self):
+ """
+ Test that InvalidFileFormat is raised when end header comment is missing.
+ :return:
+ """
+ data = '''#include "mbedcrypto/ecp.h"
+
+#define ECP_PF_UNKNOWN -1
+
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(InvalidFileFormat, parse_until_pattern, s, END_HEADER_REGEX)
+
+
+class ParseSuiteDeps(TestCase):
+ """
+ Test Suite for testing parse_suite_deps().
+ """
+
+ def test_suite_deps(self):
+ """
+
+ :return:
+ """
+ data = '''
+ * depends_on:MBEDCRYPTO_ECP_C
+ * END_DEPENDENCIES
+ */
+'''
+ expected = ['MBEDCRYPTO_ECP_C']
+ s = StringIOWrapper('test_suite_ut.function', data)
+ deps = parse_suite_deps(s)
+ self.assertEqual(deps, expected)
+
+ def test_no_end_dep_comment(self):
+ """
+ Test that InvalidFileFormat is raised when end dep comment is missing.
+ :return:
+ """
+ data = '''
+* depends_on:MBEDCRYPTO_ECP_C
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(InvalidFileFormat, parse_suite_deps, s)
+
+ def test_deps_split(self):
+ """
+ Test that InvalidFileFormat is raised when end dep comment is missing.
+ :return:
+ """
+ data = '''
+ * depends_on:MBEDCRYPTO_ECP_C:A:B: C : D :F : G: !H
+ * END_DEPENDENCIES
+ */
+'''
+ expected = ['MBEDCRYPTO_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
+ s = StringIOWrapper('test_suite_ut.function', data)
+ deps = parse_suite_deps(s)
+ self.assertEqual(deps, expected)
+
+
+class ParseFuncDeps(TestCase):
+ """
+ Test Suite for testing parse_function_deps()
+ """
+
+ def test_function_deps(self):
+ """
+ Test that parse_function_deps() correctly parses function dependencies.
+ :return:
+ """
+ line = '/* BEGIN_CASE depends_on:MBEDCRYPTO_ENTROPY_NV_SEED:MBEDCRYPTO_FS_IO */'
+ expected = ['MBEDCRYPTO_ENTROPY_NV_SEED', 'MBEDCRYPTO_FS_IO']
+ deps = parse_function_deps(line)
+ self.assertEqual(deps, expected)
+
+ def test_no_deps(self):
+ """
+ Test that parse_function_deps() correctly parses function dependencies.
+ :return:
+ """
+ line = '/* BEGIN_CASE */'
+ deps = parse_function_deps(line)
+ self.assertEqual(deps, [])
+
+ def test_poorly_defined_deps(self):
+ """
+ Test that parse_function_deps() correctly parses function dependencies.
+ :return:
+ """
+ line = '/* BEGIN_CASE depends_on:MBEDCRYPTO_FS_IO: A : !B:C : F*/'
+ deps = parse_function_deps(line)
+ self.assertEqual(deps, ['MBEDCRYPTO_FS_IO', 'A', '!B', 'C', 'F'])
+
+
+class ParseFuncSignature(TestCase):
+ """
+ Test Suite for parse_function_signature().
+ """
+
+ def test_int_and_char_params(self):
+ """
+ Test int and char parameters parsing
+ :return:
+ """
+ line = 'void entropy_threshold( char * a, int b, int result )'
+ name, args, local, arg_dispatch = parse_function_signature(line)
+ self.assertEqual(name, 'entropy_threshold')
+ self.assertEqual(args, ['char*', 'int', 'int'])
+ self.assertEqual(local, '')
+ self.assertEqual(arg_dispatch, ['(char *) params[0]', '*( (int *) params[1] )', '*( (int *) params[2] )'])
+
+ def test_hex_params(self):
+ """
+ Test hex parameters parsing
+ :return:
+ """
+ line = 'void entropy_threshold( char * a, HexParam_t * h, int result )'
+ name, args, local, arg_dispatch = parse_function_signature(line)
+ self.assertEqual(name, 'entropy_threshold')
+ self.assertEqual(args, ['char*', 'hex', 'int'])
+ self.assertEqual(local, ' HexParam_t hex1 = {(uint8_t *) params[1], *( (uint32_t *) params[2] )};\n')
+ self.assertEqual(arg_dispatch, ['(char *) params[0]', '&hex1', '*( (int *) params[3] )'])
+
+ def test_non_void_function(self):
+ """
+ Test invalid signature (non void).
+ :return:
+ """
+ line = 'int entropy_threshold( char * a, HexParam_t * h, int result )'
+ self.assertRaises(ValueError, parse_function_signature, line)
+
+ def test_unsupported_arg(self):
+ """
+ Test unsupported arguments (not among int, char * and HexParam_t)
+ :return:
+ """
+ line = 'int entropy_threshold( char * a, HexParam_t * h, int * result )'
+ self.assertRaises(ValueError, parse_function_signature, line)
+
+ def test_no_params(self):
+ """
+ Test no parameters.
+ :return:
+ """
+ line = 'void entropy_threshold()'
+ name, args, local, arg_dispatch = parse_function_signature(line)
+ self.assertEqual(name, 'entropy_threshold')
+ self.assertEqual(args, [])
+ self.assertEqual(local, '')
+ self.assertEqual(arg_dispatch, [])
+
+
+class ParseFunctionCode(TestCase):
+ """
+ Test suite for testing parse_function_code()
+ """
+
+ def test_no_function(self):
+ """
+ Test no test function found.
+ :return:
+ """
+ data = '''
+No
+test
+function
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(InvalidFileFormat, parse_function_code, s, [], [])
+
+ def test_no_end_case_comment(self):
+ """
+ Test missing end case.
+ :return:
+ """
+ data = '''
+void test_func()
+{
+}
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(InvalidFileFormat, parse_function_code, s, [], [])
+
+ @patch("generate_test_code.parse_function_signature")
+ def test_parse_function_signature_called(self, parse_function_signature_mock):
+ """
+ Test parse_function_code()
+ :return:
+ """
+ parse_function_signature_mock.return_value = ('test_func', [], '', [])
+ data = '''
+void test_func()
+{
+}
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(InvalidFileFormat, parse_function_code, s, [], [])
+ self.assertTrue(parse_function_signature_mock.called)
+ parse_function_signature_mock.assert_called_with('void test_func()\n')
+
+ @patch("generate_test_code.gen_dispatch")
+ @patch("generate_test_code.gen_deps")
+ @patch("generate_test_code.gen_function_wrapper")
+ @patch("generate_test_code.parse_function_signature")
+ def test_return(self, parse_function_signature_mock,
+ gen_function_wrapper_mock,
+ gen_deps_mock,
+ gen_dispatch_mock):
+ """
+ Test generated code.
+ :return:
+ """
+ parse_function_signature_mock.return_value = ('func', [], '', [])
+ gen_function_wrapper_mock.return_value = ''
+ gen_deps_mock.side_effect = gen_deps
+ gen_dispatch_mock.side_effect = gen_dispatch
+ data = '''
+void func()
+{
+ ba ba black sheep
+ have you any wool
+}
+/* END_CASE */
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ name, arg, code, dispatch_code = parse_function_code(s, [], [])
+
+ #self.assertRaises(InvalidFileFormat, parse_function_code, s, [], [])
+ self.assertTrue(parse_function_signature_mock.called)
+ parse_function_signature_mock.assert_called_with('void func()\n')
+ gen_function_wrapper_mock.assert_called_with('test_func', '', [])
+ self.assertEqual(name, 'test_func')
+ self.assertEqual(arg, [])
+ expected = '''#line 2 "test_suite_ut.function"
+void test_func()
+{
+ ba ba black sheep
+ have you any wool
+exit:
+ ;;
+}
+'''
+ self.assertEqual(code, expected)
+ self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
+
+ @patch("generate_test_code.gen_dispatch")
+ @patch("generate_test_code.gen_deps")
+ @patch("generate_test_code.gen_function_wrapper")
+ @patch("generate_test_code.parse_function_signature")
+ def test_with_exit_label(self, parse_function_signature_mock,
+ gen_function_wrapper_mock,
+ gen_deps_mock,
+ gen_dispatch_mock):
+ """
+ Test when exit label is present.
+ :return:
+ """
+ parse_function_signature_mock.return_value = ('func', [], '', [])
+ gen_function_wrapper_mock.return_value = ''
+ gen_deps_mock.side_effect = gen_deps
+ gen_dispatch_mock.side_effect = gen_dispatch
+ data = '''
+void func()
+{
+ ba ba black sheep
+ have you any wool
+exit:
+ yes sir yes sir
+ 3 bags full
+}
+/* END_CASE */
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ name, arg, code, dispatch_code = parse_function_code(s, [], [])
+
+ expected = '''#line 2 "test_suite_ut.function"
+void test_func()
+{
+ ba ba black sheep
+ have you any wool
+exit:
+ yes sir yes sir
+ 3 bags full
+}
+'''
+ self.assertEqual(code, expected)
+
+
+class ParseFunction(TestCase):
+ """
+ Test Suite for testing parse_functions()
+ """
+
+ @patch("generate_test_code.parse_until_pattern")
+ def test_begin_header(self, parse_until_pattern_mock):
+ """
+ Test that begin header is checked and parse_until_pattern() is called.
+ :return:
+ """
+ def stop(this):
+ raise Exception
+ parse_until_pattern_mock.side_effect = stop
+ data = '''/* BEGIN_HEADER */
+#include "mbedcrypto/ecp.h"
+
+#define ECP_PF_UNKNOWN -1
+/* END_HEADER */
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(Exception, parse_functions, s)
+ parse_until_pattern_mock.assert_called_with(s, END_HEADER_REGEX)
+ self.assertEqual(s.line_no, 2)
+
+ @patch("generate_test_code.parse_until_pattern")
+ def test_begin_helper(self, parse_until_pattern_mock):
+ """
+ Test that begin helper is checked and parse_until_pattern() is called.
+ :return:
+ """
+ def stop(this):
+ raise Exception
+ parse_until_pattern_mock.side_effect = stop
+ data = '''/* BEGIN_SUITE_HELPERS */
+void print_helloworld()
+{
+ printf ("Hello World!\n");
+}
+/* END_SUITE_HELPERS */
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(Exception, parse_functions, s)
+ parse_until_pattern_mock.assert_called_with(s, END_SUITE_HELPERS_REGEX)
+ self.assertEqual(s.line_no, 2)
+
+ @patch("generate_test_code.parse_suite_deps")
+ def test_begin_dep(self, parse_suite_deps_mock):
+ """
+ Test that begin dep is checked and parse_suite_deps() is called.
+ :return:
+ """
+ def stop(this):
+ raise Exception
+ parse_suite_deps_mock.side_effect = stop
+ data = '''/* BEGIN_DEPENDENCIES
+ * depends_on:MBEDCRYPTO_ECP_C
+ * END_DEPENDENCIES
+ */
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(Exception, parse_functions, s)
+ parse_suite_deps_mock.assert_called_with(s)
+ self.assertEqual(s.line_no, 2)
+
+ @patch("generate_test_code.parse_function_deps")
+ def test_begin_function_dep(self, parse_function_deps_mock):
+ """
+ Test that begin dep is checked and parse_function_deps() is called.
+ :return:
+ """
+ def stop(this):
+ raise Exception
+ parse_function_deps_mock.side_effect = stop
+
+ deps_str = '/* BEGIN_CASE depends_on:MBEDCRYPTO_ENTROPY_NV_SEED:MBEDCRYPTO_FS_IO */\n'
+ data = '''%svoid test_func()
+{
+}
+''' % deps_str
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(Exception, parse_functions, s)
+ parse_function_deps_mock.assert_called_with(deps_str)
+ self.assertEqual(s.line_no, 2)
+
+ @patch("generate_test_code.parse_function_code")
+ @patch("generate_test_code.parse_function_deps")
+ def test_return(self, parse_function_deps_mock, parse_function_code_mock):
+ """
+ Test that begin case is checked and parse_function_code() is called.
+ :return:
+ """
+ def stop(this):
+ raise Exception
+ parse_function_deps_mock.return_value = []
+ in_func_code= '''void test_func()
+{
+}
+'''
+ func_dispatch = '''
+ test_func_wrapper,
+'''
+ parse_function_code_mock.return_value = 'test_func', [], in_func_code, func_dispatch
+ deps_str = '/* BEGIN_CASE depends_on:MBEDCRYPTO_ENTROPY_NV_SEED:MBEDCRYPTO_FS_IO */\n'
+ data = '''%svoid test_func()
+{
+}
+''' % deps_str
+ s = StringIOWrapper('test_suite_ut.function', data)
+ suite_deps, dispatch_code, func_code, func_info = parse_functions(s)
+ parse_function_deps_mock.assert_called_with(deps_str)
+ parse_function_code_mock.assert_called_with(s, [], [])
+ self.assertEqual(s.line_no, 5)
+ self.assertEqual(suite_deps, [])
+ expected_dispatch_code = '''/* Function Id: 0 */
+
+ test_func_wrapper,
+'''
+ self.assertEqual(dispatch_code, expected_dispatch_code)
+ self.assertEqual(func_code, in_func_code)
+ self.assertEqual(func_info, {'test_func': (0, [])})
+
+ def test_parsing(self):
+ """
+ Test case parsing.
+ :return:
+ """
+ data = '''/* BEGIN_HEADER */
+#include "mbedcrypto/ecp.h"
+
+#define ECP_PF_UNKNOWN -1
+/* END_HEADER */
+
+/* BEGIN_DEPENDENCIES
+ * depends_on:MBEDCRYPTO_ECP_C
+ * END_DEPENDENCIES
+ */
+
+/* BEGIN_CASE depends_on:MBEDCRYPTO_ENTROPY_NV_SEED:MBEDCRYPTO_FS_IO */
+void func1()
+{
+}
+/* END_CASE */
+
+/* BEGIN_CASE depends_on:MBEDCRYPTO_ENTROPY_NV_SEED:MBEDCRYPTO_FS_IO */
+void func2()
+{
+}
+/* END_CASE */
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ suite_deps, dispatch_code, func_code, func_info = parse_functions(s)
+ self.assertEqual(s.line_no, 23)
+ self.assertEqual(suite_deps, ['MBEDCRYPTO_ECP_C'])
+
+ expected_dispatch_code = '''/* Function Id: 0 */
+
+#if defined(MBEDCRYPTO_ECP_C) && defined(MBEDCRYPTO_ENTROPY_NV_SEED) && defined(MBEDCRYPTO_FS_IO)
+ test_func1_wrapper,
+#else
+ NULL,
+#endif
+/* Function Id: 1 */
+
+#if defined(MBEDCRYPTO_ECP_C) && defined(MBEDCRYPTO_ENTROPY_NV_SEED) && defined(MBEDCRYPTO_FS_IO)
+ test_func2_wrapper,
+#else
+ NULL,
+#endif
+'''
+ self.assertEqual(dispatch_code, expected_dispatch_code)
+ expected_func_code = '''#if defined(MBEDCRYPTO_ECP_C)
+#line 3 "test_suite_ut.function"
+#include "mbedcrypto/ecp.h"
+
+#define ECP_PF_UNKNOWN -1
+#if defined(MBEDCRYPTO_ENTROPY_NV_SEED)
+#if defined(MBEDCRYPTO_FS_IO)
+#line 14 "test_suite_ut.function"
+void test_func1()
+{
+exit:
+ ;;
+}
+
+void test_func1_wrapper( void ** params )
+{
+ (void)params;
+
+ test_func1( );
+}
+#endif /* MBEDCRYPTO_FS_IO */
+#endif /* MBEDCRYPTO_ENTROPY_NV_SEED */
+#if defined(MBEDCRYPTO_ENTROPY_NV_SEED)
+#if defined(MBEDCRYPTO_FS_IO)
+#line 20 "test_suite_ut.function"
+void test_func2()
+{
+exit:
+ ;;
+}
+
+void test_func2_wrapper( void ** params )
+{
+ (void)params;
+
+ test_func2( );
+}
+#endif /* MBEDCRYPTO_FS_IO */
+#endif /* MBEDCRYPTO_ENTROPY_NV_SEED */
+#endif /* MBEDCRYPTO_ECP_C */
+'''
+ self.assertEqual(func_code, expected_func_code)
+ self.assertEqual(func_info, {'test_func1': (0, []), 'test_func2': (1, [])})
+
+ def test_same_function_name(self):
+ """
+ Test name conflict.
+ :return:
+ """
+ data = '''/* BEGIN_HEADER */
+#include "mbedcrypto/ecp.h"
+
+#define ECP_PF_UNKNOWN -1
+/* END_HEADER */
+
+/* BEGIN_DEPENDENCIES
+ * depends_on:MBEDCRYPTO_ECP_C
+ * END_DEPENDENCIES
+ */
+
+/* BEGIN_CASE depends_on:MBEDCRYPTO_ENTROPY_NV_SEED:MBEDCRYPTO_FS_IO */
+void func()
+{
+}
+/* END_CASE */
+
+/* BEGIN_CASE depends_on:MBEDCRYPTO_ENTROPY_NV_SEED:MBEDCRYPTO_FS_IO */
+void func()
+{
+}
+/* END_CASE */
+'''
+ s = StringIOWrapper('test_suite_ut.function', data)
+ self.assertRaises(AssertionError, parse_functions, s)
+
+
+class ExcapedSplit(TestCase):
+ """
+ Test suite for testing escaped_split().
+ Note: Since escaped_split() output is used to write back to the intermediate data file. Any escape characters
+ in the input are retained in the output.
+ """
+
+ def test_invalid_input(self):
+ """
+ Test when input split character is not a character.
+ :return:
+ """
+ self.assertRaises(ValueError, escaped_split, '', 'string')
+
+ def test_empty_string(self):
+ """
+ Test empty strig input.
+ :return:
+ """
+ splits = escaped_split('', ':')
+ self.assertEqual(splits, [])
+
+ def test_no_escape(self):
+ """
+ Test with no escape character. The behaviour should be same as str.split()
+ :return:
+ """
+ s = 'yahoo:google'
+ splits = escaped_split(s, ':')
+ self.assertEqual(splits, s.split(':'))
+
+ def test_escaped_input(self):
+ """
+ Test imput that has escaped delimiter.
+ :return:
+ """
+ s = 'yahoo\:google:facebook'
+ splits = escaped_split(s, ':')
+ self.assertEqual(splits, ['yahoo\:google', 'facebook'])
+
+ def test_escaped_escape(self):
+ """
+ Test imput that has escaped delimiter.
+ :return:
+ """
+ s = 'yahoo\\\:google:facebook'
+ splits = escaped_split(s, ':')
+ self.assertEqual(splits, ['yahoo\\\\', 'google', 'facebook'])
+
+ def test_all_at_once(self):
+ """
+ Test imput that has escaped delimiter.
+ :return:
+ """
+ s = 'yahoo\\\:google:facebook\:instagram\\\:bbc\\\\:wikipedia'
+ splits = escaped_split(s, ':')
+ self.assertEqual(splits, ['yahoo\\\\', 'google', 'facebook\:instagram\\\\', 'bbc\\\\', 'wikipedia'])
+
+
+class ParseTestData(TestCase):
+ """
+ Test suite for parse test data.
+ """
+
+ def test_parser(self):
+ """
+ Test that tests are parsed correctly from data file.
+ :return:
+ """
+ data = """
+Diffie-Hellman full exchange #1
+dhm_do_dhm:10:"23":10:"5"
+
+Diffie-Hellman full exchange #2
+dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
+
+Diffie-Hellman full exchange #3
+dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
+
+Diffie-Hellman selftest
+dhm_selftest:
+"""
+ s = StringIOWrapper('test_suite_ut.function', data)
+ tests = [(name, function, deps, args) for name, function, deps, args in parse_test_data(s)]
+ t1, t2, t3, t4 = tests
+ self.assertEqual(t1[0], 'Diffie-Hellman full exchange #1')
+ self.assertEqual(t1[1], 'dhm_do_dhm')
+ self.assertEqual(t1[2], [])
+ self.assertEqual(t1[3], ['10', '"23"', '10', '"5"'])
+
+ self.assertEqual(t2[0], 'Diffie-Hellman full exchange #2')
+ self.assertEqual(t2[1], 'dhm_do_dhm')
+ self.assertEqual(t2[2], [])
+ self.assertEqual(t2[3], ['10', '"93450983094850938450983409623"', '10', '"9345098304850938450983409622"'])
+
+ self.assertEqual(t3[0], 'Diffie-Hellman full exchange #3')
+ self.assertEqual(t3[1], 'dhm_do_dhm')
+ self.assertEqual(t3[2], [])
+ self.assertEqual(t3[3], ['10', '"9345098382739712938719287391879381271"', '10', '"9345098792137312973297123912791271"'])
+
+ self.assertEqual(t4[0], 'Diffie-Hellman selftest')
+ self.assertEqual(t4[1], 'dhm_selftest')
+ self.assertEqual(t4[2], [])
+ self.assertEqual(t4[3], [])
+
+ def test_with_dependencies(self):
+ """
+ Test that tests with dependencies are parsed.
+ :return:
+ """
+ data = """
+Diffie-Hellman full exchange #1
+depends_on:YAHOO
+dhm_do_dhm:10:"23":10:"5"
+
+Diffie-Hellman full exchange #2
+dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
+
+"""
+ s = StringIOWrapper('test_suite_ut.function', data)
+ tests = [(name, function, deps, args) for name, function, deps, args in parse_test_data(s)]
+ t1, t2 = tests
+ self.assertEqual(t1[0], 'Diffie-Hellman full exchange #1')
+ self.assertEqual(t1[1], 'dhm_do_dhm')
+ self.assertEqual(t1[2], ['YAHOO'])
+ self.assertEqual(t1[3], ['10', '"23"', '10', '"5"'])
+
+ self.assertEqual(t2[0], 'Diffie-Hellman full exchange #2')
+ self.assertEqual(t2[1], 'dhm_do_dhm')
+ self.assertEqual(t2[2], [])
+ self.assertEqual(t2[3], ['10', '"93450983094850938450983409623"', '10', '"9345098304850938450983409622"'])
+
+ def test_no_args(self):
+ """
+ Test AssertionError is raised when test function name and args line is missing.
+ :return:
+ """
+ data = """
+Diffie-Hellman full exchange #1
+depends_on:YAHOO
+
+
+Diffie-Hellman full exchange #2
+dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
+
+"""
+ s = StringIOWrapper('test_suite_ut.function', data)
+ e = None
+ try:
+ for x, y, z, a in parse_test_data(s):
+ pass
+ except AssertionError, e:
+ pass
+ self.assertEqual(type(e), AssertionError)
+
+ def test_incomplete_data(self):
+ """
+ Test AssertionError is raised when test function name and args line is missing.
+ :return:
+ """
+ data = """
+Diffie-Hellman full exchange #1
+depends_on:YAHOO
+"""
+ s = StringIOWrapper('test_suite_ut.function', data)
+ e = None
+ try:
+ for x, y, z, a in parse_test_data(s):
+ pass
+ except AssertionError, e:
+ pass
+ self.assertEqual(type(e), AssertionError)
+
+
+class GenDepCheck(TestCase):
+ """
+ Test suite for gen_dep_check(). It is assumed this function is called with valid inputs.
+ """
+
+ def test_gen_dep_check(self):
+ """
+ Test that dependency check code generated correctly.
+ :return:
+ """
+ expected = """
+ case 5:
+ {
+#if defined(YAHOO)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;"""
+ out = gen_dep_check(5, 'YAHOO')
+ self.assertEqual(out, expected)
+
+ def test_noT(self):
+ """
+ Test dependency with !.
+ :return:
+ """
+ expected = """
+ case 5:
+ {
+#if !defined(YAHOO)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;"""
+ out = gen_dep_check(5, '!YAHOO')
+ self.assertEqual(out, expected)
+
+ def test_empty_dependency(self):
+ """
+ Test invalid dependency input.
+ :return:
+ """
+ self.assertRaises(AssertionError, gen_dep_check, 5, '!')
+
+ def test_negative_dep_id(self):
+ """
+ Test invalid dependency input.
+ :return:
+ """
+ self.assertRaises(AssertionError, gen_dep_check, -1, 'YAHOO')
+
+
+class GenExpCheck(TestCase):
+ """
+ Test suite for gen_expression_check(). It is assumed this function is called with valid inputs.
+ """
+
+ def test_gen_exp_check(self):
+ """
+ Test that expression check code generated correctly.
+ :return:
+ """
+ expected = """
+ case 5:
+ {
+ *out_value = YAHOO;
+ }
+ break;"""
+ out = gen_expression_check(5, 'YAHOO')
+ self.assertEqual(out, expected)
+
+ def test_invalid_expression(self):
+ """
+ Test invalid expression input.
+ :return:
+ """
+ self.assertRaises(AssertionError, gen_expression_check, 5, '')
+
+ def test_negative_exp_id(self):
+ """
+ Test invalid expression id.
+ :return:
+ """
+ self.assertRaises(AssertionError, gen_expression_check, -1, 'YAHOO')
+
+
+class WriteDeps(TestCase):
+ """
+ Test suite for testing write_deps.
+ """
+
+ def test_no_test_deps(self):
+ """
+ Test when test_deps is empty.
+ :return:
+ """
+ s = StringIOWrapper('test_suite_ut.data', '')
+ unique_deps = []
+ dep_check_code = write_deps(s, [], unique_deps)
+ self.assertEqual(dep_check_code, '')
+ self.assertEqual(len(unique_deps), 0)
+ self.assertEqual(s.getvalue(), '')
+
+ def test_unique_dep_ids(self):
+ """
+
+ :return:
+ """
+ s = StringIOWrapper('test_suite_ut.data', '')
+ unique_deps = []
+ dep_check_code = write_deps(s, ['DEP3', 'DEP2', 'DEP1'], unique_deps)
+ expect_dep_check_code = '''
+ case 0:
+ {
+#if defined(DEP3)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;
+ case 1:
+ {
+#if defined(DEP2)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;
+ case 2:
+ {
+#if defined(DEP1)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;'''
+ self.assertEqual(dep_check_code, expect_dep_check_code)
+ self.assertEqual(len(unique_deps), 3)
+ self.assertEqual(s.getvalue(), 'depends_on:0:1:2\n')
+
+ def test_dep_id_repeat(self):
+ """
+
+ :return:
+ """
+ s = StringIOWrapper('test_suite_ut.data', '')
+ unique_deps = []
+ dep_check_code = ''
+ dep_check_code += write_deps(s, ['DEP3', 'DEP2'], unique_deps)
+ dep_check_code += write_deps(s, ['DEP2', 'DEP1'], unique_deps)
+ dep_check_code += write_deps(s, ['DEP1', 'DEP3'], unique_deps)
+ expect_dep_check_code = '''
+ case 0:
+ {
+#if defined(DEP3)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;
+ case 1:
+ {
+#if defined(DEP2)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;
+ case 2:
+ {
+#if defined(DEP1)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;'''
+ self.assertEqual(dep_check_code, expect_dep_check_code)
+ self.assertEqual(len(unique_deps), 3)
+ self.assertEqual(s.getvalue(), 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
+
+
+class WriteParams(TestCase):
+ """
+ Test Suite for testing write_parameters().
+ """
+
+ def test_no_params(self):
+ """
+ Test with empty test_args
+ :return:
+ """
+ s = StringIOWrapper('test_suite_ut.data', '')
+ unique_expressions = []
+ expression_code = write_parameters(s, [], [], unique_expressions)
+ self.assertEqual(len(unique_expressions), 0)
+ self.assertEqual(expression_code, '')
+ self.assertEqual(s.getvalue(), '\n')
+
+ def test_no_exp_param(self):
+ """
+ Test when there is no macro or expression in the params.
+ :return:
+ """
+ s = StringIOWrapper('test_suite_ut.data', '')
+ unique_expressions = []
+ expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0'], ['char*', 'hex', 'int'],
+ unique_expressions)
+ self.assertEqual(len(unique_expressions), 0)
+ self.assertEqual(expression_code, '')
+ self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0\n')
+
+ def test_hex_format_int_param(self):
+ """
+ Test int parameter in hex format.
+ :return:
+ """
+ s = StringIOWrapper('test_suite_ut.data', '')
+ unique_expressions = []
+ expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0xAA'], ['char*', 'hex', 'int'],
+ unique_expressions)
+ self.assertEqual(len(unique_expressions), 0)
+ self.assertEqual(expression_code, '')
+ self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
+
+ def test_with_exp_param(self):
+ """
+ Test when there is macro or expression in the params.
+ :return:
+ """
+ s = StringIOWrapper('test_suite_ut.data', '')
+ unique_expressions = []
+ expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0', 'MACRO1', 'MACRO2', 'MACRO3'],
+ ['char*', 'hex', 'int', 'int', 'int', 'int'],
+ unique_expressions)
+ self.assertEqual(len(unique_expressions), 3)
+ self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
+ expected_expression_code = '''
+ case 0:
+ {
+ *out_value = MACRO1;
+ }
+ break;
+ case 1:
+ {
+ *out_value = MACRO2;
+ }
+ break;
+ case 2:
+ {
+ *out_value = MACRO3;
+ }
+ break;'''
+ self.assertEqual(expression_code, expected_expression_code)
+ self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1:exp:2\n')
+
+ def test_with_repeate_calls(self):
+ """
+ Test when write_parameter() is called with same macro or expression.
+ :return:
+ """
+ s = StringIOWrapper('test_suite_ut.data', '')
+ unique_expressions = []
+ expression_code = ''
+ expression_code += write_parameters(s, ['"Yahoo"', 'MACRO1', 'MACRO2'], ['char*', 'int', 'int'],
+ unique_expressions)
+ expression_code += write_parameters(s, ['"abcdef00"', 'MACRO2', 'MACRO3'], ['hex', 'int', 'int'],
+ unique_expressions)
+ expression_code += write_parameters(s, ['0', 'MACRO3', 'MACRO1'], ['int', 'int', 'int'],
+ unique_expressions)
+ self.assertEqual(len(unique_expressions), 3)
+ self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
+ expected_expression_code = '''
+ case 0:
+ {
+ *out_value = MACRO1;
+ }
+ break;
+ case 1:
+ {
+ *out_value = MACRO2;
+ }
+ break;
+ case 2:
+ {
+ *out_value = MACRO3;
+ }
+ break;'''
+ self.assertEqual(expression_code, expected_expression_code)
+ expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
+:hex:"abcdef00":exp:1:exp:2
+:int:0:exp:2:exp:0
+'''
+ self.assertEqual(s.getvalue(), expected_data_file)
+
+
+class GenTestSuiteDepsChecks(TestCase):
+ """
+
+ """
+ def test_empty_suite_deps(self):
+ """
+ Test with empty suite_deps list.
+
+ :return:
+ """
+ dep_check_code, expression_code = gen_suite_deps_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
+ self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
+ self.assertEqual(expression_code, 'EXPRESSION_CODE')
+
+ def test_suite_deps(self):
+ """
+ Test with suite_deps list.
+
+ :return:
+ """
+ dep_check_code, expression_code = gen_suite_deps_checks(['SUITE_DEP'], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
+ exprectd_dep_check_code = '''
+#if defined(SUITE_DEP)
+DEP_CHECK_CODE
+#endif
+'''
+ expected_expression_code = '''
+#if defined(SUITE_DEP)
+EXPRESSION_CODE
+#endif
+'''
+ self.assertEqual(dep_check_code, exprectd_dep_check_code)
+ self.assertEqual(expression_code, expected_expression_code)
+
+ def test_no_dep_no_exp(self):
+ """
+ Test when there are no dependency and expression code.
+ :return:
+ """
+ dep_check_code, expression_code = gen_suite_deps_checks([], '', '')
+ self.assertEqual(dep_check_code, '')
+ self.assertEqual(expression_code, '')
+
+
+class GenFromTestData(TestCase):
+ """
+ Test suite for gen_from_test_data()
+ """
+
+ @patch("generate_test_code.write_deps")
+ @patch("generate_test_code.write_parameters")
+ @patch("generate_test_code.gen_suite_deps_checks")
+ def test_intermediate_data_file(self, gen_suite_deps_checks_mock, write_parameters_mock, write_deps_mock):
+ """
+ Test that intermediate data file is written with expected data.
+ :return:
+ """
+ data = '''
+My test
+depends_on:DEP1
+func1:0
+'''
+ data_f = StringIOWrapper('test_suite_ut.data', data)
+ out_data_f = StringIOWrapper('test_suite_ut.datax', '')
+ func_info = {'test_func1': (1, ('int',))}
+ suite_deps = []
+ write_parameters_mock.side_effect = write_parameters
+ write_deps_mock.side_effect = write_deps
+ gen_suite_deps_checks_mock.side_effect = gen_suite_deps_checks
+ gen_from_test_data(data_f, out_data_f, func_info, suite_deps)
+ write_deps_mock.assert_called_with(out_data_f, ['DEP1'], ['DEP1'])
+ write_parameters_mock.assert_called_with(out_data_f, ['0'], ('int',), [])
+ expected_dep_check_code = '''
+ case 0:
+ {
+#if defined(DEP1)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;'''
+ gen_suite_deps_checks_mock.assert_called_with(suite_deps, expected_dep_check_code, '')
+
+ def test_function_not_found(self):
+ """
+ Test that AssertError is raised when function info in not found.
+ :return:
+ """
+ data = '''
+My test
+depends_on:DEP1
+func1:0
+'''
+ data_f = StringIOWrapper('test_suite_ut.data', data)
+ out_data_f = StringIOWrapper('test_suite_ut.datax', '')
+ func_info = {'test_func2': (1, ('int',))}
+ suite_deps = []
+ self.assertRaises(AssertionError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
+
+ def test_different_func_args(self):
+ """
+ Test that AssertError is raised when no. of parameters and function args differ.
+ :return:
+ """
+ data = '''
+My test
+depends_on:DEP1
+func1:0
+'''
+ data_f = StringIOWrapper('test_suite_ut.data', data)
+ out_data_f = StringIOWrapper('test_suite_ut.datax', '')
+ func_info = {'test_func2': (1, ('int','hex'))}
+ suite_deps = []
+ self.assertRaises(AssertionError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
+
+ def test_output(self):
+ """
+ Test that intermediate data file is written with expected data.
+ :return:
+ """
+ data = '''
+My test 1
+depends_on:DEP1
+func1:0:0xfa:MACRO1:MACRO2
+
+My test 2
+depends_on:DEP1:DEP2
+func2:"yahoo":88:MACRO1
+'''
+ data_f = StringIOWrapper('test_suite_ut.data', data)
+ out_data_f = StringIOWrapper('test_suite_ut.datax', '')
+ func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')), 'test_func2': (1, ('char*', 'int', 'int'))}
+ suite_deps = []
+ dep_check_code, expression_code = gen_from_test_data(data_f, out_data_f, func_info, suite_deps)
+ expected_dep_check_code = '''
+ case 0:
+ {
+#if defined(DEP1)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;
+ case 1:
+ {
+#if defined(DEP2)
+ ret = DEPENDENCY_SUPPORTED;
+#else
+ ret = DEPENDENCY_NOT_SUPPORTED;
+#endif
+ }
+ break;'''
+ expecrted_data = '''My test 1
+depends_on:0
+0:int:0:int:0xfa:exp:0:exp:1
+
+My test 2
+depends_on:0:1
+1:char*:"yahoo":int:88:exp:0
+
+'''
+ expected_expression_code = '''
+ case 0:
+ {
+ *out_value = MACRO1;
+ }
+ break;
+ case 1:
+ {
+ *out_value = MACRO2;
+ }
+ break;'''
+ self.assertEqual(dep_check_code, expected_dep_check_code)
+ self.assertEqual(out_data_f.getvalue(), expecrted_data)
+ self.assertEqual(expression_code, expected_expression_code)
+
+
+if __name__=='__main__':
+ unittest_main()