xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-030-anycast-forwarding.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1#!/usr/bin/env python3
2#
3#  Copyright (c) 2024, 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
32import time
33
34# -----------------------------------------------------------------------------------------------------------------------
35# Test description: ALOC address forwarding to ED and SED devices.
36#
37# Network topology
38#
39#    r1 ---- r2 ---- r3
40#                    /|\
41#                   / | \
42#                 ed sed sed2
43#
44
45test_name = __file__[:-3] if __file__.endswith('.py') else __file__
46print('-' * 120)
47print('Starting \'{}\''.format(test_name))
48
49# -----------------------------------------------------------------------------------------------------------------------
50# Creating `cli.Node` instances
51
52speedup = 40
53cli.Node.set_time_speedup_factor(speedup)
54
55r1 = cli.Node()
56r2 = cli.Node()
57r3 = cli.Node()
58ed = cli.Node()
59sed = cli.Node()
60sed2 = cli.Node()
61
62nodes = [r1, r2, r3, ed, sed, sed2]
63
64# -----------------------------------------------------------------------------------------------------------------------
65# Form topology
66
67r1.allowlist_node(r2)
68
69r2.allowlist_node(r1)
70r2.allowlist_node(r3)
71
72r3.allowlist_node(r2)
73r3.allowlist_node(ed)
74r3.allowlist_node(sed)
75r3.allowlist_node(sed2)
76
77ed.allowlist_node(r3)
78sed.allowlist_node(r3)
79sed2.allowlist_node(r3)
80
81r1.form('aloc')
82r2.join(r1)
83r3.join(r2)
84ed.join(r2, cli.JOIN_TYPE_END_DEVICE)
85sed.join(r3, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
86sed2.join(r3, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
87
88sed.set_pollperiod(400)
89sed2.set_pollperiod(500)
90
91verify(r1.get_state() == 'leader')
92verify(r2.get_state() == 'router')
93verify(r3.get_state() == 'router')
94verify(ed.get_state() == 'child')
95verify(sed.get_state() == 'child')
96verify(sed.get_state() == 'child')
97
98sed.set_mode('n')
99
100for child in [ed, sed, sed2]:
101    verify(int(child.get_parent_info()['Rloc'], 16) == int(r3.get_rloc16(), 16))
102
103# -----------------------------------------------------------------------------------------------------------------------
104# Test Implementation
105
106
107def check_netdata_services(expected_num_services):
108    # Check that all nodes see the `expected_num_services` service
109    # entries in network data.
110    for node in nodes:
111        verify(len(node.get_netdata_services()) == expected_num_services)
112
113
114wait_time = 5
115
116# Add a service on ed which is non-sleepy child.
117
118ed.cli('service add 44970 11 00')
119ed.register_netdata()
120
121verify_within(check_netdata_services, wait_time, 1)
122
123# Verify that the ALOC associated with a non-sleepy child's service is
124# pingable from all nodes.
125
126aloc1 = r1.get_mesh_local_prefix().split('/')[0] + 'ff:fe00:fc10'
127
128for node in nodes:
129    node.ping(aloc1)
130
131# Add a service now from `sed` which is a sleepy child and again make sure
132# its ALOC can be pinged from all other nodes.
133
134sed.cli('service add 44970 22 00')
135sed.register_netdata()
136
137verify_within(check_netdata_services, wait_time, 2)
138
139aloc2 = r1.get_mesh_local_prefix().split('/')[0] + 'ff:fe00:fc11'
140
141r1.ping(aloc2)
142
143for node in nodes:
144    node.ping(aloc2)
145
146# -----------------------------------------------------------------------------------------------------------------------
147# Test finished
148
149cli.Node.finalize_all_nodes()
150
151print('\'{}\' passed.'.format(test_name))
152