blob: abc46a7291e0b8d60a31bc1aaccf7e5d6f499080 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Azim Khan4b543232017-06-30 09:35:21 +01006
7"""
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00008Unit tests for generate_test_code.py
Azim Khan4b543232017-06-30 09:35:21 +01009"""
10
Gilles Peskine38b66df2020-08-12 02:11:38 +020011from io import StringIO
Azim Khanb31aa442018-07-03 11:57:54 +010012from unittest import TestCase, main as unittest_main
Gilles Peskine38b66df2020-08-12 02:11:38 +020013from unittest.mock import patch
14
Azim Khanb31aa442018-07-03 11:57:54 +010015from generate_test_code import gen_dependencies, gen_dependencies_one_line
16from generate_test_code import gen_function_wrapper, gen_dispatch
17from generate_test_code import parse_until_pattern, GeneratorInputError
18from generate_test_code import parse_suite_dependencies
19from generate_test_code import parse_function_dependencies
Azim Khanfcdf6852018-07-05 17:31:46 +010020from generate_test_code import parse_function_arguments, parse_function_code
Azim Khanb31aa442018-07-03 11:57:54 +010021from generate_test_code import parse_functions, END_HEADER_REGEX
22from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
23from generate_test_code import parse_test_data, gen_dep_check
24from generate_test_code import gen_expression_check, write_dependencies
25from generate_test_code import write_parameters, gen_suite_dep_checks
26from generate_test_code import gen_from_test_data
27
28
Azim Khan4b543232017-06-30 09:35:21 +010029class GenDep(TestCase):
30 """
31 Test suite for function gen_dep()
32 """
33
Azim Khanb31aa442018-07-03 11:57:54 +010034 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010035 """
Azim Khanb31aa442018-07-03 11:57:54 +010036 Test that gen_dep() correctly creates dependencies for given
37 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010038 :return:
Azim Khan4b543232017-06-30 09:35:21 +010039 """
Azim Khanb31aa442018-07-03 11:57:54 +010040 dependencies = ['DEP1', 'DEP2']
41 dep_start, dep_end = gen_dependencies(dependencies)
42 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010043 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010044 self.assertEqual(preprocessor1, '#if defined(DEP1)',
45 'Preprocessor generated incorrectly')
46 self.assertEqual(preprocessor2, '#if defined(DEP2)',
47 'Preprocessor generated incorrectly')
48 self.assertEqual(endif1, '#endif /* DEP2 */',
49 'Preprocessor generated incorrectly')
50 self.assertEqual(endif2, '#endif /* DEP1 */',
51 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010052
Azim Khanb31aa442018-07-03 11:57:54 +010053 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010054 """
Azim Khanb31aa442018-07-03 11:57:54 +010055 Test that gen_dep() correctly creates dependencies for given
56 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010057 :return:
Azim Khan4b543232017-06-30 09:35:21 +010058 """
Azim Khanb31aa442018-07-03 11:57:54 +010059 dependencies = ['!DEP1', '!DEP2']
60 dep_start, dep_end = gen_dependencies(dependencies)
61 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010062 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010063 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
64 'Preprocessor generated incorrectly')
65 self.assertEqual(preprocessor2, '#if !defined(DEP2)',
66 'Preprocessor generated incorrectly')
67 self.assertEqual(endif1, '#endif /* !DEP2 */',
68 'Preprocessor generated incorrectly')
69 self.assertEqual(endif2, '#endif /* !DEP1 */',
70 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010071
Azim Khanb31aa442018-07-03 11:57:54 +010072 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010073 """
Azim Khanb31aa442018-07-03 11:57:54 +010074 Test that gen_dep() correctly creates dependencies for given
75 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010076 :return:
Azim Khan4b543232017-06-30 09:35:21 +010077 """
Azim Khanb31aa442018-07-03 11:57:54 +010078 dependencies = ['!DEP1', 'DEP2']
79 dep_start, dep_end = gen_dependencies(dependencies)
80 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010081 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010082 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
83 'Preprocessor generated incorrectly')
84 self.assertEqual(preprocessor2, '#if defined(DEP2)',
85 'Preprocessor generated incorrectly')
86 self.assertEqual(endif1, '#endif /* DEP2 */',
87 'Preprocessor generated incorrectly')
88 self.assertEqual(endif2, '#endif /* !DEP1 */',
89 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010090
Azim Khanb31aa442018-07-03 11:57:54 +010091 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010092 """
Azim Khanb31aa442018-07-03 11:57:54 +010093 Test that gen_dep() correctly creates dependencies for given
94 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010095 :return:
Azim Khan4b543232017-06-30 09:35:21 +010096 """
Azim Khanb31aa442018-07-03 11:57:54 +010097 dependencies = []
98 dep_start, dep_end = gen_dependencies(dependencies)
99 self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
100 self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100101
Azim Khanb31aa442018-07-03 11:57:54 +0100102 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100103 """
Azim Khanb31aa442018-07-03 11:57:54 +0100104 Test that gen_dep() correctly creates dependencies for given
105 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100106 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100107 """
Azim Khanb31aa442018-07-03 11:57:54 +0100108 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100109 count = 10
110 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100111 dependencies.append('DEP%d' % i)
112 dep_start, dep_end = gen_dependencies(dependencies)
113 self.assertEqual(len(dep_start.splitlines()), count,
114 'Preprocessor generated incorrectly')
115 self.assertEqual(len(dep_end.splitlines()), count,
116 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100117
118
119class GenDepOneLine(TestCase):
120 """
Azim Khanb31aa442018-07-03 11:57:54 +0100121 Test Suite for testing gen_dependencies_one_line()
Azim Khan4b543232017-06-30 09:35:21 +0100122 """
123
Azim Khanb31aa442018-07-03 11:57:54 +0100124 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100125 """
Azim Khanb31aa442018-07-03 11:57:54 +0100126 Test that gen_dep() correctly creates dependencies for given
127 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100128 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100129 """
Azim Khanb31aa442018-07-03 11:57:54 +0100130 dependencies = ['DEP1', 'DEP2']
131 dep_str = gen_dependencies_one_line(dependencies)
132 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
133 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100134
Azim Khanb31aa442018-07-03 11:57:54 +0100135 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100136 """
Azim Khanb31aa442018-07-03 11:57:54 +0100137 Test that gen_dep() correctly creates dependencies for given
138 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100139 :return:
140 """
Azim Khanb31aa442018-07-03 11:57:54 +0100141 dependencies = ['!DEP1', '!DEP2']
142 dep_str = gen_dependencies_one_line(dependencies)
143 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
144 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100145
Azim Khanb31aa442018-07-03 11:57:54 +0100146 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100147 """
Azim Khanb31aa442018-07-03 11:57:54 +0100148 Test that gen_dep() correctly creates dependencies for given
149 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100150 :return:
151 """
Azim Khanb31aa442018-07-03 11:57:54 +0100152 dependencies = ['!DEP1', 'DEP2']
153 dep_str = gen_dependencies_one_line(dependencies)
154 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
155 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100156
Azim Khanb31aa442018-07-03 11:57:54 +0100157 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100158 """
Azim Khanb31aa442018-07-03 11:57:54 +0100159 Test that gen_dep() correctly creates dependencies for given
160 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100161 :return:
162 """
Azim Khanb31aa442018-07-03 11:57:54 +0100163 dependencies = []
164 dep_str = gen_dependencies_one_line(dependencies)
165 self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100166
Azim Khanb31aa442018-07-03 11:57:54 +0100167 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100168 """
Azim Khanb31aa442018-07-03 11:57:54 +0100169 Test that gen_dep() correctly creates dependencies for given
170 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100171 :return:
172 """
Azim Khanb31aa442018-07-03 11:57:54 +0100173 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100174 count = 10
175 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100176 dependencies.append('DEP%d' % i)
177 dep_str = gen_dependencies_one_line(dependencies)
178 expected = '#if ' + ' && '.join(['defined(%s)' %
179 x for x in dependencies])
180 self.assertEqual(dep_str, expected,
181 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100182
183
184class GenFunctionWrapper(TestCase):
185 """
186 Test Suite for testing gen_function_wrapper()
187 """
188
189 def test_params_unpack(self):
190 """
191 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100192
193 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100194 """
195 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
196 expected = '''
197void test_a_wrapper( void ** params )
198{
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100199
Azim Khan4b543232017-06-30 09:35:21 +0100200 test_a( a, b, c, d );
201}
202'''
203 self.assertEqual(code, expected)
204
205 def test_local(self):
206 """
207 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100208
209 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100210 """
Azim Khanb31aa442018-07-03 11:57:54 +0100211 code = gen_function_wrapper('test_a',
212 'int x = 1;', ('x', 'b', 'c', 'd'))
Azim Khan4b543232017-06-30 09:35:21 +0100213 expected = '''
214void test_a_wrapper( void ** params )
215{
Azim Khan4b543232017-06-30 09:35:21 +0100216int x = 1;
217 test_a( x, b, c, d );
218}
219'''
220 self.assertEqual(code, expected)
221
222 def test_empty_params(self):
223 """
224 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100225
226 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100227 """
228 code = gen_function_wrapper('test_a', '', ())
229 expected = '''
230void test_a_wrapper( void ** params )
231{
232 (void)params;
233
234 test_a( );
235}
236'''
237 self.assertEqual(code, expected)
238
239
240class GenDispatch(TestCase):
241 """
242 Test suite for testing gen_dispatch()
243 """
244
245 def test_dispatch(self):
246 """
247 Test that dispatch table entry is generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100248 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100249 """
250 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
251 expected = '''
252#if defined(DEP1) && defined(DEP2)
253 test_a_wrapper,
254#else
255 NULL,
256#endif
257'''
258 self.assertEqual(code, expected)
259
Azim Khanb31aa442018-07-03 11:57:54 +0100260 def test_empty_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100261 """
262 Test empty dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100263 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100264 """
265 code = gen_dispatch('test_a', [])
266 expected = '''
267 test_a_wrapper,
268'''
269 self.assertEqual(code, expected)
270
271
Gilles Peskine184c0962020-03-24 18:25:17 +0100272class StringIOWrapper(StringIO):
Azim Khan4b543232017-06-30 09:35:21 +0100273 """
274 file like class to mock file object in tests.
275 """
Azim Khan4084ec72018-07-05 14:20:08 +0100276 def __init__(self, file_name, data, line_no=0):
Azim Khan4b543232017-06-30 09:35:21 +0100277 """
278 Init file handle.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100279
280 :param file_name:
Azim Khan4b543232017-06-30 09:35:21 +0100281 :param data:
282 :param line_no:
283 """
284 super(StringIOWrapper, self).__init__(data)
285 self.line_no = line_no
286 self.name = file_name
287
288 def next(self):
289 """
Azim Khanb31aa442018-07-03 11:57:54 +0100290 Iterator method. This method overrides base class's
291 next method and extends the next method to count the line
292 numbers as each line is read.
Azim Khan4b543232017-06-30 09:35:21 +0100293
Azim Khanb31aa442018-07-03 11:57:54 +0100294 :return: Line read from file.
295 """
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100296 parent = super(StringIOWrapper, self)
Gilles Peskine38b66df2020-08-12 02:11:38 +0200297 line = parent.__next__()
Azim Khan4084ec72018-07-05 14:20:08 +0100298 return line
Azim Khanb31aa442018-07-03 11:57:54 +0100299
Gilles Peskine38b66df2020-08-12 02:11:38 +0200300 def readline(self, _length=0):
Azim Khan4b543232017-06-30 09:35:21 +0100301 """
302 Wrap the base class readline.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100303
Azim Khanb31aa442018-07-03 11:57:54 +0100304 :param length:
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100305 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100306 """
307 line = super(StringIOWrapper, self).readline()
Azim Khan4084ec72018-07-05 14:20:08 +0100308 if line is not None:
Azim Khan4b543232017-06-30 09:35:21 +0100309 self.line_no += 1
310 return line
311
312
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000313class ParseUntilPattern(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100314 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000315 Test Suite for testing parse_until_pattern().
Azim Khan4b543232017-06-30 09:35:21 +0100316 """
317
318 def test_suite_headers(self):
319 """
320 Test that suite headers are parsed correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100321
322 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100323 """
324 data = '''#include "mbedtls/ecp.h"
325
326#define ECP_PF_UNKNOWN -1
327/* END_HEADER */
328'''
329 expected = '''#line 1 "test_suite_ut.function"
330#include "mbedtls/ecp.h"
331
332#define ECP_PF_UNKNOWN -1
333'''
Azim Khanb31aa442018-07-03 11:57:54 +0100334 stream = StringIOWrapper('test_suite_ut.function', data, line_no=0)
335 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100336 self.assertEqual(headers, expected)
337
338 def test_line_no(self):
339 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100340 Test that #line is set to correct line no. in source .function file.
341
342 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100343 """
344 data = '''#include "mbedtls/ecp.h"
345
346#define ECP_PF_UNKNOWN -1
347/* END_HEADER */
348'''
349 offset_line_no = 5
350 expected = '''#line %d "test_suite_ut.function"
351#include "mbedtls/ecp.h"
352
353#define ECP_PF_UNKNOWN -1
354''' % (offset_line_no + 1)
Azim Khanb31aa442018-07-03 11:57:54 +0100355 stream = StringIOWrapper('test_suite_ut.function', data,
356 offset_line_no)
357 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100358 self.assertEqual(headers, expected)
359
360 def test_no_end_header_comment(self):
361 """
Azim Khanb31aa442018-07-03 11:57:54 +0100362 Test that InvalidFileFormat is raised when end header comment is
363 missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100364 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100365 """
366 data = '''#include "mbedtls/ecp.h"
367
368#define ECP_PF_UNKNOWN -1
369
370'''
Azim Khanb31aa442018-07-03 11:57:54 +0100371 stream = StringIOWrapper('test_suite_ut.function', data)
372 self.assertRaises(GeneratorInputError, parse_until_pattern, stream,
373 END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100374
375
Azim Khanb31aa442018-07-03 11:57:54 +0100376class ParseSuiteDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100377 """
Azim Khanb31aa442018-07-03 11:57:54 +0100378 Test Suite for testing parse_suite_dependencies().
Azim Khan4b543232017-06-30 09:35:21 +0100379 """
380
Azim Khanb31aa442018-07-03 11:57:54 +0100381 def test_suite_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100382 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100383
384 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100385 """
386 data = '''
387 * depends_on:MBEDTLS_ECP_C
388 * END_DEPENDENCIES
389 */
390'''
391 expected = ['MBEDTLS_ECP_C']
Azim Khanb31aa442018-07-03 11:57:54 +0100392 stream = StringIOWrapper('test_suite_ut.function', data)
393 dependencies = parse_suite_dependencies(stream)
394 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100395
396 def test_no_end_dep_comment(self):
397 """
398 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100399 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100400 """
401 data = '''
402* depends_on:MBEDTLS_ECP_C
403'''
Azim Khanb31aa442018-07-03 11:57:54 +0100404 stream = StringIOWrapper('test_suite_ut.function', data)
405 self.assertRaises(GeneratorInputError, parse_suite_dependencies,
406 stream)
Azim Khan4b543232017-06-30 09:35:21 +0100407
Azim Khanb31aa442018-07-03 11:57:54 +0100408 def test_dependencies_split(self):
Azim Khan4b543232017-06-30 09:35:21 +0100409 """
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 = '''
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100414 * depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
Azim Khan4b543232017-06-30 09:35:21 +0100415 * END_DEPENDENCIES
416 */
417'''
418 expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
Azim Khanb31aa442018-07-03 11:57:54 +0100419 stream = StringIOWrapper('test_suite_ut.function', data)
420 dependencies = parse_suite_dependencies(stream)
421 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100422
423
Azim Khanb31aa442018-07-03 11:57:54 +0100424class ParseFuncDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100425 """
Azim Khanb31aa442018-07-03 11:57:54 +0100426 Test Suite for testing parse_function_dependencies()
Azim Khan4b543232017-06-30 09:35:21 +0100427 """
428
Azim Khanb31aa442018-07-03 11:57:54 +0100429 def test_function_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100430 """
Azim Khanb31aa442018-07-03 11:57:54 +0100431 Test that parse_function_dependencies() correctly parses function
432 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100433 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100434 """
Azim Khanb31aa442018-07-03 11:57:54 +0100435 line = '/* BEGIN_CASE ' \
436 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
Azim Khan4b543232017-06-30 09:35:21 +0100437 expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
Azim Khanb31aa442018-07-03 11:57:54 +0100438 dependencies = parse_function_dependencies(line)
439 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100440
Azim Khanb31aa442018-07-03 11:57:54 +0100441 def test_no_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 """
447 line = '/* BEGIN_CASE */'
Azim Khanb31aa442018-07-03 11:57:54 +0100448 dependencies = parse_function_dependencies(line)
449 self.assertEqual(dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100450
Azim Khanb31aa442018-07-03 11:57:54 +0100451 def test_tolerance(self):
Azim Khan4b543232017-06-30 09:35:21 +0100452 """
Azim Khanb31aa442018-07-03 11:57:54 +0100453 Test that parse_function_dependencies() correctly parses function
454 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100455 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100456 """
457 line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
Azim Khanb31aa442018-07-03 11:57:54 +0100458 dependencies = parse_function_dependencies(line)
459 self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
Azim Khan4b543232017-06-30 09:35:21 +0100460
461
462class ParseFuncSignature(TestCase):
463 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100464 Test Suite for parse_function_arguments().
Azim Khan4b543232017-06-30 09:35:21 +0100465 """
466
467 def test_int_and_char_params(self):
468 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100469 Test int and char parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100470 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100471 """
472 line = 'void entropy_threshold( char * a, int b, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100473 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100474 self.assertEqual(args, ['char*', 'int', 'int'])
475 self.assertEqual(local, '')
Gilles Peskine9ad7bd32022-12-04 13:10:55 +0100476 self.assertEqual(arg_dispatch,
477 ['(char *) params[0]',
Gilles Peskinedca05012023-04-26 19:59:28 +0200478 '((mbedtls_test_argument_t *) params[1])->sint',
479 '((mbedtls_test_argument_t *) params[2])->sint'])
Azim Khan4b543232017-06-30 09:35:21 +0100480
481 def test_hex_params(self):
482 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100483 Test hex parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100484 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100485 """
Azim Khan5fcca462018-06-29 11:05:32 +0100486 line = 'void entropy_threshold( char * a, data_t * h, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100487 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100488 self.assertEqual(args, ['char*', 'hex', 'int'])
Azim Khanb31aa442018-07-03 11:57:54 +0100489 self.assertEqual(local,
Azim Khan4084ec72018-07-05 14:20:08 +0100490 ' data_t data1 = {(uint8_t *) params[1], '
Gilles Peskinedca05012023-04-26 19:59:28 +0200491 '((mbedtls_test_argument_t *) params[2])->len};\n')
Azim Khanb31aa442018-07-03 11:57:54 +0100492 self.assertEqual(arg_dispatch, ['(char *) params[0]',
Azim Khan4084ec72018-07-05 14:20:08 +0100493 '&data1',
Gilles Peskinedca05012023-04-26 19:59:28 +0200494 '((mbedtls_test_argument_t *) params[3])->sint'])
Azim Khan4b543232017-06-30 09:35:21 +0100495
Azim Khan4b543232017-06-30 09:35:21 +0100496 def test_unsupported_arg(self):
497 """
Gilles Peskineba726622022-12-04 15:57:49 +0100498 Test unsupported argument type
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100499 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100500 """
Gilles Peskineba726622022-12-04 15:57:49 +0100501 line = 'void entropy_threshold( char * a, data_t * h, unknown_t result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100502 self.assertRaises(ValueError, parse_function_arguments, line)
Azim Khan4b543232017-06-30 09:35:21 +0100503
Gilles Peskine46476e02022-12-04 14:29:06 +0100504 def test_empty_params(self):
Azim Khan4b543232017-06-30 09:35:21 +0100505 """
Gilles Peskine46476e02022-12-04 14:29:06 +0100506 Test no parameters (nothing between parentheses).
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100507 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100508 """
509 line = 'void entropy_threshold()'
Azim Khanfcdf6852018-07-05 17:31:46 +0100510 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100511 self.assertEqual(args, [])
512 self.assertEqual(local, '')
513 self.assertEqual(arg_dispatch, [])
514
Gilles Peskine46476e02022-12-04 14:29:06 +0100515 def test_blank_params(self):
516 """
517 Test no parameters (space between parentheses).
518 :return:
519 """
520 line = 'void entropy_threshold( )'
521 args, local, arg_dispatch = parse_function_arguments(line)
522 self.assertEqual(args, [])
523 self.assertEqual(local, '')
524 self.assertEqual(arg_dispatch, [])
525
526 def test_void_params(self):
527 """
528 Test no parameters (void keyword).
529 :return:
530 """
531 line = 'void entropy_threshold(void)'
532 args, local, arg_dispatch = parse_function_arguments(line)
533 self.assertEqual(args, [])
534 self.assertEqual(local, '')
535 self.assertEqual(arg_dispatch, [])
536
537 def test_void_space_params(self):
538 """
539 Test no parameters (void with spaces).
540 :return:
541 """
542 line = 'void entropy_threshold( void )'
543 args, local, arg_dispatch = parse_function_arguments(line)
544 self.assertEqual(args, [])
545 self.assertEqual(local, '')
546 self.assertEqual(arg_dispatch, [])
547
Azim Khan4b543232017-06-30 09:35:21 +0100548
549class ParseFunctionCode(TestCase):
550 """
551 Test suite for testing parse_function_code()
552 """
553
554 def test_no_function(self):
555 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100556 Test no test function found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100557 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100558 """
559 data = '''
560No
561test
562function
563'''
Azim Khanb31aa442018-07-03 11:57:54 +0100564 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100565 err_msg = 'file: test_suite_ut.function - Test functions not found!'
Gilles Peskine38b66df2020-08-12 02:11:38 +0200566 self.assertRaisesRegex(GeneratorInputError, err_msg,
567 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100568
569 def test_no_end_case_comment(self):
570 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100571 Test missing end case.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100572 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100573 """
574 data = '''
575void test_func()
576{
577}
578'''
Azim Khanb31aa442018-07-03 11:57:54 +0100579 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100580 err_msg = r'file: test_suite_ut.function - '\
581 'end case pattern .*? not found!'
Gilles Peskine38b66df2020-08-12 02:11:38 +0200582 self.assertRaisesRegex(GeneratorInputError, err_msg,
583 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100584
Azim Khanfcdf6852018-07-05 17:31:46 +0100585 @patch("generate_test_code.parse_function_arguments")
Azim Khanb31aa442018-07-03 11:57:54 +0100586 def test_function_called(self,
Azim Khanfcdf6852018-07-05 17:31:46 +0100587 parse_function_arguments_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100588 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100589 Test parse_function_code()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100590 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100591 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100592 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100593 data = '''
594void test_func()
595{
596}
597'''
Azim Khanb31aa442018-07-03 11:57:54 +0100598 stream = StringIOWrapper('test_suite_ut.function', data)
599 self.assertRaises(GeneratorInputError, parse_function_code,
600 stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100601 self.assertTrue(parse_function_arguments_mock.called)
602 parse_function_arguments_mock.assert_called_with('void test_func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100603
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000604 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100605 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000606 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100607 @patch("generate_test_code.parse_function_arguments")
608 def test_return(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100609 gen_function_wrapper_mock,
610 gen_dependencies_mock,
611 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100612 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100613 Test generated code.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100614 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100615 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100616 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100617 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100618 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100619 gen_dispatch_mock.side_effect = gen_dispatch
620 data = '''
621void func()
622{
623 ba ba black sheep
624 have you any wool
625}
626/* END_CASE */
627'''
Azim Khanb31aa442018-07-03 11:57:54 +0100628 stream = StringIOWrapper('test_suite_ut.function', data)
629 name, arg, code, dispatch_code = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100630
Azim Khanfcdf6852018-07-05 17:31:46 +0100631 self.assertTrue(parse_function_arguments_mock.called)
632 parse_function_arguments_mock.assert_called_with('void func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100633 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
634 self.assertEqual(name, 'test_func')
635 self.assertEqual(arg, [])
Azim Khan4084ec72018-07-05 14:20:08 +0100636 expected = '''#line 1 "test_suite_ut.function"
637
Gowtham Suresh Kumar34d8bd32023-07-26 17:18:55 +0100638void test_func(void)
Azim Khan4b543232017-06-30 09:35:21 +0100639{
640 ba ba black sheep
641 have you any wool
642exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100643 ;
Azim Khan4b543232017-06-30 09:35:21 +0100644}
645'''
646 self.assertEqual(code, expected)
647 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
648
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000649 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100650 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000651 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100652 @patch("generate_test_code.parse_function_arguments")
653 def test_with_exit_label(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100654 gen_function_wrapper_mock,
655 gen_dependencies_mock,
656 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100657 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100658 Test when exit label is present.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100659 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100660 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100661 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100662 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100663 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100664 gen_dispatch_mock.side_effect = gen_dispatch
665 data = '''
666void func()
667{
668 ba ba black sheep
669 have you any wool
670exit:
671 yes sir yes sir
672 3 bags full
673}
674/* END_CASE */
675'''
Azim Khanb31aa442018-07-03 11:57:54 +0100676 stream = StringIOWrapper('test_suite_ut.function', data)
677 _, _, code, _ = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100678
Azim Khan4084ec72018-07-05 14:20:08 +0100679 expected = '''#line 1 "test_suite_ut.function"
680
Gowtham Suresh Kumar34d8bd32023-07-26 17:18:55 +0100681void test_func(void)
Azim Khan4b543232017-06-30 09:35:21 +0100682{
683 ba ba black sheep
684 have you any wool
685exit:
686 yes sir yes sir
687 3 bags full
688}
689'''
690 self.assertEqual(code, expected)
691
Azim Khanfcdf6852018-07-05 17:31:46 +0100692 def test_non_void_function(self):
693 """
694 Test invalid signature (non void).
695 :return:
696 """
697 data = 'int entropy_threshold( char * a, data_t * h, int result )'
698 err_msg = 'file: test_suite_ut.function - Test functions not found!'
699 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine38b66df2020-08-12 02:11:38 +0200700 self.assertRaisesRegex(GeneratorInputError, err_msg,
701 parse_function_code, stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100702
703 @patch("generate_test_code.gen_dispatch")
704 @patch("generate_test_code.gen_dependencies")
705 @patch("generate_test_code.gen_function_wrapper")
706 @patch("generate_test_code.parse_function_arguments")
Gilles Peskine47117312022-11-11 16:36:51 +0100707 def test_function_name_on_newline(self, parse_function_arguments_mock,
708 gen_function_wrapper_mock,
709 gen_dependencies_mock,
710 gen_dispatch_mock):
Azim Khanfcdf6852018-07-05 17:31:46 +0100711 """
Gilles Peskine47117312022-11-11 16:36:51 +0100712 Test with line break before the function name.
Azim Khanfcdf6852018-07-05 17:31:46 +0100713 :return:
714 """
715 parse_function_arguments_mock.return_value = ([], '', [])
716 gen_function_wrapper_mock.return_value = ''
717 gen_dependencies_mock.side_effect = gen_dependencies
718 gen_dispatch_mock.side_effect = gen_dispatch
719 data = '''
720void
721
722
723func()
724{
725 ba ba black sheep
726 have you any wool
727exit:
728 yes sir yes sir
729 3 bags full
730}
731/* END_CASE */
732'''
733 stream = StringIOWrapper('test_suite_ut.function', data)
734 _, _, code, _ = parse_function_code(stream, [], [])
735
736 expected = '''#line 1 "test_suite_ut.function"
737
738void
739
740
Gowtham Suresh Kumar34d8bd32023-07-26 17:18:55 +0100741test_func(void)
Azim Khanfcdf6852018-07-05 17:31:46 +0100742{
743 ba ba black sheep
744 have you any wool
745exit:
746 yes sir yes sir
747 3 bags full
748}
749'''
750 self.assertEqual(code, expected)
751
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100752 @patch("generate_test_code.gen_dispatch")
753 @patch("generate_test_code.gen_dependencies")
754 @patch("generate_test_code.gen_function_wrapper")
755 @patch("generate_test_code.parse_function_arguments")
756 def test_case_starting_with_comment(self, parse_function_arguments_mock,
757 gen_function_wrapper_mock,
758 gen_dependencies_mock,
759 gen_dispatch_mock):
760 """
761 Test with comments before the function signature
762 :return:
763 """
764 parse_function_arguments_mock.return_value = ([], '', [])
765 gen_function_wrapper_mock.return_value = ''
766 gen_dependencies_mock.side_effect = gen_dependencies
767 gen_dispatch_mock.side_effect = gen_dispatch
768 data = '''/* comment */
769/* more
770 * comment */
Gilles Peskinee54f63e2022-11-18 22:24:56 +0100771// this is\\
772still \\
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100773a comment
774void func()
775{
776 ba ba black sheep
777 have you any wool
778exit:
779 yes sir yes sir
780 3 bags full
781}
782/* END_CASE */
783'''
784 stream = StringIOWrapper('test_suite_ut.function', data)
785 _, _, code, _ = parse_function_code(stream, [], [])
786
787 expected = '''#line 1 "test_suite_ut.function"
788
789
790
Gilles Peskine07995fd2022-11-29 22:03:32 +0100791
Gilles Peskineaec4bec2022-11-30 16:38:49 +0100792
793
Gowtham Suresh Kumar34d8bd32023-07-26 17:18:55 +0100794void test_func(void)
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100795{
796 ba ba black sheep
797 have you any wool
798exit:
799 yes sir yes sir
800 3 bags full
801}
802'''
803 self.assertEqual(code, expected)
804
805 @patch("generate_test_code.gen_dispatch")
806 @patch("generate_test_code.gen_dependencies")
807 @patch("generate_test_code.gen_function_wrapper")
808 @patch("generate_test_code.parse_function_arguments")
809 def test_comment_in_prototype(self, parse_function_arguments_mock,
810 gen_function_wrapper_mock,
811 gen_dependencies_mock,
812 gen_dispatch_mock):
813 """
814 Test with comments in the function prototype
815 :return:
816 """
817 parse_function_arguments_mock.return_value = ([], '', [])
818 gen_function_wrapper_mock.return_value = ''
819 gen_dependencies_mock.side_effect = gen_dependencies
820 gen_dispatch_mock.side_effect = gen_dispatch
821 data = '''
822void func( int x, // (line \\
823 comment)
824 int y /* lone closing parenthesis) */ )
825{
826 ba ba black sheep
827 have you any wool
828exit:
829 yes sir yes sir
830 3 bags full
831}
832/* END_CASE */
833'''
834 stream = StringIOWrapper('test_suite_ut.function', data)
835 _, _, code, _ = parse_function_code(stream, [], [])
836
837 expected = '''#line 1 "test_suite_ut.function"
838
839void test_func( int x,
Gilles Peskineaec4bec2022-11-30 16:38:49 +0100840
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100841 int y )
842{
843 ba ba black sheep
844 have you any wool
845exit:
846 yes sir yes sir
847 3 bags full
848}
849'''
850 self.assertEqual(code, expected)
851
Gilles Peskine8ee3a652022-11-18 22:25:18 +0100852 @patch("generate_test_code.gen_dispatch")
853 @patch("generate_test_code.gen_dependencies")
854 @patch("generate_test_code.gen_function_wrapper")
855 @patch("generate_test_code.parse_function_arguments")
856 def test_line_comment_in_block_comment(self, parse_function_arguments_mock,
857 gen_function_wrapper_mock,
858 gen_dependencies_mock,
859 gen_dispatch_mock):
860 """
861 Test with line comment in block comment.
862 :return:
863 """
864 parse_function_arguments_mock.return_value = ([], '', [])
865 gen_function_wrapper_mock.return_value = ''
866 gen_dependencies_mock.side_effect = gen_dependencies
867 gen_dispatch_mock.side_effect = gen_dispatch
868 data = '''
869void func( int x /* // */ )
870{
871 ba ba black sheep
872 have you any wool
873exit:
874 yes sir yes sir
875 3 bags full
876}
877/* END_CASE */
878'''
879 stream = StringIOWrapper('test_suite_ut.function', data)
880 _, _, code, _ = parse_function_code(stream, [], [])
881
882 expected = '''#line 1 "test_suite_ut.function"
883
884void test_func( int x )
885{
886 ba ba black sheep
887 have you any wool
888exit:
889 yes sir yes sir
890 3 bags full
891}
892'''
893 self.assertEqual(code, expected)
894
895 @patch("generate_test_code.gen_dispatch")
896 @patch("generate_test_code.gen_dependencies")
897 @patch("generate_test_code.gen_function_wrapper")
898 @patch("generate_test_code.parse_function_arguments")
899 def test_block_comment_in_line_comment(self, parse_function_arguments_mock,
900 gen_function_wrapper_mock,
901 gen_dependencies_mock,
902 gen_dispatch_mock):
903 """
904 Test with block comment in line comment.
905 :return:
906 """
907 parse_function_arguments_mock.return_value = ([], '', [])
908 gen_function_wrapper_mock.return_value = ''
909 gen_dependencies_mock.side_effect = gen_dependencies
910 gen_dispatch_mock.side_effect = gen_dispatch
911 data = '''
912// /*
913void func( int x )
914{
915 ba ba black sheep
916 have you any wool
917exit:
918 yes sir yes sir
919 3 bags full
920}
921/* END_CASE */
922'''
923 stream = StringIOWrapper('test_suite_ut.function', data)
924 _, _, code, _ = parse_function_code(stream, [], [])
925
926 expected = '''#line 1 "test_suite_ut.function"
927
928
929void test_func( int x )
930{
931 ba ba black sheep
932 have you any wool
933exit:
934 yes sir yes sir
935 3 bags full
936}
937'''
938 self.assertEqual(code, expected)
939
Azim Khan4b543232017-06-30 09:35:21 +0100940
941class ParseFunction(TestCase):
942 """
943 Test Suite for testing parse_functions()
944 """
945
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_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100948 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000949 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100950 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100951 """
Azim Khanb31aa442018-07-03 11:57:54 +0100952 def stop(*_unused):
953 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100954 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000955 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100956 data = '''/* BEGIN_HEADER */
957#include "mbedtls/ecp.h"
958
959#define ECP_PF_UNKNOWN -1
960/* END_HEADER */
961'''
Azim Khanb31aa442018-07-03 11:57:54 +0100962 stream = StringIOWrapper('test_suite_ut.function', data)
963 self.assertRaises(Exception, parse_functions, stream)
964 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100965 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000966
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000967 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000968 def test_begin_helper(self, parse_until_pattern_mock):
969 """
970 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100971 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000972 """
Azim Khanb31aa442018-07-03 11:57:54 +0100973 def stop(*_unused):
974 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000975 raise Exception
976 parse_until_pattern_mock.side_effect = stop
977 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100978void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000979{
Azim Khanb31aa442018-07-03 11:57:54 +0100980 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000981}
982/* END_SUITE_HELPERS */
983'''
Azim Khanb31aa442018-07-03 11:57:54 +0100984 stream = StringIOWrapper('test_suite_ut.function', data)
985 self.assertRaises(Exception, parse_functions, stream)
986 parse_until_pattern_mock.assert_called_with(stream,
987 END_SUITE_HELPERS_REGEX)
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_suite_dependencies")
991 def test_begin_dep(self, parse_suite_dependencies_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_suite_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 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +01001001 data = '''/* BEGIN_DEPENDENCIES
1002 * depends_on:MBEDTLS_ECP_C
1003 * END_DEPENDENCIES
1004 */
1005'''
Azim Khanb31aa442018-07-03 11:57:54 +01001006 stream = StringIOWrapper('test_suite_ut.function', data)
1007 self.assertRaises(Exception, parse_functions, stream)
1008 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +01001009 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +01001010
Azim Khanb31aa442018-07-03 11:57:54 +01001011 @patch("generate_test_code.parse_function_dependencies")
1012 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +01001013 """
Azim Khanb31aa442018-07-03 11:57:54 +01001014 Test that begin dep is checked and parse_function_dependencies() is
1015 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001016 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001017 """
Azim Khanb31aa442018-07-03 11:57:54 +01001018 def stop(*_unused):
1019 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +01001020 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +01001021 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +01001022
Azim Khanb31aa442018-07-03 11:57:54 +01001023 dependencies_str = '/* BEGIN_CASE ' \
1024 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +01001025 data = '''%svoid test_func()
1026{
1027}
Azim Khanb31aa442018-07-03 11:57:54 +01001028''' % dependencies_str
1029 stream = StringIOWrapper('test_suite_ut.function', data)
1030 self.assertRaises(Exception, parse_functions, stream)
1031 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +01001032 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +01001033
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001034 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +01001035 @patch("generate_test_code.parse_function_dependencies")
1036 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +01001037 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001038 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001039 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001040 """
Azim Khanb31aa442018-07-03 11:57:54 +01001041 func_mock1.return_value = []
1042 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +01001043{
1044}
1045'''
1046 func_dispatch = '''
1047 test_func_wrapper,
1048'''
Azim Khanb31aa442018-07-03 11:57:54 +01001049 func_mock2.return_value = 'test_func', [],\
1050 in_func_code, func_dispatch
1051 dependencies_str = '/* BEGIN_CASE ' \
1052 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +01001053 data = '''%svoid test_func()
1054{
1055}
Azim Khanb31aa442018-07-03 11:57:54 +01001056''' % dependencies_str
1057 stream = StringIOWrapper('test_suite_ut.function', data)
1058 suite_dependencies, dispatch_code, func_code, func_info = \
1059 parse_functions(stream)
1060 func_mock1.assert_called_with(dependencies_str)
1061 func_mock2.assert_called_with(stream, [], [])
1062 self.assertEqual(stream.line_no, 5)
1063 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +01001064 expected_dispatch_code = '''/* Function Id: 0 */
1065
1066 test_func_wrapper,
1067'''
1068 self.assertEqual(dispatch_code, expected_dispatch_code)
1069 self.assertEqual(func_code, in_func_code)
1070 self.assertEqual(func_info, {'test_func': (0, [])})
1071
1072 def test_parsing(self):
1073 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001074 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001075 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001076 """
1077 data = '''/* BEGIN_HEADER */
1078#include "mbedtls/ecp.h"
1079
1080#define ECP_PF_UNKNOWN -1
1081/* END_HEADER */
1082
1083/* BEGIN_DEPENDENCIES
1084 * depends_on:MBEDTLS_ECP_C
1085 * END_DEPENDENCIES
1086 */
1087
1088/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1089void func1()
1090{
1091}
1092/* END_CASE */
1093
1094/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1095void func2()
1096{
1097}
1098/* END_CASE */
1099'''
Azim Khanb31aa442018-07-03 11:57:54 +01001100 stream = StringIOWrapper('test_suite_ut.function', data)
1101 suite_dependencies, dispatch_code, func_code, func_info = \
1102 parse_functions(stream)
1103 self.assertEqual(stream.line_no, 23)
1104 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +01001105
1106 expected_dispatch_code = '''/* Function Id: 0 */
1107
1108#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
1109 test_func1_wrapper,
1110#else
1111 NULL,
1112#endif
1113/* Function Id: 1 */
1114
1115#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
1116 test_func2_wrapper,
1117#else
1118 NULL,
1119#endif
1120'''
1121 self.assertEqual(dispatch_code, expected_dispatch_code)
1122 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +01001123#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001124#include "mbedtls/ecp.h"
1125
1126#define ECP_PF_UNKNOWN -1
1127#if defined(MBEDTLS_ENTROPY_NV_SEED)
1128#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001129#line 13 "test_suite_ut.function"
Gowtham Suresh Kumar34d8bd32023-07-26 17:18:55 +01001130void test_func1(void)
Azim Khan4b543232017-06-30 09:35:21 +01001131{
1132exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001133 ;
Azim Khan4b543232017-06-30 09:35:21 +01001134}
1135
1136void test_func1_wrapper( void ** params )
1137{
1138 (void)params;
1139
1140 test_func1( );
1141}
1142#endif /* MBEDTLS_FS_IO */
1143#endif /* MBEDTLS_ENTROPY_NV_SEED */
1144#if defined(MBEDTLS_ENTROPY_NV_SEED)
1145#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001146#line 19 "test_suite_ut.function"
Gowtham Suresh Kumar34d8bd32023-07-26 17:18:55 +01001147void test_func2(void)
Azim Khan4b543232017-06-30 09:35:21 +01001148{
1149exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001150 ;
Azim Khan4b543232017-06-30 09:35:21 +01001151}
1152
1153void test_func2_wrapper( void ** params )
1154{
1155 (void)params;
1156
1157 test_func2( );
1158}
1159#endif /* MBEDTLS_FS_IO */
1160#endif /* MBEDTLS_ENTROPY_NV_SEED */
1161#endif /* MBEDTLS_ECP_C */
1162'''
1163 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001164 self.assertEqual(func_info, {'test_func1': (0, []),
1165 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +01001166
1167 def test_same_function_name(self):
1168 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001169 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001170 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001171 """
1172 data = '''/* BEGIN_HEADER */
1173#include "mbedtls/ecp.h"
1174
1175#define ECP_PF_UNKNOWN -1
1176/* END_HEADER */
1177
1178/* BEGIN_DEPENDENCIES
1179 * depends_on:MBEDTLS_ECP_C
1180 * END_DEPENDENCIES
1181 */
1182
1183/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1184void func()
1185{
1186}
1187/* END_CASE */
1188
1189/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1190void func()
1191{
1192}
1193/* END_CASE */
1194'''
Azim Khanb31aa442018-07-03 11:57:54 +01001195 stream = StringIOWrapper('test_suite_ut.function', data)
1196 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +01001197
1198
Azim Khanb31aa442018-07-03 11:57:54 +01001199class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001200 """
Azim Khan599cd242017-07-06 17:34:27 +01001201 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +01001202 Note: Since escaped_split() output is used to write back to the
1203 intermediate data file. Any escape characters in the input are
1204 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001205 """
1206
1207 def test_invalid_input(self):
1208 """
1209 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001210 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001211 """
1212 self.assertRaises(ValueError, escaped_split, '', 'string')
1213
1214 def test_empty_string(self):
1215 """
Azim Khanb31aa442018-07-03 11:57:54 +01001216 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001217 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001218 """
1219 splits = escaped_split('', ':')
1220 self.assertEqual(splits, [])
1221
1222 def test_no_escape(self):
1223 """
Azim Khanb31aa442018-07-03 11:57:54 +01001224 Test with no escape character. The behaviour should be same as
1225 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001226 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001227 """
Azim Khanb31aa442018-07-03 11:57:54 +01001228 test_str = 'yahoo:google'
1229 splits = escaped_split(test_str, ':')
1230 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001231
1232 def test_escaped_input(self):
1233 """
Azim Khanb31aa442018-07-03 11:57:54 +01001234 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001235 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001236 """
Azim Khanb31aa442018-07-03 11:57:54 +01001237 test_str = r'yahoo\:google:facebook'
1238 splits = escaped_split(test_str, ':')
1239 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001240
1241 def test_escaped_escape(self):
1242 """
Azim Khanb31aa442018-07-03 11:57:54 +01001243 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001244 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001245 """
Azim Khan4084ec72018-07-05 14:20:08 +01001246 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +01001247 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001248 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001249
1250 def test_all_at_once(self):
1251 """
Azim Khanb31aa442018-07-03 11:57:54 +01001252 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001253 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001254 """
Azim Khan4084ec72018-07-05 14:20:08 +01001255 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +01001256 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001257 self.assertEqual(splits, [r'yahoo\\', r'google',
1258 r'facebook\:instagram\\',
1259 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001260
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001261
1262class ParseTestData(TestCase):
1263 """
1264 Test suite for parse test data.
1265 """
1266
1267 def test_parser(self):
1268 """
1269 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001270 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001271 """
1272 data = """
1273Diffie-Hellman full exchange #1
1274dhm_do_dhm:10:"23":10:"5"
1275
1276Diffie-Hellman full exchange #2
1277dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1278
1279Diffie-Hellman full exchange #3
1280dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1281
1282Diffie-Hellman selftest
1283dhm_selftest:
1284"""
Azim Khanb31aa442018-07-03 11:57:54 +01001285 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine8b022352020-03-24 18:36:56 +01001286 # List of (name, function_name, dependencies, args)
1287 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001288 test1, test2, test3, test4 = tests
Gilles Peskinec6279172023-02-23 20:47:24 +01001289 self.assertEqual(test1[0], 3)
Gilles Peskinef122aed2022-12-03 22:58:52 +01001290 self.assertEqual(test1[1], 'Diffie-Hellman full exchange #1')
1291 self.assertEqual(test1[2], 'dhm_do_dhm')
1292 self.assertEqual(test1[3], [])
1293 self.assertEqual(test1[4], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001294
Gilles Peskinec6279172023-02-23 20:47:24 +01001295 self.assertEqual(test2[0], 6)
Gilles Peskinef122aed2022-12-03 22:58:52 +01001296 self.assertEqual(test2[1], 'Diffie-Hellman full exchange #2')
1297 self.assertEqual(test2[2], 'dhm_do_dhm')
1298 self.assertEqual(test2[3], [])
1299 self.assertEqual(test2[4], ['10', '"93450983094850938450983409623"',
Azim Khanb31aa442018-07-03 11:57:54 +01001300 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001301
Gilles Peskinec6279172023-02-23 20:47:24 +01001302 self.assertEqual(test3[0], 9)
Gilles Peskinef122aed2022-12-03 22:58:52 +01001303 self.assertEqual(test3[1], 'Diffie-Hellman full exchange #3')
1304 self.assertEqual(test3[2], 'dhm_do_dhm')
1305 self.assertEqual(test3[3], [])
1306 self.assertEqual(test3[4], ['10',
Azim Khanb31aa442018-07-03 11:57:54 +01001307 '"9345098382739712938719287391879381271"',
1308 '10',
1309 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001310
Gilles Peskinec6279172023-02-23 20:47:24 +01001311 self.assertEqual(test4[0], 12)
Gilles Peskinef122aed2022-12-03 22:58:52 +01001312 self.assertEqual(test4[1], 'Diffie-Hellman selftest')
1313 self.assertEqual(test4[2], 'dhm_selftest')
Azim Khanb31aa442018-07-03 11:57:54 +01001314 self.assertEqual(test4[3], [])
Gilles Peskinef122aed2022-12-03 22:58:52 +01001315 self.assertEqual(test4[4], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001316
1317 def test_with_dependencies(self):
1318 """
1319 Test that tests with dependencies are parsed.
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
1325dhm_do_dhm:10:"23":10:"5"
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)
Gilles Peskine8b022352020-03-24 18:36:56 +01001332 # List of (name, function_name, dependencies, args)
1333 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001334 test1, test2 = tests
Gilles Peskinec6279172023-02-23 20:47:24 +01001335 self.assertEqual(test1[0], 4)
Gilles Peskinef122aed2022-12-03 22:58:52 +01001336 self.assertEqual(test1[1], 'Diffie-Hellman full exchange #1')
1337 self.assertEqual(test1[2], 'dhm_do_dhm')
1338 self.assertEqual(test1[3], ['YAHOO'])
1339 self.assertEqual(test1[4], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001340
Gilles Peskinec6279172023-02-23 20:47:24 +01001341 self.assertEqual(test2[0], 7)
Gilles Peskinef122aed2022-12-03 22:58:52 +01001342 self.assertEqual(test2[1], 'Diffie-Hellman full exchange #2')
1343 self.assertEqual(test2[2], 'dhm_do_dhm')
1344 self.assertEqual(test2[3], [])
1345 self.assertEqual(test2[4], ['10', '"93450983094850938450983409623"',
Azim Khanb31aa442018-07-03 11:57:54 +01001346 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001347
1348 def test_no_args(self):
1349 """
Azim Khanb31aa442018-07-03 11:57:54 +01001350 Test GeneratorInputError is raised when test function name and
1351 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001352 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001353 """
1354 data = """
1355Diffie-Hellman full exchange #1
1356depends_on:YAHOO
1357
1358
1359Diffie-Hellman full exchange #2
1360dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1361
1362"""
Azim Khanb31aa442018-07-03 11:57:54 +01001363 stream = StringIOWrapper('test_suite_ut.function', data)
1364 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001365 try:
Gilles Peskinef122aed2022-12-03 22:58:52 +01001366 for _, _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001367 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001368 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001369 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001370
1371 def test_incomplete_data(self):
1372 """
Azim Khanb31aa442018-07-03 11:57:54 +01001373 Test GeneratorInputError is raised when test function name
1374 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001375 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001376 """
1377 data = """
1378Diffie-Hellman full exchange #1
1379depends_on:YAHOO
1380"""
Azim Khanb31aa442018-07-03 11:57:54 +01001381 stream = StringIOWrapper('test_suite_ut.function', data)
1382 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001383 try:
Gilles Peskinef122aed2022-12-03 22:58:52 +01001384 for _, _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001385 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001386 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001387 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001388
1389
1390class GenDepCheck(TestCase):
1391 """
Azim Khanb31aa442018-07-03 11:57:54 +01001392 Test suite for gen_dep_check(). It is assumed this function is
1393 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001394 """
1395
1396 def test_gen_dep_check(self):
1397 """
1398 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001399 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001400 """
1401 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001402 case 5:
1403 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001404#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001405 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001406#else
Azim Khand61b8372017-07-10 11:54:01 +01001407 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001408#endif
Azim Khand61b8372017-07-10 11:54:01 +01001409 }
1410 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001411 out = gen_dep_check(5, 'YAHOO')
1412 self.assertEqual(out, expected)
1413
Azim Khanb31aa442018-07-03 11:57:54 +01001414 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001415 """
1416 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001417 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001418 """
1419 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001420 case 5:
1421 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001422#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001423 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001424#else
Azim Khand61b8372017-07-10 11:54:01 +01001425 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001426#endif
Azim Khand61b8372017-07-10 11:54:01 +01001427 }
1428 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001429 out = gen_dep_check(5, '!YAHOO')
1430 self.assertEqual(out, expected)
1431
1432 def test_empty_dependency(self):
1433 """
1434 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001435 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001436 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001437 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001438
1439 def test_negative_dep_id(self):
1440 """
1441 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001442 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001443 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001444 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001445
1446
1447class GenExpCheck(TestCase):
1448 """
Azim Khanb31aa442018-07-03 11:57:54 +01001449 Test suite for gen_expression_check(). It is assumed this function
1450 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001451 """
1452
1453 def test_gen_exp_check(self):
1454 """
1455 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001456 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001457 """
1458 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001459 case 5:
1460 {
1461 *out_value = YAHOO;
1462 }
1463 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001464 out = gen_expression_check(5, 'YAHOO')
1465 self.assertEqual(out, expected)
1466
1467 def test_invalid_expression(self):
1468 """
1469 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001470 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001471 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001472 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001473
1474 def test_negative_exp_id(self):
1475 """
1476 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001477 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001478 """
Azim Khanb31aa442018-07-03 11:57:54 +01001479 self.assertRaises(GeneratorInputError, gen_expression_check,
1480 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001481
1482
Azim Khanb31aa442018-07-03 11:57:54 +01001483class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001484 """
Azim Khanb31aa442018-07-03 11:57:54 +01001485 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001486 """
1487
Azim Khanb31aa442018-07-03 11:57:54 +01001488 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001489 """
Azim Khanb31aa442018-07-03 11:57:54 +01001490 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001491 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001492 """
Azim Khanb31aa442018-07-03 11:57:54 +01001493 stream = StringIOWrapper('test_suite_ut.data', '')
1494 unique_dependencies = []
1495 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001496 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001497 self.assertEqual(len(unique_dependencies), 0)
1498 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001499
1500 def test_unique_dep_ids(self):
1501 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001502
1503 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001504 """
Azim Khanb31aa442018-07-03 11:57:54 +01001505 stream = StringIOWrapper('test_suite_ut.data', '')
1506 unique_dependencies = []
1507 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1508 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001509 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001510 case 0:
1511 {
Azim Khan599cd242017-07-06 17:34:27 +01001512#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001513 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001514#else
Azim Khand61b8372017-07-10 11:54:01 +01001515 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001516#endif
Azim Khand61b8372017-07-10 11:54:01 +01001517 }
1518 break;
1519 case 1:
1520 {
Azim Khan599cd242017-07-06 17:34:27 +01001521#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001522 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001523#else
Azim Khand61b8372017-07-10 11:54:01 +01001524 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001525#endif
Azim Khand61b8372017-07-10 11:54:01 +01001526 }
1527 break;
1528 case 2:
1529 {
Azim Khan599cd242017-07-06 17:34:27 +01001530#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001531 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001532#else
Azim Khand61b8372017-07-10 11:54:01 +01001533 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001534#endif
Azim Khand61b8372017-07-10 11:54:01 +01001535 }
1536 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001537 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001538 self.assertEqual(len(unique_dependencies), 3)
1539 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001540
1541 def test_dep_id_repeat(self):
1542 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001543
1544 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001545 """
Azim Khanb31aa442018-07-03 11:57:54 +01001546 stream = StringIOWrapper('test_suite_ut.data', '')
1547 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001548 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001549 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1550 unique_dependencies)
1551 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1552 unique_dependencies)
1553 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1554 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001555 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001556 case 0:
1557 {
Azim Khan599cd242017-07-06 17:34:27 +01001558#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001559 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001560#else
Azim Khand61b8372017-07-10 11:54:01 +01001561 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001562#endif
Azim Khand61b8372017-07-10 11:54:01 +01001563 }
1564 break;
1565 case 1:
1566 {
Azim Khan599cd242017-07-06 17:34:27 +01001567#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001568 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001569#else
Azim Khand61b8372017-07-10 11:54:01 +01001570 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001571#endif
Azim Khand61b8372017-07-10 11:54:01 +01001572 }
1573 break;
1574 case 2:
1575 {
Azim Khan599cd242017-07-06 17:34:27 +01001576#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001577 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001578#else
Azim Khand61b8372017-07-10 11:54:01 +01001579 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001580#endif
Azim Khand61b8372017-07-10 11:54:01 +01001581 }
1582 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001583 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001584 self.assertEqual(len(unique_dependencies), 3)
1585 self.assertEqual(stream.getvalue(),
1586 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001587
1588
1589class WriteParams(TestCase):
1590 """
1591 Test Suite for testing write_parameters().
1592 """
1593
1594 def test_no_params(self):
1595 """
1596 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001597 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001598 """
Azim Khanb31aa442018-07-03 11:57:54 +01001599 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001600 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001601 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001602 self.assertEqual(len(unique_expressions), 0)
1603 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001604 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001605
1606 def test_no_exp_param(self):
1607 """
1608 Test when there is no 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, ['"Yahoo"', '"abcdef00"',
1614 '0'],
1615 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001616 unique_expressions)
1617 self.assertEqual(len(unique_expressions), 0)
1618 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001619 self.assertEqual(stream.getvalue(),
1620 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001621
1622 def test_hex_format_int_param(self):
1623 """
1624 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001625 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001626 """
Azim Khanb31aa442018-07-03 11:57:54 +01001627 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001628 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001629 expression_code = write_parameters(stream,
1630 ['"Yahoo"', '"abcdef00"', '0xAA'],
1631 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001632 unique_expressions)
1633 self.assertEqual(len(unique_expressions), 0)
1634 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001635 self.assertEqual(stream.getvalue(),
1636 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001637
1638 def test_with_exp_param(self):
1639 """
1640 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001641 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001642 """
Azim Khanb31aa442018-07-03 11:57:54 +01001643 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001644 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001645 expression_code = write_parameters(stream,
1646 ['"Yahoo"', '"abcdef00"', '0',
1647 'MACRO1', 'MACRO2', 'MACRO3'],
1648 ['char*', 'hex', 'int',
1649 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001650 unique_expressions)
1651 self.assertEqual(len(unique_expressions), 3)
1652 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1653 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001654 case 0:
1655 {
1656 *out_value = MACRO1;
1657 }
1658 break;
1659 case 1:
1660 {
1661 *out_value = MACRO2;
1662 }
1663 break;
1664 case 2:
1665 {
1666 *out_value = MACRO3;
1667 }
1668 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001669 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001670 self.assertEqual(stream.getvalue(),
1671 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1672 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001673
Azim Khanb31aa442018-07-03 11:57:54 +01001674 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001675 """
1676 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001677 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001678 """
Azim Khanb31aa442018-07-03 11:57:54 +01001679 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001680 unique_expressions = []
1681 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001682 expression_code += write_parameters(stream,
1683 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1684 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001685 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001686 expression_code += write_parameters(stream,
1687 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1688 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001689 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001690 expression_code += write_parameters(stream,
1691 ['0', 'MACRO3', 'MACRO1'],
1692 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001693 unique_expressions)
1694 self.assertEqual(len(unique_expressions), 3)
1695 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1696 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001697 case 0:
1698 {
1699 *out_value = MACRO1;
1700 }
1701 break;
1702 case 1:
1703 {
1704 *out_value = MACRO2;
1705 }
1706 break;
1707 case 2:
1708 {
1709 *out_value = MACRO3;
1710 }
1711 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001712 self.assertEqual(expression_code, expected_expression_code)
1713 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1714:hex:"abcdef00":exp:1:exp:2
1715:int:0:exp:2:exp:0
1716'''
Azim Khanb31aa442018-07-03 11:57:54 +01001717 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001718
1719
Azim Khanb31aa442018-07-03 11:57:54 +01001720class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001721 """
Azim Khanb31aa442018-07-03 11:57:54 +01001722 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001723 """
Azim Khanb31aa442018-07-03 11:57:54 +01001724 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001725 """
Azim Khanb31aa442018-07-03 11:57:54 +01001726 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001727
1728 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001729 """
Azim Khanb31aa442018-07-03 11:57:54 +01001730 dep_check_code, expression_code = \
1731 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001732 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1733 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1734
Azim Khanb31aa442018-07-03 11:57:54 +01001735 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001736 """
Azim Khanb31aa442018-07-03 11:57:54 +01001737 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001738
1739 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001740 """
Azim Khanb31aa442018-07-03 11:57:54 +01001741 dep_check_code, expression_code = \
1742 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1743 'EXPRESSION_CODE')
1744 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001745#if defined(SUITE_DEP)
1746DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001747#endif
1748'''
1749 expected_expression_code = '''
1750#if defined(SUITE_DEP)
1751EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001752#endif
1753'''
Azim Khanb31aa442018-07-03 11:57:54 +01001754 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001755 self.assertEqual(expression_code, expected_expression_code)
1756
1757 def test_no_dep_no_exp(self):
1758 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001759 Test when there are no dependency and expression code.
1760 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001761 """
Azim Khanb31aa442018-07-03 11:57:54 +01001762 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001763 self.assertEqual(dep_check_code, '')
1764 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001765
1766
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001767class GenFromTestData(TestCase):
1768 """
1769 Test suite for gen_from_test_data()
1770 """
1771
Azim Khanb31aa442018-07-03 11:57:54 +01001772 @staticmethod
1773 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001774 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001775 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001776 def test_intermediate_data_file(func_mock1,
1777 write_parameters_mock,
1778 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001779 """
1780 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001781 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001782 """
1783 data = '''
1784My test
1785depends_on:DEP1
1786func1:0
1787'''
1788 data_f = StringIOWrapper('test_suite_ut.data', data)
1789 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1790 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001791 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001792 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001793 write_dependencies_mock.side_effect = write_dependencies
1794 func_mock1.side_effect = gen_suite_dep_checks
1795 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1796 write_dependencies_mock.assert_called_with(out_data_f,
1797 ['DEP1'], ['DEP1'])
1798 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1799 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001800 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001801 case 0:
1802 {
Azim Khan599cd242017-07-06 17:34:27 +01001803#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001804 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001805#else
Azim Khand61b8372017-07-10 11:54:01 +01001806 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001807#endif
Azim Khand61b8372017-07-10 11:54:01 +01001808 }
1809 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001810 func_mock1.assert_called_with(
1811 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001812
1813 def test_function_not_found(self):
1814 """
1815 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001816 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001817 """
1818 data = '''
1819My test
1820depends_on:DEP1
1821func1:0
1822'''
1823 data_f = StringIOWrapper('test_suite_ut.data', data)
1824 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1825 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001826 suite_dependencies = []
1827 self.assertRaises(GeneratorInputError, gen_from_test_data,
1828 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001829
1830 def test_different_func_args(self):
1831 """
Azim Khanb31aa442018-07-03 11:57:54 +01001832 Test that AssertError is raised when no. of parameters and
1833 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001834 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001835 """
1836 data = '''
1837My test
1838depends_on:DEP1
1839func1:0
1840'''
1841 data_f = StringIOWrapper('test_suite_ut.data', data)
1842 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001843 func_info = {'test_func2': (1, ('int', 'hex'))}
1844 suite_dependencies = []
1845 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1846 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001847
1848 def test_output(self):
1849 """
1850 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001851 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001852 """
1853 data = '''
1854My test 1
1855depends_on:DEP1
1856func1:0:0xfa:MACRO1:MACRO2
1857
1858My test 2
1859depends_on:DEP1:DEP2
1860func2:"yahoo":88:MACRO1
1861'''
1862 data_f = StringIOWrapper('test_suite_ut.data', data)
1863 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001864 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1865 'test_func2': (1, ('char*', 'int', 'int'))}
1866 suite_dependencies = []
1867 dep_check_code, expression_code = \
1868 gen_from_test_data(data_f, out_data_f, func_info,
1869 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001870 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001871 case 0:
1872 {
Azim Khan599cd242017-07-06 17:34:27 +01001873#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001874 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001875#else
Azim Khand61b8372017-07-10 11:54:01 +01001876 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001877#endif
Azim Khand61b8372017-07-10 11:54:01 +01001878 }
1879 break;
1880 case 1:
1881 {
Azim Khan599cd242017-07-06 17:34:27 +01001882#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001883 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001884#else
Azim Khand61b8372017-07-10 11:54:01 +01001885 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001886#endif
Azim Khand61b8372017-07-10 11:54:01 +01001887 }
1888 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001889 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001890depends_on:0
18910:int:0:int:0xfa:exp:0:exp:1
1892
1893My test 2
1894depends_on:0:1
18951:char*:"yahoo":int:88:exp:0
1896
1897'''
1898 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001899 case 0:
1900 {
1901 *out_value = MACRO1;
1902 }
1903 break;
1904 case 1:
1905 {
1906 *out_value = MACRO2;
1907 }
1908 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001909 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001910 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001911 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001912
1913
Azim Khanb31aa442018-07-03 11:57:54 +01001914if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001915 unittest_main()