From acef2288ca3972cc848c62a8a30341d4a1becc4a Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Tue, 21 Apr 2026 04:33:39 -0700 Subject: [PATCH] [nexus] migrate test_srp_client_remove_host to Nexus (#12946) This commit migrates the SRP client host removal test from the thread-cert Python-based framework to the Nexus framework. The new C++ implementation in tests/nexus/test_srp_client_remove_host.cpp covers the same scenarios as the original Python script: - Successful registration of SRP host and services. - Verification that ClearHostAndServices() does not immediately remove server-side state. - Verification that RemoveHostAndServices(removeKey=False, sendUnregToServer=True) marks the host and services as deleted on the SRP server. - Verification that RemoveHostAndServices(removeKey=True, sendUnregToServer=True) fully removes the host and service entries from the SRP server. The original Python script test_srp_client_remove_host.py is removed as its functionality is now fully covered by the Nexus test. --- tests/nexus/CMakeLists.txt | 1 + tests/nexus/test_srp_client_remove_host.cpp | 178 ++++++++++++++++++ .../test_srp_client_remove_host.py | 176 ----------------- 3 files changed, 179 insertions(+), 176 deletions(-) create mode 100644 tests/nexus/test_srp_client_remove_host.cpp delete mode 100755 tests/scripts/thread-cert/test_srp_client_remove_host.py diff --git a/tests/nexus/CMakeLists.txt b/tests/nexus/CMakeLists.txt index 549cbb76d..b036c7aac 100644 --- a/tests/nexus/CMakeLists.txt +++ b/tests/nexus/CMakeLists.txt @@ -407,6 +407,7 @@ ot_nexus_test(reed_address_solicit_rejected "core;nexus") ot_nexus_test(router_downgrade_on_sec_policy_change "core;nexus") ot_nexus_test(srp_auto_start "core;nexus") ot_nexus_test(srp_client_change_lease "core;nexus") +ot_nexus_test(srp_client_remove_host "core;nexus") ot_nexus_test(srp_lease "core;nexus") ot_nexus_test(srp_many_services_mtu_check "core;nexus") ot_nexus_test(zero_len_external_route "core;nexus") diff --git a/tests/nexus/test_srp_client_remove_host.cpp b/tests/nexus/test_srp_client_remove_host.cpp new file mode 100644 index 000000000..e4c0f6917 --- /dev/null +++ b/tests/nexus/test_srp_client_remove_host.cpp @@ -0,0 +1,178 @@ +/* + * 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 { + +static const Srp::Server::Host *FindHost(Node &aServer, const char *aName) +{ + static constexpr uint16_t kNameStringSize = 400; + + const Srp::Server::Host *host = nullptr; + String fullName; + + fullName.Append("%s.%s", aName, aServer.Get().GetDomain()); + + while (true) + { + host = aServer.Get().GetNextHost(host); + + if (host == nullptr) + { + break; + } + + if (StringMatch(host->GetFullName(), fullName.AsCString())) + { + break; + } + } + + return host; +} + +void TestSrpClientRemoveHost(void) +{ + Core nexus; + + Node &server = nexus.CreateNode(); + Node &client = nexus.CreateNode(); + + server.SetName("server"); + client.SetName("client"); + + Log("Step 0: Start the server and client devices."); + + server.Form(); + nexus.AdvanceTime(13000); + VerifyOrQuit(server.Get().IsLeader()); + + client.Join(server); + nexus.AdvanceTime(200000); + VerifyOrQuit(client.Get().IsRouter()); + + server.Get().SetEnabled(true); + client.Get().EnableAutoStartMode(nullptr, nullptr); + nexus.AdvanceTime(15000); + + Log("Step 1: Register a single service and verify that it worked."); + + SuccessOrQuit(client.Get().SetHostName("host")); + + Ip6::Address address; + Srp::Client::Service service; + + SuccessOrQuit(address.FromString("2001::1")); + SuccessOrQuit(client.Get().SetHostAddresses(&address, 1)); + + service.mName = "_srv._udp"; + service.mInstanceName = "ins"; + service.mSubTypeLabels = nullptr; + service.mTxtEntries = nullptr; + service.mNumTxtEntries = 0; + service.mPort = 1977; + service.mPriority = 0; + service.mWeight = 0; + service.mLease = 0; + service.mKeyLease = 0; + SuccessOrQuit(service.Init()); + SuccessOrQuit(client.Get().AddService(service)); + + nexus.AdvanceTime(2000); + + VerifyOrQuit(client.Get().GetHostInfo().GetState() == Srp::Client::kRegistered); + + const Srp::Server::Host *host = FindHost(server, "host"); + VerifyOrQuit(host != nullptr); + VerifyOrQuit(!host->IsDeleted()); + + uint8_t numAddresses; + const Ip6::Address *addresses = host->GetAddresses(numAddresses); + VerifyOrQuit(numAddresses == 1); + { + Ip6::Address expectedAddress; + SuccessOrQuit(expectedAddress.FromString("2001::1")); + VerifyOrQuit(addresses[0] == expectedAddress); + } + + VerifyOrQuit(!host->GetServices().IsEmpty()); + const Srp::Server::Service *serverService = host->GetServices().GetHead(); + VerifyOrQuit(!serverService->IsDeleted()); + VerifyOrQuit(serverService->GetPort() == 1977); + + Log("Step 2: Clear the info on client, and verify that it is still present on server."); + + client.Get().ClearHostAndServices(); + nexus.AdvanceTime(2000); + + VerifyOrQuit(client.Get().GetHostInfo().GetName() == nullptr); + VerifyOrQuit(FindHost(server, "host") != nullptr); + + Log("Step 3: Set host name and request 'host remove' requiring update to server."); + + SuccessOrQuit(client.Get().SetHostName("host")); + SuccessOrQuit( + client.Get().RemoveHostAndServices(/* aRemoveKeyLease */ false, /* aSendUnregToServer */ true)); + nexus.AdvanceTime(2000); + + VerifyOrQuit(client.Get().GetHostInfo().GetName() == nullptr); + + host = FindHost(server, "host"); + VerifyOrQuit(host != nullptr); + VerifyOrQuit(host->IsDeleted()); + VerifyOrQuit(!host->GetServices().IsEmpty()); + VerifyOrQuit(host->GetServices().GetHead()->IsDeleted()); + + Log("Step 4: Request 'host remove' with `remove_key`."); + + SuccessOrQuit(client.Get().SetHostName("host")); + SuccessOrQuit( + client.Get().RemoveHostAndServices(/* aRemoveKeyLease */ true, /* aSendUnregToServer */ true)); + nexus.AdvanceTime(2000); + + VerifyOrQuit(client.Get().GetHostInfo().GetName() == nullptr); + VerifyOrQuit(FindHost(server, "host") == nullptr); + + Log("Test passed successfully"); +} + +} // namespace Nexus +} // namespace ot + +int main(void) +{ + ot::Nexus::TestSrpClientRemoveHost(); + printf("All tests passed\n"); + return 0; +} diff --git a/tests/scripts/thread-cert/test_srp_client_remove_host.py b/tests/scripts/thread-cert/test_srp_client_remove_host.py deleted file mode 100755 index 681893914..000000000 --- a/tests/scripts/thread-cert/test_srp_client_remove_host.py +++ /dev/null @@ -1,176 +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 ipaddress -import unittest - -import command -import config -import thread_cert - -# Test description: -# This test verifies SRP client `RemoveHostAndServices()` behavior when host info is not yet registered. -# -# Topology: -# -# LEADER (SRP server) -# | -# | -# ROUTER (SRP client) -# - -SERVER = 1 -CLIENT = 2 - - -class SrpRemoveHost(thread_cert.TestCase): - USE_MESSAGE_FACTORY = False - SUPPORT_NCP = False - - TOPOLOGY = { - SERVER: { - 'name': 'SRP_SERVER', - 'mode': 'rdn', - }, - CLIENT: { - 'name': 'SRP_CLIENT', - 'mode': 'rdn', - }, - } - - def test(self): - server = self.nodes[SERVER] - client = self.nodes[CLIENT] - - #------------------------------------------------------------------------------------- - # Start the server & client devices. - - server.start() - self.simulator.go(config.LEADER_STARTUP_DELAY) - self.assertEqual(server.get_state(), 'leader') - - client.start() - self.simulator.go(config.ROUTER_STARTUP_DELAY) - self.assertEqual(client.get_state(), 'router') - - server.srp_server_set_enabled(True) - client.srp_client_enable_auto_start_mode() - self.simulator.go(15) - - #------------------------------------------------------------------------------------- - # Register a single service and verify that it worked. - - client.srp_client_set_host_name('host') - client.srp_client_set_host_address('2001::1') - client.srp_client_add_service('ins', '_srv._udp', 1977) - self.simulator.go(2) - - client_services = client.srp_client_get_services() - self.assertEqual(len(client_services), 1) - client_service = client_services[0] - self.assertEqual(client_service['instance'], 'ins') - self.assertEqual(client_service['name'], '_srv._udp') - self.assertEqual(int(client_service['port']), 1977) - self.assertEqual(int(client_service['priority']), 0) - self.assertEqual(int(client_service['weight']), 0) - self.assertEqual(client_service['state'], 'Registered') - - server_hosts = server.srp_server_get_hosts() - self.assertEqual(len(server_hosts), 1) - server_host = server_hosts[0] - self.assertEqual(server_host['fullname'], 'host.default.service.arpa.') - self.assertEqual(server_host['deleted'], 'false') - self.assertEqual(len(server_host['addresses']), 1) - self.assertIn('2001:0:0:0:0:0:0:1', server_host['addresses']) - - server_services = server.srp_server_get_services() - self.assertEqual(len(server_services), 1) - server_service = server_services[0] - self.assertEqual(server_service['fullname'], 'ins._srv._udp.default.service.arpa.') - self.assertEqual(server_service['instance'], 'ins') - self.assertEqual(server_service['name'], '_srv._udp') - self.assertEqual(server_service['deleted'], 'false') - self.assertEqual(int(server_service['port']), 1977) - self.assertEqual(int(server_service['priority']), 0) - self.assertEqual(int(server_service['weight']), 0) - self.assertEqual(server_service['host'], 'host') - - #------------------------------------------------------------------------------------- - # Clear the info on client, and verify that it is still present on server. - - client.srp_client_clear_host() - self.simulator.go(2) - - client_services = client.srp_client_get_services() - self.assertEqual(len(client_services), 0) - self.assertIsNone(client.srp_client_get_host_name()) - self.assertIsNone(client.srp_client_get_host_address()) - - server_services = server.srp_server_get_services() - self.assertEqual(len(server_services), 1) - - #------------------------------------------------------------------------------------- - # Now set the host name and request "host remove" requiring that client updates server - # even if host is not yet registered. Verify that client updates the server by checking - # that the host and service entries are marked as "deleted" on the server (i.e. deleted - # by the name (and associated key) are retained). - - client.srp_client_set_host_name('host') - client.srp_client_remove_host(send_unreg_to_server=True) - self.simulator.go(2) - - self.assertIsNone(client.srp_client_get_host_name()) - - server_hosts = server.srp_server_get_hosts() - self.assertEqual(len(server_hosts), 1) - server_host = server_hosts[0] - self.assertEqual(server_host['fullname'], 'host.default.service.arpa.') - self.assertEqual(server_host['deleted'], 'true') - - server_services = server.srp_server_get_services() - self.assertEqual(len(server_services), 1) - server_service = server_services[0] - self.assertEqual(server_service['fullname'], 'ins._srv._udp.default.service.arpa.') - self.assertEqual(server_service['deleted'], 'true') - - #------------------------------------------------------------------------------------- - # Again request "host remove" but this time request `remove_key`. Verify that entries - # on the server are fully removed . - - client.srp_client_set_host_name('host') - client.srp_client_remove_host(remove_key=True, send_unreg_to_server=True) - self.simulator.go(2) - - self.assertIsNone(client.srp_client_get_host_name()) - self.assertEqual(len(server.srp_server_get_services()), 0) - self.assertEqual(len(server.srp_server_get_hosts()), 0) - - -if __name__ == '__main__': - unittest.main()