xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-011-network-data-timeout.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1#!/usr/bin/env python3
2#
3#  Copyright (c) 2022, 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: Network Data (on-mesh prefix) timeout and entry removal
36#
37# Network topology
38#
39#   r1 ----- r2
40#            |
41#            |
42#            c2 (sleepy)
43#
44#
45# Test covers the following steps:
46#
47# - Every node adds a unique on-mesh prefix.
48# - Every node also adds a common on-mesh prefix (with different flags).
49# - Verify that all the unique and common prefixes are present on all nodes are associated with correct RLOC16.
50# - Remove `r2` from network (which removes `c2` as well) from Thread partition created by `r1`.
51# - Verify that all on-mesh prefixes added by `r2` or `c2` (unique and common) are removed on `r1`.
52#
53
54test_name = __file__[:-3] if __file__.endswith('.py') else __file__
55print('-' * 120)
56print('Starting \'{}\''.format(test_name))
57
58# -----------------------------------------------------------------------------------------------------------------------
59# Creating `cli.Nodes` instances
60
61speedup = 25
62cli.Node.set_time_speedup_factor(speedup)
63
64r1 = cli.Node()
65r2 = cli.Node()
66c2 = cli.Node()
67
68# -----------------------------------------------------------------------------------------------------------------------
69# Form topology
70
71r1.allowlist_node(r2)
72
73r2.allowlist_node(r1)
74r2.allowlist_node(c2)
75
76c2.allowlist_node(r2)
77
78r1.form("netdatatmout")
79r2.join(r1)
80c2.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
81c2.set_pollperiod(500)
82
83verify(r1.get_state() == 'leader')
84verify(r2.get_state() == 'router')
85verify(c2.get_state() == 'child')
86
87nodes = [r1, r2, c2]
88
89# -----------------------------------------------------------------------------------------------------------------------
90# Test Implementation
91
92# Each node adds its own prefix.
93r1.add_prefix('fd00:1::/64', 'paros', 'med')
94r2.add_prefix('fd00:2::/64', 'paros', 'med')
95c2.add_prefix('fd00:3::/64', 'paros', 'med')
96
97# a common prefix is added by all three nodes (with different preference)
98r1.add_prefix('fd00:abba::/64', 'paros', 'high')
99r2.add_prefix('fd00:abba::/64', 'paros', 'med')
100c2.add_prefix('fd00:abba::/64', 'paros', 'low')
101
102r1.add_route('fd00:cafe::/64', 's', 'med')
103r2.add_route('fd00:cafe::/64', 's', 'med')
104
105r1.register_netdata()
106r2.register_netdata()
107c2.register_netdata()
108
109
110def check_netdata_on_all_nodes():
111    for node in nodes:
112        netdata = node.get_netdata()
113        verify(len(netdata['prefixes']) == 6)
114        verify(len(netdata['routes']) == 2)
115
116
117verify_within(check_netdata_on_all_nodes, 10)
118
119# Check netdata filtering for r1 entries only.
120
121r1_rloc = int(r1.get_rloc16(), 16)
122netdata = r1.get_netdata(r1_rloc)
123verify(len(netdata['prefixes']) == 2)
124verify(len(netdata['routes']) == 1)
125
126# Remove `r2`. This should trigger all the prefixes added by it or its
127# child to timeout and be removed.
128
129r2.thread_stop()
130r2.interface_down()
131
132
133def check_netdata_on_r1():
134    netdata = r1.get_netdata()
135    verify(len(netdata['prefixes']) == 2)
136    verify(len(netdata['routes']) == 1)
137    netdata = r1.get_netdata(r1_rloc)
138    verify(len(netdata['prefixes']) == 2)
139    verify(len(netdata['routes']) == 1)
140
141
142verify_within(check_netdata_on_r1, 120)
143
144# -----------------------------------------------------------------------------------------------------------------------
145# Test finished
146
147cli.Node.finalize_all_nodes()
148
149print('\'{}\' passed.'.format(test_name))
150