1#!/usr/bin/env python3 2# 3# Copyright (c) 2021, 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 ipaddress 31import logging 32import unittest 33 34import command 35import config 36import thread_cert 37 38# Test description: 39# This test verifies when an SRP server restarts, it will listen to a UDP port 40# that wasn't used in the last time. 41# 42# Topology: 43# 44# LEADER (SRP client) -- ROUTER (SRP server) 45# 46 47CLIENT = 1 48SERVER = 2 49 50REBOOT_TIMES = 25 51 52 53class SrpServerRebootPort(thread_cert.TestCase): 54 USE_MESSAGE_FACTORY = False 55 SUPPORT_NCP = False 56 57 TOPOLOGY = { 58 CLIENT: { 59 'name': 'SRP_CLIENT', 60 'mode': 'rdn', 61 }, 62 SERVER: { 63 'name': 'SRP_SERVER', 64 'mode': 'rdn', 65 }, 66 } 67 68 def test(self): 69 client = self.nodes[CLIENT] 70 server = self.nodes[SERVER] 71 72 # 73 # 0. Start the server & client devices. 74 # 75 76 client.srp_server_set_enabled(False) 77 client.start() 78 self.simulator.go(config.LEADER_STARTUP_DELAY) 79 self.assertEqual(client.get_state(), 'leader') 80 81 server.srp_server_set_enabled(True) 82 server.start() 83 self.simulator.go(config.ROUTER_STARTUP_DELAY) 84 self.assertEqual(server.get_state(), 'router') 85 86 # 87 # 1. Check auto start mode on client and check that server is used. 88 # 89 90 self.assertEqual(client.srp_client_get_auto_start_mode(), 'Enabled') 91 self.simulator.go(2) 92 self.assertEqual(client.srp_client_get_state(), 'Enabled') 93 self.assertTrue(server.has_ipaddr(client.srp_client_get_server_address())) 94 95 # 96 # 2. Reboot the server without any service registered. The server should 97 # switch to a new port after re-enabling. 98 # 99 old_port = server.get_srp_server_port() 100 server.srp_server_set_enabled(False) 101 self.simulator.go(5) 102 server.srp_server_set_enabled(True) 103 self.simulator.go(5) 104 self.assertNotEqual(old_port, server.get_srp_server_port()) 105 106 # 107 # 3. Register a service 108 # 109 client.srp_client_set_host_name('my-host') 110 client.srp_client_set_host_address('2001::1') 111 client.srp_client_add_service('my-service', '_ipps._tcp', 12345, 0, 0, ['abc', 'def=', 'xyz=XYZ']) 112 self.simulator.go(16) 113 self.check_host_and_service(server, client, '2001::1') 114 115 ports = [server.get_srp_server_port()] 116 117 # Reboot the SRP server several times 118 for i in range(REBOOT_TIMES): 119 # 120 # 4. Disable server and check client is stopped/disabled. 121 # 122 old_port = server.get_srp_server_port() 123 server.srp_server_set_enabled(False) 124 self.simulator.go(5) 125 126 # 127 # 5. Enable server and check client starts again. Verify that the 128 # server is using a different port, and the service have been 129 # re-registered. 130 # 131 server.srp_server_set_enabled(True) 132 self.simulator.go(16) 133 self.assertEqual(client.srp_client_get_state(), 'Enabled') 134 self.assertEqual(client.srp_client_get_server_address(), server.get_mleid()) 135 self.assertNotEqual(old_port, server.get_srp_server_port()) 136 self.check_host_and_service(server, client, '2001::1') 137 ports.append(server.get_srp_server_port()) 138 logging.info(f'ports = {ports}') 139 140 def check_host_and_service(self, server, client, host_addr): 141 # Check that we have properly registered host and service instance. 142 # Originally used in test_srp_register_single_service.py. 143 144 client_services = client.srp_client_get_services() 145 print(client_services) 146 self.assertEqual(len(client_services), 1) 147 client_service = client_services[0] 148 149 # Verify that the client possesses correct service resources. 150 self.assertEqual(client_service['instance'], 'my-service') 151 self.assertEqual(client_service['name'], '_ipps._tcp') 152 self.assertEqual(int(client_service['port']), 12345) 153 self.assertEqual(int(client_service['priority']), 0) 154 self.assertEqual(int(client_service['weight']), 0) 155 156 # Verify that the client received a SUCCESS response for the server. 157 self.assertEqual(client_service['state'], 'Registered') 158 159 server_services = server.srp_server_get_services() 160 self.assertEqual(len(server_services), 1) 161 server_service = server_services[0] 162 163 # Verify that the server accepted the SRP registration and stores 164 # the same service resources. 165 self.assertEqual(server_service['deleted'], 'false') 166 self.assertEqual(server_service['instance'], client_service['instance']) 167 self.assertEqual(server_service['name'], client_service['name']) 168 self.assertEqual(server_service['subtypes'], '(null)') 169 self.assertEqual(int(server_service['port']), int(client_service['port'])) 170 self.assertEqual(int(server_service['priority']), int(client_service['priority'])) 171 self.assertEqual(int(server_service['weight']), int(client_service['weight'])) 172 # We output value of TXT entry as HEX string. 173 print(server_service['TXT']) 174 self.assertEqual(server_service['TXT'], ['abc', 'def=', 'xyz=58595a']) 175 self.assertEqual(server_service['host'], 'my-host') 176 177 server_hosts = server.srp_server_get_hosts() 178 print(server_hosts) 179 self.assertEqual(len(server_hosts), 1) 180 server_host = server_hosts[0] 181 182 self.assertEqual(server_host['deleted'], 'false') 183 self.assertEqual(server_host['fullname'], server_service['host_fullname']) 184 self.assertEqual(len(server_host['addresses']), 1) 185 self.assertEqual(ipaddress.ip_address(server_host['addresses'][0]), ipaddress.ip_address(host_addr)) 186 187 188if __name__ == '__main__': 189 unittest.main() 190