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_ADVERTISEMENT, MLE_DATA_RESPONSE, MLE_CHILD_ID_RESPONSE, ADDR_SOL_URI, MLE_CHILD_UPDATE_RESPONSE, MODE_TLV, LEADER_DATA_TLV, ROUTE64_TLV, SOURCE_ADDRESS_TLV, ACTIVE_TIMESTAMP_TLV, ADDRESS16_TLV, NETWORK_DATA_TLV, ADDRESS_REGISTRATION_TLV
35from pktverify.packet_verifier import PacketVerifier
36from pktverify.addrs import Ipv6Addr
37
38LEADER = 1
39ROUTER = 2
40ED1 = 3
41SED1 = 4
42
43MTDS = [ED1, SED1]
44
45
46class Cert_5_6_2_NetworkDataRouterAsBr(thread_cert.TestCase):
47    TOPOLOGY = {
48        LEADER: {
49            'name': 'LEADER',
50            'mode': 'rdn',
51            'allowlist': [ROUTER, ED1, SED1]
52        },
53        ROUTER: {
54            'name': 'ROUTER',
55            'mode': 'rdn',
56            'allowlist': [LEADER]
57        },
58        ED1: {
59            'name': 'MED',
60            'is_mtd': True,
61            'mode': 'rn',
62            'allowlist': [LEADER]
63        },
64        SED1: {
65            'name': 'SED',
66            'is_mtd': True,
67            'mode': '-',
68            'timeout': config.DEFAULT_CHILD_TIMEOUT,
69            'allowlist': [LEADER]
70        },
71    }
72
73    def test(self):
74        self.nodes[LEADER].start()
75        self.simulator.go(config.LEADER_STARTUP_DELAY)
76        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
77
78        self.nodes[ROUTER].start()
79        self.simulator.go(config.ROUTER_STARTUP_DELAY)
80        self.assertEqual(self.nodes[ROUTER].get_state(), 'router')
81
82        self.nodes[ROUTER].add_prefix('2001:2:0:1::/64', 'paros')
83        self.nodes[ROUTER].add_prefix('2001:2:0:2::/64', 'paro')
84        self.nodes[ROUTER].register_netdata()
85
86        # Set lowpan context of sniffer
87        self.simulator.set_lowpan_context(1, '2001:2:0:1::/64')
88        self.simulator.set_lowpan_context(2, '2001:2:0:2::/64')
89
90        self.nodes[ED1].start()
91        self.simulator.go(5)
92        self.assertEqual(self.nodes[ED1].get_state(), 'child')
93
94        self.nodes[SED1].start()
95        self.simulator.go(5)
96        self.assertEqual(self.nodes[SED1].get_state(), 'child')
97
98        addrs = self.nodes[ED1].get_addrs()
99        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
100        self.assertTrue(any('2001:2:0:2' in addr[0:10] for addr in addrs))
101        for addr in addrs:
102            if addr[0:10] == '2001:2:0:1' or addr[0:10] == '2001:2:0:2':
103                self.assertTrue(self.nodes[LEADER].ping(addr))
104
105        addrs = self.nodes[SED1].get_addrs()
106        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
107        self.assertFalse(any('2001:2:0:2' in addr[0:10] for addr in addrs))
108        for addr in addrs:
109            if addr[0:10] == '2001:2:0:1' or addr[0:10] == '2001:2:0:2':
110                self.assertTrue(self.nodes[LEADER].ping(addr))
111
112    def verify(self, pv):
113        pkts = pv.pkts
114        pv.summary.show()
115
116        LEADER = pv.vars['LEADER']
117        ROUTER = pv.vars['ROUTER']
118        MED = pv.vars['MED']
119        SED = pv.vars['SED']
120        _lpkts = pkts.filter_wpan_src64(LEADER)
121
122        # Step 1: The DUT MUST send properly formatted MLE Advertisements
123        _lpkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify(
124            lambda p: {LEADER_DATA_TLV, ROUTE64_TLV, SOURCE_ADDRESS_TLV} <= set(p.mle.tlv.type))
125
126        # Step 3: The DUT MUST properly attach Router_1 device to the network,
127        # and transmit Network Data during the attach phase in the
128        # Child ID Response frame of the Network Data TLV
129        _lpkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next().must_verify(lambda p: p.wpan.dst64 == ROUTER and {
130            SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ACTIVE_TIMESTAMP_TLV, ADDRESS16_TLV, NETWORK_DATA_TLV
131        } <= set(p.mle.tlv.type))
132
133        # Step 5: The DUT Automatically sends a CoAP Response frame and
134        # MLE Data Response message
135        _lpkts.filter_coap_ack(ADDR_SOL_URI).must_next()
136        _lpkts.filter_mle_cmd(MLE_DATA_RESPONSE).must_next().must_verify(
137            lambda p: {Ipv6Addr('2001:2:0:1::'), Ipv6Addr('2001:2:0:2::')} == set(
138                p.thread_nwd.tlv.prefix) and p.thread_nwd.tlv.border_router.flag.p == [1, 1] and p.thread_nwd.tlv.
139            border_router.flag.s == [1, 1] and p.thread_nwd.tlv.border_router.flag.r == [1, 1] and p.thread_nwd.tlv.
140            border_router.flag.o == [1, 1] and p.thread_nwd.tlv.stable == [0, 1, 1, 1, 0, 0, 0])
141        _lpkts_med = _lpkts.copy()
142        _lpkts_sed = _lpkts.copy()
143
144        # Step 7: The DUT MUST send a MLE Child ID Response to SED_1,
145        # containing the stable Network Data
146        _lpkts_sed.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).filter_wpan_dst64(SED).must_next().must_verify(
147            lambda p: {Ipv6Addr('2001:2:0:1::')} == set(p.thread_nwd.tlv.prefix) and p.thread_nwd.tlv.border_router.
148            flag.p == [1] and p.thread_nwd.tlv.border_router.flag.s == [1] and p.thread_nwd.tlv.border_router.flag.r ==
149            [1] and p.thread_nwd.tlv.border_router.flag.o == [1] and p.thread_nwd.tlv.stable == [1, 1, 1])
150
151        # Step 9: The DUT MUST send a MLE Child ID Response to MED_1,
152        # containing the full Network Data
153        _lpkts_med.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).filter_wpan_dst64(MED).must_next().must_verify(
154            lambda p: {Ipv6Addr('2001:2:0:1::'), Ipv6Addr('2001:2:0:2::')} == set(
155                p.thread_nwd.tlv.prefix) and p.thread_nwd.tlv.border_router.flag.p == [1, 1] and p.thread_nwd.tlv.
156            border_router.flag.s == [1, 1] and p.thread_nwd.tlv.border_router.flag.r == [1, 1] and p.thread_nwd.tlv.
157            border_router.flag.o == [1, 1] and p.thread_nwd.tlv.stable == [0, 1, 1, 1, 0, 0, 0])
158
159        # Step 10: The DUT MUST send a unicast MLE Child Update
160        # Response to each of MED_1 and SED_1
161        _lpkts_med.filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).filter_wpan_dst64(MED).must_next().must_verify(
162            lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV} <= set(p.mle.tlv.type))
163        _lpkts_sed.filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).filter_wpan_dst64(SED).must_next().must_verify(
164            lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV} <= set(p.mle.tlv.type))
165
166
167if __name__ == '__main__':
168    unittest.main()
169