xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-009-router-table.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
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:
36*cfb92d14SAndroid Build Coastguard Worker#
37*cfb92d14SAndroid Build Coastguard Worker# Verify router table entries.
38*cfb92d14SAndroid Build Coastguard Worker#
39*cfb92d14SAndroid Build Coastguard Worker#     r1 ------ r2 ---- r6
40*cfb92d14SAndroid Build Coastguard Worker#      \        /
41*cfb92d14SAndroid Build Coastguard Worker#       \      /
42*cfb92d14SAndroid Build Coastguard Worker#        \    /
43*cfb92d14SAndroid Build Coastguard Worker#          r3 ------ r4 ----- r5
44*cfb92d14SAndroid Build Coastguard Worker#
45*cfb92d14SAndroid Build Coastguard Worker#
46*cfb92d14SAndroid Build Coastguard Worker
47*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
48*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
49*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
50*cfb92d14SAndroid Build Coastguard Worker
51*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
52*cfb92d14SAndroid Build Coastguard Worker# Creating `cli.Nodes` instances
53*cfb92d14SAndroid Build Coastguard Worker
54*cfb92d14SAndroid Build Coastguard Workerspeedup = 10
55*cfb92d14SAndroid Build Coastguard Workercli.Node.set_time_speedup_factor(speedup)
56*cfb92d14SAndroid Build Coastguard Worker
57*cfb92d14SAndroid Build Coastguard Workerr1 = cli.Node()
58*cfb92d14SAndroid Build Coastguard Workerr2 = cli.Node()
59*cfb92d14SAndroid Build Coastguard Workerr3 = cli.Node()
60*cfb92d14SAndroid Build Coastguard Workerr4 = cli.Node()
61*cfb92d14SAndroid Build Coastguard Workerr5 = cli.Node()
62*cfb92d14SAndroid Build Coastguard Workerr6 = cli.Node()
63*cfb92d14SAndroid Build Coastguard Worker
64*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
65*cfb92d14SAndroid Build Coastguard Worker# Form topology
66*cfb92d14SAndroid Build Coastguard Worker
67*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2)
68*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r3)
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 Workerr2.allowlist_node(r6)
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r1)
75*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r2)
76*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r4)
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Workerr4.allowlist_node(r3)
79*cfb92d14SAndroid Build Coastguard Workerr4.allowlist_node(r5)
80*cfb92d14SAndroid Build Coastguard Worker
81*cfb92d14SAndroid Build Coastguard Workerr5.allowlist_node(r4)
82*cfb92d14SAndroid Build Coastguard Worker
83*cfb92d14SAndroid Build Coastguard Workerr6.allowlist_node(r2)
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Workerr1.form("topo")
86*cfb92d14SAndroid Build Coastguard Workerfor node in [r2, r3, r4, r5, r6]:
87*cfb92d14SAndroid Build Coastguard Worker    node.join(r1)
88*cfb92d14SAndroid Build Coastguard Worker
89*cfb92d14SAndroid Build Coastguard Workerverify(r1.get_state() == 'leader')
90*cfb92d14SAndroid Build Coastguard Workerfor node in [r2, r3, r4, r5, r6]:
91*cfb92d14SAndroid Build Coastguard Worker    verify(node.get_state() == 'router')
92*cfb92d14SAndroid Build Coastguard Worker
93*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
94*cfb92d14SAndroid Build Coastguard Worker# Test Implementation
95*cfb92d14SAndroid Build Coastguard Worker
96*cfb92d14SAndroid Build Coastguard Workerr1_rloc16 = int(r1.get_rloc16(), 16)
97*cfb92d14SAndroid Build Coastguard Workerr2_rloc16 = int(r2.get_rloc16(), 16)
98*cfb92d14SAndroid Build Coastguard Workerr3_rloc16 = int(r3.get_rloc16(), 16)
99*cfb92d14SAndroid Build Coastguard Workerr4_rloc16 = int(r4.get_rloc16(), 16)
100*cfb92d14SAndroid Build Coastguard Workerr5_rloc16 = int(r5.get_rloc16(), 16)
101*cfb92d14SAndroid Build Coastguard Workerr6_rloc16 = int(r6.get_rloc16(), 16)
102*cfb92d14SAndroid Build Coastguard Worker
103*cfb92d14SAndroid Build Coastguard Workerr1_rid = r1_rloc16 / 1024
104*cfb92d14SAndroid Build Coastguard Workerr2_rid = r2_rloc16 / 1024
105*cfb92d14SAndroid Build Coastguard Workerr3_rid = r3_rloc16 / 1024
106*cfb92d14SAndroid Build Coastguard Workerr4_rid = r4_rloc16 / 1024
107*cfb92d14SAndroid Build Coastguard Workerr5_rid = r5_rloc16 / 1024
108*cfb92d14SAndroid Build Coastguard Workerr6_rid = r6_rloc16 / 1024
109*cfb92d14SAndroid Build Coastguard Worker
110*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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) == 6)
116*cfb92d14SAndroid Build Coastguard Worker    for entry in table:
117*cfb92d14SAndroid Build Coastguard Worker        rloc16 = int(entry['RLOC16'], 0)
118*cfb92d14SAndroid Build Coastguard Worker        link = int(entry['Link'])
119*cfb92d14SAndroid Build Coastguard Worker        nexthop = int(entry['Next Hop'])
120*cfb92d14SAndroid Build Coastguard Worker        cost = int(entry['Path Cost'])
121*cfb92d14SAndroid Build Coastguard Worker        if rloc16 == r1_rloc16:
122*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
123*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r2_rloc16:
124*cfb92d14SAndroid Build Coastguard Worker            verify(link == 1)
125*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r3_rid)
126*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r3_rloc16:
127*cfb92d14SAndroid Build Coastguard Worker            verify(link == 1)
128*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r2_rid)
129*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r4_rloc16:
130*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
131*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r3_rid)
132*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 1)
133*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r5_rloc16:
134*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
135*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r3_rid)
136*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 2)
137*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r6_rloc16:
138*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
139*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r2_rid)
140*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 1)
141*cfb92d14SAndroid Build Coastguard Worker
142*cfb92d14SAndroid Build Coastguard Worker
143*cfb92d14SAndroid Build Coastguard Workerverify_within(check_r1_router_table, 160)
144*cfb92d14SAndroid Build Coastguard Worker
145*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146*cfb92d14SAndroid Build Coastguard Worker
147*cfb92d14SAndroid Build Coastguard Worker
148*cfb92d14SAndroid Build Coastguard Workerdef check_r2_router_table():
149*cfb92d14SAndroid Build Coastguard Worker    table = r2.get_router_table()
150*cfb92d14SAndroid Build Coastguard Worker    verify(len(table) == 6)
151*cfb92d14SAndroid Build Coastguard Worker    for entry in table:
152*cfb92d14SAndroid Build Coastguard Worker        rloc16 = int(entry['RLOC16'], 0)
153*cfb92d14SAndroid Build Coastguard Worker        link = int(entry['Link'])
154*cfb92d14SAndroid Build Coastguard Worker        nexthop = int(entry['Next Hop'])
155*cfb92d14SAndroid Build Coastguard Worker        cost = int(entry['Path Cost'])
156*cfb92d14SAndroid Build Coastguard Worker        if rloc16 == r1_rloc16:
157*cfb92d14SAndroid Build Coastguard Worker            verify(link == 1)
158*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r3_rid)
159*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r2_rloc16:
160*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
161*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r3_rloc16:
162*cfb92d14SAndroid Build Coastguard Worker            verify(link == 1)
163*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r1_rid)
164*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r4_rloc16:
165*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
166*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r3_rid)
167*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 1)
168*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r5_rloc16:
169*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
170*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r3_rid)
171*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 2)
172*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r6_rloc16:
173*cfb92d14SAndroid Build Coastguard Worker            verify(link == 1)
174*cfb92d14SAndroid Build Coastguard Worker
175*cfb92d14SAndroid Build Coastguard Worker
176*cfb92d14SAndroid Build Coastguard Workerverify_within(check_r2_router_table, 160)
177*cfb92d14SAndroid Build Coastguard Worker
178*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179*cfb92d14SAndroid Build Coastguard Worker
180*cfb92d14SAndroid Build Coastguard Worker
181*cfb92d14SAndroid Build Coastguard Workerdef check_r4_router_table():
182*cfb92d14SAndroid Build Coastguard Worker    table = r4.get_router_table()
183*cfb92d14SAndroid Build Coastguard Worker    verify(len(table) == 6)
184*cfb92d14SAndroid Build Coastguard Worker    for entry in table:
185*cfb92d14SAndroid Build Coastguard Worker        rloc16 = int(entry['RLOC16'], 0)
186*cfb92d14SAndroid Build Coastguard Worker        link = int(entry['Link'])
187*cfb92d14SAndroid Build Coastguard Worker        nexthop = int(entry['Next Hop'])
188*cfb92d14SAndroid Build Coastguard Worker        cost = int(entry['Path Cost'])
189*cfb92d14SAndroid Build Coastguard Worker        if rloc16 == r1_rloc16:
190*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
191*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r3_rid)
192*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 1)
193*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r2_rloc16:
194*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
195*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r3_rid)
196*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 1)
197*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r3_rloc16:
198*cfb92d14SAndroid Build Coastguard Worker            verify(link == 1)
199*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r4_rloc16:
200*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
201*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r5_rloc16:
202*cfb92d14SAndroid Build Coastguard Worker            verify(link == 1)
203*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r6_rloc16:
204*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
205*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r3_rid)
206*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 2)
207*cfb92d14SAndroid Build Coastguard Worker
208*cfb92d14SAndroid Build Coastguard Worker
209*cfb92d14SAndroid Build Coastguard Workerverify_within(check_r4_router_table, 160)
210*cfb92d14SAndroid Build Coastguard Worker
211*cfb92d14SAndroid Build Coastguard Worker# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
212*cfb92d14SAndroid Build Coastguard Worker
213*cfb92d14SAndroid Build Coastguard Worker
214*cfb92d14SAndroid Build Coastguard Workerdef check_r5_router_table():
215*cfb92d14SAndroid Build Coastguard Worker    table = r5.get_router_table()
216*cfb92d14SAndroid Build Coastguard Worker    verify(len(table) == 6)
217*cfb92d14SAndroid Build Coastguard Worker    for entry in table:
218*cfb92d14SAndroid Build Coastguard Worker        rloc16 = int(entry['RLOC16'], 0)
219*cfb92d14SAndroid Build Coastguard Worker        link = int(entry['Link'])
220*cfb92d14SAndroid Build Coastguard Worker        nexthop = int(entry['Next Hop'])
221*cfb92d14SAndroid Build Coastguard Worker        cost = int(entry['Path Cost'])
222*cfb92d14SAndroid Build Coastguard Worker        if rloc16 == r1_rloc16:
223*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
224*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r4_rid)
225*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 2)
226*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r2_rloc16:
227*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
228*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r4_rid)
229*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 2)
230*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r3_rloc16:
231*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
232*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r4_rid)
233*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 1)
234*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r4_rloc16:
235*cfb92d14SAndroid Build Coastguard Worker            verify(link == 1)
236*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r5_rloc16:
237*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
238*cfb92d14SAndroid Build Coastguard Worker        elif rloc16 == r6_rloc16:
239*cfb92d14SAndroid Build Coastguard Worker            verify(link == 0)
240*cfb92d14SAndroid Build Coastguard Worker            verify(nexthop == r4_rid)
241*cfb92d14SAndroid Build Coastguard Worker            verify(cost == 3)
242*cfb92d14SAndroid Build Coastguard Worker
243*cfb92d14SAndroid Build Coastguard Worker
244*cfb92d14SAndroid Build Coastguard Workerverify_within(check_r5_router_table, 160)
245*cfb92d14SAndroid Build Coastguard Worker
246*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
247*cfb92d14SAndroid Build Coastguard Worker# Test finished
248*cfb92d14SAndroid Build Coastguard Worker
249*cfb92d14SAndroid Build Coastguard Workercli.Node.finalize_all_nodes()
250*cfb92d14SAndroid Build Coastguard Worker
251*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
252