From e57e270b220aca842c7c9988a9e80586cfaa5d04 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Fri, 27 Sep 2024 21:55:16 -0400 Subject: [PATCH] Fix data type check in MTRDevice's invokeCommandWithEndpointID. 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 6e3e7980583574..ba8d4ab55c5c21 100644 --- a/src/darwin/Framework/CHIP/MTRDevice.mm +++ b/src/darwin/Framework/CHIP/MTRDevice.mm @@ -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]); 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