xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-029-pending-dataset-key-change.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1#!/usr/bin/env python3
2#
3#  Copyright (c) 2024, 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:
36#
37# This test validates the rollback of the Active Timestamp when
38# applying a Pending Dataset. This rollback is permitted only when
39# the Network Key changes.
40
41test_name = __file__[:-3] if __file__.endswith('.py') else __file__
42print('-' * 120)
43print('Starting \'{}\''.format(test_name))
44
45# -----------------------------------------------------------------------------------------------------------------------
46# Creating `cli.Node` instances
47
48speedup = 20
49cli.Node.set_time_speedup_factor(speedup)
50
51leader = cli.Node()
52router = cli.Node()
53
54# -----------------------------------------------------------------------------------------------------------------------
55# Form topology
56
57leader.form('network')
58router.join(leader)
59
60verify(leader.get_state() == 'leader')
61verify(router.get_state() == 'router')
62
63# -----------------------------------------------------------------------------------------------------------------------
64# Test Implementation
65
66# Use Pending Dataset to change network name and update
67# Active Timestamp to 10.
68
69router.cli('dataset init active')
70router.cli('dataset activetimestamp 10')
71router.cli('dataset pendingtimestamp 10')
72router.cli('dataset networkname new')
73router.cli('dataset delaytimer 1000')
74router.cli('dataset commit pending')
75
76time.sleep(1.2 / speedup)
77
78# Validate that Active Dataset is updated.
79
80leader.cli('dataset init active')
81verify(leader.get_network_name() == 'new')
82verify(leader.cli('dataset activetimestamp') == ['10'])
83
84#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85# Use Pending Dataset with an older Active Timestamp.
86# Ensure that the new dataset is not accepted.
87
88router.cli('dataset init active')
89router.cli('dataset activetimestamp 5')
90router.cli('dataset pendingtimestamp 15')
91router.cli('dataset networkname shouldfail')
92router.cli('dataset delaytimer 1000')
93router.cli('dataset commit pending')
94
95time.sleep(1.2 / speedup)
96
97leader.cli('dataset init active')
98verify(leader.get_network_name() == 'new')
99verify(leader.cli('dataset activetimestamp') == ['10'])
100
101#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102# Use Pending Dataset with an older Active Timestamp but
103# also change the Network Key.
104
105router.cli('dataset init active')
106router.cli('dataset activetimestamp 7')
107router.cli('dataset pendingtimestamp 20')
108router.cli('dataset networkkey 00112233445566778899aabbccddeeff')
109router.cli('dataset delaytimer 1000')
110router.cli('dataset commit pending')
111
112time.sleep(1.2 / speedup)
113
114# Validate that the Active Dataset is updated and
115# Active Timestamp is rolled back.
116
117leader.cli('dataset init active')
118leader.cli('dataset')
119verify(leader.cli('dataset activetimestamp') == ['7'])
120verify(leader.get_network_key() == '00112233445566778899aabbccddeeff')
121
122# -----------------------------------------------------------------------------------------------------------------------
123# Test finished
124
125cli.Node.finalize_all_nodes()
126
127print('\'{}\' passed.'.format(test_name))
128