From c5971f7080f9f8e33bb3da54db1c66a2319cac2a Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 7 Oct 2025 14:49:16 -0700 Subject: [PATCH] [border-agent] check that BA is enabled in `RegisterService()` (#11993) This change adds a check at the beginning of `RegisterService()` to ensure that the border agent is enabled before attempting to register a service. Previously, methods like `SetServiceBaseName()` would call `RegisterService()` regardless of whether the agent was enabled, leading to unintended service registration attempts on a disabled agent. This commit also adds a new test case to verify that changing the service base name on a disabled border agent does not result in a service being registered. --- src/core/meshcop/border_agent.cpp | 1 + tests/nexus/test_border_agent.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index ff6635cbf..2f14be12f 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -466,6 +466,7 @@ void BorderAgent::RegisterService(void) uint16_t txtDataBufferSize; uint16_t txtDataLength; + VerifyOrExit(IsEnabled()); VerifyOrExit(Get().IsReady()); // Allocate a large enough buffer to fit both the TXT data diff --git a/tests/nexus/test_border_agent.cpp b/tests/nexus/test_border_agent.cpp index 51fe3502c..08794d04c 100644 --- a/tests/nexus/test_border_agent.cpp +++ b/tests/nexus/test_border_agent.cpp @@ -1786,6 +1786,26 @@ void TestBorderAgentServiceRegistration(void) sBrowseOutcomes.Clear(); + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + Log("Change the base service name while agent is disabled and validate no service is registered"); + + SuccessOrQuit(node0.Get().SetServiceBaseName("NewName")); + + VerifyOrQuit(!node0.Get().IsEnabled()); + + nexus.AdvanceTime(30 * Time::kOneSecondInMsec); + + iterator = node0.Get().AllocateIterator(); + VerifyOrQuit(iterator != nullptr); + + VerifyOrQuit(node0.Get().GetNextService(*iterator, service, entryState) == kErrorNotFound); + + node0.Get().FreeIterator(*iterator); + + VerifyOrQuit(sBrowseOutcomes.IsEmpty()); + + SuccessOrQuit(node0.Get().SetServiceBaseName("OpenThreadAgent")); + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Log("Re-enable Border Agent and validate that service is registered again");