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
40ED = 4
41
42
43class Cert_6_1_7_RouterAttachLinkQuality(thread_cert.TestCase):
44    TOPOLOGY = {
45        LEADER: {
46            'name': 'LEADER',
47            'mode': 'rdn',
48            'allowlist': [ROUTER1, ROUTER2]
49        },
50        ROUTER1: {
51            'name': 'ROUTER_1',
52            'mode': 'rdn',
53            'allowlist': [LEADER, ED]
54        },
55        ROUTER2: {
56            'name': 'ROUTER_2',
57            'mode': 'rdn',
58            'allowlist': [LEADER, (ED, -85)]
59        },
60        ED: {
61            'name': 'ED',
62            'is_mtd': True,
63            'mode': 'rn',
64            'allowlist': [ROUTER1, ROUTER2]
65        },
66    }
67
68    def test(self):
69        self.nodes[LEADER].start()
70        self.simulator.go(config.LEADER_STARTUP_DELAY)
71        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
72
73        self.nodes[ROUTER1].start()
74        self.simulator.go(config.ROUTER_STARTUP_DELAY)
75        self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
76
77        self.nodes[ROUTER2].start()
78        self.simulator.go(config.ROUTER_STARTUP_DELAY)
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.collect_ipaddrs()
86        addrs = self.nodes[ED].get_addrs()
87        for addr in addrs:
88            self.assertTrue(self.nodes[ROUTER1].ping(addr))
89
90    def verify(self, pv):
91        pkts = pv.pkts
92        pv.summary.show()
93
94        ROUTER_1 = pv.vars['ROUTER_1']
95        ED = pv.vars['ED']
96        _ed_pkts = pkts.filter_wpan_src64(ED)
97
98        # Step 3: The DUT MUST send a MLE Parent Request to the
99        # All-Routers multicast address
100        _ed_pkts.filter_mle_cmd(MLE_PARENT_REQUEST).filter_ipv6_dst(
101            LINK_LOCAL_ALL_ROUTERS_MULTICAST_ADDRESS).must_next().must_verify(
102                lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(
103                    p.mle.tlv.type) and p.mle.tlv.scan_mask.r == 1 and p.mle.tlv.scan_mask.e == 0)
104
105        # Step 5: The DUT MUST send a MLE Child ID Request to Router_1
106        _ed_pkts.filter_wpan_dst64(ROUTER_1).filter_mle_cmd(MLE_CHILD_ID_REQUEST).must_next().must_verify(
107            lambda p: {
108                RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MLE_FRAME_COUNTER_TLV, MODE_TLV, TIMEOUT_TLV, VERSION_TLV,
109                ADDRESS_REGISTRATION_TLV, TLV_REQUEST_TLV
110            } <= set(p.mle.tlv.type))
111
112        # Step 6: The DUT MUST respond with ICMPv6 Echo Reply
113        ed_mleid = pv.vars['ED_MLEID']
114        router1_mleid = pv.vars['ROUTER_1_MLEID']
115        _pkt = pkts.range(_ed_pkts.index).filter_ipv6_src_dst(router1_mleid,
116                                                              ed_mleid).filter_ping_request().must_next()
117        _ed_pkts.filter_ipv6_src_dst(
118            ed_mleid, router1_mleid).filter_ping_reply(identifier=_pkt.icmpv6.echo.identifier).must_next()
119
120
121if __name__ == '__main__':
122    unittest.main()
123