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: Adding/Removing IPv6 addresses on routers and SEDs on network interface. 34# 35# Test topology: 36# 37# r1 ---- r2 38# | | 39# | | 40# fed1 sed2 41# 42# IPv6 addresses are added as follows: 43# - On `r2` add `IP6_ADDR_1` with prefix `IP6_PREFIX_1` 44# - On `fed1` add `IP6_ADDR_2` with prefix `IP6_PREFIX_2` 45# - On `sed2` add `IP6_ADDR_3` with prefix `IP6_PREFIX_3` 46# 47# The test then covers the following: 48# - Verify that the addresses are present in "IPv6:AllAddresses" wpantund property on the corresponding node. 49# - Verify that all prefixes are present in network data with correct configuration flags (on all nodes). 50# - Verify that `sed2`'s address is present in `r2` (its parent) "Thread:ChildTable:Addresses". 51# - Verify that addresses/prefixes are retained by wpantund over NCP reset. 52# - Verify that when an IPv6 address is removed from network interface, its corresponding prefix is also removed from 53# all nodes. 54 55test_name = __file__[:-3] if __file__.endswith('.py') else __file__ 56print('-' * 120) 57print('Starting \'{}\''.format(test_name)) 58 59# ----------------------------------------------------------------------------------------------------------------------- 60# Creating `wpan.Nodes` instances 61 62speedup = 4 63wpan.Node.set_time_speedup_factor(speedup) 64 65r1 = wpan.Node() 66fed1 = wpan.Node() 67r2 = wpan.Node() 68sed2 = wpan.Node() 69 70all_nodes = [r1, fed1, r2, sed2] 71 72# ----------------------------------------------------------------------------------------------------------------------- 73# Init all nodes 74 75wpan.Node.init_all_nodes() 76 77# ----------------------------------------------------------------------------------------------------------------------- 78# Build network topology 79# 80# r1 ---- r2 81# | | 82# | | 83# fed1 sed2 84 85r1.allowlist_node(r2) 86r2.allowlist_node(r1) 87 88r1.allowlist_node(fed1) 89fed1.allowlist_node(r1) 90 91r2.allowlist_node(sed2) 92sed2.allowlist_node(r2) 93 94r1.form("ip-addr") 95r2.join_node(r1, wpan.JOIN_TYPE_ROUTER) 96 97fed1.join_node(r1, wpan.JOIN_TYPE_END_DEVICE) 98sed2.join_node(r2, wpan.JOIN_TYPE_SLEEPY_END_DEVICE) 99 100sed2.set(wpan.WPAN_POLL_INTERVAL, '300') 101 102# ----------------------------------------------------------------------------------------------------------------------- 103# Test implementation 104 105IP6_PREFIX_1 = "fd00:c0de::" 106IP6_PREFIX_2 = "fd00:deed::" 107IP6_PREFIX_3 = "fd00:beef::" 108 109IP6_ADDR_1 = IP6_PREFIX_1 + "1" 110IP6_ADDR_2 = IP6_PREFIX_2 + "2" 111IP6_ADDR_3 = IP6_PREFIX_3 + "3" 112 113# On `r2` add `IP6_ADDR_1` with prefix `IP6_PREFIX_1` 114# On `fed1` add `IP6_ADDR_2` with prefix `IP6_PREFIX_2` 115# On `sed2` add `IP6_ADDR_3` with prefix `IP6_PREFIX_3` 116 117r2.add_ip6_address_on_interface(IP6_ADDR_1, prefix_len=64) 118fed1.add_ip6_address_on_interface(IP6_ADDR_2, prefix_len=64) 119sed2.add_ip6_address_on_interface(IP6_ADDR_3, prefix_len=64) 120 121 122def check_addresses_and_prefixes(): 123 # Verify that the addresses are present in "IPv6:AllAddresses" wpantund 124 # property on the corresponding node. 125 verify(r2.find_ip6_address_with_prefix(IP6_PREFIX_1) == IP6_ADDR_1) 126 verify(fed1.find_ip6_address_with_prefix(IP6_PREFIX_2) == IP6_ADDR_2) 127 verify(sed2.find_ip6_address_with_prefix(IP6_PREFIX_3) == IP6_ADDR_3) 128 129 # Verify that all prefixes are present in network data on all nodes (with 130 # correct flags). 131 for prefix in [IP6_PREFIX_1, IP6_PREFIX_2, IP6_PREFIX_3]: 132 for node in all_nodes: 133 prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES)) 134 for p in prefixes: 135 if p.prefix == prefix: 136 verify(p.prefix_len == '64') 137 verify(p.is_stable()) 138 verify(p.is_on_mesh()) 139 verify(p.is_preferred()) 140 verify(p.is_def_route() is False) 141 verify(p.is_slaac() is False) 142 verify(p.is_dhcp() is False) 143 verify(p.is_config() is False) 144 verify(p.priority == "med") 145 break 146 else: # `for` loop finished without finding the prefix. 147 raise wpan.VerifyError('Did not find prefix {} on node {}'.format(prefix, node)) 148 149 # Verify that IPv6 address of `sed2` is present on `r2` (its parent) 150 # "Thread:ChildTable:Addresses". 151 addr_str = r2.get(wpan.WPAN_THREAD_CHILD_TABLE_ADDRESSES) 152 # search for index on address in the `addr_str` and ensure it is 153 # non-negative. 154 verify(addr_str.find(IP6_ADDR_3) >= 0) 155 156 157# Check the addresses and prefixes (wait time 20 seconds) 158wpan.verify_within(check_addresses_and_prefixes, 15) 159 160# Reset the nodes and verify that all addresses/prefixes are preserved. 161fed1.reset() 162sed2.reset() 163wpan.verify_within(check_addresses_and_prefixes, 20) 164 165# Remove address from `r2` 166r2.remove_ip6_address_on_interface(IP6_ADDR_1, prefix_len=64) 167 168 169def check_address_prefix_removed(): 170 # Verify that address is removed from r2 171 verify(r2.find_ip6_address_with_prefix(IP6_PREFIX_1) == '') 172 # Verify that the related prefix is also removed on all nodes 173 for node in all_nodes: 174 prefixes = wpan.parse_on_mesh_prefix_result(node.get(wpan.WPAN_THREAD_ON_MESH_PREFIXES)) 175 for p in prefixes: 176 verify(p.prefix != IP6_PREFIX_1) 177 178 179# Check the addresses and prefixes (wait time 15 seconds) 180wpan.verify_within(check_address_prefix_removed, 15) 181 182# ----------------------------------------------------------------------------------------------------------------------- 183# Test finished 184 185wpan.Node.finalize_all_nodes() 186 187print('\'{}\' passed.'.format(test_name)) 188