mirror of
https://github.com/espressif/openthread.git
synced 2026-06-05 21:14:49 +00:00
[nexus] add test for informing previous parent on reattach (#12919)
This commit adds a new Nexus test to verify that a Sleepy End Device (SED) correctly informs its previous parent after reattaching to a new parent. This replicates the functionality of the now-deleted test_inform_previous_parent_on_reattach.py script. The test scenario involves: - Forming a network with a Leader and a Router. - Attaching a SED to the Leader. - Simulating a link failure between the SED and Leader while allowing communication between the SED and Router. - Verifying that the SED reattaches to the Router. - Confirming that the SED sends an empty IPv6 message (Next Header 59) to the Leader's RLOC to inform it of the change. - Ensuring the SED is successfully removed from the Leader's child table. Migrating this test to Nexus allows for faster execution using virtual time and single-process simulation.
This commit is contained in:
@@ -381,6 +381,7 @@ ot_nexus_test(1_4_PIC_TC_1 "cert;nexus")
|
||||
ot_nexus_test(1_4_PIC_TC_3 "cert;nexus")
|
||||
ot_nexus_test(1_4_PIC_TC_4 "cert;nexus")
|
||||
ot_nexus_test(1_4_CS_TC_3 "cert;nexus")
|
||||
ot_nexus_test(inform_previous_parent_on_reattach "cert;nexus")
|
||||
|
||||
# Misc tests
|
||||
ot_nexus_test(border_admitter "core;nexus")
|
||||
|
||||
@@ -233,6 +233,7 @@ DEFAULT_TESTS=(
|
||||
"1_4_PIC_TC_3"
|
||||
"1_4_PIC_TC_4"
|
||||
"1_4_CS_TC_3"
|
||||
"inform_previous_parent_on_reattach"
|
||||
)
|
||||
|
||||
# Use provided arguments or the default test list
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2026, The OpenThread Authors.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "platform/nexus_core.hpp"
|
||||
#include "platform/nexus_node.hpp"
|
||||
|
||||
namespace ot {
|
||||
namespace Nexus {
|
||||
|
||||
/**
|
||||
* The purpose of this test is to verify a SED will inform its previous parent when re-attaches to another parent.
|
||||
*
|
||||
* Initial Topology:
|
||||
*
|
||||
* LEADER ----- ROUTER
|
||||
* |
|
||||
* SED
|
||||
*
|
||||
*/
|
||||
|
||||
void RunTest(const char *aJsonFileName)
|
||||
{
|
||||
Core nexus;
|
||||
|
||||
Node &leader = nexus.CreateNode();
|
||||
Node &router = nexus.CreateNode();
|
||||
Node &sed = nexus.CreateNode();
|
||||
|
||||
leader.SetName("LEADER");
|
||||
router.SetName("ROUTER");
|
||||
sed.SetName("SED");
|
||||
|
||||
nexus.AdvanceTime(0);
|
||||
|
||||
SuccessOrQuit(Instance::SetGlobalLogLevel(kLogLevelNote));
|
||||
|
||||
Log("---------------------------------------------------------------------------------------");
|
||||
Log("Step 1: Form network with LEADER and ROUTER");
|
||||
|
||||
AllowLinkBetween(leader, router);
|
||||
AllowLinkBetween(leader, sed);
|
||||
|
||||
leader.Form();
|
||||
nexus.AdvanceTime(13000);
|
||||
VerifyOrQuit(leader.Get<Mle::Mle>().IsLeader());
|
||||
|
||||
router.Join(leader);
|
||||
nexus.AdvanceTime(200000);
|
||||
VerifyOrQuit(router.Get<Mle::Mle>().IsRouter());
|
||||
|
||||
Log("---------------------------------------------------------------------------------------");
|
||||
Log("Step 2: SED joins LEADER");
|
||||
|
||||
sed.Get<Mle::Mle>().SetTimeout(60); // 60 seconds
|
||||
sed.Join(leader, Node::kAsSed);
|
||||
nexus.AdvanceTime(20000);
|
||||
VerifyOrQuit(sed.Get<Mle::Mle>().IsChild());
|
||||
VerifyOrQuit(sed.Get<Mle::Mle>().GetParent().GetExtAddress() == leader.Get<Mac::Mac>().GetExtAddress());
|
||||
|
||||
Log("---------------------------------------------------------------------------------------");
|
||||
Log("Step 3: Switch links (Block LEADER <-> SED, Allow ROUTER <-> SED)");
|
||||
|
||||
UnallowLinkBetween(leader, sed);
|
||||
AllowLinkBetween(router, sed);
|
||||
|
||||
Log("---------------------------------------------------------------------------------------");
|
||||
Log("Step 4: SED re-attaches to ROUTER");
|
||||
|
||||
// We wait for SED to realize LEADER is gone and re-attach to ROUTER.
|
||||
// Child timeout is 60s. We wait 100s.
|
||||
nexus.AdvanceTime(100000);
|
||||
|
||||
VerifyOrQuit(sed.Get<Mle::Mle>().IsChild());
|
||||
VerifyOrQuit(sed.Get<Mle::Mle>().GetParent().GetExtAddress() == router.Get<Mac::Mac>().GetExtAddress());
|
||||
|
||||
Log("---------------------------------------------------------------------------------------");
|
||||
Log("Step 5: Verify LEADER's child table");
|
||||
|
||||
// SED will re-attach to ROUTER, which will inform LEADER to remove the child.
|
||||
VerifyOrQuit(leader.Get<ChildTable>().FindChild(sed.Get<Mac::Mac>().GetExtAddress(), Child::kInStateValid) ==
|
||||
nullptr);
|
||||
|
||||
nexus.SaveTestInfo(aJsonFileName, &leader);
|
||||
}
|
||||
|
||||
} // namespace Nexus
|
||||
} // namespace ot
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
ot::Nexus::RunTest(argc > 2 ? argv[2] : "test_inform_previous_parent_on_reattach.json");
|
||||
printf("All tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright (c) 2026, The OpenThread Authors.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# 3. Neither the name of the copyright holder nor the
|
||||
# names of its contributors may be used to endorse or promote products
|
||||
# derived from this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the current directory to sys.path to find verify_utils
|
||||
CUR_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.append(CUR_DIR)
|
||||
|
||||
import verify_utils
|
||||
from pktverify import consts
|
||||
|
||||
|
||||
def verify(pv):
|
||||
pkts = pv.pkts
|
||||
pv.summary.show()
|
||||
|
||||
LEADER = pv.vars['LEADER']
|
||||
ROUTER = pv.vars['ROUTER']
|
||||
SED = pv.vars['SED']
|
||||
|
||||
# Step 1 & 2: SED joins LEADER
|
||||
print("Step 1 & 2: SED joins LEADER")
|
||||
pv.verify_attached('SED', 'LEADER', child_type='MTD')
|
||||
|
||||
# Step 3 & 4: SED re-attaches to ROUTER
|
||||
print("Step 4: SED re-attaches to ROUTER")
|
||||
pv.verify_attached('SED', 'ROUTER', child_type='MTD')
|
||||
|
||||
# Step 5: Verify SED informs previous parent (LEADER)
|
||||
print("Step 5: Verify SED informs previous parent")
|
||||
# SED should send empty IPv6 message to inform previous parent (LEADER).
|
||||
# The next header is 59 (No Next Header), which is 0x3b.
|
||||
|
||||
LEADER_RLOC = pv.vars['LEADER_RLOC']
|
||||
|
||||
pkts.filter_wpan_src64(SED).\
|
||||
filter(lambda p: p.ipv6.dst == LEADER_RLOC and p.ipv6.nxt == 59).\
|
||||
must_next()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
verify_utils.run_main(verify)
|
||||
@@ -1,142 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright (c) 2021, The OpenThread Authors.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# 3. Neither the name of the copyright holder nor the
|
||||
# names of its contributors may be used to endorse or promote products
|
||||
# derived from this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
import config
|
||||
import thread_cert
|
||||
|
||||
from pktverify.packet_filter import PacketFilter
|
||||
|
||||
# Test description:
|
||||
# The purpose of this test is to verify a MED will inform its previous parent when re-attaches to another parent.
|
||||
#
|
||||
# Initial Topology:
|
||||
#
|
||||
# LEADER ----- ROUTER
|
||||
# |
|
||||
# MED
|
||||
#
|
||||
|
||||
LEADER = 1
|
||||
ROUTER = 2
|
||||
MED = 3
|
||||
|
||||
LONG_CHILD_TIMEOUT = 120
|
||||
|
||||
|
||||
class TestReset(thread_cert.TestCase):
|
||||
SUPPORT_NCP = False
|
||||
USE_MESSAGE_FACTORY = False
|
||||
|
||||
TOPOLOGY = {
|
||||
LEADER: {
|
||||
'name': 'LEADER',
|
||||
'mode': 'rdn',
|
||||
'allowlist': [ROUTER, MED]
|
||||
},
|
||||
ROUTER: {
|
||||
'name': 'ROUTER',
|
||||
'mode': 'rdn',
|
||||
'allowlist': [LEADER]
|
||||
},
|
||||
MED: {
|
||||
'name': 'MED',
|
||||
'is_mtd': True,
|
||||
'mode': 'rn',
|
||||
'allowlist': [LEADER],
|
||||
'timeout': LONG_CHILD_TIMEOUT,
|
||||
},
|
||||
}
|
||||
|
||||
def test(self):
|
||||
if 'posix' in os.getenv('OT_CLI_PATH', ''):
|
||||
self.skipTest("skip for posix tests")
|
||||
|
||||
self.nodes[LEADER].start()
|
||||
self.simulator.go(config.LEADER_STARTUP_DELAY)
|
||||
self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
|
||||
|
||||
self.nodes[ROUTER].start()
|
||||
self.simulator.go(config.ROUTER_STARTUP_DELAY)
|
||||
self.assertEqual(self.nodes[ROUTER].get_state(), 'router')
|
||||
|
||||
self.nodes[MED].start()
|
||||
self.simulator.go(7)
|
||||
self.assertEqual(self.nodes[MED].get_state(), 'child')
|
||||
|
||||
self.assertIsChildOf(MED, LEADER)
|
||||
|
||||
self.nodes[LEADER].remove_allowlist(self.nodes[MED].get_addr64())
|
||||
self.nodes[MED].remove_allowlist(self.nodes[LEADER].get_addr64())
|
||||
|
||||
self.nodes[ROUTER].add_allowlist(self.nodes[MED].get_addr64())
|
||||
self.nodes[MED].add_allowlist(self.nodes[ROUTER].get_addr64())
|
||||
|
||||
self.nodes[MED].set_timeout(config.DEFAULT_CHILD_TIMEOUT)
|
||||
|
||||
self.simulator.go(config.DEFAULT_CHILD_TIMEOUT * 2)
|
||||
self.assertIsChildOf(MED, ROUTER)
|
||||
|
||||
# Verify MED is not in the LEADER's Child Table.
|
||||
med_extaddr = self.nodes[MED].get_addr64()
|
||||
self.assertFalse(any(info['extaddr'] == med_extaddr for info in self.nodes[LEADER].get_child_table().values()))
|
||||
|
||||
self.collect_ipaddrs()
|
||||
self.collect_rlocs()
|
||||
|
||||
def verify(self, pv):
|
||||
pkts: PacketFilter = pv.pkts
|
||||
pv.summary.show()
|
||||
|
||||
MED = pv.vars['MED']
|
||||
LEADER_RLOC = pv.vars['LEADER_RLOC']
|
||||
|
||||
# MED should attach to LEADER first.
|
||||
pv.verify_attached('MED', 'LEADER', child_type='MTD')
|
||||
# MED should re-attach to ROUTER.
|
||||
pv.verify_attached('MED', 'ROUTER', child_type='MTD')
|
||||
# MED should send empty IPv6 message to inform previous parent (LEADER).
|
||||
pkts.filter_wpan_src64(MED).filter('lowpan.dst == {LEADER_RLOC} and lowpan.next == 0x3b',
|
||||
LEADER_RLOC=LEADER_RLOC).must_next()
|
||||
|
||||
def assertIsChildOf(self, childid, parentid):
|
||||
childRloc16 = self.nodes[childid].get_addr16()
|
||||
parentRloc16 = self.nodes[parentid].get_addr16()
|
||||
self.assertEqual(parentRloc16 & 0xfc00, parentRloc16)
|
||||
self.assertEqual(childRloc16 & 0xfc00, parentRloc16)
|
||||
|
||||
child_extaddr = self.nodes[childid].get_addr64()
|
||||
self.assertTrue(
|
||||
any(info['extaddr'] == child_extaddr for info in self.nodes[parentid].get_child_table().values()))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user