Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated AppContext, LoopbackMessagingContext, and UDPMessagingContext to serve as PW test fixture classes. #34036

Merged
merged 27 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f3bf109
Modified MessagingContext and its subclasses as well as AppContext to…
feasel0 May 17, 2024
1980820
Merge branch 'master' into feature/unittest--doc--example
feasel0 Jun 18, 2024
d43b087
Modifed all tests that use LoopbackMessagingContext or AppContext. M…
feasel0 Jun 20, 2024
a02c9d7
Removed unneeded scope from call to parent setup/teardown
feasel0 Jun 20, 2024
302bf20
Made some helper functions into class methods.
feasel0 Jun 21, 2024
4fefac4
Merge branch 'master' into feature/appcontext-refactoring
feasel0 Jun 21, 2024
1c12067
Restyled by prettier-markdown
restyled-commits Jun 21, 2024
b5c630f
Reverted TestICDManager back to using LoopbackMessagingContext
feasel0 Jul 1, 2024
2d7a896
fixing merge conflicts
feasel0 Jul 3, 2024
8f75457
fixing merge conflicts
feasel0 Jul 3, 2024
75d3af3
Merge https://github.com/feasel0/connectedhomeip into feature/appcont…
feasel0 Jul 3, 2024
c2865e4
Merge branch 'master' into feature/appcontext-refactoring
feasel0 Jul 3, 2024
c043105
Fixed duplicate code and missing semicolon
feasel0 Jul 3, 2024
f9e3fef
Merge branch 'feature/appcontext-refactoring' of https://github.com/f…
feasel0 Jul 3, 2024
789d6c0
Trying to update TestReadHandler_DataVersionFiltersTruncated
feasel0 Jul 3, 2024
6525d9f
Restyled by whitespace
restyled-commits Jul 3, 2024
2460624
Restyled by clang-format
restyled-commits Jul 3, 2024
f61c712
Merge branch 'master' into feature/appcontext-refactoring
feasel0 Jul 3, 2024
eaa0e34
fixed problem with the new TestReadHandler_DataVersionFiltersTruncated
feasel0 Jul 3, 2024
3d37fc4
Merge branch 'feature/appcontext-refactoring' of https://github.com/f…
feasel0 Jul 3, 2024
f4f25fc
Merge branch 'master' into feature/appcontext-refactoring
feasel0 Jul 4, 2024
4e34d77
fix in tracing
feasel0 Jul 4, 2024
97e1e3e
Merge branch 'feature/appcontext-refactoring' of https://github.com/f…
feasel0 Jul 4, 2024
a85954e
Merge branch 'master' into feature/appcontext-refactoring
feasel0 Jul 4, 2024
57ab3d3
Changes to MessagingContext member variable initialization in respons…
feasel0 Jul 5, 2024
71f1caf
Merge branch 'feature/appcontext-refactoring' of https://github.com/f…
feasel0 Jul 5, 2024
7c35ed2
Used std::unique_ptr for MessagingContext.mpData
feasel0 Jul 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading