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