blob: ae1aa2af443b0b76e1ee944c243f6aea20a720dc [file] [log] [blame]
Azim Khanb31aa442018-07-03 11:57:54 +01001#!/usr/bin/env python3
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00002# Unit test for generate_test_code.py
Azim Khanf0e42fb2017-08-02 14:47:13 +01003#
Bence Szépkúti1e148272020-08-07 13:07:28 +02004# Copyright The Mbed TLS Contributors
Azim Khanf0e42fb2017-08-02 14:47:13 +01005# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
Azim Khan4b543232017-06-30 09:35:21 +010018
19"""
Mohammad Azim Khan78befd92018-03-06 11:49:41 +000020Unit tests for generate_test_code.py
Azim Khan4b543232017-06-30 09:35:21 +010021"""
22
Gilles Peskine38b66df2020-08-12 02:11:38 +020023from io import StringIO
Azim Khanb31aa442018-07-03 11:57:54 +010024from unittest import TestCase, main as unittest_main
Gilles Peskine38b66df2020-08-12 02:11:38 +020025from unittest.mock import patch
26
Azim Khanb31aa442018-07-03 11:57:54 +010027from generate_test_code import gen_dependencies, gen_dependencies_one_line
28from generate_test_code import gen_function_wrapper, gen_dispatch
29from generate_test_code import parse_until_pattern, GeneratorInputError
30from generate_test_code import parse_suite_dependencies
31from generate_test_code import parse_function_dependencies
Azim Khanfcdf6852018-07-05 17:31:46 +010032from generate_test_code import parse_function_arguments, parse_function_code
Azim Khanb31aa442018-07-03 11:57:54 +010033from generate_test_code import parse_functions, END_HEADER_REGEX
34from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
35from generate_test_code import parse_test_data, gen_dep_check
36from generate_test_code import gen_expression_check, write_dependencies
37from generate_test_code import write_parameters, gen_suite_dep_checks
38from generate_test_code import gen_from_test_data
39
40
Azim Khan4b543232017-06-30 09:35:21 +010041class GenDep(TestCase):
42 """
43 Test suite for function gen_dep()
44 """
45
Azim Khanb31aa442018-07-03 11:57:54 +010046 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010047 """
Azim Khanb31aa442018-07-03 11:57:54 +010048 Test that gen_dep() correctly creates dependencies for given
49 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010050 :return:
Azim Khan4b543232017-06-30 09:35:21 +010051 """
Azim Khanb31aa442018-07-03 11:57:54 +010052 dependencies = ['DEP1', 'DEP2']
53 dep_start, dep_end = gen_dependencies(dependencies)
54 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010055 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010056 self.assertEqual(preprocessor1, '#if defined(DEP1)',
57 'Preprocessor generated incorrectly')
58 self.assertEqual(preprocessor2, '#if defined(DEP2)',
59 'Preprocessor generated incorrectly')
60 self.assertEqual(endif1, '#endif /* DEP2 */',
61 'Preprocessor generated incorrectly')
62 self.assertEqual(endif2, '#endif /* DEP1 */',
63 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010064
Azim Khanb31aa442018-07-03 11:57:54 +010065 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010066 """
Azim Khanb31aa442018-07-03 11:57:54 +010067 Test that gen_dep() correctly creates dependencies for given
68 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010069 :return:
Azim Khan4b543232017-06-30 09:35:21 +010070 """
Azim Khanb31aa442018-07-03 11:57:54 +010071 dependencies = ['!DEP1', '!DEP2']
72 dep_start, dep_end = gen_dependencies(dependencies)
73 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010074 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010075 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
76 'Preprocessor generated incorrectly')
77 self.assertEqual(preprocessor2, '#if !defined(DEP2)',
78 'Preprocessor generated incorrectly')
79 self.assertEqual(endif1, '#endif /* !DEP2 */',
80 'Preprocessor generated incorrectly')
81 self.assertEqual(endif2, '#endif /* !DEP1 */',
82 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010083
Azim Khanb31aa442018-07-03 11:57:54 +010084 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010085 """
Azim Khanb31aa442018-07-03 11:57:54 +010086 Test that gen_dep() correctly creates dependencies for given
87 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010088 :return:
Azim Khan4b543232017-06-30 09:35:21 +010089 """
Azim Khanb31aa442018-07-03 11:57:54 +010090 dependencies = ['!DEP1', 'DEP2']
91 dep_start, dep_end = gen_dependencies(dependencies)
92 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010093 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010094 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
95 'Preprocessor generated incorrectly')
96 self.assertEqual(preprocessor2, '#if defined(DEP2)',
97 'Preprocessor generated incorrectly')
98 self.assertEqual(endif1, '#endif /* DEP2 */',
99 'Preprocessor generated incorrectly')
100 self.assertEqual(endif2, '#endif /* !DEP1 */',
101 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100102
Azim Khanb31aa442018-07-03 11:57:54 +0100103 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100104 """
Azim Khanb31aa442018-07-03 11:57:54 +0100105 Test that gen_dep() correctly creates dependencies for given
106 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100107 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100108 """
Azim Khanb31aa442018-07-03 11:57:54 +0100109 dependencies = []
110 dep_start, dep_end = gen_dependencies(dependencies)
111 self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
112 self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100113
Azim Khanb31aa442018-07-03 11:57:54 +0100114 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100115 """
Azim Khanb31aa442018-07-03 11:57:54 +0100116 Test that gen_dep() correctly creates dependencies for given
117 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100118 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100119 """
Azim Khanb31aa442018-07-03 11:57:54 +0100120 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100121 count = 10
122 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100123 dependencies.append('DEP%d' % i)
124 dep_start, dep_end = gen_dependencies(dependencies)
125 self.assertEqual(len(dep_start.splitlines()), count,
126 'Preprocessor generated incorrectly')
127 self.assertEqual(len(dep_end.splitlines()), count,
128 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100129
130
131class GenDepOneLine(TestCase):
132 """
Azim Khanb31aa442018-07-03 11:57:54 +0100133 Test Suite for testing gen_dependencies_one_line()
Azim Khan4b543232017-06-30 09:35:21 +0100134 """
135
Azim Khanb31aa442018-07-03 11:57:54 +0100136 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100137 """
Azim Khanb31aa442018-07-03 11:57:54 +0100138 Test that gen_dep() correctly creates dependencies for given
139 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100140 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100141 """
Azim Khanb31aa442018-07-03 11:57:54 +0100142 dependencies = ['DEP1', 'DEP2']
143 dep_str = gen_dependencies_one_line(dependencies)
144 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
145 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100146
Azim Khanb31aa442018-07-03 11:57:54 +0100147 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100148 """
Azim Khanb31aa442018-07-03 11:57:54 +0100149 Test that gen_dep() correctly creates dependencies for given
150 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100151 :return:
152 """
Azim Khanb31aa442018-07-03 11:57:54 +0100153 dependencies = ['!DEP1', '!DEP2']
154 dep_str = gen_dependencies_one_line(dependencies)
155 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
156 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100157
Azim Khanb31aa442018-07-03 11:57:54 +0100158 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100159 """
Azim Khanb31aa442018-07-03 11:57:54 +0100160 Test that gen_dep() correctly creates dependencies for given
161 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100162 :return:
163 """
Azim Khanb31aa442018-07-03 11:57:54 +0100164 dependencies = ['!DEP1', 'DEP2']
165 dep_str = gen_dependencies_one_line(dependencies)
166 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
167 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100168
Azim Khanb31aa442018-07-03 11:57:54 +0100169 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100170 """
Azim Khanb31aa442018-07-03 11:57:54 +0100171 Test that gen_dep() correctly creates dependencies for given
172 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100173 :return:
174 """
Azim Khanb31aa442018-07-03 11:57:54 +0100175 dependencies = []
176 dep_str = gen_dependencies_one_line(dependencies)
177 self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100178
Azim Khanb31aa442018-07-03 11:57:54 +0100179 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100180 """
Azim Khanb31aa442018-07-03 11:57:54 +0100181 Test that gen_dep() correctly creates dependencies for given
182 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100183 :return:
184 """
Azim Khanb31aa442018-07-03 11:57:54 +0100185 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100186 count = 10
187 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100188 dependencies.append('DEP%d' % i)
189 dep_str = gen_dependencies_one_line(dependencies)
190 expected = '#if ' + ' && '.join(['defined(%s)' %
191 x for x in dependencies])
192 self.assertEqual(dep_str, expected,
193 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100194
195
196class GenFunctionWrapper(TestCase):
197 """
198 Test Suite for testing gen_function_wrapper()
199 """
200
201 def test_params_unpack(self):
202 """
203 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100204
205 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100206 """
207 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
208 expected = '''
209void test_a_wrapper( void ** params )
210{
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100211
Azim Khan4b543232017-06-30 09:35:21 +0100212 test_a( a, b, c, d );
213}
214'''
215 self.assertEqual(code, expected)
216
217 def test_local(self):
218 """
219 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100220
221 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100222 """
Azim Khanb31aa442018-07-03 11:57:54 +0100223 code = gen_function_wrapper('test_a',
224 'int x = 1;', ('x', 'b', 'c', 'd'))
Azim Khan4b543232017-06-30 09:35:21 +0100225 expected = '''
226void test_a_wrapper( void ** params )
227{
Azim Khan4b543232017-06-30 09:35:21 +0100228int x = 1;
229 test_a( x, b, c, d );
230}
231'''
232 self.assertEqual(code, expected)
233
234 def test_empty_params(self):
235 """
236 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100237
238 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100239 """
240 code = gen_function_wrapper('test_a', '', ())
241 expected = '''
242void test_a_wrapper( void ** params )
243{
244 (void)params;
245
246 test_a( );
247}
248'''
249 self.assertEqual(code, expected)
250
251
252class GenDispatch(TestCase):
253 """
254 Test suite for testing gen_dispatch()
255 """
256
257 def test_dispatch(self):
258 """
259 Test that dispatch table entry is generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100260 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100261 """
262 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
263 expected = '''
264#if defined(DEP1) && defined(DEP2)
265 test_a_wrapper,
266#else
267 NULL,
268#endif
269'''
270 self.assertEqual(code, expected)
271
Azim Khanb31aa442018-07-03 11:57:54 +0100272 def test_empty_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100273 """
274 Test empty dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100275 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100276 """
277 code = gen_dispatch('test_a', [])
278 expected = '''
279 test_a_wrapper,
280'''
281 self.assertEqual(code, expected)
282
283
Gilles Peskine184c0962020-03-24 18:25:17 +0100284class StringIOWrapper(StringIO):
Azim Khan4b543232017-06-30 09:35:21 +0100285 """
286 file like class to mock file object in tests.
287 """
Azim Khan4084ec72018-07-05 14:20:08 +0100288 def __init__(self, file_name, data, line_no=0):
Azim Khan4b543232017-06-30 09:35:21 +0100289 """
290 Init file handle.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100291
292 :param file_name:
Azim Khan4b543232017-06-30 09:35:21 +0100293 :param data:
294 :param line_no:
295 """
296 super(StringIOWrapper, self).__init__(data)
297 self.line_no = line_no
298 self.name = file_name
299
300 def next(self):
301 """
Azim Khanb31aa442018-07-03 11:57:54 +0100302 Iterator method. This method overrides base class's
303 next method and extends the next method to count the line
304 numbers as each line is read.
Azim Khan4b543232017-06-30 09:35:21 +0100305
Azim Khanb31aa442018-07-03 11:57:54 +0100306 :return: Line read from file.
307 """
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100308 parent = super(StringIOWrapper, self)
Gilles Peskine38b66df2020-08-12 02:11:38 +0200309 line = parent.__next__()
Azim Khan4084ec72018-07-05 14:20:08 +0100310 return line
Azim Khanb31aa442018-07-03 11:57:54 +0100311
Gilles Peskine38b66df2020-08-12 02:11:38 +0200312 def readline(self, _length=0):
Azim Khan4b543232017-06-30 09:35:21 +0100313 """
314 Wrap the base class readline.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100315
Azim Khanb31aa442018-07-03 11:57:54 +0100316 :param length:
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100317 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100318 """
319 line = super(StringIOWrapper, self).readline()
Azim Khan4084ec72018-07-05 14:20:08 +0100320 if line is not None:
Azim Khan4b543232017-06-30 09:35:21 +0100321 self.line_no += 1
322 return line
323
324
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000325class ParseUntilPattern(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100326 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000327 Test Suite for testing parse_until_pattern().
Azim Khan4b543232017-06-30 09:35:21 +0100328 """
329
330 def test_suite_headers(self):
331 """
332 Test that suite headers are parsed correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100333
334 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100335 """
336 data = '''#include "mbedtls/ecp.h"
337
338#define ECP_PF_UNKNOWN -1
339/* END_HEADER */
340'''
341 expected = '''#line 1 "test_suite_ut.function"
342#include "mbedtls/ecp.h"
343
344#define ECP_PF_UNKNOWN -1
345'''
Azim Khanb31aa442018-07-03 11:57:54 +0100346 stream = StringIOWrapper('test_suite_ut.function', data, line_no=0)
347 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100348 self.assertEqual(headers, expected)
349
350 def test_line_no(self):
351 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100352 Test that #line is set to correct line no. in source .function file.
353
354 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100355 """
356 data = '''#include "mbedtls/ecp.h"
357
358#define ECP_PF_UNKNOWN -1
359/* END_HEADER */
360'''
361 offset_line_no = 5
362 expected = '''#line %d "test_suite_ut.function"
363#include "mbedtls/ecp.h"
364
365#define ECP_PF_UNKNOWN -1
366''' % (offset_line_no + 1)
Azim Khanb31aa442018-07-03 11:57:54 +0100367 stream = StringIOWrapper('test_suite_ut.function', data,
368 offset_line_no)
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_no_end_header_comment(self):
373 """
Azim Khanb31aa442018-07-03 11:57:54 +0100374 Test that InvalidFileFormat is raised when end header comment is
375 missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100376 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100377 """
378 data = '''#include "mbedtls/ecp.h"
379
380#define ECP_PF_UNKNOWN -1
381
382'''
Azim Khanb31aa442018-07-03 11:57:54 +0100383 stream = StringIOWrapper('test_suite_ut.function', data)
384 self.assertRaises(GeneratorInputError, parse_until_pattern, stream,
385 END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100386
387
Azim Khanb31aa442018-07-03 11:57:54 +0100388class ParseSuiteDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100389 """
Azim Khanb31aa442018-07-03 11:57:54 +0100390 Test Suite for testing parse_suite_dependencies().
Azim Khan4b543232017-06-30 09:35:21 +0100391 """
392
Azim Khanb31aa442018-07-03 11:57:54 +0100393 def test_suite_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100394 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100395
396 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100397 """
398 data = '''
399 * depends_on:MBEDTLS_ECP_C
400 * END_DEPENDENCIES
401 */
402'''
403 expected = ['MBEDTLS_ECP_C']
Azim Khanb31aa442018-07-03 11:57:54 +0100404 stream = StringIOWrapper('test_suite_ut.function', data)
405 dependencies = parse_suite_dependencies(stream)
406 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100407
408 def test_no_end_dep_comment(self):
409 """
410 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100411 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100412 """
413 data = '''
414* depends_on:MBEDTLS_ECP_C
415'''
Azim Khanb31aa442018-07-03 11:57:54 +0100416 stream = StringIOWrapper('test_suite_ut.function', data)
417 self.assertRaises(GeneratorInputError, parse_suite_dependencies,
418 stream)
Azim Khan4b543232017-06-30 09:35:21 +0100419
Azim Khanb31aa442018-07-03 11:57:54 +0100420 def test_dependencies_split(self):
Azim Khan4b543232017-06-30 09:35:21 +0100421 """
422 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100423 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100424 """
425 data = '''
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100426 * depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
Azim Khan4b543232017-06-30 09:35:21 +0100427 * END_DEPENDENCIES
428 */
429'''
430 expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
Azim Khanb31aa442018-07-03 11:57:54 +0100431 stream = StringIOWrapper('test_suite_ut.function', data)
432 dependencies = parse_suite_dependencies(stream)
433 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100434
435
Azim Khanb31aa442018-07-03 11:57:54 +0100436class ParseFuncDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100437 """
Azim Khanb31aa442018-07-03 11:57:54 +0100438 Test Suite for testing parse_function_dependencies()
Azim Khan4b543232017-06-30 09:35:21 +0100439 """
440
Azim Khanb31aa442018-07-03 11:57:54 +0100441 def test_function_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100442 """
Azim Khanb31aa442018-07-03 11:57:54 +0100443 Test that parse_function_dependencies() correctly parses function
444 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100445 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100446 """
Azim Khanb31aa442018-07-03 11:57:54 +0100447 line = '/* BEGIN_CASE ' \
448 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
Azim Khan4b543232017-06-30 09:35:21 +0100449 expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
Azim Khanb31aa442018-07-03 11:57:54 +0100450 dependencies = parse_function_dependencies(line)
451 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100452
Azim Khanb31aa442018-07-03 11:57:54 +0100453 def test_no_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100454 """
Azim Khanb31aa442018-07-03 11:57:54 +0100455 Test that parse_function_dependencies() correctly parses function
456 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100457 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100458 """
459 line = '/* BEGIN_CASE */'
Azim Khanb31aa442018-07-03 11:57:54 +0100460 dependencies = parse_function_dependencies(line)
461 self.assertEqual(dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100462
Azim Khanb31aa442018-07-03 11:57:54 +0100463 def test_tolerance(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 """
469 line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
Azim Khanb31aa442018-07-03 11:57:54 +0100470 dependencies = parse_function_dependencies(line)
471 self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
Azim Khan4b543232017-06-30 09:35:21 +0100472
473
474class ParseFuncSignature(TestCase):
475 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100476 Test Suite for parse_function_arguments().
Azim Khan4b543232017-06-30 09:35:21 +0100477 """
478
479 def test_int_and_char_params(self):
480 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100481 Test int and char parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100482 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100483 """
484 line = 'void entropy_threshold( char * a, int b, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100485 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100486 self.assertEqual(args, ['char*', 'int', 'int'])
487 self.assertEqual(local, '')
Azim Khanb31aa442018-07-03 11:57:54 +0100488 self.assertEqual(arg_dispatch, ['(char *) params[0]',
489 '*( (int *) params[1] )',
490 '*( (int *) params[2] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100491
492 def test_hex_params(self):
493 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100494 Test hex parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100495 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100496 """
Azim Khan5fcca462018-06-29 11:05:32 +0100497 line = 'void entropy_threshold( char * a, data_t * h, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100498 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100499 self.assertEqual(args, ['char*', 'hex', 'int'])
Azim Khanb31aa442018-07-03 11:57:54 +0100500 self.assertEqual(local,
Azim Khan4084ec72018-07-05 14:20:08 +0100501 ' data_t data1 = {(uint8_t *) params[1], '
Azim Khanb31aa442018-07-03 11:57:54 +0100502 '*( (uint32_t *) params[2] )};\n')
503 self.assertEqual(arg_dispatch, ['(char *) params[0]',
Azim Khan4084ec72018-07-05 14:20:08 +0100504 '&data1',
Azim Khanb31aa442018-07-03 11:57:54 +0100505 '*( (int *) params[3] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100506
Azim Khan4b543232017-06-30 09:35:21 +0100507 def test_unsupported_arg(self):
508 """
Azim Khan5fcca462018-06-29 11:05:32 +0100509 Test unsupported arguments (not among int, char * and data_t)
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100510 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100511 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100512 line = 'void entropy_threshold( char * a, data_t * h, char result )'
513 self.assertRaises(ValueError, parse_function_arguments, line)
Azim Khan4b543232017-06-30 09:35:21 +0100514
515 def test_no_params(self):
516 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100517 Test no parameters.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100518 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100519 """
520 line = 'void entropy_threshold()'
Azim Khanfcdf6852018-07-05 17:31:46 +0100521 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100522 self.assertEqual(args, [])
523 self.assertEqual(local, '')
524 self.assertEqual(arg_dispatch, [])
525
526
527class ParseFunctionCode(TestCase):
528 """
529 Test suite for testing parse_function_code()
530 """
531
532 def test_no_function(self):
533 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100534 Test no test function found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100535 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100536 """
537 data = '''
538No
539test
540function
541'''
Azim Khanb31aa442018-07-03 11:57:54 +0100542 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100543 err_msg = 'file: test_suite_ut.function - Test functions not found!'
Gilles Peskine38b66df2020-08-12 02:11:38 +0200544 self.assertRaisesRegex(GeneratorInputError, err_msg,
545 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100546
547 def test_no_end_case_comment(self):
548 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100549 Test missing end case.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100550 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100551 """
552 data = '''
553void test_func()
554{
555}
556'''
Azim Khanb31aa442018-07-03 11:57:54 +0100557 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100558 err_msg = r'file: test_suite_ut.function - '\
559 'end case pattern .*? not found!'
Gilles Peskine38b66df2020-08-12 02:11:38 +0200560 self.assertRaisesRegex(GeneratorInputError, err_msg,
561 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100562
Azim Khanfcdf6852018-07-05 17:31:46 +0100563 @patch("generate_test_code.parse_function_arguments")
Azim Khanb31aa442018-07-03 11:57:54 +0100564 def test_function_called(self,
Azim Khanfcdf6852018-07-05 17:31:46 +0100565 parse_function_arguments_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100566 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100567 Test parse_function_code()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100568 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100569 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100570 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100571 data = '''
572void test_func()
573{
574}
575'''
Azim Khanb31aa442018-07-03 11:57:54 +0100576 stream = StringIOWrapper('test_suite_ut.function', data)
577 self.assertRaises(GeneratorInputError, parse_function_code,
578 stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100579 self.assertTrue(parse_function_arguments_mock.called)
580 parse_function_arguments_mock.assert_called_with('void test_func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100581
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000582 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100583 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000584 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100585 @patch("generate_test_code.parse_function_arguments")
586 def test_return(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100587 gen_function_wrapper_mock,
588 gen_dependencies_mock,
589 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100590 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100591 Test generated code.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100592 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100593 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100594 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100595 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100596 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100597 gen_dispatch_mock.side_effect = gen_dispatch
598 data = '''
599void func()
600{
601 ba ba black sheep
602 have you any wool
603}
604/* END_CASE */
605'''
Azim Khanb31aa442018-07-03 11:57:54 +0100606 stream = StringIOWrapper('test_suite_ut.function', data)
607 name, arg, code, dispatch_code = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100608
Azim Khanfcdf6852018-07-05 17:31:46 +0100609 self.assertTrue(parse_function_arguments_mock.called)
610 parse_function_arguments_mock.assert_called_with('void func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100611 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
612 self.assertEqual(name, 'test_func')
613 self.assertEqual(arg, [])
Azim Khan4084ec72018-07-05 14:20:08 +0100614 expected = '''#line 1 "test_suite_ut.function"
615
Azim Khan4b543232017-06-30 09:35:21 +0100616void test_func()
617{
618 ba ba black sheep
619 have you any wool
620exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100621 ;
Azim Khan4b543232017-06-30 09:35:21 +0100622}
623'''
624 self.assertEqual(code, expected)
625 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
626
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000627 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100628 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000629 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100630 @patch("generate_test_code.parse_function_arguments")
631 def test_with_exit_label(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100632 gen_function_wrapper_mock,
633 gen_dependencies_mock,
634 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100635 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100636 Test when exit label is present.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100637 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100638 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100639 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100640 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100641 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100642 gen_dispatch_mock.side_effect = gen_dispatch
643 data = '''
644void func()
645{
646 ba ba black sheep
647 have you any wool
648exit:
649 yes sir yes sir
650 3 bags full
651}
652/* END_CASE */
653'''
Azim Khanb31aa442018-07-03 11:57:54 +0100654 stream = StringIOWrapper('test_suite_ut.function', data)
655 _, _, code, _ = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100656
Azim Khan4084ec72018-07-05 14:20:08 +0100657 expected = '''#line 1 "test_suite_ut.function"
658
Azim Khan4b543232017-06-30 09:35:21 +0100659void test_func()
660{
661 ba ba black sheep
662 have you any wool
663exit:
664 yes sir yes sir
665 3 bags full
666}
667'''
668 self.assertEqual(code, expected)
669
Azim Khanfcdf6852018-07-05 17:31:46 +0100670 def test_non_void_function(self):
671 """
672 Test invalid signature (non void).
673 :return:
674 """
675 data = 'int entropy_threshold( char * a, data_t * h, int result )'
676 err_msg = 'file: test_suite_ut.function - Test functions not found!'
677 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine38b66df2020-08-12 02:11:38 +0200678 self.assertRaisesRegex(GeneratorInputError, err_msg,
679 parse_function_code, stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100680
681 @patch("generate_test_code.gen_dispatch")
682 @patch("generate_test_code.gen_dependencies")
683 @patch("generate_test_code.gen_function_wrapper")
684 @patch("generate_test_code.parse_function_arguments")
Gilles Peskine0cf42202022-11-11 16:36:51 +0100685 def test_function_name_on_newline(self, parse_function_arguments_mock,
686 gen_function_wrapper_mock,
687 gen_dependencies_mock,
688 gen_dispatch_mock):
Azim Khanfcdf6852018-07-05 17:31:46 +0100689 """
Gilles Peskine0cf42202022-11-11 16:36:51 +0100690 Test with line break before the function name.
Azim Khanfcdf6852018-07-05 17:31:46 +0100691 :return:
692 """
693 parse_function_arguments_mock.return_value = ([], '', [])
694 gen_function_wrapper_mock.return_value = ''
695 gen_dependencies_mock.side_effect = gen_dependencies
696 gen_dispatch_mock.side_effect = gen_dispatch
697 data = '''
698void
699
700
701func()
702{
703 ba ba black sheep
704 have you any wool
705exit:
706 yes sir yes sir
707 3 bags full
708}
709/* END_CASE */
710'''
711 stream = StringIOWrapper('test_suite_ut.function', data)
712 _, _, code, _ = parse_function_code(stream, [], [])
713
714 expected = '''#line 1 "test_suite_ut.function"
715
716void
717
718
719test_func()
720{
721 ba ba black sheep
722 have you any wool
723exit:
724 yes sir yes sir
725 3 bags full
726}
727'''
728 self.assertEqual(code, expected)
729
Gilles Peskine07510f52022-11-11 16:37:16 +0100730 @patch("generate_test_code.gen_dispatch")
731 @patch("generate_test_code.gen_dependencies")
732 @patch("generate_test_code.gen_function_wrapper")
733 @patch("generate_test_code.parse_function_arguments")
734 def test_case_starting_with_comment(self, parse_function_arguments_mock,
735 gen_function_wrapper_mock,
736 gen_dependencies_mock,
737 gen_dispatch_mock):
738 """
739 Test with comments before the function signature
740 :return:
741 """
742 parse_function_arguments_mock.return_value = ([], '', [])
743 gen_function_wrapper_mock.return_value = ''
744 gen_dependencies_mock.side_effect = gen_dependencies
745 gen_dispatch_mock.side_effect = gen_dispatch
746 data = '''/* comment */
747/* more
748 * comment */
Gilles Peskine18f48eb2022-11-18 22:24:56 +0100749// this is\\
750still \\
Gilles Peskine07510f52022-11-11 16:37:16 +0100751a comment
752void func()
753{
754 ba ba black sheep
755 have you any wool
756exit:
757 yes sir yes sir
758 3 bags full
759}
760/* END_CASE */
761'''
762 stream = StringIOWrapper('test_suite_ut.function', data)
763 _, _, code, _ = parse_function_code(stream, [], [])
764
765 expected = '''#line 1 "test_suite_ut.function"
766
767
768
769void test_func()
770{
771 ba ba black sheep
772 have you any wool
773exit:
774 yes sir yes sir
775 3 bags full
776}
777'''
778 self.assertEqual(code, expected)
779
780 @patch("generate_test_code.gen_dispatch")
781 @patch("generate_test_code.gen_dependencies")
782 @patch("generate_test_code.gen_function_wrapper")
783 @patch("generate_test_code.parse_function_arguments")
784 def test_comment_in_prototype(self, parse_function_arguments_mock,
785 gen_function_wrapper_mock,
786 gen_dependencies_mock,
787 gen_dispatch_mock):
788 """
789 Test with comments in the function prototype
790 :return:
791 """
792 parse_function_arguments_mock.return_value = ([], '', [])
793 gen_function_wrapper_mock.return_value = ''
794 gen_dependencies_mock.side_effect = gen_dependencies
795 gen_dispatch_mock.side_effect = gen_dispatch
796 data = '''
797void func( int x, // (line \\
798 comment)
799 int y /* lone closing parenthesis) */ )
800{
801 ba ba black sheep
802 have you any wool
803exit:
804 yes sir yes sir
805 3 bags full
806}
807/* END_CASE */
808'''
809 stream = StringIOWrapper('test_suite_ut.function', data)
810 _, _, code, _ = parse_function_code(stream, [], [])
811
812 expected = '''#line 1 "test_suite_ut.function"
813
814void test_func( int x,
815 int y )
816{
817 ba ba black sheep
818 have you any wool
819exit:
820 yes sir yes sir
821 3 bags full
822}
823'''
824 self.assertEqual(code, expected)
825
Azim Khan4b543232017-06-30 09:35:21 +0100826
827class ParseFunction(TestCase):
828 """
829 Test Suite for testing parse_functions()
830 """
831
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000832 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000833 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100834 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000835 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100836 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100837 """
Azim Khanb31aa442018-07-03 11:57:54 +0100838 def stop(*_unused):
839 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100840 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000841 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100842 data = '''/* BEGIN_HEADER */
843#include "mbedtls/ecp.h"
844
845#define ECP_PF_UNKNOWN -1
846/* END_HEADER */
847'''
Azim Khanb31aa442018-07-03 11:57:54 +0100848 stream = StringIOWrapper('test_suite_ut.function', data)
849 self.assertRaises(Exception, parse_functions, stream)
850 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100851 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000852
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000853 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000854 def test_begin_helper(self, parse_until_pattern_mock):
855 """
856 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100857 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000858 """
Azim Khanb31aa442018-07-03 11:57:54 +0100859 def stop(*_unused):
860 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000861 raise Exception
862 parse_until_pattern_mock.side_effect = stop
863 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100864void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000865{
Azim Khanb31aa442018-07-03 11:57:54 +0100866 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000867}
868/* END_SUITE_HELPERS */
869'''
Azim Khanb31aa442018-07-03 11:57:54 +0100870 stream = StringIOWrapper('test_suite_ut.function', data)
871 self.assertRaises(Exception, parse_functions, stream)
872 parse_until_pattern_mock.assert_called_with(stream,
873 END_SUITE_HELPERS_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100874 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100875
Azim Khanb31aa442018-07-03 11:57:54 +0100876 @patch("generate_test_code.parse_suite_dependencies")
877 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100878 """
Azim Khanb31aa442018-07-03 11:57:54 +0100879 Test that begin dep is checked and parse_suite_dependencies() is
880 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100881 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100882 """
Azim Khanb31aa442018-07-03 11:57:54 +0100883 def stop(*_unused):
884 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100885 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100886 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100887 data = '''/* BEGIN_DEPENDENCIES
888 * depends_on:MBEDTLS_ECP_C
889 * END_DEPENDENCIES
890 */
891'''
Azim Khanb31aa442018-07-03 11:57:54 +0100892 stream = StringIOWrapper('test_suite_ut.function', data)
893 self.assertRaises(Exception, parse_functions, stream)
894 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +0100895 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100896
Azim Khanb31aa442018-07-03 11:57:54 +0100897 @patch("generate_test_code.parse_function_dependencies")
898 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100899 """
Azim Khanb31aa442018-07-03 11:57:54 +0100900 Test that begin dep is checked and parse_function_dependencies() is
901 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100902 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100903 """
Azim Khanb31aa442018-07-03 11:57:54 +0100904 def stop(*_unused):
905 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100906 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100907 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100908
Azim Khanb31aa442018-07-03 11:57:54 +0100909 dependencies_str = '/* BEGIN_CASE ' \
910 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100911 data = '''%svoid test_func()
912{
913}
Azim Khanb31aa442018-07-03 11:57:54 +0100914''' % dependencies_str
915 stream = StringIOWrapper('test_suite_ut.function', data)
916 self.assertRaises(Exception, parse_functions, stream)
917 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +0100918 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100919
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000920 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +0100921 @patch("generate_test_code.parse_function_dependencies")
922 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +0100923 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000924 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100925 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100926 """
Azim Khanb31aa442018-07-03 11:57:54 +0100927 func_mock1.return_value = []
928 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +0100929{
930}
931'''
932 func_dispatch = '''
933 test_func_wrapper,
934'''
Azim Khanb31aa442018-07-03 11:57:54 +0100935 func_mock2.return_value = 'test_func', [],\
936 in_func_code, func_dispatch
937 dependencies_str = '/* BEGIN_CASE ' \
938 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100939 data = '''%svoid test_func()
940{
941}
Azim Khanb31aa442018-07-03 11:57:54 +0100942''' % dependencies_str
943 stream = StringIOWrapper('test_suite_ut.function', data)
944 suite_dependencies, dispatch_code, func_code, func_info = \
945 parse_functions(stream)
946 func_mock1.assert_called_with(dependencies_str)
947 func_mock2.assert_called_with(stream, [], [])
948 self.assertEqual(stream.line_no, 5)
949 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100950 expected_dispatch_code = '''/* Function Id: 0 */
951
952 test_func_wrapper,
953'''
954 self.assertEqual(dispatch_code, expected_dispatch_code)
955 self.assertEqual(func_code, in_func_code)
956 self.assertEqual(func_info, {'test_func': (0, [])})
957
958 def test_parsing(self):
959 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000960 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100961 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100962 """
963 data = '''/* BEGIN_HEADER */
964#include "mbedtls/ecp.h"
965
966#define ECP_PF_UNKNOWN -1
967/* END_HEADER */
968
969/* BEGIN_DEPENDENCIES
970 * depends_on:MBEDTLS_ECP_C
971 * END_DEPENDENCIES
972 */
973
974/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
975void func1()
976{
977}
978/* END_CASE */
979
980/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
981void func2()
982{
983}
984/* END_CASE */
985'''
Azim Khanb31aa442018-07-03 11:57:54 +0100986 stream = StringIOWrapper('test_suite_ut.function', data)
987 suite_dependencies, dispatch_code, func_code, func_info = \
988 parse_functions(stream)
989 self.assertEqual(stream.line_no, 23)
990 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +0100991
992 expected_dispatch_code = '''/* Function Id: 0 */
993
994#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
995 test_func1_wrapper,
996#else
997 NULL,
998#endif
999/* Function Id: 1 */
1000
1001#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
1002 test_func2_wrapper,
1003#else
1004 NULL,
1005#endif
1006'''
1007 self.assertEqual(dispatch_code, expected_dispatch_code)
1008 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +01001009#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001010#include "mbedtls/ecp.h"
1011
1012#define ECP_PF_UNKNOWN -1
1013#if defined(MBEDTLS_ENTROPY_NV_SEED)
1014#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001015#line 13 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001016void test_func1()
1017{
1018exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001019 ;
Azim Khan4b543232017-06-30 09:35:21 +01001020}
1021
1022void test_func1_wrapper( void ** params )
1023{
1024 (void)params;
1025
1026 test_func1( );
1027}
1028#endif /* MBEDTLS_FS_IO */
1029#endif /* MBEDTLS_ENTROPY_NV_SEED */
1030#if defined(MBEDTLS_ENTROPY_NV_SEED)
1031#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001032#line 19 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001033void test_func2()
1034{
1035exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001036 ;
Azim Khan4b543232017-06-30 09:35:21 +01001037}
1038
1039void test_func2_wrapper( void ** params )
1040{
1041 (void)params;
1042
1043 test_func2( );
1044}
1045#endif /* MBEDTLS_FS_IO */
1046#endif /* MBEDTLS_ENTROPY_NV_SEED */
1047#endif /* MBEDTLS_ECP_C */
1048'''
1049 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001050 self.assertEqual(func_info, {'test_func1': (0, []),
1051 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +01001052
1053 def test_same_function_name(self):
1054 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001055 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001056 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001057 """
1058 data = '''/* BEGIN_HEADER */
1059#include "mbedtls/ecp.h"
1060
1061#define ECP_PF_UNKNOWN -1
1062/* END_HEADER */
1063
1064/* BEGIN_DEPENDENCIES
1065 * depends_on:MBEDTLS_ECP_C
1066 * END_DEPENDENCIES
1067 */
1068
1069/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1070void func()
1071{
1072}
1073/* END_CASE */
1074
1075/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1076void func()
1077{
1078}
1079/* END_CASE */
1080'''
Azim Khanb31aa442018-07-03 11:57:54 +01001081 stream = StringIOWrapper('test_suite_ut.function', data)
1082 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +01001083
1084
Azim Khanb31aa442018-07-03 11:57:54 +01001085class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001086 """
Azim Khan599cd242017-07-06 17:34:27 +01001087 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +01001088 Note: Since escaped_split() output is used to write back to the
1089 intermediate data file. Any escape characters in the input are
1090 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001091 """
1092
1093 def test_invalid_input(self):
1094 """
1095 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001096 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001097 """
1098 self.assertRaises(ValueError, escaped_split, '', 'string')
1099
1100 def test_empty_string(self):
1101 """
Azim Khanb31aa442018-07-03 11:57:54 +01001102 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001103 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001104 """
1105 splits = escaped_split('', ':')
1106 self.assertEqual(splits, [])
1107
1108 def test_no_escape(self):
1109 """
Azim Khanb31aa442018-07-03 11:57:54 +01001110 Test with no escape character. The behaviour should be same as
1111 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001112 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001113 """
Azim Khanb31aa442018-07-03 11:57:54 +01001114 test_str = 'yahoo:google'
1115 splits = escaped_split(test_str, ':')
1116 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001117
1118 def test_escaped_input(self):
1119 """
Azim Khanb31aa442018-07-03 11:57:54 +01001120 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001121 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001122 """
Azim Khanb31aa442018-07-03 11:57:54 +01001123 test_str = r'yahoo\:google:facebook'
1124 splits = escaped_split(test_str, ':')
1125 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001126
1127 def test_escaped_escape(self):
1128 """
Azim Khanb31aa442018-07-03 11:57:54 +01001129 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001130 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001131 """
Azim Khan4084ec72018-07-05 14:20:08 +01001132 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +01001133 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001134 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001135
1136 def test_all_at_once(self):
1137 """
Azim Khanb31aa442018-07-03 11:57:54 +01001138 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001139 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001140 """
Azim Khan4084ec72018-07-05 14:20:08 +01001141 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +01001142 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001143 self.assertEqual(splits, [r'yahoo\\', r'google',
1144 r'facebook\:instagram\\',
1145 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001146
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001147
1148class ParseTestData(TestCase):
1149 """
1150 Test suite for parse test data.
1151 """
1152
1153 def test_parser(self):
1154 """
1155 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001156 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001157 """
1158 data = """
1159Diffie-Hellman full exchange #1
1160dhm_do_dhm:10:"23":10:"5"
1161
1162Diffie-Hellman full exchange #2
1163dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1164
1165Diffie-Hellman full exchange #3
1166dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1167
1168Diffie-Hellman selftest
1169dhm_selftest:
1170"""
Azim Khanb31aa442018-07-03 11:57:54 +01001171 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-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, test3, test4 = tests
1175 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1176 self.assertEqual(test1[1], 'dhm_do_dhm')
1177 self.assertEqual(test1[2], [])
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
Azim Khanb31aa442018-07-03 11:57:54 +01001186 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1187 self.assertEqual(test3[1], 'dhm_do_dhm')
1188 self.assertEqual(test3[2], [])
1189 self.assertEqual(test3[3], ['10',
1190 '"9345098382739712938719287391879381271"',
1191 '10',
1192 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001193
Azim Khanb31aa442018-07-03 11:57:54 +01001194 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1195 self.assertEqual(test4[1], 'dhm_selftest')
1196 self.assertEqual(test4[2], [])
1197 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001198
1199 def test_with_dependencies(self):
1200 """
1201 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001202 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001203 """
1204 data = """
1205Diffie-Hellman full exchange #1
1206depends_on:YAHOO
1207dhm_do_dhm:10:"23":10:"5"
1208
1209Diffie-Hellman full exchange #2
1210dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1211
1212"""
Azim Khanb31aa442018-07-03 11:57:54 +01001213 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001214 # List of (name, function_name, dependencies, args)
1215 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001216 test1, test2 = tests
1217 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1218 self.assertEqual(test1[1], 'dhm_do_dhm')
1219 self.assertEqual(test1[2], ['YAHOO'])
1220 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001221
Azim Khanb31aa442018-07-03 11:57:54 +01001222 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1223 self.assertEqual(test2[1], 'dhm_do_dhm')
1224 self.assertEqual(test2[2], [])
1225 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1226 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001227
1228 def test_no_args(self):
1229 """
Azim Khanb31aa442018-07-03 11:57:54 +01001230 Test GeneratorInputError is raised when test function name and
1231 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001232 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001233 """
1234 data = """
1235Diffie-Hellman full exchange #1
1236depends_on:YAHOO
1237
1238
1239Diffie-Hellman full exchange #2
1240dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1241
1242"""
Azim Khanb31aa442018-07-03 11:57:54 +01001243 stream = StringIOWrapper('test_suite_ut.function', data)
1244 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001245 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001246 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001247 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001248 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001249 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001250
1251 def test_incomplete_data(self):
1252 """
Azim Khanb31aa442018-07-03 11:57:54 +01001253 Test GeneratorInputError is raised when test function name
1254 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001255 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001256 """
1257 data = """
1258Diffie-Hellman full exchange #1
1259depends_on:YAHOO
1260"""
Azim Khanb31aa442018-07-03 11:57:54 +01001261 stream = StringIOWrapper('test_suite_ut.function', data)
1262 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001263 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001264 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001265 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001266 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001267 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001268
1269
1270class GenDepCheck(TestCase):
1271 """
Azim Khanb31aa442018-07-03 11:57:54 +01001272 Test suite for gen_dep_check(). It is assumed this function is
1273 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001274 """
1275
1276 def test_gen_dep_check(self):
1277 """
1278 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001279 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001280 """
1281 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001282 case 5:
1283 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001284#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001285 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001286#else
Azim Khand61b8372017-07-10 11:54:01 +01001287 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001288#endif
Azim Khand61b8372017-07-10 11:54:01 +01001289 }
1290 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001291 out = gen_dep_check(5, 'YAHOO')
1292 self.assertEqual(out, expected)
1293
Azim Khanb31aa442018-07-03 11:57:54 +01001294 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001295 """
1296 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001297 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001298 """
1299 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001300 case 5:
1301 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001302#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001303 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001304#else
Azim Khand61b8372017-07-10 11:54:01 +01001305 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001306#endif
Azim Khand61b8372017-07-10 11:54:01 +01001307 }
1308 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001309 out = gen_dep_check(5, '!YAHOO')
1310 self.assertEqual(out, expected)
1311
1312 def test_empty_dependency(self):
1313 """
1314 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001315 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001316 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001317 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001318
1319 def test_negative_dep_id(self):
1320 """
1321 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001322 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001323 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001324 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001325
1326
1327class GenExpCheck(TestCase):
1328 """
Azim Khanb31aa442018-07-03 11:57:54 +01001329 Test suite for gen_expression_check(). It is assumed this function
1330 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001331 """
1332
1333 def test_gen_exp_check(self):
1334 """
1335 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001336 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001337 """
1338 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001339 case 5:
1340 {
1341 *out_value = YAHOO;
1342 }
1343 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001344 out = gen_expression_check(5, 'YAHOO')
1345 self.assertEqual(out, expected)
1346
1347 def test_invalid_expression(self):
1348 """
1349 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001350 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001351 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001352 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001353
1354 def test_negative_exp_id(self):
1355 """
1356 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001357 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001358 """
Azim Khanb31aa442018-07-03 11:57:54 +01001359 self.assertRaises(GeneratorInputError, gen_expression_check,
1360 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001361
1362
Azim Khanb31aa442018-07-03 11:57:54 +01001363class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001364 """
Azim Khanb31aa442018-07-03 11:57:54 +01001365 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001366 """
1367
Azim Khanb31aa442018-07-03 11:57:54 +01001368 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001369 """
Azim Khanb31aa442018-07-03 11:57:54 +01001370 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001371 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001372 """
Azim Khanb31aa442018-07-03 11:57:54 +01001373 stream = StringIOWrapper('test_suite_ut.data', '')
1374 unique_dependencies = []
1375 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001376 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001377 self.assertEqual(len(unique_dependencies), 0)
1378 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001379
1380 def test_unique_dep_ids(self):
1381 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001382
1383 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001384 """
Azim Khanb31aa442018-07-03 11:57:54 +01001385 stream = StringIOWrapper('test_suite_ut.data', '')
1386 unique_dependencies = []
1387 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1388 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001389 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001390 case 0:
1391 {
Azim Khan599cd242017-07-06 17:34:27 +01001392#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001393 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001394#else
Azim Khand61b8372017-07-10 11:54:01 +01001395 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001396#endif
Azim Khand61b8372017-07-10 11:54:01 +01001397 }
1398 break;
1399 case 1:
1400 {
Azim Khan599cd242017-07-06 17:34:27 +01001401#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001402 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001403#else
Azim Khand61b8372017-07-10 11:54:01 +01001404 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001405#endif
Azim Khand61b8372017-07-10 11:54:01 +01001406 }
1407 break;
1408 case 2:
1409 {
Azim Khan599cd242017-07-06 17:34:27 +01001410#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001411 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001412#else
Azim Khand61b8372017-07-10 11:54:01 +01001413 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001414#endif
Azim Khand61b8372017-07-10 11:54:01 +01001415 }
1416 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001417 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001418 self.assertEqual(len(unique_dependencies), 3)
1419 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001420
1421 def test_dep_id_repeat(self):
1422 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001423
1424 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001425 """
Azim Khanb31aa442018-07-03 11:57:54 +01001426 stream = StringIOWrapper('test_suite_ut.data', '')
1427 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001428 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001429 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1430 unique_dependencies)
1431 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1432 unique_dependencies)
1433 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1434 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001435 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001436 case 0:
1437 {
Azim Khan599cd242017-07-06 17:34:27 +01001438#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001439 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001440#else
Azim Khand61b8372017-07-10 11:54:01 +01001441 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001442#endif
Azim Khand61b8372017-07-10 11:54:01 +01001443 }
1444 break;
1445 case 1:
1446 {
Azim Khan599cd242017-07-06 17:34:27 +01001447#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001448 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001449#else
Azim Khand61b8372017-07-10 11:54:01 +01001450 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001451#endif
Azim Khand61b8372017-07-10 11:54:01 +01001452 }
1453 break;
1454 case 2:
1455 {
Azim Khan599cd242017-07-06 17:34:27 +01001456#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001457 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001458#else
Azim Khand61b8372017-07-10 11:54:01 +01001459 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001460#endif
Azim Khand61b8372017-07-10 11:54:01 +01001461 }
1462 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001463 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001464 self.assertEqual(len(unique_dependencies), 3)
1465 self.assertEqual(stream.getvalue(),
1466 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001467
1468
1469class WriteParams(TestCase):
1470 """
1471 Test Suite for testing write_parameters().
1472 """
1473
1474 def test_no_params(self):
1475 """
1476 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001477 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001478 """
Azim Khanb31aa442018-07-03 11:57:54 +01001479 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001480 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001481 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001482 self.assertEqual(len(unique_expressions), 0)
1483 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001484 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001485
1486 def test_no_exp_param(self):
1487 """
1488 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001489 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001490 """
Azim Khanb31aa442018-07-03 11:57:54 +01001491 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001492 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001493 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1494 '0'],
1495 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001496 unique_expressions)
1497 self.assertEqual(len(unique_expressions), 0)
1498 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001499 self.assertEqual(stream.getvalue(),
1500 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001501
1502 def test_hex_format_int_param(self):
1503 """
1504 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001505 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001506 """
Azim Khanb31aa442018-07-03 11:57:54 +01001507 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001508 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001509 expression_code = write_parameters(stream,
1510 ['"Yahoo"', '"abcdef00"', '0xAA'],
1511 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001512 unique_expressions)
1513 self.assertEqual(len(unique_expressions), 0)
1514 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001515 self.assertEqual(stream.getvalue(),
1516 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001517
1518 def test_with_exp_param(self):
1519 """
1520 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001521 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001522 """
Azim Khanb31aa442018-07-03 11:57:54 +01001523 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001524 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001525 expression_code = write_parameters(stream,
1526 ['"Yahoo"', '"abcdef00"', '0',
1527 'MACRO1', 'MACRO2', 'MACRO3'],
1528 ['char*', 'hex', 'int',
1529 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001530 unique_expressions)
1531 self.assertEqual(len(unique_expressions), 3)
1532 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1533 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001534 case 0:
1535 {
1536 *out_value = MACRO1;
1537 }
1538 break;
1539 case 1:
1540 {
1541 *out_value = MACRO2;
1542 }
1543 break;
1544 case 2:
1545 {
1546 *out_value = MACRO3;
1547 }
1548 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001549 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001550 self.assertEqual(stream.getvalue(),
1551 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1552 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001553
Azim Khanb31aa442018-07-03 11:57:54 +01001554 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001555 """
1556 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001557 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001558 """
Azim Khanb31aa442018-07-03 11:57:54 +01001559 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001560 unique_expressions = []
1561 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001562 expression_code += write_parameters(stream,
1563 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1564 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001565 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001566 expression_code += write_parameters(stream,
1567 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1568 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001569 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001570 expression_code += write_parameters(stream,
1571 ['0', 'MACRO3', 'MACRO1'],
1572 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001573 unique_expressions)
1574 self.assertEqual(len(unique_expressions), 3)
1575 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1576 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001577 case 0:
1578 {
1579 *out_value = MACRO1;
1580 }
1581 break;
1582 case 1:
1583 {
1584 *out_value = MACRO2;
1585 }
1586 break;
1587 case 2:
1588 {
1589 *out_value = MACRO3;
1590 }
1591 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001592 self.assertEqual(expression_code, expected_expression_code)
1593 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1594:hex:"abcdef00":exp:1:exp:2
1595:int:0:exp:2:exp:0
1596'''
Azim Khanb31aa442018-07-03 11:57:54 +01001597 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001598
1599
Azim Khanb31aa442018-07-03 11:57:54 +01001600class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001601 """
Azim Khanb31aa442018-07-03 11:57:54 +01001602 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001603 """
Azim Khanb31aa442018-07-03 11:57:54 +01001604 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001605 """
Azim Khanb31aa442018-07-03 11:57:54 +01001606 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001607
1608 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001609 """
Azim Khanb31aa442018-07-03 11:57:54 +01001610 dep_check_code, expression_code = \
1611 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001612 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1613 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1614
Azim Khanb31aa442018-07-03 11:57:54 +01001615 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001616 """
Azim Khanb31aa442018-07-03 11:57:54 +01001617 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001618
1619 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001620 """
Azim Khanb31aa442018-07-03 11:57:54 +01001621 dep_check_code, expression_code = \
1622 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1623 'EXPRESSION_CODE')
1624 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001625#if defined(SUITE_DEP)
1626DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001627#endif
1628'''
1629 expected_expression_code = '''
1630#if defined(SUITE_DEP)
1631EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001632#endif
1633'''
Azim Khanb31aa442018-07-03 11:57:54 +01001634 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001635 self.assertEqual(expression_code, expected_expression_code)
1636
1637 def test_no_dep_no_exp(self):
1638 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001639 Test when there are no dependency and expression code.
1640 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001641 """
Azim Khanb31aa442018-07-03 11:57:54 +01001642 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001643 self.assertEqual(dep_check_code, '')
1644 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001645
1646
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001647class GenFromTestData(TestCase):
1648 """
1649 Test suite for gen_from_test_data()
1650 """
1651
Azim Khanb31aa442018-07-03 11:57:54 +01001652 @staticmethod
1653 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001654 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001655 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001656 def test_intermediate_data_file(func_mock1,
1657 write_parameters_mock,
1658 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001659 """
1660 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001661 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001662 """
1663 data = '''
1664My test
1665depends_on:DEP1
1666func1:0
1667'''
1668 data_f = StringIOWrapper('test_suite_ut.data', data)
1669 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1670 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001671 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001672 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001673 write_dependencies_mock.side_effect = write_dependencies
1674 func_mock1.side_effect = gen_suite_dep_checks
1675 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1676 write_dependencies_mock.assert_called_with(out_data_f,
1677 ['DEP1'], ['DEP1'])
1678 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1679 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001680 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001681 case 0:
1682 {
Azim Khan599cd242017-07-06 17:34:27 +01001683#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001684 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001685#else
Azim Khand61b8372017-07-10 11:54:01 +01001686 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001687#endif
Azim Khand61b8372017-07-10 11:54:01 +01001688 }
1689 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001690 func_mock1.assert_called_with(
1691 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001692
1693 def test_function_not_found(self):
1694 """
1695 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001696 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001697 """
1698 data = '''
1699My test
1700depends_on:DEP1
1701func1:0
1702'''
1703 data_f = StringIOWrapper('test_suite_ut.data', data)
1704 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1705 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001706 suite_dependencies = []
1707 self.assertRaises(GeneratorInputError, gen_from_test_data,
1708 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001709
1710 def test_different_func_args(self):
1711 """
Azim Khanb31aa442018-07-03 11:57:54 +01001712 Test that AssertError is raised when no. of parameters and
1713 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001714 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001715 """
1716 data = '''
1717My test
1718depends_on:DEP1
1719func1:0
1720'''
1721 data_f = StringIOWrapper('test_suite_ut.data', data)
1722 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001723 func_info = {'test_func2': (1, ('int', 'hex'))}
1724 suite_dependencies = []
1725 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1726 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001727
1728 def test_output(self):
1729 """
1730 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001731 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001732 """
1733 data = '''
1734My test 1
1735depends_on:DEP1
1736func1:0:0xfa:MACRO1:MACRO2
1737
1738My test 2
1739depends_on:DEP1:DEP2
1740func2:"yahoo":88:MACRO1
1741'''
1742 data_f = StringIOWrapper('test_suite_ut.data', data)
1743 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001744 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1745 'test_func2': (1, ('char*', 'int', 'int'))}
1746 suite_dependencies = []
1747 dep_check_code, expression_code = \
1748 gen_from_test_data(data_f, out_data_f, func_info,
1749 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001750 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001751 case 0:
1752 {
Azim Khan599cd242017-07-06 17:34:27 +01001753#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001754 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001755#else
Azim Khand61b8372017-07-10 11:54:01 +01001756 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001757#endif
Azim Khand61b8372017-07-10 11:54:01 +01001758 }
1759 break;
1760 case 1:
1761 {
Azim Khan599cd242017-07-06 17:34:27 +01001762#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001763 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001764#else
Azim Khand61b8372017-07-10 11:54:01 +01001765 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001766#endif
Azim Khand61b8372017-07-10 11:54:01 +01001767 }
1768 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001769 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001770depends_on:0
17710:int:0:int:0xfa:exp:0:exp:1
1772
1773My test 2
1774depends_on:0:1
17751:char*:"yahoo":int:88:exp:0
1776
1777'''
1778 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001779 case 0:
1780 {
1781 *out_value = MACRO1;
1782 }
1783 break;
1784 case 1:
1785 {
1786 *out_value = MACRO2;
1787 }
1788 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001789 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001790 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001791 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001792
1793
Azim Khanb31aa442018-07-03 11:57:54 +01001794if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001795 unittest_main()