Skip to content

Commit

Permalink
Updated AppContext, LoopbackMessagingContext, and UDPMessagingContext…
Browse files Browse the repository at this point in the history
… to serve as PW test fixture classes. (#34036)

* Modified MessagingContext and its subclasses as well as AppContext to reflect PW migration.

* Modifed all tests that use LoopbackMessagingContext or AppContext.  Moved LoopbackMessagingContext data to heap.

* Removed unneeded scope from call to parent setup/teardown

* Made some helper functions into class methods.

* Restyled by prettier-markdown

* Reverted TestICDManager back to using LoopbackMessagingContext

* fixing merge conflicts

* fixing merge conflicts

* Fixed duplicate code and missing semicolon

* Trying to update TestReadHandler_DataVersionFiltersTruncated

* Restyled by whitespace

* Restyled by clang-format

* fixed problem with the new TestReadHandler_DataVersionFiltersTruncated

* fix in tracing

* Changes to MessagingContext member variable initialization in response to code review

* Used std::unique_ptr for MessagingContext.mpData

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Feb 18, 2025
1 parent 590d7e3 commit 2357297
Show file tree
Hide file tree
Showing 45 changed files with 1,953 additions and 2,713 deletions.
4 changes: 2 additions & 2 deletions docs/testing/unit_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ your overriding function make sure to check `HasFailure()` and return if the
parent function failed.

If you don't override any of the setup/teardown functions, you can simply make a
type alias: `using YourTestContext = Test::AppContextPW;` instead of defining
your own text context class.
type alias: `using YourTestContext = Test::AppContext;` instead of defining your
own text context class.

## Best practices

Expand Down
46 changes: 17 additions & 29 deletions src/app/icd/server/tests/TestICDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <pw_unit_test/framework.h>

#include <app/SubscriptionsInfoProvider.h>
#include <app/TestEventTriggerDelegate.h>
#include <app/icd/server/ICDConfigurationData.h>
Expand All @@ -24,7 +26,6 @@
#include <app/icd/server/ICDStateObserver.h>
#include <app/icd/server/tests/ICDConfigurationDataTestAccess.h>
#include <crypto/DefaultSessionKeystore.h>
#include <gtest/gtest.h>
#include <lib/address_resolve/AddressResolve.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/NodeId.h>
Expand Down Expand Up @@ -124,18 +125,16 @@ class TestSubscriptionsInfoProvider : public SubscriptionsInfoProvider
bool mHasPersistedSubscription = false;
};

System::Clock::Internal::MockClock * pMockClock = nullptr;
System::Clock::ClockBase * pRealClock = nullptr;
chip::Test::LoopbackMessagingContext * pMessagingContext = nullptr;
System::Clock::Internal::MockClock * pMockClock = nullptr;
System::Clock::ClockBase * pRealClock = nullptr;

} // namespace

namespace chip {
namespace app {

class TestICDManager : public ::testing::Test
class TestICDManager : public Test::LoopbackMessagingContext
{

public:
/*
* Advance the test Mock clock time by the amout passed in argument
Expand All @@ -147,7 +146,7 @@ class TestICDManager : public ::testing::Test
static void AdvanceClockAndRunEventLoop(Milliseconds64 time)
{
pMockClock->AdvanceMonotonic(time);
pMessagingContext->GetIOContext().DriveIO();
GetIOContext().DriveIO();
}

// Performs shared setup for all tests in the test suite
Expand All @@ -159,16 +158,12 @@ class TestICDManager : public ::testing::Test
ASSERT_NE(pMockClock, nullptr);
}

if (pMessagingContext == nullptr)
{
pMessagingContext = new LoopbackMessagingContext();
ASSERT_NE(pMessagingContext, nullptr);
}
LoopbackMessagingContext::SetUpTestSuite();
VerifyOrReturn(!HasFailure());

pMessagingContext->SetUpTestSuite();
ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR);

DeviceLayer::SetSystemLayerForTesting(&(pMessagingContext->GetSystemLayer()));
DeviceLayer::SetSystemLayerForTesting(&GetSystemLayer());
pRealClock = &SystemClock();
Clock::Internal::SetSystemClockForTesting(pMockClock);
}
Expand All @@ -180,39 +175,34 @@ class TestICDManager : public ::testing::Test
DeviceLayer::SetSystemLayerForTesting(nullptr);

DeviceLayer::PlatformMgr().Shutdown();
pMessagingContext->TearDownTestSuite();

LoopbackMessagingContext::TearDownTestSuite();

if (pMockClock != nullptr)
{
delete pMockClock;
pMockClock = nullptr;
}

if (pMessagingContext != nullptr)
{
delete pMessagingContext;
pMessagingContext = nullptr;
}

pRealClock = nullptr;
}

// Performs setup for each individual test in the test suite
void SetUp() override
{
pMessagingContext->SetUp();
LoopbackMessagingContext::SetUp();
VerifyOrReturn(!HasFailure());

mICDStateObserver.ResetAll();
mICDManager.RegisterObserver(&mICDStateObserver);
mICDManager.Init(&testStorage, &(pMessagingContext->GetFabricTable()), &mKeystore,
&(pMessagingContext->GetExchangeManager()), &mSubInfoProvider);
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);
}

// Performs teardown for each individual test in the test suite
void TearDown() override
{
mICDManager.Shutdown();
pMessagingContext->TearDown();
LoopbackMessagingContext::TearDown();
}

TestSessionKeystoreImpl mKeystore;
Expand Down Expand Up @@ -576,8 +566,7 @@ TEST_F(TestICDManager, TestICDCounter)

// Shut down and reinit ICDManager to increment counter
mICDManager.Shutdown();
mICDManager.Init(&(testStorage), &(pMessagingContext->GetFabricTable()), &(mKeystore),
&(pMessagingContext->GetExchangeManager()), &(mSubInfoProvider));
mICDManager.Init(&(testStorage), &GetFabricTable(), &(mKeystore), &GetExchangeManager(), &(mSubInfoProvider));
mICDManager.RegisterObserver(&(mICDStateObserver));

EXPECT_EQ(counter + ICDConfigurationData::kICDCounterPersistenceIncrement,
Expand Down Expand Up @@ -982,8 +971,7 @@ TEST_F(TestICDManager, TestICDStateObserverOnICDModeChangeOnInit)
// Shut down and reinit ICDManager - We should go to LIT mode since we have a registration
mICDManager.Shutdown();
mICDManager.RegisterObserver(&(mICDStateObserver));
mICDManager.Init(&testStorage, &(pMessagingContext->GetFabricTable()), &mKeystore, &(pMessagingContext->GetExchangeManager()),
&mSubInfoProvider);
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);

// We have a registration, transition to LIT mode
EXPECT_TRUE(mICDStateObserver.mOnICDModeChangeCalled);
Expand Down
26 changes: 12 additions & 14 deletions src/app/tests/AppTestContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

#include <app/tests/AppTestContext.h>
#include "AppTestContext.h"

#include <access/AccessControl.h>
#include <access/examples/PermissiveAccessControlDelegate.h>
Expand All @@ -40,11 +40,10 @@ namespace Test {

void AppContext::SetUpTestSuite()
{
CHIP_ERROR err = CHIP_NO_ERROR;
LoopbackMessagingContext::SetUpTestSuite();
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = chip::DeviceLayer::PlatformMgr().InitChipStack()) == CHIP_NO_ERROR, AppServer,
"Init CHIP stack failed: %" CHIP_ERROR_FORMAT, err.Format());
VerifyOrReturn(!HasFailure()); // Stop if parent had a failure.

ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR);
}

void AppContext::TearDownTestSuite()
Expand All @@ -67,24 +66,23 @@ void AppContext::TearDownTestSuite()

void AppContext::SetUp()
{
CHIP_ERROR err = CHIP_NO_ERROR;
LoopbackMessagingContext::SetUp();
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(),
app::reporting::GetDefaultReportScheduler())) ==
CHIP_NO_ERROR,
AppServer, "Init InteractionModelEngine failed: %" CHIP_ERROR_FORMAT, err.Format());
VerifyOrReturn(!HasFailure()); // Stop if parent had a failure.

ASSERT_EQ(app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(),
app::reporting::GetDefaultReportScheduler()),
CHIP_NO_ERROR);
Access::SetAccessControl(gPermissiveAccessControl);
VerifyOrDieWithMsg((err = Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(),
gDeviceTypeResolver)) == CHIP_NO_ERROR,
AppServer, "Init AccessControl failed: %" CHIP_ERROR_FORMAT, err.Format());
ASSERT_EQ(Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(), gDeviceTypeResolver),
CHIP_NO_ERROR);
}

void AppContext::TearDown()
{
Access::GetAccessControl().Finish();
Access::ResetAccessControlToDefault();
chip::app::InteractionModelEngine::GetInstance()->Shutdown();

LoopbackMessagingContext::TearDown();
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/tests/AppTestContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class AppContext : public LoopbackMessagingContext
// Performs shared teardown for all tests in the test suite
static void TearDownTestSuite();
// Performs setup for each individual test in the test suite
void SetUp();
virtual void SetUp();
// Performs teardown for each individual test in the test suite
void TearDown();
virtual void TearDown();
};

} // namespace Test
Expand Down
5 changes: 4 additions & 1 deletion src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ static_library("helpers") {
"${chip_root}/src/transport/raw/tests:helpers",
]

public_deps = [ "${chip_root}/src/messaging/tests:helpers" ]
public_deps = [
"${chip_root}/src/lib/support/tests:pw-test-macros",
"${chip_root}/src/messaging/tests:helpers",
]
}

source_set("binding-test-srcs") {
Expand Down
47 changes: 14 additions & 33 deletions src/app/tests/TestAclAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,50 +109,31 @@ class MockInteractionModelApp : public chip::app::ReadClient::Callback
namespace chip {
namespace app {

class TestAclAttribute : public ::testing::Test
class TestAclAttribute : public Test::AppContext
{
public:
static void SetUpTestSuite()
{

mpTestContext = new chip::Test::AppContext;
mpTestContext->SetUpTestSuite();
}
static void TearDownTestSuite()
{
mpTestContext->TearDownTestSuite();
delete mpTestContext;
}

void SetUp() override
{
mpTestContext->SetUp();
AppContext::SetUp();

Access::GetAccessControl().Finish();
Access::GetAccessControl().Init(GetTestAccessControlDelegate(), gDeviceTypeResolver);
}
void TearDown() override { mpTestContext->TearDown(); }

static chip::Test::AppContext * mpTestContext;
};

chip::Test::AppContext * TestAclAttribute::mpTestContext = nullptr;

// Read Client sends a malformed subscribe request, interaction model engine fails to parse the request and generates a status
// report to client, and client is closed.
TEST_F(TestAclAttribute, TestACLDeniedAttribute)
{
Messaging::ReliableMessageMgr * rm = mpTestContext->GetExchangeManager().GetReliableMessageMgr();
Messaging::ReliableMessageMgr * rm = GetExchangeManager().GetReliableMessageMgr();
EXPECT_EQ(rm->TestGetCountRetransTable(), 0);

MockInteractionModelApp delegate;
auto * engine = chip::app::InteractionModelEngine::GetInstance();
EXPECT_EQ(engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
app::reporting::GetDefaultReportScheduler()),
CHIP_NO_ERROR);
EXPECT_EQ(engine->Init(&GetExchangeManager(), &GetFabricTable(), app::reporting::GetDefaultReportScheduler()), CHIP_NO_ERROR);

{
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &GetExchangeManager(), delegate,
chip::app::ReadClient::InteractionType::Subscribe);

chip::app::AttributePathParams attributePathParams[2];
Expand All @@ -164,21 +145,21 @@ TEST_F(TestAclAttribute, TestACLDeniedAttribute)
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId1;
attributePathParams[1].mAttributeId = 2;

ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
ReadPrepareParams readPrepareParams(GetSessionBobToAlice());
readPrepareParams.mpAttributePathParamsList = attributePathParams;
readPrepareParams.mAttributePathParamsListSize = 2;

EXPECT_EQ(readClient.SendRequest(readPrepareParams), CHIP_NO_ERROR);

mpTestContext->DrainAndServiceIO();
DrainAndServiceIO();
EXPECT_EQ(delegate.mError, CHIP_IM_GLOBAL_STATUS(InvalidAction));
EXPECT_FALSE(delegate.mGotReport);
delegate.mError = CHIP_NO_ERROR;
delegate.mGotReport = false;
}

{
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &GetExchangeManager(), delegate,
chip::app::ReadClient::InteractionType::Subscribe);

chip::app::AttributePathParams attributePathParams[2];
Expand All @@ -189,21 +170,21 @@ TEST_F(TestAclAttribute, TestACLDeniedAttribute)
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId2;
attributePathParams[1].mAttributeId = 2;

ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
ReadPrepareParams readPrepareParams(GetSessionBobToAlice());
readPrepareParams.mpAttributePathParamsList = attributePathParams;
readPrepareParams.mAttributePathParamsListSize = 2;

EXPECT_EQ(readClient.SendRequest(readPrepareParams), CHIP_NO_ERROR);

mpTestContext->DrainAndServiceIO();
DrainAndServiceIO();
EXPECT_EQ(delegate.mError, CHIP_IM_GLOBAL_STATUS(InvalidAction));
EXPECT_FALSE(delegate.mGotReport);
delegate.mError = CHIP_NO_ERROR;
delegate.mGotReport = false;
}

{
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &GetExchangeManager(), delegate,
chip::app::ReadClient::InteractionType::Subscribe);

chip::app::AttributePathParams attributePathParams[2];
Expand All @@ -215,13 +196,13 @@ TEST_F(TestAclAttribute, TestACLDeniedAttribute)
attributePathParams[1].mClusterId = chip::Test::kTestClusterId;
attributePathParams[1].mAttributeId = 2;

ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
ReadPrepareParams readPrepareParams(GetSessionBobToAlice());
readPrepareParams.mpAttributePathParamsList = attributePathParams;
readPrepareParams.mAttributePathParamsListSize = 2;

EXPECT_EQ(readClient.SendRequest(readPrepareParams), CHIP_NO_ERROR);

mpTestContext->DrainAndServiceIO();
DrainAndServiceIO();
EXPECT_EQ(delegate.mError, CHIP_NO_ERROR);
EXPECT_TRUE(delegate.mGotReport);
EXPECT_EQ(engine->GetNumActiveReadHandlers(ReadHandler::InteractionType::Subscribe), 1u);
Expand All @@ -231,7 +212,7 @@ TEST_F(TestAclAttribute, TestACLDeniedAttribute)

EXPECT_EQ(engine->GetNumActiveReadClients(), 0u);
engine->Shutdown();
EXPECT_EQ(mpTestContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u);
}
} // namespace app
} // namespace chip
Loading

0 comments on commit 2357297

Please sign in to comment.