blob: 6d7113e18bee53c2a9010f3e7df278bdbf63cde5 [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
Gilles Peskinee915d532019-02-25 21:39:42 +010025# pylint: disable=wrong-import-order
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
Gilles Peskinee915d532019-02-25 21:39:42 +010039# pylint: enable=wrong-import-order
Azim Khanb31aa442018-07-03 11:57:54 +010040from generate_test_code import gen_dependencies, gen_dependencies_one_line
41from generate_test_code import gen_function_wrapper, gen_dispatch
42from generate_test_code import parse_until_pattern, GeneratorInputError
43from generate_test_code import parse_suite_dependencies
44from generate_test_code import parse_function_dependencies
Azim Khanfcdf6852018-07-05 17:31:46 +010045from generate_test_code import parse_function_arguments, parse_function_code
Azim Khanb31aa442018-07-03 11:57:54 +010046from generate_test_code import parse_functions, END_HEADER_REGEX
47from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
48from generate_test_code import parse_test_data, gen_dep_check
49from generate_test_code import gen_expression_check, write_dependencies
50from generate_test_code import write_parameters, gen_suite_dep_checks
51from generate_test_code import gen_from_test_data
52
53
Azim Khan4b543232017-06-30 09:35:21 +010054class GenDep(TestCase):
55 """
56 Test suite for function gen_dep()
57 """
58
Azim Khanb31aa442018-07-03 11:57:54 +010059 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010060 """
Azim Khanb31aa442018-07-03 11:57:54 +010061 Test that gen_dep() correctly creates dependencies for given
62 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010063 :return:
Azim Khan4b543232017-06-30 09:35:21 +010064 """
Azim Khanb31aa442018-07-03 11:57:54 +010065 dependencies = ['DEP1', 'DEP2']
66 dep_start, dep_end = gen_dependencies(dependencies)
67 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010068 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010069 self.assertEqual(preprocessor1, '#if defined(DEP1)',
70 'Preprocessor generated incorrectly')
71 self.assertEqual(preprocessor2, '#if defined(DEP2)',
72 'Preprocessor generated incorrectly')
73 self.assertEqual(endif1, '#endif /* DEP2 */',
74 'Preprocessor generated incorrectly')
75 self.assertEqual(endif2, '#endif /* DEP1 */',
76 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010077
Azim Khanb31aa442018-07-03 11:57:54 +010078 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010079 """
Azim Khanb31aa442018-07-03 11:57:54 +010080 Test that gen_dep() correctly creates dependencies for given
81 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010082 :return:
Azim Khan4b543232017-06-30 09:35:21 +010083 """
Azim Khanb31aa442018-07-03 11:57:54 +010084 dependencies = ['!DEP1', '!DEP2']
85 dep_start, dep_end = gen_dependencies(dependencies)
86 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010087 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010088 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
89 'Preprocessor generated incorrectly')
90 self.assertEqual(preprocessor2, '#if !defined(DEP2)',
91 'Preprocessor generated incorrectly')
92 self.assertEqual(endif1, '#endif /* !DEP2 */',
93 'Preprocessor generated incorrectly')
94 self.assertEqual(endif2, '#endif /* !DEP1 */',
95 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010096
Azim Khanb31aa442018-07-03 11:57:54 +010097 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010098 """
Azim Khanb31aa442018-07-03 11:57:54 +010099 Test that gen_dep() correctly creates dependencies for given
100 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100101 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100102 """
Azim Khanb31aa442018-07-03 11:57:54 +0100103 dependencies = ['!DEP1', 'DEP2']
104 dep_start, dep_end = gen_dependencies(dependencies)
105 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +0100106 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +0100107 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
108 'Preprocessor generated incorrectly')
109 self.assertEqual(preprocessor2, '#if defined(DEP2)',
110 'Preprocessor generated incorrectly')
111 self.assertEqual(endif1, '#endif /* DEP2 */',
112 'Preprocessor generated incorrectly')
113 self.assertEqual(endif2, '#endif /* !DEP1 */',
114 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100115
Azim Khanb31aa442018-07-03 11:57:54 +0100116 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100117 """
Azim Khanb31aa442018-07-03 11:57:54 +0100118 Test that gen_dep() correctly creates dependencies for given
119 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100120 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100121 """
Azim Khanb31aa442018-07-03 11:57:54 +0100122 dependencies = []
123 dep_start, dep_end = gen_dependencies(dependencies)
124 self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
125 self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100126
Azim Khanb31aa442018-07-03 11:57:54 +0100127 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100128 """
Azim Khanb31aa442018-07-03 11:57:54 +0100129 Test that gen_dep() correctly creates dependencies for given
130 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100131 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100132 """
Azim Khanb31aa442018-07-03 11:57:54 +0100133 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100134 count = 10
135 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100136 dependencies.append('DEP%d' % i)
137 dep_start, dep_end = gen_dependencies(dependencies)
138 self.assertEqual(len(dep_start.splitlines()), count,
139 'Preprocessor generated incorrectly')
140 self.assertEqual(len(dep_end.splitlines()), count,
141 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100142
143
144class GenDepOneLine(TestCase):
145 """
Azim Khanb31aa442018-07-03 11:57:54 +0100146 Test Suite for testing gen_dependencies_one_line()
Azim Khan4b543232017-06-30 09:35:21 +0100147 """
148
Azim Khanb31aa442018-07-03 11:57:54 +0100149 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100150 """
Azim Khanb31aa442018-07-03 11:57:54 +0100151 Test that gen_dep() correctly creates dependencies for given
152 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100153 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100154 """
Azim Khanb31aa442018-07-03 11:57:54 +0100155 dependencies = ['DEP1', 'DEP2']
156 dep_str = gen_dependencies_one_line(dependencies)
157 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
158 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100159
Azim Khanb31aa442018-07-03 11:57:54 +0100160 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100161 """
Azim Khanb31aa442018-07-03 11:57:54 +0100162 Test that gen_dep() correctly creates dependencies for given
163 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100164 :return:
165 """
Azim Khanb31aa442018-07-03 11:57:54 +0100166 dependencies = ['!DEP1', '!DEP2']
167 dep_str = gen_dependencies_one_line(dependencies)
168 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
169 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100170
Azim Khanb31aa442018-07-03 11:57:54 +0100171 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100172 """
Azim Khanb31aa442018-07-03 11:57:54 +0100173 Test that gen_dep() correctly creates dependencies for given
174 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100175 :return:
176 """
Azim Khanb31aa442018-07-03 11:57:54 +0100177 dependencies = ['!DEP1', 'DEP2']
178 dep_str = gen_dependencies_one_line(dependencies)
179 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
180 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100181
Azim Khanb31aa442018-07-03 11:57:54 +0100182 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100183 """
Azim Khanb31aa442018-07-03 11:57:54 +0100184 Test that gen_dep() correctly creates dependencies for given
185 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100186 :return:
187 """
Azim Khanb31aa442018-07-03 11:57:54 +0100188 dependencies = []
189 dep_str = gen_dependencies_one_line(dependencies)
190 self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100191
Azim Khanb31aa442018-07-03 11:57:54 +0100192 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100193 """
Azim Khanb31aa442018-07-03 11:57:54 +0100194 Test that gen_dep() correctly creates dependencies for given
195 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100196 :return:
197 """
Azim Khanb31aa442018-07-03 11:57:54 +0100198 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100199 count = 10
200 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100201 dependencies.append('DEP%d' % i)
202 dep_str = gen_dependencies_one_line(dependencies)
203 expected = '#if ' + ' && '.join(['defined(%s)' %
204 x for x in dependencies])
205 self.assertEqual(dep_str, expected,
206 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100207
208
209class GenFunctionWrapper(TestCase):
210 """
211 Test Suite for testing gen_function_wrapper()
212 """
213
214 def test_params_unpack(self):
215 """
216 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100217
218 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100219 """
220 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
221 expected = '''
222void test_a_wrapper( void ** params )
223{
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100224
Azim Khan4b543232017-06-30 09:35:21 +0100225 test_a( a, b, c, d );
226}
227'''
228 self.assertEqual(code, expected)
229
230 def test_local(self):
231 """
232 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100233
234 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100235 """
Azim Khanb31aa442018-07-03 11:57:54 +0100236 code = gen_function_wrapper('test_a',
237 'int x = 1;', ('x', 'b', 'c', 'd'))
Azim Khan4b543232017-06-30 09:35:21 +0100238 expected = '''
239void test_a_wrapper( void ** params )
240{
Azim Khan4b543232017-06-30 09:35:21 +0100241int x = 1;
242 test_a( x, b, c, d );
243}
244'''
245 self.assertEqual(code, expected)
246
247 def test_empty_params(self):
248 """
249 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100250
251 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100252 """
253 code = gen_function_wrapper('test_a', '', ())
254 expected = '''
255void test_a_wrapper( void ** params )
256{
257 (void)params;
258
259 test_a( );
260}
261'''
262 self.assertEqual(code, expected)
263
264
265class GenDispatch(TestCase):
266 """
267 Test suite for testing gen_dispatch()
268 """
269
270 def test_dispatch(self):
271 """
272 Test that dispatch table entry is generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100273 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100274 """
275 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
276 expected = '''
277#if defined(DEP1) && defined(DEP2)
278 test_a_wrapper,
279#else
280 NULL,
281#endif
282'''
283 self.assertEqual(code, expected)
284
Azim Khanb31aa442018-07-03 11:57:54 +0100285 def test_empty_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100286 """
287 Test empty dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100288 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100289 """
290 code = gen_dispatch('test_a', [])
291 expected = '''
292 test_a_wrapper,
293'''
294 self.assertEqual(code, expected)
295
296
297class StringIOWrapper(StringIO, object):
298 """
299 file like class to mock file object in tests.
300 """
Azim Khan4084ec72018-07-05 14:20:08 +0100301 def __init__(self, file_name, data, line_no=0):
Azim Khan4b543232017-06-30 09:35:21 +0100302 """
303 Init file handle.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100304
305 :param file_name:
Azim Khan4b543232017-06-30 09:35:21 +0100306 :param data:
307 :param line_no:
308 """
309 super(StringIOWrapper, self).__init__(data)
310 self.line_no = line_no
311 self.name = file_name
312
313 def next(self):
314 """
Azim Khanb31aa442018-07-03 11:57:54 +0100315 Iterator method. This method overrides base class's
316 next method and extends the next method to count the line
317 numbers as each line is read.
Azim Khan4b543232017-06-30 09:35:21 +0100318
Azim Khanb31aa442018-07-03 11:57:54 +0100319 :return: Line read from file.
320 """
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100321 parent = super(StringIOWrapper, self)
322 if getattr(parent, 'next', None):
323 # Python 2
324 line = parent.next()
325 else:
326 # Python 3
327 line = parent.__next__()
Azim Khan4084ec72018-07-05 14:20:08 +0100328 return line
Azim Khanb31aa442018-07-03 11:57:54 +0100329
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100330 # Python 3
Azim Khanb31aa442018-07-03 11:57:54 +0100331 __next__ = next
332
333 def readline(self, length=0):
Azim Khan4b543232017-06-30 09:35:21 +0100334 """
335 Wrap the base class readline.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100336
Azim Khanb31aa442018-07-03 11:57:54 +0100337 :param length:
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100338 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100339 """
Gilles Peskinee915d532019-02-25 21:39:42 +0100340 # pylint: disable=unused-argument
Azim Khan4b543232017-06-30 09:35:21 +0100341 line = super(StringIOWrapper, self).readline()
Azim Khan4084ec72018-07-05 14:20:08 +0100342 if line is not None:
Azim Khan4b543232017-06-30 09:35:21 +0100343 self.line_no += 1
344 return line
345
346
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000347class ParseUntilPattern(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100348 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000349 Test Suite for testing parse_until_pattern().
Azim Khan4b543232017-06-30 09:35:21 +0100350 """
351
352 def test_suite_headers(self):
353 """
354 Test that suite headers are parsed correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100355
356 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100357 """
358 data = '''#include "mbedtls/ecp.h"
359
360#define ECP_PF_UNKNOWN -1
361/* END_HEADER */
362'''
363 expected = '''#line 1 "test_suite_ut.function"
364#include "mbedtls/ecp.h"
365
366#define ECP_PF_UNKNOWN -1
367'''
Azim Khanb31aa442018-07-03 11:57:54 +0100368 stream = StringIOWrapper('test_suite_ut.function', data, line_no=0)
369 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100370 self.assertEqual(headers, expected)
371
372 def test_line_no(self):
373 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100374 Test that #line is set to correct line no. in source .function file.
375
376 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100377 """
378 data = '''#include "mbedtls/ecp.h"
379
380#define ECP_PF_UNKNOWN -1
381/* END_HEADER */
382'''
383 offset_line_no = 5
384 expected = '''#line %d "test_suite_ut.function"
385#include "mbedtls/ecp.h"
386
387#define ECP_PF_UNKNOWN -1
388''' % (offset_line_no + 1)
Azim Khanb31aa442018-07-03 11:57:54 +0100389 stream = StringIOWrapper('test_suite_ut.function', data,
390 offset_line_no)
391 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100392 self.assertEqual(headers, expected)
393
394 def test_no_end_header_comment(self):
395 """
Azim Khanb31aa442018-07-03 11:57:54 +0100396 Test that InvalidFileFormat is raised when end header comment is
397 missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100398 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100399 """
400 data = '''#include "mbedtls/ecp.h"
401
402#define ECP_PF_UNKNOWN -1
403
404'''
Azim Khanb31aa442018-07-03 11:57:54 +0100405 stream = StringIOWrapper('test_suite_ut.function', data)
406 self.assertRaises(GeneratorInputError, parse_until_pattern, stream,
407 END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100408
409
Azim Khanb31aa442018-07-03 11:57:54 +0100410class ParseSuiteDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100411 """
Azim Khanb31aa442018-07-03 11:57:54 +0100412 Test Suite for testing parse_suite_dependencies().
Azim Khan4b543232017-06-30 09:35:21 +0100413 """
414
Azim Khanb31aa442018-07-03 11:57:54 +0100415 def test_suite_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100416 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100417
418 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100419 """
420 data = '''
421 * depends_on:MBEDTLS_ECP_C
422 * END_DEPENDENCIES
423 */
424'''
425 expected = ['MBEDTLS_ECP_C']
Azim Khanb31aa442018-07-03 11:57:54 +0100426 stream = StringIOWrapper('test_suite_ut.function', data)
427 dependencies = parse_suite_dependencies(stream)
428 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100429
430 def test_no_end_dep_comment(self):
431 """
432 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100433 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100434 """
435 data = '''
436* depends_on:MBEDTLS_ECP_C
437'''
Azim Khanb31aa442018-07-03 11:57:54 +0100438 stream = StringIOWrapper('test_suite_ut.function', data)
439 self.assertRaises(GeneratorInputError, parse_suite_dependencies,
440 stream)
Azim Khan4b543232017-06-30 09:35:21 +0100441
Azim Khanb31aa442018-07-03 11:57:54 +0100442 def test_dependencies_split(self):
Azim Khan4b543232017-06-30 09:35:21 +0100443 """
444 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100445 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100446 """
447 data = '''
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100448 * depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
Azim Khan4b543232017-06-30 09:35:21 +0100449 * END_DEPENDENCIES
450 */
451'''
452 expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
Azim Khanb31aa442018-07-03 11:57:54 +0100453 stream = StringIOWrapper('test_suite_ut.function', data)
454 dependencies = parse_suite_dependencies(stream)
455 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100456
457
Azim Khanb31aa442018-07-03 11:57:54 +0100458class ParseFuncDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100459 """
Azim Khanb31aa442018-07-03 11:57:54 +0100460 Test Suite for testing parse_function_dependencies()
Azim Khan4b543232017-06-30 09:35:21 +0100461 """
462
Azim Khanb31aa442018-07-03 11:57:54 +0100463 def test_function_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100464 """
Azim Khanb31aa442018-07-03 11:57:54 +0100465 Test that parse_function_dependencies() correctly parses function
466 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100467 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100468 """
Azim Khanb31aa442018-07-03 11:57:54 +0100469 line = '/* BEGIN_CASE ' \
470 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
Azim Khan4b543232017-06-30 09:35:21 +0100471 expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
Azim Khanb31aa442018-07-03 11:57:54 +0100472 dependencies = parse_function_dependencies(line)
473 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100474
Azim Khanb31aa442018-07-03 11:57:54 +0100475 def test_no_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100476 """
Azim Khanb31aa442018-07-03 11:57:54 +0100477 Test that parse_function_dependencies() correctly parses function
478 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100479 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100480 """
481 line = '/* BEGIN_CASE */'
Azim Khanb31aa442018-07-03 11:57:54 +0100482 dependencies = parse_function_dependencies(line)
483 self.assertEqual(dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100484
Azim Khanb31aa442018-07-03 11:57:54 +0100485 def test_tolerance(self):
Azim Khan4b543232017-06-30 09:35:21 +0100486 """
Azim Khanb31aa442018-07-03 11:57:54 +0100487 Test that parse_function_dependencies() correctly parses function
488 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100489 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100490 """
491 line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
Azim Khanb31aa442018-07-03 11:57:54 +0100492 dependencies = parse_function_dependencies(line)
493 self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
Azim Khan4b543232017-06-30 09:35:21 +0100494
495
496class ParseFuncSignature(TestCase):
497 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100498 Test Suite for parse_function_arguments().
Azim Khan4b543232017-06-30 09:35:21 +0100499 """
500
501 def test_int_and_char_params(self):
502 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100503 Test int and char parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100504 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100505 """
506 line = 'void entropy_threshold( char * a, int b, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100507 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100508 self.assertEqual(args, ['char*', 'int', 'int'])
509 self.assertEqual(local, '')
Azim Khanb31aa442018-07-03 11:57:54 +0100510 self.assertEqual(arg_dispatch, ['(char *) params[0]',
511 '*( (int *) params[1] )',
512 '*( (int *) params[2] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100513
514 def test_hex_params(self):
515 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100516 Test hex parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100517 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100518 """
Azim Khan5fcca462018-06-29 11:05:32 +0100519 line = 'void entropy_threshold( char * a, data_t * h, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100520 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100521 self.assertEqual(args, ['char*', 'hex', 'int'])
Azim Khanb31aa442018-07-03 11:57:54 +0100522 self.assertEqual(local,
Azim Khan4084ec72018-07-05 14:20:08 +0100523 ' data_t data1 = {(uint8_t *) params[1], '
Azim Khanb31aa442018-07-03 11:57:54 +0100524 '*( (uint32_t *) params[2] )};\n')
525 self.assertEqual(arg_dispatch, ['(char *) params[0]',
Azim Khan4084ec72018-07-05 14:20:08 +0100526 '&data1',
Azim Khanb31aa442018-07-03 11:57:54 +0100527 '*( (int *) params[3] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100528
Azim Khan4b543232017-06-30 09:35:21 +0100529 def test_unsupported_arg(self):
530 """
Azim Khan5fcca462018-06-29 11:05:32 +0100531 Test unsupported arguments (not among int, char * and data_t)
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100532 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100533 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100534 line = 'void entropy_threshold( char * a, data_t * h, char result )'
535 self.assertRaises(ValueError, parse_function_arguments, line)
Azim Khan4b543232017-06-30 09:35:21 +0100536
537 def test_no_params(self):
538 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100539 Test no parameters.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100540 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100541 """
542 line = 'void entropy_threshold()'
Azim Khanfcdf6852018-07-05 17:31:46 +0100543 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100544 self.assertEqual(args, [])
545 self.assertEqual(local, '')
546 self.assertEqual(arg_dispatch, [])
547
548
549class ParseFunctionCode(TestCase):
550 """
551 Test suite for testing parse_function_code()
552 """
553
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100554 def assert_raises_regex(self, exp, regex, func, *args):
555 """
556 Python 2 & 3 portable wrapper of assertRaisesRegex(p)? function.
557
558 :param exp: Exception type expected to be raised by cb.
559 :param regex: Expected exception message
560 :param func: callable object under test
561 :param args: variable positional arguments
562 """
563 parent = super(ParseFunctionCode, self)
564
565 # Pylint does not appreciate that the super method called
566 # conditionally can be available in other Python version
567 # then that of Pylint.
568 # Workaround is to call the method via getattr.
569 # Pylint ignores that the method got via getattr is
570 # conditionally executed. Method has to be a callable.
571 # Hence, using a dummy callable for getattr default.
572 dummy = lambda *x: None
573 # First Python 3 assertRaisesRegex is checked, since Python 2
574 # assertRaisesRegexp is also available in Python 3 but is
575 # marked deprecated.
576 for name in ('assertRaisesRegex', 'assertRaisesRegexp'):
577 method = getattr(parent, name, dummy)
578 if method is not dummy:
579 method(exp, regex, func, *args)
580 break
581 else:
582 raise AttributeError(" 'ParseFunctionCode' object has no attribute"
583 " 'assertRaisesRegex' or 'assertRaisesRegexp'"
584 )
585
Azim Khan4b543232017-06-30 09:35:21 +0100586 def test_no_function(self):
587 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100588 Test no test function found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100589 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100590 """
591 data = '''
592No
593test
594function
595'''
Azim Khanb31aa442018-07-03 11:57:54 +0100596 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100597 err_msg = 'file: test_suite_ut.function - Test functions not found!'
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100598 self.assert_raises_regex(GeneratorInputError, err_msg,
599 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100600
601 def test_no_end_case_comment(self):
602 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100603 Test missing end case.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100604 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100605 """
606 data = '''
607void test_func()
608{
609}
610'''
Azim Khanb31aa442018-07-03 11:57:54 +0100611 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100612 err_msg = r'file: test_suite_ut.function - '\
613 'end case pattern .*? not found!'
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100614 self.assert_raises_regex(GeneratorInputError, err_msg,
615 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100616
Azim Khanfcdf6852018-07-05 17:31:46 +0100617 @patch("generate_test_code.parse_function_arguments")
Azim Khanb31aa442018-07-03 11:57:54 +0100618 def test_function_called(self,
Azim Khanfcdf6852018-07-05 17:31:46 +0100619 parse_function_arguments_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100620 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100621 Test parse_function_code()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100622 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100623 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100624 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100625 data = '''
626void test_func()
627{
628}
629'''
Azim Khanb31aa442018-07-03 11:57:54 +0100630 stream = StringIOWrapper('test_suite_ut.function', data)
631 self.assertRaises(GeneratorInputError, parse_function_code,
632 stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100633 self.assertTrue(parse_function_arguments_mock.called)
634 parse_function_arguments_mock.assert_called_with('void test_func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100635
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000636 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100637 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000638 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100639 @patch("generate_test_code.parse_function_arguments")
640 def test_return(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100641 gen_function_wrapper_mock,
642 gen_dependencies_mock,
643 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100644 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100645 Test generated code.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100646 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100647 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100648 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100649 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100650 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100651 gen_dispatch_mock.side_effect = gen_dispatch
652 data = '''
653void func()
654{
655 ba ba black sheep
656 have you any wool
657}
658/* END_CASE */
659'''
Azim Khanb31aa442018-07-03 11:57:54 +0100660 stream = StringIOWrapper('test_suite_ut.function', data)
661 name, arg, code, dispatch_code = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100662
Azim Khanfcdf6852018-07-05 17:31:46 +0100663 self.assertTrue(parse_function_arguments_mock.called)
664 parse_function_arguments_mock.assert_called_with('void func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100665 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
666 self.assertEqual(name, 'test_func')
667 self.assertEqual(arg, [])
Azim Khan4084ec72018-07-05 14:20:08 +0100668 expected = '''#line 1 "test_suite_ut.function"
669
Azim Khan4b543232017-06-30 09:35:21 +0100670void test_func()
671{
672 ba ba black sheep
673 have you any wool
674exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100675 ;
Azim Khan4b543232017-06-30 09:35:21 +0100676}
677'''
678 self.assertEqual(code, expected)
679 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
680
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000681 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100682 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000683 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100684 @patch("generate_test_code.parse_function_arguments")
685 def test_with_exit_label(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100686 gen_function_wrapper_mock,
687 gen_dependencies_mock,
688 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100689 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100690 Test when exit label is present.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100691 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100692 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100693 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100694 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100695 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100696 gen_dispatch_mock.side_effect = gen_dispatch
697 data = '''
698void func()
699{
700 ba ba black sheep
701 have you any wool
702exit:
703 yes sir yes sir
704 3 bags full
705}
706/* END_CASE */
707'''
Azim Khanb31aa442018-07-03 11:57:54 +0100708 stream = StringIOWrapper('test_suite_ut.function', data)
709 _, _, code, _ = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100710
Azim Khan4084ec72018-07-05 14:20:08 +0100711 expected = '''#line 1 "test_suite_ut.function"
712
Azim Khan4b543232017-06-30 09:35:21 +0100713void test_func()
714{
715 ba ba black sheep
716 have you any wool
717exit:
718 yes sir yes sir
719 3 bags full
720}
721'''
722 self.assertEqual(code, expected)
723
Azim Khanfcdf6852018-07-05 17:31:46 +0100724 def test_non_void_function(self):
725 """
726 Test invalid signature (non void).
727 :return:
728 """
729 data = 'int entropy_threshold( char * a, data_t * h, int result )'
730 err_msg = 'file: test_suite_ut.function - Test functions not found!'
731 stream = StringIOWrapper('test_suite_ut.function', data)
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100732 self.assert_raises_regex(GeneratorInputError, err_msg,
733 parse_function_code, stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100734
735 @patch("generate_test_code.gen_dispatch")
736 @patch("generate_test_code.gen_dependencies")
737 @patch("generate_test_code.gen_function_wrapper")
738 @patch("generate_test_code.parse_function_arguments")
739 def test_functio_name_on_newline(self, parse_function_arguments_mock,
740 gen_function_wrapper_mock,
741 gen_dependencies_mock,
742 gen_dispatch_mock):
743 """
744 Test when exit label is present.
745 :return:
746 """
747 parse_function_arguments_mock.return_value = ([], '', [])
748 gen_function_wrapper_mock.return_value = ''
749 gen_dependencies_mock.side_effect = gen_dependencies
750 gen_dispatch_mock.side_effect = gen_dispatch
751 data = '''
752void
753
754
755func()
756{
757 ba ba black sheep
758 have you any wool
759exit:
760 yes sir yes sir
761 3 bags full
762}
763/* END_CASE */
764'''
765 stream = StringIOWrapper('test_suite_ut.function', data)
766 _, _, code, _ = parse_function_code(stream, [], [])
767
768 expected = '''#line 1 "test_suite_ut.function"
769
770void
771
772
773test_func()
774{
775 ba ba black sheep
776 have you any wool
777exit:
778 yes sir yes sir
779 3 bags full
780}
781'''
782 self.assertEqual(code, expected)
783
Azim Khan4b543232017-06-30 09:35:21 +0100784
785class ParseFunction(TestCase):
786 """
787 Test Suite for testing parse_functions()
788 """
789
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000790 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000791 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100792 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000793 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100794 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100795 """
Azim Khanb31aa442018-07-03 11:57:54 +0100796 def stop(*_unused):
797 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100798 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000799 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100800 data = '''/* BEGIN_HEADER */
801#include "mbedtls/ecp.h"
802
803#define ECP_PF_UNKNOWN -1
804/* END_HEADER */
805'''
Azim Khanb31aa442018-07-03 11:57:54 +0100806 stream = StringIOWrapper('test_suite_ut.function', data)
807 self.assertRaises(Exception, parse_functions, stream)
808 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100809 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000810
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000811 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000812 def test_begin_helper(self, parse_until_pattern_mock):
813 """
814 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100815 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000816 """
Azim Khanb31aa442018-07-03 11:57:54 +0100817 def stop(*_unused):
818 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000819 raise Exception
820 parse_until_pattern_mock.side_effect = stop
821 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100822void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000823{
Azim Khanb31aa442018-07-03 11:57:54 +0100824 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000825}
826/* END_SUITE_HELPERS */
827'''
Azim Khanb31aa442018-07-03 11:57:54 +0100828 stream = StringIOWrapper('test_suite_ut.function', data)
829 self.assertRaises(Exception, parse_functions, stream)
830 parse_until_pattern_mock.assert_called_with(stream,
831 END_SUITE_HELPERS_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100832 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100833
Azim Khanb31aa442018-07-03 11:57:54 +0100834 @patch("generate_test_code.parse_suite_dependencies")
835 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100836 """
Azim Khanb31aa442018-07-03 11:57:54 +0100837 Test that begin dep is checked and parse_suite_dependencies() is
838 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100839 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100840 """
Azim Khanb31aa442018-07-03 11:57:54 +0100841 def stop(*_unused):
842 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100843 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100844 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100845 data = '''/* BEGIN_DEPENDENCIES
846 * depends_on:MBEDTLS_ECP_C
847 * END_DEPENDENCIES
848 */
849'''
Azim Khanb31aa442018-07-03 11:57:54 +0100850 stream = StringIOWrapper('test_suite_ut.function', data)
851 self.assertRaises(Exception, parse_functions, stream)
852 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +0100853 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100854
Azim Khanb31aa442018-07-03 11:57:54 +0100855 @patch("generate_test_code.parse_function_dependencies")
856 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100857 """
Azim Khanb31aa442018-07-03 11:57:54 +0100858 Test that begin dep is checked and parse_function_dependencies() is
859 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100860 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100861 """
Azim Khanb31aa442018-07-03 11:57:54 +0100862 def stop(*_unused):
863 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100864 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100865 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100866
Azim Khanb31aa442018-07-03 11:57:54 +0100867 dependencies_str = '/* BEGIN_CASE ' \
868 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100869 data = '''%svoid test_func()
870{
871}
Azim Khanb31aa442018-07-03 11:57:54 +0100872''' % dependencies_str
873 stream = StringIOWrapper('test_suite_ut.function', data)
874 self.assertRaises(Exception, parse_functions, stream)
875 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +0100876 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100877
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000878 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +0100879 @patch("generate_test_code.parse_function_dependencies")
880 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +0100881 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000882 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100883 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100884 """
Azim Khanb31aa442018-07-03 11:57:54 +0100885 func_mock1.return_value = []
886 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +0100887{
888}
889'''
890 func_dispatch = '''
891 test_func_wrapper,
892'''
Azim Khanb31aa442018-07-03 11:57:54 +0100893 func_mock2.return_value = 'test_func', [],\
894 in_func_code, func_dispatch
895 dependencies_str = '/* BEGIN_CASE ' \
896 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100897 data = '''%svoid test_func()
898{
899}
Azim Khanb31aa442018-07-03 11:57:54 +0100900''' % dependencies_str
901 stream = StringIOWrapper('test_suite_ut.function', data)
902 suite_dependencies, dispatch_code, func_code, func_info = \
903 parse_functions(stream)
904 func_mock1.assert_called_with(dependencies_str)
905 func_mock2.assert_called_with(stream, [], [])
906 self.assertEqual(stream.line_no, 5)
907 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100908 expected_dispatch_code = '''/* Function Id: 0 */
909
910 test_func_wrapper,
911'''
912 self.assertEqual(dispatch_code, expected_dispatch_code)
913 self.assertEqual(func_code, in_func_code)
914 self.assertEqual(func_info, {'test_func': (0, [])})
915
916 def test_parsing(self):
917 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000918 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100919 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100920 """
921 data = '''/* BEGIN_HEADER */
922#include "mbedtls/ecp.h"
923
924#define ECP_PF_UNKNOWN -1
925/* END_HEADER */
926
927/* BEGIN_DEPENDENCIES
928 * depends_on:MBEDTLS_ECP_C
929 * END_DEPENDENCIES
930 */
931
932/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
933void func1()
934{
935}
936/* END_CASE */
937
938/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
939void func2()
940{
941}
942/* END_CASE */
943'''
Azim Khanb31aa442018-07-03 11:57:54 +0100944 stream = StringIOWrapper('test_suite_ut.function', data)
945 suite_dependencies, dispatch_code, func_code, func_info = \
946 parse_functions(stream)
947 self.assertEqual(stream.line_no, 23)
948 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +0100949
950 expected_dispatch_code = '''/* Function Id: 0 */
951
952#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
953 test_func1_wrapper,
954#else
955 NULL,
956#endif
957/* Function Id: 1 */
958
959#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
960 test_func2_wrapper,
961#else
962 NULL,
963#endif
964'''
965 self.assertEqual(dispatch_code, expected_dispatch_code)
966 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +0100967#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100968#include "mbedtls/ecp.h"
969
970#define ECP_PF_UNKNOWN -1
971#if defined(MBEDTLS_ENTROPY_NV_SEED)
972#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +0100973#line 13 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100974void test_func1()
975{
976exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100977 ;
Azim Khan4b543232017-06-30 09:35:21 +0100978}
979
980void test_func1_wrapper( void ** params )
981{
982 (void)params;
983
984 test_func1( );
985}
986#endif /* MBEDTLS_FS_IO */
987#endif /* MBEDTLS_ENTROPY_NV_SEED */
988#if defined(MBEDTLS_ENTROPY_NV_SEED)
989#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +0100990#line 19 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100991void test_func2()
992{
993exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100994 ;
Azim Khan4b543232017-06-30 09:35:21 +0100995}
996
997void test_func2_wrapper( void ** params )
998{
999 (void)params;
1000
1001 test_func2( );
1002}
1003#endif /* MBEDTLS_FS_IO */
1004#endif /* MBEDTLS_ENTROPY_NV_SEED */
1005#endif /* MBEDTLS_ECP_C */
1006'''
1007 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001008 self.assertEqual(func_info, {'test_func1': (0, []),
1009 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +01001010
1011 def test_same_function_name(self):
1012 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001013 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001014 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001015 """
1016 data = '''/* BEGIN_HEADER */
1017#include "mbedtls/ecp.h"
1018
1019#define ECP_PF_UNKNOWN -1
1020/* END_HEADER */
1021
1022/* BEGIN_DEPENDENCIES
1023 * depends_on:MBEDTLS_ECP_C
1024 * END_DEPENDENCIES
1025 */
1026
1027/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1028void func()
1029{
1030}
1031/* END_CASE */
1032
1033/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1034void func()
1035{
1036}
1037/* END_CASE */
1038'''
Azim Khanb31aa442018-07-03 11:57:54 +01001039 stream = StringIOWrapper('test_suite_ut.function', data)
1040 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +01001041
1042
Azim Khanb31aa442018-07-03 11:57:54 +01001043class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001044 """
Azim Khan599cd242017-07-06 17:34:27 +01001045 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +01001046 Note: Since escaped_split() output is used to write back to the
1047 intermediate data file. Any escape characters in the input are
1048 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001049 """
1050
1051 def test_invalid_input(self):
1052 """
1053 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001054 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001055 """
1056 self.assertRaises(ValueError, escaped_split, '', 'string')
1057
1058 def test_empty_string(self):
1059 """
Azim Khanb31aa442018-07-03 11:57:54 +01001060 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001061 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001062 """
1063 splits = escaped_split('', ':')
1064 self.assertEqual(splits, [])
1065
1066 def test_no_escape(self):
1067 """
Azim Khanb31aa442018-07-03 11:57:54 +01001068 Test with no escape character. The behaviour should be same as
1069 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001070 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001071 """
Azim Khanb31aa442018-07-03 11:57:54 +01001072 test_str = 'yahoo:google'
1073 splits = escaped_split(test_str, ':')
1074 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001075
1076 def test_escaped_input(self):
1077 """
Azim Khanb31aa442018-07-03 11:57:54 +01001078 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001079 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001080 """
Azim Khanb31aa442018-07-03 11:57:54 +01001081 test_str = r'yahoo\:google:facebook'
1082 splits = escaped_split(test_str, ':')
1083 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001084
1085 def test_escaped_escape(self):
1086 """
Azim Khanb31aa442018-07-03 11:57:54 +01001087 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001088 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001089 """
Azim Khan4084ec72018-07-05 14:20:08 +01001090 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +01001091 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001092 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001093
1094 def test_all_at_once(self):
1095 """
Azim Khanb31aa442018-07-03 11:57:54 +01001096 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001097 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001098 """
Azim Khan4084ec72018-07-05 14:20:08 +01001099 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +01001100 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001101 self.assertEqual(splits, [r'yahoo\\', r'google',
1102 r'facebook\:instagram\\',
1103 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001104
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001105
1106class ParseTestData(TestCase):
1107 """
1108 Test suite for parse test data.
1109 """
1110
1111 def test_parser(self):
1112 """
1113 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001114 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001115 """
1116 data = """
1117Diffie-Hellman full exchange #1
1118dhm_do_dhm:10:"23":10:"5"
1119
1120Diffie-Hellman full exchange #2
1121dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1122
1123Diffie-Hellman full exchange #3
1124dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1125
1126Diffie-Hellman selftest
1127dhm_selftest:
1128"""
Azim Khanb31aa442018-07-03 11:57:54 +01001129 stream = StringIOWrapper('test_suite_ut.function', data)
1130 tests = [(name, test_function, dependencies, args)
1131 for name, test_function, dependencies, args in
1132 parse_test_data(stream)]
1133 test1, test2, test3, test4 = tests
1134 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1135 self.assertEqual(test1[1], 'dhm_do_dhm')
1136 self.assertEqual(test1[2], [])
1137 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001138
Azim Khanb31aa442018-07-03 11:57:54 +01001139 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1140 self.assertEqual(test2[1], 'dhm_do_dhm')
1141 self.assertEqual(test2[2], [])
1142 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1143 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001144
Azim Khanb31aa442018-07-03 11:57:54 +01001145 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1146 self.assertEqual(test3[1], 'dhm_do_dhm')
1147 self.assertEqual(test3[2], [])
1148 self.assertEqual(test3[3], ['10',
1149 '"9345098382739712938719287391879381271"',
1150 '10',
1151 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001152
Azim Khanb31aa442018-07-03 11:57:54 +01001153 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1154 self.assertEqual(test4[1], 'dhm_selftest')
1155 self.assertEqual(test4[2], [])
1156 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001157
1158 def test_with_dependencies(self):
1159 """
1160 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001161 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001162 """
1163 data = """
1164Diffie-Hellman full exchange #1
1165depends_on:YAHOO
1166dhm_do_dhm:10:"23":10:"5"
1167
1168Diffie-Hellman full exchange #2
1169dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1170
1171"""
Azim Khanb31aa442018-07-03 11:57:54 +01001172 stream = StringIOWrapper('test_suite_ut.function', data)
1173 tests = [(name, function_name, dependencies, args)
1174 for name, function_name, dependencies, args in
1175 parse_test_data(stream)]
1176 test1, test2 = tests
1177 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1178 self.assertEqual(test1[1], 'dhm_do_dhm')
1179 self.assertEqual(test1[2], ['YAHOO'])
1180 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001181
Azim Khanb31aa442018-07-03 11:57:54 +01001182 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1183 self.assertEqual(test2[1], 'dhm_do_dhm')
1184 self.assertEqual(test2[2], [])
1185 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1186 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001187
1188 def test_no_args(self):
1189 """
Azim Khanb31aa442018-07-03 11:57:54 +01001190 Test GeneratorInputError is raised when test function name and
1191 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001192 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001193 """
1194 data = """
1195Diffie-Hellman full exchange #1
1196depends_on:YAHOO
1197
1198
1199Diffie-Hellman full exchange #2
1200dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1201
1202"""
Azim Khanb31aa442018-07-03 11:57:54 +01001203 stream = StringIOWrapper('test_suite_ut.function', data)
1204 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001205 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001206 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001207 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001208 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001209 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001210
1211 def test_incomplete_data(self):
1212 """
Azim Khanb31aa442018-07-03 11:57:54 +01001213 Test GeneratorInputError is raised when test function name
1214 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001215 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001216 """
1217 data = """
1218Diffie-Hellman full exchange #1
1219depends_on:YAHOO
1220"""
Azim Khanb31aa442018-07-03 11:57:54 +01001221 stream = StringIOWrapper('test_suite_ut.function', data)
1222 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001223 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001224 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001225 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001226 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001227 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001228
1229
1230class GenDepCheck(TestCase):
1231 """
Azim Khanb31aa442018-07-03 11:57:54 +01001232 Test suite for gen_dep_check(). It is assumed this function is
1233 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001234 """
1235
1236 def test_gen_dep_check(self):
1237 """
1238 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001239 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001240 """
1241 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001242 case 5:
1243 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001244#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001245 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001246#else
Azim Khand61b8372017-07-10 11:54:01 +01001247 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001248#endif
Azim Khand61b8372017-07-10 11:54:01 +01001249 }
1250 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001251 out = gen_dep_check(5, 'YAHOO')
1252 self.assertEqual(out, expected)
1253
Azim Khanb31aa442018-07-03 11:57:54 +01001254 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001255 """
1256 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001257 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001258 """
1259 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001260 case 5:
1261 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001262#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001263 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001264#else
Azim Khand61b8372017-07-10 11:54:01 +01001265 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001266#endif
Azim Khand61b8372017-07-10 11:54:01 +01001267 }
1268 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001269 out = gen_dep_check(5, '!YAHOO')
1270 self.assertEqual(out, expected)
1271
1272 def test_empty_dependency(self):
1273 """
1274 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001275 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001276 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001277 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001278
1279 def test_negative_dep_id(self):
1280 """
1281 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001282 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001283 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001284 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001285
1286
1287class GenExpCheck(TestCase):
1288 """
Azim Khanb31aa442018-07-03 11:57:54 +01001289 Test suite for gen_expression_check(). It is assumed this function
1290 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001291 """
1292
1293 def test_gen_exp_check(self):
1294 """
1295 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001296 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001297 """
1298 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001299 case 5:
1300 {
1301 *out_value = YAHOO;
1302 }
1303 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001304 out = gen_expression_check(5, 'YAHOO')
1305 self.assertEqual(out, expected)
1306
1307 def test_invalid_expression(self):
1308 """
1309 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001310 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001311 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001312 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001313
1314 def test_negative_exp_id(self):
1315 """
1316 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001317 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001318 """
Azim Khanb31aa442018-07-03 11:57:54 +01001319 self.assertRaises(GeneratorInputError, gen_expression_check,
1320 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001321
1322
Azim Khanb31aa442018-07-03 11:57:54 +01001323class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001324 """
Azim Khanb31aa442018-07-03 11:57:54 +01001325 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001326 """
1327
Azim Khanb31aa442018-07-03 11:57:54 +01001328 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001329 """
Azim Khanb31aa442018-07-03 11:57:54 +01001330 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001331 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001332 """
Azim Khanb31aa442018-07-03 11:57:54 +01001333 stream = StringIOWrapper('test_suite_ut.data', '')
1334 unique_dependencies = []
1335 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001336 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001337 self.assertEqual(len(unique_dependencies), 0)
1338 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001339
1340 def test_unique_dep_ids(self):
1341 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001342
1343 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001344 """
Azim Khanb31aa442018-07-03 11:57:54 +01001345 stream = StringIOWrapper('test_suite_ut.data', '')
1346 unique_dependencies = []
1347 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1348 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001349 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001350 case 0:
1351 {
Azim Khan599cd242017-07-06 17:34:27 +01001352#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001353 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001354#else
Azim Khand61b8372017-07-10 11:54:01 +01001355 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001356#endif
Azim Khand61b8372017-07-10 11:54:01 +01001357 }
1358 break;
1359 case 1:
1360 {
Azim Khan599cd242017-07-06 17:34:27 +01001361#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001362 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001363#else
Azim Khand61b8372017-07-10 11:54:01 +01001364 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001365#endif
Azim Khand61b8372017-07-10 11:54:01 +01001366 }
1367 break;
1368 case 2:
1369 {
Azim Khan599cd242017-07-06 17:34:27 +01001370#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001371 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001372#else
Azim Khand61b8372017-07-10 11:54:01 +01001373 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001374#endif
Azim Khand61b8372017-07-10 11:54:01 +01001375 }
1376 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001377 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001378 self.assertEqual(len(unique_dependencies), 3)
1379 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001380
1381 def test_dep_id_repeat(self):
1382 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001383
1384 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001385 """
Azim Khanb31aa442018-07-03 11:57:54 +01001386 stream = StringIOWrapper('test_suite_ut.data', '')
1387 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001388 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001389 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1390 unique_dependencies)
1391 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1392 unique_dependencies)
1393 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1394 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001395 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001396 case 0:
1397 {
Azim Khan599cd242017-07-06 17:34:27 +01001398#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001399 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001400#else
Azim Khand61b8372017-07-10 11:54:01 +01001401 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001402#endif
Azim Khand61b8372017-07-10 11:54:01 +01001403 }
1404 break;
1405 case 1:
1406 {
Azim Khan599cd242017-07-06 17:34:27 +01001407#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001408 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001409#else
Azim Khand61b8372017-07-10 11:54:01 +01001410 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001411#endif
Azim Khand61b8372017-07-10 11:54:01 +01001412 }
1413 break;
1414 case 2:
1415 {
Azim Khan599cd242017-07-06 17:34:27 +01001416#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001417 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001418#else
Azim Khand61b8372017-07-10 11:54:01 +01001419 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001420#endif
Azim Khand61b8372017-07-10 11:54:01 +01001421 }
1422 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001423 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001424 self.assertEqual(len(unique_dependencies), 3)
1425 self.assertEqual(stream.getvalue(),
1426 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001427
1428
1429class WriteParams(TestCase):
1430 """
1431 Test Suite for testing write_parameters().
1432 """
1433
1434 def test_no_params(self):
1435 """
1436 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001437 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001438 """
Azim Khanb31aa442018-07-03 11:57:54 +01001439 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001440 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001441 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001442 self.assertEqual(len(unique_expressions), 0)
1443 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001444 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001445
1446 def test_no_exp_param(self):
1447 """
1448 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001449 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001450 """
Azim Khanb31aa442018-07-03 11:57:54 +01001451 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001452 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001453 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1454 '0'],
1455 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001456 unique_expressions)
1457 self.assertEqual(len(unique_expressions), 0)
1458 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001459 self.assertEqual(stream.getvalue(),
1460 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001461
1462 def test_hex_format_int_param(self):
1463 """
1464 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001465 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001466 """
Azim Khanb31aa442018-07-03 11:57:54 +01001467 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001468 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001469 expression_code = write_parameters(stream,
1470 ['"Yahoo"', '"abcdef00"', '0xAA'],
1471 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001472 unique_expressions)
1473 self.assertEqual(len(unique_expressions), 0)
1474 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001475 self.assertEqual(stream.getvalue(),
1476 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001477
1478 def test_with_exp_param(self):
1479 """
1480 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001481 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001482 """
Azim Khanb31aa442018-07-03 11:57:54 +01001483 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001484 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001485 expression_code = write_parameters(stream,
1486 ['"Yahoo"', '"abcdef00"', '0',
1487 'MACRO1', 'MACRO2', 'MACRO3'],
1488 ['char*', 'hex', 'int',
1489 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001490 unique_expressions)
1491 self.assertEqual(len(unique_expressions), 3)
1492 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1493 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001494 case 0:
1495 {
1496 *out_value = MACRO1;
1497 }
1498 break;
1499 case 1:
1500 {
1501 *out_value = MACRO2;
1502 }
1503 break;
1504 case 2:
1505 {
1506 *out_value = MACRO3;
1507 }
1508 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001509 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001510 self.assertEqual(stream.getvalue(),
1511 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1512 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001513
Azim Khanb31aa442018-07-03 11:57:54 +01001514 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001515 """
1516 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001517 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001518 """
Azim Khanb31aa442018-07-03 11:57:54 +01001519 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001520 unique_expressions = []
1521 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001522 expression_code += write_parameters(stream,
1523 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1524 ['char*', '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 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1528 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001529 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001530 expression_code += write_parameters(stream,
1531 ['0', 'MACRO3', 'MACRO1'],
1532 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001533 unique_expressions)
1534 self.assertEqual(len(unique_expressions), 3)
1535 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1536 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001537 case 0:
1538 {
1539 *out_value = MACRO1;
1540 }
1541 break;
1542 case 1:
1543 {
1544 *out_value = MACRO2;
1545 }
1546 break;
1547 case 2:
1548 {
1549 *out_value = MACRO3;
1550 }
1551 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001552 self.assertEqual(expression_code, expected_expression_code)
1553 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1554:hex:"abcdef00":exp:1:exp:2
1555:int:0:exp:2:exp:0
1556'''
Azim Khanb31aa442018-07-03 11:57:54 +01001557 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001558
1559
Azim Khanb31aa442018-07-03 11:57:54 +01001560class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001561 """
Azim Khanb31aa442018-07-03 11:57:54 +01001562 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001563 """
Azim Khanb31aa442018-07-03 11:57:54 +01001564 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001565 """
Azim Khanb31aa442018-07-03 11:57:54 +01001566 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001567
1568 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001569 """
Azim Khanb31aa442018-07-03 11:57:54 +01001570 dep_check_code, expression_code = \
1571 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001572 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1573 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1574
Azim Khanb31aa442018-07-03 11:57:54 +01001575 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001576 """
Azim Khanb31aa442018-07-03 11:57:54 +01001577 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001578
1579 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001580 """
Azim Khanb31aa442018-07-03 11:57:54 +01001581 dep_check_code, expression_code = \
1582 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1583 'EXPRESSION_CODE')
1584 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001585#if defined(SUITE_DEP)
1586DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001587#endif
1588'''
1589 expected_expression_code = '''
1590#if defined(SUITE_DEP)
1591EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001592#endif
1593'''
Azim Khanb31aa442018-07-03 11:57:54 +01001594 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001595 self.assertEqual(expression_code, expected_expression_code)
1596
1597 def test_no_dep_no_exp(self):
1598 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001599 Test when there are no dependency and expression code.
1600 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001601 """
Azim Khanb31aa442018-07-03 11:57:54 +01001602 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001603 self.assertEqual(dep_check_code, '')
1604 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001605
1606
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001607class GenFromTestData(TestCase):
1608 """
1609 Test suite for gen_from_test_data()
1610 """
1611
Azim Khanb31aa442018-07-03 11:57:54 +01001612 @staticmethod
1613 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001614 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001615 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001616 def test_intermediate_data_file(func_mock1,
1617 write_parameters_mock,
1618 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001619 """
1620 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001621 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001622 """
1623 data = '''
1624My test
1625depends_on:DEP1
1626func1:0
1627'''
1628 data_f = StringIOWrapper('test_suite_ut.data', data)
1629 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1630 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001631 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001632 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001633 write_dependencies_mock.side_effect = write_dependencies
1634 func_mock1.side_effect = gen_suite_dep_checks
1635 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1636 write_dependencies_mock.assert_called_with(out_data_f,
1637 ['DEP1'], ['DEP1'])
1638 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1639 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001640 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001641 case 0:
1642 {
Azim Khan599cd242017-07-06 17:34:27 +01001643#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001644 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001645#else
Azim Khand61b8372017-07-10 11:54:01 +01001646 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001647#endif
Azim Khand61b8372017-07-10 11:54:01 +01001648 }
1649 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001650 func_mock1.assert_called_with(
1651 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001652
1653 def test_function_not_found(self):
1654 """
1655 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001656 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001657 """
1658 data = '''
1659My test
1660depends_on:DEP1
1661func1:0
1662'''
1663 data_f = StringIOWrapper('test_suite_ut.data', data)
1664 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1665 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001666 suite_dependencies = []
1667 self.assertRaises(GeneratorInputError, gen_from_test_data,
1668 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001669
1670 def test_different_func_args(self):
1671 """
Azim Khanb31aa442018-07-03 11:57:54 +01001672 Test that AssertError is raised when no. of parameters and
1673 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001674 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001675 """
1676 data = '''
1677My test
1678depends_on:DEP1
1679func1:0
1680'''
1681 data_f = StringIOWrapper('test_suite_ut.data', data)
1682 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001683 func_info = {'test_func2': (1, ('int', 'hex'))}
1684 suite_dependencies = []
1685 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1686 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001687
1688 def test_output(self):
1689 """
1690 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001691 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001692 """
1693 data = '''
1694My test 1
1695depends_on:DEP1
1696func1:0:0xfa:MACRO1:MACRO2
1697
1698My test 2
1699depends_on:DEP1:DEP2
1700func2:"yahoo":88:MACRO1
1701'''
1702 data_f = StringIOWrapper('test_suite_ut.data', data)
1703 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001704 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1705 'test_func2': (1, ('char*', 'int', 'int'))}
1706 suite_dependencies = []
1707 dep_check_code, expression_code = \
1708 gen_from_test_data(data_f, out_data_f, func_info,
1709 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001710 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001711 case 0:
1712 {
Azim Khan599cd242017-07-06 17:34:27 +01001713#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001714 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001715#else
Azim Khand61b8372017-07-10 11:54:01 +01001716 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001717#endif
Azim Khand61b8372017-07-10 11:54:01 +01001718 }
1719 break;
1720 case 1:
1721 {
Azim Khan599cd242017-07-06 17:34:27 +01001722#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001723 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001724#else
Azim Khand61b8372017-07-10 11:54:01 +01001725 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001726#endif
Azim Khand61b8372017-07-10 11:54:01 +01001727 }
1728 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001729 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001730depends_on:0
17310:int:0:int:0xfa:exp:0:exp:1
1732
1733My test 2
1734depends_on:0:1
17351:char*:"yahoo":int:88:exp:0
1736
1737'''
1738 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001739 case 0:
1740 {
1741 *out_value = MACRO1;
1742 }
1743 break;
1744 case 1:
1745 {
1746 *out_value = MACRO2;
1747 }
1748 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001749 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001750 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001751 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001752
1753
Azim Khanb31aa442018-07-03 11:57:54 +01001754if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001755 unittest_main()