forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestServerClusterInterfaceRegistry.cpp
319 lines (268 loc) · 11.9 KB
/
TestServerClusterInterfaceRegistry.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* 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 <app-common/zap-generated/ids/Attributes.h>
#include <app/ConcreteClusterPath.h>
#include <app/server-cluster/DefaultServerCluster.h>
#include <data-model-providers/codegen/ServerClusterInterfaceRegistry.h>
#include <lib/core/CHIPError.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/StringBuilderAdapters.h>
#include <algorithm>
#include <cstdlib>
#include <random>
using namespace chip;
using namespace chip::app;
using namespace chip::app::DataModel;
using namespace chip::app::Clusters;
namespace {
constexpr chip::EndpointId kEp1 = 1;
constexpr chip::EndpointId kEp2 = 2;
constexpr chip::EndpointId kEp3 = 3;
constexpr chip::ClusterId kCluster1 = 1;
constexpr chip::ClusterId kCluster2 = 2;
constexpr chip::ClusterId kCluster3 = 3;
class FakeServerClusterInterface : public DefaultServerCluster
{
public:
FakeServerClusterInterface(EndpointId endpoint, ClusterId cluster) : mPath({ endpoint, cluster }) {}
FakeServerClusterInterface(const 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;
}
private:
ConcreteClusterPath mPath;
};
struct TestServerClusterInterfaceRegistry : public ::testing::Test
{
static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); }
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
};
} // namespace
TEST_F(TestServerClusterInterfaceRegistry, BasicTest)
{
ServerClusterInterfaceRegistry registry;
FakeServerClusterInterface cluster1(kEp1, kCluster1);
FakeServerClusterInterface cluster2(kEp2, kCluster2);
FakeServerClusterInterface cluster3(kEp2, kCluster3);
// there should be nothing registered to start with.
EXPECT_EQ(registry.Get({ kEp1, kCluster1 }), nullptr);
EXPECT_EQ(registry.Get({ kEp1, kCluster2 }), nullptr);
EXPECT_EQ(registry.Get({ kEp2, kCluster2 }), nullptr);
EXPECT_EQ(registry.Get({ kEp2, kCluster3 }), nullptr);
EXPECT_EQ(registry.Get({ kInvalidEndpointId, kCluster2 }), nullptr);
EXPECT_EQ(registry.Get({ kEp1, kInvalidClusterId }), nullptr);
// registration of invalid values is not acceptable
{
// registration has NULL interface
// next is not null (meaning registration2 looks like already registered)
ServerClusterRegistration registration1(cluster1);
ServerClusterRegistration registration2(cluster2);
registration2.next = ®istration1;
EXPECT_EQ(registry.Register(registration2), CHIP_ERROR_INVALID_ARGUMENT);
// invalid path in cluster
FakeServerClusterInterface invalidPathInterface(kInvalidEndpointId, kCluster1);
ServerClusterRegistration registration3(invalidPathInterface);
EXPECT_EQ(registry.Register(registration3), CHIP_ERROR_INVALID_ARGUMENT);
// invalid path in cluster
FakeServerClusterInterface invalidPathInterface2(kEp1, kInvalidClusterId);
ServerClusterRegistration registration4(invalidPathInterface);
EXPECT_EQ(registry.Register(registration4), CHIP_ERROR_INVALID_ARGUMENT);
}
ServerClusterRegistration registration1(cluster1);
ServerClusterRegistration registration2(cluster2);
ServerClusterRegistration registration3(cluster3);
// should be able to register
EXPECT_EQ(registry.Register(registration1), CHIP_NO_ERROR);
EXPECT_EQ(registry.Register(registration2), CHIP_NO_ERROR);
EXPECT_EQ(registry.Register(registration3), CHIP_NO_ERROR);
// cannot register two implementations on the same path
{
FakeServerClusterInterface another1(kEp1, kCluster1);
ServerClusterRegistration anotherRegisration1(another1);
EXPECT_EQ(registry.Register(anotherRegisration1), CHIP_ERROR_DUPLICATE_KEY_ID);
}
// Items can be found back
EXPECT_EQ(registry.Get({ kEp1, kCluster1 }), &cluster1);
EXPECT_EQ(registry.Get({ kEp2, kCluster2 }), &cluster2);
EXPECT_EQ(registry.Get({ kEp2, kCluster3 }), &cluster3);
EXPECT_EQ(registry.Get({ kEp2, kCluster1 }), nullptr);
EXPECT_EQ(registry.Get({ kEp1, kCluster2 }), nullptr);
EXPECT_EQ(registry.Get({ kEp3, kCluster2 }), nullptr);
// repeated calls work
EXPECT_EQ(registry.Get({ kEp1, kCluster2 }), nullptr);
EXPECT_EQ(registry.Get({ kEp1, kCluster2 }), nullptr);
EXPECT_EQ(registry.Get({ kEp1, kCluster2 }), nullptr);
EXPECT_EQ(registry.Get({ kEp2, kCluster1 }), nullptr);
EXPECT_EQ(registry.Get({ kEp2, kCluster1 }), nullptr);
EXPECT_EQ(registry.Get({ kEp2, kCluster1 }), nullptr);
// remove registrations
EXPECT_EQ(registry.Unregister({ kEp2, kCluster2 }), &cluster2);
EXPECT_EQ(registry.Unregister({ kEp2, kCluster2 }), nullptr);
// Re-adding works
EXPECT_EQ(registry.Get({ kEp2, kCluster2 }), nullptr);
EXPECT_EQ(registry.Register(registration2), CHIP_NO_ERROR);
EXPECT_EQ(registry.Get({ kEp2, kCluster2 }), &cluster2);
// clean of an entire endpoint works
EXPECT_EQ(registry.Get({ kEp2, kCluster3 }), &cluster3);
registry.UnregisterAllFromEndpoint(kEp2);
EXPECT_EQ(registry.Get({ kEp1, kCluster1 }), &cluster1);
EXPECT_EQ(registry.Get({ kEp2, kCluster3 }), nullptr);
registry.UnregisterAllFromEndpoint(kEp1);
EXPECT_EQ(registry.Get({ kEp1, kCluster1 }), nullptr);
EXPECT_EQ(registry.Get({ kEp2, kCluster3 }), nullptr);
}
TEST_F(TestServerClusterInterfaceRegistry, StressTest)
{
// make the test repeatable
srand(1234);
std::vector<FakeServerClusterInterface> items;
std::vector<ServerClusterRegistration> registrations;
static constexpr ClusterId kClusterTestCount = 200;
static constexpr EndpointId kEndpointTestCount = 10;
static constexpr size_t kTestIterations = 4;
static_assert(kInvalidClusterId > kClusterTestCount, "Tests assume all clusters IDs [0...] are valid");
static_assert(kTestIterations > 1, "Tests use different unregister methods. Need 2 or more passes.");
items.reserve(kClusterTestCount);
for (ClusterId i = 0; i < kClusterTestCount; i++)
{
auto endpointId = static_cast<EndpointId>(rand() % kEndpointTestCount);
items.emplace_back(endpointId, i);
}
for (ClusterId i = 0; i < kClusterTestCount; i++)
{
registrations.emplace_back(items[i]);
}
ServerClusterInterfaceRegistry registry;
for (size_t test = 0; test < kTestIterations; test++)
{
for (ClusterId i = 0; i < kClusterTestCount; i++)
{
ASSERT_EQ(registry.Register(registrations[i]), CHIP_NO_ERROR);
}
// test that getters work
for (ClusterId cluster = 0; cluster < kClusterTestCount; cluster++)
{
for (EndpointId ep = 0; ep < kEndpointTestCount; ep++)
{
if (items[cluster].GetPath().mEndpointId == ep)
{
ASSERT_EQ(registry.Get({ ep, cluster }), &items[cluster]);
}
else
{
ASSERT_EQ(registry.Get({ ep, cluster }), nullptr);
}
}
}
// clear endpoints. Stress test, unregister in different ways (bulk vs individual)
if (test % 2 == 1)
{
// shuffle unregister
std::vector<size_t> unregister_order;
unregister_order.reserve(kClusterTestCount);
for (size_t i = 0; i < kClusterTestCount; i++)
{
unregister_order.push_back(i);
}
std::default_random_engine eng(static_cast<std::default_random_engine::result_type>(rand()));
std::shuffle(unregister_order.begin(), unregister_order.end(), eng);
// unregister
for (auto cluster : unregister_order)
{
// item MUST exist and be accessible
ASSERT_EQ(registry.Get(items[cluster].GetPath()), &items[cluster]);
ASSERT_EQ(registry.Unregister(items[cluster].GetPath()), &items[cluster]);
// once unregistered, it is not there anymore
ASSERT_EQ(registry.Get(items[cluster].GetPath()), nullptr);
ASSERT_EQ(registry.Unregister(items[cluster].GetPath()), nullptr);
}
}
else
{
// bulk unregister
for (EndpointId ep = 0; ep < kEndpointTestCount; ep++)
{
registry.UnregisterAllFromEndpoint(ep);
}
}
// all endpoints should be clear
for (ClusterId cluster = 0; cluster < kClusterTestCount; cluster++)
{
for (EndpointId ep = 0; ep < kEndpointTestCount; ep++)
{
ASSERT_EQ(registry.Get({ ep, cluster }), nullptr);
}
}
}
}
TEST_F(TestServerClusterInterfaceRegistry, ClustersOnEndpoint)
{
std::vector<FakeServerClusterInterface> items;
std::vector<ServerClusterRegistration> registrations;
static constexpr ClusterId kClusterTestCount = 200;
static constexpr EndpointId kEndpointTestCount = 10;
static_assert(kInvalidClusterId > kClusterTestCount, "Tests assume all clusters IDs [0...] are valid");
items.reserve(kClusterTestCount);
for (ClusterId i = 0; i < kClusterTestCount; i++)
{
items.emplace_back(static_cast<EndpointId>(i % kEndpointTestCount), i);
}
for (ClusterId i = 0; i < kClusterTestCount; i++)
{
registrations.emplace_back(items[i]);
}
ServerClusterInterfaceRegistry registry;
// place the clusters on the respecitve endpoints
for (ClusterId i = 0; i < kClusterTestCount; i++)
{
ASSERT_EQ(registry.Register(registrations[i]), CHIP_NO_ERROR);
}
// this IS implementation defined: we always register at "HEAD" so the listing is in
// INVERSE order of registering.
for (EndpointId ep = 0; ep < kEndpointTestCount; ep++)
{
// Move to the end since we iterate in reverse order
ClusterId expectedClusterId = ep + kEndpointTestCount * (kClusterTestCount / kEndpointTestCount);
if (expectedClusterId >= kClusterTestCount)
{
expectedClusterId -= kEndpointTestCount;
}
// ensure that iteration happens exactly as we expect: reverse order and complete
for (auto cluster : registry.ClustersOnEndpoint(ep))
{
ASSERT_LT(expectedClusterId, kClusterTestCount);
ASSERT_EQ(cluster->GetPath(), ConcreteClusterPath(ep, expectedClusterId));
expectedClusterId -= kEndpointTestCount; // next expected/registered cluster
}
// Iterated through all : we overflowed and got a large number
ASSERT_GE(expectedClusterId, kClusterTestCount);
}
// invalid index works and iteration on empty lists is ok
auto clusters = registry.ClustersOnEndpoint(kEndpointTestCount + 1);
ASSERT_EQ(clusters.begin(), clusters.end());
}