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-1 and stub-refactoring #33638

Merged
merged 13 commits into from
Jun 3, 2024
1 change: 1 addition & 0 deletions src/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ 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",
Expand Down
6 changes: 6 additions & 0 deletions src/app/tests/AppTestContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ void AppContext::SetUpTestSuite()

void AppContext::TearDownTestSuite()
{
// Adding a DrainAndServiceIO call fixes the teardown for some tests (TestAclAttribute and TestReportScheduler); these were
// triggering failures in tests that follow them when running on nRF CI (Zephyr native_posix where all Unit Tests are compiled
// into a single file)
// TODO: understand this more and solve underlying issue
LoopbackMessagingContext::DrainAndServiceIO();
Alami-Amine marked this conversation as resolved.
Show resolved Hide resolved

chip::DeviceLayer::PlatformMgr().Shutdown();
LoopbackMessagingContext::TearDownTestSuite();
}
Expand Down
76 changes: 59 additions & 17 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/nlunit_test.gni")
import("//build_overrides/pigweed.gni")

import("${chip_root}/build/chip/chip_test_suite.gni")
import("${chip_root}/src/app/icd/icd.gni")
Expand All @@ -37,9 +38,10 @@ static_library("helpers") {
"${chip_root}/src/access",
"${chip_root}/src/app",
"${chip_root}/src/lib/support",
"${chip_root}/src/messaging/tests:helpers",
"${chip_root}/src/transport/raw/tests:helpers",
]

public_deps = [ "${chip_root}/src/messaging/tests:helpers" ]
}

source_set("binding-test-srcs") {
Expand Down Expand Up @@ -119,19 +121,67 @@ source_set("operational-state-test-srcs") {
]
}

chip_test_suite_using_nltest("tests") {
source_set("ember-apptest-compatibility") {
sources = [
"ember-test-compatibility.cpp",
"ember-test-compatibility.h",
]
public_configs = [ "${chip_root}/src/lib/support/pw_log_chip:config" ]

public_deps = [
"${chip_root}/src/app/util/mock:mock_ember",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support",
]
}

chip_test_suite("tests") {
output_name = "libAppTests"

test_sources = [
"TestBindingTable.cpp",
"TestBuilderParser.cpp",
"TestMessageDef.cpp",
"TestNullable.cpp",
"TestStatusIB.cpp",
"TestStatusResponseMessage.cpp",
]

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

cflags = [ "-Wconversion" ]

public_deps = [
":binding-test-srcs",
":ember-apptest-compatibility",
":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_test_suite_using_nltest("tests_nltest") {
output_name = "libAppTestsNL"

test_sources = [
"TestAclAttribute.cpp",
"TestAclEvent.cpp",
"TestAttributeAccessInterfaceCache.cpp",
"TestAttributePathExpandIterator.cpp",
"TestAttributePersistenceProvider.cpp",
"TestAttributeValueDecoder.cpp",
"TestAttributeValueEncoder.cpp",
"TestBasicCommandPathRegistry.cpp",
"TestBindingTable.cpp",
"TestBuilderParser.cpp",
"TestClusterInfo.cpp",
"TestCommandInteraction.cpp",
"TestCommandPathParams.cpp",
Expand All @@ -143,29 +193,19 @@ chip_test_suite_using_nltest("tests") {
"TestEventPathParams.cpp",
"TestFabricScopedEventLogging.cpp",
"TestInteractionModelEngine.cpp",
"TestMessageDef.cpp",
"TestNullable.cpp",
"TestNumericAttributeTraits.cpp",
"TestOperationalStateClusterObjects.cpp",
"TestPendingNotificationMap.cpp",
"TestPendingResponseTrackerImpl.cpp",
"TestPowerSourceCluster.cpp",
"TestReadInteraction.cpp",
"TestReportingEngine.cpp",
"TestStatusIB.cpp",
"TestStatusResponseMessage.cpp",
"TestTestEventTriggerDelegate.cpp",
"TestTimeSyncDataProvider.cpp",
"TestTimedHandler.cpp",
"TestWriteInteraction.cpp",
]

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

test_sources += [ "TestAclAttribute.cpp" ]

# DefaultICDClientStorage assumes that raw AES key is used by the application
if (chip_crypto != "psa") {
test_sources += [ "TestDefaultICDClientStorage.cpp" ]
Expand All @@ -191,18 +231,20 @@ chip_test_suite_using_nltest("tests") {

# The platform manager is not properly clearing queues in test teardown, which results in
# DrainIO calls not being able to run in expected time (5seconds) if unprocessed reported engine
# runs are remaining, causing tests to crash in Open IoT SDK and Zephyr tests since they are
# runs are remaining, causing tests to crash in Open IoT SDK since they are
# running all tests in one file. We need to figure out how to properly clean the event queues
# before enabling this test for these platforms.
if (chip_device_platform != "nrfconnect" &&
chip_device_platform != "openiotsdk") {
# Update: This was fixed for Nordic Zephyr Tests by calling DrainAndServiceIO in AppContext::TearDownTestSuite()
# TODO: check if above solution works for openiotsdk as well
if (chip_device_platform != "openiotsdk") {
test_sources += [ "TestReportScheduler.cpp" ]
}

cflags = [ "-Wconversion" ]

public_deps = [
":binding-test-srcs",
":ember-apptest-compatibility",
":operational-state-test-srcs",
":ota-requestor-test-srcs",
":time-sync-data-provider-test-srcs",
Expand Down
43 changes: 12 additions & 31 deletions src/app/tests/TestAclAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -46,18 +47,13 @@ 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:
CHIP_ERROR Check(const SubjectDescriptor & subjectDescriptor, const chip::Access::RequestPath & requestPath,
Privilege requestPrivilege) override
{
if (requestPath.cluster == kTestDeniedClusterId2)
if (requestPath.cluster == chip::Test::kTestDeniedClusterId2)
Alami-Amine marked this conversation as resolved.
Show resolved Hide resolved
{
return CHIP_ERROR_ACCESS_DENIED;
}
Expand Down Expand Up @@ -125,21 +121,6 @@ class MockInteractionModelApp : public chip::app::ReadClient::Callback
namespace chip {
namespace app {

bool ConcreteAttributePathExists(const ConcreteAttributePath & aPath)
{
return aPath.mClusterId != kTestDeniedClusterId1;
}

Protocols::InteractionModel::Status CheckEventSupportStatus(const ConcreteEventPath & aPath)
{
if (aPath.mClusterId == kTestDeniedClusterId1)
{
return Protocols::InteractionModel::Status::UnsupportedCluster;
}

return Protocols::InteractionModel::Status::Success;
}

class TestAclAttribute
{
public:
Expand All @@ -166,12 +147,12 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
chip::app::ReadClient::InteractionType::Subscribe);

chip::app::AttributePathParams attributePathParams[2];
attributePathParams[0].mEndpointId = kTestEndpointId;
attributePathParams[0].mClusterId = kTestDeniedClusterId1;
attributePathParams[0].mEndpointId = chip::Test::kTestEndpointId;
attributePathParams[0].mClusterId = chip::Test::kTestDeniedClusterId1;
Alami-Amine marked this conversation as resolved.
Show resolved Hide resolved
attributePathParams[0].mAttributeId = 1;

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

ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
Expand All @@ -194,10 +175,10 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo

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

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

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

ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
Expand All @@ -219,12 +200,12 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
chip::app::ReadClient::InteractionType::Subscribe);

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

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

ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
Expand Down
Loading
Loading