Skip to content

Commit

Permalink
Fix data type check in MTRDevice's invokeCommandWithEndpointID.
Browse files Browse the repository at this point in the history
We were checking two NSStrings for pointer-equality, when we should be testing
for logical equality.
  • Loading branch information
bzbarsky-apple committed Sep 28, 2024
1 parent 241cec1 commit e57e270
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ - (void)invokeCommandWithEndpointID:(NSNumber *)endpointID
}

MTRDeviceDataValueDictionary fieldsDataValue = commandFields;
if (fieldsDataValue[MTRTypeKey] != MTRStructureValueType) {
if (![MTRStructureValueType isEqual:fieldsDataValue[MTRTypeKey]]) {
MTR_LOG_ERROR("%@ invokeCommandWithEndpointID passed a commandFields (%@) that is not a structure-typed data-value object",
self, commandFields);
completion(nil, [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT]);
Expand Down
45 changes: 45 additions & 0 deletions src/darwin/Framework/CHIPTests/MTRDeviceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,51 @@ - (void)test019_MTRDeviceMultipleCommands
}];

[self waitForExpectations:@[ readFabricLabelExpectation ] timeout:kTimeoutInSeconds];

// Now test doing the UpdateFabricLabel command but directly via the
// MTRDevice API.
XCTestExpectation * updateLabelExpectation2 = [self expectationWithDescription:@"Fabric label updated a second time"];
// IMPORTANT: commandFields here uses hardcoded strings, not MTR* constants
// for the strings, to check for places that are doing string equality wrong.
__auto_type * commandFields = @{
@"type" : @"Structure",
@"value" : @[
@{
@"contextTag" : @0,
@"data" : @ {
@"type" : @"UTF8String",
@"value" : @"Test2",
},
},
],
};

[device invokeCommandWithEndpointID:@(0)
clusterID:@(MTRClusterIDTypeOperationalCredentialsID)
commandID:@(MTRCommandIDTypeClusterOperationalCredentialsCommandUpdateFabricLabelID)
commandFields:commandFields
expectedValues:nil
expectedValueInterval:nil
queue:queue
completion:^(NSArray<NSDictionary<NSString *, id> *> * _Nullable values, NSError * _Nullable error) {
XCTAssertNil(error);
[updateLabelExpectation2 fulfill];
}];

[self waitForExpectations:@[ updateLabelExpectation2 ] timeout:kTimeoutInSeconds];

// And again, make sure our fabric label got updated.
readFabricLabelExpectation = [self expectationWithDescription:@"Read fabric label third time"];
[baseOpCredsCluster readAttributeFabricsWithParams:nil completion:^(NSArray * _Nullable value, NSError * _Nullable error) {
XCTAssertNil(error);
XCTAssertNotNil(value);
XCTAssertEqual(value.count, 1);
MTROperationalCredentialsClusterFabricDescriptorStruct * entry = value[0];
XCTAssertEqualObjects(entry.label, @"Test2");
[readFabricLabelExpectation fulfill];
}];

[self waitForExpectations:@[ readFabricLabelExpectation ] timeout:kTimeoutInSeconds];
}

- (void)test020_ReadMultipleAttributes
Expand Down

0 comments on commit e57e270

Please sign in to comment.