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