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 mle 34import thread_cert 35from pktverify.consts import MLE_PARENT_REQUEST, MLE_PARENT_RESPONSE, MLE_CHILD_ID_REQUEST, SOURCE_ADDRESS_TLV, MODE_TLV, TIMEOUT_TLV, CHALLENGE_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_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 36from pktverify.packet_verifier import PacketVerifier 37from pktverify.null_field import nullField 38 39LEADER = 1 40ROUTER1 = 2 41ROUTER2 = 3 42ROUTER3 = 4 43ROUTER4 = 5 44 45# Test Purpose and Description: 46# ----------------------------- 47# The purpose of this test case is to verify that the DUT chooses 48# to attach to a router with better connectivity 49# 50# Test Topology: 51# ------------- 52# Leader--Router1 53# / \ / 54# Router2 Router3 55# \ / 56# Router4[DUT] 57# 58# DUT Types: 59# ---------- 60# Router 61 62 63class Cert_5_1_08_RouterAttachConnectivity(thread_cert.TestCase): 64 USE_MESSAGE_FACTORY = False 65 66 TOPOLOGY = { 67 LEADER: { 68 'name': 'LEADER', 69 'mode': 'rdn', 70 'allowlist': [ROUTER1, ROUTER2, ROUTER3] 71 }, 72 ROUTER1: { 73 'name': 'ROUTER_1', 74 'mode': 'rdn', 75 'allowlist': [LEADER, ROUTER3] 76 }, 77 ROUTER2: { 78 'name': 'ROUTER_2', 79 'mode': 'rdn', 80 'allowlist': [LEADER, ROUTER4] 81 }, 82 ROUTER3: { 83 'name': 'ROUTER_3', 84 'mode': 'rdn', 85 'allowlist': [LEADER, ROUTER1, ROUTER4] 86 }, 87 ROUTER4: { 88 'name': 'ROUTER_4', 89 'mode': 'rdn', 90 'allowlist': [ROUTER2, ROUTER3] 91 }, 92 } 93 94 def test(self): 95 self.nodes[LEADER].start() 96 self.simulator.go(config.LEADER_STARTUP_DELAY) 97 self.assertEqual(self.nodes[LEADER].get_state(), 'leader') 98 99 for i in range(2, 5): 100 self.nodes[i].start() 101 102 self.simulator.go(config.ROUTER_STARTUP_DELAY) 103 104 for i in range(2, 5): 105 self.assertEqual(self.nodes[i].get_state(), 'router') 106 107 self.simulator.go(config.MAX_ADVERTISEMENT_INTERVAL) 108 109 self.nodes[ROUTER4].start() 110 self.simulator.go(config.ROUTER_STARTUP_DELAY) 111 self.assertEqual(self.nodes[ROUTER4].get_state(), 'router') 112 113 def verify(self, pv): 114 pkts = pv.pkts 115 pv.summary.show() 116 117 ROUTER_2 = pv.vars['ROUTER_2'] 118 ROUTER_3 = pv.vars['ROUTER_3'] 119 ROUTER_4 = pv.vars['ROUTER_4'] 120 121 # Step 1: Verify all routers and Leader are sending MLE advertisements. 122 for i in (1, 2, 3): 123 with pkts.save_index(): 124 pv.verify_attached('ROUTER_%d' % i) 125 126 # Step 3: DUT sends a MLE Parent Request with an IP hop limit of 127 # 255 to the Link-Local All Routers multicast address (FF02::2). 128 # The following TLVs MUST be present in the MLE Parent Request: 129 # - Challenge TLV 130 # - Mode TLV 131 # - Scan Mask TLV 132 # If the DUT sends multiple MLE Parent Requests 133 # - The first one MUST be sent only to all Routers 134 # - Subsequent ones MAY be sent to all Routers and REEDS 135 # - Version TLV 136 # If the first MLE Parent Request was sent to all Routers and 137 # REEDS, the test fails. 138 139 pkts.filter_wpan_src64(ROUTER_4).\ 140 filter_LLARMA().\ 141 filter_mle_cmd(MLE_PARENT_REQUEST).\ 142 filter(lambda p: { 143 CHALLENGE_TLV, 144 MODE_TLV, 145 SCAN_MASK_TLV, 146 VERSION_TLV 147 } <= set(p.mle.tlv.type) and\ 148 p.ipv6.hlim == 255 and\ 149 p.mle.tlv.scan_mask.r == 1 and\ 150 p.mle.tlv.scan_mask.e == 0 151 ).\ 152 must_next() 153 154 # Step 4: Router2 and Router3 respond with a MLE Parent Response. 155 # The following TLVs MUST be present in the MLE Parent Response: 156 # - Challenge TLV 157 # - Connectivity TLV 158 # - Leader Data TLV 159 # - Link-layer Frame Counter TLV 160 # - Link Margin TLV 161 # - Response TLV 162 # - Source Address 163 # - Version TLV 164 # - MLE Frame Counter TLV (optional) 165 166 for i in (2, 3): 167 with pkts.save_index(): 168 pkts.filter_wpan_src64(pv.vars['ROUTER_%d' % i]).\ 169 filter_wpan_dst64(ROUTER_4).\ 170 filter_mle_cmd(MLE_PARENT_RESPONSE).\ 171 filter(lambda p: { 172 CHALLENGE_TLV, 173 CONNECTIVITY_TLV, 174 LEADER_DATA_TLV, 175 LINK_LAYER_FRAME_COUNTER_TLV, 176 LINK_MARGIN_TLV, 177 RESPONSE_TLV, 178 SOURCE_ADDRESS_TLV, 179 VERSION_TLV 180 } <= set(p.mle.tlv.type) 181 ).\ 182 must_next() 183 184 # Step 5: DUT sends a MLE Child ID Request to Router3. 185 # The following TLVs MUST be present in the MLE Child ID Request: 186 # - Link-layer Frame Counter TLV 187 # - Mode TLV 188 # - Response TLV 189 # - Timeout TLV 190 # - TLV Request TLV 191 # - Address16 TLV 192 # - Network Data TLV 193 # - Route64 TLV (optional) 194 # - Version TLV 195 # - MLE Frame Counter TLV (optional) 196 # The following TLV MUST NOT be present in the MLE Child ID Request: 197 # - Address Registration TLV 198 199 _pkt = pkts.filter_wpan_src64(ROUTER_4).\ 200 filter_wpan_dst64(ROUTER_3).\ 201 filter_mle_cmd(MLE_CHILD_ID_REQUEST).\ 202 filter(lambda p: { 203 LINK_LAYER_FRAME_COUNTER_TLV, 204 MODE_TLV, 205 RESPONSE_TLV, 206 TIMEOUT_TLV, 207 TLV_REQUEST_TLV, 208 ADDRESS16_TLV, 209 NETWORK_DATA_TLV, 210 VERSION_TLV 211 } <= set(p.mle.tlv.type) and\ 212 p.mle.tlv.addr16 is nullField and\ 213 p.thread_nwd.tlv.type is nullField 214 ).\ 215 must_next() 216 _pkt.must_not_verify(lambda p: (ADDRESS_REGISTRATION_TLV) in p.mle.tlv.type) 217 218 219if __name__ == '__main__': 220 unittest.main() 221