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_CHILD_ID_REQUEST, MLE_PARENT_REQUEST, MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MLE_FRAME_COUNTER_TLV, TIMEOUT_TLV, ADDRESS_REGISTRATION_TLV, TLV_REQUEST_TLV, LINK_LOCAL_ALL_ROUTERS_MULTICAST_ADDRESS
35from pktverify.packet_verifier import PacketVerifier
36
37LEADER = 1
38ROUTER1 = 2
39ROUTER2 = 3
40ROUTER3 = 4
41ED = 5
42
43
44class Cert_6_1_3_RouterAttachConnectivity(thread_cert.TestCase):
45    TOPOLOGY = {
46        LEADER: {
47            'name': 'LEADER',
48            'mode': 'rdn',
49            'allowlist': [ROUTER1, ROUTER2, ROUTER3]
50        },
51        ROUTER1: {
52            'name': 'ROUTER_1',
53            'mode': 'rdn',
54            'allowlist': [LEADER, ROUTER3]
55        },
56        ROUTER2: {
57            'name': 'ROUTER_2',
58            'mode': 'rdn',
59            'allowlist': [LEADER, ED]
60        },
61        ROUTER3: {
62            'name': 'ROUTER_3',
63            'mode': 'rdn',
64            'allowlist': [LEADER, ROUTER1, ED]
65        },
66        ED: {
67            'name': 'ED',
68            'is_mtd': True,
69            'mode': 'rn',
70            'allowlist': [ROUTER2, ROUTER3]
71        },
72    }
73
74    def test(self):
75        self.nodes[LEADER].start()
76        self.simulator.go(5)
77        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
78
79        for i in range(2, 5):
80            self.nodes[i].start()
81
82        self.simulator.go(5)
83
84        for i in range(2, 5):
85            self.assertEqual(self.nodes[i].get_state(), 'router')
86
87        self.simulator.go(config.MAX_ADVERTISEMENT_INTERVAL)
88
89        self.nodes[ED].start()
90        self.simulator.go(5)
91        self.assertEqual(self.nodes[ED].get_state(), 'child')
92
93        self.collect_ipaddrs()
94        addrs = self.nodes[ED].get_addrs()
95        for addr in addrs:
96            self.assertTrue(self.nodes[ROUTER3].ping(addr))
97
98    def verify(self, pv):
99        pkts = pv.pkts
100        pv.summary.show()
101
102        ROUTER_3 = pv.vars['ROUTER_3']
103        ED = pv.vars['ED']
104        _router3_pkts = pkts.filter_wpan_src64(ROUTER_3)
105        _ed_pkts = pkts.filter_wpan_src64(ED)
106
107        # Step 2: The DUT MUST send a MLE Parent Request to the
108        # All-Routers multicast address
109        _ed_pkts.filter_mle_cmd(MLE_PARENT_REQUEST).filter_ipv6_dst(
110            LINK_LOCAL_ALL_ROUTERS_MULTICAST_ADDRESS).must_next().must_verify(
111                lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(p.mle.tlv.type
112                                                                                      ) and p.mle.tlv.scan_mask.r == 1)
113
114        # Step 3: Router_2, Router_3 Respond with MLE Parent Response
115        # Step 4: DUT Send a Child ID Request to Router_3 due to better connectivity
116        _ed_pkts.filter_mle_cmd(MLE_CHILD_ID_REQUEST).must_next().must_verify(
117            lambda p: {
118                RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MLE_FRAME_COUNTER_TLV, MODE_TLV, TIMEOUT_TLV, VERSION_TLV,
119                ADDRESS_REGISTRATION_TLV, TLV_REQUEST_TLV
120            } <= set(p.mle.tlv.type))
121
122        # Step 5: The DUT MUST respond with ICMPv6 Echo Reply
123        ed_mleid = pv.vars['ED_MLEID']
124        router3_mleid = pv.vars['ROUTER_3_MLEID']
125        _pkt = _router3_pkts.filter(
126            lambda p: p.ipv6.src == router3_mleid and p.ipv6.dst == ed_mleid).filter_ping_request().must_next()
127        _ed_pkts.filter(lambda p: p.ipv6.src == ed_mleid and p.ipv6.dst == router3_mleid).filter_ping_reply(
128            identifier=_pkt.icmpv6.echo.identifier).must_next()
129
130
131if __name__ == '__main__':
132    unittest.main()
133