1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*cfb92d14SAndroid Build Coastguard Worker# 3*cfb92d14SAndroid Build Coastguard Worker# Copyright (c) 2022, 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 Workerfrom cli import verify 30*cfb92d14SAndroid Build Coastguard Workerfrom cli import verify_within 31*cfb92d14SAndroid Build Coastguard Workerimport cli 32*cfb92d14SAndroid Build Coastguard Workerimport time 33*cfb92d14SAndroid Build Coastguard Worker 34*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 35*cfb92d14SAndroid Build Coastguard Worker# Test description: Address Cache Table 36*cfb92d14SAndroid Build Coastguard Worker# 37*cfb92d14SAndroid Build Coastguard Worker# This test verifies the behavior of `AddressResolver` module and entries in 38*cfb92d14SAndroid Build Coastguard Worker# address cache table. It also tests the behavior of nodes when there are 39*cfb92d14SAndroid Build Coastguard Worker# topology changes in the network (e.g., a child switches parent). In 40*cfb92d14SAndroid Build Coastguard Worker# particular, the test covers the address cache update through snooping, i.e., 41*cfb92d14SAndroid Build Coastguard Worker# the logic which inspects forwarded frames to update address cache table if 42*cfb92d14SAndroid Build Coastguard Worker# source RLOC16 on a received frame differs from an existing entry in the 43*cfb92d14SAndroid Build Coastguard Worker# address cache table. 44*cfb92d14SAndroid Build Coastguard Worker# 45*cfb92d14SAndroid Build Coastguard Worker# Network topology: 46*cfb92d14SAndroid Build Coastguard Worker# 47*cfb92d14SAndroid Build Coastguard Worker# r1 ---- r2 ---- r3 48*cfb92d14SAndroid Build Coastguard Worker# | | | 49*cfb92d14SAndroid Build Coastguard Worker# | | | 50*cfb92d14SAndroid Build Coastguard Worker# c1 c2(s) c3 51*cfb92d14SAndroid Build Coastguard Worker# 52*cfb92d14SAndroid Build Coastguard Worker# c1 and c3 are FED children, c2 is an SED which is first attached to r2 and 53*cfb92d14SAndroid Build Coastguard Worker# then forced to switch to r3. 54*cfb92d14SAndroid Build Coastguard Worker 55*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__ 56*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120) 57*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name)) 58*cfb92d14SAndroid Build Coastguard Worker 59*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 60*cfb92d14SAndroid Build Coastguard Worker# Creating `cli.Nodes` instances 61*cfb92d14SAndroid Build Coastguard Worker 62*cfb92d14SAndroid Build Coastguard Workerspeedup = 25 63*cfb92d14SAndroid Build Coastguard Workercli.Node.set_time_speedup_factor(speedup) 64*cfb92d14SAndroid Build Coastguard Worker 65*cfb92d14SAndroid Build Coastguard Workerr1 = cli.Node() 66*cfb92d14SAndroid Build Coastguard Workerr2 = cli.Node() 67*cfb92d14SAndroid Build Coastguard Workerr3 = cli.Node() 68*cfb92d14SAndroid Build Coastguard Workerc1 = cli.Node() 69*cfb92d14SAndroid Build Coastguard Workerc2 = cli.Node() 70*cfb92d14SAndroid Build Coastguard Workerc3 = cli.Node() 71*cfb92d14SAndroid Build Coastguard Worker 72*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 73*cfb92d14SAndroid Build Coastguard Worker# Form topology 74*cfb92d14SAndroid Build Coastguard Worker 75*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2) 76*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(c1) 77*cfb92d14SAndroid Build Coastguard Worker 78*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1) 79*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r3) 80*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(c2) 81*cfb92d14SAndroid Build Coastguard Worker 82*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r2) 83*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(c3) 84*cfb92d14SAndroid Build Coastguard Worker 85*cfb92d14SAndroid Build Coastguard Workerc1.allowlist_node(r1) 86*cfb92d14SAndroid Build Coastguard Workerc2.allowlist_node(r2) 87*cfb92d14SAndroid Build Coastguard Workerc3.allowlist_node(r3) 88*cfb92d14SAndroid Build Coastguard Worker 89*cfb92d14SAndroid Build Coastguard Workerr1.form('addrrslvr') 90*cfb92d14SAndroid Build Coastguard Workerr2.join(r1) 91*cfb92d14SAndroid Build Coastguard Workerr3.join(r1) 92*cfb92d14SAndroid Build Coastguard Workerc1.join(r1, cli.JOIN_TYPE_REED) 93*cfb92d14SAndroid Build Coastguard Workerc2.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE) 94*cfb92d14SAndroid Build Coastguard Workerc3.join(r1, cli.JOIN_TYPE_REED) 95*cfb92d14SAndroid Build Coastguard Workerc2.set_pollperiod(400) 96*cfb92d14SAndroid Build Coastguard Worker 97*cfb92d14SAndroid Build Coastguard Workerverify(r1.get_state() == 'leader') 98*cfb92d14SAndroid Build Coastguard Workerverify(r2.get_state() == 'router') 99*cfb92d14SAndroid Build Coastguard Workerverify(r3.get_state() == 'router') 100*cfb92d14SAndroid Build Coastguard Workerverify(c1.get_state() == 'child') 101*cfb92d14SAndroid Build Coastguard Workerverify(c2.get_state() == 'child') 102*cfb92d14SAndroid Build Coastguard Workerverify(c3.get_state() == 'child') 103*cfb92d14SAndroid Build Coastguard Worker 104*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 105*cfb92d14SAndroid Build Coastguard Worker# Test Implementation 106*cfb92d14SAndroid Build Coastguard Worker 107*cfb92d14SAndroid Build Coastguard Worker# Wait till first router has either established a link or 108*cfb92d14SAndroid Build Coastguard Worker# has a valid "next hop" towards all other routers. 109*cfb92d14SAndroid Build Coastguard Worker 110*cfb92d14SAndroid Build Coastguard Workerr1_rloc16 = int(r1.get_rloc16(), 16) 111*cfb92d14SAndroid Build Coastguard Worker 112*cfb92d14SAndroid Build Coastguard Worker 113*cfb92d14SAndroid Build Coastguard Workerdef check_r1_router_table(): 114*cfb92d14SAndroid Build Coastguard Worker table = r1.get_router_table() 115*cfb92d14SAndroid Build Coastguard Worker verify(len(table) == 3) 116*cfb92d14SAndroid Build Coastguard Worker for entry in table: 117*cfb92d14SAndroid Build Coastguard Worker verify(int(entry['RLOC16'], 0) == r1_rloc16 or int(entry['Link']) == 1 or int(entry['Next Hop']) != 63) 118*cfb92d14SAndroid Build Coastguard Worker 119*cfb92d14SAndroid Build Coastguard Worker 120*cfb92d14SAndroid Build Coastguard Workerverify_within(check_r1_router_table, 120) 121*cfb92d14SAndroid Build Coastguard Worker 122*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 123*cfb92d14SAndroid Build Coastguard Worker 124*cfb92d14SAndroid Build Coastguard Workerr1_address = r1.get_mleid_ip_addr() 125*cfb92d14SAndroid Build Coastguard Workerc1_address = c1.get_mleid_ip_addr() 126*cfb92d14SAndroid Build Coastguard Workerc2_address = c2.get_mleid_ip_addr() 127*cfb92d14SAndroid Build Coastguard Workerc3_address = c3.get_mleid_ip_addr() 128*cfb92d14SAndroid Build Coastguard Worker 129*cfb92d14SAndroid Build Coastguard Workerr1_rloc16 = int(r1.get_rloc16(), 16) 130*cfb92d14SAndroid Build Coastguard Workerr2_rloc16 = int(r2.get_rloc16(), 16) 131*cfb92d14SAndroid Build Coastguard Workerr3_rloc16 = int(r3.get_rloc16(), 16) 132*cfb92d14SAndroid Build Coastguard Workerc1_rloc16 = int(c1.get_rloc16(), 16) 133*cfb92d14SAndroid Build Coastguard Workerc3_rloc16 = int(c3.get_rloc16(), 16) 134*cfb92d14SAndroid Build Coastguard Worker 135*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 136*cfb92d14SAndroid Build Coastguard Worker# From r1 ping c2 and c3 137*cfb92d14SAndroid Build Coastguard Worker 138*cfb92d14SAndroid Build Coastguard Workerr1.ping(c2_address) 139*cfb92d14SAndroid Build Coastguard Workerr1.ping(c3_address) 140*cfb92d14SAndroid Build Coastguard Worker 141*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 142*cfb92d14SAndroid Build Coastguard Worker# Verify that address cache table contains both c2 and c3 addresses c2 143*cfb92d14SAndroid Build Coastguard Worker# address should match its parent r2 (since c2 is sleepy), and c1 144*cfb92d14SAndroid Build Coastguard Worker# address should match to itself (since c3 is fed). 145*cfb92d14SAndroid Build Coastguard Worker 146*cfb92d14SAndroid Build Coastguard Workercache_table = r1.get_eidcache() 147*cfb92d14SAndroid Build Coastguard Workerverify(len(cache_table) >= 2) 148*cfb92d14SAndroid Build Coastguard Worker 149*cfb92d14SAndroid Build Coastguard Workerfor entry in cache_table: 150*cfb92d14SAndroid Build Coastguard Worker fields = entry.strip().split(' ') 151*cfb92d14SAndroid Build Coastguard Worker verify(fields[2] == 'cache') 152*cfb92d14SAndroid Build Coastguard Worker if fields[0] == c2_address: 153*cfb92d14SAndroid Build Coastguard Worker verify(int(fields[1], 16) == r2_rloc16) 154*cfb92d14SAndroid Build Coastguard Worker elif fields[0] == c3_address: 155*cfb92d14SAndroid Build Coastguard Worker verify(int(fields[1], 16) == c3_rloc16) 156*cfb92d14SAndroid Build Coastguard Worker else: 157*cfb92d14SAndroid Build Coastguard Worker verify(False) 158*cfb92d14SAndroid Build Coastguard Worker 159*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 160*cfb92d14SAndroid Build Coastguard Worker# Force c2 to switch its parent from r2 to r3 161*cfb92d14SAndroid Build Coastguard Worker# 162*cfb92d14SAndroid Build Coastguard Worker# New network topology 163*cfb92d14SAndroid Build Coastguard Worker# 164*cfb92d14SAndroid Build Coastguard Worker# r1 ---- r2 ---- r3 165*cfb92d14SAndroid Build Coastguard Worker# | /\ 166*cfb92d14SAndroid Build Coastguard Worker# | / \ 167*cfb92d14SAndroid Build Coastguard Worker# c1 c2(s) c3 168*cfb92d14SAndroid Build Coastguard Worker 169*cfb92d14SAndroid Build Coastguard Workerr2.un_allowlist_node(c2) 170*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(c2) 171*cfb92d14SAndroid Build Coastguard Workerc2.allowlist_node(r3) 172*cfb92d14SAndroid Build Coastguard Worker 173*cfb92d14SAndroid Build Coastguard Workerc2.thread_stop() 174*cfb92d14SAndroid Build Coastguard Workerc2.thread_start() 175*cfb92d14SAndroid Build Coastguard Worker 176*cfb92d14SAndroid Build Coastguard Worker 177*cfb92d14SAndroid Build Coastguard Workerdef check_c2_attaches_to_r3(): 178*cfb92d14SAndroid Build Coastguard Worker verify(c2.get_state() == 'child') 179*cfb92d14SAndroid Build Coastguard Worker verify(int(c2.get_parent_info()['Rloc'], 16) == r3_rloc16) 180*cfb92d14SAndroid Build Coastguard Worker 181*cfb92d14SAndroid Build Coastguard Worker 182*cfb92d14SAndroid Build Coastguard Workerverify_within(check_c2_attaches_to_r3, 10) 183*cfb92d14SAndroid Build Coastguard Worker 184*cfb92d14SAndroid Build Coastguard Worker 185*cfb92d14SAndroid Build Coastguard Workerdef check_r2_child_table_is_empty(): 186*cfb92d14SAndroid Build Coastguard Worker verify(len(r2.get_child_table()) == 0) 187*cfb92d14SAndroid Build Coastguard Worker 188*cfb92d14SAndroid Build Coastguard Worker 189*cfb92d14SAndroid Build Coastguard Workerverify_within(check_r2_child_table_is_empty, 10) 190*cfb92d14SAndroid Build Coastguard Worker 191*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 192*cfb92d14SAndroid Build Coastguard Worker# Note that r1 still has r2 as the destination for c2's address in its 193*cfb92d14SAndroid Build Coastguard Worker# address cache table. But since r2 is aware that c2 is no longer its 194*cfb92d14SAndroid Build Coastguard Worker# child, when it receives the IPv6 message with c2's address, r2 195*cfb92d14SAndroid Build Coastguard Worker# itself would do an address query for the address and forward the 196*cfb92d14SAndroid Build Coastguard Worker# IPv6 message. 197*cfb92d14SAndroid Build Coastguard Worker 198*cfb92d14SAndroid Build Coastguard Workercache_table = r1.get_eidcache() 199*cfb92d14SAndroid Build Coastguard Workerfor entry in cache_table: 200*cfb92d14SAndroid Build Coastguard Worker fields = entry.strip().split(' ') 201*cfb92d14SAndroid Build Coastguard Worker if fields[0] == c2_address: 202*cfb92d14SAndroid Build Coastguard Worker verify(int(fields[1], 16) == r2_rloc16) 203*cfb92d14SAndroid Build Coastguard Worker break 204*cfb92d14SAndroid Build Coastguard Workerelse: 205*cfb92d14SAndroid Build Coastguard Worker verify(False) 206*cfb92d14SAndroid Build Coastguard Worker 207*cfb92d14SAndroid Build Coastguard Workerr1.ping(c2_address) 208*cfb92d14SAndroid Build Coastguard Worker 209*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 210*cfb92d14SAndroid Build Coastguard Worker# Ping c1 from c2. This will go through c1's parent r1. r1 upon 211*cfb92d14SAndroid Build Coastguard Worker# receiving and forwarding the message should update its address 212*cfb92d14SAndroid Build Coastguard Worker# cache table for c2 (address cache update through snooping). 213*cfb92d14SAndroid Build Coastguard Worker 214*cfb92d14SAndroid Build Coastguard Workerc2.ping(c1_address) 215*cfb92d14SAndroid Build Coastguard Worker 216*cfb92d14SAndroid Build Coastguard Workercache_table = r1.get_eidcache() 217*cfb92d14SAndroid Build Coastguard Worker 218*cfb92d14SAndroid Build Coastguard Workerfor entry in cache_table: 219*cfb92d14SAndroid Build Coastguard Worker fields = entry.strip().split(' ') 220*cfb92d14SAndroid Build Coastguard Worker if fields[0] == c2_address: 221*cfb92d14SAndroid Build Coastguard Worker verify(int(fields[1], 16) == r3_rloc16) 222*cfb92d14SAndroid Build Coastguard Worker verify(fields[2] == 'snoop') 223*cfb92d14SAndroid Build Coastguard Worker break 224*cfb92d14SAndroid Build Coastguard Workerelse: 225*cfb92d14SAndroid Build Coastguard Worker verify(False) 226*cfb92d14SAndroid Build Coastguard Worker 227*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 228*cfb92d14SAndroid Build Coastguard Worker# Test finished 229*cfb92d14SAndroid Build Coastguard Worker 230*cfb92d14SAndroid Build Coastguard Workercli.Node.finalize_all_nodes() 231*cfb92d14SAndroid Build Coastguard Worker 232*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name)) 233