blob: d8b8cf9791f355738cb11b61a06a439096266480 [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
Gilles Peskine45747a02022-11-18 22:25:18 +0100826 @patch("generate_test_code.gen_dispatch")
827 @patch("generate_test_code.gen_dependencies")
828 @patch("generate_test_code.gen_function_wrapper")
829 @patch("generate_test_code.parse_function_arguments")
830 def test_line_comment_in_block_comment(self, parse_function_arguments_mock,
831 gen_function_wrapper_mock,
832 gen_dependencies_mock,
833 gen_dispatch_mock):
834 """
835 Test with line comment in block comment.
836 :return:
837 """
838 parse_function_arguments_mock.return_value = ([], '', [])
839 gen_function_wrapper_mock.return_value = ''
840 gen_dependencies_mock.side_effect = gen_dependencies
841 gen_dispatch_mock.side_effect = gen_dispatch
842 data = '''
843void func( int x /* // */ )
844{
845 ba ba black sheep
846 have you any wool
847exit:
848 yes sir yes sir
849 3 bags full
850}
851/* END_CASE */
852'''
853 stream = StringIOWrapper('test_suite_ut.function', data)
854 _, _, code, _ = parse_function_code(stream, [], [])
855
856 expected = '''#line 1 "test_suite_ut.function"
857
858void test_func( int x )
859{
860 ba ba black sheep
861 have you any wool
862exit:
863 yes sir yes sir
864 3 bags full
865}
866'''
867 self.assertEqual(code, expected)
868
869 @patch("generate_test_code.gen_dispatch")
870 @patch("generate_test_code.gen_dependencies")
871 @patch("generate_test_code.gen_function_wrapper")
872 @patch("generate_test_code.parse_function_arguments")
873 def test_block_comment_in_line_comment(self, parse_function_arguments_mock,
874 gen_function_wrapper_mock,
875 gen_dependencies_mock,
876 gen_dispatch_mock):
877 """
878 Test with block comment in line comment.
879 :return:
880 """
881 parse_function_arguments_mock.return_value = ([], '', [])
882 gen_function_wrapper_mock.return_value = ''
883 gen_dependencies_mock.side_effect = gen_dependencies
884 gen_dispatch_mock.side_effect = gen_dispatch
885 data = '''
886// /*
887void func( int x )
888{
889 ba ba black sheep
890 have you any wool
891exit:
892 yes sir yes sir
893 3 bags full
894}
895/* END_CASE */
896'''
897 stream = StringIOWrapper('test_suite_ut.function', data)
898 _, _, code, _ = parse_function_code(stream, [], [])
899
900 expected = '''#line 1 "test_suite_ut.function"
901
902
903void test_func( int x )
904{
905 ba ba black sheep
906 have you any wool
907exit:
908 yes sir yes sir
909 3 bags full
910}
911'''
912 self.assertEqual(code, expected)
913
Azim Khan4b543232017-06-30 09:35:21 +0100914
915class ParseFunction(TestCase):
916 """
917 Test Suite for testing parse_functions()
918 """
919
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000920 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000921 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100922 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000923 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100924 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100925 """
Azim Khanb31aa442018-07-03 11:57:54 +0100926 def stop(*_unused):
927 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100928 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000929 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100930 data = '''/* BEGIN_HEADER */
931#include "mbedtls/ecp.h"
932
933#define ECP_PF_UNKNOWN -1
934/* END_HEADER */
935'''
Azim Khanb31aa442018-07-03 11:57:54 +0100936 stream = StringIOWrapper('test_suite_ut.function', data)
937 self.assertRaises(Exception, parse_functions, stream)
938 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100939 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000940
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000941 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000942 def test_begin_helper(self, parse_until_pattern_mock):
943 """
944 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100945 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000946 """
Azim Khanb31aa442018-07-03 11:57:54 +0100947 def stop(*_unused):
948 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000949 raise Exception
950 parse_until_pattern_mock.side_effect = stop
951 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100952void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000953{
Azim Khanb31aa442018-07-03 11:57:54 +0100954 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000955}
956/* END_SUITE_HELPERS */
957'''
Azim Khanb31aa442018-07-03 11:57:54 +0100958 stream = StringIOWrapper('test_suite_ut.function', data)
959 self.assertRaises(Exception, parse_functions, stream)
960 parse_until_pattern_mock.assert_called_with(stream,
961 END_SUITE_HELPERS_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100962 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100963
Azim Khanb31aa442018-07-03 11:57:54 +0100964 @patch("generate_test_code.parse_suite_dependencies")
965 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100966 """
Azim Khanb31aa442018-07-03 11:57:54 +0100967 Test that begin dep is checked and parse_suite_dependencies() is
968 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100969 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100970 """
Azim Khanb31aa442018-07-03 11:57:54 +0100971 def stop(*_unused):
972 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100973 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100974 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100975 data = '''/* BEGIN_DEPENDENCIES
976 * depends_on:MBEDTLS_ECP_C
977 * END_DEPENDENCIES
978 */
979'''
Azim Khanb31aa442018-07-03 11:57:54 +0100980 stream = StringIOWrapper('test_suite_ut.function', data)
981 self.assertRaises(Exception, parse_functions, stream)
982 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +0100983 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100984
Azim Khanb31aa442018-07-03 11:57:54 +0100985 @patch("generate_test_code.parse_function_dependencies")
986 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100987 """
Azim Khanb31aa442018-07-03 11:57:54 +0100988 Test that begin dep is checked and parse_function_dependencies() is
989 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100990 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100991 """
Azim Khanb31aa442018-07-03 11:57:54 +0100992 def stop(*_unused):
993 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100994 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100995 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100996
Azim Khanb31aa442018-07-03 11:57:54 +0100997 dependencies_str = '/* BEGIN_CASE ' \
998 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100999 data = '''%svoid test_func()
1000{
1001}
Azim Khanb31aa442018-07-03 11:57:54 +01001002''' % dependencies_str
1003 stream = StringIOWrapper('test_suite_ut.function', data)
1004 self.assertRaises(Exception, parse_functions, stream)
1005 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +01001006 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +01001007
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001008 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +01001009 @patch("generate_test_code.parse_function_dependencies")
1010 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +01001011 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001012 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001013 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001014 """
Azim Khanb31aa442018-07-03 11:57:54 +01001015 func_mock1.return_value = []
1016 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +01001017{
1018}
1019'''
1020 func_dispatch = '''
1021 test_func_wrapper,
1022'''
Azim Khanb31aa442018-07-03 11:57:54 +01001023 func_mock2.return_value = 'test_func', [],\
1024 in_func_code, func_dispatch
1025 dependencies_str = '/* BEGIN_CASE ' \
1026 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +01001027 data = '''%svoid test_func()
1028{
1029}
Azim Khanb31aa442018-07-03 11:57:54 +01001030''' % dependencies_str
1031 stream = StringIOWrapper('test_suite_ut.function', data)
1032 suite_dependencies, dispatch_code, func_code, func_info = \
1033 parse_functions(stream)
1034 func_mock1.assert_called_with(dependencies_str)
1035 func_mock2.assert_called_with(stream, [], [])
1036 self.assertEqual(stream.line_no, 5)
1037 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +01001038 expected_dispatch_code = '''/* Function Id: 0 */
1039
1040 test_func_wrapper,
1041'''
1042 self.assertEqual(dispatch_code, expected_dispatch_code)
1043 self.assertEqual(func_code, in_func_code)
1044 self.assertEqual(func_info, {'test_func': (0, [])})
1045
1046 def test_parsing(self):
1047 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001048 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001049 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001050 """
1051 data = '''/* BEGIN_HEADER */
1052#include "mbedtls/ecp.h"
1053
1054#define ECP_PF_UNKNOWN -1
1055/* END_HEADER */
1056
1057/* BEGIN_DEPENDENCIES
1058 * depends_on:MBEDTLS_ECP_C
1059 * END_DEPENDENCIES
1060 */
1061
1062/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1063void func1()
1064{
1065}
1066/* END_CASE */
1067
1068/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1069void func2()
1070{
1071}
1072/* END_CASE */
1073'''
Azim Khanb31aa442018-07-03 11:57:54 +01001074 stream = StringIOWrapper('test_suite_ut.function', data)
1075 suite_dependencies, dispatch_code, func_code, func_info = \
1076 parse_functions(stream)
1077 self.assertEqual(stream.line_no, 23)
1078 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +01001079
1080 expected_dispatch_code = '''/* Function Id: 0 */
1081
1082#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
1083 test_func1_wrapper,
1084#else
1085 NULL,
1086#endif
1087/* Function Id: 1 */
1088
1089#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
1090 test_func2_wrapper,
1091#else
1092 NULL,
1093#endif
1094'''
1095 self.assertEqual(dispatch_code, expected_dispatch_code)
1096 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +01001097#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001098#include "mbedtls/ecp.h"
1099
1100#define ECP_PF_UNKNOWN -1
1101#if defined(MBEDTLS_ENTROPY_NV_SEED)
1102#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001103#line 13 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001104void test_func1()
1105{
1106exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001107 ;
Azim Khan4b543232017-06-30 09:35:21 +01001108}
1109
1110void test_func1_wrapper( void ** params )
1111{
1112 (void)params;
1113
1114 test_func1( );
1115}
1116#endif /* MBEDTLS_FS_IO */
1117#endif /* MBEDTLS_ENTROPY_NV_SEED */
1118#if defined(MBEDTLS_ENTROPY_NV_SEED)
1119#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001120#line 19 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001121void test_func2()
1122{
1123exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001124 ;
Azim Khan4b543232017-06-30 09:35:21 +01001125}
1126
1127void test_func2_wrapper( void ** params )
1128{
1129 (void)params;
1130
1131 test_func2( );
1132}
1133#endif /* MBEDTLS_FS_IO */
1134#endif /* MBEDTLS_ENTROPY_NV_SEED */
1135#endif /* MBEDTLS_ECP_C */
1136'''
1137 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001138 self.assertEqual(func_info, {'test_func1': (0, []),
1139 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +01001140
1141 def test_same_function_name(self):
1142 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001143 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001144 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001145 """
1146 data = '''/* BEGIN_HEADER */
1147#include "mbedtls/ecp.h"
1148
1149#define ECP_PF_UNKNOWN -1
1150/* END_HEADER */
1151
1152/* BEGIN_DEPENDENCIES
1153 * depends_on:MBEDTLS_ECP_C
1154 * END_DEPENDENCIES
1155 */
1156
1157/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1158void func()
1159{
1160}
1161/* END_CASE */
1162
1163/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1164void func()
1165{
1166}
1167/* END_CASE */
1168'''
Azim Khanb31aa442018-07-03 11:57:54 +01001169 stream = StringIOWrapper('test_suite_ut.function', data)
1170 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +01001171
1172
Azim Khanb31aa442018-07-03 11:57:54 +01001173class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001174 """
Azim Khan599cd242017-07-06 17:34:27 +01001175 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +01001176 Note: Since escaped_split() output is used to write back to the
1177 intermediate data file. Any escape characters in the input are
1178 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001179 """
1180
1181 def test_invalid_input(self):
1182 """
1183 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001184 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001185 """
1186 self.assertRaises(ValueError, escaped_split, '', 'string')
1187
1188 def test_empty_string(self):
1189 """
Azim Khanb31aa442018-07-03 11:57:54 +01001190 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001191 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001192 """
1193 splits = escaped_split('', ':')
1194 self.assertEqual(splits, [])
1195
1196 def test_no_escape(self):
1197 """
Azim Khanb31aa442018-07-03 11:57:54 +01001198 Test with no escape character. The behaviour should be same as
1199 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001200 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001201 """
Azim Khanb31aa442018-07-03 11:57:54 +01001202 test_str = 'yahoo:google'
1203 splits = escaped_split(test_str, ':')
1204 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001205
1206 def test_escaped_input(self):
1207 """
Azim Khanb31aa442018-07-03 11:57:54 +01001208 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001209 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001210 """
Azim Khanb31aa442018-07-03 11:57:54 +01001211 test_str = r'yahoo\:google:facebook'
1212 splits = escaped_split(test_str, ':')
1213 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001214
1215 def test_escaped_escape(self):
1216 """
Azim Khanb31aa442018-07-03 11:57:54 +01001217 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001218 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001219 """
Azim Khan4084ec72018-07-05 14:20:08 +01001220 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +01001221 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001222 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001223
1224 def test_all_at_once(self):
1225 """
Azim Khanb31aa442018-07-03 11:57:54 +01001226 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001227 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001228 """
Azim Khan4084ec72018-07-05 14:20:08 +01001229 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +01001230 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001231 self.assertEqual(splits, [r'yahoo\\', r'google',
1232 r'facebook\:instagram\\',
1233 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001234
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001235
1236class ParseTestData(TestCase):
1237 """
1238 Test suite for parse test data.
1239 """
1240
1241 def test_parser(self):
1242 """
1243 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001244 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001245 """
1246 data = """
1247Diffie-Hellman full exchange #1
1248dhm_do_dhm:10:"23":10:"5"
1249
1250Diffie-Hellman full exchange #2
1251dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1252
1253Diffie-Hellman full exchange #3
1254dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1255
1256Diffie-Hellman selftest
1257dhm_selftest:
1258"""
Azim Khanb31aa442018-07-03 11:57:54 +01001259 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001260 # List of (name, function_name, dependencies, args)
1261 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001262 test1, test2, test3, test4 = tests
1263 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1264 self.assertEqual(test1[1], 'dhm_do_dhm')
1265 self.assertEqual(test1[2], [])
1266 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001267
Azim Khanb31aa442018-07-03 11:57:54 +01001268 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1269 self.assertEqual(test2[1], 'dhm_do_dhm')
1270 self.assertEqual(test2[2], [])
1271 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1272 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001273
Azim Khanb31aa442018-07-03 11:57:54 +01001274 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1275 self.assertEqual(test3[1], 'dhm_do_dhm')
1276 self.assertEqual(test3[2], [])
1277 self.assertEqual(test3[3], ['10',
1278 '"9345098382739712938719287391879381271"',
1279 '10',
1280 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001281
Azim Khanb31aa442018-07-03 11:57:54 +01001282 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1283 self.assertEqual(test4[1], 'dhm_selftest')
1284 self.assertEqual(test4[2], [])
1285 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001286
1287 def test_with_dependencies(self):
1288 """
1289 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001290 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001291 """
1292 data = """
1293Diffie-Hellman full exchange #1
1294depends_on:YAHOO
1295dhm_do_dhm:10:"23":10:"5"
1296
1297Diffie-Hellman full exchange #2
1298dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1299
1300"""
Azim Khanb31aa442018-07-03 11:57:54 +01001301 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001302 # List of (name, function_name, dependencies, args)
1303 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001304 test1, test2 = tests
1305 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1306 self.assertEqual(test1[1], 'dhm_do_dhm')
1307 self.assertEqual(test1[2], ['YAHOO'])
1308 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001309
Azim Khanb31aa442018-07-03 11:57:54 +01001310 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1311 self.assertEqual(test2[1], 'dhm_do_dhm')
1312 self.assertEqual(test2[2], [])
1313 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1314 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001315
1316 def test_no_args(self):
1317 """
Azim Khanb31aa442018-07-03 11:57:54 +01001318 Test GeneratorInputError is raised when test function name and
1319 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001320 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001321 """
1322 data = """
1323Diffie-Hellman full exchange #1
1324depends_on:YAHOO
1325
1326
1327Diffie-Hellman full exchange #2
1328dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1329
1330"""
Azim Khanb31aa442018-07-03 11:57:54 +01001331 stream = StringIOWrapper('test_suite_ut.function', data)
1332 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001333 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001334 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001335 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001336 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001337 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001338
1339 def test_incomplete_data(self):
1340 """
Azim Khanb31aa442018-07-03 11:57:54 +01001341 Test GeneratorInputError is raised when test function name
1342 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001343 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001344 """
1345 data = """
1346Diffie-Hellman full exchange #1
1347depends_on:YAHOO
1348"""
Azim Khanb31aa442018-07-03 11:57:54 +01001349 stream = StringIOWrapper('test_suite_ut.function', data)
1350 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001351 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001352 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001353 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001354 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001355 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001356
1357
1358class GenDepCheck(TestCase):
1359 """
Azim Khanb31aa442018-07-03 11:57:54 +01001360 Test suite for gen_dep_check(). It is assumed this function is
1361 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001362 """
1363
1364 def test_gen_dep_check(self):
1365 """
1366 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001367 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001368 """
1369 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001370 case 5:
1371 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001372#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001373 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001374#else
Azim Khand61b8372017-07-10 11:54:01 +01001375 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001376#endif
Azim Khand61b8372017-07-10 11:54:01 +01001377 }
1378 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001379 out = gen_dep_check(5, 'YAHOO')
1380 self.assertEqual(out, expected)
1381
Azim Khanb31aa442018-07-03 11:57:54 +01001382 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001383 """
1384 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001385 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001386 """
1387 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001388 case 5:
1389 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001390#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001391 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001392#else
Azim Khand61b8372017-07-10 11:54:01 +01001393 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001394#endif
Azim Khand61b8372017-07-10 11:54:01 +01001395 }
1396 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001397 out = gen_dep_check(5, '!YAHOO')
1398 self.assertEqual(out, expected)
1399
1400 def test_empty_dependency(self):
1401 """
1402 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001403 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001404 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001405 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001406
1407 def test_negative_dep_id(self):
1408 """
1409 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001410 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001411 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001412 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001413
1414
1415class GenExpCheck(TestCase):
1416 """
Azim Khanb31aa442018-07-03 11:57:54 +01001417 Test suite for gen_expression_check(). It is assumed this function
1418 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001419 """
1420
1421 def test_gen_exp_check(self):
1422 """
1423 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001424 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001425 """
1426 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001427 case 5:
1428 {
1429 *out_value = YAHOO;
1430 }
1431 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001432 out = gen_expression_check(5, 'YAHOO')
1433 self.assertEqual(out, expected)
1434
1435 def test_invalid_expression(self):
1436 """
1437 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001438 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001439 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001440 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001441
1442 def test_negative_exp_id(self):
1443 """
1444 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001445 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001446 """
Azim Khanb31aa442018-07-03 11:57:54 +01001447 self.assertRaises(GeneratorInputError, gen_expression_check,
1448 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001449
1450
Azim Khanb31aa442018-07-03 11:57:54 +01001451class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001452 """
Azim Khanb31aa442018-07-03 11:57:54 +01001453 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001454 """
1455
Azim Khanb31aa442018-07-03 11:57:54 +01001456 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001457 """
Azim Khanb31aa442018-07-03 11:57:54 +01001458 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001459 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001460 """
Azim Khanb31aa442018-07-03 11:57:54 +01001461 stream = StringIOWrapper('test_suite_ut.data', '')
1462 unique_dependencies = []
1463 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001464 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001465 self.assertEqual(len(unique_dependencies), 0)
1466 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001467
1468 def test_unique_dep_ids(self):
1469 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001470
1471 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001472 """
Azim Khanb31aa442018-07-03 11:57:54 +01001473 stream = StringIOWrapper('test_suite_ut.data', '')
1474 unique_dependencies = []
1475 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1476 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001477 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001478 case 0:
1479 {
Azim Khan599cd242017-07-06 17:34:27 +01001480#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001481 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001482#else
Azim Khand61b8372017-07-10 11:54:01 +01001483 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001484#endif
Azim Khand61b8372017-07-10 11:54:01 +01001485 }
1486 break;
1487 case 1:
1488 {
Azim Khan599cd242017-07-06 17:34:27 +01001489#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001490 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001491#else
Azim Khand61b8372017-07-10 11:54:01 +01001492 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001493#endif
Azim Khand61b8372017-07-10 11:54:01 +01001494 }
1495 break;
1496 case 2:
1497 {
Azim Khan599cd242017-07-06 17:34:27 +01001498#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001499 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001500#else
Azim Khand61b8372017-07-10 11:54:01 +01001501 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001502#endif
Azim Khand61b8372017-07-10 11:54:01 +01001503 }
1504 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001505 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001506 self.assertEqual(len(unique_dependencies), 3)
1507 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001508
1509 def test_dep_id_repeat(self):
1510 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001511
1512 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001513 """
Azim Khanb31aa442018-07-03 11:57:54 +01001514 stream = StringIOWrapper('test_suite_ut.data', '')
1515 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001516 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001517 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1518 unique_dependencies)
1519 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1520 unique_dependencies)
1521 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1522 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001523 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001524 case 0:
1525 {
Azim Khan599cd242017-07-06 17:34:27 +01001526#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001527 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001528#else
Azim Khand61b8372017-07-10 11:54:01 +01001529 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001530#endif
Azim Khand61b8372017-07-10 11:54:01 +01001531 }
1532 break;
1533 case 1:
1534 {
Azim Khan599cd242017-07-06 17:34:27 +01001535#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001536 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001537#else
Azim Khand61b8372017-07-10 11:54:01 +01001538 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001539#endif
Azim Khand61b8372017-07-10 11:54:01 +01001540 }
1541 break;
1542 case 2:
1543 {
Azim Khan599cd242017-07-06 17:34:27 +01001544#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001545 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001546#else
Azim Khand61b8372017-07-10 11:54:01 +01001547 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001548#endif
Azim Khand61b8372017-07-10 11:54:01 +01001549 }
1550 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001551 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001552 self.assertEqual(len(unique_dependencies), 3)
1553 self.assertEqual(stream.getvalue(),
1554 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001555
1556
1557class WriteParams(TestCase):
1558 """
1559 Test Suite for testing write_parameters().
1560 """
1561
1562 def test_no_params(self):
1563 """
1564 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001565 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001566 """
Azim Khanb31aa442018-07-03 11:57:54 +01001567 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001568 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001569 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001570 self.assertEqual(len(unique_expressions), 0)
1571 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001572 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001573
1574 def test_no_exp_param(self):
1575 """
1576 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001577 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001578 """
Azim Khanb31aa442018-07-03 11:57:54 +01001579 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001580 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001581 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1582 '0'],
1583 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001584 unique_expressions)
1585 self.assertEqual(len(unique_expressions), 0)
1586 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001587 self.assertEqual(stream.getvalue(),
1588 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001589
1590 def test_hex_format_int_param(self):
1591 """
1592 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001593 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001594 """
Azim Khanb31aa442018-07-03 11:57:54 +01001595 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001596 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001597 expression_code = write_parameters(stream,
1598 ['"Yahoo"', '"abcdef00"', '0xAA'],
1599 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001600 unique_expressions)
1601 self.assertEqual(len(unique_expressions), 0)
1602 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001603 self.assertEqual(stream.getvalue(),
1604 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001605
1606 def test_with_exp_param(self):
1607 """
1608 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001609 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001610 """
Azim Khanb31aa442018-07-03 11:57:54 +01001611 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001612 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001613 expression_code = write_parameters(stream,
1614 ['"Yahoo"', '"abcdef00"', '0',
1615 'MACRO1', 'MACRO2', 'MACRO3'],
1616 ['char*', 'hex', 'int',
1617 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001618 unique_expressions)
1619 self.assertEqual(len(unique_expressions), 3)
1620 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1621 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001622 case 0:
1623 {
1624 *out_value = MACRO1;
1625 }
1626 break;
1627 case 1:
1628 {
1629 *out_value = MACRO2;
1630 }
1631 break;
1632 case 2:
1633 {
1634 *out_value = MACRO3;
1635 }
1636 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001637 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001638 self.assertEqual(stream.getvalue(),
1639 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1640 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001641
Azim Khanb31aa442018-07-03 11:57:54 +01001642 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001643 """
1644 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001645 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001646 """
Azim Khanb31aa442018-07-03 11:57:54 +01001647 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001648 unique_expressions = []
1649 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001650 expression_code += write_parameters(stream,
1651 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1652 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001653 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001654 expression_code += write_parameters(stream,
1655 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1656 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001657 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001658 expression_code += write_parameters(stream,
1659 ['0', 'MACRO3', 'MACRO1'],
1660 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001661 unique_expressions)
1662 self.assertEqual(len(unique_expressions), 3)
1663 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1664 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001665 case 0:
1666 {
1667 *out_value = MACRO1;
1668 }
1669 break;
1670 case 1:
1671 {
1672 *out_value = MACRO2;
1673 }
1674 break;
1675 case 2:
1676 {
1677 *out_value = MACRO3;
1678 }
1679 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001680 self.assertEqual(expression_code, expected_expression_code)
1681 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1682:hex:"abcdef00":exp:1:exp:2
1683:int:0:exp:2:exp:0
1684'''
Azim Khanb31aa442018-07-03 11:57:54 +01001685 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001686
1687
Azim Khanb31aa442018-07-03 11:57:54 +01001688class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001689 """
Azim Khanb31aa442018-07-03 11:57:54 +01001690 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001691 """
Azim Khanb31aa442018-07-03 11:57:54 +01001692 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001693 """
Azim Khanb31aa442018-07-03 11:57:54 +01001694 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001695
1696 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001697 """
Azim Khanb31aa442018-07-03 11:57:54 +01001698 dep_check_code, expression_code = \
1699 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001700 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1701 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1702
Azim Khanb31aa442018-07-03 11:57:54 +01001703 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001704 """
Azim Khanb31aa442018-07-03 11:57:54 +01001705 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001706
1707 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001708 """
Azim Khanb31aa442018-07-03 11:57:54 +01001709 dep_check_code, expression_code = \
1710 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1711 'EXPRESSION_CODE')
1712 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001713#if defined(SUITE_DEP)
1714DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001715#endif
1716'''
1717 expected_expression_code = '''
1718#if defined(SUITE_DEP)
1719EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001720#endif
1721'''
Azim Khanb31aa442018-07-03 11:57:54 +01001722 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001723 self.assertEqual(expression_code, expected_expression_code)
1724
1725 def test_no_dep_no_exp(self):
1726 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001727 Test when there are no dependency and expression code.
1728 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001729 """
Azim Khanb31aa442018-07-03 11:57:54 +01001730 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001731 self.assertEqual(dep_check_code, '')
1732 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001733
1734
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001735class GenFromTestData(TestCase):
1736 """
1737 Test suite for gen_from_test_data()
1738 """
1739
Azim Khanb31aa442018-07-03 11:57:54 +01001740 @staticmethod
1741 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001742 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001743 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001744 def test_intermediate_data_file(func_mock1,
1745 write_parameters_mock,
1746 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001747 """
1748 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001749 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001750 """
1751 data = '''
1752My test
1753depends_on:DEP1
1754func1:0
1755'''
1756 data_f = StringIOWrapper('test_suite_ut.data', data)
1757 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1758 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001759 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001760 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001761 write_dependencies_mock.side_effect = write_dependencies
1762 func_mock1.side_effect = gen_suite_dep_checks
1763 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1764 write_dependencies_mock.assert_called_with(out_data_f,
1765 ['DEP1'], ['DEP1'])
1766 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1767 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001768 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001769 case 0:
1770 {
Azim Khan599cd242017-07-06 17:34:27 +01001771#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001772 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001773#else
Azim Khand61b8372017-07-10 11:54:01 +01001774 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001775#endif
Azim Khand61b8372017-07-10 11:54:01 +01001776 }
1777 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001778 func_mock1.assert_called_with(
1779 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001780
1781 def test_function_not_found(self):
1782 """
1783 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001784 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001785 """
1786 data = '''
1787My test
1788depends_on:DEP1
1789func1:0
1790'''
1791 data_f = StringIOWrapper('test_suite_ut.data', data)
1792 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1793 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001794 suite_dependencies = []
1795 self.assertRaises(GeneratorInputError, gen_from_test_data,
1796 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001797
1798 def test_different_func_args(self):
1799 """
Azim Khanb31aa442018-07-03 11:57:54 +01001800 Test that AssertError is raised when no. of parameters and
1801 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001802 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001803 """
1804 data = '''
1805My test
1806depends_on:DEP1
1807func1:0
1808'''
1809 data_f = StringIOWrapper('test_suite_ut.data', data)
1810 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001811 func_info = {'test_func2': (1, ('int', 'hex'))}
1812 suite_dependencies = []
1813 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1814 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001815
1816 def test_output(self):
1817 """
1818 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001819 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001820 """
1821 data = '''
1822My test 1
1823depends_on:DEP1
1824func1:0:0xfa:MACRO1:MACRO2
1825
1826My test 2
1827depends_on:DEP1:DEP2
1828func2:"yahoo":88:MACRO1
1829'''
1830 data_f = StringIOWrapper('test_suite_ut.data', data)
1831 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001832 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1833 'test_func2': (1, ('char*', 'int', 'int'))}
1834 suite_dependencies = []
1835 dep_check_code, expression_code = \
1836 gen_from_test_data(data_f, out_data_f, func_info,
1837 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001838 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001839 case 0:
1840 {
Azim Khan599cd242017-07-06 17:34:27 +01001841#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001842 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001843#else
Azim Khand61b8372017-07-10 11:54:01 +01001844 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001845#endif
Azim Khand61b8372017-07-10 11:54:01 +01001846 }
1847 break;
1848 case 1:
1849 {
Azim Khan599cd242017-07-06 17:34:27 +01001850#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001851 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001852#else
Azim Khand61b8372017-07-10 11:54:01 +01001853 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001854#endif
Azim Khand61b8372017-07-10 11:54:01 +01001855 }
1856 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001857 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001858depends_on:0
18590:int:0:int:0xfa:exp:0:exp:1
1860
1861My test 2
1862depends_on:0:1
18631:char*:"yahoo":int:88:exp:0
1864
1865'''
1866 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001867 case 0:
1868 {
1869 *out_value = MACRO1;
1870 }
1871 break;
1872 case 1:
1873 {
1874 *out_value = MACRO2;
1875 }
1876 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001877 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001878 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001879 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001880
1881
Azim Khanb31aa442018-07-03 11:57:54 +01001882if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001883 unittest_main()