xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-401-srp-server-address-cache-snoop.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2024, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Workerfrom cli import verify
30*cfb92d14SAndroid Build Coastguard Workerfrom cli import verify_within
31*cfb92d14SAndroid Build Coastguard Workerimport cli
32*cfb92d14SAndroid Build Coastguard Workerimport time
33*cfb92d14SAndroid Build Coastguard Worker
34*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
35*cfb92d14SAndroid Build Coastguard Worker# Test description:
36*cfb92d14SAndroid Build Coastguard Worker#
37*cfb92d14SAndroid Build Coastguard Worker# Validate registered host addresses are properly added in address cache table
38*cfb92d14SAndroid Build Coastguard Worker# on SRP server.
39*cfb92d14SAndroid Build Coastguard Worker#
40*cfb92d14SAndroid Build Coastguard Worker#    r1 (leader) ----- r2 ------ r3
41*cfb92d14SAndroid Build Coastguard Worker#     /  |                        \
42*cfb92d14SAndroid Build Coastguard Worker#    /   |                         \
43*cfb92d14SAndroid Build Coastguard Worker#   fed1  sed1                     sed2
44*cfb92d14SAndroid Build Coastguard Worker#
45*cfb92d14SAndroid Build Coastguard Worker
46*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
47*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
48*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Worker#-----------------------------------------------------------------------------------------------------------------------
51*cfb92d14SAndroid Build Coastguard Worker# Creating `cli.Nodes` instances
52*cfb92d14SAndroid Build Coastguard Worker
53*cfb92d14SAndroid Build Coastguard Workerspeedup = 10
54*cfb92d14SAndroid Build Coastguard Workercli.Node.set_time_speedup_factor(speedup)
55*cfb92d14SAndroid Build Coastguard Worker
56*cfb92d14SAndroid Build Coastguard Workerr1 = cli.Node()
57*cfb92d14SAndroid Build Coastguard Workerr2 = cli.Node()
58*cfb92d14SAndroid Build Coastguard Workerr3 = cli.Node()
59*cfb92d14SAndroid Build Coastguard Workerfed1 = cli.Node()
60*cfb92d14SAndroid Build Coastguard Workersed1 = cli.Node()
61*cfb92d14SAndroid Build Coastguard Workersed2 = cli.Node()
62*cfb92d14SAndroid Build Coastguard Worker
63*cfb92d14SAndroid Build Coastguard WorkerWAIT_TIME = 5
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard Worker#-----------------------------------------------------------------------------------------------------------------------
66*cfb92d14SAndroid Build Coastguard Worker# Form topology
67*cfb92d14SAndroid Build Coastguard Worker
68*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2)
69*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(fed1)
70*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(sed1)
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1)
73*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r3)
74*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(sed2)
75*cfb92d14SAndroid Build Coastguard Worker
76*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r2)
77*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(sed2)
78*cfb92d14SAndroid Build Coastguard Worker
79*cfb92d14SAndroid Build Coastguard Workerfed1.allowlist_node(r1)
80*cfb92d14SAndroid Build Coastguard Workersed1.allowlist_node(r1)
81*cfb92d14SAndroid Build Coastguard Worker
82*cfb92d14SAndroid Build Coastguard Workersed2.allowlist_node(r3)
83*cfb92d14SAndroid Build Coastguard Worker
84*cfb92d14SAndroid Build Coastguard Workerr1.form('srp-snoop')
85*cfb92d14SAndroid Build Coastguard Workerr2.join(r1)
86*cfb92d14SAndroid Build Coastguard Workerr3.join(r2)
87*cfb92d14SAndroid Build Coastguard Workerfed1.join(r1, cli.JOIN_TYPE_REED)
88*cfb92d14SAndroid Build Coastguard Workersed1.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
89*cfb92d14SAndroid Build Coastguard Workersed2.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
90*cfb92d14SAndroid Build Coastguard Workersed1.set_pollperiod(500)
91*cfb92d14SAndroid Build Coastguard Workersed2.set_pollperiod(500)
92*cfb92d14SAndroid Build Coastguard Worker
93*cfb92d14SAndroid Build Coastguard Workerverify(r1.get_state() == 'leader')
94*cfb92d14SAndroid Build Coastguard Workerverify(r2.get_state() == 'router')
95*cfb92d14SAndroid Build Coastguard Workerverify(r2.get_state() == 'router')
96*cfb92d14SAndroid Build Coastguard Workerverify(sed1.get_state() == 'child')
97*cfb92d14SAndroid Build Coastguard Workerverify(sed2.get_state() == 'child')
98*cfb92d14SAndroid Build Coastguard Workerverify(fed1.get_state() == 'child')
99*cfb92d14SAndroid Build Coastguard Worker
100*cfb92d14SAndroid Build Coastguard Workerr2_rloc = int(r2.get_rloc16(), 16)
101*cfb92d14SAndroid Build Coastguard Workerr3_rloc = int(r3.get_rloc16(), 16)
102*cfb92d14SAndroid Build Coastguard Workerfed1_rloc = int(fed1.get_rloc16(), 16)
103*cfb92d14SAndroid Build Coastguard Worker
104*cfb92d14SAndroid Build Coastguard Worker# Start server and client and register single service
105*cfb92d14SAndroid Build Coastguard Workerr1.srp_server_enable()
106*cfb92d14SAndroid Build Coastguard Worker
107*cfb92d14SAndroid Build Coastguard Workerr2.srp_client_enable_auto_start_mode()
108*cfb92d14SAndroid Build Coastguard Workerr2.srp_client_set_host_name('r2')
109*cfb92d14SAndroid Build Coastguard Workerr2.srp_client_set_host_address('fd00::2')
110*cfb92d14SAndroid Build Coastguard Workerr2.srp_client_add_service('srv2', '_test._udp', 222, 0, 0)
111*cfb92d14SAndroid Build Coastguard Worker
112*cfb92d14SAndroid Build Coastguard Worker
113*cfb92d14SAndroid Build Coastguard Workerdef check_server_has_host(num_hosts):
114*cfb92d14SAndroid Build Coastguard Worker    verify(len(r1.srp_server_get_hosts()) >= num_hosts)
115*cfb92d14SAndroid Build Coastguard Worker
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Workerverify_within(check_server_has_host, WAIT_TIME, arg=1)
118*cfb92d14SAndroid Build Coastguard Worker
119*cfb92d14SAndroid Build Coastguard Workercache_table = r1.get_eidcache()
120*cfb92d14SAndroid Build Coastguard Worker
121*cfb92d14SAndroid Build Coastguard Workerfor entry in cache_table:
122*cfb92d14SAndroid Build Coastguard Worker    fields = entry.strip().split(' ')
123*cfb92d14SAndroid Build Coastguard Worker    if (fields[0] == 'fd00:0:0:0:0:0:0:2'):
124*cfb92d14SAndroid Build Coastguard Worker        verify(int(fields[1], 16) == r2_rloc)
125*cfb92d14SAndroid Build Coastguard Worker        verify(fields[2] == 'snoop')
126*cfb92d14SAndroid Build Coastguard Worker        break
127*cfb92d14SAndroid Build Coastguard Workerelse:
128*cfb92d14SAndroid Build Coastguard Worker    verify(False)  # did not find cache entry
129*cfb92d14SAndroid Build Coastguard Worker
130*cfb92d14SAndroid Build Coastguard Worker# Register from r3 which one hop away from r1 server.
131*cfb92d14SAndroid Build Coastguard Worker
132*cfb92d14SAndroid Build Coastguard Workerr3.srp_client_enable_auto_start_mode()
133*cfb92d14SAndroid Build Coastguard Workerr3.srp_client_set_host_name('r3')
134*cfb92d14SAndroid Build Coastguard Workerr3.srp_client_set_host_address('fd00::3')
135*cfb92d14SAndroid Build Coastguard Workerr3.srp_client_add_service('srv3', '_test._udp', 333, 0, 0)
136*cfb92d14SAndroid Build Coastguard Worker
137*cfb92d14SAndroid Build Coastguard Workerverify_within(check_server_has_host, WAIT_TIME, arg=2)
138*cfb92d14SAndroid Build Coastguard Worker
139*cfb92d14SAndroid Build Coastguard Workercache_table = r1.get_eidcache()
140*cfb92d14SAndroid Build Coastguard Worker
141*cfb92d14SAndroid Build Coastguard Workerfor entry in cache_table:
142*cfb92d14SAndroid Build Coastguard Worker    fields = entry.strip().split(' ')
143*cfb92d14SAndroid Build Coastguard Worker    if (fields[0] == 'fd00:0:0:0:0:0:0:3'):
144*cfb92d14SAndroid Build Coastguard Worker        verify(int(fields[1], 16) == r3_rloc)
145*cfb92d14SAndroid Build Coastguard Worker        verify(fields[2] == 'snoop')
146*cfb92d14SAndroid Build Coastguard Worker        break
147*cfb92d14SAndroid Build Coastguard Workerelse:
148*cfb92d14SAndroid Build Coastguard Worker    verify(False)  # did not find cache entry
149*cfb92d14SAndroid Build Coastguard Worker
150*cfb92d14SAndroid Build Coastguard Worker# Register from sed2 which child of r3. The cache table should
151*cfb92d14SAndroid Build Coastguard Worker# use the `r3` as the parent of sed2.
152*cfb92d14SAndroid Build Coastguard Worker
153*cfb92d14SAndroid Build Coastguard Workersed2.srp_client_enable_auto_start_mode()
154*cfb92d14SAndroid Build Coastguard Workersed2.srp_client_set_host_name('sed2')
155*cfb92d14SAndroid Build Coastguard Workersed2.srp_client_set_host_address('fd00::1:3')
156*cfb92d14SAndroid Build Coastguard Workersed2.srp_client_add_service('srv4', '_test._udp', 333, 0, 0)
157*cfb92d14SAndroid Build Coastguard Worker
158*cfb92d14SAndroid Build Coastguard Workerverify_within(check_server_has_host, WAIT_TIME, arg=3)
159*cfb92d14SAndroid Build Coastguard Worker
160*cfb92d14SAndroid Build Coastguard Workercache_table = r1.get_eidcache()
161*cfb92d14SAndroid Build Coastguard Worker
162*cfb92d14SAndroid Build Coastguard Workerfor entry in cache_table:
163*cfb92d14SAndroid Build Coastguard Worker    fields = entry.strip().split(' ')
164*cfb92d14SAndroid Build Coastguard Worker    if (fields[0] == 'fd00:0:0:0:0:0:1:3'):
165*cfb92d14SAndroid Build Coastguard Worker        verify(int(fields[1], 16) == r3_rloc)
166*cfb92d14SAndroid Build Coastguard Worker        verify(fields[2] == 'snoop')
167*cfb92d14SAndroid Build Coastguard Worker        break
168*cfb92d14SAndroid Build Coastguard Workerelse:
169*cfb92d14SAndroid Build Coastguard Worker    verify(False)  # did not find cache entry
170*cfb92d14SAndroid Build Coastguard Worker
171*cfb92d14SAndroid Build Coastguard Worker# Register from fed1 which child of server (r1) itself. The cache table should
172*cfb92d14SAndroid Build Coastguard Worker# be properly updated
173*cfb92d14SAndroid Build Coastguard Worker
174*cfb92d14SAndroid Build Coastguard Workerfed1.srp_client_enable_auto_start_mode()
175*cfb92d14SAndroid Build Coastguard Workerfed1.srp_client_set_host_name('fed1')
176*cfb92d14SAndroid Build Coastguard Workerfed1.srp_client_set_host_address('fd00::2:3')
177*cfb92d14SAndroid Build Coastguard Workerfed1.srp_client_add_service('srv5', '_test._udp', 555, 0, 0)
178*cfb92d14SAndroid Build Coastguard Worker
179*cfb92d14SAndroid Build Coastguard Workerverify_within(check_server_has_host, WAIT_TIME, arg=4)
180*cfb92d14SAndroid Build Coastguard Worker
181*cfb92d14SAndroid Build Coastguard Workercache_table = r1.get_eidcache()
182*cfb92d14SAndroid Build Coastguard Worker
183*cfb92d14SAndroid Build Coastguard Workerfor entry in cache_table:
184*cfb92d14SAndroid Build Coastguard Worker    fields = entry.strip().split(' ')
185*cfb92d14SAndroid Build Coastguard Worker    if (fields[0] == 'fd00:0:0:0:0:0:2:3'):
186*cfb92d14SAndroid Build Coastguard Worker        verify(int(fields[1], 16) == fed1_rloc)
187*cfb92d14SAndroid Build Coastguard Worker        verify(fields[2] == 'snoop')
188*cfb92d14SAndroid Build Coastguard Worker        break
189*cfb92d14SAndroid Build Coastguard Workerelse:
190*cfb92d14SAndroid Build Coastguard Worker    verify(False)  # did not find cache entry
191*cfb92d14SAndroid Build Coastguard Worker
192*cfb92d14SAndroid Build Coastguard Worker# Register from sed1 which is a sleepy child of server (r1).
193*cfb92d14SAndroid Build Coastguard Worker# The cache table should not be updated for sleepy child.
194*cfb92d14SAndroid Build Coastguard Worker
195*cfb92d14SAndroid Build Coastguard Workersed1.srp_client_enable_auto_start_mode()
196*cfb92d14SAndroid Build Coastguard Workersed1.srp_client_set_host_name('sed1')
197*cfb92d14SAndroid Build Coastguard Workersed1.srp_client_set_host_address('fd00::3:4')
198*cfb92d14SAndroid Build Coastguard Workersed1.srp_client_add_service('srv5', '_test._udp', 555, 0, 0)
199*cfb92d14SAndroid Build Coastguard Worker
200*cfb92d14SAndroid Build Coastguard Workerverify_within(check_server_has_host, WAIT_TIME, arg=4)
201*cfb92d14SAndroid Build Coastguard Worker
202*cfb92d14SAndroid Build Coastguard Workercache_table = r1.get_eidcache()
203*cfb92d14SAndroid Build Coastguard Worker
204*cfb92d14SAndroid Build Coastguard Workerfor entry in cache_table:
205*cfb92d14SAndroid Build Coastguard Worker    fields = entry.strip().split(' ')
206*cfb92d14SAndroid Build Coastguard Worker    verify(fields[0] != 'fd00:0:0:0:0:0:3:4')
207*cfb92d14SAndroid Build Coastguard Worker
208*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
209*cfb92d14SAndroid Build Coastguard Worker# Test finished
210*cfb92d14SAndroid Build Coastguard Worker
211*cfb92d14SAndroid Build Coastguard Workercli.Node.finalize_all_nodes()
212*cfb92d14SAndroid Build Coastguard Worker
213*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
214