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 config 31import re 32import unittest 33 34import thread_cert 35 36LEADER = 1 37REED = 2 38 39SRV_0_ID = 0 40SRV_0_ENT_NUMBER = '123' 41SRV_0_SERVICE_DATA = '123' 42SRV_0_SERVER_DATA = 'abc' 43 44# Topology: 45# LEADER -- REED 46# 47 48 49class TestREEDAddressSolicitRejected(thread_cert.TestCase): 50 SUPPORT_NCP = False 51 52 TOPOLOGY = { 53 LEADER: { 54 'mode': 'rdn', 55 }, 56 REED: { 57 'mode': 'rdn', 58 }, 59 } 60 61 def testAddressSolicitRejectedBeforeSvrData(self): 62 self.nodes[LEADER].start() 63 self.nodes[LEADER].set_router_upgrade_threshold(1) 64 self.simulator.go(config.LEADER_STARTUP_DELAY) 65 self.assertEqual(self.nodes[LEADER].get_state(), 'leader') 66 67 self.nodes[REED].start() 68 self.simulator.go(5) 69 self.assertEqual(self.nodes[REED].get_state(), 'child') 70 71 self.nodes[REED].add_service(SRV_0_ENT_NUMBER, SRV_0_SERVICE_DATA, SRV_0_SERVER_DATA) 72 self.nodes[REED].register_netdata() 73 self.simulator.go(2) 74 75 self.assertEqual(self.hasAloc(REED, SRV_0_ID), True) 76 77 def testAddressSolicitRejectedAfterSvrData(self): 78 self.nodes[LEADER].start() 79 self.nodes[LEADER].set_router_upgrade_threshold(1) 80 self.simulator.go(config.LEADER_STARTUP_DELAY) 81 self.assertEqual(self.nodes[LEADER].get_state(), 'leader') 82 83 self.nodes[REED].set_router_selection_jitter(120) 84 self.nodes[REED].start() 85 # set routerupgradethreshold=1 on REED so that it won't send ADDR_SOL.req 86 self.nodes[REED].set_router_upgrade_threshold(1) 87 self.simulator.go(5) 88 self.assertEqual(self.nodes[REED].get_state(), 'child') 89 90 # restore routerupgradethreshold to 16 and add service 91 self.nodes[REED].set_router_upgrade_threshold(16) 92 self.nodes[REED].add_service(SRV_0_ENT_NUMBER, SRV_0_SERVICE_DATA, SRV_0_SERVER_DATA) 93 self.nodes[REED].register_netdata() 94 self.simulator.go(130) 95 self.assertEqual(self.hasAloc(REED, SRV_0_ID), True) 96 97 def hasAloc(self, node_id, service_id): 98 for addr in self.nodes[node_id].get_ip6_address(config.ADDRESS_TYPE.ALOC): 99 m = re.match('.*:fc(..)$', addr, re.I) 100 if m is not None: 101 if m.group(1) == str(service_id + 10): # for service_id=3 look for '...:fc13' 102 return True 103 104 return False 105 106 107if __name__ == '__main__': 108 unittest.main() 109