blob: f0a935d20282b2021a61e84c29368d03811ea340 [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#
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00004# Copyright (C) 2018, ARM Limited, All Rights Reserved
Azim Khanf0e42fb2017-08-02 14:47:13 +01005# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
Azim Khanb31aa442018-07-03 11:57:54 +010019# This file is part of Mbed TLS (https://tls.mbed.org)
Azim Khan4b543232017-06-30 09:35:21 +010020
21"""
Mohammad Azim Khan78befd92018-03-06 11:49:41 +000022Unit tests for generate_test_code.py
Azim Khan4b543232017-06-30 09:35:21 +010023"""
24
25
Azim Khanb31aa442018-07-03 11:57:54 +010026import sys
27from StringIO import StringIO
28from unittest import TestCase, main as unittest_main
29from mock import patch
30from generate_test_code import gen_dependencies, gen_dependencies_one_line
31from generate_test_code import gen_function_wrapper, gen_dispatch
32from generate_test_code import parse_until_pattern, GeneratorInputError
33from generate_test_code import parse_suite_dependencies
34from generate_test_code import parse_function_dependencies
35from generate_test_code import parse_function_signature, parse_function_code
36from generate_test_code import parse_functions, END_HEADER_REGEX
37from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split
38from generate_test_code import parse_test_data, gen_dep_check
39from generate_test_code import gen_expression_check, write_dependencies
40from generate_test_code import write_parameters, gen_suite_dep_checks
41from generate_test_code import gen_from_test_data
42
43
Azim Khan4b543232017-06-30 09:35:21 +010044class GenDep(TestCase):
45 """
46 Test suite for function gen_dep()
47 """
48
Azim Khanb31aa442018-07-03 11:57:54 +010049 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010050 """
Azim Khanb31aa442018-07-03 11:57:54 +010051 Test that gen_dep() correctly creates dependencies for given
52 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010053 :return:
Azim Khan4b543232017-06-30 09:35:21 +010054 """
Azim Khanb31aa442018-07-03 11:57:54 +010055 dependencies = ['DEP1', 'DEP2']
56 dep_start, dep_end = gen_dependencies(dependencies)
57 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010058 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010059 self.assertEqual(preprocessor1, '#if defined(DEP1)',
60 'Preprocessor generated incorrectly')
61 self.assertEqual(preprocessor2, '#if defined(DEP2)',
62 'Preprocessor generated incorrectly')
63 self.assertEqual(endif1, '#endif /* DEP2 */',
64 'Preprocessor generated incorrectly')
65 self.assertEqual(endif2, '#endif /* DEP1 */',
66 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010067
Azim Khanb31aa442018-07-03 11:57:54 +010068 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010069 """
Azim Khanb31aa442018-07-03 11:57:54 +010070 Test that gen_dep() correctly creates dependencies for given
71 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010072 :return:
Azim Khan4b543232017-06-30 09:35:21 +010073 """
Azim Khanb31aa442018-07-03 11:57:54 +010074 dependencies = ['!DEP1', '!DEP2']
75 dep_start, dep_end = gen_dependencies(dependencies)
76 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010077 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010078 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
79 'Preprocessor generated incorrectly')
80 self.assertEqual(preprocessor2, '#if !defined(DEP2)',
81 'Preprocessor generated incorrectly')
82 self.assertEqual(endif1, '#endif /* !DEP2 */',
83 'Preprocessor generated incorrectly')
84 self.assertEqual(endif2, '#endif /* !DEP1 */',
85 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +010086
Azim Khanb31aa442018-07-03 11:57:54 +010087 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +010088 """
Azim Khanb31aa442018-07-03 11:57:54 +010089 Test that gen_dep() correctly creates dependencies for given
90 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +010091 :return:
Azim Khan4b543232017-06-30 09:35:21 +010092 """
Azim Khanb31aa442018-07-03 11:57:54 +010093 dependencies = ['!DEP1', 'DEP2']
94 dep_start, dep_end = gen_dependencies(dependencies)
95 preprocessor1, preprocessor2 = dep_start.splitlines()
Azim Khan4b543232017-06-30 09:35:21 +010096 endif1, endif2 = dep_end.splitlines()
Azim Khanb31aa442018-07-03 11:57:54 +010097 self.assertEqual(preprocessor1, '#if !defined(DEP1)',
98 'Preprocessor generated incorrectly')
99 self.assertEqual(preprocessor2, '#if defined(DEP2)',
100 'Preprocessor generated incorrectly')
101 self.assertEqual(endif1, '#endif /* DEP2 */',
102 'Preprocessor generated incorrectly')
103 self.assertEqual(endif2, '#endif /* !DEP1 */',
104 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100105
Azim Khanb31aa442018-07-03 11:57:54 +0100106 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100107 """
Azim Khanb31aa442018-07-03 11:57:54 +0100108 Test that gen_dep() correctly creates dependencies for given
109 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100110 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100111 """
Azim Khanb31aa442018-07-03 11:57:54 +0100112 dependencies = []
113 dep_start, dep_end = gen_dependencies(dependencies)
114 self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly')
115 self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100116
Azim Khanb31aa442018-07-03 11:57:54 +0100117 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100118 """
Azim Khanb31aa442018-07-03 11:57:54 +0100119 Test that gen_dep() correctly creates dependencies for given
120 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100121 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100122 """
Azim Khanb31aa442018-07-03 11:57:54 +0100123 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100124 count = 10
125 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100126 dependencies.append('DEP%d' % i)
127 dep_start, dep_end = gen_dependencies(dependencies)
128 self.assertEqual(len(dep_start.splitlines()), count,
129 'Preprocessor generated incorrectly')
130 self.assertEqual(len(dep_end.splitlines()), count,
131 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100132
133
134class GenDepOneLine(TestCase):
135 """
Azim Khanb31aa442018-07-03 11:57:54 +0100136 Test Suite for testing gen_dependencies_one_line()
Azim Khan4b543232017-06-30 09:35:21 +0100137 """
138
Azim Khanb31aa442018-07-03 11:57:54 +0100139 def test_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100140 """
Azim Khanb31aa442018-07-03 11:57:54 +0100141 Test that gen_dep() correctly creates dependencies for given
142 dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100143 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100144 """
Azim Khanb31aa442018-07-03 11:57:54 +0100145 dependencies = ['DEP1', 'DEP2']
146 dep_str = gen_dependencies_one_line(dependencies)
147 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)',
148 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100149
Azim Khanb31aa442018-07-03 11:57:54 +0100150 def test_disabled_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100151 """
Azim Khanb31aa442018-07-03 11:57:54 +0100152 Test that gen_dep() correctly creates dependencies for given
153 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100154 :return:
155 """
Azim Khanb31aa442018-07-03 11:57:54 +0100156 dependencies = ['!DEP1', '!DEP2']
157 dep_str = gen_dependencies_one_line(dependencies)
158 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)',
159 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100160
Azim Khanb31aa442018-07-03 11:57:54 +0100161 def test_mixed_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100162 """
Azim Khanb31aa442018-07-03 11:57:54 +0100163 Test that gen_dep() correctly creates dependencies for given
164 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100165 :return:
166 """
Azim Khanb31aa442018-07-03 11:57:54 +0100167 dependencies = ['!DEP1', 'DEP2']
168 dep_str = gen_dependencies_one_line(dependencies)
169 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)',
170 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100171
Azim Khanb31aa442018-07-03 11:57:54 +0100172 def test_empty_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100173 """
Azim Khanb31aa442018-07-03 11:57:54 +0100174 Test that gen_dep() correctly creates dependencies for given
175 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100176 :return:
177 """
Azim Khanb31aa442018-07-03 11:57:54 +0100178 dependencies = []
179 dep_str = gen_dependencies_one_line(dependencies)
180 self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100181
Azim Khanb31aa442018-07-03 11:57:54 +0100182 def test_large_dependencies_list(self):
Azim Khan4b543232017-06-30 09:35:21 +0100183 """
Azim Khanb31aa442018-07-03 11:57:54 +0100184 Test that gen_dep() correctly creates dependencies for given
185 dependency list.
Azim Khan4b543232017-06-30 09:35:21 +0100186 :return:
187 """
Azim Khanb31aa442018-07-03 11:57:54 +0100188 dependencies = []
Azim Khan4b543232017-06-30 09:35:21 +0100189 count = 10
190 for i in range(count):
Azim Khanb31aa442018-07-03 11:57:54 +0100191 dependencies.append('DEP%d' % i)
192 dep_str = gen_dependencies_one_line(dependencies)
193 expected = '#if ' + ' && '.join(['defined(%s)' %
194 x for x in dependencies])
195 self.assertEqual(dep_str, expected,
196 'Preprocessor generated incorrectly')
Azim Khan4b543232017-06-30 09:35:21 +0100197
198
199class GenFunctionWrapper(TestCase):
200 """
201 Test Suite for testing gen_function_wrapper()
202 """
203
204 def test_params_unpack(self):
205 """
206 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100207
208 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100209 """
210 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
211 expected = '''
212void test_a_wrapper( void ** params )
213{
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100214
Azim Khan4b543232017-06-30 09:35:21 +0100215 test_a( a, b, c, d );
216}
217'''
218 self.assertEqual(code, expected)
219
220 def test_local(self):
221 """
222 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100223
224 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100225 """
Azim Khanb31aa442018-07-03 11:57:54 +0100226 code = gen_function_wrapper('test_a',
227 'int x = 1;', ('x', 'b', 'c', 'd'))
Azim Khan4b543232017-06-30 09:35:21 +0100228 expected = '''
229void test_a_wrapper( void ** params )
230{
Azim Khan4b543232017-06-30 09:35:21 +0100231int x = 1;
232 test_a( x, b, c, d );
233}
234'''
235 self.assertEqual(code, expected)
236
237 def test_empty_params(self):
238 """
239 Test that params are properly unpacked in the function call.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100240
241 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100242 """
243 code = gen_function_wrapper('test_a', '', ())
244 expected = '''
245void test_a_wrapper( void ** params )
246{
247 (void)params;
248
249 test_a( );
250}
251'''
252 self.assertEqual(code, expected)
253
254
255class GenDispatch(TestCase):
256 """
257 Test suite for testing gen_dispatch()
258 """
259
260 def test_dispatch(self):
261 """
262 Test that dispatch table entry is generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100263 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100264 """
265 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
266 expected = '''
267#if defined(DEP1) && defined(DEP2)
268 test_a_wrapper,
269#else
270 NULL,
271#endif
272'''
273 self.assertEqual(code, expected)
274
Azim Khanb31aa442018-07-03 11:57:54 +0100275 def test_empty_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100276 """
277 Test empty dependency list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100278 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100279 """
280 code = gen_dispatch('test_a', [])
281 expected = '''
282 test_a_wrapper,
283'''
284 self.assertEqual(code, expected)
285
286
287class StringIOWrapper(StringIO, object):
288 """
289 file like class to mock file object in tests.
290 """
Azim Khanb31aa442018-07-03 11:57:54 +0100291 def __init__(self, file_name, data, line_no=1):
Azim Khan4b543232017-06-30 09:35:21 +0100292 """
293 Init file handle.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100294
295 :param file_name:
Azim Khan4b543232017-06-30 09:35:21 +0100296 :param data:
297 :param line_no:
298 """
299 super(StringIOWrapper, self).__init__(data)
300 self.line_no = line_no
301 self.name = file_name
302
303 def next(self):
304 """
Azim Khanb31aa442018-07-03 11:57:54 +0100305 Iterator method. This method overrides base class's
306 next method and extends the next method to count the line
307 numbers as each line is read.
Azim Khan4b543232017-06-30 09:35:21 +0100308
Azim Khanb31aa442018-07-03 11:57:54 +0100309 :return: Line read from file.
310 """
311 parent = super(StringIOWrapper, self)
312 line = parent.next() # Python 2
313 if line:
314 self.line_no += 1
315 # Convert byte array to string with correct encoding and
316 # strip any whitespaces added in the decoding process.
317 return line.decode(sys.getdefaultencoding()).strip() + "\n"
318 return None
319
320 __next__ = next
321
322 def readline(self, length=0):
Azim Khan4b543232017-06-30 09:35:21 +0100323 """
324 Wrap the base class readline.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100325
Azim Khanb31aa442018-07-03 11:57:54 +0100326 :param length:
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100327 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100328 """
329 line = super(StringIOWrapper, self).readline()
330 if line:
331 self.line_no += 1
332 return line
333
334
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000335class ParseUntilPattern(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100336 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000337 Test Suite for testing parse_until_pattern().
Azim Khan4b543232017-06-30 09:35:21 +0100338 """
339
340 def test_suite_headers(self):
341 """
342 Test that suite headers are parsed correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100343
344 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100345 """
346 data = '''#include "mbedtls/ecp.h"
347
348#define ECP_PF_UNKNOWN -1
349/* END_HEADER */
350'''
351 expected = '''#line 1 "test_suite_ut.function"
352#include "mbedtls/ecp.h"
353
354#define ECP_PF_UNKNOWN -1
355'''
Azim Khanb31aa442018-07-03 11:57:54 +0100356 stream = StringIOWrapper('test_suite_ut.function', data, line_no=0)
357 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100358 self.assertEqual(headers, expected)
359
360 def test_line_no(self):
361 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100362 Test that #line is set to correct line no. in source .function file.
363
364 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100365 """
366 data = '''#include "mbedtls/ecp.h"
367
368#define ECP_PF_UNKNOWN -1
369/* END_HEADER */
370'''
371 offset_line_no = 5
372 expected = '''#line %d "test_suite_ut.function"
373#include "mbedtls/ecp.h"
374
375#define ECP_PF_UNKNOWN -1
376''' % (offset_line_no + 1)
Azim Khanb31aa442018-07-03 11:57:54 +0100377 stream = StringIOWrapper('test_suite_ut.function', data,
378 offset_line_no)
379 headers = parse_until_pattern(stream, END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100380 self.assertEqual(headers, expected)
381
382 def test_no_end_header_comment(self):
383 """
Azim Khanb31aa442018-07-03 11:57:54 +0100384 Test that InvalidFileFormat is raised when end header comment is
385 missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100386 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100387 """
388 data = '''#include "mbedtls/ecp.h"
389
390#define ECP_PF_UNKNOWN -1
391
392'''
Azim Khanb31aa442018-07-03 11:57:54 +0100393 stream = StringIOWrapper('test_suite_ut.function', data)
394 self.assertRaises(GeneratorInputError, parse_until_pattern, stream,
395 END_HEADER_REGEX)
Azim Khan4b543232017-06-30 09:35:21 +0100396
397
Azim Khanb31aa442018-07-03 11:57:54 +0100398class ParseSuiteDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100399 """
Azim Khanb31aa442018-07-03 11:57:54 +0100400 Test Suite for testing parse_suite_dependencies().
Azim Khan4b543232017-06-30 09:35:21 +0100401 """
402
Azim Khanb31aa442018-07-03 11:57:54 +0100403 def test_suite_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100404 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100405
406 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100407 """
408 data = '''
409 * depends_on:MBEDTLS_ECP_C
410 * END_DEPENDENCIES
411 */
412'''
413 expected = ['MBEDTLS_ECP_C']
Azim Khanb31aa442018-07-03 11:57:54 +0100414 stream = StringIOWrapper('test_suite_ut.function', data)
415 dependencies = parse_suite_dependencies(stream)
416 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100417
418 def test_no_end_dep_comment(self):
419 """
420 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100421 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100422 """
423 data = '''
424* depends_on:MBEDTLS_ECP_C
425'''
Azim Khanb31aa442018-07-03 11:57:54 +0100426 stream = StringIOWrapper('test_suite_ut.function', data)
427 self.assertRaises(GeneratorInputError, parse_suite_dependencies,
428 stream)
Azim Khan4b543232017-06-30 09:35:21 +0100429
Azim Khanb31aa442018-07-03 11:57:54 +0100430 def test_dependencies_split(self):
Azim Khan4b543232017-06-30 09:35:21 +0100431 """
432 Test that InvalidFileFormat is raised when end dep comment is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100433 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100434 """
435 data = '''
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100436 * depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
Azim Khan4b543232017-06-30 09:35:21 +0100437 * END_DEPENDENCIES
438 */
439'''
440 expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
Azim Khanb31aa442018-07-03 11:57:54 +0100441 stream = StringIOWrapper('test_suite_ut.function', data)
442 dependencies = parse_suite_dependencies(stream)
443 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100444
445
Azim Khanb31aa442018-07-03 11:57:54 +0100446class ParseFuncDependencies(TestCase):
Azim Khan4b543232017-06-30 09:35:21 +0100447 """
Azim Khanb31aa442018-07-03 11:57:54 +0100448 Test Suite for testing parse_function_dependencies()
Azim Khan4b543232017-06-30 09:35:21 +0100449 """
450
Azim Khanb31aa442018-07-03 11:57:54 +0100451 def test_function_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100452 """
Azim Khanb31aa442018-07-03 11:57:54 +0100453 Test that parse_function_dependencies() correctly parses function
454 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100455 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100456 """
Azim Khanb31aa442018-07-03 11:57:54 +0100457 line = '/* BEGIN_CASE ' \
458 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
Azim Khan4b543232017-06-30 09:35:21 +0100459 expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
Azim Khanb31aa442018-07-03 11:57:54 +0100460 dependencies = parse_function_dependencies(line)
461 self.assertEqual(dependencies, expected)
Azim Khan4b543232017-06-30 09:35:21 +0100462
Azim Khanb31aa442018-07-03 11:57:54 +0100463 def test_no_dependencies(self):
Azim Khan4b543232017-06-30 09:35:21 +0100464 """
Azim Khanb31aa442018-07-03 11:57:54 +0100465 Test that parse_function_dependencies() correctly parses function
466 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100467 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100468 """
469 line = '/* BEGIN_CASE */'
Azim Khanb31aa442018-07-03 11:57:54 +0100470 dependencies = parse_function_dependencies(line)
471 self.assertEqual(dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100472
Azim Khanb31aa442018-07-03 11:57:54 +0100473 def test_tolerance(self):
Azim Khan4b543232017-06-30 09:35:21 +0100474 """
Azim Khanb31aa442018-07-03 11:57:54 +0100475 Test that parse_function_dependencies() correctly parses function
476 dependencies.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100477 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100478 """
479 line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
Azim Khanb31aa442018-07-03 11:57:54 +0100480 dependencies = parse_function_dependencies(line)
481 self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
Azim Khan4b543232017-06-30 09:35:21 +0100482
483
484class ParseFuncSignature(TestCase):
485 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100486 Test Suite for parse_function_signature().
Azim Khan4b543232017-06-30 09:35:21 +0100487 """
488
489 def test_int_and_char_params(self):
490 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100491 Test int and char parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100492 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100493 """
494 line = 'void entropy_threshold( char * a, int b, int result )'
495 name, args, local, arg_dispatch = parse_function_signature(line)
496 self.assertEqual(name, 'entropy_threshold')
497 self.assertEqual(args, ['char*', 'int', 'int'])
498 self.assertEqual(local, '')
Azim Khanb31aa442018-07-03 11:57:54 +0100499 self.assertEqual(arg_dispatch, ['(char *) params[0]',
500 '*( (int *) params[1] )',
501 '*( (int *) params[2] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100502
503 def test_hex_params(self):
504 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100505 Test hex parameters parsing
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100506 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100507 """
Azim Khan5fcca462018-06-29 11:05:32 +0100508 line = 'void entropy_threshold( char * a, data_t * h, int result )'
Azim Khan4b543232017-06-30 09:35:21 +0100509 name, args, local, arg_dispatch = parse_function_signature(line)
510 self.assertEqual(name, 'entropy_threshold')
511 self.assertEqual(args, ['char*', 'hex', 'int'])
Azim Khanb31aa442018-07-03 11:57:54 +0100512 self.assertEqual(local,
513 ' data_t hex1 = {(uint8_t *) params[1], '
514 '*( (uint32_t *) params[2] )};\n')
515 self.assertEqual(arg_dispatch, ['(char *) params[0]',
516 '&hex1',
517 '*( (int *) params[3] )'])
Azim Khan4b543232017-06-30 09:35:21 +0100518
519 def test_non_void_function(self):
520 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100521 Test invalid signature (non void).
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100522 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100523 """
Azim Khan5fcca462018-06-29 11:05:32 +0100524 line = 'int entropy_threshold( char * a, data_t * h, int result )'
Azim Khan4b543232017-06-30 09:35:21 +0100525 self.assertRaises(ValueError, parse_function_signature, line)
526
527 def test_unsupported_arg(self):
528 """
Azim Khan5fcca462018-06-29 11:05:32 +0100529 Test unsupported arguments (not among int, char * and data_t)
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100530 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100531 """
Azim Khan5fcca462018-06-29 11:05:32 +0100532 line = 'int entropy_threshold( char * a, data_t * h, int * result )'
Azim Khan4b543232017-06-30 09:35:21 +0100533 self.assertRaises(ValueError, parse_function_signature, line)
534
535 def test_no_params(self):
536 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100537 Test no parameters.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100538 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100539 """
540 line = 'void entropy_threshold()'
541 name, args, local, arg_dispatch = parse_function_signature(line)
542 self.assertEqual(name, 'entropy_threshold')
543 self.assertEqual(args, [])
544 self.assertEqual(local, '')
545 self.assertEqual(arg_dispatch, [])
546
547
548class ParseFunctionCode(TestCase):
549 """
550 Test suite for testing parse_function_code()
551 """
552
553 def test_no_function(self):
554 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100555 Test no test function found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100556 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100557 """
558 data = '''
559No
560test
561function
562'''
Azim Khanb31aa442018-07-03 11:57:54 +0100563 stream = StringIOWrapper('test_suite_ut.function', data)
564 self.assertRaises(GeneratorInputError, parse_function_code, stream, [],
565 [])
Azim Khan4b543232017-06-30 09:35:21 +0100566
567 def test_no_end_case_comment(self):
568 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100569 Test missing end case.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100570 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100571 """
572 data = '''
573void test_func()
574{
575}
576'''
Azim Khanb31aa442018-07-03 11:57:54 +0100577 stream = StringIOWrapper('test_suite_ut.function', data)
578 self.assertRaises(GeneratorInputError, parse_function_code, stream, [],
579 [])
Azim Khan4b543232017-06-30 09:35:21 +0100580
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000581 @patch("generate_test_code.parse_function_signature")
Azim Khanb31aa442018-07-03 11:57:54 +0100582 def test_function_called(self,
583 parse_function_signature_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100584 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100585 Test parse_function_code()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100586 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100587 """
588 parse_function_signature_mock.return_value = ('test_func', [], '', [])
589 data = '''
590void test_func()
591{
592}
593'''
Azim Khanb31aa442018-07-03 11:57:54 +0100594 stream = StringIOWrapper('test_suite_ut.function', data)
595 self.assertRaises(GeneratorInputError, parse_function_code,
596 stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100597 self.assertTrue(parse_function_signature_mock.called)
598 parse_function_signature_mock.assert_called_with('void test_func()\n')
599
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000600 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100601 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000602 @patch("generate_test_code.gen_function_wrapper")
603 @patch("generate_test_code.parse_function_signature")
Azim Khan4b543232017-06-30 09:35:21 +0100604 def test_return(self, parse_function_signature_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100605 gen_function_wrapper_mock,
606 gen_dependencies_mock,
607 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100608 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100609 Test generated code.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100610 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100611 """
612 parse_function_signature_mock.return_value = ('func', [], '', [])
613 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100614 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100615 gen_dispatch_mock.side_effect = gen_dispatch
616 data = '''
617void func()
618{
619 ba ba black sheep
620 have you any wool
621}
622/* END_CASE */
623'''
Azim Khanb31aa442018-07-03 11:57:54 +0100624 stream = StringIOWrapper('test_suite_ut.function', data)
625 name, arg, code, dispatch_code = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100626
Azim Khan4b543232017-06-30 09:35:21 +0100627 self.assertTrue(parse_function_signature_mock.called)
628 parse_function_signature_mock.assert_called_with('void func()\n')
629 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
630 self.assertEqual(name, 'test_func')
631 self.assertEqual(arg, [])
632 expected = '''#line 2 "test_suite_ut.function"
633void test_func()
634{
635 ba ba black sheep
636 have you any wool
637exit:
638 ;;
639}
640'''
641 self.assertEqual(code, expected)
642 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
643
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000644 @patch("generate_test_code.gen_dispatch")
Azim Khanb31aa442018-07-03 11:57:54 +0100645 @patch("generate_test_code.gen_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000646 @patch("generate_test_code.gen_function_wrapper")
647 @patch("generate_test_code.parse_function_signature")
Azim Khan4b543232017-06-30 09:35:21 +0100648 def test_with_exit_label(self, parse_function_signature_mock,
Azim Khanb31aa442018-07-03 11:57:54 +0100649 gen_function_wrapper_mock,
650 gen_dependencies_mock,
651 gen_dispatch_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100652 """
Azim Khanf0e42fb2017-08-02 14:47:13 +0100653 Test when exit label is present.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100654 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100655 """
656 parse_function_signature_mock.return_value = ('func', [], '', [])
657 gen_function_wrapper_mock.return_value = ''
Azim Khanb31aa442018-07-03 11:57:54 +0100658 gen_dependencies_mock.side_effect = gen_dependencies
Azim Khan4b543232017-06-30 09:35:21 +0100659 gen_dispatch_mock.side_effect = gen_dispatch
660 data = '''
661void func()
662{
663 ba ba black sheep
664 have you any wool
665exit:
666 yes sir yes sir
667 3 bags full
668}
669/* END_CASE */
670'''
Azim Khanb31aa442018-07-03 11:57:54 +0100671 stream = StringIOWrapper('test_suite_ut.function', data)
672 _, _, code, _ = parse_function_code(stream, [], [])
Azim Khan4b543232017-06-30 09:35:21 +0100673
674 expected = '''#line 2 "test_suite_ut.function"
675void test_func()
676{
677 ba ba black sheep
678 have you any wool
679exit:
680 yes sir yes sir
681 3 bags full
682}
683'''
684 self.assertEqual(code, expected)
685
686
687class ParseFunction(TestCase):
688 """
689 Test Suite for testing parse_functions()
690 """
691
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000692 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000693 def test_begin_header(self, parse_until_pattern_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100694 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000695 Test that begin header is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100696 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100697 """
Azim Khanb31aa442018-07-03 11:57:54 +0100698 def stop(*_unused):
699 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100700 raise Exception
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000701 parse_until_pattern_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100702 data = '''/* BEGIN_HEADER */
703#include "mbedtls/ecp.h"
704
705#define ECP_PF_UNKNOWN -1
706/* END_HEADER */
707'''
Azim Khanb31aa442018-07-03 11:57:54 +0100708 stream = StringIOWrapper('test_suite_ut.function', data)
709 self.assertRaises(Exception, parse_functions, stream)
710 parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
711 self.assertEqual(stream.line_no, 2)
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000712
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000713 @patch("generate_test_code.parse_until_pattern")
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000714 def test_begin_helper(self, parse_until_pattern_mock):
715 """
716 Test that begin helper is checked and parse_until_pattern() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100717 :return:
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000718 """
Azim Khanb31aa442018-07-03 11:57:54 +0100719 def stop(*_unused):
720 """Stop when parse_until_pattern is called."""
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000721 raise Exception
722 parse_until_pattern_mock.side_effect = stop
723 data = '''/* BEGIN_SUITE_HELPERS */
Azim Khanb31aa442018-07-03 11:57:54 +0100724void print_hello_world()
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000725{
Azim Khanb31aa442018-07-03 11:57:54 +0100726 printf("Hello World!\n");
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000727}
728/* END_SUITE_HELPERS */
729'''
Azim Khanb31aa442018-07-03 11:57:54 +0100730 stream = StringIOWrapper('test_suite_ut.function', data)
731 self.assertRaises(Exception, parse_functions, stream)
732 parse_until_pattern_mock.assert_called_with(stream,
733 END_SUITE_HELPERS_REGEX)
734 self.assertEqual(stream.line_no, 2)
Azim Khan4b543232017-06-30 09:35:21 +0100735
Azim Khanb31aa442018-07-03 11:57:54 +0100736 @patch("generate_test_code.parse_suite_dependencies")
737 def test_begin_dep(self, parse_suite_dependencies_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100738 """
Azim Khanb31aa442018-07-03 11:57:54 +0100739 Test that begin dep is checked and parse_suite_dependencies() is
740 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100741 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100742 """
Azim Khanb31aa442018-07-03 11:57:54 +0100743 def stop(*_unused):
744 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100745 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100746 parse_suite_dependencies_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100747 data = '''/* BEGIN_DEPENDENCIES
748 * depends_on:MBEDTLS_ECP_C
749 * END_DEPENDENCIES
750 */
751'''
Azim Khanb31aa442018-07-03 11:57:54 +0100752 stream = StringIOWrapper('test_suite_ut.function', data)
753 self.assertRaises(Exception, parse_functions, stream)
754 parse_suite_dependencies_mock.assert_called_with(stream)
755 self.assertEqual(stream.line_no, 2)
Azim Khan4b543232017-06-30 09:35:21 +0100756
Azim Khanb31aa442018-07-03 11:57:54 +0100757 @patch("generate_test_code.parse_function_dependencies")
758 def test_begin_function_dep(self, func_mock):
Azim Khan4b543232017-06-30 09:35:21 +0100759 """
Azim Khanb31aa442018-07-03 11:57:54 +0100760 Test that begin dep is checked and parse_function_dependencies() is
761 called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100762 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100763 """
Azim Khanb31aa442018-07-03 11:57:54 +0100764 def stop(*_unused):
765 """Stop when parse_until_pattern is called."""
Azim Khan4b543232017-06-30 09:35:21 +0100766 raise Exception
Azim Khanb31aa442018-07-03 11:57:54 +0100767 func_mock.side_effect = stop
Azim Khan4b543232017-06-30 09:35:21 +0100768
Azim Khanb31aa442018-07-03 11:57:54 +0100769 dependencies_str = '/* BEGIN_CASE ' \
770 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100771 data = '''%svoid test_func()
772{
773}
Azim Khanb31aa442018-07-03 11:57:54 +0100774''' % dependencies_str
775 stream = StringIOWrapper('test_suite_ut.function', data)
776 self.assertRaises(Exception, parse_functions, stream)
777 func_mock.assert_called_with(dependencies_str)
778 self.assertEqual(stream.line_no, 2)
Azim Khan4b543232017-06-30 09:35:21 +0100779
Mohammad Azim Khan78befd92018-03-06 11:49:41 +0000780 @patch("generate_test_code.parse_function_code")
Azim Khanb31aa442018-07-03 11:57:54 +0100781 @patch("generate_test_code.parse_function_dependencies")
782 def test_return(self, func_mock1, func_mock2):
Azim Khan4b543232017-06-30 09:35:21 +0100783 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000784 Test that begin case is checked and parse_function_code() is called.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100785 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100786 """
Azim Khanb31aa442018-07-03 11:57:54 +0100787 func_mock1.return_value = []
788 in_func_code = '''void test_func()
Azim Khan4b543232017-06-30 09:35:21 +0100789{
790}
791'''
792 func_dispatch = '''
793 test_func_wrapper,
794'''
Azim Khanb31aa442018-07-03 11:57:54 +0100795 func_mock2.return_value = 'test_func', [],\
796 in_func_code, func_dispatch
797 dependencies_str = '/* BEGIN_CASE ' \
798 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
Azim Khan4b543232017-06-30 09:35:21 +0100799 data = '''%svoid test_func()
800{
801}
Azim Khanb31aa442018-07-03 11:57:54 +0100802''' % dependencies_str
803 stream = StringIOWrapper('test_suite_ut.function', data)
804 suite_dependencies, dispatch_code, func_code, func_info = \
805 parse_functions(stream)
806 func_mock1.assert_called_with(dependencies_str)
807 func_mock2.assert_called_with(stream, [], [])
808 self.assertEqual(stream.line_no, 5)
809 self.assertEqual(suite_dependencies, [])
Azim Khan4b543232017-06-30 09:35:21 +0100810 expected_dispatch_code = '''/* Function Id: 0 */
811
812 test_func_wrapper,
813'''
814 self.assertEqual(dispatch_code, expected_dispatch_code)
815 self.assertEqual(func_code, in_func_code)
816 self.assertEqual(func_info, {'test_func': (0, [])})
817
818 def test_parsing(self):
819 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000820 Test case parsing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100821 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100822 """
823 data = '''/* BEGIN_HEADER */
824#include "mbedtls/ecp.h"
825
826#define ECP_PF_UNKNOWN -1
827/* END_HEADER */
828
829/* BEGIN_DEPENDENCIES
830 * depends_on:MBEDTLS_ECP_C
831 * END_DEPENDENCIES
832 */
833
834/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
835void func1()
836{
837}
838/* END_CASE */
839
840/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
841void func2()
842{
843}
844/* END_CASE */
845'''
Azim Khanb31aa442018-07-03 11:57:54 +0100846 stream = StringIOWrapper('test_suite_ut.function', data)
847 suite_dependencies, dispatch_code, func_code, func_info = \
848 parse_functions(stream)
849 self.assertEqual(stream.line_no, 23)
850 self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C'])
Azim Khan4b543232017-06-30 09:35:21 +0100851
852 expected_dispatch_code = '''/* Function Id: 0 */
853
854#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
855 test_func1_wrapper,
856#else
857 NULL,
858#endif
859/* Function Id: 1 */
860
861#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
862 test_func2_wrapper,
863#else
864 NULL,
865#endif
866'''
867 self.assertEqual(dispatch_code, expected_dispatch_code)
868 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
869#line 3 "test_suite_ut.function"
870#include "mbedtls/ecp.h"
871
872#define ECP_PF_UNKNOWN -1
873#if defined(MBEDTLS_ENTROPY_NV_SEED)
874#if defined(MBEDTLS_FS_IO)
875#line 14 "test_suite_ut.function"
876void test_func1()
877{
878exit:
879 ;;
880}
881
882void test_func1_wrapper( void ** params )
883{
884 (void)params;
885
886 test_func1( );
887}
888#endif /* MBEDTLS_FS_IO */
889#endif /* MBEDTLS_ENTROPY_NV_SEED */
890#if defined(MBEDTLS_ENTROPY_NV_SEED)
891#if defined(MBEDTLS_FS_IO)
892#line 20 "test_suite_ut.function"
893void test_func2()
894{
895exit:
896 ;;
897}
898
899void test_func2_wrapper( void ** params )
900{
901 (void)params;
902
903 test_func2( );
904}
905#endif /* MBEDTLS_FS_IO */
906#endif /* MBEDTLS_ENTROPY_NV_SEED */
907#endif /* MBEDTLS_ECP_C */
908'''
909 self.assertEqual(func_code, expected_func_code)
Azim Khanb31aa442018-07-03 11:57:54 +0100910 self.assertEqual(func_info, {'test_func1': (0, []),
911 'test_func2': (1, [])})
Azim Khan4b543232017-06-30 09:35:21 +0100912
913 def test_same_function_name(self):
914 """
Mohammad Azim Khanb5229292018-02-06 13:08:01 +0000915 Test name conflict.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100916 :return:
Azim Khan4b543232017-06-30 09:35:21 +0100917 """
918 data = '''/* BEGIN_HEADER */
919#include "mbedtls/ecp.h"
920
921#define ECP_PF_UNKNOWN -1
922/* END_HEADER */
923
924/* BEGIN_DEPENDENCIES
925 * depends_on:MBEDTLS_ECP_C
926 * END_DEPENDENCIES
927 */
928
929/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
930void func()
931{
932}
933/* END_CASE */
934
935/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
936void func()
937{
938}
939/* END_CASE */
940'''
Azim Khanb31aa442018-07-03 11:57:54 +0100941 stream = StringIOWrapper('test_suite_ut.function', data)
942 self.assertRaises(GeneratorInputError, parse_functions, stream)
Azim Khan4b543232017-06-30 09:35:21 +0100943
944
Azim Khanb31aa442018-07-03 11:57:54 +0100945class EscapedSplit(TestCase):
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100946 """
Azim Khan599cd242017-07-06 17:34:27 +0100947 Test suite for testing escaped_split().
Azim Khanb31aa442018-07-03 11:57:54 +0100948 Note: Since escaped_split() output is used to write back to the
949 intermediate data file. Any escape characters in the input are
950 retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100951 """
952
953 def test_invalid_input(self):
954 """
955 Test when input split character is not a character.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100956 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100957 """
958 self.assertRaises(ValueError, escaped_split, '', 'string')
959
960 def test_empty_string(self):
961 """
Azim Khanb31aa442018-07-03 11:57:54 +0100962 Test empty string input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100963 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100964 """
965 splits = escaped_split('', ':')
966 self.assertEqual(splits, [])
967
968 def test_no_escape(self):
969 """
Azim Khanb31aa442018-07-03 11:57:54 +0100970 Test with no escape character. The behaviour should be same as
971 str.split()
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100972 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100973 """
Azim Khanb31aa442018-07-03 11:57:54 +0100974 test_str = 'yahoo:google'
975 splits = escaped_split(test_str, ':')
976 self.assertEqual(splits, test_str.split(':'))
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100977
978 def test_escaped_input(self):
979 """
Azim Khanb31aa442018-07-03 11:57:54 +0100980 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100981 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100982 """
Azim Khanb31aa442018-07-03 11:57:54 +0100983 test_str = r'yahoo\:google:facebook'
984 splits = escaped_split(test_str, ':')
985 self.assertEqual(splits, [r'yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100986
987 def test_escaped_escape(self):
988 """
Azim Khanb31aa442018-07-03 11:57:54 +0100989 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100990 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100991 """
Azim Khanb31aa442018-07-03 11:57:54 +0100992 test_str = r'yahoo\\\:google:facebook'
993 splits = escaped_split(test_str, ':')
994 self.assertEqual(splits, [r'yahoo\\\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100995
996 def test_all_at_once(self):
997 """
Azim Khanb31aa442018-07-03 11:57:54 +0100998 Test input that has escaped delimiter.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +0100999 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001000 """
Azim Khanb31aa442018-07-03 11:57:54 +01001001 test_str = r'yahoo\\\:google:facebook\:instagram\\\:bbc\\\\:wikipedia'
1002 splits = escaped_split(test_str, ':')
1003 self.assertEqual(splits, [r'yahoo\\\\', r'google',
1004 r'facebook\:instagram\\\\',
1005 r'bbc\\\\', r'wikipedia'])
Azim Khan599cd242017-07-06 17:34:27 +01001006
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001007
1008class ParseTestData(TestCase):
1009 """
1010 Test suite for parse test data.
1011 """
1012
1013 def test_parser(self):
1014 """
1015 Test that tests are parsed correctly from data file.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001016 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001017 """
1018 data = """
1019Diffie-Hellman full exchange #1
1020dhm_do_dhm:10:"23":10:"5"
1021
1022Diffie-Hellman full exchange #2
1023dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1024
1025Diffie-Hellman full exchange #3
1026dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
1027
1028Diffie-Hellman selftest
1029dhm_selftest:
1030"""
Azim Khanb31aa442018-07-03 11:57:54 +01001031 stream = StringIOWrapper('test_suite_ut.function', data)
1032 tests = [(name, test_function, dependencies, args)
1033 for name, test_function, dependencies, args in
1034 parse_test_data(stream)]
1035 test1, test2, test3, test4 = tests
1036 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1037 self.assertEqual(test1[1], 'dhm_do_dhm')
1038 self.assertEqual(test1[2], [])
1039 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001040
Azim Khanb31aa442018-07-03 11:57:54 +01001041 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1042 self.assertEqual(test2[1], 'dhm_do_dhm')
1043 self.assertEqual(test2[2], [])
1044 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1045 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001046
Azim Khanb31aa442018-07-03 11:57:54 +01001047 self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3')
1048 self.assertEqual(test3[1], 'dhm_do_dhm')
1049 self.assertEqual(test3[2], [])
1050 self.assertEqual(test3[3], ['10',
1051 '"9345098382739712938719287391879381271"',
1052 '10',
1053 '"9345098792137312973297123912791271"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001054
Azim Khanb31aa442018-07-03 11:57:54 +01001055 self.assertEqual(test4[0], 'Diffie-Hellman selftest')
1056 self.assertEqual(test4[1], 'dhm_selftest')
1057 self.assertEqual(test4[2], [])
1058 self.assertEqual(test4[3], [])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001059
1060 def test_with_dependencies(self):
1061 """
1062 Test that tests with dependencies are parsed.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001063 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001064 """
1065 data = """
1066Diffie-Hellman full exchange #1
1067depends_on:YAHOO
1068dhm_do_dhm:10:"23":10:"5"
1069
1070Diffie-Hellman full exchange #2
1071dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1072
1073"""
Azim Khanb31aa442018-07-03 11:57:54 +01001074 stream = StringIOWrapper('test_suite_ut.function', data)
1075 tests = [(name, function_name, dependencies, args)
1076 for name, function_name, dependencies, args in
1077 parse_test_data(stream)]
1078 test1, test2 = tests
1079 self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1')
1080 self.assertEqual(test1[1], 'dhm_do_dhm')
1081 self.assertEqual(test1[2], ['YAHOO'])
1082 self.assertEqual(test1[3], ['10', '"23"', '10', '"5"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001083
Azim Khanb31aa442018-07-03 11:57:54 +01001084 self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2')
1085 self.assertEqual(test2[1], 'dhm_do_dhm')
1086 self.assertEqual(test2[2], [])
1087 self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"',
1088 '10', '"9345098304850938450983409622"'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001089
1090 def test_no_args(self):
1091 """
Azim Khanb31aa442018-07-03 11:57:54 +01001092 Test GeneratorInputError is raised when test function name and
1093 args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001094 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001095 """
1096 data = """
1097Diffie-Hellman full exchange #1
1098depends_on:YAHOO
1099
1100
1101Diffie-Hellman full exchange #2
1102dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
1103
1104"""
Azim Khanb31aa442018-07-03 11:57:54 +01001105 stream = StringIOWrapper('test_suite_ut.function', data)
1106 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001107 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001108 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001109 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001110 except GeneratorInputError as err:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001111 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001112 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001113
1114 def test_incomplete_data(self):
1115 """
Azim Khanb31aa442018-07-03 11:57:54 +01001116 Test GeneratorInputError is raised when test function name
1117 and args line is missing.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001118 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001119 """
1120 data = """
1121Diffie-Hellman full exchange #1
1122depends_on:YAHOO
1123"""
Azim Khanb31aa442018-07-03 11:57:54 +01001124 stream = StringIOWrapper('test_suite_ut.function', data)
1125 err = None
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001126 try:
Azim Khanb31aa442018-07-03 11:57:54 +01001127 for _, _, _, _ in parse_test_data(stream):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001128 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001129 except GeneratorInputError as err:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001130 pass
Azim Khanb31aa442018-07-03 11:57:54 +01001131 self.assertEqual(type(err), GeneratorInputError)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001132
1133
1134class GenDepCheck(TestCase):
1135 """
Azim Khanb31aa442018-07-03 11:57:54 +01001136 Test suite for gen_dep_check(). It is assumed this function is
1137 called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001138 """
1139
1140 def test_gen_dep_check(self):
1141 """
1142 Test that dependency check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001143 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001144 """
1145 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001146 case 5:
1147 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001148#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001149 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001150#else
Azim Khand61b8372017-07-10 11:54:01 +01001151 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001152#endif
Azim Khand61b8372017-07-10 11:54:01 +01001153 }
1154 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001155 out = gen_dep_check(5, 'YAHOO')
1156 self.assertEqual(out, expected)
1157
Azim Khanb31aa442018-07-03 11:57:54 +01001158 def test_not_defined_dependency(self):
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001159 """
1160 Test dependency with !.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001161 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001162 """
1163 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001164 case 5:
1165 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001166#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001167 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001168#else
Azim Khand61b8372017-07-10 11:54:01 +01001169 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001170#endif
Azim Khand61b8372017-07-10 11:54:01 +01001171 }
1172 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001173 out = gen_dep_check(5, '!YAHOO')
1174 self.assertEqual(out, expected)
1175
1176 def test_empty_dependency(self):
1177 """
1178 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001179 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001180 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001181 self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001182
1183 def test_negative_dep_id(self):
1184 """
1185 Test invalid dependency input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001186 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001187 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001188 self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001189
1190
1191class GenExpCheck(TestCase):
1192 """
Azim Khanb31aa442018-07-03 11:57:54 +01001193 Test suite for gen_expression_check(). It is assumed this function
1194 is called with valid inputs.
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001195 """
1196
1197 def test_gen_exp_check(self):
1198 """
1199 Test that expression check code generated correctly.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001200 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001201 """
1202 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001203 case 5:
1204 {
1205 *out_value = YAHOO;
1206 }
1207 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001208 out = gen_expression_check(5, 'YAHOO')
1209 self.assertEqual(out, expected)
1210
1211 def test_invalid_expression(self):
1212 """
1213 Test invalid expression input.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001214 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001215 """
Mohammad Azim Khan3b06f222018-06-26 14:35:25 +01001216 self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001217
1218 def test_negative_exp_id(self):
1219 """
1220 Test invalid expression id.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001221 :return:
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001222 """
Azim Khanb31aa442018-07-03 11:57:54 +01001223 self.assertRaises(GeneratorInputError, gen_expression_check,
1224 -1, 'YAHOO')
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001225
1226
Azim Khanb31aa442018-07-03 11:57:54 +01001227class WriteDependencies(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001228 """
Azim Khanb31aa442018-07-03 11:57:54 +01001229 Test suite for testing write_dependencies.
Azim Khan599cd242017-07-06 17:34:27 +01001230 """
1231
Azim Khanb31aa442018-07-03 11:57:54 +01001232 def test_no_test_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001233 """
Azim Khanb31aa442018-07-03 11:57:54 +01001234 Test when test dependencies input is empty.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001235 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001236 """
Azim Khanb31aa442018-07-03 11:57:54 +01001237 stream = StringIOWrapper('test_suite_ut.data', '')
1238 unique_dependencies = []
1239 dep_check_code = write_dependencies(stream, [], unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001240 self.assertEqual(dep_check_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001241 self.assertEqual(len(unique_dependencies), 0)
1242 self.assertEqual(stream.getvalue(), '')
Azim Khan599cd242017-07-06 17:34:27 +01001243
1244 def test_unique_dep_ids(self):
1245 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001246
1247 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001248 """
Azim Khanb31aa442018-07-03 11:57:54 +01001249 stream = StringIOWrapper('test_suite_ut.data', '')
1250 unique_dependencies = []
1251 dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'],
1252 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001253 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001254 case 0:
1255 {
Azim Khan599cd242017-07-06 17:34:27 +01001256#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001257 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001258#else
Azim Khand61b8372017-07-10 11:54:01 +01001259 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001260#endif
Azim Khand61b8372017-07-10 11:54:01 +01001261 }
1262 break;
1263 case 1:
1264 {
Azim Khan599cd242017-07-06 17:34:27 +01001265#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001266 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001267#else
Azim Khand61b8372017-07-10 11:54:01 +01001268 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001269#endif
Azim Khand61b8372017-07-10 11:54:01 +01001270 }
1271 break;
1272 case 2:
1273 {
Azim Khan599cd242017-07-06 17:34:27 +01001274#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001275 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001276#else
Azim Khand61b8372017-07-10 11:54:01 +01001277 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001278#endif
Azim Khand61b8372017-07-10 11:54:01 +01001279 }
1280 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001281 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001282 self.assertEqual(len(unique_dependencies), 3)
1283 self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001284
1285 def test_dep_id_repeat(self):
1286 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001287
1288 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001289 """
Azim Khanb31aa442018-07-03 11:57:54 +01001290 stream = StringIOWrapper('test_suite_ut.data', '')
1291 unique_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001292 dep_check_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001293 dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'],
1294 unique_dependencies)
1295 dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'],
1296 unique_dependencies)
1297 dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'],
1298 unique_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001299 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001300 case 0:
1301 {
Azim Khan599cd242017-07-06 17:34:27 +01001302#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001303 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001304#else
Azim Khand61b8372017-07-10 11:54:01 +01001305 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001306#endif
Azim Khand61b8372017-07-10 11:54:01 +01001307 }
1308 break;
1309 case 1:
1310 {
Azim Khan599cd242017-07-06 17:34:27 +01001311#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001312 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001313#else
Azim Khand61b8372017-07-10 11:54:01 +01001314 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001315#endif
Azim Khand61b8372017-07-10 11:54:01 +01001316 }
1317 break;
1318 case 2:
1319 {
Azim Khan599cd242017-07-06 17:34:27 +01001320#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001321 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001322#else
Azim Khand61b8372017-07-10 11:54:01 +01001323 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001324#endif
Azim Khand61b8372017-07-10 11:54:01 +01001325 }
1326 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001327 self.assertEqual(dep_check_code, expect_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001328 self.assertEqual(len(unique_dependencies), 3)
1329 self.assertEqual(stream.getvalue(),
1330 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001331
1332
1333class WriteParams(TestCase):
1334 """
1335 Test Suite for testing write_parameters().
1336 """
1337
1338 def test_no_params(self):
1339 """
1340 Test with empty test_args
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001341 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001342 """
Azim Khanb31aa442018-07-03 11:57:54 +01001343 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001344 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001345 expression_code = write_parameters(stream, [], [], unique_expressions)
Azim Khan599cd242017-07-06 17:34:27 +01001346 self.assertEqual(len(unique_expressions), 0)
1347 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001348 self.assertEqual(stream.getvalue(), '\n')
Azim Khan599cd242017-07-06 17:34:27 +01001349
1350 def test_no_exp_param(self):
1351 """
1352 Test when there is no macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001353 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001354 """
Azim Khanb31aa442018-07-03 11:57:54 +01001355 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001356 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001357 expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"',
1358 '0'],
1359 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001360 unique_expressions)
1361 self.assertEqual(len(unique_expressions), 0)
1362 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001363 self.assertEqual(stream.getvalue(),
1364 ':char*:"Yahoo":hex:"abcdef00":int:0\n')
Azim Khan599cd242017-07-06 17:34:27 +01001365
1366 def test_hex_format_int_param(self):
1367 """
1368 Test int parameter in hex format.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001369 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001370 """
Azim Khanb31aa442018-07-03 11:57:54 +01001371 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001372 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001373 expression_code = write_parameters(stream,
1374 ['"Yahoo"', '"abcdef00"', '0xAA'],
1375 ['char*', 'hex', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001376 unique_expressions)
1377 self.assertEqual(len(unique_expressions), 0)
1378 self.assertEqual(expression_code, '')
Azim Khanb31aa442018-07-03 11:57:54 +01001379 self.assertEqual(stream.getvalue(),
1380 ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
Azim Khan599cd242017-07-06 17:34:27 +01001381
1382 def test_with_exp_param(self):
1383 """
1384 Test when there is macro or expression in the params.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001385 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001386 """
Azim Khanb31aa442018-07-03 11:57:54 +01001387 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001388 unique_expressions = []
Azim Khanb31aa442018-07-03 11:57:54 +01001389 expression_code = write_parameters(stream,
1390 ['"Yahoo"', '"abcdef00"', '0',
1391 'MACRO1', 'MACRO2', 'MACRO3'],
1392 ['char*', 'hex', 'int',
1393 'int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001394 unique_expressions)
1395 self.assertEqual(len(unique_expressions), 3)
1396 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1397 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001398 case 0:
1399 {
1400 *out_value = MACRO1;
1401 }
1402 break;
1403 case 1:
1404 {
1405 *out_value = MACRO2;
1406 }
1407 break;
1408 case 2:
1409 {
1410 *out_value = MACRO3;
1411 }
1412 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001413 self.assertEqual(expression_code, expected_expression_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001414 self.assertEqual(stream.getvalue(),
1415 ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1'
1416 ':exp:2\n')
Azim Khan599cd242017-07-06 17:34:27 +01001417
Azim Khanb31aa442018-07-03 11:57:54 +01001418 def test_with_repeat_calls(self):
Azim Khan599cd242017-07-06 17:34:27 +01001419 """
1420 Test when write_parameter() is called with same macro or expression.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001421 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001422 """
Azim Khanb31aa442018-07-03 11:57:54 +01001423 stream = StringIOWrapper('test_suite_ut.data', '')
Azim Khan599cd242017-07-06 17:34:27 +01001424 unique_expressions = []
1425 expression_code = ''
Azim Khanb31aa442018-07-03 11:57:54 +01001426 expression_code += write_parameters(stream,
1427 ['"Yahoo"', 'MACRO1', 'MACRO2'],
1428 ['char*', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001429 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001430 expression_code += write_parameters(stream,
1431 ['"abcdef00"', 'MACRO2', 'MACRO3'],
1432 ['hex', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001433 unique_expressions)
Azim Khanb31aa442018-07-03 11:57:54 +01001434 expression_code += write_parameters(stream,
1435 ['0', 'MACRO3', 'MACRO1'],
1436 ['int', 'int', 'int'],
Azim Khan599cd242017-07-06 17:34:27 +01001437 unique_expressions)
1438 self.assertEqual(len(unique_expressions), 3)
1439 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1440 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001441 case 0:
1442 {
1443 *out_value = MACRO1;
1444 }
1445 break;
1446 case 1:
1447 {
1448 *out_value = MACRO2;
1449 }
1450 break;
1451 case 2:
1452 {
1453 *out_value = MACRO3;
1454 }
1455 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001456 self.assertEqual(expression_code, expected_expression_code)
1457 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1458:hex:"abcdef00":exp:1:exp:2
1459:int:0:exp:2:exp:0
1460'''
Azim Khanb31aa442018-07-03 11:57:54 +01001461 self.assertEqual(stream.getvalue(), expected_data_file)
Azim Khan599cd242017-07-06 17:34:27 +01001462
1463
Azim Khanb31aa442018-07-03 11:57:54 +01001464class GenTestSuiteDependenciesChecks(TestCase):
Azim Khan599cd242017-07-06 17:34:27 +01001465 """
Azim Khanb31aa442018-07-03 11:57:54 +01001466 Test suite for testing gen_suite_dep_checks()
Azim Khan599cd242017-07-06 17:34:27 +01001467 """
Azim Khanb31aa442018-07-03 11:57:54 +01001468 def test_empty_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001469 """
Azim Khanb31aa442018-07-03 11:57:54 +01001470 Test with empty suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001471
1472 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001473 """
Azim Khanb31aa442018-07-03 11:57:54 +01001474 dep_check_code, expression_code = \
1475 gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
Azim Khan599cd242017-07-06 17:34:27 +01001476 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1477 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1478
Azim Khanb31aa442018-07-03 11:57:54 +01001479 def test_suite_dependencies(self):
Azim Khan599cd242017-07-06 17:34:27 +01001480 """
Azim Khanb31aa442018-07-03 11:57:54 +01001481 Test with suite_dependencies list.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001482
1483 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001484 """
Azim Khanb31aa442018-07-03 11:57:54 +01001485 dep_check_code, expression_code = \
1486 gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE',
1487 'EXPRESSION_CODE')
1488 expected_dep_check_code = '''
Azim Khan599cd242017-07-06 17:34:27 +01001489#if defined(SUITE_DEP)
1490DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001491#endif
1492'''
1493 expected_expression_code = '''
1494#if defined(SUITE_DEP)
1495EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001496#endif
1497'''
Azim Khanb31aa442018-07-03 11:57:54 +01001498 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khan599cd242017-07-06 17:34:27 +01001499 self.assertEqual(expression_code, expected_expression_code)
1500
1501 def test_no_dep_no_exp(self):
1502 """
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001503 Test when there are no dependency and expression code.
1504 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001505 """
Azim Khanb31aa442018-07-03 11:57:54 +01001506 dep_check_code, expression_code = gen_suite_dep_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001507 self.assertEqual(dep_check_code, '')
1508 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001509
1510
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001511class GenFromTestData(TestCase):
1512 """
1513 Test suite for gen_from_test_data()
1514 """
1515
Azim Khanb31aa442018-07-03 11:57:54 +01001516 @staticmethod
1517 @patch("generate_test_code.write_dependencies")
Mohammad Azim Khan78befd92018-03-06 11:49:41 +00001518 @patch("generate_test_code.write_parameters")
Azim Khanb31aa442018-07-03 11:57:54 +01001519 @patch("generate_test_code.gen_suite_dependencies_checks")
1520 def test_intermediate_data_file(func_mock1,
1521 write_parameters_mock,
1522 write_dependencies_mock):
Azim Khan599cd242017-07-06 17:34:27 +01001523 """
1524 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001525 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001526 """
1527 data = '''
1528My test
1529depends_on:DEP1
1530func1:0
1531'''
1532 data_f = StringIOWrapper('test_suite_ut.data', data)
1533 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1534 func_info = {'test_func1': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001535 suite_dependencies = []
Azim Khan599cd242017-07-06 17:34:27 +01001536 write_parameters_mock.side_effect = write_parameters
Azim Khanb31aa442018-07-03 11:57:54 +01001537 write_dependencies_mock.side_effect = write_dependencies
1538 func_mock1.side_effect = gen_suite_dep_checks
1539 gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies)
1540 write_dependencies_mock.assert_called_with(out_data_f,
1541 ['DEP1'], ['DEP1'])
1542 write_parameters_mock.assert_called_with(out_data_f, ['0'],
1543 ('int',), [])
Azim Khan599cd242017-07-06 17:34:27 +01001544 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001545 case 0:
1546 {
Azim Khan599cd242017-07-06 17:34:27 +01001547#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001548 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001549#else
Azim Khand61b8372017-07-10 11:54:01 +01001550 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001551#endif
Azim Khand61b8372017-07-10 11:54:01 +01001552 }
1553 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001554 func_mock1.assert_called_with(
1555 suite_dependencies, expected_dep_check_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001556
1557 def test_function_not_found(self):
1558 """
1559 Test that AssertError is raised when function info in not found.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001560 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001561 """
1562 data = '''
1563My test
1564depends_on:DEP1
1565func1:0
1566'''
1567 data_f = StringIOWrapper('test_suite_ut.data', data)
1568 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1569 func_info = {'test_func2': (1, ('int',))}
Azim Khanb31aa442018-07-03 11:57:54 +01001570 suite_dependencies = []
1571 self.assertRaises(GeneratorInputError, gen_from_test_data,
1572 data_f, out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001573
1574 def test_different_func_args(self):
1575 """
Azim Khanb31aa442018-07-03 11:57:54 +01001576 Test that AssertError is raised when no. of parameters and
1577 function args differ.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001578 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001579 """
1580 data = '''
1581My test
1582depends_on:DEP1
1583func1:0
1584'''
1585 data_f = StringIOWrapper('test_suite_ut.data', data)
1586 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001587 func_info = {'test_func2': (1, ('int', 'hex'))}
1588 suite_dependencies = []
1589 self.assertRaises(GeneratorInputError, gen_from_test_data, data_f,
1590 out_data_f, func_info, suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001591
1592 def test_output(self):
1593 """
1594 Test that intermediate data file is written with expected data.
Mohammad Azim Khanb73159d2018-06-13 16:31:26 +01001595 :return:
Azim Khan599cd242017-07-06 17:34:27 +01001596 """
1597 data = '''
1598My test 1
1599depends_on:DEP1
1600func1:0:0xfa:MACRO1:MACRO2
1601
1602My test 2
1603depends_on:DEP1:DEP2
1604func2:"yahoo":88:MACRO1
1605'''
1606 data_f = StringIOWrapper('test_suite_ut.data', data)
1607 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
Azim Khanb31aa442018-07-03 11:57:54 +01001608 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')),
1609 'test_func2': (1, ('char*', 'int', 'int'))}
1610 suite_dependencies = []
1611 dep_check_code, expression_code = \
1612 gen_from_test_data(data_f, out_data_f, func_info,
1613 suite_dependencies)
Azim Khan599cd242017-07-06 17:34:27 +01001614 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001615 case 0:
1616 {
Azim Khan599cd242017-07-06 17:34:27 +01001617#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001618 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001619#else
Azim Khand61b8372017-07-10 11:54:01 +01001620 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001621#endif
Azim Khand61b8372017-07-10 11:54:01 +01001622 }
1623 break;
1624 case 1:
1625 {
Azim Khan599cd242017-07-06 17:34:27 +01001626#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001627 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001628#else
Azim Khand61b8372017-07-10 11:54:01 +01001629 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001630#endif
Azim Khand61b8372017-07-10 11:54:01 +01001631 }
1632 break;'''
Azim Khanb31aa442018-07-03 11:57:54 +01001633 expected_data = '''My test 1
Azim Khan599cd242017-07-06 17:34:27 +01001634depends_on:0
16350:int:0:int:0xfa:exp:0:exp:1
1636
1637My test 2
1638depends_on:0:1
16391:char*:"yahoo":int:88:exp:0
1640
1641'''
1642 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001643 case 0:
1644 {
1645 *out_value = MACRO1;
1646 }
1647 break;
1648 case 1:
1649 {
1650 *out_value = MACRO2;
1651 }
1652 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001653 self.assertEqual(dep_check_code, expected_dep_check_code)
Azim Khanb31aa442018-07-03 11:57:54 +01001654 self.assertEqual(out_data_f.getvalue(), expected_data)
Azim Khan599cd242017-07-06 17:34:27 +01001655 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001656
1657
Azim Khanb31aa442018-07-03 11:57:54 +01001658if __name__ == '__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001659 unittest_main()