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_UPDATE_RESPONSE, MLE_CHILD_ID_REQUEST, MLE_CHILD_ID_RESPONSE, MLE_LINK_REQUEST, MLE_LINK_ACCEPT, MLE_LINK_ACCEPT_AND_REQUEST, ADDR_SOL_URI, SOURCE_ADDRESS_TLV, MODE_TLV, TIMEOUT_TLV, CHALLENGE_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MLE_FRAME_COUNTER_TLV, ROUTE64_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, NL_MAC_EXTENDED_ADDRESS_TLV, NL_RLOC16_TLV, NL_STATUS_TLV, NL_ROUTER_MASK_TLV, COAP_CODE_ACK 36from pktverify.packet_verifier import PacketVerifier 37from pktverify.null_field import nullField 38 39LEADER = 1 40ROUTER1 = 2 41ROUTER2 = 3 42 43# Test Purpose and Description: 44# ----------------------------- 45# The purpose of this test case is to validate that when the DUT sees a new router for the first time, it will synchronize using the New Router Neighbor Synchronization procedure. 46# 47# Test Topology: 48# ------------- 49# Leader 50# / \ 51# Router2 Router1[DUT] 52# 53# DUT Types: 54# ---------- 55# Router 56 57 58class Cert_5_1_12_NewRouterSync(thread_cert.TestCase): 59 USE_MESSAGE_FACTORY = False 60 61 TOPOLOGY = { 62 LEADER: { 63 'name': 'LEADER', 64 'mode': 'rdn', 65 'allowlist': [ROUTER1, ROUTER2] 66 }, 67 ROUTER1: { 68 'name': 'ROUTER_1', 69 'mode': 'rdn', 70 'allowlist': [LEADER] 71 }, 72 ROUTER2: { 73 'name': 'ROUTER_2', 74 'mode': 'rdn', 75 'allowlist': [LEADER] 76 }, 77 } 78 79 def test(self): 80 self.nodes[LEADER].start() 81 self.simulator.go(config.LEADER_STARTUP_DELAY) 82 self.assertEqual(self.nodes[LEADER].get_state(), 'leader') 83 84 self.nodes[ROUTER1].start() 85 self.simulator.go(config.ROUTER_STARTUP_DELAY) 86 self.assertEqual(self.nodes[ROUTER1].get_state(), 'router') 87 88 self.nodes[ROUTER2].start() 89 self.simulator.go(config.ROUTER_STARTUP_DELAY) 90 self.assertEqual(self.nodes[ROUTER2].get_state(), 'router') 91 92 self.simulator.go(10) 93 94 self.nodes[ROUTER1].add_allowlist(self.nodes[ROUTER2].get_addr64()) 95 self.nodes[ROUTER2].add_allowlist(self.nodes[ROUTER1].get_addr64()) 96 97 self.simulator.go(35) 98 99 def verify(self, pv): 100 pkts = pv.pkts 101 pv.summary.show() 102 103 LEADER = pv.vars['LEADER'] 104 ROUTER_1 = pv.vars['ROUTER_1'] 105 ROUTER_2 = pv.vars['ROUTER_2'] 106 107 # Step 1: Verify topology is formed correctly. 108 109 # Step 2: The DUT MUST send properly formatted MLE Advertisements with 110 # an IP Hop Limit of 255 to the Link-Local All Nodes multicast 111 # address (FF02::1). 112 # The following TLVs MUST be present in the MLE Advertisements: 113 # - Leader Data TLV 114 # - Route64 TLV 115 # - Source Address TLV 116 117 pv.verify_attached('ROUTER_1') 118 pkts.filter_wpan_src64(ROUTER_1).\ 119 filter_LLANMA().\ 120 filter_mle_cmd(MLE_ADVERTISEMENT).\ 121 filter(lambda p: { 122 LEADER_DATA_TLV, 123 ROUTE64_TLV, 124 SOURCE_ADDRESS_TLV 125 } <= set(p.mle.tlv.type) and\ 126 p.ipv6.hlim == 255).\ 127 must_next() 128 129 pv.verify_attached('ROUTER_2') 130 pkts.filter_wpan_src64(ROUTER_2).\ 131 filter_LLANMA().\ 132 filter_mle_cmd(MLE_ADVERTISEMENT).\ 133 filter(lambda p: { 134 LEADER_DATA_TLV, 135 ROUTE64_TLV, 136 SOURCE_ADDRESS_TLV 137 } <= set(p.mle.tlv.type) and\ 138 p.ipv6.hlim == 255).\ 139 must_next() 140 141 # Step 4: The DUT and Router_2 exchange unicast Link Request and unicast 142 # Link Accept messages OR Link Accept and Request messages. 143 # 144 # The Link Request Message MUST be unicast and contain 145 # the following TLVs: 146 # - Challenge TLV 147 # - Leader Data TLV 148 # - Source Address TLV 149 # - Version TLV 150 # - TLV Request TLV: Link Margin 151 # 152 # Link Accept or Link Accept And Request Messages MUST be 153 # Unicast. 154 # The following TLVs MUST be present in the Link Accept or 155 # Link Accept And Request Messages : 156 # - Leader Data TLV 157 # - Link-layer Frame Counter TLV 158 # - Link Margin TLV 159 # - Response TLV 160 # - Source Address TLV 161 # - Version TLV 162 # - TLV Request TLV: Link Margin 163 # - Challenge TLV (optional) 164 # - MLE Frame Counter TLV (optional) 165 # The Challenge TLV and TLV Request TLV MUST be included 166 # if the response is an Accept and Request message. 167 168 lq_src = ROUTER_1 169 lq_dst = ROUTER_2 170 _pkt = pkts.filter_mle_cmd(MLE_LINK_REQUEST).\ 171 filter(lambda p: { 172 CHALLENGE_TLV, 173 LEADER_DATA_TLV, 174 SOURCE_ADDRESS_TLV, 175 VERSION_TLV, 176 TLV_REQUEST_TLV, 177 LINK_MARGIN_TLV 178 } <= set(p.mle.tlv.type) and\ 179 p.mle.tlv.link_margin is nullField and\ 180 (p.wpan.src64 == ROUTER_1 or\ 181 p.wpan.src64 == ROUTER_2) 182 ).\ 183 must_next() 184 if _pkt.wpan.src64 != ROUTER_1: 185 _pkt.must_verify(lambda p: p.wpan.dst64 == ROUTER_1) 186 lq_src = ROUTER_2 187 lq_dst = ROUTER_1 188 189 _pkt = pkts.filter_wpan_src64(lq_dst).\ 190 filter_wpan_dst64(lq_src).\ 191 filter_mle_cmd2(MLE_LINK_ACCEPT, MLE_LINK_ACCEPT_AND_REQUEST).\ 192 filter(lambda p: { 193 LEADER_DATA_TLV, 194 LINK_LAYER_FRAME_COUNTER_TLV, 195 LINK_MARGIN_TLV, 196 RESPONSE_TLV, 197 SOURCE_ADDRESS_TLV, 198 VERSION_TLV 199 } <= set(p.mle.tlv.type) and\ 200 p.mle.tlv.link_margin is not nullField 201 ).\ 202 must_next() 203 if _pkt.mle.cmd == MLE_LINK_ACCEPT_AND_REQUEST: 204 _pkt.must_verify(lambda p: {CHALLENGE_TLV, TLV_REQUEST_TLV} <= set(p.mle.tlv.type)) 205 206 207if __name__ == '__main__': 208 unittest.main() 209