1#!/usr/bin/env python3
2#
3#  Copyright (c) 2019, 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:
35#
36# This test covers the situation for SED child (re)attaching to a parent with multiple IPv6 addresses present on the
37# child.
38#
39# Network topology
40#
41#   leader ---- parent
42#                 |
43#                 |
44#              child (sleepy)
45#
46
47test_name = __file__[:-3] if __file__.endswith('.py') else __file__
48print('-' * 120)
49print('Starting \'{}\''.format(test_name))
50
51# -----------------------------------------------------------------------------------------------------------------------
52# Utility functions
53
54
55def verify_address(node_list, prefix):
56    """
57    This function verifies that all nodes in the `node_list` contain an IPv6 address with the given `prefix`.
58    """
59    for node in node_list:
60        all_addrs = wpan.parse_list(node.get(wpan.WPAN_IP6_ALL_ADDRESSES))
61        verify(any([addr.startswith(prefix[:-1]) for addr in all_addrs]))
62
63
64# -----------------------------------------------------------------------------------------------------------------------
65# Creating `wpan.Nodes` instances
66
67speedup = 4
68wpan.Node.set_time_speedup_factor(speedup)
69
70leader = wpan.Node()
71parent = wpan.Node()
72child = wpan.Node()
73
74# -----------------------------------------------------------------------------------------------------------------------
75# Init all nodes
76
77wpan.Node.init_all_nodes()
78
79# -----------------------------------------------------------------------------------------------------------------------
80# Build network topology
81
82leader.form('multi-addr-test')
83
84leader.allowlist_node(parent)
85parent.allowlist_node(leader)
86parent.join_node(leader, wpan.JOIN_TYPE_ROUTER)
87
88parent.allowlist_node(child)
89child.allowlist_node(parent)
90
91child.join_node(parent, node_type=wpan.JOIN_TYPE_SLEEPY_END_DEVICE)
92child.set(wpan.WPAN_POLL_INTERVAL, '400')
93
94# -----------------------------------------------------------------------------------------------------------------------
95# Test implementation
96
97WAIT_TIME = 5
98CHILD_SUPERVISION_CHECK_TIMEOUT = 1
99
100prefix1 = 'fd00:1::'
101prefix2 = 'fd00:2::'
102prefix3 = 'fd00:3::'
103prefix4 = 'fd00:4::'
104
105# Add 4 prefixes (all with SLAAC bit set).
106leader.add_prefix(prefix1, on_mesh=True, slaac=True, configure=True)
107leader.add_prefix(prefix2, on_mesh=True, slaac=True, configure=True)
108leader.add_prefix(prefix3, on_mesh=True, slaac=True, configure=True)
109leader.add_prefix(prefix4, on_mesh=True, slaac=True, configure=True)
110
111# Verify that the sleepy child gets all 4 SLAAC addresses.
112
113
114def check_addresses_on_child():
115    verify_address([child], prefix1)
116    verify_address([child], prefix2)
117    verify_address([child], prefix3)
118    verify_address([child], prefix4)
119
120
121wpan.verify_within(check_addresses_on_child, WAIT_TIME)
122
123# Remove child from parent's allowlist
124parent.remove(wpan.WPAN_MAC_ALLOWLIST_ENTRIES, child.get(wpan.WPAN_EXT_ADDRESS)[1:-1])
125
126# Enable supervision check on child, this ensures that child is detached soon.
127child.set(
128    wpan.WPAN_CHILD_SUPERVISION_CHECK_TIMEOUT,
129    str(CHILD_SUPERVISION_CHECK_TIMEOUT),
130)
131
132# Wait for child to get detached.
133
134
135def check_child_is_detached():
136    verify(not child.is_associated())
137
138
139wpan.verify_within(check_child_is_detached, WAIT_TIME)
140
141# Now reset parent and wait for it to be associated.
142parent.reset()
143
144
145def check_parent_is_associated():
146    verify(parent.is_associated())
147
148
149wpan.verify_within(check_parent_is_associated, WAIT_TIME)
150
151# Now verify that child is indeed getting attached back.
152
153
154def check_child_is_associated():
155    verify(child.is_associated())
156
157
158wpan.verify_within(check_child_is_associated, WAIT_TIME)
159
160# Any finally check that we see all the child addresses in the parent's
161# child table.
162
163
164def check_child_addresses_on_parent():
165    child_addrs = parent.get(wpan.WPAN_THREAD_CHILD_TABLE_ADDRESSES)
166    verify(child_addrs.find(prefix1) > 0)
167    verify(child_addrs.find(prefix2) > 0)
168    verify(child_addrs.find(prefix3) > 0)
169    verify(child_addrs.find(prefix4) > 0)
170
171
172wpan.verify_within(check_child_addresses_on_parent, WAIT_TIME)
173
174# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
175# Check the child recovery after a parent reset using quick re-attach
176# ("Child Update" exchange).
177
178# Disable supervision check on the child.
179child.set(wpan.WPAN_CHILD_SUPERVISION_CHECK_TIMEOUT, '1000')
180child.set(wpan.WPAN_POLL_INTERVAL, '10000')
181time.sleep(0.1)
182
183# We use the "stat:ncp" wpantund property to verify that child does not
184# get detached.
185child_num_state_changes = len(wpan.parse_list(child.get("stat:ncp")))
186
187# Reset parent and wait for it to be associated.
188parent.reset()
189wpan.verify_within(check_parent_is_associated, WAIT_TIME)
190
191child.set(wpan.WPAN_POLL_INTERVAL, '100')
192
193# Verify that we again see all the child addresses in the parent's child table.
194# Note that child should register its addresses using "Child Update
195# Request" exchange.
196wpan.verify_within(check_child_addresses_on_parent, WAIT_TIME)
197
198# Verify that there was no state change on child.
199verify(child_num_state_changes == len(wpan.parse_list(child.get("stat:ncp"))))
200
201# -----------------------------------------------------------------------------------------------------------------------
202# Test finished
203
204wpan.Node.finalize_all_nodes()
205
206print('\'{}\' passed.'.format(test_name))
207