xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-024-partition-merge.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2018, 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 Workerimport time
30*cfb92d14SAndroid Build Coastguard Workerimport wpan
31*cfb92d14SAndroid Build Coastguard Workerfrom wpan import verify
32*cfb92d14SAndroid Build Coastguard Worker
33*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
34*cfb92d14SAndroid Build Coastguard Worker# Test description: Partition formation and merge
35*cfb92d14SAndroid Build Coastguard Worker#
36*cfb92d14SAndroid Build Coastguard Worker# Network Topology:
37*cfb92d14SAndroid Build Coastguard Worker#
38*cfb92d14SAndroid Build Coastguard Worker#      r1 ---- / ---- r2
39*cfb92d14SAndroid Build Coastguard Worker#      |       \      |
40*cfb92d14SAndroid Build Coastguard Worker#      |       /      |
41*cfb92d14SAndroid Build Coastguard Worker#      c1      \      c2
42*cfb92d14SAndroid Build Coastguard Worker#
43*cfb92d14SAndroid Build Coastguard Worker#
44*cfb92d14SAndroid Build Coastguard Worker# Test covers the following situations:
45*cfb92d14SAndroid Build Coastguard Worker#
46*cfb92d14SAndroid Build Coastguard Worker# - r2 forming its own partition when it can no longer hear r1
47*cfb92d14SAndroid Build Coastguard Worker# - Partitions merging into one once r1 and r2 can talk again
48*cfb92d14SAndroid Build Coastguard Worker# - Adding on-mesh prefixes on each partition and ensuring after
49*cfb92d14SAndroid Build Coastguard Worker#   merge the info in combined.
50*cfb92d14SAndroid Build Coastguard Worker#
51*cfb92d14SAndroid Build Coastguard Worker
52*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
53*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
54*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
55*cfb92d14SAndroid Build Coastguard Worker
56*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
57*cfb92d14SAndroid Build Coastguard Worker# Utility functions
58*cfb92d14SAndroid Build Coastguard Worker
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Workerdef verify_prefix(
61*cfb92d14SAndroid Build Coastguard Worker    node_list,
62*cfb92d14SAndroid Build Coastguard Worker    prefix,
63*cfb92d14SAndroid Build Coastguard Worker    prefix_len=64,
64*cfb92d14SAndroid Build Coastguard Worker    stable=True,
65*cfb92d14SAndroid Build Coastguard Worker    priority='med',
66*cfb92d14SAndroid Build Coastguard Worker    on_mesh=False,
67*cfb92d14SAndroid Build Coastguard Worker    slaac=False,
68*cfb92d14SAndroid Build Coastguard Worker    dhcp=False,
69*cfb92d14SAndroid Build Coastguard Worker    configure=False,
70*cfb92d14SAndroid Build Coastguard Worker    default_route=False,
71*cfb92d14SAndroid Build Coastguard Worker    preferred=False,
72*cfb92d14SAndroid Build Coastguard Worker):
73*cfb92d14SAndroid Build Coastguard Worker    """
74*cfb92d14SAndroid Build Coastguard Worker    This function verifies that the `prefix` is present on all the nodes in the `node_list`.
75*cfb92d14SAndroid Build Coastguard Worker    """
76*cfb92d14SAndroid Build Coastguard Worker    for node in node_list:
77*cfb92d14SAndroid Build Coastguard Worker        prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES))
78*cfb92d14SAndroid Build Coastguard Worker        for p in prefixes:
79*cfb92d14SAndroid Build Coastguard Worker            if p.prefix == prefix:
80*cfb92d14SAndroid Build Coastguard Worker                verify(int(p.prefix_len) == prefix_len)
81*cfb92d14SAndroid Build Coastguard Worker                verify(p.is_stable() == stable)
82*cfb92d14SAndroid Build Coastguard Worker                verify(p.is_on_mesh() == on_mesh)
83*cfb92d14SAndroid Build Coastguard Worker                verify(p.is_def_route() == default_route)
84*cfb92d14SAndroid Build Coastguard Worker                verify(p.is_slaac() == slaac)
85*cfb92d14SAndroid Build Coastguard Worker                verify(p.is_dhcp() == dhcp)
86*cfb92d14SAndroid Build Coastguard Worker                verify(p.is_config() == configure)
87*cfb92d14SAndroid Build Coastguard Worker                verify(p.is_preferred() == preferred)
88*cfb92d14SAndroid Build Coastguard Worker                verify(p.priority == priority)
89*cfb92d14SAndroid Build Coastguard Worker                break
90*cfb92d14SAndroid Build Coastguard Worker        else:
91*cfb92d14SAndroid Build Coastguard Worker            raise wpan.VerifyError("Did not find prefix {} on node {}".format(prefix, node))
92*cfb92d14SAndroid Build Coastguard Worker
93*cfb92d14SAndroid Build Coastguard Worker
94*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
95*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances
96*cfb92d14SAndroid Build Coastguard Worker
97*cfb92d14SAndroid Build Coastguard Workerspeedup = 25
98*cfb92d14SAndroid Build Coastguard Workerwpan.Node.set_time_speedup_factor(speedup)
99*cfb92d14SAndroid Build Coastguard Worker
100*cfb92d14SAndroid Build Coastguard Workerr1 = wpan.Node()
101*cfb92d14SAndroid Build Coastguard Workerr2 = wpan.Node()
102*cfb92d14SAndroid Build Coastguard Workerc1 = wpan.Node()
103*cfb92d14SAndroid Build Coastguard Workerc2 = wpan.Node()
104*cfb92d14SAndroid Build Coastguard Worker
105*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
106*cfb92d14SAndroid Build Coastguard Worker# Init all nodes
107*cfb92d14SAndroid Build Coastguard Worker
108*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes()
109*cfb92d14SAndroid Build Coastguard Worker
110*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
111*cfb92d14SAndroid Build Coastguard Worker# Build network topology
112*cfb92d14SAndroid Build Coastguard Worker#
113*cfb92d14SAndroid Build Coastguard Worker
114*cfb92d14SAndroid Build Coastguard Workerr1.form("partition")
115*cfb92d14SAndroid Build Coastguard Worker
116*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2)
117*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1)
118*cfb92d14SAndroid Build Coastguard Workerr2.join_node(r1, wpan.JOIN_TYPE_ROUTER)
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Workerc1.allowlist_node(r1)
121*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(c1)
122*cfb92d14SAndroid Build Coastguard Workerc1.join_node(r1, wpan.JOIN_TYPE_END_DEVICE)
123*cfb92d14SAndroid Build Coastguard Worker
124*cfb92d14SAndroid Build Coastguard Workerc2.allowlist_node(r2)
125*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(c2)
126*cfb92d14SAndroid Build Coastguard Workerc2.join_node(r2, wpan.JOIN_TYPE_END_DEVICE)
127*cfb92d14SAndroid Build Coastguard Worker
128*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
129*cfb92d14SAndroid Build Coastguard Worker# Test implementation
130*cfb92d14SAndroid Build Coastguard Worker
131*cfb92d14SAndroid Build Coastguard Workershort_wait = 6
132*cfb92d14SAndroid Build Coastguard Workerlong_wait = 15  # With speedup of 25, this emulates 375s (~6 min).
133*cfb92d14SAndroid Build Coastguard Worker
134*cfb92d14SAndroid Build Coastguard Workerprefix1 = "fd00:1234::"
135*cfb92d14SAndroid Build Coastguard Workerprefix2 = "fd00:abcd::"
136*cfb92d14SAndroid Build Coastguard Worker
137*cfb92d14SAndroid Build Coastguard Workertime.sleep(1)
138*cfb92d14SAndroid Build Coastguard Worker
139*cfb92d14SAndroid Build Coastguard Workerverify(r1.get(wpan.WPAN_NODE_TYPE) == wpan.NODE_TYPE_LEADER)
140*cfb92d14SAndroid Build Coastguard Workerverify(r2.get(wpan.WPAN_NODE_TYPE) == wpan.NODE_TYPE_ROUTER)
141*cfb92d14SAndroid Build Coastguard Worker
142*cfb92d14SAndroid Build Coastguard Worker# Now force the two routers to form their own partition
143*cfb92d14SAndroid Build Coastguard Worker# by removing them from each other's allowlist table
144*cfb92d14SAndroid Build Coastguard Workerr1.un_allowlist_node(r2)
145*cfb92d14SAndroid Build Coastguard Workerr2.un_allowlist_node(r1)
146*cfb92d14SAndroid Build Coastguard Worker
147*cfb92d14SAndroid Build Coastguard Worker# Add a prefix before r2 realizes it can not longer talk
148*cfb92d14SAndroid Build Coastguard Worker# to leader (r1).
149*cfb92d14SAndroid Build Coastguard Workerr2.add_prefix(prefix2)
150*cfb92d14SAndroid Build Coastguard Worker
151*cfb92d14SAndroid Build Coastguard Worker# Check that r2 forms its own partition
152*cfb92d14SAndroid Build Coastguard Worker
153*cfb92d14SAndroid Build Coastguard Worker
154*cfb92d14SAndroid Build Coastguard Workerdef check_r2_become_leader():
155*cfb92d14SAndroid Build Coastguard Worker    verify(r2.get(wpan.WPAN_NODE_TYPE) == wpan.NODE_TYPE_LEADER)
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Worker
158*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_r2_become_leader, long_wait)
159*cfb92d14SAndroid Build Coastguard Worker
160*cfb92d14SAndroid Build Coastguard Worker# While we have two partition, add a prefix on r1
161*cfb92d14SAndroid Build Coastguard Workerr1.add_prefix(prefix1)
162*cfb92d14SAndroid Build Coastguard Worker
163*cfb92d14SAndroid Build Coastguard Worker# Update allowlist and wait for partitions to merge.
164*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2)
165*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1)
166*cfb92d14SAndroid Build Coastguard Worker
167*cfb92d14SAndroid Build Coastguard Worker
168*cfb92d14SAndroid Build Coastguard Workerdef check_partition_id_match():
169*cfb92d14SAndroid Build Coastguard Worker    verify(r1.get(wpan.WPAN_PARTITION_ID) == r2.get(wpan.WPAN_PARTITION_ID))
170*cfb92d14SAndroid Build Coastguard Worker
171*cfb92d14SAndroid Build Coastguard Worker
172*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_partition_id_match, long_wait)
173*cfb92d14SAndroid Build Coastguard Worker
174*cfb92d14SAndroid Build Coastguard Worker# Check that partitions merged successfully
175*cfb92d14SAndroid Build Coastguard Worker
176*cfb92d14SAndroid Build Coastguard Worker
177*cfb92d14SAndroid Build Coastguard Workerdef check_r1_r2_roles():
178*cfb92d14SAndroid Build Coastguard Worker    r1_type = r1.get(wpan.WPAN_NODE_TYPE)
179*cfb92d14SAndroid Build Coastguard Worker    r2_type = r2.get(wpan.WPAN_NODE_TYPE)
180*cfb92d14SAndroid Build Coastguard Worker    verify((r1_type == wpan.NODE_TYPE_LEADER and r2_type == wpan.NODE_TYPE_ROUTER) or
181*cfb92d14SAndroid Build Coastguard Worker           (r2_type == wpan.NODE_TYPE_LEADER and r1_type == wpan.NODE_TYPE_ROUTER))
182*cfb92d14SAndroid Build Coastguard Worker
183*cfb92d14SAndroid Build Coastguard Worker
184*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_r1_r2_roles, short_wait)
185*cfb92d14SAndroid Build Coastguard Worker
186*cfb92d14SAndroid Build Coastguard Worker# Verify all nodes have both prefixes
187*cfb92d14SAndroid Build Coastguard Worker
188*cfb92d14SAndroid Build Coastguard Worker
189*cfb92d14SAndroid Build Coastguard Workerdef check_prefixes():
190*cfb92d14SAndroid Build Coastguard Worker    verify_prefix([r1, r2, c1, c2], prefix1)
191*cfb92d14SAndroid Build Coastguard Worker    verify_prefix([r1, r2, c1, c2], prefix2)
192*cfb92d14SAndroid Build Coastguard Worker
193*cfb92d14SAndroid Build Coastguard Worker
194*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefixes, short_wait)
195*cfb92d14SAndroid Build Coastguard Worker
196*cfb92d14SAndroid Build Coastguard Worker# Verify that the children stayed with their parents
197*cfb92d14SAndroid Build Coastguard Workerverify(len(wpan.parse_list(r1.get(wpan.WPAN_THREAD_CHILD_TABLE))) == 1)
198*cfb92d14SAndroid Build Coastguard Workerverify(len(wpan.parse_list(r2.get(wpan.WPAN_THREAD_CHILD_TABLE))) == 1)
199*cfb92d14SAndroid Build Coastguard Worker
200*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
201*cfb92d14SAndroid Build Coastguard Worker# Test finished
202*cfb92d14SAndroid Build Coastguard Worker
203*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes()
204*cfb92d14SAndroid Build Coastguard Worker
205*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
206