• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
31import copy
32
33import thread_cert
34import config
35from pktverify.consts import WPAN_DATA_REQUEST, MLE_PARENT_REQUEST, MLE_PARENT_RESPONSE, MLE_CHILD_UPDATE_REQUEST, MLE_CHILD_UPDATE_RESPONSE, MLE_CHILD_ID_REQUEST, MLE_CHILD_ID_RESPONSE, ADDR_SOL_URI, SOURCE_ADDRESS_TLV, MODE_TLV, TIMEOUT_TLV, CHALLENGE_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MLE_FRAME_COUNTER_TLV, ROUTE64_TLV, ADDRESS16_TLV, LEADER_DATA_TLV, NETWORK_DATA_TLV, TLV_REQUEST_TLV, SCAN_MASK_TLV, CONNECTIVITY_TLV, LINK_MARGIN_TLV, VERSION_TLV, ADDRESS_REGISTRATION_TLV, NL_MAC_EXTENDED_ADDRESS_TLV, NL_RLOC16_TLV, NL_STATUS_TLV, NL_ROUTER_MASK_TLV, COAP_CODE_ACK
36from pktverify.packet_verifier import PacketVerifier
37from pktverify.null_field import nullField
38
39LEADER = 1
40ROUTER = 2
41MTD = 3
42POLL_PERIOD = 3  # seconds
43
44# Test Purpose and Description:
45# -----------------------------
46# The purpose of this test case is to validate that after the DUT resets and receives
47# no response from its parent, it will reattach to the network through a different parent.
48#
49# Test Topology:
50# -------------
51#  Leader
52#    |
53#  Router
54#    |
55#   DUT
56#
57# DUT Types:
58# ----------
59#  ED
60#  SED
61
62
63class Cert_6_5_2_ChildResetReattach_Base(thread_cert.TestCase):
64    USE_MESSAGE_FACTORY = False
65    SUPPORT_NCP = False
66
67    TOPOLOGY = {
68        LEADER: {
69            'name': 'LEADER',
70            'mode': 'rdn',
71            'allowlist': [ROUTER]
72        },
73        ROUTER: {
74            'name': 'ROUTER',
75            'mode': 'rdn',
76            'allowlist': [LEADER, MTD]
77        },
78        MTD: {
79            'name': 'DUT',
80            'is_mtd': True,
81            'timeout': config.DEFAULT_CHILD_TIMEOUT,
82            'allowlist': [ROUTER]
83        },
84    }
85
86    def _setUpMTD(self):
87        self.nodes[MTD].add_allowlist(self.nodes[LEADER].get_addr64())
88        self.nodes[MTD].enable_allowlist()
89        if self.TOPOLOGY[MTD]['mode'] == '-':
90            self.nodes[MTD].set_pollperiod(POLL_PERIOD * 1000)
91
92    def test(self):
93        self.nodes[LEADER].start()
94        self.simulator.go(config.LEADER_STARTUP_DELAY)
95        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
96
97        self.nodes[ROUTER].start()
98        self.simulator.go(config.ROUTER_STARTUP_DELAY)
99        self.assertEqual(self.nodes[ROUTER].get_state(), 'router')
100
101        self.nodes[MTD].start()
102        self.simulator.go(5)
103        self.assertEqual(self.nodes[MTD].get_state(), 'child')
104        self.collect_rloc16s()
105
106        self.nodes[LEADER].clear_allowlist()
107        self.nodes[MTD].clear_allowlist()
108        self.nodes[ROUTER].stop()
109
110        self.nodes[MTD].reset()
111        self._setUpMTD()
112        self.simulator.go(5)
113        self.nodes[LEADER].add_allowlist(self.nodes[MTD].get_addr64())
114        self.simulator.go(5)
115        self.nodes[MTD].start()
116        self.simulator.go(10)
117        self.assertEqual(self.nodes[MTD].get_state(), 'child')
118        self.simulator.go(360)
119
120        self.collect_ipaddrs()
121        dut_addr = self.nodes[MTD].get_ip6_address(config.ADDRESS_TYPE.LINK_LOCAL)
122        self.nodes[LEADER].ping(dut_addr)
123        self.simulator.go(15)
124
125    def verify(self, pv):
126        pkts = pv.pkts
127        pv.summary.show()
128
129        LEADER = pv.vars['LEADER']
130        LEADER_LLA = pv.vars['LEADER_LLA']
131        ROUTER = pv.vars['ROUTER']
132        ROUTER_RLOC16 = pv.vars['ROUTER_RLOC16']
133        DUT = pv.vars['DUT']
134        DUT_LLA = pv.vars['DUT_LLA']
135
136        # Step 1: Ensure topology is formed correctly
137
138        pv.verify_attached('ROUTER', 'LEADER')
139        pv.verify_attached('DUT', 'ROUTER', 'MTD')
140
141        # Step 4: DUT sends an MLE Child Update Request.
142        #         The following TLVs MUST be included in the Child Update
143        #         Request:
144        #             - Mode TLV
145        #             - Address Registration TLV (optional)
146        #         If the DUT is a SED, it MUST resume polling after sending
147        #         MLE Child Update.
148        pkts.filter_wpan_src64(DUT).\
149            filter_wpan_dst64(ROUTER).\
150            filter_mle_cmd(MLE_CHILD_UPDATE_REQUEST).\
151            filter(lambda p: {
152                              MODE_TLV
153                             } < set(p.mle.tlv.type)
154                   ).\
155            must_next()
156        if self.TOPOLOGY[MTD]['mode'] == '-':
157            _pkt = pkts.filter_wpan_src64(DUT).\
158                filter_wpan_dst16(ROUTER_RLOC16).\
159                filter_wpan_cmd(WPAN_DATA_REQUEST).\
160                must_next()
161            pkts.filter_wpan_src64(DUT).\
162                filter_wpan_dst16(ROUTER_RLOC16).\
163                filter_wpan_cmd(WPAN_DATA_REQUEST).\
164                filter(lambda p:
165                       p.sniff_timestamp - _pkt.sniff_timestamp <=
166                       POLL_PERIOD + 0.1
167                      ).\
168                must_next()
169        lstart = pkts.index
170
171        # Step 6: The DUT MUST attach to the Leader
172        pkts.filter_wpan_src64(DUT).\
173            filter_LLARMA().\
174            filter_mle_cmd(MLE_PARENT_REQUEST).\
175            filter(lambda p: {
176                              CHALLENGE_TLV,
177                              MODE_TLV,
178                              SCAN_MASK_TLV,
179                              VERSION_TLV
180                              } <= set(p.mle.tlv.type) and\
181                   p.ipv6.hlim == 255 and\
182                   p.mle.tlv.scan_mask.r == 1 and\
183                   p.mle.tlv.scan_mask.e == 0 and\
184                   p.wpan.aux_sec.key_id_mode == 0x2
185                   ).\
186            must_next()
187        lend = pkts.index
188
189        # Step 5: No response from Router.
190        pkts.range(lstart, lend).\
191            filter_wpan_dst64(ROUTER).\
192            filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).\
193            must_not_next()
194
195        pkts.filter_wpan_src64(LEADER).\
196            filter_wpan_dst64(DUT).\
197            filter_mle_cmd(MLE_PARENT_RESPONSE).\
198            filter(lambda p: {
199                              CHALLENGE_TLV,
200                              CONNECTIVITY_TLV,
201                              LEADER_DATA_TLV,
202                              LINK_LAYER_FRAME_COUNTER_TLV,
203                              LINK_MARGIN_TLV,
204                              RESPONSE_TLV,
205                              SOURCE_ADDRESS_TLV,
206                              VERSION_TLV
207                               } <= set(p.mle.tlv.type)).\
208                   must_next()
209
210        pkts.filter_wpan_src64(DUT).\
211            filter_wpan_dst64(LEADER).\
212            filter_mle_cmd(MLE_CHILD_ID_REQUEST).\
213            filter(lambda p: {
214                              ADDRESS_REGISTRATION_TLV,
215                              LINK_LAYER_FRAME_COUNTER_TLV,
216                              MODE_TLV,
217                              RESPONSE_TLV,
218                              TIMEOUT_TLV,
219                              TLV_REQUEST_TLV,
220                              VERSION_TLV
221                             } <= set(p.mle.tlv.type) and\
222                   p.wpan.aux_sec.key_id_mode == 0x2
223                   ).\
224             must_next()
225
226        pkts.filter_wpan_src64(LEADER).\
227            filter_wpan_dst64(DUT).\
228            filter_mle_cmd(MLE_CHILD_ID_RESPONSE).\
229            filter(lambda p: {
230                              ADDRESS16_TLV,
231                              LEADER_DATA_TLV,
232                              NETWORK_DATA_TLV,
233                              SOURCE_ADDRESS_TLV
234                              } <= set(p.mle.tlv.type)
235                   ).\
236            must_next()
237
238        # Step 7: Leader Verifies connectivity by sending an ICMPv6 Echo Request
239        #          to the DUT link local address
240        #          The End Device MUST respond with ICMPv6 Echo Reply
241        _pkt = pkts.filter_ping_request().\
242            filter_ipv6_src_dst(LEADER_LLA, DUT_LLA).\
243            must_next()
244        pkts.filter_ping_reply(identifier=_pkt.icmpv6.echo.identifier).\
245            filter_ipv6_src_dst(DUT_LLA, LEADER_LLA).\
246              must_next()
247
248
249class Cert_6_5_2_ChildResetReattach_ED(Cert_6_5_2_ChildResetReattach_Base):
250    TOPOLOGY = copy.deepcopy(Cert_6_5_2_ChildResetReattach_Base.TOPOLOGY)
251    TOPOLOGY[MTD]['mode'] = 'rn'
252
253
254class Cert_6_5_2_ChildResetReattach_SED(Cert_6_5_2_ChildResetReattach_Base):
255    TOPOLOGY = copy.deepcopy(Cert_6_5_2_ChildResetReattach_Base.TOPOLOGY)
256    TOPOLOGY[MTD]['mode'] = '-'
257
258
259del (Cert_6_5_2_ChildResetReattach_Base)
260
261if __name__ == '__main__':
262    unittest.main()
263