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 unittest 32 33import command 34import config 35import thread_cert 36 37# Test description: 38# This test verifies the SRP server and client properly handle SRP host 39# and service instance lease. 40# 41# Topology: 42# LEADER (SRP server) 43# | 44# | 45# ROUTER (SRP client) 46# 47 48SERVER = 1 49CLIENT = 2 50LEASE = 10 # Seconds 51KEY_LEASE = 20 # Seconds 52 53 54class SrpRegisterSingleService(thread_cert.TestCase): 55 USE_MESSAGE_FACTORY = False 56 SUPPORT_NCP = False 57 58 TOPOLOGY = { 59 SERVER: { 60 'name': 'SRP_SERVER', 61 'networkkey': '00112233445566778899aabbccddeeff', 62 'mode': 'rdn', 63 }, 64 CLIENT: { 65 'name': 'SRP_CLIENT', 66 'networkkey': '00112233445566778899aabbccddeeff', 67 'mode': 'rdn', 68 }, 69 } 70 71 def test(self): 72 server = self.nodes[SERVER] 73 client = self.nodes[CLIENT] 74 75 # 76 # 0. Start the server and client devices. 77 # 78 79 server.srp_server_set_enabled(True) 80 server.srp_server_set_lease_range(LEASE, LEASE, KEY_LEASE, KEY_LEASE) 81 server.start() 82 self.simulator.go(config.LEADER_STARTUP_DELAY) 83 self.assertEqual(server.get_state(), 'leader') 84 self.simulator.go(5) 85 86 client.srp_server_set_enabled(False) 87 client.start() 88 self.simulator.go(config.ROUTER_STARTUP_DELAY) 89 self.assertEqual(client.get_state(), 'router') 90 91 # 92 # 1. Register a single service and verify that it works. 93 # 94 95 client.srp_client_set_host_name('my-host') 96 client.srp_client_set_host_address('2001::1') 97 client.srp_client_start(server.get_addrs()[0], client.get_srp_server_port()) 98 client.srp_client_add_service('my-service', '_ipps._tcp', 12345) 99 self.simulator.go(2) 100 101 self.check_host_and_service(server, client) 102 103 # 104 # 2. Stop the client and wait for the service instance LEASE to expire. 105 # 106 107 client.srp_client_stop() 108 self.simulator.go(LEASE + 1) 109 110 # The SRP server should remove the host and service but retain their names 111 # since the the KEY LEASE hasn't expired yet. 112 self.assertEqual(server.srp_server_get_host('my-host')['deleted'], 'true') 113 self.assertEqual(server.srp_server_get_service('my-service', '_ipps._tcp')['deleted'], 'true') 114 115 # Start the client again, the same service should be successfully registered. 116 client.srp_client_start(server.get_addrs()[0], client.get_srp_server_port()) 117 self.simulator.go(2) 118 119 self.check_host_and_service(server, client) 120 121 # 122 # 3. Stop the client and wait for the KEY LEASE to expire. 123 # The host and service instance should be fully removed by the SRP server. 124 # 125 126 client.srp_client_stop() 127 self.simulator.go(KEY_LEASE + 1) 128 129 # The host and service are expected to be fully removed. 130 self.assertEqual(len(server.srp_server_get_hosts()), 0) 131 self.assertEqual(len(server.srp_server_get_services()), 0) 132 133 # Start the client again, the same service should be successfully registered. 134 client.srp_client_start(server.get_addrs()[0], client.get_srp_server_port()) 135 self.simulator.go(2) 136 137 self.check_host_and_service(server, client) 138 139 # 140 # 4. Clear the first service, shorten the lease time and register a second service. 141 # Verify that the lease time of the first service is not affected by new SRP 142 # registrations. 143 # 144 145 client.srp_client_clear_service('my-service', '_ipps._tcp') 146 server.srp_server_set_lease_range(50 * LEASE, 50 * LEASE, 50 * KEY_LEASE, 50 * KEY_LEASE) 147 client.srp_client_add_service('my-service2', '_ipps._tcp', 12345) 148 149 # Wait for the first service to expire. 150 self.simulator.go(LEASE + 2) 151 self.assertEqual(server.srp_server_get_service('my-service', '_ipps._tcp')['deleted'], 'true') 152 153 # Wait for the first service to be fully removed. 154 self.simulator.go(KEY_LEASE - LEASE + 2) 155 self.assertEqual(len(server.srp_server_get_services()), 1) 156 self.assertEqual(len(server.srp_server_get_hosts()), 1) 157 158 # 159 # 5. Clear the second service, lengthen the lease time and register a third service. 160 # Verify that the lease time of the second service is not affected by new SRP 161 # registrations. 162 # 163 164 client.srp_client_clear_service('my-service2', '_ipps._tcp') 165 server.srp_server_set_lease_range(LEASE, LEASE, KEY_LEASE, KEY_LEASE) 166 client.srp_client_add_service('my-service3', '_ipps._tcp', 12345) 167 168 # The second service has lease time of 50 * LEASE and should not expire. 169 self.simulator.go(LEASE + 2) 170 self.assertEqual(server.srp_server_get_service('my-service2', '_ipps._tcp')['deleted'], 'false') 171 172 # The second service has key-lease time of 50 * KEY_LEASE and should not expire. 173 self.simulator.go(KEY_LEASE - LEASE + 2) 174 self.assertEqual(len(server.srp_server_get_services()), 2) 175 self.assertEqual(len(server.srp_server_get_hosts()), 1) 176 177 def check_host_and_service(self, server, client): 178 """Check that we have properly registered host and service instance. 179 """ 180 181 client_services = client.srp_client_get_services() 182 print(client_services) 183 self.assertEqual(len(client_services), 1) 184 client_service = client_services[0] 185 186 # Verify that the client possesses correct service resources. 187 self.assertEqual(client_service['instance'], 'my-service') 188 self.assertEqual(client_service['name'], '_ipps._tcp') 189 self.assertEqual(int(client_service['port']), 12345) 190 self.assertEqual(int(client_service['priority']), 0) 191 self.assertEqual(int(client_service['weight']), 0) 192 193 # Verify that the client received a SUCCESS response for the server. 194 self.assertEqual(client_service['state'], 'Registered') 195 196 # Wait for a KEY LEASE period to make sure that the client has refreshed 197 # the host and service instance. 198 self.simulator.go(KEY_LEASE + 1) 199 200 server_services = server.srp_server_get_services() 201 print(server_services) 202 self.assertEqual(len(server_services), 1) 203 server_service = server_services[0] 204 205 # Verify that the server accepted the SRP registration and stored 206 # the same service resources. 207 self.assertEqual(server_service['deleted'], 'false') 208 self.assertEqual(server_service['instance'], client_service['instance']) 209 self.assertEqual(server_service['name'], client_service['name']) 210 self.assertEqual(int(server_service['port']), int(client_service['port'])) 211 self.assertEqual(int(server_service['priority']), int(client_service['priority'])) 212 self.assertEqual(int(server_service['weight']), int(client_service['weight'])) 213 self.assertEqual(server_service['host'], 'my-host') 214 215 server_hosts = server.srp_server_get_hosts() 216 print(server_hosts) 217 self.assertEqual(len(server_hosts), 1) 218 server_host = server_hosts[0] 219 220 self.assertEqual(server_host['deleted'], 'false') 221 self.assertEqual(server_host['fullname'], server_service['host_fullname']) 222 self.assertEqual(len(server_host['addresses']), 1) 223 self.assertEqual(ipaddress.ip_address(server_host['addresses'][0]), ipaddress.ip_address('2001::1')) 224 225 226if __name__ == '__main__': 227 unittest.main() 228