From 2e1fbc1f7b8d83f01cfaef6e84d6c770fdc19d9f Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Wed, 29 Apr 2026 17:28:04 -0700 Subject: [PATCH] [nexus] migrate test_coaps.py to nexus (#13009) This commit migrates the functionality covered by `tests/scripts/thread-cert/test_coaps.py` to a new Nexus test `tests/nexus/test_coaps.cpp`. The new test covers: - CoAP Secure with PSK. - CoAP Secure with X.509 certificates. The X509 test is conditionally compiled based on `MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED`. The original Python test file is removed. --- tests/nexus/CMakeLists.txt | 1 + tests/nexus/test_coaps.cpp | 244 ++++++++++++++++++++++++ tests/scripts/thread-cert/test_coaps.py | 84 -------- 3 files changed, 245 insertions(+), 84 deletions(-) create mode 100644 tests/nexus/test_coaps.cpp delete mode 100755 tests/scripts/thread-cert/test_coaps.py diff --git a/tests/nexus/CMakeLists.txt b/tests/nexus/CMakeLists.txt index 58b7a8e93..beadf319b 100644 --- a/tests/nexus/CMakeLists.txt +++ b/tests/nexus/CMakeLists.txt @@ -393,6 +393,7 @@ ot_nexus_test(border_agent_tracker "core;nexus") ot_nexus_test(child_supervision "core;nexus") ot_nexus_test(coap_block "core;nexus") ot_nexus_test(coap_observe "core;nexus") +ot_nexus_test(coaps "core;nexus") ot_nexus_test(dataset_updater "core;nexus") ot_nexus_test(discover_scan "core;nexus") ot_nexus_test(dnssd "core;nexus") diff --git a/tests/nexus/test_coaps.cpp b/tests/nexus/test_coaps.cpp new file mode 100644 index 000000000..cd7c84c9d --- /dev/null +++ b/tests/nexus/test_coaps.cpp @@ -0,0 +1,244 @@ +/* + * 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 + +#include "platform/nexus_core.hpp" +#include "platform/nexus_node.hpp" + +#include "cli/x509_cert_key.hpp" + +namespace ot { +namespace Nexus { + +static bool sRequestHandlerCalled = false; +static bool sResponseHandlerCalled = false; + +static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) +{ + OT_UNUSED_VARIABLE(aMessageInfo); + Instance &instance = *static_cast(aContext); + Coap::Message &message = AsCoapMessage(aMessage); + Coap::Message *response = nullptr; + + sRequestHandlerCalled = true; + + Log("Received CoAP request"); + + response = instance.Get().NewMessage(); + VerifyOrQuit(response != nullptr); + SuccessOrQuit(response->InitAsResponse(Coap::kTypeAck, Coap::kCodeContent, message)); + SuccessOrQuit(response->AppendPayloadMarker()); + SuccessOrQuit(response->AppendBytes("hello", 5)); + + SuccessOrQuit(instance.Get().SendMessage(*response)); +} + +static void HandleResponse(void *aContext, Coap::Msg *aMsg, otError aError) +{ + OT_UNUSED_VARIABLE(aContext); + + sResponseHandlerCalled = true; + + Log("Received CoAP response, error: %d", aError); + VerifyOrQuit(aError == OT_ERROR_NONE); + VerifyOrQuit(aMsg != nullptr); + + Coap::Message &message = static_cast(aMsg->mMessage); + VerifyOrQuit(message.ReadCode() == Coap::kCodeContent); + + uint16_t length = message.GetLength() - message.GetOffset(); + char payload[10]; + VerifyOrQuit(length < sizeof(payload)); + message.ReadBytes(message.GetOffset(), payload, length); + payload[length] = '\0'; + Log("Payload: %s", payload); + VerifyOrQuit(strcmp(payload, "hello") == 0); +} + +void TestCoapsPsk(void) +{ + Core nexus; + + Node &leader = nexus.CreateNode(); + Node &router = nexus.CreateNode(); + + nexus.AdvanceTime(0); + + SuccessOrQuit(Instance::SetGlobalLogLevel(kLogLevelInfo)); + + Log("Form network"); + leader.Form(); + nexus.AdvanceTime(13 * 1000); + VerifyOrQuit(leader.Get().IsLeader()); + + router.Join(leader); + nexus.AdvanceTime(10 * 1000); + VerifyOrQuit(router.Get().IsChild() || router.Get().IsRouter()); + + const uint8_t kPsk[] = {'p', 's', 'k'}; + const char kPskIdentity[] = "pskIdentity"; + + Log("Start CoAPS on Leader (PSK)"); + leader.Get().SetPreSharedKey( + kPsk, sizeof(kPsk), reinterpret_cast(kPskIdentity), strlen(kPskIdentity)); + SuccessOrQuit(leader.Get().Open(OT_DEFAULT_COAP_SECURE_PORT)); + + Coap::Resource resource("test", &HandleRequest, &leader.GetInstance()); + leader.Get().AddResource(resource); + + Log("Start CoAPS on Router (PSK)"); + router.Get().SetPreSharedKey( + kPsk, sizeof(kPsk), reinterpret_cast(kPskIdentity), strlen(kPskIdentity)); + SuccessOrQuit(router.Get().Open(0)); + + Ip6::SockAddr sockaddr; + sockaddr.SetAddress(leader.Get().GetMeshLocalEid()); + sockaddr.SetPort(OT_DEFAULT_COAP_SECURE_PORT); + + Log("Connect Router to Leader"); + SuccessOrQuit(router.Get().Connect(sockaddr)); + + nexus.AdvanceTime(5 * 1000); + VerifyOrQuit(router.Get().IsConnected()); + + Log("Send GET request"); + Coap::Message *message = router.Get().NewMessage(); + VerifyOrQuit(message != nullptr); + SuccessOrQuit(message->Init(Coap::kTypeConfirmable, Coap::kCodeGet)); + SuccessOrQuit(message->AppendUriPathOptions("test")); + + sRequestHandlerCalled = false; + sResponseHandlerCalled = false; + + SuccessOrQuit(router.Get().SendMessage(*message, &HandleResponse, nullptr)); + + nexus.AdvanceTime(5 * 1000); + + VerifyOrQuit(sRequestHandlerCalled); + VerifyOrQuit(sResponseHandlerCalled); + + Log("Disconnect"); + router.Get().Disconnect(); + nexus.AdvanceTime(1 * 1000); + VerifyOrQuit(!router.Get().IsConnected()); + + leader.Get().RemoveResource(resource); + leader.Get().Close(); + router.Get().Close(); +} + +void TestCoapsX509(void) +{ + Core nexus; + + Node &leader = nexus.CreateNode(); + Node &router = nexus.CreateNode(); + + nexus.AdvanceTime(0); + + SuccessOrQuit(Instance::SetGlobalLogLevel(kLogLevelInfo)); + + Log("Form network"); + leader.Form(); + nexus.AdvanceTime(13 * 1000); + VerifyOrQuit(leader.Get().IsLeader()); + + router.Join(leader); + nexus.AdvanceTime(10 * 1000); + VerifyOrQuit(router.Get().IsChild() || router.Get().IsRouter()); + + Log("Start CoAPS on Leader (X509)"); + leader.Get().SetCertificate( + reinterpret_cast(OT_CLI_COAPS_X509_CERT), sizeof(OT_CLI_COAPS_X509_CERT), + reinterpret_cast(OT_CLI_COAPS_PRIV_KEY), sizeof(OT_CLI_COAPS_PRIV_KEY)); + leader.Get().SetCaCertificateChain( + reinterpret_cast(OT_CLI_COAPS_TRUSTED_ROOT_CERTIFICATE), + sizeof(OT_CLI_COAPS_TRUSTED_ROOT_CERTIFICATE)); + SuccessOrQuit(leader.Get().Open(OT_DEFAULT_COAP_SECURE_PORT)); + + Coap::Resource resource("test", &HandleRequest, &leader.GetInstance()); + leader.Get().AddResource(resource); + + Log("Start CoAPS on Router (X509)"); + router.Get().SetCertificate( + reinterpret_cast(OT_CLI_COAPS_X509_CERT), sizeof(OT_CLI_COAPS_X509_CERT), + reinterpret_cast(OT_CLI_COAPS_PRIV_KEY), sizeof(OT_CLI_COAPS_PRIV_KEY)); + router.Get().SetCaCertificateChain( + reinterpret_cast(OT_CLI_COAPS_TRUSTED_ROOT_CERTIFICATE), + sizeof(OT_CLI_COAPS_TRUSTED_ROOT_CERTIFICATE)); + SuccessOrQuit(router.Get().Open(0)); + + Ip6::SockAddr sockaddr; + sockaddr.SetAddress(leader.Get().GetMeshLocalEid()); + sockaddr.SetPort(OT_DEFAULT_COAP_SECURE_PORT); + + Log("Connect Router to Leader"); + SuccessOrQuit(router.Get().Connect(sockaddr)); + + nexus.AdvanceTime(5 * 1000); + VerifyOrQuit(router.Get().IsConnected()); + + Log("Send GET request"); + Coap::Message *message = router.Get().NewMessage(); + VerifyOrQuit(message != nullptr); + SuccessOrQuit(message->Init(Coap::kTypeConfirmable, Coap::kCodeGet)); + SuccessOrQuit(message->AppendUriPathOptions("test")); + + sRequestHandlerCalled = false; + sResponseHandlerCalled = false; + + SuccessOrQuit(router.Get().SendMessage(*message, &HandleResponse, nullptr)); + + nexus.AdvanceTime(5 * 1000); + + VerifyOrQuit(sRequestHandlerCalled); + VerifyOrQuit(sResponseHandlerCalled); + + Log("Disconnect"); + router.Get().Disconnect(); + nexus.AdvanceTime(1 * 1000); + VerifyOrQuit(!router.Get().IsConnected()); + + leader.Get().RemoveResource(resource); + leader.Get().Close(); + router.Get().Close(); +} + +} // namespace Nexus +} // namespace ot + +int main(void) +{ + ot::Nexus::TestCoapsPsk(); + ot::Nexus::TestCoapsX509(); + printf("All tests passed\n"); + return 0; +} diff --git a/tests/scripts/thread-cert/test_coaps.py b/tests/scripts/thread-cert/test_coaps.py deleted file mode 100755 index 50e3255f7..000000000 --- a/tests/scripts/thread-cert/test_coaps.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (c) 2018, 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 config -import thread_cert - -LEADER = 1 -ROUTER = 2 - - -class TestCoaps(thread_cert.TestCase): - SUPPORT_NCP = False - - TOPOLOGY = { - LEADER: { - 'mode': 'rdn', - 'allowlist': [ROUTER] - }, - ROUTER: { - 'mode': 'rdn', - 'allowlist': [LEADER] - }, - } - - def test(self): - 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') - - mleid = self.nodes[LEADER].get_ip6_address(config.ADDRESS_TYPE.ML_EID) - - self.nodes[LEADER].coaps_start_psk('psk', 'pskIdentity') - self.nodes[LEADER].coaps_set_resource_path('test') - self.nodes[ROUTER].coaps_start_psk('psk', 'pskIdentity') - self.nodes[ROUTER].coaps_connect(mleid) - self.nodes[ROUTER].coaps_get() - self.nodes[ROUTER].coaps_disconnect() - self.nodes[ROUTER].coaps_stop() - self.nodes[LEADER].coaps_stop() - - self.nodes[LEADER].coaps_start_x509() - # self.nodes[LEADER].coaps_set_resource_path('test') - self.nodes[ROUTER].coaps_start_x509() - self.nodes[ROUTER].coaps_connect(mleid) - self.nodes[ROUTER].coaps_get() - self.nodes[ROUTER].coaps_disconnect() - self.nodes[ROUTER].coaps_stop() - self.nodes[LEADER].coaps_stop() - - -if __name__ == '__main__': - unittest.main()