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