1#!/usr/bin/env python3 2# 3# Copyright (c) 2020, 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 31import copy 32 33import config 34import thread_cert 35from pktverify.consts import WIRESHARK_OVERRIDE_PREFS, MLE_DATA_RESPONSE, SVR_DATA_URI, NWD_6LOWPAN_ID_TLV, NL_RLOC16_TLV 36from pktverify.packet_verifier import PacketVerifier 37from pktverify.addrs import Ipv6Addr 38from pktverify.null_field import nullField 39 40LEADER = 1 41ROUTER = 2 42FED = 3 43 44PREFIX_2001 = '2001::/64' 45PREFIX_2002 = '2002::/64' 46 47# Test Purpose and Description: 48# ----------------------------- 49# The purpose of this test case is to verify that when global prefix information 50# is set on the FED, the DUT properly disseminates the associated network data. 51# It also verifies that the DUT sends revised server data information to the 52# Leader when the FED is removed. 53# 54# Test Topology: 55# ------------- 56# FED 57# | 58# Router(DUT) 59# | 60# Leader 61# 62# DUT Types: 63# ---------- 64# Router 65 66 67class Cert_7_1_8_BorderRouterAsFED(thread_cert.TestCase): 68 USE_MESSAGE_FACTORY = False 69 70 TOPOLOGY = { 71 LEADER: { 72 'name': 'LEADER', 73 'mode': 'rdn', 74 'allowlist': [ROUTER] 75 }, 76 ROUTER: { 77 'name': 'ROUTER', 78 'mode': 'rdn', 79 'allowlist': [LEADER, FED] 80 }, 81 FED: { 82 'name': 'FED', 83 'mode': 'rdn', 84 'router_upgrade_threshold': 0, 85 'allowlist': [ROUTER] 86 }, 87 } 88 # override wireshark preferences with case needed parameters 89 CASE_WIRESHARK_PREFS = copy.deepcopy(WIRESHARK_OVERRIDE_PREFS) 90 CASE_WIRESHARK_PREFS['6lowpan.context1'] = PREFIX_2001 91 CASE_WIRESHARK_PREFS['6lowpan.context2'] = PREFIX_2002 92 93 def test(self): 94 self.nodes[LEADER].start() 95 self.simulator.go(config.LEADER_STARTUP_DELAY) 96 self.assertEqual(self.nodes[LEADER].get_state(), 'leader') 97 98 self.nodes[ROUTER].start() 99 self.simulator.go(config.ROUTER_STARTUP_DELAY) 100 self.assertEqual(self.nodes[ROUTER].get_state(), 'router') 101 102 self.nodes[FED].start() 103 self.simulator.go(3) 104 self.assertEqual(self.nodes[FED].get_state(), 'child') 105 106 self.collect_rlocs() 107 self.collect_rloc16s() 108 self.collect_leader_aloc(LEADER) 109 110 self.nodes[FED].add_prefix(PREFIX_2001, 'paros') 111 self.nodes[FED].add_prefix(PREFIX_2002, 'paro') 112 self.nodes[FED].register_netdata() 113 self.simulator.go(10) 114 115 self.nodes[FED].stop() 116 self.simulator.go(250) 117 118 def verify(self, pv): 119 pkts = pv.pkts 120 pv.summary.show() 121 122 LEADER = pv.vars['LEADER'] 123 LEADER_RLOC = pv.vars['LEADER_RLOC'] 124 LEADER_ALOC = pv.vars['LEADER_ALOC'] 125 ROUTER = pv.vars['ROUTER'] 126 ROUTER_RLOC = pv.vars['ROUTER_RLOC'] 127 ROUTER_RLOC16 = pv.vars['ROUTER_RLOC16'] 128 FED = pv.vars['FED'] 129 FED_RLOC = pv.vars['FED_RLOC'] 130 FED_RLOC16 = pv.vars['FED_RLOC16'] 131 132 # Step 1: Ensure topology is formed correctly 133 pv.verify_attached('ROUTER', 'LEADER') 134 pv.verify_attached('FED', 'ROUTER', 'MTD') 135 136 # Step 2: FED automatically sends a CoAP Server Data Notification 137 # message with the server’s information (Prefix, Border Router) 138 # to the Leader. 139 pkts.filter_wpan_src64(FED).\ 140 filter_ipv6_dst(LEADER_ALOC).\ 141 filter_coap_request(SVR_DATA_URI).\ 142 filter(lambda p: { 143 Ipv6Addr(PREFIX_2001[:-3]), 144 Ipv6Addr(PREFIX_2002[:-3]) 145 } == set(p.thread_nwd.tlv.prefix) and\ 146 p.thread_nwd.tlv.border_router_16 == [FED_RLOC16, FED_RLOC16] 147 ).\ 148 must_next() 149 150 # Step 3: Leader transmits a 2.04 Changed CoAP response to the DUT. 151 # And transmits multicast MLE Data Response with the new information 152 # collected, adding also 6LoWPAN ID TLV for the prefix set on FED. 153 with pkts.save_index(): 154 pkts.filter_wpan_src64(LEADER).\ 155 filter_ipv6_dst(FED_RLOC).\ 156 filter_coap_ack(SVR_DATA_URI).\ 157 must_next() 158 with pkts.save_index(): 159 pkts.filter_wpan_src64(LEADER).\ 160 filter_LLANMA().\ 161 filter_mle_cmd(MLE_DATA_RESPONSE).\ 162 filter(lambda p: { 163 Ipv6Addr(PREFIX_2001[:-3]), 164 Ipv6Addr(PREFIX_2002[:-3]) 165 } == set(p.thread_nwd.tlv.prefix) and\ 166 NWD_6LOWPAN_ID_TLV in p.thread_nwd.tlv.type and\ 167 p.thread_nwd.tlv.border_router.flag.p == [1, 1] and\ 168 p.thread_nwd.tlv.border_router.flag.s == [1, 1] and\ 169 p.thread_nwd.tlv.border_router.flag.r == [1, 1] and\ 170 p.thread_nwd.tlv.border_router.flag.o == [1, 1] and\ 171 p.thread_nwd.tlv.stable == [0, 1, 1, 1, 0, 0, 0] 172 ).\ 173 must_next() 174 175 # Step 4: The DUT transmits multicast MLE Data Response with the new information 176 # collected, adding also 6LoWPAN ID TLV for the prefix set on FED. 177 pkts.filter_wpan_src64(ROUTER).\ 178 filter_LLANMA().\ 179 filter_mle_cmd(MLE_DATA_RESPONSE).\ 180 filter(lambda p: { 181 Ipv6Addr(PREFIX_2001[:-3]), 182 Ipv6Addr(PREFIX_2002[:-3]) 183 } == set(p.thread_nwd.tlv.prefix) and\ 184 NWD_6LOWPAN_ID_TLV in p.thread_nwd.tlv.type and\ 185 p.thread_nwd.tlv.border_router.flag.p == [1, 1] and\ 186 p.thread_nwd.tlv.border_router.flag.s == [1, 1] and\ 187 p.thread_nwd.tlv.border_router.flag.r == [1, 1] and\ 188 p.thread_nwd.tlv.border_router.flag.o == [1, 1] and\ 189 p.thread_nwd.tlv.stable == [0, 1, 1, 1, 0, 0, 0] 190 ).\ 191 must_next() 192 193 # Step 6: The DUT notifies Leader of removed server’s (FED’s) RLOC16. 194 # The DUT MUST send a CoAP Server Data Notification frame 195 # to the Leader containing only the removed server’s RLOC16: 196 # CoAP Request URI 197 # coap://[<Leader address>]:MM/a/sd 198 # CoAP Payload 199 # RLOC16 TLV 200 pkts.filter_wpan_src64(ROUTER).\ 201 filter_coap_request(SVR_DATA_URI).\ 202 filter(lambda p: 203 p.thread_address.tlv.type == [NL_RLOC16_TLV] and\ 204 p.thread_address.tlv.rloc16 == FED_RLOC16 205 ).\ 206 must_next() 207 208 209if __name__ == '__main__': 210 unittest.main() 211