1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*cfb92d14SAndroid Build Coastguard Worker# 3*cfb92d14SAndroid Build Coastguard Worker# Copyright (c) 2023, 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: Validate Mesh Diag module commands. 36*cfb92d14SAndroid Build Coastguard Worker# 37*cfb92d14SAndroid Build Coastguard Worker# Network topology 38*cfb92d14SAndroid Build Coastguard Worker# 39*cfb92d14SAndroid Build Coastguard Worker# r1 ---- r2 ---- r3 40*cfb92d14SAndroid Build Coastguard Worker# /\ /|\ 41*cfb92d14SAndroid Build Coastguard Worker# / \ / | \ 42*cfb92d14SAndroid Build Coastguard Worker# f1 s1 f2 s2 s3 43*cfb92d14SAndroid Build Coastguard Worker 44*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__ 45*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120) 46*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name)) 47*cfb92d14SAndroid Build Coastguard Worker 48*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 49*cfb92d14SAndroid Build Coastguard Worker# Creating `cli.Node` instances 50*cfb92d14SAndroid Build Coastguard Worker 51*cfb92d14SAndroid Build Coastguard Workerspeedup = 40 52*cfb92d14SAndroid Build Coastguard Workercli.Node.set_time_speedup_factor(speedup) 53*cfb92d14SAndroid Build Coastguard Worker 54*cfb92d14SAndroid Build Coastguard Workerr1 = cli.Node() 55*cfb92d14SAndroid Build Coastguard Workerr2 = cli.Node() 56*cfb92d14SAndroid Build Coastguard Workerr3 = cli.Node() 57*cfb92d14SAndroid Build Coastguard Workerfed1 = cli.Node() 58*cfb92d14SAndroid Build Coastguard Workerfed2 = cli.Node() 59*cfb92d14SAndroid Build Coastguard Workersed1 = cli.Node() 60*cfb92d14SAndroid Build Coastguard Workersed2 = cli.Node() 61*cfb92d14SAndroid Build Coastguard Workersed3 = cli.Node() 62*cfb92d14SAndroid Build Coastguard Worker 63*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 64*cfb92d14SAndroid Build Coastguard Worker# Form topology 65*cfb92d14SAndroid Build Coastguard Worker 66*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2) 67*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(fed1) 68*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(sed1) 69*cfb92d14SAndroid Build Coastguard Worker 70*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1) 71*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r3) 72*cfb92d14SAndroid Build Coastguard Worker 73*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r2) 74*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(fed2) 75*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(sed2) 76*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(sed3) 77*cfb92d14SAndroid Build Coastguard Worker 78*cfb92d14SAndroid Build Coastguard Workerfed1.allowlist_node(r1) 79*cfb92d14SAndroid Build Coastguard Workerfed2.allowlist_node(r3) 80*cfb92d14SAndroid Build Coastguard Workersed1.allowlist_node(r1) 81*cfb92d14SAndroid Build Coastguard Workersed2.allowlist_node(r3) 82*cfb92d14SAndroid Build Coastguard Workersed3.allowlist_node(r3) 83*cfb92d14SAndroid Build Coastguard Worker 84*cfb92d14SAndroid Build Coastguard Workerr1.form('mesh-diag') 85*cfb92d14SAndroid Build Coastguard Worker 86*cfb92d14SAndroid Build Coastguard Workerr1.add_prefix('fd00:1::/64', 'poas') 87*cfb92d14SAndroid Build Coastguard Workerr1.add_prefix('fd00:2::/64', 'poas') 88*cfb92d14SAndroid Build Coastguard Workerr1.register_netdata() 89*cfb92d14SAndroid Build Coastguard Worker 90*cfb92d14SAndroid Build Coastguard Workerr2.join(r1) 91*cfb92d14SAndroid Build Coastguard Workerr3.join(r1) 92*cfb92d14SAndroid Build Coastguard Workerfed1.join(r1, cli.JOIN_TYPE_REED) 93*cfb92d14SAndroid Build Coastguard Workerfed2.join(r1, cli.JOIN_TYPE_REED) 94*cfb92d14SAndroid Build Coastguard Workersed1.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE) 95*cfb92d14SAndroid Build Coastguard Workersed2.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE) 96*cfb92d14SAndroid Build Coastguard Workersed3.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE) 97*cfb92d14SAndroid Build Coastguard Worker 98*cfb92d14SAndroid Build Coastguard Workerverify(r1.get_state() == 'leader') 99*cfb92d14SAndroid Build Coastguard Workerverify(r2.get_state() == 'router') 100*cfb92d14SAndroid Build Coastguard Workerverify(r3.get_state() == 'router') 101*cfb92d14SAndroid Build Coastguard Workerverify(fed1.get_state() == 'child') 102*cfb92d14SAndroid Build Coastguard Workerverify(fed2.get_state() == 'child') 103*cfb92d14SAndroid Build Coastguard Workerverify(sed1.get_state() == 'child') 104*cfb92d14SAndroid Build Coastguard Workerverify(sed2.get_state() == 'child') 105*cfb92d14SAndroid Build Coastguard Workerverify(sed3.get_state() == 'child') 106*cfb92d14SAndroid Build Coastguard Worker 107*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 108*cfb92d14SAndroid Build Coastguard Worker# Test Implementation 109*cfb92d14SAndroid Build Coastguard Worker 110*cfb92d14SAndroid Build Coastguard Workerr1_rloc = int(r1.get_rloc16(), 16) 111*cfb92d14SAndroid Build Coastguard Workerr2_rloc = int(r2.get_rloc16(), 16) 112*cfb92d14SAndroid Build Coastguard Workerr3_rloc = int(r3.get_rloc16(), 16) 113*cfb92d14SAndroid Build Coastguard Worker 114*cfb92d14SAndroid Build Coastguard Workerfed1_rloc = int(fed1.get_rloc16(), 16) 115*cfb92d14SAndroid Build Coastguard Workerfed2_rloc = int(fed2.get_rloc16(), 16) 116*cfb92d14SAndroid Build Coastguard Worker 117*cfb92d14SAndroid Build Coastguard Workersed1_rloc = int(sed1.get_rloc16(), 16) 118*cfb92d14SAndroid Build Coastguard Workersed2_rloc = int(sed2.get_rloc16(), 16) 119*cfb92d14SAndroid Build Coastguard Workersed3_rloc = int(sed3.get_rloc16(), 16) 120*cfb92d14SAndroid Build Coastguard Worker 121*cfb92d14SAndroid Build Coastguard Workertopo = r1.cli('meshdiag topology') 122*cfb92d14SAndroid Build Coastguard Workerverify(len(topo) == 6) 123*cfb92d14SAndroid Build Coastguard Worker 124*cfb92d14SAndroid Build Coastguard Workerrouters = [] 125*cfb92d14SAndroid Build Coastguard Workerfor line in topo: 126*cfb92d14SAndroid Build Coastguard Worker if line.startswith('id:'): 127*cfb92d14SAndroid Build Coastguard Worker routers.append(int(line.split(' ')[1].split(':')[1], 16)) 128*cfb92d14SAndroid Build Coastguard Worker 129*cfb92d14SAndroid Build Coastguard Workerverify(r1_rloc in routers) 130*cfb92d14SAndroid Build Coastguard Workerverify(r2_rloc in routers) 131*cfb92d14SAndroid Build Coastguard Workerverify(r3_rloc in routers) 132*cfb92d14SAndroid Build Coastguard Worker 133*cfb92d14SAndroid Build Coastguard Workerr2.cli('meshdiag topology ip6-addrs') 134*cfb92d14SAndroid Build Coastguard Workerr3.cli('meshdiag topology ip6-addrs children') 135*cfb92d14SAndroid Build Coastguard Workerr1.cli('meshdiag topology children') 136*cfb92d14SAndroid Build Coastguard Worker 137*cfb92d14SAndroid Build Coastguard Workerchildtable = r2.cli('meshdiag childtable', r1_rloc) 138*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childtable if line.startswith('rloc16')]) == 2) 139*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childtable if ' supvn:0 ' in line]) == 1) 140*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childtable if ' supvn:' in line and 'supvn:0' not in line]) == 1) 141*cfb92d14SAndroid Build Coastguard Worker 142*cfb92d14SAndroid Build Coastguard Workerchildtable = r1.cli('meshdiag childtable', r3_rloc) 143*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childtable if line.startswith('rloc16')]) == 3) 144*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childtable if ' supvn:0 ' in line]) == 1) 145*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childtable if ' supvn:' in line and 'supvn:0' not in line]) == 2) 146*cfb92d14SAndroid Build Coastguard Worker 147*cfb92d14SAndroid Build Coastguard Workerchildtable = r1.cli('meshdiag childtable', r2_rloc) 148*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childtable if line.startswith('rloc16')]) == 0) 149*cfb92d14SAndroid Build Coastguard Worker 150*cfb92d14SAndroid Build Coastguard Workerchildrenip6addrs = r2.cli('meshdiag childip6', r1_rloc) 151*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childrenip6addrs if line.startswith('child-rloc16')]) == 1) 152*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childrenip6addrs if line.startswith(' ')]) == 3) 153*cfb92d14SAndroid Build Coastguard Worker 154*cfb92d14SAndroid Build Coastguard Workerchildrenip6addrs = r1.cli('meshdiag childip6', r3_rloc) 155*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in childrenip6addrs if line.startswith('child-rloc16')]) == 2) 156*cfb92d14SAndroid Build Coastguard Worker 157*cfb92d14SAndroid Build Coastguard Workerneightable = r1.cli('meshdiag routerneighbortable', r2_rloc) 158*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in neightable if line.startswith('rloc16')]) == 2) 159*cfb92d14SAndroid Build Coastguard Worker 160*cfb92d14SAndroid Build Coastguard Workerneightable = r3.cli('meshdiag routerneighbortable', r1_rloc) 161*cfb92d14SAndroid Build Coastguard Workerverify(len([line for line in neightable if line.startswith('rloc16')]) == 1) 162*cfb92d14SAndroid Build Coastguard Worker 163*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 164*cfb92d14SAndroid Build Coastguard Worker# Test finished 165*cfb92d14SAndroid Build Coastguard Worker 166*cfb92d14SAndroid Build Coastguard Workercli.Node.finalize_all_nodes() 167*cfb92d14SAndroid Build Coastguard Worker 168*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name)) 169