Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Darwin: Simplify test setup and teardown #32061

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 40 additions & 84 deletions src/darwin/Framework/CHIPTests/MTRBackwardsCompatTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
// system dependencies
#import <XCTest/XCTest.h>

// 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;
Expand Down Expand Up @@ -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
Expand All @@ -91,64 +93,27 @@ - (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"];
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved

__auto_type * factory = [MTRControllerFactory sharedInstance];
XCTAssertNotNil(factory);

__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];
Expand All @@ -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) {
Expand All @@ -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)

/**
Expand Down Expand Up @@ -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
53 changes: 15 additions & 38 deletions src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,39 +118,11 @@ - (void)controller:(MTRDeviceController *)controller didRemoveCommissionableDevi
@interface MTRCommissionableBrowserTests : XCTestCase
@end

static BOOL sStackInitRan = NO;
static BOOL sNeedsStackShutdown = YES;

@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);
Expand All @@ -169,17 +146,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
Expand Down Expand Up @@ -248,9 +230,4 @@ - (void)test005_StartBrowseGetCommissionableOverMdns
XCTAssertTrue([sController stopBrowseForCommissionables]);
}

- (void)test999_TearDown
{
[[self class] shutdownStack];
}

@end
Loading
Loading