1#!/usr/bin/env python3 2# 3# Copyright (c) 2016, The OpenThread Authors. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 3. Neither the name of the copyright holder nor the 14# names of its contributors may be used to endorse or promote products 15# derived from this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28# 29 30import io 31import math 32import random 33import struct 34import unittest 35 36import common 37import config 38import network_data 39 40 41def convert_route_to_bytearray(route): 42 return struct.pack(">HB", route.border_router_16, ((route.prf & 0x03) << 6)) 43 44 45def convert_routes_to_bytearray(routes): 46 data = bytearray() 47 for route in routes: 48 data += convert_route_to_bytearray(route) 49 50 return data 51 52 53def convert_has_route_to_bytearray(has_route): 54 return convert_routes_to_bytearray(has_route.routes) 55 56 57def convert_border_router_to_bytearray(border_router): 58 data = struct.pack( 59 ">HBB", 60 border_router.border_router_16, 61 (border_router.o & 0x01) | ((border_router.r & 0x01) << 1) | ((border_router.c & 0x01) << 2) | 62 ((border_router.d & 0x01) << 3) | ((border_router.s & 0x01) << 4) | ((border_router.p & 0x01) << 5) | 63 ((border_router.prf & 0x03) << 6), 64 ((border_router.n & 0x01) << 7), 65 ) 66 67 return data 68 69 70def convert_lowpan_id_to_bytearray(lowpan_id): 71 return bytearray([lowpan_id.cid | (lowpan_id.c << 4), lowpan_id.context_length]) 72 73 74def convert_prefix_sub_tlvs_to_bytearray(sub_tlvs): 75 data = bytearray() 76 77 for sub_tlv in sub_tlvs: 78 if isinstance(sub_tlv, network_data.HasRoute): 79 value = convert_has_route_to_bytearray(sub_tlv) 80 _type = sub_tlv.stable | ((0 & 0x7F) << 1) 81 82 elif isinstance(sub_tlv, network_data.BorderRouter): 83 value = convert_border_router_to_bytearray(sub_tlv) 84 _type = sub_tlv.stable | ((2 & 0x7F) << 1) 85 86 elif isinstance(sub_tlv, network_data.LowpanId): 87 value = convert_lowpan_id_to_bytearray(sub_tlv) 88 _type = sub_tlv.stable | ((3 & 0x7F) << 1) 89 90 else: 91 raise ValueError 92 93 data += bytearray([_type, len(value)]) + value 94 95 return data 96 97 98def convert_server_to_bytearray(server): 99 return struct.pack(">H", server.server_16) + server.server_data 100 101 102def convert_service_sub_tlvs_to_bytearray(sub_tlvs): 103 data = bytearray() 104 105 for sub_tlv in sub_tlvs: 106 if isinstance(sub_tlv, network_data.Server): 107 value = convert_server_to_bytearray(sub_tlv) 108 _type = sub_tlv.stable | ((6 & 0x7F) << 1) 109 110 else: 111 raise ValueError 112 113 data += bytearray([_type, len(value)]) + value 114 115 return data 116 117 118def convert_service_to_bytearray(service): 119 return (struct.pack( 120 ">BLB", 121 ((service.t & 0x01) << 7) | ((service.id) & 0x0F), 122 service.enterprise_number, 123 service.service_data_length, 124 ) + service.service_data + convert_service_sub_tlvs_to_bytearray(service.sub_tlvs)) 125 126 127def any_border_router_16(): 128 return random.getrandbits(16) 129 130 131def any_prf(): 132 return random.getrandbits(2) 133 134 135def any_route(): 136 return network_data.Route(any_border_router_16(), any_prf()) 137 138 139def any_routes(count=None): 140 if count is None: 141 count = random.randint(0, 16) 142 143 return [any_route() for _ in range(6)] 144 145 146def any_has_route(): 147 return network_data.HasRoute(any_routes(), any_stable()) 148 149 150def any_domain_id(): 151 return random.getrandbits(8) 152 153 154def any_prefix_length(): 155 return random.randint(1, 16) 156 157 158def any_prefix(prefix_length=None): 159 if prefix_length is None: 160 prefix_length = any_prefix_length() 161 162 return bytearray([random.getrandbits(8) for _ in range(int(math.ceil(prefix_length / 8)))]) 163 164 165def any_p(): 166 return random.getrandbits(1) 167 168 169def any_s(): 170 return random.getrandbits(1) 171 172 173def any_d(): 174 return random.getrandbits(1) 175 176 177def any_c(): 178 return random.getrandbits(1) 179 180 181def any_r(): 182 return random.getrandbits(1) 183 184 185def any_o(): 186 return random.getrandbits(1) 187 188 189def any_n(): 190 return random.getrandbits(1) 191 192 193def any_cid(): 194 return random.getrandbits(4) 195 196 197def any_context_length(): 198 return random.getrandbits(8) 199 200 201def any_border_router(): 202 return network_data.BorderRouter( 203 any_border_router_16(), 204 any_prf(), 205 any_p(), 206 any_s(), 207 any_d(), 208 any_c(), 209 any_r(), 210 any_o(), 211 any_n(), 212 any_stable(), 213 ) 214 215 216def any_lowpan_id(): 217 return network_data.LowpanId(any_c(), any_cid(), any_context_length(), any_stable()) 218 219 220def any_prefix_sub_tlvs(): 221 creator = [any_has_route, any_border_router, any_lowpan_id] 222 223 sub_tlvs = [] 224 225 for _id in range(random.randint(1, 1)): 226 c = random.choice(creator) 227 sub_tlvs.append(c()) 228 229 return sub_tlvs 230 231 232def any_t(): 233 return random.getrandbits(1) 234 235 236def any_id(): 237 return random.getrandbits(4) 238 239 240def any_enterprise_number(): 241 return random.getrandbits(32) 242 243 244def any_service_data_length(): 245 return random.getrandbits(8) 246 247 248def any_service_data(data_length=None): 249 if data_length is None: 250 data_length = random.randint(0, 16) 251 252 return bytearray([random.getrandbits(8) for _ in range(data_length)]) 253 254 255def any_server_16(): 256 return random.getrandbits(16) 257 258 259def any_server_data(data_length=None): 260 if data_length is None: 261 data_length = random.randint(0, 32) 262 263 return bytearray([random.getrandbits(8) for _ in range(data_length)]) 264 265 266def any_server(): 267 return network_data.Server(any_server_16(), any_server_data(), any_stable()) 268 269 270def any_service_sub_tlvs(): 271 creator = [any_server] 272 273 sub_tlvs = [] 274 275 for _id in range(random.randint(0, 16)): 276 c = random.choice(creator) 277 sub_tlvs.append(c()) 278 279 return sub_tlvs 280 281 282def any_stable(): 283 return random.getrandbits(1) 284 285 286class TestRoute(unittest.TestCase): 287 288 def test_should_return_border_router_16_value_when_border_router_16_property_is_called(self): 289 # GIVEN 290 border_router_16 = any_border_router_16() 291 292 route = network_data.Route(border_router_16, any_prf()) 293 294 # WHEN 295 actual_border_router_16 = route.border_router_16 296 297 # THEN 298 self.assertEqual(border_router_16, actual_border_router_16) 299 300 def test_should_return_prf_value_when_prf_property_is_called(self): 301 # GIVEN 302 prf = any_prf() 303 304 route = network_data.Route(any_border_router_16(), prf) 305 306 # WHEN 307 actual_prf = route.prf 308 309 # THEN 310 self.assertEqual(prf, actual_prf) 311 312 313class TestRouteFactory(unittest.TestCase): 314 315 def test_should_create_Route_from_bytearray_when_parse_method_is_called(self): 316 # GIVEN 317 border_router_16 = any_border_router_16() 318 prf = any_prf() 319 320 factory = network_data.RouteFactory() 321 322 data = convert_route_to_bytearray(network_data.Route(border_router_16, prf)) 323 324 # WHEN 325 actual_route = factory.parse(io.BytesIO(data), None) 326 327 # THEN 328 self.assertTrue(isinstance(actual_route, network_data.Route)) 329 self.assertEqual(border_router_16, actual_route.border_router_16) 330 self.assertEqual(prf, actual_route.prf) 331 332 333class TestRoutesFactory(unittest.TestCase): 334 335 def test_should_create_Route_list_from_bytearray_when_parse_method_is_called(self): 336 # GIVEN 337 routes = any_routes() 338 339 factory = network_data.RoutesFactory(network_data.RouteFactory()) 340 341 data = convert_routes_to_bytearray(routes) 342 # WHEN 343 actual_routes = factory.parse(io.BytesIO(data), None) 344 345 # THEN 346 self.assertTrue(isinstance(actual_routes, list)) 347 self.assertEqual(routes, actual_routes) 348 349 350class TestHasRoute(unittest.TestCase): 351 352 def test_should_return_routes_value_when_routes_property_is_called(self): 353 # GIVEN 354 routes = any_routes() 355 356 has_route = network_data.HasRoute(routes, any_stable()) 357 358 # WHEN 359 actual_routes = has_route.routes 360 361 # THEN 362 self.assertEqual(routes, actual_routes) 363 364 def test_should_return_stable_value_when_stable_property_is_called(self): 365 # GIVEN 366 stable = any_stable() 367 368 has_route = network_data.HasRoute(any_routes(), stable) 369 370 # WHEN 371 actual_stable = has_route.stable 372 373 # THEN 374 self.assertEqual(stable, actual_stable) 375 376 377class TestHasRouteFactory(unittest.TestCase): 378 379 def test_should_create_HasRoute_from_bytearray_when_parse_method_is_called(self): 380 # GIVEN 381 routes = any_routes() 382 stable = any_stable() 383 384 factory = network_data.HasRouteFactory(network_data.RoutesFactory(network_data.RouteFactory())) 385 386 data = convert_routes_to_bytearray(routes) 387 388 message_info = common.MessageInfo() 389 message_info.stable = stable 390 391 # WHEN 392 actual_has_route = factory.parse(io.BytesIO(data), message_info) 393 394 # THEN 395 self.assertTrue(isinstance(actual_has_route, network_data.HasRoute)) 396 self.assertEqual(routes, actual_has_route.routes) 397 self.assertEqual(stable, actual_has_route.stable) 398 399 400class TestPrefix(unittest.TestCase): 401 402 def test_should_return_domain_id_value_when_domain_id_property_is_called(self): 403 # GIVEN 404 domain_id = any_domain_id() 405 406 prefix = network_data.Prefix( 407 domain_id, 408 any_prefix_length(), 409 any_prefix(), 410 any_prefix_sub_tlvs(), 411 any_stable(), 412 ) 413 414 # WHEN 415 actual_domain_id = prefix.domain_id 416 417 # THEN 418 self.assertEqual(domain_id, actual_domain_id) 419 420 def test_should_return_prefix_length_value_when_prefix_length_property_is_called(self): 421 # GIVEN 422 prefix_length = any_prefix_length() 423 424 prefix = network_data.Prefix( 425 any_domain_id(), 426 prefix_length, 427 any_prefix(), 428 any_prefix_sub_tlvs(), 429 any_stable(), 430 ) 431 432 # WHEN 433 actual_prefix_length = prefix.prefix_length 434 435 # THEN 436 self.assertEqual(prefix_length, actual_prefix_length) 437 438 def test_should_return_prefix_value_when_prefix_property_is_called(self): 439 # GIVEN 440 prefix = any_prefix() 441 442 prefix_obj = network_data.Prefix( 443 any_domain_id(), 444 any_prefix_length(), 445 prefix, 446 any_prefix_sub_tlvs(), 447 any_stable(), 448 ) 449 450 # WHEN 451 actual_prefix = prefix_obj.prefix 452 453 # THEN 454 self.assertEqual(prefix, actual_prefix) 455 456 def test_should_return_sub_tlvs_value_when_sub_tlvs_property_is_called(self): 457 # GIVEN 458 sub_tlvs = any_prefix_sub_tlvs() 459 460 prefix_obj = network_data.Prefix( 461 any_domain_id(), 462 any_prefix_length(), 463 any_prefix(), 464 sub_tlvs, 465 any_stable(), 466 ) 467 468 # WHEN 469 actual_sub_tlvs = prefix_obj.sub_tlvs 470 471 # THEN 472 self.assertEqual(sub_tlvs, actual_sub_tlvs) 473 474 def test_should_return_stable_value_when_stable_property_is_called(self): 475 # GIVEN 476 stable = any_stable() 477 478 prefix_obj = network_data.Prefix( 479 any_domain_id(), 480 any_prefix_length(), 481 any_prefix(), 482 any_prefix_sub_tlvs(), 483 stable, 484 ) 485 486 # WHEN 487 actual_stable = prefix_obj.stable 488 489 # THEN 490 self.assertEqual(stable, actual_stable) 491 492 493class TestPrefixSubTlvsFactory(unittest.TestCase): 494 495 def test_should_create_SubTlvs_from_bytearray_when_parse_method_is_called(self): 496 # GIVEN 497 sub_tlvs = any_prefix_sub_tlvs() 498 499 factory = network_data.PrefixSubTlvsFactory(config.create_default_network_data_prefix_sub_tlvs_factories()) 500 501 data = convert_prefix_sub_tlvs_to_bytearray(sub_tlvs) 502 503 # WHEN 504 actual_sub_tlvs = factory.parse(io.BytesIO(data), common.MessageInfo()) 505 506 # THEN 507 self.assertTrue(isinstance(actual_sub_tlvs, list)) 508 self.assertEqual(sub_tlvs, actual_sub_tlvs) 509 510 511class TestPrefixFactory(unittest.TestCase): 512 513 def test_should_create_Prefix_from_bytearray_when_parse_method_is_called(self): 514 # GIVEN 515 domain_id = any_domain_id() 516 prefix_length = any_prefix_length() 517 prefix = any_prefix(prefix_length) 518 sub_tlvs = any_prefix_sub_tlvs() 519 520 factory = network_data.PrefixFactory(config.create_default_network_data_prefix_sub_tlvs_factory()) 521 522 data = (bytearray([domain_id, prefix_length]) + prefix + convert_prefix_sub_tlvs_to_bytearray(sub_tlvs)) 523 524 message_info = common.MessageInfo() 525 526 # WHEN 527 actual_prefix = factory.parse(io.BytesIO(data), message_info) 528 529 # THEN 530 self.assertTrue(isinstance(actual_prefix, network_data.Prefix)) 531 self.assertEqual(domain_id, actual_prefix.domain_id) 532 self.assertEqual(prefix_length, actual_prefix.prefix_length) 533 self.assertEqual(prefix, actual_prefix.prefix) 534 self.assertEqual(sub_tlvs, actual_prefix.sub_tlvs) 535 536 537class TestBorderRouter(unittest.TestCase): 538 539 def test_should_return_border_router_16_value_when_border_router_16_property_is_called(self): 540 # GIVEN 541 border_router_16 = any_border_router_16() 542 543 border_router = network_data.BorderRouter( 544 border_router_16, 545 any_prf(), 546 any_p(), 547 any_s(), 548 any_d(), 549 any_c(), 550 any_r(), 551 any_o(), 552 any_n(), 553 any_stable(), 554 ) 555 556 # WHEN 557 actual_border_router_16 = border_router.border_router_16 558 559 # THEN 560 self.assertEqual(border_router_16, actual_border_router_16) 561 562 def test_should_return_prf_value_when_prf_property_is_called(self): 563 # GIVEN 564 prf = any_prf() 565 566 border_router = network_data.BorderRouter( 567 any_border_router_16(), 568 prf, 569 any_p(), 570 any_s(), 571 any_d(), 572 any_c(), 573 any_r(), 574 any_o(), 575 any_n(), 576 any_stable(), 577 ) 578 579 # WHEN 580 actual_prf = border_router.prf 581 582 # THEN 583 self.assertEqual(prf, actual_prf) 584 585 def test_should_return_p_value_when_p_property_is_called(self): 586 # GIVEN 587 p = any_p() 588 589 border_router = network_data.BorderRouter( 590 any_border_router_16(), 591 any_prf(), 592 p, 593 any_s(), 594 any_d(), 595 any_c(), 596 any_r(), 597 any_o(), 598 any_n(), 599 any_stable(), 600 ) 601 602 # WHEN 603 actual_p = border_router.p 604 605 # THEN 606 self.assertEqual(p, actual_p) 607 608 def test_should_return_s_value_when_s_property_is_called(self): 609 # GIVEN 610 s = any_s() 611 612 border_router = network_data.BorderRouter( 613 any_border_router_16(), 614 any_prf(), 615 any_p(), 616 s, 617 any_d(), 618 any_c(), 619 any_r(), 620 any_o(), 621 any_n(), 622 any_stable(), 623 ) 624 625 # WHEN 626 actual_s = border_router.s 627 628 # THEN 629 self.assertEqual(s, actual_s) 630 631 def test_should_return_d_value_when_d_property_is_called(self): 632 # GIVEN 633 d = any_d() 634 635 border_router = network_data.BorderRouter( 636 any_border_router_16(), 637 any_prf(), 638 any_p(), 639 any_s(), 640 d, 641 any_c(), 642 any_r(), 643 any_o(), 644 any_n(), 645 any_stable(), 646 ) 647 648 # WHEN 649 actual_d = border_router.d 650 651 # THEN 652 self.assertEqual(d, actual_d) 653 654 def test_should_return_c_value_when_c_property_is_called(self): 655 # GIVEN 656 c = any_c() 657 658 border_router = network_data.BorderRouter( 659 any_border_router_16(), 660 any_prf(), 661 any_p(), 662 any_s(), 663 any_d(), 664 c, 665 any_r(), 666 any_o(), 667 any_n(), 668 any_stable(), 669 ) 670 671 # WHEN 672 actual_c = border_router.c 673 674 # THEN 675 self.assertEqual(c, actual_c) 676 677 def test_should_return_r_value_when_r_property_is_called(self): 678 # GIVEN 679 r = any_r() 680 681 border_router = network_data.BorderRouter( 682 any_border_router_16(), 683 any_prf(), 684 any_p(), 685 any_s(), 686 any_d(), 687 any_c(), 688 r, 689 any_o(), 690 any_n(), 691 any_stable(), 692 ) 693 694 # WHEN 695 actual_r = border_router.r 696 697 # THEN 698 self.assertEqual(r, actual_r) 699 700 def test_should_return_o_value_when_o_property_is_called(self): 701 # GIVEN 702 o = any_o() 703 704 border_router = network_data.BorderRouter( 705 any_border_router_16(), 706 any_prf(), 707 any_p(), 708 any_s(), 709 any_d(), 710 any_c(), 711 any_r(), 712 o, 713 any_n(), 714 any_stable(), 715 ) 716 717 # WHEN 718 actual_o = border_router.o 719 720 # THEN 721 self.assertEqual(o, actual_o) 722 723 def test_should_return_n_value_when_n_property_is_called(self): 724 # GIVEN 725 n = any_n() 726 727 border_router = network_data.BorderRouter( 728 any_border_router_16(), 729 any_prf(), 730 any_p(), 731 any_s(), 732 any_d(), 733 any_c(), 734 any_r(), 735 any_o(), 736 n, 737 any_stable(), 738 ) 739 740 # WHEN 741 actual_n = border_router.n 742 743 # THEN 744 self.assertEqual(n, actual_n) 745 746 def test_should_return_stable_value_when_stable_property_is_called(self): 747 # GIVEN 748 stable = any_stable() 749 750 border_router = network_data.BorderRouter( 751 any_border_router_16(), 752 any_prf(), 753 any_p(), 754 any_s(), 755 any_d(), 756 any_c(), 757 any_r(), 758 any_o(), 759 any_n(), 760 stable, 761 ) 762 763 # WHEN 764 actual_stable = border_router.stable 765 766 # THEN 767 self.assertEqual(stable, actual_stable) 768 769 770class TestBorderRouterFactory(unittest.TestCase): 771 772 def test_should_create_BorderRouter_from_bytearray_when_parse_method_is_called(self): 773 # GIVEN 774 border_router_16 = any_border_router_16() 775 prf = any_prf() 776 p = any_p() 777 s = any_s() 778 d = any_d() 779 c = any_c() 780 r = any_r() 781 o = any_o() 782 n = any_n() 783 stable = any_stable() 784 785 factory = network_data.BorderRouterFactory() 786 787 data = convert_border_router_to_bytearray( 788 network_data.BorderRouter(border_router_16, prf, p, s, d, c, r, o, n, stable)) 789 790 message_info = common.MessageInfo() 791 message_info.stable = stable 792 793 # WHEN 794 actual_border_router = factory.parse(io.BytesIO(data), message_info) 795 796 # THEN 797 self.assertTrue(isinstance(actual_border_router, network_data.BorderRouter)) 798 self.assertEqual(border_router_16, actual_border_router.border_router_16) 799 self.assertEqual(prf, actual_border_router.prf) 800 self.assertEqual(p, actual_border_router.p) 801 self.assertEqual(s, actual_border_router.s) 802 self.assertEqual(d, actual_border_router.d) 803 self.assertEqual(c, actual_border_router.c) 804 self.assertEqual(r, actual_border_router.r) 805 self.assertEqual(o, actual_border_router.o) 806 self.assertEqual(n, actual_border_router.n) 807 self.assertEqual(stable, actual_border_router.stable) 808 809 810class TestLowpanId(unittest.TestCase): 811 812 def test_should_return_c_value_when_c_property_is_called(self): 813 # GIVEN 814 c = any_c() 815 816 lowpan_id = network_data.LowpanId(c, any_cid(), any_context_length(), any_stable()) 817 818 # WHEN 819 actual_c = lowpan_id.c 820 821 # THEN 822 self.assertEqual(c, actual_c) 823 824 def test_should_return_cid_value_when_cid_property_is_called(self): 825 # GIVEN 826 cid = any_cid() 827 828 lowpan_id = network_data.LowpanId(any_c(), cid, any_context_length(), any_stable()) 829 830 # WHEN 831 actual_cid = lowpan_id.cid 832 833 # THEN 834 self.assertEqual(cid, actual_cid) 835 836 def test_should_return_context_length_value_when_context_length_property_is_called(self): 837 # GIVEN 838 context_length = any_context_length() 839 840 lowpan_id = network_data.LowpanId(any_c(), any_cid(), context_length, any_stable()) 841 842 # WHEN 843 actual_context_length = lowpan_id.context_length 844 845 # THEN 846 self.assertEqual(context_length, actual_context_length) 847 848 def test_should_return_stable_value_when_stable_property_is_called(self): 849 # GIVEN 850 stable = any_stable() 851 852 lowpan_id = network_data.LowpanId(any_c(), any_cid(), any_context_length(), stable) 853 854 # WHEN 855 actual_stable = lowpan_id.stable 856 857 # THEN 858 self.assertEqual(stable, actual_stable) 859 860 861class TestLowpanIdFactory(unittest.TestCase): 862 863 def test_should_create_LowpanId_from_bytearray_when_parse_method_is_called(self): 864 # GIVEN 865 c = any_c() 866 cid = any_cid() 867 context_length = any_context_length() 868 stable = any_stable() 869 870 factory = network_data.LowpanIdFactory() 871 872 data = convert_lowpan_id_to_bytearray(network_data.LowpanId(c, cid, context_length, stable)) 873 874 message_info = common.MessageInfo() 875 message_info.stable = stable 876 877 # WHEN 878 actual_lowpan_id = factory.parse(io.BytesIO(data), message_info) 879 880 # THEN 881 self.assertTrue(isinstance(actual_lowpan_id, network_data.LowpanId)) 882 self.assertEqual(c, actual_lowpan_id.c) 883 self.assertEqual(cid, actual_lowpan_id.cid) 884 self.assertEqual(context_length, actual_lowpan_id.context_length) 885 886 887class TestService(unittest.TestCase): 888 889 def test_should_return_t_value_when_t_property_is_called(self): 890 # GIVEN 891 t = any_t() 892 893 service = network_data.Service( 894 t, 895 any_id(), 896 any_enterprise_number(), 897 any_service_data_length(), 898 any_service_data(), 899 any_service_sub_tlvs(), 900 any_stable(), 901 ) 902 903 # WHEN 904 actual_t = service.t 905 906 # THEN 907 self.assertEqual(t, actual_t) 908 909 def test_should_return_id_value_when_id_property_is_called(self): 910 # GIVEN 911 _id = any_id() 912 913 service = network_data.Service( 914 any_t(), 915 _id, 916 any_enterprise_number(), 917 any_service_data_length(), 918 any_service_data(), 919 any_service_sub_tlvs(), 920 any_stable(), 921 ) 922 923 # WHEN 924 actual_id = service.id 925 926 # THEN 927 self.assertEqual(_id, actual_id) 928 929 def test_should_return_enterprise_number_value_when_enterprise_number_property_is_called(self): 930 # GIVEN 931 enterprise_number = any_enterprise_number() 932 933 service = network_data.Service( 934 any_t(), 935 any_id(), 936 enterprise_number, 937 any_service_data_length(), 938 any_service_data(), 939 any_service_sub_tlvs(), 940 any_stable(), 941 ) 942 943 # WHEN 944 actual_enterprise_number = service.enterprise_number 945 946 # THEN 947 self.assertEqual(enterprise_number, actual_enterprise_number) 948 949 def test_should_return_service_data_length_value_when_service_data_length_property_is_called(self): 950 # GIVEN 951 service_data_length = any_service_data_length() 952 953 service = network_data.Service( 954 any_t(), 955 any_id(), 956 any_enterprise_number(), 957 service_data_length, 958 any_service_data(), 959 any_service_sub_tlvs(), 960 any_stable(), 961 ) 962 963 # WHEN 964 actual_service_data_length = service.service_data_length 965 966 # THEN 967 self.assertEqual(service_data_length, actual_service_data_length) 968 969 def test_should_return_service_data_value_when_service_data_property_is_called(self): 970 # GIVEN 971 service_data = any_service_data() 972 973 service = network_data.Service( 974 any_t(), 975 any_id(), 976 any_enterprise_number(), 977 any_service_data_length(), 978 service_data, 979 any_service_sub_tlvs(), 980 any_stable(), 981 ) 982 983 # WHEN 984 actual_service_data = service.service_data 985 986 # THEN 987 self.assertEqual(service_data, actual_service_data) 988 989 def test_should_return_sub_tlvs_value_when_sub_tlvs_property_is_called(self): 990 # GIVEN 991 sub_tlvs = any_service_sub_tlvs() 992 993 service = network_data.Service( 994 any_t(), 995 any_id(), 996 any_enterprise_number(), 997 any_service_data_length(), 998 any_service_data(), 999 sub_tlvs, 1000 any_stable(), 1001 ) 1002 1003 # WHEN 1004 actual_sub_tlvs = service.sub_tlvs 1005 1006 # THEN 1007 self.assertEqual(sub_tlvs, actual_sub_tlvs) 1008 1009 def test_should_return_stable_value_when_stable_property_is_called(self): 1010 # GIVEN 1011 stable = any_stable() 1012 1013 service = network_data.Service( 1014 any_t(), 1015 any_id(), 1016 any_enterprise_number(), 1017 any_service_data_length(), 1018 any_service_data(), 1019 any_service_sub_tlvs(), 1020 stable, 1021 ) 1022 1023 # WHEN 1024 actual_stable = service.stable 1025 1026 # THEN 1027 self.assertEqual(stable, actual_stable) 1028 1029 1030class TestServiceSubTlvsFactory(unittest.TestCase): 1031 1032 def test_should_create_SubTlvs_from_bytearray_when_parse_method_is_called(self): 1033 # GIVEN 1034 sub_tlvs = any_service_sub_tlvs() 1035 1036 factory = network_data.ServiceSubTlvsFactory(config.create_default_network_data_service_sub_tlvs_factories()) 1037 1038 data = convert_service_sub_tlvs_to_bytearray(sub_tlvs) 1039 1040 # WHEN 1041 actual_sub_tlvs = factory.parse(io.BytesIO(data), common.MessageInfo()) 1042 1043 # THEN 1044 self.assertTrue(isinstance(actual_sub_tlvs, list)) 1045 self.assertEqual(sub_tlvs, actual_sub_tlvs) 1046 1047 1048class TestServiceFactory(unittest.TestCase): 1049 1050 def test_should_create_Service_from_bytearray_when_parse_method_is_called(self): 1051 # GIVEN 1052 t = any_t() 1053 _id = any_id() 1054 enterprise_number = any_enterprise_number() 1055 service_data_length = any_service_data_length() 1056 service_data = any_service_data(service_data_length) 1057 sub_tlvs = any_service_sub_tlvs() 1058 stable = any_stable() 1059 1060 factory = network_data.ServiceFactory(config.create_default_network_data_service_sub_tlvs_factory()) 1061 1062 data = convert_service_to_bytearray( 1063 network_data.Service( 1064 t, 1065 _id, 1066 enterprise_number, 1067 service_data_length, 1068 service_data, 1069 sub_tlvs, 1070 stable, 1071 )) 1072 1073 message_info = common.MessageInfo() 1074 message_info.stable = stable 1075 1076 # WHEN 1077 actual_service = factory.parse(io.BytesIO(data), message_info) 1078 1079 # THEN 1080 self.assertTrue(isinstance(actual_service, network_data.Service)) 1081 self.assertEqual(t, actual_service.t) 1082 self.assertEqual(_id, actual_service.id) 1083 self.assertEqual(enterprise_number, actual_service.enterprise_number) 1084 self.assertEqual(service_data_length, actual_service.service_data_length) 1085 self.assertEqual(service_data, actual_service.service_data) 1086 self.assertEqual(sub_tlvs, actual_service.sub_tlvs) 1087 1088 1089class TestServer(unittest.TestCase): 1090 1091 def test_should_return_server_16_value_when_server_16_property_is_called(self): 1092 # GIVEN 1093 server_16 = any_server_16() 1094 1095 server = network_data.Server(server_16, any_server_data(), any_stable()) 1096 1097 # WHEN 1098 actual_server_16 = server.server_16 1099 1100 # THEN 1101 self.assertEqual(server_16, actual_server_16) 1102 1103 def test_should_return_server_data_value_when_server_data_property_is_called(self): 1104 # GIVEN 1105 server_data = any_server_data() 1106 1107 server = network_data.Server(any_server_16(), server_data, any_stable()) 1108 1109 # WHEN 1110 actual_server_data = server.server_data 1111 1112 # THEN 1113 self.assertEqual(server_data, actual_server_data) 1114 1115 def test_should_return_stable_value_when_stable_property_is_called(self): 1116 # GIVEN 1117 stable = any_stable() 1118 1119 server = network_data.Server(any_server_16(), any_server_data(), stable) 1120 1121 # WHEN 1122 actual_stable = server.stable 1123 1124 # THEN 1125 self.assertEqual(stable, actual_stable) 1126 1127 1128class TestServerFactory(unittest.TestCase): 1129 1130 def test_should_create_Server_from_bytearray_when_parse_method_is_called(self): 1131 # GIVEN 1132 server_16 = any_server_16() 1133 server_data = any_server_data() 1134 stable = any_stable() 1135 1136 factory = network_data.ServerFactory() 1137 1138 data = convert_server_to_bytearray(network_data.Server(server_16, server_data, stable)) 1139 1140 message_info = common.MessageInfo() 1141 message_info.stable = stable 1142 1143 # WHEN 1144 actual_server = factory.parse(io.BytesIO(data), message_info) 1145 1146 # THEN 1147 self.assertTrue(isinstance(actual_server, network_data.Server)) 1148 self.assertEqual(server_16, actual_server.server_16) 1149 self.assertEqual(server_data, actual_server.server_data) 1150 self.assertEqual(stable, actual_server.stable) 1151 1152 1153if __name__ == "__main__": 1154 unittest.main() 1155