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 thread_cert 34from pktverify.consts import MLE_ADVERTISEMENT, MLE_CHILD_ID_REQUEST, MLE_DATA_RESPONSE, MLE_CHILD_ID_RESPONSE, MLE_CHILD_UPDATE_REQUEST, MLE_CHILD_UPDATE_RESPONSE, SVR_DATA_URI, SOURCE_ADDRESS_TLV, MODE_TLV, ADDRESS_REGISTRATION_TLV, LEADER_DATA_TLV, NETWORK_DATA_TLV, ACTIVE_TIMESTAMP_TLV, ROUTE64_TLV 35from pktverify.packet_verifier import PacketVerifier 36from pktverify.addrs import Ipv6Addr 37 38LEADER = 1 39ROUTER = 2 40ED2 = 3 41SED2 = 4 42 43MTDS = [ED2, SED2] 44 45 46class Cert_7_1_2_BorderRouterAsRouter(thread_cert.TestCase): 47 TOPOLOGY = { 48 LEADER: { 49 'name': 'LEADER', 50 'mode': 'rdn', 51 'allowlist': [ROUTER] 52 }, 53 ROUTER: { 54 'name': 'ROUTER', 55 'mode': 'rdn', 56 'allowlist': [LEADER, ED2, SED2] 57 }, 58 ED2: { 59 'name': 'MED', 60 'is_mtd': True, 61 'mode': 'rn', 62 'allowlist': [ROUTER] 63 }, 64 SED2: { 65 'name': 'SED', 66 'is_mtd': True, 67 'mode': '-', 68 'timeout': config.DEFAULT_CHILD_TIMEOUT, 69 'allowlist': [ROUTER] 70 }, 71 } 72 73 def test(self): 74 self.nodes[LEADER].start() 75 self.simulator.go(5) 76 self.assertEqual(self.nodes[LEADER].get_state(), 'leader') 77 78 self.nodes[ROUTER].start() 79 self.simulator.go(5) 80 self.assertEqual(self.nodes[ROUTER].get_state(), 'router') 81 82 self.nodes[ROUTER].add_prefix('2001:2:0:1::/64', 'paros') 83 self.nodes[ROUTER].add_prefix('2001:2:0:2::/64', 'paro') 84 self.nodes[ROUTER].register_netdata() 85 86 # Set lowpan context of sniffer 87 self.simulator.set_lowpan_context(1, '2001:2:0:1::/64') 88 self.simulator.set_lowpan_context(2, '2001:2:0:2::/64') 89 90 self.nodes[ED2].start() 91 self.simulator.go(5) 92 self.assertEqual(self.nodes[ED2].get_state(), 'child') 93 94 self.nodes[SED2].start() 95 self.simulator.go(5) 96 self.assertEqual(self.nodes[SED2].get_state(), 'child') 97 98 self.collect_rloc16s() 99 addrs = self.nodes[ED2].get_addrs() 100 self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs)) 101 self.assertTrue(any('2001:2:0:2' in addr[0:10] for addr in addrs)) 102 for addr in addrs: 103 if addr[0:10] == '2001:2:0:1' or addr[0:10] == '2001:2:0:2': 104 self.assertTrue(self.nodes[LEADER].ping(addr)) 105 106 addrs = self.nodes[SED2].get_addrs() 107 self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs)) 108 self.assertFalse(any('2001:2:0:2' in addr[0:10] for addr in addrs)) 109 for addr in addrs: 110 if addr[0:10] == '2001:2:0:1' or addr[0:10] == '2001:2:0:2': 111 self.assertTrue(self.nodes[LEADER].ping(addr)) 112 113 def verify(self, pv): 114 pkts = pv.pkts 115 pv.summary.show() 116 117 LEADER = pv.vars['LEADER'] 118 ROUTER = pv.vars['ROUTER'] 119 MED = pv.vars['MED'] 120 SED = pv.vars['SED'] 121 _rpkts = pkts.filter_wpan_src64(ROUTER) 122 123 # Step 2: The DUT MUST send properly formatted MLE Advertisements 124 # The DUT MUST send a CoAP Server Data Notification message 125 # with the server’s information (Prefix, Border Router) to the Leader 126 _rpkts.filter_mle_cmd(MLE_CHILD_ID_REQUEST).must_next() 127 _rpkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify( 128 lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, ROUTE64_TLV} == set(p.mle.tlv.type)) 129 _rpkts.filter_coap_request(SVR_DATA_URI).must_next().must_verify( 130 lambda p: p.wpan.dst16 == pv.vars['LEADER_RLOC16'] and { 131 Ipv6Addr('2001:2:0:1::'), Ipv6Addr('2001:2:0:2::') 132 } == set(p.thread_nwd.tlv.prefix) and p.thread_nwd.tlv.border_router.flag.p == [1, 1] and p.thread_nwd.tlv. 133 border_router.flag.s == [1, 1] and p.thread_nwd.tlv.border_router.flag.r == [1, 1] and p.thread_nwd.tlv. 134 border_router.flag.o == [1, 1] and p.thread_nwd.tlv.stable == [1, 1, 0, 0]) 135 136 # Step 3: Automatically transmits a 2.04 Changed CoAP response to the DUT 137 # Step 4: The DUT MUST multicast the MLE Data Response message sent by the Leader 138 _rpkts.filter_mle_cmd(MLE_DATA_RESPONSE).must_next().must_verify( 139 lambda p: {SOURCE_ADDRESS_TLV, LEADER_DATA_TLV, NETWORK_DATA_TLV, ACTIVE_TIMESTAMP_TLV} == set(p.mle.tlv. 140 type)) 141 142 # Step 5: MED_1 to attach to DUT and request complete network data 143 # Step 6: The DUT MUST send an MLE Child ID Response to MED_1 144 p = _rpkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next() 145 p.must_verify(lambda p: p.wpan.dst64 == MED and {Ipv6Addr('2001:2:0:1::'), 146 Ipv6Addr('2001:2:0:2::')} == set(p.thread_nwd.tlv.prefix)) 147 _rpkts_med = _rpkts.copy() 148 149 # Step 7: SED_1 to attach to DUT and request only stable data 150 # Step 8: The DUT MUST send an MLE Child Response to SED_1 151 _rpkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next().must_verify( 152 lambda p: p.wpan.dst64 == SED and {Ipv6Addr('2001:2:0:1::')} == set( 153 p.thread_nwd.tlv.prefix) and p.thread_nwd.tlv.border_router_16 == [0xFFFE]) 154 _rpkts_sed = _rpkts.copy() 155 156 # Step 9: SED_1 and MED_1 send its configured global address to the DUT 157 # Step 10: The DUT MUST send a Child Update Response to MED_1 & SED_1 158 _med_pkt = pkts.range( 159 _rpkts_med.index).filter_wpan_src64(MED).filter_mle_cmd(MLE_CHILD_UPDATE_REQUEST).must_next() 160 _sed_pkt = pkts.range( 161 _rpkts_sed.index).filter_wpan_src64(SED).filter_mle_cmd(MLE_CHILD_UPDATE_REQUEST).must_next() 162 _rpkts_med.filter_wpan_dst64(MED).filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).must_next().must_verify( 163 lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, ADDRESS_REGISTRATION_TLV} <= set(p.mle.tlv.type) and p.wpan.dst64 164 == MED and set(p.mle.tlv.addr_reg_iid) < set(_med_pkt.mle.tlv.addr_reg_iid)) 165 _rpkts_sed.filter_wpan_dst64(SED).filter_mle_cmd(MLE_CHILD_UPDATE_RESPONSE).must_next().must_verify( 166 lambda p: {SOURCE_ADDRESS_TLV, MODE_TLV, ADDRESS_REGISTRATION_TLV} <= set(p.mle.tlv.type) and set( 167 p.mle.tlv.addr_reg_iid) < set(_sed_pkt.mle.tlv.addr_reg_iid)) 168 169 170if __name__ == '__main__': 171 unittest.main() 172