xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-022-multicast-ip6-address.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: Multicast addresses on routers, FEDs, and SEDs
34*cfb92d14SAndroid Build Coastguard Worker#
35*cfb92d14SAndroid Build Coastguard Worker# Test topology:
36*cfb92d14SAndroid Build Coastguard Worker#
37*cfb92d14SAndroid Build Coastguard Worker#       router
38*cfb92d14SAndroid Build Coastguard Worker#         /\
39*cfb92d14SAndroid Build Coastguard Worker#        /  \
40*cfb92d14SAndroid Build Coastguard Worker#      fed  sed
41*cfb92d14SAndroid Build Coastguard Worker#
42*cfb92d14SAndroid Build Coastguard Worker# - Verify all nodes subscribe to:
43*cfb92d14SAndroid Build Coastguard Worker#   * Link-local all nodes, realm-local all nodes,
44*cfb92d14SAndroid Build Coastguard Worker#   * Link-local all Thread nodes, realm-local all Thread nodes,
45*cfb92d14SAndroid Build Coastguard Worker#   * All MPL forwarder realm-local.
46*cfb92d14SAndroid Build Coastguard Worker# - Verify routers also subscribe to:
47*cfb92d14SAndroid Build Coastguard Worker#   * Link-local all routers, realm-local all nodes.
48*cfb92d14SAndroid Build Coastguard Worker# - Verify adding/removing of multicast addresses on devices.
49*cfb92d14SAndroid Build Coastguard Worker#
50*cfb92d14SAndroid Build Coastguard Worker
51*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
52*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
53*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
54*cfb92d14SAndroid Build Coastguard Worker
55*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
56*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances
57*cfb92d14SAndroid Build Coastguard Worker
58*cfb92d14SAndroid Build Coastguard Workerspeedup = 4
59*cfb92d14SAndroid Build Coastguard Workerwpan.Node.set_time_speedup_factor(speedup)
60*cfb92d14SAndroid Build Coastguard Worker
61*cfb92d14SAndroid Build Coastguard Workerrouter = wpan.Node()
62*cfb92d14SAndroid Build Coastguard Workerfed = wpan.Node()
63*cfb92d14SAndroid Build Coastguard Workersed = wpan.Node()
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
66*cfb92d14SAndroid Build Coastguard Worker# Init all nodes
67*cfb92d14SAndroid Build Coastguard Worker
68*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes()
69*cfb92d14SAndroid Build Coastguard Worker
70*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
71*cfb92d14SAndroid Build Coastguard Worker# Utility functions
72*cfb92d14SAndroid Build Coastguard Worker
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Workerdef check_multicast_addresses(node, mcast_addr_list):
75*cfb92d14SAndroid Build Coastguard Worker    addrs = wpan.parse_list(node.get(wpan.WPAN_IP6_MULTICAST_ADDRESSES))
76*cfb92d14SAndroid Build Coastguard Worker    for addr in mcast_addr_list:
77*cfb92d14SAndroid Build Coastguard Worker        verify(addr in addrs)
78*cfb92d14SAndroid Build Coastguard Worker
79*cfb92d14SAndroid Build Coastguard Worker
80*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
81*cfb92d14SAndroid Build Coastguard Worker# Build network topology
82*cfb92d14SAndroid Build Coastguard Worker#
83*cfb92d14SAndroid Build Coastguard Worker
84*cfb92d14SAndroid Build Coastguard Workerrouter.form("mcast-addr")
85*cfb92d14SAndroid Build Coastguard Worker
86*cfb92d14SAndroid Build Coastguard Workerfed.join_node(router, wpan.JOIN_TYPE_END_DEVICE)
87*cfb92d14SAndroid Build Coastguard Workersed.join_node(router, wpan.JOIN_TYPE_SLEEPY_END_DEVICE)
88*cfb92d14SAndroid Build Coastguard Worker
89*cfb92d14SAndroid Build Coastguard Workersed.set(wpan.WPAN_POLL_INTERVAL, '800')
90*cfb92d14SAndroid Build Coastguard Worker
91*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
92*cfb92d14SAndroid Build Coastguard Worker# Test implementation
93*cfb92d14SAndroid Build Coastguard Worker
94*cfb92d14SAndroid Build Coastguard Worker# Get the mesh-local prefix (remove the "/64" at the end of the string)
95*cfb92d14SAndroid Build Coastguard Workerml_prefix = router.get(wpan.WPAN_IP6_MESH_LOCAL_PREFIX)[1:-1].split('/')[0]
96*cfb92d14SAndroid Build Coastguard Worker
97*cfb92d14SAndroid Build Coastguard Worker# Derive the link-local/realm-local all thread nodes multicast addresses
98*cfb92d14SAndroid Build Coastguard Workerll_all_thread_nodes_addr = 'ff32:40:' + ml_prefix + '1'
99*cfb92d14SAndroid Build Coastguard Workerrl_all_thread_nodes_addr = 'ff33:40:' + ml_prefix + '1'
100*cfb92d14SAndroid Build Coastguard Worker
101*cfb92d14SAndroid Build Coastguard Worker# List of multicast addresses subscribed by all nodes
102*cfb92d14SAndroid Build Coastguard Workermcast_addrs = [
103*cfb92d14SAndroid Build Coastguard Worker    "ff02::1",  # All nodes link-local
104*cfb92d14SAndroid Build Coastguard Worker    "ff03::1",  # All nodes realm-local
105*cfb92d14SAndroid Build Coastguard Worker    "ff03::fc",  # All MPL forwarder realm-local
106*cfb92d14SAndroid Build Coastguard Worker    ll_all_thread_nodes_addr,
107*cfb92d14SAndroid Build Coastguard Worker    rl_all_thread_nodes_addr,
108*cfb92d14SAndroid Build Coastguard Worker]
109*cfb92d14SAndroid Build Coastguard Worker
110*cfb92d14SAndroid Build Coastguard Worker# List of multicast addresses subscribed by routers only
111*cfb92d14SAndroid Build Coastguard Workerrouter_mcast_addrs = mcast_addrs + [
112*cfb92d14SAndroid Build Coastguard Worker    "ff02::2",  # All routers link-local
113*cfb92d14SAndroid Build Coastguard Worker    "ff03::2",  # All routers realm-local
114*cfb92d14SAndroid Build Coastguard Worker]
115*cfb92d14SAndroid Build Coastguard Worker
116*cfb92d14SAndroid Build Coastguard Workercheck_multicast_addresses(router, router_mcast_addrs)
117*cfb92d14SAndroid Build Coastguard Workercheck_multicast_addresses(fed, router_mcast_addrs)
118*cfb92d14SAndroid Build Coastguard Workercheck_multicast_addresses(sed, mcast_addrs)
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Worker# Add a multicast address
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard WorkerMCAST_ADDR = "ff02::114"
123*cfb92d14SAndroid Build Coastguard Worker
124*cfb92d14SAndroid Build Coastguard Workerfor node in [router, fed, sed]:
125*cfb92d14SAndroid Build Coastguard Worker    node.add(wpan.WPAN_IP6_MULTICAST_ADDRESSES, MCAST_ADDR)
126*cfb92d14SAndroid Build Coastguard Worker    addrs = wpan.parse_list(node.get(wpan.WPAN_IP6_MULTICAST_ADDRESSES))
127*cfb92d14SAndroid Build Coastguard Worker    verify(MCAST_ADDR in addrs)
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Worker    node.remove(wpan.WPAN_IP6_MULTICAST_ADDRESSES, MCAST_ADDR)
130*cfb92d14SAndroid Build Coastguard Worker    addrs = wpan.parse_list(node.get(wpan.WPAN_IP6_MULTICAST_ADDRESSES))
131*cfb92d14SAndroid Build Coastguard Worker    verify(MCAST_ADDR not in addrs)
132*cfb92d14SAndroid Build Coastguard Worker
133*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
134*cfb92d14SAndroid Build Coastguard Worker# Test finished
135*cfb92d14SAndroid Build Coastguard Worker
136*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes()
137*cfb92d14SAndroid Build Coastguard Worker
138*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
139