|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright (c) 2025 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +#include <pw_unit_test/framework.h> |
| 19 | + |
| 20 | +#include <app-common/zap-generated/ids/Attributes.h> |
| 21 | +#include <app/server-cluster/AttributeListBuilder.h> |
| 22 | +#include <app/server-cluster/DefaultServerCluster.h> |
| 23 | + |
| 24 | +#include <cstdlib> |
| 25 | +#include <optional> |
| 26 | + |
| 27 | +using namespace chip; |
| 28 | +using namespace chip::app; |
| 29 | +using namespace chip::app::DataModel; |
| 30 | +using namespace chip::app::Clusters; |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +// Initialize memory as ReadOnlyBufferBuilder may allocate |
| 35 | +struct TestAttributeListBuilder : public ::testing::Test |
| 36 | +{ |
| 37 | + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } |
| 38 | + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } |
| 39 | +}; |
| 40 | + |
| 41 | +} // namespace |
| 42 | + |
| 43 | +TEST_F(TestAttributeListBuilder, Append) |
| 44 | +{ |
| 45 | + const size_t global_attribute_count = DefaultServerCluster::GlobalAttributes().size(); |
| 46 | + |
| 47 | + constexpr BitFlags<DataModel::AttributeQualityFlags> kNoFlags; |
| 48 | + |
| 49 | + // Only mandatory attributes |
| 50 | + { |
| 51 | + const AttributeEntry mandatory[] = { |
| 52 | + { 1, kNoFlags, Access::Privilege::kView, std::nullopt }, |
| 53 | + { 2, kNoFlags, Access::Privilege::kView, std::nullopt }, |
| 54 | + }; |
| 55 | + |
| 56 | + ReadOnlyBufferBuilder<AttributeEntry> builder; |
| 57 | + ASSERT_EQ(AttributeListBuilder(builder).Append(Span(mandatory), {}), CHIP_NO_ERROR); |
| 58 | + |
| 59 | + ReadOnlyBuffer<AttributeEntry> result = builder.TakeBuffer(); |
| 60 | + ASSERT_EQ(result.size(), 2 + global_attribute_count); |
| 61 | + ASSERT_EQ(result[0].attributeId, 1u); |
| 62 | + ASSERT_EQ(result[1].attributeId, 2u); |
| 63 | + ASSERT_EQ(result[2].attributeId, Globals::Attributes::ClusterRevision::Id); |
| 64 | + } |
| 65 | + |
| 66 | + // Only optional attributes |
| 67 | + { |
| 68 | + const AttributeEntry optional1_meta(10, kNoFlags, Access::Privilege::kView, std::nullopt); |
| 69 | + const AttributeEntry optional2_meta(11, kNoFlags, Access::Privilege::kView, std::nullopt); |
| 70 | + const AttributeEntry optional3_meta(12, kNoFlags, Access::Privilege::kView, std::nullopt); |
| 71 | + |
| 72 | + ReadOnlyBufferBuilder<AttributeEntry> builder; |
| 73 | + const AttributeListBuilder::OptionalAttributeEntry optionalEntries[] = { |
| 74 | + { .enabled = true, .metadata = optional1_meta }, |
| 75 | + { .enabled = false, .metadata = optional2_meta }, |
| 76 | + { .enabled = true, .metadata = optional3_meta }, |
| 77 | + }; |
| 78 | + ASSERT_EQ(AttributeListBuilder(builder).Append({}, Span(optionalEntries)), CHIP_NO_ERROR); |
| 79 | + |
| 80 | + ReadOnlyBuffer<AttributeEntry> result = builder.TakeBuffer(); |
| 81 | + ASSERT_EQ(result.size(), 2 + global_attribute_count); |
| 82 | + ASSERT_EQ(result[0].attributeId, 10u); |
| 83 | + ASSERT_EQ(result[1].attributeId, 12u); |
| 84 | + ASSERT_EQ(result[2].attributeId, Globals::Attributes::ClusterRevision::Id); |
| 85 | + } |
| 86 | + |
| 87 | + // Mix of mandatory and optional attributes |
| 88 | + { |
| 89 | + const AttributeEntry mandatory[] = { |
| 90 | + { 1, kNoFlags, Access::Privilege::kView, std::nullopt }, |
| 91 | + }; |
| 92 | + const AttributeEntry optional1_meta(10, kNoFlags, Access::Privilege::kView, std::nullopt); |
| 93 | + const AttributeEntry optional2_meta(11, kNoFlags, Access::Privilege::kView, std::nullopt); |
| 94 | + |
| 95 | + ReadOnlyBufferBuilder<AttributeEntry> builder; |
| 96 | + const AttributeListBuilder::OptionalAttributeEntry optionalEntries[] = { |
| 97 | + { .enabled = true, .metadata = optional1_meta }, |
| 98 | + { .enabled = false, .metadata = optional2_meta }, |
| 99 | + }; |
| 100 | + ASSERT_EQ(AttributeListBuilder(builder).Append(Span(mandatory), Span(optionalEntries)), CHIP_NO_ERROR); |
| 101 | + |
| 102 | + ReadOnlyBuffer<AttributeEntry> result = builder.TakeBuffer(); |
| 103 | + ASSERT_EQ(result.size(), 2 + global_attribute_count); |
| 104 | + ASSERT_EQ(result[0].attributeId, 1u); |
| 105 | + ASSERT_EQ(result[1].attributeId, 10u); |
| 106 | + ASSERT_EQ(result[2].attributeId, Globals::Attributes::ClusterRevision::Id); |
| 107 | + } |
| 108 | + |
| 109 | + // No attributes |
| 110 | + { |
| 111 | + ReadOnlyBufferBuilder<AttributeEntry> builder; |
| 112 | + ASSERT_EQ(AttributeListBuilder(builder).Append({}, {}), CHIP_NO_ERROR); |
| 113 | + |
| 114 | + ReadOnlyBuffer<AttributeEntry> result = builder.TakeBuffer(); |
| 115 | + ASSERT_EQ(result.size(), global_attribute_count); |
| 116 | + ASSERT_EQ(result[0].attributeId, Globals::Attributes::ClusterRevision::Id); |
| 117 | + } |
| 118 | +} |
0 commit comments