Skip to content

Commit

Permalink
Enforce constraints for sizes of labelstruct label and value lengths (#…
Browse files Browse the repository at this point in the history
…20501)

* Enforce constraints for sizes of labelstruct label and value lengths (#20431)

* Enforce constraints for sizes of labelstruct label and value lengths

* Add a test user label cluster constraint integration test

* regen for tests

* Restyle

* Ran zap regen

Co-authored-by: Andrei Litvin <andy314@gmail.com>
  • Loading branch information
woody-apple and andy31415 committed Jul 11, 2022
1 parent 3d85e72 commit 7037068
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/app/clusters/user-label-server/user-label-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ class UserLabelAttrAccess : public AttributeAccessInterface
CHIP_ERROR WriteLabelList(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder);
};

/// Matches constraints on a LabelStruct.
bool IsValidLabelEntry(const Structs::LabelStruct::Type & entry)
{
constexpr size_t kMaxLabelSize = 16;
constexpr size_t kMaxValueSize = 16;

// NOTE: spec default for label and value is empty, so empty is accepted here
return (entry.label.size() <= kMaxLabelSize) && (entry.value.size() <= kMaxValueSize);
}

bool IsValidLabelEntryList(const LabelList::TypeInfo::DecodableType & list)
{
auto iter = list.begin();
while (iter.Next())
{
if (!IsValidLabelEntry(iter.GetValue()))
{
return false;
}
}
return true;
}

UserLabelAttrAccess gAttrAccess;

CHIP_ERROR UserLabelAttrAccess::ReadLabelList(EndpointId endpoint, AttributeValueEncoder & aEncoder)
Expand Down Expand Up @@ -106,6 +129,7 @@ CHIP_ERROR UserLabelAttrAccess::WriteLabelList(const ConcreteDataAttributePath &
LabelList::TypeInfo::DecodableType decodablelist;

ReturnErrorOnFailure(aDecoder.Decode(decodablelist));
ReturnErrorCodeIf(!IsValidLabelEntryList(decodablelist), CHIP_ERROR_INVALID_ARGUMENT);

auto iter = decodablelist.begin();
while (iter.Next())
Expand All @@ -120,7 +144,10 @@ CHIP_ERROR UserLabelAttrAccess::WriteLabelList(const ConcreteDataAttributePath &
if (aPath.mListOp == ConcreteDataAttributePath::ListOperation::AppendItem)
{
Structs::LabelStruct::DecodableType entry;

ReturnErrorOnFailure(aDecoder.Decode(entry));
ReturnErrorCodeIf(!IsValidLabelEntry(entry), CHIP_ERROR_INVALID_ARGUMENT);

return provider->AppendUserLabel(endpoint, entry);
}

Expand Down
57 changes: 57 additions & 0 deletions src/app/tests/suites/TestUserLabelClusterConstraints.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) 2022 Project CHIP Authors
#
# 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.

name: User Label Cluster Tests

config:
nodeId: 0x12344321
cluster: "User Label"
endpoint: 0

tests:
- label: "Wait for the commissioned device to be retrieved"
cluster: "DelayCommands"
command: "WaitForCommissionee"
arguments:
values:
- name: "nodeId"
value: nodeId

- label: "Attempt to write overly long item for label"
command: "writeAttribute"
attribute: "label list"
arguments:
value:
[
{
label: "this is longer than sixteen characters",
value: "bedroom 2",
},
]
response:
error: FAILURE

- label: "Attempt to write overly long item for value"
command: "writeAttribute"
attribute: "label list"
arguments:
value:
[
{
label: "test",
value: "this is longer than sixteen characters",
},
]
response:
error: FAILURE
1 change: 1 addition & 0 deletions src/app/tests/suites/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ function getTests() {
"TestSystemCommands",
"TestBinding",
"TestUserLabelCluster",
"TestUserLabelClusterConstraints",
"TestArmFailSafe",
"TestFanControl",
];
Expand Down
114 changes: 114 additions & 0 deletions zzz_generated/chip-tool/zap-generated/test/Commands.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7037068

Please sign in to comment.