blob: c8e8c5ce1417fb8530624e50fbe12abe28e1b867 [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 Peskineafd19dd2019-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 Peskineafd19dd2019-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
Gilles Peskine5d1dfd42020-03-24 18:25:17 +0100297class StringIOWrapper(StringIO):
Azim Khan4b543232017-06-30 09:35:21 +0100298 """
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 Peskineafd19dd2019-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)
Gilles Peskine399b82f2020-03-24 18:36:56 +01001130 # List of (name, function_name, dependencies, args)
1131 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001132 test1, test2, test3, test4 = tests
1133 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1134 self.assertEqual(test1[1], 'dhm_do_dhm')
1135 self.assertEqual(test1[2], [])
1136 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001137
Azim Khanb31aa442018-07-03 11:57:54 +01001138 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1139 self.assertEqual(test2[1], 'dhm_do_dhm')
1140 self.assertEqual(test2[2], [])
1141 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1142 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001143
Azim Khanb31aa442018-07-03 11:57:54 +01001144 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1145 self.assertEqual(test3[1], 'dhm_do_dhm')
1146 self.assertEqual(test3[2], [])
1147 self.assertEqual(test3[3], ['10',
1148 '"9345098382739712938719287391879381271"',
1149 '10',
1150 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001151
Azim Khanb31aa442018-07-03 11:57:54 +01001152 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1153 self.assertEqual(test4[1], 'dhm_selftest')
1154 self.assertEqual(test4[2], [])
1155 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001156
1157 def test_with_dependencies(self):
1158 """
1159 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001160 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001161 """
1162 data = """
1163Diffie-Hellman full exchange #1
1164depends_on:YAHOO
1165dhm_do_dhm:10:"23":10:"5"
1166
1167Diffie-Hellman full exchange #2
1168dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1169
1170"""
Azim Khanb31aa442018-07-03 11:57:54 +01001171 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine399b82f2020-03-24 18:36:56 +01001172 # List of (name, function_name, dependencies, args)
1173 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001174 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()