blob: 4baeeafadf252a959ae4cc3d15b0825d0680aac9 [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 = """
Azim Khand61b8372017-07-10 11:54:01 +01001026 case 5:
1027 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001028#if defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001029 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001030#else
Azim Khand61b8372017-07-10 11:54:01 +01001031 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001032#endif
Azim Khand61b8372017-07-10 11:54:01 +01001033 }
1034 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001035 out = gen_dep_check(5, 'YAHOO')
1036 self.assertEqual(out, expected)
1037
1038 def test_noT(self):
1039 """
1040 Test dependency with !.
1041 :return:
1042 """
1043 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001044 case 5:
1045 {
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001046#if !defined(YAHOO)
Azim Khand61b8372017-07-10 11:54:01 +01001047 ret = DEPENDENCY_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001048#else
Azim Khand61b8372017-07-10 11:54:01 +01001049 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001050#endif
Azim Khand61b8372017-07-10 11:54:01 +01001051 }
1052 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001053 out = gen_dep_check(5, '!YAHOO')
1054 self.assertEqual(out, expected)
1055
1056 def test_empty_dependency(self):
1057 """
1058 Test invalid dependency input.
1059 :return:
1060 """
1061 self.assertRaises(AssertionError, gen_dep_check, 5, '!')
1062
1063 def test_negative_dep_id(self):
1064 """
1065 Test invalid dependency input.
1066 :return:
1067 """
1068 self.assertRaises(AssertionError, gen_dep_check, -1, 'YAHOO')
1069
1070
1071class GenExpCheck(TestCase):
1072 """
1073 Test suite for gen_expression_check(). It is assumed this function is called with valid inputs.
1074 """
1075
1076 def test_gen_exp_check(self):
1077 """
1078 Test that expression check code generated correctly.
1079 :return:
1080 """
1081 expected = """
Azim Khand61b8372017-07-10 11:54:01 +01001082 case 5:
1083 {
1084 *out_value = YAHOO;
1085 }
1086 break;"""
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001087 out = gen_expression_check(5, 'YAHOO')
1088 self.assertEqual(out, expected)
1089
1090 def test_invalid_expression(self):
1091 """
1092 Test invalid expression input.
1093 :return:
1094 """
1095 self.assertRaises(AssertionError, gen_expression_check, 5, '')
1096
1097 def test_negative_exp_id(self):
1098 """
1099 Test invalid expression id.
1100 :return:
1101 """
1102 self.assertRaises(AssertionError, gen_expression_check, -1, 'YAHOO')
1103
1104
Azim Khan599cd242017-07-06 17:34:27 +01001105class WriteDeps(TestCase):
1106 """
1107 Test suite for testing write_deps.
1108 """
1109
1110 def test_no_test_deps(self):
1111 """
1112 Test when test_deps is empty.
1113 :return:
1114 """
1115 s = StringIOWrapper('test_suite_ut.data', '')
1116 unique_deps = []
1117 dep_check_code = write_deps(s, [], unique_deps)
1118 self.assertEqual(dep_check_code, '')
1119 self.assertEqual(len(unique_deps), 0)
1120 self.assertEqual(s.getvalue(), '')
1121
1122 def test_unique_dep_ids(self):
1123 """
1124
1125 :return:
1126 """
1127 s = StringIOWrapper('test_suite_ut.data', '')
1128 unique_deps = []
1129 dep_check_code = write_deps(s, ['DEP3', 'DEP2', 'DEP1'], unique_deps)
1130 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001131 case 0:
1132 {
Azim Khan599cd242017-07-06 17:34:27 +01001133#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001134 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001135#else
Azim Khand61b8372017-07-10 11:54:01 +01001136 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001137#endif
Azim Khand61b8372017-07-10 11:54:01 +01001138 }
1139 break;
1140 case 1:
1141 {
Azim Khan599cd242017-07-06 17:34:27 +01001142#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001143 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001144#else
Azim Khand61b8372017-07-10 11:54:01 +01001145 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001146#endif
Azim Khand61b8372017-07-10 11:54:01 +01001147 }
1148 break;
1149 case 2:
1150 {
Azim Khan599cd242017-07-06 17:34:27 +01001151#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001152 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001153#else
Azim Khand61b8372017-07-10 11:54:01 +01001154 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001155#endif
Azim Khand61b8372017-07-10 11:54:01 +01001156 }
1157 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001158 self.assertEqual(dep_check_code, expect_dep_check_code)
1159 self.assertEqual(len(unique_deps), 3)
1160 self.assertEqual(s.getvalue(), 'depends_on:0:1:2\n')
1161
1162 def test_dep_id_repeat(self):
1163 """
1164
1165 :return:
1166 """
1167 s = StringIOWrapper('test_suite_ut.data', '')
1168 unique_deps = []
1169 dep_check_code = ''
1170 dep_check_code += write_deps(s, ['DEP3', 'DEP2'], unique_deps)
1171 dep_check_code += write_deps(s, ['DEP2', 'DEP1'], unique_deps)
1172 dep_check_code += write_deps(s, ['DEP1', 'DEP3'], unique_deps)
1173 expect_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001174 case 0:
1175 {
Azim Khan599cd242017-07-06 17:34:27 +01001176#if defined(DEP3)
Azim Khand61b8372017-07-10 11:54:01 +01001177 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001178#else
Azim Khand61b8372017-07-10 11:54:01 +01001179 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001180#endif
Azim Khand61b8372017-07-10 11:54:01 +01001181 }
1182 break;
1183 case 1:
1184 {
Azim Khan599cd242017-07-06 17:34:27 +01001185#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001186 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001187#else
Azim Khand61b8372017-07-10 11:54:01 +01001188 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001189#endif
Azim Khand61b8372017-07-10 11:54:01 +01001190 }
1191 break;
1192 case 2:
1193 {
Azim Khan599cd242017-07-06 17:34:27 +01001194#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001195 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001196#else
Azim Khand61b8372017-07-10 11:54:01 +01001197 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001198#endif
Azim Khand61b8372017-07-10 11:54:01 +01001199 }
1200 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001201 self.assertEqual(dep_check_code, expect_dep_check_code)
1202 self.assertEqual(len(unique_deps), 3)
1203 self.assertEqual(s.getvalue(), 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n')
1204
1205
1206class WriteParams(TestCase):
1207 """
1208 Test Suite for testing write_parameters().
1209 """
1210
1211 def test_no_params(self):
1212 """
1213 Test with empty test_args
1214 :return:
1215 """
1216 s = StringIOWrapper('test_suite_ut.data', '')
1217 unique_expressions = []
1218 expression_code = write_parameters(s, [], [], unique_expressions)
1219 self.assertEqual(len(unique_expressions), 0)
1220 self.assertEqual(expression_code, '')
1221 self.assertEqual(s.getvalue(), '\n')
1222
1223 def test_no_exp_param(self):
1224 """
1225 Test when there is no macro or expression in the params.
1226 :return:
1227 """
1228 s = StringIOWrapper('test_suite_ut.data', '')
1229 unique_expressions = []
1230 expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0'], ['char*', 'hex', 'int'],
1231 unique_expressions)
1232 self.assertEqual(len(unique_expressions), 0)
1233 self.assertEqual(expression_code, '')
1234 self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0\n')
1235
1236 def test_hex_format_int_param(self):
1237 """
1238 Test int parameter in hex format.
1239 :return:
1240 """
1241 s = StringIOWrapper('test_suite_ut.data', '')
1242 unique_expressions = []
1243 expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0xAA'], ['char*', 'hex', 'int'],
1244 unique_expressions)
1245 self.assertEqual(len(unique_expressions), 0)
1246 self.assertEqual(expression_code, '')
1247 self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n')
1248
1249 def test_with_exp_param(self):
1250 """
1251 Test when there is macro or expression in the params.
1252 :return:
1253 """
1254 s = StringIOWrapper('test_suite_ut.data', '')
1255 unique_expressions = []
1256 expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0', 'MACRO1', 'MACRO2', 'MACRO3'],
1257 ['char*', 'hex', 'int', 'int', 'int', 'int'],
1258 unique_expressions)
1259 self.assertEqual(len(unique_expressions), 3)
1260 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1261 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001262 case 0:
1263 {
1264 *out_value = MACRO1;
1265 }
1266 break;
1267 case 1:
1268 {
1269 *out_value = MACRO2;
1270 }
1271 break;
1272 case 2:
1273 {
1274 *out_value = MACRO3;
1275 }
1276 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001277 self.assertEqual(expression_code, expected_expression_code)
1278 self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1:exp:2\n')
1279
1280 def test_with_repeate_calls(self):
1281 """
1282 Test when write_parameter() is called with same macro or expression.
1283 :return:
1284 """
1285 s = StringIOWrapper('test_suite_ut.data', '')
1286 unique_expressions = []
1287 expression_code = ''
1288 expression_code += write_parameters(s, ['"Yahoo"', 'MACRO1', 'MACRO2'], ['char*', 'int', 'int'],
1289 unique_expressions)
1290 expression_code += write_parameters(s, ['"abcdef00"', 'MACRO2', 'MACRO3'], ['hex', 'int', 'int'],
1291 unique_expressions)
1292 expression_code += write_parameters(s, ['0', 'MACRO3', 'MACRO1'], ['int', 'int', 'int'],
1293 unique_expressions)
1294 self.assertEqual(len(unique_expressions), 3)
1295 self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3'])
1296 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001297 case 0:
1298 {
1299 *out_value = MACRO1;
1300 }
1301 break;
1302 case 1:
1303 {
1304 *out_value = MACRO2;
1305 }
1306 break;
1307 case 2:
1308 {
1309 *out_value = MACRO3;
1310 }
1311 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001312 self.assertEqual(expression_code, expected_expression_code)
1313 expected_data_file = ''':char*:"Yahoo":exp:0:exp:1
1314:hex:"abcdef00":exp:1:exp:2
1315:int:0:exp:2:exp:0
1316'''
1317 self.assertEqual(s.getvalue(), expected_data_file)
1318
1319
1320class GenTestSuiteDepsChecks(TestCase):
1321 """
1322
1323 """
1324 def test_empty_suite_deps(self):
1325 """
1326 Test with empty suite_deps list.
1327
1328 :return:
1329 """
1330 dep_check_code, expression_code = gen_suite_deps_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
1331 self.assertEqual(dep_check_code, 'DEP_CHECK_CODE')
1332 self.assertEqual(expression_code, 'EXPRESSION_CODE')
1333
1334 def test_suite_deps(self):
1335 """
1336 Test with suite_deps list.
1337
1338 :return:
1339 """
1340 dep_check_code, expression_code = gen_suite_deps_checks(['SUITE_DEP'], 'DEP_CHECK_CODE', 'EXPRESSION_CODE')
1341 exprectd_dep_check_code = '''
1342#if defined(SUITE_DEP)
1343DEP_CHECK_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001344#endif
1345'''
1346 expected_expression_code = '''
1347#if defined(SUITE_DEP)
1348EXPRESSION_CODE
Azim Khan599cd242017-07-06 17:34:27 +01001349#endif
1350'''
1351 self.assertEqual(dep_check_code, exprectd_dep_check_code)
1352 self.assertEqual(expression_code, expected_expression_code)
1353
1354 def test_no_dep_no_exp(self):
1355 """
1356 Test when there are no dependency and expression code.
1357 :return:
1358 """
1359 dep_check_code, expression_code = gen_suite_deps_checks([], '', '')
Azim Khand61b8372017-07-10 11:54:01 +01001360 self.assertEqual(dep_check_code, '')
1361 self.assertEqual(expression_code, '')
Azim Khan599cd242017-07-06 17:34:27 +01001362
1363
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001364class GenFromTestData(TestCase):
1365 """
1366 Test suite for gen_from_test_data()
1367 """
1368
Azim Khan599cd242017-07-06 17:34:27 +01001369 @patch("generate_code.write_deps")
1370 @patch("generate_code.write_parameters")
1371 @patch("generate_code.gen_suite_deps_checks")
1372 def test_intermediate_data_file(self, gen_suite_deps_checks_mock, write_parameters_mock, write_deps_mock):
1373 """
1374 Test that intermediate data file is written with expected data.
1375 :return:
1376 """
1377 data = '''
1378My test
1379depends_on:DEP1
1380func1:0
1381'''
1382 data_f = StringIOWrapper('test_suite_ut.data', data)
1383 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1384 func_info = {'test_func1': (1, ('int',))}
1385 suite_deps = []
1386 write_parameters_mock.side_effect = write_parameters
1387 write_deps_mock.side_effect = write_deps
1388 gen_suite_deps_checks_mock.side_effect = gen_suite_deps_checks
1389 gen_from_test_data(data_f, out_data_f, func_info, suite_deps)
1390 write_deps_mock.assert_called_with(out_data_f, ['DEP1'], ['DEP1'])
1391 write_parameters_mock.assert_called_with(out_data_f, ['0'], ('int',), [])
1392 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001393 case 0:
1394 {
Azim Khan599cd242017-07-06 17:34:27 +01001395#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001396 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001397#else
Azim Khand61b8372017-07-10 11:54:01 +01001398 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001399#endif
Azim Khand61b8372017-07-10 11:54:01 +01001400 }
1401 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001402 gen_suite_deps_checks_mock.assert_called_with(suite_deps, expected_dep_check_code, '')
1403
1404 def test_function_not_found(self):
1405 """
1406 Test that AssertError is raised when function info in not found.
1407 :return:
1408 """
1409 data = '''
1410My test
1411depends_on:DEP1
1412func1:0
1413'''
1414 data_f = StringIOWrapper('test_suite_ut.data', data)
1415 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1416 func_info = {'test_func2': (1, ('int',))}
1417 suite_deps = []
1418 self.assertRaises(AssertionError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
1419
1420 def test_different_func_args(self):
1421 """
1422 Test that AssertError is raised when no. of parameters and function args differ.
1423 :return:
1424 """
1425 data = '''
1426My test
1427depends_on:DEP1
1428func1:0
1429'''
1430 data_f = StringIOWrapper('test_suite_ut.data', data)
1431 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1432 func_info = {'test_func2': (1, ('int','hex'))}
1433 suite_deps = []
1434 self.assertRaises(AssertionError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
1435
1436 def test_output(self):
1437 """
1438 Test that intermediate data file is written with expected data.
1439 :return:
1440 """
1441 data = '''
1442My test 1
1443depends_on:DEP1
1444func1:0:0xfa:MACRO1:MACRO2
1445
1446My test 2
1447depends_on:DEP1:DEP2
1448func2:"yahoo":88:MACRO1
1449'''
1450 data_f = StringIOWrapper('test_suite_ut.data', data)
1451 out_data_f = StringIOWrapper('test_suite_ut.datax', '')
1452 func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')), 'test_func2': (1, ('char*', 'int', 'int'))}
1453 suite_deps = []
1454 dep_check_code, expression_code = gen_from_test_data(data_f, out_data_f, func_info, suite_deps)
1455 expected_dep_check_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001456 case 0:
1457 {
Azim Khan599cd242017-07-06 17:34:27 +01001458#if defined(DEP1)
Azim Khand61b8372017-07-10 11:54:01 +01001459 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001460#else
Azim Khand61b8372017-07-10 11:54:01 +01001461 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001462#endif
Azim Khand61b8372017-07-10 11:54:01 +01001463 }
1464 break;
1465 case 1:
1466 {
Azim Khan599cd242017-07-06 17:34:27 +01001467#if defined(DEP2)
Azim Khand61b8372017-07-10 11:54:01 +01001468 ret = DEPENDENCY_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001469#else
Azim Khand61b8372017-07-10 11:54:01 +01001470 ret = DEPENDENCY_NOT_SUPPORTED;
Azim Khan599cd242017-07-06 17:34:27 +01001471#endif
Azim Khand61b8372017-07-10 11:54:01 +01001472 }
1473 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001474 expecrted_data = '''My test 1
1475depends_on:0
14760:int:0:int:0xfa:exp:0:exp:1
1477
1478My test 2
1479depends_on:0:1
14801:char*:"yahoo":int:88:exp:0
1481
1482'''
1483 expected_expression_code = '''
Azim Khand61b8372017-07-10 11:54:01 +01001484 case 0:
1485 {
1486 *out_value = MACRO1;
1487 }
1488 break;
1489 case 1:
1490 {
1491 *out_value = MACRO2;
1492 }
1493 break;'''
Azim Khan599cd242017-07-06 17:34:27 +01001494 self.assertEqual(dep_check_code, expected_dep_check_code)
1495 self.assertEqual(out_data_f.getvalue(), expecrted_data)
1496 self.assertEqual(expression_code, expected_expression_code)
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001497
1498
Azim Khan4b543232017-06-30 09:35:21 +01001499if __name__=='__main__':
Azim Khan5e2ac1f2017-07-03 13:58:20 +01001500 unittest_main()