blob: 000c2a7010e60599190ddd3cc6ba7b68800c3817 [file] [log] [blame]
Azim Khanb31aa442018-07-03 11:57:54 +01001#!/usr/bin/env python3
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00002# Unit test for generate_test_code.py
Azim Khanf0e42fb2017-08-02 14:47:13 +01003#
Bence Szépkúti1e148272020-08-07 13:07:28 +02004# Copyright The Mbed TLS Contributors
Azim Khanf0e42fb2017-08-02 14:47:13 +01005# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
Azim Khan4b543232017-06-30 09:35:21 +010018
19"""
Mohammad Azim Khan78befd92018-03-06 11:49:41 +000020Unit tests for generate_test_code.py
Azim Khan4b543232017-06-30 09:35:21 +010021"""
22
Gilles Peskinee915d532019-02-25 21:39:42 +010023# pylint: disable=wrong-import-order
Mohammad Azim Khan539aa062018-07-06 00:29:50 +010024try:
25 # Python 2
26 from StringIO import StringIO
27except ImportError:
28 # Python 3
29 from io import StringIO
Azim Khanb31aa442018-07-03 11:57:54 +010030from unittest import TestCase, main as unittest_main
Mohammad Azim Khan539aa062018-07-06 00:29:50 +010031try:
32 # Python 2
33 from mock import patch
34except ImportError:
35 # Python 3
36 from unittest.mock import patch
Gilles Peskinee915d532019-02-25 21:39:42 +010037# pylint: enable=wrong-import-order
Azim Khanb31aa442018-07-03 11:57:54 +010038from generate_test_code import gen_dependencies, gen_dependencies_one_line
39from generate_test_code import gen_function_wrapper, gen_dispatch
40from generate_test_code import parse_until_pattern, GeneratorInputError
41from generate_test_code import parse_suite_dependencies
42from generate_test_code import parse_function_dependencies
Azim Khanfcdf6852018-07-05 17:31:46 +010043from generate_test_code import parse_function_arguments, parse_function_code
Azim Khanb31aa442018-07-03 11:57:54 +010044from generate_test_code import parse_functions, END_HEADER_REGEX
45from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
46from generate_test_code import parse_test_data, gen_dep_check
47from generate_test_code import gen_expression_check, write_dependencies
48from generate_test_code import write_parameters, gen_suite_dep_checks
49from generate_test_code import gen_from_test_data
50
51
Azim Khan4b543232017-06-30 09:35:21 +010052class GenDep(TestCase):
53 """
54 Test suite for function gen_dep()
55 """
56
Azim Khanb31aa442018-07-03 11:57:54 +010057 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010058 """
Azim Khanb31aa442018-07-03 11:57:54 +010059 Test that gen_dep() correctly creates dependencies for given
60 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010061 :return:
Azim Khan4b543232017-06-30 09:35:21 +010062 """
Azim Khanb31aa442018-07-03 11:57:54 +010063 dependencies = ['DEP1', 'DEP2']
64 dep_start, dep_end = gen_dependencies(dependencies)
65 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010066 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010067 self.assertEqual(preprocessor1, '#if defined(DEP1)',
68 'Preprocessor generated incorrectly')
69 self.assertEqual(preprocessor2, '#if defined(DEP2)',
70 'Preprocessor generated incorrectly')
71 self.assertEqual(endif1, '#endif /* DEP2 */',
72 'Preprocessor generated incorrectly')
73 self.assertEqual(endif2, '#endif /* DEP1 */',
74 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010075
Azim Khanb31aa442018-07-03 11:57:54 +010076 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010077 """
Azim Khanb31aa442018-07-03 11:57:54 +010078 Test that gen_dep() correctly creates dependencies for given
79 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010080 :return:
Azim Khan4b543232017-06-30 09:35:21 +010081 """
Azim Khanb31aa442018-07-03 11:57:54 +010082 dependencies = ['!DEP1', '!DEP2']
83 dep_start, dep_end = gen_dependencies(dependencies)
84 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010085 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010086 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
87 'Preprocessor generated incorrectly')
88 self.assertEqual(preprocessor2, '#if !defined(DEP2)',
89 'Preprocessor generated incorrectly')
90 self.assertEqual(endif1, '#endif /* !DEP2 */',
91 'Preprocessor generated incorrectly')
92 self.assertEqual(endif2, '#endif /* !DEP1 */',
93 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010094
Azim Khanb31aa442018-07-03 11:57:54 +010095 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010096 """
Azim Khanb31aa442018-07-03 11:57:54 +010097 Test that gen_dep() correctly creates dependencies for given
98 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010099 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100100 """
Azim Khanb31aa442018-07-03 11:57:54 +0100101 dependencies = ['!DEP1', 'DEP2']
102 dep_start, dep_end = gen_dependencies(dependencies)
103 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +0100104 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +0100105 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
106 'Preprocessor generated incorrectly')
107 self.assertEqual(preprocessor2, '#if defined(DEP2)',
108 'Preprocessor generated incorrectly')
109 self.assertEqual(endif1, '#endif /* DEP2 */',
110 'Preprocessor generated incorrectly')
111 self.assertEqual(endif2, '#endif /* !DEP1 */',
112 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100113
Azim Khanb31aa442018-07-03 11:57:54 +0100114 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100115 """
Azim Khanb31aa442018-07-03 11:57:54 +0100116 Test that gen_dep() correctly creates dependencies for given
117 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100118 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100119 """
Azim Khanb31aa442018-07-03 11:57:54 +0100120 dependencies = []
121 dep_start, dep_end = gen_dependencies(dependencies)
122 self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
123 self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100124
Azim Khanb31aa442018-07-03 11:57:54 +0100125 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100126 """
Azim Khanb31aa442018-07-03 11:57:54 +0100127 Test that gen_dep() correctly creates dependencies for given
128 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100129 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100130 """
Azim Khanb31aa442018-07-03 11:57:54 +0100131 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100132 count = 10
133 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100134 dependencies.append('DEP%d' % i)
135 dep_start, dep_end = gen_dependencies(dependencies)
136 self.assertEqual(len(dep_start.splitlines()), count,
137 'Preprocessor generated incorrectly')
138 self.assertEqual(len(dep_end.splitlines()), count,
139 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100140
141
142class GenDepOneLine(TestCase):
143 """
Azim Khanb31aa442018-07-03 11:57:54 +0100144 Test Suite for testing gen_dependencies_one_line()
Azim Khan4b543232017-06-30 09:35:21 +0100145 """
146
Azim Khanb31aa442018-07-03 11:57:54 +0100147 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100148 """
Azim Khanb31aa442018-07-03 11:57:54 +0100149 Test that gen_dep() correctly creates dependencies for given
150 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100151 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100152 """
Azim Khanb31aa442018-07-03 11:57:54 +0100153 dependencies = ['DEP1', 'DEP2']
154 dep_str = gen_dependencies_one_line(dependencies)
155 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
156 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100157
Azim Khanb31aa442018-07-03 11:57:54 +0100158 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100159 """
Azim Khanb31aa442018-07-03 11:57:54 +0100160 Test that gen_dep() correctly creates dependencies for given
161 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100162 :return:
163 """
Azim Khanb31aa442018-07-03 11:57:54 +0100164 dependencies = ['!DEP1', '!DEP2']
165 dep_str = gen_dependencies_one_line(dependencies)
166 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
167 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100168
Azim Khanb31aa442018-07-03 11:57:54 +0100169 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100170 """
Azim Khanb31aa442018-07-03 11:57:54 +0100171 Test that gen_dep() correctly creates dependencies for given
172 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100173 :return:
174 """
Azim Khanb31aa442018-07-03 11:57:54 +0100175 dependencies = ['!DEP1', 'DEP2']
176 dep_str = gen_dependencies_one_line(dependencies)
177 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
178 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100179
Azim Khanb31aa442018-07-03 11:57:54 +0100180 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100181 """
Azim Khanb31aa442018-07-03 11:57:54 +0100182 Test that gen_dep() correctly creates dependencies for given
183 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100184 :return:
185 """
Azim Khanb31aa442018-07-03 11:57:54 +0100186 dependencies = []
187 dep_str = gen_dependencies_one_line(dependencies)
188 self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100189
Azim Khanb31aa442018-07-03 11:57:54 +0100190 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100191 """
Azim Khanb31aa442018-07-03 11:57:54 +0100192 Test that gen_dep() correctly creates dependencies for given
193 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100194 :return:
195 """
Azim Khanb31aa442018-07-03 11:57:54 +0100196 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100197 count = 10
198 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100199 dependencies.append('DEP%d' % i)
200 dep_str = gen_dependencies_one_line(dependencies)
201 expected = '#if ' + ' && '.join(['defined(%s)' %
202 x for x in dependencies])
203 self.assertEqual(dep_str, expected,
204 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100205
206
207class GenFunctionWrapper(TestCase):
208 """
209 Test Suite for testing gen_function_wrapper()
210 """
211
212 def test_params_unpack(self):
213 """
214 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100215
216 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100217 """
218 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
219 expected = '''
220void test_a_wrapper( void ** params )
221{
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100222
Azim Khan4b543232017-06-30 09:35:21 +0100223 test_a( a, b, c, d );
224}
225'''
226 self.assertEqual(code, expected)
227
228 def test_local(self):
229 """
230 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100231
232 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100233 """
Azim Khanb31aa442018-07-03 11:57:54 +0100234 code = gen_function_wrapper('test_a',
235 'int x = 1;', ('x', 'b', 'c', 'd'))
Azim Khan4b543232017-06-30 09:35:21 +0100236 expected = '''
237void test_a_wrapper( void ** params )
238{
Azim Khan4b543232017-06-30 09:35:21 +0100239int x = 1;
240 test_a( x, b, c, d );
241}
242'''
243 self.assertEqual(code, expected)
244
245 def test_empty_params(self):
246 """
247 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100248
249 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100250 """
251 code = gen_function_wrapper('test_a', '', ())
252 expected = '''
253void test_a_wrapper( void ** params )
254{
255 (void)params;
256
257 test_a( );
258}
259'''
260 self.assertEqual(code, expected)
261
262
263class GenDispatch(TestCase):
264 """
265 Test suite for testing gen_dispatch()
266 """
267
268 def test_dispatch(self):
269 """
270 Test that dispatch table entry is generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100271 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100272 """
273 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
274 expected = '''
275#if defined(DEP1) && defined(DEP2)
276 test_a_wrapper,
277#else
278 NULL,
279#endif
280'''
281 self.assertEqual(code, expected)
282
Azim Khanb31aa442018-07-03 11:57:54 +0100283 def test_empty_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100284 """
285 Test empty dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100286 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100287 """
288 code = gen_dispatch('test_a', [])
289 expected = '''
290 test_a_wrapper,
291'''
292 self.assertEqual(code, expected)
293
294
Gilles Peskine184c0962020-03-24 18:25:17 +0100295class StringIOWrapper(StringIO):
Azim Khan4b543232017-06-30 09:35:21 +0100296 """
297 file like class to mock file object in tests.
298 """
Azim Khan4084ec72018-07-05 14:20:08 +0100299 def __init__(self, file_name, data, line_no=0):
Azim Khan4b543232017-06-30 09:35:21 +0100300 """
301 Init file handle.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100302
303 :param file_name:
Azim Khan4b543232017-06-30 09:35:21 +0100304 :param data:
305 :param line_no:
306 """
307 super(StringIOWrapper, self).__init__(data)
308 self.line_no = line_no
309 self.name = file_name
310
311 def next(self):
312 """
Azim Khanb31aa442018-07-03 11:57:54 +0100313 Iterator method. This method overrides base class's
314 next method and extends the next method to count the line
315 numbers as each line is read.
Azim Khan4b543232017-06-30 09:35:21 +0100316
Azim Khanb31aa442018-07-03 11:57:54 +0100317 :return: Line read from file.
318 """
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100319 parent = super(StringIOWrapper, self)
320 if getattr(parent, 'next', None):
321 # Python 2
322 line = parent.next()
323 else:
324 # Python 3
325 line = parent.__next__()
Azim Khan4084ec72018-07-05 14:20:08 +0100326 return line
Azim Khanb31aa442018-07-03 11:57:54 +0100327
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100328 # Python 3
Azim Khanb31aa442018-07-03 11:57:54 +0100329 __next__ = next
330
331 def readline(self, length=0):
Azim Khan4b543232017-06-30 09:35:21 +0100332 """
333 Wrap the base class readline.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100334
Azim Khanb31aa442018-07-03 11:57:54 +0100335 :param length:
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100336 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100337 """
Gilles Peskinee915d532019-02-25 21:39:42 +0100338 # pylint: disable=unused-argument
Azim Khan4b543232017-06-30 09:35:21 +0100339 line = super(StringIOWrapper, self).readline()
Azim Khan4084ec72018-07-05 14:20:08 +0100340 if line is not None:
Azim Khan4b543232017-06-30 09:35:21 +0100341 self.line_no += 1
342 return line
343
344
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000345class ParseUntilPattern(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100346 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000347 Test Suite for testing parse_until_pattern().
Azim Khan4b543232017-06-30 09:35:21 +0100348 """
349
350 def test_suite_headers(self):
351 """
352 Test that suite headers are parsed correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100353
354 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100355 """
356 data = '''#include "mbedtls/ecp.h"
357
358#define ECP_PF_UNKNOWN -1
359/* END_HEADER */
360'''
361 expected = '''#line 1 "test_suite_ut.function"
362#include "mbedtls/ecp.h"
363
364#define ECP_PF_UNKNOWN -1
365'''
Azim Khanb31aa442018-07-03 11:57:54 +0100366 stream = StringIOWrapper('test_suite_ut.function', data, line_no=0)
367 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100368 self.assertEqual(headers, expected)
369
370 def test_line_no(self):
371 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100372 Test that #line is set to correct line no. in source .function file.
373
374 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100375 """
376 data = '''#include "mbedtls/ecp.h"
377
378#define ECP_PF_UNKNOWN -1
379/* END_HEADER */
380'''
381 offset_line_no = 5
382 expected = '''#line %d "test_suite_ut.function"
383#include "mbedtls/ecp.h"
384
385#define ECP_PF_UNKNOWN -1
386''' % (offset_line_no + 1)
Azim Khanb31aa442018-07-03 11:57:54 +0100387 stream = StringIOWrapper('test_suite_ut.function', data,
388 offset_line_no)
389 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100390 self.assertEqual(headers, expected)
391
392 def test_no_end_header_comment(self):
393 """
Azim Khanb31aa442018-07-03 11:57:54 +0100394 Test that InvalidFileFormat is raised when end header comment is
395 missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100396 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100397 """
398 data = '''#include "mbedtls/ecp.h"
399
400#define ECP_PF_UNKNOWN -1
401
402'''
Azim Khanb31aa442018-07-03 11:57:54 +0100403 stream = StringIOWrapper('test_suite_ut.function', data)
404 self.assertRaises(GeneratorInputError, parse_until_pattern, stream,
405 END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100406
407
Azim Khanb31aa442018-07-03 11:57:54 +0100408class ParseSuiteDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100409 """
Azim Khanb31aa442018-07-03 11:57:54 +0100410 Test Suite for testing parse_suite_dependencies().
Azim Khan4b543232017-06-30 09:35:21 +0100411 """
412
Azim Khanb31aa442018-07-03 11:57:54 +0100413 def test_suite_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100414 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100415
416 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100417 """
418 data = '''
419 * depends_on:MBEDTLS_ECP_C
420 * END_DEPENDENCIES
421 */
422'''
423 expected = ['MBEDTLS_ECP_C']
Azim Khanb31aa442018-07-03 11:57:54 +0100424 stream = StringIOWrapper('test_suite_ut.function', data)
425 dependencies = parse_suite_dependencies(stream)
426 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100427
428 def test_no_end_dep_comment(self):
429 """
430 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100431 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100432 """
433 data = '''
434* depends_on:MBEDTLS_ECP_C
435'''
Azim Khanb31aa442018-07-03 11:57:54 +0100436 stream = StringIOWrapper('test_suite_ut.function', data)
437 self.assertRaises(GeneratorInputError, parse_suite_dependencies,
438 stream)
Azim Khan4b543232017-06-30 09:35:21 +0100439
Azim Khanb31aa442018-07-03 11:57:54 +0100440 def test_dependencies_split(self):
Azim Khan4b543232017-06-30 09:35:21 +0100441 """
442 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100443 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100444 """
445 data = '''
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100446 * depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
Azim Khan4b543232017-06-30 09:35:21 +0100447 * END_DEPENDENCIES
448 */
449'''
450 expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
Azim Khanb31aa442018-07-03 11:57:54 +0100451 stream = StringIOWrapper('test_suite_ut.function', data)
452 dependencies = parse_suite_dependencies(stream)
453 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100454
455
Azim Khanb31aa442018-07-03 11:57:54 +0100456class ParseFuncDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100457 """
Azim Khanb31aa442018-07-03 11:57:54 +0100458 Test Suite for testing parse_function_dependencies()
Azim Khan4b543232017-06-30 09:35:21 +0100459 """
460
Azim Khanb31aa442018-07-03 11:57:54 +0100461 def test_function_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100462 """
Azim Khanb31aa442018-07-03 11:57:54 +0100463 Test that parse_function_dependencies() correctly parses function
464 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100465 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100466 """
Azim Khanb31aa442018-07-03 11:57:54 +0100467 line = '/* BEGIN_CASE ' \
468 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
Azim Khan4b543232017-06-30 09:35:21 +0100469 expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
Azim Khanb31aa442018-07-03 11:57:54 +0100470 dependencies = parse_function_dependencies(line)
471 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100472
Azim Khanb31aa442018-07-03 11:57:54 +0100473 def test_no_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100474 """
Azim Khanb31aa442018-07-03 11:57:54 +0100475 Test that parse_function_dependencies() correctly parses function
476 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100477 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100478 """
479 line = '/* BEGIN_CASE */'
Azim Khanb31aa442018-07-03 11:57:54 +0100480 dependencies = parse_function_dependencies(line)
481 self.assertEqual(dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100482
Azim Khanb31aa442018-07-03 11:57:54 +0100483 def test_tolerance(self):
Azim Khan4b543232017-06-30 09:35:21 +0100484 """
Azim Khanb31aa442018-07-03 11:57:54 +0100485 Test that parse_function_dependencies() correctly parses function
486 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100487 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100488 """
489 line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
Azim Khanb31aa442018-07-03 11:57:54 +0100490 dependencies = parse_function_dependencies(line)
491 self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
Azim Khan4b543232017-06-30 09:35:21 +0100492
493
494class ParseFuncSignature(TestCase):
495 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100496 Test Suite for parse_function_arguments().
Azim Khan4b543232017-06-30 09:35:21 +0100497 """
498
499 def test_int_and_char_params(self):
500 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100501 Test int and char parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100502 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100503 """
504 line = 'void entropy_threshold( char * a, int b, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100505 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100506 self.assertEqual(args, ['char*', 'int', 'int'])
507 self.assertEqual(local, '')
Azim Khanb31aa442018-07-03 11:57:54 +0100508 self.assertEqual(arg_dispatch, ['(char *) params[0]',
509 '*( (int *) params[1] )',
510 '*( (int *) params[2] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100511
512 def test_hex_params(self):
513 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100514 Test hex parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100515 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100516 """
Azim Khan5fcca462018-06-29 11:05:32 +0100517 line = 'void entropy_threshold( char * a, data_t * h, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100518 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100519 self.assertEqual(args, ['char*', 'hex', 'int'])
Azim Khanb31aa442018-07-03 11:57:54 +0100520 self.assertEqual(local,
Azim Khan4084ec72018-07-05 14:20:08 +0100521 ' data_t data1 = {(uint8_t *) params[1], '
Azim Khanb31aa442018-07-03 11:57:54 +0100522 '*( (uint32_t *) params[2] )};\n')
523 self.assertEqual(arg_dispatch, ['(char *) params[0]',
Azim Khan4084ec72018-07-05 14:20:08 +0100524 '&data1',
Azim Khanb31aa442018-07-03 11:57:54 +0100525 '*( (int *) params[3] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100526
Azim Khan4b543232017-06-30 09:35:21 +0100527 def test_unsupported_arg(self):
528 """
Azim Khan5fcca462018-06-29 11:05:32 +0100529 Test unsupported arguments (not among int, char * and data_t)
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100530 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100531 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100532 line = 'void entropy_threshold( char * a, data_t * h, char result )'
533 self.assertRaises(ValueError, parse_function_arguments, line)
Azim Khan4b543232017-06-30 09:35:21 +0100534
535 def test_no_params(self):
536 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100537 Test no parameters.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100538 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100539 """
540 line = 'void entropy_threshold()'
Azim Khanfcdf6852018-07-05 17:31:46 +0100541 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100542 self.assertEqual(args, [])
543 self.assertEqual(local, '')
544 self.assertEqual(arg_dispatch, [])
545
546
547class ParseFunctionCode(TestCase):
548 """
549 Test suite for testing parse_function_code()
550 """
551
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100552 def assert_raises_regex(self, exp, regex, func, *args):
553 """
554 Python 2 & 3 portable wrapper of assertRaisesRegex(p)? function.
555
556 :param exp: Exception type expected to be raised by cb.
557 :param regex: Expected exception message
558 :param func: callable object under test
559 :param args: variable positional arguments
560 """
561 parent = super(ParseFunctionCode, self)
562
563 # Pylint does not appreciate that the super method called
564 # conditionally can be available in other Python version
565 # then that of Pylint.
566 # Workaround is to call the method via getattr.
567 # Pylint ignores that the method got via getattr is
568 # conditionally executed. Method has to be a callable.
569 # Hence, using a dummy callable for getattr default.
570 dummy = lambda *x: None
571 # First Python 3 assertRaisesRegex is checked, since Python 2
572 # assertRaisesRegexp is also available in Python 3 but is
573 # marked deprecated.
574 for name in ('assertRaisesRegex', 'assertRaisesRegexp'):
575 method = getattr(parent, name, dummy)
576 if method is not dummy:
577 method(exp, regex, func, *args)
578 break
579 else:
580 raise AttributeError(" 'ParseFunctionCode' object has no attribute"
581 " 'assertRaisesRegex' or 'assertRaisesRegexp'"
582 )
583
Azim Khan4b543232017-06-30 09:35:21 +0100584 def test_no_function(self):
585 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100586 Test no test function found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100587 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100588 """
589 data = '''
590No
591test
592function
593'''
Azim Khanb31aa442018-07-03 11:57:54 +0100594 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100595 err_msg = 'file: test_suite_ut.function - Test functions not found!'
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100596 self.assert_raises_regex(GeneratorInputError, err_msg,
597 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100598
599 def test_no_end_case_comment(self):
600 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100601 Test missing end case.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100602 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100603 """
604 data = '''
605void test_func()
606{
607}
608'''
Azim Khanb31aa442018-07-03 11:57:54 +0100609 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100610 err_msg = r'file: test_suite_ut.function - '\
611 'end case pattern .*? not found!'
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100612 self.assert_raises_regex(GeneratorInputError, err_msg,
613 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100614
Azim Khanfcdf6852018-07-05 17:31:46 +0100615 @patch("generate_test_code.parse_function_arguments")
Azim Khanb31aa442018-07-03 11:57:54 +0100616 def test_function_called(self,
Azim Khanfcdf6852018-07-05 17:31:46 +0100617 parse_function_arguments_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100618 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100619 Test parse_function_code()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100620 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100621 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100622 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100623 data = '''
624void test_func()
625{
626}
627'''
Azim Khanb31aa442018-07-03 11:57:54 +0100628 stream = StringIOWrapper('test_suite_ut.function', data)
629 self.assertRaises(GeneratorInputError, parse_function_code,
630 stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100631 self.assertTrue(parse_function_arguments_mock.called)
632 parse_function_arguments_mock.assert_called_with('void test_func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100633
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000634 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100635 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000636 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100637 @patch("generate_test_code.parse_function_arguments")
638 def test_return(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100639 gen_function_wrapper_mock,
640 gen_dependencies_mock,
641 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100642 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100643 Test generated code.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100644 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100645 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100646 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100647 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100648 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100649 gen_dispatch_mock.side_effect = gen_dispatch
650 data = '''
651void func()
652{
653 ba ba black sheep
654 have you any wool
655}
656/* END_CASE */
657'''
Azim Khanb31aa442018-07-03 11:57:54 +0100658 stream = StringIOWrapper('test_suite_ut.function', data)
659 name, arg, code, dispatch_code = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100660
Azim Khanfcdf6852018-07-05 17:31:46 +0100661 self.assertTrue(parse_function_arguments_mock.called)
662 parse_function_arguments_mock.assert_called_with('void func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100663 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
664 self.assertEqual(name, 'test_func')
665 self.assertEqual(arg, [])
Azim Khan4084ec72018-07-05 14:20:08 +0100666 expected = '''#line 1 "test_suite_ut.function"
667
Azim Khan4b543232017-06-30 09:35:21 +0100668void test_func()
669{
670 ba ba black sheep
671 have you any wool
672exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100673 ;
Azim Khan4b543232017-06-30 09:35:21 +0100674}
675'''
676 self.assertEqual(code, expected)
677 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
678
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000679 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100680 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000681 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100682 @patch("generate_test_code.parse_function_arguments")
683 def test_with_exit_label(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100684 gen_function_wrapper_mock,
685 gen_dependencies_mock,
686 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100687 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100688 Test when exit label is present.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100689 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100690 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100691 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100692 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100693 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100694 gen_dispatch_mock.side_effect = gen_dispatch
695 data = '''
696void func()
697{
698 ba ba black sheep
699 have you any wool
700exit:
701 yes sir yes sir
702 3 bags full
703}
704/* END_CASE */
705'''
Azim Khanb31aa442018-07-03 11:57:54 +0100706 stream = StringIOWrapper('test_suite_ut.function', data)
707 _, _, code, _ = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100708
Azim Khan4084ec72018-07-05 14:20:08 +0100709 expected = '''#line 1 "test_suite_ut.function"
710
Azim Khan4b543232017-06-30 09:35:21 +0100711void test_func()
712{
713 ba ba black sheep
714 have you any wool
715exit:
716 yes sir yes sir
717 3 bags full
718}
719'''
720 self.assertEqual(code, expected)
721
Azim Khanfcdf6852018-07-05 17:31:46 +0100722 def test_non_void_function(self):
723 """
724 Test invalid signature (non void).
725 :return:
726 """
727 data = 'int entropy_threshold( char * a, data_t * h, int result )'
728 err_msg = 'file: test_suite_ut.function - Test functions not found!'
729 stream = StringIOWrapper('test_suite_ut.function', data)
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100730 self.assert_raises_regex(GeneratorInputError, err_msg,
731 parse_function_code, stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100732
733 @patch("generate_test_code.gen_dispatch")
734 @patch("generate_test_code.gen_dependencies")
735 @patch("generate_test_code.gen_function_wrapper")
736 @patch("generate_test_code.parse_function_arguments")
737 def test_functio_name_on_newline(self, parse_function_arguments_mock,
738 gen_function_wrapper_mock,
739 gen_dependencies_mock,
740 gen_dispatch_mock):
741 """
742 Test when exit label is present.
743 :return:
744 """
745 parse_function_arguments_mock.return_value = ([], '', [])
746 gen_function_wrapper_mock.return_value = ''
747 gen_dependencies_mock.side_effect = gen_dependencies
748 gen_dispatch_mock.side_effect = gen_dispatch
749 data = '''
750void
751
752
753func()
754{
755 ba ba black sheep
756 have you any wool
757exit:
758 yes sir yes sir
759 3 bags full
760}
761/* END_CASE */
762'''
763 stream = StringIOWrapper('test_suite_ut.function', data)
764 _, _, code, _ = parse_function_code(stream, [], [])
765
766 expected = '''#line 1 "test_suite_ut.function"
767
768void
769
770
771test_func()
772{
773 ba ba black sheep
774 have you any wool
775exit:
776 yes sir yes sir
777 3 bags full
778}
779'''
780 self.assertEqual(code, expected)
781
Azim Khan4b543232017-06-30 09:35:21 +0100782
783class ParseFunction(TestCase):
784 """
785 Test Suite for testing parse_functions()
786 """
787
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000788 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000789 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100790 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000791 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100792 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100793 """
Azim Khanb31aa442018-07-03 11:57:54 +0100794 def stop(*_unused):
795 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100796 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000797 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100798 data = '''/* BEGIN_HEADER */
799#include "mbedtls/ecp.h"
800
801#define ECP_PF_UNKNOWN -1
802/* END_HEADER */
803'''
Azim Khanb31aa442018-07-03 11:57:54 +0100804 stream = StringIOWrapper('test_suite_ut.function', data)
805 self.assertRaises(Exception, parse_functions, stream)
806 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100807 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000808
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000809 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000810 def test_begin_helper(self, parse_until_pattern_mock):
811 """
812 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100813 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000814 """
Azim Khanb31aa442018-07-03 11:57:54 +0100815 def stop(*_unused):
816 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000817 raise Exception
818 parse_until_pattern_mock.side_effect = stop
819 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100820void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000821{
Azim Khanb31aa442018-07-03 11:57:54 +0100822 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000823}
824/* END_SUITE_HELPERS */
825'''
Azim Khanb31aa442018-07-03 11:57:54 +0100826 stream = StringIOWrapper('test_suite_ut.function', data)
827 self.assertRaises(Exception, parse_functions, stream)
828 parse_until_pattern_mock.assert_called_with(stream,
829 END_SUITE_HELPERS_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100830 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100831
Azim Khanb31aa442018-07-03 11:57:54 +0100832 @patch("generate_test_code.parse_suite_dependencies")
833 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100834 """
Azim Khanb31aa442018-07-03 11:57:54 +0100835 Test that begin dep is checked and parse_suite_dependencies() is
836 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100837 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100838 """
Azim Khanb31aa442018-07-03 11:57:54 +0100839 def stop(*_unused):
840 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100841 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100842 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100843 data = '''/* BEGIN_DEPENDENCIES
844 * depends_on:MBEDTLS_ECP_C
845 * END_DEPENDENCIES
846 */
847'''
Azim Khanb31aa442018-07-03 11:57:54 +0100848 stream = StringIOWrapper('test_suite_ut.function', data)
849 self.assertRaises(Exception, parse_functions, stream)
850 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +0100851 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100852
Azim Khanb31aa442018-07-03 11:57:54 +0100853 @patch("generate_test_code.parse_function_dependencies")
854 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100855 """
Azim Khanb31aa442018-07-03 11:57:54 +0100856 Test that begin dep is checked and parse_function_dependencies() is
857 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100858 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100859 """
Azim Khanb31aa442018-07-03 11:57:54 +0100860 def stop(*_unused):
861 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100862 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100863 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100864
Azim Khanb31aa442018-07-03 11:57:54 +0100865 dependencies_str = '/* BEGIN_CASE ' \
866 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100867 data = '''%svoid test_func()
868{
869}
Azim Khanb31aa442018-07-03 11:57:54 +0100870''' % dependencies_str
871 stream = StringIOWrapper('test_suite_ut.function', data)
872 self.assertRaises(Exception, parse_functions, stream)
873 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +0100874 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100875
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000876 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +0100877 @patch("generate_test_code.parse_function_dependencies")
878 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +0100879 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000880 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100881 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100882 """
Azim Khanb31aa442018-07-03 11:57:54 +0100883 func_mock1.return_value = []
884 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +0100885{
886}
887'''
888 func_dispatch = '''
889 test_func_wrapper,
890'''
Azim Khanb31aa442018-07-03 11:57:54 +0100891 func_mock2.return_value = 'test_func', [],\
892 in_func_code, func_dispatch
893 dependencies_str = '/* BEGIN_CASE ' \
894 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100895 data = '''%svoid test_func()
896{
897}
Azim Khanb31aa442018-07-03 11:57:54 +0100898''' % dependencies_str
899 stream = StringIOWrapper('test_suite_ut.function', data)
900 suite_dependencies, dispatch_code, func_code, func_info = \
901 parse_functions(stream)
902 func_mock1.assert_called_with(dependencies_str)
903 func_mock2.assert_called_with(stream, [], [])
904 self.assertEqual(stream.line_no, 5)
905 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100906 expected_dispatch_code = '''/* Function Id: 0 */
907
908 test_func_wrapper,
909'''
910 self.assertEqual(dispatch_code, expected_dispatch_code)
911 self.assertEqual(func_code, in_func_code)
912 self.assertEqual(func_info, {'test_func': (0, [])})
913
914 def test_parsing(self):
915 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000916 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100917 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100918 """
919 data = '''/* BEGIN_HEADER */
920#include "mbedtls/ecp.h"
921
922#define ECP_PF_UNKNOWN -1
923/* END_HEADER */
924
925/* BEGIN_DEPENDENCIES
926 * depends_on:MBEDTLS_ECP_C
927 * END_DEPENDENCIES
928 */
929
930/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
931void func1()
932{
933}
934/* END_CASE */
935
936/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
937void func2()
938{
939}
940/* END_CASE */
941'''
Azim Khanb31aa442018-07-03 11:57:54 +0100942 stream = StringIOWrapper('test_suite_ut.function', data)
943 suite_dependencies, dispatch_code, func_code, func_info = \
944 parse_functions(stream)
945 self.assertEqual(stream.line_no, 23)
946 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +0100947
948 expected_dispatch_code = '''/* Function Id: 0 */
949
950#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
951 test_func1_wrapper,
952#else
953 NULL,
954#endif
955/* Function Id: 1 */
956
957#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
958 test_func2_wrapper,
959#else
960 NULL,
961#endif
962'''
963 self.assertEqual(dispatch_code, expected_dispatch_code)
964 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +0100965#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100966#include "mbedtls/ecp.h"
967
968#define ECP_PF_UNKNOWN -1
969#if defined(MBEDTLS_ENTROPY_NV_SEED)
970#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +0100971#line 13 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100972void test_func1()
973{
974exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100975 ;
Azim Khan4b543232017-06-30 09:35:21 +0100976}
977
978void test_func1_wrapper( void ** params )
979{
980 (void)params;
981
982 test_func1( );
983}
984#endif /* MBEDTLS_FS_IO */
985#endif /* MBEDTLS_ENTROPY_NV_SEED */
986#if defined(MBEDTLS_ENTROPY_NV_SEED)
987#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +0100988#line 19 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100989void test_func2()
990{
991exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100992 ;
Azim Khan4b543232017-06-30 09:35:21 +0100993}
994
995void test_func2_wrapper( void ** params )
996{
997 (void)params;
998
999 test_func2( );
1000}
1001#endif /* MBEDTLS_FS_IO */
1002#endif /* MBEDTLS_ENTROPY_NV_SEED */
1003#endif /* MBEDTLS_ECP_C */
1004'''
1005 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001006 self.assertEqual(func_info, {'test_func1': (0, []),
1007 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +01001008
1009 def test_same_function_name(self):
1010 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001011 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001012 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001013 """
1014 data = '''/* BEGIN_HEADER */
1015#include "mbedtls/ecp.h"
1016
1017#define ECP_PF_UNKNOWN -1
1018/* END_HEADER */
1019
1020/* BEGIN_DEPENDENCIES
1021 * depends_on:MBEDTLS_ECP_C
1022 * END_DEPENDENCIES
1023 */
1024
1025/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1026void func()
1027{
1028}
1029/* END_CASE */
1030
1031/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1032void func()
1033{
1034}
1035/* END_CASE */
1036'''
Azim Khanb31aa442018-07-03 11:57:54 +01001037 stream = StringIOWrapper('test_suite_ut.function', data)
1038 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +01001039
1040
Azim Khanb31aa442018-07-03 11:57:54 +01001041class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001042 """
Azim Khan599cd242017-07-06 17:34:27 +01001043 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +01001044 Note: Since escaped_split() output is used to write back to the
1045 intermediate data file. Any escape characters in the input are
1046 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001047 """
1048
1049 def test_invalid_input(self):
1050 """
1051 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001052 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001053 """
1054 self.assertRaises(ValueError, escaped_split, '', 'string')
1055
1056 def test_empty_string(self):
1057 """
Azim Khanb31aa442018-07-03 11:57:54 +01001058 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001059 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001060 """
1061 splits = escaped_split('', ':')
1062 self.assertEqual(splits, [])
1063
1064 def test_no_escape(self):
1065 """
Azim Khanb31aa442018-07-03 11:57:54 +01001066 Test with no escape character. The behaviour should be same as
1067 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001068 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001069 """
Azim Khanb31aa442018-07-03 11:57:54 +01001070 test_str = 'yahoo:google'
1071 splits = escaped_split(test_str, ':')
1072 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001073
1074 def test_escaped_input(self):
1075 """
Azim Khanb31aa442018-07-03 11:57:54 +01001076 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001077 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001078 """
Azim Khanb31aa442018-07-03 11:57:54 +01001079 test_str = r'yahoo\:google:facebook'
1080 splits = escaped_split(test_str, ':')
1081 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001082
1083 def test_escaped_escape(self):
1084 """
Azim Khanb31aa442018-07-03 11:57:54 +01001085 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001086 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001087 """
Azim Khan4084ec72018-07-05 14:20:08 +01001088 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +01001089 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001090 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001091
1092 def test_all_at_once(self):
1093 """
Azim Khanb31aa442018-07-03 11:57:54 +01001094 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001095 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001096 """
Azim Khan4084ec72018-07-05 14:20:08 +01001097 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +01001098 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001099 self.assertEqual(splits, [r'yahoo\\', r'google',
1100 r'facebook\:instagram\\',
1101 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001102
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001103
1104class ParseTestData(TestCase):
1105 """
1106 Test suite for parse test data.
1107 """
1108
1109 def test_parser(self):
1110 """
1111 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001112 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001113 """
1114 data = """
1115Diffie-Hellman full exchange #1
1116dhm_do_dhm:10:"23":10:"5"
1117
1118Diffie-Hellman full exchange #2
1119dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1120
1121Diffie-Hellman full exchange #3
1122dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1123
1124Diffie-Hellman selftest
1125dhm_selftest:
1126"""
Azim Khanb31aa442018-07-03 11:57:54 +01001127 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001128 # List of (name, function_name, dependencies, args)
1129 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001130 test1, test2, test3, test4 = tests
1131 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1132 self.assertEqual(test1[1], 'dhm_do_dhm')
1133 self.assertEqual(test1[2], [])
1134 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001135
Azim Khanb31aa442018-07-03 11:57:54 +01001136 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1137 self.assertEqual(test2[1], 'dhm_do_dhm')
1138 self.assertEqual(test2[2], [])
1139 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1140 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001141
Azim Khanb31aa442018-07-03 11:57:54 +01001142 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1143 self.assertEqual(test3[1], 'dhm_do_dhm')
1144 self.assertEqual(test3[2], [])
1145 self.assertEqual(test3[3], ['10',
1146 '"9345098382739712938719287391879381271"',
1147 '10',
1148 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001149
Azim Khanb31aa442018-07-03 11:57:54 +01001150 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1151 self.assertEqual(test4[1], 'dhm_selftest')
1152 self.assertEqual(test4[2], [])
1153 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001154
1155 def test_with_dependencies(self):
1156 """
1157 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001158 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001159 """
1160 data = """
1161Diffie-Hellman full exchange #1
1162depends_on:YAHOO
1163dhm_do_dhm:10:"23":10:"5"
1164
1165Diffie-Hellman full exchange #2
1166dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1167
1168"""
Azim Khanb31aa442018-07-03 11:57:54 +01001169 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001170 # List of (name, function_name, dependencies, args)
1171 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001172 test1, test2 = tests
1173 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1174 self.assertEqual(test1[1], 'dhm_do_dhm')
1175 self.assertEqual(test1[2], ['YAHOO'])
1176 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001177
Azim Khanb31aa442018-07-03 11:57:54 +01001178 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1179 self.assertEqual(test2[1], 'dhm_do_dhm')
1180 self.assertEqual(test2[2], [])
1181 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1182 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001183
1184 def test_no_args(self):
1185 """
Azim Khanb31aa442018-07-03 11:57:54 +01001186 Test GeneratorInputError is raised when test function name and
1187 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001188 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001189 """
1190 data = """
1191Diffie-Hellman full exchange #1
1192depends_on:YAHOO
1193
1194
1195Diffie-Hellman full exchange #2
1196dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1197
1198"""
Azim Khanb31aa442018-07-03 11:57:54 +01001199 stream = StringIOWrapper('test_suite_ut.function', data)
1200 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001201 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001202 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001203 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001204 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001205 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001206
1207 def test_incomplete_data(self):
1208 """
Azim Khanb31aa442018-07-03 11:57:54 +01001209 Test GeneratorInputError is raised when test function name
1210 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001211 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001212 """
1213 data = """
1214Diffie-Hellman full exchange #1
1215depends_on:YAHOO
1216"""
Azim Khanb31aa442018-07-03 11:57:54 +01001217 stream = StringIOWrapper('test_suite_ut.function', data)
1218 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001219 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001220 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001221 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001222 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001223 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001224
1225
1226class GenDepCheck(TestCase):
1227 """
Azim Khanb31aa442018-07-03 11:57:54 +01001228 Test suite for gen_dep_check(). It is assumed this function is
1229 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001230 """
1231
1232 def test_gen_dep_check(self):
1233 """
1234 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001235 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001236 """
1237 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001238 case 5:
1239 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001240#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001241 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001242#else
Azim Khand61b8372017-07-10 11:54:01 +01001243 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001244#endif
Azim Khand61b8372017-07-10 11:54:01 +01001245 }
1246 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001247 out = gen_dep_check(5, 'YAHOO')
1248 self.assertEqual(out, expected)
1249
Azim Khanb31aa442018-07-03 11:57:54 +01001250 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001251 """
1252 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001253 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001254 """
1255 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001256 case 5:
1257 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001258#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001259 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001260#else
Azim Khand61b8372017-07-10 11:54:01 +01001261 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001262#endif
Azim Khand61b8372017-07-10 11:54:01 +01001263 }
1264 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001265 out = gen_dep_check(5, '!YAHOO')
1266 self.assertEqual(out, expected)
1267
1268 def test_empty_dependency(self):
1269 """
1270 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001271 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001272 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001273 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001274
1275 def test_negative_dep_id(self):
1276 """
1277 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001278 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001279 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001280 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001281
1282
1283class GenExpCheck(TestCase):
1284 """
Azim Khanb31aa442018-07-03 11:57:54 +01001285 Test suite for gen_expression_check(). It is assumed this function
1286 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001287 """
1288
1289 def test_gen_exp_check(self):
1290 """
1291 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001292 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001293 """
1294 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001295 case 5:
1296 {
1297 *out_value = YAHOO;
1298 }
1299 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001300 out = gen_expression_check(5, 'YAHOO')
1301 self.assertEqual(out, expected)
1302
1303 def test_invalid_expression(self):
1304 """
1305 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001306 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001307 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001308 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001309
1310 def test_negative_exp_id(self):
1311 """
1312 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001313 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001314 """
Azim Khanb31aa442018-07-03 11:57:54 +01001315 self.assertRaises(GeneratorInputError, gen_expression_check,
1316 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001317
1318
Azim Khanb31aa442018-07-03 11:57:54 +01001319class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001320 """
Azim Khanb31aa442018-07-03 11:57:54 +01001321 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001322 """
1323
Azim Khanb31aa442018-07-03 11:57:54 +01001324 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001325 """
Azim Khanb31aa442018-07-03 11:57:54 +01001326 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001327 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001328 """
Azim Khanb31aa442018-07-03 11:57:54 +01001329 stream = StringIOWrapper('test_suite_ut.data', '')
1330 unique_dependencies = []
1331 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001332 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001333 self.assertEqual(len(unique_dependencies), 0)
1334 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001335
1336 def test_unique_dep_ids(self):
1337 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001338
1339 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001340 """
Azim Khanb31aa442018-07-03 11:57:54 +01001341 stream = StringIOWrapper('test_suite_ut.data', '')
1342 unique_dependencies = []
1343 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1344 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001345 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001346 case 0:
1347 {
Azim Khan599cd242017-07-06 17:34:27 +01001348#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001349 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001350#else
Azim Khand61b8372017-07-10 11:54:01 +01001351 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001352#endif
Azim Khand61b8372017-07-10 11:54:01 +01001353 }
1354 break;
1355 case 1:
1356 {
Azim Khan599cd242017-07-06 17:34:27 +01001357#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001358 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001359#else
Azim Khand61b8372017-07-10 11:54:01 +01001360 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001361#endif
Azim Khand61b8372017-07-10 11:54:01 +01001362 }
1363 break;
1364 case 2:
1365 {
Azim Khan599cd242017-07-06 17:34:27 +01001366#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001367 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001368#else
Azim Khand61b8372017-07-10 11:54:01 +01001369 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001370#endif
Azim Khand61b8372017-07-10 11:54:01 +01001371 }
1372 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001373 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001374 self.assertEqual(len(unique_dependencies), 3)
1375 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001376
1377 def test_dep_id_repeat(self):
1378 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001379
1380 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001381 """
Azim Khanb31aa442018-07-03 11:57:54 +01001382 stream = StringIOWrapper('test_suite_ut.data', '')
1383 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001384 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001385 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1386 unique_dependencies)
1387 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1388 unique_dependencies)
1389 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1390 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001391 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001392 case 0:
1393 {
Azim Khan599cd242017-07-06 17:34:27 +01001394#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001395 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001396#else
Azim Khand61b8372017-07-10 11:54:01 +01001397 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001398#endif
Azim Khand61b8372017-07-10 11:54:01 +01001399 }
1400 break;
1401 case 1:
1402 {
Azim Khan599cd242017-07-06 17:34:27 +01001403#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001404 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001405#else
Azim Khand61b8372017-07-10 11:54:01 +01001406 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001407#endif
Azim Khand61b8372017-07-10 11:54:01 +01001408 }
1409 break;
1410 case 2:
1411 {
Azim Khan599cd242017-07-06 17:34:27 +01001412#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001413 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001414#else
Azim Khand61b8372017-07-10 11:54:01 +01001415 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001416#endif
Azim Khand61b8372017-07-10 11:54:01 +01001417 }
1418 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001419 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001420 self.assertEqual(len(unique_dependencies), 3)
1421 self.assertEqual(stream.getvalue(),
1422 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001423
1424
1425class WriteParams(TestCase):
1426 """
1427 Test Suite for testing write_parameters().
1428 """
1429
1430 def test_no_params(self):
1431 """
1432 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001433 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001434 """
Azim Khanb31aa442018-07-03 11:57:54 +01001435 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001436 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001437 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001438 self.assertEqual(len(unique_expressions), 0)
1439 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001440 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001441
1442 def test_no_exp_param(self):
1443 """
1444 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001445 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001446 """
Azim Khanb31aa442018-07-03 11:57:54 +01001447 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001448 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001449 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1450 '0'],
1451 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001452 unique_expressions)
1453 self.assertEqual(len(unique_expressions), 0)
1454 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001455 self.assertEqual(stream.getvalue(),
1456 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001457
1458 def test_hex_format_int_param(self):
1459 """
1460 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001461 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001462 """
Azim Khanb31aa442018-07-03 11:57:54 +01001463 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001464 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001465 expression_code = write_parameters(stream,
1466 ['"Yahoo"', '"abcdef00"', '0xAA'],
1467 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001468 unique_expressions)
1469 self.assertEqual(len(unique_expressions), 0)
1470 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001471 self.assertEqual(stream.getvalue(),
1472 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001473
1474 def test_with_exp_param(self):
1475 """
1476 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001477 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001478 """
Azim Khanb31aa442018-07-03 11:57:54 +01001479 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001480 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001481 expression_code = write_parameters(stream,
1482 ['"Yahoo"', '"abcdef00"', '0',
1483 'MACRO1', 'MACRO2', 'MACRO3'],
1484 ['char*', 'hex', 'int',
1485 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001486 unique_expressions)
1487 self.assertEqual(len(unique_expressions), 3)
1488 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1489 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001490 case 0:
1491 {
1492 *out_value = MACRO1;
1493 }
1494 break;
1495 case 1:
1496 {
1497 *out_value = MACRO2;
1498 }
1499 break;
1500 case 2:
1501 {
1502 *out_value = MACRO3;
1503 }
1504 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001505 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001506 self.assertEqual(stream.getvalue(),
1507 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1508 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001509
Azim Khanb31aa442018-07-03 11:57:54 +01001510 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001511 """
1512 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001513 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001514 """
Azim Khanb31aa442018-07-03 11:57:54 +01001515 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001516 unique_expressions = []
1517 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001518 expression_code += write_parameters(stream,
1519 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1520 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001521 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001522 expression_code += write_parameters(stream,
1523 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1524 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001525 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001526 expression_code += write_parameters(stream,
1527 ['0', 'MACRO3', 'MACRO1'],
1528 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001529 unique_expressions)
1530 self.assertEqual(len(unique_expressions), 3)
1531 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1532 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001533 case 0:
1534 {
1535 *out_value = MACRO1;
1536 }
1537 break;
1538 case 1:
1539 {
1540 *out_value = MACRO2;
1541 }
1542 break;
1543 case 2:
1544 {
1545 *out_value = MACRO3;
1546 }
1547 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001548 self.assertEqual(expression_code, expected_expression_code)
1549 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1550:hex:"abcdef00":exp:1:exp:2
1551:int:0:exp:2:exp:0
1552'''
Azim Khanb31aa442018-07-03 11:57:54 +01001553 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001554
1555
Azim Khanb31aa442018-07-03 11:57:54 +01001556class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001557 """
Azim Khanb31aa442018-07-03 11:57:54 +01001558 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001559 """
Azim Khanb31aa442018-07-03 11:57:54 +01001560 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001561 """
Azim Khanb31aa442018-07-03 11:57:54 +01001562 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001563
1564 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001565 """
Azim Khanb31aa442018-07-03 11:57:54 +01001566 dep_check_code, expression_code = \
1567 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001568 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1569 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1570
Azim Khanb31aa442018-07-03 11:57:54 +01001571 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001572 """
Azim Khanb31aa442018-07-03 11:57:54 +01001573 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001574
1575 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001576 """
Azim Khanb31aa442018-07-03 11:57:54 +01001577 dep_check_code, expression_code = \
1578 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1579 'EXPRESSION_CODE')
1580 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001581#if defined(SUITE_DEP)
1582DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001583#endif
1584'''
1585 expected_expression_code = '''
1586#if defined(SUITE_DEP)
1587EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001588#endif
1589'''
Azim Khanb31aa442018-07-03 11:57:54 +01001590 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001591 self.assertEqual(expression_code, expected_expression_code)
1592
1593 def test_no_dep_no_exp(self):
1594 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001595 Test when there are no dependency and expression code.
1596 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001597 """
Azim Khanb31aa442018-07-03 11:57:54 +01001598 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001599 self.assertEqual(dep_check_code, '')
1600 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001601
1602
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001603class GenFromTestData(TestCase):
1604 """
1605 Test suite for gen_from_test_data()
1606 """
1607
Azim Khanb31aa442018-07-03 11:57:54 +01001608 @staticmethod
1609 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001610 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001611 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001612 def test_intermediate_data_file(func_mock1,
1613 write_parameters_mock,
1614 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001615 """
1616 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001617 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001618 """
1619 data = '''
1620My test
1621depends_on:DEP1
1622func1:0
1623'''
1624 data_f = StringIOWrapper('test_suite_ut.data', data)
1625 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1626 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001627 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001628 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001629 write_dependencies_mock.side_effect = write_dependencies
1630 func_mock1.side_effect = gen_suite_dep_checks
1631 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1632 write_dependencies_mock.assert_called_with(out_data_f,
1633 ['DEP1'], ['DEP1'])
1634 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1635 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001636 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001637 case 0:
1638 {
Azim Khan599cd242017-07-06 17:34:27 +01001639#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001640 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001641#else
Azim Khand61b8372017-07-10 11:54:01 +01001642 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001643#endif
Azim Khand61b8372017-07-10 11:54:01 +01001644 }
1645 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001646 func_mock1.assert_called_with(
1647 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001648
1649 def test_function_not_found(self):
1650 """
1651 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001652 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001653 """
1654 data = '''
1655My test
1656depends_on:DEP1
1657func1:0
1658'''
1659 data_f = StringIOWrapper('test_suite_ut.data', data)
1660 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1661 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001662 suite_dependencies = []
1663 self.assertRaises(GeneratorInputError, gen_from_test_data,
1664 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001665
1666 def test_different_func_args(self):
1667 """
Azim Khanb31aa442018-07-03 11:57:54 +01001668 Test that AssertError is raised when no. of parameters and
1669 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001670 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001671 """
1672 data = '''
1673My test
1674depends_on:DEP1
1675func1:0
1676'''
1677 data_f = StringIOWrapper('test_suite_ut.data', data)
1678 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001679 func_info = {'test_func2': (1, ('int', 'hex'))}
1680 suite_dependencies = []
1681 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1682 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001683
1684 def test_output(self):
1685 """
1686 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001687 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001688 """
1689 data = '''
1690My test 1
1691depends_on:DEP1
1692func1:0:0xfa:MACRO1:MACRO2
1693
1694My test 2
1695depends_on:DEP1:DEP2
1696func2:"yahoo":88:MACRO1
1697'''
1698 data_f = StringIOWrapper('test_suite_ut.data', data)
1699 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001700 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1701 'test_func2': (1, ('char*', 'int', 'int'))}
1702 suite_dependencies = []
1703 dep_check_code, expression_code = \
1704 gen_from_test_data(data_f, out_data_f, func_info,
1705 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001706 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001707 case 0:
1708 {
Azim Khan599cd242017-07-06 17:34:27 +01001709#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001710 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001711#else
Azim Khand61b8372017-07-10 11:54:01 +01001712 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001713#endif
Azim Khand61b8372017-07-10 11:54:01 +01001714 }
1715 break;
1716 case 1:
1717 {
Azim Khan599cd242017-07-06 17:34:27 +01001718#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001719 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001720#else
Azim Khand61b8372017-07-10 11:54:01 +01001721 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001722#endif
Azim Khand61b8372017-07-10 11:54:01 +01001723 }
1724 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001725 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001726depends_on:0
17270:int:0:int:0xfa:exp:0:exp:1
1728
1729My test 2
1730depends_on:0:1
17311:char*:"yahoo":int:88:exp:0
1732
1733'''
1734 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001735 case 0:
1736 {
1737 *out_value = MACRO1;
1738 }
1739 break;
1740 case 1:
1741 {
1742 *out_value = MACRO2;
1743 }
1744 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001745 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001746 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001747 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001748
1749
Azim Khanb31aa442018-07-03 11:57:54 +01001750if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001751 unittest_main()