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.start() 87 self.simulator.go(config.ROUTER_STARTUP_DELAY) 88 self.assertEqual(client.get_state(), 'router') 89 90 # 91 # 1. Register a single service and verify that it works. 92 # 93 94 self.assertEqual(client.srp_client_get_auto_start_mode(), 'Enabled') 95 96 client.srp_client_set_host_name('my-host') 97 client.srp_client_set_host_address('2001::1') 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_enable_auto_start_mode() 117 self.simulator.go(15) 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_enable_auto_start_mode() 135 self.simulator.go(15) 136 137 self.check_host_and_service(server, client) 138 139 # 140 # 4. Clear the first service, lengthen 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, shorten 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 # 178 # 6. Clear the third service. The host and services should expire in lease time. 179 # Verify that the second service and the third service are removed when their host 180 # expires. 181 # 182 client.srp_client_clear_service('my-service3', '_ipps._tcp') 183 self.simulator.go(LEASE + 2) 184 self.assertEqual(len(server.srp_server_get_services()), 2) 185 self.assertEqual(server.srp_server_get_service('my-service2', '_ipps._tcp')['deleted'], 'true') 186 self.assertEqual(server.srp_server_get_service('my-service3', '_ipps._tcp')['deleted'], 'true') 187 self.assertEqual(len(server.srp_server_get_hosts()), 1) 188 self.assertEqual(server.srp_server_get_host('my-host')['deleted'], 'true') 189 190 def check_host_and_service(self, server, client): 191 """Check that we have properly registered host and service instance. 192 """ 193 194 client_services = client.srp_client_get_services() 195 print(client_services) 196 self.assertEqual(len(client_services), 1) 197 client_service = client_services[0] 198 199 # Verify that the client possesses correct service resources. 200 self.assertEqual(client_service['instance'], 'my-service') 201 self.assertEqual(client_service['name'], '_ipps._tcp') 202 self.assertEqual(int(client_service['port']), 12345) 203 self.assertEqual(int(client_service['priority']), 0) 204 self.assertEqual(int(client_service['weight']), 0) 205 206 # Verify that the client received a SUCCESS response for the server. 207 self.assertEqual(client_service['state'], 'Registered') 208 209 # Wait for a KEY LEASE period to make sure that the client has refreshed 210 # the host and service instance. 211 self.simulator.go(KEY_LEASE + 1) 212 213 server_services = server.srp_server_get_services() 214 print(server_services) 215 self.assertEqual(len(server_services), 1) 216 server_service = server_services[0] 217 218 # Verify that the server accepted the SRP registration and stored 219 # the same service resources. 220 self.assertEqual(server_service['deleted'], 'false') 221 self.assertEqual(server_service['instance'], client_service['instance']) 222 self.assertEqual(server_service['name'], client_service['name']) 223 self.assertEqual(int(server_service['port']), int(client_service['port'])) 224 self.assertEqual(int(server_service['priority']), int(client_service['priority'])) 225 self.assertEqual(int(server_service['weight']), int(client_service['weight'])) 226 self.assertEqual(server_service['host'], 'my-host') 227 228 server_hosts = server.srp_server_get_hosts() 229 print(server_hosts) 230 self.assertEqual(len(server_hosts), 1) 231 server_host = server_hosts[0] 232 233 self.assertEqual(server_host['deleted'], 'false') 234 self.assertEqual(server_host['fullname'], server_service['host_fullname']) 235 self.assertEqual(len(server_host['addresses']), 1) 236 self.assertEqual(ipaddress.ip_address(server_host['addresses'][0]), ipaddress.ip_address('2001::1')) 237 238 239if __name__ == '__main__': 240 unittest.main() 241