From 1ff1f9145dcb28ba0b862105d0995490481c2889 Mon Sep 17 00:00:00 2001 From: Zang MingJie Date: Wed, 27 Oct 2021 10:02:06 +0800 Subject: [PATCH] Extract duplicated code for app test cases into AppTestContext (#10968) --- src/app/tests/AppTestContext.cpp | 47 +++++++ src/app/tests/AppTestContext.h | 57 +++++++++ src/app/tests/BUILD.gn | 22 +++- src/app/tests/TestCommandInteraction.cpp | 116 +++++++----------- src/app/tests/TestEventLogging.cpp | 95 +++++++------- src/app/tests/TestInteractionModelEngine.cpp | 77 ++++-------- src/app/tests/TestReadInteraction.cpp | 99 ++++++--------- src/app/tests/TestReportingEngine.cpp | 84 ++++--------- src/app/tests/TestWriteInteraction.cpp | 52 ++------ src/controller/tests/data_model/BUILD.gn | 1 + .../tests/data_model/TestCommands.cpp | 75 ++++------- src/controller/tests/data_model/TestRead.cpp | 72 +++-------- src/controller/tests/data_model/TestWrite.cpp | 60 ++------- src/messaging/tests/MessagingContext.cpp | 2 +- src/messaging/tests/MessagingContext.h | 2 +- src/messaging/tests/TestExchangeMgr.cpp | 4 +- .../tests/TestReliableMessageProtocol.cpp | 6 +- .../secure_channel/tests/TestCASESession.cpp | 4 +- .../secure_channel/tests/TestPASESession.cpp | 4 +- .../raw/tests/NetworkTestHelpers.cpp | 3 +- src/transport/raw/tests/NetworkTestHelpers.h | 5 +- src/transport/raw/tests/TestTCP.cpp | 2 +- src/transport/tests/TestSessionManager.cpp | 2 +- 23 files changed, 371 insertions(+), 520 deletions(-) create mode 100644 src/app/tests/AppTestContext.cpp create mode 100644 src/app/tests/AppTestContext.h diff --git a/src/app/tests/AppTestContext.cpp b/src/app/tests/AppTestContext.cpp new file mode 100644 index 00000000000000..2d3d1b20fbca8c --- /dev/null +++ b/src/app/tests/AppTestContext.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include + +namespace chip { +namespace Test { + +CHIP_ERROR AppContext::Init() +{ + ReturnErrorOnFailure(chip::Platform::MemoryInit()); + ReturnErrorOnFailure(mIOContext.Init()); + ReturnErrorOnFailure(mTransportManager.Init("LOOPBACK")); + ReturnErrorOnFailure(MessagingContext::Init(&mTransportManager, &mIOContext)); + ReturnErrorOnFailure(chip::app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), nullptr)); + + return CHIP_NO_ERROR; +} + +CHIP_ERROR AppContext::Shutdown() +{ + ReturnErrorOnFailure(MessagingContext::Shutdown()); + ReturnErrorOnFailure(mIOContext.Shutdown()); + chip::Platform::MemoryShutdown(); + + return CHIP_NO_ERROR; +} + +} // namespace Test +} // namespace chip diff --git a/src/app/tests/AppTestContext.h b/src/app/tests/AppTestContext.h new file mode 100644 index 00000000000000..469dbf23c5c09c --- /dev/null +++ b/src/app/tests/AppTestContext.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +#include + +namespace chip { +namespace Test { + +/** + * @brief The context of test cases for messaging layer. It wil initialize network layer and system layer, and create + * two secure sessions, connected with each other. Exchanges can be created for each secure session. + */ +class AppContext : public MessagingContext +{ +public: + /// Initialize the underlying layers and test suite pointer + CHIP_ERROR Init(); + + // Shutdown all layers, finalize operations + CHIP_ERROR Shutdown(); + + static int Initialize(void * context) + { + auto * ctx = static_cast(context); + return ctx->Init() == CHIP_NO_ERROR ? SUCCESS : FAILURE; + } + + static int Finalize(void * context) + { + auto * ctx = static_cast(context); + return ctx->Shutdown() == CHIP_NO_ERROR ? SUCCESS : FAILURE; + } + +private: + chip::TransportMgr mTransportManager; + chip::Test::IOContext mIOContext; +}; + +} // namespace Test +} // namespace chip diff --git a/src/app/tests/BUILD.gn b/src/app/tests/BUILD.gn index 12ad872bbe47c7..9bfd53e0622530 100644 --- a/src/app/tests/BUILD.gn +++ b/src/app/tests/BUILD.gn @@ -19,6 +19,24 @@ import("//build_overrides/nlunit_test.gni") import("${chip_root}/build/chip/chip_test_suite.gni") import("${chip_root}/src/platform/device.gni") +static_library("helpers") { + output_name = "libAppTestHelpers" + output_dir = "${root_out_dir}/lib" + + sources = [ + "AppTestContext.cpp", + "AppTestContext.h", + ] + + cflags = [ "-Wconversion" ] + + deps = [ + "${chip_root}/src/lib/support", + "${chip_root}/src/messaging/tests:helpers", + "${chip_root}/src/transport/raw/tests:helpers", + ] +} + chip_test_suite("tests") { output_name = "libAppTests" @@ -49,11 +67,9 @@ chip_test_suite("tests") { public_deps = [ "${chip_root}/src/app", "${chip_root}/src/app/common:cluster-objects", + "${chip_root}/src/app/tests:helpers", "${chip_root}/src/app/util:device_callbacks_manager", "${chip_root}/src/lib/core", - "${chip_root}/src/messaging/tests:helpers", - "${chip_root}/src/protocols", - "${chip_root}/src/transport/raw/tests:helpers", "${nlunit_test_root}:nlunit-test", ] diff --git a/src/app/tests/TestCommandInteraction.cpp b/src/app/tests/TestCommandInteraction.cpp index d40a0ed62529ca..8b64654e94c84c 100644 --- a/src/app/tests/TestCommandInteraction.cpp +++ b/src/app/tests/TestCommandInteraction.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -36,34 +37,21 @@ #include #include #include -#include #include #include -#include -#include -#include #include #include -#include -#include -#include #include -using namespace chip; +using TestContext = chip::Test::AppContext; + namespace chip { namespace { -chip::TransportMgrBase gTransportManager; -chip::Test::LoopbackTransport gLoopback; -chip::Test::IOContext gIOContext; -Messaging::ExchangeManager * gExchangeManager; -secure_channel::MessageCounterManager gMessageCounterManager; FabricIndex gFabricIndex = 0; bool isCommandDispatched = false; -using TestContext = chip::Test::MessagingContext; -TestContext sContext; bool sendResponse = true; constexpr EndpointId kTestEndpointId = 1; @@ -294,9 +282,10 @@ void TestCommandInteraction::AddCommandDataIB(nlTestSuite * apSuite, void * apCo void TestCommandInteraction::TestCommandSenderWithWrongState(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; + TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; - app::CommandSender commandSender(&mockCommandSenderDelegate, gExchangeManager); + app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager()); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); err = commandSender.SendCommandRequest(kTestDeviceNodeId, gFabricIndex, Optional::Missing()); @@ -305,6 +294,7 @@ void TestCommandInteraction::TestCommandSenderWithWrongState(nlTestSuite * apSui void TestCommandInteraction::TestCommandHandlerWithWrongState(nlTestSuite * apSuite, void * apContext) { + TestContext & ctx = *static_cast(apContext); CHIP_ERROR err = CHIP_NO_ERROR; auto commandPathParams = MakeTestCommandPath(); @@ -313,7 +303,7 @@ void TestCommandInteraction::TestCommandHandlerWithWrongState(nlTestSuite * apSu err = commandHandler.PrepareCommand(commandPathParams); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - auto exchangeCtx = gExchangeManager->NewContext(SessionHandle(0, 0, 0, 0), nullptr); + auto exchangeCtx = ctx.GetExchangeManager().NewContext(SessionHandle(0, 0, 0, 0), nullptr); commandHandler.mpExchangeCtx = exchangeCtx; TestExchangeDelegate delegate; commandHandler.mpExchangeCtx->SetDelegate(&delegate); @@ -324,9 +314,10 @@ void TestCommandInteraction::TestCommandHandlerWithWrongState(nlTestSuite * apSu void TestCommandInteraction::TestCommandSenderWithSendCommand(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; + TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; - app::CommandSender commandSender(&mockCommandSenderDelegate, gExchangeManager); + app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager()); System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize); @@ -341,13 +332,14 @@ void TestCommandInteraction::TestCommandSenderWithSendCommand(nlTestSuite * apSu void TestCommandInteraction::TestCommandHandlerWithSendEmptyCommand(nlTestSuite * apSuite, void * apContext) { + TestContext & ctx = *static_cast(apContext); CHIP_ERROR err = CHIP_NO_ERROR; auto commandPathParams = MakeTestCommandPath(); app::CommandHandler commandHandler(&mockCommandHandlerDelegate); System::PacketBufferHandle commandDatabuf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize); - auto exchangeCtx = gExchangeManager->NewContext(SessionHandle(0, 0, 0, 0), nullptr); + auto exchangeCtx = ctx.GetExchangeManager().NewContext(SessionHandle(0, 0, 0, 0), nullptr); commandHandler.mpExchangeCtx = exchangeCtx; TestExchangeDelegate delegate; @@ -362,9 +354,10 @@ void TestCommandInteraction::TestCommandHandlerWithSendEmptyCommand(nlTestSuite void TestCommandInteraction::TestCommandSenderWithProcessReceivedMsg(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; + TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; - app::CommandSender commandSender(&mockCommandSenderDelegate, gExchangeManager); + app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager()); System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize); @@ -375,11 +368,12 @@ void TestCommandInteraction::TestCommandSenderWithProcessReceivedMsg(nlTestSuite void TestCommandInteraction::ValidateCommandHandlerWithSendCommand(nlTestSuite * apSuite, void * apContext, bool aNeedStatusCode) { - CHIP_ERROR err = CHIP_NO_ERROR; + TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; app::CommandHandler commandHandler(&mockCommandHandlerDelegate); System::PacketBufferHandle commandPacket; - auto exchangeCtx = gExchangeManager->NewContext(SessionHandle(0, 0, 0, 0), nullptr); + auto exchangeCtx = ctx.GetExchangeManager().NewContext(SessionHandle(0, 0, 0, 0), nullptr); commandHandler.mpExchangeCtx = exchangeCtx; TestExchangeDelegate delegate; @@ -422,11 +416,12 @@ struct Fields void TestCommandInteraction::TestCommandHandlerCommandDataEncoding(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; + TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; app::CommandHandler commandHandler(nullptr); System::PacketBufferHandle commandPacket; - commandHandler.mpExchangeCtx = gExchangeManager->NewContext(SessionHandle(0, 0, 0, 0), nullptr); + commandHandler.mpExchangeCtx = ctx.GetExchangeManager().NewContext(SessionHandle(0, 0, 0, 0), nullptr); TestExchangeDelegate delegate; commandHandler.mpExchangeCtx->SetDelegate(&delegate); @@ -500,11 +495,11 @@ void TestCommandInteraction::TestCommandHandlerWithProcessReceivedEmptyDataMsg(n void TestCommandInteraction::TestCommandSenderCommandSuccessResponseFlow(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; mockCommandSenderDelegate.ResetCounter(); - app::CommandSender commandSender(&mockCommandSenderDelegate, gExchangeManager); + app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager()); AddCommandDataIB(apSuite, apContext, &commandSender, false); err = commandSender.SendCommandRequest(0, 0, Optional(ctx.GetSessionBobToAlice())); @@ -514,16 +509,16 @@ void TestCommandInteraction::TestCommandSenderCommandSuccessResponseFlow(nlTestS mockCommandSenderDelegate.onErrorCalledTimes == 0); NL_TEST_ASSERT(apSuite, GetNumActiveHandlerObjects() == 0); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestCommandInteraction::TestCommandSenderCommandSpecificResponseFlow(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; mockCommandSenderDelegate.ResetCounter(); - app::CommandSender commandSender(&mockCommandSenderDelegate, gExchangeManager); + app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager()); AddCommandDataIB(apSuite, apContext, &commandSender, false, kTestCommandIdCommandSpecificResponse); err = commandSender.SendCommandRequest(0, 0, Optional(ctx.GetSessionBobToAlice())); @@ -533,16 +528,16 @@ void TestCommandInteraction::TestCommandSenderCommandSpecificResponseFlow(nlTest mockCommandSenderDelegate.onErrorCalledTimes == 0); NL_TEST_ASSERT(apSuite, GetNumActiveHandlerObjects() == 0); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestCommandInteraction::TestCommandSenderCommandFailureResponseFlow(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; mockCommandSenderDelegate.ResetCounter(); - app::CommandSender commandSender(&mockCommandSenderDelegate, gExchangeManager); + app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager()); AddCommandDataIB(apSuite, apContext, &commandSender, false, kTestNonExistCommandId); err = commandSender.SendCommandRequest(0, 0, Optional(ctx.GetSessionBobToAlice())); @@ -552,13 +547,13 @@ void TestCommandInteraction::TestCommandSenderCommandFailureResponseFlow(nlTestS mockCommandSenderDelegate.onErrorCalledTimes == 1); NL_TEST_ASSERT(apSuite, GetNumActiveHandlerObjects() == 0); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestCommandInteraction::TestCommandSenderAbruptDestruction(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; // // Don't send back a response, just keep the CommandHandler @@ -569,7 +564,7 @@ void TestCommandInteraction::TestCommandSenderAbruptDestruction(nlTestSuite * ap mockCommandSenderDelegate.ResetCounter(); { - app::CommandSender commandSender(&mockCommandSenderDelegate, gExchangeManager); + app::CommandSender commandSender(&mockCommandSenderDelegate, &ctx.GetExchangeManager()); AddCommandDataIB(apSuite, apContext, &commandSender, false, kTestCommandIdCommandSpecificResponse); err = commandSender.SendCommandRequest(0, 0, Optional(ctx.GetSessionBobToAlice())); @@ -582,13 +577,13 @@ void TestCommandInteraction::TestCommandSenderAbruptDestruction(nlTestSuite * ap mockCommandSenderDelegate.onResponseCalledTimes == 0 && mockCommandSenderDelegate.onFinalCalledTimes == 0 && mockCommandSenderDelegate.onErrorCalledTimes == 0); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 1); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 1); } // // Upon the sender being destructed by the application, our exchange should get cleaned up too. // - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); NL_TEST_ASSERT(apSuite, GetNumActiveHandlerObjects() == 0); } @@ -620,51 +615,22 @@ const nlTest sTests[] = }; // clang-format on -int Initialize(void * aContext); -int Finalize(void * aContext); - // clang-format off nlTestSuite sSuite = { - "TestReadInteraction", - &sTests[0], - Initialize, - Finalize + "TestReadInteraction", + &sTests[0], + TestContext::Initialize, + TestContext::Finalize }; // clang-format on -int Initialize(void * aContext) -{ - // Initialize System memory and resources - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gTransportManager.Init(&gLoopback) == CHIP_NO_ERROR, FAILURE); - - auto * ctx = static_cast(aContext); - VerifyOrReturnError(ctx->Init(&sSuite, &gTransportManager, &gIOContext) == CHIP_NO_ERROR, FAILURE); - - gTransportManager.SetSessionManager(&ctx->GetSecureSessionManager()); - gExchangeManager = &ctx->GetExchangeManager(); - VerifyOrReturnError( - chip::app::InteractionModelEngine::GetInstance()->Init(&ctx->GetExchangeManager(), nullptr) == CHIP_NO_ERROR, FAILURE); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - // Shutdown will ensure no leaked exchange context. - CHIP_ERROR err = reinterpret_cast(aContext)->Shutdown(); - gIOContext.Shutdown(); - chip::Platform::MemoryShutdown(); - return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; -} - } // namespace int TestCommandInteraction() { - nlTestRunner(&sSuite, &sContext); - + TestContext gContext; + nlTestRunner(&sSuite, &gContext); return (nlTestRunnerStats(&sSuite)); } diff --git a/src/app/tests/TestEventLogging.cpp b/src/app/tests/TestEventLogging.cpp index 699016e4fa6114..0511a183f56d1b 100644 --- a/src/app/tests/TestEventLogging.cpp +++ b/src/app/tests/TestEventLogging.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -34,16 +35,9 @@ #include #include #include -#include #include #include -#include -#include -#include -#include #include -#include -#include #include @@ -55,49 +49,45 @@ static const chip::ClusterId kLivenessClusterId = 0x00000022; static const uint32_t kLivenessChangeEvent = 1; static const chip::EndpointId kTestEndpointId = 2; static const chip::TLV::Tag kLivenessDeviceStatus = chip::TLV::ContextTag(1); -static chip::TransportMgr gTransportManager; -static chip::System::LayerImpl gSystemLayer; static uint8_t gDebugEventBuffer[128]; static uint8_t gInfoEventBuffer[128]; static uint8_t gCritEventBuffer[128]; static chip::app::CircularEventBuffer gCircularEventBuffer[3]; -chip::SessionManager gSessionManager; -chip::Messaging::ExchangeManager gExchangeManager; -chip::secure_channel::MessageCounterManager gMessageCounterManager; - -void InitializeChip(nlTestSuite * apSuite) +class TestContext : public chip::Test::AppContext { - CHIP_ERROR err = CHIP_NO_ERROR; - chip::Optional peer(chip::Transport::Type::kUndefined); +public: + static int Initialize(void * context) + { + if (AppContext::Initialize(context) != SUCCESS) + return FAILURE; - err = chip::Platform::MemoryInit(); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + auto * ctx = static_cast(context); - gSystemLayer.Init(); + chip::app::LogStorageResources logStorageResources[] = { + { &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Debug }, + { &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Info }, + { &gCritEventBuffer[0], sizeof(gCritEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Critical }, + }; - err = gSessionManager.Init(&gSystemLayer, &gTransportManager, &gMessageCounterManager); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + chip::app::EventManagement::CreateEventManagement(&ctx->GetExchangeManager(), + sizeof(logStorageResources) / sizeof(logStorageResources[0]), + gCircularEventBuffer, logStorageResources); - err = gExchangeManager.Init(&gSessionManager); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + return SUCCESS; + } - err = gMessageCounterManager.Init(&gExchangeManager); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); -} + static int Finalize(void * context) + { + chip::app::EventManagement::DestroyEventManagement(); -void InitializeEventLogging() -{ - chip::app::LogStorageResources logStorageResources[] = { - { &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Debug }, - { &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Info }, - { &gCritEventBuffer[0], sizeof(gCritEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Critical }, - }; - - chip::app::EventManagement::CreateEventManagement( - &gExchangeManager, sizeof(logStorageResources) / sizeof(logStorageResources[0]), gCircularEventBuffer, logStorageResources); -} + if (AppContext::Finalize(context) != SUCCESS) + return FAILURE; + + return SUCCESS; + } +}; void SimpleDumpWriter(const char * aFormat, ...) { @@ -293,27 +283,24 @@ static void CheckLogEventWithDiscardLowEvent(nlTestSuite * apSuite, void * apCon const nlTest sTests[] = { NL_TEST_DEF("CheckLogEventWithEvictToNextBuffer", CheckLogEventWithEvictToNextBuffer), NL_TEST_DEF("CheckLogEventWithDiscardLowEvent", CheckLogEventWithDiscardLowEvent), NL_TEST_SENTINEL() }; + +// clang-format off +nlTestSuite sSuite = +{ + "EventLogging", + &sTests[0], + TestContext::Initialize, + TestContext::Finalize +}; +// clang-format on + } // namespace int TestEventLogging() { - // clang-format off - nlTestSuite theSuite = - { - "EventLogging", - &sTests[0], - nullptr, - nullptr - }; - // clang-format on - - InitializeChip(&theSuite); - InitializeEventLogging(); - nlTestRunner(&theSuite, nullptr); - - gSystemLayer.Shutdown(); - - return (nlTestRunnerStats(&theSuite)); + TestContext gContext; + nlTestRunner(&sSuite, &gContext); + return (nlTestRunnerStats(&sSuite)); } CHIP_REGISTER_TEST_SUITE(TestEventLogging) diff --git a/src/app/tests/TestInteractionModelEngine.cpp b/src/app/tests/TestInteractionModelEngine.cpp index ea568a662467d7..b55200f7a901f7 100644 --- a/src/app/tests/TestInteractionModelEngine.cpp +++ b/src/app/tests/TestInteractionModelEngine.cpp @@ -23,6 +23,7 @@ */ #include +#include #include #include #include @@ -30,26 +31,12 @@ #include #include #include -#include #include #include -#include -#include -#include -#include -#include -#include -#include #include -namespace { -static chip::System::LayerImpl gSystemLayer; -static chip::SessionManager gSessionManager; -static chip::Messaging::ExchangeManager gExchangeManager; -static chip::secure_channel::MessageCounterManager gMessageCounterManager; -static chip::TransportMgr gTransportManager; -} // namespace +using TestContext = chip::Test::AppContext; namespace chip { namespace app { @@ -75,8 +62,9 @@ int TestInteractionModelEngine::GetClusterInfoListLength(ClusterInfo * apCluster void TestInteractionModelEngine::TestClusterInfoPushRelease(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; - err = InteractionModelEngine::GetInstance()->Init(&gExchangeManager, nullptr); + TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; + err = InteractionModelEngine::GetInstance()->Init(&ctx.GetExchangeManager(), nullptr); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); ClusterInfo * clusterInfoList = nullptr; ClusterInfo clusterInfo1; @@ -105,8 +93,9 @@ void TestInteractionModelEngine::TestClusterInfoPushRelease(nlTestSuite * apSuit void TestInteractionModelEngine::TestMergeOverlappedAttributePath(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; - err = InteractionModelEngine::GetInstance()->Init(&gExchangeManager, nullptr); + TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; + err = InteractionModelEngine::GetInstance()->Init(&ctx.GetExchangeManager(), nullptr); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); ClusterInfo clusterInfoList[2]; @@ -131,25 +120,6 @@ void TestInteractionModelEngine::TestMergeOverlappedAttributePath(nlTestSuite * } // namespace chip namespace { -void InitializeChip(nlTestSuite * apSuite) -{ - CHIP_ERROR err = CHIP_NO_ERROR; - chip::Optional peer(chip::Transport::Type::kUndefined); - - err = chip::Platform::MemoryInit(); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - - gSystemLayer.Init(); - - err = gSessionManager.Init(&gSystemLayer, &gTransportManager, &gMessageCounterManager); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - - err = gExchangeManager.Init(&gSessionManager); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - - err = gMessageCounterManager.Init(&gExchangeManager); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); -} // clang-format off const nlTest sTests[] = @@ -159,27 +129,24 @@ const nlTest sTests[] = NL_TEST_SENTINEL() }; // clang-format on -} // namespace -int TestInteractionModelEngine() +// clang-format off +nlTestSuite sSuite = { - // clang-format off - nlTestSuite theSuite = - { - "TestInteractionModelEngine", - &sTests[0], - nullptr, - nullptr - }; - // clang-format on - - InitializeChip(&theSuite); - - nlTestRunner(&theSuite, nullptr); + "TestInteractionModelEngine", + &sTests[0], + TestContext::Initialize, + TestContext::Finalize +}; +// clang-format on - gSystemLayer.Shutdown(); +} // namespace - return (nlTestRunnerStats(&theSuite)); +int TestInteractionModelEngine() +{ + TestContext gContext; + nlTestRunner(&sSuite, &gContext); + return (nlTestRunnerStats(&sSuite)); } CHIP_REGISTER_TEST_SUITE(TestInteractionModelEngine) diff --git a/src/app/tests/TestReadInteraction.cpp b/src/app/tests/TestReadInteraction.cpp index 8032ef8143ae96..7f7352560b7b54 100644 --- a/src/app/tests/TestReadInteraction.cpp +++ b/src/app/tests/TestReadInteraction.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -33,25 +34,13 @@ #include #include #include -#include #include -#include + #include -#include -#include -#include -#include -#include -#include -#include -#include + #include namespace { -chip::TransportMgrBase gTransportManager; -chip::Test::LoopbackTransport gLoopback; -chip::Test::IOContext gIOContext; -chip::secure_channel::MessageCounterManager gMessageCounterManager; uint8_t gDebugEventBuffer[128]; uint8_t gInfoEventBuffer[128]; uint8_t gCritEventBuffer[128]; @@ -64,20 +53,39 @@ chip::EventId kTestEventIdDebug = 1; chip::EventId kTestEventIdCritical = 2; uint8_t kTestFieldValue1 = 1; chip::TLV::Tag kTestEventTag = chip::TLV::ContextTag(1); -using TestContext = chip::Test::MessagingContext; -TestContext sContext; -void InitializeEventLogging(chip::Messaging::ExchangeManager & aExchangeManager) +class TestContext : public chip::Test::AppContext { - chip::app::LogStorageResources logStorageResources[] = { - { &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Debug }, - { &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Info }, - { &gCritEventBuffer[0], sizeof(gCritEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Critical }, - }; - - chip::app::EventManagement::CreateEventManagement(&aExchangeManager, ArraySize(logStorageResources), gCircularEventBuffer, - logStorageResources); -} +public: + static int Initialize(void * context) + { + if (AppContext::Initialize(context) != SUCCESS) + return FAILURE; + + auto * ctx = static_cast(context); + + chip::app::LogStorageResources logStorageResources[] = { + { &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Debug }, + { &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Info }, + { &gCritEventBuffer[0], sizeof(gCritEventBuffer), nullptr, 0, nullptr, chip::app::PriorityLevel::Critical }, + }; + + chip::app::EventManagement::CreateEventManagement(&ctx->GetExchangeManager(), ArraySize(logStorageResources), + gCircularEventBuffer, logStorageResources); + + return SUCCESS; + } + + static int Finalize(void * context) + { + chip::app::EventManagement::DestroyEventManagement(); + + if (AppContext::Finalize(context) != SUCCESS) + return FAILURE; + + return SUCCESS; + } +}; class TestEventGenerator : public chip::app::EventLoggingDelegate { @@ -1125,49 +1133,22 @@ const nlTest sTests[] = }; // clang-format on -int Initialize(void * aContext); -int Finalize(void * aContext); - // clang-format off nlTestSuite sSuite = { - "TestReadInteraction", - &sTests[0], - Initialize, - Finalize + "TestReadInteraction", + &sTests[0], + TestContext::Initialize, + TestContext::Finalize }; // clang-format on -int Initialize(void * aContext) -{ - // Initialize System memory and resources - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gTransportManager.Init(&gLoopback) == CHIP_NO_ERROR, FAILURE); - - auto * ctx = static_cast(aContext); - VerifyOrReturnError(ctx->Init(&sSuite, &gTransportManager, &gIOContext) == CHIP_NO_ERROR, FAILURE); - - InitializeEventLogging(ctx->GetExchangeManager()); - gTransportManager.SetSessionManager(&ctx->GetSecureSessionManager()); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - CHIP_ERROR err = reinterpret_cast(aContext)->Shutdown(); - gIOContext.Shutdown(); - chip::Platform::MemoryShutdown(); - chip::app::EventManagement::DestroyEventManagement(); - return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; -} - } // namespace int TestReadInteraction() { - nlTestRunner(&sSuite, &sContext); - + TestContext gContext; + nlTestRunner(&sSuite, &gContext); return (nlTestRunnerStats(&sSuite)); } diff --git a/src/app/tests/TestReportingEngine.cpp b/src/app/tests/TestReportingEngine.cpp index 3228ddf71114c7..440dc20680a7b8 100644 --- a/src/app/tests/TestReportingEngine.cpp +++ b/src/app/tests/TestReportingEngine.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -32,25 +33,14 @@ #include #include #include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include #include +using TestContext = chip::Test::AppContext; + namespace chip { -static System::LayerImpl gSystemLayer; -static SessionManager gSessionManager; -static Messaging::ExchangeManager gExchangeManager; -static TransportMgr gTransportManager; -static secure_channel::MessageCounterManager gMessageCounterManager; + constexpr ClusterId kTestClusterId = 6; constexpr EndpointId kTestEndpointId = 1; constexpr chip::AttributeId kTestFieldId1 = 1; @@ -77,7 +67,8 @@ class TestExchangeDelegate : public Messaging::ExchangeDelegate void TestReportingEngine::TestBuildAndSendSingleReportData(nlTestSuite * apSuite, void * apContext) { - CHIP_ERROR err = CHIP_NO_ERROR; + TestContext & ctx = *static_cast(apContext); + CHIP_ERROR err = CHIP_NO_ERROR; app::ReadHandler readHandler; System::PacketBufferTLVWriter writer; System::PacketBufferHandle readRequestbuf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize); @@ -85,9 +76,9 @@ void TestReportingEngine::TestBuildAndSendSingleReportData(nlTestSuite * apSuite AttributePathList::Builder attributePathListBuilder; AttributePath::Builder attributePathBuilder; - err = InteractionModelEngine::GetInstance()->Init(&gExchangeManager, nullptr); + err = InteractionModelEngine::GetInstance()->Init(&ctx.GetExchangeManager(), nullptr); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - Messaging::ExchangeContext * exchangeCtx = gExchangeManager.NewContext(SessionHandle(0, 0, 0, 0), nullptr); + Messaging::ExchangeContext * exchangeCtx = ctx.GetExchangeManager().NewContext(SessionHandle(0, 0, 0, 0), nullptr); TestExchangeDelegate delegate; exchangeCtx->SetDelegate(&delegate); @@ -120,7 +111,7 @@ void TestReportingEngine::TestBuildAndSendSingleReportData(nlTestSuite * apSuite NL_TEST_ASSERT(apSuite, readRequestBuilder.GetError() == CHIP_NO_ERROR); err = writer.Finalize(&readRequestbuf); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - readHandler.Init(&gExchangeManager, nullptr, exchangeCtx, chip::app::ReadHandler::InteractionType::Read); + readHandler.Init(&ctx.GetExchangeManager(), nullptr, exchangeCtx, chip::app::ReadHandler::InteractionType::Read); readHandler.OnReadInitialRequest(std::move(readRequestbuf)); err = InteractionModelEngine::GetInstance()->GetReportingEngine().BuildAndSendSingleReportData(&readHandler); NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_NOT_CONNECTED); @@ -131,54 +122,31 @@ void TestReportingEngine::TestBuildAndSendSingleReportData(nlTestSuite * apSuite } // namespace chip namespace { -void InitializeChip(nlTestSuite * apSuite) +// clang-format off +const nlTest sTests[] = { - CHIP_ERROR err = CHIP_NO_ERROR; - chip::Optional peer(chip::Transport::Type::kUndefined); - - err = chip::Platform::MemoryInit(); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - - chip::gSystemLayer.Init(); - - err = chip::gSessionManager.Init(&chip::gSystemLayer, &chip::gTransportManager, &chip::gMessageCounterManager); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - - err = chip::gExchangeManager.Init(&chip::gSessionManager); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - - err = chip::gMessageCounterManager.Init(&chip::gExchangeManager); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); -} + NL_TEST_DEF("CheckBuildAndSendSingleReportData", chip::app::reporting::TestReportingEngine::TestBuildAndSendSingleReportData), + NL_TEST_SENTINEL() +}; +// clang-format on // clang-format off -const nlTest sTests[] = - { - NL_TEST_DEF("CheckBuildAndSendSingleReportData", chip::app::reporting::TestReportingEngine::TestBuildAndSendSingleReportData), - NL_TEST_SENTINEL() - }; +nlTestSuite sSuite = +{ + "TestReportingEngine", + &sTests[0], + TestContext::Initialize, + TestContext::Finalize +}; // clang-format on + } // namespace int TestReportingEngine() { - // clang-format off - nlTestSuite theSuite = - { - "TestReportingEngine", - &sTests[0], - nullptr, - nullptr - }; - // clang-format on - - InitializeChip(&theSuite); - - nlTestRunner(&theSuite, nullptr); - - chip::gSystemLayer.Shutdown(); - - return (nlTestRunnerStats(&theSuite)); + TestContext gContext; + nlTestRunner(&sSuite, &gContext); + return (nlTestRunnerStats(&sSuite)); } CHIP_REGISTER_TEST_SUITE(TestReportingEngine) diff --git a/src/app/tests/TestWriteInteraction.cpp b/src/app/tests/TestWriteInteraction.cpp index 79db04edea0c83..e63647a2effc89 100644 --- a/src/app/tests/TestWriteInteraction.cpp +++ b/src/app/tests/TestWriteInteraction.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -25,31 +26,17 @@ #include #include #include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include +using TestContext = chip::Test::AppContext; + namespace { -chip::TransportMgrBase gTransportManager; -chip::Test::LoopbackTransport gLoopback; -chip::Test::IOContext gIOContext; uint8_t attributeDataTLV[CHIP_CONFIG_DEFAULT_UDP_MTU_SIZE]; size_t attributeDataTLVLen = 0; -using TestContext = chip::Test::MessagingContext; -TestContext sContext; - } // namespace namespace chip { namespace app { @@ -417,47 +404,22 @@ const nlTest sTests[] = }; // clang-format on -int Initialize(void * aContext); -int Finalize(void * aContext); - // clang-format off nlTestSuite sSuite = { "TestWriteInteraction", &sTests[0], - Initialize, - Finalize + TestContext::Initialize, + TestContext::Finalize }; // clang-format on -int Initialize(void * aContext) -{ - // Initialize System memory and resources - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gTransportManager.Init(&gLoopback) == CHIP_NO_ERROR, FAILURE); - - auto * ctx = static_cast(aContext); - VerifyOrReturnError(ctx->Init(&sSuite, &gTransportManager, &gIOContext) == CHIP_NO_ERROR, FAILURE); - - gTransportManager.SetSessionManager(&ctx->GetSecureSessionManager()); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - CHIP_ERROR err = reinterpret_cast(aContext)->Shutdown(); - gIOContext.Shutdown(); - chip::Platform::MemoryShutdown(); - return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; -} - } // namespace int TestWriteInteraction() { - nlTestRunner(&sSuite, &sContext); - + TestContext gContext; + nlTestRunner(&sSuite, &gContext); return (nlTestRunnerStats(&sSuite)); } diff --git a/src/controller/tests/data_model/BUILD.gn b/src/controller/tests/data_model/BUILD.gn index 1dd31e95667e74..7912f6c724422c 100644 --- a/src/controller/tests/data_model/BUILD.gn +++ b/src/controller/tests/data_model/BUILD.gn @@ -31,6 +31,7 @@ chip_test_suite("interaction-tests") { public_deps = [ "${chip_root}/src/app", "${chip_root}/src/app/common:cluster-objects", + "${chip_root}/src/app/tests:helpers", "${chip_root}/src/messaging/tests:helpers", "${chip_root}/src/transport/raw/tests:helpers", "${nlunit_test_root}:nlunit-test", diff --git a/src/controller/tests/data_model/TestCommands.cpp b/src/controller/tests/data_model/TestCommands.cpp index 2acd3c873aaab1..354e94da3127ee 100644 --- a/src/controller/tests/data_model/TestCommands.cpp +++ b/src/controller/tests/data_model/TestCommands.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -35,19 +36,14 @@ #include #include +using TestContext = chip::Test::AppContext; + using namespace chip; using namespace chip::app::Clusters; namespace { -chip::TransportMgrBase gTransportManager; -chip::Test::LoopbackTransport gLoopback; -chip::Test::IOContext gIOContext; -chip::Messaging::ExchangeManager * gExchangeManager; -secure_channel::MessageCounterManager gMessageCounterManager; chip::ClusterStatus kTestSuccessClusterStatus = 1; chip::ClusterStatus kTestFailureClusterStatus = 2; -using TestContext = chip::Test::MessagingContext; -TestContext sContext; constexpr EndpointId kTestEndpointId = 1; @@ -200,10 +196,10 @@ void TestCommandInteraction::TestDataResponse(nlTestSuite * apSuite, void * apCo responseDirective = kSendDataResponse; chip::Controller::InvokeCommandRequest( - gExchangeManager, sessionHandle, kTestEndpointId, request, onSuccessCb, onFailureCb); + &ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, request, onSuccessCb, onFailureCb); NL_TEST_ASSERT(apSuite, onSuccessWasCalled && !onFailureWasCalled); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestCommandInteraction::TestSuccessNoDataResponse(nlTestSuite * apSuite, void * apContext) @@ -231,10 +227,11 @@ void TestCommandInteraction::TestSuccessNoDataResponse(nlTestSuite * apSuite, vo responseDirective = kSendSuccessStatusCode; - chip::Controller::InvokeCommandRequest(gExchangeManager, sessionHandle, kTestEndpointId, request, onSuccessCb, onFailureCb); + chip::Controller::InvokeCommandRequest(&ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, request, onSuccessCb, + onFailureCb); NL_TEST_ASSERT(apSuite, onSuccessWasCalled && !onFailureWasCalled && statusCheck); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestCommandInteraction::TestFailure(nlTestSuite * apSuite, void * apContext) @@ -262,10 +259,11 @@ void TestCommandInteraction::TestFailure(nlTestSuite * apSuite, void * apContext responseDirective = kSendError; - chip::Controller::InvokeCommandRequest(gExchangeManager, sessionHandle, kTestEndpointId, request, onSuccessCb, onFailureCb); + chip::Controller::InvokeCommandRequest(&ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, request, onSuccessCb, + onFailureCb); NL_TEST_ASSERT(apSuite, !onSuccessWasCalled && onFailureWasCalled && statusCheck); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestCommandInteraction::TestSuccessNoDataResponseWithClusterStatus(nlTestSuite * apSuite, void * apContext) @@ -294,10 +292,11 @@ void TestCommandInteraction::TestSuccessNoDataResponseWithClusterStatus(nlTestSu responseDirective = kSendSuccessStatusCodeWithClusterStatus; - chip::Controller::InvokeCommandRequest(gExchangeManager, sessionHandle, kTestEndpointId, request, onSuccessCb, onFailureCb); + chip::Controller::InvokeCommandRequest(&ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, request, onSuccessCb, + onFailureCb); NL_TEST_ASSERT(apSuite, onSuccessWasCalled && !onFailureWasCalled && statusCheck); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestCommandInteraction::TestFailureWithClusterStatus(nlTestSuite * apSuite, void * apContext) @@ -326,10 +325,11 @@ void TestCommandInteraction::TestFailureWithClusterStatus(nlTestSuite * apSuite, responseDirective = kSendErrorWithClusterStatus; - chip::Controller::InvokeCommandRequest(gExchangeManager, sessionHandle, kTestEndpointId, request, onSuccessCb, onFailureCb); + chip::Controller::InvokeCommandRequest(&ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, request, onSuccessCb, + onFailureCb); NL_TEST_ASSERT(apSuite, !onSuccessWasCalled && onFailureWasCalled && statusCheck); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } // clang-format off @@ -344,51 +344,22 @@ const nlTest sTests[] = }; // clang-format on -int Initialize(void * aContext); -int Finalize(void * aContext); - // clang-format off nlTestSuite sSuite = { - "TestCommands", - &sTests[0], - Initialize, - Finalize + "TestCommands", + &sTests[0], + TestContext::Initialize, + TestContext::Finalize }; // clang-format on -int Initialize(void * aContext) -{ - // Initialize System memory and resources - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gTransportManager.Init(&gLoopback) == CHIP_NO_ERROR, FAILURE); - - auto * ctx = static_cast(aContext); - VerifyOrReturnError(ctx->Init(&sSuite, &gTransportManager, &gIOContext) == CHIP_NO_ERROR, FAILURE); - - gTransportManager.SetSessionManager(&ctx->GetSecureSessionManager()); - gExchangeManager = &ctx->GetExchangeManager(); - VerifyOrReturnError( - chip::app::InteractionModelEngine::GetInstance()->Init(&ctx->GetExchangeManager(), nullptr) == CHIP_NO_ERROR, FAILURE); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - // Shutdown will ensure no leaked exchange context. - CHIP_ERROR err = reinterpret_cast(aContext)->Shutdown(); - gIOContext.Shutdown(); - chip::Platform::MemoryShutdown(); - return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; -} - } // namespace int TestCommandInteractionTest() { - nlTestRunner(&sSuite, &sContext); - + TestContext gContext; + nlTestRunner(&sSuite, &gContext); return (nlTestRunnerStats(&sSuite)); } diff --git a/src/controller/tests/data_model/TestRead.cpp b/src/controller/tests/data_model/TestRead.cpp index feb5ef4dc1d8f0..2da76fd7191cd0 100644 --- a/src/controller/tests/data_model/TestRead.cpp +++ b/src/controller/tests/data_model/TestRead.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -25,18 +26,12 @@ #include #include +using TestContext = chip::Test::AppContext; + using namespace chip; using namespace chip::app::Clusters; namespace { -chip::TransportMgrBase gTransportManager; -chip::Test::LoopbackTransport gLoopback; -chip::Test::IOContext gIOContext; -chip::Messaging::ExchangeManager * gExchangeManager; -secure_channel::MessageCounterManager gMessageCounterManager; - -using TestContext = chip::Test::MessagingContext; -TestContext sContext; constexpr EndpointId kTestEndpointId = 1; @@ -147,14 +142,14 @@ void TestReadInteraction::TestDataResponse(nlTestSuite * apSuite, void * apConte CHIP_ERROR aError) { onFailureCbInvoked = true; }; chip::Controller::ReadAttribute( - gExchangeManager, sessionHandle, kTestEndpointId, onSuccessCb, onFailureCb); + &ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, onSuccessCb, onFailureCb); chip::app::InteractionModelEngine::GetInstance()->GetReportingEngine().Run(); NL_TEST_ASSERT(apSuite, onSuccessCbInvoked && !onFailureCbInvoked); NL_TEST_ASSERT(apSuite, chip::app::InteractionModelEngine::GetInstance()->GetNumActiveReadClients() == 0); NL_TEST_ASSERT(apSuite, chip::app::InteractionModelEngine::GetInstance()->GetNumActiveReadHandlers() == 0); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestReadInteraction::TestAttributeError(nlTestSuite * apSuite, void * apContext) @@ -180,14 +175,14 @@ void TestReadInteraction::TestAttributeError(nlTestSuite * apSuite, void * apCon }; chip::Controller::ReadAttribute( - gExchangeManager, sessionHandle, kTestEndpointId, onSuccessCb, onFailureCb); + &ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, onSuccessCb, onFailureCb); chip::app::InteractionModelEngine::GetInstance()->GetReportingEngine().Run(); NL_TEST_ASSERT(apSuite, !onSuccessCbInvoked && onFailureCbInvoked); NL_TEST_ASSERT(apSuite, chip::app::InteractionModelEngine::GetInstance()->GetNumActiveReadClients() == 0); NL_TEST_ASSERT(apSuite, chip::app::InteractionModelEngine::GetInstance()->GetNumActiveReadHandlers() == 0); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestReadInteraction::TestReadTimeout(nlTestSuite * apSuite, void * apContext) @@ -213,12 +208,12 @@ void TestReadInteraction::TestReadTimeout(nlTestSuite * apSuite, void * apContex }; chip::Controller::ReadAttribute( - gExchangeManager, sessionHandle, kTestEndpointId, onSuccessCb, onFailureCb); + &ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, onSuccessCb, onFailureCb); NL_TEST_ASSERT(apSuite, chip::app::InteractionModelEngine::GetInstance()->GetNumActiveReadClients() == 1); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 2); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 2); - gExchangeManager->OnConnectionExpired(ctx.GetSessionBobToAlice()); + ctx.GetExchangeManager().OnConnectionExpired(ctx.GetSessionBobToAlice()); NL_TEST_ASSERT(apSuite, !onSuccessCbInvoked && onFailureCbInvoked); NL_TEST_ASSERT(apSuite, chip::app::InteractionModelEngine::GetInstance()->GetNumActiveReadClients() == 0); @@ -226,18 +221,18 @@ void TestReadInteraction::TestReadTimeout(nlTestSuite * apSuite, void * apContex // // TODO: Figure out why I cannot enable this line below. // - // NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 1); + // NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 1); chip::app::InteractionModelEngine::GetInstance()->GetReportingEngine().Run(); - gExchangeManager->OnConnectionExpired(ctx.GetSessionAliceToBob()); + ctx.GetExchangeManager().OnConnectionExpired(ctx.GetSessionAliceToBob()); NL_TEST_ASSERT(apSuite, chip::app::InteractionModelEngine::GetInstance()->GetNumActiveReadHandlers() == 0); // // TODO: Figure out why I cannot enable this line below. // - // NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + // NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } // clang-format off @@ -250,51 +245,22 @@ const nlTest sTests[] = }; // clang-format on -int Initialize(void * aContext); -int Finalize(void * aContext); - // clang-format off nlTestSuite sSuite = { - "TestRead", - &sTests[0], - Initialize, - Finalize + "TestRead", + &sTests[0], + TestContext::Initialize, + TestContext::Finalize }; // clang-format on -int Initialize(void * aContext) -{ - // Initialize System memory and resources - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gTransportManager.Init(&gLoopback) == CHIP_NO_ERROR, FAILURE); - - auto * ctx = static_cast(aContext); - VerifyOrReturnError(ctx->Init(&sSuite, &gTransportManager, &gIOContext) == CHIP_NO_ERROR, FAILURE); - - gTransportManager.SetSessionManager(&ctx->GetSecureSessionManager()); - gExchangeManager = &ctx->GetExchangeManager(); - VerifyOrReturnError( - chip::app::InteractionModelEngine::GetInstance()->Init(&ctx->GetExchangeManager(), nullptr) == CHIP_NO_ERROR, FAILURE); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - // Shutdown will ensure no leaked exchange context. - CHIP_ERROR err = reinterpret_cast(aContext)->Shutdown(); - gIOContext.Shutdown(); - chip::Platform::MemoryShutdown(); - return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; -} - } // namespace int TestReadInteractionTest() { - nlTestRunner(&sSuite, &sContext); - + TestContext gContext; + nlTestRunner(&sSuite, &gContext); return (nlTestRunnerStats(&sSuite)); } diff --git a/src/controller/tests/data_model/TestWrite.cpp b/src/controller/tests/data_model/TestWrite.cpp index 2b38213759e1e3..4498f096116035 100644 --- a/src/controller/tests/data_model/TestWrite.cpp +++ b/src/controller/tests/data_model/TestWrite.cpp @@ -19,6 +19,7 @@ #include "app-common/zap-generated/ids/Clusters.h" #include #include +#include #include #include #include @@ -26,18 +27,12 @@ #include #include +using TestContext = chip::Test::AppContext; + using namespace chip; using namespace chip::app::Clusters; namespace { -chip::TransportMgrBase gTransportManager; -chip::Test::LoopbackTransport gLoopback; -chip::Test::IOContext gIOContext; -chip::Messaging::ExchangeManager * gExchangeManager; -secure_channel::MessageCounterManager gMessageCounterManager; - -using TestContext = chip::Test::MessagingContext; -TestContext sContext; constexpr EndpointId kTestEndpointId = 1; @@ -153,11 +148,11 @@ void TestWriteInteraction::TestDataResponse(nlTestSuite * apSuite, void * apCont CHIP_ERROR aError) { onFailureCbInvoked = true; }; chip::Controller::WriteAttribute( - gExchangeManager, sessionHandle, kTestEndpointId, value, onSuccessCb, onFailureCb); + &ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, value, onSuccessCb, onFailureCb); NL_TEST_ASSERT(apSuite, onSuccessCbInvoked && !onFailureCbInvoked); NL_TEST_ASSERT(apSuite, chip::app::InteractionModelEngine::GetInstance()->GetNumActiveWriteHandlers() == 0); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } void TestWriteInteraction::TestAttributeError(nlTestSuite * apSuite, void * apContext) @@ -192,11 +187,11 @@ void TestWriteInteraction::TestAttributeError(nlTestSuite * apSuite, void * apCo }; chip::Controller::WriteAttribute( - gExchangeManager, sessionHandle, kTestEndpointId, value, onSuccessCb, onFailureCb); + &ctx.GetExchangeManager(), sessionHandle, kTestEndpointId, value, onSuccessCb, onFailureCb); NL_TEST_ASSERT(apSuite, !onSuccessCbInvoked && onFailureCbInvoked); NL_TEST_ASSERT(apSuite, chip::app::InteractionModelEngine::GetInstance()->GetNumActiveWriteHandlers() == 0); - NL_TEST_ASSERT(apSuite, gExchangeManager->GetNumActiveExchanges() == 0); + NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0); } // clang-format off @@ -208,51 +203,22 @@ const nlTest sTests[] = }; // clang-format on -int Initialize(void * aContext); -int Finalize(void * aContext); - // clang-format off nlTestSuite sSuite = { - "TestWrite", - &sTests[0], - Initialize, - Finalize + "TestWrite", + &sTests[0], + TestContext::Initialize, + TestContext::Finalize }; // clang-format on -int Initialize(void * aContext) -{ - // Initialize System memory and resources - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gTransportManager.Init(&gLoopback) == CHIP_NO_ERROR, FAILURE); - - auto * ctx = static_cast(aContext); - VerifyOrReturnError(ctx->Init(&sSuite, &gTransportManager, &gIOContext) == CHIP_NO_ERROR, FAILURE); - - gTransportManager.SetSessionManager(&ctx->GetSecureSessionManager()); - gExchangeManager = &ctx->GetExchangeManager(); - VerifyOrReturnError( - chip::app::InteractionModelEngine::GetInstance()->Init(&ctx->GetExchangeManager(), nullptr) == CHIP_NO_ERROR, FAILURE); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - // Shutdown will ensure no leaked exchange context. - CHIP_ERROR err = reinterpret_cast(aContext)->Shutdown(); - gIOContext.Shutdown(); - chip::Platform::MemoryShutdown(); - return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; -} - } // namespace int TestWriteInteractionTest() { - nlTestRunner(&sSuite, &sContext); - + TestContext gContext; + nlTestRunner(&sSuite, &gContext); return (nlTestRunnerStats(&sSuite)); } diff --git a/src/messaging/tests/MessagingContext.cpp b/src/messaging/tests/MessagingContext.cpp index 3f963bb8565fbd..f680c40586a0dc 100644 --- a/src/messaging/tests/MessagingContext.cpp +++ b/src/messaging/tests/MessagingContext.cpp @@ -23,7 +23,7 @@ namespace chip { namespace Test { -CHIP_ERROR MessagingContext::Init(nlTestSuite * suite, TransportMgrBase * transport, IOContext * ioContext) +CHIP_ERROR MessagingContext::Init(TransportMgrBase * transport, IOContext * ioContext) { VerifyOrReturnError(mInitialized == false, CHIP_ERROR_INTERNAL); mInitialized = true; diff --git a/src/messaging/tests/MessagingContext.h b/src/messaging/tests/MessagingContext.h index ad8a4fa103e92d..4c8e9755fb02b1 100644 --- a/src/messaging/tests/MessagingContext.h +++ b/src/messaging/tests/MessagingContext.h @@ -42,7 +42,7 @@ class MessagingContext ~MessagingContext() { VerifyOrDie(mInitialized == false); } /// Initialize the underlying layers and test suite pointer - CHIP_ERROR Init(nlTestSuite * suite, TransportMgrBase * transport, IOContext * io); + CHIP_ERROR Init(TransportMgrBase * transport, IOContext * io); // Shutdown all layers, finalize operations CHIP_ERROR Shutdown(); diff --git a/src/messaging/tests/TestExchangeMgr.cpp b/src/messaging/tests/TestExchangeMgr.cpp index a4137b4dbb75d2..c7a80b9a7a7466 100644 --- a/src/messaging/tests/TestExchangeMgr.cpp +++ b/src/messaging/tests/TestExchangeMgr.cpp @@ -254,11 +254,11 @@ int Initialize(void * aContext) { // Initialize System memory and resources VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); + VerifyOrReturnError(gIOContext.Init() == CHIP_NO_ERROR, FAILURE); VerifyOrReturnError(gTransportMgr.Init("LOOPBACK") == CHIP_NO_ERROR, FAILURE); auto * ctx = static_cast(aContext); - VerifyOrReturnError(ctx->Init(&sSuite, &gTransportMgr, &gIOContext) == CHIP_NO_ERROR, FAILURE); + VerifyOrReturnError(ctx->Init(&gTransportMgr, &gIOContext) == CHIP_NO_ERROR, FAILURE); return SUCCESS; } diff --git a/src/messaging/tests/TestReliableMessageProtocol.cpp b/src/messaging/tests/TestReliableMessageProtocol.cpp index 6da3dca818555c..b11ba217793522 100644 --- a/src/messaging/tests/TestReliableMessageProtocol.cpp +++ b/src/messaging/tests/TestReliableMessageProtocol.cpp @@ -564,7 +564,7 @@ void CheckResendSessionEstablishmentMessageWithPeerExchange(nlTestSuite * inSuit // Making this static to reduce stack usage, as some platforms have limits on stack size. static TestContext ctx; - CHIP_ERROR err = ctx.Init(inSuite, &gTransportMgr, &gIOContext); + CHIP_ERROR err = ctx.Init(&gTransportMgr, &gIOContext); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); chip::System::PacketBufferHandle buffer = chip::MessagePacketBuffer::NewWithData(PAYLOAD, sizeof(PAYLOAD)); @@ -1443,11 +1443,11 @@ int Initialize(void * aContext) { // Initialize System memory and resources VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); + VerifyOrReturnError(gIOContext.Init() == CHIP_NO_ERROR, FAILURE); VerifyOrReturnError(gTransportMgr.Init(&gLoopback) == CHIP_NO_ERROR, FAILURE); auto * ctx = static_cast(aContext); - VerifyOrReturnError(ctx->Init(&sSuite, &gTransportMgr, &gIOContext) == CHIP_NO_ERROR, FAILURE); + VerifyOrReturnError(ctx->Init(&gTransportMgr, &gIOContext) == CHIP_NO_ERROR, FAILURE); gTransportMgr.SetSessionManager(&ctx->GetSecureSessionManager()); return SUCCESS; diff --git a/src/protocols/secure_channel/tests/TestCASESession.cpp b/src/protocols/secure_channel/tests/TestCASESession.cpp index d249fce013244f..71bb95f176e033 100644 --- a/src/protocols/secure_channel/tests/TestCASESession.cpp +++ b/src/protocols/secure_channel/tests/TestCASESession.cpp @@ -692,9 +692,9 @@ CHIP_ERROR CASETestSecurePairingSetup(void * inContext) ReturnErrorOnFailure(chip::Platform::MemoryInit()); gTransportMgr.Init(&gLoopback); - ReturnErrorOnFailure(gIOContext.Init(&sSuite)); + ReturnErrorOnFailure(gIOContext.Init()); - ReturnErrorOnFailure(ctx.Init(&sSuite, &gTransportMgr, &gIOContext)); + ReturnErrorOnFailure(ctx.Init(&gTransportMgr, &gIOContext)); ctx.SetBobNodeId(kPlaceholderNodeId); ctx.SetAliceNodeId(kPlaceholderNodeId); diff --git a/src/protocols/secure_channel/tests/TestPASESession.cpp b/src/protocols/secure_channel/tests/TestPASESession.cpp index b265163a1f40db..0491207cb42a27 100644 --- a/src/protocols/secure_channel/tests/TestPASESession.cpp +++ b/src/protocols/secure_channel/tests/TestPASESession.cpp @@ -374,11 +374,11 @@ int TestSecurePairing_Setup(void * inContext) { // Initialize System memory and resources VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - VerifyOrReturnError(gIOContext.Init(&sSuite) == CHIP_NO_ERROR, FAILURE); + VerifyOrReturnError(gIOContext.Init() == CHIP_NO_ERROR, FAILURE); VerifyOrReturnError(gTransportMgr.Init(&gLoopback) == CHIP_NO_ERROR, FAILURE); auto & ctx = *static_cast(inContext); - VerifyOrReturnError(ctx.Init(&sSuite, &gTransportMgr, &gIOContext) == CHIP_NO_ERROR, FAILURE); + VerifyOrReturnError(ctx.Init(&gTransportMgr, &gIOContext) == CHIP_NO_ERROR, FAILURE); ctx.SetBobNodeId(kPlaceholderNodeId); ctx.SetAliceNodeId(kPlaceholderNodeId); diff --git a/src/transport/raw/tests/NetworkTestHelpers.cpp b/src/transport/raw/tests/NetworkTestHelpers.cpp index 34dcbf819adec1..f4a9ab18ea8305 100644 --- a/src/transport/raw/tests/NetworkTestHelpers.cpp +++ b/src/transport/raw/tests/NetworkTestHelpers.cpp @@ -26,7 +26,7 @@ namespace chip { namespace Test { -CHIP_ERROR IOContext::Init(nlTestSuite * suite) +CHIP_ERROR IOContext::Init() { CHIP_ERROR err = Platform::MemoryInit(); @@ -34,7 +34,6 @@ CHIP_ERROR IOContext::Init(nlTestSuite * suite) InitNetwork(); - mSuite = suite; mSystemLayer = &gSystemLayer; mInetLayer = &gInet; diff --git a/src/transport/raw/tests/NetworkTestHelpers.h b/src/transport/raw/tests/NetworkTestHelpers.h index 873a6ec56a664a..52dc3402094461 100644 --- a/src/transport/raw/tests/NetworkTestHelpers.h +++ b/src/transport/raw/tests/NetworkTestHelpers.h @@ -27,7 +27,6 @@ #include #include -#include namespace chip { namespace Test { @@ -38,7 +37,7 @@ class IOContext IOContext() {} /// Initialize the underlying layers and test suite pointer - CHIP_ERROR Init(nlTestSuite * suite); + CHIP_ERROR Init(); // Shutdown all layers, finalize operations CHIP_ERROR Shutdown(); @@ -50,12 +49,10 @@ class IOContext /// completionFunction returns true void DriveIOUntil(System::Clock::Timeout maxWait, std::function completionFunction); - nlTestSuite * GetTestSuite() { return mSuite; } System::Layer & GetSystemLayer() { return *mSystemLayer; } Inet::InetLayer & GetInetLayer() { return *mInetLayer; } private: - nlTestSuite * mSuite = nullptr; System::Layer * mSystemLayer = nullptr; Inet::InetLayer * mInetLayer = nullptr; }; diff --git a/src/transport/raw/tests/TestTCP.cpp b/src/transport/raw/tests/TestTCP.cpp index 98a406966d64c9..5266c79a7632ea 100644 --- a/src/transport/raw/tests/TestTCP.cpp +++ b/src/transport/raw/tests/TestTCP.cpp @@ -475,7 +475,7 @@ static nlTestSuite sSuite = */ static int Initialize(void * aContext) { - CHIP_ERROR err = reinterpret_cast(aContext)->Init(&sSuite); + CHIP_ERROR err = reinterpret_cast(aContext)->Init(); return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; } diff --git a/src/transport/tests/TestSessionManager.cpp b/src/transport/tests/TestSessionManager.cpp index 14025c42b0e215..5c7a9d7ebfda23 100644 --- a/src/transport/tests/TestSessionManager.cpp +++ b/src/transport/tests/TestSessionManager.cpp @@ -492,7 +492,7 @@ nlTestSuite sSuite = */ int Initialize(void * aContext) { - CHIP_ERROR err = reinterpret_cast(aContext)->Init(&sSuite); + CHIP_ERROR err = reinterpret_cast(aContext)->Init(); return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE; }