blob: c198ad0e356bec6ff4cb9c0df38ec25d77cd1114 [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, '')
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100488 self.assertEqual(arg_dispatch,
489 ['(char *) params[0]',
490 '((mbedtls_test_argument_t*)params[1])->s32',
491 '((mbedtls_test_argument_t*)params[2])->s32'])
Azim Khan4b543232017-06-30 09:35:21 +0100492
493 def test_hex_params(self):
494 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100495 Test hex parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100496 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100497 """
Azim Khan5fcca462018-06-29 11:05:32 +0100498 line = 'void entropy_threshold( char * a, data_t * h, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100499 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100500 self.assertEqual(args, ['char*', 'hex', 'int'])
Azim Khanb31aa442018-07-03 11:57:54 +0100501 self.assertEqual(local,
Azim Khan4084ec72018-07-05 14:20:08 +0100502 ' data_t data1 = {(uint8_t *) params[1], '
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100503 '((mbedtls_test_argument_t*)params[2])->len};\n')
Azim Khanb31aa442018-07-03 11:57:54 +0100504 self.assertEqual(arg_dispatch, ['(char *) params[0]',
Azim Khan4084ec72018-07-05 14:20:08 +0100505 '&data1',
Gilles Peskineb3c2eaf2022-12-04 13:10:55 +0100506 '((mbedtls_test_argument_t*)params[3])->s32'])
Azim Khan4b543232017-06-30 09:35:21 +0100507
Azim Khan4b543232017-06-30 09:35:21 +0100508 def test_unsupported_arg(self):
509 """
Azim Khan5fcca462018-06-29 11:05:32 +0100510 Test unsupported arguments (not among int, char * and data_t)
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100511 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100512 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100513 line = 'void entropy_threshold( char * a, data_t * h, char result )'
514 self.assertRaises(ValueError, parse_function_arguments, line)
Azim Khan4b543232017-06-30 09:35:21 +0100515
516 def test_no_params(self):
517 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100518 Test no parameters.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100519 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100520 """
521 line = 'void entropy_threshold()'
Azim Khanfcdf6852018-07-05 17:31:46 +0100522 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100523 self.assertEqual(args, [])
524 self.assertEqual(local, '')
525 self.assertEqual(arg_dispatch, [])
526
527
528class ParseFunctionCode(TestCase):
529 """
530 Test suite for testing parse_function_code()
531 """
532
533 def test_no_function(self):
534 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100535 Test no test function found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100536 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100537 """
538 data = '''
539No
540test
541function
542'''
Azim Khanb31aa442018-07-03 11:57:54 +0100543 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100544 err_msg = 'file: test_suite_ut.function - Test functions not found!'
Gilles Peskine38b66df2020-08-12 02:11:38 +0200545 self.assertRaisesRegex(GeneratorInputError, err_msg,
546 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100547
548 def test_no_end_case_comment(self):
549 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100550 Test missing end case.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100551 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100552 """
553 data = '''
554void test_func()
555{
556}
557'''
Azim Khanb31aa442018-07-03 11:57:54 +0100558 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100559 err_msg = r'file: test_suite_ut.function - '\
560 'end case pattern .*? not found!'
Gilles Peskine38b66df2020-08-12 02:11:38 +0200561 self.assertRaisesRegex(GeneratorInputError, err_msg,
562 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100563
Azim Khanfcdf6852018-07-05 17:31:46 +0100564 @patch("generate_test_code.parse_function_arguments")
Azim Khanb31aa442018-07-03 11:57:54 +0100565 def test_function_called(self,
Azim Khanfcdf6852018-07-05 17:31:46 +0100566 parse_function_arguments_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100567 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100568 Test parse_function_code()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100569 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100570 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100571 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100572 data = '''
573void test_func()
574{
575}
576'''
Azim Khanb31aa442018-07-03 11:57:54 +0100577 stream = StringIOWrapper('test_suite_ut.function', data)
578 self.assertRaises(GeneratorInputError, parse_function_code,
579 stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100580 self.assertTrue(parse_function_arguments_mock.called)
581 parse_function_arguments_mock.assert_called_with('void test_func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100582
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000583 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100584 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000585 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100586 @patch("generate_test_code.parse_function_arguments")
587 def test_return(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100588 gen_function_wrapper_mock,
589 gen_dependencies_mock,
590 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100591 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100592 Test generated code.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100593 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100594 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100595 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100596 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100597 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100598 gen_dispatch_mock.side_effect = gen_dispatch
599 data = '''
600void func()
601{
602 ba ba black sheep
603 have you any wool
604}
605/* END_CASE */
606'''
Azim Khanb31aa442018-07-03 11:57:54 +0100607 stream = StringIOWrapper('test_suite_ut.function', data)
608 name, arg, code, dispatch_code = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100609
Azim Khanfcdf6852018-07-05 17:31:46 +0100610 self.assertTrue(parse_function_arguments_mock.called)
611 parse_function_arguments_mock.assert_called_with('void func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100612 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
613 self.assertEqual(name, 'test_func')
614 self.assertEqual(arg, [])
Azim Khan4084ec72018-07-05 14:20:08 +0100615 expected = '''#line 1 "test_suite_ut.function"
616
Azim Khan4b543232017-06-30 09:35:21 +0100617void test_func()
618{
619 ba ba black sheep
620 have you any wool
621exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100622 ;
Azim Khan4b543232017-06-30 09:35:21 +0100623}
624'''
625 self.assertEqual(code, expected)
626 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
627
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000628 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100629 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000630 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100631 @patch("generate_test_code.parse_function_arguments")
632 def test_with_exit_label(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100633 gen_function_wrapper_mock,
634 gen_dependencies_mock,
635 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100636 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100637 Test when exit label is present.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100638 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100639 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100640 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100641 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100642 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100643 gen_dispatch_mock.side_effect = gen_dispatch
644 data = '''
645void func()
646{
647 ba ba black sheep
648 have you any wool
649exit:
650 yes sir yes sir
651 3 bags full
652}
653/* END_CASE */
654'''
Azim Khanb31aa442018-07-03 11:57:54 +0100655 stream = StringIOWrapper('test_suite_ut.function', data)
656 _, _, code, _ = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100657
Azim Khan4084ec72018-07-05 14:20:08 +0100658 expected = '''#line 1 "test_suite_ut.function"
659
Azim Khan4b543232017-06-30 09:35:21 +0100660void test_func()
661{
662 ba ba black sheep
663 have you any wool
664exit:
665 yes sir yes sir
666 3 bags full
667}
668'''
669 self.assertEqual(code, expected)
670
Azim Khanfcdf6852018-07-05 17:31:46 +0100671 def test_non_void_function(self):
672 """
673 Test invalid signature (non void).
674 :return:
675 """
676 data = 'int entropy_threshold( char * a, data_t * h, int result )'
677 err_msg = 'file: test_suite_ut.function - Test functions not found!'
678 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine38b66df2020-08-12 02:11:38 +0200679 self.assertRaisesRegex(GeneratorInputError, err_msg,
680 parse_function_code, stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100681
682 @patch("generate_test_code.gen_dispatch")
683 @patch("generate_test_code.gen_dependencies")
684 @patch("generate_test_code.gen_function_wrapper")
685 @patch("generate_test_code.parse_function_arguments")
Gilles Peskine0cf42202022-11-11 16:36:51 +0100686 def test_function_name_on_newline(self, parse_function_arguments_mock,
687 gen_function_wrapper_mock,
688 gen_dependencies_mock,
689 gen_dispatch_mock):
Azim Khanfcdf6852018-07-05 17:31:46 +0100690 """
Gilles Peskine0cf42202022-11-11 16:36:51 +0100691 Test with line break before the function name.
Azim Khanfcdf6852018-07-05 17:31:46 +0100692 :return:
693 """
694 parse_function_arguments_mock.return_value = ([], '', [])
695 gen_function_wrapper_mock.return_value = ''
696 gen_dependencies_mock.side_effect = gen_dependencies
697 gen_dispatch_mock.side_effect = gen_dispatch
698 data = '''
699void
700
701
702func()
703{
704 ba ba black sheep
705 have you any wool
706exit:
707 yes sir yes sir
708 3 bags full
709}
710/* END_CASE */
711'''
712 stream = StringIOWrapper('test_suite_ut.function', data)
713 _, _, code, _ = parse_function_code(stream, [], [])
714
715 expected = '''#line 1 "test_suite_ut.function"
716
717void
718
719
720test_func()
721{
722 ba ba black sheep
723 have you any wool
724exit:
725 yes sir yes sir
726 3 bags full
727}
728'''
729 self.assertEqual(code, expected)
730
Gilles Peskine07510f52022-11-11 16:37:16 +0100731 @patch("generate_test_code.gen_dispatch")
732 @patch("generate_test_code.gen_dependencies")
733 @patch("generate_test_code.gen_function_wrapper")
734 @patch("generate_test_code.parse_function_arguments")
735 def test_case_starting_with_comment(self, parse_function_arguments_mock,
736 gen_function_wrapper_mock,
737 gen_dependencies_mock,
738 gen_dispatch_mock):
739 """
740 Test with comments before the function signature
741 :return:
742 """
743 parse_function_arguments_mock.return_value = ([], '', [])
744 gen_function_wrapper_mock.return_value = ''
745 gen_dependencies_mock.side_effect = gen_dependencies
746 gen_dispatch_mock.side_effect = gen_dispatch
747 data = '''/* comment */
748/* more
749 * comment */
Gilles Peskine18f48eb2022-11-18 22:24:56 +0100750// this is\\
751still \\
Gilles Peskine07510f52022-11-11 16:37:16 +0100752a comment
753void func()
754{
755 ba ba black sheep
756 have you any wool
757exit:
758 yes sir yes sir
759 3 bags full
760}
761/* END_CASE */
762'''
763 stream = StringIOWrapper('test_suite_ut.function', data)
764 _, _, code, _ = parse_function_code(stream, [], [])
765
766 expected = '''#line 1 "test_suite_ut.function"
767
768
769
Gilles Peskined8c08032022-11-29 22:03:32 +0100770
Gilles Peskine18f70282022-11-30 16:38:49 +0100771
772
Gilles Peskine07510f52022-11-11 16:37:16 +0100773void test_func()
774{
775 ba ba black sheep
776 have you any wool
777exit:
778 yes sir yes sir
779 3 bags full
780}
781'''
782 self.assertEqual(code, expected)
783
784 @patch("generate_test_code.gen_dispatch")
785 @patch("generate_test_code.gen_dependencies")
786 @patch("generate_test_code.gen_function_wrapper")
787 @patch("generate_test_code.parse_function_arguments")
788 def test_comment_in_prototype(self, parse_function_arguments_mock,
789 gen_function_wrapper_mock,
790 gen_dependencies_mock,
791 gen_dispatch_mock):
792 """
793 Test with comments in the function prototype
794 :return:
795 """
796 parse_function_arguments_mock.return_value = ([], '', [])
797 gen_function_wrapper_mock.return_value = ''
798 gen_dependencies_mock.side_effect = gen_dependencies
799 gen_dispatch_mock.side_effect = gen_dispatch
800 data = '''
801void func( int x, // (line \\
802 comment)
803 int y /* lone closing parenthesis) */ )
804{
805 ba ba black sheep
806 have you any wool
807exit:
808 yes sir yes sir
809 3 bags full
810}
811/* END_CASE */
812'''
813 stream = StringIOWrapper('test_suite_ut.function', data)
814 _, _, code, _ = parse_function_code(stream, [], [])
815
816 expected = '''#line 1 "test_suite_ut.function"
817
818void test_func( int x,
Gilles Peskine18f70282022-11-30 16:38:49 +0100819
Gilles Peskine07510f52022-11-11 16:37:16 +0100820 int y )
821{
822 ba ba black sheep
823 have you any wool
824exit:
825 yes sir yes sir
826 3 bags full
827}
828'''
829 self.assertEqual(code, expected)
830
Gilles Peskine45747a02022-11-18 22:25:18 +0100831 @patch("generate_test_code.gen_dispatch")
832 @patch("generate_test_code.gen_dependencies")
833 @patch("generate_test_code.gen_function_wrapper")
834 @patch("generate_test_code.parse_function_arguments")
835 def test_line_comment_in_block_comment(self, parse_function_arguments_mock,
836 gen_function_wrapper_mock,
837 gen_dependencies_mock,
838 gen_dispatch_mock):
839 """
840 Test with line comment in block comment.
841 :return:
842 """
843 parse_function_arguments_mock.return_value = ([], '', [])
844 gen_function_wrapper_mock.return_value = ''
845 gen_dependencies_mock.side_effect = gen_dependencies
846 gen_dispatch_mock.side_effect = gen_dispatch
847 data = '''
848void func( int x /* // */ )
849{
850 ba ba black sheep
851 have you any wool
852exit:
853 yes sir yes sir
854 3 bags full
855}
856/* END_CASE */
857'''
858 stream = StringIOWrapper('test_suite_ut.function', data)
859 _, _, code, _ = parse_function_code(stream, [], [])
860
861 expected = '''#line 1 "test_suite_ut.function"
862
863void test_func( int x )
864{
865 ba ba black sheep
866 have you any wool
867exit:
868 yes sir yes sir
869 3 bags full
870}
871'''
872 self.assertEqual(code, expected)
873
874 @patch("generate_test_code.gen_dispatch")
875 @patch("generate_test_code.gen_dependencies")
876 @patch("generate_test_code.gen_function_wrapper")
877 @patch("generate_test_code.parse_function_arguments")
878 def test_block_comment_in_line_comment(self, parse_function_arguments_mock,
879 gen_function_wrapper_mock,
880 gen_dependencies_mock,
881 gen_dispatch_mock):
882 """
883 Test with block comment in line comment.
884 :return:
885 """
886 parse_function_arguments_mock.return_value = ([], '', [])
887 gen_function_wrapper_mock.return_value = ''
888 gen_dependencies_mock.side_effect = gen_dependencies
889 gen_dispatch_mock.side_effect = gen_dispatch
890 data = '''
891// /*
892void func( int x )
893{
894 ba ba black sheep
895 have you any wool
896exit:
897 yes sir yes sir
898 3 bags full
899}
900/* END_CASE */
901'''
902 stream = StringIOWrapper('test_suite_ut.function', data)
903 _, _, code, _ = parse_function_code(stream, [], [])
904
905 expected = '''#line 1 "test_suite_ut.function"
906
907
908void test_func( int x )
909{
910 ba ba black sheep
911 have you any wool
912exit:
913 yes sir yes sir
914 3 bags full
915}
916'''
917 self.assertEqual(code, expected)
918
Azim Khan4b543232017-06-30 09:35:21 +0100919
920class ParseFunction(TestCase):
921 """
922 Test Suite for testing parse_functions()
923 """
924
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000925 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000926 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100927 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000928 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100929 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100930 """
Azim Khanb31aa442018-07-03 11:57:54 +0100931 def stop(*_unused):
932 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100933 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000934 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100935 data = '''/* BEGIN_HEADER */
936#include "mbedtls/ecp.h"
937
938#define ECP_PF_UNKNOWN -1
939/* END_HEADER */
940'''
Azim Khanb31aa442018-07-03 11:57:54 +0100941 stream = StringIOWrapper('test_suite_ut.function', data)
942 self.assertRaises(Exception, parse_functions, stream)
943 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100944 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000945
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000946 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000947 def test_begin_helper(self, parse_until_pattern_mock):
948 """
949 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100950 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000951 """
Azim Khanb31aa442018-07-03 11:57:54 +0100952 def stop(*_unused):
953 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000954 raise Exception
955 parse_until_pattern_mock.side_effect = stop
956 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100957void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000958{
Azim Khanb31aa442018-07-03 11:57:54 +0100959 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000960}
961/* END_SUITE_HELPERS */
962'''
Azim Khanb31aa442018-07-03 11:57:54 +0100963 stream = StringIOWrapper('test_suite_ut.function', data)
964 self.assertRaises(Exception, parse_functions, stream)
965 parse_until_pattern_mock.assert_called_with(stream,
966 END_SUITE_HELPERS_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100967 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100968
Azim Khanb31aa442018-07-03 11:57:54 +0100969 @patch("generate_test_code.parse_suite_dependencies")
970 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100971 """
Azim Khanb31aa442018-07-03 11:57:54 +0100972 Test that begin dep is checked and parse_suite_dependencies() is
973 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100974 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100975 """
Azim Khanb31aa442018-07-03 11:57:54 +0100976 def stop(*_unused):
977 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100978 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100979 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100980 data = '''/* BEGIN_DEPENDENCIES
981 * depends_on:MBEDTLS_ECP_C
982 * END_DEPENDENCIES
983 */
984'''
Azim Khanb31aa442018-07-03 11:57:54 +0100985 stream = StringIOWrapper('test_suite_ut.function', data)
986 self.assertRaises(Exception, parse_functions, stream)
987 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +0100988 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100989
Azim Khanb31aa442018-07-03 11:57:54 +0100990 @patch("generate_test_code.parse_function_dependencies")
991 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100992 """
Azim Khanb31aa442018-07-03 11:57:54 +0100993 Test that begin dep is checked and parse_function_dependencies() is
994 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100995 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100996 """
Azim Khanb31aa442018-07-03 11:57:54 +0100997 def stop(*_unused):
998 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100999 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +01001000 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +01001001
Azim Khanb31aa442018-07-03 11:57:54 +01001002 dependencies_str = '/* BEGIN_CASE ' \
1003 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +01001004 data = '''%svoid test_func()
1005{
1006}
Azim Khanb31aa442018-07-03 11:57:54 +01001007''' % dependencies_str
1008 stream = StringIOWrapper('test_suite_ut.function', data)
1009 self.assertRaises(Exception, parse_functions, stream)
1010 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +01001011 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +01001012
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001013 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +01001014 @patch("generate_test_code.parse_function_dependencies")
1015 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +01001016 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001017 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001018 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001019 """
Azim Khanb31aa442018-07-03 11:57:54 +01001020 func_mock1.return_value = []
1021 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +01001022{
1023}
1024'''
1025 func_dispatch = '''
1026 test_func_wrapper,
1027'''
Azim Khanb31aa442018-07-03 11:57:54 +01001028 func_mock2.return_value = 'test_func', [],\
1029 in_func_code, func_dispatch
1030 dependencies_str = '/* BEGIN_CASE ' \
1031 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +01001032 data = '''%svoid test_func()
1033{
1034}
Azim Khanb31aa442018-07-03 11:57:54 +01001035''' % dependencies_str
1036 stream = StringIOWrapper('test_suite_ut.function', data)
1037 suite_dependencies, dispatch_code, func_code, func_info = \
1038 parse_functions(stream)
1039 func_mock1.assert_called_with(dependencies_str)
1040 func_mock2.assert_called_with(stream, [], [])
1041 self.assertEqual(stream.line_no, 5)
1042 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +01001043 expected_dispatch_code = '''/* Function Id: 0 */
1044
1045 test_func_wrapper,
1046'''
1047 self.assertEqual(dispatch_code, expected_dispatch_code)
1048 self.assertEqual(func_code, in_func_code)
1049 self.assertEqual(func_info, {'test_func': (0, [])})
1050
1051 def test_parsing(self):
1052 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001053 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001054 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001055 """
1056 data = '''/* BEGIN_HEADER */
1057#include "mbedtls/ecp.h"
1058
1059#define ECP_PF_UNKNOWN -1
1060/* END_HEADER */
1061
1062/* BEGIN_DEPENDENCIES
1063 * depends_on:MBEDTLS_ECP_C
1064 * END_DEPENDENCIES
1065 */
1066
1067/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1068void func1()
1069{
1070}
1071/* END_CASE */
1072
1073/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1074void func2()
1075{
1076}
1077/* END_CASE */
1078'''
Azim Khanb31aa442018-07-03 11:57:54 +01001079 stream = StringIOWrapper('test_suite_ut.function', data)
1080 suite_dependencies, dispatch_code, func_code, func_info = \
1081 parse_functions(stream)
1082 self.assertEqual(stream.line_no, 23)
1083 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +01001084
1085 expected_dispatch_code = '''/* Function Id: 0 */
1086
1087#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
1088 test_func1_wrapper,
1089#else
1090 NULL,
1091#endif
1092/* Function Id: 1 */
1093
1094#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
1095 test_func2_wrapper,
1096#else
1097 NULL,
1098#endif
1099'''
1100 self.assertEqual(dispatch_code, expected_dispatch_code)
1101 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +01001102#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001103#include "mbedtls/ecp.h"
1104
1105#define ECP_PF_UNKNOWN -1
1106#if defined(MBEDTLS_ENTROPY_NV_SEED)
1107#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001108#line 13 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001109void test_func1()
1110{
1111exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001112 ;
Azim Khan4b543232017-06-30 09:35:21 +01001113}
1114
1115void test_func1_wrapper( void ** params )
1116{
1117 (void)params;
1118
1119 test_func1( );
1120}
1121#endif /* MBEDTLS_FS_IO */
1122#endif /* MBEDTLS_ENTROPY_NV_SEED */
1123#if defined(MBEDTLS_ENTROPY_NV_SEED)
1124#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001125#line 19 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001126void test_func2()
1127{
1128exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001129 ;
Azim Khan4b543232017-06-30 09:35:21 +01001130}
1131
1132void test_func2_wrapper( void ** params )
1133{
1134 (void)params;
1135
1136 test_func2( );
1137}
1138#endif /* MBEDTLS_FS_IO */
1139#endif /* MBEDTLS_ENTROPY_NV_SEED */
1140#endif /* MBEDTLS_ECP_C */
1141'''
1142 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001143 self.assertEqual(func_info, {'test_func1': (0, []),
1144 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +01001145
1146 def test_same_function_name(self):
1147 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001148 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001149 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001150 """
1151 data = '''/* BEGIN_HEADER */
1152#include "mbedtls/ecp.h"
1153
1154#define ECP_PF_UNKNOWN -1
1155/* END_HEADER */
1156
1157/* BEGIN_DEPENDENCIES
1158 * depends_on:MBEDTLS_ECP_C
1159 * END_DEPENDENCIES
1160 */
1161
1162/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1163void func()
1164{
1165}
1166/* END_CASE */
1167
1168/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1169void func()
1170{
1171}
1172/* END_CASE */
1173'''
Azim Khanb31aa442018-07-03 11:57:54 +01001174 stream = StringIOWrapper('test_suite_ut.function', data)
1175 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +01001176
1177
Azim Khanb31aa442018-07-03 11:57:54 +01001178class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001179 """
Azim Khan599cd242017-07-06 17:34:27 +01001180 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +01001181 Note: Since escaped_split() output is used to write back to the
1182 intermediate data file. Any escape characters in the input are
1183 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001184 """
1185
1186 def test_invalid_input(self):
1187 """
1188 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001189 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001190 """
1191 self.assertRaises(ValueError, escaped_split, '', 'string')
1192
1193 def test_empty_string(self):
1194 """
Azim Khanb31aa442018-07-03 11:57:54 +01001195 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001196 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001197 """
1198 splits = escaped_split('', ':')
1199 self.assertEqual(splits, [])
1200
1201 def test_no_escape(self):
1202 """
Azim Khanb31aa442018-07-03 11:57:54 +01001203 Test with no escape character. The behaviour should be same as
1204 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001205 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001206 """
Azim Khanb31aa442018-07-03 11:57:54 +01001207 test_str = 'yahoo:google'
1208 splits = escaped_split(test_str, ':')
1209 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001210
1211 def test_escaped_input(self):
1212 """
Azim Khanb31aa442018-07-03 11:57:54 +01001213 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001214 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001215 """
Azim Khanb31aa442018-07-03 11:57:54 +01001216 test_str = r'yahoo\:google:facebook'
1217 splits = escaped_split(test_str, ':')
1218 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001219
1220 def test_escaped_escape(self):
1221 """
Azim Khanb31aa442018-07-03 11:57:54 +01001222 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001223 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001224 """
Azim Khan4084ec72018-07-05 14:20:08 +01001225 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +01001226 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001227 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001228
1229 def test_all_at_once(self):
1230 """
Azim Khanb31aa442018-07-03 11:57:54 +01001231 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001232 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001233 """
Azim Khan4084ec72018-07-05 14:20:08 +01001234 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +01001235 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001236 self.assertEqual(splits, [r'yahoo\\', r'google',
1237 r'facebook\:instagram\\',
1238 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001239
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001240
1241class ParseTestData(TestCase):
1242 """
1243 Test suite for parse test data.
1244 """
1245
1246 def test_parser(self):
1247 """
1248 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001249 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001250 """
1251 data = """
1252Diffie-Hellman full exchange #1
1253dhm_do_dhm:10:"23":10:"5"
1254
1255Diffie-Hellman full exchange #2
1256dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1257
1258Diffie-Hellman full exchange #3
1259dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1260
1261Diffie-Hellman selftest
1262dhm_selftest:
1263"""
Azim Khanb31aa442018-07-03 11:57:54 +01001264 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001265 # List of (name, function_name, dependencies, args)
1266 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001267 test1, test2, test3, test4 = tests
Gilles Peskine8542f5c2022-12-03 22:58:52 +01001268 self.assertEqual(test1[1], 'Diffie-Hellman full exchange #1')
1269 self.assertEqual(test1[2], 'dhm_do_dhm')
1270 self.assertEqual(test1[3], [])
1271 self.assertEqual(test1[4], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001272
Gilles Peskine8542f5c2022-12-03 22:58:52 +01001273 self.assertEqual(test2[1], 'Diffie-Hellman full exchange #2')
1274 self.assertEqual(test2[2], 'dhm_do_dhm')
1275 self.assertEqual(test2[3], [])
1276 self.assertEqual(test2[4], ['10', '"93450983094850938450983409623"',
Azim Khanb31aa442018-07-03 11:57:54 +01001277 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001278
Gilles Peskine8542f5c2022-12-03 22:58:52 +01001279 self.assertEqual(test3[1], 'Diffie-Hellman full exchange #3')
1280 self.assertEqual(test3[2], 'dhm_do_dhm')
1281 self.assertEqual(test3[3], [])
1282 self.assertEqual(test3[4], ['10',
Azim Khanb31aa442018-07-03 11:57:54 +01001283 '"9345098382739712938719287391879381271"',
1284 '10',
1285 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001286
Gilles Peskine8542f5c2022-12-03 22:58:52 +01001287 self.assertEqual(test4[1], 'Diffie-Hellman selftest')
1288 self.assertEqual(test4[2], 'dhm_selftest')
Azim Khanb31aa442018-07-03 11:57:54 +01001289 self.assertEqual(test4[3], [])
Gilles Peskine8542f5c2022-12-03 22:58:52 +01001290 self.assertEqual(test4[4], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001291
1292 def test_with_dependencies(self):
1293 """
1294 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001295 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001296 """
1297 data = """
1298Diffie-Hellman full exchange #1
1299depends_on:YAHOO
1300dhm_do_dhm:10:"23":10:"5"
1301
1302Diffie-Hellman full exchange #2
1303dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1304
1305"""
Azim Khanb31aa442018-07-03 11:57:54 +01001306 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001307 # List of (name, function_name, dependencies, args)
1308 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001309 test1, test2 = tests
Gilles Peskine8542f5c2022-12-03 22:58:52 +01001310 self.assertEqual(test1[1], 'Diffie-Hellman full exchange #1')
1311 self.assertEqual(test1[2], 'dhm_do_dhm')
1312 self.assertEqual(test1[3], ['YAHOO'])
1313 self.assertEqual(test1[4], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001314
Gilles Peskine8542f5c2022-12-03 22:58:52 +01001315 self.assertEqual(test2[1], 'Diffie-Hellman full exchange #2')
1316 self.assertEqual(test2[2], 'dhm_do_dhm')
1317 self.assertEqual(test2[3], [])
1318 self.assertEqual(test2[4], ['10', '"93450983094850938450983409623"',
Azim Khanb31aa442018-07-03 11:57:54 +01001319 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001320
1321 def test_no_args(self):
1322 """
Azim Khanb31aa442018-07-03 11:57:54 +01001323 Test GeneratorInputError is raised when test function name and
1324 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001325 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001326 """
1327 data = """
1328Diffie-Hellman full exchange #1
1329depends_on:YAHOO
1330
1331
1332Diffie-Hellman full exchange #2
1333dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1334
1335"""
Azim Khanb31aa442018-07-03 11:57:54 +01001336 stream = StringIOWrapper('test_suite_ut.function', data)
1337 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001338 try:
Gilles Peskine8542f5c2022-12-03 22:58:52 +01001339 for _, _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001340 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001341 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001342 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001343
1344 def test_incomplete_data(self):
1345 """
Azim Khanb31aa442018-07-03 11:57:54 +01001346 Test GeneratorInputError is raised when test function name
1347 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001348 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001349 """
1350 data = """
1351Diffie-Hellman full exchange #1
1352depends_on:YAHOO
1353"""
Azim Khanb31aa442018-07-03 11:57:54 +01001354 stream = StringIOWrapper('test_suite_ut.function', data)
1355 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001356 try:
Gilles Peskine8542f5c2022-12-03 22:58:52 +01001357 for _, _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001358 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001359 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001360 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001361
1362
1363class GenDepCheck(TestCase):
1364 """
Azim Khanb31aa442018-07-03 11:57:54 +01001365 Test suite for gen_dep_check(). It is assumed this function is
1366 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001367 """
1368
1369 def test_gen_dep_check(self):
1370 """
1371 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001372 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001373 """
1374 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001375 case 5:
1376 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001377#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001378 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001379#else
Azim Khand61b8372017-07-10 11:54:01 +01001380 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001381#endif
Azim Khand61b8372017-07-10 11:54:01 +01001382 }
1383 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001384 out = gen_dep_check(5, 'YAHOO')
1385 self.assertEqual(out, expected)
1386
Azim Khanb31aa442018-07-03 11:57:54 +01001387 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001388 """
1389 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001390 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001391 """
1392 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001393 case 5:
1394 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001395#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001396 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001397#else
Azim Khand61b8372017-07-10 11:54:01 +01001398 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001399#endif
Azim Khand61b8372017-07-10 11:54:01 +01001400 }
1401 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001402 out = gen_dep_check(5, '!YAHOO')
1403 self.assertEqual(out, expected)
1404
1405 def test_empty_dependency(self):
1406 """
1407 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001408 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001409 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001410 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001411
1412 def test_negative_dep_id(self):
1413 """
1414 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001415 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001416 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001417 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001418
1419
1420class GenExpCheck(TestCase):
1421 """
Azim Khanb31aa442018-07-03 11:57:54 +01001422 Test suite for gen_expression_check(). It is assumed this function
1423 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001424 """
1425
1426 def test_gen_exp_check(self):
1427 """
1428 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001429 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001430 """
1431 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001432 case 5:
1433 {
1434 *out_value = YAHOO;
1435 }
1436 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001437 out = gen_expression_check(5, 'YAHOO')
1438 self.assertEqual(out, expected)
1439
1440 def test_invalid_expression(self):
1441 """
1442 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001443 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001444 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001445 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001446
1447 def test_negative_exp_id(self):
1448 """
1449 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001450 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001451 """
Azim Khanb31aa442018-07-03 11:57:54 +01001452 self.assertRaises(GeneratorInputError, gen_expression_check,
1453 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001454
1455
Azim Khanb31aa442018-07-03 11:57:54 +01001456class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001457 """
Azim Khanb31aa442018-07-03 11:57:54 +01001458 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001459 """
1460
Azim Khanb31aa442018-07-03 11:57:54 +01001461 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001462 """
Azim Khanb31aa442018-07-03 11:57:54 +01001463 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001464 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001465 """
Azim Khanb31aa442018-07-03 11:57:54 +01001466 stream = StringIOWrapper('test_suite_ut.data', '')
1467 unique_dependencies = []
1468 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001469 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001470 self.assertEqual(len(unique_dependencies), 0)
1471 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001472
1473 def test_unique_dep_ids(self):
1474 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001475
1476 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001477 """
Azim Khanb31aa442018-07-03 11:57:54 +01001478 stream = StringIOWrapper('test_suite_ut.data', '')
1479 unique_dependencies = []
1480 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1481 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001482 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001483 case 0:
1484 {
Azim Khan599cd242017-07-06 17:34:27 +01001485#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001486 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001487#else
Azim Khand61b8372017-07-10 11:54:01 +01001488 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001489#endif
Azim Khand61b8372017-07-10 11:54:01 +01001490 }
1491 break;
1492 case 1:
1493 {
Azim Khan599cd242017-07-06 17:34:27 +01001494#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001495 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001496#else
Azim Khand61b8372017-07-10 11:54:01 +01001497 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001498#endif
Azim Khand61b8372017-07-10 11:54:01 +01001499 }
1500 break;
1501 case 2:
1502 {
Azim Khan599cd242017-07-06 17:34:27 +01001503#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001504 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001505#else
Azim Khand61b8372017-07-10 11:54:01 +01001506 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001507#endif
Azim Khand61b8372017-07-10 11:54:01 +01001508 }
1509 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001510 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001511 self.assertEqual(len(unique_dependencies), 3)
1512 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001513
1514 def test_dep_id_repeat(self):
1515 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001516
1517 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001518 """
Azim Khanb31aa442018-07-03 11:57:54 +01001519 stream = StringIOWrapper('test_suite_ut.data', '')
1520 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001521 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001522 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1523 unique_dependencies)
1524 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1525 unique_dependencies)
1526 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1527 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001528 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001529 case 0:
1530 {
Azim Khan599cd242017-07-06 17:34:27 +01001531#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001532 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001533#else
Azim Khand61b8372017-07-10 11:54:01 +01001534 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001535#endif
Azim Khand61b8372017-07-10 11:54:01 +01001536 }
1537 break;
1538 case 1:
1539 {
Azim Khan599cd242017-07-06 17:34:27 +01001540#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001541 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001542#else
Azim Khand61b8372017-07-10 11:54:01 +01001543 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001544#endif
Azim Khand61b8372017-07-10 11:54:01 +01001545 }
1546 break;
1547 case 2:
1548 {
Azim Khan599cd242017-07-06 17:34:27 +01001549#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001550 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001551#else
Azim Khand61b8372017-07-10 11:54:01 +01001552 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001553#endif
Azim Khand61b8372017-07-10 11:54:01 +01001554 }
1555 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001556 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001557 self.assertEqual(len(unique_dependencies), 3)
1558 self.assertEqual(stream.getvalue(),
1559 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001560
1561
1562class WriteParams(TestCase):
1563 """
1564 Test Suite for testing write_parameters().
1565 """
1566
1567 def test_no_params(self):
1568 """
1569 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001570 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001571 """
Azim Khanb31aa442018-07-03 11:57:54 +01001572 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001573 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001574 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001575 self.assertEqual(len(unique_expressions), 0)
1576 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001577 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001578
1579 def test_no_exp_param(self):
1580 """
1581 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001582 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001583 """
Azim Khanb31aa442018-07-03 11:57:54 +01001584 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001585 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001586 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1587 '0'],
1588 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001589 unique_expressions)
1590 self.assertEqual(len(unique_expressions), 0)
1591 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001592 self.assertEqual(stream.getvalue(),
1593 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001594
1595 def test_hex_format_int_param(self):
1596 """
1597 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001598 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001599 """
Azim Khanb31aa442018-07-03 11:57:54 +01001600 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001601 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001602 expression_code = write_parameters(stream,
1603 ['"Yahoo"', '"abcdef00"', '0xAA'],
1604 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001605 unique_expressions)
1606 self.assertEqual(len(unique_expressions), 0)
1607 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001608 self.assertEqual(stream.getvalue(),
1609 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001610
1611 def test_with_exp_param(self):
1612 """
1613 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001614 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001615 """
Azim Khanb31aa442018-07-03 11:57:54 +01001616 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001617 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001618 expression_code = write_parameters(stream,
1619 ['"Yahoo"', '"abcdef00"', '0',
1620 'MACRO1', 'MACRO2', 'MACRO3'],
1621 ['char*', 'hex', 'int',
1622 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001623 unique_expressions)
1624 self.assertEqual(len(unique_expressions), 3)
1625 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1626 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001627 case 0:
1628 {
1629 *out_value = MACRO1;
1630 }
1631 break;
1632 case 1:
1633 {
1634 *out_value = MACRO2;
1635 }
1636 break;
1637 case 2:
1638 {
1639 *out_value = MACRO3;
1640 }
1641 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001642 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001643 self.assertEqual(stream.getvalue(),
1644 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1645 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001646
Azim Khanb31aa442018-07-03 11:57:54 +01001647 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001648 """
1649 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001650 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001651 """
Azim Khanb31aa442018-07-03 11:57:54 +01001652 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001653 unique_expressions = []
1654 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001655 expression_code += write_parameters(stream,
1656 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1657 ['char*', '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 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1661 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001662 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001663 expression_code += write_parameters(stream,
1664 ['0', 'MACRO3', 'MACRO1'],
1665 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001666 unique_expressions)
1667 self.assertEqual(len(unique_expressions), 3)
1668 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1669 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001670 case 0:
1671 {
1672 *out_value = MACRO1;
1673 }
1674 break;
1675 case 1:
1676 {
1677 *out_value = MACRO2;
1678 }
1679 break;
1680 case 2:
1681 {
1682 *out_value = MACRO3;
1683 }
1684 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001685 self.assertEqual(expression_code, expected_expression_code)
1686 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1687:hex:"abcdef00":exp:1:exp:2
1688:int:0:exp:2:exp:0
1689'''
Azim Khanb31aa442018-07-03 11:57:54 +01001690 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001691
1692
Azim Khanb31aa442018-07-03 11:57:54 +01001693class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001694 """
Azim Khanb31aa442018-07-03 11:57:54 +01001695 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001696 """
Azim Khanb31aa442018-07-03 11:57:54 +01001697 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001698 """
Azim Khanb31aa442018-07-03 11:57:54 +01001699 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001700
1701 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001702 """
Azim Khanb31aa442018-07-03 11:57:54 +01001703 dep_check_code, expression_code = \
1704 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001705 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1706 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1707
Azim Khanb31aa442018-07-03 11:57:54 +01001708 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001709 """
Azim Khanb31aa442018-07-03 11:57:54 +01001710 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001711
1712 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001713 """
Azim Khanb31aa442018-07-03 11:57:54 +01001714 dep_check_code, expression_code = \
1715 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1716 'EXPRESSION_CODE')
1717 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001718#if defined(SUITE_DEP)
1719DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001720#endif
1721'''
1722 expected_expression_code = '''
1723#if defined(SUITE_DEP)
1724EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001725#endif
1726'''
Azim Khanb31aa442018-07-03 11:57:54 +01001727 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001728 self.assertEqual(expression_code, expected_expression_code)
1729
1730 def test_no_dep_no_exp(self):
1731 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001732 Test when there are no dependency and expression code.
1733 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001734 """
Azim Khanb31aa442018-07-03 11:57:54 +01001735 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001736 self.assertEqual(dep_check_code, '')
1737 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001738
1739
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001740class GenFromTestData(TestCase):
1741 """
1742 Test suite for gen_from_test_data()
1743 """
1744
Azim Khanb31aa442018-07-03 11:57:54 +01001745 @staticmethod
1746 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001747 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001748 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001749 def test_intermediate_data_file(func_mock1,
1750 write_parameters_mock,
1751 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001752 """
1753 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001754 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001755 """
1756 data = '''
1757My test
1758depends_on:DEP1
1759func1:0
1760'''
1761 data_f = StringIOWrapper('test_suite_ut.data', data)
1762 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1763 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001764 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001765 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001766 write_dependencies_mock.side_effect = write_dependencies
1767 func_mock1.side_effect = gen_suite_dep_checks
1768 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1769 write_dependencies_mock.assert_called_with(out_data_f,
1770 ['DEP1'], ['DEP1'])
1771 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1772 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001773 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001774 case 0:
1775 {
Azim Khan599cd242017-07-06 17:34:27 +01001776#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001777 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001778#else
Azim Khand61b8372017-07-10 11:54:01 +01001779 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001780#endif
Azim Khand61b8372017-07-10 11:54:01 +01001781 }
1782 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001783 func_mock1.assert_called_with(
1784 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001785
1786 def test_function_not_found(self):
1787 """
1788 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001789 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001790 """
1791 data = '''
1792My test
1793depends_on:DEP1
1794func1:0
1795'''
1796 data_f = StringIOWrapper('test_suite_ut.data', data)
1797 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1798 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001799 suite_dependencies = []
1800 self.assertRaises(GeneratorInputError, gen_from_test_data,
1801 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001802
1803 def test_different_func_args(self):
1804 """
Azim Khanb31aa442018-07-03 11:57:54 +01001805 Test that AssertError is raised when no. of parameters and
1806 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001807 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001808 """
1809 data = '''
1810My test
1811depends_on:DEP1
1812func1:0
1813'''
1814 data_f = StringIOWrapper('test_suite_ut.data', data)
1815 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001816 func_info = {'test_func2': (1, ('int', 'hex'))}
1817 suite_dependencies = []
1818 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1819 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001820
1821 def test_output(self):
1822 """
1823 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001824 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001825 """
1826 data = '''
1827My test 1
1828depends_on:DEP1
1829func1:0:0xfa:MACRO1:MACRO2
1830
1831My test 2
1832depends_on:DEP1:DEP2
1833func2:"yahoo":88:MACRO1
1834'''
1835 data_f = StringIOWrapper('test_suite_ut.data', data)
1836 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001837 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1838 'test_func2': (1, ('char*', 'int', 'int'))}
1839 suite_dependencies = []
1840 dep_check_code, expression_code = \
1841 gen_from_test_data(data_f, out_data_f, func_info,
1842 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001843 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001844 case 0:
1845 {
Azim Khan599cd242017-07-06 17:34:27 +01001846#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001847 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001848#else
Azim Khand61b8372017-07-10 11:54:01 +01001849 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001850#endif
Azim Khand61b8372017-07-10 11:54:01 +01001851 }
1852 break;
1853 case 1:
1854 {
Azim Khan599cd242017-07-06 17:34:27 +01001855#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001856 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001857#else
Azim Khand61b8372017-07-10 11:54:01 +01001858 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001859#endif
Azim Khand61b8372017-07-10 11:54:01 +01001860 }
1861 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001862 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001863depends_on:0
18640:int:0:int:0xfa:exp:0:exp:1
1865
1866My test 2
1867depends_on:0:1
18681:char*:"yahoo":int:88:exp:0
1869
1870'''
1871 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001872 case 0:
1873 {
1874 *out_value = MACRO1;
1875 }
1876 break;
1877 case 1:
1878 {
1879 *out_value = MACRO2;
1880 }
1881 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001882 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001883 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001884 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001885
1886
Azim Khanb31aa442018-07-03 11:57:54 +01001887if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001888 unittest_main()