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

pw_unit_test migration: apps final batch #33772

Merged
merged 15 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
95 changes: 48 additions & 47 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -141,39 +141,38 @@ chip_test_suite("tests") {
output_name = "libAppTests"

test_sources = [
"TestAclAttribute.cpp",
"TestAclEvent.cpp",
"TestAttributeAccessInterfaceCache.cpp",
"TestAttributePathExpandIterator.cpp",
"TestAttributePersistenceProvider.cpp",
"TestAttributeValueDecoder.cpp",
"TestAttributeValueEncoder.cpp",
"TestBindingTable.cpp",
"TestBuilderParser.cpp",
"TestCommandInteraction.cpp",
"TestEventLoggingNoUTCTime.cpp",
"TestEventOverflow.cpp",
"TestFabricScopedEventLogging.cpp",
"TestInteractionModelEngine.cpp",
"TestMessageDef.cpp",
"TestNullable.cpp",
"TestNumericAttributeTraits.cpp",
"TestOperationalStateClusterObjects.cpp",
"TestPendingNotificationMap.cpp",
"TestPendingResponseTrackerImpl.cpp",
"TestPowerSourceCluster.cpp",
"TestReadInteraction.cpp",
"TestReportScheduler.cpp",
"TestReportingEngine.cpp",
"TestStatusIB.cpp",
"TestStatusResponseMessage.cpp",
"TestTestEventTriggerDelegate.cpp",
"TestTimeSyncDataProvider.cpp",
"TestTimedHandler.cpp",
"TestWriteInteraction.cpp",
]

if (!chip_fake_platform) {
test_sources += [ "TestFailSafeContext.cpp" ]
}

# DefaultICDClientStorage assumes that raw AES key is used by the application
if (chip_crypto != "psa") {
test_sources += [ "TestDefaultICDClientStorage.cpp" ]
}

if (chip_persist_subscriptions) {
test_sources += [ "TestSimpleSubscriptionResumptionStorage.cpp" ]
}

cflags = [ "-Wconversion" ]

public_deps = [
Expand All @@ -192,35 +191,31 @@ chip_test_suite("tests") {
"${chip_root}/src/lib/core:string-builder-adapters",
"${chip_root}/src/lib/support:test_utils",
"${chip_root}/src/lib/support:testing",
"${chip_root}/src/lib/support/tests:pw-test-macros",
]
}

chip_test_suite_using_nltest("tests_nltest") {
output_name = "libAppTestsNL"
if (chip_config_network_layer_ble &&
(chip_device_platform == "linux" || chip_device_platform == "darwin")) {
test_sources += [ "TestCommissioningWindowManager.cpp" ]
public_deps += [
"${chip_root}/src/app/server",
"${chip_root}/src/messaging/tests/echo:common",
]
}

test_sources = [
"TestAclAttribute.cpp",
"TestAclEvent.cpp",
"TestBasicCommandPathRegistry.cpp",
"TestClusterInfo.cpp",
"TestCommandInteraction.cpp",
"TestCommandPathParams.cpp",
"TestConcreteAttributePath.cpp",
"TestDataModelSerialization.cpp",
"TestDefaultOTARequestorStorage.cpp",
"TestEventLoggingNoUTCTime.cpp",
"TestEventOverflow.cpp",
"TestEventPathParams.cpp",
"TestFabricScopedEventLogging.cpp",
"TestInteractionModelEngine.cpp",
"TestReadInteraction.cpp",
"TestReportScheduler.cpp",
"TestReportingEngine.cpp",
"TestTimedHandler.cpp",
"TestWriteInteraction.cpp",
]
if (!chip_fake_platform) {
test_sources += [ "TestFailSafeContext.cpp" ]
}

# DefaultICDClientStorage assumes that raw AES key is used by the application
if (chip_crypto != "psa") {
test_sources += [ "TestDefaultICDClientStorage.cpp" ]
}

if (chip_persist_subscriptions) {
test_sources += [ "TestSimpleSubscriptionResumptionStorage.cpp" ]
}

#
# On NRF platforms, the allocation of a large number of pbufs in this test
# to exercise chunking causes it to run out of memory. For now, disable it there.
#
Expand All @@ -237,6 +232,20 @@ chip_test_suite_using_nltest("tests_nltest") {
chip_device_platform != "openiotsdk" && chip_device_platform != "fake") {
test_sources += [ "TestEventLogging.cpp" ]
}
}

chip_test_suite_using_nltest("tests_nltest") {
output_name = "libAppTestsNL"

test_sources = [
"TestBasicCommandPathRegistry.cpp",
"TestClusterInfo.cpp",
"TestCommandPathParams.cpp",
"TestConcreteAttributePath.cpp",
"TestDataModelSerialization.cpp",
"TestDefaultOTARequestorStorage.cpp",
"TestEventPathParams.cpp",
]

cflags = [ "-Wconversion" ]

Expand All @@ -252,6 +261,7 @@ chip_test_suite_using_nltest("tests_nltest") {
"${chip_root}/src/app/tests:helpers",
"${chip_root}/src/app/util/mock:mock_ember",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/core:string-builder-adapters",
"${chip_root}/src/lib/support:test_utils",
"${chip_root}/src/lib/support:testing",
"${chip_root}/src/lib/support:testing_nlunit",
Expand All @@ -268,13 +278,4 @@ chip_test_suite_using_nltest("tests_nltest") {
":scenes-table-test-srcs",
]
}

if (chip_config_network_layer_ble &&
(chip_device_platform == "linux" || chip_device_platform == "darwin")) {
test_sources += [ "TestCommissionManager.cpp" ]
public_deps += [
"${chip_root}/src/app/server",
"${chip_root}/src/messaging/tests/echo:common",
]
}
}
139 changes: 69 additions & 70 deletions src/app/tests/TestAclAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
* limitations under the License.
*/

#include <lib/core/StringBuilderAdapters.h>
#include <pw_unit_test/framework.h>

#include <access/examples/PermissiveAccessControlDelegate.h>
#include <app/ConcreteAttributePath.h>
#include <app/ConcreteEventPath.h>
Expand All @@ -34,11 +37,8 @@
#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 Down Expand Up @@ -73,18 +73,6 @@ 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 @@ -121,29 +109,65 @@ class MockInteractionModelApp : public chip::app::ReadClient::Callback
namespace chip {
namespace app {

class TestAclAttribute
class TestAclAttribute : public ::testing::Test
{
public:
static void TestACLDeniedAttribute(nlTestSuite * apSuite, void * apContext);
static void SetUpTestSuite()
{

mpTestContext = new chip::Test::AppContext;
Alami-Amine marked this conversation as resolved.
Show resolved Hide resolved

mpTestContext->SetUpTestSuite();
}
static void TearDownTestSuite()
{
mpTestContext->TearDownTestSuite();
if (mpTestContext != nullptr)
Alami-Amine marked this conversation as resolved.
Show resolved Hide resolved
{
delete mpTestContext;
}
}

void SetUp() override
{

if (mpTestContext != nullptr)
Alami-Amine marked this conversation as resolved.
Show resolved Hide resolved
{
mpTestContext->SetUp();

Access::GetAccessControl().Finish();
Access::GetAccessControl().Init(GetTestAccessControlDelegate(), gDeviceTypeResolver);
}
}
void TearDown() override
{
if (mpTestContext != nullptr)
{
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.
void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apContext)
TEST_F(TestAclAttribute, TestACLDeniedAttribute)
{
TestAccessContext & ctx = *static_cast<TestAccessContext *>(apContext);
CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err = CHIP_NO_ERROR;

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

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

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

chip::app::AttributePathParams attributePathParams[2];
Expand All @@ -155,22 +179,22 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId1;
attributePathParams[1].mAttributeId = 2;

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

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

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

chip::app::AttributePathParams attributePathParams[2];
Expand All @@ -181,22 +205,22 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId2;
attributePathParams[1].mAttributeId = 2;

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

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

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

chip::app::AttributePathParams attributePathParams[2];
Expand All @@ -208,49 +232,24 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
attributePathParams[1].mClusterId = chip::Test::kTestClusterId;
attributePathParams[1].mAttributeId = 2;

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

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

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);
mpTestContext->DrainAndServiceIO();
EXPECT_EQ(delegate.mError, CHIP_NO_ERROR);
EXPECT_TRUE(delegate.mGotReport);
EXPECT_EQ(engine->GetNumActiveReadHandlers(ReadHandler::InteractionType::Subscribe), 1u);
delegate.mError = CHIP_NO_ERROR;
delegate.mGotReport = false;
}

NL_TEST_ASSERT(apSuite, engine->GetNumActiveReadClients() == 0);
EXPECT_EQ(engine->GetNumActiveReadClients(), 0u);
engine->Shutdown();
NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0);
EXPECT_EQ(mpTestContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
}
} // 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)
Loading
Loading