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