Azim Khan | 4b54323 | 2017-06-30 09:35:21 +0100 | [diff] [blame^] | 1 | """ |
| 2 | mbed TLS |
| 3 | Copyright (c) 2017 ARM Limited |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | """ |
| 17 | from StringIO import StringIO |
| 18 | from unittest import TestCase, main as unittest_main |
| 19 | from mock import patch |
| 20 | from generate_code import * |
| 21 | |
| 22 | |
| 23 | """ |
| 24 | Unit tests for generate_code.py |
| 25 | """ |
| 26 | |
| 27 | |
| 28 | class 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 | |
| 99 | class 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 | |
| 154 | class 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 = ''' |
| 167 | void 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 = ''' |
| 184 | void test_a_wrapper( void ** params ) |
| 185 | { |
| 186 | |
| 187 | int 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 = ''' |
| 201 | void test_a_wrapper( void ** params ) |
| 202 | { |
| 203 | (void)params; |
| 204 | |
| 205 | test_a( ); |
| 206 | } |
| 207 | ''' |
| 208 | self.assertEqual(code, expected) |
| 209 | |
| 210 | |
| 211 | class 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 | |
| 243 | class 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 | |
| 280 | class 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 | |
| 340 | class 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 | |
| 387 | class 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 | |
| 421 | class 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 | |
| 479 | class 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 = ''' |
| 490 | No |
| 491 | test |
| 492 | function |
| 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 = ''' |
| 503 | void 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 = ''' |
| 518 | void 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 = ''' |
| 544 | void 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" |
| 561 | void test_func() |
| 562 | { |
| 563 | ba ba black sheep |
| 564 | have you any wool |
| 565 | exit: |
| 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 = ''' |
| 589 | void func() |
| 590 | { |
| 591 | ba ba black sheep |
| 592 | have you any wool |
| 593 | exit: |
| 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" |
| 603 | void test_func() |
| 604 | { |
| 605 | ba ba black sheep |
| 606 | have you any wool |
| 607 | exit: |
| 608 | yes sir yes sir |
| 609 | 3 bags full |
| 610 | } |
| 611 | ''' |
| 612 | self.assertEqual(code, expected) |
| 613 | |
| 614 | |
| 615 | class 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 */ |
| 733 | void func1() |
| 734 | { |
| 735 | } |
| 736 | /* END_CASE */ |
| 737 | |
| 738 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */ |
| 739 | void 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" |
| 773 | void test_func1() |
| 774 | { |
| 775 | exit: |
| 776 | ;; |
| 777 | } |
| 778 | |
| 779 | void 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" |
| 790 | void test_func2() |
| 791 | { |
| 792 | exit: |
| 793 | ;; |
| 794 | } |
| 795 | |
| 796 | void 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 */ |
| 826 | void func() |
| 827 | { |
| 828 | } |
| 829 | /* END_CASE */ |
| 830 | |
| 831 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */ |
| 832 | void func() |
| 833 | { |
| 834 | } |
| 835 | /* END_CASE */ |
| 836 | ''' |
| 837 | s = StringIOWrapper('test_suite_ut.function', data) |
| 838 | self.assertRaises(AssertionError, parse_functions, s) |
| 839 | |
| 840 | |
| 841 | if __name__=='__main__': |
| 842 | unittest_main() |