From 573511d733210bb253d9c2014d79a02b7a5671a4 Mon Sep 17 00:00:00 2001 From: Matthew Swartwout Date: Fri, 16 Feb 2024 11:17:32 -0800 Subject: [PATCH] Delete Concrete(Read|Data)AttributePath equality operator (#32131) * Delete Concrete(Read|Data)AttributePath equality operator Right now these default to using the ConcreteAttributePath equality operator, which is misleading because it will return true for objects which are not actually equal. Replace this with the MatchesConcreteAttributePath function. A new equality operator for these classes will be introduced in a follow-up PR. I'm introducing this change separately to ensure that CI catches any current usage of the equality operator. * Restyled by clang-format * Add unit tests * Restyled by clang-format --------- Co-authored-by: Restyled.io --- src/app/ConcreteAttributePath.h | 10 +++ src/app/ReadClient.cpp | 5 +- src/app/tests/BUILD.gn | 1 + src/app/tests/TestConcreteAttributePath.cpp | 94 +++++++++++++++++++++ 4 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 src/app/tests/TestConcreteAttributePath.cpp diff --git a/src/app/ConcreteAttributePath.h b/src/app/ConcreteAttributePath.h index 076575dcafa2a1..253b590ef90739 100644 --- a/src/app/ConcreteAttributePath.h +++ b/src/app/ConcreteAttributePath.h @@ -87,6 +87,10 @@ struct ConcreteReadAttributePath : public ConcreteAttributePath mListIndex.SetValue(aListIndex); } + bool operator==(const ConcreteReadAttributePath & aOther) const = delete; + bool operator!=(const ConcreteReadAttributePath & aOther) const = delete; + bool operator<(const ConcreteReadAttributePath & aOther) const = delete; + Optional mListIndex; }; @@ -138,6 +142,12 @@ struct ConcreteDataAttributePath : public ConcreteAttributePath ChipLogValueMEI(mClusterId), ChipLogValueMEI(mAttributeId)); } + bool MatchesConcreteAttributePath(const ConcreteAttributePath & aOther) { return ConcreteAttributePath::operator==(aOther); } + + bool operator==(const ConcreteDataAttributePath & aOther) const = delete; + bool operator!=(const ConcreteDataAttributePath & aOther) const = delete; + bool operator<(const ConcreteDataAttributePath & aOther) const = delete; + // // This index is only valid if `mListOp` is set to a list item operation, i.e // ReplaceItem, DeleteItem or AppendItem. Otherwise, it is to be ignored. diff --git a/src/app/ReadClient.cpp b/src/app/ReadClient.cpp index 4a123fe01b6291..57ef57362a25fa 100644 --- a/src/app/ReadClient.cpp +++ b/src/app/ReadClient.cpp @@ -799,9 +799,8 @@ CHIP_ERROR ReadClient::ProcessAttributeReportIBs(TLV::TLVReader & aAttributeRepo attributePath.mListOp = ConcreteDataAttributePath::ListOperation::ReplaceAll; } - if (attributePath == - ConcreteDataAttributePath(kRootEndpointId, Clusters::IcdManagement::Id, - Clusters::IcdManagement::Attributes::OperatingMode::Id)) + if (attributePath.MatchesConcreteAttributePath(ConcreteAttributePath( + kRootEndpointId, Clusters::IcdManagement::Id, Clusters::IcdManagement::Attributes::OperatingMode::Id))) { PeerType peerType; TLV::TLVReader operatingModeTlvReader; diff --git a/src/app/tests/BUILD.gn b/src/app/tests/BUILD.gn index be78dd8ade9e54..e4ab60329f218e 100644 --- a/src/app/tests/BUILD.gn +++ b/src/app/tests/BUILD.gn @@ -135,6 +135,7 @@ chip_test_suite_using_nltest("tests") { "TestClusterInfo.cpp", "TestCommandInteraction.cpp", "TestCommandPathParams.cpp", + "TestConcreteAttributePath.cpp", "TestDataModelSerialization.cpp", "TestDefaultOTARequestorStorage.cpp", "TestEventLoggingNoUTCTime.cpp", diff --git a/src/app/tests/TestConcreteAttributePath.cpp b/src/app/tests/TestConcreteAttributePath.cpp new file mode 100644 index 00000000000000..6e3451be26a78f --- /dev/null +++ b/src/app/tests/TestConcreteAttributePath.cpp @@ -0,0 +1,94 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +using namespace chip; +using namespace chip::app; + +namespace { + +void TestConcreteAttributePathEqualityDefaultConstructor(nlTestSuite * aSuite, void * aContext) +{ + ConcreteAttributePath path_one; + ConcreteAttributePath path_two; + NL_TEST_ASSERT(aSuite, path_one == path_two); +} + +void TestConcreteAttributePathEquality(nlTestSuite * aSuite, void * aContext) +{ + ConcreteAttributePath path_one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3); + ConcreteAttributePath path_two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3); + NL_TEST_ASSERT(aSuite, path_one == path_two); +} + +void TestConcreteAttributePathInequalityDifferentAttributeId(nlTestSuite * aSuite, void * aContext) +{ + ConcreteAttributePath path_one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3); + ConcreteAttributePath path_two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/4); + NL_TEST_ASSERT(aSuite, path_one != path_two); +} + +void TestConcreteDataAttributePathMatchesConcreteAttributePathEquality(nlTestSuite * aSuite, void * aContext) +{ + ConcreteAttributePath path(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3); + ConcreteDataAttributePath data_path(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3); + ConcreteDataAttributePath data_path_with_version(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3, + /*aDataVersion=*/MakeOptional(4U)); + ConcreteDataAttributePath data_path_with_list(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3, + /*aListOp=*/ConcreteDataAttributePath::ListOperation::ReplaceAll, + /*aListIndex=*/5U); + + NL_TEST_ASSERT(aSuite, data_path.MatchesConcreteAttributePath(path)); + NL_TEST_ASSERT(aSuite, data_path_with_version.MatchesConcreteAttributePath(path)); + NL_TEST_ASSERT(aSuite, data_path_with_list.MatchesConcreteAttributePath(path)); +} + +void TestConcreteDataAttributePathMatchesConcreteAttributePathInequality(nlTestSuite * aSuite, void * aContext) +{ + ConcreteAttributePath path(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3); + ConcreteDataAttributePath data_path(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/4); + + NL_TEST_ASSERT(aSuite, !data_path.MatchesConcreteAttributePath(path)); +} + +const nlTest sTests[] = { + NL_TEST_DEF("TestConcreteAttributePathEqualityDefaultConstructor", TestConcreteAttributePathEqualityDefaultConstructor), + NL_TEST_DEF("TestConcreteAttributePathEquality", TestConcreteAttributePathEquality), + NL_TEST_DEF("TestConcreteAttributePathInequalityDifferentAttributeId", TestConcreteAttributePathInequalityDifferentAttributeId), + NL_TEST_DEF("TestConcreteDataAttributePathMatchesConcreteAttributePathEquality", + TestConcreteDataAttributePathMatchesConcreteAttributePathEquality), + NL_TEST_DEF("TestConcreteDataAttributePathMatchesConcreteAttributePathInequality", + TestConcreteDataAttributePathMatchesConcreteAttributePathInequality), + NL_TEST_SENTINEL() +}; + +} // anonymous namespace + +int TestConcreteAttributePath() +{ + nlTestSuite theSuite = { "ConcreteAttributePath", &sTests[0], nullptr, nullptr }; + + nlTestRunner(&theSuite, nullptr); + + return (nlTestRunnerStats(&theSuite)); +} + +CHIP_REGISTER_TEST_SUITE(TestConcreteAttributePath)