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, SVR_DATA_URI, MLE_DATA_RESPONSE, MLE_CHILD_ID_RESPONSE, MLE_CHILD_UPDATE_REQUEST, MLE_CHILD_UPDATE_RESPONSE, SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV, NETWORK_DATA_TLV, ACTIVE_TIMESTAMP_TLV, ADDRESS_REGISTRATION_TLV, NWD_COMMISSIONING_DATA_TLV, NWD_PREFIX_TLV, NWD_BORDER_ROUTER_TLV, NWD_6LOWPAN_ID_TLV, LINK_LOCAL_ALL_NODES_MULTICAST_ADDRESS
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_6_NetworkDataExpiration(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[ED1].start()
83        self.simulator.go(5)
84        self.assertEqual(self.nodes[ED1].get_state(), 'child')
85
86        self.nodes[SED1].start()
87        self.simulator.go(5)
88        self.assertEqual(self.nodes[SED1].get_state(), 'child')
89
90        self.collect_rlocs()
91        self.nodes[ROUTER].add_prefix('2001:2:0:1::/64', 'paros')
92        self.nodes[ROUTER].add_prefix('2001:2:0:2::/64', 'paro')
93        self.nodes[ROUTER].add_prefix('2001:2:0:3::/64', 'paos')
94        self.nodes[ROUTER].register_netdata()
95
96        # Set lowpan context of sniffer
97        self.simulator.set_lowpan_context(1, '2001:2:0:1::/64')
98        self.simulator.set_lowpan_context(2, '2001:2:0:2::/64')
99        self.simulator.set_lowpan_context(3, '2001:2:0:3::/64')
100
101        self.simulator.go(10)
102
103        addrs = self.nodes[ED1].get_addrs()
104        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
105        self.assertTrue(any('2001:2:0:2' in addr[0:10] for addr in addrs))
106        self.assertTrue(any('2001:2:0:3' in addr[0:10] for addr in addrs))
107        for addr in addrs:
108            if addr[0:3] == '200':
109                self.assertTrue(self.nodes[LEADER].ping(addr))
110
111        addrs = self.nodes[SED1].get_addrs()
112        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
113        self.assertFalse(any('2001:2:0:2' in addr[0:10] for addr in addrs))
114        self.assertTrue(any('2001:2:0:3' in addr[0:10] for addr in addrs))
115        for addr in addrs:
116            if addr[0:3] == '200':
117                self.assertTrue(self.nodes[LEADER].ping(addr))
118
119        self.nodes[ROUTER].remove_prefix('2001:2:0:3::/64')
120        self.nodes[ROUTER].register_netdata()
121        self.simulator.go(310)
122
123        addrs = self.nodes[ED1].get_addrs()
124        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
125        self.assertTrue(any('2001:2:0:2' in addr[0:10] for addr in addrs))
126        self.assertFalse(any('2001:2:0:3' in addr[0:10] for addr in addrs))
127        for addr in addrs:
128            if addr[0:3] == '200':
129                self.assertTrue(self.nodes[LEADER].ping(addr))
130
131        addrs = self.nodes[SED1].get_addrs()
132        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
133        self.assertFalse(any('2001:2:0:2' in addr[0:10] for addr in addrs))
134        self.assertFalse(any('2001:2:0:3' in addr[0:10] for addr in addrs))
135        for addr in addrs:
136            if addr[0:3] == '200':
137                self.assertTrue(self.nodes[LEADER].ping(addr))
138
139        self.nodes[ROUTER].stop()
140        self.simulator.go(10)
141
142    def verify(self, pv):
143        pkts = pv.pkts
144        pv.summary.show()
145
146        LEADER = pv.vars['LEADER']
147        ROUTER = pv.vars['ROUTER']
148        MED = pv.vars['MED']
149        SED = pv.vars['SED']
150        _lpkts = pkts.filter_wpan_src64(LEADER)
151
152        # Step 1: Ensure the topology is formed correctly
153        _lpkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).filter_wpan_dst64(SED).must_next()
154
155        # Step 4: The DUT Automatically sends a CoAP Response frame to Router_1
156        _lpkts.copy().filter_ipv6_dst(pv.vars['ROUTER_RLOC']).filter_coap_ack(SVR_DATA_URI).must_next()
157
158        # Step 5: The DUT MUST send a multicast MLE Data Response with
159        # the new network information collected from Router_1
160        _lpkts_med = _lpkts.copy()
161        _lpkts_sed = _lpkts.copy()
162        _lpkts.filter_LLANMA().filter_mle_cmd(MLE_DATA_RESPONSE).must_next().must_verify(
163            lambda p: {
164                NWD_COMMISSIONING_DATA_TLV, NWD_PREFIX_TLV, NWD_BORDER_ROUTER_TLV, NWD_6LOWPAN_ID_TLV, NWD_PREFIX_TLV,
165                NWD_BORDER_ROUTER_TLV, NWD_6LOWPAN_ID_TLV, NWD_PREFIX_TLV, NWD_BORDER_ROUTER_TLV, NWD_6LOWPAN_ID_TLV
166            } == set(p.thread_nwd.tlv.type) and {
167                Ipv6Addr('2001:2:0:1::'), Ipv6Addr('2001:2:0:2::'),
168                Ipv6Addr('2001:2:0:3::')
169            } == set(p.thread_nwd.tlv.prefix) and p.thread_nwd.tlv.stable == [0, 1, 1, 1, 0, 0, 0, 1, 1, 1])
170
171        # Step 7: The DUT MUST send a unicast MLE Child Update Response to MED_1
172        _lpkts_med.filter_wpan_dst64(MED).filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).must_next().must_verify(
173            lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV} < set(p.mle.tlv.type))
174
175        # Step 8: The DUT MUST send a unicast MLE Child Update Request to SED_1
176        _lpkts_sed.filter_wpan_dst64(SED).filter_mle_cmd(MLE_CHILD_UPDATE_REQUEST).must_next().must_verify(
177            lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, NETWORK_DATA_TLV, ACTIVE_TIMESTAMP_TLV} == set(
178                p.mle.tlv.type) and {
179                    NWD_PREFIX_TLV, NWD_BORDER_ROUTER_TLV, NWD_6LOWPAN_ID_TLV, NWD_PREFIX_TLV, NWD_BORDER_ROUTER_TLV,
180                    NWD_6LOWPAN_ID_TLV
181                } == set(p.thread_nwd.tlv.type) and {
182                    Ipv6Addr('2001:2:0:1::'), Ipv6Addr('2001:2:0:3::')
183                } == set(p.thread_nwd.tlv.prefix) and {0xFFFE, 0xFFFE} == set(p.thread_nwd.tlv.border_router_16))
184
185        # Step 10: The DUT MUST send a unicast MLE Child Update Response to SED_1
186        _pkt = _lpkts_sed.filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).filter_wpan_dst64(SED).must_next()
187        _pkt.must_verify(
188            lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV} < set(p.mle.tlv.type))
189
190        # Step 12: The DUT updates Router ID Set and removes Router_1
191        # from Network Data TLV after Router_1 power off
192        # Step 13: The DUT MUST multicast a MLE Data Response with the
193        # new network information
194        _lpkts.filter_LLANMA().filter_mle_cmd(MLE_DATA_RESPONSE).filter_ipv6_dst(
195            LINK_LOCAL_ALL_NODES_MULTICAST_ADDRESS
196        ).must_next().must_verify(
197            lambda p: {
198                NWD_COMMISSIONING_DATA_TLV, NWD_PREFIX_TLV, NWD_BORDER_ROUTER_TLV, NWD_6LOWPAN_ID_TLV, NWD_PREFIX_TLV,
199                NWD_BORDER_ROUTER_TLV, NWD_6LOWPAN_ID_TLV, NWD_PREFIX_TLV, NWD_BORDER_ROUTER_TLV, NWD_6LOWPAN_ID_TLV
200            } == set(p.thread_nwd.tlv.type) and
201            {Ipv6Addr('2001:2:0:1::'), Ipv6Addr('2001:2:0:2::'),
202             Ipv6Addr('2001:2:0:3::')} == set(p.thread_nwd.tlv.prefix) and p.mle.tlv.leader_data.data_version ==
203            (_pkt.mle.tlv.leader_data.data_version + 1) % 256 and p.mle.tlv.leader_data.stable_data_version ==
204            (_pkt.mle.tlv.leader_data.stable_data_version + 1) % 256)
205
206        # Step 15: The DUT MUST send a unicast MLE Child Update Response to MED_1
207        _lpkts_med.filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).filter_wpan_dst64(MED).must_next().must_verify(
208            lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV} < set(p.mle.tlv.type)
209            and p.mle.tlv.leader_data.data_version == (_pkt.mle.tlv.leader_data.data_version + 1) % 256 and p.mle.tlv.
210            leader_data.stable_data_version == (_pkt.mle.tlv.leader_data.stable_data_version + 1) % 256)
211
212        # Step 16: The DUT MUST send a unicast MLE Child Update Request to SED_1
213        _lpkts_sed.filter_mle_cmd(MLE_CHILD_UPDATE_REQUEST).filter_wpan_dst64(SED).must_next().must_verify(
214            lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, NETWORK_DATA_TLV, ACTIVE_TIMESTAMP_TLV} == set(
215                p.mle.tlv.type) and p.mle.tlv.leader_data.data_version ==
216            (_pkt.mle.tlv.leader_data.data_version + 1) % 256 and p.mle.tlv.leader_data.stable_data_version ==
217            (_pkt.mle.tlv.leader_data.stable_data_version + 1) % 256)
218
219        # Step 18: The DUT MUST send a unicast MLE Child Update Response to SED_1
220        _lpkts_sed.filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).filter_wpan_dst64(SED).must_next().must_verify(
221            lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV} < set(p.mle.tlv.type))
222
223
224if __name__ == '__main__':
225    unittest.main()
226