xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-014-ip6-address-add.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/Removing IPv6 addresses on routers and SEDs on network interface.
34*cfb92d14SAndroid Build Coastguard Worker#
35*cfb92d14SAndroid Build Coastguard Worker# Test topology:
36*cfb92d14SAndroid Build Coastguard Worker#
37*cfb92d14SAndroid Build Coastguard Worker#     r1 ---- r2
38*cfb92d14SAndroid Build Coastguard Worker#     |       |
39*cfb92d14SAndroid Build Coastguard Worker#     |       |
40*cfb92d14SAndroid Build Coastguard Worker#    fed1    sed2
41*cfb92d14SAndroid Build Coastguard Worker#
42*cfb92d14SAndroid Build Coastguard Worker# IPv6 addresses are added as follows:
43*cfb92d14SAndroid Build Coastguard Worker# - On `r2`   add `IP6_ADDR_1` with prefix `IP6_PREFIX_1`
44*cfb92d14SAndroid Build Coastguard Worker# - On `fed1` add `IP6_ADDR_2` with prefix `IP6_PREFIX_2`
45*cfb92d14SAndroid Build Coastguard Worker# - On `sed2` add `IP6_ADDR_3` with prefix `IP6_PREFIX_3`
46*cfb92d14SAndroid Build Coastguard Worker#
47*cfb92d14SAndroid Build Coastguard Worker# The test then covers the following:
48*cfb92d14SAndroid Build Coastguard Worker# - Verify that the addresses are present in "IPv6:AllAddresses" wpantund property on the corresponding node.
49*cfb92d14SAndroid Build Coastguard Worker# - Verify that all prefixes are present in network data with correct configuration flags (on all nodes).
50*cfb92d14SAndroid Build Coastguard Worker# - Verify that `sed2`'s address is present in `r2` (its parent) "Thread:ChildTable:Addresses".
51*cfb92d14SAndroid Build Coastguard Worker# - Verify that addresses/prefixes are retained by wpantund over NCP reset.
52*cfb92d14SAndroid Build Coastguard Worker# - Verify that when an IPv6 address is removed from network interface, its corresponding prefix is also removed from
53*cfb92d14SAndroid Build Coastguard Worker#   all nodes.
54*cfb92d14SAndroid Build Coastguard Worker
55*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
56*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
57*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
58*cfb92d14SAndroid Build Coastguard Worker
59*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
60*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances
61*cfb92d14SAndroid Build Coastguard Worker
62*cfb92d14SAndroid Build Coastguard Workerspeedup = 4
63*cfb92d14SAndroid Build Coastguard Workerwpan.Node.set_time_speedup_factor(speedup)
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard Workerr1 = wpan.Node()
66*cfb92d14SAndroid Build Coastguard Workerfed1 = wpan.Node()
67*cfb92d14SAndroid Build Coastguard Workerr2 = wpan.Node()
68*cfb92d14SAndroid Build Coastguard Workersed2 = wpan.Node()
69*cfb92d14SAndroid Build Coastguard Worker
70*cfb92d14SAndroid Build Coastguard Workerall_nodes = [r1, fed1, r2, sed2]
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
73*cfb92d14SAndroid Build Coastguard Worker# Init all nodes
74*cfb92d14SAndroid Build Coastguard Worker
75*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes()
76*cfb92d14SAndroid Build Coastguard Worker
77*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
78*cfb92d14SAndroid Build Coastguard Worker# Build network topology
79*cfb92d14SAndroid Build Coastguard Worker#
80*cfb92d14SAndroid Build Coastguard Worker#   r1 ---- r2
81*cfb92d14SAndroid Build Coastguard Worker#   |       |
82*cfb92d14SAndroid Build Coastguard Worker#   |       |
83*cfb92d14SAndroid Build Coastguard Worker#  fed1    sed2
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2)
86*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1)
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(fed1)
89*cfb92d14SAndroid Build Coastguard Workerfed1.allowlist_node(r1)
90*cfb92d14SAndroid Build Coastguard Worker
91*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(sed2)
92*cfb92d14SAndroid Build Coastguard Workersed2.allowlist_node(r2)
93*cfb92d14SAndroid Build Coastguard Worker
94*cfb92d14SAndroid Build Coastguard Workerr1.form("ip-addr")
95*cfb92d14SAndroid Build Coastguard Workerr2.join_node(r1, wpan.JOIN_TYPE_ROUTER)
96*cfb92d14SAndroid Build Coastguard Worker
97*cfb92d14SAndroid Build Coastguard Workerfed1.join_node(r1, wpan.JOIN_TYPE_END_DEVICE)
98*cfb92d14SAndroid Build Coastguard Workersed2.join_node(r2, wpan.JOIN_TYPE_SLEEPY_END_DEVICE)
99*cfb92d14SAndroid Build Coastguard Worker
100*cfb92d14SAndroid Build Coastguard Workersed2.set(wpan.WPAN_POLL_INTERVAL, '300')
101*cfb92d14SAndroid Build Coastguard Worker
102*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
103*cfb92d14SAndroid Build Coastguard Worker# Test implementation
104*cfb92d14SAndroid Build Coastguard Worker
105*cfb92d14SAndroid Build Coastguard WorkerIP6_PREFIX_1 = "fd00:c0de::"
106*cfb92d14SAndroid Build Coastguard WorkerIP6_PREFIX_2 = "fd00:deed::"
107*cfb92d14SAndroid Build Coastguard WorkerIP6_PREFIX_3 = "fd00:beef::"
108*cfb92d14SAndroid Build Coastguard Worker
109*cfb92d14SAndroid Build Coastguard WorkerIP6_ADDR_1 = IP6_PREFIX_1 + "1"
110*cfb92d14SAndroid Build Coastguard WorkerIP6_ADDR_2 = IP6_PREFIX_2 + "2"
111*cfb92d14SAndroid Build Coastguard WorkerIP6_ADDR_3 = IP6_PREFIX_3 + "3"
112*cfb92d14SAndroid Build Coastguard Worker
113*cfb92d14SAndroid Build Coastguard Worker# On `r2`   add `IP6_ADDR_1` with prefix `IP6_PREFIX_1`
114*cfb92d14SAndroid Build Coastguard Worker# On `fed1` add `IP6_ADDR_2` with prefix `IP6_PREFIX_2`
115*cfb92d14SAndroid Build Coastguard Worker# On `sed2` add `IP6_ADDR_3` with prefix `IP6_PREFIX_3`
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Workerr2.add_ip6_address_on_interface(IP6_ADDR_1, prefix_len=64)
118*cfb92d14SAndroid Build Coastguard Workerfed1.add_ip6_address_on_interface(IP6_ADDR_2, prefix_len=64)
119*cfb92d14SAndroid Build Coastguard Workersed2.add_ip6_address_on_interface(IP6_ADDR_3, prefix_len=64)
120*cfb92d14SAndroid Build Coastguard Worker
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Workerdef check_addresses_and_prefixes():
123*cfb92d14SAndroid Build Coastguard Worker    # Verify that the addresses are present in "IPv6:AllAddresses" wpantund
124*cfb92d14SAndroid Build Coastguard Worker    # property on the corresponding node.
125*cfb92d14SAndroid Build Coastguard Worker    verify(r2.find_ip6_address_with_prefix(IP6_PREFIX_1) == IP6_ADDR_1)
126*cfb92d14SAndroid Build Coastguard Worker    verify(fed1.find_ip6_address_with_prefix(IP6_PREFIX_2) == IP6_ADDR_2)
127*cfb92d14SAndroid Build Coastguard Worker    verify(sed2.find_ip6_address_with_prefix(IP6_PREFIX_3) == IP6_ADDR_3)
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Worker    # Verify that all prefixes are present in network data on all nodes (with
130*cfb92d14SAndroid Build Coastguard Worker    # correct flags).
131*cfb92d14SAndroid Build Coastguard Worker    for prefix in [IP6_PREFIX_1, IP6_PREFIX_2, IP6_PREFIX_3]:
132*cfb92d14SAndroid Build Coastguard Worker        for node in all_nodes:
133*cfb92d14SAndroid Build Coastguard Worker            prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES))
134*cfb92d14SAndroid Build Coastguard Worker            for p in prefixes:
135*cfb92d14SAndroid Build Coastguard Worker                if p.prefix == prefix:
136*cfb92d14SAndroid Build Coastguard Worker                    verify(p.prefix_len == '64')
137*cfb92d14SAndroid Build Coastguard Worker                    verify(p.is_stable())
138*cfb92d14SAndroid Build Coastguard Worker                    verify(p.is_on_mesh())
139*cfb92d14SAndroid Build Coastguard Worker                    verify(p.is_preferred())
140*cfb92d14SAndroid Build Coastguard Worker                    verify(p.is_def_route() is False)
141*cfb92d14SAndroid Build Coastguard Worker                    verify(p.is_slaac() is False)
142*cfb92d14SAndroid Build Coastguard Worker                    verify(p.is_dhcp() is False)
143*cfb92d14SAndroid Build Coastguard Worker                    verify(p.is_config() is False)
144*cfb92d14SAndroid Build Coastguard Worker                    verify(p.priority == "med")
145*cfb92d14SAndroid Build Coastguard Worker                    break
146*cfb92d14SAndroid Build Coastguard Worker            else:  # `for` loop finished without finding the prefix.
147*cfb92d14SAndroid Build Coastguard Worker                raise wpan.VerifyError('Did not find prefix {} on node {}'.format(prefix, node))
148*cfb92d14SAndroid Build Coastguard Worker
149*cfb92d14SAndroid Build Coastguard Worker    # Verify that IPv6 address of `sed2` is present on `r2` (its parent)
150*cfb92d14SAndroid Build Coastguard Worker    # "Thread:ChildTable:Addresses".
151*cfb92d14SAndroid Build Coastguard Worker    addr_str = r2.get(wpan.WPAN_THREAD_CHILD_TABLE_ADDRESSES)
152*cfb92d14SAndroid Build Coastguard Worker    # search for index on address in the `addr_str` and ensure it is
153*cfb92d14SAndroid Build Coastguard Worker    # non-negative.
154*cfb92d14SAndroid Build Coastguard Worker    verify(addr_str.find(IP6_ADDR_3) >= 0)
155*cfb92d14SAndroid Build Coastguard Worker
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Worker# Check the addresses and prefixes (wait time 20 seconds)
158*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_addresses_and_prefixes, 15)
159*cfb92d14SAndroid Build Coastguard Worker
160*cfb92d14SAndroid Build Coastguard Worker# Reset the nodes and verify that all addresses/prefixes are preserved.
161*cfb92d14SAndroid Build Coastguard Workerfed1.reset()
162*cfb92d14SAndroid Build Coastguard Workersed2.reset()
163*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_addresses_and_prefixes, 20)
164*cfb92d14SAndroid Build Coastguard Worker
165*cfb92d14SAndroid Build Coastguard Worker# Remove address from `r2`
166*cfb92d14SAndroid Build Coastguard Workerr2.remove_ip6_address_on_interface(IP6_ADDR_1, prefix_len=64)
167*cfb92d14SAndroid Build Coastguard Worker
168*cfb92d14SAndroid Build Coastguard Worker
169*cfb92d14SAndroid Build Coastguard Workerdef check_address_prefix_removed():
170*cfb92d14SAndroid Build Coastguard Worker    # Verify that address is removed from r2
171*cfb92d14SAndroid Build Coastguard Worker    verify(r2.find_ip6_address_with_prefix(IP6_PREFIX_1) == '')
172*cfb92d14SAndroid Build Coastguard Worker    # Verify that the related prefix is also removed on all nodes
173*cfb92d14SAndroid Build Coastguard Worker    for node in all_nodes:
174*cfb92d14SAndroid Build Coastguard Worker        prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES))
175*cfb92d14SAndroid Build Coastguard Worker        for p in prefixes:
176*cfb92d14SAndroid Build Coastguard Worker            verify(p.prefix != IP6_PREFIX_1)
177*cfb92d14SAndroid Build Coastguard Worker
178*cfb92d14SAndroid Build Coastguard Worker
179*cfb92d14SAndroid Build Coastguard Worker# Check the addresses and prefixes (wait time 15 seconds)
180*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_address_prefix_removed, 15)
181*cfb92d14SAndroid Build Coastguard Worker
182*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
183*cfb92d14SAndroid Build Coastguard Worker# Test finished
184*cfb92d14SAndroid Build Coastguard Worker
185*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes()
186*cfb92d14SAndroid Build Coastguard Worker
187*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
188