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: 36# This test covers reset of parent, router, leader and restoring children after reset 37# 38 39test_name = __file__[:-3] if __file__.endswith('.py') else __file__ 40print('-' * 120) 41print('Starting \'{}\''.format(test_name)) 42 43# ----------------------------------------------------------------------------------------------------------------------- 44# Creating `cli.Nodes` instances 45 46speedup = 25 47cli.Node.set_time_speedup_factor(speedup) 48 49leader = cli.Node() 50router = cli.Node() 51child1 = cli.Node() 52child2 = cli.Node() 53 54# ----------------------------------------------------------------------------------------------------------------------- 55# Form topology 56 57leader.form('reset') 58child1.join(leader, cli.JOIN_TYPE_REED) 59child2.join(leader, cli.JOIN_TYPE_END_DEVICE) 60 61verify(leader.get_state() == 'leader') 62verify(child1.get_state() == 'child') 63verify(child2.get_state() == 'child') 64 65# ----------------------------------------------------------------------------------------------------------------------- 66# Test Implementation 67 68# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 69# Reset the parent and verify that both children are restored on 70# parent through "Child Update" exchange process and none of them got 71# detached and needed to attach back. 72 73del leader 74leader = cli.Node(index=1) 75leader.interface_up() 76leader.thread_start() 77 78 79def check_leader_state(): 80 verify(leader.get_state() == 'leader') 81 82 83verify_within(check_leader_state, 10) 84 85# Check that `child1` and `child2` did not detach 86 87verify(child1.get_state() == 'child') 88verify(child2.get_state() == 'child') 89 90verify(int(cli.Node.parse_list(child1.get_mle_counter())['Role Detached']) == 1) 91verify(int(cli.Node.parse_list(child2.get_mle_counter())['Role Detached']) == 1) 92 93# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 94# Reset `router` and make sure it recovers as router with same router ID. 95 96router.join(leader) 97 98verify(router.get_state() == 'router') 99router_rloc16 = int(router.get_rloc16(), 16) 100 101time.sleep(0.75) 102 103del router 104router = cli.Node(index=2) 105router.interface_up() 106router.thread_start() 107 108 109def check_router_state(): 110 verify(router.get_state() == 'router') 111 112 113verify_within(check_router_state, 10) 114verify(router_rloc16 == int(router.get_rloc16(), 16)) 115 116# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 117# Reset `leader` and make sure `router` is its neighbor again 118 119del leader 120leader = cli.Node(index=1) 121leader.interface_up() 122leader.thread_start() 123 124 125def check_leader_state(): 126 verify(leader.get_state() == 'leader') 127 128 129verify_within(check_leader_state, 10) 130 131 132def check_leader_neighbor_table(): 133 verify(len(leader.get_neighbor_table()) == 3) 134 135 136verify_within(check_leader_neighbor_table, 10) 137 138# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 139# Reset `child` and make sure it re-attaches successfully. 140 141del child1 142child1 = cli.Node(index=3) 143child1.set_router_eligible('disable') 144child1.interface_up() 145child1.thread_start() 146 147 148def check_child1_state(): 149 verify(child1.get_state() == 'child') 150 table = child1.get_router_table() 151 verify(len(table) == 2) 152 153 154verify_within(check_child1_state, 10) 155 156# ----------------------------------------------------------------------------------------------------------------------- 157# Test finished 158 159cli.Node.finalize_all_nodes() 160 161print('\'{}\' passed.'.format(test_name)) 162