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')
64
65# Start server and client and register single service
66server.srp_server_enable()
67
68client.srp_client_enable_auto_start_mode()
69verify(client.srp_client_get_auto_start_mode() == 'Enabled')
70client.srp_client_set_host_name('host')
71client.srp_client_set_host_address('fd00::cafe')
72client.srp_client_add_service('ins', '_test._udp', 777, 2, 1)
73
74
75def check_server_has_service():
76    verify(len(server.srp_server_get_hosts()) > 0)
77
78
79verify_within(check_server_has_service, WAIT_TIME)
80
81# Check state and service/host info on client and server
82
83verify(client.srp_client_get_auto_start_mode() == 'Enabled')
84verify(client.srp_client_get_state() == 'Enabled')
85verify(client.srp_client_get_server_address() == server.get_mleid_ip_addr())
86
87verify(client.srp_client_get_host_state() == 'Registered')
88verify(client.srp_client_get_host_name() == 'host')
89addresses = client.srp_client_get_host_address()
90verify(len(addresses) == 1)
91verify(addresses[0] == 'fd00:0:0:0:0:0:0:cafe')
92
93services = client.srp_client_get_services()
94verify(len(services) == 1)
95service = services[0]
96verify(service['instance'] == 'ins')
97verify(service['name'] == '_test._udp')
98verify(service['state'] == 'Registered')
99verify(service['port'] == '777')
100verify(service['priority'] == '2')
101verify(service['weight'] == '1')
102
103verify(server.srp_server_get_state() == 'running')
104
105hosts = server.srp_server_get_hosts()
106verify(len(hosts) == 1)
107host = hosts[0]
108verify(host['name'] == 'host')
109verify(host['deleted'] == 'false')
110verify(host['addresses'] == ['fd00:0:0:0:0:0:0:cafe'])
111
112services = server.srp_server_get_services()
113verify(len(services) == 1)
114service = services[0]
115verify(service['instance'] == 'ins')
116verify(service['name'] == '_test._udp')
117verify(service['deleted'] == 'false')
118verify(service['subtypes'] == '(null)')
119verify(service['port'] == '777')
120verify(service['priority'] == '2')
121verify(service['weight'] == '1')
122verify(service['host'] == 'host')
123verify(service['addresses'] == ['fd00:0:0:0:0:0:0:cafe'])
124
125# Check the client address is added in EID cache table (snoop).
126
127cache_table = server.get_eidcache()
128client_rloc = int(client.get_rloc16(), 16)
129found_entry = False
130
131for entry in cache_table:
132    fields = entry.strip().split(' ')
133    if (fields[0] == 'fd00:0:0:0:0:0:0:cafe'):
134        verify(int(fields[1], 16) == client_rloc)
135        verify(fields[2] == 'snoop')
136        found_entry = True
137        break
138
139verify(found_entry)
140
141# -----------------------------------------------------------------------------------------------------------------------
142# Test finished
143
144cli.Node.finalize_all_nodes()
145
146print('\'{}\' passed.'.format(test_name))
147