From f8e203b4fbe3a5108131bdf82202d3c288b0379b Mon Sep 17 00:00:00 2001 From: Kundok Park <97262718+kpark-apple@users.noreply.github.com> Date: Thu, 10 Mar 2022 07:58:49 -0800 Subject: [PATCH] Fix XPC connection failure handling (#16063) for Darwin CHIPDeviceController. --- .../CHIP/CHIPDeviceControllerXPCConnection.m | 1 + .../CHIPTests/CHIPXPCProtocolTests.m | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/darwin/Framework/CHIP/CHIPDeviceControllerXPCConnection.m b/src/darwin/Framework/CHIP/CHIPDeviceControllerXPCConnection.m index 2af67652b89f82..ea34191e7081aa 100644 --- a/src/darwin/Framework/CHIP/CHIPDeviceControllerXPCConnection.m +++ b/src/darwin/Framework/CHIP/CHIPDeviceControllerXPCConnection.m @@ -93,6 +93,7 @@ - (void)getProxyHandleWithCompletion:(void (^)(dispatch_queue_t queue, if (!xpcConnection) { CHIP_LOG_ERROR("Cannot connect to XPC server for remote controller"); completion(self.workQueue, nil); + return; } xpcConnection.remoteObjectInterface = self.remoteDeviceServerProtocol; xpcConnection.exportedInterface = self.remoteDeviceClientProtocol; diff --git a/src/darwin/Framework/CHIPTests/CHIPXPCProtocolTests.m b/src/darwin/Framework/CHIPTests/CHIPXPCProtocolTests.m index 76177624dc08b6..fea62527b4eecd 100644 --- a/src/darwin/Framework/CHIPTests/CHIPXPCProtocolTests.m +++ b/src/darwin/Framework/CHIPTests/CHIPXPCProtocolTests.m @@ -1823,4 +1823,39 @@ - (void)testReadAttributeCacheFailure XCTAssertNil(_xpcConnection); } +- (void)testXPCConnectionFailure +{ + uint64_t myNodeId = 9876543210; + NSUInteger myEndpointId = 100; + NSUInteger myClusterId = 200; + NSUInteger myAttributeId = 300; + XCTestExpectation * responseExpectation = [self expectationWithDescription:@"Read response received"]; + + // Test with a device controller which wouldn't connect to XPC listener successfully + __auto_type failingDeviceController = [CHIPDeviceController sharedControllerWithId:_controllerUUID + xpcConnectBlock:^NSXPCConnection * { + return nil; + }]; + + [failingDeviceController getConnectedDevice:myNodeId + queue:dispatch_get_main_queue() + completionHandler:^(CHIPDevice * _Nullable device, NSError * _Nullable error) { + XCTAssertNotNil(device); + XCTAssertNil(error); + NSLog(@"Device acquired. Reading..."); + [device readAttributeWithEndpointId:myEndpointId + clusterId:myClusterId + attributeId:myAttributeId + clientQueue:dispatch_get_main_queue() + completion:^(id _Nullable value, NSError * _Nullable error) { + NSLog(@"Read value: %@", value); + XCTAssertNil(value); + XCTAssertNotNil(error); + [responseExpectation fulfill]; + }]; + }]; + + [self waitForExpectations:@[ responseExpectation ] timeout:kTimeoutInSeconds]; +} + @end