xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-015-same-prefix-on-multiple-nodes.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 wpan
30*cfb92d14SAndroid Build Coastguard Workerfrom wpan import verify
31*cfb92d14SAndroid Build Coastguard Worker
32*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
33*cfb92d14SAndroid Build Coastguard Worker# Test description: Adding addresses with same prefix on multiple nodes.
34*cfb92d14SAndroid Build Coastguard Worker#
35*cfb92d14SAndroid Build Coastguard Worker
36*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
37*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
38*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
39*cfb92d14SAndroid Build Coastguard Worker
40*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
41*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances
42*cfb92d14SAndroid Build Coastguard Worker
43*cfb92d14SAndroid Build Coastguard Workerspeedup = 4
44*cfb92d14SAndroid Build Coastguard Workerwpan.Node.set_time_speedup_factor(speedup)
45*cfb92d14SAndroid Build Coastguard Worker
46*cfb92d14SAndroid Build Coastguard Workerr1 = wpan.Node()
47*cfb92d14SAndroid Build Coastguard Workerr2 = wpan.Node()
48*cfb92d14SAndroid Build Coastguard Workersed2 = wpan.Node()
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Workerall_nodes = [r1, r2, sed2]
51*cfb92d14SAndroid Build Coastguard Worker
52*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
53*cfb92d14SAndroid Build Coastguard Worker# Init all nodes
54*cfb92d14SAndroid Build Coastguard Worker
55*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes()
56*cfb92d14SAndroid Build Coastguard Worker
57*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
58*cfb92d14SAndroid Build Coastguard Worker# Build network topology
59*cfb92d14SAndroid Build Coastguard Worker#
60*cfb92d14SAndroid Build Coastguard Worker# Two routers r1 and r2 (sed2 is used for quick promotion of r2 to router
61*cfb92d14SAndroid Build Coastguard Worker# status).
62*cfb92d14SAndroid Build Coastguard Worker
63*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2)
64*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1)
65*cfb92d14SAndroid Build Coastguard Worker
66*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(sed2)
67*cfb92d14SAndroid Build Coastguard Workersed2.allowlist_node(r2)
68*cfb92d14SAndroid Build Coastguard Worker
69*cfb92d14SAndroid Build Coastguard Workerr1.form("same-prefix")
70*cfb92d14SAndroid Build Coastguard Worker
71*cfb92d14SAndroid Build Coastguard Workerr2.join_node(r1, wpan.JOIN_TYPE_ROUTER)
72*cfb92d14SAndroid Build Coastguard Workersed2.join_node(r2, wpan.JOIN_TYPE_SLEEPY_END_DEVICE)
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Workersed2.set(wpan.WPAN_POLL_INTERVAL, '500')
75*cfb92d14SAndroid Build Coastguard Worker
76*cfb92d14SAndroid Build Coastguard Workerr2.status()
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
79*cfb92d14SAndroid Build Coastguard Worker# Test implementation
80*cfb92d14SAndroid Build Coastguard Worker
81*cfb92d14SAndroid Build Coastguard WorkerIP6_PREFIX = "fd00:abba::"
82*cfb92d14SAndroid Build Coastguard Worker
83*cfb92d14SAndroid Build Coastguard WorkerIP6_ADDR_1 = IP6_PREFIX + "1"
84*cfb92d14SAndroid Build Coastguard WorkerIP6_ADDR_2 = IP6_PREFIX + "2"
85*cfb92d14SAndroid Build Coastguard Worker
86*cfb92d14SAndroid Build Coastguard Worker# Add IP6_ADDR_2 to r2.
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Workerr2.add_ip6_address_on_interface(IP6_ADDR_2, prefix_len=64)
89*cfb92d14SAndroid Build Coastguard Worker
90*cfb92d14SAndroid Build Coastguard Worker# Verify (within 5 seconds) that corresponding prefix is seen on both nodes.
91*cfb92d14SAndroid Build Coastguard Worker
92*cfb92d14SAndroid Build Coastguard Worker
93*cfb92d14SAndroid Build Coastguard Workerdef check_prefix():
94*cfb92d14SAndroid Build Coastguard Worker    for node in [r1, r2]:
95*cfb92d14SAndroid Build Coastguard Worker        prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES))
96*cfb92d14SAndroid Build Coastguard Worker        for p in prefixes:
97*cfb92d14SAndroid Build Coastguard Worker            if p.prefix == IP6_PREFIX:
98*cfb92d14SAndroid Build Coastguard Worker                if (p.origin == 'ncp' and p.prefix_len == '64' and p.is_stable() and p.is_on_mesh() and
99*cfb92d14SAndroid Build Coastguard Worker                        p.is_preferred() and not p.is_def_route() and not p.is_slaac() and not p.is_dhcp() and
100*cfb92d14SAndroid Build Coastguard Worker                        not p.is_config() and p.priority == "med"):
101*cfb92d14SAndroid Build Coastguard Worker                    break
102*cfb92d14SAndroid Build Coastguard Worker        else:  # `for` loop finished without finding the prefix.
103*cfb92d14SAndroid Build Coastguard Worker            raise wpan.VerifyError('Did not find prefix {} on node {}'.format(IP6_PREFIX, r1))
104*cfb92d14SAndroid Build Coastguard Worker
105*cfb92d14SAndroid Build Coastguard Worker
106*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix, 5)
107*cfb92d14SAndroid Build Coastguard Worker
108*cfb92d14SAndroid Build Coastguard Worker# After prefix is seen on r1, add an address with same prefix on r1.
109*cfb92d14SAndroid Build Coastguard Workerr1.add_ip6_address_on_interface(IP6_ADDR_1, prefix_len=64)
110*cfb92d14SAndroid Build Coastguard Worker
111*cfb92d14SAndroid Build Coastguard Worker# Verify that the prefix is still seen on both nodes.
112*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix, 5)
113*cfb92d14SAndroid Build Coastguard Worker
114*cfb92d14SAndroid Build Coastguard Worker# Remove the address from r2 which should remove the corresponding the prefix as well
115*cfb92d14SAndroid Build Coastguard Worker# After this since r1 still has the address, the prefix should be present
116*cfb92d14SAndroid Build Coastguard Worker# on both nodes.
117*cfb92d14SAndroid Build Coastguard Workerr2.remove_ip6_address_on_interface(IP6_ADDR_2, prefix_len=64)
118*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix, 5)
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Worker# Reset r1 and verify that the prefix is retained correctly (by wpantund).
121*cfb92d14SAndroid Build Coastguard Workerr1.reset()
122*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix, 8)
123*cfb92d14SAndroid Build Coastguard Worker
124*cfb92d14SAndroid Build Coastguard Worker# Remove the address on r1. Verify that prefix list is empty.
125*cfb92d14SAndroid Build Coastguard Workerr1.remove_ip6_address_on_interface(IP6_ADDR_1, prefix_len=64)
126*cfb92d14SAndroid Build Coastguard Worker
127*cfb92d14SAndroid Build Coastguard Worker
128*cfb92d14SAndroid Build Coastguard Workerdef check_empty_prefix_list():
129*cfb92d14SAndroid Build Coastguard Worker    for node in [r1, r2]:
130*cfb92d14SAndroid Build Coastguard Worker        prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES))
131*cfb92d14SAndroid Build Coastguard Worker        verify(len(prefixes) == 0)
132*cfb92d14SAndroid Build Coastguard Worker
133*cfb92d14SAndroid Build Coastguard Worker
134*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_empty_prefix_list, 5)
135*cfb92d14SAndroid Build Coastguard Worker
136*cfb92d14SAndroid Build Coastguard Worker# Add both addresses back-to-back and check the prefix list to contain the
137*cfb92d14SAndroid Build Coastguard Worker# prefix.
138*cfb92d14SAndroid Build Coastguard Workerr1.add_ip6_address_on_interface(IP6_ADDR_1, prefix_len=64)
139*cfb92d14SAndroid Build Coastguard Workerr2.add_ip6_address_on_interface(IP6_ADDR_2, prefix_len=64)
140*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix, 5)
141*cfb92d14SAndroid Build Coastguard Worker
142*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
143*cfb92d14SAndroid Build Coastguard Worker# Test finished
144*cfb92d14SAndroid Build Coastguard Worker
145*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes()
146*cfb92d14SAndroid Build Coastguard Worker
147*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
148