blob: 8545b4a0c3241dc898607824870e5a4a46178202 [file] [log] [blame]
Azim Khan4b543232017-06-30 09:35:21 +01001"""
2mbed TLS
3Copyright (c) 2017 ARM Limited
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16"""
17from StringIO import StringIO
18from unittest import TestCase, main as unittest_main
19from mock import patch
20from generate_code import *
21
22
23"""
24Unit tests for generate_code.py
25"""
26
27
28class GenDep(TestCase):
29 """
30 Test suite for function gen_dep()
31 """
32
33 def test_deps_list(self):
34 """
35 Test that gen_dep() correctly creates deps for given dependency list.
36 :return:
37 """
38 deps = ['DEP1', 'DEP2']
39 dep_start, dep_end = gen_deps(deps)
40 ifdef1, ifdef2 = dep_start.splitlines()
41 endif1, endif2 = dep_end.splitlines()
42 self.assertEqual(ifdef1, '#if defined(DEP1)', 'ifdef generated incorrectly')
43 self.assertEqual(ifdef2, '#if defined(DEP2)', 'ifdef generated incorrectly')
44 self.assertEqual(endif1, '#endif /* DEP2 */', 'endif generated incorrectly')
45 self.assertEqual(endif2, '#endif /* DEP1 */', 'endif generated incorrectly')
46
47 def test_disabled_deps_list(self):
48 """
49 Test that gen_dep() correctly creates deps for given dependency list.
50 :return:
51 """
52 deps = ['!DEP1', '!DEP2']
53 dep_start, dep_end = gen_deps(deps)
54 ifdef1, ifdef2 = dep_start.splitlines()
55 endif1, endif2 = dep_end.splitlines()
56 self.assertEqual(ifdef1, '#if !defined(DEP1)', 'ifdef generated incorrectly')
57 self.assertEqual(ifdef2, '#if !defined(DEP2)', 'ifdef generated incorrectly')
58 self.assertEqual(endif1, '#endif /* !DEP2 */', 'endif generated incorrectly')
59 self.assertEqual(endif2, '#endif /* !DEP1 */', 'endif generated incorrectly')
60
61 def test_mixed_deps_list(self):
62 """
63 Test that gen_dep() correctly creates deps for given dependency list.
64 :return:
65 """
66 deps = ['!DEP1', 'DEP2']
67 dep_start, dep_end = gen_deps(deps)
68 ifdef1, ifdef2 = dep_start.splitlines()
69 endif1, endif2 = dep_end.splitlines()
70 self.assertEqual(ifdef1, '#if !defined(DEP1)', 'ifdef generated incorrectly')
71 self.assertEqual(ifdef2, '#if defined(DEP2)', 'ifdef generated incorrectly')
72 self.assertEqual(endif1, '#endif /* DEP2 */', 'endif generated incorrectly')
73 self.assertEqual(endif2, '#endif /* !DEP1 */', 'endif generated incorrectly')
74
75 def test_empty_deps_list(self):
76 """
77 Test that gen_dep() correctly creates deps for given dependency list.
78 :return:
79 """
80 deps = []
81 dep_start, dep_end = gen_deps(deps)
82 self.assertEqual(dep_start, '', 'ifdef generated incorrectly')
83 self.assertEqual(dep_end, '', 'ifdef generated incorrectly')
84
85 def test_large_deps_list(self):
86 """
87 Test that gen_dep() correctly creates deps for given dependency list.
88 :return:
89 """
90 deps = []
91 count = 10
92 for i in range(count):
93 deps.append('DEP%d' % i)
94 dep_start, dep_end = gen_deps(deps)
95 self.assertEqual(len(dep_start.splitlines()), count, 'ifdef generated incorrectly')
96 self.assertEqual(len(dep_end.splitlines()), count, 'ifdef generated incorrectly')
97
98
99class GenDepOneLine(TestCase):
100 """
101 Test Suite for testing gen_deps_one_line()
102 """
103
104 def test_deps_list(self):
105 """
106 Test that gen_dep() correctly creates deps for given dependency list.
107 :return:
108 """
109 deps = ['DEP1', 'DEP2']
110 dep_str = gen_deps_one_line(deps)
111 self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)', 'ifdef generated incorrectly')
112
113 def test_disabled_deps_list(self):
114 """
115 Test that gen_dep() correctly creates deps for given dependency list.
116 :return:
117 """
118 deps = ['!DEP1', '!DEP2']
119 dep_str = gen_deps_one_line(deps)
120 self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)', 'ifdef generated incorrectly')
121
122 def test_mixed_deps_list(self):
123 """
124 Test that gen_dep() correctly creates deps for given dependency list.
125 :return:
126 """
127 deps = ['!DEP1', 'DEP2']
128 dep_str = gen_deps_one_line(deps)
129 self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)', 'ifdef generated incorrectly')
130
131 def test_empty_deps_list(self):
132 """
133 Test that gen_dep() correctly creates deps for given dependency list.
134 :return:
135 """
136 deps = []
137 dep_str = gen_deps_one_line(deps)
138 self.assertEqual(dep_str, '', 'ifdef generated incorrectly')
139
140 def test_large_deps_list(self):
141 """
142 Test that gen_dep() correctly creates deps for given dependency list.
143 :return:
144 """
145 deps = []
146 count = 10
147 for i in range(count):
148 deps.append('DEP%d' % i)
149 dep_str = gen_deps_one_line(deps)
150 expected = '#if ' + ' && '.join(['defined(%s)' % x for x in deps])
151 self.assertEqual(dep_str, expected, 'ifdef generated incorrectly')
152
153
154class GenFunctionWrapper(TestCase):
155 """
156 Test Suite for testing gen_function_wrapper()
157 """
158
159 def test_params_unpack(self):
160 """
161 Test that params are properly unpacked in the function call.
162
163 :return:
164 """
165 code = gen_function_wrapper('test_a', '', ('a', 'b', 'c', 'd'))
166 expected = '''
167void test_a_wrapper( void ** params )
168{
169
170
171 test_a( a, b, c, d );
172}
173'''
174 self.assertEqual(code, expected)
175
176 def test_local(self):
177 """
178 Test that params are properly unpacked in the function call.
179
180 :return:
181 """
182 code = gen_function_wrapper('test_a', 'int x = 1;', ('x', 'b', 'c', 'd'))
183 expected = '''
184void test_a_wrapper( void ** params )
185{
186
187int x = 1;
188 test_a( x, b, c, d );
189}
190'''
191 self.assertEqual(code, expected)
192
193 def test_empty_params(self):
194 """
195 Test that params are properly unpacked in the function call.
196
197 :return:
198 """
199 code = gen_function_wrapper('test_a', '', ())
200 expected = '''
201void test_a_wrapper( void ** params )
202{
203 (void)params;
204
205 test_a( );
206}
207'''
208 self.assertEqual(code, expected)
209
210
211class GenDispatch(TestCase):
212 """
213 Test suite for testing gen_dispatch()
214 """
215
216 def test_dispatch(self):
217 """
218 Test that dispatch table entry is generated correctly.
219 :return:
220 """
221 code = gen_dispatch('test_a', ['DEP1', 'DEP2'])
222 expected = '''
223#if defined(DEP1) && defined(DEP2)
224 test_a_wrapper,
225#else
226 NULL,
227#endif
228'''
229 self.assertEqual(code, expected)
230
231 def test_empty_deps(self):
232 """
233 Test empty dependency list.
234 :return:
235 """
236 code = gen_dispatch('test_a', [])
237 expected = '''
238 test_a_wrapper,
239'''
240 self.assertEqual(code, expected)
241
242
243class StringIOWrapper(StringIO, object):
244 """
245 file like class to mock file object in tests.
246 """
247 def __init__(self, file_name, data, line_no = 1):
248 """
249 Init file handle.
250
251 :param file_name:
252 :param data:
253 :param line_no:
254 """
255 super(StringIOWrapper, self).__init__(data)
256 self.line_no = line_no
257 self.name = file_name
258
259 def next(self):
260 """
261 Iterator return impl.
262 :return:
263 """
264 line = super(StringIOWrapper, self).next()
265 return line
266
267 def readline(self, limit=0):
268 """
269 Wrap the base class readline.
270
271 :param limit:
272 :return:
273 """
274 line = super(StringIOWrapper, self).readline()
275 if line:
276 self.line_no += 1
277 return line
278
279
280class ParseSuiteHeaders(TestCase):
281 """
282 Test Suite for testing parse_suite_headers().
283 """
284
285 def test_suite_headers(self):
286 """
287 Test that suite headers are parsed correctly.
288
289 :return:
290 """
291 data = '''#include "mbedtls/ecp.h"
292
293#define ECP_PF_UNKNOWN -1
294/* END_HEADER */
295'''
296 expected = '''#line 1 "test_suite_ut.function"
297#include "mbedtls/ecp.h"
298
299#define ECP_PF_UNKNOWN -1
300'''
301 s = StringIOWrapper('test_suite_ut.function', data, line_no=0)
302 headers = parse_suite_headers(s)
303 self.assertEqual(headers, expected)
304
305 def test_line_no(self):
306 """
307 Test that #line is set to correct line no. in source .function file.
308
309 :return:
310 """
311 data = '''#include "mbedtls/ecp.h"
312
313#define ECP_PF_UNKNOWN -1
314/* END_HEADER */
315'''
316 offset_line_no = 5
317 expected = '''#line %d "test_suite_ut.function"
318#include "mbedtls/ecp.h"
319
320#define ECP_PF_UNKNOWN -1
321''' % (offset_line_no + 1)
322 s = StringIOWrapper('test_suite_ut.function', data, offset_line_no)
323 headers = parse_suite_headers(s)
324 self.assertEqual(headers, expected)
325
326 def test_no_end_header_comment(self):
327 """
328 Test that InvalidFileFormat is raised when end header comment is missing.
329 :return:
330 """
331 data = '''#include "mbedtls/ecp.h"
332
333#define ECP_PF_UNKNOWN -1
334
335'''
336 s = StringIOWrapper('test_suite_ut.function', data)
337 self.assertRaises(InvalidFileFormat, parse_suite_headers, s)
338
339
340class ParseSuiteDeps(TestCase):
341 """
342 Test Suite for testing parse_suite_deps().
343 """
344
345 def test_suite_deps(self):
346 """
347
348 :return:
349 """
350 data = '''
351 * depends_on:MBEDTLS_ECP_C
352 * END_DEPENDENCIES
353 */
354'''
355 expected = ['MBEDTLS_ECP_C']
356 s = StringIOWrapper('test_suite_ut.function', data)
357 deps = parse_suite_deps(s)
358 self.assertEqual(deps, expected)
359
360 def test_no_end_dep_comment(self):
361 """
362 Test that InvalidFileFormat is raised when end dep comment is missing.
363 :return:
364 """
365 data = '''
366* depends_on:MBEDTLS_ECP_C
367'''
368 s = StringIOWrapper('test_suite_ut.function', data)
369 self.assertRaises(InvalidFileFormat, parse_suite_deps, s)
370
371 def test_deps_split(self):
372 """
373 Test that InvalidFileFormat is raised when end dep comment is missing.
374 :return:
375 """
376 data = '''
377 * depends_on:MBEDTLS_ECP_C:A:B: C : D :F : G: !H
378 * END_DEPENDENCIES
379 */
380'''
381 expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H']
382 s = StringIOWrapper('test_suite_ut.function', data)
383 deps = parse_suite_deps(s)
384 self.assertEqual(deps, expected)
385
386
387class ParseFuncDeps(TestCase):
388 """
389 Test Suite for testing parse_function_deps()
390 """
391
392 def test_function_deps(self):
393 """
394 Test that parse_function_deps() correctly parses function dependencies.
395 :return:
396 """
397 line = '/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */'
398 expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO']
399 deps = parse_function_deps(line)
400 self.assertEqual(deps, expected)
401
402 def test_no_deps(self):
403 """
404 Test that parse_function_deps() correctly parses function dependencies.
405 :return:
406 """
407 line = '/* BEGIN_CASE */'
408 deps = parse_function_deps(line)
409 self.assertEqual(deps, [])
410
411 def test_poorly_defined_deps(self):
412 """
413 Test that parse_function_deps() correctly parses function dependencies.
414 :return:
415 """
416 line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/'
417 deps = parse_function_deps(line)
418 self.assertEqual(deps, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F'])
419
420
421class ParseFuncSignature(TestCase):
422 """
423 Test Suite for parse_function_signature().
424 """
425
426 def test_int_and_char_params(self):
427 """
428
429 :return:
430 """
431 line = 'void entropy_threshold( char * a, int b, int result )'
432 name, args, local, arg_dispatch = parse_function_signature(line)
433 self.assertEqual(name, 'entropy_threshold')
434 self.assertEqual(args, ['char*', 'int', 'int'])
435 self.assertEqual(local, '')
436 self.assertEqual(arg_dispatch, ['(char *) params[0]', '*( (int *) params[1] )', '*( (int *) params[2] )'])
437
438 def test_hex_params(self):
439 """
440
441 :return:
442 """
443 line = 'void entropy_threshold( char * a, HexParam_t * h, int result )'
444 name, args, local, arg_dispatch = parse_function_signature(line)
445 self.assertEqual(name, 'entropy_threshold')
446 self.assertEqual(args, ['char*', 'hex', 'int'])
447 self.assertEqual(local, ' HexParam_t hex1 = {(uint8_t *) params[1], *( (uint32_t *) params[2] )};\n')
448 self.assertEqual(arg_dispatch, ['(char *) params[0]', '&hex1', '*( (int *) params[3] )'])
449
450 def test_non_void_function(self):
451 """
452
453 :return:
454 """
455 line = 'int entropy_threshold( char * a, HexParam_t * h, int result )'
456 self.assertRaises(ValueError, parse_function_signature, line)
457
458 def test_unsupported_arg(self):
459 """
460
461 :return:
462 """
463 line = 'int entropy_threshold( char * a, HexParam_t * h, int * result )'
464 self.assertRaises(ValueError, parse_function_signature, line)
465
466 def test_no_params(self):
467 """
468
469 :return:
470 """
471 line = 'void entropy_threshold()'
472 name, args, local, arg_dispatch = parse_function_signature(line)
473 self.assertEqual(name, 'entropy_threshold')
474 self.assertEqual(args, [])
475 self.assertEqual(local, '')
476 self.assertEqual(arg_dispatch, [])
477
478
479class ParseFunctionCode(TestCase):
480 """
481 Test suite for testing parse_function_code()
482 """
483
484 def test_no_function(self):
485 """
486
487 :return:
488 """
489 data = '''
490No
491test
492function
493'''
494 s = StringIOWrapper('test_suite_ut.function', data)
495 self.assertRaises(InvalidFileFormat, parse_function_code, s, [], [])
496
497 def test_no_end_case_comment(self):
498 """
499
500 :return:
501 """
502 data = '''
503void test_func()
504{
505}
506'''
507 s = StringIOWrapper('test_suite_ut.function', data)
508 self.assertRaises(InvalidFileFormat, parse_function_code, s, [], [])
509
510 @patch("generate_code.parse_function_signature")
511 def test_parse_function_signature_called(self, parse_function_signature_mock):
512 """
513
514 :return:
515 """
516 parse_function_signature_mock.return_value = ('test_func', [], '', [])
517 data = '''
518void test_func()
519{
520}
521'''
522 s = StringIOWrapper('test_suite_ut.function', data)
523 self.assertRaises(InvalidFileFormat, parse_function_code, s, [], [])
524 self.assertTrue(parse_function_signature_mock.called)
525 parse_function_signature_mock.assert_called_with('void test_func()\n')
526
527 @patch("generate_code.gen_dispatch")
528 @patch("generate_code.gen_deps")
529 @patch("generate_code.gen_function_wrapper")
530 @patch("generate_code.parse_function_signature")
531 def test_return(self, parse_function_signature_mock,
532 gen_function_wrapper_mock,
533 gen_deps_mock,
534 gen_dispatch_mock):
535 """
536
537 :return:
538 """
539 parse_function_signature_mock.return_value = ('func', [], '', [])
540 gen_function_wrapper_mock.return_value = ''
541 gen_deps_mock.side_effect = gen_deps
542 gen_dispatch_mock.side_effect = gen_dispatch
543 data = '''
544void func()
545{
546 ba ba black sheep
547 have you any wool
548}
549/* END_CASE */
550'''
551 s = StringIOWrapper('test_suite_ut.function', data)
552 name, arg, code, dispatch_code = parse_function_code(s, [], [])
553
554 #self.assertRaises(InvalidFileFormat, parse_function_code, s, [], [])
555 self.assertTrue(parse_function_signature_mock.called)
556 parse_function_signature_mock.assert_called_with('void func()\n')
557 gen_function_wrapper_mock.assert_called_with('test_func', '', [])
558 self.assertEqual(name, 'test_func')
559 self.assertEqual(arg, [])
560 expected = '''#line 2 "test_suite_ut.function"
561void test_func()
562{
563 ba ba black sheep
564 have you any wool
565exit:
566 ;;
567}
568'''
569 self.assertEqual(code, expected)
570 self.assertEqual(dispatch_code, "\n test_func_wrapper,\n")
571
572 @patch("generate_code.gen_dispatch")
573 @patch("generate_code.gen_deps")
574 @patch("generate_code.gen_function_wrapper")
575 @patch("generate_code.parse_function_signature")
576 def test_with_exit_label(self, parse_function_signature_mock,
577 gen_function_wrapper_mock,
578 gen_deps_mock,
579 gen_dispatch_mock):
580 """
581
582 :return:
583 """
584 parse_function_signature_mock.return_value = ('func', [], '', [])
585 gen_function_wrapper_mock.return_value = ''
586 gen_deps_mock.side_effect = gen_deps
587 gen_dispatch_mock.side_effect = gen_dispatch
588 data = '''
589void func()
590{
591 ba ba black sheep
592 have you any wool
593exit:
594 yes sir yes sir
595 3 bags full
596}
597/* END_CASE */
598'''
599 s = StringIOWrapper('test_suite_ut.function', data)
600 name, arg, code, dispatch_code = parse_function_code(s, [], [])
601
602 expected = '''#line 2 "test_suite_ut.function"
603void test_func()
604{
605 ba ba black sheep
606 have you any wool
607exit:
608 yes sir yes sir
609 3 bags full
610}
611'''
612 self.assertEqual(code, expected)
613
614
615class ParseFunction(TestCase):
616 """
617 Test Suite for testing parse_functions()
618 """
619
620 @patch("generate_code.parse_suite_headers")
621 def test_begin_header(self, parse_suite_headers_mock):
622 """
623 Test that begin header is checked and parse_suite_headers() is called.
624 :return:
625 """
626 def stop(this):
627 raise Exception
628 parse_suite_headers_mock.side_effect = stop
629 data = '''/* BEGIN_HEADER */
630#include "mbedtls/ecp.h"
631
632#define ECP_PF_UNKNOWN -1
633/* END_HEADER */
634'''
635 s = StringIOWrapper('test_suite_ut.function', data)
636 self.assertRaises(Exception, parse_functions, s)
637 parse_suite_headers_mock.assert_called_with(s)
638 self.assertEqual(s.line_no, 2)
639
640 @patch("generate_code.parse_suite_deps")
641 def test_begin_dep(self, parse_suite_deps_mock):
642 """
643 Test that begin header is checked and parse_suite_headers() is called.
644 :return:
645 """
646 def stop(this):
647 raise Exception
648 parse_suite_deps_mock.side_effect = stop
649 data = '''/* BEGIN_DEPENDENCIES
650 * depends_on:MBEDTLS_ECP_C
651 * END_DEPENDENCIES
652 */
653'''
654 s = StringIOWrapper('test_suite_ut.function', data)
655 self.assertRaises(Exception, parse_functions, s)
656 parse_suite_deps_mock.assert_called_with(s)
657 self.assertEqual(s.line_no, 2)
658
659 @patch("generate_code.parse_function_deps")
660 def test_begin_function_dep(self, parse_function_deps_mock):
661 """
662 Test that begin header is checked and parse_suite_headers() is called.
663 :return:
664 """
665 def stop(this):
666 raise Exception
667 parse_function_deps_mock.side_effect = stop
668
669 deps_str = '/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
670 data = '''%svoid test_func()
671{
672}
673''' % deps_str
674 s = StringIOWrapper('test_suite_ut.function', data)
675 self.assertRaises(Exception, parse_functions, s)
676 parse_function_deps_mock.assert_called_with(deps_str)
677 self.assertEqual(s.line_no, 2)
678
679 @patch("generate_code.parse_function_code")
680 @patch("generate_code.parse_function_deps")
681 def test_return(self, parse_function_deps_mock, parse_function_code_mock):
682 """
683 Test that begin header is checked and parse_suite_headers() is called.
684 :return:
685 """
686 def stop(this):
687 raise Exception
688 parse_function_deps_mock.return_value = []
689 in_func_code= '''void test_func()
690{
691}
692'''
693 func_dispatch = '''
694 test_func_wrapper,
695'''
696 parse_function_code_mock.return_value = 'test_func', [], in_func_code, func_dispatch
697 deps_str = '/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n'
698 data = '''%svoid test_func()
699{
700}
701''' % deps_str
702 s = StringIOWrapper('test_suite_ut.function', data)
703 suite_deps, dispatch_code, func_code, func_info = parse_functions(s)
704 parse_function_deps_mock.assert_called_with(deps_str)
705 parse_function_code_mock.assert_called_with(s, [], [])
706 self.assertEqual(s.line_no, 5)
707 self.assertEqual(suite_deps, [])
708 expected_dispatch_code = '''/* Function Id: 0 */
709
710 test_func_wrapper,
711'''
712 self.assertEqual(dispatch_code, expected_dispatch_code)
713 self.assertEqual(func_code, in_func_code)
714 self.assertEqual(func_info, {'test_func': (0, [])})
715
716 def test_parsing(self):
717 """
718 Test that begin header is checked and parse_suite_headers() is called.
719 :return:
720 """
721 data = '''/* BEGIN_HEADER */
722#include "mbedtls/ecp.h"
723
724#define ECP_PF_UNKNOWN -1
725/* END_HEADER */
726
727/* BEGIN_DEPENDENCIES
728 * depends_on:MBEDTLS_ECP_C
729 * END_DEPENDENCIES
730 */
731
732/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
733void func1()
734{
735}
736/* END_CASE */
737
738/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
739void func2()
740{
741}
742/* END_CASE */
743'''
744 s = StringIOWrapper('test_suite_ut.function', data)
745 suite_deps, dispatch_code, func_code, func_info = parse_functions(s)
746 self.assertEqual(s.line_no, 23)
747 self.assertEqual(suite_deps, ['MBEDTLS_ECP_C'])
748
749 expected_dispatch_code = '''/* Function Id: 0 */
750
751#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
752 test_func1_wrapper,
753#else
754 NULL,
755#endif
756/* Function Id: 1 */
757
758#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_FS_IO)
759 test_func2_wrapper,
760#else
761 NULL,
762#endif
763'''
764 self.assertEqual(dispatch_code, expected_dispatch_code)
765 expected_func_code = '''#if defined(MBEDTLS_ECP_C)
766#line 3 "test_suite_ut.function"
767#include "mbedtls/ecp.h"
768
769#define ECP_PF_UNKNOWN -1
770#if defined(MBEDTLS_ENTROPY_NV_SEED)
771#if defined(MBEDTLS_FS_IO)
772#line 14 "test_suite_ut.function"
773void test_func1()
774{
775exit:
776 ;;
777}
778
779void test_func1_wrapper( void ** params )
780{
781 (void)params;
782
783 test_func1( );
784}
785#endif /* MBEDTLS_FS_IO */
786#endif /* MBEDTLS_ENTROPY_NV_SEED */
787#if defined(MBEDTLS_ENTROPY_NV_SEED)
788#if defined(MBEDTLS_FS_IO)
789#line 20 "test_suite_ut.function"
790void test_func2()
791{
792exit:
793 ;;
794}
795
796void test_func2_wrapper( void ** params )
797{
798 (void)params;
799
800 test_func2( );
801}
802#endif /* MBEDTLS_FS_IO */
803#endif /* MBEDTLS_ENTROPY_NV_SEED */
804#endif /* MBEDTLS_ECP_C */
805'''
806 self.assertEqual(func_code, expected_func_code)
807 self.assertEqual(func_info, {'test_func1': (0, []), 'test_func2': (1, [])})
808
809 def test_same_function_name(self):
810 """
811 Test that begin header is checked and parse_suite_headers() is called.
812 :return:
813 """
814 data = '''/* BEGIN_HEADER */
815#include "mbedtls/ecp.h"
816
817#define ECP_PF_UNKNOWN -1
818/* END_HEADER */
819
820/* BEGIN_DEPENDENCIES
821 * depends_on:MBEDTLS_ECP_C
822 * END_DEPENDENCIES
823 */
824
825/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
826void func()
827{
828}
829/* END_CASE */
830
831/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
832void func()
833{
834}
835/* END_CASE */
836'''
837 s = StringIOWrapper('test_suite_ut.function', data)
838 self.assertRaises(AssertionError, parse_functions, s)
839
840
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100841class ExcapedSplit(TestCase):
842 """
Azim Khan599cd242017-07-06 17:34:27 +0100843 Test suite for testing escaped_split().
844 Note: Since escaped_split() output is used to write back to the intermediate data file. Any escape characters
845 in the input are retained in the output.
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100846 """
847
848 def test_invalid_input(self):
849 """
850 Test when input split character is not a character.
851 :return:
852 """
853 self.assertRaises(ValueError, escaped_split, '', 'string')
854
855 def test_empty_string(self):
856 """
857 Test empty strig input.
858 :return:
859 """
860 splits = escaped_split('', ':')
861 self.assertEqual(splits, [])
862
863 def test_no_escape(self):
864 """
865 Test with no escape character. The behaviour should be same as str.split()
866 :return:
867 """
868 s = 'yahoo:google'
869 splits = escaped_split(s, ':')
870 self.assertEqual(splits, s.split(':'))
871
872 def test_escaped_input(self):
873 """
874 Test imput that has escaped delimiter.
875 :return:
876 """
877 s = 'yahoo\:google:facebook'
878 splits = escaped_split(s, ':')
Azim Khan599cd242017-07-06 17:34:27 +0100879 self.assertEqual(splits, ['yahoo\:google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100880
881 def test_escaped_escape(self):
882 """
883 Test imput that has escaped delimiter.
884 :return:
885 """
886 s = 'yahoo\\\:google:facebook'
887 splits = escaped_split(s, ':')
Azim Khan599cd242017-07-06 17:34:27 +0100888 self.assertEqual(splits, ['yahoo\\\\', 'google', 'facebook'])
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100889
890 def test_all_at_once(self):
891 """
892 Test imput that has escaped delimiter.
893 :return:
894 """
895 s = 'yahoo\\\:google:facebook\:instagram\\\:bbc\\\\:wikipedia'
896 splits = escaped_split(s, ':')
Azim Khan599cd242017-07-06 17:34:27 +0100897 self.assertEqual(splits, ['yahoo\\\\', 'google', 'facebook\:instagram\\\\', 'bbc\\\\', 'wikipedia'])
898
Azim Khan5e2ac1f2017-07-03 13:58:20 +0100899
900class ParseTestData(TestCase):
901 """
902 Test suite for parse test data.
903 """
904
905 def test_parser(self):
906 """
907 Test that tests are parsed correctly from data file.
908 :return:
909 """
910 data = """
911Diffie-Hellman full exchange #1
912dhm_do_dhm:10:"23":10:"5"
913
914Diffie-Hellman full exchange #2
915dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
916
917Diffie-Hellman full exchange #3
918dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297123912791271"
919
920Diffie-Hellman selftest
921dhm_selftest:
922"""
923 s = StringIOWrapper('test_suite_ut.function', data)
924 tests = [(name, function, deps, args) for name, function, deps, args in parse_test_data(s)]
925 t1, t2, t3, t4 = tests
926 self.assertEqual(t1[0], 'Diffie-Hellman full exchange #1')
927 self.assertEqual(t1[1], 'dhm_do_dhm')
928 self.assertEqual(t1[2], [])
929 self.assertEqual(t1[3], ['10', '"23"', '10', '"5"'])
930
931 self.assertEqual(t2[0], 'Diffie-Hellman full exchange #2')
932 self.assertEqual(t2[1], 'dhm_do_dhm')
933 self.assertEqual(t2[2], [])
934 self.assertEqual(t2[3], ['10', '"93450983094850938450983409623"', '10', '"9345098304850938450983409622"'])
935
936 self.assertEqual(t3[0], 'Diffie-Hellman full exchange #3')
937 self.assertEqual(t3[1], 'dhm_do_dhm')
938 self.assertEqual(t3[2], [])
939 self.assertEqual(t3[3], ['10', '"9345098382739712938719287391879381271"', '10', '"9345098792137312973297123912791271"'])
940
941 self.assertEqual(t4[0], 'Diffie-Hellman selftest')
942 self.assertEqual(t4[1], 'dhm_selftest')
943 self.assertEqual(t4[2], [])
944 self.assertEqual(t4[3], [])
945
946 def test_with_dependencies(self):
947 """
948 Test that tests with dependencies are parsed.
949 :return:
950 """
951 data = """
952Diffie-Hellman full exchange #1
953depends_on:YAHOO
954dhm_do_dhm:10:"23":10:"5"
955
956Diffie-Hellman full exchange #2
957dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
958
959"""
960 s = StringIOWrapper('test_suite_ut.function', data)
961 tests = [(name, function, deps, args) for name, function, deps, args in parse_test_data(s)]
962 t1, t2 = tests
963 self.assertEqual(t1[0], 'Diffie-Hellman full exchange #1')
964 self.assertEqual(t1[1], 'dhm_do_dhm')
965 self.assertEqual(t1[2], ['YAHOO'])
966 self.assertEqual(t1[3], ['10', '"23"', '10', '"5"'])
967
968 self.assertEqual(t2[0], 'Diffie-Hellman full exchange #2')
969 self.assertEqual(t2[1], 'dhm_do_dhm')
970 self.assertEqual(t2[2], [])
971 self.assertEqual(t2[3], ['10', '"93450983094850938450983409623"', '10', '"9345098304850938450983409622"'])
972
973 def test_no_args(self):
974 """
975 Test AssertionError is raised when test function name and args line is missing.
976 :return:
977 """
978 data = """
979Diffie-Hellman full exchange #1
980depends_on:YAHOO
981
982
983Diffie-Hellman full exchange #2
984dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
985
986"""
987 s = StringIOWrapper('test_suite_ut.function', data)
988 e = None
989 try:
990 for x, y, z, a in parse_test_data(s):
991 pass
992 except AssertionError, e:
993 pass
994 self.assertEqual(type(e), AssertionError)
995
996 def test_incomplete_data(self):
997 """
998 Test AssertionError is raised when test function name and args line is missing.
999 :return:
1000 """
1001 data = """
1002Diffie-Hellman full exchange #1
1003depends_on:YAHOO
1004"""
1005 s = StringIOWrapper('test_suite_ut.function', data)
1006 e = None
1007 try:
1008 for x, y, z, a in parse_test_data(s):
1009 pass
1010 except AssertionError, e:
1011 pass
1012 self.assertEqual(type(e), AssertionError)
1013
1014
1015class GenDepCheck(TestCase):
1016 """
1017 Test suite for gen_dep_check(). It is assumed this function is called with valid inputs.
1018 """
1019
1020 def test_gen_dep_check(self):
1021 """
1022 Test that dependency check code generated correctly.
1023 :return:
1024 """
1025 expected = """
1026if ( dep_id == 5 )
1027{
1028#if defined(YAHOO)
1029 return( DEPENDENCY_SUPPORTED );
1030#else
1031 return( DEPENDENCY_NOT_SUPPORTED );
1032#endif
1033}
1034else
1035"""
1036 out = gen_dep_check(5, 'YAHOO')
1037 self.assertEqual(out, expected)
1038
1039 def test_noT(self):
1040 """
1041 Test dependency with !.
1042 :return:
1043 """
1044 expected = """
1045if ( dep_id == 5 )
1046{
1047#if !defined(YAHOO)
1048 return( DEPENDENCY_SUPPORTED );
1049#else
1050 return( DEPENDENCY_NOT_SUPPORTED );
1051#endif
1052}
1053else
1054"""
1055 out = gen_dep_check(5, '!YAHOO')
1056 self.assertEqual(out, expected)
1057
1058 def test_empty_dependency(self):
1059 """
1060 Test invalid dependency input.
1061 :return:
1062 """
1063 self.assertRaises(AssertionError, gen_dep_check, 5, '!')
1064
1065 def test_negative_dep_id(self):
1066 """
1067 Test invalid dependency input.
1068 :return:
1069 """
1070 self.assertRaises(AssertionError, gen_dep_check, -1, 'YAHOO')
1071
1072
1073class GenExpCheck(TestCase):
1074 """
1075 Test suite for gen_expression_check(). It is assumed this function is called with valid inputs.
1076 """
1077
1078 def test_gen_exp_check(self):
1079 """
1080 Test that expression check code generated correctly.
1081 :return:
1082 """
1083 expected = """
1084if ( exp_id == 5 )
1085{
1086 *out_value = YAHOO;
1087}
1088else
1089"""
1090 out = gen_expression_check(5, 'YAHOO')
1091 self.assertEqual(out, expected)
1092
1093 def test_invalid_expression(self):
1094 """
1095 Test invalid expression input.
1096 :return:
1097 """
1098 self.assertRaises(AssertionError, gen_expression_check, 5, '')
1099
1100 def test_negative_exp_id(self):
1101 """
1102 Test invalid expression id.
1103 :return:
1104 """
1105 self.assertRaises(AssertionError, gen_expression_check, -1, 'YAHOO')
1106
1107
Azim Khan599cd242017-07-06 17:34:27 +01001108class WriteDeps(TestCase):
1109 """
1110 Test suite for testing write_deps.
1111 """
1112
1113 def test_no_test_deps(self):
1114 """
1115 Test when test_deps is empty.
1116 :return:
1117 """
1118 s = StringIOWrapper('test_suite_ut.data', '')
1119 unique_deps = []
1120 dep_check_code = write_deps(s, [], unique_deps)
1121 self.assertEqual(dep_check_code, '')
1122 self.assertEqual(len(unique_deps), 0)
1123 self.assertEqual(s.getvalue(), '')
1124
1125 def test_unique_dep_ids(self):
1126 """
1127
1128 :return:
1129 """
1130 s = StringIOWrapper('test_suite_ut.data', '')
1131 unique_deps = []
1132 dep_check_code = write_deps(s, ['DEP3', 'DEP2', 'DEP1'], unique_deps)
1133 expect_dep_check_code = '''
1134if ( dep_id == 0 )
1135{
1136#if defined(DEP3)
1137 return( DEPENDENCY_SUPPORTED );
1138#else
1139 return( DEPENDENCY_NOT_SUPPORTED );
1140#endif
1141}
1142else
1143
1144if ( dep_id == 1 )
1145{
1146#if defined(DEP2)
1147 return( DEPENDENCY_SUPPORTED );
1148#else
1149 return( DEPENDENCY_NOT_SUPPORTED );
1150#endif
1151}
1152else
1153
1154if ( dep_id == 2 )
1155{
1156#if defined(DEP1)
1157 return( DEPENDENCY_SUPPORTED );
1158#else
1159 return( DEPENDENCY_NOT_SUPPORTED );
1160#endif
1161}
1162else
1163'''
1164 self.assertEqual(dep_check_code, expect_dep_check_code)
1165 self.assertEqual(len(unique_deps), 3)
1166 self.assertEqual(s.getvalue(), 'depends_on:0:1:2\n')
1167
1168 def test_dep_id_repeat(self):
1169 """
1170
1171 :return:
1172 """
1173 s = StringIOWrapper('test_suite_ut.data', '')
1174 unique_deps = []
1175 dep_check_code = ''
1176 dep_check_code += write_deps(s, ['DEP3', 'DEP2'], unique_deps)
1177 dep_check_code += write_deps(s, ['DEP2', 'DEP1'], unique_deps)
1178 dep_check_code += write_deps(s, ['DEP1', 'DEP3'], unique_deps)
1179 expect_dep_check_code = '''
1180if ( dep_id == 0 )
1181{
1182#if defined(DEP3)
1183 return( DEPENDENCY_SUPPORTED );
1184#else
1185 return( DEPENDENCY_NOT_SUPPORTED );
1186#endif
1187}
1188else
1189
1190if ( dep_id == 1 )
1191{
1192#if defined(DEP2)
1193 return( DEPENDENCY_SUPPORTED );
1194#else
1195 return( DEPENDENCY_NOT_SUPPORTED );
1196#endif
1197}
1198else
1199
1200if ( dep_id == 2 )
1201{
1202#if defined(DEP1)
1203 return( DEPENDENCY_SUPPORTED );
1204#else
1205 return( DEPENDENCY_NOT_SUPPORTED );
1206#endif
1207}
1208else
1209'''
1210 self.assertEqual(dep_check_code, expect_dep_check_code)
1211 self.assertEqual(len(unique_deps), 3)
1212 self.assertEqual(s.getvalue(), 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
1213
1214
1215class WriteParams(TestCase):
1216 """
1217 Test Suite for testing write_parameters().
1218 """
1219
1220 def test_no_params(self):
1221 """
1222 Test with empty test_args
1223 :return:
1224 """
1225 s = StringIOWrapper('test_suite_ut.data', '')
1226 unique_expressions = []
1227 expression_code = write_parameters(s, [], [], unique_expressions)
1228 self.assertEqual(len(unique_expressions), 0)
1229 self.assertEqual(expression_code, '')
1230 self.assertEqual(s.getvalue(), '\n')
1231
1232 def test_no_exp_param(self):
1233 """
1234 Test when there is no macro or expression in the params.
1235 :return:
1236 """
1237 s = StringIOWrapper('test_suite_ut.data', '')
1238 unique_expressions = []
1239 expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0'], ['char*', 'hex', 'int'],
1240 unique_expressions)
1241 self.assertEqual(len(unique_expressions), 0)
1242 self.assertEqual(expression_code, '')
1243 self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0\n')
1244
1245 def test_hex_format_int_param(self):
1246 """
1247 Test int parameter in hex format.
1248 :return:
1249 """
1250 s = StringIOWrapper('test_suite_ut.data', '')
1251 unique_expressions = []
1252 expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0xAA'], ['char*', 'hex', 'int'],
1253 unique_expressions)
1254 self.assertEqual(len(unique_expressions), 0)
1255 self.assertEqual(expression_code, '')
1256 self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
1257
1258 def test_with_exp_param(self):
1259 """
1260 Test when there is macro or expression in the params.
1261 :return:
1262 """
1263 s = StringIOWrapper('test_suite_ut.data', '')
1264 unique_expressions = []
1265 expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0', 'MACRO1', 'MACRO2', 'MACRO3'],
1266 ['char*', 'hex', 'int', 'int', 'int', 'int'],
1267 unique_expressions)
1268 self.assertEqual(len(unique_expressions), 3)
1269 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1270 expected_expression_code = '''
1271if ( exp_id == 0 )
1272{
1273 *out_value = MACRO1;
1274}
1275else
1276
1277if ( exp_id == 1 )
1278{
1279 *out_value = MACRO2;
1280}
1281else
1282
1283if ( exp_id == 2 )
1284{
1285 *out_value = MACRO3;
1286}
1287else
1288'''
1289 self.assertEqual(expression_code, expected_expression_code)
1290 self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1:exp:2\n')
1291
1292 def test_with_repeate_calls(self):
1293 """
1294 Test when write_parameter() is called with same macro or expression.
1295 :return:
1296 """
1297 s = StringIOWrapper('test_suite_ut.data', '')
1298 unique_expressions = []
1299 expression_code = ''
1300 expression_code += write_parameters(s, ['"Yahoo"', 'MACRO1', 'MACRO2'], ['char*', 'int', 'int'],
1301 unique_expressions)
1302 expression_code += write_parameters(s, ['"abcdef00"', 'MACRO2', 'MACRO3'], ['hex', 'int', 'int'],
1303 unique_expressions)
1304 expression_code += write_parameters(s, ['0', 'MACRO3', 'MACRO1'], ['int', 'int', 'int'],
1305 unique_expressions)
1306 self.assertEqual(len(unique_expressions), 3)
1307 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1308 expected_expression_code = '''
1309if ( exp_id == 0 )
1310{
1311 *out_value = MACRO1;
1312}
1313else
1314
1315if ( exp_id == 1 )
1316{
1317 *out_value = MACRO2;
1318}
1319else
1320
1321if ( exp_id == 2 )
1322{
1323 *out_value = MACRO3;
1324}
1325else
1326'''
1327 self.assertEqual(expression_code, expected_expression_code)
1328 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1329:hex:"abcdef00":exp:1:exp:2
1330:int:0:exp:2:exp:0
1331'''
1332 self.assertEqual(s.getvalue(), expected_data_file)
1333
1334
1335class GenTestSuiteDepsChecks(TestCase):
1336 """
1337
1338 """
1339 def test_empty_suite_deps(self):
1340 """
1341 Test with empty suite_deps list.
1342
1343 :return:
1344 """
1345 dep_check_code, expression_code = gen_suite_deps_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
1346 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1347 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1348
1349 def test_suite_deps(self):
1350 """
1351 Test with suite_deps list.
1352
1353 :return:
1354 """
1355 dep_check_code, expression_code = gen_suite_deps_checks(['SUITE_DEP'], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
1356 exprectd_dep_check_code = '''
1357#if defined(SUITE_DEP)
1358DEP_CHECK_CODE
1359#else
1360(void) dep_id;
1361#endif
1362'''
1363 expected_expression_code = '''
1364#if defined(SUITE_DEP)
1365EXPRESSION_CODE
1366#else
1367(void) exp_id;
1368(void) out_value;
1369#endif
1370'''
1371 self.assertEqual(dep_check_code, exprectd_dep_check_code)
1372 self.assertEqual(expression_code, expected_expression_code)
1373
1374 def test_no_dep_no_exp(self):
1375 """
1376 Test when there are no dependency and expression code.
1377 :return:
1378 """
1379 dep_check_code, expression_code = gen_suite_deps_checks([], '', '')
1380 self.assertEqual(dep_check_code, '(void) dep_id;\n')
1381 self.assertEqual(expression_code, '(void) exp_id;\n(void) out_value;\n')
1382
1383
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001384class GenFromTestData(TestCase):
1385 """
1386 Test suite for gen_from_test_data()
1387 """
1388
Azim Khan599cd242017-07-06 17:34:27 +01001389 @patch("generate_code.write_deps")
1390 @patch("generate_code.write_parameters")
1391 @patch("generate_code.gen_suite_deps_checks")
1392 def test_intermediate_data_file(self, gen_suite_deps_checks_mock, write_parameters_mock, write_deps_mock):
1393 """
1394 Test that intermediate data file is written with expected data.
1395 :return:
1396 """
1397 data = '''
1398My test
1399depends_on:DEP1
1400func1:0
1401'''
1402 data_f = StringIOWrapper('test_suite_ut.data', data)
1403 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1404 func_info = {'test_func1': (1, ('int',))}
1405 suite_deps = []
1406 write_parameters_mock.side_effect = write_parameters
1407 write_deps_mock.side_effect = write_deps
1408 gen_suite_deps_checks_mock.side_effect = gen_suite_deps_checks
1409 gen_from_test_data(data_f, out_data_f, func_info, suite_deps)
1410 write_deps_mock.assert_called_with(out_data_f, ['DEP1'], ['DEP1'])
1411 write_parameters_mock.assert_called_with(out_data_f, ['0'], ('int',), [])
1412 expected_dep_check_code = '''
1413if ( dep_id == 0 )
1414{
1415#if defined(DEP1)
1416 return( DEPENDENCY_SUPPORTED );
1417#else
1418 return( DEPENDENCY_NOT_SUPPORTED );
1419#endif
1420}
1421else
1422'''
1423 gen_suite_deps_checks_mock.assert_called_with(suite_deps, expected_dep_check_code, '')
1424
1425 def test_function_not_found(self):
1426 """
1427 Test that AssertError is raised when function info in not found.
1428 :return:
1429 """
1430 data = '''
1431My test
1432depends_on:DEP1
1433func1:0
1434'''
1435 data_f = StringIOWrapper('test_suite_ut.data', data)
1436 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1437 func_info = {'test_func2': (1, ('int',))}
1438 suite_deps = []
1439 self.assertRaises(AssertionError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
1440
1441 def test_different_func_args(self):
1442 """
1443 Test that AssertError is raised when no. of parameters and function args differ.
1444 :return:
1445 """
1446 data = '''
1447My test
1448depends_on:DEP1
1449func1:0
1450'''
1451 data_f = StringIOWrapper('test_suite_ut.data', data)
1452 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1453 func_info = {'test_func2': (1, ('int','hex'))}
1454 suite_deps = []
1455 self.assertRaises(AssertionError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
1456
1457 def test_output(self):
1458 """
1459 Test that intermediate data file is written with expected data.
1460 :return:
1461 """
1462 data = '''
1463My test 1
1464depends_on:DEP1
1465func1:0:0xfa:MACRO1:MACRO2
1466
1467My test 2
1468depends_on:DEP1:DEP2
1469func2:"yahoo":88:MACRO1
1470'''
1471 data_f = StringIOWrapper('test_suite_ut.data', data)
1472 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1473 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')), 'test_func2': (1, ('char*', 'int', 'int'))}
1474 suite_deps = []
1475 dep_check_code, expression_code = gen_from_test_data(data_f, out_data_f, func_info, suite_deps)
1476 expected_dep_check_code = '''
1477if ( dep_id == 0 )
1478{
1479#if defined(DEP1)
1480 return( DEPENDENCY_SUPPORTED );
1481#else
1482 return( DEPENDENCY_NOT_SUPPORTED );
1483#endif
1484}
1485else
1486
1487if ( dep_id == 1 )
1488{
1489#if defined(DEP2)
1490 return( DEPENDENCY_SUPPORTED );
1491#else
1492 return( DEPENDENCY_NOT_SUPPORTED );
1493#endif
1494}
1495else
1496'''
1497 expecrted_data = '''My test 1
1498depends_on:0
14990:int:0:int:0xfa:exp:0:exp:1
1500
1501My test 2
1502depends_on:0:1
15031:char*:"yahoo":int:88:exp:0
1504
1505'''
1506 expected_expression_code = '''
1507if ( exp_id == 0 )
1508{
1509 *out_value = MACRO1;
1510}
1511else
1512
1513if ( exp_id == 1 )
1514{
1515 *out_value = MACRO2;
1516}
1517else
1518'''
1519 self.assertEqual(dep_check_code, expected_dep_check_code)
1520 self.assertEqual(out_data_f.getvalue(), expecrted_data)
1521 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001522
1523
Azim Khan4b543232017-06-30 09:35:21 +01001524if __name__=='__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001525 unittest_main()