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: Partition formation and merge 36*cfb92d14SAndroid Build Coastguard Worker# 37*cfb92d14SAndroid Build Coastguard Worker# Network Topology: 38*cfb92d14SAndroid Build Coastguard Worker# 39*cfb92d14SAndroid Build Coastguard Worker# r1 ---- / ---- r2 40*cfb92d14SAndroid Build Coastguard Worker# | \ | 41*cfb92d14SAndroid Build Coastguard Worker# | / | 42*cfb92d14SAndroid Build Coastguard Worker# c1 \ c2 43*cfb92d14SAndroid Build Coastguard Worker# 44*cfb92d14SAndroid Build Coastguard Worker# 45*cfb92d14SAndroid Build Coastguard Worker# Test covers the following situations: 46*cfb92d14SAndroid Build Coastguard Worker# 47*cfb92d14SAndroid Build Coastguard Worker# - r2 forming its own partition when it can no longer hear r1 48*cfb92d14SAndroid Build Coastguard Worker# - Partitions merging into one once r1 and r2 can talk again 49*cfb92d14SAndroid Build Coastguard Worker# - Adding on-mesh prefixes on each partition and ensuring after 50*cfb92d14SAndroid Build Coastguard Worker# merge the info in combined. 51*cfb92d14SAndroid Build Coastguard Worker# 52*cfb92d14SAndroid Build Coastguard Worker 53*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__ 54*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120) 55*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name)) 56*cfb92d14SAndroid Build Coastguard Worker 57*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 58*cfb92d14SAndroid Build Coastguard Worker# Creating `cli.Nodes` instances 59*cfb92d14SAndroid Build Coastguard Worker 60*cfb92d14SAndroid Build Coastguard Workerspeedup = 25 61*cfb92d14SAndroid Build Coastguard Workercli.Node.set_time_speedup_factor(speedup) 62*cfb92d14SAndroid Build Coastguard Worker 63*cfb92d14SAndroid Build Coastguard Workerr1 = cli.Node() 64*cfb92d14SAndroid Build Coastguard Workerr2 = cli.Node() 65*cfb92d14SAndroid Build Coastguard Workerc1 = cli.Node() 66*cfb92d14SAndroid Build Coastguard Workerc2 = cli.Node() 67*cfb92d14SAndroid Build Coastguard Worker 68*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 69*cfb92d14SAndroid Build Coastguard Worker# Form topology 70*cfb92d14SAndroid Build Coastguard Worker 71*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2) 72*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(c1) 73*cfb92d14SAndroid Build Coastguard Worker 74*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1) 75*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(c2) 76*cfb92d14SAndroid Build Coastguard Worker 77*cfb92d14SAndroid Build Coastguard Workerr1.form("partmrge") 78*cfb92d14SAndroid Build Coastguard Workerr2.join(r1) 79*cfb92d14SAndroid Build Coastguard Workerc1.join(r1, cli.JOIN_TYPE_END_DEVICE) 80*cfb92d14SAndroid Build Coastguard Workerc2.join(r2, cli.JOIN_TYPE_END_DEVICE) 81*cfb92d14SAndroid Build Coastguard Worker 82*cfb92d14SAndroid Build Coastguard Workerverify(r1.get_state() == 'leader') 83*cfb92d14SAndroid Build Coastguard Workerverify(r2.get_state() == 'router') 84*cfb92d14SAndroid Build Coastguard Workerverify(c1.get_state() == 'child') 85*cfb92d14SAndroid Build Coastguard Workerverify(c2.get_state() == 'child') 86*cfb92d14SAndroid Build Coastguard Worker 87*cfb92d14SAndroid Build Coastguard Workernodes = [r1, r2, c1, c2] 88*cfb92d14SAndroid Build Coastguard Worker 89*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 90*cfb92d14SAndroid Build Coastguard Worker# Test Implementation 91*cfb92d14SAndroid Build Coastguard Worker 92*cfb92d14SAndroid Build Coastguard Worker# Force the two routers to form their own partition 93*cfb92d14SAndroid Build Coastguard Worker# by removing them from each other's allowlist table 94*cfb92d14SAndroid Build Coastguard Worker 95*cfb92d14SAndroid Build Coastguard Workerr1.un_allowlist_node(r2) 96*cfb92d14SAndroid Build Coastguard Workerr2.un_allowlist_node(r1) 97*cfb92d14SAndroid Build Coastguard Worker 98*cfb92d14SAndroid Build Coastguard Worker# Add a prefix before r2 realizes it can no longer talk 99*cfb92d14SAndroid Build Coastguard Worker# to leader (r1). 100*cfb92d14SAndroid Build Coastguard Worker 101*cfb92d14SAndroid Build Coastguard Workerr2.add_prefix('fd00:abba::/64', 'paros', 'med') 102*cfb92d14SAndroid Build Coastguard Workerr2.register_netdata() 103*cfb92d14SAndroid Build Coastguard Worker 104*cfb92d14SAndroid Build Coastguard Worker# Check that r2 forms its own partition 105*cfb92d14SAndroid Build Coastguard Worker 106*cfb92d14SAndroid Build Coastguard Worker 107*cfb92d14SAndroid Build Coastguard Workerdef check_r2_become_leader(): 108*cfb92d14SAndroid Build Coastguard Worker verify(r2.get_state() == 'leader') 109*cfb92d14SAndroid Build Coastguard Worker 110*cfb92d14SAndroid Build Coastguard Worker 111*cfb92d14SAndroid Build Coastguard Workerverify_within(check_r2_become_leader, 20) 112*cfb92d14SAndroid Build Coastguard Worker 113*cfb92d14SAndroid Build Coastguard Worker# While we have two partition, add a prefix on r1 114*cfb92d14SAndroid Build Coastguard Workerr1.add_prefix('fd00:1234::/64', 'paros', 'med') 115*cfb92d14SAndroid Build Coastguard Workerr1.register_netdata() 116*cfb92d14SAndroid Build Coastguard Worker 117*cfb92d14SAndroid Build Coastguard Worker# Update allowlist and wait for partitions to merge. 118*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2) 119*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1) 120*cfb92d14SAndroid Build Coastguard Worker 121*cfb92d14SAndroid Build Coastguard Worker 122*cfb92d14SAndroid Build Coastguard Workerdef check_partition_id_match(): 123*cfb92d14SAndroid Build Coastguard Worker verify(r1.get_partition_id() == r2.get_partition_id()) 124*cfb92d14SAndroid Build Coastguard Worker 125*cfb92d14SAndroid Build Coastguard Worker 126*cfb92d14SAndroid Build Coastguard Workerverify_within(check_partition_id_match, 20) 127*cfb92d14SAndroid Build Coastguard Worker 128*cfb92d14SAndroid Build Coastguard Worker# Check that partitions merged successfully 129*cfb92d14SAndroid Build Coastguard Worker 130*cfb92d14SAndroid Build Coastguard Worker 131*cfb92d14SAndroid Build Coastguard Workerdef check_r1_r2_roles(): 132*cfb92d14SAndroid Build Coastguard Worker verify(r1.get_state() in ['leader', 'router']) 133*cfb92d14SAndroid Build Coastguard Worker verify(r2.get_state() in ['leader', 'router']) 134*cfb92d14SAndroid Build Coastguard Worker 135*cfb92d14SAndroid Build Coastguard Worker 136*cfb92d14SAndroid Build Coastguard Workerverify_within(check_r1_r2_roles, 10) 137*cfb92d14SAndroid Build Coastguard Worker 138*cfb92d14SAndroid Build Coastguard Worker# Verify all nodes see both prefixes 139*cfb92d14SAndroid Build Coastguard Worker 140*cfb92d14SAndroid Build Coastguard Worker 141*cfb92d14SAndroid Build Coastguard Workerdef check_netdata_on_all_nodes(): 142*cfb92d14SAndroid Build Coastguard Worker for node in nodes: 143*cfb92d14SAndroid Build Coastguard Worker netdata = node.get_netdata() 144*cfb92d14SAndroid Build Coastguard Worker verify(len(netdata['prefixes']) == 2) 145*cfb92d14SAndroid Build Coastguard Worker 146*cfb92d14SAndroid Build Coastguard Worker 147*cfb92d14SAndroid Build Coastguard Workerverify_within(check_netdata_on_all_nodes, 10) 148*cfb92d14SAndroid Build Coastguard Worker 149*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 150*cfb92d14SAndroid Build Coastguard Worker# Test finished 151*cfb92d14SAndroid Build Coastguard Worker 152*cfb92d14SAndroid Build Coastguard Workercli.Node.finalize_all_nodes() 153*cfb92d14SAndroid Build Coastguard Worker 154*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name)) 155