forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestDefaultServerCluster.cpp
153 lines (122 loc) · 5.14 KB
/
TestDefaultServerCluster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2025 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 <pw_unit_test/framework.h>
#include <access/Privilege.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app/ConcreteClusterPath.h>
#include <app/data-model-provider/MetadataList.h>
#include <app/data-model-provider/MetadataTypes.h>
#include <app/data-model-provider/OperationTypes.h>
#include <app/data-model-provider/tests/ReadTesting.h>
#include <app/data-model-provider/tests/WriteTesting.h>
#include <app/server-cluster/DefaultServerCluster.h>
#include <lib/core/CHIPError.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/StringBuilderAdapters.h>
#include <lib/core/TLVReader.h>
#include <cstdlib>
#include <optional>
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::DataModel;
using namespace chip::app::Testing;
using namespace chip::Protocols::InteractionModel;
namespace {
class FakeDefaultServerCluster : public DefaultServerCluster
{
public:
FakeDefaultServerCluster(ConcreteClusterPath path) : mPath(path) {}
[[nodiscard]] ConcreteClusterPath GetPath() const override { return mPath; }
DataModel::ActionReturnStatus ReadAttribute(const DataModel::ReadAttributeRequest & request,
AttributeValueEncoder & encoder) override
{
switch (request.path.mAttributeId)
{
case Globals::Attributes::FeatureMap::Id:
return encoder.Encode<uint32_t>(0);
case Globals::Attributes::ClusterRevision::Id:
return encoder.Encode<uint32_t>(123);
}
return CHIP_ERROR_INVALID_ARGUMENT;
}
void TestIncreaseDataVersion() { IncreaseDataVersion(); }
private:
ConcreteClusterPath mPath;
};
} // namespace
TEST(TestDefaultServerCluster, TestDataVersion)
{
FakeDefaultServerCluster cluster({ 1, 2 });
DataVersion v1 = cluster.GetDataVersion();
cluster.TestIncreaseDataVersion();
ASSERT_EQ(cluster.GetDataVersion(), v1 + 1);
}
TEST(TestDefaultServerCluster, TestFlagsDefault)
{
FakeDefaultServerCluster cluster({ 1, 2 });
ASSERT_EQ(cluster.GetClusterFlags().Raw(), 0u);
}
TEST(TestDefaultServerCluster, AttributesDefault)
{
FakeDefaultServerCluster cluster({ 1, 2 });
DataModel::ListBuilder<AttributeEntry> attributes;
ASSERT_EQ(cluster.Attributes({ 1, 1 }, attributes), CHIP_NO_ERROR);
ReadOnlyBuffer<AttributeEntry> data = attributes.TakeBuffer();
// 5 global attributes are currently supported. Ensure they are returned.
ASSERT_EQ(data.size(), 5u);
ASSERT_EQ(data[0].attributeId, Globals::Attributes::ClusterRevision::Id);
ASSERT_EQ(data[1].attributeId, Globals::Attributes::FeatureMap::Id);
ASSERT_EQ(data[2].attributeId, Globals::Attributes::AttributeList::Id);
ASSERT_EQ(data[3].attributeId, Globals::Attributes::AcceptedCommandList::Id);
ASSERT_EQ(data[4].attributeId, Globals::Attributes::GeneratedCommandList::Id);
// first 2 are normal, the rest are list
for (size_t i = 0; i < 5; i++)
{
ASSERT_EQ(data[i].flags.Has(AttributeQualityFlags::kListAttribute), (i >= 2));
ASSERT_EQ(data[i].readPrivilege, Access::Privilege::kView);
ASSERT_FALSE(data[i].writePrivilege.has_value());
}
}
TEST(TestDefaultServerCluster, CommandsDefault)
{
FakeDefaultServerCluster cluster({ 1, 2 });
DataModel::ListBuilder<AcceptedCommandEntry> acceptedCommands;
ASSERT_EQ(cluster.AcceptedCommands({ 1, 1 }, acceptedCommands), CHIP_NO_ERROR);
ASSERT_TRUE(acceptedCommands.TakeBuffer().empty());
DataModel::ListBuilder<CommandId> generatedCommands;
ASSERT_EQ(cluster.GeneratedCommands({ 1, 1 }, generatedCommands), CHIP_NO_ERROR);
ASSERT_TRUE(generatedCommands.TakeBuffer().empty());
}
TEST(TestDefaultServerCluster, WriteAttributeDefault)
{
FakeDefaultServerCluster cluster({ 1, 2 });
WriteOperation test(0 /* endpoint */, 1 /* cluster */, 1234 /* attribute */);
test.SetSubjectDescriptor(kAdminSubjectDescriptor);
AttributeValueDecoder decoder = test.DecoderFor<uint32_t>(12345);
ASSERT_EQ(cluster.WriteAttribute(test.GetRequest(), decoder), Status::UnsupportedWrite);
ASSERT_FALSE(decoder.TriedDecode());
}
TEST(TestDefaultServerCluster, InvokeDefault)
{
FakeDefaultServerCluster cluster({ 1, 2 });
TLV::TLVReader tlvReader;
InvokeRequest request;
request.path = { 0 /* endpoint */, 1 /* cluster */, 1234 /* command */ };
ASSERT_EQ(cluster.InvokeCommand(request, tlvReader, nullptr /* command handler, assumed unused here */),
Status::UnsupportedCommand);
}