From 2772617c5ca44e5cfe29be90e8d117b76bf24ac3 Mon Sep 17 00:00:00 2001 From: Maksymilian Knust Date: Tue, 14 May 2024 14:10:25 +0200 Subject: [PATCH] Make test suite setup and teardown functions static (#33330) * Make test suite setup and teardown functions static * Restyled by clang-format * Fixed bug in LoopbackTransport initialization --------- Co-authored-by: Restyled.io --- src/app/tests/AppTestContext.h | 8 +- src/app/tests/TestAclAttribute.cpp | 8 +- src/app/tests/TestAclEvent.cpp | 8 +- src/app/tests/TestBufferedReadCallback.cpp | 8 +- src/app/tests/TestClusterStateCache.cpp | 8 +- src/app/tests/TestCommandInteraction.cpp | 8 +- src/app/tests/TestEventLogging.cpp | 8 +- src/app/tests/TestEventLoggingNoUTCTime.cpp | 20 ++-- src/app/tests/TestEventOverflow.cpp | 8 +- .../tests/TestFabricScopedEventLogging.cpp | 8 +- src/app/tests/TestInteractionModelEngine.cpp | 8 +- src/app/tests/TestReadInteraction.cpp | 44 ++++--- src/app/tests/TestReportScheduler.cpp | 8 +- src/app/tests/TestReportingEngine.cpp | 8 +- src/app/tests/TestTimedHandler.cpp | 8 +- src/app/tests/TestWriteInteraction.cpp | 8 +- src/lib/support/UnitTestRegistration.h | 14 +++ src/messaging/tests/MessagingContext.cpp | 8 +- src/messaging/tests/MessagingContext.h | 113 ++++++------------ .../tests/TestAbortExchangesForFabric.cpp | 8 +- src/messaging/tests/TestExchange.cpp | 8 +- src/messaging/tests/TestExchangeHolder.cpp | 8 +- src/messaging/tests/TestExchangeMgr.cpp | 8 +- src/messaging/tests/TestMessagingLayer.cpp | 8 +- .../tests/TestReliableMessageProtocol.cpp | 8 +- .../secure_channel/tests/TestCASESession.cpp | 12 +- .../secure_channel/tests/TestPASESession.cpp | 10 +- src/transport/raw/tests/NetworkTestHelpers.h | 7 +- 28 files changed, 191 insertions(+), 197 deletions(-) diff --git a/src/app/tests/AppTestContext.h b/src/app/tests/AppTestContext.h index b9945fecd3a90a..a2fe3387d2d7aa 100644 --- a/src/app/tests/AppTestContext.h +++ b/src/app/tests/AppTestContext.h @@ -28,13 +28,13 @@ class AppContext : public LoopbackMessagingContext { public: // Performs shared setup for all tests in the test suite - void SetUpTestSuite() override; + static void SetUpTestSuite(); // Performs shared teardown for all tests in the test suite - void TearDownTestSuite() override; + static void TearDownTestSuite(); // Performs setup for each individual test in the test suite - void SetUp() override; + void SetUp(); // Performs teardown for each individual test in the test suite - void TearDown() override; + void TearDown(); }; } // namespace Test diff --git a/src/app/tests/TestAclAttribute.cpp b/src/app/tests/TestAclAttribute.cpp index c1bfd8acf68a73..427b257f9efd7c 100644 --- a/src/app/tests/TestAclAttribute.cpp +++ b/src/app/tests/TestAclAttribute.cpp @@ -259,10 +259,10 @@ const nlTest sTests[] = { nlTestSuite sSuite = { "TestAclAttribute", &sTests[0], - TestAccessContext::nlTestSetUpTestSuite, - TestAccessContext::nlTestTearDownTestSuite, - TestAccessContext::nlTestSetUp, - TestAccessContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestAccessContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestAccessContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestAccessContext, SetUp), + NL_TEST_WRAP_METHOD(TestAccessContext, TearDown), }; } // namespace diff --git a/src/app/tests/TestAclEvent.cpp b/src/app/tests/TestAclEvent.cpp index f7c85e0bccaed8..8f377cc263e9bd 100644 --- a/src/app/tests/TestAclEvent.cpp +++ b/src/app/tests/TestAclEvent.cpp @@ -377,10 +377,10 @@ nlTestSuite sSuite = { "TestAclEvent", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/app/tests/TestBufferedReadCallback.cpp b/src/app/tests/TestBufferedReadCallback.cpp index 93eca2ad5044f4..6079c21b50b49f 100644 --- a/src/app/tests/TestBufferedReadCallback.cpp +++ b/src/app/tests/TestBufferedReadCallback.cpp @@ -610,10 +610,10 @@ nlTestSuite theSuite = { "TestBufferedReadCallback", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; } diff --git a/src/app/tests/TestClusterStateCache.cpp b/src/app/tests/TestClusterStateCache.cpp index a871f3dd1a434e..3247af55bccd03 100644 --- a/src/app/tests/TestClusterStateCache.cpp +++ b/src/app/tests/TestClusterStateCache.cpp @@ -695,10 +695,10 @@ nlTestSuite theSuite = { "TestClusterStateCache", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; } diff --git a/src/app/tests/TestCommandInteraction.cpp b/src/app/tests/TestCommandInteraction.cpp index 87296580fa695f..195580ce8702a5 100644 --- a/src/app/tests/TestCommandInteraction.cpp +++ b/src/app/tests/TestCommandInteraction.cpp @@ -2120,10 +2120,10 @@ nlTestSuite sSuite = { "TestCommandInteraction", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/app/tests/TestEventLogging.cpp b/src/app/tests/TestEventLogging.cpp index 41ae31bd4295ff..854247240ac701 100644 --- a/src/app/tests/TestEventLogging.cpp +++ b/src/app/tests/TestEventLogging.cpp @@ -318,10 +318,10 @@ const nlTest sTests[] = { nlTestSuite sSuite = { "EventLogging", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; } // namespace diff --git a/src/app/tests/TestEventLoggingNoUTCTime.cpp b/src/app/tests/TestEventLoggingNoUTCTime.cpp index 77a8463a5f9718..c6dc4182b79e0e 100644 --- a/src/app/tests/TestEventLoggingNoUTCTime.cpp +++ b/src/app/tests/TestEventLoggingNoUTCTime.cpp @@ -84,16 +84,16 @@ class TestContext : public chip::Test::AppContext { public: // Performs shared setup for all tests in the test suite - void SetUpTestSuite() override + static void SetUpTestSuite() { chip::Test::AppContext::SetUpTestSuite(); - mClock.Emplace(chip::System::SystemClock()); + sClock.Emplace(chip::System::SystemClock()); } // Performs shared teardown for all tests in the test suite - void TearDownTestSuite() override + static void TearDownTestSuite() { - mClock.ClearValue(); + sClock.ClearValue(); chip::Test::AppContext::TearDownTestSuite(); } @@ -125,9 +125,11 @@ class TestContext : public chip::Test::AppContext private: chip::MonotonicallyIncreasingCounter mEventCounter; - chip::Optional mClock; + static chip::Optional sClock; }; +chip::Optional TestContext::sClock; + void ENFORCE_FORMAT(1, 2) SimpleDumpWriter(const char * aFormat, ...) { va_list args; @@ -370,10 +372,10 @@ const nlTest sTests[] = { nlTestSuite sSuite = { "EventLogging", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; } // namespace diff --git a/src/app/tests/TestEventOverflow.cpp b/src/app/tests/TestEventOverflow.cpp index a328bec9783ef2..23615f401d6f75 100644 --- a/src/app/tests/TestEventOverflow.cpp +++ b/src/app/tests/TestEventOverflow.cpp @@ -169,10 +169,10 @@ nlTestSuite sSuite = { "TestEventOverflow", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/app/tests/TestFabricScopedEventLogging.cpp b/src/app/tests/TestFabricScopedEventLogging.cpp index 082aca0a2ecb7d..2ed758573aa5b6 100644 --- a/src/app/tests/TestFabricScopedEventLogging.cpp +++ b/src/app/tests/TestFabricScopedEventLogging.cpp @@ -278,10 +278,10 @@ nlTestSuite sSuite = { "TestFabricScopedEventLogging", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/app/tests/TestInteractionModelEngine.cpp b/src/app/tests/TestInteractionModelEngine.cpp index 4f0b75315355ad..3d6abd9e651d49 100644 --- a/src/app/tests/TestInteractionModelEngine.cpp +++ b/src/app/tests/TestInteractionModelEngine.cpp @@ -773,10 +773,10 @@ nlTestSuite sSuite = { "TestInteractionModelEngine", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/app/tests/TestReadInteraction.cpp b/src/app/tests/TestReadInteraction.cpp index 7e18cca7883c8f..e06721806732f6 100644 --- a/src/app/tests/TestReadInteraction.cpp +++ b/src/app/tests/TestReadInteraction.cpp @@ -79,13 +79,13 @@ class TestContext : public chip::Test::AppContext { public: // Performs shared setup for all tests in the test suite - void SetUpTestSuite() override + static void SetUpTestSuite() { chip::Test::AppContext::SetUpTestSuite(); gRealClock = &chip::System::SystemClock(); chip::System::Clock::Internal::SetSystemClockForTesting(&gMockClock); - if (mSyncScheduler) + if (sSyncScheduler) { gReportScheduler = chip::app::reporting::GetSynchronizedReportScheduler(); sUsingSubSync = true; @@ -96,14 +96,8 @@ class TestContext : public chip::Test::AppContext } } - static int nlTestSetUpTestSuite_Sync(void * context) - { - static_cast(context)->mSyncScheduler = true; - return nlTestSetUpTestSuite(context); - } - // Performs shared teardown for all tests in the test suite - void TearDownTestSuite() override + static void TearDownTestSuite() { chip::System::Clock::Internal::SetSystemClockForTesting(gRealClock); chip::Test::AppContext::TearDownTestSuite(); @@ -133,9 +127,21 @@ class TestContext : public chip::Test::AppContext chip::Test::AppContext::TearDown(); } -private: +protected: chip::MonotonicallyIncreasingCounter mEventCounter; - bool mSyncScheduler = false; + static bool sSyncScheduler; +}; + +bool TestContext::sSyncScheduler = false; + +class TestSyncContext : public TestContext +{ +public: + static void SetUpTestSuite() + { + sSyncScheduler = true; + TestContext::SetUpTestSuite(); + } }; class TestEventGenerator : public chip::app::EventLoggingDelegate @@ -5146,19 +5152,19 @@ const nlTest sTests[] = { nlTestSuite sSuite = { "TestReadInteraction", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; nlTestSuite sSyncSuite = { "TestSyncReadInteraction", &sTests[0], - TestContext::nlTestSetUpTestSuite_Sync, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestSyncContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestSyncContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestSyncContext, SetUp), + NL_TEST_WRAP_METHOD(TestSyncContext, TearDown), }; } // namespace diff --git a/src/app/tests/TestReportScheduler.cpp b/src/app/tests/TestReportScheduler.cpp index 5219943136847d..b7289f5f2daa90 100644 --- a/src/app/tests/TestReportScheduler.cpp +++ b/src/app/tests/TestReportScheduler.cpp @@ -821,10 +821,10 @@ static nlTest sTests[] = { nlTestSuite sSuite = { "TestReportScheduler", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; } // namespace diff --git a/src/app/tests/TestReportingEngine.cpp b/src/app/tests/TestReportingEngine.cpp index 618a2e892e789b..459c9752016e48 100644 --- a/src/app/tests/TestReportingEngine.cpp +++ b/src/app/tests/TestReportingEngine.cpp @@ -350,10 +350,10 @@ nlTestSuite sSuite = { "TestReportingEngine", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/app/tests/TestTimedHandler.cpp b/src/app/tests/TestTimedHandler.cpp index 29e8aac71d35a7..64e174a2b9e7d7 100644 --- a/src/app/tests/TestTimedHandler.cpp +++ b/src/app/tests/TestTimedHandler.cpp @@ -264,10 +264,10 @@ nlTestSuite sSuite = { "TestTimedHandler", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/app/tests/TestWriteInteraction.cpp b/src/app/tests/TestWriteInteraction.cpp index 16647ca523829e..25c9106f2986a0 100644 --- a/src/app/tests/TestWriteInteraction.cpp +++ b/src/app/tests/TestWriteInteraction.cpp @@ -1063,10 +1063,10 @@ const nlTest sTests[] = nlTestSuite sSuite = { "TestWriteInteraction", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; } // namespace diff --git a/src/lib/support/UnitTestRegistration.h b/src/lib/support/UnitTestRegistration.h index 791a62be855a36..be7cc710a9278d 100644 --- a/src/lib/support/UnitTestRegistration.h +++ b/src/lib/support/UnitTestRegistration.h @@ -48,6 +48,20 @@ VerifyOrDie(chip::RegisterUnitTests(&FUNCTION) == CHIP_NO_ERROR); \ } +// TODO: remove these once transition to pw_unit_test is completed +#define NL_TEST_WRAP_FUNCTION(FUNCTION) \ + [](void * _context) -> int { \ + FUNCTION(); \ + return SUCCESS; \ + } + +#define NL_TEST_WRAP_METHOD(CLASS, METHOD) \ + [](void * context) -> int { \ + auto ctx = static_cast(context); \ + ctx->METHOD(); \ + return SUCCESS; \ + } + namespace chip { typedef int (*UnitTestTriggerFunction)(); diff --git a/src/messaging/tests/MessagingContext.cpp b/src/messaging/tests/MessagingContext.cpp index 72ab1650da552c..61565da1cd4ac0 100644 --- a/src/messaging/tests/MessagingContext.cpp +++ b/src/messaging/tests/MessagingContext.cpp @@ -58,7 +58,7 @@ CHIP_ERROR MessagingContext::Init(TransportMgrBase * transport, IOContext * ioCo ReturnErrorOnFailure(mExchangeManager.Init(&mSessionManager)); ReturnErrorOnFailure(mMessageCounterManager.Init(&mExchangeManager)); - if (mInitializeNodes) + if (sInitializeNodes) { ReturnErrorOnFailure(CreateAliceFabric()); ReturnErrorOnFailure(CreateBobFabric()); @@ -112,6 +112,8 @@ using namespace System::Clock::Literals; constexpr chip::System::Clock::Timeout MessagingContext::kResponsiveIdleRetransTimeout; constexpr chip::System::Clock::Timeout MessagingContext::kResponsiveActiveRetransTimeout; +bool MessagingContext::sInitializeNodes = true; + void MessagingContext::SetMRPMode(MRPMode mode) { if (mode == MRPMode::kDefault) @@ -302,6 +304,10 @@ Messaging::ExchangeContext * MessagingContext::NewExchangeToBob(Messaging::Excha return mExchangeManager.NewContext(GetSessionAliceToBob(), delegate, isInitiator); } +LoopbackTransportManager LoopbackMessagingContext::sLoopbackTransportManager; + +UDPTransportManager UDPMessagingContext::sUDPTransportManager; + void MessageCapturer::OnMessageReceived(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, const SessionHandle & session, DuplicateMessage isDuplicate, System::PacketBufferHandle && msgBuf) diff --git a/src/messaging/tests/MessagingContext.h b/src/messaging/tests/MessagingContext.h index 9ed82de60e4d5d..ab9912924e713c 100644 --- a/src/messaging/tests/MessagingContext.h +++ b/src/messaging/tests/MessagingContext.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -100,7 +101,7 @@ class MessagingContext : public PlatformMemoryUser ~MessagingContext() { VerifyOrDie(mInitialized == false); } // Whether Alice and Bob are initialized, must be called before Init - void ConfigInitializeNodes(bool initializeNodes) { mInitializeNodes = initializeNodes; } + static void ConfigInitializeNodes(bool initializeNodes) { sInitializeNodes = initializeNodes; } /// Initialize the underlying layers and test suite pointer CHIP_ERROR Init(TransportMgrBase * transport, IOContext * io); @@ -177,7 +178,7 @@ class MessagingContext : public PlatformMemoryUser System::Layer & GetSystemLayer() { return mIOContext->GetSystemLayer(); } private: - bool mInitializeNodes = true; + static bool sInitializeNodes; bool mInitialized; FabricTable mFabricTable; @@ -206,26 +207,38 @@ class MessagingContext : public PlatformMemoryUser }; // LoopbackMessagingContext enriches MessagingContext with an async loopback transport -class LoopbackMessagingContext : public LoopbackTransportManager, public MessagingContext +class LoopbackMessagingContext : public MessagingContext { public: virtual ~LoopbackMessagingContext() {} + // These functions wrap sLoopbackTransportManager methods + static auto & GetSystemLayer() { return sLoopbackTransportManager.GetSystemLayer(); } + static auto & GetLoopback() { return sLoopbackTransportManager.GetLoopback(); } + static auto & GetTransportMgr() { return sLoopbackTransportManager.GetTransportMgr(); } + static auto & GetIOContext() { return sLoopbackTransportManager.GetIOContext(); } + + template + static void DrainAndServiceIO(Ts... args) + { + return sLoopbackTransportManager.DrainAndServiceIO(args...); + } + // Performs shared setup for all tests in the test suite - virtual void SetUpTestSuite() + static void SetUpTestSuite() { CHIP_ERROR err = CHIP_NO_ERROR; // TODO: use ASSERT_EQ, once transition to pw_unit_test is complete VerifyOrDieWithMsg((err = chip::Platform::MemoryInit()) == CHIP_NO_ERROR, AppServer, "Init CHIP memory failed: %" CHIP_ERROR_FORMAT, err.Format()); - VerifyOrDieWithMsg((err = LoopbackTransportManager::Init()) == CHIP_NO_ERROR, AppServer, + VerifyOrDieWithMsg((err = sLoopbackTransportManager.Init()) == CHIP_NO_ERROR, AppServer, "Init LoopbackTransportManager failed: %" CHIP_ERROR_FORMAT, err.Format()); } // Performs shared teardown for all tests in the test suite - virtual void TearDownTestSuite() + static void TearDownTestSuite() { - LoopbackTransportManager::Shutdown(); + sLoopbackTransportManager.Shutdown(); chip::Platform::MemoryShutdown(); } @@ -240,100 +253,48 @@ class LoopbackMessagingContext : public LoopbackTransportManager, public Messagi // Performs teardown for each individual test in the test suite virtual void TearDown() { MessagingContext::Shutdown(); } - // Helpers that can be used directly by the nlTestSuite - - static int nlTestSetUpTestSuite(void * context) - { - static_cast(context)->SetUpTestSuite(); - return SUCCESS; - } - - static int nlTestTearDownTestSuite(void * context) - { - static_cast(context)->TearDownTestSuite(); - return SUCCESS; - } - - static int nlTestSetUp(void * context) - { - static_cast(context)->SetUp(); - return SUCCESS; - } - - static int nlTestTearDown(void * context) - { - static_cast(context)->TearDown(); - return SUCCESS; - } - - using LoopbackTransportManager::GetSystemLayer; + static LoopbackTransportManager sLoopbackTransportManager; }; // UDPMessagingContext enriches MessagingContext with an UDP transport -class UDPMessagingContext : public UDPTransportManager, public MessagingContext +class UDPMessagingContext : public MessagingContext { public: virtual ~UDPMessagingContext() {} + static auto & GetSystemLayer() { return sUDPTransportManager.GetSystemLayer(); } + static auto & GetTransportMgr() { return sUDPTransportManager.GetTransportMgr(); } + static auto & GetIOContext() { return sUDPTransportManager.GetIOContext(); } + // Performs shared setup for all tests in the test suite - virtual CHIP_ERROR SetUpTestSuite() + static void SetUpTestSuite() { CHIP_ERROR err = CHIP_NO_ERROR; - VerifyOrExit((err = chip::Platform::MemoryInit()) == CHIP_NO_ERROR, - ChipLogError(AppServer, "Init CHIP memory failed: %" CHIP_ERROR_FORMAT, err.Format())); - VerifyOrExit((err = UDPTransportManager::Init()) == CHIP_NO_ERROR, - ChipLogError(AppServer, "Init UDPTransportManager failed: %" CHIP_ERROR_FORMAT, err.Format())); - exit: - return err; + VerifyOrDieWithMsg((err = chip::Platform::MemoryInit()) == CHIP_NO_ERROR, AppServer, + "Init CHIP memory failed: %" CHIP_ERROR_FORMAT, err.Format()); + VerifyOrDieWithMsg((err = sUDPTransportManager.Init()) == CHIP_NO_ERROR, AppServer, + "Init UDPTransportManager failed: %" CHIP_ERROR_FORMAT, err.Format()); } // Performs shared teardown for all tests in the test suite - virtual void TearDownTestSuite() + static void TearDownTestSuite() { - UDPTransportManager::Shutdown(); + sUDPTransportManager.Shutdown(); chip::Platform::MemoryShutdown(); } // Performs setup for each individual test in the test suite - virtual CHIP_ERROR SetUp() + virtual void SetUp() { CHIP_ERROR err = CHIP_NO_ERROR; - VerifyOrExit((err = MessagingContext::Init(&GetTransportMgr(), &GetIOContext())) == CHIP_NO_ERROR, - ChipLogError(AppServer, "Init MessagingContext failed: %" CHIP_ERROR_FORMAT, err.Format())); - exit: - return err; + VerifyOrDieWithMsg((err = MessagingContext::Init(&GetTransportMgr(), &GetIOContext())) == CHIP_NO_ERROR, AppServer, + "Init MessagingContext failed: %" CHIP_ERROR_FORMAT, err.Format()); } // Performs teardown for each individual test in the test suite virtual void TearDown() { MessagingContext::Shutdown(); } - // Helpers that can be used directly by the nlTestSuite - - static int nlTestSetUpTestSuite(void * context) - { - auto err = static_cast(context)->SetUpTestSuite(); - return err == CHIP_NO_ERROR ? SUCCESS : FAILURE; - } - - static int nlTestTearDownTestSuite(void * context) - { - static_cast(context)->TearDownTestSuite(); - return SUCCESS; - } - - static int nlTestSetUp(void * context) - { - auto err = static_cast(context)->SetUp(); - return err == CHIP_NO_ERROR ? SUCCESS : FAILURE; - } - - static int nlTestTearDown(void * context) - { - static_cast(context)->TearDown(); - return SUCCESS; - } - - using UDPTransportManager::GetSystemLayer; + static UDPTransportManager sUDPTransportManager; }; // Class that can be used to capture decrypted message traffic in tests using diff --git a/src/messaging/tests/TestAbortExchangesForFabric.cpp b/src/messaging/tests/TestAbortExchangesForFabric.cpp index 1540b91725a3aa..d5c945c848a9aa 100644 --- a/src/messaging/tests/TestAbortExchangesForFabric.cpp +++ b/src/messaging/tests/TestAbortExchangesForFabric.cpp @@ -273,10 +273,10 @@ const nlTest sTests[] = { nlTestSuite sSuite = { "Test-AbortExchangesForFabric", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/messaging/tests/TestExchange.cpp b/src/messaging/tests/TestExchange.cpp index 67b9ab6d16d0d3..fbc1c9e505204d 100644 --- a/src/messaging/tests/TestExchange.cpp +++ b/src/messaging/tests/TestExchange.cpp @@ -241,10 +241,10 @@ nlTestSuite sSuite = { "Test-Exchange", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/messaging/tests/TestExchangeHolder.cpp b/src/messaging/tests/TestExchangeHolder.cpp index 9325350d262e1b..849c1856d9b013 100644 --- a/src/messaging/tests/TestExchangeHolder.cpp +++ b/src/messaging/tests/TestExchangeHolder.cpp @@ -821,10 +821,10 @@ nlTestSuite sSuite = { "Test-TestExchangeHolder", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/messaging/tests/TestExchangeMgr.cpp b/src/messaging/tests/TestExchangeMgr.cpp index cc4dbf10e5a4f4..efca83e6d1a92a 100644 --- a/src/messaging/tests/TestExchangeMgr.cpp +++ b/src/messaging/tests/TestExchangeMgr.cpp @@ -301,10 +301,10 @@ nlTestSuite sSuite = { "Test-CHIP-ExchangeManager", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/messaging/tests/TestMessagingLayer.cpp b/src/messaging/tests/TestMessagingLayer.cpp index 2c05c067989e42..00dca89f2b6639 100644 --- a/src/messaging/tests/TestMessagingLayer.cpp +++ b/src/messaging/tests/TestMessagingLayer.cpp @@ -162,10 +162,10 @@ nlTestSuite sSuite = { "Test-CHIP-MessagingLayer", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/messaging/tests/TestReliableMessageProtocol.cpp b/src/messaging/tests/TestReliableMessageProtocol.cpp index 057aa599726df3..68342838a735b1 100644 --- a/src/messaging/tests/TestReliableMessageProtocol.cpp +++ b/src/messaging/tests/TestReliableMessageProtocol.cpp @@ -2229,10 +2229,10 @@ const nlTest sTests[] = { nlTestSuite sSuite = { "Test-CHIP-ReliableMessageProtocol", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/protocols/secure_channel/tests/TestCASESession.cpp b/src/protocols/secure_channel/tests/TestCASESession.cpp index 1ef821f30c8b50..5fc6a37f72f4ab 100644 --- a/src/protocols/secure_channel/tests/TestCASESession.cpp +++ b/src/protocols/secure_channel/tests/TestCASESession.cpp @@ -61,9 +61,9 @@ class TestContext : public Test::LoopbackMessagingContext { public: // Performs shared setup for all tests in the test suite - void SetUpTestSuite() override; + static void SetUpTestSuite(); // Performs shared teardown for all tests in the test suite - void TearDownTestSuite() override; + static void TearDownTestSuite(); }; void ServiceEvents(TestContext & ctx) @@ -1240,10 +1240,10 @@ static nlTestSuite sSuite = { "Test-CHIP-SecurePairing-CASE", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/protocols/secure_channel/tests/TestPASESession.cpp b/src/protocols/secure_channel/tests/TestPASESession.cpp index 368811a273ff66..d610b304cec481 100644 --- a/src/protocols/secure_channel/tests/TestPASESession.cpp +++ b/src/protocols/secure_channel/tests/TestPASESession.cpp @@ -89,7 +89,7 @@ class TestContext : public chip::Test::LoopbackMessagingContext { public: // Performs shared setup for all tests in the test suite - void SetUpTestSuite() override + static void SetUpTestSuite() { ConfigInitializeNodes(false); chip::Test::LoopbackMessagingContext::SetUpTestSuite(); @@ -541,10 +541,10 @@ static nlTestSuite sSuite = { "Test-CHIP-SecurePairing-PASE", &sTests[0], - TestContext::nlTestSetUpTestSuite, - TestContext::nlTestTearDownTestSuite, - TestContext::nlTestSetUp, - TestContext::nlTestTearDown, + NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite), + NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite), + NL_TEST_WRAP_METHOD(TestContext, SetUp), + NL_TEST_WRAP_METHOD(TestContext, TearDown), }; // clang-format on diff --git a/src/transport/raw/tests/NetworkTestHelpers.h b/src/transport/raw/tests/NetworkTestHelpers.h index bf1e812a87fe02..49890406bc6afb 100644 --- a/src/transport/raw/tests/NetworkTestHelpers.h +++ b/src/transport/raw/tests/NetworkTestHelpers.h @@ -72,7 +72,12 @@ class LoopbackTransportDelegate class LoopbackTransport : public Transport::Base { public: - void InitLoopbackTransport(System::Layer * systemLayer) { mSystemLayer = systemLayer; } + void InitLoopbackTransport(System::Layer * systemLayer) + { + Reset(); + mSystemLayer = systemLayer; + } + void ShutdownLoopbackTransport() { // Make sure no one left packets hanging out that they thought got