-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
test_cluster.zapt
415 lines (372 loc) · 18.8 KB
/
test_cluster.zapt
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
{{#chip_tests tests}}
class {{filename}}: public TestCommand
{
public:
{{filename}}(CredentialIssuerCommands * credsIssuerConfig): TestCommand("{{filename}}", credsIssuerConfig), mTestIndex(0)
{
{{#chip_tests_config}}
{{#if (isString type)}}
AddArgument("{{name}}", &m{{asUpperCamelCase name}});
{{else}}
AddArgument("{{name}}", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &m{{asUpperCamelCase name}});
{{/if}}
{{/chip_tests_config}}
}
~{{filename}}()
{
{{#chip_tests_items}}
{{#chip_tests_item_response_parameters}}
{{#if saveAs}}
{{#if (isString type)}}
if ({{saveAs}}Buffer != nullptr)
{
chip::Platform::MemoryFree({{saveAs}}Buffer);
{{saveAs}}Buffer = nullptr;
}
{{/if}}
{{/if}}
{{/chip_tests_item_response_parameters}}
{{/chip_tests_items}}
}
/////////// TestCommand Interface /////////
void NextTest() override
{
CHIP_ERROR err = CHIP_NO_ERROR;
if (0 == mTestIndex)
{
ChipLogProgress(chipTool, " **** Test Start: {{filename}}\n");
}
if (mTestCount == mTestIndex)
{
ChipLogProgress(chipTool, " **** Test Complete: {{filename}}\n");
SetCommandExitStatus(CHIP_NO_ERROR);
return;
}
Wait();
// Ensure we increment mTestIndex before we start running the relevant
// command. That way if we lose the timeslice after we send the message
// but before our function call returns, we won't end up with an
// incorrect mTestIndex value observed when we get the response.
switch (mTestIndex++)
{
{{#chip_tests_items}}
case {{index}}:
ChipLogProgress(chipTool, " ***** Test Step {{index}} : {{label}}\n");
{{#if PICS}}
if (ShouldSkip("{{PICS}}"))
{
NextTest();
return;
}
{{/if}}
err = Test{{asUpperCamelCase label}}_{{index}}();
break;
{{/chip_tests_items}}
}
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, " ***** Test Failure: %s\n", chip::ErrorStr(err));
SetCommandExitStatus(err);
}
}
{{#if timeout}}
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(mTimeout.HasValue() ? mTimeout.Value() : {{timeout}}); }
{{/if}}
private:
std::atomic_uint16_t mTestIndex;
const uint16_t mTestCount = {{totalTests}};
{{#chip_tests_config}}
chip::Optional<{{chipType}}> m{{asUpperCamelCase name}};
{{/chip_tests_config}}
{{#chip_tests_items}}
{{#chip_tests_item_response_parameters}}
{{#if saveAs}}
{{~#if (isString type)}}{{#if (isOctetString type)}}uint8_t{{else}}char{{/if}} * {{saveAs}}Buffer = nullptr;{{/if~}}
{{chipType}} {{saveAs}};
{{/if}}
{{/chip_tests_item_response_parameters}}
{{/chip_tests_items}}
{{! Helper around zapTypeToDecodableClusterObjectType that lets us set the
array/nullable/etc context appropriately.}}
{{~#*inline "subscribeResponseDataArgument"~}}
{{zapTypeToDecodableClusterObjectType type ns=cluster isArgument=true}} value
{{~/inline~}}
{{~#*inline "subscribeResponseArguments"~}}
{{> subscribeResponseDataArgument type=attr.type isArray=attr.isArray isNullable=attr.isNullable}}
{{~/inline~}}
{{~#*inline "subscribeDataCallback"}}
mTest_{{parent.filename}}_{{attribute}}_Reported
{{/inline}}
{{#*inline "subscribeDataCallbackType"}}
Test_{{parent.filename}}_{{attribute}}_ReportCallback
{{/inline}}
{{#chip_tests_items}}
{{#if allocateSubscribeDataCallback}}
typedef void (*{{> subscribeDataCallbackType}})(void * context, {{> subscribeResponseArguments attr=attributeObject}});
{{> subscribeDataCallbackType}} {{> subscribeDataCallback}} = nullptr;
{{/if}}
{{/chip_tests_items}}
{{#*inline "failureResponse"}}OnFailureResponse_{{index}}{{/inline}}
{{#*inline "successResponse"}}OnSuccessResponse_{{index}}{{/inline}}
{{#*inline "subscriptionEstablished"}}OnSubscriptionEstablishedResponse_{{index}}{{/inline}}
{{#*inline "doneResponse"}}OnDoneResponse_{{index}}{{/inline}}
{{#*inline "staticFailureResponse"}}OnFailureCallback_{{index}}{{/inline}}
{{#*inline "staticSuccessResponse"}}OnSuccessCallback_{{index}}{{/inline}}
{{#*inline "staticSubscriptionEstablished"}}OnSubscriptionEstablished_{{index}}{{/inline}}
{{#*inline "staticDoneResponse"}}OnDoneCallback_{{index}}{{/inline}}
{{#*inline "successArguments"}}{{#chip_tests_item_response_parameters}}{{#first}}{{#if ../leadingComma}}, {{/if}}{{/first}} {{zapTypeToDecodableClusterObjectType type ns=parent.cluster isArgument=true}} {{asLowerCamelCase name}}{{#not_last}}, {{/not_last}}{{/chip_tests_item_response_parameters}}{{/inline}}
{{! TODO: Temporary if cascade until everything is converted to the new status setup }}
{{#*inline "failureArguments"}}{{#if leadingComma}}, {{/if}}CHIP_ERROR error{{/inline}}
{{#*inline "staticSuccessArguments"}}void * context{{> successArguments leadingComma=true}}{{/inline}}
{{#*inline "staticFailureArguments"}}void * context{{> failureArguments leadingComma=true}}{{/inline}}
{{#*inline "staticDoneArguments"}}void * context{{/inline}}
{{#*inline "doneArguments"}}{{/inline}}
{{#*inline "staticSubscriptionEstablishedArguments"}}void * context{{/inline}}
{{#*inline "subscriptionEstablishedArguments"}}{{/inline}}
{{#chip_tests_items}}
{{#unless (isTestOnlyCluster cluster)}}
{{#unless isWait}}
{{#unless isCommand}}
{{#if isWriteAttribute}}
{{#if isGroupCommand}}
static void {{>staticDoneResponse}}({{>staticDoneArguments}})
{
(static_cast<{{filename}} *>(context))->{{> doneResponse }}();
}
{{/if}}
{{/if}}
static void {{>staticFailureResponse}}({{>staticFailureArguments}})
{
(static_cast<{{filename}} *>(context))->{{>failureResponse}}(error);
}
static void {{>staticSuccessResponse}}({{> staticSuccessArguments}})
{
(static_cast<{{filename}} *>(context))->{{>successResponse}}({{#chip_tests_item_response_parameters}}{{#not_first}}, {{/not_first}}{{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}});
}
{{#if isSubscribe}}
static void {{> staticSubscriptionEstablished}}({{> staticSubscriptionEstablishedArguments}})
{
(static_cast<{{filename}} *>(context))->{{> subscriptionEstablished}}();
}
{{/if}}
{{#if isWaitForReport}}
bool mReceivedReport_{{index}} = false;
{{/if}}
{{/unless}}
{{/unless}}
{{/unless}}
{{/chip_tests_items}}
//
// Tests methods
//
{{#chip_tests_items}}
{{#*inline "testCommand"}}Test{{asUpperCamelCase label}}_{{index}}{{/inline}}
{{#if (isTestOnlyCluster cluster)}}
CHIP_ERROR {{>testCommand}}()
{
SetIdentity(kIdentity{{asUpperCamelCase identity}});
return {{command}}({{#chip_tests_item_parameters}}{{#not_first}}, {{/not_first}}{{#if (isString type)}}"{{/if}}{{definedValue}}{{#if (isString type)}}"{{/if}}{{/chip_tests_item_parameters}});
}
{{else if isWait}}
CHIP_ERROR {{>testCommand}}()
{
const chip::EndpointId endpoint = {{#if (chip_tests_config_has "endpoint")}}mEndpoint.HasValue() ? mEndpoint.Value() : {{/if}}{{endpoint}};
ChipLogError(chipTool, "[Endpoint: 0x%08x Cluster: {{cluster}} {{#if isAttribute}}Attribute: {{attribute}}{{else}}Command: {{wait}}{{/if}}] {{label}}", endpoint);
{{#*inline "waitForTypeName"}}{{#if isAttribute}}Attribute{{else}}Command{{/if}}{{/inline}}
{{#*inline "waitForTypeId"}}chip::app::Clusters::{{asUpperCamelCase cluster}}::{{#if isAttribute}}Attributes::{{attribute}}{{else}}Commands::{{wait}}{{/if}}::Id{{/inline}}
ClearAttributeAndCommandPaths();
m{{>waitForTypeName}}Path = chip::app::Concrete{{>waitForTypeName}}Path(endpoint, chip::app::Clusters::{{asUpperCamelCase cluster}}::Id, {{>waitForTypeId}});
return CHIP_NO_ERROR;
}
{{else}}
{{#*inline "device"}}mDevices[kIdentity{{asUpperCamelCase identity}}]{{/inline}}
CHIP_ERROR {{>testCommand}}()
{
{{#if isGroupCommand}}
const chip::GroupId groupId = {{groupId}};
{{else}}
const chip::EndpointId endpoint = {{#if (chip_tests_config_has "endpoint")}}mEndpoint.HasValue() ? mEndpoint.Value() : {{/if}}{{endpoint}};
{{/if}}
{{~#*inline "maybeTimedInteractionTimeout"}}
{{#if timedInteractionTimeoutMs}}
, {{timedInteractionTimeoutMs}}
{{else if commandObject.mustUseTimedInvoke}}
, chip::NullOptional
{{else if attributeObject.mustUseTimedWrite}}
, chip::NullOptional
{{/if}}
{{/inline~}}
{{~#*inline "maybeWait"}}
{{#if busyWaitMs}}
{
using namespace chip::System::Clock::Literals;
// Busy-wait for {{busyWaitMs}} milliseconds.
auto & clock = chip::System::SystemClock();
auto start = clock.GetMonotonicTimestamp();
while (clock.GetMonotonicTimestamp() - start < {{busyWaitMs}}_ms);
}
{{/if}}
{{/inline~}}
{{#if isCommand}}
using RequestType = chip::app::Clusters::{{asUpperCamelCase cluster}}::Commands::{{asUpperCamelCase command}}::Type;
RequestType request;
{{#chip_tests_item_parameters}}
{{>commandValue ns=parent.cluster container=(concat "request." (asLowerCamelCase label)) definedValue=definedValue depth=0}}
{{/chip_tests_item_parameters}}
auto success = [](void * context, const typename RequestType::ResponseType & data) {
(static_cast<{{filename}} *>(context))->{{>successResponse}}({{#chip_tests_item_response_parameters}}{{#not_first}}, {{/not_first}}data.{{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}});
};
auto failure = [](void * context, CHIP_ERROR error) {
(static_cast<{{filename}} *>(context))->{{>failureResponse}}(error);
};
{{#if isGroupCommand}}
auto done = [](void * context) {
(static_cast<{{filename}} *>(context))->OnDoneResponse_{{index}}();
};
{{/if}}
ReturnErrorOnFailure(chip::Controller::{{#if isGroupCommand}}InvokeGroupCommand{{else}}InvokeCommand{{/if}}({{>device}}, this, success, failure, {{#if isGroupCommand}}done,{{/if}}
{{#if isGroupCommand}}groupId{{else}}endpoint{{/if}},
request
{{> maybeTimedInteractionTimeout }}
));
{{> maybeWait }}
{{else}}
chip::Controller::{{asUpperCamelCase cluster}}ClusterTest cluster;
{{#if isGroupCommand}}
cluster.AssociateWithGroup({{>device}}, groupId);
{{else}}
cluster.Associate({{>device}}, endpoint);
{{/if}}
{{#chip_tests_item_parameters}}
{{zapTypeToEncodableClusterObjectType type ns=parent.cluster}} {{asLowerCamelCase name}}Argument;
{{>commandValue ns=parent.cluster container=(concat (asLowerCamelCase name) "Argument") definedValue=definedValue depth=0}}
{{/chip_tests_item_parameters}}
{{#if isWriteAttribute}}
ReturnErrorOnFailure(cluster.WriteAttribute<chip::app::Clusters::{{asUpperCamelCase cluster}}::Attributes::{{asUpperCamelCase attribute}}::TypeInfo>({{#chip_tests_item_parameters}}{{asLowerCamelCase name}}Argument, {{/chip_tests_item_parameters}}this, {{>staticSuccessResponse}}, {{>staticFailureResponse}}
{{~> maybeTimedInteractionTimeout ~}}
{{~#if isGroupCommand}}, {{>staticDoneResponse}}{{/if~}}
));
{{> maybeWait }}
{{else if isReadEvent}}
ReturnErrorOnFailure(cluster.ReadEvent<{{zapTypeToDecodableClusterObjectType event ns=cluster isArgument=true}}>(this, {{>staticSuccessResponse}}, {{>staticFailureResponse}}));
{{else if isSubscribeEvent}}
ReturnErrorOnFailure(cluster.SubscribeEvent<{{zapTypeToDecodableClusterObjectType event ns=cluster isArgument=true}}>(this, {{>staticSuccessResponse}}, {{>staticFailureResponse}}, minIntervalArgument, maxIntervalArgument, {{>staticSubscriptionEstablished}}));
{{else if isReadAttribute}}
ReturnErrorOnFailure(cluster.ReadAttribute<chip::app::Clusters::{{asUpperCamelCase cluster}}::Attributes::{{asUpperCamelCase attribute}}::TypeInfo>({{#chip_tests_item_parameters}}{{asLowerCamelCase name}}Argument, {{/chip_tests_item_parameters}}this, {{>staticSuccessResponse}}, {{>staticFailureResponse}}));
{{else if isSubscribeAttribute}}
ReturnErrorOnFailure(cluster.SubscribeAttribute<chip::app::Clusters::{{asUpperCamelCase cluster}}::Attributes::{{asUpperCamelCase attribute}}::TypeInfo>(this, {{>staticSuccessResponse}}, {{>staticFailureResponse}}, minIntervalArgument, maxIntervalArgument, {{>staticSubscriptionEstablished}}));
{{else if isWaitForReport}}
{{> subscribeDataCallback}} = {{> staticSuccessResponse}};
{{else}}
UNEXPECTED COMMAND: {{>commandName}}
{{/if}}
{{/if}}
{{#if async}}
return WaitForMs(0);
{{else}}
return CHIP_NO_ERROR;
{{/if}}
}
void {{>failureResponse}}({{>failureArguments}})
{
chip::app::StatusIB status(error);
{{#if response.error}}
VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), {{response.error}}));
{{#unless async}}NextTest();{{/unless}}
{{else if response.errorWrongValue}}
VerifyOrReturn(CheckConstraintNotValue("status", chip::to_underlying(status.mStatus), 0));
{{#unless async}}NextTest();{{/unless}}
{{else}}
{{#if optional}}(status.mStatus == chip::Protocols::InteractionModel::Status::UnsupportedAttribute) ? NextTest() : {{/if}}ThrowFailureResponse();
{{/if}}
}
{{#if isSubscribe}}
void {{>successResponse}}({{> subscribeResponseArguments attr=attributeObject}})
{
{{#if response.error}}
ThrowSuccessResponse();
{{else if response.errorWrongValue}}
ThrowSuccessResponse();
{{else}}
if ({{> subscribeDataCallback}}) {
auto callback = {{> subscribeDataCallback}};
{{> subscribeDataCallback}} = nullptr;
callback(this, value);
}
{{/if}}
}
void {{>subscriptionEstablished}}({{> subscriptionEstablishedArguments}})
{
{{#if hasWaitForReport}}
VerifyOrReturn(mReceivedReport_{{waitForReport.index}}, Exit("Initial report not received!"));
{{/if}}
{{#unless async}}NextTest();{{/unless}}
}
{{else}}
void {{>successResponse}}({{>successArguments}})
{
{{~#if response.error}}
ThrowSuccessResponse();
{{else if response.errorWrongValue}}
ThrowSuccessResponse();
{{else}}
{{#if isWaitForReport}}
mReceivedReport_{{index}} = true;
{{/if}}
{{#chip_tests_item_response_parameters}}
{{~#*inline "item"}}{{asLowerCamelCase name}}{{#if isOptional}}.Value(){{/if}}{{/inline}}
{{#if hasExpectedValue}}
{{>valueEquals actual=(asLowerCamelCase name) label=(asLowerCamelCase name) expected=expectedValue depth=0}}
{{/if}}
{{#if hasExpectedConstraints}}
{{#if isOptional}}
{{~#*inline "item"}}{{asLowerCamelCase name}}{{/inline}}
VerifyOrReturn(CheckValuePresent("{{> item}}", {{> item}}));
{{/if}}
{{#if (hasProperty expectedConstraints "type")}}VerifyOrReturn(CheckConstraintType("{{>item}}", "", "{{expectedConstraints.type}}"));{{/if}}
{{~#if (hasProperty expectedConstraints "format")}}VerifyOrReturn(CheckConstraintFormat("{{>item}}", "", "{{expectedConstraints.format}}"));{{/if}}
{{~#if (hasProperty expectedConstraints "startsWith")}}VerifyOrReturn(CheckConstraintStartsWith("{{>item}}", {{>item}}, "{{expectedConstraints.startsWith}}"));{{/if}}
{{~#if (hasProperty expectedConstraints "endsWith")}}VerifyOrReturn(CheckConstraintEndsWith("{{>item}}", {{>item}}, "{{expectedConstraints.endsWith}}"));{{/if}}
{{~#if (hasProperty expectedConstraints "isUpperCase")}}VerifyOrReturn(CheckConstraintIsUpperCase("{{>item}}", {{>item}}, {{expectedConstraints.isUpperCase}}));{{/if}}
{{~#if (hasProperty expectedConstraints "isLowerCase")}}VerifyOrReturn(CheckConstraintIsLowerCase("{{>item}}", {{>item}}, {{expectedConstraints.isLowerCase}}));{{/if}}
{{~#if (hasProperty expectedConstraints "isHexString")}}VerifyOrReturn(CheckConstraintIsHexString("{{>item}}", {{>item}}, {{expectedConstraints.isHexString}}));{{/if}}
{{~#if (hasProperty expectedConstraints "minLength")}}VerifyOrReturn(CheckConstraintMinLength("{{>item}}", {{>item}}.size(), {{expectedConstraints.minLength}}));{{/if}}
{{~#if (hasProperty expectedConstraints "maxLength")}}VerifyOrReturn(CheckConstraintMaxLength("{{>item}}", {{>item}}.size(), {{expectedConstraints.maxLength}}));{{/if}}
{{~#if (hasProperty expectedConstraints "minValue")}}VerifyOrReturn(CheckConstraintMinValue<{{chipType}}>("{{>item}}", {{>item}}, {{asTypedLiteral expectedConstraints.minValue type}}));{{/if}}
{{~#if (hasProperty expectedConstraints "maxValue")}}VerifyOrReturn(CheckConstraintMaxValue<{{chipType}}>("{{>item}}", {{>item}}, {{asTypedLiteral expectedConstraints.maxValue type}}));{{/if}}
{{~#if (hasProperty expectedConstraints "notValue")}}
{{#if (isLiteralNull expectedConstraints.notValue)}}
VerifyOrReturn(CheckValueNonNull("{{>item}}", {{>item}}));
{{else}}
VerifyOrReturn(CheckConstraintNotValue("{{>item}}", {{>item}}, {{asTypedLiteral expectedConstraints.notValue type}}));
{{/if}}
{{/if}}
{{/if}}
{{#if saveAs}}
{{#if (isString type)}}
if ({{saveAs}}Buffer != nullptr)
{
chip::Platform::MemoryFree({{saveAs}}Buffer);
}
{{saveAs}}Buffer = static_cast<{{#if (isOctetString type)}}uint8_t{{else}}char{{/if}} *>(chip::Platform::MemoryAlloc({{>item}}.size()));
memcpy({{saveAs}}Buffer, {{>item}}.data(), {{>item}}.size());
{{saveAs}} = {{chipType}}({{saveAs}}Buffer, {{>item}}.size());
{{else}}
{{saveAs}} = {{>item}};
{{/if}}
{{/if}}
{{/chip_tests_item_response_parameters}}
{{#unless async}}NextTest();{{/unless}}
{{/if}}
}
{{/if}}
{{#if isGroupCommand}}
void {{>doneResponse}}({{>doneArguments}})
{
NextTest();
}
{{/if}}
{{/if}}
{{/chip_tests_items}}
};
{{/chip_tests}}