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