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: 34*cfb92d14SAndroid Build Coastguard Worker# 35*cfb92d14SAndroid Build Coastguard Worker# Adding on-mesh prefix and `config-gateway` command 36*cfb92d14SAndroid Build Coastguard Worker# 37*cfb92d14SAndroid Build Coastguard Worker# - Verifies adding an on-mesh prefix using `config-gateway`, `add-prefix` commands. 38*cfb92d14SAndroid Build Coastguard Worker# - Verifies prefixes with different flags/priorities added on routers and/or end-devices. 39*cfb92d14SAndroid Build Coastguard Worker# - Verifies `wpantund` adding SLAAC based IPv6 address based on prefix. 40*cfb92d14SAndroid Build Coastguard Worker# - Verifies `wpantund` retaining user-added prefixes and adding them back after an NCP reset. 41*cfb92d14SAndroid Build Coastguard Worker# 42*cfb92d14SAndroid Build Coastguard Worker 43*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__ 44*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120) 45*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name)) 46*cfb92d14SAndroid Build Coastguard Worker 47*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 48*cfb92d14SAndroid Build Coastguard Worker# Utility functions 49*cfb92d14SAndroid Build Coastguard Worker 50*cfb92d14SAndroid Build Coastguard Worker 51*cfb92d14SAndroid Build Coastguard Workerdef verify_address(node_list, prefix): 52*cfb92d14SAndroid Build Coastguard Worker """ 53*cfb92d14SAndroid Build Coastguard Worker This function verifies that all nodes in the `node_list` contain an IPv6 address with the given `prefix`. 54*cfb92d14SAndroid Build Coastguard Worker """ 55*cfb92d14SAndroid Build Coastguard Worker for node in node_list: 56*cfb92d14SAndroid Build Coastguard Worker all_addrs = wpan.parse_list(node.get(wpan.WPAN_IP6_ALL_ADDRESSES)) 57*cfb92d14SAndroid Build Coastguard Worker verify(any([addr.startswith(prefix[:-1]) for addr in all_addrs])) 58*cfb92d14SAndroid Build Coastguard Worker 59*cfb92d14SAndroid Build Coastguard Worker 60*cfb92d14SAndroid Build Coastguard Workerdef verify_prefix( 61*cfb92d14SAndroid Build Coastguard Worker node_list, 62*cfb92d14SAndroid Build Coastguard Worker prefix, 63*cfb92d14SAndroid Build Coastguard Worker prefix_len=64, 64*cfb92d14SAndroid Build Coastguard Worker stable=True, 65*cfb92d14SAndroid Build Coastguard Worker priority='med', 66*cfb92d14SAndroid Build Coastguard Worker on_mesh=False, 67*cfb92d14SAndroid Build Coastguard Worker slaac=False, 68*cfb92d14SAndroid Build Coastguard Worker dhcp=False, 69*cfb92d14SAndroid Build Coastguard Worker configure=False, 70*cfb92d14SAndroid Build Coastguard Worker default_route=False, 71*cfb92d14SAndroid Build Coastguard Worker preferred=True, 72*cfb92d14SAndroid Build Coastguard Worker): 73*cfb92d14SAndroid Build Coastguard Worker """ 74*cfb92d14SAndroid Build Coastguard Worker This function verifies that the `prefix` is present on all the nodes in the `node_list`. 75*cfb92d14SAndroid Build Coastguard Worker """ 76*cfb92d14SAndroid Build Coastguard Worker for node in node_list: 77*cfb92d14SAndroid Build Coastguard Worker prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES)) 78*cfb92d14SAndroid Build Coastguard Worker for p in prefixes: 79*cfb92d14SAndroid Build Coastguard Worker if p.prefix == prefix: 80*cfb92d14SAndroid Build Coastguard Worker verify(int(p.prefix_len) == prefix_len) 81*cfb92d14SAndroid Build Coastguard Worker verify(p.is_stable() == stable) 82*cfb92d14SAndroid Build Coastguard Worker verify(p.is_on_mesh() == on_mesh) 83*cfb92d14SAndroid Build Coastguard Worker verify(p.is_def_route() == default_route) 84*cfb92d14SAndroid Build Coastguard Worker verify(p.is_slaac() == slaac) 85*cfb92d14SAndroid Build Coastguard Worker verify(p.is_dhcp() == dhcp) 86*cfb92d14SAndroid Build Coastguard Worker verify(p.is_config() == configure) 87*cfb92d14SAndroid Build Coastguard Worker verify(p.is_preferred() == preferred) 88*cfb92d14SAndroid Build Coastguard Worker verify(p.priority == priority) 89*cfb92d14SAndroid Build Coastguard Worker break 90*cfb92d14SAndroid Build Coastguard Worker else: 91*cfb92d14SAndroid Build Coastguard Worker raise wpan.VerifyError('Did not find prefix {} on node {}'.format(prefix, node)) 92*cfb92d14SAndroid Build Coastguard Worker 93*cfb92d14SAndroid Build Coastguard Worker 94*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 95*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances 96*cfb92d14SAndroid Build Coastguard Worker 97*cfb92d14SAndroid Build Coastguard Workerspeedup = 4 98*cfb92d14SAndroid Build Coastguard Workerwpan.Node.set_time_speedup_factor(speedup) 99*cfb92d14SAndroid Build Coastguard Worker 100*cfb92d14SAndroid Build Coastguard Workerr1 = wpan.Node() 101*cfb92d14SAndroid Build Coastguard Workerr2 = wpan.Node() 102*cfb92d14SAndroid Build Coastguard Workersc1 = wpan.Node() 103*cfb92d14SAndroid Build Coastguard Workersc2 = wpan.Node() 104*cfb92d14SAndroid Build Coastguard Worker 105*cfb92d14SAndroid Build Coastguard Workerall_nodes = [r1, r2, sc1, sc2] 106*cfb92d14SAndroid Build Coastguard Worker 107*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 108*cfb92d14SAndroid Build Coastguard Worker# Init all nodes 109*cfb92d14SAndroid Build Coastguard Worker 110*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes() 111*cfb92d14SAndroid Build Coastguard Worker 112*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 113*cfb92d14SAndroid Build Coastguard Worker# Build network topology 114*cfb92d14SAndroid Build Coastguard Worker 115*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2) 116*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1) 117*cfb92d14SAndroid Build Coastguard Worker 118*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(sc1) 119*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(sc2) 120*cfb92d14SAndroid Build Coastguard Worker 121*cfb92d14SAndroid Build Coastguard Workerr1.form('config-gtway') 122*cfb92d14SAndroid Build Coastguard Workerr2.join_node(r1, node_type=wpan.JOIN_TYPE_ROUTER) 123*cfb92d14SAndroid Build Coastguard Workersc1.join_node(r1, node_type=wpan.JOIN_TYPE_SLEEPY_END_DEVICE) 124*cfb92d14SAndroid Build Coastguard Workersc2.join_node(r2, node_type=wpan.JOIN_TYPE_SLEEPY_END_DEVICE) 125*cfb92d14SAndroid Build Coastguard Worker 126*cfb92d14SAndroid Build Coastguard Workersc1.set(wpan.WPAN_POLL_INTERVAL, '200') 127*cfb92d14SAndroid Build Coastguard Workersc2.set(wpan.WPAN_POLL_INTERVAL, '200') 128*cfb92d14SAndroid Build Coastguard Worker 129*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 130*cfb92d14SAndroid Build Coastguard Worker# Test implementation 131*cfb92d14SAndroid Build Coastguard Worker 132*cfb92d14SAndroid Build Coastguard WorkerWAIT_TIME = 5 133*cfb92d14SAndroid Build Coastguard Worker 134*cfb92d14SAndroid Build Coastguard Workerprefix1 = 'fd00:abba:cafe::' 135*cfb92d14SAndroid Build Coastguard Workerprefix2 = 'fd00:1234::' 136*cfb92d14SAndroid Build Coastguard Workerprefix3 = 'fd00:deed::' 137*cfb92d14SAndroid Build Coastguard Workerprefix4 = 'fd00:abcd::' 138*cfb92d14SAndroid Build Coastguard Worker 139*cfb92d14SAndroid Build Coastguard Worker# Add on-mesh prefix1 on router r1 140*cfb92d14SAndroid Build Coastguard Workerr1.config_gateway(prefix1) 141*cfb92d14SAndroid Build Coastguard Worker 142*cfb92d14SAndroid Build Coastguard Worker# Verify that the prefix1 and its corresponding address are present on all 143*cfb92d14SAndroid Build Coastguard Worker# nodes 144*cfb92d14SAndroid Build Coastguard Worker 145*cfb92d14SAndroid Build Coastguard Worker 146*cfb92d14SAndroid Build Coastguard Workerdef check_prefix1_on_all_nodes(): 147*cfb92d14SAndroid Build Coastguard Worker verify_prefix(all_nodes, prefix1, stable=True, on_mesh=True, slaac=True) 148*cfb92d14SAndroid Build Coastguard Worker verify_address(all_nodes, prefix1) 149*cfb92d14SAndroid Build Coastguard Worker 150*cfb92d14SAndroid Build Coastguard Worker 151*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix1_on_all_nodes, WAIT_TIME) 152*cfb92d14SAndroid Build Coastguard Worker 153*cfb92d14SAndroid Build Coastguard Worker# Now add prefix2 with priority `high` on router r2 and check all nodes 154*cfb92d14SAndroid Build Coastguard Worker# for the new prefix/address 155*cfb92d14SAndroid Build Coastguard Workerr2.config_gateway(prefix2, default_route=True, priority='1') 156*cfb92d14SAndroid Build Coastguard Worker 157*cfb92d14SAndroid Build Coastguard Worker 158*cfb92d14SAndroid Build Coastguard Workerdef check_prefix2_on_all_nodes(): 159*cfb92d14SAndroid Build Coastguard Worker verify_prefix( 160*cfb92d14SAndroid Build Coastguard Worker all_nodes, 161*cfb92d14SAndroid Build Coastguard Worker prefix2, 162*cfb92d14SAndroid Build Coastguard Worker stable=True, 163*cfb92d14SAndroid Build Coastguard Worker on_mesh=True, 164*cfb92d14SAndroid Build Coastguard Worker slaac=True, 165*cfb92d14SAndroid Build Coastguard Worker default_route=True, 166*cfb92d14SAndroid Build Coastguard Worker priority='high', 167*cfb92d14SAndroid Build Coastguard Worker ) 168*cfb92d14SAndroid Build Coastguard Worker verify_address(all_nodes, prefix2) 169*cfb92d14SAndroid Build Coastguard Worker 170*cfb92d14SAndroid Build Coastguard Worker 171*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix2_on_all_nodes, WAIT_TIME) 172*cfb92d14SAndroid Build Coastguard Worker 173*cfb92d14SAndroid Build Coastguard Worker# Add prefix3 on sleepy end-device and check for it on all nodes 174*cfb92d14SAndroid Build Coastguard Workersc1.config_gateway(prefix3, priority='-1') 175*cfb92d14SAndroid Build Coastguard Worker 176*cfb92d14SAndroid Build Coastguard Worker 177*cfb92d14SAndroid Build Coastguard Workerdef check_prefix3_on_all_nodes(): 178*cfb92d14SAndroid Build Coastguard Worker verify_prefix( 179*cfb92d14SAndroid Build Coastguard Worker all_nodes, 180*cfb92d14SAndroid Build Coastguard Worker prefix3, 181*cfb92d14SAndroid Build Coastguard Worker stable=True, 182*cfb92d14SAndroid Build Coastguard Worker on_mesh=True, 183*cfb92d14SAndroid Build Coastguard Worker slaac=True, 184*cfb92d14SAndroid Build Coastguard Worker priority='low', 185*cfb92d14SAndroid Build Coastguard Worker ) 186*cfb92d14SAndroid Build Coastguard Worker verify_address(all_nodes, prefix3) 187*cfb92d14SAndroid Build Coastguard Worker 188*cfb92d14SAndroid Build Coastguard Worker 189*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix3_on_all_nodes, WAIT_TIME) 190*cfb92d14SAndroid Build Coastguard Worker 191*cfb92d14SAndroid Build Coastguard Worker# Verify that prefix1 is retained by `wpantund` and pushed to NCP after a reset 192*cfb92d14SAndroid Build Coastguard Workerr1.reset() 193*cfb92d14SAndroid Build Coastguard Worker 194*cfb92d14SAndroid Build Coastguard Worker 195*cfb92d14SAndroid Build Coastguard Workerdef check_r1_is_associated(): 196*cfb92d14SAndroid Build Coastguard Worker verify(r1.is_associated()) 197*cfb92d14SAndroid Build Coastguard Worker 198*cfb92d14SAndroid Build Coastguard Worker 199*cfb92d14SAndroid Build Coastguard Worker# Wait for r1 to recover after reset 200*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_r1_is_associated, WAIT_TIME) 201*cfb92d14SAndroid Build Coastguard Worker 202*cfb92d14SAndroid Build Coastguard Worker# Wait for on-mesh prefix to be updated 203*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix1_on_all_nodes, WAIT_TIME) 204*cfb92d14SAndroid Build Coastguard Worker 205*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 206*cfb92d14SAndroid Build Coastguard Worker# Test `add-prefix` and `remove-prefix` 207*cfb92d14SAndroid Build Coastguard Worker 208*cfb92d14SAndroid Build Coastguard Workerr1.add_prefix( 209*cfb92d14SAndroid Build Coastguard Worker prefix4, 210*cfb92d14SAndroid Build Coastguard Worker 48, 211*cfb92d14SAndroid Build Coastguard Worker priority="1", 212*cfb92d14SAndroid Build Coastguard Worker stable=False, 213*cfb92d14SAndroid Build Coastguard Worker on_mesh=True, 214*cfb92d14SAndroid Build Coastguard Worker slaac=False, 215*cfb92d14SAndroid Build Coastguard Worker dhcp=True, 216*cfb92d14SAndroid Build Coastguard Worker configure=False, 217*cfb92d14SAndroid Build Coastguard Worker default_route=True, 218*cfb92d14SAndroid Build Coastguard Worker preferred=False, 219*cfb92d14SAndroid Build Coastguard Worker) 220*cfb92d14SAndroid Build Coastguard Worker 221*cfb92d14SAndroid Build Coastguard Worker 222*cfb92d14SAndroid Build Coastguard Workerdef check_prefix4_on_r1(): 223*cfb92d14SAndroid Build Coastguard Worker verify_prefix( 224*cfb92d14SAndroid Build Coastguard Worker all_nodes, 225*cfb92d14SAndroid Build Coastguard Worker prefix4, 226*cfb92d14SAndroid Build Coastguard Worker 48, 227*cfb92d14SAndroid Build Coastguard Worker priority="high", 228*cfb92d14SAndroid Build Coastguard Worker stable=False, 229*cfb92d14SAndroid Build Coastguard Worker on_mesh=True, 230*cfb92d14SAndroid Build Coastguard Worker slaac=False, 231*cfb92d14SAndroid Build Coastguard Worker dhcp=True, 232*cfb92d14SAndroid Build Coastguard Worker configure=False, 233*cfb92d14SAndroid Build Coastguard Worker default_route=True, 234*cfb92d14SAndroid Build Coastguard Worker preferred=False, 235*cfb92d14SAndroid Build Coastguard Worker ) 236*cfb92d14SAndroid Build Coastguard Worker 237*cfb92d14SAndroid Build Coastguard Worker 238*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix4_on_r1, WAIT_TIME) 239*cfb92d14SAndroid Build Coastguard Worker 240*cfb92d14SAndroid Build Coastguard Worker# Remove prefix and verify that it is removed from list 241*cfb92d14SAndroid Build Coastguard Workerr1.remove_prefix(prefix4, 48) 242*cfb92d14SAndroid Build Coastguard Worker 243*cfb92d14SAndroid Build Coastguard Worker 244*cfb92d14SAndroid Build Coastguard Workerdef check_prefix4_removed_from_r1(): 245*cfb92d14SAndroid Build Coastguard Worker verify(r1.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES).find(prefix4) < 0) 246*cfb92d14SAndroid Build Coastguard Worker 247*cfb92d14SAndroid Build Coastguard Worker 248*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_prefix4_removed_from_r1, WAIT_TIME) 249*cfb92d14SAndroid Build Coastguard Worker 250*cfb92d14SAndroid Build Coastguard Workerr1.add_prefix( 251*cfb92d14SAndroid Build Coastguard Worker prefix4, 252*cfb92d14SAndroid Build Coastguard Worker 64, 253*cfb92d14SAndroid Build Coastguard Worker priority="-1", 254*cfb92d14SAndroid Build Coastguard Worker stable=True, 255*cfb92d14SAndroid Build Coastguard Worker on_mesh=False, 256*cfb92d14SAndroid Build Coastguard Worker slaac=True, 257*cfb92d14SAndroid Build Coastguard Worker dhcp=False, 258*cfb92d14SAndroid Build Coastguard Worker configure=True, 259*cfb92d14SAndroid Build Coastguard Worker default_route=False, 260*cfb92d14SAndroid Build Coastguard Worker preferred=True, 261*cfb92d14SAndroid Build Coastguard Worker) 262*cfb92d14SAndroid Build Coastguard Workerverify_prefix( 263*cfb92d14SAndroid Build Coastguard Worker [r1], 264*cfb92d14SAndroid Build Coastguard Worker prefix4, 265*cfb92d14SAndroid Build Coastguard Worker 64, 266*cfb92d14SAndroid Build Coastguard Worker priority="low", 267*cfb92d14SAndroid Build Coastguard Worker stable=True, 268*cfb92d14SAndroid Build Coastguard Worker on_mesh=False, 269*cfb92d14SAndroid Build Coastguard Worker slaac=True, 270*cfb92d14SAndroid Build Coastguard Worker dhcp=False, 271*cfb92d14SAndroid Build Coastguard Worker configure=True, 272*cfb92d14SAndroid Build Coastguard Worker default_route=False, 273*cfb92d14SAndroid Build Coastguard Worker preferred=True, 274*cfb92d14SAndroid Build Coastguard Worker) 275*cfb92d14SAndroid Build Coastguard Worker 276*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 277*cfb92d14SAndroid Build Coastguard Worker# Test finished 278*cfb92d14SAndroid Build Coastguard Worker 279*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes() 280*cfb92d14SAndroid Build Coastguard Worker 281*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name)) 282