xref: /aosp_15_r20/external/openthread/tests/scripts/thread-cert/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 Worker
30*cfb92d14SAndroid Build Coastguard Workerimport struct
31*cfb92d14SAndroid Build Coastguard Worker
32*cfb92d14SAndroid Build Coastguard Workerfrom binascii import hexlify
33*cfb92d14SAndroid Build Coastguard Workerfrom enum import IntEnum
34*cfb92d14SAndroid Build Coastguard Worker
35*cfb92d14SAndroid Build Coastguard Workerimport ipaddress
36*cfb92d14SAndroid Build Coastguard Worker
37*cfb92d14SAndroid Build Coastguard Worker# Map of 2 bits of parent priority.
38*cfb92d14SAndroid Build Coastguard Workerpp_map = {1: 1, 0: 0, 3: -1, 2: -2}
39*cfb92d14SAndroid Build Coastguard Worker
40*cfb92d14SAndroid Build Coastguard WorkerUDP_TEST_PORT = 12345
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Worker
43*cfb92d14SAndroid Build Coastguard Worker# Get the signed parent priority from the byte that parent priority is in.
44*cfb92d14SAndroid Build Coastguard Workerdef map_pp(pp_byte):
45*cfb92d14SAndroid Build Coastguard Worker    return pp_map[((pp_byte & 0xC0) >> 6)]
46*cfb92d14SAndroid Build Coastguard Worker
47*cfb92d14SAndroid Build Coastguard Worker
48*cfb92d14SAndroid Build Coastguard Workerdef expect_the_same_class(self, other):
49*cfb92d14SAndroid Build Coastguard Worker    if not isinstance(other, self.__class__):
50*cfb92d14SAndroid Build Coastguard Worker        raise TypeError("Expected the same class. Got {} and {}".format(type(self), type(other)))
51*cfb92d14SAndroid Build Coastguard Worker
52*cfb92d14SAndroid Build Coastguard Worker
53*cfb92d14SAndroid Build Coastguard Workerclass MessageInfo(object):
54*cfb92d14SAndroid Build Coastguard Worker
55*cfb92d14SAndroid Build Coastguard Worker    def __init__(self):
56*cfb92d14SAndroid Build Coastguard Worker        self.aux_sec_hdr = None
57*cfb92d14SAndroid Build Coastguard Worker        self.aux_sec_hdr_bytes = None
58*cfb92d14SAndroid Build Coastguard Worker
59*cfb92d14SAndroid Build Coastguard Worker        self.mhr_bytes = None
60*cfb92d14SAndroid Build Coastguard Worker        self.extra_open_fields = None
61*cfb92d14SAndroid Build Coastguard Worker
62*cfb92d14SAndroid Build Coastguard Worker        self.source_mac_address = None
63*cfb92d14SAndroid Build Coastguard Worker        self.destination_mac_address = None
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard Worker        self._source_ipv6 = None
66*cfb92d14SAndroid Build Coastguard Worker        self._destination_ipv6 = None
67*cfb92d14SAndroid Build Coastguard Worker
68*cfb92d14SAndroid Build Coastguard Worker        self._src_port = None
69*cfb92d14SAndroid Build Coastguard Worker        self._dst_port = None
70*cfb92d14SAndroid Build Coastguard Worker
71*cfb92d14SAndroid Build Coastguard Worker        self.stable = None
72*cfb92d14SAndroid Build Coastguard Worker        self.payload_length = 0
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Worker    def _convert_value_to_ip_address(self, value):
75*cfb92d14SAndroid Build Coastguard Worker        if isinstance(value, bytearray):
76*cfb92d14SAndroid Build Coastguard Worker            value = bytes(value)
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Worker        return ipaddress.ip_address(value)
79*cfb92d14SAndroid Build Coastguard Worker
80*cfb92d14SAndroid Build Coastguard Worker    @property
81*cfb92d14SAndroid Build Coastguard Worker    def source_ipv6(self):
82*cfb92d14SAndroid Build Coastguard Worker        return self._source_ipv6
83*cfb92d14SAndroid Build Coastguard Worker
84*cfb92d14SAndroid Build Coastguard Worker    @source_ipv6.setter
85*cfb92d14SAndroid Build Coastguard Worker    def source_ipv6(self, value):
86*cfb92d14SAndroid Build Coastguard Worker        self._source_ipv6 = self._convert_value_to_ip_address(value)
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Worker    @property
89*cfb92d14SAndroid Build Coastguard Worker    def destination_ipv6(self):
90*cfb92d14SAndroid Build Coastguard Worker        return self._destination_ipv6
91*cfb92d14SAndroid Build Coastguard Worker
92*cfb92d14SAndroid Build Coastguard Worker    @destination_ipv6.setter
93*cfb92d14SAndroid Build Coastguard Worker    def destination_ipv6(self, value):
94*cfb92d14SAndroid Build Coastguard Worker        self._destination_ipv6 = self._convert_value_to_ip_address(value)
95*cfb92d14SAndroid Build Coastguard Worker
96*cfb92d14SAndroid Build Coastguard Worker    @property
97*cfb92d14SAndroid Build Coastguard Worker    def src_port(self):
98*cfb92d14SAndroid Build Coastguard Worker        return self._src_port
99*cfb92d14SAndroid Build Coastguard Worker
100*cfb92d14SAndroid Build Coastguard Worker    @src_port.setter
101*cfb92d14SAndroid Build Coastguard Worker    def src_port(self, value):
102*cfb92d14SAndroid Build Coastguard Worker        self._src_port = value
103*cfb92d14SAndroid Build Coastguard Worker
104*cfb92d14SAndroid Build Coastguard Worker    @property
105*cfb92d14SAndroid Build Coastguard Worker    def dst_port(self):
106*cfb92d14SAndroid Build Coastguard Worker        return self._dst_port
107*cfb92d14SAndroid Build Coastguard Worker
108*cfb92d14SAndroid Build Coastguard Worker    @dst_port.setter
109*cfb92d14SAndroid Build Coastguard Worker    def dst_port(self, value):
110*cfb92d14SAndroid Build Coastguard Worker        self._dst_port = value
111*cfb92d14SAndroid Build Coastguard Worker
112*cfb92d14SAndroid Build Coastguard Worker
113*cfb92d14SAndroid Build Coastguard Workerclass MacAddressType(IntEnum):
114*cfb92d14SAndroid Build Coastguard Worker    SHORT = 0
115*cfb92d14SAndroid Build Coastguard Worker    LONG = 1
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Worker
118*cfb92d14SAndroid Build Coastguard Workerclass MacAddress(object):
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Worker    def __init__(self, mac_address, _type, big_endian=True):
121*cfb92d14SAndroid Build Coastguard Worker        if _type == MacAddressType.SHORT:
122*cfb92d14SAndroid Build Coastguard Worker            length = 2
123*cfb92d14SAndroid Build Coastguard Worker        elif _type == MacAddressType.LONG:
124*cfb92d14SAndroid Build Coastguard Worker            length = 8
125*cfb92d14SAndroid Build Coastguard Worker
126*cfb92d14SAndroid Build Coastguard Worker        if not big_endian:
127*cfb92d14SAndroid Build Coastguard Worker            mac_address = mac_address[::-1]
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Worker        self._mac_address = bytearray(mac_address[:length])
130*cfb92d14SAndroid Build Coastguard Worker        self._type = _type
131*cfb92d14SAndroid Build Coastguard Worker
132*cfb92d14SAndroid Build Coastguard Worker    @property
133*cfb92d14SAndroid Build Coastguard Worker    def type(self):
134*cfb92d14SAndroid Build Coastguard Worker        return self._type
135*cfb92d14SAndroid Build Coastguard Worker
136*cfb92d14SAndroid Build Coastguard Worker    @property
137*cfb92d14SAndroid Build Coastguard Worker    def mac_address(self):
138*cfb92d14SAndroid Build Coastguard Worker        return self._mac_address
139*cfb92d14SAndroid Build Coastguard Worker
140*cfb92d14SAndroid Build Coastguard Worker    @property
141*cfb92d14SAndroid Build Coastguard Worker    def rloc(self):
142*cfb92d14SAndroid Build Coastguard Worker        return struct.unpack(">H", self._mac_address)[0]
143*cfb92d14SAndroid Build Coastguard Worker
144*cfb92d14SAndroid Build Coastguard Worker    def convert_to_iid(self):
145*cfb92d14SAndroid Build Coastguard Worker        if self._type == MacAddressType.SHORT:
146*cfb92d14SAndroid Build Coastguard Worker            return (bytearray([0x00, 0x00, 0x00, 0xff, 0xfe, 0x00]) + self._mac_address[:2])
147*cfb92d14SAndroid Build Coastguard Worker        elif self._type == MacAddressType.LONG:
148*cfb92d14SAndroid Build Coastguard Worker            return (bytearray([self._mac_address[0] ^ 0x02]) + self._mac_address[1:])
149*cfb92d14SAndroid Build Coastguard Worker        else:
150*cfb92d14SAndroid Build Coastguard Worker            raise RuntimeError("Could not convert to IID. Invalid MAC address type: {}".format(self._type))
151*cfb92d14SAndroid Build Coastguard Worker
152*cfb92d14SAndroid Build Coastguard Worker    @classmethod
153*cfb92d14SAndroid Build Coastguard Worker    def from_eui64(cls, eui64, big_endian=True):
154*cfb92d14SAndroid Build Coastguard Worker        if not isinstance(eui64, bytearray):
155*cfb92d14SAndroid Build Coastguard Worker            raise RuntimeError("Could not create MAC address from EUI64. Invalid data type: {}".format(type(eui64)))
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Worker        return cls(eui64, MacAddressType.LONG)
158*cfb92d14SAndroid Build Coastguard Worker
159*cfb92d14SAndroid Build Coastguard Worker    @classmethod
160*cfb92d14SAndroid Build Coastguard Worker    def from_rloc16(cls, rloc16, big_endian=True):
161*cfb92d14SAndroid Build Coastguard Worker        if isinstance(rloc16, int):
162*cfb92d14SAndroid Build Coastguard Worker            mac_address = struct.pack(">H", rloc16)
163*cfb92d14SAndroid Build Coastguard Worker        elif isinstance(rloc16, bytearray):
164*cfb92d14SAndroid Build Coastguard Worker            mac_address = rloc16[:2]
165*cfb92d14SAndroid Build Coastguard Worker        else:
166*cfb92d14SAndroid Build Coastguard Worker            raise RuntimeError("Could not create MAC address from RLOC16. Invalid data type: {}".format(type(rloc16)))
167*cfb92d14SAndroid Build Coastguard Worker
168*cfb92d14SAndroid Build Coastguard Worker        return cls(mac_address, MacAddressType.SHORT)
169*cfb92d14SAndroid Build Coastguard Worker
170*cfb92d14SAndroid Build Coastguard Worker    def __eq__(self, other):
171*cfb92d14SAndroid Build Coastguard Worker        return (self.type == other.type) and (self.mac_address == other.mac_address)
172*cfb92d14SAndroid Build Coastguard Worker
173*cfb92d14SAndroid Build Coastguard Worker    def __repr__(self):
174*cfb92d14SAndroid Build Coastguard Worker        return "MacAddress(mac_address=b'{}', type={})".format(hexlify(self.mac_address), MacAddressType(self._type))
175