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
Changes from 10 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
1 change: 0 additions & 1 deletion src/BUILD.gn
Original file line number Diff line number Diff line change
@@ -96,7 +96,6 @@ if (chip_build_tests) {
if (chip_device_platform != "efr32") {
tests += [
"${chip_root}/src/app/tests",
"${chip_root}/src/app/tests:tests_nltest",
"${chip_root}/src/credentials/tests",
"${chip_root}/src/lib/format/tests",
"${chip_root}/src/lib/support/tests",
93 changes: 33 additions & 60 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
@@ -141,6 +141,8 @@ chip_test_suite("tests") {
output_name = "libAppTests"

test_sources = [
"TestAclAttribute.cpp",
"TestAclEvent.cpp",
"TestAttributeAccessInterfaceCache.cpp",
"TestAttributePathExpandIterator.cpp",
"TestAttributePathParams.cpp",
@@ -150,37 +152,34 @@ chip_test_suite("tests") {
"TestBasicCommandPathRegistry.cpp",
"TestBindingTable.cpp",
"TestBuilderParser.cpp",
"TestCommandInteraction.cpp",
"TestCommandPathParams.cpp",
"TestConcreteAttributePath.cpp",
"TestDataModelSerialization.cpp",
"TestDefaultOTARequestorStorage.cpp",
"TestEventLoggingNoUTCTime.cpp",
"TestEventOverflow.cpp",
"TestEventPathParams.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 = [
@@ -199,6 +198,7 @@ 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",
]

if (chip_device_platform != "android") {
@@ -211,27 +211,29 @@ chip_test_suite("tests") {
":scenes-table-test-srcs",
]
}
}

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",
"TestCommandInteraction.cpp",
"TestEventLoggingNoUTCTime.cpp",
"TestEventOverflow.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.
#
@@ -248,33 +250,4 @@ chip_test_suite_using_nltest("tests_nltest") {
chip_device_platform != "openiotsdk" && chip_device_platform != "fake") {
test_sources += [ "TestEventLogging.cpp" ]
}

cflags = [ "-Wconversion" ]

public_deps = [
":app-test-stubs",
":binding-test-srcs",
":operational-state-test-srcs",
":ota-requestor-test-srcs",
":time-sync-data-provider-test-srcs",
"${chip_root}/src/app",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/app/icd/client:manager",
"${chip_root}/src/app/tests:helpers",
"${chip_root}/src/app/util/mock:mock_ember",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support:test_utils",
"${chip_root}/src/lib/support:testing",
"${chip_root}/src/lib/support:testing_nlunit",
"${nlunit_test_root}:nlunit-test",
]

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",
]
}
}
142 changes: 68 additions & 74 deletions src/app/tests/TestAclAttribute.cpp
Original file line number Diff line number Diff line change
@@ -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>
@@ -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>
@@ -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:
@@ -121,29 +109,63 @@ 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;

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

void SetUp() override
{

if (mpTestContext != nullptr)
{
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;

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);
EXPECT_EQ(engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
app::reporting::GetDefaultReportScheduler()),
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];
@@ -155,22 +177,21 @@ 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(readClient.SendRequest(readPrepareParams), 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];
@@ -181,22 +202,21 @@ 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(readClient.SendRequest(readPrepareParams), 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];
@@ -208,49 +228,23 @@ 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(readClient.SendRequest(readPrepareParams), 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