From 10682042114b06e9565a9bf85c8c4102e68187dc Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 3 Oct 2022 15:02:36 -0400 Subject: [PATCH] Enable all the Darwin framework tests. (#22781) We had some tests disabled because multiple test files needed to commission a server, and after the first one ran the second one would fail. The fix is to put the server back into commissioning mode after a test is done. Co-authored-by: Andrei Litvin --- .../Framework/CHIPTests/MTRDeviceTests.m | 52 +++++++++++++++++++ .../CHIPTests/MTRXPCListenerSampleTests.m | 52 +++++++++++++++++++ .../xcschemes/Matter Framework Tests.xcscheme | 3 -- 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m index d051fd6c480e50..bf9699b7e17964 100644 --- a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m +++ b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m @@ -1357,6 +1357,58 @@ - (void)test900_SubscribeAllAttributes #if !MANUAL_INDIVIDUAL_TEST - (void)test999_TearDown { + // Put the device back in the state we found it: open commissioning window, no fabrics commissioned. + MTRBaseDevice * device = GetConnectedDevice(); + dispatch_queue_t queue = dispatch_get_main_queue(); + + // Get our current fabric index, for later deletion. + XCTestExpectation * readFabricIndexExpectation = [self expectationWithDescription:@"Fabric index read"]; + + __block NSNumber * fabricIndex; + __auto_type * opCredsCluster = [[MTRBaseClusterOperationalCredentials alloc] initWithDevice:device endpoint:@(0) queue:queue]; + [opCredsCluster readAttributeCurrentFabricIndexWithCompletion:^(NSNumber * _Nullable value, NSError * _Nullable readError) { + XCTAssertNil(readError); + XCTAssertNotNil(value); + fabricIndex = value; + [readFabricIndexExpectation fulfill]; + }]; + + [self waitForExpectations:@[ readFabricIndexExpectation ] timeout:kTimeoutInSeconds]; + + // Open a commissioning window. + XCTestExpectation * openCommissioningWindowExpectation = [self expectationWithDescription:@"Commissioning window opened"]; + + __auto_type * adminCommissioningCluster = [[MTRBaseClusterAdministratorCommissioning alloc] initWithDevice:device + endpoint:@(0) + queue:queue]; + __auto_type * openWindowParams = [[MTRAdministratorCommissioningClusterOpenBasicCommissioningWindowParams alloc] init]; + openWindowParams.commissioningTimeout = @(900); + openWindowParams.timedInvokeTimeoutMs = @(50000); + [adminCommissioningCluster openBasicCommissioningWindowWithParams:openWindowParams + completion:^(NSError * _Nullable error) { + XCTAssertNil(error); + [openCommissioningWindowExpectation fulfill]; + }]; + + [self waitForExpectations:@[ openCommissioningWindowExpectation ] timeout:kTimeoutInSeconds]; + + // Remove our fabric from the device. + XCTestExpectation * removeFabricExpectation = [self expectationWithDescription:@"Fabric removed"]; + + __auto_type * removeParams = [[MTROperationalCredentialsClusterRemoveFabricParams alloc] init]; + removeParams.fabricIndex = fabricIndex; + + [opCredsCluster removeFabricWithParams:removeParams + completion:^(MTROperationalCredentialsClusterNOCResponseParams * _Nullable data, + NSError * _Nullable removeError) { + XCTAssertNil(removeError); + XCTAssertNotNil(data); + XCTAssertEqualObjects(data.statusCode, @(0)); + [removeFabricExpectation fulfill]; + }]; + + [self waitForExpectations:@[ removeFabricExpectation ] timeout:kTimeoutInSeconds]; + [self shutdownStack]; } #endif diff --git a/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m b/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m index 905295c13b112a..c6ff3884d28714 100644 --- a/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m +++ b/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m @@ -1813,6 +1813,58 @@ - (void)test900_SubscribeClusterStateCache #if !MANUAL_INDIVIDUAL_TEST - (void)test999_TearDown { + // Put the device back in the state we found it: open commissioning window, no fabrics commissioned. + MTRBaseDevice * device = [MTRBaseDevice deviceWithNodeID:@(kDeviceId) controller:sController]; + dispatch_queue_t queue = dispatch_get_main_queue(); + + // Get our current fabric index, for later deletion. + XCTestExpectation * readFabricIndexExpectation = [self expectationWithDescription:@"Fabric index read"]; + + __block NSNumber * fabricIndex; + __auto_type * opCredsCluster = [[MTRBaseClusterOperationalCredentials alloc] initWithDevice:device endpoint:@(0) queue:queue]; + [opCredsCluster readAttributeCurrentFabricIndexWithCompletion:^(NSNumber * _Nullable value, NSError * _Nullable readError) { + XCTAssertNil(readError); + XCTAssertNotNil(value); + fabricIndex = value; + [readFabricIndexExpectation fulfill]; + }]; + + [self waitForExpectations:@[ readFabricIndexExpectation ] timeout:kTimeoutInSeconds]; + + // Open a commissioning window. + XCTestExpectation * openCommissioningWindowExpectation = [self expectationWithDescription:@"Commissioning window opened"]; + + __auto_type * adminCommissioningCluster = [[MTRBaseClusterAdministratorCommissioning alloc] initWithDevice:device + endpoint:@(0) + queue:queue]; + __auto_type * openWindowParams = [[MTRAdministratorCommissioningClusterOpenBasicCommissioningWindowParams alloc] init]; + openWindowParams.commissioningTimeout = @(900); + openWindowParams.timedInvokeTimeoutMs = @(50000); + [adminCommissioningCluster openBasicCommissioningWindowWithParams:openWindowParams + completion:^(NSError * _Nullable error) { + XCTAssertNil(error); + [openCommissioningWindowExpectation fulfill]; + }]; + + [self waitForExpectations:@[ openCommissioningWindowExpectation ] timeout:kTimeoutInSeconds]; + + // Remove our fabric from the device. + XCTestExpectation * removeFabricExpectation = [self expectationWithDescription:@"Fabric removed"]; + + __auto_type * removeParams = [[MTROperationalCredentialsClusterRemoveFabricParams alloc] init]; + removeParams.fabricIndex = fabricIndex; + + [opCredsCluster removeFabricWithParams:removeParams + completion:^(MTROperationalCredentialsClusterNOCResponseParams * _Nullable data, + NSError * _Nullable removeError) { + XCTAssertNil(removeError); + XCTAssertNotNil(data); + XCTAssertEqualObjects(data.statusCode, @(0)); + [removeFabricExpectation fulfill]; + }]; + + [self waitForExpectations:@[ removeFabricExpectation ] timeout:kTimeoutInSeconds]; + [self shutdownStack]; } #endif diff --git a/src/darwin/Framework/Matter.xcodeproj/xcshareddata/xcschemes/Matter Framework Tests.xcscheme b/src/darwin/Framework/Matter.xcodeproj/xcshareddata/xcschemes/Matter Framework Tests.xcscheme index 29d307b7ed6e94..72d9cc7094a26a 100644 --- a/src/darwin/Framework/Matter.xcodeproj/xcshareddata/xcschemes/Matter Framework Tests.xcscheme +++ b/src/darwin/Framework/Matter.xcodeproj/xcshareddata/xcschemes/Matter Framework Tests.xcscheme @@ -38,9 +38,6 @@ ReferencedContainer = "container:Matter.xcodeproj"> - -