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 copy 31import unittest 32 33import command 34import config 35import ipv6 36import mle 37import thread_cert 38from pktverify.consts import ADDR_SOL_URI, MLE_LINK_REQUEST, MLE_LINK_ACCEPT, MLE_LINK_ACCEPT_AND_REQUEST, SOURCE_ADDRESS_TLV, CHALLENGE_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, LEADER_DATA_TLV, VERSION_TLV 39from pktverify.packet_verifier import PacketVerifier 40from pktverify.null_field import nullField 41 42LEADER = 1 43DUT_ROUTER1 = 2 44ROUTER2 = 3 45DUT_REED = 17 46 47MLE_MIN_LINKS = 3 48 49# Test Purpose and Description: 50# ----------------------------- 51# The purpose of this test case is to validate the REED’s Synchronization 52# procedure after attaching to a network with multiple Routers. A REED 53# MUST process incoming Advertisements and perform a one-way frame-counter 54# synchronization with at least 3 neighboring Routers. When Router receives 55# unicast MLE Link Request from REED, it replies with MLE Link Accept. 56# 57# Test Topology: 58# ------------- 59# Router_15 - Leader 60# ... / \ 61# Router_n Router_1(DUT) 62# | 63# REED(DUT) 64# 65# DUT Types: 66# ---------- 67# Router 68# REED 69 70 71class Cert_5_2_7_REEDSynchronization_Base(thread_cert.TestCase): 72 USE_MESSAGE_FACTORY = False 73 74 TOPOLOGY = { 75 LEADER: { 76 'name': 'LEADER', 77 'mode': 'rdn', 78 }, 79 DUT_ROUTER1: { 80 'name': 'ROUTER_1', 81 'mode': 'rdn', 82 }, 83 ROUTER2: { 84 'name': 'ROUTER_2', 85 'mode': 'rdn', 86 }, 87 4: { 88 'name': 'ROUTER_3', 89 'mode': 'rdn', 90 }, 91 5: { 92 'name': 'ROUTER_4', 93 'mode': 'rdn', 94 }, 95 6: { 96 'name': 'ROUTER_5', 97 'mode': 'rdn', 98 }, 99 7: { 100 'name': 'ROUTER_6', 101 'mode': 'rdn', 102 }, 103 8: { 104 'name': 'ROUTER_7', 105 'mode': 'rdn', 106 }, 107 9: { 108 'name': 'ROUTER_8', 109 'mode': 'rdn', 110 }, 111 10: { 112 'name': 'ROUTER_9', 113 'mode': 'rdn', 114 }, 115 11: { 116 'name': 'ROUTER_10', 117 'mode': 'rdn', 118 }, 119 12: { 120 'name': 'ROUTER_11', 121 'mode': 'rdn', 122 }, 123 13: { 124 'name': 'ROUTER_12', 125 'mode': 'rdn', 126 }, 127 14: { 128 'name': 'ROUTER_13', 129 'mode': 'rdn', 130 }, 131 15: { 132 'name': 'ROUTER_14', 133 'mode': 'rdn', 134 }, 135 16: { 136 'name': 'ROUTER_15', 137 'mode': 'rdn', 138 }, 139 DUT_REED: { 140 'name': 'REED', 141 'mode': 'rdn', 142 }, 143 } 144 145 DUT = 0 146 147 def test(self): 148 self.nodes[LEADER].start() 149 self.simulator.go(5) 150 self.assertEqual(self.nodes[LEADER].get_state(), 'leader') 151 152 for i in range(2, 17): 153 self.nodes[i].start() 154 self.simulator.go(10) 155 156 for i in range(2, 17): 157 self.assertEqual(self.nodes[i].get_state(), 'router') 158 159 # Avoid DUT_REED attach to DUT_ROUTER1. 160 if self.DUT == DUT_REED: 161 self.nodes[DUT_REED].\ 162 add_allowlist(self.nodes[DUT_ROUTER1].get_addr64(), 163 config.RSSI['LINK_QULITY_1']) 164 165 if self.DUT == DUT_ROUTER1: 166 self.nodes[DUT_REED].\ 167 add_allowlist(self.nodes[DUT_ROUTER1].get_addr64(), 168 config.RSSI['LINK_QULITY_1']) 169 self.nodes[DUT_REED].add_allowlist(self.nodes[ROUTER2].get_addr64()) 170 self.nodes[DUT_REED].enable_allowlist() 171 172 self.nodes[DUT_REED].start() 173 self.simulator.go(config.MAX_ADVERTISEMENT_INTERVAL) 174 self.assertEqual(self.nodes[DUT_REED].get_state(), 'child') 175 176 self.simulator.go(config.MAX_ADVERTISEMENT_INTERVAL) 177 178 def verify(self, pv): 179 pkts = pv.pkts 180 pv.summary.show() 181 182 LEADER = pv.vars['LEADER'] 183 ROUTER_1 = pv.vars['ROUTER_1'] 184 REED = pv.vars['REED'] 185 186 # Step 1: Verify topology is formed correctly except REED. 187 188 for i in range(1, 16): 189 with pkts.save_index(): 190 pv.verify_attached('ROUTER_%d' % i) 191 192 # Step 2: REED attaches to the network and MUST NOT attempt to become 193 # an active router by sending an Address Solicit Request 194 195 pv.verify_attached('REED') 196 pkts.filter_wpan_src64(REED).\ 197 filter_coap_request(ADDR_SOL_URI).\ 198 filter(lambda p: { 199 NL_MAC_EXTENDED_ADDRESS_TLV, 200 NL_STATUS_TLV 201 } == set(p.coap.tlv.type) 202 ).\ 203 must_not_next() 204 205 # Step 3: REED sends a unicast Link Request message to at lease 3 Routers 206 # 207 # The Link Request Message MUST contain the following TLVs: 208 # - Challenge TLV 209 # - Leader Data TLV 210 # - Source Address TLV 211 # - Version TLV 212 213 if self.DUT == DUT_REED: 214 for i in range(0, MLE_MIN_LINKS): 215 pkts.filter_wpan_src64(REED).\ 216 filter_mle_cmd(MLE_LINK_REQUEST).\ 217 filter(lambda p: { 218 CHALLENGE_TLV, 219 LEADER_DATA_TLV, 220 SOURCE_ADDRESS_TLV, 221 VERSION_TLV 222 } <= set(p.mle.tlv.type) 223 ).\ 224 must_next() 225 226 # Step 4: Router_1 sends Link Accept message 227 # The following TLVs MUST be present in the Link Accept message: 228 # - Link-layer Frame Counter TLV 229 # - Source Address TLV 230 # - Response TLV 231 # - Version TLV 232 # - MLE Frame Counter TLV (optional) 233 # 234 # The recipient does not make any change to its local state and 235 # MUST NOT reply with a Link Accept And Request message. 236 237 if self.DUT == DUT_ROUTER1: 238 pkts.filter_wpan_dst64(REED).\ 239 filter_wpan_src64(ROUTER_1).\ 240 filter_mle_cmd(MLE_LINK_ACCEPT).\ 241 filter(lambda p: { 242 LINK_LAYER_FRAME_COUNTER_TLV, 243 RESPONSE_TLV, 244 SOURCE_ADDRESS_TLV, 245 VERSION_TLV 246 } <= set(p.mle.tlv.type) 247 ).\ 248 must_next() 249 pkts.filter_wpan_src64(REED).\ 250 filter_mle_cmd(MLE_LINK_ACCEPT_AND_REQUEST).\ 251 must_not_next() 252 253 254class Cert_5_2_7_REEDSynchronization_REED(Cert_5_2_7_REEDSynchronization_Base): 255 DUT = DUT_REED 256 257 258class Cert_5_2_7_REEDSynchronization_ROUTER(Cert_5_2_7_REEDSynchronization_Base): 259 DUT = DUT_ROUTER1 260 261 262if __name__ == '__main__': 263 unittest.main() 264