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 unittest
31
32import config
33import thread_cert
34from pktverify.consts import MLE_ADVERTISEMENT, MLE_PARENT_REQUEST, MLE_CHILD_ID_RESPONSE, MLE_CHILD_ID_REQUEST, MGMT_ACTIVE_SET_URI, MGMT_ACTIVE_GET_URI, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MODE_TLV, TIMEOUT_TLV, VERSION_TLV, TLV_REQUEST_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, ADDRESS_REGISTRATION_TLV
35from pktverify.packet_verifier import PacketVerifier
36
37CHANNEL_INIT = 19
38PANID_INIT = 0xface
39
40PANID_FINAL = 0xabcd
41
42COMMISSIONER = 1
43LEADER = 2
44ROUTER1 = 3
45ROUTER2 = 4
46
47
48class Cert_9_2_15_PendingPartition(thread_cert.TestCase):
49    SUPPORT_NCP = False
50
51    TOPOLOGY = {
52        COMMISSIONER: {
53            'name': 'COMMISSIONER',
54            'active_dataset': {
55                'timestamp': 15,
56                'panid': PANID_INIT,
57                'channel': CHANNEL_INIT
58            },
59            'mode': 'rdn',
60            'allowlist': [LEADER]
61        },
62        LEADER: {
63            'name': 'LEADER',
64            'active_dataset': {
65                'timestamp': 15,
66                'panid': PANID_INIT,
67                'channel': CHANNEL_INIT
68            },
69            'mode': 'rdn',
70            'partition_id': 0xffffffff,
71            'allowlist': [COMMISSIONER, ROUTER1]
72        },
73        ROUTER1: {
74            'name': 'ROUTER_1',
75            'active_dataset': {
76                'timestamp': 15,
77                'panid': PANID_INIT,
78                'channel': CHANNEL_INIT
79            },
80            'mode': 'rdn',
81            'allowlist': [LEADER, ROUTER2]
82        },
83        ROUTER2: {
84            'name': 'ROUTER_2',
85            'active_dataset': {
86                'timestamp': 15,
87                'panid': PANID_INIT,
88                'channel': CHANNEL_INIT
89            },
90            'mode': 'rdn',
91            'allowlist': [ROUTER1]
92        },
93    }
94
95    def _setUpRouter2(self):
96        self.nodes[ROUTER2].add_allowlist(self.nodes[ROUTER1].get_addr64())
97        self.nodes[ROUTER2].enable_allowlist()
98        self.nodes[ROUTER2].set_router_selection_jitter(1)
99
100    def test(self):
101        self.nodes[LEADER].start()
102        self.simulator.go(config.LEADER_STARTUP_DELAY)
103        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
104
105        self.nodes[COMMISSIONER].start()
106        self.simulator.go(config.ROUTER_STARTUP_DELAY)
107        self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router')
108        self.nodes[COMMISSIONER].commissioner_start()
109        self.simulator.go(3)
110
111        self.nodes[ROUTER1].start()
112        self.simulator.go(config.ROUTER_STARTUP_DELAY)
113        self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
114
115        self.nodes[COMMISSIONER].send_mgmt_pending_set(
116            pending_timestamp=10,
117            active_timestamp=70,
118            delay_timer=600000,
119            mesh_local='fd00:0db9::',
120        )
121        self.simulator.go(5)
122
123        self.nodes[ROUTER2].start()
124        self.simulator.go(config.ROUTER_STARTUP_DELAY)
125        self.assertEqual(self.nodes[ROUTER2].get_state(), 'router')
126
127        self.nodes[ROUTER2].reset()
128        self._setUpRouter2()
129        self.simulator.go(100)
130
131        self.nodes[COMMISSIONER].send_mgmt_pending_set(
132            pending_timestamp=20,
133            active_timestamp=80,
134            delay_timer=200000,
135            mesh_local='fd00:0db7::',
136            panid=PANID_FINAL,
137        )
138        self.simulator.go(100)
139
140        self.nodes[ROUTER2].start()
141        self.simulator.go(config.ROUTER_RESET_DELAY)
142        self.assertEqual(self.nodes[ROUTER2].get_state(), 'router')
143        self.simulator.go(100)
144
145        self.assertEqual(self.nodes[COMMISSIONER].get_panid(), PANID_FINAL)
146        self.assertEqual(self.nodes[LEADER].get_panid(), PANID_FINAL)
147        self.assertEqual(self.nodes[ROUTER1].get_panid(), PANID_FINAL)
148        self.assertEqual(self.nodes[ROUTER2].get_panid(), PANID_FINAL)
149
150        ipaddrs = self.nodes[ROUTER2].get_addrs()
151        for ipaddr in ipaddrs:
152            if ipaddr[0:4] != 'fe80':
153                break
154        self.assertTrue(self.nodes[LEADER].ping(ipaddr))
155
156    def verify(self, pv):
157        pkts = pv.pkts
158        pv.summary.show()
159
160        LEADER = pv.vars['LEADER']
161        COMMISSIONER = pv.vars['COMMISSIONER']
162        ROUTER_1 = pv.vars['ROUTER_1']
163        ROUTER_2 = pv.vars['ROUTER_2']
164        _router2_pkts = pkts.filter_wpan_src64(ROUTER_2)
165
166        # Step 1: Ensure the topology is formed correctly
167        # Verify Commissioner, Leader and Router_1 are sending MLE advertisements
168        pkts.copy().filter_wpan_src64(LEADER).filter_mle_cmd(MLE_ADVERTISEMENT).must_next()
169        pkts.filter_wpan_dst64(COMMISSIONER).filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next()
170        pkts.copy().filter_wpan_src64(COMMISSIONER).filter_mle_cmd(MLE_ADVERTISEMENT).must_next()
171        pkts.filter_wpan_dst64(ROUTER_1).filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next()
172        pkts.copy().filter_wpan_src64(ROUTER_1).filter_mle_cmd(MLE_ADVERTISEMENT).must_next()
173
174        # Step 5: Router_2 begins attach process by sending a multicast MLE Parent Request
175        # The first MLE Parent Request sent MUST NOT be sent to all routers and REEDS
176        _router2_pkts.range(pkts.index).filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify(
177            lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(
178                p.mle.tlv.type) and p.mle.tlv.scan_mask.r == 1 and p.mle.tlv.scan_mask.e == 0)
179
180        # Step 7: Router_2  MUST send a MLE Child ID Request to Router_1
181        _router2_pkts.filter_mle_cmd(MLE_CHILD_ID_REQUEST).must_next().must_verify(lambda p: {
182            RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MODE_TLV, TIMEOUT_TLV, VERSION_TLV, TLV_REQUEST_TLV
183        } < set(p.mle.tlv.type) and ADDRESS_REGISTRATION_TLV not in p.mle.tlv.type)
184
185        # Step 14: Router_2 begins attach process by sending a multicast MLE Parent Request
186        # The first MLE Parent Request sent MUST NOT be sent to all routers and REEDS
187        _router2_pkts.filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify(
188            lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(
189                p.mle.tlv.type) and p.mle.tlv.scan_mask.r == 1 and p.mle.tlv.scan_mask.e == 0)
190
191        # Step 16: Router_2 MUST send a MLE Child ID Request to Router_1
192        _router2_pkts.filter_mle_cmd(MLE_CHILD_ID_REQUEST).must_next().must_verify(lambda p: {
193            RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MODE_TLV, TIMEOUT_TLV, VERSION_TLV, TLV_REQUEST_TLV
194        } < set(p.mle.tlv.type) and ADDRESS_REGISTRATION_TLV not in p.mle.tlv.type)
195
196
197if __name__ == '__main__':
198    unittest.main()
199