diff --git a/tests/nexus/CMakeLists.txt b/tests/nexus/CMakeLists.txt index 4e802fef8..d363f895e 100644 --- a/tests/nexus/CMakeLists.txt +++ b/tests/nexus/CMakeLists.txt @@ -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") diff --git a/tests/nexus/run_nexus_tests.sh b/tests/nexus/run_nexus_tests.sh index e4ef5be52..893caa6df 100755 --- a/tests/nexus/run_nexus_tests.sh +++ b/tests/nexus/run_nexus_tests.sh @@ -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 diff --git a/tests/nexus/test_inform_previous_parent_on_reattach.cpp b/tests/nexus/test_inform_previous_parent_on_reattach.cpp new file mode 100644 index 000000000..30f034a1d --- /dev/null +++ b/tests/nexus/test_inform_previous_parent_on_reattach.cpp @@ -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 +#include + +#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().IsLeader()); + + router.Join(leader); + nexus.AdvanceTime(200000); + VerifyOrQuit(router.Get().IsRouter()); + + Log("---------------------------------------------------------------------------------------"); + Log("Step 2: SED joins LEADER"); + + sed.Get().SetTimeout(60); // 60 seconds + sed.Join(leader, Node::kAsSed); + nexus.AdvanceTime(20000); + VerifyOrQuit(sed.Get().IsChild()); + VerifyOrQuit(sed.Get().GetParent().GetExtAddress() == leader.Get().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().IsChild()); + VerifyOrQuit(sed.Get().GetParent().GetExtAddress() == router.Get().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().FindChild(sed.Get().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; +} diff --git a/tests/nexus/verify_inform_previous_parent_on_reattach.py b/tests/nexus/verify_inform_previous_parent_on_reattach.py new file mode 100644 index 000000000..ab8123000 --- /dev/null +++ b/tests/nexus/verify_inform_previous_parent_on_reattach.py @@ -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) diff --git a/tests/scripts/thread-cert/test_inform_previous_parent_on_reattach.py b/tests/scripts/thread-cert/test_inform_previous_parent_on_reattach.py deleted file mode 100755 index 308bab39f..000000000 --- a/tests/scripts/thread-cert/test_inform_previous_parent_on_reattach.py +++ /dev/null @@ -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()