From e9e482be3adf77278c70d275b47264542ab254b3 Mon Sep 17 00:00:00 2001 From: Karsten Sperling Date: Mon, 12 Feb 2024 17:35:36 +1300 Subject: [PATCH 1/2] Darwin: Simplify test setup and teardown Avoid relying on dummy tests that are ordered first / last. --- .../CHIPTests/MTRBackwardsCompatTests.m | 124 +++++---------- .../CHIPTests/MTRCommissionableBrowserTests.m | 50 ++----- .../Framework/CHIPTests/MTRDeviceTests.m | 76 ++-------- .../Framework/CHIPTests/MTRPairingTests.m | 141 ++++++------------ .../CHIPTests/MTRPerControllerStorageTests.m | 4 - .../CHIPTests/MTRSwiftDeviceTests.swift | 73 +++------ .../MTRTestResetCommissioneeHelper.m | 15 +- 7 files changed, 144 insertions(+), 339 deletions(-) diff --git a/src/darwin/Framework/CHIPTests/MTRBackwardsCompatTests.m b/src/darwin/Framework/CHIPTests/MTRBackwardsCompatTests.m index 3db07720954f17..eb26a9af379ec3 100644 --- a/src/darwin/Framework/CHIPTests/MTRBackwardsCompatTests.m +++ b/src/darwin/Framework/CHIPTests/MTRBackwardsCompatTests.m @@ -30,6 +30,8 @@ // system dependencies #import +// Fixture: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1 + static const uint16_t kPairingTimeoutInSeconds = 10; static const uint16_t kCASESetupTimeoutInSeconds = 30; static const uint16_t kTimeoutInSeconds = 3; @@ -73,7 +75,7 @@ - (void)onPairingComplete:(NSError *)error XCTAssertEqual(error.code, 0); NSError * commissionError = nil; - [sController commissionDevice:kDeviceId commissioningParams:[[MTRCommissioningParameters alloc] init] error:&commissionError]; + XCTAssertTrue([sController commissionDevice:kDeviceId commissioningParams:[[MTRCommissioningParameters alloc] init] error:&commissionError]); XCTAssertNil(commissionError); // Keep waiting for onCommissioningComplete @@ -91,45 +93,13 @@ - (void)onCommissioningComplete:(NSError *)error @interface MTRBackwardsCompatTests : XCTestCase @end -static BOOL sStackInitRan = NO; -static BOOL sNeedsStackShutdown = YES; - @implementation MTRBackwardsCompatTests -+ (void)tearDown ++ (void)setUp { - // Global teardown, runs once - if (sNeedsStackShutdown) { - // We don't need to worry about ResetCommissionee. If we get here, - // we're running only one of our test methods (using - // -only-testing:MatterTests/MTROTAProviderTests/testMethodName), since - // we did not run test999_TearDown. - [self shutdownStack]; - } -} - -- (void)setUp -{ - // Per-test setup, runs before each test. [super setUp]; - [self setContinueAfterFailure:NO]; - if (sStackInitRan == NO) { - [self initStack]; - } -} - -- (void)tearDown -{ - // Per-test teardown, runs after each test. - [super tearDown]; -} - -- (void)initStack -{ - sStackInitRan = YES; - - XCTestExpectation * expectation = [self expectationWithDescription:@"Pairing Complete"]; + XCTestExpectation * expectation = [[XCTestExpectation alloc] initWithDescription:@"Pairing Complete"]; __auto_type * factory = [MTRControllerFactory sharedInstance]; XCTAssertNotNil(factory); @@ -137,18 +107,13 @@ - (void)initStack __auto_type * storage = [[MTRTestStorage alloc] init]; __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; factoryParams.port = @(kLocalPort); + XCTAssertTrue([factory startup:factoryParams]); - BOOL ok = [factory startup:factoryParams]; - XCTAssertTrue(ok); - - __auto_type * testKeys = [[MTRTestKeys alloc] init]; - XCTAssertNotNil(testKeys); - - sTestKeys = testKeys; + XCTAssertNotNil(sTestKeys = [[MTRTestKeys alloc] init]); // Needs to match what startControllerOnExistingFabric calls elsewhere in // this file do. - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys fabricId:1 ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:sTestKeys fabricId:1 ipk:sTestKeys.ipk]; params.vendorId = @(kTestVendorId); MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; @@ -166,12 +131,12 @@ - (void)initStack XCTAssertNotNil(payload); XCTAssertNil(error); - [controller setupCommissioningSessionWithPayload:payload newNodeID:@(kDeviceId) error:&error]; + XCTAssertTrue([controller setupCommissioningSessionWithPayload:payload newNodeID:@(kDeviceId) error:&error]); XCTAssertNil(error); - [self waitForExpectationsWithTimeout:kPairingTimeoutInSeconds handler:nil]; + XCTAssertEqual([XCTWaiter waitForExpectations:@[ expectation ] timeout:kPairingTimeoutInSeconds], XCTWaiterResultCompleted); - __block XCTestExpectation * connectionExpectation = [self expectationWithDescription:@"CASE established"]; + __block XCTestExpectation * connectionExpectation = [[XCTestExpectation alloc] initWithDescription:@"CASE established"]; [controller getBaseDevice:kDeviceId queue:dispatch_get_main_queue() completionHandler:^(MTRBaseDevice * _Nullable device, NSError * _Nullable error) { @@ -180,57 +145,54 @@ - (void)initStack sConnectedDevice = device; connectionExpectation = nil; }]; - [self waitForExpectationsWithTimeout:kCASESetupTimeoutInSeconds handler:nil]; + XCTAssertEqual([XCTWaiter waitForExpectations:@[ connectionExpectation ] timeout:kCASESetupTimeoutInSeconds], XCTWaiterResultCompleted); } -+ (void)shutdownStack ++ (void)tearDown { - sNeedsStackShutdown = NO; - - MTRDeviceController * controller = sController; - XCTAssertNotNil(controller); - - [controller shutdown]; - XCTAssertFalse([controller isRunning]); + ResetCommissionee(GetConnectedDevice(), dispatch_get_main_queue(), nil, kTimeoutInSeconds); + [sController shutdown]; + XCTAssertFalse([sController isRunning]); [[MTRControllerFactory sharedInstance] shutdown]; + + [super tearDown]; } -- (void)test000_SetUp +- (void)setUp { - // Nothing to do here; our setUp method handled this already. This test - // just exists to make the setup not look like it's happening inside other - // tests. + [super setUp]; + [self setContinueAfterFailure:NO]; } -#define CHECK_RETURN_TYPE(sig, type) \ - do { \ - XCTAssertNotNil(sig); \ - XCTAssertTrue(strcmp([sig methodReturnType], @encode(type)) == 0); \ +#define CHECK_RETURN_TYPE(sig, type) \ + do { \ + XCTAssertNotNil(sig); \ + XCTAssertTrue(strcmp([sig methodReturnType], @encode(type)) == 0); \ } while (0) /** * Arguments 0 and 1 are the implicit self and _cmd arguments; the real arguments begin at index 2. */ -#define CHECK_ARGUMENT(sig, index, type) \ - do { \ - XCTAssertTrue(strcmp([sig getArgumentTypeAtIndex:(index) + 2], @encode(type)) == 0); \ +#define CHECK_ARGUMENT(sig, index, type) \ + do { \ + XCTAssertTrue(strcmp([sig getArgumentTypeAtIndex:(index) + 2], @encode(type)) == 0); \ } while (0) -#define CHECK_READONLY_PROPERTY(instance, propName, type) \ - do { \ - NSMethodSignature * signature = [instance methodSignatureForSelector:@selector(propName)]; \ - CHECK_RETURN_TYPE(signature, type); \ - /* Check that getting the property directly compiles too */ \ - (void) instance.propName; \ +#define CHECK_READONLY_PROPERTY(instance, propName, type) \ + do { \ + NSMethodSignature * signature = [instance methodSignatureForSelector:@selector(propName)]; \ + CHECK_RETURN_TYPE(signature, type); \ + /* Check that getting the property directly compiles too */ \ + (void) instance.propName; \ } while (0) -#define CHECK_PROPERTY(instance, propName, setterName, type) \ - do { \ - CHECK_READONLY_PROPERTY(instance, propName, type); \ - NSMethodSignature * signature = [instance methodSignatureForSelector:@selector(setterName:)]; \ - CHECK_RETURN_TYPE(signature, void); \ - CHECK_ARGUMENT(signature, 0, type); \ +#define CHECK_PROPERTY(instance, propName, setterName, type) \ + do { \ + CHECK_READONLY_PROPERTY(instance, propName, type); \ + NSMethodSignature * signature = [instance methodSignatureForSelector:@selector(setterName:)]; \ + CHECK_RETURN_TYPE(signature, void); \ + CHECK_ARGUMENT(signature, 0, type); \ } while (0) /** @@ -1212,10 +1174,4 @@ - (void)test048_MTRModeSelectClusterSemanticTagStruct CHECK_PROPERTY(obj, value, setValue, NSNumber *); } -- (void)test999_TearDown -{ - ResetCommissionee(GetConnectedDevice(), dispatch_get_main_queue(), self, kTimeoutInSeconds); - [[self class] shutdownStack]; -} - @end diff --git a/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m b/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m index 2acd1c2d379d83..94537c5d8d8934 100644 --- a/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m +++ b/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m @@ -23,6 +23,11 @@ #import "MTRTestKeys.h" #import "MTRTestStorage.h" +// Fixture 1: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1 +// Fixture 2: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1 \ + --dac_provider credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json \ + --product-id 32768 --discriminator 3839 + static const uint16_t kLocalPort = 5541; static const uint16_t kTestVendorId = 0xFFF1u; static const uint16_t kTestProductId1 = 0x8000u; @@ -118,34 +123,9 @@ @interface MTRCommissionableBrowserTests : XCTestCase @implementation MTRCommissionableBrowserTests -+ (void)tearDown -{ - // Global teardown, runs once - if (sNeedsStackShutdown) { - [self shutdownStack]; - } -} - -- (void)setUp ++ (void)setUp { - // Per-test setup, runs before each test. [super setUp]; - [self setContinueAfterFailure:NO]; - - if (sStackInitRan == NO) { - [self initStack]; - } -} - -- (void)tearDown -{ - // Per-test teardown, runs after each test. - [super tearDown]; -} - -- (void)initStack -{ - sStackInitRan = YES; __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); @@ -169,17 +149,22 @@ - (void)initStack sController = controller; } -+ (void)shutdownStack ++ (void)tearDown { - sNeedsStackShutdown = NO; - MTRDeviceController * controller = sController; XCTAssertNotNil(controller); - [controller shutdown]; XCTAssertFalse([controller isRunning]); [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; + + [super tearDown]; +} + +- (void)setUp +{ + [super setUp]; + [self setContinueAfterFailure:NO]; } - (void)test001_StartBrowseAndStopBrowse @@ -248,9 +233,4 @@ - (void)test005_StartBrowseGetCommissionableOverMdns XCTAssertTrue([sController stopBrowseForCommissionables]); } -- (void)test999_TearDown -{ - [[self class] shutdownStack]; -} - @end diff --git a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m index 4fbe535e46a942..97899653057e6c 100644 --- a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m +++ b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m @@ -38,6 +38,8 @@ // system dependencies #import +// Fixture: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1 + static const uint16_t kPairingTimeoutInSeconds = 10; static const uint16_t kTimeoutInSeconds = 3; static const uint64_t kDeviceId = 0x12344321; @@ -127,46 +129,11 @@ - (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSEr @interface MTRDeviceTests : XCTestCase @end -static BOOL sStackInitRan = NO; -static BOOL sNeedsStackShutdown = YES; - @implementation MTRDeviceTests -+ (void)tearDown -{ - // Global teardown, runs once - if (sNeedsStackShutdown) { - // We don't need to worry about ResetCommissionee. If we get here, - // we're running only one of our test methods (using - // -only-testing:MatterTests/MTROTAProviderTests/testMethodName), since - // we did not run test999_TearDown. - [self shutdownStack]; - } -} - -- (void)setUp -{ - // Per-test setup, runs before each test. - [super setUp]; - [self setContinueAfterFailure:NO]; - - if (sStackInitRan == NO) { - [self initStack]; - [self waitForCommissionee]; - } -} - -- (void)tearDown ++ (void)setUp { - // Per-test teardown, runs after each test. - [super tearDown]; -} - -- (void)initStack -{ - sStackInitRan = YES; - - XCTestExpectation * expectation = [self expectationWithDescription:@"Pairing Complete"]; + XCTestExpectation * pairingExpectation = [[XCTestExpectation alloc] initWithDescription:@"Pairing Complete"]; __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); @@ -194,7 +161,7 @@ - (void)initStack sController = controller; MTRDeviceTestDeviceControllerDelegate * deviceControllerDelegate = - [[MTRDeviceTestDeviceControllerDelegate alloc] initWithExpectation:expectation]; + [[MTRDeviceTestDeviceControllerDelegate alloc] initWithExpectation:pairingExpectation]; dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.device_controller_delegate", DISPATCH_QUEUE_SERIAL); [controller setDeviceControllerDelegate:deviceControllerDelegate queue:callbackQueue]; @@ -204,38 +171,33 @@ - (void)initStack XCTAssertNotNil(payload); XCTAssertNil(error); - [controller setupCommissioningSessionWithPayload:payload newNodeID:@(kDeviceId) error:&error]; + XCTAssertTrue([controller setupCommissioningSessionWithPayload:payload newNodeID:@(kDeviceId) error:&error]); XCTAssertNil(error); + XCTAssertEqual([XCTWaiter waitForExpectations:@[ pairingExpectation ] timeout:kPairingTimeoutInSeconds], XCTWaiterResultCompleted); - [self waitForExpectationsWithTimeout:kPairingTimeoutInSeconds handler:nil]; + XCTestExpectation * expectation = [[XCTestExpectation alloc] initWithDescription:@"Wait for the commissioned device to be retrieved"]; + WaitForCommissionee(expectation); + XCTAssertEqual([XCTWaiter waitForExpectations:@[ expectation ] timeout:kTimeoutInSeconds], XCTWaiterResultCompleted); } -+ (void)shutdownStack ++ (void)tearDown { - sNeedsStackShutdown = NO; + ResetCommissionee(GetConnectedDevice(), dispatch_get_main_queue(), nil, kTimeoutInSeconds); MTRDeviceController * controller = sController; XCTAssertNotNil(controller); - [controller shutdown]; XCTAssertFalse([controller isRunning]); [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; -} - -- (void)waitForCommissionee -{ - XCTestExpectation * expectation = [self expectationWithDescription:@"Wait for the commissioned device to be retrieved"]; - WaitForCommissionee(expectation); - [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; + [super tearDown]; } -- (void)test000_SetUp +- (void)setUp { - // Nothing to do here; our setUp method handled this already. This test - // just exists to make the setup not look like it's happening inside other - // tests. + [super setUp]; + [self setContinueAfterFailure:NO]; } - (void)test001_ReadAttribute @@ -2888,12 +2850,6 @@ - (void)test030_DeviceAndClusterProperties XCTAssertEqualObjects(cluster.endpointID, @(0)); } -- (void)test999_TearDown -{ - ResetCommissionee(GetConnectedDevice(), dispatch_get_main_queue(), self, kTimeoutInSeconds); - [[self class] shutdownStack]; -} - @end @interface MTRDeviceEncoderTests : XCTestCase diff --git a/src/darwin/Framework/CHIPTests/MTRPairingTests.m b/src/darwin/Framework/CHIPTests/MTRPairingTests.m index 94ce4e93131169..6757dea7ceb541 100644 --- a/src/darwin/Framework/CHIPTests/MTRPairingTests.m +++ b/src/darwin/Framework/CHIPTests/MTRPairingTests.m @@ -26,6 +26,10 @@ // system dependencies #import +// Fixture: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1 \ + --dac_provider credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json \ + --product-id 32768 --discriminator 3839 + static const uint16_t kPairingTimeoutInSeconds = 10; static const uint16_t kTimeoutInSeconds = 3; static uint64_t sDeviceId = 0x12344321; @@ -118,106 +122,32 @@ - (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSEr @end -// attestationDelegate and failSafeExtension can both be nil -static void DoPairingTest(XCTestCase * testcase, id attestationDelegate, NSNumber * failSafeExtension) -{ - // Don't reuse node ids, because that will confuse us. - ++sDeviceId; - XCTestExpectation * expectation = [testcase expectationWithDescription:@"Commissioning Complete"]; - __auto_type * controller = sController; - - __auto_type * controllerDelegate = [[MTRPairingTestControllerDelegate alloc] initWithExpectation:expectation - attestationDelegate:attestationDelegate - failSafeExtension:failSafeExtension]; - dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.pairing", DISPATCH_QUEUE_SERIAL); - - [controller setDeviceControllerDelegate:controllerDelegate queue:callbackQueue]; - - NSError * error; - __auto_type * payload = [MTRSetupPayload setupPayloadWithOnboardingPayload:kOnboardingPayload error:&error]; - XCTAssertNotNil(payload); - XCTAssertNil(error); - - [controller setupCommissioningSessionWithPayload:payload newNodeID:@(sDeviceId) error:&error]; - XCTAssertNil(error); - - [testcase waitForExpectations:@[ expectation ] timeout:kPairingTimeoutInSeconds]; - - ResetCommissionee([MTRBaseDevice deviceWithNodeID:@(sDeviceId) controller:controller], dispatch_get_main_queue(), testcase, - kTimeoutInSeconds); -} - @interface MTRPairingTests : XCTestCase @end -static BOOL sStackInitRan = NO; -static BOOL sNeedsStackShutdown = YES; - @implementation MTRPairingTests -+ (void)tearDown -{ - // Global teardown, runs once - if (sNeedsStackShutdown) { - // We don't need to worry about ResetCommissionee. If we get here, - // we're running only one of our test methods (using - // -only-testing:MatterTests/MTROTAProviderTests/testMethodName), since - // we did not run test999_TearDown. - [self shutdownStack]; - } -} - -- (void)setUp -{ - // Per-test setup, runs before each test. - [super setUp]; - [self setContinueAfterFailure:NO]; - - if (sStackInitRan == NO) { - [self initStack]; - } -} - -- (void)tearDown -{ - // Per-test teardown, runs after each test. - [super tearDown]; -} - -- (void)initStack ++ (void)setUp { - sStackInitRan = YES; - __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; factoryParams.port = @(kLocalPort); + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); - BOOL ok = [factory startControllerFactory:factoryParams error:nil]; - XCTAssertTrue(ok); - - __auto_type * testKeys = [[MTRTestKeys alloc] init]; - XCTAssertNotNil(testKeys); - - sTestKeys = testKeys; + XCTAssertNotNil(sTestKeys = [[MTRTestKeys alloc] init]); // Needs to match what startControllerOnExistingFabric calls elsewhere in // this file do. - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:sTestKeys.ipk fabricID:@(1) nocSigner:sTestKeys]; params.vendorID = @(kTestVendorId); - - MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; - XCTAssertNotNil(controller); - - sController = controller; + XCTAssertNotNil(sController = [factory createControllerOnNewFabric:params error:nil]); } -+ (void)shutdownStack ++ (void)tearDown { - sNeedsStackShutdown = NO; - MTRDeviceController * controller = sController; XCTAssertNotNil(controller); @@ -227,23 +157,51 @@ + (void)shutdownStack [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; } -- (void)test000_SetUp +- (void)setUp { - // Nothing to do here; our setUp method handled this already. This test - // just exists to make the setup not look like it's happening inside other - // tests. + [super setUp]; + [self setContinueAfterFailure:NO]; +} + +// attestationDelegate and failSafeExtension can both be nil +- (void)doPairingTestWithAttestationDelegate:(id)attestationDelegate failSafeExtension:(NSNumber *)failSafeExtension +{ + // Don't reuse node ids, because that will confuse us. + ++sDeviceId; + XCTestExpectation * expectation = [self expectationWithDescription:@"Commissioning Complete"]; + + __auto_type * controllerDelegate = [[MTRPairingTestControllerDelegate alloc] initWithExpectation:expectation + attestationDelegate:attestationDelegate + failSafeExtension:failSafeExtension]; + dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.pairing", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL); + + [sController setDeviceControllerDelegate:controllerDelegate queue:callbackQueue]; + + NSError * error; + __auto_type * payload = [MTRSetupPayload setupPayloadWithOnboardingPayload:kOnboardingPayload error:&error]; + XCTAssertNotNil(payload); + XCTAssertNil(error); + + XCTAssertTrue([sController setupCommissioningSessionWithPayload:payload newNodeID:@(sDeviceId) error:&error]); + XCTAssertNil(error); + + [self waitForExpectations:@[ expectation ] timeout:kPairingTimeoutInSeconds]; + + ResetCommissionee([MTRBaseDevice deviceWithNodeID:@(sDeviceId) controller:sController], dispatch_get_main_queue(), self, + kTimeoutInSeconds); } - (void)test001_PairWithoutAttestationDelegate { - DoPairingTest(self, nil, nil); + [self doPairingTestWithAttestationDelegate:nil failSafeExtension:nil]; } - (void)test002_PairWithAttestationDelegateNoFailsafeExtension { XCTestExpectation * expectation = [self expectationWithDescription:@"Attestation delegate called"]; - DoPairingTest(self, [[NoOpAttestationDelegate alloc] initWithExpectation:expectation], nil); + [self doPairingTestWithAttestationDelegate:[[NoOpAttestationDelegate alloc] initWithExpectation:expectation] + failSafeExtension:nil]; [self waitForExpectations:@[ expectation ] timeout:kTimeoutInSeconds]; } @@ -255,7 +213,8 @@ - (void)test003_PairWithAttestationDelegateFailsafeExtensionShort // Extend by a time that is going to be smaller than the 60s default we // already have set via CHIP_DEVICE_CONFIG_FAILSAFE_EXPIRY_LENGTH_SEC on the // server side, minus whatever time that has likely passed. - DoPairingTest(self, [[NoOpAttestationDelegate alloc] initWithExpectation:expectation], @(30)); + [self doPairingTestWithAttestationDelegate:[[NoOpAttestationDelegate alloc] initWithExpectation:expectation] + failSafeExtension:@(30)]; [self waitForExpectations:@[ expectation ] timeout:kTimeoutInSeconds]; } @@ -267,14 +226,10 @@ - (void)test004_PairWithAttestationDelegateFailsafeExtensionLong // Extend by a time that is going to be larger than the 60s default we // already have set via CHIP_DEVICE_CONFIG_FAILSAFE_EXPIRY_LENGTH_SEC on the // server side. - DoPairingTest(self, [[NoOpAttestationDelegate alloc] initWithExpectation:expectation], @(90)); + [self doPairingTestWithAttestationDelegate:[[NoOpAttestationDelegate alloc] initWithExpectation:expectation] + failSafeExtension:@(90)]; [self waitForExpectations:@[ expectation ] timeout:kTimeoutInSeconds]; } -- (void)test999_TearDown -{ - [[self class] shutdownStack]; -} - @end diff --git a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m index ae2503bfeb42e2..29a4b4d321099f 100644 --- a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m +++ b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m @@ -204,10 +204,6 @@ @implementation MTRPerControllerStorageTests { dispatch_queue_t _storageQueue; } -+ (void)tearDown -{ -} - - (void)setUp { // Per-test setup, runs before each test. diff --git a/src/darwin/Framework/CHIPTests/MTRSwiftDeviceTests.swift b/src/darwin/Framework/CHIPTests/MTRSwiftDeviceTests.swift index 4a35a431347045..1276868a14c5fa 100644 --- a/src/darwin/Framework/CHIPTests/MTRSwiftDeviceTests.swift +++ b/src/darwin/Framework/CHIPTests/MTRSwiftDeviceTests.swift @@ -3,6 +3,8 @@ import XCTest // This should eventually grow into a Swift copy of MTRDeviceTests +// Fixture: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1 + struct DeviceConstants { static let testVendorID = 0xFFF1 static let onboardingPayload = "MT:-24J0AFN00KA0648G00" @@ -93,41 +95,10 @@ class MTRSwiftDeviceTestDelegate : NSObject, MTRDeviceDelegate { } class MTRSwiftDeviceTests : XCTestCase { - static var sStackInitRan : Bool = false - static var sNeedsStackShutdown : Bool = true - - static override func tearDown() { - // Global teardown, runs once - if (sNeedsStackShutdown) { - // We don't need to worry about ResetCommissionee. If we get here, - // we're running only one of our test methods (using - // -only-testing:MatterTests/MTRSwiftDeviceTests/testMethodName), since - // we did not run test999_TearDown. - shutdownStack() - } - } - - override func setUp() + static override func setUp() { - // Per-test setup, runs before each test. super.setUp() - self.continueAfterFailure = false - - if (!MTRSwiftDeviceTests.sStackInitRan) { - initStack() - } - } - - override func tearDown() - { - // Per-test teardown, runs after each test. - super.tearDown() - } - - func initStack() - { - MTRSwiftDeviceTests.sStackInitRan = true - + let factory = MTRDeviceControllerFactory.sharedInstance() let storage = MTRTestStorage() @@ -141,7 +112,6 @@ class MTRSwiftDeviceTests : XCTestCase { XCTAssertTrue(factory.isRunning) let testKeys = MTRTestKeys() - sTestKeys = testKeys // Needs to match what startControllerOnExistingFabric calls elsewhere in @@ -160,8 +130,8 @@ class MTRSwiftDeviceTests : XCTestCase { sController = controller - let expectation = expectation(description : "Commissioning Complete") - + let expectation = XCTestExpectation(description : "Commissioning Complete") + let controllerDelegate = MTRSwiftDeviceTestControllerDelegate(withExpectation: expectation) let serialQueue = DispatchQueue(label: "com.chip.device_controller_delegate") @@ -180,30 +150,29 @@ class MTRSwiftDeviceTests : XCTestCase { } catch { XCTFail("Could not start setting up PASE session: \(error)") return } - - wait(for: [expectation], timeout: DeviceConstants.pairingTimeoutInSeconds) + + XCTAssertEqual(XCTWaiter.wait(for: [expectation], timeout: DeviceConstants.pairingTimeoutInSeconds), XCTWaiter.Result.completed) } - static func shutdownStack() - { - sNeedsStackShutdown = false - + static override func tearDown() { + ResetCommissionee(sConnectedDevice, DispatchQueue.main, nil, UInt16(DeviceConstants.timeoutInSeconds)) + let controller = sController XCTAssertNotNil(controller) - controller!.shutdown() XCTAssertFalse(controller!.isRunning) MTRDeviceControllerFactory.sharedInstance().stop() + + super.tearDown() } - - func test000_SetUp() + + override func setUp() { - // Nothing to do here; our setUp method handled this already. This test - // just exists to make the setup not look like it's happening inside other - // tests. + super.setUp() + self.continueAfterFailure = false } - + func test017_TestMTRDeviceBasics() { let device = MTRDevice(nodeID: DeviceConstants.deviceID as NSNumber, controller:sController!) @@ -545,10 +514,4 @@ class MTRSwiftDeviceTests : XCTestCase { XCTAssertEqual(cluster.device, device) XCTAssertEqual(cluster.__endpointID, 0 as NSNumber) } - - func test999_TearDown() - { - ResetCommissionee(sConnectedDevice, DispatchQueue.main, self, UInt16(DeviceConstants.timeoutInSeconds)) - type(of: self).shutdownStack() - } } diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestResetCommissioneeHelper.m b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestResetCommissioneeHelper.m index 25ec161420bcfe..841a1ce656fa0b 100644 --- a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestResetCommissioneeHelper.m +++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestResetCommissioneeHelper.m @@ -16,11 +16,11 @@ #import "MTRTestResetCommissioneeHelper.h" -void ResetCommissionee(MTRBaseDevice * device, dispatch_queue_t queue, XCTestCase * testcase, uint16_t commandTimeout) +void ResetCommissionee(MTRBaseDevice * device, dispatch_queue_t queue, XCTestCase * testcaseUnused, uint16_t commandTimeout) { // Put the device back in the state we found it: open commissioning window, no fabrics commissioned. // Get our current fabric index, for later deletion. - XCTestExpectation * readFabricIndexExpectation = [testcase expectationWithDescription:@"Fabric index read"]; + XCTestExpectation * readFabricIndexExpectation = [[XCTestExpectation alloc] initWithDescription:@"Fabric index read"]; __block NSNumber * fabricIndex; __auto_type * opCredsCluster = [[MTRBaseClusterOperationalCredentials alloc] initWithDevice:device endpoint:0 queue:queue]; @@ -32,11 +32,10 @@ void ResetCommissionee(MTRBaseDevice * device, dispatch_queue_t queue, XCTestCas [readFabricIndexExpectation fulfill]; }]; - [testcase waitForExpectations:@[ readFabricIndexExpectation ] timeout:commandTimeout]; + XCTAssertEqual([XCTWaiter waitForExpectations:@[ readFabricIndexExpectation ] timeout:commandTimeout], XCTWaiterResultCompleted); // Open a commissioning window. - XCTestExpectation * openCommissioningWindowExpectation = [testcase expectationWithDescription:@"Commissioning window opened"]; - + XCTestExpectation * openCommissioningWindowExpectation = [[XCTestExpectation alloc] initWithDescription:@"Commissioning window opened"]; __auto_type * adminCommissioningCluster = [[MTRBaseClusterAdministratorCommissioning alloc] initWithDevice:device endpoint:0 queue:queue]; @@ -49,10 +48,10 @@ void ResetCommissionee(MTRBaseDevice * device, dispatch_queue_t queue, XCTestCas [openCommissioningWindowExpectation fulfill]; }]; - [testcase waitForExpectations:@[ openCommissioningWindowExpectation ] timeout:commandTimeout]; + XCTAssertEqual([XCTWaiter waitForExpectations:@[ openCommissioningWindowExpectation ] timeout:commandTimeout], XCTWaiterResultCompleted); // Remove our fabric from the device. - XCTestExpectation * removeFabricExpectation = [testcase expectationWithDescription:@"Fabric removed"]; + XCTestExpectation * removeFabricExpectation = [[XCTestExpectation alloc] initWithDescription:@"Fabric removed"]; __auto_type * removeParams = [[MTROperationalCredentialsClusterRemoveFabricParams alloc] init]; removeParams.fabricIndex = fabricIndex; @@ -66,5 +65,5 @@ void ResetCommissionee(MTRBaseDevice * device, dispatch_queue_t queue, XCTestCas [removeFabricExpectation fulfill]; }]; - [testcase waitForExpectations:@[ removeFabricExpectation ] timeout:commandTimeout]; + XCTAssertEqual([XCTWaiter waitForExpectations:@[ removeFabricExpectation ] timeout:commandTimeout], XCTWaiterResultCompleted); } From 1eb28b7f1093c7a32075bdd7bed005698676078c Mon Sep 17 00:00:00 2001 From: Karsten Sperling Date: Tue, 13 Feb 2024 10:00:18 +1300 Subject: [PATCH 2/2] fix unused variables --- src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m b/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m index 94537c5d8d8934..047ce4f8588816 100644 --- a/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m +++ b/src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m @@ -118,9 +118,6 @@ - (void)controller:(MTRDeviceController *)controller didRemoveCommissionableDevi @interface MTRCommissionableBrowserTests : XCTestCase @end -static BOOL sStackInitRan = NO; -static BOOL sNeedsStackShutdown = YES; - @implementation MTRCommissionableBrowserTests + (void)setUp