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 33import config 34from pktverify.consts import MLE_ADVERTISEMENT, MLE_PARENT_REQUEST, MLE_PARENT_RESPONSE, MLE_CHILD_UPDATE_RESPONSE, MLE_CHILD_ID_REQUEST, ADDR_SOL_URI, SOURCE_ADDRESS_TLV, MODE_TLV, TIMEOUT_TLV, CHALLENGE_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, ROUTE64_TLV, ADDRESS16_TLV, LEADER_DATA_TLV, NETWORK_DATA_TLV, TLV_REQUEST_TLV, SCAN_MASK_TLV, VERSION_TLV, ADDRESS_REGISTRATION_TLV, ACTIVE_TIMESTAMP_TLV, NL_PARENT_PARTITION_CHANGE 35from pktverify.packet_verifier import PacketVerifier 36from pktverify.null_field import nullField 37 38LEADER = 1 39ROUTER1 = 2 40ROUTER2 = 3 41ED2 = 4 42ED3 = 5 43 44MTDS = [ED2, ED3] 45 46 47class Cert_5_5_3_SplitMergeChildren(thread_cert.TestCase): 48 SUPPORT_NCP = False 49 50 TOPOLOGY = { 51 LEADER: { 52 'name': 'LEADER', 53 'mode': 'rdn', 54 'allowlist': [ROUTER1, ROUTER2] 55 }, 56 ROUTER1: { 57 'name': 'ROUTER_1', 58 'mode': 'rdn', 59 'allowlist': [LEADER, ED2] 60 }, 61 ROUTER2: { 62 'name': 'ROUTER_2', 63 'mode': 'rdn', 64 'allowlist': [LEADER, ED3] 65 }, 66 ED2: { 67 'name': 'MED_2', 68 'is_mtd': True, 69 'mode': 'rn', 70 'allowlist': [ROUTER1] 71 }, 72 ED3: { 73 'name': 'MED_3', 74 'is_mtd': True, 75 'mode': 'rn', 76 'allowlist': [ROUTER2] 77 }, 78 } 79 80 def _setUpLeader(self): 81 self.nodes[LEADER].add_allowlist(self.nodes[ROUTER1].get_addr64()) 82 self.nodes[LEADER].add_allowlist(self.nodes[ROUTER2].get_addr64()) 83 self.nodes[LEADER].enable_allowlist() 84 self.nodes[LEADER].set_router_selection_jitter(1) 85 86 def test(self): 87 self.nodes[LEADER].start() 88 self.simulator.go(config.LEADER_STARTUP_DELAY) 89 self.assertEqual(self.nodes[LEADER].get_state(), 'leader') 90 91 self.nodes[ROUTER1].start() 92 self.simulator.go(config.ROUTER_STARTUP_DELAY) 93 self.assertEqual(self.nodes[ROUTER1].get_state(), 'router') 94 95 self.nodes[ROUTER2].start() 96 self.simulator.go(config.ROUTER_STARTUP_DELAY) 97 self.assertEqual(self.nodes[ROUTER1].get_state(), 'router') 98 99 self.nodes[ED2].start() 100 self.simulator.go(5) 101 self.assertEqual(self.nodes[ED2].get_state(), 'child') 102 103 self.nodes[ED3].start() 104 self.simulator.go(5) 105 self.assertEqual(self.nodes[ED2].get_state(), 'child') 106 107 self.nodes[LEADER].reset() 108 self._setUpLeader() 109 self.nodes[ROUTER2].set_preferred_partition_id(0xffffffff) 110 111 self.simulator.go(150) 112 113 self.assertEqual(self.nodes[ROUTER1].get_state(), 'leader') 114 self.assertEqual(self.nodes[ROUTER2].get_state(), 'leader') 115 116 self.nodes[LEADER].start() 117 self.simulator.go(config.ROUTER_STARTUP_DELAY) 118 self.assertEqual(self.nodes[LEADER].get_state(), 'router') 119 120 self.simulator.go(30) 121 self.assertEqual(self.nodes[ROUTER1].get_state(), 'router') 122 self.assertEqual(self.nodes[ROUTER2].get_state(), 'leader') 123 124 self.collect_rloc16s() 125 126 addrs = self.nodes[ED3].get_addrs() 127 for addr in addrs: 128 if addr[0:4] != 'fe80': 129 self.assertTrue(self.nodes[ED2].ping(addr)) 130 131 def verify(self, pv): 132 pkts = pv.pkts 133 pv.summary.show() 134 135 LEADER = pv.vars['LEADER'] 136 LEADER_RLOC16 = pv.vars['LEADER_RLOC16'] 137 ROUTER_1 = pv.vars['ROUTER_1'] 138 ROUTER_2 = pv.vars['ROUTER_2'] 139 MED_2 = pv.vars['MED_2'] 140 MED_3 = pv.vars['MED_3'] 141 _lpkts = pkts.filter_wpan_src64(LEADER) 142 _router1_pkts = pkts.filter_wpan_src64(ROUTER_1) 143 144 # Step 2: The Leader and Router_1 MUST send properly formatted MLE Advertisements 145 pkts.filter_wpan_src64(LEADER).filter_LLANMA().filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify( 146 lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type)) 147 _pkt = pkts.filter_wpan_src64(ROUTER_1).filter_LLANMA().filter_mle_cmd(MLE_ADVERTISEMENT).must_next() 148 _pkt.must_verify( 149 lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type) and p.ipv6.hlim == 255) 150 151 # Step 4: Router_1 MUST attempt to reattach to its original partition by 152 # sending MLE Parent Requests to the All-Routers multicast address 153 _router1_pkts.range(pkts.index).filter_LLARMA().filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify( 154 lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set( 155 p.mle.tlv.type) and p.mle.tlv.scan_mask.r == 1 and p.mle.tlv.scan_mask.e == 1 and p.ipv6.hlim == 255) 156 lreset_start = _router1_pkts.index 157 158 # Step 6: Router_1 MUST attempt to attach to any other Partition 159 # within range by sending a MLE Parent Request. 160 _router1_pkts.filter_LLARMA().filter_mle_cmd(MLE_PARENT_REQUEST).filter( 161 lambda p: p.mle.tlv.scan_mask.r == 1 and p.mle.tlv.scan_mask.e == 0).must_next().must_verify( 162 lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(p.mle.tlv.type 163 ) and p.ipv6.hlim == 255) 164 lreset_stop = _router1_pkts.index 165 166 # Step 3: The Leader MUST stop sending MLE advertisements. 167 _lpkts.range(lreset_start, lreset_stop).filter_LLARMA().filter_mle_cmd(MLE_ADVERTISEMENT).must_not_next() 168 169 # Step 5: Leader MUST NOT respond to the MLE Parent Requests 170 _lpkts.range(lreset_start, 171 lreset_stop).filter_wpan_src64(LEADER).filter_mle_cmd(MLE_PARENT_RESPONSE).must_not_next() 172 173 # Step 7: Router_1 takes over leader role of a new Partition and 174 # begin transmitting MLE Advertisements 175 with _router1_pkts.save_index(): 176 _router1_pkts.filter_LLANMA().filter_mle_cmd(MLE_ADVERTISEMENT).filter( 177 lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type) and p.mle.tlv. 178 leader_data.partition_id != _pkt.mle.tlv.leader_data.partition_id and p.mle.tlv.leader_data. 179 data_version != _pkt.mle.tlv.leader_data.data_version and p.mle.tlv.leader_data.stable_data_version != 180 _pkt.mle.tlv.leader_data.stable_data_version and p.ipv6.hlim == 255).must_next() 181 182 # Step 9: Router_1 MUST respond with an MLE Child Update Response, 183 # with the updated TLVs of the new partition 184 _router1_pkts.filter_wpan_dst64(MED_2).filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).must_next().must_verify( 185 lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, LEADER_DATA_TLV, ADDRESS_REGISTRATION_TLV} < set(p.mle.tlv.type)) 186 187 # Step 10: The Leader MUST send properly formatted MLE Parent 188 # Requests to the All-Routers multicast address 189 _lpkts.filter_LLARMA().filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify( 190 lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(p.mle.tlv.type 191 ) and p.ipv6.hlim == 255) 192 193 # Step 11: Leader send MLE Child ID Request to Router_2 194 _lpkts.filter_wpan_dst64(ROUTER_2).filter_mle_cmd(MLE_CHILD_ID_REQUEST).must_next().must_verify( 195 lambda p: { 196 RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MODE_TLV, TIMEOUT_TLV, VERSION_TLV, TLV_REQUEST_TLV, 197 ADDRESS16_TLV, NETWORK_DATA_TLV, ROUTE64_TLV, ACTIVE_TIMESTAMP_TLV 198 } < set(p.mle.tlv.type)) 199 200 # Step 12: Leader send MLE ADVERTISEMENT 201 _lpkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify( 202 lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type) and p.ipv6.hlim == 255) 203 204 # Step 13: Router_1 send an Address Solicit Request 205 _router1_pkts.filter_coap_request(ADDR_SOL_URI).must_next().must_verify( 206 lambda p: p.wpan.dst16 == LEADER_RLOC16 and p.thread_address.tlv.ext_mac_addr is not nullField and p. 207 thread_address.tlv.status == NL_PARENT_PARTITION_CHANGE) 208 209 # Step 14: MED_2 MUST receive an ICMPv6 Echo Reply from MED_3 210 p = pkts.filter_ping_request().filter_wpan_src64(MED_2).must_next() 211 pkts.filter_ping_reply(identifier=p.icmpv6.echo.identifier).filter_wpan_src64(MED_3).must_next() 212 213 214if __name__ == '__main__': 215 unittest.main() 216