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_PARENT_REQUEST, MLE_PARENT_RESPONSE, MLE_CHILD_ID_REQUEST, MLE_CHILD_ID_RESPONSE, SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV, MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV, LINK_LAYER_FRAME_COUNTER_TLV, RESPONSE_TLV, LINK_MARGIN_TLV, CONNECTIVITY_TLV, TIMEOUT_TLV, TLV_REQUEST_TLV, ADDRESS16_TLV, NETWORK_DATA_TLV, ACTIVE_TIMESTAMP_TLV 35from pktverify.packet_verifier import PacketVerifier 36 37LEADER1 = 1 38ROUTER1 = 2 39ROUTER2 = 3 40ROUTER3 = 4 41 42 43class Cert_5_5_7_SplitMergeThreeWay(thread_cert.TestCase): 44 TOPOLOGY = { 45 LEADER1: { 46 'name': 'LEADER_1', 47 'mode': 'rdn', 48 'allowlist': [ROUTER1, ROUTER2, ROUTER3] 49 }, 50 ROUTER1: { 51 'name': 'ROUTER_1', 52 'mode': 'rdn', 53 'allowlist': [LEADER1] 54 }, 55 ROUTER2: { 56 'name': 'ROUTER_2', 57 'mode': 'rdn', 58 'allowlist': [LEADER1] 59 }, 60 ROUTER3: { 61 'name': 'ROUTER_3', 62 'mode': 'rdn', 63 'allowlist': [LEADER1] 64 }, 65 } 66 67 def _setUpLeader1(self): 68 self.nodes[LEADER1].add_allowlist(self.nodes[ROUTER1].get_addr64()) 69 self.nodes[LEADER1].add_allowlist(self.nodes[ROUTER2].get_addr64()) 70 self.nodes[LEADER1].add_allowlist(self.nodes[ROUTER3].get_addr64()) 71 self.nodes[LEADER1].enable_allowlist() 72 self.nodes[LEADER1].set_router_selection_jitter(1) 73 74 def test(self): 75 self.nodes[LEADER1].start() 76 self.simulator.go(config.LEADER_STARTUP_DELAY) 77 self.assertEqual(self.nodes[LEADER1].get_state(), 'leader') 78 79 self.nodes[ROUTER1].start() 80 self.simulator.go(config.ROUTER_STARTUP_DELAY) 81 self.assertEqual(self.nodes[ROUTER1].get_state(), 'router') 82 83 self.nodes[ROUTER2].start() 84 self.simulator.go(config.ROUTER_STARTUP_DELAY) 85 self.assertEqual(self.nodes[ROUTER2].get_state(), 'router') 86 87 self.nodes[ROUTER3].start() 88 self.simulator.go(config.ROUTER_STARTUP_DELAY) 89 self.assertEqual(self.nodes[ROUTER3].get_state(), 'router') 90 91 self.nodes[LEADER1].reset() 92 self._setUpLeader1() 93 self.simulator.go(140) 94 95 self.nodes[LEADER1].start() 96 self.simulator.go(30) 97 98 addrs = self.nodes[LEADER1].get_addrs() 99 for addr in addrs: 100 if addr[0:4] != 'fe80': 101 self.assertTrue(self.nodes[ROUTER1].ping(addr)) 102 103 addrs = self.nodes[ROUTER2].get_addrs() 104 for addr in addrs: 105 if addr[0:4] != 'fe80': 106 self.assertTrue(self.nodes[ROUTER1].ping(addr)) 107 108 addrs = self.nodes[ROUTER3].get_addrs() 109 for addr in addrs: 110 if addr[0:4] != 'fe80': 111 self.assertTrue(self.nodes[ROUTER1].ping(addr)) 112 113 def verify(self, pv): 114 pkts = pv.pkts 115 pv.summary.show() 116 117 LEADER = pv.vars['LEADER_1'] 118 ROUTER_1 = pv.vars['ROUTER_1'] 119 ROUTER_2 = pv.vars['ROUTER_2'] 120 ROUTER_3 = pv.vars['ROUTER_3'] 121 leader_pkts = pkts.filter_wpan_src64(LEADER) 122 router1_pkts = pkts.filter_wpan_src64(ROUTER_1) 123 router2_pkts = pkts.filter_wpan_src64(ROUTER_2) 124 router3_pkts = pkts.filter_wpan_src64(ROUTER_3) 125 126 # Step 2: The Leader and Router_1 MUST send properly formatted MLE Advertisements 127 leader_pkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next() 128 _lpkts = leader_pkts.copy() 129 _lpkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify( 130 lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type)) 131 132 router1_pkts.range(leader_pkts.index).filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify( 133 lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type)) 134 135 # Step 4: Each router forms a partition with the lowest possible partition ID 136 # Step 5: Router_1 MUST send MLE Parent Requests and MUST make two separate attempts 137 router1_pkts.filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify( 138 lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set( 139 p.mle.tlv.type) and p.mle.tlv.scan_mask.r == 1 and p.mle.tlv.scan_mask.e == 1) 140 lreset_start = router1_pkts.index 141 router1_pkts.filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify( 142 lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set( 143 p.mle.tlv.type) and p.mle.tlv.scan_mask.r == 1 and p.mle.tlv.scan_mask.e == 1) 144 145 # Step 7: Router_1 MUST attempt to attach to any other Partition 146 # within range by sending a MLE Parent Request. 147 router1_pkts.filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify( 148 lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(p.mle.tlv.type)) 149 lreset_stop = router1_pkts.index 150 151 # Step 3: The Leader MUST stop sending MLE advertisements. 152 leader_pkts.range(lreset_start, lreset_stop).filter_mle_cmd(MLE_ADVERTISEMENT).must_not_next() 153 154 # Step 6: The Leader does not respond to the Parent Requests 155 leader_pkts.range(lreset_start, lreset_stop).filter_mle_cmd(MLE_PARENT_RESPONSE).must_not_next() 156 157 # Step 8: Router_1 take over leader role of a new Partition and begin transmitting 158 # MLE Advertisements 159 router1_pkts.copy().filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify( 160 lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type)) 161 162 # Step 9: The Leader MUST send properly formatted MLE Parent Requests to the 163 # All-Routers multicast address 164 _lpkts.filter_mle_cmd(MLE_PARENT_REQUEST).must_next().must_verify( 165 lambda p: {MODE_TLV, CHALLENGE_TLV, SCAN_MASK_TLV, VERSION_TLV} == set(p.mle.tlv.type)) 166 167 # Step 10: Router_1 MUST send an MLE Parent Response 168 router1_pkts.filter_mle_cmd(MLE_PARENT_RESPONSE).must_next().must_verify( 169 lambda p: { 170 SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, LINK_LAYER_FRAME_COUNTER_TLV, RESPONSE_TLV, CHALLENGE_TLV, 171 LINK_MARGIN_TLV, CONNECTIVITY_TLV, VERSION_TLV 172 } <= set(p.mle.tlv.type)) 173 174 # Step 11: Leader send MLE Child ID Request 175 _lpkts.filter_mle_cmd(MLE_CHILD_ID_REQUEST).must_next().must_verify( 176 lambda p: { 177 RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MODE_TLV, TIMEOUT_TLV, VERSION_TLV, TLV_REQUEST_TLV, 178 ADDRESS16_TLV, NETWORK_DATA_TLV, ROUTE64_TLV, ACTIVE_TIMESTAMP_TLV 179 } <= set(p.mle.tlv.type)) 180 181 # Step 12: DUT (Router or Leader) MUST respond with a ICMPv6 Echo Reply 182 _lpkts.filter_ping_reply().must_next() 183 router2_pkts.filter_ping_reply().must_next() 184 router3_pkts.filter_ping_reply().must_next() 185 186 187if __name__ == '__main__': 188 unittest.main() 189