Skip to content

Commit

Permalink
[YAML] Issue an error on pairing timeout (#16774)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored Apr 5, 2022
1 parent 04f1acd commit 3034129
Show file tree
Hide file tree
Showing 24 changed files with 6,569 additions and 1,901 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ class {{filename}}: public TestCommandBridge
{{else}}
{{#if (isString type)}}@"{{/if}}{{definedValue}}{{#if (isString type)}}"{{/if}}
{{~/if~}}
{{/chip_tests_item_parameters}}
{{additionalArguments}});
{{/chip_tests_item_parameters}});
{{else}}
CHIPDevice * device = GetConnectedDevice();
CHIPTest{{asUpperCamelCase cluster}} * cluster = [[CHIPTest{{asUpperCamelCase cluster}} alloc] initWithDevice:device endpoint:{{endpoint}} queue:mCallbackQueue];
Expand Down
6 changes: 6 additions & 0 deletions examples/chip-tool/commands/tests/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ class TestCommand : public CHIPCommand,
chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;

bool IsUnsupported(const chip::app::StatusIB & status)
{
return status.mStatus == chip::Protocols::InteractionModel::Status::UnsupportedAttribute ||
status.mStatus == chip::Protocols::InteractionModel::Status::UnsupportedCommand;
}

void Wait()
{
if (mDelayInMs.HasValue())
Expand Down
31 changes: 30 additions & 1 deletion examples/chip-tool/templates/tests/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* limitations under the License.
*/

const { asLowerCamelCase } = require('../../../../src/app/zap-templates/templates/app/helper.js');
const { zapTypeToDecodableClusterObjectType, asUpperCamelCase, asLowerCamelCase }
= require('../../../../src/app/zap-templates/templates/app/helper.js');
const { isTestOnlyCluster } = require('../../../../src/app/zap-templates/common/simulated-clusters/SimulatedClusters.js');

function utf8StringLength(str)
Expand Down Expand Up @@ -47,8 +48,36 @@ function asPropertyValue(options)
return name;
}

async function asDecodableType()
{
// Copy some properties needed by zapTypeToDecodableClusterObjectType
let target = { global : this.global, entryType : this.entryType };

let type;
if ('commandObject' in this) {
type = this.commandObject.responseName;
} else if ('attributeObject' in this) {
type = this.attributeObject.type;
target.isArray = this.attributeObject.isArray;
target.isOptional = this.attributeObject.isOptional;
target.isNullable = this.attributeObject.isNullable;
} else if ('eventObject' in this) {
type = this.eventObject.type;
} else {
throw new Error("Unsupported decodable type");
}

if (isTestOnlyCluster(this.cluster) || 'commandObject' in this) {
return `chip::app::Clusters::${asUpperCamelCase(this.cluster)}::Commands::${asUpperCamelCase(type)}::DecodableType`;
}

const options = { 'hash' : { ns : this.cluster } };
return await zapTypeToDecodableClusterObjectType.call(target, type, options);
}

//
// Module exports
//
exports.utf8StringLength = utf8StringLength;
exports.asPropertyValue = asPropertyValue;
exports.asDecodableType = asDecodableType;
43 changes: 24 additions & 19 deletions examples/chip-tool/templates/tests/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,31 @@ class {{filename}}Suite: public TestCommand

{{>setupSaveAs}}

void OnDiscoveryCommandsResults(const DiscoveryCommandResult & value) override
void OnResponse(const chip::app::StatusIB & status, chip::TLV::TLVReader * data) override
{
bool isExpectedDnssdResult = false;
{{#chip_tests_items}}
{{#if (isStrEqual cluster "DiscoveryCommands")}}
if ((mTestIndex - 1) == {{index}})
{
isExpectedDnssdResult = true;
{{#chip_tests_item_response_parameters}}
{{>maybeCheckExpectedValue}}
{{>maybeCheckExpectedConstraints}}
{{>maybeSaveAs}}
{{/chip_tests_item_response_parameters}}
}
{{/if}}
{{/chip_tests_items}}
bool shouldContinue = false;

VerifyOrReturn(isExpectedDnssdResult, Exit("An unexpected dnssd result has been received"));
NextTest();
switch (mTestIndex - 1)
{
{{#chip_tests_items}}
{{!
This check can be removed once the cluster APIs have been converted from Invoke to
ReadClient/WriteClient/CommandSender
}}
{{~#if (isTestOnlyCluster cluster)~}}
case {{index}}:
{{>test_step_response}}
break;
{{/if~}}
{{/chip_tests_items}}
default:
LogErrorOnFailure(ContinueOnChipMainThread(CHIP_ERROR_INVALID_ARGUMENT));
}

if (shouldContinue)
{
ContinueOnChipMainThread(CHIP_NO_ERROR);
}
}

{{! Helper around zapTypeToDecodableClusterObjectType that lets us set the
Expand Down Expand Up @@ -203,8 +209,7 @@ class {{filename}}Suite: public TestCommand
{{~else if (isString type)}}"{{definedValue}}"
{{else}}{{definedValue}}
{{~/if~}}
{{/chip_tests_item_parameters}}
{{additionalArguments}});
{{/chip_tests_item_parameters}});
}
{{else if isWait}}
CHIP_ERROR {{>testCommand}}()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{~#*inline "maybeCheckClusterError"}}
{{#if response.clusterError}}
VerifyOrReturn(CheckValue("clusterStatus", status.mClusterStatus.HasValue(), true));
VerifyOrReturn(CheckValue("clusterStatus", status.mClusterStatus.Value(), {{response.clusterError}}));
{{/if}}
{{/inline~}}

{{~#*inline "maybeContinueWithoutWaitingOnDone"}}
{{~#if isWaitForReport}}shouldContinue = true;{{/if~}}
{{~#if (isTestOnlyCluster cluster)}}shouldContinue = true;{{/if~}}
{{/inline~}}

{{~#*inline "maybeReturnOnUnsupported"}}
{{~#if optional}}
if (IsUnsupported(status.mStatus))
{
{{>maybeContinueWithoutWaitingOnDone}}
return;
}
{{/if~}}
{{/inline~}}

{{! --- Test Step Response Content --}}
{{#if response.error}}
VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), {{response.error}}));
{{>maybeCheckClusterError}}
{{else}}
{{>maybeReturnOnUnsupported}}
VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), {{response.error}}));
{{#if hasSpecificResponse}}
{
{{asDecodableType}} value;
VerifyOrReturn(CheckDecodeValue(chip::app::DataModel::Decode(*data, value)));
{{#chip_tests_item_response_parameters}}
{{>maybeCheckExpectedValue}}
{{>maybeCheckExpectedConstraints}}
{{>maybeSaveAs}}
{{/chip_tests_item_response_parameters}}
}
{{/if}}
{{/if}}
{{>maybeContinueWithoutWaitingOnDone}}
4 changes: 4 additions & 0 deletions examples/chip-tool/templates/tests/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"name": "test_cluster",
"path": "partials/test_cluster.zapt"
},
{
"name": "test_step_response",
"path": "partials/test_step_response.zapt"
},
{
"name": "maybeCheckExpectedValue",
"path": "partials/checks/maybeCheckExpectedValue.zapt"
Expand Down
4 changes: 4 additions & 0 deletions examples/placeholder/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"name": "test_cluster",
"path": "../../../examples/chip-tool/templates/tests/partials/test_cluster.zapt"
},
{
"name": "test_step_response",
"path": "../../../examples/chip-tool/templates/tests/partials/test_step_response.zapt"
},
{
"name": "maybeCheckExpectedValue",
"path": "../../../examples/chip-tool/templates/tests/partials/checks/maybeCheckExpectedValue.zapt"
Expand Down
17 changes: 16 additions & 1 deletion src/app/tests/suites/TestMultiAdmin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ tests:
- name: "nodeId"
value: nodeId

- label: "Commission from alpha when the commissioning window is not opened"
identity: "alpha"
cluster: "CommissionerCommands"
command: "PairWithQRCode"
arguments:
values:
- name: "nodeId"
value: nodeIdForDuplicateCommissioning
- name: "payload"
value: payload
response:
error: FAILURE

- label: "Open Commissioning Window from alpha"
cluster: "AdministratorCommissioning"
command: "OpenBasicCommissioningWindow"
Expand All @@ -63,13 +76,15 @@ tests:
identity: "alpha"
cluster: "CommissionerCommands"
command: "PairWithQRCode"
additionalArguments: ", CHIP_ERROR_FABRIC_EXISTS"
arguments:
values:
- name: "nodeId"
value: nodeIdForDuplicateCommissioning
- name: "payload"
value: payload
response:
error: FAILURE
clusterError: 0x09 #OperationalCertStatus::kFabricConflict

- label: "Check that we just have the one fabric and did not add a new one"
identity: "alpha"
Expand Down
82 changes: 42 additions & 40 deletions src/app/tests/suites/commands/commissioner/CommissionerCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@

constexpr uint16_t kPayloadMaxSize = 64;

CHIP_ERROR CommissionerCommands::PairWithQRCode(chip::NodeId nodeId, const chip::CharSpan payload, CHIP_ERROR expectedStatus)
CHIP_ERROR CommissionerCommands::PairWithQRCode(chip::NodeId nodeId, const chip::CharSpan payload)
{
VerifyOrReturnError(payload.size() > 0 && payload.size() < kPayloadMaxSize, CHIP_ERROR_INVALID_ARGUMENT);

mExpectedStatus = expectedStatus;

GetCurrentCommissioner().RegisterPairingDelegate(this);

char qrCode[kPayloadMaxSize];
Expand All @@ -37,8 +35,6 @@ CHIP_ERROR CommissionerCommands::PairWithManualCode(chip::NodeId nodeId, const c
{
VerifyOrReturnError(payload.size() > 0 && payload.size() < kPayloadMaxSize, CHIP_ERROR_INVALID_ARGUMENT);

mExpectedStatus = CHIP_NO_ERROR;

GetCurrentCommissioner().RegisterPairingDelegate(this);

char manualCode[kPayloadMaxSize];
Expand All @@ -48,11 +44,42 @@ CHIP_ERROR CommissionerCommands::PairWithManualCode(chip::NodeId nodeId, const c

CHIP_ERROR CommissionerCommands::Unpair(chip::NodeId nodeId)
{
mExpectedStatus = CHIP_NO_ERROR;

return GetCurrentCommissioner().UnpairDevice(nodeId);
}

chip::app::StatusIB ConvertToStatusIB(CHIP_ERROR err)
{
using chip::app::StatusIB;
using namespace chip;
using namespace chip::Protocols::InteractionModel;
using namespace chip::app::Clusters::OperationalCredentials;

if (CHIP_ERROR_INVALID_PUBLIC_KEY == err)
{
return StatusIB(Status::Failure, to_underlying(OperationalCertStatus::kInvalidPublicKey));
}
else if (CHIP_ERROR_WRONG_NODE_ID == err)
{
return StatusIB(Status::Failure, to_underlying(OperationalCertStatus::kInvalidNodeOpId));
}
else if (CHIP_ERROR_UNSUPPORTED_CERT_FORMAT == err)
{
return StatusIB(Status::Failure, to_underlying(OperationalCertStatus::kInvalidNOC));
}
else if (CHIP_ERROR_FABRIC_EXISTS == err)
{
return StatusIB(Status::Failure, to_underlying(OperationalCertStatus::kFabricConflict));
}
else if (CHIP_ERROR_INVALID_FABRIC_ID == err)
{
return StatusIB(Status::Failure, to_underlying(OperationalCertStatus::kInvalidFabricIndex));
}
else
{
return StatusIB(err);
}
}

void CommissionerCommands::OnStatusUpdate(DevicePairingDelegate::Status status)
{
switch (status)
Expand All @@ -62,6 +89,7 @@ void CommissionerCommands::OnStatusUpdate(DevicePairingDelegate::Status status)
break;
case DevicePairingDelegate::Status::SecurePairingFailed:
ChipLogError(chipTool, "Secure Pairing Failed");
OnResponse(ConvertToStatusIB(CHIP_ERROR_INCORRECT_STATE), nullptr);
break;
}
}
Expand All @@ -71,52 +99,26 @@ void CommissionerCommands::OnPairingComplete(CHIP_ERROR err)
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, "Pairing Complete Failure: %s", ErrorStr(err));
LogErrorOnFailure(ContinueOnChipMainThread(err));
OnResponse(ConvertToStatusIB(err), nullptr);
}
}

void CommissionerCommands::OnPairingDeleted(CHIP_ERROR err)
{
if (mExpectedStatus != err)
if (err != CHIP_NO_ERROR)
{
if (err != CHIP_NO_ERROR)
{
ChipLogError(chipTool, "Pairing Delete Failure: %s", ErrorStr(err));
}
else
{
ChipLogError(chipTool, "Got success but expected: %s", ErrorStr(mExpectedStatus));
err = CHIP_ERROR_INCORRECT_STATE;
}
}
else
{
// Treat as success.
err = CHIP_NO_ERROR;
ChipLogError(chipTool, "Pairing Delete Failure: %s", ErrorStr(err));
}

LogErrorOnFailure(ContinueOnChipMainThread(err));
OnResponse(ConvertToStatusIB(err), nullptr);
}

void CommissionerCommands::OnCommissioningComplete(chip::NodeId nodeId, CHIP_ERROR err)
{
if (mExpectedStatus != err)
{
if (err != CHIP_NO_ERROR)
{
ChipLogError(chipTool, "Commissioning Complete Failure: %s", ErrorStr(err));
}
else
{
ChipLogError(chipTool, "Got success but expected: %s", ErrorStr(mExpectedStatus));
err = CHIP_ERROR_INCORRECT_STATE;
}
}
else
if (err != CHIP_NO_ERROR)
{
// Treat as success.
err = CHIP_NO_ERROR;
ChipLogError(chipTool, "Commissioning Complete Failure: %s", ErrorStr(err));
}

LogErrorOnFailure(ContinueOnChipMainThread(err));
OnResponse(ConvertToStatusIB(err), nullptr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ class CommissionerCommands : public chip::Controller::DevicePairingDelegate
CommissionerCommands(){};
~CommissionerCommands() override{};

virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;
virtual chip::Controller::DeviceCommissioner & GetCurrentCommissioner() = 0;
virtual void OnResponse(const chip::app::StatusIB & status, chip::TLV::TLVReader * data) = 0;
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;
virtual chip::Controller::DeviceCommissioner & GetCurrentCommissioner() = 0;

CHIP_ERROR PairWithQRCode(chip::NodeId nodeId, const chip::CharSpan payload, CHIP_ERROR expectedStatus = CHIP_NO_ERROR);
CHIP_ERROR PairWithQRCode(chip::NodeId nodeId, const chip::CharSpan payload);
CHIP_ERROR PairWithManualCode(chip::NodeId nodeId, const chip::CharSpan payload);
CHIP_ERROR Unpair(chip::NodeId nodeId);

Expand All @@ -39,7 +40,4 @@ class CommissionerCommands : public chip::Controller::DevicePairingDelegate
void OnPairingComplete(CHIP_ERROR error) override;
void OnPairingDeleted(CHIP_ERROR error) override;
void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR error) override;

private:
CHIP_ERROR mExpectedStatus = CHIP_NO_ERROR;
};
1 change: 1 addition & 0 deletions src/app/tests/suites/commands/discovery/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static_library("discovery") {
cflags = [ "-Wconversion" ]

public_deps = [
"${chip_root}/src/app",
"${chip_root}/src/lib/address_resolve",
"${chip_root}/src/lib/support",
]
Expand Down
Loading

0 comments on commit 3034129

Please sign in to comment.