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