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