blob: 09f488d586fde5b00c59414278ad3a68862a1962 [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
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# **********
45#
Azim Khanb31aa442018-07-03 11:57:54 +010046# This file is part of Mbed TLS (https://tls.mbed.org)
Azim Khan4b543232017-06-30 09:35:21 +010047
48"""
Mohammad Azim Khan78befd92018-03-06 11:49:41 +000049Unit tests for generate_test_code.py
Azim Khan4b543232017-06-30 09:35:21 +010050"""
51
Gilles Peskineafd19dd2019-02-25 21:39:42 +010052# pylint: disable=wrong-import-order
Mohammad Azim Khan539aa062018-07-06 00:29:50 +010053try:
54 # Python 2
55 from StringIO import StringIO
56except ImportError:
57 # Python 3
58 from io import StringIO
Azim Khanb31aa442018-07-03 11:57:54 +010059from unittest import TestCase, main as unittest_main
Mohammad Azim Khan539aa062018-07-06 00:29:50 +010060try:
61 # Python 2
62 from mock import patch
63except ImportError:
64 # Python 3
65 from unittest.mock import patch
Gilles Peskineafd19dd2019-02-25 21:39:42 +010066# pylint: enable=wrong-import-order
Azim Khanb31aa442018-07-03 11:57:54 +010067from generate_test_code import gen_dependencies, gen_dependencies_one_line
68from generate_test_code import gen_function_wrapper, gen_dispatch
69from generate_test_code import parse_until_pattern, GeneratorInputError
70from generate_test_code import parse_suite_dependencies
71from generate_test_code import parse_function_dependencies
Azim Khanfcdf6852018-07-05 17:31:46 +010072from generate_test_code import parse_function_arguments, parse_function_code
Azim Khanb31aa442018-07-03 11:57:54 +010073from generate_test_code import parse_functions, END_HEADER_REGEX
74from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
75from generate_test_code import parse_test_data, gen_dep_check
76from generate_test_code import gen_expression_check, write_dependencies
77from generate_test_code import write_parameters, gen_suite_dep_checks
78from generate_test_code import gen_from_test_data
79
80
Azim Khan4b543232017-06-30 09:35:21 +010081class GenDep(TestCase):
82 """
83 Test suite for function gen_dep()
84 """
85
Azim Khanb31aa442018-07-03 11:57:54 +010086 def test_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_disabled_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 = ['!DEP1', '!DEP2']
112 dep_start, dep_end = gen_dependencies(dependencies)
113 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +0100114 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +0100115 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
116 'Preprocessor generated incorrectly')
117 self.assertEqual(preprocessor2, '#if !defined(DEP2)',
118 'Preprocessor generated incorrectly')
119 self.assertEqual(endif1, '#endif /* !DEP2 */',
120 'Preprocessor generated incorrectly')
121 self.assertEqual(endif2, '#endif /* !DEP1 */',
122 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100123
Azim Khanb31aa442018-07-03 11:57:54 +0100124 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100125 """
Azim Khanb31aa442018-07-03 11:57:54 +0100126 Test that gen_dep() correctly creates dependencies for given
127 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100128 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100129 """
Azim Khanb31aa442018-07-03 11:57:54 +0100130 dependencies = ['!DEP1', 'DEP2']
131 dep_start, dep_end = gen_dependencies(dependencies)
132 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +0100133 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +0100134 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
135 'Preprocessor generated incorrectly')
136 self.assertEqual(preprocessor2, '#if defined(DEP2)',
137 'Preprocessor generated incorrectly')
138 self.assertEqual(endif1, '#endif /* DEP2 */',
139 'Preprocessor generated incorrectly')
140 self.assertEqual(endif2, '#endif /* !DEP1 */',
141 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100142
Azim Khanb31aa442018-07-03 11:57:54 +0100143 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100144 """
Azim Khanb31aa442018-07-03 11:57:54 +0100145 Test that gen_dep() correctly creates dependencies for given
146 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100147 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100148 """
Azim Khanb31aa442018-07-03 11:57:54 +0100149 dependencies = []
150 dep_start, dep_end = gen_dependencies(dependencies)
151 self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
152 self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100153
Azim Khanb31aa442018-07-03 11:57:54 +0100154 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100155 """
Azim Khanb31aa442018-07-03 11:57:54 +0100156 Test that gen_dep() correctly creates dependencies for given
157 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100158 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100159 """
Azim Khanb31aa442018-07-03 11:57:54 +0100160 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100161 count = 10
162 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100163 dependencies.append('DEP%d' % i)
164 dep_start, dep_end = gen_dependencies(dependencies)
165 self.assertEqual(len(dep_start.splitlines()), count,
166 'Preprocessor generated incorrectly')
167 self.assertEqual(len(dep_end.splitlines()), count,
168 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100169
170
171class GenDepOneLine(TestCase):
172 """
Azim Khanb31aa442018-07-03 11:57:54 +0100173 Test Suite for testing gen_dependencies_one_line()
Azim Khan4b543232017-06-30 09:35:21 +0100174 """
175
Azim Khanb31aa442018-07-03 11:57:54 +0100176 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100177 """
Azim Khanb31aa442018-07-03 11:57:54 +0100178 Test that gen_dep() correctly creates dependencies for given
179 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100180 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100181 """
Azim Khanb31aa442018-07-03 11:57:54 +0100182 dependencies = ['DEP1', 'DEP2']
183 dep_str = gen_dependencies_one_line(dependencies)
184 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
185 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100186
Azim Khanb31aa442018-07-03 11:57:54 +0100187 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100188 """
Azim Khanb31aa442018-07-03 11:57:54 +0100189 Test that gen_dep() correctly creates dependencies for given
190 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100191 :return:
192 """
Azim Khanb31aa442018-07-03 11:57:54 +0100193 dependencies = ['!DEP1', '!DEP2']
194 dep_str = gen_dependencies_one_line(dependencies)
195 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
196 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100197
Azim Khanb31aa442018-07-03 11:57:54 +0100198 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100199 """
Azim Khanb31aa442018-07-03 11:57:54 +0100200 Test that gen_dep() correctly creates dependencies for given
201 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100202 :return:
203 """
Azim Khanb31aa442018-07-03 11:57:54 +0100204 dependencies = ['!DEP1', 'DEP2']
205 dep_str = gen_dependencies_one_line(dependencies)
206 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
207 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100208
Azim Khanb31aa442018-07-03 11:57:54 +0100209 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100210 """
Azim Khanb31aa442018-07-03 11:57:54 +0100211 Test that gen_dep() correctly creates dependencies for given
212 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100213 :return:
214 """
Azim Khanb31aa442018-07-03 11:57:54 +0100215 dependencies = []
216 dep_str = gen_dependencies_one_line(dependencies)
217 self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100218
Azim Khanb31aa442018-07-03 11:57:54 +0100219 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100220 """
Azim Khanb31aa442018-07-03 11:57:54 +0100221 Test that gen_dep() correctly creates dependencies for given
222 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100223 :return:
224 """
Azim Khanb31aa442018-07-03 11:57:54 +0100225 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100226 count = 10
227 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100228 dependencies.append('DEP%d' % i)
229 dep_str = gen_dependencies_one_line(dependencies)
230 expected = '#if ' + ' && '.join(['defined(%s)' %
231 x for x in dependencies])
232 self.assertEqual(dep_str, expected,
233 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100234
235
236class GenFunctionWrapper(TestCase):
237 """
238 Test Suite for testing gen_function_wrapper()
239 """
240
241 def test_params_unpack(self):
242 """
243 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100244
245 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100246 """
247 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
248 expected = '''
249void test_a_wrapper( void ** params )
250{
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100251
Azim Khan4b543232017-06-30 09:35:21 +0100252 test_a( a, b, c, d );
253}
254'''
255 self.assertEqual(code, expected)
256
257 def test_local(self):
258 """
259 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100260
261 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100262 """
Azim Khanb31aa442018-07-03 11:57:54 +0100263 code = gen_function_wrapper('test_a',
264 'int x = 1;', ('x', 'b', 'c', 'd'))
Azim Khan4b543232017-06-30 09:35:21 +0100265 expected = '''
266void test_a_wrapper( void ** params )
267{
Azim Khan4b543232017-06-30 09:35:21 +0100268int x = 1;
269 test_a( x, b, c, d );
270}
271'''
272 self.assertEqual(code, expected)
273
274 def test_empty_params(self):
275 """
276 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100277
278 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100279 """
280 code = gen_function_wrapper('test_a', '', ())
281 expected = '''
282void test_a_wrapper( void ** params )
283{
284 (void)params;
285
286 test_a( );
287}
288'''
289 self.assertEqual(code, expected)
290
291
292class GenDispatch(TestCase):
293 """
294 Test suite for testing gen_dispatch()
295 """
296
297 def test_dispatch(self):
298 """
299 Test that dispatch table entry is generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100300 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100301 """
302 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
303 expected = '''
304#if defined(DEP1) && defined(DEP2)
305 test_a_wrapper,
306#else
307 NULL,
308#endif
309'''
310 self.assertEqual(code, expected)
311
Azim Khanb31aa442018-07-03 11:57:54 +0100312 def test_empty_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100313 """
314 Test empty dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100315 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100316 """
317 code = gen_dispatch('test_a', [])
318 expected = '''
319 test_a_wrapper,
320'''
321 self.assertEqual(code, expected)
322
323
Gilles Peskine5d1dfd42020-03-24 18:25:17 +0100324class StringIOWrapper(StringIO):
Azim Khan4b543232017-06-30 09:35:21 +0100325 """
326 file like class to mock file object in tests.
327 """
Azim Khan4084ec72018-07-05 14:20:08 +0100328 def __init__(self, file_name, data, line_no=0):
Azim Khan4b543232017-06-30 09:35:21 +0100329 """
330 Init file handle.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100331
332 :param file_name:
Azim Khan4b543232017-06-30 09:35:21 +0100333 :param data:
334 :param line_no:
335 """
336 super(StringIOWrapper, self).__init__(data)
337 self.line_no = line_no
338 self.name = file_name
339
340 def next(self):
341 """
Azim Khanb31aa442018-07-03 11:57:54 +0100342 Iterator method. This method overrides base class's
343 next method and extends the next method to count the line
344 numbers as each line is read.
Azim Khan4b543232017-06-30 09:35:21 +0100345
Azim Khanb31aa442018-07-03 11:57:54 +0100346 :return: Line read from file.
347 """
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100348 parent = super(StringIOWrapper, self)
349 if getattr(parent, 'next', None):
350 # Python 2
351 line = parent.next()
352 else:
353 # Python 3
354 line = parent.__next__()
Azim Khan4084ec72018-07-05 14:20:08 +0100355 return line
Azim Khanb31aa442018-07-03 11:57:54 +0100356
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100357 # Python 3
Azim Khanb31aa442018-07-03 11:57:54 +0100358 __next__ = next
359
360 def readline(self, length=0):
Azim Khan4b543232017-06-30 09:35:21 +0100361 """
362 Wrap the base class readline.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100363
Azim Khanb31aa442018-07-03 11:57:54 +0100364 :param length:
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100365 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100366 """
Gilles Peskineafd19dd2019-02-25 21:39:42 +0100367 # pylint: disable=unused-argument
Azim Khan4b543232017-06-30 09:35:21 +0100368 line = super(StringIOWrapper, self).readline()
Azim Khan4084ec72018-07-05 14:20:08 +0100369 if line is not None:
Azim Khan4b543232017-06-30 09:35:21 +0100370 self.line_no += 1
371 return line
372
373
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000374class ParseUntilPattern(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100375 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000376 Test Suite for testing parse_until_pattern().
Azim Khan4b543232017-06-30 09:35:21 +0100377 """
378
379 def test_suite_headers(self):
380 """
381 Test that suite headers are parsed correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100382
383 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100384 """
385 data = '''#include "mbedtls/ecp.h"
386
387#define ECP_PF_UNKNOWN -1
388/* END_HEADER */
389'''
390 expected = '''#line 1 "test_suite_ut.function"
391#include "mbedtls/ecp.h"
392
393#define ECP_PF_UNKNOWN -1
394'''
Azim Khanb31aa442018-07-03 11:57:54 +0100395 stream = StringIOWrapper('test_suite_ut.function', data, line_no=0)
396 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100397 self.assertEqual(headers, expected)
398
399 def test_line_no(self):
400 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100401 Test that #line is set to correct line no. in source .function file.
402
403 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100404 """
405 data = '''#include "mbedtls/ecp.h"
406
407#define ECP_PF_UNKNOWN -1
408/* END_HEADER */
409'''
410 offset_line_no = 5
411 expected = '''#line %d "test_suite_ut.function"
412#include "mbedtls/ecp.h"
413
414#define ECP_PF_UNKNOWN -1
415''' % (offset_line_no + 1)
Azim Khanb31aa442018-07-03 11:57:54 +0100416 stream = StringIOWrapper('test_suite_ut.function', data,
417 offset_line_no)
418 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100419 self.assertEqual(headers, expected)
420
421 def test_no_end_header_comment(self):
422 """
Azim Khanb31aa442018-07-03 11:57:54 +0100423 Test that InvalidFileFormat is raised when end header comment is
424 missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100425 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100426 """
427 data = '''#include "mbedtls/ecp.h"
428
429#define ECP_PF_UNKNOWN -1
430
431'''
Azim Khanb31aa442018-07-03 11:57:54 +0100432 stream = StringIOWrapper('test_suite_ut.function', data)
433 self.assertRaises(GeneratorInputError, parse_until_pattern, stream,
434 END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100435
436
Azim Khanb31aa442018-07-03 11:57:54 +0100437class ParseSuiteDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100438 """
Azim Khanb31aa442018-07-03 11:57:54 +0100439 Test Suite for testing parse_suite_dependencies().
Azim Khan4b543232017-06-30 09:35:21 +0100440 """
441
Azim Khanb31aa442018-07-03 11:57:54 +0100442 def test_suite_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100443 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100444
445 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100446 """
447 data = '''
448 * depends_on:MBEDTLS_ECP_C
449 * END_DEPENDENCIES
450 */
451'''
452 expected = ['MBEDTLS_ECP_C']
Azim Khanb31aa442018-07-03 11:57:54 +0100453 stream = StringIOWrapper('test_suite_ut.function', data)
454 dependencies = parse_suite_dependencies(stream)
455 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100456
457 def test_no_end_dep_comment(self):
458 """
459 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100460 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100461 """
462 data = '''
463* depends_on:MBEDTLS_ECP_C
464'''
Azim Khanb31aa442018-07-03 11:57:54 +0100465 stream = StringIOWrapper('test_suite_ut.function', data)
466 self.assertRaises(GeneratorInputError, parse_suite_dependencies,
467 stream)
Azim Khan4b543232017-06-30 09:35:21 +0100468
Azim Khanb31aa442018-07-03 11:57:54 +0100469 def test_dependencies_split(self):
Azim Khan4b543232017-06-30 09:35:21 +0100470 """
471 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100472 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100473 """
474 data = '''
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100475 * depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
Azim Khan4b543232017-06-30 09:35:21 +0100476 * END_DEPENDENCIES
477 */
478'''
479 expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
Azim Khanb31aa442018-07-03 11:57:54 +0100480 stream = StringIOWrapper('test_suite_ut.function', data)
481 dependencies = parse_suite_dependencies(stream)
482 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100483
484
Azim Khanb31aa442018-07-03 11:57:54 +0100485class ParseFuncDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100486 """
Azim Khanb31aa442018-07-03 11:57:54 +0100487 Test Suite for testing parse_function_dependencies()
Azim Khan4b543232017-06-30 09:35:21 +0100488 """
489
Azim Khanb31aa442018-07-03 11:57:54 +0100490 def test_function_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100491 """
Azim Khanb31aa442018-07-03 11:57:54 +0100492 Test that parse_function_dependencies() correctly parses function
493 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100494 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100495 """
Azim Khanb31aa442018-07-03 11:57:54 +0100496 line = '/* BEGIN_CASE ' \
497 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
Azim Khan4b543232017-06-30 09:35:21 +0100498 expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
Azim Khanb31aa442018-07-03 11:57:54 +0100499 dependencies = parse_function_dependencies(line)
500 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100501
Azim Khanb31aa442018-07-03 11:57:54 +0100502 def test_no_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100503 """
Azim Khanb31aa442018-07-03 11:57:54 +0100504 Test that parse_function_dependencies() correctly parses function
505 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100506 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100507 """
508 line = '/* BEGIN_CASE */'
Azim Khanb31aa442018-07-03 11:57:54 +0100509 dependencies = parse_function_dependencies(line)
510 self.assertEqual(dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100511
Azim Khanb31aa442018-07-03 11:57:54 +0100512 def test_tolerance(self):
Azim Khan4b543232017-06-30 09:35:21 +0100513 """
Azim Khanb31aa442018-07-03 11:57:54 +0100514 Test that parse_function_dependencies() correctly parses function
515 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100516 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100517 """
518 line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
Azim Khanb31aa442018-07-03 11:57:54 +0100519 dependencies = parse_function_dependencies(line)
520 self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
Azim Khan4b543232017-06-30 09:35:21 +0100521
522
523class ParseFuncSignature(TestCase):
524 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100525 Test Suite for parse_function_arguments().
Azim Khan4b543232017-06-30 09:35:21 +0100526 """
527
528 def test_int_and_char_params(self):
529 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100530 Test int and char parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100531 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100532 """
533 line = 'void entropy_threshold( char * a, int b, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100534 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100535 self.assertEqual(args, ['char*', 'int', 'int'])
536 self.assertEqual(local, '')
Azim Khanb31aa442018-07-03 11:57:54 +0100537 self.assertEqual(arg_dispatch, ['(char *) params[0]',
538 '*( (int *) params[1] )',
539 '*( (int *) params[2] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100540
541 def test_hex_params(self):
542 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100543 Test hex parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100544 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100545 """
Azim Khan5fcca462018-06-29 11:05:32 +0100546 line = 'void entropy_threshold( char * a, data_t * h, int result )'
Azim Khanfcdf6852018-07-05 17:31:46 +0100547 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100548 self.assertEqual(args, ['char*', 'hex', 'int'])
Azim Khanb31aa442018-07-03 11:57:54 +0100549 self.assertEqual(local,
Azim Khan4084ec72018-07-05 14:20:08 +0100550 ' data_t data1 = {(uint8_t *) params[1], '
Azim Khanb31aa442018-07-03 11:57:54 +0100551 '*( (uint32_t *) params[2] )};\n')
552 self.assertEqual(arg_dispatch, ['(char *) params[0]',
Azim Khan4084ec72018-07-05 14:20:08 +0100553 '&data1',
Azim Khanb31aa442018-07-03 11:57:54 +0100554 '*( (int *) params[3] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100555
Azim Khan4b543232017-06-30 09:35:21 +0100556 def test_unsupported_arg(self):
557 """
Azim Khan5fcca462018-06-29 11:05:32 +0100558 Test unsupported arguments (not among int, char * and data_t)
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100559 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100560 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100561 line = 'void entropy_threshold( char * a, data_t * h, char result )'
562 self.assertRaises(ValueError, parse_function_arguments, line)
Azim Khan4b543232017-06-30 09:35:21 +0100563
564 def test_no_params(self):
565 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100566 Test no parameters.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100567 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100568 """
569 line = 'void entropy_threshold()'
Azim Khanfcdf6852018-07-05 17:31:46 +0100570 args, local, arg_dispatch = parse_function_arguments(line)
Azim Khan4b543232017-06-30 09:35:21 +0100571 self.assertEqual(args, [])
572 self.assertEqual(local, '')
573 self.assertEqual(arg_dispatch, [])
574
575
576class ParseFunctionCode(TestCase):
577 """
578 Test suite for testing parse_function_code()
579 """
580
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100581 def assert_raises_regex(self, exp, regex, func, *args):
582 """
583 Python 2 & 3 portable wrapper of assertRaisesRegex(p)? function.
584
585 :param exp: Exception type expected to be raised by cb.
586 :param regex: Expected exception message
587 :param func: callable object under test
588 :param args: variable positional arguments
589 """
590 parent = super(ParseFunctionCode, self)
591
592 # Pylint does not appreciate that the super method called
593 # conditionally can be available in other Python version
594 # then that of Pylint.
595 # Workaround is to call the method via getattr.
596 # Pylint ignores that the method got via getattr is
597 # conditionally executed. Method has to be a callable.
598 # Hence, using a dummy callable for getattr default.
599 dummy = lambda *x: None
600 # First Python 3 assertRaisesRegex is checked, since Python 2
601 # assertRaisesRegexp is also available in Python 3 but is
602 # marked deprecated.
603 for name in ('assertRaisesRegex', 'assertRaisesRegexp'):
604 method = getattr(parent, name, dummy)
605 if method is not dummy:
606 method(exp, regex, func, *args)
607 break
608 else:
609 raise AttributeError(" 'ParseFunctionCode' object has no attribute"
610 " 'assertRaisesRegex' or 'assertRaisesRegexp'"
611 )
612
Azim Khan4b543232017-06-30 09:35:21 +0100613 def test_no_function(self):
614 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100615 Test no test function found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100616 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100617 """
618 data = '''
619No
620test
621function
622'''
Azim Khanb31aa442018-07-03 11:57:54 +0100623 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100624 err_msg = 'file: test_suite_ut.function - Test functions not found!'
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100625 self.assert_raises_regex(GeneratorInputError, err_msg,
626 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100627
628 def test_no_end_case_comment(self):
629 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100630 Test missing end case.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100631 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100632 """
633 data = '''
634void test_func()
635{
636}
637'''
Azim Khanb31aa442018-07-03 11:57:54 +0100638 stream = StringIOWrapper('test_suite_ut.function', data)
Azim Khanfcdf6852018-07-05 17:31:46 +0100639 err_msg = r'file: test_suite_ut.function - '\
640 'end case pattern .*? not found!'
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100641 self.assert_raises_regex(GeneratorInputError, err_msg,
642 parse_function_code, stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100643
Azim Khanfcdf6852018-07-05 17:31:46 +0100644 @patch("generate_test_code.parse_function_arguments")
Azim Khanb31aa442018-07-03 11:57:54 +0100645 def test_function_called(self,
Azim Khanfcdf6852018-07-05 17:31:46 +0100646 parse_function_arguments_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100647 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100648 Test parse_function_code()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100649 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100650 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100651 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100652 data = '''
653void test_func()
654{
655}
656'''
Azim Khanb31aa442018-07-03 11:57:54 +0100657 stream = StringIOWrapper('test_suite_ut.function', data)
658 self.assertRaises(GeneratorInputError, parse_function_code,
659 stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100660 self.assertTrue(parse_function_arguments_mock.called)
661 parse_function_arguments_mock.assert_called_with('void test_func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100662
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000663 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100664 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000665 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100666 @patch("generate_test_code.parse_function_arguments")
667 def test_return(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100668 gen_function_wrapper_mock,
669 gen_dependencies_mock,
670 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100671 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100672 Test generated code.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100673 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100674 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100675 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100676 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100677 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100678 gen_dispatch_mock.side_effect = gen_dispatch
679 data = '''
680void func()
681{
682 ba ba black sheep
683 have you any wool
684}
685/* END_CASE */
686'''
Azim Khanb31aa442018-07-03 11:57:54 +0100687 stream = StringIOWrapper('test_suite_ut.function', data)
688 name, arg, code, dispatch_code = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100689
Azim Khanfcdf6852018-07-05 17:31:46 +0100690 self.assertTrue(parse_function_arguments_mock.called)
691 parse_function_arguments_mock.assert_called_with('void func()\n')
Azim Khan4b543232017-06-30 09:35:21 +0100692 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
693 self.assertEqual(name, 'test_func')
694 self.assertEqual(arg, [])
Azim Khan4084ec72018-07-05 14:20:08 +0100695 expected = '''#line 1 "test_suite_ut.function"
696
Azim Khan4b543232017-06-30 09:35:21 +0100697void test_func()
698{
699 ba ba black sheep
700 have you any wool
701exit:
Azim Khan4084ec72018-07-05 14:20:08 +0100702 ;
Azim Khan4b543232017-06-30 09:35:21 +0100703}
704'''
705 self.assertEqual(code, expected)
706 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
707
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000708 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100709 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000710 @patch("generate_test_code.gen_function_wrapper")
Azim Khanfcdf6852018-07-05 17:31:46 +0100711 @patch("generate_test_code.parse_function_arguments")
712 def test_with_exit_label(self, parse_function_arguments_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100713 gen_function_wrapper_mock,
714 gen_dependencies_mock,
715 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100716 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100717 Test when exit label is present.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100718 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100719 """
Azim Khanfcdf6852018-07-05 17:31:46 +0100720 parse_function_arguments_mock.return_value = ([], '', [])
Azim Khan4b543232017-06-30 09:35:21 +0100721 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100722 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100723 gen_dispatch_mock.side_effect = gen_dispatch
724 data = '''
725void func()
726{
727 ba ba black sheep
728 have you any wool
729exit:
730 yes sir yes sir
731 3 bags full
732}
733/* END_CASE */
734'''
Azim Khanb31aa442018-07-03 11:57:54 +0100735 stream = StringIOWrapper('test_suite_ut.function', data)
736 _, _, code, _ = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100737
Azim Khan4084ec72018-07-05 14:20:08 +0100738 expected = '''#line 1 "test_suite_ut.function"
739
Azim Khan4b543232017-06-30 09:35:21 +0100740void test_func()
741{
742 ba ba black sheep
743 have you any wool
744exit:
745 yes sir yes sir
746 3 bags full
747}
748'''
749 self.assertEqual(code, expected)
750
Azim Khanfcdf6852018-07-05 17:31:46 +0100751 def test_non_void_function(self):
752 """
753 Test invalid signature (non void).
754 :return:
755 """
756 data = 'int entropy_threshold( char * a, data_t * h, int result )'
757 err_msg = 'file: test_suite_ut.function - Test functions not found!'
758 stream = StringIOWrapper('test_suite_ut.function', data)
Mohammad Azim Khan539aa062018-07-06 00:29:50 +0100759 self.assert_raises_regex(GeneratorInputError, err_msg,
760 parse_function_code, stream, [], [])
Azim Khanfcdf6852018-07-05 17:31:46 +0100761
762 @patch("generate_test_code.gen_dispatch")
763 @patch("generate_test_code.gen_dependencies")
764 @patch("generate_test_code.gen_function_wrapper")
765 @patch("generate_test_code.parse_function_arguments")
766 def test_functio_name_on_newline(self, parse_function_arguments_mock,
767 gen_function_wrapper_mock,
768 gen_dependencies_mock,
769 gen_dispatch_mock):
770 """
771 Test when exit label is present.
772 :return:
773 """
774 parse_function_arguments_mock.return_value = ([], '', [])
775 gen_function_wrapper_mock.return_value = ''
776 gen_dependencies_mock.side_effect = gen_dependencies
777 gen_dispatch_mock.side_effect = gen_dispatch
778 data = '''
779void
780
781
782func()
783{
784 ba ba black sheep
785 have you any wool
786exit:
787 yes sir yes sir
788 3 bags full
789}
790/* END_CASE */
791'''
792 stream = StringIOWrapper('test_suite_ut.function', data)
793 _, _, code, _ = parse_function_code(stream, [], [])
794
795 expected = '''#line 1 "test_suite_ut.function"
796
797void
798
799
800test_func()
801{
802 ba ba black sheep
803 have you any wool
804exit:
805 yes sir yes sir
806 3 bags full
807}
808'''
809 self.assertEqual(code, expected)
810
Azim Khan4b543232017-06-30 09:35:21 +0100811
812class ParseFunction(TestCase):
813 """
814 Test Suite for testing parse_functions()
815 """
816
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000817 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000818 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100819 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000820 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100821 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100822 """
Azim Khanb31aa442018-07-03 11:57:54 +0100823 def stop(*_unused):
824 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100825 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000826 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100827 data = '''/* BEGIN_HEADER */
828#include "mbedtls/ecp.h"
829
830#define ECP_PF_UNKNOWN -1
831/* END_HEADER */
832'''
Azim Khanb31aa442018-07-03 11:57:54 +0100833 stream = StringIOWrapper('test_suite_ut.function', data)
834 self.assertRaises(Exception, parse_functions, stream)
835 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100836 self.assertEqual(stream.line_no, 1)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000837
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000838 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000839 def test_begin_helper(self, parse_until_pattern_mock):
840 """
841 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100842 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000843 """
Azim Khanb31aa442018-07-03 11:57:54 +0100844 def stop(*_unused):
845 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000846 raise Exception
847 parse_until_pattern_mock.side_effect = stop
848 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100849void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000850{
Azim Khanb31aa442018-07-03 11:57:54 +0100851 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000852}
853/* END_SUITE_HELPERS */
854'''
Azim Khanb31aa442018-07-03 11:57:54 +0100855 stream = StringIOWrapper('test_suite_ut.function', data)
856 self.assertRaises(Exception, parse_functions, stream)
857 parse_until_pattern_mock.assert_called_with(stream,
858 END_SUITE_HELPERS_REGEX)
Azim Khan4084ec72018-07-05 14:20:08 +0100859 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100860
Azim Khanb31aa442018-07-03 11:57:54 +0100861 @patch("generate_test_code.parse_suite_dependencies")
862 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100863 """
Azim Khanb31aa442018-07-03 11:57:54 +0100864 Test that begin dep is checked and parse_suite_dependencies() is
865 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100866 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100867 """
Azim Khanb31aa442018-07-03 11:57:54 +0100868 def stop(*_unused):
869 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100870 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100871 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100872 data = '''/* BEGIN_DEPENDENCIES
873 * depends_on:MBEDTLS_ECP_C
874 * END_DEPENDENCIES
875 */
876'''
Azim Khanb31aa442018-07-03 11:57:54 +0100877 stream = StringIOWrapper('test_suite_ut.function', data)
878 self.assertRaises(Exception, parse_functions, stream)
879 parse_suite_dependencies_mock.assert_called_with(stream)
Azim Khan4084ec72018-07-05 14:20:08 +0100880 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100881
Azim Khanb31aa442018-07-03 11:57:54 +0100882 @patch("generate_test_code.parse_function_dependencies")
883 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100884 """
Azim Khanb31aa442018-07-03 11:57:54 +0100885 Test that begin dep is checked and parse_function_dependencies() is
886 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100887 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100888 """
Azim Khanb31aa442018-07-03 11:57:54 +0100889 def stop(*_unused):
890 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100891 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100892 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100893
Azim Khanb31aa442018-07-03 11:57:54 +0100894 dependencies_str = '/* BEGIN_CASE ' \
895 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100896 data = '''%svoid test_func()
897{
898}
Azim Khanb31aa442018-07-03 11:57:54 +0100899''' % dependencies_str
900 stream = StringIOWrapper('test_suite_ut.function', data)
901 self.assertRaises(Exception, parse_functions, stream)
902 func_mock.assert_called_with(dependencies_str)
Azim Khan4084ec72018-07-05 14:20:08 +0100903 self.assertEqual(stream.line_no, 1)
Azim Khan4b543232017-06-30 09:35:21 +0100904
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000905 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +0100906 @patch("generate_test_code.parse_function_dependencies")
907 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +0100908 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000909 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100910 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100911 """
Azim Khanb31aa442018-07-03 11:57:54 +0100912 func_mock1.return_value = []
913 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +0100914{
915}
916'''
917 func_dispatch = '''
918 test_func_wrapper,
919'''
Azim Khanb31aa442018-07-03 11:57:54 +0100920 func_mock2.return_value = 'test_func', [],\
921 in_func_code, func_dispatch
922 dependencies_str = '/* BEGIN_CASE ' \
923 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100924 data = '''%svoid test_func()
925{
926}
Azim Khanb31aa442018-07-03 11:57:54 +0100927''' % dependencies_str
928 stream = StringIOWrapper('test_suite_ut.function', data)
929 suite_dependencies, dispatch_code, func_code, func_info = \
930 parse_functions(stream)
931 func_mock1.assert_called_with(dependencies_str)
932 func_mock2.assert_called_with(stream, [], [])
933 self.assertEqual(stream.line_no, 5)
934 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100935 expected_dispatch_code = '''/* Function Id: 0 */
936
937 test_func_wrapper,
938'''
939 self.assertEqual(dispatch_code, expected_dispatch_code)
940 self.assertEqual(func_code, in_func_code)
941 self.assertEqual(func_info, {'test_func': (0, [])})
942
943 def test_parsing(self):
944 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000945 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100946 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100947 """
948 data = '''/* BEGIN_HEADER */
949#include "mbedtls/ecp.h"
950
951#define ECP_PF_UNKNOWN -1
952/* END_HEADER */
953
954/* BEGIN_DEPENDENCIES
955 * depends_on:MBEDTLS_ECP_C
956 * END_DEPENDENCIES
957 */
958
959/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
960void func1()
961{
962}
963/* END_CASE */
964
965/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
966void func2()
967{
968}
969/* END_CASE */
970'''
Azim Khanb31aa442018-07-03 11:57:54 +0100971 stream = StringIOWrapper('test_suite_ut.function', data)
972 suite_dependencies, dispatch_code, func_code, func_info = \
973 parse_functions(stream)
974 self.assertEqual(stream.line_no, 23)
975 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +0100976
977 expected_dispatch_code = '''/* Function Id: 0 */
978
979#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
980 test_func1_wrapper,
981#else
982 NULL,
983#endif
984/* Function Id: 1 */
985
986#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
987 test_func2_wrapper,
988#else
989 NULL,
990#endif
991'''
992 self.assertEqual(dispatch_code, expected_dispatch_code)
993 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
Azim Khan4084ec72018-07-05 14:20:08 +0100994#line 2 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +0100995#include "mbedtls/ecp.h"
996
997#define ECP_PF_UNKNOWN -1
998#if defined(MBEDTLS_ENTROPY_NV_SEED)
999#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001000#line 13 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001001void test_func1()
1002{
1003exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001004 ;
Azim Khan4b543232017-06-30 09:35:21 +01001005}
1006
1007void test_func1_wrapper( void ** params )
1008{
1009 (void)params;
1010
1011 test_func1( );
1012}
1013#endif /* MBEDTLS_FS_IO */
1014#endif /* MBEDTLS_ENTROPY_NV_SEED */
1015#if defined(MBEDTLS_ENTROPY_NV_SEED)
1016#if defined(MBEDTLS_FS_IO)
Azim Khan4084ec72018-07-05 14:20:08 +01001017#line 19 "test_suite_ut.function"
Azim Khan4b543232017-06-30 09:35:21 +01001018void test_func2()
1019{
1020exit:
Azim Khan4084ec72018-07-05 14:20:08 +01001021 ;
Azim Khan4b543232017-06-30 09:35:21 +01001022}
1023
1024void test_func2_wrapper( void ** params )
1025{
1026 (void)params;
1027
1028 test_func2( );
1029}
1030#endif /* MBEDTLS_FS_IO */
1031#endif /* MBEDTLS_ENTROPY_NV_SEED */
1032#endif /* MBEDTLS_ECP_C */
1033'''
1034 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001035 self.assertEqual(func_info, {'test_func1': (0, []),
1036 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +01001037
1038 def test_same_function_name(self):
1039 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +00001040 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001041 :return:
Azim Khan4b543232017-06-30 09:35:21 +01001042 """
1043 data = '''/* BEGIN_HEADER */
1044#include "mbedtls/ecp.h"
1045
1046#define ECP_PF_UNKNOWN -1
1047/* END_HEADER */
1048
1049/* BEGIN_DEPENDENCIES
1050 * depends_on:MBEDTLS_ECP_C
1051 * END_DEPENDENCIES
1052 */
1053
1054/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1055void func()
1056{
1057}
1058/* END_CASE */
1059
1060/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
1061void func()
1062{
1063}
1064/* END_CASE */
1065'''
Azim Khanb31aa442018-07-03 11:57:54 +01001066 stream = StringIOWrapper('test_suite_ut.function', data)
1067 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +01001068
1069
Azim Khanb31aa442018-07-03 11:57:54 +01001070class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001071 """
Azim Khan599cd242017-07-06 17:34:27 +01001072 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +01001073 Note: Since escaped_split() output is used to write back to the
1074 intermediate data file. Any escape characters in the input are
1075 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001076 """
1077
1078 def test_invalid_input(self):
1079 """
1080 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001081 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001082 """
1083 self.assertRaises(ValueError, escaped_split, '', 'string')
1084
1085 def test_empty_string(self):
1086 """
Azim Khanb31aa442018-07-03 11:57:54 +01001087 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001088 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001089 """
1090 splits = escaped_split('', ':')
1091 self.assertEqual(splits, [])
1092
1093 def test_no_escape(self):
1094 """
Azim Khanb31aa442018-07-03 11:57:54 +01001095 Test with no escape character. The behaviour should be same as
1096 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001097 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001098 """
Azim Khanb31aa442018-07-03 11:57:54 +01001099 test_str = 'yahoo:google'
1100 splits = escaped_split(test_str, ':')
1101 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001102
1103 def test_escaped_input(self):
1104 """
Azim Khanb31aa442018-07-03 11:57:54 +01001105 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001106 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001107 """
Azim Khanb31aa442018-07-03 11:57:54 +01001108 test_str = r'yahoo\:google:facebook'
1109 splits = escaped_split(test_str, ':')
1110 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001111
1112 def test_escaped_escape(self):
1113 """
Azim Khanb31aa442018-07-03 11:57:54 +01001114 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001115 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001116 """
Azim Khan4084ec72018-07-05 14:20:08 +01001117 test_str = r'yahoo\\:google:facebook'
Azim Khanb31aa442018-07-03 11:57:54 +01001118 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001119 self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001120
1121 def test_all_at_once(self):
1122 """
Azim Khanb31aa442018-07-03 11:57:54 +01001123 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001124 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001125 """
Azim Khan4084ec72018-07-05 14:20:08 +01001126 test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
Azim Khanb31aa442018-07-03 11:57:54 +01001127 splits = escaped_split(test_str, ':')
Azim Khan4084ec72018-07-05 14:20:08 +01001128 self.assertEqual(splits, [r'yahoo\\', r'google',
1129 r'facebook\:instagram\\',
1130 r'bbc\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001131
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001132
1133class ParseTestData(TestCase):
1134 """
1135 Test suite for parse test data.
1136 """
1137
1138 def test_parser(self):
1139 """
1140 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001141 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001142 """
1143 data = """
1144Diffie-Hellman full exchange #1
1145dhm_do_dhm:10:"23":10:"5"
1146
1147Diffie-Hellman full exchange #2
1148dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1149
1150Diffie-Hellman full exchange #3
1151dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1152
1153Diffie-Hellman selftest
1154dhm_selftest:
1155"""
Azim Khanb31aa442018-07-03 11:57:54 +01001156 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine399b82f2020-03-24 18:36:56 +01001157 # List of (name, function_name, dependencies, args)
1158 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001159 test1, test2, test3, test4 = tests
1160 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1161 self.assertEqual(test1[1], 'dhm_do_dhm')
1162 self.assertEqual(test1[2], [])
1163 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001164
Azim Khanb31aa442018-07-03 11:57:54 +01001165 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1166 self.assertEqual(test2[1], 'dhm_do_dhm')
1167 self.assertEqual(test2[2], [])
1168 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1169 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001170
Azim Khanb31aa442018-07-03 11:57:54 +01001171 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1172 self.assertEqual(test3[1], 'dhm_do_dhm')
1173 self.assertEqual(test3[2], [])
1174 self.assertEqual(test3[3], ['10',
1175 '"9345098382739712938719287391879381271"',
1176 '10',
1177 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001178
Azim Khanb31aa442018-07-03 11:57:54 +01001179 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1180 self.assertEqual(test4[1], 'dhm_selftest')
1181 self.assertEqual(test4[2], [])
1182 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001183
1184 def test_with_dependencies(self):
1185 """
1186 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001187 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001188 """
1189 data = """
1190Diffie-Hellman full exchange #1
1191depends_on:YAHOO
1192dhm_do_dhm:10:"23":10:"5"
1193
1194Diffie-Hellman full exchange #2
1195dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1196
1197"""
Azim Khanb31aa442018-07-03 11:57:54 +01001198 stream = StringIOWrapper('test_suite_ut.function', data)
Gilles Peskine399b82f2020-03-24 18:36:56 +01001199 # List of (name, function_name, dependencies, args)
1200 tests = list(parse_test_data(stream))
Azim Khanb31aa442018-07-03 11:57:54 +01001201 test1, test2 = tests
1202 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1203 self.assertEqual(test1[1], 'dhm_do_dhm')
1204 self.assertEqual(test1[2], ['YAHOO'])
1205 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001206
Azim Khanb31aa442018-07-03 11:57:54 +01001207 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1208 self.assertEqual(test2[1], 'dhm_do_dhm')
1209 self.assertEqual(test2[2], [])
1210 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1211 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001212
1213 def test_no_args(self):
1214 """
Azim Khanb31aa442018-07-03 11:57:54 +01001215 Test GeneratorInputError is raised when test function name and
1216 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001217 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001218 """
1219 data = """
1220Diffie-Hellman full exchange #1
1221depends_on:YAHOO
1222
1223
1224Diffie-Hellman full exchange #2
1225dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1226
1227"""
Azim Khanb31aa442018-07-03 11:57:54 +01001228 stream = StringIOWrapper('test_suite_ut.function', data)
1229 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001230 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001231 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001232 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001233 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001234 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001235
1236 def test_incomplete_data(self):
1237 """
Azim Khanb31aa442018-07-03 11:57:54 +01001238 Test GeneratorInputError is raised when test function name
1239 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001240 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001241 """
1242 data = """
1243Diffie-Hellman full exchange #1
1244depends_on:YAHOO
1245"""
Azim Khanb31aa442018-07-03 11:57:54 +01001246 stream = StringIOWrapper('test_suite_ut.function', data)
1247 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001248 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001249 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001250 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001251 except GeneratorInputError as err:
Mohammad Azim Khan539aa062018-07-06 00:29:50 +01001252 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001253
1254
1255class GenDepCheck(TestCase):
1256 """
Azim Khanb31aa442018-07-03 11:57:54 +01001257 Test suite for gen_dep_check(). It is assumed this function is
1258 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001259 """
1260
1261 def test_gen_dep_check(self):
1262 """
1263 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001264 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001265 """
1266 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001267 case 5:
1268 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001269#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001270 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001271#else
Azim Khand61b8372017-07-10 11:54:01 +01001272 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001273#endif
Azim Khand61b8372017-07-10 11:54:01 +01001274 }
1275 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001276 out = gen_dep_check(5, 'YAHOO')
1277 self.assertEqual(out, expected)
1278
Azim Khanb31aa442018-07-03 11:57:54 +01001279 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001280 """
1281 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001282 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001283 """
1284 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001285 case 5:
1286 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001287#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001288 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001289#else
Azim Khand61b8372017-07-10 11:54:01 +01001290 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001291#endif
Azim Khand61b8372017-07-10 11:54:01 +01001292 }
1293 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001294 out = gen_dep_check(5, '!YAHOO')
1295 self.assertEqual(out, expected)
1296
1297 def test_empty_dependency(self):
1298 """
1299 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001300 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001301 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001302 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001303
1304 def test_negative_dep_id(self):
1305 """
1306 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001307 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001308 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001309 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001310
1311
1312class GenExpCheck(TestCase):
1313 """
Azim Khanb31aa442018-07-03 11:57:54 +01001314 Test suite for gen_expression_check(). It is assumed this function
1315 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001316 """
1317
1318 def test_gen_exp_check(self):
1319 """
1320 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001321 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001322 """
1323 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001324 case 5:
1325 {
1326 *out_value = YAHOO;
1327 }
1328 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001329 out = gen_expression_check(5, 'YAHOO')
1330 self.assertEqual(out, expected)
1331
1332 def test_invalid_expression(self):
1333 """
1334 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001335 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001336 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001337 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001338
1339 def test_negative_exp_id(self):
1340 """
1341 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001342 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001343 """
Azim Khanb31aa442018-07-03 11:57:54 +01001344 self.assertRaises(GeneratorInputError, gen_expression_check,
1345 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001346
1347
Azim Khanb31aa442018-07-03 11:57:54 +01001348class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001349 """
Azim Khanb31aa442018-07-03 11:57:54 +01001350 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001351 """
1352
Azim Khanb31aa442018-07-03 11:57:54 +01001353 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001354 """
Azim Khanb31aa442018-07-03 11:57:54 +01001355 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001356 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001357 """
Azim Khanb31aa442018-07-03 11:57:54 +01001358 stream = StringIOWrapper('test_suite_ut.data', '')
1359 unique_dependencies = []
1360 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001361 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001362 self.assertEqual(len(unique_dependencies), 0)
1363 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001364
1365 def test_unique_dep_ids(self):
1366 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001367
1368 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001369 """
Azim Khanb31aa442018-07-03 11:57:54 +01001370 stream = StringIOWrapper('test_suite_ut.data', '')
1371 unique_dependencies = []
1372 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1373 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001374 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001375 case 0:
1376 {
Azim Khan599cd242017-07-06 17:34:27 +01001377#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001378 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001379#else
Azim Khand61b8372017-07-10 11:54:01 +01001380 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001381#endif
Azim Khand61b8372017-07-10 11:54:01 +01001382 }
1383 break;
1384 case 1:
1385 {
Azim Khan599cd242017-07-06 17:34:27 +01001386#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001387 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001388#else
Azim Khand61b8372017-07-10 11:54:01 +01001389 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001390#endif
Azim Khand61b8372017-07-10 11:54:01 +01001391 }
1392 break;
1393 case 2:
1394 {
Azim Khan599cd242017-07-06 17:34:27 +01001395#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001396 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001397#else
Azim Khand61b8372017-07-10 11:54:01 +01001398 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001399#endif
Azim Khand61b8372017-07-10 11:54:01 +01001400 }
1401 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001402 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001403 self.assertEqual(len(unique_dependencies), 3)
1404 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001405
1406 def test_dep_id_repeat(self):
1407 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001408
1409 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001410 """
Azim Khanb31aa442018-07-03 11:57:54 +01001411 stream = StringIOWrapper('test_suite_ut.data', '')
1412 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001413 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001414 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1415 unique_dependencies)
1416 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1417 unique_dependencies)
1418 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1419 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001420 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001421 case 0:
1422 {
Azim Khan599cd242017-07-06 17:34:27 +01001423#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001424 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001425#else
Azim Khand61b8372017-07-10 11:54:01 +01001426 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001427#endif
Azim Khand61b8372017-07-10 11:54:01 +01001428 }
1429 break;
1430 case 1:
1431 {
Azim Khan599cd242017-07-06 17:34:27 +01001432#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001433 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001434#else
Azim Khand61b8372017-07-10 11:54:01 +01001435 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001436#endif
Azim Khand61b8372017-07-10 11:54:01 +01001437 }
1438 break;
1439 case 2:
1440 {
Azim Khan599cd242017-07-06 17:34:27 +01001441#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001442 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001443#else
Azim Khand61b8372017-07-10 11:54:01 +01001444 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001445#endif
Azim Khand61b8372017-07-10 11:54:01 +01001446 }
1447 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001448 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001449 self.assertEqual(len(unique_dependencies), 3)
1450 self.assertEqual(stream.getvalue(),
1451 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001452
1453
1454class WriteParams(TestCase):
1455 """
1456 Test Suite for testing write_parameters().
1457 """
1458
1459 def test_no_params(self):
1460 """
1461 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001462 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001463 """
Azim Khanb31aa442018-07-03 11:57:54 +01001464 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001465 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001466 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001467 self.assertEqual(len(unique_expressions), 0)
1468 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001469 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001470
1471 def test_no_exp_param(self):
1472 """
1473 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001474 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001475 """
Azim Khanb31aa442018-07-03 11:57:54 +01001476 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001477 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001478 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1479 '0'],
1480 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001481 unique_expressions)
1482 self.assertEqual(len(unique_expressions), 0)
1483 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001484 self.assertEqual(stream.getvalue(),
1485 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001486
1487 def test_hex_format_int_param(self):
1488 """
1489 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001490 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001491 """
Azim Khanb31aa442018-07-03 11:57:54 +01001492 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001493 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001494 expression_code = write_parameters(stream,
1495 ['"Yahoo"', '"abcdef00"', '0xAA'],
1496 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001497 unique_expressions)
1498 self.assertEqual(len(unique_expressions), 0)
1499 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001500 self.assertEqual(stream.getvalue(),
1501 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001502
1503 def test_with_exp_param(self):
1504 """
1505 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001506 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001507 """
Azim Khanb31aa442018-07-03 11:57:54 +01001508 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001509 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001510 expression_code = write_parameters(stream,
1511 ['"Yahoo"', '"abcdef00"', '0',
1512 'MACRO1', 'MACRO2', 'MACRO3'],
1513 ['char*', 'hex', 'int',
1514 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001515 unique_expressions)
1516 self.assertEqual(len(unique_expressions), 3)
1517 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1518 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001519 case 0:
1520 {
1521 *out_value = MACRO1;
1522 }
1523 break;
1524 case 1:
1525 {
1526 *out_value = MACRO2;
1527 }
1528 break;
1529 case 2:
1530 {
1531 *out_value = MACRO3;
1532 }
1533 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001534 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001535 self.assertEqual(stream.getvalue(),
1536 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1537 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001538
Azim Khanb31aa442018-07-03 11:57:54 +01001539 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001540 """
1541 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001542 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001543 """
Azim Khanb31aa442018-07-03 11:57:54 +01001544 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001545 unique_expressions = []
1546 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001547 expression_code += write_parameters(stream,
1548 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1549 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001550 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001551 expression_code += write_parameters(stream,
1552 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1553 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001554 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001555 expression_code += write_parameters(stream,
1556 ['0', 'MACRO3', 'MACRO1'],
1557 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001558 unique_expressions)
1559 self.assertEqual(len(unique_expressions), 3)
1560 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1561 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001562 case 0:
1563 {
1564 *out_value = MACRO1;
1565 }
1566 break;
1567 case 1:
1568 {
1569 *out_value = MACRO2;
1570 }
1571 break;
1572 case 2:
1573 {
1574 *out_value = MACRO3;
1575 }
1576 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001577 self.assertEqual(expression_code, expected_expression_code)
1578 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1579:hex:"abcdef00":exp:1:exp:2
1580:int:0:exp:2:exp:0
1581'''
Azim Khanb31aa442018-07-03 11:57:54 +01001582 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001583
1584
Azim Khanb31aa442018-07-03 11:57:54 +01001585class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001586 """
Azim Khanb31aa442018-07-03 11:57:54 +01001587 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001588 """
Azim Khanb31aa442018-07-03 11:57:54 +01001589 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001590 """
Azim Khanb31aa442018-07-03 11:57:54 +01001591 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001592
1593 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001594 """
Azim Khanb31aa442018-07-03 11:57:54 +01001595 dep_check_code, expression_code = \
1596 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001597 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1598 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1599
Azim Khanb31aa442018-07-03 11:57:54 +01001600 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001601 """
Azim Khanb31aa442018-07-03 11:57:54 +01001602 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001603
1604 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001605 """
Azim Khanb31aa442018-07-03 11:57:54 +01001606 dep_check_code, expression_code = \
1607 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1608 'EXPRESSION_CODE')
1609 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001610#if defined(SUITE_DEP)
1611DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001612#endif
1613'''
1614 expected_expression_code = '''
1615#if defined(SUITE_DEP)
1616EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001617#endif
1618'''
Azim Khanb31aa442018-07-03 11:57:54 +01001619 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001620 self.assertEqual(expression_code, expected_expression_code)
1621
1622 def test_no_dep_no_exp(self):
1623 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001624 Test when there are no dependency and expression code.
1625 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001626 """
Azim Khanb31aa442018-07-03 11:57:54 +01001627 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001628 self.assertEqual(dep_check_code, '')
1629 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001630
1631
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001632class GenFromTestData(TestCase):
1633 """
1634 Test suite for gen_from_test_data()
1635 """
1636
Azim Khanb31aa442018-07-03 11:57:54 +01001637 @staticmethod
1638 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001639 @patch("generate_test_code.write_parameters")
Azim Khan4084ec72018-07-05 14:20:08 +01001640 @patch("generate_test_code.gen_suite_dep_checks")
Azim Khanb31aa442018-07-03 11:57:54 +01001641 def test_intermediate_data_file(func_mock1,
1642 write_parameters_mock,
1643 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001644 """
1645 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001646 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001647 """
1648 data = '''
1649My test
1650depends_on:DEP1
1651func1:0
1652'''
1653 data_f = StringIOWrapper('test_suite_ut.data', data)
1654 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1655 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001656 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001657 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001658 write_dependencies_mock.side_effect = write_dependencies
1659 func_mock1.side_effect = gen_suite_dep_checks
1660 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1661 write_dependencies_mock.assert_called_with(out_data_f,
1662 ['DEP1'], ['DEP1'])
1663 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1664 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001665 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001666 case 0:
1667 {
Azim Khan599cd242017-07-06 17:34:27 +01001668#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001669 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001670#else
Azim Khand61b8372017-07-10 11:54:01 +01001671 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001672#endif
Azim Khand61b8372017-07-10 11:54:01 +01001673 }
1674 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001675 func_mock1.assert_called_with(
1676 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001677
1678 def test_function_not_found(self):
1679 """
1680 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001681 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001682 """
1683 data = '''
1684My test
1685depends_on:DEP1
1686func1:0
1687'''
1688 data_f = StringIOWrapper('test_suite_ut.data', data)
1689 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1690 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001691 suite_dependencies = []
1692 self.assertRaises(GeneratorInputError, gen_from_test_data,
1693 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001694
1695 def test_different_func_args(self):
1696 """
Azim Khanb31aa442018-07-03 11:57:54 +01001697 Test that AssertError is raised when no. of parameters and
1698 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001699 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001700 """
1701 data = '''
1702My test
1703depends_on:DEP1
1704func1:0
1705'''
1706 data_f = StringIOWrapper('test_suite_ut.data', data)
1707 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001708 func_info = {'test_func2': (1, ('int', 'hex'))}
1709 suite_dependencies = []
1710 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1711 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001712
1713 def test_output(self):
1714 """
1715 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001716 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001717 """
1718 data = '''
1719My test 1
1720depends_on:DEP1
1721func1:0:0xfa:MACRO1:MACRO2
1722
1723My test 2
1724depends_on:DEP1:DEP2
1725func2:"yahoo":88:MACRO1
1726'''
1727 data_f = StringIOWrapper('test_suite_ut.data', data)
1728 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001729 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1730 'test_func2': (1, ('char*', 'int', 'int'))}
1731 suite_dependencies = []
1732 dep_check_code, expression_code = \
1733 gen_from_test_data(data_f, out_data_f, func_info,
1734 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001735 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001736 case 0:
1737 {
Azim Khan599cd242017-07-06 17:34:27 +01001738#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001739 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001740#else
Azim Khand61b8372017-07-10 11:54:01 +01001741 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001742#endif
Azim Khand61b8372017-07-10 11:54:01 +01001743 }
1744 break;
1745 case 1:
1746 {
Azim Khan599cd242017-07-06 17:34:27 +01001747#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001748 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001749#else
Azim Khand61b8372017-07-10 11:54:01 +01001750 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001751#endif
Azim Khand61b8372017-07-10 11:54:01 +01001752 }
1753 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001754 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001755depends_on:0
17560:int:0:int:0xfa:exp:0:exp:1
1757
1758My test 2
1759depends_on:0:1
17601:char*:"yahoo":int:88:exp:0
1761
1762'''
1763 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001764 case 0:
1765 {
1766 *out_value = MACRO1;
1767 }
1768 break;
1769 case 1:
1770 {
1771 *out_value = MACRO2;
1772 }
1773 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001774 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001775 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001776 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001777
1778
Azim Khanb31aa442018-07-03 11:57:54 +01001779if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001780 unittest_main()