From 524240fdb013e2828d66d1714e3bedc4af432d56 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Mon, 20 Apr 2026 00:09:35 -0700 Subject: [PATCH] [nexus] migrate dns_client_config_auto_start to nexus (#12936) This commit migrates the dns_client_config_auto_start test from the thread-cert Python-based test framework to the Nexus C++ simulation framework. The new Nexus test (test_dns_client_config_auto_start.cpp) verifies: - DNS client uses the SRP server address automatically when no explicit config is set. - Explicitly set DNS config takes precedence over auto-discovered SRP server address. - Clearing an explicit DNS config allows the client to fall back to the auto-discovered SRP server address. - DNS client updates its default config when the SRP server changes. Changes: - Added tests/nexus/test_dns_client_config_auto_start.cpp - Updated tests/nexus/CMakeLists.txt to build the new test - Updated tests/nexus/run_nexus_tests.sh to include it in default - Removed tests/scripts/thread-cert/test_dns_client_config_auto_start.py --- tests/nexus/CMakeLists.txt | 1 + .../test_dns_client_config_auto_start.cpp | 195 ++++++++++++++++++ .../test_dns_client_config_auto_start.py | 150 -------------- 3 files changed, 196 insertions(+), 150 deletions(-) create mode 100644 tests/nexus/test_dns_client_config_auto_start.cpp delete mode 100755 tests/scripts/thread-cert/test_dns_client_config_auto_start.py diff --git a/tests/nexus/CMakeLists.txt b/tests/nexus/CMakeLists.txt index aa5682b45..9cefce828 100644 --- a/tests/nexus/CMakeLists.txt +++ b/tests/nexus/CMakeLists.txt @@ -391,6 +391,7 @@ ot_nexus_test(border_agent_tracker "core;nexus") ot_nexus_test(child_supervision "core;nexus") ot_nexus_test(dataset_updater "core;nexus") ot_nexus_test(discover_scan "core;nexus") +ot_nexus_test(dns_client_config_auto_start "core;nexus") ot_nexus_test(dtls "core;nexus") ot_nexus_test(form_join "core;nexus") ot_nexus_test(ipv6_source_selection "core;nexus") diff --git a/tests/nexus/test_dns_client_config_auto_start.cpp b/tests/nexus/test_dns_client_config_auto_start.cpp new file mode 100644 index 000000000..1216c9c1d --- /dev/null +++ b/tests/nexus/test_dns_client_config_auto_start.cpp @@ -0,0 +1,195 @@ +/* + * 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 { + +/** + * Time to advance for a node to form a network and become leader, in milliseconds. + */ +static constexpr uint32_t kFormNetworkTime = 13 * 1000; + +/** + * Time to advance for a node to join a network, in milliseconds. + */ +static constexpr uint32_t kJoinNetworkTime = 10 * 1000; + +/** + * Time to advance for the network to stabilize, in milliseconds. + */ +static constexpr uint32_t kStabilizationTime = 15 * 1000; + +/** + * Default DNS server address for testing. + */ +static const char kDefaultAddress[] = "fd00:1:2:3:4:5:6:7"; + +void TestDnsClientConfigAutoStart(void) +{ + Core nexus; + + Node &leader = nexus.CreateNode(); + Node &router = nexus.CreateNode(); + + leader.SetName("LEADER"); + router.SetName("ROUTER"); + + SuccessOrQuit(Instance::SetGlobalLogLevel(kLogLevelNote)); + + Log("---------------------------------------------------------------------------------------"); + Log("Form network"); + + leader.Form(); + nexus.AdvanceTime(kFormNetworkTime); + VerifyOrQuit(leader.Get().IsLeader()); + + router.Join(leader); + nexus.AdvanceTime(kJoinNetworkTime + kStabilizationTime); + VerifyOrQuit(router.Get().IsRouter()); + + Log("---------------------------------------------------------------------------------------"); + Log("On leader set DNS config explicitly"); + + { + Dns::Client::QueryConfig dnsConfig = leader.Get().GetDefaultConfig(); + SuccessOrQuit(AsCoreType(&dnsConfig.mServerSockAddr.mAddress).FromString(kDefaultAddress)); + dnsConfig.mServerSockAddr.mPort = 53; + leader.Get().SetDefaultConfig(dnsConfig); + + const Dns::Client::QueryConfig ¤tConfig = leader.Get().GetDefaultConfig(); + VerifyOrQuit(currentConfig.GetServerSockAddr().GetAddress() == AsCoreType(&dnsConfig.mServerSockAddr.mAddress)); + VerifyOrQuit(currentConfig.GetServerSockAddr().GetPort() == 53); + } + + Log("---------------------------------------------------------------------------------------"); + Log("Start leader to act as SRP server"); + + leader.Get().SetEnabled(true); + + Log("---------------------------------------------------------------------------------------"); + Log("Enable SRP client auto start on both nodes"); + + leader.Get().EnableAutoStartMode(nullptr, nullptr); + router.Get().EnableAutoStartMode(nullptr, nullptr); + + nexus.AdvanceTime(kStabilizationTime); + + Log("---------------------------------------------------------------------------------------"); + Log("Verify that on router the default DNS config is changed to the SRP server address"); + + Ip6::Address srpServerAddress; + + srpServerAddress = router.Get().GetServerAddress().GetAddress(); + VerifyOrQuit(!srpServerAddress.IsUnspecified()); + VerifyOrQuit(srpServerAddress == leader.Get().GetServerAddress().GetAddress()); + + { + const Dns::Client::QueryConfig &dnsConfig = router.Get().GetDefaultConfig(); + VerifyOrQuit(dnsConfig.GetServerSockAddr().GetAddress() == srpServerAddress); + VerifyOrQuit(dnsConfig.GetServerSockAddr().GetPort() == 53); + } + + Log("---------------------------------------------------------------------------------------"); + Log("Verify that on leader the default DNS config remains as before"); + + { + const Dns::Client::QueryConfig &dnsConfig = leader.Get().GetDefaultConfig(); + Ip6::Address expectedAddress; + SuccessOrQuit(expectedAddress.FromString(kDefaultAddress)); + VerifyOrQuit(dnsConfig.GetServerSockAddr().GetAddress() == expectedAddress); + } + + Log("---------------------------------------------------------------------------------------"); + Log("On leader clear DNS config and verify it adopts the SRP server address"); + + { + Dns::Client::QueryConfig dnsConfig = leader.Get().GetDefaultConfig(); + AsCoreType(&dnsConfig.mServerSockAddr.mAddress).Clear(); + leader.Get().SetDefaultConfig(dnsConfig); + + const Dns::Client::QueryConfig ¤tConfig = leader.Get().GetDefaultConfig(); + VerifyOrQuit(currentConfig.GetServerSockAddr().GetAddress() == srpServerAddress); + } + + Log("---------------------------------------------------------------------------------------"); + Log("On leader set DNS config explicitly again"); + + { + Dns::Client::QueryConfig dnsConfig = leader.Get().GetDefaultConfig(); + SuccessOrQuit(AsCoreType(&dnsConfig.mServerSockAddr.mAddress).FromString(kDefaultAddress)); + leader.Get().SetDefaultConfig(dnsConfig); + } + + Log("---------------------------------------------------------------------------------------"); + Log("Stop SRP server on leader and start it on router"); + + leader.Get().SetEnabled(false); + router.Get().SetEnabled(true); + + nexus.AdvanceTime(kStabilizationTime); + + Log("---------------------------------------------------------------------------------------"); + Log("Verify that SRP client on router switched to new SRP server"); + + Ip6::Address newSrpServerAddress; + newSrpServerAddress = router.Get().GetServerAddress().GetAddress(); + VerifyOrQuit(!newSrpServerAddress.IsUnspecified()); + VerifyOrQuit(newSrpServerAddress != srpServerAddress); + VerifyOrQuit(newSrpServerAddress == leader.Get().GetServerAddress().GetAddress()); + + Log("---------------------------------------------------------------------------------------"); + Log("Verify that config on router gets changed while on leader it remains unchanged"); + + { + const Dns::Client::QueryConfig &dnsConfig = router.Get().GetDefaultConfig(); + VerifyOrQuit(dnsConfig.GetServerSockAddr().GetAddress() == newSrpServerAddress); + } + + { + const Dns::Client::QueryConfig &dnsConfig = leader.Get().GetDefaultConfig(); + Ip6::Address expectedAddress; + SuccessOrQuit(expectedAddress.FromString(kDefaultAddress)); + VerifyOrQuit(dnsConfig.GetServerSockAddr().GetAddress() == expectedAddress); + } +} + +} // namespace Nexus +} // namespace ot + +int main(void) +{ + ot::Nexus::TestDnsClientConfigAutoStart(); + printf("All tests passed\n"); + return 0; +} diff --git a/tests/scripts/thread-cert/test_dns_client_config_auto_start.py b/tests/scripts/thread-cert/test_dns_client_config_auto_start.py deleted file mode 100755 index 9b9a2e774..000000000 --- a/tests/scripts/thread-cert/test_dns_client_config_auto_start.py +++ /dev/null @@ -1,150 +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 unittest - -import command -import config -import thread_cert - -# Test description: -# -# This test verifies the behavior of DNS client feature where the SRP -# server address (from SRP client auto-start feature) is used as the -# default IPv6 address for DNS resolver config when user does not -# explicitly set a default address -# -# Topology: two nodes -# -# leader (SRP server & client & DNS client) --- router (SRP client & DNS client) -# - -LEADER = 1 -ROUTER = 2 - -DEFAULT_ADDRESS = 'fd00:1:2:3:4:5:6:7' - - -class DnsClientConfigAutoStart(thread_cert.TestCase): - USE_MESSAGE_FACTORY = False - SUPPORT_NCP = False - - TOPOLOGY = { - LEADER: { - 'name': 'LEADER', - 'mode': 'rdn', - }, - ROUTER: { - 'name': 'ROUTER', - 'mode': 'rdn', - }, - } - - def test(self): - leader = self.nodes[LEADER] - router = self.nodes[ROUTER] - - # Form network. - - leader.start() - self.simulator.go(config.LEADER_STARTUP_DELAY) - self.assertEqual(leader.get_state(), 'leader') - - router.start() - self.simulator.go(config.ROUTER_STARTUP_DELAY) - self.assertEqual(router.get_state(), 'router') - - # On leader set DNS config explicitly. - - leader.dns_set_config(DEFAULT_ADDRESS) - dns_config = leader.dns_get_config() - self.assertEqual(dns_config['Server'], '[{}]:53'.format(DEFAULT_ADDRESS)) - - # Start leader to act as SRP server. - - leader.srp_server_set_enabled(True) - - # Enable SRP client auto start on both nodes. - - leader.srp_client_enable_auto_start_mode() - router.srp_client_enable_auto_start_mode() - self.simulator.go(5) - - # Verify that on router the default DNS config is changed - # to the same address being used by SRP client. - - srp_server_address = router.srp_client_get_server_address() - self.assertEqual(leader.srp_client_get_server_address(), srp_server_address) - - dns_config = router.dns_get_config() - self.assertEqual(dns_config['Server'], '[{}]:53'.format(srp_server_address)) - - # Verify that on leader the default DNS config remains - # as before (the address explicitly set earlier). - - dns_config = leader.dns_get_config() - self.assertEqual(dns_config['Server'], '[{}]:53'.format(DEFAULT_ADDRESS)) - - # On leader clear DNS config (the explicitly set address) - # and verify that it adopts the SRP server address. - - leader.dns_set_config("::") - dns_config = leader.dns_get_config() - self.assertEqual(dns_config['Server'], '[{}]:53'.format(srp_server_address)) - - # On leader set DNS config explicitly again. - - leader.dns_set_config(DEFAULT_ADDRESS) - dns_config = leader.dns_get_config() - self.assertEqual(dns_config['Server'], '[{}]:53'.format(DEFAULT_ADDRESS)) - - # Stop SRP server on leader and start it on router. - - leader.srp_server_set_enabled(False) - router.srp_server_set_enabled(True) - self.simulator.go(5) - - # Verify that SRP client on router switched to new SRP server. - - self.assertNotEqual(srp_server_address, router.srp_client_get_server_address()) - self.assertEqual(router.srp_client_get_server_address(), leader.srp_client_get_server_address()) - - # Verify that config on router gets changed while on leader - # it remains unchanged. - - dns_config = router.dns_get_config() - srp_server_address = router.srp_client_get_server_address() - self.assertEqual(dns_config['Server'], '[{}]:53'.format(srp_server_address)) - - dns_config = leader.dns_get_config() - self.assertEqual(dns_config['Server'], '[{}]:53'.format(DEFAULT_ADDRESS)) - - -if __name__ == '__main__': - unittest.main()