xref: /aosp_15_r20/external/openthread/tests/scripts/thread-cert/test_common.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2016, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker#
29*cfb92d14SAndroid Build Coastguard Workerimport random
30*cfb92d14SAndroid Build Coastguard Workerimport struct
31*cfb92d14SAndroid Build Coastguard Workerimport unittest
32*cfb92d14SAndroid Build Coastguard Workerimport ipaddress
33*cfb92d14SAndroid Build Coastguard Worker
34*cfb92d14SAndroid Build Coastguard Workerimport common
35*cfb92d14SAndroid Build Coastguard Worker
36*cfb92d14SAndroid Build Coastguard Worker
37*cfb92d14SAndroid Build Coastguard Workerdef any_eui64():
38*cfb92d14SAndroid Build Coastguard Worker    return bytearray([random.getrandbits(8) for _ in range(8)])
39*cfb92d14SAndroid Build Coastguard Worker
40*cfb92d14SAndroid Build Coastguard Worker
41*cfb92d14SAndroid Build Coastguard Workerdef any_rloc16_int():
42*cfb92d14SAndroid Build Coastguard Worker    return random.getrandbits(16)
43*cfb92d14SAndroid Build Coastguard Worker
44*cfb92d14SAndroid Build Coastguard Worker
45*cfb92d14SAndroid Build Coastguard Workerdef any_rloc16_bytearray():
46*cfb92d14SAndroid Build Coastguard Worker    return bytearray([random.getrandbits(8) for _ in range(2)])
47*cfb92d14SAndroid Build Coastguard Worker
48*cfb92d14SAndroid Build Coastguard Worker
49*cfb92d14SAndroid Build Coastguard Workerdef any_ipv6_address():
50*cfb92d14SAndroid Build Coastguard Worker    return bytearray([random.getrandbits(8) for _ in range(16)])
51*cfb92d14SAndroid Build Coastguard Worker
52*cfb92d14SAndroid Build Coastguard Worker
53*cfb92d14SAndroid Build Coastguard Workerclass TestMessageInfo(unittest.TestCase):
54*cfb92d14SAndroid Build Coastguard Worker
55*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_source_ipv6_value_when_source_ipv6_property_is_called(self):
56*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
57*cfb92d14SAndroid Build Coastguard Worker        source_ipv6 = any_ipv6_address()
58*cfb92d14SAndroid Build Coastguard Worker
59*cfb92d14SAndroid Build Coastguard Worker        message_info = common.MessageInfo()
60*cfb92d14SAndroid Build Coastguard Worker        message_info.source_ipv6 = source_ipv6
61*cfb92d14SAndroid Build Coastguard Worker
62*cfb92d14SAndroid Build Coastguard Worker        # WHEN
63*cfb92d14SAndroid Build Coastguard Worker        actual_source_ipv6 = message_info.source_ipv6
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard Worker        # THEN
66*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(ipaddress.ip_address(bytes(source_ipv6)), actual_source_ipv6)
67*cfb92d14SAndroid Build Coastguard Worker
68*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_destination_ipv6_value_when_destination_ipv6_property_is_called(self):
69*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
70*cfb92d14SAndroid Build Coastguard Worker        destination_ipv6 = any_ipv6_address()
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Worker        message_info = common.MessageInfo()
73*cfb92d14SAndroid Build Coastguard Worker        message_info.destination_ipv6 = destination_ipv6
74*cfb92d14SAndroid Build Coastguard Worker
75*cfb92d14SAndroid Build Coastguard Worker        # WHEN
76*cfb92d14SAndroid Build Coastguard Worker        actual_destination_ipv6 = message_info.destination_ipv6
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Worker        # THEN
79*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(
80*cfb92d14SAndroid Build Coastguard Worker            ipaddress.ip_address(bytes(destination_ipv6)),
81*cfb92d14SAndroid Build Coastguard Worker            actual_destination_ipv6,
82*cfb92d14SAndroid Build Coastguard Worker        )
83*cfb92d14SAndroid Build Coastguard Worker
84*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_source_eui64_value_when_source_eui64_property_is_called(self):
85*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
86*cfb92d14SAndroid Build Coastguard Worker        source_mac_address = any_eui64()
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Worker        message_info = common.MessageInfo()
89*cfb92d14SAndroid Build Coastguard Worker        message_info.source_mac_address = source_mac_address
90*cfb92d14SAndroid Build Coastguard Worker
91*cfb92d14SAndroid Build Coastguard Worker        # WHEN
92*cfb92d14SAndroid Build Coastguard Worker        actual_source_mac_address = message_info.source_mac_address
93*cfb92d14SAndroid Build Coastguard Worker
94*cfb92d14SAndroid Build Coastguard Worker        # THEN
95*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(source_mac_address, actual_source_mac_address)
96*cfb92d14SAndroid Build Coastguard Worker
97*cfb92d14SAndroid Build Coastguard Worker    def test_should_return_destination_eui64_value_when_destination_eui64_property_is_called(self):
98*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
99*cfb92d14SAndroid Build Coastguard Worker        destination_mac_address = any_eui64()
100*cfb92d14SAndroid Build Coastguard Worker
101*cfb92d14SAndroid Build Coastguard Worker        message_info = common.MessageInfo()
102*cfb92d14SAndroid Build Coastguard Worker        message_info.destination_mac_address = destination_mac_address
103*cfb92d14SAndroid Build Coastguard Worker
104*cfb92d14SAndroid Build Coastguard Worker        # WHEN
105*cfb92d14SAndroid Build Coastguard Worker        actual_destination_mac_address = message_info.destination_mac_address
106*cfb92d14SAndroid Build Coastguard Worker
107*cfb92d14SAndroid Build Coastguard Worker        # THEN
108*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(destination_mac_address, actual_destination_mac_address)
109*cfb92d14SAndroid Build Coastguard Worker
110*cfb92d14SAndroid Build Coastguard Worker
111*cfb92d14SAndroid Build Coastguard Workerclass TestMacAddress(unittest.TestCase):
112*cfb92d14SAndroid Build Coastguard Worker
113*cfb92d14SAndroid Build Coastguard Worker    def test_should_create_MacAddress_from_eui64_when_from_eui64_classmethod_is_called(self):
114*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
115*cfb92d14SAndroid Build Coastguard Worker        eui64 = any_eui64()
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Worker        # WHEN
118*cfb92d14SAndroid Build Coastguard Worker        mac_address = common.MacAddress.from_eui64(eui64)
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Worker        # THEN
121*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(common.MacAddressType.LONG, mac_address.type)
122*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(eui64, mac_address.mac_address)
123*cfb92d14SAndroid Build Coastguard Worker
124*cfb92d14SAndroid Build Coastguard Worker    def test_should_create_MacAddress_from_rloc16_int_when_from_rloc16_classmethod_is_called(self):
125*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
126*cfb92d14SAndroid Build Coastguard Worker        rloc16 = any_rloc16_int()
127*cfb92d14SAndroid Build Coastguard Worker
128*cfb92d14SAndroid Build Coastguard Worker        # WHEN
129*cfb92d14SAndroid Build Coastguard Worker        mac_address = common.MacAddress.from_rloc16(int(rloc16))
130*cfb92d14SAndroid Build Coastguard Worker
131*cfb92d14SAndroid Build Coastguard Worker        # THEN
132*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(common.MacAddressType.SHORT, mac_address.type)
133*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(struct.pack(">H", rloc16), mac_address.mac_address)
134*cfb92d14SAndroid Build Coastguard Worker
135*cfb92d14SAndroid Build Coastguard Worker    def test_should_create_MacAddress_from_rloc16_bytearray_when_from_rloc16_classmethod_is_called(self):
136*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
137*cfb92d14SAndroid Build Coastguard Worker        rloc16 = any_rloc16_bytearray()
138*cfb92d14SAndroid Build Coastguard Worker
139*cfb92d14SAndroid Build Coastguard Worker        # WHEN
140*cfb92d14SAndroid Build Coastguard Worker        mac_address = common.MacAddress.from_rloc16(rloc16)
141*cfb92d14SAndroid Build Coastguard Worker
142*cfb92d14SAndroid Build Coastguard Worker        # THEN
143*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(common.MacAddressType.SHORT, mac_address.type)
144*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(rloc16, mac_address.mac_address)
145*cfb92d14SAndroid Build Coastguard Worker
146*cfb92d14SAndroid Build Coastguard Worker    def test_should_convert_short_MacAddress_to_iid_when_convert_method_is_called(self):
147*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
148*cfb92d14SAndroid Build Coastguard Worker        rloc16 = any_rloc16_bytearray()
149*cfb92d14SAndroid Build Coastguard Worker
150*cfb92d14SAndroid Build Coastguard Worker        mac_address = common.MacAddress.from_rloc16(rloc16)
151*cfb92d14SAndroid Build Coastguard Worker
152*cfb92d14SAndroid Build Coastguard Worker        # WHEN
153*cfb92d14SAndroid Build Coastguard Worker        iid = mac_address.convert_to_iid()
154*cfb92d14SAndroid Build Coastguard Worker
155*cfb92d14SAndroid Build Coastguard Worker        # THEN
156*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(bytearray([0x00, 0x00, 0x00, 0xff, 0xfe, 0x00]) + rloc16, iid)
157*cfb92d14SAndroid Build Coastguard Worker
158*cfb92d14SAndroid Build Coastguard Worker    def test_should_convert_eui64_MacAddress_to_iid_when_convert_method_is_called(self):
159*cfb92d14SAndroid Build Coastguard Worker        # GIVEN
160*cfb92d14SAndroid Build Coastguard Worker        eui64 = any_eui64()
161*cfb92d14SAndroid Build Coastguard Worker
162*cfb92d14SAndroid Build Coastguard Worker        mac_address = common.MacAddress.from_eui64(eui64)
163*cfb92d14SAndroid Build Coastguard Worker
164*cfb92d14SAndroid Build Coastguard Worker        # WHEN
165*cfb92d14SAndroid Build Coastguard Worker        iid = mac_address.convert_to_iid()
166*cfb92d14SAndroid Build Coastguard Worker
167*cfb92d14SAndroid Build Coastguard Worker        # THEN
168*cfb92d14SAndroid Build Coastguard Worker        self.assertEqual(bytearray([eui64[0] ^ 0x02]) + eui64[1:], iid)
169*cfb92d14SAndroid Build Coastguard Worker
170*cfb92d14SAndroid Build Coastguard Worker
171*cfb92d14SAndroid Build Coastguard Workerif __name__ == "__main__":
172*cfb92d14SAndroid Build Coastguard Worker    unittest.main()
173