blob: e64d26dac3a41266c37ce66a29812d6676c8a9f3 [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útia2947ac2020-08-19 16:37:36 +02004# Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6#
7# This file is provided under the Apache License 2.0, or the
8# GNU General Public License v2.0 or later.
9#
10# **********
11# Apache License 2.0:
Azim Khanf0e42fb2017-08-02 14:47:13 +010012#
13# Licensed under the Apache License, Version 2.0 (the "License"); you may
14# not use this file except in compliance with the License.
15# You may obtain a copy of the License at
16#
17# http://www.apache.org/licenses/LICENSE-2.0
18#
19# Unless required by applicable law or agreed to in writing, software
20# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22# See the License for the specific language governing permissions and
23# limitations under the License.
24#
Bence Szépkútif744bd72020-06-05 13:02:18 +020025# **********
26#
27# **********
28# GNU General Public License v2.0 or later:
29#
30# This program is free software; you can redistribute it and/or modify
31# it under the terms of the GNU General Public License as published by
32# the Free Software Foundation; either version 2 of the License, or
33# (at your option) any later version.
34#
35# This program is distributed in the hope that it will be useful,
36# but WITHOUT ANY WARRANTY; without even the implied warranty of
37# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38# GNU General Public License for more details.
39#
40# You should have received a copy of the GNU General Public License along
41# with this program; if not, write to the Free Software Foundation, Inc.,
42# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43#
44# **********
Azim Khan4b543232017-06-30 09:35:21 +010045
46"""
Mohammad Azim Khan78befd92018-03-06 11:49:41 +000047Unit tests for generate_test_code.py
Azim Khan4b543232017-06-30 09:35:21 +010048"""
49
Gilles Peskineafd19dd2019-02-25 21:39:42 +010050# pylint: disable=wrong-import-order
Mohammad Azim Khan539aa062018-07-06 00:29:50 +010051try:
52 # Python 2
53 from StringIO import StringIO
54except ImportError:
55 # Python 3
56 from io import StringIO
Azim Khanb31aa442018-07-03 11:57:54 +010057from unittest import TestCase, main as unittest_main
Mohammad Azim Khan539aa062018-07-06 00:29:50 +010058try:
59 # Python 2
60 from mock import patch
61except ImportError:
62 # Python 3
63 from unittest.mock import patch
Gilles Peskineafd19dd2019-02-25 21:39:42 +010064# pylint: enable=wrong-import-order
Azim Khanb31aa442018-07-03 11:57:54 +010065from generate_test_code import gen_dependencies, gen_dependencies_one_line
66from generate_test_code import gen_function_wrapper, gen_dispatch
67from generate_test_code import parse_until_pattern, GeneratorInputError
68from generate_test_code import parse_suite_dependencies
69from generate_test_code import parse_function_dependencies
Azim Khanfcdf6852018-07-05 17:31:46 +010070from generate_test_code import parse_function_arguments, parse_function_code
Azim Khanb31aa442018-07-03 11:57:54 +010071from generate_test_code import parse_functions, END_HEADER_REGEX
72from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
73from generate_test_code import parse_test_data, gen_dep_check
74from generate_test_code import gen_expression_check, write_dependencies
75from generate_test_code import write_parameters, gen_suite_dep_checks
76from generate_test_code import gen_from_test_data
77
78
Azim Khan4b543232017-06-30 09:35:21 +010079class GenDep(TestCase):
80 """
81 Test suite for function gen_dep()
82 """
83
Azim Khanb31aa442018-07-03 11:57:54 +010084 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010085 """
Azim Khanb31aa442018-07-03 11:57:54 +010086 Test that gen_dep() correctly creates dependencies for given
87 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010088 :return:
Azim Khan4b543232017-06-30 09:35:21 +010089 """
Azim Khanb31aa442018-07-03 11:57:54 +010090 dependencies = ['DEP1', 'DEP2']
91 dep_start, dep_end = gen_dependencies(dependencies)
92 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010093 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010094 self.assertEqual(preprocessor1, '#if defined(DEP1)',
95 'Preprocessor generated incorrectly')
96 self.assertEqual(preprocessor2, '#if defined(DEP2)',
97 'Preprocessor generated incorrectly')
98 self.assertEqual(endif1, '#endif /* DEP2 */',
99 'Preprocessor generated incorrectly')
100 self.assertEqual(endif2, '#endif /* DEP1 */',
101 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100102
Azim Khanb31aa442018-07-03 11:57:54 +0100103 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100104 """
Azim Khanb31aa442018-07-03 11:57:54 +0100105 Test that gen_dep() correctly creates dependencies for given
106 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100107 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100108 """
Azim Khanb31aa442018-07-03 11:57:54 +0100109 dependencies = ['!DEP1', '!DEP2']
110 dep_start, dep_end = gen_dependencies(dependencies)
111 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +0100112 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +0100113 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
114 'Preprocessor generated incorrectly')
115 self.assertEqual(preprocessor2, '#if !defined(DEP2)',
116 'Preprocessor generated incorrectly')
117 self.assertEqual(endif1, '#endif /* !DEP2 */',
118 'Preprocessor generated incorrectly')
119 self.assertEqual(endif2, '#endif /* !DEP1 */',
120 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100121
Azim Khanb31aa442018-07-03 11:57:54 +0100122 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100123 """
Azim Khanb31aa442018-07-03 11:57:54 +0100124 Test that gen_dep() correctly creates dependencies for given
125 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100126 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100127 """
Azim Khanb31aa442018-07-03 11:57:54 +0100128 dependencies = ['!DEP1', 'DEP2']
129 dep_start, dep_end = gen_dependencies(dependencies)
130 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +0100131 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +0100132 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
133 'Preprocessor generated incorrectly')
134 self.assertEqual(preprocessor2, '#if defined(DEP2)',
135 'Preprocessor generated incorrectly')
136 self.assertEqual(endif1, '#endif /* DEP2 */',
137 'Preprocessor generated incorrectly')
138 self.assertEqual(endif2, '#endif /* !DEP1 */',
139 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100140
Azim Khanb31aa442018-07-03 11:57:54 +0100141 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100142 """
Azim Khanb31aa442018-07-03 11:57:54 +0100143 Test that gen_dep() correctly creates dependencies for given
144 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100145 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100146 """
Azim Khanb31aa442018-07-03 11:57:54 +0100147 dependencies = []
148 dep_start, dep_end = gen_dependencies(dependencies)
149 self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
150 self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100151
Azim Khanb31aa442018-07-03 11:57:54 +0100152 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100153 """
Azim Khanb31aa442018-07-03 11:57:54 +0100154 Test that gen_dep() correctly creates dependencies for given
155 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100156 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100157 """
Azim Khanb31aa442018-07-03 11:57:54 +0100158 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100159 count = 10
160 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100161 dependencies.append('DEP%d' % i)
162 dep_start, dep_end = gen_dependencies(dependencies)
163 self.assertEqual(len(dep_start.splitlines()), count,
164 'Preprocessor generated incorrectly')
165 self.assertEqual(len(dep_end.splitlines()), count,
166 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100167
168
169class GenDepOneLine(TestCase):
170 """
Azim Khanb31aa442018-07-03 11:57:54 +0100171 Test Suite for testing gen_dependencies_one_line()
Azim Khan4b543232017-06-30 09:35:21 +0100172 """
173
Azim Khanb31aa442018-07-03 11:57:54 +0100174 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100175 """
Azim Khanb31aa442018-07-03 11:57:54 +0100176 Test that gen_dep() correctly creates dependencies for given
177 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100178 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100179 """
Azim Khanb31aa442018-07-03 11:57:54 +0100180 dependencies = ['DEP1', 'DEP2']
181 dep_str = gen_dependencies_one_line(dependencies)
182 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
183 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100184
Azim Khanb31aa442018-07-03 11:57:54 +0100185 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100186 """
Azim Khanb31aa442018-07-03 11:57:54 +0100187 Test that gen_dep() correctly creates dependencies for given
188 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100189 :return:
190 """
Azim Khanb31aa442018-07-03 11:57:54 +0100191 dependencies = ['!DEP1', '!DEP2']
192 dep_str = gen_dependencies_one_line(dependencies)
193 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
194 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100195
Azim Khanb31aa442018-07-03 11:57:54 +0100196 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100197 """
Azim Khanb31aa442018-07-03 11:57:54 +0100198 Test that gen_dep() correctly creates dependencies for given
199 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100200 :return:
201 """
Azim Khanb31aa442018-07-03 11:57:54 +0100202 dependencies = ['!DEP1', 'DEP2']
203 dep_str = gen_dependencies_one_line(dependencies)
204 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
205 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100206
Azim Khanb31aa442018-07-03 11:57:54 +0100207 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100208 """
Azim Khanb31aa442018-07-03 11:57:54 +0100209 Test that gen_dep() correctly creates dependencies for given
210 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100211 :return:
212 """
Azim Khanb31aa442018-07-03 11:57:54 +0100213 dependencies = []
214 dep_str = gen_dependencies_one_line(dependencies)
215 self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100216
Azim Khanb31aa442018-07-03 11:57:54 +0100217 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100218 """
Azim Khanb31aa442018-07-03 11:57:54 +0100219 Test that gen_dep() correctly creates dependencies for given
220 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100221 :return:
222 """
Azim Khanb31aa442018-07-03 11:57:54 +0100223 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100224 count = 10
225 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100226 dependencies.append('DEP%d' % i)
227 dep_str = gen_dependencies_one_line(dependencies)
228 expected = '#if ' + ' && '.join(['defined(%s)' %
229 x for x in dependencies])
230 self.assertEqual(dep_str, expected,
231 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100232
233
234class GenFunctionWrapper(TestCase):
235 """
236 Test Suite for testing gen_function_wrapper()
237 """
238
239 def test_params_unpack(self):
240 """
241 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100242
243 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100244 """
245 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
246 expected = '''
247void test_a_wrapper( void ** params )
248{
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100249
Azim Khan4b543232017-06-30 09:35:21 +0100250 test_a( a, b, c, d );
251}
252'''
253 self.assertEqual(code, expected)
254
255 def test_local(self):
256 """
257 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100258
259 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100260 """
Azim Khanb31aa442018-07-03 11:57:54 +0100261 code = gen_function_wrapper('test_a',
262 'int x = 1;', ('x', 'b', 'c', 'd'))
Azim Khan4b543232017-06-30 09:35:21 +0100263 expected = '''
264void test_a_wrapper( void ** params )
265{
Azim Khan4b543232017-06-30 09:35:21 +0100266int x = 1;
267 test_a( x, b, c, d );
268}
269'''
270 self.assertEqual(code, expected)
271
272 def test_empty_params(self):
273 """
274 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100275
276 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100277 """
278 code = gen_function_wrapper('test_a', '', ())
279 expected = '''
280void test_a_wrapper( void ** params )
281{
282 (void)params;
283
284 test_a( );
285}
286'''
287 self.assertEqual(code, expected)
288
289
290class GenDispatch(TestCase):
291 """
292 Test suite for testing gen_dispatch()
293 """
294
295 def test_dispatch(self):
296 """
297 Test that dispatch table entry is generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100298 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100299 """
300 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
301 expected = '''
302#if defined(DEP1) && defined(DEP2)
303 test_a_wrapper,
304#else
305 NULL,
306#endif
307'''
308 self.assertEqual(code, expected)
309
Azim Khanb31aa442018-07-03 11:57:54 +0100310 def test_empty_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100311 """
312 Test empty dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100313 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100314 """
315 code = gen_dispatch('test_a', [])
316 expected = '''
317 test_a_wrapper,
318'''
319 self.assertEqual(code, expected)
320
321
Gilles Peskine5d1dfd42020-03-24 18:25:17 +0100322class StringIOWrapper(StringIO):
Azim Khan4b543232017-06-30 09:35:21 +0100323 """
324 file like class to mock file object in tests.
325 """
Azim Khan4084ec72018-07-05 14:20:08 +0100326 def __init__(self, file_name, data, line_no=0):
Azim Khan4b543232017-06-30 09:35:21 +0100327 """
328 Init file handle.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100329
330 :param file_name:
Azim Khan4b543232017-06-30 09:35:21 +0100331 :param data:
332 :param line_no:
333 """
334 super(StringIOWrapper, self).__init__(data)
335 self.line_no = line_no
336 self.name = file_name
337
338 def next(self):
339 """
Azim Khanb31aa442018-07-03 11:57:54 +0100340 Iterator method. This method overrides base class's
341 next method and extends the next method to count the line
342 numbers as each line is read.
Azim Khan4b543232017-06-30 09:35:21 +0100343
Azim Khanb31aa442018-07-03 11:57:54 +0100344 :return: Line read from file.
345 """
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100346 parent = super(StringIOWrapper, self)
347 if getattr(parent, 'next', None):
348 # Python 2
349 line = parent.next()
350 else:
351 # Python 3
352 line = parent.__next__()
Azim Khan4084ec72018-07-05 14:20:08 +0100353 return line
Azim Khanb31aa442018-07-03 11:57:54 +0100354
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100355 # Python 3
Azim Khanb31aa442018-07-03 11:57:54 +0100356 __next__ = next
357
358 def readline(self, length=0):
Azim Khan4b543232017-06-30 09:35:21 +0100359 """
360 Wrap the base class readline.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100361
Azim Khanb31aa442018-07-03 11:57:54 +0100362 :param length:
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100363 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100364 """
Gilles Peskineafd19dd2019-02-25 21:39:42 +0100365 # pylint: disable=unused-argument
Azim Khan4b543232017-06-30 09:35:21 +0100366 line = super(StringIOWrapper, self).readline()
Azim Khan4084ec72018-07-05 14:20:08 +0100367 if line is not None:
Azim Khan4b543232017-06-30 09:35:21 +0100368 self.line_no += 1
369 return line
370
371
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000372class ParseUntilPattern(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100373 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000374 Test Suite for testing parse_until_pattern().
Azim Khan4b543232017-06-30 09:35:21 +0100375 """
376
377 def test_suite_headers(self):
378 """
379 Test that suite headers are parsed correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100380
381 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100382 """
383 data = '''#include "mbedtls/ecp.h"
384
385#define ECP_PF_UNKNOWN -1
386/* END_HEADER */
387'''
388 expected = '''#line 1 "test_suite_ut.function"
389#include "mbedtls/ecp.h"
390
391#define ECP_PF_UNKNOWN -1
392'''
Azim Khanb31aa442018-07-03 11:57:54 +0100393 stream = StringIOWrapper('test_suite_ut.function', data, line_no=0)
394 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100395 self.assertEqual(headers, expected)
396
397 def test_line_no(self):
398 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100399 Test that #line is set to correct line no. in source .function file.
400
401 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100402 """
403 data = '''#include "mbedtls/ecp.h"
404
405#define ECP_PF_UNKNOWN -1
406/* END_HEADER */
407'''
408 offset_line_no = 5
409 expected = '''#line %d "test_suite_ut.function"
410#include "mbedtls/ecp.h"
411
412#define ECP_PF_UNKNOWN -1
413''' % (offset_line_no + 1)
Azim Khanb31aa442018-07-03 11:57:54 +0100414 stream = StringIOWrapper('test_suite_ut.function', data,
415 offset_line_no)
416 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100417 self.assertEqual(headers, expected)
418
419 def test_no_end_header_comment(self):
420 """
Azim Khanb31aa442018-07-03 11:57:54 +0100421 Test that InvalidFileFormat is raised when end header comment is
422 missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100423 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100424 """
425 data = '''#include "mbedtls/ecp.h"
426
427#define ECP_PF_UNKNOWN -1
428
429'''
Azim Khanb31aa442018-07-03 11:57:54 +0100430 stream = StringIOWrapper('test_suite_ut.function', data)
431 self.assertRaises(GeneratorInputError, parse_until_pattern, stream,
432 END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100433
434
Azim Khanb31aa442018-07-03 11:57:54 +0100435class ParseSuiteDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100436 """
Azim Khanb31aa442018-07-03 11:57:54 +0100437 Test Suite for testing parse_suite_dependencies().
Azim Khan4b543232017-06-30 09:35:21 +0100438 """
439
Azim Khanb31aa442018-07-03 11:57:54 +0100440 def test_suite_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100441 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100442
443 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100444 """
445 data = '''
446 * depends_on:MBEDTLS_ECP_C
447 * END_DEPENDENCIES
448 */
449'''
450 expected = ['MBEDTLS_ECP_C']
Azim Khanb31aa442018-07-03 11:57:54 +0100451 stream = StringIOWrapper('test_suite_ut.function', data)
452 dependencies = parse_suite_dependencies(stream)
453 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100454
455 def test_no_end_dep_comment(self):
456 """
457 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100458 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100459 """
460 data = '''
461* depends_on:MBEDTLS_ECP_C
462'''
Azim Khanb31aa442018-07-03 11:57:54 +0100463 stream = StringIOWrapper('test_suite_ut.function', data)
464 self.assertRaises(GeneratorInputError, parse_suite_dependencies,
465 stream)
Azim Khan4b543232017-06-30 09:35:21 +0100466
Azim Khanb31aa442018-07-03 11:57:54 +0100467 def test_dependencies_split(self):
Azim Khan4b543232017-06-30 09:35:21 +0100468 """
469 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100470 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100471 """
472 data = '''
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100473 * depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
Azim Khan4b543232017-06-30 09:35:21 +0100474 * END_DEPENDENCIES
475 */
476'''
477 expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
Azim Khanb31aa442018-07-03 11:57:54 +0100478 stream = StringIOWrapper('test_suite_ut.function', data)
479 dependencies = parse_suite_dependencies(stream)
480 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100481
482
Azim Khanb31aa442018-07-03 11:57:54 +0100483class ParseFuncDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100484 """
Azim Khanb31aa442018-07-03 11:57:54 +0100485 Test Suite for testing parse_function_dependencies()
Azim Khan4b543232017-06-30 09:35:21 +0100486 """
487
Azim Khanb31aa442018-07-03 11:57:54 +0100488 def test_function_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100489 """
Azim Khanb31aa442018-07-03 11:57:54 +0100490 Test that parse_function_dependencies() correctly parses function
491 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100492 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100493 """
Azim Khanb31aa442018-07-03 11:57:54 +0100494 line = '/* BEGIN_CASE ' \
495 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
Azim Khan4b543232017-06-30 09:35:21 +0100496 expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
Azim Khanb31aa442018-07-03 11:57:54 +0100497 dependencies = parse_function_dependencies(line)
498 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100499
Azim Khanb31aa442018-07-03 11:57:54 +0100500 def test_no_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100501 """
Azim Khanb31aa442018-07-03 11:57:54 +0100502 Test that parse_function_dependencies() correctly parses function
503 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100504 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100505 """
506 line = '/* BEGIN_CASE */'
Azim Khanb31aa442018-07-03 11:57:54 +0100507 dependencies = parse_function_dependencies(line)
508 self.assertEqual(dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100509
Azim Khanb31aa442018-07-03 11:57:54 +0100510 def test_tolerance(self):
Azim Khan4b543232017-06-30 09:35:21 +0100511 """
Azim Khanb31aa442018-07-03 11:57:54 +0100512 Test that parse_function_dependencies() correctly parses function
513 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100514 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100515 """
516 line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
Azim Khanb31aa442018-07-03 11:57:54 +0100517 dependencies = parse_function_dependencies(line)
518 self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
Azim Khan4b543232017-06-30 09:35:21 +0100519
520
521class ParseFuncSignature(TestCase):
522 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100523 Test Suite for parse_function_arguments().
Azim Khan4b543232017-06-30 09:35:21 +0100524 """
525
526 def test_int_and_char_params(self):
527 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100528 Test int and char parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100529 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100530 """
531 line = 'void entropy_threshold( char * a, int b, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100532 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100533 self.assertEqual(args, ['char*', 'int', 'int'])
534 self.assertEqual(local, '')
Azim Khanb31aa442018-07-03 11:57:54 +0100535 self.assertEqual(arg_dispatch, ['(char *) params[0]',
536 '*( (int *) params[1] )',
537 '*( (int *) params[2] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100538
539 def test_hex_params(self):
540 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100541 Test hex parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100542 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100543 """
Azim Khan5fcca462018-06-29 11:05:32 +0100544 line = 'void entropy_threshold( char * a, data_t * h, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100545 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100546 self.assertEqual(args, ['char*', 'hex', 'int'])
Azim Khanb31aa442018-07-03 11:57:54 +0100547 self.assertEqual(local,
Azim Khan4084ec72018-07-05 14:20:08 +0100548 ' data_t data1 = {(uint8_t *) params[1], '
Azim Khanb31aa442018-07-03 11:57:54 +0100549 '*( (uint32_t *) params[2] )};\n')
550 self.assertEqual(arg_dispatch, ['(char *) params[0]',
Azim Khan4084ec72018-07-05 14:20:08 +0100551 '&data1',
Azim Khanb31aa442018-07-03 11:57:54 +0100552 '*( (int *) params[3] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100553
Azim Khan4b543232017-06-30 09:35:21 +0100554 def test_unsupported_arg(self):
555 """
Azim Khan5fcca462018-06-29 11:05:32 +0100556 Test unsupported arguments (not among int, char * and data_t)
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100557 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100558 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100559 line = 'void entropy_threshold( char * a, data_t * h, char result )'
560 self.assertRaises(ValueError, parse_function_arguments, line)
Azim Khan4b543232017-06-30 09:35:21 +0100561
562 def test_no_params(self):
563 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100564 Test no parameters.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100565 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100566 """
567 line = 'void entropy_threshold()'
Azim Khanfcdf6852018-07-05 17:31:46 +0100568 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100569 self.assertEqual(args, [])
570 self.assertEqual(local, '')
571 self.assertEqual(arg_dispatch, [])
572
573
574class ParseFunctionCode(TestCase):
575 """
576 Test suite for testing parse_function_code()
577 """
578
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100579 def assert_raises_regex(self, exp, regex, func, *args):
580 """
581 Python 2 & 3 portable wrapper of assertRaisesRegex(p)? function.
582
583 :param exp: Exception type expected to be raised by cb.
584 :param regex: Expected exception message
585 :param func: callable object under test
586 :param args: variable positional arguments
587 """
588 parent = super(ParseFunctionCode, self)
589
590 # Pylint does not appreciate that the super method called
591 # conditionally can be available in other Python version
592 # then that of Pylint.
593 # Workaround is to call the method via getattr.
594 # Pylint ignores that the method got via getattr is
595 # conditionally executed. Method has to be a callable.
596 # Hence, using a dummy callable for getattr default.
597 dummy = lambda *x: None
598 # First Python 3 assertRaisesRegex is checked, since Python 2
599 # assertRaisesRegexp is also available in Python 3 but is
600 # marked deprecated.
601 for name in ('assertRaisesRegex', 'assertRaisesRegexp'):
602 method = getattr(parent, name, dummy)
603 if method is not dummy:
604 method(exp, regex, func, *args)
605 break
606 else:
607 raise AttributeError(" 'ParseFunctionCode' object has no attribute"
608 " 'assertRaisesRegex' or 'assertRaisesRegexp'"
609 )
610
Azim Khan4b543232017-06-30 09:35:21 +0100611 def test_no_function(self):
612 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100613 Test no test function found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100614 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100615 """
616 data = '''
617No
618test
619function
620'''
Azim Khanb31aa442018-07-03 11:57:54 +0100621 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100622 err_msg = 'file: test_suite_ut.function - Test functions not found!'
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100623 self.assert_raises_regex(GeneratorInputError, err_msg,
624 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100625
626 def test_no_end_case_comment(self):
627 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100628 Test missing end case.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100629 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100630 """
631 data = '''
632void test_func()
633{
634}
635'''
Azim Khanb31aa442018-07-03 11:57:54 +0100636 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100637 err_msg = r'file: test_suite_ut.function - '\
638 'end case pattern .*? not found!'
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100639 self.assert_raises_regex(GeneratorInputError, err_msg,
640 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100641
Azim Khanfcdf6852018-07-05 17:31:46 +0100642 @patch("generate_test_code.parse_function_arguments")
Azim Khanb31aa442018-07-03 11:57:54 +0100643 def test_function_called(self,
Azim Khanfcdf6852018-07-05 17:31:46 +0100644 parse_function_arguments_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100645 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100646 Test parse_function_code()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100647 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100648 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100649 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100650 data = '''
651void test_func()
652{
653}
654'''
Azim Khanb31aa442018-07-03 11:57:54 +0100655 stream = StringIOWrapper('test_suite_ut.function', data)
656 self.assertRaises(GeneratorInputError, parse_function_code,
657 stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100658 self.assertTrue(parse_function_arguments_mock.called)
659 parse_function_arguments_mock.assert_called_with('void test_func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100660
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000661 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100662 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000663 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100664 @patch("generate_test_code.parse_function_arguments")
665 def test_return(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100666 gen_function_wrapper_mock,
667 gen_dependencies_mock,
668 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100669 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100670 Test generated code.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100671 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100672 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100673 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100674 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100675 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100676 gen_dispatch_mock.side_effect = gen_dispatch
677 data = '''
678void func()
679{
680 ba ba black sheep
681 have you any wool
682}
683/* END_CASE */
684'''
Azim Khanb31aa442018-07-03 11:57:54 +0100685 stream = StringIOWrapper('test_suite_ut.function', data)
686 name, arg, code, dispatch_code = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100687
Azim Khanfcdf6852018-07-05 17:31:46 +0100688 self.assertTrue(parse_function_arguments_mock.called)
689 parse_function_arguments_mock.assert_called_with('void func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100690 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
691 self.assertEqual(name, 'test_func')
692 self.assertEqual(arg, [])
Azim Khan4084ec72018-07-05 14:20:08 +0100693 expected = '''#line 1 "test_suite_ut.function"
694
Azim Khan4b543232017-06-30 09:35:21 +0100695void test_func()
696{
697 ba ba black sheep
698 have you any wool
699exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100700 ;
Azim Khan4b543232017-06-30 09:35:21 +0100701}
702'''
703 self.assertEqual(code, expected)
704 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
705
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000706 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100707 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000708 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100709 @patch("generate_test_code.parse_function_arguments")
710 def test_with_exit_label(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100711 gen_function_wrapper_mock,
712 gen_dependencies_mock,
713 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100714 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100715 Test when exit label is present.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100716 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100717 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100718 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100719 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100720 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100721 gen_dispatch_mock.side_effect = gen_dispatch
722 data = '''
723void func()
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'''
Azim Khanb31aa442018-07-03 11:57:54 +0100733 stream = StringIOWrapper('test_suite_ut.function', data)
734 _, _, code, _ = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100735
Azim Khan4084ec72018-07-05 14:20:08 +0100736 expected = '''#line 1 "test_suite_ut.function"
737
Azim Khan4b543232017-06-30 09:35:21 +0100738void test_func()
739{
740 ba ba black sheep
741 have you any wool
742exit:
743 yes sir yes sir
744 3 bags full
745}
746'''
747 self.assertEqual(code, expected)
748
Azim Khanfcdf6852018-07-05 17:31:46 +0100749 def test_non_void_function(self):
750 """
751 Test invalid signature (non void).
752 :return:
753 """
754 data = 'int entropy_threshold( char * a, data_t * h, int result )'
755 err_msg = 'file: test_suite_ut.function - Test functions not found!'
756 stream = StringIOWrapper('test_suite_ut.function', data)
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100757 self.assert_raises_regex(GeneratorInputError, err_msg,
758 parse_function_code, stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100759
760 @patch("generate_test_code.gen_dispatch")
761 @patch("generate_test_code.gen_dependencies")
762 @patch("generate_test_code.gen_function_wrapper")
763 @patch("generate_test_code.parse_function_arguments")
764 def test_functio_name_on_newline(self, parse_function_arguments_mock,
765 gen_function_wrapper_mock,
766 gen_dependencies_mock,
767 gen_dispatch_mock):
768 """
769 Test when exit label is present.
770 :return:
771 """
772 parse_function_arguments_mock.return_value = ([], '', [])
773 gen_function_wrapper_mock.return_value = ''
774 gen_dependencies_mock.side_effect = gen_dependencies
775 gen_dispatch_mock.side_effect = gen_dispatch
776 data = '''
777void
778
779
780func()
781{
782 ba ba black sheep
783 have you any wool
784exit:
785 yes sir yes sir
786 3 bags full
787}
788/* END_CASE */
789'''
790 stream = StringIOWrapper('test_suite_ut.function', data)
791 _, _, code, _ = parse_function_code(stream, [], [])
792
793 expected = '''#line 1 "test_suite_ut.function"
794
795void
796
797
798test_func()
799{
800 ba ba black sheep
801 have you any wool
802exit:
803 yes sir yes sir
804 3 bags full
805}
806'''
807 self.assertEqual(code, expected)
808
Azim Khan4b543232017-06-30 09:35:21 +0100809
810class ParseFunction(TestCase):
811 """
812 Test Suite for testing parse_functions()
813 """
814
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000815 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000816 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100817 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000818 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100819 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100820 """
Azim Khanb31aa442018-07-03 11:57:54 +0100821 def stop(*_unused):
822 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100823 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000824 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100825 data = '''/* BEGIN_HEADER */
826#include "mbedtls/ecp.h"
827
828#define ECP_PF_UNKNOWN -1
829/* END_HEADER */
830'''
Azim Khanb31aa442018-07-03 11:57:54 +0100831 stream = StringIOWrapper('test_suite_ut.function', data)
832 self.assertRaises(Exception, parse_functions, stream)
833 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100834 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000835
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000836 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000837 def test_begin_helper(self, parse_until_pattern_mock):
838 """
839 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100840 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000841 """
Azim Khanb31aa442018-07-03 11:57:54 +0100842 def stop(*_unused):
843 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000844 raise Exception
845 parse_until_pattern_mock.side_effect = stop
846 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100847void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000848{
Azim Khanb31aa442018-07-03 11:57:54 +0100849 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000850}
851/* END_SUITE_HELPERS */
852'''
Azim Khanb31aa442018-07-03 11:57:54 +0100853 stream = StringIOWrapper('test_suite_ut.function', data)
854 self.assertRaises(Exception, parse_functions, stream)
855 parse_until_pattern_mock.assert_called_with(stream,
856 END_SUITE_HELPERS_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100857 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100858
Azim Khanb31aa442018-07-03 11:57:54 +0100859 @patch("generate_test_code.parse_suite_dependencies")
860 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100861 """
Azim Khanb31aa442018-07-03 11:57:54 +0100862 Test that begin dep is checked and parse_suite_dependencies() is
863 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100864 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100865 """
Azim Khanb31aa442018-07-03 11:57:54 +0100866 def stop(*_unused):
867 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100868 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100869 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100870 data = '''/* BEGIN_DEPENDENCIES
871 * depends_on:MBEDTLS_ECP_C
872 * END_DEPENDENCIES
873 */
874'''
Azim Khanb31aa442018-07-03 11:57:54 +0100875 stream = StringIOWrapper('test_suite_ut.function', data)
876 self.assertRaises(Exception, parse_functions, stream)
877 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +0100878 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100879
Azim Khanb31aa442018-07-03 11:57:54 +0100880 @patch("generate_test_code.parse_function_dependencies")
881 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100882 """
Azim Khanb31aa442018-07-03 11:57:54 +0100883 Test that begin dep is checked and parse_function_dependencies() is
884 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100885 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100886 """
Azim Khanb31aa442018-07-03 11:57:54 +0100887 def stop(*_unused):
888 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100889 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100890 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100891
Azim Khanb31aa442018-07-03 11:57:54 +0100892 dependencies_str = '/* BEGIN_CASE ' \
893 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100894 data = '''%svoid test_func()
895{
896}
Azim Khanb31aa442018-07-03 11:57:54 +0100897''' % dependencies_str
898 stream = StringIOWrapper('test_suite_ut.function', data)
899 self.assertRaises(Exception, parse_functions, stream)
900 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +0100901 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100902
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000903 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +0100904 @patch("generate_test_code.parse_function_dependencies")
905 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +0100906 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000907 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100908 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100909 """
Azim Khanb31aa442018-07-03 11:57:54 +0100910 func_mock1.return_value = []
911 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +0100912{
913}
914'''
915 func_dispatch = '''
916 test_func_wrapper,
917'''
Azim Khanb31aa442018-07-03 11:57:54 +0100918 func_mock2.return_value = 'test_func', [],\
919 in_func_code, func_dispatch
920 dependencies_str = '/* BEGIN_CASE ' \
921 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100922 data = '''%svoid test_func()
923{
924}
Azim Khanb31aa442018-07-03 11:57:54 +0100925''' % dependencies_str
926 stream = StringIOWrapper('test_suite_ut.function', data)
927 suite_dependencies, dispatch_code, func_code, func_info = \
928 parse_functions(stream)
929 func_mock1.assert_called_with(dependencies_str)
930 func_mock2.assert_called_with(stream, [], [])
931 self.assertEqual(stream.line_no, 5)
932 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100933 expected_dispatch_code = '''/* Function Id: 0 */
934
935 test_func_wrapper,
936'''
937 self.assertEqual(dispatch_code, expected_dispatch_code)
938 self.assertEqual(func_code, in_func_code)
939 self.assertEqual(func_info, {'test_func': (0, [])})
940
941 def test_parsing(self):
942 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000943 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100944 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100945 """
946 data = '''/* BEGIN_HEADER */
947#include "mbedtls/ecp.h"
948
949#define ECP_PF_UNKNOWN -1
950/* END_HEADER */
951
952/* BEGIN_DEPENDENCIES
953 * depends_on:MBEDTLS_ECP_C
954 * END_DEPENDENCIES
955 */
956
957/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
958void func1()
959{
960}
961/* END_CASE */
962
963/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
964void func2()
965{
966}
967/* END_CASE */
968'''
Azim Khanb31aa442018-07-03 11:57:54 +0100969 stream = StringIOWrapper('test_suite_ut.function', data)
970 suite_dependencies, dispatch_code, func_code, func_info = \
971 parse_functions(stream)
972 self.assertEqual(stream.line_no, 23)
973 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +0100974
975 expected_dispatch_code = '''/* Function Id: 0 */
976
977#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
978 test_func1_wrapper,
979#else
980 NULL,
981#endif
982/* Function Id: 1 */
983
984#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
985 test_func2_wrapper,
986#else
987 NULL,
988#endif
989'''
990 self.assertEqual(dispatch_code, expected_dispatch_code)
991 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +0100992#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100993#include "mbedtls/ecp.h"
994
995#define ECP_PF_UNKNOWN -1
996#if defined(MBEDTLS_ENTROPY_NV_SEED)
997#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +0100998#line 13 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100999void test_func1()
1000{
1001exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001002 ;
Azim Khan4b543232017-06-30 09:35:21 +01001003}
1004
1005void test_func1_wrapper( void ** params )
1006{
1007 (void)params;
1008
1009 test_func1( );
1010}
1011#endif /* MBEDTLS_FS_IO */
1012#endif /* MBEDTLS_ENTROPY_NV_SEED */
1013#if defined(MBEDTLS_ENTROPY_NV_SEED)
1014#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001015#line 19 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001016void test_func2()
1017{
1018exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001019 ;
Azim Khan4b543232017-06-30 09:35:21 +01001020}
1021
1022void test_func2_wrapper( void ** params )
1023{
1024 (void)params;
1025
1026 test_func2( );
1027}
1028#endif /* MBEDTLS_FS_IO */
1029#endif /* MBEDTLS_ENTROPY_NV_SEED */
1030#endif /* MBEDTLS_ECP_C */
1031'''
1032 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001033 self.assertEqual(func_info, {'test_func1': (0, []),
1034 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +01001035
1036 def test_same_function_name(self):
1037 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001038 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001039 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001040 """
1041 data = '''/* BEGIN_HEADER */
1042#include "mbedtls/ecp.h"
1043
1044#define ECP_PF_UNKNOWN -1
1045/* END_HEADER */
1046
1047/* BEGIN_DEPENDENCIES
1048 * depends_on:MBEDTLS_ECP_C
1049 * END_DEPENDENCIES
1050 */
1051
1052/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1053void func()
1054{
1055}
1056/* END_CASE */
1057
1058/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1059void func()
1060{
1061}
1062/* END_CASE */
1063'''
Azim Khanb31aa442018-07-03 11:57:54 +01001064 stream = StringIOWrapper('test_suite_ut.function', data)
1065 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +01001066
1067
Azim Khanb31aa442018-07-03 11:57:54 +01001068class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001069 """
Azim Khan599cd242017-07-06 17:34:27 +01001070 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +01001071 Note: Since escaped_split() output is used to write back to the
1072 intermediate data file. Any escape characters in the input are
1073 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001074 """
1075
1076 def test_invalid_input(self):
1077 """
1078 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001079 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001080 """
1081 self.assertRaises(ValueError, escaped_split, '', 'string')
1082
1083 def test_empty_string(self):
1084 """
Azim Khanb31aa442018-07-03 11:57:54 +01001085 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001086 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001087 """
1088 splits = escaped_split('', ':')
1089 self.assertEqual(splits, [])
1090
1091 def test_no_escape(self):
1092 """
Azim Khanb31aa442018-07-03 11:57:54 +01001093 Test with no escape character. The behaviour should be same as
1094 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001095 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001096 """
Azim Khanb31aa442018-07-03 11:57:54 +01001097 test_str = 'yahoo:google'
1098 splits = escaped_split(test_str, ':')
1099 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001100
1101 def test_escaped_input(self):
1102 """
Azim Khanb31aa442018-07-03 11:57:54 +01001103 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001104 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001105 """
Azim Khanb31aa442018-07-03 11:57:54 +01001106 test_str = r'yahoo\:google:facebook'
1107 splits = escaped_split(test_str, ':')
1108 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001109
1110 def test_escaped_escape(self):
1111 """
Azim Khanb31aa442018-07-03 11:57:54 +01001112 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001113 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001114 """
Azim Khan4084ec72018-07-05 14:20:08 +01001115 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +01001116 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001117 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001118
1119 def test_all_at_once(self):
1120 """
Azim Khanb31aa442018-07-03 11:57:54 +01001121 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001122 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001123 """
Azim Khan4084ec72018-07-05 14:20:08 +01001124 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +01001125 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001126 self.assertEqual(splits, [r'yahoo\\', r'google',
1127 r'facebook\:instagram\\',
1128 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001129
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001130
1131class ParseTestData(TestCase):
1132 """
1133 Test suite for parse test data.
1134 """
1135
1136 def test_parser(self):
1137 """
1138 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001139 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001140 """
1141 data = """
1142Diffie-Hellman full exchange #1
1143dhm_do_dhm:10:"23":10:"5"
1144
1145Diffie-Hellman full exchange #2
1146dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1147
1148Diffie-Hellman full exchange #3
1149dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1150
1151Diffie-Hellman selftest
1152dhm_selftest:
1153"""
Azim Khanb31aa442018-07-03 11:57:54 +01001154 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine399b82f2020-03-24 18:36:56 +01001155 # List of (name, function_name, dependencies, args)
1156 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001157 test1, test2, test3, test4 = tests
1158 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1159 self.assertEqual(test1[1], 'dhm_do_dhm')
1160 self.assertEqual(test1[2], [])
1161 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001162
Azim Khanb31aa442018-07-03 11:57:54 +01001163 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1164 self.assertEqual(test2[1], 'dhm_do_dhm')
1165 self.assertEqual(test2[2], [])
1166 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1167 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001168
Azim Khanb31aa442018-07-03 11:57:54 +01001169 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1170 self.assertEqual(test3[1], 'dhm_do_dhm')
1171 self.assertEqual(test3[2], [])
1172 self.assertEqual(test3[3], ['10',
1173 '"9345098382739712938719287391879381271"',
1174 '10',
1175 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001176
Azim Khanb31aa442018-07-03 11:57:54 +01001177 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1178 self.assertEqual(test4[1], 'dhm_selftest')
1179 self.assertEqual(test4[2], [])
1180 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001181
1182 def test_with_dependencies(self):
1183 """
1184 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001185 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001186 """
1187 data = """
1188Diffie-Hellman full exchange #1
1189depends_on:YAHOO
1190dhm_do_dhm:10:"23":10:"5"
1191
1192Diffie-Hellman full exchange #2
1193dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1194
1195"""
Azim Khanb31aa442018-07-03 11:57:54 +01001196 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine399b82f2020-03-24 18:36:56 +01001197 # List of (name, function_name, dependencies, args)
1198 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001199 test1, test2 = tests
1200 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1201 self.assertEqual(test1[1], 'dhm_do_dhm')
1202 self.assertEqual(test1[2], ['YAHOO'])
1203 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001204
Azim Khanb31aa442018-07-03 11:57:54 +01001205 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1206 self.assertEqual(test2[1], 'dhm_do_dhm')
1207 self.assertEqual(test2[2], [])
1208 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1209 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001210
1211 def test_no_args(self):
1212 """
Azim Khanb31aa442018-07-03 11:57:54 +01001213 Test GeneratorInputError is raised when test function name and
1214 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001215 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001216 """
1217 data = """
1218Diffie-Hellman full exchange #1
1219depends_on:YAHOO
1220
1221
1222Diffie-Hellman full exchange #2
1223dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1224
1225"""
Azim Khanb31aa442018-07-03 11:57:54 +01001226 stream = StringIOWrapper('test_suite_ut.function', data)
1227 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001228 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001229 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001230 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001231 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001232 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001233
1234 def test_incomplete_data(self):
1235 """
Azim Khanb31aa442018-07-03 11:57:54 +01001236 Test GeneratorInputError is raised when test function name
1237 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001238 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001239 """
1240 data = """
1241Diffie-Hellman full exchange #1
1242depends_on:YAHOO
1243"""
Azim Khanb31aa442018-07-03 11:57:54 +01001244 stream = StringIOWrapper('test_suite_ut.function', data)
1245 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001246 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001247 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001248 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001249 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001250 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001251
1252
1253class GenDepCheck(TestCase):
1254 """
Azim Khanb31aa442018-07-03 11:57:54 +01001255 Test suite for gen_dep_check(). It is assumed this function is
1256 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001257 """
1258
1259 def test_gen_dep_check(self):
1260 """
1261 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001262 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001263 """
1264 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001265 case 5:
1266 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001267#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001268 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001269#else
Azim Khand61b8372017-07-10 11:54:01 +01001270 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001271#endif
Azim Khand61b8372017-07-10 11:54:01 +01001272 }
1273 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001274 out = gen_dep_check(5, 'YAHOO')
1275 self.assertEqual(out, expected)
1276
Azim Khanb31aa442018-07-03 11:57:54 +01001277 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001278 """
1279 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001280 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001281 """
1282 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001283 case 5:
1284 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001285#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001286 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001287#else
Azim Khand61b8372017-07-10 11:54:01 +01001288 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001289#endif
Azim Khand61b8372017-07-10 11:54:01 +01001290 }
1291 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001292 out = gen_dep_check(5, '!YAHOO')
1293 self.assertEqual(out, expected)
1294
1295 def test_empty_dependency(self):
1296 """
1297 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001298 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001299 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001300 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001301
1302 def test_negative_dep_id(self):
1303 """
1304 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001305 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001306 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001307 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001308
1309
1310class GenExpCheck(TestCase):
1311 """
Azim Khanb31aa442018-07-03 11:57:54 +01001312 Test suite for gen_expression_check(). It is assumed this function
1313 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001314 """
1315
1316 def test_gen_exp_check(self):
1317 """
1318 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001319 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001320 """
1321 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001322 case 5:
1323 {
1324 *out_value = YAHOO;
1325 }
1326 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001327 out = gen_expression_check(5, 'YAHOO')
1328 self.assertEqual(out, expected)
1329
1330 def test_invalid_expression(self):
1331 """
1332 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001333 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001334 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001335 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001336
1337 def test_negative_exp_id(self):
1338 """
1339 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001340 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001341 """
Azim Khanb31aa442018-07-03 11:57:54 +01001342 self.assertRaises(GeneratorInputError, gen_expression_check,
1343 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001344
1345
Azim Khanb31aa442018-07-03 11:57:54 +01001346class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001347 """
Azim Khanb31aa442018-07-03 11:57:54 +01001348 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001349 """
1350
Azim Khanb31aa442018-07-03 11:57:54 +01001351 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001352 """
Azim Khanb31aa442018-07-03 11:57:54 +01001353 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001354 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001355 """
Azim Khanb31aa442018-07-03 11:57:54 +01001356 stream = StringIOWrapper('test_suite_ut.data', '')
1357 unique_dependencies = []
1358 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001359 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001360 self.assertEqual(len(unique_dependencies), 0)
1361 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001362
1363 def test_unique_dep_ids(self):
1364 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001365
1366 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001367 """
Azim Khanb31aa442018-07-03 11:57:54 +01001368 stream = StringIOWrapper('test_suite_ut.data', '')
1369 unique_dependencies = []
1370 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1371 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001372 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001373 case 0:
1374 {
Azim Khan599cd242017-07-06 17:34:27 +01001375#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001376 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001377#else
Azim Khand61b8372017-07-10 11:54:01 +01001378 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001379#endif
Azim Khand61b8372017-07-10 11:54:01 +01001380 }
1381 break;
1382 case 1:
1383 {
Azim Khan599cd242017-07-06 17:34:27 +01001384#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001385 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001386#else
Azim Khand61b8372017-07-10 11:54:01 +01001387 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001388#endif
Azim Khand61b8372017-07-10 11:54:01 +01001389 }
1390 break;
1391 case 2:
1392 {
Azim Khan599cd242017-07-06 17:34:27 +01001393#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001394 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001395#else
Azim Khand61b8372017-07-10 11:54:01 +01001396 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001397#endif
Azim Khand61b8372017-07-10 11:54:01 +01001398 }
1399 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001400 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001401 self.assertEqual(len(unique_dependencies), 3)
1402 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001403
1404 def test_dep_id_repeat(self):
1405 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001406
1407 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001408 """
Azim Khanb31aa442018-07-03 11:57:54 +01001409 stream = StringIOWrapper('test_suite_ut.data', '')
1410 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001411 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001412 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1413 unique_dependencies)
1414 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1415 unique_dependencies)
1416 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1417 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001418 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001419 case 0:
1420 {
Azim Khan599cd242017-07-06 17:34:27 +01001421#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001422 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001423#else
Azim Khand61b8372017-07-10 11:54:01 +01001424 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001425#endif
Azim Khand61b8372017-07-10 11:54:01 +01001426 }
1427 break;
1428 case 1:
1429 {
Azim Khan599cd242017-07-06 17:34:27 +01001430#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001431 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001432#else
Azim Khand61b8372017-07-10 11:54:01 +01001433 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001434#endif
Azim Khand61b8372017-07-10 11:54:01 +01001435 }
1436 break;
1437 case 2:
1438 {
Azim Khan599cd242017-07-06 17:34:27 +01001439#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001440 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001441#else
Azim Khand61b8372017-07-10 11:54:01 +01001442 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001443#endif
Azim Khand61b8372017-07-10 11:54:01 +01001444 }
1445 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001446 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001447 self.assertEqual(len(unique_dependencies), 3)
1448 self.assertEqual(stream.getvalue(),
1449 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001450
1451
1452class WriteParams(TestCase):
1453 """
1454 Test Suite for testing write_parameters().
1455 """
1456
1457 def test_no_params(self):
1458 """
1459 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001460 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001461 """
Azim Khanb31aa442018-07-03 11:57:54 +01001462 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001463 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001464 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001465 self.assertEqual(len(unique_expressions), 0)
1466 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001467 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001468
1469 def test_no_exp_param(self):
1470 """
1471 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001472 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001473 """
Azim Khanb31aa442018-07-03 11:57:54 +01001474 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001475 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001476 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1477 '0'],
1478 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001479 unique_expressions)
1480 self.assertEqual(len(unique_expressions), 0)
1481 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001482 self.assertEqual(stream.getvalue(),
1483 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001484
1485 def test_hex_format_int_param(self):
1486 """
1487 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001488 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001489 """
Azim Khanb31aa442018-07-03 11:57:54 +01001490 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001491 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001492 expression_code = write_parameters(stream,
1493 ['"Yahoo"', '"abcdef00"', '0xAA'],
1494 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001495 unique_expressions)
1496 self.assertEqual(len(unique_expressions), 0)
1497 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001498 self.assertEqual(stream.getvalue(),
1499 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001500
1501 def test_with_exp_param(self):
1502 """
1503 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001504 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001505 """
Azim Khanb31aa442018-07-03 11:57:54 +01001506 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001507 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001508 expression_code = write_parameters(stream,
1509 ['"Yahoo"', '"abcdef00"', '0',
1510 'MACRO1', 'MACRO2', 'MACRO3'],
1511 ['char*', 'hex', 'int',
1512 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001513 unique_expressions)
1514 self.assertEqual(len(unique_expressions), 3)
1515 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1516 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001517 case 0:
1518 {
1519 *out_value = MACRO1;
1520 }
1521 break;
1522 case 1:
1523 {
1524 *out_value = MACRO2;
1525 }
1526 break;
1527 case 2:
1528 {
1529 *out_value = MACRO3;
1530 }
1531 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001532 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001533 self.assertEqual(stream.getvalue(),
1534 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1535 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001536
Azim Khanb31aa442018-07-03 11:57:54 +01001537 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001538 """
1539 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001540 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001541 """
Azim Khanb31aa442018-07-03 11:57:54 +01001542 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001543 unique_expressions = []
1544 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001545 expression_code += write_parameters(stream,
1546 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1547 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001548 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001549 expression_code += write_parameters(stream,
1550 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1551 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001552 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001553 expression_code += write_parameters(stream,
1554 ['0', 'MACRO3', 'MACRO1'],
1555 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001556 unique_expressions)
1557 self.assertEqual(len(unique_expressions), 3)
1558 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1559 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001560 case 0:
1561 {
1562 *out_value = MACRO1;
1563 }
1564 break;
1565 case 1:
1566 {
1567 *out_value = MACRO2;
1568 }
1569 break;
1570 case 2:
1571 {
1572 *out_value = MACRO3;
1573 }
1574 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001575 self.assertEqual(expression_code, expected_expression_code)
1576 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1577:hex:"abcdef00":exp:1:exp:2
1578:int:0:exp:2:exp:0
1579'''
Azim Khanb31aa442018-07-03 11:57:54 +01001580 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001581
1582
Azim Khanb31aa442018-07-03 11:57:54 +01001583class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001584 """
Azim Khanb31aa442018-07-03 11:57:54 +01001585 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001586 """
Azim Khanb31aa442018-07-03 11:57:54 +01001587 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001588 """
Azim Khanb31aa442018-07-03 11:57:54 +01001589 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001590
1591 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001592 """
Azim Khanb31aa442018-07-03 11:57:54 +01001593 dep_check_code, expression_code = \
1594 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001595 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1596 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1597
Azim Khanb31aa442018-07-03 11:57:54 +01001598 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001599 """
Azim Khanb31aa442018-07-03 11:57:54 +01001600 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001601
1602 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001603 """
Azim Khanb31aa442018-07-03 11:57:54 +01001604 dep_check_code, expression_code = \
1605 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1606 'EXPRESSION_CODE')
1607 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001608#if defined(SUITE_DEP)
1609DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001610#endif
1611'''
1612 expected_expression_code = '''
1613#if defined(SUITE_DEP)
1614EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001615#endif
1616'''
Azim Khanb31aa442018-07-03 11:57:54 +01001617 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001618 self.assertEqual(expression_code, expected_expression_code)
1619
1620 def test_no_dep_no_exp(self):
1621 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001622 Test when there are no dependency and expression code.
1623 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001624 """
Azim Khanb31aa442018-07-03 11:57:54 +01001625 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001626 self.assertEqual(dep_check_code, '')
1627 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001628
1629
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001630class GenFromTestData(TestCase):
1631 """
1632 Test suite for gen_from_test_data()
1633 """
1634
Azim Khanb31aa442018-07-03 11:57:54 +01001635 @staticmethod
1636 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001637 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001638 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001639 def test_intermediate_data_file(func_mock1,
1640 write_parameters_mock,
1641 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001642 """
1643 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001644 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001645 """
1646 data = '''
1647My test
1648depends_on:DEP1
1649func1:0
1650'''
1651 data_f = StringIOWrapper('test_suite_ut.data', data)
1652 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1653 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001654 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001655 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001656 write_dependencies_mock.side_effect = write_dependencies
1657 func_mock1.side_effect = gen_suite_dep_checks
1658 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1659 write_dependencies_mock.assert_called_with(out_data_f,
1660 ['DEP1'], ['DEP1'])
1661 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1662 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001663 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001664 case 0:
1665 {
Azim Khan599cd242017-07-06 17:34:27 +01001666#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001667 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001668#else
Azim Khand61b8372017-07-10 11:54:01 +01001669 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001670#endif
Azim Khand61b8372017-07-10 11:54:01 +01001671 }
1672 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001673 func_mock1.assert_called_with(
1674 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001675
1676 def test_function_not_found(self):
1677 """
1678 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001679 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001680 """
1681 data = '''
1682My test
1683depends_on:DEP1
1684func1:0
1685'''
1686 data_f = StringIOWrapper('test_suite_ut.data', data)
1687 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1688 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001689 suite_dependencies = []
1690 self.assertRaises(GeneratorInputError, gen_from_test_data,
1691 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001692
1693 def test_different_func_args(self):
1694 """
Azim Khanb31aa442018-07-03 11:57:54 +01001695 Test that AssertError is raised when no. of parameters and
1696 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001697 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001698 """
1699 data = '''
1700My test
1701depends_on:DEP1
1702func1:0
1703'''
1704 data_f = StringIOWrapper('test_suite_ut.data', data)
1705 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001706 func_info = {'test_func2': (1, ('int', 'hex'))}
1707 suite_dependencies = []
1708 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1709 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001710
1711 def test_output(self):
1712 """
1713 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001714 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001715 """
1716 data = '''
1717My test 1
1718depends_on:DEP1
1719func1:0:0xfa:MACRO1:MACRO2
1720
1721My test 2
1722depends_on:DEP1:DEP2
1723func2:"yahoo":88:MACRO1
1724'''
1725 data_f = StringIOWrapper('test_suite_ut.data', data)
1726 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001727 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1728 'test_func2': (1, ('char*', 'int', 'int'))}
1729 suite_dependencies = []
1730 dep_check_code, expression_code = \
1731 gen_from_test_data(data_f, out_data_f, func_info,
1732 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001733 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001734 case 0:
1735 {
Azim Khan599cd242017-07-06 17:34:27 +01001736#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001737 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001738#else
Azim Khand61b8372017-07-10 11:54:01 +01001739 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001740#endif
Azim Khand61b8372017-07-10 11:54:01 +01001741 }
1742 break;
1743 case 1:
1744 {
Azim Khan599cd242017-07-06 17:34:27 +01001745#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001746 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001747#else
Azim Khand61b8372017-07-10 11:54:01 +01001748 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001749#endif
Azim Khand61b8372017-07-10 11:54:01 +01001750 }
1751 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001752 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001753depends_on:0
17540:int:0:int:0xfa:exp:0:exp:1
1755
1756My test 2
1757depends_on:0:1
17581:char*:"yahoo":int:88:exp:0
1759
1760'''
1761 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001762 case 0:
1763 {
1764 *out_value = MACRO1;
1765 }
1766 break;
1767 case 1:
1768 {
1769 *out_value = MACRO2;
1770 }
1771 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001772 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001773 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001774 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001775
1776
Azim Khanb31aa442018-07-03 11:57:54 +01001777if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001778 unittest_main()