diff --git a/tests/nexus/CMakeLists.txt b/tests/nexus/CMakeLists.txt index 6708dd717..b1e7d4dc5 100644 --- a/tests/nexus/CMakeLists.txt +++ b/tests/nexus/CMakeLists.txt @@ -391,6 +391,7 @@ ot_nexus_test(border_admitter "core;nexus") ot_nexus_test(border_agent "core;nexus") 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(dataset_updater "core;nexus") ot_nexus_test(discover_scan "core;nexus") ot_nexus_test(dnssd "core;nexus") diff --git a/tests/nexus/openthread-core-nexus-config.h b/tests/nexus/openthread-core-nexus-config.h index c99fe672b..4359b1947 100644 --- a/tests/nexus/openthread-core-nexus-config.h +++ b/tests/nexus/openthread-core-nexus-config.h @@ -60,6 +60,7 @@ #define OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE 1 #define OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE 1 #define OPENTHREAD_CONFIG_COAP_API_ENABLE 1 +#define OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE 1 #define OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE 1 #define OPENTHREAD_CONFIG_COMMISSIONER_ENABLE 1 #define OPENTHREAD_CONFIG_COMMISSIONER_MAX_JOINER_ENTRIES 4 diff --git a/tests/nexus/test_coap_block.cpp b/tests/nexus/test_coap_block.cpp new file mode 100644 index 000000000..7bae77fd6 --- /dev/null +++ b/tests/nexus/test_coap_block.cpp @@ -0,0 +1,228 @@ +/* + * 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" + +namespace ot { +namespace Nexus { + +static otError TransmitHook(void *aContext, uint8_t *aBlock, uint32_t aPosition, uint16_t *aBlockLength, bool *aMore); + +static bool sRequestHandlerCalled = false; +static bool sReceiveHookCalled = false; +static bool sTransmitHookCalled = false; + +static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) +{ + Instance &instance = *static_cast(aContext); + Coap::Message &message = AsCoapMessage(aMessage); + Coap::Code code = static_cast(message.ReadCode()); + Coap::Message *response = nullptr; + + sRequestHandlerCalled = true; + + if (code == Coap::kCodeGet) + { + response = instance.Get().NewMessage(); + VerifyOrQuit(response != nullptr); + SuccessOrQuit(response->InitAsResponse(Coap::kTypeAck, Coap::kCodeContent, message)); + + Coap::Option::Iterator iterator; + SuccessOrQuit(iterator.Init(message, Coap::kOptionBlock2)); + if (iterator.GetOption() != nullptr) + { + uint64_t blockValue = 0; + SuccessOrQuit(iterator.ReadOptionValue(blockValue)); + uint32_t blockNum = static_cast(blockValue >> 4); + bool more = (blockNum < 2); // We send 3 blocks in total (0, 1, 2) + + Coap::BlockInfo blockInfo; + blockInfo.mBlockNumber = blockNum; + blockInfo.mBlockSzx = static_cast(blockValue & 0x7); + blockInfo.mMoreBlocks = more; + + SuccessOrQuit(response->AppendBlockOption(Coap::kOptionBlock2, blockInfo)); + } + + SuccessOrQuit(instance.Get().SendMessageWithResponseHandlerSeparateParams( + *response, AsCoreType(aMessageInfo), nullptr, nullptr, &TransmitHook, nullptr, nullptr)); + } + else + { + response = instance.Get().NewMessage(); + VerifyOrQuit(response != nullptr); + SuccessOrQuit(response->InitAsResponse(Coap::kTypeAck, Coap::kCodeChanged, message)); + SuccessOrQuit(instance.Get().SendMessage(*response, AsCoreType(aMessageInfo))); + } +} + +static otError ReceiveHook(void *aContext, + const uint8_t *aBlock, + uint32_t aPosition, + uint16_t aBlockLength, + bool aMore, + uint32_t aTotalLength) +{ + OT_UNUSED_VARIABLE(aContext); + OT_UNUSED_VARIABLE(aBlock); + OT_UNUSED_VARIABLE(aPosition); + OT_UNUSED_VARIABLE(aBlockLength); + OT_UNUSED_VARIABLE(aMore); + OT_UNUSED_VARIABLE(aTotalLength); + sReceiveHookCalled = true; + return OT_ERROR_NONE; +} + +static otError TransmitHook(void *aContext, uint8_t *aBlock, uint32_t aPosition, uint16_t *aBlockLength, bool *aMore) +{ + OT_UNUSED_VARIABLE(aContext); + OT_UNUSED_VARIABLE(aBlock); + OT_UNUSED_VARIABLE(aPosition); + + // Provide some data + memset(aBlock, 0xaa, *aBlockLength); + + const uint32_t kTotalLength = 48; // 3 blocks of 16 bytes + + if (aPosition + *aBlockLength >= kTotalLength) + { + *aMore = false; + *aBlockLength = kTotalLength - aPosition; + } + else + { + *aMore = true; + } + + sTransmitHookCalled = true; + return OT_ERROR_NONE; +} + +void TestCoapBlock(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()); + + // Start CoAP on Leader + SuccessOrQuit(leader.Get().Start(OT_DEFAULT_COAP_PORT)); + + Coap::ResourceBlockWise resource("test", &HandleRequest, &leader.GetInstance(), &ReceiveHook, &TransmitHook); + + leader.Get().AddBlockWiseResource(resource); + + // Start CoAP on Router + SuccessOrQuit(router.Get().Start(OT_DEFAULT_COAP_PORT)); + + // Router sends GET request + Coap::Message *message = router.Get().NewMessage(); + VerifyOrQuit(message != nullptr); + SuccessOrQuit(message->Init(Coap::kTypeConfirmable, Coap::kCodeGet)); + SuccessOrQuit(message->AppendUriPathOptions("test")); + + Coap::BlockInfo blockInfo; + blockInfo.mBlockNumber = 0; + blockInfo.mBlockSzx = Coap::kBlockSzx16; + blockInfo.mMoreBlocks = false; + SuccessOrQuit(message->AppendBlockOption(Coap::kOptionBlock2, blockInfo)); + + Ip6::MessageInfo messageInfo; + messageInfo.SetPeerAddr(leader.Get().GetMeshLocalEid()); + messageInfo.SetPeerPort(OT_DEFAULT_COAP_PORT); + + sRequestHandlerCalled = false; + sTransmitHookCalled = false; + sReceiveHookCalled = false; + + SuccessOrQuit(router.Get().SendMessageWithResponseHandlerSeparateParams( + *message, messageInfo, nullptr, nullptr, &TransmitHook, &ReceiveHook, nullptr)); + + nexus.AdvanceTime(5 * 1000); + + // Verify hooks were called + VerifyOrQuit(sRequestHandlerCalled); + VerifyOrQuit(sTransmitHookCalled); // Leader should transmit blocks + VerifyOrQuit(sReceiveHookCalled); // Router should receive blocks + + // Router sends PUT request + message = router.Get().NewMessage(); + VerifyOrQuit(message != nullptr); + SuccessOrQuit(message->Init(Coap::kTypeConfirmable, Coap::kCodePut)); + SuccessOrQuit(message->AppendUriPathOptions("test")); + + blockInfo.mBlockNumber = 0; + blockInfo.mBlockSzx = Coap::kBlockSzx16; + blockInfo.mMoreBlocks = true; + SuccessOrQuit(message->AppendBlockOption(Coap::kOptionBlock1, blockInfo)); + + sRequestHandlerCalled = false; + sTransmitHookCalled = false; + sReceiveHookCalled = false; + + SuccessOrQuit(router.Get().SendMessageWithResponseHandlerSeparateParams( + *message, messageInfo, nullptr, nullptr, &TransmitHook, &ReceiveHook, nullptr)); + + nexus.AdvanceTime(5 * 1000); + + VerifyOrQuit(sRequestHandlerCalled); + VerifyOrQuit(sTransmitHookCalled); // Router should transmit blocks + VerifyOrQuit(sReceiveHookCalled); // Leader should receive blocks + + leader.Get().RemoveBlockWiseResource(resource); + IgnoreError(leader.Get().Stop()); + IgnoreError(router.Get().Stop()); +} + +} // namespace Nexus +} // namespace ot + +int main(void) +{ + ot::Nexus::TestCoapBlock(); + printf("All tests passed\n"); + return 0; +} diff --git a/tests/scripts/thread-cert/test_coap_block.py b/tests/scripts/thread-cert/test_coap_block.py deleted file mode 100755 index 672b96323..000000000 --- a/tests/scripts/thread-cert/test_coap_block.py +++ /dev/null @@ -1,108 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (c) 2020, 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 pexpect -import config -import thread_cert - -LEADER = 1 -ROUTER = 2 - - -class TestCoapBlockTransfer(thread_cert.TestCase): - """ - Test suite for CoAP Block-Wise Transfers (RFC7959). - """ - - SUPPORT_NCP = False - - TOPOLOGY = { - LEADER: { - 'mode': 'rdn', - 'whitelist': [ROUTER] - }, - ROUTER: { - 'mode': 'rdn', - 'whitelist': [LEADER] - }, - } - - def test(self): - leader = self.nodes[LEADER] - router = self.nodes[ROUTER] - - 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') - - leader_mleid = leader.get_ip6_address(config.ADDRESS_TYPE.ML_EID) - router_mleid = router.get_ip6_address(config.ADDRESS_TYPE.ML_EID) - - leader.coap_set_resource_path_block('test', 3) - - leader.coap_start() - router.coap_start() - - for method in ['GET', 'PUT', 'POST']: - if method == 'GET': - response = router.coap_get_block(leader_mleid, 'test', size=32) - elif method == 'PUT': - response = router.coap_put_block(leader_mleid, 'test', size=256, count=2) - elif method == 'POST': - response = router.coap_post_block(leader_mleid, 'test', size=1024, count=2) - else: - raise ValueError(method) - - self.assertEqual(response['source'], leader_mleid) - - request = leader.coap_wait_request() - self.assertEqual(request['method'], method) - self.assertEqual(request['source'], router_mleid) - - if method == 'GET': - self.assertIsNotNone(response['payload']) - self.assertIsNone(request['payload']) - else: - self.assertIsNone(response['payload']) - self.assertIsNotNone(request['payload']) - - self.simulator.go(10) - - router.coap_stop() - leader.coap_stop() - - -if __name__ == '__main__': - unittest.main()