2018-07-03 10:57:54 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-03-06 11:49:41 +00:00
|
|
|
# Unit test for generate_test_code.py
|
2017-08-02 13:47:13 +00:00
|
|
|
#
|
2018-07-05 13:20:08 +00:00
|
|
|
# Copyright (C) 2018, Arm Limited, All Rights Reserved
|
2017-08-02 13:47:13 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
2018-07-03 10:57:54 +00:00
|
|
|
# This file is part of Mbed TLS (https://tls.mbed.org)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
"""
|
2018-03-06 11:49:41 +00:00
|
|
|
Unit tests for generate_test_code.py
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
|
2019-02-25 20:39:42 +00:00
|
|
|
# pylint: disable=wrong-import-order
|
2018-07-05 23:29:50 +00:00
|
|
|
try:
|
|
|
|
# Python 2
|
|
|
|
from StringIO import StringIO
|
|
|
|
except ImportError:
|
|
|
|
# Python 3
|
|
|
|
from io import StringIO
|
2018-07-03 10:57:54 +00:00
|
|
|
from unittest import TestCase, main as unittest_main
|
2018-07-05 23:29:50 +00:00
|
|
|
try:
|
|
|
|
# Python 2
|
|
|
|
from mock import patch
|
|
|
|
except ImportError:
|
|
|
|
# Python 3
|
|
|
|
from unittest.mock import patch
|
2019-02-25 20:39:42 +00:00
|
|
|
# pylint: enable=wrong-import-order
|
2018-07-03 10:57:54 +00:00
|
|
|
from generate_test_code import gen_dependencies, gen_dependencies_one_line
|
|
|
|
from generate_test_code import gen_function_wrapper, gen_dispatch
|
|
|
|
from generate_test_code import parse_until_pattern, GeneratorInputError
|
|
|
|
from generate_test_code import parse_suite_dependencies
|
|
|
|
from generate_test_code import parse_function_dependencies
|
2018-07-05 16:31:46 +00:00
|
|
|
from generate_test_code import parse_function_arguments, parse_function_code
|
2018-07-03 10:57:54 +00:00
|
|
|
from generate_test_code import parse_functions, END_HEADER_REGEX
|
|
|
|
from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
|
|
|
|
from generate_test_code import parse_test_data, gen_dep_check
|
|
|
|
from generate_test_code import gen_expression_check, write_dependencies
|
|
|
|
from generate_test_code import write_parameters, gen_suite_dep_checks
|
|
|
|
from generate_test_code import gen_from_test_data
|
|
|
|
|
|
|
|
|
2017-06-30 08:35:21 +00:00
|
|
|
class GenDep(TestCase):
|
|
|
|
"""
|
|
|
|
Test suite for function gen_dep()
|
|
|
|
"""
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = ['DEP1', 'DEP2']
|
|
|
|
dep_start, dep_end = gen_dependencies(dependencies)
|
|
|
|
preprocessor1, preprocessor2 = dep_start.splitlines()
|
2017-06-30 08:35:21 +00:00
|
|
|
endif1, endif2 = dep_end.splitlines()
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(preprocessor1, '#if defined(DEP1)',
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(preprocessor2, '#if defined(DEP2)',
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(endif1, '#endif /* DEP2 */',
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(endif2, '#endif /* DEP1 */',
|
|
|
|
'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_disabled_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = ['!DEP1', '!DEP2']
|
|
|
|
dep_start, dep_end = gen_dependencies(dependencies)
|
|
|
|
preprocessor1, preprocessor2 = dep_start.splitlines()
|
2017-06-30 08:35:21 +00:00
|
|
|
endif1, endif2 = dep_end.splitlines()
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(preprocessor1, '#if !defined(DEP1)',
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(preprocessor2, '#if !defined(DEP2)',
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(endif1, '#endif /* !DEP2 */',
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(endif2, '#endif /* !DEP1 */',
|
|
|
|
'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_mixed_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = ['!DEP1', 'DEP2']
|
|
|
|
dep_start, dep_end = gen_dependencies(dependencies)
|
|
|
|
preprocessor1, preprocessor2 = dep_start.splitlines()
|
2017-06-30 08:35:21 +00:00
|
|
|
endif1, endif2 = dep_end.splitlines()
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(preprocessor1, '#if !defined(DEP1)',
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(preprocessor2, '#if defined(DEP2)',
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(endif1, '#endif /* DEP2 */',
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(endif2, '#endif /* !DEP1 */',
|
|
|
|
'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_empty_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = []
|
|
|
|
dep_start, dep_end = gen_dependencies(dependencies)
|
|
|
|
self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_large_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = []
|
2017-06-30 08:35:21 +00:00
|
|
|
count = 10
|
|
|
|
for i in range(count):
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies.append('DEP%d' % i)
|
|
|
|
dep_start, dep_end = gen_dependencies(dependencies)
|
|
|
|
self.assertEqual(len(dep_start.splitlines()), count,
|
|
|
|
'Preprocessor generated incorrectly')
|
|
|
|
self.assertEqual(len(dep_end.splitlines()), count,
|
|
|
|
'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenDepOneLine(TestCase):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test Suite for testing gen_dependencies_one_line()
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = ['DEP1', 'DEP2']
|
|
|
|
dep_str = gen_dependencies_one_line(dependencies)
|
|
|
|
self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
|
|
|
|
'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_disabled_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2017-06-30 08:35:21 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = ['!DEP1', '!DEP2']
|
|
|
|
dep_str = gen_dependencies_one_line(dependencies)
|
|
|
|
self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
|
|
|
|
'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_mixed_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2017-06-30 08:35:21 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = ['!DEP1', 'DEP2']
|
|
|
|
dep_str = gen_dependencies_one_line(dependencies)
|
|
|
|
self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
|
|
|
|
'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_empty_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2017-06-30 08:35:21 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = []
|
|
|
|
dep_str = gen_dependencies_one_line(dependencies)
|
|
|
|
self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_large_dependencies_list(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that gen_dep() correctly creates dependencies for given
|
|
|
|
dependency list.
|
2017-06-30 08:35:21 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = []
|
2017-06-30 08:35:21 +00:00
|
|
|
count = 10
|
|
|
|
for i in range(count):
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies.append('DEP%d' % i)
|
|
|
|
dep_str = gen_dependencies_one_line(dependencies)
|
|
|
|
expected = '#if ' + ' && '.join(['defined(%s)' %
|
|
|
|
x for x in dependencies])
|
|
|
|
self.assertEqual(dep_str, expected,
|
|
|
|
'Preprocessor generated incorrectly')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
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.
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
|
|
|
|
expected = '''
|
|
|
|
void test_a_wrapper( void ** params )
|
|
|
|
{
|
2018-06-13 15:31:26 +00:00
|
|
|
|
2017-06-30 08:35:21 +00:00
|
|
|
test_a( a, b, c, d );
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
self.assertEqual(code, expected)
|
|
|
|
|
|
|
|
def test_local(self):
|
|
|
|
"""
|
|
|
|
Test that params are properly unpacked in the function call.
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
code = gen_function_wrapper('test_a',
|
|
|
|
'int x = 1;', ('x', 'b', 'c', 'd'))
|
2017-06-30 08:35:21 +00:00
|
|
|
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.
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
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.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
|
|
|
|
expected = '''
|
|
|
|
#if defined(DEP1) && defined(DEP2)
|
|
|
|
test_a_wrapper,
|
|
|
|
#else
|
|
|
|
NULL,
|
|
|
|
#endif
|
|
|
|
'''
|
|
|
|
self.assertEqual(code, expected)
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_empty_dependencies(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
Test empty dependency list.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2018-07-05 13:20:08 +00:00
|
|
|
def __init__(self, file_name, data, line_no=0):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
Init file handle.
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:param file_name:
|
2017-06-30 08:35:21 +00:00
|
|
|
:param data:
|
|
|
|
:param line_no:
|
|
|
|
"""
|
|
|
|
super(StringIOWrapper, self).__init__(data)
|
|
|
|
self.line_no = line_no
|
|
|
|
self.name = file_name
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Iterator method. This method overrides base class's
|
|
|
|
next method and extends the next method to count the line
|
|
|
|
numbers as each line is read.
|
|
|
|
|
|
|
|
:return: Line read from file.
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-05 23:29:50 +00:00
|
|
|
parent = super(StringIOWrapper, self)
|
|
|
|
if getattr(parent, 'next', None):
|
|
|
|
# Python 2
|
|
|
|
line = parent.next()
|
|
|
|
else:
|
|
|
|
# Python 3
|
|
|
|
line = parent.__next__()
|
2018-07-05 13:20:08 +00:00
|
|
|
return line
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-05 23:29:50 +00:00
|
|
|
# Python 3
|
2018-07-03 10:57:54 +00:00
|
|
|
__next__ = next
|
|
|
|
|
|
|
|
def readline(self, length=0):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
Wrap the base class readline.
|
2018-06-13 15:31:26 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
:param length:
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2019-02-25 20:39:42 +00:00
|
|
|
# pylint: disable=unused-argument
|
2017-06-30 08:35:21 +00:00
|
|
|
line = super(StringIOWrapper, self).readline()
|
2018-07-05 13:20:08 +00:00
|
|
|
if line is not None:
|
2017-06-30 08:35:21 +00:00
|
|
|
self.line_no += 1
|
|
|
|
return line
|
|
|
|
|
|
|
|
|
2018-02-06 13:08:01 +00:00
|
|
|
class ParseUntilPattern(TestCase):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-02-06 13:08:01 +00:00
|
|
|
Test Suite for testing parse_until_pattern().
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_suite_headers(self):
|
|
|
|
"""
|
|
|
|
Test that suite headers are parsed correctly.
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''#include "mbedtls/ecp.h"
|
|
|
|
|
|
|
|
#define ECP_PF_UNKNOWN -1
|
|
|
|
/* END_HEADER */
|
|
|
|
'''
|
|
|
|
expected = '''#line 1 "test_suite_ut.function"
|
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
|
|
|
|
#define ECP_PF_UNKNOWN -1
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data, line_no=0)
|
|
|
|
headers = parse_until_pattern(stream, END_HEADER_REGEX)
|
2017-06-30 08:35:21 +00:00
|
|
|
self.assertEqual(headers, expected)
|
|
|
|
|
|
|
|
def test_line_no(self):
|
|
|
|
"""
|
2018-06-13 15:31:26 +00:00
|
|
|
Test that #line is set to correct line no. in source .function file.
|
|
|
|
|
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''#include "mbedtls/ecp.h"
|
|
|
|
|
|
|
|
#define ECP_PF_UNKNOWN -1
|
|
|
|
/* END_HEADER */
|
|
|
|
'''
|
|
|
|
offset_line_no = 5
|
|
|
|
expected = '''#line %d "test_suite_ut.function"
|
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
|
|
|
|
#define ECP_PF_UNKNOWN -1
|
|
|
|
''' % (offset_line_no + 1)
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data,
|
|
|
|
offset_line_no)
|
|
|
|
headers = parse_until_pattern(stream, END_HEADER_REGEX)
|
2017-06-30 08:35:21 +00:00
|
|
|
self.assertEqual(headers, expected)
|
|
|
|
|
|
|
|
def test_no_end_header_comment(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that InvalidFileFormat is raised when end header comment is
|
|
|
|
missing.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''#include "mbedtls/ecp.h"
|
|
|
|
|
|
|
|
#define ECP_PF_UNKNOWN -1
|
|
|
|
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
self.assertRaises(GeneratorInputError, parse_until_pattern, stream,
|
|
|
|
END_HEADER_REGEX)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
class ParseSuiteDependencies(TestCase):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test Suite for testing parse_suite_dependencies().
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_suite_dependencies(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''
|
|
|
|
* depends_on:MBEDTLS_ECP_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
'''
|
|
|
|
expected = ['MBEDTLS_ECP_C']
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
dependencies = parse_suite_dependencies(stream)
|
|
|
|
self.assertEqual(dependencies, expected)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
def test_no_end_dep_comment(self):
|
|
|
|
"""
|
|
|
|
Test that InvalidFileFormat is raised when end dep comment is missing.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''
|
|
|
|
* depends_on:MBEDTLS_ECP_C
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
self.assertRaises(GeneratorInputError, parse_suite_dependencies,
|
|
|
|
stream)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_dependencies_split(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
Test that InvalidFileFormat is raised when end dep comment is missing.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''
|
2018-06-13 15:31:26 +00:00
|
|
|
* depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
|
2017-06-30 08:35:21 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
'''
|
|
|
|
expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
dependencies = parse_suite_dependencies(stream)
|
|
|
|
self.assertEqual(dependencies, expected)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
class ParseFuncDependencies(TestCase):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test Suite for testing parse_function_dependencies()
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_function_dependencies(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that parse_function_dependencies() correctly parses function
|
|
|
|
dependencies.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
line = '/* BEGIN_CASE ' \
|
|
|
|
'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
|
2017-06-30 08:35:21 +00:00
|
|
|
expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = parse_function_dependencies(line)
|
|
|
|
self.assertEqual(dependencies, expected)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_no_dependencies(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that parse_function_dependencies() correctly parses function
|
|
|
|
dependencies.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
line = '/* BEGIN_CASE */'
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = parse_function_dependencies(line)
|
|
|
|
self.assertEqual(dependencies, [])
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_tolerance(self):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that parse_function_dependencies() correctly parses function
|
|
|
|
dependencies.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies = parse_function_dependencies(line)
|
|
|
|
self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ParseFuncSignature(TestCase):
|
|
|
|
"""
|
2018-07-05 16:31:46 +00:00
|
|
|
Test Suite for parse_function_arguments().
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_int_and_char_params(self):
|
|
|
|
"""
|
2017-08-02 13:47:13 +00:00
|
|
|
Test int and char parameters parsing
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
line = 'void entropy_threshold( char * a, int b, int result )'
|
2018-07-05 16:31:46 +00:00
|
|
|
args, local, arg_dispatch = parse_function_arguments(line)
|
2017-06-30 08:35:21 +00:00
|
|
|
self.assertEqual(args, ['char*', 'int', 'int'])
|
|
|
|
self.assertEqual(local, '')
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(arg_dispatch, ['(char *) params[0]',
|
|
|
|
'*( (int *) params[1] )',
|
|
|
|
'*( (int *) params[2] )'])
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
def test_hex_params(self):
|
|
|
|
"""
|
2017-08-02 13:47:13 +00:00
|
|
|
Test hex parameters parsing
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-06-29 10:05:32 +00:00
|
|
|
line = 'void entropy_threshold( char * a, data_t * h, int result )'
|
2018-07-05 16:31:46 +00:00
|
|
|
args, local, arg_dispatch = parse_function_arguments(line)
|
2017-06-30 08:35:21 +00:00
|
|
|
self.assertEqual(args, ['char*', 'hex', 'int'])
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(local,
|
2018-07-05 13:20:08 +00:00
|
|
|
' data_t data1 = {(uint8_t *) params[1], '
|
2018-07-03 10:57:54 +00:00
|
|
|
'*( (uint32_t *) params[2] )};\n')
|
|
|
|
self.assertEqual(arg_dispatch, ['(char *) params[0]',
|
2018-07-05 13:20:08 +00:00
|
|
|
'&data1',
|
2018-07-03 10:57:54 +00:00
|
|
|
'*( (int *) params[3] )'])
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
def test_unsupported_arg(self):
|
|
|
|
"""
|
2018-06-29 10:05:32 +00:00
|
|
|
Test unsupported arguments (not among int, char * and data_t)
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-05 16:31:46 +00:00
|
|
|
line = 'void entropy_threshold( char * a, data_t * h, char result )'
|
|
|
|
self.assertRaises(ValueError, parse_function_arguments, line)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
def test_no_params(self):
|
|
|
|
"""
|
2017-08-02 13:47:13 +00:00
|
|
|
Test no parameters.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
line = 'void entropy_threshold()'
|
2018-07-05 16:31:46 +00:00
|
|
|
args, local, arg_dispatch = parse_function_arguments(line)
|
2017-06-30 08:35:21 +00:00
|
|
|
self.assertEqual(args, [])
|
|
|
|
self.assertEqual(local, '')
|
|
|
|
self.assertEqual(arg_dispatch, [])
|
|
|
|
|
|
|
|
|
|
|
|
class ParseFunctionCode(TestCase):
|
|
|
|
"""
|
|
|
|
Test suite for testing parse_function_code()
|
|
|
|
"""
|
|
|
|
|
2018-07-05 23:29:50 +00:00
|
|
|
def assert_raises_regex(self, exp, regex, func, *args):
|
|
|
|
"""
|
|
|
|
Python 2 & 3 portable wrapper of assertRaisesRegex(p)? function.
|
|
|
|
|
|
|
|
:param exp: Exception type expected to be raised by cb.
|
|
|
|
:param regex: Expected exception message
|
|
|
|
:param func: callable object under test
|
|
|
|
:param args: variable positional arguments
|
|
|
|
"""
|
|
|
|
parent = super(ParseFunctionCode, self)
|
|
|
|
|
|
|
|
# Pylint does not appreciate that the super method called
|
|
|
|
# conditionally can be available in other Python version
|
|
|
|
# then that of Pylint.
|
|
|
|
# Workaround is to call the method via getattr.
|
|
|
|
# Pylint ignores that the method got via getattr is
|
|
|
|
# conditionally executed. Method has to be a callable.
|
|
|
|
# Hence, using a dummy callable for getattr default.
|
|
|
|
dummy = lambda *x: None
|
|
|
|
# First Python 3 assertRaisesRegex is checked, since Python 2
|
|
|
|
# assertRaisesRegexp is also available in Python 3 but is
|
|
|
|
# marked deprecated.
|
|
|
|
for name in ('assertRaisesRegex', 'assertRaisesRegexp'):
|
|
|
|
method = getattr(parent, name, dummy)
|
|
|
|
if method is not dummy:
|
|
|
|
method(exp, regex, func, *args)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AttributeError(" 'ParseFunctionCode' object has no attribute"
|
|
|
|
" 'assertRaisesRegex' or 'assertRaisesRegexp'"
|
|
|
|
)
|
|
|
|
|
2017-06-30 08:35:21 +00:00
|
|
|
def test_no_function(self):
|
|
|
|
"""
|
2017-08-02 13:47:13 +00:00
|
|
|
Test no test function found.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''
|
|
|
|
No
|
|
|
|
test
|
|
|
|
function
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
2018-07-05 16:31:46 +00:00
|
|
|
err_msg = 'file: test_suite_ut.function - Test functions not found!'
|
2018-07-05 23:29:50 +00:00
|
|
|
self.assert_raises_regex(GeneratorInputError, err_msg,
|
|
|
|
parse_function_code, stream, [], [])
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
def test_no_end_case_comment(self):
|
|
|
|
"""
|
2017-08-02 13:47:13 +00:00
|
|
|
Test missing end case.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''
|
|
|
|
void test_func()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
2018-07-05 16:31:46 +00:00
|
|
|
err_msg = r'file: test_suite_ut.function - '\
|
|
|
|
'end case pattern .*? not found!'
|
2018-07-05 23:29:50 +00:00
|
|
|
self.assert_raises_regex(GeneratorInputError, err_msg,
|
|
|
|
parse_function_code, stream, [], [])
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-05 16:31:46 +00:00
|
|
|
@patch("generate_test_code.parse_function_arguments")
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_function_called(self,
|
2018-07-05 16:31:46 +00:00
|
|
|
parse_function_arguments_mock):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2017-08-02 13:47:13 +00:00
|
|
|
Test parse_function_code()
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-05 16:31:46 +00:00
|
|
|
parse_function_arguments_mock.return_value = ([], '', [])
|
2017-06-30 08:35:21 +00:00
|
|
|
data = '''
|
|
|
|
void test_func()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
self.assertRaises(GeneratorInputError, parse_function_code,
|
|
|
|
stream, [], [])
|
2018-07-05 16:31:46 +00:00
|
|
|
self.assertTrue(parse_function_arguments_mock.called)
|
|
|
|
parse_function_arguments_mock.assert_called_with('void test_func()\n')
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-03-06 11:49:41 +00:00
|
|
|
@patch("generate_test_code.gen_dispatch")
|
2018-07-03 10:57:54 +00:00
|
|
|
@patch("generate_test_code.gen_dependencies")
|
2018-03-06 11:49:41 +00:00
|
|
|
@patch("generate_test_code.gen_function_wrapper")
|
2018-07-05 16:31:46 +00:00
|
|
|
@patch("generate_test_code.parse_function_arguments")
|
|
|
|
def test_return(self, parse_function_arguments_mock,
|
2018-07-03 10:57:54 +00:00
|
|
|
gen_function_wrapper_mock,
|
|
|
|
gen_dependencies_mock,
|
|
|
|
gen_dispatch_mock):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2017-08-02 13:47:13 +00:00
|
|
|
Test generated code.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-05 16:31:46 +00:00
|
|
|
parse_function_arguments_mock.return_value = ([], '', [])
|
2017-06-30 08:35:21 +00:00
|
|
|
gen_function_wrapper_mock.return_value = ''
|
2018-07-03 10:57:54 +00:00
|
|
|
gen_dependencies_mock.side_effect = gen_dependencies
|
2017-06-30 08:35:21 +00:00
|
|
|
gen_dispatch_mock.side_effect = gen_dispatch
|
|
|
|
data = '''
|
|
|
|
void func()
|
|
|
|
{
|
|
|
|
ba ba black sheep
|
|
|
|
have you any wool
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
name, arg, code, dispatch_code = parse_function_code(stream, [], [])
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-05 16:31:46 +00:00
|
|
|
self.assertTrue(parse_function_arguments_mock.called)
|
|
|
|
parse_function_arguments_mock.assert_called_with('void func()\n')
|
2017-06-30 08:35:21 +00:00
|
|
|
gen_function_wrapper_mock.assert_called_with('test_func', '', [])
|
|
|
|
self.assertEqual(name, 'test_func')
|
|
|
|
self.assertEqual(arg, [])
|
2018-07-05 13:20:08 +00:00
|
|
|
expected = '''#line 1 "test_suite_ut.function"
|
|
|
|
|
2017-06-30 08:35:21 +00:00
|
|
|
void test_func()
|
|
|
|
{
|
|
|
|
ba ba black sheep
|
|
|
|
have you any wool
|
|
|
|
exit:
|
2018-07-05 13:20:08 +00:00
|
|
|
;
|
2017-06-30 08:35:21 +00:00
|
|
|
}
|
|
|
|
'''
|
|
|
|
self.assertEqual(code, expected)
|
|
|
|
self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
|
|
|
|
|
2018-03-06 11:49:41 +00:00
|
|
|
@patch("generate_test_code.gen_dispatch")
|
2018-07-03 10:57:54 +00:00
|
|
|
@patch("generate_test_code.gen_dependencies")
|
2018-03-06 11:49:41 +00:00
|
|
|
@patch("generate_test_code.gen_function_wrapper")
|
2018-07-05 16:31:46 +00:00
|
|
|
@patch("generate_test_code.parse_function_arguments")
|
|
|
|
def test_with_exit_label(self, parse_function_arguments_mock,
|
2018-07-03 10:57:54 +00:00
|
|
|
gen_function_wrapper_mock,
|
|
|
|
gen_dependencies_mock,
|
|
|
|
gen_dispatch_mock):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2017-08-02 13:47:13 +00:00
|
|
|
Test when exit label is present.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-05 16:31:46 +00:00
|
|
|
parse_function_arguments_mock.return_value = ([], '', [])
|
2017-06-30 08:35:21 +00:00
|
|
|
gen_function_wrapper_mock.return_value = ''
|
2018-07-03 10:57:54 +00:00
|
|
|
gen_dependencies_mock.side_effect = gen_dependencies
|
2017-06-30 08:35:21 +00:00
|
|
|
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 */
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
_, _, code, _ = parse_function_code(stream, [], [])
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-05 13:20:08 +00:00
|
|
|
expected = '''#line 1 "test_suite_ut.function"
|
|
|
|
|
2017-06-30 08:35:21 +00:00
|
|
|
void test_func()
|
|
|
|
{
|
|
|
|
ba ba black sheep
|
|
|
|
have you any wool
|
|
|
|
exit:
|
|
|
|
yes sir yes sir
|
|
|
|
3 bags full
|
|
|
|
}
|
2018-07-05 16:31:46 +00:00
|
|
|
'''
|
|
|
|
self.assertEqual(code, expected)
|
|
|
|
|
|
|
|
def test_non_void_function(self):
|
|
|
|
"""
|
|
|
|
Test invalid signature (non void).
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
data = 'int entropy_threshold( char * a, data_t * h, int result )'
|
|
|
|
err_msg = 'file: test_suite_ut.function - Test functions not found!'
|
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
2018-07-05 23:29:50 +00:00
|
|
|
self.assert_raises_regex(GeneratorInputError, err_msg,
|
|
|
|
parse_function_code, stream, [], [])
|
2018-07-05 16:31:46 +00:00
|
|
|
|
|
|
|
@patch("generate_test_code.gen_dispatch")
|
|
|
|
@patch("generate_test_code.gen_dependencies")
|
|
|
|
@patch("generate_test_code.gen_function_wrapper")
|
|
|
|
@patch("generate_test_code.parse_function_arguments")
|
|
|
|
def test_functio_name_on_newline(self, parse_function_arguments_mock,
|
|
|
|
gen_function_wrapper_mock,
|
|
|
|
gen_dependencies_mock,
|
|
|
|
gen_dispatch_mock):
|
|
|
|
"""
|
|
|
|
Test when exit label is present.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
parse_function_arguments_mock.return_value = ([], '', [])
|
|
|
|
gen_function_wrapper_mock.return_value = ''
|
|
|
|
gen_dependencies_mock.side_effect = gen_dependencies
|
|
|
|
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 */
|
|
|
|
'''
|
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
_, _, code, _ = parse_function_code(stream, [], [])
|
|
|
|
|
|
|
|
expected = '''#line 1 "test_suite_ut.function"
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
|
|
|
|
|
|
test_func()
|
|
|
|
{
|
|
|
|
ba ba black sheep
|
|
|
|
have you any wool
|
|
|
|
exit:
|
|
|
|
yes sir yes sir
|
|
|
|
3 bags full
|
|
|
|
}
|
2017-06-30 08:35:21 +00:00
|
|
|
'''
|
|
|
|
self.assertEqual(code, expected)
|
|
|
|
|
|
|
|
|
|
|
|
class ParseFunction(TestCase):
|
|
|
|
"""
|
|
|
|
Test Suite for testing parse_functions()
|
|
|
|
"""
|
|
|
|
|
2018-03-06 11:49:41 +00:00
|
|
|
@patch("generate_test_code.parse_until_pattern")
|
2018-02-06 13:08:01 +00:00
|
|
|
def test_begin_header(self, parse_until_pattern_mock):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-02-06 13:08:01 +00:00
|
|
|
Test that begin header is checked and parse_until_pattern() is called.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
def stop(*_unused):
|
|
|
|
"""Stop when parse_until_pattern is called."""
|
2017-06-30 08:35:21 +00:00
|
|
|
raise Exception
|
2018-02-06 13:08:01 +00:00
|
|
|
parse_until_pattern_mock.side_effect = stop
|
2017-06-30 08:35:21 +00:00
|
|
|
data = '''/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
|
|
|
|
#define ECP_PF_UNKNOWN -1
|
|
|
|
/* END_HEADER */
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
self.assertRaises(Exception, parse_functions, stream)
|
|
|
|
parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
|
2018-07-05 13:20:08 +00:00
|
|
|
self.assertEqual(stream.line_no, 1)
|
2018-02-06 13:08:01 +00:00
|
|
|
|
2018-03-06 11:49:41 +00:00
|
|
|
@patch("generate_test_code.parse_until_pattern")
|
2018-02-06 13:08:01 +00:00
|
|
|
def test_begin_helper(self, parse_until_pattern_mock):
|
|
|
|
"""
|
|
|
|
Test that begin helper is checked and parse_until_pattern() is called.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2018-02-06 13:08:01 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
def stop(*_unused):
|
|
|
|
"""Stop when parse_until_pattern is called."""
|
2018-02-06 13:08:01 +00:00
|
|
|
raise Exception
|
|
|
|
parse_until_pattern_mock.side_effect = stop
|
|
|
|
data = '''/* BEGIN_SUITE_HELPERS */
|
2018-07-03 10:57:54 +00:00
|
|
|
void print_hello_world()
|
2018-02-06 13:08:01 +00:00
|
|
|
{
|
2018-07-03 10:57:54 +00:00
|
|
|
printf("Hello World!\n");
|
2018-02-06 13:08:01 +00:00
|
|
|
}
|
|
|
|
/* END_SUITE_HELPERS */
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
self.assertRaises(Exception, parse_functions, stream)
|
|
|
|
parse_until_pattern_mock.assert_called_with(stream,
|
|
|
|
END_SUITE_HELPERS_REGEX)
|
2018-07-05 13:20:08 +00:00
|
|
|
self.assertEqual(stream.line_no, 1)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
@patch("generate_test_code.parse_suite_dependencies")
|
|
|
|
def test_begin_dep(self, parse_suite_dependencies_mock):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that begin dep is checked and parse_suite_dependencies() is
|
|
|
|
called.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
def stop(*_unused):
|
|
|
|
"""Stop when parse_until_pattern is called."""
|
2017-06-30 08:35:21 +00:00
|
|
|
raise Exception
|
2018-07-03 10:57:54 +00:00
|
|
|
parse_suite_dependencies_mock.side_effect = stop
|
2017-06-30 08:35:21 +00:00
|
|
|
data = '''/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_ECP_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
self.assertRaises(Exception, parse_functions, stream)
|
|
|
|
parse_suite_dependencies_mock.assert_called_with(stream)
|
2018-07-05 13:20:08 +00:00
|
|
|
self.assertEqual(stream.line_no, 1)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
@patch("generate_test_code.parse_function_dependencies")
|
|
|
|
def test_begin_function_dep(self, func_mock):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that begin dep is checked and parse_function_dependencies() is
|
|
|
|
called.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
def stop(*_unused):
|
|
|
|
"""Stop when parse_until_pattern is called."""
|
2017-06-30 08:35:21 +00:00
|
|
|
raise Exception
|
2018-07-03 10:57:54 +00:00
|
|
|
func_mock.side_effect = stop
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
dependencies_str = '/* BEGIN_CASE ' \
|
|
|
|
'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
|
2017-06-30 08:35:21 +00:00
|
|
|
data = '''%svoid test_func()
|
|
|
|
{
|
|
|
|
}
|
2018-07-03 10:57:54 +00:00
|
|
|
''' % dependencies_str
|
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
self.assertRaises(Exception, parse_functions, stream)
|
|
|
|
func_mock.assert_called_with(dependencies_str)
|
2018-07-05 13:20:08 +00:00
|
|
|
self.assertEqual(stream.line_no, 1)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
2018-03-06 11:49:41 +00:00
|
|
|
@patch("generate_test_code.parse_function_code")
|
2018-07-03 10:57:54 +00:00
|
|
|
@patch("generate_test_code.parse_function_dependencies")
|
|
|
|
def test_return(self, func_mock1, func_mock2):
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-02-06 13:08:01 +00:00
|
|
|
Test that begin case is checked and parse_function_code() is called.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
func_mock1.return_value = []
|
|
|
|
in_func_code = '''void test_func()
|
2017-06-30 08:35:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
func_dispatch = '''
|
|
|
|
test_func_wrapper,
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
func_mock2.return_value = 'test_func', [],\
|
|
|
|
in_func_code, func_dispatch
|
|
|
|
dependencies_str = '/* BEGIN_CASE ' \
|
|
|
|
'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
|
2017-06-30 08:35:21 +00:00
|
|
|
data = '''%svoid test_func()
|
|
|
|
{
|
|
|
|
}
|
2018-07-03 10:57:54 +00:00
|
|
|
''' % dependencies_str
|
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
suite_dependencies, dispatch_code, func_code, func_info = \
|
|
|
|
parse_functions(stream)
|
|
|
|
func_mock1.assert_called_with(dependencies_str)
|
|
|
|
func_mock2.assert_called_with(stream, [], [])
|
|
|
|
self.assertEqual(stream.line_no, 5)
|
|
|
|
self.assertEqual(suite_dependencies, [])
|
2017-06-30 08:35:21 +00:00
|
|
|
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):
|
|
|
|
"""
|
2018-02-06 13:08:01 +00:00
|
|
|
Test case parsing.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
|
|
|
|
#define ECP_PF_UNKNOWN -1
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_ECP_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
|
|
|
|
void func1()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
|
|
|
|
void func2()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
suite_dependencies, dispatch_code, func_code, func_info = \
|
|
|
|
parse_functions(stream)
|
|
|
|
self.assertEqual(stream.line_no, 23)
|
|
|
|
self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
expected_dispatch_code = '''/* Function Id: 0 */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
|
|
|
|
test_func1_wrapper,
|
|
|
|
#else
|
|
|
|
NULL,
|
|
|
|
#endif
|
|
|
|
/* Function Id: 1 */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
|
|
|
|
test_func2_wrapper,
|
|
|
|
#else
|
|
|
|
NULL,
|
|
|
|
#endif
|
|
|
|
'''
|
|
|
|
self.assertEqual(dispatch_code, expected_dispatch_code)
|
|
|
|
expected_func_code = '''#if defined(MBEDTLS_ECP_C)
|
2018-07-05 13:20:08 +00:00
|
|
|
#line 2 "test_suite_ut.function"
|
2017-06-30 08:35:21 +00:00
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
|
|
|
|
#define ECP_PF_UNKNOWN -1
|
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2018-07-05 13:20:08 +00:00
|
|
|
#line 13 "test_suite_ut.function"
|
2017-06-30 08:35:21 +00:00
|
|
|
void test_func1()
|
|
|
|
{
|
|
|
|
exit:
|
2018-07-05 13:20:08 +00:00
|
|
|
;
|
2017-06-30 08:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_func1_wrapper( void ** params )
|
|
|
|
{
|
|
|
|
(void)params;
|
|
|
|
|
|
|
|
test_func1( );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_FS_IO */
|
|
|
|
#endif /* MBEDTLS_ENTROPY_NV_SEED */
|
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2018-07-05 13:20:08 +00:00
|
|
|
#line 19 "test_suite_ut.function"
|
2017-06-30 08:35:21 +00:00
|
|
|
void test_func2()
|
|
|
|
{
|
|
|
|
exit:
|
2018-07-05 13:20:08 +00:00
|
|
|
;
|
2017-06-30 08:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_func2_wrapper( void ** params )
|
|
|
|
{
|
|
|
|
(void)params;
|
|
|
|
|
|
|
|
test_func2( );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_FS_IO */
|
|
|
|
#endif /* MBEDTLS_ENTROPY_NV_SEED */
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
|
|
|
'''
|
|
|
|
self.assertEqual(func_code, expected_func_code)
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(func_info, {'test_func1': (0, []),
|
|
|
|
'test_func2': (1, [])})
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
def test_same_function_name(self):
|
|
|
|
"""
|
2018-02-06 13:08:01 +00:00
|
|
|
Test name conflict.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-06-30 08:35:21 +00:00
|
|
|
"""
|
|
|
|
data = '''/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
|
|
|
|
#define ECP_PF_UNKNOWN -1
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_ECP_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
|
|
|
|
void func()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
|
|
|
|
void func()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
self.assertRaises(GeneratorInputError, parse_functions, stream)
|
2017-06-30 08:35:21 +00:00
|
|
|
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
class EscapedSplit(TestCase):
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
2017-07-06 16:34:27 +00:00
|
|
|
Test suite for testing escaped_split().
|
2018-07-03 10:57:54 +00:00
|
|
|
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.
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_invalid_input(self):
|
|
|
|
"""
|
|
|
|
Test when input split character is not a character.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
self.assertRaises(ValueError, escaped_split, '', 'string')
|
|
|
|
|
|
|
|
def test_empty_string(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test empty string input.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
splits = escaped_split('', ':')
|
|
|
|
self.assertEqual(splits, [])
|
|
|
|
|
|
|
|
def test_no_escape(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test with no escape character. The behaviour should be same as
|
|
|
|
str.split()
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
test_str = 'yahoo:google'
|
|
|
|
splits = escaped_split(test_str, ':')
|
|
|
|
self.assertEqual(splits, test_str.split(':'))
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
def test_escaped_input(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test input that has escaped delimiter.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
test_str = r'yahoo\:google:facebook'
|
|
|
|
splits = escaped_split(test_str, ':')
|
|
|
|
self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
def test_escaped_escape(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test input that has escaped delimiter.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
2018-07-05 13:20:08 +00:00
|
|
|
test_str = r'yahoo\\:google:facebook'
|
2018-07-03 10:57:54 +00:00
|
|
|
splits = escaped_split(test_str, ':')
|
2018-07-05 13:20:08 +00:00
|
|
|
self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
def test_all_at_once(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test input that has escaped delimiter.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
2018-07-05 13:20:08 +00:00
|
|
|
test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
|
2018-07-03 10:57:54 +00:00
|
|
|
splits = escaped_split(test_str, ':')
|
2018-07-05 13:20:08 +00:00
|
|
|
self.assertEqual(splits, [r'yahoo\\', r'google',
|
|
|
|
r'facebook\:instagram\\',
|
|
|
|
r'bbc\\', r'wikipedia'])
|
2017-07-06 16:34:27 +00:00
|
|
|
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
class ParseTestData(TestCase):
|
|
|
|
"""
|
|
|
|
Test suite for parse test data.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_parser(self):
|
|
|
|
"""
|
|
|
|
Test that tests are parsed correctly from data file.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
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:
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
tests = [(name, test_function, dependencies, args)
|
|
|
|
for name, test_function, dependencies, args in
|
|
|
|
parse_test_data(stream)]
|
|
|
|
test1, test2, test3, test4 = tests
|
|
|
|
self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
|
|
|
|
self.assertEqual(test1[1], 'dhm_do_dhm')
|
|
|
|
self.assertEqual(test1[2], [])
|
|
|
|
self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
|
|
|
|
|
|
|
|
self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
|
|
|
|
self.assertEqual(test2[1], 'dhm_do_dhm')
|
|
|
|
self.assertEqual(test2[2], [])
|
|
|
|
self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
|
|
|
|
'10', '"9345098304850938450983409622"'])
|
|
|
|
|
|
|
|
self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
|
|
|
|
self.assertEqual(test3[1], 'dhm_do_dhm')
|
|
|
|
self.assertEqual(test3[2], [])
|
|
|
|
self.assertEqual(test3[3], ['10',
|
|
|
|
'"9345098382739712938719287391879381271"',
|
|
|
|
'10',
|
|
|
|
'"9345098792137312973297123912791271"'])
|
|
|
|
|
|
|
|
self.assertEqual(test4[0], 'Diffie-Hellman selftest')
|
|
|
|
self.assertEqual(test4[1], 'dhm_selftest')
|
|
|
|
self.assertEqual(test4[2], [])
|
|
|
|
self.assertEqual(test4[3], [])
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
def test_with_dependencies(self):
|
|
|
|
"""
|
|
|
|
Test that tests with dependencies are parsed.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
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"
|
|
|
|
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
tests = [(name, function_name, dependencies, args)
|
|
|
|
for name, function_name, dependencies, args in
|
|
|
|
parse_test_data(stream)]
|
|
|
|
test1, test2 = tests
|
|
|
|
self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
|
|
|
|
self.assertEqual(test1[1], 'dhm_do_dhm')
|
|
|
|
self.assertEqual(test1[2], ['YAHOO'])
|
|
|
|
self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
|
|
|
|
|
|
|
|
self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
|
|
|
|
self.assertEqual(test2[1], 'dhm_do_dhm')
|
|
|
|
self.assertEqual(test2[2], [])
|
|
|
|
self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
|
|
|
|
'10', '"9345098304850938450983409622"'])
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
def test_no_args(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test GeneratorInputError is raised when test function name and
|
|
|
|
args line is missing.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
data = """
|
|
|
|
Diffie-Hellman full exchange #1
|
|
|
|
depends_on:YAHOO
|
|
|
|
|
|
|
|
|
|
|
|
Diffie-Hellman full exchange #2
|
|
|
|
dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
|
|
|
|
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
err = None
|
2017-07-03 12:58:20 +00:00
|
|
|
try:
|
2018-07-03 10:57:54 +00:00
|
|
|
for _, _, _, _ in parse_test_data(stream):
|
2017-07-03 12:58:20 +00:00
|
|
|
pass
|
2018-07-03 10:57:54 +00:00
|
|
|
except GeneratorInputError as err:
|
2018-07-05 23:29:50 +00:00
|
|
|
self.assertEqual(type(err), GeneratorInputError)
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
def test_incomplete_data(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test GeneratorInputError is raised when test function name
|
|
|
|
and args line is missing.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
data = """
|
|
|
|
Diffie-Hellman full exchange #1
|
|
|
|
depends_on:YAHOO
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
|
|
|
err = None
|
2017-07-03 12:58:20 +00:00
|
|
|
try:
|
2018-07-03 10:57:54 +00:00
|
|
|
for _, _, _, _ in parse_test_data(stream):
|
2017-07-03 12:58:20 +00:00
|
|
|
pass
|
2018-07-03 10:57:54 +00:00
|
|
|
except GeneratorInputError as err:
|
2018-07-05 23:29:50 +00:00
|
|
|
self.assertEqual(type(err), GeneratorInputError)
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenDepCheck(TestCase):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test suite for gen_dep_check(). It is assumed this function is
|
|
|
|
called with valid inputs.
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_gen_dep_check(self):
|
|
|
|
"""
|
|
|
|
Test that dependency check code generated correctly.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
expected = """
|
2017-07-10 10:54:01 +00:00
|
|
|
case 5:
|
|
|
|
{
|
2017-07-03 12:58:20 +00:00
|
|
|
#if defined(YAHOO)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-03 12:58:20 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-03 12:58:20 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;"""
|
2017-07-03 12:58:20 +00:00
|
|
|
out = gen_dep_check(5, 'YAHOO')
|
|
|
|
self.assertEqual(out, expected)
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_not_defined_dependency(self):
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
Test dependency with !.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
expected = """
|
2017-07-10 10:54:01 +00:00
|
|
|
case 5:
|
|
|
|
{
|
2017-07-03 12:58:20 +00:00
|
|
|
#if !defined(YAHOO)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-03 12:58:20 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-03 12:58:20 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;"""
|
2017-07-03 12:58:20 +00:00
|
|
|
out = gen_dep_check(5, '!YAHOO')
|
|
|
|
self.assertEqual(out, expected)
|
|
|
|
|
|
|
|
def test_empty_dependency(self):
|
|
|
|
"""
|
|
|
|
Test invalid dependency input.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
2018-06-26 13:35:25 +00:00
|
|
|
self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
def test_negative_dep_id(self):
|
|
|
|
"""
|
|
|
|
Test invalid dependency input.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
2018-06-26 13:35:25 +00:00
|
|
|
self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenExpCheck(TestCase):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test suite for gen_expression_check(). It is assumed this function
|
|
|
|
is called with valid inputs.
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_gen_exp_check(self):
|
|
|
|
"""
|
|
|
|
Test that expression check code generated correctly.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
|
|
|
expected = """
|
2017-07-10 10:54:01 +00:00
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
*out_value = YAHOO;
|
|
|
|
}
|
|
|
|
break;"""
|
2017-07-03 12:58:20 +00:00
|
|
|
out = gen_expression_check(5, 'YAHOO')
|
|
|
|
self.assertEqual(out, expected)
|
|
|
|
|
|
|
|
def test_invalid_expression(self):
|
|
|
|
"""
|
|
|
|
Test invalid expression input.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
2018-06-26 13:35:25 +00:00
|
|
|
self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
def test_negative_exp_id(self):
|
|
|
|
"""
|
|
|
|
Test invalid expression id.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-03 12:58:20 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertRaises(GeneratorInputError, gen_expression_check,
|
|
|
|
-1, 'YAHOO')
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
class WriteDependencies(TestCase):
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test suite for testing write_dependencies.
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_no_test_dependencies(self):
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test when test dependencies input is empty.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.data', '')
|
|
|
|
unique_dependencies = []
|
|
|
|
dep_check_code = write_dependencies(stream, [], unique_dependencies)
|
2017-07-06 16:34:27 +00:00
|
|
|
self.assertEqual(dep_check_code, '')
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(len(unique_dependencies), 0)
|
|
|
|
self.assertEqual(stream.getvalue(), '')
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
def test_unique_dep_ids(self):
|
|
|
|
"""
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.data', '')
|
|
|
|
unique_dependencies = []
|
|
|
|
dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
|
|
|
|
unique_dependencies)
|
2017-07-06 16:34:27 +00:00
|
|
|
expect_dep_check_code = '''
|
2017-07-10 10:54:01 +00:00
|
|
|
case 0:
|
|
|
|
{
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(DEP3)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(DEP2)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
{
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(DEP1)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;'''
|
2017-07-06 16:34:27 +00:00
|
|
|
self.assertEqual(dep_check_code, expect_dep_check_code)
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(len(unique_dependencies), 3)
|
|
|
|
self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
def test_dep_id_repeat(self):
|
|
|
|
"""
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.data', '')
|
|
|
|
unique_dependencies = []
|
2017-07-06 16:34:27 +00:00
|
|
|
dep_check_code = ''
|
2018-07-03 10:57:54 +00:00
|
|
|
dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
|
|
|
|
unique_dependencies)
|
|
|
|
dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
|
|
|
|
unique_dependencies)
|
|
|
|
dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
|
|
|
|
unique_dependencies)
|
2017-07-06 16:34:27 +00:00
|
|
|
expect_dep_check_code = '''
|
2017-07-10 10:54:01 +00:00
|
|
|
case 0:
|
|
|
|
{
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(DEP3)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(DEP2)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
{
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(DEP1)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;'''
|
2017-07-06 16:34:27 +00:00
|
|
|
self.assertEqual(dep_check_code, expect_dep_check_code)
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(len(unique_dependencies), 3)
|
|
|
|
self.assertEqual(stream.getvalue(),
|
|
|
|
'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WriteParams(TestCase):
|
|
|
|
"""
|
|
|
|
Test Suite for testing write_parameters().
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_no_params(self):
|
|
|
|
"""
|
|
|
|
Test with empty test_args
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.data', '')
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions = []
|
2018-07-03 10:57:54 +00:00
|
|
|
expression_code = write_parameters(stream, [], [], unique_expressions)
|
2017-07-06 16:34:27 +00:00
|
|
|
self.assertEqual(len(unique_expressions), 0)
|
|
|
|
self.assertEqual(expression_code, '')
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(stream.getvalue(), '\n')
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
def test_no_exp_param(self):
|
|
|
|
"""
|
|
|
|
Test when there is no macro or expression in the params.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.data', '')
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions = []
|
2018-07-03 10:57:54 +00:00
|
|
|
expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
|
|
|
|
'0'],
|
|
|
|
['char*', 'hex', 'int'],
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions)
|
|
|
|
self.assertEqual(len(unique_expressions), 0)
|
|
|
|
self.assertEqual(expression_code, '')
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(stream.getvalue(),
|
|
|
|
':char*:"Yahoo":hex:"abcdef00":int:0\n')
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
def test_hex_format_int_param(self):
|
|
|
|
"""
|
|
|
|
Test int parameter in hex format.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.data', '')
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions = []
|
2018-07-03 10:57:54 +00:00
|
|
|
expression_code = write_parameters(stream,
|
|
|
|
['"Yahoo"', '"abcdef00"', '0xAA'],
|
|
|
|
['char*', 'hex', 'int'],
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions)
|
|
|
|
self.assertEqual(len(unique_expressions), 0)
|
|
|
|
self.assertEqual(expression_code, '')
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(stream.getvalue(),
|
|
|
|
':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
def test_with_exp_param(self):
|
|
|
|
"""
|
|
|
|
Test when there is macro or expression in the params.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.data', '')
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions = []
|
2018-07-03 10:57:54 +00:00
|
|
|
expression_code = write_parameters(stream,
|
|
|
|
['"Yahoo"', '"abcdef00"', '0',
|
|
|
|
'MACRO1', 'MACRO2', 'MACRO3'],
|
|
|
|
['char*', 'hex', 'int',
|
|
|
|
'int', 'int', 'int'],
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions)
|
|
|
|
self.assertEqual(len(unique_expressions), 3)
|
|
|
|
self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
|
|
|
|
expected_expression_code = '''
|
2017-07-10 10:54:01 +00:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
*out_value = MACRO1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
*out_value = MACRO2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
*out_value = MACRO3;
|
|
|
|
}
|
|
|
|
break;'''
|
2017-07-06 16:34:27 +00:00
|
|
|
self.assertEqual(expression_code, expected_expression_code)
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(stream.getvalue(),
|
|
|
|
':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
|
|
|
|
':exp:2\n')
|
2017-07-06 16:34:27 +00:00
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_with_repeat_calls(self):
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
|
|
|
Test when write_parameter() is called with same macro or expression.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
stream = StringIOWrapper('test_suite_ut.data', '')
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions = []
|
|
|
|
expression_code = ''
|
2018-07-03 10:57:54 +00:00
|
|
|
expression_code += write_parameters(stream,
|
|
|
|
['"Yahoo"', 'MACRO1', 'MACRO2'],
|
|
|
|
['char*', 'int', 'int'],
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions)
|
2018-07-03 10:57:54 +00:00
|
|
|
expression_code += write_parameters(stream,
|
|
|
|
['"abcdef00"', 'MACRO2', 'MACRO3'],
|
|
|
|
['hex', 'int', 'int'],
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions)
|
2018-07-03 10:57:54 +00:00
|
|
|
expression_code += write_parameters(stream,
|
|
|
|
['0', 'MACRO3', 'MACRO1'],
|
|
|
|
['int', 'int', 'int'],
|
2017-07-06 16:34:27 +00:00
|
|
|
unique_expressions)
|
|
|
|
self.assertEqual(len(unique_expressions), 3)
|
|
|
|
self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
|
|
|
|
expected_expression_code = '''
|
2017-07-10 10:54:01 +00:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
*out_value = MACRO1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
*out_value = MACRO2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
*out_value = MACRO3;
|
|
|
|
}
|
|
|
|
break;'''
|
2017-07-06 16:34:27 +00:00
|
|
|
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
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(stream.getvalue(), expected_data_file)
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
class GenTestSuiteDependenciesChecks(TestCase):
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test suite for testing gen_suite_dep_checks()
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_empty_suite_dependencies(self):
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test with empty suite_dependencies list.
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dep_check_code, expression_code = \
|
|
|
|
gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
|
2017-07-06 16:34:27 +00:00
|
|
|
self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
|
|
|
|
self.assertEqual(expression_code, 'EXPRESSION_CODE')
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_suite_dependencies(self):
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test with suite_dependencies list.
|
2018-06-13 15:31:26 +00:00
|
|
|
|
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dep_check_code, expression_code = \
|
|
|
|
gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
|
|
|
|
'EXPRESSION_CODE')
|
|
|
|
expected_dep_check_code = '''
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(SUITE_DEP)
|
|
|
|
DEP_CHECK_CODE
|
|
|
|
#endif
|
|
|
|
'''
|
|
|
|
expected_expression_code = '''
|
|
|
|
#if defined(SUITE_DEP)
|
|
|
|
EXPRESSION_CODE
|
|
|
|
#endif
|
|
|
|
'''
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(dep_check_code, expected_dep_check_code)
|
2017-07-06 16:34:27 +00:00
|
|
|
self.assertEqual(expression_code, expected_expression_code)
|
|
|
|
|
|
|
|
def test_no_dep_no_exp(self):
|
|
|
|
"""
|
2018-06-13 15:31:26 +00:00
|
|
|
Test when there are no dependency and expression code.
|
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
|
2017-07-10 10:54:01 +00:00
|
|
|
self.assertEqual(dep_check_code, '')
|
|
|
|
self.assertEqual(expression_code, '')
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
|
2017-07-03 12:58:20 +00:00
|
|
|
class GenFromTestData(TestCase):
|
|
|
|
"""
|
|
|
|
Test suite for gen_from_test_data()
|
|
|
|
"""
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
@staticmethod
|
|
|
|
@patch("generate_test_code.write_dependencies")
|
2018-03-06 11:49:41 +00:00
|
|
|
@patch("generate_test_code.write_parameters")
|
2018-07-05 13:20:08 +00:00
|
|
|
@patch("generate_test_code.gen_suite_dep_checks")
|
2018-07-03 10:57:54 +00:00
|
|
|
def test_intermediate_data_file(func_mock1,
|
|
|
|
write_parameters_mock,
|
|
|
|
write_dependencies_mock):
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
|
|
|
Test that intermediate data file is written with expected data.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
|
|
|
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',))}
|
2018-07-03 10:57:54 +00:00
|
|
|
suite_dependencies = []
|
2017-07-06 16:34:27 +00:00
|
|
|
write_parameters_mock.side_effect = write_parameters
|
2018-07-03 10:57:54 +00:00
|
|
|
write_dependencies_mock.side_effect = write_dependencies
|
|
|
|
func_mock1.side_effect = gen_suite_dep_checks
|
|
|
|
gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
|
|
|
|
write_dependencies_mock.assert_called_with(out_data_f,
|
|
|
|
['DEP1'], ['DEP1'])
|
|
|
|
write_parameters_mock.assert_called_with(out_data_f, ['0'],
|
|
|
|
('int',), [])
|
2017-07-06 16:34:27 +00:00
|
|
|
expected_dep_check_code = '''
|
2017-07-10 10:54:01 +00:00
|
|
|
case 0:
|
|
|
|
{
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(DEP1)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;'''
|
2018-07-03 10:57:54 +00:00
|
|
|
func_mock1.assert_called_with(
|
|
|
|
suite_dependencies, expected_dep_check_code, '')
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
def test_function_not_found(self):
|
|
|
|
"""
|
|
|
|
Test that AssertError is raised when function info in not found.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
|
|
|
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',))}
|
2018-07-03 10:57:54 +00:00
|
|
|
suite_dependencies = []
|
|
|
|
self.assertRaises(GeneratorInputError, gen_from_test_data,
|
|
|
|
data_f, out_data_f, func_info, suite_dependencies)
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
def test_different_func_args(self):
|
|
|
|
"""
|
2018-07-03 10:57:54 +00:00
|
|
|
Test that AssertError is raised when no. of parameters and
|
|
|
|
function args differ.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
|
|
|
data = '''
|
|
|
|
My test
|
|
|
|
depends_on:DEP1
|
|
|
|
func1:0
|
|
|
|
'''
|
|
|
|
data_f = StringIOWrapper('test_suite_ut.data', data)
|
|
|
|
out_data_f = StringIOWrapper('test_suite_ut.datax', '')
|
2018-07-03 10:57:54 +00:00
|
|
|
func_info = {'test_func2': (1, ('int', 'hex'))}
|
|
|
|
suite_dependencies = []
|
|
|
|
self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
|
|
|
|
out_data_f, func_info, suite_dependencies)
|
2017-07-06 16:34:27 +00:00
|
|
|
|
|
|
|
def test_output(self):
|
|
|
|
"""
|
|
|
|
Test that intermediate data file is written with expected data.
|
2018-06-13 15:31:26 +00:00
|
|
|
:return:
|
2017-07-06 16:34:27 +00:00
|
|
|
"""
|
|
|
|
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', '')
|
2018-07-03 10:57:54 +00:00
|
|
|
func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
|
|
|
|
'test_func2': (1, ('char*', 'int', 'int'))}
|
|
|
|
suite_dependencies = []
|
|
|
|
dep_check_code, expression_code = \
|
|
|
|
gen_from_test_data(data_f, out_data_f, func_info,
|
|
|
|
suite_dependencies)
|
2017-07-06 16:34:27 +00:00
|
|
|
expected_dep_check_code = '''
|
2017-07-10 10:54:01 +00:00
|
|
|
case 0:
|
|
|
|
{
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(DEP1)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
2017-07-06 16:34:27 +00:00
|
|
|
#if defined(DEP2)
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#else
|
2017-07-10 10:54:01 +00:00
|
|
|
ret = DEPENDENCY_NOT_SUPPORTED;
|
2017-07-06 16:34:27 +00:00
|
|
|
#endif
|
2017-07-10 10:54:01 +00:00
|
|
|
}
|
|
|
|
break;'''
|
2018-07-03 10:57:54 +00:00
|
|
|
expected_data = '''My test 1
|
2017-07-06 16:34:27 +00:00
|
|
|
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 = '''
|
2017-07-10 10:54:01 +00:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
*out_value = MACRO1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
*out_value = MACRO2;
|
|
|
|
}
|
|
|
|
break;'''
|
2017-07-06 16:34:27 +00:00
|
|
|
self.assertEqual(dep_check_code, expected_dep_check_code)
|
2018-07-03 10:57:54 +00:00
|
|
|
self.assertEqual(out_data_f.getvalue(), expected_data)
|
2017-07-06 16:34:27 +00:00
|
|
|
self.assertEqual(expression_code, expected_expression_code)
|
2017-07-03 12:58:20 +00:00
|
|
|
|
|
|
|
|
2018-07-03 10:57:54 +00:00
|
|
|
if __name__ == '__main__':
|
2017-07-03 12:58:20 +00:00
|
|
|
unittest_main()
|