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 time
30import wpan
31from wpan import verify
32
33# -----------------------------------------------------------------------------------------------------------------------
34# Test description: SLAAC address
35#
36# This test covers the addition/removal of SLAAC IPv6 address by `wpantund`.
37#
38
39test_name = __file__[:-3] if __file__.endswith('.py') else __file__
40print('-' * 120)
41print('Starting \'{}\''.format(test_name))
42
43# -----------------------------------------------------------------------------------------------------------------------
44# Utility functions
45
46
47def verify_address(node_list, prefix):
48    """
49    This function verifies that all nodes in the `node_list` contain an IPv6 address with the given `prefix`.
50    """
51    for node in node_list:
52        all_addrs = wpan.parse_list(node.get(wpan.WPAN_IP6_ALL_ADDRESSES))
53        verify(any([addr.startswith(prefix[:-1]) for addr in all_addrs]))
54
55
56def verify_no_address(node_list, prefix):
57    """
58    This function verifies that none of nodes in the `node_list` contain an IPv6 address with the given `prefix`.
59    """
60    for node in node_list:
61        all_addrs = wpan.parse_list(node.get(wpan.WPAN_IP6_ALL_ADDRESSES))
62        verify(all([not addr.startswith(prefix[:-1]) for addr in all_addrs]))
63
64
65def verify_prefix(
66    node_list,
67    prefix,
68    prefix_len=64,
69    stable=True,
70    priority='med',
71    on_mesh=False,
72    slaac=False,
73    dhcp=False,
74    configure=False,
75    default_route=False,
76    preferred=False,
77):
78    """
79    This function verifies that the `prefix` is present on all nodes in the `node_list`.
80    """
81    for node in node_list:
82        prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES))
83        for p in prefixes:
84            if p.prefix == prefix:
85                if (int(p.prefix_len) == prefix_len and p.is_stable() == stable and p.is_on_mesh() == on_mesh and
86                        p.is_def_route() == default_route and p.is_slaac() == slaac and p.is_dhcp() == dhcp and
87                        p.is_config() == configure and p.is_preferred() == preferred and p.priority == priority):
88                    break
89        else:
90            raise wpan.VerifyError("Did not find prefix {} on node {}".format(prefix, node))
91
92
93def verify_no_prefix(node_list, prefix):
94    """
95    This function verifies that the `prefix` is NOT present on any node in the `node_list`.
96    """
97    for node in node_list:
98        prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES))
99        for p in prefixes:
100            verify(not p.prefix == prefix)
101
102
103# -----------------------------------------------------------------------------------------------------------------------
104# Creating `wpan.Nodes` instances
105
106speedup = 4
107wpan.Node.set_time_speedup_factor(speedup)
108
109r1 = wpan.Node()
110r2 = wpan.Node()
111c2 = wpan.Node()
112
113all_nodes = [r1, r2, c2]
114
115# -----------------------------------------------------------------------------------------------------------------------
116# Init all nodes
117
118wpan.Node.init_all_nodes()
119
120# -----------------------------------------------------------------------------------------------------------------------
121# Build network topology
122
123r1.form('slaac-test')
124
125r1.allowlist_node(r2)
126r2.allowlist_node(r1)
127
128r2.join_node(r1, node_type=wpan.JOIN_TYPE_ROUTER)
129
130c2.allowlist_node(r2)
131r2.allowlist_node(c2)
132
133c2.join_node(r2, node_type=wpan.JOIN_TYPE_END_DEVICE)
134
135# -----------------------------------------------------------------------------------------------------------------------
136# Test implementation
137
138# This test covers the SLAAC address management by `wpantund`. So before starting the test we ensure that SLAAC module
139# on NCP is disabled on all nodes
140
141for node in all_nodes:
142    node.set(wpan.WPAN_OT_SLAAC_ENABLED, 'false')
143    verify(node.get(wpan.WPAN_OT_SLAAC_ENABLED) == 'false')
144
145WAIT_INTERVAL = 5
146
147PREFIX = 'fd00:abba:beef:cafe::'
148
149r1.add_prefix(PREFIX, stable=True, on_mesh=True, slaac=True)
150
151# Verify that all nodes get the prefix and add the SLAAC address
152
153
154def check_prefix_and_slaac_address_are_added():
155    verify_prefix(all_nodes, PREFIX, stable=True, on_mesh=True, slaac=True)
156    verify_address(all_nodes, PREFIX)
157
158
159wpan.verify_within(check_prefix_and_slaac_address_are_added, WAIT_INTERVAL)
160
161# Reset r1 and check that prefix and SLAAC address are re-added
162r1.reset()
163wpan.verify_within(check_prefix_and_slaac_address_are_added, WAIT_INTERVAL)
164
165# Remove the prefix on r1 and verify that the address and prefix are
166# removed on all nodes.
167r1.remove_prefix(PREFIX)
168
169
170def check_prefix_and_slaac_address_are_removed():
171    verify_no_prefix(all_nodes, PREFIX)
172    verify_no_address(all_nodes, PREFIX)
173
174
175wpan.verify_within(check_prefix_and_slaac_address_are_removed, WAIT_INTERVAL)
176
177# Add prefix on r2
178r2.add_prefix(PREFIX, stable=True, on_mesh=True, slaac=True)
179wpan.verify_within(check_prefix_and_slaac_address_are_added, WAIT_INTERVAL)
180
181# Add same prefix on r1 and verify prefix and addresses stay as before
182r1.add_prefix(PREFIX, stable=True, on_mesh=True, slaac=True)
183wpan.verify_within(check_prefix_and_slaac_address_are_added, WAIT_INTERVAL)
184
185# Remove on r1, addresses and prefixes should stay as before (r2 still has
186# the same prefix)
187r1.remove_prefix(PREFIX)
188time.sleep(0.5)
189wpan.verify_within(check_prefix_and_slaac_address_are_added, WAIT_INTERVAL)
190
191# Remove the prefix on r2 and verify that the address and prefix are now
192# removed on all nodes.
193r2.remove_prefix(PREFIX)
194wpan.verify_within(check_prefix_and_slaac_address_are_removed, WAIT_INTERVAL)
195
196# Add prefix on r1 without SLAAC flag, and or r2 with SLAAC flag
197r1.add_prefix(PREFIX, stable=True, on_mesh=True, slaac=False)
198r2.add_prefix(PREFIX, stable=True, on_mesh=True, slaac=True)
199wpan.verify_within(check_prefix_and_slaac_address_are_added, WAIT_INTERVAL)
200
201# Now remove the prefix on r2 and verify that SLAAC address is removed
202r2.remove_prefix(PREFIX)
203
204
205def check_slaac_address_is_removed():
206    verify_no_address(all_nodes, PREFIX)
207
208
209wpan.verify_within(check_slaac_address_is_removed, WAIT_INTERVAL)
210
211r1.remove_prefix(PREFIX)
212wpan.verify_within(check_prefix_and_slaac_address_are_removed, WAIT_INTERVAL)
213
214# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
215
216IP_ADDRESS = PREFIX + "1234"
217
218# Explicitly add an address with the prefix on r1
219r1.add_ip6_address_on_interface(IP_ADDRESS)
220
221# Afterwards, add the prefix on r2 (with SLAAC flag)
222r2.add_prefix(PREFIX, stable=True, on_mesh=True, slaac=True)
223wpan.verify_within(check_prefix_and_slaac_address_are_added, WAIT_INTERVAL)
224
225# Verify that on r1 we do see the user-added address
226r1_addrs = wpan.parse_list(r1.get(wpan.WPAN_IP6_ALL_ADDRESSES))
227verify(IP_ADDRESS in r1_addrs)
228
229# Also verify that adding the prefix did not add a SLAAC address for same
230# prefix on r1
231r1_addrs.remove(IP_ADDRESS)
232verify(all([not addr.startswith(PREFIX[:-1]) for addr in r1_addrs]))
233
234# Remove the PREFIX on r2
235r2.remove_prefix(PREFIX)
236
237
238def check_ip6_addresses():
239    # Verify that SLAAC addresses are removed on r2 and c2
240    verify_no_address([r2, c2], PREFIX)
241    # And that user-added address matching the preifx is not removed on r1
242    r1_addrs = wpan.parse_list(r1.get(wpan.WPAN_IP6_ALL_ADDRESSES))
243    verify(IP_ADDRESS in r1_addrs)
244
245
246wpan.verify_within(check_ip6_addresses, WAIT_INTERVAL)
247
248# Send from r2 to r1 using the user-added address verifying that address
249# is present on NCP
250IP_ADDRESS_2 = PREFIX + "2"
251r2.add_ip6_address_on_interface(IP_ADDRESS_2)
252sender = r2.prepare_tx(IP_ADDRESS_2, IP_ADDRESS, "Hello r1 from r2")
253recver = r1.prepare_rx(sender)
254wpan.Node.perform_async_tx_rx()
255verify(sender.was_successful)
256verify(recver.was_successful)
257
258# -----------------------------------------------------------------------------------------------------------------------
259# Test finished
260
261wpan.Node.finalize_all_nodes()
262
263print('\'{}\' passed.'.format(test_name))
264