blob: 2ef12e18dc4087214a520b20382f6ea272897ac9 [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#
Azim Khan4084ec72018-07-05 14:20:08 +01004# Copyright (C) 2018, Arm Limited, All Rights Reserved
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.
18#
Azim Khanb31aa442018-07-03 11:57:54 +010019# This file is part of Mbed TLS (https://tls.mbed.org)
Azim Khan4b543232017-06-30 09:35:21 +010020
21"""
Mohammad Azim Khan78befd92018-03-06 11:49:41 +000022Unit tests for generate_test_code.py
Azim Khan4b543232017-06-30 09:35:21 +010023"""
24
25
Mohammad Azim Khan539aa062018-07-06 00:29:50 +010026try:
27 # Python 2
28 from StringIO import StringIO
29except ImportError:
30 # Python 3
31 from io import StringIO
Azim Khanb31aa442018-07-03 11:57:54 +010032from unittest import TestCase, main as unittest_main
Mohammad Azim Khan539aa062018-07-06 00:29:50 +010033try:
34 # Python 2
35 from mock import patch
36except ImportError:
37 # Python 3
38 from unittest.mock import patch
Azim Khanb31aa442018-07-03 11:57:54 +010039from generate_test_code import gen_dependencies, gen_dependencies_one_line
40from generate_test_code import gen_function_wrapper, gen_dispatch
41from generate_test_code import parse_until_pattern, GeneratorInputError
42from generate_test_code import parse_suite_dependencies
43from generate_test_code import parse_function_dependencies
Azim Khanfcdf6852018-07-05 17:31:46 +010044from generate_test_code import parse_function_arguments, parse_function_code
Azim Khanb31aa442018-07-03 11:57:54 +010045from generate_test_code import parse_functions, END_HEADER_REGEX
46from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
47from generate_test_code import parse_test_data, gen_dep_check
48from generate_test_code import gen_expression_check, write_dependencies
49from generate_test_code import write_parameters, gen_suite_dep_checks
50from generate_test_code import gen_from_test_data
51
52
Azim Khan4b543232017-06-30 09:35:21 +010053class GenDep(TestCase):
54 """
55 Test suite for function gen_dep()
56 """
57
Azim Khanb31aa442018-07-03 11:57:54 +010058 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010059 """
Azim Khanb31aa442018-07-03 11:57:54 +010060 Test that gen_dep() correctly creates dependencies for given
61 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010062 :return:
Azim Khan4b543232017-06-30 09:35:21 +010063 """
Azim Khanb31aa442018-07-03 11:57:54 +010064 dependencies = ['DEP1', 'DEP2']
65 dep_start, dep_end = gen_dependencies(dependencies)
66 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010067 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010068 self.assertEqual(preprocessor1, '#if defined(DEP1)',
69 'Preprocessor generated incorrectly')
70 self.assertEqual(preprocessor2, '#if defined(DEP2)',
71 'Preprocessor generated incorrectly')
72 self.assertEqual(endif1, '#endif /* DEP2 */',
73 'Preprocessor generated incorrectly')
74 self.assertEqual(endif2, '#endif /* DEP1 */',
75 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010076
Azim Khanb31aa442018-07-03 11:57:54 +010077 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010078 """
Azim Khanb31aa442018-07-03 11:57:54 +010079 Test that gen_dep() correctly creates dependencies for given
80 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010081 :return:
Azim Khan4b543232017-06-30 09:35:21 +010082 """
Azim Khanb31aa442018-07-03 11:57:54 +010083 dependencies = ['!DEP1', '!DEP2']
84 dep_start, dep_end = gen_dependencies(dependencies)
85 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010086 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010087 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
88 'Preprocessor generated incorrectly')
89 self.assertEqual(preprocessor2, '#if !defined(DEP2)',
90 'Preprocessor generated incorrectly')
91 self.assertEqual(endif1, '#endif /* !DEP2 */',
92 'Preprocessor generated incorrectly')
93 self.assertEqual(endif2, '#endif /* !DEP1 */',
94 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010095
Azim Khanb31aa442018-07-03 11:57:54 +010096 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010097 """
Azim Khanb31aa442018-07-03 11:57:54 +010098 Test that gen_dep() correctly creates dependencies for given
99 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100100 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100101 """
Azim Khanb31aa442018-07-03 11:57:54 +0100102 dependencies = ['!DEP1', 'DEP2']
103 dep_start, dep_end = gen_dependencies(dependencies)
104 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +0100105 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +0100106 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
107 'Preprocessor generated incorrectly')
108 self.assertEqual(preprocessor2, '#if defined(DEP2)',
109 'Preprocessor generated incorrectly')
110 self.assertEqual(endif1, '#endif /* DEP2 */',
111 'Preprocessor generated incorrectly')
112 self.assertEqual(endif2, '#endif /* !DEP1 */',
113 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100114
Azim Khanb31aa442018-07-03 11:57:54 +0100115 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100116 """
Azim Khanb31aa442018-07-03 11:57:54 +0100117 Test that gen_dep() correctly creates dependencies for given
118 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100119 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100120 """
Azim Khanb31aa442018-07-03 11:57:54 +0100121 dependencies = []
122 dep_start, dep_end = gen_dependencies(dependencies)
123 self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
124 self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100125
Azim Khanb31aa442018-07-03 11:57:54 +0100126 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100127 """
Azim Khanb31aa442018-07-03 11:57:54 +0100128 Test that gen_dep() correctly creates dependencies for given
129 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100130 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100131 """
Azim Khanb31aa442018-07-03 11:57:54 +0100132 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100133 count = 10
134 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100135 dependencies.append('DEP%d' % i)
136 dep_start, dep_end = gen_dependencies(dependencies)
137 self.assertEqual(len(dep_start.splitlines()), count,
138 'Preprocessor generated incorrectly')
139 self.assertEqual(len(dep_end.splitlines()), count,
140 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100141
142
143class GenDepOneLine(TestCase):
144 """
Azim Khanb31aa442018-07-03 11:57:54 +0100145 Test Suite for testing gen_dependencies_one_line()
Azim Khan4b543232017-06-30 09:35:21 +0100146 """
147
Azim Khanb31aa442018-07-03 11:57:54 +0100148 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100149 """
Azim Khanb31aa442018-07-03 11:57:54 +0100150 Test that gen_dep() correctly creates dependencies for given
151 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100152 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100153 """
Azim Khanb31aa442018-07-03 11:57:54 +0100154 dependencies = ['DEP1', 'DEP2']
155 dep_str = gen_dependencies_one_line(dependencies)
156 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
157 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100158
Azim Khanb31aa442018-07-03 11:57:54 +0100159 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100160 """
Azim Khanb31aa442018-07-03 11:57:54 +0100161 Test that gen_dep() correctly creates dependencies for given
162 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100163 :return:
164 """
Azim Khanb31aa442018-07-03 11:57:54 +0100165 dependencies = ['!DEP1', '!DEP2']
166 dep_str = gen_dependencies_one_line(dependencies)
167 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
168 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100169
Azim Khanb31aa442018-07-03 11:57:54 +0100170 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100171 """
Azim Khanb31aa442018-07-03 11:57:54 +0100172 Test that gen_dep() correctly creates dependencies for given
173 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100174 :return:
175 """
Azim Khanb31aa442018-07-03 11:57:54 +0100176 dependencies = ['!DEP1', 'DEP2']
177 dep_str = gen_dependencies_one_line(dependencies)
178 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
179 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100180
Azim Khanb31aa442018-07-03 11:57:54 +0100181 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100182 """
Azim Khanb31aa442018-07-03 11:57:54 +0100183 Test that gen_dep() correctly creates dependencies for given
184 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100185 :return:
186 """
Azim Khanb31aa442018-07-03 11:57:54 +0100187 dependencies = []
188 dep_str = gen_dependencies_one_line(dependencies)
189 self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100190
Azim Khanb31aa442018-07-03 11:57:54 +0100191 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100192 """
Azim Khanb31aa442018-07-03 11:57:54 +0100193 Test that gen_dep() correctly creates dependencies for given
194 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100195 :return:
196 """
Azim Khanb31aa442018-07-03 11:57:54 +0100197 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100198 count = 10
199 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100200 dependencies.append('DEP%d' % i)
201 dep_str = gen_dependencies_one_line(dependencies)
202 expected = '#if ' + ' && '.join(['defined(%s)' %
203 x for x in dependencies])
204 self.assertEqual(dep_str, expected,
205 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100206
207
208class GenFunctionWrapper(TestCase):
209 """
210 Test Suite for testing gen_function_wrapper()
211 """
212
213 def test_params_unpack(self):
214 """
215 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100216
217 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100218 """
219 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
220 expected = '''
221void test_a_wrapper( void ** params )
222{
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100223
Azim Khan4b543232017-06-30 09:35:21 +0100224 test_a( a, b, c, d );
225}
226'''
227 self.assertEqual(code, expected)
228
229 def test_local(self):
230 """
231 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100232
233 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100234 """
Azim Khanb31aa442018-07-03 11:57:54 +0100235 code = gen_function_wrapper('test_a',
236 'int x = 1;', ('x', 'b', 'c', 'd'))
Azim Khan4b543232017-06-30 09:35:21 +0100237 expected = '''
238void test_a_wrapper( void ** params )
239{
Azim Khan4b543232017-06-30 09:35:21 +0100240int x = 1;
241 test_a( x, b, c, d );
242}
243'''
244 self.assertEqual(code, expected)
245
246 def test_empty_params(self):
247 """
248 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100249
250 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100251 """
252 code = gen_function_wrapper('test_a', '', ())
253 expected = '''
254void test_a_wrapper( void ** params )
255{
256 (void)params;
257
258 test_a( );
259}
260'''
261 self.assertEqual(code, expected)
262
263
264class GenDispatch(TestCase):
265 """
266 Test suite for testing gen_dispatch()
267 """
268
269 def test_dispatch(self):
270 """
271 Test that dispatch table entry is generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100272 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100273 """
274 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
275 expected = '''
276#if defined(DEP1) && defined(DEP2)
277 test_a_wrapper,
278#else
279 NULL,
280#endif
281'''
282 self.assertEqual(code, expected)
283
Azim Khanb31aa442018-07-03 11:57:54 +0100284 def test_empty_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100285 """
286 Test empty dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100287 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100288 """
289 code = gen_dispatch('test_a', [])
290 expected = '''
291 test_a_wrapper,
292'''
293 self.assertEqual(code, expected)
294
295
296class StringIOWrapper(StringIO, object):
297 """
298 file like class to mock file object in tests.
299 """
Azim Khan4084ec72018-07-05 14:20:08 +0100300 def __init__(self, file_name, data, line_no=0):
Azim Khan4b543232017-06-30 09:35:21 +0100301 """
302 Init file handle.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100303
304 :param file_name:
Azim Khan4b543232017-06-30 09:35:21 +0100305 :param data:
306 :param line_no:
307 """
308 super(StringIOWrapper, self).__init__(data)
309 self.line_no = line_no
310 self.name = file_name
311
312 def next(self):
313 """
Azim Khanb31aa442018-07-03 11:57:54 +0100314 Iterator method. This method overrides base class's
315 next method and extends the next method to count the line
316 numbers as each line is read.
Azim Khan4b543232017-06-30 09:35:21 +0100317
Azim Khanb31aa442018-07-03 11:57:54 +0100318 :return: Line read from file.
319 """
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100320 parent = super(StringIOWrapper, self)
321 if getattr(parent, 'next', None):
322 # Python 2
323 line = parent.next()
324 else:
325 # Python 3
326 line = parent.__next__()
Azim Khan4084ec72018-07-05 14:20:08 +0100327 return line
Azim Khanb31aa442018-07-03 11:57:54 +0100328
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100329 # Python 3
Azim Khanb31aa442018-07-03 11:57:54 +0100330 __next__ = next
331
332 def readline(self, length=0):
Azim Khan4b543232017-06-30 09:35:21 +0100333 """
334 Wrap the base class readline.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100335
Azim Khanb31aa442018-07-03 11:57:54 +0100336 :param length:
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100337 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100338 """
339 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)
1128 tests = [(name, test_function, dependencies, args)
1129 for name, test_function, dependencies, args in
1130 parse_test_data(stream)]
1131 test1, test2, test3, test4 = tests
1132 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1133 self.assertEqual(test1[1], 'dhm_do_dhm')
1134 self.assertEqual(test1[2], [])
1135 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001136
Azim Khanb31aa442018-07-03 11:57:54 +01001137 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1138 self.assertEqual(test2[1], 'dhm_do_dhm')
1139 self.assertEqual(test2[2], [])
1140 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1141 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001142
Azim Khanb31aa442018-07-03 11:57:54 +01001143 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1144 self.assertEqual(test3[1], 'dhm_do_dhm')
1145 self.assertEqual(test3[2], [])
1146 self.assertEqual(test3[3], ['10',
1147 '"9345098382739712938719287391879381271"',
1148 '10',
1149 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001150
Azim Khanb31aa442018-07-03 11:57:54 +01001151 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1152 self.assertEqual(test4[1], 'dhm_selftest')
1153 self.assertEqual(test4[2], [])
1154 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001155
1156 def test_with_dependencies(self):
1157 """
1158 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001159 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001160 """
1161 data = """
1162Diffie-Hellman full exchange #1
1163depends_on:YAHOO
1164dhm_do_dhm:10:"23":10:"5"
1165
1166Diffie-Hellman full exchange #2
1167dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1168
1169"""
Azim Khanb31aa442018-07-03 11:57:54 +01001170 stream = StringIOWrapper('test_suite_ut.function', data)
1171 tests = [(name, function_name, dependencies, args)
1172 for name, function_name, dependencies, args in
1173 parse_test_data(stream)]
1174 test1, test2 = tests
1175 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1176 self.assertEqual(test1[1], 'dhm_do_dhm')
1177 self.assertEqual(test1[2], ['YAHOO'])
1178 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001179
Azim Khanb31aa442018-07-03 11:57:54 +01001180 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1181 self.assertEqual(test2[1], 'dhm_do_dhm')
1182 self.assertEqual(test2[2], [])
1183 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1184 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001185
1186 def test_no_args(self):
1187 """
Azim Khanb31aa442018-07-03 11:57:54 +01001188 Test GeneratorInputError is raised when test function name and
1189 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001190 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001191 """
1192 data = """
1193Diffie-Hellman full exchange #1
1194depends_on:YAHOO
1195
1196
1197Diffie-Hellman full exchange #2
1198dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1199
1200"""
Azim Khanb31aa442018-07-03 11:57:54 +01001201 stream = StringIOWrapper('test_suite_ut.function', data)
1202 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001203 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001204 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001205 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001206 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001207 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001208
1209 def test_incomplete_data(self):
1210 """
Azim Khanb31aa442018-07-03 11:57:54 +01001211 Test GeneratorInputError is raised when test function name
1212 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001213 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001214 """
1215 data = """
1216Diffie-Hellman full exchange #1
1217depends_on:YAHOO
1218"""
Azim Khanb31aa442018-07-03 11:57:54 +01001219 stream = StringIOWrapper('test_suite_ut.function', data)
1220 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001221 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001222 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001223 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001224 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001225 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001226
1227
1228class GenDepCheck(TestCase):
1229 """
Azim Khanb31aa442018-07-03 11:57:54 +01001230 Test suite for gen_dep_check(). It is assumed this function is
1231 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001232 """
1233
1234 def test_gen_dep_check(self):
1235 """
1236 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001237 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001238 """
1239 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001240 case 5:
1241 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001242#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001243 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001244#else
Azim Khand61b8372017-07-10 11:54:01 +01001245 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001246#endif
Azim Khand61b8372017-07-10 11:54:01 +01001247 }
1248 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001249 out = gen_dep_check(5, 'YAHOO')
1250 self.assertEqual(out, expected)
1251
Azim Khanb31aa442018-07-03 11:57:54 +01001252 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001253 """
1254 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001255 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001256 """
1257 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001258 case 5:
1259 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001260#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001261 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001262#else
Azim Khand61b8372017-07-10 11:54:01 +01001263 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001264#endif
Azim Khand61b8372017-07-10 11:54:01 +01001265 }
1266 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001267 out = gen_dep_check(5, '!YAHOO')
1268 self.assertEqual(out, expected)
1269
1270 def test_empty_dependency(self):
1271 """
1272 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001273 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001274 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001275 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001276
1277 def test_negative_dep_id(self):
1278 """
1279 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001280 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001281 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001282 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001283
1284
1285class GenExpCheck(TestCase):
1286 """
Azim Khanb31aa442018-07-03 11:57:54 +01001287 Test suite for gen_expression_check(). It is assumed this function
1288 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001289 """
1290
1291 def test_gen_exp_check(self):
1292 """
1293 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001294 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001295 """
1296 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001297 case 5:
1298 {
1299 *out_value = YAHOO;
1300 }
1301 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001302 out = gen_expression_check(5, 'YAHOO')
1303 self.assertEqual(out, expected)
1304
1305 def test_invalid_expression(self):
1306 """
1307 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001308 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001309 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001310 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001311
1312 def test_negative_exp_id(self):
1313 """
1314 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001315 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001316 """
Azim Khanb31aa442018-07-03 11:57:54 +01001317 self.assertRaises(GeneratorInputError, gen_expression_check,
1318 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001319
1320
Azim Khanb31aa442018-07-03 11:57:54 +01001321class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001322 """
Azim Khanb31aa442018-07-03 11:57:54 +01001323 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001324 """
1325
Azim Khanb31aa442018-07-03 11:57:54 +01001326 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001327 """
Azim Khanb31aa442018-07-03 11:57:54 +01001328 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001329 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001330 """
Azim Khanb31aa442018-07-03 11:57:54 +01001331 stream = StringIOWrapper('test_suite_ut.data', '')
1332 unique_dependencies = []
1333 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001334 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001335 self.assertEqual(len(unique_dependencies), 0)
1336 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001337
1338 def test_unique_dep_ids(self):
1339 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001340
1341 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001342 """
Azim Khanb31aa442018-07-03 11:57:54 +01001343 stream = StringIOWrapper('test_suite_ut.data', '')
1344 unique_dependencies = []
1345 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1346 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001347 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001348 case 0:
1349 {
Azim Khan599cd242017-07-06 17:34:27 +01001350#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001351 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001352#else
Azim Khand61b8372017-07-10 11:54:01 +01001353 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001354#endif
Azim Khand61b8372017-07-10 11:54:01 +01001355 }
1356 break;
1357 case 1:
1358 {
Azim Khan599cd242017-07-06 17:34:27 +01001359#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001360 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001361#else
Azim Khand61b8372017-07-10 11:54:01 +01001362 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001363#endif
Azim Khand61b8372017-07-10 11:54:01 +01001364 }
1365 break;
1366 case 2:
1367 {
Azim Khan599cd242017-07-06 17:34:27 +01001368#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001369 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001370#else
Azim Khand61b8372017-07-10 11:54:01 +01001371 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001372#endif
Azim Khand61b8372017-07-10 11:54:01 +01001373 }
1374 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001375 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001376 self.assertEqual(len(unique_dependencies), 3)
1377 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001378
1379 def test_dep_id_repeat(self):
1380 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001381
1382 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001383 """
Azim Khanb31aa442018-07-03 11:57:54 +01001384 stream = StringIOWrapper('test_suite_ut.data', '')
1385 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001386 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001387 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1388 unique_dependencies)
1389 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1390 unique_dependencies)
1391 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1392 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001393 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001394 case 0:
1395 {
Azim Khan599cd242017-07-06 17:34:27 +01001396#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001397 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001398#else
Azim Khand61b8372017-07-10 11:54:01 +01001399 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001400#endif
Azim Khand61b8372017-07-10 11:54:01 +01001401 }
1402 break;
1403 case 1:
1404 {
Azim Khan599cd242017-07-06 17:34:27 +01001405#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001406 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001407#else
Azim Khand61b8372017-07-10 11:54:01 +01001408 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001409#endif
Azim Khand61b8372017-07-10 11:54:01 +01001410 }
1411 break;
1412 case 2:
1413 {
Azim Khan599cd242017-07-06 17:34:27 +01001414#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001415 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001416#else
Azim Khand61b8372017-07-10 11:54:01 +01001417 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001418#endif
Azim Khand61b8372017-07-10 11:54:01 +01001419 }
1420 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001421 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001422 self.assertEqual(len(unique_dependencies), 3)
1423 self.assertEqual(stream.getvalue(),
1424 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001425
1426
1427class WriteParams(TestCase):
1428 """
1429 Test Suite for testing write_parameters().
1430 """
1431
1432 def test_no_params(self):
1433 """
1434 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001435 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001436 """
Azim Khanb31aa442018-07-03 11:57:54 +01001437 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001438 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001439 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001440 self.assertEqual(len(unique_expressions), 0)
1441 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001442 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001443
1444 def test_no_exp_param(self):
1445 """
1446 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001447 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001448 """
Azim Khanb31aa442018-07-03 11:57:54 +01001449 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001450 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001451 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1452 '0'],
1453 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001454 unique_expressions)
1455 self.assertEqual(len(unique_expressions), 0)
1456 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001457 self.assertEqual(stream.getvalue(),
1458 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001459
1460 def test_hex_format_int_param(self):
1461 """
1462 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001463 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001464 """
Azim Khanb31aa442018-07-03 11:57:54 +01001465 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001466 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001467 expression_code = write_parameters(stream,
1468 ['"Yahoo"', '"abcdef00"', '0xAA'],
1469 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001470 unique_expressions)
1471 self.assertEqual(len(unique_expressions), 0)
1472 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001473 self.assertEqual(stream.getvalue(),
1474 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001475
1476 def test_with_exp_param(self):
1477 """
1478 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001479 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001480 """
Azim Khanb31aa442018-07-03 11:57:54 +01001481 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001482 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001483 expression_code = write_parameters(stream,
1484 ['"Yahoo"', '"abcdef00"', '0',
1485 'MACRO1', 'MACRO2', 'MACRO3'],
1486 ['char*', 'hex', 'int',
1487 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001488 unique_expressions)
1489 self.assertEqual(len(unique_expressions), 3)
1490 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1491 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001492 case 0:
1493 {
1494 *out_value = MACRO1;
1495 }
1496 break;
1497 case 1:
1498 {
1499 *out_value = MACRO2;
1500 }
1501 break;
1502 case 2:
1503 {
1504 *out_value = MACRO3;
1505 }
1506 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001507 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001508 self.assertEqual(stream.getvalue(),
1509 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1510 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001511
Azim Khanb31aa442018-07-03 11:57:54 +01001512 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001513 """
1514 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001515 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001516 """
Azim Khanb31aa442018-07-03 11:57:54 +01001517 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001518 unique_expressions = []
1519 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001520 expression_code += write_parameters(stream,
1521 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1522 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001523 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001524 expression_code += write_parameters(stream,
1525 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1526 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001527 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001528 expression_code += write_parameters(stream,
1529 ['0', 'MACRO3', 'MACRO1'],
1530 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001531 unique_expressions)
1532 self.assertEqual(len(unique_expressions), 3)
1533 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1534 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001535 case 0:
1536 {
1537 *out_value = MACRO1;
1538 }
1539 break;
1540 case 1:
1541 {
1542 *out_value = MACRO2;
1543 }
1544 break;
1545 case 2:
1546 {
1547 *out_value = MACRO3;
1548 }
1549 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001550 self.assertEqual(expression_code, expected_expression_code)
1551 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1552:hex:"abcdef00":exp:1:exp:2
1553:int:0:exp:2:exp:0
1554'''
Azim Khanb31aa442018-07-03 11:57:54 +01001555 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001556
1557
Azim Khanb31aa442018-07-03 11:57:54 +01001558class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001559 """
Azim Khanb31aa442018-07-03 11:57:54 +01001560 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001561 """
Azim Khanb31aa442018-07-03 11:57:54 +01001562 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001563 """
Azim Khanb31aa442018-07-03 11:57:54 +01001564 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001565
1566 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001567 """
Azim Khanb31aa442018-07-03 11:57:54 +01001568 dep_check_code, expression_code = \
1569 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001570 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1571 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1572
Azim Khanb31aa442018-07-03 11:57:54 +01001573 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001574 """
Azim Khanb31aa442018-07-03 11:57:54 +01001575 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001576
1577 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001578 """
Azim Khanb31aa442018-07-03 11:57:54 +01001579 dep_check_code, expression_code = \
1580 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1581 'EXPRESSION_CODE')
1582 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001583#if defined(SUITE_DEP)
1584DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001585#endif
1586'''
1587 expected_expression_code = '''
1588#if defined(SUITE_DEP)
1589EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001590#endif
1591'''
Azim Khanb31aa442018-07-03 11:57:54 +01001592 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001593 self.assertEqual(expression_code, expected_expression_code)
1594
1595 def test_no_dep_no_exp(self):
1596 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001597 Test when there are no dependency and expression code.
1598 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001599 """
Azim Khanb31aa442018-07-03 11:57:54 +01001600 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001601 self.assertEqual(dep_check_code, '')
1602 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001603
1604
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001605class GenFromTestData(TestCase):
1606 """
1607 Test suite for gen_from_test_data()
1608 """
1609
Azim Khanb31aa442018-07-03 11:57:54 +01001610 @staticmethod
1611 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001612 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001613 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001614 def test_intermediate_data_file(func_mock1,
1615 write_parameters_mock,
1616 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001617 """
1618 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001619 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001620 """
1621 data = '''
1622My test
1623depends_on:DEP1
1624func1:0
1625'''
1626 data_f = StringIOWrapper('test_suite_ut.data', data)
1627 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1628 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001629 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001630 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001631 write_dependencies_mock.side_effect = write_dependencies
1632 func_mock1.side_effect = gen_suite_dep_checks
1633 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1634 write_dependencies_mock.assert_called_with(out_data_f,
1635 ['DEP1'], ['DEP1'])
1636 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1637 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001638 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001639 case 0:
1640 {
Azim Khan599cd242017-07-06 17:34:27 +01001641#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001642 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001643#else
Azim Khand61b8372017-07-10 11:54:01 +01001644 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001645#endif
Azim Khand61b8372017-07-10 11:54:01 +01001646 }
1647 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001648 func_mock1.assert_called_with(
1649 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001650
1651 def test_function_not_found(self):
1652 """
1653 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001654 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001655 """
1656 data = '''
1657My test
1658depends_on:DEP1
1659func1:0
1660'''
1661 data_f = StringIOWrapper('test_suite_ut.data', data)
1662 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1663 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001664 suite_dependencies = []
1665 self.assertRaises(GeneratorInputError, gen_from_test_data,
1666 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001667
1668 def test_different_func_args(self):
1669 """
Azim Khanb31aa442018-07-03 11:57:54 +01001670 Test that AssertError is raised when no. of parameters and
1671 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001672 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001673 """
1674 data = '''
1675My test
1676depends_on:DEP1
1677func1:0
1678'''
1679 data_f = StringIOWrapper('test_suite_ut.data', data)
1680 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001681 func_info = {'test_func2': (1, ('int', 'hex'))}
1682 suite_dependencies = []
1683 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1684 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001685
1686 def test_output(self):
1687 """
1688 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001689 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001690 """
1691 data = '''
1692My test 1
1693depends_on:DEP1
1694func1:0:0xfa:MACRO1:MACRO2
1695
1696My test 2
1697depends_on:DEP1:DEP2
1698func2:"yahoo":88:MACRO1
1699'''
1700 data_f = StringIOWrapper('test_suite_ut.data', data)
1701 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001702 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1703 'test_func2': (1, ('char*', 'int', 'int'))}
1704 suite_dependencies = []
1705 dep_check_code, expression_code = \
1706 gen_from_test_data(data_f, out_data_f, func_info,
1707 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001708 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001709 case 0:
1710 {
Azim Khan599cd242017-07-06 17:34:27 +01001711#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001712 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001713#else
Azim Khand61b8372017-07-10 11:54:01 +01001714 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001715#endif
Azim Khand61b8372017-07-10 11:54:01 +01001716 }
1717 break;
1718 case 1:
1719 {
Azim Khan599cd242017-07-06 17:34:27 +01001720#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001721 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001722#else
Azim Khand61b8372017-07-10 11:54:01 +01001723 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001724#endif
Azim Khand61b8372017-07-10 11:54:01 +01001725 }
1726 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001727 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001728depends_on:0
17290:int:0:int:0xfa:exp:0:exp:1
1730
1731My test 2
1732depends_on:0:1
17331:char*:"yahoo":int:88:exp:0
1734
1735'''
1736 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001737 case 0:
1738 {
1739 *out_value = MACRO1;
1740 }
1741 break;
1742 case 1:
1743 {
1744 *out_value = MACRO2;
1745 }
1746 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001747 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001748 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001749 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001750
1751
Azim Khanb31aa442018-07-03 11:57:54 +01001752if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001753 unittest_main()