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: Router table 34*cfb92d14SAndroid Build Coastguard Worker# 35*cfb92d14SAndroid Build Coastguard Worker# Verify router table entries on a network with 4 routers: 36*cfb92d14SAndroid Build Coastguard Worker# {r1, r2, r3} forming a loop with r4 connecting to r3. 37*cfb92d14SAndroid Build Coastguard Worker# 38*cfb92d14SAndroid Build Coastguard Worker 39*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__ 40*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120) 41*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name)) 42*cfb92d14SAndroid Build Coastguard Worker 43*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 44*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances 45*cfb92d14SAndroid Build Coastguard Worker 46*cfb92d14SAndroid Build Coastguard Workerspeedup = 4 47*cfb92d14SAndroid Build Coastguard Workerwpan.Node.set_time_speedup_factor(speedup) 48*cfb92d14SAndroid Build Coastguard Worker 49*cfb92d14SAndroid Build Coastguard Workerr1 = wpan.Node() 50*cfb92d14SAndroid Build Coastguard Workerr2 = wpan.Node() 51*cfb92d14SAndroid Build Coastguard Workerr3 = wpan.Node() 52*cfb92d14SAndroid Build Coastguard Workerr4 = wpan.Node() 53*cfb92d14SAndroid Build Coastguard Workerc4 = wpan.Node() 54*cfb92d14SAndroid Build Coastguard Worker 55*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 56*cfb92d14SAndroid Build Coastguard Worker# Init all nodes 57*cfb92d14SAndroid Build Coastguard Worker 58*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes() 59*cfb92d14SAndroid Build Coastguard Worker 60*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 61*cfb92d14SAndroid Build Coastguard Worker# Build network topology 62*cfb92d14SAndroid Build Coastguard Worker# 63*cfb92d14SAndroid Build Coastguard Worker# 64*cfb92d14SAndroid Build Coastguard Worker# r1 ------ r2 65*cfb92d14SAndroid Build Coastguard Worker# \ / 66*cfb92d14SAndroid Build Coastguard Worker# \ / 67*cfb92d14SAndroid Build Coastguard Worker# \ / 68*cfb92d14SAndroid Build Coastguard Worker# r3 _____ r4 69*cfb92d14SAndroid Build Coastguard Worker# 70*cfb92d14SAndroid Build Coastguard Worker# 71*cfb92d14SAndroid Build Coastguard Worker 72*cfb92d14SAndroid Build Coastguard Workerr1.form("route-table") 73*cfb92d14SAndroid Build Coastguard Worker 74*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2) 75*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1) 76*cfb92d14SAndroid Build Coastguard Workerr2.join_node(r1, wpan.JOIN_TYPE_ROUTER) 77*cfb92d14SAndroid Build Coastguard Worker 78*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r3) 79*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r2) 80*cfb92d14SAndroid Build Coastguard Workerr3.join_node(r2, wpan.JOIN_TYPE_ROUTER) 81*cfb92d14SAndroid Build Coastguard Worker 82*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r1) 83*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r3) 84*cfb92d14SAndroid Build Coastguard Worker 85*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r4) 86*cfb92d14SAndroid Build Coastguard Workerr4.allowlist_node(r3) 87*cfb92d14SAndroid Build Coastguard Workerr4.join_node(r3, wpan.JOIN_TYPE_ROUTER) 88*cfb92d14SAndroid Build Coastguard Worker 89*cfb92d14SAndroid Build Coastguard Worker# c4 is attached to r4 so that it quickly gets promoted to a router role. 90*cfb92d14SAndroid Build Coastguard Workerc4.allowlist_node(r4) 91*cfb92d14SAndroid Build Coastguard Workerr4.allowlist_node(c4) 92*cfb92d14SAndroid Build Coastguard Workerc4.join_node(r4, wpan.JOIN_TYPE_SLEEPY_END_DEVICE) 93*cfb92d14SAndroid Build Coastguard Workerc4.set(wpan.WPAN_POLL_INTERVAL, '2000') 94*cfb92d14SAndroid Build Coastguard Worker 95*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 96*cfb92d14SAndroid Build Coastguard Worker# Test implementation 97*cfb92d14SAndroid Build Coastguard Worker# 98*cfb92d14SAndroid Build Coastguard Worker 99*cfb92d14SAndroid Build Coastguard Workerverify(r1.get(wpan.WPAN_NODE_TYPE) == wpan.NODE_TYPE_LEADER) 100*cfb92d14SAndroid Build Coastguard Workerverify(r2.get(wpan.WPAN_NODE_TYPE) == wpan.NODE_TYPE_ROUTER) 101*cfb92d14SAndroid Build Coastguard Workerverify(r3.get(wpan.WPAN_NODE_TYPE) == wpan.NODE_TYPE_ROUTER) 102*cfb92d14SAndroid Build Coastguard Workerverify(r4.get(wpan.WPAN_NODE_TYPE) == wpan.NODE_TYPE_ROUTER) 103*cfb92d14SAndroid Build Coastguard Worker 104*cfb92d14SAndroid Build Coastguard Workerr1_id = int(r1.get(wpan.WPAN_THREAD_ROUTER_ID), 0) 105*cfb92d14SAndroid Build Coastguard Workerr2_id = int(r2.get(wpan.WPAN_THREAD_ROUTER_ID), 0) 106*cfb92d14SAndroid Build Coastguard Workerr3_id = int(r3.get(wpan.WPAN_THREAD_ROUTER_ID), 0) 107*cfb92d14SAndroid Build Coastguard Workerr4_id = int(r4.get(wpan.WPAN_THREAD_ROUTER_ID), 0) 108*cfb92d14SAndroid Build Coastguard Worker 109*cfb92d14SAndroid Build Coastguard Workerr1_ext_addr = r1.get(wpan.WPAN_EXT_ADDRESS)[1:-1] 110*cfb92d14SAndroid Build Coastguard Workerr2_ext_addr = r2.get(wpan.WPAN_EXT_ADDRESS)[1:-1] 111*cfb92d14SAndroid Build Coastguard Workerr3_ext_addr = r3.get(wpan.WPAN_EXT_ADDRESS)[1:-1] 112*cfb92d14SAndroid Build Coastguard Workerr4_ext_addr = r4.get(wpan.WPAN_EXT_ADDRESS)[1:-1] 113*cfb92d14SAndroid Build Coastguard Worker 114*cfb92d14SAndroid Build Coastguard Workerr1_rloc = int(r1.get(wpan.WPAN_THREAD_RLOC16), 16) 115*cfb92d14SAndroid Build Coastguard Workerr2_rloc = int(r2.get(wpan.WPAN_THREAD_RLOC16), 16) 116*cfb92d14SAndroid Build Coastguard Workerr3_rloc = int(r3.get(wpan.WPAN_THREAD_RLOC16), 16) 117*cfb92d14SAndroid Build Coastguard Workerr4_rloc = int(r4.get(wpan.WPAN_THREAD_RLOC16), 16) 118*cfb92d14SAndroid Build Coastguard Worker 119*cfb92d14SAndroid Build Coastguard WorkerWAIT_TIME = 30 / speedup + 5 120*cfb92d14SAndroid Build Coastguard Worker 121*cfb92d14SAndroid Build Coastguard WorkerINVALID_ROUTER_ID = 63 122*cfb92d14SAndroid Build Coastguard Worker 123*cfb92d14SAndroid Build Coastguard Worker 124*cfb92d14SAndroid Build Coastguard Workerdef check_r1_router_table(): 125*cfb92d14SAndroid Build Coastguard Worker router_table = wpan.parse_router_table_result(r1.get(wpan.WPAN_THREAD_ROUTER_TABLE)) 126*cfb92d14SAndroid Build Coastguard Worker verify(len(router_table) == 4) 127*cfb92d14SAndroid Build Coastguard Worker for entry in router_table: 128*cfb92d14SAndroid Build Coastguard Worker if entry.rloc16 == r1_rloc: 129*cfb92d14SAndroid Build Coastguard Worker pass 130*cfb92d14SAndroid Build Coastguard Worker elif entry.rloc16 == r2_rloc: 131*cfb92d14SAndroid Build Coastguard Worker # r1 should be directly connected to r2. 132*cfb92d14SAndroid Build Coastguard Worker verify(entry.is_link_established()) 133*cfb92d14SAndroid Build Coastguard Worker verify(entry.ext_address == r2_ext_addr) 134*cfb92d14SAndroid Build Coastguard Worker elif entry.rloc16 == r3_rloc: 135*cfb92d14SAndroid Build Coastguard Worker # r1 should be directly connected to r3. 136*cfb92d14SAndroid Build Coastguard Worker verify(entry.is_link_established()) 137*cfb92d14SAndroid Build Coastguard Worker verify(entry.ext_address == r3_ext_addr) 138*cfb92d14SAndroid Build Coastguard Worker elif entry.rloc16 == r4_rloc: 139*cfb92d14SAndroid Build Coastguard Worker # r1's next hop towards r4 should be through r3. 140*cfb92d14SAndroid Build Coastguard Worker verify(not entry.is_link_established()) 141*cfb92d14SAndroid Build Coastguard Worker verify(entry.next_hop == r3_id) 142*cfb92d14SAndroid Build Coastguard Worker else: 143*cfb92d14SAndroid Build Coastguard Worker raise (wpan.VerifyError("unknown entry in the router table of r1")) 144*cfb92d14SAndroid Build Coastguard Worker 145*cfb92d14SAndroid Build Coastguard Worker 146*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_r1_router_table, WAIT_TIME) 147*cfb92d14SAndroid Build Coastguard Worker 148*cfb92d14SAndroid Build Coastguard Worker 149*cfb92d14SAndroid Build Coastguard Workerdef check_r3_router_table(): 150*cfb92d14SAndroid Build Coastguard Worker router_table = wpan.parse_router_table_result(r3.get(wpan.WPAN_THREAD_ROUTER_TABLE)) 151*cfb92d14SAndroid Build Coastguard Worker verify(len(router_table) == 4) 152*cfb92d14SAndroid Build Coastguard Worker for entry in router_table: 153*cfb92d14SAndroid Build Coastguard Worker if entry.rloc16 == r1_rloc: 154*cfb92d14SAndroid Build Coastguard Worker # r3 should be directly connected to r1. 155*cfb92d14SAndroid Build Coastguard Worker verify(entry.is_link_established()) 156*cfb92d14SAndroid Build Coastguard Worker verify(entry.ext_address == r1_ext_addr) 157*cfb92d14SAndroid Build Coastguard Worker elif entry.rloc16 == r2_rloc: 158*cfb92d14SAndroid Build Coastguard Worker # r3 should be directly connected to r2. 159*cfb92d14SAndroid Build Coastguard Worker verify(entry.is_link_established()) 160*cfb92d14SAndroid Build Coastguard Worker verify(entry.ext_address == r2_ext_addr) 161*cfb92d14SAndroid Build Coastguard Worker elif entry.rloc16 == r3_rloc: 162*cfb92d14SAndroid Build Coastguard Worker pass 163*cfb92d14SAndroid Build Coastguard Worker elif entry.rloc16 == r4_rloc: 164*cfb92d14SAndroid Build Coastguard Worker # r3 should be directly connected to r4. 165*cfb92d14SAndroid Build Coastguard Worker verify(entry.is_link_established()) 166*cfb92d14SAndroid Build Coastguard Worker verify(entry.ext_address == r4_ext_addr) 167*cfb92d14SAndroid Build Coastguard Worker else: 168*cfb92d14SAndroid Build Coastguard Worker raise (wpan.VerifyError("unknown entry in the router table of r3")) 169*cfb92d14SAndroid Build Coastguard Worker 170*cfb92d14SAndroid Build Coastguard Worker 171*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_r3_router_table, WAIT_TIME) 172*cfb92d14SAndroid Build Coastguard Worker 173*cfb92d14SAndroid Build Coastguard Worker 174*cfb92d14SAndroid Build Coastguard Workerdef check_r4_router_table(): 175*cfb92d14SAndroid Build Coastguard Worker router_table = wpan.parse_router_table_result(r4.get(wpan.WPAN_THREAD_ROUTER_TABLE)) 176*cfb92d14SAndroid Build Coastguard Worker verify(len(router_table) == 4) 177*cfb92d14SAndroid Build Coastguard Worker for entry in router_table: 178*cfb92d14SAndroid Build Coastguard Worker if entry.rloc16 == r1_rloc: 179*cfb92d14SAndroid Build Coastguard Worker # r4's next hop towards r1 should be through r3. 180*cfb92d14SAndroid Build Coastguard Worker verify(not entry.is_link_established()) 181*cfb92d14SAndroid Build Coastguard Worker verify(entry.next_hop == r3_id) 182*cfb92d14SAndroid Build Coastguard Worker elif entry.rloc16 == r2_rloc: 183*cfb92d14SAndroid Build Coastguard Worker # r4's next hop towards r2 should be through r3. 184*cfb92d14SAndroid Build Coastguard Worker verify(not entry.is_link_established()) 185*cfb92d14SAndroid Build Coastguard Worker verify(entry.next_hop == r3_id) 186*cfb92d14SAndroid Build Coastguard Worker elif entry.rloc16 == r3_rloc: 187*cfb92d14SAndroid Build Coastguard Worker # r4 should be directly connected to r3. 188*cfb92d14SAndroid Build Coastguard Worker verify(entry.is_link_established()) 189*cfb92d14SAndroid Build Coastguard Worker verify(entry.ext_address == r3_ext_addr) 190*cfb92d14SAndroid Build Coastguard Worker elif entry.rloc16 == r4_rloc: 191*cfb92d14SAndroid Build Coastguard Worker pass 192*cfb92d14SAndroid Build Coastguard Worker else: 193*cfb92d14SAndroid Build Coastguard Worker raise (wpan.VerifyError("unknown entry in the router table of r4")) 194*cfb92d14SAndroid Build Coastguard Worker 195*cfb92d14SAndroid Build Coastguard Worker 196*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_r4_router_table, WAIT_TIME) 197*cfb92d14SAndroid Build Coastguard Worker 198*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 199*cfb92d14SAndroid Build Coastguard Worker# Test finished 200*cfb92d14SAndroid Build Coastguard Worker 201*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes() 202*cfb92d14SAndroid Build Coastguard Worker 203*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name)) 204