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 thread_cert
33from pktverify.consts import MLE_CHILD_ID_REQUEST, MLE_ADVERTISEMENT, MLE_CHILD_UPDATE_REQUEST, SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV
34from pktverify.packet_verifier import PacketVerifier
35
36LEADER = 1
37ROUTER1 = 2
38ROUTER2 = 3
39ED = 4
40
41
42class Cert_6_2_2_NewPartition(thread_cert.TestCase):
43    TOPOLOGY = {
44        LEADER: {
45            'name': 'LEADER',
46            'mode': 'rdn',
47            'allowlist': [ROUTER1, ROUTER2]
48        },
49        ROUTER1: {
50            'name': 'ROUTER_1',
51            'mode': 'rdn',
52            'allowlist': [LEADER, ROUTER2, ED]
53        },
54        ROUTER2: {
55            'name': 'ROUTER_2',
56            'mode': 'rdn',
57            'network_id_timeout': 110,
58            'allowlist': [LEADER, ROUTER1]
59        },
60        ED: {
61            'name': 'ED',
62            'is_mtd': True,
63            'mode': 'rn',
64            'allowlist': [ROUTER1]
65        },
66    }
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[ROUTER1].start()
74        self.simulator.go(5)
75        self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
76
77        self.nodes[ROUTER2].start()
78        self.simulator.go(5)
79        self.assertEqual(self.nodes[ROUTER2].get_state(), 'router')
80
81        self.nodes[ED].start()
82        self.simulator.go(5)
83        self.assertEqual(self.nodes[ED].get_state(), 'child')
84
85        self.nodes[LEADER].stop()
86        self.simulator.go(140)
87        self.assertEqual(self.nodes[ROUTER2].get_state(), 'leader')
88        self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
89        self.assertEqual(self.nodes[ED].get_state(), 'child')
90
91        self.collect_ipaddrs()
92        addrs = self.nodes[ED].get_addrs()
93        for addr in addrs:
94            self.assertTrue(self.nodes[ROUTER1].ping(addr))
95
96    def verify(self, pv):
97        pkts = pv.pkts
98        pv.summary.show()
99
100        ROUTER_1 = pv.vars['ROUTER_1']
101        ED = pv.vars['ED']
102        _router1_pkts = pkts.filter_wpan_src64(ROUTER_1)
103        _ed_pkts = pkts.filter_wpan_src64(ED)
104
105        # Step 1: Ensure that the DUT successfully attached to Router_1
106        _ed_pkts.filter_mle_cmd(MLE_CHILD_ID_REQUEST).filter_wpan_dst64(ROUTER_1).must_next()
107
108        # Step 2-6 : remove Leader and make ROUTER_2 as new Leader
109        # Step 7: The DUT MUST send a MLE Child Update Request to Router_1
110        _ed_pkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_not_next()
111        _ed_pkts.filter_mle_cmd(MLE_CHILD_ID_REQUEST).must_not_next()
112        _ed_pkts.filter_wpan_dst64(ROUTER_1).filter_mle_cmd(MLE_CHILD_UPDATE_REQUEST).must_next().must_verify(
113            lambda p: {MODE_TLV, SOURCE_ADDRESS_TLV, LEADER_DATA_TLV} < set(p.mle.tlv.type))
114
115        # Step 8: The DUT MUST respond with ICMPv6 Echo Reply
116        _ed_pkts.filter('ipv6.dst == {ROUTER_1_MLEID} and ipv6.src == {ED_MLEID}',
117                        **pv.vars).filter_ping_reply().must_next()
118
119
120if __name__ == '__main__':
121    unittest.main()
122