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_PARENT_RESPONSE, MLE_CHILD_UPDATE_RESPONSE, MLE_CHILD_ID_REQUEST, MLE_CHILD_ID_RESPONSE, MLE_LINK_REQUEST, MLE_LINK_ACCEPT_AND_REQUEST, ADDR_SOL_URI, SOURCE_ADDRESS_TLV, MODE_TLV, TIMEOUT_TLV, CHALLENGE_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MLE_FRAME_COUNTER_TLV, ROUTE64_TLV, ADDRESS16_TLV, LEADER_DATA_TLV, NETWORK_DATA_TLV, TLV_REQUEST_TLV, SCAN_MASK_TLV, CONNECTIVITY_TLV, LINK_MARGIN_TLV, VERSION_TLV, ADDRESS_REGISTRATION_TLV, ACTIVE_TIMESTAMP_TLV
35from pktverify.packet_verifier import PacketVerifier
36from pktverify.null_field import nullField
37
38LEADER = 1
39ROUTER = 2
40ED = 3
41
42
43class Cert_5_5_2_LeaderReboot(thread_cert.TestCase):
44    TOPOLOGY = {
45        LEADER: {
46            'name': 'LEADER',
47            'mode': 'rdn',
48            'allowlist': [ROUTER]
49        },
50        ROUTER: {
51            'name': 'ROUTER',
52            'mode': 'rdn',
53            'allowlist': [LEADER, ED]
54        },
55        ED: {
56            'name': 'MED',
57            'is_mtd': True,
58            'mode': 'rn',
59            'allowlist': [ROUTER]
60        },
61    }
62
63    def _setUpLeader(self):
64        self.nodes[LEADER].add_allowlist(self.nodes[ROUTER].get_addr64())
65        self.nodes[LEADER].enable_allowlist()
66        self.nodes[LEADER].set_router_selection_jitter(1)
67
68    def test(self):
69        self.nodes[LEADER].start()
70        self.simulator.go(5)
71        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
72
73        self.nodes[ROUTER].start()
74        self.simulator.go(5)
75        self.assertEqual(self.nodes[ROUTER].get_state(), 'router')
76
77        self.nodes[ED].start()
78        self.simulator.go(5)
79        self.assertEqual(self.nodes[ED].get_state(), 'child')
80
81        self.nodes[LEADER].reset()
82        self._setUpLeader()
83        self.simulator.go(140)
84        self.assertEqual(self.nodes[ROUTER].get_state(), 'leader')
85
86        self.nodes[LEADER].start()
87        self.simulator.go(5)
88        self.assertEqual(self.nodes[LEADER].get_state(), 'router')
89
90        addrs = self.nodes[ED].get_addrs()
91        for addr in addrs:
92            self.assertTrue(self.nodes[ROUTER].ping(addr))
93
94    def verify(self, pv):
95        pkts = pv.pkts
96        pv.summary.show()
97
98        LEADER = pv.vars['LEADER']
99        ROUTER = pv.vars['ROUTER']
100        MED = pv.vars['MED']
101        leader_pkts = pkts.filter_wpan_src64(LEADER)
102        _rpkts = pkts.filter_wpan_src64(ROUTER)
103
104        # Step 2: The DUT MUST send properly formatted MLE Advertisements
105        _rpkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next()
106        _lpkts = leader_pkts.range(_rpkts.index)
107        _lpkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify(
108            lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type))
109
110        _rpkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify(
111            lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type))
112
113        # Step 4: Router_1 MUST attempt to reattach to its original partition by
114        # sending MLE Parent Requests to the All-Routers multicast
115        # address (FFxx::xx) with a hop limit of 255. MUST make two separate attempts
116        for i in range(1, 3):
117            _rpkts.filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify(
118                lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(
119                    p.mle.tlv.type) and p.mle.tlv.scan_mask.r == 1 and p.mle.tlv.scan_mask.e == 1)
120        lreset_start = _rpkts.index
121
122        # Step 6:Router_1 MUST attempt to attach to any other Partition
123        # within range by sending a MLE Parent Request.
124        _rpkts.filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify(
125            lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(p.mle.tlv.type))
126        lreset_stop = _rpkts.index
127
128        # Step 3: The Leader MUST stop sending MLE advertisements.
129        leader_pkts.range(lreset_start, lreset_stop).filter_mle_cmd(MLE_ADVERTISEMENT).must_not_next()
130
131        # Step 5: Leader MUST NOT respond to the MLE Parent Requests
132        leader_pkts.range(lreset_start, lreset_stop).filter_mle_cmd(MLE_PARENT_RESPONSE).must_not_next()
133
134        # Step 7: Take over leader role of a new Partition and
135        # begin transmitting MLE Advertisements
136        with _rpkts.save_index():
137            _rpkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify(
138                lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type))
139
140        # Step 8: Router_1 MUST respond with an MLE Child Update Response,
141        # with the updated TLVs of the new partition
142        _rpkts.filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).must_next().must_verify(
143            lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV} < set(p.mle.tlv.type))
144
145        # Step 9: The Leader MUST send properly formatted MLE Parent
146        # Requests to the All-Routers multicast address
147        _lpkts.range(lreset_stop).filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify(
148            lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(p.mle.tlv.type))
149
150        # Step 10: Router_1 MUST send an MLE Parent Response
151        _rpkts.filter_mle_cmd(MLE_PARENT_RESPONSE).must_next().must_verify(
152            lambda p: {
153                SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, LINK_LAYER_FRAME_COUNTER_TLV, RESPONSE_TLV, CHALLENGE_TLV,
154                LINK_MARGIN_TLV, CONNECTIVITY_TLV, VERSION_TLV
155            } < set(p.mle.tlv.type))
156
157        # Step 11: Leader send MLE Child ID Request
158        _lpkts.filter_mle_cmd(MLE_CHILD_ID_REQUEST).must_next().must_verify(
159            lambda p: {
160                RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MODE_TLV, TIMEOUT_TLV, VERSION_TLV, TLV_REQUEST_TLV,
161                ADDRESS16_TLV, NETWORK_DATA_TLV, ROUTE64_TLV, ACTIVE_TIMESTAMP_TLV
162            } < set(p.mle.tlv.type))
163
164        #Step 12: Router_1 send MLE Child ID Response
165        _rpkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next().must_verify(
166            lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ADDRESS16_TLV, NETWORK_DATA_TLV, ROUTE64_TLV} < set(
167                p.mle.tlv.type))
168
169        #Step 13: Leader send an Address Solicit Request
170        _lpkts.filter_coap_request(ADDR_SOL_URI).must_next().must_verify(
171            lambda p: p.thread_address.tlv.ext_mac_addr and p.thread_address.tlv.status != 0)
172
173        #Step 14: Router_1 send an Address Solicit Response
174        _rpkts.filter_coap_ack(ADDR_SOL_URI).must_next().must_verify(
175            lambda p: p.thread_address.tlv.router_mask_assigned and p.thread_address.tlv.rloc16 is not nullField and p.
176            thread_address.tlv.status == 0)
177
178        #Step 15: Leader Send a Multicast Link Request
179        _lpkts.filter_mle_cmd(MLE_LINK_REQUEST).must_next().must_verify(
180            lambda p: {VERSION_TLV, TLV_REQUEST_TLV, SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, CHALLENGE_TLV} < set(
181                p.mle.tlv.type))
182
183        #Step 16: Router_1 send a Unicast Link Accept
184        _rpkts.filter_mle_cmd(MLE_LINK_ACCEPT_AND_REQUEST).must_next().must_verify(lambda p: {
185            VERSION_TLV, SOURCE_ADDRESS_TLV, RESPONSE_TLV, MLE_FRAME_COUNTER_TLV, LINK_MARGIN_TLV, LEADER_DATA_TLV
186        } < set(p.mle.tlv.type))
187
188        #Step 17: Router_1 MUST respond with an ICMPv6 Echo Reply
189        _rpkts.filter_ping_request().filter_wpan_dst64(MED).must_next()
190
191
192if __name__ == '__main__':
193    unittest.main()
194