1#!/usr/bin/env python3
2#
3#  Copyright (c) 2023, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28
29from cli import verify
30from cli import verify_within
31import cli
32import time
33
34# -----------------------------------------------------------------------------------------------------------------------
35# Test description: Validate Mesh Diag module commands.
36#
37# Network topology
38#
39#    r1 ---- r2 ---- r3
40#    /\              /|\
41#   /  \            / | \
42#  f1  s1          f2 s2 s3
43
44test_name = __file__[:-3] if __file__.endswith('.py') else __file__
45print('-' * 120)
46print('Starting \'{}\''.format(test_name))
47
48# -----------------------------------------------------------------------------------------------------------------------
49# Creating `cli.Node` instances
50
51speedup = 40
52cli.Node.set_time_speedup_factor(speedup)
53
54r1 = cli.Node()
55r2 = cli.Node()
56r3 = cli.Node()
57fed1 = cli.Node()
58fed2 = cli.Node()
59sed1 = cli.Node()
60sed2 = cli.Node()
61sed3 = cli.Node()
62
63# -----------------------------------------------------------------------------------------------------------------------
64# Form topology
65
66r1.allowlist_node(r2)
67r1.allowlist_node(fed1)
68r1.allowlist_node(sed1)
69
70r2.allowlist_node(r1)
71r2.allowlist_node(r3)
72
73r3.allowlist_node(r2)
74r3.allowlist_node(fed2)
75r3.allowlist_node(sed2)
76r3.allowlist_node(sed3)
77
78fed1.allowlist_node(r1)
79fed2.allowlist_node(r3)
80sed1.allowlist_node(r1)
81sed2.allowlist_node(r3)
82sed3.allowlist_node(r3)
83
84r1.form('mesh-diag')
85
86r1.add_prefix('fd00:1::/64', 'poas')
87r1.add_prefix('fd00:2::/64', 'poas')
88r1.register_netdata()
89
90r2.join(r1)
91r3.join(r1)
92fed1.join(r1, cli.JOIN_TYPE_REED)
93fed2.join(r1, cli.JOIN_TYPE_REED)
94sed1.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
95sed2.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
96sed3.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
97
98verify(r1.get_state() == 'leader')
99verify(r2.get_state() == 'router')
100verify(r3.get_state() == 'router')
101verify(fed1.get_state() == 'child')
102verify(fed2.get_state() == 'child')
103verify(sed1.get_state() == 'child')
104verify(sed2.get_state() == 'child')
105verify(sed3.get_state() == 'child')
106
107# -----------------------------------------------------------------------------------------------------------------------
108# Test Implementation
109
110r1_rloc = int(r1.get_rloc16(), 16)
111r2_rloc = int(r2.get_rloc16(), 16)
112r3_rloc = int(r3.get_rloc16(), 16)
113
114fed1_rloc = int(fed1.get_rloc16(), 16)
115fed2_rloc = int(fed2.get_rloc16(), 16)
116
117sed1_rloc = int(sed1.get_rloc16(), 16)
118sed2_rloc = int(sed2.get_rloc16(), 16)
119sed3_rloc = int(sed3.get_rloc16(), 16)
120
121topo = r1.cli('meshdiag topology')
122verify(len(topo) == 6)
123
124routers = []
125for line in topo:
126    if line.startswith('id:'):
127        routers.append(int(line.split(' ')[1].split(':')[1], 16))
128
129verify(r1_rloc in routers)
130verify(r2_rloc in routers)
131verify(r3_rloc in routers)
132
133r2.cli('meshdiag topology ip6-addrs')
134r3.cli('meshdiag topology ip6-addrs children')
135r1.cli('meshdiag topology children')
136
137childtable = r2.cli('meshdiag childtable', r1_rloc)
138verify(len([line for line in childtable if line.startswith('rloc16')]) == 2)
139verify(len([line for line in childtable if ' supvn:0 ' in line]) == 1)
140verify(len([line for line in childtable if ' supvn:' in line and 'supvn:0' not in line]) == 1)
141
142childtable = r1.cli('meshdiag childtable', r3_rloc)
143verify(len([line for line in childtable if line.startswith('rloc16')]) == 3)
144verify(len([line for line in childtable if ' supvn:0 ' in line]) == 1)
145verify(len([line for line in childtable if ' supvn:' in line and 'supvn:0' not in line]) == 2)
146
147childtable = r1.cli('meshdiag childtable', r2_rloc)
148verify(len([line for line in childtable if line.startswith('rloc16')]) == 0)
149
150childrenip6addrs = r2.cli('meshdiag childip6', r1_rloc)
151verify(len([line for line in childrenip6addrs if line.startswith('child-rloc16')]) == 1)
152verify(len([line for line in childrenip6addrs if line.startswith('   ')]) == 3)
153
154childrenip6addrs = r1.cli('meshdiag childip6', r3_rloc)
155verify(len([line for line in childrenip6addrs if line.startswith('child-rloc16')]) == 2)
156
157neightable = r1.cli('meshdiag routerneighbortable', r2_rloc)
158verify(len([line for line in neightable if line.startswith('rloc16')]) == 2)
159
160neightable = r3.cli('meshdiag routerneighbortable', r1_rloc)
161verify(len([line for line in neightable if line.startswith('rloc16')]) == 1)
162
163# -----------------------------------------------------------------------------------------------------------------------
164# Test finished
165
166cli.Node.finalize_all_nodes()
167
168print('\'{}\' passed.'.format(test_name))
169