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_RESPONSE, MLE_DATA_REQUEST, MLE_CHILD_UPDATE_REQUEST, LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV, MODE_TLV, TIMEOUT_TLV, TLV_REQUEST_TLV, NETWORK_DATA_TLV
34from pktverify.packet_verifier import PacketVerifier
35
36LEADER = 1
37ED = 2
38
39
40class Cert_6_3_2_NetworkDataUpdate(thread_cert.TestCase):
41    TOPOLOGY = {
42        LEADER: {
43            'name': 'LEADER',
44            'mode': 'rdn',
45            'allowlist': [ED]
46        },
47        ED: {
48            'name': 'MED',
49            'is_mtd': True,
50            'mode': 'rn',
51            'timeout': 10,
52            'allowlist': [LEADER]
53        },
54    }
55
56    def test(self):
57        self.nodes[LEADER].start()
58        self.simulator.go(5)
59        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
60
61        self.nodes[ED].start()
62        self.simulator.go(5)
63        self.assertEqual(self.nodes[ED].get_state(), 'child')
64
65        self.nodes[LEADER].add_prefix('2001:2:0:1::/64', 'paros')
66        self.nodes[LEADER].register_netdata()
67
68        # Set lowpan context of sniffer
69        self.simulator.set_lowpan_context(1, '2001:2:0:1::/64')
70
71        self.simulator.go(5)
72
73        addrs = self.nodes[ED].get_addrs()
74        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
75        for addr in addrs:
76            if addr[0:10] == '2001:2:0:1':
77                self.assertTrue(self.nodes[LEADER].ping(addr))
78
79        self.nodes[LEADER].remove_allowlist(self.nodes[ED].get_addr64())
80        self.nodes[ED].remove_allowlist(self.nodes[LEADER].get_addr64())
81
82        self.nodes[LEADER].add_prefix('2001:2:0:2::/64', 'paros')
83        self.nodes[LEADER].register_netdata()
84
85        # Set lowpan context of sniffer
86        self.simulator.set_lowpan_context(2, '2001:2:0:2::/64')
87
88        self.simulator.go(5)
89
90        self.nodes[LEADER].add_allowlist(self.nodes[ED].get_addr64())
91        self.nodes[ED].add_allowlist(self.nodes[LEADER].get_addr64())
92        self.simulator.go(10)
93
94        addrs = self.nodes[ED].get_addrs()
95        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
96        self.assertTrue(any('2001:2:0:2' in addr[0:10] for addr in addrs))
97        for addr in addrs:
98            if addr[0:10] == '2001:2:0:1' or addr[0:10] == '2001:2:0:2':
99                self.assertTrue(self.nodes[LEADER].ping(addr))
100
101    def verify(self, pv):
102        pkts = pv.pkts
103        pv.summary.show()
104
105        LEADER = pv.vars['LEADER']
106        MED = pv.vars['MED']
107        _epkts = pkts.filter_wpan_src64(MED)
108
109        # Step 1: Ensure the topology is formed correctly
110        pkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).filter_wpan_src64(LEADER).must_next()
111
112        # Step 3: The DUT MUST send a MLE Child Update Request to the Leader
113        _epkts.range(pkts.index).filter_mle_cmd(MLE_CHILD_UPDATE_REQUEST).must_next().must_verify(
114            lambda p: p.wpan.dst64 == LEADER and {LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV, MODE_TLV, TIMEOUT_TLV
115                                                 } < set(p.mle.tlv.type))
116
117        # Step 10: The DUT MUST send a MLE Data Request frame to
118        # request the updated Network Data
119        _epkts.filter_mle_cmd(MLE_DATA_REQUEST).must_next().must_verify(
120            lambda p: {TLV_REQUEST_TLV, NETWORK_DATA_TLV} < set(p.mle.tlv.type))
121
122        # Step 12: The DUT MUST send a MLE Child Update Request to the Leader
123        _epkts.filter_mle_cmd(MLE_CHILD_UPDATE_REQUEST).filter_wpan_dst64(LEADER).must_next().must_verify(
124            lambda p: {ADDRESS_REGISTRATION_TLV, MODE_TLV, TIMEOUT_TLV} < set(p.mle.tlv.type))
125
126
127if __name__ == '__main__':
128    unittest.main()
129