blob: 8b2bf78d8aaa16a9222df77d75bb8b9567857b56 [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
Gilles Peskined8c08032022-11-29 22:03:32 +0100769
Gilles Peskine07510f52022-11-11 16:37:16 +0100770void test_func()
771{
772 ba ba black sheep
773 have you any wool
774exit:
775 yes sir yes sir
776 3 bags full
777}
778'''
779 self.assertEqual(code, expected)
780
781 @patch("generate_test_code.gen_dispatch")
782 @patch("generate_test_code.gen_dependencies")
783 @patch("generate_test_code.gen_function_wrapper")
784 @patch("generate_test_code.parse_function_arguments")
785 def test_comment_in_prototype(self, parse_function_arguments_mock,
786 gen_function_wrapper_mock,
787 gen_dependencies_mock,
788 gen_dispatch_mock):
789 """
790 Test with comments in the function prototype
791 :return:
792 """
793 parse_function_arguments_mock.return_value = ([], '', [])
794 gen_function_wrapper_mock.return_value = ''
795 gen_dependencies_mock.side_effect = gen_dependencies
796 gen_dispatch_mock.side_effect = gen_dispatch
797 data = '''
798void func( int x, // (line \\
799 comment)
800 int y /* lone closing parenthesis) */ )
801{
802 ba ba black sheep
803 have you any wool
804exit:
805 yes sir yes sir
806 3 bags full
807}
808/* END_CASE */
809'''
810 stream = StringIOWrapper('test_suite_ut.function', data)
811 _, _, code, _ = parse_function_code(stream, [], [])
812
813 expected = '''#line 1 "test_suite_ut.function"
814
815void test_func( int x,
816 int y )
817{
818 ba ba black sheep
819 have you any wool
820exit:
821 yes sir yes sir
822 3 bags full
823}
824'''
825 self.assertEqual(code, expected)
826
Gilles Peskine45747a02022-11-18 22:25:18 +0100827 @patch("generate_test_code.gen_dispatch")
828 @patch("generate_test_code.gen_dependencies")
829 @patch("generate_test_code.gen_function_wrapper")
830 @patch("generate_test_code.parse_function_arguments")
831 def test_line_comment_in_block_comment(self, parse_function_arguments_mock,
832 gen_function_wrapper_mock,
833 gen_dependencies_mock,
834 gen_dispatch_mock):
835 """
836 Test with line comment in block comment.
837 :return:
838 """
839 parse_function_arguments_mock.return_value = ([], '', [])
840 gen_function_wrapper_mock.return_value = ''
841 gen_dependencies_mock.side_effect = gen_dependencies
842 gen_dispatch_mock.side_effect = gen_dispatch
843 data = '''
844void func( int x /* // */ )
845{
846 ba ba black sheep
847 have you any wool
848exit:
849 yes sir yes sir
850 3 bags full
851}
852/* END_CASE */
853'''
854 stream = StringIOWrapper('test_suite_ut.function', data)
855 _, _, code, _ = parse_function_code(stream, [], [])
856
857 expected = '''#line 1 "test_suite_ut.function"
858
859void test_func( int x )
860{
861 ba ba black sheep
862 have you any wool
863exit:
864 yes sir yes sir
865 3 bags full
866}
867'''
868 self.assertEqual(code, expected)
869
870 @patch("generate_test_code.gen_dispatch")
871 @patch("generate_test_code.gen_dependencies")
872 @patch("generate_test_code.gen_function_wrapper")
873 @patch("generate_test_code.parse_function_arguments")
874 def test_block_comment_in_line_comment(self, parse_function_arguments_mock,
875 gen_function_wrapper_mock,
876 gen_dependencies_mock,
877 gen_dispatch_mock):
878 """
879 Test with block comment in line comment.
880 :return:
881 """
882 parse_function_arguments_mock.return_value = ([], '', [])
883 gen_function_wrapper_mock.return_value = ''
884 gen_dependencies_mock.side_effect = gen_dependencies
885 gen_dispatch_mock.side_effect = gen_dispatch
886 data = '''
887// /*
888void func( int x )
889{
890 ba ba black sheep
891 have you any wool
892exit:
893 yes sir yes sir
894 3 bags full
895}
896/* END_CASE */
897'''
898 stream = StringIOWrapper('test_suite_ut.function', data)
899 _, _, code, _ = parse_function_code(stream, [], [])
900
901 expected = '''#line 1 "test_suite_ut.function"
902
903
904void test_func( int x )
905{
906 ba ba black sheep
907 have you any wool
908exit:
909 yes sir yes sir
910 3 bags full
911}
912'''
913 self.assertEqual(code, expected)
914
Azim Khan4b543232017-06-30 09:35:21 +0100915
916class ParseFunction(TestCase):
917 """
918 Test Suite for testing parse_functions()
919 """
920
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000921 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000922 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100923 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000924 Test that begin header is checked and parse_until_pattern() 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 def stop(*_unused):
928 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100929 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000930 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100931 data = '''/* BEGIN_HEADER */
932#include "mbedtls/ecp.h"
933
934#define ECP_PF_UNKNOWN -1
935/* END_HEADER */
936'''
Azim Khanb31aa442018-07-03 11:57:54 +0100937 stream = StringIOWrapper('test_suite_ut.function', data)
938 self.assertRaises(Exception, parse_functions, stream)
939 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100940 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000941
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000942 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000943 def test_begin_helper(self, parse_until_pattern_mock):
944 """
945 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100946 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000947 """
Azim Khanb31aa442018-07-03 11:57:54 +0100948 def stop(*_unused):
949 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000950 raise Exception
951 parse_until_pattern_mock.side_effect = stop
952 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100953void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000954{
Azim Khanb31aa442018-07-03 11:57:54 +0100955 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000956}
957/* END_SUITE_HELPERS */
958'''
Azim Khanb31aa442018-07-03 11:57:54 +0100959 stream = StringIOWrapper('test_suite_ut.function', data)
960 self.assertRaises(Exception, parse_functions, stream)
961 parse_until_pattern_mock.assert_called_with(stream,
962 END_SUITE_HELPERS_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100963 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100964
Azim Khanb31aa442018-07-03 11:57:54 +0100965 @patch("generate_test_code.parse_suite_dependencies")
966 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100967 """
Azim Khanb31aa442018-07-03 11:57:54 +0100968 Test that begin dep is checked and parse_suite_dependencies() is
969 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100970 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100971 """
Azim Khanb31aa442018-07-03 11:57:54 +0100972 def stop(*_unused):
973 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100974 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100975 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100976 data = '''/* BEGIN_DEPENDENCIES
977 * depends_on:MBEDTLS_ECP_C
978 * END_DEPENDENCIES
979 */
980'''
Azim Khanb31aa442018-07-03 11:57:54 +0100981 stream = StringIOWrapper('test_suite_ut.function', data)
982 self.assertRaises(Exception, parse_functions, stream)
983 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +0100984 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100985
Azim Khanb31aa442018-07-03 11:57:54 +0100986 @patch("generate_test_code.parse_function_dependencies")
987 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100988 """
Azim Khanb31aa442018-07-03 11:57:54 +0100989 Test that begin dep is checked and parse_function_dependencies() is
990 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100991 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100992 """
Azim Khanb31aa442018-07-03 11:57:54 +0100993 def stop(*_unused):
994 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100995 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100996 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100997
Azim Khanb31aa442018-07-03 11:57:54 +0100998 dependencies_str = '/* BEGIN_CASE ' \
999 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +01001000 data = '''%svoid test_func()
1001{
1002}
Azim Khanb31aa442018-07-03 11:57:54 +01001003''' % dependencies_str
1004 stream = StringIOWrapper('test_suite_ut.function', data)
1005 self.assertRaises(Exception, parse_functions, stream)
1006 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +01001007 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +01001008
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001009 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +01001010 @patch("generate_test_code.parse_function_dependencies")
1011 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +01001012 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001013 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001014 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001015 """
Azim Khanb31aa442018-07-03 11:57:54 +01001016 func_mock1.return_value = []
1017 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +01001018{
1019}
1020'''
1021 func_dispatch = '''
1022 test_func_wrapper,
1023'''
Azim Khanb31aa442018-07-03 11:57:54 +01001024 func_mock2.return_value = 'test_func', [],\
1025 in_func_code, func_dispatch
1026 dependencies_str = '/* BEGIN_CASE ' \
1027 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +01001028 data = '''%svoid test_func()
1029{
1030}
Azim Khanb31aa442018-07-03 11:57:54 +01001031''' % dependencies_str
1032 stream = StringIOWrapper('test_suite_ut.function', data)
1033 suite_dependencies, dispatch_code, func_code, func_info = \
1034 parse_functions(stream)
1035 func_mock1.assert_called_with(dependencies_str)
1036 func_mock2.assert_called_with(stream, [], [])
1037 self.assertEqual(stream.line_no, 5)
1038 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +01001039 expected_dispatch_code = '''/* Function Id: 0 */
1040
1041 test_func_wrapper,
1042'''
1043 self.assertEqual(dispatch_code, expected_dispatch_code)
1044 self.assertEqual(func_code, in_func_code)
1045 self.assertEqual(func_info, {'test_func': (0, [])})
1046
1047 def test_parsing(self):
1048 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001049 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001050 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001051 """
1052 data = '''/* BEGIN_HEADER */
1053#include "mbedtls/ecp.h"
1054
1055#define ECP_PF_UNKNOWN -1
1056/* END_HEADER */
1057
1058/* BEGIN_DEPENDENCIES
1059 * depends_on:MBEDTLS_ECP_C
1060 * END_DEPENDENCIES
1061 */
1062
1063/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1064void func1()
1065{
1066}
1067/* END_CASE */
1068
1069/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1070void func2()
1071{
1072}
1073/* END_CASE */
1074'''
Azim Khanb31aa442018-07-03 11:57:54 +01001075 stream = StringIOWrapper('test_suite_ut.function', data)
1076 suite_dependencies, dispatch_code, func_code, func_info = \
1077 parse_functions(stream)
1078 self.assertEqual(stream.line_no, 23)
1079 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +01001080
1081 expected_dispatch_code = '''/* Function Id: 0 */
1082
1083#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
1084 test_func1_wrapper,
1085#else
1086 NULL,
1087#endif
1088/* Function Id: 1 */
1089
1090#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
1091 test_func2_wrapper,
1092#else
1093 NULL,
1094#endif
1095'''
1096 self.assertEqual(dispatch_code, expected_dispatch_code)
1097 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +01001098#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001099#include "mbedtls/ecp.h"
1100
1101#define ECP_PF_UNKNOWN -1
1102#if defined(MBEDTLS_ENTROPY_NV_SEED)
1103#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001104#line 13 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001105void test_func1()
1106{
1107exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001108 ;
Azim Khan4b543232017-06-30 09:35:21 +01001109}
1110
1111void test_func1_wrapper( void ** params )
1112{
1113 (void)params;
1114
1115 test_func1( );
1116}
1117#endif /* MBEDTLS_FS_IO */
1118#endif /* MBEDTLS_ENTROPY_NV_SEED */
1119#if defined(MBEDTLS_ENTROPY_NV_SEED)
1120#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001121#line 19 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001122void test_func2()
1123{
1124exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001125 ;
Azim Khan4b543232017-06-30 09:35:21 +01001126}
1127
1128void test_func2_wrapper( void ** params )
1129{
1130 (void)params;
1131
1132 test_func2( );
1133}
1134#endif /* MBEDTLS_FS_IO */
1135#endif /* MBEDTLS_ENTROPY_NV_SEED */
1136#endif /* MBEDTLS_ECP_C */
1137'''
1138 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001139 self.assertEqual(func_info, {'test_func1': (0, []),
1140 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +01001141
1142 def test_same_function_name(self):
1143 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001144 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001145 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001146 """
1147 data = '''/* BEGIN_HEADER */
1148#include "mbedtls/ecp.h"
1149
1150#define ECP_PF_UNKNOWN -1
1151/* END_HEADER */
1152
1153/* BEGIN_DEPENDENCIES
1154 * depends_on:MBEDTLS_ECP_C
1155 * END_DEPENDENCIES
1156 */
1157
1158/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1159void func()
1160{
1161}
1162/* END_CASE */
1163
1164/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1165void func()
1166{
1167}
1168/* END_CASE */
1169'''
Azim Khanb31aa442018-07-03 11:57:54 +01001170 stream = StringIOWrapper('test_suite_ut.function', data)
1171 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +01001172
1173
Azim Khanb31aa442018-07-03 11:57:54 +01001174class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001175 """
Azim Khan599cd242017-07-06 17:34:27 +01001176 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +01001177 Note: Since escaped_split() output is used to write back to the
1178 intermediate data file. Any escape characters in the input are
1179 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001180 """
1181
1182 def test_invalid_input(self):
1183 """
1184 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001185 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001186 """
1187 self.assertRaises(ValueError, escaped_split, '', 'string')
1188
1189 def test_empty_string(self):
1190 """
Azim Khanb31aa442018-07-03 11:57:54 +01001191 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001192 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001193 """
1194 splits = escaped_split('', ':')
1195 self.assertEqual(splits, [])
1196
1197 def test_no_escape(self):
1198 """
Azim Khanb31aa442018-07-03 11:57:54 +01001199 Test with no escape character. The behaviour should be same as
1200 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001201 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001202 """
Azim Khanb31aa442018-07-03 11:57:54 +01001203 test_str = 'yahoo:google'
1204 splits = escaped_split(test_str, ':')
1205 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001206
1207 def test_escaped_input(self):
1208 """
Azim Khanb31aa442018-07-03 11:57:54 +01001209 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001210 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001211 """
Azim Khanb31aa442018-07-03 11:57:54 +01001212 test_str = r'yahoo\:google:facebook'
1213 splits = escaped_split(test_str, ':')
1214 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001215
1216 def test_escaped_escape(self):
1217 """
Azim Khanb31aa442018-07-03 11:57:54 +01001218 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001219 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001220 """
Azim Khan4084ec72018-07-05 14:20:08 +01001221 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +01001222 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001223 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001224
1225 def test_all_at_once(self):
1226 """
Azim Khanb31aa442018-07-03 11:57:54 +01001227 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001228 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001229 """
Azim Khan4084ec72018-07-05 14:20:08 +01001230 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +01001231 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001232 self.assertEqual(splits, [r'yahoo\\', r'google',
1233 r'facebook\:instagram\\',
1234 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001235
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001236
1237class ParseTestData(TestCase):
1238 """
1239 Test suite for parse test data.
1240 """
1241
1242 def test_parser(self):
1243 """
1244 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001245 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001246 """
1247 data = """
1248Diffie-Hellman full exchange #1
1249dhm_do_dhm:10:"23":10:"5"
1250
1251Diffie-Hellman full exchange #2
1252dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1253
1254Diffie-Hellman full exchange #3
1255dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1256
1257Diffie-Hellman selftest
1258dhm_selftest:
1259"""
Azim Khanb31aa442018-07-03 11:57:54 +01001260 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001261 # List of (name, function_name, dependencies, args)
1262 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001263 test1, test2, test3, test4 = tests
1264 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1265 self.assertEqual(test1[1], 'dhm_do_dhm')
1266 self.assertEqual(test1[2], [])
1267 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001268
Azim Khanb31aa442018-07-03 11:57:54 +01001269 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1270 self.assertEqual(test2[1], 'dhm_do_dhm')
1271 self.assertEqual(test2[2], [])
1272 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1273 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001274
Azim Khanb31aa442018-07-03 11:57:54 +01001275 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1276 self.assertEqual(test3[1], 'dhm_do_dhm')
1277 self.assertEqual(test3[2], [])
1278 self.assertEqual(test3[3], ['10',
1279 '"9345098382739712938719287391879381271"',
1280 '10',
1281 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001282
Azim Khanb31aa442018-07-03 11:57:54 +01001283 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1284 self.assertEqual(test4[1], 'dhm_selftest')
1285 self.assertEqual(test4[2], [])
1286 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001287
1288 def test_with_dependencies(self):
1289 """
1290 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001291 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001292 """
1293 data = """
1294Diffie-Hellman full exchange #1
1295depends_on:YAHOO
1296dhm_do_dhm:10:"23":10:"5"
1297
1298Diffie-Hellman full exchange #2
1299dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1300
1301"""
Azim Khanb31aa442018-07-03 11:57:54 +01001302 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001303 # List of (name, function_name, dependencies, args)
1304 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001305 test1, test2 = tests
1306 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1307 self.assertEqual(test1[1], 'dhm_do_dhm')
1308 self.assertEqual(test1[2], ['YAHOO'])
1309 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001310
Azim Khanb31aa442018-07-03 11:57:54 +01001311 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1312 self.assertEqual(test2[1], 'dhm_do_dhm')
1313 self.assertEqual(test2[2], [])
1314 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1315 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001316
1317 def test_no_args(self):
1318 """
Azim Khanb31aa442018-07-03 11:57:54 +01001319 Test GeneratorInputError is raised when test function name and
1320 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001321 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001322 """
1323 data = """
1324Diffie-Hellman full exchange #1
1325depends_on:YAHOO
1326
1327
1328Diffie-Hellman full exchange #2
1329dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1330
1331"""
Azim Khanb31aa442018-07-03 11:57:54 +01001332 stream = StringIOWrapper('test_suite_ut.function', data)
1333 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001334 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001335 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001336 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001337 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001338 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001339
1340 def test_incomplete_data(self):
1341 """
Azim Khanb31aa442018-07-03 11:57:54 +01001342 Test GeneratorInputError is raised when test function name
1343 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001344 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001345 """
1346 data = """
1347Diffie-Hellman full exchange #1
1348depends_on:YAHOO
1349"""
Azim Khanb31aa442018-07-03 11:57:54 +01001350 stream = StringIOWrapper('test_suite_ut.function', data)
1351 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001352 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001353 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001354 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001355 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001356 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001357
1358
1359class GenDepCheck(TestCase):
1360 """
Azim Khanb31aa442018-07-03 11:57:54 +01001361 Test suite for gen_dep_check(). It is assumed this function is
1362 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001363 """
1364
1365 def test_gen_dep_check(self):
1366 """
1367 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001368 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001369 """
1370 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001371 case 5:
1372 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001373#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001374 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001375#else
Azim Khand61b8372017-07-10 11:54:01 +01001376 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001377#endif
Azim Khand61b8372017-07-10 11:54:01 +01001378 }
1379 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001380 out = gen_dep_check(5, 'YAHOO')
1381 self.assertEqual(out, expected)
1382
Azim Khanb31aa442018-07-03 11:57:54 +01001383 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001384 """
1385 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001386 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001387 """
1388 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001389 case 5:
1390 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001391#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001392 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001393#else
Azim Khand61b8372017-07-10 11:54:01 +01001394 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001395#endif
Azim Khand61b8372017-07-10 11:54:01 +01001396 }
1397 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001398 out = gen_dep_check(5, '!YAHOO')
1399 self.assertEqual(out, expected)
1400
1401 def test_empty_dependency(self):
1402 """
1403 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001404 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001405 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001406 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001407
1408 def test_negative_dep_id(self):
1409 """
1410 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001411 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001412 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001413 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001414
1415
1416class GenExpCheck(TestCase):
1417 """
Azim Khanb31aa442018-07-03 11:57:54 +01001418 Test suite for gen_expression_check(). It is assumed this function
1419 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001420 """
1421
1422 def test_gen_exp_check(self):
1423 """
1424 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001425 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001426 """
1427 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001428 case 5:
1429 {
1430 *out_value = YAHOO;
1431 }
1432 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001433 out = gen_expression_check(5, 'YAHOO')
1434 self.assertEqual(out, expected)
1435
1436 def test_invalid_expression(self):
1437 """
1438 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001439 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001440 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001441 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001442
1443 def test_negative_exp_id(self):
1444 """
1445 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001446 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001447 """
Azim Khanb31aa442018-07-03 11:57:54 +01001448 self.assertRaises(GeneratorInputError, gen_expression_check,
1449 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001450
1451
Azim Khanb31aa442018-07-03 11:57:54 +01001452class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001453 """
Azim Khanb31aa442018-07-03 11:57:54 +01001454 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001455 """
1456
Azim Khanb31aa442018-07-03 11:57:54 +01001457 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001458 """
Azim Khanb31aa442018-07-03 11:57:54 +01001459 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001460 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001461 """
Azim Khanb31aa442018-07-03 11:57:54 +01001462 stream = StringIOWrapper('test_suite_ut.data', '')
1463 unique_dependencies = []
1464 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001465 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001466 self.assertEqual(len(unique_dependencies), 0)
1467 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001468
1469 def test_unique_dep_ids(self):
1470 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001471
1472 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001473 """
Azim Khanb31aa442018-07-03 11:57:54 +01001474 stream = StringIOWrapper('test_suite_ut.data', '')
1475 unique_dependencies = []
1476 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1477 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001478 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001479 case 0:
1480 {
Azim Khan599cd242017-07-06 17:34:27 +01001481#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001482 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001483#else
Azim Khand61b8372017-07-10 11:54:01 +01001484 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001485#endif
Azim Khand61b8372017-07-10 11:54:01 +01001486 }
1487 break;
1488 case 1:
1489 {
Azim Khan599cd242017-07-06 17:34:27 +01001490#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001491 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001492#else
Azim Khand61b8372017-07-10 11:54:01 +01001493 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001494#endif
Azim Khand61b8372017-07-10 11:54:01 +01001495 }
1496 break;
1497 case 2:
1498 {
Azim Khan599cd242017-07-06 17:34:27 +01001499#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001500 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001501#else
Azim Khand61b8372017-07-10 11:54:01 +01001502 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001503#endif
Azim Khand61b8372017-07-10 11:54:01 +01001504 }
1505 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001506 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001507 self.assertEqual(len(unique_dependencies), 3)
1508 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001509
1510 def test_dep_id_repeat(self):
1511 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001512
1513 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001514 """
Azim Khanb31aa442018-07-03 11:57:54 +01001515 stream = StringIOWrapper('test_suite_ut.data', '')
1516 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001517 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001518 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1519 unique_dependencies)
1520 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1521 unique_dependencies)
1522 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1523 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001524 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001525 case 0:
1526 {
Azim Khan599cd242017-07-06 17:34:27 +01001527#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001528 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001529#else
Azim Khand61b8372017-07-10 11:54:01 +01001530 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001531#endif
Azim Khand61b8372017-07-10 11:54:01 +01001532 }
1533 break;
1534 case 1:
1535 {
Azim Khan599cd242017-07-06 17:34:27 +01001536#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001537 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001538#else
Azim Khand61b8372017-07-10 11:54:01 +01001539 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001540#endif
Azim Khand61b8372017-07-10 11:54:01 +01001541 }
1542 break;
1543 case 2:
1544 {
Azim Khan599cd242017-07-06 17:34:27 +01001545#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001546 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001547#else
Azim Khand61b8372017-07-10 11:54:01 +01001548 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001549#endif
Azim Khand61b8372017-07-10 11:54:01 +01001550 }
1551 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001552 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001553 self.assertEqual(len(unique_dependencies), 3)
1554 self.assertEqual(stream.getvalue(),
1555 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001556
1557
1558class WriteParams(TestCase):
1559 """
1560 Test Suite for testing write_parameters().
1561 """
1562
1563 def test_no_params(self):
1564 """
1565 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001566 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001567 """
Azim Khanb31aa442018-07-03 11:57:54 +01001568 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001569 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001570 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001571 self.assertEqual(len(unique_expressions), 0)
1572 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001573 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001574
1575 def test_no_exp_param(self):
1576 """
1577 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001578 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001579 """
Azim Khanb31aa442018-07-03 11:57:54 +01001580 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001581 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001582 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1583 '0'],
1584 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001585 unique_expressions)
1586 self.assertEqual(len(unique_expressions), 0)
1587 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001588 self.assertEqual(stream.getvalue(),
1589 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001590
1591 def test_hex_format_int_param(self):
1592 """
1593 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001594 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001595 """
Azim Khanb31aa442018-07-03 11:57:54 +01001596 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001597 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001598 expression_code = write_parameters(stream,
1599 ['"Yahoo"', '"abcdef00"', '0xAA'],
1600 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001601 unique_expressions)
1602 self.assertEqual(len(unique_expressions), 0)
1603 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001604 self.assertEqual(stream.getvalue(),
1605 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001606
1607 def test_with_exp_param(self):
1608 """
1609 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001610 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001611 """
Azim Khanb31aa442018-07-03 11:57:54 +01001612 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001613 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001614 expression_code = write_parameters(stream,
1615 ['"Yahoo"', '"abcdef00"', '0',
1616 'MACRO1', 'MACRO2', 'MACRO3'],
1617 ['char*', 'hex', 'int',
1618 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001619 unique_expressions)
1620 self.assertEqual(len(unique_expressions), 3)
1621 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1622 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001623 case 0:
1624 {
1625 *out_value = MACRO1;
1626 }
1627 break;
1628 case 1:
1629 {
1630 *out_value = MACRO2;
1631 }
1632 break;
1633 case 2:
1634 {
1635 *out_value = MACRO3;
1636 }
1637 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001638 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001639 self.assertEqual(stream.getvalue(),
1640 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1641 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001642
Azim Khanb31aa442018-07-03 11:57:54 +01001643 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001644 """
1645 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001646 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001647 """
Azim Khanb31aa442018-07-03 11:57:54 +01001648 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001649 unique_expressions = []
1650 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001651 expression_code += write_parameters(stream,
1652 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1653 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001654 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001655 expression_code += write_parameters(stream,
1656 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1657 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001658 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001659 expression_code += write_parameters(stream,
1660 ['0', 'MACRO3', 'MACRO1'],
1661 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001662 unique_expressions)
1663 self.assertEqual(len(unique_expressions), 3)
1664 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1665 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001666 case 0:
1667 {
1668 *out_value = MACRO1;
1669 }
1670 break;
1671 case 1:
1672 {
1673 *out_value = MACRO2;
1674 }
1675 break;
1676 case 2:
1677 {
1678 *out_value = MACRO3;
1679 }
1680 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001681 self.assertEqual(expression_code, expected_expression_code)
1682 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1683:hex:"abcdef00":exp:1:exp:2
1684:int:0:exp:2:exp:0
1685'''
Azim Khanb31aa442018-07-03 11:57:54 +01001686 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001687
1688
Azim Khanb31aa442018-07-03 11:57:54 +01001689class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001690 """
Azim Khanb31aa442018-07-03 11:57:54 +01001691 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001692 """
Azim Khanb31aa442018-07-03 11:57:54 +01001693 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001694 """
Azim Khanb31aa442018-07-03 11:57:54 +01001695 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001696
1697 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001698 """
Azim Khanb31aa442018-07-03 11:57:54 +01001699 dep_check_code, expression_code = \
1700 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001701 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1702 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1703
Azim Khanb31aa442018-07-03 11:57:54 +01001704 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001705 """
Azim Khanb31aa442018-07-03 11:57:54 +01001706 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001707
1708 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001709 """
Azim Khanb31aa442018-07-03 11:57:54 +01001710 dep_check_code, expression_code = \
1711 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1712 'EXPRESSION_CODE')
1713 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001714#if defined(SUITE_DEP)
1715DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001716#endif
1717'''
1718 expected_expression_code = '''
1719#if defined(SUITE_DEP)
1720EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001721#endif
1722'''
Azim Khanb31aa442018-07-03 11:57:54 +01001723 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001724 self.assertEqual(expression_code, expected_expression_code)
1725
1726 def test_no_dep_no_exp(self):
1727 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001728 Test when there are no dependency and expression code.
1729 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001730 """
Azim Khanb31aa442018-07-03 11:57:54 +01001731 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001732 self.assertEqual(dep_check_code, '')
1733 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001734
1735
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001736class GenFromTestData(TestCase):
1737 """
1738 Test suite for gen_from_test_data()
1739 """
1740
Azim Khanb31aa442018-07-03 11:57:54 +01001741 @staticmethod
1742 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001743 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001744 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001745 def test_intermediate_data_file(func_mock1,
1746 write_parameters_mock,
1747 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001748 """
1749 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001750 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001751 """
1752 data = '''
1753My test
1754depends_on:DEP1
1755func1:0
1756'''
1757 data_f = StringIOWrapper('test_suite_ut.data', data)
1758 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1759 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001760 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001761 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001762 write_dependencies_mock.side_effect = write_dependencies
1763 func_mock1.side_effect = gen_suite_dep_checks
1764 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1765 write_dependencies_mock.assert_called_with(out_data_f,
1766 ['DEP1'], ['DEP1'])
1767 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1768 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001769 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001770 case 0:
1771 {
Azim Khan599cd242017-07-06 17:34:27 +01001772#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001773 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001774#else
Azim Khand61b8372017-07-10 11:54:01 +01001775 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001776#endif
Azim Khand61b8372017-07-10 11:54:01 +01001777 }
1778 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001779 func_mock1.assert_called_with(
1780 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001781
1782 def test_function_not_found(self):
1783 """
1784 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001785 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001786 """
1787 data = '''
1788My test
1789depends_on:DEP1
1790func1:0
1791'''
1792 data_f = StringIOWrapper('test_suite_ut.data', data)
1793 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1794 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001795 suite_dependencies = []
1796 self.assertRaises(GeneratorInputError, gen_from_test_data,
1797 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001798
1799 def test_different_func_args(self):
1800 """
Azim Khanb31aa442018-07-03 11:57:54 +01001801 Test that AssertError is raised when no. of parameters and
1802 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001803 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001804 """
1805 data = '''
1806My test
1807depends_on:DEP1
1808func1:0
1809'''
1810 data_f = StringIOWrapper('test_suite_ut.data', data)
1811 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001812 func_info = {'test_func2': (1, ('int', 'hex'))}
1813 suite_dependencies = []
1814 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1815 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001816
1817 def test_output(self):
1818 """
1819 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001820 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001821 """
1822 data = '''
1823My test 1
1824depends_on:DEP1
1825func1:0:0xfa:MACRO1:MACRO2
1826
1827My test 2
1828depends_on:DEP1:DEP2
1829func2:"yahoo":88:MACRO1
1830'''
1831 data_f = StringIOWrapper('test_suite_ut.data', data)
1832 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001833 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1834 'test_func2': (1, ('char*', 'int', 'int'))}
1835 suite_dependencies = []
1836 dep_check_code, expression_code = \
1837 gen_from_test_data(data_f, out_data_f, func_info,
1838 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001839 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001840 case 0:
1841 {
Azim Khan599cd242017-07-06 17:34:27 +01001842#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001843 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001844#else
Azim Khand61b8372017-07-10 11:54:01 +01001845 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001846#endif
Azim Khand61b8372017-07-10 11:54:01 +01001847 }
1848 break;
1849 case 1:
1850 {
Azim Khan599cd242017-07-06 17:34:27 +01001851#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001852 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001853#else
Azim Khand61b8372017-07-10 11:54:01 +01001854 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001855#endif
Azim Khand61b8372017-07-10 11:54:01 +01001856 }
1857 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001858 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001859depends_on:0
18600:int:0:int:0xfa:exp:0:exp:1
1861
1862My test 2
1863depends_on:0:1
18641:char*:"yahoo":int:88:exp:0
1865
1866'''
1867 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001868 case 0:
1869 {
1870 *out_value = MACRO1;
1871 }
1872 break;
1873 case 1:
1874 {
1875 *out_value = MACRO2;
1876 }
1877 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001878 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001879 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001880 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001881
1882
Azim Khanb31aa442018-07-03 11:57:54 +01001883if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001884 unittest_main()