From 6bbe1d630ef2284b5c3326728bfce267c1b2eb84 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Sat, 28 Sep 2024 01:19:56 -0400 Subject: [PATCH] Fix data type check in MTRDevice's invokeCommandWithEndpointID. (#35834) We were checking two NSStrings for pointer-equality, when we should be testing for logical equality. --- src/darwin/Framework/CHIP/MTRDevice.mm | 2 +- .../Framework/CHIPTests/MTRDeviceTests.m | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice.mm b/src/darwin/Framework/CHIP/MTRDevice.mm index 2de4d6e3399b16..a584bd68e06f9f 100644 --- a/src/darwin/Framework/CHIP/MTRDevice.mm +++ b/src/darwin/Framework/CHIP/MTRDevice.mm @@ -1063,7 +1063,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]); diff --git a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m index b8552ce325dc8b..cb2acf2db2ce28 100644 --- a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m +++ b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m @@ -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 *> * _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