Skip to content

Commit

Permalink
removing TestACLAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Alami-Amine committed May 21, 2024
1 parent 41a4bb8 commit 64020eb
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ chip_test_suite("tests") {
"WriteHandlerTestAccess.h",
]
test_sources = [
"TestAclAttribute.cpp",
"TestBuilderParser.cpp",
"TestCommandInteraction.cpp",
"TestMessageDef.cpp",
Expand Down Expand Up @@ -182,6 +181,7 @@ chip_test_suite_using_nltest("tests_nltest") {
output_name = "libAppTestsNL"

test_sources = [
"TestAclAttribute.cpp",
"TestAclEvent.cpp",
"TestAttributeAccessInterfaceCache.cpp",
"TestAttributePathExpandIterator.cpp",
Expand Down
169 changes: 85 additions & 84 deletions src/app/tests/TestAclAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* limitations under the License.
*/

#include <pw_unit_test/framework.h>

#include <access/examples/PermissiveAccessControlDelegate.h>
#include <app/ConcreteAttributePath.h>
#include <app/ConcreteEventPath.h>
Expand All @@ -26,7 +24,6 @@
#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 @@ -36,8 +33,11 @@
#include <lib/core/TLVDebug.h>
#include <lib/core/TLVUtilities.h>
#include <lib/support/CHIPCounter.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <messaging/ExchangeContext.h>
#include <messaging/Flags.h>
#include <nlunit-test.h>
#include <protocols/interaction_model/Constants.h>

#include <type_traits>
Expand All @@ -46,18 +46,18 @@ namespace {
using namespace chip;
using namespace chip::Access;

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

class TestAccessControlDelegate : public AccessControl::Delegate
{
public:
CHIP_ERROR Check(const SubjectDescriptor & subjectDescriptor, const chip::Access::RequestPath & requestPath,
Privilege requestPrivilege) override
{
if (requestPath.cluster == chip::Test::kTestDeniedClusterId2)
if (requestPath.cluster == kTestDeniedClusterId2)
{
return CHIP_ERROR_ACCESS_DENIED;
}
Expand All @@ -77,6 +77,18 @@ class TestDeviceTypeResolver : public AccessControl::DeviceTypeResolver
bool IsDeviceTypeOnEndpoint(DeviceTypeId deviceType, EndpointId endpoint) override { return false; }
} gDeviceTypeResolver;

class TestAccessContext : public chip::Test::AppContext
{
public:
// Performs setup for each individual test in the test suite
void SetUp() override
{
chip::Test::AppContext::SetUp();
Access::GetAccessControl().Finish();
Access::GetAccessControl().Init(GetTestAccessControlDelegate(), gDeviceTypeResolver);
}
};

class MockInteractionModelApp : public chip::app::ReadClient::Callback
{
public:
Expand Down Expand Up @@ -113,147 +125,136 @@ class MockInteractionModelApp : public chip::app::ReadClient::Callback
namespace chip {
namespace app {

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

pTestContext = new chip::Test::AppContext;

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

void SetUp() override
{

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

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

chip::Test::AppContext * TestAclAttribute::pTestContext = 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)
void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apContext)
{
CHIP_ERROR err = CHIP_NO_ERROR;
TestAccessContext & ctx = *static_cast<TestAccessContext *>(apContext);
CHIP_ERROR err = CHIP_NO_ERROR;

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

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

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

chip::app::AttributePathParams attributePathParams[2];
attributePathParams[0].mEndpointId = chip::Test::kTestEndpointId;
attributePathParams[0].mClusterId = chip::Test::kTestDeniedClusterId1;
attributePathParams[0].mEndpointId = kTestEndpointId;
attributePathParams[0].mClusterId = kTestDeniedClusterId1;
attributePathParams[0].mAttributeId = 1;

attributePathParams[1].mEndpointId = chip::Test::kTestEndpointId;
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId1;
attributePathParams[1].mEndpointId = kTestEndpointId;
attributePathParams[1].mClusterId = kTestDeniedClusterId1;
attributePathParams[1].mAttributeId = 2;

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

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

pTestContext->DrainAndServiceIO();
EXPECT_EQ(delegate.mError, CHIP_IM_GLOBAL_STATUS(InvalidAction));
EXPECT_FALSE(delegate.mGotReport);
ctx.DrainAndServiceIO();
NL_TEST_ASSERT(apSuite, delegate.mError == CHIP_IM_GLOBAL_STATUS(InvalidAction));
NL_TEST_ASSERT(apSuite, !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(), &ctx.GetExchangeManager(), delegate,
chip::app::ReadClient::InteractionType::Subscribe);

chip::app::AttributePathParams attributePathParams[2];

attributePathParams[0].mClusterId = chip::Test::kTestDeniedClusterId2;
attributePathParams[0].mClusterId = kTestDeniedClusterId2;
attributePathParams[0].mAttributeId = 1;

attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId2;
attributePathParams[1].mClusterId = kTestDeniedClusterId2;
attributePathParams[1].mAttributeId = 2;

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

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

pTestContext->DrainAndServiceIO();
EXPECT_EQ(delegate.mError, CHIP_IM_GLOBAL_STATUS(InvalidAction));
EXPECT_FALSE(delegate.mGotReport);
ctx.DrainAndServiceIO();
NL_TEST_ASSERT(apSuite, delegate.mError == CHIP_IM_GLOBAL_STATUS(InvalidAction));
NL_TEST_ASSERT(apSuite, !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(), &ctx.GetExchangeManager(), delegate,
chip::app::ReadClient::InteractionType::Subscribe);

chip::app::AttributePathParams attributePathParams[2];
attributePathParams[0].mEndpointId = chip::Test::kTestEndpointId;
attributePathParams[0].mClusterId = chip::Test::kTestDeniedClusterId1;
attributePathParams[0].mEndpointId = kTestEndpointId;
attributePathParams[0].mClusterId = kTestDeniedClusterId1;
attributePathParams[0].mAttributeId = 1;

attributePathParams[1].mEndpointId = chip::Test::kTestEndpointId;
attributePathParams[1].mClusterId = chip::Test::kTestClusterId;
attributePathParams[1].mEndpointId = kTestEndpointId;
attributePathParams[1].mClusterId = kTestClusterId;
attributePathParams[1].mAttributeId = 2;

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

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

pTestContext->DrainAndServiceIO();
EXPECT_EQ(delegate.mError, CHIP_NO_ERROR);
EXPECT_TRUE(delegate.mGotReport);
EXPECT_EQ(engine->GetNumActiveReadHandlers(ReadHandler::InteractionType::Subscribe), 1u);
ctx.DrainAndServiceIO();
NL_TEST_ASSERT(apSuite, delegate.mError == CHIP_NO_ERROR);
NL_TEST_ASSERT(apSuite, delegate.mGotReport);
NL_TEST_ASSERT(apSuite, engine->GetNumActiveReadHandlers(ReadHandler::InteractionType::Subscribe) == 1);
delegate.mError = CHIP_NO_ERROR;
delegate.mGotReport = false;
}

EXPECT_EQ(engine->GetNumActiveReadClients(), 0u);
NL_TEST_ASSERT(apSuite, engine->GetNumActiveReadClients() == 0);
engine->Shutdown();
EXPECT_EQ(pTestContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0);
}
} // namespace app
} // namespace chip

namespace {

const nlTest sTests[] = {
NL_TEST_DEF("TestACLDeniedAttribute", chip::app::TestAclAttribute::TestACLDeniedAttribute),
NL_TEST_SENTINEL(),
};

nlTestSuite sSuite = {
"TestAclAttribute",
&sTests[0],
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

int TestAclAttribute()
{
return chip::ExecuteTestsWithContext<TestAccessContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestAclAttribute)

0 comments on commit 64020eb

Please sign in to comment.