xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-025-mesh-local-prefix-change.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1#!/usr/bin/env python3
2#
3#  Copyright (c) 2023, 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: Validate update of unicast/multicast mesh-local addresses on prefix change
36#
37# Network topology
38#
39#    r1 ---- r2
40#
41
42test_name = __file__[:-3] if __file__.endswith('.py') else __file__
43print('-' * 120)
44print('Starting \'{}\''.format(test_name))
45
46# -----------------------------------------------------------------------------------------------------------------------
47# Creating `cli.Node` instances
48
49speedup = 40
50cli.Node.set_time_speedup_factor(speedup)
51
52r1 = cli.Node()
53r2 = cli.Node()
54
55# -----------------------------------------------------------------------------------------------------------------------
56# Form topology
57
58r1.form('ml-change')
59r2.join(r1)
60
61verify(r1.get_state() == 'leader')
62verify(r2.get_state() == 'router')
63
64# -----------------------------------------------------------------------------------------------------------------------
65# Test Implementation
66
67r1.srp_server_enable()
68r1.srp_client_enable_auto_start_mode()
69r2.srp_client_enable_auto_start_mode()
70
71time.sleep(0.5)
72
73ml_prefix = r1.get_mesh_local_prefix()
74ml_prefix = ml_prefix[:ml_prefix.index("/64") - 1]
75
76# Validate that r1 has 4 mesh-local address: ML-EID, RLOC, leader ALOC
77# and since it is acting as SRP server the service ALOC.
78
79r1_addrs = r1.get_ip_addrs()
80verify(sum([addr.startswith(ml_prefix) for addr in r1_addrs]) == 4)
81
82# Validate that r1 has link-local and realm-local All Thread Nodes
83# multicast addresses which are prefix-based and use mesh-local
84# prefix.
85
86r1_maddrs = r1.get_ip_maddrs()
87verify(sum([ml_prefix in maddr for maddr in r1_maddrs]) == 2)
88
89# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90# Change the mesh-local prefix on all devices.
91
92r2.cli('dataset clear')
93r2.cli('dataset meshlocalprefix fd00:1122:3344:5566::')
94r2.cli('dataset delaytimer 1100')  # `toranj` allows 1000 msec as minimum in its config
95r2.cli('dataset updater start')
96
97time.sleep(0.5)
98
99ml_prefix = r1.get_mesh_local_prefix()
100ml_prefix = ml_prefix[:ml_prefix.index("/64") - 1]
101
102verify(ml_prefix == 'fd00:1122:3344:5566:')
103
104# Validate that all 4 mesh-local address on r1 are updated to use the
105# new mesh-local prefix.
106
107r1_addrs = r1.get_ip_addrs()
108verify(sum([addr.startswith(ml_prefix) for addr in r1_addrs]) == 4)
109
110# Validate the network data entry for SRP server is also updated to
111# used the new address.
112
113verify(r1.srp_client_get_server_address().startswith(ml_prefix))
114
115# Validate the r1 multicast addresses are also updated
116
117r1_maddrs = r1.get_ip_maddrs()
118verify(sum([ml_prefix in maddr for maddr in r1_maddrs]) == 2)
119
120# -----------------------------------------------------------------------------------------------------------------------
121# Test finished
122
123cli.Node.finalize_all_nodes()
124
125print('\'{}\' passed.'.format(test_name))
126