[tests] add CI tests for SRP server & client (#6074)

This commit is contained in:
kangping
2021-01-14 13:23:56 +08:00
committed by Jonathan Hui
parent 1ec9fd525d
commit 7cd85bfe5d
7 changed files with 913 additions and 2 deletions
+6
View File
@@ -170,6 +170,9 @@ EXTRA_DIST = \
test_route_table.py \
test_router_reattach.py \
test_service.py \
test_srp_lease.py \
test_srp_name_conflicts.py \
test_srp_register_single_service.py \
thread_cert.py \
tlvs_parsing.py \
thread_cert.py \
@@ -218,6 +221,9 @@ check_SCRIPTS = \
test_route_table.py \
test_router_reattach.py \
test_service.py \
test_srp_lease.py \
test_srp_name_conflicts.py \
test_srp_register_single_service.py \
Cert_5_1_01_RouterAttach.py \
Cert_5_1_02_ChildAddressTimeout.py \
Cert_5_1_03_RouterAddressReallocation.py \
+223 -2
View File
@@ -688,6 +688,215 @@ class NodeImpl:
self.send_command(cmd)
self._expect_done()
def srp_server_set_enabled(self, enable):
cmd = f'srp server {"enable" if enable else "disable"}'
self.send_command(cmd)
self._expect_done()
def srp_server_set_lease_range(self, min_lease, max_lease, min_key_lease, max_key_lease):
self.send_command(f'srp server lease {min_lease} {max_lease} {min_key_lease} {max_key_lease}')
self._expect_done()
def srp_server_get_hosts(self):
"""Returns the host list on the SRP server as a list of property
dictionary.
Example output:
[{
'fullname': 'my-host.default.service.arpa.',
'name': 'my-host',
'deleted': 'false',
'addresses': ['2001::1', '2001::2']
}]
"""
cmd = 'srp server host'
self.send_command(cmd)
lines = self._expect_command_output(cmd)
host_list = []
while lines:
host = {}
host['fullname'] = lines.pop(0).strip()
host['name'] = host['fullname'].split('.')[0]
host['deleted'] = lines.pop(0).strip().split(':')[1].strip()
if host['deleted'] == 'true':
host_list.append(host)
continue
addresses = lines.pop(0).strip().split('[')[1].strip(' ]').split(',')
map(str.strip, addresses)
host['addresses'] = [addr for addr in addresses if addr]
host_list.append(host)
return host_list
def srp_server_get_host(self, host_name):
"""Returns host on the SRP server that matches given host name.
Example usage:
self.srp_server_get_host("my-host")
"""
for host in self.srp_server_get_hosts():
if host_name == host['name']:
return host
def srp_server_get_services(self):
"""Returns the service list on the SRP server as a list of property
dictionary.
Example output:
[{
'fullname': 'my-service._ipps._tcp.default.service.arpa.',
'instance': 'my-service',
'name': '_ipps._tcp',
'deleted': 'false',
'port': '12345',
'priority': '0',
'weight': '0',
'TXT': '00',
'host_fullname': 'my-host.default.service.arpa.',
'host': 'my-host',
'addresses': ['2001::1', '2001::2']
}]
Note that the TXT data is output as a HEX string.
"""
cmd = 'srp server service'
self.send_command(cmd)
lines = self._expect_command_output(cmd)
service_list = []
while lines:
service = {}
service['fullname'] = lines.pop(0).strip()
name_labels = service['fullname'].split('.')
service['instance'] = name_labels[0]
service['name'] = '.'.join(name_labels[1:3])
service['deleted'] = lines.pop(0).strip().split(':')[1].strip()
if service['deleted'] == 'true':
service_list.append(service)
continue
# 'port', 'priority', 'weight', 'TXT'
for i in range(0, 4):
key_value = lines.pop(0).strip().split(':')
service[key_value[0].strip()] = key_value[1].strip()
service['host_fullname'] = lines.pop(0).strip().split(':')[1].strip()
service['host'] = service['host_fullname'].split('.')[0]
addresses = lines.pop(0).strip().split('[')[1].strip(' ]').split(',')
map(str.strip, addresses)
service['addresses'] = [addr for addr in addresses if addr]
service_list.append(service)
return service_list
def srp_server_get_service(self, instance_name, service_name):
"""Returns service on the SRP server that matches given instance
name and service name.
Example usage:
self.srp_server_get_service("my-service", "_ipps._tcp")
"""
for service in self.srp_server_get_services():
if (instance_name == service['instance'] and service_name == service['name']):
return service
def get_srp_server_port(self):
"""Returns the dynamic SRP server UDP port by parsing
the SRP Server Data in Network Data.
"""
for service in self.get_services():
# TODO: for now, we are using 0xfd as the SRP service data.
# May use a dedicated bit flag for SRP server.
if int(service[1], 16) == 0x5d:
# The SRP server data are 2-bytes UDP port number.
return int(service[2], 16)
def srp_client_start(self, server_address, server_port):
self.send_command(f'srp client start {server_address} {server_port}')
self._expect_done()
def srp_client_stop(self):
self.send_command(f'srp client stop')
self._expect_done()
def srp_client_get_host_state(self):
cmd = 'srp client host state'
self.send_command(cmd)
return self._expect_command_output(cmd)[0]
def srp_client_set_host_name(self, name):
self.send_command(f'srp client host name {name}')
self._expect_done()
def srp_client_get_host_name(self):
self.send_command(f'srp client host name')
self._expect_done()
def srp_client_remove_host(self, remove_key=False):
self.send_command(f'srp client host remove {"1" if remove_key else "0"}')
self._expect_done()
def srp_client_clear_host(self):
self.send_command(f'srp client host clear')
self._expect_done()
def srp_client_set_host_address(self, address):
self.send_command(f'srp client host address {address}')
self._expect_done()
def srp_client_get_host_address(self):
self.send_command(f'srp client host address')
self._expect_done()
def srp_client_add_service(self, instance_name, service_name, port):
self.send_command(f'srp client service add {instance_name} {service_name} {port}')
self._expect_done()
def srp_client_remove_service(self, instance_name, service_name):
self.send_command(f'srp client service remove {instance_name} {service_name}')
self._expect_done()
def srp_client_get_services(self):
cmd = 'srp client service'
self.send_command(cmd)
service_lines = self._expect_command_output(cmd)
return [self._parse_srp_client_service(line) for line in service_lines]
def _parse_srp_client_service(self, line: str):
"""Parse one line of srp service list into a dictionary which
maps string keys to string values.
Example output for input
'instance:\"%s\", name:\"%s\", state:%s, port:%d, priority:%d, weight:%d"'
{
'instance': 'my-service',
'name': '_ipps._udp',
'state': 'ToAdd',
'port': '12345',
'priority': '0',
'weight': '0'
}
Note that value of 'port', 'priority' and 'weight' are represented
as strings but not integers.
"""
key_values = [word.strip().split(':') for word in line.split(',')]
keys = [key_value[0] for key_value in key_values]
values = [key_value[1].strip('"') for key_value in key_values]
return dict(zip(keys, values))
def enable_backbone_router(self):
cmd = 'bbr enable'
self.send_command(cmd)
@@ -1286,11 +1495,11 @@ class NodeImpl:
def enable_br(self):
self.send_command('br enable')
self._expect('Done')
self._expect_done()
def disable_br(self):
self.send_command('br disable')
self._expect('Done')
self._expect_done()
def get_prefixes(self):
return self.get_netdata()['Prefixes']
@@ -1298,6 +1507,18 @@ class NodeImpl:
def get_routes(self):
return self.get_netdata()['Routes']
def get_services(self):
netdata = self.netdata_show()
services = []
services_section = False
for line in netdata:
if line.startswith('Services:'):
services_section = True
elif services_section:
services.append(line.strip().split(' '))
return services
def netdata_show(self):
self.send_command('netdata show')
return self._expect_command_output('netdata show')
+191
View File
@@ -0,0 +1,191 @@
#!/usr/bin/env python3
#
# Copyright (c) 2021, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import ipaddress
import unittest
import command
import thread_cert
# Test description:
# This test verifies the SRP server and client properly handle SRP host
# and service instance lease.
#
# Topology:
# LEADER (SRP server)
# |
# |
# ROUTER (SRP client)
#
SERVER = 1
CLIENT = 2
LEASE = 10 # Seconds
KEY_LEASE = 20 # Seconds
class SrpRegisterSingleService(thread_cert.TestCase):
USE_MESSAGE_FACTORY = False
SUPPORT_NCP = False
TOPOLOGY = {
SERVER: {
'name': 'SRP_SERVER',
'masterkey': '00112233445566778899aabbccddeeff',
'mode': 'rdn',
'panid': 0xface
},
CLIENT: {
'name': 'SRP_CLIENT',
'masterkey': '00112233445566778899aabbccddeeff',
'mode': 'rdn',
'panid': 0xface,
'router_selection_jitter': 1
},
}
def test(self):
server = self.nodes[SERVER]
client = self.nodes[CLIENT]
#
# 0. Start the server and client devices.
#
server.srp_server_set_enabled(True)
server.srp_server_set_lease_range(LEASE, LEASE, KEY_LEASE, KEY_LEASE)
server.start()
self.simulator.go(5)
self.assertEqual(server.get_state(), 'leader')
self.simulator.go(5)
client.srp_server_set_enabled(False)
client.start()
self.simulator.go(5)
self.assertEqual(client.get_state(), 'router')
#
# 1. Register a single service and verify that it works.
#
client.srp_client_set_host_name('my-host')
client.srp_client_set_host_address('2001::1')
client.srp_client_start(server.get_addrs()[0], client.get_srp_server_port())
client.srp_client_add_service('my-service', '_ipps._tcp', 12345)
self.simulator.go(2)
self.check_host_and_service(server, client)
#
# 2. Stop the client and wait for the service instance LEASE to expire.
#
client.srp_client_stop()
self.simulator.go(LEASE + 1)
# The SRP server should remove the host and service but retain their names
# since the the KEY LEASE hasn't expired yet.
self.assertEqual(server.srp_server_get_host('my-host')['deleted'], 'true')
self.assertEqual(server.srp_server_get_service('my-service', '_ipps._tcp')['deleted'], 'true')
# Start the client again, the same service should be successfully registered.
client.srp_client_start(server.get_addrs()[0], client.get_srp_server_port())
self.simulator.go(2)
self.check_host_and_service(server, client)
#
# 3. Stop the client and wait for the KEY LEASE to expire.
# The host and service instance should be fully removed by the SRP server.
#
client.srp_client_stop()
self.simulator.go(KEY_LEASE + 1)
# The host and service are expected to be fully removed.
self.assertEqual(len(server.srp_server_get_hosts()), 0)
self.assertEqual(len(server.srp_server_get_services()), 0)
# Start the client again, the same service should be successfully registered.
client.srp_client_start(server.get_addrs()[0], client.get_srp_server_port())
self.simulator.go(2)
self.check_host_and_service(server, client)
def check_host_and_service(self, server, client):
"""Check that we have properly registered host and service instance.
"""
client_services = client.srp_client_get_services()
print(client_services)
self.assertEqual(len(client_services), 1)
client_service = client_services[0]
# Verify that the client possesses correct service resources.
self.assertEqual(client_service['instance'], 'my-service')
self.assertEqual(client_service['name'], '_ipps._tcp')
self.assertEqual(int(client_service['port']), 12345)
self.assertEqual(int(client_service['priority']), 0)
self.assertEqual(int(client_service['weight']), 0)
# Verify that the client received a SUCCESS response for the server.
self.assertEqual(client_service['state'], 'Registered')
# Wait for a KEY LEASE period to make sure that the client has refreshed
# the host and service instance.
self.simulator.go(KEY_LEASE + 1)
server_services = server.srp_server_get_services()
print(server_services)
self.assertEqual(len(server_services), 1)
server_service = server_services[0]
# Verify that the server accepted the SRP registration and stored
# the same service resources.
self.assertEqual(server_service['deleted'], 'false')
self.assertEqual(server_service['instance'], client_service['instance'])
self.assertEqual(server_service['name'], client_service['name'])
self.assertEqual(int(server_service['port']), int(client_service['port']))
self.assertEqual(int(server_service['priority']), int(client_service['priority']))
self.assertEqual(int(server_service['weight']), int(client_service['weight']))
self.assertEqual(server_service['host'], 'my-host')
server_hosts = server.srp_server_get_hosts()
print(server_hosts)
self.assertEqual(len(server_hosts), 1)
server_host = server_hosts[0]
self.assertEqual(server_host['deleted'], 'false')
self.assertEqual(server_host['fullname'], server_service['host_fullname'])
self.assertEqual(len(server_host['addresses']), 1)
self.assertEqual(ipaddress.ip_address(server_host['addresses'][0]), ipaddress.ip_address('2001::1'))
if __name__ == '__main__':
unittest.main()
+279
View File
@@ -0,0 +1,279 @@
#!/usr/bin/env python3
#
# Copyright (c) 2021, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import ipaddress
import unittest
import command
import thread_cert
# Test description:
# This test verifies if the SRP server can handle name conflicts correctly.
#
# Topology:
# LEADER (SRP server)
# / \
# / \
# / \
# ROUTER1 ROUTER2
#
SERVER = 1
CLIENT1 = 2
CLIENT2 = 3
class SrpNameConflicts(thread_cert.TestCase):
USE_MESSAGE_FACTORY = False
SUPPORT_NCP = False
TOPOLOGY = {
SERVER: {
'name': 'SRP_SERVER',
'masterkey': '00112233445566778899aabbccddeeff',
'mode': 'rdn',
'panid': 0xface
},
CLIENT1: {
'name': 'SRP_CLIENT1',
'masterkey': '00112233445566778899aabbccddeeff',
'mode': 'rdn',
'panid': 0xface,
'router_selection_jitter': 1
},
CLIENT2: {
'name': 'SRP_CLIENT2',
'masterkey': '00112233445566778899aabbccddeeff',
'mode': 'rdn',
'panid': 0xface,
'router_selection_jitter': 1
},
}
def test(self):
server = self.nodes[SERVER]
client_1 = self.nodes[CLIENT1]
client_2 = self.nodes[CLIENT2]
#
# 0. Start the server & client devices.
#
server.srp_server_set_enabled(True)
server.start()
self.simulator.go(5)
self.assertEqual(server.get_state(), 'leader')
self.simulator.go(5)
client_1.srp_server_set_enabled(False)
client_1.start()
self.simulator.go(5)
self.assertEqual(client_1.get_state(), 'router')
client_2.srp_server_set_enabled(False)
client_2.start()
self.simulator.go(5)
self.assertEqual(client_2.get_state(), 'router')
#
# 1. Register a single service and verify that it works.
#
client_1.srp_client_set_host_name('my-host-1')
client_1.srp_client_set_host_address('2001::1')
client_1.srp_client_start(server.get_addrs()[0], client_1.get_srp_server_port())
client_1.srp_client_add_service('my-service-1', '_ipps._tcp', 12345)
self.simulator.go(2)
# Verify that the client possesses correct service resources.
client_1_service = client_1.srp_client_get_services()[0]
self.assertEqual(client_1_service['instance'], 'my-service-1')
self.assertEqual(client_1_service['name'], '_ipps._tcp')
self.assertEqual(int(client_1_service['port']), 12345)
self.assertEqual(int(client_1_service['priority']), 0)
self.assertEqual(int(client_1_service['weight']), 0)
# Verify that the client receives a SUCCESS response for the server.
self.assertEqual(client_1_service['state'], 'Registered')
# Verify that the server accepts the SRP registration and stored
# the same service resources.
server_service = server.srp_server_get_services()[0]
self.assertEqual(server_service['deleted'], 'false')
self.assertEqual(server_service['instance'], client_1_service['instance'])
self.assertEqual(server_service['name'], client_1_service['name'])
self.assertEqual(int(server_service['port']), int(client_1_service['port']))
self.assertEqual(int(server_service['priority']), int(client_1_service['priority']))
self.assertEqual(int(server_service['weight']), int(client_1_service['weight']))
self.assertEqual(server_service['host'], 'my-host-1')
server_host = server.srp_server_get_hosts()[0]
self.assertEqual(server_host['deleted'], 'false')
self.assertEqual(server_host['fullname'], server_service['host_fullname'])
self.assertEqual(len(server_host['addresses']), 1)
self.assertEqual(ipaddress.ip_address(server_host['addresses'][0]), ipaddress.ip_address('2001::1'))
#
# 2. Register with the same host name from the second client and it should fail.
#
client_2.srp_client_set_host_name('my-host-1')
client_2.srp_client_set_host_address('2001::2')
client_2.srp_client_start(server.get_addrs()[0], client_2.get_srp_server_port())
client_2.srp_client_add_service('my-service-2', '_ipps._tcp', 12345)
self.simulator.go(2)
# It is expected that the registration will be rejected.
client_2_service = client_2.srp_client_get_services()[0]
self.assertEqual(client_2_service['state'], 'Adding')
self.assertEqual(client_2.srp_client_get_host_state(), 'ToAdd')
self.assertEqual(len(server.srp_server_get_services()), 1)
self.assertEqual(len(server.srp_server_get_hosts()), 1)
client_2.srp_client_clear_host()
client_2.srp_client_stop()
#
# 3. Register with the same service name from the second client and it should fail.
#
client_2.srp_client_set_host_name('my-host-2')
client_2.srp_client_set_host_address('2001::2')
client_2.srp_client_start(server.get_addrs()[0], client_2.get_srp_server_port())
client_2.srp_client_add_service('my-service-1', '_ipps._tcp', 12345)
self.simulator.go(2)
# It is expected that the registration will be rejected.
client_2_service = client_2.srp_client_get_services()[0]
self.assertEqual(client_2_service['state'], 'Adding')
self.assertEqual(client_2.srp_client_get_host_state(), 'ToAdd')
self.assertEqual(len(server.srp_server_get_services()), 1)
self.assertEqual(len(server.srp_server_get_hosts()), 1)
client_2.srp_client_clear_host()
client_2.srp_client_stop()
#
# 4. Register with different host & service instance name, it should succeed.
#
client_2.srp_client_set_host_name('my-host-2')
client_2.srp_client_set_host_address('2001::2')
client_2.srp_client_start(server.get_addrs()[0], client_2.get_srp_server_port())
client_2.srp_client_add_service('my-service-2', '_ipps._tcp', 12345)
self.simulator.go(2)
# It is expected that the registration will be accepted.
client_2_service = client_2.srp_client_get_services()[0]
self.assertEqual(client_2_service['state'], 'Registered')
self.assertEqual(client_2.srp_client_get_host_state(), 'Registered')
self.assertEqual(len(server.srp_server_get_services()), 2)
self.assertEqual(len(server.srp_server_get_hosts()), 2)
self.assertEqual(server.srp_server_get_host('my-host-2')['deleted'], 'false')
self.assertEqual(server.srp_server_get_service('my-service-2', '_ipps._tcp')['deleted'], 'false')
# Remove the host and all services registered on the SRP server.
client_2.srp_client_remove_host(remove_key=True)
self.simulator.go(2)
client_2.srp_client_clear_host()
client_2.srp_client_stop()
#
# 5. Register with the same service instance name before its KEY LEASE expires,
# it is expected to fail.
#
# Remove the service instance from SRP server but retains its name.
client_1.srp_client_remove_service('my-service-1', '_ipps._tcp')
self.simulator.go(2)
client_2.srp_client_set_host_name('my-host-2')
client_2.srp_client_set_host_address('2001::2')
client_2.srp_client_start(server.get_addrs()[0], client_2.get_srp_server_port())
client_2.srp_client_add_service('my-service-1', '_ipps._tcp', 12345)
self.simulator.go(2)
# It is expected that the registration will be rejected.
client_2_service = client_2.srp_client_get_services()[0]
self.assertEqual(client_2_service['state'], 'Adding')
self.assertEqual(client_2.srp_client_get_host_state(), 'ToAdd')
# The service 'my-service-1' is removed but its name is retained.
# This is why we can see the service record on the SRP server.
self.assertEqual(len(server.srp_server_get_services()), 1)
self.assertEqual(len(server.srp_server_get_hosts()), 1)
self.assertEqual(server.srp_server_get_host('my-host-1')['deleted'], 'false')
self.assertEqual(server.srp_server_get_service('my-service-1', '_ipps._tcp')['deleted'], 'true')
client_2.srp_client_clear_host()
client_2.srp_client_stop()
#
# 6. The service instance name can be re-used by another client when
# the service has been permanently removed (the KEY resource is
# removed) from the host.
#
# Client 1 adds back the service, it should success.
client_1.srp_client_add_service('my-service-1', '_ipps._tcp', 12345)
self.simulator.go(2)
self.assertEqual(len(server.srp_server_get_services()), 1)
self.assertEqual(len(server.srp_server_get_hosts()), 1)
self.assertEqual(server.srp_server_get_host('my-host-1')['deleted'], 'false')
self.assertEqual(server.srp_server_get_service('my-service-1', '_ipps._tcp')['deleted'], 'false')
# Permanently removes the service instance.
client_1.srp_client_remove_host(remove_key=True)
self.simulator.go(2)
self.assertEqual(len(server.srp_server_get_services()), 0)
self.assertEqual(len(server.srp_server_get_hosts()), 0)
# Client 2 registers the same host & service instance name with Client 1.
client_2.srp_client_stop()
client_2.srp_client_clear_host()
client_2.srp_client_set_host_name('my-host-1')
client_2.srp_client_set_host_address('2001::2')
client_2.srp_client_start(server.get_addrs()[0], client_2.get_srp_server_port())
client_2.srp_client_add_service('my-service-1', '_ipps._tcp', 12345)
self.simulator.go(2)
# It is expected that client 2 will success because those names has been
# released by client 1.
self.assertEqual(len(server.srp_server_get_services()), 1)
self.assertEqual(len(server.srp_server_get_hosts()), 1)
self.assertEqual(server.srp_server_get_host('my-host-1')['deleted'], 'false')
self.assertEqual(server.srp_server_get_service('my-service-1', '_ipps._tcp')['deleted'], 'false')
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,210 @@
#!/usr/bin/env python3
#
# Copyright (c) 2021, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import ipaddress
import unittest
import command
import thread_cert
# Test description:
# This test verifies basic SRP function that a service can be registered to
# and unregistered from the SRP server.
#
# Topology:
# LEADER (SRP server)
# |
# |
# ROUTER (SRP client)
#
SERVER = 1
CLIENT = 2
class SrpRegisterSingleService(thread_cert.TestCase):
USE_MESSAGE_FACTORY = False
SUPPORT_NCP = False
TOPOLOGY = {
SERVER: {
'name': 'SRP_SERVER',
'masterkey': '00112233445566778899aabbccddeeff',
'mode': 'rdn',
'panid': 0xface
},
CLIENT: {
'name': 'SRP_CLIENT',
'masterkey': '00112233445566778899aabbccddeeff',
'mode': 'rdn',
'panid': 0xface,
'router_selection_jitter': 1
},
}
def test(self):
server = self.nodes[SERVER]
client = self.nodes[CLIENT]
#
# 0. Start the server & client devices.
#
server.srp_server_set_enabled(True)
server.start()
self.simulator.go(5)
self.assertEqual(server.get_state(), 'leader')
self.simulator.go(5)
client.srp_server_set_enabled(False)
client.start()
self.simulator.go(5)
self.assertEqual(client.get_state(), 'router')
#
# 1. Register a single service and verify that it works.
#
client.srp_client_set_host_name('my-host')
client.srp_client_set_host_address('2001::1')
client.srp_client_start(server.get_addrs()[0], client.get_srp_server_port())
client.srp_client_add_service('my-service', '_ipps._tcp', 12345)
self.simulator.go(2)
self.check_host_and_service(server, client)
#
# 2. Unregister a service but retain the name. The service name should be
# retained on the server.
#
client.srp_client_remove_service('my-service', '_ipps._tcp')
self.simulator.go(2)
client_services = client.srp_client_get_services()
print(client_services)
self.assertEqual(len(client_services), 0)
server_services = server.srp_server_get_services()
print(server_services)
self.assertEqual(len(server_services), 1)
server_service = server_services[0]
# Verify that the service has been successfully removed on the server side.
self.assertEqual(server_service['deleted'], 'true')
server_hosts = server.srp_server_get_hosts()
print(server_hosts)
self.assertEqual(len(server_hosts), 1)
server_host = server_hosts[0]
# The registered host should not be touched.
self.assertEqual(server_host['deleted'], 'false')
self.assertEqual(server_host['name'], 'my-host')
self.assertEqual(len(server_host['addresses']), 1)
self.assertEqual(ipaddress.ip_address(server_host['addresses'][0]), ipaddress.ip_address('2001::1'))
#
# 3. Register the same service again. It should succeed and the name should be
# reused.
#
client.srp_client_add_service('my-service', '_ipps._tcp', 12345)
self.simulator.go(2)
self.check_host_and_service(server, client)
#
# 4. Fully remove the host and all its service instances.
#
client.srp_client_remove_host(remove_key=True)
self.simulator.go(2)
client_services = client.srp_client_get_services()
print(client_services)
self.assertEqual(len(client_services), 0)
print(client.srp_client_get_host_state())
server_services = server.srp_server_get_services()
print(server_services)
self.assertEqual(len(server_services), 0)
server_hosts = server.srp_server_get_hosts()
print(server_hosts)
self.assertEqual(len(server_hosts), 0)
def check_host_and_service(self, server, client):
"""Check that we have properly registered host and service instance.
"""
client_services = client.srp_client_get_services()
print(client_services)
self.assertEqual(len(client_services), 1)
client_service = client_services[0]
# Verify that the client possesses correct service resources.
self.assertEqual(client_service['instance'], 'my-service')
self.assertEqual(client_service['name'], '_ipps._tcp')
self.assertEqual(int(client_service['port']), 12345)
self.assertEqual(int(client_service['priority']), 0)
self.assertEqual(int(client_service['weight']), 0)
# Verify that the client received a SUCCESS response for the server.
self.assertEqual(client_service['state'], 'Registered')
server_services = server.srp_server_get_services()
print(server_services)
self.assertEqual(len(server_services), 1)
server_service = server_services[0]
# Verify that the server accepted the SRP registration and stores
# the same service resources.
self.assertEqual(server_service['deleted'], 'false')
self.assertEqual(server_service['instance'], client_service['instance'])
self.assertEqual(server_service['name'], client_service['name'])
self.assertEqual(int(server_service['port']), int(client_service['port']))
self.assertEqual(int(server_service['priority']), int(client_service['priority']))
self.assertEqual(int(server_service['weight']), int(client_service['weight']))
self.assertEqual(server_service['host'], 'my-host')
server_hosts = server.srp_server_get_hosts()
print(server_hosts)
self.assertEqual(len(server_hosts), 1)
server_host = server_hosts[0]
self.assertEqual(server_host['deleted'], 'false')
self.assertEqual(server_host['fullname'], server_service['host_fullname'])
self.assertEqual(len(server_host['addresses']), 1)
self.assertEqual(ipaddress.ip_address(server_host['addresses'][0]), ipaddress.ip_address('2001::1'))
if __name__ == '__main__':
unittest.main()