Skip to content

Commit

Permalink
pw_unit_test migration: apps-1 and stub-refactoring (project-chip#33638)
Browse files Browse the repository at this point in the history
* Decoupling ember functions + some tests

* Fix TearDown issue of some App Tests

* adding more tests

* Integrating comments

* Rename ember-test-compatibility to test-interaction-model-api

* adding ember test-utilities source files

* restyled patch

* integrating comments

* restyled
  • Loading branch information
Alami-Amine authored Jun 3, 2024
1 parent 8a64366 commit 5dd912f
Show file tree
Hide file tree
Showing 32 changed files with 2,323 additions and 2,921 deletions.
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
96 changes: 71 additions & 25 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,47 +121,44 @@ source_set("operational-state-test-srcs") {
]
}

chip_test_suite_using_nltest("tests") {
source_set("app-test-stubs") {
sources = [
"test-ember-api.cpp",
"test-ember-api.h",
"test-interaction-model-api.cpp",
"test-interaction-model-api.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 = [
"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",
"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) {
Expand All @@ -171,6 +170,56 @@ chip_test_suite_using_nltest("tests") {
test_sources += [ "TestDefaultICDClientStorage.cpp" ]
}

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

cflags = [ "-Wconversion" ]

public_deps = [
":app-test-stubs",
":binding-test-srcs",
":operational-state-test-srcs",
":ota-requestor-test-srcs",
":power-cluster-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/core:string-builder-adapters",
"${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",
"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",
]

#
# 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 @@ -192,6 +241,7 @@ chip_test_suite_using_nltest("tests") {
cflags = [ "-Wconversion" ]

public_deps = [
":app-test-stubs",
":binding-test-srcs",
":operational-state-test-srcs",
":ota-requestor-test-srcs",
Expand Down Expand Up @@ -227,8 +277,4 @@ chip_test_suite_using_nltest("tests") {
"${chip_root}/src/messaging/tests/echo:common",
]
}

if (chip_persist_subscriptions) {
test_sources += [ "TestSimpleSubscriptionResumptionStorage.cpp" ]
}
}
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/test-interaction-model-api.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)
{
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;
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
88 changes: 30 additions & 58 deletions src/app/tests/TestAttributeAccessInterfaceCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

#include <app/AttributeAccessInterface.h>
#include <app/AttributeAccessInterfaceCache.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>
#include <lib/core/StringBuilderAdapters.h>
#include <pw_unit_test/framework.h>

using namespace chip;
using namespace chip::app;

namespace {

void TestBasicLifecycle(nlTestSuite * inSuite, void * inContext)
TEST(TestAttributeAccessInterfaceCache, TestBasicLifecycle)
{
using CacheResult = AttributeAccessInterfaceCache::CacheResult;

Expand All @@ -44,86 +44,58 @@ void TestBasicLifecycle(nlTestSuite * inSuite, void * inContext)
// Cache can keep track of at least 1 entry,
AttributeAccessInterface * entry = nullptr;

NL_TEST_ASSERT(inSuite, cache.Get(1, 1, &entry) == CacheResult::kCacheMiss);
NL_TEST_ASSERT(inSuite, entry == nullptr);
EXPECT_EQ(cache.Get(1, 1, &entry), CacheResult::kCacheMiss);
EXPECT_EQ(entry, nullptr);
cache.MarkUsed(1, 1, accessor1);

NL_TEST_ASSERT(inSuite, cache.Get(1, 1, &entry) == CacheResult::kDefinitelyUsed);
NL_TEST_ASSERT(inSuite, entry == accessor1);
EXPECT_EQ(cache.Get(1, 1, &entry), CacheResult::kDefinitelyUsed);
EXPECT_EQ(entry, accessor1);

entry = nullptr;
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kCacheMiss);
NL_TEST_ASSERT(inSuite, entry == nullptr);
NL_TEST_ASSERT(inSuite, cache.Get(2, 1, &entry) == CacheResult::kCacheMiss);
NL_TEST_ASSERT(inSuite, entry == nullptr);
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kCacheMiss);
EXPECT_EQ(entry, nullptr);
EXPECT_EQ(cache.Get(2, 1, &entry), CacheResult::kCacheMiss);
EXPECT_EQ(entry, nullptr);

cache.MarkUsed(1, 2, accessor1);

entry = nullptr;
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kDefinitelyUsed);
NL_TEST_ASSERT(inSuite, entry == accessor1);
NL_TEST_ASSERT(inSuite, cache.Get(2, 1, &entry) == CacheResult::kCacheMiss);
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kDefinitelyUsed);
EXPECT_EQ(entry, accessor1);
EXPECT_EQ(cache.Get(2, 1, &entry), CacheResult::kCacheMiss);

cache.MarkUsed(1, 2, accessor2);

entry = nullptr;
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kDefinitelyUsed);
NL_TEST_ASSERT(inSuite, entry == accessor2);
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kDefinitelyUsed);
EXPECT_EQ(entry, accessor2);
// The following should not crash (e.g. output not used if nullptr).
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, nullptr) == CacheResult::kDefinitelyUsed);
EXPECT_EQ(cache.Get(1, 2, nullptr), CacheResult::kDefinitelyUsed);

// Setting used to nullptr == does not mark used.
cache.MarkUsed(1, 2, nullptr);
entry = nullptr;
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kCacheMiss);
NL_TEST_ASSERT(inSuite, entry == nullptr);
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kCacheMiss);
EXPECT_EQ(entry, nullptr);

cache.Invalidate();
NL_TEST_ASSERT(inSuite, cache.Get(1, 1, &entry) == CacheResult::kCacheMiss);
NL_TEST_ASSERT(inSuite, entry == nullptr);
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kCacheMiss);
NL_TEST_ASSERT(inSuite, cache.Get(2, 1, &entry) == CacheResult::kCacheMiss);
EXPECT_EQ(cache.Get(1, 1, &entry), CacheResult::kCacheMiss);
EXPECT_EQ(entry, nullptr);
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kCacheMiss);
EXPECT_EQ(cache.Get(2, 1, &entry), CacheResult::kCacheMiss);

// Marking unused works, keeps single entry, and is invalidated when invalidated fully.
NL_TEST_ASSERT(inSuite, cache.Get(2, 2, nullptr) != CacheResult::kDefinitelyUnused);
NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) != CacheResult::kDefinitelyUnused);
EXPECT_NE(cache.Get(2, 2, nullptr), CacheResult::kDefinitelyUnused);
EXPECT_NE(cache.Get(3, 3, nullptr), CacheResult::kDefinitelyUnused);
cache.MarkUnused(2, 2);
NL_TEST_ASSERT(inSuite, cache.Get(2, 2, nullptr) == CacheResult::kDefinitelyUnused);
NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) != CacheResult::kDefinitelyUnused);
EXPECT_EQ(cache.Get(2, 2, nullptr), CacheResult::kDefinitelyUnused);
EXPECT_NE(cache.Get(3, 3, nullptr), CacheResult::kDefinitelyUnused);

cache.MarkUnused(3, 3);
NL_TEST_ASSERT(inSuite, cache.Get(2, 2, nullptr) != CacheResult::kDefinitelyUnused);
NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) == CacheResult::kDefinitelyUnused);
EXPECT_NE(cache.Get(2, 2, nullptr), CacheResult::kDefinitelyUnused);
EXPECT_EQ(cache.Get(3, 3, nullptr), CacheResult::kDefinitelyUnused);

cache.Invalidate();
NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) != CacheResult::kDefinitelyUnused);
EXPECT_NE(cache.Get(3, 3, nullptr), CacheResult::kDefinitelyUnused);
}

// clang-format off
const nlTest sTests[] =
{
NL_TEST_DEF("Basic AttributeAccessInterfaceCache lifecycle works", TestBasicLifecycle),
NL_TEST_SENTINEL()
};
// clang-format on

} // namespace

int TestAttributeAccessInterfaceCache()
{
// clang-format off
nlTestSuite theSuite =
{
"Test for AttributeAccessInterface cache utility",
&sTests[0],
nullptr,
nullptr
};
// clang-format on

nlTestRunner(&theSuite, nullptr);

return (nlTestRunnerStats(&theSuite));
}

CHIP_REGISTER_TEST_SUITE(TestAttributeAccessInterfaceCache)
Loading

0 comments on commit 5dd912f

Please sign in to comment.