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. Enable auto start mode on client and check that server is used. 88 # 89 90 self.assertEqual(client.srp_client_get_state(), 'Disabled') 91 client.srp_client_enable_auto_start_mode() 92 self.assertEqual(client.srp_client_get_auto_start_mode(), 'Enabled') 93 self.simulator.go(2) 94 self.assertEqual(client.srp_client_get_state(), 'Enabled') 95 self.assertTrue(server.has_ipaddr(client.srp_client_get_server_address())) 96 97 # 98 # 2. Reboot the server without any service registered. The server should 99 # listen to the same port after the reboot. 100 # 101 old_port = server.get_srp_server_port() 102 server.srp_server_set_enabled(False) 103 self.simulator.go(5) 104 server.srp_server_set_enabled(True) 105 self.simulator.go(5) 106 self.assertEqual(old_port, server.get_srp_server_port()) 107 108 # 109 # 3. Register a service 110 # 111 client.srp_client_set_host_name('my-host') 112 client.srp_client_set_host_address('2001::1') 113 client.srp_client_add_service('my-service', '_ipps._tcp', 12345, 0, 0, ['abc', 'def=', 'xyz=XYZ']) 114 self.simulator.go(5) 115 self.check_host_and_service(server, client, '2001::1') 116 117 ports = [server.get_srp_server_port()] 118 119 # Reboot the SRP server several times 120 for i in range(REBOOT_TIMES): 121 # 122 # 4. Disable server and check client is stopped/disabled. 123 # 124 old_port = server.get_srp_server_port() 125 server.srp_server_set_enabled(False) 126 self.simulator.go(5) 127 128 # 129 # 5. Enable server and check client starts again. Verify that the 130 # server is using a different port, and the service have been 131 # re-registered. 132 # 133 server.srp_server_set_enabled(True) 134 self.simulator.go(5) 135 self.assertEqual(client.srp_client_get_state(), 'Enabled') 136 self.assertEqual(client.srp_client_get_server_address(), server.get_mleid()) 137 self.assertNotEqual(old_port, server.get_srp_server_port()) 138 self.check_host_and_service(server, client, '2001::1') 139 ports.append(server.get_srp_server_port()) 140 logging.info(f'ports = {ports}') 141 142 def check_host_and_service(self, server, client, host_addr): 143 # Check that we have properly registered host and service instance. 144 # Originally used in test_srp_register_single_service.py. 145 146 client_services = client.srp_client_get_services() 147 print(client_services) 148 self.assertEqual(len(client_services), 1) 149 client_service = client_services[0] 150 151 # Verify that the client possesses correct service resources. 152 self.assertEqual(client_service['instance'], 'my-service') 153 self.assertEqual(client_service['name'], '_ipps._tcp') 154 self.assertEqual(int(client_service['port']), 12345) 155 self.assertEqual(int(client_service['priority']), 0) 156 self.assertEqual(int(client_service['weight']), 0) 157 158 # Verify that the client received a SUCCESS response for the server. 159 self.assertEqual(client_service['state'], 'Registered') 160 161 server_services = server.srp_server_get_services() 162 self.assertEqual(len(server_services), 1) 163 server_service = server_services[0] 164 165 # Verify that the server accepted the SRP registration and stores 166 # the same service resources. 167 self.assertEqual(server_service['deleted'], 'false') 168 self.assertEqual(server_service['instance'], client_service['instance']) 169 self.assertEqual(server_service['name'], client_service['name']) 170 self.assertEqual(server_service['subtypes'], '(null)') 171 self.assertEqual(int(server_service['port']), int(client_service['port'])) 172 self.assertEqual(int(server_service['priority']), int(client_service['priority'])) 173 self.assertEqual(int(server_service['weight']), int(client_service['weight'])) 174 # We output value of TXT entry as HEX string. 175 print(server_service['TXT']) 176 self.assertEqual(server_service['TXT'], ['abc', 'def=', 'xyz=58595a']) 177 self.assertEqual(server_service['host'], 'my-host') 178 179 server_hosts = server.srp_server_get_hosts() 180 print(server_hosts) 181 self.assertEqual(len(server_hosts), 1) 182 server_host = server_hosts[0] 183 184 self.assertEqual(server_host['deleted'], 'false') 185 self.assertEqual(server_host['fullname'], server_service['host_fullname']) 186 self.assertEqual(len(server_host['addresses']), 1) 187 self.assertEqual(ipaddress.ip_address(server_host['addresses'][0]), ipaddress.ip_address(host_addr)) 188 189 190if __name__ == '__main__': 191 unittest.main() 192