1#!/usr/bin/env python3
2#
3#  Copyright (c) 2020, 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# This test verifies that PBBR sends BMLR.ntf correctly when multicast addresses are registered.
30#
31# Topology:
32#    ---- -(eth)-------
33#       |        |
34#      PBBR----SBBR
35#        \     /
36#        Router1---Commissioner
37#
38
39import unittest
40
41import thread_cert
42import config
43from pktverify.packet_verifier import PacketVerifier
44
45PBBR = 1
46SBBR = 2
47ROUTER1 = 3
48COMMISSIONER = 4
49
50REREG_DELAY = 4  # Seconds
51MLR_TIMEOUT = 300  # Seconds
52CUSTOM_MLR_TIMEOUT = 1000  # Seconds
53
54MA1 = 'ff04::1'
55MA2 = 'ff04::2'
56MA3 = 'ff04::3'
57MA4 = 'ff04::4'
58MA5 = 'ff04::5'
59
60
61class BBR_5_11_01(thread_cert.TestCase):
62    USE_MESSAGE_FACTORY = False
63
64    TOPOLOGY = {
65        PBBR: {
66            'name': 'PBBR',
67            'allowlist': [SBBR, ROUTER1],
68            'is_otbr': True,
69            'version': '1.2',
70        },
71        SBBR: {
72            'name': 'SBBR',
73            'allowlist': [PBBR, ROUTER1],
74            'is_otbr': True,
75            'version': '1.2',
76        },
77        ROUTER1: {
78            'name': 'ROUTER1',
79            'allowlist': [PBBR, SBBR, COMMISSIONER],
80            'version': '1.2',
81        },
82        COMMISSIONER: {
83            'name': 'COMMISSIONER',
84            'allowlist': [ROUTER1],
85            'version': '1.2',
86        }
87    }
88
89    def test(self):
90        self.nodes[PBBR].start()
91        self.wait_node_state(PBBR, 'leader', 10)
92        self.nodes[PBBR].set_backbone_router(reg_delay=REREG_DELAY, mlr_timeout=MLR_TIMEOUT)
93        self.nodes[PBBR].enable_backbone_router()
94        self.wait_until(lambda: self.nodes[PBBR].is_primary_backbone_router, 5)
95
96        self.nodes[SBBR].start()
97        self.wait_node_state(SBBR, 'router', 5)
98        self.nodes[SBBR].set_backbone_router(reg_delay=REREG_DELAY, mlr_timeout=MLR_TIMEOUT)
99        self.nodes[SBBR].enable_backbone_router()
100        self.simulator.go(5)
101        self.assertFalse(self.nodes[SBBR].is_primary_backbone_router)
102
103        self.nodes[ROUTER1].start()
104        self.wait_node_state(ROUTER1, 'router', 5)
105
106        self.nodes[COMMISSIONER].start()
107        self.wait_node_state(COMMISSIONER, 'router', 5)
108
109        self.wait_route_established(COMMISSIONER, PBBR)
110
111        self.nodes[COMMISSIONER].commissioner_start()
112        self.simulator.go(10)
113        self.assertEqual('active', self.nodes[COMMISSIONER].commissioner_state())
114
115        self.nodes[PBBR].add_ipmaddr(MA1)
116        self.simulator.go(REREG_DELAY)
117        self.nodes[ROUTER1].add_ipmaddr(MA2)
118        self.simulator.go(REREG_DELAY)
119
120        # Commissioner registers MA3 with default timeout
121        self.assertEqual((0, []), self.nodes[COMMISSIONER].register_multicast_listener(MA3, timeout=None))
122        # Commissioner registers MA4 with a custom timeout
123        self.assertEqual((0, []), self.nodes[COMMISSIONER].register_multicast_listener(MA4,
124                                                                                       timeout=CUSTOM_MLR_TIMEOUT))
125        # Commissioner unregisters MA5
126        self.assertEqual((0, []), self.nodes[COMMISSIONER].register_multicast_listener(MA5, timeout=0))
127
128        self.collect_ipaddrs()
129        self.collect_rloc16s()
130
131    def verify(self, pv: PacketVerifier):
132        pkts = pv.pkts
133        pv.add_common_vars()
134        pv.summary.show()
135        pv.verify_attached('ROUTER1')
136
137        ROUTER1 = pv.vars['ROUTER1']
138        COMMISSIONER = pv.vars['COMMISSIONER']
139        PBBR_ETH = pv.vars['PBBR_ETH']
140        SBBR_ETH = pv.vars['SBBR_ETH']
141
142        # Verify SBBR must not send `/b/bmr` during the test.
143        pkts.filter_eth_src(SBBR_ETH).filter_coap_request('/b/bmr').must_not_next()
144
145        # Verify PBBR sends `/b/bmr` on the Backbone link for MA1 with default timeout.
146        pkts.filter_eth_src(PBBR_ETH).filter_coap_request('/b/bmr').must_next().must_verify(f"""
147            thread_meshcop.tlv.ipv6_addr == ['{MA1}']
148            and thread_bl.tlv.timeout == {MLR_TIMEOUT}
149            and ipv6.src.is_link_local
150        """)
151
152        # Router registers MA2 with default timeout
153        pkts.filter_wpan_src64(ROUTER1).filter_coap_request('/n/mr').must_next().must_verify(f"""
154            thread_meshcop.tlv.ipv6_addr == ['{MA2}']
155            and thread_bl.tlv.timeout is null
156        """)
157        # Verify PBBR sends `/b/bmr` on the Backbone link for MA2 with default timeout.
158        pkts.filter_eth_src(PBBR_ETH).filter_coap_request('/b/bmr').must_next().must_verify(f"""
159            thread_meshcop.tlv.ipv6_addr == ['{MA2}']
160            and thread_bl.tlv.timeout == {MLR_TIMEOUT}
161            and ipv6.src.is_link_local
162        """)
163
164        # Commissioner registers MA3 with default timeout
165        pkts.filter_wpan_src64(COMMISSIONER).filter_coap_request('/n/mr').must_next().must_verify(f"""
166            thread_meshcop.tlv.ipv6_addr == ['{MA3}']
167            and thread_bl.tlv.timeout is null
168        """)
169        # Verify PBBR sends `/b/bmr` on the Backbone link for MA3 with default timeout.
170        pkts.filter_eth_src(PBBR_ETH).filter_coap_request('/b/bmr').must_next().must_verify(f"""
171            thread_meshcop.tlv.ipv6_addr == ['{MA3}']
172            and thread_bl.tlv.timeout == {MLR_TIMEOUT}
173            and ipv6.src.is_link_local
174        """)
175
176        # Commissioner registers MA4 with custom timeout
177        pkts.filter_wpan_src64(COMMISSIONER).filter_coap_request('/n/mr').must_next().must_verify(f"""
178            thread_meshcop.tlv.ipv6_addr == ['{MA4}']
179            and thread_nm.tlv.timeout == {CUSTOM_MLR_TIMEOUT}
180        """)
181        # Verify PBBR sends `/b/bmr` on the Backbone link for MA4 with custom timeout.
182        pkts.filter_eth_src(PBBR_ETH).filter_coap_request('/b/bmr').must_next().must_verify(f"""
183            thread_meshcop.tlv.ipv6_addr == ['{MA4}']
184            and thread_bl.tlv.timeout == {CUSTOM_MLR_TIMEOUT}
185            and ipv6.src.is_link_local
186        """)
187
188        # Commissioner unregisters MA5
189        pkts.filter_wpan_src64(COMMISSIONER).filter_coap_request('/n/mr').must_next().must_verify(f"""
190            thread_meshcop.tlv.ipv6_addr == ['{MA5}']
191            and thread_nm.tlv.timeout == 0
192        """)
193        # Verify PBBR sends `/b/bmr` on the Backbone link for MA5 with timeout equal to zero.
194        pkts.filter_eth_src(PBBR_ETH).filter_coap_request('/b/bmr').must_next().must_verify(f"""
195            thread_meshcop.tlv.ipv6_addr == ['{MA5}']
196            and thread_bl.tlv.timeout == 0
197            and ipv6.src.is_link_local
198        """)
199
200
201if __name__ == '__main__':
202    unittest.main()
203