Skip to content

Commit

Permalink
clean TestAclAttribute and TestAclEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Alami-Amine committed May 28, 2024
1 parent 74d663b commit 6d7f58e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
51 changes: 23 additions & 28 deletions src/app/tests/TestAclAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ namespace {
using namespace chip;
using namespace chip::Access;

// chip::ClusterId kTestClusterId = 1;
// chip::ClusterId kTestDeniedClusterId1 = 1000;
// chip::ClusterId kTestDeniedClusterId2 = 3;
// chip::EndpointId kTestEndpointId = 4;

class TestAccessControlDelegate : public AccessControl::Delegate
{
public:
Expand Down Expand Up @@ -119,59 +114,59 @@ class TestAclAttribute : public ::testing::Test
static void SetUpTestSuite()
{

pTestContext = new chip::Test::AppContext;
mpTestContext = new chip::Test::AppContext;

pTestContext->SetUpTestSuite();
mpTestContext->SetUpTestSuite();
}
static void TearDownTestSuite()
{
pTestContext->TearDownTestSuite();
if (pTestContext != nullptr)
mpTestContext->TearDownTestSuite();
if (mpTestContext != nullptr)
{
delete pTestContext;
delete mpTestContext;
}
}

void SetUp() override
{

if (pTestContext != nullptr)
if (mpTestContext != nullptr)
{
pTestContext->SetUp();
mpTestContext->SetUp();

Access::GetAccessControl().Finish();
Access::GetAccessControl().Init(GetTestAccessControlDelegate(), gDeviceTypeResolver);
}
}
void TearDown() override
{
if (pTestContext != nullptr)
if (mpTestContext != nullptr)
{
pTestContext->TearDown();
mpTestContext->TearDown();
}
}
static chip::Test::AppContext * pTestContext;
static chip::Test::AppContext * mpTestContext;
};

chip::Test::AppContext * TestAclAttribute::pTestContext = nullptr;
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)
{
CHIP_ERROR err = CHIP_NO_ERROR;

Messaging::ReliableMessageMgr * rm = pTestContext->GetExchangeManager().GetReliableMessageMgr();
Messaging::ReliableMessageMgr * rm = mpTestContext->GetExchangeManager().GetReliableMessageMgr();
EXPECT_EQ(rm->TestGetCountRetransTable(), 0);

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

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

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

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

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

pTestContext->DrainAndServiceIO();
mpTestContext->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(), &pTestContext->GetExchangeManager(), delegate,
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
chip::app::ReadClient::InteractionType::Subscribe);

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

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

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

pTestContext->DrainAndServiceIO();
mpTestContext->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(), &pTestContext->GetExchangeManager(), delegate,
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
chip::app::ReadClient::InteractionType::Subscribe);

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

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

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

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

EXPECT_EQ(engine->GetNumActiveReadClients(), 0u);
engine->Shutdown();
EXPECT_EQ(pTestContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
EXPECT_EQ(mpTestContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
}
} // namespace app
} // namespace chip
13 changes: 6 additions & 7 deletions src/app/tests/TestAclEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <app/MessageDef/EventDataIB.h>
#include <app/reporting/tests/MockReportScheduler.h>
#include <app/tests/AppTestContext.h>
#include <app/tests/ember-test-compatibility.h>
#include <app/util/basic-types.h>
#include <app/util/mock/Constants.h>
#include <app/util/mock/Functions.h>
Expand All @@ -47,7 +48,6 @@ uint8_t gDebugEventBuffer[128];
uint8_t gInfoEventBuffer[128];
uint8_t gCritEventBuffer[128];
chip::app::CircularEventBuffer gCircularEventBuffer[3];
chip::ClusterId kTestClusterId1 = 100;
chip::ClusterId kTestClusterId2 = 7;
chip::EndpointId kTestEndpointId = 1;
chip::EventId kTestEventIdDebug = 1;
Expand All @@ -61,7 +61,7 @@ class TestAccessControlDelegate : public AccessControl::Delegate
CHIP_ERROR Check(const SubjectDescriptor & subjectDescriptor, const chip::Access::RequestPath & requestPath,
Privilege requestPrivilege) override
{
if (requestPath.cluster == kTestClusterId1)
if (requestPath.cluster == chip::Test::kTestDeniedClusterId2)
{
return CHIP_ERROR_ACCESS_DENIED;
}
Expand Down Expand Up @@ -104,7 +104,7 @@ void GenerateEvents()
CHIP_ERROR err = CHIP_NO_ERROR;
chip::EventNumber eid1, eid2;
chip::app::EventOptions options1;
options1.mPath = { kTestEndpointId, kTestClusterId1, kTestEventIdDebug };
options1.mPath = { kTestEndpointId, chip::Test::kTestDeniedClusterId2, kTestEventIdDebug };
options1.mPriority = chip::app::PriorityLevel::Info;

chip::app::EventOptions options2;
Expand Down Expand Up @@ -215,7 +215,6 @@ class TestAclEvent : public ::testing::Test
// Performs teardown for each individual test in the test suite
void TearDown() override
{

chip::app::EventManagement::DestroyEventManagement();
mpTestContext->TearDown();
}
Expand Down Expand Up @@ -248,7 +247,7 @@ TEST_F(TestAclEvent, TestReadRoundtripWithEventStatusIBInEventReport)
{
chip::app::EventPathParams eventPathParams[1];
eventPathParams[0].mEndpointId = kTestEndpointId;
eventPathParams[0].mClusterId = kTestClusterId1;
eventPathParams[0].mClusterId = chip::Test::kTestDeniedClusterId2;
eventPathParams[0].mEventId = kTestEventIdDebug;

ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
Expand Down Expand Up @@ -280,7 +279,7 @@ TEST_F(TestAclEvent, TestReadRoundtripWithEventStatusIBInEventReport)
{
chip::app::EventPathParams eventPathParams[1];
eventPathParams[0].mEndpointId = kTestEndpointId;
eventPathParams[0].mClusterId = kTestClusterId1;
eventPathParams[0].mClusterId = chip::Test::kTestDeniedClusterId2;

ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
readPrepareParams.mpEventPathParamsList = eventPathParams;
Expand Down Expand Up @@ -337,7 +336,7 @@ TEST_F(TestAclEvent, TestReadRoundtripWithEventStatusIBInEventReport)
{
chip::app::EventPathParams eventPathParams[2];
eventPathParams[0].mEndpointId = kTestEndpointId;
eventPathParams[0].mClusterId = kTestClusterId1;
eventPathParams[0].mClusterId = chip::Test::kTestDeniedClusterId2;
eventPathParams[0].mEventId = kTestEventIdDebug;
eventPathParams[1].mEndpointId = kTestEndpointId;
eventPathParams[1].mClusterId = kTestClusterId2;
Expand Down

0 comments on commit 6d7f58e

Please sign in to comment.