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#
29import random
30import struct
31import unittest
32import ipaddress
33
34import common
35
36
37def any_eui64():
38    return bytearray([random.getrandbits(8) for _ in range(8)])
39
40
41def any_rloc16_int():
42    return random.getrandbits(16)
43
44
45def any_rloc16_bytearray():
46    return bytearray([random.getrandbits(8) for _ in range(2)])
47
48
49def any_ipv6_address():
50    return bytearray([random.getrandbits(8) for _ in range(16)])
51
52
53class TestMessageInfo(unittest.TestCase):
54
55    def test_should_return_source_ipv6_value_when_source_ipv6_property_is_called(self):
56        # GIVEN
57        source_ipv6 = any_ipv6_address()
58
59        message_info = common.MessageInfo()
60        message_info.source_ipv6 = source_ipv6
61
62        # WHEN
63        actual_source_ipv6 = message_info.source_ipv6
64
65        # THEN
66        self.assertEqual(ipaddress.ip_address(bytes(source_ipv6)), actual_source_ipv6)
67
68    def test_should_return_destination_ipv6_value_when_destination_ipv6_property_is_called(self):
69        # GIVEN
70        destination_ipv6 = any_ipv6_address()
71
72        message_info = common.MessageInfo()
73        message_info.destination_ipv6 = destination_ipv6
74
75        # WHEN
76        actual_destination_ipv6 = message_info.destination_ipv6
77
78        # THEN
79        self.assertEqual(
80            ipaddress.ip_address(bytes(destination_ipv6)),
81            actual_destination_ipv6,
82        )
83
84    def test_should_return_source_eui64_value_when_source_eui64_property_is_called(self):
85        # GIVEN
86        source_mac_address = any_eui64()
87
88        message_info = common.MessageInfo()
89        message_info.source_mac_address = source_mac_address
90
91        # WHEN
92        actual_source_mac_address = message_info.source_mac_address
93
94        # THEN
95        self.assertEqual(source_mac_address, actual_source_mac_address)
96
97    def test_should_return_destination_eui64_value_when_destination_eui64_property_is_called(self):
98        # GIVEN
99        destination_mac_address = any_eui64()
100
101        message_info = common.MessageInfo()
102        message_info.destination_mac_address = destination_mac_address
103
104        # WHEN
105        actual_destination_mac_address = message_info.destination_mac_address
106
107        # THEN
108        self.assertEqual(destination_mac_address, actual_destination_mac_address)
109
110
111class TestMacAddress(unittest.TestCase):
112
113    def test_should_create_MacAddress_from_eui64_when_from_eui64_classmethod_is_called(self):
114        # GIVEN
115        eui64 = any_eui64()
116
117        # WHEN
118        mac_address = common.MacAddress.from_eui64(eui64)
119
120        # THEN
121        self.assertEqual(common.MacAddressType.LONG, mac_address.type)
122        self.assertEqual(eui64, mac_address.mac_address)
123
124    def test_should_create_MacAddress_from_rloc16_int_when_from_rloc16_classmethod_is_called(self):
125        # GIVEN
126        rloc16 = any_rloc16_int()
127
128        # WHEN
129        mac_address = common.MacAddress.from_rloc16(int(rloc16))
130
131        # THEN
132        self.assertEqual(common.MacAddressType.SHORT, mac_address.type)
133        self.assertEqual(struct.pack(">H", rloc16), mac_address.mac_address)
134
135    def test_should_create_MacAddress_from_rloc16_bytearray_when_from_rloc16_classmethod_is_called(self):
136        # GIVEN
137        rloc16 = any_rloc16_bytearray()
138
139        # WHEN
140        mac_address = common.MacAddress.from_rloc16(rloc16)
141
142        # THEN
143        self.assertEqual(common.MacAddressType.SHORT, mac_address.type)
144        self.assertEqual(rloc16, mac_address.mac_address)
145
146    def test_should_convert_short_MacAddress_to_iid_when_convert_method_is_called(self):
147        # GIVEN
148        rloc16 = any_rloc16_bytearray()
149
150        mac_address = common.MacAddress.from_rloc16(rloc16)
151
152        # WHEN
153        iid = mac_address.convert_to_iid()
154
155        # THEN
156        self.assertEqual(bytearray([0x00, 0x00, 0x00, 0xff, 0xfe, 0x00]) + rloc16, iid)
157
158    def test_should_convert_eui64_MacAddress_to_iid_when_convert_method_is_called(self):
159        # GIVEN
160        eui64 = any_eui64()
161
162        mac_address = common.MacAddress.from_eui64(eui64)
163
164        # WHEN
165        iid = mac_address.convert_to_iid()
166
167        # THEN
168        self.assertEqual(bytearray([eui64[0] ^ 0x02]) + eui64[1:], iid)
169
170
171if __name__ == "__main__":
172    unittest.main()
173