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 29from cli import verify 30from cli import verify_within 31import cli 32 33# ----------------------------------------------------------------------------------------------------------------------- 34# Test description: joining (as router, end-device, sleepy) - two node network 35 36test_name = __file__[:-3] if __file__.endswith('.py') else __file__ 37print('-' * 120) 38print('Starting \'{}\''.format(test_name)) 39 40# ----------------------------------------------------------------------------------------------------------------------- 41# Creating `cli.Nodes` instances 42 43speedup = 4 44cli.Node.set_time_speedup_factor(speedup) 45 46server = cli.Node() 47client = cli.Node() 48 49WAIT_TIME = 5 50 51# ----------------------------------------------------------------------------------------------------------------------- 52# Test implementation 53 54server.form('srp-test') 55verify(server.get_state() == 'leader') 56 57client.join(server) 58verify(client.get_state() == 'router') 59 60# Check initial state of SRP client and server 61verify(server.srp_server_get_state() == 'disabled') 62verify(server.srp_server_get_addr_mode() == 'unicast') 63verify(client.srp_client_get_state() == 'Disabled') 64verify(client.srp_client_get_auto_start_mode() == 'Enabled') 65 66# Start server and client and register single service 67server.srp_server_enable() 68 69client.srp_client_set_host_name('host') 70client.srp_client_set_host_address('fd00::cafe') 71client.srp_client_add_service('ins', '_test._udp', 777, 2, 1) 72 73 74def check_server_has_service(): 75 verify(len(server.srp_server_get_hosts()) > 0) 76 77 78verify_within(check_server_has_service, WAIT_TIME) 79 80# Check state and service/host info on client and server 81 82verify(client.srp_client_get_auto_start_mode() == 'Enabled') 83verify(client.srp_client_get_state() == 'Enabled') 84verify(client.srp_client_get_server_address() == server.get_mleid_ip_addr()) 85 86verify(client.srp_client_get_host_state() == 'Registered') 87verify(client.srp_client_get_host_name() == 'host') 88addresses = client.srp_client_get_host_address() 89verify(len(addresses) == 1) 90verify(addresses[0] == 'fd00:0:0:0:0:0:0:cafe') 91 92services = client.srp_client_get_services() 93verify(len(services) == 1) 94service = services[0] 95verify(service['instance'] == 'ins') 96verify(service['name'] == '_test._udp') 97verify(service['state'] == 'Registered') 98verify(service['port'] == '777') 99verify(service['priority'] == '2') 100verify(service['weight'] == '1') 101 102verify(server.srp_server_get_state() == 'running') 103 104hosts = server.srp_server_get_hosts() 105verify(len(hosts) == 1) 106host = hosts[0] 107verify(host['name'] == 'host') 108verify(host['deleted'] == 'false') 109verify(host['addresses'] == ['fd00:0:0:0:0:0:0:cafe']) 110 111services = server.srp_server_get_services() 112verify(len(services) == 1) 113service = services[0] 114verify(service['instance'] == 'ins') 115verify(service['name'] == '_test._udp') 116verify(service['deleted'] == 'false') 117verify(service['subtypes'] == '(null)') 118verify(service['port'] == '777') 119verify(service['priority'] == '2') 120verify(service['weight'] == '1') 121verify(service['host'] == 'host') 122verify(service['addresses'] == ['fd00:0:0:0:0:0:0:cafe']) 123 124# Check the client address is added in EID cache table (snoop). 125 126cache_table = server.get_eidcache() 127client_rloc = int(client.get_rloc16(), 16) 128found_entry = False 129 130for entry in cache_table: 131 fields = entry.strip().split(' ') 132 if (fields[0] == 'fd00:0:0:0:0:0:0:cafe'): 133 verify(int(fields[1], 16) == client_rloc) 134 verify(fields[2] == 'snoop') 135 found_entry = True 136 break 137 138verify(found_entry) 139 140# ----------------------------------------------------------------------------------------------------------------------- 141# Test finished 142 143cli.Node.finalize_all_nodes() 144 145print('\'{}\' passed.'.format(test_name)) 146