blob: 29d9e4f4463c779c8f533d23af33c93f2aeda19a [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#
Azim Khan4084ec72018-07-05 14:20:08 +01004# Copyright (C) 2018, Arm Limited, All Rights Reserved
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.
18#
Azim Khanb31aa442018-07-03 11:57:54 +010019# This file is part of Mbed TLS (https://tls.mbed.org)
Azim Khan4b543232017-06-30 09:35:21 +010020
21"""
Mohammad Azim Khan78befd92018-03-06 11:49:41 +000022Unit tests for generate_test_code.py
Azim Khan4b543232017-06-30 09:35:21 +010023"""
24
25
Azim Khanb31aa442018-07-03 11:57:54 +010026from StringIO import StringIO
27from unittest import TestCase, main as unittest_main
28from mock import patch
29from generate_test_code import gen_dependencies, gen_dependencies_one_line
30from generate_test_code import gen_function_wrapper, gen_dispatch
31from generate_test_code import parse_until_pattern, GeneratorInputError
32from generate_test_code import parse_suite_dependencies
33from generate_test_code import parse_function_dependencies
34from generate_test_code import parse_function_signature, parse_function_code
35from generate_test_code import parse_functions, END_HEADER_REGEX
36from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
37from generate_test_code import parse_test_data, gen_dep_check
38from generate_test_code import gen_expression_check, write_dependencies
39from generate_test_code import write_parameters, gen_suite_dep_checks
40from generate_test_code import gen_from_test_data
41
42
Azim Khan4b543232017-06-30 09:35:21 +010043class GenDep(TestCase):
44 """
45 Test suite for function gen_dep()
46 """
47
Azim Khanb31aa442018-07-03 11:57:54 +010048 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010049 """
Azim Khanb31aa442018-07-03 11:57:54 +010050 Test that gen_dep() correctly creates dependencies for given
51 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010052 :return:
Azim Khan4b543232017-06-30 09:35:21 +010053 """
Azim Khanb31aa442018-07-03 11:57:54 +010054 dependencies = ['DEP1', 'DEP2']
55 dep_start, dep_end = gen_dependencies(dependencies)
56 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010057 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010058 self.assertEqual(preprocessor1, '#if defined(DEP1)',
59 'Preprocessor generated incorrectly')
60 self.assertEqual(preprocessor2, '#if defined(DEP2)',
61 'Preprocessor generated incorrectly')
62 self.assertEqual(endif1, '#endif /* DEP2 */',
63 'Preprocessor generated incorrectly')
64 self.assertEqual(endif2, '#endif /* DEP1 */',
65 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010066
Azim Khanb31aa442018-07-03 11:57:54 +010067 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010068 """
Azim Khanb31aa442018-07-03 11:57:54 +010069 Test that gen_dep() correctly creates dependencies for given
70 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010071 :return:
Azim Khan4b543232017-06-30 09:35:21 +010072 """
Azim Khanb31aa442018-07-03 11:57:54 +010073 dependencies = ['!DEP1', '!DEP2']
74 dep_start, dep_end = gen_dependencies(dependencies)
75 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010076 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010077 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
78 'Preprocessor generated incorrectly')
79 self.assertEqual(preprocessor2, '#if !defined(DEP2)',
80 'Preprocessor generated incorrectly')
81 self.assertEqual(endif1, '#endif /* !DEP2 */',
82 'Preprocessor generated incorrectly')
83 self.assertEqual(endif2, '#endif /* !DEP1 */',
84 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010085
Azim Khanb31aa442018-07-03 11:57:54 +010086 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010087 """
Azim Khanb31aa442018-07-03 11:57:54 +010088 Test that gen_dep() correctly creates dependencies for given
89 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010090 :return:
Azim Khan4b543232017-06-30 09:35:21 +010091 """
Azim Khanb31aa442018-07-03 11:57:54 +010092 dependencies = ['!DEP1', 'DEP2']
93 dep_start, dep_end = gen_dependencies(dependencies)
94 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010095 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010096 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
97 'Preprocessor generated incorrectly')
98 self.assertEqual(preprocessor2, '#if defined(DEP2)',
99 'Preprocessor generated incorrectly')
100 self.assertEqual(endif1, '#endif /* DEP2 */',
101 'Preprocessor generated incorrectly')
102 self.assertEqual(endif2, '#endif /* !DEP1 */',
103 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100104
Azim Khanb31aa442018-07-03 11:57:54 +0100105 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100106 """
Azim Khanb31aa442018-07-03 11:57:54 +0100107 Test that gen_dep() correctly creates dependencies for given
108 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100109 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100110 """
Azim Khanb31aa442018-07-03 11:57:54 +0100111 dependencies = []
112 dep_start, dep_end = gen_dependencies(dependencies)
113 self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
114 self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100115
Azim Khanb31aa442018-07-03 11:57:54 +0100116 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100117 """
Azim Khanb31aa442018-07-03 11:57:54 +0100118 Test that gen_dep() correctly creates dependencies for given
119 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100120 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100121 """
Azim Khanb31aa442018-07-03 11:57:54 +0100122 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100123 count = 10
124 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100125 dependencies.append('DEP%d' % i)
126 dep_start, dep_end = gen_dependencies(dependencies)
127 self.assertEqual(len(dep_start.splitlines()), count,
128 'Preprocessor generated incorrectly')
129 self.assertEqual(len(dep_end.splitlines()), count,
130 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100131
132
133class GenDepOneLine(TestCase):
134 """
Azim Khanb31aa442018-07-03 11:57:54 +0100135 Test Suite for testing gen_dependencies_one_line()
Azim Khan4b543232017-06-30 09:35:21 +0100136 """
137
Azim Khanb31aa442018-07-03 11:57:54 +0100138 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100139 """
Azim Khanb31aa442018-07-03 11:57:54 +0100140 Test that gen_dep() correctly creates dependencies for given
141 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100142 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100143 """
Azim Khanb31aa442018-07-03 11:57:54 +0100144 dependencies = ['DEP1', 'DEP2']
145 dep_str = gen_dependencies_one_line(dependencies)
146 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
147 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100148
Azim Khanb31aa442018-07-03 11:57:54 +0100149 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100150 """
Azim Khanb31aa442018-07-03 11:57:54 +0100151 Test that gen_dep() correctly creates dependencies for given
152 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100153 :return:
154 """
Azim Khanb31aa442018-07-03 11:57:54 +0100155 dependencies = ['!DEP1', '!DEP2']
156 dep_str = gen_dependencies_one_line(dependencies)
157 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
158 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100159
Azim Khanb31aa442018-07-03 11:57:54 +0100160 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100161 """
Azim Khanb31aa442018-07-03 11:57:54 +0100162 Test that gen_dep() correctly creates dependencies for given
163 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100164 :return:
165 """
Azim Khanb31aa442018-07-03 11:57:54 +0100166 dependencies = ['!DEP1', 'DEP2']
167 dep_str = gen_dependencies_one_line(dependencies)
168 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
169 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100170
Azim Khanb31aa442018-07-03 11:57:54 +0100171 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100172 """
Azim Khanb31aa442018-07-03 11:57:54 +0100173 Test that gen_dep() correctly creates dependencies for given
174 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100175 :return:
176 """
Azim Khanb31aa442018-07-03 11:57:54 +0100177 dependencies = []
178 dep_str = gen_dependencies_one_line(dependencies)
179 self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100180
Azim Khanb31aa442018-07-03 11:57:54 +0100181 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100182 """
Azim Khanb31aa442018-07-03 11:57:54 +0100183 Test that gen_dep() correctly creates dependencies for given
184 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100185 :return:
186 """
Azim Khanb31aa442018-07-03 11:57:54 +0100187 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100188 count = 10
189 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100190 dependencies.append('DEP%d' % i)
191 dep_str = gen_dependencies_one_line(dependencies)
192 expected = '#if ' + ' && '.join(['defined(%s)' %
193 x for x in dependencies])
194 self.assertEqual(dep_str, expected,
195 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100196
197
198class GenFunctionWrapper(TestCase):
199 """
200 Test Suite for testing gen_function_wrapper()
201 """
202
203 def test_params_unpack(self):
204 """
205 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100206
207 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100208 """
209 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
210 expected = '''
211void test_a_wrapper( void ** params )
212{
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100213
Azim Khan4b543232017-06-30 09:35:21 +0100214 test_a( a, b, c, d );
215}
216'''
217 self.assertEqual(code, expected)
218
219 def test_local(self):
220 """
221 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100222
223 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100224 """
Azim Khanb31aa442018-07-03 11:57:54 +0100225 code = gen_function_wrapper('test_a',
226 'int x = 1;', ('x', 'b', 'c', 'd'))
Azim Khan4b543232017-06-30 09:35:21 +0100227 expected = '''
228void test_a_wrapper( void ** params )
229{
Azim Khan4b543232017-06-30 09:35:21 +0100230int x = 1;
231 test_a( x, b, c, d );
232}
233'''
234 self.assertEqual(code, expected)
235
236 def test_empty_params(self):
237 """
238 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100239
240 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100241 """
242 code = gen_function_wrapper('test_a', '', ())
243 expected = '''
244void test_a_wrapper( void ** params )
245{
246 (void)params;
247
248 test_a( );
249}
250'''
251 self.assertEqual(code, expected)
252
253
254class GenDispatch(TestCase):
255 """
256 Test suite for testing gen_dispatch()
257 """
258
259 def test_dispatch(self):
260 """
261 Test that dispatch table entry is generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100262 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100263 """
264 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
265 expected = '''
266#if defined(DEP1) && defined(DEP2)
267 test_a_wrapper,
268#else
269 NULL,
270#endif
271'''
272 self.assertEqual(code, expected)
273
Azim Khanb31aa442018-07-03 11:57:54 +0100274 def test_empty_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100275 """
276 Test empty dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100277 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100278 """
279 code = gen_dispatch('test_a', [])
280 expected = '''
281 test_a_wrapper,
282'''
283 self.assertEqual(code, expected)
284
285
286class StringIOWrapper(StringIO, object):
287 """
288 file like class to mock file object in tests.
289 """
Azim Khan4084ec72018-07-05 14:20:08 +0100290 def __init__(self, file_name, data, line_no=0):
Azim Khan4b543232017-06-30 09:35:21 +0100291 """
292 Init file handle.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100293
294 :param file_name:
Azim Khan4b543232017-06-30 09:35:21 +0100295 :param data:
296 :param line_no:
297 """
298 super(StringIOWrapper, self).__init__(data)
299 self.line_no = line_no
300 self.name = file_name
301
302 def next(self):
303 """
Azim Khanb31aa442018-07-03 11:57:54 +0100304 Iterator method. This method overrides base class's
305 next method and extends the next method to count the line
306 numbers as each line is read.
Azim Khan4b543232017-06-30 09:35:21 +0100307
Azim Khanb31aa442018-07-03 11:57:54 +0100308 :return: Line read from file.
309 """
Azim Khan4084ec72018-07-05 14:20:08 +0100310 line = super(StringIOWrapper, self).next()
311 return line
Azim Khanb31aa442018-07-03 11:57:54 +0100312
313 __next__ = next
314
315 def readline(self, length=0):
Azim Khan4b543232017-06-30 09:35:21 +0100316 """
317 Wrap the base class readline.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100318
Azim Khanb31aa442018-07-03 11:57:54 +0100319 :param length:
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100320 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100321 """
322 line = super(StringIOWrapper, self).readline()
Azim Khan4084ec72018-07-05 14:20:08 +0100323 if line is not None:
Azim Khan4b543232017-06-30 09:35:21 +0100324 self.line_no += 1
325 return line
326
327
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000328class ParseUntilPattern(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100329 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000330 Test Suite for testing parse_until_pattern().
Azim Khan4b543232017-06-30 09:35:21 +0100331 """
332
333 def test_suite_headers(self):
334 """
335 Test that suite headers are parsed correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100336
337 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100338 """
339 data = '''#include "mbedtls/ecp.h"
340
341#define ECP_PF_UNKNOWN -1
342/* END_HEADER */
343'''
344 expected = '''#line 1 "test_suite_ut.function"
345#include "mbedtls/ecp.h"
346
347#define ECP_PF_UNKNOWN -1
348'''
Azim Khanb31aa442018-07-03 11:57:54 +0100349 stream = StringIOWrapper('test_suite_ut.function', data, line_no=0)
350 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100351 self.assertEqual(headers, expected)
352
353 def test_line_no(self):
354 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100355 Test that #line is set to correct line no. in source .function file.
356
357 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100358 """
359 data = '''#include "mbedtls/ecp.h"
360
361#define ECP_PF_UNKNOWN -1
362/* END_HEADER */
363'''
364 offset_line_no = 5
365 expected = '''#line %d "test_suite_ut.function"
366#include "mbedtls/ecp.h"
367
368#define ECP_PF_UNKNOWN -1
369''' % (offset_line_no + 1)
Azim Khanb31aa442018-07-03 11:57:54 +0100370 stream = StringIOWrapper('test_suite_ut.function', data,
371 offset_line_no)
372 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100373 self.assertEqual(headers, expected)
374
375 def test_no_end_header_comment(self):
376 """
Azim Khanb31aa442018-07-03 11:57:54 +0100377 Test that InvalidFileFormat is raised when end header comment is
378 missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100379 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100380 """
381 data = '''#include "mbedtls/ecp.h"
382
383#define ECP_PF_UNKNOWN -1
384
385'''
Azim Khanb31aa442018-07-03 11:57:54 +0100386 stream = StringIOWrapper('test_suite_ut.function', data)
387 self.assertRaises(GeneratorInputError, parse_until_pattern, stream,
388 END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100389
390
Azim Khanb31aa442018-07-03 11:57:54 +0100391class ParseSuiteDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100392 """
Azim Khanb31aa442018-07-03 11:57:54 +0100393 Test Suite for testing parse_suite_dependencies().
Azim Khan4b543232017-06-30 09:35:21 +0100394 """
395
Azim Khanb31aa442018-07-03 11:57:54 +0100396 def test_suite_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100397 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100398
399 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100400 """
401 data = '''
402 * depends_on:MBEDTLS_ECP_C
403 * END_DEPENDENCIES
404 */
405'''
406 expected = ['MBEDTLS_ECP_C']
Azim Khanb31aa442018-07-03 11:57:54 +0100407 stream = StringIOWrapper('test_suite_ut.function', data)
408 dependencies = parse_suite_dependencies(stream)
409 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100410
411 def test_no_end_dep_comment(self):
412 """
413 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100414 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100415 """
416 data = '''
417* depends_on:MBEDTLS_ECP_C
418'''
Azim Khanb31aa442018-07-03 11:57:54 +0100419 stream = StringIOWrapper('test_suite_ut.function', data)
420 self.assertRaises(GeneratorInputError, parse_suite_dependencies,
421 stream)
Azim Khan4b543232017-06-30 09:35:21 +0100422
Azim Khanb31aa442018-07-03 11:57:54 +0100423 def test_dependencies_split(self):
Azim Khan4b543232017-06-30 09:35:21 +0100424 """
425 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100426 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100427 """
428 data = '''
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100429 * depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
Azim Khan4b543232017-06-30 09:35:21 +0100430 * END_DEPENDENCIES
431 */
432'''
433 expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
Azim Khanb31aa442018-07-03 11:57:54 +0100434 stream = StringIOWrapper('test_suite_ut.function', data)
435 dependencies = parse_suite_dependencies(stream)
436 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100437
438
Azim Khanb31aa442018-07-03 11:57:54 +0100439class ParseFuncDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100440 """
Azim Khanb31aa442018-07-03 11:57:54 +0100441 Test Suite for testing parse_function_dependencies()
Azim Khan4b543232017-06-30 09:35:21 +0100442 """
443
Azim Khanb31aa442018-07-03 11:57:54 +0100444 def test_function_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100445 """
Azim Khanb31aa442018-07-03 11:57:54 +0100446 Test that parse_function_dependencies() correctly parses function
447 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100448 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100449 """
Azim Khanb31aa442018-07-03 11:57:54 +0100450 line = '/* BEGIN_CASE ' \
451 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
Azim Khan4b543232017-06-30 09:35:21 +0100452 expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
Azim Khanb31aa442018-07-03 11:57:54 +0100453 dependencies = parse_function_dependencies(line)
454 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100455
Azim Khanb31aa442018-07-03 11:57:54 +0100456 def test_no_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100457 """
Azim Khanb31aa442018-07-03 11:57:54 +0100458 Test that parse_function_dependencies() correctly parses function
459 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100460 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100461 """
462 line = '/* BEGIN_CASE */'
Azim Khanb31aa442018-07-03 11:57:54 +0100463 dependencies = parse_function_dependencies(line)
464 self.assertEqual(dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100465
Azim Khanb31aa442018-07-03 11:57:54 +0100466 def test_tolerance(self):
Azim Khan4b543232017-06-30 09:35:21 +0100467 """
Azim Khanb31aa442018-07-03 11:57:54 +0100468 Test that parse_function_dependencies() correctly parses function
469 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100470 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100471 """
472 line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
Azim Khanb31aa442018-07-03 11:57:54 +0100473 dependencies = parse_function_dependencies(line)
474 self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
Azim Khan4b543232017-06-30 09:35:21 +0100475
476
477class ParseFuncSignature(TestCase):
478 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100479 Test Suite for parse_function_signature().
Azim Khan4b543232017-06-30 09:35:21 +0100480 """
481
482 def test_int_and_char_params(self):
483 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100484 Test int and char parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100485 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100486 """
487 line = 'void entropy_threshold( char * a, int b, int result )'
488 name, args, local, arg_dispatch = parse_function_signature(line)
489 self.assertEqual(name, 'entropy_threshold')
490 self.assertEqual(args, ['char*', 'int', 'int'])
491 self.assertEqual(local, '')
Azim Khanb31aa442018-07-03 11:57:54 +0100492 self.assertEqual(arg_dispatch, ['(char *) params[0]',
493 '*( (int *) params[1] )',
494 '*( (int *) params[2] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100495
496 def test_hex_params(self):
497 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100498 Test hex parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100499 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100500 """
Azim Khan5fcca462018-06-29 11:05:32 +0100501 line = 'void entropy_threshold( char * a, data_t * h, int result )'
Azim Khan4b543232017-06-30 09:35:21 +0100502 name, args, local, arg_dispatch = parse_function_signature(line)
503 self.assertEqual(name, 'entropy_threshold')
504 self.assertEqual(args, ['char*', 'hex', 'int'])
Azim Khanb31aa442018-07-03 11:57:54 +0100505 self.assertEqual(local,
Azim Khan4084ec72018-07-05 14:20:08 +0100506 ' data_t data1 = {(uint8_t *) params[1], '
Azim Khanb31aa442018-07-03 11:57:54 +0100507 '*( (uint32_t *) params[2] )};\n')
508 self.assertEqual(arg_dispatch, ['(char *) params[0]',
Azim Khan4084ec72018-07-05 14:20:08 +0100509 '&data1',
Azim Khanb31aa442018-07-03 11:57:54 +0100510 '*( (int *) params[3] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100511
512 def test_non_void_function(self):
513 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100514 Test invalid signature (non void).
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100515 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100516 """
Azim Khan5fcca462018-06-29 11:05:32 +0100517 line = 'int entropy_threshold( char * a, data_t * h, int result )'
Azim Khan4b543232017-06-30 09:35:21 +0100518 self.assertRaises(ValueError, parse_function_signature, line)
519
520 def test_unsupported_arg(self):
521 """
Azim Khan5fcca462018-06-29 11:05:32 +0100522 Test unsupported arguments (not among int, char * and data_t)
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100523 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100524 """
Azim Khan5fcca462018-06-29 11:05:32 +0100525 line = 'int entropy_threshold( char * a, data_t * h, int * result )'
Azim Khan4b543232017-06-30 09:35:21 +0100526 self.assertRaises(ValueError, parse_function_signature, line)
527
528 def test_no_params(self):
529 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100530 Test no parameters.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100531 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100532 """
533 line = 'void entropy_threshold()'
534 name, args, local, arg_dispatch = parse_function_signature(line)
535 self.assertEqual(name, 'entropy_threshold')
536 self.assertEqual(args, [])
537 self.assertEqual(local, '')
538 self.assertEqual(arg_dispatch, [])
539
540
541class ParseFunctionCode(TestCase):
542 """
543 Test suite for testing parse_function_code()
544 """
545
546 def test_no_function(self):
547 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100548 Test no test function found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100549 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100550 """
551 data = '''
552No
553test
554function
555'''
Azim Khanb31aa442018-07-03 11:57:54 +0100556 stream = StringIOWrapper('test_suite_ut.function', data)
557 self.assertRaises(GeneratorInputError, parse_function_code, stream, [],
558 [])
Azim Khan4b543232017-06-30 09:35:21 +0100559
560 def test_no_end_case_comment(self):
561 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100562 Test missing end case.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100563 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100564 """
565 data = '''
566void test_func()
567{
568}
569'''
Azim Khanb31aa442018-07-03 11:57:54 +0100570 stream = StringIOWrapper('test_suite_ut.function', data)
571 self.assertRaises(GeneratorInputError, parse_function_code, stream, [],
572 [])
Azim Khan4b543232017-06-30 09:35:21 +0100573
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000574 @patch("generate_test_code.parse_function_signature")
Azim Khanb31aa442018-07-03 11:57:54 +0100575 def test_function_called(self,
576 parse_function_signature_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100577 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100578 Test parse_function_code()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100579 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100580 """
581 parse_function_signature_mock.return_value = ('test_func', [], '', [])
582 data = '''
583void test_func()
584{
585}
586'''
Azim Khanb31aa442018-07-03 11:57:54 +0100587 stream = StringIOWrapper('test_suite_ut.function', data)
588 self.assertRaises(GeneratorInputError, parse_function_code,
589 stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100590 self.assertTrue(parse_function_signature_mock.called)
591 parse_function_signature_mock.assert_called_with('void test_func()\n')
592
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000593 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100594 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000595 @patch("generate_test_code.gen_function_wrapper")
596 @patch("generate_test_code.parse_function_signature")
Azim Khan4b543232017-06-30 09:35:21 +0100597 def test_return(self, parse_function_signature_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100598 gen_function_wrapper_mock,
599 gen_dependencies_mock,
600 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100601 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100602 Test generated code.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100603 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100604 """
605 parse_function_signature_mock.return_value = ('func', [], '', [])
606 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100607 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100608 gen_dispatch_mock.side_effect = gen_dispatch
609 data = '''
610void func()
611{
612 ba ba black sheep
613 have you any wool
614}
615/* END_CASE */
616'''
Azim Khanb31aa442018-07-03 11:57:54 +0100617 stream = StringIOWrapper('test_suite_ut.function', data)
618 name, arg, code, dispatch_code = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100619
Azim Khan4b543232017-06-30 09:35:21 +0100620 self.assertTrue(parse_function_signature_mock.called)
621 parse_function_signature_mock.assert_called_with('void func()\n')
622 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
623 self.assertEqual(name, 'test_func')
624 self.assertEqual(arg, [])
Azim Khan4084ec72018-07-05 14:20:08 +0100625 expected = '''#line 1 "test_suite_ut.function"
626
Azim Khan4b543232017-06-30 09:35:21 +0100627void test_func()
628{
629 ba ba black sheep
630 have you any wool
631exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100632 ;
Azim Khan4b543232017-06-30 09:35:21 +0100633}
634'''
635 self.assertEqual(code, expected)
636 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
637
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000638 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100639 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000640 @patch("generate_test_code.gen_function_wrapper")
641 @patch("generate_test_code.parse_function_signature")
Azim Khan4b543232017-06-30 09:35:21 +0100642 def test_with_exit_label(self, parse_function_signature_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100643 gen_function_wrapper_mock,
644 gen_dependencies_mock,
645 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100646 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100647 Test when exit label is present.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100648 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100649 """
650 parse_function_signature_mock.return_value = ('func', [], '', [])
651 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100652 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100653 gen_dispatch_mock.side_effect = gen_dispatch
654 data = '''
655void func()
656{
657 ba ba black sheep
658 have you any wool
659exit:
660 yes sir yes sir
661 3 bags full
662}
663/* END_CASE */
664'''
Azim Khanb31aa442018-07-03 11:57:54 +0100665 stream = StringIOWrapper('test_suite_ut.function', data)
666 _, _, code, _ = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100667
Azim Khan4084ec72018-07-05 14:20:08 +0100668 expected = '''#line 1 "test_suite_ut.function"
669
Azim Khan4b543232017-06-30 09:35:21 +0100670void test_func()
671{
672 ba ba black sheep
673 have you any wool
674exit:
675 yes sir yes sir
676 3 bags full
677}
678'''
679 self.assertEqual(code, expected)
680
681
682class ParseFunction(TestCase):
683 """
684 Test Suite for testing parse_functions()
685 """
686
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000687 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000688 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100689 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000690 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100691 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100692 """
Azim Khanb31aa442018-07-03 11:57:54 +0100693 def stop(*_unused):
694 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100695 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000696 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100697 data = '''/* BEGIN_HEADER */
698#include "mbedtls/ecp.h"
699
700#define ECP_PF_UNKNOWN -1
701/* END_HEADER */
702'''
Azim Khanb31aa442018-07-03 11:57:54 +0100703 stream = StringIOWrapper('test_suite_ut.function', data)
704 self.assertRaises(Exception, parse_functions, stream)
705 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100706 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000707
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000708 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000709 def test_begin_helper(self, parse_until_pattern_mock):
710 """
711 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100712 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000713 """
Azim Khanb31aa442018-07-03 11:57:54 +0100714 def stop(*_unused):
715 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000716 raise Exception
717 parse_until_pattern_mock.side_effect = stop
718 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100719void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000720{
Azim Khanb31aa442018-07-03 11:57:54 +0100721 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000722}
723/* END_SUITE_HELPERS */
724'''
Azim Khanb31aa442018-07-03 11:57:54 +0100725 stream = StringIOWrapper('test_suite_ut.function', data)
726 self.assertRaises(Exception, parse_functions, stream)
727 parse_until_pattern_mock.assert_called_with(stream,
728 END_SUITE_HELPERS_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100729 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100730
Azim Khanb31aa442018-07-03 11:57:54 +0100731 @patch("generate_test_code.parse_suite_dependencies")
732 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100733 """
Azim Khanb31aa442018-07-03 11:57:54 +0100734 Test that begin dep is checked and parse_suite_dependencies() is
735 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100736 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100737 """
Azim Khanb31aa442018-07-03 11:57:54 +0100738 def stop(*_unused):
739 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100740 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100741 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100742 data = '''/* BEGIN_DEPENDENCIES
743 * depends_on:MBEDTLS_ECP_C
744 * END_DEPENDENCIES
745 */
746'''
Azim Khanb31aa442018-07-03 11:57:54 +0100747 stream = StringIOWrapper('test_suite_ut.function', data)
748 self.assertRaises(Exception, parse_functions, stream)
749 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +0100750 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100751
Azim Khanb31aa442018-07-03 11:57:54 +0100752 @patch("generate_test_code.parse_function_dependencies")
753 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100754 """
Azim Khanb31aa442018-07-03 11:57:54 +0100755 Test that begin dep is checked and parse_function_dependencies() is
756 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100757 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100758 """
Azim Khanb31aa442018-07-03 11:57:54 +0100759 def stop(*_unused):
760 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100761 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100762 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100763
Azim Khanb31aa442018-07-03 11:57:54 +0100764 dependencies_str = '/* BEGIN_CASE ' \
765 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100766 data = '''%svoid test_func()
767{
768}
Azim Khanb31aa442018-07-03 11:57:54 +0100769''' % dependencies_str
770 stream = StringIOWrapper('test_suite_ut.function', data)
771 self.assertRaises(Exception, parse_functions, stream)
772 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +0100773 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100774
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000775 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +0100776 @patch("generate_test_code.parse_function_dependencies")
777 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +0100778 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000779 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100780 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100781 """
Azim Khanb31aa442018-07-03 11:57:54 +0100782 func_mock1.return_value = []
783 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +0100784{
785}
786'''
787 func_dispatch = '''
788 test_func_wrapper,
789'''
Azim Khanb31aa442018-07-03 11:57:54 +0100790 func_mock2.return_value = 'test_func', [],\
791 in_func_code, func_dispatch
792 dependencies_str = '/* BEGIN_CASE ' \
793 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100794 data = '''%svoid test_func()
795{
796}
Azim Khanb31aa442018-07-03 11:57:54 +0100797''' % dependencies_str
798 stream = StringIOWrapper('test_suite_ut.function', data)
799 suite_dependencies, dispatch_code, func_code, func_info = \
800 parse_functions(stream)
801 func_mock1.assert_called_with(dependencies_str)
802 func_mock2.assert_called_with(stream, [], [])
803 self.assertEqual(stream.line_no, 5)
804 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100805 expected_dispatch_code = '''/* Function Id: 0 */
806
807 test_func_wrapper,
808'''
809 self.assertEqual(dispatch_code, expected_dispatch_code)
810 self.assertEqual(func_code, in_func_code)
811 self.assertEqual(func_info, {'test_func': (0, [])})
812
813 def test_parsing(self):
814 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000815 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100816 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100817 """
818 data = '''/* BEGIN_HEADER */
819#include "mbedtls/ecp.h"
820
821#define ECP_PF_UNKNOWN -1
822/* END_HEADER */
823
824/* BEGIN_DEPENDENCIES
825 * depends_on:MBEDTLS_ECP_C
826 * END_DEPENDENCIES
827 */
828
829/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
830void func1()
831{
832}
833/* END_CASE */
834
835/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
836void func2()
837{
838}
839/* END_CASE */
840'''
Azim Khanb31aa442018-07-03 11:57:54 +0100841 stream = StringIOWrapper('test_suite_ut.function', data)
842 suite_dependencies, dispatch_code, func_code, func_info = \
843 parse_functions(stream)
844 self.assertEqual(stream.line_no, 23)
845 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +0100846
847 expected_dispatch_code = '''/* Function Id: 0 */
848
849#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
850 test_func1_wrapper,
851#else
852 NULL,
853#endif
854/* Function Id: 1 */
855
856#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
857 test_func2_wrapper,
858#else
859 NULL,
860#endif
861'''
862 self.assertEqual(dispatch_code, expected_dispatch_code)
863 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +0100864#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100865#include "mbedtls/ecp.h"
866
867#define ECP_PF_UNKNOWN -1
868#if defined(MBEDTLS_ENTROPY_NV_SEED)
869#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +0100870#line 13 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100871void test_func1()
872{
873exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100874 ;
Azim Khan4b543232017-06-30 09:35:21 +0100875}
876
877void test_func1_wrapper( void ** params )
878{
879 (void)params;
880
881 test_func1( );
882}
883#endif /* MBEDTLS_FS_IO */
884#endif /* MBEDTLS_ENTROPY_NV_SEED */
885#if defined(MBEDTLS_ENTROPY_NV_SEED)
886#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +0100887#line 19 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100888void test_func2()
889{
890exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100891 ;
Azim Khan4b543232017-06-30 09:35:21 +0100892}
893
894void test_func2_wrapper( void ** params )
895{
896 (void)params;
897
898 test_func2( );
899}
900#endif /* MBEDTLS_FS_IO */
901#endif /* MBEDTLS_ENTROPY_NV_SEED */
902#endif /* MBEDTLS_ECP_C */
903'''
904 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +0100905 self.assertEqual(func_info, {'test_func1': (0, []),
906 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +0100907
908 def test_same_function_name(self):
909 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000910 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100911 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100912 """
913 data = '''/* BEGIN_HEADER */
914#include "mbedtls/ecp.h"
915
916#define ECP_PF_UNKNOWN -1
917/* END_HEADER */
918
919/* BEGIN_DEPENDENCIES
920 * depends_on:MBEDTLS_ECP_C
921 * END_DEPENDENCIES
922 */
923
924/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
925void func()
926{
927}
928/* END_CASE */
929
930/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
931void func()
932{
933}
934/* END_CASE */
935'''
Azim Khanb31aa442018-07-03 11:57:54 +0100936 stream = StringIOWrapper('test_suite_ut.function', data)
937 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +0100938
939
Azim Khanb31aa442018-07-03 11:57:54 +0100940class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100941 """
Azim Khan599cd242017-07-06 17:34:27 +0100942 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +0100943 Note: Since escaped_split() output is used to write back to the
944 intermediate data file. Any escape characters in the input are
945 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100946 """
947
948 def test_invalid_input(self):
949 """
950 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100951 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100952 """
953 self.assertRaises(ValueError, escaped_split, '', 'string')
954
955 def test_empty_string(self):
956 """
Azim Khanb31aa442018-07-03 11:57:54 +0100957 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100958 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100959 """
960 splits = escaped_split('', ':')
961 self.assertEqual(splits, [])
962
963 def test_no_escape(self):
964 """
Azim Khanb31aa442018-07-03 11:57:54 +0100965 Test with no escape character. The behaviour should be same as
966 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100967 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100968 """
Azim Khanb31aa442018-07-03 11:57:54 +0100969 test_str = 'yahoo:google'
970 splits = escaped_split(test_str, ':')
971 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100972
973 def test_escaped_input(self):
974 """
Azim Khanb31aa442018-07-03 11:57:54 +0100975 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100976 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100977 """
Azim Khanb31aa442018-07-03 11:57:54 +0100978 test_str = r'yahoo\:google:facebook'
979 splits = escaped_split(test_str, ':')
980 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100981
982 def test_escaped_escape(self):
983 """
Azim Khanb31aa442018-07-03 11:57:54 +0100984 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100985 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100986 """
Azim Khan4084ec72018-07-05 14:20:08 +0100987 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +0100988 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +0100989 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100990
991 def test_all_at_once(self):
992 """
Azim Khanb31aa442018-07-03 11:57:54 +0100993 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100994 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100995 """
Azim Khan4084ec72018-07-05 14:20:08 +0100996 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +0100997 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +0100998 self.assertEqual(splits, [r'yahoo\\', r'google',
999 r'facebook\:instagram\\',
1000 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001001
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001002
1003class ParseTestData(TestCase):
1004 """
1005 Test suite for parse test data.
1006 """
1007
1008 def test_parser(self):
1009 """
1010 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001011 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001012 """
1013 data = """
1014Diffie-Hellman full exchange #1
1015dhm_do_dhm:10:"23":10:"5"
1016
1017Diffie-Hellman full exchange #2
1018dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1019
1020Diffie-Hellman full exchange #3
1021dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1022
1023Diffie-Hellman selftest
1024dhm_selftest:
1025"""
Azim Khanb31aa442018-07-03 11:57:54 +01001026 stream = StringIOWrapper('test_suite_ut.function', data)
1027 tests = [(name, test_function, dependencies, args)
1028 for name, test_function, dependencies, args in
1029 parse_test_data(stream)]
1030 test1, test2, test3, test4 = tests
1031 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1032 self.assertEqual(test1[1], 'dhm_do_dhm')
1033 self.assertEqual(test1[2], [])
1034 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001035
Azim Khanb31aa442018-07-03 11:57:54 +01001036 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1037 self.assertEqual(test2[1], 'dhm_do_dhm')
1038 self.assertEqual(test2[2], [])
1039 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1040 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001041
Azim Khanb31aa442018-07-03 11:57:54 +01001042 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1043 self.assertEqual(test3[1], 'dhm_do_dhm')
1044 self.assertEqual(test3[2], [])
1045 self.assertEqual(test3[3], ['10',
1046 '"9345098382739712938719287391879381271"',
1047 '10',
1048 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001049
Azim Khanb31aa442018-07-03 11:57:54 +01001050 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1051 self.assertEqual(test4[1], 'dhm_selftest')
1052 self.assertEqual(test4[2], [])
1053 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001054
1055 def test_with_dependencies(self):
1056 """
1057 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001058 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001059 """
1060 data = """
1061Diffie-Hellman full exchange #1
1062depends_on:YAHOO
1063dhm_do_dhm:10:"23":10:"5"
1064
1065Diffie-Hellman full exchange #2
1066dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1067
1068"""
Azim Khanb31aa442018-07-03 11:57:54 +01001069 stream = StringIOWrapper('test_suite_ut.function', data)
1070 tests = [(name, function_name, dependencies, args)
1071 for name, function_name, dependencies, args in
1072 parse_test_data(stream)]
1073 test1, test2 = tests
1074 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1075 self.assertEqual(test1[1], 'dhm_do_dhm')
1076 self.assertEqual(test1[2], ['YAHOO'])
1077 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001078
Azim Khanb31aa442018-07-03 11:57:54 +01001079 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1080 self.assertEqual(test2[1], 'dhm_do_dhm')
1081 self.assertEqual(test2[2], [])
1082 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1083 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001084
1085 def test_no_args(self):
1086 """
Azim Khanb31aa442018-07-03 11:57:54 +01001087 Test GeneratorInputError is raised when test function name and
1088 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001089 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001090 """
1091 data = """
1092Diffie-Hellman full exchange #1
1093depends_on:YAHOO
1094
1095
1096Diffie-Hellman full exchange #2
1097dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1098
1099"""
Azim Khanb31aa442018-07-03 11:57:54 +01001100 stream = StringIOWrapper('test_suite_ut.function', data)
1101 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001102 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001103 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001104 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001105 except GeneratorInputError as err:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001106 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001107 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001108
1109 def test_incomplete_data(self):
1110 """
Azim Khanb31aa442018-07-03 11:57:54 +01001111 Test GeneratorInputError is raised when test function name
1112 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001113 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001114 """
1115 data = """
1116Diffie-Hellman full exchange #1
1117depends_on:YAHOO
1118"""
Azim Khanb31aa442018-07-03 11:57:54 +01001119 stream = StringIOWrapper('test_suite_ut.function', data)
1120 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001121 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001122 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001123 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001124 except GeneratorInputError as err:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001125 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001126 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001127
1128
1129class GenDepCheck(TestCase):
1130 """
Azim Khanb31aa442018-07-03 11:57:54 +01001131 Test suite for gen_dep_check(). It is assumed this function is
1132 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001133 """
1134
1135 def test_gen_dep_check(self):
1136 """
1137 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001138 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001139 """
1140 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001141 case 5:
1142 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001143#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001144 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001145#else
Azim Khand61b8372017-07-10 11:54:01 +01001146 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001147#endif
Azim Khand61b8372017-07-10 11:54:01 +01001148 }
1149 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001150 out = gen_dep_check(5, 'YAHOO')
1151 self.assertEqual(out, expected)
1152
Azim Khanb31aa442018-07-03 11:57:54 +01001153 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001154 """
1155 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001156 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001157 """
1158 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001159 case 5:
1160 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001161#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001162 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001163#else
Azim Khand61b8372017-07-10 11:54:01 +01001164 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001165#endif
Azim Khand61b8372017-07-10 11:54:01 +01001166 }
1167 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001168 out = gen_dep_check(5, '!YAHOO')
1169 self.assertEqual(out, expected)
1170
1171 def test_empty_dependency(self):
1172 """
1173 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001174 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001175 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001176 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001177
1178 def test_negative_dep_id(self):
1179 """
1180 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001181 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001182 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001183 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001184
1185
1186class GenExpCheck(TestCase):
1187 """
Azim Khanb31aa442018-07-03 11:57:54 +01001188 Test suite for gen_expression_check(). It is assumed this function
1189 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001190 """
1191
1192 def test_gen_exp_check(self):
1193 """
1194 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001195 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001196 """
1197 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001198 case 5:
1199 {
1200 *out_value = YAHOO;
1201 }
1202 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001203 out = gen_expression_check(5, 'YAHOO')
1204 self.assertEqual(out, expected)
1205
1206 def test_invalid_expression(self):
1207 """
1208 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001209 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001210 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001211 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001212
1213 def test_negative_exp_id(self):
1214 """
1215 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001216 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001217 """
Azim Khanb31aa442018-07-03 11:57:54 +01001218 self.assertRaises(GeneratorInputError, gen_expression_check,
1219 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001220
1221
Azim Khanb31aa442018-07-03 11:57:54 +01001222class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001223 """
Azim Khanb31aa442018-07-03 11:57:54 +01001224 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001225 """
1226
Azim Khanb31aa442018-07-03 11:57:54 +01001227 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001228 """
Azim Khanb31aa442018-07-03 11:57:54 +01001229 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001230 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001231 """
Azim Khanb31aa442018-07-03 11:57:54 +01001232 stream = StringIOWrapper('test_suite_ut.data', '')
1233 unique_dependencies = []
1234 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001235 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001236 self.assertEqual(len(unique_dependencies), 0)
1237 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001238
1239 def test_unique_dep_ids(self):
1240 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001241
1242 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001243 """
Azim Khanb31aa442018-07-03 11:57:54 +01001244 stream = StringIOWrapper('test_suite_ut.data', '')
1245 unique_dependencies = []
1246 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1247 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001248 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001249 case 0:
1250 {
Azim Khan599cd242017-07-06 17:34:27 +01001251#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001252 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001253#else
Azim Khand61b8372017-07-10 11:54:01 +01001254 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001255#endif
Azim Khand61b8372017-07-10 11:54:01 +01001256 }
1257 break;
1258 case 1:
1259 {
Azim Khan599cd242017-07-06 17:34:27 +01001260#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001261 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001262#else
Azim Khand61b8372017-07-10 11:54:01 +01001263 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001264#endif
Azim Khand61b8372017-07-10 11:54:01 +01001265 }
1266 break;
1267 case 2:
1268 {
Azim Khan599cd242017-07-06 17:34:27 +01001269#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001270 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001271#else
Azim Khand61b8372017-07-10 11:54:01 +01001272 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001273#endif
Azim Khand61b8372017-07-10 11:54:01 +01001274 }
1275 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001276 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001277 self.assertEqual(len(unique_dependencies), 3)
1278 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001279
1280 def test_dep_id_repeat(self):
1281 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001282
1283 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001284 """
Azim Khanb31aa442018-07-03 11:57:54 +01001285 stream = StringIOWrapper('test_suite_ut.data', '')
1286 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001287 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001288 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1289 unique_dependencies)
1290 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1291 unique_dependencies)
1292 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1293 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001294 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001295 case 0:
1296 {
Azim Khan599cd242017-07-06 17:34:27 +01001297#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001298 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001299#else
Azim Khand61b8372017-07-10 11:54:01 +01001300 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001301#endif
Azim Khand61b8372017-07-10 11:54:01 +01001302 }
1303 break;
1304 case 1:
1305 {
Azim Khan599cd242017-07-06 17:34:27 +01001306#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001307 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001308#else
Azim Khand61b8372017-07-10 11:54:01 +01001309 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001310#endif
Azim Khand61b8372017-07-10 11:54:01 +01001311 }
1312 break;
1313 case 2:
1314 {
Azim Khan599cd242017-07-06 17:34:27 +01001315#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001316 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001317#else
Azim Khand61b8372017-07-10 11:54:01 +01001318 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001319#endif
Azim Khand61b8372017-07-10 11:54:01 +01001320 }
1321 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001322 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001323 self.assertEqual(len(unique_dependencies), 3)
1324 self.assertEqual(stream.getvalue(),
1325 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001326
1327
1328class WriteParams(TestCase):
1329 """
1330 Test Suite for testing write_parameters().
1331 """
1332
1333 def test_no_params(self):
1334 """
1335 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001336 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001337 """
Azim Khanb31aa442018-07-03 11:57:54 +01001338 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001339 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001340 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001341 self.assertEqual(len(unique_expressions), 0)
1342 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001343 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001344
1345 def test_no_exp_param(self):
1346 """
1347 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001348 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001349 """
Azim Khanb31aa442018-07-03 11:57:54 +01001350 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001351 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001352 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1353 '0'],
1354 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001355 unique_expressions)
1356 self.assertEqual(len(unique_expressions), 0)
1357 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001358 self.assertEqual(stream.getvalue(),
1359 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001360
1361 def test_hex_format_int_param(self):
1362 """
1363 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001364 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001365 """
Azim Khanb31aa442018-07-03 11:57:54 +01001366 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001367 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001368 expression_code = write_parameters(stream,
1369 ['"Yahoo"', '"abcdef00"', '0xAA'],
1370 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001371 unique_expressions)
1372 self.assertEqual(len(unique_expressions), 0)
1373 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001374 self.assertEqual(stream.getvalue(),
1375 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001376
1377 def test_with_exp_param(self):
1378 """
1379 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001380 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001381 """
Azim Khanb31aa442018-07-03 11:57:54 +01001382 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001383 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001384 expression_code = write_parameters(stream,
1385 ['"Yahoo"', '"abcdef00"', '0',
1386 'MACRO1', 'MACRO2', 'MACRO3'],
1387 ['char*', 'hex', 'int',
1388 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001389 unique_expressions)
1390 self.assertEqual(len(unique_expressions), 3)
1391 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1392 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001393 case 0:
1394 {
1395 *out_value = MACRO1;
1396 }
1397 break;
1398 case 1:
1399 {
1400 *out_value = MACRO2;
1401 }
1402 break;
1403 case 2:
1404 {
1405 *out_value = MACRO3;
1406 }
1407 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001408 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001409 self.assertEqual(stream.getvalue(),
1410 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1411 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001412
Azim Khanb31aa442018-07-03 11:57:54 +01001413 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001414 """
1415 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001416 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001417 """
Azim Khanb31aa442018-07-03 11:57:54 +01001418 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001419 unique_expressions = []
1420 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001421 expression_code += write_parameters(stream,
1422 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1423 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001424 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001425 expression_code += write_parameters(stream,
1426 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1427 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001428 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001429 expression_code += write_parameters(stream,
1430 ['0', 'MACRO3', 'MACRO1'],
1431 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001432 unique_expressions)
1433 self.assertEqual(len(unique_expressions), 3)
1434 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1435 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001436 case 0:
1437 {
1438 *out_value = MACRO1;
1439 }
1440 break;
1441 case 1:
1442 {
1443 *out_value = MACRO2;
1444 }
1445 break;
1446 case 2:
1447 {
1448 *out_value = MACRO3;
1449 }
1450 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001451 self.assertEqual(expression_code, expected_expression_code)
1452 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1453:hex:"abcdef00":exp:1:exp:2
1454:int:0:exp:2:exp:0
1455'''
Azim Khanb31aa442018-07-03 11:57:54 +01001456 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001457
1458
Azim Khanb31aa442018-07-03 11:57:54 +01001459class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001460 """
Azim Khanb31aa442018-07-03 11:57:54 +01001461 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001462 """
Azim Khanb31aa442018-07-03 11:57:54 +01001463 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001464 """
Azim Khanb31aa442018-07-03 11:57:54 +01001465 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001466
1467 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001468 """
Azim Khanb31aa442018-07-03 11:57:54 +01001469 dep_check_code, expression_code = \
1470 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001471 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1472 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1473
Azim Khanb31aa442018-07-03 11:57:54 +01001474 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001475 """
Azim Khanb31aa442018-07-03 11:57:54 +01001476 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001477
1478 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001479 """
Azim Khanb31aa442018-07-03 11:57:54 +01001480 dep_check_code, expression_code = \
1481 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1482 'EXPRESSION_CODE')
1483 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001484#if defined(SUITE_DEP)
1485DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001486#endif
1487'''
1488 expected_expression_code = '''
1489#if defined(SUITE_DEP)
1490EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001491#endif
1492'''
Azim Khanb31aa442018-07-03 11:57:54 +01001493 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001494 self.assertEqual(expression_code, expected_expression_code)
1495
1496 def test_no_dep_no_exp(self):
1497 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001498 Test when there are no dependency and expression code.
1499 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001500 """
Azim Khanb31aa442018-07-03 11:57:54 +01001501 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001502 self.assertEqual(dep_check_code, '')
1503 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001504
1505
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001506class GenFromTestData(TestCase):
1507 """
1508 Test suite for gen_from_test_data()
1509 """
1510
Azim Khanb31aa442018-07-03 11:57:54 +01001511 @staticmethod
1512 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001513 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001514 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001515 def test_intermediate_data_file(func_mock1,
1516 write_parameters_mock,
1517 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001518 """
1519 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001520 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001521 """
1522 data = '''
1523My test
1524depends_on:DEP1
1525func1:0
1526'''
1527 data_f = StringIOWrapper('test_suite_ut.data', data)
1528 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1529 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001530 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001531 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001532 write_dependencies_mock.side_effect = write_dependencies
1533 func_mock1.side_effect = gen_suite_dep_checks
1534 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1535 write_dependencies_mock.assert_called_with(out_data_f,
1536 ['DEP1'], ['DEP1'])
1537 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1538 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001539 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001540 case 0:
1541 {
Azim Khan599cd242017-07-06 17:34:27 +01001542#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001543 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001544#else
Azim Khand61b8372017-07-10 11:54:01 +01001545 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001546#endif
Azim Khand61b8372017-07-10 11:54:01 +01001547 }
1548 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001549 func_mock1.assert_called_with(
1550 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001551
1552 def test_function_not_found(self):
1553 """
1554 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001555 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001556 """
1557 data = '''
1558My test
1559depends_on:DEP1
1560func1:0
1561'''
1562 data_f = StringIOWrapper('test_suite_ut.data', data)
1563 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1564 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001565 suite_dependencies = []
1566 self.assertRaises(GeneratorInputError, gen_from_test_data,
1567 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001568
1569 def test_different_func_args(self):
1570 """
Azim Khanb31aa442018-07-03 11:57:54 +01001571 Test that AssertError is raised when no. of parameters and
1572 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001573 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001574 """
1575 data = '''
1576My test
1577depends_on:DEP1
1578func1:0
1579'''
1580 data_f = StringIOWrapper('test_suite_ut.data', data)
1581 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001582 func_info = {'test_func2': (1, ('int', 'hex'))}
1583 suite_dependencies = []
1584 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1585 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001586
1587 def test_output(self):
1588 """
1589 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001590 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001591 """
1592 data = '''
1593My test 1
1594depends_on:DEP1
1595func1:0:0xfa:MACRO1:MACRO2
1596
1597My test 2
1598depends_on:DEP1:DEP2
1599func2:"yahoo":88:MACRO1
1600'''
1601 data_f = StringIOWrapper('test_suite_ut.data', data)
1602 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001603 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1604 'test_func2': (1, ('char*', 'int', 'int'))}
1605 suite_dependencies = []
1606 dep_check_code, expression_code = \
1607 gen_from_test_data(data_f, out_data_f, func_info,
1608 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001609 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001610 case 0:
1611 {
Azim Khan599cd242017-07-06 17:34:27 +01001612#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001613 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001614#else
Azim Khand61b8372017-07-10 11:54:01 +01001615 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001616#endif
Azim Khand61b8372017-07-10 11:54:01 +01001617 }
1618 break;
1619 case 1:
1620 {
Azim Khan599cd242017-07-06 17:34:27 +01001621#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001622 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001623#else
Azim Khand61b8372017-07-10 11:54:01 +01001624 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001625#endif
Azim Khand61b8372017-07-10 11:54:01 +01001626 }
1627 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001628 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001629depends_on:0
16300:int:0:int:0xfa:exp:0:exp:1
1631
1632My test 2
1633depends_on:0:1
16341:char*:"yahoo":int:88:exp:0
1635
1636'''
1637 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001638 case 0:
1639 {
1640 *out_value = MACRO1;
1641 }
1642 break;
1643 case 1:
1644 {
1645 *out_value = MACRO2;
1646 }
1647 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001648 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001649 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001650 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001651
1652
Azim Khanb31aa442018-07-03 11:57:54 +01001653if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001654 unittest_main()